Extension Language API
[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"
6dddc817 82#include "extension.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 {
6dddc817
DE
1050 /* Check if this breakpoint has a "stop" method implemented in an
1051 extension language. This method and conditions entered into GDB
1052 from the CLI are mutually exclusive. */
1053 const struct extension_language_defn *extlang
1054 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1055
1056 if (extlang != NULL)
1057 {
1058 error (_("Only one stop condition allowed. There is currently"
1059 " a %s stop condition defined for this breakpoint."),
1060 ext_lang_capitalized_name (extlang));
1061 }
2566ad2d 1062 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1063
1064 if (is_breakpoint (b))
1065 update_global_location_list (1);
1066
2f069f6f
JB
1067 return;
1068 }
c906108c 1069
8a3fe4f8 1070 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1071}
1072
a7bdde9e
VP
1073/* Check that COMMAND do not contain commands that are suitable
1074 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1075 Throw if any such commands is found. */
1076
a7bdde9e
VP
1077static void
1078check_no_tracepoint_commands (struct command_line *commands)
1079{
1080 struct command_line *c;
cc59ec59 1081
a7bdde9e
VP
1082 for (c = commands; c; c = c->next)
1083 {
1084 int i;
1085
1086 if (c->control_type == while_stepping_control)
3e43a32a
MS
1087 error (_("The 'while-stepping' command can "
1088 "only be used for tracepoints"));
a7bdde9e
VP
1089
1090 for (i = 0; i < c->body_count; ++i)
1091 check_no_tracepoint_commands ((c->body_list)[i]);
1092
1093 /* Not that command parsing removes leading whitespace and comment
4a64f543 1094 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1095 command directly. */
1096 if (strstr (c->line, "collect ") == c->line)
1097 error (_("The 'collect' command can only be used for tracepoints"));
1098
51661e93
VP
1099 if (strstr (c->line, "teval ") == c->line)
1100 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1101 }
1102}
1103
d77f58be
SS
1104/* Encapsulate tests for different types of tracepoints. */
1105
d9b3f62e
PA
1106static int
1107is_tracepoint_type (enum bptype type)
1108{
1109 return (type == bp_tracepoint
1110 || type == bp_fast_tracepoint
1111 || type == bp_static_tracepoint);
1112}
1113
a7bdde9e 1114int
d77f58be 1115is_tracepoint (const struct breakpoint *b)
a7bdde9e 1116{
d9b3f62e 1117 return is_tracepoint_type (b->type);
a7bdde9e 1118}
d9b3f62e 1119
e5dd4106 1120/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1121 breakpoint. This function will throw an exception if a problem is
1122 found. */
48cb2d85 1123
95a42b64
TT
1124static void
1125validate_commands_for_breakpoint (struct breakpoint *b,
1126 struct command_line *commands)
48cb2d85 1127{
d77f58be 1128 if (is_tracepoint (b))
a7bdde9e 1129 {
c9a6ce02 1130 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1131 struct command_line *c;
1132 struct command_line *while_stepping = 0;
c9a6ce02
PA
1133
1134 /* Reset the while-stepping step count. The previous commands
1135 might have included a while-stepping action, while the new
1136 ones might not. */
1137 t->step_count = 0;
1138
1139 /* We need to verify that each top-level element of commands is
1140 valid for tracepoints, that there's at most one
1141 while-stepping element, and that the while-stepping's body
1142 has valid tracing commands excluding nested while-stepping.
1143 We also need to validate the tracepoint action line in the
1144 context of the tracepoint --- validate_actionline actually
1145 has side effects, like setting the tracepoint's
1146 while-stepping STEP_COUNT, in addition to checking if the
1147 collect/teval actions parse and make sense in the
1148 tracepoint's context. */
a7bdde9e
VP
1149 for (c = commands; c; c = c->next)
1150 {
a7bdde9e
VP
1151 if (c->control_type == while_stepping_control)
1152 {
1153 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1154 error (_("The 'while-stepping' command "
1155 "cannot be used for fast tracepoint"));
0fb4aa4b 1156 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1157 error (_("The 'while-stepping' command "
1158 "cannot be used for static tracepoint"));
a7bdde9e
VP
1159
1160 if (while_stepping)
3e43a32a
MS
1161 error (_("The 'while-stepping' command "
1162 "can be used only once"));
a7bdde9e
VP
1163 else
1164 while_stepping = c;
1165 }
c9a6ce02
PA
1166
1167 validate_actionline (c->line, b);
a7bdde9e
VP
1168 }
1169 if (while_stepping)
1170 {
1171 struct command_line *c2;
1172
1173 gdb_assert (while_stepping->body_count == 1);
1174 c2 = while_stepping->body_list[0];
1175 for (; c2; c2 = c2->next)
1176 {
a7bdde9e
VP
1177 if (c2->control_type == while_stepping_control)
1178 error (_("The 'while-stepping' command cannot be nested"));
1179 }
1180 }
1181 }
1182 else
1183 {
1184 check_no_tracepoint_commands (commands);
1185 }
95a42b64
TT
1186}
1187
0fb4aa4b
PA
1188/* Return a vector of all the static tracepoints set at ADDR. The
1189 caller is responsible for releasing the vector. */
1190
1191VEC(breakpoint_p) *
1192static_tracepoints_here (CORE_ADDR addr)
1193{
1194 struct breakpoint *b;
1195 VEC(breakpoint_p) *found = 0;
1196 struct bp_location *loc;
1197
1198 ALL_BREAKPOINTS (b)
1199 if (b->type == bp_static_tracepoint)
1200 {
1201 for (loc = b->loc; loc; loc = loc->next)
1202 if (loc->address == addr)
1203 VEC_safe_push(breakpoint_p, found, b);
1204 }
1205
1206 return found;
1207}
1208
95a42b64 1209/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1210 validate that only allowed commands are included. */
95a42b64
TT
1211
1212void
4a64f543
MS
1213breakpoint_set_commands (struct breakpoint *b,
1214 struct command_line *commands)
95a42b64
TT
1215{
1216 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1217
9add0f1b
TT
1218 decref_counted_command_line (&b->commands);
1219 b->commands = alloc_counted_command_line (commands);
8d3788bd 1220 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1221}
1222
45a43567
TT
1223/* Set the internal `silent' flag on the breakpoint. Note that this
1224 is not the same as the "silent" that may appear in the breakpoint's
1225 commands. */
1226
1227void
1228breakpoint_set_silent (struct breakpoint *b, int silent)
1229{
1230 int old_silent = b->silent;
1231
1232 b->silent = silent;
1233 if (old_silent != silent)
8d3788bd 1234 observer_notify_breakpoint_modified (b);
45a43567
TT
1235}
1236
1237/* Set the thread for this breakpoint. If THREAD is -1, make the
1238 breakpoint work for any thread. */
1239
1240void
1241breakpoint_set_thread (struct breakpoint *b, int thread)
1242{
1243 int old_thread = b->thread;
1244
1245 b->thread = thread;
1246 if (old_thread != thread)
8d3788bd 1247 observer_notify_breakpoint_modified (b);
45a43567
TT
1248}
1249
1250/* Set the task for this breakpoint. If TASK is 0, make the
1251 breakpoint work for any task. */
1252
1253void
1254breakpoint_set_task (struct breakpoint *b, int task)
1255{
1256 int old_task = b->task;
1257
1258 b->task = task;
1259 if (old_task != task)
8d3788bd 1260 observer_notify_breakpoint_modified (b);
45a43567
TT
1261}
1262
95a42b64
TT
1263void
1264check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1265{
1266 struct breakpoint *b = closure;
cc59ec59 1267
6f937416 1268 validate_actionline (line, b);
a7bdde9e
VP
1269}
1270
95a42b64
TT
1271/* A structure used to pass information through
1272 map_breakpoint_numbers. */
1273
1274struct commands_info
1275{
1276 /* True if the command was typed at a tty. */
1277 int from_tty;
86b17b60
PA
1278
1279 /* The breakpoint range spec. */
1280 char *arg;
1281
95a42b64
TT
1282 /* Non-NULL if the body of the commands are being read from this
1283 already-parsed command. */
1284 struct command_line *control;
86b17b60 1285
95a42b64
TT
1286 /* The command lines read from the user, or NULL if they have not
1287 yet been read. */
1288 struct counted_command_line *cmd;
1289};
1290
1291/* A callback for map_breakpoint_numbers that sets the commands for
1292 commands_command. */
1293
c906108c 1294static void
95a42b64 1295do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1296{
95a42b64 1297 struct commands_info *info = data;
c906108c 1298
95a42b64
TT
1299 if (info->cmd == NULL)
1300 {
1301 struct command_line *l;
5c44784c 1302
95a42b64
TT
1303 if (info->control != NULL)
1304 l = copy_command_lines (info->control->body_list[0]);
1305 else
86b17b60
PA
1306 {
1307 struct cleanup *old_chain;
1308 char *str;
c5aa993b 1309
3e43a32a
MS
1310 str = xstrprintf (_("Type commands for breakpoint(s) "
1311 "%s, one per line."),
86b17b60
PA
1312 info->arg);
1313
1314 old_chain = make_cleanup (xfree, str);
1315
1316 l = read_command_lines (str,
1317 info->from_tty, 1,
d77f58be 1318 (is_tracepoint (b)
86b17b60
PA
1319 ? check_tracepoint_command : 0),
1320 b);
1321
1322 do_cleanups (old_chain);
1323 }
a7bdde9e 1324
95a42b64
TT
1325 info->cmd = alloc_counted_command_line (l);
1326 }
1327
1328 /* If a breakpoint was on the list more than once, we don't need to
1329 do anything. */
1330 if (b->commands != info->cmd)
1331 {
1332 validate_commands_for_breakpoint (b, info->cmd->commands);
1333 incref_counted_command_line (info->cmd);
1334 decref_counted_command_line (&b->commands);
1335 b->commands = info->cmd;
8d3788bd 1336 observer_notify_breakpoint_modified (b);
c5aa993b 1337 }
95a42b64
TT
1338}
1339
1340static void
4a64f543
MS
1341commands_command_1 (char *arg, int from_tty,
1342 struct command_line *control)
95a42b64
TT
1343{
1344 struct cleanup *cleanups;
1345 struct commands_info info;
1346
1347 info.from_tty = from_tty;
1348 info.control = control;
1349 info.cmd = NULL;
1350 /* If we read command lines from the user, then `info' will hold an
1351 extra reference to the commands that we must clean up. */
1352 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1353
1354 if (arg == NULL || !*arg)
1355 {
86b17b60 1356 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1357 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1358 breakpoint_count);
95a42b64
TT
1359 else if (breakpoint_count > 0)
1360 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1361 else
1362 {
1363 /* So that we don't try to free the incoming non-NULL
1364 argument in the cleanup below. Mapping breakpoint
1365 numbers will fail in this case. */
1366 arg = NULL;
1367 }
95a42b64 1368 }
9766ced4
SS
1369 else
1370 /* The command loop has some static state, so we need to preserve
1371 our argument. */
1372 arg = xstrdup (arg);
86b17b60
PA
1373
1374 if (arg != NULL)
1375 make_cleanup (xfree, arg);
1376
1377 info.arg = arg;
95a42b64
TT
1378
1379 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1380
1381 if (info.cmd == NULL)
1382 error (_("No breakpoints specified."));
1383
1384 do_cleanups (cleanups);
1385}
1386
1387static void
1388commands_command (char *arg, int from_tty)
1389{
1390 commands_command_1 (arg, from_tty, NULL);
c906108c 1391}
40c03ae8
EZ
1392
1393/* Like commands_command, but instead of reading the commands from
1394 input stream, takes them from an already parsed command structure.
1395
1396 This is used by cli-script.c to DTRT with breakpoint commands
1397 that are part of if and while bodies. */
1398enum command_control_type
1399commands_from_control_command (char *arg, struct command_line *cmd)
1400{
95a42b64
TT
1401 commands_command_1 (arg, 0, cmd);
1402 return simple_control;
40c03ae8 1403}
876fa593
JK
1404
1405/* Return non-zero if BL->TARGET_INFO contains valid information. */
1406
1407static int
1408bp_location_has_shadow (struct bp_location *bl)
1409{
1410 if (bl->loc_type != bp_loc_software_breakpoint)
1411 return 0;
1412 if (!bl->inserted)
1413 return 0;
1414 if (bl->target_info.shadow_len == 0)
e5dd4106 1415 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1416 return 0;
1417 return 1;
1418}
1419
8defab1a 1420/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1421 by replacing any memory breakpoints with their shadowed contents.
1422
35c63cd8
JB
1423 If READBUF is not NULL, this buffer must not overlap with any of
1424 the breakpoint location's shadow_contents buffers. Otherwise,
1425 a failed assertion internal error will be raised.
1426
876fa593 1427 The range of shadowed area by each bp_location is:
35df4500
TJB
1428 bl->address - bp_location_placed_address_before_address_max
1429 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1430 The range we were requested to resolve shadows for is:
1431 memaddr ... memaddr + len
1432 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1433 memaddr + len <= (bl->address
1434 - bp_location_placed_address_before_address_max)
876fa593 1435 and:
35df4500 1436 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1437
8defab1a 1438void
f0ba3972
PA
1439breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1440 const gdb_byte *writebuf_org,
1441 ULONGEST memaddr, LONGEST len)
c906108c 1442{
4a64f543
MS
1443 /* Left boundary, right boundary and median element of our binary
1444 search. */
876fa593
JK
1445 unsigned bc_l, bc_r, bc;
1446
4a64f543
MS
1447 /* Find BC_L which is a leftmost element which may affect BUF
1448 content. It is safe to report lower value but a failure to
1449 report higher one. */
876fa593
JK
1450
1451 bc_l = 0;
1452 bc_r = bp_location_count;
1453 while (bc_l + 1 < bc_r)
1454 {
35df4500 1455 struct bp_location *bl;
876fa593
JK
1456
1457 bc = (bc_l + bc_r) / 2;
35df4500 1458 bl = bp_location[bc];
876fa593 1459
4a64f543
MS
1460 /* Check first BL->ADDRESS will not overflow due to the added
1461 constant. Then advance the left boundary only if we are sure
1462 the BC element can in no way affect the BUF content (MEMADDR
1463 to MEMADDR + LEN range).
876fa593 1464
4a64f543
MS
1465 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1466 offset so that we cannot miss a breakpoint with its shadow
1467 range tail still reaching MEMADDR. */
c5aa993b 1468
35df4500
TJB
1469 if ((bl->address + bp_location_shadow_len_after_address_max
1470 >= bl->address)
1471 && (bl->address + bp_location_shadow_len_after_address_max
1472 <= memaddr))
876fa593
JK
1473 bc_l = bc;
1474 else
1475 bc_r = bc;
1476 }
1477
128070bb
PA
1478 /* Due to the binary search above, we need to make sure we pick the
1479 first location that's at BC_L's address. E.g., if there are
1480 multiple locations at the same address, BC_L may end up pointing
1481 at a duplicate location, and miss the "master"/"inserted"
1482 location. Say, given locations L1, L2 and L3 at addresses A and
1483 B:
1484
1485 L1@A, L2@A, L3@B, ...
1486
1487 BC_L could end up pointing at location L2, while the "master"
1488 location could be L1. Since the `loc->inserted' flag is only set
1489 on "master" locations, we'd forget to restore the shadow of L1
1490 and L2. */
1491 while (bc_l > 0
1492 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1493 bc_l--;
1494
876fa593
JK
1495 /* Now do full processing of the found relevant range of elements. */
1496
1497 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1498 {
35df4500 1499 struct bp_location *bl = bp_location[bc];
876fa593
JK
1500 CORE_ADDR bp_addr = 0;
1501 int bp_size = 0;
1502 int bptoffset = 0;
1503
35df4500
TJB
1504 /* bp_location array has BL->OWNER always non-NULL. */
1505 if (bl->owner->type == bp_none)
8a3fe4f8 1506 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1507 bl->owner->number);
ffce0d52 1508
e5dd4106 1509 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1510 content. */
1511
35df4500
TJB
1512 if (bl->address >= bp_location_placed_address_before_address_max
1513 && memaddr + len <= (bl->address
1514 - bp_location_placed_address_before_address_max))
876fa593
JK
1515 break;
1516
35df4500 1517 if (!bp_location_has_shadow (bl))
c5aa993b 1518 continue;
35df4500 1519 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1520 current_program_space->aspace, 0))
1521 continue;
1522
c5aa993b
JM
1523 /* Addresses and length of the part of the breakpoint that
1524 we need to copy. */
35df4500
TJB
1525 bp_addr = bl->target_info.placed_address;
1526 bp_size = bl->target_info.shadow_len;
8defab1a 1527
c5aa993b
JM
1528 if (bp_addr + bp_size <= memaddr)
1529 /* The breakpoint is entirely before the chunk of memory we
1530 are reading. */
1531 continue;
8defab1a 1532
c5aa993b
JM
1533 if (bp_addr >= memaddr + len)
1534 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1535 reading. */
c5aa993b 1536 continue;
c5aa993b 1537
8defab1a
DJ
1538 /* Offset within shadow_contents. */
1539 if (bp_addr < memaddr)
1540 {
1541 /* Only copy the second part of the breakpoint. */
1542 bp_size -= memaddr - bp_addr;
1543 bptoffset = memaddr - bp_addr;
1544 bp_addr = memaddr;
1545 }
c5aa993b 1546
8defab1a
DJ
1547 if (bp_addr + bp_size > memaddr + len)
1548 {
1549 /* Only copy the first part of the breakpoint. */
1550 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1551 }
c5aa993b 1552
f0ba3972
PA
1553 if (readbuf != NULL)
1554 {
35c63cd8
JB
1555 /* Verify that the readbuf buffer does not overlap with
1556 the shadow_contents buffer. */
1557 gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1558 || readbuf >= (bl->target_info.shadow_contents
1559 + bl->target_info.shadow_len));
1560
f0ba3972
PA
1561 /* Update the read buffer with this inserted breakpoint's
1562 shadow. */
1563 memcpy (readbuf + bp_addr - memaddr,
1564 bl->target_info.shadow_contents + bptoffset, bp_size);
1565 }
1566 else
1567 {
1568 struct gdbarch *gdbarch = bl->gdbarch;
1569 const unsigned char *bp;
1570 CORE_ADDR placed_address = bl->target_info.placed_address;
20ced3e4 1571 int placed_size = bl->target_info.placed_size;
f0ba3972
PA
1572
1573 /* Update the shadow with what we want to write to memory. */
1574 memcpy (bl->target_info.shadow_contents + bptoffset,
1575 writebuf_org + bp_addr - memaddr, bp_size);
1576
1577 /* Determine appropriate breakpoint contents and size for this
1578 address. */
1579 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1580
1581 /* Update the final write buffer with this inserted
1582 breakpoint's INSN. */
1583 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1584 }
c5aa993b 1585 }
c906108c 1586}
c906108c 1587\f
c5aa993b 1588
b775012e
LM
1589/* Return true if BPT is either a software breakpoint or a hardware
1590 breakpoint. */
1591
1592int
1593is_breakpoint (const struct breakpoint *bpt)
1594{
1595 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1596 || bpt->type == bp_hardware_breakpoint
1597 || bpt->type == bp_dprintf);
b775012e
LM
1598}
1599
60e1c644
PA
1600/* Return true if BPT is of any hardware watchpoint kind. */
1601
a5606eee 1602static int
d77f58be 1603is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1604{
1605 return (bpt->type == bp_hardware_watchpoint
1606 || bpt->type == bp_read_watchpoint
1607 || bpt->type == bp_access_watchpoint);
1608}
7270d8f2 1609
60e1c644
PA
1610/* Return true if BPT is of any watchpoint kind, hardware or
1611 software. */
1612
3a5c3e22 1613int
d77f58be 1614is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1615{
1616 return (is_hardware_watchpoint (bpt)
1617 || bpt->type == bp_watchpoint);
1618}
1619
3a5c3e22
PA
1620/* Returns true if the current thread and its running state are safe
1621 to evaluate or update watchpoint B. Watchpoints on local
1622 expressions need to be evaluated in the context of the thread that
1623 was current when the watchpoint was created, and, that thread needs
1624 to be stopped to be able to select the correct frame context.
1625 Watchpoints on global expressions can be evaluated on any thread,
1626 and in any state. It is presently left to the target allowing
1627 memory accesses when threads are running. */
f6bc2008
PA
1628
1629static int
3a5c3e22 1630watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1631{
d0d8b0c6
JK
1632 return (b->base.pspace == current_program_space
1633 && (ptid_equal (b->watchpoint_thread, null_ptid)
1634 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1635 && !is_executing (inferior_ptid))));
f6bc2008
PA
1636}
1637
d0fb5eae
JK
1638/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1639 associated bp_watchpoint_scope breakpoint. */
1640
1641static void
3a5c3e22 1642watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1643{
3a5c3e22 1644 struct breakpoint *b = &w->base;
d0fb5eae
JK
1645
1646 if (b->related_breakpoint != b)
1647 {
1648 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1649 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1650 b->related_breakpoint->disposition = disp_del_at_next_stop;
1651 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1652 b->related_breakpoint = b;
1653 }
1654 b->disposition = disp_del_at_next_stop;
1655}
1656
567e1b4e
JB
1657/* Assuming that B is a watchpoint:
1658 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1659 - Evaluate expression and store the result in B->val
567e1b4e
JB
1660 - Evaluate the condition if there is one, and store the result
1661 in b->loc->cond.
a5606eee
VP
1662 - Update the list of values that must be watched in B->loc.
1663
4a64f543
MS
1664 If the watchpoint disposition is disp_del_at_next_stop, then do
1665 nothing. If this is local watchpoint that is out of scope, delete
1666 it.
1667
1668 Even with `set breakpoint always-inserted on' the watchpoints are
1669 removed + inserted on each stop here. Normal breakpoints must
1670 never be removed because they might be missed by a running thread
1671 when debugging in non-stop mode. On the other hand, hardware
1672 watchpoints (is_hardware_watchpoint; processed here) are specific
1673 to each LWP since they are stored in each LWP's hardware debug
1674 registers. Therefore, such LWP must be stopped first in order to
1675 be able to modify its hardware watchpoints.
1676
1677 Hardware watchpoints must be reset exactly once after being
1678 presented to the user. It cannot be done sooner, because it would
1679 reset the data used to present the watchpoint hit to the user. And
1680 it must not be done later because it could display the same single
1681 watchpoint hit during multiple GDB stops. Note that the latter is
1682 relevant only to the hardware watchpoint types bp_read_watchpoint
1683 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1684 not user-visible - its hit is suppressed if the memory content has
1685 not changed.
1686
1687 The following constraints influence the location where we can reset
1688 hardware watchpoints:
1689
1690 * target_stopped_by_watchpoint and target_stopped_data_address are
1691 called several times when GDB stops.
1692
1693 [linux]
1694 * Multiple hardware watchpoints can be hit at the same time,
1695 causing GDB to stop. GDB only presents one hardware watchpoint
1696 hit at a time as the reason for stopping, and all the other hits
1697 are presented later, one after the other, each time the user
1698 requests the execution to be resumed. Execution is not resumed
1699 for the threads still having pending hit event stored in
1700 LWP_INFO->STATUS. While the watchpoint is already removed from
1701 the inferior on the first stop the thread hit event is kept being
1702 reported from its cached value by linux_nat_stopped_data_address
1703 until the real thread resume happens after the watchpoint gets
1704 presented and thus its LWP_INFO->STATUS gets reset.
1705
1706 Therefore the hardware watchpoint hit can get safely reset on the
1707 watchpoint removal from inferior. */
a79d3c27 1708
b40ce68a 1709static void
3a5c3e22 1710update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1711{
a5606eee 1712 int within_current_scope;
a5606eee 1713 struct frame_id saved_frame_id;
66076460 1714 int frame_saved;
a5606eee 1715
f6bc2008
PA
1716 /* If this is a local watchpoint, we only want to check if the
1717 watchpoint frame is in scope if the current thread is the thread
1718 that was used to create the watchpoint. */
1719 if (!watchpoint_in_thread_scope (b))
1720 return;
1721
3a5c3e22 1722 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1723 return;
1724
66076460 1725 frame_saved = 0;
a5606eee
VP
1726
1727 /* Determine if the watchpoint is within scope. */
1728 if (b->exp_valid_block == NULL)
1729 within_current_scope = 1;
1730 else
1731 {
b5db5dfc
UW
1732 struct frame_info *fi = get_current_frame ();
1733 struct gdbarch *frame_arch = get_frame_arch (fi);
1734 CORE_ADDR frame_pc = get_frame_pc (fi);
1735
1736 /* If we're in a function epilogue, unwinding may not work
1737 properly, so do not attempt to recreate locations at this
1738 point. See similar comments in watchpoint_check. */
1739 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1740 return;
66076460
DJ
1741
1742 /* Save the current frame's ID so we can restore it after
1743 evaluating the watchpoint expression on its own frame. */
1744 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1745 took a frame parameter, so that we didn't have to change the
1746 selected frame. */
1747 frame_saved = 1;
1748 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1749
a5606eee
VP
1750 fi = frame_find_by_id (b->watchpoint_frame);
1751 within_current_scope = (fi != NULL);
1752 if (within_current_scope)
1753 select_frame (fi);
1754 }
1755
b5db5dfc
UW
1756 /* We don't free locations. They are stored in the bp_location array
1757 and update_global_location_list will eventually delete them and
1758 remove breakpoints if needed. */
3a5c3e22 1759 b->base.loc = NULL;
b5db5dfc 1760
a5606eee
VP
1761 if (within_current_scope && reparse)
1762 {
bbc13ae3 1763 const char *s;
d63d0675 1764
a5606eee
VP
1765 if (b->exp)
1766 {
1767 xfree (b->exp);
1768 b->exp = NULL;
1769 }
d63d0675 1770 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1771 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1772 /* If the meaning of expression itself changed, the old value is
1773 no longer relevant. We don't want to report a watchpoint hit
1774 to the user when the old value and the new value may actually
1775 be completely different objects. */
1776 value_free (b->val);
fa4727a6
DJ
1777 b->val = NULL;
1778 b->val_valid = 0;
60e1c644
PA
1779
1780 /* Note that unlike with breakpoints, the watchpoint's condition
1781 expression is stored in the breakpoint object, not in the
1782 locations (re)created below. */
3a5c3e22 1783 if (b->base.cond_string != NULL)
60e1c644
PA
1784 {
1785 if (b->cond_exp != NULL)
1786 {
1787 xfree (b->cond_exp);
1788 b->cond_exp = NULL;
1789 }
1790
3a5c3e22 1791 s = b->base.cond_string;
1bb9788d 1792 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1793 }
a5606eee 1794 }
a5606eee
VP
1795
1796 /* If we failed to parse the expression, for example because
1797 it refers to a global variable in a not-yet-loaded shared library,
1798 don't try to insert watchpoint. We don't automatically delete
1799 such watchpoint, though, since failure to parse expression
1800 is different from out-of-scope watchpoint. */
e8369a73 1801 if (!target_has_execution)
2d134ed3
PA
1802 {
1803 /* Without execution, memory can't change. No use to try and
1804 set watchpoint locations. The watchpoint will be reset when
1805 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1806 if (!can_use_hw_watchpoints)
1807 {
1808 if (b->base.ops->works_in_software_mode (&b->base))
1809 b->base.type = bp_watchpoint;
1810 else
638aa5a1
AB
1811 error (_("Can't set read/access watchpoint when "
1812 "hardware watchpoints are disabled."));
e8369a73 1813 }
2d134ed3
PA
1814 }
1815 else if (within_current_scope && b->exp)
a5606eee 1816 {
0cf6dd15 1817 int pc = 0;
fa4727a6 1818 struct value *val_chain, *v, *result, *next;
2d134ed3 1819 struct program_space *frame_pspace;
a5606eee 1820
3a1115a0 1821 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1822
a5606eee
VP
1823 /* Avoid setting b->val if it's already set. The meaning of
1824 b->val is 'the last value' user saw, and we should update
1825 it only if we reported that last value to user. As it
9c06b0b4
TJB
1826 happens, the code that reports it updates b->val directly.
1827 We don't keep track of the memory value for masked
1828 watchpoints. */
3a5c3e22 1829 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1830 {
1831 b->val = v;
1832 b->val_valid = 1;
1833 }
a5606eee 1834
2d134ed3
PA
1835 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1836
a5606eee 1837 /* Look at each value on the value chain. */
9fa40276 1838 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1839 {
1840 /* If it's a memory location, and GDB actually needed
1841 its contents to evaluate the expression, then we
fa4727a6
DJ
1842 must watch it. If the first value returned is
1843 still lazy, that means an error occurred reading it;
1844 watch it anyway in case it becomes readable. */
a5606eee 1845 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1846 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1847 {
1848 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1849
a5606eee
VP
1850 /* We only watch structs and arrays if user asked
1851 for it explicitly, never if they just happen to
1852 appear in the middle of some value chain. */
fa4727a6 1853 if (v == result
a5606eee
VP
1854 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1855 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1856 {
1857 CORE_ADDR addr;
744a8059 1858 int type;
a5606eee
VP
1859 struct bp_location *loc, **tmp;
1860
42ae5230 1861 addr = value_address (v);
a5606eee 1862 type = hw_write;
3a5c3e22 1863 if (b->base.type == bp_read_watchpoint)
a5606eee 1864 type = hw_read;
3a5c3e22 1865 else if (b->base.type == bp_access_watchpoint)
a5606eee 1866 type = hw_access;
3a5c3e22
PA
1867
1868 loc = allocate_bp_location (&b->base);
1869 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1870 ;
1871 *tmp = loc;
a6d9a66e 1872 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1873
1874 loc->pspace = frame_pspace;
a5606eee 1875 loc->address = addr;
744a8059 1876 loc->length = TYPE_LENGTH (value_type (v));
a5606eee
VP
1877 loc->watchpoint_type = type;
1878 }
1879 }
9fa40276
TJB
1880 }
1881
1882 /* Change the type of breakpoint between hardware assisted or
1883 an ordinary watchpoint depending on the hardware support
1884 and free hardware slots. REPARSE is set when the inferior
1885 is started. */
a9634178 1886 if (reparse)
9fa40276 1887 {
e09342b5 1888 int reg_cnt;
9fa40276
TJB
1889 enum bp_loc_type loc_type;
1890 struct bp_location *bl;
a5606eee 1891
a9634178 1892 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1893
1894 if (reg_cnt)
9fa40276
TJB
1895 {
1896 int i, target_resources_ok, other_type_used;
a1398e0c 1897 enum bptype type;
9fa40276 1898
a9634178
TJB
1899 /* Use an exact watchpoint when there's only one memory region to be
1900 watched, and only one debug register is needed to watch it. */
1901 b->exact = target_exact_watchpoints && reg_cnt == 1;
1902
9fa40276 1903 /* We need to determine how many resources are already
e09342b5
TJB
1904 used for all other hardware watchpoints plus this one
1905 to see if we still have enough resources to also fit
a1398e0c
PA
1906 this watchpoint in as well. */
1907
1908 /* If this is a software watchpoint, we try to turn it
1909 to a hardware one -- count resources as if B was of
1910 hardware watchpoint type. */
1911 type = b->base.type;
1912 if (type == bp_watchpoint)
1913 type = bp_hardware_watchpoint;
1914
1915 /* This watchpoint may or may not have been placed on
1916 the list yet at this point (it won't be in the list
1917 if we're trying to create it for the first time,
1918 through watch_command), so always account for it
1919 manually. */
1920
1921 /* Count resources used by all watchpoints except B. */
1922 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1923
1924 /* Add in the resources needed for B. */
1925 i += hw_watchpoint_use_count (&b->base);
1926
1927 target_resources_ok
1928 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1929 if (target_resources_ok <= 0)
a9634178 1930 {
3a5c3e22 1931 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1932
1933 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1934 error (_("Target does not support this type of "
1935 "hardware watchpoint."));
9c06b0b4
TJB
1936 else if (target_resources_ok < 0 && !sw_mode)
1937 error (_("There are not enough available hardware "
1938 "resources for this watchpoint."));
a1398e0c
PA
1939
1940 /* Downgrade to software watchpoint. */
1941 b->base.type = bp_watchpoint;
1942 }
1943 else
1944 {
1945 /* If this was a software watchpoint, we've just
1946 found we have enough resources to turn it to a
1947 hardware watchpoint. Otherwise, this is a
1948 nop. */
1949 b->base.type = type;
a9634178 1950 }
9fa40276 1951 }
3a5c3e22 1952 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
1953 {
1954 if (!can_use_hw_watchpoints)
1955 error (_("Can't set read/access watchpoint when "
1956 "hardware watchpoints are disabled."));
1957 else
1958 error (_("Expression cannot be implemented with "
1959 "read/access watchpoint."));
1960 }
9fa40276 1961 else
3a5c3e22 1962 b->base.type = bp_watchpoint;
9fa40276 1963
3a5c3e22 1964 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1965 : bp_loc_hardware_watchpoint);
3a5c3e22 1966 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1967 bl->loc_type = loc_type;
1968 }
1969
1970 for (v = val_chain; v; v = next)
1971 {
a5606eee
VP
1972 next = value_next (v);
1973 if (v != b->val)
1974 value_free (v);
1975 }
1976
c7437ca6
PA
1977 /* If a software watchpoint is not watching any memory, then the
1978 above left it without any location set up. But,
1979 bpstat_stop_status requires a location to be able to report
1980 stops, so make sure there's at least a dummy one. */
3a5c3e22 1981 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1982 {
3a5c3e22
PA
1983 struct breakpoint *base = &b->base;
1984 base->loc = allocate_bp_location (base);
1985 base->loc->pspace = frame_pspace;
1986 base->loc->address = -1;
1987 base->loc->length = -1;
1988 base->loc->watchpoint_type = -1;
c7437ca6 1989 }
a5606eee
VP
1990 }
1991 else if (!within_current_scope)
7270d8f2 1992 {
ac74f770
MS
1993 printf_filtered (_("\
1994Watchpoint %d deleted because the program has left the block\n\
1995in which its expression is valid.\n"),
3a5c3e22 1996 b->base.number);
d0fb5eae 1997 watchpoint_del_at_next_stop (b);
7270d8f2 1998 }
a5606eee
VP
1999
2000 /* Restore the selected frame. */
66076460
DJ
2001 if (frame_saved)
2002 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2003}
2004
a5606eee 2005
74960c60 2006/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2007 inserted in the inferior. We don't differentiate the type of BL's owner
2008 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2009 breakpoint_ops is not defined, because in insert_bp_location,
2010 tracepoint's insert_location will not be called. */
74960c60 2011static int
35df4500 2012should_be_inserted (struct bp_location *bl)
74960c60 2013{
35df4500 2014 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2015 return 0;
2016
35df4500 2017 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2018 return 0;
2019
35df4500 2020 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2021 return 0;
2022
f8eba3c6
TT
2023 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2024 return 0;
2025
56710373
PA
2026 /* This is set for example, when we're attached to the parent of a
2027 vfork, and have detached from the child. The child is running
2028 free, and we expect it to do an exec or exit, at which point the
2029 OS makes the parent schedulable again (and the target reports
2030 that the vfork is done). Until the child is done with the shared
2031 memory region, do not insert breakpoints in the parent, otherwise
2032 the child could still trip on the parent's breakpoints. Since
2033 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2034 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2035 return 0;
2036
74960c60
VP
2037 return 1;
2038}
2039
934709f0
PW
2040/* Same as should_be_inserted but does the check assuming
2041 that the location is not duplicated. */
2042
2043static int
2044unduplicated_should_be_inserted (struct bp_location *bl)
2045{
2046 int result;
2047 const int save_duplicate = bl->duplicate;
2048
2049 bl->duplicate = 0;
2050 result = should_be_inserted (bl);
2051 bl->duplicate = save_duplicate;
2052 return result;
2053}
2054
b775012e
LM
2055/* Parses a conditional described by an expression COND into an
2056 agent expression bytecode suitable for evaluation
2057 by the bytecode interpreter. Return NULL if there was
2058 any error during parsing. */
2059
2060static struct agent_expr *
2061parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2062{
2063 struct agent_expr *aexpr = NULL;
b775012e
LM
2064 volatile struct gdb_exception ex;
2065
2066 if (!cond)
2067 return NULL;
2068
2069 /* We don't want to stop processing, so catch any errors
2070 that may show up. */
2071 TRY_CATCH (ex, RETURN_MASK_ERROR)
2072 {
2073 aexpr = gen_eval_for_expr (scope, cond);
2074 }
2075
2076 if (ex.reason < 0)
2077 {
2078 /* If we got here, it means the condition could not be parsed to a valid
2079 bytecode expression and thus can't be evaluated on the target's side.
2080 It's no use iterating through the conditions. */
2081 return NULL;
2082 }
2083
2084 /* We have a valid agent expression. */
2085 return aexpr;
2086}
2087
2088/* Based on location BL, create a list of breakpoint conditions to be
2089 passed on to the target. If we have duplicated locations with different
2090 conditions, we will add such conditions to the list. The idea is that the
2091 target will evaluate the list of conditions and will only notify GDB when
2092 one of them is true. */
2093
2094static void
2095build_target_condition_list (struct bp_location *bl)
2096{
2097 struct bp_location **locp = NULL, **loc2p;
2098 int null_condition_or_parse_error = 0;
2099 int modified = bl->needs_update;
2100 struct bp_location *loc;
2101
8b4f3082
PA
2102 /* Release conditions left over from a previous insert. */
2103 VEC_free (agent_expr_p, bl->target_info.conditions);
2104
b775012e
LM
2105 /* This is only meaningful if the target is
2106 evaluating conditions and if the user has
2107 opted for condition evaluation on the target's
2108 side. */
2109 if (gdb_evaluates_breakpoint_condition_p ()
2110 || !target_supports_evaluation_of_breakpoint_conditions ())
2111 return;
2112
2113 /* Do a first pass to check for locations with no assigned
2114 conditions or conditions that fail to parse to a valid agent expression
2115 bytecode. If any of these happen, then it's no use to send conditions
2116 to the target since this location will always trigger and generate a
2117 response back to GDB. */
2118 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2119 {
2120 loc = (*loc2p);
2121 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2122 {
2123 if (modified)
2124 {
2125 struct agent_expr *aexpr;
2126
2127 /* Re-parse the conditions since something changed. In that
2128 case we already freed the condition bytecodes (see
2129 force_breakpoint_reinsertion). We just
2130 need to parse the condition to bytecodes again. */
2131 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2132 loc->cond_bytecode = aexpr;
2133
2134 /* Check if we managed to parse the conditional expression
2135 correctly. If not, we will not send this condition
2136 to the target. */
2137 if (aexpr)
2138 continue;
2139 }
2140
2141 /* If we have a NULL bytecode expression, it means something
2142 went wrong or we have a null condition expression. */
2143 if (!loc->cond_bytecode)
2144 {
2145 null_condition_or_parse_error = 1;
2146 break;
2147 }
2148 }
2149 }
2150
2151 /* If any of these happened, it means we will have to evaluate the conditions
2152 for the location's address on gdb's side. It is no use keeping bytecodes
2153 for all the other duplicate locations, thus we free all of them here.
2154
2155 This is so we have a finer control over which locations' conditions are
2156 being evaluated by GDB or the remote stub. */
2157 if (null_condition_or_parse_error)
2158 {
2159 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2160 {
2161 loc = (*loc2p);
2162 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2163 {
2164 /* Only go as far as the first NULL bytecode is
2165 located. */
2166 if (!loc->cond_bytecode)
2167 return;
2168
2169 free_agent_expr (loc->cond_bytecode);
2170 loc->cond_bytecode = NULL;
2171 }
2172 }
2173 }
2174
2175 /* No NULL conditions or failed bytecode generation. Build a condition list
2176 for this location's address. */
2177 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2178 {
2179 loc = (*loc2p);
2180 if (loc->cond
2181 && is_breakpoint (loc->owner)
2182 && loc->pspace->num == bl->pspace->num
2183 && loc->owner->enable_state == bp_enabled
2184 && loc->enabled)
2185 /* Add the condition to the vector. This will be used later to send the
2186 conditions to the target. */
2187 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2188 loc->cond_bytecode);
2189 }
2190
2191 return;
2192}
2193
d3ce09f5
SS
2194/* Parses a command described by string CMD into an agent expression
2195 bytecode suitable for evaluation by the bytecode interpreter.
2196 Return NULL if there was any error during parsing. */
2197
2198static struct agent_expr *
2199parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2200{
2201 struct cleanup *old_cleanups = 0;
2202 struct expression *expr, **argvec;
2203 struct agent_expr *aexpr = NULL;
d3ce09f5 2204 volatile struct gdb_exception ex;
bbc13ae3
KS
2205 const char *cmdrest;
2206 const char *format_start, *format_end;
d3ce09f5
SS
2207 struct format_piece *fpieces;
2208 int nargs;
2209 struct gdbarch *gdbarch = get_current_arch ();
2210
2211 if (!cmd)
2212 return NULL;
2213
2214 cmdrest = cmd;
2215
2216 if (*cmdrest == ',')
2217 ++cmdrest;
bbc13ae3 2218 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2219
2220 if (*cmdrest++ != '"')
2221 error (_("No format string following the location"));
2222
2223 format_start = cmdrest;
2224
2225 fpieces = parse_format_string (&cmdrest);
2226
2227 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2228
2229 format_end = cmdrest;
2230
2231 if (*cmdrest++ != '"')
2232 error (_("Bad format string, non-terminated '\"'."));
2233
bbc13ae3 2234 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2235
2236 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2237 error (_("Invalid argument syntax"));
2238
2239 if (*cmdrest == ',')
2240 cmdrest++;
bbc13ae3 2241 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2242
2243 /* For each argument, make an expression. */
2244
2245 argvec = (struct expression **) alloca (strlen (cmd)
2246 * sizeof (struct expression *));
2247
2248 nargs = 0;
2249 while (*cmdrest != '\0')
2250 {
bbc13ae3 2251 const char *cmd1;
d3ce09f5
SS
2252
2253 cmd1 = cmdrest;
2254 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2255 argvec[nargs++] = expr;
2256 cmdrest = cmd1;
2257 if (*cmdrest == ',')
2258 ++cmdrest;
2259 }
2260
2261 /* We don't want to stop processing, so catch any errors
2262 that may show up. */
2263 TRY_CATCH (ex, RETURN_MASK_ERROR)
2264 {
2265 aexpr = gen_printf (scope, gdbarch, 0, 0,
2266 format_start, format_end - format_start,
2267 fpieces, nargs, argvec);
2268 }
2269
752eb8b4
TT
2270 do_cleanups (old_cleanups);
2271
d3ce09f5
SS
2272 if (ex.reason < 0)
2273 {
2274 /* If we got here, it means the command could not be parsed to a valid
2275 bytecode expression and thus can't be evaluated on the target's side.
2276 It's no use iterating through the other commands. */
2277 return NULL;
2278 }
2279
d3ce09f5
SS
2280 /* We have a valid agent expression, return it. */
2281 return aexpr;
2282}
2283
2284/* Based on location BL, create a list of breakpoint commands to be
2285 passed on to the target. If we have duplicated locations with
2286 different commands, we will add any such to the list. */
2287
2288static void
2289build_target_command_list (struct bp_location *bl)
2290{
2291 struct bp_location **locp = NULL, **loc2p;
2292 int null_command_or_parse_error = 0;
2293 int modified = bl->needs_update;
2294 struct bp_location *loc;
2295
8b4f3082
PA
2296 /* Release commands left over from a previous insert. */
2297 VEC_free (agent_expr_p, bl->target_info.tcommands);
2298
d3ce09f5
SS
2299 /* For now, limit to agent-style dprintf breakpoints. */
2300 if (bl->owner->type != bp_dprintf
2301 || strcmp (dprintf_style, dprintf_style_agent) != 0)
2302 return;
2303
2304 if (!target_can_run_breakpoint_commands ())
2305 return;
2306
2307 /* Do a first pass to check for locations with no assigned
2308 conditions or conditions that fail to parse to a valid agent expression
2309 bytecode. If any of these happen, then it's no use to send conditions
2310 to the target since this location will always trigger and generate a
2311 response back to GDB. */
2312 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2313 {
2314 loc = (*loc2p);
2315 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2316 {
2317 if (modified)
2318 {
2319 struct agent_expr *aexpr;
2320
2321 /* Re-parse the commands since something changed. In that
2322 case we already freed the command bytecodes (see
2323 force_breakpoint_reinsertion). We just
2324 need to parse the command to bytecodes again. */
2325 aexpr = parse_cmd_to_aexpr (bl->address,
2326 loc->owner->extra_string);
2327 loc->cmd_bytecode = aexpr;
2328
2329 if (!aexpr)
2330 continue;
2331 }
2332
2333 /* If we have a NULL bytecode expression, it means something
2334 went wrong or we have a null command expression. */
2335 if (!loc->cmd_bytecode)
2336 {
2337 null_command_or_parse_error = 1;
2338 break;
2339 }
2340 }
2341 }
2342
2343 /* If anything failed, then we're not doing target-side commands,
2344 and so clean up. */
2345 if (null_command_or_parse_error)
2346 {
2347 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2348 {
2349 loc = (*loc2p);
2350 if (is_breakpoint (loc->owner)
2351 && loc->pspace->num == bl->pspace->num)
2352 {
2353 /* Only go as far as the first NULL bytecode is
2354 located. */
40fb6c5e 2355 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2356 return;
2357
40fb6c5e
HZ
2358 free_agent_expr (loc->cmd_bytecode);
2359 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2360 }
2361 }
2362 }
2363
2364 /* No NULL commands or failed bytecode generation. Build a command list
2365 for this location's address. */
2366 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2367 {
2368 loc = (*loc2p);
2369 if (loc->owner->extra_string
2370 && is_breakpoint (loc->owner)
2371 && loc->pspace->num == bl->pspace->num
2372 && loc->owner->enable_state == bp_enabled
2373 && loc->enabled)
2374 /* Add the command to the vector. This will be used later
2375 to send the commands to the target. */
2376 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2377 loc->cmd_bytecode);
2378 }
2379
2380 bl->target_info.persist = 0;
2381 /* Maybe flag this location as persistent. */
2382 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2383 bl->target_info.persist = 1;
2384}
2385
35df4500
TJB
2386/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2387 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2388 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2389 Returns 0 for success, 1 if the bp_location type is not supported or
2390 -1 for failure.
879bfdc2 2391
4a64f543
MS
2392 NOTE drow/2003-09-09: This routine could be broken down to an
2393 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2394static int
35df4500 2395insert_bp_location (struct bp_location *bl,
26bb91f3 2396 struct ui_file *tmp_error_stream,
3fbb6ffa 2397 int *disabled_breaks,
dd61ec5c
MW
2398 int *hw_breakpoint_error,
2399 int *hw_bp_error_explained_already)
879bfdc2 2400{
0000e5cc
PA
2401 enum errors bp_err = GDB_NO_ERROR;
2402 const char *bp_err_message = NULL;
c90a6fb7 2403 volatile struct gdb_exception e;
879bfdc2 2404
b775012e 2405 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2406 return 0;
2407
35c63cd8
JB
2408 /* Note we don't initialize bl->target_info, as that wipes out
2409 the breakpoint location's shadow_contents if the breakpoint
2410 is still inserted at that location. This in turn breaks
2411 target_read_memory which depends on these buffers when
2412 a memory read is requested at the breakpoint location:
2413 Once the target_info has been wiped, we fail to see that
2414 we have a breakpoint inserted at that address and thus
2415 read the breakpoint instead of returning the data saved in
2416 the breakpoint location's shadow contents. */
35df4500
TJB
2417 bl->target_info.placed_address = bl->address;
2418 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2419 bl->target_info.length = bl->length;
8181d85f 2420
b775012e
LM
2421 /* When working with target-side conditions, we must pass all the conditions
2422 for the same breakpoint address down to the target since GDB will not
2423 insert those locations. With a list of breakpoint conditions, the target
2424 can decide when to stop and notify GDB. */
2425
2426 if (is_breakpoint (bl->owner))
2427 {
2428 build_target_condition_list (bl);
d3ce09f5
SS
2429 build_target_command_list (bl);
2430 /* Reset the modification marker. */
b775012e
LM
2431 bl->needs_update = 0;
2432 }
2433
35df4500
TJB
2434 if (bl->loc_type == bp_loc_software_breakpoint
2435 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2436 {
35df4500 2437 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2438 {
2439 /* If the explicitly specified breakpoint type
2440 is not hardware breakpoint, check the memory map to see
2441 if the breakpoint address is in read only memory or not.
4a64f543 2442
765dc015
VP
2443 Two important cases are:
2444 - location type is not hardware breakpoint, memory
2445 is readonly. We change the type of the location to
2446 hardware breakpoint.
4a64f543
MS
2447 - location type is hardware breakpoint, memory is
2448 read-write. This means we've previously made the
2449 location hardware one, but then the memory map changed,
2450 so we undo.
765dc015 2451
4a64f543
MS
2452 When breakpoints are removed, remove_breakpoints will use
2453 location types we've just set here, the only possible
2454 problem is that memory map has changed during running
2455 program, but it's not going to work anyway with current
2456 gdb. */
765dc015 2457 struct mem_region *mr
35df4500 2458 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2459
2460 if (mr)
2461 {
2462 if (automatic_hardware_breakpoints)
2463 {
765dc015
VP
2464 enum bp_loc_type new_type;
2465
2466 if (mr->attrib.mode != MEM_RW)
2467 new_type = bp_loc_hardware_breakpoint;
2468 else
2469 new_type = bp_loc_software_breakpoint;
2470
35df4500 2471 if (new_type != bl->loc_type)
765dc015
VP
2472 {
2473 static int said = 0;
cc59ec59 2474
35df4500 2475 bl->loc_type = new_type;
765dc015
VP
2476 if (!said)
2477 {
3e43a32a
MS
2478 fprintf_filtered (gdb_stdout,
2479 _("Note: automatically using "
2480 "hardware breakpoints for "
2481 "read-only addresses.\n"));
765dc015
VP
2482 said = 1;
2483 }
2484 }
2485 }
35df4500 2486 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 2487 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
2488 warning (_("cannot set software breakpoint "
2489 "at readonly address %s"),
35df4500 2490 paddress (bl->gdbarch, bl->address));
765dc015
VP
2491 }
2492 }
2493
879bfdc2
DJ
2494 /* First check to see if we have to handle an overlay. */
2495 if (overlay_debugging == ovly_off
35df4500
TJB
2496 || bl->section == NULL
2497 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2498 {
2499 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2500 TRY_CATCH (e, RETURN_MASK_ALL)
2501 {
0000e5cc
PA
2502 int val;
2503
dd61ec5c 2504 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2505 if (val)
2506 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2507 }
2508 if (e.reason < 0)
2509 {
0000e5cc
PA
2510 bp_err = e.error;
2511 bp_err_message = e.message;
dd61ec5c 2512 }
879bfdc2
DJ
2513 }
2514 else
2515 {
4a64f543 2516 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2517 Shall we set a breakpoint at the LMA? */
2518 if (!overlay_events_enabled)
2519 {
2520 /* Yes -- overlay event support is not active,
2521 so we must try to set a breakpoint at the LMA.
2522 This will not work for a hardware breakpoint. */
35df4500 2523 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2524 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2525 bl->owner->number);
879bfdc2
DJ
2526 else
2527 {
35df4500
TJB
2528 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2529 bl->section);
879bfdc2 2530 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2531 bl->overlay_target_info = bl->target_info;
2532 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2533
2534 /* No overlay handling: just set the breakpoint. */
2535 TRY_CATCH (e, RETURN_MASK_ALL)
2536 {
2537 int val;
2538
2539 val = target_insert_breakpoint (bl->gdbarch,
2540 &bl->overlay_target_info);
2541 if (val)
2542 bp_err = GENERIC_ERROR;
2543 }
2544 if (e.reason < 0)
2545 {
2546 bp_err = e.error;
2547 bp_err_message = e.message;
2548 }
2549
2550 if (bp_err != GDB_NO_ERROR)
99361f52 2551 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2552 "Overlay breakpoint %d "
2553 "failed: in ROM?\n",
35df4500 2554 bl->owner->number);
879bfdc2
DJ
2555 }
2556 }
2557 /* Shall we set a breakpoint at the VMA? */
35df4500 2558 if (section_is_mapped (bl->section))
879bfdc2
DJ
2559 {
2560 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2561 TRY_CATCH (e, RETURN_MASK_ALL)
2562 {
0000e5cc
PA
2563 int val;
2564
dd61ec5c 2565 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2566 if (val)
2567 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2568 }
2569 if (e.reason < 0)
2570 {
0000e5cc
PA
2571 bp_err = e.error;
2572 bp_err_message = e.message;
dd61ec5c 2573 }
879bfdc2
DJ
2574 }
2575 else
2576 {
2577 /* No. This breakpoint will not be inserted.
2578 No error, but do not mark the bp as 'inserted'. */
2579 return 0;
2580 }
2581 }
2582
0000e5cc 2583 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2584 {
2585 /* Can't set the breakpoint. */
0000e5cc
PA
2586
2587 /* In some cases, we might not be able to insert a
2588 breakpoint in a shared library that has already been
2589 removed, but we have not yet processed the shlib unload
2590 event. Unfortunately, some targets that implement
2591 breakpoint insertion themselves (necessary if this is a
2592 HW breakpoint, but SW breakpoints likewise) can't tell
2593 why the breakpoint insertion failed (e.g., the remote
2594 target doesn't define error codes), so we must treat
2595 generic errors as memory errors. */
2596 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2597 && solib_name_from_address (bl->pspace, bl->address))
879bfdc2 2598 {
4a64f543 2599 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2600 bl->shlib_disabled = 1;
8d3788bd 2601 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2602 if (!*disabled_breaks)
2603 {
2604 fprintf_unfiltered (tmp_error_stream,
2605 "Cannot insert breakpoint %d.\n",
2606 bl->owner->number);
2607 fprintf_unfiltered (tmp_error_stream,
2608 "Temporarily disabling shared "
2609 "library breakpoints:\n");
2610 }
2611 *disabled_breaks = 1;
879bfdc2 2612 fprintf_unfiltered (tmp_error_stream,
35df4500 2613 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2614 return 0;
879bfdc2
DJ
2615 }
2616 else
879bfdc2 2617 {
35df4500 2618 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2619 {
0000e5cc
PA
2620 *hw_breakpoint_error = 1;
2621 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2622 fprintf_unfiltered (tmp_error_stream,
2623 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2624 bl->owner->number, bp_err_message ? ":" : ".\n");
2625 if (bp_err_message != NULL)
2626 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2627 }
2628 else
2629 {
0000e5cc
PA
2630 if (bp_err_message == NULL)
2631 {
2632 char *message
2633 = memory_error_message (TARGET_XFER_E_IO,
2634 bl->gdbarch, bl->address);
2635 struct cleanup *old_chain = make_cleanup (xfree, message);
2636
2637 fprintf_unfiltered (tmp_error_stream,
2638 "Cannot insert breakpoint %d.\n"
2639 "%s\n",
2640 bl->owner->number, message);
2641 do_cleanups (old_chain);
2642 }
2643 else
2644 {
2645 fprintf_unfiltered (tmp_error_stream,
2646 "Cannot insert breakpoint %d: %s\n",
2647 bl->owner->number,
2648 bp_err_message);
2649 }
879bfdc2 2650 }
0000e5cc 2651 return 1;
879bfdc2
DJ
2652
2653 }
2654 }
2655 else
35df4500 2656 bl->inserted = 1;
879bfdc2 2657
0000e5cc 2658 return 0;
879bfdc2
DJ
2659 }
2660
35df4500 2661 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2662 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2663 watchpoints. It's not clear that it's necessary... */
35df4500 2664 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2665 {
0000e5cc
PA
2666 int val;
2667
77b06cd7
TJB
2668 gdb_assert (bl->owner->ops != NULL
2669 && bl->owner->ops->insert_location != NULL);
2670
2671 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2672
2673 /* If trying to set a read-watchpoint, and it turns out it's not
2674 supported, try emulating one with an access watchpoint. */
35df4500 2675 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2676 {
2677 struct bp_location *loc, **loc_temp;
2678
2679 /* But don't try to insert it, if there's already another
2680 hw_access location that would be considered a duplicate
2681 of this one. */
2682 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2683 if (loc != bl
85d721b8 2684 && loc->watchpoint_type == hw_access
35df4500 2685 && watchpoint_locations_match (bl, loc))
85d721b8 2686 {
35df4500
TJB
2687 bl->duplicate = 1;
2688 bl->inserted = 1;
2689 bl->target_info = loc->target_info;
2690 bl->watchpoint_type = hw_access;
85d721b8
PA
2691 val = 0;
2692 break;
2693 }
2694
2695 if (val == 1)
2696 {
77b06cd7
TJB
2697 bl->watchpoint_type = hw_access;
2698 val = bl->owner->ops->insert_location (bl);
2699
2700 if (val)
2701 /* Back to the original value. */
2702 bl->watchpoint_type = hw_read;
85d721b8
PA
2703 }
2704 }
2705
35df4500 2706 bl->inserted = (val == 0);
879bfdc2
DJ
2707 }
2708
35df4500 2709 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2710 {
0000e5cc
PA
2711 int val;
2712
77b06cd7
TJB
2713 gdb_assert (bl->owner->ops != NULL
2714 && bl->owner->ops->insert_location != NULL);
2715
2716 val = bl->owner->ops->insert_location (bl);
2717 if (val)
2718 {
2719 bl->owner->enable_state = bp_disabled;
2720
2721 if (val == 1)
2722 warning (_("\
2723Error inserting catchpoint %d: Your system does not support this type\n\
2724of catchpoint."), bl->owner->number);
2725 else
2726 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2727 }
2728
2729 bl->inserted = (val == 0);
1640b821
DJ
2730
2731 /* We've already printed an error message if there was a problem
2732 inserting this catchpoint, and we've disabled the catchpoint,
2733 so just return success. */
2734 return 0;
879bfdc2
DJ
2735 }
2736
2737 return 0;
2738}
2739
6c95b8df
PA
2740/* This function is called when program space PSPACE is about to be
2741 deleted. It takes care of updating breakpoints to not reference
2742 PSPACE anymore. */
2743
2744void
2745breakpoint_program_space_exit (struct program_space *pspace)
2746{
2747 struct breakpoint *b, *b_temp;
876fa593 2748 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2749
2750 /* Remove any breakpoint that was set through this program space. */
2751 ALL_BREAKPOINTS_SAFE (b, b_temp)
2752 {
2753 if (b->pspace == pspace)
2754 delete_breakpoint (b);
2755 }
2756
2757 /* Breakpoints set through other program spaces could have locations
2758 bound to PSPACE as well. Remove those. */
876fa593 2759 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2760 {
2761 struct bp_location *tmp;
2762
2763 if (loc->pspace == pspace)
2764 {
2bdf28a0 2765 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2766 if (loc->owner->loc == loc)
2767 loc->owner->loc = loc->next;
2768 else
2769 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2770 if (tmp->next == loc)
2771 {
2772 tmp->next = loc->next;
2773 break;
2774 }
2775 }
2776 }
2777
2778 /* Now update the global location list to permanently delete the
2779 removed locations above. */
2780 update_global_location_list (0);
2781}
2782
74960c60
VP
2783/* Make sure all breakpoints are inserted in inferior.
2784 Throws exception on any error.
2785 A breakpoint that is already inserted won't be inserted
2786 again, so calling this function twice is safe. */
2787void
2788insert_breakpoints (void)
2789{
2790 struct breakpoint *bpt;
2791
2792 ALL_BREAKPOINTS (bpt)
2793 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2794 {
2795 struct watchpoint *w = (struct watchpoint *) bpt;
2796
2797 update_watchpoint (w, 0 /* don't reparse. */);
2798 }
74960c60 2799
b60e7edf 2800 update_global_location_list (1);
74960c60 2801
c35b1492
PA
2802 /* update_global_location_list does not insert breakpoints when
2803 always_inserted_mode is not enabled. Explicitly insert them
2804 now. */
2805 if (!breakpoints_always_inserted_mode ())
74960c60
VP
2806 insert_breakpoint_locations ();
2807}
2808
20388dd6
YQ
2809/* Invoke CALLBACK for each of bp_location. */
2810
2811void
2812iterate_over_bp_locations (walk_bp_location_callback callback)
2813{
2814 struct bp_location *loc, **loc_tmp;
2815
2816 ALL_BP_LOCATIONS (loc, loc_tmp)
2817 {
2818 callback (loc, NULL);
2819 }
2820}
2821
b775012e
LM
2822/* This is used when we need to synch breakpoint conditions between GDB and the
2823 target. It is the case with deleting and disabling of breakpoints when using
2824 always-inserted mode. */
2825
2826static void
2827update_inserted_breakpoint_locations (void)
2828{
2829 struct bp_location *bl, **blp_tmp;
2830 int error_flag = 0;
2831 int val = 0;
2832 int disabled_breaks = 0;
2833 int hw_breakpoint_error = 0;
dd61ec5c 2834 int hw_bp_details_reported = 0;
b775012e
LM
2835
2836 struct ui_file *tmp_error_stream = mem_fileopen ();
2837 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2838
2839 /* Explicitly mark the warning -- this will only be printed if
2840 there was an error. */
2841 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2842
2843 save_current_space_and_thread ();
2844
2845 ALL_BP_LOCATIONS (bl, blp_tmp)
2846 {
2847 /* We only want to update software breakpoints and hardware
2848 breakpoints. */
2849 if (!is_breakpoint (bl->owner))
2850 continue;
2851
2852 /* We only want to update locations that are already inserted
2853 and need updating. This is to avoid unwanted insertion during
2854 deletion of breakpoints. */
2855 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2856 continue;
2857
2858 switch_to_program_space_and_thread (bl->pspace);
2859
2860 /* For targets that support global breakpoints, there's no need
2861 to select an inferior to insert breakpoint to. In fact, even
2862 if we aren't attached to any process yet, we should still
2863 insert breakpoints. */
f5656ead 2864 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2865 && ptid_equal (inferior_ptid, null_ptid))
2866 continue;
2867
2868 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 2869 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2870 if (val)
2871 error_flag = val;
2872 }
2873
2874 if (error_flag)
2875 {
2876 target_terminal_ours_for_output ();
2877 error_stream (tmp_error_stream);
2878 }
2879
2880 do_cleanups (cleanups);
2881}
2882
c30eee59 2883/* Used when starting or continuing the program. */
c906108c 2884
74960c60
VP
2885static void
2886insert_breakpoint_locations (void)
c906108c 2887{
a5606eee 2888 struct breakpoint *bpt;
35df4500 2889 struct bp_location *bl, **blp_tmp;
eacd795a 2890 int error_flag = 0;
c906108c 2891 int val = 0;
3fbb6ffa 2892 int disabled_breaks = 0;
81d0cc19 2893 int hw_breakpoint_error = 0;
dd61ec5c 2894 int hw_bp_error_explained_already = 0;
c906108c 2895
81d0cc19 2896 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2897 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2898
81d0cc19
GS
2899 /* Explicitly mark the warning -- this will only be printed if
2900 there was an error. */
2901 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2902
2903 save_current_space_and_thread ();
2904
35df4500 2905 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2906 {
b775012e 2907 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2908 continue;
2909
4a64f543
MS
2910 /* There is no point inserting thread-specific breakpoints if
2911 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2912 has BL->OWNER always non-NULL. */
35df4500
TJB
2913 if (bl->owner->thread != -1
2914 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2915 continue;
2916
35df4500 2917 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2918
2919 /* For targets that support global breakpoints, there's no need
2920 to select an inferior to insert breakpoint to. In fact, even
2921 if we aren't attached to any process yet, we should still
2922 insert breakpoints. */
f5656ead 2923 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
2924 && ptid_equal (inferior_ptid, null_ptid))
2925 continue;
2926
3fbb6ffa 2927 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 2928 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 2929 if (val)
eacd795a 2930 error_flag = val;
879bfdc2 2931 }
c906108c 2932
4a64f543
MS
2933 /* If we failed to insert all locations of a watchpoint, remove
2934 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2935 ALL_BREAKPOINTS (bpt)
2936 {
2937 int some_failed = 0;
2938 struct bp_location *loc;
2939
2940 if (!is_hardware_watchpoint (bpt))
2941 continue;
2942
d6b74ac4 2943 if (!breakpoint_enabled (bpt))
a5606eee 2944 continue;
74960c60
VP
2945
2946 if (bpt->disposition == disp_del_at_next_stop)
2947 continue;
a5606eee
VP
2948
2949 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2950 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2951 {
2952 some_failed = 1;
2953 break;
2954 }
2955 if (some_failed)
2956 {
2957 for (loc = bpt->loc; loc; loc = loc->next)
2958 if (loc->inserted)
2959 remove_breakpoint (loc, mark_uninserted);
2960
2961 hw_breakpoint_error = 1;
2962 fprintf_unfiltered (tmp_error_stream,
2963 "Could not insert hardware watchpoint %d.\n",
2964 bpt->number);
eacd795a 2965 error_flag = -1;
a5606eee
VP
2966 }
2967 }
2968
eacd795a 2969 if (error_flag)
81d0cc19
GS
2970 {
2971 /* If a hardware breakpoint or watchpoint was inserted, add a
2972 message about possibly exhausted resources. */
dd61ec5c 2973 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 2974 {
c6510018
MS
2975 fprintf_unfiltered (tmp_error_stream,
2976 "Could not insert hardware breakpoints:\n\
2977You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2978 }
81d0cc19
GS
2979 target_terminal_ours_for_output ();
2980 error_stream (tmp_error_stream);
2981 }
f7545552
TT
2982
2983 do_cleanups (cleanups);
c906108c
SS
2984}
2985
c30eee59
TJB
2986/* Used when the program stops.
2987 Returns zero if successful, or non-zero if there was a problem
2988 removing a breakpoint location. */
2989
c906108c 2990int
fba45db2 2991remove_breakpoints (void)
c906108c 2992{
35df4500 2993 struct bp_location *bl, **blp_tmp;
3a1bae8e 2994 int val = 0;
c906108c 2995
35df4500 2996 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2997 {
1e4d1764 2998 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2999 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3000 }
3a1bae8e 3001 return val;
c906108c
SS
3002}
3003
49fa26b0
PA
3004/* When a thread exits, remove breakpoints that are related to
3005 that thread. */
3006
3007static void
3008remove_threaded_breakpoints (struct thread_info *tp, int silent)
3009{
3010 struct breakpoint *b, *b_tmp;
3011
3012 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3013 {
96181529 3014 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3015 {
3016 b->disposition = disp_del_at_next_stop;
3017
3018 printf_filtered (_("\
46ecd527 3019Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3020 b->number, tp->num);
3021
3022 /* Hide it from the user. */
3023 b->number = 0;
3024 }
3025 }
3026}
3027
6c95b8df
PA
3028/* Remove breakpoints of process PID. */
3029
3030int
3031remove_breakpoints_pid (int pid)
3032{
35df4500 3033 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3034 int val;
3035 struct inferior *inf = find_inferior_pid (pid);
3036
35df4500 3037 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3038 {
35df4500 3039 if (bl->pspace != inf->pspace)
6c95b8df
PA
3040 continue;
3041
d3ce09f5
SS
3042 if (bl->owner->type == bp_dprintf)
3043 continue;
3044
35df4500 3045 if (bl->inserted)
6c95b8df 3046 {
35df4500 3047 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3048 if (val != 0)
3049 return val;
3050 }
3051 }
3052 return 0;
3053}
3054
c906108c 3055int
fba45db2 3056reattach_breakpoints (int pid)
c906108c 3057{
6c95b8df 3058 struct cleanup *old_chain;
35df4500 3059 struct bp_location *bl, **blp_tmp;
c906108c 3060 int val;
86b887df 3061 struct ui_file *tmp_error_stream;
dd61ec5c 3062 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3063 struct inferior *inf;
3064 struct thread_info *tp;
3065
3066 tp = any_live_thread_of_process (pid);
3067 if (tp == NULL)
3068 return 1;
3069
3070 inf = find_inferior_pid (pid);
3071 old_chain = save_inferior_ptid ();
3072
3073 inferior_ptid = tp->ptid;
a4954f26 3074
86b887df 3075 tmp_error_stream = mem_fileopen ();
a4954f26 3076 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3077
35df4500 3078 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3079 {
35df4500 3080 if (bl->pspace != inf->pspace)
6c95b8df
PA
3081 continue;
3082
35df4500 3083 if (bl->inserted)
c5aa993b 3084 {
35df4500 3085 bl->inserted = 0;
dd61ec5c 3086 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3087 if (val != 0)
3088 {
ce696e05 3089 do_cleanups (old_chain);
c5aa993b
JM
3090 return val;
3091 }
3092 }
3093 }
ce696e05 3094 do_cleanups (old_chain);
c906108c
SS
3095 return 0;
3096}
3097
e58b0e63
PA
3098static int internal_breakpoint_number = -1;
3099
84f4c1fe
PM
3100/* Set the breakpoint number of B, depending on the value of INTERNAL.
3101 If INTERNAL is non-zero, the breakpoint number will be populated
3102 from internal_breakpoint_number and that variable decremented.
e5dd4106 3103 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3104 breakpoint_count and that value incremented. Internal breakpoints
3105 do not set the internal var bpnum. */
3106static void
3107set_breakpoint_number (int internal, struct breakpoint *b)
3108{
3109 if (internal)
3110 b->number = internal_breakpoint_number--;
3111 else
3112 {
3113 set_breakpoint_count (breakpoint_count + 1);
3114 b->number = breakpoint_count;
3115 }
3116}
3117
e62c965a 3118static struct breakpoint *
a6d9a66e 3119create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3120 CORE_ADDR address, enum bptype type,
c0a91b2b 3121 const struct breakpoint_ops *ops)
e62c965a 3122{
e62c965a
PP
3123 struct symtab_and_line sal;
3124 struct breakpoint *b;
3125
4a64f543 3126 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3127
3128 sal.pc = address;
3129 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3130 sal.pspace = current_program_space;
e62c965a 3131
06edf0c0 3132 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3133 b->number = internal_breakpoint_number--;
3134 b->disposition = disp_donttouch;
3135
3136 return b;
3137}
3138
17450429
PP
3139static const char *const longjmp_names[] =
3140 {
3141 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3142 };
3143#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3144
3145/* Per-objfile data private to breakpoint.c. */
3146struct breakpoint_objfile_data
3147{
3148 /* Minimal symbol for "_ovly_debug_event" (if any). */
3149 struct minimal_symbol *overlay_msym;
3150
3151 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3152 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
3153
28106bc2
SDJ
3154 /* True if we have looked for longjmp probes. */
3155 int longjmp_searched;
3156
3157 /* SystemTap probe points for longjmp (if any). */
3158 VEC (probe_p) *longjmp_probes;
3159
17450429
PP
3160 /* Minimal symbol for "std::terminate()" (if any). */
3161 struct minimal_symbol *terminate_msym;
3162
3163 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3164 struct minimal_symbol *exception_msym;
28106bc2
SDJ
3165
3166 /* True if we have looked for exception probes. */
3167 int exception_searched;
3168
3169 /* SystemTap probe points for unwinding (if any). */
3170 VEC (probe_p) *exception_probes;
17450429
PP
3171};
3172
3173static const struct objfile_data *breakpoint_objfile_key;
3174
3175/* Minimal symbol not found sentinel. */
3176static struct minimal_symbol msym_not_found;
3177
3178/* Returns TRUE if MSYM point to the "not found" sentinel. */
3179
3180static int
3181msym_not_found_p (const struct minimal_symbol *msym)
3182{
3183 return msym == &msym_not_found;
3184}
3185
3186/* Return per-objfile data needed by breakpoint.c.
3187 Allocate the data if necessary. */
3188
3189static struct breakpoint_objfile_data *
3190get_breakpoint_objfile_data (struct objfile *objfile)
3191{
3192 struct breakpoint_objfile_data *bp_objfile_data;
3193
3194 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3195 if (bp_objfile_data == NULL)
3196 {
3197 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3198 sizeof (*bp_objfile_data));
3199
3200 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3201 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3202 }
3203 return bp_objfile_data;
3204}
3205
28106bc2
SDJ
3206static void
3207free_breakpoint_probes (struct objfile *obj, void *data)
3208{
3209 struct breakpoint_objfile_data *bp_objfile_data = data;
3210
3211 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3212 VEC_free (probe_p, bp_objfile_data->exception_probes);
3213}
3214
e62c965a 3215static void
af02033e 3216create_overlay_event_breakpoint (void)
e62c965a 3217{
69de3c6a 3218 struct objfile *objfile;
af02033e 3219 const char *const func_name = "_ovly_debug_event";
e62c965a 3220
69de3c6a
PP
3221 ALL_OBJFILES (objfile)
3222 {
3223 struct breakpoint *b;
17450429
PP
3224 struct breakpoint_objfile_data *bp_objfile_data;
3225 CORE_ADDR addr;
69de3c6a 3226
17450429
PP
3227 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3228
3229 if (msym_not_found_p (bp_objfile_data->overlay_msym))
3230 continue;
3231
3232 if (bp_objfile_data->overlay_msym == NULL)
3233 {
3234 struct minimal_symbol *m;
3235
3236 m = lookup_minimal_symbol_text (func_name, objfile);
3237 if (m == NULL)
3238 {
3239 /* Avoid future lookups in this objfile. */
3240 bp_objfile_data->overlay_msym = &msym_not_found;
3241 continue;
3242 }
3243 bp_objfile_data->overlay_msym = m;
3244 }
e62c965a 3245
17450429
PP
3246 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3247 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3248 bp_overlay_event,
3249 &internal_breakpoint_ops);
69de3c6a 3250 b->addr_string = xstrdup (func_name);
e62c965a 3251
69de3c6a
PP
3252 if (overlay_debugging == ovly_auto)
3253 {
3254 b->enable_state = bp_enabled;
3255 overlay_events_enabled = 1;
3256 }
3257 else
3258 {
3259 b->enable_state = bp_disabled;
3260 overlay_events_enabled = 0;
3261 }
e62c965a
PP
3262 }
3263 update_global_location_list (1);
3264}
3265
0fd8e87f 3266static void
af02033e 3267create_longjmp_master_breakpoint (void)
0fd8e87f 3268{
6c95b8df 3269 struct program_space *pspace;
6c95b8df
PA
3270 struct cleanup *old_chain;
3271
3272 old_chain = save_current_program_space ();
0fd8e87f 3273
6c95b8df 3274 ALL_PSPACES (pspace)
af02033e
PP
3275 {
3276 struct objfile *objfile;
3277
3278 set_current_program_space (pspace);
3279
3280 ALL_OBJFILES (objfile)
0fd8e87f 3281 {
af02033e
PP
3282 int i;
3283 struct gdbarch *gdbarch;
17450429 3284 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3285
af02033e 3286 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3287
17450429
PP
3288 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3289
28106bc2
SDJ
3290 if (!bp_objfile_data->longjmp_searched)
3291 {
25f9533e
SDJ
3292 VEC (probe_p) *ret;
3293
3294 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3295 if (ret != NULL)
3296 {
3297 /* We are only interested in checking one element. */
3298 struct probe *p = VEC_index (probe_p, ret, 0);
3299
3300 if (!can_evaluate_probe_arguments (p))
3301 {
3302 /* We cannot use the probe interface here, because it does
3303 not know how to evaluate arguments. */
3304 VEC_free (probe_p, ret);
3305 ret = NULL;
3306 }
3307 }
3308 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3309 bp_objfile_data->longjmp_searched = 1;
3310 }
3311
3312 if (bp_objfile_data->longjmp_probes != NULL)
3313 {
3314 int i;
3315 struct probe *probe;
3316 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3317
3318 for (i = 0;
3319 VEC_iterate (probe_p,
3320 bp_objfile_data->longjmp_probes,
3321 i, probe);
3322 ++i)
3323 {
3324 struct breakpoint *b;
3325
3326 b = create_internal_breakpoint (gdbarch, probe->address,
3327 bp_longjmp_master,
3328 &internal_breakpoint_ops);
3329 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3330 b->enable_state = bp_disabled;
3331 }
3332
3333 continue;
3334 }
3335
0569175e
TSD
3336 if (!gdbarch_get_longjmp_target_p (gdbarch))
3337 continue;
3338
17450429 3339 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3340 {
3341 struct breakpoint *b;
af02033e 3342 const char *func_name;
17450429 3343 CORE_ADDR addr;
6c95b8df 3344
17450429 3345 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 3346 continue;
0fd8e87f 3347
17450429
PP
3348 func_name = longjmp_names[i];
3349 if (bp_objfile_data->longjmp_msym[i] == NULL)
3350 {
3351 struct minimal_symbol *m;
3352
3353 m = lookup_minimal_symbol_text (func_name, objfile);
3354 if (m == NULL)
3355 {
3356 /* Prevent future lookups in this objfile. */
3357 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
3358 continue;
3359 }
3360 bp_objfile_data->longjmp_msym[i] = m;
3361 }
3362
3363 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3364 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3365 &internal_breakpoint_ops);
af02033e
PP
3366 b->addr_string = xstrdup (func_name);
3367 b->enable_state = bp_disabled;
3368 }
0fd8e87f 3369 }
af02033e 3370 }
0fd8e87f 3371 update_global_location_list (1);
6c95b8df
PA
3372
3373 do_cleanups (old_chain);
0fd8e87f
UW
3374}
3375
af02033e 3376/* Create a master std::terminate breakpoint. */
aa7d318d 3377static void
af02033e 3378create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3379{
3380 struct program_space *pspace;
aa7d318d 3381 struct cleanup *old_chain;
af02033e 3382 const char *const func_name = "std::terminate()";
aa7d318d
TT
3383
3384 old_chain = save_current_program_space ();
3385
3386 ALL_PSPACES (pspace)
17450429
PP
3387 {
3388 struct objfile *objfile;
3389 CORE_ADDR addr;
3390
3391 set_current_program_space (pspace);
3392
aa7d318d
TT
3393 ALL_OBJFILES (objfile)
3394 {
3395 struct breakpoint *b;
17450429 3396 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3397
17450429 3398 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3399
17450429
PP
3400 if (msym_not_found_p (bp_objfile_data->terminate_msym))
3401 continue;
3402
3403 if (bp_objfile_data->terminate_msym == NULL)
3404 {
3405 struct minimal_symbol *m;
3406
3407 m = lookup_minimal_symbol (func_name, NULL, objfile);
3408 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
3409 && MSYMBOL_TYPE (m) != mst_file_text))
3410 {
3411 /* Prevent future lookups in this objfile. */
3412 bp_objfile_data->terminate_msym = &msym_not_found;
3413 continue;
3414 }
3415 bp_objfile_data->terminate_msym = m;
3416 }
aa7d318d 3417
17450429
PP
3418 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3419 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3420 bp_std_terminate_master,
3421 &internal_breakpoint_ops);
aa7d318d
TT
3422 b->addr_string = xstrdup (func_name);
3423 b->enable_state = bp_disabled;
3424 }
17450429
PP
3425 }
3426
aa7d318d
TT
3427 update_global_location_list (1);
3428
3429 do_cleanups (old_chain);
3430}
3431
186c406b
TT
3432/* Install a master breakpoint on the unwinder's debug hook. */
3433
70221824 3434static void
186c406b
TT
3435create_exception_master_breakpoint (void)
3436{
3437 struct objfile *objfile;
17450429 3438 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3439
3440 ALL_OBJFILES (objfile)
3441 {
17450429
PP
3442 struct breakpoint *b;
3443 struct gdbarch *gdbarch;
3444 struct breakpoint_objfile_data *bp_objfile_data;
3445 CORE_ADDR addr;
3446
3447 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3448
28106bc2
SDJ
3449 /* We prefer the SystemTap probe point if it exists. */
3450 if (!bp_objfile_data->exception_searched)
3451 {
25f9533e
SDJ
3452 VEC (probe_p) *ret;
3453
3454 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3455
3456 if (ret != NULL)
3457 {
3458 /* We are only interested in checking one element. */
3459 struct probe *p = VEC_index (probe_p, ret, 0);
3460
3461 if (!can_evaluate_probe_arguments (p))
3462 {
3463 /* We cannot use the probe interface here, because it does
3464 not know how to evaluate arguments. */
3465 VEC_free (probe_p, ret);
3466 ret = NULL;
3467 }
3468 }
3469 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3470 bp_objfile_data->exception_searched = 1;
3471 }
3472
3473 if (bp_objfile_data->exception_probes != NULL)
3474 {
3475 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3476 int i;
3477 struct probe *probe;
3478
3479 for (i = 0;
3480 VEC_iterate (probe_p,
3481 bp_objfile_data->exception_probes,
3482 i, probe);
3483 ++i)
3484 {
3485 struct breakpoint *b;
3486
3487 b = create_internal_breakpoint (gdbarch, probe->address,
3488 bp_exception_master,
3489 &internal_breakpoint_ops);
3490 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3491 b->enable_state = bp_disabled;
3492 }
3493
3494 continue;
3495 }
3496
3497 /* Otherwise, try the hook function. */
3498
17450429
PP
3499 if (msym_not_found_p (bp_objfile_data->exception_msym))
3500 continue;
3501
3502 gdbarch = get_objfile_arch (objfile);
186c406b 3503
17450429 3504 if (bp_objfile_data->exception_msym == NULL)
186c406b 3505 {
17450429 3506 struct minimal_symbol *debug_hook;
186c406b 3507
17450429
PP
3508 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3509 if (debug_hook == NULL)
3510 {
3511 bp_objfile_data->exception_msym = &msym_not_found;
3512 continue;
3513 }
3514
3515 bp_objfile_data->exception_msym = debug_hook;
186c406b 3516 }
17450429
PP
3517
3518 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3519 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3520 &current_target);
06edf0c0
PA
3521 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3522 &internal_breakpoint_ops);
17450429
PP
3523 b->addr_string = xstrdup (func_name);
3524 b->enable_state = bp_disabled;
186c406b
TT
3525 }
3526
3527 update_global_location_list (1);
3528}
3529
c906108c 3530void
fba45db2 3531update_breakpoints_after_exec (void)
c906108c 3532{
35df4500 3533 struct breakpoint *b, *b_tmp;
876fa593 3534 struct bp_location *bploc, **bplocp_tmp;
c906108c 3535
25b22b0a
PA
3536 /* We're about to delete breakpoints from GDB's lists. If the
3537 INSERTED flag is true, GDB will try to lift the breakpoints by
3538 writing the breakpoints' "shadow contents" back into memory. The
3539 "shadow contents" are NOT valid after an exec, so GDB should not
3540 do that. Instead, the target is responsible from marking
3541 breakpoints out as soon as it detects an exec. We don't do that
3542 here instead, because there may be other attempts to delete
3543 breakpoints after detecting an exec and before reaching here. */
876fa593 3544 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3545 if (bploc->pspace == current_program_space)
3546 gdb_assert (!bploc->inserted);
c906108c 3547
35df4500 3548 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3549 {
6c95b8df
PA
3550 if (b->pspace != current_program_space)
3551 continue;
3552
4a64f543 3553 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3554 if (b->type == bp_shlib_event)
3555 {
3556 delete_breakpoint (b);
3557 continue;
3558 }
c906108c 3559
4a64f543 3560 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3561 if (b->type == bp_jit_event)
3562 {
3563 delete_breakpoint (b);
3564 continue;
3565 }
3566
1900040c 3567 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3568 as must overlay event and longjmp master breakpoints. */
3569 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3570 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3571 || b->type == bp_exception_master)
c4093a6a
JM
3572 {
3573 delete_breakpoint (b);
3574 continue;
3575 }
3576
4a64f543 3577 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3578 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3579 {
3580 delete_breakpoint (b);
3581 continue;
3582 }
3583
611c83ae
PA
3584 /* Longjmp and longjmp-resume breakpoints are also meaningless
3585 after an exec. */
186c406b 3586 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3587 || b->type == bp_longjmp_call_dummy
186c406b 3588 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3589 {
3590 delete_breakpoint (b);
3591 continue;
3592 }
3593
ce78b96d
JB
3594 if (b->type == bp_catchpoint)
3595 {
3596 /* For now, none of the bp_catchpoint breakpoints need to
3597 do anything at this point. In the future, if some of
3598 the catchpoints need to something, we will need to add
3599 a new method, and call this method from here. */
3600 continue;
3601 }
3602
c5aa993b
JM
3603 /* bp_finish is a special case. The only way we ought to be able
3604 to see one of these when an exec() has happened, is if the user
3605 caught a vfork, and then said "finish". Ordinarily a finish just
3606 carries them to the call-site of the current callee, by setting
3607 a temporary bp there and resuming. But in this case, the finish
3608 will carry them entirely through the vfork & exec.
3609
3610 We don't want to allow a bp_finish to remain inserted now. But
3611 we can't safely delete it, 'cause finish_command has a handle to
3612 the bp on a bpstat, and will later want to delete it. There's a
3613 chance (and I've seen it happen) that if we delete the bp_finish
3614 here, that its storage will get reused by the time finish_command
3615 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3616 We really must allow finish_command to delete a bp_finish.
3617
e5dd4106 3618 In the absence of a general solution for the "how do we know
53a5351d
JM
3619 it's safe to delete something others may have handles to?"
3620 problem, what we'll do here is just uninsert the bp_finish, and
3621 let finish_command delete it.
3622
3623 (We know the bp_finish is "doomed" in the sense that it's
3624 momentary, and will be deleted as soon as finish_command sees
3625 the inferior stopped. So it doesn't matter that the bp's
3626 address is probably bogus in the new a.out, unlike e.g., the
3627 solib breakpoints.) */
c5aa993b 3628
c5aa993b
JM
3629 if (b->type == bp_finish)
3630 {
3631 continue;
3632 }
3633
3634 /* Without a symbolic address, we have little hope of the
3635 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3636 a.out. */
c5aa993b
JM
3637 if (b->addr_string == NULL)
3638 {
3639 delete_breakpoint (b);
3640 continue;
3641 }
c5aa993b 3642 }
1900040c 3643 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3644 create_overlay_event_breakpoint ();
3645 create_longjmp_master_breakpoint ();
3646 create_std_terminate_master_breakpoint ();
186c406b 3647 create_exception_master_breakpoint ();
c906108c
SS
3648}
3649
3650int
d80ee84f 3651detach_breakpoints (ptid_t ptid)
c906108c 3652{
35df4500 3653 struct bp_location *bl, **blp_tmp;
3a1bae8e 3654 int val = 0;
ce696e05 3655 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3656 struct inferior *inf = current_inferior ();
c5aa993b 3657
dfd4cc63 3658 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3659 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3660
6c95b8df 3661 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3662 inferior_ptid = ptid;
35df4500 3663 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3664 {
35df4500 3665 if (bl->pspace != inf->pspace)
6c95b8df
PA
3666 continue;
3667
bd9673a4
PW
3668 /* This function must physically remove breakpoints locations
3669 from the specified ptid, without modifying the breakpoint
3670 package's state. Locations of type bp_loc_other are only
3671 maintained at GDB side. So, there is no need to remove
3672 these bp_loc_other locations. Moreover, removing these
3673 would modify the breakpoint package's state. */
3674 if (bl->loc_type == bp_loc_other)
3675 continue;
3676
35df4500
TJB
3677 if (bl->inserted)
3678 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3679 }
d03285ec
UW
3680
3681 /* Detach single-step breakpoints as well. */
3682 detach_single_step_breakpoints ();
3683
ce696e05 3684 do_cleanups (old_chain);
3a1bae8e 3685 return val;
c906108c
SS
3686}
3687
35df4500 3688/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3689 Note that this is used to detach breakpoints from a child fork.
3690 When we get here, the child isn't in the inferior list, and neither
3691 do we have objects to represent its address space --- we should
35df4500 3692 *not* look at bl->pspace->aspace here. */
6c95b8df 3693
c906108c 3694static int
35df4500 3695remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3696{
3697 int val;
c5aa993b 3698
35df4500
TJB
3699 /* BL is never in moribund_locations by our callers. */
3700 gdb_assert (bl->owner != NULL);
2bdf28a0 3701
35df4500 3702 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3703 /* Permanent breakpoints cannot be inserted or removed. */
3704 return 0;
3705
74960c60
VP
3706 /* The type of none suggests that owner is actually deleted.
3707 This should not ever happen. */
35df4500 3708 gdb_assert (bl->owner->type != bp_none);
0bde7532 3709
35df4500
TJB
3710 if (bl->loc_type == bp_loc_software_breakpoint
3711 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3712 {
c02f5703
MS
3713 /* "Normal" instruction breakpoint: either the standard
3714 trap-instruction bp (bp_breakpoint), or a
3715 bp_hardware_breakpoint. */
3716
3717 /* First check to see if we have to handle an overlay. */
3718 if (overlay_debugging == ovly_off
35df4500
TJB
3719 || bl->section == NULL
3720 || !(section_is_overlay (bl->section)))
c02f5703
MS
3721 {
3722 /* No overlay handling: just remove the breakpoint. */
348d480f 3723 val = bl->owner->ops->remove_location (bl);
c02f5703 3724 }
c906108c
SS
3725 else
3726 {
4a64f543 3727 /* This breakpoint is in an overlay section.
c02f5703
MS
3728 Did we set a breakpoint at the LMA? */
3729 if (!overlay_events_enabled)
3730 {
3731 /* Yes -- overlay event support is not active, so we
3732 should have set a breakpoint at the LMA. Remove it.
3733 */
c02f5703
MS
3734 /* Ignore any failures: if the LMA is in ROM, we will
3735 have already warned when we failed to insert it. */
35df4500
TJB
3736 if (bl->loc_type == bp_loc_hardware_breakpoint)
3737 target_remove_hw_breakpoint (bl->gdbarch,
3738 &bl->overlay_target_info);
c02f5703 3739 else
35df4500
TJB
3740 target_remove_breakpoint (bl->gdbarch,
3741 &bl->overlay_target_info);
c02f5703
MS
3742 }
3743 /* Did we set a breakpoint at the VMA?
3744 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3745 if (bl->inserted)
c906108c 3746 {
c02f5703
MS
3747 /* Yes -- remove it. Previously we did not bother to
3748 remove the breakpoint if the section had been
3749 unmapped, but let's not rely on that being safe. We
3750 don't know what the overlay manager might do. */
aa67235e
UW
3751
3752 /* However, we should remove *software* breakpoints only
3753 if the section is still mapped, or else we overwrite
3754 wrong code with the saved shadow contents. */
348d480f
PA
3755 if (bl->loc_type == bp_loc_hardware_breakpoint
3756 || section_is_mapped (bl->section))
3757 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3758 else
3759 val = 0;
c906108c 3760 }
c02f5703
MS
3761 else
3762 {
3763 /* No -- not inserted, so no need to remove. No error. */
3764 val = 0;
3765 }
c906108c 3766 }
879d1e6b
UW
3767
3768 /* In some cases, we might not be able to remove a breakpoint
3769 in a shared library that has already been removed, but we
3770 have not yet processed the shlib unload event. */
35df4500 3771 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
3772 val = 0;
3773
c906108c
SS
3774 if (val)
3775 return val;
35df4500 3776 bl->inserted = (is == mark_inserted);
c906108c 3777 }
35df4500 3778 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3779 {
77b06cd7
TJB
3780 gdb_assert (bl->owner->ops != NULL
3781 && bl->owner->ops->remove_location != NULL);
3782
35df4500 3783 bl->inserted = (is == mark_inserted);
77b06cd7 3784 bl->owner->ops->remove_location (bl);
2e70b7b9 3785
c906108c 3786 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3787 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 3788 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3789 bl->owner->number);
c906108c 3790 }
35df4500
TJB
3791 else if (bl->owner->type == bp_catchpoint
3792 && breakpoint_enabled (bl->owner)
3793 && !bl->duplicate)
ce78b96d 3794 {
77b06cd7
TJB
3795 gdb_assert (bl->owner->ops != NULL
3796 && bl->owner->ops->remove_location != NULL);
ce78b96d 3797
77b06cd7 3798 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
3799 if (val)
3800 return val;
77b06cd7 3801
35df4500 3802 bl->inserted = (is == mark_inserted);
ce78b96d 3803 }
c906108c
SS
3804
3805 return 0;
3806}
3807
6c95b8df 3808static int
35df4500 3809remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
3810{
3811 int ret;
3812 struct cleanup *old_chain;
3813
35df4500
TJB
3814 /* BL is never in moribund_locations by our callers. */
3815 gdb_assert (bl->owner != NULL);
2bdf28a0 3816
35df4500 3817 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
3818 /* Permanent breakpoints cannot be inserted or removed. */
3819 return 0;
3820
3821 /* The type of none suggests that owner is actually deleted.
3822 This should not ever happen. */
35df4500 3823 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
3824
3825 old_chain = save_current_space_and_thread ();
3826
35df4500 3827 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3828
35df4500 3829 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
3830
3831 do_cleanups (old_chain);
3832 return ret;
3833}
3834
c906108c
SS
3835/* Clear the "inserted" flag in all breakpoints. */
3836
25b22b0a 3837void
fba45db2 3838mark_breakpoints_out (void)
c906108c 3839{
35df4500 3840 struct bp_location *bl, **blp_tmp;
c906108c 3841
35df4500
TJB
3842 ALL_BP_LOCATIONS (bl, blp_tmp)
3843 if (bl->pspace == current_program_space)
3844 bl->inserted = 0;
c906108c
SS
3845}
3846
53a5351d
JM
3847/* Clear the "inserted" flag in all breakpoints and delete any
3848 breakpoints which should go away between runs of the program.
c906108c
SS
3849
3850 Plus other such housekeeping that has to be done for breakpoints
3851 between runs.
3852
53a5351d
JM
3853 Note: this function gets called at the end of a run (by
3854 generic_mourn_inferior) and when a run begins (by
4a64f543 3855 init_wait_for_inferior). */
c906108c
SS
3856
3857
3858
3859void
fba45db2 3860breakpoint_init_inferior (enum inf_context context)
c906108c 3861{
35df4500
TJB
3862 struct breakpoint *b, *b_tmp;
3863 struct bp_location *bl, **blp_tmp;
1c5cfe86 3864 int ix;
6c95b8df 3865 struct program_space *pspace = current_program_space;
c906108c 3866
50c71eaf
PA
3867 /* If breakpoint locations are shared across processes, then there's
3868 nothing to do. */
f5656ead 3869 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3870 return;
3871
35df4500 3872 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3873 {
35df4500
TJB
3874 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3875 if (bl->pspace == pspace
3876 && bl->owner->enable_state != bp_permanent)
3877 bl->inserted = 0;
6c95b8df 3878 }
075f6582 3879
35df4500 3880 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3881 {
6c95b8df
PA
3882 if (b->loc && b->loc->pspace != pspace)
3883 continue;
3884
c5aa993b
JM
3885 switch (b->type)
3886 {
3887 case bp_call_dummy:
e2e4d78b 3888 case bp_longjmp_call_dummy:
c906108c 3889
c5aa993b 3890 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3891 cause problems when the inferior is rerun, so we better get
3892 rid of it. */
3893
3894 case bp_watchpoint_scope:
3895
3896 /* Also get rid of scope breakpoints. */
3897
3898 case bp_shlib_event:
3899
3900 /* Also remove solib event breakpoints. Their addresses may
3901 have changed since the last time we ran the program.
3902 Actually we may now be debugging against different target;
3903 and so the solib backend that installed this breakpoint may
3904 not be used in by the target. E.g.,
3905
3906 (gdb) file prog-linux
3907 (gdb) run # native linux target
3908 ...
3909 (gdb) kill
3910 (gdb) file prog-win.exe
3911 (gdb) tar rem :9999 # remote Windows gdbserver.
3912 */
c906108c 3913
f59f708a
PA
3914 case bp_step_resume:
3915
3916 /* Also remove step-resume breakpoints. */
3917
c5aa993b
JM
3918 delete_breakpoint (b);
3919 break;
c906108c 3920
c5aa993b
JM
3921 case bp_watchpoint:
3922 case bp_hardware_watchpoint:
3923 case bp_read_watchpoint:
3924 case bp_access_watchpoint:
3a5c3e22
PA
3925 {
3926 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3927
3a5c3e22
PA
3928 /* Likewise for watchpoints on local expressions. */
3929 if (w->exp_valid_block != NULL)
3930 delete_breakpoint (b);
3931 else if (context == inf_starting)
3932 {
3933 /* Reset val field to force reread of starting value in
3934 insert_breakpoints. */
3935 if (w->val)
3936 value_free (w->val);
3937 w->val = NULL;
3938 w->val_valid = 0;
c860120c 3939 }
3a5c3e22 3940 }
c5aa993b
JM
3941 break;
3942 default:
c5aa993b
JM
3943 break;
3944 }
3945 }
1c5cfe86
PA
3946
3947 /* Get rid of the moribund locations. */
35df4500
TJB
3948 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3949 decref_bp_location (&bl);
1c5cfe86 3950 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
3951}
3952
6c95b8df
PA
3953/* These functions concern about actual breakpoints inserted in the
3954 target --- to e.g. check if we need to do decr_pc adjustment or if
3955 we need to hop over the bkpt --- so we check for address space
3956 match, not program space. */
3957
c2c6d25f
JM
3958/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3959 exists at PC. It returns ordinary_breakpoint_here if it's an
3960 ordinary breakpoint, or permanent_breakpoint_here if it's a
3961 permanent breakpoint.
3962 - When continuing from a location with an ordinary breakpoint, we
3963 actually single step once before calling insert_breakpoints.
e5dd4106 3964 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3965 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3966 the target, to advance the PC past the breakpoint. */
c906108c 3967
c2c6d25f 3968enum breakpoint_here
6c95b8df 3969breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 3970{
35df4500 3971 struct bp_location *bl, **blp_tmp;
c2c6d25f 3972 int any_breakpoint_here = 0;
c906108c 3973
35df4500 3974 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3975 {
35df4500
TJB
3976 if (bl->loc_type != bp_loc_software_breakpoint
3977 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3978 continue;
3979
f1310107 3980 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
3981 if ((breakpoint_enabled (bl->owner)
3982 || bl->owner->enable_state == bp_permanent)
f1310107 3983 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3984 {
3985 if (overlay_debugging
35df4500
TJB
3986 && section_is_overlay (bl->section)
3987 && !section_is_mapped (bl->section))
075f6582 3988 continue; /* unmapped overlay -- can't be a match */
35df4500 3989 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
3990 return permanent_breakpoint_here;
3991 else
3992 any_breakpoint_here = 1;
3993 }
3994 }
c906108c 3995
c2c6d25f 3996 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
3997}
3998
1c5cfe86
PA
3999/* Return true if there's a moribund breakpoint at PC. */
4000
4001int
6c95b8df 4002moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4003{
4004 struct bp_location *loc;
4005 int ix;
4006
4007 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4008 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4009 return 1;
4010
4011 return 0;
4012}
c2c6d25f 4013
c36b740a 4014/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
4015 inserted using regular breakpoint_chain / bp_location array
4016 mechanism. This does not check for single-step breakpoints, which
4017 are inserted and removed using direct target manipulation. */
c906108c
SS
4018
4019int
4a64f543
MS
4020regular_breakpoint_inserted_here_p (struct address_space *aspace,
4021 CORE_ADDR pc)
c906108c 4022{
35df4500 4023 struct bp_location *bl, **blp_tmp;
c906108c 4024
35df4500 4025 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4026 {
35df4500
TJB
4027 if (bl->loc_type != bp_loc_software_breakpoint
4028 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4029 continue;
4030
35df4500 4031 if (bl->inserted
f1310107 4032 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4033 {
4034 if (overlay_debugging
35df4500
TJB
4035 && section_is_overlay (bl->section)
4036 && !section_is_mapped (bl->section))
075f6582
DJ
4037 continue; /* unmapped overlay -- can't be a match */
4038 else
4039 return 1;
4040 }
c5aa993b 4041 }
c36b740a
VP
4042 return 0;
4043}
4044
4045/* Returns non-zero iff there's either regular breakpoint
4046 or a single step breakpoint inserted at PC. */
4047
4048int
6c95b8df 4049breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 4050{
6c95b8df 4051 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 4052 return 1;
c906108c 4053
6c95b8df 4054 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4055 return 1;
4056
c906108c
SS
4057 return 0;
4058}
4059
4fa8626c
DJ
4060/* This function returns non-zero iff there is a software breakpoint
4061 inserted at PC. */
4062
4063int
3e43a32a
MS
4064software_breakpoint_inserted_here_p (struct address_space *aspace,
4065 CORE_ADDR pc)
4fa8626c 4066{
35df4500 4067 struct bp_location *bl, **blp_tmp;
4fa8626c 4068
35df4500 4069 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 4070 {
35df4500 4071 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4072 continue;
4073
35df4500
TJB
4074 if (bl->inserted
4075 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 4076 aspace, pc))
4fa8626c
DJ
4077 {
4078 if (overlay_debugging
35df4500
TJB
4079 && section_is_overlay (bl->section)
4080 && !section_is_mapped (bl->section))
4fa8626c
DJ
4081 continue; /* unmapped overlay -- can't be a match */
4082 else
4083 return 1;
4084 }
4085 }
4086
1aafd4da 4087 /* Also check for software single-step breakpoints. */
6c95b8df 4088 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4089 return 1;
4090
4fa8626c
DJ
4091 return 0;
4092}
4093
9093389c
PA
4094int
4095hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4096 CORE_ADDR addr, ULONGEST len)
4097{
4098 struct breakpoint *bpt;
4099
4100 ALL_BREAKPOINTS (bpt)
4101 {
4102 struct bp_location *loc;
4103
4104 if (bpt->type != bp_hardware_watchpoint
4105 && bpt->type != bp_access_watchpoint)
4106 continue;
4107
4108 if (!breakpoint_enabled (bpt))
4109 continue;
4110
4111 for (loc = bpt->loc; loc; loc = loc->next)
4112 if (loc->pspace->aspace == aspace && loc->inserted)
4113 {
4114 CORE_ADDR l, h;
4115
4116 /* Check for intersection. */
4117 l = max (loc->address, addr);
4118 h = min (loc->address + loc->length, addr + len);
4119 if (l < h)
4120 return 1;
4121 }
4122 }
4123 return 0;
4124}
4125
075f6582
DJ
4126/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4127 PC is valid for process/thread PTID. */
c906108c
SS
4128
4129int
6c95b8df
PA
4130breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4131 ptid_t ptid)
c906108c 4132{
35df4500 4133 struct bp_location *bl, **blp_tmp;
4a306c9a 4134 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 4135 int thread = -1;
4a306c9a 4136 int task = 0;
a6f1cd96 4137
35df4500 4138 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4139 {
35df4500
TJB
4140 if (bl->loc_type != bp_loc_software_breakpoint
4141 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4142 continue;
4143
35df4500
TJB
4144 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4145 if (!breakpoint_enabled (bl->owner)
4146 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
4147 continue;
4148
f1310107 4149 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
4150 continue;
4151
35df4500 4152 if (bl->owner->thread != -1)
075f6582 4153 {
a6f1cd96
JB
4154 /* This is a thread-specific breakpoint. Check that ptid
4155 matches that thread. If thread hasn't been computed yet,
4156 it is now time to do so. */
4157 if (thread == -1)
4158 thread = pid_to_thread_id (ptid);
35df4500 4159 if (bl->owner->thread != thread)
a6f1cd96 4160 continue;
075f6582 4161 }
a6f1cd96 4162
35df4500 4163 if (bl->owner->task != 0)
4a306c9a
JB
4164 {
4165 /* This is a task-specific breakpoint. Check that ptid
4166 matches that task. If task hasn't been computed yet,
4167 it is now time to do so. */
4168 if (task == 0)
4169 task = ada_get_task_number (ptid);
35df4500 4170 if (bl->owner->task != task)
4a306c9a
JB
4171 continue;
4172 }
4173
a6f1cd96 4174 if (overlay_debugging
35df4500
TJB
4175 && section_is_overlay (bl->section)
4176 && !section_is_mapped (bl->section))
a6f1cd96
JB
4177 continue; /* unmapped overlay -- can't be a match */
4178
4179 return 1;
c5aa993b 4180 }
c906108c
SS
4181
4182 return 0;
4183}
c906108c 4184\f
c5aa993b 4185
c906108c
SS
4186/* bpstat stuff. External routines' interfaces are documented
4187 in breakpoint.h. */
4188
4189int
c326b90e 4190is_catchpoint (struct breakpoint *ep)
c906108c 4191{
533be4dd 4192 return (ep->type == bp_catchpoint);
c906108c
SS
4193}
4194
f431efe5
PA
4195/* Frees any storage that is part of a bpstat. Does not walk the
4196 'next' chain. */
4197
4198static void
198757a8
VP
4199bpstat_free (bpstat bs)
4200{
4201 if (bs->old_val != NULL)
4202 value_free (bs->old_val);
9add0f1b 4203 decref_counted_command_line (&bs->commands);
f431efe5 4204 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4205 xfree (bs);
4206}
4207
c906108c
SS
4208/* Clear a bpstat so that it says we are not at any breakpoint.
4209 Also free any storage that is part of a bpstat. */
4210
4211void
fba45db2 4212bpstat_clear (bpstat *bsp)
c906108c
SS
4213{
4214 bpstat p;
4215 bpstat q;
4216
4217 if (bsp == 0)
4218 return;
4219 p = *bsp;
4220 while (p != NULL)
4221 {
4222 q = p->next;
198757a8 4223 bpstat_free (p);
c906108c
SS
4224 p = q;
4225 }
4226 *bsp = NULL;
4227}
4228
4229/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4230 is part of the bpstat is copied as well. */
4231
4232bpstat
fba45db2 4233bpstat_copy (bpstat bs)
c906108c
SS
4234{
4235 bpstat p = NULL;
4236 bpstat tmp;
4237 bpstat retval = NULL;
4238
4239 if (bs == NULL)
4240 return bs;
4241
4242 for (; bs != NULL; bs = bs->next)
4243 {
4244 tmp = (bpstat) xmalloc (sizeof (*tmp));
4245 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4246 incref_counted_command_line (tmp->commands);
f431efe5 4247 incref_bp_location (tmp->bp_location_at);
31cc81e9 4248 if (bs->old_val != NULL)
3c3185ac
JK
4249 {
4250 tmp->old_val = value_copy (bs->old_val);
4251 release_value (tmp->old_val);
4252 }
31cc81e9 4253
c906108c
SS
4254 if (p == NULL)
4255 /* This is the first thing in the chain. */
4256 retval = tmp;
4257 else
4258 p->next = tmp;
4259 p = tmp;
4260 }
4261 p->next = NULL;
4262 return retval;
4263}
4264
4a64f543 4265/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4266
4267bpstat
fba45db2 4268bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4269{
c5aa993b
JM
4270 if (bsp == NULL)
4271 return NULL;
c906108c 4272
c5aa993b
JM
4273 for (; bsp != NULL; bsp = bsp->next)
4274 {
f431efe5 4275 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4276 return bsp;
4277 }
c906108c
SS
4278 return NULL;
4279}
4280
ab04a2af
TT
4281/* See breakpoint.h. */
4282
47591c29 4283int
427cd150 4284bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4285{
ab04a2af
TT
4286 for (; bsp != NULL; bsp = bsp->next)
4287 {
427cd150
TT
4288 if (bsp->breakpoint_at == NULL)
4289 {
4290 /* A moribund location can never explain a signal other than
4291 GDB_SIGNAL_TRAP. */
4292 if (sig == GDB_SIGNAL_TRAP)
47591c29 4293 return 1;
427cd150
TT
4294 }
4295 else
47591c29
PA
4296 {
4297 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4298 sig))
4299 return 1;
4300 }
ab04a2af
TT
4301 }
4302
47591c29 4303 return 0;
ab04a2af
TT
4304}
4305
4a64f543
MS
4306/* Put in *NUM the breakpoint number of the first breakpoint we are
4307 stopped at. *BSP upon return is a bpstat which points to the
4308 remaining breakpoints stopped at (but which is not guaranteed to be
4309 good for anything but further calls to bpstat_num).
4310
8671a17b
PA
4311 Return 0 if passed a bpstat which does not indicate any breakpoints.
4312 Return -1 if stopped at a breakpoint that has been deleted since
4313 we set it.
4314 Return 1 otherwise. */
c906108c
SS
4315
4316int
8671a17b 4317bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4318{
4319 struct breakpoint *b;
4320
4321 if ((*bsp) == NULL)
4322 return 0; /* No more breakpoint values */
8671a17b 4323
4a64f543
MS
4324 /* We assume we'll never have several bpstats that correspond to a
4325 single breakpoint -- otherwise, this function might return the
4326 same number more than once and this will look ugly. */
f431efe5 4327 b = (*bsp)->breakpoint_at;
8671a17b
PA
4328 *bsp = (*bsp)->next;
4329 if (b == NULL)
4330 return -1; /* breakpoint that's been deleted since */
4331
4332 *num = b->number; /* We have its number */
4333 return 1;
c906108c
SS
4334}
4335
e93ca019 4336/* See breakpoint.h. */
c906108c
SS
4337
4338void
e93ca019 4339bpstat_clear_actions (void)
c906108c 4340{
e93ca019
JK
4341 struct thread_info *tp;
4342 bpstat bs;
4343
4344 if (ptid_equal (inferior_ptid, null_ptid))
4345 return;
4346
4347 tp = find_thread_ptid (inferior_ptid);
4348 if (tp == NULL)
4349 return;
4350
4351 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4352 {
9add0f1b 4353 decref_counted_command_line (&bs->commands);
abf85f46 4354
c906108c
SS
4355 if (bs->old_val != NULL)
4356 {
4357 value_free (bs->old_val);
4358 bs->old_val = NULL;
4359 }
4360 }
4361}
4362
f3b1572e
PA
4363/* Called when a command is about to proceed the inferior. */
4364
4365static void
4366breakpoint_about_to_proceed (void)
4367{
4368 if (!ptid_equal (inferior_ptid, null_ptid))
4369 {
4370 struct thread_info *tp = inferior_thread ();
4371
4372 /* Allow inferior function calls in breakpoint commands to not
4373 interrupt the command list. When the call finishes
4374 successfully, the inferior will be standing at the same
4375 breakpoint as if nothing happened. */
16c381f0 4376 if (tp->control.in_infcall)
f3b1572e
PA
4377 return;
4378 }
4379
4380 breakpoint_proceeded = 1;
4381}
4382
4a64f543
MS
4383/* Stub for cleaning up our state if we error-out of a breakpoint
4384 command. */
c906108c 4385static void
4efb68b1 4386cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4387{
4388 executing_breakpoint_commands = 0;
4389}
4390
abf85f46
JK
4391/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4392 or its equivalent. */
4393
4394static int
4395command_line_is_silent (struct command_line *cmd)
4396{
4397 return cmd && (strcmp ("silent", cmd->line) == 0
4398 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4399}
4400
4a64f543
MS
4401/* Execute all the commands associated with all the breakpoints at
4402 this location. Any of these commands could cause the process to
4403 proceed beyond this point, etc. We look out for such changes by
4404 checking the global "breakpoint_proceeded" after each command.
c906108c 4405
347bddb7
PA
4406 Returns true if a breakpoint command resumed the inferior. In that
4407 case, it is the caller's responsibility to recall it again with the
4408 bpstat of the current thread. */
4409
4410static int
4411bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4412{
4413 bpstat bs;
4414 struct cleanup *old_chain;
347bddb7 4415 int again = 0;
c906108c
SS
4416
4417 /* Avoid endless recursion if a `source' command is contained
4418 in bs->commands. */
4419 if (executing_breakpoint_commands)
347bddb7 4420 return 0;
c906108c
SS
4421
4422 executing_breakpoint_commands = 1;
4423 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4424
cf6c5ffb
TT
4425 prevent_dont_repeat ();
4426
4a64f543 4427 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4428 bs = *bsp;
4429
4430 breakpoint_proceeded = 0;
4431 for (; bs != NULL; bs = bs->next)
4432 {
9add0f1b 4433 struct counted_command_line *ccmd;
6c50ab1c
JB
4434 struct command_line *cmd;
4435 struct cleanup *this_cmd_tree_chain;
4436
4437 /* Take ownership of the BSP's command tree, if it has one.
4438
4439 The command tree could legitimately contain commands like
4440 'step' and 'next', which call clear_proceed_status, which
4441 frees stop_bpstat's command tree. To make sure this doesn't
4442 free the tree we're executing out from under us, we need to
4443 take ownership of the tree ourselves. Since a given bpstat's
4444 commands are only executed once, we don't need to copy it; we
4445 can clear the pointer in the bpstat, and make sure we free
4446 the tree when we're done. */
9add0f1b
TT
4447 ccmd = bs->commands;
4448 bs->commands = NULL;
abf85f46
JK
4449 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4450 cmd = ccmd ? ccmd->commands : NULL;
4451 if (command_line_is_silent (cmd))
4452 {
4453 /* The action has been already done by bpstat_stop_status. */
4454 cmd = cmd->next;
4455 }
6c50ab1c 4456
c906108c
SS
4457 while (cmd != NULL)
4458 {
4459 execute_control_command (cmd);
4460
4461 if (breakpoint_proceeded)
4462 break;
4463 else
4464 cmd = cmd->next;
4465 }
6c50ab1c
JB
4466
4467 /* We can free this command tree now. */
4468 do_cleanups (this_cmd_tree_chain);
4469
c906108c 4470 if (breakpoint_proceeded)
32c1e744
VP
4471 {
4472 if (target_can_async_p ())
347bddb7
PA
4473 /* If we are in async mode, then the target might be still
4474 running, not stopped at any breakpoint, so nothing for
4475 us to do here -- just return to the event loop. */
4476 ;
32c1e744
VP
4477 else
4478 /* In sync mode, when execute_control_command returns
4479 we're already standing on the next breakpoint.
347bddb7
PA
4480 Breakpoint commands for that stop were not run, since
4481 execute_command does not run breakpoint commands --
4482 only command_line_handler does, but that one is not
4483 involved in execution of breakpoint commands. So, we
4484 can now execute breakpoint commands. It should be
4485 noted that making execute_command do bpstat actions is
4486 not an option -- in this case we'll have recursive
4487 invocation of bpstat for each breakpoint with a
4488 command, and can easily blow up GDB stack. Instead, we
4489 return true, which will trigger the caller to recall us
4490 with the new stop_bpstat. */
4491 again = 1;
4492 break;
32c1e744 4493 }
c906108c 4494 }
c2b8ed2c 4495 do_cleanups (old_chain);
347bddb7
PA
4496 return again;
4497}
4498
4499void
4500bpstat_do_actions (void)
4501{
353d1d73
JK
4502 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4503
347bddb7
PA
4504 /* Do any commands attached to breakpoint we are stopped at. */
4505 while (!ptid_equal (inferior_ptid, null_ptid)
4506 && target_has_execution
4507 && !is_exited (inferior_ptid)
4508 && !is_executing (inferior_ptid))
4509 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4510 and only return when it is stopped at the next breakpoint, we
4511 keep doing breakpoint actions until it returns false to
4512 indicate the inferior was not resumed. */
16c381f0 4513 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4514 break;
353d1d73
JK
4515
4516 discard_cleanups (cleanup_if_error);
c906108c
SS
4517}
4518
fa4727a6
DJ
4519/* Print out the (old or new) value associated with a watchpoint. */
4520
4521static void
4522watchpoint_value_print (struct value *val, struct ui_file *stream)
4523{
4524 if (val == NULL)
4525 fprintf_unfiltered (stream, _("<unreadable>"));
4526 else
79a45b7d
TT
4527 {
4528 struct value_print_options opts;
4529 get_user_print_options (&opts);
4530 value_print (val, stream, &opts);
4531 }
fa4727a6
DJ
4532}
4533
e514a9d6 4534/* Generic routine for printing messages indicating why we
4a64f543 4535 stopped. The behavior of this function depends on the value
e514a9d6
JM
4536 'print_it' in the bpstat structure. Under some circumstances we
4537 may decide not to print anything here and delegate the task to
4a64f543 4538 normal_stop(). */
e514a9d6
JM
4539
4540static enum print_stop_action
4541print_bp_stop_message (bpstat bs)
4542{
4543 switch (bs->print_it)
4544 {
4545 case print_it_noop:
4a64f543 4546 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4547 return PRINT_UNKNOWN;
4548 break;
4549
4550 case print_it_done:
4551 /* We still want to print the frame, but we already printed the
4a64f543 4552 relevant messages. */
e514a9d6
JM
4553 return PRINT_SRC_AND_LOC;
4554 break;
4555
4556 case print_it_normal:
4f8d1dc6 4557 {
f431efe5
PA
4558 struct breakpoint *b = bs->breakpoint_at;
4559
1a6a67de
TJB
4560 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4561 which has since been deleted. */
4562 if (b == NULL)
4563 return PRINT_UNKNOWN;
4564
348d480f
PA
4565 /* Normal case. Call the breakpoint's print_it method. */
4566 return b->ops->print_it (bs);
4f8d1dc6 4567 }
348d480f 4568 break;
3086aeae 4569
e514a9d6 4570 default:
8e65ff28 4571 internal_error (__FILE__, __LINE__,
e2e0b3e5 4572 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4573 break;
c906108c 4574 }
c906108c
SS
4575}
4576
edcc5120
TT
4577/* A helper function that prints a shared library stopped event. */
4578
4579static void
4580print_solib_event (int is_catchpoint)
4581{
4582 int any_deleted
4583 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4584 int any_added
4585 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4586
4587 if (!is_catchpoint)
4588 {
4589 if (any_added || any_deleted)
4590 ui_out_text (current_uiout,
4591 _("Stopped due to shared library event:\n"));
4592 else
4593 ui_out_text (current_uiout,
4594 _("Stopped due to shared library event (no "
4595 "libraries added or removed)\n"));
4596 }
4597
4598 if (ui_out_is_mi_like_p (current_uiout))
4599 ui_out_field_string (current_uiout, "reason",
4600 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4601
4602 if (any_deleted)
4603 {
4604 struct cleanup *cleanup;
4605 char *name;
4606 int ix;
4607
4608 ui_out_text (current_uiout, _(" Inferior unloaded "));
4609 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4610 "removed");
4611 for (ix = 0;
4612 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4613 ix, name);
4614 ++ix)
4615 {
4616 if (ix > 0)
4617 ui_out_text (current_uiout, " ");
4618 ui_out_field_string (current_uiout, "library", name);
4619 ui_out_text (current_uiout, "\n");
4620 }
4621
4622 do_cleanups (cleanup);
4623 }
4624
4625 if (any_added)
4626 {
4627 struct so_list *iter;
4628 int ix;
4629 struct cleanup *cleanup;
4630
4631 ui_out_text (current_uiout, _(" Inferior loaded "));
4632 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4633 "added");
4634 for (ix = 0;
4635 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4636 ix, iter);
4637 ++ix)
4638 {
4639 if (ix > 0)
4640 ui_out_text (current_uiout, " ");
4641 ui_out_field_string (current_uiout, "library", iter->so_name);
4642 ui_out_text (current_uiout, "\n");
4643 }
4644
4645 do_cleanups (cleanup);
4646 }
4647}
4648
e514a9d6
JM
4649/* Print a message indicating what happened. This is called from
4650 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4651 list - a list of the eventpoints that caused this stop. KIND is
4652 the target_waitkind for the stopping event. This
e514a9d6
JM
4653 routine calls the generic print routine for printing a message
4654 about reasons for stopping. This will print (for example) the
4655 "Breakpoint n," part of the output. The return value of this
4656 routine is one of:
c906108c 4657
4a64f543 4658 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4659 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4660 code to print the location. An example is
c5aa993b
JM
4661 "Breakpoint 1, " which should be followed by
4662 the location.
917317f4 4663 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4664 to also print the location part of the message.
4665 An example is the catch/throw messages, which
4a64f543 4666 don't require a location appended to the end.
917317f4 4667 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4668 further info to be printed. */
c906108c 4669
917317f4 4670enum print_stop_action
36dfb11c 4671bpstat_print (bpstat bs, int kind)
c906108c
SS
4672{
4673 int val;
c5aa993b 4674
c906108c 4675 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4676 (Currently all watchpoints go on the bpstat whether hit or not.
4677 That probably could (should) be changed, provided care is taken
c906108c 4678 with respect to bpstat_explains_signal). */
e514a9d6
JM
4679 for (; bs; bs = bs->next)
4680 {
4681 val = print_bp_stop_message (bs);
4682 if (val == PRINT_SRC_ONLY
4683 || val == PRINT_SRC_AND_LOC
4684 || val == PRINT_NOTHING)
4685 return val;
4686 }
c906108c 4687
36dfb11c
TT
4688 /* If we had hit a shared library event breakpoint,
4689 print_bp_stop_message would print out this message. If we hit an
4690 OS-level shared library event, do the same thing. */
4691 if (kind == TARGET_WAITKIND_LOADED)
4692 {
edcc5120 4693 print_solib_event (0);
36dfb11c
TT
4694 return PRINT_NOTHING;
4695 }
4696
e514a9d6 4697 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4698 with and nothing was printed. */
917317f4 4699 return PRINT_UNKNOWN;
c906108c
SS
4700}
4701
c42bd95a
DE
4702/* Evaluate the expression EXP and return 1 if value is zero.
4703 This returns the inverse of the condition because it is called
4704 from catch_errors which returns 0 if an exception happened, and if an
4705 exception happens we want execution to stop.
4a64f543 4706 The argument is a "struct expression *" that has been cast to a
c42bd95a 4707 "void *" to make it pass through catch_errors. */
c906108c
SS
4708
4709static int
4efb68b1 4710breakpoint_cond_eval (void *exp)
c906108c 4711{
278cd55f 4712 struct value *mark = value_mark ();
c5aa993b 4713 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4714
c906108c
SS
4715 value_free_to_mark (mark);
4716 return i;
4717}
4718
5760d0ab 4719/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4720
4721static bpstat
5760d0ab 4722bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4723{
4724 bpstat bs;
4725
4726 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4727 bs->next = NULL;
4728 **bs_link_pointer = bs;
4729 *bs_link_pointer = &bs->next;
f431efe5
PA
4730 bs->breakpoint_at = bl->owner;
4731 bs->bp_location_at = bl;
4732 incref_bp_location (bl);
c906108c
SS
4733 /* If the condition is false, etc., don't do the commands. */
4734 bs->commands = NULL;
4735 bs->old_val = NULL;
4736 bs->print_it = print_it_normal;
4737 return bs;
4738}
4739\f
d983da9c
DJ
4740/* The target has stopped with waitstatus WS. Check if any hardware
4741 watchpoints have triggered, according to the target. */
4742
4743int
4744watchpoints_triggered (struct target_waitstatus *ws)
4745{
d92524f1 4746 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4747 CORE_ADDR addr;
4748 struct breakpoint *b;
4749
4750 if (!stopped_by_watchpoint)
4751 {
4752 /* We were not stopped by a watchpoint. Mark all watchpoints
4753 as not triggered. */
4754 ALL_BREAKPOINTS (b)
cc60f2e3 4755 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4756 {
4757 struct watchpoint *w = (struct watchpoint *) b;
4758
4759 w->watchpoint_triggered = watch_triggered_no;
4760 }
d983da9c
DJ
4761
4762 return 0;
4763 }
4764
4765 if (!target_stopped_data_address (&current_target, &addr))
4766 {
4767 /* We were stopped by a watchpoint, but we don't know where.
4768 Mark all watchpoints as unknown. */
4769 ALL_BREAKPOINTS (b)
cc60f2e3 4770 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4771 {
4772 struct watchpoint *w = (struct watchpoint *) b;
4773
4774 w->watchpoint_triggered = watch_triggered_unknown;
4775 }
d983da9c 4776
3c4797ba 4777 return 1;
d983da9c
DJ
4778 }
4779
4780 /* The target could report the data address. Mark watchpoints
4781 affected by this data address as triggered, and all others as not
4782 triggered. */
4783
4784 ALL_BREAKPOINTS (b)
cc60f2e3 4785 if (is_hardware_watchpoint (b))
d983da9c 4786 {
3a5c3e22 4787 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4788 struct bp_location *loc;
d983da9c 4789
3a5c3e22 4790 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4791 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4792 {
3a5c3e22 4793 if (is_masked_watchpoint (b))
9c06b0b4 4794 {
3a5c3e22
PA
4795 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4796 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4797
4798 if (newaddr == start)
4799 {
3a5c3e22 4800 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4801 break;
4802 }
4803 }
4804 /* Exact match not required. Within range is sufficient. */
4805 else if (target_watchpoint_addr_within_range (&current_target,
4806 addr, loc->address,
4807 loc->length))
4808 {
3a5c3e22 4809 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4810 break;
4811 }
4812 }
d983da9c
DJ
4813 }
4814
4815 return 1;
4816}
4817
c906108c
SS
4818/* Possible return values for watchpoint_check (this can't be an enum
4819 because of check_errors). */
4820/* The watchpoint has been deleted. */
4821#define WP_DELETED 1
4822/* The value has changed. */
4823#define WP_VALUE_CHANGED 2
4824/* The value has not changed. */
4825#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4826/* Ignore this watchpoint, no matter if the value changed or not. */
4827#define WP_IGNORE 4
c906108c
SS
4828
4829#define BP_TEMPFLAG 1
4830#define BP_HARDWAREFLAG 2
4831
4a64f543
MS
4832/* Evaluate watchpoint condition expression and check if its value
4833 changed.
553e4c11
JB
4834
4835 P should be a pointer to struct bpstat, but is defined as a void *
4836 in order for this function to be usable with catch_errors. */
c906108c
SS
4837
4838static int
4efb68b1 4839watchpoint_check (void *p)
c906108c
SS
4840{
4841 bpstat bs = (bpstat) p;
3a5c3e22 4842 struct watchpoint *b;
c906108c
SS
4843 struct frame_info *fr;
4844 int within_current_scope;
4845
f431efe5 4846 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4847 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4848 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4849
f6bc2008
PA
4850 /* If this is a local watchpoint, we only want to check if the
4851 watchpoint frame is in scope if the current thread is the thread
4852 that was used to create the watchpoint. */
4853 if (!watchpoint_in_thread_scope (b))
60e1c644 4854 return WP_IGNORE;
f6bc2008 4855
c906108c
SS
4856 if (b->exp_valid_block == NULL)
4857 within_current_scope = 1;
4858 else
4859 {
edb3359d
DJ
4860 struct frame_info *frame = get_current_frame ();
4861 struct gdbarch *frame_arch = get_frame_arch (frame);
4862 CORE_ADDR frame_pc = get_frame_pc (frame);
4863
4a64f543
MS
4864 /* in_function_epilogue_p() returns a non-zero value if we're
4865 still in the function but the stack frame has already been
4866 invalidated. Since we can't rely on the values of local
4867 variables after the stack has been destroyed, we are treating
4868 the watchpoint in that state as `not changed' without further
4869 checking. Don't mark watchpoints as changed if the current
4870 frame is in an epilogue - even if they are in some other
4871 frame, our view of the stack is likely to be wrong and
4872 frame_find_by_id could error out. */
a0f49112 4873 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 4874 return WP_IGNORE;
a0f49112 4875
101dcfbe 4876 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4877 within_current_scope = (fr != NULL);
69fbadd5
DJ
4878
4879 /* If we've gotten confused in the unwinder, we might have
4880 returned a frame that can't describe this variable. */
edb3359d
DJ
4881 if (within_current_scope)
4882 {
4883 struct symbol *function;
4884
4885 function = get_frame_function (fr);
4886 if (function == NULL
4887 || !contained_in (b->exp_valid_block,
4888 SYMBOL_BLOCK_VALUE (function)))
4889 within_current_scope = 0;
4890 }
69fbadd5 4891
edb3359d 4892 if (within_current_scope)
c906108c
SS
4893 /* If we end up stopping, the current frame will get selected
4894 in normal_stop. So this call to select_frame won't affect
4895 the user. */
0f7d239c 4896 select_frame (fr);
c906108c 4897 }
c5aa993b 4898
c906108c
SS
4899 if (within_current_scope)
4900 {
4a64f543
MS
4901 /* We use value_{,free_to_}mark because it could be a *long*
4902 time before we return to the command level and call
4903 free_all_values. We can't call free_all_values because we
4904 might be in the middle of evaluating a function call. */
c906108c 4905
0cf6dd15 4906 int pc = 0;
9c06b0b4 4907 struct value *mark;
fa4727a6
DJ
4908 struct value *new_val;
4909
3a5c3e22 4910 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
4911 /* Since we don't know the exact trigger address (from
4912 stopped_data_address), just tell the user we've triggered
4913 a mask watchpoint. */
4914 return WP_VALUE_CHANGED;
4915
4916 mark = value_mark ();
3a1115a0 4917 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 4918
4a64f543
MS
4919 /* We use value_equal_contents instead of value_equal because
4920 the latter coerces an array to a pointer, thus comparing just
4921 the address of the array instead of its contents. This is
4922 not what we want. */
fa4727a6 4923 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 4924 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 4925 {
fa4727a6
DJ
4926 if (new_val != NULL)
4927 {
4928 release_value (new_val);
4929 value_free_to_mark (mark);
4930 }
c906108c
SS
4931 bs->old_val = b->val;
4932 b->val = new_val;
fa4727a6 4933 b->val_valid = 1;
c906108c
SS
4934 return WP_VALUE_CHANGED;
4935 }
4936 else
4937 {
60e1c644 4938 /* Nothing changed. */
c906108c 4939 value_free_to_mark (mark);
c906108c
SS
4940 return WP_VALUE_NOT_CHANGED;
4941 }
4942 }
4943 else
4944 {
79a45e25
PA
4945 struct ui_out *uiout = current_uiout;
4946
c906108c 4947 /* This seems like the only logical thing to do because
c5aa993b
JM
4948 if we temporarily ignored the watchpoint, then when
4949 we reenter the block in which it is valid it contains
4950 garbage (in the case of a function, it may have two
4951 garbage values, one before and one after the prologue).
4952 So we can't even detect the first assignment to it and
4953 watch after that (since the garbage may or may not equal
4954 the first value assigned). */
348d480f
PA
4955 /* We print all the stop information in
4956 breakpoint_ops->print_it, but in this case, by the time we
4957 call breakpoint_ops->print_it this bp will be deleted
4958 already. So we have no choice but print the information
4959 here. */
9dc5e2a9 4960 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4961 ui_out_field_string
4962 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 4963 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 4964 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
4965 ui_out_text (uiout,
4966 " deleted because the program has left the block in\n\
8b93c638 4967which its expression is valid.\n");
4ce44c66 4968
cdac0397 4969 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 4970 decref_counted_command_line (&b->base.commands);
d0fb5eae 4971 watchpoint_del_at_next_stop (b);
c906108c
SS
4972
4973 return WP_DELETED;
4974 }
4975}
4976
18a18393 4977/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4978 breakpoint location BL. This function does not check if we should
4979 stop, only if BL explains the stop. */
4980
18a18393 4981static int
6c95b8df 4982bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
4983 struct address_space *aspace, CORE_ADDR bp_addr,
4984 const struct target_waitstatus *ws)
18a18393
VP
4985{
4986 struct breakpoint *b = bl->owner;
4987
348d480f 4988 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4989 gdb_assert (b != NULL);
4990
09ac7c10 4991 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4992}
4993
3a5c3e22
PA
4994/* Determine if the watched values have actually changed, and we
4995 should stop. If not, set BS->stop to 0. */
4996
18a18393
VP
4997static void
4998bpstat_check_watchpoint (bpstat bs)
4999{
2bdf28a0 5000 const struct bp_location *bl;
3a5c3e22 5001 struct watchpoint *b;
2bdf28a0
JK
5002
5003 /* BS is built for existing struct breakpoint. */
f431efe5 5004 bl = bs->bp_location_at;
2bdf28a0 5005 gdb_assert (bl != NULL);
3a5c3e22 5006 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5007 gdb_assert (b != NULL);
18a18393 5008
18a18393 5009 {
18a18393
VP
5010 int must_check_value = 0;
5011
3a5c3e22 5012 if (b->base.type == bp_watchpoint)
18a18393
VP
5013 /* For a software watchpoint, we must always check the
5014 watched value. */
5015 must_check_value = 1;
5016 else if (b->watchpoint_triggered == watch_triggered_yes)
5017 /* We have a hardware watchpoint (read, write, or access)
5018 and the target earlier reported an address watched by
5019 this watchpoint. */
5020 must_check_value = 1;
5021 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5022 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5023 /* We were stopped by a hardware watchpoint, but the target could
5024 not report the data address. We must check the watchpoint's
5025 value. Access and read watchpoints are out of luck; without
5026 a data address, we can't figure it out. */
5027 must_check_value = 1;
3a5c3e22 5028
18a18393
VP
5029 if (must_check_value)
5030 {
3e43a32a
MS
5031 char *message
5032 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5033 b->base.number);
18a18393
VP
5034 struct cleanup *cleanups = make_cleanup (xfree, message);
5035 int e = catch_errors (watchpoint_check, bs, message,
5036 RETURN_MASK_ALL);
5037 do_cleanups (cleanups);
5038 switch (e)
5039 {
5040 case WP_DELETED:
5041 /* We've already printed what needs to be printed. */
5042 bs->print_it = print_it_done;
5043 /* Stop. */
5044 break;
60e1c644
PA
5045 case WP_IGNORE:
5046 bs->print_it = print_it_noop;
5047 bs->stop = 0;
5048 break;
18a18393 5049 case WP_VALUE_CHANGED:
3a5c3e22 5050 if (b->base.type == bp_read_watchpoint)
18a18393 5051 {
85d721b8
PA
5052 /* There are two cases to consider here:
5053
4a64f543 5054 1. We're watching the triggered memory for reads.
85d721b8
PA
5055 In that case, trust the target, and always report
5056 the watchpoint hit to the user. Even though
5057 reads don't cause value changes, the value may
5058 have changed since the last time it was read, and
5059 since we're not trapping writes, we will not see
5060 those, and as such we should ignore our notion of
5061 old value.
5062
4a64f543 5063 2. We're watching the triggered memory for both
85d721b8
PA
5064 reads and writes. There are two ways this may
5065 happen:
5066
4a64f543 5067 2.1. This is a target that can't break on data
85d721b8
PA
5068 reads only, but can break on accesses (reads or
5069 writes), such as e.g., x86. We detect this case
5070 at the time we try to insert read watchpoints.
5071
4a64f543 5072 2.2. Otherwise, the target supports read
85d721b8
PA
5073 watchpoints, but, the user set an access or write
5074 watchpoint watching the same memory as this read
5075 watchpoint.
5076
5077 If we're watching memory writes as well as reads,
5078 ignore watchpoint hits when we find that the
5079 value hasn't changed, as reads don't cause
5080 changes. This still gives false positives when
5081 the program writes the same value to memory as
5082 what there was already in memory (we will confuse
5083 it for a read), but it's much better than
5084 nothing. */
5085
5086 int other_write_watchpoint = 0;
5087
5088 if (bl->watchpoint_type == hw_read)
5089 {
5090 struct breakpoint *other_b;
5091
5092 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5093 if (other_b->type == bp_hardware_watchpoint
5094 || other_b->type == bp_access_watchpoint)
85d721b8 5095 {
3a5c3e22
PA
5096 struct watchpoint *other_w =
5097 (struct watchpoint *) other_b;
5098
5099 if (other_w->watchpoint_triggered
5100 == watch_triggered_yes)
5101 {
5102 other_write_watchpoint = 1;
5103 break;
5104 }
85d721b8
PA
5105 }
5106 }
5107
5108 if (other_write_watchpoint
5109 || bl->watchpoint_type == hw_access)
5110 {
5111 /* We're watching the same memory for writes,
5112 and the value changed since the last time we
5113 updated it, so this trap must be for a write.
5114 Ignore it. */
5115 bs->print_it = print_it_noop;
5116 bs->stop = 0;
5117 }
18a18393
VP
5118 }
5119 break;
5120 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5121 if (b->base.type == bp_hardware_watchpoint
5122 || b->base.type == bp_watchpoint)
18a18393
VP
5123 {
5124 /* Don't stop: write watchpoints shouldn't fire if
5125 the value hasn't changed. */
5126 bs->print_it = print_it_noop;
5127 bs->stop = 0;
5128 }
5129 /* Stop. */
5130 break;
5131 default:
5132 /* Can't happen. */
5133 case 0:
5134 /* Error from catch_errors. */
3a5c3e22 5135 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5136 watchpoint_del_at_next_stop (b);
18a18393
VP
5137 /* We've already printed what needs to be printed. */
5138 bs->print_it = print_it_done;
5139 break;
5140 }
5141 }
5142 else /* must_check_value == 0 */
5143 {
5144 /* This is a case where some watchpoint(s) triggered, but
5145 not at the address of this watchpoint, or else no
5146 watchpoint triggered after all. So don't print
5147 anything for this watchpoint. */
5148 bs->print_it = print_it_noop;
5149 bs->stop = 0;
5150 }
5151 }
5152}
5153
7d4df6a4
DE
5154/* For breakpoints that are currently marked as telling gdb to stop,
5155 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5156 of breakpoint referred to by BS. If we should not stop for this
5157 breakpoint, set BS->stop to 0. */
f431efe5 5158
18a18393
VP
5159static void
5160bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5161{
5162 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
5163 const struct bp_location *bl;
5164 struct breakpoint *b;
7d4df6a4
DE
5165 int value_is_zero = 0;
5166 struct expression *cond;
5167
5168 gdb_assert (bs->stop);
2bdf28a0
JK
5169
5170 /* BS is built for existing struct breakpoint. */
f431efe5 5171 bl = bs->bp_location_at;
2bdf28a0 5172 gdb_assert (bl != NULL);
f431efe5 5173 b = bs->breakpoint_at;
2bdf28a0 5174 gdb_assert (b != NULL);
18a18393 5175
b775012e
LM
5176 /* Even if the target evaluated the condition on its end and notified GDB, we
5177 need to do so again since GDB does not know if we stopped due to a
5178 breakpoint or a single step breakpoint. */
5179
18a18393 5180 if (frame_id_p (b->frame_id)
edb3359d 5181 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5182 {
7d4df6a4
DE
5183 bs->stop = 0;
5184 return;
5185 }
60e1c644 5186
6c1b0f7b
DE
5187 /* If this is a thread-specific breakpoint, don't waste cpu evaluating the
5188 condition if this isn't the specified thread. */
5189 if (b->thread != -1 && b->thread != thread_id)
5190 {
5191 bs->stop = 0;
5192 return;
5193 }
5194
6dddc817
DE
5195 /* Evaluate extension language breakpoints that have a "stop" method
5196 implemented. */
5197 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5198
7d4df6a4
DE
5199 if (is_watchpoint (b))
5200 {
5201 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5202
7d4df6a4
DE
5203 cond = w->cond_exp;
5204 }
5205 else
5206 cond = bl->cond;
60e1c644 5207
7d4df6a4
DE
5208 if (cond && b->disposition != disp_del_at_next_stop)
5209 {
5210 int within_current_scope = 1;
5211 struct watchpoint * w;
60e1c644 5212
7d4df6a4
DE
5213 /* We use value_mark and value_free_to_mark because it could
5214 be a long time before we return to the command level and
5215 call free_all_values. We can't call free_all_values
5216 because we might be in the middle of evaluating a
5217 function call. */
5218 struct value *mark = value_mark ();
5219
5220 if (is_watchpoint (b))
5221 w = (struct watchpoint *) b;
5222 else
5223 w = NULL;
5224
5225 /* Need to select the frame, with all that implies so that
5226 the conditions will have the right context. Because we
5227 use the frame, we will not see an inlined function's
5228 variables when we arrive at a breakpoint at the start
5229 of the inlined function; the current frame will be the
5230 call site. */
5231 if (w == NULL || w->cond_exp_valid_block == NULL)
5232 select_frame (get_current_frame ());
5233 else
18a18393 5234 {
7d4df6a4
DE
5235 struct frame_info *frame;
5236
5237 /* For local watchpoint expressions, which particular
5238 instance of a local is being watched matters, so we
5239 keep track of the frame to evaluate the expression
5240 in. To evaluate the condition however, it doesn't
5241 really matter which instantiation of the function
5242 where the condition makes sense triggers the
5243 watchpoint. This allows an expression like "watch
5244 global if q > 10" set in `func', catch writes to
5245 global on all threads that call `func', or catch
5246 writes on all recursive calls of `func' by a single
5247 thread. We simply always evaluate the condition in
5248 the innermost frame that's executing where it makes
5249 sense to evaluate the condition. It seems
5250 intuitive. */
5251 frame = block_innermost_frame (w->cond_exp_valid_block);
5252 if (frame != NULL)
5253 select_frame (frame);
5254 else
5255 within_current_scope = 0;
18a18393 5256 }
7d4df6a4
DE
5257 if (within_current_scope)
5258 value_is_zero
5259 = catch_errors (breakpoint_cond_eval, cond,
5260 "Error in testing breakpoint condition:\n",
5261 RETURN_MASK_ALL);
5262 else
18a18393 5263 {
7d4df6a4
DE
5264 warning (_("Watchpoint condition cannot be tested "
5265 "in the current scope"));
5266 /* If we failed to set the right context for this
5267 watchpoint, unconditionally report it. */
5268 value_is_zero = 0;
18a18393 5269 }
7d4df6a4
DE
5270 /* FIXME-someday, should give breakpoint #. */
5271 value_free_to_mark (mark);
18a18393 5272 }
7d4df6a4
DE
5273
5274 if (cond && value_is_zero)
5275 {
5276 bs->stop = 0;
5277 }
7d4df6a4
DE
5278 else if (b->ignore_count > 0)
5279 {
5280 b->ignore_count--;
5281 bs->stop = 0;
5282 /* Increase the hit count even though we don't stop. */
5283 ++(b->hit_count);
5284 observer_notify_breakpoint_modified (b);
5285 }
18a18393
VP
5286}
5287
5288
9709f61c 5289/* Get a bpstat associated with having just stopped at address
d983da9c 5290 BP_ADDR in thread PTID.
c906108c 5291
d983da9c 5292 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5293 don't understand this stop. Result is a chain of bpstat's such
5294 that:
c906108c 5295
c5aa993b 5296 if we don't understand the stop, the result is a null pointer.
c906108c 5297
c5aa993b 5298 if we understand why we stopped, the result is not null.
c906108c 5299
c5aa993b
JM
5300 Each element of the chain refers to a particular breakpoint or
5301 watchpoint at which we have stopped. (We may have stopped for
5302 several reasons concurrently.)
c906108c 5303
c5aa993b
JM
5304 Each element of the chain has valid next, breakpoint_at,
5305 commands, FIXME??? fields. */
c906108c
SS
5306
5307bpstat
6c95b8df 5308bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5309 CORE_ADDR bp_addr, ptid_t ptid,
5310 const struct target_waitstatus *ws)
c906108c 5311{
0d381245 5312 struct breakpoint *b = NULL;
afe38095 5313 struct bp_location *bl;
20874c92 5314 struct bp_location *loc;
5760d0ab
JK
5315 /* First item of allocated bpstat's. */
5316 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5317 /* Pointer to the last thing in the chain currently. */
5760d0ab 5318 bpstat bs;
20874c92 5319 int ix;
429374b8 5320 int need_remove_insert;
f431efe5 5321 int removed_any;
c906108c 5322
f431efe5
PA
5323 /* First, build the bpstat chain with locations that explain a
5324 target stop, while being careful to not set the target running,
5325 as that may invalidate locations (in particular watchpoint
5326 locations are recreated). Resuming will happen here with
5327 breakpoint conditions or watchpoint expressions that include
5328 inferior function calls. */
c5aa993b 5329
429374b8
JK
5330 ALL_BREAKPOINTS (b)
5331 {
5332 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5333 continue;
a5606eee 5334
429374b8
JK
5335 for (bl = b->loc; bl != NULL; bl = bl->next)
5336 {
4a64f543
MS
5337 /* For hardware watchpoints, we look only at the first
5338 location. The watchpoint_check function will work on the
5339 entire expression, not the individual locations. For
5340 read watchpoints, the watchpoints_triggered function has
5341 checked all locations already. */
429374b8
JK
5342 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5343 break;
18a18393 5344
f6592439 5345 if (!bl->enabled || bl->shlib_disabled)
429374b8 5346 continue;
c5aa993b 5347
09ac7c10 5348 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5349 continue;
c5aa993b 5350
4a64f543
MS
5351 /* Come here if it's a watchpoint, or if the break address
5352 matches. */
c5aa993b 5353
4a64f543
MS
5354 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5355 explain stop. */
c5aa993b 5356
f431efe5
PA
5357 /* Assume we stop. Should we find a watchpoint that is not
5358 actually triggered, or if the condition of the breakpoint
5359 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5360 bs->stop = 1;
5361 bs->print = 1;
d983da9c 5362
f431efe5
PA
5363 /* If this is a scope breakpoint, mark the associated
5364 watchpoint as triggered so that we will handle the
5365 out-of-scope event. We'll get to the watchpoint next
5366 iteration. */
d0fb5eae 5367 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5368 {
5369 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5370
5371 w->watchpoint_triggered = watch_triggered_yes;
5372 }
f431efe5
PA
5373 }
5374 }
5375
5376 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5377 {
f1310107 5378 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5379 {
5760d0ab 5380 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5381 /* For hits of moribund locations, we should just proceed. */
5382 bs->stop = 0;
5383 bs->print = 0;
5384 bs->print_it = print_it_noop;
5385 }
5386 }
5387
edcc5120
TT
5388 /* A bit of special processing for shlib breakpoints. We need to
5389 process solib loading here, so that the lists of loaded and
5390 unloaded libraries are correct before we handle "catch load" and
5391 "catch unload". */
5392 for (bs = bs_head; bs != NULL; bs = bs->next)
5393 {
5d268276 5394 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5395 {
5396 handle_solib_event ();
5397 break;
5398 }
5399 }
5400
f431efe5
PA
5401 /* Now go through the locations that caused the target to stop, and
5402 check whether we're interested in reporting this stop to higher
5403 layers, or whether we should resume the target transparently. */
5404
5405 removed_any = 0;
5406
5760d0ab 5407 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5408 {
5409 if (!bs->stop)
5410 continue;
5411
f431efe5 5412 b = bs->breakpoint_at;
348d480f
PA
5413 b->ops->check_status (bs);
5414 if (bs->stop)
28010a5d 5415 {
348d480f 5416 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5417
429374b8
JK
5418 if (bs->stop)
5419 {
5420 ++(b->hit_count);
8d3788bd 5421 observer_notify_breakpoint_modified (b);
c906108c 5422
4a64f543 5423 /* We will stop here. */
429374b8
JK
5424 if (b->disposition == disp_disable)
5425 {
816338b5
SS
5426 --(b->enable_count);
5427 if (b->enable_count <= 0
5428 && b->enable_state != bp_permanent)
429374b8 5429 b->enable_state = bp_disabled;
f431efe5 5430 removed_any = 1;
429374b8
JK
5431 }
5432 if (b->silent)
5433 bs->print = 0;
5434 bs->commands = b->commands;
9add0f1b 5435 incref_counted_command_line (bs->commands);
abf85f46
JK
5436 if (command_line_is_silent (bs->commands
5437 ? bs->commands->commands : NULL))
5438 bs->print = 0;
9d6e6e84
HZ
5439
5440 b->ops->after_condition_true (bs);
429374b8
JK
5441 }
5442
348d480f 5443 }
a9b3a50f
PA
5444
5445 /* Print nothing for this entry if we don't stop or don't
5446 print. */
5447 if (!bs->stop || !bs->print)
5448 bs->print_it = print_it_noop;
429374b8 5449 }
876fa593 5450
d983da9c
DJ
5451 /* If we aren't stopping, the value of some hardware watchpoint may
5452 not have changed, but the intermediate memory locations we are
5453 watching may have. Don't bother if we're stopping; this will get
5454 done later. */
d832cb68 5455 need_remove_insert = 0;
5760d0ab
JK
5456 if (! bpstat_causes_stop (bs_head))
5457 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5458 if (!bs->stop
f431efe5
PA
5459 && bs->breakpoint_at
5460 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5461 {
3a5c3e22
PA
5462 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5463
5464 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5465 need_remove_insert = 1;
d983da9c
DJ
5466 }
5467
d832cb68 5468 if (need_remove_insert)
2d134ed3 5469 update_global_location_list (1);
f431efe5
PA
5470 else if (removed_any)
5471 update_global_location_list (0);
d832cb68 5472
5760d0ab 5473 return bs_head;
c906108c 5474}
628fe4e4
JK
5475
5476static void
5477handle_jit_event (void)
5478{
5479 struct frame_info *frame;
5480 struct gdbarch *gdbarch;
5481
5482 /* Switch terminal for any messages produced by
5483 breakpoint_re_set. */
5484 target_terminal_ours_for_output ();
5485
5486 frame = get_current_frame ();
5487 gdbarch = get_frame_arch (frame);
5488
5489 jit_event_handler (gdbarch);
5490
5491 target_terminal_inferior ();
5492}
5493
5494/* Prepare WHAT final decision for infrun. */
5495
5496/* Decide what infrun needs to do with this bpstat. */
5497
c906108c 5498struct bpstat_what
0e30163f 5499bpstat_what (bpstat bs_head)
c906108c 5500{
c906108c 5501 struct bpstat_what retval;
628fe4e4 5502 int jit_event = 0;
0e30163f 5503 bpstat bs;
c906108c 5504
628fe4e4 5505 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5506 retval.call_dummy = STOP_NONE;
186c406b 5507 retval.is_longjmp = 0;
628fe4e4 5508
0e30163f 5509 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5510 {
628fe4e4
JK
5511 /* Extract this BS's action. After processing each BS, we check
5512 if its action overrides all we've seem so far. */
5513 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5514 enum bptype bptype;
5515
c906108c 5516 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5517 {
5518 /* I suspect this can happen if it was a momentary
5519 breakpoint which has since been deleted. */
5520 bptype = bp_none;
5521 }
20874c92 5522 else
f431efe5 5523 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5524
5525 switch (bptype)
c906108c
SS
5526 {
5527 case bp_none:
628fe4e4 5528 break;
c906108c
SS
5529 case bp_breakpoint:
5530 case bp_hardware_breakpoint:
5531 case bp_until:
5532 case bp_finish:
a9b3a50f 5533 case bp_shlib_event:
c906108c
SS
5534 if (bs->stop)
5535 {
5536 if (bs->print)
628fe4e4 5537 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5538 else
628fe4e4 5539 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5540 }
5541 else
628fe4e4 5542 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5543 break;
5544 case bp_watchpoint:
5545 case bp_hardware_watchpoint:
5546 case bp_read_watchpoint:
5547 case bp_access_watchpoint:
5548 if (bs->stop)
5549 {
5550 if (bs->print)
628fe4e4 5551 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5552 else
628fe4e4 5553 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5554 }
5555 else
628fe4e4
JK
5556 {
5557 /* There was a watchpoint, but we're not stopping.
5558 This requires no further action. */
5559 }
c906108c
SS
5560 break;
5561 case bp_longjmp:
e2e4d78b 5562 case bp_longjmp_call_dummy:
186c406b 5563 case bp_exception:
628fe4e4 5564 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5565 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5566 break;
5567 case bp_longjmp_resume:
186c406b 5568 case bp_exception_resume:
628fe4e4 5569 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5570 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5571 break;
5572 case bp_step_resume:
5573 if (bs->stop)
628fe4e4
JK
5574 this_action = BPSTAT_WHAT_STEP_RESUME;
5575 else
c906108c 5576 {
628fe4e4
JK
5577 /* It is for the wrong frame. */
5578 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5579 }
c906108c 5580 break;
2c03e5be
PA
5581 case bp_hp_step_resume:
5582 if (bs->stop)
5583 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5584 else
5585 {
5586 /* It is for the wrong frame. */
5587 this_action = BPSTAT_WHAT_SINGLE;
5588 }
5589 break;
c906108c 5590 case bp_watchpoint_scope:
c4093a6a 5591 case bp_thread_event:
1900040c 5592 case bp_overlay_event:
0fd8e87f 5593 case bp_longjmp_master:
aa7d318d 5594 case bp_std_terminate_master:
186c406b 5595 case bp_exception_master:
628fe4e4 5596 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5597 break;
ce78b96d 5598 case bp_catchpoint:
c5aa993b
JM
5599 if (bs->stop)
5600 {
5601 if (bs->print)
628fe4e4 5602 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5603 else
628fe4e4 5604 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5605 }
5606 else
628fe4e4
JK
5607 {
5608 /* There was a catchpoint, but we're not stopping.
5609 This requires no further action. */
5610 }
5611 break;
628fe4e4
JK
5612 case bp_jit_event:
5613 jit_event = 1;
5614 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5615 break;
c906108c 5616 case bp_call_dummy:
53a5351d
JM
5617 /* Make sure the action is stop (silent or noisy),
5618 so infrun.c pops the dummy frame. */
aa7d318d 5619 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5620 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5621 break;
5622 case bp_std_terminate:
5623 /* Make sure the action is stop (silent or noisy),
5624 so infrun.c pops the dummy frame. */
aa7d318d 5625 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5626 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5627 break;
1042e4c0 5628 case bp_tracepoint:
7a697b8d 5629 case bp_fast_tracepoint:
0fb4aa4b 5630 case bp_static_tracepoint:
1042e4c0
SS
5631 /* Tracepoint hits should not be reported back to GDB, and
5632 if one got through somehow, it should have been filtered
5633 out already. */
5634 internal_error (__FILE__, __LINE__,
7a697b8d 5635 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5636 break;
5637 case bp_gnu_ifunc_resolver:
5638 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5639 this_action = BPSTAT_WHAT_SINGLE;
5640 break;
5641 case bp_gnu_ifunc_resolver_return:
5642 /* The breakpoint will be removed, execution will restart from the
5643 PC of the former breakpoint. */
5644 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5645 break;
e7e0cddf
SS
5646
5647 case bp_dprintf:
a11cfd87
HZ
5648 if (bs->stop)
5649 this_action = BPSTAT_WHAT_STOP_SILENT;
5650 else
5651 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5652 break;
5653
628fe4e4
JK
5654 default:
5655 internal_error (__FILE__, __LINE__,
5656 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5657 }
628fe4e4
JK
5658
5659 retval.main_action = max (retval.main_action, this_action);
c906108c 5660 }
628fe4e4 5661
0e30163f
JK
5662 /* These operations may affect the bs->breakpoint_at state so they are
5663 delayed after MAIN_ACTION is decided above. */
5664
628fe4e4
JK
5665 if (jit_event)
5666 {
5667 if (debug_infrun)
5668 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5669
5670 handle_jit_event ();
5671 }
5672
0e30163f
JK
5673 for (bs = bs_head; bs != NULL; bs = bs->next)
5674 {
5675 struct breakpoint *b = bs->breakpoint_at;
5676
5677 if (b == NULL)
5678 continue;
5679 switch (b->type)
5680 {
5681 case bp_gnu_ifunc_resolver:
5682 gnu_ifunc_resolver_stop (b);
5683 break;
5684 case bp_gnu_ifunc_resolver_return:
5685 gnu_ifunc_resolver_return_stop (b);
5686 break;
5687 }
5688 }
5689
c906108c
SS
5690 return retval;
5691}
5692
5693/* Nonzero if we should step constantly (e.g. watchpoints on machines
5694 without hardware support). This isn't related to a specific bpstat,
5695 just to things like whether watchpoints are set. */
5696
c5aa993b 5697int
fba45db2 5698bpstat_should_step (void)
c906108c
SS
5699{
5700 struct breakpoint *b;
cc59ec59 5701
c906108c 5702 ALL_BREAKPOINTS (b)
717a8278 5703 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5704 return 1;
c906108c
SS
5705 return 0;
5706}
5707
67822962
PA
5708int
5709bpstat_causes_stop (bpstat bs)
5710{
5711 for (; bs != NULL; bs = bs->next)
5712 if (bs->stop)
5713 return 1;
5714
5715 return 0;
5716}
5717
c906108c 5718\f
c5aa993b 5719
170b53b2
UW
5720/* Compute a string of spaces suitable to indent the next line
5721 so it starts at the position corresponding to the table column
5722 named COL_NAME in the currently active table of UIOUT. */
5723
5724static char *
5725wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5726{
5727 static char wrap_indent[80];
5728 int i, total_width, width, align;
5729 char *text;
5730
5731 total_width = 0;
5732 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5733 {
5734 if (strcmp (text, col_name) == 0)
5735 {
5736 gdb_assert (total_width < sizeof wrap_indent);
5737 memset (wrap_indent, ' ', total_width);
5738 wrap_indent[total_width] = 0;
5739
5740 return wrap_indent;
5741 }
5742
5743 total_width += width + 1;
5744 }
5745
5746 return NULL;
5747}
5748
b775012e
LM
5749/* Determine if the locations of this breakpoint will have their conditions
5750 evaluated by the target, host or a mix of both. Returns the following:
5751
5752 "host": Host evals condition.
5753 "host or target": Host or Target evals condition.
5754 "target": Target evals condition.
5755*/
5756
5757static const char *
5758bp_condition_evaluator (struct breakpoint *b)
5759{
5760 struct bp_location *bl;
5761 char host_evals = 0;
5762 char target_evals = 0;
5763
5764 if (!b)
5765 return NULL;
5766
5767 if (!is_breakpoint (b))
5768 return NULL;
5769
5770 if (gdb_evaluates_breakpoint_condition_p ()
5771 || !target_supports_evaluation_of_breakpoint_conditions ())
5772 return condition_evaluation_host;
5773
5774 for (bl = b->loc; bl; bl = bl->next)
5775 {
5776 if (bl->cond_bytecode)
5777 target_evals++;
5778 else
5779 host_evals++;
5780 }
5781
5782 if (host_evals && target_evals)
5783 return condition_evaluation_both;
5784 else if (target_evals)
5785 return condition_evaluation_target;
5786 else
5787 return condition_evaluation_host;
5788}
5789
5790/* Determine the breakpoint location's condition evaluator. This is
5791 similar to bp_condition_evaluator, but for locations. */
5792
5793static const char *
5794bp_location_condition_evaluator (struct bp_location *bl)
5795{
5796 if (bl && !is_breakpoint (bl->owner))
5797 return NULL;
5798
5799 if (gdb_evaluates_breakpoint_condition_p ()
5800 || !target_supports_evaluation_of_breakpoint_conditions ())
5801 return condition_evaluation_host;
5802
5803 if (bl && bl->cond_bytecode)
5804 return condition_evaluation_target;
5805 else
5806 return condition_evaluation_host;
5807}
5808
859825b8
JK
5809/* Print the LOC location out of the list of B->LOC locations. */
5810
170b53b2
UW
5811static void
5812print_breakpoint_location (struct breakpoint *b,
5813 struct bp_location *loc)
0d381245 5814{
79a45e25 5815 struct ui_out *uiout = current_uiout;
6c95b8df
PA
5816 struct cleanup *old_chain = save_current_program_space ();
5817
859825b8
JK
5818 if (loc != NULL && loc->shlib_disabled)
5819 loc = NULL;
5820
6c95b8df
PA
5821 if (loc != NULL)
5822 set_current_program_space (loc->pspace);
5823
56435ebe
TT
5824 if (b->display_canonical)
5825 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 5826 else if (loc && loc->symtab)
0d381245
VP
5827 {
5828 struct symbol *sym
5829 = find_pc_sect_function (loc->address, loc->section);
5830 if (sym)
5831 {
5832 ui_out_text (uiout, "in ");
5833 ui_out_field_string (uiout, "func",
5834 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
5835 ui_out_text (uiout, " ");
5836 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5837 ui_out_text (uiout, "at ");
0d381245 5838 }
05cba821
JK
5839 ui_out_field_string (uiout, "file",
5840 symtab_to_filename_for_display (loc->symtab));
0d381245 5841 ui_out_text (uiout, ":");
05cba821 5842
0d381245 5843 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
5844 ui_out_field_string (uiout, "fullname",
5845 symtab_to_fullname (loc->symtab));
0d381245 5846
f8eba3c6 5847 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 5848 }
859825b8 5849 else if (loc)
0d381245 5850 {
f99d8bf4
PA
5851 struct ui_file *stb = mem_fileopen ();
5852 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 5853
f99d8bf4 5854 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 5855 demangle, "");
0d381245 5856 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
5857
5858 do_cleanups (stb_chain);
0d381245 5859 }
859825b8
JK
5860 else
5861 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 5862
b775012e
LM
5863 if (loc && is_breakpoint (b)
5864 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5865 && bp_condition_evaluator (b) == condition_evaluation_both)
5866 {
5867 ui_out_text (uiout, " (");
5868 ui_out_field_string (uiout, "evaluated-by",
5869 bp_location_condition_evaluator (loc));
5870 ui_out_text (uiout, ")");
5871 }
5872
6c95b8df 5873 do_cleanups (old_chain);
0d381245
VP
5874}
5875
269b11a2
PA
5876static const char *
5877bptype_string (enum bptype type)
c906108c 5878{
c4093a6a
JM
5879 struct ep_type_description
5880 {
5881 enum bptype type;
5882 char *description;
5883 };
5884 static struct ep_type_description bptypes[] =
c906108c 5885 {
c5aa993b
JM
5886 {bp_none, "?deleted?"},
5887 {bp_breakpoint, "breakpoint"},
c906108c 5888 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
5889 {bp_until, "until"},
5890 {bp_finish, "finish"},
5891 {bp_watchpoint, "watchpoint"},
c906108c 5892 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5893 {bp_read_watchpoint, "read watchpoint"},
5894 {bp_access_watchpoint, "acc watchpoint"},
5895 {bp_longjmp, "longjmp"},
5896 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5897 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5898 {bp_exception, "exception"},
5899 {bp_exception_resume, "exception resume"},
c5aa993b 5900 {bp_step_resume, "step resume"},
2c03e5be 5901 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5902 {bp_watchpoint_scope, "watchpoint scope"},
5903 {bp_call_dummy, "call dummy"},
aa7d318d 5904 {bp_std_terminate, "std::terminate"},
c5aa993b 5905 {bp_shlib_event, "shlib events"},
c4093a6a 5906 {bp_thread_event, "thread events"},
1900040c 5907 {bp_overlay_event, "overlay events"},
0fd8e87f 5908 {bp_longjmp_master, "longjmp master"},
aa7d318d 5909 {bp_std_terminate_master, "std::terminate master"},
186c406b 5910 {bp_exception_master, "exception master"},
ce78b96d 5911 {bp_catchpoint, "catchpoint"},
1042e4c0 5912 {bp_tracepoint, "tracepoint"},
7a697b8d 5913 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5914 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5915 {bp_dprintf, "dprintf"},
4efc6507 5916 {bp_jit_event, "jit events"},
0e30163f
JK
5917 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5918 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5919 };
269b11a2
PA
5920
5921 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5922 || ((int) type != bptypes[(int) type].type))
5923 internal_error (__FILE__, __LINE__,
5924 _("bptypes table does not describe type #%d."),
5925 (int) type);
5926
5927 return bptypes[(int) type].description;
5928}
5929
998580f1
MK
5930/* For MI, output a field named 'thread-groups' with a list as the value.
5931 For CLI, prefix the list with the string 'inf'. */
5932
5933static void
5934output_thread_groups (struct ui_out *uiout,
5935 const char *field_name,
5936 VEC(int) *inf_num,
5937 int mi_only)
5938{
752eb8b4 5939 struct cleanup *back_to;
998580f1
MK
5940 int is_mi = ui_out_is_mi_like_p (uiout);
5941 int inf;
5942 int i;
5943
5944 /* For backward compatibility, don't display inferiors in CLI unless
5945 there are several. Always display them for MI. */
5946 if (!is_mi && mi_only)
5947 return;
5948
752eb8b4
TT
5949 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
5950
998580f1
MK
5951 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
5952 {
5953 if (is_mi)
5954 {
5955 char mi_group[10];
5956
5957 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
5958 ui_out_field_string (uiout, NULL, mi_group);
5959 }
5960 else
5961 {
5962 if (i == 0)
5963 ui_out_text (uiout, " inf ");
5964 else
5965 ui_out_text (uiout, ", ");
5966
5967 ui_out_text (uiout, plongest (inf));
5968 }
5969 }
5970
5971 do_cleanups (back_to);
5972}
5973
269b11a2
PA
5974/* Print B to gdb_stdout. */
5975
5976static void
5977print_one_breakpoint_location (struct breakpoint *b,
5978 struct bp_location *loc,
5979 int loc_number,
5980 struct bp_location **last_loc,
269b11a2
PA
5981 int allflag)
5982{
5983 struct command_line *l;
c2c6d25f 5984 static char bpenables[] = "nynny";
c906108c 5985
79a45e25 5986 struct ui_out *uiout = current_uiout;
0d381245
VP
5987 int header_of_multiple = 0;
5988 int part_of_multiple = (loc != NULL);
79a45b7d
TT
5989 struct value_print_options opts;
5990
5991 get_user_print_options (&opts);
0d381245
VP
5992
5993 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
5994 /* See comment in print_one_breakpoint concerning treatment of
5995 breakpoints with single disabled location. */
0d381245
VP
5996 if (loc == NULL
5997 && (b->loc != NULL
5998 && (b->loc->next != NULL || !b->loc->enabled)))
5999 header_of_multiple = 1;
6000 if (loc == NULL)
6001 loc = b->loc;
6002
c4093a6a
JM
6003 annotate_record ();
6004
6005 /* 1 */
6006 annotate_field (0);
0d381245
VP
6007 if (part_of_multiple)
6008 {
6009 char *formatted;
0c6773c1 6010 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6011 ui_out_field_string (uiout, "number", formatted);
6012 xfree (formatted);
6013 }
6014 else
6015 {
6016 ui_out_field_int (uiout, "number", b->number);
6017 }
c4093a6a
JM
6018
6019 /* 2 */
6020 annotate_field (1);
0d381245
VP
6021 if (part_of_multiple)
6022 ui_out_field_skip (uiout, "type");
269b11a2
PA
6023 else
6024 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6025
6026 /* 3 */
6027 annotate_field (2);
0d381245
VP
6028 if (part_of_multiple)
6029 ui_out_field_skip (uiout, "disp");
6030 else
2cec12e5 6031 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6032
c4093a6a
JM
6033
6034 /* 4 */
6035 annotate_field (3);
0d381245 6036 if (part_of_multiple)
54e52265 6037 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6038 else
4a64f543
MS
6039 ui_out_field_fmt (uiout, "enabled", "%c",
6040 bpenables[(int) b->enable_state]);
54e52265 6041 ui_out_spaces (uiout, 2);
0d381245 6042
c4093a6a
JM
6043
6044 /* 5 and 6 */
3086aeae 6045 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6046 {
4a64f543
MS
6047 /* Although the print_one can possibly print all locations,
6048 calling it here is not likely to get any nice result. So,
6049 make sure there's just one location. */
0d381245 6050 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6051 b->ops->print_one (b, last_loc);
0d381245 6052 }
3086aeae
DJ
6053 else
6054 switch (b->type)
6055 {
6056 case bp_none:
6057 internal_error (__FILE__, __LINE__,
e2e0b3e5 6058 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6059 break;
c906108c 6060
3086aeae
DJ
6061 case bp_watchpoint:
6062 case bp_hardware_watchpoint:
6063 case bp_read_watchpoint:
6064 case bp_access_watchpoint:
3a5c3e22
PA
6065 {
6066 struct watchpoint *w = (struct watchpoint *) b;
6067
6068 /* Field 4, the address, is omitted (which makes the columns
6069 not line up too nicely with the headers, but the effect
6070 is relatively readable). */
6071 if (opts.addressprint)
6072 ui_out_field_skip (uiout, "addr");
6073 annotate_field (5);
6074 ui_out_field_string (uiout, "what", w->exp_string);
6075 }
3086aeae
DJ
6076 break;
6077
3086aeae
DJ
6078 case bp_breakpoint:
6079 case bp_hardware_breakpoint:
6080 case bp_until:
6081 case bp_finish:
6082 case bp_longjmp:
6083 case bp_longjmp_resume:
e2e4d78b 6084 case bp_longjmp_call_dummy:
186c406b
TT
6085 case bp_exception:
6086 case bp_exception_resume:
3086aeae 6087 case bp_step_resume:
2c03e5be 6088 case bp_hp_step_resume:
3086aeae
DJ
6089 case bp_watchpoint_scope:
6090 case bp_call_dummy:
aa7d318d 6091 case bp_std_terminate:
3086aeae
DJ
6092 case bp_shlib_event:
6093 case bp_thread_event:
6094 case bp_overlay_event:
0fd8e87f 6095 case bp_longjmp_master:
aa7d318d 6096 case bp_std_terminate_master:
186c406b 6097 case bp_exception_master:
1042e4c0 6098 case bp_tracepoint:
7a697b8d 6099 case bp_fast_tracepoint:
0fb4aa4b 6100 case bp_static_tracepoint:
e7e0cddf 6101 case bp_dprintf:
4efc6507 6102 case bp_jit_event:
0e30163f
JK
6103 case bp_gnu_ifunc_resolver:
6104 case bp_gnu_ifunc_resolver_return:
79a45b7d 6105 if (opts.addressprint)
3086aeae
DJ
6106 {
6107 annotate_field (4);
54e52265 6108 if (header_of_multiple)
0d381245 6109 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6110 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6111 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6112 else
5af949e3
UW
6113 ui_out_field_core_addr (uiout, "addr",
6114 loc->gdbarch, loc->address);
3086aeae
DJ
6115 }
6116 annotate_field (5);
0d381245 6117 if (!header_of_multiple)
170b53b2 6118 print_breakpoint_location (b, loc);
0d381245 6119 if (b->loc)
a6d9a66e 6120 *last_loc = b->loc;
3086aeae
DJ
6121 break;
6122 }
c906108c 6123
6c95b8df 6124
998580f1 6125 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6126 {
6127 struct inferior *inf;
998580f1
MK
6128 VEC(int) *inf_num = NULL;
6129 int mi_only = 1;
6c95b8df 6130
998580f1 6131 ALL_INFERIORS (inf)
6c95b8df
PA
6132 {
6133 if (inf->pspace == loc->pspace)
998580f1 6134 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6135 }
998580f1
MK
6136
6137 /* For backward compatibility, don't display inferiors in CLI unless
6138 there are several. Always display for MI. */
6139 if (allflag
6140 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6141 && (number_of_program_spaces () > 1
6142 || number_of_inferiors () > 1)
6143 /* LOC is for existing B, it cannot be in
6144 moribund_locations and thus having NULL OWNER. */
6145 && loc->owner->type != bp_catchpoint))
6146 mi_only = 0;
6147 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6148 VEC_free (int, inf_num);
6c95b8df
PA
6149 }
6150
4a306c9a 6151 if (!part_of_multiple)
c4093a6a 6152 {
4a306c9a
JB
6153 if (b->thread != -1)
6154 {
6155 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6156 "stop only in" line a little further down. */
4a306c9a
JB
6157 ui_out_text (uiout, " thread ");
6158 ui_out_field_int (uiout, "thread", b->thread);
6159 }
6160 else if (b->task != 0)
6161 {
6162 ui_out_text (uiout, " task ");
6163 ui_out_field_int (uiout, "task", b->task);
6164 }
c4093a6a 6165 }
f1310107 6166
8b93c638 6167 ui_out_text (uiout, "\n");
f1310107 6168
348d480f 6169 if (!part_of_multiple)
f1310107
TJB
6170 b->ops->print_one_detail (b, uiout);
6171
0d381245 6172 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6173 {
6174 annotate_field (6);
8b93c638 6175 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6176 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6177 the frame ID. */
5af949e3
UW
6178 ui_out_field_core_addr (uiout, "frame",
6179 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6180 ui_out_text (uiout, "\n");
c4093a6a
JM
6181 }
6182
28010a5d 6183 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6184 {
6185 annotate_field (7);
d77f58be 6186 if (is_tracepoint (b))
1042e4c0
SS
6187 ui_out_text (uiout, "\ttrace only if ");
6188 else
6189 ui_out_text (uiout, "\tstop only if ");
0101ce28 6190 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6191
6192 /* Print whether the target is doing the breakpoint's condition
6193 evaluation. If GDB is doing the evaluation, don't print anything. */
6194 if (is_breakpoint (b)
6195 && breakpoint_condition_evaluation_mode ()
6196 == condition_evaluation_target)
6197 {
6198 ui_out_text (uiout, " (");
6199 ui_out_field_string (uiout, "evaluated-by",
6200 bp_condition_evaluator (b));
6201 ui_out_text (uiout, " evals)");
6202 }
0101ce28
JJ
6203 ui_out_text (uiout, "\n");
6204 }
6205
0d381245 6206 if (!part_of_multiple && b->thread != -1)
c4093a6a 6207 {
4a64f543 6208 /* FIXME should make an annotation for this. */
8b93c638
JM
6209 ui_out_text (uiout, "\tstop only in thread ");
6210 ui_out_field_int (uiout, "thread", b->thread);
6211 ui_out_text (uiout, "\n");
c4093a6a
JM
6212 }
6213
556ec64d
YQ
6214 if (!part_of_multiple)
6215 {
6216 if (b->hit_count)
31f56a27
YQ
6217 {
6218 /* FIXME should make an annotation for this. */
6219 if (is_catchpoint (b))
6220 ui_out_text (uiout, "\tcatchpoint");
6221 else if (is_tracepoint (b))
6222 ui_out_text (uiout, "\ttracepoint");
6223 else
6224 ui_out_text (uiout, "\tbreakpoint");
6225 ui_out_text (uiout, " already hit ");
6226 ui_out_field_int (uiout, "times", b->hit_count);
6227 if (b->hit_count == 1)
6228 ui_out_text (uiout, " time\n");
6229 else
6230 ui_out_text (uiout, " times\n");
6231 }
556ec64d
YQ
6232 else
6233 {
31f56a27
YQ
6234 /* Output the count also if it is zero, but only if this is mi. */
6235 if (ui_out_is_mi_like_p (uiout))
6236 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6237 }
6238 }
8b93c638 6239
0d381245 6240 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6241 {
6242 annotate_field (8);
8b93c638
JM
6243 ui_out_text (uiout, "\tignore next ");
6244 ui_out_field_int (uiout, "ignore", b->ignore_count);
6245 ui_out_text (uiout, " hits\n");
c4093a6a 6246 }
059fb39f 6247
816338b5
SS
6248 /* Note that an enable count of 1 corresponds to "enable once"
6249 behavior, which is reported by the combination of enablement and
6250 disposition, so we don't need to mention it here. */
6251 if (!part_of_multiple && b->enable_count > 1)
6252 {
6253 annotate_field (8);
6254 ui_out_text (uiout, "\tdisable after ");
6255 /* Tweak the wording to clarify that ignore and enable counts
6256 are distinct, and have additive effect. */
6257 if (b->ignore_count)
6258 ui_out_text (uiout, "additional ");
6259 else
6260 ui_out_text (uiout, "next ");
6261 ui_out_field_int (uiout, "enable", b->enable_count);
6262 ui_out_text (uiout, " hits\n");
6263 }
6264
f196051f
SS
6265 if (!part_of_multiple && is_tracepoint (b))
6266 {
6267 struct tracepoint *tp = (struct tracepoint *) b;
6268
6269 if (tp->traceframe_usage)
6270 {
6271 ui_out_text (uiout, "\ttrace buffer usage ");
6272 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6273 ui_out_text (uiout, " bytes\n");
6274 }
6275 }
d3ce09f5 6276
9add0f1b 6277 l = b->commands ? b->commands->commands : NULL;
059fb39f 6278 if (!part_of_multiple && l)
c4093a6a 6279 {
3b31d625
EZ
6280 struct cleanup *script_chain;
6281
c4093a6a 6282 annotate_field (9);
3b31d625 6283 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6284 print_command_lines (uiout, l, 4);
3b31d625 6285 do_cleanups (script_chain);
c4093a6a 6286 }
d24317b4 6287
d9b3f62e 6288 if (is_tracepoint (b))
1042e4c0 6289 {
d9b3f62e
PA
6290 struct tracepoint *t = (struct tracepoint *) b;
6291
6292 if (!part_of_multiple && t->pass_count)
6293 {
6294 annotate_field (10);
6295 ui_out_text (uiout, "\tpass count ");
6296 ui_out_field_int (uiout, "pass", t->pass_count);
6297 ui_out_text (uiout, " \n");
6298 }
f2a8bc8a
YQ
6299
6300 /* Don't display it when tracepoint or tracepoint location is
6301 pending. */
6302 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6303 {
6304 annotate_field (11);
6305
6306 if (ui_out_is_mi_like_p (uiout))
6307 ui_out_field_string (uiout, "installed",
6308 loc->inserted ? "y" : "n");
6309 else
6310 {
6311 if (loc->inserted)
6312 ui_out_text (uiout, "\t");
6313 else
6314 ui_out_text (uiout, "\tnot ");
6315 ui_out_text (uiout, "installed on target\n");
6316 }
6317 }
1042e4c0
SS
6318 }
6319
d24317b4
VP
6320 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6321 {
3a5c3e22
PA
6322 if (is_watchpoint (b))
6323 {
6324 struct watchpoint *w = (struct watchpoint *) b;
6325
6326 ui_out_field_string (uiout, "original-location", w->exp_string);
6327 }
6328 else if (b->addr_string)
d24317b4 6329 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6330 }
c4093a6a 6331}
c5aa993b 6332
0d381245
VP
6333static void
6334print_one_breakpoint (struct breakpoint *b,
4a64f543 6335 struct bp_location **last_loc,
6c95b8df 6336 int allflag)
0d381245 6337{
8d3788bd 6338 struct cleanup *bkpt_chain;
79a45e25 6339 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6340
6341 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6342
12c5a436 6343 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6344 do_cleanups (bkpt_chain);
0d381245
VP
6345
6346 /* If this breakpoint has custom print function,
6347 it's already printed. Otherwise, print individual
6348 locations, if any. */
6349 if (b->ops == NULL || b->ops->print_one == NULL)
6350 {
4a64f543
MS
6351 /* If breakpoint has a single location that is disabled, we
6352 print it as if it had several locations, since otherwise it's
6353 hard to represent "breakpoint enabled, location disabled"
6354 situation.
6355
6356 Note that while hardware watchpoints have several locations
a3be7890 6357 internally, that's not a property exposed to user. */
0d381245 6358 if (b->loc
a5606eee 6359 && !is_hardware_watchpoint (b)
8d3788bd 6360 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6361 {
6362 struct bp_location *loc;
6363 int n = 1;
8d3788bd 6364
0d381245 6365 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6366 {
6367 struct cleanup *inner2 =
6368 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6369 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6370 do_cleanups (inner2);
6371 }
0d381245
VP
6372 }
6373 }
6374}
6375
a6d9a66e
UW
6376static int
6377breakpoint_address_bits (struct breakpoint *b)
6378{
6379 int print_address_bits = 0;
6380 struct bp_location *loc;
6381
6382 for (loc = b->loc; loc; loc = loc->next)
6383 {
c7437ca6
PA
6384 int addr_bit;
6385
6386 /* Software watchpoints that aren't watching memory don't have
6387 an address to print. */
6388 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6389 continue;
6390
6391 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6392 if (addr_bit > print_address_bits)
6393 print_address_bits = addr_bit;
6394 }
6395
6396 return print_address_bits;
6397}
0d381245 6398
c4093a6a
JM
6399struct captured_breakpoint_query_args
6400 {
6401 int bnum;
6402 };
c5aa993b 6403
c4093a6a 6404static int
2b65245e 6405do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6406{
6407 struct captured_breakpoint_query_args *args = data;
52f0bd74 6408 struct breakpoint *b;
a6d9a66e 6409 struct bp_location *dummy_loc = NULL;
cc59ec59 6410
c4093a6a
JM
6411 ALL_BREAKPOINTS (b)
6412 {
6413 if (args->bnum == b->number)
c5aa993b 6414 {
12c5a436 6415 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6416 return GDB_RC_OK;
c5aa993b 6417 }
c4093a6a
JM
6418 }
6419 return GDB_RC_NONE;
6420}
c5aa993b 6421
c4093a6a 6422enum gdb_rc
4a64f543
MS
6423gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6424 char **error_message)
c4093a6a
JM
6425{
6426 struct captured_breakpoint_query_args args;
cc59ec59 6427
c4093a6a
JM
6428 args.bnum = bnum;
6429 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6430 an error. */
b0b13bb4
DJ
6431 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6432 error_message, RETURN_MASK_ALL) < 0)
6433 return GDB_RC_FAIL;
6434 else
6435 return GDB_RC_OK;
c4093a6a 6436}
c5aa993b 6437
09d682a4
TT
6438/* Return true if this breakpoint was set by the user, false if it is
6439 internal or momentary. */
6440
6441int
6442user_breakpoint_p (struct breakpoint *b)
6443{
46c6471b 6444 return b->number > 0;
09d682a4
TT
6445}
6446
7f3b0473 6447/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6448 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6449 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6450 FILTER is non-NULL, call it on each breakpoint and only include the
6451 ones for which it returns non-zero. Return the total number of
6452 breakpoints listed. */
c906108c 6453
d77f58be 6454static int
e5a67952 6455breakpoint_1 (char *args, int allflag,
4a64f543 6456 int (*filter) (const struct breakpoint *))
c4093a6a 6457{
52f0bd74 6458 struct breakpoint *b;
a6d9a66e 6459 struct bp_location *last_loc = NULL;
7f3b0473 6460 int nr_printable_breakpoints;
3b31d625 6461 struct cleanup *bkpttbl_chain;
79a45b7d 6462 struct value_print_options opts;
a6d9a66e 6463 int print_address_bits = 0;
269b11a2 6464 int print_type_col_width = 14;
79a45e25 6465 struct ui_out *uiout = current_uiout;
269b11a2 6466
79a45b7d
TT
6467 get_user_print_options (&opts);
6468
4a64f543
MS
6469 /* Compute the number of rows in the table, as well as the size
6470 required for address fields. */
7f3b0473
AC
6471 nr_printable_breakpoints = 0;
6472 ALL_BREAKPOINTS (b)
e5a67952
MS
6473 {
6474 /* If we have a filter, only list the breakpoints it accepts. */
6475 if (filter && !filter (b))
6476 continue;
6477
6478 /* If we have an "args" string, it is a list of breakpoints to
6479 accept. Skip the others. */
6480 if (args != NULL && *args != '\0')
6481 {
6482 if (allflag && parse_and_eval_long (args) != b->number)
6483 continue;
6484 if (!allflag && !number_is_in_list (args, b->number))
6485 continue;
6486 }
269b11a2 6487
e5a67952
MS
6488 if (allflag || user_breakpoint_p (b))
6489 {
6490 int addr_bit, type_len;
a6d9a66e 6491
e5a67952
MS
6492 addr_bit = breakpoint_address_bits (b);
6493 if (addr_bit > print_address_bits)
6494 print_address_bits = addr_bit;
269b11a2 6495
e5a67952
MS
6496 type_len = strlen (bptype_string (b->type));
6497 if (type_len > print_type_col_width)
6498 print_type_col_width = type_len;
6499
6500 nr_printable_breakpoints++;
6501 }
6502 }
7f3b0473 6503
79a45b7d 6504 if (opts.addressprint)
3b31d625 6505 bkpttbl_chain
3e43a32a
MS
6506 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6507 nr_printable_breakpoints,
3b31d625 6508 "BreakpointTable");
8b93c638 6509 else
3b31d625 6510 bkpttbl_chain
3e43a32a
MS
6511 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6512 nr_printable_breakpoints,
3b31d625 6513 "BreakpointTable");
8b93c638 6514
7f3b0473 6515 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6516 annotate_breakpoints_headers ();
6517 if (nr_printable_breakpoints > 0)
6518 annotate_field (0);
4a64f543 6519 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6520 if (nr_printable_breakpoints > 0)
6521 annotate_field (1);
269b11a2 6522 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6523 "type", "Type"); /* 2 */
d7faa9e7
AC
6524 if (nr_printable_breakpoints > 0)
6525 annotate_field (2);
4a64f543 6526 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6527 if (nr_printable_breakpoints > 0)
6528 annotate_field (3);
54e52265 6529 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6530 if (opts.addressprint)
e5a67952
MS
6531 {
6532 if (nr_printable_breakpoints > 0)
6533 annotate_field (4);
6534 if (print_address_bits <= 32)
6535 ui_out_table_header (uiout, 10, ui_left,
6536 "addr", "Address"); /* 5 */
6537 else
6538 ui_out_table_header (uiout, 18, ui_left,
6539 "addr", "Address"); /* 5 */
6540 }
d7faa9e7
AC
6541 if (nr_printable_breakpoints > 0)
6542 annotate_field (5);
6543 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6544 ui_out_table_body (uiout);
6545 if (nr_printable_breakpoints > 0)
6546 annotate_breakpoints_table ();
7f3b0473 6547
c4093a6a 6548 ALL_BREAKPOINTS (b)
e5a67952
MS
6549 {
6550 QUIT;
6551 /* If we have a filter, only list the breakpoints it accepts. */
6552 if (filter && !filter (b))
6553 continue;
6554
6555 /* If we have an "args" string, it is a list of breakpoints to
6556 accept. Skip the others. */
6557
6558 if (args != NULL && *args != '\0')
6559 {
6560 if (allflag) /* maintenance info breakpoint */
6561 {
6562 if (parse_and_eval_long (args) != b->number)
6563 continue;
6564 }
6565 else /* all others */
6566 {
6567 if (!number_is_in_list (args, b->number))
6568 continue;
6569 }
6570 }
6571 /* We only print out user settable breakpoints unless the
6572 allflag is set. */
6573 if (allflag || user_breakpoint_p (b))
12c5a436 6574 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6575 }
6576
3b31d625 6577 do_cleanups (bkpttbl_chain);
698384cd 6578
7f3b0473 6579 if (nr_printable_breakpoints == 0)
c906108c 6580 {
4a64f543
MS
6581 /* If there's a filter, let the caller decide how to report
6582 empty list. */
d77f58be
SS
6583 if (!filter)
6584 {
e5a67952 6585 if (args == NULL || *args == '\0')
d77f58be
SS
6586 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6587 else
4a64f543 6588 ui_out_message (uiout, 0,
e5a67952
MS
6589 "No breakpoint or watchpoint matching '%s'.\n",
6590 args);
d77f58be 6591 }
c906108c
SS
6592 }
6593 else
c4093a6a 6594 {
a6d9a66e
UW
6595 if (last_loc && !server_command)
6596 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6597 }
c906108c 6598
4a64f543 6599 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6600 there have been breakpoints? */
c906108c 6601 annotate_breakpoints_table_end ();
d77f58be
SS
6602
6603 return nr_printable_breakpoints;
c906108c
SS
6604}
6605
ad443146
SS
6606/* Display the value of default-collect in a way that is generally
6607 compatible with the breakpoint list. */
6608
6609static void
6610default_collect_info (void)
6611{
79a45e25
PA
6612 struct ui_out *uiout = current_uiout;
6613
ad443146
SS
6614 /* If it has no value (which is frequently the case), say nothing; a
6615 message like "No default-collect." gets in user's face when it's
6616 not wanted. */
6617 if (!*default_collect)
6618 return;
6619
6620 /* The following phrase lines up nicely with per-tracepoint collect
6621 actions. */
6622 ui_out_text (uiout, "default collect ");
6623 ui_out_field_string (uiout, "default-collect", default_collect);
6624 ui_out_text (uiout, " \n");
6625}
6626
c906108c 6627static void
e5a67952 6628breakpoints_info (char *args, int from_tty)
c906108c 6629{
e5a67952 6630 breakpoint_1 (args, 0, NULL);
ad443146
SS
6631
6632 default_collect_info ();
d77f58be
SS
6633}
6634
6635static void
e5a67952 6636watchpoints_info (char *args, int from_tty)
d77f58be 6637{
e5a67952 6638 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6639 struct ui_out *uiout = current_uiout;
d77f58be
SS
6640
6641 if (num_printed == 0)
6642 {
e5a67952 6643 if (args == NULL || *args == '\0')
d77f58be
SS
6644 ui_out_message (uiout, 0, "No watchpoints.\n");
6645 else
e5a67952 6646 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6647 }
c906108c
SS
6648}
6649
7a292a7a 6650static void
e5a67952 6651maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6652{
e5a67952 6653 breakpoint_1 (args, 1, NULL);
ad443146
SS
6654
6655 default_collect_info ();
c906108c
SS
6656}
6657
0d381245 6658static int
714835d5 6659breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6660 struct program_space *pspace,
714835d5 6661 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6662{
6663 struct bp_location *bl = b->loc;
cc59ec59 6664
0d381245
VP
6665 for (; bl; bl = bl->next)
6666 {
6c95b8df
PA
6667 if (bl->pspace == pspace
6668 && bl->address == pc
0d381245
VP
6669 && (!overlay_debugging || bl->section == section))
6670 return 1;
6671 }
6672 return 0;
6673}
6674
672f9b60 6675/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6676 concerns with logical breakpoints, so we match program spaces, not
6677 address spaces. */
c906108c
SS
6678
6679static void
6c95b8df
PA
6680describe_other_breakpoints (struct gdbarch *gdbarch,
6681 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6682 struct obj_section *section, int thread)
c906108c 6683{
52f0bd74
AC
6684 int others = 0;
6685 struct breakpoint *b;
c906108c
SS
6686
6687 ALL_BREAKPOINTS (b)
672f9b60
KP
6688 others += (user_breakpoint_p (b)
6689 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6690 if (others > 0)
6691 {
a3f17187
AC
6692 if (others == 1)
6693 printf_filtered (_("Note: breakpoint "));
6694 else /* if (others == ???) */
6695 printf_filtered (_("Note: breakpoints "));
c906108c 6696 ALL_BREAKPOINTS (b)
672f9b60 6697 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6698 {
6699 others--;
6700 printf_filtered ("%d", b->number);
6701 if (b->thread == -1 && thread != -1)
6702 printf_filtered (" (all threads)");
6703 else if (b->thread != -1)
6704 printf_filtered (" (thread %d)", b->thread);
6705 printf_filtered ("%s%s ",
059fb39f 6706 ((b->enable_state == bp_disabled
f8eba3c6 6707 || b->enable_state == bp_call_disabled)
0d381245
VP
6708 ? " (disabled)"
6709 : b->enable_state == bp_permanent
6710 ? " (permanent)"
6711 : ""),
6712 (others > 1) ? ","
6713 : ((others == 1) ? " and" : ""));
6714 }
a3f17187 6715 printf_filtered (_("also set at pc "));
5af949e3 6716 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6717 printf_filtered (".\n");
6718 }
6719}
6720\f
c906108c 6721
e4f237da
KB
6722/* Return true iff it is meaningful to use the address member of
6723 BPT. For some breakpoint types, the address member is irrelevant
6724 and it makes no sense to attempt to compare it to other addresses
6725 (or use it for any other purpose either).
6726
4a64f543
MS
6727 More specifically, each of the following breakpoint types will
6728 always have a zero valued address and we don't want to mark
6729 breakpoints of any of these types to be a duplicate of an actual
6730 breakpoint at address zero:
e4f237da
KB
6731
6732 bp_watchpoint
2d134ed3
PA
6733 bp_catchpoint
6734
6735*/
e4f237da
KB
6736
6737static int
6738breakpoint_address_is_meaningful (struct breakpoint *bpt)
6739{
6740 enum bptype type = bpt->type;
6741
2d134ed3
PA
6742 return (type != bp_watchpoint && type != bp_catchpoint);
6743}
6744
6745/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6746 true if LOC1 and LOC2 represent the same watchpoint location. */
6747
6748static int
4a64f543
MS
6749watchpoint_locations_match (struct bp_location *loc1,
6750 struct bp_location *loc2)
2d134ed3 6751{
3a5c3e22
PA
6752 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6753 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6754
6755 /* Both of them must exist. */
6756 gdb_assert (w1 != NULL);
6757 gdb_assert (w2 != NULL);
2bdf28a0 6758
4a64f543
MS
6759 /* If the target can evaluate the condition expression in hardware,
6760 then we we need to insert both watchpoints even if they are at
6761 the same place. Otherwise the watchpoint will only trigger when
6762 the condition of whichever watchpoint was inserted evaluates to
6763 true, not giving a chance for GDB to check the condition of the
6764 other watchpoint. */
3a5c3e22 6765 if ((w1->cond_exp
4a64f543
MS
6766 && target_can_accel_watchpoint_condition (loc1->address,
6767 loc1->length,
0cf6dd15 6768 loc1->watchpoint_type,
3a5c3e22
PA
6769 w1->cond_exp))
6770 || (w2->cond_exp
4a64f543
MS
6771 && target_can_accel_watchpoint_condition (loc2->address,
6772 loc2->length,
0cf6dd15 6773 loc2->watchpoint_type,
3a5c3e22 6774 w2->cond_exp)))
0cf6dd15
TJB
6775 return 0;
6776
85d721b8
PA
6777 /* Note that this checks the owner's type, not the location's. In
6778 case the target does not support read watchpoints, but does
6779 support access watchpoints, we'll have bp_read_watchpoint
6780 watchpoints with hw_access locations. Those should be considered
6781 duplicates of hw_read locations. The hw_read locations will
6782 become hw_access locations later. */
2d134ed3
PA
6783 return (loc1->owner->type == loc2->owner->type
6784 && loc1->pspace->aspace == loc2->pspace->aspace
6785 && loc1->address == loc2->address
6786 && loc1->length == loc2->length);
e4f237da
KB
6787}
6788
6c95b8df
PA
6789/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6790 same breakpoint location. In most targets, this can only be true
6791 if ASPACE1 matches ASPACE2. On targets that have global
6792 breakpoints, the address space doesn't really matter. */
6793
6794static int
6795breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6796 struct address_space *aspace2, CORE_ADDR addr2)
6797{
f5656ead 6798 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6799 || aspace1 == aspace2)
6800 && addr1 == addr2);
6801}
6802
f1310107
TJB
6803/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6804 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6805 matches ASPACE2. On targets that have global breakpoints, the address
6806 space doesn't really matter. */
6807
6808static int
6809breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6810 int len1, struct address_space *aspace2,
6811 CORE_ADDR addr2)
6812{
f5656ead 6813 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6814 || aspace1 == aspace2)
6815 && addr2 >= addr1 && addr2 < addr1 + len1);
6816}
6817
6818/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6819 a ranged breakpoint. In most targets, a match happens only if ASPACE
6820 matches the breakpoint's address space. On targets that have global
6821 breakpoints, the address space doesn't really matter. */
6822
6823static int
6824breakpoint_location_address_match (struct bp_location *bl,
6825 struct address_space *aspace,
6826 CORE_ADDR addr)
6827{
6828 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6829 aspace, addr)
6830 || (bl->length
6831 && breakpoint_address_match_range (bl->pspace->aspace,
6832 bl->address, bl->length,
6833 aspace, addr)));
6834}
6835
1e4d1764
YQ
6836/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6837 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6838 true, otherwise returns false. */
6839
6840static int
6841tracepoint_locations_match (struct bp_location *loc1,
6842 struct bp_location *loc2)
6843{
6844 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6845 /* Since tracepoint locations are never duplicated with others', tracepoint
6846 locations at the same address of different tracepoints are regarded as
6847 different locations. */
6848 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6849 else
6850 return 0;
6851}
6852
2d134ed3
PA
6853/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6854 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6855 represent the same location. */
6856
6857static int
4a64f543
MS
6858breakpoint_locations_match (struct bp_location *loc1,
6859 struct bp_location *loc2)
2d134ed3 6860{
2bdf28a0
JK
6861 int hw_point1, hw_point2;
6862
6863 /* Both of them must not be in moribund_locations. */
6864 gdb_assert (loc1->owner != NULL);
6865 gdb_assert (loc2->owner != NULL);
6866
6867 hw_point1 = is_hardware_watchpoint (loc1->owner);
6868 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6869
6870 if (hw_point1 != hw_point2)
6871 return 0;
6872 else if (hw_point1)
6873 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6874 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6875 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6876 else
f1310107
TJB
6877 /* We compare bp_location.length in order to cover ranged breakpoints. */
6878 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6879 loc2->pspace->aspace, loc2->address)
6880 && loc1->length == loc2->length);
2d134ed3
PA
6881}
6882
76897487
KB
6883static void
6884breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6885 int bnum, int have_bnum)
6886{
f63fbe86
MS
6887 /* The longest string possibly returned by hex_string_custom
6888 is 50 chars. These must be at least that big for safety. */
6889 char astr1[64];
6890 char astr2[64];
76897487 6891
bb599908
PH
6892 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6893 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6894 if (have_bnum)
8a3fe4f8 6895 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6896 bnum, astr1, astr2);
6897 else
8a3fe4f8 6898 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6899}
6900
4a64f543
MS
6901/* Adjust a breakpoint's address to account for architectural
6902 constraints on breakpoint placement. Return the adjusted address.
6903 Note: Very few targets require this kind of adjustment. For most
6904 targets, this function is simply the identity function. */
76897487
KB
6905
6906static CORE_ADDR
a6d9a66e
UW
6907adjust_breakpoint_address (struct gdbarch *gdbarch,
6908 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6909{
a6d9a66e 6910 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
6911 {
6912 /* Very few targets need any kind of breakpoint adjustment. */
6913 return bpaddr;
6914 }
88f7da05
KB
6915 else if (bptype == bp_watchpoint
6916 || bptype == bp_hardware_watchpoint
6917 || bptype == bp_read_watchpoint
6918 || bptype == bp_access_watchpoint
fe798b75 6919 || bptype == bp_catchpoint)
88f7da05
KB
6920 {
6921 /* Watchpoints and the various bp_catch_* eventpoints should not
6922 have their addresses modified. */
6923 return bpaddr;
6924 }
76897487
KB
6925 else
6926 {
6927 CORE_ADDR adjusted_bpaddr;
6928
6929 /* Some targets have architectural constraints on the placement
6930 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 6931 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
6932
6933 /* An adjusted breakpoint address can significantly alter
6934 a user's expectations. Print a warning if an adjustment
6935 is required. */
6936 if (adjusted_bpaddr != bpaddr)
6937 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6938
6939 return adjusted_bpaddr;
6940 }
6941}
6942
28010a5d
PA
6943void
6944init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6945 struct breakpoint *owner)
7cc221ef 6946{
7cc221ef
DJ
6947 memset (loc, 0, sizeof (*loc));
6948
348d480f
PA
6949 gdb_assert (ops != NULL);
6950
28010a5d
PA
6951 loc->ops = ops;
6952 loc->owner = owner;
511a6cd4 6953 loc->cond = NULL;
b775012e 6954 loc->cond_bytecode = NULL;
0d381245
VP
6955 loc->shlib_disabled = 0;
6956 loc->enabled = 1;
e049a4b5 6957
28010a5d 6958 switch (owner->type)
e049a4b5
DJ
6959 {
6960 case bp_breakpoint:
6961 case bp_until:
6962 case bp_finish:
6963 case bp_longjmp:
6964 case bp_longjmp_resume:
e2e4d78b 6965 case bp_longjmp_call_dummy:
186c406b
TT
6966 case bp_exception:
6967 case bp_exception_resume:
e049a4b5 6968 case bp_step_resume:
2c03e5be 6969 case bp_hp_step_resume:
e049a4b5
DJ
6970 case bp_watchpoint_scope:
6971 case bp_call_dummy:
aa7d318d 6972 case bp_std_terminate:
e049a4b5
DJ
6973 case bp_shlib_event:
6974 case bp_thread_event:
6975 case bp_overlay_event:
4efc6507 6976 case bp_jit_event:
0fd8e87f 6977 case bp_longjmp_master:
aa7d318d 6978 case bp_std_terminate_master:
186c406b 6979 case bp_exception_master:
0e30163f
JK
6980 case bp_gnu_ifunc_resolver:
6981 case bp_gnu_ifunc_resolver_return:
e7e0cddf 6982 case bp_dprintf:
e049a4b5 6983 loc->loc_type = bp_loc_software_breakpoint;
b775012e 6984 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6985 break;
6986 case bp_hardware_breakpoint:
6987 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 6988 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6989 break;
6990 case bp_hardware_watchpoint:
6991 case bp_read_watchpoint:
6992 case bp_access_watchpoint:
6993 loc->loc_type = bp_loc_hardware_watchpoint;
6994 break;
6995 case bp_watchpoint:
ce78b96d 6996 case bp_catchpoint:
15c3d785
PA
6997 case bp_tracepoint:
6998 case bp_fast_tracepoint:
0fb4aa4b 6999 case bp_static_tracepoint:
e049a4b5
DJ
7000 loc->loc_type = bp_loc_other;
7001 break;
7002 default:
e2e0b3e5 7003 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7004 }
7005
f431efe5 7006 loc->refc = 1;
28010a5d
PA
7007}
7008
7009/* Allocate a struct bp_location. */
7010
7011static struct bp_location *
7012allocate_bp_location (struct breakpoint *bpt)
7013{
348d480f
PA
7014 return bpt->ops->allocate_location (bpt);
7015}
7cc221ef 7016
f431efe5
PA
7017static void
7018free_bp_location (struct bp_location *loc)
fe3f5fa8 7019{
348d480f 7020 loc->ops->dtor (loc);
fe3f5fa8
VP
7021 xfree (loc);
7022}
7023
f431efe5
PA
7024/* Increment reference count. */
7025
7026static void
7027incref_bp_location (struct bp_location *bl)
7028{
7029 ++bl->refc;
7030}
7031
7032/* Decrement reference count. If the reference count reaches 0,
7033 destroy the bp_location. Sets *BLP to NULL. */
7034
7035static void
7036decref_bp_location (struct bp_location **blp)
7037{
0807b50c
PA
7038 gdb_assert ((*blp)->refc > 0);
7039
f431efe5
PA
7040 if (--(*blp)->refc == 0)
7041 free_bp_location (*blp);
7042 *blp = NULL;
7043}
7044
346774a9 7045/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7046
346774a9
PA
7047static void
7048add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7049{
346774a9 7050 struct breakpoint *b1;
c906108c 7051
346774a9
PA
7052 /* Add this breakpoint to the end of the chain so that a list of
7053 breakpoints will come out in order of increasing numbers. */
7054
7055 b1 = breakpoint_chain;
7056 if (b1 == 0)
7057 breakpoint_chain = b;
7058 else
7059 {
7060 while (b1->next)
7061 b1 = b1->next;
7062 b1->next = b;
7063 }
7064}
7065
7066/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7067
7068static void
7069init_raw_breakpoint_without_location (struct breakpoint *b,
7070 struct gdbarch *gdbarch,
28010a5d 7071 enum bptype bptype,
c0a91b2b 7072 const struct breakpoint_ops *ops)
346774a9 7073{
c906108c 7074 memset (b, 0, sizeof (*b));
2219d63c 7075
348d480f
PA
7076 gdb_assert (ops != NULL);
7077
28010a5d 7078 b->ops = ops;
4d28f7a8 7079 b->type = bptype;
a6d9a66e 7080 b->gdbarch = gdbarch;
c906108c
SS
7081 b->language = current_language->la_language;
7082 b->input_radix = input_radix;
7083 b->thread = -1;
b5de0fa7 7084 b->enable_state = bp_enabled;
c906108c
SS
7085 b->next = 0;
7086 b->silent = 0;
7087 b->ignore_count = 0;
7088 b->commands = NULL;
818dd999 7089 b->frame_id = null_frame_id;
0d381245 7090 b->condition_not_parsed = 0;
84f4c1fe 7091 b->py_bp_object = NULL;
d0fb5eae 7092 b->related_breakpoint = b;
346774a9
PA
7093}
7094
7095/* Helper to set_raw_breakpoint below. Creates a breakpoint
7096 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7097
7098static struct breakpoint *
7099set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7100 enum bptype bptype,
c0a91b2b 7101 const struct breakpoint_ops *ops)
346774a9
PA
7102{
7103 struct breakpoint *b = XNEW (struct breakpoint);
7104
348d480f 7105 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7106 add_to_breakpoint_chain (b);
0d381245
VP
7107 return b;
7108}
7109
0e30163f
JK
7110/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7111 resolutions should be made as the user specified the location explicitly
7112 enough. */
7113
0d381245 7114static void
0e30163f 7115set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7116{
2bdf28a0
JK
7117 gdb_assert (loc->owner != NULL);
7118
0d381245 7119 if (loc->owner->type == bp_breakpoint
1042e4c0 7120 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7121 || is_tracepoint (loc->owner))
0d381245 7122 {
0e30163f 7123 int is_gnu_ifunc;
2c02bd72 7124 const char *function_name;
6a3a010b 7125 CORE_ADDR func_addr;
0e30163f 7126
2c02bd72 7127 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7128 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7129
7130 if (is_gnu_ifunc && !explicit_loc)
7131 {
7132 struct breakpoint *b = loc->owner;
7133
7134 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7135 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7136 &loc->requested_address))
7137 {
7138 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7139 loc->address = adjust_breakpoint_address (loc->gdbarch,
7140 loc->requested_address,
7141 b->type);
7142 }
7143 else if (b->type == bp_breakpoint && b->loc == loc
7144 && loc->next == NULL && b->related_breakpoint == b)
7145 {
7146 /* Create only the whole new breakpoint of this type but do not
7147 mess more complicated breakpoints with multiple locations. */
7148 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7149 /* Remember the resolver's address for use by the return
7150 breakpoint. */
7151 loc->related_address = func_addr;
0e30163f
JK
7152 }
7153 }
7154
2c02bd72
DE
7155 if (function_name)
7156 loc->function_name = xstrdup (function_name);
0d381245
VP
7157 }
7158}
7159
a6d9a66e 7160/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7161struct gdbarch *
a6d9a66e
UW
7162get_sal_arch (struct symtab_and_line sal)
7163{
7164 if (sal.section)
7165 return get_objfile_arch (sal.section->objfile);
7166 if (sal.symtab)
7167 return get_objfile_arch (sal.symtab->objfile);
7168
7169 return NULL;
7170}
7171
346774a9
PA
7172/* Low level routine for partially initializing a breakpoint of type
7173 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7174 file name, and line number are provided by SAL.
0d381245
VP
7175
7176 It is expected that the caller will complete the initialization of
7177 the newly created breakpoint struct as well as output any status
c56053d2 7178 information regarding the creation of a new breakpoint. */
0d381245 7179
346774a9
PA
7180static void
7181init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7182 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7183 const struct breakpoint_ops *ops)
0d381245 7184{
28010a5d 7185 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7186
3742cc8b 7187 add_location_to_breakpoint (b, &sal);
0d381245 7188
6c95b8df
PA
7189 if (bptype != bp_catchpoint)
7190 gdb_assert (sal.pspace != NULL);
7191
f8eba3c6
TT
7192 /* Store the program space that was used to set the breakpoint,
7193 except for ordinary breakpoints, which are independent of the
7194 program space. */
7195 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7196 b->pspace = sal.pspace;
346774a9 7197}
c906108c 7198
346774a9
PA
7199/* set_raw_breakpoint is a low level routine for allocating and
7200 partially initializing a breakpoint of type BPTYPE. The newly
7201 created breakpoint's address, section, source file name, and line
7202 number are provided by SAL. The newly created and partially
7203 initialized breakpoint is added to the breakpoint chain and
7204 is also returned as the value of this function.
7205
7206 It is expected that the caller will complete the initialization of
7207 the newly created breakpoint struct as well as output any status
7208 information regarding the creation of a new breakpoint. In
7209 particular, set_raw_breakpoint does NOT set the breakpoint
7210 number! Care should be taken to not allow an error to occur
7211 prior to completing the initialization of the breakpoint. If this
7212 should happen, a bogus breakpoint will be left on the chain. */
7213
7214struct breakpoint *
7215set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7216 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7217 const struct breakpoint_ops *ops)
346774a9
PA
7218{
7219 struct breakpoint *b = XNEW (struct breakpoint);
7220
348d480f 7221 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7222 add_to_breakpoint_chain (b);
c906108c
SS
7223 return b;
7224}
7225
c2c6d25f
JM
7226
7227/* Note that the breakpoint object B describes a permanent breakpoint
7228 instruction, hard-wired into the inferior's code. */
7229void
7230make_breakpoint_permanent (struct breakpoint *b)
7231{
0d381245 7232 struct bp_location *bl;
cc59ec59 7233
b5de0fa7 7234 b->enable_state = bp_permanent;
c2c6d25f 7235
4a64f543
MS
7236 /* By definition, permanent breakpoints are already present in the
7237 code. Mark all locations as inserted. For now,
7238 make_breakpoint_permanent is called in just one place, so it's
7239 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7240 multiple locations or not, but it's easy to implement. */
0d381245
VP
7241 for (bl = b->loc; bl; bl = bl->next)
7242 bl->inserted = 1;
c2c6d25f
JM
7243}
7244
53a5351d 7245/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7246 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7247 initiated the operation. */
c906108c
SS
7248
7249void
186c406b 7250set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7251{
35df4500 7252 struct breakpoint *b, *b_tmp;
186c406b 7253 int thread = tp->num;
0fd8e87f
UW
7254
7255 /* To avoid having to rescan all objfile symbols at every step,
7256 we maintain a list of continually-inserted but always disabled
7257 longjmp "master" breakpoints. Here, we simply create momentary
7258 clones of those and enable them for the requested thread. */
35df4500 7259 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7260 if (b->pspace == current_program_space
186c406b
TT
7261 && (b->type == bp_longjmp_master
7262 || b->type == bp_exception_master))
0fd8e87f 7263 {
06edf0c0
PA
7264 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7265 struct breakpoint *clone;
cc59ec59 7266
e2e4d78b
JK
7267 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7268 after their removal. */
06edf0c0 7269 clone = momentary_breakpoint_from_master (b, type,
e2e4d78b 7270 &longjmp_breakpoint_ops);
0fd8e87f
UW
7271 clone->thread = thread;
7272 }
186c406b
TT
7273
7274 tp->initiating_frame = frame;
c906108c
SS
7275}
7276
611c83ae 7277/* Delete all longjmp breakpoints from THREAD. */
c906108c 7278void
611c83ae 7279delete_longjmp_breakpoint (int thread)
c906108c 7280{
35df4500 7281 struct breakpoint *b, *b_tmp;
c906108c 7282
35df4500 7283 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7284 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7285 {
7286 if (b->thread == thread)
7287 delete_breakpoint (b);
7288 }
c906108c
SS
7289}
7290
f59f708a
PA
7291void
7292delete_longjmp_breakpoint_at_next_stop (int thread)
7293{
7294 struct breakpoint *b, *b_tmp;
7295
7296 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7297 if (b->type == bp_longjmp || b->type == bp_exception)
7298 {
7299 if (b->thread == thread)
7300 b->disposition = disp_del_at_next_stop;
7301 }
7302}
7303
e2e4d78b
JK
7304/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7305 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7306 pointer to any of them. Return NULL if this system cannot place longjmp
7307 breakpoints. */
7308
7309struct breakpoint *
7310set_longjmp_breakpoint_for_call_dummy (void)
7311{
7312 struct breakpoint *b, *retval = NULL;
7313
7314 ALL_BREAKPOINTS (b)
7315 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7316 {
7317 struct breakpoint *new_b;
7318
7319 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7320 &momentary_breakpoint_ops);
7321 new_b->thread = pid_to_thread_id (inferior_ptid);
7322
7323 /* Link NEW_B into the chain of RETVAL breakpoints. */
7324
7325 gdb_assert (new_b->related_breakpoint == new_b);
7326 if (retval == NULL)
7327 retval = new_b;
7328 new_b->related_breakpoint = retval;
7329 while (retval->related_breakpoint != new_b->related_breakpoint)
7330 retval = retval->related_breakpoint;
7331 retval->related_breakpoint = new_b;
7332 }
7333
7334 return retval;
7335}
7336
7337/* Verify all existing dummy frames and their associated breakpoints for
7338 THREAD. Remove those which can no longer be found in the current frame
7339 stack.
7340
7341 You should call this function only at places where it is safe to currently
7342 unwind the whole stack. Failed stack unwind would discard live dummy
7343 frames. */
7344
7345void
7346check_longjmp_breakpoint_for_call_dummy (int thread)
7347{
7348 struct breakpoint *b, *b_tmp;
7349
7350 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7351 if (b->type == bp_longjmp_call_dummy && b->thread == thread)
7352 {
7353 struct breakpoint *dummy_b = b->related_breakpoint;
7354
7355 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7356 dummy_b = dummy_b->related_breakpoint;
7357 if (dummy_b->type != bp_call_dummy
7358 || frame_find_by_id (dummy_b->frame_id) != NULL)
7359 continue;
7360
7361 dummy_frame_discard (dummy_b->frame_id);
7362
7363 while (b->related_breakpoint != b)
7364 {
7365 if (b_tmp == b->related_breakpoint)
7366 b_tmp = b->related_breakpoint->next;
7367 delete_breakpoint (b->related_breakpoint);
7368 }
7369 delete_breakpoint (b);
7370 }
7371}
7372
1900040c
MS
7373void
7374enable_overlay_breakpoints (void)
7375{
52f0bd74 7376 struct breakpoint *b;
1900040c
MS
7377
7378 ALL_BREAKPOINTS (b)
7379 if (b->type == bp_overlay_event)
7380 {
7381 b->enable_state = bp_enabled;
b60e7edf 7382 update_global_location_list (1);
c02f5703 7383 overlay_events_enabled = 1;
1900040c
MS
7384 }
7385}
7386
7387void
7388disable_overlay_breakpoints (void)
7389{
52f0bd74 7390 struct breakpoint *b;
1900040c
MS
7391
7392 ALL_BREAKPOINTS (b)
7393 if (b->type == bp_overlay_event)
7394 {
7395 b->enable_state = bp_disabled;
b60e7edf 7396 update_global_location_list (0);
c02f5703 7397 overlay_events_enabled = 0;
1900040c
MS
7398 }
7399}
7400
aa7d318d
TT
7401/* Set an active std::terminate breakpoint for each std::terminate
7402 master breakpoint. */
7403void
7404set_std_terminate_breakpoint (void)
7405{
35df4500 7406 struct breakpoint *b, *b_tmp;
aa7d318d 7407
35df4500 7408 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7409 if (b->pspace == current_program_space
7410 && b->type == bp_std_terminate_master)
7411 {
06edf0c0
PA
7412 momentary_breakpoint_from_master (b, bp_std_terminate,
7413 &momentary_breakpoint_ops);
aa7d318d
TT
7414 }
7415}
7416
7417/* Delete all the std::terminate breakpoints. */
7418void
7419delete_std_terminate_breakpoint (void)
7420{
35df4500 7421 struct breakpoint *b, *b_tmp;
aa7d318d 7422
35df4500 7423 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7424 if (b->type == bp_std_terminate)
7425 delete_breakpoint (b);
7426}
7427
c4093a6a 7428struct breakpoint *
a6d9a66e 7429create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7430{
7431 struct breakpoint *b;
c4093a6a 7432
06edf0c0
PA
7433 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7434 &internal_breakpoint_ops);
7435
b5de0fa7 7436 b->enable_state = bp_enabled;
c4093a6a 7437 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7438 b->addr_string
7439 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7440
b60e7edf 7441 update_global_location_list_nothrow (1);
74960c60 7442
c4093a6a
JM
7443 return b;
7444}
7445
7446void
7447remove_thread_event_breakpoints (void)
7448{
35df4500 7449 struct breakpoint *b, *b_tmp;
c4093a6a 7450
35df4500 7451 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7452 if (b->type == bp_thread_event
7453 && b->loc->pspace == current_program_space)
c4093a6a
JM
7454 delete_breakpoint (b);
7455}
7456
0101ce28
JJ
7457struct lang_and_radix
7458 {
7459 enum language lang;
7460 int radix;
7461 };
7462
4efc6507
DE
7463/* Create a breakpoint for JIT code registration and unregistration. */
7464
7465struct breakpoint *
7466create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7467{
7468 struct breakpoint *b;
7469
06edf0c0
PA
7470 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7471 &internal_breakpoint_ops);
4efc6507
DE
7472 update_global_location_list_nothrow (1);
7473 return b;
7474}
0101ce28 7475
03673fc7
PP
7476/* Remove JIT code registration and unregistration breakpoint(s). */
7477
7478void
7479remove_jit_event_breakpoints (void)
7480{
7481 struct breakpoint *b, *b_tmp;
7482
7483 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7484 if (b->type == bp_jit_event
7485 && b->loc->pspace == current_program_space)
7486 delete_breakpoint (b);
7487}
7488
cae688ec
JJ
7489void
7490remove_solib_event_breakpoints (void)
7491{
35df4500 7492 struct breakpoint *b, *b_tmp;
cae688ec 7493
35df4500 7494 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7495 if (b->type == bp_shlib_event
7496 && b->loc->pspace == current_program_space)
cae688ec
JJ
7497 delete_breakpoint (b);
7498}
7499
7500struct breakpoint *
a6d9a66e 7501create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
7502{
7503 struct breakpoint *b;
7504
06edf0c0
PA
7505 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7506 &internal_breakpoint_ops);
b60e7edf 7507 update_global_location_list_nothrow (1);
cae688ec
JJ
7508 return b;
7509}
7510
7511/* Disable any breakpoints that are on code in shared libraries. Only
7512 apply to enabled breakpoints, disabled ones can just stay disabled. */
7513
7514void
cb851954 7515disable_breakpoints_in_shlibs (void)
cae688ec 7516{
876fa593 7517 struct bp_location *loc, **locp_tmp;
cae688ec 7518
876fa593 7519 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7520 {
2bdf28a0 7521 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7522 struct breakpoint *b = loc->owner;
2bdf28a0 7523
4a64f543
MS
7524 /* We apply the check to all breakpoints, including disabled for
7525 those with loc->duplicate set. This is so that when breakpoint
7526 becomes enabled, or the duplicate is removed, gdb will try to
7527 insert all breakpoints. If we don't set shlib_disabled here,
7528 we'll try to insert those breakpoints and fail. */
1042e4c0 7529 if (((b->type == bp_breakpoint)
508ccb1f 7530 || (b->type == bp_jit_event)
1042e4c0 7531 || (b->type == bp_hardware_breakpoint)
d77f58be 7532 || (is_tracepoint (b)))
6c95b8df 7533 && loc->pspace == current_program_space
0d381245 7534 && !loc->shlib_disabled
6c95b8df 7535 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7536 )
0d381245
VP
7537 {
7538 loc->shlib_disabled = 1;
7539 }
cae688ec
JJ
7540 }
7541}
7542
63644780
NB
7543/* Disable any breakpoints and tracepoints that are in SOLIB upon
7544 notification of unloaded_shlib. Only apply to enabled breakpoints,
7545 disabled ones can just stay disabled. */
84acb35a 7546
75149521 7547static void
84acb35a
JJ
7548disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7549{
876fa593 7550 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7551 int disabled_shlib_breaks = 0;
7552
c86cf029
VP
7553 /* SunOS a.out shared libraries are always mapped, so do not
7554 disable breakpoints; they will only be reported as unloaded
7555 through clear_solib when GDB discards its shared library
7556 list. See clear_solib for more information. */
7557 if (exec_bfd != NULL
7558 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7559 return;
7560
876fa593 7561 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7562 {
2bdf28a0 7563 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7564 struct breakpoint *b = loc->owner;
cc59ec59 7565
1e4d1764 7566 if (solib->pspace == loc->pspace
e2dd7057 7567 && !loc->shlib_disabled
1e4d1764
YQ
7568 && (((b->type == bp_breakpoint
7569 || b->type == bp_jit_event
7570 || b->type == bp_hardware_breakpoint)
7571 && (loc->loc_type == bp_loc_hardware_breakpoint
7572 || loc->loc_type == bp_loc_software_breakpoint))
7573 || is_tracepoint (b))
e2dd7057 7574 && solib_contains_address_p (solib, loc->address))
84acb35a 7575 {
e2dd7057
PP
7576 loc->shlib_disabled = 1;
7577 /* At this point, we cannot rely on remove_breakpoint
7578 succeeding so we must mark the breakpoint as not inserted
7579 to prevent future errors occurring in remove_breakpoints. */
7580 loc->inserted = 0;
8d3788bd
VP
7581
7582 /* This may cause duplicate notifications for the same breakpoint. */
7583 observer_notify_breakpoint_modified (b);
7584
e2dd7057
PP
7585 if (!disabled_shlib_breaks)
7586 {
7587 target_terminal_ours_for_output ();
3e43a32a
MS
7588 warning (_("Temporarily disabling breakpoints "
7589 "for unloaded shared library \"%s\""),
e2dd7057 7590 solib->so_name);
84acb35a 7591 }
e2dd7057 7592 disabled_shlib_breaks = 1;
84acb35a
JJ
7593 }
7594 }
84acb35a
JJ
7595}
7596
63644780
NB
7597/* Disable any breakpoints and tracepoints in OBJFILE upon
7598 notification of free_objfile. Only apply to enabled breakpoints,
7599 disabled ones can just stay disabled. */
7600
7601static void
7602disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7603{
7604 struct breakpoint *b;
7605
7606 if (objfile == NULL)
7607 return;
7608
7609 /* If the file is a shared library not loaded by the user then
7610 solib_unloaded was notified and disable_breakpoints_in_unloaded_shlib
7611 was called. In that case there is no need to take action again. */
7612 if ((objfile->flags & OBJF_SHARED) && !(objfile->flags & OBJF_USERLOADED))
7613 return;
7614
7615 ALL_BREAKPOINTS (b)
7616 {
7617 struct bp_location *loc;
7618 int bp_modified = 0;
7619
7620 if (!is_breakpoint (b) && !is_tracepoint (b))
7621 continue;
7622
7623 for (loc = b->loc; loc != NULL; loc = loc->next)
7624 {
7625 CORE_ADDR loc_addr = loc->address;
7626
7627 if (loc->loc_type != bp_loc_hardware_breakpoint
7628 && loc->loc_type != bp_loc_software_breakpoint)
7629 continue;
7630
7631 if (loc->shlib_disabled != 0)
7632 continue;
7633
7634 if (objfile->pspace != loc->pspace)
7635 continue;
7636
7637 if (loc->loc_type != bp_loc_hardware_breakpoint
7638 && loc->loc_type != bp_loc_software_breakpoint)
7639 continue;
7640
7641 if (is_addr_in_objfile (loc_addr, objfile))
7642 {
7643 loc->shlib_disabled = 1;
7644 loc->inserted = 0;
7645
7646 mark_breakpoint_location_modified (loc);
7647
7648 bp_modified = 1;
7649 }
7650 }
7651
7652 if (bp_modified)
7653 observer_notify_breakpoint_modified (b);
7654 }
7655}
7656
ce78b96d
JB
7657/* FORK & VFORK catchpoints. */
7658
e29a4733
PA
7659/* An instance of this type is used to represent a fork or vfork
7660 catchpoint. It includes a "struct breakpoint" as a kind of base
7661 class; users downcast to "struct breakpoint *" when needed. A
7662 breakpoint is really of this type iff its ops pointer points to
7663 CATCH_FORK_BREAKPOINT_OPS. */
7664
7665struct fork_catchpoint
7666{
7667 /* The base class. */
7668 struct breakpoint base;
7669
7670 /* Process id of a child process whose forking triggered this
7671 catchpoint. This field is only valid immediately after this
7672 catchpoint has triggered. */
7673 ptid_t forked_inferior_pid;
7674};
7675
4a64f543
MS
7676/* Implement the "insert" breakpoint_ops method for fork
7677 catchpoints. */
ce78b96d 7678
77b06cd7
TJB
7679static int
7680insert_catch_fork (struct bp_location *bl)
ce78b96d 7681{
dfd4cc63 7682 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7683}
7684
4a64f543
MS
7685/* Implement the "remove" breakpoint_ops method for fork
7686 catchpoints. */
ce78b96d
JB
7687
7688static int
77b06cd7 7689remove_catch_fork (struct bp_location *bl)
ce78b96d 7690{
dfd4cc63 7691 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7692}
7693
7694/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7695 catchpoints. */
7696
7697static int
f1310107 7698breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7699 struct address_space *aspace, CORE_ADDR bp_addr,
7700 const struct target_waitstatus *ws)
ce78b96d 7701{
e29a4733
PA
7702 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7703
f90263c1
TT
7704 if (ws->kind != TARGET_WAITKIND_FORKED)
7705 return 0;
7706
7707 c->forked_inferior_pid = ws->value.related_pid;
7708 return 1;
ce78b96d
JB
7709}
7710
4a64f543
MS
7711/* Implement the "print_it" breakpoint_ops method for fork
7712 catchpoints. */
ce78b96d
JB
7713
7714static enum print_stop_action
348d480f 7715print_it_catch_fork (bpstat bs)
ce78b96d 7716{
36dfb11c 7717 struct ui_out *uiout = current_uiout;
348d480f
PA
7718 struct breakpoint *b = bs->breakpoint_at;
7719 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7720
ce78b96d 7721 annotate_catchpoint (b->number);
36dfb11c
TT
7722 if (b->disposition == disp_del)
7723 ui_out_text (uiout, "\nTemporary catchpoint ");
7724 else
7725 ui_out_text (uiout, "\nCatchpoint ");
7726 if (ui_out_is_mi_like_p (uiout))
7727 {
7728 ui_out_field_string (uiout, "reason",
7729 async_reason_lookup (EXEC_ASYNC_FORK));
7730 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7731 }
7732 ui_out_field_int (uiout, "bkptno", b->number);
7733 ui_out_text (uiout, " (forked process ");
7734 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7735 ui_out_text (uiout, "), ");
ce78b96d
JB
7736 return PRINT_SRC_AND_LOC;
7737}
7738
4a64f543
MS
7739/* Implement the "print_one" breakpoint_ops method for fork
7740 catchpoints. */
ce78b96d
JB
7741
7742static void
a6d9a66e 7743print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7744{
e29a4733 7745 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7746 struct value_print_options opts;
79a45e25 7747 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7748
7749 get_user_print_options (&opts);
7750
4a64f543
MS
7751 /* Field 4, the address, is omitted (which makes the columns not
7752 line up too nicely with the headers, but the effect is relatively
7753 readable). */
79a45b7d 7754 if (opts.addressprint)
ce78b96d
JB
7755 ui_out_field_skip (uiout, "addr");
7756 annotate_field (5);
7757 ui_out_text (uiout, "fork");
e29a4733 7758 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7759 {
7760 ui_out_text (uiout, ", process ");
7761 ui_out_field_int (uiout, "what",
e29a4733 7762 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7763 ui_out_spaces (uiout, 1);
7764 }
8ac3646f
TT
7765
7766 if (ui_out_is_mi_like_p (uiout))
7767 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
7768}
7769
7770/* Implement the "print_mention" breakpoint_ops method for fork
7771 catchpoints. */
7772
7773static void
7774print_mention_catch_fork (struct breakpoint *b)
7775{
7776 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7777}
7778
6149aea9
PA
7779/* Implement the "print_recreate" breakpoint_ops method for fork
7780 catchpoints. */
7781
7782static void
7783print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7784{
7785 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7786 print_recreate_thread (b, fp);
6149aea9
PA
7787}
7788
ce78b96d
JB
7789/* The breakpoint_ops structure to be used in fork catchpoints. */
7790
2060206e 7791static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7792
4a64f543
MS
7793/* Implement the "insert" breakpoint_ops method for vfork
7794 catchpoints. */
ce78b96d 7795
77b06cd7
TJB
7796static int
7797insert_catch_vfork (struct bp_location *bl)
ce78b96d 7798{
dfd4cc63 7799 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7800}
7801
4a64f543
MS
7802/* Implement the "remove" breakpoint_ops method for vfork
7803 catchpoints. */
ce78b96d
JB
7804
7805static int
77b06cd7 7806remove_catch_vfork (struct bp_location *bl)
ce78b96d 7807{
dfd4cc63 7808 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7809}
7810
7811/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7812 catchpoints. */
7813
7814static int
f1310107 7815breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
7816 struct address_space *aspace, CORE_ADDR bp_addr,
7817 const struct target_waitstatus *ws)
ce78b96d 7818{
e29a4733
PA
7819 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7820
f90263c1
TT
7821 if (ws->kind != TARGET_WAITKIND_VFORKED)
7822 return 0;
7823
7824 c->forked_inferior_pid = ws->value.related_pid;
7825 return 1;
ce78b96d
JB
7826}
7827
4a64f543
MS
7828/* Implement the "print_it" breakpoint_ops method for vfork
7829 catchpoints. */
ce78b96d
JB
7830
7831static enum print_stop_action
348d480f 7832print_it_catch_vfork (bpstat bs)
ce78b96d 7833{
36dfb11c 7834 struct ui_out *uiout = current_uiout;
348d480f 7835 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7836 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7837
ce78b96d 7838 annotate_catchpoint (b->number);
36dfb11c
TT
7839 if (b->disposition == disp_del)
7840 ui_out_text (uiout, "\nTemporary catchpoint ");
7841 else
7842 ui_out_text (uiout, "\nCatchpoint ");
7843 if (ui_out_is_mi_like_p (uiout))
7844 {
7845 ui_out_field_string (uiout, "reason",
7846 async_reason_lookup (EXEC_ASYNC_VFORK));
7847 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7848 }
7849 ui_out_field_int (uiout, "bkptno", b->number);
7850 ui_out_text (uiout, " (vforked process ");
7851 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7852 ui_out_text (uiout, "), ");
ce78b96d
JB
7853 return PRINT_SRC_AND_LOC;
7854}
7855
4a64f543
MS
7856/* Implement the "print_one" breakpoint_ops method for vfork
7857 catchpoints. */
ce78b96d
JB
7858
7859static void
a6d9a66e 7860print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7861{
e29a4733 7862 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7863 struct value_print_options opts;
79a45e25 7864 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7865
7866 get_user_print_options (&opts);
4a64f543
MS
7867 /* Field 4, the address, is omitted (which makes the columns not
7868 line up too nicely with the headers, but the effect is relatively
7869 readable). */
79a45b7d 7870 if (opts.addressprint)
ce78b96d
JB
7871 ui_out_field_skip (uiout, "addr");
7872 annotate_field (5);
7873 ui_out_text (uiout, "vfork");
e29a4733 7874 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7875 {
7876 ui_out_text (uiout, ", process ");
7877 ui_out_field_int (uiout, "what",
e29a4733 7878 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7879 ui_out_spaces (uiout, 1);
7880 }
8ac3646f
TT
7881
7882 if (ui_out_is_mi_like_p (uiout))
7883 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
7884}
7885
7886/* Implement the "print_mention" breakpoint_ops method for vfork
7887 catchpoints. */
7888
7889static void
7890print_mention_catch_vfork (struct breakpoint *b)
7891{
7892 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7893}
7894
6149aea9
PA
7895/* Implement the "print_recreate" breakpoint_ops method for vfork
7896 catchpoints. */
7897
7898static void
7899print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7900{
7901 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7902 print_recreate_thread (b, fp);
6149aea9
PA
7903}
7904
ce78b96d
JB
7905/* The breakpoint_ops structure to be used in vfork catchpoints. */
7906
2060206e 7907static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7908
edcc5120
TT
7909/* An instance of this type is used to represent an solib catchpoint.
7910 It includes a "struct breakpoint" as a kind of base class; users
7911 downcast to "struct breakpoint *" when needed. A breakpoint is
7912 really of this type iff its ops pointer points to
7913 CATCH_SOLIB_BREAKPOINT_OPS. */
7914
7915struct solib_catchpoint
7916{
7917 /* The base class. */
7918 struct breakpoint base;
7919
7920 /* True for "catch load", false for "catch unload". */
7921 unsigned char is_load;
7922
7923 /* Regular expression to match, if any. COMPILED is only valid when
7924 REGEX is non-NULL. */
7925 char *regex;
7926 regex_t compiled;
7927};
7928
7929static void
7930dtor_catch_solib (struct breakpoint *b)
7931{
7932 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7933
7934 if (self->regex)
7935 regfree (&self->compiled);
7936 xfree (self->regex);
7937
7938 base_breakpoint_ops.dtor (b);
7939}
7940
7941static int
7942insert_catch_solib (struct bp_location *ignore)
7943{
7944 return 0;
7945}
7946
7947static int
7948remove_catch_solib (struct bp_location *ignore)
7949{
7950 return 0;
7951}
7952
7953static int
7954breakpoint_hit_catch_solib (const struct bp_location *bl,
7955 struct address_space *aspace,
7956 CORE_ADDR bp_addr,
7957 const struct target_waitstatus *ws)
7958{
7959 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7960 struct breakpoint *other;
7961
7962 if (ws->kind == TARGET_WAITKIND_LOADED)
7963 return 1;
7964
7965 ALL_BREAKPOINTS (other)
7966 {
7967 struct bp_location *other_bl;
7968
7969 if (other == bl->owner)
7970 continue;
7971
7972 if (other->type != bp_shlib_event)
7973 continue;
7974
7975 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7976 continue;
7977
7978 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7979 {
7980 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7981 return 1;
7982 }
7983 }
7984
7985 return 0;
7986}
7987
7988static void
7989check_status_catch_solib (struct bpstats *bs)
7990{
7991 struct solib_catchpoint *self
7992 = (struct solib_catchpoint *) bs->breakpoint_at;
7993 int ix;
7994
7995 if (self->is_load)
7996 {
7997 struct so_list *iter;
7998
7999 for (ix = 0;
8000 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8001 ix, iter);
8002 ++ix)
8003 {
8004 if (!self->regex
8005 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8006 return;
8007 }
8008 }
8009 else
8010 {
8011 char *iter;
8012
8013 for (ix = 0;
8014 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8015 ix, iter);
8016 ++ix)
8017 {
8018 if (!self->regex
8019 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8020 return;
8021 }
8022 }
8023
8024 bs->stop = 0;
8025 bs->print_it = print_it_noop;
8026}
8027
8028static enum print_stop_action
8029print_it_catch_solib (bpstat bs)
8030{
8031 struct breakpoint *b = bs->breakpoint_at;
8032 struct ui_out *uiout = current_uiout;
8033
8034 annotate_catchpoint (b->number);
8035 if (b->disposition == disp_del)
8036 ui_out_text (uiout, "\nTemporary catchpoint ");
8037 else
8038 ui_out_text (uiout, "\nCatchpoint ");
8039 ui_out_field_int (uiout, "bkptno", b->number);
8040 ui_out_text (uiout, "\n");
8041 if (ui_out_is_mi_like_p (uiout))
8042 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8043 print_solib_event (1);
8044 return PRINT_SRC_AND_LOC;
8045}
8046
8047static void
8048print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8049{
8050 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8051 struct value_print_options opts;
8052 struct ui_out *uiout = current_uiout;
8053 char *msg;
8054
8055 get_user_print_options (&opts);
8056 /* Field 4, the address, is omitted (which makes the columns not
8057 line up too nicely with the headers, but the effect is relatively
8058 readable). */
8059 if (opts.addressprint)
8060 {
8061 annotate_field (4);
8062 ui_out_field_skip (uiout, "addr");
8063 }
8064
8065 annotate_field (5);
8066 if (self->is_load)
8067 {
8068 if (self->regex)
8069 msg = xstrprintf (_("load of library matching %s"), self->regex);
8070 else
8071 msg = xstrdup (_("load of library"));
8072 }
8073 else
8074 {
8075 if (self->regex)
8076 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8077 else
8078 msg = xstrdup (_("unload of library"));
8079 }
8080 ui_out_field_string (uiout, "what", msg);
8081 xfree (msg);
8ac3646f
TT
8082
8083 if (ui_out_is_mi_like_p (uiout))
8084 ui_out_field_string (uiout, "catch-type",
8085 self->is_load ? "load" : "unload");
edcc5120
TT
8086}
8087
8088static void
8089print_mention_catch_solib (struct breakpoint *b)
8090{
8091 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8092
8093 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8094 self->is_load ? "load" : "unload");
8095}
8096
8097static void
8098print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8099{
8100 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8101
8102 fprintf_unfiltered (fp, "%s %s",
8103 b->disposition == disp_del ? "tcatch" : "catch",
8104 self->is_load ? "load" : "unload");
8105 if (self->regex)
8106 fprintf_unfiltered (fp, " %s", self->regex);
8107 fprintf_unfiltered (fp, "\n");
8108}
8109
8110static struct breakpoint_ops catch_solib_breakpoint_ops;
8111
91985142
MG
8112/* Shared helper function (MI and CLI) for creating and installing
8113 a shared object event catchpoint. If IS_LOAD is non-zero then
8114 the events to be caught are load events, otherwise they are
8115 unload events. If IS_TEMP is non-zero the catchpoint is a
8116 temporary one. If ENABLED is non-zero the catchpoint is
8117 created in an enabled state. */
edcc5120 8118
91985142
MG
8119void
8120add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8121{
8122 struct solib_catchpoint *c;
8123 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8124 struct cleanup *cleanup;
8125
edcc5120
TT
8126 if (!arg)
8127 arg = "";
8128 arg = skip_spaces (arg);
8129
8130 c = XCNEW (struct solib_catchpoint);
8131 cleanup = make_cleanup (xfree, c);
8132
8133 if (*arg != '\0')
8134 {
8135 int errcode;
8136
8137 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8138 if (errcode != 0)
8139 {
8140 char *err = get_regcomp_error (errcode, &c->compiled);
8141
8142 make_cleanup (xfree, err);
8143 error (_("Invalid regexp (%s): %s"), err, arg);
8144 }
8145 c->regex = xstrdup (arg);
8146 }
8147
8148 c->is_load = is_load;
91985142 8149 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8150 &catch_solib_breakpoint_ops);
8151
91985142
MG
8152 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8153
edcc5120
TT
8154 discard_cleanups (cleanup);
8155 install_breakpoint (0, &c->base, 1);
8156}
8157
91985142
MG
8158/* A helper function that does all the work for "catch load" and
8159 "catch unload". */
8160
8161static void
8162catch_load_or_unload (char *arg, int from_tty, int is_load,
8163 struct cmd_list_element *command)
8164{
8165 int tempflag;
8166 const int enabled = 1;
8167
8168 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8169
8170 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8171}
8172
edcc5120
TT
8173static void
8174catch_load_command_1 (char *arg, int from_tty,
8175 struct cmd_list_element *command)
8176{
8177 catch_load_or_unload (arg, from_tty, 1, command);
8178}
8179
8180static void
8181catch_unload_command_1 (char *arg, int from_tty,
8182 struct cmd_list_element *command)
8183{
8184 catch_load_or_unload (arg, from_tty, 0, command);
8185}
8186
be5c67c1
PA
8187/* An instance of this type is used to represent a syscall catchpoint.
8188 It includes a "struct breakpoint" as a kind of base class; users
8189 downcast to "struct breakpoint *" when needed. A breakpoint is
8190 really of this type iff its ops pointer points to
8191 CATCH_SYSCALL_BREAKPOINT_OPS. */
8192
8193struct syscall_catchpoint
8194{
8195 /* The base class. */
8196 struct breakpoint base;
8197
8198 /* Syscall numbers used for the 'catch syscall' feature. If no
8199 syscall has been specified for filtering, its value is NULL.
8200 Otherwise, it holds a list of all syscalls to be caught. The
8201 list elements are allocated with xmalloc. */
8202 VEC(int) *syscalls_to_be_caught;
8203};
8204
8205/* Implement the "dtor" breakpoint_ops method for syscall
8206 catchpoints. */
8207
8208static void
8209dtor_catch_syscall (struct breakpoint *b)
8210{
8211 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8212
8213 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8214
2060206e 8215 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8216}
8217
fa3064dd
YQ
8218static const struct inferior_data *catch_syscall_inferior_data = NULL;
8219
8220struct catch_syscall_inferior_data
8221{
8222 /* We keep a count of the number of times the user has requested a
8223 particular syscall to be tracked, and pass this information to the
8224 target. This lets capable targets implement filtering directly. */
8225
8226 /* Number of times that "any" syscall is requested. */
8227 int any_syscall_count;
8228
8229 /* Count of each system call. */
8230 VEC(int) *syscalls_counts;
8231
8232 /* This counts all syscall catch requests, so we can readily determine
8233 if any catching is necessary. */
8234 int total_syscalls_count;
8235};
8236
8237static struct catch_syscall_inferior_data*
8238get_catch_syscall_inferior_data (struct inferior *inf)
8239{
8240 struct catch_syscall_inferior_data *inf_data;
8241
8242 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8243 if (inf_data == NULL)
8244 {
41bf6aca 8245 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8246 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8247 }
8248
8249 return inf_data;
8250}
8251
8252static void
8253catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8254{
8255 xfree (arg);
8256}
8257
8258
a96d9b2e
SDJ
8259/* Implement the "insert" breakpoint_ops method for syscall
8260 catchpoints. */
8261
77b06cd7
TJB
8262static int
8263insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8264{
be5c67c1 8265 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8266 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8267 struct catch_syscall_inferior_data *inf_data
8268 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8269
fa3064dd 8270 ++inf_data->total_syscalls_count;
be5c67c1 8271 if (!c->syscalls_to_be_caught)
fa3064dd 8272 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8273 else
8274 {
8275 int i, iter;
cc59ec59 8276
a96d9b2e 8277 for (i = 0;
be5c67c1 8278 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8279 i++)
8280 {
8281 int elem;
cc59ec59 8282
fa3064dd 8283 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8284 {
fa3064dd 8285 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8286 uintptr_t vec_addr_offset
8287 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8288 uintptr_t vec_addr;
fa3064dd
YQ
8289 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8290 vec_addr = ((uintptr_t) VEC_address (int,
8291 inf_data->syscalls_counts)
8292 + vec_addr_offset);
a96d9b2e
SDJ
8293 memset ((void *) vec_addr, 0,
8294 (iter + 1 - old_size) * sizeof (int));
8295 }
fa3064dd
YQ
8296 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8297 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8298 }
8299 }
8300
dfd4cc63 8301 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8302 inf_data->total_syscalls_count != 0,
8303 inf_data->any_syscall_count,
8304 VEC_length (int,
8305 inf_data->syscalls_counts),
8306 VEC_address (int,
8307 inf_data->syscalls_counts));
a96d9b2e
SDJ
8308}
8309
8310/* Implement the "remove" breakpoint_ops method for syscall
8311 catchpoints. */
8312
8313static int
77b06cd7 8314remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8315{
be5c67c1 8316 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8317 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8318 struct catch_syscall_inferior_data *inf_data
8319 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8320
fa3064dd 8321 --inf_data->total_syscalls_count;
be5c67c1 8322 if (!c->syscalls_to_be_caught)
fa3064dd 8323 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8324 else
8325 {
8326 int i, iter;
cc59ec59 8327
a96d9b2e 8328 for (i = 0;
be5c67c1 8329 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8330 i++)
8331 {
8332 int elem;
fa3064dd 8333 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8334 /* Shouldn't happen. */
8335 continue;
fa3064dd
YQ
8336 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8337 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8338 }
8339 }
8340
dfd4cc63 8341 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8342 inf_data->total_syscalls_count != 0,
8343 inf_data->any_syscall_count,
8344 VEC_length (int,
8345 inf_data->syscalls_counts),
3e43a32a 8346 VEC_address (int,
fa3064dd 8347 inf_data->syscalls_counts));
a96d9b2e
SDJ
8348}
8349
8350/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8351 catchpoints. */
8352
8353static int
f1310107 8354breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8355 struct address_space *aspace, CORE_ADDR bp_addr,
8356 const struct target_waitstatus *ws)
a96d9b2e 8357{
4a64f543
MS
8358 /* We must check if we are catching specific syscalls in this
8359 breakpoint. If we are, then we must guarantee that the called
8360 syscall is the same syscall we are catching. */
a96d9b2e 8361 int syscall_number = 0;
be5c67c1
PA
8362 const struct syscall_catchpoint *c
8363 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8364
f90263c1
TT
8365 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8366 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8367 return 0;
8368
f90263c1
TT
8369 syscall_number = ws->value.syscall_number;
8370
a96d9b2e 8371 /* Now, checking if the syscall is the same. */
be5c67c1 8372 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8373 {
8374 int i, iter;
cc59ec59 8375
a96d9b2e 8376 for (i = 0;
be5c67c1 8377 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8378 i++)
8379 if (syscall_number == iter)
4924df79
GKB
8380 return 1;
8381
8382 return 0;
a96d9b2e
SDJ
8383 }
8384
8385 return 1;
8386}
8387
8388/* Implement the "print_it" breakpoint_ops method for syscall
8389 catchpoints. */
8390
8391static enum print_stop_action
348d480f 8392print_it_catch_syscall (bpstat bs)
a96d9b2e 8393{
36dfb11c 8394 struct ui_out *uiout = current_uiout;
348d480f 8395 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8396 /* These are needed because we want to know in which state a
8397 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8398 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8399 must print "called syscall" or "returned from syscall". */
8400 ptid_t ptid;
8401 struct target_waitstatus last;
8402 struct syscall s;
a96d9b2e
SDJ
8403
8404 get_last_target_status (&ptid, &last);
8405
8406 get_syscall_by_number (last.value.syscall_number, &s);
8407
8408 annotate_catchpoint (b->number);
8409
36dfb11c
TT
8410 if (b->disposition == disp_del)
8411 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8412 else
36dfb11c
TT
8413 ui_out_text (uiout, "\nCatchpoint ");
8414 if (ui_out_is_mi_like_p (uiout))
8415 {
8416 ui_out_field_string (uiout, "reason",
8417 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8418 ? EXEC_ASYNC_SYSCALL_ENTRY
8419 : EXEC_ASYNC_SYSCALL_RETURN));
8420 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8421 }
8422 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8423
8424 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8425 ui_out_text (uiout, " (call to syscall ");
8426 else
8427 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8428
36dfb11c
TT
8429 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8430 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8431 if (s.name != NULL)
8432 ui_out_field_string (uiout, "syscall-name", s.name);
8433
8434 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8435
8436 return PRINT_SRC_AND_LOC;
8437}
8438
8439/* Implement the "print_one" breakpoint_ops method for syscall
8440 catchpoints. */
8441
8442static void
8443print_one_catch_syscall (struct breakpoint *b,
f1310107 8444 struct bp_location **last_loc)
a96d9b2e 8445{
be5c67c1 8446 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8447 struct value_print_options opts;
79a45e25 8448 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
8449
8450 get_user_print_options (&opts);
4a64f543
MS
8451 /* Field 4, the address, is omitted (which makes the columns not
8452 line up too nicely with the headers, but the effect is relatively
8453 readable). */
a96d9b2e
SDJ
8454 if (opts.addressprint)
8455 ui_out_field_skip (uiout, "addr");
8456 annotate_field (5);
8457
be5c67c1
PA
8458 if (c->syscalls_to_be_caught
8459 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8460 ui_out_text (uiout, "syscalls \"");
8461 else
8462 ui_out_text (uiout, "syscall \"");
8463
be5c67c1 8464 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8465 {
8466 int i, iter;
8467 char *text = xstrprintf ("%s", "");
cc59ec59 8468
a96d9b2e 8469 for (i = 0;
be5c67c1 8470 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8471 i++)
8472 {
8473 char *x = text;
8474 struct syscall s;
8475 get_syscall_by_number (iter, &s);
8476
8477 if (s.name != NULL)
8478 text = xstrprintf ("%s%s, ", text, s.name);
8479 else
8480 text = xstrprintf ("%s%d, ", text, iter);
8481
8482 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8483 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8484 on every call. */
8485 xfree (x);
8486 }
8487 /* Remove the last comma. */
8488 text[strlen (text) - 2] = '\0';
8489 ui_out_field_string (uiout, "what", text);
8490 }
8491 else
8492 ui_out_field_string (uiout, "what", "<any syscall>");
8493 ui_out_text (uiout, "\" ");
8ac3646f
TT
8494
8495 if (ui_out_is_mi_like_p (uiout))
8496 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8497}
8498
8499/* Implement the "print_mention" breakpoint_ops method for syscall
8500 catchpoints. */
8501
8502static void
8503print_mention_catch_syscall (struct breakpoint *b)
8504{
be5c67c1
PA
8505 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8506
8507 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8508 {
8509 int i, iter;
8510
be5c67c1 8511 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8512 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8513 else
8514 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8515
8516 for (i = 0;
be5c67c1 8517 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8518 i++)
8519 {
8520 struct syscall s;
8521 get_syscall_by_number (iter, &s);
8522
8523 if (s.name)
8524 printf_filtered (" '%s' [%d]", s.name, s.number);
8525 else
8526 printf_filtered (" %d", s.number);
8527 }
8528 printf_filtered (")");
8529 }
8530 else
8531 printf_filtered (_("Catchpoint %d (any syscall)"),
8532 b->number);
8533}
8534
6149aea9
PA
8535/* Implement the "print_recreate" breakpoint_ops method for syscall
8536 catchpoints. */
8537
8538static void
8539print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8540{
be5c67c1
PA
8541 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8542
6149aea9
PA
8543 fprintf_unfiltered (fp, "catch syscall");
8544
be5c67c1 8545 if (c->syscalls_to_be_caught)
6149aea9
PA
8546 {
8547 int i, iter;
8548
8549 for (i = 0;
be5c67c1 8550 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8551 i++)
8552 {
8553 struct syscall s;
8554
8555 get_syscall_by_number (iter, &s);
8556 if (s.name)
8557 fprintf_unfiltered (fp, " %s", s.name);
8558 else
8559 fprintf_unfiltered (fp, " %d", s.number);
8560 }
8561 }
d9b3f62e 8562 print_recreate_thread (b, fp);
6149aea9
PA
8563}
8564
a96d9b2e
SDJ
8565/* The breakpoint_ops structure to be used in syscall catchpoints. */
8566
2060206e 8567static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8568
8569/* Returns non-zero if 'b' is a syscall catchpoint. */
8570
8571static int
8572syscall_catchpoint_p (struct breakpoint *b)
8573{
8574 return (b->ops == &catch_syscall_breakpoint_ops);
8575}
8576
346774a9
PA
8577/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8578 is non-zero, then make the breakpoint temporary. If COND_STRING is
8579 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8580 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8581
ab04a2af 8582void
346774a9
PA
8583init_catchpoint (struct breakpoint *b,
8584 struct gdbarch *gdbarch, int tempflag,
8585 char *cond_string,
c0a91b2b 8586 const struct breakpoint_ops *ops)
c906108c 8587{
c5aa993b 8588 struct symtab_and_line sal;
346774a9 8589
fe39c653 8590 init_sal (&sal);
6c95b8df 8591 sal.pspace = current_program_space;
c5aa993b 8592
28010a5d 8593 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8594
1b36a34b 8595 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8596 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8597}
8598
28010a5d 8599void
3ea46bff 8600install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8601{
8602 add_to_breakpoint_chain (b);
3a5c3e22 8603 set_breakpoint_number (internal, b);
558a9d82
YQ
8604 if (is_tracepoint (b))
8605 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8606 if (!internal)
8607 mention (b);
c56053d2 8608 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8609
8610 if (update_gll)
8611 update_global_location_list (1);
c56053d2
PA
8612}
8613
9b70b993 8614static void
a6d9a66e
UW
8615create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8616 int tempflag, char *cond_string,
c0a91b2b 8617 const struct breakpoint_ops *ops)
c906108c 8618{
e29a4733 8619 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8620
e29a4733
PA
8621 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8622
8623 c->forked_inferior_pid = null_ptid;
8624
3ea46bff 8625 install_breakpoint (0, &c->base, 1);
c906108c
SS
8626}
8627
fe798b75
JB
8628/* Exec catchpoints. */
8629
b4d90040
PA
8630/* An instance of this type is used to represent an exec catchpoint.
8631 It includes a "struct breakpoint" as a kind of base class; users
8632 downcast to "struct breakpoint *" when needed. A breakpoint is
8633 really of this type iff its ops pointer points to
8634 CATCH_EXEC_BREAKPOINT_OPS. */
8635
8636struct exec_catchpoint
8637{
8638 /* The base class. */
8639 struct breakpoint base;
8640
8641 /* Filename of a program whose exec triggered this catchpoint.
8642 This field is only valid immediately after this catchpoint has
8643 triggered. */
8644 char *exec_pathname;
8645};
8646
8647/* Implement the "dtor" breakpoint_ops method for exec
8648 catchpoints. */
8649
8650static void
8651dtor_catch_exec (struct breakpoint *b)
8652{
8653 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8654
8655 xfree (c->exec_pathname);
348d480f 8656
2060206e 8657 base_breakpoint_ops.dtor (b);
b4d90040
PA
8658}
8659
77b06cd7
TJB
8660static int
8661insert_catch_exec (struct bp_location *bl)
c906108c 8662{
dfd4cc63 8663 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8664}
c906108c 8665
fe798b75 8666static int
77b06cd7 8667remove_catch_exec (struct bp_location *bl)
fe798b75 8668{
dfd4cc63 8669 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8670}
c906108c 8671
fe798b75 8672static int
f1310107 8673breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8674 struct address_space *aspace, CORE_ADDR bp_addr,
8675 const struct target_waitstatus *ws)
fe798b75 8676{
b4d90040
PA
8677 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8678
f90263c1
TT
8679 if (ws->kind != TARGET_WAITKIND_EXECD)
8680 return 0;
8681
8682 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8683 return 1;
fe798b75 8684}
c906108c 8685
fe798b75 8686static enum print_stop_action
348d480f 8687print_it_catch_exec (bpstat bs)
fe798b75 8688{
36dfb11c 8689 struct ui_out *uiout = current_uiout;
348d480f 8690 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8691 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8692
fe798b75 8693 annotate_catchpoint (b->number);
36dfb11c
TT
8694 if (b->disposition == disp_del)
8695 ui_out_text (uiout, "\nTemporary catchpoint ");
8696 else
8697 ui_out_text (uiout, "\nCatchpoint ");
8698 if (ui_out_is_mi_like_p (uiout))
8699 {
8700 ui_out_field_string (uiout, "reason",
8701 async_reason_lookup (EXEC_ASYNC_EXEC));
8702 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8703 }
8704 ui_out_field_int (uiout, "bkptno", b->number);
8705 ui_out_text (uiout, " (exec'd ");
8706 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8707 ui_out_text (uiout, "), ");
8708
fe798b75 8709 return PRINT_SRC_AND_LOC;
c906108c
SS
8710}
8711
fe798b75 8712static void
a6d9a66e 8713print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8714{
b4d90040 8715 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8716 struct value_print_options opts;
79a45e25 8717 struct ui_out *uiout = current_uiout;
fe798b75
JB
8718
8719 get_user_print_options (&opts);
8720
8721 /* Field 4, the address, is omitted (which makes the columns
8722 not line up too nicely with the headers, but the effect
8723 is relatively readable). */
8724 if (opts.addressprint)
8725 ui_out_field_skip (uiout, "addr");
8726 annotate_field (5);
8727 ui_out_text (uiout, "exec");
b4d90040 8728 if (c->exec_pathname != NULL)
fe798b75
JB
8729 {
8730 ui_out_text (uiout, ", program \"");
b4d90040 8731 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8732 ui_out_text (uiout, "\" ");
8733 }
8ac3646f
TT
8734
8735 if (ui_out_is_mi_like_p (uiout))
8736 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8737}
8738
8739static void
8740print_mention_catch_exec (struct breakpoint *b)
8741{
8742 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8743}
8744
6149aea9
PA
8745/* Implement the "print_recreate" breakpoint_ops method for exec
8746 catchpoints. */
8747
8748static void
8749print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8750{
8751 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8752 print_recreate_thread (b, fp);
6149aea9
PA
8753}
8754
2060206e 8755static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8756
a96d9b2e
SDJ
8757static void
8758create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 8759 const struct breakpoint_ops *ops)
a96d9b2e 8760{
be5c67c1 8761 struct syscall_catchpoint *c;
a96d9b2e 8762 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 8763
be5c67c1
PA
8764 c = XNEW (struct syscall_catchpoint);
8765 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8766 c->syscalls_to_be_caught = filter;
a96d9b2e 8767
3ea46bff 8768 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
8769}
8770
c906108c 8771static int
fba45db2 8772hw_breakpoint_used_count (void)
c906108c 8773{
c906108c 8774 int i = 0;
f1310107
TJB
8775 struct breakpoint *b;
8776 struct bp_location *bl;
c906108c
SS
8777
8778 ALL_BREAKPOINTS (b)
c5aa993b 8779 {
d6b74ac4 8780 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8781 for (bl = b->loc; bl; bl = bl->next)
8782 {
8783 /* Special types of hardware breakpoints may use more than
8784 one register. */
348d480f 8785 i += b->ops->resources_needed (bl);
f1310107 8786 }
c5aa993b 8787 }
c906108c
SS
8788
8789 return i;
8790}
8791
a1398e0c
PA
8792/* Returns the resources B would use if it were a hardware
8793 watchpoint. */
8794
c906108c 8795static int
a1398e0c 8796hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8797{
c906108c 8798 int i = 0;
e09342b5 8799 struct bp_location *bl;
c906108c 8800
a1398e0c
PA
8801 if (!breakpoint_enabled (b))
8802 return 0;
8803
8804 for (bl = b->loc; bl; bl = bl->next)
8805 {
8806 /* Special types of hardware watchpoints may use more than
8807 one register. */
8808 i += b->ops->resources_needed (bl);
8809 }
8810
8811 return i;
8812}
8813
8814/* Returns the sum the used resources of all hardware watchpoints of
8815 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8816 the sum of the used resources of all hardware watchpoints of other
8817 types _not_ TYPE. */
8818
8819static int
8820hw_watchpoint_used_count_others (struct breakpoint *except,
8821 enum bptype type, int *other_type_used)
8822{
8823 int i = 0;
8824 struct breakpoint *b;
8825
c906108c
SS
8826 *other_type_used = 0;
8827 ALL_BREAKPOINTS (b)
e09342b5 8828 {
a1398e0c
PA
8829 if (b == except)
8830 continue;
e09342b5
TJB
8831 if (!breakpoint_enabled (b))
8832 continue;
8833
a1398e0c
PA
8834 if (b->type == type)
8835 i += hw_watchpoint_use_count (b);
8836 else if (is_hardware_watchpoint (b))
8837 *other_type_used = 1;
e09342b5
TJB
8838 }
8839
c906108c
SS
8840 return i;
8841}
8842
c906108c 8843void
fba45db2 8844disable_watchpoints_before_interactive_call_start (void)
c906108c 8845{
c5aa993b 8846 struct breakpoint *b;
c906108c
SS
8847
8848 ALL_BREAKPOINTS (b)
c5aa993b 8849 {
cc60f2e3 8850 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8851 {
b5de0fa7 8852 b->enable_state = bp_call_disabled;
b60e7edf 8853 update_global_location_list (0);
c5aa993b
JM
8854 }
8855 }
c906108c
SS
8856}
8857
8858void
fba45db2 8859enable_watchpoints_after_interactive_call_stop (void)
c906108c 8860{
c5aa993b 8861 struct breakpoint *b;
c906108c
SS
8862
8863 ALL_BREAKPOINTS (b)
c5aa993b 8864 {
cc60f2e3 8865 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8866 {
b5de0fa7 8867 b->enable_state = bp_enabled;
b60e7edf 8868 update_global_location_list (1);
c5aa993b
JM
8869 }
8870 }
c906108c
SS
8871}
8872
8bea4e01
UW
8873void
8874disable_breakpoints_before_startup (void)
8875{
6c95b8df 8876 current_program_space->executing_startup = 1;
f8eba3c6 8877 update_global_location_list (0);
8bea4e01
UW
8878}
8879
8880void
8881enable_breakpoints_after_startup (void)
8882{
6c95b8df 8883 current_program_space->executing_startup = 0;
f8eba3c6 8884 breakpoint_re_set ();
8bea4e01
UW
8885}
8886
c906108c
SS
8887
8888/* Set a breakpoint that will evaporate an end of command
8889 at address specified by SAL.
8890 Restrict it to frame FRAME if FRAME is nonzero. */
8891
8892struct breakpoint *
a6d9a66e
UW
8893set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8894 struct frame_id frame_id, enum bptype type)
c906108c 8895{
52f0bd74 8896 struct breakpoint *b;
edb3359d 8897
193facb3
JK
8898 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8899 tail-called one. */
8900 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8901
06edf0c0 8902 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8903 b->enable_state = bp_enabled;
8904 b->disposition = disp_donttouch;
818dd999 8905 b->frame_id = frame_id;
c906108c 8906
4a64f543
MS
8907 /* If we're debugging a multi-threaded program, then we want
8908 momentary breakpoints to be active in only a single thread of
8909 control. */
39f77062
KB
8910 if (in_thread_list (inferior_ptid))
8911 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8912
b60e7edf 8913 update_global_location_list_nothrow (1);
74960c60 8914
c906108c
SS
8915 return b;
8916}
611c83ae 8917
06edf0c0
PA
8918/* Make a momentary breakpoint based on the master breakpoint ORIG.
8919 The new breakpoint will have type TYPE, and use OPS as it
8920 breakpoint_ops. */
e58b0e63 8921
06edf0c0
PA
8922static struct breakpoint *
8923momentary_breakpoint_from_master (struct breakpoint *orig,
8924 enum bptype type,
c0a91b2b 8925 const struct breakpoint_ops *ops)
e58b0e63
PA
8926{
8927 struct breakpoint *copy;
8928
06edf0c0 8929 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8930 copy->loc = allocate_bp_location (copy);
0e30163f 8931 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8932
a6d9a66e 8933 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8934 copy->loc->requested_address = orig->loc->requested_address;
8935 copy->loc->address = orig->loc->address;
8936 copy->loc->section = orig->loc->section;
6c95b8df 8937 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8938 copy->loc->probe = orig->loc->probe;
f8eba3c6 8939 copy->loc->line_number = orig->loc->line_number;
2f202fde 8940 copy->loc->symtab = orig->loc->symtab;
e58b0e63
PA
8941 copy->frame_id = orig->frame_id;
8942 copy->thread = orig->thread;
6c95b8df 8943 copy->pspace = orig->pspace;
e58b0e63
PA
8944
8945 copy->enable_state = bp_enabled;
8946 copy->disposition = disp_donttouch;
8947 copy->number = internal_breakpoint_number--;
8948
8949 update_global_location_list_nothrow (0);
8950 return copy;
8951}
8952
06edf0c0
PA
8953/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8954 ORIG is NULL. */
8955
8956struct breakpoint *
8957clone_momentary_breakpoint (struct breakpoint *orig)
8958{
8959 /* If there's nothing to clone, then return nothing. */
8960 if (orig == NULL)
8961 return NULL;
8962
8963 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8964}
8965
611c83ae 8966struct breakpoint *
a6d9a66e
UW
8967set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8968 enum bptype type)
611c83ae
PA
8969{
8970 struct symtab_and_line sal;
8971
8972 sal = find_pc_line (pc, 0);
8973 sal.pc = pc;
8974 sal.section = find_pc_overlay (pc);
8975 sal.explicit_pc = 1;
8976
a6d9a66e 8977 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8978}
c906108c 8979\f
c5aa993b 8980
c906108c
SS
8981/* Tell the user we have just set a breakpoint B. */
8982
8983static void
fba45db2 8984mention (struct breakpoint *b)
c906108c 8985{
348d480f 8986 b->ops->print_mention (b);
79a45e25 8987 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8988 return;
c906108c
SS
8989 printf_filtered ("\n");
8990}
c906108c 8991\f
c5aa993b 8992
0d381245 8993static struct bp_location *
39d61571 8994add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8995 const struct symtab_and_line *sal)
8996{
8997 struct bp_location *loc, **tmp;
3742cc8b
YQ
8998 CORE_ADDR adjusted_address;
8999 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9000
9001 if (loc_gdbarch == NULL)
9002 loc_gdbarch = b->gdbarch;
9003
9004 /* Adjust the breakpoint's address prior to allocating a location.
9005 Once we call allocate_bp_location(), that mostly uninitialized
9006 location will be placed on the location chain. Adjustment of the
9007 breakpoint may cause target_read_memory() to be called and we do
9008 not want its scan of the location chain to find a breakpoint and
9009 location that's only been partially initialized. */
9010 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9011 sal->pc, b->type);
0d381245 9012
d30113d4 9013 /* Sort the locations by their ADDRESS. */
39d61571 9014 loc = allocate_bp_location (b);
d30113d4
JK
9015 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9016 tmp = &((*tmp)->next))
0d381245 9017 ;
d30113d4 9018 loc->next = *tmp;
0d381245 9019 *tmp = loc;
3742cc8b 9020
0d381245 9021 loc->requested_address = sal->pc;
3742cc8b 9022 loc->address = adjusted_address;
6c95b8df 9023 loc->pspace = sal->pspace;
55aa24fb 9024 loc->probe = sal->probe;
6c95b8df 9025 gdb_assert (loc->pspace != NULL);
0d381245 9026 loc->section = sal->section;
3742cc8b 9027 loc->gdbarch = loc_gdbarch;
f8eba3c6 9028 loc->line_number = sal->line;
2f202fde 9029 loc->symtab = sal->symtab;
f8eba3c6 9030
0e30163f
JK
9031 set_breakpoint_location_function (loc,
9032 sal->explicit_pc || sal->explicit_line);
0d381245
VP
9033 return loc;
9034}
514f746b
AR
9035\f
9036
9037/* Return 1 if LOC is pointing to a permanent breakpoint,
9038 return 0 otherwise. */
9039
9040static int
9041bp_loc_is_permanent (struct bp_location *loc)
9042{
9043 int len;
9044 CORE_ADDR addr;
1afeeb75 9045 const gdb_byte *bpoint;
514f746b 9046 gdb_byte *target_mem;
939c61fa
JK
9047 struct cleanup *cleanup;
9048 int retval = 0;
514f746b
AR
9049
9050 gdb_assert (loc != NULL);
9051
9052 addr = loc->address;
1afeeb75 9053 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 9054
939c61fa 9055 /* Software breakpoints unsupported? */
1afeeb75 9056 if (bpoint == NULL)
939c61fa
JK
9057 return 0;
9058
514f746b
AR
9059 target_mem = alloca (len);
9060
939c61fa
JK
9061 /* Enable the automatic memory restoration from breakpoints while
9062 we read the memory. Otherwise we could say about our temporary
9063 breakpoints they are permanent. */
6c95b8df
PA
9064 cleanup = save_current_space_and_thread ();
9065
9066 switch_to_program_space_and_thread (loc->pspace);
9067 make_show_memory_breakpoints_cleanup (0);
939c61fa 9068
514f746b 9069 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 9070 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 9071 retval = 1;
514f746b 9072
939c61fa
JK
9073 do_cleanups (cleanup);
9074
9075 return retval;
514f746b
AR
9076}
9077
e7e0cddf
SS
9078/* Build a command list for the dprintf corresponding to the current
9079 settings of the dprintf style options. */
9080
9081static void
9082update_dprintf_command_list (struct breakpoint *b)
9083{
9084 char *dprintf_args = b->extra_string;
9085 char *printf_line = NULL;
9086
9087 if (!dprintf_args)
9088 return;
9089
9090 dprintf_args = skip_spaces (dprintf_args);
9091
9092 /* Allow a comma, as it may have terminated a location, but don't
9093 insist on it. */
9094 if (*dprintf_args == ',')
9095 ++dprintf_args;
9096 dprintf_args = skip_spaces (dprintf_args);
9097
9098 if (*dprintf_args != '"')
9099 error (_("Bad format string, missing '\"'."));
9100
d3ce09f5 9101 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9102 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9103 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9104 {
9105 if (!dprintf_function)
9106 error (_("No function supplied for dprintf call"));
9107
9108 if (dprintf_channel && strlen (dprintf_channel) > 0)
9109 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9110 dprintf_function,
9111 dprintf_channel,
9112 dprintf_args);
9113 else
9114 printf_line = xstrprintf ("call (void) %s (%s)",
9115 dprintf_function,
9116 dprintf_args);
9117 }
d3ce09f5
SS
9118 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9119 {
9120 if (target_can_run_breakpoint_commands ())
9121 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9122 else
9123 {
9124 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9125 printf_line = xstrprintf ("printf %s", dprintf_args);
9126 }
9127 }
e7e0cddf
SS
9128 else
9129 internal_error (__FILE__, __LINE__,
9130 _("Invalid dprintf style."));
9131
f28045c2 9132 gdb_assert (printf_line != NULL);
9d6e6e84 9133 /* Manufacture a printf sequence. */
f28045c2 9134 {
9d6e6e84
HZ
9135 struct command_line *printf_cmd_line
9136 = xmalloc (sizeof (struct command_line));
e7e0cddf 9137
f28045c2
YQ
9138 printf_cmd_line = xmalloc (sizeof (struct command_line));
9139 printf_cmd_line->control_type = simple_control;
9140 printf_cmd_line->body_count = 0;
9141 printf_cmd_line->body_list = NULL;
9d6e6e84 9142 printf_cmd_line->next = NULL;
f28045c2 9143 printf_cmd_line->line = printf_line;
e7e0cddf 9144
f28045c2
YQ
9145 breakpoint_set_commands (b, printf_cmd_line);
9146 }
e7e0cddf
SS
9147}
9148
9149/* Update all dprintf commands, making their command lists reflect
9150 current style settings. */
9151
9152static void
9153update_dprintf_commands (char *args, int from_tty,
9154 struct cmd_list_element *c)
9155{
9156 struct breakpoint *b;
9157
9158 ALL_BREAKPOINTS (b)
9159 {
9160 if (b->type == bp_dprintf)
9161 update_dprintf_command_list (b);
9162 }
9163}
c3f6f71d 9164
018d34a4
VP
9165/* Create a breakpoint with SAL as location. Use ADDR_STRING
9166 as textual description of the location, and COND_STRING
db107f19 9167 as condition expression. */
018d34a4
VP
9168
9169static void
d9b3f62e
PA
9170init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9171 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9172 char *filter, char *cond_string,
e7e0cddf 9173 char *extra_string,
d9b3f62e
PA
9174 enum bptype type, enum bpdisp disposition,
9175 int thread, int task, int ignore_count,
c0a91b2b 9176 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9177 int enabled, int internal, unsigned flags,
9178 int display_canonical)
018d34a4 9179{
0d381245 9180 int i;
018d34a4
VP
9181
9182 if (type == bp_hardware_breakpoint)
9183 {
fbbd034e
AS
9184 int target_resources_ok;
9185
9186 i = hw_breakpoint_used_count ();
9187 target_resources_ok =
9188 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9189 i + 1, 0);
9190 if (target_resources_ok == 0)
9191 error (_("No hardware breakpoint support in the target."));
9192 else if (target_resources_ok < 0)
9193 error (_("Hardware breakpoints used exceeds limit."));
9194 }
9195
6c95b8df
PA
9196 gdb_assert (sals.nelts > 0);
9197
0d381245
VP
9198 for (i = 0; i < sals.nelts; ++i)
9199 {
9200 struct symtab_and_line sal = sals.sals[i];
9201 struct bp_location *loc;
9202
9203 if (from_tty)
5af949e3
UW
9204 {
9205 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9206 if (!loc_gdbarch)
9207 loc_gdbarch = gdbarch;
9208
9209 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9210 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9211 }
0d381245
VP
9212
9213 if (i == 0)
9214 {
d9b3f62e 9215 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9216 b->thread = thread;
4a306c9a 9217 b->task = task;
855a6e68 9218
0d381245 9219 b->cond_string = cond_string;
e7e0cddf 9220 b->extra_string = extra_string;
0d381245 9221 b->ignore_count = ignore_count;
41447f92 9222 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9223 b->disposition = disposition;
6c95b8df 9224
44f238bb
PA
9225 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9226 b->loc->inserted = 1;
9227
0fb4aa4b
PA
9228 if (type == bp_static_tracepoint)
9229 {
d9b3f62e 9230 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9231 struct static_tracepoint_marker marker;
9232
983af33b 9233 if (strace_marker_p (b))
0fb4aa4b
PA
9234 {
9235 /* We already know the marker exists, otherwise, we
9236 wouldn't see a sal for it. */
9237 char *p = &addr_string[3];
9238 char *endp;
9239 char *marker_str;
0fb4aa4b 9240
e9cafbcc 9241 p = skip_spaces (p);
0fb4aa4b 9242
e9cafbcc 9243 endp = skip_to_space (p);
0fb4aa4b
PA
9244
9245 marker_str = savestring (p, endp - p);
d9b3f62e 9246 t->static_trace_marker_id = marker_str;
0fb4aa4b 9247
3e43a32a
MS
9248 printf_filtered (_("Probed static tracepoint "
9249 "marker \"%s\"\n"),
d9b3f62e 9250 t->static_trace_marker_id);
0fb4aa4b
PA
9251 }
9252 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9253 {
d9b3f62e 9254 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9255 release_static_tracepoint_marker (&marker);
9256
3e43a32a
MS
9257 printf_filtered (_("Probed static tracepoint "
9258 "marker \"%s\"\n"),
d9b3f62e 9259 t->static_trace_marker_id);
0fb4aa4b
PA
9260 }
9261 else
3e43a32a
MS
9262 warning (_("Couldn't determine the static "
9263 "tracepoint marker to probe"));
0fb4aa4b
PA
9264 }
9265
0d381245
VP
9266 loc = b->loc;
9267 }
9268 else
018d34a4 9269 {
39d61571 9270 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9271 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9272 loc->inserted = 1;
0d381245
VP
9273 }
9274
514f746b
AR
9275 if (bp_loc_is_permanent (loc))
9276 make_breakpoint_permanent (b);
9277
0d381245
VP
9278 if (b->cond_string)
9279 {
bbc13ae3
KS
9280 const char *arg = b->cond_string;
9281
1bb9788d
TT
9282 loc->cond = parse_exp_1 (&arg, loc->address,
9283 block_for_pc (loc->address), 0);
0d381245 9284 if (*arg)
588ae58c 9285 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9286 }
e7e0cddf
SS
9287
9288 /* Dynamic printf requires and uses additional arguments on the
9289 command line, otherwise it's an error. */
9290 if (type == bp_dprintf)
9291 {
9292 if (b->extra_string)
9293 update_dprintf_command_list (b);
9294 else
9295 error (_("Format string required"));
9296 }
9297 else if (b->extra_string)
588ae58c 9298 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9299 }
018d34a4 9300
56435ebe 9301 b->display_canonical = display_canonical;
018d34a4
VP
9302 if (addr_string)
9303 b->addr_string = addr_string;
9304 else
9305 /* addr_string has to be used or breakpoint_re_set will delete
9306 me. */
5af949e3
UW
9307 b->addr_string
9308 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9309 b->filter = filter;
d9b3f62e 9310}
018d34a4 9311
d9b3f62e
PA
9312static void
9313create_breakpoint_sal (struct gdbarch *gdbarch,
9314 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9315 char *filter, char *cond_string,
e7e0cddf 9316 char *extra_string,
d9b3f62e
PA
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
PA
9320 int enabled, int internal, unsigned flags,
9321 int display_canonical)
d9b3f62e
PA
9322{
9323 struct breakpoint *b;
9324 struct cleanup *old_chain;
9325
9326 if (is_tracepoint_type (type))
9327 {
9328 struct tracepoint *t;
9329
9330 t = XCNEW (struct tracepoint);
9331 b = &t->base;
9332 }
9333 else
9334 b = XNEW (struct breakpoint);
9335
9336 old_chain = make_cleanup (xfree, b);
9337
9338 init_breakpoint_sal (b, gdbarch,
9339 sals, addr_string,
e7e0cddf 9340 filter, cond_string, extra_string,
d9b3f62e
PA
9341 type, disposition,
9342 thread, task, ignore_count,
9343 ops, from_tty,
44f238bb
PA
9344 enabled, internal, flags,
9345 display_canonical);
d9b3f62e
PA
9346 discard_cleanups (old_chain);
9347
3ea46bff 9348 install_breakpoint (internal, b, 0);
018d34a4
VP
9349}
9350
9351/* Add SALS.nelts breakpoints to the breakpoint table. For each
9352 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9353 value. COND_STRING, if not NULL, specified the condition to be
9354 used for all breakpoints. Essentially the only case where
9355 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9356 function. In that case, it's still not possible to specify
9357 separate conditions for different overloaded functions, so
9358 we take just a single condition string.
9359
c3f6f71d 9360 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9361 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9362 array contents). If the function fails (error() is called), the
9363 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9364 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9365
9366static void
8cdf0e15 9367create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9368 struct linespec_result *canonical,
e7e0cddf 9369 char *cond_string, char *extra_string,
8cdf0e15
VP
9370 enum bptype type, enum bpdisp disposition,
9371 int thread, int task, int ignore_count,
c0a91b2b 9372 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9373 int enabled, int internal, unsigned flags)
c906108c 9374{
018d34a4 9375 int i;
f8eba3c6 9376 struct linespec_sals *lsal;
cc59ec59 9377
f8eba3c6
TT
9378 if (canonical->pre_expanded)
9379 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9380
9381 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9382 {
f8eba3c6
TT
9383 /* Note that 'addr_string' can be NULL in the case of a plain
9384 'break', without arguments. */
9385 char *addr_string = (canonical->addr_string
9386 ? xstrdup (canonical->addr_string)
9387 : NULL);
9388 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9389 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9390
f8eba3c6
TT
9391 make_cleanup (xfree, filter_string);
9392 create_breakpoint_sal (gdbarch, lsal->sals,
9393 addr_string,
9394 filter_string,
e7e0cddf
SS
9395 cond_string, extra_string,
9396 type, disposition,
84f4c1fe 9397 thread, task, ignore_count, ops,
44f238bb 9398 from_tty, enabled, internal, flags,
56435ebe 9399 canonical->special_display);
f8eba3c6 9400 discard_cleanups (inner);
c3f6f71d 9401 }
c3f6f71d 9402}
c906108c 9403
9998af43 9404/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9405 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9406 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9407 address strings. ADDRESS points to the end of the SAL.
9408
9409 The array and the line spec strings are allocated on the heap, it is
9410 the caller's responsibility to free them. */
c906108c 9411
b9362cc7 9412static void
c3f6f71d 9413parse_breakpoint_sals (char **address,
58438ac1 9414 struct linespec_result *canonical)
c3f6f71d 9415{
c3f6f71d 9416 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9417 breakpoint. */
c3f6f71d
JM
9418 if ((*address) == NULL
9419 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9420 {
1bfeeb0f
JL
9421 /* The last displayed codepoint, if it's valid, is our default breakpoint
9422 address. */
9423 if (last_displayed_sal_is_valid ())
c906108c 9424 {
f8eba3c6 9425 struct linespec_sals lsal;
c3f6f71d 9426 struct symtab_and_line sal;
1c8cdcb1 9427 CORE_ADDR pc;
cc59ec59 9428
4a64f543 9429 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9430 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9431 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9432
9433 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9434 corresponding to the last call to print_frame_info.
9435 Be sure to reinitialize LINE with NOTCURRENT == 0
9436 as the breakpoint line number is inappropriate otherwise.
9437 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9438 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9439 pc = sal.pc;
9440 sal = find_pc_line (pc, 0);
00903456 9441
4a64f543 9442 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9443 where PC is the last displayed codepoint's address. So
9444 make sure to set sal.explicit_pc to prevent GDB from
9445 trying to expand the list of sals to include all other
9446 instances with the same symtab and line. */
1c8cdcb1 9447 sal.pc = pc;
00903456
JK
9448 sal.explicit_pc = 1;
9449
f8eba3c6
TT
9450 lsal.sals.sals[0] = sal;
9451 lsal.sals.nelts = 1;
9452 lsal.canonical = NULL;
9453
9454 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9455 }
9456 else
8a3fe4f8 9457 error (_("No default breakpoint address now."));
c906108c
SS
9458 }
9459 else
9460 {
cc80f267
JK
9461 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9462
c906108c 9463 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9464 current_source_symtab (which is decode_line_1's default).
9465 This should produce the results we want almost all of the
cc80f267
JK
9466 time while leaving default_breakpoint_* alone.
9467
9468 ObjC: However, don't match an Objective-C method name which
9469 may have a '+' or '-' succeeded by a '['. */
9470 if (last_displayed_sal_is_valid ()
9471 && (!cursal.symtab
9472 || ((strchr ("+-", (*address)[0]) != NULL)
9473 && ((*address)[1] != '['))))
f8eba3c6
TT
9474 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9475 get_last_displayed_symtab (),
9476 get_last_displayed_line (),
9477 canonical, NULL, NULL);
c906108c 9478 else
f8eba3c6 9479 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9480 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9481 }
c3f6f71d 9482}
c906108c 9483
c906108c 9484
c3f6f71d 9485/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9486 inserted as a breakpoint. If it can't throw an error. */
c906108c 9487
b9362cc7 9488static void
23e7acfb 9489breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9490{
9491 int i;
cc59ec59 9492
c3f6f71d 9493 for (i = 0; i < sals->nelts; i++)
ee53e872 9494 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9495}
9496
7a697b8d
SS
9497/* Fast tracepoints may have restrictions on valid locations. For
9498 instance, a fast tracepoint using a jump instead of a trap will
9499 likely have to overwrite more bytes than a trap would, and so can
9500 only be placed where the instruction is longer than the jump, or a
9501 multi-instruction sequence does not have a jump into the middle of
9502 it, etc. */
9503
9504static void
9505check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9506 struct symtabs_and_lines *sals)
9507{
9508 int i, rslt;
9509 struct symtab_and_line *sal;
9510 char *msg;
9511 struct cleanup *old_chain;
9512
9513 for (i = 0; i < sals->nelts; i++)
9514 {
f8eba3c6
TT
9515 struct gdbarch *sarch;
9516
7a697b8d
SS
9517 sal = &sals->sals[i];
9518
f8eba3c6
TT
9519 sarch = get_sal_arch (*sal);
9520 /* We fall back to GDBARCH if there is no architecture
9521 associated with SAL. */
9522 if (sarch == NULL)
9523 sarch = gdbarch;
9524 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9525 NULL, &msg);
9526 old_chain = make_cleanup (xfree, msg);
9527
9528 if (!rslt)
9529 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9530 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9531
9532 do_cleanups (old_chain);
9533 }
9534}
9535
af4908ba
KS
9536/* Issue an invalid thread ID error. */
9537
9538static void ATTRIBUTE_NORETURN
9539invalid_thread_id_error (int id)
9540{
9541 error (_("Unknown thread %d."), id);
9542}
9543
018d34a4
VP
9544/* Given TOK, a string specification of condition and thread, as
9545 accepted by the 'break' command, extract the condition
9546 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9547 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9548 If no condition is found, *COND_STRING is set to NULL.
9549 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9550
9551static void
bbc13ae3 9552find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9553 char **cond_string, int *thread, int *task,
9554 char **rest)
018d34a4
VP
9555{
9556 *cond_string = NULL;
9557 *thread = -1;
ed1d1739
KS
9558 *task = 0;
9559 *rest = NULL;
9560
018d34a4
VP
9561 while (tok && *tok)
9562 {
bbc13ae3 9563 const char *end_tok;
018d34a4 9564 int toklen;
bbc13ae3
KS
9565 const char *cond_start = NULL;
9566 const char *cond_end = NULL;
cc59ec59 9567
bbc13ae3 9568 tok = skip_spaces_const (tok);
e7e0cddf
SS
9569
9570 if ((*tok == '"' || *tok == ',') && rest)
9571 {
9572 *rest = savestring (tok, strlen (tok));
9573 return;
9574 }
9575
bbc13ae3 9576 end_tok = skip_to_space_const (tok);
d634f2de 9577
018d34a4 9578 toklen = end_tok - tok;
d634f2de 9579
018d34a4
VP
9580 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9581 {
f7545552
TT
9582 struct expression *expr;
9583
018d34a4 9584 tok = cond_start = end_tok + 1;
1bb9788d 9585 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9586 xfree (expr);
018d34a4 9587 cond_end = tok;
d634f2de 9588 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9589 }
9590 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9591 {
9592 char *tmptok;
d634f2de 9593
018d34a4 9594 tok = end_tok + 1;
bbc13ae3 9595 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9596 if (tok == tmptok)
9597 error (_("Junk after thread keyword."));
9598 if (!valid_thread_id (*thread))
af4908ba 9599 invalid_thread_id_error (*thread);
bbc13ae3 9600 tok = tmptok;
018d34a4 9601 }
4a306c9a
JB
9602 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9603 {
9604 char *tmptok;
9605
9606 tok = end_tok + 1;
bbc13ae3 9607 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9608 if (tok == tmptok)
9609 error (_("Junk after task keyword."));
9610 if (!valid_task_id (*task))
b6199126 9611 error (_("Unknown task %d."), *task);
bbc13ae3 9612 tok = tmptok;
4a306c9a 9613 }
e7e0cddf
SS
9614 else if (rest)
9615 {
9616 *rest = savestring (tok, strlen (tok));
ccab2054 9617 return;
e7e0cddf 9618 }
018d34a4
VP
9619 else
9620 error (_("Junk at end of arguments."));
9621 }
9622}
9623
0fb4aa4b
PA
9624/* Decode a static tracepoint marker spec. */
9625
9626static struct symtabs_and_lines
9627decode_static_tracepoint_spec (char **arg_p)
9628{
9629 VEC(static_tracepoint_marker_p) *markers = NULL;
9630 struct symtabs_and_lines sals;
0fb4aa4b
PA
9631 struct cleanup *old_chain;
9632 char *p = &(*arg_p)[3];
9633 char *endp;
9634 char *marker_str;
9635 int i;
9636
e9cafbcc 9637 p = skip_spaces (p);
0fb4aa4b 9638
e9cafbcc 9639 endp = skip_to_space (p);
0fb4aa4b
PA
9640
9641 marker_str = savestring (p, endp - p);
9642 old_chain = make_cleanup (xfree, marker_str);
9643
9644 markers = target_static_tracepoint_markers_by_strid (marker_str);
9645 if (VEC_empty(static_tracepoint_marker_p, markers))
9646 error (_("No known static tracepoint marker named %s"), marker_str);
9647
9648 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9649 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9650
9651 for (i = 0; i < sals.nelts; i++)
9652 {
9653 struct static_tracepoint_marker *marker;
9654
9655 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9656
9657 init_sal (&sals.sals[i]);
9658
9659 sals.sals[i] = find_pc_line (marker->address, 0);
9660 sals.sals[i].pc = marker->address;
9661
9662 release_static_tracepoint_marker (marker);
9663 }
9664
9665 do_cleanups (old_chain);
9666
9667 *arg_p = endp;
9668 return sals;
9669}
9670
fd9b8c24
PA
9671/* Set a breakpoint. This function is shared between CLI and MI
9672 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9673 modes of operations, selected by the PARSE_ARG parameter. If
9674 non-zero, the function will parse ARG, extracting location,
9675 condition, thread and extra string. Otherwise, ARG is just the
9676 breakpoint's location, with condition, thread, and extra string
9677 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9678 If INTERNAL is non-zero, the breakpoint number will be allocated
9679 from the internal breakpoint count. Returns true if any breakpoint
9680 was created; false otherwise. */
0101ce28 9681
8cdf0e15
VP
9682int
9683create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9684 char *arg, char *cond_string,
9685 int thread, char *extra_string,
f6de8ec2 9686 int parse_arg,
0fb4aa4b 9687 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9688 int ignore_count,
9689 enum auto_boolean pending_break_support,
c0a91b2b 9690 const struct breakpoint_ops *ops,
44f238bb
PA
9691 int from_tty, int enabled, int internal,
9692 unsigned flags)
c3f6f71d 9693{
b78a6381 9694 volatile struct gdb_exception e;
f8eba3c6 9695 char *copy_arg = NULL;
c3f6f71d 9696 char *addr_start = arg;
7efd8fc2 9697 struct linespec_result canonical;
c3f6f71d 9698 struct cleanup *old_chain;
80c99de1 9699 struct cleanup *bkpt_chain = NULL;
0101ce28 9700 int pending = 0;
4a306c9a 9701 int task = 0;
86b17b60 9702 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9703
348d480f
PA
9704 gdb_assert (ops != NULL);
9705
7efd8fc2 9706 init_linespec_result (&canonical);
c3f6f71d 9707
b78a6381
TT
9708 TRY_CATCH (e, RETURN_MASK_ALL)
9709 {
983af33b
SDJ
9710 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9711 addr_start, &copy_arg);
b78a6381 9712 }
0101ce28
JJ
9713
9714 /* If caller is interested in rc value from parse, set value. */
05ff989b 9715 switch (e.reason)
0101ce28 9716 {
983af33b
SDJ
9717 case GDB_NO_ERROR:
9718 if (VEC_empty (linespec_sals, canonical.sals))
9719 return 0;
9720 break;
05ff989b
AC
9721 case RETURN_ERROR:
9722 switch (e.error)
0101ce28 9723 {
05ff989b 9724 case NOT_FOUND_ERROR:
0101ce28 9725
05ff989b
AC
9726 /* If pending breakpoint support is turned off, throw
9727 error. */
fa8d40ab
JJ
9728
9729 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9730 throw_exception (e);
9731
9732 exception_print (gdb_stderr, e);
fa8d40ab 9733
05ff989b
AC
9734 /* If pending breakpoint support is auto query and the user
9735 selects no, then simply return the error code. */
059fb39f 9736 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9737 && !nquery (_("Make %s pending on future shared library load? "),
9738 bptype_string (type_wanted)))
fd9b8c24 9739 return 0;
fa8d40ab 9740
05ff989b
AC
9741 /* At this point, either the user was queried about setting
9742 a pending breakpoint and selected yes, or pending
9743 breakpoint behavior is on and thus a pending breakpoint
9744 is defaulted on behalf of the user. */
f8eba3c6
TT
9745 {
9746 struct linespec_sals lsal;
9747
9748 copy_arg = xstrdup (addr_start);
9749 lsal.canonical = xstrdup (copy_arg);
9750 lsal.sals.nelts = 1;
9751 lsal.sals.sals = XNEW (struct symtab_and_line);
9752 init_sal (&lsal.sals.sals[0]);
9753 pending = 1;
9754 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9755 }
05ff989b
AC
9756 break;
9757 default:
98deb0da 9758 throw_exception (e);
0101ce28 9759 }
2abae994 9760 break;
05ff989b 9761 default:
983af33b 9762 throw_exception (e);
0101ce28 9763 }
c3f6f71d 9764
4a64f543 9765 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9766 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9767
c3f6f71d
JM
9768 /* ----------------------------- SNIP -----------------------------
9769 Anything added to the cleanup chain beyond this point is assumed
9770 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9771 then the memory is not reclaimed. */
9772 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9773
c3f6f71d
JM
9774 /* Resolve all line numbers to PC's and verify that the addresses
9775 are ok for the target. */
0101ce28 9776 if (!pending)
f8eba3c6
TT
9777 {
9778 int ix;
9779 struct linespec_sals *iter;
9780
9781 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9782 breakpoint_sals_to_pc (&iter->sals);
9783 }
c3f6f71d 9784
7a697b8d 9785 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9786 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9787 {
9788 int ix;
9789 struct linespec_sals *iter;
9790
9791 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9792 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9793 }
7a697b8d 9794
c3f6f71d
JM
9795 /* Verify that condition can be parsed, before setting any
9796 breakpoints. Allocate a separate condition expression for each
4a64f543 9797 breakpoint. */
0101ce28 9798 if (!pending)
c3f6f71d 9799 {
f6de8ec2 9800 if (parse_arg)
72b2ff0e 9801 {
0878d0fa 9802 char *rest;
52d361e1
YQ
9803 struct linespec_sals *lsal;
9804
9805 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9806
0878d0fa
YQ
9807 /* Here we only parse 'arg' to separate condition
9808 from thread number, so parsing in context of first
9809 sal is OK. When setting the breakpoint we'll
9810 re-parse it in context of each sal. */
9811
9812 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9813 &thread, &task, &rest);
9814 if (cond_string)
9815 make_cleanup (xfree, cond_string);
9816 if (rest)
9817 make_cleanup (xfree, rest);
9818 if (rest)
9819 extra_string = rest;
72b2ff0e 9820 }
2f069f6f 9821 else
72b2ff0e 9822 {
0878d0fa
YQ
9823 if (*arg != '\0')
9824 error (_("Garbage '%s' at end of location"), arg);
9825
9826 /* Create a private copy of condition string. */
9827 if (cond_string)
9828 {
9829 cond_string = xstrdup (cond_string);
9830 make_cleanup (xfree, cond_string);
9831 }
9832 /* Create a private copy of any extra string. */
9833 if (extra_string)
9834 {
9835 extra_string = xstrdup (extra_string);
9836 make_cleanup (xfree, extra_string);
9837 }
72b2ff0e 9838 }
0fb4aa4b 9839
52d361e1 9840 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9841 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9842 tempflag ? disp_del : disp_donttouch,
9843 thread, task, ignore_count, ops,
44f238bb 9844 from_tty, enabled, internal, flags);
c906108c 9845 }
0101ce28
JJ
9846 else
9847 {
0101ce28
JJ
9848 struct breakpoint *b;
9849
0101ce28
JJ
9850 make_cleanup (xfree, copy_arg);
9851
bfccc43c
YQ
9852 if (is_tracepoint_type (type_wanted))
9853 {
9854 struct tracepoint *t;
9855
9856 t = XCNEW (struct tracepoint);
9857 b = &t->base;
9858 }
9859 else
9860 b = XNEW (struct breakpoint);
9861
9862 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9863
f8eba3c6 9864 b->addr_string = copy_arg;
f6de8ec2 9865 if (parse_arg)
e12c7713
MK
9866 b->cond_string = NULL;
9867 else
9868 {
9869 /* Create a private copy of condition string. */
9870 if (cond_string)
9871 {
9872 cond_string = xstrdup (cond_string);
9873 make_cleanup (xfree, cond_string);
9874 }
9875 b->cond_string = cond_string;
9876 }
e7e0cddf 9877 b->extra_string = NULL;
0101ce28 9878 b->ignore_count = ignore_count;
0101ce28 9879 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9880 b->condition_not_parsed = 1;
41447f92 9881 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9882 if ((type_wanted != bp_breakpoint
9883 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9884 b->pspace = current_program_space;
8bea4e01 9885
bfccc43c 9886 install_breakpoint (internal, b, 0);
0101ce28
JJ
9887 }
9888
f8eba3c6 9889 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9890 {
3e43a32a
MS
9891 warning (_("Multiple breakpoints were set.\nUse the "
9892 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9893 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9894 }
9895
80c99de1
PA
9896 /* That's it. Discard the cleanups for data inserted into the
9897 breakpoint. */
9898 discard_cleanups (bkpt_chain);
9899 /* But cleanup everything else. */
c3f6f71d 9900 do_cleanups (old_chain);
217dc9e2 9901
80c99de1 9902 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 9903 update_global_location_list (1);
fd9b8c24
PA
9904
9905 return 1;
c3f6f71d 9906}
c906108c 9907
348d480f 9908/* Set a breakpoint.
72b2ff0e
VP
9909 ARG is a string describing breakpoint address,
9910 condition, and thread.
9911 FLAG specifies if a breakpoint is hardware on,
9912 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9913 and BP_TEMPFLAG. */
348d480f 9914
98deb0da 9915static void
72b2ff0e 9916break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9917{
72b2ff0e 9918 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9919 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9920 ? bp_hardware_breakpoint
9921 : bp_breakpoint);
55aa24fb
SDJ
9922 struct breakpoint_ops *ops;
9923 const char *arg_cp = arg;
9924
9925 /* Matching breakpoints on probes. */
9926 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9927 ops = &bkpt_probe_breakpoint_ops;
9928 else
9929 ops = &bkpt_breakpoint_ops;
c3f6f71d 9930
8cdf0e15
VP
9931 create_breakpoint (get_current_arch (),
9932 arg,
e7e0cddf 9933 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 9934 tempflag, type_wanted,
8cdf0e15
VP
9935 0 /* Ignore count */,
9936 pending_break_support,
55aa24fb 9937 ops,
8cdf0e15 9938 from_tty,
84f4c1fe 9939 1 /* enabled */,
44f238bb
PA
9940 0 /* internal */,
9941 0);
c906108c
SS
9942}
9943
c906108c
SS
9944/* Helper function for break_command_1 and disassemble_command. */
9945
9946void
fba45db2 9947resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9948{
9949 CORE_ADDR pc;
9950
9951 if (sal->pc == 0 && sal->symtab != NULL)
9952 {
9953 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9954 error (_("No line %d in file \"%s\"."),
05cba821 9955 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9956 sal->pc = pc;
6a048695 9957
4a64f543
MS
9958 /* If this SAL corresponds to a breakpoint inserted using a line
9959 number, then skip the function prologue if necessary. */
6a048695 9960 if (sal->explicit_line)
059acae7 9961 skip_prologue_sal (sal);
c906108c
SS
9962 }
9963
9964 if (sal->section == 0 && sal->symtab != NULL)
9965 {
9966 struct blockvector *bv;
c5aa993b
JM
9967 struct block *b;
9968 struct symbol *sym;
c906108c 9969
801e3a5b 9970 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
9971 if (bv != NULL)
9972 {
7f0df278 9973 sym = block_linkage_function (b);
c906108c
SS
9974 if (sym != NULL)
9975 {
9976 fixup_symbol_section (sym, sal->symtab->objfile);
e27d198c 9977 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
c906108c
SS
9978 }
9979 else
9980 {
4a64f543
MS
9981 /* It really is worthwhile to have the section, so we'll
9982 just have to look harder. This case can be executed
9983 if we have line numbers but no functions (as can
9984 happen in assembly source). */
c906108c 9985
7cbd4a93 9986 struct bound_minimal_symbol msym;
6c95b8df
PA
9987 struct cleanup *old_chain = save_current_space_and_thread ();
9988
9989 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9990
9991 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9992 if (msym.minsym)
e27d198c 9993 sal->section = SYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
9994
9995 do_cleanups (old_chain);
c906108c
SS
9996 }
9997 }
9998 }
9999}
10000
10001void
fba45db2 10002break_command (char *arg, int from_tty)
c906108c 10003{
db107f19 10004 break_command_1 (arg, 0, from_tty);
c906108c
SS
10005}
10006
c906108c 10007void
fba45db2 10008tbreak_command (char *arg, int from_tty)
c906108c 10009{
db107f19 10010 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10011}
10012
c906108c 10013static void
fba45db2 10014hbreak_command (char *arg, int from_tty)
c906108c 10015{
db107f19 10016 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10017}
10018
10019static void
fba45db2 10020thbreak_command (char *arg, int from_tty)
c906108c 10021{
db107f19 10022 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10023}
10024
10025static void
fba45db2 10026stop_command (char *arg, int from_tty)
c906108c 10027{
a3f17187 10028 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10029Usage: stop in <function | address>\n\
a3f17187 10030 stop at <line>\n"));
c906108c
SS
10031}
10032
10033static void
fba45db2 10034stopin_command (char *arg, int from_tty)
c906108c
SS
10035{
10036 int badInput = 0;
10037
c5aa993b 10038 if (arg == (char *) NULL)
c906108c
SS
10039 badInput = 1;
10040 else if (*arg != '*')
10041 {
10042 char *argptr = arg;
10043 int hasColon = 0;
10044
4a64f543 10045 /* Look for a ':'. If this is a line number specification, then
53a5351d 10046 say it is bad, otherwise, it should be an address or
4a64f543 10047 function/method name. */
c906108c 10048 while (*argptr && !hasColon)
c5aa993b
JM
10049 {
10050 hasColon = (*argptr == ':');
10051 argptr++;
10052 }
c906108c
SS
10053
10054 if (hasColon)
c5aa993b 10055 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10056 else
c5aa993b 10057 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10058 }
10059
10060 if (badInput)
a3f17187 10061 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10062 else
db107f19 10063 break_command_1 (arg, 0, from_tty);
c906108c
SS
10064}
10065
10066static void
fba45db2 10067stopat_command (char *arg, int from_tty)
c906108c
SS
10068{
10069 int badInput = 0;
10070
c5aa993b 10071 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10072 badInput = 1;
10073 else
10074 {
10075 char *argptr = arg;
10076 int hasColon = 0;
10077
4a64f543
MS
10078 /* Look for a ':'. If there is a '::' then get out, otherwise
10079 it is probably a line number. */
c906108c 10080 while (*argptr && !hasColon)
c5aa993b
JM
10081 {
10082 hasColon = (*argptr == ':');
10083 argptr++;
10084 }
c906108c
SS
10085
10086 if (hasColon)
c5aa993b 10087 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10088 else
c5aa993b 10089 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10090 }
10091
10092 if (badInput)
a3f17187 10093 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10094 else
db107f19 10095 break_command_1 (arg, 0, from_tty);
c906108c
SS
10096}
10097
e7e0cddf
SS
10098/* The dynamic printf command is mostly like a regular breakpoint, but
10099 with a prewired command list consisting of a single output command,
10100 built from extra arguments supplied on the dprintf command
10101 line. */
10102
da821c7b 10103static void
e7e0cddf
SS
10104dprintf_command (char *arg, int from_tty)
10105{
10106 create_breakpoint (get_current_arch (),
10107 arg,
10108 NULL, 0, NULL, 1 /* parse arg */,
10109 0, bp_dprintf,
10110 0 /* Ignore count */,
10111 pending_break_support,
10112 &dprintf_breakpoint_ops,
10113 from_tty,
10114 1 /* enabled */,
10115 0 /* internal */,
10116 0);
10117}
10118
d3ce09f5
SS
10119static void
10120agent_printf_command (char *arg, int from_tty)
10121{
10122 error (_("May only run agent-printf on the target"));
10123}
10124
f1310107
TJB
10125/* Implement the "breakpoint_hit" breakpoint_ops method for
10126 ranged breakpoints. */
10127
10128static int
10129breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10130 struct address_space *aspace,
09ac7c10
TT
10131 CORE_ADDR bp_addr,
10132 const struct target_waitstatus *ws)
f1310107 10133{
09ac7c10 10134 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10135 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10136 return 0;
10137
f1310107
TJB
10138 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10139 bl->length, aspace, bp_addr);
10140}
10141
10142/* Implement the "resources_needed" breakpoint_ops method for
10143 ranged breakpoints. */
10144
10145static int
10146resources_needed_ranged_breakpoint (const struct bp_location *bl)
10147{
10148 return target_ranged_break_num_registers ();
10149}
10150
10151/* Implement the "print_it" breakpoint_ops method for
10152 ranged breakpoints. */
10153
10154static enum print_stop_action
348d480f 10155print_it_ranged_breakpoint (bpstat bs)
f1310107 10156{
348d480f 10157 struct breakpoint *b = bs->breakpoint_at;
f1310107 10158 struct bp_location *bl = b->loc;
79a45e25 10159 struct ui_out *uiout = current_uiout;
f1310107
TJB
10160
10161 gdb_assert (b->type == bp_hardware_breakpoint);
10162
10163 /* Ranged breakpoints have only one location. */
10164 gdb_assert (bl && bl->next == NULL);
10165
10166 annotate_breakpoint (b->number);
10167 if (b->disposition == disp_del)
10168 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10169 else
10170 ui_out_text (uiout, "\nRanged breakpoint ");
10171 if (ui_out_is_mi_like_p (uiout))
10172 {
10173 ui_out_field_string (uiout, "reason",
10174 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10175 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10176 }
10177 ui_out_field_int (uiout, "bkptno", b->number);
10178 ui_out_text (uiout, ", ");
10179
10180 return PRINT_SRC_AND_LOC;
10181}
10182
10183/* Implement the "print_one" breakpoint_ops method for
10184 ranged breakpoints. */
10185
10186static void
10187print_one_ranged_breakpoint (struct breakpoint *b,
10188 struct bp_location **last_loc)
10189{
10190 struct bp_location *bl = b->loc;
10191 struct value_print_options opts;
79a45e25 10192 struct ui_out *uiout = current_uiout;
f1310107
TJB
10193
10194 /* Ranged breakpoints have only one location. */
10195 gdb_assert (bl && bl->next == NULL);
10196
10197 get_user_print_options (&opts);
10198
10199 if (opts.addressprint)
10200 /* We don't print the address range here, it will be printed later
10201 by print_one_detail_ranged_breakpoint. */
10202 ui_out_field_skip (uiout, "addr");
10203 annotate_field (5);
10204 print_breakpoint_location (b, bl);
10205 *last_loc = bl;
10206}
10207
10208/* Implement the "print_one_detail" breakpoint_ops method for
10209 ranged breakpoints. */
10210
10211static void
10212print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10213 struct ui_out *uiout)
10214{
10215 CORE_ADDR address_start, address_end;
10216 struct bp_location *bl = b->loc;
f99d8bf4
PA
10217 struct ui_file *stb = mem_fileopen ();
10218 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10219
10220 gdb_assert (bl);
10221
10222 address_start = bl->address;
10223 address_end = address_start + bl->length - 1;
10224
10225 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10226 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10227 print_core_address (bl->gdbarch, address_start),
10228 print_core_address (bl->gdbarch, address_end));
10229 ui_out_field_stream (uiout, "addr", stb);
10230 ui_out_text (uiout, "\n");
10231
10232 do_cleanups (cleanup);
10233}
10234
10235/* Implement the "print_mention" breakpoint_ops method for
10236 ranged breakpoints. */
10237
10238static void
10239print_mention_ranged_breakpoint (struct breakpoint *b)
10240{
10241 struct bp_location *bl = b->loc;
79a45e25 10242 struct ui_out *uiout = current_uiout;
f1310107
TJB
10243
10244 gdb_assert (bl);
10245 gdb_assert (b->type == bp_hardware_breakpoint);
10246
10247 if (ui_out_is_mi_like_p (uiout))
10248 return;
10249
10250 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10251 b->number, paddress (bl->gdbarch, bl->address),
10252 paddress (bl->gdbarch, bl->address + bl->length - 1));
10253}
10254
10255/* Implement the "print_recreate" breakpoint_ops method for
10256 ranged breakpoints. */
10257
10258static void
10259print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10260{
10261 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10262 b->addr_string_range_end);
d9b3f62e 10263 print_recreate_thread (b, fp);
f1310107
TJB
10264}
10265
10266/* The breakpoint_ops structure to be used in ranged breakpoints. */
10267
2060206e 10268static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10269
10270/* Find the address where the end of the breakpoint range should be
10271 placed, given the SAL of the end of the range. This is so that if
10272 the user provides a line number, the end of the range is set to the
10273 last instruction of the given line. */
10274
10275static CORE_ADDR
10276find_breakpoint_range_end (struct symtab_and_line sal)
10277{
10278 CORE_ADDR end;
10279
10280 /* If the user provided a PC value, use it. Otherwise,
10281 find the address of the end of the given location. */
10282 if (sal.explicit_pc)
10283 end = sal.pc;
10284 else
10285 {
10286 int ret;
10287 CORE_ADDR start;
10288
10289 ret = find_line_pc_range (sal, &start, &end);
10290 if (!ret)
10291 error (_("Could not find location of the end of the range."));
10292
10293 /* find_line_pc_range returns the start of the next line. */
10294 end--;
10295 }
10296
10297 return end;
10298}
10299
10300/* Implement the "break-range" CLI command. */
10301
10302static void
10303break_range_command (char *arg, int from_tty)
10304{
10305 char *arg_start, *addr_string_start, *addr_string_end;
10306 struct linespec_result canonical_start, canonical_end;
10307 int bp_count, can_use_bp, length;
10308 CORE_ADDR end;
10309 struct breakpoint *b;
10310 struct symtab_and_line sal_start, sal_end;
f1310107 10311 struct cleanup *cleanup_bkpt;
f8eba3c6 10312 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10313
10314 /* We don't support software ranged breakpoints. */
10315 if (target_ranged_break_num_registers () < 0)
10316 error (_("This target does not support hardware ranged breakpoints."));
10317
10318 bp_count = hw_breakpoint_used_count ();
10319 bp_count += target_ranged_break_num_registers ();
10320 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10321 bp_count, 0);
10322 if (can_use_bp < 0)
10323 error (_("Hardware breakpoints used exceeds limit."));
10324
f8eba3c6 10325 arg = skip_spaces (arg);
f1310107
TJB
10326 if (arg == NULL || arg[0] == '\0')
10327 error(_("No address range specified."));
10328
f1310107
TJB
10329 init_linespec_result (&canonical_start);
10330
f8eba3c6
TT
10331 arg_start = arg;
10332 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10333
f8eba3c6 10334 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10335
10336 if (arg[0] != ',')
10337 error (_("Too few arguments."));
f8eba3c6 10338 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10339 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10340
10341 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10342
10343 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10344 || lsal_start->sals.nelts != 1)
f1310107
TJB
10345 error (_("Cannot create a ranged breakpoint with multiple locations."));
10346
f8eba3c6
TT
10347 sal_start = lsal_start->sals.sals[0];
10348 addr_string_start = savestring (arg_start, arg - arg_start);
10349 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10350
10351 arg++; /* Skip the comma. */
f8eba3c6 10352 arg = skip_spaces (arg);
f1310107
TJB
10353
10354 /* Parse the end location. */
10355
f1310107
TJB
10356 init_linespec_result (&canonical_end);
10357 arg_start = arg;
10358
f8eba3c6 10359 /* We call decode_line_full directly here instead of using
f1310107
TJB
10360 parse_breakpoint_sals because we need to specify the start location's
10361 symtab and line as the default symtab and line for the end of the
10362 range. This makes it possible to have ranges like "foo.c:27, +14",
10363 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10364 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10365 sal_start.symtab, sal_start.line,
10366 &canonical_end, NULL, NULL);
10367
10368 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10369
f8eba3c6 10370 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10371 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10372
10373 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10374 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10375 || lsal_end->sals.nelts != 1)
f1310107
TJB
10376 error (_("Cannot create a ranged breakpoint with multiple locations."));
10377
f8eba3c6
TT
10378 sal_end = lsal_end->sals.sals[0];
10379 addr_string_end = savestring (arg_start, arg - arg_start);
10380 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10381
10382 end = find_breakpoint_range_end (sal_end);
10383 if (sal_start.pc > end)
177b42fe 10384 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10385
10386 length = end - sal_start.pc + 1;
10387 if (length < 0)
10388 /* Length overflowed. */
10389 error (_("Address range too large."));
10390 else if (length == 1)
10391 {
10392 /* This range is simple enough to be handled by
10393 the `hbreak' command. */
10394 hbreak_command (addr_string_start, 1);
10395
10396 do_cleanups (cleanup_bkpt);
10397
10398 return;
10399 }
10400
10401 /* Now set up the breakpoint. */
10402 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10403 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10404 set_breakpoint_count (breakpoint_count + 1);
10405 b->number = breakpoint_count;
10406 b->disposition = disp_donttouch;
f8eba3c6
TT
10407 b->addr_string = xstrdup (addr_string_start);
10408 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10409 b->loc->length = length;
10410
f8eba3c6 10411 do_cleanups (cleanup_bkpt);
f1310107
TJB
10412
10413 mention (b);
8d3788bd 10414 observer_notify_breakpoint_created (b);
f1310107
TJB
10415 update_global_location_list (1);
10416}
10417
4a64f543
MS
10418/* Return non-zero if EXP is verified as constant. Returned zero
10419 means EXP is variable. Also the constant detection may fail for
10420 some constant expressions and in such case still falsely return
10421 zero. */
2e6e3d9c 10422
65d79d4b
SDJ
10423static int
10424watchpoint_exp_is_const (const struct expression *exp)
10425{
10426 int i = exp->nelts;
10427
10428 while (i > 0)
10429 {
10430 int oplenp, argsp;
10431
10432 /* We are only interested in the descriptor of each element. */
10433 operator_length (exp, i, &oplenp, &argsp);
10434 i -= oplenp;
10435
10436 switch (exp->elts[i].opcode)
10437 {
10438 case BINOP_ADD:
10439 case BINOP_SUB:
10440 case BINOP_MUL:
10441 case BINOP_DIV:
10442 case BINOP_REM:
10443 case BINOP_MOD:
10444 case BINOP_LSH:
10445 case BINOP_RSH:
10446 case BINOP_LOGICAL_AND:
10447 case BINOP_LOGICAL_OR:
10448 case BINOP_BITWISE_AND:
10449 case BINOP_BITWISE_IOR:
10450 case BINOP_BITWISE_XOR:
10451 case BINOP_EQUAL:
10452 case BINOP_NOTEQUAL:
10453 case BINOP_LESS:
10454 case BINOP_GTR:
10455 case BINOP_LEQ:
10456 case BINOP_GEQ:
10457 case BINOP_REPEAT:
10458 case BINOP_COMMA:
10459 case BINOP_EXP:
10460 case BINOP_MIN:
10461 case BINOP_MAX:
10462 case BINOP_INTDIV:
10463 case BINOP_CONCAT:
10464 case BINOP_IN:
10465 case BINOP_RANGE:
10466 case TERNOP_COND:
10467 case TERNOP_SLICE:
65d79d4b
SDJ
10468
10469 case OP_LONG:
10470 case OP_DOUBLE:
10471 case OP_DECFLOAT:
10472 case OP_LAST:
10473 case OP_COMPLEX:
10474 case OP_STRING:
65d79d4b
SDJ
10475 case OP_ARRAY:
10476 case OP_TYPE:
608b4967
TT
10477 case OP_TYPEOF:
10478 case OP_DECLTYPE:
6e72ca20 10479 case OP_TYPEID:
65d79d4b
SDJ
10480 case OP_NAME:
10481 case OP_OBJC_NSSTRING:
10482
10483 case UNOP_NEG:
10484 case UNOP_LOGICAL_NOT:
10485 case UNOP_COMPLEMENT:
10486 case UNOP_ADDR:
10487 case UNOP_HIGH:
aeaa2474 10488 case UNOP_CAST:
9eaf6705
TT
10489
10490 case UNOP_CAST_TYPE:
10491 case UNOP_REINTERPRET_CAST:
10492 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10493 /* Unary, binary and ternary operators: We have to check
10494 their operands. If they are constant, then so is the
10495 result of that operation. For instance, if A and B are
10496 determined to be constants, then so is "A + B".
10497
10498 UNOP_IND is one exception to the rule above, because the
10499 value of *ADDR is not necessarily a constant, even when
10500 ADDR is. */
65d79d4b
SDJ
10501 break;
10502
10503 case OP_VAR_VALUE:
10504 /* Check whether the associated symbol is a constant.
4a64f543 10505
65d79d4b 10506 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10507 possible that a buggy compiler could mark a variable as
10508 constant even when it is not, and TYPE_CONST would return
10509 true in this case, while SYMBOL_CLASS wouldn't.
10510
10511 We also have to check for function symbols because they
10512 are always constant. */
65d79d4b
SDJ
10513 {
10514 struct symbol *s = exp->elts[i + 2].symbol;
10515
10516 if (SYMBOL_CLASS (s) != LOC_BLOCK
10517 && SYMBOL_CLASS (s) != LOC_CONST
10518 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10519 return 0;
10520 break;
10521 }
10522
10523 /* The default action is to return 0 because we are using
10524 the optimistic approach here: If we don't know something,
10525 then it is not a constant. */
10526 default:
10527 return 0;
10528 }
10529 }
10530
10531 return 1;
10532}
10533
3a5c3e22
PA
10534/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10535
10536static void
10537dtor_watchpoint (struct breakpoint *self)
10538{
10539 struct watchpoint *w = (struct watchpoint *) self;
10540
10541 xfree (w->cond_exp);
10542 xfree (w->exp);
10543 xfree (w->exp_string);
10544 xfree (w->exp_string_reparse);
10545 value_free (w->val);
10546
10547 base_breakpoint_ops.dtor (self);
10548}
10549
348d480f
PA
10550/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10551
10552static void
10553re_set_watchpoint (struct breakpoint *b)
10554{
3a5c3e22
PA
10555 struct watchpoint *w = (struct watchpoint *) b;
10556
348d480f
PA
10557 /* Watchpoint can be either on expression using entirely global
10558 variables, or it can be on local variables.
10559
10560 Watchpoints of the first kind are never auto-deleted, and even
10561 persist across program restarts. Since they can use variables
10562 from shared libraries, we need to reparse expression as libraries
10563 are loaded and unloaded.
10564
10565 Watchpoints on local variables can also change meaning as result
10566 of solib event. For example, if a watchpoint uses both a local
10567 and a global variables in expression, it's a local watchpoint,
10568 but unloading of a shared library will make the expression
10569 invalid. This is not a very common use case, but we still
10570 re-evaluate expression, to avoid surprises to the user.
10571
10572 Note that for local watchpoints, we re-evaluate it only if
10573 watchpoints frame id is still valid. If it's not, it means the
10574 watchpoint is out of scope and will be deleted soon. In fact,
10575 I'm not sure we'll ever be called in this case.
10576
10577 If a local watchpoint's frame id is still valid, then
3a5c3e22 10578 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10579
3a5c3e22
PA
10580 Don't do anything about disabled watchpoints, since they will be
10581 reevaluated again when enabled. */
10582 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10583}
10584
77b06cd7
TJB
10585/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10586
10587static int
10588insert_watchpoint (struct bp_location *bl)
10589{
3a5c3e22
PA
10590 struct watchpoint *w = (struct watchpoint *) bl->owner;
10591 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10592
10593 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10594 w->cond_exp);
77b06cd7
TJB
10595}
10596
10597/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10598
10599static int
10600remove_watchpoint (struct bp_location *bl)
10601{
3a5c3e22
PA
10602 struct watchpoint *w = (struct watchpoint *) bl->owner;
10603 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10604
10605 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10606 w->cond_exp);
e09342b5
TJB
10607}
10608
e09342b5 10609static int
348d480f 10610breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10611 struct address_space *aspace, CORE_ADDR bp_addr,
10612 const struct target_waitstatus *ws)
e09342b5 10613{
348d480f 10614 struct breakpoint *b = bl->owner;
3a5c3e22 10615 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10616
348d480f
PA
10617 /* Continuable hardware watchpoints are treated as non-existent if the
10618 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10619 some data address). Otherwise gdb won't stop on a break instruction
10620 in the code (not from a breakpoint) when a hardware watchpoint has
10621 been defined. Also skip watchpoints which we know did not trigger
10622 (did not match the data address). */
10623 if (is_hardware_watchpoint (b)
3a5c3e22 10624 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10625 return 0;
9c06b0b4 10626
348d480f 10627 return 1;
9c06b0b4
TJB
10628}
10629
348d480f
PA
10630static void
10631check_status_watchpoint (bpstat bs)
9c06b0b4 10632{
348d480f 10633 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10634
348d480f 10635 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10636}
10637
10638/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10639 hardware watchpoints. */
9c06b0b4
TJB
10640
10641static int
348d480f 10642resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10643{
3a5c3e22
PA
10644 struct watchpoint *w = (struct watchpoint *) bl->owner;
10645 int length = w->exact? 1 : bl->length;
348d480f
PA
10646
10647 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10648}
10649
10650/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10651 hardware watchpoints. */
9c06b0b4
TJB
10652
10653static int
348d480f 10654works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10655{
efa80663
PA
10656 /* Read and access watchpoints only work with hardware support. */
10657 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10658}
10659
9c06b0b4 10660static enum print_stop_action
348d480f 10661print_it_watchpoint (bpstat bs)
9c06b0b4 10662{
348d480f
PA
10663 struct cleanup *old_chain;
10664 struct breakpoint *b;
f99d8bf4 10665 struct ui_file *stb;
348d480f 10666 enum print_stop_action result;
3a5c3e22 10667 struct watchpoint *w;
79a45e25 10668 struct ui_out *uiout = current_uiout;
348d480f
PA
10669
10670 gdb_assert (bs->bp_location_at != NULL);
10671
348d480f 10672 b = bs->breakpoint_at;
3a5c3e22 10673 w = (struct watchpoint *) b;
348d480f 10674
f99d8bf4
PA
10675 stb = mem_fileopen ();
10676 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10677
10678 switch (b->type)
10679 {
348d480f 10680 case bp_watchpoint:
9c06b0b4
TJB
10681 case bp_hardware_watchpoint:
10682 annotate_watchpoint (b->number);
10683 if (ui_out_is_mi_like_p (uiout))
10684 ui_out_field_string
10685 (uiout, "reason",
10686 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10687 mention (b);
10688 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10689 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10690 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10691 ui_out_field_stream (uiout, "old", stb);
10692 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10693 watchpoint_value_print (w->val, stb);
348d480f
PA
10694 ui_out_field_stream (uiout, "new", stb);
10695 ui_out_text (uiout, "\n");
10696 /* More than one watchpoint may have been triggered. */
10697 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10698 break;
10699
10700 case bp_read_watchpoint:
10701 if (ui_out_is_mi_like_p (uiout))
10702 ui_out_field_string
10703 (uiout, "reason",
10704 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10705 mention (b);
10706 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10707 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10708 watchpoint_value_print (w->val, stb);
348d480f
PA
10709 ui_out_field_stream (uiout, "value", stb);
10710 ui_out_text (uiout, "\n");
10711 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10712 break;
10713
10714 case bp_access_watchpoint:
348d480f
PA
10715 if (bs->old_val != NULL)
10716 {
10717 annotate_watchpoint (b->number);
10718 if (ui_out_is_mi_like_p (uiout))
10719 ui_out_field_string
10720 (uiout, "reason",
10721 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10722 mention (b);
10723 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10724 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10725 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10726 ui_out_field_stream (uiout, "old", stb);
10727 ui_out_text (uiout, "\nNew value = ");
10728 }
10729 else
10730 {
10731 mention (b);
10732 if (ui_out_is_mi_like_p (uiout))
10733 ui_out_field_string
10734 (uiout, "reason",
10735 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10736 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10737 ui_out_text (uiout, "\nValue = ");
10738 }
f99d8bf4 10739 watchpoint_value_print (w->val, stb);
348d480f
PA
10740 ui_out_field_stream (uiout, "new", stb);
10741 ui_out_text (uiout, "\n");
10742 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10743 break;
10744 default:
348d480f 10745 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10746 }
10747
348d480f
PA
10748 do_cleanups (old_chain);
10749 return result;
10750}
10751
10752/* Implement the "print_mention" breakpoint_ops method for hardware
10753 watchpoints. */
10754
10755static void
10756print_mention_watchpoint (struct breakpoint *b)
10757{
10758 struct cleanup *ui_out_chain;
3a5c3e22 10759 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10760 struct ui_out *uiout = current_uiout;
348d480f
PA
10761
10762 switch (b->type)
10763 {
10764 case bp_watchpoint:
10765 ui_out_text (uiout, "Watchpoint ");
10766 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10767 break;
10768 case bp_hardware_watchpoint:
10769 ui_out_text (uiout, "Hardware watchpoint ");
10770 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10771 break;
10772 case bp_read_watchpoint:
10773 ui_out_text (uiout, "Hardware read watchpoint ");
10774 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10775 break;
10776 case bp_access_watchpoint:
10777 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10778 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10779 break;
10780 default:
10781 internal_error (__FILE__, __LINE__,
10782 _("Invalid hardware watchpoint type."));
10783 }
10784
10785 ui_out_field_int (uiout, "number", b->number);
10786 ui_out_text (uiout, ": ");
3a5c3e22 10787 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10788 do_cleanups (ui_out_chain);
10789}
10790
10791/* Implement the "print_recreate" breakpoint_ops method for
10792 watchpoints. */
10793
10794static void
10795print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10796{
3a5c3e22
PA
10797 struct watchpoint *w = (struct watchpoint *) b;
10798
348d480f
PA
10799 switch (b->type)
10800 {
10801 case bp_watchpoint:
10802 case bp_hardware_watchpoint:
10803 fprintf_unfiltered (fp, "watch");
10804 break;
10805 case bp_read_watchpoint:
10806 fprintf_unfiltered (fp, "rwatch");
10807 break;
10808 case bp_access_watchpoint:
10809 fprintf_unfiltered (fp, "awatch");
10810 break;
10811 default:
10812 internal_error (__FILE__, __LINE__,
10813 _("Invalid watchpoint type."));
10814 }
10815
3a5c3e22 10816 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10817 print_recreate_thread (b, fp);
348d480f
PA
10818}
10819
427cd150
TT
10820/* Implement the "explains_signal" breakpoint_ops method for
10821 watchpoints. */
10822
47591c29 10823static int
427cd150
TT
10824explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10825{
10826 /* A software watchpoint cannot cause a signal other than
10827 GDB_SIGNAL_TRAP. */
10828 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10829 return 0;
427cd150 10830
47591c29 10831 return 1;
427cd150
TT
10832}
10833
348d480f
PA
10834/* The breakpoint_ops structure to be used in hardware watchpoints. */
10835
2060206e 10836static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10837
10838/* Implement the "insert" breakpoint_ops method for
10839 masked hardware watchpoints. */
10840
10841static int
10842insert_masked_watchpoint (struct bp_location *bl)
10843{
3a5c3e22
PA
10844 struct watchpoint *w = (struct watchpoint *) bl->owner;
10845
10846 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10847 bl->watchpoint_type);
10848}
10849
10850/* Implement the "remove" breakpoint_ops method for
10851 masked hardware watchpoints. */
10852
10853static int
10854remove_masked_watchpoint (struct bp_location *bl)
10855{
3a5c3e22
PA
10856 struct watchpoint *w = (struct watchpoint *) bl->owner;
10857
10858 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10859 bl->watchpoint_type);
10860}
10861
10862/* Implement the "resources_needed" breakpoint_ops method for
10863 masked hardware watchpoints. */
10864
10865static int
10866resources_needed_masked_watchpoint (const struct bp_location *bl)
10867{
3a5c3e22
PA
10868 struct watchpoint *w = (struct watchpoint *) bl->owner;
10869
10870 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10871}
10872
10873/* Implement the "works_in_software_mode" breakpoint_ops method for
10874 masked hardware watchpoints. */
10875
10876static int
10877works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10878{
10879 return 0;
10880}
10881
10882/* Implement the "print_it" breakpoint_ops method for
10883 masked hardware watchpoints. */
10884
10885static enum print_stop_action
10886print_it_masked_watchpoint (bpstat bs)
10887{
10888 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10889 struct ui_out *uiout = current_uiout;
348d480f
PA
10890
10891 /* Masked watchpoints have only one location. */
10892 gdb_assert (b->loc && b->loc->next == NULL);
10893
10894 switch (b->type)
10895 {
10896 case bp_hardware_watchpoint:
10897 annotate_watchpoint (b->number);
10898 if (ui_out_is_mi_like_p (uiout))
10899 ui_out_field_string
10900 (uiout, "reason",
10901 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10902 break;
10903
10904 case bp_read_watchpoint:
10905 if (ui_out_is_mi_like_p (uiout))
10906 ui_out_field_string
10907 (uiout, "reason",
10908 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10909 break;
10910
10911 case bp_access_watchpoint:
10912 if (ui_out_is_mi_like_p (uiout))
10913 ui_out_field_string
10914 (uiout, "reason",
10915 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10916 break;
10917 default:
10918 internal_error (__FILE__, __LINE__,
10919 _("Invalid hardware watchpoint type."));
10920 }
10921
10922 mention (b);
9c06b0b4
TJB
10923 ui_out_text (uiout, _("\n\
10924Check the underlying instruction at PC for the memory\n\
10925address and value which triggered this watchpoint.\n"));
10926 ui_out_text (uiout, "\n");
10927
10928 /* More than one watchpoint may have been triggered. */
10929 return PRINT_UNKNOWN;
10930}
10931
10932/* Implement the "print_one_detail" breakpoint_ops method for
10933 masked hardware watchpoints. */
10934
10935static void
10936print_one_detail_masked_watchpoint (const struct breakpoint *b,
10937 struct ui_out *uiout)
10938{
3a5c3e22
PA
10939 struct watchpoint *w = (struct watchpoint *) b;
10940
9c06b0b4
TJB
10941 /* Masked watchpoints have only one location. */
10942 gdb_assert (b->loc && b->loc->next == NULL);
10943
10944 ui_out_text (uiout, "\tmask ");
3a5c3e22 10945 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10946 ui_out_text (uiout, "\n");
10947}
10948
10949/* Implement the "print_mention" breakpoint_ops method for
10950 masked hardware watchpoints. */
10951
10952static void
10953print_mention_masked_watchpoint (struct breakpoint *b)
10954{
3a5c3e22 10955 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10956 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10957 struct cleanup *ui_out_chain;
10958
10959 switch (b->type)
10960 {
10961 case bp_hardware_watchpoint:
10962 ui_out_text (uiout, "Masked hardware watchpoint ");
10963 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10964 break;
10965 case bp_read_watchpoint:
10966 ui_out_text (uiout, "Masked hardware read watchpoint ");
10967 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10968 break;
10969 case bp_access_watchpoint:
10970 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10971 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10972 break;
10973 default:
10974 internal_error (__FILE__, __LINE__,
10975 _("Invalid hardware watchpoint type."));
10976 }
10977
10978 ui_out_field_int (uiout, "number", b->number);
10979 ui_out_text (uiout, ": ");
3a5c3e22 10980 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10981 do_cleanups (ui_out_chain);
10982}
10983
10984/* Implement the "print_recreate" breakpoint_ops method for
10985 masked hardware watchpoints. */
10986
10987static void
10988print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10989{
3a5c3e22 10990 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10991 char tmp[40];
10992
10993 switch (b->type)
10994 {
10995 case bp_hardware_watchpoint:
10996 fprintf_unfiltered (fp, "watch");
10997 break;
10998 case bp_read_watchpoint:
10999 fprintf_unfiltered (fp, "rwatch");
11000 break;
11001 case bp_access_watchpoint:
11002 fprintf_unfiltered (fp, "awatch");
11003 break;
11004 default:
11005 internal_error (__FILE__, __LINE__,
11006 _("Invalid hardware watchpoint type."));
11007 }
11008
3a5c3e22
PA
11009 sprintf_vma (tmp, w->hw_wp_mask);
11010 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11011 print_recreate_thread (b, fp);
9c06b0b4
TJB
11012}
11013
11014/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11015
2060206e 11016static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11017
11018/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11019
11020static int
11021is_masked_watchpoint (const struct breakpoint *b)
11022{
11023 return b->ops == &masked_watchpoint_breakpoint_ops;
11024}
11025
53a5351d
JM
11026/* accessflag: hw_write: watch write,
11027 hw_read: watch read,
11028 hw_access: watch access (read or write) */
c906108c 11029static void
bbc13ae3 11030watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11031 int just_location, int internal)
c906108c 11032{
a9634178 11033 volatile struct gdb_exception e;
d983da9c 11034 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11035 struct expression *exp;
270140bd 11036 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11037 struct value *val, *mark, *result;
c906108c 11038 struct frame_info *frame;
bbc13ae3
KS
11039 const char *exp_start = NULL;
11040 const char *exp_end = NULL;
11041 const char *tok, *end_tok;
9c06b0b4 11042 int toklen = -1;
bbc13ae3
KS
11043 const char *cond_start = NULL;
11044 const char *cond_end = NULL;
c906108c 11045 enum bptype bp_type;
37e4754d 11046 int thread = -1;
0cf6dd15 11047 int pc = 0;
9c06b0b4
TJB
11048 /* Flag to indicate whether we are going to use masks for
11049 the hardware watchpoint. */
11050 int use_mask = 0;
11051 CORE_ADDR mask = 0;
3a5c3e22 11052 struct watchpoint *w;
bbc13ae3
KS
11053 char *expression;
11054 struct cleanup *back_to;
c906108c 11055
37e4754d
LM
11056 /* Make sure that we actually have parameters to parse. */
11057 if (arg != NULL && arg[0] != '\0')
11058 {
bbc13ae3
KS
11059 const char *value_start;
11060
11061 exp_end = arg + strlen (arg);
37e4754d 11062
9c06b0b4
TJB
11063 /* Look for "parameter value" pairs at the end
11064 of the arguments string. */
bbc13ae3 11065 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11066 {
11067 /* Skip whitespace at the end of the argument list. */
11068 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11069 tok--;
11070
11071 /* Find the beginning of the last token.
11072 This is the value of the parameter. */
11073 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11074 tok--;
11075 value_start = tok + 1;
11076
11077 /* Skip whitespace. */
11078 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11079 tok--;
11080
11081 end_tok = tok;
11082
11083 /* Find the beginning of the second to last token.
11084 This is the parameter itself. */
11085 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11086 tok--;
11087 tok++;
11088 toklen = end_tok - tok + 1;
11089
11090 if (toklen == 6 && !strncmp (tok, "thread", 6))
11091 {
11092 /* At this point we've found a "thread" token, which means
11093 the user is trying to set a watchpoint that triggers
11094 only in a specific thread. */
11095 char *endp;
37e4754d 11096
9c06b0b4
TJB
11097 if (thread != -1)
11098 error(_("You can specify only one thread."));
37e4754d 11099
9c06b0b4
TJB
11100 /* Extract the thread ID from the next token. */
11101 thread = strtol (value_start, &endp, 0);
37e4754d 11102
9c06b0b4
TJB
11103 /* Check if the user provided a valid numeric value for the
11104 thread ID. */
11105 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11106 error (_("Invalid thread ID specification %s."), value_start);
11107
11108 /* Check if the thread actually exists. */
11109 if (!valid_thread_id (thread))
af4908ba 11110 invalid_thread_id_error (thread);
9c06b0b4
TJB
11111 }
11112 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11113 {
11114 /* We've found a "mask" token, which means the user wants to
11115 create a hardware watchpoint that is going to have the mask
11116 facility. */
11117 struct value *mask_value, *mark;
37e4754d 11118
9c06b0b4
TJB
11119 if (use_mask)
11120 error(_("You can specify only one mask."));
37e4754d 11121
9c06b0b4 11122 use_mask = just_location = 1;
37e4754d 11123
9c06b0b4
TJB
11124 mark = value_mark ();
11125 mask_value = parse_to_comma_and_eval (&value_start);
11126 mask = value_as_address (mask_value);
11127 value_free_to_mark (mark);
11128 }
11129 else
11130 /* We didn't recognize what we found. We should stop here. */
11131 break;
37e4754d 11132
9c06b0b4
TJB
11133 /* Truncate the string and get rid of the "parameter value" pair before
11134 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11135 exp_end = tok;
9c06b0b4 11136 }
37e4754d 11137 }
bbc13ae3
KS
11138 else
11139 exp_end = arg;
37e4754d 11140
bbc13ae3
KS
11141 /* Parse the rest of the arguments. From here on out, everything
11142 is in terms of a newly allocated string instead of the original
11143 ARG. */
c906108c 11144 innermost_block = NULL;
bbc13ae3
KS
11145 expression = savestring (arg, exp_end - arg);
11146 back_to = make_cleanup (xfree, expression);
11147 exp_start = arg = expression;
1bb9788d 11148 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11149 exp_end = arg;
fa8a61dc
TT
11150 /* Remove trailing whitespace from the expression before saving it.
11151 This makes the eventual display of the expression string a bit
11152 prettier. */
11153 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11154 --exp_end;
11155
65d79d4b
SDJ
11156 /* Checking if the expression is not constant. */
11157 if (watchpoint_exp_is_const (exp))
11158 {
11159 int len;
11160
11161 len = exp_end - exp_start;
11162 while (len > 0 && isspace (exp_start[len - 1]))
11163 len--;
11164 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11165 }
11166
c906108c
SS
11167 exp_valid_block = innermost_block;
11168 mark = value_mark ();
3a1115a0 11169 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b
TT
11170
11171 if (just_location)
11172 {
9c06b0b4
TJB
11173 int ret;
11174
06a64a0b 11175 exp_valid_block = NULL;
a1442452 11176 val = value_addr (result);
06a64a0b
TT
11177 release_value (val);
11178 value_free_to_mark (mark);
9c06b0b4
TJB
11179
11180 if (use_mask)
11181 {
11182 ret = target_masked_watch_num_registers (value_as_address (val),
11183 mask);
11184 if (ret == -1)
11185 error (_("This target does not support masked watchpoints."));
11186 else if (ret == -2)
11187 error (_("Invalid mask or memory region."));
11188 }
06a64a0b
TT
11189 }
11190 else if (val != NULL)
fa4727a6 11191 release_value (val);
c906108c 11192
bbc13ae3
KS
11193 tok = skip_spaces_const (arg);
11194 end_tok = skip_to_space_const (tok);
c906108c
SS
11195
11196 toklen = end_tok - tok;
11197 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11198 {
2d134ed3
PA
11199 struct expression *cond;
11200
60e1c644 11201 innermost_block = NULL;
c906108c 11202 tok = cond_start = end_tok + 1;
1bb9788d 11203 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11204
11205 /* The watchpoint expression may not be local, but the condition
11206 may still be. E.g.: `watch global if local > 0'. */
11207 cond_exp_valid_block = innermost_block;
11208
2d134ed3 11209 xfree (cond);
c906108c
SS
11210 cond_end = tok;
11211 }
11212 if (*tok)
8a3fe4f8 11213 error (_("Junk at end of command."));
c906108c 11214
d983da9c 11215 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11216
11217 /* If the expression is "local", then set up a "watchpoint scope"
11218 breakpoint at the point where we've left the scope of the watchpoint
11219 expression. Create the scope breakpoint before the watchpoint, so
11220 that we will encounter it first in bpstat_stop_status. */
60e1c644 11221 if (exp_valid_block && frame)
d983da9c 11222 {
edb3359d
DJ
11223 if (frame_id_p (frame_unwind_caller_id (frame)))
11224 {
11225 scope_breakpoint
a6d9a66e
UW
11226 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11227 frame_unwind_caller_pc (frame),
06edf0c0
PA
11228 bp_watchpoint_scope,
11229 &momentary_breakpoint_ops);
d983da9c 11230
edb3359d 11231 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11232
edb3359d
DJ
11233 /* Automatically delete the breakpoint when it hits. */
11234 scope_breakpoint->disposition = disp_del;
d983da9c 11235
edb3359d
DJ
11236 /* Only break in the proper frame (help with recursion). */
11237 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11238
edb3359d 11239 /* Set the address at which we will stop. */
a6d9a66e
UW
11240 scope_breakpoint->loc->gdbarch
11241 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11242 scope_breakpoint->loc->requested_address
11243 = frame_unwind_caller_pc (frame);
11244 scope_breakpoint->loc->address
a6d9a66e
UW
11245 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11246 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11247 scope_breakpoint->type);
11248 }
d983da9c
DJ
11249 }
11250
e8369a73
AB
11251 /* Now set up the breakpoint. We create all watchpoints as hardware
11252 watchpoints here even if hardware watchpoints are turned off, a call
11253 to update_watchpoint later in this function will cause the type to
11254 drop back to bp_watchpoint (software watchpoint) if required. */
11255
11256 if (accessflag == hw_read)
11257 bp_type = bp_read_watchpoint;
11258 else if (accessflag == hw_access)
11259 bp_type = bp_access_watchpoint;
11260 else
11261 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11262
11263 w = XCNEW (struct watchpoint);
11264 b = &w->base;
348d480f 11265 if (use_mask)
3a5c3e22
PA
11266 init_raw_breakpoint_without_location (b, NULL, bp_type,
11267 &masked_watchpoint_breakpoint_ops);
348d480f 11268 else
3a5c3e22
PA
11269 init_raw_breakpoint_without_location (b, NULL, bp_type,
11270 &watchpoint_breakpoint_ops);
37e4754d 11271 b->thread = thread;
b5de0fa7 11272 b->disposition = disp_donttouch;
348d480f 11273 b->pspace = current_program_space;
3a5c3e22
PA
11274 w->exp = exp;
11275 w->exp_valid_block = exp_valid_block;
11276 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11277 if (just_location)
11278 {
11279 struct type *t = value_type (val);
11280 CORE_ADDR addr = value_as_address (val);
11281 char *name;
11282
11283 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11284 name = type_to_string (t);
11285
3a5c3e22 11286 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11287 core_addr_to_string (addr));
06a64a0b
TT
11288 xfree (name);
11289
3a5c3e22 11290 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11291 (int) (exp_end - exp_start), exp_start);
11292
06a64a0b
TT
11293 /* The above expression is in C. */
11294 b->language = language_c;
11295 }
11296 else
3a5c3e22 11297 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11298
11299 if (use_mask)
11300 {
3a5c3e22 11301 w->hw_wp_mask = mask;
9c06b0b4
TJB
11302 }
11303 else
11304 {
3a5c3e22
PA
11305 w->val = val;
11306 w->val_valid = 1;
9c06b0b4 11307 }
77b06cd7 11308
c906108c
SS
11309 if (cond_start)
11310 b->cond_string = savestring (cond_start, cond_end - cond_start);
11311 else
11312 b->cond_string = 0;
c5aa993b 11313
c906108c 11314 if (frame)
f6bc2008 11315 {
3a5c3e22
PA
11316 w->watchpoint_frame = get_frame_id (frame);
11317 w->watchpoint_thread = inferior_ptid;
f6bc2008 11318 }
c906108c 11319 else
f6bc2008 11320 {
3a5c3e22
PA
11321 w->watchpoint_frame = null_frame_id;
11322 w->watchpoint_thread = null_ptid;
f6bc2008 11323 }
c906108c 11324
d983da9c 11325 if (scope_breakpoint != NULL)
c906108c 11326 {
d983da9c
DJ
11327 /* The scope breakpoint is related to the watchpoint. We will
11328 need to act on them together. */
11329 b->related_breakpoint = scope_breakpoint;
11330 scope_breakpoint->related_breakpoint = b;
c906108c 11331 }
d983da9c 11332
06a64a0b
TT
11333 if (!just_location)
11334 value_free_to_mark (mark);
2d134ed3 11335
a9634178
TJB
11336 TRY_CATCH (e, RETURN_MASK_ALL)
11337 {
11338 /* Finally update the new watchpoint. This creates the locations
11339 that should be inserted. */
3a5c3e22 11340 update_watchpoint (w, 1);
a9634178
TJB
11341 }
11342 if (e.reason < 0)
11343 {
11344 delete_breakpoint (b);
11345 throw_exception (e);
11346 }
11347
3ea46bff 11348 install_breakpoint (internal, b, 1);
bbc13ae3 11349 do_cleanups (back_to);
c906108c
SS
11350}
11351
e09342b5 11352/* Return count of debug registers needed to watch the given expression.
e09342b5 11353 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11354
c906108c 11355static int
a9634178 11356can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11357{
11358 int found_memory_cnt = 0;
2e70b7b9 11359 struct value *head = v;
c906108c
SS
11360
11361 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11362 if (!can_use_hw_watchpoints)
c906108c 11363 return 0;
c5aa993b 11364
5c44784c
JM
11365 /* Make sure that the value of the expression depends only upon
11366 memory contents, and values computed from them within GDB. If we
11367 find any register references or function calls, we can't use a
11368 hardware watchpoint.
11369
11370 The idea here is that evaluating an expression generates a series
11371 of values, one holding the value of every subexpression. (The
11372 expression a*b+c has five subexpressions: a, b, a*b, c, and
11373 a*b+c.) GDB's values hold almost enough information to establish
11374 the criteria given above --- they identify memory lvalues,
11375 register lvalues, computed values, etcetera. So we can evaluate
11376 the expression, and then scan the chain of values that leaves
11377 behind to decide whether we can detect any possible change to the
11378 expression's final value using only hardware watchpoints.
11379
11380 However, I don't think that the values returned by inferior
11381 function calls are special in any way. So this function may not
11382 notice that an expression involving an inferior function call
11383 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11384 for (; v; v = value_next (v))
c906108c 11385 {
5c44784c 11386 if (VALUE_LVAL (v) == lval_memory)
c906108c 11387 {
8464be76
DJ
11388 if (v != head && value_lazy (v))
11389 /* A lazy memory lvalue in the chain is one that GDB never
11390 needed to fetch; we either just used its address (e.g.,
11391 `a' in `a.b') or we never needed it at all (e.g., `a'
11392 in `a,b'). This doesn't apply to HEAD; if that is
11393 lazy then it was not readable, but watch it anyway. */
5c44784c 11394 ;
53a5351d 11395 else
5c44784c
JM
11396 {
11397 /* Ahh, memory we actually used! Check if we can cover
11398 it with hardware watchpoints. */
df407dfe 11399 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11400
11401 /* We only watch structs and arrays if user asked for it
11402 explicitly, never if they just happen to appear in a
11403 middle of some value chain. */
11404 if (v == head
11405 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11406 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11407 {
42ae5230 11408 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11409 int len;
11410 int num_regs;
11411
a9634178 11412 len = (target_exact_watchpoints
e09342b5
TJB
11413 && is_scalar_type_recursive (vtype))?
11414 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11415
e09342b5
TJB
11416 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11417 if (!num_regs)
2e70b7b9
MS
11418 return 0;
11419 else
e09342b5 11420 found_memory_cnt += num_regs;
2e70b7b9 11421 }
5c44784c 11422 }
c5aa993b 11423 }
5086187c
AC
11424 else if (VALUE_LVAL (v) != not_lval
11425 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11426 return 0; /* These are values from the history (e.g., $1). */
5086187c 11427 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11428 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11429 }
11430
11431 /* The expression itself looks suitable for using a hardware
11432 watchpoint, but give the target machine a chance to reject it. */
11433 return found_memory_cnt;
11434}
11435
8b93c638 11436void
84f4c1fe 11437watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11438{
84f4c1fe 11439 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11440}
11441
06a64a0b
TT
11442/* A helper function that looks for the "-location" argument and then
11443 calls watch_command_1. */
11444
11445static void
11446watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11447{
11448 int just_location = 0;
11449
11450 if (arg
11451 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11452 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11453 {
e9cafbcc 11454 arg = skip_spaces (arg);
06a64a0b
TT
11455 just_location = 1;
11456 }
11457
84f4c1fe 11458 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11459}
8926118c 11460
c5aa993b 11461static void
fba45db2 11462watch_command (char *arg, int from_tty)
c906108c 11463{
06a64a0b 11464 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11465}
11466
8b93c638 11467void
84f4c1fe 11468rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11469{
84f4c1fe 11470 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11471}
8926118c 11472
c5aa993b 11473static void
fba45db2 11474rwatch_command (char *arg, int from_tty)
c906108c 11475{
06a64a0b 11476 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11477}
11478
8b93c638 11479void
84f4c1fe 11480awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11481{
84f4c1fe 11482 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11483}
8926118c 11484
c5aa993b 11485static void
fba45db2 11486awatch_command (char *arg, int from_tty)
c906108c 11487{
06a64a0b 11488 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11489}
c906108c 11490\f
c5aa993b 11491
43ff13b4 11492/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11493 because it uses the mechanisms of breakpoints. */
11494
bfec99b2
PA
11495struct until_break_command_continuation_args
11496{
11497 struct breakpoint *breakpoint;
11498 struct breakpoint *breakpoint2;
186c406b 11499 int thread_num;
bfec99b2
PA
11500};
11501
43ff13b4 11502/* This function is called by fetch_inferior_event via the
4a64f543 11503 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11504 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11505 command. */
c2c6d25f 11506static void
fa4cd53f 11507until_break_command_continuation (void *arg, int err)
43ff13b4 11508{
bfec99b2
PA
11509 struct until_break_command_continuation_args *a = arg;
11510
11511 delete_breakpoint (a->breakpoint);
11512 if (a->breakpoint2)
11513 delete_breakpoint (a->breakpoint2);
186c406b 11514 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11515}
11516
c906108c 11517void
ae66c1fc 11518until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11519{
11520 struct symtabs_and_lines sals;
11521 struct symtab_and_line sal;
8556afb4
PA
11522 struct frame_info *frame;
11523 struct gdbarch *frame_gdbarch;
11524 struct frame_id stack_frame_id;
11525 struct frame_id caller_frame_id;
c906108c 11526 struct breakpoint *breakpoint;
f107f563 11527 struct breakpoint *breakpoint2 = NULL;
c906108c 11528 struct cleanup *old_chain;
186c406b
TT
11529 int thread;
11530 struct thread_info *tp;
c906108c
SS
11531
11532 clear_proceed_status ();
11533
11534 /* Set a breakpoint where the user wants it and at return from
4a64f543 11535 this function. */
c5aa993b 11536
1bfeeb0f 11537 if (last_displayed_sal_is_valid ())
f8eba3c6 11538 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11539 get_last_displayed_symtab (),
f8eba3c6 11540 get_last_displayed_line ());
c906108c 11541 else
f8eba3c6
TT
11542 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11543 (struct symtab *) NULL, 0);
c5aa993b 11544
c906108c 11545 if (sals.nelts != 1)
8a3fe4f8 11546 error (_("Couldn't get information on specified line."));
c5aa993b 11547
c906108c 11548 sal = sals.sals[0];
4a64f543 11549 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11550
c906108c 11551 if (*arg)
8a3fe4f8 11552 error (_("Junk at end of arguments."));
c5aa993b 11553
c906108c 11554 resolve_sal_pc (&sal);
c5aa993b 11555
186c406b
TT
11556 tp = inferior_thread ();
11557 thread = tp->num;
11558
883bc8d1
PA
11559 old_chain = make_cleanup (null_cleanup, NULL);
11560
8556afb4
PA
11561 /* Note linespec handling above invalidates the frame chain.
11562 Installing a breakpoint also invalidates the frame chain (as it
11563 may need to switch threads), so do any frame handling before
11564 that. */
11565
11566 frame = get_selected_frame (NULL);
11567 frame_gdbarch = get_frame_arch (frame);
11568 stack_frame_id = get_stack_frame_id (frame);
11569 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11570
ae66c1fc
EZ
11571 /* Keep within the current frame, or in frames called by the current
11572 one. */
edb3359d 11573
883bc8d1 11574 if (frame_id_p (caller_frame_id))
c906108c 11575 {
883bc8d1
PA
11576 struct symtab_and_line sal2;
11577
11578 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11579 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11580 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11581 sal2,
11582 caller_frame_id,
f107f563
VP
11583 bp_until);
11584 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11585
883bc8d1 11586 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11587 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11588 }
c5aa993b 11589
c70a6932
JK
11590 /* set_momentary_breakpoint could invalidate FRAME. */
11591 frame = NULL;
11592
883bc8d1
PA
11593 if (anywhere)
11594 /* If the user told us to continue until a specified location,
11595 we don't specify a frame at which we need to stop. */
11596 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11597 null_frame_id, bp_until);
11598 else
11599 /* Otherwise, specify the selected frame, because we want to stop
11600 only at the very same frame. */
11601 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11602 stack_frame_id, bp_until);
11603 make_cleanup_delete_breakpoint (breakpoint);
11604
a493e3e2 11605 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11606
4a64f543
MS
11607 /* If we are running asynchronously, and proceed call above has
11608 actually managed to start the target, arrange for breakpoints to
11609 be deleted when the target stops. Otherwise, we're already
11610 stopped and delete breakpoints via cleanup chain. */
f107f563 11611
8ea051c5 11612 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11613 {
bfec99b2
PA
11614 struct until_break_command_continuation_args *args;
11615 args = xmalloc (sizeof (*args));
f107f563 11616
bfec99b2
PA
11617 args->breakpoint = breakpoint;
11618 args->breakpoint2 = breakpoint2;
186c406b 11619 args->thread_num = thread;
f107f563
VP
11620
11621 discard_cleanups (old_chain);
95e54da7
PA
11622 add_continuation (inferior_thread (),
11623 until_break_command_continuation, args,
604ead4a 11624 xfree);
f107f563
VP
11625 }
11626 else
c5aa993b 11627 do_cleanups (old_chain);
c906108c 11628}
ae66c1fc 11629
c906108c
SS
11630/* This function attempts to parse an optional "if <cond>" clause
11631 from the arg string. If one is not found, it returns NULL.
c5aa993b 11632
c906108c
SS
11633 Else, it returns a pointer to the condition string. (It does not
11634 attempt to evaluate the string against a particular block.) And,
11635 it updates arg to point to the first character following the parsed
4a64f543 11636 if clause in the arg string. */
53a5351d 11637
916703c0 11638char *
fba45db2 11639ep_parse_optional_if_clause (char **arg)
c906108c 11640{
c5aa993b
JM
11641 char *cond_string;
11642
11643 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11644 return NULL;
c5aa993b 11645
4a64f543 11646 /* Skip the "if" keyword. */
c906108c 11647 (*arg) += 2;
c5aa993b 11648
c906108c 11649 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11650 condition string. */
e9cafbcc 11651 *arg = skip_spaces (*arg);
c906108c 11652 cond_string = *arg;
c5aa993b 11653
4a64f543
MS
11654 /* Assume that the condition occupies the remainder of the arg
11655 string. */
c906108c 11656 (*arg) += strlen (cond_string);
c5aa993b 11657
c906108c
SS
11658 return cond_string;
11659}
c5aa993b 11660
c906108c
SS
11661/* Commands to deal with catching events, such as signals, exceptions,
11662 process start/exit, etc. */
c5aa993b
JM
11663
11664typedef enum
11665{
44feb3ce
TT
11666 catch_fork_temporary, catch_vfork_temporary,
11667 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11668}
11669catch_fork_kind;
11670
c906108c 11671static void
cc59ec59
MS
11672catch_fork_command_1 (char *arg, int from_tty,
11673 struct cmd_list_element *command)
c906108c 11674{
a6d9a66e 11675 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11676 char *cond_string = NULL;
44feb3ce
TT
11677 catch_fork_kind fork_kind;
11678 int tempflag;
11679
11680 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11681 tempflag = (fork_kind == catch_fork_temporary
11682 || fork_kind == catch_vfork_temporary);
c5aa993b 11683
44feb3ce
TT
11684 if (!arg)
11685 arg = "";
e9cafbcc 11686 arg = skip_spaces (arg);
c5aa993b 11687
c906108c 11688 /* The allowed syntax is:
c5aa993b
JM
11689 catch [v]fork
11690 catch [v]fork if <cond>
11691
4a64f543 11692 First, check if there's an if clause. */
c906108c 11693 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11694
c906108c 11695 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11696 error (_("Junk at end of arguments."));
c5aa993b 11697
c906108c 11698 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11699 and enable reporting of such events. */
c5aa993b
JM
11700 switch (fork_kind)
11701 {
44feb3ce
TT
11702 case catch_fork_temporary:
11703 case catch_fork_permanent:
a6d9a66e 11704 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11705 &catch_fork_breakpoint_ops);
c906108c 11706 break;
44feb3ce
TT
11707 case catch_vfork_temporary:
11708 case catch_vfork_permanent:
a6d9a66e 11709 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11710 &catch_vfork_breakpoint_ops);
c906108c 11711 break;
c5aa993b 11712 default:
8a3fe4f8 11713 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11714 break;
c5aa993b 11715 }
c906108c
SS
11716}
11717
11718static void
cc59ec59
MS
11719catch_exec_command_1 (char *arg, int from_tty,
11720 struct cmd_list_element *command)
c906108c 11721{
b4d90040 11722 struct exec_catchpoint *c;
a6d9a66e 11723 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11724 int tempflag;
c5aa993b 11725 char *cond_string = NULL;
c906108c 11726
44feb3ce
TT
11727 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11728
11729 if (!arg)
11730 arg = "";
e9cafbcc 11731 arg = skip_spaces (arg);
c906108c
SS
11732
11733 /* The allowed syntax is:
c5aa993b
JM
11734 catch exec
11735 catch exec if <cond>
c906108c 11736
4a64f543 11737 First, check if there's an if clause. */
c906108c
SS
11738 cond_string = ep_parse_optional_if_clause (&arg);
11739
11740 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11741 error (_("Junk at end of arguments."));
c906108c 11742
b4d90040
PA
11743 c = XNEW (struct exec_catchpoint);
11744 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11745 &catch_exec_breakpoint_ops);
11746 c->exec_pathname = NULL;
11747
3ea46bff 11748 install_breakpoint (0, &c->base, 1);
c906108c 11749}
c5aa993b 11750
9ac4176b 11751void
28010a5d
PA
11752init_ada_exception_breakpoint (struct breakpoint *b,
11753 struct gdbarch *gdbarch,
11754 struct symtab_and_line sal,
11755 char *addr_string,
c0a91b2b 11756 const struct breakpoint_ops *ops,
28010a5d 11757 int tempflag,
349774ef 11758 int enabled,
28010a5d 11759 int from_tty)
f7f9143b 11760{
f7f9143b
JB
11761 if (from_tty)
11762 {
5af949e3
UW
11763 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11764 if (!loc_gdbarch)
11765 loc_gdbarch = gdbarch;
11766
6c95b8df
PA
11767 describe_other_breakpoints (loc_gdbarch,
11768 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11769 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11770 version for exception catchpoints, because two catchpoints
11771 used for different exception names will use the same address.
11772 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11773 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11774 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11775 the user what type of catchpoint it is. The above is good
11776 enough for now, though. */
11777 }
11778
28010a5d 11779 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11780
349774ef 11781 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11782 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11783 b->addr_string = addr_string;
11784 b->language = language_ada;
f7f9143b
JB
11785}
11786
a96d9b2e
SDJ
11787/* Splits the argument using space as delimiter. Returns an xmalloc'd
11788 filter list, or NULL if no filtering is required. */
11789static VEC(int) *
11790catch_syscall_split_args (char *arg)
11791{
11792 VEC(int) *result = NULL;
29d0bb3d 11793 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
11794
11795 while (*arg != '\0')
11796 {
11797 int i, syscall_number;
11798 char *endptr;
11799 char cur_name[128];
11800 struct syscall s;
11801
11802 /* Skip whitespace. */
529480d0 11803 arg = skip_spaces (arg);
a96d9b2e
SDJ
11804
11805 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11806 cur_name[i] = arg[i];
11807 cur_name[i] = '\0';
11808 arg += i;
11809
11810 /* Check if the user provided a syscall name or a number. */
11811 syscall_number = (int) strtol (cur_name, &endptr, 0);
11812 if (*endptr == '\0')
bccd0dd2 11813 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
11814 else
11815 {
11816 /* We have a name. Let's check if it's valid and convert it
11817 to a number. */
11818 get_syscall_by_name (cur_name, &s);
11819
11820 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
11821 /* Here we have to issue an error instead of a warning,
11822 because GDB cannot do anything useful if there's no
11823 syscall number to be caught. */
a96d9b2e
SDJ
11824 error (_("Unknown syscall name '%s'."), cur_name);
11825 }
11826
11827 /* Ok, it's valid. */
11828 VEC_safe_push (int, result, s.number);
11829 }
11830
11831 discard_cleanups (cleanup);
11832 return result;
11833}
11834
11835/* Implement the "catch syscall" command. */
11836
11837static void
cc59ec59
MS
11838catch_syscall_command_1 (char *arg, int from_tty,
11839 struct cmd_list_element *command)
a96d9b2e
SDJ
11840{
11841 int tempflag;
11842 VEC(int) *filter;
11843 struct syscall s;
11844 struct gdbarch *gdbarch = get_current_arch ();
11845
11846 /* Checking if the feature if supported. */
11847 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11848 error (_("The feature 'catch syscall' is not supported on \
ea666128 11849this architecture yet."));
a96d9b2e
SDJ
11850
11851 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11852
e9cafbcc 11853 arg = skip_spaces (arg);
a96d9b2e
SDJ
11854
11855 /* We need to do this first "dummy" translation in order
11856 to get the syscall XML file loaded or, most important,
11857 to display a warning to the user if there's no XML file
11858 for his/her architecture. */
11859 get_syscall_by_number (0, &s);
11860
11861 /* The allowed syntax is:
11862 catch syscall
11863 catch syscall <name | number> [<name | number> ... <name | number>]
11864
11865 Let's check if there's a syscall name. */
11866
11867 if (arg != NULL)
11868 filter = catch_syscall_split_args (arg);
11869 else
11870 filter = NULL;
11871
11872 create_syscall_event_catchpoint (tempflag, filter,
11873 &catch_syscall_breakpoint_ops);
11874}
11875
c906108c 11876static void
fba45db2 11877catch_command (char *arg, int from_tty)
c906108c 11878{
44feb3ce 11879 error (_("Catch requires an event name."));
c906108c
SS
11880}
11881\f
11882
11883static void
fba45db2 11884tcatch_command (char *arg, int from_tty)
c906108c 11885{
44feb3ce 11886 error (_("Catch requires an event name."));
c906108c
SS
11887}
11888
8a2c437b
TT
11889/* A qsort comparison function that sorts breakpoints in order. */
11890
11891static int
11892compare_breakpoints (const void *a, const void *b)
11893{
11894 const breakpoint_p *ba = a;
11895 uintptr_t ua = (uintptr_t) *ba;
11896 const breakpoint_p *bb = b;
11897 uintptr_t ub = (uintptr_t) *bb;
11898
11899 if ((*ba)->number < (*bb)->number)
11900 return -1;
11901 else if ((*ba)->number > (*bb)->number)
11902 return 1;
11903
11904 /* Now sort by address, in case we see, e..g, two breakpoints with
11905 the number 0. */
11906 if (ua < ub)
11907 return -1;
94b0e70d 11908 return ua > ub ? 1 : 0;
8a2c437b
TT
11909}
11910
80f8a6eb 11911/* Delete breakpoints by address or line. */
c906108c
SS
11912
11913static void
fba45db2 11914clear_command (char *arg, int from_tty)
c906108c 11915{
8a2c437b 11916 struct breakpoint *b, *prev;
d6e956e5
VP
11917 VEC(breakpoint_p) *found = 0;
11918 int ix;
c906108c
SS
11919 int default_match;
11920 struct symtabs_and_lines sals;
11921 struct symtab_and_line sal;
c906108c 11922 int i;
8a2c437b 11923 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11924
11925 if (arg)
11926 {
39cf75f7
DE
11927 sals = decode_line_with_current_source (arg,
11928 (DECODE_LINE_FUNFIRSTLINE
11929 | DECODE_LINE_LIST_MODE));
cf4ded82 11930 make_cleanup (xfree, sals.sals);
c906108c
SS
11931 default_match = 0;
11932 }
11933 else
11934 {
c5aa993b 11935 sals.sals = (struct symtab_and_line *)
c906108c 11936 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 11937 make_cleanup (xfree, sals.sals);
4a64f543 11938 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11939
11940 /* Set sal's line, symtab, pc, and pspace to the values
11941 corresponding to the last call to print_frame_info. If the
11942 codepoint is not valid, this will set all the fields to 0. */
11943 get_last_displayed_sal (&sal);
c906108c 11944 if (sal.symtab == 0)
8a3fe4f8 11945 error (_("No source file specified."));
c906108c
SS
11946
11947 sals.sals[0] = sal;
11948 sals.nelts = 1;
11949
11950 default_match = 1;
11951 }
11952
4a64f543
MS
11953 /* We don't call resolve_sal_pc here. That's not as bad as it
11954 seems, because all existing breakpoints typically have both
11955 file/line and pc set. So, if clear is given file/line, we can
11956 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11957
11958 We only support clearing given the address explicitly
11959 present in breakpoint table. Say, we've set breakpoint
4a64f543 11960 at file:line. There were several PC values for that file:line,
ed0616c6 11961 due to optimization, all in one block.
4a64f543
MS
11962
11963 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11964 PC corresponding to the same file:line, the breakpoint won't
11965 be cleared. We probably can still clear the breakpoint, but
11966 since the other PC value is never presented to user, user
11967 can only find it by guessing, and it does not seem important
11968 to support that. */
11969
4a64f543
MS
11970 /* For each line spec given, delete bps which correspond to it. Do
11971 it in two passes, solely to preserve the current behavior that
11972 from_tty is forced true if we delete more than one
11973 breakpoint. */
c906108c 11974
80f8a6eb 11975 found = NULL;
8a2c437b 11976 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11977 for (i = 0; i < sals.nelts; i++)
11978 {
05cba821
JK
11979 const char *sal_fullname;
11980
c906108c 11981 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11982 If line given (pc == 0), clear all bpts on specified line.
11983 If defaulting, clear all bpts on default line
c906108c 11984 or at default pc.
c5aa993b
JM
11985
11986 defaulting sal.pc != 0 tests to do
11987
11988 0 1 pc
11989 1 1 pc _and_ line
11990 0 0 line
11991 1 0 <can't happen> */
c906108c
SS
11992
11993 sal = sals.sals[i];
05cba821
JK
11994 sal_fullname = (sal.symtab == NULL
11995 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11996
4a64f543 11997 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11998 ALL_BREAKPOINTS (b)
c5aa993b 11999 {
0d381245 12000 int match = 0;
4a64f543 12001 /* Are we going to delete b? */
cc60f2e3 12002 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12003 {
12004 struct bp_location *loc = b->loc;
12005 for (; loc; loc = loc->next)
12006 {
f8eba3c6
TT
12007 /* If the user specified file:line, don't allow a PC
12008 match. This matches historical gdb behavior. */
12009 int pc_match = (!sal.explicit_line
12010 && sal.pc
12011 && (loc->pspace == sal.pspace)
12012 && (loc->address == sal.pc)
12013 && (!section_is_overlay (loc->section)
12014 || loc->section == sal.section));
4aac40c8
TT
12015 int line_match = 0;
12016
12017 if ((default_match || sal.explicit_line)
2f202fde 12018 && loc->symtab != NULL
05cba821 12019 && sal_fullname != NULL
4aac40c8 12020 && sal.pspace == loc->pspace
05cba821
JK
12021 && loc->line_number == sal.line
12022 && filename_cmp (symtab_to_fullname (loc->symtab),
12023 sal_fullname) == 0)
12024 line_match = 1;
4aac40c8 12025
0d381245
VP
12026 if (pc_match || line_match)
12027 {
12028 match = 1;
12029 break;
12030 }
12031 }
12032 }
12033
12034 if (match)
d6e956e5 12035 VEC_safe_push(breakpoint_p, found, b);
c906108c 12036 }
80f8a6eb 12037 }
8a2c437b 12038
80f8a6eb 12039 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12040 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12041 {
12042 if (arg)
8a3fe4f8 12043 error (_("No breakpoint at %s."), arg);
80f8a6eb 12044 else
8a3fe4f8 12045 error (_("No breakpoint at this line."));
80f8a6eb 12046 }
c906108c 12047
8a2c437b
TT
12048 /* Remove duplicates from the vec. */
12049 qsort (VEC_address (breakpoint_p, found),
12050 VEC_length (breakpoint_p, found),
12051 sizeof (breakpoint_p),
12052 compare_breakpoints);
12053 prev = VEC_index (breakpoint_p, found, 0);
12054 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12055 {
12056 if (b == prev)
12057 {
12058 VEC_ordered_remove (breakpoint_p, found, ix);
12059 --ix;
12060 }
12061 }
12062
d6e956e5 12063 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12064 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12065 if (from_tty)
a3f17187 12066 {
d6e956e5 12067 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12068 printf_unfiltered (_("Deleted breakpoint "));
12069 else
12070 printf_unfiltered (_("Deleted breakpoints "));
12071 }
d6e956e5
VP
12072
12073 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12074 {
c5aa993b 12075 if (from_tty)
d6e956e5
VP
12076 printf_unfiltered ("%d ", b->number);
12077 delete_breakpoint (b);
c906108c 12078 }
80f8a6eb
MS
12079 if (from_tty)
12080 putchar_unfiltered ('\n');
8a2c437b
TT
12081
12082 do_cleanups (cleanups);
c906108c
SS
12083}
12084\f
12085/* Delete breakpoint in BS if they are `delete' breakpoints and
12086 all breakpoints that are marked for deletion, whether hit or not.
12087 This is called after any breakpoint is hit, or after errors. */
12088
12089void
fba45db2 12090breakpoint_auto_delete (bpstat bs)
c906108c 12091{
35df4500 12092 struct breakpoint *b, *b_tmp;
c906108c
SS
12093
12094 for (; bs; bs = bs->next)
f431efe5
PA
12095 if (bs->breakpoint_at
12096 && bs->breakpoint_at->disposition == disp_del
c906108c 12097 && bs->stop)
f431efe5 12098 delete_breakpoint (bs->breakpoint_at);
c906108c 12099
35df4500 12100 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12101 {
b5de0fa7 12102 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12103 delete_breakpoint (b);
12104 }
c906108c
SS
12105}
12106
4a64f543
MS
12107/* A comparison function for bp_location AP and BP being interfaced to
12108 qsort. Sort elements primarily by their ADDRESS (no matter what
12109 does breakpoint_address_is_meaningful say for its OWNER),
12110 secondarily by ordering first bp_permanent OWNERed elements and
12111 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12112 qsort being an unstable algorithm. */
876fa593
JK
12113
12114static int
494cfb0f 12115bp_location_compare (const void *ap, const void *bp)
876fa593 12116{
494cfb0f
JK
12117 struct bp_location *a = *(void **) ap;
12118 struct bp_location *b = *(void **) bp;
2bdf28a0 12119 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
12120 int a_perm = a->owner->enable_state == bp_permanent;
12121 int b_perm = b->owner->enable_state == bp_permanent;
12122
12123 if (a->address != b->address)
12124 return (a->address > b->address) - (a->address < b->address);
12125
dea2aa5f
LM
12126 /* Sort locations at the same address by their pspace number, keeping
12127 locations of the same inferior (in a multi-inferior environment)
12128 grouped. */
12129
12130 if (a->pspace->num != b->pspace->num)
12131 return ((a->pspace->num > b->pspace->num)
12132 - (a->pspace->num < b->pspace->num));
12133
876fa593
JK
12134 /* Sort permanent breakpoints first. */
12135 if (a_perm != b_perm)
12136 return (a_perm < b_perm) - (a_perm > b_perm);
12137
c56a97f9
JK
12138 /* Make the internal GDB representation stable across GDB runs
12139 where A and B memory inside GDB can differ. Breakpoint locations of
12140 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12141
12142 if (a->owner->number != b->owner->number)
c56a97f9
JK
12143 return ((a->owner->number > b->owner->number)
12144 - (a->owner->number < b->owner->number));
876fa593
JK
12145
12146 return (a > b) - (a < b);
12147}
12148
876fa593 12149/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12150 bp_location_shadow_len_after_address_max according to the current
12151 content of the bp_location array. */
f7545552
TT
12152
12153static void
876fa593 12154bp_location_target_extensions_update (void)
f7545552 12155{
876fa593
JK
12156 struct bp_location *bl, **blp_tmp;
12157
12158 bp_location_placed_address_before_address_max = 0;
12159 bp_location_shadow_len_after_address_max = 0;
12160
12161 ALL_BP_LOCATIONS (bl, blp_tmp)
12162 {
12163 CORE_ADDR start, end, addr;
12164
12165 if (!bp_location_has_shadow (bl))
12166 continue;
12167
12168 start = bl->target_info.placed_address;
12169 end = start + bl->target_info.shadow_len;
12170
12171 gdb_assert (bl->address >= start);
12172 addr = bl->address - start;
12173 if (addr > bp_location_placed_address_before_address_max)
12174 bp_location_placed_address_before_address_max = addr;
12175
12176 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12177
12178 gdb_assert (bl->address < end);
12179 addr = end - bl->address;
12180 if (addr > bp_location_shadow_len_after_address_max)
12181 bp_location_shadow_len_after_address_max = addr;
12182 }
f7545552
TT
12183}
12184
1e4d1764
YQ
12185/* Download tracepoint locations if they haven't been. */
12186
12187static void
12188download_tracepoint_locations (void)
12189{
7ed2c994 12190 struct breakpoint *b;
1e4d1764
YQ
12191 struct cleanup *old_chain;
12192
12193 if (!target_can_download_tracepoint ())
12194 return;
12195
12196 old_chain = save_current_space_and_thread ();
12197
7ed2c994 12198 ALL_TRACEPOINTS (b)
1e4d1764 12199 {
7ed2c994 12200 struct bp_location *bl;
1e4d1764 12201 struct tracepoint *t;
f2a8bc8a 12202 int bp_location_downloaded = 0;
1e4d1764 12203
7ed2c994 12204 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12205 ? !may_insert_fast_tracepoints
12206 : !may_insert_tracepoints))
12207 continue;
12208
7ed2c994
YQ
12209 for (bl = b->loc; bl; bl = bl->next)
12210 {
12211 /* In tracepoint, locations are _never_ duplicated, so
12212 should_be_inserted is equivalent to
12213 unduplicated_should_be_inserted. */
12214 if (!should_be_inserted (bl) || bl->inserted)
12215 continue;
1e4d1764 12216
7ed2c994 12217 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12218
7ed2c994 12219 target_download_tracepoint (bl);
1e4d1764 12220
7ed2c994 12221 bl->inserted = 1;
f2a8bc8a 12222 bp_location_downloaded = 1;
7ed2c994
YQ
12223 }
12224 t = (struct tracepoint *) b;
12225 t->number_on_target = b->number;
f2a8bc8a
YQ
12226 if (bp_location_downloaded)
12227 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12228 }
12229
12230 do_cleanups (old_chain);
12231}
12232
934709f0
PW
12233/* Swap the insertion/duplication state between two locations. */
12234
12235static void
12236swap_insertion (struct bp_location *left, struct bp_location *right)
12237{
12238 const int left_inserted = left->inserted;
12239 const int left_duplicate = left->duplicate;
b775012e 12240 const int left_needs_update = left->needs_update;
934709f0
PW
12241 const struct bp_target_info left_target_info = left->target_info;
12242
1e4d1764
YQ
12243 /* Locations of tracepoints can never be duplicated. */
12244 if (is_tracepoint (left->owner))
12245 gdb_assert (!left->duplicate);
12246 if (is_tracepoint (right->owner))
12247 gdb_assert (!right->duplicate);
12248
934709f0
PW
12249 left->inserted = right->inserted;
12250 left->duplicate = right->duplicate;
b775012e 12251 left->needs_update = right->needs_update;
934709f0
PW
12252 left->target_info = right->target_info;
12253 right->inserted = left_inserted;
12254 right->duplicate = left_duplicate;
b775012e 12255 right->needs_update = left_needs_update;
934709f0
PW
12256 right->target_info = left_target_info;
12257}
12258
b775012e
LM
12259/* Force the re-insertion of the locations at ADDRESS. This is called
12260 once a new/deleted/modified duplicate location is found and we are evaluating
12261 conditions on the target's side. Such conditions need to be updated on
12262 the target. */
12263
12264static void
12265force_breakpoint_reinsertion (struct bp_location *bl)
12266{
12267 struct bp_location **locp = NULL, **loc2p;
12268 struct bp_location *loc;
12269 CORE_ADDR address = 0;
12270 int pspace_num;
12271
12272 address = bl->address;
12273 pspace_num = bl->pspace->num;
12274
12275 /* This is only meaningful if the target is
12276 evaluating conditions and if the user has
12277 opted for condition evaluation on the target's
12278 side. */
12279 if (gdb_evaluates_breakpoint_condition_p ()
12280 || !target_supports_evaluation_of_breakpoint_conditions ())
12281 return;
12282
12283 /* Flag all breakpoint locations with this address and
12284 the same program space as the location
12285 as "its condition has changed". We need to
12286 update the conditions on the target's side. */
12287 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12288 {
12289 loc = *loc2p;
12290
12291 if (!is_breakpoint (loc->owner)
12292 || pspace_num != loc->pspace->num)
12293 continue;
12294
12295 /* Flag the location appropriately. We use a different state to
12296 let everyone know that we already updated the set of locations
12297 with addr bl->address and program space bl->pspace. This is so
12298 we don't have to keep calling these functions just to mark locations
12299 that have already been marked. */
12300 loc->condition_changed = condition_updated;
12301
12302 /* Free the agent expression bytecode as well. We will compute
12303 it later on. */
12304 if (loc->cond_bytecode)
12305 {
12306 free_agent_expr (loc->cond_bytecode);
12307 loc->cond_bytecode = NULL;
12308 }
12309 }
12310}
12311
4cd9bd08 12312/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
12313 into the inferior, only remove already-inserted locations that no
12314 longer should be inserted. Functions that delete a breakpoint or
12315 breakpoints should pass false, so that deleting a breakpoint
12316 doesn't have the side effect of inserting the locations of other
12317 breakpoints that are marked not-inserted, but should_be_inserted
12318 returns true on them.
12319
12320 This behaviour is useful is situations close to tear-down -- e.g.,
12321 after an exec, while the target still has execution, but breakpoint
12322 shadows of the previous executable image should *NOT* be restored
12323 to the new image; or before detaching, where the target still has
12324 execution and wants to delete breakpoints from GDB's lists, and all
12325 breakpoints had already been removed from the inferior. */
12326
0d381245 12327static void
b60e7edf 12328update_global_location_list (int should_insert)
0d381245 12329{
74960c60 12330 struct breakpoint *b;
876fa593 12331 struct bp_location **locp, *loc;
f7545552 12332 struct cleanup *cleanups;
b775012e
LM
12333 /* Last breakpoint location address that was marked for update. */
12334 CORE_ADDR last_addr = 0;
12335 /* Last breakpoint location program space that was marked for update. */
12336 int last_pspace_num = -1;
f7545552 12337
2d134ed3
PA
12338 /* Used in the duplicates detection below. When iterating over all
12339 bp_locations, points to the first bp_location of a given address.
12340 Breakpoints and watchpoints of different types are never
12341 duplicates of each other. Keep one pointer for each type of
12342 breakpoint/watchpoint, so we only need to loop over all locations
12343 once. */
12344 struct bp_location *bp_loc_first; /* breakpoint */
12345 struct bp_location *wp_loc_first; /* hardware watchpoint */
12346 struct bp_location *awp_loc_first; /* access watchpoint */
12347 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12348
4a64f543
MS
12349 /* Saved former bp_location array which we compare against the newly
12350 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12351 struct bp_location **old_location, **old_locp;
12352 unsigned old_location_count;
12353
12354 old_location = bp_location;
12355 old_location_count = bp_location_count;
12356 bp_location = NULL;
12357 bp_location_count = 0;
12358 cleanups = make_cleanup (xfree, old_location);
0d381245 12359
74960c60 12360 ALL_BREAKPOINTS (b)
876fa593
JK
12361 for (loc = b->loc; loc; loc = loc->next)
12362 bp_location_count++;
12363
12364 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12365 locp = bp_location;
12366 ALL_BREAKPOINTS (b)
12367 for (loc = b->loc; loc; loc = loc->next)
12368 *locp++ = loc;
12369 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12370 bp_location_compare);
876fa593
JK
12371
12372 bp_location_target_extensions_update ();
74960c60 12373
4a64f543
MS
12374 /* Identify bp_location instances that are no longer present in the
12375 new list, and therefore should be freed. Note that it's not
12376 necessary that those locations should be removed from inferior --
12377 if there's another location at the same address (previously
12378 marked as duplicate), we don't need to remove/insert the
12379 location.
876fa593 12380
4a64f543
MS
12381 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12382 and former bp_location array state respectively. */
876fa593
JK
12383
12384 locp = bp_location;
12385 for (old_locp = old_location; old_locp < old_location + old_location_count;
12386 old_locp++)
74960c60 12387 {
876fa593 12388 struct bp_location *old_loc = *old_locp;
c7d46a38 12389 struct bp_location **loc2p;
876fa593 12390
e5dd4106 12391 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12392 not, we have to free it. */
c7d46a38 12393 int found_object = 0;
20874c92
VP
12394 /* Tells if the location should remain inserted in the target. */
12395 int keep_in_target = 0;
12396 int removed = 0;
876fa593 12397
4a64f543
MS
12398 /* Skip LOCP entries which will definitely never be needed.
12399 Stop either at or being the one matching OLD_LOC. */
876fa593 12400 while (locp < bp_location + bp_location_count
c7d46a38 12401 && (*locp)->address < old_loc->address)
876fa593 12402 locp++;
c7d46a38
PA
12403
12404 for (loc2p = locp;
12405 (loc2p < bp_location + bp_location_count
12406 && (*loc2p)->address == old_loc->address);
12407 loc2p++)
12408 {
b775012e
LM
12409 /* Check if this is a new/duplicated location or a duplicated
12410 location that had its condition modified. If so, we want to send
12411 its condition to the target if evaluation of conditions is taking
12412 place there. */
12413 if ((*loc2p)->condition_changed == condition_modified
12414 && (last_addr != old_loc->address
12415 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12416 {
b775012e
LM
12417 force_breakpoint_reinsertion (*loc2p);
12418 last_pspace_num = old_loc->pspace->num;
c7d46a38 12419 }
b775012e
LM
12420
12421 if (*loc2p == old_loc)
12422 found_object = 1;
c7d46a38 12423 }
74960c60 12424
b775012e
LM
12425 /* We have already handled this address, update it so that we don't
12426 have to go through updates again. */
12427 last_addr = old_loc->address;
12428
12429 /* Target-side condition evaluation: Handle deleted locations. */
12430 if (!found_object)
12431 force_breakpoint_reinsertion (old_loc);
12432
4a64f543
MS
12433 /* If this location is no longer present, and inserted, look if
12434 there's maybe a new location at the same address. If so,
12435 mark that one inserted, and don't remove this one. This is
12436 needed so that we don't have a time window where a breakpoint
12437 at certain location is not inserted. */
74960c60 12438
876fa593 12439 if (old_loc->inserted)
0d381245 12440 {
4a64f543
MS
12441 /* If the location is inserted now, we might have to remove
12442 it. */
74960c60 12443
876fa593 12444 if (found_object && should_be_inserted (old_loc))
74960c60 12445 {
4a64f543
MS
12446 /* The location is still present in the location list,
12447 and still should be inserted. Don't do anything. */
20874c92 12448 keep_in_target = 1;
74960c60
VP
12449 }
12450 else
12451 {
b775012e
LM
12452 /* This location still exists, but it won't be kept in the
12453 target since it may have been disabled. We proceed to
12454 remove its target-side condition. */
12455
4a64f543
MS
12456 /* The location is either no longer present, or got
12457 disabled. See if there's another location at the
12458 same address, in which case we don't need to remove
12459 this one from the target. */
876fa593 12460
2bdf28a0 12461 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12462 if (breakpoint_address_is_meaningful (old_loc->owner))
12463 {
876fa593 12464 for (loc2p = locp;
c7d46a38
PA
12465 (loc2p < bp_location + bp_location_count
12466 && (*loc2p)->address == old_loc->address);
876fa593
JK
12467 loc2p++)
12468 {
12469 struct bp_location *loc2 = *loc2p;
12470
2d134ed3 12471 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12472 {
85d721b8
PA
12473 /* Read watchpoint locations are switched to
12474 access watchpoints, if the former are not
12475 supported, but the latter are. */
12476 if (is_hardware_watchpoint (old_loc->owner))
12477 {
12478 gdb_assert (is_hardware_watchpoint (loc2->owner));
12479 loc2->watchpoint_type = old_loc->watchpoint_type;
12480 }
12481
934709f0
PW
12482 /* loc2 is a duplicated location. We need to check
12483 if it should be inserted in case it will be
12484 unduplicated. */
12485 if (loc2 != old_loc
12486 && unduplicated_should_be_inserted (loc2))
c7d46a38 12487 {
934709f0 12488 swap_insertion (old_loc, loc2);
c7d46a38
PA
12489 keep_in_target = 1;
12490 break;
12491 }
876fa593
JK
12492 }
12493 }
12494 }
74960c60
VP
12495 }
12496
20874c92
VP
12497 if (!keep_in_target)
12498 {
876fa593 12499 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12500 {
4a64f543
MS
12501 /* This is just about all we can do. We could keep
12502 this location on the global list, and try to
12503 remove it next time, but there's no particular
12504 reason why we will succeed next time.
20874c92 12505
4a64f543
MS
12506 Note that at this point, old_loc->owner is still
12507 valid, as delete_breakpoint frees the breakpoint
12508 only after calling us. */
3e43a32a
MS
12509 printf_filtered (_("warning: Error removing "
12510 "breakpoint %d\n"),
876fa593 12511 old_loc->owner->number);
20874c92
VP
12512 }
12513 removed = 1;
12514 }
0d381245 12515 }
74960c60
VP
12516
12517 if (!found_object)
1c5cfe86 12518 {
db82e815
PA
12519 if (removed && non_stop
12520 && breakpoint_address_is_meaningful (old_loc->owner)
12521 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12522 {
db82e815
PA
12523 /* This location was removed from the target. In
12524 non-stop mode, a race condition is possible where
12525 we've removed a breakpoint, but stop events for that
12526 breakpoint are already queued and will arrive later.
12527 We apply an heuristic to be able to distinguish such
12528 SIGTRAPs from other random SIGTRAPs: we keep this
12529 breakpoint location for a bit, and will retire it
12530 after we see some number of events. The theory here
12531 is that reporting of events should, "on the average",
12532 be fair, so after a while we'll see events from all
12533 threads that have anything of interest, and no longer
12534 need to keep this breakpoint location around. We
12535 don't hold locations forever so to reduce chances of
12536 mistaking a non-breakpoint SIGTRAP for a breakpoint
12537 SIGTRAP.
12538
12539 The heuristic failing can be disastrous on
12540 decr_pc_after_break targets.
12541
12542 On decr_pc_after_break targets, like e.g., x86-linux,
12543 if we fail to recognize a late breakpoint SIGTRAP,
12544 because events_till_retirement has reached 0 too
12545 soon, we'll fail to do the PC adjustment, and report
12546 a random SIGTRAP to the user. When the user resumes
12547 the inferior, it will most likely immediately crash
2dec564e 12548 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12549 corrupted, because of being resumed e.g., in the
12550 middle of a multi-byte instruction, or skipped a
12551 one-byte instruction. This was actually seen happen
12552 on native x86-linux, and should be less rare on
12553 targets that do not support new thread events, like
12554 remote, due to the heuristic depending on
12555 thread_count.
12556
12557 Mistaking a random SIGTRAP for a breakpoint trap
12558 causes similar symptoms (PC adjustment applied when
12559 it shouldn't), but then again, playing with SIGTRAPs
12560 behind the debugger's back is asking for trouble.
12561
12562 Since hardware watchpoint traps are always
12563 distinguishable from other traps, so we don't need to
12564 apply keep hardware watchpoint moribund locations
12565 around. We simply always ignore hardware watchpoint
12566 traps we can no longer explain. */
12567
876fa593
JK
12568 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12569 old_loc->owner = NULL;
20874c92 12570
876fa593 12571 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12572 }
12573 else
f431efe5
PA
12574 {
12575 old_loc->owner = NULL;
12576 decref_bp_location (&old_loc);
12577 }
20874c92 12578 }
74960c60 12579 }
1c5cfe86 12580
348d480f
PA
12581 /* Rescan breakpoints at the same address and section, marking the
12582 first one as "first" and any others as "duplicates". This is so
12583 that the bpt instruction is only inserted once. If we have a
12584 permanent breakpoint at the same place as BPT, make that one the
12585 official one, and the rest as duplicates. Permanent breakpoints
12586 are sorted first for the same address.
12587
12588 Do the same for hardware watchpoints, but also considering the
12589 watchpoint's type (regular/access/read) and length. */
12590
12591 bp_loc_first = NULL;
12592 wp_loc_first = NULL;
12593 awp_loc_first = NULL;
12594 rwp_loc_first = NULL;
12595 ALL_BP_LOCATIONS (loc, locp)
12596 {
12597 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12598 non-NULL. */
348d480f 12599 struct bp_location **loc_first_p;
d3fbdd86 12600 b = loc->owner;
348d480f 12601
6f380991 12602 if (!unduplicated_should_be_inserted (loc)
348d480f 12603 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12604 /* Don't detect duplicate for tracepoint locations because they are
12605 never duplicated. See the comments in field `duplicate' of
12606 `struct bp_location'. */
348d480f 12607 || is_tracepoint (b))
b775012e
LM
12608 {
12609 /* Clear the condition modification flag. */
12610 loc->condition_changed = condition_unchanged;
12611 continue;
12612 }
348d480f
PA
12613
12614 /* Permanent breakpoint should always be inserted. */
12615 if (b->enable_state == bp_permanent && ! loc->inserted)
12616 internal_error (__FILE__, __LINE__,
12617 _("allegedly permanent breakpoint is not "
12618 "actually inserted"));
12619
12620 if (b->type == bp_hardware_watchpoint)
12621 loc_first_p = &wp_loc_first;
12622 else if (b->type == bp_read_watchpoint)
12623 loc_first_p = &rwp_loc_first;
12624 else if (b->type == bp_access_watchpoint)
12625 loc_first_p = &awp_loc_first;
12626 else
12627 loc_first_p = &bp_loc_first;
12628
12629 if (*loc_first_p == NULL
12630 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12631 || !breakpoint_locations_match (loc, *loc_first_p))
12632 {
12633 *loc_first_p = loc;
12634 loc->duplicate = 0;
b775012e
LM
12635
12636 if (is_breakpoint (loc->owner) && loc->condition_changed)
12637 {
12638 loc->needs_update = 1;
12639 /* Clear the condition modification flag. */
12640 loc->condition_changed = condition_unchanged;
12641 }
348d480f
PA
12642 continue;
12643 }
12644
934709f0
PW
12645
12646 /* This and the above ensure the invariant that the first location
12647 is not duplicated, and is the inserted one.
12648 All following are marked as duplicated, and are not inserted. */
12649 if (loc->inserted)
12650 swap_insertion (loc, *loc_first_p);
348d480f
PA
12651 loc->duplicate = 1;
12652
b775012e
LM
12653 /* Clear the condition modification flag. */
12654 loc->condition_changed = condition_unchanged;
12655
348d480f
PA
12656 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12657 && b->enable_state != bp_permanent)
12658 internal_error (__FILE__, __LINE__,
12659 _("another breakpoint was inserted on top of "
12660 "a permanent breakpoint"));
12661 }
12662
b775012e 12663 if (breakpoints_always_inserted_mode ()
348d480f 12664 && (have_live_inferiors ()
f5656ead 12665 || (gdbarch_has_global_breakpoints (target_gdbarch ()))))
b775012e
LM
12666 {
12667 if (should_insert)
12668 insert_breakpoint_locations ();
12669 else
12670 {
12671 /* Though should_insert is false, we may need to update conditions
12672 on the target's side if it is evaluating such conditions. We
12673 only update conditions for locations that are marked
12674 "needs_update". */
12675 update_inserted_breakpoint_locations ();
12676 }
12677 }
348d480f 12678
1e4d1764
YQ
12679 if (should_insert)
12680 download_tracepoint_locations ();
12681
348d480f
PA
12682 do_cleanups (cleanups);
12683}
12684
12685void
12686breakpoint_retire_moribund (void)
12687{
12688 struct bp_location *loc;
12689 int ix;
12690
12691 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12692 if (--(loc->events_till_retirement) == 0)
12693 {
12694 decref_bp_location (&loc);
12695 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12696 --ix;
12697 }
12698}
12699
12700static void
12701update_global_location_list_nothrow (int inserting)
12702{
bfd189b1 12703 volatile struct gdb_exception e;
348d480f
PA
12704
12705 TRY_CATCH (e, RETURN_MASK_ERROR)
12706 update_global_location_list (inserting);
12707}
12708
12709/* Clear BKP from a BPS. */
12710
12711static void
12712bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12713{
12714 bpstat bs;
12715
12716 for (bs = bps; bs; bs = bs->next)
12717 if (bs->breakpoint_at == bpt)
12718 {
12719 bs->breakpoint_at = NULL;
12720 bs->old_val = NULL;
12721 /* bs->commands will be freed later. */
12722 }
12723}
12724
12725/* Callback for iterate_over_threads. */
12726static int
12727bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12728{
12729 struct breakpoint *bpt = data;
12730
12731 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12732 return 0;
12733}
12734
12735/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12736 callbacks. */
12737
12738static void
12739say_where (struct breakpoint *b)
12740{
12741 struct value_print_options opts;
12742
12743 get_user_print_options (&opts);
12744
12745 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12746 single string. */
12747 if (b->loc == NULL)
12748 {
12749 printf_filtered (_(" (%s) pending."), b->addr_string);
12750 }
12751 else
12752 {
2f202fde 12753 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12754 {
12755 printf_filtered (" at ");
12756 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12757 gdb_stdout);
12758 }
2f202fde 12759 if (b->loc->symtab != NULL)
f8eba3c6
TT
12760 {
12761 /* If there is a single location, we can print the location
12762 more nicely. */
12763 if (b->loc->next == NULL)
12764 printf_filtered (": file %s, line %d.",
05cba821
JK
12765 symtab_to_filename_for_display (b->loc->symtab),
12766 b->loc->line_number);
f8eba3c6
TT
12767 else
12768 /* This is not ideal, but each location may have a
12769 different file name, and this at least reflects the
12770 real situation somewhat. */
12771 printf_filtered (": %s.", b->addr_string);
12772 }
348d480f
PA
12773
12774 if (b->loc->next)
12775 {
12776 struct bp_location *loc = b->loc;
12777 int n = 0;
12778 for (; loc; loc = loc->next)
12779 ++n;
12780 printf_filtered (" (%d locations)", n);
12781 }
12782 }
12783}
12784
348d480f
PA
12785/* Default bp_location_ops methods. */
12786
12787static void
12788bp_location_dtor (struct bp_location *self)
12789{
12790 xfree (self->cond);
b775012e
LM
12791 if (self->cond_bytecode)
12792 free_agent_expr (self->cond_bytecode);
348d480f 12793 xfree (self->function_name);
8b4f3082
PA
12794
12795 VEC_free (agent_expr_p, self->target_info.conditions);
12796 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12797}
12798
12799static const struct bp_location_ops bp_location_ops =
12800{
12801 bp_location_dtor
12802};
12803
2060206e
PA
12804/* Default breakpoint_ops methods all breakpoint_ops ultimately
12805 inherit from. */
348d480f 12806
2060206e
PA
12807static void
12808base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12809{
12810 decref_counted_command_line (&self->commands);
12811 xfree (self->cond_string);
fb81d016 12812 xfree (self->extra_string);
348d480f 12813 xfree (self->addr_string);
f8eba3c6 12814 xfree (self->filter);
348d480f 12815 xfree (self->addr_string_range_end);
348d480f
PA
12816}
12817
2060206e
PA
12818static struct bp_location *
12819base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12820{
12821 struct bp_location *loc;
12822
12823 loc = XNEW (struct bp_location);
12824 init_bp_location (loc, &bp_location_ops, self);
12825 return loc;
12826}
12827
2060206e
PA
12828static void
12829base_breakpoint_re_set (struct breakpoint *b)
12830{
12831 /* Nothing to re-set. */
12832}
12833
12834#define internal_error_pure_virtual_called() \
12835 gdb_assert_not_reached ("pure virtual function called")
12836
12837static int
12838base_breakpoint_insert_location (struct bp_location *bl)
12839{
12840 internal_error_pure_virtual_called ();
12841}
12842
12843static int
12844base_breakpoint_remove_location (struct bp_location *bl)
12845{
12846 internal_error_pure_virtual_called ();
12847}
12848
12849static int
12850base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12851 struct address_space *aspace,
09ac7c10
TT
12852 CORE_ADDR bp_addr,
12853 const struct target_waitstatus *ws)
2060206e
PA
12854{
12855 internal_error_pure_virtual_called ();
12856}
12857
12858static void
12859base_breakpoint_check_status (bpstat bs)
12860{
12861 /* Always stop. */
12862}
12863
12864/* A "works_in_software_mode" breakpoint_ops method that just internal
12865 errors. */
12866
12867static int
12868base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12869{
12870 internal_error_pure_virtual_called ();
12871}
12872
12873/* A "resources_needed" breakpoint_ops method that just internal
12874 errors. */
12875
12876static int
12877base_breakpoint_resources_needed (const struct bp_location *bl)
12878{
12879 internal_error_pure_virtual_called ();
12880}
12881
12882static enum print_stop_action
12883base_breakpoint_print_it (bpstat bs)
12884{
12885 internal_error_pure_virtual_called ();
12886}
12887
12888static void
12889base_breakpoint_print_one_detail (const struct breakpoint *self,
12890 struct ui_out *uiout)
12891{
12892 /* nothing */
12893}
12894
12895static void
12896base_breakpoint_print_mention (struct breakpoint *b)
12897{
12898 internal_error_pure_virtual_called ();
12899}
12900
12901static void
12902base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12903{
12904 internal_error_pure_virtual_called ();
12905}
12906
983af33b
SDJ
12907static void
12908base_breakpoint_create_sals_from_address (char **arg,
12909 struct linespec_result *canonical,
12910 enum bptype type_wanted,
12911 char *addr_start,
12912 char **copy_arg)
12913{
12914 internal_error_pure_virtual_called ();
12915}
12916
12917static void
12918base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12919 struct linespec_result *c,
983af33b 12920 char *cond_string,
e7e0cddf 12921 char *extra_string,
983af33b
SDJ
12922 enum bptype type_wanted,
12923 enum bpdisp disposition,
12924 int thread,
12925 int task, int ignore_count,
12926 const struct breakpoint_ops *o,
12927 int from_tty, int enabled,
44f238bb 12928 int internal, unsigned flags)
983af33b
SDJ
12929{
12930 internal_error_pure_virtual_called ();
12931}
12932
12933static void
12934base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12935 struct symtabs_and_lines *sals)
12936{
12937 internal_error_pure_virtual_called ();
12938}
12939
ab04a2af
TT
12940/* The default 'explains_signal' method. */
12941
47591c29 12942static int
427cd150 12943base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12944{
47591c29 12945 return 1;
ab04a2af
TT
12946}
12947
9d6e6e84
HZ
12948/* The default "after_condition_true" method. */
12949
12950static void
12951base_breakpoint_after_condition_true (struct bpstats *bs)
12952{
12953 /* Nothing to do. */
12954}
12955
ab04a2af 12956struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
12957{
12958 base_breakpoint_dtor,
12959 base_breakpoint_allocate_location,
12960 base_breakpoint_re_set,
12961 base_breakpoint_insert_location,
12962 base_breakpoint_remove_location,
12963 base_breakpoint_breakpoint_hit,
12964 base_breakpoint_check_status,
12965 base_breakpoint_resources_needed,
12966 base_breakpoint_works_in_software_mode,
12967 base_breakpoint_print_it,
12968 NULL,
12969 base_breakpoint_print_one_detail,
12970 base_breakpoint_print_mention,
983af33b
SDJ
12971 base_breakpoint_print_recreate,
12972 base_breakpoint_create_sals_from_address,
12973 base_breakpoint_create_breakpoints_sal,
12974 base_breakpoint_decode_linespec,
9d6e6e84
HZ
12975 base_breakpoint_explains_signal,
12976 base_breakpoint_after_condition_true,
2060206e
PA
12977};
12978
12979/* Default breakpoint_ops methods. */
12980
12981static void
348d480f
PA
12982bkpt_re_set (struct breakpoint *b)
12983{
06edf0c0
PA
12984 /* FIXME: is this still reachable? */
12985 if (b->addr_string == NULL)
12986 {
12987 /* Anything without a string can't be re-set. */
348d480f 12988 delete_breakpoint (b);
06edf0c0 12989 return;
348d480f 12990 }
06edf0c0
PA
12991
12992 breakpoint_re_set_default (b);
348d480f
PA
12993}
12994
2060206e 12995static int
348d480f
PA
12996bkpt_insert_location (struct bp_location *bl)
12997{
12998 if (bl->loc_type == bp_loc_hardware_breakpoint)
12999 return target_insert_hw_breakpoint (bl->gdbarch,
13000 &bl->target_info);
13001 else
13002 return target_insert_breakpoint (bl->gdbarch,
13003 &bl->target_info);
13004}
13005
2060206e 13006static int
348d480f
PA
13007bkpt_remove_location (struct bp_location *bl)
13008{
13009 if (bl->loc_type == bp_loc_hardware_breakpoint)
13010 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13011 else
13012 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
13013}
13014
2060206e 13015static int
348d480f 13016bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13017 struct address_space *aspace, CORE_ADDR bp_addr,
13018 const struct target_waitstatus *ws)
348d480f 13019{
09ac7c10 13020 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13021 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13022 return 0;
13023
348d480f
PA
13024 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13025 aspace, bp_addr))
13026 return 0;
13027
13028 if (overlay_debugging /* unmapped overlay section */
13029 && section_is_overlay (bl->section)
13030 && !section_is_mapped (bl->section))
13031 return 0;
13032
13033 return 1;
13034}
13035
2060206e 13036static int
348d480f
PA
13037bkpt_resources_needed (const struct bp_location *bl)
13038{
13039 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13040
13041 return 1;
13042}
13043
2060206e 13044static enum print_stop_action
348d480f
PA
13045bkpt_print_it (bpstat bs)
13046{
348d480f
PA
13047 struct breakpoint *b;
13048 const struct bp_location *bl;
001c8c33 13049 int bp_temp;
79a45e25 13050 struct ui_out *uiout = current_uiout;
348d480f
PA
13051
13052 gdb_assert (bs->bp_location_at != NULL);
13053
13054 bl = bs->bp_location_at;
13055 b = bs->breakpoint_at;
13056
001c8c33
PA
13057 bp_temp = b->disposition == disp_del;
13058 if (bl->address != bl->requested_address)
13059 breakpoint_adjustment_warning (bl->requested_address,
13060 bl->address,
13061 b->number, 1);
13062 annotate_breakpoint (b->number);
13063 if (bp_temp)
13064 ui_out_text (uiout, "\nTemporary breakpoint ");
13065 else
13066 ui_out_text (uiout, "\nBreakpoint ");
13067 if (ui_out_is_mi_like_p (uiout))
348d480f 13068 {
001c8c33
PA
13069 ui_out_field_string (uiout, "reason",
13070 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13071 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13072 }
001c8c33
PA
13073 ui_out_field_int (uiout, "bkptno", b->number);
13074 ui_out_text (uiout, ", ");
06edf0c0 13075
001c8c33 13076 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13077}
13078
2060206e 13079static void
06edf0c0
PA
13080bkpt_print_mention (struct breakpoint *b)
13081{
79a45e25 13082 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13083 return;
13084
13085 switch (b->type)
13086 {
13087 case bp_breakpoint:
13088 case bp_gnu_ifunc_resolver:
13089 if (b->disposition == disp_del)
13090 printf_filtered (_("Temporary breakpoint"));
13091 else
13092 printf_filtered (_("Breakpoint"));
13093 printf_filtered (_(" %d"), b->number);
13094 if (b->type == bp_gnu_ifunc_resolver)
13095 printf_filtered (_(" at gnu-indirect-function resolver"));
13096 break;
13097 case bp_hardware_breakpoint:
13098 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13099 break;
e7e0cddf
SS
13100 case bp_dprintf:
13101 printf_filtered (_("Dprintf %d"), b->number);
13102 break;
06edf0c0
PA
13103 }
13104
13105 say_where (b);
13106}
13107
2060206e 13108static void
06edf0c0
PA
13109bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13110{
13111 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13112 fprintf_unfiltered (fp, "tbreak");
13113 else if (tp->type == bp_breakpoint)
13114 fprintf_unfiltered (fp, "break");
13115 else if (tp->type == bp_hardware_breakpoint
13116 && tp->disposition == disp_del)
13117 fprintf_unfiltered (fp, "thbreak");
13118 else if (tp->type == bp_hardware_breakpoint)
13119 fprintf_unfiltered (fp, "hbreak");
13120 else
13121 internal_error (__FILE__, __LINE__,
13122 _("unhandled breakpoint type %d"), (int) tp->type);
13123
2060206e 13124 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13125 print_recreate_thread (tp, fp);
06edf0c0
PA
13126}
13127
983af33b
SDJ
13128static void
13129bkpt_create_sals_from_address (char **arg,
13130 struct linespec_result *canonical,
13131 enum bptype type_wanted,
13132 char *addr_start, char **copy_arg)
13133{
13134 create_sals_from_address_default (arg, canonical, type_wanted,
13135 addr_start, copy_arg);
13136}
13137
13138static void
13139bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13140 struct linespec_result *canonical,
983af33b 13141 char *cond_string,
e7e0cddf 13142 char *extra_string,
983af33b
SDJ
13143 enum bptype type_wanted,
13144 enum bpdisp disposition,
13145 int thread,
13146 int task, int ignore_count,
13147 const struct breakpoint_ops *ops,
13148 int from_tty, int enabled,
44f238bb 13149 int internal, unsigned flags)
983af33b 13150{
023fa29b 13151 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13152 cond_string, extra_string,
13153 type_wanted,
983af33b
SDJ
13154 disposition, thread, task,
13155 ignore_count, ops, from_tty,
44f238bb 13156 enabled, internal, flags);
983af33b
SDJ
13157}
13158
13159static void
13160bkpt_decode_linespec (struct breakpoint *b, char **s,
13161 struct symtabs_and_lines *sals)
13162{
13163 decode_linespec_default (b, s, sals);
13164}
13165
06edf0c0
PA
13166/* Virtual table for internal breakpoints. */
13167
13168static void
13169internal_bkpt_re_set (struct breakpoint *b)
13170{
13171 switch (b->type)
13172 {
13173 /* Delete overlay event and longjmp master breakpoints; they
13174 will be reset later by breakpoint_re_set. */
13175 case bp_overlay_event:
13176 case bp_longjmp_master:
13177 case bp_std_terminate_master:
13178 case bp_exception_master:
13179 delete_breakpoint (b);
13180 break;
13181
13182 /* This breakpoint is special, it's set up when the inferior
13183 starts and we really don't want to touch it. */
13184 case bp_shlib_event:
13185
13186 /* Like bp_shlib_event, this breakpoint type is special. Once
13187 it is set up, we do not want to touch it. */
13188 case bp_thread_event:
13189 break;
13190 }
13191}
13192
13193static void
13194internal_bkpt_check_status (bpstat bs)
13195{
a9b3a50f
PA
13196 if (bs->breakpoint_at->type == bp_shlib_event)
13197 {
13198 /* If requested, stop when the dynamic linker notifies GDB of
13199 events. This allows the user to get control and place
13200 breakpoints in initializer routines for dynamically loaded
13201 objects (among other things). */
13202 bs->stop = stop_on_solib_events;
13203 bs->print = stop_on_solib_events;
13204 }
13205 else
13206 bs->stop = 0;
06edf0c0
PA
13207}
13208
13209static enum print_stop_action
13210internal_bkpt_print_it (bpstat bs)
13211{
06edf0c0 13212 struct breakpoint *b;
06edf0c0 13213
06edf0c0
PA
13214 b = bs->breakpoint_at;
13215
06edf0c0
PA
13216 switch (b->type)
13217 {
348d480f
PA
13218 case bp_shlib_event:
13219 /* Did we stop because the user set the stop_on_solib_events
13220 variable? (If so, we report this as a generic, "Stopped due
13221 to shlib event" message.) */
edcc5120 13222 print_solib_event (0);
348d480f
PA
13223 break;
13224
13225 case bp_thread_event:
13226 /* Not sure how we will get here.
13227 GDB should not stop for these breakpoints. */
13228 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13229 break;
13230
13231 case bp_overlay_event:
13232 /* By analogy with the thread event, GDB should not stop for these. */
13233 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13234 break;
13235
13236 case bp_longjmp_master:
13237 /* These should never be enabled. */
13238 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13239 break;
13240
13241 case bp_std_terminate_master:
13242 /* These should never be enabled. */
13243 printf_filtered (_("std::terminate Master Breakpoint: "
13244 "gdb should not stop!\n"));
348d480f
PA
13245 break;
13246
13247 case bp_exception_master:
13248 /* These should never be enabled. */
13249 printf_filtered (_("Exception Master Breakpoint: "
13250 "gdb should not stop!\n"));
06edf0c0
PA
13251 break;
13252 }
13253
001c8c33 13254 return PRINT_NOTHING;
06edf0c0
PA
13255}
13256
13257static void
13258internal_bkpt_print_mention (struct breakpoint *b)
13259{
13260 /* Nothing to mention. These breakpoints are internal. */
13261}
13262
06edf0c0
PA
13263/* Virtual table for momentary breakpoints */
13264
13265static void
13266momentary_bkpt_re_set (struct breakpoint *b)
13267{
13268 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13269 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13270 Otherwise these should have been blown away via the cleanup chain
13271 or by breakpoint_init_inferior when we rerun the executable. */
13272}
13273
13274static void
13275momentary_bkpt_check_status (bpstat bs)
13276{
13277 /* Nothing. The point of these breakpoints is causing a stop. */
13278}
13279
13280static enum print_stop_action
13281momentary_bkpt_print_it (bpstat bs)
13282{
79a45e25
PA
13283 struct ui_out *uiout = current_uiout;
13284
001c8c33 13285 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13286 {
001c8c33 13287 struct breakpoint *b = bs->breakpoint_at;
348d480f 13288
001c8c33
PA
13289 switch (b->type)
13290 {
13291 case bp_finish:
13292 ui_out_field_string
13293 (uiout, "reason",
13294 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13295 break;
348d480f 13296
001c8c33
PA
13297 case bp_until:
13298 ui_out_field_string
13299 (uiout, "reason",
13300 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13301 break;
13302 }
348d480f
PA
13303 }
13304
001c8c33 13305 return PRINT_UNKNOWN;
348d480f
PA
13306}
13307
06edf0c0
PA
13308static void
13309momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13310{
06edf0c0 13311 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13312}
13313
e2e4d78b
JK
13314/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13315
13316 It gets cleared already on the removal of the first one of such placed
13317 breakpoints. This is OK as they get all removed altogether. */
13318
13319static void
13320longjmp_bkpt_dtor (struct breakpoint *self)
13321{
13322 struct thread_info *tp = find_thread_id (self->thread);
13323
13324 if (tp)
13325 tp->initiating_frame = null_frame_id;
13326
13327 momentary_breakpoint_ops.dtor (self);
13328}
13329
55aa24fb
SDJ
13330/* Specific methods for probe breakpoints. */
13331
13332static int
13333bkpt_probe_insert_location (struct bp_location *bl)
13334{
13335 int v = bkpt_insert_location (bl);
13336
13337 if (v == 0)
13338 {
13339 /* The insertion was successful, now let's set the probe's semaphore
13340 if needed. */
13341 bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
13342 }
13343
13344 return v;
13345}
13346
13347static int
13348bkpt_probe_remove_location (struct bp_location *bl)
13349{
13350 /* Let's clear the semaphore before removing the location. */
13351 bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
13352
13353 return bkpt_remove_location (bl);
13354}
13355
13356static void
13357bkpt_probe_create_sals_from_address (char **arg,
13358 struct linespec_result *canonical,
13359 enum bptype type_wanted,
13360 char *addr_start, char **copy_arg)
13361{
13362 struct linespec_sals lsal;
13363
13364 lsal.sals = parse_probes (arg, canonical);
13365
13366 *copy_arg = xstrdup (canonical->addr_string);
13367 lsal.canonical = xstrdup (*copy_arg);
13368
13369 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13370}
13371
13372static void
13373bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13374 struct symtabs_and_lines *sals)
13375{
13376 *sals = parse_probes (s, NULL);
13377 if (!sals->sals)
13378 error (_("probe not found"));
13379}
13380
348d480f 13381/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13382
348d480f
PA
13383static void
13384tracepoint_re_set (struct breakpoint *b)
13385{
13386 breakpoint_re_set_default (b);
13387}
876fa593 13388
348d480f
PA
13389static int
13390tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13391 struct address_space *aspace, CORE_ADDR bp_addr,
13392 const struct target_waitstatus *ws)
348d480f
PA
13393{
13394 /* By definition, the inferior does not report stops at
13395 tracepoints. */
13396 return 0;
74960c60
VP
13397}
13398
13399static void
348d480f
PA
13400tracepoint_print_one_detail (const struct breakpoint *self,
13401 struct ui_out *uiout)
74960c60 13402{
d9b3f62e
PA
13403 struct tracepoint *tp = (struct tracepoint *) self;
13404 if (tp->static_trace_marker_id)
348d480f
PA
13405 {
13406 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13407
348d480f
PA
13408 ui_out_text (uiout, "\tmarker id is ");
13409 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13410 tp->static_trace_marker_id);
348d480f
PA
13411 ui_out_text (uiout, "\n");
13412 }
0d381245
VP
13413}
13414
a474d7c2 13415static void
348d480f 13416tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13417{
79a45e25 13418 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13419 return;
cc59ec59 13420
348d480f
PA
13421 switch (b->type)
13422 {
13423 case bp_tracepoint:
13424 printf_filtered (_("Tracepoint"));
13425 printf_filtered (_(" %d"), b->number);
13426 break;
13427 case bp_fast_tracepoint:
13428 printf_filtered (_("Fast tracepoint"));
13429 printf_filtered (_(" %d"), b->number);
13430 break;
13431 case bp_static_tracepoint:
13432 printf_filtered (_("Static tracepoint"));
13433 printf_filtered (_(" %d"), b->number);
13434 break;
13435 default:
13436 internal_error (__FILE__, __LINE__,
13437 _("unhandled tracepoint type %d"), (int) b->type);
13438 }
13439
13440 say_where (b);
a474d7c2
PA
13441}
13442
348d480f 13443static void
d9b3f62e 13444tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13445{
d9b3f62e
PA
13446 struct tracepoint *tp = (struct tracepoint *) self;
13447
13448 if (self->type == bp_fast_tracepoint)
348d480f 13449 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13450 if (self->type == bp_static_tracepoint)
348d480f 13451 fprintf_unfiltered (fp, "strace");
d9b3f62e 13452 else if (self->type == bp_tracepoint)
348d480f
PA
13453 fprintf_unfiltered (fp, "trace");
13454 else
13455 internal_error (__FILE__, __LINE__,
d9b3f62e 13456 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13457
d9b3f62e
PA
13458 fprintf_unfiltered (fp, " %s", self->addr_string);
13459 print_recreate_thread (self, fp);
13460
13461 if (tp->pass_count)
13462 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13463}
13464
983af33b
SDJ
13465static void
13466tracepoint_create_sals_from_address (char **arg,
13467 struct linespec_result *canonical,
13468 enum bptype type_wanted,
13469 char *addr_start, char **copy_arg)
13470{
13471 create_sals_from_address_default (arg, canonical, type_wanted,
13472 addr_start, copy_arg);
13473}
13474
13475static void
13476tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13477 struct linespec_result *canonical,
983af33b 13478 char *cond_string,
e7e0cddf 13479 char *extra_string,
983af33b
SDJ
13480 enum bptype type_wanted,
13481 enum bpdisp disposition,
13482 int thread,
13483 int task, int ignore_count,
13484 const struct breakpoint_ops *ops,
13485 int from_tty, int enabled,
44f238bb 13486 int internal, unsigned flags)
983af33b 13487{
023fa29b 13488 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13489 cond_string, extra_string,
13490 type_wanted,
983af33b
SDJ
13491 disposition, thread, task,
13492 ignore_count, ops, from_tty,
44f238bb 13493 enabled, internal, flags);
983af33b
SDJ
13494}
13495
13496static void
13497tracepoint_decode_linespec (struct breakpoint *b, char **s,
13498 struct symtabs_and_lines *sals)
13499{
13500 decode_linespec_default (b, s, sals);
13501}
13502
2060206e 13503struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13504
55aa24fb
SDJ
13505/* The breakpoint_ops structure to be use on tracepoints placed in a
13506 static probe. */
13507
13508static void
13509tracepoint_probe_create_sals_from_address (char **arg,
13510 struct linespec_result *canonical,
13511 enum bptype type_wanted,
13512 char *addr_start, char **copy_arg)
13513{
13514 /* We use the same method for breakpoint on probes. */
13515 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13516 addr_start, copy_arg);
13517}
13518
13519static void
13520tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13521 struct symtabs_and_lines *sals)
13522{
13523 /* We use the same method for breakpoint on probes. */
13524 bkpt_probe_decode_linespec (b, s, sals);
13525}
13526
13527static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13528
5c2b4418
HZ
13529/* Dprintf breakpoint_ops methods. */
13530
13531static void
13532dprintf_re_set (struct breakpoint *b)
13533{
13534 breakpoint_re_set_default (b);
13535
13536 /* This breakpoint could have been pending, and be resolved now, and
13537 if so, we should now have the extra string. If we don't, the
13538 dprintf was malformed when created, but we couldn't tell because
13539 we can't extract the extra string until the location is
13540 resolved. */
13541 if (b->loc != NULL && b->extra_string == NULL)
13542 error (_("Format string required"));
13543
13544 /* 1 - connect to target 1, that can run breakpoint commands.
13545 2 - create a dprintf, which resolves fine.
13546 3 - disconnect from target 1
13547 4 - connect to target 2, that can NOT run breakpoint commands.
13548
13549 After steps #3/#4, you'll want the dprintf command list to
13550 be updated, because target 1 and 2 may well return different
13551 answers for target_can_run_breakpoint_commands().
13552 Given absence of finer grained resetting, we get to do
13553 it all the time. */
13554 if (b->extra_string != NULL)
13555 update_dprintf_command_list (b);
13556}
13557
2d9442cc
HZ
13558/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13559
13560static void
13561dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13562{
13563 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13564 tp->extra_string);
13565 print_recreate_thread (tp, fp);
13566}
13567
9d6e6e84
HZ
13568/* Implement the "after_condition_true" breakpoint_ops method for
13569 dprintf.
13570
13571 dprintf's are implemented with regular commands in their command
13572 list, but we run the commands here instead of before presenting the
13573 stop to the user, as dprintf's don't actually cause a stop. This
13574 also makes it so that the commands of multiple dprintfs at the same
13575 address are all handled. */
13576
13577static void
13578dprintf_after_condition_true (struct bpstats *bs)
13579{
13580 struct cleanup *old_chain;
13581 struct bpstats tmp_bs = { NULL };
13582 struct bpstats *tmp_bs_p = &tmp_bs;
13583
13584 /* dprintf's never cause a stop. This wasn't set in the
13585 check_status hook instead because that would make the dprintf's
13586 condition not be evaluated. */
13587 bs->stop = 0;
13588
13589 /* Run the command list here. Take ownership of it instead of
13590 copying. We never want these commands to run later in
13591 bpstat_do_actions, if a breakpoint that causes a stop happens to
13592 be set at same address as this dprintf, or even if running the
13593 commands here throws. */
13594 tmp_bs.commands = bs->commands;
13595 bs->commands = NULL;
13596 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13597
13598 bpstat_do_actions_1 (&tmp_bs_p);
13599
13600 /* 'tmp_bs.commands' will usually be NULL by now, but
13601 bpstat_do_actions_1 may return early without processing the whole
13602 list. */
13603 do_cleanups (old_chain);
13604}
13605
983af33b
SDJ
13606/* The breakpoint_ops structure to be used on static tracepoints with
13607 markers (`-m'). */
13608
13609static void
13610strace_marker_create_sals_from_address (char **arg,
13611 struct linespec_result *canonical,
13612 enum bptype type_wanted,
13613 char *addr_start, char **copy_arg)
13614{
13615 struct linespec_sals lsal;
13616
13617 lsal.sals = decode_static_tracepoint_spec (arg);
13618
13619 *copy_arg = savestring (addr_start, *arg - addr_start);
13620
13621 canonical->addr_string = xstrdup (*copy_arg);
13622 lsal.canonical = xstrdup (*copy_arg);
13623 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13624}
13625
13626static void
13627strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13628 struct linespec_result *canonical,
983af33b 13629 char *cond_string,
e7e0cddf 13630 char *extra_string,
983af33b
SDJ
13631 enum bptype type_wanted,
13632 enum bpdisp disposition,
13633 int thread,
13634 int task, int ignore_count,
13635 const struct breakpoint_ops *ops,
13636 int from_tty, int enabled,
44f238bb 13637 int internal, unsigned flags)
983af33b
SDJ
13638{
13639 int i;
52d361e1
YQ
13640 struct linespec_sals *lsal = VEC_index (linespec_sals,
13641 canonical->sals, 0);
983af33b
SDJ
13642
13643 /* If the user is creating a static tracepoint by marker id
13644 (strace -m MARKER_ID), then store the sals index, so that
13645 breakpoint_re_set can try to match up which of the newly
13646 found markers corresponds to this one, and, don't try to
13647 expand multiple locations for each sal, given than SALS
13648 already should contain all sals for MARKER_ID. */
13649
13650 for (i = 0; i < lsal->sals.nelts; ++i)
13651 {
13652 struct symtabs_and_lines expanded;
13653 struct tracepoint *tp;
13654 struct cleanup *old_chain;
13655 char *addr_string;
13656
13657 expanded.nelts = 1;
13658 expanded.sals = &lsal->sals.sals[i];
13659
13660 addr_string = xstrdup (canonical->addr_string);
13661 old_chain = make_cleanup (xfree, addr_string);
13662
13663 tp = XCNEW (struct tracepoint);
13664 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13665 addr_string, NULL,
e7e0cddf
SS
13666 cond_string, extra_string,
13667 type_wanted, disposition,
983af33b 13668 thread, task, ignore_count, ops,
44f238bb 13669 from_tty, enabled, internal, flags,
983af33b
SDJ
13670 canonical->special_display);
13671 /* Given that its possible to have multiple markers with
13672 the same string id, if the user is creating a static
13673 tracepoint by marker id ("strace -m MARKER_ID"), then
13674 store the sals index, so that breakpoint_re_set can
13675 try to match up which of the newly found markers
13676 corresponds to this one */
13677 tp->static_trace_marker_id_idx = i;
13678
13679 install_breakpoint (internal, &tp->base, 0);
13680
13681 discard_cleanups (old_chain);
13682 }
13683}
13684
13685static void
13686strace_marker_decode_linespec (struct breakpoint *b, char **s,
13687 struct symtabs_and_lines *sals)
13688{
13689 struct tracepoint *tp = (struct tracepoint *) b;
13690
13691 *sals = decode_static_tracepoint_spec (s);
13692 if (sals->nelts > tp->static_trace_marker_id_idx)
13693 {
13694 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13695 sals->nelts = 1;
13696 }
13697 else
13698 error (_("marker %s not found"), tp->static_trace_marker_id);
13699}
13700
13701static struct breakpoint_ops strace_marker_breakpoint_ops;
13702
13703static int
13704strace_marker_p (struct breakpoint *b)
13705{
13706 return b->ops == &strace_marker_breakpoint_ops;
13707}
13708
53a5351d 13709/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13710 structures. */
c906108c
SS
13711
13712void
fba45db2 13713delete_breakpoint (struct breakpoint *bpt)
c906108c 13714{
52f0bd74 13715 struct breakpoint *b;
c906108c 13716
8a3fe4f8 13717 gdb_assert (bpt != NULL);
c906108c 13718
4a64f543
MS
13719 /* Has this bp already been deleted? This can happen because
13720 multiple lists can hold pointers to bp's. bpstat lists are
13721 especial culprits.
13722
13723 One example of this happening is a watchpoint's scope bp. When
13724 the scope bp triggers, we notice that the watchpoint is out of
13725 scope, and delete it. We also delete its scope bp. But the
13726 scope bp is marked "auto-deleting", and is already on a bpstat.
13727 That bpstat is then checked for auto-deleting bp's, which are
13728 deleted.
13729
13730 A real solution to this problem might involve reference counts in
13731 bp's, and/or giving them pointers back to their referencing
13732 bpstat's, and teaching delete_breakpoint to only free a bp's
13733 storage when no more references were extent. A cheaper bandaid
13734 was chosen. */
c906108c
SS
13735 if (bpt->type == bp_none)
13736 return;
13737
4a64f543
MS
13738 /* At least avoid this stale reference until the reference counting
13739 of breakpoints gets resolved. */
d0fb5eae 13740 if (bpt->related_breakpoint != bpt)
e5a0a904 13741 {
d0fb5eae 13742 struct breakpoint *related;
3a5c3e22 13743 struct watchpoint *w;
d0fb5eae
JK
13744
13745 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13746 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13747 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13748 w = (struct watchpoint *) bpt;
13749 else
13750 w = NULL;
13751 if (w != NULL)
13752 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13753
13754 /* Unlink bpt from the bpt->related_breakpoint ring. */
13755 for (related = bpt; related->related_breakpoint != bpt;
13756 related = related->related_breakpoint);
13757 related->related_breakpoint = bpt->related_breakpoint;
13758 bpt->related_breakpoint = bpt;
e5a0a904
JK
13759 }
13760
a9634178
TJB
13761 /* watch_command_1 creates a watchpoint but only sets its number if
13762 update_watchpoint succeeds in creating its bp_locations. If there's
13763 a problem in that process, we'll be asked to delete the half-created
13764 watchpoint. In that case, don't announce the deletion. */
13765 if (bpt->number)
13766 observer_notify_breakpoint_deleted (bpt);
c906108c 13767
c906108c
SS
13768 if (breakpoint_chain == bpt)
13769 breakpoint_chain = bpt->next;
13770
c906108c
SS
13771 ALL_BREAKPOINTS (b)
13772 if (b->next == bpt)
c5aa993b
JM
13773 {
13774 b->next = bpt->next;
13775 break;
13776 }
c906108c 13777
f431efe5
PA
13778 /* Be sure no bpstat's are pointing at the breakpoint after it's
13779 been freed. */
13780 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13781 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13782 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13783 commands are associated with the bpstat; if we remove it here,
13784 then the later call to bpstat_do_actions (&stop_bpstat); in
13785 event-top.c won't do anything, and temporary breakpoints with
13786 commands won't work. */
13787
13788 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13789
4a64f543
MS
13790 /* Now that breakpoint is removed from breakpoint list, update the
13791 global location list. This will remove locations that used to
13792 belong to this breakpoint. Do this before freeing the breakpoint
13793 itself, since remove_breakpoint looks at location's owner. It
13794 might be better design to have location completely
13795 self-contained, but it's not the case now. */
b60e7edf 13796 update_global_location_list (0);
74960c60 13797
348d480f 13798 bpt->ops->dtor (bpt);
4a64f543
MS
13799 /* On the chance that someone will soon try again to delete this
13800 same bp, we mark it as deleted before freeing its storage. */
c906108c 13801 bpt->type = bp_none;
b8c9b27d 13802 xfree (bpt);
c906108c
SS
13803}
13804
4d6140d9
AC
13805static void
13806do_delete_breakpoint_cleanup (void *b)
13807{
13808 delete_breakpoint (b);
13809}
13810
13811struct cleanup *
13812make_cleanup_delete_breakpoint (struct breakpoint *b)
13813{
13814 return make_cleanup (do_delete_breakpoint_cleanup, b);
13815}
13816
51be5b68
PA
13817/* Iterator function to call a user-provided callback function once
13818 for each of B and its related breakpoints. */
13819
13820static void
13821iterate_over_related_breakpoints (struct breakpoint *b,
13822 void (*function) (struct breakpoint *,
13823 void *),
13824 void *data)
13825{
13826 struct breakpoint *related;
13827
13828 related = b;
13829 do
13830 {
13831 struct breakpoint *next;
13832
13833 /* FUNCTION may delete RELATED. */
13834 next = related->related_breakpoint;
13835
13836 if (next == related)
13837 {
13838 /* RELATED is the last ring entry. */
13839 function (related, data);
13840
13841 /* FUNCTION may have deleted it, so we'd never reach back to
13842 B. There's nothing left to do anyway, so just break
13843 out. */
13844 break;
13845 }
13846 else
13847 function (related, data);
13848
13849 related = next;
13850 }
13851 while (related != b);
13852}
95a42b64
TT
13853
13854static void
13855do_delete_breakpoint (struct breakpoint *b, void *ignore)
13856{
13857 delete_breakpoint (b);
13858}
13859
51be5b68
PA
13860/* A callback for map_breakpoint_numbers that calls
13861 delete_breakpoint. */
13862
13863static void
13864do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13865{
13866 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13867}
13868
c906108c 13869void
fba45db2 13870delete_command (char *arg, int from_tty)
c906108c 13871{
35df4500 13872 struct breakpoint *b, *b_tmp;
c906108c 13873
ea9365bb
TT
13874 dont_repeat ();
13875
c906108c
SS
13876 if (arg == 0)
13877 {
13878 int breaks_to_delete = 0;
13879
46c6471b
PA
13880 /* Delete all breakpoints if no argument. Do not delete
13881 internal breakpoints, these have to be deleted with an
13882 explicit breakpoint number argument. */
c5aa993b 13883 ALL_BREAKPOINTS (b)
46c6471b 13884 if (user_breakpoint_p (b))
973d738b
DJ
13885 {
13886 breaks_to_delete = 1;
13887 break;
13888 }
c906108c
SS
13889
13890 /* Ask user only if there are some breakpoints to delete. */
13891 if (!from_tty
e2e0b3e5 13892 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13893 {
35df4500 13894 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13895 if (user_breakpoint_p (b))
c5aa993b 13896 delete_breakpoint (b);
c906108c
SS
13897 }
13898 }
13899 else
51be5b68 13900 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13901}
13902
0d381245
VP
13903static int
13904all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13905{
0d381245 13906 for (; loc; loc = loc->next)
8645ff69
UW
13907 if (!loc->shlib_disabled
13908 && !loc->pspace->executing_startup)
0d381245
VP
13909 return 0;
13910 return 1;
fe3f5fa8
VP
13911}
13912
776592bf
DE
13913/* Subroutine of update_breakpoint_locations to simplify it.
13914 Return non-zero if multiple fns in list LOC have the same name.
13915 Null names are ignored. */
13916
13917static int
13918ambiguous_names_p (struct bp_location *loc)
13919{
13920 struct bp_location *l;
13921 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13922 (int (*) (const void *,
13923 const void *)) streq,
776592bf
DE
13924 NULL, xcalloc, xfree);
13925
13926 for (l = loc; l != NULL; l = l->next)
13927 {
13928 const char **slot;
13929 const char *name = l->function_name;
13930
13931 /* Allow for some names to be NULL, ignore them. */
13932 if (name == NULL)
13933 continue;
13934
13935 slot = (const char **) htab_find_slot (htab, (const void *) name,
13936 INSERT);
4a64f543
MS
13937 /* NOTE: We can assume slot != NULL here because xcalloc never
13938 returns NULL. */
776592bf
DE
13939 if (*slot != NULL)
13940 {
13941 htab_delete (htab);
13942 return 1;
13943 }
13944 *slot = name;
13945 }
13946
13947 htab_delete (htab);
13948 return 0;
13949}
13950
0fb4aa4b
PA
13951/* When symbols change, it probably means the sources changed as well,
13952 and it might mean the static tracepoint markers are no longer at
13953 the same address or line numbers they used to be at last we
13954 checked. Losing your static tracepoints whenever you rebuild is
13955 undesirable. This function tries to resync/rematch gdb static
13956 tracepoints with the markers on the target, for static tracepoints
13957 that have not been set by marker id. Static tracepoint that have
13958 been set by marker id are reset by marker id in breakpoint_re_set.
13959 The heuristic is:
13960
13961 1) For a tracepoint set at a specific address, look for a marker at
13962 the old PC. If one is found there, assume to be the same marker.
13963 If the name / string id of the marker found is different from the
13964 previous known name, assume that means the user renamed the marker
13965 in the sources, and output a warning.
13966
13967 2) For a tracepoint set at a given line number, look for a marker
13968 at the new address of the old line number. If one is found there,
13969 assume to be the same marker. If the name / string id of the
13970 marker found is different from the previous known name, assume that
13971 means the user renamed the marker in the sources, and output a
13972 warning.
13973
13974 3) If a marker is no longer found at the same address or line, it
13975 may mean the marker no longer exists. But it may also just mean
13976 the code changed a bit. Maybe the user added a few lines of code
13977 that made the marker move up or down (in line number terms). Ask
13978 the target for info about the marker with the string id as we knew
13979 it. If found, update line number and address in the matching
13980 static tracepoint. This will get confused if there's more than one
13981 marker with the same ID (possible in UST, although unadvised
13982 precisely because it confuses tools). */
13983
13984static struct symtab_and_line
13985update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13986{
d9b3f62e 13987 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13988 struct static_tracepoint_marker marker;
13989 CORE_ADDR pc;
0fb4aa4b
PA
13990
13991 pc = sal.pc;
13992 if (sal.line)
13993 find_line_pc (sal.symtab, sal.line, &pc);
13994
13995 if (target_static_tracepoint_marker_at (pc, &marker))
13996 {
d9b3f62e 13997 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13998 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13999 b->number,
d9b3f62e 14000 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14001
d9b3f62e
PA
14002 xfree (tp->static_trace_marker_id);
14003 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14004 release_static_tracepoint_marker (&marker);
14005
14006 return sal;
14007 }
14008
14009 /* Old marker wasn't found on target at lineno. Try looking it up
14010 by string ID. */
14011 if (!sal.explicit_pc
14012 && sal.line != 0
14013 && sal.symtab != NULL
d9b3f62e 14014 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14015 {
14016 VEC(static_tracepoint_marker_p) *markers;
14017
14018 markers
d9b3f62e 14019 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14020
14021 if (!VEC_empty(static_tracepoint_marker_p, markers))
14022 {
80e1d417 14023 struct symtab_and_line sal2;
0fb4aa4b 14024 struct symbol *sym;
80e1d417 14025 struct static_tracepoint_marker *tpmarker;
79a45e25 14026 struct ui_out *uiout = current_uiout;
0fb4aa4b 14027
80e1d417 14028 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14029
d9b3f62e 14030 xfree (tp->static_trace_marker_id);
80e1d417 14031 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14032
14033 warning (_("marker for static tracepoint %d (%s) not "
14034 "found at previous line number"),
d9b3f62e 14035 b->number, tp->static_trace_marker_id);
0fb4aa4b 14036
80e1d417 14037 init_sal (&sal2);
0fb4aa4b 14038
80e1d417 14039 sal2.pc = tpmarker->address;
0fb4aa4b 14040
80e1d417
AS
14041 sal2 = find_pc_line (tpmarker->address, 0);
14042 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14043 ui_out_text (uiout, "Now in ");
14044 if (sym)
14045 {
14046 ui_out_field_string (uiout, "func",
14047 SYMBOL_PRINT_NAME (sym));
14048 ui_out_text (uiout, " at ");
14049 }
05cba821
JK
14050 ui_out_field_string (uiout, "file",
14051 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14052 ui_out_text (uiout, ":");
14053
14054 if (ui_out_is_mi_like_p (uiout))
14055 {
0b0865da 14056 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14057
f35a17b5 14058 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14059 }
14060
80e1d417 14061 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14062 ui_out_text (uiout, "\n");
14063
80e1d417 14064 b->loc->line_number = sal2.line;
2f202fde 14065 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14066
14067 xfree (b->addr_string);
14068 b->addr_string = xstrprintf ("%s:%d",
05cba821 14069 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14070 b->loc->line_number);
0fb4aa4b
PA
14071
14072 /* Might be nice to check if function changed, and warn if
14073 so. */
14074
80e1d417 14075 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14076 }
14077 }
14078 return sal;
14079}
14080
8d3788bd
VP
14081/* Returns 1 iff locations A and B are sufficiently same that
14082 we don't need to report breakpoint as changed. */
14083
14084static int
14085locations_are_equal (struct bp_location *a, struct bp_location *b)
14086{
14087 while (a && b)
14088 {
14089 if (a->address != b->address)
14090 return 0;
14091
14092 if (a->shlib_disabled != b->shlib_disabled)
14093 return 0;
14094
14095 if (a->enabled != b->enabled)
14096 return 0;
14097
14098 a = a->next;
14099 b = b->next;
14100 }
14101
14102 if ((a == NULL) != (b == NULL))
14103 return 0;
14104
14105 return 1;
14106}
14107
f1310107
TJB
14108/* Create new breakpoint locations for B (a hardware or software breakpoint)
14109 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14110 a ranged breakpoint. */
14111
0e30163f 14112void
0d381245 14113update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14114 struct symtabs_and_lines sals,
14115 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14116{
14117 int i;
0d381245
VP
14118 struct bp_location *existing_locations = b->loc;
14119
f8eba3c6
TT
14120 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14121 {
14122 /* Ranged breakpoints have only one start location and one end
14123 location. */
14124 b->enable_state = bp_disabled;
14125 update_global_location_list (1);
14126 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14127 "multiple locations found\n"),
14128 b->number);
14129 return;
14130 }
f1310107 14131
4a64f543
MS
14132 /* If there's no new locations, and all existing locations are
14133 pending, don't do anything. This optimizes the common case where
14134 all locations are in the same shared library, that was unloaded.
14135 We'd like to retain the location, so that when the library is
14136 loaded again, we don't loose the enabled/disabled status of the
14137 individual locations. */
0d381245 14138 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14139 return;
14140
fe3f5fa8
VP
14141 b->loc = NULL;
14142
0d381245 14143 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14144 {
f8eba3c6
TT
14145 struct bp_location *new_loc;
14146
14147 switch_to_program_space_and_thread (sals.sals[i].pspace);
14148
14149 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14150
0d381245
VP
14151 /* Reparse conditions, they might contain references to the
14152 old symtab. */
14153 if (b->cond_string != NULL)
14154 {
bbc13ae3 14155 const char *s;
bfd189b1 14156 volatile struct gdb_exception e;
fe3f5fa8 14157
0d381245
VP
14158 s = b->cond_string;
14159 TRY_CATCH (e, RETURN_MASK_ERROR)
14160 {
1bb9788d
TT
14161 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14162 block_for_pc (sals.sals[i].pc),
0d381245
VP
14163 0);
14164 }
14165 if (e.reason < 0)
14166 {
3e43a32a
MS
14167 warning (_("failed to reevaluate condition "
14168 "for breakpoint %d: %s"),
0d381245
VP
14169 b->number, e.message);
14170 new_loc->enabled = 0;
14171 }
14172 }
fe3f5fa8 14173
f1310107
TJB
14174 if (sals_end.nelts)
14175 {
14176 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14177
14178 new_loc->length = end - sals.sals[0].pc + 1;
14179 }
0d381245 14180 }
fe3f5fa8 14181
514f746b
AR
14182 /* Update locations of permanent breakpoints. */
14183 if (b->enable_state == bp_permanent)
14184 make_breakpoint_permanent (b);
14185
4a64f543
MS
14186 /* If possible, carry over 'disable' status from existing
14187 breakpoints. */
0d381245
VP
14188 {
14189 struct bp_location *e = existing_locations;
776592bf
DE
14190 /* If there are multiple breakpoints with the same function name,
14191 e.g. for inline functions, comparing function names won't work.
14192 Instead compare pc addresses; this is just a heuristic as things
14193 may have moved, but in practice it gives the correct answer
14194 often enough until a better solution is found. */
14195 int have_ambiguous_names = ambiguous_names_p (b->loc);
14196
0d381245
VP
14197 for (; e; e = e->next)
14198 {
14199 if (!e->enabled && e->function_name)
14200 {
14201 struct bp_location *l = b->loc;
776592bf
DE
14202 if (have_ambiguous_names)
14203 {
14204 for (; l; l = l->next)
f1310107 14205 if (breakpoint_locations_match (e, l))
776592bf
DE
14206 {
14207 l->enabled = 0;
14208 break;
14209 }
14210 }
14211 else
14212 {
14213 for (; l; l = l->next)
14214 if (l->function_name
14215 && strcmp (e->function_name, l->function_name) == 0)
14216 {
14217 l->enabled = 0;
14218 break;
14219 }
14220 }
0d381245
VP
14221 }
14222 }
14223 }
fe3f5fa8 14224
8d3788bd
VP
14225 if (!locations_are_equal (existing_locations, b->loc))
14226 observer_notify_breakpoint_modified (b);
14227
b60e7edf 14228 update_global_location_list (1);
fe3f5fa8
VP
14229}
14230
ef23e705
TJB
14231/* Find the SaL locations corresponding to the given ADDR_STRING.
14232 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14233
14234static struct symtabs_and_lines
14235addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14236{
14237 char *s;
02d20e4a 14238 struct symtabs_and_lines sals = {0};
f8eba3c6 14239 volatile struct gdb_exception e;
ef23e705 14240
983af33b 14241 gdb_assert (b->ops != NULL);
ef23e705 14242 s = addr_string;
ef23e705
TJB
14243
14244 TRY_CATCH (e, RETURN_MASK_ERROR)
14245 {
983af33b 14246 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14247 }
14248 if (e.reason < 0)
14249 {
14250 int not_found_and_ok = 0;
14251 /* For pending breakpoints, it's expected that parsing will
14252 fail until the right shared library is loaded. User has
14253 already told to create pending breakpoints and don't need
14254 extra messages. If breakpoint is in bp_shlib_disabled
14255 state, then user already saw the message about that
14256 breakpoint being disabled, and don't want to see more
14257 errors. */
58438ac1 14258 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14259 && (b->condition_not_parsed
14260 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14261 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14262 || b->enable_state == bp_disabled))
14263 not_found_and_ok = 1;
14264
14265 if (!not_found_and_ok)
14266 {
14267 /* We surely don't want to warn about the same breakpoint
14268 10 times. One solution, implemented here, is disable
14269 the breakpoint on error. Another solution would be to
14270 have separate 'warning emitted' flag. Since this
14271 happens only when a binary has changed, I don't know
14272 which approach is better. */
14273 b->enable_state = bp_disabled;
14274 throw_exception (e);
14275 }
14276 }
14277
58438ac1 14278 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14279 {
f8eba3c6 14280 int i;
ef23e705 14281
f8eba3c6
TT
14282 for (i = 0; i < sals.nelts; ++i)
14283 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14284 if (b->condition_not_parsed && s && s[0])
14285 {
ed1d1739
KS
14286 char *cond_string, *extra_string;
14287 int thread, task;
ef23e705
TJB
14288
14289 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14290 &cond_string, &thread, &task,
14291 &extra_string);
ef23e705
TJB
14292 if (cond_string)
14293 b->cond_string = cond_string;
14294 b->thread = thread;
14295 b->task = task;
e7e0cddf
SS
14296 if (extra_string)
14297 b->extra_string = extra_string;
ef23e705
TJB
14298 b->condition_not_parsed = 0;
14299 }
14300
983af33b 14301 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14302 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14303
58438ac1
TT
14304 *found = 1;
14305 }
14306 else
14307 *found = 0;
ef23e705
TJB
14308
14309 return sals;
14310}
14311
348d480f
PA
14312/* The default re_set method, for typical hardware or software
14313 breakpoints. Reevaluate the breakpoint and recreate its
14314 locations. */
14315
14316static void
28010a5d 14317breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14318{
14319 int found;
f1310107 14320 struct symtabs_and_lines sals, sals_end;
ef23e705 14321 struct symtabs_and_lines expanded = {0};
f1310107 14322 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14323
14324 sals = addr_string_to_sals (b, b->addr_string, &found);
14325 if (found)
14326 {
14327 make_cleanup (xfree, sals.sals);
f8eba3c6 14328 expanded = sals;
ef23e705
TJB
14329 }
14330
f1310107
TJB
14331 if (b->addr_string_range_end)
14332 {
14333 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14334 if (found)
14335 {
14336 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14337 expanded_end = sals_end;
f1310107
TJB
14338 }
14339 }
14340
14341 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14342}
14343
983af33b
SDJ
14344/* Default method for creating SALs from an address string. It basically
14345 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14346
14347static void
14348create_sals_from_address_default (char **arg,
14349 struct linespec_result *canonical,
14350 enum bptype type_wanted,
14351 char *addr_start, char **copy_arg)
14352{
14353 parse_breakpoint_sals (arg, canonical);
14354}
14355
14356/* Call create_breakpoints_sal for the given arguments. This is the default
14357 function for the `create_breakpoints_sal' method of
14358 breakpoint_ops. */
14359
14360static void
14361create_breakpoints_sal_default (struct gdbarch *gdbarch,
14362 struct linespec_result *canonical,
983af33b 14363 char *cond_string,
e7e0cddf 14364 char *extra_string,
983af33b
SDJ
14365 enum bptype type_wanted,
14366 enum bpdisp disposition,
14367 int thread,
14368 int task, int ignore_count,
14369 const struct breakpoint_ops *ops,
14370 int from_tty, int enabled,
44f238bb 14371 int internal, unsigned flags)
983af33b
SDJ
14372{
14373 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14374 extra_string,
983af33b
SDJ
14375 type_wanted, disposition,
14376 thread, task, ignore_count, ops, from_tty,
44f238bb 14377 enabled, internal, flags);
983af33b
SDJ
14378}
14379
14380/* Decode the line represented by S by calling decode_line_full. This is the
14381 default function for the `decode_linespec' method of breakpoint_ops. */
14382
14383static void
14384decode_linespec_default (struct breakpoint *b, char **s,
14385 struct symtabs_and_lines *sals)
14386{
14387 struct linespec_result canonical;
14388
14389 init_linespec_result (&canonical);
14390 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14391 (struct symtab *) NULL, 0,
14392 &canonical, multiple_symbols_all,
14393 b->filter);
14394
14395 /* We should get 0 or 1 resulting SALs. */
14396 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14397
14398 if (VEC_length (linespec_sals, canonical.sals) > 0)
14399 {
14400 struct linespec_sals *lsal;
14401
14402 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14403 *sals = lsal->sals;
14404 /* Arrange it so the destructor does not free the
14405 contents. */
14406 lsal->sals.sals = NULL;
14407 }
14408
14409 destroy_linespec_result (&canonical);
14410}
14411
28010a5d
PA
14412/* Prepare the global context for a re-set of breakpoint B. */
14413
14414static struct cleanup *
14415prepare_re_set_context (struct breakpoint *b)
14416{
14417 struct cleanup *cleanups;
14418
14419 input_radix = b->input_radix;
14420 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14421 if (b->pspace != NULL)
14422 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14423 set_language (b->language);
14424
14425 return cleanups;
ef23e705
TJB
14426}
14427
c906108c
SS
14428/* Reset a breakpoint given it's struct breakpoint * BINT.
14429 The value we return ends up being the return value from catch_errors.
14430 Unused in this case. */
14431
14432static int
4efb68b1 14433breakpoint_re_set_one (void *bint)
c906108c 14434{
4a64f543 14435 /* Get past catch_errs. */
53a5351d 14436 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14437 struct cleanup *cleanups;
c906108c 14438
348d480f
PA
14439 cleanups = prepare_re_set_context (b);
14440 b->ops->re_set (b);
14441 do_cleanups (cleanups);
c906108c
SS
14442 return 0;
14443}
14444
69de3c6a 14445/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14446void
69de3c6a 14447breakpoint_re_set (void)
c906108c 14448{
35df4500 14449 struct breakpoint *b, *b_tmp;
c906108c
SS
14450 enum language save_language;
14451 int save_input_radix;
6c95b8df 14452 struct cleanup *old_chain;
c5aa993b 14453
c906108c
SS
14454 save_language = current_language->la_language;
14455 save_input_radix = input_radix;
6c95b8df
PA
14456 old_chain = save_current_program_space ();
14457
35df4500 14458 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14459 {
4a64f543 14460 /* Format possible error msg. */
fe3f5fa8 14461 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14462 b->number);
14463 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14464 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14465 do_cleanups (cleanups);
c5aa993b 14466 }
c906108c
SS
14467 set_language (save_language);
14468 input_radix = save_input_radix;
e62c965a 14469
0756c555 14470 jit_breakpoint_re_set ();
4efc6507 14471
6c95b8df
PA
14472 do_cleanups (old_chain);
14473
af02033e
PP
14474 create_overlay_event_breakpoint ();
14475 create_longjmp_master_breakpoint ();
14476 create_std_terminate_master_breakpoint ();
186c406b 14477 create_exception_master_breakpoint ();
c906108c
SS
14478}
14479\f
c906108c
SS
14480/* Reset the thread number of this breakpoint:
14481
14482 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14483 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14484void
fba45db2 14485breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14486{
14487 if (b->thread != -1)
14488 {
39f77062
KB
14489 if (in_thread_list (inferior_ptid))
14490 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14491
14492 /* We're being called after following a fork. The new fork is
14493 selected as current, and unless this was a vfork will have a
14494 different program space from the original thread. Reset that
14495 as well. */
14496 b->loc->pspace = current_program_space;
c906108c
SS
14497 }
14498}
14499
03ac34d5
MS
14500/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14501 If from_tty is nonzero, it prints a message to that effect,
14502 which ends with a period (no newline). */
14503
c906108c 14504void
fba45db2 14505set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14506{
52f0bd74 14507 struct breakpoint *b;
c906108c
SS
14508
14509 if (count < 0)
14510 count = 0;
14511
14512 ALL_BREAKPOINTS (b)
14513 if (b->number == bptnum)
c5aa993b 14514 {
d77f58be
SS
14515 if (is_tracepoint (b))
14516 {
14517 if (from_tty && count != 0)
14518 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14519 bptnum);
14520 return;
14521 }
14522
c5aa993b 14523 b->ignore_count = count;
221ea385
KS
14524 if (from_tty)
14525 {
14526 if (count == 0)
3e43a32a
MS
14527 printf_filtered (_("Will stop next time "
14528 "breakpoint %d is reached."),
221ea385
KS
14529 bptnum);
14530 else if (count == 1)
a3f17187 14531 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14532 bptnum);
14533 else
3e43a32a
MS
14534 printf_filtered (_("Will ignore next %d "
14535 "crossings of breakpoint %d."),
221ea385
KS
14536 count, bptnum);
14537 }
8d3788bd 14538 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14539 return;
14540 }
c906108c 14541
8a3fe4f8 14542 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14543}
14544
c906108c
SS
14545/* Command to set ignore-count of breakpoint N to COUNT. */
14546
14547static void
fba45db2 14548ignore_command (char *args, int from_tty)
c906108c
SS
14549{
14550 char *p = args;
52f0bd74 14551 int num;
c906108c
SS
14552
14553 if (p == 0)
e2e0b3e5 14554 error_no_arg (_("a breakpoint number"));
c5aa993b 14555
c906108c 14556 num = get_number (&p);
5c44784c 14557 if (num == 0)
8a3fe4f8 14558 error (_("bad breakpoint number: '%s'"), args);
c906108c 14559 if (*p == 0)
8a3fe4f8 14560 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14561
14562 set_ignore_count (num,
14563 longest_to_int (value_as_long (parse_and_eval (p))),
14564 from_tty);
221ea385
KS
14565 if (from_tty)
14566 printf_filtered ("\n");
c906108c
SS
14567}
14568\f
14569/* Call FUNCTION on each of the breakpoints
14570 whose numbers are given in ARGS. */
14571
14572static void
95a42b64
TT
14573map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14574 void *),
14575 void *data)
c906108c 14576{
52f0bd74
AC
14577 int num;
14578 struct breakpoint *b, *tmp;
11cf8741 14579 int match;
197f0a60 14580 struct get_number_or_range_state state;
c906108c 14581
197f0a60 14582 if (args == 0)
e2e0b3e5 14583 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14584
197f0a60
TT
14585 init_number_or_range (&state, args);
14586
14587 while (!state.finished)
c906108c 14588 {
197f0a60
TT
14589 char *p = state.string;
14590
11cf8741 14591 match = 0;
c5aa993b 14592
197f0a60 14593 num = get_number_or_range (&state);
5c44784c 14594 if (num == 0)
c5aa993b 14595 {
8a3fe4f8 14596 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14597 }
14598 else
14599 {
14600 ALL_BREAKPOINTS_SAFE (b, tmp)
14601 if (b->number == num)
14602 {
11cf8741 14603 match = 1;
cdac0397 14604 function (b, data);
11cf8741 14605 break;
5c44784c 14606 }
11cf8741 14607 if (match == 0)
a3f17187 14608 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14609 }
c906108c
SS
14610 }
14611}
14612
0d381245
VP
14613static struct bp_location *
14614find_location_by_number (char *number)
14615{
14616 char *dot = strchr (number, '.');
14617 char *p1;
14618 int bp_num;
14619 int loc_num;
14620 struct breakpoint *b;
14621 struct bp_location *loc;
14622
14623 *dot = '\0';
14624
14625 p1 = number;
197f0a60 14626 bp_num = get_number (&p1);
0d381245
VP
14627 if (bp_num == 0)
14628 error (_("Bad breakpoint number '%s'"), number);
14629
14630 ALL_BREAKPOINTS (b)
14631 if (b->number == bp_num)
14632 {
14633 break;
14634 }
14635
14636 if (!b || b->number != bp_num)
14637 error (_("Bad breakpoint number '%s'"), number);
14638
14639 p1 = dot+1;
197f0a60 14640 loc_num = get_number (&p1);
0d381245
VP
14641 if (loc_num == 0)
14642 error (_("Bad breakpoint location number '%s'"), number);
14643
14644 --loc_num;
14645 loc = b->loc;
14646 for (;loc_num && loc; --loc_num, loc = loc->next)
14647 ;
14648 if (!loc)
14649 error (_("Bad breakpoint location number '%s'"), dot+1);
14650
14651 return loc;
14652}
14653
14654
1900040c
MS
14655/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14656 If from_tty is nonzero, it prints a message to that effect,
14657 which ends with a period (no newline). */
14658
c906108c 14659void
fba45db2 14660disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14661{
14662 /* Never disable a watchpoint scope breakpoint; we want to
14663 hit them when we leave scope so we can delete both the
14664 watchpoint and its scope breakpoint at that time. */
14665 if (bpt->type == bp_watchpoint_scope)
14666 return;
14667
c2c6d25f 14668 /* You can't disable permanent breakpoints. */
b5de0fa7 14669 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14670 return;
14671
b5de0fa7 14672 bpt->enable_state = bp_disabled;
c906108c 14673
b775012e
LM
14674 /* Mark breakpoint locations modified. */
14675 mark_breakpoint_modified (bpt);
14676
d248b706
KY
14677 if (target_supports_enable_disable_tracepoint ()
14678 && current_trace_status ()->running && is_tracepoint (bpt))
14679 {
14680 struct bp_location *location;
14681
14682 for (location = bpt->loc; location; location = location->next)
14683 target_disable_tracepoint (location);
14684 }
14685
b60e7edf 14686 update_global_location_list (0);
c906108c 14687
8d3788bd 14688 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14689}
14690
51be5b68
PA
14691/* A callback for iterate_over_related_breakpoints. */
14692
14693static void
14694do_disable_breakpoint (struct breakpoint *b, void *ignore)
14695{
14696 disable_breakpoint (b);
14697}
14698
95a42b64
TT
14699/* A callback for map_breakpoint_numbers that calls
14700 disable_breakpoint. */
14701
14702static void
14703do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14704{
51be5b68 14705 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14706}
14707
c906108c 14708static void
fba45db2 14709disable_command (char *args, int from_tty)
c906108c 14710{
c906108c 14711 if (args == 0)
46c6471b
PA
14712 {
14713 struct breakpoint *bpt;
14714
14715 ALL_BREAKPOINTS (bpt)
14716 if (user_breakpoint_p (bpt))
14717 disable_breakpoint (bpt);
14718 }
9eaabc75 14719 else
0d381245 14720 {
9eaabc75
MW
14721 char *num = extract_arg (&args);
14722
14723 while (num)
d248b706 14724 {
9eaabc75 14725 if (strchr (num, '.'))
b775012e 14726 {
9eaabc75
MW
14727 struct bp_location *loc = find_location_by_number (num);
14728
14729 if (loc)
14730 {
14731 if (loc->enabled)
14732 {
14733 loc->enabled = 0;
14734 mark_breakpoint_location_modified (loc);
14735 }
14736 if (target_supports_enable_disable_tracepoint ()
14737 && current_trace_status ()->running && loc->owner
14738 && is_tracepoint (loc->owner))
14739 target_disable_tracepoint (loc);
14740 }
14741 update_global_location_list (0);
b775012e 14742 }
9eaabc75
MW
14743 else
14744 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14745 num = extract_arg (&args);
d248b706 14746 }
0d381245 14747 }
c906108c
SS
14748}
14749
14750static void
816338b5
SS
14751enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14752 int count)
c906108c 14753{
afe38095 14754 int target_resources_ok;
c906108c
SS
14755
14756 if (bpt->type == bp_hardware_breakpoint)
14757 {
14758 int i;
c5aa993b 14759 i = hw_breakpoint_used_count ();
53a5351d 14760 target_resources_ok =
d92524f1 14761 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14762 i + 1, 0);
c906108c 14763 if (target_resources_ok == 0)
8a3fe4f8 14764 error (_("No hardware breakpoint support in the target."));
c906108c 14765 else if (target_resources_ok < 0)
8a3fe4f8 14766 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14767 }
14768
cc60f2e3 14769 if (is_watchpoint (bpt))
c906108c 14770 {
d07205c2
JK
14771 /* Initialize it just to avoid a GCC false warning. */
14772 enum enable_state orig_enable_state = 0;
bfd189b1 14773 volatile struct gdb_exception e;
dde02812
ES
14774
14775 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 14776 {
3a5c3e22
PA
14777 struct watchpoint *w = (struct watchpoint *) bpt;
14778
1e718ff1
TJB
14779 orig_enable_state = bpt->enable_state;
14780 bpt->enable_state = bp_enabled;
3a5c3e22 14781 update_watchpoint (w, 1 /* reparse */);
c906108c 14782 }
dde02812 14783 if (e.reason < 0)
c5aa993b 14784 {
1e718ff1 14785 bpt->enable_state = orig_enable_state;
dde02812
ES
14786 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14787 bpt->number);
14788 return;
c5aa993b 14789 }
c906108c 14790 }
0101ce28 14791
b4c291bb
KH
14792 if (bpt->enable_state != bp_permanent)
14793 bpt->enable_state = bp_enabled;
d248b706 14794
b775012e
LM
14795 bpt->enable_state = bp_enabled;
14796
14797 /* Mark breakpoint locations modified. */
14798 mark_breakpoint_modified (bpt);
14799
d248b706
KY
14800 if (target_supports_enable_disable_tracepoint ()
14801 && current_trace_status ()->running && is_tracepoint (bpt))
14802 {
14803 struct bp_location *location;
14804
14805 for (location = bpt->loc; location; location = location->next)
14806 target_enable_tracepoint (location);
14807 }
14808
b4c291bb 14809 bpt->disposition = disposition;
816338b5 14810 bpt->enable_count = count;
b60e7edf 14811 update_global_location_list (1);
9c97429f 14812
8d3788bd 14813 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14814}
14815
fe3f5fa8 14816
c906108c 14817void
fba45db2 14818enable_breakpoint (struct breakpoint *bpt)
c906108c 14819{
816338b5 14820 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14821}
14822
14823static void
14824do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14825{
14826 enable_breakpoint (bpt);
c906108c
SS
14827}
14828
95a42b64
TT
14829/* A callback for map_breakpoint_numbers that calls
14830 enable_breakpoint. */
14831
14832static void
14833do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14834{
51be5b68 14835 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14836}
14837
c906108c
SS
14838/* The enable command enables the specified breakpoints (or all defined
14839 breakpoints) so they once again become (or continue to be) effective
1272ad14 14840 in stopping the inferior. */
c906108c 14841
c906108c 14842static void
fba45db2 14843enable_command (char *args, int from_tty)
c906108c 14844{
c906108c 14845 if (args == 0)
46c6471b
PA
14846 {
14847 struct breakpoint *bpt;
14848
14849 ALL_BREAKPOINTS (bpt)
14850 if (user_breakpoint_p (bpt))
14851 enable_breakpoint (bpt);
14852 }
9eaabc75 14853 else
0d381245 14854 {
9eaabc75
MW
14855 char *num = extract_arg (&args);
14856
14857 while (num)
d248b706 14858 {
9eaabc75 14859 if (strchr (num, '.'))
b775012e 14860 {
9eaabc75
MW
14861 struct bp_location *loc = find_location_by_number (num);
14862
14863 if (loc)
14864 {
14865 if (!loc->enabled)
14866 {
14867 loc->enabled = 1;
14868 mark_breakpoint_location_modified (loc);
14869 }
14870 if (target_supports_enable_disable_tracepoint ()
14871 && current_trace_status ()->running && loc->owner
14872 && is_tracepoint (loc->owner))
14873 target_enable_tracepoint (loc);
14874 }
14875 update_global_location_list (1);
b775012e 14876 }
9eaabc75
MW
14877 else
14878 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14879 num = extract_arg (&args);
d248b706 14880 }
0d381245 14881 }
c906108c
SS
14882}
14883
816338b5
SS
14884/* This struct packages up disposition data for application to multiple
14885 breakpoints. */
14886
14887struct disp_data
14888{
14889 enum bpdisp disp;
14890 int count;
14891};
14892
c906108c 14893static void
51be5b68
PA
14894do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14895{
816338b5 14896 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14897
816338b5 14898 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14899}
14900
14901static void
14902do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14903{
816338b5 14904 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14905
14906 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14907}
14908
c906108c 14909static void
fba45db2 14910enable_once_command (char *args, int from_tty)
c906108c 14911{
51be5b68 14912 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14913}
14914
816338b5
SS
14915static void
14916do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14917{
14918 struct disp_data disp = { disp_disable, *(int *) countptr };
14919
14920 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14921}
14922
14923static void
14924enable_count_command (char *args, int from_tty)
14925{
14926 int count = get_number (&args);
14927
14928 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14929}
14930
c906108c 14931static void
51be5b68 14932do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14933{
816338b5 14934 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14935
14936 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14937}
14938
c906108c 14939static void
fba45db2 14940enable_delete_command (char *args, int from_tty)
c906108c 14941{
51be5b68 14942 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14943}
14944\f
fa8d40ab
JJ
14945static void
14946set_breakpoint_cmd (char *args, int from_tty)
14947{
14948}
14949
14950static void
14951show_breakpoint_cmd (char *args, int from_tty)
14952{
14953}
14954
1f3b5d1b
PP
14955/* Invalidate last known value of any hardware watchpoint if
14956 the memory which that value represents has been written to by
14957 GDB itself. */
14958
14959static void
8de0566d
YQ
14960invalidate_bp_value_on_memory_change (struct inferior *inferior,
14961 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14962 const bfd_byte *data)
14963{
14964 struct breakpoint *bp;
14965
14966 ALL_BREAKPOINTS (bp)
14967 if (bp->enable_state == bp_enabled
3a5c3e22 14968 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14969 {
3a5c3e22 14970 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14971
3a5c3e22
PA
14972 if (wp->val_valid && wp->val)
14973 {
14974 struct bp_location *loc;
14975
14976 for (loc = bp->loc; loc != NULL; loc = loc->next)
14977 if (loc->loc_type == bp_loc_hardware_watchpoint
14978 && loc->address + loc->length > addr
14979 && addr + len > loc->address)
14980 {
14981 value_free (wp->val);
14982 wp->val = NULL;
14983 wp->val_valid = 0;
14984 }
14985 }
1f3b5d1b
PP
14986 }
14987}
14988
8181d85f
DJ
14989/* Create and insert a raw software breakpoint at PC. Return an
14990 identifier, which should be used to remove the breakpoint later.
14991 In general, places which call this should be using something on the
14992 breakpoint chain instead; this function should be eliminated
14993 someday. */
14994
14995void *
6c95b8df
PA
14996deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14997 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
14998{
14999 struct bp_target_info *bp_tgt;
15000
41bf6aca 15001 bp_tgt = XCNEW (struct bp_target_info);
8181d85f 15002
6c95b8df 15003 bp_tgt->placed_address_space = aspace;
8181d85f 15004 bp_tgt->placed_address = pc;
6c95b8df 15005
a6d9a66e 15006 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
15007 {
15008 /* Could not insert the breakpoint. */
15009 xfree (bp_tgt);
15010 return NULL;
15011 }
15012
15013 return bp_tgt;
15014}
15015
4a64f543
MS
15016/* Remove a breakpoint BP inserted by
15017 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
15018
15019int
a6d9a66e 15020deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
15021{
15022 struct bp_target_info *bp_tgt = bp;
15023 int ret;
15024
a6d9a66e 15025 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
15026 xfree (bp_tgt);
15027
15028 return ret;
15029}
15030
4a64f543
MS
15031/* One (or perhaps two) breakpoints used for software single
15032 stepping. */
8181d85f
DJ
15033
15034static void *single_step_breakpoints[2];
a6d9a66e 15035static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
15036
15037/* Create and insert a breakpoint for software single step. */
15038
15039void
6c95b8df 15040insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15041 struct address_space *aspace,
15042 CORE_ADDR next_pc)
8181d85f
DJ
15043{
15044 void **bpt_p;
15045
15046 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
15047 {
15048 bpt_p = &single_step_breakpoints[0];
15049 single_step_gdbarch[0] = gdbarch;
15050 }
8181d85f
DJ
15051 else
15052 {
15053 gdb_assert (single_step_breakpoints[1] == NULL);
15054 bpt_p = &single_step_breakpoints[1];
a6d9a66e 15055 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
15056 }
15057
4a64f543
MS
15058 /* NOTE drow/2006-04-11: A future improvement to this function would
15059 be to only create the breakpoints once, and actually put them on
15060 the breakpoint chain. That would let us use set_raw_breakpoint.
15061 We could adjust the addresses each time they were needed. Doing
15062 this requires corresponding changes elsewhere where single step
15063 breakpoints are handled, however. So, for now, we use this. */
8181d85f 15064
6c95b8df 15065 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 15066 if (*bpt_p == NULL)
5af949e3
UW
15067 error (_("Could not insert single-step breakpoint at %s"),
15068 paddress (gdbarch, next_pc));
8181d85f
DJ
15069}
15070
f02253f1
HZ
15071/* Check if the breakpoints used for software single stepping
15072 were inserted or not. */
15073
15074int
15075single_step_breakpoints_inserted (void)
15076{
15077 return (single_step_breakpoints[0] != NULL
15078 || single_step_breakpoints[1] != NULL);
15079}
15080
8181d85f
DJ
15081/* Remove and delete any breakpoints used for software single step. */
15082
15083void
15084remove_single_step_breakpoints (void)
15085{
15086 gdb_assert (single_step_breakpoints[0] != NULL);
15087
15088 /* See insert_single_step_breakpoint for more about this deprecated
15089 call. */
a6d9a66e
UW
15090 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15091 single_step_breakpoints[0]);
15092 single_step_gdbarch[0] = NULL;
8181d85f
DJ
15093 single_step_breakpoints[0] = NULL;
15094
15095 if (single_step_breakpoints[1] != NULL)
15096 {
a6d9a66e
UW
15097 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15098 single_step_breakpoints[1]);
15099 single_step_gdbarch[1] = NULL;
8181d85f
DJ
15100 single_step_breakpoints[1] = NULL;
15101 }
15102}
15103
d03285ec
UW
15104/* Delete software single step breakpoints without removing them from
15105 the inferior. This is intended to be used if the inferior's address
15106 space where they were inserted is already gone, e.g. after exit or
15107 exec. */
15108
15109void
15110cancel_single_step_breakpoints (void)
15111{
15112 int i;
15113
15114 for (i = 0; i < 2; i++)
15115 if (single_step_breakpoints[i])
15116 {
15117 xfree (single_step_breakpoints[i]);
15118 single_step_breakpoints[i] = NULL;
15119 single_step_gdbarch[i] = NULL;
15120 }
15121}
15122
15123/* Detach software single-step breakpoints from INFERIOR_PTID without
15124 removing them. */
15125
15126static void
15127detach_single_step_breakpoints (void)
15128{
15129 int i;
15130
15131 for (i = 0; i < 2; i++)
15132 if (single_step_breakpoints[i])
15133 target_remove_breakpoint (single_step_gdbarch[i],
15134 single_step_breakpoints[i]);
15135}
15136
4a64f543
MS
15137/* Check whether a software single-step breakpoint is inserted at
15138 PC. */
1aafd4da
UW
15139
15140static int
cc59ec59
MS
15141single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15142 CORE_ADDR pc)
1aafd4da
UW
15143{
15144 int i;
15145
15146 for (i = 0; i < 2; i++)
15147 {
15148 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
15149 if (bp_tgt
15150 && breakpoint_address_match (bp_tgt->placed_address_space,
15151 bp_tgt->placed_address,
15152 aspace, pc))
1aafd4da
UW
15153 return 1;
15154 }
15155
15156 return 0;
15157}
15158
a96d9b2e
SDJ
15159/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15160 non-zero otherwise. */
15161static int
15162is_syscall_catchpoint_enabled (struct breakpoint *bp)
15163{
15164 if (syscall_catchpoint_p (bp)
15165 && bp->enable_state != bp_disabled
15166 && bp->enable_state != bp_call_disabled)
15167 return 1;
15168 else
15169 return 0;
15170}
15171
15172int
15173catch_syscall_enabled (void)
15174{
fa3064dd
YQ
15175 struct catch_syscall_inferior_data *inf_data
15176 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15177
fa3064dd 15178 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15179}
15180
15181int
15182catching_syscall_number (int syscall_number)
15183{
15184 struct breakpoint *bp;
15185
15186 ALL_BREAKPOINTS (bp)
15187 if (is_syscall_catchpoint_enabled (bp))
15188 {
be5c67c1
PA
15189 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15190
15191 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15192 {
15193 int i, iter;
15194 for (i = 0;
be5c67c1 15195 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15196 i++)
15197 if (syscall_number == iter)
15198 return 1;
15199 }
15200 else
15201 return 1;
15202 }
15203
15204 return 0;
15205}
15206
15207/* Complete syscall names. Used by "catch syscall". */
49c4e619 15208static VEC (char_ptr) *
a96d9b2e 15209catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15210 const char *text, const char *word)
a96d9b2e
SDJ
15211{
15212 const char **list = get_syscall_names ();
49c4e619 15213 VEC (char_ptr) *retlist
b45627a0 15214 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15215
c38eea1a
MS
15216 xfree (list);
15217 return retlist;
a96d9b2e
SDJ
15218}
15219
1042e4c0
SS
15220/* Tracepoint-specific operations. */
15221
15222/* Set tracepoint count to NUM. */
15223static void
15224set_tracepoint_count (int num)
15225{
15226 tracepoint_count = num;
4fa62494 15227 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15228}
15229
70221824 15230static void
1042e4c0
SS
15231trace_command (char *arg, int from_tty)
15232{
55aa24fb
SDJ
15233 struct breakpoint_ops *ops;
15234 const char *arg_cp = arg;
15235
15236 if (arg && probe_linespec_to_ops (&arg_cp))
15237 ops = &tracepoint_probe_breakpoint_ops;
15238 else
15239 ops = &tracepoint_breakpoint_ops;
15240
558a9d82
YQ
15241 create_breakpoint (get_current_arch (),
15242 arg,
15243 NULL, 0, NULL, 1 /* parse arg */,
15244 0 /* tempflag */,
15245 bp_tracepoint /* type_wanted */,
15246 0 /* Ignore count */,
15247 pending_break_support,
15248 ops,
15249 from_tty,
15250 1 /* enabled */,
15251 0 /* internal */, 0);
1042e4c0
SS
15252}
15253
70221824 15254static void
7a697b8d
SS
15255ftrace_command (char *arg, int from_tty)
15256{
558a9d82
YQ
15257 create_breakpoint (get_current_arch (),
15258 arg,
15259 NULL, 0, NULL, 1 /* parse arg */,
15260 0 /* tempflag */,
15261 bp_fast_tracepoint /* type_wanted */,
15262 0 /* Ignore count */,
15263 pending_break_support,
15264 &tracepoint_breakpoint_ops,
15265 from_tty,
15266 1 /* enabled */,
15267 0 /* internal */, 0);
0fb4aa4b
PA
15268}
15269
15270/* strace command implementation. Creates a static tracepoint. */
15271
70221824 15272static void
0fb4aa4b
PA
15273strace_command (char *arg, int from_tty)
15274{
983af33b
SDJ
15275 struct breakpoint_ops *ops;
15276
15277 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15278 or with a normal static tracepoint. */
15279 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15280 ops = &strace_marker_breakpoint_ops;
15281 else
15282 ops = &tracepoint_breakpoint_ops;
15283
558a9d82
YQ
15284 create_breakpoint (get_current_arch (),
15285 arg,
15286 NULL, 0, NULL, 1 /* parse arg */,
15287 0 /* tempflag */,
15288 bp_static_tracepoint /* type_wanted */,
15289 0 /* Ignore count */,
15290 pending_break_support,
15291 ops,
15292 from_tty,
15293 1 /* enabled */,
15294 0 /* internal */, 0);
7a697b8d
SS
15295}
15296
409873ef
SS
15297/* Set up a fake reader function that gets command lines from a linked
15298 list that was acquired during tracepoint uploading. */
15299
15300static struct uploaded_tp *this_utp;
3149d8c1 15301static int next_cmd;
409873ef
SS
15302
15303static char *
15304read_uploaded_action (void)
15305{
15306 char *rslt;
15307
3149d8c1 15308 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15309
3149d8c1 15310 next_cmd++;
409873ef
SS
15311
15312 return rslt;
15313}
15314
00bf0b85
SS
15315/* Given information about a tracepoint as recorded on a target (which
15316 can be either a live system or a trace file), attempt to create an
15317 equivalent GDB tracepoint. This is not a reliable process, since
15318 the target does not necessarily have all the information used when
15319 the tracepoint was originally defined. */
15320
d9b3f62e 15321struct tracepoint *
00bf0b85 15322create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15323{
409873ef 15324 char *addr_str, small_buf[100];
d9b3f62e 15325 struct tracepoint *tp;
fd9b8c24 15326
409873ef
SS
15327 if (utp->at_string)
15328 addr_str = utp->at_string;
15329 else
15330 {
15331 /* In the absence of a source location, fall back to raw
15332 address. Since there is no way to confirm that the address
15333 means the same thing as when the trace was started, warn the
15334 user. */
3e43a32a
MS
15335 warning (_("Uploaded tracepoint %d has no "
15336 "source location, using raw address"),
409873ef 15337 utp->number);
8c042590 15338 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15339 addr_str = small_buf;
15340 }
15341
15342 /* There's not much we can do with a sequence of bytecodes. */
15343 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15344 warning (_("Uploaded tracepoint %d condition "
15345 "has no source form, ignoring it"),
409873ef 15346 utp->number);
d5551862 15347
8cdf0e15 15348 if (!create_breakpoint (get_current_arch (),
409873ef 15349 addr_str,
e7e0cddf
SS
15350 utp->cond_string, -1, NULL,
15351 0 /* parse cond/thread */,
8cdf0e15 15352 0 /* tempflag */,
0fb4aa4b 15353 utp->type /* type_wanted */,
8cdf0e15
VP
15354 0 /* Ignore count */,
15355 pending_break_support,
348d480f 15356 &tracepoint_breakpoint_ops,
8cdf0e15 15357 0 /* from_tty */,
84f4c1fe 15358 utp->enabled /* enabled */,
44f238bb
PA
15359 0 /* internal */,
15360 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15361 return NULL;
15362
409873ef 15363 /* Get the tracepoint we just created. */
fd9b8c24
PA
15364 tp = get_tracepoint (tracepoint_count);
15365 gdb_assert (tp != NULL);
d5551862 15366
00bf0b85
SS
15367 if (utp->pass > 0)
15368 {
8c042590
PM
15369 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15370 tp->base.number);
00bf0b85 15371
409873ef 15372 trace_pass_command (small_buf, 0);
00bf0b85
SS
15373 }
15374
409873ef
SS
15375 /* If we have uploaded versions of the original commands, set up a
15376 special-purpose "reader" function and call the usual command line
15377 reader, then pass the result to the breakpoint command-setting
15378 function. */
3149d8c1 15379 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15380 {
409873ef 15381 struct command_line *cmd_list;
00bf0b85 15382
409873ef 15383 this_utp = utp;
3149d8c1 15384 next_cmd = 0;
d5551862 15385
409873ef
SS
15386 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15387
d9b3f62e 15388 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15389 }
3149d8c1
SS
15390 else if (!VEC_empty (char_ptr, utp->actions)
15391 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15392 warning (_("Uploaded tracepoint %d actions "
15393 "have no source form, ignoring them"),
409873ef 15394 utp->number);
00bf0b85 15395
f196051f
SS
15396 /* Copy any status information that might be available. */
15397 tp->base.hit_count = utp->hit_count;
15398 tp->traceframe_usage = utp->traceframe_usage;
15399
00bf0b85 15400 return tp;
d9b3f62e 15401}
00bf0b85 15402
1042e4c0
SS
15403/* Print information on tracepoint number TPNUM_EXP, or all if
15404 omitted. */
15405
15406static void
e5a67952 15407tracepoints_info (char *args, int from_tty)
1042e4c0 15408{
79a45e25 15409 struct ui_out *uiout = current_uiout;
e5a67952 15410 int num_printed;
1042e4c0 15411
e5a67952 15412 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15413
15414 if (num_printed == 0)
1042e4c0 15415 {
e5a67952 15416 if (args == NULL || *args == '\0')
d77f58be
SS
15417 ui_out_message (uiout, 0, "No tracepoints.\n");
15418 else
e5a67952 15419 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15420 }
ad443146
SS
15421
15422 default_collect_info ();
1042e4c0
SS
15423}
15424
4a64f543 15425/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15426 Not supported by all targets. */
15427static void
15428enable_trace_command (char *args, int from_tty)
15429{
15430 enable_command (args, from_tty);
15431}
15432
4a64f543 15433/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15434 Not supported by all targets. */
15435static void
15436disable_trace_command (char *args, int from_tty)
15437{
15438 disable_command (args, from_tty);
15439}
15440
4a64f543 15441/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15442static void
15443delete_trace_command (char *arg, int from_tty)
15444{
35df4500 15445 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15446
15447 dont_repeat ();
15448
15449 if (arg == 0)
15450 {
15451 int breaks_to_delete = 0;
15452
15453 /* Delete all breakpoints if no argument.
15454 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15455 have to be deleted with an explicit breakpoint number
15456 argument. */
1042e4c0 15457 ALL_TRACEPOINTS (b)
46c6471b 15458 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15459 {
15460 breaks_to_delete = 1;
15461 break;
15462 }
1042e4c0
SS
15463
15464 /* Ask user only if there are some breakpoints to delete. */
15465 if (!from_tty
15466 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15467 {
35df4500 15468 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15469 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15470 delete_breakpoint (b);
1042e4c0
SS
15471 }
15472 }
15473 else
51be5b68 15474 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15475}
15476
197f0a60
TT
15477/* Helper function for trace_pass_command. */
15478
15479static void
d9b3f62e 15480trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15481{
d9b3f62e 15482 tp->pass_count = count;
6f6484cd 15483 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15484 if (from_tty)
15485 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15486 tp->base.number, count);
197f0a60
TT
15487}
15488
1042e4c0
SS
15489/* Set passcount for tracepoint.
15490
15491 First command argument is passcount, second is tracepoint number.
15492 If tracepoint number omitted, apply to most recently defined.
15493 Also accepts special argument "all". */
15494
15495static void
15496trace_pass_command (char *args, int from_tty)
15497{
d9b3f62e 15498 struct tracepoint *t1;
1042e4c0 15499 unsigned int count;
1042e4c0
SS
15500
15501 if (args == 0 || *args == 0)
3e43a32a
MS
15502 error (_("passcount command requires an "
15503 "argument (count + optional TP num)"));
1042e4c0 15504
4a64f543 15505 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15506
529480d0 15507 args = skip_spaces (args);
1042e4c0
SS
15508 if (*args && strncasecmp (args, "all", 3) == 0)
15509 {
d9b3f62e
PA
15510 struct breakpoint *b;
15511
1042e4c0 15512 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15513 if (*args)
15514 error (_("Junk at end of arguments."));
1042e4c0 15515
d9b3f62e 15516 ALL_TRACEPOINTS (b)
197f0a60 15517 {
d9b3f62e 15518 t1 = (struct tracepoint *) b;
197f0a60
TT
15519 trace_pass_set_count (t1, count, from_tty);
15520 }
15521 }
15522 else if (*args == '\0')
1042e4c0 15523 {
197f0a60 15524 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 15525 if (t1)
197f0a60
TT
15526 trace_pass_set_count (t1, count, from_tty);
15527 }
15528 else
15529 {
15530 struct get_number_or_range_state state;
15531
15532 init_number_or_range (&state, args);
15533 while (!state.finished)
1042e4c0 15534 {
197f0a60
TT
15535 t1 = get_tracepoint_by_number (&args, &state, 1);
15536 if (t1)
15537 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15538 }
15539 }
1042e4c0
SS
15540}
15541
d9b3f62e 15542struct tracepoint *
1042e4c0
SS
15543get_tracepoint (int num)
15544{
15545 struct breakpoint *t;
15546
15547 ALL_TRACEPOINTS (t)
15548 if (t->number == num)
d9b3f62e 15549 return (struct tracepoint *) t;
1042e4c0
SS
15550
15551 return NULL;
15552}
15553
d5551862
SS
15554/* Find the tracepoint with the given target-side number (which may be
15555 different from the tracepoint number after disconnecting and
15556 reconnecting). */
15557
d9b3f62e 15558struct tracepoint *
d5551862
SS
15559get_tracepoint_by_number_on_target (int num)
15560{
d9b3f62e 15561 struct breakpoint *b;
d5551862 15562
d9b3f62e
PA
15563 ALL_TRACEPOINTS (b)
15564 {
15565 struct tracepoint *t = (struct tracepoint *) b;
15566
15567 if (t->number_on_target == num)
15568 return t;
15569 }
d5551862
SS
15570
15571 return NULL;
15572}
15573
1042e4c0 15574/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
15575 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15576 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 15577 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 15578struct tracepoint *
197f0a60
TT
15579get_tracepoint_by_number (char **arg,
15580 struct get_number_or_range_state *state,
15581 int optional_p)
1042e4c0 15582{
1042e4c0
SS
15583 struct breakpoint *t;
15584 int tpnum;
15585 char *instring = arg == NULL ? NULL : *arg;
15586
197f0a60
TT
15587 if (state)
15588 {
15589 gdb_assert (!state->finished);
15590 tpnum = get_number_or_range (state);
15591 }
15592 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
15593 {
15594 if (optional_p)
15595 tpnum = tracepoint_count;
15596 else
15597 error_no_arg (_("tracepoint number"));
15598 }
15599 else
197f0a60 15600 tpnum = get_number (arg);
1042e4c0
SS
15601
15602 if (tpnum <= 0)
15603 {
15604 if (instring && *instring)
15605 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15606 instring);
15607 else
3e43a32a
MS
15608 printf_filtered (_("Tracepoint argument missing "
15609 "and no previous tracepoint\n"));
1042e4c0
SS
15610 return NULL;
15611 }
15612
15613 ALL_TRACEPOINTS (t)
15614 if (t->number == tpnum)
15615 {
d9b3f62e 15616 return (struct tracepoint *) t;
1042e4c0
SS
15617 }
15618
1042e4c0
SS
15619 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15620 return NULL;
15621}
15622
d9b3f62e
PA
15623void
15624print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15625{
15626 if (b->thread != -1)
15627 fprintf_unfiltered (fp, " thread %d", b->thread);
15628
15629 if (b->task != 0)
15630 fprintf_unfiltered (fp, " task %d", b->task);
15631
15632 fprintf_unfiltered (fp, "\n");
15633}
15634
6149aea9
PA
15635/* Save information on user settable breakpoints (watchpoints, etc) to
15636 a new script file named FILENAME. If FILTER is non-NULL, call it
15637 on each breakpoint and only include the ones for which it returns
15638 non-zero. */
15639
1042e4c0 15640static void
6149aea9
PA
15641save_breakpoints (char *filename, int from_tty,
15642 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15643{
15644 struct breakpoint *tp;
6149aea9 15645 int any = 0;
1042e4c0 15646 struct cleanup *cleanup;
a7bdde9e 15647 struct ui_file *fp;
6149aea9 15648 int extra_trace_bits = 0;
1042e4c0 15649
6149aea9
PA
15650 if (filename == 0 || *filename == 0)
15651 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15652
15653 /* See if we have anything to save. */
6149aea9 15654 ALL_BREAKPOINTS (tp)
1042e4c0 15655 {
6149aea9 15656 /* Skip internal and momentary breakpoints. */
09d682a4 15657 if (!user_breakpoint_p (tp))
6149aea9
PA
15658 continue;
15659
15660 /* If we have a filter, only save the breakpoints it accepts. */
15661 if (filter && !filter (tp))
15662 continue;
15663
15664 any = 1;
15665
15666 if (is_tracepoint (tp))
15667 {
15668 extra_trace_bits = 1;
15669
15670 /* We can stop searching. */
15671 break;
15672 }
1042e4c0 15673 }
6149aea9
PA
15674
15675 if (!any)
1042e4c0 15676 {
6149aea9 15677 warning (_("Nothing to save."));
1042e4c0
SS
15678 return;
15679 }
15680
c718be47
PA
15681 filename = tilde_expand (filename);
15682 cleanup = make_cleanup (xfree, filename);
15683 fp = gdb_fopen (filename, "w");
059fb39f 15684 if (!fp)
6149aea9
PA
15685 error (_("Unable to open file '%s' for saving (%s)"),
15686 filename, safe_strerror (errno));
a7bdde9e 15687 make_cleanup_ui_file_delete (fp);
8bf6485c 15688
6149aea9
PA
15689 if (extra_trace_bits)
15690 save_trace_state_variables (fp);
8bf6485c 15691
6149aea9 15692 ALL_BREAKPOINTS (tp)
1042e4c0 15693 {
6149aea9 15694 /* Skip internal and momentary breakpoints. */
09d682a4 15695 if (!user_breakpoint_p (tp))
6149aea9 15696 continue;
8bf6485c 15697
6149aea9
PA
15698 /* If we have a filter, only save the breakpoints it accepts. */
15699 if (filter && !filter (tp))
15700 continue;
15701
348d480f 15702 tp->ops->print_recreate (tp, fp);
1042e4c0 15703
6149aea9
PA
15704 /* Note, we can't rely on tp->number for anything, as we can't
15705 assume the recreated breakpoint numbers will match. Use $bpnum
15706 instead. */
15707
15708 if (tp->cond_string)
15709 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15710
15711 if (tp->ignore_count)
15712 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15713
2d9442cc 15714 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15715 {
a7bdde9e
VP
15716 volatile struct gdb_exception ex;
15717
6149aea9 15718 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15719
79a45e25 15720 ui_out_redirect (current_uiout, fp);
14dba4b4 15721 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15722 {
79a45e25 15723 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15724 }
79a45e25 15725 ui_out_redirect (current_uiout, NULL);
1042e4c0 15726
a7bdde9e
VP
15727 if (ex.reason < 0)
15728 throw_exception (ex);
1042e4c0 15729
a7bdde9e 15730 fprintf_unfiltered (fp, " end\n");
1042e4c0 15731 }
6149aea9
PA
15732
15733 if (tp->enable_state == bp_disabled)
15734 fprintf_unfiltered (fp, "disable\n");
15735
15736 /* If this is a multi-location breakpoint, check if the locations
15737 should be individually disabled. Watchpoint locations are
15738 special, and not user visible. */
15739 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15740 {
15741 struct bp_location *loc;
15742 int n = 1;
15743
15744 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15745 if (!loc->enabled)
15746 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15747 }
1042e4c0 15748 }
8bf6485c 15749
6149aea9 15750 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15751 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15752
1042e4c0 15753 if (from_tty)
6149aea9 15754 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15755 do_cleanups (cleanup);
6149aea9
PA
15756}
15757
15758/* The `save breakpoints' command. */
15759
15760static void
15761save_breakpoints_command (char *args, int from_tty)
15762{
15763 save_breakpoints (args, from_tty, NULL);
15764}
15765
15766/* The `save tracepoints' command. */
15767
15768static void
15769save_tracepoints_command (char *args, int from_tty)
15770{
15771 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15772}
15773
15774/* Create a vector of all tracepoints. */
15775
15776VEC(breakpoint_p) *
eeae04df 15777all_tracepoints (void)
1042e4c0
SS
15778{
15779 VEC(breakpoint_p) *tp_vec = 0;
15780 struct breakpoint *tp;
15781
15782 ALL_TRACEPOINTS (tp)
15783 {
15784 VEC_safe_push (breakpoint_p, tp_vec, tp);
15785 }
15786
15787 return tp_vec;
15788}
15789
c906108c 15790\f
4a64f543
MS
15791/* This help string is used for the break, hbreak, tbreak and thbreak
15792 commands. It is defined as a macro to prevent duplication.
15793 COMMAND should be a string constant containing the name of the
15794 command. */
31e2b00f 15795#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15796command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15797PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15798probe point. Accepted values are `-probe' (for a generic, automatically\n\
15799guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
15800LOCATION may be a line number, function name, or \"*\" and an address.\n\
15801If a line number is specified, break at start of code for that line.\n\
15802If a function is specified, break at start of code for that function.\n\
15803If an address is specified, break at that exact address.\n\
dc10affe
PA
15804With no LOCATION, uses current execution address of the selected\n\
15805stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15806\n\
15807THREADNUM is the number from \"info threads\".\n\
15808CONDITION is a boolean expression.\n\
15809\n\
d41c0fc8
PA
15810Multiple breakpoints at one place are permitted, and useful if their\n\
15811conditions are different.\n\
31e2b00f
AS
15812\n\
15813Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15814
44feb3ce
TT
15815/* List of subcommands for "catch". */
15816static struct cmd_list_element *catch_cmdlist;
15817
15818/* List of subcommands for "tcatch". */
15819static struct cmd_list_element *tcatch_cmdlist;
15820
9ac4176b 15821void
44feb3ce
TT
15822add_catch_command (char *name, char *docstring,
15823 void (*sfunc) (char *args, int from_tty,
15824 struct cmd_list_element *command),
625e8578 15825 completer_ftype *completer,
44feb3ce
TT
15826 void *user_data_catch,
15827 void *user_data_tcatch)
15828{
15829 struct cmd_list_element *command;
15830
15831 command = add_cmd (name, class_breakpoint, NULL, docstring,
15832 &catch_cmdlist);
15833 set_cmd_sfunc (command, sfunc);
15834 set_cmd_context (command, user_data_catch);
a96d9b2e 15835 set_cmd_completer (command, completer);
44feb3ce
TT
15836
15837 command = add_cmd (name, class_breakpoint, NULL, docstring,
15838 &tcatch_cmdlist);
15839 set_cmd_sfunc (command, sfunc);
15840 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15841 set_cmd_completer (command, completer);
44feb3ce
TT
15842}
15843
6c95b8df 15844static void
a79b8f6e 15845clear_syscall_counts (struct inferior *inf)
6c95b8df 15846{
fa3064dd
YQ
15847 struct catch_syscall_inferior_data *inf_data
15848 = get_catch_syscall_inferior_data (inf);
15849
15850 inf_data->total_syscalls_count = 0;
15851 inf_data->any_syscall_count = 0;
15852 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
15853}
15854
6149aea9
PA
15855static void
15856save_command (char *arg, int from_tty)
15857{
3e43a32a
MS
15858 printf_unfiltered (_("\"save\" must be followed by "
15859 "the name of a save subcommand.\n"));
6149aea9
PA
15860 help_list (save_cmdlist, "save ", -1, gdb_stdout);
15861}
15862
84f4c1fe
PM
15863struct breakpoint *
15864iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15865 void *data)
15866{
35df4500 15867 struct breakpoint *b, *b_tmp;
84f4c1fe 15868
35df4500 15869 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15870 {
15871 if ((*callback) (b, data))
15872 return b;
15873 }
15874
15875 return NULL;
15876}
15877
0574c78f
GB
15878/* Zero if any of the breakpoint's locations could be a location where
15879 functions have been inlined, nonzero otherwise. */
15880
15881static int
15882is_non_inline_function (struct breakpoint *b)
15883{
15884 /* The shared library event breakpoint is set on the address of a
15885 non-inline function. */
15886 if (b->type == bp_shlib_event)
15887 return 1;
15888
15889 return 0;
15890}
15891
15892/* Nonzero if the specified PC cannot be a location where functions
15893 have been inlined. */
15894
15895int
09ac7c10
TT
15896pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15897 const struct target_waitstatus *ws)
0574c78f
GB
15898{
15899 struct breakpoint *b;
15900 struct bp_location *bl;
15901
15902 ALL_BREAKPOINTS (b)
15903 {
15904 if (!is_non_inline_function (b))
15905 continue;
15906
15907 for (bl = b->loc; bl != NULL; bl = bl->next)
15908 {
15909 if (!bl->shlib_disabled
09ac7c10 15910 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15911 return 1;
15912 }
15913 }
15914
15915 return 0;
15916}
15917
2f202fde
JK
15918/* Remove any references to OBJFILE which is going to be freed. */
15919
15920void
15921breakpoint_free_objfile (struct objfile *objfile)
15922{
15923 struct bp_location **locp, *loc;
15924
15925 ALL_BP_LOCATIONS (loc, locp)
15926 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
15927 loc->symtab = NULL;
15928}
15929
2060206e
PA
15930void
15931initialize_breakpoint_ops (void)
15932{
15933 static int initialized = 0;
15934
15935 struct breakpoint_ops *ops;
15936
15937 if (initialized)
15938 return;
15939 initialized = 1;
15940
15941 /* The breakpoint_ops structure to be inherit by all kinds of
15942 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15943 internal and momentary breakpoints, etc.). */
15944 ops = &bkpt_base_breakpoint_ops;
15945 *ops = base_breakpoint_ops;
15946 ops->re_set = bkpt_re_set;
15947 ops->insert_location = bkpt_insert_location;
15948 ops->remove_location = bkpt_remove_location;
15949 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
15950 ops->create_sals_from_address = bkpt_create_sals_from_address;
15951 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15952 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
15953
15954 /* The breakpoint_ops structure to be used in regular breakpoints. */
15955 ops = &bkpt_breakpoint_ops;
15956 *ops = bkpt_base_breakpoint_ops;
15957 ops->re_set = bkpt_re_set;
15958 ops->resources_needed = bkpt_resources_needed;
15959 ops->print_it = bkpt_print_it;
15960 ops->print_mention = bkpt_print_mention;
15961 ops->print_recreate = bkpt_print_recreate;
15962
15963 /* Ranged breakpoints. */
15964 ops = &ranged_breakpoint_ops;
15965 *ops = bkpt_breakpoint_ops;
15966 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15967 ops->resources_needed = resources_needed_ranged_breakpoint;
15968 ops->print_it = print_it_ranged_breakpoint;
15969 ops->print_one = print_one_ranged_breakpoint;
15970 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15971 ops->print_mention = print_mention_ranged_breakpoint;
15972 ops->print_recreate = print_recreate_ranged_breakpoint;
15973
15974 /* Internal breakpoints. */
15975 ops = &internal_breakpoint_ops;
15976 *ops = bkpt_base_breakpoint_ops;
15977 ops->re_set = internal_bkpt_re_set;
15978 ops->check_status = internal_bkpt_check_status;
15979 ops->print_it = internal_bkpt_print_it;
15980 ops->print_mention = internal_bkpt_print_mention;
15981
15982 /* Momentary breakpoints. */
15983 ops = &momentary_breakpoint_ops;
15984 *ops = bkpt_base_breakpoint_ops;
15985 ops->re_set = momentary_bkpt_re_set;
15986 ops->check_status = momentary_bkpt_check_status;
15987 ops->print_it = momentary_bkpt_print_it;
15988 ops->print_mention = momentary_bkpt_print_mention;
15989
e2e4d78b
JK
15990 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15991 ops = &longjmp_breakpoint_ops;
15992 *ops = momentary_breakpoint_ops;
15993 ops->dtor = longjmp_bkpt_dtor;
15994
55aa24fb
SDJ
15995 /* Probe breakpoints. */
15996 ops = &bkpt_probe_breakpoint_ops;
15997 *ops = bkpt_breakpoint_ops;
15998 ops->insert_location = bkpt_probe_insert_location;
15999 ops->remove_location = bkpt_probe_remove_location;
16000 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16001 ops->decode_linespec = bkpt_probe_decode_linespec;
16002
2060206e
PA
16003 /* Watchpoints. */
16004 ops = &watchpoint_breakpoint_ops;
16005 *ops = base_breakpoint_ops;
3a5c3e22 16006 ops->dtor = dtor_watchpoint;
2060206e
PA
16007 ops->re_set = re_set_watchpoint;
16008 ops->insert_location = insert_watchpoint;
16009 ops->remove_location = remove_watchpoint;
16010 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16011 ops->check_status = check_status_watchpoint;
16012 ops->resources_needed = resources_needed_watchpoint;
16013 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16014 ops->print_it = print_it_watchpoint;
16015 ops->print_mention = print_mention_watchpoint;
16016 ops->print_recreate = print_recreate_watchpoint;
427cd150 16017 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16018
16019 /* Masked watchpoints. */
16020 ops = &masked_watchpoint_breakpoint_ops;
16021 *ops = watchpoint_breakpoint_ops;
16022 ops->insert_location = insert_masked_watchpoint;
16023 ops->remove_location = remove_masked_watchpoint;
16024 ops->resources_needed = resources_needed_masked_watchpoint;
16025 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16026 ops->print_it = print_it_masked_watchpoint;
16027 ops->print_one_detail = print_one_detail_masked_watchpoint;
16028 ops->print_mention = print_mention_masked_watchpoint;
16029 ops->print_recreate = print_recreate_masked_watchpoint;
16030
16031 /* Tracepoints. */
16032 ops = &tracepoint_breakpoint_ops;
16033 *ops = base_breakpoint_ops;
16034 ops->re_set = tracepoint_re_set;
16035 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16036 ops->print_one_detail = tracepoint_print_one_detail;
16037 ops->print_mention = tracepoint_print_mention;
16038 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16039 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16040 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16041 ops->decode_linespec = tracepoint_decode_linespec;
16042
55aa24fb
SDJ
16043 /* Probe tracepoints. */
16044 ops = &tracepoint_probe_breakpoint_ops;
16045 *ops = tracepoint_breakpoint_ops;
16046 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16047 ops->decode_linespec = tracepoint_probe_decode_linespec;
16048
983af33b
SDJ
16049 /* Static tracepoints with marker (`-m'). */
16050 ops = &strace_marker_breakpoint_ops;
16051 *ops = tracepoint_breakpoint_ops;
16052 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16053 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16054 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16055
16056 /* Fork catchpoints. */
16057 ops = &catch_fork_breakpoint_ops;
16058 *ops = base_breakpoint_ops;
16059 ops->insert_location = insert_catch_fork;
16060 ops->remove_location = remove_catch_fork;
16061 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16062 ops->print_it = print_it_catch_fork;
16063 ops->print_one = print_one_catch_fork;
16064 ops->print_mention = print_mention_catch_fork;
16065 ops->print_recreate = print_recreate_catch_fork;
16066
16067 /* Vfork catchpoints. */
16068 ops = &catch_vfork_breakpoint_ops;
16069 *ops = base_breakpoint_ops;
16070 ops->insert_location = insert_catch_vfork;
16071 ops->remove_location = remove_catch_vfork;
16072 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16073 ops->print_it = print_it_catch_vfork;
16074 ops->print_one = print_one_catch_vfork;
16075 ops->print_mention = print_mention_catch_vfork;
16076 ops->print_recreate = print_recreate_catch_vfork;
16077
16078 /* Exec catchpoints. */
16079 ops = &catch_exec_breakpoint_ops;
16080 *ops = base_breakpoint_ops;
16081 ops->dtor = dtor_catch_exec;
16082 ops->insert_location = insert_catch_exec;
16083 ops->remove_location = remove_catch_exec;
16084 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16085 ops->print_it = print_it_catch_exec;
16086 ops->print_one = print_one_catch_exec;
16087 ops->print_mention = print_mention_catch_exec;
16088 ops->print_recreate = print_recreate_catch_exec;
16089
16090 /* Syscall catchpoints. */
16091 ops = &catch_syscall_breakpoint_ops;
16092 *ops = base_breakpoint_ops;
16093 ops->dtor = dtor_catch_syscall;
16094 ops->insert_location = insert_catch_syscall;
16095 ops->remove_location = remove_catch_syscall;
16096 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16097 ops->print_it = print_it_catch_syscall;
16098 ops->print_one = print_one_catch_syscall;
16099 ops->print_mention = print_mention_catch_syscall;
16100 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16101
16102 /* Solib-related catchpoints. */
16103 ops = &catch_solib_breakpoint_ops;
16104 *ops = base_breakpoint_ops;
16105 ops->dtor = dtor_catch_solib;
16106 ops->insert_location = insert_catch_solib;
16107 ops->remove_location = remove_catch_solib;
16108 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16109 ops->check_status = check_status_catch_solib;
16110 ops->print_it = print_it_catch_solib;
16111 ops->print_one = print_one_catch_solib;
16112 ops->print_mention = print_mention_catch_solib;
16113 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16114
16115 ops = &dprintf_breakpoint_ops;
16116 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16117 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16118 ops->resources_needed = bkpt_resources_needed;
16119 ops->print_it = bkpt_print_it;
16120 ops->print_mention = bkpt_print_mention;
2d9442cc 16121 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16122 ops->after_condition_true = dprintf_after_condition_true;
2060206e
PA
16123}
16124
8bfd80db
YQ
16125/* Chain containing all defined "enable breakpoint" subcommands. */
16126
16127static struct cmd_list_element *enablebreaklist = NULL;
16128
c906108c 16129void
fba45db2 16130_initialize_breakpoint (void)
c906108c
SS
16131{
16132 struct cmd_list_element *c;
16133
2060206e
PA
16134 initialize_breakpoint_ops ();
16135
84acb35a 16136 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16137 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16138 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16139 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16140
55aa24fb
SDJ
16141 breakpoint_objfile_key
16142 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16143
fa3064dd 16144 catch_syscall_inferior_data
8e260fc0
TT
16145 = register_inferior_data_with_cleanup (NULL,
16146 catch_syscall_inferior_data_cleanup);
fa3064dd 16147
c906108c
SS
16148 breakpoint_chain = 0;
16149 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16150 before a breakpoint is set. */
16151 breakpoint_count = 0;
16152
1042e4c0
SS
16153 tracepoint_count = 0;
16154
1bedd215
AC
16155 add_com ("ignore", class_breakpoint, ignore_command, _("\
16156Set ignore-count of breakpoint number N to COUNT.\n\
16157Usage is `ignore N COUNT'."));
c906108c 16158 if (xdb_commands)
c5aa993b 16159 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16160
1bedd215
AC
16161 add_com ("commands", class_breakpoint, commands_command, _("\
16162Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16163Give breakpoint number as argument after \"commands\".\n\
16164With no argument, the targeted breakpoint is the last one set.\n\
16165The commands themselves follow starting on the next line.\n\
16166Type a line containing \"end\" to indicate the end of them.\n\
16167Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16168then no output is printed when it is hit, except what the commands print."));
c906108c 16169
d55637df 16170 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16171Specify breakpoint number N to break only if COND is true.\n\
c906108c 16172Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16173expression to be evaluated whenever breakpoint N is reached."));
d55637df 16174 set_cmd_completer (c, condition_completer);
c906108c 16175
1bedd215 16176 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16177Set a temporary breakpoint.\n\
c906108c
SS
16178Like \"break\" except the breakpoint is only temporary,\n\
16179so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16180by using \"enable delete\" on the breakpoint number.\n\
16181\n"
16182BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16183 set_cmd_completer (c, location_completer);
c94fdfd0 16184
1bedd215 16185 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16186Set a hardware assisted breakpoint.\n\
c906108c 16187Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16188some target hardware may not have this support.\n\
16189\n"
16190BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16191 set_cmd_completer (c, location_completer);
c906108c 16192
1bedd215 16193 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16194Set a temporary hardware assisted breakpoint.\n\
c906108c 16195Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16196so it will be deleted when hit.\n\
16197\n"
16198BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16199 set_cmd_completer (c, location_completer);
c906108c 16200
1bedd215
AC
16201 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16202Enable some breakpoints.\n\
c906108c
SS
16203Give breakpoint numbers (separated by spaces) as arguments.\n\
16204With no subcommand, breakpoints are enabled until you command otherwise.\n\
16205This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16206With a subcommand you can enable temporarily."),
c906108c
SS
16207 &enablelist, "enable ", 1, &cmdlist);
16208 if (xdb_commands)
1bedd215
AC
16209 add_com ("ab", class_breakpoint, enable_command, _("\
16210Enable some breakpoints.\n\
c906108c
SS
16211Give breakpoint numbers (separated by spaces) as arguments.\n\
16212With no subcommand, breakpoints are enabled until you command otherwise.\n\
16213This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16214With a subcommand you can enable temporarily."));
c906108c
SS
16215
16216 add_com_alias ("en", "enable", class_breakpoint, 1);
16217
84951ab5 16218 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16219Enable some breakpoints.\n\
c906108c
SS
16220Give breakpoint numbers (separated by spaces) as arguments.\n\
16221This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16222May be abbreviated to simply \"enable\".\n"),
c5aa993b 16223 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16224
1a966eab
AC
16225 add_cmd ("once", no_class, enable_once_command, _("\
16226Enable breakpoints for one hit. Give breakpoint numbers.\n\
16227If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16228 &enablebreaklist);
16229
1a966eab
AC
16230 add_cmd ("delete", no_class, enable_delete_command, _("\
16231Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16232If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16233 &enablebreaklist);
16234
816338b5
SS
16235 add_cmd ("count", no_class, enable_count_command, _("\
16236Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16237If a breakpoint is hit while enabled in this fashion,\n\
16238the count is decremented; when it reaches zero, the breakpoint is disabled."),
16239 &enablebreaklist);
16240
1a966eab
AC
16241 add_cmd ("delete", no_class, enable_delete_command, _("\
16242Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16243If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16244 &enablelist);
16245
1a966eab
AC
16246 add_cmd ("once", no_class, enable_once_command, _("\
16247Enable breakpoints for one hit. Give breakpoint numbers.\n\
16248If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16249 &enablelist);
16250
16251 add_cmd ("count", no_class, enable_count_command, _("\
16252Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16253If a breakpoint is hit while enabled in this fashion,\n\
16254the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16255 &enablelist);
16256
1bedd215
AC
16257 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16258Disable some breakpoints.\n\
c906108c
SS
16259Arguments are breakpoint numbers with spaces in between.\n\
16260To disable all breakpoints, give no argument.\n\
64b9b334 16261A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16262 &disablelist, "disable ", 1, &cmdlist);
16263 add_com_alias ("dis", "disable", class_breakpoint, 1);
16264 add_com_alias ("disa", "disable", class_breakpoint, 1);
16265 if (xdb_commands)
1bedd215
AC
16266 add_com ("sb", class_breakpoint, disable_command, _("\
16267Disable some breakpoints.\n\
c906108c
SS
16268Arguments are breakpoint numbers with spaces in between.\n\
16269To disable all breakpoints, give no argument.\n\
64b9b334 16270A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16271
1a966eab
AC
16272 add_cmd ("breakpoints", class_alias, disable_command, _("\
16273Disable some breakpoints.\n\
c906108c
SS
16274Arguments are breakpoint numbers with spaces in between.\n\
16275To disable all breakpoints, give no argument.\n\
64b9b334 16276A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16277This command may be abbreviated \"disable\"."),
c906108c
SS
16278 &disablelist);
16279
1bedd215
AC
16280 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16281Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16282Arguments are breakpoint numbers with spaces in between.\n\
16283To delete all breakpoints, give no argument.\n\
16284\n\
16285Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16286The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16287 &deletelist, "delete ", 1, &cmdlist);
16288 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16289 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16290 if (xdb_commands)
1bedd215
AC
16291 add_com ("db", class_breakpoint, delete_command, _("\
16292Delete some breakpoints.\n\
c906108c 16293Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16294To delete all breakpoints, give no argument.\n"));
c906108c 16295
1a966eab
AC
16296 add_cmd ("breakpoints", class_alias, delete_command, _("\
16297Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16298Arguments are breakpoint numbers with spaces in between.\n\
16299To delete all breakpoints, give no argument.\n\
1a966eab 16300This command may be abbreviated \"delete\"."),
c906108c
SS
16301 &deletelist);
16302
1bedd215
AC
16303 add_com ("clear", class_breakpoint, clear_command, _("\
16304Clear breakpoint at specified line or function.\n\
c906108c
SS
16305Argument may be line number, function name, or \"*\" and an address.\n\
16306If line number is specified, all breakpoints in that line are cleared.\n\
16307If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16308If an address is specified, breakpoints at that address are cleared.\n\
16309\n\
16310With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16311is executing in.\n\
16312\n\
1bedd215 16313See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16314 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16315
1bedd215 16316 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16317Set breakpoint at specified line or function.\n"
16318BREAK_ARGS_HELP ("break")));
5ba2abeb 16319 set_cmd_completer (c, location_completer);
c94fdfd0 16320
c906108c
SS
16321 add_com_alias ("b", "break", class_run, 1);
16322 add_com_alias ("br", "break", class_run, 1);
16323 add_com_alias ("bre", "break", class_run, 1);
16324 add_com_alias ("brea", "break", class_run, 1);
16325
7681d515
PM
16326 if (xdb_commands)
16327 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16328
16329 if (dbx_commands)
16330 {
1bedd215
AC
16331 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16332Break in function/address or break at a line in the current file."),
c5aa993b
JM
16333 &stoplist, "stop ", 1, &cmdlist);
16334 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16335 _("Break in function or address."), &stoplist);
c5aa993b 16336 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16337 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16338 add_com ("status", class_info, breakpoints_info, _("\
16339Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16340The \"Type\" column indicates one of:\n\
16341\tbreakpoint - normal breakpoint\n\
16342\twatchpoint - watchpoint\n\
16343The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16344the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16345breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16346address and file/line number respectively.\n\
16347\n\
16348Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16349are set to the address of the last breakpoint listed unless the command\n\
16350is prefixed with \"server \".\n\n\
c906108c 16351Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16352breakpoint set."));
c906108c
SS
16353 }
16354
1bedd215 16355 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16356Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16357The \"Type\" column indicates one of:\n\
16358\tbreakpoint - normal breakpoint\n\
16359\twatchpoint - watchpoint\n\
16360The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16361the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16362breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16363address and file/line number respectively.\n\
16364\n\
16365Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16366are set to the address of the last breakpoint listed unless the command\n\
16367is prefixed with \"server \".\n\n\
c906108c 16368Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16369breakpoint set."));
c906108c 16370
6b04bdb7
MS
16371 add_info_alias ("b", "breakpoints", 1);
16372
c906108c 16373 if (xdb_commands)
1bedd215
AC
16374 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16375Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16376The \"Type\" column indicates one of:\n\
16377\tbreakpoint - normal breakpoint\n\
16378\twatchpoint - watchpoint\n\
16379The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16380the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16381breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16382address and file/line number respectively.\n\
16383\n\
16384Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16385are set to the address of the last breakpoint listed unless the command\n\
16386is prefixed with \"server \".\n\n\
c906108c 16387Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16388breakpoint set."));
c906108c 16389
1a966eab
AC
16390 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16391Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16392The \"Type\" column indicates one of:\n\
16393\tbreakpoint - normal breakpoint\n\
16394\twatchpoint - watchpoint\n\
16395\tlongjmp - internal breakpoint used to step through longjmp()\n\
16396\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16397\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16398\tfinish - internal breakpoint used by the \"finish\" command\n\
16399The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16400the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16401breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16402address and file/line number respectively.\n\
16403\n\
16404Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16405are set to the address of the last breakpoint listed unless the command\n\
16406is prefixed with \"server \".\n\n\
c906108c 16407Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16408breakpoint set."),
c906108c
SS
16409 &maintenanceinfolist);
16410
44feb3ce
TT
16411 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16412Set catchpoints to catch events."),
16413 &catch_cmdlist, "catch ",
16414 0/*allow-unknown*/, &cmdlist);
16415
16416 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16417Set temporary catchpoints to catch events."),
16418 &tcatch_cmdlist, "tcatch ",
16419 0/*allow-unknown*/, &cmdlist);
16420
44feb3ce
TT
16421 add_catch_command ("fork", _("Catch calls to fork."),
16422 catch_fork_command_1,
a96d9b2e 16423 NULL,
44feb3ce
TT
16424 (void *) (uintptr_t) catch_fork_permanent,
16425 (void *) (uintptr_t) catch_fork_temporary);
16426 add_catch_command ("vfork", _("Catch calls to vfork."),
16427 catch_fork_command_1,
a96d9b2e 16428 NULL,
44feb3ce
TT
16429 (void *) (uintptr_t) catch_vfork_permanent,
16430 (void *) (uintptr_t) catch_vfork_temporary);
16431 add_catch_command ("exec", _("Catch calls to exec."),
16432 catch_exec_command_1,
a96d9b2e
SDJ
16433 NULL,
16434 CATCH_PERMANENT,
16435 CATCH_TEMPORARY);
edcc5120
TT
16436 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16437Usage: catch load [REGEX]\n\
16438If REGEX is given, only stop for libraries matching the regular expression."),
16439 catch_load_command_1,
16440 NULL,
16441 CATCH_PERMANENT,
16442 CATCH_TEMPORARY);
16443 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16444Usage: catch unload [REGEX]\n\
16445If REGEX is given, only stop for libraries matching the regular expression."),
16446 catch_unload_command_1,
16447 NULL,
16448 CATCH_PERMANENT,
16449 CATCH_TEMPORARY);
a96d9b2e
SDJ
16450 add_catch_command ("syscall", _("\
16451Catch system calls by their names and/or numbers.\n\
16452Arguments say which system calls to catch. If no arguments\n\
16453are given, every system call will be caught.\n\
16454Arguments, if given, should be one or more system call names\n\
16455(if your system supports that), or system call numbers."),
16456 catch_syscall_command_1,
16457 catch_syscall_completer,
44feb3ce
TT
16458 CATCH_PERMANENT,
16459 CATCH_TEMPORARY);
c5aa993b 16460
1bedd215
AC
16461 c = add_com ("watch", class_breakpoint, watch_command, _("\
16462Set a watchpoint for an expression.\n\
06a64a0b 16463Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16464A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16465an expression changes.\n\
16466If -l or -location is given, this evaluates EXPRESSION and watches\n\
16467the memory to which it refers."));
65d12d83 16468 set_cmd_completer (c, expression_completer);
c906108c 16469
1bedd215
AC
16470 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16471Set a read watchpoint for an expression.\n\
06a64a0b 16472Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16473A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16474an expression is read.\n\
16475If -l or -location is given, this evaluates EXPRESSION and watches\n\
16476the memory to which it refers."));
65d12d83 16477 set_cmd_completer (c, expression_completer);
c906108c 16478
1bedd215
AC
16479 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16480Set a watchpoint for an expression.\n\
06a64a0b 16481Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16482A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16483an expression is either read or written.\n\
16484If -l or -location is given, this evaluates EXPRESSION and watches\n\
16485the memory to which it refers."));
65d12d83 16486 set_cmd_completer (c, expression_completer);
c906108c 16487
d77f58be 16488 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16489Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16490
920d2a44
AC
16491 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16492 respond to changes - contrary to the description. */
85c07804
AC
16493 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16494 &can_use_hw_watchpoints, _("\
16495Set debugger's willingness to use watchpoint hardware."), _("\
16496Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16497If zero, gdb will not use hardware for new watchpoints, even if\n\
16498such is available. (However, any hardware watchpoints that were\n\
16499created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16500hardware.)"),
16501 NULL,
920d2a44 16502 show_can_use_hw_watchpoints,
85c07804 16503 &setlist, &showlist);
c906108c
SS
16504
16505 can_use_hw_watchpoints = 1;
fa8d40ab 16506
1042e4c0
SS
16507 /* Tracepoint manipulation commands. */
16508
16509 c = add_com ("trace", class_breakpoint, trace_command, _("\
16510Set a tracepoint at specified line or function.\n\
16511\n"
16512BREAK_ARGS_HELP ("trace") "\n\
16513Do \"help tracepoints\" for info on other tracepoint commands."));
16514 set_cmd_completer (c, location_completer);
16515
16516 add_com_alias ("tp", "trace", class_alias, 0);
16517 add_com_alias ("tr", "trace", class_alias, 1);
16518 add_com_alias ("tra", "trace", class_alias, 1);
16519 add_com_alias ("trac", "trace", class_alias, 1);
16520
7a697b8d
SS
16521 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16522Set a fast tracepoint at specified line or function.\n\
16523\n"
16524BREAK_ARGS_HELP ("ftrace") "\n\
16525Do \"help tracepoints\" for info on other tracepoint commands."));
16526 set_cmd_completer (c, location_completer);
16527
0fb4aa4b
PA
16528 c = add_com ("strace", class_breakpoint, strace_command, _("\
16529Set a static tracepoint at specified line, function or marker.\n\
16530\n\
16531strace [LOCATION] [if CONDITION]\n\
16532LOCATION may be a line number, function name, \"*\" and an address,\n\
16533or -m MARKER_ID.\n\
16534If a line number is specified, probe the marker at start of code\n\
16535for that line. If a function is specified, probe the marker at start\n\
16536of code for that function. If an address is specified, probe the marker\n\
16537at that exact address. If a marker id is specified, probe the marker\n\
16538with that name. With no LOCATION, uses current execution address of\n\
16539the selected stack frame.\n\
16540Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16541This collects arbitrary user data passed in the probe point call to the\n\
16542tracing library. You can inspect it when analyzing the trace buffer,\n\
16543by printing the $_sdata variable like any other convenience variable.\n\
16544\n\
16545CONDITION is a boolean expression.\n\
16546\n\
d41c0fc8
PA
16547Multiple tracepoints at one place are permitted, and useful if their\n\
16548conditions are different.\n\
0fb4aa4b
PA
16549\n\
16550Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16551Do \"help tracepoints\" for info on other tracepoint commands."));
16552 set_cmd_completer (c, location_completer);
16553
1042e4c0 16554 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16555Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16556Convenience variable \"$tpnum\" contains the number of the\n\
16557last tracepoint set."));
16558
16559 add_info_alias ("tp", "tracepoints", 1);
16560
16561 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16562Delete specified tracepoints.\n\
16563Arguments are tracepoint numbers, separated by spaces.\n\
16564No argument means delete all tracepoints."),
16565 &deletelist);
7e20dfcd 16566 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16567
16568 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16569Disable specified tracepoints.\n\
16570Arguments are tracepoint numbers, separated by spaces.\n\
16571No argument means disable all tracepoints."),
16572 &disablelist);
16573 deprecate_cmd (c, "disable");
16574
16575 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16576Enable specified tracepoints.\n\
16577Arguments are tracepoint numbers, separated by spaces.\n\
16578No argument means enable all tracepoints."),
16579 &enablelist);
16580 deprecate_cmd (c, "enable");
16581
16582 add_com ("passcount", class_trace, trace_pass_command, _("\
16583Set the passcount for a tracepoint.\n\
16584The trace will end when the tracepoint has been passed 'count' times.\n\
16585Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16586if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16587
6149aea9
PA
16588 add_prefix_cmd ("save", class_breakpoint, save_command,
16589 _("Save breakpoint definitions as a script."),
16590 &save_cmdlist, "save ",
16591 0/*allow-unknown*/, &cmdlist);
16592
16593 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16594Save current breakpoint definitions as a script.\n\
cce7e648 16595This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16596catchpoints, tracepoints). Use the 'source' command in another debug\n\
16597session to restore them."),
16598 &save_cmdlist);
16599 set_cmd_completer (c, filename_completer);
16600
16601 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16602Save current tracepoint definitions as a script.\n\
6149aea9
PA
16603Use the 'source' command in another debug session to restore them."),
16604 &save_cmdlist);
1042e4c0
SS
16605 set_cmd_completer (c, filename_completer);
16606
6149aea9
PA
16607 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16608 deprecate_cmd (c, "save tracepoints");
16609
1bedd215 16610 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16611Breakpoint specific settings\n\
16612Configure various breakpoint-specific variables such as\n\
1bedd215 16613pending breakpoint behavior"),
fa8d40ab
JJ
16614 &breakpoint_set_cmdlist, "set breakpoint ",
16615 0/*allow-unknown*/, &setlist);
1bedd215 16616 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16617Breakpoint specific settings\n\
16618Configure various breakpoint-specific variables such as\n\
1bedd215 16619pending breakpoint behavior"),
fa8d40ab
JJ
16620 &breakpoint_show_cmdlist, "show breakpoint ",
16621 0/*allow-unknown*/, &showlist);
16622
7915a72c
AC
16623 add_setshow_auto_boolean_cmd ("pending", no_class,
16624 &pending_break_support, _("\
16625Set debugger's behavior regarding pending breakpoints."), _("\
16626Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16627If on, an unrecognized breakpoint location will cause gdb to create a\n\
16628pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16629an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16630user-query to see if a pending breakpoint should be created."),
2c5b56ce 16631 NULL,
920d2a44 16632 show_pending_break_support,
6e1d7d6c
AC
16633 &breakpoint_set_cmdlist,
16634 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16635
16636 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16637
16638 add_setshow_boolean_cmd ("auto-hw", no_class,
16639 &automatic_hardware_breakpoints, _("\
16640Set automatic usage of hardware breakpoints."), _("\
16641Show automatic usage of hardware breakpoints."), _("\
16642If set, the debugger will automatically use hardware breakpoints for\n\
16643breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16644a warning will be emitted for such breakpoints."),
16645 NULL,
16646 show_automatic_hardware_breakpoints,
16647 &breakpoint_set_cmdlist,
16648 &breakpoint_show_cmdlist);
74960c60 16649
72d0e2c5
YQ
16650 add_setshow_auto_boolean_cmd ("always-inserted", class_support,
16651 &always_inserted_mode, _("\
74960c60
VP
16652Set mode for inserting breakpoints."), _("\
16653Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
16654When this mode is off, breakpoints are inserted in inferior when it is\n\
16655resumed, and removed when execution stops. When this mode is on,\n\
16656breakpoints are inserted immediately and removed only when the user\n\
16657deletes the breakpoint. When this mode is auto (which is the default),\n\
16658the behaviour depends on the non-stop setting (see help set non-stop).\n\
16659In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16660behaves as if always-inserted mode is on; if gdb is controlling the\n\
16661inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
72d0e2c5
YQ
16662 NULL,
16663 &show_always_inserted_mode,
16664 &breakpoint_set_cmdlist,
16665 &breakpoint_show_cmdlist);
f1310107 16666
b775012e
LM
16667 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16668 condition_evaluation_enums,
16669 &condition_evaluation_mode_1, _("\
16670Set mode of breakpoint condition evaluation."), _("\
16671Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16672When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16673evaluated on the host's side by GDB. When it is set to \"target\",\n\
16674breakpoint conditions will be downloaded to the target (if the target\n\
16675supports such feature) and conditions will be evaluated on the target's side.\n\
16676If this is set to \"auto\" (default), this will be automatically set to\n\
16677\"target\" if it supports condition evaluation, otherwise it will\n\
16678be set to \"gdb\""),
16679 &set_condition_evaluation_mode,
16680 &show_condition_evaluation_mode,
16681 &breakpoint_set_cmdlist,
16682 &breakpoint_show_cmdlist);
16683
f1310107
TJB
16684 add_com ("break-range", class_breakpoint, break_range_command, _("\
16685Set a breakpoint for an address range.\n\
16686break-range START-LOCATION, END-LOCATION\n\
16687where START-LOCATION and END-LOCATION can be one of the following:\n\
16688 LINENUM, for that line in the current file,\n\
16689 FILE:LINENUM, for that line in that file,\n\
16690 +OFFSET, for that number of lines after the current line\n\
16691 or the start of the range\n\
16692 FUNCTION, for the first line in that function,\n\
16693 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16694 *ADDRESS, for the instruction at that address.\n\
16695\n\
16696The breakpoint will stop execution of the inferior whenever it executes\n\
16697an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16698range (including START-LOCATION and END-LOCATION)."));
16699
e7e0cddf
SS
16700 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16701Set a dynamic printf at specified line or function.\n\
16702dprintf location,format string,arg1,arg2,...\n\
16703location may be a line number, function name, or \"*\" and an address.\n\
16704If a line number is specified, break at start of code for that line.\n\
0e4777df 16705If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16706 set_cmd_completer (c, location_completer);
16707
16708 add_setshow_enum_cmd ("dprintf-style", class_support,
16709 dprintf_style_enums, &dprintf_style, _("\
16710Set the style of usage for dynamic printf."), _("\
16711Show the style of usage for dynamic printf."), _("\
16712This setting chooses how GDB will do a dynamic printf.\n\
16713If the value is \"gdb\", then the printing is done by GDB to its own\n\
16714console, as with the \"printf\" command.\n\
16715If the value is \"call\", the print is done by calling a function in your\n\
16716program; by default printf(), but you can choose a different function or\n\
16717output stream by setting dprintf-function and dprintf-channel."),
16718 update_dprintf_commands, NULL,
16719 &setlist, &showlist);
16720
16721 dprintf_function = xstrdup ("printf");
16722 add_setshow_string_cmd ("dprintf-function", class_support,
16723 &dprintf_function, _("\
16724Set the function to use for dynamic printf"), _("\
16725Show the function to use for dynamic printf"), NULL,
16726 update_dprintf_commands, NULL,
16727 &setlist, &showlist);
16728
16729 dprintf_channel = xstrdup ("");
16730 add_setshow_string_cmd ("dprintf-channel", class_support,
16731 &dprintf_channel, _("\
16732Set the channel to use for dynamic printf"), _("\
16733Show the channel to use for dynamic printf"), NULL,
16734 update_dprintf_commands, NULL,
16735 &setlist, &showlist);
16736
d3ce09f5
SS
16737 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16738 &disconnected_dprintf, _("\
16739Set whether dprintf continues after GDB disconnects."), _("\
16740Show whether dprintf continues after GDB disconnects."), _("\
16741Use this to let dprintf commands continue to hit and produce output\n\
16742even if GDB disconnects or detaches from the target."),
16743 NULL,
16744 NULL,
16745 &setlist, &showlist);
16746
16747 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16748agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16749(target agent only) This is useful for formatted output in user-defined commands."));
16750
765dc015 16751 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16752
16753 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16754 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16755}
This page took 3.036115 seconds and 4 git commands to generate.