fix incorrect comments in minsyms.h
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
ecd75fc8 3 Copyright (C) 1986-2014 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
45741a9c 35#include "infrun.h"
c906108c
SS
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
0e9f083f 39#include <string.h>
50f182aa 40#include "gdb-demangle.h"
0ba1096a 41#include "filenames.h"
c906108c
SS
42#include "annotate.h"
43#include "symfile.h"
44#include "objfiles.h"
0378c332 45#include "source.h"
c5f0f3d0 46#include "linespec.h"
c94fdfd0 47#include "completer.h"
5b7f31a4 48#include "gdb.h"
8b93c638 49#include "ui-out.h"
e1507482 50#include "cli/cli-script.h"
0225421b 51#include "gdb_assert.h"
fe898f56 52#include "block.h"
a77053c2 53#include "solib.h"
84acb35a
JJ
54#include "solist.h"
55#include "observer.h"
60250e8b 56#include "exceptions.h"
765dc015 57#include "memattr.h"
f7f9143b 58#include "ada-lang.h"
d1aa2f50 59#include "top.h"
79a45b7d 60#include "valprint.h"
4efc6507 61#include "jit.h"
a96d9b2e 62#include "xml-syscall.h"
65d79d4b 63#include "parser-defs.h"
55aa24fb
SDJ
64#include "gdb_regex.h"
65#include "probe.h"
e9cafbcc 66#include "cli/cli-utils.h"
be34f849 67#include "continuations.h"
1bfeeb0f
JL
68#include "stack.h"
69#include "skip.h"
b775012e 70#include "ax-gdb.h"
e2e4d78b 71#include "dummy-frame.h"
c906108c 72
d3ce09f5
SS
73#include "format.h"
74
1042e4c0
SS
75/* readline include files */
76#include "readline/readline.h"
77#include "readline/history.h"
78
79/* readline defines this. */
80#undef savestring
81
034dad6f 82#include "mi/mi-common.h"
6dddc817 83#include "extension.h"
104c1213 84
e7e8980f
YQ
85/* Enums for exception-handling support. */
86enum exception_event_kind
87{
88 EX_EVENT_THROW,
591f19e8 89 EX_EVENT_RETHROW,
e7e8980f
YQ
90 EX_EVENT_CATCH
91};
92
4a64f543 93/* Prototypes for local functions. */
c906108c 94
a14ed312 95static void enable_delete_command (char *, int);
c906108c 96
a14ed312 97static void enable_once_command (char *, int);
c906108c 98
816338b5
SS
99static void enable_count_command (char *, int);
100
a14ed312 101static void disable_command (char *, int);
c906108c 102
a14ed312 103static void enable_command (char *, int);
c906108c 104
95a42b64
TT
105static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
106 void *),
107 void *);
c906108c 108
a14ed312 109static void ignore_command (char *, int);
c906108c 110
4efb68b1 111static int breakpoint_re_set_one (void *);
c906108c 112
348d480f
PA
113static void breakpoint_re_set_default (struct breakpoint *);
114
983af33b
SDJ
115static void create_sals_from_address_default (char **,
116 struct linespec_result *,
117 enum bptype, char *,
118 char **);
119
120static void create_breakpoints_sal_default (struct gdbarch *,
121 struct linespec_result *,
e7e0cddf 122 char *, char *, enum bptype,
983af33b
SDJ
123 enum bpdisp, int, int,
124 int,
125 const struct breakpoint_ops *,
44f238bb 126 int, int, int, unsigned);
983af33b
SDJ
127
128static void decode_linespec_default (struct breakpoint *, char **,
129 struct symtabs_and_lines *);
130
a14ed312 131static void clear_command (char *, int);
c906108c 132
a14ed312 133static void catch_command (char *, int);
c906108c 134
a9634178 135static int can_use_hardware_watchpoint (struct value *);
c906108c 136
98deb0da 137static void break_command_1 (char *, int, int);
c906108c 138
a14ed312 139static void mention (struct breakpoint *);
c906108c 140
348d480f
PA
141static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
142 enum bptype,
c0a91b2b 143 const struct breakpoint_ops *);
3742cc8b
YQ
144static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
145 const struct symtab_and_line *);
146
4a64f543
MS
147/* This function is used in gdbtk sources and thus can not be made
148 static. */
63c252f8 149struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 150 struct symtab_and_line,
c0a91b2b
TT
151 enum bptype,
152 const struct breakpoint_ops *);
c906108c 153
06edf0c0
PA
154static struct breakpoint *
155 momentary_breakpoint_from_master (struct breakpoint *orig,
156 enum bptype type,
c0a91b2b 157 const struct breakpoint_ops *ops);
06edf0c0 158
76897487
KB
159static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
160
a6d9a66e
UW
161static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
162 CORE_ADDR bpaddr,
88f7da05 163 enum bptype bptype);
76897487 164
6c95b8df
PA
165static void describe_other_breakpoints (struct gdbarch *,
166 struct program_space *, CORE_ADDR,
5af949e3 167 struct obj_section *, int);
c906108c 168
85d721b8
PA
169static int watchpoint_locations_match (struct bp_location *loc1,
170 struct bp_location *loc2);
171
f1310107
TJB
172static int breakpoint_location_address_match (struct bp_location *bl,
173 struct address_space *aspace,
174 CORE_ADDR addr);
175
a14ed312 176static void breakpoints_info (char *, int);
c906108c 177
d77f58be
SS
178static void watchpoints_info (char *, int);
179
e5a67952
MS
180static int breakpoint_1 (char *, int,
181 int (*) (const struct breakpoint *));
c906108c 182
4efb68b1 183static int breakpoint_cond_eval (void *);
c906108c 184
4efb68b1 185static void cleanup_executing_breakpoints (void *);
c906108c 186
a14ed312 187static void commands_command (char *, int);
c906108c 188
a14ed312 189static void condition_command (char *, int);
c906108c 190
c5aa993b
JM
191typedef enum
192 {
193 mark_inserted,
194 mark_uninserted
195 }
196insertion_state_t;
c906108c 197
0bde7532 198static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 199static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 200
e514a9d6 201static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 202
4efb68b1 203static int watchpoint_check (void *);
c906108c 204
a14ed312 205static void maintenance_info_breakpoints (char *, int);
c906108c 206
a14ed312 207static int hw_breakpoint_used_count (void);
c906108c 208
a1398e0c
PA
209static int hw_watchpoint_use_count (struct breakpoint *);
210
211static int hw_watchpoint_used_count_others (struct breakpoint *except,
212 enum bptype type,
213 int *other_type_used);
c906108c 214
a14ed312 215static void hbreak_command (char *, int);
c906108c 216
a14ed312 217static void thbreak_command (char *, int);
c906108c 218
816338b5
SS
219static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
220 int count);
c906108c 221
a14ed312 222static void stop_command (char *arg, int from_tty);
7a292a7a 223
a14ed312 224static void stopin_command (char *arg, int from_tty);
7a292a7a 225
a14ed312 226static void stopat_command (char *arg, int from_tty);
7a292a7a 227
a14ed312 228static void tcatch_command (char *arg, int from_tty);
7a292a7a 229
d03285ec
UW
230static void detach_single_step_breakpoints (void);
231
ef370185
JB
232static int find_single_step_breakpoint (struct address_space *aspace,
233 CORE_ADDR pc);
234
fe3f5fa8 235static void free_bp_location (struct bp_location *loc);
f431efe5
PA
236static void incref_bp_location (struct bp_location *loc);
237static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 238
39d61571 239static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 240
b60e7edf 241static void update_global_location_list (int);
a5606eee 242
b60e7edf 243static void update_global_location_list_nothrow (int);
74960c60 244
d77f58be 245static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
246
247static void insert_breakpoint_locations (void);
a5606eee 248
a96d9b2e
SDJ
249static int syscall_catchpoint_p (struct breakpoint *b);
250
1042e4c0
SS
251static void tracepoints_info (char *, int);
252
253static void delete_trace_command (char *, int);
254
255static void enable_trace_command (char *, int);
256
257static void disable_trace_command (char *, int);
258
259static void trace_pass_command (char *, int);
260
558a9d82
YQ
261static void set_tracepoint_count (int num);
262
9c06b0b4
TJB
263static int is_masked_watchpoint (const struct breakpoint *b);
264
b775012e
LM
265static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
266
983af33b
SDJ
267/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
268 otherwise. */
269
270static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 271
2060206e
PA
272/* The abstract base class all breakpoint_ops structures inherit
273 from. */
ab04a2af 274struct breakpoint_ops base_breakpoint_ops;
2060206e
PA
275
276/* The breakpoint_ops structure to be inherited by all breakpoint_ops
277 that are implemented on top of software or hardware breakpoints
278 (user breakpoints, internal and momentary breakpoints, etc.). */
279static struct breakpoint_ops bkpt_base_breakpoint_ops;
280
281/* Internal breakpoints class type. */
06edf0c0 282static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
283
284/* Momentary breakpoints class type. */
06edf0c0
PA
285static struct breakpoint_ops momentary_breakpoint_ops;
286
e2e4d78b
JK
287/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
288static struct breakpoint_ops longjmp_breakpoint_ops;
289
2060206e
PA
290/* The breakpoint_ops structure to be used in regular user created
291 breakpoints. */
292struct breakpoint_ops bkpt_breakpoint_ops;
293
55aa24fb
SDJ
294/* Breakpoints set on probes. */
295static struct breakpoint_ops bkpt_probe_breakpoint_ops;
296
e7e0cddf 297/* Dynamic printf class type. */
c5867ab6 298struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 299
9d497a19
PA
300/* One (or perhaps two) breakpoints used for software single
301 stepping. */
302
303static void *single_step_breakpoints[2];
304static struct gdbarch *single_step_gdbarch[2];
305
d3ce09f5
SS
306/* The style in which to perform a dynamic printf. This is a user
307 option because different output options have different tradeoffs;
308 if GDB does the printing, there is better error handling if there
309 is a problem with any of the arguments, but using an inferior
310 function lets you have special-purpose printers and sending of
311 output to the same place as compiled-in print functions. */
312
313static const char dprintf_style_gdb[] = "gdb";
314static const char dprintf_style_call[] = "call";
315static const char dprintf_style_agent[] = "agent";
316static const char *const dprintf_style_enums[] = {
317 dprintf_style_gdb,
318 dprintf_style_call,
319 dprintf_style_agent,
320 NULL
321};
322static const char *dprintf_style = dprintf_style_gdb;
323
324/* The function to use for dynamic printf if the preferred style is to
325 call into the inferior. The value is simply a string that is
326 copied into the command, so it can be anything that GDB can
327 evaluate to a callable address, not necessarily a function name. */
328
329static char *dprintf_function = "";
330
331/* The channel to use for dynamic printf if the preferred style is to
332 call into the inferior; if a nonempty string, it will be passed to
333 the call as the first argument, with the format string as the
334 second. As with the dprintf function, this can be anything that
335 GDB knows how to evaluate, so in addition to common choices like
336 "stderr", this could be an app-specific expression like
337 "mystreams[curlogger]". */
338
339static char *dprintf_channel = "";
340
341/* True if dprintf commands should continue to operate even if GDB
342 has disconnected. */
343static int disconnected_dprintf = 1;
344
5cea2a26
PA
345/* A reference-counted struct command_line. This lets multiple
346 breakpoints share a single command list. */
347struct counted_command_line
348{
349 /* The reference count. */
350 int refc;
351
352 /* The command list. */
353 struct command_line *commands;
354};
355
356struct command_line *
357breakpoint_commands (struct breakpoint *b)
358{
359 return b->commands ? b->commands->commands : NULL;
360}
3daf8fe5 361
f3b1572e
PA
362/* Flag indicating that a command has proceeded the inferior past the
363 current breakpoint. */
364
365static int breakpoint_proceeded;
366
956a9fb9 367const char *
2cec12e5
AR
368bpdisp_text (enum bpdisp disp)
369{
4a64f543
MS
370 /* NOTE: the following values are a part of MI protocol and
371 represent values of 'disp' field returned when inferior stops at
372 a breakpoint. */
bc043ef3 373 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 374
2cec12e5
AR
375 return bpdisps[(int) disp];
376}
c906108c 377
4a64f543 378/* Prototypes for exported functions. */
c906108c 379/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 380 if such is available. */
c906108c
SS
381static int can_use_hw_watchpoints;
382
920d2a44
AC
383static void
384show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
385 struct cmd_list_element *c,
386 const char *value)
387{
3e43a32a
MS
388 fprintf_filtered (file,
389 _("Debugger's willingness to use "
390 "watchpoint hardware is %s.\n"),
920d2a44
AC
391 value);
392}
393
fa8d40ab
JJ
394/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
395 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 396 for unrecognized breakpoint locations.
fa8d40ab
JJ
397 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
398static enum auto_boolean pending_break_support;
920d2a44
AC
399static void
400show_pending_break_support (struct ui_file *file, int from_tty,
401 struct cmd_list_element *c,
402 const char *value)
403{
3e43a32a
MS
404 fprintf_filtered (file,
405 _("Debugger's behavior regarding "
406 "pending breakpoints is %s.\n"),
920d2a44
AC
407 value);
408}
fa8d40ab 409
765dc015 410/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 411 set with "break" but falling in read-only memory.
765dc015
VP
412 If 0, gdb will warn about such breakpoints, but won't automatically
413 use hardware breakpoints. */
414static int automatic_hardware_breakpoints;
415static void
416show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
417 struct cmd_list_element *c,
418 const char *value)
419{
3e43a32a
MS
420 fprintf_filtered (file,
421 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
422 value);
423}
424
33e5cbd6
PA
425/* If on, gdb will keep breakpoints inserted even as inferior is
426 stopped, and immediately insert any new breakpoints. If off, gdb
427 will insert breakpoints into inferior only when resuming it, and
428 will remove breakpoints upon stop. If auto, GDB will behave as ON
429 if in non-stop mode, and as OFF if all-stop mode.*/
430
72d0e2c5
YQ
431static enum auto_boolean always_inserted_mode = AUTO_BOOLEAN_AUTO;
432
33e5cbd6 433static void
74960c60 434show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 435 struct cmd_list_element *c, const char *value)
74960c60 436{
72d0e2c5 437 if (always_inserted_mode == AUTO_BOOLEAN_AUTO)
3e43a32a
MS
438 fprintf_filtered (file,
439 _("Always inserted breakpoint "
440 "mode is %s (currently %s).\n"),
33e5cbd6
PA
441 value,
442 breakpoints_always_inserted_mode () ? "on" : "off");
443 else
3e43a32a
MS
444 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
445 value);
74960c60
VP
446}
447
33e5cbd6
PA
448int
449breakpoints_always_inserted_mode (void)
450{
72d0e2c5
YQ
451 return (always_inserted_mode == AUTO_BOOLEAN_TRUE
452 || (always_inserted_mode == AUTO_BOOLEAN_AUTO && non_stop));
33e5cbd6 453}
765dc015 454
b775012e
LM
455static const char condition_evaluation_both[] = "host or target";
456
457/* Modes for breakpoint condition evaluation. */
458static const char condition_evaluation_auto[] = "auto";
459static const char condition_evaluation_host[] = "host";
460static const char condition_evaluation_target[] = "target";
461static const char *const condition_evaluation_enums[] = {
462 condition_evaluation_auto,
463 condition_evaluation_host,
464 condition_evaluation_target,
465 NULL
466};
467
468/* Global that holds the current mode for breakpoint condition evaluation. */
469static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
470
471/* Global that we use to display information to the user (gets its value from
472 condition_evaluation_mode_1. */
473static const char *condition_evaluation_mode = condition_evaluation_auto;
474
475/* Translate a condition evaluation mode MODE into either "host"
476 or "target". This is used mostly to translate from "auto" to the
477 real setting that is being used. It returns the translated
478 evaluation mode. */
479
480static const char *
481translate_condition_evaluation_mode (const char *mode)
482{
483 if (mode == condition_evaluation_auto)
484 {
485 if (target_supports_evaluation_of_breakpoint_conditions ())
486 return condition_evaluation_target;
487 else
488 return condition_evaluation_host;
489 }
490 else
491 return mode;
492}
493
494/* Discovers what condition_evaluation_auto translates to. */
495
496static const char *
497breakpoint_condition_evaluation_mode (void)
498{
499 return translate_condition_evaluation_mode (condition_evaluation_mode);
500}
501
502/* Return true if GDB should evaluate breakpoint conditions or false
503 otherwise. */
504
505static int
506gdb_evaluates_breakpoint_condition_p (void)
507{
508 const char *mode = breakpoint_condition_evaluation_mode ();
509
510 return (mode == condition_evaluation_host);
511}
512
a14ed312 513void _initialize_breakpoint (void);
c906108c 514
c906108c
SS
515/* Are we executing breakpoint commands? */
516static int executing_breakpoint_commands;
517
c02f5703
MS
518/* Are overlay event breakpoints enabled? */
519static int overlay_events_enabled;
520
e09342b5
TJB
521/* See description in breakpoint.h. */
522int target_exact_watchpoints = 0;
523
c906108c 524/* Walk the following statement or block through all breakpoints.
e5dd4106 525 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 526 current breakpoint. */
c906108c 527
5c44784c 528#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 529
5c44784c
JM
530#define ALL_BREAKPOINTS_SAFE(B,TMP) \
531 for (B = breakpoint_chain; \
532 B ? (TMP=B->next, 1): 0; \
533 B = TMP)
c906108c 534
4a64f543
MS
535/* Similar iterator for the low-level breakpoints. SAFE variant is
536 not provided so update_global_location_list must not be called
537 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 538
876fa593
JK
539#define ALL_BP_LOCATIONS(B,BP_TMP) \
540 for (BP_TMP = bp_location; \
541 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
542 BP_TMP++)
7cc221ef 543
b775012e
LM
544/* Iterates through locations with address ADDRESS for the currently selected
545 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
546 to where the loop should start from.
547 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
548 appropriate location to start with. */
549
550#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
551 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
552 BP_LOCP_TMP = BP_LOCP_START; \
553 BP_LOCP_START \
554 && (BP_LOCP_TMP < bp_location + bp_location_count \
555 && (*BP_LOCP_TMP)->address == ADDRESS); \
556 BP_LOCP_TMP++)
557
1042e4c0
SS
558/* Iterator for tracepoints only. */
559
560#define ALL_TRACEPOINTS(B) \
561 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 562 if (is_tracepoint (B))
1042e4c0 563
7cc221ef 564/* Chains of all breakpoints defined. */
c906108c
SS
565
566struct breakpoint *breakpoint_chain;
567
876fa593
JK
568/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
569
570static struct bp_location **bp_location;
571
572/* Number of elements of BP_LOCATION. */
573
574static unsigned bp_location_count;
575
4a64f543
MS
576/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
577 ADDRESS for the current elements of BP_LOCATION which get a valid
578 result from bp_location_has_shadow. You can use it for roughly
579 limiting the subrange of BP_LOCATION to scan for shadow bytes for
580 an address you need to read. */
876fa593
JK
581
582static CORE_ADDR bp_location_placed_address_before_address_max;
583
4a64f543
MS
584/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
585 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
586 BP_LOCATION which get a valid result from bp_location_has_shadow.
587 You can use it for roughly limiting the subrange of BP_LOCATION to
588 scan for shadow bytes for an address you need to read. */
876fa593
JK
589
590static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 591
4a64f543
MS
592/* The locations that no longer correspond to any breakpoint, unlinked
593 from bp_location array, but for which a hit may still be reported
594 by a target. */
20874c92
VP
595VEC(bp_location_p) *moribund_locations = NULL;
596
c906108c
SS
597/* Number of last breakpoint made. */
598
95a42b64
TT
599static int breakpoint_count;
600
86b17b60
PA
601/* The value of `breakpoint_count' before the last command that
602 created breakpoints. If the last (break-like) command created more
603 than one breakpoint, then the difference between BREAKPOINT_COUNT
604 and PREV_BREAKPOINT_COUNT is more than one. */
605static int prev_breakpoint_count;
c906108c 606
1042e4c0
SS
607/* Number of last tracepoint made. */
608
95a42b64 609static int tracepoint_count;
1042e4c0 610
6149aea9
PA
611static struct cmd_list_element *breakpoint_set_cmdlist;
612static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 613struct cmd_list_element *save_cmdlist;
6149aea9 614
468d015d
JJ
615/* Return whether a breakpoint is an active enabled breakpoint. */
616static int
617breakpoint_enabled (struct breakpoint *b)
618{
0d381245 619 return (b->enable_state == bp_enabled);
468d015d
JJ
620}
621
c906108c
SS
622/* Set breakpoint count to NUM. */
623
95a42b64 624static void
fba45db2 625set_breakpoint_count (int num)
c906108c 626{
86b17b60 627 prev_breakpoint_count = breakpoint_count;
c906108c 628 breakpoint_count = num;
4fa62494 629 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
630}
631
86b17b60
PA
632/* Used by `start_rbreak_breakpoints' below, to record the current
633 breakpoint count before "rbreak" creates any breakpoint. */
634static int rbreak_start_breakpoint_count;
635
95a42b64
TT
636/* Called at the start an "rbreak" command to record the first
637 breakpoint made. */
86b17b60 638
95a42b64
TT
639void
640start_rbreak_breakpoints (void)
641{
86b17b60 642 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
643}
644
645/* Called at the end of an "rbreak" command to record the last
646 breakpoint made. */
86b17b60 647
95a42b64
TT
648void
649end_rbreak_breakpoints (void)
650{
86b17b60 651 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
652}
653
4a64f543 654/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
655
656void
fba45db2 657clear_breakpoint_hit_counts (void)
c906108c
SS
658{
659 struct breakpoint *b;
660
661 ALL_BREAKPOINTS (b)
662 b->hit_count = 0;
663}
664
9add0f1b
TT
665/* Allocate a new counted_command_line with reference count of 1.
666 The new structure owns COMMANDS. */
667
668static struct counted_command_line *
669alloc_counted_command_line (struct command_line *commands)
670{
671 struct counted_command_line *result
672 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 673
9add0f1b
TT
674 result->refc = 1;
675 result->commands = commands;
676 return result;
677}
678
679/* Increment reference count. This does nothing if CMD is NULL. */
680
681static void
682incref_counted_command_line (struct counted_command_line *cmd)
683{
684 if (cmd)
685 ++cmd->refc;
686}
687
688/* Decrement reference count. If the reference count reaches 0,
689 destroy the counted_command_line. Sets *CMDP to NULL. This does
690 nothing if *CMDP is NULL. */
691
692static void
693decref_counted_command_line (struct counted_command_line **cmdp)
694{
695 if (*cmdp)
696 {
697 if (--(*cmdp)->refc == 0)
698 {
699 free_command_lines (&(*cmdp)->commands);
700 xfree (*cmdp);
701 }
702 *cmdp = NULL;
703 }
704}
705
706/* A cleanup function that calls decref_counted_command_line. */
707
708static void
709do_cleanup_counted_command_line (void *arg)
710{
711 decref_counted_command_line (arg);
712}
713
714/* Create a cleanup that calls decref_counted_command_line on the
715 argument. */
716
717static struct cleanup *
718make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
719{
720 return make_cleanup (do_cleanup_counted_command_line, cmdp);
721}
722
c906108c 723\f
48cb2d85
VP
724/* Return the breakpoint with the specified number, or NULL
725 if the number does not refer to an existing breakpoint. */
726
727struct breakpoint *
728get_breakpoint (int num)
729{
730 struct breakpoint *b;
731
732 ALL_BREAKPOINTS (b)
733 if (b->number == num)
734 return b;
735
736 return NULL;
737}
5c44784c 738
c906108c 739\f
adc36818 740
b775012e
LM
741/* Mark locations as "conditions have changed" in case the target supports
742 evaluating conditions on its side. */
743
744static void
745mark_breakpoint_modified (struct breakpoint *b)
746{
747 struct bp_location *loc;
748
749 /* This is only meaningful if the target is
750 evaluating conditions and if the user has
751 opted for condition evaluation on the target's
752 side. */
753 if (gdb_evaluates_breakpoint_condition_p ()
754 || !target_supports_evaluation_of_breakpoint_conditions ())
755 return;
756
757 if (!is_breakpoint (b))
758 return;
759
760 for (loc = b->loc; loc; loc = loc->next)
761 loc->condition_changed = condition_modified;
762}
763
764/* Mark location as "conditions have changed" in case the target supports
765 evaluating conditions on its side. */
766
767static void
768mark_breakpoint_location_modified (struct bp_location *loc)
769{
770 /* This is only meaningful if the target is
771 evaluating conditions and if the user has
772 opted for condition evaluation on the target's
773 side. */
774 if (gdb_evaluates_breakpoint_condition_p ()
775 || !target_supports_evaluation_of_breakpoint_conditions ())
776
777 return;
778
779 if (!is_breakpoint (loc->owner))
780 return;
781
782 loc->condition_changed = condition_modified;
783}
784
785/* Sets the condition-evaluation mode using the static global
786 condition_evaluation_mode. */
787
788static void
789set_condition_evaluation_mode (char *args, int from_tty,
790 struct cmd_list_element *c)
791{
b775012e
LM
792 const char *old_mode, *new_mode;
793
794 if ((condition_evaluation_mode_1 == condition_evaluation_target)
795 && !target_supports_evaluation_of_breakpoint_conditions ())
796 {
797 condition_evaluation_mode_1 = condition_evaluation_mode;
798 warning (_("Target does not support breakpoint condition evaluation.\n"
799 "Using host evaluation mode instead."));
800 return;
801 }
802
803 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
804 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
805
abf1152a
JK
806 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
807 settings was "auto". */
808 condition_evaluation_mode = condition_evaluation_mode_1;
809
b775012e
LM
810 /* Only update the mode if the user picked a different one. */
811 if (new_mode != old_mode)
812 {
813 struct bp_location *loc, **loc_tmp;
814 /* If the user switched to a different evaluation mode, we
815 need to synch the changes with the target as follows:
816
817 "host" -> "target": Send all (valid) conditions to the target.
818 "target" -> "host": Remove all the conditions from the target.
819 */
820
b775012e
LM
821 if (new_mode == condition_evaluation_target)
822 {
823 /* Mark everything modified and synch conditions with the
824 target. */
825 ALL_BP_LOCATIONS (loc, loc_tmp)
826 mark_breakpoint_location_modified (loc);
827 }
828 else
829 {
830 /* Manually mark non-duplicate locations to synch conditions
831 with the target. We do this to remove all the conditions the
832 target knows about. */
833 ALL_BP_LOCATIONS (loc, loc_tmp)
834 if (is_breakpoint (loc->owner) && loc->inserted)
835 loc->needs_update = 1;
836 }
837
838 /* Do the update. */
839 update_global_location_list (1);
840 }
841
842 return;
843}
844
845/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
846 what "auto" is translating to. */
847
848static void
849show_condition_evaluation_mode (struct ui_file *file, int from_tty,
850 struct cmd_list_element *c, const char *value)
851{
852 if (condition_evaluation_mode == condition_evaluation_auto)
853 fprintf_filtered (file,
854 _("Breakpoint condition evaluation "
855 "mode is %s (currently %s).\n"),
856 value,
857 breakpoint_condition_evaluation_mode ());
858 else
859 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
860 value);
861}
862
863/* A comparison function for bp_location AP and BP that is used by
864 bsearch. This comparison function only cares about addresses, unlike
865 the more general bp_location_compare function. */
866
867static int
868bp_location_compare_addrs (const void *ap, const void *bp)
869{
870 struct bp_location *a = *(void **) ap;
871 struct bp_location *b = *(void **) bp;
872
873 if (a->address == b->address)
874 return 0;
875 else
876 return ((a->address > b->address) - (a->address < b->address));
877}
878
879/* Helper function to skip all bp_locations with addresses
880 less than ADDRESS. It returns the first bp_location that
881 is greater than or equal to ADDRESS. If none is found, just
882 return NULL. */
883
884static struct bp_location **
885get_first_locp_gte_addr (CORE_ADDR address)
886{
887 struct bp_location dummy_loc;
888 struct bp_location *dummy_locp = &dummy_loc;
889 struct bp_location **locp_found = NULL;
890
891 /* Initialize the dummy location's address field. */
892 memset (&dummy_loc, 0, sizeof (struct bp_location));
893 dummy_loc.address = address;
894
895 /* Find a close match to the first location at ADDRESS. */
896 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
897 sizeof (struct bp_location **),
898 bp_location_compare_addrs);
899
900 /* Nothing was found, nothing left to do. */
901 if (locp_found == NULL)
902 return NULL;
903
904 /* We may have found a location that is at ADDRESS but is not the first in the
905 location's list. Go backwards (if possible) and locate the first one. */
906 while ((locp_found - 1) >= bp_location
907 && (*(locp_found - 1))->address == address)
908 locp_found--;
909
910 return locp_found;
911}
912
adc36818
PM
913void
914set_breakpoint_condition (struct breakpoint *b, char *exp,
915 int from_tty)
916{
3a5c3e22
PA
917 xfree (b->cond_string);
918 b->cond_string = NULL;
adc36818 919
3a5c3e22 920 if (is_watchpoint (b))
adc36818 921 {
3a5c3e22
PA
922 struct watchpoint *w = (struct watchpoint *) b;
923
924 xfree (w->cond_exp);
925 w->cond_exp = NULL;
926 }
927 else
928 {
929 struct bp_location *loc;
930
931 for (loc = b->loc; loc; loc = loc->next)
932 {
933 xfree (loc->cond);
934 loc->cond = NULL;
b775012e
LM
935
936 /* No need to free the condition agent expression
937 bytecode (if we have one). We will handle this
938 when we go through update_global_location_list. */
3a5c3e22 939 }
adc36818 940 }
adc36818
PM
941
942 if (*exp == 0)
943 {
944 if (from_tty)
945 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
946 }
947 else
948 {
bbc13ae3 949 const char *arg = exp;
cc59ec59 950
adc36818
PM
951 /* I don't know if it matters whether this is the string the user
952 typed in or the decompiled expression. */
953 b->cond_string = xstrdup (arg);
954 b->condition_not_parsed = 0;
955
956 if (is_watchpoint (b))
957 {
3a5c3e22
PA
958 struct watchpoint *w = (struct watchpoint *) b;
959
adc36818
PM
960 innermost_block = NULL;
961 arg = exp;
1bb9788d 962 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
963 if (*arg)
964 error (_("Junk at end of expression"));
3a5c3e22 965 w->cond_exp_valid_block = innermost_block;
adc36818
PM
966 }
967 else
968 {
3a5c3e22
PA
969 struct bp_location *loc;
970
adc36818
PM
971 for (loc = b->loc; loc; loc = loc->next)
972 {
973 arg = exp;
974 loc->cond =
1bb9788d
TT
975 parse_exp_1 (&arg, loc->address,
976 block_for_pc (loc->address), 0);
adc36818
PM
977 if (*arg)
978 error (_("Junk at end of expression"));
979 }
980 }
981 }
b775012e
LM
982 mark_breakpoint_modified (b);
983
8d3788bd 984 observer_notify_breakpoint_modified (b);
adc36818
PM
985}
986
d55637df
TT
987/* Completion for the "condition" command. */
988
989static VEC (char_ptr) *
6f937416
PA
990condition_completer (struct cmd_list_element *cmd,
991 const char *text, const char *word)
d55637df 992{
6f937416 993 const char *space;
d55637df 994
6f937416
PA
995 text = skip_spaces_const (text);
996 space = skip_to_space_const (text);
d55637df
TT
997 if (*space == '\0')
998 {
999 int len;
1000 struct breakpoint *b;
1001 VEC (char_ptr) *result = NULL;
1002
1003 if (text[0] == '$')
1004 {
1005 /* We don't support completion of history indices. */
1006 if (isdigit (text[1]))
1007 return NULL;
1008 return complete_internalvar (&text[1]);
1009 }
1010
1011 /* We're completing the breakpoint number. */
1012 len = strlen (text);
1013
1014 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1015 {
1016 char number[50];
1017
1018 xsnprintf (number, sizeof (number), "%d", b->number);
1019
1020 if (strncmp (number, text, len) == 0)
1021 VEC_safe_push (char_ptr, result, xstrdup (number));
1022 }
d55637df
TT
1023
1024 return result;
1025 }
1026
1027 /* We're completing the expression part. */
6f937416 1028 text = skip_spaces_const (space);
d55637df
TT
1029 return expression_completer (cmd, text, word);
1030}
1031
c906108c
SS
1032/* condition N EXP -- set break condition of breakpoint N to EXP. */
1033
1034static void
fba45db2 1035condition_command (char *arg, int from_tty)
c906108c 1036{
52f0bd74 1037 struct breakpoint *b;
c906108c 1038 char *p;
52f0bd74 1039 int bnum;
c906108c
SS
1040
1041 if (arg == 0)
e2e0b3e5 1042 error_no_arg (_("breakpoint number"));
c906108c
SS
1043
1044 p = arg;
1045 bnum = get_number (&p);
5c44784c 1046 if (bnum == 0)
8a3fe4f8 1047 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1048
1049 ALL_BREAKPOINTS (b)
1050 if (b->number == bnum)
2f069f6f 1051 {
6dddc817
DE
1052 /* Check if this breakpoint has a "stop" method implemented in an
1053 extension language. This method and conditions entered into GDB
1054 from the CLI are mutually exclusive. */
1055 const struct extension_language_defn *extlang
1056 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1057
1058 if (extlang != NULL)
1059 {
1060 error (_("Only one stop condition allowed. There is currently"
1061 " a %s stop condition defined for this breakpoint."),
1062 ext_lang_capitalized_name (extlang));
1063 }
2566ad2d 1064 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1065
1066 if (is_breakpoint (b))
1067 update_global_location_list (1);
1068
2f069f6f
JB
1069 return;
1070 }
c906108c 1071
8a3fe4f8 1072 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1073}
1074
a7bdde9e
VP
1075/* Check that COMMAND do not contain commands that are suitable
1076 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1077 Throw if any such commands is found. */
1078
a7bdde9e
VP
1079static void
1080check_no_tracepoint_commands (struct command_line *commands)
1081{
1082 struct command_line *c;
cc59ec59 1083
a7bdde9e
VP
1084 for (c = commands; c; c = c->next)
1085 {
1086 int i;
1087
1088 if (c->control_type == while_stepping_control)
3e43a32a
MS
1089 error (_("The 'while-stepping' command can "
1090 "only be used for tracepoints"));
a7bdde9e
VP
1091
1092 for (i = 0; i < c->body_count; ++i)
1093 check_no_tracepoint_commands ((c->body_list)[i]);
1094
1095 /* Not that command parsing removes leading whitespace and comment
4a64f543 1096 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1097 command directly. */
1098 if (strstr (c->line, "collect ") == c->line)
1099 error (_("The 'collect' command can only be used for tracepoints"));
1100
51661e93
VP
1101 if (strstr (c->line, "teval ") == c->line)
1102 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1103 }
1104}
1105
d77f58be
SS
1106/* Encapsulate tests for different types of tracepoints. */
1107
d9b3f62e
PA
1108static int
1109is_tracepoint_type (enum bptype type)
1110{
1111 return (type == bp_tracepoint
1112 || type == bp_fast_tracepoint
1113 || type == bp_static_tracepoint);
1114}
1115
a7bdde9e 1116int
d77f58be 1117is_tracepoint (const struct breakpoint *b)
a7bdde9e 1118{
d9b3f62e 1119 return is_tracepoint_type (b->type);
a7bdde9e 1120}
d9b3f62e 1121
e5dd4106 1122/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1123 breakpoint. This function will throw an exception if a problem is
1124 found. */
48cb2d85 1125
95a42b64
TT
1126static void
1127validate_commands_for_breakpoint (struct breakpoint *b,
1128 struct command_line *commands)
48cb2d85 1129{
d77f58be 1130 if (is_tracepoint (b))
a7bdde9e 1131 {
c9a6ce02 1132 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1133 struct command_line *c;
1134 struct command_line *while_stepping = 0;
c9a6ce02
PA
1135
1136 /* Reset the while-stepping step count. The previous commands
1137 might have included a while-stepping action, while the new
1138 ones might not. */
1139 t->step_count = 0;
1140
1141 /* We need to verify that each top-level element of commands is
1142 valid for tracepoints, that there's at most one
1143 while-stepping element, and that the while-stepping's body
1144 has valid tracing commands excluding nested while-stepping.
1145 We also need to validate the tracepoint action line in the
1146 context of the tracepoint --- validate_actionline actually
1147 has side effects, like setting the tracepoint's
1148 while-stepping STEP_COUNT, in addition to checking if the
1149 collect/teval actions parse and make sense in the
1150 tracepoint's context. */
a7bdde9e
VP
1151 for (c = commands; c; c = c->next)
1152 {
a7bdde9e
VP
1153 if (c->control_type == while_stepping_control)
1154 {
1155 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1156 error (_("The 'while-stepping' command "
1157 "cannot be used for fast tracepoint"));
0fb4aa4b 1158 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1159 error (_("The 'while-stepping' command "
1160 "cannot be used for static tracepoint"));
a7bdde9e
VP
1161
1162 if (while_stepping)
3e43a32a
MS
1163 error (_("The 'while-stepping' command "
1164 "can be used only once"));
a7bdde9e
VP
1165 else
1166 while_stepping = c;
1167 }
c9a6ce02
PA
1168
1169 validate_actionline (c->line, b);
a7bdde9e
VP
1170 }
1171 if (while_stepping)
1172 {
1173 struct command_line *c2;
1174
1175 gdb_assert (while_stepping->body_count == 1);
1176 c2 = while_stepping->body_list[0];
1177 for (; c2; c2 = c2->next)
1178 {
a7bdde9e
VP
1179 if (c2->control_type == while_stepping_control)
1180 error (_("The 'while-stepping' command cannot be nested"));
1181 }
1182 }
1183 }
1184 else
1185 {
1186 check_no_tracepoint_commands (commands);
1187 }
95a42b64
TT
1188}
1189
0fb4aa4b
PA
1190/* Return a vector of all the static tracepoints set at ADDR. The
1191 caller is responsible for releasing the vector. */
1192
1193VEC(breakpoint_p) *
1194static_tracepoints_here (CORE_ADDR addr)
1195{
1196 struct breakpoint *b;
1197 VEC(breakpoint_p) *found = 0;
1198 struct bp_location *loc;
1199
1200 ALL_BREAKPOINTS (b)
1201 if (b->type == bp_static_tracepoint)
1202 {
1203 for (loc = b->loc; loc; loc = loc->next)
1204 if (loc->address == addr)
1205 VEC_safe_push(breakpoint_p, found, b);
1206 }
1207
1208 return found;
1209}
1210
95a42b64 1211/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1212 validate that only allowed commands are included. */
95a42b64
TT
1213
1214void
4a64f543
MS
1215breakpoint_set_commands (struct breakpoint *b,
1216 struct command_line *commands)
95a42b64
TT
1217{
1218 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1219
9add0f1b
TT
1220 decref_counted_command_line (&b->commands);
1221 b->commands = alloc_counted_command_line (commands);
8d3788bd 1222 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1223}
1224
45a43567
TT
1225/* Set the internal `silent' flag on the breakpoint. Note that this
1226 is not the same as the "silent" that may appear in the breakpoint's
1227 commands. */
1228
1229void
1230breakpoint_set_silent (struct breakpoint *b, int silent)
1231{
1232 int old_silent = b->silent;
1233
1234 b->silent = silent;
1235 if (old_silent != silent)
8d3788bd 1236 observer_notify_breakpoint_modified (b);
45a43567
TT
1237}
1238
1239/* Set the thread for this breakpoint. If THREAD is -1, make the
1240 breakpoint work for any thread. */
1241
1242void
1243breakpoint_set_thread (struct breakpoint *b, int thread)
1244{
1245 int old_thread = b->thread;
1246
1247 b->thread = thread;
1248 if (old_thread != thread)
8d3788bd 1249 observer_notify_breakpoint_modified (b);
45a43567
TT
1250}
1251
1252/* Set the task for this breakpoint. If TASK is 0, make the
1253 breakpoint work for any task. */
1254
1255void
1256breakpoint_set_task (struct breakpoint *b, int task)
1257{
1258 int old_task = b->task;
1259
1260 b->task = task;
1261 if (old_task != task)
8d3788bd 1262 observer_notify_breakpoint_modified (b);
45a43567
TT
1263}
1264
95a42b64
TT
1265void
1266check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1267{
1268 struct breakpoint *b = closure;
cc59ec59 1269
6f937416 1270 validate_actionline (line, b);
a7bdde9e
VP
1271}
1272
95a42b64
TT
1273/* A structure used to pass information through
1274 map_breakpoint_numbers. */
1275
1276struct commands_info
1277{
1278 /* True if the command was typed at a tty. */
1279 int from_tty;
86b17b60
PA
1280
1281 /* The breakpoint range spec. */
1282 char *arg;
1283
95a42b64
TT
1284 /* Non-NULL if the body of the commands are being read from this
1285 already-parsed command. */
1286 struct command_line *control;
86b17b60 1287
95a42b64
TT
1288 /* The command lines read from the user, or NULL if they have not
1289 yet been read. */
1290 struct counted_command_line *cmd;
1291};
1292
1293/* A callback for map_breakpoint_numbers that sets the commands for
1294 commands_command. */
1295
c906108c 1296static void
95a42b64 1297do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1298{
95a42b64 1299 struct commands_info *info = data;
c906108c 1300
95a42b64
TT
1301 if (info->cmd == NULL)
1302 {
1303 struct command_line *l;
5c44784c 1304
95a42b64
TT
1305 if (info->control != NULL)
1306 l = copy_command_lines (info->control->body_list[0]);
1307 else
86b17b60
PA
1308 {
1309 struct cleanup *old_chain;
1310 char *str;
c5aa993b 1311
3e43a32a
MS
1312 str = xstrprintf (_("Type commands for breakpoint(s) "
1313 "%s, one per line."),
86b17b60
PA
1314 info->arg);
1315
1316 old_chain = make_cleanup (xfree, str);
1317
1318 l = read_command_lines (str,
1319 info->from_tty, 1,
d77f58be 1320 (is_tracepoint (b)
86b17b60
PA
1321 ? check_tracepoint_command : 0),
1322 b);
1323
1324 do_cleanups (old_chain);
1325 }
a7bdde9e 1326
95a42b64
TT
1327 info->cmd = alloc_counted_command_line (l);
1328 }
1329
1330 /* If a breakpoint was on the list more than once, we don't need to
1331 do anything. */
1332 if (b->commands != info->cmd)
1333 {
1334 validate_commands_for_breakpoint (b, info->cmd->commands);
1335 incref_counted_command_line (info->cmd);
1336 decref_counted_command_line (&b->commands);
1337 b->commands = info->cmd;
8d3788bd 1338 observer_notify_breakpoint_modified (b);
c5aa993b 1339 }
95a42b64
TT
1340}
1341
1342static void
4a64f543
MS
1343commands_command_1 (char *arg, int from_tty,
1344 struct command_line *control)
95a42b64
TT
1345{
1346 struct cleanup *cleanups;
1347 struct commands_info info;
1348
1349 info.from_tty = from_tty;
1350 info.control = control;
1351 info.cmd = NULL;
1352 /* If we read command lines from the user, then `info' will hold an
1353 extra reference to the commands that we must clean up. */
1354 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1355
1356 if (arg == NULL || !*arg)
1357 {
86b17b60 1358 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1359 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1360 breakpoint_count);
95a42b64
TT
1361 else if (breakpoint_count > 0)
1362 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1363 else
1364 {
1365 /* So that we don't try to free the incoming non-NULL
1366 argument in the cleanup below. Mapping breakpoint
1367 numbers will fail in this case. */
1368 arg = NULL;
1369 }
95a42b64 1370 }
9766ced4
SS
1371 else
1372 /* The command loop has some static state, so we need to preserve
1373 our argument. */
1374 arg = xstrdup (arg);
86b17b60
PA
1375
1376 if (arg != NULL)
1377 make_cleanup (xfree, arg);
1378
1379 info.arg = arg;
95a42b64
TT
1380
1381 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1382
1383 if (info.cmd == NULL)
1384 error (_("No breakpoints specified."));
1385
1386 do_cleanups (cleanups);
1387}
1388
1389static void
1390commands_command (char *arg, int from_tty)
1391{
1392 commands_command_1 (arg, from_tty, NULL);
c906108c 1393}
40c03ae8
EZ
1394
1395/* Like commands_command, but instead of reading the commands from
1396 input stream, takes them from an already parsed command structure.
1397
1398 This is used by cli-script.c to DTRT with breakpoint commands
1399 that are part of if and while bodies. */
1400enum command_control_type
1401commands_from_control_command (char *arg, struct command_line *cmd)
1402{
95a42b64
TT
1403 commands_command_1 (arg, 0, cmd);
1404 return simple_control;
40c03ae8 1405}
876fa593
JK
1406
1407/* Return non-zero if BL->TARGET_INFO contains valid information. */
1408
1409static int
1410bp_location_has_shadow (struct bp_location *bl)
1411{
1412 if (bl->loc_type != bp_loc_software_breakpoint)
1413 return 0;
1414 if (!bl->inserted)
1415 return 0;
1416 if (bl->target_info.shadow_len == 0)
e5dd4106 1417 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1418 return 0;
1419 return 1;
1420}
1421
9d497a19
PA
1422/* Update BUF, which is LEN bytes read from the target address
1423 MEMADDR, by replacing a memory breakpoint with its shadowed
1424 contents.
1425
1426 If READBUF is not NULL, this buffer must not overlap with the of
1427 the breakpoint location's shadow_contents buffer. Otherwise, a
1428 failed assertion internal error will be raised. */
1429
1430static void
1431one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1432 const gdb_byte *writebuf_org,
1433 ULONGEST memaddr, LONGEST len,
1434 struct bp_target_info *target_info,
1435 struct gdbarch *gdbarch)
1436{
1437 /* Now do full processing of the found relevant range of elements. */
1438 CORE_ADDR bp_addr = 0;
1439 int bp_size = 0;
1440 int bptoffset = 0;
1441
1442 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1443 current_program_space->aspace, 0))
1444 {
1445 /* The breakpoint is inserted in a different address space. */
1446 return;
1447 }
1448
1449 /* Addresses and length of the part of the breakpoint that
1450 we need to copy. */
1451 bp_addr = target_info->placed_address;
1452 bp_size = target_info->shadow_len;
1453
1454 if (bp_addr + bp_size <= memaddr)
1455 {
1456 /* The breakpoint is entirely before the chunk of memory we are
1457 reading. */
1458 return;
1459 }
1460
1461 if (bp_addr >= memaddr + len)
1462 {
1463 /* The breakpoint is entirely after the chunk of memory we are
1464 reading. */
1465 return;
1466 }
1467
1468 /* Offset within shadow_contents. */
1469 if (bp_addr < memaddr)
1470 {
1471 /* Only copy the second part of the breakpoint. */
1472 bp_size -= memaddr - bp_addr;
1473 bptoffset = memaddr - bp_addr;
1474 bp_addr = memaddr;
1475 }
1476
1477 if (bp_addr + bp_size > memaddr + len)
1478 {
1479 /* Only copy the first part of the breakpoint. */
1480 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1481 }
1482
1483 if (readbuf != NULL)
1484 {
1485 /* Verify that the readbuf buffer does not overlap with the
1486 shadow_contents buffer. */
1487 gdb_assert (target_info->shadow_contents >= readbuf + len
1488 || readbuf >= (target_info->shadow_contents
1489 + target_info->shadow_len));
1490
1491 /* Update the read buffer with this inserted breakpoint's
1492 shadow. */
1493 memcpy (readbuf + bp_addr - memaddr,
1494 target_info->shadow_contents + bptoffset, bp_size);
1495 }
1496 else
1497 {
1498 const unsigned char *bp;
1499 CORE_ADDR placed_address = target_info->placed_address;
1500 int placed_size = target_info->placed_size;
1501
1502 /* Update the shadow with what we want to write to memory. */
1503 memcpy (target_info->shadow_contents + bptoffset,
1504 writebuf_org + bp_addr - memaddr, bp_size);
1505
1506 /* Determine appropriate breakpoint contents and size for this
1507 address. */
1508 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1509
1510 /* Update the final write buffer with this inserted
1511 breakpoint's INSN. */
1512 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1513 }
1514}
1515
8defab1a 1516/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1517 by replacing any memory breakpoints with their shadowed contents.
1518
35c63cd8
JB
1519 If READBUF is not NULL, this buffer must not overlap with any of
1520 the breakpoint location's shadow_contents buffers. Otherwise,
1521 a failed assertion internal error will be raised.
1522
876fa593 1523 The range of shadowed area by each bp_location is:
35df4500
TJB
1524 bl->address - bp_location_placed_address_before_address_max
1525 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1526 The range we were requested to resolve shadows for is:
1527 memaddr ... memaddr + len
1528 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1529 memaddr + len <= (bl->address
1530 - bp_location_placed_address_before_address_max)
876fa593 1531 and:
35df4500 1532 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1533
8defab1a 1534void
f0ba3972
PA
1535breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1536 const gdb_byte *writebuf_org,
1537 ULONGEST memaddr, LONGEST len)
c906108c 1538{
4a64f543
MS
1539 /* Left boundary, right boundary and median element of our binary
1540 search. */
876fa593 1541 unsigned bc_l, bc_r, bc;
9d497a19 1542 size_t i;
876fa593 1543
4a64f543
MS
1544 /* Find BC_L which is a leftmost element which may affect BUF
1545 content. It is safe to report lower value but a failure to
1546 report higher one. */
876fa593
JK
1547
1548 bc_l = 0;
1549 bc_r = bp_location_count;
1550 while (bc_l + 1 < bc_r)
1551 {
35df4500 1552 struct bp_location *bl;
876fa593
JK
1553
1554 bc = (bc_l + bc_r) / 2;
35df4500 1555 bl = bp_location[bc];
876fa593 1556
4a64f543
MS
1557 /* Check first BL->ADDRESS will not overflow due to the added
1558 constant. Then advance the left boundary only if we are sure
1559 the BC element can in no way affect the BUF content (MEMADDR
1560 to MEMADDR + LEN range).
876fa593 1561
4a64f543
MS
1562 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1563 offset so that we cannot miss a breakpoint with its shadow
1564 range tail still reaching MEMADDR. */
c5aa993b 1565
35df4500
TJB
1566 if ((bl->address + bp_location_shadow_len_after_address_max
1567 >= bl->address)
1568 && (bl->address + bp_location_shadow_len_after_address_max
1569 <= memaddr))
876fa593
JK
1570 bc_l = bc;
1571 else
1572 bc_r = bc;
1573 }
1574
128070bb
PA
1575 /* Due to the binary search above, we need to make sure we pick the
1576 first location that's at BC_L's address. E.g., if there are
1577 multiple locations at the same address, BC_L may end up pointing
1578 at a duplicate location, and miss the "master"/"inserted"
1579 location. Say, given locations L1, L2 and L3 at addresses A and
1580 B:
1581
1582 L1@A, L2@A, L3@B, ...
1583
1584 BC_L could end up pointing at location L2, while the "master"
1585 location could be L1. Since the `loc->inserted' flag is only set
1586 on "master" locations, we'd forget to restore the shadow of L1
1587 and L2. */
1588 while (bc_l > 0
1589 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1590 bc_l--;
1591
876fa593
JK
1592 /* Now do full processing of the found relevant range of elements. */
1593
1594 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1595 {
35df4500 1596 struct bp_location *bl = bp_location[bc];
876fa593
JK
1597 CORE_ADDR bp_addr = 0;
1598 int bp_size = 0;
1599 int bptoffset = 0;
1600
35df4500
TJB
1601 /* bp_location array has BL->OWNER always non-NULL. */
1602 if (bl->owner->type == bp_none)
8a3fe4f8 1603 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1604 bl->owner->number);
ffce0d52 1605
e5dd4106 1606 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1607 content. */
1608
35df4500
TJB
1609 if (bl->address >= bp_location_placed_address_before_address_max
1610 && memaddr + len <= (bl->address
1611 - bp_location_placed_address_before_address_max))
876fa593
JK
1612 break;
1613
35df4500 1614 if (!bp_location_has_shadow (bl))
c5aa993b 1615 continue;
6c95b8df 1616
9d497a19
PA
1617 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1618 memaddr, len, &bl->target_info, bl->gdbarch);
1619 }
c5aa993b 1620
9d497a19
PA
1621 /* Now process single-step breakpoints. These are not found in the
1622 bp_location array. */
1623 for (i = 0; i < 2; i++)
1624 {
1625 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
c5aa993b 1626
9d497a19
PA
1627 if (bp_tgt != NULL)
1628 {
1629 struct gdbarch *gdbarch = single_step_gdbarch[i];
c5aa993b 1630
9d497a19
PA
1631 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1632 memaddr, len, bp_tgt, gdbarch);
1633 }
1634 }
c906108c 1635}
9d497a19 1636
c906108c 1637\f
c5aa993b 1638
b775012e
LM
1639/* Return true if BPT is either a software breakpoint or a hardware
1640 breakpoint. */
1641
1642int
1643is_breakpoint (const struct breakpoint *bpt)
1644{
1645 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1646 || bpt->type == bp_hardware_breakpoint
1647 || bpt->type == bp_dprintf);
b775012e
LM
1648}
1649
60e1c644
PA
1650/* Return true if BPT is of any hardware watchpoint kind. */
1651
a5606eee 1652static int
d77f58be 1653is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1654{
1655 return (bpt->type == bp_hardware_watchpoint
1656 || bpt->type == bp_read_watchpoint
1657 || bpt->type == bp_access_watchpoint);
1658}
7270d8f2 1659
60e1c644
PA
1660/* Return true if BPT is of any watchpoint kind, hardware or
1661 software. */
1662
3a5c3e22 1663int
d77f58be 1664is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1665{
1666 return (is_hardware_watchpoint (bpt)
1667 || bpt->type == bp_watchpoint);
1668}
1669
3a5c3e22
PA
1670/* Returns true if the current thread and its running state are safe
1671 to evaluate or update watchpoint B. Watchpoints on local
1672 expressions need to be evaluated in the context of the thread that
1673 was current when the watchpoint was created, and, that thread needs
1674 to be stopped to be able to select the correct frame context.
1675 Watchpoints on global expressions can be evaluated on any thread,
1676 and in any state. It is presently left to the target allowing
1677 memory accesses when threads are running. */
f6bc2008
PA
1678
1679static int
3a5c3e22 1680watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1681{
d0d8b0c6
JK
1682 return (b->base.pspace == current_program_space
1683 && (ptid_equal (b->watchpoint_thread, null_ptid)
1684 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1685 && !is_executing (inferior_ptid))));
f6bc2008
PA
1686}
1687
d0fb5eae
JK
1688/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1689 associated bp_watchpoint_scope breakpoint. */
1690
1691static void
3a5c3e22 1692watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1693{
3a5c3e22 1694 struct breakpoint *b = &w->base;
d0fb5eae
JK
1695
1696 if (b->related_breakpoint != b)
1697 {
1698 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1699 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1700 b->related_breakpoint->disposition = disp_del_at_next_stop;
1701 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1702 b->related_breakpoint = b;
1703 }
1704 b->disposition = disp_del_at_next_stop;
1705}
1706
567e1b4e
JB
1707/* Assuming that B is a watchpoint:
1708 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1709 - Evaluate expression and store the result in B->val
567e1b4e
JB
1710 - Evaluate the condition if there is one, and store the result
1711 in b->loc->cond.
a5606eee
VP
1712 - Update the list of values that must be watched in B->loc.
1713
4a64f543
MS
1714 If the watchpoint disposition is disp_del_at_next_stop, then do
1715 nothing. If this is local watchpoint that is out of scope, delete
1716 it.
1717
1718 Even with `set breakpoint always-inserted on' the watchpoints are
1719 removed + inserted on each stop here. Normal breakpoints must
1720 never be removed because they might be missed by a running thread
1721 when debugging in non-stop mode. On the other hand, hardware
1722 watchpoints (is_hardware_watchpoint; processed here) are specific
1723 to each LWP since they are stored in each LWP's hardware debug
1724 registers. Therefore, such LWP must be stopped first in order to
1725 be able to modify its hardware watchpoints.
1726
1727 Hardware watchpoints must be reset exactly once after being
1728 presented to the user. It cannot be done sooner, because it would
1729 reset the data used to present the watchpoint hit to the user. And
1730 it must not be done later because it could display the same single
1731 watchpoint hit during multiple GDB stops. Note that the latter is
1732 relevant only to the hardware watchpoint types bp_read_watchpoint
1733 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1734 not user-visible - its hit is suppressed if the memory content has
1735 not changed.
1736
1737 The following constraints influence the location where we can reset
1738 hardware watchpoints:
1739
1740 * target_stopped_by_watchpoint and target_stopped_data_address are
1741 called several times when GDB stops.
1742
1743 [linux]
1744 * Multiple hardware watchpoints can be hit at the same time,
1745 causing GDB to stop. GDB only presents one hardware watchpoint
1746 hit at a time as the reason for stopping, and all the other hits
1747 are presented later, one after the other, each time the user
1748 requests the execution to be resumed. Execution is not resumed
1749 for the threads still having pending hit event stored in
1750 LWP_INFO->STATUS. While the watchpoint is already removed from
1751 the inferior on the first stop the thread hit event is kept being
1752 reported from its cached value by linux_nat_stopped_data_address
1753 until the real thread resume happens after the watchpoint gets
1754 presented and thus its LWP_INFO->STATUS gets reset.
1755
1756 Therefore the hardware watchpoint hit can get safely reset on the
1757 watchpoint removal from inferior. */
a79d3c27 1758
b40ce68a 1759static void
3a5c3e22 1760update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1761{
a5606eee 1762 int within_current_scope;
a5606eee 1763 struct frame_id saved_frame_id;
66076460 1764 int frame_saved;
a5606eee 1765
f6bc2008
PA
1766 /* If this is a local watchpoint, we only want to check if the
1767 watchpoint frame is in scope if the current thread is the thread
1768 that was used to create the watchpoint. */
1769 if (!watchpoint_in_thread_scope (b))
1770 return;
1771
3a5c3e22 1772 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1773 return;
1774
66076460 1775 frame_saved = 0;
a5606eee
VP
1776
1777 /* Determine if the watchpoint is within scope. */
1778 if (b->exp_valid_block == NULL)
1779 within_current_scope = 1;
1780 else
1781 {
b5db5dfc
UW
1782 struct frame_info *fi = get_current_frame ();
1783 struct gdbarch *frame_arch = get_frame_arch (fi);
1784 CORE_ADDR frame_pc = get_frame_pc (fi);
1785
1786 /* If we're in a function epilogue, unwinding may not work
1787 properly, so do not attempt to recreate locations at this
1788 point. See similar comments in watchpoint_check. */
1789 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1790 return;
66076460
DJ
1791
1792 /* Save the current frame's ID so we can restore it after
1793 evaluating the watchpoint expression on its own frame. */
1794 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1795 took a frame parameter, so that we didn't have to change the
1796 selected frame. */
1797 frame_saved = 1;
1798 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1799
a5606eee
VP
1800 fi = frame_find_by_id (b->watchpoint_frame);
1801 within_current_scope = (fi != NULL);
1802 if (within_current_scope)
1803 select_frame (fi);
1804 }
1805
b5db5dfc
UW
1806 /* We don't free locations. They are stored in the bp_location array
1807 and update_global_location_list will eventually delete them and
1808 remove breakpoints if needed. */
3a5c3e22 1809 b->base.loc = NULL;
b5db5dfc 1810
a5606eee
VP
1811 if (within_current_scope && reparse)
1812 {
bbc13ae3 1813 const char *s;
d63d0675 1814
a5606eee
VP
1815 if (b->exp)
1816 {
1817 xfree (b->exp);
1818 b->exp = NULL;
1819 }
d63d0675 1820 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1821 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1822 /* If the meaning of expression itself changed, the old value is
1823 no longer relevant. We don't want to report a watchpoint hit
1824 to the user when the old value and the new value may actually
1825 be completely different objects. */
1826 value_free (b->val);
fa4727a6
DJ
1827 b->val = NULL;
1828 b->val_valid = 0;
60e1c644
PA
1829
1830 /* Note that unlike with breakpoints, the watchpoint's condition
1831 expression is stored in the breakpoint object, not in the
1832 locations (re)created below. */
3a5c3e22 1833 if (b->base.cond_string != NULL)
60e1c644
PA
1834 {
1835 if (b->cond_exp != NULL)
1836 {
1837 xfree (b->cond_exp);
1838 b->cond_exp = NULL;
1839 }
1840
3a5c3e22 1841 s = b->base.cond_string;
1bb9788d 1842 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1843 }
a5606eee 1844 }
a5606eee
VP
1845
1846 /* If we failed to parse the expression, for example because
1847 it refers to a global variable in a not-yet-loaded shared library,
1848 don't try to insert watchpoint. We don't automatically delete
1849 such watchpoint, though, since failure to parse expression
1850 is different from out-of-scope watchpoint. */
e8369a73 1851 if (!target_has_execution)
2d134ed3
PA
1852 {
1853 /* Without execution, memory can't change. No use to try and
1854 set watchpoint locations. The watchpoint will be reset when
1855 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1856 if (!can_use_hw_watchpoints)
1857 {
1858 if (b->base.ops->works_in_software_mode (&b->base))
1859 b->base.type = bp_watchpoint;
1860 else
638aa5a1
AB
1861 error (_("Can't set read/access watchpoint when "
1862 "hardware watchpoints are disabled."));
e8369a73 1863 }
2d134ed3
PA
1864 }
1865 else if (within_current_scope && b->exp)
a5606eee 1866 {
0cf6dd15 1867 int pc = 0;
fa4727a6 1868 struct value *val_chain, *v, *result, *next;
2d134ed3 1869 struct program_space *frame_pspace;
a5606eee 1870
3a1115a0 1871 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1872
a5606eee
VP
1873 /* Avoid setting b->val if it's already set. The meaning of
1874 b->val is 'the last value' user saw, and we should update
1875 it only if we reported that last value to user. As it
9c06b0b4
TJB
1876 happens, the code that reports it updates b->val directly.
1877 We don't keep track of the memory value for masked
1878 watchpoints. */
3a5c3e22 1879 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1880 {
1881 b->val = v;
1882 b->val_valid = 1;
1883 }
a5606eee 1884
2d134ed3
PA
1885 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1886
a5606eee 1887 /* Look at each value on the value chain. */
9fa40276 1888 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1889 {
1890 /* If it's a memory location, and GDB actually needed
1891 its contents to evaluate the expression, then we
fa4727a6
DJ
1892 must watch it. If the first value returned is
1893 still lazy, that means an error occurred reading it;
1894 watch it anyway in case it becomes readable. */
a5606eee 1895 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1896 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1897 {
1898 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1899
a5606eee
VP
1900 /* We only watch structs and arrays if user asked
1901 for it explicitly, never if they just happen to
1902 appear in the middle of some value chain. */
fa4727a6 1903 if (v == result
a5606eee
VP
1904 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1905 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1906 {
1907 CORE_ADDR addr;
744a8059 1908 int type;
a5606eee
VP
1909 struct bp_location *loc, **tmp;
1910
42ae5230 1911 addr = value_address (v);
a5606eee 1912 type = hw_write;
3a5c3e22 1913 if (b->base.type == bp_read_watchpoint)
a5606eee 1914 type = hw_read;
3a5c3e22 1915 else if (b->base.type == bp_access_watchpoint)
a5606eee 1916 type = hw_access;
3a5c3e22
PA
1917
1918 loc = allocate_bp_location (&b->base);
1919 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1920 ;
1921 *tmp = loc;
a6d9a66e 1922 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1923
1924 loc->pspace = frame_pspace;
a5606eee 1925 loc->address = addr;
744a8059 1926 loc->length = TYPE_LENGTH (value_type (v));
a5606eee
VP
1927 loc->watchpoint_type = type;
1928 }
1929 }
9fa40276
TJB
1930 }
1931
1932 /* Change the type of breakpoint between hardware assisted or
1933 an ordinary watchpoint depending on the hardware support
1934 and free hardware slots. REPARSE is set when the inferior
1935 is started. */
a9634178 1936 if (reparse)
9fa40276 1937 {
e09342b5 1938 int reg_cnt;
9fa40276
TJB
1939 enum bp_loc_type loc_type;
1940 struct bp_location *bl;
a5606eee 1941
a9634178 1942 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1943
1944 if (reg_cnt)
9fa40276
TJB
1945 {
1946 int i, target_resources_ok, other_type_used;
a1398e0c 1947 enum bptype type;
9fa40276 1948
a9634178
TJB
1949 /* Use an exact watchpoint when there's only one memory region to be
1950 watched, and only one debug register is needed to watch it. */
1951 b->exact = target_exact_watchpoints && reg_cnt == 1;
1952
9fa40276 1953 /* We need to determine how many resources are already
e09342b5
TJB
1954 used for all other hardware watchpoints plus this one
1955 to see if we still have enough resources to also fit
a1398e0c
PA
1956 this watchpoint in as well. */
1957
1958 /* If this is a software watchpoint, we try to turn it
1959 to a hardware one -- count resources as if B was of
1960 hardware watchpoint type. */
1961 type = b->base.type;
1962 if (type == bp_watchpoint)
1963 type = bp_hardware_watchpoint;
1964
1965 /* This watchpoint may or may not have been placed on
1966 the list yet at this point (it won't be in the list
1967 if we're trying to create it for the first time,
1968 through watch_command), so always account for it
1969 manually. */
1970
1971 /* Count resources used by all watchpoints except B. */
1972 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1973
1974 /* Add in the resources needed for B. */
1975 i += hw_watchpoint_use_count (&b->base);
1976
1977 target_resources_ok
1978 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1979 if (target_resources_ok <= 0)
a9634178 1980 {
3a5c3e22 1981 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1982
1983 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1984 error (_("Target does not support this type of "
1985 "hardware watchpoint."));
9c06b0b4
TJB
1986 else if (target_resources_ok < 0 && !sw_mode)
1987 error (_("There are not enough available hardware "
1988 "resources for this watchpoint."));
a1398e0c
PA
1989
1990 /* Downgrade to software watchpoint. */
1991 b->base.type = bp_watchpoint;
1992 }
1993 else
1994 {
1995 /* If this was a software watchpoint, we've just
1996 found we have enough resources to turn it to a
1997 hardware watchpoint. Otherwise, this is a
1998 nop. */
1999 b->base.type = type;
a9634178 2000 }
9fa40276 2001 }
3a5c3e22 2002 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2003 {
2004 if (!can_use_hw_watchpoints)
2005 error (_("Can't set read/access watchpoint when "
2006 "hardware watchpoints are disabled."));
2007 else
2008 error (_("Expression cannot be implemented with "
2009 "read/access watchpoint."));
2010 }
9fa40276 2011 else
3a5c3e22 2012 b->base.type = bp_watchpoint;
9fa40276 2013
3a5c3e22 2014 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2015 : bp_loc_hardware_watchpoint);
3a5c3e22 2016 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2017 bl->loc_type = loc_type;
2018 }
2019
2020 for (v = val_chain; v; v = next)
2021 {
a5606eee
VP
2022 next = value_next (v);
2023 if (v != b->val)
2024 value_free (v);
2025 }
2026
c7437ca6
PA
2027 /* If a software watchpoint is not watching any memory, then the
2028 above left it without any location set up. But,
2029 bpstat_stop_status requires a location to be able to report
2030 stops, so make sure there's at least a dummy one. */
3a5c3e22 2031 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2032 {
3a5c3e22
PA
2033 struct breakpoint *base = &b->base;
2034 base->loc = allocate_bp_location (base);
2035 base->loc->pspace = frame_pspace;
2036 base->loc->address = -1;
2037 base->loc->length = -1;
2038 base->loc->watchpoint_type = -1;
c7437ca6 2039 }
a5606eee
VP
2040 }
2041 else if (!within_current_scope)
7270d8f2 2042 {
ac74f770
MS
2043 printf_filtered (_("\
2044Watchpoint %d deleted because the program has left the block\n\
2045in which its expression is valid.\n"),
3a5c3e22 2046 b->base.number);
d0fb5eae 2047 watchpoint_del_at_next_stop (b);
7270d8f2 2048 }
a5606eee
VP
2049
2050 /* Restore the selected frame. */
66076460
DJ
2051 if (frame_saved)
2052 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2053}
2054
a5606eee 2055
74960c60 2056/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2057 inserted in the inferior. We don't differentiate the type of BL's owner
2058 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2059 breakpoint_ops is not defined, because in insert_bp_location,
2060 tracepoint's insert_location will not be called. */
74960c60 2061static int
35df4500 2062should_be_inserted (struct bp_location *bl)
74960c60 2063{
35df4500 2064 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2065 return 0;
2066
35df4500 2067 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2068 return 0;
2069
35df4500 2070 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2071 return 0;
2072
f8eba3c6
TT
2073 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2074 return 0;
2075
56710373
PA
2076 /* This is set for example, when we're attached to the parent of a
2077 vfork, and have detached from the child. The child is running
2078 free, and we expect it to do an exec or exit, at which point the
2079 OS makes the parent schedulable again (and the target reports
2080 that the vfork is done). Until the child is done with the shared
2081 memory region, do not insert breakpoints in the parent, otherwise
2082 the child could still trip on the parent's breakpoints. Since
2083 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2084 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2085 return 0;
2086
31e77af2
PA
2087 /* Don't insert a breakpoint if we're trying to step past its
2088 location. */
2089 if ((bl->loc_type == bp_loc_software_breakpoint
2090 || bl->loc_type == bp_loc_hardware_breakpoint)
2091 && stepping_past_instruction_at (bl->pspace->aspace,
2092 bl->address))
2093 return 0;
2094
74960c60
VP
2095 return 1;
2096}
2097
934709f0
PW
2098/* Same as should_be_inserted but does the check assuming
2099 that the location is not duplicated. */
2100
2101static int
2102unduplicated_should_be_inserted (struct bp_location *bl)
2103{
2104 int result;
2105 const int save_duplicate = bl->duplicate;
2106
2107 bl->duplicate = 0;
2108 result = should_be_inserted (bl);
2109 bl->duplicate = save_duplicate;
2110 return result;
2111}
2112
b775012e
LM
2113/* Parses a conditional described by an expression COND into an
2114 agent expression bytecode suitable for evaluation
2115 by the bytecode interpreter. Return NULL if there was
2116 any error during parsing. */
2117
2118static struct agent_expr *
2119parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2120{
2121 struct agent_expr *aexpr = NULL;
b775012e
LM
2122 volatile struct gdb_exception ex;
2123
2124 if (!cond)
2125 return NULL;
2126
2127 /* We don't want to stop processing, so catch any errors
2128 that may show up. */
2129 TRY_CATCH (ex, RETURN_MASK_ERROR)
2130 {
2131 aexpr = gen_eval_for_expr (scope, cond);
2132 }
2133
2134 if (ex.reason < 0)
2135 {
2136 /* If we got here, it means the condition could not be parsed to a valid
2137 bytecode expression and thus can't be evaluated on the target's side.
2138 It's no use iterating through the conditions. */
2139 return NULL;
2140 }
2141
2142 /* We have a valid agent expression. */
2143 return aexpr;
2144}
2145
2146/* Based on location BL, create a list of breakpoint conditions to be
2147 passed on to the target. If we have duplicated locations with different
2148 conditions, we will add such conditions to the list. The idea is that the
2149 target will evaluate the list of conditions and will only notify GDB when
2150 one of them is true. */
2151
2152static void
2153build_target_condition_list (struct bp_location *bl)
2154{
2155 struct bp_location **locp = NULL, **loc2p;
2156 int null_condition_or_parse_error = 0;
2157 int modified = bl->needs_update;
2158 struct bp_location *loc;
2159
8b4f3082
PA
2160 /* Release conditions left over from a previous insert. */
2161 VEC_free (agent_expr_p, bl->target_info.conditions);
2162
b775012e
LM
2163 /* This is only meaningful if the target is
2164 evaluating conditions and if the user has
2165 opted for condition evaluation on the target's
2166 side. */
2167 if (gdb_evaluates_breakpoint_condition_p ()
2168 || !target_supports_evaluation_of_breakpoint_conditions ())
2169 return;
2170
2171 /* Do a first pass to check for locations with no assigned
2172 conditions or conditions that fail to parse to a valid agent expression
2173 bytecode. If any of these happen, then it's no use to send conditions
2174 to the target since this location will always trigger and generate a
2175 response back to GDB. */
2176 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2177 {
2178 loc = (*loc2p);
2179 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2180 {
2181 if (modified)
2182 {
2183 struct agent_expr *aexpr;
2184
2185 /* Re-parse the conditions since something changed. In that
2186 case we already freed the condition bytecodes (see
2187 force_breakpoint_reinsertion). We just
2188 need to parse the condition to bytecodes again. */
2189 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2190 loc->cond_bytecode = aexpr;
2191
2192 /* Check if we managed to parse the conditional expression
2193 correctly. If not, we will not send this condition
2194 to the target. */
2195 if (aexpr)
2196 continue;
2197 }
2198
2199 /* If we have a NULL bytecode expression, it means something
2200 went wrong or we have a null condition expression. */
2201 if (!loc->cond_bytecode)
2202 {
2203 null_condition_or_parse_error = 1;
2204 break;
2205 }
2206 }
2207 }
2208
2209 /* If any of these happened, it means we will have to evaluate the conditions
2210 for the location's address on gdb's side. It is no use keeping bytecodes
2211 for all the other duplicate locations, thus we free all of them here.
2212
2213 This is so we have a finer control over which locations' conditions are
2214 being evaluated by GDB or the remote stub. */
2215 if (null_condition_or_parse_error)
2216 {
2217 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2218 {
2219 loc = (*loc2p);
2220 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2221 {
2222 /* Only go as far as the first NULL bytecode is
2223 located. */
2224 if (!loc->cond_bytecode)
2225 return;
2226
2227 free_agent_expr (loc->cond_bytecode);
2228 loc->cond_bytecode = NULL;
2229 }
2230 }
2231 }
2232
2233 /* No NULL conditions or failed bytecode generation. Build a condition list
2234 for this location's address. */
2235 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2236 {
2237 loc = (*loc2p);
2238 if (loc->cond
2239 && is_breakpoint (loc->owner)
2240 && loc->pspace->num == bl->pspace->num
2241 && loc->owner->enable_state == bp_enabled
2242 && loc->enabled)
2243 /* Add the condition to the vector. This will be used later to send the
2244 conditions to the target. */
2245 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2246 loc->cond_bytecode);
2247 }
2248
2249 return;
2250}
2251
d3ce09f5
SS
2252/* Parses a command described by string CMD into an agent expression
2253 bytecode suitable for evaluation by the bytecode interpreter.
2254 Return NULL if there was any error during parsing. */
2255
2256static struct agent_expr *
2257parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2258{
2259 struct cleanup *old_cleanups = 0;
2260 struct expression *expr, **argvec;
2261 struct agent_expr *aexpr = NULL;
d3ce09f5 2262 volatile struct gdb_exception ex;
bbc13ae3
KS
2263 const char *cmdrest;
2264 const char *format_start, *format_end;
d3ce09f5
SS
2265 struct format_piece *fpieces;
2266 int nargs;
2267 struct gdbarch *gdbarch = get_current_arch ();
2268
2269 if (!cmd)
2270 return NULL;
2271
2272 cmdrest = cmd;
2273
2274 if (*cmdrest == ',')
2275 ++cmdrest;
bbc13ae3 2276 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2277
2278 if (*cmdrest++ != '"')
2279 error (_("No format string following the location"));
2280
2281 format_start = cmdrest;
2282
2283 fpieces = parse_format_string (&cmdrest);
2284
2285 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2286
2287 format_end = cmdrest;
2288
2289 if (*cmdrest++ != '"')
2290 error (_("Bad format string, non-terminated '\"'."));
2291
bbc13ae3 2292 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2293
2294 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2295 error (_("Invalid argument syntax"));
2296
2297 if (*cmdrest == ',')
2298 cmdrest++;
bbc13ae3 2299 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2300
2301 /* For each argument, make an expression. */
2302
2303 argvec = (struct expression **) alloca (strlen (cmd)
2304 * sizeof (struct expression *));
2305
2306 nargs = 0;
2307 while (*cmdrest != '\0')
2308 {
bbc13ae3 2309 const char *cmd1;
d3ce09f5
SS
2310
2311 cmd1 = cmdrest;
2312 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2313 argvec[nargs++] = expr;
2314 cmdrest = cmd1;
2315 if (*cmdrest == ',')
2316 ++cmdrest;
2317 }
2318
2319 /* We don't want to stop processing, so catch any errors
2320 that may show up. */
2321 TRY_CATCH (ex, RETURN_MASK_ERROR)
2322 {
2323 aexpr = gen_printf (scope, gdbarch, 0, 0,
2324 format_start, format_end - format_start,
2325 fpieces, nargs, argvec);
2326 }
2327
752eb8b4
TT
2328 do_cleanups (old_cleanups);
2329
d3ce09f5
SS
2330 if (ex.reason < 0)
2331 {
2332 /* If we got here, it means the command could not be parsed to a valid
2333 bytecode expression and thus can't be evaluated on the target's side.
2334 It's no use iterating through the other commands. */
2335 return NULL;
2336 }
2337
d3ce09f5
SS
2338 /* We have a valid agent expression, return it. */
2339 return aexpr;
2340}
2341
2342/* Based on location BL, create a list of breakpoint commands to be
2343 passed on to the target. If we have duplicated locations with
2344 different commands, we will add any such to the list. */
2345
2346static void
2347build_target_command_list (struct bp_location *bl)
2348{
2349 struct bp_location **locp = NULL, **loc2p;
2350 int null_command_or_parse_error = 0;
2351 int modified = bl->needs_update;
2352 struct bp_location *loc;
2353
8b4f3082
PA
2354 /* Release commands left over from a previous insert. */
2355 VEC_free (agent_expr_p, bl->target_info.tcommands);
2356
41fac0cf 2357 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2358 return;
2359
41fac0cf
PA
2360 /* For now, limit to agent-style dprintf breakpoints. */
2361 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2362 return;
2363
41fac0cf
PA
2364 /* For now, if we have any duplicate location that isn't a dprintf,
2365 don't install the target-side commands, as that would make the
2366 breakpoint not be reported to the core, and we'd lose
2367 control. */
2368 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2369 {
2370 loc = (*loc2p);
2371 if (is_breakpoint (loc->owner)
2372 && loc->pspace->num == bl->pspace->num
2373 && loc->owner->type != bp_dprintf)
2374 return;
2375 }
2376
d3ce09f5
SS
2377 /* Do a first pass to check for locations with no assigned
2378 conditions or conditions that fail to parse to a valid agent expression
2379 bytecode. If any of these happen, then it's no use to send conditions
2380 to the target since this location will always trigger and generate a
2381 response back to GDB. */
2382 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2383 {
2384 loc = (*loc2p);
2385 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2386 {
2387 if (modified)
2388 {
2389 struct agent_expr *aexpr;
2390
2391 /* Re-parse the commands since something changed. In that
2392 case we already freed the command bytecodes (see
2393 force_breakpoint_reinsertion). We just
2394 need to parse the command to bytecodes again. */
2395 aexpr = parse_cmd_to_aexpr (bl->address,
2396 loc->owner->extra_string);
2397 loc->cmd_bytecode = aexpr;
2398
2399 if (!aexpr)
2400 continue;
2401 }
2402
2403 /* If we have a NULL bytecode expression, it means something
2404 went wrong or we have a null command expression. */
2405 if (!loc->cmd_bytecode)
2406 {
2407 null_command_or_parse_error = 1;
2408 break;
2409 }
2410 }
2411 }
2412
2413 /* If anything failed, then we're not doing target-side commands,
2414 and so clean up. */
2415 if (null_command_or_parse_error)
2416 {
2417 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2418 {
2419 loc = (*loc2p);
2420 if (is_breakpoint (loc->owner)
2421 && loc->pspace->num == bl->pspace->num)
2422 {
2423 /* Only go as far as the first NULL bytecode is
2424 located. */
40fb6c5e 2425 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2426 return;
2427
40fb6c5e
HZ
2428 free_agent_expr (loc->cmd_bytecode);
2429 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2430 }
2431 }
2432 }
2433
2434 /* No NULL commands or failed bytecode generation. Build a command list
2435 for this location's address. */
2436 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2437 {
2438 loc = (*loc2p);
2439 if (loc->owner->extra_string
2440 && is_breakpoint (loc->owner)
2441 && loc->pspace->num == bl->pspace->num
2442 && loc->owner->enable_state == bp_enabled
2443 && loc->enabled)
2444 /* Add the command to the vector. This will be used later
2445 to send the commands to the target. */
2446 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2447 loc->cmd_bytecode);
2448 }
2449
2450 bl->target_info.persist = 0;
2451 /* Maybe flag this location as persistent. */
2452 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2453 bl->target_info.persist = 1;
2454}
2455
35df4500
TJB
2456/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2457 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2458 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2459 Returns 0 for success, 1 if the bp_location type is not supported or
2460 -1 for failure.
879bfdc2 2461
4a64f543
MS
2462 NOTE drow/2003-09-09: This routine could be broken down to an
2463 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2464static int
35df4500 2465insert_bp_location (struct bp_location *bl,
26bb91f3 2466 struct ui_file *tmp_error_stream,
3fbb6ffa 2467 int *disabled_breaks,
dd61ec5c
MW
2468 int *hw_breakpoint_error,
2469 int *hw_bp_error_explained_already)
879bfdc2 2470{
0000e5cc
PA
2471 enum errors bp_err = GDB_NO_ERROR;
2472 const char *bp_err_message = NULL;
c90a6fb7 2473 volatile struct gdb_exception e;
879bfdc2 2474
b775012e 2475 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2476 return 0;
2477
35c63cd8
JB
2478 /* Note we don't initialize bl->target_info, as that wipes out
2479 the breakpoint location's shadow_contents if the breakpoint
2480 is still inserted at that location. This in turn breaks
2481 target_read_memory which depends on these buffers when
2482 a memory read is requested at the breakpoint location:
2483 Once the target_info has been wiped, we fail to see that
2484 we have a breakpoint inserted at that address and thus
2485 read the breakpoint instead of returning the data saved in
2486 the breakpoint location's shadow contents. */
35df4500
TJB
2487 bl->target_info.placed_address = bl->address;
2488 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2489 bl->target_info.length = bl->length;
8181d85f 2490
b775012e
LM
2491 /* When working with target-side conditions, we must pass all the conditions
2492 for the same breakpoint address down to the target since GDB will not
2493 insert those locations. With a list of breakpoint conditions, the target
2494 can decide when to stop and notify GDB. */
2495
2496 if (is_breakpoint (bl->owner))
2497 {
2498 build_target_condition_list (bl);
d3ce09f5
SS
2499 build_target_command_list (bl);
2500 /* Reset the modification marker. */
b775012e
LM
2501 bl->needs_update = 0;
2502 }
2503
35df4500
TJB
2504 if (bl->loc_type == bp_loc_software_breakpoint
2505 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2506 {
35df4500 2507 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2508 {
2509 /* If the explicitly specified breakpoint type
2510 is not hardware breakpoint, check the memory map to see
2511 if the breakpoint address is in read only memory or not.
4a64f543 2512
765dc015
VP
2513 Two important cases are:
2514 - location type is not hardware breakpoint, memory
2515 is readonly. We change the type of the location to
2516 hardware breakpoint.
4a64f543
MS
2517 - location type is hardware breakpoint, memory is
2518 read-write. This means we've previously made the
2519 location hardware one, but then the memory map changed,
2520 so we undo.
765dc015 2521
4a64f543
MS
2522 When breakpoints are removed, remove_breakpoints will use
2523 location types we've just set here, the only possible
2524 problem is that memory map has changed during running
2525 program, but it's not going to work anyway with current
2526 gdb. */
765dc015 2527 struct mem_region *mr
35df4500 2528 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2529
2530 if (mr)
2531 {
2532 if (automatic_hardware_breakpoints)
2533 {
765dc015
VP
2534 enum bp_loc_type new_type;
2535
2536 if (mr->attrib.mode != MEM_RW)
2537 new_type = bp_loc_hardware_breakpoint;
2538 else
2539 new_type = bp_loc_software_breakpoint;
2540
35df4500 2541 if (new_type != bl->loc_type)
765dc015
VP
2542 {
2543 static int said = 0;
cc59ec59 2544
35df4500 2545 bl->loc_type = new_type;
765dc015
VP
2546 if (!said)
2547 {
3e43a32a
MS
2548 fprintf_filtered (gdb_stdout,
2549 _("Note: automatically using "
2550 "hardware breakpoints for "
2551 "read-only addresses.\n"));
765dc015
VP
2552 said = 1;
2553 }
2554 }
2555 }
35df4500 2556 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 2557 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
2558 warning (_("cannot set software breakpoint "
2559 "at readonly address %s"),
35df4500 2560 paddress (bl->gdbarch, bl->address));
765dc015
VP
2561 }
2562 }
2563
879bfdc2
DJ
2564 /* First check to see if we have to handle an overlay. */
2565 if (overlay_debugging == ovly_off
35df4500
TJB
2566 || bl->section == NULL
2567 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2568 {
2569 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2570 TRY_CATCH (e, RETURN_MASK_ALL)
2571 {
0000e5cc
PA
2572 int val;
2573
dd61ec5c 2574 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2575 if (val)
2576 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2577 }
2578 if (e.reason < 0)
2579 {
0000e5cc
PA
2580 bp_err = e.error;
2581 bp_err_message = e.message;
dd61ec5c 2582 }
879bfdc2
DJ
2583 }
2584 else
2585 {
4a64f543 2586 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2587 Shall we set a breakpoint at the LMA? */
2588 if (!overlay_events_enabled)
2589 {
2590 /* Yes -- overlay event support is not active,
2591 so we must try to set a breakpoint at the LMA.
2592 This will not work for a hardware breakpoint. */
35df4500 2593 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2594 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2595 bl->owner->number);
879bfdc2
DJ
2596 else
2597 {
35df4500
TJB
2598 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2599 bl->section);
879bfdc2 2600 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2601 bl->overlay_target_info = bl->target_info;
2602 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2603
2604 /* No overlay handling: just set the breakpoint. */
2605 TRY_CATCH (e, RETURN_MASK_ALL)
2606 {
2607 int val;
2608
2609 val = target_insert_breakpoint (bl->gdbarch,
2610 &bl->overlay_target_info);
2611 if (val)
2612 bp_err = GENERIC_ERROR;
2613 }
2614 if (e.reason < 0)
2615 {
2616 bp_err = e.error;
2617 bp_err_message = e.message;
2618 }
2619
2620 if (bp_err != GDB_NO_ERROR)
99361f52 2621 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2622 "Overlay breakpoint %d "
2623 "failed: in ROM?\n",
35df4500 2624 bl->owner->number);
879bfdc2
DJ
2625 }
2626 }
2627 /* Shall we set a breakpoint at the VMA? */
35df4500 2628 if (section_is_mapped (bl->section))
879bfdc2
DJ
2629 {
2630 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2631 TRY_CATCH (e, RETURN_MASK_ALL)
2632 {
0000e5cc
PA
2633 int val;
2634
dd61ec5c 2635 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2636 if (val)
2637 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2638 }
2639 if (e.reason < 0)
2640 {
0000e5cc
PA
2641 bp_err = e.error;
2642 bp_err_message = e.message;
dd61ec5c 2643 }
879bfdc2
DJ
2644 }
2645 else
2646 {
2647 /* No. This breakpoint will not be inserted.
2648 No error, but do not mark the bp as 'inserted'. */
2649 return 0;
2650 }
2651 }
2652
0000e5cc 2653 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2654 {
2655 /* Can't set the breakpoint. */
0000e5cc
PA
2656
2657 /* In some cases, we might not be able to insert a
2658 breakpoint in a shared library that has already been
2659 removed, but we have not yet processed the shlib unload
2660 event. Unfortunately, some targets that implement
076855f9
PA
2661 breakpoint insertion themselves can't tell why the
2662 breakpoint insertion failed (e.g., the remote target
2663 doesn't define error codes), so we must treat generic
2664 errors as memory errors. */
0000e5cc 2665 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2666 && bl->loc_type == bp_loc_software_breakpoint
08351840
PA
2667 && (solib_name_from_address (bl->pspace, bl->address)
2668 || userloaded_objfile_contains_address_p (bl->pspace,
2669 bl->address)))
879bfdc2 2670 {
4a64f543 2671 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2672 bl->shlib_disabled = 1;
8d3788bd 2673 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2674 if (!*disabled_breaks)
2675 {
2676 fprintf_unfiltered (tmp_error_stream,
2677 "Cannot insert breakpoint %d.\n",
2678 bl->owner->number);
2679 fprintf_unfiltered (tmp_error_stream,
2680 "Temporarily disabling shared "
2681 "library breakpoints:\n");
2682 }
2683 *disabled_breaks = 1;
879bfdc2 2684 fprintf_unfiltered (tmp_error_stream,
35df4500 2685 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2686 return 0;
879bfdc2
DJ
2687 }
2688 else
879bfdc2 2689 {
35df4500 2690 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2691 {
0000e5cc
PA
2692 *hw_breakpoint_error = 1;
2693 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2694 fprintf_unfiltered (tmp_error_stream,
2695 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2696 bl->owner->number, bp_err_message ? ":" : ".\n");
2697 if (bp_err_message != NULL)
2698 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2699 }
2700 else
2701 {
0000e5cc
PA
2702 if (bp_err_message == NULL)
2703 {
2704 char *message
2705 = memory_error_message (TARGET_XFER_E_IO,
2706 bl->gdbarch, bl->address);
2707 struct cleanup *old_chain = make_cleanup (xfree, message);
2708
2709 fprintf_unfiltered (tmp_error_stream,
2710 "Cannot insert breakpoint %d.\n"
2711 "%s\n",
2712 bl->owner->number, message);
2713 do_cleanups (old_chain);
2714 }
2715 else
2716 {
2717 fprintf_unfiltered (tmp_error_stream,
2718 "Cannot insert breakpoint %d: %s\n",
2719 bl->owner->number,
2720 bp_err_message);
2721 }
879bfdc2 2722 }
0000e5cc 2723 return 1;
879bfdc2
DJ
2724
2725 }
2726 }
2727 else
35df4500 2728 bl->inserted = 1;
879bfdc2 2729
0000e5cc 2730 return 0;
879bfdc2
DJ
2731 }
2732
35df4500 2733 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2734 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2735 watchpoints. It's not clear that it's necessary... */
35df4500 2736 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2737 {
0000e5cc
PA
2738 int val;
2739
77b06cd7
TJB
2740 gdb_assert (bl->owner->ops != NULL
2741 && bl->owner->ops->insert_location != NULL);
2742
2743 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2744
2745 /* If trying to set a read-watchpoint, and it turns out it's not
2746 supported, try emulating one with an access watchpoint. */
35df4500 2747 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2748 {
2749 struct bp_location *loc, **loc_temp;
2750
2751 /* But don't try to insert it, if there's already another
2752 hw_access location that would be considered a duplicate
2753 of this one. */
2754 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2755 if (loc != bl
85d721b8 2756 && loc->watchpoint_type == hw_access
35df4500 2757 && watchpoint_locations_match (bl, loc))
85d721b8 2758 {
35df4500
TJB
2759 bl->duplicate = 1;
2760 bl->inserted = 1;
2761 bl->target_info = loc->target_info;
2762 bl->watchpoint_type = hw_access;
85d721b8
PA
2763 val = 0;
2764 break;
2765 }
2766
2767 if (val == 1)
2768 {
77b06cd7
TJB
2769 bl->watchpoint_type = hw_access;
2770 val = bl->owner->ops->insert_location (bl);
2771
2772 if (val)
2773 /* Back to the original value. */
2774 bl->watchpoint_type = hw_read;
85d721b8
PA
2775 }
2776 }
2777
35df4500 2778 bl->inserted = (val == 0);
879bfdc2
DJ
2779 }
2780
35df4500 2781 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2782 {
0000e5cc
PA
2783 int val;
2784
77b06cd7
TJB
2785 gdb_assert (bl->owner->ops != NULL
2786 && bl->owner->ops->insert_location != NULL);
2787
2788 val = bl->owner->ops->insert_location (bl);
2789 if (val)
2790 {
2791 bl->owner->enable_state = bp_disabled;
2792
2793 if (val == 1)
2794 warning (_("\
2795Error inserting catchpoint %d: Your system does not support this type\n\
2796of catchpoint."), bl->owner->number);
2797 else
2798 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2799 }
2800
2801 bl->inserted = (val == 0);
1640b821
DJ
2802
2803 /* We've already printed an error message if there was a problem
2804 inserting this catchpoint, and we've disabled the catchpoint,
2805 so just return success. */
2806 return 0;
879bfdc2
DJ
2807 }
2808
2809 return 0;
2810}
2811
6c95b8df
PA
2812/* This function is called when program space PSPACE is about to be
2813 deleted. It takes care of updating breakpoints to not reference
2814 PSPACE anymore. */
2815
2816void
2817breakpoint_program_space_exit (struct program_space *pspace)
2818{
2819 struct breakpoint *b, *b_temp;
876fa593 2820 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2821
2822 /* Remove any breakpoint that was set through this program space. */
2823 ALL_BREAKPOINTS_SAFE (b, b_temp)
2824 {
2825 if (b->pspace == pspace)
2826 delete_breakpoint (b);
2827 }
2828
2829 /* Breakpoints set through other program spaces could have locations
2830 bound to PSPACE as well. Remove those. */
876fa593 2831 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2832 {
2833 struct bp_location *tmp;
2834
2835 if (loc->pspace == pspace)
2836 {
2bdf28a0 2837 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2838 if (loc->owner->loc == loc)
2839 loc->owner->loc = loc->next;
2840 else
2841 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2842 if (tmp->next == loc)
2843 {
2844 tmp->next = loc->next;
2845 break;
2846 }
2847 }
2848 }
2849
2850 /* Now update the global location list to permanently delete the
2851 removed locations above. */
2852 update_global_location_list (0);
2853}
2854
74960c60
VP
2855/* Make sure all breakpoints are inserted in inferior.
2856 Throws exception on any error.
2857 A breakpoint that is already inserted won't be inserted
2858 again, so calling this function twice is safe. */
2859void
2860insert_breakpoints (void)
2861{
2862 struct breakpoint *bpt;
2863
2864 ALL_BREAKPOINTS (bpt)
2865 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2866 {
2867 struct watchpoint *w = (struct watchpoint *) bpt;
2868
2869 update_watchpoint (w, 0 /* don't reparse. */);
2870 }
74960c60 2871
b60e7edf 2872 update_global_location_list (1);
74960c60 2873
c35b1492
PA
2874 /* update_global_location_list does not insert breakpoints when
2875 always_inserted_mode is not enabled. Explicitly insert them
2876 now. */
2877 if (!breakpoints_always_inserted_mode ())
74960c60
VP
2878 insert_breakpoint_locations ();
2879}
2880
20388dd6
YQ
2881/* Invoke CALLBACK for each of bp_location. */
2882
2883void
2884iterate_over_bp_locations (walk_bp_location_callback callback)
2885{
2886 struct bp_location *loc, **loc_tmp;
2887
2888 ALL_BP_LOCATIONS (loc, loc_tmp)
2889 {
2890 callback (loc, NULL);
2891 }
2892}
2893
b775012e
LM
2894/* This is used when we need to synch breakpoint conditions between GDB and the
2895 target. It is the case with deleting and disabling of breakpoints when using
2896 always-inserted mode. */
2897
2898static void
2899update_inserted_breakpoint_locations (void)
2900{
2901 struct bp_location *bl, **blp_tmp;
2902 int error_flag = 0;
2903 int val = 0;
2904 int disabled_breaks = 0;
2905 int hw_breakpoint_error = 0;
dd61ec5c 2906 int hw_bp_details_reported = 0;
b775012e
LM
2907
2908 struct ui_file *tmp_error_stream = mem_fileopen ();
2909 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2910
2911 /* Explicitly mark the warning -- this will only be printed if
2912 there was an error. */
2913 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2914
2915 save_current_space_and_thread ();
2916
2917 ALL_BP_LOCATIONS (bl, blp_tmp)
2918 {
2919 /* We only want to update software breakpoints and hardware
2920 breakpoints. */
2921 if (!is_breakpoint (bl->owner))
2922 continue;
2923
2924 /* We only want to update locations that are already inserted
2925 and need updating. This is to avoid unwanted insertion during
2926 deletion of breakpoints. */
2927 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2928 continue;
2929
2930 switch_to_program_space_and_thread (bl->pspace);
2931
2932 /* For targets that support global breakpoints, there's no need
2933 to select an inferior to insert breakpoint to. In fact, even
2934 if we aren't attached to any process yet, we should still
2935 insert breakpoints. */
f5656ead 2936 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2937 && ptid_equal (inferior_ptid, null_ptid))
2938 continue;
2939
2940 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 2941 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2942 if (val)
2943 error_flag = val;
2944 }
2945
2946 if (error_flag)
2947 {
2948 target_terminal_ours_for_output ();
2949 error_stream (tmp_error_stream);
2950 }
2951
2952 do_cleanups (cleanups);
2953}
2954
c30eee59 2955/* Used when starting or continuing the program. */
c906108c 2956
74960c60
VP
2957static void
2958insert_breakpoint_locations (void)
c906108c 2959{
a5606eee 2960 struct breakpoint *bpt;
35df4500 2961 struct bp_location *bl, **blp_tmp;
eacd795a 2962 int error_flag = 0;
c906108c 2963 int val = 0;
3fbb6ffa 2964 int disabled_breaks = 0;
81d0cc19 2965 int hw_breakpoint_error = 0;
dd61ec5c 2966 int hw_bp_error_explained_already = 0;
c906108c 2967
81d0cc19 2968 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2969 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2970
81d0cc19
GS
2971 /* Explicitly mark the warning -- this will only be printed if
2972 there was an error. */
2973 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2974
2975 save_current_space_and_thread ();
2976
35df4500 2977 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2978 {
b775012e 2979 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2980 continue;
2981
4a64f543
MS
2982 /* There is no point inserting thread-specific breakpoints if
2983 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2984 has BL->OWNER always non-NULL. */
35df4500
TJB
2985 if (bl->owner->thread != -1
2986 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2987 continue;
2988
35df4500 2989 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2990
2991 /* For targets that support global breakpoints, there's no need
2992 to select an inferior to insert breakpoint to. In fact, even
2993 if we aren't attached to any process yet, we should still
2994 insert breakpoints. */
f5656ead 2995 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
2996 && ptid_equal (inferior_ptid, null_ptid))
2997 continue;
2998
3fbb6ffa 2999 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3000 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3001 if (val)
eacd795a 3002 error_flag = val;
879bfdc2 3003 }
c906108c 3004
4a64f543
MS
3005 /* If we failed to insert all locations of a watchpoint, remove
3006 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3007 ALL_BREAKPOINTS (bpt)
3008 {
3009 int some_failed = 0;
3010 struct bp_location *loc;
3011
3012 if (!is_hardware_watchpoint (bpt))
3013 continue;
3014
d6b74ac4 3015 if (!breakpoint_enabled (bpt))
a5606eee 3016 continue;
74960c60
VP
3017
3018 if (bpt->disposition == disp_del_at_next_stop)
3019 continue;
a5606eee
VP
3020
3021 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3022 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3023 {
3024 some_failed = 1;
3025 break;
3026 }
3027 if (some_failed)
3028 {
3029 for (loc = bpt->loc; loc; loc = loc->next)
3030 if (loc->inserted)
3031 remove_breakpoint (loc, mark_uninserted);
3032
3033 hw_breakpoint_error = 1;
3034 fprintf_unfiltered (tmp_error_stream,
3035 "Could not insert hardware watchpoint %d.\n",
3036 bpt->number);
eacd795a 3037 error_flag = -1;
a5606eee
VP
3038 }
3039 }
3040
eacd795a 3041 if (error_flag)
81d0cc19
GS
3042 {
3043 /* If a hardware breakpoint or watchpoint was inserted, add a
3044 message about possibly exhausted resources. */
dd61ec5c 3045 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3046 {
c6510018
MS
3047 fprintf_unfiltered (tmp_error_stream,
3048 "Could not insert hardware breakpoints:\n\
3049You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3050 }
81d0cc19
GS
3051 target_terminal_ours_for_output ();
3052 error_stream (tmp_error_stream);
3053 }
f7545552
TT
3054
3055 do_cleanups (cleanups);
c906108c
SS
3056}
3057
c30eee59
TJB
3058/* Used when the program stops.
3059 Returns zero if successful, or non-zero if there was a problem
3060 removing a breakpoint location. */
3061
c906108c 3062int
fba45db2 3063remove_breakpoints (void)
c906108c 3064{
35df4500 3065 struct bp_location *bl, **blp_tmp;
3a1bae8e 3066 int val = 0;
c906108c 3067
35df4500 3068 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3069 {
1e4d1764 3070 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3071 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3072 }
3a1bae8e 3073 return val;
c906108c
SS
3074}
3075
49fa26b0
PA
3076/* When a thread exits, remove breakpoints that are related to
3077 that thread. */
3078
3079static void
3080remove_threaded_breakpoints (struct thread_info *tp, int silent)
3081{
3082 struct breakpoint *b, *b_tmp;
3083
3084 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3085 {
96181529 3086 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3087 {
3088 b->disposition = disp_del_at_next_stop;
3089
3090 printf_filtered (_("\
46ecd527 3091Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3092 b->number, tp->num);
3093
3094 /* Hide it from the user. */
3095 b->number = 0;
3096 }
3097 }
3098}
3099
6c95b8df
PA
3100/* Remove breakpoints of process PID. */
3101
3102int
3103remove_breakpoints_pid (int pid)
3104{
35df4500 3105 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3106 int val;
3107 struct inferior *inf = find_inferior_pid (pid);
3108
35df4500 3109 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3110 {
35df4500 3111 if (bl->pspace != inf->pspace)
6c95b8df
PA
3112 continue;
3113
d3ce09f5
SS
3114 if (bl->owner->type == bp_dprintf)
3115 continue;
3116
35df4500 3117 if (bl->inserted)
6c95b8df 3118 {
35df4500 3119 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3120 if (val != 0)
3121 return val;
3122 }
3123 }
3124 return 0;
3125}
3126
c906108c 3127int
fba45db2 3128reattach_breakpoints (int pid)
c906108c 3129{
6c95b8df 3130 struct cleanup *old_chain;
35df4500 3131 struct bp_location *bl, **blp_tmp;
c906108c 3132 int val;
86b887df 3133 struct ui_file *tmp_error_stream;
dd61ec5c 3134 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3135 struct inferior *inf;
3136 struct thread_info *tp;
3137
3138 tp = any_live_thread_of_process (pid);
3139 if (tp == NULL)
3140 return 1;
3141
3142 inf = find_inferior_pid (pid);
3143 old_chain = save_inferior_ptid ();
3144
3145 inferior_ptid = tp->ptid;
a4954f26 3146
86b887df 3147 tmp_error_stream = mem_fileopen ();
a4954f26 3148 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3149
35df4500 3150 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3151 {
35df4500 3152 if (bl->pspace != inf->pspace)
6c95b8df
PA
3153 continue;
3154
35df4500 3155 if (bl->inserted)
c5aa993b 3156 {
35df4500 3157 bl->inserted = 0;
dd61ec5c 3158 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3159 if (val != 0)
3160 {
ce696e05 3161 do_cleanups (old_chain);
c5aa993b
JM
3162 return val;
3163 }
3164 }
3165 }
ce696e05 3166 do_cleanups (old_chain);
c906108c
SS
3167 return 0;
3168}
3169
e58b0e63
PA
3170static int internal_breakpoint_number = -1;
3171
84f4c1fe
PM
3172/* Set the breakpoint number of B, depending on the value of INTERNAL.
3173 If INTERNAL is non-zero, the breakpoint number will be populated
3174 from internal_breakpoint_number and that variable decremented.
e5dd4106 3175 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3176 breakpoint_count and that value incremented. Internal breakpoints
3177 do not set the internal var bpnum. */
3178static void
3179set_breakpoint_number (int internal, struct breakpoint *b)
3180{
3181 if (internal)
3182 b->number = internal_breakpoint_number--;
3183 else
3184 {
3185 set_breakpoint_count (breakpoint_count + 1);
3186 b->number = breakpoint_count;
3187 }
3188}
3189
e62c965a 3190static struct breakpoint *
a6d9a66e 3191create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3192 CORE_ADDR address, enum bptype type,
c0a91b2b 3193 const struct breakpoint_ops *ops)
e62c965a 3194{
e62c965a
PP
3195 struct symtab_and_line sal;
3196 struct breakpoint *b;
3197
4a64f543 3198 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3199
3200 sal.pc = address;
3201 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3202 sal.pspace = current_program_space;
e62c965a 3203
06edf0c0 3204 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3205 b->number = internal_breakpoint_number--;
3206 b->disposition = disp_donttouch;
3207
3208 return b;
3209}
3210
17450429
PP
3211static const char *const longjmp_names[] =
3212 {
3213 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3214 };
3215#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3216
3217/* Per-objfile data private to breakpoint.c. */
3218struct breakpoint_objfile_data
3219{
3220 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3221 struct bound_minimal_symbol overlay_msym;
17450429
PP
3222
3223 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3224 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3225
28106bc2
SDJ
3226 /* True if we have looked for longjmp probes. */
3227 int longjmp_searched;
3228
3229 /* SystemTap probe points for longjmp (if any). */
3230 VEC (probe_p) *longjmp_probes;
3231
17450429 3232 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3233 struct bound_minimal_symbol terminate_msym;
17450429
PP
3234
3235 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3236 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3237
3238 /* True if we have looked for exception probes. */
3239 int exception_searched;
3240
3241 /* SystemTap probe points for unwinding (if any). */
3242 VEC (probe_p) *exception_probes;
17450429
PP
3243};
3244
3245static const struct objfile_data *breakpoint_objfile_key;
3246
3247/* Minimal symbol not found sentinel. */
3248static struct minimal_symbol msym_not_found;
3249
3250/* Returns TRUE if MSYM point to the "not found" sentinel. */
3251
3252static int
3253msym_not_found_p (const struct minimal_symbol *msym)
3254{
3255 return msym == &msym_not_found;
3256}
3257
3258/* Return per-objfile data needed by breakpoint.c.
3259 Allocate the data if necessary. */
3260
3261static struct breakpoint_objfile_data *
3262get_breakpoint_objfile_data (struct objfile *objfile)
3263{
3264 struct breakpoint_objfile_data *bp_objfile_data;
3265
3266 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3267 if (bp_objfile_data == NULL)
3268 {
3269 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3270 sizeof (*bp_objfile_data));
3271
3272 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3273 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3274 }
3275 return bp_objfile_data;
3276}
3277
28106bc2
SDJ
3278static void
3279free_breakpoint_probes (struct objfile *obj, void *data)
3280{
3281 struct breakpoint_objfile_data *bp_objfile_data = data;
3282
3283 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3284 VEC_free (probe_p, bp_objfile_data->exception_probes);
3285}
3286
e62c965a 3287static void
af02033e 3288create_overlay_event_breakpoint (void)
e62c965a 3289{
69de3c6a 3290 struct objfile *objfile;
af02033e 3291 const char *const func_name = "_ovly_debug_event";
e62c965a 3292
69de3c6a
PP
3293 ALL_OBJFILES (objfile)
3294 {
3295 struct breakpoint *b;
17450429
PP
3296 struct breakpoint_objfile_data *bp_objfile_data;
3297 CORE_ADDR addr;
69de3c6a 3298
17450429
PP
3299 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3300
3b7344d5 3301 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3302 continue;
3303
3b7344d5 3304 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3305 {
3b7344d5 3306 struct bound_minimal_symbol m;
17450429
PP
3307
3308 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3309 if (m.minsym == NULL)
17450429
PP
3310 {
3311 /* Avoid future lookups in this objfile. */
3b7344d5 3312 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3313 continue;
3314 }
3315 bp_objfile_data->overlay_msym = m;
3316 }
e62c965a 3317
77e371c0 3318 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3319 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3320 bp_overlay_event,
3321 &internal_breakpoint_ops);
69de3c6a 3322 b->addr_string = xstrdup (func_name);
e62c965a 3323
69de3c6a
PP
3324 if (overlay_debugging == ovly_auto)
3325 {
3326 b->enable_state = bp_enabled;
3327 overlay_events_enabled = 1;
3328 }
3329 else
3330 {
3331 b->enable_state = bp_disabled;
3332 overlay_events_enabled = 0;
3333 }
e62c965a
PP
3334 }
3335 update_global_location_list (1);
3336}
3337
0fd8e87f 3338static void
af02033e 3339create_longjmp_master_breakpoint (void)
0fd8e87f 3340{
6c95b8df 3341 struct program_space *pspace;
6c95b8df
PA
3342 struct cleanup *old_chain;
3343
3344 old_chain = save_current_program_space ();
0fd8e87f 3345
6c95b8df 3346 ALL_PSPACES (pspace)
af02033e
PP
3347 {
3348 struct objfile *objfile;
3349
3350 set_current_program_space (pspace);
3351
3352 ALL_OBJFILES (objfile)
0fd8e87f 3353 {
af02033e
PP
3354 int i;
3355 struct gdbarch *gdbarch;
17450429 3356 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3357
af02033e 3358 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3359
17450429
PP
3360 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3361
28106bc2
SDJ
3362 if (!bp_objfile_data->longjmp_searched)
3363 {
25f9533e
SDJ
3364 VEC (probe_p) *ret;
3365
3366 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3367 if (ret != NULL)
3368 {
3369 /* We are only interested in checking one element. */
3370 struct probe *p = VEC_index (probe_p, ret, 0);
3371
3372 if (!can_evaluate_probe_arguments (p))
3373 {
3374 /* We cannot use the probe interface here, because it does
3375 not know how to evaluate arguments. */
3376 VEC_free (probe_p, ret);
3377 ret = NULL;
3378 }
3379 }
3380 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3381 bp_objfile_data->longjmp_searched = 1;
3382 }
3383
3384 if (bp_objfile_data->longjmp_probes != NULL)
3385 {
3386 int i;
3387 struct probe *probe;
3388 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3389
3390 for (i = 0;
3391 VEC_iterate (probe_p,
3392 bp_objfile_data->longjmp_probes,
3393 i, probe);
3394 ++i)
3395 {
3396 struct breakpoint *b;
3397
729662a5
TT
3398 b = create_internal_breakpoint (gdbarch,
3399 get_probe_address (probe,
3400 objfile),
28106bc2
SDJ
3401 bp_longjmp_master,
3402 &internal_breakpoint_ops);
3403 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3404 b->enable_state = bp_disabled;
3405 }
3406
3407 continue;
3408 }
3409
0569175e
TSD
3410 if (!gdbarch_get_longjmp_target_p (gdbarch))
3411 continue;
3412
17450429 3413 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3414 {
3415 struct breakpoint *b;
af02033e 3416 const char *func_name;
17450429 3417 CORE_ADDR addr;
6c95b8df 3418
3b7344d5 3419 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3420 continue;
0fd8e87f 3421
17450429 3422 func_name = longjmp_names[i];
3b7344d5 3423 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3424 {
3b7344d5 3425 struct bound_minimal_symbol m;
17450429
PP
3426
3427 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3428 if (m.minsym == NULL)
17450429
PP
3429 {
3430 /* Prevent future lookups in this objfile. */
3b7344d5 3431 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3432 continue;
3433 }
3434 bp_objfile_data->longjmp_msym[i] = m;
3435 }
3436
77e371c0 3437 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3438 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3439 &internal_breakpoint_ops);
af02033e
PP
3440 b->addr_string = xstrdup (func_name);
3441 b->enable_state = bp_disabled;
3442 }
0fd8e87f 3443 }
af02033e 3444 }
0fd8e87f 3445 update_global_location_list (1);
6c95b8df
PA
3446
3447 do_cleanups (old_chain);
0fd8e87f
UW
3448}
3449
af02033e 3450/* Create a master std::terminate breakpoint. */
aa7d318d 3451static void
af02033e 3452create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3453{
3454 struct program_space *pspace;
aa7d318d 3455 struct cleanup *old_chain;
af02033e 3456 const char *const func_name = "std::terminate()";
aa7d318d
TT
3457
3458 old_chain = save_current_program_space ();
3459
3460 ALL_PSPACES (pspace)
17450429
PP
3461 {
3462 struct objfile *objfile;
3463 CORE_ADDR addr;
3464
3465 set_current_program_space (pspace);
3466
aa7d318d
TT
3467 ALL_OBJFILES (objfile)
3468 {
3469 struct breakpoint *b;
17450429 3470 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3471
17450429 3472 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3473
3b7344d5 3474 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3475 continue;
3476
3b7344d5 3477 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3478 {
3b7344d5 3479 struct bound_minimal_symbol m;
17450429
PP
3480
3481 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3482 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3483 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3484 {
3485 /* Prevent future lookups in this objfile. */
3b7344d5 3486 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3487 continue;
3488 }
3489 bp_objfile_data->terminate_msym = m;
3490 }
aa7d318d 3491
77e371c0 3492 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3493 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3494 bp_std_terminate_master,
3495 &internal_breakpoint_ops);
aa7d318d
TT
3496 b->addr_string = xstrdup (func_name);
3497 b->enable_state = bp_disabled;
3498 }
17450429
PP
3499 }
3500
aa7d318d
TT
3501 update_global_location_list (1);
3502
3503 do_cleanups (old_chain);
3504}
3505
186c406b
TT
3506/* Install a master breakpoint on the unwinder's debug hook. */
3507
70221824 3508static void
186c406b
TT
3509create_exception_master_breakpoint (void)
3510{
3511 struct objfile *objfile;
17450429 3512 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3513
3514 ALL_OBJFILES (objfile)
3515 {
17450429
PP
3516 struct breakpoint *b;
3517 struct gdbarch *gdbarch;
3518 struct breakpoint_objfile_data *bp_objfile_data;
3519 CORE_ADDR addr;
3520
3521 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3522
28106bc2
SDJ
3523 /* We prefer the SystemTap probe point if it exists. */
3524 if (!bp_objfile_data->exception_searched)
3525 {
25f9533e
SDJ
3526 VEC (probe_p) *ret;
3527
3528 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3529
3530 if (ret != NULL)
3531 {
3532 /* We are only interested in checking one element. */
3533 struct probe *p = VEC_index (probe_p, ret, 0);
3534
3535 if (!can_evaluate_probe_arguments (p))
3536 {
3537 /* We cannot use the probe interface here, because it does
3538 not know how to evaluate arguments. */
3539 VEC_free (probe_p, ret);
3540 ret = NULL;
3541 }
3542 }
3543 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3544 bp_objfile_data->exception_searched = 1;
3545 }
3546
3547 if (bp_objfile_data->exception_probes != NULL)
3548 {
3549 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3550 int i;
3551 struct probe *probe;
3552
3553 for (i = 0;
3554 VEC_iterate (probe_p,
3555 bp_objfile_data->exception_probes,
3556 i, probe);
3557 ++i)
3558 {
3559 struct breakpoint *b;
3560
729662a5
TT
3561 b = create_internal_breakpoint (gdbarch,
3562 get_probe_address (probe,
3563 objfile),
28106bc2
SDJ
3564 bp_exception_master,
3565 &internal_breakpoint_ops);
3566 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3567 b->enable_state = bp_disabled;
3568 }
3569
3570 continue;
3571 }
3572
3573 /* Otherwise, try the hook function. */
3574
3b7344d5 3575 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3576 continue;
3577
3578 gdbarch = get_objfile_arch (objfile);
186c406b 3579
3b7344d5 3580 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3581 {
3b7344d5 3582 struct bound_minimal_symbol debug_hook;
186c406b 3583
17450429 3584 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3585 if (debug_hook.minsym == NULL)
17450429 3586 {
3b7344d5 3587 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3588 continue;
3589 }
3590
3591 bp_objfile_data->exception_msym = debug_hook;
186c406b 3592 }
17450429 3593
77e371c0 3594 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3595 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3596 &current_target);
06edf0c0
PA
3597 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3598 &internal_breakpoint_ops);
17450429
PP
3599 b->addr_string = xstrdup (func_name);
3600 b->enable_state = bp_disabled;
186c406b
TT
3601 }
3602
3603 update_global_location_list (1);
3604}
3605
c906108c 3606void
fba45db2 3607update_breakpoints_after_exec (void)
c906108c 3608{
35df4500 3609 struct breakpoint *b, *b_tmp;
876fa593 3610 struct bp_location *bploc, **bplocp_tmp;
c906108c 3611
25b22b0a
PA
3612 /* We're about to delete breakpoints from GDB's lists. If the
3613 INSERTED flag is true, GDB will try to lift the breakpoints by
3614 writing the breakpoints' "shadow contents" back into memory. The
3615 "shadow contents" are NOT valid after an exec, so GDB should not
3616 do that. Instead, the target is responsible from marking
3617 breakpoints out as soon as it detects an exec. We don't do that
3618 here instead, because there may be other attempts to delete
3619 breakpoints after detecting an exec and before reaching here. */
876fa593 3620 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3621 if (bploc->pspace == current_program_space)
3622 gdb_assert (!bploc->inserted);
c906108c 3623
35df4500 3624 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3625 {
6c95b8df
PA
3626 if (b->pspace != current_program_space)
3627 continue;
3628
4a64f543 3629 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3630 if (b->type == bp_shlib_event)
3631 {
3632 delete_breakpoint (b);
3633 continue;
3634 }
c906108c 3635
4a64f543 3636 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3637 if (b->type == bp_jit_event)
3638 {
3639 delete_breakpoint (b);
3640 continue;
3641 }
3642
1900040c 3643 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3644 as must overlay event and longjmp master breakpoints. */
3645 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3646 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3647 || b->type == bp_exception_master)
c4093a6a
JM
3648 {
3649 delete_breakpoint (b);
3650 continue;
3651 }
3652
4a64f543 3653 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3654 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3655 {
3656 delete_breakpoint (b);
3657 continue;
3658 }
3659
611c83ae
PA
3660 /* Longjmp and longjmp-resume breakpoints are also meaningless
3661 after an exec. */
186c406b 3662 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3663 || b->type == bp_longjmp_call_dummy
186c406b 3664 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3665 {
3666 delete_breakpoint (b);
3667 continue;
3668 }
3669
ce78b96d
JB
3670 if (b->type == bp_catchpoint)
3671 {
3672 /* For now, none of the bp_catchpoint breakpoints need to
3673 do anything at this point. In the future, if some of
3674 the catchpoints need to something, we will need to add
3675 a new method, and call this method from here. */
3676 continue;
3677 }
3678
c5aa993b
JM
3679 /* bp_finish is a special case. The only way we ought to be able
3680 to see one of these when an exec() has happened, is if the user
3681 caught a vfork, and then said "finish". Ordinarily a finish just
3682 carries them to the call-site of the current callee, by setting
3683 a temporary bp there and resuming. But in this case, the finish
3684 will carry them entirely through the vfork & exec.
3685
3686 We don't want to allow a bp_finish to remain inserted now. But
3687 we can't safely delete it, 'cause finish_command has a handle to
3688 the bp on a bpstat, and will later want to delete it. There's a
3689 chance (and I've seen it happen) that if we delete the bp_finish
3690 here, that its storage will get reused by the time finish_command
3691 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3692 We really must allow finish_command to delete a bp_finish.
3693
e5dd4106 3694 In the absence of a general solution for the "how do we know
53a5351d
JM
3695 it's safe to delete something others may have handles to?"
3696 problem, what we'll do here is just uninsert the bp_finish, and
3697 let finish_command delete it.
3698
3699 (We know the bp_finish is "doomed" in the sense that it's
3700 momentary, and will be deleted as soon as finish_command sees
3701 the inferior stopped. So it doesn't matter that the bp's
3702 address is probably bogus in the new a.out, unlike e.g., the
3703 solib breakpoints.) */
c5aa993b 3704
c5aa993b
JM
3705 if (b->type == bp_finish)
3706 {
3707 continue;
3708 }
3709
3710 /* Without a symbolic address, we have little hope of the
3711 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3712 a.out. */
c5aa993b
JM
3713 if (b->addr_string == NULL)
3714 {
3715 delete_breakpoint (b);
3716 continue;
3717 }
c5aa993b 3718 }
1900040c 3719 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3720 create_overlay_event_breakpoint ();
3721 create_longjmp_master_breakpoint ();
3722 create_std_terminate_master_breakpoint ();
186c406b 3723 create_exception_master_breakpoint ();
c906108c
SS
3724}
3725
3726int
d80ee84f 3727detach_breakpoints (ptid_t ptid)
c906108c 3728{
35df4500 3729 struct bp_location *bl, **blp_tmp;
3a1bae8e 3730 int val = 0;
ce696e05 3731 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3732 struct inferior *inf = current_inferior ();
c5aa993b 3733
dfd4cc63 3734 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3735 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3736
6c95b8df 3737 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3738 inferior_ptid = ptid;
35df4500 3739 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3740 {
35df4500 3741 if (bl->pspace != inf->pspace)
6c95b8df
PA
3742 continue;
3743
bd9673a4
PW
3744 /* This function must physically remove breakpoints locations
3745 from the specified ptid, without modifying the breakpoint
3746 package's state. Locations of type bp_loc_other are only
3747 maintained at GDB side. So, there is no need to remove
3748 these bp_loc_other locations. Moreover, removing these
3749 would modify the breakpoint package's state. */
3750 if (bl->loc_type == bp_loc_other)
3751 continue;
3752
35df4500
TJB
3753 if (bl->inserted)
3754 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3755 }
d03285ec
UW
3756
3757 /* Detach single-step breakpoints as well. */
3758 detach_single_step_breakpoints ();
3759
ce696e05 3760 do_cleanups (old_chain);
3a1bae8e 3761 return val;
c906108c
SS
3762}
3763
35df4500 3764/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3765 Note that this is used to detach breakpoints from a child fork.
3766 When we get here, the child isn't in the inferior list, and neither
3767 do we have objects to represent its address space --- we should
35df4500 3768 *not* look at bl->pspace->aspace here. */
6c95b8df 3769
c906108c 3770static int
35df4500 3771remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3772{
3773 int val;
c5aa993b 3774
35df4500
TJB
3775 /* BL is never in moribund_locations by our callers. */
3776 gdb_assert (bl->owner != NULL);
2bdf28a0 3777
35df4500 3778 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3779 /* Permanent breakpoints cannot be inserted or removed. */
3780 return 0;
3781
74960c60
VP
3782 /* The type of none suggests that owner is actually deleted.
3783 This should not ever happen. */
35df4500 3784 gdb_assert (bl->owner->type != bp_none);
0bde7532 3785
35df4500
TJB
3786 if (bl->loc_type == bp_loc_software_breakpoint
3787 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3788 {
c02f5703
MS
3789 /* "Normal" instruction breakpoint: either the standard
3790 trap-instruction bp (bp_breakpoint), or a
3791 bp_hardware_breakpoint. */
3792
3793 /* First check to see if we have to handle an overlay. */
3794 if (overlay_debugging == ovly_off
35df4500
TJB
3795 || bl->section == NULL
3796 || !(section_is_overlay (bl->section)))
c02f5703
MS
3797 {
3798 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3799
3800 /* If we're trying to uninsert a memory breakpoint that we
3801 know is set in a dynamic object that is marked
3802 shlib_disabled, then either the dynamic object was
3803 removed with "remove-symbol-file" or with
3804 "nosharedlibrary". In the former case, we don't know
3805 whether another dynamic object might have loaded over the
3806 breakpoint's address -- the user might well let us know
3807 about it next with add-symbol-file (the whole point of
3808 OBJF_USERLOADED is letting the user manually maintain a
3809 list of dynamically loaded objects). If we have the
3810 breakpoint's shadow memory, that is, this is a software
3811 breakpoint managed by GDB, check whether the breakpoint
3812 is still inserted in memory, to avoid overwriting wrong
3813 code with stale saved shadow contents. Note that HW
3814 breakpoints don't have shadow memory, as they're
3815 implemented using a mechanism that is not dependent on
3816 being able to modify the target's memory, and as such
3817 they should always be removed. */
3818 if (bl->shlib_disabled
3819 && bl->target_info.shadow_len != 0
3820 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3821 val = 0;
3822 else
3823 val = bl->owner->ops->remove_location (bl);
c02f5703 3824 }
c906108c
SS
3825 else
3826 {
4a64f543 3827 /* This breakpoint is in an overlay section.
c02f5703
MS
3828 Did we set a breakpoint at the LMA? */
3829 if (!overlay_events_enabled)
3830 {
3831 /* Yes -- overlay event support is not active, so we
3832 should have set a breakpoint at the LMA. Remove it.
3833 */
c02f5703
MS
3834 /* Ignore any failures: if the LMA is in ROM, we will
3835 have already warned when we failed to insert it. */
35df4500
TJB
3836 if (bl->loc_type == bp_loc_hardware_breakpoint)
3837 target_remove_hw_breakpoint (bl->gdbarch,
3838 &bl->overlay_target_info);
c02f5703 3839 else
35df4500
TJB
3840 target_remove_breakpoint (bl->gdbarch,
3841 &bl->overlay_target_info);
c02f5703
MS
3842 }
3843 /* Did we set a breakpoint at the VMA?
3844 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3845 if (bl->inserted)
c906108c 3846 {
c02f5703
MS
3847 /* Yes -- remove it. Previously we did not bother to
3848 remove the breakpoint if the section had been
3849 unmapped, but let's not rely on that being safe. We
3850 don't know what the overlay manager might do. */
aa67235e
UW
3851
3852 /* However, we should remove *software* breakpoints only
3853 if the section is still mapped, or else we overwrite
3854 wrong code with the saved shadow contents. */
348d480f
PA
3855 if (bl->loc_type == bp_loc_hardware_breakpoint
3856 || section_is_mapped (bl->section))
3857 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3858 else
3859 val = 0;
c906108c 3860 }
c02f5703
MS
3861 else
3862 {
3863 /* No -- not inserted, so no need to remove. No error. */
3864 val = 0;
3865 }
c906108c 3866 }
879d1e6b 3867
08351840
PA
3868 /* In some cases, we might not be able to remove a breakpoint in
3869 a shared library that has already been removed, but we have
3870 not yet processed the shlib unload event. Similarly for an
3871 unloaded add-symbol-file object - the user might not yet have
3872 had the chance to remove-symbol-file it. shlib_disabled will
3873 be set if the library/object has already been removed, but
3874 the breakpoint hasn't been uninserted yet, e.g., after
3875 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3876 always-inserted mode. */
076855f9 3877 if (val
08351840
PA
3878 && (bl->loc_type == bp_loc_software_breakpoint
3879 && (bl->shlib_disabled
3880 || solib_name_from_address (bl->pspace, bl->address)
3881 || userloaded_objfile_contains_address_p (bl->pspace,
3882 bl->address))))
879d1e6b
UW
3883 val = 0;
3884
c906108c
SS
3885 if (val)
3886 return val;
35df4500 3887 bl->inserted = (is == mark_inserted);
c906108c 3888 }
35df4500 3889 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3890 {
77b06cd7
TJB
3891 gdb_assert (bl->owner->ops != NULL
3892 && bl->owner->ops->remove_location != NULL);
3893
35df4500 3894 bl->inserted = (is == mark_inserted);
77b06cd7 3895 bl->owner->ops->remove_location (bl);
2e70b7b9 3896
c906108c 3897 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3898 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 3899 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3900 bl->owner->number);
c906108c 3901 }
35df4500
TJB
3902 else if (bl->owner->type == bp_catchpoint
3903 && breakpoint_enabled (bl->owner)
3904 && !bl->duplicate)
ce78b96d 3905 {
77b06cd7
TJB
3906 gdb_assert (bl->owner->ops != NULL
3907 && bl->owner->ops->remove_location != NULL);
ce78b96d 3908
77b06cd7 3909 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
3910 if (val)
3911 return val;
77b06cd7 3912
35df4500 3913 bl->inserted = (is == mark_inserted);
ce78b96d 3914 }
c906108c
SS
3915
3916 return 0;
3917}
3918
6c95b8df 3919static int
35df4500 3920remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
3921{
3922 int ret;
3923 struct cleanup *old_chain;
3924
35df4500
TJB
3925 /* BL is never in moribund_locations by our callers. */
3926 gdb_assert (bl->owner != NULL);
2bdf28a0 3927
35df4500 3928 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
3929 /* Permanent breakpoints cannot be inserted or removed. */
3930 return 0;
3931
3932 /* The type of none suggests that owner is actually deleted.
3933 This should not ever happen. */
35df4500 3934 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
3935
3936 old_chain = save_current_space_and_thread ();
3937
35df4500 3938 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3939
35df4500 3940 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
3941
3942 do_cleanups (old_chain);
3943 return ret;
3944}
3945
c906108c
SS
3946/* Clear the "inserted" flag in all breakpoints. */
3947
25b22b0a 3948void
fba45db2 3949mark_breakpoints_out (void)
c906108c 3950{
35df4500 3951 struct bp_location *bl, **blp_tmp;
c906108c 3952
35df4500
TJB
3953 ALL_BP_LOCATIONS (bl, blp_tmp)
3954 if (bl->pspace == current_program_space)
3955 bl->inserted = 0;
c906108c
SS
3956}
3957
53a5351d
JM
3958/* Clear the "inserted" flag in all breakpoints and delete any
3959 breakpoints which should go away between runs of the program.
c906108c
SS
3960
3961 Plus other such housekeeping that has to be done for breakpoints
3962 between runs.
3963
53a5351d
JM
3964 Note: this function gets called at the end of a run (by
3965 generic_mourn_inferior) and when a run begins (by
4a64f543 3966 init_wait_for_inferior). */
c906108c
SS
3967
3968
3969
3970void
fba45db2 3971breakpoint_init_inferior (enum inf_context context)
c906108c 3972{
35df4500
TJB
3973 struct breakpoint *b, *b_tmp;
3974 struct bp_location *bl, **blp_tmp;
1c5cfe86 3975 int ix;
6c95b8df 3976 struct program_space *pspace = current_program_space;
c906108c 3977
50c71eaf
PA
3978 /* If breakpoint locations are shared across processes, then there's
3979 nothing to do. */
f5656ead 3980 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3981 return;
3982
35df4500 3983 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3984 {
35df4500
TJB
3985 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3986 if (bl->pspace == pspace
3987 && bl->owner->enable_state != bp_permanent)
3988 bl->inserted = 0;
6c95b8df 3989 }
075f6582 3990
35df4500 3991 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3992 {
6c95b8df
PA
3993 if (b->loc && b->loc->pspace != pspace)
3994 continue;
3995
c5aa993b
JM
3996 switch (b->type)
3997 {
3998 case bp_call_dummy:
e2e4d78b 3999 case bp_longjmp_call_dummy:
c906108c 4000
c5aa993b 4001 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4002 cause problems when the inferior is rerun, so we better get
4003 rid of it. */
4004
4005 case bp_watchpoint_scope:
4006
4007 /* Also get rid of scope breakpoints. */
4008
4009 case bp_shlib_event:
4010
4011 /* Also remove solib event breakpoints. Their addresses may
4012 have changed since the last time we ran the program.
4013 Actually we may now be debugging against different target;
4014 and so the solib backend that installed this breakpoint may
4015 not be used in by the target. E.g.,
4016
4017 (gdb) file prog-linux
4018 (gdb) run # native linux target
4019 ...
4020 (gdb) kill
4021 (gdb) file prog-win.exe
4022 (gdb) tar rem :9999 # remote Windows gdbserver.
4023 */
c906108c 4024
f59f708a
PA
4025 case bp_step_resume:
4026
4027 /* Also remove step-resume breakpoints. */
4028
c5aa993b
JM
4029 delete_breakpoint (b);
4030 break;
c906108c 4031
c5aa993b
JM
4032 case bp_watchpoint:
4033 case bp_hardware_watchpoint:
4034 case bp_read_watchpoint:
4035 case bp_access_watchpoint:
3a5c3e22
PA
4036 {
4037 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4038
3a5c3e22
PA
4039 /* Likewise for watchpoints on local expressions. */
4040 if (w->exp_valid_block != NULL)
4041 delete_breakpoint (b);
4042 else if (context == inf_starting)
4043 {
4044 /* Reset val field to force reread of starting value in
4045 insert_breakpoints. */
4046 if (w->val)
4047 value_free (w->val);
4048 w->val = NULL;
4049 w->val_valid = 0;
c860120c 4050 }
3a5c3e22 4051 }
c5aa993b
JM
4052 break;
4053 default:
c5aa993b
JM
4054 break;
4055 }
4056 }
1c5cfe86
PA
4057
4058 /* Get rid of the moribund locations. */
35df4500
TJB
4059 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4060 decref_bp_location (&bl);
1c5cfe86 4061 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4062}
4063
6c95b8df
PA
4064/* These functions concern about actual breakpoints inserted in the
4065 target --- to e.g. check if we need to do decr_pc adjustment or if
4066 we need to hop over the bkpt --- so we check for address space
4067 match, not program space. */
4068
c2c6d25f
JM
4069/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4070 exists at PC. It returns ordinary_breakpoint_here if it's an
4071 ordinary breakpoint, or permanent_breakpoint_here if it's a
4072 permanent breakpoint.
4073 - When continuing from a location with an ordinary breakpoint, we
4074 actually single step once before calling insert_breakpoints.
e5dd4106 4075 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4076 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4077 the target, to advance the PC past the breakpoint. */
c906108c 4078
c2c6d25f 4079enum breakpoint_here
6c95b8df 4080breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4081{
35df4500 4082 struct bp_location *bl, **blp_tmp;
c2c6d25f 4083 int any_breakpoint_here = 0;
c906108c 4084
35df4500 4085 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4086 {
35df4500
TJB
4087 if (bl->loc_type != bp_loc_software_breakpoint
4088 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4089 continue;
4090
f1310107 4091 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
4092 if ((breakpoint_enabled (bl->owner)
4093 || bl->owner->enable_state == bp_permanent)
f1310107 4094 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4095 {
4096 if (overlay_debugging
35df4500
TJB
4097 && section_is_overlay (bl->section)
4098 && !section_is_mapped (bl->section))
075f6582 4099 continue; /* unmapped overlay -- can't be a match */
35df4500 4100 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
4101 return permanent_breakpoint_here;
4102 else
4103 any_breakpoint_here = 1;
4104 }
4105 }
c906108c 4106
c2c6d25f 4107 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4108}
4109
1c5cfe86
PA
4110/* Return true if there's a moribund breakpoint at PC. */
4111
4112int
6c95b8df 4113moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4114{
4115 struct bp_location *loc;
4116 int ix;
4117
4118 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4119 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4120 return 1;
4121
4122 return 0;
4123}
c2c6d25f 4124
c36b740a 4125/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
4126 inserted using regular breakpoint_chain / bp_location array
4127 mechanism. This does not check for single-step breakpoints, which
4128 are inserted and removed using direct target manipulation. */
c906108c
SS
4129
4130int
4a64f543
MS
4131regular_breakpoint_inserted_here_p (struct address_space *aspace,
4132 CORE_ADDR pc)
c906108c 4133{
35df4500 4134 struct bp_location *bl, **blp_tmp;
c906108c 4135
35df4500 4136 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4137 {
35df4500
TJB
4138 if (bl->loc_type != bp_loc_software_breakpoint
4139 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4140 continue;
4141
35df4500 4142 if (bl->inserted
f1310107 4143 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4144 {
4145 if (overlay_debugging
35df4500
TJB
4146 && section_is_overlay (bl->section)
4147 && !section_is_mapped (bl->section))
075f6582
DJ
4148 continue; /* unmapped overlay -- can't be a match */
4149 else
4150 return 1;
4151 }
c5aa993b 4152 }
c36b740a
VP
4153 return 0;
4154}
4155
4156/* Returns non-zero iff there's either regular breakpoint
4157 or a single step breakpoint inserted at PC. */
4158
4159int
6c95b8df 4160breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 4161{
6c95b8df 4162 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 4163 return 1;
c906108c 4164
6c95b8df 4165 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4166 return 1;
4167
c906108c
SS
4168 return 0;
4169}
4170
ef370185
JB
4171/* Ignoring deprecated raw breakpoints, return non-zero iff there is a
4172 software breakpoint inserted at PC. */
4fa8626c 4173
ef370185
JB
4174static struct bp_location *
4175find_non_raw_software_breakpoint_inserted_here (struct address_space *aspace,
4176 CORE_ADDR pc)
4fa8626c 4177{
35df4500 4178 struct bp_location *bl, **blp_tmp;
4fa8626c 4179
35df4500 4180 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 4181 {
35df4500 4182 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4183 continue;
4184
35df4500
TJB
4185 if (bl->inserted
4186 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 4187 aspace, pc))
4fa8626c
DJ
4188 {
4189 if (overlay_debugging
35df4500
TJB
4190 && section_is_overlay (bl->section)
4191 && !section_is_mapped (bl->section))
4fa8626c
DJ
4192 continue; /* unmapped overlay -- can't be a match */
4193 else
ef370185 4194 return bl;
4fa8626c
DJ
4195 }
4196 }
4197
ef370185
JB
4198 return NULL;
4199}
4200
4201/* This function returns non-zero iff there is a software breakpoint
4202 inserted at PC. */
4203
4204int
4205software_breakpoint_inserted_here_p (struct address_space *aspace,
4206 CORE_ADDR pc)
4207{
4208 if (find_non_raw_software_breakpoint_inserted_here (aspace, pc) != NULL)
4209 return 1;
4210
1aafd4da 4211 /* Also check for software single-step breakpoints. */
6c95b8df 4212 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4213 return 1;
4214
4fa8626c
DJ
4215 return 0;
4216}
4217
9093389c
PA
4218int
4219hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4220 CORE_ADDR addr, ULONGEST len)
4221{
4222 struct breakpoint *bpt;
4223
4224 ALL_BREAKPOINTS (bpt)
4225 {
4226 struct bp_location *loc;
4227
4228 if (bpt->type != bp_hardware_watchpoint
4229 && bpt->type != bp_access_watchpoint)
4230 continue;
4231
4232 if (!breakpoint_enabled (bpt))
4233 continue;
4234
4235 for (loc = bpt->loc; loc; loc = loc->next)
4236 if (loc->pspace->aspace == aspace && loc->inserted)
4237 {
4238 CORE_ADDR l, h;
4239
4240 /* Check for intersection. */
4241 l = max (loc->address, addr);
4242 h = min (loc->address + loc->length, addr + len);
4243 if (l < h)
4244 return 1;
4245 }
4246 }
4247 return 0;
4248}
4249
075f6582
DJ
4250/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4251 PC is valid for process/thread PTID. */
c906108c
SS
4252
4253int
6c95b8df
PA
4254breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4255 ptid_t ptid)
c906108c 4256{
35df4500 4257 struct bp_location *bl, **blp_tmp;
4a306c9a 4258 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 4259 int thread = -1;
4a306c9a 4260 int task = 0;
a6f1cd96 4261
35df4500 4262 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4263 {
35df4500
TJB
4264 if (bl->loc_type != bp_loc_software_breakpoint
4265 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4266 continue;
4267
35df4500
TJB
4268 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4269 if (!breakpoint_enabled (bl->owner)
4270 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
4271 continue;
4272
f1310107 4273 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
4274 continue;
4275
35df4500 4276 if (bl->owner->thread != -1)
075f6582 4277 {
a6f1cd96
JB
4278 /* This is a thread-specific breakpoint. Check that ptid
4279 matches that thread. If thread hasn't been computed yet,
4280 it is now time to do so. */
4281 if (thread == -1)
4282 thread = pid_to_thread_id (ptid);
35df4500 4283 if (bl->owner->thread != thread)
a6f1cd96 4284 continue;
075f6582 4285 }
a6f1cd96 4286
35df4500 4287 if (bl->owner->task != 0)
4a306c9a
JB
4288 {
4289 /* This is a task-specific breakpoint. Check that ptid
4290 matches that task. If task hasn't been computed yet,
4291 it is now time to do so. */
4292 if (task == 0)
4293 task = ada_get_task_number (ptid);
35df4500 4294 if (bl->owner->task != task)
4a306c9a
JB
4295 continue;
4296 }
4297
a6f1cd96 4298 if (overlay_debugging
35df4500
TJB
4299 && section_is_overlay (bl->section)
4300 && !section_is_mapped (bl->section))
a6f1cd96
JB
4301 continue; /* unmapped overlay -- can't be a match */
4302
4303 return 1;
c5aa993b 4304 }
c906108c
SS
4305
4306 return 0;
4307}
c906108c 4308\f
c5aa993b 4309
c906108c
SS
4310/* bpstat stuff. External routines' interfaces are documented
4311 in breakpoint.h. */
4312
4313int
c326b90e 4314is_catchpoint (struct breakpoint *ep)
c906108c 4315{
533be4dd 4316 return (ep->type == bp_catchpoint);
c906108c
SS
4317}
4318
f431efe5
PA
4319/* Frees any storage that is part of a bpstat. Does not walk the
4320 'next' chain. */
4321
4322static void
198757a8
VP
4323bpstat_free (bpstat bs)
4324{
4325 if (bs->old_val != NULL)
4326 value_free (bs->old_val);
9add0f1b 4327 decref_counted_command_line (&bs->commands);
f431efe5 4328 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4329 xfree (bs);
4330}
4331
c906108c
SS
4332/* Clear a bpstat so that it says we are not at any breakpoint.
4333 Also free any storage that is part of a bpstat. */
4334
4335void
fba45db2 4336bpstat_clear (bpstat *bsp)
c906108c
SS
4337{
4338 bpstat p;
4339 bpstat q;
4340
4341 if (bsp == 0)
4342 return;
4343 p = *bsp;
4344 while (p != NULL)
4345 {
4346 q = p->next;
198757a8 4347 bpstat_free (p);
c906108c
SS
4348 p = q;
4349 }
4350 *bsp = NULL;
4351}
4352
4353/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4354 is part of the bpstat is copied as well. */
4355
4356bpstat
fba45db2 4357bpstat_copy (bpstat bs)
c906108c
SS
4358{
4359 bpstat p = NULL;
4360 bpstat tmp;
4361 bpstat retval = NULL;
4362
4363 if (bs == NULL)
4364 return bs;
4365
4366 for (; bs != NULL; bs = bs->next)
4367 {
4368 tmp = (bpstat) xmalloc (sizeof (*tmp));
4369 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4370 incref_counted_command_line (tmp->commands);
f431efe5 4371 incref_bp_location (tmp->bp_location_at);
31cc81e9 4372 if (bs->old_val != NULL)
3c3185ac
JK
4373 {
4374 tmp->old_val = value_copy (bs->old_val);
4375 release_value (tmp->old_val);
4376 }
31cc81e9 4377
c906108c
SS
4378 if (p == NULL)
4379 /* This is the first thing in the chain. */
4380 retval = tmp;
4381 else
4382 p->next = tmp;
4383 p = tmp;
4384 }
4385 p->next = NULL;
4386 return retval;
4387}
4388
4a64f543 4389/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4390
4391bpstat
fba45db2 4392bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4393{
c5aa993b
JM
4394 if (bsp == NULL)
4395 return NULL;
c906108c 4396
c5aa993b
JM
4397 for (; bsp != NULL; bsp = bsp->next)
4398 {
f431efe5 4399 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4400 return bsp;
4401 }
c906108c
SS
4402 return NULL;
4403}
4404
ab04a2af
TT
4405/* See breakpoint.h. */
4406
47591c29 4407int
427cd150 4408bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4409{
ab04a2af
TT
4410 for (; bsp != NULL; bsp = bsp->next)
4411 {
427cd150
TT
4412 if (bsp->breakpoint_at == NULL)
4413 {
4414 /* A moribund location can never explain a signal other than
4415 GDB_SIGNAL_TRAP. */
4416 if (sig == GDB_SIGNAL_TRAP)
47591c29 4417 return 1;
427cd150
TT
4418 }
4419 else
47591c29
PA
4420 {
4421 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4422 sig))
4423 return 1;
4424 }
ab04a2af
TT
4425 }
4426
47591c29 4427 return 0;
ab04a2af
TT
4428}
4429
4a64f543
MS
4430/* Put in *NUM the breakpoint number of the first breakpoint we are
4431 stopped at. *BSP upon return is a bpstat which points to the
4432 remaining breakpoints stopped at (but which is not guaranteed to be
4433 good for anything but further calls to bpstat_num).
4434
8671a17b
PA
4435 Return 0 if passed a bpstat which does not indicate any breakpoints.
4436 Return -1 if stopped at a breakpoint that has been deleted since
4437 we set it.
4438 Return 1 otherwise. */
c906108c
SS
4439
4440int
8671a17b 4441bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4442{
4443 struct breakpoint *b;
4444
4445 if ((*bsp) == NULL)
4446 return 0; /* No more breakpoint values */
8671a17b 4447
4a64f543
MS
4448 /* We assume we'll never have several bpstats that correspond to a
4449 single breakpoint -- otherwise, this function might return the
4450 same number more than once and this will look ugly. */
f431efe5 4451 b = (*bsp)->breakpoint_at;
8671a17b
PA
4452 *bsp = (*bsp)->next;
4453 if (b == NULL)
4454 return -1; /* breakpoint that's been deleted since */
4455
4456 *num = b->number; /* We have its number */
4457 return 1;
c906108c
SS
4458}
4459
e93ca019 4460/* See breakpoint.h. */
c906108c
SS
4461
4462void
e93ca019 4463bpstat_clear_actions (void)
c906108c 4464{
e93ca019
JK
4465 struct thread_info *tp;
4466 bpstat bs;
4467
4468 if (ptid_equal (inferior_ptid, null_ptid))
4469 return;
4470
4471 tp = find_thread_ptid (inferior_ptid);
4472 if (tp == NULL)
4473 return;
4474
4475 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4476 {
9add0f1b 4477 decref_counted_command_line (&bs->commands);
abf85f46 4478
c906108c
SS
4479 if (bs->old_val != NULL)
4480 {
4481 value_free (bs->old_val);
4482 bs->old_val = NULL;
4483 }
4484 }
4485}
4486
f3b1572e
PA
4487/* Called when a command is about to proceed the inferior. */
4488
4489static void
4490breakpoint_about_to_proceed (void)
4491{
4492 if (!ptid_equal (inferior_ptid, null_ptid))
4493 {
4494 struct thread_info *tp = inferior_thread ();
4495
4496 /* Allow inferior function calls in breakpoint commands to not
4497 interrupt the command list. When the call finishes
4498 successfully, the inferior will be standing at the same
4499 breakpoint as if nothing happened. */
16c381f0 4500 if (tp->control.in_infcall)
f3b1572e
PA
4501 return;
4502 }
4503
4504 breakpoint_proceeded = 1;
4505}
4506
4a64f543
MS
4507/* Stub for cleaning up our state if we error-out of a breakpoint
4508 command. */
c906108c 4509static void
4efb68b1 4510cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4511{
4512 executing_breakpoint_commands = 0;
4513}
4514
abf85f46
JK
4515/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4516 or its equivalent. */
4517
4518static int
4519command_line_is_silent (struct command_line *cmd)
4520{
4521 return cmd && (strcmp ("silent", cmd->line) == 0
4522 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4523}
4524
4a64f543
MS
4525/* Execute all the commands associated with all the breakpoints at
4526 this location. Any of these commands could cause the process to
4527 proceed beyond this point, etc. We look out for such changes by
4528 checking the global "breakpoint_proceeded" after each command.
c906108c 4529
347bddb7
PA
4530 Returns true if a breakpoint command resumed the inferior. In that
4531 case, it is the caller's responsibility to recall it again with the
4532 bpstat of the current thread. */
4533
4534static int
4535bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4536{
4537 bpstat bs;
4538 struct cleanup *old_chain;
347bddb7 4539 int again = 0;
c906108c
SS
4540
4541 /* Avoid endless recursion if a `source' command is contained
4542 in bs->commands. */
4543 if (executing_breakpoint_commands)
347bddb7 4544 return 0;
c906108c
SS
4545
4546 executing_breakpoint_commands = 1;
4547 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4548
cf6c5ffb
TT
4549 prevent_dont_repeat ();
4550
4a64f543 4551 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4552 bs = *bsp;
4553
4554 breakpoint_proceeded = 0;
4555 for (; bs != NULL; bs = bs->next)
4556 {
9add0f1b 4557 struct counted_command_line *ccmd;
6c50ab1c
JB
4558 struct command_line *cmd;
4559 struct cleanup *this_cmd_tree_chain;
4560
4561 /* Take ownership of the BSP's command tree, if it has one.
4562
4563 The command tree could legitimately contain commands like
4564 'step' and 'next', which call clear_proceed_status, which
4565 frees stop_bpstat's command tree. To make sure this doesn't
4566 free the tree we're executing out from under us, we need to
4567 take ownership of the tree ourselves. Since a given bpstat's
4568 commands are only executed once, we don't need to copy it; we
4569 can clear the pointer in the bpstat, and make sure we free
4570 the tree when we're done. */
9add0f1b
TT
4571 ccmd = bs->commands;
4572 bs->commands = NULL;
abf85f46
JK
4573 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4574 cmd = ccmd ? ccmd->commands : NULL;
4575 if (command_line_is_silent (cmd))
4576 {
4577 /* The action has been already done by bpstat_stop_status. */
4578 cmd = cmd->next;
4579 }
6c50ab1c 4580
c906108c
SS
4581 while (cmd != NULL)
4582 {
4583 execute_control_command (cmd);
4584
4585 if (breakpoint_proceeded)
4586 break;
4587 else
4588 cmd = cmd->next;
4589 }
6c50ab1c
JB
4590
4591 /* We can free this command tree now. */
4592 do_cleanups (this_cmd_tree_chain);
4593
c906108c 4594 if (breakpoint_proceeded)
32c1e744
VP
4595 {
4596 if (target_can_async_p ())
347bddb7
PA
4597 /* If we are in async mode, then the target might be still
4598 running, not stopped at any breakpoint, so nothing for
4599 us to do here -- just return to the event loop. */
4600 ;
32c1e744
VP
4601 else
4602 /* In sync mode, when execute_control_command returns
4603 we're already standing on the next breakpoint.
347bddb7
PA
4604 Breakpoint commands for that stop were not run, since
4605 execute_command does not run breakpoint commands --
4606 only command_line_handler does, but that one is not
4607 involved in execution of breakpoint commands. So, we
4608 can now execute breakpoint commands. It should be
4609 noted that making execute_command do bpstat actions is
4610 not an option -- in this case we'll have recursive
4611 invocation of bpstat for each breakpoint with a
4612 command, and can easily blow up GDB stack. Instead, we
4613 return true, which will trigger the caller to recall us
4614 with the new stop_bpstat. */
4615 again = 1;
4616 break;
32c1e744 4617 }
c906108c 4618 }
c2b8ed2c 4619 do_cleanups (old_chain);
347bddb7
PA
4620 return again;
4621}
4622
4623void
4624bpstat_do_actions (void)
4625{
353d1d73
JK
4626 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4627
347bddb7
PA
4628 /* Do any commands attached to breakpoint we are stopped at. */
4629 while (!ptid_equal (inferior_ptid, null_ptid)
4630 && target_has_execution
4631 && !is_exited (inferior_ptid)
4632 && !is_executing (inferior_ptid))
4633 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4634 and only return when it is stopped at the next breakpoint, we
4635 keep doing breakpoint actions until it returns false to
4636 indicate the inferior was not resumed. */
16c381f0 4637 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4638 break;
353d1d73
JK
4639
4640 discard_cleanups (cleanup_if_error);
c906108c
SS
4641}
4642
fa4727a6
DJ
4643/* Print out the (old or new) value associated with a watchpoint. */
4644
4645static void
4646watchpoint_value_print (struct value *val, struct ui_file *stream)
4647{
4648 if (val == NULL)
4649 fprintf_unfiltered (stream, _("<unreadable>"));
4650 else
79a45b7d
TT
4651 {
4652 struct value_print_options opts;
4653 get_user_print_options (&opts);
4654 value_print (val, stream, &opts);
4655 }
fa4727a6
DJ
4656}
4657
e514a9d6 4658/* Generic routine for printing messages indicating why we
4a64f543 4659 stopped. The behavior of this function depends on the value
e514a9d6
JM
4660 'print_it' in the bpstat structure. Under some circumstances we
4661 may decide not to print anything here and delegate the task to
4a64f543 4662 normal_stop(). */
e514a9d6
JM
4663
4664static enum print_stop_action
4665print_bp_stop_message (bpstat bs)
4666{
4667 switch (bs->print_it)
4668 {
4669 case print_it_noop:
4a64f543 4670 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4671 return PRINT_UNKNOWN;
4672 break;
4673
4674 case print_it_done:
4675 /* We still want to print the frame, but we already printed the
4a64f543 4676 relevant messages. */
e514a9d6
JM
4677 return PRINT_SRC_AND_LOC;
4678 break;
4679
4680 case print_it_normal:
4f8d1dc6 4681 {
f431efe5
PA
4682 struct breakpoint *b = bs->breakpoint_at;
4683
1a6a67de
TJB
4684 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4685 which has since been deleted. */
4686 if (b == NULL)
4687 return PRINT_UNKNOWN;
4688
348d480f
PA
4689 /* Normal case. Call the breakpoint's print_it method. */
4690 return b->ops->print_it (bs);
4f8d1dc6 4691 }
348d480f 4692 break;
3086aeae 4693
e514a9d6 4694 default:
8e65ff28 4695 internal_error (__FILE__, __LINE__,
e2e0b3e5 4696 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4697 break;
c906108c 4698 }
c906108c
SS
4699}
4700
edcc5120
TT
4701/* A helper function that prints a shared library stopped event. */
4702
4703static void
4704print_solib_event (int is_catchpoint)
4705{
4706 int any_deleted
4707 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4708 int any_added
4709 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4710
4711 if (!is_catchpoint)
4712 {
4713 if (any_added || any_deleted)
4714 ui_out_text (current_uiout,
4715 _("Stopped due to shared library event:\n"));
4716 else
4717 ui_out_text (current_uiout,
4718 _("Stopped due to shared library event (no "
4719 "libraries added or removed)\n"));
4720 }
4721
4722 if (ui_out_is_mi_like_p (current_uiout))
4723 ui_out_field_string (current_uiout, "reason",
4724 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4725
4726 if (any_deleted)
4727 {
4728 struct cleanup *cleanup;
4729 char *name;
4730 int ix;
4731
4732 ui_out_text (current_uiout, _(" Inferior unloaded "));
4733 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4734 "removed");
4735 for (ix = 0;
4736 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4737 ix, name);
4738 ++ix)
4739 {
4740 if (ix > 0)
4741 ui_out_text (current_uiout, " ");
4742 ui_out_field_string (current_uiout, "library", name);
4743 ui_out_text (current_uiout, "\n");
4744 }
4745
4746 do_cleanups (cleanup);
4747 }
4748
4749 if (any_added)
4750 {
4751 struct so_list *iter;
4752 int ix;
4753 struct cleanup *cleanup;
4754
4755 ui_out_text (current_uiout, _(" Inferior loaded "));
4756 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4757 "added");
4758 for (ix = 0;
4759 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4760 ix, iter);
4761 ++ix)
4762 {
4763 if (ix > 0)
4764 ui_out_text (current_uiout, " ");
4765 ui_out_field_string (current_uiout, "library", iter->so_name);
4766 ui_out_text (current_uiout, "\n");
4767 }
4768
4769 do_cleanups (cleanup);
4770 }
4771}
4772
e514a9d6
JM
4773/* Print a message indicating what happened. This is called from
4774 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4775 list - a list of the eventpoints that caused this stop. KIND is
4776 the target_waitkind for the stopping event. This
e514a9d6
JM
4777 routine calls the generic print routine for printing a message
4778 about reasons for stopping. This will print (for example) the
4779 "Breakpoint n," part of the output. The return value of this
4780 routine is one of:
c906108c 4781
4a64f543 4782 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4783 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4784 code to print the location. An example is
c5aa993b
JM
4785 "Breakpoint 1, " which should be followed by
4786 the location.
917317f4 4787 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4788 to also print the location part of the message.
4789 An example is the catch/throw messages, which
4a64f543 4790 don't require a location appended to the end.
917317f4 4791 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4792 further info to be printed. */
c906108c 4793
917317f4 4794enum print_stop_action
36dfb11c 4795bpstat_print (bpstat bs, int kind)
c906108c
SS
4796{
4797 int val;
c5aa993b 4798
c906108c 4799 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4800 (Currently all watchpoints go on the bpstat whether hit or not.
4801 That probably could (should) be changed, provided care is taken
c906108c 4802 with respect to bpstat_explains_signal). */
e514a9d6
JM
4803 for (; bs; bs = bs->next)
4804 {
4805 val = print_bp_stop_message (bs);
4806 if (val == PRINT_SRC_ONLY
4807 || val == PRINT_SRC_AND_LOC
4808 || val == PRINT_NOTHING)
4809 return val;
4810 }
c906108c 4811
36dfb11c
TT
4812 /* If we had hit a shared library event breakpoint,
4813 print_bp_stop_message would print out this message. If we hit an
4814 OS-level shared library event, do the same thing. */
4815 if (kind == TARGET_WAITKIND_LOADED)
4816 {
edcc5120 4817 print_solib_event (0);
36dfb11c
TT
4818 return PRINT_NOTHING;
4819 }
4820
e514a9d6 4821 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4822 with and nothing was printed. */
917317f4 4823 return PRINT_UNKNOWN;
c906108c
SS
4824}
4825
c42bd95a
DE
4826/* Evaluate the expression EXP and return 1 if value is zero.
4827 This returns the inverse of the condition because it is called
4828 from catch_errors which returns 0 if an exception happened, and if an
4829 exception happens we want execution to stop.
4a64f543 4830 The argument is a "struct expression *" that has been cast to a
c42bd95a 4831 "void *" to make it pass through catch_errors. */
c906108c
SS
4832
4833static int
4efb68b1 4834breakpoint_cond_eval (void *exp)
c906108c 4835{
278cd55f 4836 struct value *mark = value_mark ();
c5aa993b 4837 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4838
c906108c
SS
4839 value_free_to_mark (mark);
4840 return i;
4841}
4842
5760d0ab 4843/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4844
4845static bpstat
5760d0ab 4846bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4847{
4848 bpstat bs;
4849
4850 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4851 bs->next = NULL;
4852 **bs_link_pointer = bs;
4853 *bs_link_pointer = &bs->next;
f431efe5
PA
4854 bs->breakpoint_at = bl->owner;
4855 bs->bp_location_at = bl;
4856 incref_bp_location (bl);
c906108c
SS
4857 /* If the condition is false, etc., don't do the commands. */
4858 bs->commands = NULL;
4859 bs->old_val = NULL;
4860 bs->print_it = print_it_normal;
4861 return bs;
4862}
4863\f
d983da9c
DJ
4864/* The target has stopped with waitstatus WS. Check if any hardware
4865 watchpoints have triggered, according to the target. */
4866
4867int
4868watchpoints_triggered (struct target_waitstatus *ws)
4869{
d92524f1 4870 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4871 CORE_ADDR addr;
4872 struct breakpoint *b;
4873
4874 if (!stopped_by_watchpoint)
4875 {
4876 /* We were not stopped by a watchpoint. Mark all watchpoints
4877 as not triggered. */
4878 ALL_BREAKPOINTS (b)
cc60f2e3 4879 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4880 {
4881 struct watchpoint *w = (struct watchpoint *) b;
4882
4883 w->watchpoint_triggered = watch_triggered_no;
4884 }
d983da9c
DJ
4885
4886 return 0;
4887 }
4888
4889 if (!target_stopped_data_address (&current_target, &addr))
4890 {
4891 /* We were stopped by a watchpoint, but we don't know where.
4892 Mark all watchpoints as unknown. */
4893 ALL_BREAKPOINTS (b)
cc60f2e3 4894 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4895 {
4896 struct watchpoint *w = (struct watchpoint *) b;
4897
4898 w->watchpoint_triggered = watch_triggered_unknown;
4899 }
d983da9c 4900
3c4797ba 4901 return 1;
d983da9c
DJ
4902 }
4903
4904 /* The target could report the data address. Mark watchpoints
4905 affected by this data address as triggered, and all others as not
4906 triggered. */
4907
4908 ALL_BREAKPOINTS (b)
cc60f2e3 4909 if (is_hardware_watchpoint (b))
d983da9c 4910 {
3a5c3e22 4911 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4912 struct bp_location *loc;
d983da9c 4913
3a5c3e22 4914 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4915 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4916 {
3a5c3e22 4917 if (is_masked_watchpoint (b))
9c06b0b4 4918 {
3a5c3e22
PA
4919 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4920 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4921
4922 if (newaddr == start)
4923 {
3a5c3e22 4924 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4925 break;
4926 }
4927 }
4928 /* Exact match not required. Within range is sufficient. */
4929 else if (target_watchpoint_addr_within_range (&current_target,
4930 addr, loc->address,
4931 loc->length))
4932 {
3a5c3e22 4933 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4934 break;
4935 }
4936 }
d983da9c
DJ
4937 }
4938
4939 return 1;
4940}
4941
c906108c
SS
4942/* Possible return values for watchpoint_check (this can't be an enum
4943 because of check_errors). */
4944/* The watchpoint has been deleted. */
4945#define WP_DELETED 1
4946/* The value has changed. */
4947#define WP_VALUE_CHANGED 2
4948/* The value has not changed. */
4949#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4950/* Ignore this watchpoint, no matter if the value changed or not. */
4951#define WP_IGNORE 4
c906108c
SS
4952
4953#define BP_TEMPFLAG 1
4954#define BP_HARDWAREFLAG 2
4955
4a64f543
MS
4956/* Evaluate watchpoint condition expression and check if its value
4957 changed.
553e4c11
JB
4958
4959 P should be a pointer to struct bpstat, but is defined as a void *
4960 in order for this function to be usable with catch_errors. */
c906108c
SS
4961
4962static int
4efb68b1 4963watchpoint_check (void *p)
c906108c
SS
4964{
4965 bpstat bs = (bpstat) p;
3a5c3e22 4966 struct watchpoint *b;
c906108c
SS
4967 struct frame_info *fr;
4968 int within_current_scope;
4969
f431efe5 4970 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4971 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4972 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4973
f6bc2008
PA
4974 /* If this is a local watchpoint, we only want to check if the
4975 watchpoint frame is in scope if the current thread is the thread
4976 that was used to create the watchpoint. */
4977 if (!watchpoint_in_thread_scope (b))
60e1c644 4978 return WP_IGNORE;
f6bc2008 4979
c906108c
SS
4980 if (b->exp_valid_block == NULL)
4981 within_current_scope = 1;
4982 else
4983 {
edb3359d
DJ
4984 struct frame_info *frame = get_current_frame ();
4985 struct gdbarch *frame_arch = get_frame_arch (frame);
4986 CORE_ADDR frame_pc = get_frame_pc (frame);
4987
4a64f543
MS
4988 /* in_function_epilogue_p() returns a non-zero value if we're
4989 still in the function but the stack frame has already been
4990 invalidated. Since we can't rely on the values of local
4991 variables after the stack has been destroyed, we are treating
4992 the watchpoint in that state as `not changed' without further
4993 checking. Don't mark watchpoints as changed if the current
4994 frame is in an epilogue - even if they are in some other
4995 frame, our view of the stack is likely to be wrong and
4996 frame_find_by_id could error out. */
a0f49112 4997 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 4998 return WP_IGNORE;
a0f49112 4999
101dcfbe 5000 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5001 within_current_scope = (fr != NULL);
69fbadd5
DJ
5002
5003 /* If we've gotten confused in the unwinder, we might have
5004 returned a frame that can't describe this variable. */
edb3359d
DJ
5005 if (within_current_scope)
5006 {
5007 struct symbol *function;
5008
5009 function = get_frame_function (fr);
5010 if (function == NULL
5011 || !contained_in (b->exp_valid_block,
5012 SYMBOL_BLOCK_VALUE (function)))
5013 within_current_scope = 0;
5014 }
69fbadd5 5015
edb3359d 5016 if (within_current_scope)
c906108c
SS
5017 /* If we end up stopping, the current frame will get selected
5018 in normal_stop. So this call to select_frame won't affect
5019 the user. */
0f7d239c 5020 select_frame (fr);
c906108c 5021 }
c5aa993b 5022
c906108c
SS
5023 if (within_current_scope)
5024 {
4a64f543
MS
5025 /* We use value_{,free_to_}mark because it could be a *long*
5026 time before we return to the command level and call
5027 free_all_values. We can't call free_all_values because we
5028 might be in the middle of evaluating a function call. */
c906108c 5029
0cf6dd15 5030 int pc = 0;
9c06b0b4 5031 struct value *mark;
fa4727a6
DJ
5032 struct value *new_val;
5033
3a5c3e22 5034 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5035 /* Since we don't know the exact trigger address (from
5036 stopped_data_address), just tell the user we've triggered
5037 a mask watchpoint. */
5038 return WP_VALUE_CHANGED;
5039
5040 mark = value_mark ();
3a1115a0 5041 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5042
4a64f543
MS
5043 /* We use value_equal_contents instead of value_equal because
5044 the latter coerces an array to a pointer, thus comparing just
5045 the address of the array instead of its contents. This is
5046 not what we want. */
fa4727a6 5047 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5048 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5049 {
fa4727a6
DJ
5050 if (new_val != NULL)
5051 {
5052 release_value (new_val);
5053 value_free_to_mark (mark);
5054 }
c906108c
SS
5055 bs->old_val = b->val;
5056 b->val = new_val;
fa4727a6 5057 b->val_valid = 1;
c906108c
SS
5058 return WP_VALUE_CHANGED;
5059 }
5060 else
5061 {
60e1c644 5062 /* Nothing changed. */
c906108c 5063 value_free_to_mark (mark);
c906108c
SS
5064 return WP_VALUE_NOT_CHANGED;
5065 }
5066 }
5067 else
5068 {
79a45e25
PA
5069 struct ui_out *uiout = current_uiout;
5070
c906108c 5071 /* This seems like the only logical thing to do because
c5aa993b
JM
5072 if we temporarily ignored the watchpoint, then when
5073 we reenter the block in which it is valid it contains
5074 garbage (in the case of a function, it may have two
5075 garbage values, one before and one after the prologue).
5076 So we can't even detect the first assignment to it and
5077 watch after that (since the garbage may or may not equal
5078 the first value assigned). */
348d480f
PA
5079 /* We print all the stop information in
5080 breakpoint_ops->print_it, but in this case, by the time we
5081 call breakpoint_ops->print_it this bp will be deleted
5082 already. So we have no choice but print the information
5083 here. */
9dc5e2a9 5084 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5085 ui_out_field_string
5086 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5087 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5088 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5089 ui_out_text (uiout,
5090 " deleted because the program has left the block in\n\
8b93c638 5091which its expression is valid.\n");
4ce44c66 5092
cdac0397 5093 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5094 decref_counted_command_line (&b->base.commands);
d0fb5eae 5095 watchpoint_del_at_next_stop (b);
c906108c
SS
5096
5097 return WP_DELETED;
5098 }
5099}
5100
18a18393 5101/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5102 breakpoint location BL. This function does not check if we should
5103 stop, only if BL explains the stop. */
5104
18a18393 5105static int
6c95b8df 5106bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5107 struct address_space *aspace, CORE_ADDR bp_addr,
5108 const struct target_waitstatus *ws)
18a18393
VP
5109{
5110 struct breakpoint *b = bl->owner;
5111
348d480f 5112 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5113 gdb_assert (b != NULL);
5114
09ac7c10 5115 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5116}
5117
3a5c3e22
PA
5118/* Determine if the watched values have actually changed, and we
5119 should stop. If not, set BS->stop to 0. */
5120
18a18393
VP
5121static void
5122bpstat_check_watchpoint (bpstat bs)
5123{
2bdf28a0 5124 const struct bp_location *bl;
3a5c3e22 5125 struct watchpoint *b;
2bdf28a0
JK
5126
5127 /* BS is built for existing struct breakpoint. */
f431efe5 5128 bl = bs->bp_location_at;
2bdf28a0 5129 gdb_assert (bl != NULL);
3a5c3e22 5130 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5131 gdb_assert (b != NULL);
18a18393 5132
18a18393 5133 {
18a18393
VP
5134 int must_check_value = 0;
5135
3a5c3e22 5136 if (b->base.type == bp_watchpoint)
18a18393
VP
5137 /* For a software watchpoint, we must always check the
5138 watched value. */
5139 must_check_value = 1;
5140 else if (b->watchpoint_triggered == watch_triggered_yes)
5141 /* We have a hardware watchpoint (read, write, or access)
5142 and the target earlier reported an address watched by
5143 this watchpoint. */
5144 must_check_value = 1;
5145 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5146 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5147 /* We were stopped by a hardware watchpoint, but the target could
5148 not report the data address. We must check the watchpoint's
5149 value. Access and read watchpoints are out of luck; without
5150 a data address, we can't figure it out. */
5151 must_check_value = 1;
3a5c3e22 5152
18a18393
VP
5153 if (must_check_value)
5154 {
3e43a32a
MS
5155 char *message
5156 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5157 b->base.number);
18a18393
VP
5158 struct cleanup *cleanups = make_cleanup (xfree, message);
5159 int e = catch_errors (watchpoint_check, bs, message,
5160 RETURN_MASK_ALL);
5161 do_cleanups (cleanups);
5162 switch (e)
5163 {
5164 case WP_DELETED:
5165 /* We've already printed what needs to be printed. */
5166 bs->print_it = print_it_done;
5167 /* Stop. */
5168 break;
60e1c644
PA
5169 case WP_IGNORE:
5170 bs->print_it = print_it_noop;
5171 bs->stop = 0;
5172 break;
18a18393 5173 case WP_VALUE_CHANGED:
3a5c3e22 5174 if (b->base.type == bp_read_watchpoint)
18a18393 5175 {
85d721b8
PA
5176 /* There are two cases to consider here:
5177
4a64f543 5178 1. We're watching the triggered memory for reads.
85d721b8
PA
5179 In that case, trust the target, and always report
5180 the watchpoint hit to the user. Even though
5181 reads don't cause value changes, the value may
5182 have changed since the last time it was read, and
5183 since we're not trapping writes, we will not see
5184 those, and as such we should ignore our notion of
5185 old value.
5186
4a64f543 5187 2. We're watching the triggered memory for both
85d721b8
PA
5188 reads and writes. There are two ways this may
5189 happen:
5190
4a64f543 5191 2.1. This is a target that can't break on data
85d721b8
PA
5192 reads only, but can break on accesses (reads or
5193 writes), such as e.g., x86. We detect this case
5194 at the time we try to insert read watchpoints.
5195
4a64f543 5196 2.2. Otherwise, the target supports read
85d721b8
PA
5197 watchpoints, but, the user set an access or write
5198 watchpoint watching the same memory as this read
5199 watchpoint.
5200
5201 If we're watching memory writes as well as reads,
5202 ignore watchpoint hits when we find that the
5203 value hasn't changed, as reads don't cause
5204 changes. This still gives false positives when
5205 the program writes the same value to memory as
5206 what there was already in memory (we will confuse
5207 it for a read), but it's much better than
5208 nothing. */
5209
5210 int other_write_watchpoint = 0;
5211
5212 if (bl->watchpoint_type == hw_read)
5213 {
5214 struct breakpoint *other_b;
5215
5216 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5217 if (other_b->type == bp_hardware_watchpoint
5218 || other_b->type == bp_access_watchpoint)
85d721b8 5219 {
3a5c3e22
PA
5220 struct watchpoint *other_w =
5221 (struct watchpoint *) other_b;
5222
5223 if (other_w->watchpoint_triggered
5224 == watch_triggered_yes)
5225 {
5226 other_write_watchpoint = 1;
5227 break;
5228 }
85d721b8
PA
5229 }
5230 }
5231
5232 if (other_write_watchpoint
5233 || bl->watchpoint_type == hw_access)
5234 {
5235 /* We're watching the same memory for writes,
5236 and the value changed since the last time we
5237 updated it, so this trap must be for a write.
5238 Ignore it. */
5239 bs->print_it = print_it_noop;
5240 bs->stop = 0;
5241 }
18a18393
VP
5242 }
5243 break;
5244 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5245 if (b->base.type == bp_hardware_watchpoint
5246 || b->base.type == bp_watchpoint)
18a18393
VP
5247 {
5248 /* Don't stop: write watchpoints shouldn't fire if
5249 the value hasn't changed. */
5250 bs->print_it = print_it_noop;
5251 bs->stop = 0;
5252 }
5253 /* Stop. */
5254 break;
5255 default:
5256 /* Can't happen. */
5257 case 0:
5258 /* Error from catch_errors. */
3a5c3e22 5259 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5260 watchpoint_del_at_next_stop (b);
18a18393
VP
5261 /* We've already printed what needs to be printed. */
5262 bs->print_it = print_it_done;
5263 break;
5264 }
5265 }
5266 else /* must_check_value == 0 */
5267 {
5268 /* This is a case where some watchpoint(s) triggered, but
5269 not at the address of this watchpoint, or else no
5270 watchpoint triggered after all. So don't print
5271 anything for this watchpoint. */
5272 bs->print_it = print_it_noop;
5273 bs->stop = 0;
5274 }
5275 }
5276}
5277
7d4df6a4
DE
5278/* For breakpoints that are currently marked as telling gdb to stop,
5279 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5280 of breakpoint referred to by BS. If we should not stop for this
5281 breakpoint, set BS->stop to 0. */
f431efe5 5282
18a18393
VP
5283static void
5284bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5285{
2bdf28a0
JK
5286 const struct bp_location *bl;
5287 struct breakpoint *b;
7d4df6a4
DE
5288 int value_is_zero = 0;
5289 struct expression *cond;
5290
5291 gdb_assert (bs->stop);
2bdf28a0
JK
5292
5293 /* BS is built for existing struct breakpoint. */
f431efe5 5294 bl = bs->bp_location_at;
2bdf28a0 5295 gdb_assert (bl != NULL);
f431efe5 5296 b = bs->breakpoint_at;
2bdf28a0 5297 gdb_assert (b != NULL);
18a18393 5298
b775012e
LM
5299 /* Even if the target evaluated the condition on its end and notified GDB, we
5300 need to do so again since GDB does not know if we stopped due to a
5301 breakpoint or a single step breakpoint. */
5302
18a18393 5303 if (frame_id_p (b->frame_id)
edb3359d 5304 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5305 {
7d4df6a4
DE
5306 bs->stop = 0;
5307 return;
5308 }
60e1c644 5309
12ab52e9
PA
5310 /* If this is a thread/task-specific breakpoint, don't waste cpu
5311 evaluating the condition if this isn't the specified
5312 thread/task. */
5313 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5314 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5315
6c1b0f7b
DE
5316 {
5317 bs->stop = 0;
5318 return;
5319 }
5320
6dddc817
DE
5321 /* Evaluate extension language breakpoints that have a "stop" method
5322 implemented. */
5323 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5324
7d4df6a4
DE
5325 if (is_watchpoint (b))
5326 {
5327 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5328
7d4df6a4
DE
5329 cond = w->cond_exp;
5330 }
5331 else
5332 cond = bl->cond;
60e1c644 5333
7d4df6a4
DE
5334 if (cond && b->disposition != disp_del_at_next_stop)
5335 {
5336 int within_current_scope = 1;
5337 struct watchpoint * w;
60e1c644 5338
7d4df6a4
DE
5339 /* We use value_mark and value_free_to_mark because it could
5340 be a long time before we return to the command level and
5341 call free_all_values. We can't call free_all_values
5342 because we might be in the middle of evaluating a
5343 function call. */
5344 struct value *mark = value_mark ();
5345
5346 if (is_watchpoint (b))
5347 w = (struct watchpoint *) b;
5348 else
5349 w = NULL;
5350
5351 /* Need to select the frame, with all that implies so that
5352 the conditions will have the right context. Because we
5353 use the frame, we will not see an inlined function's
5354 variables when we arrive at a breakpoint at the start
5355 of the inlined function; the current frame will be the
5356 call site. */
5357 if (w == NULL || w->cond_exp_valid_block == NULL)
5358 select_frame (get_current_frame ());
5359 else
18a18393 5360 {
7d4df6a4
DE
5361 struct frame_info *frame;
5362
5363 /* For local watchpoint expressions, which particular
5364 instance of a local is being watched matters, so we
5365 keep track of the frame to evaluate the expression
5366 in. To evaluate the condition however, it doesn't
5367 really matter which instantiation of the function
5368 where the condition makes sense triggers the
5369 watchpoint. This allows an expression like "watch
5370 global if q > 10" set in `func', catch writes to
5371 global on all threads that call `func', or catch
5372 writes on all recursive calls of `func' by a single
5373 thread. We simply always evaluate the condition in
5374 the innermost frame that's executing where it makes
5375 sense to evaluate the condition. It seems
5376 intuitive. */
5377 frame = block_innermost_frame (w->cond_exp_valid_block);
5378 if (frame != NULL)
5379 select_frame (frame);
5380 else
5381 within_current_scope = 0;
18a18393 5382 }
7d4df6a4
DE
5383 if (within_current_scope)
5384 value_is_zero
5385 = catch_errors (breakpoint_cond_eval, cond,
5386 "Error in testing breakpoint condition:\n",
5387 RETURN_MASK_ALL);
5388 else
18a18393 5389 {
7d4df6a4
DE
5390 warning (_("Watchpoint condition cannot be tested "
5391 "in the current scope"));
5392 /* If we failed to set the right context for this
5393 watchpoint, unconditionally report it. */
5394 value_is_zero = 0;
18a18393 5395 }
7d4df6a4
DE
5396 /* FIXME-someday, should give breakpoint #. */
5397 value_free_to_mark (mark);
18a18393 5398 }
7d4df6a4
DE
5399
5400 if (cond && value_is_zero)
5401 {
5402 bs->stop = 0;
5403 }
7d4df6a4
DE
5404 else if (b->ignore_count > 0)
5405 {
5406 b->ignore_count--;
5407 bs->stop = 0;
5408 /* Increase the hit count even though we don't stop. */
5409 ++(b->hit_count);
5410 observer_notify_breakpoint_modified (b);
5411 }
18a18393
VP
5412}
5413
5414
9709f61c 5415/* Get a bpstat associated with having just stopped at address
d983da9c 5416 BP_ADDR in thread PTID.
c906108c 5417
d983da9c 5418 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5419 don't understand this stop. Result is a chain of bpstat's such
5420 that:
c906108c 5421
c5aa993b 5422 if we don't understand the stop, the result is a null pointer.
c906108c 5423
c5aa993b 5424 if we understand why we stopped, the result is not null.
c906108c 5425
c5aa993b
JM
5426 Each element of the chain refers to a particular breakpoint or
5427 watchpoint at which we have stopped. (We may have stopped for
5428 several reasons concurrently.)
c906108c 5429
c5aa993b
JM
5430 Each element of the chain has valid next, breakpoint_at,
5431 commands, FIXME??? fields. */
c906108c
SS
5432
5433bpstat
6c95b8df 5434bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5435 CORE_ADDR bp_addr, ptid_t ptid,
5436 const struct target_waitstatus *ws)
c906108c 5437{
0d381245 5438 struct breakpoint *b = NULL;
afe38095 5439 struct bp_location *bl;
20874c92 5440 struct bp_location *loc;
5760d0ab
JK
5441 /* First item of allocated bpstat's. */
5442 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5443 /* Pointer to the last thing in the chain currently. */
5760d0ab 5444 bpstat bs;
20874c92 5445 int ix;
429374b8 5446 int need_remove_insert;
f431efe5 5447 int removed_any;
c906108c 5448
f431efe5
PA
5449 /* First, build the bpstat chain with locations that explain a
5450 target stop, while being careful to not set the target running,
5451 as that may invalidate locations (in particular watchpoint
5452 locations are recreated). Resuming will happen here with
5453 breakpoint conditions or watchpoint expressions that include
5454 inferior function calls. */
c5aa993b 5455
429374b8
JK
5456 ALL_BREAKPOINTS (b)
5457 {
5458 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5459 continue;
a5606eee 5460
429374b8
JK
5461 for (bl = b->loc; bl != NULL; bl = bl->next)
5462 {
4a64f543
MS
5463 /* For hardware watchpoints, we look only at the first
5464 location. The watchpoint_check function will work on the
5465 entire expression, not the individual locations. For
5466 read watchpoints, the watchpoints_triggered function has
5467 checked all locations already. */
429374b8
JK
5468 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5469 break;
18a18393 5470
f6592439 5471 if (!bl->enabled || bl->shlib_disabled)
429374b8 5472 continue;
c5aa993b 5473
09ac7c10 5474 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5475 continue;
c5aa993b 5476
4a64f543
MS
5477 /* Come here if it's a watchpoint, or if the break address
5478 matches. */
c5aa993b 5479
4a64f543
MS
5480 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5481 explain stop. */
c5aa993b 5482
f431efe5
PA
5483 /* Assume we stop. Should we find a watchpoint that is not
5484 actually triggered, or if the condition of the breakpoint
5485 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5486 bs->stop = 1;
5487 bs->print = 1;
d983da9c 5488
f431efe5
PA
5489 /* If this is a scope breakpoint, mark the associated
5490 watchpoint as triggered so that we will handle the
5491 out-of-scope event. We'll get to the watchpoint next
5492 iteration. */
d0fb5eae 5493 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5494 {
5495 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5496
5497 w->watchpoint_triggered = watch_triggered_yes;
5498 }
f431efe5
PA
5499 }
5500 }
5501
5502 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5503 {
f1310107 5504 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5505 {
5760d0ab 5506 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5507 /* For hits of moribund locations, we should just proceed. */
5508 bs->stop = 0;
5509 bs->print = 0;
5510 bs->print_it = print_it_noop;
5511 }
5512 }
5513
edcc5120
TT
5514 /* A bit of special processing for shlib breakpoints. We need to
5515 process solib loading here, so that the lists of loaded and
5516 unloaded libraries are correct before we handle "catch load" and
5517 "catch unload". */
5518 for (bs = bs_head; bs != NULL; bs = bs->next)
5519 {
5d268276 5520 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5521 {
5522 handle_solib_event ();
5523 break;
5524 }
5525 }
5526
f431efe5
PA
5527 /* Now go through the locations that caused the target to stop, and
5528 check whether we're interested in reporting this stop to higher
5529 layers, or whether we should resume the target transparently. */
5530
5531 removed_any = 0;
5532
5760d0ab 5533 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5534 {
5535 if (!bs->stop)
5536 continue;
5537
f431efe5 5538 b = bs->breakpoint_at;
348d480f
PA
5539 b->ops->check_status (bs);
5540 if (bs->stop)
28010a5d 5541 {
348d480f 5542 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5543
429374b8
JK
5544 if (bs->stop)
5545 {
5546 ++(b->hit_count);
8d3788bd 5547 observer_notify_breakpoint_modified (b);
c906108c 5548
4a64f543 5549 /* We will stop here. */
429374b8
JK
5550 if (b->disposition == disp_disable)
5551 {
816338b5
SS
5552 --(b->enable_count);
5553 if (b->enable_count <= 0
5554 && b->enable_state != bp_permanent)
429374b8 5555 b->enable_state = bp_disabled;
f431efe5 5556 removed_any = 1;
429374b8
JK
5557 }
5558 if (b->silent)
5559 bs->print = 0;
5560 bs->commands = b->commands;
9add0f1b 5561 incref_counted_command_line (bs->commands);
abf85f46
JK
5562 if (command_line_is_silent (bs->commands
5563 ? bs->commands->commands : NULL))
5564 bs->print = 0;
9d6e6e84
HZ
5565
5566 b->ops->after_condition_true (bs);
429374b8
JK
5567 }
5568
348d480f 5569 }
a9b3a50f
PA
5570
5571 /* Print nothing for this entry if we don't stop or don't
5572 print. */
5573 if (!bs->stop || !bs->print)
5574 bs->print_it = print_it_noop;
429374b8 5575 }
876fa593 5576
d983da9c
DJ
5577 /* If we aren't stopping, the value of some hardware watchpoint may
5578 not have changed, but the intermediate memory locations we are
5579 watching may have. Don't bother if we're stopping; this will get
5580 done later. */
d832cb68 5581 need_remove_insert = 0;
5760d0ab
JK
5582 if (! bpstat_causes_stop (bs_head))
5583 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5584 if (!bs->stop
f431efe5
PA
5585 && bs->breakpoint_at
5586 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5587 {
3a5c3e22
PA
5588 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5589
5590 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5591 need_remove_insert = 1;
d983da9c
DJ
5592 }
5593
d832cb68 5594 if (need_remove_insert)
2d134ed3 5595 update_global_location_list (1);
f431efe5
PA
5596 else if (removed_any)
5597 update_global_location_list (0);
d832cb68 5598
5760d0ab 5599 return bs_head;
c906108c 5600}
628fe4e4
JK
5601
5602static void
5603handle_jit_event (void)
5604{
5605 struct frame_info *frame;
5606 struct gdbarch *gdbarch;
5607
5608 /* Switch terminal for any messages produced by
5609 breakpoint_re_set. */
5610 target_terminal_ours_for_output ();
5611
5612 frame = get_current_frame ();
5613 gdbarch = get_frame_arch (frame);
5614
5615 jit_event_handler (gdbarch);
5616
5617 target_terminal_inferior ();
5618}
5619
5620/* Prepare WHAT final decision for infrun. */
5621
5622/* Decide what infrun needs to do with this bpstat. */
5623
c906108c 5624struct bpstat_what
0e30163f 5625bpstat_what (bpstat bs_head)
c906108c 5626{
c906108c 5627 struct bpstat_what retval;
628fe4e4 5628 int jit_event = 0;
0e30163f 5629 bpstat bs;
c906108c 5630
628fe4e4 5631 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5632 retval.call_dummy = STOP_NONE;
186c406b 5633 retval.is_longjmp = 0;
628fe4e4 5634
0e30163f 5635 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5636 {
628fe4e4
JK
5637 /* Extract this BS's action. After processing each BS, we check
5638 if its action overrides all we've seem so far. */
5639 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5640 enum bptype bptype;
5641
c906108c 5642 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5643 {
5644 /* I suspect this can happen if it was a momentary
5645 breakpoint which has since been deleted. */
5646 bptype = bp_none;
5647 }
20874c92 5648 else
f431efe5 5649 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5650
5651 switch (bptype)
c906108c
SS
5652 {
5653 case bp_none:
628fe4e4 5654 break;
c906108c
SS
5655 case bp_breakpoint:
5656 case bp_hardware_breakpoint:
5657 case bp_until:
5658 case bp_finish:
a9b3a50f 5659 case bp_shlib_event:
c906108c
SS
5660 if (bs->stop)
5661 {
5662 if (bs->print)
628fe4e4 5663 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5664 else
628fe4e4 5665 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5666 }
5667 else
628fe4e4 5668 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5669 break;
5670 case bp_watchpoint:
5671 case bp_hardware_watchpoint:
5672 case bp_read_watchpoint:
5673 case bp_access_watchpoint:
5674 if (bs->stop)
5675 {
5676 if (bs->print)
628fe4e4 5677 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5678 else
628fe4e4 5679 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5680 }
5681 else
628fe4e4
JK
5682 {
5683 /* There was a watchpoint, but we're not stopping.
5684 This requires no further action. */
5685 }
c906108c
SS
5686 break;
5687 case bp_longjmp:
e2e4d78b 5688 case bp_longjmp_call_dummy:
186c406b 5689 case bp_exception:
628fe4e4 5690 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5691 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5692 break;
5693 case bp_longjmp_resume:
186c406b 5694 case bp_exception_resume:
628fe4e4 5695 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5696 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5697 break;
5698 case bp_step_resume:
5699 if (bs->stop)
628fe4e4
JK
5700 this_action = BPSTAT_WHAT_STEP_RESUME;
5701 else
c906108c 5702 {
628fe4e4
JK
5703 /* It is for the wrong frame. */
5704 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5705 }
c906108c 5706 break;
2c03e5be
PA
5707 case bp_hp_step_resume:
5708 if (bs->stop)
5709 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5710 else
5711 {
5712 /* It is for the wrong frame. */
5713 this_action = BPSTAT_WHAT_SINGLE;
5714 }
5715 break;
c906108c 5716 case bp_watchpoint_scope:
c4093a6a 5717 case bp_thread_event:
1900040c 5718 case bp_overlay_event:
0fd8e87f 5719 case bp_longjmp_master:
aa7d318d 5720 case bp_std_terminate_master:
186c406b 5721 case bp_exception_master:
628fe4e4 5722 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5723 break;
ce78b96d 5724 case bp_catchpoint:
c5aa993b
JM
5725 if (bs->stop)
5726 {
5727 if (bs->print)
628fe4e4 5728 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5729 else
628fe4e4 5730 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5731 }
5732 else
628fe4e4
JK
5733 {
5734 /* There was a catchpoint, but we're not stopping.
5735 This requires no further action. */
5736 }
5737 break;
628fe4e4
JK
5738 case bp_jit_event:
5739 jit_event = 1;
5740 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5741 break;
c906108c 5742 case bp_call_dummy:
53a5351d
JM
5743 /* Make sure the action is stop (silent or noisy),
5744 so infrun.c pops the dummy frame. */
aa7d318d 5745 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5746 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5747 break;
5748 case bp_std_terminate:
5749 /* Make sure the action is stop (silent or noisy),
5750 so infrun.c pops the dummy frame. */
aa7d318d 5751 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5752 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5753 break;
1042e4c0 5754 case bp_tracepoint:
7a697b8d 5755 case bp_fast_tracepoint:
0fb4aa4b 5756 case bp_static_tracepoint:
1042e4c0
SS
5757 /* Tracepoint hits should not be reported back to GDB, and
5758 if one got through somehow, it should have been filtered
5759 out already. */
5760 internal_error (__FILE__, __LINE__,
7a697b8d 5761 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5762 break;
5763 case bp_gnu_ifunc_resolver:
5764 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5765 this_action = BPSTAT_WHAT_SINGLE;
5766 break;
5767 case bp_gnu_ifunc_resolver_return:
5768 /* The breakpoint will be removed, execution will restart from the
5769 PC of the former breakpoint. */
5770 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5771 break;
e7e0cddf
SS
5772
5773 case bp_dprintf:
a11cfd87
HZ
5774 if (bs->stop)
5775 this_action = BPSTAT_WHAT_STOP_SILENT;
5776 else
5777 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5778 break;
5779
628fe4e4
JK
5780 default:
5781 internal_error (__FILE__, __LINE__,
5782 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5783 }
628fe4e4
JK
5784
5785 retval.main_action = max (retval.main_action, this_action);
c906108c 5786 }
628fe4e4 5787
0e30163f
JK
5788 /* These operations may affect the bs->breakpoint_at state so they are
5789 delayed after MAIN_ACTION is decided above. */
5790
628fe4e4
JK
5791 if (jit_event)
5792 {
5793 if (debug_infrun)
5794 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5795
5796 handle_jit_event ();
5797 }
5798
0e30163f
JK
5799 for (bs = bs_head; bs != NULL; bs = bs->next)
5800 {
5801 struct breakpoint *b = bs->breakpoint_at;
5802
5803 if (b == NULL)
5804 continue;
5805 switch (b->type)
5806 {
5807 case bp_gnu_ifunc_resolver:
5808 gnu_ifunc_resolver_stop (b);
5809 break;
5810 case bp_gnu_ifunc_resolver_return:
5811 gnu_ifunc_resolver_return_stop (b);
5812 break;
5813 }
5814 }
5815
c906108c
SS
5816 return retval;
5817}
5818
5819/* Nonzero if we should step constantly (e.g. watchpoints on machines
5820 without hardware support). This isn't related to a specific bpstat,
5821 just to things like whether watchpoints are set. */
5822
c5aa993b 5823int
fba45db2 5824bpstat_should_step (void)
c906108c
SS
5825{
5826 struct breakpoint *b;
cc59ec59 5827
c906108c 5828 ALL_BREAKPOINTS (b)
717a8278 5829 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5830 return 1;
c906108c
SS
5831 return 0;
5832}
5833
67822962
PA
5834int
5835bpstat_causes_stop (bpstat bs)
5836{
5837 for (; bs != NULL; bs = bs->next)
5838 if (bs->stop)
5839 return 1;
5840
5841 return 0;
5842}
5843
c906108c 5844\f
c5aa993b 5845
170b53b2
UW
5846/* Compute a string of spaces suitable to indent the next line
5847 so it starts at the position corresponding to the table column
5848 named COL_NAME in the currently active table of UIOUT. */
5849
5850static char *
5851wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5852{
5853 static char wrap_indent[80];
5854 int i, total_width, width, align;
5855 char *text;
5856
5857 total_width = 0;
5858 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5859 {
5860 if (strcmp (text, col_name) == 0)
5861 {
5862 gdb_assert (total_width < sizeof wrap_indent);
5863 memset (wrap_indent, ' ', total_width);
5864 wrap_indent[total_width] = 0;
5865
5866 return wrap_indent;
5867 }
5868
5869 total_width += width + 1;
5870 }
5871
5872 return NULL;
5873}
5874
b775012e
LM
5875/* Determine if the locations of this breakpoint will have their conditions
5876 evaluated by the target, host or a mix of both. Returns the following:
5877
5878 "host": Host evals condition.
5879 "host or target": Host or Target evals condition.
5880 "target": Target evals condition.
5881*/
5882
5883static const char *
5884bp_condition_evaluator (struct breakpoint *b)
5885{
5886 struct bp_location *bl;
5887 char host_evals = 0;
5888 char target_evals = 0;
5889
5890 if (!b)
5891 return NULL;
5892
5893 if (!is_breakpoint (b))
5894 return NULL;
5895
5896 if (gdb_evaluates_breakpoint_condition_p ()
5897 || !target_supports_evaluation_of_breakpoint_conditions ())
5898 return condition_evaluation_host;
5899
5900 for (bl = b->loc; bl; bl = bl->next)
5901 {
5902 if (bl->cond_bytecode)
5903 target_evals++;
5904 else
5905 host_evals++;
5906 }
5907
5908 if (host_evals && target_evals)
5909 return condition_evaluation_both;
5910 else if (target_evals)
5911 return condition_evaluation_target;
5912 else
5913 return condition_evaluation_host;
5914}
5915
5916/* Determine the breakpoint location's condition evaluator. This is
5917 similar to bp_condition_evaluator, but for locations. */
5918
5919static const char *
5920bp_location_condition_evaluator (struct bp_location *bl)
5921{
5922 if (bl && !is_breakpoint (bl->owner))
5923 return NULL;
5924
5925 if (gdb_evaluates_breakpoint_condition_p ()
5926 || !target_supports_evaluation_of_breakpoint_conditions ())
5927 return condition_evaluation_host;
5928
5929 if (bl && bl->cond_bytecode)
5930 return condition_evaluation_target;
5931 else
5932 return condition_evaluation_host;
5933}
5934
859825b8
JK
5935/* Print the LOC location out of the list of B->LOC locations. */
5936
170b53b2
UW
5937static void
5938print_breakpoint_location (struct breakpoint *b,
5939 struct bp_location *loc)
0d381245 5940{
79a45e25 5941 struct ui_out *uiout = current_uiout;
6c95b8df
PA
5942 struct cleanup *old_chain = save_current_program_space ();
5943
859825b8
JK
5944 if (loc != NULL && loc->shlib_disabled)
5945 loc = NULL;
5946
6c95b8df
PA
5947 if (loc != NULL)
5948 set_current_program_space (loc->pspace);
5949
56435ebe
TT
5950 if (b->display_canonical)
5951 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 5952 else if (loc && loc->symtab)
0d381245
VP
5953 {
5954 struct symbol *sym
5955 = find_pc_sect_function (loc->address, loc->section);
5956 if (sym)
5957 {
5958 ui_out_text (uiout, "in ");
5959 ui_out_field_string (uiout, "func",
5960 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
5961 ui_out_text (uiout, " ");
5962 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5963 ui_out_text (uiout, "at ");
0d381245 5964 }
05cba821
JK
5965 ui_out_field_string (uiout, "file",
5966 symtab_to_filename_for_display (loc->symtab));
0d381245 5967 ui_out_text (uiout, ":");
05cba821 5968
0d381245 5969 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
5970 ui_out_field_string (uiout, "fullname",
5971 symtab_to_fullname (loc->symtab));
0d381245 5972
f8eba3c6 5973 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 5974 }
859825b8 5975 else if (loc)
0d381245 5976 {
f99d8bf4
PA
5977 struct ui_file *stb = mem_fileopen ();
5978 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 5979
f99d8bf4 5980 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 5981 demangle, "");
0d381245 5982 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
5983
5984 do_cleanups (stb_chain);
0d381245 5985 }
859825b8
JK
5986 else
5987 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 5988
b775012e
LM
5989 if (loc && is_breakpoint (b)
5990 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5991 && bp_condition_evaluator (b) == condition_evaluation_both)
5992 {
5993 ui_out_text (uiout, " (");
5994 ui_out_field_string (uiout, "evaluated-by",
5995 bp_location_condition_evaluator (loc));
5996 ui_out_text (uiout, ")");
5997 }
5998
6c95b8df 5999 do_cleanups (old_chain);
0d381245
VP
6000}
6001
269b11a2
PA
6002static const char *
6003bptype_string (enum bptype type)
c906108c 6004{
c4093a6a
JM
6005 struct ep_type_description
6006 {
6007 enum bptype type;
6008 char *description;
6009 };
6010 static struct ep_type_description bptypes[] =
c906108c 6011 {
c5aa993b
JM
6012 {bp_none, "?deleted?"},
6013 {bp_breakpoint, "breakpoint"},
c906108c 6014 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
6015 {bp_until, "until"},
6016 {bp_finish, "finish"},
6017 {bp_watchpoint, "watchpoint"},
c906108c 6018 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6019 {bp_read_watchpoint, "read watchpoint"},
6020 {bp_access_watchpoint, "acc watchpoint"},
6021 {bp_longjmp, "longjmp"},
6022 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6023 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6024 {bp_exception, "exception"},
6025 {bp_exception_resume, "exception resume"},
c5aa993b 6026 {bp_step_resume, "step resume"},
2c03e5be 6027 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6028 {bp_watchpoint_scope, "watchpoint scope"},
6029 {bp_call_dummy, "call dummy"},
aa7d318d 6030 {bp_std_terminate, "std::terminate"},
c5aa993b 6031 {bp_shlib_event, "shlib events"},
c4093a6a 6032 {bp_thread_event, "thread events"},
1900040c 6033 {bp_overlay_event, "overlay events"},
0fd8e87f 6034 {bp_longjmp_master, "longjmp master"},
aa7d318d 6035 {bp_std_terminate_master, "std::terminate master"},
186c406b 6036 {bp_exception_master, "exception master"},
ce78b96d 6037 {bp_catchpoint, "catchpoint"},
1042e4c0 6038 {bp_tracepoint, "tracepoint"},
7a697b8d 6039 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6040 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6041 {bp_dprintf, "dprintf"},
4efc6507 6042 {bp_jit_event, "jit events"},
0e30163f
JK
6043 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6044 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6045 };
269b11a2
PA
6046
6047 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6048 || ((int) type != bptypes[(int) type].type))
6049 internal_error (__FILE__, __LINE__,
6050 _("bptypes table does not describe type #%d."),
6051 (int) type);
6052
6053 return bptypes[(int) type].description;
6054}
6055
998580f1
MK
6056/* For MI, output a field named 'thread-groups' with a list as the value.
6057 For CLI, prefix the list with the string 'inf'. */
6058
6059static void
6060output_thread_groups (struct ui_out *uiout,
6061 const char *field_name,
6062 VEC(int) *inf_num,
6063 int mi_only)
6064{
752eb8b4 6065 struct cleanup *back_to;
998580f1
MK
6066 int is_mi = ui_out_is_mi_like_p (uiout);
6067 int inf;
6068 int i;
6069
6070 /* For backward compatibility, don't display inferiors in CLI unless
6071 there are several. Always display them for MI. */
6072 if (!is_mi && mi_only)
6073 return;
6074
752eb8b4
TT
6075 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6076
998580f1
MK
6077 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6078 {
6079 if (is_mi)
6080 {
6081 char mi_group[10];
6082
6083 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6084 ui_out_field_string (uiout, NULL, mi_group);
6085 }
6086 else
6087 {
6088 if (i == 0)
6089 ui_out_text (uiout, " inf ");
6090 else
6091 ui_out_text (uiout, ", ");
6092
6093 ui_out_text (uiout, plongest (inf));
6094 }
6095 }
6096
6097 do_cleanups (back_to);
6098}
6099
269b11a2
PA
6100/* Print B to gdb_stdout. */
6101
6102static void
6103print_one_breakpoint_location (struct breakpoint *b,
6104 struct bp_location *loc,
6105 int loc_number,
6106 struct bp_location **last_loc,
269b11a2
PA
6107 int allflag)
6108{
6109 struct command_line *l;
c2c6d25f 6110 static char bpenables[] = "nynny";
c906108c 6111
79a45e25 6112 struct ui_out *uiout = current_uiout;
0d381245
VP
6113 int header_of_multiple = 0;
6114 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6115 struct value_print_options opts;
6116
6117 get_user_print_options (&opts);
0d381245
VP
6118
6119 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6120 /* See comment in print_one_breakpoint concerning treatment of
6121 breakpoints with single disabled location. */
0d381245
VP
6122 if (loc == NULL
6123 && (b->loc != NULL
6124 && (b->loc->next != NULL || !b->loc->enabled)))
6125 header_of_multiple = 1;
6126 if (loc == NULL)
6127 loc = b->loc;
6128
c4093a6a
JM
6129 annotate_record ();
6130
6131 /* 1 */
6132 annotate_field (0);
0d381245
VP
6133 if (part_of_multiple)
6134 {
6135 char *formatted;
0c6773c1 6136 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6137 ui_out_field_string (uiout, "number", formatted);
6138 xfree (formatted);
6139 }
6140 else
6141 {
6142 ui_out_field_int (uiout, "number", b->number);
6143 }
c4093a6a
JM
6144
6145 /* 2 */
6146 annotate_field (1);
0d381245
VP
6147 if (part_of_multiple)
6148 ui_out_field_skip (uiout, "type");
269b11a2
PA
6149 else
6150 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6151
6152 /* 3 */
6153 annotate_field (2);
0d381245
VP
6154 if (part_of_multiple)
6155 ui_out_field_skip (uiout, "disp");
6156 else
2cec12e5 6157 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6158
c4093a6a
JM
6159
6160 /* 4 */
6161 annotate_field (3);
0d381245 6162 if (part_of_multiple)
54e52265 6163 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6164 else
4a64f543
MS
6165 ui_out_field_fmt (uiout, "enabled", "%c",
6166 bpenables[(int) b->enable_state]);
54e52265 6167 ui_out_spaces (uiout, 2);
0d381245 6168
c4093a6a
JM
6169
6170 /* 5 and 6 */
3086aeae 6171 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6172 {
4a64f543
MS
6173 /* Although the print_one can possibly print all locations,
6174 calling it here is not likely to get any nice result. So,
6175 make sure there's just one location. */
0d381245 6176 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6177 b->ops->print_one (b, last_loc);
0d381245 6178 }
3086aeae
DJ
6179 else
6180 switch (b->type)
6181 {
6182 case bp_none:
6183 internal_error (__FILE__, __LINE__,
e2e0b3e5 6184 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6185 break;
c906108c 6186
3086aeae
DJ
6187 case bp_watchpoint:
6188 case bp_hardware_watchpoint:
6189 case bp_read_watchpoint:
6190 case bp_access_watchpoint:
3a5c3e22
PA
6191 {
6192 struct watchpoint *w = (struct watchpoint *) b;
6193
6194 /* Field 4, the address, is omitted (which makes the columns
6195 not line up too nicely with the headers, but the effect
6196 is relatively readable). */
6197 if (opts.addressprint)
6198 ui_out_field_skip (uiout, "addr");
6199 annotate_field (5);
6200 ui_out_field_string (uiout, "what", w->exp_string);
6201 }
3086aeae
DJ
6202 break;
6203
3086aeae
DJ
6204 case bp_breakpoint:
6205 case bp_hardware_breakpoint:
6206 case bp_until:
6207 case bp_finish:
6208 case bp_longjmp:
6209 case bp_longjmp_resume:
e2e4d78b 6210 case bp_longjmp_call_dummy:
186c406b
TT
6211 case bp_exception:
6212 case bp_exception_resume:
3086aeae 6213 case bp_step_resume:
2c03e5be 6214 case bp_hp_step_resume:
3086aeae
DJ
6215 case bp_watchpoint_scope:
6216 case bp_call_dummy:
aa7d318d 6217 case bp_std_terminate:
3086aeae
DJ
6218 case bp_shlib_event:
6219 case bp_thread_event:
6220 case bp_overlay_event:
0fd8e87f 6221 case bp_longjmp_master:
aa7d318d 6222 case bp_std_terminate_master:
186c406b 6223 case bp_exception_master:
1042e4c0 6224 case bp_tracepoint:
7a697b8d 6225 case bp_fast_tracepoint:
0fb4aa4b 6226 case bp_static_tracepoint:
e7e0cddf 6227 case bp_dprintf:
4efc6507 6228 case bp_jit_event:
0e30163f
JK
6229 case bp_gnu_ifunc_resolver:
6230 case bp_gnu_ifunc_resolver_return:
79a45b7d 6231 if (opts.addressprint)
3086aeae
DJ
6232 {
6233 annotate_field (4);
54e52265 6234 if (header_of_multiple)
0d381245 6235 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6236 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6237 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6238 else
5af949e3
UW
6239 ui_out_field_core_addr (uiout, "addr",
6240 loc->gdbarch, loc->address);
3086aeae
DJ
6241 }
6242 annotate_field (5);
0d381245 6243 if (!header_of_multiple)
170b53b2 6244 print_breakpoint_location (b, loc);
0d381245 6245 if (b->loc)
a6d9a66e 6246 *last_loc = b->loc;
3086aeae
DJ
6247 break;
6248 }
c906108c 6249
6c95b8df 6250
998580f1 6251 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6252 {
6253 struct inferior *inf;
998580f1
MK
6254 VEC(int) *inf_num = NULL;
6255 int mi_only = 1;
6c95b8df 6256
998580f1 6257 ALL_INFERIORS (inf)
6c95b8df
PA
6258 {
6259 if (inf->pspace == loc->pspace)
998580f1 6260 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6261 }
998580f1
MK
6262
6263 /* For backward compatibility, don't display inferiors in CLI unless
6264 there are several. Always display for MI. */
6265 if (allflag
6266 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6267 && (number_of_program_spaces () > 1
6268 || number_of_inferiors () > 1)
6269 /* LOC is for existing B, it cannot be in
6270 moribund_locations and thus having NULL OWNER. */
6271 && loc->owner->type != bp_catchpoint))
6272 mi_only = 0;
6273 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6274 VEC_free (int, inf_num);
6c95b8df
PA
6275 }
6276
4a306c9a 6277 if (!part_of_multiple)
c4093a6a 6278 {
4a306c9a
JB
6279 if (b->thread != -1)
6280 {
6281 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6282 "stop only in" line a little further down. */
4a306c9a
JB
6283 ui_out_text (uiout, " thread ");
6284 ui_out_field_int (uiout, "thread", b->thread);
6285 }
6286 else if (b->task != 0)
6287 {
6288 ui_out_text (uiout, " task ");
6289 ui_out_field_int (uiout, "task", b->task);
6290 }
c4093a6a 6291 }
f1310107 6292
8b93c638 6293 ui_out_text (uiout, "\n");
f1310107 6294
348d480f 6295 if (!part_of_multiple)
f1310107
TJB
6296 b->ops->print_one_detail (b, uiout);
6297
0d381245 6298 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6299 {
6300 annotate_field (6);
8b93c638 6301 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6302 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6303 the frame ID. */
5af949e3
UW
6304 ui_out_field_core_addr (uiout, "frame",
6305 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6306 ui_out_text (uiout, "\n");
c4093a6a
JM
6307 }
6308
28010a5d 6309 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6310 {
6311 annotate_field (7);
d77f58be 6312 if (is_tracepoint (b))
1042e4c0
SS
6313 ui_out_text (uiout, "\ttrace only if ");
6314 else
6315 ui_out_text (uiout, "\tstop only if ");
0101ce28 6316 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6317
6318 /* Print whether the target is doing the breakpoint's condition
6319 evaluation. If GDB is doing the evaluation, don't print anything. */
6320 if (is_breakpoint (b)
6321 && breakpoint_condition_evaluation_mode ()
6322 == condition_evaluation_target)
6323 {
6324 ui_out_text (uiout, " (");
6325 ui_out_field_string (uiout, "evaluated-by",
6326 bp_condition_evaluator (b));
6327 ui_out_text (uiout, " evals)");
6328 }
0101ce28
JJ
6329 ui_out_text (uiout, "\n");
6330 }
6331
0d381245 6332 if (!part_of_multiple && b->thread != -1)
c4093a6a 6333 {
4a64f543 6334 /* FIXME should make an annotation for this. */
8b93c638
JM
6335 ui_out_text (uiout, "\tstop only in thread ");
6336 ui_out_field_int (uiout, "thread", b->thread);
6337 ui_out_text (uiout, "\n");
c4093a6a
JM
6338 }
6339
556ec64d
YQ
6340 if (!part_of_multiple)
6341 {
6342 if (b->hit_count)
31f56a27
YQ
6343 {
6344 /* FIXME should make an annotation for this. */
6345 if (is_catchpoint (b))
6346 ui_out_text (uiout, "\tcatchpoint");
6347 else if (is_tracepoint (b))
6348 ui_out_text (uiout, "\ttracepoint");
6349 else
6350 ui_out_text (uiout, "\tbreakpoint");
6351 ui_out_text (uiout, " already hit ");
6352 ui_out_field_int (uiout, "times", b->hit_count);
6353 if (b->hit_count == 1)
6354 ui_out_text (uiout, " time\n");
6355 else
6356 ui_out_text (uiout, " times\n");
6357 }
556ec64d
YQ
6358 else
6359 {
31f56a27
YQ
6360 /* Output the count also if it is zero, but only if this is mi. */
6361 if (ui_out_is_mi_like_p (uiout))
6362 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6363 }
6364 }
8b93c638 6365
0d381245 6366 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6367 {
6368 annotate_field (8);
8b93c638
JM
6369 ui_out_text (uiout, "\tignore next ");
6370 ui_out_field_int (uiout, "ignore", b->ignore_count);
6371 ui_out_text (uiout, " hits\n");
c4093a6a 6372 }
059fb39f 6373
816338b5
SS
6374 /* Note that an enable count of 1 corresponds to "enable once"
6375 behavior, which is reported by the combination of enablement and
6376 disposition, so we don't need to mention it here. */
6377 if (!part_of_multiple && b->enable_count > 1)
6378 {
6379 annotate_field (8);
6380 ui_out_text (uiout, "\tdisable after ");
6381 /* Tweak the wording to clarify that ignore and enable counts
6382 are distinct, and have additive effect. */
6383 if (b->ignore_count)
6384 ui_out_text (uiout, "additional ");
6385 else
6386 ui_out_text (uiout, "next ");
6387 ui_out_field_int (uiout, "enable", b->enable_count);
6388 ui_out_text (uiout, " hits\n");
6389 }
6390
f196051f
SS
6391 if (!part_of_multiple && is_tracepoint (b))
6392 {
6393 struct tracepoint *tp = (struct tracepoint *) b;
6394
6395 if (tp->traceframe_usage)
6396 {
6397 ui_out_text (uiout, "\ttrace buffer usage ");
6398 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6399 ui_out_text (uiout, " bytes\n");
6400 }
6401 }
d3ce09f5 6402
9add0f1b 6403 l = b->commands ? b->commands->commands : NULL;
059fb39f 6404 if (!part_of_multiple && l)
c4093a6a 6405 {
3b31d625
EZ
6406 struct cleanup *script_chain;
6407
c4093a6a 6408 annotate_field (9);
3b31d625 6409 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6410 print_command_lines (uiout, l, 4);
3b31d625 6411 do_cleanups (script_chain);
c4093a6a 6412 }
d24317b4 6413
d9b3f62e 6414 if (is_tracepoint (b))
1042e4c0 6415 {
d9b3f62e
PA
6416 struct tracepoint *t = (struct tracepoint *) b;
6417
6418 if (!part_of_multiple && t->pass_count)
6419 {
6420 annotate_field (10);
6421 ui_out_text (uiout, "\tpass count ");
6422 ui_out_field_int (uiout, "pass", t->pass_count);
6423 ui_out_text (uiout, " \n");
6424 }
f2a8bc8a
YQ
6425
6426 /* Don't display it when tracepoint or tracepoint location is
6427 pending. */
6428 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6429 {
6430 annotate_field (11);
6431
6432 if (ui_out_is_mi_like_p (uiout))
6433 ui_out_field_string (uiout, "installed",
6434 loc->inserted ? "y" : "n");
6435 else
6436 {
6437 if (loc->inserted)
6438 ui_out_text (uiout, "\t");
6439 else
6440 ui_out_text (uiout, "\tnot ");
6441 ui_out_text (uiout, "installed on target\n");
6442 }
6443 }
1042e4c0
SS
6444 }
6445
d24317b4
VP
6446 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6447 {
3a5c3e22
PA
6448 if (is_watchpoint (b))
6449 {
6450 struct watchpoint *w = (struct watchpoint *) b;
6451
6452 ui_out_field_string (uiout, "original-location", w->exp_string);
6453 }
6454 else if (b->addr_string)
d24317b4 6455 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6456 }
c4093a6a 6457}
c5aa993b 6458
0d381245
VP
6459static void
6460print_one_breakpoint (struct breakpoint *b,
4a64f543 6461 struct bp_location **last_loc,
6c95b8df 6462 int allflag)
0d381245 6463{
8d3788bd 6464 struct cleanup *bkpt_chain;
79a45e25 6465 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6466
6467 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6468
12c5a436 6469 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6470 do_cleanups (bkpt_chain);
0d381245
VP
6471
6472 /* If this breakpoint has custom print function,
6473 it's already printed. Otherwise, print individual
6474 locations, if any. */
6475 if (b->ops == NULL || b->ops->print_one == NULL)
6476 {
4a64f543
MS
6477 /* If breakpoint has a single location that is disabled, we
6478 print it as if it had several locations, since otherwise it's
6479 hard to represent "breakpoint enabled, location disabled"
6480 situation.
6481
6482 Note that while hardware watchpoints have several locations
a3be7890 6483 internally, that's not a property exposed to user. */
0d381245 6484 if (b->loc
a5606eee 6485 && !is_hardware_watchpoint (b)
8d3788bd 6486 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6487 {
6488 struct bp_location *loc;
6489 int n = 1;
8d3788bd 6490
0d381245 6491 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6492 {
6493 struct cleanup *inner2 =
6494 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6495 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6496 do_cleanups (inner2);
6497 }
0d381245
VP
6498 }
6499 }
6500}
6501
a6d9a66e
UW
6502static int
6503breakpoint_address_bits (struct breakpoint *b)
6504{
6505 int print_address_bits = 0;
6506 struct bp_location *loc;
6507
6508 for (loc = b->loc; loc; loc = loc->next)
6509 {
c7437ca6
PA
6510 int addr_bit;
6511
6512 /* Software watchpoints that aren't watching memory don't have
6513 an address to print. */
6514 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6515 continue;
6516
6517 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6518 if (addr_bit > print_address_bits)
6519 print_address_bits = addr_bit;
6520 }
6521
6522 return print_address_bits;
6523}
0d381245 6524
c4093a6a
JM
6525struct captured_breakpoint_query_args
6526 {
6527 int bnum;
6528 };
c5aa993b 6529
c4093a6a 6530static int
2b65245e 6531do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6532{
6533 struct captured_breakpoint_query_args *args = data;
52f0bd74 6534 struct breakpoint *b;
a6d9a66e 6535 struct bp_location *dummy_loc = NULL;
cc59ec59 6536
c4093a6a
JM
6537 ALL_BREAKPOINTS (b)
6538 {
6539 if (args->bnum == b->number)
c5aa993b 6540 {
12c5a436 6541 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6542 return GDB_RC_OK;
c5aa993b 6543 }
c4093a6a
JM
6544 }
6545 return GDB_RC_NONE;
6546}
c5aa993b 6547
c4093a6a 6548enum gdb_rc
4a64f543
MS
6549gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6550 char **error_message)
c4093a6a
JM
6551{
6552 struct captured_breakpoint_query_args args;
cc59ec59 6553
c4093a6a
JM
6554 args.bnum = bnum;
6555 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6556 an error. */
b0b13bb4
DJ
6557 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6558 error_message, RETURN_MASK_ALL) < 0)
6559 return GDB_RC_FAIL;
6560 else
6561 return GDB_RC_OK;
c4093a6a 6562}
c5aa993b 6563
09d682a4
TT
6564/* Return true if this breakpoint was set by the user, false if it is
6565 internal or momentary. */
6566
6567int
6568user_breakpoint_p (struct breakpoint *b)
6569{
46c6471b 6570 return b->number > 0;
09d682a4
TT
6571}
6572
7f3b0473 6573/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6574 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6575 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6576 FILTER is non-NULL, call it on each breakpoint and only include the
6577 ones for which it returns non-zero. Return the total number of
6578 breakpoints listed. */
c906108c 6579
d77f58be 6580static int
e5a67952 6581breakpoint_1 (char *args, int allflag,
4a64f543 6582 int (*filter) (const struct breakpoint *))
c4093a6a 6583{
52f0bd74 6584 struct breakpoint *b;
a6d9a66e 6585 struct bp_location *last_loc = NULL;
7f3b0473 6586 int nr_printable_breakpoints;
3b31d625 6587 struct cleanup *bkpttbl_chain;
79a45b7d 6588 struct value_print_options opts;
a6d9a66e 6589 int print_address_bits = 0;
269b11a2 6590 int print_type_col_width = 14;
79a45e25 6591 struct ui_out *uiout = current_uiout;
269b11a2 6592
79a45b7d
TT
6593 get_user_print_options (&opts);
6594
4a64f543
MS
6595 /* Compute the number of rows in the table, as well as the size
6596 required for address fields. */
7f3b0473
AC
6597 nr_printable_breakpoints = 0;
6598 ALL_BREAKPOINTS (b)
e5a67952
MS
6599 {
6600 /* If we have a filter, only list the breakpoints it accepts. */
6601 if (filter && !filter (b))
6602 continue;
6603
6604 /* If we have an "args" string, it is a list of breakpoints to
6605 accept. Skip the others. */
6606 if (args != NULL && *args != '\0')
6607 {
6608 if (allflag && parse_and_eval_long (args) != b->number)
6609 continue;
6610 if (!allflag && !number_is_in_list (args, b->number))
6611 continue;
6612 }
269b11a2 6613
e5a67952
MS
6614 if (allflag || user_breakpoint_p (b))
6615 {
6616 int addr_bit, type_len;
a6d9a66e 6617
e5a67952
MS
6618 addr_bit = breakpoint_address_bits (b);
6619 if (addr_bit > print_address_bits)
6620 print_address_bits = addr_bit;
269b11a2 6621
e5a67952
MS
6622 type_len = strlen (bptype_string (b->type));
6623 if (type_len > print_type_col_width)
6624 print_type_col_width = type_len;
6625
6626 nr_printable_breakpoints++;
6627 }
6628 }
7f3b0473 6629
79a45b7d 6630 if (opts.addressprint)
3b31d625 6631 bkpttbl_chain
3e43a32a
MS
6632 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6633 nr_printable_breakpoints,
3b31d625 6634 "BreakpointTable");
8b93c638 6635 else
3b31d625 6636 bkpttbl_chain
3e43a32a
MS
6637 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6638 nr_printable_breakpoints,
3b31d625 6639 "BreakpointTable");
8b93c638 6640
7f3b0473 6641 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6642 annotate_breakpoints_headers ();
6643 if (nr_printable_breakpoints > 0)
6644 annotate_field (0);
4a64f543 6645 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6646 if (nr_printable_breakpoints > 0)
6647 annotate_field (1);
269b11a2 6648 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6649 "type", "Type"); /* 2 */
d7faa9e7
AC
6650 if (nr_printable_breakpoints > 0)
6651 annotate_field (2);
4a64f543 6652 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6653 if (nr_printable_breakpoints > 0)
6654 annotate_field (3);
54e52265 6655 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6656 if (opts.addressprint)
e5a67952
MS
6657 {
6658 if (nr_printable_breakpoints > 0)
6659 annotate_field (4);
6660 if (print_address_bits <= 32)
6661 ui_out_table_header (uiout, 10, ui_left,
6662 "addr", "Address"); /* 5 */
6663 else
6664 ui_out_table_header (uiout, 18, ui_left,
6665 "addr", "Address"); /* 5 */
6666 }
d7faa9e7
AC
6667 if (nr_printable_breakpoints > 0)
6668 annotate_field (5);
6669 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6670 ui_out_table_body (uiout);
6671 if (nr_printable_breakpoints > 0)
6672 annotate_breakpoints_table ();
7f3b0473 6673
c4093a6a 6674 ALL_BREAKPOINTS (b)
e5a67952
MS
6675 {
6676 QUIT;
6677 /* If we have a filter, only list the breakpoints it accepts. */
6678 if (filter && !filter (b))
6679 continue;
6680
6681 /* If we have an "args" string, it is a list of breakpoints to
6682 accept. Skip the others. */
6683
6684 if (args != NULL && *args != '\0')
6685 {
6686 if (allflag) /* maintenance info breakpoint */
6687 {
6688 if (parse_and_eval_long (args) != b->number)
6689 continue;
6690 }
6691 else /* all others */
6692 {
6693 if (!number_is_in_list (args, b->number))
6694 continue;
6695 }
6696 }
6697 /* We only print out user settable breakpoints unless the
6698 allflag is set. */
6699 if (allflag || user_breakpoint_p (b))
12c5a436 6700 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6701 }
6702
3b31d625 6703 do_cleanups (bkpttbl_chain);
698384cd 6704
7f3b0473 6705 if (nr_printable_breakpoints == 0)
c906108c 6706 {
4a64f543
MS
6707 /* If there's a filter, let the caller decide how to report
6708 empty list. */
d77f58be
SS
6709 if (!filter)
6710 {
e5a67952 6711 if (args == NULL || *args == '\0')
d77f58be
SS
6712 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6713 else
4a64f543 6714 ui_out_message (uiout, 0,
e5a67952
MS
6715 "No breakpoint or watchpoint matching '%s'.\n",
6716 args);
d77f58be 6717 }
c906108c
SS
6718 }
6719 else
c4093a6a 6720 {
a6d9a66e
UW
6721 if (last_loc && !server_command)
6722 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6723 }
c906108c 6724
4a64f543 6725 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6726 there have been breakpoints? */
c906108c 6727 annotate_breakpoints_table_end ();
d77f58be
SS
6728
6729 return nr_printable_breakpoints;
c906108c
SS
6730}
6731
ad443146
SS
6732/* Display the value of default-collect in a way that is generally
6733 compatible with the breakpoint list. */
6734
6735static void
6736default_collect_info (void)
6737{
79a45e25
PA
6738 struct ui_out *uiout = current_uiout;
6739
ad443146
SS
6740 /* If it has no value (which is frequently the case), say nothing; a
6741 message like "No default-collect." gets in user's face when it's
6742 not wanted. */
6743 if (!*default_collect)
6744 return;
6745
6746 /* The following phrase lines up nicely with per-tracepoint collect
6747 actions. */
6748 ui_out_text (uiout, "default collect ");
6749 ui_out_field_string (uiout, "default-collect", default_collect);
6750 ui_out_text (uiout, " \n");
6751}
6752
c906108c 6753static void
e5a67952 6754breakpoints_info (char *args, int from_tty)
c906108c 6755{
e5a67952 6756 breakpoint_1 (args, 0, NULL);
ad443146
SS
6757
6758 default_collect_info ();
d77f58be
SS
6759}
6760
6761static void
e5a67952 6762watchpoints_info (char *args, int from_tty)
d77f58be 6763{
e5a67952 6764 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6765 struct ui_out *uiout = current_uiout;
d77f58be
SS
6766
6767 if (num_printed == 0)
6768 {
e5a67952 6769 if (args == NULL || *args == '\0')
d77f58be
SS
6770 ui_out_message (uiout, 0, "No watchpoints.\n");
6771 else
e5a67952 6772 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6773 }
c906108c
SS
6774}
6775
7a292a7a 6776static void
e5a67952 6777maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6778{
e5a67952 6779 breakpoint_1 (args, 1, NULL);
ad443146
SS
6780
6781 default_collect_info ();
c906108c
SS
6782}
6783
0d381245 6784static int
714835d5 6785breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6786 struct program_space *pspace,
714835d5 6787 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6788{
6789 struct bp_location *bl = b->loc;
cc59ec59 6790
0d381245
VP
6791 for (; bl; bl = bl->next)
6792 {
6c95b8df
PA
6793 if (bl->pspace == pspace
6794 && bl->address == pc
0d381245
VP
6795 && (!overlay_debugging || bl->section == section))
6796 return 1;
6797 }
6798 return 0;
6799}
6800
672f9b60 6801/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6802 concerns with logical breakpoints, so we match program spaces, not
6803 address spaces. */
c906108c
SS
6804
6805static void
6c95b8df
PA
6806describe_other_breakpoints (struct gdbarch *gdbarch,
6807 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6808 struct obj_section *section, int thread)
c906108c 6809{
52f0bd74
AC
6810 int others = 0;
6811 struct breakpoint *b;
c906108c
SS
6812
6813 ALL_BREAKPOINTS (b)
672f9b60
KP
6814 others += (user_breakpoint_p (b)
6815 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6816 if (others > 0)
6817 {
a3f17187
AC
6818 if (others == 1)
6819 printf_filtered (_("Note: breakpoint "));
6820 else /* if (others == ???) */
6821 printf_filtered (_("Note: breakpoints "));
c906108c 6822 ALL_BREAKPOINTS (b)
672f9b60 6823 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6824 {
6825 others--;
6826 printf_filtered ("%d", b->number);
6827 if (b->thread == -1 && thread != -1)
6828 printf_filtered (" (all threads)");
6829 else if (b->thread != -1)
6830 printf_filtered (" (thread %d)", b->thread);
6831 printf_filtered ("%s%s ",
059fb39f 6832 ((b->enable_state == bp_disabled
f8eba3c6 6833 || b->enable_state == bp_call_disabled)
0d381245
VP
6834 ? " (disabled)"
6835 : b->enable_state == bp_permanent
6836 ? " (permanent)"
6837 : ""),
6838 (others > 1) ? ","
6839 : ((others == 1) ? " and" : ""));
6840 }
a3f17187 6841 printf_filtered (_("also set at pc "));
5af949e3 6842 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6843 printf_filtered (".\n");
6844 }
6845}
6846\f
c906108c 6847
e4f237da
KB
6848/* Return true iff it is meaningful to use the address member of
6849 BPT. For some breakpoint types, the address member is irrelevant
6850 and it makes no sense to attempt to compare it to other addresses
6851 (or use it for any other purpose either).
6852
4a64f543
MS
6853 More specifically, each of the following breakpoint types will
6854 always have a zero valued address and we don't want to mark
6855 breakpoints of any of these types to be a duplicate of an actual
6856 breakpoint at address zero:
e4f237da
KB
6857
6858 bp_watchpoint
2d134ed3
PA
6859 bp_catchpoint
6860
6861*/
e4f237da
KB
6862
6863static int
6864breakpoint_address_is_meaningful (struct breakpoint *bpt)
6865{
6866 enum bptype type = bpt->type;
6867
2d134ed3
PA
6868 return (type != bp_watchpoint && type != bp_catchpoint);
6869}
6870
6871/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6872 true if LOC1 and LOC2 represent the same watchpoint location. */
6873
6874static int
4a64f543
MS
6875watchpoint_locations_match (struct bp_location *loc1,
6876 struct bp_location *loc2)
2d134ed3 6877{
3a5c3e22
PA
6878 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6879 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6880
6881 /* Both of them must exist. */
6882 gdb_assert (w1 != NULL);
6883 gdb_assert (w2 != NULL);
2bdf28a0 6884
4a64f543
MS
6885 /* If the target can evaluate the condition expression in hardware,
6886 then we we need to insert both watchpoints even if they are at
6887 the same place. Otherwise the watchpoint will only trigger when
6888 the condition of whichever watchpoint was inserted evaluates to
6889 true, not giving a chance for GDB to check the condition of the
6890 other watchpoint. */
3a5c3e22 6891 if ((w1->cond_exp
4a64f543
MS
6892 && target_can_accel_watchpoint_condition (loc1->address,
6893 loc1->length,
0cf6dd15 6894 loc1->watchpoint_type,
3a5c3e22
PA
6895 w1->cond_exp))
6896 || (w2->cond_exp
4a64f543
MS
6897 && target_can_accel_watchpoint_condition (loc2->address,
6898 loc2->length,
0cf6dd15 6899 loc2->watchpoint_type,
3a5c3e22 6900 w2->cond_exp)))
0cf6dd15
TJB
6901 return 0;
6902
85d721b8
PA
6903 /* Note that this checks the owner's type, not the location's. In
6904 case the target does not support read watchpoints, but does
6905 support access watchpoints, we'll have bp_read_watchpoint
6906 watchpoints with hw_access locations. Those should be considered
6907 duplicates of hw_read locations. The hw_read locations will
6908 become hw_access locations later. */
2d134ed3
PA
6909 return (loc1->owner->type == loc2->owner->type
6910 && loc1->pspace->aspace == loc2->pspace->aspace
6911 && loc1->address == loc2->address
6912 && loc1->length == loc2->length);
e4f237da
KB
6913}
6914
31e77af2 6915/* See breakpoint.h. */
6c95b8df 6916
31e77af2 6917int
6c95b8df
PA
6918breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6919 struct address_space *aspace2, CORE_ADDR addr2)
6920{
f5656ead 6921 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6922 || aspace1 == aspace2)
6923 && addr1 == addr2);
6924}
6925
f1310107
TJB
6926/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6927 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6928 matches ASPACE2. On targets that have global breakpoints, the address
6929 space doesn't really matter. */
6930
6931static int
6932breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6933 int len1, struct address_space *aspace2,
6934 CORE_ADDR addr2)
6935{
f5656ead 6936 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6937 || aspace1 == aspace2)
6938 && addr2 >= addr1 && addr2 < addr1 + len1);
6939}
6940
6941/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6942 a ranged breakpoint. In most targets, a match happens only if ASPACE
6943 matches the breakpoint's address space. On targets that have global
6944 breakpoints, the address space doesn't really matter. */
6945
6946static int
6947breakpoint_location_address_match (struct bp_location *bl,
6948 struct address_space *aspace,
6949 CORE_ADDR addr)
6950{
6951 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6952 aspace, addr)
6953 || (bl->length
6954 && breakpoint_address_match_range (bl->pspace->aspace,
6955 bl->address, bl->length,
6956 aspace, addr)));
6957}
6958
1e4d1764
YQ
6959/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6960 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6961 true, otherwise returns false. */
6962
6963static int
6964tracepoint_locations_match (struct bp_location *loc1,
6965 struct bp_location *loc2)
6966{
6967 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6968 /* Since tracepoint locations are never duplicated with others', tracepoint
6969 locations at the same address of different tracepoints are regarded as
6970 different locations. */
6971 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6972 else
6973 return 0;
6974}
6975
2d134ed3
PA
6976/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6977 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6978 represent the same location. */
6979
6980static int
4a64f543
MS
6981breakpoint_locations_match (struct bp_location *loc1,
6982 struct bp_location *loc2)
2d134ed3 6983{
2bdf28a0
JK
6984 int hw_point1, hw_point2;
6985
6986 /* Both of them must not be in moribund_locations. */
6987 gdb_assert (loc1->owner != NULL);
6988 gdb_assert (loc2->owner != NULL);
6989
6990 hw_point1 = is_hardware_watchpoint (loc1->owner);
6991 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6992
6993 if (hw_point1 != hw_point2)
6994 return 0;
6995 else if (hw_point1)
6996 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6997 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6998 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6999 else
f1310107
TJB
7000 /* We compare bp_location.length in order to cover ranged breakpoints. */
7001 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7002 loc2->pspace->aspace, loc2->address)
7003 && loc1->length == loc2->length);
2d134ed3
PA
7004}
7005
76897487
KB
7006static void
7007breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7008 int bnum, int have_bnum)
7009{
f63fbe86
MS
7010 /* The longest string possibly returned by hex_string_custom
7011 is 50 chars. These must be at least that big for safety. */
7012 char astr1[64];
7013 char astr2[64];
76897487 7014
bb599908
PH
7015 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7016 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7017 if (have_bnum)
8a3fe4f8 7018 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7019 bnum, astr1, astr2);
7020 else
8a3fe4f8 7021 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7022}
7023
4a64f543
MS
7024/* Adjust a breakpoint's address to account for architectural
7025 constraints on breakpoint placement. Return the adjusted address.
7026 Note: Very few targets require this kind of adjustment. For most
7027 targets, this function is simply the identity function. */
76897487
KB
7028
7029static CORE_ADDR
a6d9a66e
UW
7030adjust_breakpoint_address (struct gdbarch *gdbarch,
7031 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7032{
a6d9a66e 7033 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7034 {
7035 /* Very few targets need any kind of breakpoint adjustment. */
7036 return bpaddr;
7037 }
88f7da05
KB
7038 else if (bptype == bp_watchpoint
7039 || bptype == bp_hardware_watchpoint
7040 || bptype == bp_read_watchpoint
7041 || bptype == bp_access_watchpoint
fe798b75 7042 || bptype == bp_catchpoint)
88f7da05
KB
7043 {
7044 /* Watchpoints and the various bp_catch_* eventpoints should not
7045 have their addresses modified. */
7046 return bpaddr;
7047 }
76897487
KB
7048 else
7049 {
7050 CORE_ADDR adjusted_bpaddr;
7051
7052 /* Some targets have architectural constraints on the placement
7053 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7054 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7055
7056 /* An adjusted breakpoint address can significantly alter
7057 a user's expectations. Print a warning if an adjustment
7058 is required. */
7059 if (adjusted_bpaddr != bpaddr)
7060 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7061
7062 return adjusted_bpaddr;
7063 }
7064}
7065
28010a5d
PA
7066void
7067init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7068 struct breakpoint *owner)
7cc221ef 7069{
7cc221ef
DJ
7070 memset (loc, 0, sizeof (*loc));
7071
348d480f
PA
7072 gdb_assert (ops != NULL);
7073
28010a5d
PA
7074 loc->ops = ops;
7075 loc->owner = owner;
511a6cd4 7076 loc->cond = NULL;
b775012e 7077 loc->cond_bytecode = NULL;
0d381245
VP
7078 loc->shlib_disabled = 0;
7079 loc->enabled = 1;
e049a4b5 7080
28010a5d 7081 switch (owner->type)
e049a4b5
DJ
7082 {
7083 case bp_breakpoint:
7084 case bp_until:
7085 case bp_finish:
7086 case bp_longjmp:
7087 case bp_longjmp_resume:
e2e4d78b 7088 case bp_longjmp_call_dummy:
186c406b
TT
7089 case bp_exception:
7090 case bp_exception_resume:
e049a4b5 7091 case bp_step_resume:
2c03e5be 7092 case bp_hp_step_resume:
e049a4b5
DJ
7093 case bp_watchpoint_scope:
7094 case bp_call_dummy:
aa7d318d 7095 case bp_std_terminate:
e049a4b5
DJ
7096 case bp_shlib_event:
7097 case bp_thread_event:
7098 case bp_overlay_event:
4efc6507 7099 case bp_jit_event:
0fd8e87f 7100 case bp_longjmp_master:
aa7d318d 7101 case bp_std_terminate_master:
186c406b 7102 case bp_exception_master:
0e30163f
JK
7103 case bp_gnu_ifunc_resolver:
7104 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7105 case bp_dprintf:
e049a4b5 7106 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7107 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7108 break;
7109 case bp_hardware_breakpoint:
7110 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7111 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7112 break;
7113 case bp_hardware_watchpoint:
7114 case bp_read_watchpoint:
7115 case bp_access_watchpoint:
7116 loc->loc_type = bp_loc_hardware_watchpoint;
7117 break;
7118 case bp_watchpoint:
ce78b96d 7119 case bp_catchpoint:
15c3d785
PA
7120 case bp_tracepoint:
7121 case bp_fast_tracepoint:
0fb4aa4b 7122 case bp_static_tracepoint:
e049a4b5
DJ
7123 loc->loc_type = bp_loc_other;
7124 break;
7125 default:
e2e0b3e5 7126 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7127 }
7128
f431efe5 7129 loc->refc = 1;
28010a5d
PA
7130}
7131
7132/* Allocate a struct bp_location. */
7133
7134static struct bp_location *
7135allocate_bp_location (struct breakpoint *bpt)
7136{
348d480f
PA
7137 return bpt->ops->allocate_location (bpt);
7138}
7cc221ef 7139
f431efe5
PA
7140static void
7141free_bp_location (struct bp_location *loc)
fe3f5fa8 7142{
348d480f 7143 loc->ops->dtor (loc);
fe3f5fa8
VP
7144 xfree (loc);
7145}
7146
f431efe5
PA
7147/* Increment reference count. */
7148
7149static void
7150incref_bp_location (struct bp_location *bl)
7151{
7152 ++bl->refc;
7153}
7154
7155/* Decrement reference count. If the reference count reaches 0,
7156 destroy the bp_location. Sets *BLP to NULL. */
7157
7158static void
7159decref_bp_location (struct bp_location **blp)
7160{
0807b50c
PA
7161 gdb_assert ((*blp)->refc > 0);
7162
f431efe5
PA
7163 if (--(*blp)->refc == 0)
7164 free_bp_location (*blp);
7165 *blp = NULL;
7166}
7167
346774a9 7168/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7169
346774a9
PA
7170static void
7171add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7172{
346774a9 7173 struct breakpoint *b1;
c906108c 7174
346774a9
PA
7175 /* Add this breakpoint to the end of the chain so that a list of
7176 breakpoints will come out in order of increasing numbers. */
7177
7178 b1 = breakpoint_chain;
7179 if (b1 == 0)
7180 breakpoint_chain = b;
7181 else
7182 {
7183 while (b1->next)
7184 b1 = b1->next;
7185 b1->next = b;
7186 }
7187}
7188
7189/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7190
7191static void
7192init_raw_breakpoint_without_location (struct breakpoint *b,
7193 struct gdbarch *gdbarch,
28010a5d 7194 enum bptype bptype,
c0a91b2b 7195 const struct breakpoint_ops *ops)
346774a9 7196{
c906108c 7197 memset (b, 0, sizeof (*b));
2219d63c 7198
348d480f
PA
7199 gdb_assert (ops != NULL);
7200
28010a5d 7201 b->ops = ops;
4d28f7a8 7202 b->type = bptype;
a6d9a66e 7203 b->gdbarch = gdbarch;
c906108c
SS
7204 b->language = current_language->la_language;
7205 b->input_radix = input_radix;
7206 b->thread = -1;
b5de0fa7 7207 b->enable_state = bp_enabled;
c906108c
SS
7208 b->next = 0;
7209 b->silent = 0;
7210 b->ignore_count = 0;
7211 b->commands = NULL;
818dd999 7212 b->frame_id = null_frame_id;
0d381245 7213 b->condition_not_parsed = 0;
84f4c1fe 7214 b->py_bp_object = NULL;
d0fb5eae 7215 b->related_breakpoint = b;
346774a9
PA
7216}
7217
7218/* Helper to set_raw_breakpoint below. Creates a breakpoint
7219 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7220
7221static struct breakpoint *
7222set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7223 enum bptype bptype,
c0a91b2b 7224 const struct breakpoint_ops *ops)
346774a9
PA
7225{
7226 struct breakpoint *b = XNEW (struct breakpoint);
7227
348d480f 7228 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7229 add_to_breakpoint_chain (b);
0d381245
VP
7230 return b;
7231}
7232
0e30163f
JK
7233/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7234 resolutions should be made as the user specified the location explicitly
7235 enough. */
7236
0d381245 7237static void
0e30163f 7238set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7239{
2bdf28a0
JK
7240 gdb_assert (loc->owner != NULL);
7241
0d381245 7242 if (loc->owner->type == bp_breakpoint
1042e4c0 7243 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7244 || is_tracepoint (loc->owner))
0d381245 7245 {
0e30163f 7246 int is_gnu_ifunc;
2c02bd72 7247 const char *function_name;
6a3a010b 7248 CORE_ADDR func_addr;
0e30163f 7249
2c02bd72 7250 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7251 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7252
7253 if (is_gnu_ifunc && !explicit_loc)
7254 {
7255 struct breakpoint *b = loc->owner;
7256
7257 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7258 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7259 &loc->requested_address))
7260 {
7261 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7262 loc->address = adjust_breakpoint_address (loc->gdbarch,
7263 loc->requested_address,
7264 b->type);
7265 }
7266 else if (b->type == bp_breakpoint && b->loc == loc
7267 && loc->next == NULL && b->related_breakpoint == b)
7268 {
7269 /* Create only the whole new breakpoint of this type but do not
7270 mess more complicated breakpoints with multiple locations. */
7271 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7272 /* Remember the resolver's address for use by the return
7273 breakpoint. */
7274 loc->related_address = func_addr;
0e30163f
JK
7275 }
7276 }
7277
2c02bd72
DE
7278 if (function_name)
7279 loc->function_name = xstrdup (function_name);
0d381245
VP
7280 }
7281}
7282
a6d9a66e 7283/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7284struct gdbarch *
a6d9a66e
UW
7285get_sal_arch (struct symtab_and_line sal)
7286{
7287 if (sal.section)
7288 return get_objfile_arch (sal.section->objfile);
7289 if (sal.symtab)
7290 return get_objfile_arch (sal.symtab->objfile);
7291
7292 return NULL;
7293}
7294
346774a9
PA
7295/* Low level routine for partially initializing a breakpoint of type
7296 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7297 file name, and line number are provided by SAL.
0d381245
VP
7298
7299 It is expected that the caller will complete the initialization of
7300 the newly created breakpoint struct as well as output any status
c56053d2 7301 information regarding the creation of a new breakpoint. */
0d381245 7302
346774a9
PA
7303static void
7304init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7305 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7306 const struct breakpoint_ops *ops)
0d381245 7307{
28010a5d 7308 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7309
3742cc8b 7310 add_location_to_breakpoint (b, &sal);
0d381245 7311
6c95b8df
PA
7312 if (bptype != bp_catchpoint)
7313 gdb_assert (sal.pspace != NULL);
7314
f8eba3c6
TT
7315 /* Store the program space that was used to set the breakpoint,
7316 except for ordinary breakpoints, which are independent of the
7317 program space. */
7318 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7319 b->pspace = sal.pspace;
346774a9 7320}
c906108c 7321
346774a9
PA
7322/* set_raw_breakpoint is a low level routine for allocating and
7323 partially initializing a breakpoint of type BPTYPE. The newly
7324 created breakpoint's address, section, source file name, and line
7325 number are provided by SAL. The newly created and partially
7326 initialized breakpoint is added to the breakpoint chain and
7327 is also returned as the value of this function.
7328
7329 It is expected that the caller will complete the initialization of
7330 the newly created breakpoint struct as well as output any status
7331 information regarding the creation of a new breakpoint. In
7332 particular, set_raw_breakpoint does NOT set the breakpoint
7333 number! Care should be taken to not allow an error to occur
7334 prior to completing the initialization of the breakpoint. If this
7335 should happen, a bogus breakpoint will be left on the chain. */
7336
7337struct breakpoint *
7338set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7339 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7340 const struct breakpoint_ops *ops)
346774a9
PA
7341{
7342 struct breakpoint *b = XNEW (struct breakpoint);
7343
348d480f 7344 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7345 add_to_breakpoint_chain (b);
c906108c
SS
7346 return b;
7347}
7348
c2c6d25f
JM
7349
7350/* Note that the breakpoint object B describes a permanent breakpoint
7351 instruction, hard-wired into the inferior's code. */
7352void
7353make_breakpoint_permanent (struct breakpoint *b)
7354{
0d381245 7355 struct bp_location *bl;
cc59ec59 7356
b5de0fa7 7357 b->enable_state = bp_permanent;
c2c6d25f 7358
4a64f543
MS
7359 /* By definition, permanent breakpoints are already present in the
7360 code. Mark all locations as inserted. For now,
7361 make_breakpoint_permanent is called in just one place, so it's
7362 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7363 multiple locations or not, but it's easy to implement. */
0d381245
VP
7364 for (bl = b->loc; bl; bl = bl->next)
7365 bl->inserted = 1;
c2c6d25f
JM
7366}
7367
53a5351d 7368/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7369 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7370 initiated the operation. */
c906108c
SS
7371
7372void
186c406b 7373set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7374{
35df4500 7375 struct breakpoint *b, *b_tmp;
186c406b 7376 int thread = tp->num;
0fd8e87f
UW
7377
7378 /* To avoid having to rescan all objfile symbols at every step,
7379 we maintain a list of continually-inserted but always disabled
7380 longjmp "master" breakpoints. Here, we simply create momentary
7381 clones of those and enable them for the requested thread. */
35df4500 7382 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7383 if (b->pspace == current_program_space
186c406b
TT
7384 && (b->type == bp_longjmp_master
7385 || b->type == bp_exception_master))
0fd8e87f 7386 {
06edf0c0
PA
7387 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7388 struct breakpoint *clone;
cc59ec59 7389
e2e4d78b
JK
7390 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7391 after their removal. */
06edf0c0 7392 clone = momentary_breakpoint_from_master (b, type,
e2e4d78b 7393 &longjmp_breakpoint_ops);
0fd8e87f
UW
7394 clone->thread = thread;
7395 }
186c406b
TT
7396
7397 tp->initiating_frame = frame;
c906108c
SS
7398}
7399
611c83ae 7400/* Delete all longjmp breakpoints from THREAD. */
c906108c 7401void
611c83ae 7402delete_longjmp_breakpoint (int thread)
c906108c 7403{
35df4500 7404 struct breakpoint *b, *b_tmp;
c906108c 7405
35df4500 7406 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7407 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7408 {
7409 if (b->thread == thread)
7410 delete_breakpoint (b);
7411 }
c906108c
SS
7412}
7413
f59f708a
PA
7414void
7415delete_longjmp_breakpoint_at_next_stop (int thread)
7416{
7417 struct breakpoint *b, *b_tmp;
7418
7419 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7420 if (b->type == bp_longjmp || b->type == bp_exception)
7421 {
7422 if (b->thread == thread)
7423 b->disposition = disp_del_at_next_stop;
7424 }
7425}
7426
e2e4d78b
JK
7427/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7428 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7429 pointer to any of them. Return NULL if this system cannot place longjmp
7430 breakpoints. */
7431
7432struct breakpoint *
7433set_longjmp_breakpoint_for_call_dummy (void)
7434{
7435 struct breakpoint *b, *retval = NULL;
7436
7437 ALL_BREAKPOINTS (b)
7438 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7439 {
7440 struct breakpoint *new_b;
7441
7442 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7443 &momentary_breakpoint_ops);
7444 new_b->thread = pid_to_thread_id (inferior_ptid);
7445
7446 /* Link NEW_B into the chain of RETVAL breakpoints. */
7447
7448 gdb_assert (new_b->related_breakpoint == new_b);
7449 if (retval == NULL)
7450 retval = new_b;
7451 new_b->related_breakpoint = retval;
7452 while (retval->related_breakpoint != new_b->related_breakpoint)
7453 retval = retval->related_breakpoint;
7454 retval->related_breakpoint = new_b;
7455 }
7456
7457 return retval;
7458}
7459
7460/* Verify all existing dummy frames and their associated breakpoints for
7461 THREAD. Remove those which can no longer be found in the current frame
7462 stack.
7463
7464 You should call this function only at places where it is safe to currently
7465 unwind the whole stack. Failed stack unwind would discard live dummy
7466 frames. */
7467
7468void
7469check_longjmp_breakpoint_for_call_dummy (int thread)
7470{
7471 struct breakpoint *b, *b_tmp;
7472
7473 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7474 if (b->type == bp_longjmp_call_dummy && b->thread == thread)
7475 {
7476 struct breakpoint *dummy_b = b->related_breakpoint;
7477
7478 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7479 dummy_b = dummy_b->related_breakpoint;
7480 if (dummy_b->type != bp_call_dummy
7481 || frame_find_by_id (dummy_b->frame_id) != NULL)
7482 continue;
7483
7484 dummy_frame_discard (dummy_b->frame_id);
7485
7486 while (b->related_breakpoint != b)
7487 {
7488 if (b_tmp == b->related_breakpoint)
7489 b_tmp = b->related_breakpoint->next;
7490 delete_breakpoint (b->related_breakpoint);
7491 }
7492 delete_breakpoint (b);
7493 }
7494}
7495
1900040c
MS
7496void
7497enable_overlay_breakpoints (void)
7498{
52f0bd74 7499 struct breakpoint *b;
1900040c
MS
7500
7501 ALL_BREAKPOINTS (b)
7502 if (b->type == bp_overlay_event)
7503 {
7504 b->enable_state = bp_enabled;
b60e7edf 7505 update_global_location_list (1);
c02f5703 7506 overlay_events_enabled = 1;
1900040c
MS
7507 }
7508}
7509
7510void
7511disable_overlay_breakpoints (void)
7512{
52f0bd74 7513 struct breakpoint *b;
1900040c
MS
7514
7515 ALL_BREAKPOINTS (b)
7516 if (b->type == bp_overlay_event)
7517 {
7518 b->enable_state = bp_disabled;
b60e7edf 7519 update_global_location_list (0);
c02f5703 7520 overlay_events_enabled = 0;
1900040c
MS
7521 }
7522}
7523
aa7d318d
TT
7524/* Set an active std::terminate breakpoint for each std::terminate
7525 master breakpoint. */
7526void
7527set_std_terminate_breakpoint (void)
7528{
35df4500 7529 struct breakpoint *b, *b_tmp;
aa7d318d 7530
35df4500 7531 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7532 if (b->pspace == current_program_space
7533 && b->type == bp_std_terminate_master)
7534 {
06edf0c0
PA
7535 momentary_breakpoint_from_master (b, bp_std_terminate,
7536 &momentary_breakpoint_ops);
aa7d318d
TT
7537 }
7538}
7539
7540/* Delete all the std::terminate breakpoints. */
7541void
7542delete_std_terminate_breakpoint (void)
7543{
35df4500 7544 struct breakpoint *b, *b_tmp;
aa7d318d 7545
35df4500 7546 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7547 if (b->type == bp_std_terminate)
7548 delete_breakpoint (b);
7549}
7550
c4093a6a 7551struct breakpoint *
a6d9a66e 7552create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7553{
7554 struct breakpoint *b;
c4093a6a 7555
06edf0c0
PA
7556 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7557 &internal_breakpoint_ops);
7558
b5de0fa7 7559 b->enable_state = bp_enabled;
c4093a6a 7560 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7561 b->addr_string
7562 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7563
b60e7edf 7564 update_global_location_list_nothrow (1);
74960c60 7565
c4093a6a
JM
7566 return b;
7567}
7568
7569void
7570remove_thread_event_breakpoints (void)
7571{
35df4500 7572 struct breakpoint *b, *b_tmp;
c4093a6a 7573
35df4500 7574 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7575 if (b->type == bp_thread_event
7576 && b->loc->pspace == current_program_space)
c4093a6a
JM
7577 delete_breakpoint (b);
7578}
7579
0101ce28
JJ
7580struct lang_and_radix
7581 {
7582 enum language lang;
7583 int radix;
7584 };
7585
4efc6507
DE
7586/* Create a breakpoint for JIT code registration and unregistration. */
7587
7588struct breakpoint *
7589create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7590{
7591 struct breakpoint *b;
7592
06edf0c0
PA
7593 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7594 &internal_breakpoint_ops);
4efc6507
DE
7595 update_global_location_list_nothrow (1);
7596 return b;
7597}
0101ce28 7598
03673fc7
PP
7599/* Remove JIT code registration and unregistration breakpoint(s). */
7600
7601void
7602remove_jit_event_breakpoints (void)
7603{
7604 struct breakpoint *b, *b_tmp;
7605
7606 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7607 if (b->type == bp_jit_event
7608 && b->loc->pspace == current_program_space)
7609 delete_breakpoint (b);
7610}
7611
cae688ec
JJ
7612void
7613remove_solib_event_breakpoints (void)
7614{
35df4500 7615 struct breakpoint *b, *b_tmp;
cae688ec 7616
35df4500 7617 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7618 if (b->type == bp_shlib_event
7619 && b->loc->pspace == current_program_space)
cae688ec
JJ
7620 delete_breakpoint (b);
7621}
7622
7623struct breakpoint *
a6d9a66e 7624create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
7625{
7626 struct breakpoint *b;
7627
06edf0c0
PA
7628 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7629 &internal_breakpoint_ops);
b60e7edf 7630 update_global_location_list_nothrow (1);
cae688ec
JJ
7631 return b;
7632}
7633
7634/* Disable any breakpoints that are on code in shared libraries. Only
7635 apply to enabled breakpoints, disabled ones can just stay disabled. */
7636
7637void
cb851954 7638disable_breakpoints_in_shlibs (void)
cae688ec 7639{
876fa593 7640 struct bp_location *loc, **locp_tmp;
cae688ec 7641
876fa593 7642 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7643 {
2bdf28a0 7644 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7645 struct breakpoint *b = loc->owner;
2bdf28a0 7646
4a64f543
MS
7647 /* We apply the check to all breakpoints, including disabled for
7648 those with loc->duplicate set. This is so that when breakpoint
7649 becomes enabled, or the duplicate is removed, gdb will try to
7650 insert all breakpoints. If we don't set shlib_disabled here,
7651 we'll try to insert those breakpoints and fail. */
1042e4c0 7652 if (((b->type == bp_breakpoint)
508ccb1f 7653 || (b->type == bp_jit_event)
1042e4c0 7654 || (b->type == bp_hardware_breakpoint)
d77f58be 7655 || (is_tracepoint (b)))
6c95b8df 7656 && loc->pspace == current_program_space
0d381245 7657 && !loc->shlib_disabled
6c95b8df 7658 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7659 )
0d381245
VP
7660 {
7661 loc->shlib_disabled = 1;
7662 }
cae688ec
JJ
7663 }
7664}
7665
63644780
NB
7666/* Disable any breakpoints and tracepoints that are in SOLIB upon
7667 notification of unloaded_shlib. Only apply to enabled breakpoints,
7668 disabled ones can just stay disabled. */
84acb35a 7669
75149521 7670static void
84acb35a
JJ
7671disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7672{
876fa593 7673 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7674 int disabled_shlib_breaks = 0;
7675
c86cf029
VP
7676 /* SunOS a.out shared libraries are always mapped, so do not
7677 disable breakpoints; they will only be reported as unloaded
7678 through clear_solib when GDB discards its shared library
7679 list. See clear_solib for more information. */
7680 if (exec_bfd != NULL
7681 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7682 return;
7683
876fa593 7684 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7685 {
2bdf28a0 7686 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7687 struct breakpoint *b = loc->owner;
cc59ec59 7688
1e4d1764 7689 if (solib->pspace == loc->pspace
e2dd7057 7690 && !loc->shlib_disabled
1e4d1764
YQ
7691 && (((b->type == bp_breakpoint
7692 || b->type == bp_jit_event
7693 || b->type == bp_hardware_breakpoint)
7694 && (loc->loc_type == bp_loc_hardware_breakpoint
7695 || loc->loc_type == bp_loc_software_breakpoint))
7696 || is_tracepoint (b))
e2dd7057 7697 && solib_contains_address_p (solib, loc->address))
84acb35a 7698 {
e2dd7057
PP
7699 loc->shlib_disabled = 1;
7700 /* At this point, we cannot rely on remove_breakpoint
7701 succeeding so we must mark the breakpoint as not inserted
7702 to prevent future errors occurring in remove_breakpoints. */
7703 loc->inserted = 0;
8d3788bd
VP
7704
7705 /* This may cause duplicate notifications for the same breakpoint. */
7706 observer_notify_breakpoint_modified (b);
7707
e2dd7057
PP
7708 if (!disabled_shlib_breaks)
7709 {
7710 target_terminal_ours_for_output ();
3e43a32a
MS
7711 warning (_("Temporarily disabling breakpoints "
7712 "for unloaded shared library \"%s\""),
e2dd7057 7713 solib->so_name);
84acb35a 7714 }
e2dd7057 7715 disabled_shlib_breaks = 1;
84acb35a
JJ
7716 }
7717 }
84acb35a
JJ
7718}
7719
63644780
NB
7720/* Disable any breakpoints and tracepoints in OBJFILE upon
7721 notification of free_objfile. Only apply to enabled breakpoints,
7722 disabled ones can just stay disabled. */
7723
7724static void
7725disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7726{
7727 struct breakpoint *b;
7728
7729 if (objfile == NULL)
7730 return;
7731
08351840
PA
7732 /* OBJF_USERLOADED are dynamic modules manually managed by the user
7733 with add-symbol-file/remove-symbol-file. Similarly to how
7734 breakpoints in shared libraries are handled in response to
7735 "nosharedlibrary", mark breakpoints in OBJF_USERLOADED modules
7736 shlib_disabled so they end up uninserted on the next global
7737 location list update. Shared libraries not loaded by the user
7738 aren't handled here -- they're already handled in
7739 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7740 solib_unloaded observer. We skip objfiles that are not
7741 OBJF_USERLOADED (nor OBJF_SHARED) as those aren't considered
7742 dynamic objects (e.g. the main objfile). */
7743 if ((objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7744 return;
7745
7746 ALL_BREAKPOINTS (b)
7747 {
7748 struct bp_location *loc;
7749 int bp_modified = 0;
7750
7751 if (!is_breakpoint (b) && !is_tracepoint (b))
7752 continue;
7753
7754 for (loc = b->loc; loc != NULL; loc = loc->next)
7755 {
7756 CORE_ADDR loc_addr = loc->address;
7757
7758 if (loc->loc_type != bp_loc_hardware_breakpoint
7759 && loc->loc_type != bp_loc_software_breakpoint)
7760 continue;
7761
7762 if (loc->shlib_disabled != 0)
7763 continue;
7764
7765 if (objfile->pspace != loc->pspace)
7766 continue;
7767
7768 if (loc->loc_type != bp_loc_hardware_breakpoint
7769 && loc->loc_type != bp_loc_software_breakpoint)
7770 continue;
7771
7772 if (is_addr_in_objfile (loc_addr, objfile))
7773 {
7774 loc->shlib_disabled = 1;
08351840
PA
7775 /* At this point, we don't know whether the object was
7776 unmapped from the inferior or not, so leave the
7777 inserted flag alone. We'll handle failure to
7778 uninsert quietly, in case the object was indeed
7779 unmapped. */
63644780
NB
7780
7781 mark_breakpoint_location_modified (loc);
7782
7783 bp_modified = 1;
7784 }
7785 }
7786
7787 if (bp_modified)
7788 observer_notify_breakpoint_modified (b);
7789 }
7790}
7791
ce78b96d
JB
7792/* FORK & VFORK catchpoints. */
7793
e29a4733
PA
7794/* An instance of this type is used to represent a fork or vfork
7795 catchpoint. It includes a "struct breakpoint" as a kind of base
7796 class; users downcast to "struct breakpoint *" when needed. A
7797 breakpoint is really of this type iff its ops pointer points to
7798 CATCH_FORK_BREAKPOINT_OPS. */
7799
7800struct fork_catchpoint
7801{
7802 /* The base class. */
7803 struct breakpoint base;
7804
7805 /* Process id of a child process whose forking triggered this
7806 catchpoint. This field is only valid immediately after this
7807 catchpoint has triggered. */
7808 ptid_t forked_inferior_pid;
7809};
7810
4a64f543
MS
7811/* Implement the "insert" breakpoint_ops method for fork
7812 catchpoints. */
ce78b96d 7813
77b06cd7
TJB
7814static int
7815insert_catch_fork (struct bp_location *bl)
ce78b96d 7816{
dfd4cc63 7817 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7818}
7819
4a64f543
MS
7820/* Implement the "remove" breakpoint_ops method for fork
7821 catchpoints. */
ce78b96d
JB
7822
7823static int
77b06cd7 7824remove_catch_fork (struct bp_location *bl)
ce78b96d 7825{
dfd4cc63 7826 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7827}
7828
7829/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7830 catchpoints. */
7831
7832static int
f1310107 7833breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7834 struct address_space *aspace, CORE_ADDR bp_addr,
7835 const struct target_waitstatus *ws)
ce78b96d 7836{
e29a4733
PA
7837 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7838
f90263c1
TT
7839 if (ws->kind != TARGET_WAITKIND_FORKED)
7840 return 0;
7841
7842 c->forked_inferior_pid = ws->value.related_pid;
7843 return 1;
ce78b96d
JB
7844}
7845
4a64f543
MS
7846/* Implement the "print_it" breakpoint_ops method for fork
7847 catchpoints. */
ce78b96d
JB
7848
7849static enum print_stop_action
348d480f 7850print_it_catch_fork (bpstat bs)
ce78b96d 7851{
36dfb11c 7852 struct ui_out *uiout = current_uiout;
348d480f
PA
7853 struct breakpoint *b = bs->breakpoint_at;
7854 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7855
ce78b96d 7856 annotate_catchpoint (b->number);
36dfb11c
TT
7857 if (b->disposition == disp_del)
7858 ui_out_text (uiout, "\nTemporary catchpoint ");
7859 else
7860 ui_out_text (uiout, "\nCatchpoint ");
7861 if (ui_out_is_mi_like_p (uiout))
7862 {
7863 ui_out_field_string (uiout, "reason",
7864 async_reason_lookup (EXEC_ASYNC_FORK));
7865 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7866 }
7867 ui_out_field_int (uiout, "bkptno", b->number);
7868 ui_out_text (uiout, " (forked process ");
7869 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7870 ui_out_text (uiout, "), ");
ce78b96d
JB
7871 return PRINT_SRC_AND_LOC;
7872}
7873
4a64f543
MS
7874/* Implement the "print_one" breakpoint_ops method for fork
7875 catchpoints. */
ce78b96d
JB
7876
7877static void
a6d9a66e 7878print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7879{
e29a4733 7880 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7881 struct value_print_options opts;
79a45e25 7882 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7883
7884 get_user_print_options (&opts);
7885
4a64f543
MS
7886 /* Field 4, the address, is omitted (which makes the columns not
7887 line up too nicely with the headers, but the effect is relatively
7888 readable). */
79a45b7d 7889 if (opts.addressprint)
ce78b96d
JB
7890 ui_out_field_skip (uiout, "addr");
7891 annotate_field (5);
7892 ui_out_text (uiout, "fork");
e29a4733 7893 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7894 {
7895 ui_out_text (uiout, ", process ");
7896 ui_out_field_int (uiout, "what",
e29a4733 7897 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7898 ui_out_spaces (uiout, 1);
7899 }
8ac3646f
TT
7900
7901 if (ui_out_is_mi_like_p (uiout))
7902 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
7903}
7904
7905/* Implement the "print_mention" breakpoint_ops method for fork
7906 catchpoints. */
7907
7908static void
7909print_mention_catch_fork (struct breakpoint *b)
7910{
7911 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7912}
7913
6149aea9
PA
7914/* Implement the "print_recreate" breakpoint_ops method for fork
7915 catchpoints. */
7916
7917static void
7918print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7919{
7920 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7921 print_recreate_thread (b, fp);
6149aea9
PA
7922}
7923
ce78b96d
JB
7924/* The breakpoint_ops structure to be used in fork catchpoints. */
7925
2060206e 7926static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7927
4a64f543
MS
7928/* Implement the "insert" breakpoint_ops method for vfork
7929 catchpoints. */
ce78b96d 7930
77b06cd7
TJB
7931static int
7932insert_catch_vfork (struct bp_location *bl)
ce78b96d 7933{
dfd4cc63 7934 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7935}
7936
4a64f543
MS
7937/* Implement the "remove" breakpoint_ops method for vfork
7938 catchpoints. */
ce78b96d
JB
7939
7940static int
77b06cd7 7941remove_catch_vfork (struct bp_location *bl)
ce78b96d 7942{
dfd4cc63 7943 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7944}
7945
7946/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7947 catchpoints. */
7948
7949static int
f1310107 7950breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
7951 struct address_space *aspace, CORE_ADDR bp_addr,
7952 const struct target_waitstatus *ws)
ce78b96d 7953{
e29a4733
PA
7954 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7955
f90263c1
TT
7956 if (ws->kind != TARGET_WAITKIND_VFORKED)
7957 return 0;
7958
7959 c->forked_inferior_pid = ws->value.related_pid;
7960 return 1;
ce78b96d
JB
7961}
7962
4a64f543
MS
7963/* Implement the "print_it" breakpoint_ops method for vfork
7964 catchpoints. */
ce78b96d
JB
7965
7966static enum print_stop_action
348d480f 7967print_it_catch_vfork (bpstat bs)
ce78b96d 7968{
36dfb11c 7969 struct ui_out *uiout = current_uiout;
348d480f 7970 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7971 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7972
ce78b96d 7973 annotate_catchpoint (b->number);
36dfb11c
TT
7974 if (b->disposition == disp_del)
7975 ui_out_text (uiout, "\nTemporary catchpoint ");
7976 else
7977 ui_out_text (uiout, "\nCatchpoint ");
7978 if (ui_out_is_mi_like_p (uiout))
7979 {
7980 ui_out_field_string (uiout, "reason",
7981 async_reason_lookup (EXEC_ASYNC_VFORK));
7982 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7983 }
7984 ui_out_field_int (uiout, "bkptno", b->number);
7985 ui_out_text (uiout, " (vforked process ");
7986 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7987 ui_out_text (uiout, "), ");
ce78b96d
JB
7988 return PRINT_SRC_AND_LOC;
7989}
7990
4a64f543
MS
7991/* Implement the "print_one" breakpoint_ops method for vfork
7992 catchpoints. */
ce78b96d
JB
7993
7994static void
a6d9a66e 7995print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7996{
e29a4733 7997 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7998 struct value_print_options opts;
79a45e25 7999 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8000
8001 get_user_print_options (&opts);
4a64f543
MS
8002 /* Field 4, the address, is omitted (which makes the columns not
8003 line up too nicely with the headers, but the effect is relatively
8004 readable). */
79a45b7d 8005 if (opts.addressprint)
ce78b96d
JB
8006 ui_out_field_skip (uiout, "addr");
8007 annotate_field (5);
8008 ui_out_text (uiout, "vfork");
e29a4733 8009 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8010 {
8011 ui_out_text (uiout, ", process ");
8012 ui_out_field_int (uiout, "what",
e29a4733 8013 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8014 ui_out_spaces (uiout, 1);
8015 }
8ac3646f
TT
8016
8017 if (ui_out_is_mi_like_p (uiout))
8018 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8019}
8020
8021/* Implement the "print_mention" breakpoint_ops method for vfork
8022 catchpoints. */
8023
8024static void
8025print_mention_catch_vfork (struct breakpoint *b)
8026{
8027 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8028}
8029
6149aea9
PA
8030/* Implement the "print_recreate" breakpoint_ops method for vfork
8031 catchpoints. */
8032
8033static void
8034print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8035{
8036 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8037 print_recreate_thread (b, fp);
6149aea9
PA
8038}
8039
ce78b96d
JB
8040/* The breakpoint_ops structure to be used in vfork catchpoints. */
8041
2060206e 8042static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8043
edcc5120
TT
8044/* An instance of this type is used to represent an solib catchpoint.
8045 It includes a "struct breakpoint" as a kind of base class; users
8046 downcast to "struct breakpoint *" when needed. A breakpoint is
8047 really of this type iff its ops pointer points to
8048 CATCH_SOLIB_BREAKPOINT_OPS. */
8049
8050struct solib_catchpoint
8051{
8052 /* The base class. */
8053 struct breakpoint base;
8054
8055 /* True for "catch load", false for "catch unload". */
8056 unsigned char is_load;
8057
8058 /* Regular expression to match, if any. COMPILED is only valid when
8059 REGEX is non-NULL. */
8060 char *regex;
8061 regex_t compiled;
8062};
8063
8064static void
8065dtor_catch_solib (struct breakpoint *b)
8066{
8067 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8068
8069 if (self->regex)
8070 regfree (&self->compiled);
8071 xfree (self->regex);
8072
8073 base_breakpoint_ops.dtor (b);
8074}
8075
8076static int
8077insert_catch_solib (struct bp_location *ignore)
8078{
8079 return 0;
8080}
8081
8082static int
8083remove_catch_solib (struct bp_location *ignore)
8084{
8085 return 0;
8086}
8087
8088static int
8089breakpoint_hit_catch_solib (const struct bp_location *bl,
8090 struct address_space *aspace,
8091 CORE_ADDR bp_addr,
8092 const struct target_waitstatus *ws)
8093{
8094 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8095 struct breakpoint *other;
8096
8097 if (ws->kind == TARGET_WAITKIND_LOADED)
8098 return 1;
8099
8100 ALL_BREAKPOINTS (other)
8101 {
8102 struct bp_location *other_bl;
8103
8104 if (other == bl->owner)
8105 continue;
8106
8107 if (other->type != bp_shlib_event)
8108 continue;
8109
8110 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8111 continue;
8112
8113 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8114 {
8115 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8116 return 1;
8117 }
8118 }
8119
8120 return 0;
8121}
8122
8123static void
8124check_status_catch_solib (struct bpstats *bs)
8125{
8126 struct solib_catchpoint *self
8127 = (struct solib_catchpoint *) bs->breakpoint_at;
8128 int ix;
8129
8130 if (self->is_load)
8131 {
8132 struct so_list *iter;
8133
8134 for (ix = 0;
8135 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8136 ix, iter);
8137 ++ix)
8138 {
8139 if (!self->regex
8140 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8141 return;
8142 }
8143 }
8144 else
8145 {
8146 char *iter;
8147
8148 for (ix = 0;
8149 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8150 ix, iter);
8151 ++ix)
8152 {
8153 if (!self->regex
8154 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8155 return;
8156 }
8157 }
8158
8159 bs->stop = 0;
8160 bs->print_it = print_it_noop;
8161}
8162
8163static enum print_stop_action
8164print_it_catch_solib (bpstat bs)
8165{
8166 struct breakpoint *b = bs->breakpoint_at;
8167 struct ui_out *uiout = current_uiout;
8168
8169 annotate_catchpoint (b->number);
8170 if (b->disposition == disp_del)
8171 ui_out_text (uiout, "\nTemporary catchpoint ");
8172 else
8173 ui_out_text (uiout, "\nCatchpoint ");
8174 ui_out_field_int (uiout, "bkptno", b->number);
8175 ui_out_text (uiout, "\n");
8176 if (ui_out_is_mi_like_p (uiout))
8177 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8178 print_solib_event (1);
8179 return PRINT_SRC_AND_LOC;
8180}
8181
8182static void
8183print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8184{
8185 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8186 struct value_print_options opts;
8187 struct ui_out *uiout = current_uiout;
8188 char *msg;
8189
8190 get_user_print_options (&opts);
8191 /* Field 4, the address, is omitted (which makes the columns not
8192 line up too nicely with the headers, but the effect is relatively
8193 readable). */
8194 if (opts.addressprint)
8195 {
8196 annotate_field (4);
8197 ui_out_field_skip (uiout, "addr");
8198 }
8199
8200 annotate_field (5);
8201 if (self->is_load)
8202 {
8203 if (self->regex)
8204 msg = xstrprintf (_("load of library matching %s"), self->regex);
8205 else
8206 msg = xstrdup (_("load of library"));
8207 }
8208 else
8209 {
8210 if (self->regex)
8211 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8212 else
8213 msg = xstrdup (_("unload of library"));
8214 }
8215 ui_out_field_string (uiout, "what", msg);
8216 xfree (msg);
8ac3646f
TT
8217
8218 if (ui_out_is_mi_like_p (uiout))
8219 ui_out_field_string (uiout, "catch-type",
8220 self->is_load ? "load" : "unload");
edcc5120
TT
8221}
8222
8223static void
8224print_mention_catch_solib (struct breakpoint *b)
8225{
8226 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8227
8228 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8229 self->is_load ? "load" : "unload");
8230}
8231
8232static void
8233print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8234{
8235 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8236
8237 fprintf_unfiltered (fp, "%s %s",
8238 b->disposition == disp_del ? "tcatch" : "catch",
8239 self->is_load ? "load" : "unload");
8240 if (self->regex)
8241 fprintf_unfiltered (fp, " %s", self->regex);
8242 fprintf_unfiltered (fp, "\n");
8243}
8244
8245static struct breakpoint_ops catch_solib_breakpoint_ops;
8246
91985142
MG
8247/* Shared helper function (MI and CLI) for creating and installing
8248 a shared object event catchpoint. If IS_LOAD is non-zero then
8249 the events to be caught are load events, otherwise they are
8250 unload events. If IS_TEMP is non-zero the catchpoint is a
8251 temporary one. If ENABLED is non-zero the catchpoint is
8252 created in an enabled state. */
edcc5120 8253
91985142
MG
8254void
8255add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8256{
8257 struct solib_catchpoint *c;
8258 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8259 struct cleanup *cleanup;
8260
edcc5120
TT
8261 if (!arg)
8262 arg = "";
8263 arg = skip_spaces (arg);
8264
8265 c = XCNEW (struct solib_catchpoint);
8266 cleanup = make_cleanup (xfree, c);
8267
8268 if (*arg != '\0')
8269 {
8270 int errcode;
8271
8272 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8273 if (errcode != 0)
8274 {
8275 char *err = get_regcomp_error (errcode, &c->compiled);
8276
8277 make_cleanup (xfree, err);
8278 error (_("Invalid regexp (%s): %s"), err, arg);
8279 }
8280 c->regex = xstrdup (arg);
8281 }
8282
8283 c->is_load = is_load;
91985142 8284 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8285 &catch_solib_breakpoint_ops);
8286
91985142
MG
8287 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8288
edcc5120
TT
8289 discard_cleanups (cleanup);
8290 install_breakpoint (0, &c->base, 1);
8291}
8292
91985142
MG
8293/* A helper function that does all the work for "catch load" and
8294 "catch unload". */
8295
8296static void
8297catch_load_or_unload (char *arg, int from_tty, int is_load,
8298 struct cmd_list_element *command)
8299{
8300 int tempflag;
8301 const int enabled = 1;
8302
8303 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8304
8305 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8306}
8307
edcc5120
TT
8308static void
8309catch_load_command_1 (char *arg, int from_tty,
8310 struct cmd_list_element *command)
8311{
8312 catch_load_or_unload (arg, from_tty, 1, command);
8313}
8314
8315static void
8316catch_unload_command_1 (char *arg, int from_tty,
8317 struct cmd_list_element *command)
8318{
8319 catch_load_or_unload (arg, from_tty, 0, command);
8320}
8321
be5c67c1
PA
8322/* An instance of this type is used to represent a syscall catchpoint.
8323 It includes a "struct breakpoint" as a kind of base class; users
8324 downcast to "struct breakpoint *" when needed. A breakpoint is
8325 really of this type iff its ops pointer points to
8326 CATCH_SYSCALL_BREAKPOINT_OPS. */
8327
8328struct syscall_catchpoint
8329{
8330 /* The base class. */
8331 struct breakpoint base;
8332
8333 /* Syscall numbers used for the 'catch syscall' feature. If no
8334 syscall has been specified for filtering, its value is NULL.
8335 Otherwise, it holds a list of all syscalls to be caught. The
8336 list elements are allocated with xmalloc. */
8337 VEC(int) *syscalls_to_be_caught;
8338};
8339
8340/* Implement the "dtor" breakpoint_ops method for syscall
8341 catchpoints. */
8342
8343static void
8344dtor_catch_syscall (struct breakpoint *b)
8345{
8346 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8347
8348 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8349
2060206e 8350 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8351}
8352
fa3064dd
YQ
8353static const struct inferior_data *catch_syscall_inferior_data = NULL;
8354
8355struct catch_syscall_inferior_data
8356{
8357 /* We keep a count of the number of times the user has requested a
8358 particular syscall to be tracked, and pass this information to the
8359 target. This lets capable targets implement filtering directly. */
8360
8361 /* Number of times that "any" syscall is requested. */
8362 int any_syscall_count;
8363
8364 /* Count of each system call. */
8365 VEC(int) *syscalls_counts;
8366
8367 /* This counts all syscall catch requests, so we can readily determine
8368 if any catching is necessary. */
8369 int total_syscalls_count;
8370};
8371
8372static struct catch_syscall_inferior_data*
8373get_catch_syscall_inferior_data (struct inferior *inf)
8374{
8375 struct catch_syscall_inferior_data *inf_data;
8376
8377 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8378 if (inf_data == NULL)
8379 {
41bf6aca 8380 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8381 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8382 }
8383
8384 return inf_data;
8385}
8386
8387static void
8388catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8389{
8390 xfree (arg);
8391}
8392
8393
a96d9b2e
SDJ
8394/* Implement the "insert" breakpoint_ops method for syscall
8395 catchpoints. */
8396
77b06cd7
TJB
8397static int
8398insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8399{
be5c67c1 8400 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8401 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8402 struct catch_syscall_inferior_data *inf_data
8403 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8404
fa3064dd 8405 ++inf_data->total_syscalls_count;
be5c67c1 8406 if (!c->syscalls_to_be_caught)
fa3064dd 8407 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8408 else
8409 {
8410 int i, iter;
cc59ec59 8411
a96d9b2e 8412 for (i = 0;
be5c67c1 8413 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8414 i++)
8415 {
8416 int elem;
cc59ec59 8417
fa3064dd 8418 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8419 {
fa3064dd 8420 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8421 uintptr_t vec_addr_offset
8422 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8423 uintptr_t vec_addr;
fa3064dd
YQ
8424 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8425 vec_addr = ((uintptr_t) VEC_address (int,
8426 inf_data->syscalls_counts)
8427 + vec_addr_offset);
a96d9b2e
SDJ
8428 memset ((void *) vec_addr, 0,
8429 (iter + 1 - old_size) * sizeof (int));
8430 }
fa3064dd
YQ
8431 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8432 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8433 }
8434 }
8435
dfd4cc63 8436 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8437 inf_data->total_syscalls_count != 0,
8438 inf_data->any_syscall_count,
8439 VEC_length (int,
8440 inf_data->syscalls_counts),
8441 VEC_address (int,
8442 inf_data->syscalls_counts));
a96d9b2e
SDJ
8443}
8444
8445/* Implement the "remove" breakpoint_ops method for syscall
8446 catchpoints. */
8447
8448static int
77b06cd7 8449remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8450{
be5c67c1 8451 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8452 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8453 struct catch_syscall_inferior_data *inf_data
8454 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8455
fa3064dd 8456 --inf_data->total_syscalls_count;
be5c67c1 8457 if (!c->syscalls_to_be_caught)
fa3064dd 8458 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8459 else
8460 {
8461 int i, iter;
cc59ec59 8462
a96d9b2e 8463 for (i = 0;
be5c67c1 8464 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8465 i++)
8466 {
8467 int elem;
fa3064dd 8468 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8469 /* Shouldn't happen. */
8470 continue;
fa3064dd
YQ
8471 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8472 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8473 }
8474 }
8475
dfd4cc63 8476 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8477 inf_data->total_syscalls_count != 0,
8478 inf_data->any_syscall_count,
8479 VEC_length (int,
8480 inf_data->syscalls_counts),
3e43a32a 8481 VEC_address (int,
fa3064dd 8482 inf_data->syscalls_counts));
a96d9b2e
SDJ
8483}
8484
8485/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8486 catchpoints. */
8487
8488static int
f1310107 8489breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8490 struct address_space *aspace, CORE_ADDR bp_addr,
8491 const struct target_waitstatus *ws)
a96d9b2e 8492{
4a64f543
MS
8493 /* We must check if we are catching specific syscalls in this
8494 breakpoint. If we are, then we must guarantee that the called
8495 syscall is the same syscall we are catching. */
a96d9b2e 8496 int syscall_number = 0;
be5c67c1
PA
8497 const struct syscall_catchpoint *c
8498 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8499
f90263c1
TT
8500 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8501 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8502 return 0;
8503
f90263c1
TT
8504 syscall_number = ws->value.syscall_number;
8505
a96d9b2e 8506 /* Now, checking if the syscall is the same. */
be5c67c1 8507 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8508 {
8509 int i, iter;
cc59ec59 8510
a96d9b2e 8511 for (i = 0;
be5c67c1 8512 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8513 i++)
8514 if (syscall_number == iter)
4924df79
GKB
8515 return 1;
8516
8517 return 0;
a96d9b2e
SDJ
8518 }
8519
8520 return 1;
8521}
8522
8523/* Implement the "print_it" breakpoint_ops method for syscall
8524 catchpoints. */
8525
8526static enum print_stop_action
348d480f 8527print_it_catch_syscall (bpstat bs)
a96d9b2e 8528{
36dfb11c 8529 struct ui_out *uiout = current_uiout;
348d480f 8530 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8531 /* These are needed because we want to know in which state a
8532 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8533 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8534 must print "called syscall" or "returned from syscall". */
8535 ptid_t ptid;
8536 struct target_waitstatus last;
8537 struct syscall s;
a96d9b2e
SDJ
8538
8539 get_last_target_status (&ptid, &last);
8540
8541 get_syscall_by_number (last.value.syscall_number, &s);
8542
8543 annotate_catchpoint (b->number);
8544
36dfb11c
TT
8545 if (b->disposition == disp_del)
8546 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8547 else
36dfb11c
TT
8548 ui_out_text (uiout, "\nCatchpoint ");
8549 if (ui_out_is_mi_like_p (uiout))
8550 {
8551 ui_out_field_string (uiout, "reason",
8552 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8553 ? EXEC_ASYNC_SYSCALL_ENTRY
8554 : EXEC_ASYNC_SYSCALL_RETURN));
8555 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8556 }
8557 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8558
8559 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8560 ui_out_text (uiout, " (call to syscall ");
8561 else
8562 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8563
36dfb11c
TT
8564 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8565 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8566 if (s.name != NULL)
8567 ui_out_field_string (uiout, "syscall-name", s.name);
8568
8569 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8570
8571 return PRINT_SRC_AND_LOC;
8572}
8573
8574/* Implement the "print_one" breakpoint_ops method for syscall
8575 catchpoints. */
8576
8577static void
8578print_one_catch_syscall (struct breakpoint *b,
f1310107 8579 struct bp_location **last_loc)
a96d9b2e 8580{
be5c67c1 8581 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8582 struct value_print_options opts;
79a45e25 8583 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
8584
8585 get_user_print_options (&opts);
4a64f543
MS
8586 /* Field 4, the address, is omitted (which makes the columns not
8587 line up too nicely with the headers, but the effect is relatively
8588 readable). */
a96d9b2e
SDJ
8589 if (opts.addressprint)
8590 ui_out_field_skip (uiout, "addr");
8591 annotate_field (5);
8592
be5c67c1
PA
8593 if (c->syscalls_to_be_caught
8594 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8595 ui_out_text (uiout, "syscalls \"");
8596 else
8597 ui_out_text (uiout, "syscall \"");
8598
be5c67c1 8599 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8600 {
8601 int i, iter;
8602 char *text = xstrprintf ("%s", "");
cc59ec59 8603
a96d9b2e 8604 for (i = 0;
be5c67c1 8605 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8606 i++)
8607 {
8608 char *x = text;
8609 struct syscall s;
8610 get_syscall_by_number (iter, &s);
8611
8612 if (s.name != NULL)
8613 text = xstrprintf ("%s%s, ", text, s.name);
8614 else
8615 text = xstrprintf ("%s%d, ", text, iter);
8616
8617 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8618 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8619 on every call. */
8620 xfree (x);
8621 }
8622 /* Remove the last comma. */
8623 text[strlen (text) - 2] = '\0';
8624 ui_out_field_string (uiout, "what", text);
8625 }
8626 else
8627 ui_out_field_string (uiout, "what", "<any syscall>");
8628 ui_out_text (uiout, "\" ");
8ac3646f
TT
8629
8630 if (ui_out_is_mi_like_p (uiout))
8631 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8632}
8633
8634/* Implement the "print_mention" breakpoint_ops method for syscall
8635 catchpoints. */
8636
8637static void
8638print_mention_catch_syscall (struct breakpoint *b)
8639{
be5c67c1
PA
8640 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8641
8642 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8643 {
8644 int i, iter;
8645
be5c67c1 8646 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8647 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8648 else
8649 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8650
8651 for (i = 0;
be5c67c1 8652 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8653 i++)
8654 {
8655 struct syscall s;
8656 get_syscall_by_number (iter, &s);
8657
8658 if (s.name)
8659 printf_filtered (" '%s' [%d]", s.name, s.number);
8660 else
8661 printf_filtered (" %d", s.number);
8662 }
8663 printf_filtered (")");
8664 }
8665 else
8666 printf_filtered (_("Catchpoint %d (any syscall)"),
8667 b->number);
8668}
8669
6149aea9
PA
8670/* Implement the "print_recreate" breakpoint_ops method for syscall
8671 catchpoints. */
8672
8673static void
8674print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8675{
be5c67c1
PA
8676 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8677
6149aea9
PA
8678 fprintf_unfiltered (fp, "catch syscall");
8679
be5c67c1 8680 if (c->syscalls_to_be_caught)
6149aea9
PA
8681 {
8682 int i, iter;
8683
8684 for (i = 0;
be5c67c1 8685 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8686 i++)
8687 {
8688 struct syscall s;
8689
8690 get_syscall_by_number (iter, &s);
8691 if (s.name)
8692 fprintf_unfiltered (fp, " %s", s.name);
8693 else
8694 fprintf_unfiltered (fp, " %d", s.number);
8695 }
8696 }
d9b3f62e 8697 print_recreate_thread (b, fp);
6149aea9
PA
8698}
8699
a96d9b2e
SDJ
8700/* The breakpoint_ops structure to be used in syscall catchpoints. */
8701
2060206e 8702static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8703
8704/* Returns non-zero if 'b' is a syscall catchpoint. */
8705
8706static int
8707syscall_catchpoint_p (struct breakpoint *b)
8708{
8709 return (b->ops == &catch_syscall_breakpoint_ops);
8710}
8711
346774a9
PA
8712/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8713 is non-zero, then make the breakpoint temporary. If COND_STRING is
8714 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8715 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8716
ab04a2af 8717void
346774a9
PA
8718init_catchpoint (struct breakpoint *b,
8719 struct gdbarch *gdbarch, int tempflag,
8720 char *cond_string,
c0a91b2b 8721 const struct breakpoint_ops *ops)
c906108c 8722{
c5aa993b 8723 struct symtab_and_line sal;
346774a9 8724
fe39c653 8725 init_sal (&sal);
6c95b8df 8726 sal.pspace = current_program_space;
c5aa993b 8727
28010a5d 8728 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8729
1b36a34b 8730 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8731 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8732}
8733
28010a5d 8734void
3ea46bff 8735install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8736{
8737 add_to_breakpoint_chain (b);
3a5c3e22 8738 set_breakpoint_number (internal, b);
558a9d82
YQ
8739 if (is_tracepoint (b))
8740 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8741 if (!internal)
8742 mention (b);
c56053d2 8743 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8744
8745 if (update_gll)
8746 update_global_location_list (1);
c56053d2
PA
8747}
8748
9b70b993 8749static void
a6d9a66e
UW
8750create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8751 int tempflag, char *cond_string,
c0a91b2b 8752 const struct breakpoint_ops *ops)
c906108c 8753{
e29a4733 8754 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8755
e29a4733
PA
8756 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8757
8758 c->forked_inferior_pid = null_ptid;
8759
3ea46bff 8760 install_breakpoint (0, &c->base, 1);
c906108c
SS
8761}
8762
fe798b75
JB
8763/* Exec catchpoints. */
8764
b4d90040
PA
8765/* An instance of this type is used to represent an exec catchpoint.
8766 It includes a "struct breakpoint" as a kind of base class; users
8767 downcast to "struct breakpoint *" when needed. A breakpoint is
8768 really of this type iff its ops pointer points to
8769 CATCH_EXEC_BREAKPOINT_OPS. */
8770
8771struct exec_catchpoint
8772{
8773 /* The base class. */
8774 struct breakpoint base;
8775
8776 /* Filename of a program whose exec triggered this catchpoint.
8777 This field is only valid immediately after this catchpoint has
8778 triggered. */
8779 char *exec_pathname;
8780};
8781
8782/* Implement the "dtor" breakpoint_ops method for exec
8783 catchpoints. */
8784
8785static void
8786dtor_catch_exec (struct breakpoint *b)
8787{
8788 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8789
8790 xfree (c->exec_pathname);
348d480f 8791
2060206e 8792 base_breakpoint_ops.dtor (b);
b4d90040
PA
8793}
8794
77b06cd7
TJB
8795static int
8796insert_catch_exec (struct bp_location *bl)
c906108c 8797{
dfd4cc63 8798 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8799}
c906108c 8800
fe798b75 8801static int
77b06cd7 8802remove_catch_exec (struct bp_location *bl)
fe798b75 8803{
dfd4cc63 8804 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8805}
c906108c 8806
fe798b75 8807static int
f1310107 8808breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8809 struct address_space *aspace, CORE_ADDR bp_addr,
8810 const struct target_waitstatus *ws)
fe798b75 8811{
b4d90040
PA
8812 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8813
f90263c1
TT
8814 if (ws->kind != TARGET_WAITKIND_EXECD)
8815 return 0;
8816
8817 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8818 return 1;
fe798b75 8819}
c906108c 8820
fe798b75 8821static enum print_stop_action
348d480f 8822print_it_catch_exec (bpstat bs)
fe798b75 8823{
36dfb11c 8824 struct ui_out *uiout = current_uiout;
348d480f 8825 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8826 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8827
fe798b75 8828 annotate_catchpoint (b->number);
36dfb11c
TT
8829 if (b->disposition == disp_del)
8830 ui_out_text (uiout, "\nTemporary catchpoint ");
8831 else
8832 ui_out_text (uiout, "\nCatchpoint ");
8833 if (ui_out_is_mi_like_p (uiout))
8834 {
8835 ui_out_field_string (uiout, "reason",
8836 async_reason_lookup (EXEC_ASYNC_EXEC));
8837 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8838 }
8839 ui_out_field_int (uiout, "bkptno", b->number);
8840 ui_out_text (uiout, " (exec'd ");
8841 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8842 ui_out_text (uiout, "), ");
8843
fe798b75 8844 return PRINT_SRC_AND_LOC;
c906108c
SS
8845}
8846
fe798b75 8847static void
a6d9a66e 8848print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8849{
b4d90040 8850 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8851 struct value_print_options opts;
79a45e25 8852 struct ui_out *uiout = current_uiout;
fe798b75
JB
8853
8854 get_user_print_options (&opts);
8855
8856 /* Field 4, the address, is omitted (which makes the columns
8857 not line up too nicely with the headers, but the effect
8858 is relatively readable). */
8859 if (opts.addressprint)
8860 ui_out_field_skip (uiout, "addr");
8861 annotate_field (5);
8862 ui_out_text (uiout, "exec");
b4d90040 8863 if (c->exec_pathname != NULL)
fe798b75
JB
8864 {
8865 ui_out_text (uiout, ", program \"");
b4d90040 8866 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8867 ui_out_text (uiout, "\" ");
8868 }
8ac3646f
TT
8869
8870 if (ui_out_is_mi_like_p (uiout))
8871 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8872}
8873
8874static void
8875print_mention_catch_exec (struct breakpoint *b)
8876{
8877 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8878}
8879
6149aea9
PA
8880/* Implement the "print_recreate" breakpoint_ops method for exec
8881 catchpoints. */
8882
8883static void
8884print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8885{
8886 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8887 print_recreate_thread (b, fp);
6149aea9
PA
8888}
8889
2060206e 8890static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8891
a96d9b2e
SDJ
8892static void
8893create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 8894 const struct breakpoint_ops *ops)
a96d9b2e 8895{
be5c67c1 8896 struct syscall_catchpoint *c;
a96d9b2e 8897 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 8898
be5c67c1
PA
8899 c = XNEW (struct syscall_catchpoint);
8900 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8901 c->syscalls_to_be_caught = filter;
a96d9b2e 8902
3ea46bff 8903 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
8904}
8905
c906108c 8906static int
fba45db2 8907hw_breakpoint_used_count (void)
c906108c 8908{
c906108c 8909 int i = 0;
f1310107
TJB
8910 struct breakpoint *b;
8911 struct bp_location *bl;
c906108c
SS
8912
8913 ALL_BREAKPOINTS (b)
c5aa993b 8914 {
d6b74ac4 8915 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8916 for (bl = b->loc; bl; bl = bl->next)
8917 {
8918 /* Special types of hardware breakpoints may use more than
8919 one register. */
348d480f 8920 i += b->ops->resources_needed (bl);
f1310107 8921 }
c5aa993b 8922 }
c906108c
SS
8923
8924 return i;
8925}
8926
a1398e0c
PA
8927/* Returns the resources B would use if it were a hardware
8928 watchpoint. */
8929
c906108c 8930static int
a1398e0c 8931hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8932{
c906108c 8933 int i = 0;
e09342b5 8934 struct bp_location *bl;
c906108c 8935
a1398e0c
PA
8936 if (!breakpoint_enabled (b))
8937 return 0;
8938
8939 for (bl = b->loc; bl; bl = bl->next)
8940 {
8941 /* Special types of hardware watchpoints may use more than
8942 one register. */
8943 i += b->ops->resources_needed (bl);
8944 }
8945
8946 return i;
8947}
8948
8949/* Returns the sum the used resources of all hardware watchpoints of
8950 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8951 the sum of the used resources of all hardware watchpoints of other
8952 types _not_ TYPE. */
8953
8954static int
8955hw_watchpoint_used_count_others (struct breakpoint *except,
8956 enum bptype type, int *other_type_used)
8957{
8958 int i = 0;
8959 struct breakpoint *b;
8960
c906108c
SS
8961 *other_type_used = 0;
8962 ALL_BREAKPOINTS (b)
e09342b5 8963 {
a1398e0c
PA
8964 if (b == except)
8965 continue;
e09342b5
TJB
8966 if (!breakpoint_enabled (b))
8967 continue;
8968
a1398e0c
PA
8969 if (b->type == type)
8970 i += hw_watchpoint_use_count (b);
8971 else if (is_hardware_watchpoint (b))
8972 *other_type_used = 1;
e09342b5
TJB
8973 }
8974
c906108c
SS
8975 return i;
8976}
8977
c906108c 8978void
fba45db2 8979disable_watchpoints_before_interactive_call_start (void)
c906108c 8980{
c5aa993b 8981 struct breakpoint *b;
c906108c
SS
8982
8983 ALL_BREAKPOINTS (b)
c5aa993b 8984 {
cc60f2e3 8985 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8986 {
b5de0fa7 8987 b->enable_state = bp_call_disabled;
b60e7edf 8988 update_global_location_list (0);
c5aa993b
JM
8989 }
8990 }
c906108c
SS
8991}
8992
8993void
fba45db2 8994enable_watchpoints_after_interactive_call_stop (void)
c906108c 8995{
c5aa993b 8996 struct breakpoint *b;
c906108c
SS
8997
8998 ALL_BREAKPOINTS (b)
c5aa993b 8999 {
cc60f2e3 9000 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 9001 {
b5de0fa7 9002 b->enable_state = bp_enabled;
b60e7edf 9003 update_global_location_list (1);
c5aa993b
JM
9004 }
9005 }
c906108c
SS
9006}
9007
8bea4e01
UW
9008void
9009disable_breakpoints_before_startup (void)
9010{
6c95b8df 9011 current_program_space->executing_startup = 1;
f8eba3c6 9012 update_global_location_list (0);
8bea4e01
UW
9013}
9014
9015void
9016enable_breakpoints_after_startup (void)
9017{
6c95b8df 9018 current_program_space->executing_startup = 0;
f8eba3c6 9019 breakpoint_re_set ();
8bea4e01
UW
9020}
9021
c906108c
SS
9022
9023/* Set a breakpoint that will evaporate an end of command
9024 at address specified by SAL.
9025 Restrict it to frame FRAME if FRAME is nonzero. */
9026
9027struct breakpoint *
a6d9a66e
UW
9028set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9029 struct frame_id frame_id, enum bptype type)
c906108c 9030{
52f0bd74 9031 struct breakpoint *b;
edb3359d 9032
193facb3
JK
9033 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9034 tail-called one. */
9035 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 9036
06edf0c0 9037 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
9038 b->enable_state = bp_enabled;
9039 b->disposition = disp_donttouch;
818dd999 9040 b->frame_id = frame_id;
c906108c 9041
4a64f543
MS
9042 /* If we're debugging a multi-threaded program, then we want
9043 momentary breakpoints to be active in only a single thread of
9044 control. */
39f77062
KB
9045 if (in_thread_list (inferior_ptid))
9046 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 9047
b60e7edf 9048 update_global_location_list_nothrow (1);
74960c60 9049
c906108c
SS
9050 return b;
9051}
611c83ae 9052
06edf0c0
PA
9053/* Make a momentary breakpoint based on the master breakpoint ORIG.
9054 The new breakpoint will have type TYPE, and use OPS as it
9055 breakpoint_ops. */
e58b0e63 9056
06edf0c0
PA
9057static struct breakpoint *
9058momentary_breakpoint_from_master (struct breakpoint *orig,
9059 enum bptype type,
c0a91b2b 9060 const struct breakpoint_ops *ops)
e58b0e63
PA
9061{
9062 struct breakpoint *copy;
9063
06edf0c0 9064 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 9065 copy->loc = allocate_bp_location (copy);
0e30163f 9066 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 9067
a6d9a66e 9068 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
9069 copy->loc->requested_address = orig->loc->requested_address;
9070 copy->loc->address = orig->loc->address;
9071 copy->loc->section = orig->loc->section;
6c95b8df 9072 copy->loc->pspace = orig->loc->pspace;
55aa24fb 9073 copy->loc->probe = orig->loc->probe;
f8eba3c6 9074 copy->loc->line_number = orig->loc->line_number;
2f202fde 9075 copy->loc->symtab = orig->loc->symtab;
e58b0e63
PA
9076 copy->frame_id = orig->frame_id;
9077 copy->thread = orig->thread;
6c95b8df 9078 copy->pspace = orig->pspace;
e58b0e63
PA
9079
9080 copy->enable_state = bp_enabled;
9081 copy->disposition = disp_donttouch;
9082 copy->number = internal_breakpoint_number--;
9083
9084 update_global_location_list_nothrow (0);
9085 return copy;
9086}
9087
06edf0c0
PA
9088/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9089 ORIG is NULL. */
9090
9091struct breakpoint *
9092clone_momentary_breakpoint (struct breakpoint *orig)
9093{
9094 /* If there's nothing to clone, then return nothing. */
9095 if (orig == NULL)
9096 return NULL;
9097
9098 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
9099}
9100
611c83ae 9101struct breakpoint *
a6d9a66e
UW
9102set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9103 enum bptype type)
611c83ae
PA
9104{
9105 struct symtab_and_line sal;
9106
9107 sal = find_pc_line (pc, 0);
9108 sal.pc = pc;
9109 sal.section = find_pc_overlay (pc);
9110 sal.explicit_pc = 1;
9111
a6d9a66e 9112 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9113}
c906108c 9114\f
c5aa993b 9115
c906108c
SS
9116/* Tell the user we have just set a breakpoint B. */
9117
9118static void
fba45db2 9119mention (struct breakpoint *b)
c906108c 9120{
348d480f 9121 b->ops->print_mention (b);
79a45e25 9122 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9123 return;
c906108c
SS
9124 printf_filtered ("\n");
9125}
c906108c 9126\f
c5aa993b 9127
0d381245 9128static struct bp_location *
39d61571 9129add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9130 const struct symtab_and_line *sal)
9131{
9132 struct bp_location *loc, **tmp;
3742cc8b
YQ
9133 CORE_ADDR adjusted_address;
9134 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9135
9136 if (loc_gdbarch == NULL)
9137 loc_gdbarch = b->gdbarch;
9138
9139 /* Adjust the breakpoint's address prior to allocating a location.
9140 Once we call allocate_bp_location(), that mostly uninitialized
9141 location will be placed on the location chain. Adjustment of the
9142 breakpoint may cause target_read_memory() to be called and we do
9143 not want its scan of the location chain to find a breakpoint and
9144 location that's only been partially initialized. */
9145 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9146 sal->pc, b->type);
0d381245 9147
d30113d4 9148 /* Sort the locations by their ADDRESS. */
39d61571 9149 loc = allocate_bp_location (b);
d30113d4
JK
9150 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9151 tmp = &((*tmp)->next))
0d381245 9152 ;
d30113d4 9153 loc->next = *tmp;
0d381245 9154 *tmp = loc;
3742cc8b 9155
0d381245 9156 loc->requested_address = sal->pc;
3742cc8b 9157 loc->address = adjusted_address;
6c95b8df 9158 loc->pspace = sal->pspace;
729662a5
TT
9159 loc->probe.probe = sal->probe;
9160 loc->probe.objfile = sal->objfile;
6c95b8df 9161 gdb_assert (loc->pspace != NULL);
0d381245 9162 loc->section = sal->section;
3742cc8b 9163 loc->gdbarch = loc_gdbarch;
f8eba3c6 9164 loc->line_number = sal->line;
2f202fde 9165 loc->symtab = sal->symtab;
f8eba3c6 9166
0e30163f
JK
9167 set_breakpoint_location_function (loc,
9168 sal->explicit_pc || sal->explicit_line);
0d381245
VP
9169 return loc;
9170}
514f746b
AR
9171\f
9172
9173/* Return 1 if LOC is pointing to a permanent breakpoint,
9174 return 0 otherwise. */
9175
9176static int
9177bp_loc_is_permanent (struct bp_location *loc)
9178{
9179 int len;
9180 CORE_ADDR addr;
1afeeb75 9181 const gdb_byte *bpoint;
514f746b 9182 gdb_byte *target_mem;
939c61fa
JK
9183 struct cleanup *cleanup;
9184 int retval = 0;
514f746b
AR
9185
9186 gdb_assert (loc != NULL);
9187
9188 addr = loc->address;
1afeeb75 9189 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 9190
939c61fa 9191 /* Software breakpoints unsupported? */
1afeeb75 9192 if (bpoint == NULL)
939c61fa
JK
9193 return 0;
9194
514f746b
AR
9195 target_mem = alloca (len);
9196
939c61fa
JK
9197 /* Enable the automatic memory restoration from breakpoints while
9198 we read the memory. Otherwise we could say about our temporary
9199 breakpoints they are permanent. */
6c95b8df
PA
9200 cleanup = save_current_space_and_thread ();
9201
9202 switch_to_program_space_and_thread (loc->pspace);
9203 make_show_memory_breakpoints_cleanup (0);
939c61fa 9204
514f746b 9205 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 9206 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 9207 retval = 1;
514f746b 9208
939c61fa
JK
9209 do_cleanups (cleanup);
9210
9211 return retval;
514f746b
AR
9212}
9213
e7e0cddf
SS
9214/* Build a command list for the dprintf corresponding to the current
9215 settings of the dprintf style options. */
9216
9217static void
9218update_dprintf_command_list (struct breakpoint *b)
9219{
9220 char *dprintf_args = b->extra_string;
9221 char *printf_line = NULL;
9222
9223 if (!dprintf_args)
9224 return;
9225
9226 dprintf_args = skip_spaces (dprintf_args);
9227
9228 /* Allow a comma, as it may have terminated a location, but don't
9229 insist on it. */
9230 if (*dprintf_args == ',')
9231 ++dprintf_args;
9232 dprintf_args = skip_spaces (dprintf_args);
9233
9234 if (*dprintf_args != '"')
9235 error (_("Bad format string, missing '\"'."));
9236
d3ce09f5 9237 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9238 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9239 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9240 {
9241 if (!dprintf_function)
9242 error (_("No function supplied for dprintf call"));
9243
9244 if (dprintf_channel && strlen (dprintf_channel) > 0)
9245 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9246 dprintf_function,
9247 dprintf_channel,
9248 dprintf_args);
9249 else
9250 printf_line = xstrprintf ("call (void) %s (%s)",
9251 dprintf_function,
9252 dprintf_args);
9253 }
d3ce09f5
SS
9254 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9255 {
9256 if (target_can_run_breakpoint_commands ())
9257 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9258 else
9259 {
9260 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9261 printf_line = xstrprintf ("printf %s", dprintf_args);
9262 }
9263 }
e7e0cddf
SS
9264 else
9265 internal_error (__FILE__, __LINE__,
9266 _("Invalid dprintf style."));
9267
f28045c2 9268 gdb_assert (printf_line != NULL);
9d6e6e84 9269 /* Manufacture a printf sequence. */
f28045c2 9270 {
9d6e6e84
HZ
9271 struct command_line *printf_cmd_line
9272 = xmalloc (sizeof (struct command_line));
e7e0cddf 9273
f28045c2
YQ
9274 printf_cmd_line = xmalloc (sizeof (struct command_line));
9275 printf_cmd_line->control_type = simple_control;
9276 printf_cmd_line->body_count = 0;
9277 printf_cmd_line->body_list = NULL;
9d6e6e84 9278 printf_cmd_line->next = NULL;
f28045c2 9279 printf_cmd_line->line = printf_line;
e7e0cddf 9280
f28045c2
YQ
9281 breakpoint_set_commands (b, printf_cmd_line);
9282 }
e7e0cddf
SS
9283}
9284
9285/* Update all dprintf commands, making their command lists reflect
9286 current style settings. */
9287
9288static void
9289update_dprintf_commands (char *args, int from_tty,
9290 struct cmd_list_element *c)
9291{
9292 struct breakpoint *b;
9293
9294 ALL_BREAKPOINTS (b)
9295 {
9296 if (b->type == bp_dprintf)
9297 update_dprintf_command_list (b);
9298 }
9299}
c3f6f71d 9300
018d34a4
VP
9301/* Create a breakpoint with SAL as location. Use ADDR_STRING
9302 as textual description of the location, and COND_STRING
db107f19 9303 as condition expression. */
018d34a4
VP
9304
9305static void
d9b3f62e
PA
9306init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9307 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9308 char *filter, char *cond_string,
e7e0cddf 9309 char *extra_string,
d9b3f62e
PA
9310 enum bptype type, enum bpdisp disposition,
9311 int thread, int task, int ignore_count,
c0a91b2b 9312 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9313 int enabled, int internal, unsigned flags,
9314 int display_canonical)
018d34a4 9315{
0d381245 9316 int i;
018d34a4
VP
9317
9318 if (type == bp_hardware_breakpoint)
9319 {
fbbd034e
AS
9320 int target_resources_ok;
9321
9322 i = hw_breakpoint_used_count ();
9323 target_resources_ok =
9324 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9325 i + 1, 0);
9326 if (target_resources_ok == 0)
9327 error (_("No hardware breakpoint support in the target."));
9328 else if (target_resources_ok < 0)
9329 error (_("Hardware breakpoints used exceeds limit."));
9330 }
9331
6c95b8df
PA
9332 gdb_assert (sals.nelts > 0);
9333
0d381245
VP
9334 for (i = 0; i < sals.nelts; ++i)
9335 {
9336 struct symtab_and_line sal = sals.sals[i];
9337 struct bp_location *loc;
9338
9339 if (from_tty)
5af949e3
UW
9340 {
9341 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9342 if (!loc_gdbarch)
9343 loc_gdbarch = gdbarch;
9344
9345 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9346 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9347 }
0d381245
VP
9348
9349 if (i == 0)
9350 {
d9b3f62e 9351 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9352 b->thread = thread;
4a306c9a 9353 b->task = task;
855a6e68 9354
0d381245 9355 b->cond_string = cond_string;
e7e0cddf 9356 b->extra_string = extra_string;
0d381245 9357 b->ignore_count = ignore_count;
41447f92 9358 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9359 b->disposition = disposition;
6c95b8df 9360
44f238bb
PA
9361 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9362 b->loc->inserted = 1;
9363
0fb4aa4b
PA
9364 if (type == bp_static_tracepoint)
9365 {
d9b3f62e 9366 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9367 struct static_tracepoint_marker marker;
9368
983af33b 9369 if (strace_marker_p (b))
0fb4aa4b
PA
9370 {
9371 /* We already know the marker exists, otherwise, we
9372 wouldn't see a sal for it. */
9373 char *p = &addr_string[3];
9374 char *endp;
9375 char *marker_str;
0fb4aa4b 9376
e9cafbcc 9377 p = skip_spaces (p);
0fb4aa4b 9378
e9cafbcc 9379 endp = skip_to_space (p);
0fb4aa4b
PA
9380
9381 marker_str = savestring (p, endp - p);
d9b3f62e 9382 t->static_trace_marker_id = marker_str;
0fb4aa4b 9383
3e43a32a
MS
9384 printf_filtered (_("Probed static tracepoint "
9385 "marker \"%s\"\n"),
d9b3f62e 9386 t->static_trace_marker_id);
0fb4aa4b
PA
9387 }
9388 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9389 {
d9b3f62e 9390 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9391 release_static_tracepoint_marker (&marker);
9392
3e43a32a
MS
9393 printf_filtered (_("Probed static tracepoint "
9394 "marker \"%s\"\n"),
d9b3f62e 9395 t->static_trace_marker_id);
0fb4aa4b
PA
9396 }
9397 else
3e43a32a
MS
9398 warning (_("Couldn't determine the static "
9399 "tracepoint marker to probe"));
0fb4aa4b
PA
9400 }
9401
0d381245
VP
9402 loc = b->loc;
9403 }
9404 else
018d34a4 9405 {
39d61571 9406 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9407 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9408 loc->inserted = 1;
0d381245
VP
9409 }
9410
514f746b
AR
9411 if (bp_loc_is_permanent (loc))
9412 make_breakpoint_permanent (b);
9413
0d381245
VP
9414 if (b->cond_string)
9415 {
bbc13ae3
KS
9416 const char *arg = b->cond_string;
9417
1bb9788d
TT
9418 loc->cond = parse_exp_1 (&arg, loc->address,
9419 block_for_pc (loc->address), 0);
0d381245 9420 if (*arg)
588ae58c 9421 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9422 }
e7e0cddf
SS
9423
9424 /* Dynamic printf requires and uses additional arguments on the
9425 command line, otherwise it's an error. */
9426 if (type == bp_dprintf)
9427 {
9428 if (b->extra_string)
9429 update_dprintf_command_list (b);
9430 else
9431 error (_("Format string required"));
9432 }
9433 else if (b->extra_string)
588ae58c 9434 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9435 }
018d34a4 9436
56435ebe 9437 b->display_canonical = display_canonical;
018d34a4
VP
9438 if (addr_string)
9439 b->addr_string = addr_string;
9440 else
9441 /* addr_string has to be used or breakpoint_re_set will delete
9442 me. */
5af949e3
UW
9443 b->addr_string
9444 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9445 b->filter = filter;
d9b3f62e 9446}
018d34a4 9447
d9b3f62e
PA
9448static void
9449create_breakpoint_sal (struct gdbarch *gdbarch,
9450 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9451 char *filter, char *cond_string,
e7e0cddf 9452 char *extra_string,
d9b3f62e
PA
9453 enum bptype type, enum bpdisp disposition,
9454 int thread, int task, int ignore_count,
c0a91b2b 9455 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9456 int enabled, int internal, unsigned flags,
9457 int display_canonical)
d9b3f62e
PA
9458{
9459 struct breakpoint *b;
9460 struct cleanup *old_chain;
9461
9462 if (is_tracepoint_type (type))
9463 {
9464 struct tracepoint *t;
9465
9466 t = XCNEW (struct tracepoint);
9467 b = &t->base;
9468 }
9469 else
9470 b = XNEW (struct breakpoint);
9471
9472 old_chain = make_cleanup (xfree, b);
9473
9474 init_breakpoint_sal (b, gdbarch,
9475 sals, addr_string,
e7e0cddf 9476 filter, cond_string, extra_string,
d9b3f62e
PA
9477 type, disposition,
9478 thread, task, ignore_count,
9479 ops, from_tty,
44f238bb
PA
9480 enabled, internal, flags,
9481 display_canonical);
d9b3f62e
PA
9482 discard_cleanups (old_chain);
9483
3ea46bff 9484 install_breakpoint (internal, b, 0);
018d34a4
VP
9485}
9486
9487/* Add SALS.nelts breakpoints to the breakpoint table. For each
9488 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9489 value. COND_STRING, if not NULL, specified the condition to be
9490 used for all breakpoints. Essentially the only case where
9491 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9492 function. In that case, it's still not possible to specify
9493 separate conditions for different overloaded functions, so
9494 we take just a single condition string.
9495
c3f6f71d 9496 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9497 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9498 array contents). If the function fails (error() is called), the
9499 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9500 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9501
9502static void
8cdf0e15 9503create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9504 struct linespec_result *canonical,
e7e0cddf 9505 char *cond_string, char *extra_string,
8cdf0e15
VP
9506 enum bptype type, enum bpdisp disposition,
9507 int thread, int task, int ignore_count,
c0a91b2b 9508 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9509 int enabled, int internal, unsigned flags)
c906108c 9510{
018d34a4 9511 int i;
f8eba3c6 9512 struct linespec_sals *lsal;
cc59ec59 9513
f8eba3c6
TT
9514 if (canonical->pre_expanded)
9515 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9516
9517 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9518 {
f8eba3c6
TT
9519 /* Note that 'addr_string' can be NULL in the case of a plain
9520 'break', without arguments. */
9521 char *addr_string = (canonical->addr_string
9522 ? xstrdup (canonical->addr_string)
9523 : NULL);
9524 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9525 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9526
f8eba3c6
TT
9527 make_cleanup (xfree, filter_string);
9528 create_breakpoint_sal (gdbarch, lsal->sals,
9529 addr_string,
9530 filter_string,
e7e0cddf
SS
9531 cond_string, extra_string,
9532 type, disposition,
84f4c1fe 9533 thread, task, ignore_count, ops,
44f238bb 9534 from_tty, enabled, internal, flags,
56435ebe 9535 canonical->special_display);
f8eba3c6 9536 discard_cleanups (inner);
c3f6f71d 9537 }
c3f6f71d 9538}
c906108c 9539
9998af43 9540/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9541 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9542 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9543 address strings. ADDRESS points to the end of the SAL.
9544
9545 The array and the line spec strings are allocated on the heap, it is
9546 the caller's responsibility to free them. */
c906108c 9547
b9362cc7 9548static void
c3f6f71d 9549parse_breakpoint_sals (char **address,
58438ac1 9550 struct linespec_result *canonical)
c3f6f71d 9551{
c3f6f71d 9552 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9553 breakpoint. */
c3f6f71d
JM
9554 if ((*address) == NULL
9555 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9556 {
1bfeeb0f
JL
9557 /* The last displayed codepoint, if it's valid, is our default breakpoint
9558 address. */
9559 if (last_displayed_sal_is_valid ())
c906108c 9560 {
f8eba3c6 9561 struct linespec_sals lsal;
c3f6f71d 9562 struct symtab_and_line sal;
1c8cdcb1 9563 CORE_ADDR pc;
cc59ec59 9564
4a64f543 9565 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9566 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9567 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9568
9569 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9570 corresponding to the last call to print_frame_info.
9571 Be sure to reinitialize LINE with NOTCURRENT == 0
9572 as the breakpoint line number is inappropriate otherwise.
9573 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9574 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9575 pc = sal.pc;
9576 sal = find_pc_line (pc, 0);
00903456 9577
4a64f543 9578 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9579 where PC is the last displayed codepoint's address. So
9580 make sure to set sal.explicit_pc to prevent GDB from
9581 trying to expand the list of sals to include all other
9582 instances with the same symtab and line. */
1c8cdcb1 9583 sal.pc = pc;
00903456
JK
9584 sal.explicit_pc = 1;
9585
f8eba3c6
TT
9586 lsal.sals.sals[0] = sal;
9587 lsal.sals.nelts = 1;
9588 lsal.canonical = NULL;
9589
9590 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9591 }
9592 else
8a3fe4f8 9593 error (_("No default breakpoint address now."));
c906108c
SS
9594 }
9595 else
9596 {
cc80f267
JK
9597 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9598
c906108c 9599 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9600 current_source_symtab (which is decode_line_1's default).
9601 This should produce the results we want almost all of the
cc80f267
JK
9602 time while leaving default_breakpoint_* alone.
9603
9604 ObjC: However, don't match an Objective-C method name which
9605 may have a '+' or '-' succeeded by a '['. */
9606 if (last_displayed_sal_is_valid ()
9607 && (!cursal.symtab
9608 || ((strchr ("+-", (*address)[0]) != NULL)
9609 && ((*address)[1] != '['))))
f8eba3c6
TT
9610 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9611 get_last_displayed_symtab (),
9612 get_last_displayed_line (),
9613 canonical, NULL, NULL);
c906108c 9614 else
f8eba3c6 9615 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9616 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9617 }
c3f6f71d 9618}
c906108c 9619
c906108c 9620
c3f6f71d 9621/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9622 inserted as a breakpoint. If it can't throw an error. */
c906108c 9623
b9362cc7 9624static void
23e7acfb 9625breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9626{
9627 int i;
cc59ec59 9628
c3f6f71d 9629 for (i = 0; i < sals->nelts; i++)
ee53e872 9630 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9631}
9632
7a697b8d
SS
9633/* Fast tracepoints may have restrictions on valid locations. For
9634 instance, a fast tracepoint using a jump instead of a trap will
9635 likely have to overwrite more bytes than a trap would, and so can
9636 only be placed where the instruction is longer than the jump, or a
9637 multi-instruction sequence does not have a jump into the middle of
9638 it, etc. */
9639
9640static void
9641check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9642 struct symtabs_and_lines *sals)
9643{
9644 int i, rslt;
9645 struct symtab_and_line *sal;
9646 char *msg;
9647 struct cleanup *old_chain;
9648
9649 for (i = 0; i < sals->nelts; i++)
9650 {
f8eba3c6
TT
9651 struct gdbarch *sarch;
9652
7a697b8d
SS
9653 sal = &sals->sals[i];
9654
f8eba3c6
TT
9655 sarch = get_sal_arch (*sal);
9656 /* We fall back to GDBARCH if there is no architecture
9657 associated with SAL. */
9658 if (sarch == NULL)
9659 sarch = gdbarch;
9660 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9661 NULL, &msg);
9662 old_chain = make_cleanup (xfree, msg);
9663
9664 if (!rslt)
9665 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9666 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9667
9668 do_cleanups (old_chain);
9669 }
9670}
9671
af4908ba
KS
9672/* Issue an invalid thread ID error. */
9673
9674static void ATTRIBUTE_NORETURN
9675invalid_thread_id_error (int id)
9676{
9677 error (_("Unknown thread %d."), id);
9678}
9679
018d34a4
VP
9680/* Given TOK, a string specification of condition and thread, as
9681 accepted by the 'break' command, extract the condition
9682 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9683 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9684 If no condition is found, *COND_STRING is set to NULL.
9685 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9686
9687static void
bbc13ae3 9688find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9689 char **cond_string, int *thread, int *task,
9690 char **rest)
018d34a4
VP
9691{
9692 *cond_string = NULL;
9693 *thread = -1;
ed1d1739
KS
9694 *task = 0;
9695 *rest = NULL;
9696
018d34a4
VP
9697 while (tok && *tok)
9698 {
bbc13ae3 9699 const char *end_tok;
018d34a4 9700 int toklen;
bbc13ae3
KS
9701 const char *cond_start = NULL;
9702 const char *cond_end = NULL;
cc59ec59 9703
bbc13ae3 9704 tok = skip_spaces_const (tok);
e7e0cddf
SS
9705
9706 if ((*tok == '"' || *tok == ',') && rest)
9707 {
9708 *rest = savestring (tok, strlen (tok));
9709 return;
9710 }
9711
bbc13ae3 9712 end_tok = skip_to_space_const (tok);
d634f2de 9713
018d34a4 9714 toklen = end_tok - tok;
d634f2de 9715
018d34a4
VP
9716 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9717 {
f7545552
TT
9718 struct expression *expr;
9719
018d34a4 9720 tok = cond_start = end_tok + 1;
1bb9788d 9721 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9722 xfree (expr);
018d34a4 9723 cond_end = tok;
d634f2de 9724 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9725 }
9726 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9727 {
9728 char *tmptok;
d634f2de 9729
018d34a4 9730 tok = end_tok + 1;
bbc13ae3 9731 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9732 if (tok == tmptok)
9733 error (_("Junk after thread keyword."));
9734 if (!valid_thread_id (*thread))
af4908ba 9735 invalid_thread_id_error (*thread);
bbc13ae3 9736 tok = tmptok;
018d34a4 9737 }
4a306c9a
JB
9738 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9739 {
9740 char *tmptok;
9741
9742 tok = end_tok + 1;
bbc13ae3 9743 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9744 if (tok == tmptok)
9745 error (_("Junk after task keyword."));
9746 if (!valid_task_id (*task))
b6199126 9747 error (_("Unknown task %d."), *task);
bbc13ae3 9748 tok = tmptok;
4a306c9a 9749 }
e7e0cddf
SS
9750 else if (rest)
9751 {
9752 *rest = savestring (tok, strlen (tok));
ccab2054 9753 return;
e7e0cddf 9754 }
018d34a4
VP
9755 else
9756 error (_("Junk at end of arguments."));
9757 }
9758}
9759
0fb4aa4b
PA
9760/* Decode a static tracepoint marker spec. */
9761
9762static struct symtabs_and_lines
9763decode_static_tracepoint_spec (char **arg_p)
9764{
9765 VEC(static_tracepoint_marker_p) *markers = NULL;
9766 struct symtabs_and_lines sals;
0fb4aa4b
PA
9767 struct cleanup *old_chain;
9768 char *p = &(*arg_p)[3];
9769 char *endp;
9770 char *marker_str;
9771 int i;
9772
e9cafbcc 9773 p = skip_spaces (p);
0fb4aa4b 9774
e9cafbcc 9775 endp = skip_to_space (p);
0fb4aa4b
PA
9776
9777 marker_str = savestring (p, endp - p);
9778 old_chain = make_cleanup (xfree, marker_str);
9779
9780 markers = target_static_tracepoint_markers_by_strid (marker_str);
9781 if (VEC_empty(static_tracepoint_marker_p, markers))
9782 error (_("No known static tracepoint marker named %s"), marker_str);
9783
9784 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9785 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9786
9787 for (i = 0; i < sals.nelts; i++)
9788 {
9789 struct static_tracepoint_marker *marker;
9790
9791 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9792
9793 init_sal (&sals.sals[i]);
9794
9795 sals.sals[i] = find_pc_line (marker->address, 0);
9796 sals.sals[i].pc = marker->address;
9797
9798 release_static_tracepoint_marker (marker);
9799 }
9800
9801 do_cleanups (old_chain);
9802
9803 *arg_p = endp;
9804 return sals;
9805}
9806
fd9b8c24
PA
9807/* Set a breakpoint. This function is shared between CLI and MI
9808 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9809 modes of operations, selected by the PARSE_ARG parameter. If
9810 non-zero, the function will parse ARG, extracting location,
9811 condition, thread and extra string. Otherwise, ARG is just the
9812 breakpoint's location, with condition, thread, and extra string
9813 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9814 If INTERNAL is non-zero, the breakpoint number will be allocated
9815 from the internal breakpoint count. Returns true if any breakpoint
9816 was created; false otherwise. */
0101ce28 9817
8cdf0e15
VP
9818int
9819create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9820 char *arg, char *cond_string,
9821 int thread, char *extra_string,
f6de8ec2 9822 int parse_arg,
0fb4aa4b 9823 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9824 int ignore_count,
9825 enum auto_boolean pending_break_support,
c0a91b2b 9826 const struct breakpoint_ops *ops,
44f238bb
PA
9827 int from_tty, int enabled, int internal,
9828 unsigned flags)
c3f6f71d 9829{
b78a6381 9830 volatile struct gdb_exception e;
f8eba3c6 9831 char *copy_arg = NULL;
c3f6f71d 9832 char *addr_start = arg;
7efd8fc2 9833 struct linespec_result canonical;
c3f6f71d 9834 struct cleanup *old_chain;
80c99de1 9835 struct cleanup *bkpt_chain = NULL;
0101ce28 9836 int pending = 0;
4a306c9a 9837 int task = 0;
86b17b60 9838 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9839
348d480f
PA
9840 gdb_assert (ops != NULL);
9841
7efd8fc2 9842 init_linespec_result (&canonical);
c3f6f71d 9843
b78a6381
TT
9844 TRY_CATCH (e, RETURN_MASK_ALL)
9845 {
983af33b
SDJ
9846 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9847 addr_start, &copy_arg);
b78a6381 9848 }
0101ce28
JJ
9849
9850 /* If caller is interested in rc value from parse, set value. */
05ff989b 9851 switch (e.reason)
0101ce28 9852 {
983af33b
SDJ
9853 case GDB_NO_ERROR:
9854 if (VEC_empty (linespec_sals, canonical.sals))
9855 return 0;
9856 break;
05ff989b
AC
9857 case RETURN_ERROR:
9858 switch (e.error)
0101ce28 9859 {
05ff989b 9860 case NOT_FOUND_ERROR:
0101ce28 9861
05ff989b
AC
9862 /* If pending breakpoint support is turned off, throw
9863 error. */
fa8d40ab
JJ
9864
9865 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9866 throw_exception (e);
9867
9868 exception_print (gdb_stderr, e);
fa8d40ab 9869
05ff989b
AC
9870 /* If pending breakpoint support is auto query and the user
9871 selects no, then simply return the error code. */
059fb39f 9872 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9873 && !nquery (_("Make %s pending on future shared library load? "),
9874 bptype_string (type_wanted)))
fd9b8c24 9875 return 0;
fa8d40ab 9876
05ff989b
AC
9877 /* At this point, either the user was queried about setting
9878 a pending breakpoint and selected yes, or pending
9879 breakpoint behavior is on and thus a pending breakpoint
9880 is defaulted on behalf of the user. */
f8eba3c6
TT
9881 {
9882 struct linespec_sals lsal;
9883
9884 copy_arg = xstrdup (addr_start);
9885 lsal.canonical = xstrdup (copy_arg);
9886 lsal.sals.nelts = 1;
9887 lsal.sals.sals = XNEW (struct symtab_and_line);
9888 init_sal (&lsal.sals.sals[0]);
9889 pending = 1;
9890 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9891 }
05ff989b
AC
9892 break;
9893 default:
98deb0da 9894 throw_exception (e);
0101ce28 9895 }
2abae994 9896 break;
05ff989b 9897 default:
983af33b 9898 throw_exception (e);
0101ce28 9899 }
c3f6f71d 9900
4a64f543 9901 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9902 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9903
c3f6f71d
JM
9904 /* ----------------------------- SNIP -----------------------------
9905 Anything added to the cleanup chain beyond this point is assumed
9906 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9907 then the memory is not reclaimed. */
9908 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9909
c3f6f71d
JM
9910 /* Resolve all line numbers to PC's and verify that the addresses
9911 are ok for the target. */
0101ce28 9912 if (!pending)
f8eba3c6
TT
9913 {
9914 int ix;
9915 struct linespec_sals *iter;
9916
9917 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9918 breakpoint_sals_to_pc (&iter->sals);
9919 }
c3f6f71d 9920
7a697b8d 9921 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9922 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9923 {
9924 int ix;
9925 struct linespec_sals *iter;
9926
9927 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9928 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9929 }
7a697b8d 9930
c3f6f71d
JM
9931 /* Verify that condition can be parsed, before setting any
9932 breakpoints. Allocate a separate condition expression for each
4a64f543 9933 breakpoint. */
0101ce28 9934 if (!pending)
c3f6f71d 9935 {
f6de8ec2 9936 if (parse_arg)
72b2ff0e 9937 {
0878d0fa 9938 char *rest;
52d361e1
YQ
9939 struct linespec_sals *lsal;
9940
9941 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9942
0878d0fa
YQ
9943 /* Here we only parse 'arg' to separate condition
9944 from thread number, so parsing in context of first
9945 sal is OK. When setting the breakpoint we'll
9946 re-parse it in context of each sal. */
9947
9948 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9949 &thread, &task, &rest);
9950 if (cond_string)
9951 make_cleanup (xfree, cond_string);
9952 if (rest)
9953 make_cleanup (xfree, rest);
9954 if (rest)
9955 extra_string = rest;
72b2ff0e 9956 }
2f069f6f 9957 else
72b2ff0e 9958 {
0878d0fa
YQ
9959 if (*arg != '\0')
9960 error (_("Garbage '%s' at end of location"), arg);
9961
9962 /* Create a private copy of condition string. */
9963 if (cond_string)
9964 {
9965 cond_string = xstrdup (cond_string);
9966 make_cleanup (xfree, cond_string);
9967 }
9968 /* Create a private copy of any extra string. */
9969 if (extra_string)
9970 {
9971 extra_string = xstrdup (extra_string);
9972 make_cleanup (xfree, extra_string);
9973 }
72b2ff0e 9974 }
0fb4aa4b 9975
52d361e1 9976 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9977 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9978 tempflag ? disp_del : disp_donttouch,
9979 thread, task, ignore_count, ops,
44f238bb 9980 from_tty, enabled, internal, flags);
c906108c 9981 }
0101ce28
JJ
9982 else
9983 {
0101ce28
JJ
9984 struct breakpoint *b;
9985
0101ce28
JJ
9986 make_cleanup (xfree, copy_arg);
9987
bfccc43c
YQ
9988 if (is_tracepoint_type (type_wanted))
9989 {
9990 struct tracepoint *t;
9991
9992 t = XCNEW (struct tracepoint);
9993 b = &t->base;
9994 }
9995 else
9996 b = XNEW (struct breakpoint);
9997
9998 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9999
f8eba3c6 10000 b->addr_string = copy_arg;
f6de8ec2 10001 if (parse_arg)
e12c7713
MK
10002 b->cond_string = NULL;
10003 else
10004 {
10005 /* Create a private copy of condition string. */
10006 if (cond_string)
10007 {
10008 cond_string = xstrdup (cond_string);
10009 make_cleanup (xfree, cond_string);
10010 }
10011 b->cond_string = cond_string;
10012 }
e7e0cddf 10013 b->extra_string = NULL;
0101ce28 10014 b->ignore_count = ignore_count;
0101ce28 10015 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 10016 b->condition_not_parsed = 1;
41447f92 10017 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
10018 if ((type_wanted != bp_breakpoint
10019 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 10020 b->pspace = current_program_space;
8bea4e01 10021
bfccc43c 10022 install_breakpoint (internal, b, 0);
0101ce28
JJ
10023 }
10024
f8eba3c6 10025 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 10026 {
3e43a32a
MS
10027 warning (_("Multiple breakpoints were set.\nUse the "
10028 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 10029 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
10030 }
10031
80c99de1
PA
10032 /* That's it. Discard the cleanups for data inserted into the
10033 breakpoint. */
10034 discard_cleanups (bkpt_chain);
10035 /* But cleanup everything else. */
c3f6f71d 10036 do_cleanups (old_chain);
217dc9e2 10037
80c99de1 10038 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 10039 update_global_location_list (1);
fd9b8c24
PA
10040
10041 return 1;
c3f6f71d 10042}
c906108c 10043
348d480f 10044/* Set a breakpoint.
72b2ff0e
VP
10045 ARG is a string describing breakpoint address,
10046 condition, and thread.
10047 FLAG specifies if a breakpoint is hardware on,
10048 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10049 and BP_TEMPFLAG. */
348d480f 10050
98deb0da 10051static void
72b2ff0e 10052break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10053{
72b2ff0e 10054 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10055 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10056 ? bp_hardware_breakpoint
10057 : bp_breakpoint);
55aa24fb
SDJ
10058 struct breakpoint_ops *ops;
10059 const char *arg_cp = arg;
10060
10061 /* Matching breakpoints on probes. */
10062 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10063 ops = &bkpt_probe_breakpoint_ops;
10064 else
10065 ops = &bkpt_breakpoint_ops;
c3f6f71d 10066
8cdf0e15
VP
10067 create_breakpoint (get_current_arch (),
10068 arg,
e7e0cddf 10069 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 10070 tempflag, type_wanted,
8cdf0e15
VP
10071 0 /* Ignore count */,
10072 pending_break_support,
55aa24fb 10073 ops,
8cdf0e15 10074 from_tty,
84f4c1fe 10075 1 /* enabled */,
44f238bb
PA
10076 0 /* internal */,
10077 0);
c906108c
SS
10078}
10079
c906108c
SS
10080/* Helper function for break_command_1 and disassemble_command. */
10081
10082void
fba45db2 10083resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10084{
10085 CORE_ADDR pc;
10086
10087 if (sal->pc == 0 && sal->symtab != NULL)
10088 {
10089 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10090 error (_("No line %d in file \"%s\"."),
05cba821 10091 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10092 sal->pc = pc;
6a048695 10093
4a64f543
MS
10094 /* If this SAL corresponds to a breakpoint inserted using a line
10095 number, then skip the function prologue if necessary. */
6a048695 10096 if (sal->explicit_line)
059acae7 10097 skip_prologue_sal (sal);
c906108c
SS
10098 }
10099
10100 if (sal->section == 0 && sal->symtab != NULL)
10101 {
10102 struct blockvector *bv;
c5aa993b
JM
10103 struct block *b;
10104 struct symbol *sym;
c906108c 10105
801e3a5b 10106 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
10107 if (bv != NULL)
10108 {
7f0df278 10109 sym = block_linkage_function (b);
c906108c
SS
10110 if (sym != NULL)
10111 {
10112 fixup_symbol_section (sym, sal->symtab->objfile);
e27d198c 10113 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
c906108c
SS
10114 }
10115 else
10116 {
4a64f543
MS
10117 /* It really is worthwhile to have the section, so we'll
10118 just have to look harder. This case can be executed
10119 if we have line numbers but no functions (as can
10120 happen in assembly source). */
c906108c 10121
7cbd4a93 10122 struct bound_minimal_symbol msym;
6c95b8df
PA
10123 struct cleanup *old_chain = save_current_space_and_thread ();
10124
10125 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10126
10127 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10128 if (msym.minsym)
efd66ac6 10129 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10130
10131 do_cleanups (old_chain);
c906108c
SS
10132 }
10133 }
10134 }
10135}
10136
10137void
fba45db2 10138break_command (char *arg, int from_tty)
c906108c 10139{
db107f19 10140 break_command_1 (arg, 0, from_tty);
c906108c
SS
10141}
10142
c906108c 10143void
fba45db2 10144tbreak_command (char *arg, int from_tty)
c906108c 10145{
db107f19 10146 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10147}
10148
c906108c 10149static void
fba45db2 10150hbreak_command (char *arg, int from_tty)
c906108c 10151{
db107f19 10152 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10153}
10154
10155static void
fba45db2 10156thbreak_command (char *arg, int from_tty)
c906108c 10157{
db107f19 10158 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10159}
10160
10161static void
fba45db2 10162stop_command (char *arg, int from_tty)
c906108c 10163{
a3f17187 10164 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10165Usage: stop in <function | address>\n\
a3f17187 10166 stop at <line>\n"));
c906108c
SS
10167}
10168
10169static void
fba45db2 10170stopin_command (char *arg, int from_tty)
c906108c
SS
10171{
10172 int badInput = 0;
10173
c5aa993b 10174 if (arg == (char *) NULL)
c906108c
SS
10175 badInput = 1;
10176 else if (*arg != '*')
10177 {
10178 char *argptr = arg;
10179 int hasColon = 0;
10180
4a64f543 10181 /* Look for a ':'. If this is a line number specification, then
53a5351d 10182 say it is bad, otherwise, it should be an address or
4a64f543 10183 function/method name. */
c906108c 10184 while (*argptr && !hasColon)
c5aa993b
JM
10185 {
10186 hasColon = (*argptr == ':');
10187 argptr++;
10188 }
c906108c
SS
10189
10190 if (hasColon)
c5aa993b 10191 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10192 else
c5aa993b 10193 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10194 }
10195
10196 if (badInput)
a3f17187 10197 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10198 else
db107f19 10199 break_command_1 (arg, 0, from_tty);
c906108c
SS
10200}
10201
10202static void
fba45db2 10203stopat_command (char *arg, int from_tty)
c906108c
SS
10204{
10205 int badInput = 0;
10206
c5aa993b 10207 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10208 badInput = 1;
10209 else
10210 {
10211 char *argptr = arg;
10212 int hasColon = 0;
10213
4a64f543
MS
10214 /* Look for a ':'. If there is a '::' then get out, otherwise
10215 it is probably a line number. */
c906108c 10216 while (*argptr && !hasColon)
c5aa993b
JM
10217 {
10218 hasColon = (*argptr == ':');
10219 argptr++;
10220 }
c906108c
SS
10221
10222 if (hasColon)
c5aa993b 10223 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10224 else
c5aa993b 10225 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10226 }
10227
10228 if (badInput)
a3f17187 10229 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10230 else
db107f19 10231 break_command_1 (arg, 0, from_tty);
c906108c
SS
10232}
10233
e7e0cddf
SS
10234/* The dynamic printf command is mostly like a regular breakpoint, but
10235 with a prewired command list consisting of a single output command,
10236 built from extra arguments supplied on the dprintf command
10237 line. */
10238
da821c7b 10239static void
e7e0cddf
SS
10240dprintf_command (char *arg, int from_tty)
10241{
10242 create_breakpoint (get_current_arch (),
10243 arg,
10244 NULL, 0, NULL, 1 /* parse arg */,
10245 0, bp_dprintf,
10246 0 /* Ignore count */,
10247 pending_break_support,
10248 &dprintf_breakpoint_ops,
10249 from_tty,
10250 1 /* enabled */,
10251 0 /* internal */,
10252 0);
10253}
10254
d3ce09f5
SS
10255static void
10256agent_printf_command (char *arg, int from_tty)
10257{
10258 error (_("May only run agent-printf on the target"));
10259}
10260
f1310107
TJB
10261/* Implement the "breakpoint_hit" breakpoint_ops method for
10262 ranged breakpoints. */
10263
10264static int
10265breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10266 struct address_space *aspace,
09ac7c10
TT
10267 CORE_ADDR bp_addr,
10268 const struct target_waitstatus *ws)
f1310107 10269{
09ac7c10 10270 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10271 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10272 return 0;
10273
f1310107
TJB
10274 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10275 bl->length, aspace, bp_addr);
10276}
10277
10278/* Implement the "resources_needed" breakpoint_ops method for
10279 ranged breakpoints. */
10280
10281static int
10282resources_needed_ranged_breakpoint (const struct bp_location *bl)
10283{
10284 return target_ranged_break_num_registers ();
10285}
10286
10287/* Implement the "print_it" breakpoint_ops method for
10288 ranged breakpoints. */
10289
10290static enum print_stop_action
348d480f 10291print_it_ranged_breakpoint (bpstat bs)
f1310107 10292{
348d480f 10293 struct breakpoint *b = bs->breakpoint_at;
f1310107 10294 struct bp_location *bl = b->loc;
79a45e25 10295 struct ui_out *uiout = current_uiout;
f1310107
TJB
10296
10297 gdb_assert (b->type == bp_hardware_breakpoint);
10298
10299 /* Ranged breakpoints have only one location. */
10300 gdb_assert (bl && bl->next == NULL);
10301
10302 annotate_breakpoint (b->number);
10303 if (b->disposition == disp_del)
10304 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10305 else
10306 ui_out_text (uiout, "\nRanged breakpoint ");
10307 if (ui_out_is_mi_like_p (uiout))
10308 {
10309 ui_out_field_string (uiout, "reason",
10310 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10311 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10312 }
10313 ui_out_field_int (uiout, "bkptno", b->number);
10314 ui_out_text (uiout, ", ");
10315
10316 return PRINT_SRC_AND_LOC;
10317}
10318
10319/* Implement the "print_one" breakpoint_ops method for
10320 ranged breakpoints. */
10321
10322static void
10323print_one_ranged_breakpoint (struct breakpoint *b,
10324 struct bp_location **last_loc)
10325{
10326 struct bp_location *bl = b->loc;
10327 struct value_print_options opts;
79a45e25 10328 struct ui_out *uiout = current_uiout;
f1310107
TJB
10329
10330 /* Ranged breakpoints have only one location. */
10331 gdb_assert (bl && bl->next == NULL);
10332
10333 get_user_print_options (&opts);
10334
10335 if (opts.addressprint)
10336 /* We don't print the address range here, it will be printed later
10337 by print_one_detail_ranged_breakpoint. */
10338 ui_out_field_skip (uiout, "addr");
10339 annotate_field (5);
10340 print_breakpoint_location (b, bl);
10341 *last_loc = bl;
10342}
10343
10344/* Implement the "print_one_detail" breakpoint_ops method for
10345 ranged breakpoints. */
10346
10347static void
10348print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10349 struct ui_out *uiout)
10350{
10351 CORE_ADDR address_start, address_end;
10352 struct bp_location *bl = b->loc;
f99d8bf4
PA
10353 struct ui_file *stb = mem_fileopen ();
10354 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10355
10356 gdb_assert (bl);
10357
10358 address_start = bl->address;
10359 address_end = address_start + bl->length - 1;
10360
10361 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10362 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10363 print_core_address (bl->gdbarch, address_start),
10364 print_core_address (bl->gdbarch, address_end));
10365 ui_out_field_stream (uiout, "addr", stb);
10366 ui_out_text (uiout, "\n");
10367
10368 do_cleanups (cleanup);
10369}
10370
10371/* Implement the "print_mention" breakpoint_ops method for
10372 ranged breakpoints. */
10373
10374static void
10375print_mention_ranged_breakpoint (struct breakpoint *b)
10376{
10377 struct bp_location *bl = b->loc;
79a45e25 10378 struct ui_out *uiout = current_uiout;
f1310107
TJB
10379
10380 gdb_assert (bl);
10381 gdb_assert (b->type == bp_hardware_breakpoint);
10382
10383 if (ui_out_is_mi_like_p (uiout))
10384 return;
10385
10386 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10387 b->number, paddress (bl->gdbarch, bl->address),
10388 paddress (bl->gdbarch, bl->address + bl->length - 1));
10389}
10390
10391/* Implement the "print_recreate" breakpoint_ops method for
10392 ranged breakpoints. */
10393
10394static void
10395print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10396{
10397 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10398 b->addr_string_range_end);
d9b3f62e 10399 print_recreate_thread (b, fp);
f1310107
TJB
10400}
10401
10402/* The breakpoint_ops structure to be used in ranged breakpoints. */
10403
2060206e 10404static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10405
10406/* Find the address where the end of the breakpoint range should be
10407 placed, given the SAL of the end of the range. This is so that if
10408 the user provides a line number, the end of the range is set to the
10409 last instruction of the given line. */
10410
10411static CORE_ADDR
10412find_breakpoint_range_end (struct symtab_and_line sal)
10413{
10414 CORE_ADDR end;
10415
10416 /* If the user provided a PC value, use it. Otherwise,
10417 find the address of the end of the given location. */
10418 if (sal.explicit_pc)
10419 end = sal.pc;
10420 else
10421 {
10422 int ret;
10423 CORE_ADDR start;
10424
10425 ret = find_line_pc_range (sal, &start, &end);
10426 if (!ret)
10427 error (_("Could not find location of the end of the range."));
10428
10429 /* find_line_pc_range returns the start of the next line. */
10430 end--;
10431 }
10432
10433 return end;
10434}
10435
10436/* Implement the "break-range" CLI command. */
10437
10438static void
10439break_range_command (char *arg, int from_tty)
10440{
10441 char *arg_start, *addr_string_start, *addr_string_end;
10442 struct linespec_result canonical_start, canonical_end;
10443 int bp_count, can_use_bp, length;
10444 CORE_ADDR end;
10445 struct breakpoint *b;
10446 struct symtab_and_line sal_start, sal_end;
f1310107 10447 struct cleanup *cleanup_bkpt;
f8eba3c6 10448 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10449
10450 /* We don't support software ranged breakpoints. */
10451 if (target_ranged_break_num_registers () < 0)
10452 error (_("This target does not support hardware ranged breakpoints."));
10453
10454 bp_count = hw_breakpoint_used_count ();
10455 bp_count += target_ranged_break_num_registers ();
10456 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10457 bp_count, 0);
10458 if (can_use_bp < 0)
10459 error (_("Hardware breakpoints used exceeds limit."));
10460
f8eba3c6 10461 arg = skip_spaces (arg);
f1310107
TJB
10462 if (arg == NULL || arg[0] == '\0')
10463 error(_("No address range specified."));
10464
f1310107
TJB
10465 init_linespec_result (&canonical_start);
10466
f8eba3c6
TT
10467 arg_start = arg;
10468 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10469
f8eba3c6 10470 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10471
10472 if (arg[0] != ',')
10473 error (_("Too few arguments."));
f8eba3c6 10474 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10475 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10476
10477 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10478
10479 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10480 || lsal_start->sals.nelts != 1)
f1310107
TJB
10481 error (_("Cannot create a ranged breakpoint with multiple locations."));
10482
f8eba3c6
TT
10483 sal_start = lsal_start->sals.sals[0];
10484 addr_string_start = savestring (arg_start, arg - arg_start);
10485 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10486
10487 arg++; /* Skip the comma. */
f8eba3c6 10488 arg = skip_spaces (arg);
f1310107
TJB
10489
10490 /* Parse the end location. */
10491
f1310107
TJB
10492 init_linespec_result (&canonical_end);
10493 arg_start = arg;
10494
f8eba3c6 10495 /* We call decode_line_full directly here instead of using
f1310107
TJB
10496 parse_breakpoint_sals because we need to specify the start location's
10497 symtab and line as the default symtab and line for the end of the
10498 range. This makes it possible to have ranges like "foo.c:27, +14",
10499 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10500 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10501 sal_start.symtab, sal_start.line,
10502 &canonical_end, NULL, NULL);
10503
10504 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10505
f8eba3c6 10506 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10507 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10508
10509 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10510 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10511 || lsal_end->sals.nelts != 1)
f1310107
TJB
10512 error (_("Cannot create a ranged breakpoint with multiple locations."));
10513
f8eba3c6
TT
10514 sal_end = lsal_end->sals.sals[0];
10515 addr_string_end = savestring (arg_start, arg - arg_start);
10516 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10517
10518 end = find_breakpoint_range_end (sal_end);
10519 if (sal_start.pc > end)
177b42fe 10520 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10521
10522 length = end - sal_start.pc + 1;
10523 if (length < 0)
10524 /* Length overflowed. */
10525 error (_("Address range too large."));
10526 else if (length == 1)
10527 {
10528 /* This range is simple enough to be handled by
10529 the `hbreak' command. */
10530 hbreak_command (addr_string_start, 1);
10531
10532 do_cleanups (cleanup_bkpt);
10533
10534 return;
10535 }
10536
10537 /* Now set up the breakpoint. */
10538 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10539 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10540 set_breakpoint_count (breakpoint_count + 1);
10541 b->number = breakpoint_count;
10542 b->disposition = disp_donttouch;
f8eba3c6
TT
10543 b->addr_string = xstrdup (addr_string_start);
10544 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10545 b->loc->length = length;
10546
f8eba3c6 10547 do_cleanups (cleanup_bkpt);
f1310107
TJB
10548
10549 mention (b);
8d3788bd 10550 observer_notify_breakpoint_created (b);
f1310107
TJB
10551 update_global_location_list (1);
10552}
10553
4a64f543
MS
10554/* Return non-zero if EXP is verified as constant. Returned zero
10555 means EXP is variable. Also the constant detection may fail for
10556 some constant expressions and in such case still falsely return
10557 zero. */
2e6e3d9c 10558
65d79d4b
SDJ
10559static int
10560watchpoint_exp_is_const (const struct expression *exp)
10561{
10562 int i = exp->nelts;
10563
10564 while (i > 0)
10565 {
10566 int oplenp, argsp;
10567
10568 /* We are only interested in the descriptor of each element. */
10569 operator_length (exp, i, &oplenp, &argsp);
10570 i -= oplenp;
10571
10572 switch (exp->elts[i].opcode)
10573 {
10574 case BINOP_ADD:
10575 case BINOP_SUB:
10576 case BINOP_MUL:
10577 case BINOP_DIV:
10578 case BINOP_REM:
10579 case BINOP_MOD:
10580 case BINOP_LSH:
10581 case BINOP_RSH:
10582 case BINOP_LOGICAL_AND:
10583 case BINOP_LOGICAL_OR:
10584 case BINOP_BITWISE_AND:
10585 case BINOP_BITWISE_IOR:
10586 case BINOP_BITWISE_XOR:
10587 case BINOP_EQUAL:
10588 case BINOP_NOTEQUAL:
10589 case BINOP_LESS:
10590 case BINOP_GTR:
10591 case BINOP_LEQ:
10592 case BINOP_GEQ:
10593 case BINOP_REPEAT:
10594 case BINOP_COMMA:
10595 case BINOP_EXP:
10596 case BINOP_MIN:
10597 case BINOP_MAX:
10598 case BINOP_INTDIV:
10599 case BINOP_CONCAT:
10600 case BINOP_IN:
10601 case BINOP_RANGE:
10602 case TERNOP_COND:
10603 case TERNOP_SLICE:
65d79d4b
SDJ
10604
10605 case OP_LONG:
10606 case OP_DOUBLE:
10607 case OP_DECFLOAT:
10608 case OP_LAST:
10609 case OP_COMPLEX:
10610 case OP_STRING:
65d79d4b
SDJ
10611 case OP_ARRAY:
10612 case OP_TYPE:
608b4967
TT
10613 case OP_TYPEOF:
10614 case OP_DECLTYPE:
6e72ca20 10615 case OP_TYPEID:
65d79d4b
SDJ
10616 case OP_NAME:
10617 case OP_OBJC_NSSTRING:
10618
10619 case UNOP_NEG:
10620 case UNOP_LOGICAL_NOT:
10621 case UNOP_COMPLEMENT:
10622 case UNOP_ADDR:
10623 case UNOP_HIGH:
aeaa2474 10624 case UNOP_CAST:
9eaf6705
TT
10625
10626 case UNOP_CAST_TYPE:
10627 case UNOP_REINTERPRET_CAST:
10628 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10629 /* Unary, binary and ternary operators: We have to check
10630 their operands. If they are constant, then so is the
10631 result of that operation. For instance, if A and B are
10632 determined to be constants, then so is "A + B".
10633
10634 UNOP_IND is one exception to the rule above, because the
10635 value of *ADDR is not necessarily a constant, even when
10636 ADDR is. */
65d79d4b
SDJ
10637 break;
10638
10639 case OP_VAR_VALUE:
10640 /* Check whether the associated symbol is a constant.
4a64f543 10641
65d79d4b 10642 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10643 possible that a buggy compiler could mark a variable as
10644 constant even when it is not, and TYPE_CONST would return
10645 true in this case, while SYMBOL_CLASS wouldn't.
10646
10647 We also have to check for function symbols because they
10648 are always constant. */
65d79d4b
SDJ
10649 {
10650 struct symbol *s = exp->elts[i + 2].symbol;
10651
10652 if (SYMBOL_CLASS (s) != LOC_BLOCK
10653 && SYMBOL_CLASS (s) != LOC_CONST
10654 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10655 return 0;
10656 break;
10657 }
10658
10659 /* The default action is to return 0 because we are using
10660 the optimistic approach here: If we don't know something,
10661 then it is not a constant. */
10662 default:
10663 return 0;
10664 }
10665 }
10666
10667 return 1;
10668}
10669
3a5c3e22
PA
10670/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10671
10672static void
10673dtor_watchpoint (struct breakpoint *self)
10674{
10675 struct watchpoint *w = (struct watchpoint *) self;
10676
10677 xfree (w->cond_exp);
10678 xfree (w->exp);
10679 xfree (w->exp_string);
10680 xfree (w->exp_string_reparse);
10681 value_free (w->val);
10682
10683 base_breakpoint_ops.dtor (self);
10684}
10685
348d480f
PA
10686/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10687
10688static void
10689re_set_watchpoint (struct breakpoint *b)
10690{
3a5c3e22
PA
10691 struct watchpoint *w = (struct watchpoint *) b;
10692
348d480f
PA
10693 /* Watchpoint can be either on expression using entirely global
10694 variables, or it can be on local variables.
10695
10696 Watchpoints of the first kind are never auto-deleted, and even
10697 persist across program restarts. Since they can use variables
10698 from shared libraries, we need to reparse expression as libraries
10699 are loaded and unloaded.
10700
10701 Watchpoints on local variables can also change meaning as result
10702 of solib event. For example, if a watchpoint uses both a local
10703 and a global variables in expression, it's a local watchpoint,
10704 but unloading of a shared library will make the expression
10705 invalid. This is not a very common use case, but we still
10706 re-evaluate expression, to avoid surprises to the user.
10707
10708 Note that for local watchpoints, we re-evaluate it only if
10709 watchpoints frame id is still valid. If it's not, it means the
10710 watchpoint is out of scope and will be deleted soon. In fact,
10711 I'm not sure we'll ever be called in this case.
10712
10713 If a local watchpoint's frame id is still valid, then
3a5c3e22 10714 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10715
3a5c3e22
PA
10716 Don't do anything about disabled watchpoints, since they will be
10717 reevaluated again when enabled. */
10718 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10719}
10720
77b06cd7
TJB
10721/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10722
10723static int
10724insert_watchpoint (struct bp_location *bl)
10725{
3a5c3e22
PA
10726 struct watchpoint *w = (struct watchpoint *) bl->owner;
10727 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10728
10729 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10730 w->cond_exp);
77b06cd7
TJB
10731}
10732
10733/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10734
10735static int
10736remove_watchpoint (struct bp_location *bl)
10737{
3a5c3e22
PA
10738 struct watchpoint *w = (struct watchpoint *) bl->owner;
10739 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10740
10741 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10742 w->cond_exp);
e09342b5
TJB
10743}
10744
e09342b5 10745static int
348d480f 10746breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10747 struct address_space *aspace, CORE_ADDR bp_addr,
10748 const struct target_waitstatus *ws)
e09342b5 10749{
348d480f 10750 struct breakpoint *b = bl->owner;
3a5c3e22 10751 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10752
348d480f
PA
10753 /* Continuable hardware watchpoints are treated as non-existent if the
10754 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10755 some data address). Otherwise gdb won't stop on a break instruction
10756 in the code (not from a breakpoint) when a hardware watchpoint has
10757 been defined. Also skip watchpoints which we know did not trigger
10758 (did not match the data address). */
10759 if (is_hardware_watchpoint (b)
3a5c3e22 10760 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10761 return 0;
9c06b0b4 10762
348d480f 10763 return 1;
9c06b0b4
TJB
10764}
10765
348d480f
PA
10766static void
10767check_status_watchpoint (bpstat bs)
9c06b0b4 10768{
348d480f 10769 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10770
348d480f 10771 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10772}
10773
10774/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10775 hardware watchpoints. */
9c06b0b4
TJB
10776
10777static int
348d480f 10778resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10779{
3a5c3e22
PA
10780 struct watchpoint *w = (struct watchpoint *) bl->owner;
10781 int length = w->exact? 1 : bl->length;
348d480f
PA
10782
10783 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10784}
10785
10786/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10787 hardware watchpoints. */
9c06b0b4
TJB
10788
10789static int
348d480f 10790works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10791{
efa80663
PA
10792 /* Read and access watchpoints only work with hardware support. */
10793 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10794}
10795
9c06b0b4 10796static enum print_stop_action
348d480f 10797print_it_watchpoint (bpstat bs)
9c06b0b4 10798{
348d480f
PA
10799 struct cleanup *old_chain;
10800 struct breakpoint *b;
f99d8bf4 10801 struct ui_file *stb;
348d480f 10802 enum print_stop_action result;
3a5c3e22 10803 struct watchpoint *w;
79a45e25 10804 struct ui_out *uiout = current_uiout;
348d480f
PA
10805
10806 gdb_assert (bs->bp_location_at != NULL);
10807
348d480f 10808 b = bs->breakpoint_at;
3a5c3e22 10809 w = (struct watchpoint *) b;
348d480f 10810
f99d8bf4
PA
10811 stb = mem_fileopen ();
10812 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10813
10814 switch (b->type)
10815 {
348d480f 10816 case bp_watchpoint:
9c06b0b4
TJB
10817 case bp_hardware_watchpoint:
10818 annotate_watchpoint (b->number);
10819 if (ui_out_is_mi_like_p (uiout))
10820 ui_out_field_string
10821 (uiout, "reason",
10822 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10823 mention (b);
10824 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10825 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10826 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10827 ui_out_field_stream (uiout, "old", stb);
10828 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10829 watchpoint_value_print (w->val, stb);
348d480f
PA
10830 ui_out_field_stream (uiout, "new", stb);
10831 ui_out_text (uiout, "\n");
10832 /* More than one watchpoint may have been triggered. */
10833 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10834 break;
10835
10836 case bp_read_watchpoint:
10837 if (ui_out_is_mi_like_p (uiout))
10838 ui_out_field_string
10839 (uiout, "reason",
10840 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10841 mention (b);
10842 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10843 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10844 watchpoint_value_print (w->val, stb);
348d480f
PA
10845 ui_out_field_stream (uiout, "value", stb);
10846 ui_out_text (uiout, "\n");
10847 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10848 break;
10849
10850 case bp_access_watchpoint:
348d480f
PA
10851 if (bs->old_val != NULL)
10852 {
10853 annotate_watchpoint (b->number);
10854 if (ui_out_is_mi_like_p (uiout))
10855 ui_out_field_string
10856 (uiout, "reason",
10857 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10858 mention (b);
10859 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10860 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10861 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10862 ui_out_field_stream (uiout, "old", stb);
10863 ui_out_text (uiout, "\nNew value = ");
10864 }
10865 else
10866 {
10867 mention (b);
10868 if (ui_out_is_mi_like_p (uiout))
10869 ui_out_field_string
10870 (uiout, "reason",
10871 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10872 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10873 ui_out_text (uiout, "\nValue = ");
10874 }
f99d8bf4 10875 watchpoint_value_print (w->val, stb);
348d480f
PA
10876 ui_out_field_stream (uiout, "new", stb);
10877 ui_out_text (uiout, "\n");
10878 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10879 break;
10880 default:
348d480f 10881 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10882 }
10883
348d480f
PA
10884 do_cleanups (old_chain);
10885 return result;
10886}
10887
10888/* Implement the "print_mention" breakpoint_ops method for hardware
10889 watchpoints. */
10890
10891static void
10892print_mention_watchpoint (struct breakpoint *b)
10893{
10894 struct cleanup *ui_out_chain;
3a5c3e22 10895 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10896 struct ui_out *uiout = current_uiout;
348d480f
PA
10897
10898 switch (b->type)
10899 {
10900 case bp_watchpoint:
10901 ui_out_text (uiout, "Watchpoint ");
10902 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10903 break;
10904 case bp_hardware_watchpoint:
10905 ui_out_text (uiout, "Hardware watchpoint ");
10906 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10907 break;
10908 case bp_read_watchpoint:
10909 ui_out_text (uiout, "Hardware read watchpoint ");
10910 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10911 break;
10912 case bp_access_watchpoint:
10913 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10914 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10915 break;
10916 default:
10917 internal_error (__FILE__, __LINE__,
10918 _("Invalid hardware watchpoint type."));
10919 }
10920
10921 ui_out_field_int (uiout, "number", b->number);
10922 ui_out_text (uiout, ": ");
3a5c3e22 10923 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10924 do_cleanups (ui_out_chain);
10925}
10926
10927/* Implement the "print_recreate" breakpoint_ops method for
10928 watchpoints. */
10929
10930static void
10931print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10932{
3a5c3e22
PA
10933 struct watchpoint *w = (struct watchpoint *) b;
10934
348d480f
PA
10935 switch (b->type)
10936 {
10937 case bp_watchpoint:
10938 case bp_hardware_watchpoint:
10939 fprintf_unfiltered (fp, "watch");
10940 break;
10941 case bp_read_watchpoint:
10942 fprintf_unfiltered (fp, "rwatch");
10943 break;
10944 case bp_access_watchpoint:
10945 fprintf_unfiltered (fp, "awatch");
10946 break;
10947 default:
10948 internal_error (__FILE__, __LINE__,
10949 _("Invalid watchpoint type."));
10950 }
10951
3a5c3e22 10952 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10953 print_recreate_thread (b, fp);
348d480f
PA
10954}
10955
427cd150
TT
10956/* Implement the "explains_signal" breakpoint_ops method for
10957 watchpoints. */
10958
47591c29 10959static int
427cd150
TT
10960explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10961{
10962 /* A software watchpoint cannot cause a signal other than
10963 GDB_SIGNAL_TRAP. */
10964 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10965 return 0;
427cd150 10966
47591c29 10967 return 1;
427cd150
TT
10968}
10969
348d480f
PA
10970/* The breakpoint_ops structure to be used in hardware watchpoints. */
10971
2060206e 10972static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10973
10974/* Implement the "insert" breakpoint_ops method for
10975 masked hardware watchpoints. */
10976
10977static int
10978insert_masked_watchpoint (struct bp_location *bl)
10979{
3a5c3e22
PA
10980 struct watchpoint *w = (struct watchpoint *) bl->owner;
10981
10982 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10983 bl->watchpoint_type);
10984}
10985
10986/* Implement the "remove" breakpoint_ops method for
10987 masked hardware watchpoints. */
10988
10989static int
10990remove_masked_watchpoint (struct bp_location *bl)
10991{
3a5c3e22
PA
10992 struct watchpoint *w = (struct watchpoint *) bl->owner;
10993
10994 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10995 bl->watchpoint_type);
10996}
10997
10998/* Implement the "resources_needed" breakpoint_ops method for
10999 masked hardware watchpoints. */
11000
11001static int
11002resources_needed_masked_watchpoint (const struct bp_location *bl)
11003{
3a5c3e22
PA
11004 struct watchpoint *w = (struct watchpoint *) bl->owner;
11005
11006 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
11007}
11008
11009/* Implement the "works_in_software_mode" breakpoint_ops method for
11010 masked hardware watchpoints. */
11011
11012static int
11013works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11014{
11015 return 0;
11016}
11017
11018/* Implement the "print_it" breakpoint_ops method for
11019 masked hardware watchpoints. */
11020
11021static enum print_stop_action
11022print_it_masked_watchpoint (bpstat bs)
11023{
11024 struct breakpoint *b = bs->breakpoint_at;
79a45e25 11025 struct ui_out *uiout = current_uiout;
348d480f
PA
11026
11027 /* Masked watchpoints have only one location. */
11028 gdb_assert (b->loc && b->loc->next == NULL);
11029
11030 switch (b->type)
11031 {
11032 case bp_hardware_watchpoint:
11033 annotate_watchpoint (b->number);
11034 if (ui_out_is_mi_like_p (uiout))
11035 ui_out_field_string
11036 (uiout, "reason",
11037 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11038 break;
11039
11040 case bp_read_watchpoint:
11041 if (ui_out_is_mi_like_p (uiout))
11042 ui_out_field_string
11043 (uiout, "reason",
11044 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11045 break;
11046
11047 case bp_access_watchpoint:
11048 if (ui_out_is_mi_like_p (uiout))
11049 ui_out_field_string
11050 (uiout, "reason",
11051 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11052 break;
11053 default:
11054 internal_error (__FILE__, __LINE__,
11055 _("Invalid hardware watchpoint type."));
11056 }
11057
11058 mention (b);
9c06b0b4
TJB
11059 ui_out_text (uiout, _("\n\
11060Check the underlying instruction at PC for the memory\n\
11061address and value which triggered this watchpoint.\n"));
11062 ui_out_text (uiout, "\n");
11063
11064 /* More than one watchpoint may have been triggered. */
11065 return PRINT_UNKNOWN;
11066}
11067
11068/* Implement the "print_one_detail" breakpoint_ops method for
11069 masked hardware watchpoints. */
11070
11071static void
11072print_one_detail_masked_watchpoint (const struct breakpoint *b,
11073 struct ui_out *uiout)
11074{
3a5c3e22
PA
11075 struct watchpoint *w = (struct watchpoint *) b;
11076
9c06b0b4
TJB
11077 /* Masked watchpoints have only one location. */
11078 gdb_assert (b->loc && b->loc->next == NULL);
11079
11080 ui_out_text (uiout, "\tmask ");
3a5c3e22 11081 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11082 ui_out_text (uiout, "\n");
11083}
11084
11085/* Implement the "print_mention" breakpoint_ops method for
11086 masked hardware watchpoints. */
11087
11088static void
11089print_mention_masked_watchpoint (struct breakpoint *b)
11090{
3a5c3e22 11091 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11092 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11093 struct cleanup *ui_out_chain;
11094
11095 switch (b->type)
11096 {
11097 case bp_hardware_watchpoint:
11098 ui_out_text (uiout, "Masked hardware watchpoint ");
11099 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11100 break;
11101 case bp_read_watchpoint:
11102 ui_out_text (uiout, "Masked hardware read watchpoint ");
11103 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11104 break;
11105 case bp_access_watchpoint:
11106 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11107 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11108 break;
11109 default:
11110 internal_error (__FILE__, __LINE__,
11111 _("Invalid hardware watchpoint type."));
11112 }
11113
11114 ui_out_field_int (uiout, "number", b->number);
11115 ui_out_text (uiout, ": ");
3a5c3e22 11116 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11117 do_cleanups (ui_out_chain);
11118}
11119
11120/* Implement the "print_recreate" breakpoint_ops method for
11121 masked hardware watchpoints. */
11122
11123static void
11124print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11125{
3a5c3e22 11126 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11127 char tmp[40];
11128
11129 switch (b->type)
11130 {
11131 case bp_hardware_watchpoint:
11132 fprintf_unfiltered (fp, "watch");
11133 break;
11134 case bp_read_watchpoint:
11135 fprintf_unfiltered (fp, "rwatch");
11136 break;
11137 case bp_access_watchpoint:
11138 fprintf_unfiltered (fp, "awatch");
11139 break;
11140 default:
11141 internal_error (__FILE__, __LINE__,
11142 _("Invalid hardware watchpoint type."));
11143 }
11144
3a5c3e22
PA
11145 sprintf_vma (tmp, w->hw_wp_mask);
11146 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11147 print_recreate_thread (b, fp);
9c06b0b4
TJB
11148}
11149
11150/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11151
2060206e 11152static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11153
11154/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11155
11156static int
11157is_masked_watchpoint (const struct breakpoint *b)
11158{
11159 return b->ops == &masked_watchpoint_breakpoint_ops;
11160}
11161
53a5351d
JM
11162/* accessflag: hw_write: watch write,
11163 hw_read: watch read,
11164 hw_access: watch access (read or write) */
c906108c 11165static void
bbc13ae3 11166watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11167 int just_location, int internal)
c906108c 11168{
a9634178 11169 volatile struct gdb_exception e;
d983da9c 11170 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11171 struct expression *exp;
270140bd 11172 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11173 struct value *val, *mark, *result;
c906108c 11174 struct frame_info *frame;
bbc13ae3
KS
11175 const char *exp_start = NULL;
11176 const char *exp_end = NULL;
11177 const char *tok, *end_tok;
9c06b0b4 11178 int toklen = -1;
bbc13ae3
KS
11179 const char *cond_start = NULL;
11180 const char *cond_end = NULL;
c906108c 11181 enum bptype bp_type;
37e4754d 11182 int thread = -1;
0cf6dd15 11183 int pc = 0;
9c06b0b4
TJB
11184 /* Flag to indicate whether we are going to use masks for
11185 the hardware watchpoint. */
11186 int use_mask = 0;
11187 CORE_ADDR mask = 0;
3a5c3e22 11188 struct watchpoint *w;
bbc13ae3
KS
11189 char *expression;
11190 struct cleanup *back_to;
c906108c 11191
37e4754d
LM
11192 /* Make sure that we actually have parameters to parse. */
11193 if (arg != NULL && arg[0] != '\0')
11194 {
bbc13ae3
KS
11195 const char *value_start;
11196
11197 exp_end = arg + strlen (arg);
37e4754d 11198
9c06b0b4
TJB
11199 /* Look for "parameter value" pairs at the end
11200 of the arguments string. */
bbc13ae3 11201 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11202 {
11203 /* Skip whitespace at the end of the argument list. */
11204 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11205 tok--;
11206
11207 /* Find the beginning of the last token.
11208 This is the value of the parameter. */
11209 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11210 tok--;
11211 value_start = tok + 1;
11212
11213 /* Skip whitespace. */
11214 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11215 tok--;
11216
11217 end_tok = tok;
11218
11219 /* Find the beginning of the second to last token.
11220 This is the parameter itself. */
11221 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11222 tok--;
11223 tok++;
11224 toklen = end_tok - tok + 1;
11225
11226 if (toklen == 6 && !strncmp (tok, "thread", 6))
11227 {
11228 /* At this point we've found a "thread" token, which means
11229 the user is trying to set a watchpoint that triggers
11230 only in a specific thread. */
11231 char *endp;
37e4754d 11232
9c06b0b4
TJB
11233 if (thread != -1)
11234 error(_("You can specify only one thread."));
37e4754d 11235
9c06b0b4
TJB
11236 /* Extract the thread ID from the next token. */
11237 thread = strtol (value_start, &endp, 0);
37e4754d 11238
9c06b0b4
TJB
11239 /* Check if the user provided a valid numeric value for the
11240 thread ID. */
11241 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11242 error (_("Invalid thread ID specification %s."), value_start);
11243
11244 /* Check if the thread actually exists. */
11245 if (!valid_thread_id (thread))
af4908ba 11246 invalid_thread_id_error (thread);
9c06b0b4
TJB
11247 }
11248 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11249 {
11250 /* We've found a "mask" token, which means the user wants to
11251 create a hardware watchpoint that is going to have the mask
11252 facility. */
11253 struct value *mask_value, *mark;
37e4754d 11254
9c06b0b4
TJB
11255 if (use_mask)
11256 error(_("You can specify only one mask."));
37e4754d 11257
9c06b0b4 11258 use_mask = just_location = 1;
37e4754d 11259
9c06b0b4
TJB
11260 mark = value_mark ();
11261 mask_value = parse_to_comma_and_eval (&value_start);
11262 mask = value_as_address (mask_value);
11263 value_free_to_mark (mark);
11264 }
11265 else
11266 /* We didn't recognize what we found. We should stop here. */
11267 break;
37e4754d 11268
9c06b0b4
TJB
11269 /* Truncate the string and get rid of the "parameter value" pair before
11270 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11271 exp_end = tok;
9c06b0b4 11272 }
37e4754d 11273 }
bbc13ae3
KS
11274 else
11275 exp_end = arg;
37e4754d 11276
bbc13ae3
KS
11277 /* Parse the rest of the arguments. From here on out, everything
11278 is in terms of a newly allocated string instead of the original
11279 ARG. */
c906108c 11280 innermost_block = NULL;
bbc13ae3
KS
11281 expression = savestring (arg, exp_end - arg);
11282 back_to = make_cleanup (xfree, expression);
11283 exp_start = arg = expression;
1bb9788d 11284 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11285 exp_end = arg;
fa8a61dc
TT
11286 /* Remove trailing whitespace from the expression before saving it.
11287 This makes the eventual display of the expression string a bit
11288 prettier. */
11289 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11290 --exp_end;
11291
65d79d4b
SDJ
11292 /* Checking if the expression is not constant. */
11293 if (watchpoint_exp_is_const (exp))
11294 {
11295 int len;
11296
11297 len = exp_end - exp_start;
11298 while (len > 0 && isspace (exp_start[len - 1]))
11299 len--;
11300 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11301 }
11302
c906108c
SS
11303 exp_valid_block = innermost_block;
11304 mark = value_mark ();
3a1115a0 11305 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b
TT
11306
11307 if (just_location)
11308 {
9c06b0b4
TJB
11309 int ret;
11310
06a64a0b 11311 exp_valid_block = NULL;
a1442452 11312 val = value_addr (result);
06a64a0b
TT
11313 release_value (val);
11314 value_free_to_mark (mark);
9c06b0b4
TJB
11315
11316 if (use_mask)
11317 {
11318 ret = target_masked_watch_num_registers (value_as_address (val),
11319 mask);
11320 if (ret == -1)
11321 error (_("This target does not support masked watchpoints."));
11322 else if (ret == -2)
11323 error (_("Invalid mask or memory region."));
11324 }
06a64a0b
TT
11325 }
11326 else if (val != NULL)
fa4727a6 11327 release_value (val);
c906108c 11328
bbc13ae3
KS
11329 tok = skip_spaces_const (arg);
11330 end_tok = skip_to_space_const (tok);
c906108c
SS
11331
11332 toklen = end_tok - tok;
11333 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11334 {
2d134ed3
PA
11335 struct expression *cond;
11336
60e1c644 11337 innermost_block = NULL;
c906108c 11338 tok = cond_start = end_tok + 1;
1bb9788d 11339 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11340
11341 /* The watchpoint expression may not be local, but the condition
11342 may still be. E.g.: `watch global if local > 0'. */
11343 cond_exp_valid_block = innermost_block;
11344
2d134ed3 11345 xfree (cond);
c906108c
SS
11346 cond_end = tok;
11347 }
11348 if (*tok)
8a3fe4f8 11349 error (_("Junk at end of command."));
c906108c 11350
d983da9c 11351 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11352
11353 /* If the expression is "local", then set up a "watchpoint scope"
11354 breakpoint at the point where we've left the scope of the watchpoint
11355 expression. Create the scope breakpoint before the watchpoint, so
11356 that we will encounter it first in bpstat_stop_status. */
60e1c644 11357 if (exp_valid_block && frame)
d983da9c 11358 {
edb3359d
DJ
11359 if (frame_id_p (frame_unwind_caller_id (frame)))
11360 {
11361 scope_breakpoint
a6d9a66e
UW
11362 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11363 frame_unwind_caller_pc (frame),
06edf0c0
PA
11364 bp_watchpoint_scope,
11365 &momentary_breakpoint_ops);
d983da9c 11366
edb3359d 11367 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11368
edb3359d
DJ
11369 /* Automatically delete the breakpoint when it hits. */
11370 scope_breakpoint->disposition = disp_del;
d983da9c 11371
edb3359d
DJ
11372 /* Only break in the proper frame (help with recursion). */
11373 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11374
edb3359d 11375 /* Set the address at which we will stop. */
a6d9a66e
UW
11376 scope_breakpoint->loc->gdbarch
11377 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11378 scope_breakpoint->loc->requested_address
11379 = frame_unwind_caller_pc (frame);
11380 scope_breakpoint->loc->address
a6d9a66e
UW
11381 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11382 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11383 scope_breakpoint->type);
11384 }
d983da9c
DJ
11385 }
11386
e8369a73
AB
11387 /* Now set up the breakpoint. We create all watchpoints as hardware
11388 watchpoints here even if hardware watchpoints are turned off, a call
11389 to update_watchpoint later in this function will cause the type to
11390 drop back to bp_watchpoint (software watchpoint) if required. */
11391
11392 if (accessflag == hw_read)
11393 bp_type = bp_read_watchpoint;
11394 else if (accessflag == hw_access)
11395 bp_type = bp_access_watchpoint;
11396 else
11397 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11398
11399 w = XCNEW (struct watchpoint);
11400 b = &w->base;
348d480f 11401 if (use_mask)
3a5c3e22
PA
11402 init_raw_breakpoint_without_location (b, NULL, bp_type,
11403 &masked_watchpoint_breakpoint_ops);
348d480f 11404 else
3a5c3e22
PA
11405 init_raw_breakpoint_without_location (b, NULL, bp_type,
11406 &watchpoint_breakpoint_ops);
37e4754d 11407 b->thread = thread;
b5de0fa7 11408 b->disposition = disp_donttouch;
348d480f 11409 b->pspace = current_program_space;
3a5c3e22
PA
11410 w->exp = exp;
11411 w->exp_valid_block = exp_valid_block;
11412 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11413 if (just_location)
11414 {
11415 struct type *t = value_type (val);
11416 CORE_ADDR addr = value_as_address (val);
11417 char *name;
11418
11419 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11420 name = type_to_string (t);
11421
3a5c3e22 11422 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11423 core_addr_to_string (addr));
06a64a0b
TT
11424 xfree (name);
11425
3a5c3e22 11426 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11427 (int) (exp_end - exp_start), exp_start);
11428
06a64a0b
TT
11429 /* The above expression is in C. */
11430 b->language = language_c;
11431 }
11432 else
3a5c3e22 11433 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11434
11435 if (use_mask)
11436 {
3a5c3e22 11437 w->hw_wp_mask = mask;
9c06b0b4
TJB
11438 }
11439 else
11440 {
3a5c3e22
PA
11441 w->val = val;
11442 w->val_valid = 1;
9c06b0b4 11443 }
77b06cd7 11444
c906108c
SS
11445 if (cond_start)
11446 b->cond_string = savestring (cond_start, cond_end - cond_start);
11447 else
11448 b->cond_string = 0;
c5aa993b 11449
c906108c 11450 if (frame)
f6bc2008 11451 {
3a5c3e22
PA
11452 w->watchpoint_frame = get_frame_id (frame);
11453 w->watchpoint_thread = inferior_ptid;
f6bc2008 11454 }
c906108c 11455 else
f6bc2008 11456 {
3a5c3e22
PA
11457 w->watchpoint_frame = null_frame_id;
11458 w->watchpoint_thread = null_ptid;
f6bc2008 11459 }
c906108c 11460
d983da9c 11461 if (scope_breakpoint != NULL)
c906108c 11462 {
d983da9c
DJ
11463 /* The scope breakpoint is related to the watchpoint. We will
11464 need to act on them together. */
11465 b->related_breakpoint = scope_breakpoint;
11466 scope_breakpoint->related_breakpoint = b;
c906108c 11467 }
d983da9c 11468
06a64a0b
TT
11469 if (!just_location)
11470 value_free_to_mark (mark);
2d134ed3 11471
a9634178
TJB
11472 TRY_CATCH (e, RETURN_MASK_ALL)
11473 {
11474 /* Finally update the new watchpoint. This creates the locations
11475 that should be inserted. */
3a5c3e22 11476 update_watchpoint (w, 1);
a9634178
TJB
11477 }
11478 if (e.reason < 0)
11479 {
11480 delete_breakpoint (b);
11481 throw_exception (e);
11482 }
11483
3ea46bff 11484 install_breakpoint (internal, b, 1);
bbc13ae3 11485 do_cleanups (back_to);
c906108c
SS
11486}
11487
e09342b5 11488/* Return count of debug registers needed to watch the given expression.
e09342b5 11489 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11490
c906108c 11491static int
a9634178 11492can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11493{
11494 int found_memory_cnt = 0;
2e70b7b9 11495 struct value *head = v;
c906108c
SS
11496
11497 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11498 if (!can_use_hw_watchpoints)
c906108c 11499 return 0;
c5aa993b 11500
5c44784c
JM
11501 /* Make sure that the value of the expression depends only upon
11502 memory contents, and values computed from them within GDB. If we
11503 find any register references or function calls, we can't use a
11504 hardware watchpoint.
11505
11506 The idea here is that evaluating an expression generates a series
11507 of values, one holding the value of every subexpression. (The
11508 expression a*b+c has five subexpressions: a, b, a*b, c, and
11509 a*b+c.) GDB's values hold almost enough information to establish
11510 the criteria given above --- they identify memory lvalues,
11511 register lvalues, computed values, etcetera. So we can evaluate
11512 the expression, and then scan the chain of values that leaves
11513 behind to decide whether we can detect any possible change to the
11514 expression's final value using only hardware watchpoints.
11515
11516 However, I don't think that the values returned by inferior
11517 function calls are special in any way. So this function may not
11518 notice that an expression involving an inferior function call
11519 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11520 for (; v; v = value_next (v))
c906108c 11521 {
5c44784c 11522 if (VALUE_LVAL (v) == lval_memory)
c906108c 11523 {
8464be76
DJ
11524 if (v != head && value_lazy (v))
11525 /* A lazy memory lvalue in the chain is one that GDB never
11526 needed to fetch; we either just used its address (e.g.,
11527 `a' in `a.b') or we never needed it at all (e.g., `a'
11528 in `a,b'). This doesn't apply to HEAD; if that is
11529 lazy then it was not readable, but watch it anyway. */
5c44784c 11530 ;
53a5351d 11531 else
5c44784c
JM
11532 {
11533 /* Ahh, memory we actually used! Check if we can cover
11534 it with hardware watchpoints. */
df407dfe 11535 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11536
11537 /* We only watch structs and arrays if user asked for it
11538 explicitly, never if they just happen to appear in a
11539 middle of some value chain. */
11540 if (v == head
11541 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11542 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11543 {
42ae5230 11544 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11545 int len;
11546 int num_regs;
11547
a9634178 11548 len = (target_exact_watchpoints
e09342b5
TJB
11549 && is_scalar_type_recursive (vtype))?
11550 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11551
e09342b5
TJB
11552 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11553 if (!num_regs)
2e70b7b9
MS
11554 return 0;
11555 else
e09342b5 11556 found_memory_cnt += num_regs;
2e70b7b9 11557 }
5c44784c 11558 }
c5aa993b 11559 }
5086187c
AC
11560 else if (VALUE_LVAL (v) != not_lval
11561 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11562 return 0; /* These are values from the history (e.g., $1). */
5086187c 11563 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11564 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11565 }
11566
11567 /* The expression itself looks suitable for using a hardware
11568 watchpoint, but give the target machine a chance to reject it. */
11569 return found_memory_cnt;
11570}
11571
8b93c638 11572void
84f4c1fe 11573watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11574{
84f4c1fe 11575 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11576}
11577
06a64a0b
TT
11578/* A helper function that looks for the "-location" argument and then
11579 calls watch_command_1. */
11580
11581static void
11582watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11583{
11584 int just_location = 0;
11585
11586 if (arg
11587 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11588 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11589 {
e9cafbcc 11590 arg = skip_spaces (arg);
06a64a0b
TT
11591 just_location = 1;
11592 }
11593
84f4c1fe 11594 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11595}
8926118c 11596
c5aa993b 11597static void
fba45db2 11598watch_command (char *arg, int from_tty)
c906108c 11599{
06a64a0b 11600 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11601}
11602
8b93c638 11603void
84f4c1fe 11604rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11605{
84f4c1fe 11606 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11607}
8926118c 11608
c5aa993b 11609static void
fba45db2 11610rwatch_command (char *arg, int from_tty)
c906108c 11611{
06a64a0b 11612 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11613}
11614
8b93c638 11615void
84f4c1fe 11616awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11617{
84f4c1fe 11618 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11619}
8926118c 11620
c5aa993b 11621static void
fba45db2 11622awatch_command (char *arg, int from_tty)
c906108c 11623{
06a64a0b 11624 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11625}
c906108c 11626\f
c5aa993b 11627
43ff13b4 11628/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11629 because it uses the mechanisms of breakpoints. */
11630
bfec99b2
PA
11631struct until_break_command_continuation_args
11632{
11633 struct breakpoint *breakpoint;
11634 struct breakpoint *breakpoint2;
186c406b 11635 int thread_num;
bfec99b2
PA
11636};
11637
43ff13b4 11638/* This function is called by fetch_inferior_event via the
4a64f543 11639 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11640 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11641 command. */
c2c6d25f 11642static void
fa4cd53f 11643until_break_command_continuation (void *arg, int err)
43ff13b4 11644{
bfec99b2
PA
11645 struct until_break_command_continuation_args *a = arg;
11646
11647 delete_breakpoint (a->breakpoint);
11648 if (a->breakpoint2)
11649 delete_breakpoint (a->breakpoint2);
186c406b 11650 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11651}
11652
c906108c 11653void
ae66c1fc 11654until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11655{
11656 struct symtabs_and_lines sals;
11657 struct symtab_and_line sal;
8556afb4
PA
11658 struct frame_info *frame;
11659 struct gdbarch *frame_gdbarch;
11660 struct frame_id stack_frame_id;
11661 struct frame_id caller_frame_id;
c906108c 11662 struct breakpoint *breakpoint;
f107f563 11663 struct breakpoint *breakpoint2 = NULL;
c906108c 11664 struct cleanup *old_chain;
186c406b
TT
11665 int thread;
11666 struct thread_info *tp;
c906108c
SS
11667
11668 clear_proceed_status ();
11669
11670 /* Set a breakpoint where the user wants it and at return from
4a64f543 11671 this function. */
c5aa993b 11672
1bfeeb0f 11673 if (last_displayed_sal_is_valid ())
f8eba3c6 11674 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11675 get_last_displayed_symtab (),
f8eba3c6 11676 get_last_displayed_line ());
c906108c 11677 else
f8eba3c6
TT
11678 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11679 (struct symtab *) NULL, 0);
c5aa993b 11680
c906108c 11681 if (sals.nelts != 1)
8a3fe4f8 11682 error (_("Couldn't get information on specified line."));
c5aa993b 11683
c906108c 11684 sal = sals.sals[0];
4a64f543 11685 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11686
c906108c 11687 if (*arg)
8a3fe4f8 11688 error (_("Junk at end of arguments."));
c5aa993b 11689
c906108c 11690 resolve_sal_pc (&sal);
c5aa993b 11691
186c406b
TT
11692 tp = inferior_thread ();
11693 thread = tp->num;
11694
883bc8d1
PA
11695 old_chain = make_cleanup (null_cleanup, NULL);
11696
8556afb4
PA
11697 /* Note linespec handling above invalidates the frame chain.
11698 Installing a breakpoint also invalidates the frame chain (as it
11699 may need to switch threads), so do any frame handling before
11700 that. */
11701
11702 frame = get_selected_frame (NULL);
11703 frame_gdbarch = get_frame_arch (frame);
11704 stack_frame_id = get_stack_frame_id (frame);
11705 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11706
ae66c1fc
EZ
11707 /* Keep within the current frame, or in frames called by the current
11708 one. */
edb3359d 11709
883bc8d1 11710 if (frame_id_p (caller_frame_id))
c906108c 11711 {
883bc8d1
PA
11712 struct symtab_and_line sal2;
11713
11714 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11715 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11716 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11717 sal2,
11718 caller_frame_id,
f107f563
VP
11719 bp_until);
11720 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11721
883bc8d1 11722 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11723 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11724 }
c5aa993b 11725
c70a6932
JK
11726 /* set_momentary_breakpoint could invalidate FRAME. */
11727 frame = NULL;
11728
883bc8d1
PA
11729 if (anywhere)
11730 /* If the user told us to continue until a specified location,
11731 we don't specify a frame at which we need to stop. */
11732 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11733 null_frame_id, bp_until);
11734 else
11735 /* Otherwise, specify the selected frame, because we want to stop
11736 only at the very same frame. */
11737 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11738 stack_frame_id, bp_until);
11739 make_cleanup_delete_breakpoint (breakpoint);
11740
a493e3e2 11741 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11742
4a64f543
MS
11743 /* If we are running asynchronously, and proceed call above has
11744 actually managed to start the target, arrange for breakpoints to
11745 be deleted when the target stops. Otherwise, we're already
11746 stopped and delete breakpoints via cleanup chain. */
f107f563 11747
8ea051c5 11748 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11749 {
bfec99b2
PA
11750 struct until_break_command_continuation_args *args;
11751 args = xmalloc (sizeof (*args));
f107f563 11752
bfec99b2
PA
11753 args->breakpoint = breakpoint;
11754 args->breakpoint2 = breakpoint2;
186c406b 11755 args->thread_num = thread;
f107f563
VP
11756
11757 discard_cleanups (old_chain);
95e54da7
PA
11758 add_continuation (inferior_thread (),
11759 until_break_command_continuation, args,
604ead4a 11760 xfree);
f107f563
VP
11761 }
11762 else
c5aa993b 11763 do_cleanups (old_chain);
c906108c 11764}
ae66c1fc 11765
c906108c
SS
11766/* This function attempts to parse an optional "if <cond>" clause
11767 from the arg string. If one is not found, it returns NULL.
c5aa993b 11768
c906108c
SS
11769 Else, it returns a pointer to the condition string. (It does not
11770 attempt to evaluate the string against a particular block.) And,
11771 it updates arg to point to the first character following the parsed
4a64f543 11772 if clause in the arg string. */
53a5351d 11773
916703c0 11774char *
fba45db2 11775ep_parse_optional_if_clause (char **arg)
c906108c 11776{
c5aa993b
JM
11777 char *cond_string;
11778
11779 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11780 return NULL;
c5aa993b 11781
4a64f543 11782 /* Skip the "if" keyword. */
c906108c 11783 (*arg) += 2;
c5aa993b 11784
c906108c 11785 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11786 condition string. */
e9cafbcc 11787 *arg = skip_spaces (*arg);
c906108c 11788 cond_string = *arg;
c5aa993b 11789
4a64f543
MS
11790 /* Assume that the condition occupies the remainder of the arg
11791 string. */
c906108c 11792 (*arg) += strlen (cond_string);
c5aa993b 11793
c906108c
SS
11794 return cond_string;
11795}
c5aa993b 11796
c906108c
SS
11797/* Commands to deal with catching events, such as signals, exceptions,
11798 process start/exit, etc. */
c5aa993b
JM
11799
11800typedef enum
11801{
44feb3ce
TT
11802 catch_fork_temporary, catch_vfork_temporary,
11803 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11804}
11805catch_fork_kind;
11806
c906108c 11807static void
cc59ec59
MS
11808catch_fork_command_1 (char *arg, int from_tty,
11809 struct cmd_list_element *command)
c906108c 11810{
a6d9a66e 11811 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11812 char *cond_string = NULL;
44feb3ce
TT
11813 catch_fork_kind fork_kind;
11814 int tempflag;
11815
11816 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11817 tempflag = (fork_kind == catch_fork_temporary
11818 || fork_kind == catch_vfork_temporary);
c5aa993b 11819
44feb3ce
TT
11820 if (!arg)
11821 arg = "";
e9cafbcc 11822 arg = skip_spaces (arg);
c5aa993b 11823
c906108c 11824 /* The allowed syntax is:
c5aa993b
JM
11825 catch [v]fork
11826 catch [v]fork if <cond>
11827
4a64f543 11828 First, check if there's an if clause. */
c906108c 11829 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11830
c906108c 11831 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11832 error (_("Junk at end of arguments."));
c5aa993b 11833
c906108c 11834 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11835 and enable reporting of such events. */
c5aa993b
JM
11836 switch (fork_kind)
11837 {
44feb3ce
TT
11838 case catch_fork_temporary:
11839 case catch_fork_permanent:
a6d9a66e 11840 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11841 &catch_fork_breakpoint_ops);
c906108c 11842 break;
44feb3ce
TT
11843 case catch_vfork_temporary:
11844 case catch_vfork_permanent:
a6d9a66e 11845 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11846 &catch_vfork_breakpoint_ops);
c906108c 11847 break;
c5aa993b 11848 default:
8a3fe4f8 11849 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11850 break;
c5aa993b 11851 }
c906108c
SS
11852}
11853
11854static void
cc59ec59
MS
11855catch_exec_command_1 (char *arg, int from_tty,
11856 struct cmd_list_element *command)
c906108c 11857{
b4d90040 11858 struct exec_catchpoint *c;
a6d9a66e 11859 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11860 int tempflag;
c5aa993b 11861 char *cond_string = NULL;
c906108c 11862
44feb3ce
TT
11863 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11864
11865 if (!arg)
11866 arg = "";
e9cafbcc 11867 arg = skip_spaces (arg);
c906108c
SS
11868
11869 /* The allowed syntax is:
c5aa993b
JM
11870 catch exec
11871 catch exec if <cond>
c906108c 11872
4a64f543 11873 First, check if there's an if clause. */
c906108c
SS
11874 cond_string = ep_parse_optional_if_clause (&arg);
11875
11876 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11877 error (_("Junk at end of arguments."));
c906108c 11878
b4d90040
PA
11879 c = XNEW (struct exec_catchpoint);
11880 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11881 &catch_exec_breakpoint_ops);
11882 c->exec_pathname = NULL;
11883
3ea46bff 11884 install_breakpoint (0, &c->base, 1);
c906108c 11885}
c5aa993b 11886
9ac4176b 11887void
28010a5d
PA
11888init_ada_exception_breakpoint (struct breakpoint *b,
11889 struct gdbarch *gdbarch,
11890 struct symtab_and_line sal,
11891 char *addr_string,
c0a91b2b 11892 const struct breakpoint_ops *ops,
28010a5d 11893 int tempflag,
349774ef 11894 int enabled,
28010a5d 11895 int from_tty)
f7f9143b 11896{
f7f9143b
JB
11897 if (from_tty)
11898 {
5af949e3
UW
11899 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11900 if (!loc_gdbarch)
11901 loc_gdbarch = gdbarch;
11902
6c95b8df
PA
11903 describe_other_breakpoints (loc_gdbarch,
11904 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11905 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11906 version for exception catchpoints, because two catchpoints
11907 used for different exception names will use the same address.
11908 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11909 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11910 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11911 the user what type of catchpoint it is. The above is good
11912 enough for now, though. */
11913 }
11914
28010a5d 11915 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11916
349774ef 11917 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11918 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11919 b->addr_string = addr_string;
11920 b->language = language_ada;
f7f9143b
JB
11921}
11922
a96d9b2e
SDJ
11923/* Splits the argument using space as delimiter. Returns an xmalloc'd
11924 filter list, or NULL if no filtering is required. */
11925static VEC(int) *
11926catch_syscall_split_args (char *arg)
11927{
11928 VEC(int) *result = NULL;
29d0bb3d 11929 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
11930
11931 while (*arg != '\0')
11932 {
11933 int i, syscall_number;
11934 char *endptr;
11935 char cur_name[128];
11936 struct syscall s;
11937
11938 /* Skip whitespace. */
529480d0 11939 arg = skip_spaces (arg);
a96d9b2e
SDJ
11940
11941 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11942 cur_name[i] = arg[i];
11943 cur_name[i] = '\0';
11944 arg += i;
11945
11946 /* Check if the user provided a syscall name or a number. */
11947 syscall_number = (int) strtol (cur_name, &endptr, 0);
11948 if (*endptr == '\0')
bccd0dd2 11949 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
11950 else
11951 {
11952 /* We have a name. Let's check if it's valid and convert it
11953 to a number. */
11954 get_syscall_by_name (cur_name, &s);
11955
11956 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
11957 /* Here we have to issue an error instead of a warning,
11958 because GDB cannot do anything useful if there's no
11959 syscall number to be caught. */
a96d9b2e
SDJ
11960 error (_("Unknown syscall name '%s'."), cur_name);
11961 }
11962
11963 /* Ok, it's valid. */
11964 VEC_safe_push (int, result, s.number);
11965 }
11966
11967 discard_cleanups (cleanup);
11968 return result;
11969}
11970
11971/* Implement the "catch syscall" command. */
11972
11973static void
cc59ec59
MS
11974catch_syscall_command_1 (char *arg, int from_tty,
11975 struct cmd_list_element *command)
a96d9b2e
SDJ
11976{
11977 int tempflag;
11978 VEC(int) *filter;
11979 struct syscall s;
11980 struct gdbarch *gdbarch = get_current_arch ();
11981
11982 /* Checking if the feature if supported. */
11983 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11984 error (_("The feature 'catch syscall' is not supported on \
ea666128 11985this architecture yet."));
a96d9b2e
SDJ
11986
11987 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11988
e9cafbcc 11989 arg = skip_spaces (arg);
a96d9b2e
SDJ
11990
11991 /* We need to do this first "dummy" translation in order
11992 to get the syscall XML file loaded or, most important,
11993 to display a warning to the user if there's no XML file
11994 for his/her architecture. */
11995 get_syscall_by_number (0, &s);
11996
11997 /* The allowed syntax is:
11998 catch syscall
11999 catch syscall <name | number> [<name | number> ... <name | number>]
12000
12001 Let's check if there's a syscall name. */
12002
12003 if (arg != NULL)
12004 filter = catch_syscall_split_args (arg);
12005 else
12006 filter = NULL;
12007
12008 create_syscall_event_catchpoint (tempflag, filter,
12009 &catch_syscall_breakpoint_ops);
12010}
12011
c906108c 12012static void
fba45db2 12013catch_command (char *arg, int from_tty)
c906108c 12014{
44feb3ce 12015 error (_("Catch requires an event name."));
c906108c
SS
12016}
12017\f
12018
12019static void
fba45db2 12020tcatch_command (char *arg, int from_tty)
c906108c 12021{
44feb3ce 12022 error (_("Catch requires an event name."));
c906108c
SS
12023}
12024
8a2c437b
TT
12025/* A qsort comparison function that sorts breakpoints in order. */
12026
12027static int
12028compare_breakpoints (const void *a, const void *b)
12029{
12030 const breakpoint_p *ba = a;
12031 uintptr_t ua = (uintptr_t) *ba;
12032 const breakpoint_p *bb = b;
12033 uintptr_t ub = (uintptr_t) *bb;
12034
12035 if ((*ba)->number < (*bb)->number)
12036 return -1;
12037 else if ((*ba)->number > (*bb)->number)
12038 return 1;
12039
12040 /* Now sort by address, in case we see, e..g, two breakpoints with
12041 the number 0. */
12042 if (ua < ub)
12043 return -1;
94b0e70d 12044 return ua > ub ? 1 : 0;
8a2c437b
TT
12045}
12046
80f8a6eb 12047/* Delete breakpoints by address or line. */
c906108c
SS
12048
12049static void
fba45db2 12050clear_command (char *arg, int from_tty)
c906108c 12051{
8a2c437b 12052 struct breakpoint *b, *prev;
d6e956e5
VP
12053 VEC(breakpoint_p) *found = 0;
12054 int ix;
c906108c
SS
12055 int default_match;
12056 struct symtabs_and_lines sals;
12057 struct symtab_and_line sal;
c906108c 12058 int i;
8a2c437b 12059 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12060
12061 if (arg)
12062 {
39cf75f7
DE
12063 sals = decode_line_with_current_source (arg,
12064 (DECODE_LINE_FUNFIRSTLINE
12065 | DECODE_LINE_LIST_MODE));
cf4ded82 12066 make_cleanup (xfree, sals.sals);
c906108c
SS
12067 default_match = 0;
12068 }
12069 else
12070 {
c5aa993b 12071 sals.sals = (struct symtab_and_line *)
c906108c 12072 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 12073 make_cleanup (xfree, sals.sals);
4a64f543 12074 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12075
12076 /* Set sal's line, symtab, pc, and pspace to the values
12077 corresponding to the last call to print_frame_info. If the
12078 codepoint is not valid, this will set all the fields to 0. */
12079 get_last_displayed_sal (&sal);
c906108c 12080 if (sal.symtab == 0)
8a3fe4f8 12081 error (_("No source file specified."));
c906108c
SS
12082
12083 sals.sals[0] = sal;
12084 sals.nelts = 1;
12085
12086 default_match = 1;
12087 }
12088
4a64f543
MS
12089 /* We don't call resolve_sal_pc here. That's not as bad as it
12090 seems, because all existing breakpoints typically have both
12091 file/line and pc set. So, if clear is given file/line, we can
12092 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12093
12094 We only support clearing given the address explicitly
12095 present in breakpoint table. Say, we've set breakpoint
4a64f543 12096 at file:line. There were several PC values for that file:line,
ed0616c6 12097 due to optimization, all in one block.
4a64f543
MS
12098
12099 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12100 PC corresponding to the same file:line, the breakpoint won't
12101 be cleared. We probably can still clear the breakpoint, but
12102 since the other PC value is never presented to user, user
12103 can only find it by guessing, and it does not seem important
12104 to support that. */
12105
4a64f543
MS
12106 /* For each line spec given, delete bps which correspond to it. Do
12107 it in two passes, solely to preserve the current behavior that
12108 from_tty is forced true if we delete more than one
12109 breakpoint. */
c906108c 12110
80f8a6eb 12111 found = NULL;
8a2c437b 12112 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12113 for (i = 0; i < sals.nelts; i++)
12114 {
05cba821
JK
12115 const char *sal_fullname;
12116
c906108c 12117 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12118 If line given (pc == 0), clear all bpts on specified line.
12119 If defaulting, clear all bpts on default line
c906108c 12120 or at default pc.
c5aa993b
JM
12121
12122 defaulting sal.pc != 0 tests to do
12123
12124 0 1 pc
12125 1 1 pc _and_ line
12126 0 0 line
12127 1 0 <can't happen> */
c906108c
SS
12128
12129 sal = sals.sals[i];
05cba821
JK
12130 sal_fullname = (sal.symtab == NULL
12131 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12132
4a64f543 12133 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12134 ALL_BREAKPOINTS (b)
c5aa993b 12135 {
0d381245 12136 int match = 0;
4a64f543 12137 /* Are we going to delete b? */
cc60f2e3 12138 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12139 {
12140 struct bp_location *loc = b->loc;
12141 for (; loc; loc = loc->next)
12142 {
f8eba3c6
TT
12143 /* If the user specified file:line, don't allow a PC
12144 match. This matches historical gdb behavior. */
12145 int pc_match = (!sal.explicit_line
12146 && sal.pc
12147 && (loc->pspace == sal.pspace)
12148 && (loc->address == sal.pc)
12149 && (!section_is_overlay (loc->section)
12150 || loc->section == sal.section));
4aac40c8
TT
12151 int line_match = 0;
12152
12153 if ((default_match || sal.explicit_line)
2f202fde 12154 && loc->symtab != NULL
05cba821 12155 && sal_fullname != NULL
4aac40c8 12156 && sal.pspace == loc->pspace
05cba821
JK
12157 && loc->line_number == sal.line
12158 && filename_cmp (symtab_to_fullname (loc->symtab),
12159 sal_fullname) == 0)
12160 line_match = 1;
4aac40c8 12161
0d381245
VP
12162 if (pc_match || line_match)
12163 {
12164 match = 1;
12165 break;
12166 }
12167 }
12168 }
12169
12170 if (match)
d6e956e5 12171 VEC_safe_push(breakpoint_p, found, b);
c906108c 12172 }
80f8a6eb 12173 }
8a2c437b 12174
80f8a6eb 12175 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12176 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12177 {
12178 if (arg)
8a3fe4f8 12179 error (_("No breakpoint at %s."), arg);
80f8a6eb 12180 else
8a3fe4f8 12181 error (_("No breakpoint at this line."));
80f8a6eb 12182 }
c906108c 12183
8a2c437b
TT
12184 /* Remove duplicates from the vec. */
12185 qsort (VEC_address (breakpoint_p, found),
12186 VEC_length (breakpoint_p, found),
12187 sizeof (breakpoint_p),
12188 compare_breakpoints);
12189 prev = VEC_index (breakpoint_p, found, 0);
12190 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12191 {
12192 if (b == prev)
12193 {
12194 VEC_ordered_remove (breakpoint_p, found, ix);
12195 --ix;
12196 }
12197 }
12198
d6e956e5 12199 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12200 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12201 if (from_tty)
a3f17187 12202 {
d6e956e5 12203 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12204 printf_unfiltered (_("Deleted breakpoint "));
12205 else
12206 printf_unfiltered (_("Deleted breakpoints "));
12207 }
d6e956e5
VP
12208
12209 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12210 {
c5aa993b 12211 if (from_tty)
d6e956e5
VP
12212 printf_unfiltered ("%d ", b->number);
12213 delete_breakpoint (b);
c906108c 12214 }
80f8a6eb
MS
12215 if (from_tty)
12216 putchar_unfiltered ('\n');
8a2c437b
TT
12217
12218 do_cleanups (cleanups);
c906108c
SS
12219}
12220\f
12221/* Delete breakpoint in BS if they are `delete' breakpoints and
12222 all breakpoints that are marked for deletion, whether hit or not.
12223 This is called after any breakpoint is hit, or after errors. */
12224
12225void
fba45db2 12226breakpoint_auto_delete (bpstat bs)
c906108c 12227{
35df4500 12228 struct breakpoint *b, *b_tmp;
c906108c
SS
12229
12230 for (; bs; bs = bs->next)
f431efe5
PA
12231 if (bs->breakpoint_at
12232 && bs->breakpoint_at->disposition == disp_del
c906108c 12233 && bs->stop)
f431efe5 12234 delete_breakpoint (bs->breakpoint_at);
c906108c 12235
35df4500 12236 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12237 {
b5de0fa7 12238 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12239 delete_breakpoint (b);
12240 }
c906108c
SS
12241}
12242
4a64f543
MS
12243/* A comparison function for bp_location AP and BP being interfaced to
12244 qsort. Sort elements primarily by their ADDRESS (no matter what
12245 does breakpoint_address_is_meaningful say for its OWNER),
12246 secondarily by ordering first bp_permanent OWNERed elements and
12247 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12248 qsort being an unstable algorithm. */
876fa593
JK
12249
12250static int
494cfb0f 12251bp_location_compare (const void *ap, const void *bp)
876fa593 12252{
494cfb0f
JK
12253 struct bp_location *a = *(void **) ap;
12254 struct bp_location *b = *(void **) bp;
2bdf28a0 12255 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
12256 int a_perm = a->owner->enable_state == bp_permanent;
12257 int b_perm = b->owner->enable_state == bp_permanent;
12258
12259 if (a->address != b->address)
12260 return (a->address > b->address) - (a->address < b->address);
12261
dea2aa5f
LM
12262 /* Sort locations at the same address by their pspace number, keeping
12263 locations of the same inferior (in a multi-inferior environment)
12264 grouped. */
12265
12266 if (a->pspace->num != b->pspace->num)
12267 return ((a->pspace->num > b->pspace->num)
12268 - (a->pspace->num < b->pspace->num));
12269
876fa593
JK
12270 /* Sort permanent breakpoints first. */
12271 if (a_perm != b_perm)
12272 return (a_perm < b_perm) - (a_perm > b_perm);
12273
c56a97f9
JK
12274 /* Make the internal GDB representation stable across GDB runs
12275 where A and B memory inside GDB can differ. Breakpoint locations of
12276 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12277
12278 if (a->owner->number != b->owner->number)
c56a97f9
JK
12279 return ((a->owner->number > b->owner->number)
12280 - (a->owner->number < b->owner->number));
876fa593
JK
12281
12282 return (a > b) - (a < b);
12283}
12284
876fa593 12285/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12286 bp_location_shadow_len_after_address_max according to the current
12287 content of the bp_location array. */
f7545552
TT
12288
12289static void
876fa593 12290bp_location_target_extensions_update (void)
f7545552 12291{
876fa593
JK
12292 struct bp_location *bl, **blp_tmp;
12293
12294 bp_location_placed_address_before_address_max = 0;
12295 bp_location_shadow_len_after_address_max = 0;
12296
12297 ALL_BP_LOCATIONS (bl, blp_tmp)
12298 {
12299 CORE_ADDR start, end, addr;
12300
12301 if (!bp_location_has_shadow (bl))
12302 continue;
12303
12304 start = bl->target_info.placed_address;
12305 end = start + bl->target_info.shadow_len;
12306
12307 gdb_assert (bl->address >= start);
12308 addr = bl->address - start;
12309 if (addr > bp_location_placed_address_before_address_max)
12310 bp_location_placed_address_before_address_max = addr;
12311
12312 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12313
12314 gdb_assert (bl->address < end);
12315 addr = end - bl->address;
12316 if (addr > bp_location_shadow_len_after_address_max)
12317 bp_location_shadow_len_after_address_max = addr;
12318 }
f7545552
TT
12319}
12320
1e4d1764
YQ
12321/* Download tracepoint locations if they haven't been. */
12322
12323static void
12324download_tracepoint_locations (void)
12325{
7ed2c994 12326 struct breakpoint *b;
1e4d1764
YQ
12327 struct cleanup *old_chain;
12328
12329 if (!target_can_download_tracepoint ())
12330 return;
12331
12332 old_chain = save_current_space_and_thread ();
12333
7ed2c994 12334 ALL_TRACEPOINTS (b)
1e4d1764 12335 {
7ed2c994 12336 struct bp_location *bl;
1e4d1764 12337 struct tracepoint *t;
f2a8bc8a 12338 int bp_location_downloaded = 0;
1e4d1764 12339
7ed2c994 12340 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12341 ? !may_insert_fast_tracepoints
12342 : !may_insert_tracepoints))
12343 continue;
12344
7ed2c994
YQ
12345 for (bl = b->loc; bl; bl = bl->next)
12346 {
12347 /* In tracepoint, locations are _never_ duplicated, so
12348 should_be_inserted is equivalent to
12349 unduplicated_should_be_inserted. */
12350 if (!should_be_inserted (bl) || bl->inserted)
12351 continue;
1e4d1764 12352
7ed2c994 12353 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12354
7ed2c994 12355 target_download_tracepoint (bl);
1e4d1764 12356
7ed2c994 12357 bl->inserted = 1;
f2a8bc8a 12358 bp_location_downloaded = 1;
7ed2c994
YQ
12359 }
12360 t = (struct tracepoint *) b;
12361 t->number_on_target = b->number;
f2a8bc8a
YQ
12362 if (bp_location_downloaded)
12363 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12364 }
12365
12366 do_cleanups (old_chain);
12367}
12368
934709f0
PW
12369/* Swap the insertion/duplication state between two locations. */
12370
12371static void
12372swap_insertion (struct bp_location *left, struct bp_location *right)
12373{
12374 const int left_inserted = left->inserted;
12375 const int left_duplicate = left->duplicate;
b775012e 12376 const int left_needs_update = left->needs_update;
934709f0
PW
12377 const struct bp_target_info left_target_info = left->target_info;
12378
1e4d1764
YQ
12379 /* Locations of tracepoints can never be duplicated. */
12380 if (is_tracepoint (left->owner))
12381 gdb_assert (!left->duplicate);
12382 if (is_tracepoint (right->owner))
12383 gdb_assert (!right->duplicate);
12384
934709f0
PW
12385 left->inserted = right->inserted;
12386 left->duplicate = right->duplicate;
b775012e 12387 left->needs_update = right->needs_update;
934709f0
PW
12388 left->target_info = right->target_info;
12389 right->inserted = left_inserted;
12390 right->duplicate = left_duplicate;
b775012e 12391 right->needs_update = left_needs_update;
934709f0
PW
12392 right->target_info = left_target_info;
12393}
12394
b775012e
LM
12395/* Force the re-insertion of the locations at ADDRESS. This is called
12396 once a new/deleted/modified duplicate location is found and we are evaluating
12397 conditions on the target's side. Such conditions need to be updated on
12398 the target. */
12399
12400static void
12401force_breakpoint_reinsertion (struct bp_location *bl)
12402{
12403 struct bp_location **locp = NULL, **loc2p;
12404 struct bp_location *loc;
12405 CORE_ADDR address = 0;
12406 int pspace_num;
12407
12408 address = bl->address;
12409 pspace_num = bl->pspace->num;
12410
12411 /* This is only meaningful if the target is
12412 evaluating conditions and if the user has
12413 opted for condition evaluation on the target's
12414 side. */
12415 if (gdb_evaluates_breakpoint_condition_p ()
12416 || !target_supports_evaluation_of_breakpoint_conditions ())
12417 return;
12418
12419 /* Flag all breakpoint locations with this address and
12420 the same program space as the location
12421 as "its condition has changed". We need to
12422 update the conditions on the target's side. */
12423 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12424 {
12425 loc = *loc2p;
12426
12427 if (!is_breakpoint (loc->owner)
12428 || pspace_num != loc->pspace->num)
12429 continue;
12430
12431 /* Flag the location appropriately. We use a different state to
12432 let everyone know that we already updated the set of locations
12433 with addr bl->address and program space bl->pspace. This is so
12434 we don't have to keep calling these functions just to mark locations
12435 that have already been marked. */
12436 loc->condition_changed = condition_updated;
12437
12438 /* Free the agent expression bytecode as well. We will compute
12439 it later on. */
12440 if (loc->cond_bytecode)
12441 {
12442 free_agent_expr (loc->cond_bytecode);
12443 loc->cond_bytecode = NULL;
12444 }
12445 }
12446}
12447
4cd9bd08 12448/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
12449 into the inferior, only remove already-inserted locations that no
12450 longer should be inserted. Functions that delete a breakpoint or
12451 breakpoints should pass false, so that deleting a breakpoint
12452 doesn't have the side effect of inserting the locations of other
12453 breakpoints that are marked not-inserted, but should_be_inserted
12454 returns true on them.
12455
12456 This behaviour is useful is situations close to tear-down -- e.g.,
12457 after an exec, while the target still has execution, but breakpoint
12458 shadows of the previous executable image should *NOT* be restored
12459 to the new image; or before detaching, where the target still has
12460 execution and wants to delete breakpoints from GDB's lists, and all
12461 breakpoints had already been removed from the inferior. */
12462
0d381245 12463static void
b60e7edf 12464update_global_location_list (int should_insert)
0d381245 12465{
74960c60 12466 struct breakpoint *b;
876fa593 12467 struct bp_location **locp, *loc;
f7545552 12468 struct cleanup *cleanups;
b775012e
LM
12469 /* Last breakpoint location address that was marked for update. */
12470 CORE_ADDR last_addr = 0;
12471 /* Last breakpoint location program space that was marked for update. */
12472 int last_pspace_num = -1;
f7545552 12473
2d134ed3
PA
12474 /* Used in the duplicates detection below. When iterating over all
12475 bp_locations, points to the first bp_location of a given address.
12476 Breakpoints and watchpoints of different types are never
12477 duplicates of each other. Keep one pointer for each type of
12478 breakpoint/watchpoint, so we only need to loop over all locations
12479 once. */
12480 struct bp_location *bp_loc_first; /* breakpoint */
12481 struct bp_location *wp_loc_first; /* hardware watchpoint */
12482 struct bp_location *awp_loc_first; /* access watchpoint */
12483 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12484
4a64f543
MS
12485 /* Saved former bp_location array which we compare against the newly
12486 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12487 struct bp_location **old_location, **old_locp;
12488 unsigned old_location_count;
12489
12490 old_location = bp_location;
12491 old_location_count = bp_location_count;
12492 bp_location = NULL;
12493 bp_location_count = 0;
12494 cleanups = make_cleanup (xfree, old_location);
0d381245 12495
74960c60 12496 ALL_BREAKPOINTS (b)
876fa593
JK
12497 for (loc = b->loc; loc; loc = loc->next)
12498 bp_location_count++;
12499
12500 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12501 locp = bp_location;
12502 ALL_BREAKPOINTS (b)
12503 for (loc = b->loc; loc; loc = loc->next)
12504 *locp++ = loc;
12505 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12506 bp_location_compare);
876fa593
JK
12507
12508 bp_location_target_extensions_update ();
74960c60 12509
4a64f543
MS
12510 /* Identify bp_location instances that are no longer present in the
12511 new list, and therefore should be freed. Note that it's not
12512 necessary that those locations should be removed from inferior --
12513 if there's another location at the same address (previously
12514 marked as duplicate), we don't need to remove/insert the
12515 location.
876fa593 12516
4a64f543
MS
12517 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12518 and former bp_location array state respectively. */
876fa593
JK
12519
12520 locp = bp_location;
12521 for (old_locp = old_location; old_locp < old_location + old_location_count;
12522 old_locp++)
74960c60 12523 {
876fa593 12524 struct bp_location *old_loc = *old_locp;
c7d46a38 12525 struct bp_location **loc2p;
876fa593 12526
e5dd4106 12527 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12528 not, we have to free it. */
c7d46a38 12529 int found_object = 0;
20874c92
VP
12530 /* Tells if the location should remain inserted in the target. */
12531 int keep_in_target = 0;
12532 int removed = 0;
876fa593 12533
4a64f543
MS
12534 /* Skip LOCP entries which will definitely never be needed.
12535 Stop either at or being the one matching OLD_LOC. */
876fa593 12536 while (locp < bp_location + bp_location_count
c7d46a38 12537 && (*locp)->address < old_loc->address)
876fa593 12538 locp++;
c7d46a38
PA
12539
12540 for (loc2p = locp;
12541 (loc2p < bp_location + bp_location_count
12542 && (*loc2p)->address == old_loc->address);
12543 loc2p++)
12544 {
b775012e
LM
12545 /* Check if this is a new/duplicated location or a duplicated
12546 location that had its condition modified. If so, we want to send
12547 its condition to the target if evaluation of conditions is taking
12548 place there. */
12549 if ((*loc2p)->condition_changed == condition_modified
12550 && (last_addr != old_loc->address
12551 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12552 {
b775012e
LM
12553 force_breakpoint_reinsertion (*loc2p);
12554 last_pspace_num = old_loc->pspace->num;
c7d46a38 12555 }
b775012e
LM
12556
12557 if (*loc2p == old_loc)
12558 found_object = 1;
c7d46a38 12559 }
74960c60 12560
b775012e
LM
12561 /* We have already handled this address, update it so that we don't
12562 have to go through updates again. */
12563 last_addr = old_loc->address;
12564
12565 /* Target-side condition evaluation: Handle deleted locations. */
12566 if (!found_object)
12567 force_breakpoint_reinsertion (old_loc);
12568
4a64f543
MS
12569 /* If this location is no longer present, and inserted, look if
12570 there's maybe a new location at the same address. If so,
12571 mark that one inserted, and don't remove this one. This is
12572 needed so that we don't have a time window where a breakpoint
12573 at certain location is not inserted. */
74960c60 12574
876fa593 12575 if (old_loc->inserted)
0d381245 12576 {
4a64f543
MS
12577 /* If the location is inserted now, we might have to remove
12578 it. */
74960c60 12579
876fa593 12580 if (found_object && should_be_inserted (old_loc))
74960c60 12581 {
4a64f543
MS
12582 /* The location is still present in the location list,
12583 and still should be inserted. Don't do anything. */
20874c92 12584 keep_in_target = 1;
74960c60
VP
12585 }
12586 else
12587 {
b775012e
LM
12588 /* This location still exists, but it won't be kept in the
12589 target since it may have been disabled. We proceed to
12590 remove its target-side condition. */
12591
4a64f543
MS
12592 /* The location is either no longer present, or got
12593 disabled. See if there's another location at the
12594 same address, in which case we don't need to remove
12595 this one from the target. */
876fa593 12596
2bdf28a0 12597 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12598 if (breakpoint_address_is_meaningful (old_loc->owner))
12599 {
876fa593 12600 for (loc2p = locp;
c7d46a38
PA
12601 (loc2p < bp_location + bp_location_count
12602 && (*loc2p)->address == old_loc->address);
876fa593
JK
12603 loc2p++)
12604 {
12605 struct bp_location *loc2 = *loc2p;
12606
2d134ed3 12607 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12608 {
85d721b8
PA
12609 /* Read watchpoint locations are switched to
12610 access watchpoints, if the former are not
12611 supported, but the latter are. */
12612 if (is_hardware_watchpoint (old_loc->owner))
12613 {
12614 gdb_assert (is_hardware_watchpoint (loc2->owner));
12615 loc2->watchpoint_type = old_loc->watchpoint_type;
12616 }
12617
934709f0
PW
12618 /* loc2 is a duplicated location. We need to check
12619 if it should be inserted in case it will be
12620 unduplicated. */
12621 if (loc2 != old_loc
12622 && unduplicated_should_be_inserted (loc2))
c7d46a38 12623 {
934709f0 12624 swap_insertion (old_loc, loc2);
c7d46a38
PA
12625 keep_in_target = 1;
12626 break;
12627 }
876fa593
JK
12628 }
12629 }
12630 }
74960c60
VP
12631 }
12632
20874c92
VP
12633 if (!keep_in_target)
12634 {
876fa593 12635 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12636 {
4a64f543
MS
12637 /* This is just about all we can do. We could keep
12638 this location on the global list, and try to
12639 remove it next time, but there's no particular
12640 reason why we will succeed next time.
20874c92 12641
4a64f543
MS
12642 Note that at this point, old_loc->owner is still
12643 valid, as delete_breakpoint frees the breakpoint
12644 only after calling us. */
3e43a32a
MS
12645 printf_filtered (_("warning: Error removing "
12646 "breakpoint %d\n"),
876fa593 12647 old_loc->owner->number);
20874c92
VP
12648 }
12649 removed = 1;
12650 }
0d381245 12651 }
74960c60
VP
12652
12653 if (!found_object)
1c5cfe86 12654 {
db82e815
PA
12655 if (removed && non_stop
12656 && breakpoint_address_is_meaningful (old_loc->owner)
12657 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12658 {
db82e815
PA
12659 /* This location was removed from the target. In
12660 non-stop mode, a race condition is possible where
12661 we've removed a breakpoint, but stop events for that
12662 breakpoint are already queued and will arrive later.
12663 We apply an heuristic to be able to distinguish such
12664 SIGTRAPs from other random SIGTRAPs: we keep this
12665 breakpoint location for a bit, and will retire it
12666 after we see some number of events. The theory here
12667 is that reporting of events should, "on the average",
12668 be fair, so after a while we'll see events from all
12669 threads that have anything of interest, and no longer
12670 need to keep this breakpoint location around. We
12671 don't hold locations forever so to reduce chances of
12672 mistaking a non-breakpoint SIGTRAP for a breakpoint
12673 SIGTRAP.
12674
12675 The heuristic failing can be disastrous on
12676 decr_pc_after_break targets.
12677
12678 On decr_pc_after_break targets, like e.g., x86-linux,
12679 if we fail to recognize a late breakpoint SIGTRAP,
12680 because events_till_retirement has reached 0 too
12681 soon, we'll fail to do the PC adjustment, and report
12682 a random SIGTRAP to the user. When the user resumes
12683 the inferior, it will most likely immediately crash
2dec564e 12684 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12685 corrupted, because of being resumed e.g., in the
12686 middle of a multi-byte instruction, or skipped a
12687 one-byte instruction. This was actually seen happen
12688 on native x86-linux, and should be less rare on
12689 targets that do not support new thread events, like
12690 remote, due to the heuristic depending on
12691 thread_count.
12692
12693 Mistaking a random SIGTRAP for a breakpoint trap
12694 causes similar symptoms (PC adjustment applied when
12695 it shouldn't), but then again, playing with SIGTRAPs
12696 behind the debugger's back is asking for trouble.
12697
12698 Since hardware watchpoint traps are always
12699 distinguishable from other traps, so we don't need to
12700 apply keep hardware watchpoint moribund locations
12701 around. We simply always ignore hardware watchpoint
12702 traps we can no longer explain. */
12703
876fa593
JK
12704 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12705 old_loc->owner = NULL;
20874c92 12706
876fa593 12707 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12708 }
12709 else
f431efe5
PA
12710 {
12711 old_loc->owner = NULL;
12712 decref_bp_location (&old_loc);
12713 }
20874c92 12714 }
74960c60 12715 }
1c5cfe86 12716
348d480f
PA
12717 /* Rescan breakpoints at the same address and section, marking the
12718 first one as "first" and any others as "duplicates". This is so
12719 that the bpt instruction is only inserted once. If we have a
12720 permanent breakpoint at the same place as BPT, make that one the
12721 official one, and the rest as duplicates. Permanent breakpoints
12722 are sorted first for the same address.
12723
12724 Do the same for hardware watchpoints, but also considering the
12725 watchpoint's type (regular/access/read) and length. */
12726
12727 bp_loc_first = NULL;
12728 wp_loc_first = NULL;
12729 awp_loc_first = NULL;
12730 rwp_loc_first = NULL;
12731 ALL_BP_LOCATIONS (loc, locp)
12732 {
12733 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12734 non-NULL. */
348d480f 12735 struct bp_location **loc_first_p;
d3fbdd86 12736 b = loc->owner;
348d480f 12737
6f380991 12738 if (!unduplicated_should_be_inserted (loc)
348d480f 12739 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12740 /* Don't detect duplicate for tracepoint locations because they are
12741 never duplicated. See the comments in field `duplicate' of
12742 `struct bp_location'. */
348d480f 12743 || is_tracepoint (b))
b775012e
LM
12744 {
12745 /* Clear the condition modification flag. */
12746 loc->condition_changed = condition_unchanged;
12747 continue;
12748 }
348d480f
PA
12749
12750 /* Permanent breakpoint should always be inserted. */
12751 if (b->enable_state == bp_permanent && ! loc->inserted)
12752 internal_error (__FILE__, __LINE__,
12753 _("allegedly permanent breakpoint is not "
12754 "actually inserted"));
12755
12756 if (b->type == bp_hardware_watchpoint)
12757 loc_first_p = &wp_loc_first;
12758 else if (b->type == bp_read_watchpoint)
12759 loc_first_p = &rwp_loc_first;
12760 else if (b->type == bp_access_watchpoint)
12761 loc_first_p = &awp_loc_first;
12762 else
12763 loc_first_p = &bp_loc_first;
12764
12765 if (*loc_first_p == NULL
12766 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12767 || !breakpoint_locations_match (loc, *loc_first_p))
12768 {
12769 *loc_first_p = loc;
12770 loc->duplicate = 0;
b775012e
LM
12771
12772 if (is_breakpoint (loc->owner) && loc->condition_changed)
12773 {
12774 loc->needs_update = 1;
12775 /* Clear the condition modification flag. */
12776 loc->condition_changed = condition_unchanged;
12777 }
348d480f
PA
12778 continue;
12779 }
12780
934709f0
PW
12781
12782 /* This and the above ensure the invariant that the first location
12783 is not duplicated, and is the inserted one.
12784 All following are marked as duplicated, and are not inserted. */
12785 if (loc->inserted)
12786 swap_insertion (loc, *loc_first_p);
348d480f
PA
12787 loc->duplicate = 1;
12788
b775012e
LM
12789 /* Clear the condition modification flag. */
12790 loc->condition_changed = condition_unchanged;
12791
348d480f
PA
12792 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12793 && b->enable_state != bp_permanent)
12794 internal_error (__FILE__, __LINE__,
12795 _("another breakpoint was inserted on top of "
12796 "a permanent breakpoint"));
12797 }
12798
b775012e 12799 if (breakpoints_always_inserted_mode ()
348d480f 12800 && (have_live_inferiors ()
f5656ead 12801 || (gdbarch_has_global_breakpoints (target_gdbarch ()))))
b775012e
LM
12802 {
12803 if (should_insert)
12804 insert_breakpoint_locations ();
12805 else
12806 {
12807 /* Though should_insert is false, we may need to update conditions
12808 on the target's side if it is evaluating such conditions. We
12809 only update conditions for locations that are marked
12810 "needs_update". */
12811 update_inserted_breakpoint_locations ();
12812 }
12813 }
348d480f 12814
1e4d1764
YQ
12815 if (should_insert)
12816 download_tracepoint_locations ();
12817
348d480f
PA
12818 do_cleanups (cleanups);
12819}
12820
12821void
12822breakpoint_retire_moribund (void)
12823{
12824 struct bp_location *loc;
12825 int ix;
12826
12827 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12828 if (--(loc->events_till_retirement) == 0)
12829 {
12830 decref_bp_location (&loc);
12831 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12832 --ix;
12833 }
12834}
12835
12836static void
12837update_global_location_list_nothrow (int inserting)
12838{
bfd189b1 12839 volatile struct gdb_exception e;
348d480f
PA
12840
12841 TRY_CATCH (e, RETURN_MASK_ERROR)
12842 update_global_location_list (inserting);
12843}
12844
12845/* Clear BKP from a BPS. */
12846
12847static void
12848bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12849{
12850 bpstat bs;
12851
12852 for (bs = bps; bs; bs = bs->next)
12853 if (bs->breakpoint_at == bpt)
12854 {
12855 bs->breakpoint_at = NULL;
12856 bs->old_val = NULL;
12857 /* bs->commands will be freed later. */
12858 }
12859}
12860
12861/* Callback for iterate_over_threads. */
12862static int
12863bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12864{
12865 struct breakpoint *bpt = data;
12866
12867 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12868 return 0;
12869}
12870
12871/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12872 callbacks. */
12873
12874static void
12875say_where (struct breakpoint *b)
12876{
12877 struct value_print_options opts;
12878
12879 get_user_print_options (&opts);
12880
12881 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12882 single string. */
12883 if (b->loc == NULL)
12884 {
12885 printf_filtered (_(" (%s) pending."), b->addr_string);
12886 }
12887 else
12888 {
2f202fde 12889 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12890 {
12891 printf_filtered (" at ");
12892 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12893 gdb_stdout);
12894 }
2f202fde 12895 if (b->loc->symtab != NULL)
f8eba3c6
TT
12896 {
12897 /* If there is a single location, we can print the location
12898 more nicely. */
12899 if (b->loc->next == NULL)
12900 printf_filtered (": file %s, line %d.",
05cba821
JK
12901 symtab_to_filename_for_display (b->loc->symtab),
12902 b->loc->line_number);
f8eba3c6
TT
12903 else
12904 /* This is not ideal, but each location may have a
12905 different file name, and this at least reflects the
12906 real situation somewhat. */
12907 printf_filtered (": %s.", b->addr_string);
12908 }
348d480f
PA
12909
12910 if (b->loc->next)
12911 {
12912 struct bp_location *loc = b->loc;
12913 int n = 0;
12914 for (; loc; loc = loc->next)
12915 ++n;
12916 printf_filtered (" (%d locations)", n);
12917 }
12918 }
12919}
12920
348d480f
PA
12921/* Default bp_location_ops methods. */
12922
12923static void
12924bp_location_dtor (struct bp_location *self)
12925{
12926 xfree (self->cond);
b775012e
LM
12927 if (self->cond_bytecode)
12928 free_agent_expr (self->cond_bytecode);
348d480f 12929 xfree (self->function_name);
8b4f3082
PA
12930
12931 VEC_free (agent_expr_p, self->target_info.conditions);
12932 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12933}
12934
12935static const struct bp_location_ops bp_location_ops =
12936{
12937 bp_location_dtor
12938};
12939
2060206e
PA
12940/* Default breakpoint_ops methods all breakpoint_ops ultimately
12941 inherit from. */
348d480f 12942
2060206e
PA
12943static void
12944base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12945{
12946 decref_counted_command_line (&self->commands);
12947 xfree (self->cond_string);
fb81d016 12948 xfree (self->extra_string);
348d480f 12949 xfree (self->addr_string);
f8eba3c6 12950 xfree (self->filter);
348d480f 12951 xfree (self->addr_string_range_end);
348d480f
PA
12952}
12953
2060206e
PA
12954static struct bp_location *
12955base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12956{
12957 struct bp_location *loc;
12958
12959 loc = XNEW (struct bp_location);
12960 init_bp_location (loc, &bp_location_ops, self);
12961 return loc;
12962}
12963
2060206e
PA
12964static void
12965base_breakpoint_re_set (struct breakpoint *b)
12966{
12967 /* Nothing to re-set. */
12968}
12969
12970#define internal_error_pure_virtual_called() \
12971 gdb_assert_not_reached ("pure virtual function called")
12972
12973static int
12974base_breakpoint_insert_location (struct bp_location *bl)
12975{
12976 internal_error_pure_virtual_called ();
12977}
12978
12979static int
12980base_breakpoint_remove_location (struct bp_location *bl)
12981{
12982 internal_error_pure_virtual_called ();
12983}
12984
12985static int
12986base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12987 struct address_space *aspace,
09ac7c10
TT
12988 CORE_ADDR bp_addr,
12989 const struct target_waitstatus *ws)
2060206e
PA
12990{
12991 internal_error_pure_virtual_called ();
12992}
12993
12994static void
12995base_breakpoint_check_status (bpstat bs)
12996{
12997 /* Always stop. */
12998}
12999
13000/* A "works_in_software_mode" breakpoint_ops method that just internal
13001 errors. */
13002
13003static int
13004base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13005{
13006 internal_error_pure_virtual_called ();
13007}
13008
13009/* A "resources_needed" breakpoint_ops method that just internal
13010 errors. */
13011
13012static int
13013base_breakpoint_resources_needed (const struct bp_location *bl)
13014{
13015 internal_error_pure_virtual_called ();
13016}
13017
13018static enum print_stop_action
13019base_breakpoint_print_it (bpstat bs)
13020{
13021 internal_error_pure_virtual_called ();
13022}
13023
13024static void
13025base_breakpoint_print_one_detail (const struct breakpoint *self,
13026 struct ui_out *uiout)
13027{
13028 /* nothing */
13029}
13030
13031static void
13032base_breakpoint_print_mention (struct breakpoint *b)
13033{
13034 internal_error_pure_virtual_called ();
13035}
13036
13037static void
13038base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13039{
13040 internal_error_pure_virtual_called ();
13041}
13042
983af33b
SDJ
13043static void
13044base_breakpoint_create_sals_from_address (char **arg,
13045 struct linespec_result *canonical,
13046 enum bptype type_wanted,
13047 char *addr_start,
13048 char **copy_arg)
13049{
13050 internal_error_pure_virtual_called ();
13051}
13052
13053static void
13054base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13055 struct linespec_result *c,
983af33b 13056 char *cond_string,
e7e0cddf 13057 char *extra_string,
983af33b
SDJ
13058 enum bptype type_wanted,
13059 enum bpdisp disposition,
13060 int thread,
13061 int task, int ignore_count,
13062 const struct breakpoint_ops *o,
13063 int from_tty, int enabled,
44f238bb 13064 int internal, unsigned flags)
983af33b
SDJ
13065{
13066 internal_error_pure_virtual_called ();
13067}
13068
13069static void
13070base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13071 struct symtabs_and_lines *sals)
13072{
13073 internal_error_pure_virtual_called ();
13074}
13075
ab04a2af
TT
13076/* The default 'explains_signal' method. */
13077
47591c29 13078static int
427cd150 13079base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13080{
47591c29 13081 return 1;
ab04a2af
TT
13082}
13083
9d6e6e84
HZ
13084/* The default "after_condition_true" method. */
13085
13086static void
13087base_breakpoint_after_condition_true (struct bpstats *bs)
13088{
13089 /* Nothing to do. */
13090}
13091
ab04a2af 13092struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13093{
13094 base_breakpoint_dtor,
13095 base_breakpoint_allocate_location,
13096 base_breakpoint_re_set,
13097 base_breakpoint_insert_location,
13098 base_breakpoint_remove_location,
13099 base_breakpoint_breakpoint_hit,
13100 base_breakpoint_check_status,
13101 base_breakpoint_resources_needed,
13102 base_breakpoint_works_in_software_mode,
13103 base_breakpoint_print_it,
13104 NULL,
13105 base_breakpoint_print_one_detail,
13106 base_breakpoint_print_mention,
983af33b
SDJ
13107 base_breakpoint_print_recreate,
13108 base_breakpoint_create_sals_from_address,
13109 base_breakpoint_create_breakpoints_sal,
13110 base_breakpoint_decode_linespec,
9d6e6e84
HZ
13111 base_breakpoint_explains_signal,
13112 base_breakpoint_after_condition_true,
2060206e
PA
13113};
13114
13115/* Default breakpoint_ops methods. */
13116
13117static void
348d480f
PA
13118bkpt_re_set (struct breakpoint *b)
13119{
06edf0c0
PA
13120 /* FIXME: is this still reachable? */
13121 if (b->addr_string == NULL)
13122 {
13123 /* Anything without a string can't be re-set. */
348d480f 13124 delete_breakpoint (b);
06edf0c0 13125 return;
348d480f 13126 }
06edf0c0
PA
13127
13128 breakpoint_re_set_default (b);
348d480f
PA
13129}
13130
ef370185
JB
13131/* Copy SRC's shadow buffer and whatever else we'd set if we actually
13132 inserted DEST, so we can remove it later, in case SRC is removed
13133 first. */
13134
13135static void
13136bp_target_info_copy_insertion_state (struct bp_target_info *dest,
13137 const struct bp_target_info *src)
13138{
13139 dest->shadow_len = src->shadow_len;
13140 memcpy (dest->shadow_contents, src->shadow_contents, src->shadow_len);
13141 dest->placed_size = src->placed_size;
13142}
13143
2060206e 13144static int
348d480f
PA
13145bkpt_insert_location (struct bp_location *bl)
13146{
13147 if (bl->loc_type == bp_loc_hardware_breakpoint)
13148 return target_insert_hw_breakpoint (bl->gdbarch,
13149 &bl->target_info);
13150 else
ef370185
JB
13151 {
13152 struct bp_target_info *bp_tgt = &bl->target_info;
13153 int ret;
13154 int sss_slot;
13155
13156 /* There is no need to insert a breakpoint if an unconditional
13157 raw/sss breakpoint is already inserted at that location. */
13158 sss_slot = find_single_step_breakpoint (bp_tgt->placed_address_space,
13159 bp_tgt->placed_address);
13160 if (sss_slot >= 0)
13161 {
13162 struct bp_target_info *sss_bp_tgt = single_step_breakpoints[sss_slot];
13163
13164 bp_target_info_copy_insertion_state (bp_tgt, sss_bp_tgt);
13165 return 0;
13166 }
13167
13168 return target_insert_breakpoint (bl->gdbarch, bp_tgt);
13169 }
348d480f
PA
13170}
13171
2060206e 13172static int
348d480f
PA
13173bkpt_remove_location (struct bp_location *bl)
13174{
13175 if (bl->loc_type == bp_loc_hardware_breakpoint)
13176 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13177 else
ef370185
JB
13178 {
13179 struct bp_target_info *bp_tgt = &bl->target_info;
13180 struct address_space *aspace = bp_tgt->placed_address_space;
13181 CORE_ADDR address = bp_tgt->placed_address;
13182
13183 /* Only remove the breakpoint if there is no raw/sss breakpoint
13184 still inserted at this location. Otherwise, we would be
13185 effectively disabling the raw/sss breakpoint. */
13186 if (single_step_breakpoint_inserted_here_p (aspace, address))
13187 return 0;
13188
13189 return target_remove_breakpoint (bl->gdbarch, bp_tgt);
13190 }
348d480f
PA
13191}
13192
2060206e 13193static int
348d480f 13194bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13195 struct address_space *aspace, CORE_ADDR bp_addr,
13196 const struct target_waitstatus *ws)
348d480f 13197{
09ac7c10 13198 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13199 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13200 return 0;
13201
348d480f
PA
13202 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13203 aspace, bp_addr))
13204 return 0;
13205
13206 if (overlay_debugging /* unmapped overlay section */
13207 && section_is_overlay (bl->section)
13208 && !section_is_mapped (bl->section))
13209 return 0;
13210
13211 return 1;
13212}
13213
cd1608cc
PA
13214static int
13215dprintf_breakpoint_hit (const struct bp_location *bl,
13216 struct address_space *aspace, CORE_ADDR bp_addr,
13217 const struct target_waitstatus *ws)
13218{
13219 if (dprintf_style == dprintf_style_agent
13220 && target_can_run_breakpoint_commands ())
13221 {
13222 /* An agent-style dprintf never causes a stop. If we see a trap
13223 for this address it must be for a breakpoint that happens to
13224 be set at the same address. */
13225 return 0;
13226 }
13227
13228 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13229}
13230
2060206e 13231static int
348d480f
PA
13232bkpt_resources_needed (const struct bp_location *bl)
13233{
13234 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13235
13236 return 1;
13237}
13238
2060206e 13239static enum print_stop_action
348d480f
PA
13240bkpt_print_it (bpstat bs)
13241{
348d480f
PA
13242 struct breakpoint *b;
13243 const struct bp_location *bl;
001c8c33 13244 int bp_temp;
79a45e25 13245 struct ui_out *uiout = current_uiout;
348d480f
PA
13246
13247 gdb_assert (bs->bp_location_at != NULL);
13248
13249 bl = bs->bp_location_at;
13250 b = bs->breakpoint_at;
13251
001c8c33
PA
13252 bp_temp = b->disposition == disp_del;
13253 if (bl->address != bl->requested_address)
13254 breakpoint_adjustment_warning (bl->requested_address,
13255 bl->address,
13256 b->number, 1);
13257 annotate_breakpoint (b->number);
13258 if (bp_temp)
13259 ui_out_text (uiout, "\nTemporary breakpoint ");
13260 else
13261 ui_out_text (uiout, "\nBreakpoint ");
13262 if (ui_out_is_mi_like_p (uiout))
348d480f 13263 {
001c8c33
PA
13264 ui_out_field_string (uiout, "reason",
13265 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13266 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13267 }
001c8c33
PA
13268 ui_out_field_int (uiout, "bkptno", b->number);
13269 ui_out_text (uiout, ", ");
06edf0c0 13270
001c8c33 13271 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13272}
13273
2060206e 13274static void
06edf0c0
PA
13275bkpt_print_mention (struct breakpoint *b)
13276{
79a45e25 13277 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13278 return;
13279
13280 switch (b->type)
13281 {
13282 case bp_breakpoint:
13283 case bp_gnu_ifunc_resolver:
13284 if (b->disposition == disp_del)
13285 printf_filtered (_("Temporary breakpoint"));
13286 else
13287 printf_filtered (_("Breakpoint"));
13288 printf_filtered (_(" %d"), b->number);
13289 if (b->type == bp_gnu_ifunc_resolver)
13290 printf_filtered (_(" at gnu-indirect-function resolver"));
13291 break;
13292 case bp_hardware_breakpoint:
13293 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13294 break;
e7e0cddf
SS
13295 case bp_dprintf:
13296 printf_filtered (_("Dprintf %d"), b->number);
13297 break;
06edf0c0
PA
13298 }
13299
13300 say_where (b);
13301}
13302
2060206e 13303static void
06edf0c0
PA
13304bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13305{
13306 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13307 fprintf_unfiltered (fp, "tbreak");
13308 else if (tp->type == bp_breakpoint)
13309 fprintf_unfiltered (fp, "break");
13310 else if (tp->type == bp_hardware_breakpoint
13311 && tp->disposition == disp_del)
13312 fprintf_unfiltered (fp, "thbreak");
13313 else if (tp->type == bp_hardware_breakpoint)
13314 fprintf_unfiltered (fp, "hbreak");
13315 else
13316 internal_error (__FILE__, __LINE__,
13317 _("unhandled breakpoint type %d"), (int) tp->type);
13318
2060206e 13319 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13320 print_recreate_thread (tp, fp);
06edf0c0
PA
13321}
13322
983af33b
SDJ
13323static void
13324bkpt_create_sals_from_address (char **arg,
13325 struct linespec_result *canonical,
13326 enum bptype type_wanted,
13327 char *addr_start, char **copy_arg)
13328{
13329 create_sals_from_address_default (arg, canonical, type_wanted,
13330 addr_start, copy_arg);
13331}
13332
13333static void
13334bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13335 struct linespec_result *canonical,
983af33b 13336 char *cond_string,
e7e0cddf 13337 char *extra_string,
983af33b
SDJ
13338 enum bptype type_wanted,
13339 enum bpdisp disposition,
13340 int thread,
13341 int task, int ignore_count,
13342 const struct breakpoint_ops *ops,
13343 int from_tty, int enabled,
44f238bb 13344 int internal, unsigned flags)
983af33b 13345{
023fa29b 13346 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13347 cond_string, extra_string,
13348 type_wanted,
983af33b
SDJ
13349 disposition, thread, task,
13350 ignore_count, ops, from_tty,
44f238bb 13351 enabled, internal, flags);
983af33b
SDJ
13352}
13353
13354static void
13355bkpt_decode_linespec (struct breakpoint *b, char **s,
13356 struct symtabs_and_lines *sals)
13357{
13358 decode_linespec_default (b, s, sals);
13359}
13360
06edf0c0
PA
13361/* Virtual table for internal breakpoints. */
13362
13363static void
13364internal_bkpt_re_set (struct breakpoint *b)
13365{
13366 switch (b->type)
13367 {
13368 /* Delete overlay event and longjmp master breakpoints; they
13369 will be reset later by breakpoint_re_set. */
13370 case bp_overlay_event:
13371 case bp_longjmp_master:
13372 case bp_std_terminate_master:
13373 case bp_exception_master:
13374 delete_breakpoint (b);
13375 break;
13376
13377 /* This breakpoint is special, it's set up when the inferior
13378 starts and we really don't want to touch it. */
13379 case bp_shlib_event:
13380
13381 /* Like bp_shlib_event, this breakpoint type is special. Once
13382 it is set up, we do not want to touch it. */
13383 case bp_thread_event:
13384 break;
13385 }
13386}
13387
13388static void
13389internal_bkpt_check_status (bpstat bs)
13390{
a9b3a50f
PA
13391 if (bs->breakpoint_at->type == bp_shlib_event)
13392 {
13393 /* If requested, stop when the dynamic linker notifies GDB of
13394 events. This allows the user to get control and place
13395 breakpoints in initializer routines for dynamically loaded
13396 objects (among other things). */
13397 bs->stop = stop_on_solib_events;
13398 bs->print = stop_on_solib_events;
13399 }
13400 else
13401 bs->stop = 0;
06edf0c0
PA
13402}
13403
13404static enum print_stop_action
13405internal_bkpt_print_it (bpstat bs)
13406{
06edf0c0 13407 struct breakpoint *b;
06edf0c0 13408
06edf0c0
PA
13409 b = bs->breakpoint_at;
13410
06edf0c0
PA
13411 switch (b->type)
13412 {
348d480f
PA
13413 case bp_shlib_event:
13414 /* Did we stop because the user set the stop_on_solib_events
13415 variable? (If so, we report this as a generic, "Stopped due
13416 to shlib event" message.) */
edcc5120 13417 print_solib_event (0);
348d480f
PA
13418 break;
13419
13420 case bp_thread_event:
13421 /* Not sure how we will get here.
13422 GDB should not stop for these breakpoints. */
13423 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13424 break;
13425
13426 case bp_overlay_event:
13427 /* By analogy with the thread event, GDB should not stop for these. */
13428 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13429 break;
13430
13431 case bp_longjmp_master:
13432 /* These should never be enabled. */
13433 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13434 break;
13435
13436 case bp_std_terminate_master:
13437 /* These should never be enabled. */
13438 printf_filtered (_("std::terminate Master Breakpoint: "
13439 "gdb should not stop!\n"));
348d480f
PA
13440 break;
13441
13442 case bp_exception_master:
13443 /* These should never be enabled. */
13444 printf_filtered (_("Exception Master Breakpoint: "
13445 "gdb should not stop!\n"));
06edf0c0
PA
13446 break;
13447 }
13448
001c8c33 13449 return PRINT_NOTHING;
06edf0c0
PA
13450}
13451
13452static void
13453internal_bkpt_print_mention (struct breakpoint *b)
13454{
13455 /* Nothing to mention. These breakpoints are internal. */
13456}
13457
06edf0c0
PA
13458/* Virtual table for momentary breakpoints */
13459
13460static void
13461momentary_bkpt_re_set (struct breakpoint *b)
13462{
13463 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13464 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13465 Otherwise these should have been blown away via the cleanup chain
13466 or by breakpoint_init_inferior when we rerun the executable. */
13467}
13468
13469static void
13470momentary_bkpt_check_status (bpstat bs)
13471{
13472 /* Nothing. The point of these breakpoints is causing a stop. */
13473}
13474
13475static enum print_stop_action
13476momentary_bkpt_print_it (bpstat bs)
13477{
79a45e25
PA
13478 struct ui_out *uiout = current_uiout;
13479
001c8c33 13480 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13481 {
001c8c33 13482 struct breakpoint *b = bs->breakpoint_at;
348d480f 13483
001c8c33
PA
13484 switch (b->type)
13485 {
13486 case bp_finish:
13487 ui_out_field_string
13488 (uiout, "reason",
13489 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13490 break;
348d480f 13491
001c8c33
PA
13492 case bp_until:
13493 ui_out_field_string
13494 (uiout, "reason",
13495 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13496 break;
13497 }
348d480f
PA
13498 }
13499
001c8c33 13500 return PRINT_UNKNOWN;
348d480f
PA
13501}
13502
06edf0c0
PA
13503static void
13504momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13505{
06edf0c0 13506 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13507}
13508
e2e4d78b
JK
13509/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13510
13511 It gets cleared already on the removal of the first one of such placed
13512 breakpoints. This is OK as they get all removed altogether. */
13513
13514static void
13515longjmp_bkpt_dtor (struct breakpoint *self)
13516{
13517 struct thread_info *tp = find_thread_id (self->thread);
13518
13519 if (tp)
13520 tp->initiating_frame = null_frame_id;
13521
13522 momentary_breakpoint_ops.dtor (self);
13523}
13524
55aa24fb
SDJ
13525/* Specific methods for probe breakpoints. */
13526
13527static int
13528bkpt_probe_insert_location (struct bp_location *bl)
13529{
13530 int v = bkpt_insert_location (bl);
13531
13532 if (v == 0)
13533 {
13534 /* The insertion was successful, now let's set the probe's semaphore
13535 if needed. */
729662a5
TT
13536 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13537 bl->probe.objfile,
13538 bl->gdbarch);
55aa24fb
SDJ
13539 }
13540
13541 return v;
13542}
13543
13544static int
13545bkpt_probe_remove_location (struct bp_location *bl)
13546{
13547 /* Let's clear the semaphore before removing the location. */
729662a5
TT
13548 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13549 bl->probe.objfile,
13550 bl->gdbarch);
55aa24fb
SDJ
13551
13552 return bkpt_remove_location (bl);
13553}
13554
13555static void
13556bkpt_probe_create_sals_from_address (char **arg,
13557 struct linespec_result *canonical,
13558 enum bptype type_wanted,
13559 char *addr_start, char **copy_arg)
13560{
13561 struct linespec_sals lsal;
13562
13563 lsal.sals = parse_probes (arg, canonical);
13564
13565 *copy_arg = xstrdup (canonical->addr_string);
13566 lsal.canonical = xstrdup (*copy_arg);
13567
13568 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13569}
13570
13571static void
13572bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13573 struct symtabs_and_lines *sals)
13574{
13575 *sals = parse_probes (s, NULL);
13576 if (!sals->sals)
13577 error (_("probe not found"));
13578}
13579
348d480f 13580/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13581
348d480f
PA
13582static void
13583tracepoint_re_set (struct breakpoint *b)
13584{
13585 breakpoint_re_set_default (b);
13586}
876fa593 13587
348d480f
PA
13588static int
13589tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13590 struct address_space *aspace, CORE_ADDR bp_addr,
13591 const struct target_waitstatus *ws)
348d480f
PA
13592{
13593 /* By definition, the inferior does not report stops at
13594 tracepoints. */
13595 return 0;
74960c60
VP
13596}
13597
13598static void
348d480f
PA
13599tracepoint_print_one_detail (const struct breakpoint *self,
13600 struct ui_out *uiout)
74960c60 13601{
d9b3f62e
PA
13602 struct tracepoint *tp = (struct tracepoint *) self;
13603 if (tp->static_trace_marker_id)
348d480f
PA
13604 {
13605 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13606
348d480f
PA
13607 ui_out_text (uiout, "\tmarker id is ");
13608 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13609 tp->static_trace_marker_id);
348d480f
PA
13610 ui_out_text (uiout, "\n");
13611 }
0d381245
VP
13612}
13613
a474d7c2 13614static void
348d480f 13615tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13616{
79a45e25 13617 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13618 return;
cc59ec59 13619
348d480f
PA
13620 switch (b->type)
13621 {
13622 case bp_tracepoint:
13623 printf_filtered (_("Tracepoint"));
13624 printf_filtered (_(" %d"), b->number);
13625 break;
13626 case bp_fast_tracepoint:
13627 printf_filtered (_("Fast tracepoint"));
13628 printf_filtered (_(" %d"), b->number);
13629 break;
13630 case bp_static_tracepoint:
13631 printf_filtered (_("Static tracepoint"));
13632 printf_filtered (_(" %d"), b->number);
13633 break;
13634 default:
13635 internal_error (__FILE__, __LINE__,
13636 _("unhandled tracepoint type %d"), (int) b->type);
13637 }
13638
13639 say_where (b);
a474d7c2
PA
13640}
13641
348d480f 13642static void
d9b3f62e 13643tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13644{
d9b3f62e
PA
13645 struct tracepoint *tp = (struct tracepoint *) self;
13646
13647 if (self->type == bp_fast_tracepoint)
348d480f 13648 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13649 if (self->type == bp_static_tracepoint)
348d480f 13650 fprintf_unfiltered (fp, "strace");
d9b3f62e 13651 else if (self->type == bp_tracepoint)
348d480f
PA
13652 fprintf_unfiltered (fp, "trace");
13653 else
13654 internal_error (__FILE__, __LINE__,
d9b3f62e 13655 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13656
d9b3f62e
PA
13657 fprintf_unfiltered (fp, " %s", self->addr_string);
13658 print_recreate_thread (self, fp);
13659
13660 if (tp->pass_count)
13661 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13662}
13663
983af33b
SDJ
13664static void
13665tracepoint_create_sals_from_address (char **arg,
13666 struct linespec_result *canonical,
13667 enum bptype type_wanted,
13668 char *addr_start, char **copy_arg)
13669{
13670 create_sals_from_address_default (arg, canonical, type_wanted,
13671 addr_start, copy_arg);
13672}
13673
13674static void
13675tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13676 struct linespec_result *canonical,
983af33b 13677 char *cond_string,
e7e0cddf 13678 char *extra_string,
983af33b
SDJ
13679 enum bptype type_wanted,
13680 enum bpdisp disposition,
13681 int thread,
13682 int task, int ignore_count,
13683 const struct breakpoint_ops *ops,
13684 int from_tty, int enabled,
44f238bb 13685 int internal, unsigned flags)
983af33b 13686{
023fa29b 13687 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13688 cond_string, extra_string,
13689 type_wanted,
983af33b
SDJ
13690 disposition, thread, task,
13691 ignore_count, ops, from_tty,
44f238bb 13692 enabled, internal, flags);
983af33b
SDJ
13693}
13694
13695static void
13696tracepoint_decode_linespec (struct breakpoint *b, char **s,
13697 struct symtabs_and_lines *sals)
13698{
13699 decode_linespec_default (b, s, sals);
13700}
13701
2060206e 13702struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13703
55aa24fb
SDJ
13704/* The breakpoint_ops structure to be use on tracepoints placed in a
13705 static probe. */
13706
13707static void
13708tracepoint_probe_create_sals_from_address (char **arg,
13709 struct linespec_result *canonical,
13710 enum bptype type_wanted,
13711 char *addr_start, char **copy_arg)
13712{
13713 /* We use the same method for breakpoint on probes. */
13714 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13715 addr_start, copy_arg);
13716}
13717
13718static void
13719tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13720 struct symtabs_and_lines *sals)
13721{
13722 /* We use the same method for breakpoint on probes. */
13723 bkpt_probe_decode_linespec (b, s, sals);
13724}
13725
13726static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13727
5c2b4418
HZ
13728/* Dprintf breakpoint_ops methods. */
13729
13730static void
13731dprintf_re_set (struct breakpoint *b)
13732{
13733 breakpoint_re_set_default (b);
13734
13735 /* This breakpoint could have been pending, and be resolved now, and
13736 if so, we should now have the extra string. If we don't, the
13737 dprintf was malformed when created, but we couldn't tell because
13738 we can't extract the extra string until the location is
13739 resolved. */
13740 if (b->loc != NULL && b->extra_string == NULL)
13741 error (_("Format string required"));
13742
13743 /* 1 - connect to target 1, that can run breakpoint commands.
13744 2 - create a dprintf, which resolves fine.
13745 3 - disconnect from target 1
13746 4 - connect to target 2, that can NOT run breakpoint commands.
13747
13748 After steps #3/#4, you'll want the dprintf command list to
13749 be updated, because target 1 and 2 may well return different
13750 answers for target_can_run_breakpoint_commands().
13751 Given absence of finer grained resetting, we get to do
13752 it all the time. */
13753 if (b->extra_string != NULL)
13754 update_dprintf_command_list (b);
13755}
13756
2d9442cc
HZ
13757/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13758
13759static void
13760dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13761{
13762 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13763 tp->extra_string);
13764 print_recreate_thread (tp, fp);
13765}
13766
9d6e6e84
HZ
13767/* Implement the "after_condition_true" breakpoint_ops method for
13768 dprintf.
13769
13770 dprintf's are implemented with regular commands in their command
13771 list, but we run the commands here instead of before presenting the
13772 stop to the user, as dprintf's don't actually cause a stop. This
13773 also makes it so that the commands of multiple dprintfs at the same
13774 address are all handled. */
13775
13776static void
13777dprintf_after_condition_true (struct bpstats *bs)
13778{
13779 struct cleanup *old_chain;
13780 struct bpstats tmp_bs = { NULL };
13781 struct bpstats *tmp_bs_p = &tmp_bs;
13782
13783 /* dprintf's never cause a stop. This wasn't set in the
13784 check_status hook instead because that would make the dprintf's
13785 condition not be evaluated. */
13786 bs->stop = 0;
13787
13788 /* Run the command list here. Take ownership of it instead of
13789 copying. We never want these commands to run later in
13790 bpstat_do_actions, if a breakpoint that causes a stop happens to
13791 be set at same address as this dprintf, or even if running the
13792 commands here throws. */
13793 tmp_bs.commands = bs->commands;
13794 bs->commands = NULL;
13795 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13796
13797 bpstat_do_actions_1 (&tmp_bs_p);
13798
13799 /* 'tmp_bs.commands' will usually be NULL by now, but
13800 bpstat_do_actions_1 may return early without processing the whole
13801 list. */
13802 do_cleanups (old_chain);
13803}
13804
983af33b
SDJ
13805/* The breakpoint_ops structure to be used on static tracepoints with
13806 markers (`-m'). */
13807
13808static void
13809strace_marker_create_sals_from_address (char **arg,
13810 struct linespec_result *canonical,
13811 enum bptype type_wanted,
13812 char *addr_start, char **copy_arg)
13813{
13814 struct linespec_sals lsal;
13815
13816 lsal.sals = decode_static_tracepoint_spec (arg);
13817
13818 *copy_arg = savestring (addr_start, *arg - addr_start);
13819
13820 canonical->addr_string = xstrdup (*copy_arg);
13821 lsal.canonical = xstrdup (*copy_arg);
13822 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13823}
13824
13825static void
13826strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13827 struct linespec_result *canonical,
983af33b 13828 char *cond_string,
e7e0cddf 13829 char *extra_string,
983af33b
SDJ
13830 enum bptype type_wanted,
13831 enum bpdisp disposition,
13832 int thread,
13833 int task, int ignore_count,
13834 const struct breakpoint_ops *ops,
13835 int from_tty, int enabled,
44f238bb 13836 int internal, unsigned flags)
983af33b
SDJ
13837{
13838 int i;
52d361e1
YQ
13839 struct linespec_sals *lsal = VEC_index (linespec_sals,
13840 canonical->sals, 0);
983af33b
SDJ
13841
13842 /* If the user is creating a static tracepoint by marker id
13843 (strace -m MARKER_ID), then store the sals index, so that
13844 breakpoint_re_set can try to match up which of the newly
13845 found markers corresponds to this one, and, don't try to
13846 expand multiple locations for each sal, given than SALS
13847 already should contain all sals for MARKER_ID. */
13848
13849 for (i = 0; i < lsal->sals.nelts; ++i)
13850 {
13851 struct symtabs_and_lines expanded;
13852 struct tracepoint *tp;
13853 struct cleanup *old_chain;
13854 char *addr_string;
13855
13856 expanded.nelts = 1;
13857 expanded.sals = &lsal->sals.sals[i];
13858
13859 addr_string = xstrdup (canonical->addr_string);
13860 old_chain = make_cleanup (xfree, addr_string);
13861
13862 tp = XCNEW (struct tracepoint);
13863 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13864 addr_string, NULL,
e7e0cddf
SS
13865 cond_string, extra_string,
13866 type_wanted, disposition,
983af33b 13867 thread, task, ignore_count, ops,
44f238bb 13868 from_tty, enabled, internal, flags,
983af33b
SDJ
13869 canonical->special_display);
13870 /* Given that its possible to have multiple markers with
13871 the same string id, if the user is creating a static
13872 tracepoint by marker id ("strace -m MARKER_ID"), then
13873 store the sals index, so that breakpoint_re_set can
13874 try to match up which of the newly found markers
13875 corresponds to this one */
13876 tp->static_trace_marker_id_idx = i;
13877
13878 install_breakpoint (internal, &tp->base, 0);
13879
13880 discard_cleanups (old_chain);
13881 }
13882}
13883
13884static void
13885strace_marker_decode_linespec (struct breakpoint *b, char **s,
13886 struct symtabs_and_lines *sals)
13887{
13888 struct tracepoint *tp = (struct tracepoint *) b;
13889
13890 *sals = decode_static_tracepoint_spec (s);
13891 if (sals->nelts > tp->static_trace_marker_id_idx)
13892 {
13893 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13894 sals->nelts = 1;
13895 }
13896 else
13897 error (_("marker %s not found"), tp->static_trace_marker_id);
13898}
13899
13900static struct breakpoint_ops strace_marker_breakpoint_ops;
13901
13902static int
13903strace_marker_p (struct breakpoint *b)
13904{
13905 return b->ops == &strace_marker_breakpoint_ops;
13906}
13907
53a5351d 13908/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13909 structures. */
c906108c
SS
13910
13911void
fba45db2 13912delete_breakpoint (struct breakpoint *bpt)
c906108c 13913{
52f0bd74 13914 struct breakpoint *b;
c906108c 13915
8a3fe4f8 13916 gdb_assert (bpt != NULL);
c906108c 13917
4a64f543
MS
13918 /* Has this bp already been deleted? This can happen because
13919 multiple lists can hold pointers to bp's. bpstat lists are
13920 especial culprits.
13921
13922 One example of this happening is a watchpoint's scope bp. When
13923 the scope bp triggers, we notice that the watchpoint is out of
13924 scope, and delete it. We also delete its scope bp. But the
13925 scope bp is marked "auto-deleting", and is already on a bpstat.
13926 That bpstat is then checked for auto-deleting bp's, which are
13927 deleted.
13928
13929 A real solution to this problem might involve reference counts in
13930 bp's, and/or giving them pointers back to their referencing
13931 bpstat's, and teaching delete_breakpoint to only free a bp's
13932 storage when no more references were extent. A cheaper bandaid
13933 was chosen. */
c906108c
SS
13934 if (bpt->type == bp_none)
13935 return;
13936
4a64f543
MS
13937 /* At least avoid this stale reference until the reference counting
13938 of breakpoints gets resolved. */
d0fb5eae 13939 if (bpt->related_breakpoint != bpt)
e5a0a904 13940 {
d0fb5eae 13941 struct breakpoint *related;
3a5c3e22 13942 struct watchpoint *w;
d0fb5eae
JK
13943
13944 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13945 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13946 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13947 w = (struct watchpoint *) bpt;
13948 else
13949 w = NULL;
13950 if (w != NULL)
13951 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13952
13953 /* Unlink bpt from the bpt->related_breakpoint ring. */
13954 for (related = bpt; related->related_breakpoint != bpt;
13955 related = related->related_breakpoint);
13956 related->related_breakpoint = bpt->related_breakpoint;
13957 bpt->related_breakpoint = bpt;
e5a0a904
JK
13958 }
13959
a9634178
TJB
13960 /* watch_command_1 creates a watchpoint but only sets its number if
13961 update_watchpoint succeeds in creating its bp_locations. If there's
13962 a problem in that process, we'll be asked to delete the half-created
13963 watchpoint. In that case, don't announce the deletion. */
13964 if (bpt->number)
13965 observer_notify_breakpoint_deleted (bpt);
c906108c 13966
c906108c
SS
13967 if (breakpoint_chain == bpt)
13968 breakpoint_chain = bpt->next;
13969
c906108c
SS
13970 ALL_BREAKPOINTS (b)
13971 if (b->next == bpt)
c5aa993b
JM
13972 {
13973 b->next = bpt->next;
13974 break;
13975 }
c906108c 13976
f431efe5
PA
13977 /* Be sure no bpstat's are pointing at the breakpoint after it's
13978 been freed. */
13979 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13980 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13981 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13982 commands are associated with the bpstat; if we remove it here,
13983 then the later call to bpstat_do_actions (&stop_bpstat); in
13984 event-top.c won't do anything, and temporary breakpoints with
13985 commands won't work. */
13986
13987 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13988
4a64f543
MS
13989 /* Now that breakpoint is removed from breakpoint list, update the
13990 global location list. This will remove locations that used to
13991 belong to this breakpoint. Do this before freeing the breakpoint
13992 itself, since remove_breakpoint looks at location's owner. It
13993 might be better design to have location completely
13994 self-contained, but it's not the case now. */
b60e7edf 13995 update_global_location_list (0);
74960c60 13996
348d480f 13997 bpt->ops->dtor (bpt);
4a64f543
MS
13998 /* On the chance that someone will soon try again to delete this
13999 same bp, we mark it as deleted before freeing its storage. */
c906108c 14000 bpt->type = bp_none;
b8c9b27d 14001 xfree (bpt);
c906108c
SS
14002}
14003
4d6140d9
AC
14004static void
14005do_delete_breakpoint_cleanup (void *b)
14006{
14007 delete_breakpoint (b);
14008}
14009
14010struct cleanup *
14011make_cleanup_delete_breakpoint (struct breakpoint *b)
14012{
14013 return make_cleanup (do_delete_breakpoint_cleanup, b);
14014}
14015
51be5b68
PA
14016/* Iterator function to call a user-provided callback function once
14017 for each of B and its related breakpoints. */
14018
14019static void
14020iterate_over_related_breakpoints (struct breakpoint *b,
14021 void (*function) (struct breakpoint *,
14022 void *),
14023 void *data)
14024{
14025 struct breakpoint *related;
14026
14027 related = b;
14028 do
14029 {
14030 struct breakpoint *next;
14031
14032 /* FUNCTION may delete RELATED. */
14033 next = related->related_breakpoint;
14034
14035 if (next == related)
14036 {
14037 /* RELATED is the last ring entry. */
14038 function (related, data);
14039
14040 /* FUNCTION may have deleted it, so we'd never reach back to
14041 B. There's nothing left to do anyway, so just break
14042 out. */
14043 break;
14044 }
14045 else
14046 function (related, data);
14047
14048 related = next;
14049 }
14050 while (related != b);
14051}
95a42b64
TT
14052
14053static void
14054do_delete_breakpoint (struct breakpoint *b, void *ignore)
14055{
14056 delete_breakpoint (b);
14057}
14058
51be5b68
PA
14059/* A callback for map_breakpoint_numbers that calls
14060 delete_breakpoint. */
14061
14062static void
14063do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14064{
14065 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14066}
14067
c906108c 14068void
fba45db2 14069delete_command (char *arg, int from_tty)
c906108c 14070{
35df4500 14071 struct breakpoint *b, *b_tmp;
c906108c 14072
ea9365bb
TT
14073 dont_repeat ();
14074
c906108c
SS
14075 if (arg == 0)
14076 {
14077 int breaks_to_delete = 0;
14078
46c6471b
PA
14079 /* Delete all breakpoints if no argument. Do not delete
14080 internal breakpoints, these have to be deleted with an
14081 explicit breakpoint number argument. */
c5aa993b 14082 ALL_BREAKPOINTS (b)
46c6471b 14083 if (user_breakpoint_p (b))
973d738b
DJ
14084 {
14085 breaks_to_delete = 1;
14086 break;
14087 }
c906108c
SS
14088
14089 /* Ask user only if there are some breakpoints to delete. */
14090 if (!from_tty
e2e0b3e5 14091 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14092 {
35df4500 14093 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14094 if (user_breakpoint_p (b))
c5aa993b 14095 delete_breakpoint (b);
c906108c
SS
14096 }
14097 }
14098 else
51be5b68 14099 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14100}
14101
0d381245
VP
14102static int
14103all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 14104{
0d381245 14105 for (; loc; loc = loc->next)
8645ff69
UW
14106 if (!loc->shlib_disabled
14107 && !loc->pspace->executing_startup)
0d381245
VP
14108 return 0;
14109 return 1;
fe3f5fa8
VP
14110}
14111
776592bf
DE
14112/* Subroutine of update_breakpoint_locations to simplify it.
14113 Return non-zero if multiple fns in list LOC have the same name.
14114 Null names are ignored. */
14115
14116static int
14117ambiguous_names_p (struct bp_location *loc)
14118{
14119 struct bp_location *l;
14120 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14121 (int (*) (const void *,
14122 const void *)) streq,
776592bf
DE
14123 NULL, xcalloc, xfree);
14124
14125 for (l = loc; l != NULL; l = l->next)
14126 {
14127 const char **slot;
14128 const char *name = l->function_name;
14129
14130 /* Allow for some names to be NULL, ignore them. */
14131 if (name == NULL)
14132 continue;
14133
14134 slot = (const char **) htab_find_slot (htab, (const void *) name,
14135 INSERT);
4a64f543
MS
14136 /* NOTE: We can assume slot != NULL here because xcalloc never
14137 returns NULL. */
776592bf
DE
14138 if (*slot != NULL)
14139 {
14140 htab_delete (htab);
14141 return 1;
14142 }
14143 *slot = name;
14144 }
14145
14146 htab_delete (htab);
14147 return 0;
14148}
14149
0fb4aa4b
PA
14150/* When symbols change, it probably means the sources changed as well,
14151 and it might mean the static tracepoint markers are no longer at
14152 the same address or line numbers they used to be at last we
14153 checked. Losing your static tracepoints whenever you rebuild is
14154 undesirable. This function tries to resync/rematch gdb static
14155 tracepoints with the markers on the target, for static tracepoints
14156 that have not been set by marker id. Static tracepoint that have
14157 been set by marker id are reset by marker id in breakpoint_re_set.
14158 The heuristic is:
14159
14160 1) For a tracepoint set at a specific address, look for a marker at
14161 the old PC. If one is found there, assume to be the same marker.
14162 If the name / string id of the marker found is different from the
14163 previous known name, assume that means the user renamed the marker
14164 in the sources, and output a warning.
14165
14166 2) For a tracepoint set at a given line number, look for a marker
14167 at the new address of the old line number. If one is found there,
14168 assume to be the same marker. If the name / string id of the
14169 marker found is different from the previous known name, assume that
14170 means the user renamed the marker in the sources, and output a
14171 warning.
14172
14173 3) If a marker is no longer found at the same address or line, it
14174 may mean the marker no longer exists. But it may also just mean
14175 the code changed a bit. Maybe the user added a few lines of code
14176 that made the marker move up or down (in line number terms). Ask
14177 the target for info about the marker with the string id as we knew
14178 it. If found, update line number and address in the matching
14179 static tracepoint. This will get confused if there's more than one
14180 marker with the same ID (possible in UST, although unadvised
14181 precisely because it confuses tools). */
14182
14183static struct symtab_and_line
14184update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14185{
d9b3f62e 14186 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14187 struct static_tracepoint_marker marker;
14188 CORE_ADDR pc;
0fb4aa4b
PA
14189
14190 pc = sal.pc;
14191 if (sal.line)
14192 find_line_pc (sal.symtab, sal.line, &pc);
14193
14194 if (target_static_tracepoint_marker_at (pc, &marker))
14195 {
d9b3f62e 14196 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14197 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14198 b->number,
d9b3f62e 14199 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14200
d9b3f62e
PA
14201 xfree (tp->static_trace_marker_id);
14202 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14203 release_static_tracepoint_marker (&marker);
14204
14205 return sal;
14206 }
14207
14208 /* Old marker wasn't found on target at lineno. Try looking it up
14209 by string ID. */
14210 if (!sal.explicit_pc
14211 && sal.line != 0
14212 && sal.symtab != NULL
d9b3f62e 14213 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14214 {
14215 VEC(static_tracepoint_marker_p) *markers;
14216
14217 markers
d9b3f62e 14218 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14219
14220 if (!VEC_empty(static_tracepoint_marker_p, markers))
14221 {
80e1d417 14222 struct symtab_and_line sal2;
0fb4aa4b 14223 struct symbol *sym;
80e1d417 14224 struct static_tracepoint_marker *tpmarker;
79a45e25 14225 struct ui_out *uiout = current_uiout;
0fb4aa4b 14226
80e1d417 14227 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14228
d9b3f62e 14229 xfree (tp->static_trace_marker_id);
80e1d417 14230 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14231
14232 warning (_("marker for static tracepoint %d (%s) not "
14233 "found at previous line number"),
d9b3f62e 14234 b->number, tp->static_trace_marker_id);
0fb4aa4b 14235
80e1d417 14236 init_sal (&sal2);
0fb4aa4b 14237
80e1d417 14238 sal2.pc = tpmarker->address;
0fb4aa4b 14239
80e1d417
AS
14240 sal2 = find_pc_line (tpmarker->address, 0);
14241 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14242 ui_out_text (uiout, "Now in ");
14243 if (sym)
14244 {
14245 ui_out_field_string (uiout, "func",
14246 SYMBOL_PRINT_NAME (sym));
14247 ui_out_text (uiout, " at ");
14248 }
05cba821
JK
14249 ui_out_field_string (uiout, "file",
14250 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14251 ui_out_text (uiout, ":");
14252
14253 if (ui_out_is_mi_like_p (uiout))
14254 {
0b0865da 14255 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14256
f35a17b5 14257 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14258 }
14259
80e1d417 14260 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14261 ui_out_text (uiout, "\n");
14262
80e1d417 14263 b->loc->line_number = sal2.line;
2f202fde 14264 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14265
14266 xfree (b->addr_string);
14267 b->addr_string = xstrprintf ("%s:%d",
05cba821 14268 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14269 b->loc->line_number);
0fb4aa4b
PA
14270
14271 /* Might be nice to check if function changed, and warn if
14272 so. */
14273
80e1d417 14274 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14275 }
14276 }
14277 return sal;
14278}
14279
8d3788bd
VP
14280/* Returns 1 iff locations A and B are sufficiently same that
14281 we don't need to report breakpoint as changed. */
14282
14283static int
14284locations_are_equal (struct bp_location *a, struct bp_location *b)
14285{
14286 while (a && b)
14287 {
14288 if (a->address != b->address)
14289 return 0;
14290
14291 if (a->shlib_disabled != b->shlib_disabled)
14292 return 0;
14293
14294 if (a->enabled != b->enabled)
14295 return 0;
14296
14297 a = a->next;
14298 b = b->next;
14299 }
14300
14301 if ((a == NULL) != (b == NULL))
14302 return 0;
14303
14304 return 1;
14305}
14306
f1310107
TJB
14307/* Create new breakpoint locations for B (a hardware or software breakpoint)
14308 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14309 a ranged breakpoint. */
14310
0e30163f 14311void
0d381245 14312update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14313 struct symtabs_and_lines sals,
14314 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14315{
14316 int i;
0d381245
VP
14317 struct bp_location *existing_locations = b->loc;
14318
f8eba3c6
TT
14319 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14320 {
14321 /* Ranged breakpoints have only one start location and one end
14322 location. */
14323 b->enable_state = bp_disabled;
14324 update_global_location_list (1);
14325 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14326 "multiple locations found\n"),
14327 b->number);
14328 return;
14329 }
f1310107 14330
4a64f543
MS
14331 /* If there's no new locations, and all existing locations are
14332 pending, don't do anything. This optimizes the common case where
14333 all locations are in the same shared library, that was unloaded.
14334 We'd like to retain the location, so that when the library is
14335 loaded again, we don't loose the enabled/disabled status of the
14336 individual locations. */
0d381245 14337 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14338 return;
14339
fe3f5fa8
VP
14340 b->loc = NULL;
14341
0d381245 14342 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14343 {
f8eba3c6
TT
14344 struct bp_location *new_loc;
14345
14346 switch_to_program_space_and_thread (sals.sals[i].pspace);
14347
14348 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14349
0d381245
VP
14350 /* Reparse conditions, they might contain references to the
14351 old symtab. */
14352 if (b->cond_string != NULL)
14353 {
bbc13ae3 14354 const char *s;
bfd189b1 14355 volatile struct gdb_exception e;
fe3f5fa8 14356
0d381245
VP
14357 s = b->cond_string;
14358 TRY_CATCH (e, RETURN_MASK_ERROR)
14359 {
1bb9788d
TT
14360 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14361 block_for_pc (sals.sals[i].pc),
0d381245
VP
14362 0);
14363 }
14364 if (e.reason < 0)
14365 {
3e43a32a
MS
14366 warning (_("failed to reevaluate condition "
14367 "for breakpoint %d: %s"),
0d381245
VP
14368 b->number, e.message);
14369 new_loc->enabled = 0;
14370 }
14371 }
fe3f5fa8 14372
f1310107
TJB
14373 if (sals_end.nelts)
14374 {
14375 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14376
14377 new_loc->length = end - sals.sals[0].pc + 1;
14378 }
0d381245 14379 }
fe3f5fa8 14380
514f746b
AR
14381 /* Update locations of permanent breakpoints. */
14382 if (b->enable_state == bp_permanent)
14383 make_breakpoint_permanent (b);
14384
4a64f543
MS
14385 /* If possible, carry over 'disable' status from existing
14386 breakpoints. */
0d381245
VP
14387 {
14388 struct bp_location *e = existing_locations;
776592bf
DE
14389 /* If there are multiple breakpoints with the same function name,
14390 e.g. for inline functions, comparing function names won't work.
14391 Instead compare pc addresses; this is just a heuristic as things
14392 may have moved, but in practice it gives the correct answer
14393 often enough until a better solution is found. */
14394 int have_ambiguous_names = ambiguous_names_p (b->loc);
14395
0d381245
VP
14396 for (; e; e = e->next)
14397 {
14398 if (!e->enabled && e->function_name)
14399 {
14400 struct bp_location *l = b->loc;
776592bf
DE
14401 if (have_ambiguous_names)
14402 {
14403 for (; l; l = l->next)
f1310107 14404 if (breakpoint_locations_match (e, l))
776592bf
DE
14405 {
14406 l->enabled = 0;
14407 break;
14408 }
14409 }
14410 else
14411 {
14412 for (; l; l = l->next)
14413 if (l->function_name
14414 && strcmp (e->function_name, l->function_name) == 0)
14415 {
14416 l->enabled = 0;
14417 break;
14418 }
14419 }
0d381245
VP
14420 }
14421 }
14422 }
fe3f5fa8 14423
8d3788bd
VP
14424 if (!locations_are_equal (existing_locations, b->loc))
14425 observer_notify_breakpoint_modified (b);
14426
b60e7edf 14427 update_global_location_list (1);
fe3f5fa8
VP
14428}
14429
ef23e705
TJB
14430/* Find the SaL locations corresponding to the given ADDR_STRING.
14431 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14432
14433static struct symtabs_and_lines
14434addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14435{
14436 char *s;
02d20e4a 14437 struct symtabs_and_lines sals = {0};
f8eba3c6 14438 volatile struct gdb_exception e;
ef23e705 14439
983af33b 14440 gdb_assert (b->ops != NULL);
ef23e705 14441 s = addr_string;
ef23e705
TJB
14442
14443 TRY_CATCH (e, RETURN_MASK_ERROR)
14444 {
983af33b 14445 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14446 }
14447 if (e.reason < 0)
14448 {
14449 int not_found_and_ok = 0;
14450 /* For pending breakpoints, it's expected that parsing will
14451 fail until the right shared library is loaded. User has
14452 already told to create pending breakpoints and don't need
14453 extra messages. If breakpoint is in bp_shlib_disabled
14454 state, then user already saw the message about that
14455 breakpoint being disabled, and don't want to see more
14456 errors. */
58438ac1 14457 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14458 && (b->condition_not_parsed
14459 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14460 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14461 || b->enable_state == bp_disabled))
14462 not_found_and_ok = 1;
14463
14464 if (!not_found_and_ok)
14465 {
14466 /* We surely don't want to warn about the same breakpoint
14467 10 times. One solution, implemented here, is disable
14468 the breakpoint on error. Another solution would be to
14469 have separate 'warning emitted' flag. Since this
14470 happens only when a binary has changed, I don't know
14471 which approach is better. */
14472 b->enable_state = bp_disabled;
14473 throw_exception (e);
14474 }
14475 }
14476
58438ac1 14477 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14478 {
f8eba3c6 14479 int i;
ef23e705 14480
f8eba3c6
TT
14481 for (i = 0; i < sals.nelts; ++i)
14482 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14483 if (b->condition_not_parsed && s && s[0])
14484 {
ed1d1739
KS
14485 char *cond_string, *extra_string;
14486 int thread, task;
ef23e705
TJB
14487
14488 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14489 &cond_string, &thread, &task,
14490 &extra_string);
ef23e705
TJB
14491 if (cond_string)
14492 b->cond_string = cond_string;
14493 b->thread = thread;
14494 b->task = task;
e7e0cddf
SS
14495 if (extra_string)
14496 b->extra_string = extra_string;
ef23e705
TJB
14497 b->condition_not_parsed = 0;
14498 }
14499
983af33b 14500 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14501 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14502
58438ac1
TT
14503 *found = 1;
14504 }
14505 else
14506 *found = 0;
ef23e705
TJB
14507
14508 return sals;
14509}
14510
348d480f
PA
14511/* The default re_set method, for typical hardware or software
14512 breakpoints. Reevaluate the breakpoint and recreate its
14513 locations. */
14514
14515static void
28010a5d 14516breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14517{
14518 int found;
f1310107 14519 struct symtabs_and_lines sals, sals_end;
ef23e705 14520 struct symtabs_and_lines expanded = {0};
f1310107 14521 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14522
14523 sals = addr_string_to_sals (b, b->addr_string, &found);
14524 if (found)
14525 {
14526 make_cleanup (xfree, sals.sals);
f8eba3c6 14527 expanded = sals;
ef23e705
TJB
14528 }
14529
f1310107
TJB
14530 if (b->addr_string_range_end)
14531 {
14532 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14533 if (found)
14534 {
14535 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14536 expanded_end = sals_end;
f1310107
TJB
14537 }
14538 }
14539
14540 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14541}
14542
983af33b
SDJ
14543/* Default method for creating SALs from an address string. It basically
14544 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14545
14546static void
14547create_sals_from_address_default (char **arg,
14548 struct linespec_result *canonical,
14549 enum bptype type_wanted,
14550 char *addr_start, char **copy_arg)
14551{
14552 parse_breakpoint_sals (arg, canonical);
14553}
14554
14555/* Call create_breakpoints_sal for the given arguments. This is the default
14556 function for the `create_breakpoints_sal' method of
14557 breakpoint_ops. */
14558
14559static void
14560create_breakpoints_sal_default (struct gdbarch *gdbarch,
14561 struct linespec_result *canonical,
983af33b 14562 char *cond_string,
e7e0cddf 14563 char *extra_string,
983af33b
SDJ
14564 enum bptype type_wanted,
14565 enum bpdisp disposition,
14566 int thread,
14567 int task, int ignore_count,
14568 const struct breakpoint_ops *ops,
14569 int from_tty, int enabled,
44f238bb 14570 int internal, unsigned flags)
983af33b
SDJ
14571{
14572 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14573 extra_string,
983af33b
SDJ
14574 type_wanted, disposition,
14575 thread, task, ignore_count, ops, from_tty,
44f238bb 14576 enabled, internal, flags);
983af33b
SDJ
14577}
14578
14579/* Decode the line represented by S by calling decode_line_full. This is the
14580 default function for the `decode_linespec' method of breakpoint_ops. */
14581
14582static void
14583decode_linespec_default (struct breakpoint *b, char **s,
14584 struct symtabs_and_lines *sals)
14585{
14586 struct linespec_result canonical;
14587
14588 init_linespec_result (&canonical);
14589 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14590 (struct symtab *) NULL, 0,
14591 &canonical, multiple_symbols_all,
14592 b->filter);
14593
14594 /* We should get 0 or 1 resulting SALs. */
14595 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14596
14597 if (VEC_length (linespec_sals, canonical.sals) > 0)
14598 {
14599 struct linespec_sals *lsal;
14600
14601 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14602 *sals = lsal->sals;
14603 /* Arrange it so the destructor does not free the
14604 contents. */
14605 lsal->sals.sals = NULL;
14606 }
14607
14608 destroy_linespec_result (&canonical);
14609}
14610
28010a5d
PA
14611/* Prepare the global context for a re-set of breakpoint B. */
14612
14613static struct cleanup *
14614prepare_re_set_context (struct breakpoint *b)
14615{
14616 struct cleanup *cleanups;
14617
14618 input_radix = b->input_radix;
14619 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14620 if (b->pspace != NULL)
14621 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14622 set_language (b->language);
14623
14624 return cleanups;
ef23e705
TJB
14625}
14626
c906108c
SS
14627/* Reset a breakpoint given it's struct breakpoint * BINT.
14628 The value we return ends up being the return value from catch_errors.
14629 Unused in this case. */
14630
14631static int
4efb68b1 14632breakpoint_re_set_one (void *bint)
c906108c 14633{
4a64f543 14634 /* Get past catch_errs. */
53a5351d 14635 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14636 struct cleanup *cleanups;
c906108c 14637
348d480f
PA
14638 cleanups = prepare_re_set_context (b);
14639 b->ops->re_set (b);
14640 do_cleanups (cleanups);
c906108c
SS
14641 return 0;
14642}
14643
69de3c6a 14644/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14645void
69de3c6a 14646breakpoint_re_set (void)
c906108c 14647{
35df4500 14648 struct breakpoint *b, *b_tmp;
c906108c
SS
14649 enum language save_language;
14650 int save_input_radix;
6c95b8df 14651 struct cleanup *old_chain;
c5aa993b 14652
c906108c
SS
14653 save_language = current_language->la_language;
14654 save_input_radix = input_radix;
6c95b8df
PA
14655 old_chain = save_current_program_space ();
14656
35df4500 14657 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14658 {
4a64f543 14659 /* Format possible error msg. */
fe3f5fa8 14660 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14661 b->number);
14662 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14663 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14664 do_cleanups (cleanups);
c5aa993b 14665 }
c906108c
SS
14666 set_language (save_language);
14667 input_radix = save_input_radix;
e62c965a 14668
0756c555 14669 jit_breakpoint_re_set ();
4efc6507 14670
6c95b8df
PA
14671 do_cleanups (old_chain);
14672
af02033e
PP
14673 create_overlay_event_breakpoint ();
14674 create_longjmp_master_breakpoint ();
14675 create_std_terminate_master_breakpoint ();
186c406b 14676 create_exception_master_breakpoint ();
c906108c
SS
14677}
14678\f
c906108c
SS
14679/* Reset the thread number of this breakpoint:
14680
14681 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14682 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14683void
fba45db2 14684breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14685{
14686 if (b->thread != -1)
14687 {
39f77062
KB
14688 if (in_thread_list (inferior_ptid))
14689 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14690
14691 /* We're being called after following a fork. The new fork is
14692 selected as current, and unless this was a vfork will have a
14693 different program space from the original thread. Reset that
14694 as well. */
14695 b->loc->pspace = current_program_space;
c906108c
SS
14696 }
14697}
14698
03ac34d5
MS
14699/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14700 If from_tty is nonzero, it prints a message to that effect,
14701 which ends with a period (no newline). */
14702
c906108c 14703void
fba45db2 14704set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14705{
52f0bd74 14706 struct breakpoint *b;
c906108c
SS
14707
14708 if (count < 0)
14709 count = 0;
14710
14711 ALL_BREAKPOINTS (b)
14712 if (b->number == bptnum)
c5aa993b 14713 {
d77f58be
SS
14714 if (is_tracepoint (b))
14715 {
14716 if (from_tty && count != 0)
14717 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14718 bptnum);
14719 return;
14720 }
14721
c5aa993b 14722 b->ignore_count = count;
221ea385
KS
14723 if (from_tty)
14724 {
14725 if (count == 0)
3e43a32a
MS
14726 printf_filtered (_("Will stop next time "
14727 "breakpoint %d is reached."),
221ea385
KS
14728 bptnum);
14729 else if (count == 1)
a3f17187 14730 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14731 bptnum);
14732 else
3e43a32a
MS
14733 printf_filtered (_("Will ignore next %d "
14734 "crossings of breakpoint %d."),
221ea385
KS
14735 count, bptnum);
14736 }
8d3788bd 14737 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14738 return;
14739 }
c906108c 14740
8a3fe4f8 14741 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14742}
14743
c906108c
SS
14744/* Command to set ignore-count of breakpoint N to COUNT. */
14745
14746static void
fba45db2 14747ignore_command (char *args, int from_tty)
c906108c
SS
14748{
14749 char *p = args;
52f0bd74 14750 int num;
c906108c
SS
14751
14752 if (p == 0)
e2e0b3e5 14753 error_no_arg (_("a breakpoint number"));
c5aa993b 14754
c906108c 14755 num = get_number (&p);
5c44784c 14756 if (num == 0)
8a3fe4f8 14757 error (_("bad breakpoint number: '%s'"), args);
c906108c 14758 if (*p == 0)
8a3fe4f8 14759 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14760
14761 set_ignore_count (num,
14762 longest_to_int (value_as_long (parse_and_eval (p))),
14763 from_tty);
221ea385
KS
14764 if (from_tty)
14765 printf_filtered ("\n");
c906108c
SS
14766}
14767\f
14768/* Call FUNCTION on each of the breakpoints
14769 whose numbers are given in ARGS. */
14770
14771static void
95a42b64
TT
14772map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14773 void *),
14774 void *data)
c906108c 14775{
52f0bd74
AC
14776 int num;
14777 struct breakpoint *b, *tmp;
11cf8741 14778 int match;
197f0a60 14779 struct get_number_or_range_state state;
c906108c 14780
197f0a60 14781 if (args == 0)
e2e0b3e5 14782 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14783
197f0a60
TT
14784 init_number_or_range (&state, args);
14785
14786 while (!state.finished)
c906108c 14787 {
197f0a60
TT
14788 char *p = state.string;
14789
11cf8741 14790 match = 0;
c5aa993b 14791
197f0a60 14792 num = get_number_or_range (&state);
5c44784c 14793 if (num == 0)
c5aa993b 14794 {
8a3fe4f8 14795 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14796 }
14797 else
14798 {
14799 ALL_BREAKPOINTS_SAFE (b, tmp)
14800 if (b->number == num)
14801 {
11cf8741 14802 match = 1;
cdac0397 14803 function (b, data);
11cf8741 14804 break;
5c44784c 14805 }
11cf8741 14806 if (match == 0)
a3f17187 14807 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14808 }
c906108c
SS
14809 }
14810}
14811
0d381245
VP
14812static struct bp_location *
14813find_location_by_number (char *number)
14814{
14815 char *dot = strchr (number, '.');
14816 char *p1;
14817 int bp_num;
14818 int loc_num;
14819 struct breakpoint *b;
14820 struct bp_location *loc;
14821
14822 *dot = '\0';
14823
14824 p1 = number;
197f0a60 14825 bp_num = get_number (&p1);
0d381245
VP
14826 if (bp_num == 0)
14827 error (_("Bad breakpoint number '%s'"), number);
14828
14829 ALL_BREAKPOINTS (b)
14830 if (b->number == bp_num)
14831 {
14832 break;
14833 }
14834
14835 if (!b || b->number != bp_num)
14836 error (_("Bad breakpoint number '%s'"), number);
14837
14838 p1 = dot+1;
197f0a60 14839 loc_num = get_number (&p1);
0d381245
VP
14840 if (loc_num == 0)
14841 error (_("Bad breakpoint location number '%s'"), number);
14842
14843 --loc_num;
14844 loc = b->loc;
14845 for (;loc_num && loc; --loc_num, loc = loc->next)
14846 ;
14847 if (!loc)
14848 error (_("Bad breakpoint location number '%s'"), dot+1);
14849
14850 return loc;
14851}
14852
14853
1900040c
MS
14854/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14855 If from_tty is nonzero, it prints a message to that effect,
14856 which ends with a period (no newline). */
14857
c906108c 14858void
fba45db2 14859disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14860{
14861 /* Never disable a watchpoint scope breakpoint; we want to
14862 hit them when we leave scope so we can delete both the
14863 watchpoint and its scope breakpoint at that time. */
14864 if (bpt->type == bp_watchpoint_scope)
14865 return;
14866
c2c6d25f 14867 /* You can't disable permanent breakpoints. */
b5de0fa7 14868 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14869 return;
14870
b5de0fa7 14871 bpt->enable_state = bp_disabled;
c906108c 14872
b775012e
LM
14873 /* Mark breakpoint locations modified. */
14874 mark_breakpoint_modified (bpt);
14875
d248b706
KY
14876 if (target_supports_enable_disable_tracepoint ()
14877 && current_trace_status ()->running && is_tracepoint (bpt))
14878 {
14879 struct bp_location *location;
14880
14881 for (location = bpt->loc; location; location = location->next)
14882 target_disable_tracepoint (location);
14883 }
14884
b60e7edf 14885 update_global_location_list (0);
c906108c 14886
8d3788bd 14887 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14888}
14889
51be5b68
PA
14890/* A callback for iterate_over_related_breakpoints. */
14891
14892static void
14893do_disable_breakpoint (struct breakpoint *b, void *ignore)
14894{
14895 disable_breakpoint (b);
14896}
14897
95a42b64
TT
14898/* A callback for map_breakpoint_numbers that calls
14899 disable_breakpoint. */
14900
14901static void
14902do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14903{
51be5b68 14904 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14905}
14906
c906108c 14907static void
fba45db2 14908disable_command (char *args, int from_tty)
c906108c 14909{
c906108c 14910 if (args == 0)
46c6471b
PA
14911 {
14912 struct breakpoint *bpt;
14913
14914 ALL_BREAKPOINTS (bpt)
14915 if (user_breakpoint_p (bpt))
14916 disable_breakpoint (bpt);
14917 }
9eaabc75 14918 else
0d381245 14919 {
9eaabc75
MW
14920 char *num = extract_arg (&args);
14921
14922 while (num)
d248b706 14923 {
9eaabc75 14924 if (strchr (num, '.'))
b775012e 14925 {
9eaabc75
MW
14926 struct bp_location *loc = find_location_by_number (num);
14927
14928 if (loc)
14929 {
14930 if (loc->enabled)
14931 {
14932 loc->enabled = 0;
14933 mark_breakpoint_location_modified (loc);
14934 }
14935 if (target_supports_enable_disable_tracepoint ()
14936 && current_trace_status ()->running && loc->owner
14937 && is_tracepoint (loc->owner))
14938 target_disable_tracepoint (loc);
14939 }
14940 update_global_location_list (0);
b775012e 14941 }
9eaabc75
MW
14942 else
14943 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14944 num = extract_arg (&args);
d248b706 14945 }
0d381245 14946 }
c906108c
SS
14947}
14948
14949static void
816338b5
SS
14950enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14951 int count)
c906108c 14952{
afe38095 14953 int target_resources_ok;
c906108c
SS
14954
14955 if (bpt->type == bp_hardware_breakpoint)
14956 {
14957 int i;
c5aa993b 14958 i = hw_breakpoint_used_count ();
53a5351d 14959 target_resources_ok =
d92524f1 14960 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14961 i + 1, 0);
c906108c 14962 if (target_resources_ok == 0)
8a3fe4f8 14963 error (_("No hardware breakpoint support in the target."));
c906108c 14964 else if (target_resources_ok < 0)
8a3fe4f8 14965 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14966 }
14967
cc60f2e3 14968 if (is_watchpoint (bpt))
c906108c 14969 {
d07205c2
JK
14970 /* Initialize it just to avoid a GCC false warning. */
14971 enum enable_state orig_enable_state = 0;
bfd189b1 14972 volatile struct gdb_exception e;
dde02812
ES
14973
14974 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 14975 {
3a5c3e22
PA
14976 struct watchpoint *w = (struct watchpoint *) bpt;
14977
1e718ff1
TJB
14978 orig_enable_state = bpt->enable_state;
14979 bpt->enable_state = bp_enabled;
3a5c3e22 14980 update_watchpoint (w, 1 /* reparse */);
c906108c 14981 }
dde02812 14982 if (e.reason < 0)
c5aa993b 14983 {
1e718ff1 14984 bpt->enable_state = orig_enable_state;
dde02812
ES
14985 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14986 bpt->number);
14987 return;
c5aa993b 14988 }
c906108c 14989 }
0101ce28 14990
b4c291bb
KH
14991 if (bpt->enable_state != bp_permanent)
14992 bpt->enable_state = bp_enabled;
d248b706 14993
b775012e
LM
14994 bpt->enable_state = bp_enabled;
14995
14996 /* Mark breakpoint locations modified. */
14997 mark_breakpoint_modified (bpt);
14998
d248b706
KY
14999 if (target_supports_enable_disable_tracepoint ()
15000 && current_trace_status ()->running && is_tracepoint (bpt))
15001 {
15002 struct bp_location *location;
15003
15004 for (location = bpt->loc; location; location = location->next)
15005 target_enable_tracepoint (location);
15006 }
15007
b4c291bb 15008 bpt->disposition = disposition;
816338b5 15009 bpt->enable_count = count;
b60e7edf 15010 update_global_location_list (1);
9c97429f 15011
8d3788bd 15012 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15013}
15014
fe3f5fa8 15015
c906108c 15016void
fba45db2 15017enable_breakpoint (struct breakpoint *bpt)
c906108c 15018{
816338b5 15019 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
15020}
15021
15022static void
15023do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15024{
15025 enable_breakpoint (bpt);
c906108c
SS
15026}
15027
95a42b64
TT
15028/* A callback for map_breakpoint_numbers that calls
15029 enable_breakpoint. */
15030
15031static void
15032do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15033{
51be5b68 15034 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15035}
15036
c906108c
SS
15037/* The enable command enables the specified breakpoints (or all defined
15038 breakpoints) so they once again become (or continue to be) effective
1272ad14 15039 in stopping the inferior. */
c906108c 15040
c906108c 15041static void
fba45db2 15042enable_command (char *args, int from_tty)
c906108c 15043{
c906108c 15044 if (args == 0)
46c6471b
PA
15045 {
15046 struct breakpoint *bpt;
15047
15048 ALL_BREAKPOINTS (bpt)
15049 if (user_breakpoint_p (bpt))
15050 enable_breakpoint (bpt);
15051 }
9eaabc75 15052 else
0d381245 15053 {
9eaabc75
MW
15054 char *num = extract_arg (&args);
15055
15056 while (num)
d248b706 15057 {
9eaabc75 15058 if (strchr (num, '.'))
b775012e 15059 {
9eaabc75
MW
15060 struct bp_location *loc = find_location_by_number (num);
15061
15062 if (loc)
15063 {
15064 if (!loc->enabled)
15065 {
15066 loc->enabled = 1;
15067 mark_breakpoint_location_modified (loc);
15068 }
15069 if (target_supports_enable_disable_tracepoint ()
15070 && current_trace_status ()->running && loc->owner
15071 && is_tracepoint (loc->owner))
15072 target_enable_tracepoint (loc);
15073 }
15074 update_global_location_list (1);
b775012e 15075 }
9eaabc75
MW
15076 else
15077 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15078 num = extract_arg (&args);
d248b706 15079 }
0d381245 15080 }
c906108c
SS
15081}
15082
816338b5
SS
15083/* This struct packages up disposition data for application to multiple
15084 breakpoints. */
15085
15086struct disp_data
15087{
15088 enum bpdisp disp;
15089 int count;
15090};
15091
c906108c 15092static void
51be5b68
PA
15093do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15094{
816338b5 15095 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15096
816338b5 15097 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15098}
15099
15100static void
15101do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15102{
816338b5 15103 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15104
15105 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15106}
15107
c906108c 15108static void
fba45db2 15109enable_once_command (char *args, int from_tty)
c906108c 15110{
51be5b68 15111 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15112}
15113
816338b5
SS
15114static void
15115do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15116{
15117 struct disp_data disp = { disp_disable, *(int *) countptr };
15118
15119 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15120}
15121
15122static void
15123enable_count_command (char *args, int from_tty)
15124{
15125 int count = get_number (&args);
15126
15127 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15128}
15129
c906108c 15130static void
51be5b68 15131do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15132{
816338b5 15133 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15134
15135 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15136}
15137
c906108c 15138static void
fba45db2 15139enable_delete_command (char *args, int from_tty)
c906108c 15140{
51be5b68 15141 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15142}
15143\f
fa8d40ab
JJ
15144static void
15145set_breakpoint_cmd (char *args, int from_tty)
15146{
15147}
15148
15149static void
15150show_breakpoint_cmd (char *args, int from_tty)
15151{
15152}
15153
1f3b5d1b
PP
15154/* Invalidate last known value of any hardware watchpoint if
15155 the memory which that value represents has been written to by
15156 GDB itself. */
15157
15158static void
8de0566d
YQ
15159invalidate_bp_value_on_memory_change (struct inferior *inferior,
15160 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15161 const bfd_byte *data)
15162{
15163 struct breakpoint *bp;
15164
15165 ALL_BREAKPOINTS (bp)
15166 if (bp->enable_state == bp_enabled
3a5c3e22 15167 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15168 {
3a5c3e22 15169 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15170
3a5c3e22
PA
15171 if (wp->val_valid && wp->val)
15172 {
15173 struct bp_location *loc;
15174
15175 for (loc = bp->loc; loc != NULL; loc = loc->next)
15176 if (loc->loc_type == bp_loc_hardware_watchpoint
15177 && loc->address + loc->length > addr
15178 && addr + len > loc->address)
15179 {
15180 value_free (wp->val);
15181 wp->val = NULL;
15182 wp->val_valid = 0;
15183 }
15184 }
1f3b5d1b
PP
15185 }
15186}
15187
8181d85f
DJ
15188/* Create and insert a raw software breakpoint at PC. Return an
15189 identifier, which should be used to remove the breakpoint later.
15190 In general, places which call this should be using something on the
15191 breakpoint chain instead; this function should be eliminated
15192 someday. */
15193
15194void *
6c95b8df
PA
15195deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
15196 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
15197{
15198 struct bp_target_info *bp_tgt;
ef370185 15199 struct bp_location *bl;
8181d85f 15200
41bf6aca 15201 bp_tgt = XCNEW (struct bp_target_info);
8181d85f 15202
6c95b8df 15203 bp_tgt->placed_address_space = aspace;
8181d85f 15204 bp_tgt->placed_address = pc;
6c95b8df 15205
ef370185
JB
15206 /* If an unconditional non-raw breakpoint is already inserted at
15207 that location, there's no need to insert another. However, with
15208 target-side evaluation of breakpoint conditions, if the
15209 breakpoint that is currently inserted on the target is
15210 conditional, we need to make it unconditional. Note that a
15211 breakpoint with target-side commands is not reported even if
15212 unconditional, so we need to remove the commands from the target
15213 as well. */
15214 bl = find_non_raw_software_breakpoint_inserted_here (aspace, pc);
15215 if (bl != NULL
15216 && VEC_empty (agent_expr_p, bl->target_info.conditions)
15217 && VEC_empty (agent_expr_p, bl->target_info.tcommands))
15218 {
15219 bp_target_info_copy_insertion_state (bp_tgt, &bl->target_info);
15220 return bp_tgt;
15221 }
15222
a6d9a66e 15223 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
15224 {
15225 /* Could not insert the breakpoint. */
15226 xfree (bp_tgt);
15227 return NULL;
15228 }
15229
15230 return bp_tgt;
15231}
15232
4a64f543
MS
15233/* Remove a breakpoint BP inserted by
15234 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
15235
15236int
a6d9a66e 15237deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
15238{
15239 struct bp_target_info *bp_tgt = bp;
ef370185
JB
15240 struct address_space *aspace = bp_tgt->placed_address_space;
15241 CORE_ADDR address = bp_tgt->placed_address;
15242 struct bp_location *bl;
8181d85f
DJ
15243 int ret;
15244
ef370185
JB
15245 bl = find_non_raw_software_breakpoint_inserted_here (aspace, address);
15246
15247 /* Only remove the raw breakpoint if there are no other non-raw
15248 breakpoints still inserted at this location. Otherwise, we would
15249 be effectively disabling those breakpoints. */
15250 if (bl == NULL)
15251 ret = target_remove_breakpoint (gdbarch, bp_tgt);
15252 else if (!VEC_empty (agent_expr_p, bl->target_info.conditions)
15253 || !VEC_empty (agent_expr_p, bl->target_info.tcommands))
15254 {
15255 /* The target is evaluating conditions, and when we inserted the
15256 software single-step breakpoint, we had made the breakpoint
15257 unconditional and command-less on the target side. Reinsert
15258 to restore the conditions/commands. */
15259 ret = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
15260 }
15261 else
15262 ret = 0;
15263
8181d85f
DJ
15264 xfree (bp_tgt);
15265
15266 return ret;
15267}
15268
8181d85f
DJ
15269/* Create and insert a breakpoint for software single step. */
15270
15271void
6c95b8df 15272insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15273 struct address_space *aspace,
15274 CORE_ADDR next_pc)
8181d85f
DJ
15275{
15276 void **bpt_p;
15277
15278 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
15279 {
15280 bpt_p = &single_step_breakpoints[0];
15281 single_step_gdbarch[0] = gdbarch;
15282 }
8181d85f
DJ
15283 else
15284 {
15285 gdb_assert (single_step_breakpoints[1] == NULL);
15286 bpt_p = &single_step_breakpoints[1];
a6d9a66e 15287 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
15288 }
15289
4a64f543
MS
15290 /* NOTE drow/2006-04-11: A future improvement to this function would
15291 be to only create the breakpoints once, and actually put them on
15292 the breakpoint chain. That would let us use set_raw_breakpoint.
15293 We could adjust the addresses each time they were needed. Doing
15294 this requires corresponding changes elsewhere where single step
15295 breakpoints are handled, however. So, for now, we use this. */
8181d85f 15296
6c95b8df 15297 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 15298 if (*bpt_p == NULL)
5af949e3
UW
15299 error (_("Could not insert single-step breakpoint at %s"),
15300 paddress (gdbarch, next_pc));
8181d85f
DJ
15301}
15302
f02253f1
HZ
15303/* Check if the breakpoints used for software single stepping
15304 were inserted or not. */
15305
15306int
15307single_step_breakpoints_inserted (void)
15308{
15309 return (single_step_breakpoints[0] != NULL
15310 || single_step_breakpoints[1] != NULL);
15311}
15312
8181d85f
DJ
15313/* Remove and delete any breakpoints used for software single step. */
15314
15315void
15316remove_single_step_breakpoints (void)
15317{
15318 gdb_assert (single_step_breakpoints[0] != NULL);
15319
15320 /* See insert_single_step_breakpoint for more about this deprecated
15321 call. */
a6d9a66e
UW
15322 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15323 single_step_breakpoints[0]);
15324 single_step_gdbarch[0] = NULL;
8181d85f
DJ
15325 single_step_breakpoints[0] = NULL;
15326
15327 if (single_step_breakpoints[1] != NULL)
15328 {
a6d9a66e
UW
15329 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15330 single_step_breakpoints[1]);
15331 single_step_gdbarch[1] = NULL;
8181d85f
DJ
15332 single_step_breakpoints[1] = NULL;
15333 }
15334}
15335
d03285ec
UW
15336/* Delete software single step breakpoints without removing them from
15337 the inferior. This is intended to be used if the inferior's address
15338 space where they were inserted is already gone, e.g. after exit or
15339 exec. */
15340
15341void
15342cancel_single_step_breakpoints (void)
15343{
15344 int i;
15345
15346 for (i = 0; i < 2; i++)
15347 if (single_step_breakpoints[i])
15348 {
15349 xfree (single_step_breakpoints[i]);
15350 single_step_breakpoints[i] = NULL;
15351 single_step_gdbarch[i] = NULL;
15352 }
15353}
15354
15355/* Detach software single-step breakpoints from INFERIOR_PTID without
15356 removing them. */
15357
15358static void
15359detach_single_step_breakpoints (void)
15360{
15361 int i;
15362
15363 for (i = 0; i < 2; i++)
15364 if (single_step_breakpoints[i])
15365 target_remove_breakpoint (single_step_gdbarch[i],
15366 single_step_breakpoints[i]);
15367}
15368
ef370185
JB
15369/* Find the software single-step breakpoint that inserted at PC.
15370 Returns its slot if found, and -1 if not found. */
1aafd4da 15371
ef370185
JB
15372static int
15373find_single_step_breakpoint (struct address_space *aspace,
15374 CORE_ADDR pc)
1aafd4da
UW
15375{
15376 int i;
15377
15378 for (i = 0; i < 2; i++)
15379 {
15380 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
15381 if (bp_tgt
15382 && breakpoint_address_match (bp_tgt->placed_address_space,
15383 bp_tgt->placed_address,
15384 aspace, pc))
ef370185 15385 return i;
1aafd4da
UW
15386 }
15387
ef370185
JB
15388 return -1;
15389}
15390
15391/* Check whether a software single-step breakpoint is inserted at
15392 PC. */
15393
15394int
15395single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15396 CORE_ADDR pc)
15397{
15398 return find_single_step_breakpoint (aspace, pc) >= 0;
1aafd4da
UW
15399}
15400
a96d9b2e
SDJ
15401/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15402 non-zero otherwise. */
15403static int
15404is_syscall_catchpoint_enabled (struct breakpoint *bp)
15405{
15406 if (syscall_catchpoint_p (bp)
15407 && bp->enable_state != bp_disabled
15408 && bp->enable_state != bp_call_disabled)
15409 return 1;
15410 else
15411 return 0;
15412}
15413
15414int
15415catch_syscall_enabled (void)
15416{
fa3064dd
YQ
15417 struct catch_syscall_inferior_data *inf_data
15418 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15419
fa3064dd 15420 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15421}
15422
15423int
15424catching_syscall_number (int syscall_number)
15425{
15426 struct breakpoint *bp;
15427
15428 ALL_BREAKPOINTS (bp)
15429 if (is_syscall_catchpoint_enabled (bp))
15430 {
be5c67c1
PA
15431 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15432
15433 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15434 {
15435 int i, iter;
15436 for (i = 0;
be5c67c1 15437 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15438 i++)
15439 if (syscall_number == iter)
15440 return 1;
15441 }
15442 else
15443 return 1;
15444 }
15445
15446 return 0;
15447}
15448
15449/* Complete syscall names. Used by "catch syscall". */
49c4e619 15450static VEC (char_ptr) *
a96d9b2e 15451catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15452 const char *text, const char *word)
a96d9b2e
SDJ
15453{
15454 const char **list = get_syscall_names ();
49c4e619 15455 VEC (char_ptr) *retlist
b45627a0 15456 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15457
c38eea1a
MS
15458 xfree (list);
15459 return retlist;
a96d9b2e
SDJ
15460}
15461
1042e4c0
SS
15462/* Tracepoint-specific operations. */
15463
15464/* Set tracepoint count to NUM. */
15465static void
15466set_tracepoint_count (int num)
15467{
15468 tracepoint_count = num;
4fa62494 15469 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15470}
15471
70221824 15472static void
1042e4c0
SS
15473trace_command (char *arg, int from_tty)
15474{
55aa24fb
SDJ
15475 struct breakpoint_ops *ops;
15476 const char *arg_cp = arg;
15477
15478 if (arg && probe_linespec_to_ops (&arg_cp))
15479 ops = &tracepoint_probe_breakpoint_ops;
15480 else
15481 ops = &tracepoint_breakpoint_ops;
15482
558a9d82
YQ
15483 create_breakpoint (get_current_arch (),
15484 arg,
15485 NULL, 0, NULL, 1 /* parse arg */,
15486 0 /* tempflag */,
15487 bp_tracepoint /* type_wanted */,
15488 0 /* Ignore count */,
15489 pending_break_support,
15490 ops,
15491 from_tty,
15492 1 /* enabled */,
15493 0 /* internal */, 0);
1042e4c0
SS
15494}
15495
70221824 15496static void
7a697b8d
SS
15497ftrace_command (char *arg, int from_tty)
15498{
558a9d82
YQ
15499 create_breakpoint (get_current_arch (),
15500 arg,
15501 NULL, 0, NULL, 1 /* parse arg */,
15502 0 /* tempflag */,
15503 bp_fast_tracepoint /* type_wanted */,
15504 0 /* Ignore count */,
15505 pending_break_support,
15506 &tracepoint_breakpoint_ops,
15507 from_tty,
15508 1 /* enabled */,
15509 0 /* internal */, 0);
0fb4aa4b
PA
15510}
15511
15512/* strace command implementation. Creates a static tracepoint. */
15513
70221824 15514static void
0fb4aa4b
PA
15515strace_command (char *arg, int from_tty)
15516{
983af33b
SDJ
15517 struct breakpoint_ops *ops;
15518
15519 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15520 or with a normal static tracepoint. */
15521 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15522 ops = &strace_marker_breakpoint_ops;
15523 else
15524 ops = &tracepoint_breakpoint_ops;
15525
558a9d82
YQ
15526 create_breakpoint (get_current_arch (),
15527 arg,
15528 NULL, 0, NULL, 1 /* parse arg */,
15529 0 /* tempflag */,
15530 bp_static_tracepoint /* type_wanted */,
15531 0 /* Ignore count */,
15532 pending_break_support,
15533 ops,
15534 from_tty,
15535 1 /* enabled */,
15536 0 /* internal */, 0);
7a697b8d
SS
15537}
15538
409873ef
SS
15539/* Set up a fake reader function that gets command lines from a linked
15540 list that was acquired during tracepoint uploading. */
15541
15542static struct uploaded_tp *this_utp;
3149d8c1 15543static int next_cmd;
409873ef
SS
15544
15545static char *
15546read_uploaded_action (void)
15547{
15548 char *rslt;
15549
3149d8c1 15550 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15551
3149d8c1 15552 next_cmd++;
409873ef
SS
15553
15554 return rslt;
15555}
15556
00bf0b85
SS
15557/* Given information about a tracepoint as recorded on a target (which
15558 can be either a live system or a trace file), attempt to create an
15559 equivalent GDB tracepoint. This is not a reliable process, since
15560 the target does not necessarily have all the information used when
15561 the tracepoint was originally defined. */
15562
d9b3f62e 15563struct tracepoint *
00bf0b85 15564create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15565{
409873ef 15566 char *addr_str, small_buf[100];
d9b3f62e 15567 struct tracepoint *tp;
fd9b8c24 15568
409873ef
SS
15569 if (utp->at_string)
15570 addr_str = utp->at_string;
15571 else
15572 {
15573 /* In the absence of a source location, fall back to raw
15574 address. Since there is no way to confirm that the address
15575 means the same thing as when the trace was started, warn the
15576 user. */
3e43a32a
MS
15577 warning (_("Uploaded tracepoint %d has no "
15578 "source location, using raw address"),
409873ef 15579 utp->number);
8c042590 15580 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15581 addr_str = small_buf;
15582 }
15583
15584 /* There's not much we can do with a sequence of bytecodes. */
15585 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15586 warning (_("Uploaded tracepoint %d condition "
15587 "has no source form, ignoring it"),
409873ef 15588 utp->number);
d5551862 15589
8cdf0e15 15590 if (!create_breakpoint (get_current_arch (),
409873ef 15591 addr_str,
e7e0cddf
SS
15592 utp->cond_string, -1, NULL,
15593 0 /* parse cond/thread */,
8cdf0e15 15594 0 /* tempflag */,
0fb4aa4b 15595 utp->type /* type_wanted */,
8cdf0e15
VP
15596 0 /* Ignore count */,
15597 pending_break_support,
348d480f 15598 &tracepoint_breakpoint_ops,
8cdf0e15 15599 0 /* from_tty */,
84f4c1fe 15600 utp->enabled /* enabled */,
44f238bb
PA
15601 0 /* internal */,
15602 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15603 return NULL;
15604
409873ef 15605 /* Get the tracepoint we just created. */
fd9b8c24
PA
15606 tp = get_tracepoint (tracepoint_count);
15607 gdb_assert (tp != NULL);
d5551862 15608
00bf0b85
SS
15609 if (utp->pass > 0)
15610 {
8c042590
PM
15611 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15612 tp->base.number);
00bf0b85 15613
409873ef 15614 trace_pass_command (small_buf, 0);
00bf0b85
SS
15615 }
15616
409873ef
SS
15617 /* If we have uploaded versions of the original commands, set up a
15618 special-purpose "reader" function and call the usual command line
15619 reader, then pass the result to the breakpoint command-setting
15620 function. */
3149d8c1 15621 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15622 {
409873ef 15623 struct command_line *cmd_list;
00bf0b85 15624
409873ef 15625 this_utp = utp;
3149d8c1 15626 next_cmd = 0;
d5551862 15627
409873ef
SS
15628 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15629
d9b3f62e 15630 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15631 }
3149d8c1
SS
15632 else if (!VEC_empty (char_ptr, utp->actions)
15633 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15634 warning (_("Uploaded tracepoint %d actions "
15635 "have no source form, ignoring them"),
409873ef 15636 utp->number);
00bf0b85 15637
f196051f
SS
15638 /* Copy any status information that might be available. */
15639 tp->base.hit_count = utp->hit_count;
15640 tp->traceframe_usage = utp->traceframe_usage;
15641
00bf0b85 15642 return tp;
d9b3f62e 15643}
00bf0b85 15644
1042e4c0
SS
15645/* Print information on tracepoint number TPNUM_EXP, or all if
15646 omitted. */
15647
15648static void
e5a67952 15649tracepoints_info (char *args, int from_tty)
1042e4c0 15650{
79a45e25 15651 struct ui_out *uiout = current_uiout;
e5a67952 15652 int num_printed;
1042e4c0 15653
e5a67952 15654 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15655
15656 if (num_printed == 0)
1042e4c0 15657 {
e5a67952 15658 if (args == NULL || *args == '\0')
d77f58be
SS
15659 ui_out_message (uiout, 0, "No tracepoints.\n");
15660 else
e5a67952 15661 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15662 }
ad443146
SS
15663
15664 default_collect_info ();
1042e4c0
SS
15665}
15666
4a64f543 15667/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15668 Not supported by all targets. */
15669static void
15670enable_trace_command (char *args, int from_tty)
15671{
15672 enable_command (args, from_tty);
15673}
15674
4a64f543 15675/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15676 Not supported by all targets. */
15677static void
15678disable_trace_command (char *args, int from_tty)
15679{
15680 disable_command (args, from_tty);
15681}
15682
4a64f543 15683/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15684static void
15685delete_trace_command (char *arg, int from_tty)
15686{
35df4500 15687 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15688
15689 dont_repeat ();
15690
15691 if (arg == 0)
15692 {
15693 int breaks_to_delete = 0;
15694
15695 /* Delete all breakpoints if no argument.
15696 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15697 have to be deleted with an explicit breakpoint number
15698 argument. */
1042e4c0 15699 ALL_TRACEPOINTS (b)
46c6471b 15700 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15701 {
15702 breaks_to_delete = 1;
15703 break;
15704 }
1042e4c0
SS
15705
15706 /* Ask user only if there are some breakpoints to delete. */
15707 if (!from_tty
15708 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15709 {
35df4500 15710 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15711 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15712 delete_breakpoint (b);
1042e4c0
SS
15713 }
15714 }
15715 else
51be5b68 15716 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15717}
15718
197f0a60
TT
15719/* Helper function for trace_pass_command. */
15720
15721static void
d9b3f62e 15722trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15723{
d9b3f62e 15724 tp->pass_count = count;
6f6484cd 15725 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15726 if (from_tty)
15727 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15728 tp->base.number, count);
197f0a60
TT
15729}
15730
1042e4c0
SS
15731/* Set passcount for tracepoint.
15732
15733 First command argument is passcount, second is tracepoint number.
15734 If tracepoint number omitted, apply to most recently defined.
15735 Also accepts special argument "all". */
15736
15737static void
15738trace_pass_command (char *args, int from_tty)
15739{
d9b3f62e 15740 struct tracepoint *t1;
1042e4c0 15741 unsigned int count;
1042e4c0
SS
15742
15743 if (args == 0 || *args == 0)
3e43a32a
MS
15744 error (_("passcount command requires an "
15745 "argument (count + optional TP num)"));
1042e4c0 15746
4a64f543 15747 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15748
529480d0 15749 args = skip_spaces (args);
1042e4c0
SS
15750 if (*args && strncasecmp (args, "all", 3) == 0)
15751 {
d9b3f62e
PA
15752 struct breakpoint *b;
15753
1042e4c0 15754 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15755 if (*args)
15756 error (_("Junk at end of arguments."));
1042e4c0 15757
d9b3f62e 15758 ALL_TRACEPOINTS (b)
197f0a60 15759 {
d9b3f62e 15760 t1 = (struct tracepoint *) b;
197f0a60
TT
15761 trace_pass_set_count (t1, count, from_tty);
15762 }
15763 }
15764 else if (*args == '\0')
1042e4c0 15765 {
5fa1d40e 15766 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15767 if (t1)
197f0a60
TT
15768 trace_pass_set_count (t1, count, from_tty);
15769 }
15770 else
15771 {
15772 struct get_number_or_range_state state;
15773
15774 init_number_or_range (&state, args);
15775 while (!state.finished)
1042e4c0 15776 {
5fa1d40e 15777 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15778 if (t1)
15779 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15780 }
15781 }
1042e4c0
SS
15782}
15783
d9b3f62e 15784struct tracepoint *
1042e4c0
SS
15785get_tracepoint (int num)
15786{
15787 struct breakpoint *t;
15788
15789 ALL_TRACEPOINTS (t)
15790 if (t->number == num)
d9b3f62e 15791 return (struct tracepoint *) t;
1042e4c0
SS
15792
15793 return NULL;
15794}
15795
d5551862
SS
15796/* Find the tracepoint with the given target-side number (which may be
15797 different from the tracepoint number after disconnecting and
15798 reconnecting). */
15799
d9b3f62e 15800struct tracepoint *
d5551862
SS
15801get_tracepoint_by_number_on_target (int num)
15802{
d9b3f62e 15803 struct breakpoint *b;
d5551862 15804
d9b3f62e
PA
15805 ALL_TRACEPOINTS (b)
15806 {
15807 struct tracepoint *t = (struct tracepoint *) b;
15808
15809 if (t->number_on_target == num)
15810 return t;
15811 }
d5551862
SS
15812
15813 return NULL;
15814}
15815
1042e4c0 15816/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15817 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15818 If the argument is missing, the most recent tracepoint
15819 (tracepoint_count) is returned. */
15820
d9b3f62e 15821struct tracepoint *
197f0a60 15822get_tracepoint_by_number (char **arg,
5fa1d40e 15823 struct get_number_or_range_state *state)
1042e4c0 15824{
1042e4c0
SS
15825 struct breakpoint *t;
15826 int tpnum;
15827 char *instring = arg == NULL ? NULL : *arg;
15828
197f0a60
TT
15829 if (state)
15830 {
15831 gdb_assert (!state->finished);
15832 tpnum = get_number_or_range (state);
15833 }
15834 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15835 tpnum = tracepoint_count;
1042e4c0 15836 else
197f0a60 15837 tpnum = get_number (arg);
1042e4c0
SS
15838
15839 if (tpnum <= 0)
15840 {
15841 if (instring && *instring)
15842 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15843 instring);
15844 else
5fa1d40e 15845 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15846 return NULL;
15847 }
15848
15849 ALL_TRACEPOINTS (t)
15850 if (t->number == tpnum)
15851 {
d9b3f62e 15852 return (struct tracepoint *) t;
1042e4c0
SS
15853 }
15854
1042e4c0
SS
15855 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15856 return NULL;
15857}
15858
d9b3f62e
PA
15859void
15860print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15861{
15862 if (b->thread != -1)
15863 fprintf_unfiltered (fp, " thread %d", b->thread);
15864
15865 if (b->task != 0)
15866 fprintf_unfiltered (fp, " task %d", b->task);
15867
15868 fprintf_unfiltered (fp, "\n");
15869}
15870
6149aea9
PA
15871/* Save information on user settable breakpoints (watchpoints, etc) to
15872 a new script file named FILENAME. If FILTER is non-NULL, call it
15873 on each breakpoint and only include the ones for which it returns
15874 non-zero. */
15875
1042e4c0 15876static void
6149aea9
PA
15877save_breakpoints (char *filename, int from_tty,
15878 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15879{
15880 struct breakpoint *tp;
6149aea9 15881 int any = 0;
1042e4c0 15882 struct cleanup *cleanup;
a7bdde9e 15883 struct ui_file *fp;
6149aea9 15884 int extra_trace_bits = 0;
1042e4c0 15885
6149aea9
PA
15886 if (filename == 0 || *filename == 0)
15887 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15888
15889 /* See if we have anything to save. */
6149aea9 15890 ALL_BREAKPOINTS (tp)
1042e4c0 15891 {
6149aea9 15892 /* Skip internal and momentary breakpoints. */
09d682a4 15893 if (!user_breakpoint_p (tp))
6149aea9
PA
15894 continue;
15895
15896 /* If we have a filter, only save the breakpoints it accepts. */
15897 if (filter && !filter (tp))
15898 continue;
15899
15900 any = 1;
15901
15902 if (is_tracepoint (tp))
15903 {
15904 extra_trace_bits = 1;
15905
15906 /* We can stop searching. */
15907 break;
15908 }
1042e4c0 15909 }
6149aea9
PA
15910
15911 if (!any)
1042e4c0 15912 {
6149aea9 15913 warning (_("Nothing to save."));
1042e4c0
SS
15914 return;
15915 }
15916
c718be47
PA
15917 filename = tilde_expand (filename);
15918 cleanup = make_cleanup (xfree, filename);
15919 fp = gdb_fopen (filename, "w");
059fb39f 15920 if (!fp)
6149aea9
PA
15921 error (_("Unable to open file '%s' for saving (%s)"),
15922 filename, safe_strerror (errno));
a7bdde9e 15923 make_cleanup_ui_file_delete (fp);
8bf6485c 15924
6149aea9
PA
15925 if (extra_trace_bits)
15926 save_trace_state_variables (fp);
8bf6485c 15927
6149aea9 15928 ALL_BREAKPOINTS (tp)
1042e4c0 15929 {
6149aea9 15930 /* Skip internal and momentary breakpoints. */
09d682a4 15931 if (!user_breakpoint_p (tp))
6149aea9 15932 continue;
8bf6485c 15933
6149aea9
PA
15934 /* If we have a filter, only save the breakpoints it accepts. */
15935 if (filter && !filter (tp))
15936 continue;
15937
348d480f 15938 tp->ops->print_recreate (tp, fp);
1042e4c0 15939
6149aea9
PA
15940 /* Note, we can't rely on tp->number for anything, as we can't
15941 assume the recreated breakpoint numbers will match. Use $bpnum
15942 instead. */
15943
15944 if (tp->cond_string)
15945 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15946
15947 if (tp->ignore_count)
15948 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15949
2d9442cc 15950 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15951 {
a7bdde9e
VP
15952 volatile struct gdb_exception ex;
15953
6149aea9 15954 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15955
79a45e25 15956 ui_out_redirect (current_uiout, fp);
14dba4b4 15957 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15958 {
79a45e25 15959 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15960 }
79a45e25 15961 ui_out_redirect (current_uiout, NULL);
1042e4c0 15962
a7bdde9e
VP
15963 if (ex.reason < 0)
15964 throw_exception (ex);
1042e4c0 15965
a7bdde9e 15966 fprintf_unfiltered (fp, " end\n");
1042e4c0 15967 }
6149aea9
PA
15968
15969 if (tp->enable_state == bp_disabled)
15970 fprintf_unfiltered (fp, "disable\n");
15971
15972 /* If this is a multi-location breakpoint, check if the locations
15973 should be individually disabled. Watchpoint locations are
15974 special, and not user visible. */
15975 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15976 {
15977 struct bp_location *loc;
15978 int n = 1;
15979
15980 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15981 if (!loc->enabled)
15982 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15983 }
1042e4c0 15984 }
8bf6485c 15985
6149aea9 15986 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15987 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15988
1042e4c0 15989 if (from_tty)
6149aea9 15990 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15991 do_cleanups (cleanup);
6149aea9
PA
15992}
15993
15994/* The `save breakpoints' command. */
15995
15996static void
15997save_breakpoints_command (char *args, int from_tty)
15998{
15999 save_breakpoints (args, from_tty, NULL);
16000}
16001
16002/* The `save tracepoints' command. */
16003
16004static void
16005save_tracepoints_command (char *args, int from_tty)
16006{
16007 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
16008}
16009
16010/* Create a vector of all tracepoints. */
16011
16012VEC(breakpoint_p) *
eeae04df 16013all_tracepoints (void)
1042e4c0
SS
16014{
16015 VEC(breakpoint_p) *tp_vec = 0;
16016 struct breakpoint *tp;
16017
16018 ALL_TRACEPOINTS (tp)
16019 {
16020 VEC_safe_push (breakpoint_p, tp_vec, tp);
16021 }
16022
16023 return tp_vec;
16024}
16025
c906108c 16026\f
4a64f543
MS
16027/* This help string is used for the break, hbreak, tbreak and thbreak
16028 commands. It is defined as a macro to prevent duplication.
16029 COMMAND should be a string constant containing the name of the
16030 command. */
31e2b00f 16031#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
16032command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
16033PROBE_MODIFIER shall be present if the command is to be placed in a\n\
16034probe point. Accepted values are `-probe' (for a generic, automatically\n\
16035guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
16036LOCATION may be a line number, function name, or \"*\" and an address.\n\
16037If a line number is specified, break at start of code for that line.\n\
16038If a function is specified, break at start of code for that function.\n\
16039If an address is specified, break at that exact address.\n\
dc10affe
PA
16040With no LOCATION, uses current execution address of the selected\n\
16041stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
16042\n\
16043THREADNUM is the number from \"info threads\".\n\
16044CONDITION is a boolean expression.\n\
16045\n\
d41c0fc8
PA
16046Multiple breakpoints at one place are permitted, and useful if their\n\
16047conditions are different.\n\
31e2b00f
AS
16048\n\
16049Do \"help breakpoints\" for info on other commands dealing with breakpoints."
16050
44feb3ce
TT
16051/* List of subcommands for "catch". */
16052static struct cmd_list_element *catch_cmdlist;
16053
16054/* List of subcommands for "tcatch". */
16055static struct cmd_list_element *tcatch_cmdlist;
16056
9ac4176b 16057void
44feb3ce
TT
16058add_catch_command (char *name, char *docstring,
16059 void (*sfunc) (char *args, int from_tty,
16060 struct cmd_list_element *command),
625e8578 16061 completer_ftype *completer,
44feb3ce
TT
16062 void *user_data_catch,
16063 void *user_data_tcatch)
16064{
16065 struct cmd_list_element *command;
16066
16067 command = add_cmd (name, class_breakpoint, NULL, docstring,
16068 &catch_cmdlist);
16069 set_cmd_sfunc (command, sfunc);
16070 set_cmd_context (command, user_data_catch);
a96d9b2e 16071 set_cmd_completer (command, completer);
44feb3ce
TT
16072
16073 command = add_cmd (name, class_breakpoint, NULL, docstring,
16074 &tcatch_cmdlist);
16075 set_cmd_sfunc (command, sfunc);
16076 set_cmd_context (command, user_data_tcatch);
a96d9b2e 16077 set_cmd_completer (command, completer);
44feb3ce
TT
16078}
16079
6c95b8df 16080static void
a79b8f6e 16081clear_syscall_counts (struct inferior *inf)
6c95b8df 16082{
fa3064dd
YQ
16083 struct catch_syscall_inferior_data *inf_data
16084 = get_catch_syscall_inferior_data (inf);
16085
16086 inf_data->total_syscalls_count = 0;
16087 inf_data->any_syscall_count = 0;
16088 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
16089}
16090
6149aea9
PA
16091static void
16092save_command (char *arg, int from_tty)
16093{
3e43a32a
MS
16094 printf_unfiltered (_("\"save\" must be followed by "
16095 "the name of a save subcommand.\n"));
635c7e8a 16096 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
16097}
16098
84f4c1fe
PM
16099struct breakpoint *
16100iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16101 void *data)
16102{
35df4500 16103 struct breakpoint *b, *b_tmp;
84f4c1fe 16104
35df4500 16105 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
16106 {
16107 if ((*callback) (b, data))
16108 return b;
16109 }
16110
16111 return NULL;
16112}
16113
0574c78f
GB
16114/* Zero if any of the breakpoint's locations could be a location where
16115 functions have been inlined, nonzero otherwise. */
16116
16117static int
16118is_non_inline_function (struct breakpoint *b)
16119{
16120 /* The shared library event breakpoint is set on the address of a
16121 non-inline function. */
16122 if (b->type == bp_shlib_event)
16123 return 1;
16124
16125 return 0;
16126}
16127
16128/* Nonzero if the specified PC cannot be a location where functions
16129 have been inlined. */
16130
16131int
09ac7c10
TT
16132pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16133 const struct target_waitstatus *ws)
0574c78f
GB
16134{
16135 struct breakpoint *b;
16136 struct bp_location *bl;
16137
16138 ALL_BREAKPOINTS (b)
16139 {
16140 if (!is_non_inline_function (b))
16141 continue;
16142
16143 for (bl = b->loc; bl != NULL; bl = bl->next)
16144 {
16145 if (!bl->shlib_disabled
09ac7c10 16146 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
16147 return 1;
16148 }
16149 }
16150
16151 return 0;
16152}
16153
2f202fde
JK
16154/* Remove any references to OBJFILE which is going to be freed. */
16155
16156void
16157breakpoint_free_objfile (struct objfile *objfile)
16158{
16159 struct bp_location **locp, *loc;
16160
16161 ALL_BP_LOCATIONS (loc, locp)
16162 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
16163 loc->symtab = NULL;
16164}
16165
2060206e
PA
16166void
16167initialize_breakpoint_ops (void)
16168{
16169 static int initialized = 0;
16170
16171 struct breakpoint_ops *ops;
16172
16173 if (initialized)
16174 return;
16175 initialized = 1;
16176
16177 /* The breakpoint_ops structure to be inherit by all kinds of
16178 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16179 internal and momentary breakpoints, etc.). */
16180 ops = &bkpt_base_breakpoint_ops;
16181 *ops = base_breakpoint_ops;
16182 ops->re_set = bkpt_re_set;
16183 ops->insert_location = bkpt_insert_location;
16184 ops->remove_location = bkpt_remove_location;
16185 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
16186 ops->create_sals_from_address = bkpt_create_sals_from_address;
16187 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16188 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
16189
16190 /* The breakpoint_ops structure to be used in regular breakpoints. */
16191 ops = &bkpt_breakpoint_ops;
16192 *ops = bkpt_base_breakpoint_ops;
16193 ops->re_set = bkpt_re_set;
16194 ops->resources_needed = bkpt_resources_needed;
16195 ops->print_it = bkpt_print_it;
16196 ops->print_mention = bkpt_print_mention;
16197 ops->print_recreate = bkpt_print_recreate;
16198
16199 /* Ranged breakpoints. */
16200 ops = &ranged_breakpoint_ops;
16201 *ops = bkpt_breakpoint_ops;
16202 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16203 ops->resources_needed = resources_needed_ranged_breakpoint;
16204 ops->print_it = print_it_ranged_breakpoint;
16205 ops->print_one = print_one_ranged_breakpoint;
16206 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16207 ops->print_mention = print_mention_ranged_breakpoint;
16208 ops->print_recreate = print_recreate_ranged_breakpoint;
16209
16210 /* Internal breakpoints. */
16211 ops = &internal_breakpoint_ops;
16212 *ops = bkpt_base_breakpoint_ops;
16213 ops->re_set = internal_bkpt_re_set;
16214 ops->check_status = internal_bkpt_check_status;
16215 ops->print_it = internal_bkpt_print_it;
16216 ops->print_mention = internal_bkpt_print_mention;
16217
16218 /* Momentary breakpoints. */
16219 ops = &momentary_breakpoint_ops;
16220 *ops = bkpt_base_breakpoint_ops;
16221 ops->re_set = momentary_bkpt_re_set;
16222 ops->check_status = momentary_bkpt_check_status;
16223 ops->print_it = momentary_bkpt_print_it;
16224 ops->print_mention = momentary_bkpt_print_mention;
16225
e2e4d78b
JK
16226 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16227 ops = &longjmp_breakpoint_ops;
16228 *ops = momentary_breakpoint_ops;
16229 ops->dtor = longjmp_bkpt_dtor;
16230
55aa24fb
SDJ
16231 /* Probe breakpoints. */
16232 ops = &bkpt_probe_breakpoint_ops;
16233 *ops = bkpt_breakpoint_ops;
16234 ops->insert_location = bkpt_probe_insert_location;
16235 ops->remove_location = bkpt_probe_remove_location;
16236 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16237 ops->decode_linespec = bkpt_probe_decode_linespec;
16238
2060206e
PA
16239 /* Watchpoints. */
16240 ops = &watchpoint_breakpoint_ops;
16241 *ops = base_breakpoint_ops;
3a5c3e22 16242 ops->dtor = dtor_watchpoint;
2060206e
PA
16243 ops->re_set = re_set_watchpoint;
16244 ops->insert_location = insert_watchpoint;
16245 ops->remove_location = remove_watchpoint;
16246 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16247 ops->check_status = check_status_watchpoint;
16248 ops->resources_needed = resources_needed_watchpoint;
16249 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16250 ops->print_it = print_it_watchpoint;
16251 ops->print_mention = print_mention_watchpoint;
16252 ops->print_recreate = print_recreate_watchpoint;
427cd150 16253 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16254
16255 /* Masked watchpoints. */
16256 ops = &masked_watchpoint_breakpoint_ops;
16257 *ops = watchpoint_breakpoint_ops;
16258 ops->insert_location = insert_masked_watchpoint;
16259 ops->remove_location = remove_masked_watchpoint;
16260 ops->resources_needed = resources_needed_masked_watchpoint;
16261 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16262 ops->print_it = print_it_masked_watchpoint;
16263 ops->print_one_detail = print_one_detail_masked_watchpoint;
16264 ops->print_mention = print_mention_masked_watchpoint;
16265 ops->print_recreate = print_recreate_masked_watchpoint;
16266
16267 /* Tracepoints. */
16268 ops = &tracepoint_breakpoint_ops;
16269 *ops = base_breakpoint_ops;
16270 ops->re_set = tracepoint_re_set;
16271 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16272 ops->print_one_detail = tracepoint_print_one_detail;
16273 ops->print_mention = tracepoint_print_mention;
16274 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16275 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16276 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16277 ops->decode_linespec = tracepoint_decode_linespec;
16278
55aa24fb
SDJ
16279 /* Probe tracepoints. */
16280 ops = &tracepoint_probe_breakpoint_ops;
16281 *ops = tracepoint_breakpoint_ops;
16282 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16283 ops->decode_linespec = tracepoint_probe_decode_linespec;
16284
983af33b
SDJ
16285 /* Static tracepoints with marker (`-m'). */
16286 ops = &strace_marker_breakpoint_ops;
16287 *ops = tracepoint_breakpoint_ops;
16288 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16289 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16290 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16291
16292 /* Fork catchpoints. */
16293 ops = &catch_fork_breakpoint_ops;
16294 *ops = base_breakpoint_ops;
16295 ops->insert_location = insert_catch_fork;
16296 ops->remove_location = remove_catch_fork;
16297 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16298 ops->print_it = print_it_catch_fork;
16299 ops->print_one = print_one_catch_fork;
16300 ops->print_mention = print_mention_catch_fork;
16301 ops->print_recreate = print_recreate_catch_fork;
16302
16303 /* Vfork catchpoints. */
16304 ops = &catch_vfork_breakpoint_ops;
16305 *ops = base_breakpoint_ops;
16306 ops->insert_location = insert_catch_vfork;
16307 ops->remove_location = remove_catch_vfork;
16308 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16309 ops->print_it = print_it_catch_vfork;
16310 ops->print_one = print_one_catch_vfork;
16311 ops->print_mention = print_mention_catch_vfork;
16312 ops->print_recreate = print_recreate_catch_vfork;
16313
16314 /* Exec catchpoints. */
16315 ops = &catch_exec_breakpoint_ops;
16316 *ops = base_breakpoint_ops;
16317 ops->dtor = dtor_catch_exec;
16318 ops->insert_location = insert_catch_exec;
16319 ops->remove_location = remove_catch_exec;
16320 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16321 ops->print_it = print_it_catch_exec;
16322 ops->print_one = print_one_catch_exec;
16323 ops->print_mention = print_mention_catch_exec;
16324 ops->print_recreate = print_recreate_catch_exec;
16325
16326 /* Syscall catchpoints. */
16327 ops = &catch_syscall_breakpoint_ops;
16328 *ops = base_breakpoint_ops;
16329 ops->dtor = dtor_catch_syscall;
16330 ops->insert_location = insert_catch_syscall;
16331 ops->remove_location = remove_catch_syscall;
16332 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16333 ops->print_it = print_it_catch_syscall;
16334 ops->print_one = print_one_catch_syscall;
16335 ops->print_mention = print_mention_catch_syscall;
16336 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16337
16338 /* Solib-related catchpoints. */
16339 ops = &catch_solib_breakpoint_ops;
16340 *ops = base_breakpoint_ops;
16341 ops->dtor = dtor_catch_solib;
16342 ops->insert_location = insert_catch_solib;
16343 ops->remove_location = remove_catch_solib;
16344 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16345 ops->check_status = check_status_catch_solib;
16346 ops->print_it = print_it_catch_solib;
16347 ops->print_one = print_one_catch_solib;
16348 ops->print_mention = print_mention_catch_solib;
16349 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16350
16351 ops = &dprintf_breakpoint_ops;
16352 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16353 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16354 ops->resources_needed = bkpt_resources_needed;
16355 ops->print_it = bkpt_print_it;
16356 ops->print_mention = bkpt_print_mention;
2d9442cc 16357 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16358 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16359 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16360}
16361
8bfd80db
YQ
16362/* Chain containing all defined "enable breakpoint" subcommands. */
16363
16364static struct cmd_list_element *enablebreaklist = NULL;
16365
c906108c 16366void
fba45db2 16367_initialize_breakpoint (void)
c906108c
SS
16368{
16369 struct cmd_list_element *c;
16370
2060206e
PA
16371 initialize_breakpoint_ops ();
16372
84acb35a 16373 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16374 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16375 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16376 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16377
55aa24fb
SDJ
16378 breakpoint_objfile_key
16379 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16380
fa3064dd 16381 catch_syscall_inferior_data
8e260fc0
TT
16382 = register_inferior_data_with_cleanup (NULL,
16383 catch_syscall_inferior_data_cleanup);
fa3064dd 16384
c906108c
SS
16385 breakpoint_chain = 0;
16386 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16387 before a breakpoint is set. */
16388 breakpoint_count = 0;
16389
1042e4c0
SS
16390 tracepoint_count = 0;
16391
1bedd215
AC
16392 add_com ("ignore", class_breakpoint, ignore_command, _("\
16393Set ignore-count of breakpoint number N to COUNT.\n\
16394Usage is `ignore N COUNT'."));
c906108c 16395 if (xdb_commands)
c5aa993b 16396 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16397
1bedd215
AC
16398 add_com ("commands", class_breakpoint, commands_command, _("\
16399Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16400Give breakpoint number as argument after \"commands\".\n\
16401With no argument, the targeted breakpoint is the last one set.\n\
16402The commands themselves follow starting on the next line.\n\
16403Type a line containing \"end\" to indicate the end of them.\n\
16404Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16405then no output is printed when it is hit, except what the commands print."));
c906108c 16406
d55637df 16407 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16408Specify breakpoint number N to break only if COND is true.\n\
c906108c 16409Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16410expression to be evaluated whenever breakpoint N is reached."));
d55637df 16411 set_cmd_completer (c, condition_completer);
c906108c 16412
1bedd215 16413 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16414Set a temporary breakpoint.\n\
c906108c
SS
16415Like \"break\" except the breakpoint is only temporary,\n\
16416so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16417by using \"enable delete\" on the breakpoint number.\n\
16418\n"
16419BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16420 set_cmd_completer (c, location_completer);
c94fdfd0 16421
1bedd215 16422 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16423Set a hardware assisted breakpoint.\n\
c906108c 16424Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16425some target hardware may not have this support.\n\
16426\n"
16427BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16428 set_cmd_completer (c, location_completer);
c906108c 16429
1bedd215 16430 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16431Set a temporary hardware assisted breakpoint.\n\
c906108c 16432Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16433so it will be deleted when hit.\n\
16434\n"
16435BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16436 set_cmd_completer (c, location_completer);
c906108c 16437
1bedd215
AC
16438 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16439Enable some breakpoints.\n\
c906108c
SS
16440Give breakpoint numbers (separated by spaces) as arguments.\n\
16441With no subcommand, breakpoints are enabled until you command otherwise.\n\
16442This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16443With a subcommand you can enable temporarily."),
c906108c
SS
16444 &enablelist, "enable ", 1, &cmdlist);
16445 if (xdb_commands)
1bedd215
AC
16446 add_com ("ab", class_breakpoint, enable_command, _("\
16447Enable some breakpoints.\n\
c906108c
SS
16448Give breakpoint numbers (separated by spaces) as arguments.\n\
16449With no subcommand, breakpoints are enabled until you command otherwise.\n\
16450This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16451With a subcommand you can enable temporarily."));
c906108c
SS
16452
16453 add_com_alias ("en", "enable", class_breakpoint, 1);
16454
84951ab5 16455 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16456Enable some breakpoints.\n\
c906108c
SS
16457Give breakpoint numbers (separated by spaces) as arguments.\n\
16458This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16459May be abbreviated to simply \"enable\".\n"),
c5aa993b 16460 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16461
1a966eab
AC
16462 add_cmd ("once", no_class, enable_once_command, _("\
16463Enable breakpoints for one hit. Give breakpoint numbers.\n\
16464If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16465 &enablebreaklist);
16466
1a966eab
AC
16467 add_cmd ("delete", no_class, enable_delete_command, _("\
16468Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16469If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16470 &enablebreaklist);
16471
816338b5
SS
16472 add_cmd ("count", no_class, enable_count_command, _("\
16473Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16474If a breakpoint is hit while enabled in this fashion,\n\
16475the count is decremented; when it reaches zero, the breakpoint is disabled."),
16476 &enablebreaklist);
16477
1a966eab
AC
16478 add_cmd ("delete", no_class, enable_delete_command, _("\
16479Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16480If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16481 &enablelist);
16482
1a966eab
AC
16483 add_cmd ("once", no_class, enable_once_command, _("\
16484Enable breakpoints for one hit. Give breakpoint numbers.\n\
16485If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16486 &enablelist);
16487
16488 add_cmd ("count", no_class, enable_count_command, _("\
16489Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16490If a breakpoint is hit while enabled in this fashion,\n\
16491the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16492 &enablelist);
16493
1bedd215
AC
16494 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16495Disable some breakpoints.\n\
c906108c
SS
16496Arguments are breakpoint numbers with spaces in between.\n\
16497To disable all breakpoints, give no argument.\n\
64b9b334 16498A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16499 &disablelist, "disable ", 1, &cmdlist);
16500 add_com_alias ("dis", "disable", class_breakpoint, 1);
16501 add_com_alias ("disa", "disable", class_breakpoint, 1);
16502 if (xdb_commands)
1bedd215
AC
16503 add_com ("sb", class_breakpoint, disable_command, _("\
16504Disable some breakpoints.\n\
c906108c
SS
16505Arguments are breakpoint numbers with spaces in between.\n\
16506To disable all breakpoints, give no argument.\n\
64b9b334 16507A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16508
1a966eab
AC
16509 add_cmd ("breakpoints", class_alias, disable_command, _("\
16510Disable some breakpoints.\n\
c906108c
SS
16511Arguments are breakpoint numbers with spaces in between.\n\
16512To disable all breakpoints, give no argument.\n\
64b9b334 16513A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16514This command may be abbreviated \"disable\"."),
c906108c
SS
16515 &disablelist);
16516
1bedd215
AC
16517 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16518Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16519Arguments are breakpoint numbers with spaces in between.\n\
16520To delete all breakpoints, give no argument.\n\
16521\n\
16522Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16523The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16524 &deletelist, "delete ", 1, &cmdlist);
16525 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16526 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16527 if (xdb_commands)
1bedd215
AC
16528 add_com ("db", class_breakpoint, delete_command, _("\
16529Delete some breakpoints.\n\
c906108c 16530Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16531To delete all breakpoints, give no argument.\n"));
c906108c 16532
1a966eab
AC
16533 add_cmd ("breakpoints", class_alias, delete_command, _("\
16534Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16535Arguments are breakpoint numbers with spaces in between.\n\
16536To delete all breakpoints, give no argument.\n\
1a966eab 16537This command may be abbreviated \"delete\"."),
c906108c
SS
16538 &deletelist);
16539
1bedd215
AC
16540 add_com ("clear", class_breakpoint, clear_command, _("\
16541Clear breakpoint at specified line or function.\n\
c906108c
SS
16542Argument may be line number, function name, or \"*\" and an address.\n\
16543If line number is specified, all breakpoints in that line are cleared.\n\
16544If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16545If an address is specified, breakpoints at that address are cleared.\n\
16546\n\
16547With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16548is executing in.\n\
16549\n\
1bedd215 16550See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16551 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16552
1bedd215 16553 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16554Set breakpoint at specified line or function.\n"
16555BREAK_ARGS_HELP ("break")));
5ba2abeb 16556 set_cmd_completer (c, location_completer);
c94fdfd0 16557
c906108c
SS
16558 add_com_alias ("b", "break", class_run, 1);
16559 add_com_alias ("br", "break", class_run, 1);
16560 add_com_alias ("bre", "break", class_run, 1);
16561 add_com_alias ("brea", "break", class_run, 1);
16562
7681d515
PM
16563 if (xdb_commands)
16564 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16565
16566 if (dbx_commands)
16567 {
1bedd215
AC
16568 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16569Break in function/address or break at a line in the current file."),
c5aa993b
JM
16570 &stoplist, "stop ", 1, &cmdlist);
16571 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16572 _("Break in function or address."), &stoplist);
c5aa993b 16573 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16574 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16575 add_com ("status", class_info, breakpoints_info, _("\
16576Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16577The \"Type\" column indicates one of:\n\
16578\tbreakpoint - normal breakpoint\n\
16579\twatchpoint - watchpoint\n\
16580The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16581the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16582breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16583address and file/line number respectively.\n\
16584\n\
16585Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16586are set to the address of the last breakpoint listed unless the command\n\
16587is prefixed with \"server \".\n\n\
c906108c 16588Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16589breakpoint set."));
c906108c
SS
16590 }
16591
1bedd215 16592 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16593Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16594The \"Type\" column indicates one of:\n\
16595\tbreakpoint - normal breakpoint\n\
16596\twatchpoint - watchpoint\n\
16597The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16598the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16599breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16600address and file/line number respectively.\n\
16601\n\
16602Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16603are set to the address of the last breakpoint listed unless the command\n\
16604is prefixed with \"server \".\n\n\
c906108c 16605Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16606breakpoint set."));
c906108c 16607
6b04bdb7
MS
16608 add_info_alias ("b", "breakpoints", 1);
16609
c906108c 16610 if (xdb_commands)
1bedd215
AC
16611 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16612Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16613The \"Type\" column indicates one of:\n\
16614\tbreakpoint - normal breakpoint\n\
16615\twatchpoint - watchpoint\n\
16616The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16617the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16618breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16619address and file/line number respectively.\n\
16620\n\
16621Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16622are set to the address of the last breakpoint listed unless the command\n\
16623is prefixed with \"server \".\n\n\
c906108c 16624Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16625breakpoint set."));
c906108c 16626
1a966eab
AC
16627 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16628Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16629The \"Type\" column indicates one of:\n\
16630\tbreakpoint - normal breakpoint\n\
16631\twatchpoint - watchpoint\n\
16632\tlongjmp - internal breakpoint used to step through longjmp()\n\
16633\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16634\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16635\tfinish - internal breakpoint used by the \"finish\" command\n\
16636The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16637the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16638breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16639address and file/line number respectively.\n\
16640\n\
16641Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16642are set to the address of the last breakpoint listed unless the command\n\
16643is prefixed with \"server \".\n\n\
c906108c 16644Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16645breakpoint set."),
c906108c
SS
16646 &maintenanceinfolist);
16647
44feb3ce
TT
16648 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16649Set catchpoints to catch events."),
16650 &catch_cmdlist, "catch ",
16651 0/*allow-unknown*/, &cmdlist);
16652
16653 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16654Set temporary catchpoints to catch events."),
16655 &tcatch_cmdlist, "tcatch ",
16656 0/*allow-unknown*/, &cmdlist);
16657
44feb3ce
TT
16658 add_catch_command ("fork", _("Catch calls to fork."),
16659 catch_fork_command_1,
a96d9b2e 16660 NULL,
44feb3ce
TT
16661 (void *) (uintptr_t) catch_fork_permanent,
16662 (void *) (uintptr_t) catch_fork_temporary);
16663 add_catch_command ("vfork", _("Catch calls to vfork."),
16664 catch_fork_command_1,
a96d9b2e 16665 NULL,
44feb3ce
TT
16666 (void *) (uintptr_t) catch_vfork_permanent,
16667 (void *) (uintptr_t) catch_vfork_temporary);
16668 add_catch_command ("exec", _("Catch calls to exec."),
16669 catch_exec_command_1,
a96d9b2e
SDJ
16670 NULL,
16671 CATCH_PERMANENT,
16672 CATCH_TEMPORARY);
edcc5120
TT
16673 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16674Usage: catch load [REGEX]\n\
16675If REGEX is given, only stop for libraries matching the regular expression."),
16676 catch_load_command_1,
16677 NULL,
16678 CATCH_PERMANENT,
16679 CATCH_TEMPORARY);
16680 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16681Usage: catch unload [REGEX]\n\
16682If REGEX is given, only stop for libraries matching the regular expression."),
16683 catch_unload_command_1,
16684 NULL,
16685 CATCH_PERMANENT,
16686 CATCH_TEMPORARY);
a96d9b2e
SDJ
16687 add_catch_command ("syscall", _("\
16688Catch system calls by their names and/or numbers.\n\
16689Arguments say which system calls to catch. If no arguments\n\
16690are given, every system call will be caught.\n\
16691Arguments, if given, should be one or more system call names\n\
16692(if your system supports that), or system call numbers."),
16693 catch_syscall_command_1,
16694 catch_syscall_completer,
44feb3ce
TT
16695 CATCH_PERMANENT,
16696 CATCH_TEMPORARY);
c5aa993b 16697
1bedd215
AC
16698 c = add_com ("watch", class_breakpoint, watch_command, _("\
16699Set a watchpoint for an expression.\n\
06a64a0b 16700Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16701A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16702an expression changes.\n\
16703If -l or -location is given, this evaluates EXPRESSION and watches\n\
16704the memory to which it refers."));
65d12d83 16705 set_cmd_completer (c, expression_completer);
c906108c 16706
1bedd215
AC
16707 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16708Set a read watchpoint for an expression.\n\
06a64a0b 16709Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16710A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16711an expression is read.\n\
16712If -l or -location is given, this evaluates EXPRESSION and watches\n\
16713the memory to which it refers."));
65d12d83 16714 set_cmd_completer (c, expression_completer);
c906108c 16715
1bedd215
AC
16716 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16717Set a watchpoint for an expression.\n\
06a64a0b 16718Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16719A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16720an expression is either read or written.\n\
16721If -l or -location is given, this evaluates EXPRESSION and watches\n\
16722the memory to which it refers."));
65d12d83 16723 set_cmd_completer (c, expression_completer);
c906108c 16724
d77f58be 16725 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16726Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16727
920d2a44
AC
16728 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16729 respond to changes - contrary to the description. */
85c07804
AC
16730 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16731 &can_use_hw_watchpoints, _("\
16732Set debugger's willingness to use watchpoint hardware."), _("\
16733Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16734If zero, gdb will not use hardware for new watchpoints, even if\n\
16735such is available. (However, any hardware watchpoints that were\n\
16736created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16737hardware.)"),
16738 NULL,
920d2a44 16739 show_can_use_hw_watchpoints,
85c07804 16740 &setlist, &showlist);
c906108c
SS
16741
16742 can_use_hw_watchpoints = 1;
fa8d40ab 16743
1042e4c0
SS
16744 /* Tracepoint manipulation commands. */
16745
16746 c = add_com ("trace", class_breakpoint, trace_command, _("\
16747Set a tracepoint at specified line or function.\n\
16748\n"
16749BREAK_ARGS_HELP ("trace") "\n\
16750Do \"help tracepoints\" for info on other tracepoint commands."));
16751 set_cmd_completer (c, location_completer);
16752
16753 add_com_alias ("tp", "trace", class_alias, 0);
16754 add_com_alias ("tr", "trace", class_alias, 1);
16755 add_com_alias ("tra", "trace", class_alias, 1);
16756 add_com_alias ("trac", "trace", class_alias, 1);
16757
7a697b8d
SS
16758 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16759Set a fast tracepoint at specified line or function.\n\
16760\n"
16761BREAK_ARGS_HELP ("ftrace") "\n\
16762Do \"help tracepoints\" for info on other tracepoint commands."));
16763 set_cmd_completer (c, location_completer);
16764
0fb4aa4b
PA
16765 c = add_com ("strace", class_breakpoint, strace_command, _("\
16766Set a static tracepoint at specified line, function or marker.\n\
16767\n\
16768strace [LOCATION] [if CONDITION]\n\
16769LOCATION may be a line number, function name, \"*\" and an address,\n\
16770or -m MARKER_ID.\n\
16771If a line number is specified, probe the marker at start of code\n\
16772for that line. If a function is specified, probe the marker at start\n\
16773of code for that function. If an address is specified, probe the marker\n\
16774at that exact address. If a marker id is specified, probe the marker\n\
16775with that name. With no LOCATION, uses current execution address of\n\
16776the selected stack frame.\n\
16777Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16778This collects arbitrary user data passed in the probe point call to the\n\
16779tracing library. You can inspect it when analyzing the trace buffer,\n\
16780by printing the $_sdata variable like any other convenience variable.\n\
16781\n\
16782CONDITION is a boolean expression.\n\
16783\n\
d41c0fc8
PA
16784Multiple tracepoints at one place are permitted, and useful if their\n\
16785conditions are different.\n\
0fb4aa4b
PA
16786\n\
16787Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16788Do \"help tracepoints\" for info on other tracepoint commands."));
16789 set_cmd_completer (c, location_completer);
16790
1042e4c0 16791 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16792Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16793Convenience variable \"$tpnum\" contains the number of the\n\
16794last tracepoint set."));
16795
16796 add_info_alias ("tp", "tracepoints", 1);
16797
16798 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16799Delete specified tracepoints.\n\
16800Arguments are tracepoint numbers, separated by spaces.\n\
16801No argument means delete all tracepoints."),
16802 &deletelist);
7e20dfcd 16803 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16804
16805 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16806Disable specified tracepoints.\n\
16807Arguments are tracepoint numbers, separated by spaces.\n\
16808No argument means disable all tracepoints."),
16809 &disablelist);
16810 deprecate_cmd (c, "disable");
16811
16812 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16813Enable specified tracepoints.\n\
16814Arguments are tracepoint numbers, separated by spaces.\n\
16815No argument means enable all tracepoints."),
16816 &enablelist);
16817 deprecate_cmd (c, "enable");
16818
16819 add_com ("passcount", class_trace, trace_pass_command, _("\
16820Set the passcount for a tracepoint.\n\
16821The trace will end when the tracepoint has been passed 'count' times.\n\
16822Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16823if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16824
6149aea9
PA
16825 add_prefix_cmd ("save", class_breakpoint, save_command,
16826 _("Save breakpoint definitions as a script."),
16827 &save_cmdlist, "save ",
16828 0/*allow-unknown*/, &cmdlist);
16829
16830 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16831Save current breakpoint definitions as a script.\n\
cce7e648 16832This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16833catchpoints, tracepoints). Use the 'source' command in another debug\n\
16834session to restore them."),
16835 &save_cmdlist);
16836 set_cmd_completer (c, filename_completer);
16837
16838 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16839Save current tracepoint definitions as a script.\n\
6149aea9
PA
16840Use the 'source' command in another debug session to restore them."),
16841 &save_cmdlist);
1042e4c0
SS
16842 set_cmd_completer (c, filename_completer);
16843
6149aea9
PA
16844 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16845 deprecate_cmd (c, "save tracepoints");
16846
1bedd215 16847 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16848Breakpoint specific settings\n\
16849Configure various breakpoint-specific variables such as\n\
1bedd215 16850pending breakpoint behavior"),
fa8d40ab
JJ
16851 &breakpoint_set_cmdlist, "set breakpoint ",
16852 0/*allow-unknown*/, &setlist);
1bedd215 16853 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16854Breakpoint specific settings\n\
16855Configure various breakpoint-specific variables such as\n\
1bedd215 16856pending breakpoint behavior"),
fa8d40ab
JJ
16857 &breakpoint_show_cmdlist, "show breakpoint ",
16858 0/*allow-unknown*/, &showlist);
16859
7915a72c
AC
16860 add_setshow_auto_boolean_cmd ("pending", no_class,
16861 &pending_break_support, _("\
16862Set debugger's behavior regarding pending breakpoints."), _("\
16863Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16864If on, an unrecognized breakpoint location will cause gdb to create a\n\
16865pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16866an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16867user-query to see if a pending breakpoint should be created."),
2c5b56ce 16868 NULL,
920d2a44 16869 show_pending_break_support,
6e1d7d6c
AC
16870 &breakpoint_set_cmdlist,
16871 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16872
16873 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16874
16875 add_setshow_boolean_cmd ("auto-hw", no_class,
16876 &automatic_hardware_breakpoints, _("\
16877Set automatic usage of hardware breakpoints."), _("\
16878Show automatic usage of hardware breakpoints."), _("\
16879If set, the debugger will automatically use hardware breakpoints for\n\
16880breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16881a warning will be emitted for such breakpoints."),
16882 NULL,
16883 show_automatic_hardware_breakpoints,
16884 &breakpoint_set_cmdlist,
16885 &breakpoint_show_cmdlist);
74960c60 16886
72d0e2c5
YQ
16887 add_setshow_auto_boolean_cmd ("always-inserted", class_support,
16888 &always_inserted_mode, _("\
74960c60
VP
16889Set mode for inserting breakpoints."), _("\
16890Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
16891When this mode is off, breakpoints are inserted in inferior when it is\n\
16892resumed, and removed when execution stops. When this mode is on,\n\
16893breakpoints are inserted immediately and removed only when the user\n\
16894deletes the breakpoint. When this mode is auto (which is the default),\n\
16895the behaviour depends on the non-stop setting (see help set non-stop).\n\
16896In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16897behaves as if always-inserted mode is on; if gdb is controlling the\n\
16898inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
72d0e2c5
YQ
16899 NULL,
16900 &show_always_inserted_mode,
16901 &breakpoint_set_cmdlist,
16902 &breakpoint_show_cmdlist);
f1310107 16903
b775012e
LM
16904 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16905 condition_evaluation_enums,
16906 &condition_evaluation_mode_1, _("\
16907Set mode of breakpoint condition evaluation."), _("\
16908Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16909When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16910evaluated on the host's side by GDB. When it is set to \"target\",\n\
16911breakpoint conditions will be downloaded to the target (if the target\n\
16912supports such feature) and conditions will be evaluated on the target's side.\n\
16913If this is set to \"auto\" (default), this will be automatically set to\n\
16914\"target\" if it supports condition evaluation, otherwise it will\n\
16915be set to \"gdb\""),
16916 &set_condition_evaluation_mode,
16917 &show_condition_evaluation_mode,
16918 &breakpoint_set_cmdlist,
16919 &breakpoint_show_cmdlist);
16920
f1310107
TJB
16921 add_com ("break-range", class_breakpoint, break_range_command, _("\
16922Set a breakpoint for an address range.\n\
16923break-range START-LOCATION, END-LOCATION\n\
16924where START-LOCATION and END-LOCATION can be one of the following:\n\
16925 LINENUM, for that line in the current file,\n\
16926 FILE:LINENUM, for that line in that file,\n\
16927 +OFFSET, for that number of lines after the current line\n\
16928 or the start of the range\n\
16929 FUNCTION, for the first line in that function,\n\
16930 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16931 *ADDRESS, for the instruction at that address.\n\
16932\n\
16933The breakpoint will stop execution of the inferior whenever it executes\n\
16934an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16935range (including START-LOCATION and END-LOCATION)."));
16936
e7e0cddf
SS
16937 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16938Set a dynamic printf at specified line or function.\n\
16939dprintf location,format string,arg1,arg2,...\n\
16940location may be a line number, function name, or \"*\" and an address.\n\
16941If a line number is specified, break at start of code for that line.\n\
0e4777df 16942If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16943 set_cmd_completer (c, location_completer);
16944
16945 add_setshow_enum_cmd ("dprintf-style", class_support,
16946 dprintf_style_enums, &dprintf_style, _("\
16947Set the style of usage for dynamic printf."), _("\
16948Show the style of usage for dynamic printf."), _("\
16949This setting chooses how GDB will do a dynamic printf.\n\
16950If the value is \"gdb\", then the printing is done by GDB to its own\n\
16951console, as with the \"printf\" command.\n\
16952If the value is \"call\", the print is done by calling a function in your\n\
16953program; by default printf(), but you can choose a different function or\n\
16954output stream by setting dprintf-function and dprintf-channel."),
16955 update_dprintf_commands, NULL,
16956 &setlist, &showlist);
16957
16958 dprintf_function = xstrdup ("printf");
16959 add_setshow_string_cmd ("dprintf-function", class_support,
16960 &dprintf_function, _("\
16961Set the function to use for dynamic printf"), _("\
16962Show the function to use for dynamic printf"), NULL,
16963 update_dprintf_commands, NULL,
16964 &setlist, &showlist);
16965
16966 dprintf_channel = xstrdup ("");
16967 add_setshow_string_cmd ("dprintf-channel", class_support,
16968 &dprintf_channel, _("\
16969Set the channel to use for dynamic printf"), _("\
16970Show the channel to use for dynamic printf"), NULL,
16971 update_dprintf_commands, NULL,
16972 &setlist, &showlist);
16973
d3ce09f5
SS
16974 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16975 &disconnected_dprintf, _("\
16976Set whether dprintf continues after GDB disconnects."), _("\
16977Show whether dprintf continues after GDB disconnects."), _("\
16978Use this to let dprintf commands continue to hit and produce output\n\
16979even if GDB disconnects or detaches from the target."),
16980 NULL,
16981 NULL,
16982 &setlist, &showlist);
16983
16984 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16985agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16986(target agent only) This is useful for formatted output in user-defined commands."));
16987
765dc015 16988 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16989
16990 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16991 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16992}
This page took 3.330582 seconds and 4 git commands to generate.