Fix copyright year
[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"
35#include "gdbthread.h"
36#include "target.h"
37#include "language.h"
0e9f083f 38#include <string.h>
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
0225421b 50#include "gdb_assert.h"
fe898f56 51#include "block.h"
a77053c2 52#include "solib.h"
84acb35a
JJ
53#include "solist.h"
54#include "observer.h"
60250e8b 55#include "exceptions.h"
765dc015 56#include "memattr.h"
f7f9143b 57#include "ada-lang.h"
d1aa2f50 58#include "top.h"
79a45b7d 59#include "valprint.h"
4efc6507 60#include "jit.h"
a96d9b2e 61#include "xml-syscall.h"
65d79d4b 62#include "parser-defs.h"
55aa24fb
SDJ
63#include "gdb_regex.h"
64#include "probe.h"
e9cafbcc 65#include "cli/cli-utils.h"
be34f849 66#include "continuations.h"
1bfeeb0f
JL
67#include "stack.h"
68#include "skip.h"
b775012e 69#include "ax-gdb.h"
e2e4d78b 70#include "dummy-frame.h"
c906108c 71
d3ce09f5
SS
72#include "format.h"
73
1042e4c0
SS
74/* readline include files */
75#include "readline/readline.h"
76#include "readline/history.h"
77
78/* readline defines this. */
79#undef savestring
80
034dad6f 81#include "mi/mi-common.h"
7371cf6d 82#include "python/python.h"
104c1213 83
e7e8980f
YQ
84/* Enums for exception-handling support. */
85enum exception_event_kind
86{
87 EX_EVENT_THROW,
591f19e8 88 EX_EVENT_RETHROW,
e7e8980f
YQ
89 EX_EVENT_CATCH
90};
91
4a64f543 92/* Prototypes for local functions. */
c906108c 93
a14ed312 94static void enable_delete_command (char *, int);
c906108c 95
a14ed312 96static void enable_once_command (char *, int);
c906108c 97
816338b5
SS
98static void enable_count_command (char *, int);
99
a14ed312 100static void disable_command (char *, int);
c906108c 101
a14ed312 102static void enable_command (char *, int);
c906108c 103
95a42b64
TT
104static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
105 void *),
106 void *);
c906108c 107
a14ed312 108static void ignore_command (char *, int);
c906108c 109
4efb68b1 110static int breakpoint_re_set_one (void *);
c906108c 111
348d480f
PA
112static void breakpoint_re_set_default (struct breakpoint *);
113
983af33b
SDJ
114static void create_sals_from_address_default (char **,
115 struct linespec_result *,
116 enum bptype, char *,
117 char **);
118
119static void create_breakpoints_sal_default (struct gdbarch *,
120 struct linespec_result *,
e7e0cddf 121 char *, char *, enum bptype,
983af33b
SDJ
122 enum bpdisp, int, int,
123 int,
124 const struct breakpoint_ops *,
44f238bb 125 int, int, int, unsigned);
983af33b
SDJ
126
127static void decode_linespec_default (struct breakpoint *, char **,
128 struct symtabs_and_lines *);
129
a14ed312 130static void clear_command (char *, int);
c906108c 131
a14ed312 132static void catch_command (char *, int);
c906108c 133
a9634178 134static int can_use_hardware_watchpoint (struct value *);
c906108c 135
98deb0da 136static void break_command_1 (char *, int, int);
c906108c 137
a14ed312 138static void mention (struct breakpoint *);
c906108c 139
348d480f
PA
140static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
141 enum bptype,
c0a91b2b 142 const struct breakpoint_ops *);
3742cc8b
YQ
143static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
144 const struct symtab_and_line *);
145
4a64f543
MS
146/* This function is used in gdbtk sources and thus can not be made
147 static. */
63c252f8 148struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 149 struct symtab_and_line,
c0a91b2b
TT
150 enum bptype,
151 const struct breakpoint_ops *);
c906108c 152
06edf0c0
PA
153static struct breakpoint *
154 momentary_breakpoint_from_master (struct breakpoint *orig,
155 enum bptype type,
c0a91b2b 156 const struct breakpoint_ops *ops);
06edf0c0 157
76897487
KB
158static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
159
a6d9a66e
UW
160static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
161 CORE_ADDR bpaddr,
88f7da05 162 enum bptype bptype);
76897487 163
6c95b8df
PA
164static void describe_other_breakpoints (struct gdbarch *,
165 struct program_space *, CORE_ADDR,
5af949e3 166 struct obj_section *, int);
c906108c 167
6c95b8df
PA
168static int breakpoint_address_match (struct address_space *aspace1,
169 CORE_ADDR addr1,
170 struct address_space *aspace2,
171 CORE_ADDR addr2);
172
85d721b8
PA
173static int watchpoint_locations_match (struct bp_location *loc1,
174 struct bp_location *loc2);
175
f1310107
TJB
176static int breakpoint_location_address_match (struct bp_location *bl,
177 struct address_space *aspace,
178 CORE_ADDR addr);
179
a14ed312 180static void breakpoints_info (char *, int);
c906108c 181
d77f58be
SS
182static void watchpoints_info (char *, int);
183
e5a67952
MS
184static int breakpoint_1 (char *, int,
185 int (*) (const struct breakpoint *));
c906108c 186
4efb68b1 187static int breakpoint_cond_eval (void *);
c906108c 188
4efb68b1 189static void cleanup_executing_breakpoints (void *);
c906108c 190
a14ed312 191static void commands_command (char *, int);
c906108c 192
a14ed312 193static void condition_command (char *, int);
c906108c 194
c5aa993b
JM
195typedef enum
196 {
197 mark_inserted,
198 mark_uninserted
199 }
200insertion_state_t;
c906108c 201
0bde7532 202static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 203static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 204
e514a9d6 205static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 206
4efb68b1 207static int watchpoint_check (void *);
c906108c 208
a14ed312 209static void maintenance_info_breakpoints (char *, int);
c906108c 210
a14ed312 211static int hw_breakpoint_used_count (void);
c906108c 212
a1398e0c
PA
213static int hw_watchpoint_use_count (struct breakpoint *);
214
215static int hw_watchpoint_used_count_others (struct breakpoint *except,
216 enum bptype type,
217 int *other_type_used);
c906108c 218
a14ed312 219static void hbreak_command (char *, int);
c906108c 220
a14ed312 221static void thbreak_command (char *, int);
c906108c 222
816338b5
SS
223static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
224 int count);
c906108c 225
a14ed312 226static void stop_command (char *arg, int from_tty);
7a292a7a 227
a14ed312 228static void stopin_command (char *arg, int from_tty);
7a292a7a 229
a14ed312 230static void stopat_command (char *arg, int from_tty);
7a292a7a 231
a14ed312 232static void tcatch_command (char *arg, int from_tty);
7a292a7a 233
d03285ec
UW
234static void detach_single_step_breakpoints (void);
235
6c95b8df
PA
236static int single_step_breakpoint_inserted_here_p (struct address_space *,
237 CORE_ADDR pc);
1aafd4da 238
fe3f5fa8 239static void free_bp_location (struct bp_location *loc);
f431efe5
PA
240static void incref_bp_location (struct bp_location *loc);
241static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 242
39d61571 243static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 244
b60e7edf 245static void update_global_location_list (int);
a5606eee 246
b60e7edf 247static void update_global_location_list_nothrow (int);
74960c60 248
d77f58be 249static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
250
251static void insert_breakpoint_locations (void);
a5606eee 252
a96d9b2e
SDJ
253static int syscall_catchpoint_p (struct breakpoint *b);
254
1042e4c0
SS
255static void tracepoints_info (char *, int);
256
257static void delete_trace_command (char *, int);
258
259static void enable_trace_command (char *, int);
260
261static void disable_trace_command (char *, int);
262
263static void trace_pass_command (char *, int);
264
558a9d82
YQ
265static void set_tracepoint_count (int num);
266
9c06b0b4
TJB
267static int is_masked_watchpoint (const struct breakpoint *b);
268
b775012e
LM
269static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
270
983af33b
SDJ
271/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
272 otherwise. */
273
274static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 275
2060206e
PA
276/* The abstract base class all breakpoint_ops structures inherit
277 from. */
ab04a2af 278struct breakpoint_ops base_breakpoint_ops;
2060206e
PA
279
280/* The breakpoint_ops structure to be inherited by all breakpoint_ops
281 that are implemented on top of software or hardware breakpoints
282 (user breakpoints, internal and momentary breakpoints, etc.). */
283static struct breakpoint_ops bkpt_base_breakpoint_ops;
284
285/* Internal breakpoints class type. */
06edf0c0 286static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
287
288/* Momentary breakpoints class type. */
06edf0c0
PA
289static struct breakpoint_ops momentary_breakpoint_ops;
290
e2e4d78b
JK
291/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
292static struct breakpoint_ops longjmp_breakpoint_ops;
293
2060206e
PA
294/* The breakpoint_ops structure to be used in regular user created
295 breakpoints. */
296struct breakpoint_ops bkpt_breakpoint_ops;
297
55aa24fb
SDJ
298/* Breakpoints set on probes. */
299static struct breakpoint_ops bkpt_probe_breakpoint_ops;
300
e7e0cddf 301/* Dynamic printf class type. */
c5867ab6 302struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 303
d3ce09f5
SS
304/* The style in which to perform a dynamic printf. This is a user
305 option because different output options have different tradeoffs;
306 if GDB does the printing, there is better error handling if there
307 is a problem with any of the arguments, but using an inferior
308 function lets you have special-purpose printers and sending of
309 output to the same place as compiled-in print functions. */
310
311static const char dprintf_style_gdb[] = "gdb";
312static const char dprintf_style_call[] = "call";
313static const char dprintf_style_agent[] = "agent";
314static const char *const dprintf_style_enums[] = {
315 dprintf_style_gdb,
316 dprintf_style_call,
317 dprintf_style_agent,
318 NULL
319};
320static const char *dprintf_style = dprintf_style_gdb;
321
322/* The function to use for dynamic printf if the preferred style is to
323 call into the inferior. The value is simply a string that is
324 copied into the command, so it can be anything that GDB can
325 evaluate to a callable address, not necessarily a function name. */
326
327static char *dprintf_function = "";
328
329/* The channel to use for dynamic printf if the preferred style is to
330 call into the inferior; if a nonempty string, it will be passed to
331 the call as the first argument, with the format string as the
332 second. As with the dprintf function, this can be anything that
333 GDB knows how to evaluate, so in addition to common choices like
334 "stderr", this could be an app-specific expression like
335 "mystreams[curlogger]". */
336
337static char *dprintf_channel = "";
338
339/* True if dprintf commands should continue to operate even if GDB
340 has disconnected. */
341static int disconnected_dprintf = 1;
342
5cea2a26
PA
343/* A reference-counted struct command_line. This lets multiple
344 breakpoints share a single command list. */
345struct counted_command_line
346{
347 /* The reference count. */
348 int refc;
349
350 /* The command list. */
351 struct command_line *commands;
352};
353
354struct command_line *
355breakpoint_commands (struct breakpoint *b)
356{
357 return b->commands ? b->commands->commands : NULL;
358}
3daf8fe5 359
f3b1572e
PA
360/* Flag indicating that a command has proceeded the inferior past the
361 current breakpoint. */
362
363static int breakpoint_proceeded;
364
956a9fb9 365const char *
2cec12e5
AR
366bpdisp_text (enum bpdisp disp)
367{
4a64f543
MS
368 /* NOTE: the following values are a part of MI protocol and
369 represent values of 'disp' field returned when inferior stops at
370 a breakpoint. */
bc043ef3 371 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 372
2cec12e5
AR
373 return bpdisps[(int) disp];
374}
c906108c 375
4a64f543 376/* Prototypes for exported functions. */
c906108c 377/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 378 if such is available. */
c906108c
SS
379static int can_use_hw_watchpoints;
380
920d2a44
AC
381static void
382show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
383 struct cmd_list_element *c,
384 const char *value)
385{
3e43a32a
MS
386 fprintf_filtered (file,
387 _("Debugger's willingness to use "
388 "watchpoint hardware is %s.\n"),
920d2a44
AC
389 value);
390}
391
fa8d40ab
JJ
392/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
393 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 394 for unrecognized breakpoint locations.
fa8d40ab
JJ
395 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
396static enum auto_boolean pending_break_support;
920d2a44
AC
397static void
398show_pending_break_support (struct ui_file *file, int from_tty,
399 struct cmd_list_element *c,
400 const char *value)
401{
3e43a32a
MS
402 fprintf_filtered (file,
403 _("Debugger's behavior regarding "
404 "pending breakpoints is %s.\n"),
920d2a44
AC
405 value);
406}
fa8d40ab 407
765dc015 408/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 409 set with "break" but falling in read-only memory.
765dc015
VP
410 If 0, gdb will warn about such breakpoints, but won't automatically
411 use hardware breakpoints. */
412static int automatic_hardware_breakpoints;
413static void
414show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
415 struct cmd_list_element *c,
416 const char *value)
417{
3e43a32a
MS
418 fprintf_filtered (file,
419 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
420 value);
421}
422
33e5cbd6
PA
423/* If on, gdb will keep breakpoints inserted even as inferior is
424 stopped, and immediately insert any new breakpoints. If off, gdb
425 will insert breakpoints into inferior only when resuming it, and
426 will remove breakpoints upon stop. If auto, GDB will behave as ON
427 if in non-stop mode, and as OFF if all-stop mode.*/
428
72d0e2c5
YQ
429static enum auto_boolean always_inserted_mode = AUTO_BOOLEAN_AUTO;
430
33e5cbd6 431static void
74960c60 432show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 433 struct cmd_list_element *c, const char *value)
74960c60 434{
72d0e2c5 435 if (always_inserted_mode == AUTO_BOOLEAN_AUTO)
3e43a32a
MS
436 fprintf_filtered (file,
437 _("Always inserted breakpoint "
438 "mode is %s (currently %s).\n"),
33e5cbd6
PA
439 value,
440 breakpoints_always_inserted_mode () ? "on" : "off");
441 else
3e43a32a
MS
442 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
443 value);
74960c60
VP
444}
445
33e5cbd6
PA
446int
447breakpoints_always_inserted_mode (void)
448{
72d0e2c5
YQ
449 return (always_inserted_mode == AUTO_BOOLEAN_TRUE
450 || (always_inserted_mode == AUTO_BOOLEAN_AUTO && non_stop));
33e5cbd6 451}
765dc015 452
b775012e
LM
453static const char condition_evaluation_both[] = "host or target";
454
455/* Modes for breakpoint condition evaluation. */
456static const char condition_evaluation_auto[] = "auto";
457static const char condition_evaluation_host[] = "host";
458static const char condition_evaluation_target[] = "target";
459static const char *const condition_evaluation_enums[] = {
460 condition_evaluation_auto,
461 condition_evaluation_host,
462 condition_evaluation_target,
463 NULL
464};
465
466/* Global that holds the current mode for breakpoint condition evaluation. */
467static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
468
469/* Global that we use to display information to the user (gets its value from
470 condition_evaluation_mode_1. */
471static const char *condition_evaluation_mode = condition_evaluation_auto;
472
473/* Translate a condition evaluation mode MODE into either "host"
474 or "target". This is used mostly to translate from "auto" to the
475 real setting that is being used. It returns the translated
476 evaluation mode. */
477
478static const char *
479translate_condition_evaluation_mode (const char *mode)
480{
481 if (mode == condition_evaluation_auto)
482 {
483 if (target_supports_evaluation_of_breakpoint_conditions ())
484 return condition_evaluation_target;
485 else
486 return condition_evaluation_host;
487 }
488 else
489 return mode;
490}
491
492/* Discovers what condition_evaluation_auto translates to. */
493
494static const char *
495breakpoint_condition_evaluation_mode (void)
496{
497 return translate_condition_evaluation_mode (condition_evaluation_mode);
498}
499
500/* Return true if GDB should evaluate breakpoint conditions or false
501 otherwise. */
502
503static int
504gdb_evaluates_breakpoint_condition_p (void)
505{
506 const char *mode = breakpoint_condition_evaluation_mode ();
507
508 return (mode == condition_evaluation_host);
509}
510
a14ed312 511void _initialize_breakpoint (void);
c906108c 512
c906108c
SS
513/* Are we executing breakpoint commands? */
514static int executing_breakpoint_commands;
515
c02f5703
MS
516/* Are overlay event breakpoints enabled? */
517static int overlay_events_enabled;
518
e09342b5
TJB
519/* See description in breakpoint.h. */
520int target_exact_watchpoints = 0;
521
c906108c 522/* Walk the following statement or block through all breakpoints.
e5dd4106 523 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 524 current breakpoint. */
c906108c 525
5c44784c 526#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 527
5c44784c
JM
528#define ALL_BREAKPOINTS_SAFE(B,TMP) \
529 for (B = breakpoint_chain; \
530 B ? (TMP=B->next, 1): 0; \
531 B = TMP)
c906108c 532
4a64f543
MS
533/* Similar iterator for the low-level breakpoints. SAFE variant is
534 not provided so update_global_location_list must not be called
535 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 536
876fa593
JK
537#define ALL_BP_LOCATIONS(B,BP_TMP) \
538 for (BP_TMP = bp_location; \
539 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
540 BP_TMP++)
7cc221ef 541
b775012e
LM
542/* Iterates through locations with address ADDRESS for the currently selected
543 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
544 to where the loop should start from.
545 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
546 appropriate location to start with. */
547
548#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
549 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
550 BP_LOCP_TMP = BP_LOCP_START; \
551 BP_LOCP_START \
552 && (BP_LOCP_TMP < bp_location + bp_location_count \
553 && (*BP_LOCP_TMP)->address == ADDRESS); \
554 BP_LOCP_TMP++)
555
1042e4c0
SS
556/* Iterator for tracepoints only. */
557
558#define ALL_TRACEPOINTS(B) \
559 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 560 if (is_tracepoint (B))
1042e4c0 561
7cc221ef 562/* Chains of all breakpoints defined. */
c906108c
SS
563
564struct breakpoint *breakpoint_chain;
565
876fa593
JK
566/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
567
568static struct bp_location **bp_location;
569
570/* Number of elements of BP_LOCATION. */
571
572static unsigned bp_location_count;
573
4a64f543
MS
574/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
575 ADDRESS for the current elements of BP_LOCATION which get a valid
576 result from bp_location_has_shadow. You can use it for roughly
577 limiting the subrange of BP_LOCATION to scan for shadow bytes for
578 an address you need to read. */
876fa593
JK
579
580static CORE_ADDR bp_location_placed_address_before_address_max;
581
4a64f543
MS
582/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
583 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
584 BP_LOCATION which get a valid result from bp_location_has_shadow.
585 You can use it for roughly limiting the subrange of BP_LOCATION to
586 scan for shadow bytes for an address you need to read. */
876fa593
JK
587
588static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 589
4a64f543
MS
590/* The locations that no longer correspond to any breakpoint, unlinked
591 from bp_location array, but for which a hit may still be reported
592 by a target. */
20874c92
VP
593VEC(bp_location_p) *moribund_locations = NULL;
594
c906108c
SS
595/* Number of last breakpoint made. */
596
95a42b64
TT
597static int breakpoint_count;
598
86b17b60
PA
599/* The value of `breakpoint_count' before the last command that
600 created breakpoints. If the last (break-like) command created more
601 than one breakpoint, then the difference between BREAKPOINT_COUNT
602 and PREV_BREAKPOINT_COUNT is more than one. */
603static int prev_breakpoint_count;
c906108c 604
1042e4c0
SS
605/* Number of last tracepoint made. */
606
95a42b64 607static int tracepoint_count;
1042e4c0 608
6149aea9
PA
609static struct cmd_list_element *breakpoint_set_cmdlist;
610static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 611struct cmd_list_element *save_cmdlist;
6149aea9 612
468d015d
JJ
613/* Return whether a breakpoint is an active enabled breakpoint. */
614static int
615breakpoint_enabled (struct breakpoint *b)
616{
0d381245 617 return (b->enable_state == bp_enabled);
468d015d
JJ
618}
619
c906108c
SS
620/* Set breakpoint count to NUM. */
621
95a42b64 622static void
fba45db2 623set_breakpoint_count (int num)
c906108c 624{
86b17b60 625 prev_breakpoint_count = breakpoint_count;
c906108c 626 breakpoint_count = num;
4fa62494 627 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
628}
629
86b17b60
PA
630/* Used by `start_rbreak_breakpoints' below, to record the current
631 breakpoint count before "rbreak" creates any breakpoint. */
632static int rbreak_start_breakpoint_count;
633
95a42b64
TT
634/* Called at the start an "rbreak" command to record the first
635 breakpoint made. */
86b17b60 636
95a42b64
TT
637void
638start_rbreak_breakpoints (void)
639{
86b17b60 640 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
641}
642
643/* Called at the end of an "rbreak" command to record the last
644 breakpoint made. */
86b17b60 645
95a42b64
TT
646void
647end_rbreak_breakpoints (void)
648{
86b17b60 649 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
650}
651
4a64f543 652/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
653
654void
fba45db2 655clear_breakpoint_hit_counts (void)
c906108c
SS
656{
657 struct breakpoint *b;
658
659 ALL_BREAKPOINTS (b)
660 b->hit_count = 0;
661}
662
9add0f1b
TT
663/* Allocate a new counted_command_line with reference count of 1.
664 The new structure owns COMMANDS. */
665
666static struct counted_command_line *
667alloc_counted_command_line (struct command_line *commands)
668{
669 struct counted_command_line *result
670 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 671
9add0f1b
TT
672 result->refc = 1;
673 result->commands = commands;
674 return result;
675}
676
677/* Increment reference count. This does nothing if CMD is NULL. */
678
679static void
680incref_counted_command_line (struct counted_command_line *cmd)
681{
682 if (cmd)
683 ++cmd->refc;
684}
685
686/* Decrement reference count. If the reference count reaches 0,
687 destroy the counted_command_line. Sets *CMDP to NULL. This does
688 nothing if *CMDP is NULL. */
689
690static void
691decref_counted_command_line (struct counted_command_line **cmdp)
692{
693 if (*cmdp)
694 {
695 if (--(*cmdp)->refc == 0)
696 {
697 free_command_lines (&(*cmdp)->commands);
698 xfree (*cmdp);
699 }
700 *cmdp = NULL;
701 }
702}
703
704/* A cleanup function that calls decref_counted_command_line. */
705
706static void
707do_cleanup_counted_command_line (void *arg)
708{
709 decref_counted_command_line (arg);
710}
711
712/* Create a cleanup that calls decref_counted_command_line on the
713 argument. */
714
715static struct cleanup *
716make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
717{
718 return make_cleanup (do_cleanup_counted_command_line, cmdp);
719}
720
c906108c 721\f
48cb2d85
VP
722/* Return the breakpoint with the specified number, or NULL
723 if the number does not refer to an existing breakpoint. */
724
725struct breakpoint *
726get_breakpoint (int num)
727{
728 struct breakpoint *b;
729
730 ALL_BREAKPOINTS (b)
731 if (b->number == num)
732 return b;
733
734 return NULL;
735}
5c44784c 736
c906108c 737\f
adc36818 738
b775012e
LM
739/* Mark locations as "conditions have changed" in case the target supports
740 evaluating conditions on its side. */
741
742static void
743mark_breakpoint_modified (struct breakpoint *b)
744{
745 struct bp_location *loc;
746
747 /* This is only meaningful if the target is
748 evaluating conditions and if the user has
749 opted for condition evaluation on the target's
750 side. */
751 if (gdb_evaluates_breakpoint_condition_p ()
752 || !target_supports_evaluation_of_breakpoint_conditions ())
753 return;
754
755 if (!is_breakpoint (b))
756 return;
757
758 for (loc = b->loc; loc; loc = loc->next)
759 loc->condition_changed = condition_modified;
760}
761
762/* Mark location as "conditions have changed" in case the target supports
763 evaluating conditions on its side. */
764
765static void
766mark_breakpoint_location_modified (struct bp_location *loc)
767{
768 /* This is only meaningful if the target is
769 evaluating conditions and if the user has
770 opted for condition evaluation on the target's
771 side. */
772 if (gdb_evaluates_breakpoint_condition_p ()
773 || !target_supports_evaluation_of_breakpoint_conditions ())
774
775 return;
776
777 if (!is_breakpoint (loc->owner))
778 return;
779
780 loc->condition_changed = condition_modified;
781}
782
783/* Sets the condition-evaluation mode using the static global
784 condition_evaluation_mode. */
785
786static void
787set_condition_evaluation_mode (char *args, int from_tty,
788 struct cmd_list_element *c)
789{
b775012e
LM
790 const char *old_mode, *new_mode;
791
792 if ((condition_evaluation_mode_1 == condition_evaluation_target)
793 && !target_supports_evaluation_of_breakpoint_conditions ())
794 {
795 condition_evaluation_mode_1 = condition_evaluation_mode;
796 warning (_("Target does not support breakpoint condition evaluation.\n"
797 "Using host evaluation mode instead."));
798 return;
799 }
800
801 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
802 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
803
abf1152a
JK
804 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
805 settings was "auto". */
806 condition_evaluation_mode = condition_evaluation_mode_1;
807
b775012e
LM
808 /* Only update the mode if the user picked a different one. */
809 if (new_mode != old_mode)
810 {
811 struct bp_location *loc, **loc_tmp;
812 /* If the user switched to a different evaluation mode, we
813 need to synch the changes with the target as follows:
814
815 "host" -> "target": Send all (valid) conditions to the target.
816 "target" -> "host": Remove all the conditions from the target.
817 */
818
b775012e
LM
819 if (new_mode == condition_evaluation_target)
820 {
821 /* Mark everything modified and synch conditions with the
822 target. */
823 ALL_BP_LOCATIONS (loc, loc_tmp)
824 mark_breakpoint_location_modified (loc);
825 }
826 else
827 {
828 /* Manually mark non-duplicate locations to synch conditions
829 with the target. We do this to remove all the conditions the
830 target knows about. */
831 ALL_BP_LOCATIONS (loc, loc_tmp)
832 if (is_breakpoint (loc->owner) && loc->inserted)
833 loc->needs_update = 1;
834 }
835
836 /* Do the update. */
837 update_global_location_list (1);
838 }
839
840 return;
841}
842
843/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
844 what "auto" is translating to. */
845
846static void
847show_condition_evaluation_mode (struct ui_file *file, int from_tty,
848 struct cmd_list_element *c, const char *value)
849{
850 if (condition_evaluation_mode == condition_evaluation_auto)
851 fprintf_filtered (file,
852 _("Breakpoint condition evaluation "
853 "mode is %s (currently %s).\n"),
854 value,
855 breakpoint_condition_evaluation_mode ());
856 else
857 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
858 value);
859}
860
861/* A comparison function for bp_location AP and BP that is used by
862 bsearch. This comparison function only cares about addresses, unlike
863 the more general bp_location_compare function. */
864
865static int
866bp_location_compare_addrs (const void *ap, const void *bp)
867{
868 struct bp_location *a = *(void **) ap;
869 struct bp_location *b = *(void **) bp;
870
871 if (a->address == b->address)
872 return 0;
873 else
874 return ((a->address > b->address) - (a->address < b->address));
875}
876
877/* Helper function to skip all bp_locations with addresses
878 less than ADDRESS. It returns the first bp_location that
879 is greater than or equal to ADDRESS. If none is found, just
880 return NULL. */
881
882static struct bp_location **
883get_first_locp_gte_addr (CORE_ADDR address)
884{
885 struct bp_location dummy_loc;
886 struct bp_location *dummy_locp = &dummy_loc;
887 struct bp_location **locp_found = NULL;
888
889 /* Initialize the dummy location's address field. */
890 memset (&dummy_loc, 0, sizeof (struct bp_location));
891 dummy_loc.address = address;
892
893 /* Find a close match to the first location at ADDRESS. */
894 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
895 sizeof (struct bp_location **),
896 bp_location_compare_addrs);
897
898 /* Nothing was found, nothing left to do. */
899 if (locp_found == NULL)
900 return NULL;
901
902 /* We may have found a location that is at ADDRESS but is not the first in the
903 location's list. Go backwards (if possible) and locate the first one. */
904 while ((locp_found - 1) >= bp_location
905 && (*(locp_found - 1))->address == address)
906 locp_found--;
907
908 return locp_found;
909}
910
adc36818
PM
911void
912set_breakpoint_condition (struct breakpoint *b, char *exp,
913 int from_tty)
914{
3a5c3e22
PA
915 xfree (b->cond_string);
916 b->cond_string = NULL;
adc36818 917
3a5c3e22 918 if (is_watchpoint (b))
adc36818 919 {
3a5c3e22
PA
920 struct watchpoint *w = (struct watchpoint *) b;
921
922 xfree (w->cond_exp);
923 w->cond_exp = NULL;
924 }
925 else
926 {
927 struct bp_location *loc;
928
929 for (loc = b->loc; loc; loc = loc->next)
930 {
931 xfree (loc->cond);
932 loc->cond = NULL;
b775012e
LM
933
934 /* No need to free the condition agent expression
935 bytecode (if we have one). We will handle this
936 when we go through update_global_location_list. */
3a5c3e22 937 }
adc36818 938 }
adc36818
PM
939
940 if (*exp == 0)
941 {
942 if (from_tty)
943 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
944 }
945 else
946 {
bbc13ae3 947 const char *arg = exp;
cc59ec59 948
adc36818
PM
949 /* I don't know if it matters whether this is the string the user
950 typed in or the decompiled expression. */
951 b->cond_string = xstrdup (arg);
952 b->condition_not_parsed = 0;
953
954 if (is_watchpoint (b))
955 {
3a5c3e22
PA
956 struct watchpoint *w = (struct watchpoint *) b;
957
adc36818
PM
958 innermost_block = NULL;
959 arg = exp;
1bb9788d 960 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
961 if (*arg)
962 error (_("Junk at end of expression"));
3a5c3e22 963 w->cond_exp_valid_block = innermost_block;
adc36818
PM
964 }
965 else
966 {
3a5c3e22
PA
967 struct bp_location *loc;
968
adc36818
PM
969 for (loc = b->loc; loc; loc = loc->next)
970 {
971 arg = exp;
972 loc->cond =
1bb9788d
TT
973 parse_exp_1 (&arg, loc->address,
974 block_for_pc (loc->address), 0);
adc36818
PM
975 if (*arg)
976 error (_("Junk at end of expression"));
977 }
978 }
979 }
b775012e
LM
980 mark_breakpoint_modified (b);
981
8d3788bd 982 observer_notify_breakpoint_modified (b);
adc36818
PM
983}
984
d55637df
TT
985/* Completion for the "condition" command. */
986
987static VEC (char_ptr) *
6f937416
PA
988condition_completer (struct cmd_list_element *cmd,
989 const char *text, const char *word)
d55637df 990{
6f937416 991 const char *space;
d55637df 992
6f937416
PA
993 text = skip_spaces_const (text);
994 space = skip_to_space_const (text);
d55637df
TT
995 if (*space == '\0')
996 {
997 int len;
998 struct breakpoint *b;
999 VEC (char_ptr) *result = NULL;
1000
1001 if (text[0] == '$')
1002 {
1003 /* We don't support completion of history indices. */
1004 if (isdigit (text[1]))
1005 return NULL;
1006 return complete_internalvar (&text[1]);
1007 }
1008
1009 /* We're completing the breakpoint number. */
1010 len = strlen (text);
1011
1012 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1013 {
1014 char number[50];
1015
1016 xsnprintf (number, sizeof (number), "%d", b->number);
1017
1018 if (strncmp (number, text, len) == 0)
1019 VEC_safe_push (char_ptr, result, xstrdup (number));
1020 }
d55637df
TT
1021
1022 return result;
1023 }
1024
1025 /* We're completing the expression part. */
6f937416 1026 text = skip_spaces_const (space);
d55637df
TT
1027 return expression_completer (cmd, text, word);
1028}
1029
c906108c
SS
1030/* condition N EXP -- set break condition of breakpoint N to EXP. */
1031
1032static void
fba45db2 1033condition_command (char *arg, int from_tty)
c906108c 1034{
52f0bd74 1035 struct breakpoint *b;
c906108c 1036 char *p;
52f0bd74 1037 int bnum;
c906108c
SS
1038
1039 if (arg == 0)
e2e0b3e5 1040 error_no_arg (_("breakpoint number"));
c906108c
SS
1041
1042 p = arg;
1043 bnum = get_number (&p);
5c44784c 1044 if (bnum == 0)
8a3fe4f8 1045 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1046
1047 ALL_BREAKPOINTS (b)
1048 if (b->number == bnum)
2f069f6f 1049 {
7371cf6d
PM
1050 /* Check if this breakpoint has a Python object assigned to
1051 it, and if it has a definition of the "stop"
1052 method. This method and conditions entered into GDB from
1053 the CLI are mutually exclusive. */
1054 if (b->py_bp_object
1055 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
1056 error (_("Cannot set a condition where a Python 'stop' "
1057 "method has been defined in the breakpoint."));
2566ad2d 1058 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1059
1060 if (is_breakpoint (b))
1061 update_global_location_list (1);
1062
2f069f6f
JB
1063 return;
1064 }
c906108c 1065
8a3fe4f8 1066 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1067}
1068
a7bdde9e
VP
1069/* Check that COMMAND do not contain commands that are suitable
1070 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1071 Throw if any such commands is found. */
1072
a7bdde9e
VP
1073static void
1074check_no_tracepoint_commands (struct command_line *commands)
1075{
1076 struct command_line *c;
cc59ec59 1077
a7bdde9e
VP
1078 for (c = commands; c; c = c->next)
1079 {
1080 int i;
1081
1082 if (c->control_type == while_stepping_control)
3e43a32a
MS
1083 error (_("The 'while-stepping' command can "
1084 "only be used for tracepoints"));
a7bdde9e
VP
1085
1086 for (i = 0; i < c->body_count; ++i)
1087 check_no_tracepoint_commands ((c->body_list)[i]);
1088
1089 /* Not that command parsing removes leading whitespace and comment
4a64f543 1090 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1091 command directly. */
1092 if (strstr (c->line, "collect ") == c->line)
1093 error (_("The 'collect' command can only be used for tracepoints"));
1094
51661e93
VP
1095 if (strstr (c->line, "teval ") == c->line)
1096 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1097 }
1098}
1099
d77f58be
SS
1100/* Encapsulate tests for different types of tracepoints. */
1101
d9b3f62e
PA
1102static int
1103is_tracepoint_type (enum bptype type)
1104{
1105 return (type == bp_tracepoint
1106 || type == bp_fast_tracepoint
1107 || type == bp_static_tracepoint);
1108}
1109
a7bdde9e 1110int
d77f58be 1111is_tracepoint (const struct breakpoint *b)
a7bdde9e 1112{
d9b3f62e 1113 return is_tracepoint_type (b->type);
a7bdde9e 1114}
d9b3f62e 1115
e5dd4106 1116/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1117 breakpoint. This function will throw an exception if a problem is
1118 found. */
48cb2d85 1119
95a42b64
TT
1120static void
1121validate_commands_for_breakpoint (struct breakpoint *b,
1122 struct command_line *commands)
48cb2d85 1123{
d77f58be 1124 if (is_tracepoint (b))
a7bdde9e 1125 {
c9a6ce02 1126 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1127 struct command_line *c;
1128 struct command_line *while_stepping = 0;
c9a6ce02
PA
1129
1130 /* Reset the while-stepping step count. The previous commands
1131 might have included a while-stepping action, while the new
1132 ones might not. */
1133 t->step_count = 0;
1134
1135 /* We need to verify that each top-level element of commands is
1136 valid for tracepoints, that there's at most one
1137 while-stepping element, and that the while-stepping's body
1138 has valid tracing commands excluding nested while-stepping.
1139 We also need to validate the tracepoint action line in the
1140 context of the tracepoint --- validate_actionline actually
1141 has side effects, like setting the tracepoint's
1142 while-stepping STEP_COUNT, in addition to checking if the
1143 collect/teval actions parse and make sense in the
1144 tracepoint's context. */
a7bdde9e
VP
1145 for (c = commands; c; c = c->next)
1146 {
a7bdde9e
VP
1147 if (c->control_type == while_stepping_control)
1148 {
1149 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1150 error (_("The 'while-stepping' command "
1151 "cannot be used for fast tracepoint"));
0fb4aa4b 1152 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1153 error (_("The 'while-stepping' command "
1154 "cannot be used for static tracepoint"));
a7bdde9e
VP
1155
1156 if (while_stepping)
3e43a32a
MS
1157 error (_("The 'while-stepping' command "
1158 "can be used only once"));
a7bdde9e
VP
1159 else
1160 while_stepping = c;
1161 }
c9a6ce02
PA
1162
1163 validate_actionline (c->line, b);
a7bdde9e
VP
1164 }
1165 if (while_stepping)
1166 {
1167 struct command_line *c2;
1168
1169 gdb_assert (while_stepping->body_count == 1);
1170 c2 = while_stepping->body_list[0];
1171 for (; c2; c2 = c2->next)
1172 {
a7bdde9e
VP
1173 if (c2->control_type == while_stepping_control)
1174 error (_("The 'while-stepping' command cannot be nested"));
1175 }
1176 }
1177 }
1178 else
1179 {
1180 check_no_tracepoint_commands (commands);
1181 }
95a42b64
TT
1182}
1183
0fb4aa4b
PA
1184/* Return a vector of all the static tracepoints set at ADDR. The
1185 caller is responsible for releasing the vector. */
1186
1187VEC(breakpoint_p) *
1188static_tracepoints_here (CORE_ADDR addr)
1189{
1190 struct breakpoint *b;
1191 VEC(breakpoint_p) *found = 0;
1192 struct bp_location *loc;
1193
1194 ALL_BREAKPOINTS (b)
1195 if (b->type == bp_static_tracepoint)
1196 {
1197 for (loc = b->loc; loc; loc = loc->next)
1198 if (loc->address == addr)
1199 VEC_safe_push(breakpoint_p, found, b);
1200 }
1201
1202 return found;
1203}
1204
95a42b64 1205/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1206 validate that only allowed commands are included. */
95a42b64
TT
1207
1208void
4a64f543
MS
1209breakpoint_set_commands (struct breakpoint *b,
1210 struct command_line *commands)
95a42b64
TT
1211{
1212 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1213
9add0f1b
TT
1214 decref_counted_command_line (&b->commands);
1215 b->commands = alloc_counted_command_line (commands);
8d3788bd 1216 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1217}
1218
45a43567
TT
1219/* Set the internal `silent' flag on the breakpoint. Note that this
1220 is not the same as the "silent" that may appear in the breakpoint's
1221 commands. */
1222
1223void
1224breakpoint_set_silent (struct breakpoint *b, int silent)
1225{
1226 int old_silent = b->silent;
1227
1228 b->silent = silent;
1229 if (old_silent != silent)
8d3788bd 1230 observer_notify_breakpoint_modified (b);
45a43567
TT
1231}
1232
1233/* Set the thread for this breakpoint. If THREAD is -1, make the
1234 breakpoint work for any thread. */
1235
1236void
1237breakpoint_set_thread (struct breakpoint *b, int thread)
1238{
1239 int old_thread = b->thread;
1240
1241 b->thread = thread;
1242 if (old_thread != thread)
8d3788bd 1243 observer_notify_breakpoint_modified (b);
45a43567
TT
1244}
1245
1246/* Set the task for this breakpoint. If TASK is 0, make the
1247 breakpoint work for any task. */
1248
1249void
1250breakpoint_set_task (struct breakpoint *b, int task)
1251{
1252 int old_task = b->task;
1253
1254 b->task = task;
1255 if (old_task != task)
8d3788bd 1256 observer_notify_breakpoint_modified (b);
45a43567
TT
1257}
1258
95a42b64
TT
1259void
1260check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1261{
1262 struct breakpoint *b = closure;
cc59ec59 1263
6f937416 1264 validate_actionline (line, b);
a7bdde9e
VP
1265}
1266
95a42b64
TT
1267/* A structure used to pass information through
1268 map_breakpoint_numbers. */
1269
1270struct commands_info
1271{
1272 /* True if the command was typed at a tty. */
1273 int from_tty;
86b17b60
PA
1274
1275 /* The breakpoint range spec. */
1276 char *arg;
1277
95a42b64
TT
1278 /* Non-NULL if the body of the commands are being read from this
1279 already-parsed command. */
1280 struct command_line *control;
86b17b60 1281
95a42b64
TT
1282 /* The command lines read from the user, or NULL if they have not
1283 yet been read. */
1284 struct counted_command_line *cmd;
1285};
1286
1287/* A callback for map_breakpoint_numbers that sets the commands for
1288 commands_command. */
1289
c906108c 1290static void
95a42b64 1291do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1292{
95a42b64 1293 struct commands_info *info = data;
c906108c 1294
95a42b64
TT
1295 if (info->cmd == NULL)
1296 {
1297 struct command_line *l;
5c44784c 1298
95a42b64
TT
1299 if (info->control != NULL)
1300 l = copy_command_lines (info->control->body_list[0]);
1301 else
86b17b60
PA
1302 {
1303 struct cleanup *old_chain;
1304 char *str;
c5aa993b 1305
3e43a32a
MS
1306 str = xstrprintf (_("Type commands for breakpoint(s) "
1307 "%s, one per line."),
86b17b60
PA
1308 info->arg);
1309
1310 old_chain = make_cleanup (xfree, str);
1311
1312 l = read_command_lines (str,
1313 info->from_tty, 1,
d77f58be 1314 (is_tracepoint (b)
86b17b60
PA
1315 ? check_tracepoint_command : 0),
1316 b);
1317
1318 do_cleanups (old_chain);
1319 }
a7bdde9e 1320
95a42b64
TT
1321 info->cmd = alloc_counted_command_line (l);
1322 }
1323
1324 /* If a breakpoint was on the list more than once, we don't need to
1325 do anything. */
1326 if (b->commands != info->cmd)
1327 {
1328 validate_commands_for_breakpoint (b, info->cmd->commands);
1329 incref_counted_command_line (info->cmd);
1330 decref_counted_command_line (&b->commands);
1331 b->commands = info->cmd;
8d3788bd 1332 observer_notify_breakpoint_modified (b);
c5aa993b 1333 }
95a42b64
TT
1334}
1335
1336static void
4a64f543
MS
1337commands_command_1 (char *arg, int from_tty,
1338 struct command_line *control)
95a42b64
TT
1339{
1340 struct cleanup *cleanups;
1341 struct commands_info info;
1342
1343 info.from_tty = from_tty;
1344 info.control = control;
1345 info.cmd = NULL;
1346 /* If we read command lines from the user, then `info' will hold an
1347 extra reference to the commands that we must clean up. */
1348 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1349
1350 if (arg == NULL || !*arg)
1351 {
86b17b60 1352 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1353 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1354 breakpoint_count);
95a42b64
TT
1355 else if (breakpoint_count > 0)
1356 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1357 else
1358 {
1359 /* So that we don't try to free the incoming non-NULL
1360 argument in the cleanup below. Mapping breakpoint
1361 numbers will fail in this case. */
1362 arg = NULL;
1363 }
95a42b64 1364 }
9766ced4
SS
1365 else
1366 /* The command loop has some static state, so we need to preserve
1367 our argument. */
1368 arg = xstrdup (arg);
86b17b60
PA
1369
1370 if (arg != NULL)
1371 make_cleanup (xfree, arg);
1372
1373 info.arg = arg;
95a42b64
TT
1374
1375 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1376
1377 if (info.cmd == NULL)
1378 error (_("No breakpoints specified."));
1379
1380 do_cleanups (cleanups);
1381}
1382
1383static void
1384commands_command (char *arg, int from_tty)
1385{
1386 commands_command_1 (arg, from_tty, NULL);
c906108c 1387}
40c03ae8
EZ
1388
1389/* Like commands_command, but instead of reading the commands from
1390 input stream, takes them from an already parsed command structure.
1391
1392 This is used by cli-script.c to DTRT with breakpoint commands
1393 that are part of if and while bodies. */
1394enum command_control_type
1395commands_from_control_command (char *arg, struct command_line *cmd)
1396{
95a42b64
TT
1397 commands_command_1 (arg, 0, cmd);
1398 return simple_control;
40c03ae8 1399}
876fa593
JK
1400
1401/* Return non-zero if BL->TARGET_INFO contains valid information. */
1402
1403static int
1404bp_location_has_shadow (struct bp_location *bl)
1405{
1406 if (bl->loc_type != bp_loc_software_breakpoint)
1407 return 0;
1408 if (!bl->inserted)
1409 return 0;
1410 if (bl->target_info.shadow_len == 0)
e5dd4106 1411 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1412 return 0;
1413 return 1;
1414}
1415
8defab1a 1416/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1417 by replacing any memory breakpoints with their shadowed contents.
1418
35c63cd8
JB
1419 If READBUF is not NULL, this buffer must not overlap with any of
1420 the breakpoint location's shadow_contents buffers. Otherwise,
1421 a failed assertion internal error will be raised.
1422
876fa593 1423 The range of shadowed area by each bp_location is:
35df4500
TJB
1424 bl->address - bp_location_placed_address_before_address_max
1425 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1426 The range we were requested to resolve shadows for is:
1427 memaddr ... memaddr + len
1428 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1429 memaddr + len <= (bl->address
1430 - bp_location_placed_address_before_address_max)
876fa593 1431 and:
35df4500 1432 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1433
8defab1a 1434void
f0ba3972
PA
1435breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1436 const gdb_byte *writebuf_org,
1437 ULONGEST memaddr, LONGEST len)
c906108c 1438{
4a64f543
MS
1439 /* Left boundary, right boundary and median element of our binary
1440 search. */
876fa593
JK
1441 unsigned bc_l, bc_r, bc;
1442
4a64f543
MS
1443 /* Find BC_L which is a leftmost element which may affect BUF
1444 content. It is safe to report lower value but a failure to
1445 report higher one. */
876fa593
JK
1446
1447 bc_l = 0;
1448 bc_r = bp_location_count;
1449 while (bc_l + 1 < bc_r)
1450 {
35df4500 1451 struct bp_location *bl;
876fa593
JK
1452
1453 bc = (bc_l + bc_r) / 2;
35df4500 1454 bl = bp_location[bc];
876fa593 1455
4a64f543
MS
1456 /* Check first BL->ADDRESS will not overflow due to the added
1457 constant. Then advance the left boundary only if we are sure
1458 the BC element can in no way affect the BUF content (MEMADDR
1459 to MEMADDR + LEN range).
876fa593 1460
4a64f543
MS
1461 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1462 offset so that we cannot miss a breakpoint with its shadow
1463 range tail still reaching MEMADDR. */
c5aa993b 1464
35df4500
TJB
1465 if ((bl->address + bp_location_shadow_len_after_address_max
1466 >= bl->address)
1467 && (bl->address + bp_location_shadow_len_after_address_max
1468 <= memaddr))
876fa593
JK
1469 bc_l = bc;
1470 else
1471 bc_r = bc;
1472 }
1473
128070bb
PA
1474 /* Due to the binary search above, we need to make sure we pick the
1475 first location that's at BC_L's address. E.g., if there are
1476 multiple locations at the same address, BC_L may end up pointing
1477 at a duplicate location, and miss the "master"/"inserted"
1478 location. Say, given locations L1, L2 and L3 at addresses A and
1479 B:
1480
1481 L1@A, L2@A, L3@B, ...
1482
1483 BC_L could end up pointing at location L2, while the "master"
1484 location could be L1. Since the `loc->inserted' flag is only set
1485 on "master" locations, we'd forget to restore the shadow of L1
1486 and L2. */
1487 while (bc_l > 0
1488 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1489 bc_l--;
1490
876fa593
JK
1491 /* Now do full processing of the found relevant range of elements. */
1492
1493 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1494 {
35df4500 1495 struct bp_location *bl = bp_location[bc];
876fa593
JK
1496 CORE_ADDR bp_addr = 0;
1497 int bp_size = 0;
1498 int bptoffset = 0;
1499
35df4500
TJB
1500 /* bp_location array has BL->OWNER always non-NULL. */
1501 if (bl->owner->type == bp_none)
8a3fe4f8 1502 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1503 bl->owner->number);
ffce0d52 1504
e5dd4106 1505 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1506 content. */
1507
35df4500
TJB
1508 if (bl->address >= bp_location_placed_address_before_address_max
1509 && memaddr + len <= (bl->address
1510 - bp_location_placed_address_before_address_max))
876fa593
JK
1511 break;
1512
35df4500 1513 if (!bp_location_has_shadow (bl))
c5aa993b 1514 continue;
35df4500 1515 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1516 current_program_space->aspace, 0))
1517 continue;
1518
c5aa993b
JM
1519 /* Addresses and length of the part of the breakpoint that
1520 we need to copy. */
35df4500
TJB
1521 bp_addr = bl->target_info.placed_address;
1522 bp_size = bl->target_info.shadow_len;
8defab1a 1523
c5aa993b
JM
1524 if (bp_addr + bp_size <= memaddr)
1525 /* The breakpoint is entirely before the chunk of memory we
1526 are reading. */
1527 continue;
8defab1a 1528
c5aa993b
JM
1529 if (bp_addr >= memaddr + len)
1530 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1531 reading. */
c5aa993b 1532 continue;
c5aa993b 1533
8defab1a
DJ
1534 /* Offset within shadow_contents. */
1535 if (bp_addr < memaddr)
1536 {
1537 /* Only copy the second part of the breakpoint. */
1538 bp_size -= memaddr - bp_addr;
1539 bptoffset = memaddr - bp_addr;
1540 bp_addr = memaddr;
1541 }
c5aa993b 1542
8defab1a
DJ
1543 if (bp_addr + bp_size > memaddr + len)
1544 {
1545 /* Only copy the first part of the breakpoint. */
1546 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1547 }
c5aa993b 1548
f0ba3972
PA
1549 if (readbuf != NULL)
1550 {
35c63cd8
JB
1551 /* Verify that the readbuf buffer does not overlap with
1552 the shadow_contents buffer. */
1553 gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1554 || readbuf >= (bl->target_info.shadow_contents
1555 + bl->target_info.shadow_len));
1556
f0ba3972
PA
1557 /* Update the read buffer with this inserted breakpoint's
1558 shadow. */
1559 memcpy (readbuf + bp_addr - memaddr,
1560 bl->target_info.shadow_contents + bptoffset, bp_size);
1561 }
1562 else
1563 {
1564 struct gdbarch *gdbarch = bl->gdbarch;
1565 const unsigned char *bp;
1566 CORE_ADDR placed_address = bl->target_info.placed_address;
20ced3e4 1567 int placed_size = bl->target_info.placed_size;
f0ba3972
PA
1568
1569 /* Update the shadow with what we want to write to memory. */
1570 memcpy (bl->target_info.shadow_contents + bptoffset,
1571 writebuf_org + bp_addr - memaddr, bp_size);
1572
1573 /* Determine appropriate breakpoint contents and size for this
1574 address. */
1575 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1576
1577 /* Update the final write buffer with this inserted
1578 breakpoint's INSN. */
1579 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1580 }
c5aa993b 1581 }
c906108c 1582}
c906108c 1583\f
c5aa993b 1584
b775012e
LM
1585/* Return true if BPT is either a software breakpoint or a hardware
1586 breakpoint. */
1587
1588int
1589is_breakpoint (const struct breakpoint *bpt)
1590{
1591 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1592 || bpt->type == bp_hardware_breakpoint
1593 || bpt->type == bp_dprintf);
b775012e
LM
1594}
1595
60e1c644
PA
1596/* Return true if BPT is of any hardware watchpoint kind. */
1597
a5606eee 1598static int
d77f58be 1599is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1600{
1601 return (bpt->type == bp_hardware_watchpoint
1602 || bpt->type == bp_read_watchpoint
1603 || bpt->type == bp_access_watchpoint);
1604}
7270d8f2 1605
60e1c644
PA
1606/* Return true if BPT is of any watchpoint kind, hardware or
1607 software. */
1608
3a5c3e22 1609int
d77f58be 1610is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1611{
1612 return (is_hardware_watchpoint (bpt)
1613 || bpt->type == bp_watchpoint);
1614}
1615
3a5c3e22
PA
1616/* Returns true if the current thread and its running state are safe
1617 to evaluate or update watchpoint B. Watchpoints on local
1618 expressions need to be evaluated in the context of the thread that
1619 was current when the watchpoint was created, and, that thread needs
1620 to be stopped to be able to select the correct frame context.
1621 Watchpoints on global expressions can be evaluated on any thread,
1622 and in any state. It is presently left to the target allowing
1623 memory accesses when threads are running. */
f6bc2008
PA
1624
1625static int
3a5c3e22 1626watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1627{
d0d8b0c6
JK
1628 return (b->base.pspace == current_program_space
1629 && (ptid_equal (b->watchpoint_thread, null_ptid)
1630 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1631 && !is_executing (inferior_ptid))));
f6bc2008
PA
1632}
1633
d0fb5eae
JK
1634/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1635 associated bp_watchpoint_scope breakpoint. */
1636
1637static void
3a5c3e22 1638watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1639{
3a5c3e22 1640 struct breakpoint *b = &w->base;
d0fb5eae
JK
1641
1642 if (b->related_breakpoint != b)
1643 {
1644 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1645 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1646 b->related_breakpoint->disposition = disp_del_at_next_stop;
1647 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1648 b->related_breakpoint = b;
1649 }
1650 b->disposition = disp_del_at_next_stop;
1651}
1652
567e1b4e
JB
1653/* Assuming that B is a watchpoint:
1654 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1655 - Evaluate expression and store the result in B->val
567e1b4e
JB
1656 - Evaluate the condition if there is one, and store the result
1657 in b->loc->cond.
a5606eee
VP
1658 - Update the list of values that must be watched in B->loc.
1659
4a64f543
MS
1660 If the watchpoint disposition is disp_del_at_next_stop, then do
1661 nothing. If this is local watchpoint that is out of scope, delete
1662 it.
1663
1664 Even with `set breakpoint always-inserted on' the watchpoints are
1665 removed + inserted on each stop here. Normal breakpoints must
1666 never be removed because they might be missed by a running thread
1667 when debugging in non-stop mode. On the other hand, hardware
1668 watchpoints (is_hardware_watchpoint; processed here) are specific
1669 to each LWP since they are stored in each LWP's hardware debug
1670 registers. Therefore, such LWP must be stopped first in order to
1671 be able to modify its hardware watchpoints.
1672
1673 Hardware watchpoints must be reset exactly once after being
1674 presented to the user. It cannot be done sooner, because it would
1675 reset the data used to present the watchpoint hit to the user. And
1676 it must not be done later because it could display the same single
1677 watchpoint hit during multiple GDB stops. Note that the latter is
1678 relevant only to the hardware watchpoint types bp_read_watchpoint
1679 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1680 not user-visible - its hit is suppressed if the memory content has
1681 not changed.
1682
1683 The following constraints influence the location where we can reset
1684 hardware watchpoints:
1685
1686 * target_stopped_by_watchpoint and target_stopped_data_address are
1687 called several times when GDB stops.
1688
1689 [linux]
1690 * Multiple hardware watchpoints can be hit at the same time,
1691 causing GDB to stop. GDB only presents one hardware watchpoint
1692 hit at a time as the reason for stopping, and all the other hits
1693 are presented later, one after the other, each time the user
1694 requests the execution to be resumed. Execution is not resumed
1695 for the threads still having pending hit event stored in
1696 LWP_INFO->STATUS. While the watchpoint is already removed from
1697 the inferior on the first stop the thread hit event is kept being
1698 reported from its cached value by linux_nat_stopped_data_address
1699 until the real thread resume happens after the watchpoint gets
1700 presented and thus its LWP_INFO->STATUS gets reset.
1701
1702 Therefore the hardware watchpoint hit can get safely reset on the
1703 watchpoint removal from inferior. */
a79d3c27 1704
b40ce68a 1705static void
3a5c3e22 1706update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1707{
a5606eee 1708 int within_current_scope;
a5606eee 1709 struct frame_id saved_frame_id;
66076460 1710 int frame_saved;
a5606eee 1711
f6bc2008
PA
1712 /* If this is a local watchpoint, we only want to check if the
1713 watchpoint frame is in scope if the current thread is the thread
1714 that was used to create the watchpoint. */
1715 if (!watchpoint_in_thread_scope (b))
1716 return;
1717
3a5c3e22 1718 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1719 return;
1720
66076460 1721 frame_saved = 0;
a5606eee
VP
1722
1723 /* Determine if the watchpoint is within scope. */
1724 if (b->exp_valid_block == NULL)
1725 within_current_scope = 1;
1726 else
1727 {
b5db5dfc
UW
1728 struct frame_info *fi = get_current_frame ();
1729 struct gdbarch *frame_arch = get_frame_arch (fi);
1730 CORE_ADDR frame_pc = get_frame_pc (fi);
1731
1732 /* If we're in a function epilogue, unwinding may not work
1733 properly, so do not attempt to recreate locations at this
1734 point. See similar comments in watchpoint_check. */
1735 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1736 return;
66076460
DJ
1737
1738 /* Save the current frame's ID so we can restore it after
1739 evaluating the watchpoint expression on its own frame. */
1740 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1741 took a frame parameter, so that we didn't have to change the
1742 selected frame. */
1743 frame_saved = 1;
1744 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1745
a5606eee
VP
1746 fi = frame_find_by_id (b->watchpoint_frame);
1747 within_current_scope = (fi != NULL);
1748 if (within_current_scope)
1749 select_frame (fi);
1750 }
1751
b5db5dfc
UW
1752 /* We don't free locations. They are stored in the bp_location array
1753 and update_global_location_list will eventually delete them and
1754 remove breakpoints if needed. */
3a5c3e22 1755 b->base.loc = NULL;
b5db5dfc 1756
a5606eee
VP
1757 if (within_current_scope && reparse)
1758 {
bbc13ae3 1759 const char *s;
d63d0675 1760
a5606eee
VP
1761 if (b->exp)
1762 {
1763 xfree (b->exp);
1764 b->exp = NULL;
1765 }
d63d0675 1766 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1767 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1768 /* If the meaning of expression itself changed, the old value is
1769 no longer relevant. We don't want to report a watchpoint hit
1770 to the user when the old value and the new value may actually
1771 be completely different objects. */
1772 value_free (b->val);
fa4727a6
DJ
1773 b->val = NULL;
1774 b->val_valid = 0;
60e1c644
PA
1775
1776 /* Note that unlike with breakpoints, the watchpoint's condition
1777 expression is stored in the breakpoint object, not in the
1778 locations (re)created below. */
3a5c3e22 1779 if (b->base.cond_string != NULL)
60e1c644
PA
1780 {
1781 if (b->cond_exp != NULL)
1782 {
1783 xfree (b->cond_exp);
1784 b->cond_exp = NULL;
1785 }
1786
3a5c3e22 1787 s = b->base.cond_string;
1bb9788d 1788 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1789 }
a5606eee 1790 }
a5606eee
VP
1791
1792 /* If we failed to parse the expression, for example because
1793 it refers to a global variable in a not-yet-loaded shared library,
1794 don't try to insert watchpoint. We don't automatically delete
1795 such watchpoint, though, since failure to parse expression
1796 is different from out-of-scope watchpoint. */
e8369a73 1797 if (!target_has_execution)
2d134ed3
PA
1798 {
1799 /* Without execution, memory can't change. No use to try and
1800 set watchpoint locations. The watchpoint will be reset when
1801 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1802 if (!can_use_hw_watchpoints)
1803 {
1804 if (b->base.ops->works_in_software_mode (&b->base))
1805 b->base.type = bp_watchpoint;
1806 else
638aa5a1
AB
1807 error (_("Can't set read/access watchpoint when "
1808 "hardware watchpoints are disabled."));
e8369a73 1809 }
2d134ed3
PA
1810 }
1811 else if (within_current_scope && b->exp)
a5606eee 1812 {
0cf6dd15 1813 int pc = 0;
fa4727a6 1814 struct value *val_chain, *v, *result, *next;
2d134ed3 1815 struct program_space *frame_pspace;
a5606eee 1816
3a1115a0 1817 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1818
a5606eee
VP
1819 /* Avoid setting b->val if it's already set. The meaning of
1820 b->val is 'the last value' user saw, and we should update
1821 it only if we reported that last value to user. As it
9c06b0b4
TJB
1822 happens, the code that reports it updates b->val directly.
1823 We don't keep track of the memory value for masked
1824 watchpoints. */
3a5c3e22 1825 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1826 {
1827 b->val = v;
1828 b->val_valid = 1;
1829 }
a5606eee 1830
2d134ed3
PA
1831 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1832
a5606eee 1833 /* Look at each value on the value chain. */
9fa40276 1834 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1835 {
1836 /* If it's a memory location, and GDB actually needed
1837 its contents to evaluate the expression, then we
fa4727a6
DJ
1838 must watch it. If the first value returned is
1839 still lazy, that means an error occurred reading it;
1840 watch it anyway in case it becomes readable. */
a5606eee 1841 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1842 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1843 {
1844 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1845
a5606eee
VP
1846 /* We only watch structs and arrays if user asked
1847 for it explicitly, never if they just happen to
1848 appear in the middle of some value chain. */
fa4727a6 1849 if (v == result
a5606eee
VP
1850 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1851 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1852 {
1853 CORE_ADDR addr;
744a8059 1854 int type;
a5606eee
VP
1855 struct bp_location *loc, **tmp;
1856
42ae5230 1857 addr = value_address (v);
a5606eee 1858 type = hw_write;
3a5c3e22 1859 if (b->base.type == bp_read_watchpoint)
a5606eee 1860 type = hw_read;
3a5c3e22 1861 else if (b->base.type == bp_access_watchpoint)
a5606eee 1862 type = hw_access;
3a5c3e22
PA
1863
1864 loc = allocate_bp_location (&b->base);
1865 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1866 ;
1867 *tmp = loc;
a6d9a66e 1868 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1869
1870 loc->pspace = frame_pspace;
a5606eee 1871 loc->address = addr;
744a8059 1872 loc->length = TYPE_LENGTH (value_type (v));
a5606eee
VP
1873 loc->watchpoint_type = type;
1874 }
1875 }
9fa40276
TJB
1876 }
1877
1878 /* Change the type of breakpoint between hardware assisted or
1879 an ordinary watchpoint depending on the hardware support
1880 and free hardware slots. REPARSE is set when the inferior
1881 is started. */
a9634178 1882 if (reparse)
9fa40276 1883 {
e09342b5 1884 int reg_cnt;
9fa40276
TJB
1885 enum bp_loc_type loc_type;
1886 struct bp_location *bl;
a5606eee 1887
a9634178 1888 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1889
1890 if (reg_cnt)
9fa40276
TJB
1891 {
1892 int i, target_resources_ok, other_type_used;
a1398e0c 1893 enum bptype type;
9fa40276 1894
a9634178
TJB
1895 /* Use an exact watchpoint when there's only one memory region to be
1896 watched, and only one debug register is needed to watch it. */
1897 b->exact = target_exact_watchpoints && reg_cnt == 1;
1898
9fa40276 1899 /* We need to determine how many resources are already
e09342b5
TJB
1900 used for all other hardware watchpoints plus this one
1901 to see if we still have enough resources to also fit
a1398e0c
PA
1902 this watchpoint in as well. */
1903
1904 /* If this is a software watchpoint, we try to turn it
1905 to a hardware one -- count resources as if B was of
1906 hardware watchpoint type. */
1907 type = b->base.type;
1908 if (type == bp_watchpoint)
1909 type = bp_hardware_watchpoint;
1910
1911 /* This watchpoint may or may not have been placed on
1912 the list yet at this point (it won't be in the list
1913 if we're trying to create it for the first time,
1914 through watch_command), so always account for it
1915 manually. */
1916
1917 /* Count resources used by all watchpoints except B. */
1918 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1919
1920 /* Add in the resources needed for B. */
1921 i += hw_watchpoint_use_count (&b->base);
1922
1923 target_resources_ok
1924 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1925 if (target_resources_ok <= 0)
a9634178 1926 {
3a5c3e22 1927 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1928
1929 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1930 error (_("Target does not support this type of "
1931 "hardware watchpoint."));
9c06b0b4
TJB
1932 else if (target_resources_ok < 0 && !sw_mode)
1933 error (_("There are not enough available hardware "
1934 "resources for this watchpoint."));
a1398e0c
PA
1935
1936 /* Downgrade to software watchpoint. */
1937 b->base.type = bp_watchpoint;
1938 }
1939 else
1940 {
1941 /* If this was a software watchpoint, we've just
1942 found we have enough resources to turn it to a
1943 hardware watchpoint. Otherwise, this is a
1944 nop. */
1945 b->base.type = type;
a9634178 1946 }
9fa40276 1947 }
3a5c3e22 1948 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
1949 {
1950 if (!can_use_hw_watchpoints)
1951 error (_("Can't set read/access watchpoint when "
1952 "hardware watchpoints are disabled."));
1953 else
1954 error (_("Expression cannot be implemented with "
1955 "read/access watchpoint."));
1956 }
9fa40276 1957 else
3a5c3e22 1958 b->base.type = bp_watchpoint;
9fa40276 1959
3a5c3e22 1960 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1961 : bp_loc_hardware_watchpoint);
3a5c3e22 1962 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1963 bl->loc_type = loc_type;
1964 }
1965
1966 for (v = val_chain; v; v = next)
1967 {
a5606eee
VP
1968 next = value_next (v);
1969 if (v != b->val)
1970 value_free (v);
1971 }
1972
c7437ca6
PA
1973 /* If a software watchpoint is not watching any memory, then the
1974 above left it without any location set up. But,
1975 bpstat_stop_status requires a location to be able to report
1976 stops, so make sure there's at least a dummy one. */
3a5c3e22 1977 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1978 {
3a5c3e22
PA
1979 struct breakpoint *base = &b->base;
1980 base->loc = allocate_bp_location (base);
1981 base->loc->pspace = frame_pspace;
1982 base->loc->address = -1;
1983 base->loc->length = -1;
1984 base->loc->watchpoint_type = -1;
c7437ca6 1985 }
a5606eee
VP
1986 }
1987 else if (!within_current_scope)
7270d8f2 1988 {
ac74f770
MS
1989 printf_filtered (_("\
1990Watchpoint %d deleted because the program has left the block\n\
1991in which its expression is valid.\n"),
3a5c3e22 1992 b->base.number);
d0fb5eae 1993 watchpoint_del_at_next_stop (b);
7270d8f2 1994 }
a5606eee
VP
1995
1996 /* Restore the selected frame. */
66076460
DJ
1997 if (frame_saved)
1998 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1999}
2000
a5606eee 2001
74960c60 2002/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2003 inserted in the inferior. We don't differentiate the type of BL's owner
2004 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2005 breakpoint_ops is not defined, because in insert_bp_location,
2006 tracepoint's insert_location will not be called. */
74960c60 2007static int
35df4500 2008should_be_inserted (struct bp_location *bl)
74960c60 2009{
35df4500 2010 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2011 return 0;
2012
35df4500 2013 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2014 return 0;
2015
35df4500 2016 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2017 return 0;
2018
f8eba3c6
TT
2019 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2020 return 0;
2021
56710373
PA
2022 /* This is set for example, when we're attached to the parent of a
2023 vfork, and have detached from the child. The child is running
2024 free, and we expect it to do an exec or exit, at which point the
2025 OS makes the parent schedulable again (and the target reports
2026 that the vfork is done). Until the child is done with the shared
2027 memory region, do not insert breakpoints in the parent, otherwise
2028 the child could still trip on the parent's breakpoints. Since
2029 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2030 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2031 return 0;
2032
74960c60
VP
2033 return 1;
2034}
2035
934709f0
PW
2036/* Same as should_be_inserted but does the check assuming
2037 that the location is not duplicated. */
2038
2039static int
2040unduplicated_should_be_inserted (struct bp_location *bl)
2041{
2042 int result;
2043 const int save_duplicate = bl->duplicate;
2044
2045 bl->duplicate = 0;
2046 result = should_be_inserted (bl);
2047 bl->duplicate = save_duplicate;
2048 return result;
2049}
2050
b775012e
LM
2051/* Parses a conditional described by an expression COND into an
2052 agent expression bytecode suitable for evaluation
2053 by the bytecode interpreter. Return NULL if there was
2054 any error during parsing. */
2055
2056static struct agent_expr *
2057parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2058{
2059 struct agent_expr *aexpr = NULL;
b775012e
LM
2060 volatile struct gdb_exception ex;
2061
2062 if (!cond)
2063 return NULL;
2064
2065 /* We don't want to stop processing, so catch any errors
2066 that may show up. */
2067 TRY_CATCH (ex, RETURN_MASK_ERROR)
2068 {
2069 aexpr = gen_eval_for_expr (scope, cond);
2070 }
2071
2072 if (ex.reason < 0)
2073 {
2074 /* If we got here, it means the condition could not be parsed to a valid
2075 bytecode expression and thus can't be evaluated on the target's side.
2076 It's no use iterating through the conditions. */
2077 return NULL;
2078 }
2079
2080 /* We have a valid agent expression. */
2081 return aexpr;
2082}
2083
2084/* Based on location BL, create a list of breakpoint conditions to be
2085 passed on to the target. If we have duplicated locations with different
2086 conditions, we will add such conditions to the list. The idea is that the
2087 target will evaluate the list of conditions and will only notify GDB when
2088 one of them is true. */
2089
2090static void
2091build_target_condition_list (struct bp_location *bl)
2092{
2093 struct bp_location **locp = NULL, **loc2p;
2094 int null_condition_or_parse_error = 0;
2095 int modified = bl->needs_update;
2096 struct bp_location *loc;
2097
8b4f3082
PA
2098 /* Release conditions left over from a previous insert. */
2099 VEC_free (agent_expr_p, bl->target_info.conditions);
2100
b775012e
LM
2101 /* This is only meaningful if the target is
2102 evaluating conditions and if the user has
2103 opted for condition evaluation on the target's
2104 side. */
2105 if (gdb_evaluates_breakpoint_condition_p ()
2106 || !target_supports_evaluation_of_breakpoint_conditions ())
2107 return;
2108
2109 /* Do a first pass to check for locations with no assigned
2110 conditions or conditions that fail to parse to a valid agent expression
2111 bytecode. If any of these happen, then it's no use to send conditions
2112 to the target since this location will always trigger and generate a
2113 response back to GDB. */
2114 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2115 {
2116 loc = (*loc2p);
2117 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2118 {
2119 if (modified)
2120 {
2121 struct agent_expr *aexpr;
2122
2123 /* Re-parse the conditions since something changed. In that
2124 case we already freed the condition bytecodes (see
2125 force_breakpoint_reinsertion). We just
2126 need to parse the condition to bytecodes again. */
2127 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2128 loc->cond_bytecode = aexpr;
2129
2130 /* Check if we managed to parse the conditional expression
2131 correctly. If not, we will not send this condition
2132 to the target. */
2133 if (aexpr)
2134 continue;
2135 }
2136
2137 /* If we have a NULL bytecode expression, it means something
2138 went wrong or we have a null condition expression. */
2139 if (!loc->cond_bytecode)
2140 {
2141 null_condition_or_parse_error = 1;
2142 break;
2143 }
2144 }
2145 }
2146
2147 /* If any of these happened, it means we will have to evaluate the conditions
2148 for the location's address on gdb's side. It is no use keeping bytecodes
2149 for all the other duplicate locations, thus we free all of them here.
2150
2151 This is so we have a finer control over which locations' conditions are
2152 being evaluated by GDB or the remote stub. */
2153 if (null_condition_or_parse_error)
2154 {
2155 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2156 {
2157 loc = (*loc2p);
2158 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2159 {
2160 /* Only go as far as the first NULL bytecode is
2161 located. */
2162 if (!loc->cond_bytecode)
2163 return;
2164
2165 free_agent_expr (loc->cond_bytecode);
2166 loc->cond_bytecode = NULL;
2167 }
2168 }
2169 }
2170
2171 /* No NULL conditions or failed bytecode generation. Build a condition list
2172 for this location's address. */
2173 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2174 {
2175 loc = (*loc2p);
2176 if (loc->cond
2177 && is_breakpoint (loc->owner)
2178 && loc->pspace->num == bl->pspace->num
2179 && loc->owner->enable_state == bp_enabled
2180 && loc->enabled)
2181 /* Add the condition to the vector. This will be used later to send the
2182 conditions to the target. */
2183 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2184 loc->cond_bytecode);
2185 }
2186
2187 return;
2188}
2189
d3ce09f5
SS
2190/* Parses a command described by string CMD into an agent expression
2191 bytecode suitable for evaluation by the bytecode interpreter.
2192 Return NULL if there was any error during parsing. */
2193
2194static struct agent_expr *
2195parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2196{
2197 struct cleanup *old_cleanups = 0;
2198 struct expression *expr, **argvec;
2199 struct agent_expr *aexpr = NULL;
d3ce09f5 2200 volatile struct gdb_exception ex;
bbc13ae3
KS
2201 const char *cmdrest;
2202 const char *format_start, *format_end;
d3ce09f5
SS
2203 struct format_piece *fpieces;
2204 int nargs;
2205 struct gdbarch *gdbarch = get_current_arch ();
2206
2207 if (!cmd)
2208 return NULL;
2209
2210 cmdrest = cmd;
2211
2212 if (*cmdrest == ',')
2213 ++cmdrest;
bbc13ae3 2214 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2215
2216 if (*cmdrest++ != '"')
2217 error (_("No format string following the location"));
2218
2219 format_start = cmdrest;
2220
2221 fpieces = parse_format_string (&cmdrest);
2222
2223 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2224
2225 format_end = cmdrest;
2226
2227 if (*cmdrest++ != '"')
2228 error (_("Bad format string, non-terminated '\"'."));
2229
bbc13ae3 2230 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2231
2232 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2233 error (_("Invalid argument syntax"));
2234
2235 if (*cmdrest == ',')
2236 cmdrest++;
bbc13ae3 2237 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2238
2239 /* For each argument, make an expression. */
2240
2241 argvec = (struct expression **) alloca (strlen (cmd)
2242 * sizeof (struct expression *));
2243
2244 nargs = 0;
2245 while (*cmdrest != '\0')
2246 {
bbc13ae3 2247 const char *cmd1;
d3ce09f5
SS
2248
2249 cmd1 = cmdrest;
2250 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2251 argvec[nargs++] = expr;
2252 cmdrest = cmd1;
2253 if (*cmdrest == ',')
2254 ++cmdrest;
2255 }
2256
2257 /* We don't want to stop processing, so catch any errors
2258 that may show up. */
2259 TRY_CATCH (ex, RETURN_MASK_ERROR)
2260 {
2261 aexpr = gen_printf (scope, gdbarch, 0, 0,
2262 format_start, format_end - format_start,
2263 fpieces, nargs, argvec);
2264 }
2265
752eb8b4
TT
2266 do_cleanups (old_cleanups);
2267
d3ce09f5
SS
2268 if (ex.reason < 0)
2269 {
2270 /* If we got here, it means the command could not be parsed to a valid
2271 bytecode expression and thus can't be evaluated on the target's side.
2272 It's no use iterating through the other commands. */
2273 return NULL;
2274 }
2275
d3ce09f5
SS
2276 /* We have a valid agent expression, return it. */
2277 return aexpr;
2278}
2279
2280/* Based on location BL, create a list of breakpoint commands to be
2281 passed on to the target. If we have duplicated locations with
2282 different commands, we will add any such to the list. */
2283
2284static void
2285build_target_command_list (struct bp_location *bl)
2286{
2287 struct bp_location **locp = NULL, **loc2p;
2288 int null_command_or_parse_error = 0;
2289 int modified = bl->needs_update;
2290 struct bp_location *loc;
2291
8b4f3082
PA
2292 /* Release commands left over from a previous insert. */
2293 VEC_free (agent_expr_p, bl->target_info.tcommands);
2294
d3ce09f5
SS
2295 /* For now, limit to agent-style dprintf breakpoints. */
2296 if (bl->owner->type != bp_dprintf
2297 || strcmp (dprintf_style, dprintf_style_agent) != 0)
2298 return;
2299
2300 if (!target_can_run_breakpoint_commands ())
2301 return;
2302
2303 /* Do a first pass to check for locations with no assigned
2304 conditions or conditions that fail to parse to a valid agent expression
2305 bytecode. If any of these happen, then it's no use to send conditions
2306 to the target since this location will always trigger and generate a
2307 response back to GDB. */
2308 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2309 {
2310 loc = (*loc2p);
2311 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2312 {
2313 if (modified)
2314 {
2315 struct agent_expr *aexpr;
2316
2317 /* Re-parse the commands since something changed. In that
2318 case we already freed the command bytecodes (see
2319 force_breakpoint_reinsertion). We just
2320 need to parse the command to bytecodes again. */
2321 aexpr = parse_cmd_to_aexpr (bl->address,
2322 loc->owner->extra_string);
2323 loc->cmd_bytecode = aexpr;
2324
2325 if (!aexpr)
2326 continue;
2327 }
2328
2329 /* If we have a NULL bytecode expression, it means something
2330 went wrong or we have a null command expression. */
2331 if (!loc->cmd_bytecode)
2332 {
2333 null_command_or_parse_error = 1;
2334 break;
2335 }
2336 }
2337 }
2338
2339 /* If anything failed, then we're not doing target-side commands,
2340 and so clean up. */
2341 if (null_command_or_parse_error)
2342 {
2343 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2344 {
2345 loc = (*loc2p);
2346 if (is_breakpoint (loc->owner)
2347 && loc->pspace->num == bl->pspace->num)
2348 {
2349 /* Only go as far as the first NULL bytecode is
2350 located. */
40fb6c5e 2351 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2352 return;
2353
40fb6c5e
HZ
2354 free_agent_expr (loc->cmd_bytecode);
2355 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2356 }
2357 }
2358 }
2359
2360 /* No NULL commands or failed bytecode generation. Build a command list
2361 for this location's address. */
2362 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2363 {
2364 loc = (*loc2p);
2365 if (loc->owner->extra_string
2366 && is_breakpoint (loc->owner)
2367 && loc->pspace->num == bl->pspace->num
2368 && loc->owner->enable_state == bp_enabled
2369 && loc->enabled)
2370 /* Add the command to the vector. This will be used later
2371 to send the commands to the target. */
2372 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2373 loc->cmd_bytecode);
2374 }
2375
2376 bl->target_info.persist = 0;
2377 /* Maybe flag this location as persistent. */
2378 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2379 bl->target_info.persist = 1;
2380}
2381
35df4500
TJB
2382/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2383 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2384 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2385 Returns 0 for success, 1 if the bp_location type is not supported or
2386 -1 for failure.
879bfdc2 2387
4a64f543
MS
2388 NOTE drow/2003-09-09: This routine could be broken down to an
2389 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2390static int
35df4500 2391insert_bp_location (struct bp_location *bl,
26bb91f3 2392 struct ui_file *tmp_error_stream,
3fbb6ffa 2393 int *disabled_breaks,
dd61ec5c
MW
2394 int *hw_breakpoint_error,
2395 int *hw_bp_error_explained_already)
879bfdc2
DJ
2396{
2397 int val = 0;
b15e5c54 2398 const char *hw_bp_err_string = NULL;
dd61ec5c 2399 struct gdb_exception e;
879bfdc2 2400
b775012e 2401 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2402 return 0;
2403
35c63cd8
JB
2404 /* Note we don't initialize bl->target_info, as that wipes out
2405 the breakpoint location's shadow_contents if the breakpoint
2406 is still inserted at that location. This in turn breaks
2407 target_read_memory which depends on these buffers when
2408 a memory read is requested at the breakpoint location:
2409 Once the target_info has been wiped, we fail to see that
2410 we have a breakpoint inserted at that address and thus
2411 read the breakpoint instead of returning the data saved in
2412 the breakpoint location's shadow contents. */
35df4500
TJB
2413 bl->target_info.placed_address = bl->address;
2414 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2415 bl->target_info.length = bl->length;
8181d85f 2416
b775012e
LM
2417 /* When working with target-side conditions, we must pass all the conditions
2418 for the same breakpoint address down to the target since GDB will not
2419 insert those locations. With a list of breakpoint conditions, the target
2420 can decide when to stop and notify GDB. */
2421
2422 if (is_breakpoint (bl->owner))
2423 {
2424 build_target_condition_list (bl);
d3ce09f5
SS
2425 build_target_command_list (bl);
2426 /* Reset the modification marker. */
b775012e
LM
2427 bl->needs_update = 0;
2428 }
2429
35df4500
TJB
2430 if (bl->loc_type == bp_loc_software_breakpoint
2431 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2432 {
35df4500 2433 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2434 {
2435 /* If the explicitly specified breakpoint type
2436 is not hardware breakpoint, check the memory map to see
2437 if the breakpoint address is in read only memory or not.
4a64f543 2438
765dc015
VP
2439 Two important cases are:
2440 - location type is not hardware breakpoint, memory
2441 is readonly. We change the type of the location to
2442 hardware breakpoint.
4a64f543
MS
2443 - location type is hardware breakpoint, memory is
2444 read-write. This means we've previously made the
2445 location hardware one, but then the memory map changed,
2446 so we undo.
765dc015 2447
4a64f543
MS
2448 When breakpoints are removed, remove_breakpoints will use
2449 location types we've just set here, the only possible
2450 problem is that memory map has changed during running
2451 program, but it's not going to work anyway with current
2452 gdb. */
765dc015 2453 struct mem_region *mr
35df4500 2454 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2455
2456 if (mr)
2457 {
2458 if (automatic_hardware_breakpoints)
2459 {
765dc015
VP
2460 enum bp_loc_type new_type;
2461
2462 if (mr->attrib.mode != MEM_RW)
2463 new_type = bp_loc_hardware_breakpoint;
2464 else
2465 new_type = bp_loc_software_breakpoint;
2466
35df4500 2467 if (new_type != bl->loc_type)
765dc015
VP
2468 {
2469 static int said = 0;
cc59ec59 2470
35df4500 2471 bl->loc_type = new_type;
765dc015
VP
2472 if (!said)
2473 {
3e43a32a
MS
2474 fprintf_filtered (gdb_stdout,
2475 _("Note: automatically using "
2476 "hardware breakpoints for "
2477 "read-only addresses.\n"));
765dc015
VP
2478 said = 1;
2479 }
2480 }
2481 }
35df4500 2482 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 2483 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
2484 warning (_("cannot set software breakpoint "
2485 "at readonly address %s"),
35df4500 2486 paddress (bl->gdbarch, bl->address));
765dc015
VP
2487 }
2488 }
2489
879bfdc2
DJ
2490 /* First check to see if we have to handle an overlay. */
2491 if (overlay_debugging == ovly_off
35df4500
TJB
2492 || bl->section == NULL
2493 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2494 {
2495 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2496 TRY_CATCH (e, RETURN_MASK_ALL)
2497 {
2498 val = bl->owner->ops->insert_location (bl);
2499 }
2500 if (e.reason < 0)
2501 {
2502 val = 1;
b15e5c54 2503 hw_bp_err_string = e.message;
dd61ec5c 2504 }
879bfdc2
DJ
2505 }
2506 else
2507 {
4a64f543 2508 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2509 Shall we set a breakpoint at the LMA? */
2510 if (!overlay_events_enabled)
2511 {
2512 /* Yes -- overlay event support is not active,
2513 so we must try to set a breakpoint at the LMA.
2514 This will not work for a hardware breakpoint. */
35df4500 2515 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2516 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2517 bl->owner->number);
879bfdc2
DJ
2518 else
2519 {
35df4500
TJB
2520 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2521 bl->section);
879bfdc2 2522 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2523 bl->overlay_target_info = bl->target_info;
2524 bl->overlay_target_info.placed_address = addr;
2525 val = target_insert_breakpoint (bl->gdbarch,
2526 &bl->overlay_target_info);
879bfdc2 2527 if (val != 0)
99361f52 2528 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2529 "Overlay breakpoint %d "
2530 "failed: in ROM?\n",
35df4500 2531 bl->owner->number);
879bfdc2
DJ
2532 }
2533 }
2534 /* Shall we set a breakpoint at the VMA? */
35df4500 2535 if (section_is_mapped (bl->section))
879bfdc2
DJ
2536 {
2537 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2538 TRY_CATCH (e, RETURN_MASK_ALL)
2539 {
2540 val = bl->owner->ops->insert_location (bl);
2541 }
2542 if (e.reason < 0)
2543 {
2544 val = 1;
b15e5c54 2545 hw_bp_err_string = e.message;
dd61ec5c 2546 }
879bfdc2
DJ
2547 }
2548 else
2549 {
2550 /* No. This breakpoint will not be inserted.
2551 No error, but do not mark the bp as 'inserted'. */
2552 return 0;
2553 }
2554 }
2555
2556 if (val)
2557 {
2558 /* Can't set the breakpoint. */
35df4500 2559 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 2560 {
4a64f543 2561 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 2562 val = 0;
35df4500 2563 bl->shlib_disabled = 1;
8d3788bd 2564 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2565 if (!*disabled_breaks)
2566 {
2567 fprintf_unfiltered (tmp_error_stream,
2568 "Cannot insert breakpoint %d.\n",
2569 bl->owner->number);
2570 fprintf_unfiltered (tmp_error_stream,
2571 "Temporarily disabling shared "
2572 "library breakpoints:\n");
2573 }
2574 *disabled_breaks = 1;
879bfdc2 2575 fprintf_unfiltered (tmp_error_stream,
35df4500 2576 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
2577 }
2578 else
879bfdc2 2579 {
35df4500 2580 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2581 {
dd61ec5c
MW
2582 *hw_breakpoint_error = 1;
2583 *hw_bp_error_explained_already = hw_bp_err_string != NULL;
2584 fprintf_unfiltered (tmp_error_stream,
2585 "Cannot insert hardware breakpoint %d%s",
2586 bl->owner->number, hw_bp_err_string ? ":" : ".\n");
2587 if (hw_bp_err_string)
2588 fprintf_unfiltered (tmp_error_stream, "%s.\n", hw_bp_err_string);
879bfdc2
DJ
2589 }
2590 else
2591 {
578d3588
PA
2592 char *message = memory_error_message (TARGET_XFER_E_IO,
2593 bl->gdbarch, bl->address);
2594 struct cleanup *old_chain = make_cleanup (xfree, message);
2595
879bfdc2 2596 fprintf_unfiltered (tmp_error_stream,
578d3588
PA
2597 "Cannot insert breakpoint %d.\n"
2598 "%s\n",
2599 bl->owner->number, message);
2600
2601 do_cleanups (old_chain);
879bfdc2
DJ
2602 }
2603
2604 }
2605 }
2606 else
35df4500 2607 bl->inserted = 1;
879bfdc2
DJ
2608
2609 return val;
2610 }
2611
35df4500 2612 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2613 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2614 watchpoints. It's not clear that it's necessary... */
35df4500 2615 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2616 {
77b06cd7
TJB
2617 gdb_assert (bl->owner->ops != NULL
2618 && bl->owner->ops->insert_location != NULL);
2619
2620 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2621
2622 /* If trying to set a read-watchpoint, and it turns out it's not
2623 supported, try emulating one with an access watchpoint. */
35df4500 2624 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2625 {
2626 struct bp_location *loc, **loc_temp;
2627
2628 /* But don't try to insert it, if there's already another
2629 hw_access location that would be considered a duplicate
2630 of this one. */
2631 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2632 if (loc != bl
85d721b8 2633 && loc->watchpoint_type == hw_access
35df4500 2634 && watchpoint_locations_match (bl, loc))
85d721b8 2635 {
35df4500
TJB
2636 bl->duplicate = 1;
2637 bl->inserted = 1;
2638 bl->target_info = loc->target_info;
2639 bl->watchpoint_type = hw_access;
85d721b8
PA
2640 val = 0;
2641 break;
2642 }
2643
2644 if (val == 1)
2645 {
77b06cd7
TJB
2646 bl->watchpoint_type = hw_access;
2647 val = bl->owner->ops->insert_location (bl);
2648
2649 if (val)
2650 /* Back to the original value. */
2651 bl->watchpoint_type = hw_read;
85d721b8
PA
2652 }
2653 }
2654
35df4500 2655 bl->inserted = (val == 0);
879bfdc2
DJ
2656 }
2657
35df4500 2658 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2659 {
77b06cd7
TJB
2660 gdb_assert (bl->owner->ops != NULL
2661 && bl->owner->ops->insert_location != NULL);
2662
2663 val = bl->owner->ops->insert_location (bl);
2664 if (val)
2665 {
2666 bl->owner->enable_state = bp_disabled;
2667
2668 if (val == 1)
2669 warning (_("\
2670Error inserting catchpoint %d: Your system does not support this type\n\
2671of catchpoint."), bl->owner->number);
2672 else
2673 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2674 }
2675
2676 bl->inserted = (val == 0);
1640b821
DJ
2677
2678 /* We've already printed an error message if there was a problem
2679 inserting this catchpoint, and we've disabled the catchpoint,
2680 so just return success. */
2681 return 0;
879bfdc2
DJ
2682 }
2683
2684 return 0;
2685}
2686
6c95b8df
PA
2687/* This function is called when program space PSPACE is about to be
2688 deleted. It takes care of updating breakpoints to not reference
2689 PSPACE anymore. */
2690
2691void
2692breakpoint_program_space_exit (struct program_space *pspace)
2693{
2694 struct breakpoint *b, *b_temp;
876fa593 2695 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2696
2697 /* Remove any breakpoint that was set through this program space. */
2698 ALL_BREAKPOINTS_SAFE (b, b_temp)
2699 {
2700 if (b->pspace == pspace)
2701 delete_breakpoint (b);
2702 }
2703
2704 /* Breakpoints set through other program spaces could have locations
2705 bound to PSPACE as well. Remove those. */
876fa593 2706 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2707 {
2708 struct bp_location *tmp;
2709
2710 if (loc->pspace == pspace)
2711 {
2bdf28a0 2712 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2713 if (loc->owner->loc == loc)
2714 loc->owner->loc = loc->next;
2715 else
2716 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2717 if (tmp->next == loc)
2718 {
2719 tmp->next = loc->next;
2720 break;
2721 }
2722 }
2723 }
2724
2725 /* Now update the global location list to permanently delete the
2726 removed locations above. */
2727 update_global_location_list (0);
2728}
2729
74960c60
VP
2730/* Make sure all breakpoints are inserted in inferior.
2731 Throws exception on any error.
2732 A breakpoint that is already inserted won't be inserted
2733 again, so calling this function twice is safe. */
2734void
2735insert_breakpoints (void)
2736{
2737 struct breakpoint *bpt;
2738
2739 ALL_BREAKPOINTS (bpt)
2740 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2741 {
2742 struct watchpoint *w = (struct watchpoint *) bpt;
2743
2744 update_watchpoint (w, 0 /* don't reparse. */);
2745 }
74960c60 2746
b60e7edf 2747 update_global_location_list (1);
74960c60 2748
c35b1492
PA
2749 /* update_global_location_list does not insert breakpoints when
2750 always_inserted_mode is not enabled. Explicitly insert them
2751 now. */
2752 if (!breakpoints_always_inserted_mode ())
74960c60
VP
2753 insert_breakpoint_locations ();
2754}
2755
20388dd6
YQ
2756/* Invoke CALLBACK for each of bp_location. */
2757
2758void
2759iterate_over_bp_locations (walk_bp_location_callback callback)
2760{
2761 struct bp_location *loc, **loc_tmp;
2762
2763 ALL_BP_LOCATIONS (loc, loc_tmp)
2764 {
2765 callback (loc, NULL);
2766 }
2767}
2768
b775012e
LM
2769/* This is used when we need to synch breakpoint conditions between GDB and the
2770 target. It is the case with deleting and disabling of breakpoints when using
2771 always-inserted mode. */
2772
2773static void
2774update_inserted_breakpoint_locations (void)
2775{
2776 struct bp_location *bl, **blp_tmp;
2777 int error_flag = 0;
2778 int val = 0;
2779 int disabled_breaks = 0;
2780 int hw_breakpoint_error = 0;
dd61ec5c 2781 int hw_bp_details_reported = 0;
b775012e
LM
2782
2783 struct ui_file *tmp_error_stream = mem_fileopen ();
2784 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2785
2786 /* Explicitly mark the warning -- this will only be printed if
2787 there was an error. */
2788 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2789
2790 save_current_space_and_thread ();
2791
2792 ALL_BP_LOCATIONS (bl, blp_tmp)
2793 {
2794 /* We only want to update software breakpoints and hardware
2795 breakpoints. */
2796 if (!is_breakpoint (bl->owner))
2797 continue;
2798
2799 /* We only want to update locations that are already inserted
2800 and need updating. This is to avoid unwanted insertion during
2801 deletion of breakpoints. */
2802 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2803 continue;
2804
2805 switch_to_program_space_and_thread (bl->pspace);
2806
2807 /* For targets that support global breakpoints, there's no need
2808 to select an inferior to insert breakpoint to. In fact, even
2809 if we aren't attached to any process yet, we should still
2810 insert breakpoints. */
f5656ead 2811 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2812 && ptid_equal (inferior_ptid, null_ptid))
2813 continue;
2814
2815 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 2816 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2817 if (val)
2818 error_flag = val;
2819 }
2820
2821 if (error_flag)
2822 {
2823 target_terminal_ours_for_output ();
2824 error_stream (tmp_error_stream);
2825 }
2826
2827 do_cleanups (cleanups);
2828}
2829
c30eee59 2830/* Used when starting or continuing the program. */
c906108c 2831
74960c60
VP
2832static void
2833insert_breakpoint_locations (void)
c906108c 2834{
a5606eee 2835 struct breakpoint *bpt;
35df4500 2836 struct bp_location *bl, **blp_tmp;
eacd795a 2837 int error_flag = 0;
c906108c 2838 int val = 0;
3fbb6ffa 2839 int disabled_breaks = 0;
81d0cc19 2840 int hw_breakpoint_error = 0;
dd61ec5c 2841 int hw_bp_error_explained_already = 0;
c906108c 2842
81d0cc19 2843 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2844 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2845
81d0cc19
GS
2846 /* Explicitly mark the warning -- this will only be printed if
2847 there was an error. */
2848 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2849
2850 save_current_space_and_thread ();
2851
35df4500 2852 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2853 {
b775012e 2854 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2855 continue;
2856
4a64f543
MS
2857 /* There is no point inserting thread-specific breakpoints if
2858 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2859 has BL->OWNER always non-NULL. */
35df4500
TJB
2860 if (bl->owner->thread != -1
2861 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2862 continue;
2863
35df4500 2864 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2865
2866 /* For targets that support global breakpoints, there's no need
2867 to select an inferior to insert breakpoint to. In fact, even
2868 if we aren't attached to any process yet, we should still
2869 insert breakpoints. */
f5656ead 2870 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
2871 && ptid_equal (inferior_ptid, null_ptid))
2872 continue;
2873
3fbb6ffa 2874 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 2875 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 2876 if (val)
eacd795a 2877 error_flag = val;
879bfdc2 2878 }
c906108c 2879
4a64f543
MS
2880 /* If we failed to insert all locations of a watchpoint, remove
2881 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2882 ALL_BREAKPOINTS (bpt)
2883 {
2884 int some_failed = 0;
2885 struct bp_location *loc;
2886
2887 if (!is_hardware_watchpoint (bpt))
2888 continue;
2889
d6b74ac4 2890 if (!breakpoint_enabled (bpt))
a5606eee 2891 continue;
74960c60
VP
2892
2893 if (bpt->disposition == disp_del_at_next_stop)
2894 continue;
a5606eee
VP
2895
2896 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2897 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2898 {
2899 some_failed = 1;
2900 break;
2901 }
2902 if (some_failed)
2903 {
2904 for (loc = bpt->loc; loc; loc = loc->next)
2905 if (loc->inserted)
2906 remove_breakpoint (loc, mark_uninserted);
2907
2908 hw_breakpoint_error = 1;
2909 fprintf_unfiltered (tmp_error_stream,
2910 "Could not insert hardware watchpoint %d.\n",
2911 bpt->number);
eacd795a 2912 error_flag = -1;
a5606eee
VP
2913 }
2914 }
2915
eacd795a 2916 if (error_flag)
81d0cc19
GS
2917 {
2918 /* If a hardware breakpoint or watchpoint was inserted, add a
2919 message about possibly exhausted resources. */
dd61ec5c 2920 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 2921 {
c6510018
MS
2922 fprintf_unfiltered (tmp_error_stream,
2923 "Could not insert hardware breakpoints:\n\
2924You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2925 }
81d0cc19
GS
2926 target_terminal_ours_for_output ();
2927 error_stream (tmp_error_stream);
2928 }
f7545552
TT
2929
2930 do_cleanups (cleanups);
c906108c
SS
2931}
2932
c30eee59
TJB
2933/* Used when the program stops.
2934 Returns zero if successful, or non-zero if there was a problem
2935 removing a breakpoint location. */
2936
c906108c 2937int
fba45db2 2938remove_breakpoints (void)
c906108c 2939{
35df4500 2940 struct bp_location *bl, **blp_tmp;
3a1bae8e 2941 int val = 0;
c906108c 2942
35df4500 2943 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2944 {
1e4d1764 2945 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2946 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2947 }
3a1bae8e 2948 return val;
c906108c
SS
2949}
2950
49fa26b0
PA
2951/* When a thread exits, remove breakpoints that are related to
2952 that thread. */
2953
2954static void
2955remove_threaded_breakpoints (struct thread_info *tp, int silent)
2956{
2957 struct breakpoint *b, *b_tmp;
2958
2959 ALL_BREAKPOINTS_SAFE (b, b_tmp)
2960 {
96181529 2961 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
2962 {
2963 b->disposition = disp_del_at_next_stop;
2964
2965 printf_filtered (_("\
46ecd527 2966Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
2967 b->number, tp->num);
2968
2969 /* Hide it from the user. */
2970 b->number = 0;
2971 }
2972 }
2973}
2974
6c95b8df
PA
2975/* Remove breakpoints of process PID. */
2976
2977int
2978remove_breakpoints_pid (int pid)
2979{
35df4500 2980 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2981 int val;
2982 struct inferior *inf = find_inferior_pid (pid);
2983
35df4500 2984 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2985 {
35df4500 2986 if (bl->pspace != inf->pspace)
6c95b8df
PA
2987 continue;
2988
d3ce09f5
SS
2989 if (bl->owner->type == bp_dprintf)
2990 continue;
2991
35df4500 2992 if (bl->inserted)
6c95b8df 2993 {
35df4500 2994 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2995 if (val != 0)
2996 return val;
2997 }
2998 }
2999 return 0;
3000}
3001
c906108c 3002int
fba45db2 3003reattach_breakpoints (int pid)
c906108c 3004{
6c95b8df 3005 struct cleanup *old_chain;
35df4500 3006 struct bp_location *bl, **blp_tmp;
c906108c 3007 int val;
86b887df 3008 struct ui_file *tmp_error_stream;
dd61ec5c 3009 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3010 struct inferior *inf;
3011 struct thread_info *tp;
3012
3013 tp = any_live_thread_of_process (pid);
3014 if (tp == NULL)
3015 return 1;
3016
3017 inf = find_inferior_pid (pid);
3018 old_chain = save_inferior_ptid ();
3019
3020 inferior_ptid = tp->ptid;
a4954f26 3021
86b887df 3022 tmp_error_stream = mem_fileopen ();
a4954f26 3023 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3024
35df4500 3025 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3026 {
35df4500 3027 if (bl->pspace != inf->pspace)
6c95b8df
PA
3028 continue;
3029
35df4500 3030 if (bl->inserted)
c5aa993b 3031 {
35df4500 3032 bl->inserted = 0;
dd61ec5c 3033 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3034 if (val != 0)
3035 {
ce696e05 3036 do_cleanups (old_chain);
c5aa993b
JM
3037 return val;
3038 }
3039 }
3040 }
ce696e05 3041 do_cleanups (old_chain);
c906108c
SS
3042 return 0;
3043}
3044
e58b0e63
PA
3045static int internal_breakpoint_number = -1;
3046
84f4c1fe
PM
3047/* Set the breakpoint number of B, depending on the value of INTERNAL.
3048 If INTERNAL is non-zero, the breakpoint number will be populated
3049 from internal_breakpoint_number and that variable decremented.
e5dd4106 3050 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3051 breakpoint_count and that value incremented. Internal breakpoints
3052 do not set the internal var bpnum. */
3053static void
3054set_breakpoint_number (int internal, struct breakpoint *b)
3055{
3056 if (internal)
3057 b->number = internal_breakpoint_number--;
3058 else
3059 {
3060 set_breakpoint_count (breakpoint_count + 1);
3061 b->number = breakpoint_count;
3062 }
3063}
3064
e62c965a 3065static struct breakpoint *
a6d9a66e 3066create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3067 CORE_ADDR address, enum bptype type,
c0a91b2b 3068 const struct breakpoint_ops *ops)
e62c965a 3069{
e62c965a
PP
3070 struct symtab_and_line sal;
3071 struct breakpoint *b;
3072
4a64f543 3073 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3074
3075 sal.pc = address;
3076 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3077 sal.pspace = current_program_space;
e62c965a 3078
06edf0c0 3079 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3080 b->number = internal_breakpoint_number--;
3081 b->disposition = disp_donttouch;
3082
3083 return b;
3084}
3085
17450429
PP
3086static const char *const longjmp_names[] =
3087 {
3088 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3089 };
3090#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3091
3092/* Per-objfile data private to breakpoint.c. */
3093struct breakpoint_objfile_data
3094{
3095 /* Minimal symbol for "_ovly_debug_event" (if any). */
3096 struct minimal_symbol *overlay_msym;
3097
3098 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3099 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
3100
28106bc2
SDJ
3101 /* True if we have looked for longjmp probes. */
3102 int longjmp_searched;
3103
3104 /* SystemTap probe points for longjmp (if any). */
3105 VEC (probe_p) *longjmp_probes;
3106
17450429
PP
3107 /* Minimal symbol for "std::terminate()" (if any). */
3108 struct minimal_symbol *terminate_msym;
3109
3110 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3111 struct minimal_symbol *exception_msym;
28106bc2
SDJ
3112
3113 /* True if we have looked for exception probes. */
3114 int exception_searched;
3115
3116 /* SystemTap probe points for unwinding (if any). */
3117 VEC (probe_p) *exception_probes;
17450429
PP
3118};
3119
3120static const struct objfile_data *breakpoint_objfile_key;
3121
3122/* Minimal symbol not found sentinel. */
3123static struct minimal_symbol msym_not_found;
3124
3125/* Returns TRUE if MSYM point to the "not found" sentinel. */
3126
3127static int
3128msym_not_found_p (const struct minimal_symbol *msym)
3129{
3130 return msym == &msym_not_found;
3131}
3132
3133/* Return per-objfile data needed by breakpoint.c.
3134 Allocate the data if necessary. */
3135
3136static struct breakpoint_objfile_data *
3137get_breakpoint_objfile_data (struct objfile *objfile)
3138{
3139 struct breakpoint_objfile_data *bp_objfile_data;
3140
3141 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3142 if (bp_objfile_data == NULL)
3143 {
3144 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3145 sizeof (*bp_objfile_data));
3146
3147 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3148 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3149 }
3150 return bp_objfile_data;
3151}
3152
28106bc2
SDJ
3153static void
3154free_breakpoint_probes (struct objfile *obj, void *data)
3155{
3156 struct breakpoint_objfile_data *bp_objfile_data = data;
3157
3158 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3159 VEC_free (probe_p, bp_objfile_data->exception_probes);
3160}
3161
e62c965a 3162static void
af02033e 3163create_overlay_event_breakpoint (void)
e62c965a 3164{
69de3c6a 3165 struct objfile *objfile;
af02033e 3166 const char *const func_name = "_ovly_debug_event";
e62c965a 3167
69de3c6a
PP
3168 ALL_OBJFILES (objfile)
3169 {
3170 struct breakpoint *b;
17450429
PP
3171 struct breakpoint_objfile_data *bp_objfile_data;
3172 CORE_ADDR addr;
69de3c6a 3173
17450429
PP
3174 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3175
3176 if (msym_not_found_p (bp_objfile_data->overlay_msym))
3177 continue;
3178
3179 if (bp_objfile_data->overlay_msym == NULL)
3180 {
3181 struct minimal_symbol *m;
3182
3183 m = lookup_minimal_symbol_text (func_name, objfile);
3184 if (m == NULL)
3185 {
3186 /* Avoid future lookups in this objfile. */
3187 bp_objfile_data->overlay_msym = &msym_not_found;
3188 continue;
3189 }
3190 bp_objfile_data->overlay_msym = m;
3191 }
e62c965a 3192
17450429
PP
3193 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3194 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3195 bp_overlay_event,
3196 &internal_breakpoint_ops);
69de3c6a 3197 b->addr_string = xstrdup (func_name);
e62c965a 3198
69de3c6a
PP
3199 if (overlay_debugging == ovly_auto)
3200 {
3201 b->enable_state = bp_enabled;
3202 overlay_events_enabled = 1;
3203 }
3204 else
3205 {
3206 b->enable_state = bp_disabled;
3207 overlay_events_enabled = 0;
3208 }
e62c965a
PP
3209 }
3210 update_global_location_list (1);
3211}
3212
0fd8e87f 3213static void
af02033e 3214create_longjmp_master_breakpoint (void)
0fd8e87f 3215{
6c95b8df 3216 struct program_space *pspace;
6c95b8df
PA
3217 struct cleanup *old_chain;
3218
3219 old_chain = save_current_program_space ();
0fd8e87f 3220
6c95b8df 3221 ALL_PSPACES (pspace)
af02033e
PP
3222 {
3223 struct objfile *objfile;
3224
3225 set_current_program_space (pspace);
3226
3227 ALL_OBJFILES (objfile)
0fd8e87f 3228 {
af02033e
PP
3229 int i;
3230 struct gdbarch *gdbarch;
17450429 3231 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3232
af02033e 3233 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3234
17450429
PP
3235 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3236
28106bc2
SDJ
3237 if (!bp_objfile_data->longjmp_searched)
3238 {
25f9533e
SDJ
3239 VEC (probe_p) *ret;
3240
3241 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3242 if (ret != NULL)
3243 {
3244 /* We are only interested in checking one element. */
3245 struct probe *p = VEC_index (probe_p, ret, 0);
3246
3247 if (!can_evaluate_probe_arguments (p))
3248 {
3249 /* We cannot use the probe interface here, because it does
3250 not know how to evaluate arguments. */
3251 VEC_free (probe_p, ret);
3252 ret = NULL;
3253 }
3254 }
3255 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3256 bp_objfile_data->longjmp_searched = 1;
3257 }
3258
3259 if (bp_objfile_data->longjmp_probes != NULL)
3260 {
3261 int i;
3262 struct probe *probe;
3263 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3264
3265 for (i = 0;
3266 VEC_iterate (probe_p,
3267 bp_objfile_data->longjmp_probes,
3268 i, probe);
3269 ++i)
3270 {
3271 struct breakpoint *b;
3272
3273 b = create_internal_breakpoint (gdbarch, probe->address,
3274 bp_longjmp_master,
3275 &internal_breakpoint_ops);
3276 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3277 b->enable_state = bp_disabled;
3278 }
3279
3280 continue;
3281 }
3282
0569175e
TSD
3283 if (!gdbarch_get_longjmp_target_p (gdbarch))
3284 continue;
3285
17450429 3286 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3287 {
3288 struct breakpoint *b;
af02033e 3289 const char *func_name;
17450429 3290 CORE_ADDR addr;
6c95b8df 3291
17450429 3292 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 3293 continue;
0fd8e87f 3294
17450429
PP
3295 func_name = longjmp_names[i];
3296 if (bp_objfile_data->longjmp_msym[i] == NULL)
3297 {
3298 struct minimal_symbol *m;
3299
3300 m = lookup_minimal_symbol_text (func_name, objfile);
3301 if (m == NULL)
3302 {
3303 /* Prevent future lookups in this objfile. */
3304 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
3305 continue;
3306 }
3307 bp_objfile_data->longjmp_msym[i] = m;
3308 }
3309
3310 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3311 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3312 &internal_breakpoint_ops);
af02033e
PP
3313 b->addr_string = xstrdup (func_name);
3314 b->enable_state = bp_disabled;
3315 }
0fd8e87f 3316 }
af02033e 3317 }
0fd8e87f 3318 update_global_location_list (1);
6c95b8df
PA
3319
3320 do_cleanups (old_chain);
0fd8e87f
UW
3321}
3322
af02033e 3323/* Create a master std::terminate breakpoint. */
aa7d318d 3324static void
af02033e 3325create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3326{
3327 struct program_space *pspace;
aa7d318d 3328 struct cleanup *old_chain;
af02033e 3329 const char *const func_name = "std::terminate()";
aa7d318d
TT
3330
3331 old_chain = save_current_program_space ();
3332
3333 ALL_PSPACES (pspace)
17450429
PP
3334 {
3335 struct objfile *objfile;
3336 CORE_ADDR addr;
3337
3338 set_current_program_space (pspace);
3339
aa7d318d
TT
3340 ALL_OBJFILES (objfile)
3341 {
3342 struct breakpoint *b;
17450429 3343 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3344
17450429 3345 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3346
17450429
PP
3347 if (msym_not_found_p (bp_objfile_data->terminate_msym))
3348 continue;
3349
3350 if (bp_objfile_data->terminate_msym == NULL)
3351 {
3352 struct minimal_symbol *m;
3353
3354 m = lookup_minimal_symbol (func_name, NULL, objfile);
3355 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
3356 && MSYMBOL_TYPE (m) != mst_file_text))
3357 {
3358 /* Prevent future lookups in this objfile. */
3359 bp_objfile_data->terminate_msym = &msym_not_found;
3360 continue;
3361 }
3362 bp_objfile_data->terminate_msym = m;
3363 }
aa7d318d 3364
17450429
PP
3365 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3366 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3367 bp_std_terminate_master,
3368 &internal_breakpoint_ops);
aa7d318d
TT
3369 b->addr_string = xstrdup (func_name);
3370 b->enable_state = bp_disabled;
3371 }
17450429
PP
3372 }
3373
aa7d318d
TT
3374 update_global_location_list (1);
3375
3376 do_cleanups (old_chain);
3377}
3378
186c406b
TT
3379/* Install a master breakpoint on the unwinder's debug hook. */
3380
70221824 3381static void
186c406b
TT
3382create_exception_master_breakpoint (void)
3383{
3384 struct objfile *objfile;
17450429 3385 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3386
3387 ALL_OBJFILES (objfile)
3388 {
17450429
PP
3389 struct breakpoint *b;
3390 struct gdbarch *gdbarch;
3391 struct breakpoint_objfile_data *bp_objfile_data;
3392 CORE_ADDR addr;
3393
3394 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3395
28106bc2
SDJ
3396 /* We prefer the SystemTap probe point if it exists. */
3397 if (!bp_objfile_data->exception_searched)
3398 {
25f9533e
SDJ
3399 VEC (probe_p) *ret;
3400
3401 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3402
3403 if (ret != NULL)
3404 {
3405 /* We are only interested in checking one element. */
3406 struct probe *p = VEC_index (probe_p, ret, 0);
3407
3408 if (!can_evaluate_probe_arguments (p))
3409 {
3410 /* We cannot use the probe interface here, because it does
3411 not know how to evaluate arguments. */
3412 VEC_free (probe_p, ret);
3413 ret = NULL;
3414 }
3415 }
3416 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3417 bp_objfile_data->exception_searched = 1;
3418 }
3419
3420 if (bp_objfile_data->exception_probes != NULL)
3421 {
3422 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3423 int i;
3424 struct probe *probe;
3425
3426 for (i = 0;
3427 VEC_iterate (probe_p,
3428 bp_objfile_data->exception_probes,
3429 i, probe);
3430 ++i)
3431 {
3432 struct breakpoint *b;
3433
3434 b = create_internal_breakpoint (gdbarch, probe->address,
3435 bp_exception_master,
3436 &internal_breakpoint_ops);
3437 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3438 b->enable_state = bp_disabled;
3439 }
3440
3441 continue;
3442 }
3443
3444 /* Otherwise, try the hook function. */
3445
17450429
PP
3446 if (msym_not_found_p (bp_objfile_data->exception_msym))
3447 continue;
3448
3449 gdbarch = get_objfile_arch (objfile);
186c406b 3450
17450429 3451 if (bp_objfile_data->exception_msym == NULL)
186c406b 3452 {
17450429 3453 struct minimal_symbol *debug_hook;
186c406b 3454
17450429
PP
3455 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3456 if (debug_hook == NULL)
3457 {
3458 bp_objfile_data->exception_msym = &msym_not_found;
3459 continue;
3460 }
3461
3462 bp_objfile_data->exception_msym = debug_hook;
186c406b 3463 }
17450429
PP
3464
3465 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3466 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3467 &current_target);
06edf0c0
PA
3468 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3469 &internal_breakpoint_ops);
17450429
PP
3470 b->addr_string = xstrdup (func_name);
3471 b->enable_state = bp_disabled;
186c406b
TT
3472 }
3473
3474 update_global_location_list (1);
3475}
3476
c906108c 3477void
fba45db2 3478update_breakpoints_after_exec (void)
c906108c 3479{
35df4500 3480 struct breakpoint *b, *b_tmp;
876fa593 3481 struct bp_location *bploc, **bplocp_tmp;
c906108c 3482
25b22b0a
PA
3483 /* We're about to delete breakpoints from GDB's lists. If the
3484 INSERTED flag is true, GDB will try to lift the breakpoints by
3485 writing the breakpoints' "shadow contents" back into memory. The
3486 "shadow contents" are NOT valid after an exec, so GDB should not
3487 do that. Instead, the target is responsible from marking
3488 breakpoints out as soon as it detects an exec. We don't do that
3489 here instead, because there may be other attempts to delete
3490 breakpoints after detecting an exec and before reaching here. */
876fa593 3491 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3492 if (bploc->pspace == current_program_space)
3493 gdb_assert (!bploc->inserted);
c906108c 3494
35df4500 3495 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3496 {
6c95b8df
PA
3497 if (b->pspace != current_program_space)
3498 continue;
3499
4a64f543 3500 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3501 if (b->type == bp_shlib_event)
3502 {
3503 delete_breakpoint (b);
3504 continue;
3505 }
c906108c 3506
4a64f543 3507 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3508 if (b->type == bp_jit_event)
3509 {
3510 delete_breakpoint (b);
3511 continue;
3512 }
3513
1900040c 3514 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3515 as must overlay event and longjmp master breakpoints. */
3516 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3517 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3518 || b->type == bp_exception_master)
c4093a6a
JM
3519 {
3520 delete_breakpoint (b);
3521 continue;
3522 }
3523
4a64f543 3524 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3525 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3526 {
3527 delete_breakpoint (b);
3528 continue;
3529 }
3530
611c83ae
PA
3531 /* Longjmp and longjmp-resume breakpoints are also meaningless
3532 after an exec. */
186c406b 3533 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3534 || b->type == bp_longjmp_call_dummy
186c406b 3535 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3536 {
3537 delete_breakpoint (b);
3538 continue;
3539 }
3540
ce78b96d
JB
3541 if (b->type == bp_catchpoint)
3542 {
3543 /* For now, none of the bp_catchpoint breakpoints need to
3544 do anything at this point. In the future, if some of
3545 the catchpoints need to something, we will need to add
3546 a new method, and call this method from here. */
3547 continue;
3548 }
3549
c5aa993b
JM
3550 /* bp_finish is a special case. The only way we ought to be able
3551 to see one of these when an exec() has happened, is if the user
3552 caught a vfork, and then said "finish". Ordinarily a finish just
3553 carries them to the call-site of the current callee, by setting
3554 a temporary bp there and resuming. But in this case, the finish
3555 will carry them entirely through the vfork & exec.
3556
3557 We don't want to allow a bp_finish to remain inserted now. But
3558 we can't safely delete it, 'cause finish_command has a handle to
3559 the bp on a bpstat, and will later want to delete it. There's a
3560 chance (and I've seen it happen) that if we delete the bp_finish
3561 here, that its storage will get reused by the time finish_command
3562 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3563 We really must allow finish_command to delete a bp_finish.
3564
e5dd4106 3565 In the absence of a general solution for the "how do we know
53a5351d
JM
3566 it's safe to delete something others may have handles to?"
3567 problem, what we'll do here is just uninsert the bp_finish, and
3568 let finish_command delete it.
3569
3570 (We know the bp_finish is "doomed" in the sense that it's
3571 momentary, and will be deleted as soon as finish_command sees
3572 the inferior stopped. So it doesn't matter that the bp's
3573 address is probably bogus in the new a.out, unlike e.g., the
3574 solib breakpoints.) */
c5aa993b 3575
c5aa993b
JM
3576 if (b->type == bp_finish)
3577 {
3578 continue;
3579 }
3580
3581 /* Without a symbolic address, we have little hope of the
3582 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3583 a.out. */
c5aa993b
JM
3584 if (b->addr_string == NULL)
3585 {
3586 delete_breakpoint (b);
3587 continue;
3588 }
c5aa993b 3589 }
1900040c 3590 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3591 create_overlay_event_breakpoint ();
3592 create_longjmp_master_breakpoint ();
3593 create_std_terminate_master_breakpoint ();
186c406b 3594 create_exception_master_breakpoint ();
c906108c
SS
3595}
3596
3597int
d80ee84f 3598detach_breakpoints (ptid_t ptid)
c906108c 3599{
35df4500 3600 struct bp_location *bl, **blp_tmp;
3a1bae8e 3601 int val = 0;
ce696e05 3602 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3603 struct inferior *inf = current_inferior ();
c5aa993b 3604
dfd4cc63 3605 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3606 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3607
6c95b8df 3608 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3609 inferior_ptid = ptid;
35df4500 3610 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3611 {
35df4500 3612 if (bl->pspace != inf->pspace)
6c95b8df
PA
3613 continue;
3614
bd9673a4
PW
3615 /* This function must physically remove breakpoints locations
3616 from the specified ptid, without modifying the breakpoint
3617 package's state. Locations of type bp_loc_other are only
3618 maintained at GDB side. So, there is no need to remove
3619 these bp_loc_other locations. Moreover, removing these
3620 would modify the breakpoint package's state. */
3621 if (bl->loc_type == bp_loc_other)
3622 continue;
3623
35df4500
TJB
3624 if (bl->inserted)
3625 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3626 }
d03285ec
UW
3627
3628 /* Detach single-step breakpoints as well. */
3629 detach_single_step_breakpoints ();
3630
ce696e05 3631 do_cleanups (old_chain);
3a1bae8e 3632 return val;
c906108c
SS
3633}
3634
35df4500 3635/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3636 Note that this is used to detach breakpoints from a child fork.
3637 When we get here, the child isn't in the inferior list, and neither
3638 do we have objects to represent its address space --- we should
35df4500 3639 *not* look at bl->pspace->aspace here. */
6c95b8df 3640
c906108c 3641static int
35df4500 3642remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3643{
3644 int val;
c5aa993b 3645
35df4500
TJB
3646 /* BL is never in moribund_locations by our callers. */
3647 gdb_assert (bl->owner != NULL);
2bdf28a0 3648
35df4500 3649 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3650 /* Permanent breakpoints cannot be inserted or removed. */
3651 return 0;
3652
74960c60
VP
3653 /* The type of none suggests that owner is actually deleted.
3654 This should not ever happen. */
35df4500 3655 gdb_assert (bl->owner->type != bp_none);
0bde7532 3656
35df4500
TJB
3657 if (bl->loc_type == bp_loc_software_breakpoint
3658 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3659 {
c02f5703
MS
3660 /* "Normal" instruction breakpoint: either the standard
3661 trap-instruction bp (bp_breakpoint), or a
3662 bp_hardware_breakpoint. */
3663
3664 /* First check to see if we have to handle an overlay. */
3665 if (overlay_debugging == ovly_off
35df4500
TJB
3666 || bl->section == NULL
3667 || !(section_is_overlay (bl->section)))
c02f5703
MS
3668 {
3669 /* No overlay handling: just remove the breakpoint. */
348d480f 3670 val = bl->owner->ops->remove_location (bl);
c02f5703 3671 }
c906108c
SS
3672 else
3673 {
4a64f543 3674 /* This breakpoint is in an overlay section.
c02f5703
MS
3675 Did we set a breakpoint at the LMA? */
3676 if (!overlay_events_enabled)
3677 {
3678 /* Yes -- overlay event support is not active, so we
3679 should have set a breakpoint at the LMA. Remove it.
3680 */
c02f5703
MS
3681 /* Ignore any failures: if the LMA is in ROM, we will
3682 have already warned when we failed to insert it. */
35df4500
TJB
3683 if (bl->loc_type == bp_loc_hardware_breakpoint)
3684 target_remove_hw_breakpoint (bl->gdbarch,
3685 &bl->overlay_target_info);
c02f5703 3686 else
35df4500
TJB
3687 target_remove_breakpoint (bl->gdbarch,
3688 &bl->overlay_target_info);
c02f5703
MS
3689 }
3690 /* Did we set a breakpoint at the VMA?
3691 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3692 if (bl->inserted)
c906108c 3693 {
c02f5703
MS
3694 /* Yes -- remove it. Previously we did not bother to
3695 remove the breakpoint if the section had been
3696 unmapped, but let's not rely on that being safe. We
3697 don't know what the overlay manager might do. */
aa67235e
UW
3698
3699 /* However, we should remove *software* breakpoints only
3700 if the section is still mapped, or else we overwrite
3701 wrong code with the saved shadow contents. */
348d480f
PA
3702 if (bl->loc_type == bp_loc_hardware_breakpoint
3703 || section_is_mapped (bl->section))
3704 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3705 else
3706 val = 0;
c906108c 3707 }
c02f5703
MS
3708 else
3709 {
3710 /* No -- not inserted, so no need to remove. No error. */
3711 val = 0;
3712 }
c906108c 3713 }
879d1e6b
UW
3714
3715 /* In some cases, we might not be able to remove a breakpoint
3716 in a shared library that has already been removed, but we
3717 have not yet processed the shlib unload event. */
35df4500 3718 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
3719 val = 0;
3720
c906108c
SS
3721 if (val)
3722 return val;
35df4500 3723 bl->inserted = (is == mark_inserted);
c906108c 3724 }
35df4500 3725 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3726 {
77b06cd7
TJB
3727 gdb_assert (bl->owner->ops != NULL
3728 && bl->owner->ops->remove_location != NULL);
3729
35df4500 3730 bl->inserted = (is == mark_inserted);
77b06cd7 3731 bl->owner->ops->remove_location (bl);
2e70b7b9 3732
c906108c 3733 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3734 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 3735 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3736 bl->owner->number);
c906108c 3737 }
35df4500
TJB
3738 else if (bl->owner->type == bp_catchpoint
3739 && breakpoint_enabled (bl->owner)
3740 && !bl->duplicate)
ce78b96d 3741 {
77b06cd7
TJB
3742 gdb_assert (bl->owner->ops != NULL
3743 && bl->owner->ops->remove_location != NULL);
ce78b96d 3744
77b06cd7 3745 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
3746 if (val)
3747 return val;
77b06cd7 3748
35df4500 3749 bl->inserted = (is == mark_inserted);
ce78b96d 3750 }
c906108c
SS
3751
3752 return 0;
3753}
3754
6c95b8df 3755static int
35df4500 3756remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
3757{
3758 int ret;
3759 struct cleanup *old_chain;
3760
35df4500
TJB
3761 /* BL is never in moribund_locations by our callers. */
3762 gdb_assert (bl->owner != NULL);
2bdf28a0 3763
35df4500 3764 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
3765 /* Permanent breakpoints cannot be inserted or removed. */
3766 return 0;
3767
3768 /* The type of none suggests that owner is actually deleted.
3769 This should not ever happen. */
35df4500 3770 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
3771
3772 old_chain = save_current_space_and_thread ();
3773
35df4500 3774 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3775
35df4500 3776 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
3777
3778 do_cleanups (old_chain);
3779 return ret;
3780}
3781
c906108c
SS
3782/* Clear the "inserted" flag in all breakpoints. */
3783
25b22b0a 3784void
fba45db2 3785mark_breakpoints_out (void)
c906108c 3786{
35df4500 3787 struct bp_location *bl, **blp_tmp;
c906108c 3788
35df4500
TJB
3789 ALL_BP_LOCATIONS (bl, blp_tmp)
3790 if (bl->pspace == current_program_space)
3791 bl->inserted = 0;
c906108c
SS
3792}
3793
53a5351d
JM
3794/* Clear the "inserted" flag in all breakpoints and delete any
3795 breakpoints which should go away between runs of the program.
c906108c
SS
3796
3797 Plus other such housekeeping that has to be done for breakpoints
3798 between runs.
3799
53a5351d
JM
3800 Note: this function gets called at the end of a run (by
3801 generic_mourn_inferior) and when a run begins (by
4a64f543 3802 init_wait_for_inferior). */
c906108c
SS
3803
3804
3805
3806void
fba45db2 3807breakpoint_init_inferior (enum inf_context context)
c906108c 3808{
35df4500
TJB
3809 struct breakpoint *b, *b_tmp;
3810 struct bp_location *bl, **blp_tmp;
1c5cfe86 3811 int ix;
6c95b8df 3812 struct program_space *pspace = current_program_space;
c906108c 3813
50c71eaf
PA
3814 /* If breakpoint locations are shared across processes, then there's
3815 nothing to do. */
f5656ead 3816 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3817 return;
3818
35df4500 3819 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3820 {
35df4500
TJB
3821 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3822 if (bl->pspace == pspace
3823 && bl->owner->enable_state != bp_permanent)
3824 bl->inserted = 0;
6c95b8df 3825 }
075f6582 3826
35df4500 3827 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3828 {
6c95b8df
PA
3829 if (b->loc && b->loc->pspace != pspace)
3830 continue;
3831
c5aa993b
JM
3832 switch (b->type)
3833 {
3834 case bp_call_dummy:
e2e4d78b 3835 case bp_longjmp_call_dummy:
c906108c 3836
c5aa993b 3837 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3838 cause problems when the inferior is rerun, so we better get
3839 rid of it. */
3840
3841 case bp_watchpoint_scope:
3842
3843 /* Also get rid of scope breakpoints. */
3844
3845 case bp_shlib_event:
3846
3847 /* Also remove solib event breakpoints. Their addresses may
3848 have changed since the last time we ran the program.
3849 Actually we may now be debugging against different target;
3850 and so the solib backend that installed this breakpoint may
3851 not be used in by the target. E.g.,
3852
3853 (gdb) file prog-linux
3854 (gdb) run # native linux target
3855 ...
3856 (gdb) kill
3857 (gdb) file prog-win.exe
3858 (gdb) tar rem :9999 # remote Windows gdbserver.
3859 */
c906108c 3860
f59f708a
PA
3861 case bp_step_resume:
3862
3863 /* Also remove step-resume breakpoints. */
3864
c5aa993b
JM
3865 delete_breakpoint (b);
3866 break;
c906108c 3867
c5aa993b
JM
3868 case bp_watchpoint:
3869 case bp_hardware_watchpoint:
3870 case bp_read_watchpoint:
3871 case bp_access_watchpoint:
3a5c3e22
PA
3872 {
3873 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3874
3a5c3e22
PA
3875 /* Likewise for watchpoints on local expressions. */
3876 if (w->exp_valid_block != NULL)
3877 delete_breakpoint (b);
3878 else if (context == inf_starting)
3879 {
3880 /* Reset val field to force reread of starting value in
3881 insert_breakpoints. */
3882 if (w->val)
3883 value_free (w->val);
3884 w->val = NULL;
3885 w->val_valid = 0;
c860120c 3886 }
3a5c3e22 3887 }
c5aa993b
JM
3888 break;
3889 default:
c5aa993b
JM
3890 break;
3891 }
3892 }
1c5cfe86
PA
3893
3894 /* Get rid of the moribund locations. */
35df4500
TJB
3895 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3896 decref_bp_location (&bl);
1c5cfe86 3897 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
3898}
3899
6c95b8df
PA
3900/* These functions concern about actual breakpoints inserted in the
3901 target --- to e.g. check if we need to do decr_pc adjustment or if
3902 we need to hop over the bkpt --- so we check for address space
3903 match, not program space. */
3904
c2c6d25f
JM
3905/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3906 exists at PC. It returns ordinary_breakpoint_here if it's an
3907 ordinary breakpoint, or permanent_breakpoint_here if it's a
3908 permanent breakpoint.
3909 - When continuing from a location with an ordinary breakpoint, we
3910 actually single step once before calling insert_breakpoints.
e5dd4106 3911 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3912 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3913 the target, to advance the PC past the breakpoint. */
c906108c 3914
c2c6d25f 3915enum breakpoint_here
6c95b8df 3916breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 3917{
35df4500 3918 struct bp_location *bl, **blp_tmp;
c2c6d25f 3919 int any_breakpoint_here = 0;
c906108c 3920
35df4500 3921 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3922 {
35df4500
TJB
3923 if (bl->loc_type != bp_loc_software_breakpoint
3924 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3925 continue;
3926
f1310107 3927 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
3928 if ((breakpoint_enabled (bl->owner)
3929 || bl->owner->enable_state == bp_permanent)
f1310107 3930 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3931 {
3932 if (overlay_debugging
35df4500
TJB
3933 && section_is_overlay (bl->section)
3934 && !section_is_mapped (bl->section))
075f6582 3935 continue; /* unmapped overlay -- can't be a match */
35df4500 3936 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
3937 return permanent_breakpoint_here;
3938 else
3939 any_breakpoint_here = 1;
3940 }
3941 }
c906108c 3942
c2c6d25f 3943 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
3944}
3945
1c5cfe86
PA
3946/* Return true if there's a moribund breakpoint at PC. */
3947
3948int
6c95b8df 3949moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
3950{
3951 struct bp_location *loc;
3952 int ix;
3953
3954 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 3955 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
3956 return 1;
3957
3958 return 0;
3959}
c2c6d25f 3960
c36b740a 3961/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
3962 inserted using regular breakpoint_chain / bp_location array
3963 mechanism. This does not check for single-step breakpoints, which
3964 are inserted and removed using direct target manipulation. */
c906108c
SS
3965
3966int
4a64f543
MS
3967regular_breakpoint_inserted_here_p (struct address_space *aspace,
3968 CORE_ADDR pc)
c906108c 3969{
35df4500 3970 struct bp_location *bl, **blp_tmp;
c906108c 3971
35df4500 3972 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3973 {
35df4500
TJB
3974 if (bl->loc_type != bp_loc_software_breakpoint
3975 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3976 continue;
3977
35df4500 3978 if (bl->inserted
f1310107 3979 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3980 {
3981 if (overlay_debugging
35df4500
TJB
3982 && section_is_overlay (bl->section)
3983 && !section_is_mapped (bl->section))
075f6582
DJ
3984 continue; /* unmapped overlay -- can't be a match */
3985 else
3986 return 1;
3987 }
c5aa993b 3988 }
c36b740a
VP
3989 return 0;
3990}
3991
3992/* Returns non-zero iff there's either regular breakpoint
3993 or a single step breakpoint inserted at PC. */
3994
3995int
6c95b8df 3996breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 3997{
6c95b8df 3998 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 3999 return 1;
c906108c 4000
6c95b8df 4001 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4002 return 1;
4003
c906108c
SS
4004 return 0;
4005}
4006
4fa8626c
DJ
4007/* This function returns non-zero iff there is a software breakpoint
4008 inserted at PC. */
4009
4010int
3e43a32a
MS
4011software_breakpoint_inserted_here_p (struct address_space *aspace,
4012 CORE_ADDR pc)
4fa8626c 4013{
35df4500 4014 struct bp_location *bl, **blp_tmp;
4fa8626c 4015
35df4500 4016 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 4017 {
35df4500 4018 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4019 continue;
4020
35df4500
TJB
4021 if (bl->inserted
4022 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 4023 aspace, pc))
4fa8626c
DJ
4024 {
4025 if (overlay_debugging
35df4500
TJB
4026 && section_is_overlay (bl->section)
4027 && !section_is_mapped (bl->section))
4fa8626c
DJ
4028 continue; /* unmapped overlay -- can't be a match */
4029 else
4030 return 1;
4031 }
4032 }
4033
1aafd4da 4034 /* Also check for software single-step breakpoints. */
6c95b8df 4035 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4036 return 1;
4037
4fa8626c
DJ
4038 return 0;
4039}
4040
9093389c
PA
4041int
4042hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4043 CORE_ADDR addr, ULONGEST len)
4044{
4045 struct breakpoint *bpt;
4046
4047 ALL_BREAKPOINTS (bpt)
4048 {
4049 struct bp_location *loc;
4050
4051 if (bpt->type != bp_hardware_watchpoint
4052 && bpt->type != bp_access_watchpoint)
4053 continue;
4054
4055 if (!breakpoint_enabled (bpt))
4056 continue;
4057
4058 for (loc = bpt->loc; loc; loc = loc->next)
4059 if (loc->pspace->aspace == aspace && loc->inserted)
4060 {
4061 CORE_ADDR l, h;
4062
4063 /* Check for intersection. */
4064 l = max (loc->address, addr);
4065 h = min (loc->address + loc->length, addr + len);
4066 if (l < h)
4067 return 1;
4068 }
4069 }
4070 return 0;
4071}
4072
075f6582
DJ
4073/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4074 PC is valid for process/thread PTID. */
c906108c
SS
4075
4076int
6c95b8df
PA
4077breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4078 ptid_t ptid)
c906108c 4079{
35df4500 4080 struct bp_location *bl, **blp_tmp;
4a306c9a 4081 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 4082 int thread = -1;
4a306c9a 4083 int task = 0;
a6f1cd96 4084
35df4500 4085 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4086 {
35df4500
TJB
4087 if (bl->loc_type != bp_loc_software_breakpoint
4088 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4089 continue;
4090
35df4500
TJB
4091 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4092 if (!breakpoint_enabled (bl->owner)
4093 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
4094 continue;
4095
f1310107 4096 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
4097 continue;
4098
35df4500 4099 if (bl->owner->thread != -1)
075f6582 4100 {
a6f1cd96
JB
4101 /* This is a thread-specific breakpoint. Check that ptid
4102 matches that thread. If thread hasn't been computed yet,
4103 it is now time to do so. */
4104 if (thread == -1)
4105 thread = pid_to_thread_id (ptid);
35df4500 4106 if (bl->owner->thread != thread)
a6f1cd96 4107 continue;
075f6582 4108 }
a6f1cd96 4109
35df4500 4110 if (bl->owner->task != 0)
4a306c9a
JB
4111 {
4112 /* This is a task-specific breakpoint. Check that ptid
4113 matches that task. If task hasn't been computed yet,
4114 it is now time to do so. */
4115 if (task == 0)
4116 task = ada_get_task_number (ptid);
35df4500 4117 if (bl->owner->task != task)
4a306c9a
JB
4118 continue;
4119 }
4120
a6f1cd96 4121 if (overlay_debugging
35df4500
TJB
4122 && section_is_overlay (bl->section)
4123 && !section_is_mapped (bl->section))
a6f1cd96
JB
4124 continue; /* unmapped overlay -- can't be a match */
4125
4126 return 1;
c5aa993b 4127 }
c906108c
SS
4128
4129 return 0;
4130}
c906108c 4131\f
c5aa993b 4132
c906108c
SS
4133/* bpstat stuff. External routines' interfaces are documented
4134 in breakpoint.h. */
4135
4136int
c326b90e 4137is_catchpoint (struct breakpoint *ep)
c906108c 4138{
533be4dd 4139 return (ep->type == bp_catchpoint);
c906108c
SS
4140}
4141
f431efe5
PA
4142/* Frees any storage that is part of a bpstat. Does not walk the
4143 'next' chain. */
4144
4145static void
198757a8
VP
4146bpstat_free (bpstat bs)
4147{
4148 if (bs->old_val != NULL)
4149 value_free (bs->old_val);
9add0f1b 4150 decref_counted_command_line (&bs->commands);
f431efe5 4151 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4152 xfree (bs);
4153}
4154
c906108c
SS
4155/* Clear a bpstat so that it says we are not at any breakpoint.
4156 Also free any storage that is part of a bpstat. */
4157
4158void
fba45db2 4159bpstat_clear (bpstat *bsp)
c906108c
SS
4160{
4161 bpstat p;
4162 bpstat q;
4163
4164 if (bsp == 0)
4165 return;
4166 p = *bsp;
4167 while (p != NULL)
4168 {
4169 q = p->next;
198757a8 4170 bpstat_free (p);
c906108c
SS
4171 p = q;
4172 }
4173 *bsp = NULL;
4174}
4175
4176/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4177 is part of the bpstat is copied as well. */
4178
4179bpstat
fba45db2 4180bpstat_copy (bpstat bs)
c906108c
SS
4181{
4182 bpstat p = NULL;
4183 bpstat tmp;
4184 bpstat retval = NULL;
4185
4186 if (bs == NULL)
4187 return bs;
4188
4189 for (; bs != NULL; bs = bs->next)
4190 {
4191 tmp = (bpstat) xmalloc (sizeof (*tmp));
4192 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4193 incref_counted_command_line (tmp->commands);
f431efe5 4194 incref_bp_location (tmp->bp_location_at);
31cc81e9 4195 if (bs->old_val != NULL)
3c3185ac
JK
4196 {
4197 tmp->old_val = value_copy (bs->old_val);
4198 release_value (tmp->old_val);
4199 }
31cc81e9 4200
c906108c
SS
4201 if (p == NULL)
4202 /* This is the first thing in the chain. */
4203 retval = tmp;
4204 else
4205 p->next = tmp;
4206 p = tmp;
4207 }
4208 p->next = NULL;
4209 return retval;
4210}
4211
4a64f543 4212/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4213
4214bpstat
fba45db2 4215bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4216{
c5aa993b
JM
4217 if (bsp == NULL)
4218 return NULL;
c906108c 4219
c5aa993b
JM
4220 for (; bsp != NULL; bsp = bsp->next)
4221 {
f431efe5 4222 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4223 return bsp;
4224 }
c906108c
SS
4225 return NULL;
4226}
4227
ab04a2af
TT
4228/* See breakpoint.h. */
4229
47591c29 4230int
427cd150 4231bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4232{
ab04a2af
TT
4233 for (; bsp != NULL; bsp = bsp->next)
4234 {
427cd150
TT
4235 if (bsp->breakpoint_at == NULL)
4236 {
4237 /* A moribund location can never explain a signal other than
4238 GDB_SIGNAL_TRAP. */
4239 if (sig == GDB_SIGNAL_TRAP)
47591c29 4240 return 1;
427cd150
TT
4241 }
4242 else
47591c29
PA
4243 {
4244 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4245 sig))
4246 return 1;
4247 }
ab04a2af
TT
4248 }
4249
47591c29 4250 return 0;
ab04a2af
TT
4251}
4252
4a64f543
MS
4253/* Put in *NUM the breakpoint number of the first breakpoint we are
4254 stopped at. *BSP upon return is a bpstat which points to the
4255 remaining breakpoints stopped at (but which is not guaranteed to be
4256 good for anything but further calls to bpstat_num).
4257
8671a17b
PA
4258 Return 0 if passed a bpstat which does not indicate any breakpoints.
4259 Return -1 if stopped at a breakpoint that has been deleted since
4260 we set it.
4261 Return 1 otherwise. */
c906108c
SS
4262
4263int
8671a17b 4264bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4265{
4266 struct breakpoint *b;
4267
4268 if ((*bsp) == NULL)
4269 return 0; /* No more breakpoint values */
8671a17b 4270
4a64f543
MS
4271 /* We assume we'll never have several bpstats that correspond to a
4272 single breakpoint -- otherwise, this function might return the
4273 same number more than once and this will look ugly. */
f431efe5 4274 b = (*bsp)->breakpoint_at;
8671a17b
PA
4275 *bsp = (*bsp)->next;
4276 if (b == NULL)
4277 return -1; /* breakpoint that's been deleted since */
4278
4279 *num = b->number; /* We have its number */
4280 return 1;
c906108c
SS
4281}
4282
e93ca019 4283/* See breakpoint.h. */
c906108c
SS
4284
4285void
e93ca019 4286bpstat_clear_actions (void)
c906108c 4287{
e93ca019
JK
4288 struct thread_info *tp;
4289 bpstat bs;
4290
4291 if (ptid_equal (inferior_ptid, null_ptid))
4292 return;
4293
4294 tp = find_thread_ptid (inferior_ptid);
4295 if (tp == NULL)
4296 return;
4297
4298 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4299 {
9add0f1b 4300 decref_counted_command_line (&bs->commands);
abf85f46 4301
c906108c
SS
4302 if (bs->old_val != NULL)
4303 {
4304 value_free (bs->old_val);
4305 bs->old_val = NULL;
4306 }
4307 }
4308}
4309
f3b1572e
PA
4310/* Called when a command is about to proceed the inferior. */
4311
4312static void
4313breakpoint_about_to_proceed (void)
4314{
4315 if (!ptid_equal (inferior_ptid, null_ptid))
4316 {
4317 struct thread_info *tp = inferior_thread ();
4318
4319 /* Allow inferior function calls in breakpoint commands to not
4320 interrupt the command list. When the call finishes
4321 successfully, the inferior will be standing at the same
4322 breakpoint as if nothing happened. */
16c381f0 4323 if (tp->control.in_infcall)
f3b1572e
PA
4324 return;
4325 }
4326
4327 breakpoint_proceeded = 1;
4328}
4329
4a64f543
MS
4330/* Stub for cleaning up our state if we error-out of a breakpoint
4331 command. */
c906108c 4332static void
4efb68b1 4333cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4334{
4335 executing_breakpoint_commands = 0;
4336}
4337
abf85f46
JK
4338/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4339 or its equivalent. */
4340
4341static int
4342command_line_is_silent (struct command_line *cmd)
4343{
4344 return cmd && (strcmp ("silent", cmd->line) == 0
4345 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4346}
4347
4a64f543
MS
4348/* Execute all the commands associated with all the breakpoints at
4349 this location. Any of these commands could cause the process to
4350 proceed beyond this point, etc. We look out for such changes by
4351 checking the global "breakpoint_proceeded" after each command.
c906108c 4352
347bddb7
PA
4353 Returns true if a breakpoint command resumed the inferior. In that
4354 case, it is the caller's responsibility to recall it again with the
4355 bpstat of the current thread. */
4356
4357static int
4358bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4359{
4360 bpstat bs;
4361 struct cleanup *old_chain;
347bddb7 4362 int again = 0;
c906108c
SS
4363
4364 /* Avoid endless recursion if a `source' command is contained
4365 in bs->commands. */
4366 if (executing_breakpoint_commands)
347bddb7 4367 return 0;
c906108c
SS
4368
4369 executing_breakpoint_commands = 1;
4370 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4371
cf6c5ffb
TT
4372 prevent_dont_repeat ();
4373
4a64f543 4374 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4375 bs = *bsp;
4376
4377 breakpoint_proceeded = 0;
4378 for (; bs != NULL; bs = bs->next)
4379 {
9add0f1b 4380 struct counted_command_line *ccmd;
6c50ab1c
JB
4381 struct command_line *cmd;
4382 struct cleanup *this_cmd_tree_chain;
4383
4384 /* Take ownership of the BSP's command tree, if it has one.
4385
4386 The command tree could legitimately contain commands like
4387 'step' and 'next', which call clear_proceed_status, which
4388 frees stop_bpstat's command tree. To make sure this doesn't
4389 free the tree we're executing out from under us, we need to
4390 take ownership of the tree ourselves. Since a given bpstat's
4391 commands are only executed once, we don't need to copy it; we
4392 can clear the pointer in the bpstat, and make sure we free
4393 the tree when we're done. */
9add0f1b
TT
4394 ccmd = bs->commands;
4395 bs->commands = NULL;
abf85f46
JK
4396 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4397 cmd = ccmd ? ccmd->commands : NULL;
4398 if (command_line_is_silent (cmd))
4399 {
4400 /* The action has been already done by bpstat_stop_status. */
4401 cmd = cmd->next;
4402 }
6c50ab1c 4403
c906108c
SS
4404 while (cmd != NULL)
4405 {
4406 execute_control_command (cmd);
4407
4408 if (breakpoint_proceeded)
4409 break;
4410 else
4411 cmd = cmd->next;
4412 }
6c50ab1c
JB
4413
4414 /* We can free this command tree now. */
4415 do_cleanups (this_cmd_tree_chain);
4416
c906108c 4417 if (breakpoint_proceeded)
32c1e744
VP
4418 {
4419 if (target_can_async_p ())
347bddb7
PA
4420 /* If we are in async mode, then the target might be still
4421 running, not stopped at any breakpoint, so nothing for
4422 us to do here -- just return to the event loop. */
4423 ;
32c1e744
VP
4424 else
4425 /* In sync mode, when execute_control_command returns
4426 we're already standing on the next breakpoint.
347bddb7
PA
4427 Breakpoint commands for that stop were not run, since
4428 execute_command does not run breakpoint commands --
4429 only command_line_handler does, but that one is not
4430 involved in execution of breakpoint commands. So, we
4431 can now execute breakpoint commands. It should be
4432 noted that making execute_command do bpstat actions is
4433 not an option -- in this case we'll have recursive
4434 invocation of bpstat for each breakpoint with a
4435 command, and can easily blow up GDB stack. Instead, we
4436 return true, which will trigger the caller to recall us
4437 with the new stop_bpstat. */
4438 again = 1;
4439 break;
32c1e744 4440 }
c906108c 4441 }
c2b8ed2c 4442 do_cleanups (old_chain);
347bddb7
PA
4443 return again;
4444}
4445
4446void
4447bpstat_do_actions (void)
4448{
353d1d73
JK
4449 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4450
347bddb7
PA
4451 /* Do any commands attached to breakpoint we are stopped at. */
4452 while (!ptid_equal (inferior_ptid, null_ptid)
4453 && target_has_execution
4454 && !is_exited (inferior_ptid)
4455 && !is_executing (inferior_ptid))
4456 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4457 and only return when it is stopped at the next breakpoint, we
4458 keep doing breakpoint actions until it returns false to
4459 indicate the inferior was not resumed. */
16c381f0 4460 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4461 break;
353d1d73
JK
4462
4463 discard_cleanups (cleanup_if_error);
c906108c
SS
4464}
4465
fa4727a6
DJ
4466/* Print out the (old or new) value associated with a watchpoint. */
4467
4468static void
4469watchpoint_value_print (struct value *val, struct ui_file *stream)
4470{
4471 if (val == NULL)
4472 fprintf_unfiltered (stream, _("<unreadable>"));
4473 else
79a45b7d
TT
4474 {
4475 struct value_print_options opts;
4476 get_user_print_options (&opts);
4477 value_print (val, stream, &opts);
4478 }
fa4727a6
DJ
4479}
4480
e514a9d6 4481/* Generic routine for printing messages indicating why we
4a64f543 4482 stopped. The behavior of this function depends on the value
e514a9d6
JM
4483 'print_it' in the bpstat structure. Under some circumstances we
4484 may decide not to print anything here and delegate the task to
4a64f543 4485 normal_stop(). */
e514a9d6
JM
4486
4487static enum print_stop_action
4488print_bp_stop_message (bpstat bs)
4489{
4490 switch (bs->print_it)
4491 {
4492 case print_it_noop:
4a64f543 4493 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4494 return PRINT_UNKNOWN;
4495 break;
4496
4497 case print_it_done:
4498 /* We still want to print the frame, but we already printed the
4a64f543 4499 relevant messages. */
e514a9d6
JM
4500 return PRINT_SRC_AND_LOC;
4501 break;
4502
4503 case print_it_normal:
4f8d1dc6 4504 {
f431efe5
PA
4505 struct breakpoint *b = bs->breakpoint_at;
4506
1a6a67de
TJB
4507 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4508 which has since been deleted. */
4509 if (b == NULL)
4510 return PRINT_UNKNOWN;
4511
348d480f
PA
4512 /* Normal case. Call the breakpoint's print_it method. */
4513 return b->ops->print_it (bs);
4f8d1dc6 4514 }
348d480f 4515 break;
3086aeae 4516
e514a9d6 4517 default:
8e65ff28 4518 internal_error (__FILE__, __LINE__,
e2e0b3e5 4519 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4520 break;
c906108c 4521 }
c906108c
SS
4522}
4523
edcc5120
TT
4524/* A helper function that prints a shared library stopped event. */
4525
4526static void
4527print_solib_event (int is_catchpoint)
4528{
4529 int any_deleted
4530 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4531 int any_added
4532 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4533
4534 if (!is_catchpoint)
4535 {
4536 if (any_added || any_deleted)
4537 ui_out_text (current_uiout,
4538 _("Stopped due to shared library event:\n"));
4539 else
4540 ui_out_text (current_uiout,
4541 _("Stopped due to shared library event (no "
4542 "libraries added or removed)\n"));
4543 }
4544
4545 if (ui_out_is_mi_like_p (current_uiout))
4546 ui_out_field_string (current_uiout, "reason",
4547 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4548
4549 if (any_deleted)
4550 {
4551 struct cleanup *cleanup;
4552 char *name;
4553 int ix;
4554
4555 ui_out_text (current_uiout, _(" Inferior unloaded "));
4556 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4557 "removed");
4558 for (ix = 0;
4559 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4560 ix, name);
4561 ++ix)
4562 {
4563 if (ix > 0)
4564 ui_out_text (current_uiout, " ");
4565 ui_out_field_string (current_uiout, "library", name);
4566 ui_out_text (current_uiout, "\n");
4567 }
4568
4569 do_cleanups (cleanup);
4570 }
4571
4572 if (any_added)
4573 {
4574 struct so_list *iter;
4575 int ix;
4576 struct cleanup *cleanup;
4577
4578 ui_out_text (current_uiout, _(" Inferior loaded "));
4579 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4580 "added");
4581 for (ix = 0;
4582 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4583 ix, iter);
4584 ++ix)
4585 {
4586 if (ix > 0)
4587 ui_out_text (current_uiout, " ");
4588 ui_out_field_string (current_uiout, "library", iter->so_name);
4589 ui_out_text (current_uiout, "\n");
4590 }
4591
4592 do_cleanups (cleanup);
4593 }
4594}
4595
e514a9d6
JM
4596/* Print a message indicating what happened. This is called from
4597 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4598 list - a list of the eventpoints that caused this stop. KIND is
4599 the target_waitkind for the stopping event. This
e514a9d6
JM
4600 routine calls the generic print routine for printing a message
4601 about reasons for stopping. This will print (for example) the
4602 "Breakpoint n," part of the output. The return value of this
4603 routine is one of:
c906108c 4604
4a64f543 4605 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4606 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4607 code to print the location. An example is
c5aa993b
JM
4608 "Breakpoint 1, " which should be followed by
4609 the location.
917317f4 4610 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4611 to also print the location part of the message.
4612 An example is the catch/throw messages, which
4a64f543 4613 don't require a location appended to the end.
917317f4 4614 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4615 further info to be printed. */
c906108c 4616
917317f4 4617enum print_stop_action
36dfb11c 4618bpstat_print (bpstat bs, int kind)
c906108c
SS
4619{
4620 int val;
c5aa993b 4621
c906108c 4622 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4623 (Currently all watchpoints go on the bpstat whether hit or not.
4624 That probably could (should) be changed, provided care is taken
c906108c 4625 with respect to bpstat_explains_signal). */
e514a9d6
JM
4626 for (; bs; bs = bs->next)
4627 {
4628 val = print_bp_stop_message (bs);
4629 if (val == PRINT_SRC_ONLY
4630 || val == PRINT_SRC_AND_LOC
4631 || val == PRINT_NOTHING)
4632 return val;
4633 }
c906108c 4634
36dfb11c
TT
4635 /* If we had hit a shared library event breakpoint,
4636 print_bp_stop_message would print out this message. If we hit an
4637 OS-level shared library event, do the same thing. */
4638 if (kind == TARGET_WAITKIND_LOADED)
4639 {
edcc5120 4640 print_solib_event (0);
36dfb11c
TT
4641 return PRINT_NOTHING;
4642 }
4643
e514a9d6 4644 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4645 with and nothing was printed. */
917317f4 4646 return PRINT_UNKNOWN;
c906108c
SS
4647}
4648
c42bd95a
DE
4649/* Evaluate the expression EXP and return 1 if value is zero.
4650 This returns the inverse of the condition because it is called
4651 from catch_errors which returns 0 if an exception happened, and if an
4652 exception happens we want execution to stop.
4a64f543 4653 The argument is a "struct expression *" that has been cast to a
c42bd95a 4654 "void *" to make it pass through catch_errors. */
c906108c
SS
4655
4656static int
4efb68b1 4657breakpoint_cond_eval (void *exp)
c906108c 4658{
278cd55f 4659 struct value *mark = value_mark ();
c5aa993b 4660 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4661
c906108c
SS
4662 value_free_to_mark (mark);
4663 return i;
4664}
4665
5760d0ab 4666/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4667
4668static bpstat
5760d0ab 4669bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4670{
4671 bpstat bs;
4672
4673 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4674 bs->next = NULL;
4675 **bs_link_pointer = bs;
4676 *bs_link_pointer = &bs->next;
f431efe5
PA
4677 bs->breakpoint_at = bl->owner;
4678 bs->bp_location_at = bl;
4679 incref_bp_location (bl);
c906108c
SS
4680 /* If the condition is false, etc., don't do the commands. */
4681 bs->commands = NULL;
4682 bs->old_val = NULL;
4683 bs->print_it = print_it_normal;
4684 return bs;
4685}
4686\f
d983da9c
DJ
4687/* The target has stopped with waitstatus WS. Check if any hardware
4688 watchpoints have triggered, according to the target. */
4689
4690int
4691watchpoints_triggered (struct target_waitstatus *ws)
4692{
d92524f1 4693 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4694 CORE_ADDR addr;
4695 struct breakpoint *b;
4696
4697 if (!stopped_by_watchpoint)
4698 {
4699 /* We were not stopped by a watchpoint. Mark all watchpoints
4700 as not triggered. */
4701 ALL_BREAKPOINTS (b)
cc60f2e3 4702 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4703 {
4704 struct watchpoint *w = (struct watchpoint *) b;
4705
4706 w->watchpoint_triggered = watch_triggered_no;
4707 }
d983da9c
DJ
4708
4709 return 0;
4710 }
4711
4712 if (!target_stopped_data_address (&current_target, &addr))
4713 {
4714 /* We were stopped by a watchpoint, but we don't know where.
4715 Mark all watchpoints as unknown. */
4716 ALL_BREAKPOINTS (b)
cc60f2e3 4717 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4718 {
4719 struct watchpoint *w = (struct watchpoint *) b;
4720
4721 w->watchpoint_triggered = watch_triggered_unknown;
4722 }
d983da9c 4723
3c4797ba 4724 return 1;
d983da9c
DJ
4725 }
4726
4727 /* The target could report the data address. Mark watchpoints
4728 affected by this data address as triggered, and all others as not
4729 triggered. */
4730
4731 ALL_BREAKPOINTS (b)
cc60f2e3 4732 if (is_hardware_watchpoint (b))
d983da9c 4733 {
3a5c3e22 4734 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4735 struct bp_location *loc;
d983da9c 4736
3a5c3e22 4737 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4738 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4739 {
3a5c3e22 4740 if (is_masked_watchpoint (b))
9c06b0b4 4741 {
3a5c3e22
PA
4742 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4743 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4744
4745 if (newaddr == start)
4746 {
3a5c3e22 4747 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4748 break;
4749 }
4750 }
4751 /* Exact match not required. Within range is sufficient. */
4752 else if (target_watchpoint_addr_within_range (&current_target,
4753 addr, loc->address,
4754 loc->length))
4755 {
3a5c3e22 4756 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4757 break;
4758 }
4759 }
d983da9c
DJ
4760 }
4761
4762 return 1;
4763}
4764
c906108c
SS
4765/* Possible return values for watchpoint_check (this can't be an enum
4766 because of check_errors). */
4767/* The watchpoint has been deleted. */
4768#define WP_DELETED 1
4769/* The value has changed. */
4770#define WP_VALUE_CHANGED 2
4771/* The value has not changed. */
4772#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4773/* Ignore this watchpoint, no matter if the value changed or not. */
4774#define WP_IGNORE 4
c906108c
SS
4775
4776#define BP_TEMPFLAG 1
4777#define BP_HARDWAREFLAG 2
4778
4a64f543
MS
4779/* Evaluate watchpoint condition expression and check if its value
4780 changed.
553e4c11
JB
4781
4782 P should be a pointer to struct bpstat, but is defined as a void *
4783 in order for this function to be usable with catch_errors. */
c906108c
SS
4784
4785static int
4efb68b1 4786watchpoint_check (void *p)
c906108c
SS
4787{
4788 bpstat bs = (bpstat) p;
3a5c3e22 4789 struct watchpoint *b;
c906108c
SS
4790 struct frame_info *fr;
4791 int within_current_scope;
4792
f431efe5 4793 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4794 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4795 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4796
f6bc2008
PA
4797 /* If this is a local watchpoint, we only want to check if the
4798 watchpoint frame is in scope if the current thread is the thread
4799 that was used to create the watchpoint. */
4800 if (!watchpoint_in_thread_scope (b))
60e1c644 4801 return WP_IGNORE;
f6bc2008 4802
c906108c
SS
4803 if (b->exp_valid_block == NULL)
4804 within_current_scope = 1;
4805 else
4806 {
edb3359d
DJ
4807 struct frame_info *frame = get_current_frame ();
4808 struct gdbarch *frame_arch = get_frame_arch (frame);
4809 CORE_ADDR frame_pc = get_frame_pc (frame);
4810
4a64f543
MS
4811 /* in_function_epilogue_p() returns a non-zero value if we're
4812 still in the function but the stack frame has already been
4813 invalidated. Since we can't rely on the values of local
4814 variables after the stack has been destroyed, we are treating
4815 the watchpoint in that state as `not changed' without further
4816 checking. Don't mark watchpoints as changed if the current
4817 frame is in an epilogue - even if they are in some other
4818 frame, our view of the stack is likely to be wrong and
4819 frame_find_by_id could error out. */
a0f49112 4820 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 4821 return WP_IGNORE;
a0f49112 4822
101dcfbe 4823 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4824 within_current_scope = (fr != NULL);
69fbadd5
DJ
4825
4826 /* If we've gotten confused in the unwinder, we might have
4827 returned a frame that can't describe this variable. */
edb3359d
DJ
4828 if (within_current_scope)
4829 {
4830 struct symbol *function;
4831
4832 function = get_frame_function (fr);
4833 if (function == NULL
4834 || !contained_in (b->exp_valid_block,
4835 SYMBOL_BLOCK_VALUE (function)))
4836 within_current_scope = 0;
4837 }
69fbadd5 4838
edb3359d 4839 if (within_current_scope)
c906108c
SS
4840 /* If we end up stopping, the current frame will get selected
4841 in normal_stop. So this call to select_frame won't affect
4842 the user. */
0f7d239c 4843 select_frame (fr);
c906108c 4844 }
c5aa993b 4845
c906108c
SS
4846 if (within_current_scope)
4847 {
4a64f543
MS
4848 /* We use value_{,free_to_}mark because it could be a *long*
4849 time before we return to the command level and call
4850 free_all_values. We can't call free_all_values because we
4851 might be in the middle of evaluating a function call. */
c906108c 4852
0cf6dd15 4853 int pc = 0;
9c06b0b4 4854 struct value *mark;
fa4727a6
DJ
4855 struct value *new_val;
4856
3a5c3e22 4857 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
4858 /* Since we don't know the exact trigger address (from
4859 stopped_data_address), just tell the user we've triggered
4860 a mask watchpoint. */
4861 return WP_VALUE_CHANGED;
4862
4863 mark = value_mark ();
3a1115a0 4864 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 4865
4a64f543
MS
4866 /* We use value_equal_contents instead of value_equal because
4867 the latter coerces an array to a pointer, thus comparing just
4868 the address of the array instead of its contents. This is
4869 not what we want. */
fa4727a6 4870 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 4871 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 4872 {
fa4727a6
DJ
4873 if (new_val != NULL)
4874 {
4875 release_value (new_val);
4876 value_free_to_mark (mark);
4877 }
c906108c
SS
4878 bs->old_val = b->val;
4879 b->val = new_val;
fa4727a6 4880 b->val_valid = 1;
c906108c
SS
4881 return WP_VALUE_CHANGED;
4882 }
4883 else
4884 {
60e1c644 4885 /* Nothing changed. */
c906108c 4886 value_free_to_mark (mark);
c906108c
SS
4887 return WP_VALUE_NOT_CHANGED;
4888 }
4889 }
4890 else
4891 {
79a45e25
PA
4892 struct ui_out *uiout = current_uiout;
4893
c906108c 4894 /* This seems like the only logical thing to do because
c5aa993b
JM
4895 if we temporarily ignored the watchpoint, then when
4896 we reenter the block in which it is valid it contains
4897 garbage (in the case of a function, it may have two
4898 garbage values, one before and one after the prologue).
4899 So we can't even detect the first assignment to it and
4900 watch after that (since the garbage may or may not equal
4901 the first value assigned). */
348d480f
PA
4902 /* We print all the stop information in
4903 breakpoint_ops->print_it, but in this case, by the time we
4904 call breakpoint_ops->print_it this bp will be deleted
4905 already. So we have no choice but print the information
4906 here. */
9dc5e2a9 4907 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4908 ui_out_field_string
4909 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 4910 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 4911 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
4912 ui_out_text (uiout,
4913 " deleted because the program has left the block in\n\
8b93c638 4914which its expression is valid.\n");
4ce44c66 4915
cdac0397 4916 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 4917 decref_counted_command_line (&b->base.commands);
d0fb5eae 4918 watchpoint_del_at_next_stop (b);
c906108c
SS
4919
4920 return WP_DELETED;
4921 }
4922}
4923
18a18393 4924/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4925 breakpoint location BL. This function does not check if we should
4926 stop, only if BL explains the stop. */
4927
18a18393 4928static int
6c95b8df 4929bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
4930 struct address_space *aspace, CORE_ADDR bp_addr,
4931 const struct target_waitstatus *ws)
18a18393
VP
4932{
4933 struct breakpoint *b = bl->owner;
4934
348d480f 4935 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4936 gdb_assert (b != NULL);
4937
09ac7c10 4938 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4939}
4940
3a5c3e22
PA
4941/* Determine if the watched values have actually changed, and we
4942 should stop. If not, set BS->stop to 0. */
4943
18a18393
VP
4944static void
4945bpstat_check_watchpoint (bpstat bs)
4946{
2bdf28a0 4947 const struct bp_location *bl;
3a5c3e22 4948 struct watchpoint *b;
2bdf28a0
JK
4949
4950 /* BS is built for existing struct breakpoint. */
f431efe5 4951 bl = bs->bp_location_at;
2bdf28a0 4952 gdb_assert (bl != NULL);
3a5c3e22 4953 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 4954 gdb_assert (b != NULL);
18a18393 4955
18a18393 4956 {
18a18393
VP
4957 int must_check_value = 0;
4958
3a5c3e22 4959 if (b->base.type == bp_watchpoint)
18a18393
VP
4960 /* For a software watchpoint, we must always check the
4961 watched value. */
4962 must_check_value = 1;
4963 else if (b->watchpoint_triggered == watch_triggered_yes)
4964 /* We have a hardware watchpoint (read, write, or access)
4965 and the target earlier reported an address watched by
4966 this watchpoint. */
4967 must_check_value = 1;
4968 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 4969 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
4970 /* We were stopped by a hardware watchpoint, but the target could
4971 not report the data address. We must check the watchpoint's
4972 value. Access and read watchpoints are out of luck; without
4973 a data address, we can't figure it out. */
4974 must_check_value = 1;
3a5c3e22 4975
18a18393
VP
4976 if (must_check_value)
4977 {
3e43a32a
MS
4978 char *message
4979 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 4980 b->base.number);
18a18393
VP
4981 struct cleanup *cleanups = make_cleanup (xfree, message);
4982 int e = catch_errors (watchpoint_check, bs, message,
4983 RETURN_MASK_ALL);
4984 do_cleanups (cleanups);
4985 switch (e)
4986 {
4987 case WP_DELETED:
4988 /* We've already printed what needs to be printed. */
4989 bs->print_it = print_it_done;
4990 /* Stop. */
4991 break;
60e1c644
PA
4992 case WP_IGNORE:
4993 bs->print_it = print_it_noop;
4994 bs->stop = 0;
4995 break;
18a18393 4996 case WP_VALUE_CHANGED:
3a5c3e22 4997 if (b->base.type == bp_read_watchpoint)
18a18393 4998 {
85d721b8
PA
4999 /* There are two cases to consider here:
5000
4a64f543 5001 1. We're watching the triggered memory for reads.
85d721b8
PA
5002 In that case, trust the target, and always report
5003 the watchpoint hit to the user. Even though
5004 reads don't cause value changes, the value may
5005 have changed since the last time it was read, and
5006 since we're not trapping writes, we will not see
5007 those, and as such we should ignore our notion of
5008 old value.
5009
4a64f543 5010 2. We're watching the triggered memory for both
85d721b8
PA
5011 reads and writes. There are two ways this may
5012 happen:
5013
4a64f543 5014 2.1. This is a target that can't break on data
85d721b8
PA
5015 reads only, but can break on accesses (reads or
5016 writes), such as e.g., x86. We detect this case
5017 at the time we try to insert read watchpoints.
5018
4a64f543 5019 2.2. Otherwise, the target supports read
85d721b8
PA
5020 watchpoints, but, the user set an access or write
5021 watchpoint watching the same memory as this read
5022 watchpoint.
5023
5024 If we're watching memory writes as well as reads,
5025 ignore watchpoint hits when we find that the
5026 value hasn't changed, as reads don't cause
5027 changes. This still gives false positives when
5028 the program writes the same value to memory as
5029 what there was already in memory (we will confuse
5030 it for a read), but it's much better than
5031 nothing. */
5032
5033 int other_write_watchpoint = 0;
5034
5035 if (bl->watchpoint_type == hw_read)
5036 {
5037 struct breakpoint *other_b;
5038
5039 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5040 if (other_b->type == bp_hardware_watchpoint
5041 || other_b->type == bp_access_watchpoint)
85d721b8 5042 {
3a5c3e22
PA
5043 struct watchpoint *other_w =
5044 (struct watchpoint *) other_b;
5045
5046 if (other_w->watchpoint_triggered
5047 == watch_triggered_yes)
5048 {
5049 other_write_watchpoint = 1;
5050 break;
5051 }
85d721b8
PA
5052 }
5053 }
5054
5055 if (other_write_watchpoint
5056 || bl->watchpoint_type == hw_access)
5057 {
5058 /* We're watching the same memory for writes,
5059 and the value changed since the last time we
5060 updated it, so this trap must be for a write.
5061 Ignore it. */
5062 bs->print_it = print_it_noop;
5063 bs->stop = 0;
5064 }
18a18393
VP
5065 }
5066 break;
5067 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5068 if (b->base.type == bp_hardware_watchpoint
5069 || b->base.type == bp_watchpoint)
18a18393
VP
5070 {
5071 /* Don't stop: write watchpoints shouldn't fire if
5072 the value hasn't changed. */
5073 bs->print_it = print_it_noop;
5074 bs->stop = 0;
5075 }
5076 /* Stop. */
5077 break;
5078 default:
5079 /* Can't happen. */
5080 case 0:
5081 /* Error from catch_errors. */
3a5c3e22 5082 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5083 watchpoint_del_at_next_stop (b);
18a18393
VP
5084 /* We've already printed what needs to be printed. */
5085 bs->print_it = print_it_done;
5086 break;
5087 }
5088 }
5089 else /* must_check_value == 0 */
5090 {
5091 /* This is a case where some watchpoint(s) triggered, but
5092 not at the address of this watchpoint, or else no
5093 watchpoint triggered after all. So don't print
5094 anything for this watchpoint. */
5095 bs->print_it = print_it_noop;
5096 bs->stop = 0;
5097 }
5098 }
5099}
5100
7d4df6a4
DE
5101/* For breakpoints that are currently marked as telling gdb to stop,
5102 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5103 of breakpoint referred to by BS. If we should not stop for this
5104 breakpoint, set BS->stop to 0. */
f431efe5 5105
18a18393
VP
5106static void
5107bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5108{
5109 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
5110 const struct bp_location *bl;
5111 struct breakpoint *b;
7d4df6a4
DE
5112 int value_is_zero = 0;
5113 struct expression *cond;
5114
5115 gdb_assert (bs->stop);
2bdf28a0
JK
5116
5117 /* BS is built for existing struct breakpoint. */
f431efe5 5118 bl = bs->bp_location_at;
2bdf28a0 5119 gdb_assert (bl != NULL);
f431efe5 5120 b = bs->breakpoint_at;
2bdf28a0 5121 gdb_assert (b != NULL);
18a18393 5122
b775012e
LM
5123 /* Even if the target evaluated the condition on its end and notified GDB, we
5124 need to do so again since GDB does not know if we stopped due to a
5125 breakpoint or a single step breakpoint. */
5126
18a18393 5127 if (frame_id_p (b->frame_id)
edb3359d 5128 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5129 {
7d4df6a4
DE
5130 bs->stop = 0;
5131 return;
5132 }
60e1c644 5133
6c1b0f7b
DE
5134 /* If this is a thread-specific breakpoint, don't waste cpu evaluating the
5135 condition if this isn't the specified thread. */
5136 if (b->thread != -1 && b->thread != thread_id)
5137 {
5138 bs->stop = 0;
5139 return;
5140 }
5141
7d4df6a4
DE
5142 /* Evaluate Python breakpoints that have a "stop" method implemented. */
5143 if (b->py_bp_object)
5144 bs->stop = gdbpy_should_stop (b->py_bp_object);
7371cf6d 5145
7d4df6a4
DE
5146 if (is_watchpoint (b))
5147 {
5148 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5149
7d4df6a4
DE
5150 cond = w->cond_exp;
5151 }
5152 else
5153 cond = bl->cond;
60e1c644 5154
7d4df6a4
DE
5155 if (cond && b->disposition != disp_del_at_next_stop)
5156 {
5157 int within_current_scope = 1;
5158 struct watchpoint * w;
60e1c644 5159
7d4df6a4
DE
5160 /* We use value_mark and value_free_to_mark because it could
5161 be a long time before we return to the command level and
5162 call free_all_values. We can't call free_all_values
5163 because we might be in the middle of evaluating a
5164 function call. */
5165 struct value *mark = value_mark ();
5166
5167 if (is_watchpoint (b))
5168 w = (struct watchpoint *) b;
5169 else
5170 w = NULL;
5171
5172 /* Need to select the frame, with all that implies so that
5173 the conditions will have the right context. Because we
5174 use the frame, we will not see an inlined function's
5175 variables when we arrive at a breakpoint at the start
5176 of the inlined function; the current frame will be the
5177 call site. */
5178 if (w == NULL || w->cond_exp_valid_block == NULL)
5179 select_frame (get_current_frame ());
5180 else
18a18393 5181 {
7d4df6a4
DE
5182 struct frame_info *frame;
5183
5184 /* For local watchpoint expressions, which particular
5185 instance of a local is being watched matters, so we
5186 keep track of the frame to evaluate the expression
5187 in. To evaluate the condition however, it doesn't
5188 really matter which instantiation of the function
5189 where the condition makes sense triggers the
5190 watchpoint. This allows an expression like "watch
5191 global if q > 10" set in `func', catch writes to
5192 global on all threads that call `func', or catch
5193 writes on all recursive calls of `func' by a single
5194 thread. We simply always evaluate the condition in
5195 the innermost frame that's executing where it makes
5196 sense to evaluate the condition. It seems
5197 intuitive. */
5198 frame = block_innermost_frame (w->cond_exp_valid_block);
5199 if (frame != NULL)
5200 select_frame (frame);
5201 else
5202 within_current_scope = 0;
18a18393 5203 }
7d4df6a4
DE
5204 if (within_current_scope)
5205 value_is_zero
5206 = catch_errors (breakpoint_cond_eval, cond,
5207 "Error in testing breakpoint condition:\n",
5208 RETURN_MASK_ALL);
5209 else
18a18393 5210 {
7d4df6a4
DE
5211 warning (_("Watchpoint condition cannot be tested "
5212 "in the current scope"));
5213 /* If we failed to set the right context for this
5214 watchpoint, unconditionally report it. */
5215 value_is_zero = 0;
18a18393 5216 }
7d4df6a4
DE
5217 /* FIXME-someday, should give breakpoint #. */
5218 value_free_to_mark (mark);
18a18393 5219 }
7d4df6a4
DE
5220
5221 if (cond && value_is_zero)
5222 {
5223 bs->stop = 0;
5224 }
7d4df6a4
DE
5225 else if (b->ignore_count > 0)
5226 {
5227 b->ignore_count--;
5228 bs->stop = 0;
5229 /* Increase the hit count even though we don't stop. */
5230 ++(b->hit_count);
5231 observer_notify_breakpoint_modified (b);
5232 }
18a18393
VP
5233}
5234
5235
9709f61c 5236/* Get a bpstat associated with having just stopped at address
d983da9c 5237 BP_ADDR in thread PTID.
c906108c 5238
d983da9c 5239 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5240 don't understand this stop. Result is a chain of bpstat's such
5241 that:
c906108c 5242
c5aa993b 5243 if we don't understand the stop, the result is a null pointer.
c906108c 5244
c5aa993b 5245 if we understand why we stopped, the result is not null.
c906108c 5246
c5aa993b
JM
5247 Each element of the chain refers to a particular breakpoint or
5248 watchpoint at which we have stopped. (We may have stopped for
5249 several reasons concurrently.)
c906108c 5250
c5aa993b
JM
5251 Each element of the chain has valid next, breakpoint_at,
5252 commands, FIXME??? fields. */
c906108c
SS
5253
5254bpstat
6c95b8df 5255bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5256 CORE_ADDR bp_addr, ptid_t ptid,
5257 const struct target_waitstatus *ws)
c906108c 5258{
0d381245 5259 struct breakpoint *b = NULL;
afe38095 5260 struct bp_location *bl;
20874c92 5261 struct bp_location *loc;
5760d0ab
JK
5262 /* First item of allocated bpstat's. */
5263 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5264 /* Pointer to the last thing in the chain currently. */
5760d0ab 5265 bpstat bs;
20874c92 5266 int ix;
429374b8 5267 int need_remove_insert;
f431efe5 5268 int removed_any;
c906108c 5269
f431efe5
PA
5270 /* First, build the bpstat chain with locations that explain a
5271 target stop, while being careful to not set the target running,
5272 as that may invalidate locations (in particular watchpoint
5273 locations are recreated). Resuming will happen here with
5274 breakpoint conditions or watchpoint expressions that include
5275 inferior function calls. */
c5aa993b 5276
429374b8
JK
5277 ALL_BREAKPOINTS (b)
5278 {
5279 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5280 continue;
a5606eee 5281
429374b8
JK
5282 for (bl = b->loc; bl != NULL; bl = bl->next)
5283 {
4a64f543
MS
5284 /* For hardware watchpoints, we look only at the first
5285 location. The watchpoint_check function will work on the
5286 entire expression, not the individual locations. For
5287 read watchpoints, the watchpoints_triggered function has
5288 checked all locations already. */
429374b8
JK
5289 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5290 break;
18a18393 5291
f6592439 5292 if (!bl->enabled || bl->shlib_disabled)
429374b8 5293 continue;
c5aa993b 5294
09ac7c10 5295 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5296 continue;
c5aa993b 5297
4a64f543
MS
5298 /* Come here if it's a watchpoint, or if the break address
5299 matches. */
c5aa993b 5300
4a64f543
MS
5301 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5302 explain stop. */
c5aa993b 5303
f431efe5
PA
5304 /* Assume we stop. Should we find a watchpoint that is not
5305 actually triggered, or if the condition of the breakpoint
5306 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5307 bs->stop = 1;
5308 bs->print = 1;
d983da9c 5309
f431efe5
PA
5310 /* If this is a scope breakpoint, mark the associated
5311 watchpoint as triggered so that we will handle the
5312 out-of-scope event. We'll get to the watchpoint next
5313 iteration. */
d0fb5eae 5314 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5315 {
5316 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5317
5318 w->watchpoint_triggered = watch_triggered_yes;
5319 }
f431efe5
PA
5320 }
5321 }
5322
5323 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5324 {
f1310107 5325 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5326 {
5760d0ab 5327 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5328 /* For hits of moribund locations, we should just proceed. */
5329 bs->stop = 0;
5330 bs->print = 0;
5331 bs->print_it = print_it_noop;
5332 }
5333 }
5334
edcc5120
TT
5335 /* A bit of special processing for shlib breakpoints. We need to
5336 process solib loading here, so that the lists of loaded and
5337 unloaded libraries are correct before we handle "catch load" and
5338 "catch unload". */
5339 for (bs = bs_head; bs != NULL; bs = bs->next)
5340 {
5d268276 5341 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5342 {
5343 handle_solib_event ();
5344 break;
5345 }
5346 }
5347
f431efe5
PA
5348 /* Now go through the locations that caused the target to stop, and
5349 check whether we're interested in reporting this stop to higher
5350 layers, or whether we should resume the target transparently. */
5351
5352 removed_any = 0;
5353
5760d0ab 5354 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5355 {
5356 if (!bs->stop)
5357 continue;
5358
f431efe5 5359 b = bs->breakpoint_at;
348d480f
PA
5360 b->ops->check_status (bs);
5361 if (bs->stop)
28010a5d 5362 {
348d480f 5363 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5364
429374b8
JK
5365 if (bs->stop)
5366 {
5367 ++(b->hit_count);
8d3788bd 5368 observer_notify_breakpoint_modified (b);
c906108c 5369
4a64f543 5370 /* We will stop here. */
429374b8
JK
5371 if (b->disposition == disp_disable)
5372 {
816338b5
SS
5373 --(b->enable_count);
5374 if (b->enable_count <= 0
5375 && b->enable_state != bp_permanent)
429374b8 5376 b->enable_state = bp_disabled;
f431efe5 5377 removed_any = 1;
429374b8
JK
5378 }
5379 if (b->silent)
5380 bs->print = 0;
5381 bs->commands = b->commands;
9add0f1b 5382 incref_counted_command_line (bs->commands);
abf85f46
JK
5383 if (command_line_is_silent (bs->commands
5384 ? bs->commands->commands : NULL))
5385 bs->print = 0;
9d6e6e84
HZ
5386
5387 b->ops->after_condition_true (bs);
429374b8
JK
5388 }
5389
348d480f 5390 }
a9b3a50f
PA
5391
5392 /* Print nothing for this entry if we don't stop or don't
5393 print. */
5394 if (!bs->stop || !bs->print)
5395 bs->print_it = print_it_noop;
429374b8 5396 }
876fa593 5397
d983da9c
DJ
5398 /* If we aren't stopping, the value of some hardware watchpoint may
5399 not have changed, but the intermediate memory locations we are
5400 watching may have. Don't bother if we're stopping; this will get
5401 done later. */
d832cb68 5402 need_remove_insert = 0;
5760d0ab
JK
5403 if (! bpstat_causes_stop (bs_head))
5404 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5405 if (!bs->stop
f431efe5
PA
5406 && bs->breakpoint_at
5407 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5408 {
3a5c3e22
PA
5409 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5410
5411 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5412 need_remove_insert = 1;
d983da9c
DJ
5413 }
5414
d832cb68 5415 if (need_remove_insert)
2d134ed3 5416 update_global_location_list (1);
f431efe5
PA
5417 else if (removed_any)
5418 update_global_location_list (0);
d832cb68 5419
5760d0ab 5420 return bs_head;
c906108c 5421}
628fe4e4
JK
5422
5423static void
5424handle_jit_event (void)
5425{
5426 struct frame_info *frame;
5427 struct gdbarch *gdbarch;
5428
5429 /* Switch terminal for any messages produced by
5430 breakpoint_re_set. */
5431 target_terminal_ours_for_output ();
5432
5433 frame = get_current_frame ();
5434 gdbarch = get_frame_arch (frame);
5435
5436 jit_event_handler (gdbarch);
5437
5438 target_terminal_inferior ();
5439}
5440
5441/* Prepare WHAT final decision for infrun. */
5442
5443/* Decide what infrun needs to do with this bpstat. */
5444
c906108c 5445struct bpstat_what
0e30163f 5446bpstat_what (bpstat bs_head)
c906108c 5447{
c906108c 5448 struct bpstat_what retval;
628fe4e4 5449 int jit_event = 0;
0e30163f 5450 bpstat bs;
c906108c 5451
628fe4e4 5452 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5453 retval.call_dummy = STOP_NONE;
186c406b 5454 retval.is_longjmp = 0;
628fe4e4 5455
0e30163f 5456 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5457 {
628fe4e4
JK
5458 /* Extract this BS's action. After processing each BS, we check
5459 if its action overrides all we've seem so far. */
5460 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5461 enum bptype bptype;
5462
c906108c 5463 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5464 {
5465 /* I suspect this can happen if it was a momentary
5466 breakpoint which has since been deleted. */
5467 bptype = bp_none;
5468 }
20874c92 5469 else
f431efe5 5470 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5471
5472 switch (bptype)
c906108c
SS
5473 {
5474 case bp_none:
628fe4e4 5475 break;
c906108c
SS
5476 case bp_breakpoint:
5477 case bp_hardware_breakpoint:
5478 case bp_until:
5479 case bp_finish:
a9b3a50f 5480 case bp_shlib_event:
c906108c
SS
5481 if (bs->stop)
5482 {
5483 if (bs->print)
628fe4e4 5484 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5485 else
628fe4e4 5486 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5487 }
5488 else
628fe4e4 5489 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5490 break;
5491 case bp_watchpoint:
5492 case bp_hardware_watchpoint:
5493 case bp_read_watchpoint:
5494 case bp_access_watchpoint:
5495 if (bs->stop)
5496 {
5497 if (bs->print)
628fe4e4 5498 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5499 else
628fe4e4 5500 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5501 }
5502 else
628fe4e4
JK
5503 {
5504 /* There was a watchpoint, but we're not stopping.
5505 This requires no further action. */
5506 }
c906108c
SS
5507 break;
5508 case bp_longjmp:
e2e4d78b 5509 case bp_longjmp_call_dummy:
186c406b 5510 case bp_exception:
628fe4e4 5511 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5512 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5513 break;
5514 case bp_longjmp_resume:
186c406b 5515 case bp_exception_resume:
628fe4e4 5516 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5517 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5518 break;
5519 case bp_step_resume:
5520 if (bs->stop)
628fe4e4
JK
5521 this_action = BPSTAT_WHAT_STEP_RESUME;
5522 else
c906108c 5523 {
628fe4e4
JK
5524 /* It is for the wrong frame. */
5525 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5526 }
c906108c 5527 break;
2c03e5be
PA
5528 case bp_hp_step_resume:
5529 if (bs->stop)
5530 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5531 else
5532 {
5533 /* It is for the wrong frame. */
5534 this_action = BPSTAT_WHAT_SINGLE;
5535 }
5536 break;
c906108c 5537 case bp_watchpoint_scope:
c4093a6a 5538 case bp_thread_event:
1900040c 5539 case bp_overlay_event:
0fd8e87f 5540 case bp_longjmp_master:
aa7d318d 5541 case bp_std_terminate_master:
186c406b 5542 case bp_exception_master:
628fe4e4 5543 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5544 break;
ce78b96d 5545 case bp_catchpoint:
c5aa993b
JM
5546 if (bs->stop)
5547 {
5548 if (bs->print)
628fe4e4 5549 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5550 else
628fe4e4 5551 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5552 }
5553 else
628fe4e4
JK
5554 {
5555 /* There was a catchpoint, but we're not stopping.
5556 This requires no further action. */
5557 }
5558 break;
628fe4e4
JK
5559 case bp_jit_event:
5560 jit_event = 1;
5561 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5562 break;
c906108c 5563 case bp_call_dummy:
53a5351d
JM
5564 /* Make sure the action is stop (silent or noisy),
5565 so infrun.c pops the dummy frame. */
aa7d318d 5566 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5567 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5568 break;
5569 case bp_std_terminate:
5570 /* Make sure the action is stop (silent or noisy),
5571 so infrun.c pops the dummy frame. */
aa7d318d 5572 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5573 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5574 break;
1042e4c0 5575 case bp_tracepoint:
7a697b8d 5576 case bp_fast_tracepoint:
0fb4aa4b 5577 case bp_static_tracepoint:
1042e4c0
SS
5578 /* Tracepoint hits should not be reported back to GDB, and
5579 if one got through somehow, it should have been filtered
5580 out already. */
5581 internal_error (__FILE__, __LINE__,
7a697b8d 5582 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5583 break;
5584 case bp_gnu_ifunc_resolver:
5585 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5586 this_action = BPSTAT_WHAT_SINGLE;
5587 break;
5588 case bp_gnu_ifunc_resolver_return:
5589 /* The breakpoint will be removed, execution will restart from the
5590 PC of the former breakpoint. */
5591 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5592 break;
e7e0cddf
SS
5593
5594 case bp_dprintf:
a11cfd87
HZ
5595 if (bs->stop)
5596 this_action = BPSTAT_WHAT_STOP_SILENT;
5597 else
5598 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5599 break;
5600
628fe4e4
JK
5601 default:
5602 internal_error (__FILE__, __LINE__,
5603 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5604 }
628fe4e4
JK
5605
5606 retval.main_action = max (retval.main_action, this_action);
c906108c 5607 }
628fe4e4 5608
0e30163f
JK
5609 /* These operations may affect the bs->breakpoint_at state so they are
5610 delayed after MAIN_ACTION is decided above. */
5611
628fe4e4
JK
5612 if (jit_event)
5613 {
5614 if (debug_infrun)
5615 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5616
5617 handle_jit_event ();
5618 }
5619
0e30163f
JK
5620 for (bs = bs_head; bs != NULL; bs = bs->next)
5621 {
5622 struct breakpoint *b = bs->breakpoint_at;
5623
5624 if (b == NULL)
5625 continue;
5626 switch (b->type)
5627 {
5628 case bp_gnu_ifunc_resolver:
5629 gnu_ifunc_resolver_stop (b);
5630 break;
5631 case bp_gnu_ifunc_resolver_return:
5632 gnu_ifunc_resolver_return_stop (b);
5633 break;
5634 }
5635 }
5636
c906108c
SS
5637 return retval;
5638}
5639
5640/* Nonzero if we should step constantly (e.g. watchpoints on machines
5641 without hardware support). This isn't related to a specific bpstat,
5642 just to things like whether watchpoints are set. */
5643
c5aa993b 5644int
fba45db2 5645bpstat_should_step (void)
c906108c
SS
5646{
5647 struct breakpoint *b;
cc59ec59 5648
c906108c 5649 ALL_BREAKPOINTS (b)
717a8278 5650 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5651 return 1;
c906108c
SS
5652 return 0;
5653}
5654
67822962
PA
5655int
5656bpstat_causes_stop (bpstat bs)
5657{
5658 for (; bs != NULL; bs = bs->next)
5659 if (bs->stop)
5660 return 1;
5661
5662 return 0;
5663}
5664
c906108c 5665\f
c5aa993b 5666
170b53b2
UW
5667/* Compute a string of spaces suitable to indent the next line
5668 so it starts at the position corresponding to the table column
5669 named COL_NAME in the currently active table of UIOUT. */
5670
5671static char *
5672wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5673{
5674 static char wrap_indent[80];
5675 int i, total_width, width, align;
5676 char *text;
5677
5678 total_width = 0;
5679 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5680 {
5681 if (strcmp (text, col_name) == 0)
5682 {
5683 gdb_assert (total_width < sizeof wrap_indent);
5684 memset (wrap_indent, ' ', total_width);
5685 wrap_indent[total_width] = 0;
5686
5687 return wrap_indent;
5688 }
5689
5690 total_width += width + 1;
5691 }
5692
5693 return NULL;
5694}
5695
b775012e
LM
5696/* Determine if the locations of this breakpoint will have their conditions
5697 evaluated by the target, host or a mix of both. Returns the following:
5698
5699 "host": Host evals condition.
5700 "host or target": Host or Target evals condition.
5701 "target": Target evals condition.
5702*/
5703
5704static const char *
5705bp_condition_evaluator (struct breakpoint *b)
5706{
5707 struct bp_location *bl;
5708 char host_evals = 0;
5709 char target_evals = 0;
5710
5711 if (!b)
5712 return NULL;
5713
5714 if (!is_breakpoint (b))
5715 return NULL;
5716
5717 if (gdb_evaluates_breakpoint_condition_p ()
5718 || !target_supports_evaluation_of_breakpoint_conditions ())
5719 return condition_evaluation_host;
5720
5721 for (bl = b->loc; bl; bl = bl->next)
5722 {
5723 if (bl->cond_bytecode)
5724 target_evals++;
5725 else
5726 host_evals++;
5727 }
5728
5729 if (host_evals && target_evals)
5730 return condition_evaluation_both;
5731 else if (target_evals)
5732 return condition_evaluation_target;
5733 else
5734 return condition_evaluation_host;
5735}
5736
5737/* Determine the breakpoint location's condition evaluator. This is
5738 similar to bp_condition_evaluator, but for locations. */
5739
5740static const char *
5741bp_location_condition_evaluator (struct bp_location *bl)
5742{
5743 if (bl && !is_breakpoint (bl->owner))
5744 return NULL;
5745
5746 if (gdb_evaluates_breakpoint_condition_p ()
5747 || !target_supports_evaluation_of_breakpoint_conditions ())
5748 return condition_evaluation_host;
5749
5750 if (bl && bl->cond_bytecode)
5751 return condition_evaluation_target;
5752 else
5753 return condition_evaluation_host;
5754}
5755
859825b8
JK
5756/* Print the LOC location out of the list of B->LOC locations. */
5757
170b53b2
UW
5758static void
5759print_breakpoint_location (struct breakpoint *b,
5760 struct bp_location *loc)
0d381245 5761{
79a45e25 5762 struct ui_out *uiout = current_uiout;
6c95b8df
PA
5763 struct cleanup *old_chain = save_current_program_space ();
5764
859825b8
JK
5765 if (loc != NULL && loc->shlib_disabled)
5766 loc = NULL;
5767
6c95b8df
PA
5768 if (loc != NULL)
5769 set_current_program_space (loc->pspace);
5770
56435ebe
TT
5771 if (b->display_canonical)
5772 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 5773 else if (loc && loc->symtab)
0d381245
VP
5774 {
5775 struct symbol *sym
5776 = find_pc_sect_function (loc->address, loc->section);
5777 if (sym)
5778 {
5779 ui_out_text (uiout, "in ");
5780 ui_out_field_string (uiout, "func",
5781 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
5782 ui_out_text (uiout, " ");
5783 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5784 ui_out_text (uiout, "at ");
0d381245 5785 }
05cba821
JK
5786 ui_out_field_string (uiout, "file",
5787 symtab_to_filename_for_display (loc->symtab));
0d381245 5788 ui_out_text (uiout, ":");
05cba821 5789
0d381245 5790 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
5791 ui_out_field_string (uiout, "fullname",
5792 symtab_to_fullname (loc->symtab));
0d381245 5793
f8eba3c6 5794 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 5795 }
859825b8 5796 else if (loc)
0d381245 5797 {
f99d8bf4
PA
5798 struct ui_file *stb = mem_fileopen ();
5799 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 5800
f99d8bf4 5801 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 5802 demangle, "");
0d381245 5803 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
5804
5805 do_cleanups (stb_chain);
0d381245 5806 }
859825b8
JK
5807 else
5808 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 5809
b775012e
LM
5810 if (loc && is_breakpoint (b)
5811 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5812 && bp_condition_evaluator (b) == condition_evaluation_both)
5813 {
5814 ui_out_text (uiout, " (");
5815 ui_out_field_string (uiout, "evaluated-by",
5816 bp_location_condition_evaluator (loc));
5817 ui_out_text (uiout, ")");
5818 }
5819
6c95b8df 5820 do_cleanups (old_chain);
0d381245
VP
5821}
5822
269b11a2
PA
5823static const char *
5824bptype_string (enum bptype type)
c906108c 5825{
c4093a6a
JM
5826 struct ep_type_description
5827 {
5828 enum bptype type;
5829 char *description;
5830 };
5831 static struct ep_type_description bptypes[] =
c906108c 5832 {
c5aa993b
JM
5833 {bp_none, "?deleted?"},
5834 {bp_breakpoint, "breakpoint"},
c906108c 5835 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
5836 {bp_until, "until"},
5837 {bp_finish, "finish"},
5838 {bp_watchpoint, "watchpoint"},
c906108c 5839 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5840 {bp_read_watchpoint, "read watchpoint"},
5841 {bp_access_watchpoint, "acc watchpoint"},
5842 {bp_longjmp, "longjmp"},
5843 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5844 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5845 {bp_exception, "exception"},
5846 {bp_exception_resume, "exception resume"},
c5aa993b 5847 {bp_step_resume, "step resume"},
2c03e5be 5848 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5849 {bp_watchpoint_scope, "watchpoint scope"},
5850 {bp_call_dummy, "call dummy"},
aa7d318d 5851 {bp_std_terminate, "std::terminate"},
c5aa993b 5852 {bp_shlib_event, "shlib events"},
c4093a6a 5853 {bp_thread_event, "thread events"},
1900040c 5854 {bp_overlay_event, "overlay events"},
0fd8e87f 5855 {bp_longjmp_master, "longjmp master"},
aa7d318d 5856 {bp_std_terminate_master, "std::terminate master"},
186c406b 5857 {bp_exception_master, "exception master"},
ce78b96d 5858 {bp_catchpoint, "catchpoint"},
1042e4c0 5859 {bp_tracepoint, "tracepoint"},
7a697b8d 5860 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5861 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5862 {bp_dprintf, "dprintf"},
4efc6507 5863 {bp_jit_event, "jit events"},
0e30163f
JK
5864 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5865 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5866 };
269b11a2
PA
5867
5868 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5869 || ((int) type != bptypes[(int) type].type))
5870 internal_error (__FILE__, __LINE__,
5871 _("bptypes table does not describe type #%d."),
5872 (int) type);
5873
5874 return bptypes[(int) type].description;
5875}
5876
998580f1
MK
5877/* For MI, output a field named 'thread-groups' with a list as the value.
5878 For CLI, prefix the list with the string 'inf'. */
5879
5880static void
5881output_thread_groups (struct ui_out *uiout,
5882 const char *field_name,
5883 VEC(int) *inf_num,
5884 int mi_only)
5885{
752eb8b4 5886 struct cleanup *back_to;
998580f1
MK
5887 int is_mi = ui_out_is_mi_like_p (uiout);
5888 int inf;
5889 int i;
5890
5891 /* For backward compatibility, don't display inferiors in CLI unless
5892 there are several. Always display them for MI. */
5893 if (!is_mi && mi_only)
5894 return;
5895
752eb8b4
TT
5896 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
5897
998580f1
MK
5898 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
5899 {
5900 if (is_mi)
5901 {
5902 char mi_group[10];
5903
5904 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
5905 ui_out_field_string (uiout, NULL, mi_group);
5906 }
5907 else
5908 {
5909 if (i == 0)
5910 ui_out_text (uiout, " inf ");
5911 else
5912 ui_out_text (uiout, ", ");
5913
5914 ui_out_text (uiout, plongest (inf));
5915 }
5916 }
5917
5918 do_cleanups (back_to);
5919}
5920
269b11a2
PA
5921/* Print B to gdb_stdout. */
5922
5923static void
5924print_one_breakpoint_location (struct breakpoint *b,
5925 struct bp_location *loc,
5926 int loc_number,
5927 struct bp_location **last_loc,
269b11a2
PA
5928 int allflag)
5929{
5930 struct command_line *l;
c2c6d25f 5931 static char bpenables[] = "nynny";
c906108c 5932
79a45e25 5933 struct ui_out *uiout = current_uiout;
0d381245
VP
5934 int header_of_multiple = 0;
5935 int part_of_multiple = (loc != NULL);
79a45b7d
TT
5936 struct value_print_options opts;
5937
5938 get_user_print_options (&opts);
0d381245
VP
5939
5940 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
5941 /* See comment in print_one_breakpoint concerning treatment of
5942 breakpoints with single disabled location. */
0d381245
VP
5943 if (loc == NULL
5944 && (b->loc != NULL
5945 && (b->loc->next != NULL || !b->loc->enabled)))
5946 header_of_multiple = 1;
5947 if (loc == NULL)
5948 loc = b->loc;
5949
c4093a6a
JM
5950 annotate_record ();
5951
5952 /* 1 */
5953 annotate_field (0);
0d381245
VP
5954 if (part_of_multiple)
5955 {
5956 char *formatted;
0c6773c1 5957 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
5958 ui_out_field_string (uiout, "number", formatted);
5959 xfree (formatted);
5960 }
5961 else
5962 {
5963 ui_out_field_int (uiout, "number", b->number);
5964 }
c4093a6a
JM
5965
5966 /* 2 */
5967 annotate_field (1);
0d381245
VP
5968 if (part_of_multiple)
5969 ui_out_field_skip (uiout, "type");
269b11a2
PA
5970 else
5971 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
5972
5973 /* 3 */
5974 annotate_field (2);
0d381245
VP
5975 if (part_of_multiple)
5976 ui_out_field_skip (uiout, "disp");
5977 else
2cec12e5 5978 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 5979
c4093a6a
JM
5980
5981 /* 4 */
5982 annotate_field (3);
0d381245 5983 if (part_of_multiple)
54e52265 5984 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 5985 else
4a64f543
MS
5986 ui_out_field_fmt (uiout, "enabled", "%c",
5987 bpenables[(int) b->enable_state]);
54e52265 5988 ui_out_spaces (uiout, 2);
0d381245 5989
c4093a6a
JM
5990
5991 /* 5 and 6 */
3086aeae 5992 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 5993 {
4a64f543
MS
5994 /* Although the print_one can possibly print all locations,
5995 calling it here is not likely to get any nice result. So,
5996 make sure there's just one location. */
0d381245 5997 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 5998 b->ops->print_one (b, last_loc);
0d381245 5999 }
3086aeae
DJ
6000 else
6001 switch (b->type)
6002 {
6003 case bp_none:
6004 internal_error (__FILE__, __LINE__,
e2e0b3e5 6005 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6006 break;
c906108c 6007
3086aeae
DJ
6008 case bp_watchpoint:
6009 case bp_hardware_watchpoint:
6010 case bp_read_watchpoint:
6011 case bp_access_watchpoint:
3a5c3e22
PA
6012 {
6013 struct watchpoint *w = (struct watchpoint *) b;
6014
6015 /* Field 4, the address, is omitted (which makes the columns
6016 not line up too nicely with the headers, but the effect
6017 is relatively readable). */
6018 if (opts.addressprint)
6019 ui_out_field_skip (uiout, "addr");
6020 annotate_field (5);
6021 ui_out_field_string (uiout, "what", w->exp_string);
6022 }
3086aeae
DJ
6023 break;
6024
3086aeae
DJ
6025 case bp_breakpoint:
6026 case bp_hardware_breakpoint:
6027 case bp_until:
6028 case bp_finish:
6029 case bp_longjmp:
6030 case bp_longjmp_resume:
e2e4d78b 6031 case bp_longjmp_call_dummy:
186c406b
TT
6032 case bp_exception:
6033 case bp_exception_resume:
3086aeae 6034 case bp_step_resume:
2c03e5be 6035 case bp_hp_step_resume:
3086aeae
DJ
6036 case bp_watchpoint_scope:
6037 case bp_call_dummy:
aa7d318d 6038 case bp_std_terminate:
3086aeae
DJ
6039 case bp_shlib_event:
6040 case bp_thread_event:
6041 case bp_overlay_event:
0fd8e87f 6042 case bp_longjmp_master:
aa7d318d 6043 case bp_std_terminate_master:
186c406b 6044 case bp_exception_master:
1042e4c0 6045 case bp_tracepoint:
7a697b8d 6046 case bp_fast_tracepoint:
0fb4aa4b 6047 case bp_static_tracepoint:
e7e0cddf 6048 case bp_dprintf:
4efc6507 6049 case bp_jit_event:
0e30163f
JK
6050 case bp_gnu_ifunc_resolver:
6051 case bp_gnu_ifunc_resolver_return:
79a45b7d 6052 if (opts.addressprint)
3086aeae
DJ
6053 {
6054 annotate_field (4);
54e52265 6055 if (header_of_multiple)
0d381245 6056 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6057 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6058 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6059 else
5af949e3
UW
6060 ui_out_field_core_addr (uiout, "addr",
6061 loc->gdbarch, loc->address);
3086aeae
DJ
6062 }
6063 annotate_field (5);
0d381245 6064 if (!header_of_multiple)
170b53b2 6065 print_breakpoint_location (b, loc);
0d381245 6066 if (b->loc)
a6d9a66e 6067 *last_loc = b->loc;
3086aeae
DJ
6068 break;
6069 }
c906108c 6070
6c95b8df 6071
998580f1 6072 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6073 {
6074 struct inferior *inf;
998580f1
MK
6075 VEC(int) *inf_num = NULL;
6076 int mi_only = 1;
6c95b8df 6077
998580f1 6078 ALL_INFERIORS (inf)
6c95b8df
PA
6079 {
6080 if (inf->pspace == loc->pspace)
998580f1 6081 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6082 }
998580f1
MK
6083
6084 /* For backward compatibility, don't display inferiors in CLI unless
6085 there are several. Always display for MI. */
6086 if (allflag
6087 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6088 && (number_of_program_spaces () > 1
6089 || number_of_inferiors () > 1)
6090 /* LOC is for existing B, it cannot be in
6091 moribund_locations and thus having NULL OWNER. */
6092 && loc->owner->type != bp_catchpoint))
6093 mi_only = 0;
6094 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6095 VEC_free (int, inf_num);
6c95b8df
PA
6096 }
6097
4a306c9a 6098 if (!part_of_multiple)
c4093a6a 6099 {
4a306c9a
JB
6100 if (b->thread != -1)
6101 {
6102 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6103 "stop only in" line a little further down. */
4a306c9a
JB
6104 ui_out_text (uiout, " thread ");
6105 ui_out_field_int (uiout, "thread", b->thread);
6106 }
6107 else if (b->task != 0)
6108 {
6109 ui_out_text (uiout, " task ");
6110 ui_out_field_int (uiout, "task", b->task);
6111 }
c4093a6a 6112 }
f1310107 6113
8b93c638 6114 ui_out_text (uiout, "\n");
f1310107 6115
348d480f 6116 if (!part_of_multiple)
f1310107
TJB
6117 b->ops->print_one_detail (b, uiout);
6118
0d381245 6119 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6120 {
6121 annotate_field (6);
8b93c638 6122 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6123 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6124 the frame ID. */
5af949e3
UW
6125 ui_out_field_core_addr (uiout, "frame",
6126 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6127 ui_out_text (uiout, "\n");
c4093a6a
JM
6128 }
6129
28010a5d 6130 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6131 {
6132 annotate_field (7);
d77f58be 6133 if (is_tracepoint (b))
1042e4c0
SS
6134 ui_out_text (uiout, "\ttrace only if ");
6135 else
6136 ui_out_text (uiout, "\tstop only if ");
0101ce28 6137 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6138
6139 /* Print whether the target is doing the breakpoint's condition
6140 evaluation. If GDB is doing the evaluation, don't print anything. */
6141 if (is_breakpoint (b)
6142 && breakpoint_condition_evaluation_mode ()
6143 == condition_evaluation_target)
6144 {
6145 ui_out_text (uiout, " (");
6146 ui_out_field_string (uiout, "evaluated-by",
6147 bp_condition_evaluator (b));
6148 ui_out_text (uiout, " evals)");
6149 }
0101ce28
JJ
6150 ui_out_text (uiout, "\n");
6151 }
6152
0d381245 6153 if (!part_of_multiple && b->thread != -1)
c4093a6a 6154 {
4a64f543 6155 /* FIXME should make an annotation for this. */
8b93c638
JM
6156 ui_out_text (uiout, "\tstop only in thread ");
6157 ui_out_field_int (uiout, "thread", b->thread);
6158 ui_out_text (uiout, "\n");
c4093a6a
JM
6159 }
6160
556ec64d
YQ
6161 if (!part_of_multiple)
6162 {
6163 if (b->hit_count)
31f56a27
YQ
6164 {
6165 /* FIXME should make an annotation for this. */
6166 if (is_catchpoint (b))
6167 ui_out_text (uiout, "\tcatchpoint");
6168 else if (is_tracepoint (b))
6169 ui_out_text (uiout, "\ttracepoint");
6170 else
6171 ui_out_text (uiout, "\tbreakpoint");
6172 ui_out_text (uiout, " already hit ");
6173 ui_out_field_int (uiout, "times", b->hit_count);
6174 if (b->hit_count == 1)
6175 ui_out_text (uiout, " time\n");
6176 else
6177 ui_out_text (uiout, " times\n");
6178 }
556ec64d
YQ
6179 else
6180 {
31f56a27
YQ
6181 /* Output the count also if it is zero, but only if this is mi. */
6182 if (ui_out_is_mi_like_p (uiout))
6183 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6184 }
6185 }
8b93c638 6186
0d381245 6187 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6188 {
6189 annotate_field (8);
8b93c638
JM
6190 ui_out_text (uiout, "\tignore next ");
6191 ui_out_field_int (uiout, "ignore", b->ignore_count);
6192 ui_out_text (uiout, " hits\n");
c4093a6a 6193 }
059fb39f 6194
816338b5
SS
6195 /* Note that an enable count of 1 corresponds to "enable once"
6196 behavior, which is reported by the combination of enablement and
6197 disposition, so we don't need to mention it here. */
6198 if (!part_of_multiple && b->enable_count > 1)
6199 {
6200 annotate_field (8);
6201 ui_out_text (uiout, "\tdisable after ");
6202 /* Tweak the wording to clarify that ignore and enable counts
6203 are distinct, and have additive effect. */
6204 if (b->ignore_count)
6205 ui_out_text (uiout, "additional ");
6206 else
6207 ui_out_text (uiout, "next ");
6208 ui_out_field_int (uiout, "enable", b->enable_count);
6209 ui_out_text (uiout, " hits\n");
6210 }
6211
f196051f
SS
6212 if (!part_of_multiple && is_tracepoint (b))
6213 {
6214 struct tracepoint *tp = (struct tracepoint *) b;
6215
6216 if (tp->traceframe_usage)
6217 {
6218 ui_out_text (uiout, "\ttrace buffer usage ");
6219 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6220 ui_out_text (uiout, " bytes\n");
6221 }
6222 }
d3ce09f5 6223
9add0f1b 6224 l = b->commands ? b->commands->commands : NULL;
059fb39f 6225 if (!part_of_multiple && l)
c4093a6a 6226 {
3b31d625
EZ
6227 struct cleanup *script_chain;
6228
c4093a6a 6229 annotate_field (9);
3b31d625 6230 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6231 print_command_lines (uiout, l, 4);
3b31d625 6232 do_cleanups (script_chain);
c4093a6a 6233 }
d24317b4 6234
d9b3f62e 6235 if (is_tracepoint (b))
1042e4c0 6236 {
d9b3f62e
PA
6237 struct tracepoint *t = (struct tracepoint *) b;
6238
6239 if (!part_of_multiple && t->pass_count)
6240 {
6241 annotate_field (10);
6242 ui_out_text (uiout, "\tpass count ");
6243 ui_out_field_int (uiout, "pass", t->pass_count);
6244 ui_out_text (uiout, " \n");
6245 }
f2a8bc8a
YQ
6246
6247 /* Don't display it when tracepoint or tracepoint location is
6248 pending. */
6249 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6250 {
6251 annotate_field (11);
6252
6253 if (ui_out_is_mi_like_p (uiout))
6254 ui_out_field_string (uiout, "installed",
6255 loc->inserted ? "y" : "n");
6256 else
6257 {
6258 if (loc->inserted)
6259 ui_out_text (uiout, "\t");
6260 else
6261 ui_out_text (uiout, "\tnot ");
6262 ui_out_text (uiout, "installed on target\n");
6263 }
6264 }
1042e4c0
SS
6265 }
6266
d24317b4
VP
6267 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6268 {
3a5c3e22
PA
6269 if (is_watchpoint (b))
6270 {
6271 struct watchpoint *w = (struct watchpoint *) b;
6272
6273 ui_out_field_string (uiout, "original-location", w->exp_string);
6274 }
6275 else if (b->addr_string)
d24317b4 6276 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6277 }
c4093a6a 6278}
c5aa993b 6279
0d381245
VP
6280static void
6281print_one_breakpoint (struct breakpoint *b,
4a64f543 6282 struct bp_location **last_loc,
6c95b8df 6283 int allflag)
0d381245 6284{
8d3788bd 6285 struct cleanup *bkpt_chain;
79a45e25 6286 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6287
6288 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6289
12c5a436 6290 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6291 do_cleanups (bkpt_chain);
0d381245
VP
6292
6293 /* If this breakpoint has custom print function,
6294 it's already printed. Otherwise, print individual
6295 locations, if any. */
6296 if (b->ops == NULL || b->ops->print_one == NULL)
6297 {
4a64f543
MS
6298 /* If breakpoint has a single location that is disabled, we
6299 print it as if it had several locations, since otherwise it's
6300 hard to represent "breakpoint enabled, location disabled"
6301 situation.
6302
6303 Note that while hardware watchpoints have several locations
a3be7890 6304 internally, that's not a property exposed to user. */
0d381245 6305 if (b->loc
a5606eee 6306 && !is_hardware_watchpoint (b)
8d3788bd 6307 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6308 {
6309 struct bp_location *loc;
6310 int n = 1;
8d3788bd 6311
0d381245 6312 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6313 {
6314 struct cleanup *inner2 =
6315 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6316 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6317 do_cleanups (inner2);
6318 }
0d381245
VP
6319 }
6320 }
6321}
6322
a6d9a66e
UW
6323static int
6324breakpoint_address_bits (struct breakpoint *b)
6325{
6326 int print_address_bits = 0;
6327 struct bp_location *loc;
6328
6329 for (loc = b->loc; loc; loc = loc->next)
6330 {
c7437ca6
PA
6331 int addr_bit;
6332
6333 /* Software watchpoints that aren't watching memory don't have
6334 an address to print. */
6335 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6336 continue;
6337
6338 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6339 if (addr_bit > print_address_bits)
6340 print_address_bits = addr_bit;
6341 }
6342
6343 return print_address_bits;
6344}
0d381245 6345
c4093a6a
JM
6346struct captured_breakpoint_query_args
6347 {
6348 int bnum;
6349 };
c5aa993b 6350
c4093a6a 6351static int
2b65245e 6352do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6353{
6354 struct captured_breakpoint_query_args *args = data;
52f0bd74 6355 struct breakpoint *b;
a6d9a66e 6356 struct bp_location *dummy_loc = NULL;
cc59ec59 6357
c4093a6a
JM
6358 ALL_BREAKPOINTS (b)
6359 {
6360 if (args->bnum == b->number)
c5aa993b 6361 {
12c5a436 6362 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6363 return GDB_RC_OK;
c5aa993b 6364 }
c4093a6a
JM
6365 }
6366 return GDB_RC_NONE;
6367}
c5aa993b 6368
c4093a6a 6369enum gdb_rc
4a64f543
MS
6370gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6371 char **error_message)
c4093a6a
JM
6372{
6373 struct captured_breakpoint_query_args args;
cc59ec59 6374
c4093a6a
JM
6375 args.bnum = bnum;
6376 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6377 an error. */
b0b13bb4
DJ
6378 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6379 error_message, RETURN_MASK_ALL) < 0)
6380 return GDB_RC_FAIL;
6381 else
6382 return GDB_RC_OK;
c4093a6a 6383}
c5aa993b 6384
09d682a4
TT
6385/* Return true if this breakpoint was set by the user, false if it is
6386 internal or momentary. */
6387
6388int
6389user_breakpoint_p (struct breakpoint *b)
6390{
46c6471b 6391 return b->number > 0;
09d682a4
TT
6392}
6393
7f3b0473 6394/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6395 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6396 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6397 FILTER is non-NULL, call it on each breakpoint and only include the
6398 ones for which it returns non-zero. Return the total number of
6399 breakpoints listed. */
c906108c 6400
d77f58be 6401static int
e5a67952 6402breakpoint_1 (char *args, int allflag,
4a64f543 6403 int (*filter) (const struct breakpoint *))
c4093a6a 6404{
52f0bd74 6405 struct breakpoint *b;
a6d9a66e 6406 struct bp_location *last_loc = NULL;
7f3b0473 6407 int nr_printable_breakpoints;
3b31d625 6408 struct cleanup *bkpttbl_chain;
79a45b7d 6409 struct value_print_options opts;
a6d9a66e 6410 int print_address_bits = 0;
269b11a2 6411 int print_type_col_width = 14;
79a45e25 6412 struct ui_out *uiout = current_uiout;
269b11a2 6413
79a45b7d
TT
6414 get_user_print_options (&opts);
6415
4a64f543
MS
6416 /* Compute the number of rows in the table, as well as the size
6417 required for address fields. */
7f3b0473
AC
6418 nr_printable_breakpoints = 0;
6419 ALL_BREAKPOINTS (b)
e5a67952
MS
6420 {
6421 /* If we have a filter, only list the breakpoints it accepts. */
6422 if (filter && !filter (b))
6423 continue;
6424
6425 /* If we have an "args" string, it is a list of breakpoints to
6426 accept. Skip the others. */
6427 if (args != NULL && *args != '\0')
6428 {
6429 if (allflag && parse_and_eval_long (args) != b->number)
6430 continue;
6431 if (!allflag && !number_is_in_list (args, b->number))
6432 continue;
6433 }
269b11a2 6434
e5a67952
MS
6435 if (allflag || user_breakpoint_p (b))
6436 {
6437 int addr_bit, type_len;
a6d9a66e 6438
e5a67952
MS
6439 addr_bit = breakpoint_address_bits (b);
6440 if (addr_bit > print_address_bits)
6441 print_address_bits = addr_bit;
269b11a2 6442
e5a67952
MS
6443 type_len = strlen (bptype_string (b->type));
6444 if (type_len > print_type_col_width)
6445 print_type_col_width = type_len;
6446
6447 nr_printable_breakpoints++;
6448 }
6449 }
7f3b0473 6450
79a45b7d 6451 if (opts.addressprint)
3b31d625 6452 bkpttbl_chain
3e43a32a
MS
6453 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6454 nr_printable_breakpoints,
3b31d625 6455 "BreakpointTable");
8b93c638 6456 else
3b31d625 6457 bkpttbl_chain
3e43a32a
MS
6458 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6459 nr_printable_breakpoints,
3b31d625 6460 "BreakpointTable");
8b93c638 6461
7f3b0473 6462 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6463 annotate_breakpoints_headers ();
6464 if (nr_printable_breakpoints > 0)
6465 annotate_field (0);
4a64f543 6466 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6467 if (nr_printable_breakpoints > 0)
6468 annotate_field (1);
269b11a2 6469 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6470 "type", "Type"); /* 2 */
d7faa9e7
AC
6471 if (nr_printable_breakpoints > 0)
6472 annotate_field (2);
4a64f543 6473 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6474 if (nr_printable_breakpoints > 0)
6475 annotate_field (3);
54e52265 6476 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6477 if (opts.addressprint)
e5a67952
MS
6478 {
6479 if (nr_printable_breakpoints > 0)
6480 annotate_field (4);
6481 if (print_address_bits <= 32)
6482 ui_out_table_header (uiout, 10, ui_left,
6483 "addr", "Address"); /* 5 */
6484 else
6485 ui_out_table_header (uiout, 18, ui_left,
6486 "addr", "Address"); /* 5 */
6487 }
d7faa9e7
AC
6488 if (nr_printable_breakpoints > 0)
6489 annotate_field (5);
6490 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6491 ui_out_table_body (uiout);
6492 if (nr_printable_breakpoints > 0)
6493 annotate_breakpoints_table ();
7f3b0473 6494
c4093a6a 6495 ALL_BREAKPOINTS (b)
e5a67952
MS
6496 {
6497 QUIT;
6498 /* If we have a filter, only list the breakpoints it accepts. */
6499 if (filter && !filter (b))
6500 continue;
6501
6502 /* If we have an "args" string, it is a list of breakpoints to
6503 accept. Skip the others. */
6504
6505 if (args != NULL && *args != '\0')
6506 {
6507 if (allflag) /* maintenance info breakpoint */
6508 {
6509 if (parse_and_eval_long (args) != b->number)
6510 continue;
6511 }
6512 else /* all others */
6513 {
6514 if (!number_is_in_list (args, b->number))
6515 continue;
6516 }
6517 }
6518 /* We only print out user settable breakpoints unless the
6519 allflag is set. */
6520 if (allflag || user_breakpoint_p (b))
12c5a436 6521 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6522 }
6523
3b31d625 6524 do_cleanups (bkpttbl_chain);
698384cd 6525
7f3b0473 6526 if (nr_printable_breakpoints == 0)
c906108c 6527 {
4a64f543
MS
6528 /* If there's a filter, let the caller decide how to report
6529 empty list. */
d77f58be
SS
6530 if (!filter)
6531 {
e5a67952 6532 if (args == NULL || *args == '\0')
d77f58be
SS
6533 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6534 else
4a64f543 6535 ui_out_message (uiout, 0,
e5a67952
MS
6536 "No breakpoint or watchpoint matching '%s'.\n",
6537 args);
d77f58be 6538 }
c906108c
SS
6539 }
6540 else
c4093a6a 6541 {
a6d9a66e
UW
6542 if (last_loc && !server_command)
6543 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6544 }
c906108c 6545
4a64f543 6546 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6547 there have been breakpoints? */
c906108c 6548 annotate_breakpoints_table_end ();
d77f58be
SS
6549
6550 return nr_printable_breakpoints;
c906108c
SS
6551}
6552
ad443146
SS
6553/* Display the value of default-collect in a way that is generally
6554 compatible with the breakpoint list. */
6555
6556static void
6557default_collect_info (void)
6558{
79a45e25
PA
6559 struct ui_out *uiout = current_uiout;
6560
ad443146
SS
6561 /* If it has no value (which is frequently the case), say nothing; a
6562 message like "No default-collect." gets in user's face when it's
6563 not wanted. */
6564 if (!*default_collect)
6565 return;
6566
6567 /* The following phrase lines up nicely with per-tracepoint collect
6568 actions. */
6569 ui_out_text (uiout, "default collect ");
6570 ui_out_field_string (uiout, "default-collect", default_collect);
6571 ui_out_text (uiout, " \n");
6572}
6573
c906108c 6574static void
e5a67952 6575breakpoints_info (char *args, int from_tty)
c906108c 6576{
e5a67952 6577 breakpoint_1 (args, 0, NULL);
ad443146
SS
6578
6579 default_collect_info ();
d77f58be
SS
6580}
6581
6582static void
e5a67952 6583watchpoints_info (char *args, int from_tty)
d77f58be 6584{
e5a67952 6585 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6586 struct ui_out *uiout = current_uiout;
d77f58be
SS
6587
6588 if (num_printed == 0)
6589 {
e5a67952 6590 if (args == NULL || *args == '\0')
d77f58be
SS
6591 ui_out_message (uiout, 0, "No watchpoints.\n");
6592 else
e5a67952 6593 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6594 }
c906108c
SS
6595}
6596
7a292a7a 6597static void
e5a67952 6598maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6599{
e5a67952 6600 breakpoint_1 (args, 1, NULL);
ad443146
SS
6601
6602 default_collect_info ();
c906108c
SS
6603}
6604
0d381245 6605static int
714835d5 6606breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6607 struct program_space *pspace,
714835d5 6608 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6609{
6610 struct bp_location *bl = b->loc;
cc59ec59 6611
0d381245
VP
6612 for (; bl; bl = bl->next)
6613 {
6c95b8df
PA
6614 if (bl->pspace == pspace
6615 && bl->address == pc
0d381245
VP
6616 && (!overlay_debugging || bl->section == section))
6617 return 1;
6618 }
6619 return 0;
6620}
6621
672f9b60 6622/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6623 concerns with logical breakpoints, so we match program spaces, not
6624 address spaces. */
c906108c
SS
6625
6626static void
6c95b8df
PA
6627describe_other_breakpoints (struct gdbarch *gdbarch,
6628 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6629 struct obj_section *section, int thread)
c906108c 6630{
52f0bd74
AC
6631 int others = 0;
6632 struct breakpoint *b;
c906108c
SS
6633
6634 ALL_BREAKPOINTS (b)
672f9b60
KP
6635 others += (user_breakpoint_p (b)
6636 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6637 if (others > 0)
6638 {
a3f17187
AC
6639 if (others == 1)
6640 printf_filtered (_("Note: breakpoint "));
6641 else /* if (others == ???) */
6642 printf_filtered (_("Note: breakpoints "));
c906108c 6643 ALL_BREAKPOINTS (b)
672f9b60 6644 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6645 {
6646 others--;
6647 printf_filtered ("%d", b->number);
6648 if (b->thread == -1 && thread != -1)
6649 printf_filtered (" (all threads)");
6650 else if (b->thread != -1)
6651 printf_filtered (" (thread %d)", b->thread);
6652 printf_filtered ("%s%s ",
059fb39f 6653 ((b->enable_state == bp_disabled
f8eba3c6 6654 || b->enable_state == bp_call_disabled)
0d381245
VP
6655 ? " (disabled)"
6656 : b->enable_state == bp_permanent
6657 ? " (permanent)"
6658 : ""),
6659 (others > 1) ? ","
6660 : ((others == 1) ? " and" : ""));
6661 }
a3f17187 6662 printf_filtered (_("also set at pc "));
5af949e3 6663 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6664 printf_filtered (".\n");
6665 }
6666}
6667\f
c906108c 6668
e4f237da
KB
6669/* Return true iff it is meaningful to use the address member of
6670 BPT. For some breakpoint types, the address member is irrelevant
6671 and it makes no sense to attempt to compare it to other addresses
6672 (or use it for any other purpose either).
6673
4a64f543
MS
6674 More specifically, each of the following breakpoint types will
6675 always have a zero valued address and we don't want to mark
6676 breakpoints of any of these types to be a duplicate of an actual
6677 breakpoint at address zero:
e4f237da
KB
6678
6679 bp_watchpoint
2d134ed3
PA
6680 bp_catchpoint
6681
6682*/
e4f237da
KB
6683
6684static int
6685breakpoint_address_is_meaningful (struct breakpoint *bpt)
6686{
6687 enum bptype type = bpt->type;
6688
2d134ed3
PA
6689 return (type != bp_watchpoint && type != bp_catchpoint);
6690}
6691
6692/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6693 true if LOC1 and LOC2 represent the same watchpoint location. */
6694
6695static int
4a64f543
MS
6696watchpoint_locations_match (struct bp_location *loc1,
6697 struct bp_location *loc2)
2d134ed3 6698{
3a5c3e22
PA
6699 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6700 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6701
6702 /* Both of them must exist. */
6703 gdb_assert (w1 != NULL);
6704 gdb_assert (w2 != NULL);
2bdf28a0 6705
4a64f543
MS
6706 /* If the target can evaluate the condition expression in hardware,
6707 then we we need to insert both watchpoints even if they are at
6708 the same place. Otherwise the watchpoint will only trigger when
6709 the condition of whichever watchpoint was inserted evaluates to
6710 true, not giving a chance for GDB to check the condition of the
6711 other watchpoint. */
3a5c3e22 6712 if ((w1->cond_exp
4a64f543
MS
6713 && target_can_accel_watchpoint_condition (loc1->address,
6714 loc1->length,
0cf6dd15 6715 loc1->watchpoint_type,
3a5c3e22
PA
6716 w1->cond_exp))
6717 || (w2->cond_exp
4a64f543
MS
6718 && target_can_accel_watchpoint_condition (loc2->address,
6719 loc2->length,
0cf6dd15 6720 loc2->watchpoint_type,
3a5c3e22 6721 w2->cond_exp)))
0cf6dd15
TJB
6722 return 0;
6723
85d721b8
PA
6724 /* Note that this checks the owner's type, not the location's. In
6725 case the target does not support read watchpoints, but does
6726 support access watchpoints, we'll have bp_read_watchpoint
6727 watchpoints with hw_access locations. Those should be considered
6728 duplicates of hw_read locations. The hw_read locations will
6729 become hw_access locations later. */
2d134ed3
PA
6730 return (loc1->owner->type == loc2->owner->type
6731 && loc1->pspace->aspace == loc2->pspace->aspace
6732 && loc1->address == loc2->address
6733 && loc1->length == loc2->length);
e4f237da
KB
6734}
6735
6c95b8df
PA
6736/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6737 same breakpoint location. In most targets, this can only be true
6738 if ASPACE1 matches ASPACE2. On targets that have global
6739 breakpoints, the address space doesn't really matter. */
6740
6741static int
6742breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6743 struct address_space *aspace2, CORE_ADDR addr2)
6744{
f5656ead 6745 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6746 || aspace1 == aspace2)
6747 && addr1 == addr2);
6748}
6749
f1310107
TJB
6750/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6751 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6752 matches ASPACE2. On targets that have global breakpoints, the address
6753 space doesn't really matter. */
6754
6755static int
6756breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6757 int len1, struct address_space *aspace2,
6758 CORE_ADDR addr2)
6759{
f5656ead 6760 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6761 || aspace1 == aspace2)
6762 && addr2 >= addr1 && addr2 < addr1 + len1);
6763}
6764
6765/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6766 a ranged breakpoint. In most targets, a match happens only if ASPACE
6767 matches the breakpoint's address space. On targets that have global
6768 breakpoints, the address space doesn't really matter. */
6769
6770static int
6771breakpoint_location_address_match (struct bp_location *bl,
6772 struct address_space *aspace,
6773 CORE_ADDR addr)
6774{
6775 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6776 aspace, addr)
6777 || (bl->length
6778 && breakpoint_address_match_range (bl->pspace->aspace,
6779 bl->address, bl->length,
6780 aspace, addr)));
6781}
6782
1e4d1764
YQ
6783/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6784 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6785 true, otherwise returns false. */
6786
6787static int
6788tracepoint_locations_match (struct bp_location *loc1,
6789 struct bp_location *loc2)
6790{
6791 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6792 /* Since tracepoint locations are never duplicated with others', tracepoint
6793 locations at the same address of different tracepoints are regarded as
6794 different locations. */
6795 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6796 else
6797 return 0;
6798}
6799
2d134ed3
PA
6800/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6801 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6802 represent the same location. */
6803
6804static int
4a64f543
MS
6805breakpoint_locations_match (struct bp_location *loc1,
6806 struct bp_location *loc2)
2d134ed3 6807{
2bdf28a0
JK
6808 int hw_point1, hw_point2;
6809
6810 /* Both of them must not be in moribund_locations. */
6811 gdb_assert (loc1->owner != NULL);
6812 gdb_assert (loc2->owner != NULL);
6813
6814 hw_point1 = is_hardware_watchpoint (loc1->owner);
6815 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6816
6817 if (hw_point1 != hw_point2)
6818 return 0;
6819 else if (hw_point1)
6820 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6821 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6822 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6823 else
f1310107
TJB
6824 /* We compare bp_location.length in order to cover ranged breakpoints. */
6825 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6826 loc2->pspace->aspace, loc2->address)
6827 && loc1->length == loc2->length);
2d134ed3
PA
6828}
6829
76897487
KB
6830static void
6831breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6832 int bnum, int have_bnum)
6833{
f63fbe86
MS
6834 /* The longest string possibly returned by hex_string_custom
6835 is 50 chars. These must be at least that big for safety. */
6836 char astr1[64];
6837 char astr2[64];
76897487 6838
bb599908
PH
6839 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6840 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6841 if (have_bnum)
8a3fe4f8 6842 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6843 bnum, astr1, astr2);
6844 else
8a3fe4f8 6845 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6846}
6847
4a64f543
MS
6848/* Adjust a breakpoint's address to account for architectural
6849 constraints on breakpoint placement. Return the adjusted address.
6850 Note: Very few targets require this kind of adjustment. For most
6851 targets, this function is simply the identity function. */
76897487
KB
6852
6853static CORE_ADDR
a6d9a66e
UW
6854adjust_breakpoint_address (struct gdbarch *gdbarch,
6855 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6856{
a6d9a66e 6857 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
6858 {
6859 /* Very few targets need any kind of breakpoint adjustment. */
6860 return bpaddr;
6861 }
88f7da05
KB
6862 else if (bptype == bp_watchpoint
6863 || bptype == bp_hardware_watchpoint
6864 || bptype == bp_read_watchpoint
6865 || bptype == bp_access_watchpoint
fe798b75 6866 || bptype == bp_catchpoint)
88f7da05
KB
6867 {
6868 /* Watchpoints and the various bp_catch_* eventpoints should not
6869 have their addresses modified. */
6870 return bpaddr;
6871 }
76897487
KB
6872 else
6873 {
6874 CORE_ADDR adjusted_bpaddr;
6875
6876 /* Some targets have architectural constraints on the placement
6877 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 6878 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
6879
6880 /* An adjusted breakpoint address can significantly alter
6881 a user's expectations. Print a warning if an adjustment
6882 is required. */
6883 if (adjusted_bpaddr != bpaddr)
6884 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6885
6886 return adjusted_bpaddr;
6887 }
6888}
6889
28010a5d
PA
6890void
6891init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6892 struct breakpoint *owner)
7cc221ef 6893{
7cc221ef
DJ
6894 memset (loc, 0, sizeof (*loc));
6895
348d480f
PA
6896 gdb_assert (ops != NULL);
6897
28010a5d
PA
6898 loc->ops = ops;
6899 loc->owner = owner;
511a6cd4 6900 loc->cond = NULL;
b775012e 6901 loc->cond_bytecode = NULL;
0d381245
VP
6902 loc->shlib_disabled = 0;
6903 loc->enabled = 1;
e049a4b5 6904
28010a5d 6905 switch (owner->type)
e049a4b5
DJ
6906 {
6907 case bp_breakpoint:
6908 case bp_until:
6909 case bp_finish:
6910 case bp_longjmp:
6911 case bp_longjmp_resume:
e2e4d78b 6912 case bp_longjmp_call_dummy:
186c406b
TT
6913 case bp_exception:
6914 case bp_exception_resume:
e049a4b5 6915 case bp_step_resume:
2c03e5be 6916 case bp_hp_step_resume:
e049a4b5
DJ
6917 case bp_watchpoint_scope:
6918 case bp_call_dummy:
aa7d318d 6919 case bp_std_terminate:
e049a4b5
DJ
6920 case bp_shlib_event:
6921 case bp_thread_event:
6922 case bp_overlay_event:
4efc6507 6923 case bp_jit_event:
0fd8e87f 6924 case bp_longjmp_master:
aa7d318d 6925 case bp_std_terminate_master:
186c406b 6926 case bp_exception_master:
0e30163f
JK
6927 case bp_gnu_ifunc_resolver:
6928 case bp_gnu_ifunc_resolver_return:
e7e0cddf 6929 case bp_dprintf:
e049a4b5 6930 loc->loc_type = bp_loc_software_breakpoint;
b775012e 6931 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6932 break;
6933 case bp_hardware_breakpoint:
6934 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 6935 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6936 break;
6937 case bp_hardware_watchpoint:
6938 case bp_read_watchpoint:
6939 case bp_access_watchpoint:
6940 loc->loc_type = bp_loc_hardware_watchpoint;
6941 break;
6942 case bp_watchpoint:
ce78b96d 6943 case bp_catchpoint:
15c3d785
PA
6944 case bp_tracepoint:
6945 case bp_fast_tracepoint:
0fb4aa4b 6946 case bp_static_tracepoint:
e049a4b5
DJ
6947 loc->loc_type = bp_loc_other;
6948 break;
6949 default:
e2e0b3e5 6950 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
6951 }
6952
f431efe5 6953 loc->refc = 1;
28010a5d
PA
6954}
6955
6956/* Allocate a struct bp_location. */
6957
6958static struct bp_location *
6959allocate_bp_location (struct breakpoint *bpt)
6960{
348d480f
PA
6961 return bpt->ops->allocate_location (bpt);
6962}
7cc221ef 6963
f431efe5
PA
6964static void
6965free_bp_location (struct bp_location *loc)
fe3f5fa8 6966{
348d480f 6967 loc->ops->dtor (loc);
fe3f5fa8
VP
6968 xfree (loc);
6969}
6970
f431efe5
PA
6971/* Increment reference count. */
6972
6973static void
6974incref_bp_location (struct bp_location *bl)
6975{
6976 ++bl->refc;
6977}
6978
6979/* Decrement reference count. If the reference count reaches 0,
6980 destroy the bp_location. Sets *BLP to NULL. */
6981
6982static void
6983decref_bp_location (struct bp_location **blp)
6984{
0807b50c
PA
6985 gdb_assert ((*blp)->refc > 0);
6986
f431efe5
PA
6987 if (--(*blp)->refc == 0)
6988 free_bp_location (*blp);
6989 *blp = NULL;
6990}
6991
346774a9 6992/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 6993
346774a9
PA
6994static void
6995add_to_breakpoint_chain (struct breakpoint *b)
c906108c 6996{
346774a9 6997 struct breakpoint *b1;
c906108c 6998
346774a9
PA
6999 /* Add this breakpoint to the end of the chain so that a list of
7000 breakpoints will come out in order of increasing numbers. */
7001
7002 b1 = breakpoint_chain;
7003 if (b1 == 0)
7004 breakpoint_chain = b;
7005 else
7006 {
7007 while (b1->next)
7008 b1 = b1->next;
7009 b1->next = b;
7010 }
7011}
7012
7013/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7014
7015static void
7016init_raw_breakpoint_without_location (struct breakpoint *b,
7017 struct gdbarch *gdbarch,
28010a5d 7018 enum bptype bptype,
c0a91b2b 7019 const struct breakpoint_ops *ops)
346774a9 7020{
c906108c 7021 memset (b, 0, sizeof (*b));
2219d63c 7022
348d480f
PA
7023 gdb_assert (ops != NULL);
7024
28010a5d 7025 b->ops = ops;
4d28f7a8 7026 b->type = bptype;
a6d9a66e 7027 b->gdbarch = gdbarch;
c906108c
SS
7028 b->language = current_language->la_language;
7029 b->input_radix = input_radix;
7030 b->thread = -1;
b5de0fa7 7031 b->enable_state = bp_enabled;
c906108c
SS
7032 b->next = 0;
7033 b->silent = 0;
7034 b->ignore_count = 0;
7035 b->commands = NULL;
818dd999 7036 b->frame_id = null_frame_id;
0d381245 7037 b->condition_not_parsed = 0;
84f4c1fe 7038 b->py_bp_object = NULL;
d0fb5eae 7039 b->related_breakpoint = b;
346774a9
PA
7040}
7041
7042/* Helper to set_raw_breakpoint below. Creates a breakpoint
7043 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7044
7045static struct breakpoint *
7046set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7047 enum bptype bptype,
c0a91b2b 7048 const struct breakpoint_ops *ops)
346774a9
PA
7049{
7050 struct breakpoint *b = XNEW (struct breakpoint);
7051
348d480f 7052 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7053 add_to_breakpoint_chain (b);
0d381245
VP
7054 return b;
7055}
7056
0e30163f
JK
7057/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7058 resolutions should be made as the user specified the location explicitly
7059 enough. */
7060
0d381245 7061static void
0e30163f 7062set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7063{
2bdf28a0
JK
7064 gdb_assert (loc->owner != NULL);
7065
0d381245 7066 if (loc->owner->type == bp_breakpoint
1042e4c0 7067 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7068 || is_tracepoint (loc->owner))
0d381245 7069 {
0e30163f 7070 int is_gnu_ifunc;
2c02bd72 7071 const char *function_name;
6a3a010b 7072 CORE_ADDR func_addr;
0e30163f 7073
2c02bd72 7074 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7075 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7076
7077 if (is_gnu_ifunc && !explicit_loc)
7078 {
7079 struct breakpoint *b = loc->owner;
7080
7081 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7082 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7083 &loc->requested_address))
7084 {
7085 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7086 loc->address = adjust_breakpoint_address (loc->gdbarch,
7087 loc->requested_address,
7088 b->type);
7089 }
7090 else if (b->type == bp_breakpoint && b->loc == loc
7091 && loc->next == NULL && b->related_breakpoint == b)
7092 {
7093 /* Create only the whole new breakpoint of this type but do not
7094 mess more complicated breakpoints with multiple locations. */
7095 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7096 /* Remember the resolver's address for use by the return
7097 breakpoint. */
7098 loc->related_address = func_addr;
0e30163f
JK
7099 }
7100 }
7101
2c02bd72
DE
7102 if (function_name)
7103 loc->function_name = xstrdup (function_name);
0d381245
VP
7104 }
7105}
7106
a6d9a66e 7107/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7108struct gdbarch *
a6d9a66e
UW
7109get_sal_arch (struct symtab_and_line sal)
7110{
7111 if (sal.section)
7112 return get_objfile_arch (sal.section->objfile);
7113 if (sal.symtab)
7114 return get_objfile_arch (sal.symtab->objfile);
7115
7116 return NULL;
7117}
7118
346774a9
PA
7119/* Low level routine for partially initializing a breakpoint of type
7120 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7121 file name, and line number are provided by SAL.
0d381245
VP
7122
7123 It is expected that the caller will complete the initialization of
7124 the newly created breakpoint struct as well as output any status
c56053d2 7125 information regarding the creation of a new breakpoint. */
0d381245 7126
346774a9
PA
7127static void
7128init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7129 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7130 const struct breakpoint_ops *ops)
0d381245 7131{
28010a5d 7132 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7133
3742cc8b 7134 add_location_to_breakpoint (b, &sal);
0d381245 7135
6c95b8df
PA
7136 if (bptype != bp_catchpoint)
7137 gdb_assert (sal.pspace != NULL);
7138
f8eba3c6
TT
7139 /* Store the program space that was used to set the breakpoint,
7140 except for ordinary breakpoints, which are independent of the
7141 program space. */
7142 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7143 b->pspace = sal.pspace;
346774a9 7144}
c906108c 7145
346774a9
PA
7146/* set_raw_breakpoint is a low level routine for allocating and
7147 partially initializing a breakpoint of type BPTYPE. The newly
7148 created breakpoint's address, section, source file name, and line
7149 number are provided by SAL. The newly created and partially
7150 initialized breakpoint is added to the breakpoint chain and
7151 is also returned as the value of this function.
7152
7153 It is expected that the caller will complete the initialization of
7154 the newly created breakpoint struct as well as output any status
7155 information regarding the creation of a new breakpoint. In
7156 particular, set_raw_breakpoint does NOT set the breakpoint
7157 number! Care should be taken to not allow an error to occur
7158 prior to completing the initialization of the breakpoint. If this
7159 should happen, a bogus breakpoint will be left on the chain. */
7160
7161struct breakpoint *
7162set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7163 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7164 const struct breakpoint_ops *ops)
346774a9
PA
7165{
7166 struct breakpoint *b = XNEW (struct breakpoint);
7167
348d480f 7168 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7169 add_to_breakpoint_chain (b);
c906108c
SS
7170 return b;
7171}
7172
c2c6d25f
JM
7173
7174/* Note that the breakpoint object B describes a permanent breakpoint
7175 instruction, hard-wired into the inferior's code. */
7176void
7177make_breakpoint_permanent (struct breakpoint *b)
7178{
0d381245 7179 struct bp_location *bl;
cc59ec59 7180
b5de0fa7 7181 b->enable_state = bp_permanent;
c2c6d25f 7182
4a64f543
MS
7183 /* By definition, permanent breakpoints are already present in the
7184 code. Mark all locations as inserted. For now,
7185 make_breakpoint_permanent is called in just one place, so it's
7186 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7187 multiple locations or not, but it's easy to implement. */
0d381245
VP
7188 for (bl = b->loc; bl; bl = bl->next)
7189 bl->inserted = 1;
c2c6d25f
JM
7190}
7191
53a5351d 7192/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7193 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7194 initiated the operation. */
c906108c
SS
7195
7196void
186c406b 7197set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7198{
35df4500 7199 struct breakpoint *b, *b_tmp;
186c406b 7200 int thread = tp->num;
0fd8e87f
UW
7201
7202 /* To avoid having to rescan all objfile symbols at every step,
7203 we maintain a list of continually-inserted but always disabled
7204 longjmp "master" breakpoints. Here, we simply create momentary
7205 clones of those and enable them for the requested thread. */
35df4500 7206 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7207 if (b->pspace == current_program_space
186c406b
TT
7208 && (b->type == bp_longjmp_master
7209 || b->type == bp_exception_master))
0fd8e87f 7210 {
06edf0c0
PA
7211 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7212 struct breakpoint *clone;
cc59ec59 7213
e2e4d78b
JK
7214 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7215 after their removal. */
06edf0c0 7216 clone = momentary_breakpoint_from_master (b, type,
e2e4d78b 7217 &longjmp_breakpoint_ops);
0fd8e87f
UW
7218 clone->thread = thread;
7219 }
186c406b
TT
7220
7221 tp->initiating_frame = frame;
c906108c
SS
7222}
7223
611c83ae 7224/* Delete all longjmp breakpoints from THREAD. */
c906108c 7225void
611c83ae 7226delete_longjmp_breakpoint (int thread)
c906108c 7227{
35df4500 7228 struct breakpoint *b, *b_tmp;
c906108c 7229
35df4500 7230 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7231 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7232 {
7233 if (b->thread == thread)
7234 delete_breakpoint (b);
7235 }
c906108c
SS
7236}
7237
f59f708a
PA
7238void
7239delete_longjmp_breakpoint_at_next_stop (int thread)
7240{
7241 struct breakpoint *b, *b_tmp;
7242
7243 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7244 if (b->type == bp_longjmp || b->type == bp_exception)
7245 {
7246 if (b->thread == thread)
7247 b->disposition = disp_del_at_next_stop;
7248 }
7249}
7250
e2e4d78b
JK
7251/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7252 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7253 pointer to any of them. Return NULL if this system cannot place longjmp
7254 breakpoints. */
7255
7256struct breakpoint *
7257set_longjmp_breakpoint_for_call_dummy (void)
7258{
7259 struct breakpoint *b, *retval = NULL;
7260
7261 ALL_BREAKPOINTS (b)
7262 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7263 {
7264 struct breakpoint *new_b;
7265
7266 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7267 &momentary_breakpoint_ops);
7268 new_b->thread = pid_to_thread_id (inferior_ptid);
7269
7270 /* Link NEW_B into the chain of RETVAL breakpoints. */
7271
7272 gdb_assert (new_b->related_breakpoint == new_b);
7273 if (retval == NULL)
7274 retval = new_b;
7275 new_b->related_breakpoint = retval;
7276 while (retval->related_breakpoint != new_b->related_breakpoint)
7277 retval = retval->related_breakpoint;
7278 retval->related_breakpoint = new_b;
7279 }
7280
7281 return retval;
7282}
7283
7284/* Verify all existing dummy frames and their associated breakpoints for
7285 THREAD. Remove those which can no longer be found in the current frame
7286 stack.
7287
7288 You should call this function only at places where it is safe to currently
7289 unwind the whole stack. Failed stack unwind would discard live dummy
7290 frames. */
7291
7292void
7293check_longjmp_breakpoint_for_call_dummy (int thread)
7294{
7295 struct breakpoint *b, *b_tmp;
7296
7297 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7298 if (b->type == bp_longjmp_call_dummy && b->thread == thread)
7299 {
7300 struct breakpoint *dummy_b = b->related_breakpoint;
7301
7302 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7303 dummy_b = dummy_b->related_breakpoint;
7304 if (dummy_b->type != bp_call_dummy
7305 || frame_find_by_id (dummy_b->frame_id) != NULL)
7306 continue;
7307
7308 dummy_frame_discard (dummy_b->frame_id);
7309
7310 while (b->related_breakpoint != b)
7311 {
7312 if (b_tmp == b->related_breakpoint)
7313 b_tmp = b->related_breakpoint->next;
7314 delete_breakpoint (b->related_breakpoint);
7315 }
7316 delete_breakpoint (b);
7317 }
7318}
7319
1900040c
MS
7320void
7321enable_overlay_breakpoints (void)
7322{
52f0bd74 7323 struct breakpoint *b;
1900040c
MS
7324
7325 ALL_BREAKPOINTS (b)
7326 if (b->type == bp_overlay_event)
7327 {
7328 b->enable_state = bp_enabled;
b60e7edf 7329 update_global_location_list (1);
c02f5703 7330 overlay_events_enabled = 1;
1900040c
MS
7331 }
7332}
7333
7334void
7335disable_overlay_breakpoints (void)
7336{
52f0bd74 7337 struct breakpoint *b;
1900040c
MS
7338
7339 ALL_BREAKPOINTS (b)
7340 if (b->type == bp_overlay_event)
7341 {
7342 b->enable_state = bp_disabled;
b60e7edf 7343 update_global_location_list (0);
c02f5703 7344 overlay_events_enabled = 0;
1900040c
MS
7345 }
7346}
7347
aa7d318d
TT
7348/* Set an active std::terminate breakpoint for each std::terminate
7349 master breakpoint. */
7350void
7351set_std_terminate_breakpoint (void)
7352{
35df4500 7353 struct breakpoint *b, *b_tmp;
aa7d318d 7354
35df4500 7355 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7356 if (b->pspace == current_program_space
7357 && b->type == bp_std_terminate_master)
7358 {
06edf0c0
PA
7359 momentary_breakpoint_from_master (b, bp_std_terminate,
7360 &momentary_breakpoint_ops);
aa7d318d
TT
7361 }
7362}
7363
7364/* Delete all the std::terminate breakpoints. */
7365void
7366delete_std_terminate_breakpoint (void)
7367{
35df4500 7368 struct breakpoint *b, *b_tmp;
aa7d318d 7369
35df4500 7370 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7371 if (b->type == bp_std_terminate)
7372 delete_breakpoint (b);
7373}
7374
c4093a6a 7375struct breakpoint *
a6d9a66e 7376create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7377{
7378 struct breakpoint *b;
c4093a6a 7379
06edf0c0
PA
7380 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7381 &internal_breakpoint_ops);
7382
b5de0fa7 7383 b->enable_state = bp_enabled;
c4093a6a 7384 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7385 b->addr_string
7386 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7387
b60e7edf 7388 update_global_location_list_nothrow (1);
74960c60 7389
c4093a6a
JM
7390 return b;
7391}
7392
7393void
7394remove_thread_event_breakpoints (void)
7395{
35df4500 7396 struct breakpoint *b, *b_tmp;
c4093a6a 7397
35df4500 7398 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7399 if (b->type == bp_thread_event
7400 && b->loc->pspace == current_program_space)
c4093a6a
JM
7401 delete_breakpoint (b);
7402}
7403
0101ce28
JJ
7404struct lang_and_radix
7405 {
7406 enum language lang;
7407 int radix;
7408 };
7409
4efc6507
DE
7410/* Create a breakpoint for JIT code registration and unregistration. */
7411
7412struct breakpoint *
7413create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7414{
7415 struct breakpoint *b;
7416
06edf0c0
PA
7417 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7418 &internal_breakpoint_ops);
4efc6507
DE
7419 update_global_location_list_nothrow (1);
7420 return b;
7421}
0101ce28 7422
03673fc7
PP
7423/* Remove JIT code registration and unregistration breakpoint(s). */
7424
7425void
7426remove_jit_event_breakpoints (void)
7427{
7428 struct breakpoint *b, *b_tmp;
7429
7430 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7431 if (b->type == bp_jit_event
7432 && b->loc->pspace == current_program_space)
7433 delete_breakpoint (b);
7434}
7435
cae688ec
JJ
7436void
7437remove_solib_event_breakpoints (void)
7438{
35df4500 7439 struct breakpoint *b, *b_tmp;
cae688ec 7440
35df4500 7441 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7442 if (b->type == bp_shlib_event
7443 && b->loc->pspace == current_program_space)
cae688ec
JJ
7444 delete_breakpoint (b);
7445}
7446
7447struct breakpoint *
a6d9a66e 7448create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
7449{
7450 struct breakpoint *b;
7451
06edf0c0
PA
7452 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7453 &internal_breakpoint_ops);
b60e7edf 7454 update_global_location_list_nothrow (1);
cae688ec
JJ
7455 return b;
7456}
7457
7458/* Disable any breakpoints that are on code in shared libraries. Only
7459 apply to enabled breakpoints, disabled ones can just stay disabled. */
7460
7461void
cb851954 7462disable_breakpoints_in_shlibs (void)
cae688ec 7463{
876fa593 7464 struct bp_location *loc, **locp_tmp;
cae688ec 7465
876fa593 7466 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7467 {
2bdf28a0 7468 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7469 struct breakpoint *b = loc->owner;
2bdf28a0 7470
4a64f543
MS
7471 /* We apply the check to all breakpoints, including disabled for
7472 those with loc->duplicate set. This is so that when breakpoint
7473 becomes enabled, or the duplicate is removed, gdb will try to
7474 insert all breakpoints. If we don't set shlib_disabled here,
7475 we'll try to insert those breakpoints and fail. */
1042e4c0 7476 if (((b->type == bp_breakpoint)
508ccb1f 7477 || (b->type == bp_jit_event)
1042e4c0 7478 || (b->type == bp_hardware_breakpoint)
d77f58be 7479 || (is_tracepoint (b)))
6c95b8df 7480 && loc->pspace == current_program_space
0d381245 7481 && !loc->shlib_disabled
6c95b8df 7482 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7483 )
0d381245
VP
7484 {
7485 loc->shlib_disabled = 1;
7486 }
cae688ec
JJ
7487 }
7488}
7489
63644780
NB
7490/* Disable any breakpoints and tracepoints that are in SOLIB upon
7491 notification of unloaded_shlib. Only apply to enabled breakpoints,
7492 disabled ones can just stay disabled. */
84acb35a 7493
75149521 7494static void
84acb35a
JJ
7495disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7496{
876fa593 7497 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7498 int disabled_shlib_breaks = 0;
7499
c86cf029
VP
7500 /* SunOS a.out shared libraries are always mapped, so do not
7501 disable breakpoints; they will only be reported as unloaded
7502 through clear_solib when GDB discards its shared library
7503 list. See clear_solib for more information. */
7504 if (exec_bfd != NULL
7505 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7506 return;
7507
876fa593 7508 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7509 {
2bdf28a0 7510 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7511 struct breakpoint *b = loc->owner;
cc59ec59 7512
1e4d1764 7513 if (solib->pspace == loc->pspace
e2dd7057 7514 && !loc->shlib_disabled
1e4d1764
YQ
7515 && (((b->type == bp_breakpoint
7516 || b->type == bp_jit_event
7517 || b->type == bp_hardware_breakpoint)
7518 && (loc->loc_type == bp_loc_hardware_breakpoint
7519 || loc->loc_type == bp_loc_software_breakpoint))
7520 || is_tracepoint (b))
e2dd7057 7521 && solib_contains_address_p (solib, loc->address))
84acb35a 7522 {
e2dd7057
PP
7523 loc->shlib_disabled = 1;
7524 /* At this point, we cannot rely on remove_breakpoint
7525 succeeding so we must mark the breakpoint as not inserted
7526 to prevent future errors occurring in remove_breakpoints. */
7527 loc->inserted = 0;
8d3788bd
VP
7528
7529 /* This may cause duplicate notifications for the same breakpoint. */
7530 observer_notify_breakpoint_modified (b);
7531
e2dd7057
PP
7532 if (!disabled_shlib_breaks)
7533 {
7534 target_terminal_ours_for_output ();
3e43a32a
MS
7535 warning (_("Temporarily disabling breakpoints "
7536 "for unloaded shared library \"%s\""),
e2dd7057 7537 solib->so_name);
84acb35a 7538 }
e2dd7057 7539 disabled_shlib_breaks = 1;
84acb35a
JJ
7540 }
7541 }
84acb35a
JJ
7542}
7543
63644780
NB
7544/* Disable any breakpoints and tracepoints in OBJFILE upon
7545 notification of free_objfile. Only apply to enabled breakpoints,
7546 disabled ones can just stay disabled. */
7547
7548static void
7549disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7550{
7551 struct breakpoint *b;
7552
7553 if (objfile == NULL)
7554 return;
7555
7556 /* If the file is a shared library not loaded by the user then
7557 solib_unloaded was notified and disable_breakpoints_in_unloaded_shlib
7558 was called. In that case there is no need to take action again. */
7559 if ((objfile->flags & OBJF_SHARED) && !(objfile->flags & OBJF_USERLOADED))
7560 return;
7561
7562 ALL_BREAKPOINTS (b)
7563 {
7564 struct bp_location *loc;
7565 int bp_modified = 0;
7566
7567 if (!is_breakpoint (b) && !is_tracepoint (b))
7568 continue;
7569
7570 for (loc = b->loc; loc != NULL; loc = loc->next)
7571 {
7572 CORE_ADDR loc_addr = loc->address;
7573
7574 if (loc->loc_type != bp_loc_hardware_breakpoint
7575 && loc->loc_type != bp_loc_software_breakpoint)
7576 continue;
7577
7578 if (loc->shlib_disabled != 0)
7579 continue;
7580
7581 if (objfile->pspace != loc->pspace)
7582 continue;
7583
7584 if (loc->loc_type != bp_loc_hardware_breakpoint
7585 && loc->loc_type != bp_loc_software_breakpoint)
7586 continue;
7587
7588 if (is_addr_in_objfile (loc_addr, objfile))
7589 {
7590 loc->shlib_disabled = 1;
7591 loc->inserted = 0;
7592
7593 mark_breakpoint_location_modified (loc);
7594
7595 bp_modified = 1;
7596 }
7597 }
7598
7599 if (bp_modified)
7600 observer_notify_breakpoint_modified (b);
7601 }
7602}
7603
ce78b96d
JB
7604/* FORK & VFORK catchpoints. */
7605
e29a4733
PA
7606/* An instance of this type is used to represent a fork or vfork
7607 catchpoint. It includes a "struct breakpoint" as a kind of base
7608 class; users downcast to "struct breakpoint *" when needed. A
7609 breakpoint is really of this type iff its ops pointer points to
7610 CATCH_FORK_BREAKPOINT_OPS. */
7611
7612struct fork_catchpoint
7613{
7614 /* The base class. */
7615 struct breakpoint base;
7616
7617 /* Process id of a child process whose forking triggered this
7618 catchpoint. This field is only valid immediately after this
7619 catchpoint has triggered. */
7620 ptid_t forked_inferior_pid;
7621};
7622
4a64f543
MS
7623/* Implement the "insert" breakpoint_ops method for fork
7624 catchpoints. */
ce78b96d 7625
77b06cd7
TJB
7626static int
7627insert_catch_fork (struct bp_location *bl)
ce78b96d 7628{
dfd4cc63 7629 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7630}
7631
4a64f543
MS
7632/* Implement the "remove" breakpoint_ops method for fork
7633 catchpoints. */
ce78b96d
JB
7634
7635static int
77b06cd7 7636remove_catch_fork (struct bp_location *bl)
ce78b96d 7637{
dfd4cc63 7638 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7639}
7640
7641/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7642 catchpoints. */
7643
7644static int
f1310107 7645breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7646 struct address_space *aspace, CORE_ADDR bp_addr,
7647 const struct target_waitstatus *ws)
ce78b96d 7648{
e29a4733
PA
7649 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7650
f90263c1
TT
7651 if (ws->kind != TARGET_WAITKIND_FORKED)
7652 return 0;
7653
7654 c->forked_inferior_pid = ws->value.related_pid;
7655 return 1;
ce78b96d
JB
7656}
7657
4a64f543
MS
7658/* Implement the "print_it" breakpoint_ops method for fork
7659 catchpoints. */
ce78b96d
JB
7660
7661static enum print_stop_action
348d480f 7662print_it_catch_fork (bpstat bs)
ce78b96d 7663{
36dfb11c 7664 struct ui_out *uiout = current_uiout;
348d480f
PA
7665 struct breakpoint *b = bs->breakpoint_at;
7666 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7667
ce78b96d 7668 annotate_catchpoint (b->number);
36dfb11c
TT
7669 if (b->disposition == disp_del)
7670 ui_out_text (uiout, "\nTemporary catchpoint ");
7671 else
7672 ui_out_text (uiout, "\nCatchpoint ");
7673 if (ui_out_is_mi_like_p (uiout))
7674 {
7675 ui_out_field_string (uiout, "reason",
7676 async_reason_lookup (EXEC_ASYNC_FORK));
7677 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7678 }
7679 ui_out_field_int (uiout, "bkptno", b->number);
7680 ui_out_text (uiout, " (forked process ");
7681 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7682 ui_out_text (uiout, "), ");
ce78b96d
JB
7683 return PRINT_SRC_AND_LOC;
7684}
7685
4a64f543
MS
7686/* Implement the "print_one" breakpoint_ops method for fork
7687 catchpoints. */
ce78b96d
JB
7688
7689static void
a6d9a66e 7690print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7691{
e29a4733 7692 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7693 struct value_print_options opts;
79a45e25 7694 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7695
7696 get_user_print_options (&opts);
7697
4a64f543
MS
7698 /* Field 4, the address, is omitted (which makes the columns not
7699 line up too nicely with the headers, but the effect is relatively
7700 readable). */
79a45b7d 7701 if (opts.addressprint)
ce78b96d
JB
7702 ui_out_field_skip (uiout, "addr");
7703 annotate_field (5);
7704 ui_out_text (uiout, "fork");
e29a4733 7705 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7706 {
7707 ui_out_text (uiout, ", process ");
7708 ui_out_field_int (uiout, "what",
e29a4733 7709 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7710 ui_out_spaces (uiout, 1);
7711 }
8ac3646f
TT
7712
7713 if (ui_out_is_mi_like_p (uiout))
7714 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
7715}
7716
7717/* Implement the "print_mention" breakpoint_ops method for fork
7718 catchpoints. */
7719
7720static void
7721print_mention_catch_fork (struct breakpoint *b)
7722{
7723 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7724}
7725
6149aea9
PA
7726/* Implement the "print_recreate" breakpoint_ops method for fork
7727 catchpoints. */
7728
7729static void
7730print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7731{
7732 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7733 print_recreate_thread (b, fp);
6149aea9
PA
7734}
7735
ce78b96d
JB
7736/* The breakpoint_ops structure to be used in fork catchpoints. */
7737
2060206e 7738static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7739
4a64f543
MS
7740/* Implement the "insert" breakpoint_ops method for vfork
7741 catchpoints. */
ce78b96d 7742
77b06cd7
TJB
7743static int
7744insert_catch_vfork (struct bp_location *bl)
ce78b96d 7745{
dfd4cc63 7746 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7747}
7748
4a64f543
MS
7749/* Implement the "remove" breakpoint_ops method for vfork
7750 catchpoints. */
ce78b96d
JB
7751
7752static int
77b06cd7 7753remove_catch_vfork (struct bp_location *bl)
ce78b96d 7754{
dfd4cc63 7755 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7756}
7757
7758/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7759 catchpoints. */
7760
7761static int
f1310107 7762breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
7763 struct address_space *aspace, CORE_ADDR bp_addr,
7764 const struct target_waitstatus *ws)
ce78b96d 7765{
e29a4733
PA
7766 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7767
f90263c1
TT
7768 if (ws->kind != TARGET_WAITKIND_VFORKED)
7769 return 0;
7770
7771 c->forked_inferior_pid = ws->value.related_pid;
7772 return 1;
ce78b96d
JB
7773}
7774
4a64f543
MS
7775/* Implement the "print_it" breakpoint_ops method for vfork
7776 catchpoints. */
ce78b96d
JB
7777
7778static enum print_stop_action
348d480f 7779print_it_catch_vfork (bpstat bs)
ce78b96d 7780{
36dfb11c 7781 struct ui_out *uiout = current_uiout;
348d480f 7782 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7783 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7784
ce78b96d 7785 annotate_catchpoint (b->number);
36dfb11c
TT
7786 if (b->disposition == disp_del)
7787 ui_out_text (uiout, "\nTemporary catchpoint ");
7788 else
7789 ui_out_text (uiout, "\nCatchpoint ");
7790 if (ui_out_is_mi_like_p (uiout))
7791 {
7792 ui_out_field_string (uiout, "reason",
7793 async_reason_lookup (EXEC_ASYNC_VFORK));
7794 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7795 }
7796 ui_out_field_int (uiout, "bkptno", b->number);
7797 ui_out_text (uiout, " (vforked process ");
7798 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7799 ui_out_text (uiout, "), ");
ce78b96d
JB
7800 return PRINT_SRC_AND_LOC;
7801}
7802
4a64f543
MS
7803/* Implement the "print_one" breakpoint_ops method for vfork
7804 catchpoints. */
ce78b96d
JB
7805
7806static void
a6d9a66e 7807print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7808{
e29a4733 7809 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7810 struct value_print_options opts;
79a45e25 7811 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7812
7813 get_user_print_options (&opts);
4a64f543
MS
7814 /* Field 4, the address, is omitted (which makes the columns not
7815 line up too nicely with the headers, but the effect is relatively
7816 readable). */
79a45b7d 7817 if (opts.addressprint)
ce78b96d
JB
7818 ui_out_field_skip (uiout, "addr");
7819 annotate_field (5);
7820 ui_out_text (uiout, "vfork");
e29a4733 7821 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7822 {
7823 ui_out_text (uiout, ", process ");
7824 ui_out_field_int (uiout, "what",
e29a4733 7825 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7826 ui_out_spaces (uiout, 1);
7827 }
8ac3646f
TT
7828
7829 if (ui_out_is_mi_like_p (uiout))
7830 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
7831}
7832
7833/* Implement the "print_mention" breakpoint_ops method for vfork
7834 catchpoints. */
7835
7836static void
7837print_mention_catch_vfork (struct breakpoint *b)
7838{
7839 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7840}
7841
6149aea9
PA
7842/* Implement the "print_recreate" breakpoint_ops method for vfork
7843 catchpoints. */
7844
7845static void
7846print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7847{
7848 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7849 print_recreate_thread (b, fp);
6149aea9
PA
7850}
7851
ce78b96d
JB
7852/* The breakpoint_ops structure to be used in vfork catchpoints. */
7853
2060206e 7854static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7855
edcc5120
TT
7856/* An instance of this type is used to represent an solib catchpoint.
7857 It includes a "struct breakpoint" as a kind of base class; users
7858 downcast to "struct breakpoint *" when needed. A breakpoint is
7859 really of this type iff its ops pointer points to
7860 CATCH_SOLIB_BREAKPOINT_OPS. */
7861
7862struct solib_catchpoint
7863{
7864 /* The base class. */
7865 struct breakpoint base;
7866
7867 /* True for "catch load", false for "catch unload". */
7868 unsigned char is_load;
7869
7870 /* Regular expression to match, if any. COMPILED is only valid when
7871 REGEX is non-NULL. */
7872 char *regex;
7873 regex_t compiled;
7874};
7875
7876static void
7877dtor_catch_solib (struct breakpoint *b)
7878{
7879 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7880
7881 if (self->regex)
7882 regfree (&self->compiled);
7883 xfree (self->regex);
7884
7885 base_breakpoint_ops.dtor (b);
7886}
7887
7888static int
7889insert_catch_solib (struct bp_location *ignore)
7890{
7891 return 0;
7892}
7893
7894static int
7895remove_catch_solib (struct bp_location *ignore)
7896{
7897 return 0;
7898}
7899
7900static int
7901breakpoint_hit_catch_solib (const struct bp_location *bl,
7902 struct address_space *aspace,
7903 CORE_ADDR bp_addr,
7904 const struct target_waitstatus *ws)
7905{
7906 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7907 struct breakpoint *other;
7908
7909 if (ws->kind == TARGET_WAITKIND_LOADED)
7910 return 1;
7911
7912 ALL_BREAKPOINTS (other)
7913 {
7914 struct bp_location *other_bl;
7915
7916 if (other == bl->owner)
7917 continue;
7918
7919 if (other->type != bp_shlib_event)
7920 continue;
7921
7922 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7923 continue;
7924
7925 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7926 {
7927 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7928 return 1;
7929 }
7930 }
7931
7932 return 0;
7933}
7934
7935static void
7936check_status_catch_solib (struct bpstats *bs)
7937{
7938 struct solib_catchpoint *self
7939 = (struct solib_catchpoint *) bs->breakpoint_at;
7940 int ix;
7941
7942 if (self->is_load)
7943 {
7944 struct so_list *iter;
7945
7946 for (ix = 0;
7947 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7948 ix, iter);
7949 ++ix)
7950 {
7951 if (!self->regex
7952 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7953 return;
7954 }
7955 }
7956 else
7957 {
7958 char *iter;
7959
7960 for (ix = 0;
7961 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7962 ix, iter);
7963 ++ix)
7964 {
7965 if (!self->regex
7966 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7967 return;
7968 }
7969 }
7970
7971 bs->stop = 0;
7972 bs->print_it = print_it_noop;
7973}
7974
7975static enum print_stop_action
7976print_it_catch_solib (bpstat bs)
7977{
7978 struct breakpoint *b = bs->breakpoint_at;
7979 struct ui_out *uiout = current_uiout;
7980
7981 annotate_catchpoint (b->number);
7982 if (b->disposition == disp_del)
7983 ui_out_text (uiout, "\nTemporary catchpoint ");
7984 else
7985 ui_out_text (uiout, "\nCatchpoint ");
7986 ui_out_field_int (uiout, "bkptno", b->number);
7987 ui_out_text (uiout, "\n");
7988 if (ui_out_is_mi_like_p (uiout))
7989 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7990 print_solib_event (1);
7991 return PRINT_SRC_AND_LOC;
7992}
7993
7994static void
7995print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7996{
7997 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7998 struct value_print_options opts;
7999 struct ui_out *uiout = current_uiout;
8000 char *msg;
8001
8002 get_user_print_options (&opts);
8003 /* Field 4, the address, is omitted (which makes the columns not
8004 line up too nicely with the headers, but the effect is relatively
8005 readable). */
8006 if (opts.addressprint)
8007 {
8008 annotate_field (4);
8009 ui_out_field_skip (uiout, "addr");
8010 }
8011
8012 annotate_field (5);
8013 if (self->is_load)
8014 {
8015 if (self->regex)
8016 msg = xstrprintf (_("load of library matching %s"), self->regex);
8017 else
8018 msg = xstrdup (_("load of library"));
8019 }
8020 else
8021 {
8022 if (self->regex)
8023 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8024 else
8025 msg = xstrdup (_("unload of library"));
8026 }
8027 ui_out_field_string (uiout, "what", msg);
8028 xfree (msg);
8ac3646f
TT
8029
8030 if (ui_out_is_mi_like_p (uiout))
8031 ui_out_field_string (uiout, "catch-type",
8032 self->is_load ? "load" : "unload");
edcc5120
TT
8033}
8034
8035static void
8036print_mention_catch_solib (struct breakpoint *b)
8037{
8038 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8039
8040 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8041 self->is_load ? "load" : "unload");
8042}
8043
8044static void
8045print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8046{
8047 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8048
8049 fprintf_unfiltered (fp, "%s %s",
8050 b->disposition == disp_del ? "tcatch" : "catch",
8051 self->is_load ? "load" : "unload");
8052 if (self->regex)
8053 fprintf_unfiltered (fp, " %s", self->regex);
8054 fprintf_unfiltered (fp, "\n");
8055}
8056
8057static struct breakpoint_ops catch_solib_breakpoint_ops;
8058
91985142
MG
8059/* Shared helper function (MI and CLI) for creating and installing
8060 a shared object event catchpoint. If IS_LOAD is non-zero then
8061 the events to be caught are load events, otherwise they are
8062 unload events. If IS_TEMP is non-zero the catchpoint is a
8063 temporary one. If ENABLED is non-zero the catchpoint is
8064 created in an enabled state. */
edcc5120 8065
91985142
MG
8066void
8067add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8068{
8069 struct solib_catchpoint *c;
8070 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8071 struct cleanup *cleanup;
8072
edcc5120
TT
8073 if (!arg)
8074 arg = "";
8075 arg = skip_spaces (arg);
8076
8077 c = XCNEW (struct solib_catchpoint);
8078 cleanup = make_cleanup (xfree, c);
8079
8080 if (*arg != '\0')
8081 {
8082 int errcode;
8083
8084 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8085 if (errcode != 0)
8086 {
8087 char *err = get_regcomp_error (errcode, &c->compiled);
8088
8089 make_cleanup (xfree, err);
8090 error (_("Invalid regexp (%s): %s"), err, arg);
8091 }
8092 c->regex = xstrdup (arg);
8093 }
8094
8095 c->is_load = is_load;
91985142 8096 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8097 &catch_solib_breakpoint_ops);
8098
91985142
MG
8099 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8100
edcc5120
TT
8101 discard_cleanups (cleanup);
8102 install_breakpoint (0, &c->base, 1);
8103}
8104
91985142
MG
8105/* A helper function that does all the work for "catch load" and
8106 "catch unload". */
8107
8108static void
8109catch_load_or_unload (char *arg, int from_tty, int is_load,
8110 struct cmd_list_element *command)
8111{
8112 int tempflag;
8113 const int enabled = 1;
8114
8115 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8116
8117 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8118}
8119
edcc5120
TT
8120static void
8121catch_load_command_1 (char *arg, int from_tty,
8122 struct cmd_list_element *command)
8123{
8124 catch_load_or_unload (arg, from_tty, 1, command);
8125}
8126
8127static void
8128catch_unload_command_1 (char *arg, int from_tty,
8129 struct cmd_list_element *command)
8130{
8131 catch_load_or_unload (arg, from_tty, 0, command);
8132}
8133
be5c67c1
PA
8134/* An instance of this type is used to represent a syscall catchpoint.
8135 It includes a "struct breakpoint" as a kind of base class; users
8136 downcast to "struct breakpoint *" when needed. A breakpoint is
8137 really of this type iff its ops pointer points to
8138 CATCH_SYSCALL_BREAKPOINT_OPS. */
8139
8140struct syscall_catchpoint
8141{
8142 /* The base class. */
8143 struct breakpoint base;
8144
8145 /* Syscall numbers used for the 'catch syscall' feature. If no
8146 syscall has been specified for filtering, its value is NULL.
8147 Otherwise, it holds a list of all syscalls to be caught. The
8148 list elements are allocated with xmalloc. */
8149 VEC(int) *syscalls_to_be_caught;
8150};
8151
8152/* Implement the "dtor" breakpoint_ops method for syscall
8153 catchpoints. */
8154
8155static void
8156dtor_catch_syscall (struct breakpoint *b)
8157{
8158 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8159
8160 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8161
2060206e 8162 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8163}
8164
fa3064dd
YQ
8165static const struct inferior_data *catch_syscall_inferior_data = NULL;
8166
8167struct catch_syscall_inferior_data
8168{
8169 /* We keep a count of the number of times the user has requested a
8170 particular syscall to be tracked, and pass this information to the
8171 target. This lets capable targets implement filtering directly. */
8172
8173 /* Number of times that "any" syscall is requested. */
8174 int any_syscall_count;
8175
8176 /* Count of each system call. */
8177 VEC(int) *syscalls_counts;
8178
8179 /* This counts all syscall catch requests, so we can readily determine
8180 if any catching is necessary. */
8181 int total_syscalls_count;
8182};
8183
8184static struct catch_syscall_inferior_data*
8185get_catch_syscall_inferior_data (struct inferior *inf)
8186{
8187 struct catch_syscall_inferior_data *inf_data;
8188
8189 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8190 if (inf_data == NULL)
8191 {
8192 inf_data = XZALLOC (struct catch_syscall_inferior_data);
8193 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8194 }
8195
8196 return inf_data;
8197}
8198
8199static void
8200catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8201{
8202 xfree (arg);
8203}
8204
8205
a96d9b2e
SDJ
8206/* Implement the "insert" breakpoint_ops method for syscall
8207 catchpoints. */
8208
77b06cd7
TJB
8209static int
8210insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8211{
be5c67c1 8212 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8213 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8214 struct catch_syscall_inferior_data *inf_data
8215 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8216
fa3064dd 8217 ++inf_data->total_syscalls_count;
be5c67c1 8218 if (!c->syscalls_to_be_caught)
fa3064dd 8219 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8220 else
8221 {
8222 int i, iter;
cc59ec59 8223
a96d9b2e 8224 for (i = 0;
be5c67c1 8225 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8226 i++)
8227 {
8228 int elem;
cc59ec59 8229
fa3064dd 8230 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8231 {
fa3064dd 8232 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8233 uintptr_t vec_addr_offset
8234 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8235 uintptr_t vec_addr;
fa3064dd
YQ
8236 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8237 vec_addr = ((uintptr_t) VEC_address (int,
8238 inf_data->syscalls_counts)
8239 + vec_addr_offset);
a96d9b2e
SDJ
8240 memset ((void *) vec_addr, 0,
8241 (iter + 1 - old_size) * sizeof (int));
8242 }
fa3064dd
YQ
8243 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8244 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8245 }
8246 }
8247
dfd4cc63 8248 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8249 inf_data->total_syscalls_count != 0,
8250 inf_data->any_syscall_count,
8251 VEC_length (int,
8252 inf_data->syscalls_counts),
8253 VEC_address (int,
8254 inf_data->syscalls_counts));
a96d9b2e
SDJ
8255}
8256
8257/* Implement the "remove" breakpoint_ops method for syscall
8258 catchpoints. */
8259
8260static int
77b06cd7 8261remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8262{
be5c67c1 8263 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8264 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8265 struct catch_syscall_inferior_data *inf_data
8266 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8267
fa3064dd 8268 --inf_data->total_syscalls_count;
be5c67c1 8269 if (!c->syscalls_to_be_caught)
fa3064dd 8270 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8271 else
8272 {
8273 int i, iter;
cc59ec59 8274
a96d9b2e 8275 for (i = 0;
be5c67c1 8276 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8277 i++)
8278 {
8279 int elem;
fa3064dd 8280 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8281 /* Shouldn't happen. */
8282 continue;
fa3064dd
YQ
8283 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8284 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8285 }
8286 }
8287
dfd4cc63 8288 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8289 inf_data->total_syscalls_count != 0,
8290 inf_data->any_syscall_count,
8291 VEC_length (int,
8292 inf_data->syscalls_counts),
3e43a32a 8293 VEC_address (int,
fa3064dd 8294 inf_data->syscalls_counts));
a96d9b2e
SDJ
8295}
8296
8297/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8298 catchpoints. */
8299
8300static int
f1310107 8301breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8302 struct address_space *aspace, CORE_ADDR bp_addr,
8303 const struct target_waitstatus *ws)
a96d9b2e 8304{
4a64f543
MS
8305 /* We must check if we are catching specific syscalls in this
8306 breakpoint. If we are, then we must guarantee that the called
8307 syscall is the same syscall we are catching. */
a96d9b2e 8308 int syscall_number = 0;
be5c67c1
PA
8309 const struct syscall_catchpoint *c
8310 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8311
f90263c1
TT
8312 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8313 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8314 return 0;
8315
f90263c1
TT
8316 syscall_number = ws->value.syscall_number;
8317
a96d9b2e 8318 /* Now, checking if the syscall is the same. */
be5c67c1 8319 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8320 {
8321 int i, iter;
cc59ec59 8322
a96d9b2e 8323 for (i = 0;
be5c67c1 8324 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8325 i++)
8326 if (syscall_number == iter)
4924df79
GKB
8327 return 1;
8328
8329 return 0;
a96d9b2e
SDJ
8330 }
8331
8332 return 1;
8333}
8334
8335/* Implement the "print_it" breakpoint_ops method for syscall
8336 catchpoints. */
8337
8338static enum print_stop_action
348d480f 8339print_it_catch_syscall (bpstat bs)
a96d9b2e 8340{
36dfb11c 8341 struct ui_out *uiout = current_uiout;
348d480f 8342 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8343 /* These are needed because we want to know in which state a
8344 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8345 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8346 must print "called syscall" or "returned from syscall". */
8347 ptid_t ptid;
8348 struct target_waitstatus last;
8349 struct syscall s;
a96d9b2e
SDJ
8350
8351 get_last_target_status (&ptid, &last);
8352
8353 get_syscall_by_number (last.value.syscall_number, &s);
8354
8355 annotate_catchpoint (b->number);
8356
36dfb11c
TT
8357 if (b->disposition == disp_del)
8358 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8359 else
36dfb11c
TT
8360 ui_out_text (uiout, "\nCatchpoint ");
8361 if (ui_out_is_mi_like_p (uiout))
8362 {
8363 ui_out_field_string (uiout, "reason",
8364 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8365 ? EXEC_ASYNC_SYSCALL_ENTRY
8366 : EXEC_ASYNC_SYSCALL_RETURN));
8367 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8368 }
8369 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8370
8371 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8372 ui_out_text (uiout, " (call to syscall ");
8373 else
8374 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8375
36dfb11c
TT
8376 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8377 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8378 if (s.name != NULL)
8379 ui_out_field_string (uiout, "syscall-name", s.name);
8380
8381 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8382
8383 return PRINT_SRC_AND_LOC;
8384}
8385
8386/* Implement the "print_one" breakpoint_ops method for syscall
8387 catchpoints. */
8388
8389static void
8390print_one_catch_syscall (struct breakpoint *b,
f1310107 8391 struct bp_location **last_loc)
a96d9b2e 8392{
be5c67c1 8393 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8394 struct value_print_options opts;
79a45e25 8395 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
8396
8397 get_user_print_options (&opts);
4a64f543
MS
8398 /* Field 4, the address, is omitted (which makes the columns not
8399 line up too nicely with the headers, but the effect is relatively
8400 readable). */
a96d9b2e
SDJ
8401 if (opts.addressprint)
8402 ui_out_field_skip (uiout, "addr");
8403 annotate_field (5);
8404
be5c67c1
PA
8405 if (c->syscalls_to_be_caught
8406 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8407 ui_out_text (uiout, "syscalls \"");
8408 else
8409 ui_out_text (uiout, "syscall \"");
8410
be5c67c1 8411 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8412 {
8413 int i, iter;
8414 char *text = xstrprintf ("%s", "");
cc59ec59 8415
a96d9b2e 8416 for (i = 0;
be5c67c1 8417 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8418 i++)
8419 {
8420 char *x = text;
8421 struct syscall s;
8422 get_syscall_by_number (iter, &s);
8423
8424 if (s.name != NULL)
8425 text = xstrprintf ("%s%s, ", text, s.name);
8426 else
8427 text = xstrprintf ("%s%d, ", text, iter);
8428
8429 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8430 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8431 on every call. */
8432 xfree (x);
8433 }
8434 /* Remove the last comma. */
8435 text[strlen (text) - 2] = '\0';
8436 ui_out_field_string (uiout, "what", text);
8437 }
8438 else
8439 ui_out_field_string (uiout, "what", "<any syscall>");
8440 ui_out_text (uiout, "\" ");
8ac3646f
TT
8441
8442 if (ui_out_is_mi_like_p (uiout))
8443 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8444}
8445
8446/* Implement the "print_mention" breakpoint_ops method for syscall
8447 catchpoints. */
8448
8449static void
8450print_mention_catch_syscall (struct breakpoint *b)
8451{
be5c67c1
PA
8452 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8453
8454 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8455 {
8456 int i, iter;
8457
be5c67c1 8458 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8459 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8460 else
8461 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8462
8463 for (i = 0;
be5c67c1 8464 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8465 i++)
8466 {
8467 struct syscall s;
8468 get_syscall_by_number (iter, &s);
8469
8470 if (s.name)
8471 printf_filtered (" '%s' [%d]", s.name, s.number);
8472 else
8473 printf_filtered (" %d", s.number);
8474 }
8475 printf_filtered (")");
8476 }
8477 else
8478 printf_filtered (_("Catchpoint %d (any syscall)"),
8479 b->number);
8480}
8481
6149aea9
PA
8482/* Implement the "print_recreate" breakpoint_ops method for syscall
8483 catchpoints. */
8484
8485static void
8486print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8487{
be5c67c1
PA
8488 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8489
6149aea9
PA
8490 fprintf_unfiltered (fp, "catch syscall");
8491
be5c67c1 8492 if (c->syscalls_to_be_caught)
6149aea9
PA
8493 {
8494 int i, iter;
8495
8496 for (i = 0;
be5c67c1 8497 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8498 i++)
8499 {
8500 struct syscall s;
8501
8502 get_syscall_by_number (iter, &s);
8503 if (s.name)
8504 fprintf_unfiltered (fp, " %s", s.name);
8505 else
8506 fprintf_unfiltered (fp, " %d", s.number);
8507 }
8508 }
d9b3f62e 8509 print_recreate_thread (b, fp);
6149aea9
PA
8510}
8511
a96d9b2e
SDJ
8512/* The breakpoint_ops structure to be used in syscall catchpoints. */
8513
2060206e 8514static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8515
8516/* Returns non-zero if 'b' is a syscall catchpoint. */
8517
8518static int
8519syscall_catchpoint_p (struct breakpoint *b)
8520{
8521 return (b->ops == &catch_syscall_breakpoint_ops);
8522}
8523
346774a9
PA
8524/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8525 is non-zero, then make the breakpoint temporary. If COND_STRING is
8526 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8527 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8528
ab04a2af 8529void
346774a9
PA
8530init_catchpoint (struct breakpoint *b,
8531 struct gdbarch *gdbarch, int tempflag,
8532 char *cond_string,
c0a91b2b 8533 const struct breakpoint_ops *ops)
c906108c 8534{
c5aa993b 8535 struct symtab_and_line sal;
346774a9 8536
fe39c653 8537 init_sal (&sal);
6c95b8df 8538 sal.pspace = current_program_space;
c5aa993b 8539
28010a5d 8540 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8541
1b36a34b 8542 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8543 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8544}
8545
28010a5d 8546void
3ea46bff 8547install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8548{
8549 add_to_breakpoint_chain (b);
3a5c3e22 8550 set_breakpoint_number (internal, b);
558a9d82
YQ
8551 if (is_tracepoint (b))
8552 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8553 if (!internal)
8554 mention (b);
c56053d2 8555 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8556
8557 if (update_gll)
8558 update_global_location_list (1);
c56053d2
PA
8559}
8560
9b70b993 8561static void
a6d9a66e
UW
8562create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8563 int tempflag, char *cond_string,
c0a91b2b 8564 const struct breakpoint_ops *ops)
c906108c 8565{
e29a4733 8566 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8567
e29a4733
PA
8568 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8569
8570 c->forked_inferior_pid = null_ptid;
8571
3ea46bff 8572 install_breakpoint (0, &c->base, 1);
c906108c
SS
8573}
8574
fe798b75
JB
8575/* Exec catchpoints. */
8576
b4d90040
PA
8577/* An instance of this type is used to represent an exec catchpoint.
8578 It includes a "struct breakpoint" as a kind of base class; users
8579 downcast to "struct breakpoint *" when needed. A breakpoint is
8580 really of this type iff its ops pointer points to
8581 CATCH_EXEC_BREAKPOINT_OPS. */
8582
8583struct exec_catchpoint
8584{
8585 /* The base class. */
8586 struct breakpoint base;
8587
8588 /* Filename of a program whose exec triggered this catchpoint.
8589 This field is only valid immediately after this catchpoint has
8590 triggered. */
8591 char *exec_pathname;
8592};
8593
8594/* Implement the "dtor" breakpoint_ops method for exec
8595 catchpoints. */
8596
8597static void
8598dtor_catch_exec (struct breakpoint *b)
8599{
8600 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8601
8602 xfree (c->exec_pathname);
348d480f 8603
2060206e 8604 base_breakpoint_ops.dtor (b);
b4d90040
PA
8605}
8606
77b06cd7
TJB
8607static int
8608insert_catch_exec (struct bp_location *bl)
c906108c 8609{
dfd4cc63 8610 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8611}
c906108c 8612
fe798b75 8613static int
77b06cd7 8614remove_catch_exec (struct bp_location *bl)
fe798b75 8615{
dfd4cc63 8616 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8617}
c906108c 8618
fe798b75 8619static int
f1310107 8620breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8621 struct address_space *aspace, CORE_ADDR bp_addr,
8622 const struct target_waitstatus *ws)
fe798b75 8623{
b4d90040
PA
8624 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8625
f90263c1
TT
8626 if (ws->kind != TARGET_WAITKIND_EXECD)
8627 return 0;
8628
8629 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8630 return 1;
fe798b75 8631}
c906108c 8632
fe798b75 8633static enum print_stop_action
348d480f 8634print_it_catch_exec (bpstat bs)
fe798b75 8635{
36dfb11c 8636 struct ui_out *uiout = current_uiout;
348d480f 8637 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8638 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8639
fe798b75 8640 annotate_catchpoint (b->number);
36dfb11c
TT
8641 if (b->disposition == disp_del)
8642 ui_out_text (uiout, "\nTemporary catchpoint ");
8643 else
8644 ui_out_text (uiout, "\nCatchpoint ");
8645 if (ui_out_is_mi_like_p (uiout))
8646 {
8647 ui_out_field_string (uiout, "reason",
8648 async_reason_lookup (EXEC_ASYNC_EXEC));
8649 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8650 }
8651 ui_out_field_int (uiout, "bkptno", b->number);
8652 ui_out_text (uiout, " (exec'd ");
8653 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8654 ui_out_text (uiout, "), ");
8655
fe798b75 8656 return PRINT_SRC_AND_LOC;
c906108c
SS
8657}
8658
fe798b75 8659static void
a6d9a66e 8660print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8661{
b4d90040 8662 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8663 struct value_print_options opts;
79a45e25 8664 struct ui_out *uiout = current_uiout;
fe798b75
JB
8665
8666 get_user_print_options (&opts);
8667
8668 /* Field 4, the address, is omitted (which makes the columns
8669 not line up too nicely with the headers, but the effect
8670 is relatively readable). */
8671 if (opts.addressprint)
8672 ui_out_field_skip (uiout, "addr");
8673 annotate_field (5);
8674 ui_out_text (uiout, "exec");
b4d90040 8675 if (c->exec_pathname != NULL)
fe798b75
JB
8676 {
8677 ui_out_text (uiout, ", program \"");
b4d90040 8678 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8679 ui_out_text (uiout, "\" ");
8680 }
8ac3646f
TT
8681
8682 if (ui_out_is_mi_like_p (uiout))
8683 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8684}
8685
8686static void
8687print_mention_catch_exec (struct breakpoint *b)
8688{
8689 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8690}
8691
6149aea9
PA
8692/* Implement the "print_recreate" breakpoint_ops method for exec
8693 catchpoints. */
8694
8695static void
8696print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8697{
8698 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8699 print_recreate_thread (b, fp);
6149aea9
PA
8700}
8701
2060206e 8702static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8703
a96d9b2e
SDJ
8704static void
8705create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 8706 const struct breakpoint_ops *ops)
a96d9b2e 8707{
be5c67c1 8708 struct syscall_catchpoint *c;
a96d9b2e 8709 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 8710
be5c67c1
PA
8711 c = XNEW (struct syscall_catchpoint);
8712 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8713 c->syscalls_to_be_caught = filter;
a96d9b2e 8714
3ea46bff 8715 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
8716}
8717
c906108c 8718static int
fba45db2 8719hw_breakpoint_used_count (void)
c906108c 8720{
c906108c 8721 int i = 0;
f1310107
TJB
8722 struct breakpoint *b;
8723 struct bp_location *bl;
c906108c
SS
8724
8725 ALL_BREAKPOINTS (b)
c5aa993b 8726 {
d6b74ac4 8727 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8728 for (bl = b->loc; bl; bl = bl->next)
8729 {
8730 /* Special types of hardware breakpoints may use more than
8731 one register. */
348d480f 8732 i += b->ops->resources_needed (bl);
f1310107 8733 }
c5aa993b 8734 }
c906108c
SS
8735
8736 return i;
8737}
8738
a1398e0c
PA
8739/* Returns the resources B would use if it were a hardware
8740 watchpoint. */
8741
c906108c 8742static int
a1398e0c 8743hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8744{
c906108c 8745 int i = 0;
e09342b5 8746 struct bp_location *bl;
c906108c 8747
a1398e0c
PA
8748 if (!breakpoint_enabled (b))
8749 return 0;
8750
8751 for (bl = b->loc; bl; bl = bl->next)
8752 {
8753 /* Special types of hardware watchpoints may use more than
8754 one register. */
8755 i += b->ops->resources_needed (bl);
8756 }
8757
8758 return i;
8759}
8760
8761/* Returns the sum the used resources of all hardware watchpoints of
8762 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8763 the sum of the used resources of all hardware watchpoints of other
8764 types _not_ TYPE. */
8765
8766static int
8767hw_watchpoint_used_count_others (struct breakpoint *except,
8768 enum bptype type, int *other_type_used)
8769{
8770 int i = 0;
8771 struct breakpoint *b;
8772
c906108c
SS
8773 *other_type_used = 0;
8774 ALL_BREAKPOINTS (b)
e09342b5 8775 {
a1398e0c
PA
8776 if (b == except)
8777 continue;
e09342b5
TJB
8778 if (!breakpoint_enabled (b))
8779 continue;
8780
a1398e0c
PA
8781 if (b->type == type)
8782 i += hw_watchpoint_use_count (b);
8783 else if (is_hardware_watchpoint (b))
8784 *other_type_used = 1;
e09342b5
TJB
8785 }
8786
c906108c
SS
8787 return i;
8788}
8789
c906108c 8790void
fba45db2 8791disable_watchpoints_before_interactive_call_start (void)
c906108c 8792{
c5aa993b 8793 struct breakpoint *b;
c906108c
SS
8794
8795 ALL_BREAKPOINTS (b)
c5aa993b 8796 {
cc60f2e3 8797 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8798 {
b5de0fa7 8799 b->enable_state = bp_call_disabled;
b60e7edf 8800 update_global_location_list (0);
c5aa993b
JM
8801 }
8802 }
c906108c
SS
8803}
8804
8805void
fba45db2 8806enable_watchpoints_after_interactive_call_stop (void)
c906108c 8807{
c5aa993b 8808 struct breakpoint *b;
c906108c
SS
8809
8810 ALL_BREAKPOINTS (b)
c5aa993b 8811 {
cc60f2e3 8812 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8813 {
b5de0fa7 8814 b->enable_state = bp_enabled;
b60e7edf 8815 update_global_location_list (1);
c5aa993b
JM
8816 }
8817 }
c906108c
SS
8818}
8819
8bea4e01
UW
8820void
8821disable_breakpoints_before_startup (void)
8822{
6c95b8df 8823 current_program_space->executing_startup = 1;
f8eba3c6 8824 update_global_location_list (0);
8bea4e01
UW
8825}
8826
8827void
8828enable_breakpoints_after_startup (void)
8829{
6c95b8df 8830 current_program_space->executing_startup = 0;
f8eba3c6 8831 breakpoint_re_set ();
8bea4e01
UW
8832}
8833
c906108c
SS
8834
8835/* Set a breakpoint that will evaporate an end of command
8836 at address specified by SAL.
8837 Restrict it to frame FRAME if FRAME is nonzero. */
8838
8839struct breakpoint *
a6d9a66e
UW
8840set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8841 struct frame_id frame_id, enum bptype type)
c906108c 8842{
52f0bd74 8843 struct breakpoint *b;
edb3359d 8844
193facb3
JK
8845 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8846 tail-called one. */
8847 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8848
06edf0c0 8849 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8850 b->enable_state = bp_enabled;
8851 b->disposition = disp_donttouch;
818dd999 8852 b->frame_id = frame_id;
c906108c 8853
4a64f543
MS
8854 /* If we're debugging a multi-threaded program, then we want
8855 momentary breakpoints to be active in only a single thread of
8856 control. */
39f77062
KB
8857 if (in_thread_list (inferior_ptid))
8858 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8859
b60e7edf 8860 update_global_location_list_nothrow (1);
74960c60 8861
c906108c
SS
8862 return b;
8863}
611c83ae 8864
06edf0c0
PA
8865/* Make a momentary breakpoint based on the master breakpoint ORIG.
8866 The new breakpoint will have type TYPE, and use OPS as it
8867 breakpoint_ops. */
e58b0e63 8868
06edf0c0
PA
8869static struct breakpoint *
8870momentary_breakpoint_from_master (struct breakpoint *orig,
8871 enum bptype type,
c0a91b2b 8872 const struct breakpoint_ops *ops)
e58b0e63
PA
8873{
8874 struct breakpoint *copy;
8875
06edf0c0 8876 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8877 copy->loc = allocate_bp_location (copy);
0e30163f 8878 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8879
a6d9a66e 8880 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8881 copy->loc->requested_address = orig->loc->requested_address;
8882 copy->loc->address = orig->loc->address;
8883 copy->loc->section = orig->loc->section;
6c95b8df 8884 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8885 copy->loc->probe = orig->loc->probe;
f8eba3c6 8886 copy->loc->line_number = orig->loc->line_number;
2f202fde 8887 copy->loc->symtab = orig->loc->symtab;
e58b0e63
PA
8888 copy->frame_id = orig->frame_id;
8889 copy->thread = orig->thread;
6c95b8df 8890 copy->pspace = orig->pspace;
e58b0e63
PA
8891
8892 copy->enable_state = bp_enabled;
8893 copy->disposition = disp_donttouch;
8894 copy->number = internal_breakpoint_number--;
8895
8896 update_global_location_list_nothrow (0);
8897 return copy;
8898}
8899
06edf0c0
PA
8900/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8901 ORIG is NULL. */
8902
8903struct breakpoint *
8904clone_momentary_breakpoint (struct breakpoint *orig)
8905{
8906 /* If there's nothing to clone, then return nothing. */
8907 if (orig == NULL)
8908 return NULL;
8909
8910 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8911}
8912
611c83ae 8913struct breakpoint *
a6d9a66e
UW
8914set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8915 enum bptype type)
611c83ae
PA
8916{
8917 struct symtab_and_line sal;
8918
8919 sal = find_pc_line (pc, 0);
8920 sal.pc = pc;
8921 sal.section = find_pc_overlay (pc);
8922 sal.explicit_pc = 1;
8923
a6d9a66e 8924 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8925}
c906108c 8926\f
c5aa993b 8927
c906108c
SS
8928/* Tell the user we have just set a breakpoint B. */
8929
8930static void
fba45db2 8931mention (struct breakpoint *b)
c906108c 8932{
348d480f 8933 b->ops->print_mention (b);
79a45e25 8934 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8935 return;
c906108c
SS
8936 printf_filtered ("\n");
8937}
c906108c 8938\f
c5aa993b 8939
0d381245 8940static struct bp_location *
39d61571 8941add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8942 const struct symtab_and_line *sal)
8943{
8944 struct bp_location *loc, **tmp;
3742cc8b
YQ
8945 CORE_ADDR adjusted_address;
8946 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8947
8948 if (loc_gdbarch == NULL)
8949 loc_gdbarch = b->gdbarch;
8950
8951 /* Adjust the breakpoint's address prior to allocating a location.
8952 Once we call allocate_bp_location(), that mostly uninitialized
8953 location will be placed on the location chain. Adjustment of the
8954 breakpoint may cause target_read_memory() to be called and we do
8955 not want its scan of the location chain to find a breakpoint and
8956 location that's only been partially initialized. */
8957 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8958 sal->pc, b->type);
0d381245 8959
d30113d4 8960 /* Sort the locations by their ADDRESS. */
39d61571 8961 loc = allocate_bp_location (b);
d30113d4
JK
8962 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8963 tmp = &((*tmp)->next))
0d381245 8964 ;
d30113d4 8965 loc->next = *tmp;
0d381245 8966 *tmp = loc;
3742cc8b 8967
0d381245 8968 loc->requested_address = sal->pc;
3742cc8b 8969 loc->address = adjusted_address;
6c95b8df 8970 loc->pspace = sal->pspace;
55aa24fb 8971 loc->probe = sal->probe;
6c95b8df 8972 gdb_assert (loc->pspace != NULL);
0d381245 8973 loc->section = sal->section;
3742cc8b 8974 loc->gdbarch = loc_gdbarch;
f8eba3c6 8975 loc->line_number = sal->line;
2f202fde 8976 loc->symtab = sal->symtab;
f8eba3c6 8977
0e30163f
JK
8978 set_breakpoint_location_function (loc,
8979 sal->explicit_pc || sal->explicit_line);
0d381245
VP
8980 return loc;
8981}
514f746b
AR
8982\f
8983
8984/* Return 1 if LOC is pointing to a permanent breakpoint,
8985 return 0 otherwise. */
8986
8987static int
8988bp_loc_is_permanent (struct bp_location *loc)
8989{
8990 int len;
8991 CORE_ADDR addr;
1afeeb75 8992 const gdb_byte *bpoint;
514f746b 8993 gdb_byte *target_mem;
939c61fa
JK
8994 struct cleanup *cleanup;
8995 int retval = 0;
514f746b
AR
8996
8997 gdb_assert (loc != NULL);
8998
8999 addr = loc->address;
1afeeb75 9000 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 9001
939c61fa 9002 /* Software breakpoints unsupported? */
1afeeb75 9003 if (bpoint == NULL)
939c61fa
JK
9004 return 0;
9005
514f746b
AR
9006 target_mem = alloca (len);
9007
939c61fa
JK
9008 /* Enable the automatic memory restoration from breakpoints while
9009 we read the memory. Otherwise we could say about our temporary
9010 breakpoints they are permanent. */
6c95b8df
PA
9011 cleanup = save_current_space_and_thread ();
9012
9013 switch_to_program_space_and_thread (loc->pspace);
9014 make_show_memory_breakpoints_cleanup (0);
939c61fa 9015
514f746b 9016 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 9017 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 9018 retval = 1;
514f746b 9019
939c61fa
JK
9020 do_cleanups (cleanup);
9021
9022 return retval;
514f746b
AR
9023}
9024
e7e0cddf
SS
9025/* Build a command list for the dprintf corresponding to the current
9026 settings of the dprintf style options. */
9027
9028static void
9029update_dprintf_command_list (struct breakpoint *b)
9030{
9031 char *dprintf_args = b->extra_string;
9032 char *printf_line = NULL;
9033
9034 if (!dprintf_args)
9035 return;
9036
9037 dprintf_args = skip_spaces (dprintf_args);
9038
9039 /* Allow a comma, as it may have terminated a location, but don't
9040 insist on it. */
9041 if (*dprintf_args == ',')
9042 ++dprintf_args;
9043 dprintf_args = skip_spaces (dprintf_args);
9044
9045 if (*dprintf_args != '"')
9046 error (_("Bad format string, missing '\"'."));
9047
d3ce09f5 9048 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9049 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9050 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9051 {
9052 if (!dprintf_function)
9053 error (_("No function supplied for dprintf call"));
9054
9055 if (dprintf_channel && strlen (dprintf_channel) > 0)
9056 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9057 dprintf_function,
9058 dprintf_channel,
9059 dprintf_args);
9060 else
9061 printf_line = xstrprintf ("call (void) %s (%s)",
9062 dprintf_function,
9063 dprintf_args);
9064 }
d3ce09f5
SS
9065 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9066 {
9067 if (target_can_run_breakpoint_commands ())
9068 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9069 else
9070 {
9071 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9072 printf_line = xstrprintf ("printf %s", dprintf_args);
9073 }
9074 }
e7e0cddf
SS
9075 else
9076 internal_error (__FILE__, __LINE__,
9077 _("Invalid dprintf style."));
9078
f28045c2 9079 gdb_assert (printf_line != NULL);
9d6e6e84 9080 /* Manufacture a printf sequence. */
f28045c2 9081 {
9d6e6e84
HZ
9082 struct command_line *printf_cmd_line
9083 = xmalloc (sizeof (struct command_line));
e7e0cddf 9084
f28045c2
YQ
9085 printf_cmd_line = xmalloc (sizeof (struct command_line));
9086 printf_cmd_line->control_type = simple_control;
9087 printf_cmd_line->body_count = 0;
9088 printf_cmd_line->body_list = NULL;
9d6e6e84 9089 printf_cmd_line->next = NULL;
f28045c2 9090 printf_cmd_line->line = printf_line;
e7e0cddf 9091
f28045c2
YQ
9092 breakpoint_set_commands (b, printf_cmd_line);
9093 }
e7e0cddf
SS
9094}
9095
9096/* Update all dprintf commands, making their command lists reflect
9097 current style settings. */
9098
9099static void
9100update_dprintf_commands (char *args, int from_tty,
9101 struct cmd_list_element *c)
9102{
9103 struct breakpoint *b;
9104
9105 ALL_BREAKPOINTS (b)
9106 {
9107 if (b->type == bp_dprintf)
9108 update_dprintf_command_list (b);
9109 }
9110}
c3f6f71d 9111
018d34a4
VP
9112/* Create a breakpoint with SAL as location. Use ADDR_STRING
9113 as textual description of the location, and COND_STRING
db107f19 9114 as condition expression. */
018d34a4
VP
9115
9116static void
d9b3f62e
PA
9117init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9118 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9119 char *filter, char *cond_string,
e7e0cddf 9120 char *extra_string,
d9b3f62e
PA
9121 enum bptype type, enum bpdisp disposition,
9122 int thread, int task, int ignore_count,
c0a91b2b 9123 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9124 int enabled, int internal, unsigned flags,
9125 int display_canonical)
018d34a4 9126{
0d381245 9127 int i;
018d34a4
VP
9128
9129 if (type == bp_hardware_breakpoint)
9130 {
fbbd034e
AS
9131 int target_resources_ok;
9132
9133 i = hw_breakpoint_used_count ();
9134 target_resources_ok =
9135 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9136 i + 1, 0);
9137 if (target_resources_ok == 0)
9138 error (_("No hardware breakpoint support in the target."));
9139 else if (target_resources_ok < 0)
9140 error (_("Hardware breakpoints used exceeds limit."));
9141 }
9142
6c95b8df
PA
9143 gdb_assert (sals.nelts > 0);
9144
0d381245
VP
9145 for (i = 0; i < sals.nelts; ++i)
9146 {
9147 struct symtab_and_line sal = sals.sals[i];
9148 struct bp_location *loc;
9149
9150 if (from_tty)
5af949e3
UW
9151 {
9152 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9153 if (!loc_gdbarch)
9154 loc_gdbarch = gdbarch;
9155
9156 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9157 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9158 }
0d381245
VP
9159
9160 if (i == 0)
9161 {
d9b3f62e 9162 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9163 b->thread = thread;
4a306c9a 9164 b->task = task;
855a6e68 9165
0d381245 9166 b->cond_string = cond_string;
e7e0cddf 9167 b->extra_string = extra_string;
0d381245 9168 b->ignore_count = ignore_count;
41447f92 9169 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9170 b->disposition = disposition;
6c95b8df 9171
44f238bb
PA
9172 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9173 b->loc->inserted = 1;
9174
0fb4aa4b
PA
9175 if (type == bp_static_tracepoint)
9176 {
d9b3f62e 9177 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9178 struct static_tracepoint_marker marker;
9179
983af33b 9180 if (strace_marker_p (b))
0fb4aa4b
PA
9181 {
9182 /* We already know the marker exists, otherwise, we
9183 wouldn't see a sal for it. */
9184 char *p = &addr_string[3];
9185 char *endp;
9186 char *marker_str;
0fb4aa4b 9187
e9cafbcc 9188 p = skip_spaces (p);
0fb4aa4b 9189
e9cafbcc 9190 endp = skip_to_space (p);
0fb4aa4b
PA
9191
9192 marker_str = savestring (p, endp - p);
d9b3f62e 9193 t->static_trace_marker_id = marker_str;
0fb4aa4b 9194
3e43a32a
MS
9195 printf_filtered (_("Probed static tracepoint "
9196 "marker \"%s\"\n"),
d9b3f62e 9197 t->static_trace_marker_id);
0fb4aa4b
PA
9198 }
9199 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9200 {
d9b3f62e 9201 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9202 release_static_tracepoint_marker (&marker);
9203
3e43a32a
MS
9204 printf_filtered (_("Probed static tracepoint "
9205 "marker \"%s\"\n"),
d9b3f62e 9206 t->static_trace_marker_id);
0fb4aa4b
PA
9207 }
9208 else
3e43a32a
MS
9209 warning (_("Couldn't determine the static "
9210 "tracepoint marker to probe"));
0fb4aa4b
PA
9211 }
9212
0d381245
VP
9213 loc = b->loc;
9214 }
9215 else
018d34a4 9216 {
39d61571 9217 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9218 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9219 loc->inserted = 1;
0d381245
VP
9220 }
9221
514f746b
AR
9222 if (bp_loc_is_permanent (loc))
9223 make_breakpoint_permanent (b);
9224
0d381245
VP
9225 if (b->cond_string)
9226 {
bbc13ae3
KS
9227 const char *arg = b->cond_string;
9228
1bb9788d
TT
9229 loc->cond = parse_exp_1 (&arg, loc->address,
9230 block_for_pc (loc->address), 0);
0d381245 9231 if (*arg)
588ae58c 9232 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9233 }
e7e0cddf
SS
9234
9235 /* Dynamic printf requires and uses additional arguments on the
9236 command line, otherwise it's an error. */
9237 if (type == bp_dprintf)
9238 {
9239 if (b->extra_string)
9240 update_dprintf_command_list (b);
9241 else
9242 error (_("Format string required"));
9243 }
9244 else if (b->extra_string)
588ae58c 9245 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9246 }
018d34a4 9247
56435ebe 9248 b->display_canonical = display_canonical;
018d34a4
VP
9249 if (addr_string)
9250 b->addr_string = addr_string;
9251 else
9252 /* addr_string has to be used or breakpoint_re_set will delete
9253 me. */
5af949e3
UW
9254 b->addr_string
9255 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9256 b->filter = filter;
d9b3f62e 9257}
018d34a4 9258
d9b3f62e
PA
9259static void
9260create_breakpoint_sal (struct gdbarch *gdbarch,
9261 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9262 char *filter, char *cond_string,
e7e0cddf 9263 char *extra_string,
d9b3f62e
PA
9264 enum bptype type, enum bpdisp disposition,
9265 int thread, int task, int ignore_count,
c0a91b2b 9266 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9267 int enabled, int internal, unsigned flags,
9268 int display_canonical)
d9b3f62e
PA
9269{
9270 struct breakpoint *b;
9271 struct cleanup *old_chain;
9272
9273 if (is_tracepoint_type (type))
9274 {
9275 struct tracepoint *t;
9276
9277 t = XCNEW (struct tracepoint);
9278 b = &t->base;
9279 }
9280 else
9281 b = XNEW (struct breakpoint);
9282
9283 old_chain = make_cleanup (xfree, b);
9284
9285 init_breakpoint_sal (b, gdbarch,
9286 sals, addr_string,
e7e0cddf 9287 filter, cond_string, extra_string,
d9b3f62e
PA
9288 type, disposition,
9289 thread, task, ignore_count,
9290 ops, from_tty,
44f238bb
PA
9291 enabled, internal, flags,
9292 display_canonical);
d9b3f62e
PA
9293 discard_cleanups (old_chain);
9294
3ea46bff 9295 install_breakpoint (internal, b, 0);
018d34a4
VP
9296}
9297
9298/* Add SALS.nelts breakpoints to the breakpoint table. For each
9299 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9300 value. COND_STRING, if not NULL, specified the condition to be
9301 used for all breakpoints. Essentially the only case where
9302 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9303 function. In that case, it's still not possible to specify
9304 separate conditions for different overloaded functions, so
9305 we take just a single condition string.
9306
c3f6f71d 9307 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9308 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9309 array contents). If the function fails (error() is called), the
9310 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9311 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9312
9313static void
8cdf0e15 9314create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9315 struct linespec_result *canonical,
e7e0cddf 9316 char *cond_string, char *extra_string,
8cdf0e15
VP
9317 enum bptype type, enum bpdisp disposition,
9318 int thread, int task, int ignore_count,
c0a91b2b 9319 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9320 int enabled, int internal, unsigned flags)
c906108c 9321{
018d34a4 9322 int i;
f8eba3c6 9323 struct linespec_sals *lsal;
cc59ec59 9324
f8eba3c6
TT
9325 if (canonical->pre_expanded)
9326 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9327
9328 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9329 {
f8eba3c6
TT
9330 /* Note that 'addr_string' can be NULL in the case of a plain
9331 'break', without arguments. */
9332 char *addr_string = (canonical->addr_string
9333 ? xstrdup (canonical->addr_string)
9334 : NULL);
9335 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9336 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9337
f8eba3c6
TT
9338 make_cleanup (xfree, filter_string);
9339 create_breakpoint_sal (gdbarch, lsal->sals,
9340 addr_string,
9341 filter_string,
e7e0cddf
SS
9342 cond_string, extra_string,
9343 type, disposition,
84f4c1fe 9344 thread, task, ignore_count, ops,
44f238bb 9345 from_tty, enabled, internal, flags,
56435ebe 9346 canonical->special_display);
f8eba3c6 9347 discard_cleanups (inner);
c3f6f71d 9348 }
c3f6f71d 9349}
c906108c 9350
9998af43 9351/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9352 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9353 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9354 address strings. ADDRESS points to the end of the SAL.
9355
9356 The array and the line spec strings are allocated on the heap, it is
9357 the caller's responsibility to free them. */
c906108c 9358
b9362cc7 9359static void
c3f6f71d 9360parse_breakpoint_sals (char **address,
58438ac1 9361 struct linespec_result *canonical)
c3f6f71d 9362{
c3f6f71d 9363 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9364 breakpoint. */
c3f6f71d
JM
9365 if ((*address) == NULL
9366 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9367 {
1bfeeb0f
JL
9368 /* The last displayed codepoint, if it's valid, is our default breakpoint
9369 address. */
9370 if (last_displayed_sal_is_valid ())
c906108c 9371 {
f8eba3c6 9372 struct linespec_sals lsal;
c3f6f71d 9373 struct symtab_and_line sal;
1c8cdcb1 9374 CORE_ADDR pc;
cc59ec59 9375
4a64f543 9376 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9377 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9378 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9379
9380 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9381 corresponding to the last call to print_frame_info.
9382 Be sure to reinitialize LINE with NOTCURRENT == 0
9383 as the breakpoint line number is inappropriate otherwise.
9384 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9385 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9386 pc = sal.pc;
9387 sal = find_pc_line (pc, 0);
00903456 9388
4a64f543 9389 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9390 where PC is the last displayed codepoint's address. So
9391 make sure to set sal.explicit_pc to prevent GDB from
9392 trying to expand the list of sals to include all other
9393 instances with the same symtab and line. */
1c8cdcb1 9394 sal.pc = pc;
00903456
JK
9395 sal.explicit_pc = 1;
9396
f8eba3c6
TT
9397 lsal.sals.sals[0] = sal;
9398 lsal.sals.nelts = 1;
9399 lsal.canonical = NULL;
9400
9401 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9402 }
9403 else
8a3fe4f8 9404 error (_("No default breakpoint address now."));
c906108c
SS
9405 }
9406 else
9407 {
cc80f267
JK
9408 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9409
c906108c 9410 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9411 current_source_symtab (which is decode_line_1's default).
9412 This should produce the results we want almost all of the
cc80f267
JK
9413 time while leaving default_breakpoint_* alone.
9414
9415 ObjC: However, don't match an Objective-C method name which
9416 may have a '+' or '-' succeeded by a '['. */
9417 if (last_displayed_sal_is_valid ()
9418 && (!cursal.symtab
9419 || ((strchr ("+-", (*address)[0]) != NULL)
9420 && ((*address)[1] != '['))))
f8eba3c6
TT
9421 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9422 get_last_displayed_symtab (),
9423 get_last_displayed_line (),
9424 canonical, NULL, NULL);
c906108c 9425 else
f8eba3c6 9426 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9427 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9428 }
c3f6f71d 9429}
c906108c 9430
c906108c 9431
c3f6f71d 9432/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9433 inserted as a breakpoint. If it can't throw an error. */
c906108c 9434
b9362cc7 9435static void
23e7acfb 9436breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9437{
9438 int i;
cc59ec59 9439
c3f6f71d 9440 for (i = 0; i < sals->nelts; i++)
ee53e872 9441 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9442}
9443
7a697b8d
SS
9444/* Fast tracepoints may have restrictions on valid locations. For
9445 instance, a fast tracepoint using a jump instead of a trap will
9446 likely have to overwrite more bytes than a trap would, and so can
9447 only be placed where the instruction is longer than the jump, or a
9448 multi-instruction sequence does not have a jump into the middle of
9449 it, etc. */
9450
9451static void
9452check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9453 struct symtabs_and_lines *sals)
9454{
9455 int i, rslt;
9456 struct symtab_and_line *sal;
9457 char *msg;
9458 struct cleanup *old_chain;
9459
9460 for (i = 0; i < sals->nelts; i++)
9461 {
f8eba3c6
TT
9462 struct gdbarch *sarch;
9463
7a697b8d
SS
9464 sal = &sals->sals[i];
9465
f8eba3c6
TT
9466 sarch = get_sal_arch (*sal);
9467 /* We fall back to GDBARCH if there is no architecture
9468 associated with SAL. */
9469 if (sarch == NULL)
9470 sarch = gdbarch;
9471 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9472 NULL, &msg);
9473 old_chain = make_cleanup (xfree, msg);
9474
9475 if (!rslt)
9476 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9477 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9478
9479 do_cleanups (old_chain);
9480 }
9481}
9482
af4908ba
KS
9483/* Issue an invalid thread ID error. */
9484
9485static void ATTRIBUTE_NORETURN
9486invalid_thread_id_error (int id)
9487{
9488 error (_("Unknown thread %d."), id);
9489}
9490
018d34a4
VP
9491/* Given TOK, a string specification of condition and thread, as
9492 accepted by the 'break' command, extract the condition
9493 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9494 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9495 If no condition is found, *COND_STRING is set to NULL.
9496 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9497
9498static void
bbc13ae3 9499find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9500 char **cond_string, int *thread, int *task,
9501 char **rest)
018d34a4
VP
9502{
9503 *cond_string = NULL;
9504 *thread = -1;
ed1d1739
KS
9505 *task = 0;
9506 *rest = NULL;
9507
018d34a4
VP
9508 while (tok && *tok)
9509 {
bbc13ae3 9510 const char *end_tok;
018d34a4 9511 int toklen;
bbc13ae3
KS
9512 const char *cond_start = NULL;
9513 const char *cond_end = NULL;
cc59ec59 9514
bbc13ae3 9515 tok = skip_spaces_const (tok);
e7e0cddf
SS
9516
9517 if ((*tok == '"' || *tok == ',') && rest)
9518 {
9519 *rest = savestring (tok, strlen (tok));
9520 return;
9521 }
9522
bbc13ae3 9523 end_tok = skip_to_space_const (tok);
d634f2de 9524
018d34a4 9525 toklen = end_tok - tok;
d634f2de 9526
018d34a4
VP
9527 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9528 {
f7545552
TT
9529 struct expression *expr;
9530
018d34a4 9531 tok = cond_start = end_tok + 1;
1bb9788d 9532 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9533 xfree (expr);
018d34a4 9534 cond_end = tok;
d634f2de 9535 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9536 }
9537 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9538 {
9539 char *tmptok;
d634f2de 9540
018d34a4 9541 tok = end_tok + 1;
bbc13ae3 9542 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9543 if (tok == tmptok)
9544 error (_("Junk after thread keyword."));
9545 if (!valid_thread_id (*thread))
af4908ba 9546 invalid_thread_id_error (*thread);
bbc13ae3 9547 tok = tmptok;
018d34a4 9548 }
4a306c9a
JB
9549 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9550 {
9551 char *tmptok;
9552
9553 tok = end_tok + 1;
bbc13ae3 9554 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9555 if (tok == tmptok)
9556 error (_("Junk after task keyword."));
9557 if (!valid_task_id (*task))
b6199126 9558 error (_("Unknown task %d."), *task);
bbc13ae3 9559 tok = tmptok;
4a306c9a 9560 }
e7e0cddf
SS
9561 else if (rest)
9562 {
9563 *rest = savestring (tok, strlen (tok));
ccab2054 9564 return;
e7e0cddf 9565 }
018d34a4
VP
9566 else
9567 error (_("Junk at end of arguments."));
9568 }
9569}
9570
0fb4aa4b
PA
9571/* Decode a static tracepoint marker spec. */
9572
9573static struct symtabs_and_lines
9574decode_static_tracepoint_spec (char **arg_p)
9575{
9576 VEC(static_tracepoint_marker_p) *markers = NULL;
9577 struct symtabs_and_lines sals;
0fb4aa4b
PA
9578 struct cleanup *old_chain;
9579 char *p = &(*arg_p)[3];
9580 char *endp;
9581 char *marker_str;
9582 int i;
9583
e9cafbcc 9584 p = skip_spaces (p);
0fb4aa4b 9585
e9cafbcc 9586 endp = skip_to_space (p);
0fb4aa4b
PA
9587
9588 marker_str = savestring (p, endp - p);
9589 old_chain = make_cleanup (xfree, marker_str);
9590
9591 markers = target_static_tracepoint_markers_by_strid (marker_str);
9592 if (VEC_empty(static_tracepoint_marker_p, markers))
9593 error (_("No known static tracepoint marker named %s"), marker_str);
9594
9595 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9596 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9597
9598 for (i = 0; i < sals.nelts; i++)
9599 {
9600 struct static_tracepoint_marker *marker;
9601
9602 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9603
9604 init_sal (&sals.sals[i]);
9605
9606 sals.sals[i] = find_pc_line (marker->address, 0);
9607 sals.sals[i].pc = marker->address;
9608
9609 release_static_tracepoint_marker (marker);
9610 }
9611
9612 do_cleanups (old_chain);
9613
9614 *arg_p = endp;
9615 return sals;
9616}
9617
fd9b8c24
PA
9618/* Set a breakpoint. This function is shared between CLI and MI
9619 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9620 modes of operations, selected by the PARSE_ARG parameter. If
9621 non-zero, the function will parse ARG, extracting location,
9622 condition, thread and extra string. Otherwise, ARG is just the
9623 breakpoint's location, with condition, thread, and extra string
9624 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9625 If INTERNAL is non-zero, the breakpoint number will be allocated
9626 from the internal breakpoint count. Returns true if any breakpoint
9627 was created; false otherwise. */
0101ce28 9628
8cdf0e15
VP
9629int
9630create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9631 char *arg, char *cond_string,
9632 int thread, char *extra_string,
f6de8ec2 9633 int parse_arg,
0fb4aa4b 9634 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9635 int ignore_count,
9636 enum auto_boolean pending_break_support,
c0a91b2b 9637 const struct breakpoint_ops *ops,
44f238bb
PA
9638 int from_tty, int enabled, int internal,
9639 unsigned flags)
c3f6f71d 9640{
b78a6381 9641 volatile struct gdb_exception e;
f8eba3c6 9642 char *copy_arg = NULL;
c3f6f71d 9643 char *addr_start = arg;
7efd8fc2 9644 struct linespec_result canonical;
c3f6f71d 9645 struct cleanup *old_chain;
80c99de1 9646 struct cleanup *bkpt_chain = NULL;
0101ce28 9647 int pending = 0;
4a306c9a 9648 int task = 0;
86b17b60 9649 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9650
348d480f
PA
9651 gdb_assert (ops != NULL);
9652
7efd8fc2 9653 init_linespec_result (&canonical);
c3f6f71d 9654
b78a6381
TT
9655 TRY_CATCH (e, RETURN_MASK_ALL)
9656 {
983af33b
SDJ
9657 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9658 addr_start, &copy_arg);
b78a6381 9659 }
0101ce28
JJ
9660
9661 /* If caller is interested in rc value from parse, set value. */
05ff989b 9662 switch (e.reason)
0101ce28 9663 {
983af33b
SDJ
9664 case GDB_NO_ERROR:
9665 if (VEC_empty (linespec_sals, canonical.sals))
9666 return 0;
9667 break;
05ff989b
AC
9668 case RETURN_ERROR:
9669 switch (e.error)
0101ce28 9670 {
05ff989b 9671 case NOT_FOUND_ERROR:
0101ce28 9672
05ff989b
AC
9673 /* If pending breakpoint support is turned off, throw
9674 error. */
fa8d40ab
JJ
9675
9676 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9677 throw_exception (e);
9678
9679 exception_print (gdb_stderr, e);
fa8d40ab 9680
05ff989b
AC
9681 /* If pending breakpoint support is auto query and the user
9682 selects no, then simply return the error code. */
059fb39f 9683 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9684 && !nquery (_("Make %s pending on future shared library load? "),
9685 bptype_string (type_wanted)))
fd9b8c24 9686 return 0;
fa8d40ab 9687
05ff989b
AC
9688 /* At this point, either the user was queried about setting
9689 a pending breakpoint and selected yes, or pending
9690 breakpoint behavior is on and thus a pending breakpoint
9691 is defaulted on behalf of the user. */
f8eba3c6
TT
9692 {
9693 struct linespec_sals lsal;
9694
9695 copy_arg = xstrdup (addr_start);
9696 lsal.canonical = xstrdup (copy_arg);
9697 lsal.sals.nelts = 1;
9698 lsal.sals.sals = XNEW (struct symtab_and_line);
9699 init_sal (&lsal.sals.sals[0]);
9700 pending = 1;
9701 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9702 }
05ff989b
AC
9703 break;
9704 default:
98deb0da 9705 throw_exception (e);
0101ce28 9706 }
2abae994 9707 break;
05ff989b 9708 default:
983af33b 9709 throw_exception (e);
0101ce28 9710 }
c3f6f71d 9711
4a64f543 9712 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9713 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9714
c3f6f71d
JM
9715 /* ----------------------------- SNIP -----------------------------
9716 Anything added to the cleanup chain beyond this point is assumed
9717 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9718 then the memory is not reclaimed. */
9719 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9720
c3f6f71d
JM
9721 /* Resolve all line numbers to PC's and verify that the addresses
9722 are ok for the target. */
0101ce28 9723 if (!pending)
f8eba3c6
TT
9724 {
9725 int ix;
9726 struct linespec_sals *iter;
9727
9728 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9729 breakpoint_sals_to_pc (&iter->sals);
9730 }
c3f6f71d 9731
7a697b8d 9732 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9733 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9734 {
9735 int ix;
9736 struct linespec_sals *iter;
9737
9738 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9739 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9740 }
7a697b8d 9741
c3f6f71d
JM
9742 /* Verify that condition can be parsed, before setting any
9743 breakpoints. Allocate a separate condition expression for each
4a64f543 9744 breakpoint. */
0101ce28 9745 if (!pending)
c3f6f71d 9746 {
f6de8ec2 9747 if (parse_arg)
72b2ff0e 9748 {
0878d0fa 9749 char *rest;
52d361e1
YQ
9750 struct linespec_sals *lsal;
9751
9752 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9753
0878d0fa
YQ
9754 /* Here we only parse 'arg' to separate condition
9755 from thread number, so parsing in context of first
9756 sal is OK. When setting the breakpoint we'll
9757 re-parse it in context of each sal. */
9758
9759 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9760 &thread, &task, &rest);
9761 if (cond_string)
9762 make_cleanup (xfree, cond_string);
9763 if (rest)
9764 make_cleanup (xfree, rest);
9765 if (rest)
9766 extra_string = rest;
72b2ff0e 9767 }
2f069f6f 9768 else
72b2ff0e 9769 {
0878d0fa
YQ
9770 if (*arg != '\0')
9771 error (_("Garbage '%s' at end of location"), arg);
9772
9773 /* Create a private copy of condition string. */
9774 if (cond_string)
9775 {
9776 cond_string = xstrdup (cond_string);
9777 make_cleanup (xfree, cond_string);
9778 }
9779 /* Create a private copy of any extra string. */
9780 if (extra_string)
9781 {
9782 extra_string = xstrdup (extra_string);
9783 make_cleanup (xfree, extra_string);
9784 }
72b2ff0e 9785 }
0fb4aa4b 9786
52d361e1 9787 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9788 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9789 tempflag ? disp_del : disp_donttouch,
9790 thread, task, ignore_count, ops,
44f238bb 9791 from_tty, enabled, internal, flags);
c906108c 9792 }
0101ce28
JJ
9793 else
9794 {
0101ce28
JJ
9795 struct breakpoint *b;
9796
0101ce28
JJ
9797 make_cleanup (xfree, copy_arg);
9798
bfccc43c
YQ
9799 if (is_tracepoint_type (type_wanted))
9800 {
9801 struct tracepoint *t;
9802
9803 t = XCNEW (struct tracepoint);
9804 b = &t->base;
9805 }
9806 else
9807 b = XNEW (struct breakpoint);
9808
9809 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9810
f8eba3c6 9811 b->addr_string = copy_arg;
f6de8ec2 9812 if (parse_arg)
e12c7713
MK
9813 b->cond_string = NULL;
9814 else
9815 {
9816 /* Create a private copy of condition string. */
9817 if (cond_string)
9818 {
9819 cond_string = xstrdup (cond_string);
9820 make_cleanup (xfree, cond_string);
9821 }
9822 b->cond_string = cond_string;
9823 }
e7e0cddf 9824 b->extra_string = NULL;
0101ce28 9825 b->ignore_count = ignore_count;
0101ce28 9826 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9827 b->condition_not_parsed = 1;
41447f92 9828 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9829 if ((type_wanted != bp_breakpoint
9830 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9831 b->pspace = current_program_space;
8bea4e01 9832
bfccc43c 9833 install_breakpoint (internal, b, 0);
0101ce28
JJ
9834 }
9835
f8eba3c6 9836 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9837 {
3e43a32a
MS
9838 warning (_("Multiple breakpoints were set.\nUse the "
9839 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9840 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9841 }
9842
80c99de1
PA
9843 /* That's it. Discard the cleanups for data inserted into the
9844 breakpoint. */
9845 discard_cleanups (bkpt_chain);
9846 /* But cleanup everything else. */
c3f6f71d 9847 do_cleanups (old_chain);
217dc9e2 9848
80c99de1 9849 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 9850 update_global_location_list (1);
fd9b8c24
PA
9851
9852 return 1;
c3f6f71d 9853}
c906108c 9854
348d480f 9855/* Set a breakpoint.
72b2ff0e
VP
9856 ARG is a string describing breakpoint address,
9857 condition, and thread.
9858 FLAG specifies if a breakpoint is hardware on,
9859 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9860 and BP_TEMPFLAG. */
348d480f 9861
98deb0da 9862static void
72b2ff0e 9863break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9864{
72b2ff0e 9865 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9866 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9867 ? bp_hardware_breakpoint
9868 : bp_breakpoint);
55aa24fb
SDJ
9869 struct breakpoint_ops *ops;
9870 const char *arg_cp = arg;
9871
9872 /* Matching breakpoints on probes. */
9873 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9874 ops = &bkpt_probe_breakpoint_ops;
9875 else
9876 ops = &bkpt_breakpoint_ops;
c3f6f71d 9877
8cdf0e15
VP
9878 create_breakpoint (get_current_arch (),
9879 arg,
e7e0cddf 9880 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 9881 tempflag, type_wanted,
8cdf0e15
VP
9882 0 /* Ignore count */,
9883 pending_break_support,
55aa24fb 9884 ops,
8cdf0e15 9885 from_tty,
84f4c1fe 9886 1 /* enabled */,
44f238bb
PA
9887 0 /* internal */,
9888 0);
c906108c
SS
9889}
9890
c906108c
SS
9891/* Helper function for break_command_1 and disassemble_command. */
9892
9893void
fba45db2 9894resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9895{
9896 CORE_ADDR pc;
9897
9898 if (sal->pc == 0 && sal->symtab != NULL)
9899 {
9900 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9901 error (_("No line %d in file \"%s\"."),
05cba821 9902 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9903 sal->pc = pc;
6a048695 9904
4a64f543
MS
9905 /* If this SAL corresponds to a breakpoint inserted using a line
9906 number, then skip the function prologue if necessary. */
6a048695 9907 if (sal->explicit_line)
059acae7 9908 skip_prologue_sal (sal);
c906108c
SS
9909 }
9910
9911 if (sal->section == 0 && sal->symtab != NULL)
9912 {
9913 struct blockvector *bv;
c5aa993b
JM
9914 struct block *b;
9915 struct symbol *sym;
c906108c 9916
801e3a5b 9917 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
9918 if (bv != NULL)
9919 {
7f0df278 9920 sym = block_linkage_function (b);
c906108c
SS
9921 if (sym != NULL)
9922 {
9923 fixup_symbol_section (sym, sal->symtab->objfile);
e27d198c 9924 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
c906108c
SS
9925 }
9926 else
9927 {
4a64f543
MS
9928 /* It really is worthwhile to have the section, so we'll
9929 just have to look harder. This case can be executed
9930 if we have line numbers but no functions (as can
9931 happen in assembly source). */
c906108c 9932
7cbd4a93 9933 struct bound_minimal_symbol msym;
6c95b8df
PA
9934 struct cleanup *old_chain = save_current_space_and_thread ();
9935
9936 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9937
9938 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9939 if (msym.minsym)
e27d198c 9940 sal->section = SYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
9941
9942 do_cleanups (old_chain);
c906108c
SS
9943 }
9944 }
9945 }
9946}
9947
9948void
fba45db2 9949break_command (char *arg, int from_tty)
c906108c 9950{
db107f19 9951 break_command_1 (arg, 0, from_tty);
c906108c
SS
9952}
9953
c906108c 9954void
fba45db2 9955tbreak_command (char *arg, int from_tty)
c906108c 9956{
db107f19 9957 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9958}
9959
c906108c 9960static void
fba45db2 9961hbreak_command (char *arg, int from_tty)
c906108c 9962{
db107f19 9963 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9964}
9965
9966static void
fba45db2 9967thbreak_command (char *arg, int from_tty)
c906108c 9968{
db107f19 9969 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9970}
9971
9972static void
fba45db2 9973stop_command (char *arg, int from_tty)
c906108c 9974{
a3f17187 9975 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9976Usage: stop in <function | address>\n\
a3f17187 9977 stop at <line>\n"));
c906108c
SS
9978}
9979
9980static void
fba45db2 9981stopin_command (char *arg, int from_tty)
c906108c
SS
9982{
9983 int badInput = 0;
9984
c5aa993b 9985 if (arg == (char *) NULL)
c906108c
SS
9986 badInput = 1;
9987 else if (*arg != '*')
9988 {
9989 char *argptr = arg;
9990 int hasColon = 0;
9991
4a64f543 9992 /* Look for a ':'. If this is a line number specification, then
53a5351d 9993 say it is bad, otherwise, it should be an address or
4a64f543 9994 function/method name. */
c906108c 9995 while (*argptr && !hasColon)
c5aa993b
JM
9996 {
9997 hasColon = (*argptr == ':');
9998 argptr++;
9999 }
c906108c
SS
10000
10001 if (hasColon)
c5aa993b 10002 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10003 else
c5aa993b 10004 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10005 }
10006
10007 if (badInput)
a3f17187 10008 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10009 else
db107f19 10010 break_command_1 (arg, 0, from_tty);
c906108c
SS
10011}
10012
10013static void
fba45db2 10014stopat_command (char *arg, int from_tty)
c906108c
SS
10015{
10016 int badInput = 0;
10017
c5aa993b 10018 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10019 badInput = 1;
10020 else
10021 {
10022 char *argptr = arg;
10023 int hasColon = 0;
10024
4a64f543
MS
10025 /* Look for a ':'. If there is a '::' then get out, otherwise
10026 it is probably a line number. */
c906108c 10027 while (*argptr && !hasColon)
c5aa993b
JM
10028 {
10029 hasColon = (*argptr == ':');
10030 argptr++;
10031 }
c906108c
SS
10032
10033 if (hasColon)
c5aa993b 10034 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10035 else
c5aa993b 10036 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10037 }
10038
10039 if (badInput)
a3f17187 10040 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10041 else
db107f19 10042 break_command_1 (arg, 0, from_tty);
c906108c
SS
10043}
10044
e7e0cddf
SS
10045/* The dynamic printf command is mostly like a regular breakpoint, but
10046 with a prewired command list consisting of a single output command,
10047 built from extra arguments supplied on the dprintf command
10048 line. */
10049
da821c7b 10050static void
e7e0cddf
SS
10051dprintf_command (char *arg, int from_tty)
10052{
10053 create_breakpoint (get_current_arch (),
10054 arg,
10055 NULL, 0, NULL, 1 /* parse arg */,
10056 0, bp_dprintf,
10057 0 /* Ignore count */,
10058 pending_break_support,
10059 &dprintf_breakpoint_ops,
10060 from_tty,
10061 1 /* enabled */,
10062 0 /* internal */,
10063 0);
10064}
10065
d3ce09f5
SS
10066static void
10067agent_printf_command (char *arg, int from_tty)
10068{
10069 error (_("May only run agent-printf on the target"));
10070}
10071
f1310107
TJB
10072/* Implement the "breakpoint_hit" breakpoint_ops method for
10073 ranged breakpoints. */
10074
10075static int
10076breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10077 struct address_space *aspace,
09ac7c10
TT
10078 CORE_ADDR bp_addr,
10079 const struct target_waitstatus *ws)
f1310107 10080{
09ac7c10 10081 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10082 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10083 return 0;
10084
f1310107
TJB
10085 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10086 bl->length, aspace, bp_addr);
10087}
10088
10089/* Implement the "resources_needed" breakpoint_ops method for
10090 ranged breakpoints. */
10091
10092static int
10093resources_needed_ranged_breakpoint (const struct bp_location *bl)
10094{
10095 return target_ranged_break_num_registers ();
10096}
10097
10098/* Implement the "print_it" breakpoint_ops method for
10099 ranged breakpoints. */
10100
10101static enum print_stop_action
348d480f 10102print_it_ranged_breakpoint (bpstat bs)
f1310107 10103{
348d480f 10104 struct breakpoint *b = bs->breakpoint_at;
f1310107 10105 struct bp_location *bl = b->loc;
79a45e25 10106 struct ui_out *uiout = current_uiout;
f1310107
TJB
10107
10108 gdb_assert (b->type == bp_hardware_breakpoint);
10109
10110 /* Ranged breakpoints have only one location. */
10111 gdb_assert (bl && bl->next == NULL);
10112
10113 annotate_breakpoint (b->number);
10114 if (b->disposition == disp_del)
10115 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10116 else
10117 ui_out_text (uiout, "\nRanged breakpoint ");
10118 if (ui_out_is_mi_like_p (uiout))
10119 {
10120 ui_out_field_string (uiout, "reason",
10121 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10122 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10123 }
10124 ui_out_field_int (uiout, "bkptno", b->number);
10125 ui_out_text (uiout, ", ");
10126
10127 return PRINT_SRC_AND_LOC;
10128}
10129
10130/* Implement the "print_one" breakpoint_ops method for
10131 ranged breakpoints. */
10132
10133static void
10134print_one_ranged_breakpoint (struct breakpoint *b,
10135 struct bp_location **last_loc)
10136{
10137 struct bp_location *bl = b->loc;
10138 struct value_print_options opts;
79a45e25 10139 struct ui_out *uiout = current_uiout;
f1310107
TJB
10140
10141 /* Ranged breakpoints have only one location. */
10142 gdb_assert (bl && bl->next == NULL);
10143
10144 get_user_print_options (&opts);
10145
10146 if (opts.addressprint)
10147 /* We don't print the address range here, it will be printed later
10148 by print_one_detail_ranged_breakpoint. */
10149 ui_out_field_skip (uiout, "addr");
10150 annotate_field (5);
10151 print_breakpoint_location (b, bl);
10152 *last_loc = bl;
10153}
10154
10155/* Implement the "print_one_detail" breakpoint_ops method for
10156 ranged breakpoints. */
10157
10158static void
10159print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10160 struct ui_out *uiout)
10161{
10162 CORE_ADDR address_start, address_end;
10163 struct bp_location *bl = b->loc;
f99d8bf4
PA
10164 struct ui_file *stb = mem_fileopen ();
10165 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10166
10167 gdb_assert (bl);
10168
10169 address_start = bl->address;
10170 address_end = address_start + bl->length - 1;
10171
10172 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10173 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10174 print_core_address (bl->gdbarch, address_start),
10175 print_core_address (bl->gdbarch, address_end));
10176 ui_out_field_stream (uiout, "addr", stb);
10177 ui_out_text (uiout, "\n");
10178
10179 do_cleanups (cleanup);
10180}
10181
10182/* Implement the "print_mention" breakpoint_ops method for
10183 ranged breakpoints. */
10184
10185static void
10186print_mention_ranged_breakpoint (struct breakpoint *b)
10187{
10188 struct bp_location *bl = b->loc;
79a45e25 10189 struct ui_out *uiout = current_uiout;
f1310107
TJB
10190
10191 gdb_assert (bl);
10192 gdb_assert (b->type == bp_hardware_breakpoint);
10193
10194 if (ui_out_is_mi_like_p (uiout))
10195 return;
10196
10197 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10198 b->number, paddress (bl->gdbarch, bl->address),
10199 paddress (bl->gdbarch, bl->address + bl->length - 1));
10200}
10201
10202/* Implement the "print_recreate" breakpoint_ops method for
10203 ranged breakpoints. */
10204
10205static void
10206print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10207{
10208 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10209 b->addr_string_range_end);
d9b3f62e 10210 print_recreate_thread (b, fp);
f1310107
TJB
10211}
10212
10213/* The breakpoint_ops structure to be used in ranged breakpoints. */
10214
2060206e 10215static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10216
10217/* Find the address where the end of the breakpoint range should be
10218 placed, given the SAL of the end of the range. This is so that if
10219 the user provides a line number, the end of the range is set to the
10220 last instruction of the given line. */
10221
10222static CORE_ADDR
10223find_breakpoint_range_end (struct symtab_and_line sal)
10224{
10225 CORE_ADDR end;
10226
10227 /* If the user provided a PC value, use it. Otherwise,
10228 find the address of the end of the given location. */
10229 if (sal.explicit_pc)
10230 end = sal.pc;
10231 else
10232 {
10233 int ret;
10234 CORE_ADDR start;
10235
10236 ret = find_line_pc_range (sal, &start, &end);
10237 if (!ret)
10238 error (_("Could not find location of the end of the range."));
10239
10240 /* find_line_pc_range returns the start of the next line. */
10241 end--;
10242 }
10243
10244 return end;
10245}
10246
10247/* Implement the "break-range" CLI command. */
10248
10249static void
10250break_range_command (char *arg, int from_tty)
10251{
10252 char *arg_start, *addr_string_start, *addr_string_end;
10253 struct linespec_result canonical_start, canonical_end;
10254 int bp_count, can_use_bp, length;
10255 CORE_ADDR end;
10256 struct breakpoint *b;
10257 struct symtab_and_line sal_start, sal_end;
f1310107 10258 struct cleanup *cleanup_bkpt;
f8eba3c6 10259 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10260
10261 /* We don't support software ranged breakpoints. */
10262 if (target_ranged_break_num_registers () < 0)
10263 error (_("This target does not support hardware ranged breakpoints."));
10264
10265 bp_count = hw_breakpoint_used_count ();
10266 bp_count += target_ranged_break_num_registers ();
10267 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10268 bp_count, 0);
10269 if (can_use_bp < 0)
10270 error (_("Hardware breakpoints used exceeds limit."));
10271
f8eba3c6 10272 arg = skip_spaces (arg);
f1310107
TJB
10273 if (arg == NULL || arg[0] == '\0')
10274 error(_("No address range specified."));
10275
f1310107
TJB
10276 init_linespec_result (&canonical_start);
10277
f8eba3c6
TT
10278 arg_start = arg;
10279 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10280
f8eba3c6 10281 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10282
10283 if (arg[0] != ',')
10284 error (_("Too few arguments."));
f8eba3c6 10285 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10286 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10287
10288 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10289
10290 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10291 || lsal_start->sals.nelts != 1)
f1310107
TJB
10292 error (_("Cannot create a ranged breakpoint with multiple locations."));
10293
f8eba3c6
TT
10294 sal_start = lsal_start->sals.sals[0];
10295 addr_string_start = savestring (arg_start, arg - arg_start);
10296 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10297
10298 arg++; /* Skip the comma. */
f8eba3c6 10299 arg = skip_spaces (arg);
f1310107
TJB
10300
10301 /* Parse the end location. */
10302
f1310107
TJB
10303 init_linespec_result (&canonical_end);
10304 arg_start = arg;
10305
f8eba3c6 10306 /* We call decode_line_full directly here instead of using
f1310107
TJB
10307 parse_breakpoint_sals because we need to specify the start location's
10308 symtab and line as the default symtab and line for the end of the
10309 range. This makes it possible to have ranges like "foo.c:27, +14",
10310 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10311 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10312 sal_start.symtab, sal_start.line,
10313 &canonical_end, NULL, NULL);
10314
10315 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10316
f8eba3c6 10317 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10318 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10319
10320 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10321 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10322 || lsal_end->sals.nelts != 1)
f1310107
TJB
10323 error (_("Cannot create a ranged breakpoint with multiple locations."));
10324
f8eba3c6
TT
10325 sal_end = lsal_end->sals.sals[0];
10326 addr_string_end = savestring (arg_start, arg - arg_start);
10327 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10328
10329 end = find_breakpoint_range_end (sal_end);
10330 if (sal_start.pc > end)
177b42fe 10331 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10332
10333 length = end - sal_start.pc + 1;
10334 if (length < 0)
10335 /* Length overflowed. */
10336 error (_("Address range too large."));
10337 else if (length == 1)
10338 {
10339 /* This range is simple enough to be handled by
10340 the `hbreak' command. */
10341 hbreak_command (addr_string_start, 1);
10342
10343 do_cleanups (cleanup_bkpt);
10344
10345 return;
10346 }
10347
10348 /* Now set up the breakpoint. */
10349 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10350 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10351 set_breakpoint_count (breakpoint_count + 1);
10352 b->number = breakpoint_count;
10353 b->disposition = disp_donttouch;
f8eba3c6
TT
10354 b->addr_string = xstrdup (addr_string_start);
10355 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10356 b->loc->length = length;
10357
f8eba3c6 10358 do_cleanups (cleanup_bkpt);
f1310107
TJB
10359
10360 mention (b);
8d3788bd 10361 observer_notify_breakpoint_created (b);
f1310107
TJB
10362 update_global_location_list (1);
10363}
10364
4a64f543
MS
10365/* Return non-zero if EXP is verified as constant. Returned zero
10366 means EXP is variable. Also the constant detection may fail for
10367 some constant expressions and in such case still falsely return
10368 zero. */
2e6e3d9c 10369
65d79d4b
SDJ
10370static int
10371watchpoint_exp_is_const (const struct expression *exp)
10372{
10373 int i = exp->nelts;
10374
10375 while (i > 0)
10376 {
10377 int oplenp, argsp;
10378
10379 /* We are only interested in the descriptor of each element. */
10380 operator_length (exp, i, &oplenp, &argsp);
10381 i -= oplenp;
10382
10383 switch (exp->elts[i].opcode)
10384 {
10385 case BINOP_ADD:
10386 case BINOP_SUB:
10387 case BINOP_MUL:
10388 case BINOP_DIV:
10389 case BINOP_REM:
10390 case BINOP_MOD:
10391 case BINOP_LSH:
10392 case BINOP_RSH:
10393 case BINOP_LOGICAL_AND:
10394 case BINOP_LOGICAL_OR:
10395 case BINOP_BITWISE_AND:
10396 case BINOP_BITWISE_IOR:
10397 case BINOP_BITWISE_XOR:
10398 case BINOP_EQUAL:
10399 case BINOP_NOTEQUAL:
10400 case BINOP_LESS:
10401 case BINOP_GTR:
10402 case BINOP_LEQ:
10403 case BINOP_GEQ:
10404 case BINOP_REPEAT:
10405 case BINOP_COMMA:
10406 case BINOP_EXP:
10407 case BINOP_MIN:
10408 case BINOP_MAX:
10409 case BINOP_INTDIV:
10410 case BINOP_CONCAT:
10411 case BINOP_IN:
10412 case BINOP_RANGE:
10413 case TERNOP_COND:
10414 case TERNOP_SLICE:
65d79d4b
SDJ
10415
10416 case OP_LONG:
10417 case OP_DOUBLE:
10418 case OP_DECFLOAT:
10419 case OP_LAST:
10420 case OP_COMPLEX:
10421 case OP_STRING:
65d79d4b
SDJ
10422 case OP_ARRAY:
10423 case OP_TYPE:
608b4967
TT
10424 case OP_TYPEOF:
10425 case OP_DECLTYPE:
6e72ca20 10426 case OP_TYPEID:
65d79d4b
SDJ
10427 case OP_NAME:
10428 case OP_OBJC_NSSTRING:
10429
10430 case UNOP_NEG:
10431 case UNOP_LOGICAL_NOT:
10432 case UNOP_COMPLEMENT:
10433 case UNOP_ADDR:
10434 case UNOP_HIGH:
aeaa2474 10435 case UNOP_CAST:
9eaf6705
TT
10436
10437 case UNOP_CAST_TYPE:
10438 case UNOP_REINTERPRET_CAST:
10439 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10440 /* Unary, binary and ternary operators: We have to check
10441 their operands. If they are constant, then so is the
10442 result of that operation. For instance, if A and B are
10443 determined to be constants, then so is "A + B".
10444
10445 UNOP_IND is one exception to the rule above, because the
10446 value of *ADDR is not necessarily a constant, even when
10447 ADDR is. */
65d79d4b
SDJ
10448 break;
10449
10450 case OP_VAR_VALUE:
10451 /* Check whether the associated symbol is a constant.
4a64f543 10452
65d79d4b 10453 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10454 possible that a buggy compiler could mark a variable as
10455 constant even when it is not, and TYPE_CONST would return
10456 true in this case, while SYMBOL_CLASS wouldn't.
10457
10458 We also have to check for function symbols because they
10459 are always constant. */
65d79d4b
SDJ
10460 {
10461 struct symbol *s = exp->elts[i + 2].symbol;
10462
10463 if (SYMBOL_CLASS (s) != LOC_BLOCK
10464 && SYMBOL_CLASS (s) != LOC_CONST
10465 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10466 return 0;
10467 break;
10468 }
10469
10470 /* The default action is to return 0 because we are using
10471 the optimistic approach here: If we don't know something,
10472 then it is not a constant. */
10473 default:
10474 return 0;
10475 }
10476 }
10477
10478 return 1;
10479}
10480
3a5c3e22
PA
10481/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10482
10483static void
10484dtor_watchpoint (struct breakpoint *self)
10485{
10486 struct watchpoint *w = (struct watchpoint *) self;
10487
10488 xfree (w->cond_exp);
10489 xfree (w->exp);
10490 xfree (w->exp_string);
10491 xfree (w->exp_string_reparse);
10492 value_free (w->val);
10493
10494 base_breakpoint_ops.dtor (self);
10495}
10496
348d480f
PA
10497/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10498
10499static void
10500re_set_watchpoint (struct breakpoint *b)
10501{
3a5c3e22
PA
10502 struct watchpoint *w = (struct watchpoint *) b;
10503
348d480f
PA
10504 /* Watchpoint can be either on expression using entirely global
10505 variables, or it can be on local variables.
10506
10507 Watchpoints of the first kind are never auto-deleted, and even
10508 persist across program restarts. Since they can use variables
10509 from shared libraries, we need to reparse expression as libraries
10510 are loaded and unloaded.
10511
10512 Watchpoints on local variables can also change meaning as result
10513 of solib event. For example, if a watchpoint uses both a local
10514 and a global variables in expression, it's a local watchpoint,
10515 but unloading of a shared library will make the expression
10516 invalid. This is not a very common use case, but we still
10517 re-evaluate expression, to avoid surprises to the user.
10518
10519 Note that for local watchpoints, we re-evaluate it only if
10520 watchpoints frame id is still valid. If it's not, it means the
10521 watchpoint is out of scope and will be deleted soon. In fact,
10522 I'm not sure we'll ever be called in this case.
10523
10524 If a local watchpoint's frame id is still valid, then
3a5c3e22 10525 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10526
3a5c3e22
PA
10527 Don't do anything about disabled watchpoints, since they will be
10528 reevaluated again when enabled. */
10529 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10530}
10531
77b06cd7
TJB
10532/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10533
10534static int
10535insert_watchpoint (struct bp_location *bl)
10536{
3a5c3e22
PA
10537 struct watchpoint *w = (struct watchpoint *) bl->owner;
10538 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10539
10540 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10541 w->cond_exp);
77b06cd7
TJB
10542}
10543
10544/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10545
10546static int
10547remove_watchpoint (struct bp_location *bl)
10548{
3a5c3e22
PA
10549 struct watchpoint *w = (struct watchpoint *) bl->owner;
10550 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10551
10552 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10553 w->cond_exp);
e09342b5
TJB
10554}
10555
e09342b5 10556static int
348d480f 10557breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10558 struct address_space *aspace, CORE_ADDR bp_addr,
10559 const struct target_waitstatus *ws)
e09342b5 10560{
348d480f 10561 struct breakpoint *b = bl->owner;
3a5c3e22 10562 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10563
348d480f
PA
10564 /* Continuable hardware watchpoints are treated as non-existent if the
10565 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10566 some data address). Otherwise gdb won't stop on a break instruction
10567 in the code (not from a breakpoint) when a hardware watchpoint has
10568 been defined. Also skip watchpoints which we know did not trigger
10569 (did not match the data address). */
10570 if (is_hardware_watchpoint (b)
3a5c3e22 10571 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10572 return 0;
9c06b0b4 10573
348d480f 10574 return 1;
9c06b0b4
TJB
10575}
10576
348d480f
PA
10577static void
10578check_status_watchpoint (bpstat bs)
9c06b0b4 10579{
348d480f 10580 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10581
348d480f 10582 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10583}
10584
10585/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10586 hardware watchpoints. */
9c06b0b4
TJB
10587
10588static int
348d480f 10589resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10590{
3a5c3e22
PA
10591 struct watchpoint *w = (struct watchpoint *) bl->owner;
10592 int length = w->exact? 1 : bl->length;
348d480f
PA
10593
10594 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10595}
10596
10597/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10598 hardware watchpoints. */
9c06b0b4
TJB
10599
10600static int
348d480f 10601works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10602{
efa80663
PA
10603 /* Read and access watchpoints only work with hardware support. */
10604 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10605}
10606
9c06b0b4 10607static enum print_stop_action
348d480f 10608print_it_watchpoint (bpstat bs)
9c06b0b4 10609{
348d480f
PA
10610 struct cleanup *old_chain;
10611 struct breakpoint *b;
f99d8bf4 10612 struct ui_file *stb;
348d480f 10613 enum print_stop_action result;
3a5c3e22 10614 struct watchpoint *w;
79a45e25 10615 struct ui_out *uiout = current_uiout;
348d480f
PA
10616
10617 gdb_assert (bs->bp_location_at != NULL);
10618
348d480f 10619 b = bs->breakpoint_at;
3a5c3e22 10620 w = (struct watchpoint *) b;
348d480f 10621
f99d8bf4
PA
10622 stb = mem_fileopen ();
10623 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10624
10625 switch (b->type)
10626 {
348d480f 10627 case bp_watchpoint:
9c06b0b4
TJB
10628 case bp_hardware_watchpoint:
10629 annotate_watchpoint (b->number);
10630 if (ui_out_is_mi_like_p (uiout))
10631 ui_out_field_string
10632 (uiout, "reason",
10633 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10634 mention (b);
10635 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10636 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10637 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10638 ui_out_field_stream (uiout, "old", stb);
10639 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10640 watchpoint_value_print (w->val, stb);
348d480f
PA
10641 ui_out_field_stream (uiout, "new", stb);
10642 ui_out_text (uiout, "\n");
10643 /* More than one watchpoint may have been triggered. */
10644 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10645 break;
10646
10647 case bp_read_watchpoint:
10648 if (ui_out_is_mi_like_p (uiout))
10649 ui_out_field_string
10650 (uiout, "reason",
10651 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10652 mention (b);
10653 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10654 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10655 watchpoint_value_print (w->val, stb);
348d480f
PA
10656 ui_out_field_stream (uiout, "value", stb);
10657 ui_out_text (uiout, "\n");
10658 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10659 break;
10660
10661 case bp_access_watchpoint:
348d480f
PA
10662 if (bs->old_val != NULL)
10663 {
10664 annotate_watchpoint (b->number);
10665 if (ui_out_is_mi_like_p (uiout))
10666 ui_out_field_string
10667 (uiout, "reason",
10668 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10669 mention (b);
10670 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10671 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10672 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10673 ui_out_field_stream (uiout, "old", stb);
10674 ui_out_text (uiout, "\nNew value = ");
10675 }
10676 else
10677 {
10678 mention (b);
10679 if (ui_out_is_mi_like_p (uiout))
10680 ui_out_field_string
10681 (uiout, "reason",
10682 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10683 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10684 ui_out_text (uiout, "\nValue = ");
10685 }
f99d8bf4 10686 watchpoint_value_print (w->val, stb);
348d480f
PA
10687 ui_out_field_stream (uiout, "new", stb);
10688 ui_out_text (uiout, "\n");
10689 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10690 break;
10691 default:
348d480f 10692 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10693 }
10694
348d480f
PA
10695 do_cleanups (old_chain);
10696 return result;
10697}
10698
10699/* Implement the "print_mention" breakpoint_ops method for hardware
10700 watchpoints. */
10701
10702static void
10703print_mention_watchpoint (struct breakpoint *b)
10704{
10705 struct cleanup *ui_out_chain;
3a5c3e22 10706 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10707 struct ui_out *uiout = current_uiout;
348d480f
PA
10708
10709 switch (b->type)
10710 {
10711 case bp_watchpoint:
10712 ui_out_text (uiout, "Watchpoint ");
10713 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10714 break;
10715 case bp_hardware_watchpoint:
10716 ui_out_text (uiout, "Hardware watchpoint ");
10717 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10718 break;
10719 case bp_read_watchpoint:
10720 ui_out_text (uiout, "Hardware read watchpoint ");
10721 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10722 break;
10723 case bp_access_watchpoint:
10724 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10725 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10726 break;
10727 default:
10728 internal_error (__FILE__, __LINE__,
10729 _("Invalid hardware watchpoint type."));
10730 }
10731
10732 ui_out_field_int (uiout, "number", b->number);
10733 ui_out_text (uiout, ": ");
3a5c3e22 10734 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10735 do_cleanups (ui_out_chain);
10736}
10737
10738/* Implement the "print_recreate" breakpoint_ops method for
10739 watchpoints. */
10740
10741static void
10742print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10743{
3a5c3e22
PA
10744 struct watchpoint *w = (struct watchpoint *) b;
10745
348d480f
PA
10746 switch (b->type)
10747 {
10748 case bp_watchpoint:
10749 case bp_hardware_watchpoint:
10750 fprintf_unfiltered (fp, "watch");
10751 break;
10752 case bp_read_watchpoint:
10753 fprintf_unfiltered (fp, "rwatch");
10754 break;
10755 case bp_access_watchpoint:
10756 fprintf_unfiltered (fp, "awatch");
10757 break;
10758 default:
10759 internal_error (__FILE__, __LINE__,
10760 _("Invalid watchpoint type."));
10761 }
10762
3a5c3e22 10763 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10764 print_recreate_thread (b, fp);
348d480f
PA
10765}
10766
427cd150
TT
10767/* Implement the "explains_signal" breakpoint_ops method for
10768 watchpoints. */
10769
47591c29 10770static int
427cd150
TT
10771explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10772{
10773 /* A software watchpoint cannot cause a signal other than
10774 GDB_SIGNAL_TRAP. */
10775 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10776 return 0;
427cd150 10777
47591c29 10778 return 1;
427cd150
TT
10779}
10780
348d480f
PA
10781/* The breakpoint_ops structure to be used in hardware watchpoints. */
10782
2060206e 10783static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10784
10785/* Implement the "insert" breakpoint_ops method for
10786 masked hardware watchpoints. */
10787
10788static int
10789insert_masked_watchpoint (struct bp_location *bl)
10790{
3a5c3e22
PA
10791 struct watchpoint *w = (struct watchpoint *) bl->owner;
10792
10793 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10794 bl->watchpoint_type);
10795}
10796
10797/* Implement the "remove" breakpoint_ops method for
10798 masked hardware watchpoints. */
10799
10800static int
10801remove_masked_watchpoint (struct bp_location *bl)
10802{
3a5c3e22
PA
10803 struct watchpoint *w = (struct watchpoint *) bl->owner;
10804
10805 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10806 bl->watchpoint_type);
10807}
10808
10809/* Implement the "resources_needed" breakpoint_ops method for
10810 masked hardware watchpoints. */
10811
10812static int
10813resources_needed_masked_watchpoint (const struct bp_location *bl)
10814{
3a5c3e22
PA
10815 struct watchpoint *w = (struct watchpoint *) bl->owner;
10816
10817 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10818}
10819
10820/* Implement the "works_in_software_mode" breakpoint_ops method for
10821 masked hardware watchpoints. */
10822
10823static int
10824works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10825{
10826 return 0;
10827}
10828
10829/* Implement the "print_it" breakpoint_ops method for
10830 masked hardware watchpoints. */
10831
10832static enum print_stop_action
10833print_it_masked_watchpoint (bpstat bs)
10834{
10835 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10836 struct ui_out *uiout = current_uiout;
348d480f
PA
10837
10838 /* Masked watchpoints have only one location. */
10839 gdb_assert (b->loc && b->loc->next == NULL);
10840
10841 switch (b->type)
10842 {
10843 case bp_hardware_watchpoint:
10844 annotate_watchpoint (b->number);
10845 if (ui_out_is_mi_like_p (uiout))
10846 ui_out_field_string
10847 (uiout, "reason",
10848 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10849 break;
10850
10851 case bp_read_watchpoint:
10852 if (ui_out_is_mi_like_p (uiout))
10853 ui_out_field_string
10854 (uiout, "reason",
10855 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10856 break;
10857
10858 case bp_access_watchpoint:
10859 if (ui_out_is_mi_like_p (uiout))
10860 ui_out_field_string
10861 (uiout, "reason",
10862 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10863 break;
10864 default:
10865 internal_error (__FILE__, __LINE__,
10866 _("Invalid hardware watchpoint type."));
10867 }
10868
10869 mention (b);
9c06b0b4
TJB
10870 ui_out_text (uiout, _("\n\
10871Check the underlying instruction at PC for the memory\n\
10872address and value which triggered this watchpoint.\n"));
10873 ui_out_text (uiout, "\n");
10874
10875 /* More than one watchpoint may have been triggered. */
10876 return PRINT_UNKNOWN;
10877}
10878
10879/* Implement the "print_one_detail" breakpoint_ops method for
10880 masked hardware watchpoints. */
10881
10882static void
10883print_one_detail_masked_watchpoint (const struct breakpoint *b,
10884 struct ui_out *uiout)
10885{
3a5c3e22
PA
10886 struct watchpoint *w = (struct watchpoint *) b;
10887
9c06b0b4
TJB
10888 /* Masked watchpoints have only one location. */
10889 gdb_assert (b->loc && b->loc->next == NULL);
10890
10891 ui_out_text (uiout, "\tmask ");
3a5c3e22 10892 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10893 ui_out_text (uiout, "\n");
10894}
10895
10896/* Implement the "print_mention" breakpoint_ops method for
10897 masked hardware watchpoints. */
10898
10899static void
10900print_mention_masked_watchpoint (struct breakpoint *b)
10901{
3a5c3e22 10902 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10903 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10904 struct cleanup *ui_out_chain;
10905
10906 switch (b->type)
10907 {
10908 case bp_hardware_watchpoint:
10909 ui_out_text (uiout, "Masked hardware watchpoint ");
10910 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10911 break;
10912 case bp_read_watchpoint:
10913 ui_out_text (uiout, "Masked hardware read watchpoint ");
10914 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10915 break;
10916 case bp_access_watchpoint:
10917 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10918 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10919 break;
10920 default:
10921 internal_error (__FILE__, __LINE__,
10922 _("Invalid hardware watchpoint type."));
10923 }
10924
10925 ui_out_field_int (uiout, "number", b->number);
10926 ui_out_text (uiout, ": ");
3a5c3e22 10927 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10928 do_cleanups (ui_out_chain);
10929}
10930
10931/* Implement the "print_recreate" breakpoint_ops method for
10932 masked hardware watchpoints. */
10933
10934static void
10935print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10936{
3a5c3e22 10937 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10938 char tmp[40];
10939
10940 switch (b->type)
10941 {
10942 case bp_hardware_watchpoint:
10943 fprintf_unfiltered (fp, "watch");
10944 break;
10945 case bp_read_watchpoint:
10946 fprintf_unfiltered (fp, "rwatch");
10947 break;
10948 case bp_access_watchpoint:
10949 fprintf_unfiltered (fp, "awatch");
10950 break;
10951 default:
10952 internal_error (__FILE__, __LINE__,
10953 _("Invalid hardware watchpoint type."));
10954 }
10955
3a5c3e22
PA
10956 sprintf_vma (tmp, w->hw_wp_mask);
10957 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10958 print_recreate_thread (b, fp);
9c06b0b4
TJB
10959}
10960
10961/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10962
2060206e 10963static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10964
10965/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10966
10967static int
10968is_masked_watchpoint (const struct breakpoint *b)
10969{
10970 return b->ops == &masked_watchpoint_breakpoint_ops;
10971}
10972
53a5351d
JM
10973/* accessflag: hw_write: watch write,
10974 hw_read: watch read,
10975 hw_access: watch access (read or write) */
c906108c 10976static void
bbc13ae3 10977watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10978 int just_location, int internal)
c906108c 10979{
a9634178 10980 volatile struct gdb_exception e;
d983da9c 10981 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 10982 struct expression *exp;
270140bd 10983 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10984 struct value *val, *mark, *result;
c906108c 10985 struct frame_info *frame;
bbc13ae3
KS
10986 const char *exp_start = NULL;
10987 const char *exp_end = NULL;
10988 const char *tok, *end_tok;
9c06b0b4 10989 int toklen = -1;
bbc13ae3
KS
10990 const char *cond_start = NULL;
10991 const char *cond_end = NULL;
c906108c 10992 enum bptype bp_type;
37e4754d 10993 int thread = -1;
0cf6dd15 10994 int pc = 0;
9c06b0b4
TJB
10995 /* Flag to indicate whether we are going to use masks for
10996 the hardware watchpoint. */
10997 int use_mask = 0;
10998 CORE_ADDR mask = 0;
3a5c3e22 10999 struct watchpoint *w;
bbc13ae3
KS
11000 char *expression;
11001 struct cleanup *back_to;
c906108c 11002
37e4754d
LM
11003 /* Make sure that we actually have parameters to parse. */
11004 if (arg != NULL && arg[0] != '\0')
11005 {
bbc13ae3
KS
11006 const char *value_start;
11007
11008 exp_end = arg + strlen (arg);
37e4754d 11009
9c06b0b4
TJB
11010 /* Look for "parameter value" pairs at the end
11011 of the arguments string. */
bbc13ae3 11012 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11013 {
11014 /* Skip whitespace at the end of the argument list. */
11015 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11016 tok--;
11017
11018 /* Find the beginning of the last token.
11019 This is the value of the parameter. */
11020 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11021 tok--;
11022 value_start = tok + 1;
11023
11024 /* Skip whitespace. */
11025 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11026 tok--;
11027
11028 end_tok = tok;
11029
11030 /* Find the beginning of the second to last token.
11031 This is the parameter itself. */
11032 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11033 tok--;
11034 tok++;
11035 toklen = end_tok - tok + 1;
11036
11037 if (toklen == 6 && !strncmp (tok, "thread", 6))
11038 {
11039 /* At this point we've found a "thread" token, which means
11040 the user is trying to set a watchpoint that triggers
11041 only in a specific thread. */
11042 char *endp;
37e4754d 11043
9c06b0b4
TJB
11044 if (thread != -1)
11045 error(_("You can specify only one thread."));
37e4754d 11046
9c06b0b4
TJB
11047 /* Extract the thread ID from the next token. */
11048 thread = strtol (value_start, &endp, 0);
37e4754d 11049
9c06b0b4
TJB
11050 /* Check if the user provided a valid numeric value for the
11051 thread ID. */
11052 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11053 error (_("Invalid thread ID specification %s."), value_start);
11054
11055 /* Check if the thread actually exists. */
11056 if (!valid_thread_id (thread))
af4908ba 11057 invalid_thread_id_error (thread);
9c06b0b4
TJB
11058 }
11059 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11060 {
11061 /* We've found a "mask" token, which means the user wants to
11062 create a hardware watchpoint that is going to have the mask
11063 facility. */
11064 struct value *mask_value, *mark;
37e4754d 11065
9c06b0b4
TJB
11066 if (use_mask)
11067 error(_("You can specify only one mask."));
37e4754d 11068
9c06b0b4 11069 use_mask = just_location = 1;
37e4754d 11070
9c06b0b4
TJB
11071 mark = value_mark ();
11072 mask_value = parse_to_comma_and_eval (&value_start);
11073 mask = value_as_address (mask_value);
11074 value_free_to_mark (mark);
11075 }
11076 else
11077 /* We didn't recognize what we found. We should stop here. */
11078 break;
37e4754d 11079
9c06b0b4
TJB
11080 /* Truncate the string and get rid of the "parameter value" pair before
11081 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11082 exp_end = tok;
9c06b0b4 11083 }
37e4754d 11084 }
bbc13ae3
KS
11085 else
11086 exp_end = arg;
37e4754d 11087
bbc13ae3
KS
11088 /* Parse the rest of the arguments. From here on out, everything
11089 is in terms of a newly allocated string instead of the original
11090 ARG. */
c906108c 11091 innermost_block = NULL;
bbc13ae3
KS
11092 expression = savestring (arg, exp_end - arg);
11093 back_to = make_cleanup (xfree, expression);
11094 exp_start = arg = expression;
1bb9788d 11095 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11096 exp_end = arg;
fa8a61dc
TT
11097 /* Remove trailing whitespace from the expression before saving it.
11098 This makes the eventual display of the expression string a bit
11099 prettier. */
11100 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11101 --exp_end;
11102
65d79d4b
SDJ
11103 /* Checking if the expression is not constant. */
11104 if (watchpoint_exp_is_const (exp))
11105 {
11106 int len;
11107
11108 len = exp_end - exp_start;
11109 while (len > 0 && isspace (exp_start[len - 1]))
11110 len--;
11111 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11112 }
11113
c906108c
SS
11114 exp_valid_block = innermost_block;
11115 mark = value_mark ();
3a1115a0 11116 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b
TT
11117
11118 if (just_location)
11119 {
9c06b0b4
TJB
11120 int ret;
11121
06a64a0b 11122 exp_valid_block = NULL;
a1442452 11123 val = value_addr (result);
06a64a0b
TT
11124 release_value (val);
11125 value_free_to_mark (mark);
9c06b0b4
TJB
11126
11127 if (use_mask)
11128 {
11129 ret = target_masked_watch_num_registers (value_as_address (val),
11130 mask);
11131 if (ret == -1)
11132 error (_("This target does not support masked watchpoints."));
11133 else if (ret == -2)
11134 error (_("Invalid mask or memory region."));
11135 }
06a64a0b
TT
11136 }
11137 else if (val != NULL)
fa4727a6 11138 release_value (val);
c906108c 11139
bbc13ae3
KS
11140 tok = skip_spaces_const (arg);
11141 end_tok = skip_to_space_const (tok);
c906108c
SS
11142
11143 toklen = end_tok - tok;
11144 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11145 {
2d134ed3
PA
11146 struct expression *cond;
11147
60e1c644 11148 innermost_block = NULL;
c906108c 11149 tok = cond_start = end_tok + 1;
1bb9788d 11150 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11151
11152 /* The watchpoint expression may not be local, but the condition
11153 may still be. E.g.: `watch global if local > 0'. */
11154 cond_exp_valid_block = innermost_block;
11155
2d134ed3 11156 xfree (cond);
c906108c
SS
11157 cond_end = tok;
11158 }
11159 if (*tok)
8a3fe4f8 11160 error (_("Junk at end of command."));
c906108c 11161
d983da9c 11162 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11163
11164 /* If the expression is "local", then set up a "watchpoint scope"
11165 breakpoint at the point where we've left the scope of the watchpoint
11166 expression. Create the scope breakpoint before the watchpoint, so
11167 that we will encounter it first in bpstat_stop_status. */
60e1c644 11168 if (exp_valid_block && frame)
d983da9c 11169 {
edb3359d
DJ
11170 if (frame_id_p (frame_unwind_caller_id (frame)))
11171 {
11172 scope_breakpoint
a6d9a66e
UW
11173 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11174 frame_unwind_caller_pc (frame),
06edf0c0
PA
11175 bp_watchpoint_scope,
11176 &momentary_breakpoint_ops);
d983da9c 11177
edb3359d 11178 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11179
edb3359d
DJ
11180 /* Automatically delete the breakpoint when it hits. */
11181 scope_breakpoint->disposition = disp_del;
d983da9c 11182
edb3359d
DJ
11183 /* Only break in the proper frame (help with recursion). */
11184 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11185
edb3359d 11186 /* Set the address at which we will stop. */
a6d9a66e
UW
11187 scope_breakpoint->loc->gdbarch
11188 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11189 scope_breakpoint->loc->requested_address
11190 = frame_unwind_caller_pc (frame);
11191 scope_breakpoint->loc->address
a6d9a66e
UW
11192 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11193 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11194 scope_breakpoint->type);
11195 }
d983da9c
DJ
11196 }
11197
e8369a73
AB
11198 /* Now set up the breakpoint. We create all watchpoints as hardware
11199 watchpoints here even if hardware watchpoints are turned off, a call
11200 to update_watchpoint later in this function will cause the type to
11201 drop back to bp_watchpoint (software watchpoint) if required. */
11202
11203 if (accessflag == hw_read)
11204 bp_type = bp_read_watchpoint;
11205 else if (accessflag == hw_access)
11206 bp_type = bp_access_watchpoint;
11207 else
11208 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11209
11210 w = XCNEW (struct watchpoint);
11211 b = &w->base;
348d480f 11212 if (use_mask)
3a5c3e22
PA
11213 init_raw_breakpoint_without_location (b, NULL, bp_type,
11214 &masked_watchpoint_breakpoint_ops);
348d480f 11215 else
3a5c3e22
PA
11216 init_raw_breakpoint_without_location (b, NULL, bp_type,
11217 &watchpoint_breakpoint_ops);
37e4754d 11218 b->thread = thread;
b5de0fa7 11219 b->disposition = disp_donttouch;
348d480f 11220 b->pspace = current_program_space;
3a5c3e22
PA
11221 w->exp = exp;
11222 w->exp_valid_block = exp_valid_block;
11223 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11224 if (just_location)
11225 {
11226 struct type *t = value_type (val);
11227 CORE_ADDR addr = value_as_address (val);
11228 char *name;
11229
11230 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11231 name = type_to_string (t);
11232
3a5c3e22 11233 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11234 core_addr_to_string (addr));
06a64a0b
TT
11235 xfree (name);
11236
3a5c3e22 11237 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11238 (int) (exp_end - exp_start), exp_start);
11239
06a64a0b
TT
11240 /* The above expression is in C. */
11241 b->language = language_c;
11242 }
11243 else
3a5c3e22 11244 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11245
11246 if (use_mask)
11247 {
3a5c3e22 11248 w->hw_wp_mask = mask;
9c06b0b4
TJB
11249 }
11250 else
11251 {
3a5c3e22
PA
11252 w->val = val;
11253 w->val_valid = 1;
9c06b0b4 11254 }
77b06cd7 11255
c906108c
SS
11256 if (cond_start)
11257 b->cond_string = savestring (cond_start, cond_end - cond_start);
11258 else
11259 b->cond_string = 0;
c5aa993b 11260
c906108c 11261 if (frame)
f6bc2008 11262 {
3a5c3e22
PA
11263 w->watchpoint_frame = get_frame_id (frame);
11264 w->watchpoint_thread = inferior_ptid;
f6bc2008 11265 }
c906108c 11266 else
f6bc2008 11267 {
3a5c3e22
PA
11268 w->watchpoint_frame = null_frame_id;
11269 w->watchpoint_thread = null_ptid;
f6bc2008 11270 }
c906108c 11271
d983da9c 11272 if (scope_breakpoint != NULL)
c906108c 11273 {
d983da9c
DJ
11274 /* The scope breakpoint is related to the watchpoint. We will
11275 need to act on them together. */
11276 b->related_breakpoint = scope_breakpoint;
11277 scope_breakpoint->related_breakpoint = b;
c906108c 11278 }
d983da9c 11279
06a64a0b
TT
11280 if (!just_location)
11281 value_free_to_mark (mark);
2d134ed3 11282
a9634178
TJB
11283 TRY_CATCH (e, RETURN_MASK_ALL)
11284 {
11285 /* Finally update the new watchpoint. This creates the locations
11286 that should be inserted. */
3a5c3e22 11287 update_watchpoint (w, 1);
a9634178
TJB
11288 }
11289 if (e.reason < 0)
11290 {
11291 delete_breakpoint (b);
11292 throw_exception (e);
11293 }
11294
3ea46bff 11295 install_breakpoint (internal, b, 1);
bbc13ae3 11296 do_cleanups (back_to);
c906108c
SS
11297}
11298
e09342b5 11299/* Return count of debug registers needed to watch the given expression.
e09342b5 11300 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11301
c906108c 11302static int
a9634178 11303can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11304{
11305 int found_memory_cnt = 0;
2e70b7b9 11306 struct value *head = v;
c906108c
SS
11307
11308 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11309 if (!can_use_hw_watchpoints)
c906108c 11310 return 0;
c5aa993b 11311
5c44784c
JM
11312 /* Make sure that the value of the expression depends only upon
11313 memory contents, and values computed from them within GDB. If we
11314 find any register references or function calls, we can't use a
11315 hardware watchpoint.
11316
11317 The idea here is that evaluating an expression generates a series
11318 of values, one holding the value of every subexpression. (The
11319 expression a*b+c has five subexpressions: a, b, a*b, c, and
11320 a*b+c.) GDB's values hold almost enough information to establish
11321 the criteria given above --- they identify memory lvalues,
11322 register lvalues, computed values, etcetera. So we can evaluate
11323 the expression, and then scan the chain of values that leaves
11324 behind to decide whether we can detect any possible change to the
11325 expression's final value using only hardware watchpoints.
11326
11327 However, I don't think that the values returned by inferior
11328 function calls are special in any way. So this function may not
11329 notice that an expression involving an inferior function call
11330 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11331 for (; v; v = value_next (v))
c906108c 11332 {
5c44784c 11333 if (VALUE_LVAL (v) == lval_memory)
c906108c 11334 {
8464be76
DJ
11335 if (v != head && value_lazy (v))
11336 /* A lazy memory lvalue in the chain is one that GDB never
11337 needed to fetch; we either just used its address (e.g.,
11338 `a' in `a.b') or we never needed it at all (e.g., `a'
11339 in `a,b'). This doesn't apply to HEAD; if that is
11340 lazy then it was not readable, but watch it anyway. */
5c44784c 11341 ;
53a5351d 11342 else
5c44784c
JM
11343 {
11344 /* Ahh, memory we actually used! Check if we can cover
11345 it with hardware watchpoints. */
df407dfe 11346 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11347
11348 /* We only watch structs and arrays if user asked for it
11349 explicitly, never if they just happen to appear in a
11350 middle of some value chain. */
11351 if (v == head
11352 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11353 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11354 {
42ae5230 11355 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11356 int len;
11357 int num_regs;
11358
a9634178 11359 len = (target_exact_watchpoints
e09342b5
TJB
11360 && is_scalar_type_recursive (vtype))?
11361 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11362
e09342b5
TJB
11363 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11364 if (!num_regs)
2e70b7b9
MS
11365 return 0;
11366 else
e09342b5 11367 found_memory_cnt += num_regs;
2e70b7b9 11368 }
5c44784c 11369 }
c5aa993b 11370 }
5086187c
AC
11371 else if (VALUE_LVAL (v) != not_lval
11372 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11373 return 0; /* These are values from the history (e.g., $1). */
5086187c 11374 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11375 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11376 }
11377
11378 /* The expression itself looks suitable for using a hardware
11379 watchpoint, but give the target machine a chance to reject it. */
11380 return found_memory_cnt;
11381}
11382
8b93c638 11383void
84f4c1fe 11384watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11385{
84f4c1fe 11386 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11387}
11388
06a64a0b
TT
11389/* A helper function that looks for the "-location" argument and then
11390 calls watch_command_1. */
11391
11392static void
11393watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11394{
11395 int just_location = 0;
11396
11397 if (arg
11398 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11399 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11400 {
e9cafbcc 11401 arg = skip_spaces (arg);
06a64a0b
TT
11402 just_location = 1;
11403 }
11404
84f4c1fe 11405 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11406}
8926118c 11407
c5aa993b 11408static void
fba45db2 11409watch_command (char *arg, int from_tty)
c906108c 11410{
06a64a0b 11411 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11412}
11413
8b93c638 11414void
84f4c1fe 11415rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11416{
84f4c1fe 11417 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11418}
8926118c 11419
c5aa993b 11420static void
fba45db2 11421rwatch_command (char *arg, int from_tty)
c906108c 11422{
06a64a0b 11423 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11424}
11425
8b93c638 11426void
84f4c1fe 11427awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11428{
84f4c1fe 11429 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11430}
8926118c 11431
c5aa993b 11432static void
fba45db2 11433awatch_command (char *arg, int from_tty)
c906108c 11434{
06a64a0b 11435 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11436}
c906108c 11437\f
c5aa993b 11438
43ff13b4 11439/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11440 because it uses the mechanisms of breakpoints. */
11441
bfec99b2
PA
11442struct until_break_command_continuation_args
11443{
11444 struct breakpoint *breakpoint;
11445 struct breakpoint *breakpoint2;
186c406b 11446 int thread_num;
bfec99b2
PA
11447};
11448
43ff13b4 11449/* This function is called by fetch_inferior_event via the
4a64f543 11450 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11451 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11452 command. */
c2c6d25f 11453static void
fa4cd53f 11454until_break_command_continuation (void *arg, int err)
43ff13b4 11455{
bfec99b2
PA
11456 struct until_break_command_continuation_args *a = arg;
11457
11458 delete_breakpoint (a->breakpoint);
11459 if (a->breakpoint2)
11460 delete_breakpoint (a->breakpoint2);
186c406b 11461 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11462}
11463
c906108c 11464void
ae66c1fc 11465until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11466{
11467 struct symtabs_and_lines sals;
11468 struct symtab_and_line sal;
8556afb4
PA
11469 struct frame_info *frame;
11470 struct gdbarch *frame_gdbarch;
11471 struct frame_id stack_frame_id;
11472 struct frame_id caller_frame_id;
c906108c 11473 struct breakpoint *breakpoint;
f107f563 11474 struct breakpoint *breakpoint2 = NULL;
c906108c 11475 struct cleanup *old_chain;
186c406b
TT
11476 int thread;
11477 struct thread_info *tp;
c906108c
SS
11478
11479 clear_proceed_status ();
11480
11481 /* Set a breakpoint where the user wants it and at return from
4a64f543 11482 this function. */
c5aa993b 11483
1bfeeb0f 11484 if (last_displayed_sal_is_valid ())
f8eba3c6 11485 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11486 get_last_displayed_symtab (),
f8eba3c6 11487 get_last_displayed_line ());
c906108c 11488 else
f8eba3c6
TT
11489 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11490 (struct symtab *) NULL, 0);
c5aa993b 11491
c906108c 11492 if (sals.nelts != 1)
8a3fe4f8 11493 error (_("Couldn't get information on specified line."));
c5aa993b 11494
c906108c 11495 sal = sals.sals[0];
4a64f543 11496 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11497
c906108c 11498 if (*arg)
8a3fe4f8 11499 error (_("Junk at end of arguments."));
c5aa993b 11500
c906108c 11501 resolve_sal_pc (&sal);
c5aa993b 11502
186c406b
TT
11503 tp = inferior_thread ();
11504 thread = tp->num;
11505
883bc8d1
PA
11506 old_chain = make_cleanup (null_cleanup, NULL);
11507
8556afb4
PA
11508 /* Note linespec handling above invalidates the frame chain.
11509 Installing a breakpoint also invalidates the frame chain (as it
11510 may need to switch threads), so do any frame handling before
11511 that. */
11512
11513 frame = get_selected_frame (NULL);
11514 frame_gdbarch = get_frame_arch (frame);
11515 stack_frame_id = get_stack_frame_id (frame);
11516 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11517
ae66c1fc
EZ
11518 /* Keep within the current frame, or in frames called by the current
11519 one. */
edb3359d 11520
883bc8d1 11521 if (frame_id_p (caller_frame_id))
c906108c 11522 {
883bc8d1
PA
11523 struct symtab_and_line sal2;
11524
11525 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11526 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11527 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11528 sal2,
11529 caller_frame_id,
f107f563
VP
11530 bp_until);
11531 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11532
883bc8d1 11533 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11534 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11535 }
c5aa993b 11536
c70a6932
JK
11537 /* set_momentary_breakpoint could invalidate FRAME. */
11538 frame = NULL;
11539
883bc8d1
PA
11540 if (anywhere)
11541 /* If the user told us to continue until a specified location,
11542 we don't specify a frame at which we need to stop. */
11543 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11544 null_frame_id, bp_until);
11545 else
11546 /* Otherwise, specify the selected frame, because we want to stop
11547 only at the very same frame. */
11548 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11549 stack_frame_id, bp_until);
11550 make_cleanup_delete_breakpoint (breakpoint);
11551
a493e3e2 11552 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11553
4a64f543
MS
11554 /* If we are running asynchronously, and proceed call above has
11555 actually managed to start the target, arrange for breakpoints to
11556 be deleted when the target stops. Otherwise, we're already
11557 stopped and delete breakpoints via cleanup chain. */
f107f563 11558
8ea051c5 11559 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11560 {
bfec99b2
PA
11561 struct until_break_command_continuation_args *args;
11562 args = xmalloc (sizeof (*args));
f107f563 11563
bfec99b2
PA
11564 args->breakpoint = breakpoint;
11565 args->breakpoint2 = breakpoint2;
186c406b 11566 args->thread_num = thread;
f107f563
VP
11567
11568 discard_cleanups (old_chain);
95e54da7
PA
11569 add_continuation (inferior_thread (),
11570 until_break_command_continuation, args,
604ead4a 11571 xfree);
f107f563
VP
11572 }
11573 else
c5aa993b 11574 do_cleanups (old_chain);
c906108c 11575}
ae66c1fc 11576
c906108c
SS
11577/* This function attempts to parse an optional "if <cond>" clause
11578 from the arg string. If one is not found, it returns NULL.
c5aa993b 11579
c906108c
SS
11580 Else, it returns a pointer to the condition string. (It does not
11581 attempt to evaluate the string against a particular block.) And,
11582 it updates arg to point to the first character following the parsed
4a64f543 11583 if clause in the arg string. */
53a5351d 11584
916703c0 11585char *
fba45db2 11586ep_parse_optional_if_clause (char **arg)
c906108c 11587{
c5aa993b
JM
11588 char *cond_string;
11589
11590 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11591 return NULL;
c5aa993b 11592
4a64f543 11593 /* Skip the "if" keyword. */
c906108c 11594 (*arg) += 2;
c5aa993b 11595
c906108c 11596 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11597 condition string. */
e9cafbcc 11598 *arg = skip_spaces (*arg);
c906108c 11599 cond_string = *arg;
c5aa993b 11600
4a64f543
MS
11601 /* Assume that the condition occupies the remainder of the arg
11602 string. */
c906108c 11603 (*arg) += strlen (cond_string);
c5aa993b 11604
c906108c
SS
11605 return cond_string;
11606}
c5aa993b 11607
c906108c
SS
11608/* Commands to deal with catching events, such as signals, exceptions,
11609 process start/exit, etc. */
c5aa993b
JM
11610
11611typedef enum
11612{
44feb3ce
TT
11613 catch_fork_temporary, catch_vfork_temporary,
11614 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11615}
11616catch_fork_kind;
11617
c906108c 11618static void
cc59ec59
MS
11619catch_fork_command_1 (char *arg, int from_tty,
11620 struct cmd_list_element *command)
c906108c 11621{
a6d9a66e 11622 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11623 char *cond_string = NULL;
44feb3ce
TT
11624 catch_fork_kind fork_kind;
11625 int tempflag;
11626
11627 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11628 tempflag = (fork_kind == catch_fork_temporary
11629 || fork_kind == catch_vfork_temporary);
c5aa993b 11630
44feb3ce
TT
11631 if (!arg)
11632 arg = "";
e9cafbcc 11633 arg = skip_spaces (arg);
c5aa993b 11634
c906108c 11635 /* The allowed syntax is:
c5aa993b
JM
11636 catch [v]fork
11637 catch [v]fork if <cond>
11638
4a64f543 11639 First, check if there's an if clause. */
c906108c 11640 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11641
c906108c 11642 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11643 error (_("Junk at end of arguments."));
c5aa993b 11644
c906108c 11645 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11646 and enable reporting of such events. */
c5aa993b
JM
11647 switch (fork_kind)
11648 {
44feb3ce
TT
11649 case catch_fork_temporary:
11650 case catch_fork_permanent:
a6d9a66e 11651 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11652 &catch_fork_breakpoint_ops);
c906108c 11653 break;
44feb3ce
TT
11654 case catch_vfork_temporary:
11655 case catch_vfork_permanent:
a6d9a66e 11656 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11657 &catch_vfork_breakpoint_ops);
c906108c 11658 break;
c5aa993b 11659 default:
8a3fe4f8 11660 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11661 break;
c5aa993b 11662 }
c906108c
SS
11663}
11664
11665static void
cc59ec59
MS
11666catch_exec_command_1 (char *arg, int from_tty,
11667 struct cmd_list_element *command)
c906108c 11668{
b4d90040 11669 struct exec_catchpoint *c;
a6d9a66e 11670 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11671 int tempflag;
c5aa993b 11672 char *cond_string = NULL;
c906108c 11673
44feb3ce
TT
11674 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11675
11676 if (!arg)
11677 arg = "";
e9cafbcc 11678 arg = skip_spaces (arg);
c906108c
SS
11679
11680 /* The allowed syntax is:
c5aa993b
JM
11681 catch exec
11682 catch exec if <cond>
c906108c 11683
4a64f543 11684 First, check if there's an if clause. */
c906108c
SS
11685 cond_string = ep_parse_optional_if_clause (&arg);
11686
11687 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11688 error (_("Junk at end of arguments."));
c906108c 11689
b4d90040
PA
11690 c = XNEW (struct exec_catchpoint);
11691 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11692 &catch_exec_breakpoint_ops);
11693 c->exec_pathname = NULL;
11694
3ea46bff 11695 install_breakpoint (0, &c->base, 1);
c906108c 11696}
c5aa993b 11697
9ac4176b 11698void
28010a5d
PA
11699init_ada_exception_breakpoint (struct breakpoint *b,
11700 struct gdbarch *gdbarch,
11701 struct symtab_and_line sal,
11702 char *addr_string,
c0a91b2b 11703 const struct breakpoint_ops *ops,
28010a5d 11704 int tempflag,
349774ef 11705 int enabled,
28010a5d 11706 int from_tty)
f7f9143b 11707{
f7f9143b
JB
11708 if (from_tty)
11709 {
5af949e3
UW
11710 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11711 if (!loc_gdbarch)
11712 loc_gdbarch = gdbarch;
11713
6c95b8df
PA
11714 describe_other_breakpoints (loc_gdbarch,
11715 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11716 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11717 version for exception catchpoints, because two catchpoints
11718 used for different exception names will use the same address.
11719 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11720 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11721 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11722 the user what type of catchpoint it is. The above is good
11723 enough for now, though. */
11724 }
11725
28010a5d 11726 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11727
349774ef 11728 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11729 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11730 b->addr_string = addr_string;
11731 b->language = language_ada;
f7f9143b
JB
11732}
11733
a96d9b2e
SDJ
11734/* Splits the argument using space as delimiter. Returns an xmalloc'd
11735 filter list, or NULL if no filtering is required. */
11736static VEC(int) *
11737catch_syscall_split_args (char *arg)
11738{
11739 VEC(int) *result = NULL;
29d0bb3d 11740 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
11741
11742 while (*arg != '\0')
11743 {
11744 int i, syscall_number;
11745 char *endptr;
11746 char cur_name[128];
11747 struct syscall s;
11748
11749 /* Skip whitespace. */
529480d0 11750 arg = skip_spaces (arg);
a96d9b2e
SDJ
11751
11752 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11753 cur_name[i] = arg[i];
11754 cur_name[i] = '\0';
11755 arg += i;
11756
11757 /* Check if the user provided a syscall name or a number. */
11758 syscall_number = (int) strtol (cur_name, &endptr, 0);
11759 if (*endptr == '\0')
bccd0dd2 11760 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
11761 else
11762 {
11763 /* We have a name. Let's check if it's valid and convert it
11764 to a number. */
11765 get_syscall_by_name (cur_name, &s);
11766
11767 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
11768 /* Here we have to issue an error instead of a warning,
11769 because GDB cannot do anything useful if there's no
11770 syscall number to be caught. */
a96d9b2e
SDJ
11771 error (_("Unknown syscall name '%s'."), cur_name);
11772 }
11773
11774 /* Ok, it's valid. */
11775 VEC_safe_push (int, result, s.number);
11776 }
11777
11778 discard_cleanups (cleanup);
11779 return result;
11780}
11781
11782/* Implement the "catch syscall" command. */
11783
11784static void
cc59ec59
MS
11785catch_syscall_command_1 (char *arg, int from_tty,
11786 struct cmd_list_element *command)
a96d9b2e
SDJ
11787{
11788 int tempflag;
11789 VEC(int) *filter;
11790 struct syscall s;
11791 struct gdbarch *gdbarch = get_current_arch ();
11792
11793 /* Checking if the feature if supported. */
11794 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11795 error (_("The feature 'catch syscall' is not supported on \
ea666128 11796this architecture yet."));
a96d9b2e
SDJ
11797
11798 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11799
e9cafbcc 11800 arg = skip_spaces (arg);
a96d9b2e
SDJ
11801
11802 /* We need to do this first "dummy" translation in order
11803 to get the syscall XML file loaded or, most important,
11804 to display a warning to the user if there's no XML file
11805 for his/her architecture. */
11806 get_syscall_by_number (0, &s);
11807
11808 /* The allowed syntax is:
11809 catch syscall
11810 catch syscall <name | number> [<name | number> ... <name | number>]
11811
11812 Let's check if there's a syscall name. */
11813
11814 if (arg != NULL)
11815 filter = catch_syscall_split_args (arg);
11816 else
11817 filter = NULL;
11818
11819 create_syscall_event_catchpoint (tempflag, filter,
11820 &catch_syscall_breakpoint_ops);
11821}
11822
c906108c 11823static void
fba45db2 11824catch_command (char *arg, int from_tty)
c906108c 11825{
44feb3ce 11826 error (_("Catch requires an event name."));
c906108c
SS
11827}
11828\f
11829
11830static void
fba45db2 11831tcatch_command (char *arg, int from_tty)
c906108c 11832{
44feb3ce 11833 error (_("Catch requires an event name."));
c906108c
SS
11834}
11835
8a2c437b
TT
11836/* A qsort comparison function that sorts breakpoints in order. */
11837
11838static int
11839compare_breakpoints (const void *a, const void *b)
11840{
11841 const breakpoint_p *ba = a;
11842 uintptr_t ua = (uintptr_t) *ba;
11843 const breakpoint_p *bb = b;
11844 uintptr_t ub = (uintptr_t) *bb;
11845
11846 if ((*ba)->number < (*bb)->number)
11847 return -1;
11848 else if ((*ba)->number > (*bb)->number)
11849 return 1;
11850
11851 /* Now sort by address, in case we see, e..g, two breakpoints with
11852 the number 0. */
11853 if (ua < ub)
11854 return -1;
94b0e70d 11855 return ua > ub ? 1 : 0;
8a2c437b
TT
11856}
11857
80f8a6eb 11858/* Delete breakpoints by address or line. */
c906108c
SS
11859
11860static void
fba45db2 11861clear_command (char *arg, int from_tty)
c906108c 11862{
8a2c437b 11863 struct breakpoint *b, *prev;
d6e956e5
VP
11864 VEC(breakpoint_p) *found = 0;
11865 int ix;
c906108c
SS
11866 int default_match;
11867 struct symtabs_and_lines sals;
11868 struct symtab_and_line sal;
c906108c 11869 int i;
8a2c437b 11870 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11871
11872 if (arg)
11873 {
39cf75f7
DE
11874 sals = decode_line_with_current_source (arg,
11875 (DECODE_LINE_FUNFIRSTLINE
11876 | DECODE_LINE_LIST_MODE));
cf4ded82 11877 make_cleanup (xfree, sals.sals);
c906108c
SS
11878 default_match = 0;
11879 }
11880 else
11881 {
c5aa993b 11882 sals.sals = (struct symtab_and_line *)
c906108c 11883 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 11884 make_cleanup (xfree, sals.sals);
4a64f543 11885 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11886
11887 /* Set sal's line, symtab, pc, and pspace to the values
11888 corresponding to the last call to print_frame_info. If the
11889 codepoint is not valid, this will set all the fields to 0. */
11890 get_last_displayed_sal (&sal);
c906108c 11891 if (sal.symtab == 0)
8a3fe4f8 11892 error (_("No source file specified."));
c906108c
SS
11893
11894 sals.sals[0] = sal;
11895 sals.nelts = 1;
11896
11897 default_match = 1;
11898 }
11899
4a64f543
MS
11900 /* We don't call resolve_sal_pc here. That's not as bad as it
11901 seems, because all existing breakpoints typically have both
11902 file/line and pc set. So, if clear is given file/line, we can
11903 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11904
11905 We only support clearing given the address explicitly
11906 present in breakpoint table. Say, we've set breakpoint
4a64f543 11907 at file:line. There were several PC values for that file:line,
ed0616c6 11908 due to optimization, all in one block.
4a64f543
MS
11909
11910 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11911 PC corresponding to the same file:line, the breakpoint won't
11912 be cleared. We probably can still clear the breakpoint, but
11913 since the other PC value is never presented to user, user
11914 can only find it by guessing, and it does not seem important
11915 to support that. */
11916
4a64f543
MS
11917 /* For each line spec given, delete bps which correspond to it. Do
11918 it in two passes, solely to preserve the current behavior that
11919 from_tty is forced true if we delete more than one
11920 breakpoint. */
c906108c 11921
80f8a6eb 11922 found = NULL;
8a2c437b 11923 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11924 for (i = 0; i < sals.nelts; i++)
11925 {
05cba821
JK
11926 const char *sal_fullname;
11927
c906108c 11928 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11929 If line given (pc == 0), clear all bpts on specified line.
11930 If defaulting, clear all bpts on default line
c906108c 11931 or at default pc.
c5aa993b
JM
11932
11933 defaulting sal.pc != 0 tests to do
11934
11935 0 1 pc
11936 1 1 pc _and_ line
11937 0 0 line
11938 1 0 <can't happen> */
c906108c
SS
11939
11940 sal = sals.sals[i];
05cba821
JK
11941 sal_fullname = (sal.symtab == NULL
11942 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11943
4a64f543 11944 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11945 ALL_BREAKPOINTS (b)
c5aa993b 11946 {
0d381245 11947 int match = 0;
4a64f543 11948 /* Are we going to delete b? */
cc60f2e3 11949 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11950 {
11951 struct bp_location *loc = b->loc;
11952 for (; loc; loc = loc->next)
11953 {
f8eba3c6
TT
11954 /* If the user specified file:line, don't allow a PC
11955 match. This matches historical gdb behavior. */
11956 int pc_match = (!sal.explicit_line
11957 && sal.pc
11958 && (loc->pspace == sal.pspace)
11959 && (loc->address == sal.pc)
11960 && (!section_is_overlay (loc->section)
11961 || loc->section == sal.section));
4aac40c8
TT
11962 int line_match = 0;
11963
11964 if ((default_match || sal.explicit_line)
2f202fde 11965 && loc->symtab != NULL
05cba821 11966 && sal_fullname != NULL
4aac40c8 11967 && sal.pspace == loc->pspace
05cba821
JK
11968 && loc->line_number == sal.line
11969 && filename_cmp (symtab_to_fullname (loc->symtab),
11970 sal_fullname) == 0)
11971 line_match = 1;
4aac40c8 11972
0d381245
VP
11973 if (pc_match || line_match)
11974 {
11975 match = 1;
11976 break;
11977 }
11978 }
11979 }
11980
11981 if (match)
d6e956e5 11982 VEC_safe_push(breakpoint_p, found, b);
c906108c 11983 }
80f8a6eb 11984 }
8a2c437b 11985
80f8a6eb 11986 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11987 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11988 {
11989 if (arg)
8a3fe4f8 11990 error (_("No breakpoint at %s."), arg);
80f8a6eb 11991 else
8a3fe4f8 11992 error (_("No breakpoint at this line."));
80f8a6eb 11993 }
c906108c 11994
8a2c437b
TT
11995 /* Remove duplicates from the vec. */
11996 qsort (VEC_address (breakpoint_p, found),
11997 VEC_length (breakpoint_p, found),
11998 sizeof (breakpoint_p),
11999 compare_breakpoints);
12000 prev = VEC_index (breakpoint_p, found, 0);
12001 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12002 {
12003 if (b == prev)
12004 {
12005 VEC_ordered_remove (breakpoint_p, found, ix);
12006 --ix;
12007 }
12008 }
12009
d6e956e5 12010 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12011 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12012 if (from_tty)
a3f17187 12013 {
d6e956e5 12014 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12015 printf_unfiltered (_("Deleted breakpoint "));
12016 else
12017 printf_unfiltered (_("Deleted breakpoints "));
12018 }
d6e956e5
VP
12019
12020 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12021 {
c5aa993b 12022 if (from_tty)
d6e956e5
VP
12023 printf_unfiltered ("%d ", b->number);
12024 delete_breakpoint (b);
c906108c 12025 }
80f8a6eb
MS
12026 if (from_tty)
12027 putchar_unfiltered ('\n');
8a2c437b
TT
12028
12029 do_cleanups (cleanups);
c906108c
SS
12030}
12031\f
12032/* Delete breakpoint in BS if they are `delete' breakpoints and
12033 all breakpoints that are marked for deletion, whether hit or not.
12034 This is called after any breakpoint is hit, or after errors. */
12035
12036void
fba45db2 12037breakpoint_auto_delete (bpstat bs)
c906108c 12038{
35df4500 12039 struct breakpoint *b, *b_tmp;
c906108c
SS
12040
12041 for (; bs; bs = bs->next)
f431efe5
PA
12042 if (bs->breakpoint_at
12043 && bs->breakpoint_at->disposition == disp_del
c906108c 12044 && bs->stop)
f431efe5 12045 delete_breakpoint (bs->breakpoint_at);
c906108c 12046
35df4500 12047 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12048 {
b5de0fa7 12049 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12050 delete_breakpoint (b);
12051 }
c906108c
SS
12052}
12053
4a64f543
MS
12054/* A comparison function for bp_location AP and BP being interfaced to
12055 qsort. Sort elements primarily by their ADDRESS (no matter what
12056 does breakpoint_address_is_meaningful say for its OWNER),
12057 secondarily by ordering first bp_permanent OWNERed elements and
12058 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12059 qsort being an unstable algorithm. */
876fa593
JK
12060
12061static int
494cfb0f 12062bp_location_compare (const void *ap, const void *bp)
876fa593 12063{
494cfb0f
JK
12064 struct bp_location *a = *(void **) ap;
12065 struct bp_location *b = *(void **) bp;
2bdf28a0 12066 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
12067 int a_perm = a->owner->enable_state == bp_permanent;
12068 int b_perm = b->owner->enable_state == bp_permanent;
12069
12070 if (a->address != b->address)
12071 return (a->address > b->address) - (a->address < b->address);
12072
dea2aa5f
LM
12073 /* Sort locations at the same address by their pspace number, keeping
12074 locations of the same inferior (in a multi-inferior environment)
12075 grouped. */
12076
12077 if (a->pspace->num != b->pspace->num)
12078 return ((a->pspace->num > b->pspace->num)
12079 - (a->pspace->num < b->pspace->num));
12080
876fa593
JK
12081 /* Sort permanent breakpoints first. */
12082 if (a_perm != b_perm)
12083 return (a_perm < b_perm) - (a_perm > b_perm);
12084
c56a97f9
JK
12085 /* Make the internal GDB representation stable across GDB runs
12086 where A and B memory inside GDB can differ. Breakpoint locations of
12087 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12088
12089 if (a->owner->number != b->owner->number)
c56a97f9
JK
12090 return ((a->owner->number > b->owner->number)
12091 - (a->owner->number < b->owner->number));
876fa593
JK
12092
12093 return (a > b) - (a < b);
12094}
12095
876fa593 12096/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12097 bp_location_shadow_len_after_address_max according to the current
12098 content of the bp_location array. */
f7545552
TT
12099
12100static void
876fa593 12101bp_location_target_extensions_update (void)
f7545552 12102{
876fa593
JK
12103 struct bp_location *bl, **blp_tmp;
12104
12105 bp_location_placed_address_before_address_max = 0;
12106 bp_location_shadow_len_after_address_max = 0;
12107
12108 ALL_BP_LOCATIONS (bl, blp_tmp)
12109 {
12110 CORE_ADDR start, end, addr;
12111
12112 if (!bp_location_has_shadow (bl))
12113 continue;
12114
12115 start = bl->target_info.placed_address;
12116 end = start + bl->target_info.shadow_len;
12117
12118 gdb_assert (bl->address >= start);
12119 addr = bl->address - start;
12120 if (addr > bp_location_placed_address_before_address_max)
12121 bp_location_placed_address_before_address_max = addr;
12122
12123 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12124
12125 gdb_assert (bl->address < end);
12126 addr = end - bl->address;
12127 if (addr > bp_location_shadow_len_after_address_max)
12128 bp_location_shadow_len_after_address_max = addr;
12129 }
f7545552
TT
12130}
12131
1e4d1764
YQ
12132/* Download tracepoint locations if they haven't been. */
12133
12134static void
12135download_tracepoint_locations (void)
12136{
7ed2c994 12137 struct breakpoint *b;
1e4d1764
YQ
12138 struct cleanup *old_chain;
12139
12140 if (!target_can_download_tracepoint ())
12141 return;
12142
12143 old_chain = save_current_space_and_thread ();
12144
7ed2c994 12145 ALL_TRACEPOINTS (b)
1e4d1764 12146 {
7ed2c994 12147 struct bp_location *bl;
1e4d1764 12148 struct tracepoint *t;
f2a8bc8a 12149 int bp_location_downloaded = 0;
1e4d1764 12150
7ed2c994 12151 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12152 ? !may_insert_fast_tracepoints
12153 : !may_insert_tracepoints))
12154 continue;
12155
7ed2c994
YQ
12156 for (bl = b->loc; bl; bl = bl->next)
12157 {
12158 /* In tracepoint, locations are _never_ duplicated, so
12159 should_be_inserted is equivalent to
12160 unduplicated_should_be_inserted. */
12161 if (!should_be_inserted (bl) || bl->inserted)
12162 continue;
1e4d1764 12163
7ed2c994 12164 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12165
7ed2c994 12166 target_download_tracepoint (bl);
1e4d1764 12167
7ed2c994 12168 bl->inserted = 1;
f2a8bc8a 12169 bp_location_downloaded = 1;
7ed2c994
YQ
12170 }
12171 t = (struct tracepoint *) b;
12172 t->number_on_target = b->number;
f2a8bc8a
YQ
12173 if (bp_location_downloaded)
12174 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12175 }
12176
12177 do_cleanups (old_chain);
12178}
12179
934709f0
PW
12180/* Swap the insertion/duplication state between two locations. */
12181
12182static void
12183swap_insertion (struct bp_location *left, struct bp_location *right)
12184{
12185 const int left_inserted = left->inserted;
12186 const int left_duplicate = left->duplicate;
b775012e 12187 const int left_needs_update = left->needs_update;
934709f0
PW
12188 const struct bp_target_info left_target_info = left->target_info;
12189
1e4d1764
YQ
12190 /* Locations of tracepoints can never be duplicated. */
12191 if (is_tracepoint (left->owner))
12192 gdb_assert (!left->duplicate);
12193 if (is_tracepoint (right->owner))
12194 gdb_assert (!right->duplicate);
12195
934709f0
PW
12196 left->inserted = right->inserted;
12197 left->duplicate = right->duplicate;
b775012e 12198 left->needs_update = right->needs_update;
934709f0
PW
12199 left->target_info = right->target_info;
12200 right->inserted = left_inserted;
12201 right->duplicate = left_duplicate;
b775012e 12202 right->needs_update = left_needs_update;
934709f0
PW
12203 right->target_info = left_target_info;
12204}
12205
b775012e
LM
12206/* Force the re-insertion of the locations at ADDRESS. This is called
12207 once a new/deleted/modified duplicate location is found and we are evaluating
12208 conditions on the target's side. Such conditions need to be updated on
12209 the target. */
12210
12211static void
12212force_breakpoint_reinsertion (struct bp_location *bl)
12213{
12214 struct bp_location **locp = NULL, **loc2p;
12215 struct bp_location *loc;
12216 CORE_ADDR address = 0;
12217 int pspace_num;
12218
12219 address = bl->address;
12220 pspace_num = bl->pspace->num;
12221
12222 /* This is only meaningful if the target is
12223 evaluating conditions and if the user has
12224 opted for condition evaluation on the target's
12225 side. */
12226 if (gdb_evaluates_breakpoint_condition_p ()
12227 || !target_supports_evaluation_of_breakpoint_conditions ())
12228 return;
12229
12230 /* Flag all breakpoint locations with this address and
12231 the same program space as the location
12232 as "its condition has changed". We need to
12233 update the conditions on the target's side. */
12234 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12235 {
12236 loc = *loc2p;
12237
12238 if (!is_breakpoint (loc->owner)
12239 || pspace_num != loc->pspace->num)
12240 continue;
12241
12242 /* Flag the location appropriately. We use a different state to
12243 let everyone know that we already updated the set of locations
12244 with addr bl->address and program space bl->pspace. This is so
12245 we don't have to keep calling these functions just to mark locations
12246 that have already been marked. */
12247 loc->condition_changed = condition_updated;
12248
12249 /* Free the agent expression bytecode as well. We will compute
12250 it later on. */
12251 if (loc->cond_bytecode)
12252 {
12253 free_agent_expr (loc->cond_bytecode);
12254 loc->cond_bytecode = NULL;
12255 }
12256 }
12257}
12258
4cd9bd08 12259/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
12260 into the inferior, only remove already-inserted locations that no
12261 longer should be inserted. Functions that delete a breakpoint or
12262 breakpoints should pass false, so that deleting a breakpoint
12263 doesn't have the side effect of inserting the locations of other
12264 breakpoints that are marked not-inserted, but should_be_inserted
12265 returns true on them.
12266
12267 This behaviour is useful is situations close to tear-down -- e.g.,
12268 after an exec, while the target still has execution, but breakpoint
12269 shadows of the previous executable image should *NOT* be restored
12270 to the new image; or before detaching, where the target still has
12271 execution and wants to delete breakpoints from GDB's lists, and all
12272 breakpoints had already been removed from the inferior. */
12273
0d381245 12274static void
b60e7edf 12275update_global_location_list (int should_insert)
0d381245 12276{
74960c60 12277 struct breakpoint *b;
876fa593 12278 struct bp_location **locp, *loc;
f7545552 12279 struct cleanup *cleanups;
b775012e
LM
12280 /* Last breakpoint location address that was marked for update. */
12281 CORE_ADDR last_addr = 0;
12282 /* Last breakpoint location program space that was marked for update. */
12283 int last_pspace_num = -1;
f7545552 12284
2d134ed3
PA
12285 /* Used in the duplicates detection below. When iterating over all
12286 bp_locations, points to the first bp_location of a given address.
12287 Breakpoints and watchpoints of different types are never
12288 duplicates of each other. Keep one pointer for each type of
12289 breakpoint/watchpoint, so we only need to loop over all locations
12290 once. */
12291 struct bp_location *bp_loc_first; /* breakpoint */
12292 struct bp_location *wp_loc_first; /* hardware watchpoint */
12293 struct bp_location *awp_loc_first; /* access watchpoint */
12294 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12295
4a64f543
MS
12296 /* Saved former bp_location array which we compare against the newly
12297 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12298 struct bp_location **old_location, **old_locp;
12299 unsigned old_location_count;
12300
12301 old_location = bp_location;
12302 old_location_count = bp_location_count;
12303 bp_location = NULL;
12304 bp_location_count = 0;
12305 cleanups = make_cleanup (xfree, old_location);
0d381245 12306
74960c60 12307 ALL_BREAKPOINTS (b)
876fa593
JK
12308 for (loc = b->loc; loc; loc = loc->next)
12309 bp_location_count++;
12310
12311 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12312 locp = bp_location;
12313 ALL_BREAKPOINTS (b)
12314 for (loc = b->loc; loc; loc = loc->next)
12315 *locp++ = loc;
12316 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12317 bp_location_compare);
876fa593
JK
12318
12319 bp_location_target_extensions_update ();
74960c60 12320
4a64f543
MS
12321 /* Identify bp_location instances that are no longer present in the
12322 new list, and therefore should be freed. Note that it's not
12323 necessary that those locations should be removed from inferior --
12324 if there's another location at the same address (previously
12325 marked as duplicate), we don't need to remove/insert the
12326 location.
876fa593 12327
4a64f543
MS
12328 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12329 and former bp_location array state respectively. */
876fa593
JK
12330
12331 locp = bp_location;
12332 for (old_locp = old_location; old_locp < old_location + old_location_count;
12333 old_locp++)
74960c60 12334 {
876fa593 12335 struct bp_location *old_loc = *old_locp;
c7d46a38 12336 struct bp_location **loc2p;
876fa593 12337
e5dd4106 12338 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12339 not, we have to free it. */
c7d46a38 12340 int found_object = 0;
20874c92
VP
12341 /* Tells if the location should remain inserted in the target. */
12342 int keep_in_target = 0;
12343 int removed = 0;
876fa593 12344
4a64f543
MS
12345 /* Skip LOCP entries which will definitely never be needed.
12346 Stop either at or being the one matching OLD_LOC. */
876fa593 12347 while (locp < bp_location + bp_location_count
c7d46a38 12348 && (*locp)->address < old_loc->address)
876fa593 12349 locp++;
c7d46a38
PA
12350
12351 for (loc2p = locp;
12352 (loc2p < bp_location + bp_location_count
12353 && (*loc2p)->address == old_loc->address);
12354 loc2p++)
12355 {
b775012e
LM
12356 /* Check if this is a new/duplicated location or a duplicated
12357 location that had its condition modified. If so, we want to send
12358 its condition to the target if evaluation of conditions is taking
12359 place there. */
12360 if ((*loc2p)->condition_changed == condition_modified
12361 && (last_addr != old_loc->address
12362 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12363 {
b775012e
LM
12364 force_breakpoint_reinsertion (*loc2p);
12365 last_pspace_num = old_loc->pspace->num;
c7d46a38 12366 }
b775012e
LM
12367
12368 if (*loc2p == old_loc)
12369 found_object = 1;
c7d46a38 12370 }
74960c60 12371
b775012e
LM
12372 /* We have already handled this address, update it so that we don't
12373 have to go through updates again. */
12374 last_addr = old_loc->address;
12375
12376 /* Target-side condition evaluation: Handle deleted locations. */
12377 if (!found_object)
12378 force_breakpoint_reinsertion (old_loc);
12379
4a64f543
MS
12380 /* If this location is no longer present, and inserted, look if
12381 there's maybe a new location at the same address. If so,
12382 mark that one inserted, and don't remove this one. This is
12383 needed so that we don't have a time window where a breakpoint
12384 at certain location is not inserted. */
74960c60 12385
876fa593 12386 if (old_loc->inserted)
0d381245 12387 {
4a64f543
MS
12388 /* If the location is inserted now, we might have to remove
12389 it. */
74960c60 12390
876fa593 12391 if (found_object && should_be_inserted (old_loc))
74960c60 12392 {
4a64f543
MS
12393 /* The location is still present in the location list,
12394 and still should be inserted. Don't do anything. */
20874c92 12395 keep_in_target = 1;
74960c60
VP
12396 }
12397 else
12398 {
b775012e
LM
12399 /* This location still exists, but it won't be kept in the
12400 target since it may have been disabled. We proceed to
12401 remove its target-side condition. */
12402
4a64f543
MS
12403 /* The location is either no longer present, or got
12404 disabled. See if there's another location at the
12405 same address, in which case we don't need to remove
12406 this one from the target. */
876fa593 12407
2bdf28a0 12408 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12409 if (breakpoint_address_is_meaningful (old_loc->owner))
12410 {
876fa593 12411 for (loc2p = locp;
c7d46a38
PA
12412 (loc2p < bp_location + bp_location_count
12413 && (*loc2p)->address == old_loc->address);
876fa593
JK
12414 loc2p++)
12415 {
12416 struct bp_location *loc2 = *loc2p;
12417
2d134ed3 12418 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12419 {
85d721b8
PA
12420 /* Read watchpoint locations are switched to
12421 access watchpoints, if the former are not
12422 supported, but the latter are. */
12423 if (is_hardware_watchpoint (old_loc->owner))
12424 {
12425 gdb_assert (is_hardware_watchpoint (loc2->owner));
12426 loc2->watchpoint_type = old_loc->watchpoint_type;
12427 }
12428
934709f0
PW
12429 /* loc2 is a duplicated location. We need to check
12430 if it should be inserted in case it will be
12431 unduplicated. */
12432 if (loc2 != old_loc
12433 && unduplicated_should_be_inserted (loc2))
c7d46a38 12434 {
934709f0 12435 swap_insertion (old_loc, loc2);
c7d46a38
PA
12436 keep_in_target = 1;
12437 break;
12438 }
876fa593
JK
12439 }
12440 }
12441 }
74960c60
VP
12442 }
12443
20874c92
VP
12444 if (!keep_in_target)
12445 {
876fa593 12446 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12447 {
4a64f543
MS
12448 /* This is just about all we can do. We could keep
12449 this location on the global list, and try to
12450 remove it next time, but there's no particular
12451 reason why we will succeed next time.
20874c92 12452
4a64f543
MS
12453 Note that at this point, old_loc->owner is still
12454 valid, as delete_breakpoint frees the breakpoint
12455 only after calling us. */
3e43a32a
MS
12456 printf_filtered (_("warning: Error removing "
12457 "breakpoint %d\n"),
876fa593 12458 old_loc->owner->number);
20874c92
VP
12459 }
12460 removed = 1;
12461 }
0d381245 12462 }
74960c60
VP
12463
12464 if (!found_object)
1c5cfe86 12465 {
db82e815
PA
12466 if (removed && non_stop
12467 && breakpoint_address_is_meaningful (old_loc->owner)
12468 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12469 {
db82e815
PA
12470 /* This location was removed from the target. In
12471 non-stop mode, a race condition is possible where
12472 we've removed a breakpoint, but stop events for that
12473 breakpoint are already queued and will arrive later.
12474 We apply an heuristic to be able to distinguish such
12475 SIGTRAPs from other random SIGTRAPs: we keep this
12476 breakpoint location for a bit, and will retire it
12477 after we see some number of events. The theory here
12478 is that reporting of events should, "on the average",
12479 be fair, so after a while we'll see events from all
12480 threads that have anything of interest, and no longer
12481 need to keep this breakpoint location around. We
12482 don't hold locations forever so to reduce chances of
12483 mistaking a non-breakpoint SIGTRAP for a breakpoint
12484 SIGTRAP.
12485
12486 The heuristic failing can be disastrous on
12487 decr_pc_after_break targets.
12488
12489 On decr_pc_after_break targets, like e.g., x86-linux,
12490 if we fail to recognize a late breakpoint SIGTRAP,
12491 because events_till_retirement has reached 0 too
12492 soon, we'll fail to do the PC adjustment, and report
12493 a random SIGTRAP to the user. When the user resumes
12494 the inferior, it will most likely immediately crash
2dec564e 12495 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12496 corrupted, because of being resumed e.g., in the
12497 middle of a multi-byte instruction, or skipped a
12498 one-byte instruction. This was actually seen happen
12499 on native x86-linux, and should be less rare on
12500 targets that do not support new thread events, like
12501 remote, due to the heuristic depending on
12502 thread_count.
12503
12504 Mistaking a random SIGTRAP for a breakpoint trap
12505 causes similar symptoms (PC adjustment applied when
12506 it shouldn't), but then again, playing with SIGTRAPs
12507 behind the debugger's back is asking for trouble.
12508
12509 Since hardware watchpoint traps are always
12510 distinguishable from other traps, so we don't need to
12511 apply keep hardware watchpoint moribund locations
12512 around. We simply always ignore hardware watchpoint
12513 traps we can no longer explain. */
12514
876fa593
JK
12515 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12516 old_loc->owner = NULL;
20874c92 12517
876fa593 12518 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12519 }
12520 else
f431efe5
PA
12521 {
12522 old_loc->owner = NULL;
12523 decref_bp_location (&old_loc);
12524 }
20874c92 12525 }
74960c60 12526 }
1c5cfe86 12527
348d480f
PA
12528 /* Rescan breakpoints at the same address and section, marking the
12529 first one as "first" and any others as "duplicates". This is so
12530 that the bpt instruction is only inserted once. If we have a
12531 permanent breakpoint at the same place as BPT, make that one the
12532 official one, and the rest as duplicates. Permanent breakpoints
12533 are sorted first for the same address.
12534
12535 Do the same for hardware watchpoints, but also considering the
12536 watchpoint's type (regular/access/read) and length. */
12537
12538 bp_loc_first = NULL;
12539 wp_loc_first = NULL;
12540 awp_loc_first = NULL;
12541 rwp_loc_first = NULL;
12542 ALL_BP_LOCATIONS (loc, locp)
12543 {
12544 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12545 non-NULL. */
348d480f 12546 struct bp_location **loc_first_p;
d3fbdd86 12547 b = loc->owner;
348d480f 12548
6f380991 12549 if (!unduplicated_should_be_inserted (loc)
348d480f 12550 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12551 /* Don't detect duplicate for tracepoint locations because they are
12552 never duplicated. See the comments in field `duplicate' of
12553 `struct bp_location'. */
348d480f 12554 || is_tracepoint (b))
b775012e
LM
12555 {
12556 /* Clear the condition modification flag. */
12557 loc->condition_changed = condition_unchanged;
12558 continue;
12559 }
348d480f
PA
12560
12561 /* Permanent breakpoint should always be inserted. */
12562 if (b->enable_state == bp_permanent && ! loc->inserted)
12563 internal_error (__FILE__, __LINE__,
12564 _("allegedly permanent breakpoint is not "
12565 "actually inserted"));
12566
12567 if (b->type == bp_hardware_watchpoint)
12568 loc_first_p = &wp_loc_first;
12569 else if (b->type == bp_read_watchpoint)
12570 loc_first_p = &rwp_loc_first;
12571 else if (b->type == bp_access_watchpoint)
12572 loc_first_p = &awp_loc_first;
12573 else
12574 loc_first_p = &bp_loc_first;
12575
12576 if (*loc_first_p == NULL
12577 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12578 || !breakpoint_locations_match (loc, *loc_first_p))
12579 {
12580 *loc_first_p = loc;
12581 loc->duplicate = 0;
b775012e
LM
12582
12583 if (is_breakpoint (loc->owner) && loc->condition_changed)
12584 {
12585 loc->needs_update = 1;
12586 /* Clear the condition modification flag. */
12587 loc->condition_changed = condition_unchanged;
12588 }
348d480f
PA
12589 continue;
12590 }
12591
934709f0
PW
12592
12593 /* This and the above ensure the invariant that the first location
12594 is not duplicated, and is the inserted one.
12595 All following are marked as duplicated, and are not inserted. */
12596 if (loc->inserted)
12597 swap_insertion (loc, *loc_first_p);
348d480f
PA
12598 loc->duplicate = 1;
12599
b775012e
LM
12600 /* Clear the condition modification flag. */
12601 loc->condition_changed = condition_unchanged;
12602
348d480f
PA
12603 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12604 && b->enable_state != bp_permanent)
12605 internal_error (__FILE__, __LINE__,
12606 _("another breakpoint was inserted on top of "
12607 "a permanent breakpoint"));
12608 }
12609
b775012e 12610 if (breakpoints_always_inserted_mode ()
348d480f 12611 && (have_live_inferiors ()
f5656ead 12612 || (gdbarch_has_global_breakpoints (target_gdbarch ()))))
b775012e
LM
12613 {
12614 if (should_insert)
12615 insert_breakpoint_locations ();
12616 else
12617 {
12618 /* Though should_insert is false, we may need to update conditions
12619 on the target's side if it is evaluating such conditions. We
12620 only update conditions for locations that are marked
12621 "needs_update". */
12622 update_inserted_breakpoint_locations ();
12623 }
12624 }
348d480f 12625
1e4d1764
YQ
12626 if (should_insert)
12627 download_tracepoint_locations ();
12628
348d480f
PA
12629 do_cleanups (cleanups);
12630}
12631
12632void
12633breakpoint_retire_moribund (void)
12634{
12635 struct bp_location *loc;
12636 int ix;
12637
12638 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12639 if (--(loc->events_till_retirement) == 0)
12640 {
12641 decref_bp_location (&loc);
12642 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12643 --ix;
12644 }
12645}
12646
12647static void
12648update_global_location_list_nothrow (int inserting)
12649{
bfd189b1 12650 volatile struct gdb_exception e;
348d480f
PA
12651
12652 TRY_CATCH (e, RETURN_MASK_ERROR)
12653 update_global_location_list (inserting);
12654}
12655
12656/* Clear BKP from a BPS. */
12657
12658static void
12659bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12660{
12661 bpstat bs;
12662
12663 for (bs = bps; bs; bs = bs->next)
12664 if (bs->breakpoint_at == bpt)
12665 {
12666 bs->breakpoint_at = NULL;
12667 bs->old_val = NULL;
12668 /* bs->commands will be freed later. */
12669 }
12670}
12671
12672/* Callback for iterate_over_threads. */
12673static int
12674bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12675{
12676 struct breakpoint *bpt = data;
12677
12678 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12679 return 0;
12680}
12681
12682/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12683 callbacks. */
12684
12685static void
12686say_where (struct breakpoint *b)
12687{
12688 struct value_print_options opts;
12689
12690 get_user_print_options (&opts);
12691
12692 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12693 single string. */
12694 if (b->loc == NULL)
12695 {
12696 printf_filtered (_(" (%s) pending."), b->addr_string);
12697 }
12698 else
12699 {
2f202fde 12700 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12701 {
12702 printf_filtered (" at ");
12703 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12704 gdb_stdout);
12705 }
2f202fde 12706 if (b->loc->symtab != NULL)
f8eba3c6
TT
12707 {
12708 /* If there is a single location, we can print the location
12709 more nicely. */
12710 if (b->loc->next == NULL)
12711 printf_filtered (": file %s, line %d.",
05cba821
JK
12712 symtab_to_filename_for_display (b->loc->symtab),
12713 b->loc->line_number);
f8eba3c6
TT
12714 else
12715 /* This is not ideal, but each location may have a
12716 different file name, and this at least reflects the
12717 real situation somewhat. */
12718 printf_filtered (": %s.", b->addr_string);
12719 }
348d480f
PA
12720
12721 if (b->loc->next)
12722 {
12723 struct bp_location *loc = b->loc;
12724 int n = 0;
12725 for (; loc; loc = loc->next)
12726 ++n;
12727 printf_filtered (" (%d locations)", n);
12728 }
12729 }
12730}
12731
348d480f
PA
12732/* Default bp_location_ops methods. */
12733
12734static void
12735bp_location_dtor (struct bp_location *self)
12736{
12737 xfree (self->cond);
b775012e
LM
12738 if (self->cond_bytecode)
12739 free_agent_expr (self->cond_bytecode);
348d480f 12740 xfree (self->function_name);
8b4f3082
PA
12741
12742 VEC_free (agent_expr_p, self->target_info.conditions);
12743 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12744}
12745
12746static const struct bp_location_ops bp_location_ops =
12747{
12748 bp_location_dtor
12749};
12750
2060206e
PA
12751/* Default breakpoint_ops methods all breakpoint_ops ultimately
12752 inherit from. */
348d480f 12753
2060206e
PA
12754static void
12755base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12756{
12757 decref_counted_command_line (&self->commands);
12758 xfree (self->cond_string);
fb81d016 12759 xfree (self->extra_string);
348d480f 12760 xfree (self->addr_string);
f8eba3c6 12761 xfree (self->filter);
348d480f 12762 xfree (self->addr_string_range_end);
348d480f
PA
12763}
12764
2060206e
PA
12765static struct bp_location *
12766base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12767{
12768 struct bp_location *loc;
12769
12770 loc = XNEW (struct bp_location);
12771 init_bp_location (loc, &bp_location_ops, self);
12772 return loc;
12773}
12774
2060206e
PA
12775static void
12776base_breakpoint_re_set (struct breakpoint *b)
12777{
12778 /* Nothing to re-set. */
12779}
12780
12781#define internal_error_pure_virtual_called() \
12782 gdb_assert_not_reached ("pure virtual function called")
12783
12784static int
12785base_breakpoint_insert_location (struct bp_location *bl)
12786{
12787 internal_error_pure_virtual_called ();
12788}
12789
12790static int
12791base_breakpoint_remove_location (struct bp_location *bl)
12792{
12793 internal_error_pure_virtual_called ();
12794}
12795
12796static int
12797base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12798 struct address_space *aspace,
09ac7c10
TT
12799 CORE_ADDR bp_addr,
12800 const struct target_waitstatus *ws)
2060206e
PA
12801{
12802 internal_error_pure_virtual_called ();
12803}
12804
12805static void
12806base_breakpoint_check_status (bpstat bs)
12807{
12808 /* Always stop. */
12809}
12810
12811/* A "works_in_software_mode" breakpoint_ops method that just internal
12812 errors. */
12813
12814static int
12815base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12816{
12817 internal_error_pure_virtual_called ();
12818}
12819
12820/* A "resources_needed" breakpoint_ops method that just internal
12821 errors. */
12822
12823static int
12824base_breakpoint_resources_needed (const struct bp_location *bl)
12825{
12826 internal_error_pure_virtual_called ();
12827}
12828
12829static enum print_stop_action
12830base_breakpoint_print_it (bpstat bs)
12831{
12832 internal_error_pure_virtual_called ();
12833}
12834
12835static void
12836base_breakpoint_print_one_detail (const struct breakpoint *self,
12837 struct ui_out *uiout)
12838{
12839 /* nothing */
12840}
12841
12842static void
12843base_breakpoint_print_mention (struct breakpoint *b)
12844{
12845 internal_error_pure_virtual_called ();
12846}
12847
12848static void
12849base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12850{
12851 internal_error_pure_virtual_called ();
12852}
12853
983af33b
SDJ
12854static void
12855base_breakpoint_create_sals_from_address (char **arg,
12856 struct linespec_result *canonical,
12857 enum bptype type_wanted,
12858 char *addr_start,
12859 char **copy_arg)
12860{
12861 internal_error_pure_virtual_called ();
12862}
12863
12864static void
12865base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12866 struct linespec_result *c,
983af33b 12867 char *cond_string,
e7e0cddf 12868 char *extra_string,
983af33b
SDJ
12869 enum bptype type_wanted,
12870 enum bpdisp disposition,
12871 int thread,
12872 int task, int ignore_count,
12873 const struct breakpoint_ops *o,
12874 int from_tty, int enabled,
44f238bb 12875 int internal, unsigned flags)
983af33b
SDJ
12876{
12877 internal_error_pure_virtual_called ();
12878}
12879
12880static void
12881base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12882 struct symtabs_and_lines *sals)
12883{
12884 internal_error_pure_virtual_called ();
12885}
12886
ab04a2af
TT
12887/* The default 'explains_signal' method. */
12888
47591c29 12889static int
427cd150 12890base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12891{
47591c29 12892 return 1;
ab04a2af
TT
12893}
12894
9d6e6e84
HZ
12895/* The default "after_condition_true" method. */
12896
12897static void
12898base_breakpoint_after_condition_true (struct bpstats *bs)
12899{
12900 /* Nothing to do. */
12901}
12902
ab04a2af 12903struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
12904{
12905 base_breakpoint_dtor,
12906 base_breakpoint_allocate_location,
12907 base_breakpoint_re_set,
12908 base_breakpoint_insert_location,
12909 base_breakpoint_remove_location,
12910 base_breakpoint_breakpoint_hit,
12911 base_breakpoint_check_status,
12912 base_breakpoint_resources_needed,
12913 base_breakpoint_works_in_software_mode,
12914 base_breakpoint_print_it,
12915 NULL,
12916 base_breakpoint_print_one_detail,
12917 base_breakpoint_print_mention,
983af33b
SDJ
12918 base_breakpoint_print_recreate,
12919 base_breakpoint_create_sals_from_address,
12920 base_breakpoint_create_breakpoints_sal,
12921 base_breakpoint_decode_linespec,
9d6e6e84
HZ
12922 base_breakpoint_explains_signal,
12923 base_breakpoint_after_condition_true,
2060206e
PA
12924};
12925
12926/* Default breakpoint_ops methods. */
12927
12928static void
348d480f
PA
12929bkpt_re_set (struct breakpoint *b)
12930{
06edf0c0
PA
12931 /* FIXME: is this still reachable? */
12932 if (b->addr_string == NULL)
12933 {
12934 /* Anything without a string can't be re-set. */
348d480f 12935 delete_breakpoint (b);
06edf0c0 12936 return;
348d480f 12937 }
06edf0c0
PA
12938
12939 breakpoint_re_set_default (b);
348d480f
PA
12940}
12941
2060206e 12942static int
348d480f
PA
12943bkpt_insert_location (struct bp_location *bl)
12944{
12945 if (bl->loc_type == bp_loc_hardware_breakpoint)
12946 return target_insert_hw_breakpoint (bl->gdbarch,
12947 &bl->target_info);
12948 else
12949 return target_insert_breakpoint (bl->gdbarch,
12950 &bl->target_info);
12951}
12952
2060206e 12953static int
348d480f
PA
12954bkpt_remove_location (struct bp_location *bl)
12955{
12956 if (bl->loc_type == bp_loc_hardware_breakpoint)
12957 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12958 else
12959 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12960}
12961
2060206e 12962static int
348d480f 12963bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12964 struct address_space *aspace, CORE_ADDR bp_addr,
12965 const struct target_waitstatus *ws)
348d480f 12966{
09ac7c10 12967 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12968 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12969 return 0;
12970
348d480f
PA
12971 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12972 aspace, bp_addr))
12973 return 0;
12974
12975 if (overlay_debugging /* unmapped overlay section */
12976 && section_is_overlay (bl->section)
12977 && !section_is_mapped (bl->section))
12978 return 0;
12979
12980 return 1;
12981}
12982
2060206e 12983static int
348d480f
PA
12984bkpt_resources_needed (const struct bp_location *bl)
12985{
12986 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12987
12988 return 1;
12989}
12990
2060206e 12991static enum print_stop_action
348d480f
PA
12992bkpt_print_it (bpstat bs)
12993{
348d480f
PA
12994 struct breakpoint *b;
12995 const struct bp_location *bl;
001c8c33 12996 int bp_temp;
79a45e25 12997 struct ui_out *uiout = current_uiout;
348d480f
PA
12998
12999 gdb_assert (bs->bp_location_at != NULL);
13000
13001 bl = bs->bp_location_at;
13002 b = bs->breakpoint_at;
13003
001c8c33
PA
13004 bp_temp = b->disposition == disp_del;
13005 if (bl->address != bl->requested_address)
13006 breakpoint_adjustment_warning (bl->requested_address,
13007 bl->address,
13008 b->number, 1);
13009 annotate_breakpoint (b->number);
13010 if (bp_temp)
13011 ui_out_text (uiout, "\nTemporary breakpoint ");
13012 else
13013 ui_out_text (uiout, "\nBreakpoint ");
13014 if (ui_out_is_mi_like_p (uiout))
348d480f 13015 {
001c8c33
PA
13016 ui_out_field_string (uiout, "reason",
13017 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13018 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13019 }
001c8c33
PA
13020 ui_out_field_int (uiout, "bkptno", b->number);
13021 ui_out_text (uiout, ", ");
06edf0c0 13022
001c8c33 13023 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13024}
13025
2060206e 13026static void
06edf0c0
PA
13027bkpt_print_mention (struct breakpoint *b)
13028{
79a45e25 13029 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13030 return;
13031
13032 switch (b->type)
13033 {
13034 case bp_breakpoint:
13035 case bp_gnu_ifunc_resolver:
13036 if (b->disposition == disp_del)
13037 printf_filtered (_("Temporary breakpoint"));
13038 else
13039 printf_filtered (_("Breakpoint"));
13040 printf_filtered (_(" %d"), b->number);
13041 if (b->type == bp_gnu_ifunc_resolver)
13042 printf_filtered (_(" at gnu-indirect-function resolver"));
13043 break;
13044 case bp_hardware_breakpoint:
13045 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13046 break;
e7e0cddf
SS
13047 case bp_dprintf:
13048 printf_filtered (_("Dprintf %d"), b->number);
13049 break;
06edf0c0
PA
13050 }
13051
13052 say_where (b);
13053}
13054
2060206e 13055static void
06edf0c0
PA
13056bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13057{
13058 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13059 fprintf_unfiltered (fp, "tbreak");
13060 else if (tp->type == bp_breakpoint)
13061 fprintf_unfiltered (fp, "break");
13062 else if (tp->type == bp_hardware_breakpoint
13063 && tp->disposition == disp_del)
13064 fprintf_unfiltered (fp, "thbreak");
13065 else if (tp->type == bp_hardware_breakpoint)
13066 fprintf_unfiltered (fp, "hbreak");
13067 else
13068 internal_error (__FILE__, __LINE__,
13069 _("unhandled breakpoint type %d"), (int) tp->type);
13070
2060206e 13071 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13072 print_recreate_thread (tp, fp);
06edf0c0
PA
13073}
13074
983af33b
SDJ
13075static void
13076bkpt_create_sals_from_address (char **arg,
13077 struct linespec_result *canonical,
13078 enum bptype type_wanted,
13079 char *addr_start, char **copy_arg)
13080{
13081 create_sals_from_address_default (arg, canonical, type_wanted,
13082 addr_start, copy_arg);
13083}
13084
13085static void
13086bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13087 struct linespec_result *canonical,
983af33b 13088 char *cond_string,
e7e0cddf 13089 char *extra_string,
983af33b
SDJ
13090 enum bptype type_wanted,
13091 enum bpdisp disposition,
13092 int thread,
13093 int task, int ignore_count,
13094 const struct breakpoint_ops *ops,
13095 int from_tty, int enabled,
44f238bb 13096 int internal, unsigned flags)
983af33b 13097{
023fa29b 13098 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13099 cond_string, extra_string,
13100 type_wanted,
983af33b
SDJ
13101 disposition, thread, task,
13102 ignore_count, ops, from_tty,
44f238bb 13103 enabled, internal, flags);
983af33b
SDJ
13104}
13105
13106static void
13107bkpt_decode_linespec (struct breakpoint *b, char **s,
13108 struct symtabs_and_lines *sals)
13109{
13110 decode_linespec_default (b, s, sals);
13111}
13112
06edf0c0
PA
13113/* Virtual table for internal breakpoints. */
13114
13115static void
13116internal_bkpt_re_set (struct breakpoint *b)
13117{
13118 switch (b->type)
13119 {
13120 /* Delete overlay event and longjmp master breakpoints; they
13121 will be reset later by breakpoint_re_set. */
13122 case bp_overlay_event:
13123 case bp_longjmp_master:
13124 case bp_std_terminate_master:
13125 case bp_exception_master:
13126 delete_breakpoint (b);
13127 break;
13128
13129 /* This breakpoint is special, it's set up when the inferior
13130 starts and we really don't want to touch it. */
13131 case bp_shlib_event:
13132
13133 /* Like bp_shlib_event, this breakpoint type is special. Once
13134 it is set up, we do not want to touch it. */
13135 case bp_thread_event:
13136 break;
13137 }
13138}
13139
13140static void
13141internal_bkpt_check_status (bpstat bs)
13142{
a9b3a50f
PA
13143 if (bs->breakpoint_at->type == bp_shlib_event)
13144 {
13145 /* If requested, stop when the dynamic linker notifies GDB of
13146 events. This allows the user to get control and place
13147 breakpoints in initializer routines for dynamically loaded
13148 objects (among other things). */
13149 bs->stop = stop_on_solib_events;
13150 bs->print = stop_on_solib_events;
13151 }
13152 else
13153 bs->stop = 0;
06edf0c0
PA
13154}
13155
13156static enum print_stop_action
13157internal_bkpt_print_it (bpstat bs)
13158{
06edf0c0 13159 struct breakpoint *b;
06edf0c0 13160
06edf0c0
PA
13161 b = bs->breakpoint_at;
13162
06edf0c0
PA
13163 switch (b->type)
13164 {
348d480f
PA
13165 case bp_shlib_event:
13166 /* Did we stop because the user set the stop_on_solib_events
13167 variable? (If so, we report this as a generic, "Stopped due
13168 to shlib event" message.) */
edcc5120 13169 print_solib_event (0);
348d480f
PA
13170 break;
13171
13172 case bp_thread_event:
13173 /* Not sure how we will get here.
13174 GDB should not stop for these breakpoints. */
13175 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13176 break;
13177
13178 case bp_overlay_event:
13179 /* By analogy with the thread event, GDB should not stop for these. */
13180 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13181 break;
13182
13183 case bp_longjmp_master:
13184 /* These should never be enabled. */
13185 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13186 break;
13187
13188 case bp_std_terminate_master:
13189 /* These should never be enabled. */
13190 printf_filtered (_("std::terminate Master Breakpoint: "
13191 "gdb should not stop!\n"));
348d480f
PA
13192 break;
13193
13194 case bp_exception_master:
13195 /* These should never be enabled. */
13196 printf_filtered (_("Exception Master Breakpoint: "
13197 "gdb should not stop!\n"));
06edf0c0
PA
13198 break;
13199 }
13200
001c8c33 13201 return PRINT_NOTHING;
06edf0c0
PA
13202}
13203
13204static void
13205internal_bkpt_print_mention (struct breakpoint *b)
13206{
13207 /* Nothing to mention. These breakpoints are internal. */
13208}
13209
06edf0c0
PA
13210/* Virtual table for momentary breakpoints */
13211
13212static void
13213momentary_bkpt_re_set (struct breakpoint *b)
13214{
13215 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13216 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13217 Otherwise these should have been blown away via the cleanup chain
13218 or by breakpoint_init_inferior when we rerun the executable. */
13219}
13220
13221static void
13222momentary_bkpt_check_status (bpstat bs)
13223{
13224 /* Nothing. The point of these breakpoints is causing a stop. */
13225}
13226
13227static enum print_stop_action
13228momentary_bkpt_print_it (bpstat bs)
13229{
79a45e25
PA
13230 struct ui_out *uiout = current_uiout;
13231
001c8c33 13232 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13233 {
001c8c33 13234 struct breakpoint *b = bs->breakpoint_at;
348d480f 13235
001c8c33
PA
13236 switch (b->type)
13237 {
13238 case bp_finish:
13239 ui_out_field_string
13240 (uiout, "reason",
13241 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13242 break;
348d480f 13243
001c8c33
PA
13244 case bp_until:
13245 ui_out_field_string
13246 (uiout, "reason",
13247 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13248 break;
13249 }
348d480f
PA
13250 }
13251
001c8c33 13252 return PRINT_UNKNOWN;
348d480f
PA
13253}
13254
06edf0c0
PA
13255static void
13256momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13257{
06edf0c0 13258 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13259}
13260
e2e4d78b
JK
13261/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13262
13263 It gets cleared already on the removal of the first one of such placed
13264 breakpoints. This is OK as they get all removed altogether. */
13265
13266static void
13267longjmp_bkpt_dtor (struct breakpoint *self)
13268{
13269 struct thread_info *tp = find_thread_id (self->thread);
13270
13271 if (tp)
13272 tp->initiating_frame = null_frame_id;
13273
13274 momentary_breakpoint_ops.dtor (self);
13275}
13276
55aa24fb
SDJ
13277/* Specific methods for probe breakpoints. */
13278
13279static int
13280bkpt_probe_insert_location (struct bp_location *bl)
13281{
13282 int v = bkpt_insert_location (bl);
13283
13284 if (v == 0)
13285 {
13286 /* The insertion was successful, now let's set the probe's semaphore
13287 if needed. */
13288 bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
13289 }
13290
13291 return v;
13292}
13293
13294static int
13295bkpt_probe_remove_location (struct bp_location *bl)
13296{
13297 /* Let's clear the semaphore before removing the location. */
13298 bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
13299
13300 return bkpt_remove_location (bl);
13301}
13302
13303static void
13304bkpt_probe_create_sals_from_address (char **arg,
13305 struct linespec_result *canonical,
13306 enum bptype type_wanted,
13307 char *addr_start, char **copy_arg)
13308{
13309 struct linespec_sals lsal;
13310
13311 lsal.sals = parse_probes (arg, canonical);
13312
13313 *copy_arg = xstrdup (canonical->addr_string);
13314 lsal.canonical = xstrdup (*copy_arg);
13315
13316 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13317}
13318
13319static void
13320bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13321 struct symtabs_and_lines *sals)
13322{
13323 *sals = parse_probes (s, NULL);
13324 if (!sals->sals)
13325 error (_("probe not found"));
13326}
13327
348d480f 13328/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13329
348d480f
PA
13330static void
13331tracepoint_re_set (struct breakpoint *b)
13332{
13333 breakpoint_re_set_default (b);
13334}
876fa593 13335
348d480f
PA
13336static int
13337tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13338 struct address_space *aspace, CORE_ADDR bp_addr,
13339 const struct target_waitstatus *ws)
348d480f
PA
13340{
13341 /* By definition, the inferior does not report stops at
13342 tracepoints. */
13343 return 0;
74960c60
VP
13344}
13345
13346static void
348d480f
PA
13347tracepoint_print_one_detail (const struct breakpoint *self,
13348 struct ui_out *uiout)
74960c60 13349{
d9b3f62e
PA
13350 struct tracepoint *tp = (struct tracepoint *) self;
13351 if (tp->static_trace_marker_id)
348d480f
PA
13352 {
13353 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13354
348d480f
PA
13355 ui_out_text (uiout, "\tmarker id is ");
13356 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13357 tp->static_trace_marker_id);
348d480f
PA
13358 ui_out_text (uiout, "\n");
13359 }
0d381245
VP
13360}
13361
a474d7c2 13362static void
348d480f 13363tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13364{
79a45e25 13365 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13366 return;
cc59ec59 13367
348d480f
PA
13368 switch (b->type)
13369 {
13370 case bp_tracepoint:
13371 printf_filtered (_("Tracepoint"));
13372 printf_filtered (_(" %d"), b->number);
13373 break;
13374 case bp_fast_tracepoint:
13375 printf_filtered (_("Fast tracepoint"));
13376 printf_filtered (_(" %d"), b->number);
13377 break;
13378 case bp_static_tracepoint:
13379 printf_filtered (_("Static tracepoint"));
13380 printf_filtered (_(" %d"), b->number);
13381 break;
13382 default:
13383 internal_error (__FILE__, __LINE__,
13384 _("unhandled tracepoint type %d"), (int) b->type);
13385 }
13386
13387 say_where (b);
a474d7c2
PA
13388}
13389
348d480f 13390static void
d9b3f62e 13391tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13392{
d9b3f62e
PA
13393 struct tracepoint *tp = (struct tracepoint *) self;
13394
13395 if (self->type == bp_fast_tracepoint)
348d480f 13396 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13397 if (self->type == bp_static_tracepoint)
348d480f 13398 fprintf_unfiltered (fp, "strace");
d9b3f62e 13399 else if (self->type == bp_tracepoint)
348d480f
PA
13400 fprintf_unfiltered (fp, "trace");
13401 else
13402 internal_error (__FILE__, __LINE__,
d9b3f62e 13403 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13404
d9b3f62e
PA
13405 fprintf_unfiltered (fp, " %s", self->addr_string);
13406 print_recreate_thread (self, fp);
13407
13408 if (tp->pass_count)
13409 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13410}
13411
983af33b
SDJ
13412static void
13413tracepoint_create_sals_from_address (char **arg,
13414 struct linespec_result *canonical,
13415 enum bptype type_wanted,
13416 char *addr_start, char **copy_arg)
13417{
13418 create_sals_from_address_default (arg, canonical, type_wanted,
13419 addr_start, copy_arg);
13420}
13421
13422static void
13423tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13424 struct linespec_result *canonical,
983af33b 13425 char *cond_string,
e7e0cddf 13426 char *extra_string,
983af33b
SDJ
13427 enum bptype type_wanted,
13428 enum bpdisp disposition,
13429 int thread,
13430 int task, int ignore_count,
13431 const struct breakpoint_ops *ops,
13432 int from_tty, int enabled,
44f238bb 13433 int internal, unsigned flags)
983af33b 13434{
023fa29b 13435 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13436 cond_string, extra_string,
13437 type_wanted,
983af33b
SDJ
13438 disposition, thread, task,
13439 ignore_count, ops, from_tty,
44f238bb 13440 enabled, internal, flags);
983af33b
SDJ
13441}
13442
13443static void
13444tracepoint_decode_linespec (struct breakpoint *b, char **s,
13445 struct symtabs_and_lines *sals)
13446{
13447 decode_linespec_default (b, s, sals);
13448}
13449
2060206e 13450struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13451
55aa24fb
SDJ
13452/* The breakpoint_ops structure to be use on tracepoints placed in a
13453 static probe. */
13454
13455static void
13456tracepoint_probe_create_sals_from_address (char **arg,
13457 struct linespec_result *canonical,
13458 enum bptype type_wanted,
13459 char *addr_start, char **copy_arg)
13460{
13461 /* We use the same method for breakpoint on probes. */
13462 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13463 addr_start, copy_arg);
13464}
13465
13466static void
13467tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13468 struct symtabs_and_lines *sals)
13469{
13470 /* We use the same method for breakpoint on probes. */
13471 bkpt_probe_decode_linespec (b, s, sals);
13472}
13473
13474static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13475
5c2b4418
HZ
13476/* Dprintf breakpoint_ops methods. */
13477
13478static void
13479dprintf_re_set (struct breakpoint *b)
13480{
13481 breakpoint_re_set_default (b);
13482
13483 /* This breakpoint could have been pending, and be resolved now, and
13484 if so, we should now have the extra string. If we don't, the
13485 dprintf was malformed when created, but we couldn't tell because
13486 we can't extract the extra string until the location is
13487 resolved. */
13488 if (b->loc != NULL && b->extra_string == NULL)
13489 error (_("Format string required"));
13490
13491 /* 1 - connect to target 1, that can run breakpoint commands.
13492 2 - create a dprintf, which resolves fine.
13493 3 - disconnect from target 1
13494 4 - connect to target 2, that can NOT run breakpoint commands.
13495
13496 After steps #3/#4, you'll want the dprintf command list to
13497 be updated, because target 1 and 2 may well return different
13498 answers for target_can_run_breakpoint_commands().
13499 Given absence of finer grained resetting, we get to do
13500 it all the time. */
13501 if (b->extra_string != NULL)
13502 update_dprintf_command_list (b);
13503}
13504
2d9442cc
HZ
13505/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13506
13507static void
13508dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13509{
13510 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13511 tp->extra_string);
13512 print_recreate_thread (tp, fp);
13513}
13514
9d6e6e84
HZ
13515/* Implement the "after_condition_true" breakpoint_ops method for
13516 dprintf.
13517
13518 dprintf's are implemented with regular commands in their command
13519 list, but we run the commands here instead of before presenting the
13520 stop to the user, as dprintf's don't actually cause a stop. This
13521 also makes it so that the commands of multiple dprintfs at the same
13522 address are all handled. */
13523
13524static void
13525dprintf_after_condition_true (struct bpstats *bs)
13526{
13527 struct cleanup *old_chain;
13528 struct bpstats tmp_bs = { NULL };
13529 struct bpstats *tmp_bs_p = &tmp_bs;
13530
13531 /* dprintf's never cause a stop. This wasn't set in the
13532 check_status hook instead because that would make the dprintf's
13533 condition not be evaluated. */
13534 bs->stop = 0;
13535
13536 /* Run the command list here. Take ownership of it instead of
13537 copying. We never want these commands to run later in
13538 bpstat_do_actions, if a breakpoint that causes a stop happens to
13539 be set at same address as this dprintf, or even if running the
13540 commands here throws. */
13541 tmp_bs.commands = bs->commands;
13542 bs->commands = NULL;
13543 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13544
13545 bpstat_do_actions_1 (&tmp_bs_p);
13546
13547 /* 'tmp_bs.commands' will usually be NULL by now, but
13548 bpstat_do_actions_1 may return early without processing the whole
13549 list. */
13550 do_cleanups (old_chain);
13551}
13552
983af33b
SDJ
13553/* The breakpoint_ops structure to be used on static tracepoints with
13554 markers (`-m'). */
13555
13556static void
13557strace_marker_create_sals_from_address (char **arg,
13558 struct linespec_result *canonical,
13559 enum bptype type_wanted,
13560 char *addr_start, char **copy_arg)
13561{
13562 struct linespec_sals lsal;
13563
13564 lsal.sals = decode_static_tracepoint_spec (arg);
13565
13566 *copy_arg = savestring (addr_start, *arg - addr_start);
13567
13568 canonical->addr_string = xstrdup (*copy_arg);
13569 lsal.canonical = xstrdup (*copy_arg);
13570 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13571}
13572
13573static void
13574strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13575 struct linespec_result *canonical,
983af33b 13576 char *cond_string,
e7e0cddf 13577 char *extra_string,
983af33b
SDJ
13578 enum bptype type_wanted,
13579 enum bpdisp disposition,
13580 int thread,
13581 int task, int ignore_count,
13582 const struct breakpoint_ops *ops,
13583 int from_tty, int enabled,
44f238bb 13584 int internal, unsigned flags)
983af33b
SDJ
13585{
13586 int i;
52d361e1
YQ
13587 struct linespec_sals *lsal = VEC_index (linespec_sals,
13588 canonical->sals, 0);
983af33b
SDJ
13589
13590 /* If the user is creating a static tracepoint by marker id
13591 (strace -m MARKER_ID), then store the sals index, so that
13592 breakpoint_re_set can try to match up which of the newly
13593 found markers corresponds to this one, and, don't try to
13594 expand multiple locations for each sal, given than SALS
13595 already should contain all sals for MARKER_ID. */
13596
13597 for (i = 0; i < lsal->sals.nelts; ++i)
13598 {
13599 struct symtabs_and_lines expanded;
13600 struct tracepoint *tp;
13601 struct cleanup *old_chain;
13602 char *addr_string;
13603
13604 expanded.nelts = 1;
13605 expanded.sals = &lsal->sals.sals[i];
13606
13607 addr_string = xstrdup (canonical->addr_string);
13608 old_chain = make_cleanup (xfree, addr_string);
13609
13610 tp = XCNEW (struct tracepoint);
13611 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13612 addr_string, NULL,
e7e0cddf
SS
13613 cond_string, extra_string,
13614 type_wanted, disposition,
983af33b 13615 thread, task, ignore_count, ops,
44f238bb 13616 from_tty, enabled, internal, flags,
983af33b
SDJ
13617 canonical->special_display);
13618 /* Given that its possible to have multiple markers with
13619 the same string id, if the user is creating a static
13620 tracepoint by marker id ("strace -m MARKER_ID"), then
13621 store the sals index, so that breakpoint_re_set can
13622 try to match up which of the newly found markers
13623 corresponds to this one */
13624 tp->static_trace_marker_id_idx = i;
13625
13626 install_breakpoint (internal, &tp->base, 0);
13627
13628 discard_cleanups (old_chain);
13629 }
13630}
13631
13632static void
13633strace_marker_decode_linespec (struct breakpoint *b, char **s,
13634 struct symtabs_and_lines *sals)
13635{
13636 struct tracepoint *tp = (struct tracepoint *) b;
13637
13638 *sals = decode_static_tracepoint_spec (s);
13639 if (sals->nelts > tp->static_trace_marker_id_idx)
13640 {
13641 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13642 sals->nelts = 1;
13643 }
13644 else
13645 error (_("marker %s not found"), tp->static_trace_marker_id);
13646}
13647
13648static struct breakpoint_ops strace_marker_breakpoint_ops;
13649
13650static int
13651strace_marker_p (struct breakpoint *b)
13652{
13653 return b->ops == &strace_marker_breakpoint_ops;
13654}
13655
53a5351d 13656/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13657 structures. */
c906108c
SS
13658
13659void
fba45db2 13660delete_breakpoint (struct breakpoint *bpt)
c906108c 13661{
52f0bd74 13662 struct breakpoint *b;
c906108c 13663
8a3fe4f8 13664 gdb_assert (bpt != NULL);
c906108c 13665
4a64f543
MS
13666 /* Has this bp already been deleted? This can happen because
13667 multiple lists can hold pointers to bp's. bpstat lists are
13668 especial culprits.
13669
13670 One example of this happening is a watchpoint's scope bp. When
13671 the scope bp triggers, we notice that the watchpoint is out of
13672 scope, and delete it. We also delete its scope bp. But the
13673 scope bp is marked "auto-deleting", and is already on a bpstat.
13674 That bpstat is then checked for auto-deleting bp's, which are
13675 deleted.
13676
13677 A real solution to this problem might involve reference counts in
13678 bp's, and/or giving them pointers back to their referencing
13679 bpstat's, and teaching delete_breakpoint to only free a bp's
13680 storage when no more references were extent. A cheaper bandaid
13681 was chosen. */
c906108c
SS
13682 if (bpt->type == bp_none)
13683 return;
13684
4a64f543
MS
13685 /* At least avoid this stale reference until the reference counting
13686 of breakpoints gets resolved. */
d0fb5eae 13687 if (bpt->related_breakpoint != bpt)
e5a0a904 13688 {
d0fb5eae 13689 struct breakpoint *related;
3a5c3e22 13690 struct watchpoint *w;
d0fb5eae
JK
13691
13692 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13693 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13694 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13695 w = (struct watchpoint *) bpt;
13696 else
13697 w = NULL;
13698 if (w != NULL)
13699 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13700
13701 /* Unlink bpt from the bpt->related_breakpoint ring. */
13702 for (related = bpt; related->related_breakpoint != bpt;
13703 related = related->related_breakpoint);
13704 related->related_breakpoint = bpt->related_breakpoint;
13705 bpt->related_breakpoint = bpt;
e5a0a904
JK
13706 }
13707
a9634178
TJB
13708 /* watch_command_1 creates a watchpoint but only sets its number if
13709 update_watchpoint succeeds in creating its bp_locations. If there's
13710 a problem in that process, we'll be asked to delete the half-created
13711 watchpoint. In that case, don't announce the deletion. */
13712 if (bpt->number)
13713 observer_notify_breakpoint_deleted (bpt);
c906108c 13714
c906108c
SS
13715 if (breakpoint_chain == bpt)
13716 breakpoint_chain = bpt->next;
13717
c906108c
SS
13718 ALL_BREAKPOINTS (b)
13719 if (b->next == bpt)
c5aa993b
JM
13720 {
13721 b->next = bpt->next;
13722 break;
13723 }
c906108c 13724
f431efe5
PA
13725 /* Be sure no bpstat's are pointing at the breakpoint after it's
13726 been freed. */
13727 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13728 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13729 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13730 commands are associated with the bpstat; if we remove it here,
13731 then the later call to bpstat_do_actions (&stop_bpstat); in
13732 event-top.c won't do anything, and temporary breakpoints with
13733 commands won't work. */
13734
13735 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13736
4a64f543
MS
13737 /* Now that breakpoint is removed from breakpoint list, update the
13738 global location list. This will remove locations that used to
13739 belong to this breakpoint. Do this before freeing the breakpoint
13740 itself, since remove_breakpoint looks at location's owner. It
13741 might be better design to have location completely
13742 self-contained, but it's not the case now. */
b60e7edf 13743 update_global_location_list (0);
74960c60 13744
348d480f 13745 bpt->ops->dtor (bpt);
4a64f543
MS
13746 /* On the chance that someone will soon try again to delete this
13747 same bp, we mark it as deleted before freeing its storage. */
c906108c 13748 bpt->type = bp_none;
b8c9b27d 13749 xfree (bpt);
c906108c
SS
13750}
13751
4d6140d9
AC
13752static void
13753do_delete_breakpoint_cleanup (void *b)
13754{
13755 delete_breakpoint (b);
13756}
13757
13758struct cleanup *
13759make_cleanup_delete_breakpoint (struct breakpoint *b)
13760{
13761 return make_cleanup (do_delete_breakpoint_cleanup, b);
13762}
13763
51be5b68
PA
13764/* Iterator function to call a user-provided callback function once
13765 for each of B and its related breakpoints. */
13766
13767static void
13768iterate_over_related_breakpoints (struct breakpoint *b,
13769 void (*function) (struct breakpoint *,
13770 void *),
13771 void *data)
13772{
13773 struct breakpoint *related;
13774
13775 related = b;
13776 do
13777 {
13778 struct breakpoint *next;
13779
13780 /* FUNCTION may delete RELATED. */
13781 next = related->related_breakpoint;
13782
13783 if (next == related)
13784 {
13785 /* RELATED is the last ring entry. */
13786 function (related, data);
13787
13788 /* FUNCTION may have deleted it, so we'd never reach back to
13789 B. There's nothing left to do anyway, so just break
13790 out. */
13791 break;
13792 }
13793 else
13794 function (related, data);
13795
13796 related = next;
13797 }
13798 while (related != b);
13799}
95a42b64
TT
13800
13801static void
13802do_delete_breakpoint (struct breakpoint *b, void *ignore)
13803{
13804 delete_breakpoint (b);
13805}
13806
51be5b68
PA
13807/* A callback for map_breakpoint_numbers that calls
13808 delete_breakpoint. */
13809
13810static void
13811do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13812{
13813 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13814}
13815
c906108c 13816void
fba45db2 13817delete_command (char *arg, int from_tty)
c906108c 13818{
35df4500 13819 struct breakpoint *b, *b_tmp;
c906108c 13820
ea9365bb
TT
13821 dont_repeat ();
13822
c906108c
SS
13823 if (arg == 0)
13824 {
13825 int breaks_to_delete = 0;
13826
46c6471b
PA
13827 /* Delete all breakpoints if no argument. Do not delete
13828 internal breakpoints, these have to be deleted with an
13829 explicit breakpoint number argument. */
c5aa993b 13830 ALL_BREAKPOINTS (b)
46c6471b 13831 if (user_breakpoint_p (b))
973d738b
DJ
13832 {
13833 breaks_to_delete = 1;
13834 break;
13835 }
c906108c
SS
13836
13837 /* Ask user only if there are some breakpoints to delete. */
13838 if (!from_tty
e2e0b3e5 13839 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13840 {
35df4500 13841 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13842 if (user_breakpoint_p (b))
c5aa993b 13843 delete_breakpoint (b);
c906108c
SS
13844 }
13845 }
13846 else
51be5b68 13847 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13848}
13849
0d381245
VP
13850static int
13851all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13852{
0d381245 13853 for (; loc; loc = loc->next)
8645ff69
UW
13854 if (!loc->shlib_disabled
13855 && !loc->pspace->executing_startup)
0d381245
VP
13856 return 0;
13857 return 1;
fe3f5fa8
VP
13858}
13859
776592bf
DE
13860/* Subroutine of update_breakpoint_locations to simplify it.
13861 Return non-zero if multiple fns in list LOC have the same name.
13862 Null names are ignored. */
13863
13864static int
13865ambiguous_names_p (struct bp_location *loc)
13866{
13867 struct bp_location *l;
13868 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13869 (int (*) (const void *,
13870 const void *)) streq,
776592bf
DE
13871 NULL, xcalloc, xfree);
13872
13873 for (l = loc; l != NULL; l = l->next)
13874 {
13875 const char **slot;
13876 const char *name = l->function_name;
13877
13878 /* Allow for some names to be NULL, ignore them. */
13879 if (name == NULL)
13880 continue;
13881
13882 slot = (const char **) htab_find_slot (htab, (const void *) name,
13883 INSERT);
4a64f543
MS
13884 /* NOTE: We can assume slot != NULL here because xcalloc never
13885 returns NULL. */
776592bf
DE
13886 if (*slot != NULL)
13887 {
13888 htab_delete (htab);
13889 return 1;
13890 }
13891 *slot = name;
13892 }
13893
13894 htab_delete (htab);
13895 return 0;
13896}
13897
0fb4aa4b
PA
13898/* When symbols change, it probably means the sources changed as well,
13899 and it might mean the static tracepoint markers are no longer at
13900 the same address or line numbers they used to be at last we
13901 checked. Losing your static tracepoints whenever you rebuild is
13902 undesirable. This function tries to resync/rematch gdb static
13903 tracepoints with the markers on the target, for static tracepoints
13904 that have not been set by marker id. Static tracepoint that have
13905 been set by marker id are reset by marker id in breakpoint_re_set.
13906 The heuristic is:
13907
13908 1) For a tracepoint set at a specific address, look for a marker at
13909 the old PC. If one is found there, assume to be the same marker.
13910 If the name / string id of the marker found is different from the
13911 previous known name, assume that means the user renamed the marker
13912 in the sources, and output a warning.
13913
13914 2) For a tracepoint set at a given line number, look for a marker
13915 at the new address of the old line number. If one is found there,
13916 assume to be the same marker. If the name / string id of the
13917 marker found is different from the previous known name, assume that
13918 means the user renamed the marker in the sources, and output a
13919 warning.
13920
13921 3) If a marker is no longer found at the same address or line, it
13922 may mean the marker no longer exists. But it may also just mean
13923 the code changed a bit. Maybe the user added a few lines of code
13924 that made the marker move up or down (in line number terms). Ask
13925 the target for info about the marker with the string id as we knew
13926 it. If found, update line number and address in the matching
13927 static tracepoint. This will get confused if there's more than one
13928 marker with the same ID (possible in UST, although unadvised
13929 precisely because it confuses tools). */
13930
13931static struct symtab_and_line
13932update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13933{
d9b3f62e 13934 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13935 struct static_tracepoint_marker marker;
13936 CORE_ADDR pc;
0fb4aa4b
PA
13937
13938 pc = sal.pc;
13939 if (sal.line)
13940 find_line_pc (sal.symtab, sal.line, &pc);
13941
13942 if (target_static_tracepoint_marker_at (pc, &marker))
13943 {
d9b3f62e 13944 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13945 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13946 b->number,
d9b3f62e 13947 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13948
d9b3f62e
PA
13949 xfree (tp->static_trace_marker_id);
13950 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13951 release_static_tracepoint_marker (&marker);
13952
13953 return sal;
13954 }
13955
13956 /* Old marker wasn't found on target at lineno. Try looking it up
13957 by string ID. */
13958 if (!sal.explicit_pc
13959 && sal.line != 0
13960 && sal.symtab != NULL
d9b3f62e 13961 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13962 {
13963 VEC(static_tracepoint_marker_p) *markers;
13964
13965 markers
d9b3f62e 13966 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13967
13968 if (!VEC_empty(static_tracepoint_marker_p, markers))
13969 {
80e1d417 13970 struct symtab_and_line sal2;
0fb4aa4b 13971 struct symbol *sym;
80e1d417 13972 struct static_tracepoint_marker *tpmarker;
79a45e25 13973 struct ui_out *uiout = current_uiout;
0fb4aa4b 13974
80e1d417 13975 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13976
d9b3f62e 13977 xfree (tp->static_trace_marker_id);
80e1d417 13978 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13979
13980 warning (_("marker for static tracepoint %d (%s) not "
13981 "found at previous line number"),
d9b3f62e 13982 b->number, tp->static_trace_marker_id);
0fb4aa4b 13983
80e1d417 13984 init_sal (&sal2);
0fb4aa4b 13985
80e1d417 13986 sal2.pc = tpmarker->address;
0fb4aa4b 13987
80e1d417
AS
13988 sal2 = find_pc_line (tpmarker->address, 0);
13989 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
13990 ui_out_text (uiout, "Now in ");
13991 if (sym)
13992 {
13993 ui_out_field_string (uiout, "func",
13994 SYMBOL_PRINT_NAME (sym));
13995 ui_out_text (uiout, " at ");
13996 }
05cba821
JK
13997 ui_out_field_string (uiout, "file",
13998 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
13999 ui_out_text (uiout, ":");
14000
14001 if (ui_out_is_mi_like_p (uiout))
14002 {
0b0865da 14003 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14004
f35a17b5 14005 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14006 }
14007
80e1d417 14008 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14009 ui_out_text (uiout, "\n");
14010
80e1d417 14011 b->loc->line_number = sal2.line;
2f202fde 14012 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14013
14014 xfree (b->addr_string);
14015 b->addr_string = xstrprintf ("%s:%d",
05cba821 14016 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14017 b->loc->line_number);
0fb4aa4b
PA
14018
14019 /* Might be nice to check if function changed, and warn if
14020 so. */
14021
80e1d417 14022 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14023 }
14024 }
14025 return sal;
14026}
14027
8d3788bd
VP
14028/* Returns 1 iff locations A and B are sufficiently same that
14029 we don't need to report breakpoint as changed. */
14030
14031static int
14032locations_are_equal (struct bp_location *a, struct bp_location *b)
14033{
14034 while (a && b)
14035 {
14036 if (a->address != b->address)
14037 return 0;
14038
14039 if (a->shlib_disabled != b->shlib_disabled)
14040 return 0;
14041
14042 if (a->enabled != b->enabled)
14043 return 0;
14044
14045 a = a->next;
14046 b = b->next;
14047 }
14048
14049 if ((a == NULL) != (b == NULL))
14050 return 0;
14051
14052 return 1;
14053}
14054
f1310107
TJB
14055/* Create new breakpoint locations for B (a hardware or software breakpoint)
14056 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14057 a ranged breakpoint. */
14058
0e30163f 14059void
0d381245 14060update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14061 struct symtabs_and_lines sals,
14062 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14063{
14064 int i;
0d381245
VP
14065 struct bp_location *existing_locations = b->loc;
14066
f8eba3c6
TT
14067 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14068 {
14069 /* Ranged breakpoints have only one start location and one end
14070 location. */
14071 b->enable_state = bp_disabled;
14072 update_global_location_list (1);
14073 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14074 "multiple locations found\n"),
14075 b->number);
14076 return;
14077 }
f1310107 14078
4a64f543
MS
14079 /* If there's no new locations, and all existing locations are
14080 pending, don't do anything. This optimizes the common case where
14081 all locations are in the same shared library, that was unloaded.
14082 We'd like to retain the location, so that when the library is
14083 loaded again, we don't loose the enabled/disabled status of the
14084 individual locations. */
0d381245 14085 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14086 return;
14087
fe3f5fa8
VP
14088 b->loc = NULL;
14089
0d381245 14090 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14091 {
f8eba3c6
TT
14092 struct bp_location *new_loc;
14093
14094 switch_to_program_space_and_thread (sals.sals[i].pspace);
14095
14096 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14097
0d381245
VP
14098 /* Reparse conditions, they might contain references to the
14099 old symtab. */
14100 if (b->cond_string != NULL)
14101 {
bbc13ae3 14102 const char *s;
bfd189b1 14103 volatile struct gdb_exception e;
fe3f5fa8 14104
0d381245
VP
14105 s = b->cond_string;
14106 TRY_CATCH (e, RETURN_MASK_ERROR)
14107 {
1bb9788d
TT
14108 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14109 block_for_pc (sals.sals[i].pc),
0d381245
VP
14110 0);
14111 }
14112 if (e.reason < 0)
14113 {
3e43a32a
MS
14114 warning (_("failed to reevaluate condition "
14115 "for breakpoint %d: %s"),
0d381245
VP
14116 b->number, e.message);
14117 new_loc->enabled = 0;
14118 }
14119 }
fe3f5fa8 14120
f1310107
TJB
14121 if (sals_end.nelts)
14122 {
14123 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14124
14125 new_loc->length = end - sals.sals[0].pc + 1;
14126 }
0d381245 14127 }
fe3f5fa8 14128
514f746b
AR
14129 /* Update locations of permanent breakpoints. */
14130 if (b->enable_state == bp_permanent)
14131 make_breakpoint_permanent (b);
14132
4a64f543
MS
14133 /* If possible, carry over 'disable' status from existing
14134 breakpoints. */
0d381245
VP
14135 {
14136 struct bp_location *e = existing_locations;
776592bf
DE
14137 /* If there are multiple breakpoints with the same function name,
14138 e.g. for inline functions, comparing function names won't work.
14139 Instead compare pc addresses; this is just a heuristic as things
14140 may have moved, but in practice it gives the correct answer
14141 often enough until a better solution is found. */
14142 int have_ambiguous_names = ambiguous_names_p (b->loc);
14143
0d381245
VP
14144 for (; e; e = e->next)
14145 {
14146 if (!e->enabled && e->function_name)
14147 {
14148 struct bp_location *l = b->loc;
776592bf
DE
14149 if (have_ambiguous_names)
14150 {
14151 for (; l; l = l->next)
f1310107 14152 if (breakpoint_locations_match (e, l))
776592bf
DE
14153 {
14154 l->enabled = 0;
14155 break;
14156 }
14157 }
14158 else
14159 {
14160 for (; l; l = l->next)
14161 if (l->function_name
14162 && strcmp (e->function_name, l->function_name) == 0)
14163 {
14164 l->enabled = 0;
14165 break;
14166 }
14167 }
0d381245
VP
14168 }
14169 }
14170 }
fe3f5fa8 14171
8d3788bd
VP
14172 if (!locations_are_equal (existing_locations, b->loc))
14173 observer_notify_breakpoint_modified (b);
14174
b60e7edf 14175 update_global_location_list (1);
fe3f5fa8
VP
14176}
14177
ef23e705
TJB
14178/* Find the SaL locations corresponding to the given ADDR_STRING.
14179 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14180
14181static struct symtabs_and_lines
14182addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14183{
14184 char *s;
02d20e4a 14185 struct symtabs_and_lines sals = {0};
f8eba3c6 14186 volatile struct gdb_exception e;
ef23e705 14187
983af33b 14188 gdb_assert (b->ops != NULL);
ef23e705 14189 s = addr_string;
ef23e705
TJB
14190
14191 TRY_CATCH (e, RETURN_MASK_ERROR)
14192 {
983af33b 14193 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14194 }
14195 if (e.reason < 0)
14196 {
14197 int not_found_and_ok = 0;
14198 /* For pending breakpoints, it's expected that parsing will
14199 fail until the right shared library is loaded. User has
14200 already told to create pending breakpoints and don't need
14201 extra messages. If breakpoint is in bp_shlib_disabled
14202 state, then user already saw the message about that
14203 breakpoint being disabled, and don't want to see more
14204 errors. */
58438ac1 14205 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14206 && (b->condition_not_parsed
14207 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14208 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14209 || b->enable_state == bp_disabled))
14210 not_found_and_ok = 1;
14211
14212 if (!not_found_and_ok)
14213 {
14214 /* We surely don't want to warn about the same breakpoint
14215 10 times. One solution, implemented here, is disable
14216 the breakpoint on error. Another solution would be to
14217 have separate 'warning emitted' flag. Since this
14218 happens only when a binary has changed, I don't know
14219 which approach is better. */
14220 b->enable_state = bp_disabled;
14221 throw_exception (e);
14222 }
14223 }
14224
58438ac1 14225 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14226 {
f8eba3c6 14227 int i;
ef23e705 14228
f8eba3c6
TT
14229 for (i = 0; i < sals.nelts; ++i)
14230 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14231 if (b->condition_not_parsed && s && s[0])
14232 {
ed1d1739
KS
14233 char *cond_string, *extra_string;
14234 int thread, task;
ef23e705
TJB
14235
14236 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14237 &cond_string, &thread, &task,
14238 &extra_string);
ef23e705
TJB
14239 if (cond_string)
14240 b->cond_string = cond_string;
14241 b->thread = thread;
14242 b->task = task;
e7e0cddf
SS
14243 if (extra_string)
14244 b->extra_string = extra_string;
ef23e705
TJB
14245 b->condition_not_parsed = 0;
14246 }
14247
983af33b 14248 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14249 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14250
58438ac1
TT
14251 *found = 1;
14252 }
14253 else
14254 *found = 0;
ef23e705
TJB
14255
14256 return sals;
14257}
14258
348d480f
PA
14259/* The default re_set method, for typical hardware or software
14260 breakpoints. Reevaluate the breakpoint and recreate its
14261 locations. */
14262
14263static void
28010a5d 14264breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14265{
14266 int found;
f1310107 14267 struct symtabs_and_lines sals, sals_end;
ef23e705 14268 struct symtabs_and_lines expanded = {0};
f1310107 14269 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14270
14271 sals = addr_string_to_sals (b, b->addr_string, &found);
14272 if (found)
14273 {
14274 make_cleanup (xfree, sals.sals);
f8eba3c6 14275 expanded = sals;
ef23e705
TJB
14276 }
14277
f1310107
TJB
14278 if (b->addr_string_range_end)
14279 {
14280 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14281 if (found)
14282 {
14283 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14284 expanded_end = sals_end;
f1310107
TJB
14285 }
14286 }
14287
14288 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14289}
14290
983af33b
SDJ
14291/* Default method for creating SALs from an address string. It basically
14292 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14293
14294static void
14295create_sals_from_address_default (char **arg,
14296 struct linespec_result *canonical,
14297 enum bptype type_wanted,
14298 char *addr_start, char **copy_arg)
14299{
14300 parse_breakpoint_sals (arg, canonical);
14301}
14302
14303/* Call create_breakpoints_sal for the given arguments. This is the default
14304 function for the `create_breakpoints_sal' method of
14305 breakpoint_ops. */
14306
14307static void
14308create_breakpoints_sal_default (struct gdbarch *gdbarch,
14309 struct linespec_result *canonical,
983af33b 14310 char *cond_string,
e7e0cddf 14311 char *extra_string,
983af33b
SDJ
14312 enum bptype type_wanted,
14313 enum bpdisp disposition,
14314 int thread,
14315 int task, int ignore_count,
14316 const struct breakpoint_ops *ops,
14317 int from_tty, int enabled,
44f238bb 14318 int internal, unsigned flags)
983af33b
SDJ
14319{
14320 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14321 extra_string,
983af33b
SDJ
14322 type_wanted, disposition,
14323 thread, task, ignore_count, ops, from_tty,
44f238bb 14324 enabled, internal, flags);
983af33b
SDJ
14325}
14326
14327/* Decode the line represented by S by calling decode_line_full. This is the
14328 default function for the `decode_linespec' method of breakpoint_ops. */
14329
14330static void
14331decode_linespec_default (struct breakpoint *b, char **s,
14332 struct symtabs_and_lines *sals)
14333{
14334 struct linespec_result canonical;
14335
14336 init_linespec_result (&canonical);
14337 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14338 (struct symtab *) NULL, 0,
14339 &canonical, multiple_symbols_all,
14340 b->filter);
14341
14342 /* We should get 0 or 1 resulting SALs. */
14343 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14344
14345 if (VEC_length (linespec_sals, canonical.sals) > 0)
14346 {
14347 struct linespec_sals *lsal;
14348
14349 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14350 *sals = lsal->sals;
14351 /* Arrange it so the destructor does not free the
14352 contents. */
14353 lsal->sals.sals = NULL;
14354 }
14355
14356 destroy_linespec_result (&canonical);
14357}
14358
28010a5d
PA
14359/* Prepare the global context for a re-set of breakpoint B. */
14360
14361static struct cleanup *
14362prepare_re_set_context (struct breakpoint *b)
14363{
14364 struct cleanup *cleanups;
14365
14366 input_radix = b->input_radix;
14367 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14368 if (b->pspace != NULL)
14369 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14370 set_language (b->language);
14371
14372 return cleanups;
ef23e705
TJB
14373}
14374
c906108c
SS
14375/* Reset a breakpoint given it's struct breakpoint * BINT.
14376 The value we return ends up being the return value from catch_errors.
14377 Unused in this case. */
14378
14379static int
4efb68b1 14380breakpoint_re_set_one (void *bint)
c906108c 14381{
4a64f543 14382 /* Get past catch_errs. */
53a5351d 14383 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14384 struct cleanup *cleanups;
c906108c 14385
348d480f
PA
14386 cleanups = prepare_re_set_context (b);
14387 b->ops->re_set (b);
14388 do_cleanups (cleanups);
c906108c
SS
14389 return 0;
14390}
14391
69de3c6a 14392/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14393void
69de3c6a 14394breakpoint_re_set (void)
c906108c 14395{
35df4500 14396 struct breakpoint *b, *b_tmp;
c906108c
SS
14397 enum language save_language;
14398 int save_input_radix;
6c95b8df 14399 struct cleanup *old_chain;
c5aa993b 14400
c906108c
SS
14401 save_language = current_language->la_language;
14402 save_input_radix = input_radix;
6c95b8df
PA
14403 old_chain = save_current_program_space ();
14404
35df4500 14405 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14406 {
4a64f543 14407 /* Format possible error msg. */
fe3f5fa8 14408 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14409 b->number);
14410 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14411 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14412 do_cleanups (cleanups);
c5aa993b 14413 }
c906108c
SS
14414 set_language (save_language);
14415 input_radix = save_input_radix;
e62c965a 14416
0756c555 14417 jit_breakpoint_re_set ();
4efc6507 14418
6c95b8df
PA
14419 do_cleanups (old_chain);
14420
af02033e
PP
14421 create_overlay_event_breakpoint ();
14422 create_longjmp_master_breakpoint ();
14423 create_std_terminate_master_breakpoint ();
186c406b 14424 create_exception_master_breakpoint ();
c906108c
SS
14425}
14426\f
c906108c
SS
14427/* Reset the thread number of this breakpoint:
14428
14429 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14430 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14431void
fba45db2 14432breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14433{
14434 if (b->thread != -1)
14435 {
39f77062
KB
14436 if (in_thread_list (inferior_ptid))
14437 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14438
14439 /* We're being called after following a fork. The new fork is
14440 selected as current, and unless this was a vfork will have a
14441 different program space from the original thread. Reset that
14442 as well. */
14443 b->loc->pspace = current_program_space;
c906108c
SS
14444 }
14445}
14446
03ac34d5
MS
14447/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14448 If from_tty is nonzero, it prints a message to that effect,
14449 which ends with a period (no newline). */
14450
c906108c 14451void
fba45db2 14452set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14453{
52f0bd74 14454 struct breakpoint *b;
c906108c
SS
14455
14456 if (count < 0)
14457 count = 0;
14458
14459 ALL_BREAKPOINTS (b)
14460 if (b->number == bptnum)
c5aa993b 14461 {
d77f58be
SS
14462 if (is_tracepoint (b))
14463 {
14464 if (from_tty && count != 0)
14465 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14466 bptnum);
14467 return;
14468 }
14469
c5aa993b 14470 b->ignore_count = count;
221ea385
KS
14471 if (from_tty)
14472 {
14473 if (count == 0)
3e43a32a
MS
14474 printf_filtered (_("Will stop next time "
14475 "breakpoint %d is reached."),
221ea385
KS
14476 bptnum);
14477 else if (count == 1)
a3f17187 14478 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14479 bptnum);
14480 else
3e43a32a
MS
14481 printf_filtered (_("Will ignore next %d "
14482 "crossings of breakpoint %d."),
221ea385
KS
14483 count, bptnum);
14484 }
8d3788bd 14485 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14486 return;
14487 }
c906108c 14488
8a3fe4f8 14489 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14490}
14491
c906108c
SS
14492/* Command to set ignore-count of breakpoint N to COUNT. */
14493
14494static void
fba45db2 14495ignore_command (char *args, int from_tty)
c906108c
SS
14496{
14497 char *p = args;
52f0bd74 14498 int num;
c906108c
SS
14499
14500 if (p == 0)
e2e0b3e5 14501 error_no_arg (_("a breakpoint number"));
c5aa993b 14502
c906108c 14503 num = get_number (&p);
5c44784c 14504 if (num == 0)
8a3fe4f8 14505 error (_("bad breakpoint number: '%s'"), args);
c906108c 14506 if (*p == 0)
8a3fe4f8 14507 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14508
14509 set_ignore_count (num,
14510 longest_to_int (value_as_long (parse_and_eval (p))),
14511 from_tty);
221ea385
KS
14512 if (from_tty)
14513 printf_filtered ("\n");
c906108c
SS
14514}
14515\f
14516/* Call FUNCTION on each of the breakpoints
14517 whose numbers are given in ARGS. */
14518
14519static void
95a42b64
TT
14520map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14521 void *),
14522 void *data)
c906108c 14523{
52f0bd74
AC
14524 int num;
14525 struct breakpoint *b, *tmp;
11cf8741 14526 int match;
197f0a60 14527 struct get_number_or_range_state state;
c906108c 14528
197f0a60 14529 if (args == 0)
e2e0b3e5 14530 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14531
197f0a60
TT
14532 init_number_or_range (&state, args);
14533
14534 while (!state.finished)
c906108c 14535 {
197f0a60
TT
14536 char *p = state.string;
14537
11cf8741 14538 match = 0;
c5aa993b 14539
197f0a60 14540 num = get_number_or_range (&state);
5c44784c 14541 if (num == 0)
c5aa993b 14542 {
8a3fe4f8 14543 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14544 }
14545 else
14546 {
14547 ALL_BREAKPOINTS_SAFE (b, tmp)
14548 if (b->number == num)
14549 {
11cf8741 14550 match = 1;
cdac0397 14551 function (b, data);
11cf8741 14552 break;
5c44784c 14553 }
11cf8741 14554 if (match == 0)
a3f17187 14555 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14556 }
c906108c
SS
14557 }
14558}
14559
0d381245
VP
14560static struct bp_location *
14561find_location_by_number (char *number)
14562{
14563 char *dot = strchr (number, '.');
14564 char *p1;
14565 int bp_num;
14566 int loc_num;
14567 struct breakpoint *b;
14568 struct bp_location *loc;
14569
14570 *dot = '\0';
14571
14572 p1 = number;
197f0a60 14573 bp_num = get_number (&p1);
0d381245
VP
14574 if (bp_num == 0)
14575 error (_("Bad breakpoint number '%s'"), number);
14576
14577 ALL_BREAKPOINTS (b)
14578 if (b->number == bp_num)
14579 {
14580 break;
14581 }
14582
14583 if (!b || b->number != bp_num)
14584 error (_("Bad breakpoint number '%s'"), number);
14585
14586 p1 = dot+1;
197f0a60 14587 loc_num = get_number (&p1);
0d381245
VP
14588 if (loc_num == 0)
14589 error (_("Bad breakpoint location number '%s'"), number);
14590
14591 --loc_num;
14592 loc = b->loc;
14593 for (;loc_num && loc; --loc_num, loc = loc->next)
14594 ;
14595 if (!loc)
14596 error (_("Bad breakpoint location number '%s'"), dot+1);
14597
14598 return loc;
14599}
14600
14601
1900040c
MS
14602/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14603 If from_tty is nonzero, it prints a message to that effect,
14604 which ends with a period (no newline). */
14605
c906108c 14606void
fba45db2 14607disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14608{
14609 /* Never disable a watchpoint scope breakpoint; we want to
14610 hit them when we leave scope so we can delete both the
14611 watchpoint and its scope breakpoint at that time. */
14612 if (bpt->type == bp_watchpoint_scope)
14613 return;
14614
c2c6d25f 14615 /* You can't disable permanent breakpoints. */
b5de0fa7 14616 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14617 return;
14618
b5de0fa7 14619 bpt->enable_state = bp_disabled;
c906108c 14620
b775012e
LM
14621 /* Mark breakpoint locations modified. */
14622 mark_breakpoint_modified (bpt);
14623
d248b706
KY
14624 if (target_supports_enable_disable_tracepoint ()
14625 && current_trace_status ()->running && is_tracepoint (bpt))
14626 {
14627 struct bp_location *location;
14628
14629 for (location = bpt->loc; location; location = location->next)
14630 target_disable_tracepoint (location);
14631 }
14632
b60e7edf 14633 update_global_location_list (0);
c906108c 14634
8d3788bd 14635 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14636}
14637
51be5b68
PA
14638/* A callback for iterate_over_related_breakpoints. */
14639
14640static void
14641do_disable_breakpoint (struct breakpoint *b, void *ignore)
14642{
14643 disable_breakpoint (b);
14644}
14645
95a42b64
TT
14646/* A callback for map_breakpoint_numbers that calls
14647 disable_breakpoint. */
14648
14649static void
14650do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14651{
51be5b68 14652 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14653}
14654
c906108c 14655static void
fba45db2 14656disable_command (char *args, int from_tty)
c906108c 14657{
c906108c 14658 if (args == 0)
46c6471b
PA
14659 {
14660 struct breakpoint *bpt;
14661
14662 ALL_BREAKPOINTS (bpt)
14663 if (user_breakpoint_p (bpt))
14664 disable_breakpoint (bpt);
14665 }
9eaabc75 14666 else
0d381245 14667 {
9eaabc75
MW
14668 char *num = extract_arg (&args);
14669
14670 while (num)
d248b706 14671 {
9eaabc75 14672 if (strchr (num, '.'))
b775012e 14673 {
9eaabc75
MW
14674 struct bp_location *loc = find_location_by_number (num);
14675
14676 if (loc)
14677 {
14678 if (loc->enabled)
14679 {
14680 loc->enabled = 0;
14681 mark_breakpoint_location_modified (loc);
14682 }
14683 if (target_supports_enable_disable_tracepoint ()
14684 && current_trace_status ()->running && loc->owner
14685 && is_tracepoint (loc->owner))
14686 target_disable_tracepoint (loc);
14687 }
14688 update_global_location_list (0);
b775012e 14689 }
9eaabc75
MW
14690 else
14691 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14692 num = extract_arg (&args);
d248b706 14693 }
0d381245 14694 }
c906108c
SS
14695}
14696
14697static void
816338b5
SS
14698enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14699 int count)
c906108c 14700{
afe38095 14701 int target_resources_ok;
c906108c
SS
14702
14703 if (bpt->type == bp_hardware_breakpoint)
14704 {
14705 int i;
c5aa993b 14706 i = hw_breakpoint_used_count ();
53a5351d 14707 target_resources_ok =
d92524f1 14708 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14709 i + 1, 0);
c906108c 14710 if (target_resources_ok == 0)
8a3fe4f8 14711 error (_("No hardware breakpoint support in the target."));
c906108c 14712 else if (target_resources_ok < 0)
8a3fe4f8 14713 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14714 }
14715
cc60f2e3 14716 if (is_watchpoint (bpt))
c906108c 14717 {
d07205c2
JK
14718 /* Initialize it just to avoid a GCC false warning. */
14719 enum enable_state orig_enable_state = 0;
bfd189b1 14720 volatile struct gdb_exception e;
dde02812
ES
14721
14722 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 14723 {
3a5c3e22
PA
14724 struct watchpoint *w = (struct watchpoint *) bpt;
14725
1e718ff1
TJB
14726 orig_enable_state = bpt->enable_state;
14727 bpt->enable_state = bp_enabled;
3a5c3e22 14728 update_watchpoint (w, 1 /* reparse */);
c906108c 14729 }
dde02812 14730 if (e.reason < 0)
c5aa993b 14731 {
1e718ff1 14732 bpt->enable_state = orig_enable_state;
dde02812
ES
14733 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14734 bpt->number);
14735 return;
c5aa993b 14736 }
c906108c 14737 }
0101ce28 14738
b4c291bb
KH
14739 if (bpt->enable_state != bp_permanent)
14740 bpt->enable_state = bp_enabled;
d248b706 14741
b775012e
LM
14742 bpt->enable_state = bp_enabled;
14743
14744 /* Mark breakpoint locations modified. */
14745 mark_breakpoint_modified (bpt);
14746
d248b706
KY
14747 if (target_supports_enable_disable_tracepoint ()
14748 && current_trace_status ()->running && is_tracepoint (bpt))
14749 {
14750 struct bp_location *location;
14751
14752 for (location = bpt->loc; location; location = location->next)
14753 target_enable_tracepoint (location);
14754 }
14755
b4c291bb 14756 bpt->disposition = disposition;
816338b5 14757 bpt->enable_count = count;
b60e7edf 14758 update_global_location_list (1);
9c97429f 14759
8d3788bd 14760 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14761}
14762
fe3f5fa8 14763
c906108c 14764void
fba45db2 14765enable_breakpoint (struct breakpoint *bpt)
c906108c 14766{
816338b5 14767 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14768}
14769
14770static void
14771do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14772{
14773 enable_breakpoint (bpt);
c906108c
SS
14774}
14775
95a42b64
TT
14776/* A callback for map_breakpoint_numbers that calls
14777 enable_breakpoint. */
14778
14779static void
14780do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14781{
51be5b68 14782 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14783}
14784
c906108c
SS
14785/* The enable command enables the specified breakpoints (or all defined
14786 breakpoints) so they once again become (or continue to be) effective
1272ad14 14787 in stopping the inferior. */
c906108c 14788
c906108c 14789static void
fba45db2 14790enable_command (char *args, int from_tty)
c906108c 14791{
c906108c 14792 if (args == 0)
46c6471b
PA
14793 {
14794 struct breakpoint *bpt;
14795
14796 ALL_BREAKPOINTS (bpt)
14797 if (user_breakpoint_p (bpt))
14798 enable_breakpoint (bpt);
14799 }
9eaabc75 14800 else
0d381245 14801 {
9eaabc75
MW
14802 char *num = extract_arg (&args);
14803
14804 while (num)
d248b706 14805 {
9eaabc75 14806 if (strchr (num, '.'))
b775012e 14807 {
9eaabc75
MW
14808 struct bp_location *loc = find_location_by_number (num);
14809
14810 if (loc)
14811 {
14812 if (!loc->enabled)
14813 {
14814 loc->enabled = 1;
14815 mark_breakpoint_location_modified (loc);
14816 }
14817 if (target_supports_enable_disable_tracepoint ()
14818 && current_trace_status ()->running && loc->owner
14819 && is_tracepoint (loc->owner))
14820 target_enable_tracepoint (loc);
14821 }
14822 update_global_location_list (1);
b775012e 14823 }
9eaabc75
MW
14824 else
14825 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14826 num = extract_arg (&args);
d248b706 14827 }
0d381245 14828 }
c906108c
SS
14829}
14830
816338b5
SS
14831/* This struct packages up disposition data for application to multiple
14832 breakpoints. */
14833
14834struct disp_data
14835{
14836 enum bpdisp disp;
14837 int count;
14838};
14839
c906108c 14840static void
51be5b68
PA
14841do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14842{
816338b5 14843 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14844
816338b5 14845 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14846}
14847
14848static void
14849do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14850{
816338b5 14851 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14852
14853 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14854}
14855
c906108c 14856static void
fba45db2 14857enable_once_command (char *args, int from_tty)
c906108c 14858{
51be5b68 14859 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14860}
14861
816338b5
SS
14862static void
14863do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14864{
14865 struct disp_data disp = { disp_disable, *(int *) countptr };
14866
14867 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14868}
14869
14870static void
14871enable_count_command (char *args, int from_tty)
14872{
14873 int count = get_number (&args);
14874
14875 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14876}
14877
c906108c 14878static void
51be5b68 14879do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14880{
816338b5 14881 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14882
14883 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14884}
14885
c906108c 14886static void
fba45db2 14887enable_delete_command (char *args, int from_tty)
c906108c 14888{
51be5b68 14889 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14890}
14891\f
fa8d40ab
JJ
14892static void
14893set_breakpoint_cmd (char *args, int from_tty)
14894{
14895}
14896
14897static void
14898show_breakpoint_cmd (char *args, int from_tty)
14899{
14900}
14901
1f3b5d1b
PP
14902/* Invalidate last known value of any hardware watchpoint if
14903 the memory which that value represents has been written to by
14904 GDB itself. */
14905
14906static void
8de0566d
YQ
14907invalidate_bp_value_on_memory_change (struct inferior *inferior,
14908 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14909 const bfd_byte *data)
14910{
14911 struct breakpoint *bp;
14912
14913 ALL_BREAKPOINTS (bp)
14914 if (bp->enable_state == bp_enabled
3a5c3e22 14915 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14916 {
3a5c3e22 14917 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14918
3a5c3e22
PA
14919 if (wp->val_valid && wp->val)
14920 {
14921 struct bp_location *loc;
14922
14923 for (loc = bp->loc; loc != NULL; loc = loc->next)
14924 if (loc->loc_type == bp_loc_hardware_watchpoint
14925 && loc->address + loc->length > addr
14926 && addr + len > loc->address)
14927 {
14928 value_free (wp->val);
14929 wp->val = NULL;
14930 wp->val_valid = 0;
14931 }
14932 }
1f3b5d1b
PP
14933 }
14934}
14935
8181d85f
DJ
14936/* Create and insert a raw software breakpoint at PC. Return an
14937 identifier, which should be used to remove the breakpoint later.
14938 In general, places which call this should be using something on the
14939 breakpoint chain instead; this function should be eliminated
14940 someday. */
14941
14942void *
6c95b8df
PA
14943deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14944 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
14945{
14946 struct bp_target_info *bp_tgt;
14947
6c95b8df 14948 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 14949
6c95b8df 14950 bp_tgt->placed_address_space = aspace;
8181d85f 14951 bp_tgt->placed_address = pc;
6c95b8df 14952
a6d9a66e 14953 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
14954 {
14955 /* Could not insert the breakpoint. */
14956 xfree (bp_tgt);
14957 return NULL;
14958 }
14959
14960 return bp_tgt;
14961}
14962
4a64f543
MS
14963/* Remove a breakpoint BP inserted by
14964 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
14965
14966int
a6d9a66e 14967deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
14968{
14969 struct bp_target_info *bp_tgt = bp;
14970 int ret;
14971
a6d9a66e 14972 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
14973 xfree (bp_tgt);
14974
14975 return ret;
14976}
14977
4a64f543
MS
14978/* One (or perhaps two) breakpoints used for software single
14979 stepping. */
8181d85f
DJ
14980
14981static void *single_step_breakpoints[2];
a6d9a66e 14982static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
14983
14984/* Create and insert a breakpoint for software single step. */
14985
14986void
6c95b8df 14987insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14988 struct address_space *aspace,
14989 CORE_ADDR next_pc)
8181d85f
DJ
14990{
14991 void **bpt_p;
14992
14993 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
14994 {
14995 bpt_p = &single_step_breakpoints[0];
14996 single_step_gdbarch[0] = gdbarch;
14997 }
8181d85f
DJ
14998 else
14999 {
15000 gdb_assert (single_step_breakpoints[1] == NULL);
15001 bpt_p = &single_step_breakpoints[1];
a6d9a66e 15002 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
15003 }
15004
4a64f543
MS
15005 /* NOTE drow/2006-04-11: A future improvement to this function would
15006 be to only create the breakpoints once, and actually put them on
15007 the breakpoint chain. That would let us use set_raw_breakpoint.
15008 We could adjust the addresses each time they were needed. Doing
15009 this requires corresponding changes elsewhere where single step
15010 breakpoints are handled, however. So, for now, we use this. */
8181d85f 15011
6c95b8df 15012 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 15013 if (*bpt_p == NULL)
5af949e3
UW
15014 error (_("Could not insert single-step breakpoint at %s"),
15015 paddress (gdbarch, next_pc));
8181d85f
DJ
15016}
15017
f02253f1
HZ
15018/* Check if the breakpoints used for software single stepping
15019 were inserted or not. */
15020
15021int
15022single_step_breakpoints_inserted (void)
15023{
15024 return (single_step_breakpoints[0] != NULL
15025 || single_step_breakpoints[1] != NULL);
15026}
15027
8181d85f
DJ
15028/* Remove and delete any breakpoints used for software single step. */
15029
15030void
15031remove_single_step_breakpoints (void)
15032{
15033 gdb_assert (single_step_breakpoints[0] != NULL);
15034
15035 /* See insert_single_step_breakpoint for more about this deprecated
15036 call. */
a6d9a66e
UW
15037 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15038 single_step_breakpoints[0]);
15039 single_step_gdbarch[0] = NULL;
8181d85f
DJ
15040 single_step_breakpoints[0] = NULL;
15041
15042 if (single_step_breakpoints[1] != NULL)
15043 {
a6d9a66e
UW
15044 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15045 single_step_breakpoints[1]);
15046 single_step_gdbarch[1] = NULL;
8181d85f
DJ
15047 single_step_breakpoints[1] = NULL;
15048 }
15049}
15050
d03285ec
UW
15051/* Delete software single step breakpoints without removing them from
15052 the inferior. This is intended to be used if the inferior's address
15053 space where they were inserted is already gone, e.g. after exit or
15054 exec. */
15055
15056void
15057cancel_single_step_breakpoints (void)
15058{
15059 int i;
15060
15061 for (i = 0; i < 2; i++)
15062 if (single_step_breakpoints[i])
15063 {
15064 xfree (single_step_breakpoints[i]);
15065 single_step_breakpoints[i] = NULL;
15066 single_step_gdbarch[i] = NULL;
15067 }
15068}
15069
15070/* Detach software single-step breakpoints from INFERIOR_PTID without
15071 removing them. */
15072
15073static void
15074detach_single_step_breakpoints (void)
15075{
15076 int i;
15077
15078 for (i = 0; i < 2; i++)
15079 if (single_step_breakpoints[i])
15080 target_remove_breakpoint (single_step_gdbarch[i],
15081 single_step_breakpoints[i]);
15082}
15083
4a64f543
MS
15084/* Check whether a software single-step breakpoint is inserted at
15085 PC. */
1aafd4da
UW
15086
15087static int
cc59ec59
MS
15088single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15089 CORE_ADDR pc)
1aafd4da
UW
15090{
15091 int i;
15092
15093 for (i = 0; i < 2; i++)
15094 {
15095 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
15096 if (bp_tgt
15097 && breakpoint_address_match (bp_tgt->placed_address_space,
15098 bp_tgt->placed_address,
15099 aspace, pc))
1aafd4da
UW
15100 return 1;
15101 }
15102
15103 return 0;
15104}
15105
a96d9b2e
SDJ
15106/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15107 non-zero otherwise. */
15108static int
15109is_syscall_catchpoint_enabled (struct breakpoint *bp)
15110{
15111 if (syscall_catchpoint_p (bp)
15112 && bp->enable_state != bp_disabled
15113 && bp->enable_state != bp_call_disabled)
15114 return 1;
15115 else
15116 return 0;
15117}
15118
15119int
15120catch_syscall_enabled (void)
15121{
fa3064dd
YQ
15122 struct catch_syscall_inferior_data *inf_data
15123 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15124
fa3064dd 15125 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15126}
15127
15128int
15129catching_syscall_number (int syscall_number)
15130{
15131 struct breakpoint *bp;
15132
15133 ALL_BREAKPOINTS (bp)
15134 if (is_syscall_catchpoint_enabled (bp))
15135 {
be5c67c1
PA
15136 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15137
15138 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15139 {
15140 int i, iter;
15141 for (i = 0;
be5c67c1 15142 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15143 i++)
15144 if (syscall_number == iter)
15145 return 1;
15146 }
15147 else
15148 return 1;
15149 }
15150
15151 return 0;
15152}
15153
15154/* Complete syscall names. Used by "catch syscall". */
49c4e619 15155static VEC (char_ptr) *
a96d9b2e 15156catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15157 const char *text, const char *word)
a96d9b2e
SDJ
15158{
15159 const char **list = get_syscall_names ();
49c4e619 15160 VEC (char_ptr) *retlist
b45627a0 15161 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15162
c38eea1a
MS
15163 xfree (list);
15164 return retlist;
a96d9b2e
SDJ
15165}
15166
1042e4c0
SS
15167/* Tracepoint-specific operations. */
15168
15169/* Set tracepoint count to NUM. */
15170static void
15171set_tracepoint_count (int num)
15172{
15173 tracepoint_count = num;
4fa62494 15174 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15175}
15176
70221824 15177static void
1042e4c0
SS
15178trace_command (char *arg, int from_tty)
15179{
55aa24fb
SDJ
15180 struct breakpoint_ops *ops;
15181 const char *arg_cp = arg;
15182
15183 if (arg && probe_linespec_to_ops (&arg_cp))
15184 ops = &tracepoint_probe_breakpoint_ops;
15185 else
15186 ops = &tracepoint_breakpoint_ops;
15187
558a9d82
YQ
15188 create_breakpoint (get_current_arch (),
15189 arg,
15190 NULL, 0, NULL, 1 /* parse arg */,
15191 0 /* tempflag */,
15192 bp_tracepoint /* type_wanted */,
15193 0 /* Ignore count */,
15194 pending_break_support,
15195 ops,
15196 from_tty,
15197 1 /* enabled */,
15198 0 /* internal */, 0);
1042e4c0
SS
15199}
15200
70221824 15201static void
7a697b8d
SS
15202ftrace_command (char *arg, int from_tty)
15203{
558a9d82
YQ
15204 create_breakpoint (get_current_arch (),
15205 arg,
15206 NULL, 0, NULL, 1 /* parse arg */,
15207 0 /* tempflag */,
15208 bp_fast_tracepoint /* type_wanted */,
15209 0 /* Ignore count */,
15210 pending_break_support,
15211 &tracepoint_breakpoint_ops,
15212 from_tty,
15213 1 /* enabled */,
15214 0 /* internal */, 0);
0fb4aa4b
PA
15215}
15216
15217/* strace command implementation. Creates a static tracepoint. */
15218
70221824 15219static void
0fb4aa4b
PA
15220strace_command (char *arg, int from_tty)
15221{
983af33b
SDJ
15222 struct breakpoint_ops *ops;
15223
15224 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15225 or with a normal static tracepoint. */
15226 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15227 ops = &strace_marker_breakpoint_ops;
15228 else
15229 ops = &tracepoint_breakpoint_ops;
15230
558a9d82
YQ
15231 create_breakpoint (get_current_arch (),
15232 arg,
15233 NULL, 0, NULL, 1 /* parse arg */,
15234 0 /* tempflag */,
15235 bp_static_tracepoint /* type_wanted */,
15236 0 /* Ignore count */,
15237 pending_break_support,
15238 ops,
15239 from_tty,
15240 1 /* enabled */,
15241 0 /* internal */, 0);
7a697b8d
SS
15242}
15243
409873ef
SS
15244/* Set up a fake reader function that gets command lines from a linked
15245 list that was acquired during tracepoint uploading. */
15246
15247static struct uploaded_tp *this_utp;
3149d8c1 15248static int next_cmd;
409873ef
SS
15249
15250static char *
15251read_uploaded_action (void)
15252{
15253 char *rslt;
15254
3149d8c1 15255 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15256
3149d8c1 15257 next_cmd++;
409873ef
SS
15258
15259 return rslt;
15260}
15261
00bf0b85
SS
15262/* Given information about a tracepoint as recorded on a target (which
15263 can be either a live system or a trace file), attempt to create an
15264 equivalent GDB tracepoint. This is not a reliable process, since
15265 the target does not necessarily have all the information used when
15266 the tracepoint was originally defined. */
15267
d9b3f62e 15268struct tracepoint *
00bf0b85 15269create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15270{
409873ef 15271 char *addr_str, small_buf[100];
d9b3f62e 15272 struct tracepoint *tp;
fd9b8c24 15273
409873ef
SS
15274 if (utp->at_string)
15275 addr_str = utp->at_string;
15276 else
15277 {
15278 /* In the absence of a source location, fall back to raw
15279 address. Since there is no way to confirm that the address
15280 means the same thing as when the trace was started, warn the
15281 user. */
3e43a32a
MS
15282 warning (_("Uploaded tracepoint %d has no "
15283 "source location, using raw address"),
409873ef 15284 utp->number);
8c042590 15285 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15286 addr_str = small_buf;
15287 }
15288
15289 /* There's not much we can do with a sequence of bytecodes. */
15290 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15291 warning (_("Uploaded tracepoint %d condition "
15292 "has no source form, ignoring it"),
409873ef 15293 utp->number);
d5551862 15294
8cdf0e15 15295 if (!create_breakpoint (get_current_arch (),
409873ef 15296 addr_str,
e7e0cddf
SS
15297 utp->cond_string, -1, NULL,
15298 0 /* parse cond/thread */,
8cdf0e15 15299 0 /* tempflag */,
0fb4aa4b 15300 utp->type /* type_wanted */,
8cdf0e15
VP
15301 0 /* Ignore count */,
15302 pending_break_support,
348d480f 15303 &tracepoint_breakpoint_ops,
8cdf0e15 15304 0 /* from_tty */,
84f4c1fe 15305 utp->enabled /* enabled */,
44f238bb
PA
15306 0 /* internal */,
15307 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15308 return NULL;
15309
409873ef 15310 /* Get the tracepoint we just created. */
fd9b8c24
PA
15311 tp = get_tracepoint (tracepoint_count);
15312 gdb_assert (tp != NULL);
d5551862 15313
00bf0b85
SS
15314 if (utp->pass > 0)
15315 {
8c042590
PM
15316 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15317 tp->base.number);
00bf0b85 15318
409873ef 15319 trace_pass_command (small_buf, 0);
00bf0b85
SS
15320 }
15321
409873ef
SS
15322 /* If we have uploaded versions of the original commands, set up a
15323 special-purpose "reader" function and call the usual command line
15324 reader, then pass the result to the breakpoint command-setting
15325 function. */
3149d8c1 15326 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15327 {
409873ef 15328 struct command_line *cmd_list;
00bf0b85 15329
409873ef 15330 this_utp = utp;
3149d8c1 15331 next_cmd = 0;
d5551862 15332
409873ef
SS
15333 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15334
d9b3f62e 15335 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15336 }
3149d8c1
SS
15337 else if (!VEC_empty (char_ptr, utp->actions)
15338 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15339 warning (_("Uploaded tracepoint %d actions "
15340 "have no source form, ignoring them"),
409873ef 15341 utp->number);
00bf0b85 15342
f196051f
SS
15343 /* Copy any status information that might be available. */
15344 tp->base.hit_count = utp->hit_count;
15345 tp->traceframe_usage = utp->traceframe_usage;
15346
00bf0b85 15347 return tp;
d9b3f62e 15348}
00bf0b85 15349
1042e4c0
SS
15350/* Print information on tracepoint number TPNUM_EXP, or all if
15351 omitted. */
15352
15353static void
e5a67952 15354tracepoints_info (char *args, int from_tty)
1042e4c0 15355{
79a45e25 15356 struct ui_out *uiout = current_uiout;
e5a67952 15357 int num_printed;
1042e4c0 15358
e5a67952 15359 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15360
15361 if (num_printed == 0)
1042e4c0 15362 {
e5a67952 15363 if (args == NULL || *args == '\0')
d77f58be
SS
15364 ui_out_message (uiout, 0, "No tracepoints.\n");
15365 else
e5a67952 15366 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15367 }
ad443146
SS
15368
15369 default_collect_info ();
1042e4c0
SS
15370}
15371
4a64f543 15372/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15373 Not supported by all targets. */
15374static void
15375enable_trace_command (char *args, int from_tty)
15376{
15377 enable_command (args, from_tty);
15378}
15379
4a64f543 15380/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15381 Not supported by all targets. */
15382static void
15383disable_trace_command (char *args, int from_tty)
15384{
15385 disable_command (args, from_tty);
15386}
15387
4a64f543 15388/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15389static void
15390delete_trace_command (char *arg, int from_tty)
15391{
35df4500 15392 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15393
15394 dont_repeat ();
15395
15396 if (arg == 0)
15397 {
15398 int breaks_to_delete = 0;
15399
15400 /* Delete all breakpoints if no argument.
15401 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15402 have to be deleted with an explicit breakpoint number
15403 argument. */
1042e4c0 15404 ALL_TRACEPOINTS (b)
46c6471b 15405 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15406 {
15407 breaks_to_delete = 1;
15408 break;
15409 }
1042e4c0
SS
15410
15411 /* Ask user only if there are some breakpoints to delete. */
15412 if (!from_tty
15413 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15414 {
35df4500 15415 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15416 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15417 delete_breakpoint (b);
1042e4c0
SS
15418 }
15419 }
15420 else
51be5b68 15421 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15422}
15423
197f0a60
TT
15424/* Helper function for trace_pass_command. */
15425
15426static void
d9b3f62e 15427trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15428{
d9b3f62e 15429 tp->pass_count = count;
6f6484cd 15430 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15431 if (from_tty)
15432 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15433 tp->base.number, count);
197f0a60
TT
15434}
15435
1042e4c0
SS
15436/* Set passcount for tracepoint.
15437
15438 First command argument is passcount, second is tracepoint number.
15439 If tracepoint number omitted, apply to most recently defined.
15440 Also accepts special argument "all". */
15441
15442static void
15443trace_pass_command (char *args, int from_tty)
15444{
d9b3f62e 15445 struct tracepoint *t1;
1042e4c0 15446 unsigned int count;
1042e4c0
SS
15447
15448 if (args == 0 || *args == 0)
3e43a32a
MS
15449 error (_("passcount command requires an "
15450 "argument (count + optional TP num)"));
1042e4c0 15451
4a64f543 15452 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15453
529480d0 15454 args = skip_spaces (args);
1042e4c0
SS
15455 if (*args && strncasecmp (args, "all", 3) == 0)
15456 {
d9b3f62e
PA
15457 struct breakpoint *b;
15458
1042e4c0 15459 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15460 if (*args)
15461 error (_("Junk at end of arguments."));
1042e4c0 15462
d9b3f62e 15463 ALL_TRACEPOINTS (b)
197f0a60 15464 {
d9b3f62e 15465 t1 = (struct tracepoint *) b;
197f0a60
TT
15466 trace_pass_set_count (t1, count, from_tty);
15467 }
15468 }
15469 else if (*args == '\0')
1042e4c0 15470 {
197f0a60 15471 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 15472 if (t1)
197f0a60
TT
15473 trace_pass_set_count (t1, count, from_tty);
15474 }
15475 else
15476 {
15477 struct get_number_or_range_state state;
15478
15479 init_number_or_range (&state, args);
15480 while (!state.finished)
1042e4c0 15481 {
197f0a60
TT
15482 t1 = get_tracepoint_by_number (&args, &state, 1);
15483 if (t1)
15484 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15485 }
15486 }
1042e4c0
SS
15487}
15488
d9b3f62e 15489struct tracepoint *
1042e4c0
SS
15490get_tracepoint (int num)
15491{
15492 struct breakpoint *t;
15493
15494 ALL_TRACEPOINTS (t)
15495 if (t->number == num)
d9b3f62e 15496 return (struct tracepoint *) t;
1042e4c0
SS
15497
15498 return NULL;
15499}
15500
d5551862
SS
15501/* Find the tracepoint with the given target-side number (which may be
15502 different from the tracepoint number after disconnecting and
15503 reconnecting). */
15504
d9b3f62e 15505struct tracepoint *
d5551862
SS
15506get_tracepoint_by_number_on_target (int num)
15507{
d9b3f62e 15508 struct breakpoint *b;
d5551862 15509
d9b3f62e
PA
15510 ALL_TRACEPOINTS (b)
15511 {
15512 struct tracepoint *t = (struct tracepoint *) b;
15513
15514 if (t->number_on_target == num)
15515 return t;
15516 }
d5551862
SS
15517
15518 return NULL;
15519}
15520
1042e4c0 15521/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
15522 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15523 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 15524 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 15525struct tracepoint *
197f0a60
TT
15526get_tracepoint_by_number (char **arg,
15527 struct get_number_or_range_state *state,
15528 int optional_p)
1042e4c0 15529{
1042e4c0
SS
15530 struct breakpoint *t;
15531 int tpnum;
15532 char *instring = arg == NULL ? NULL : *arg;
15533
197f0a60
TT
15534 if (state)
15535 {
15536 gdb_assert (!state->finished);
15537 tpnum = get_number_or_range (state);
15538 }
15539 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
15540 {
15541 if (optional_p)
15542 tpnum = tracepoint_count;
15543 else
15544 error_no_arg (_("tracepoint number"));
15545 }
15546 else
197f0a60 15547 tpnum = get_number (arg);
1042e4c0
SS
15548
15549 if (tpnum <= 0)
15550 {
15551 if (instring && *instring)
15552 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15553 instring);
15554 else
3e43a32a
MS
15555 printf_filtered (_("Tracepoint argument missing "
15556 "and no previous tracepoint\n"));
1042e4c0
SS
15557 return NULL;
15558 }
15559
15560 ALL_TRACEPOINTS (t)
15561 if (t->number == tpnum)
15562 {
d9b3f62e 15563 return (struct tracepoint *) t;
1042e4c0
SS
15564 }
15565
1042e4c0
SS
15566 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15567 return NULL;
15568}
15569
d9b3f62e
PA
15570void
15571print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15572{
15573 if (b->thread != -1)
15574 fprintf_unfiltered (fp, " thread %d", b->thread);
15575
15576 if (b->task != 0)
15577 fprintf_unfiltered (fp, " task %d", b->task);
15578
15579 fprintf_unfiltered (fp, "\n");
15580}
15581
6149aea9
PA
15582/* Save information on user settable breakpoints (watchpoints, etc) to
15583 a new script file named FILENAME. If FILTER is non-NULL, call it
15584 on each breakpoint and only include the ones for which it returns
15585 non-zero. */
15586
1042e4c0 15587static void
6149aea9
PA
15588save_breakpoints (char *filename, int from_tty,
15589 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15590{
15591 struct breakpoint *tp;
6149aea9 15592 int any = 0;
1042e4c0 15593 struct cleanup *cleanup;
a7bdde9e 15594 struct ui_file *fp;
6149aea9 15595 int extra_trace_bits = 0;
1042e4c0 15596
6149aea9
PA
15597 if (filename == 0 || *filename == 0)
15598 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15599
15600 /* See if we have anything to save. */
6149aea9 15601 ALL_BREAKPOINTS (tp)
1042e4c0 15602 {
6149aea9 15603 /* Skip internal and momentary breakpoints. */
09d682a4 15604 if (!user_breakpoint_p (tp))
6149aea9
PA
15605 continue;
15606
15607 /* If we have a filter, only save the breakpoints it accepts. */
15608 if (filter && !filter (tp))
15609 continue;
15610
15611 any = 1;
15612
15613 if (is_tracepoint (tp))
15614 {
15615 extra_trace_bits = 1;
15616
15617 /* We can stop searching. */
15618 break;
15619 }
1042e4c0 15620 }
6149aea9
PA
15621
15622 if (!any)
1042e4c0 15623 {
6149aea9 15624 warning (_("Nothing to save."));
1042e4c0
SS
15625 return;
15626 }
15627
c718be47
PA
15628 filename = tilde_expand (filename);
15629 cleanup = make_cleanup (xfree, filename);
15630 fp = gdb_fopen (filename, "w");
059fb39f 15631 if (!fp)
6149aea9
PA
15632 error (_("Unable to open file '%s' for saving (%s)"),
15633 filename, safe_strerror (errno));
a7bdde9e 15634 make_cleanup_ui_file_delete (fp);
8bf6485c 15635
6149aea9
PA
15636 if (extra_trace_bits)
15637 save_trace_state_variables (fp);
8bf6485c 15638
6149aea9 15639 ALL_BREAKPOINTS (tp)
1042e4c0 15640 {
6149aea9 15641 /* Skip internal and momentary breakpoints. */
09d682a4 15642 if (!user_breakpoint_p (tp))
6149aea9 15643 continue;
8bf6485c 15644
6149aea9
PA
15645 /* If we have a filter, only save the breakpoints it accepts. */
15646 if (filter && !filter (tp))
15647 continue;
15648
348d480f 15649 tp->ops->print_recreate (tp, fp);
1042e4c0 15650
6149aea9
PA
15651 /* Note, we can't rely on tp->number for anything, as we can't
15652 assume the recreated breakpoint numbers will match. Use $bpnum
15653 instead. */
15654
15655 if (tp->cond_string)
15656 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15657
15658 if (tp->ignore_count)
15659 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15660
2d9442cc 15661 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15662 {
a7bdde9e
VP
15663 volatile struct gdb_exception ex;
15664
6149aea9 15665 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15666
79a45e25 15667 ui_out_redirect (current_uiout, fp);
14dba4b4 15668 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15669 {
79a45e25 15670 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15671 }
79a45e25 15672 ui_out_redirect (current_uiout, NULL);
1042e4c0 15673
a7bdde9e
VP
15674 if (ex.reason < 0)
15675 throw_exception (ex);
1042e4c0 15676
a7bdde9e 15677 fprintf_unfiltered (fp, " end\n");
1042e4c0 15678 }
6149aea9
PA
15679
15680 if (tp->enable_state == bp_disabled)
15681 fprintf_unfiltered (fp, "disable\n");
15682
15683 /* If this is a multi-location breakpoint, check if the locations
15684 should be individually disabled. Watchpoint locations are
15685 special, and not user visible. */
15686 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15687 {
15688 struct bp_location *loc;
15689 int n = 1;
15690
15691 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15692 if (!loc->enabled)
15693 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15694 }
1042e4c0 15695 }
8bf6485c 15696
6149aea9 15697 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15698 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15699
1042e4c0 15700 if (from_tty)
6149aea9 15701 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15702 do_cleanups (cleanup);
6149aea9
PA
15703}
15704
15705/* The `save breakpoints' command. */
15706
15707static void
15708save_breakpoints_command (char *args, int from_tty)
15709{
15710 save_breakpoints (args, from_tty, NULL);
15711}
15712
15713/* The `save tracepoints' command. */
15714
15715static void
15716save_tracepoints_command (char *args, int from_tty)
15717{
15718 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15719}
15720
15721/* Create a vector of all tracepoints. */
15722
15723VEC(breakpoint_p) *
eeae04df 15724all_tracepoints (void)
1042e4c0
SS
15725{
15726 VEC(breakpoint_p) *tp_vec = 0;
15727 struct breakpoint *tp;
15728
15729 ALL_TRACEPOINTS (tp)
15730 {
15731 VEC_safe_push (breakpoint_p, tp_vec, tp);
15732 }
15733
15734 return tp_vec;
15735}
15736
c906108c 15737\f
4a64f543
MS
15738/* This help string is used for the break, hbreak, tbreak and thbreak
15739 commands. It is defined as a macro to prevent duplication.
15740 COMMAND should be a string constant containing the name of the
15741 command. */
31e2b00f 15742#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15743command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15744PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15745probe point. Accepted values are `-probe' (for a generic, automatically\n\
15746guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
15747LOCATION may be a line number, function name, or \"*\" and an address.\n\
15748If a line number is specified, break at start of code for that line.\n\
15749If a function is specified, break at start of code for that function.\n\
15750If an address is specified, break at that exact address.\n\
dc10affe
PA
15751With no LOCATION, uses current execution address of the selected\n\
15752stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15753\n\
15754THREADNUM is the number from \"info threads\".\n\
15755CONDITION is a boolean expression.\n\
15756\n\
d41c0fc8
PA
15757Multiple breakpoints at one place are permitted, and useful if their\n\
15758conditions are different.\n\
31e2b00f
AS
15759\n\
15760Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15761
44feb3ce
TT
15762/* List of subcommands for "catch". */
15763static struct cmd_list_element *catch_cmdlist;
15764
15765/* List of subcommands for "tcatch". */
15766static struct cmd_list_element *tcatch_cmdlist;
15767
9ac4176b 15768void
44feb3ce
TT
15769add_catch_command (char *name, char *docstring,
15770 void (*sfunc) (char *args, int from_tty,
15771 struct cmd_list_element *command),
625e8578 15772 completer_ftype *completer,
44feb3ce
TT
15773 void *user_data_catch,
15774 void *user_data_tcatch)
15775{
15776 struct cmd_list_element *command;
15777
15778 command = add_cmd (name, class_breakpoint, NULL, docstring,
15779 &catch_cmdlist);
15780 set_cmd_sfunc (command, sfunc);
15781 set_cmd_context (command, user_data_catch);
a96d9b2e 15782 set_cmd_completer (command, completer);
44feb3ce
TT
15783
15784 command = add_cmd (name, class_breakpoint, NULL, docstring,
15785 &tcatch_cmdlist);
15786 set_cmd_sfunc (command, sfunc);
15787 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15788 set_cmd_completer (command, completer);
44feb3ce
TT
15789}
15790
6c95b8df 15791static void
a79b8f6e 15792clear_syscall_counts (struct inferior *inf)
6c95b8df 15793{
fa3064dd
YQ
15794 struct catch_syscall_inferior_data *inf_data
15795 = get_catch_syscall_inferior_data (inf);
15796
15797 inf_data->total_syscalls_count = 0;
15798 inf_data->any_syscall_count = 0;
15799 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
15800}
15801
6149aea9
PA
15802static void
15803save_command (char *arg, int from_tty)
15804{
3e43a32a
MS
15805 printf_unfiltered (_("\"save\" must be followed by "
15806 "the name of a save subcommand.\n"));
6149aea9
PA
15807 help_list (save_cmdlist, "save ", -1, gdb_stdout);
15808}
15809
84f4c1fe
PM
15810struct breakpoint *
15811iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15812 void *data)
15813{
35df4500 15814 struct breakpoint *b, *b_tmp;
84f4c1fe 15815
35df4500 15816 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15817 {
15818 if ((*callback) (b, data))
15819 return b;
15820 }
15821
15822 return NULL;
15823}
15824
0574c78f
GB
15825/* Zero if any of the breakpoint's locations could be a location where
15826 functions have been inlined, nonzero otherwise. */
15827
15828static int
15829is_non_inline_function (struct breakpoint *b)
15830{
15831 /* The shared library event breakpoint is set on the address of a
15832 non-inline function. */
15833 if (b->type == bp_shlib_event)
15834 return 1;
15835
15836 return 0;
15837}
15838
15839/* Nonzero if the specified PC cannot be a location where functions
15840 have been inlined. */
15841
15842int
09ac7c10
TT
15843pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15844 const struct target_waitstatus *ws)
0574c78f
GB
15845{
15846 struct breakpoint *b;
15847 struct bp_location *bl;
15848
15849 ALL_BREAKPOINTS (b)
15850 {
15851 if (!is_non_inline_function (b))
15852 continue;
15853
15854 for (bl = b->loc; bl != NULL; bl = bl->next)
15855 {
15856 if (!bl->shlib_disabled
09ac7c10 15857 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15858 return 1;
15859 }
15860 }
15861
15862 return 0;
15863}
15864
2f202fde
JK
15865/* Remove any references to OBJFILE which is going to be freed. */
15866
15867void
15868breakpoint_free_objfile (struct objfile *objfile)
15869{
15870 struct bp_location **locp, *loc;
15871
15872 ALL_BP_LOCATIONS (loc, locp)
15873 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
15874 loc->symtab = NULL;
15875}
15876
2060206e
PA
15877void
15878initialize_breakpoint_ops (void)
15879{
15880 static int initialized = 0;
15881
15882 struct breakpoint_ops *ops;
15883
15884 if (initialized)
15885 return;
15886 initialized = 1;
15887
15888 /* The breakpoint_ops structure to be inherit by all kinds of
15889 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15890 internal and momentary breakpoints, etc.). */
15891 ops = &bkpt_base_breakpoint_ops;
15892 *ops = base_breakpoint_ops;
15893 ops->re_set = bkpt_re_set;
15894 ops->insert_location = bkpt_insert_location;
15895 ops->remove_location = bkpt_remove_location;
15896 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
15897 ops->create_sals_from_address = bkpt_create_sals_from_address;
15898 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15899 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
15900
15901 /* The breakpoint_ops structure to be used in regular breakpoints. */
15902 ops = &bkpt_breakpoint_ops;
15903 *ops = bkpt_base_breakpoint_ops;
15904 ops->re_set = bkpt_re_set;
15905 ops->resources_needed = bkpt_resources_needed;
15906 ops->print_it = bkpt_print_it;
15907 ops->print_mention = bkpt_print_mention;
15908 ops->print_recreate = bkpt_print_recreate;
15909
15910 /* Ranged breakpoints. */
15911 ops = &ranged_breakpoint_ops;
15912 *ops = bkpt_breakpoint_ops;
15913 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15914 ops->resources_needed = resources_needed_ranged_breakpoint;
15915 ops->print_it = print_it_ranged_breakpoint;
15916 ops->print_one = print_one_ranged_breakpoint;
15917 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15918 ops->print_mention = print_mention_ranged_breakpoint;
15919 ops->print_recreate = print_recreate_ranged_breakpoint;
15920
15921 /* Internal breakpoints. */
15922 ops = &internal_breakpoint_ops;
15923 *ops = bkpt_base_breakpoint_ops;
15924 ops->re_set = internal_bkpt_re_set;
15925 ops->check_status = internal_bkpt_check_status;
15926 ops->print_it = internal_bkpt_print_it;
15927 ops->print_mention = internal_bkpt_print_mention;
15928
15929 /* Momentary breakpoints. */
15930 ops = &momentary_breakpoint_ops;
15931 *ops = bkpt_base_breakpoint_ops;
15932 ops->re_set = momentary_bkpt_re_set;
15933 ops->check_status = momentary_bkpt_check_status;
15934 ops->print_it = momentary_bkpt_print_it;
15935 ops->print_mention = momentary_bkpt_print_mention;
15936
e2e4d78b
JK
15937 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15938 ops = &longjmp_breakpoint_ops;
15939 *ops = momentary_breakpoint_ops;
15940 ops->dtor = longjmp_bkpt_dtor;
15941
55aa24fb
SDJ
15942 /* Probe breakpoints. */
15943 ops = &bkpt_probe_breakpoint_ops;
15944 *ops = bkpt_breakpoint_ops;
15945 ops->insert_location = bkpt_probe_insert_location;
15946 ops->remove_location = bkpt_probe_remove_location;
15947 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15948 ops->decode_linespec = bkpt_probe_decode_linespec;
15949
2060206e
PA
15950 /* Watchpoints. */
15951 ops = &watchpoint_breakpoint_ops;
15952 *ops = base_breakpoint_ops;
3a5c3e22 15953 ops->dtor = dtor_watchpoint;
2060206e
PA
15954 ops->re_set = re_set_watchpoint;
15955 ops->insert_location = insert_watchpoint;
15956 ops->remove_location = remove_watchpoint;
15957 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15958 ops->check_status = check_status_watchpoint;
15959 ops->resources_needed = resources_needed_watchpoint;
15960 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15961 ops->print_it = print_it_watchpoint;
15962 ops->print_mention = print_mention_watchpoint;
15963 ops->print_recreate = print_recreate_watchpoint;
427cd150 15964 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15965
15966 /* Masked watchpoints. */
15967 ops = &masked_watchpoint_breakpoint_ops;
15968 *ops = watchpoint_breakpoint_ops;
15969 ops->insert_location = insert_masked_watchpoint;
15970 ops->remove_location = remove_masked_watchpoint;
15971 ops->resources_needed = resources_needed_masked_watchpoint;
15972 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15973 ops->print_it = print_it_masked_watchpoint;
15974 ops->print_one_detail = print_one_detail_masked_watchpoint;
15975 ops->print_mention = print_mention_masked_watchpoint;
15976 ops->print_recreate = print_recreate_masked_watchpoint;
15977
15978 /* Tracepoints. */
15979 ops = &tracepoint_breakpoint_ops;
15980 *ops = base_breakpoint_ops;
15981 ops->re_set = tracepoint_re_set;
15982 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15983 ops->print_one_detail = tracepoint_print_one_detail;
15984 ops->print_mention = tracepoint_print_mention;
15985 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
15986 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15987 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15988 ops->decode_linespec = tracepoint_decode_linespec;
15989
55aa24fb
SDJ
15990 /* Probe tracepoints. */
15991 ops = &tracepoint_probe_breakpoint_ops;
15992 *ops = tracepoint_breakpoint_ops;
15993 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15994 ops->decode_linespec = tracepoint_probe_decode_linespec;
15995
983af33b
SDJ
15996 /* Static tracepoints with marker (`-m'). */
15997 ops = &strace_marker_breakpoint_ops;
15998 *ops = tracepoint_breakpoint_ops;
15999 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16000 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16001 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16002
16003 /* Fork catchpoints. */
16004 ops = &catch_fork_breakpoint_ops;
16005 *ops = base_breakpoint_ops;
16006 ops->insert_location = insert_catch_fork;
16007 ops->remove_location = remove_catch_fork;
16008 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16009 ops->print_it = print_it_catch_fork;
16010 ops->print_one = print_one_catch_fork;
16011 ops->print_mention = print_mention_catch_fork;
16012 ops->print_recreate = print_recreate_catch_fork;
16013
16014 /* Vfork catchpoints. */
16015 ops = &catch_vfork_breakpoint_ops;
16016 *ops = base_breakpoint_ops;
16017 ops->insert_location = insert_catch_vfork;
16018 ops->remove_location = remove_catch_vfork;
16019 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16020 ops->print_it = print_it_catch_vfork;
16021 ops->print_one = print_one_catch_vfork;
16022 ops->print_mention = print_mention_catch_vfork;
16023 ops->print_recreate = print_recreate_catch_vfork;
16024
16025 /* Exec catchpoints. */
16026 ops = &catch_exec_breakpoint_ops;
16027 *ops = base_breakpoint_ops;
16028 ops->dtor = dtor_catch_exec;
16029 ops->insert_location = insert_catch_exec;
16030 ops->remove_location = remove_catch_exec;
16031 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16032 ops->print_it = print_it_catch_exec;
16033 ops->print_one = print_one_catch_exec;
16034 ops->print_mention = print_mention_catch_exec;
16035 ops->print_recreate = print_recreate_catch_exec;
16036
16037 /* Syscall catchpoints. */
16038 ops = &catch_syscall_breakpoint_ops;
16039 *ops = base_breakpoint_ops;
16040 ops->dtor = dtor_catch_syscall;
16041 ops->insert_location = insert_catch_syscall;
16042 ops->remove_location = remove_catch_syscall;
16043 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16044 ops->print_it = print_it_catch_syscall;
16045 ops->print_one = print_one_catch_syscall;
16046 ops->print_mention = print_mention_catch_syscall;
16047 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16048
16049 /* Solib-related catchpoints. */
16050 ops = &catch_solib_breakpoint_ops;
16051 *ops = base_breakpoint_ops;
16052 ops->dtor = dtor_catch_solib;
16053 ops->insert_location = insert_catch_solib;
16054 ops->remove_location = remove_catch_solib;
16055 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16056 ops->check_status = check_status_catch_solib;
16057 ops->print_it = print_it_catch_solib;
16058 ops->print_one = print_one_catch_solib;
16059 ops->print_mention = print_mention_catch_solib;
16060 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16061
16062 ops = &dprintf_breakpoint_ops;
16063 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16064 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16065 ops->resources_needed = bkpt_resources_needed;
16066 ops->print_it = bkpt_print_it;
16067 ops->print_mention = bkpt_print_mention;
2d9442cc 16068 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16069 ops->after_condition_true = dprintf_after_condition_true;
2060206e
PA
16070}
16071
8bfd80db
YQ
16072/* Chain containing all defined "enable breakpoint" subcommands. */
16073
16074static struct cmd_list_element *enablebreaklist = NULL;
16075
c906108c 16076void
fba45db2 16077_initialize_breakpoint (void)
c906108c
SS
16078{
16079 struct cmd_list_element *c;
16080
2060206e
PA
16081 initialize_breakpoint_ops ();
16082
84acb35a 16083 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16084 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16085 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16086 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16087
55aa24fb
SDJ
16088 breakpoint_objfile_key
16089 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16090
fa3064dd 16091 catch_syscall_inferior_data
8e260fc0
TT
16092 = register_inferior_data_with_cleanup (NULL,
16093 catch_syscall_inferior_data_cleanup);
fa3064dd 16094
c906108c
SS
16095 breakpoint_chain = 0;
16096 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16097 before a breakpoint is set. */
16098 breakpoint_count = 0;
16099
1042e4c0
SS
16100 tracepoint_count = 0;
16101
1bedd215
AC
16102 add_com ("ignore", class_breakpoint, ignore_command, _("\
16103Set ignore-count of breakpoint number N to COUNT.\n\
16104Usage is `ignore N COUNT'."));
c906108c 16105 if (xdb_commands)
c5aa993b 16106 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16107
1bedd215
AC
16108 add_com ("commands", class_breakpoint, commands_command, _("\
16109Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16110Give breakpoint number as argument after \"commands\".\n\
16111With no argument, the targeted breakpoint is the last one set.\n\
16112The commands themselves follow starting on the next line.\n\
16113Type a line containing \"end\" to indicate the end of them.\n\
16114Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16115then no output is printed when it is hit, except what the commands print."));
c906108c 16116
d55637df 16117 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16118Specify breakpoint number N to break only if COND is true.\n\
c906108c 16119Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16120expression to be evaluated whenever breakpoint N is reached."));
d55637df 16121 set_cmd_completer (c, condition_completer);
c906108c 16122
1bedd215 16123 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16124Set a temporary breakpoint.\n\
c906108c
SS
16125Like \"break\" except the breakpoint is only temporary,\n\
16126so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16127by using \"enable delete\" on the breakpoint number.\n\
16128\n"
16129BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16130 set_cmd_completer (c, location_completer);
c94fdfd0 16131
1bedd215 16132 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16133Set a hardware assisted breakpoint.\n\
c906108c 16134Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16135some target hardware may not have this support.\n\
16136\n"
16137BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16138 set_cmd_completer (c, location_completer);
c906108c 16139
1bedd215 16140 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16141Set a temporary hardware assisted breakpoint.\n\
c906108c 16142Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16143so it will be deleted when hit.\n\
16144\n"
16145BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16146 set_cmd_completer (c, location_completer);
c906108c 16147
1bedd215
AC
16148 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16149Enable some breakpoints.\n\
c906108c
SS
16150Give breakpoint numbers (separated by spaces) as arguments.\n\
16151With no subcommand, breakpoints are enabled until you command otherwise.\n\
16152This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16153With a subcommand you can enable temporarily."),
c906108c
SS
16154 &enablelist, "enable ", 1, &cmdlist);
16155 if (xdb_commands)
1bedd215
AC
16156 add_com ("ab", class_breakpoint, enable_command, _("\
16157Enable some breakpoints.\n\
c906108c
SS
16158Give breakpoint numbers (separated by spaces) as arguments.\n\
16159With no subcommand, breakpoints are enabled until you command otherwise.\n\
16160This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16161With a subcommand you can enable temporarily."));
c906108c
SS
16162
16163 add_com_alias ("en", "enable", class_breakpoint, 1);
16164
84951ab5 16165 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16166Enable some breakpoints.\n\
c906108c
SS
16167Give breakpoint numbers (separated by spaces) as arguments.\n\
16168This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16169May be abbreviated to simply \"enable\".\n"),
c5aa993b 16170 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16171
1a966eab
AC
16172 add_cmd ("once", no_class, enable_once_command, _("\
16173Enable breakpoints for one hit. Give breakpoint numbers.\n\
16174If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16175 &enablebreaklist);
16176
1a966eab
AC
16177 add_cmd ("delete", no_class, enable_delete_command, _("\
16178Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16179If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16180 &enablebreaklist);
16181
816338b5
SS
16182 add_cmd ("count", no_class, enable_count_command, _("\
16183Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16184If a breakpoint is hit while enabled in this fashion,\n\
16185the count is decremented; when it reaches zero, the breakpoint is disabled."),
16186 &enablebreaklist);
16187
1a966eab
AC
16188 add_cmd ("delete", no_class, enable_delete_command, _("\
16189Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16190If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16191 &enablelist);
16192
1a966eab
AC
16193 add_cmd ("once", no_class, enable_once_command, _("\
16194Enable breakpoints for one hit. Give breakpoint numbers.\n\
16195If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16196 &enablelist);
16197
16198 add_cmd ("count", no_class, enable_count_command, _("\
16199Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16200If a breakpoint is hit while enabled in this fashion,\n\
16201the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16202 &enablelist);
16203
1bedd215
AC
16204 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16205Disable some breakpoints.\n\
c906108c
SS
16206Arguments are breakpoint numbers with spaces in between.\n\
16207To disable all breakpoints, give no argument.\n\
64b9b334 16208A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16209 &disablelist, "disable ", 1, &cmdlist);
16210 add_com_alias ("dis", "disable", class_breakpoint, 1);
16211 add_com_alias ("disa", "disable", class_breakpoint, 1);
16212 if (xdb_commands)
1bedd215
AC
16213 add_com ("sb", class_breakpoint, disable_command, _("\
16214Disable some breakpoints.\n\
c906108c
SS
16215Arguments are breakpoint numbers with spaces in between.\n\
16216To disable all breakpoints, give no argument.\n\
64b9b334 16217A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16218
1a966eab
AC
16219 add_cmd ("breakpoints", class_alias, disable_command, _("\
16220Disable some breakpoints.\n\
c906108c
SS
16221Arguments are breakpoint numbers with spaces in between.\n\
16222To disable all breakpoints, give no argument.\n\
64b9b334 16223A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16224This command may be abbreviated \"disable\"."),
c906108c
SS
16225 &disablelist);
16226
1bedd215
AC
16227 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16228Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16229Arguments are breakpoint numbers with spaces in between.\n\
16230To delete all breakpoints, give no argument.\n\
16231\n\
16232Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16233The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16234 &deletelist, "delete ", 1, &cmdlist);
16235 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16236 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16237 if (xdb_commands)
1bedd215
AC
16238 add_com ("db", class_breakpoint, delete_command, _("\
16239Delete some breakpoints.\n\
c906108c 16240Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16241To delete all breakpoints, give no argument.\n"));
c906108c 16242
1a966eab
AC
16243 add_cmd ("breakpoints", class_alias, delete_command, _("\
16244Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16245Arguments are breakpoint numbers with spaces in between.\n\
16246To delete all breakpoints, give no argument.\n\
1a966eab 16247This command may be abbreviated \"delete\"."),
c906108c
SS
16248 &deletelist);
16249
1bedd215
AC
16250 add_com ("clear", class_breakpoint, clear_command, _("\
16251Clear breakpoint at specified line or function.\n\
c906108c
SS
16252Argument may be line number, function name, or \"*\" and an address.\n\
16253If line number is specified, all breakpoints in that line are cleared.\n\
16254If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16255If an address is specified, breakpoints at that address are cleared.\n\
16256\n\
16257With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16258is executing in.\n\
16259\n\
1bedd215 16260See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16261 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16262
1bedd215 16263 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16264Set breakpoint at specified line or function.\n"
16265BREAK_ARGS_HELP ("break")));
5ba2abeb 16266 set_cmd_completer (c, location_completer);
c94fdfd0 16267
c906108c
SS
16268 add_com_alias ("b", "break", class_run, 1);
16269 add_com_alias ("br", "break", class_run, 1);
16270 add_com_alias ("bre", "break", class_run, 1);
16271 add_com_alias ("brea", "break", class_run, 1);
16272
7681d515
PM
16273 if (xdb_commands)
16274 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16275
16276 if (dbx_commands)
16277 {
1bedd215
AC
16278 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16279Break in function/address or break at a line in the current file."),
c5aa993b
JM
16280 &stoplist, "stop ", 1, &cmdlist);
16281 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16282 _("Break in function or address."), &stoplist);
c5aa993b 16283 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16284 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16285 add_com ("status", class_info, breakpoints_info, _("\
16286Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16287The \"Type\" column indicates one of:\n\
16288\tbreakpoint - normal breakpoint\n\
16289\twatchpoint - watchpoint\n\
16290The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16291the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16292breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16293address and file/line number respectively.\n\
16294\n\
16295Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16296are set to the address of the last breakpoint listed unless the command\n\
16297is prefixed with \"server \".\n\n\
c906108c 16298Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16299breakpoint set."));
c906108c
SS
16300 }
16301
1bedd215 16302 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16303Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16304The \"Type\" column indicates one of:\n\
16305\tbreakpoint - normal breakpoint\n\
16306\twatchpoint - watchpoint\n\
16307The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16308the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16309breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16310address and file/line number respectively.\n\
16311\n\
16312Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16313are set to the address of the last breakpoint listed unless the command\n\
16314is prefixed with \"server \".\n\n\
c906108c 16315Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16316breakpoint set."));
c906108c 16317
6b04bdb7
MS
16318 add_info_alias ("b", "breakpoints", 1);
16319
c906108c 16320 if (xdb_commands)
1bedd215
AC
16321 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16322Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16323The \"Type\" column indicates one of:\n\
16324\tbreakpoint - normal breakpoint\n\
16325\twatchpoint - watchpoint\n\
16326The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16327the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16328breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16329address and file/line number respectively.\n\
16330\n\
16331Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16332are set to the address of the last breakpoint listed unless the command\n\
16333is prefixed with \"server \".\n\n\
c906108c 16334Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16335breakpoint set."));
c906108c 16336
1a966eab
AC
16337 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16338Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16339The \"Type\" column indicates one of:\n\
16340\tbreakpoint - normal breakpoint\n\
16341\twatchpoint - watchpoint\n\
16342\tlongjmp - internal breakpoint used to step through longjmp()\n\
16343\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16344\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16345\tfinish - internal breakpoint used by the \"finish\" command\n\
16346The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16347the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16348breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16349address and file/line number respectively.\n\
16350\n\
16351Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16352are set to the address of the last breakpoint listed unless the command\n\
16353is prefixed with \"server \".\n\n\
c906108c 16354Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16355breakpoint set."),
c906108c
SS
16356 &maintenanceinfolist);
16357
44feb3ce
TT
16358 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16359Set catchpoints to catch events."),
16360 &catch_cmdlist, "catch ",
16361 0/*allow-unknown*/, &cmdlist);
16362
16363 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16364Set temporary catchpoints to catch events."),
16365 &tcatch_cmdlist, "tcatch ",
16366 0/*allow-unknown*/, &cmdlist);
16367
44feb3ce
TT
16368 add_catch_command ("fork", _("Catch calls to fork."),
16369 catch_fork_command_1,
a96d9b2e 16370 NULL,
44feb3ce
TT
16371 (void *) (uintptr_t) catch_fork_permanent,
16372 (void *) (uintptr_t) catch_fork_temporary);
16373 add_catch_command ("vfork", _("Catch calls to vfork."),
16374 catch_fork_command_1,
a96d9b2e 16375 NULL,
44feb3ce
TT
16376 (void *) (uintptr_t) catch_vfork_permanent,
16377 (void *) (uintptr_t) catch_vfork_temporary);
16378 add_catch_command ("exec", _("Catch calls to exec."),
16379 catch_exec_command_1,
a96d9b2e
SDJ
16380 NULL,
16381 CATCH_PERMANENT,
16382 CATCH_TEMPORARY);
edcc5120
TT
16383 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16384Usage: catch load [REGEX]\n\
16385If REGEX is given, only stop for libraries matching the regular expression."),
16386 catch_load_command_1,
16387 NULL,
16388 CATCH_PERMANENT,
16389 CATCH_TEMPORARY);
16390 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16391Usage: catch unload [REGEX]\n\
16392If REGEX is given, only stop for libraries matching the regular expression."),
16393 catch_unload_command_1,
16394 NULL,
16395 CATCH_PERMANENT,
16396 CATCH_TEMPORARY);
a96d9b2e
SDJ
16397 add_catch_command ("syscall", _("\
16398Catch system calls by their names and/or numbers.\n\
16399Arguments say which system calls to catch. If no arguments\n\
16400are given, every system call will be caught.\n\
16401Arguments, if given, should be one or more system call names\n\
16402(if your system supports that), or system call numbers."),
16403 catch_syscall_command_1,
16404 catch_syscall_completer,
44feb3ce
TT
16405 CATCH_PERMANENT,
16406 CATCH_TEMPORARY);
c5aa993b 16407
1bedd215
AC
16408 c = add_com ("watch", class_breakpoint, watch_command, _("\
16409Set a watchpoint for an expression.\n\
06a64a0b 16410Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16411A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16412an expression changes.\n\
16413If -l or -location is given, this evaluates EXPRESSION and watches\n\
16414the memory to which it refers."));
65d12d83 16415 set_cmd_completer (c, expression_completer);
c906108c 16416
1bedd215
AC
16417 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16418Set a read watchpoint for an expression.\n\
06a64a0b 16419Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16420A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16421an expression is read.\n\
16422If -l or -location is given, this evaluates EXPRESSION and watches\n\
16423the memory to which it refers."));
65d12d83 16424 set_cmd_completer (c, expression_completer);
c906108c 16425
1bedd215
AC
16426 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16427Set a watchpoint for an expression.\n\
06a64a0b 16428Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16429A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16430an expression is either read or written.\n\
16431If -l or -location is given, this evaluates EXPRESSION and watches\n\
16432the memory to which it refers."));
65d12d83 16433 set_cmd_completer (c, expression_completer);
c906108c 16434
d77f58be 16435 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16436Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16437
920d2a44
AC
16438 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16439 respond to changes - contrary to the description. */
85c07804
AC
16440 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16441 &can_use_hw_watchpoints, _("\
16442Set debugger's willingness to use watchpoint hardware."), _("\
16443Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16444If zero, gdb will not use hardware for new watchpoints, even if\n\
16445such is available. (However, any hardware watchpoints that were\n\
16446created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16447hardware.)"),
16448 NULL,
920d2a44 16449 show_can_use_hw_watchpoints,
85c07804 16450 &setlist, &showlist);
c906108c
SS
16451
16452 can_use_hw_watchpoints = 1;
fa8d40ab 16453
1042e4c0
SS
16454 /* Tracepoint manipulation commands. */
16455
16456 c = add_com ("trace", class_breakpoint, trace_command, _("\
16457Set a tracepoint at specified line or function.\n\
16458\n"
16459BREAK_ARGS_HELP ("trace") "\n\
16460Do \"help tracepoints\" for info on other tracepoint commands."));
16461 set_cmd_completer (c, location_completer);
16462
16463 add_com_alias ("tp", "trace", class_alias, 0);
16464 add_com_alias ("tr", "trace", class_alias, 1);
16465 add_com_alias ("tra", "trace", class_alias, 1);
16466 add_com_alias ("trac", "trace", class_alias, 1);
16467
7a697b8d
SS
16468 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16469Set a fast tracepoint at specified line or function.\n\
16470\n"
16471BREAK_ARGS_HELP ("ftrace") "\n\
16472Do \"help tracepoints\" for info on other tracepoint commands."));
16473 set_cmd_completer (c, location_completer);
16474
0fb4aa4b
PA
16475 c = add_com ("strace", class_breakpoint, strace_command, _("\
16476Set a static tracepoint at specified line, function or marker.\n\
16477\n\
16478strace [LOCATION] [if CONDITION]\n\
16479LOCATION may be a line number, function name, \"*\" and an address,\n\
16480or -m MARKER_ID.\n\
16481If a line number is specified, probe the marker at start of code\n\
16482for that line. If a function is specified, probe the marker at start\n\
16483of code for that function. If an address is specified, probe the marker\n\
16484at that exact address. If a marker id is specified, probe the marker\n\
16485with that name. With no LOCATION, uses current execution address of\n\
16486the selected stack frame.\n\
16487Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16488This collects arbitrary user data passed in the probe point call to the\n\
16489tracing library. You can inspect it when analyzing the trace buffer,\n\
16490by printing the $_sdata variable like any other convenience variable.\n\
16491\n\
16492CONDITION is a boolean expression.\n\
16493\n\
d41c0fc8
PA
16494Multiple tracepoints at one place are permitted, and useful if their\n\
16495conditions are different.\n\
0fb4aa4b
PA
16496\n\
16497Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16498Do \"help tracepoints\" for info on other tracepoint commands."));
16499 set_cmd_completer (c, location_completer);
16500
1042e4c0 16501 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16502Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16503Convenience variable \"$tpnum\" contains the number of the\n\
16504last tracepoint set."));
16505
16506 add_info_alias ("tp", "tracepoints", 1);
16507
16508 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16509Delete specified tracepoints.\n\
16510Arguments are tracepoint numbers, separated by spaces.\n\
16511No argument means delete all tracepoints."),
16512 &deletelist);
7e20dfcd 16513 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16514
16515 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16516Disable specified tracepoints.\n\
16517Arguments are tracepoint numbers, separated by spaces.\n\
16518No argument means disable all tracepoints."),
16519 &disablelist);
16520 deprecate_cmd (c, "disable");
16521
16522 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16523Enable specified tracepoints.\n\
16524Arguments are tracepoint numbers, separated by spaces.\n\
16525No argument means enable all tracepoints."),
16526 &enablelist);
16527 deprecate_cmd (c, "enable");
16528
16529 add_com ("passcount", class_trace, trace_pass_command, _("\
16530Set the passcount for a tracepoint.\n\
16531The trace will end when the tracepoint has been passed 'count' times.\n\
16532Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16533if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16534
6149aea9
PA
16535 add_prefix_cmd ("save", class_breakpoint, save_command,
16536 _("Save breakpoint definitions as a script."),
16537 &save_cmdlist, "save ",
16538 0/*allow-unknown*/, &cmdlist);
16539
16540 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16541Save current breakpoint definitions as a script.\n\
cce7e648 16542This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16543catchpoints, tracepoints). Use the 'source' command in another debug\n\
16544session to restore them."),
16545 &save_cmdlist);
16546 set_cmd_completer (c, filename_completer);
16547
16548 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16549Save current tracepoint definitions as a script.\n\
6149aea9
PA
16550Use the 'source' command in another debug session to restore them."),
16551 &save_cmdlist);
1042e4c0
SS
16552 set_cmd_completer (c, filename_completer);
16553
6149aea9
PA
16554 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16555 deprecate_cmd (c, "save tracepoints");
16556
1bedd215 16557 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16558Breakpoint specific settings\n\
16559Configure various breakpoint-specific variables such as\n\
1bedd215 16560pending breakpoint behavior"),
fa8d40ab
JJ
16561 &breakpoint_set_cmdlist, "set breakpoint ",
16562 0/*allow-unknown*/, &setlist);
1bedd215 16563 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16564Breakpoint specific settings\n\
16565Configure various breakpoint-specific variables such as\n\
1bedd215 16566pending breakpoint behavior"),
fa8d40ab
JJ
16567 &breakpoint_show_cmdlist, "show breakpoint ",
16568 0/*allow-unknown*/, &showlist);
16569
7915a72c
AC
16570 add_setshow_auto_boolean_cmd ("pending", no_class,
16571 &pending_break_support, _("\
16572Set debugger's behavior regarding pending breakpoints."), _("\
16573Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16574If on, an unrecognized breakpoint location will cause gdb to create a\n\
16575pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16576an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16577user-query to see if a pending breakpoint should be created."),
2c5b56ce 16578 NULL,
920d2a44 16579 show_pending_break_support,
6e1d7d6c
AC
16580 &breakpoint_set_cmdlist,
16581 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16582
16583 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16584
16585 add_setshow_boolean_cmd ("auto-hw", no_class,
16586 &automatic_hardware_breakpoints, _("\
16587Set automatic usage of hardware breakpoints."), _("\
16588Show automatic usage of hardware breakpoints."), _("\
16589If set, the debugger will automatically use hardware breakpoints for\n\
16590breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16591a warning will be emitted for such breakpoints."),
16592 NULL,
16593 show_automatic_hardware_breakpoints,
16594 &breakpoint_set_cmdlist,
16595 &breakpoint_show_cmdlist);
74960c60 16596
72d0e2c5
YQ
16597 add_setshow_auto_boolean_cmd ("always-inserted", class_support,
16598 &always_inserted_mode, _("\
74960c60
VP
16599Set mode for inserting breakpoints."), _("\
16600Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
16601When this mode is off, breakpoints are inserted in inferior when it is\n\
16602resumed, and removed when execution stops. When this mode is on,\n\
16603breakpoints are inserted immediately and removed only when the user\n\
16604deletes the breakpoint. When this mode is auto (which is the default),\n\
16605the behaviour depends on the non-stop setting (see help set non-stop).\n\
16606In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16607behaves as if always-inserted mode is on; if gdb is controlling the\n\
16608inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
72d0e2c5
YQ
16609 NULL,
16610 &show_always_inserted_mode,
16611 &breakpoint_set_cmdlist,
16612 &breakpoint_show_cmdlist);
f1310107 16613
b775012e
LM
16614 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16615 condition_evaluation_enums,
16616 &condition_evaluation_mode_1, _("\
16617Set mode of breakpoint condition evaluation."), _("\
16618Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16619When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16620evaluated on the host's side by GDB. When it is set to \"target\",\n\
16621breakpoint conditions will be downloaded to the target (if the target\n\
16622supports such feature) and conditions will be evaluated on the target's side.\n\
16623If this is set to \"auto\" (default), this will be automatically set to\n\
16624\"target\" if it supports condition evaluation, otherwise it will\n\
16625be set to \"gdb\""),
16626 &set_condition_evaluation_mode,
16627 &show_condition_evaluation_mode,
16628 &breakpoint_set_cmdlist,
16629 &breakpoint_show_cmdlist);
16630
f1310107
TJB
16631 add_com ("break-range", class_breakpoint, break_range_command, _("\
16632Set a breakpoint for an address range.\n\
16633break-range START-LOCATION, END-LOCATION\n\
16634where START-LOCATION and END-LOCATION can be one of the following:\n\
16635 LINENUM, for that line in the current file,\n\
16636 FILE:LINENUM, for that line in that file,\n\
16637 +OFFSET, for that number of lines after the current line\n\
16638 or the start of the range\n\
16639 FUNCTION, for the first line in that function,\n\
16640 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16641 *ADDRESS, for the instruction at that address.\n\
16642\n\
16643The breakpoint will stop execution of the inferior whenever it executes\n\
16644an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16645range (including START-LOCATION and END-LOCATION)."));
16646
e7e0cddf
SS
16647 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16648Set a dynamic printf at specified line or function.\n\
16649dprintf location,format string,arg1,arg2,...\n\
16650location may be a line number, function name, or \"*\" and an address.\n\
16651If a line number is specified, break at start of code for that line.\n\
0e4777df 16652If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16653 set_cmd_completer (c, location_completer);
16654
16655 add_setshow_enum_cmd ("dprintf-style", class_support,
16656 dprintf_style_enums, &dprintf_style, _("\
16657Set the style of usage for dynamic printf."), _("\
16658Show the style of usage for dynamic printf."), _("\
16659This setting chooses how GDB will do a dynamic printf.\n\
16660If the value is \"gdb\", then the printing is done by GDB to its own\n\
16661console, as with the \"printf\" command.\n\
16662If the value is \"call\", the print is done by calling a function in your\n\
16663program; by default printf(), but you can choose a different function or\n\
16664output stream by setting dprintf-function and dprintf-channel."),
16665 update_dprintf_commands, NULL,
16666 &setlist, &showlist);
16667
16668 dprintf_function = xstrdup ("printf");
16669 add_setshow_string_cmd ("dprintf-function", class_support,
16670 &dprintf_function, _("\
16671Set the function to use for dynamic printf"), _("\
16672Show the function to use for dynamic printf"), NULL,
16673 update_dprintf_commands, NULL,
16674 &setlist, &showlist);
16675
16676 dprintf_channel = xstrdup ("");
16677 add_setshow_string_cmd ("dprintf-channel", class_support,
16678 &dprintf_channel, _("\
16679Set the channel to use for dynamic printf"), _("\
16680Show the channel to use for dynamic printf"), NULL,
16681 update_dprintf_commands, NULL,
16682 &setlist, &showlist);
16683
d3ce09f5
SS
16684 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16685 &disconnected_dprintf, _("\
16686Set whether dprintf continues after GDB disconnects."), _("\
16687Show whether dprintf continues after GDB disconnects."), _("\
16688Use this to let dprintf commands continue to hit and produce output\n\
16689even if GDB disconnects or detaches from the target."),
16690 NULL,
16691 NULL,
16692 &setlist, &showlist);
16693
16694 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16695agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16696(target agent only) This is useful for formatted output in user-defined commands."));
16697
765dc015 16698 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16699
16700 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16701 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16702}
This page took 2.525754 seconds and 4 git commands to generate.