2012-07-02 Sergio Durigan Junior <sergiodj@redhat.com>
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
0b302171 3 Copyright (C) 1986-2012 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
35#include "gdbthread.h"
36#include "target.h"
37#include "language.h"
38#include "gdb_string.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
0225421b 50#include "gdb_assert.h"
fe898f56 51#include "block.h"
a77053c2 52#include "solib.h"
84acb35a
JJ
53#include "solist.h"
54#include "observer.h"
60250e8b 55#include "exceptions.h"
765dc015 56#include "memattr.h"
f7f9143b 57#include "ada-lang.h"
d1aa2f50 58#include "top.h"
79a45b7d 59#include "valprint.h"
4efc6507 60#include "jit.h"
a96d9b2e 61#include "xml-syscall.h"
65d79d4b 62#include "parser-defs.h"
55aa24fb
SDJ
63#include "gdb_regex.h"
64#include "probe.h"
e9cafbcc 65#include "cli/cli-utils.h"
be34f849 66#include "continuations.h"
1bfeeb0f
JL
67#include "stack.h"
68#include "skip.h"
edcc5120 69#include "gdb_regex.h"
b775012e 70#include "ax-gdb.h"
e2e4d78b 71#include "dummy-frame.h"
c906108c 72
1042e4c0
SS
73/* readline include files */
74#include "readline/readline.h"
75#include "readline/history.h"
76
77/* readline defines this. */
78#undef savestring
79
034dad6f 80#include "mi/mi-common.h"
7371cf6d 81#include "python/python.h"
104c1213 82
4a64f543 83/* Prototypes for local functions. */
c906108c 84
a14ed312 85static void enable_delete_command (char *, int);
c906108c 86
a14ed312 87static void enable_once_command (char *, int);
c906108c 88
816338b5
SS
89static void enable_count_command (char *, int);
90
a14ed312 91static void disable_command (char *, int);
c906108c 92
a14ed312 93static void enable_command (char *, int);
c906108c 94
95a42b64
TT
95static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
96 void *),
97 void *);
c906108c 98
a14ed312 99static void ignore_command (char *, int);
c906108c 100
4efb68b1 101static int breakpoint_re_set_one (void *);
c906108c 102
348d480f
PA
103static void breakpoint_re_set_default (struct breakpoint *);
104
983af33b
SDJ
105static void create_sals_from_address_default (char **,
106 struct linespec_result *,
107 enum bptype, char *,
108 char **);
109
110static void create_breakpoints_sal_default (struct gdbarch *,
111 struct linespec_result *,
112 struct linespec_sals *,
e7e0cddf 113 char *, char *, enum bptype,
983af33b
SDJ
114 enum bpdisp, int, int,
115 int,
116 const struct breakpoint_ops *,
44f238bb 117 int, int, int, unsigned);
983af33b
SDJ
118
119static void decode_linespec_default (struct breakpoint *, char **,
120 struct symtabs_and_lines *);
121
a14ed312 122static void clear_command (char *, int);
c906108c 123
a14ed312 124static void catch_command (char *, int);
c906108c 125
a9634178 126static int can_use_hardware_watchpoint (struct value *);
c906108c 127
98deb0da 128static void break_command_1 (char *, int, int);
c906108c 129
a14ed312 130static void mention (struct breakpoint *);
c906108c 131
348d480f
PA
132static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
133 enum bptype,
c0a91b2b 134 const struct breakpoint_ops *);
3742cc8b
YQ
135static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
136 const struct symtab_and_line *);
137
4a64f543
MS
138/* This function is used in gdbtk sources and thus can not be made
139 static. */
63c252f8 140struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 141 struct symtab_and_line,
c0a91b2b
TT
142 enum bptype,
143 const struct breakpoint_ops *);
c906108c 144
06edf0c0
PA
145static struct breakpoint *
146 momentary_breakpoint_from_master (struct breakpoint *orig,
147 enum bptype type,
c0a91b2b 148 const struct breakpoint_ops *ops);
06edf0c0 149
76897487
KB
150static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
151
a6d9a66e
UW
152static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
153 CORE_ADDR bpaddr,
88f7da05 154 enum bptype bptype);
76897487 155
6c95b8df
PA
156static void describe_other_breakpoints (struct gdbarch *,
157 struct program_space *, CORE_ADDR,
5af949e3 158 struct obj_section *, int);
c906108c 159
6c95b8df
PA
160static int breakpoint_address_match (struct address_space *aspace1,
161 CORE_ADDR addr1,
162 struct address_space *aspace2,
163 CORE_ADDR addr2);
164
85d721b8
PA
165static int watchpoint_locations_match (struct bp_location *loc1,
166 struct bp_location *loc2);
167
f1310107
TJB
168static int breakpoint_location_address_match (struct bp_location *bl,
169 struct address_space *aspace,
170 CORE_ADDR addr);
171
a14ed312 172static void breakpoints_info (char *, int);
c906108c 173
d77f58be
SS
174static void watchpoints_info (char *, int);
175
e5a67952
MS
176static int breakpoint_1 (char *, int,
177 int (*) (const struct breakpoint *));
c906108c 178
4efb68b1 179static int breakpoint_cond_eval (void *);
c906108c 180
4efb68b1 181static void cleanup_executing_breakpoints (void *);
c906108c 182
a14ed312 183static void commands_command (char *, int);
c906108c 184
a14ed312 185static void condition_command (char *, int);
c906108c 186
c5aa993b
JM
187typedef enum
188 {
189 mark_inserted,
190 mark_uninserted
191 }
192insertion_state_t;
c906108c 193
0bde7532 194static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 195static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 196
e514a9d6 197static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 198
4efb68b1 199static int watchpoint_check (void *);
c906108c 200
a14ed312 201static void maintenance_info_breakpoints (char *, int);
c906108c 202
a14ed312 203static int hw_breakpoint_used_count (void);
c906108c 204
a1398e0c
PA
205static int hw_watchpoint_use_count (struct breakpoint *);
206
207static int hw_watchpoint_used_count_others (struct breakpoint *except,
208 enum bptype type,
209 int *other_type_used);
c906108c 210
a14ed312 211static void hbreak_command (char *, int);
c906108c 212
a14ed312 213static void thbreak_command (char *, int);
c906108c 214
816338b5
SS
215static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
216 int count);
c906108c 217
a14ed312 218static void stop_command (char *arg, int from_tty);
7a292a7a 219
a14ed312 220static void stopin_command (char *arg, int from_tty);
7a292a7a 221
a14ed312 222static void stopat_command (char *arg, int from_tty);
7a292a7a 223
a14ed312 224static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 225
d85310f7
MS
226static void catch_exception_command_1 (enum exception_event_kind ex_event,
227 char *arg, int tempflag, int from_tty);
7a292a7a 228
a14ed312 229static void tcatch_command (char *arg, int from_tty);
7a292a7a 230
d03285ec
UW
231static void detach_single_step_breakpoints (void);
232
6c95b8df
PA
233static int single_step_breakpoint_inserted_here_p (struct address_space *,
234 CORE_ADDR pc);
1aafd4da 235
fe3f5fa8 236static void free_bp_location (struct bp_location *loc);
f431efe5
PA
237static void incref_bp_location (struct bp_location *loc);
238static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 239
39d61571 240static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 241
b60e7edf 242static void update_global_location_list (int);
a5606eee 243
b60e7edf 244static void update_global_location_list_nothrow (int);
74960c60 245
d77f58be 246static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
247
248static void insert_breakpoint_locations (void);
a5606eee 249
a96d9b2e
SDJ
250static int syscall_catchpoint_p (struct breakpoint *b);
251
1042e4c0
SS
252static void tracepoints_info (char *, int);
253
254static void delete_trace_command (char *, int);
255
256static void enable_trace_command (char *, int);
257
258static void disable_trace_command (char *, int);
259
260static void trace_pass_command (char *, int);
261
9c06b0b4
TJB
262static int is_masked_watchpoint (const struct breakpoint *b);
263
b775012e
LM
264static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
265
983af33b
SDJ
266/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
267 otherwise. */
268
269static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 270
edcc5120
TT
271static void init_catchpoint (struct breakpoint *b,
272 struct gdbarch *gdbarch, int tempflag,
273 char *cond_string,
274 const struct breakpoint_ops *ops);
275
2060206e
PA
276/* The abstract base class all breakpoint_ops structures inherit
277 from. */
278static struct breakpoint_ops base_breakpoint_ops;
279
280/* The breakpoint_ops structure to be inherited by all breakpoint_ops
281 that are implemented on top of software or hardware breakpoints
282 (user breakpoints, internal and momentary breakpoints, etc.). */
283static struct breakpoint_ops bkpt_base_breakpoint_ops;
284
285/* Internal breakpoints class type. */
06edf0c0 286static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
287
288/* Momentary breakpoints class type. */
06edf0c0
PA
289static struct breakpoint_ops momentary_breakpoint_ops;
290
e2e4d78b
JK
291/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
292static struct breakpoint_ops longjmp_breakpoint_ops;
293
2060206e
PA
294/* The breakpoint_ops structure to be used in regular user created
295 breakpoints. */
296struct breakpoint_ops bkpt_breakpoint_ops;
297
55aa24fb
SDJ
298/* Breakpoints set on probes. */
299static struct breakpoint_ops bkpt_probe_breakpoint_ops;
300
e7e0cddf
SS
301/* Dynamic printf class type. */
302static struct breakpoint_ops dprintf_breakpoint_ops;
303
5cea2a26
PA
304/* A reference-counted struct command_line. This lets multiple
305 breakpoints share a single command list. */
306struct counted_command_line
307{
308 /* The reference count. */
309 int refc;
310
311 /* The command list. */
312 struct command_line *commands;
313};
314
315struct command_line *
316breakpoint_commands (struct breakpoint *b)
317{
318 return b->commands ? b->commands->commands : NULL;
319}
3daf8fe5 320
f3b1572e
PA
321/* Flag indicating that a command has proceeded the inferior past the
322 current breakpoint. */
323
324static int breakpoint_proceeded;
325
956a9fb9 326const char *
2cec12e5
AR
327bpdisp_text (enum bpdisp disp)
328{
4a64f543
MS
329 /* NOTE: the following values are a part of MI protocol and
330 represent values of 'disp' field returned when inferior stops at
331 a breakpoint. */
bc043ef3 332 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 333
2cec12e5
AR
334 return bpdisps[(int) disp];
335}
c906108c 336
4a64f543 337/* Prototypes for exported functions. */
c906108c 338/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 339 if such is available. */
c906108c
SS
340static int can_use_hw_watchpoints;
341
920d2a44
AC
342static void
343show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
344 struct cmd_list_element *c,
345 const char *value)
346{
3e43a32a
MS
347 fprintf_filtered (file,
348 _("Debugger's willingness to use "
349 "watchpoint hardware is %s.\n"),
920d2a44
AC
350 value);
351}
352
fa8d40ab
JJ
353/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
354 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 355 for unrecognized breakpoint locations.
fa8d40ab
JJ
356 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
357static enum auto_boolean pending_break_support;
920d2a44
AC
358static void
359show_pending_break_support (struct ui_file *file, int from_tty,
360 struct cmd_list_element *c,
361 const char *value)
362{
3e43a32a
MS
363 fprintf_filtered (file,
364 _("Debugger's behavior regarding "
365 "pending breakpoints is %s.\n"),
920d2a44
AC
366 value);
367}
fa8d40ab 368
765dc015 369/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 370 set with "break" but falling in read-only memory.
765dc015
VP
371 If 0, gdb will warn about such breakpoints, but won't automatically
372 use hardware breakpoints. */
373static int automatic_hardware_breakpoints;
374static void
375show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
376 struct cmd_list_element *c,
377 const char *value)
378{
3e43a32a
MS
379 fprintf_filtered (file,
380 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
381 value);
382}
383
33e5cbd6
PA
384/* If on, gdb will keep breakpoints inserted even as inferior is
385 stopped, and immediately insert any new breakpoints. If off, gdb
386 will insert breakpoints into inferior only when resuming it, and
387 will remove breakpoints upon stop. If auto, GDB will behave as ON
388 if in non-stop mode, and as OFF if all-stop mode.*/
389
72d0e2c5
YQ
390static enum auto_boolean always_inserted_mode = AUTO_BOOLEAN_AUTO;
391
33e5cbd6 392static void
74960c60 393show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 394 struct cmd_list_element *c, const char *value)
74960c60 395{
72d0e2c5 396 if (always_inserted_mode == AUTO_BOOLEAN_AUTO)
3e43a32a
MS
397 fprintf_filtered (file,
398 _("Always inserted breakpoint "
399 "mode is %s (currently %s).\n"),
33e5cbd6
PA
400 value,
401 breakpoints_always_inserted_mode () ? "on" : "off");
402 else
3e43a32a
MS
403 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
404 value);
74960c60
VP
405}
406
33e5cbd6
PA
407int
408breakpoints_always_inserted_mode (void)
409{
72d0e2c5
YQ
410 return (always_inserted_mode == AUTO_BOOLEAN_TRUE
411 || (always_inserted_mode == AUTO_BOOLEAN_AUTO && non_stop));
33e5cbd6 412}
765dc015 413
b775012e
LM
414static const char condition_evaluation_both[] = "host or target";
415
416/* Modes for breakpoint condition evaluation. */
417static const char condition_evaluation_auto[] = "auto";
418static const char condition_evaluation_host[] = "host";
419static const char condition_evaluation_target[] = "target";
420static const char *const condition_evaluation_enums[] = {
421 condition_evaluation_auto,
422 condition_evaluation_host,
423 condition_evaluation_target,
424 NULL
425};
426
427/* Global that holds the current mode for breakpoint condition evaluation. */
428static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
429
430/* Global that we use to display information to the user (gets its value from
431 condition_evaluation_mode_1. */
432static const char *condition_evaluation_mode = condition_evaluation_auto;
433
434/* Translate a condition evaluation mode MODE into either "host"
435 or "target". This is used mostly to translate from "auto" to the
436 real setting that is being used. It returns the translated
437 evaluation mode. */
438
439static const char *
440translate_condition_evaluation_mode (const char *mode)
441{
442 if (mode == condition_evaluation_auto)
443 {
444 if (target_supports_evaluation_of_breakpoint_conditions ())
445 return condition_evaluation_target;
446 else
447 return condition_evaluation_host;
448 }
449 else
450 return mode;
451}
452
453/* Discovers what condition_evaluation_auto translates to. */
454
455static const char *
456breakpoint_condition_evaluation_mode (void)
457{
458 return translate_condition_evaluation_mode (condition_evaluation_mode);
459}
460
461/* Return true if GDB should evaluate breakpoint conditions or false
462 otherwise. */
463
464static int
465gdb_evaluates_breakpoint_condition_p (void)
466{
467 const char *mode = breakpoint_condition_evaluation_mode ();
468
469 return (mode == condition_evaluation_host);
470}
471
a14ed312 472void _initialize_breakpoint (void);
c906108c 473
c906108c
SS
474/* Are we executing breakpoint commands? */
475static int executing_breakpoint_commands;
476
c02f5703
MS
477/* Are overlay event breakpoints enabled? */
478static int overlay_events_enabled;
479
e09342b5
TJB
480/* See description in breakpoint.h. */
481int target_exact_watchpoints = 0;
482
c906108c 483/* Walk the following statement or block through all breakpoints.
e5dd4106 484 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 485 current breakpoint. */
c906108c 486
5c44784c 487#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 488
5c44784c
JM
489#define ALL_BREAKPOINTS_SAFE(B,TMP) \
490 for (B = breakpoint_chain; \
491 B ? (TMP=B->next, 1): 0; \
492 B = TMP)
c906108c 493
4a64f543
MS
494/* Similar iterator for the low-level breakpoints. SAFE variant is
495 not provided so update_global_location_list must not be called
496 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 497
876fa593
JK
498#define ALL_BP_LOCATIONS(B,BP_TMP) \
499 for (BP_TMP = bp_location; \
500 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
501 BP_TMP++)
7cc221ef 502
b775012e
LM
503/* Iterates through locations with address ADDRESS for the currently selected
504 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
505 to where the loop should start from.
506 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
507 appropriate location to start with. */
508
509#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
510 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
511 BP_LOCP_TMP = BP_LOCP_START; \
512 BP_LOCP_START \
513 && (BP_LOCP_TMP < bp_location + bp_location_count \
514 && (*BP_LOCP_TMP)->address == ADDRESS); \
515 BP_LOCP_TMP++)
516
1042e4c0
SS
517/* Iterator for tracepoints only. */
518
519#define ALL_TRACEPOINTS(B) \
520 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 521 if (is_tracepoint (B))
1042e4c0 522
7cc221ef 523/* Chains of all breakpoints defined. */
c906108c
SS
524
525struct breakpoint *breakpoint_chain;
526
876fa593
JK
527/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
528
529static struct bp_location **bp_location;
530
531/* Number of elements of BP_LOCATION. */
532
533static unsigned bp_location_count;
534
4a64f543
MS
535/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
536 ADDRESS for the current elements of BP_LOCATION which get a valid
537 result from bp_location_has_shadow. You can use it for roughly
538 limiting the subrange of BP_LOCATION to scan for shadow bytes for
539 an address you need to read. */
876fa593
JK
540
541static CORE_ADDR bp_location_placed_address_before_address_max;
542
4a64f543
MS
543/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
544 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
545 BP_LOCATION which get a valid result from bp_location_has_shadow.
546 You can use it for roughly limiting the subrange of BP_LOCATION to
547 scan for shadow bytes for an address you need to read. */
876fa593
JK
548
549static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 550
4a64f543
MS
551/* The locations that no longer correspond to any breakpoint, unlinked
552 from bp_location array, but for which a hit may still be reported
553 by a target. */
20874c92
VP
554VEC(bp_location_p) *moribund_locations = NULL;
555
c906108c
SS
556/* Number of last breakpoint made. */
557
95a42b64
TT
558static int breakpoint_count;
559
86b17b60
PA
560/* The value of `breakpoint_count' before the last command that
561 created breakpoints. If the last (break-like) command created more
562 than one breakpoint, then the difference between BREAKPOINT_COUNT
563 and PREV_BREAKPOINT_COUNT is more than one. */
564static int prev_breakpoint_count;
c906108c 565
1042e4c0
SS
566/* Number of last tracepoint made. */
567
95a42b64 568static int tracepoint_count;
1042e4c0 569
6149aea9
PA
570static struct cmd_list_element *breakpoint_set_cmdlist;
571static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 572struct cmd_list_element *save_cmdlist;
6149aea9 573
468d015d
JJ
574/* Return whether a breakpoint is an active enabled breakpoint. */
575static int
576breakpoint_enabled (struct breakpoint *b)
577{
0d381245 578 return (b->enable_state == bp_enabled);
468d015d
JJ
579}
580
c906108c
SS
581/* Set breakpoint count to NUM. */
582
95a42b64 583static void
fba45db2 584set_breakpoint_count (int num)
c906108c 585{
86b17b60 586 prev_breakpoint_count = breakpoint_count;
c906108c 587 breakpoint_count = num;
4fa62494 588 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
589}
590
86b17b60
PA
591/* Used by `start_rbreak_breakpoints' below, to record the current
592 breakpoint count before "rbreak" creates any breakpoint. */
593static int rbreak_start_breakpoint_count;
594
95a42b64
TT
595/* Called at the start an "rbreak" command to record the first
596 breakpoint made. */
86b17b60 597
95a42b64
TT
598void
599start_rbreak_breakpoints (void)
600{
86b17b60 601 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
602}
603
604/* Called at the end of an "rbreak" command to record the last
605 breakpoint made. */
86b17b60 606
95a42b64
TT
607void
608end_rbreak_breakpoints (void)
609{
86b17b60 610 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
611}
612
4a64f543 613/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
614
615void
fba45db2 616clear_breakpoint_hit_counts (void)
c906108c
SS
617{
618 struct breakpoint *b;
619
620 ALL_BREAKPOINTS (b)
621 b->hit_count = 0;
622}
623
9add0f1b
TT
624/* Allocate a new counted_command_line with reference count of 1.
625 The new structure owns COMMANDS. */
626
627static struct counted_command_line *
628alloc_counted_command_line (struct command_line *commands)
629{
630 struct counted_command_line *result
631 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 632
9add0f1b
TT
633 result->refc = 1;
634 result->commands = commands;
635 return result;
636}
637
638/* Increment reference count. This does nothing if CMD is NULL. */
639
640static void
641incref_counted_command_line (struct counted_command_line *cmd)
642{
643 if (cmd)
644 ++cmd->refc;
645}
646
647/* Decrement reference count. If the reference count reaches 0,
648 destroy the counted_command_line. Sets *CMDP to NULL. This does
649 nothing if *CMDP is NULL. */
650
651static void
652decref_counted_command_line (struct counted_command_line **cmdp)
653{
654 if (*cmdp)
655 {
656 if (--(*cmdp)->refc == 0)
657 {
658 free_command_lines (&(*cmdp)->commands);
659 xfree (*cmdp);
660 }
661 *cmdp = NULL;
662 }
663}
664
665/* A cleanup function that calls decref_counted_command_line. */
666
667static void
668do_cleanup_counted_command_line (void *arg)
669{
670 decref_counted_command_line (arg);
671}
672
673/* Create a cleanup that calls decref_counted_command_line on the
674 argument. */
675
676static struct cleanup *
677make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
678{
679 return make_cleanup (do_cleanup_counted_command_line, cmdp);
680}
681
c906108c 682\f
48cb2d85
VP
683/* Return the breakpoint with the specified number, or NULL
684 if the number does not refer to an existing breakpoint. */
685
686struct breakpoint *
687get_breakpoint (int num)
688{
689 struct breakpoint *b;
690
691 ALL_BREAKPOINTS (b)
692 if (b->number == num)
693 return b;
694
695 return NULL;
696}
5c44784c 697
c906108c 698\f
adc36818 699
b775012e
LM
700/* Mark locations as "conditions have changed" in case the target supports
701 evaluating conditions on its side. */
702
703static void
704mark_breakpoint_modified (struct breakpoint *b)
705{
706 struct bp_location *loc;
707
708 /* This is only meaningful if the target is
709 evaluating conditions and if the user has
710 opted for condition evaluation on the target's
711 side. */
712 if (gdb_evaluates_breakpoint_condition_p ()
713 || !target_supports_evaluation_of_breakpoint_conditions ())
714 return;
715
716 if (!is_breakpoint (b))
717 return;
718
719 for (loc = b->loc; loc; loc = loc->next)
720 loc->condition_changed = condition_modified;
721}
722
723/* Mark location as "conditions have changed" in case the target supports
724 evaluating conditions on its side. */
725
726static void
727mark_breakpoint_location_modified (struct bp_location *loc)
728{
729 /* This is only meaningful if the target is
730 evaluating conditions and if the user has
731 opted for condition evaluation on the target's
732 side. */
733 if (gdb_evaluates_breakpoint_condition_p ()
734 || !target_supports_evaluation_of_breakpoint_conditions ())
735
736 return;
737
738 if (!is_breakpoint (loc->owner))
739 return;
740
741 loc->condition_changed = condition_modified;
742}
743
744/* Sets the condition-evaluation mode using the static global
745 condition_evaluation_mode. */
746
747static void
748set_condition_evaluation_mode (char *args, int from_tty,
749 struct cmd_list_element *c)
750{
b775012e
LM
751 const char *old_mode, *new_mode;
752
753 if ((condition_evaluation_mode_1 == condition_evaluation_target)
754 && !target_supports_evaluation_of_breakpoint_conditions ())
755 {
756 condition_evaluation_mode_1 = condition_evaluation_mode;
757 warning (_("Target does not support breakpoint condition evaluation.\n"
758 "Using host evaluation mode instead."));
759 return;
760 }
761
762 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
763 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
764
abf1152a
JK
765 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
766 settings was "auto". */
767 condition_evaluation_mode = condition_evaluation_mode_1;
768
b775012e
LM
769 /* Only update the mode if the user picked a different one. */
770 if (new_mode != old_mode)
771 {
772 struct bp_location *loc, **loc_tmp;
773 /* If the user switched to a different evaluation mode, we
774 need to synch the changes with the target as follows:
775
776 "host" -> "target": Send all (valid) conditions to the target.
777 "target" -> "host": Remove all the conditions from the target.
778 */
779
b775012e
LM
780 if (new_mode == condition_evaluation_target)
781 {
782 /* Mark everything modified and synch conditions with the
783 target. */
784 ALL_BP_LOCATIONS (loc, loc_tmp)
785 mark_breakpoint_location_modified (loc);
786 }
787 else
788 {
789 /* Manually mark non-duplicate locations to synch conditions
790 with the target. We do this to remove all the conditions the
791 target knows about. */
792 ALL_BP_LOCATIONS (loc, loc_tmp)
793 if (is_breakpoint (loc->owner) && loc->inserted)
794 loc->needs_update = 1;
795 }
796
797 /* Do the update. */
798 update_global_location_list (1);
799 }
800
801 return;
802}
803
804/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
805 what "auto" is translating to. */
806
807static void
808show_condition_evaluation_mode (struct ui_file *file, int from_tty,
809 struct cmd_list_element *c, const char *value)
810{
811 if (condition_evaluation_mode == condition_evaluation_auto)
812 fprintf_filtered (file,
813 _("Breakpoint condition evaluation "
814 "mode is %s (currently %s).\n"),
815 value,
816 breakpoint_condition_evaluation_mode ());
817 else
818 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
819 value);
820}
821
822/* A comparison function for bp_location AP and BP that is used by
823 bsearch. This comparison function only cares about addresses, unlike
824 the more general bp_location_compare function. */
825
826static int
827bp_location_compare_addrs (const void *ap, const void *bp)
828{
829 struct bp_location *a = *(void **) ap;
830 struct bp_location *b = *(void **) bp;
831
832 if (a->address == b->address)
833 return 0;
834 else
835 return ((a->address > b->address) - (a->address < b->address));
836}
837
838/* Helper function to skip all bp_locations with addresses
839 less than ADDRESS. It returns the first bp_location that
840 is greater than or equal to ADDRESS. If none is found, just
841 return NULL. */
842
843static struct bp_location **
844get_first_locp_gte_addr (CORE_ADDR address)
845{
846 struct bp_location dummy_loc;
847 struct bp_location *dummy_locp = &dummy_loc;
848 struct bp_location **locp_found = NULL;
849
850 /* Initialize the dummy location's address field. */
851 memset (&dummy_loc, 0, sizeof (struct bp_location));
852 dummy_loc.address = address;
853
854 /* Find a close match to the first location at ADDRESS. */
855 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
856 sizeof (struct bp_location **),
857 bp_location_compare_addrs);
858
859 /* Nothing was found, nothing left to do. */
860 if (locp_found == NULL)
861 return NULL;
862
863 /* We may have found a location that is at ADDRESS but is not the first in the
864 location's list. Go backwards (if possible) and locate the first one. */
865 while ((locp_found - 1) >= bp_location
866 && (*(locp_found - 1))->address == address)
867 locp_found--;
868
869 return locp_found;
870}
871
adc36818
PM
872void
873set_breakpoint_condition (struct breakpoint *b, char *exp,
874 int from_tty)
875{
3a5c3e22
PA
876 xfree (b->cond_string);
877 b->cond_string = NULL;
adc36818 878
3a5c3e22 879 if (is_watchpoint (b))
adc36818 880 {
3a5c3e22
PA
881 struct watchpoint *w = (struct watchpoint *) b;
882
883 xfree (w->cond_exp);
884 w->cond_exp = NULL;
885 }
886 else
887 {
888 struct bp_location *loc;
889
890 for (loc = b->loc; loc; loc = loc->next)
891 {
892 xfree (loc->cond);
893 loc->cond = NULL;
b775012e
LM
894
895 /* No need to free the condition agent expression
896 bytecode (if we have one). We will handle this
897 when we go through update_global_location_list. */
3a5c3e22 898 }
adc36818 899 }
adc36818
PM
900
901 if (*exp == 0)
902 {
903 if (from_tty)
904 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
905 }
906 else
907 {
908 char *arg = exp;
cc59ec59 909
adc36818
PM
910 /* I don't know if it matters whether this is the string the user
911 typed in or the decompiled expression. */
912 b->cond_string = xstrdup (arg);
913 b->condition_not_parsed = 0;
914
915 if (is_watchpoint (b))
916 {
3a5c3e22
PA
917 struct watchpoint *w = (struct watchpoint *) b;
918
adc36818
PM
919 innermost_block = NULL;
920 arg = exp;
1bb9788d 921 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
922 if (*arg)
923 error (_("Junk at end of expression"));
3a5c3e22 924 w->cond_exp_valid_block = innermost_block;
adc36818
PM
925 }
926 else
927 {
3a5c3e22
PA
928 struct bp_location *loc;
929
adc36818
PM
930 for (loc = b->loc; loc; loc = loc->next)
931 {
932 arg = exp;
933 loc->cond =
1bb9788d
TT
934 parse_exp_1 (&arg, loc->address,
935 block_for_pc (loc->address), 0);
adc36818
PM
936 if (*arg)
937 error (_("Junk at end of expression"));
938 }
939 }
940 }
b775012e
LM
941 mark_breakpoint_modified (b);
942
adc36818 943 breakpoints_changed ();
8d3788bd 944 observer_notify_breakpoint_modified (b);
adc36818
PM
945}
946
d55637df
TT
947/* Completion for the "condition" command. */
948
949static VEC (char_ptr) *
950condition_completer (struct cmd_list_element *cmd, char *text, char *word)
951{
952 char *space;
953
954 text = skip_spaces (text);
955 space = skip_to_space (text);
956 if (*space == '\0')
957 {
958 int len;
959 struct breakpoint *b;
960 VEC (char_ptr) *result = NULL;
961
962 if (text[0] == '$')
963 {
964 /* We don't support completion of history indices. */
965 if (isdigit (text[1]))
966 return NULL;
967 return complete_internalvar (&text[1]);
968 }
969
970 /* We're completing the breakpoint number. */
971 len = strlen (text);
972
973 ALL_BREAKPOINTS (b)
974 {
975 int single = b->loc->next == NULL;
976 struct bp_location *loc;
977 int count = 1;
978
979 for (loc = b->loc; loc; loc = loc->next)
980 {
981 char location[50];
982
983 if (single)
984 sprintf (location, "%d", b->number);
985 else
986 sprintf (location, "%d.%d", b->number, count);
987
988 if (strncmp (location, text, len) == 0)
989 VEC_safe_push (char_ptr, result, xstrdup (location));
990
991 ++count;
992 }
993 }
994
995 return result;
996 }
997
998 /* We're completing the expression part. */
999 text = skip_spaces (space);
1000 return expression_completer (cmd, text, word);
1001}
1002
c906108c
SS
1003/* condition N EXP -- set break condition of breakpoint N to EXP. */
1004
1005static void
fba45db2 1006condition_command (char *arg, int from_tty)
c906108c 1007{
52f0bd74 1008 struct breakpoint *b;
c906108c 1009 char *p;
52f0bd74 1010 int bnum;
c906108c
SS
1011
1012 if (arg == 0)
e2e0b3e5 1013 error_no_arg (_("breakpoint number"));
c906108c
SS
1014
1015 p = arg;
1016 bnum = get_number (&p);
5c44784c 1017 if (bnum == 0)
8a3fe4f8 1018 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1019
1020 ALL_BREAKPOINTS (b)
1021 if (b->number == bnum)
2f069f6f 1022 {
7371cf6d
PM
1023 /* Check if this breakpoint has a Python object assigned to
1024 it, and if it has a definition of the "stop"
1025 method. This method and conditions entered into GDB from
1026 the CLI are mutually exclusive. */
1027 if (b->py_bp_object
1028 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
1029 error (_("Cannot set a condition where a Python 'stop' "
1030 "method has been defined in the breakpoint."));
2566ad2d 1031 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1032
1033 if (is_breakpoint (b))
1034 update_global_location_list (1);
1035
2f069f6f
JB
1036 return;
1037 }
c906108c 1038
8a3fe4f8 1039 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1040}
1041
a7bdde9e
VP
1042/* Check that COMMAND do not contain commands that are suitable
1043 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1044 Throw if any such commands is found. */
1045
a7bdde9e
VP
1046static void
1047check_no_tracepoint_commands (struct command_line *commands)
1048{
1049 struct command_line *c;
cc59ec59 1050
a7bdde9e
VP
1051 for (c = commands; c; c = c->next)
1052 {
1053 int i;
1054
1055 if (c->control_type == while_stepping_control)
3e43a32a
MS
1056 error (_("The 'while-stepping' command can "
1057 "only be used for tracepoints"));
a7bdde9e
VP
1058
1059 for (i = 0; i < c->body_count; ++i)
1060 check_no_tracepoint_commands ((c->body_list)[i]);
1061
1062 /* Not that command parsing removes leading whitespace and comment
4a64f543 1063 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1064 command directly. */
1065 if (strstr (c->line, "collect ") == c->line)
1066 error (_("The 'collect' command can only be used for tracepoints"));
1067
51661e93
VP
1068 if (strstr (c->line, "teval ") == c->line)
1069 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1070 }
1071}
1072
d77f58be
SS
1073/* Encapsulate tests for different types of tracepoints. */
1074
d9b3f62e
PA
1075static int
1076is_tracepoint_type (enum bptype type)
1077{
1078 return (type == bp_tracepoint
1079 || type == bp_fast_tracepoint
1080 || type == bp_static_tracepoint);
1081}
1082
a7bdde9e 1083int
d77f58be 1084is_tracepoint (const struct breakpoint *b)
a7bdde9e 1085{
d9b3f62e 1086 return is_tracepoint_type (b->type);
a7bdde9e 1087}
d9b3f62e 1088
e5dd4106 1089/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1090 breakpoint. This function will throw an exception if a problem is
1091 found. */
48cb2d85 1092
95a42b64
TT
1093static void
1094validate_commands_for_breakpoint (struct breakpoint *b,
1095 struct command_line *commands)
48cb2d85 1096{
d77f58be 1097 if (is_tracepoint (b))
a7bdde9e 1098 {
4a64f543
MS
1099 /* We need to verify that each top-level element of commands is
1100 valid for tracepoints, that there's at most one
1101 while-stepping element, and that while-stepping's body has
1102 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
1103 struct command_line *c;
1104 struct command_line *while_stepping = 0;
1105 for (c = commands; c; c = c->next)
1106 {
a7bdde9e
VP
1107 if (c->control_type == while_stepping_control)
1108 {
1109 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1110 error (_("The 'while-stepping' command "
1111 "cannot be used for fast tracepoint"));
0fb4aa4b 1112 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1113 error (_("The 'while-stepping' command "
1114 "cannot be used for static tracepoint"));
a7bdde9e
VP
1115
1116 if (while_stepping)
3e43a32a
MS
1117 error (_("The 'while-stepping' command "
1118 "can be used only once"));
a7bdde9e
VP
1119 else
1120 while_stepping = c;
1121 }
1122 }
1123 if (while_stepping)
1124 {
1125 struct command_line *c2;
1126
1127 gdb_assert (while_stepping->body_count == 1);
1128 c2 = while_stepping->body_list[0];
1129 for (; c2; c2 = c2->next)
1130 {
a7bdde9e
VP
1131 if (c2->control_type == while_stepping_control)
1132 error (_("The 'while-stepping' command cannot be nested"));
1133 }
1134 }
1135 }
1136 else
1137 {
1138 check_no_tracepoint_commands (commands);
1139 }
95a42b64
TT
1140}
1141
0fb4aa4b
PA
1142/* Return a vector of all the static tracepoints set at ADDR. The
1143 caller is responsible for releasing the vector. */
1144
1145VEC(breakpoint_p) *
1146static_tracepoints_here (CORE_ADDR addr)
1147{
1148 struct breakpoint *b;
1149 VEC(breakpoint_p) *found = 0;
1150 struct bp_location *loc;
1151
1152 ALL_BREAKPOINTS (b)
1153 if (b->type == bp_static_tracepoint)
1154 {
1155 for (loc = b->loc; loc; loc = loc->next)
1156 if (loc->address == addr)
1157 VEC_safe_push(breakpoint_p, found, b);
1158 }
1159
1160 return found;
1161}
1162
95a42b64 1163/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1164 validate that only allowed commands are included. */
95a42b64
TT
1165
1166void
4a64f543
MS
1167breakpoint_set_commands (struct breakpoint *b,
1168 struct command_line *commands)
95a42b64
TT
1169{
1170 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1171
9add0f1b
TT
1172 decref_counted_command_line (&b->commands);
1173 b->commands = alloc_counted_command_line (commands);
48cb2d85 1174 breakpoints_changed ();
8d3788bd 1175 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1176}
1177
45a43567
TT
1178/* Set the internal `silent' flag on the breakpoint. Note that this
1179 is not the same as the "silent" that may appear in the breakpoint's
1180 commands. */
1181
1182void
1183breakpoint_set_silent (struct breakpoint *b, int silent)
1184{
1185 int old_silent = b->silent;
1186
1187 b->silent = silent;
1188 if (old_silent != silent)
8d3788bd 1189 observer_notify_breakpoint_modified (b);
45a43567
TT
1190}
1191
1192/* Set the thread for this breakpoint. If THREAD is -1, make the
1193 breakpoint work for any thread. */
1194
1195void
1196breakpoint_set_thread (struct breakpoint *b, int thread)
1197{
1198 int old_thread = b->thread;
1199
1200 b->thread = thread;
1201 if (old_thread != thread)
8d3788bd 1202 observer_notify_breakpoint_modified (b);
45a43567
TT
1203}
1204
1205/* Set the task for this breakpoint. If TASK is 0, make the
1206 breakpoint work for any task. */
1207
1208void
1209breakpoint_set_task (struct breakpoint *b, int task)
1210{
1211 int old_task = b->task;
1212
1213 b->task = task;
1214 if (old_task != task)
8d3788bd 1215 observer_notify_breakpoint_modified (b);
45a43567
TT
1216}
1217
95a42b64
TT
1218void
1219check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1220{
1221 struct breakpoint *b = closure;
cc59ec59 1222
a7bdde9e
VP
1223 validate_actionline (&line, b);
1224}
1225
95a42b64
TT
1226/* A structure used to pass information through
1227 map_breakpoint_numbers. */
1228
1229struct commands_info
1230{
1231 /* True if the command was typed at a tty. */
1232 int from_tty;
86b17b60
PA
1233
1234 /* The breakpoint range spec. */
1235 char *arg;
1236
95a42b64
TT
1237 /* Non-NULL if the body of the commands are being read from this
1238 already-parsed command. */
1239 struct command_line *control;
86b17b60 1240
95a42b64
TT
1241 /* The command lines read from the user, or NULL if they have not
1242 yet been read. */
1243 struct counted_command_line *cmd;
1244};
1245
1246/* A callback for map_breakpoint_numbers that sets the commands for
1247 commands_command. */
1248
c906108c 1249static void
95a42b64 1250do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1251{
95a42b64 1252 struct commands_info *info = data;
c906108c 1253
95a42b64
TT
1254 if (info->cmd == NULL)
1255 {
1256 struct command_line *l;
5c44784c 1257
95a42b64
TT
1258 if (info->control != NULL)
1259 l = copy_command_lines (info->control->body_list[0]);
1260 else
86b17b60
PA
1261 {
1262 struct cleanup *old_chain;
1263 char *str;
c5aa993b 1264
3e43a32a
MS
1265 str = xstrprintf (_("Type commands for breakpoint(s) "
1266 "%s, one per line."),
86b17b60
PA
1267 info->arg);
1268
1269 old_chain = make_cleanup (xfree, str);
1270
1271 l = read_command_lines (str,
1272 info->from_tty, 1,
d77f58be 1273 (is_tracepoint (b)
86b17b60
PA
1274 ? check_tracepoint_command : 0),
1275 b);
1276
1277 do_cleanups (old_chain);
1278 }
a7bdde9e 1279
95a42b64
TT
1280 info->cmd = alloc_counted_command_line (l);
1281 }
1282
1283 /* If a breakpoint was on the list more than once, we don't need to
1284 do anything. */
1285 if (b->commands != info->cmd)
1286 {
1287 validate_commands_for_breakpoint (b, info->cmd->commands);
1288 incref_counted_command_line (info->cmd);
1289 decref_counted_command_line (&b->commands);
1290 b->commands = info->cmd;
1291 breakpoints_changed ();
8d3788bd 1292 observer_notify_breakpoint_modified (b);
c5aa993b 1293 }
95a42b64
TT
1294}
1295
1296static void
4a64f543
MS
1297commands_command_1 (char *arg, int from_tty,
1298 struct command_line *control)
95a42b64
TT
1299{
1300 struct cleanup *cleanups;
1301 struct commands_info info;
1302
1303 info.from_tty = from_tty;
1304 info.control = control;
1305 info.cmd = NULL;
1306 /* If we read command lines from the user, then `info' will hold an
1307 extra reference to the commands that we must clean up. */
1308 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1309
1310 if (arg == NULL || !*arg)
1311 {
86b17b60 1312 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1313 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1314 breakpoint_count);
95a42b64
TT
1315 else if (breakpoint_count > 0)
1316 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1317 else
1318 {
1319 /* So that we don't try to free the incoming non-NULL
1320 argument in the cleanup below. Mapping breakpoint
1321 numbers will fail in this case. */
1322 arg = NULL;
1323 }
95a42b64 1324 }
9766ced4
SS
1325 else
1326 /* The command loop has some static state, so we need to preserve
1327 our argument. */
1328 arg = xstrdup (arg);
86b17b60
PA
1329
1330 if (arg != NULL)
1331 make_cleanup (xfree, arg);
1332
1333 info.arg = arg;
95a42b64
TT
1334
1335 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1336
1337 if (info.cmd == NULL)
1338 error (_("No breakpoints specified."));
1339
1340 do_cleanups (cleanups);
1341}
1342
1343static void
1344commands_command (char *arg, int from_tty)
1345{
1346 commands_command_1 (arg, from_tty, NULL);
c906108c 1347}
40c03ae8
EZ
1348
1349/* Like commands_command, but instead of reading the commands from
1350 input stream, takes them from an already parsed command structure.
1351
1352 This is used by cli-script.c to DTRT with breakpoint commands
1353 that are part of if and while bodies. */
1354enum command_control_type
1355commands_from_control_command (char *arg, struct command_line *cmd)
1356{
95a42b64
TT
1357 commands_command_1 (arg, 0, cmd);
1358 return simple_control;
40c03ae8 1359}
876fa593
JK
1360
1361/* Return non-zero if BL->TARGET_INFO contains valid information. */
1362
1363static int
1364bp_location_has_shadow (struct bp_location *bl)
1365{
1366 if (bl->loc_type != bp_loc_software_breakpoint)
1367 return 0;
1368 if (!bl->inserted)
1369 return 0;
1370 if (bl->target_info.shadow_len == 0)
e5dd4106 1371 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1372 return 0;
1373 return 1;
1374}
1375
8defab1a 1376/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1377 by replacing any memory breakpoints with their shadowed contents.
1378
35c63cd8
JB
1379 If READBUF is not NULL, this buffer must not overlap with any of
1380 the breakpoint location's shadow_contents buffers. Otherwise,
1381 a failed assertion internal error will be raised.
1382
876fa593 1383 The range of shadowed area by each bp_location is:
35df4500
TJB
1384 bl->address - bp_location_placed_address_before_address_max
1385 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1386 The range we were requested to resolve shadows for is:
1387 memaddr ... memaddr + len
1388 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1389 memaddr + len <= (bl->address
1390 - bp_location_placed_address_before_address_max)
876fa593 1391 and:
35df4500 1392 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1393
8defab1a 1394void
f0ba3972
PA
1395breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1396 const gdb_byte *writebuf_org,
1397 ULONGEST memaddr, LONGEST len)
c906108c 1398{
4a64f543
MS
1399 /* Left boundary, right boundary and median element of our binary
1400 search. */
876fa593
JK
1401 unsigned bc_l, bc_r, bc;
1402
4a64f543
MS
1403 /* Find BC_L which is a leftmost element which may affect BUF
1404 content. It is safe to report lower value but a failure to
1405 report higher one. */
876fa593
JK
1406
1407 bc_l = 0;
1408 bc_r = bp_location_count;
1409 while (bc_l + 1 < bc_r)
1410 {
35df4500 1411 struct bp_location *bl;
876fa593
JK
1412
1413 bc = (bc_l + bc_r) / 2;
35df4500 1414 bl = bp_location[bc];
876fa593 1415
4a64f543
MS
1416 /* Check first BL->ADDRESS will not overflow due to the added
1417 constant. Then advance the left boundary only if we are sure
1418 the BC element can in no way affect the BUF content (MEMADDR
1419 to MEMADDR + LEN range).
876fa593 1420
4a64f543
MS
1421 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1422 offset so that we cannot miss a breakpoint with its shadow
1423 range tail still reaching MEMADDR. */
c5aa993b 1424
35df4500
TJB
1425 if ((bl->address + bp_location_shadow_len_after_address_max
1426 >= bl->address)
1427 && (bl->address + bp_location_shadow_len_after_address_max
1428 <= memaddr))
876fa593
JK
1429 bc_l = bc;
1430 else
1431 bc_r = bc;
1432 }
1433
128070bb
PA
1434 /* Due to the binary search above, we need to make sure we pick the
1435 first location that's at BC_L's address. E.g., if there are
1436 multiple locations at the same address, BC_L may end up pointing
1437 at a duplicate location, and miss the "master"/"inserted"
1438 location. Say, given locations L1, L2 and L3 at addresses A and
1439 B:
1440
1441 L1@A, L2@A, L3@B, ...
1442
1443 BC_L could end up pointing at location L2, while the "master"
1444 location could be L1. Since the `loc->inserted' flag is only set
1445 on "master" locations, we'd forget to restore the shadow of L1
1446 and L2. */
1447 while (bc_l > 0
1448 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1449 bc_l--;
1450
876fa593
JK
1451 /* Now do full processing of the found relevant range of elements. */
1452
1453 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1454 {
35df4500 1455 struct bp_location *bl = bp_location[bc];
876fa593
JK
1456 CORE_ADDR bp_addr = 0;
1457 int bp_size = 0;
1458 int bptoffset = 0;
1459
35df4500
TJB
1460 /* bp_location array has BL->OWNER always non-NULL. */
1461 if (bl->owner->type == bp_none)
8a3fe4f8 1462 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1463 bl->owner->number);
ffce0d52 1464
e5dd4106 1465 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1466 content. */
1467
35df4500
TJB
1468 if (bl->address >= bp_location_placed_address_before_address_max
1469 && memaddr + len <= (bl->address
1470 - bp_location_placed_address_before_address_max))
876fa593
JK
1471 break;
1472
35df4500 1473 if (!bp_location_has_shadow (bl))
c5aa993b 1474 continue;
35df4500 1475 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1476 current_program_space->aspace, 0))
1477 continue;
1478
c5aa993b
JM
1479 /* Addresses and length of the part of the breakpoint that
1480 we need to copy. */
35df4500
TJB
1481 bp_addr = bl->target_info.placed_address;
1482 bp_size = bl->target_info.shadow_len;
8defab1a 1483
c5aa993b
JM
1484 if (bp_addr + bp_size <= memaddr)
1485 /* The breakpoint is entirely before the chunk of memory we
1486 are reading. */
1487 continue;
8defab1a 1488
c5aa993b
JM
1489 if (bp_addr >= memaddr + len)
1490 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1491 reading. */
c5aa993b 1492 continue;
c5aa993b 1493
8defab1a
DJ
1494 /* Offset within shadow_contents. */
1495 if (bp_addr < memaddr)
1496 {
1497 /* Only copy the second part of the breakpoint. */
1498 bp_size -= memaddr - bp_addr;
1499 bptoffset = memaddr - bp_addr;
1500 bp_addr = memaddr;
1501 }
c5aa993b 1502
8defab1a
DJ
1503 if (bp_addr + bp_size > memaddr + len)
1504 {
1505 /* Only copy the first part of the breakpoint. */
1506 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1507 }
c5aa993b 1508
f0ba3972
PA
1509 if (readbuf != NULL)
1510 {
35c63cd8
JB
1511 /* Verify that the readbuf buffer does not overlap with
1512 the shadow_contents buffer. */
1513 gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1514 || readbuf >= (bl->target_info.shadow_contents
1515 + bl->target_info.shadow_len));
1516
f0ba3972
PA
1517 /* Update the read buffer with this inserted breakpoint's
1518 shadow. */
1519 memcpy (readbuf + bp_addr - memaddr,
1520 bl->target_info.shadow_contents + bptoffset, bp_size);
1521 }
1522 else
1523 {
1524 struct gdbarch *gdbarch = bl->gdbarch;
1525 const unsigned char *bp;
1526 CORE_ADDR placed_address = bl->target_info.placed_address;
1527 unsigned placed_size = bl->target_info.placed_size;
1528
1529 /* Update the shadow with what we want to write to memory. */
1530 memcpy (bl->target_info.shadow_contents + bptoffset,
1531 writebuf_org + bp_addr - memaddr, bp_size);
1532
1533 /* Determine appropriate breakpoint contents and size for this
1534 address. */
1535 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1536
1537 /* Update the final write buffer with this inserted
1538 breakpoint's INSN. */
1539 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1540 }
c5aa993b 1541 }
c906108c 1542}
c906108c 1543\f
c5aa993b 1544
b775012e
LM
1545/* Return true if BPT is either a software breakpoint or a hardware
1546 breakpoint. */
1547
1548int
1549is_breakpoint (const struct breakpoint *bpt)
1550{
1551 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1552 || bpt->type == bp_hardware_breakpoint
1553 || bpt->type == bp_dprintf);
b775012e
LM
1554}
1555
60e1c644
PA
1556/* Return true if BPT is of any hardware watchpoint kind. */
1557
a5606eee 1558static int
d77f58be 1559is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1560{
1561 return (bpt->type == bp_hardware_watchpoint
1562 || bpt->type == bp_read_watchpoint
1563 || bpt->type == bp_access_watchpoint);
1564}
7270d8f2 1565
60e1c644
PA
1566/* Return true if BPT is of any watchpoint kind, hardware or
1567 software. */
1568
3a5c3e22 1569int
d77f58be 1570is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1571{
1572 return (is_hardware_watchpoint (bpt)
1573 || bpt->type == bp_watchpoint);
1574}
1575
3a5c3e22
PA
1576/* Returns true if the current thread and its running state are safe
1577 to evaluate or update watchpoint B. Watchpoints on local
1578 expressions need to be evaluated in the context of the thread that
1579 was current when the watchpoint was created, and, that thread needs
1580 to be stopped to be able to select the correct frame context.
1581 Watchpoints on global expressions can be evaluated on any thread,
1582 and in any state. It is presently left to the target allowing
1583 memory accesses when threads are running. */
f6bc2008
PA
1584
1585static int
3a5c3e22 1586watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1587{
d0d8b0c6
JK
1588 return (b->base.pspace == current_program_space
1589 && (ptid_equal (b->watchpoint_thread, null_ptid)
1590 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1591 && !is_executing (inferior_ptid))));
f6bc2008
PA
1592}
1593
d0fb5eae
JK
1594/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1595 associated bp_watchpoint_scope breakpoint. */
1596
1597static void
3a5c3e22 1598watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1599{
3a5c3e22 1600 struct breakpoint *b = &w->base;
d0fb5eae
JK
1601
1602 if (b->related_breakpoint != b)
1603 {
1604 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1605 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1606 b->related_breakpoint->disposition = disp_del_at_next_stop;
1607 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1608 b->related_breakpoint = b;
1609 }
1610 b->disposition = disp_del_at_next_stop;
1611}
1612
567e1b4e
JB
1613/* Assuming that B is a watchpoint:
1614 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1615 - Evaluate expression and store the result in B->val
567e1b4e
JB
1616 - Evaluate the condition if there is one, and store the result
1617 in b->loc->cond.
a5606eee
VP
1618 - Update the list of values that must be watched in B->loc.
1619
4a64f543
MS
1620 If the watchpoint disposition is disp_del_at_next_stop, then do
1621 nothing. If this is local watchpoint that is out of scope, delete
1622 it.
1623
1624 Even with `set breakpoint always-inserted on' the watchpoints are
1625 removed + inserted on each stop here. Normal breakpoints must
1626 never be removed because they might be missed by a running thread
1627 when debugging in non-stop mode. On the other hand, hardware
1628 watchpoints (is_hardware_watchpoint; processed here) are specific
1629 to each LWP since they are stored in each LWP's hardware debug
1630 registers. Therefore, such LWP must be stopped first in order to
1631 be able to modify its hardware watchpoints.
1632
1633 Hardware watchpoints must be reset exactly once after being
1634 presented to the user. It cannot be done sooner, because it would
1635 reset the data used to present the watchpoint hit to the user. And
1636 it must not be done later because it could display the same single
1637 watchpoint hit during multiple GDB stops. Note that the latter is
1638 relevant only to the hardware watchpoint types bp_read_watchpoint
1639 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1640 not user-visible - its hit is suppressed if the memory content has
1641 not changed.
1642
1643 The following constraints influence the location where we can reset
1644 hardware watchpoints:
1645
1646 * target_stopped_by_watchpoint and target_stopped_data_address are
1647 called several times when GDB stops.
1648
1649 [linux]
1650 * Multiple hardware watchpoints can be hit at the same time,
1651 causing GDB to stop. GDB only presents one hardware watchpoint
1652 hit at a time as the reason for stopping, and all the other hits
1653 are presented later, one after the other, each time the user
1654 requests the execution to be resumed. Execution is not resumed
1655 for the threads still having pending hit event stored in
1656 LWP_INFO->STATUS. While the watchpoint is already removed from
1657 the inferior on the first stop the thread hit event is kept being
1658 reported from its cached value by linux_nat_stopped_data_address
1659 until the real thread resume happens after the watchpoint gets
1660 presented and thus its LWP_INFO->STATUS gets reset.
1661
1662 Therefore the hardware watchpoint hit can get safely reset on the
1663 watchpoint removal from inferior. */
a79d3c27 1664
b40ce68a 1665static void
3a5c3e22 1666update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1667{
a5606eee 1668 int within_current_scope;
a5606eee 1669 struct frame_id saved_frame_id;
66076460 1670 int frame_saved;
a5606eee 1671
f6bc2008
PA
1672 /* If this is a local watchpoint, we only want to check if the
1673 watchpoint frame is in scope if the current thread is the thread
1674 that was used to create the watchpoint. */
1675 if (!watchpoint_in_thread_scope (b))
1676 return;
1677
3a5c3e22 1678 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1679 return;
1680
66076460 1681 frame_saved = 0;
a5606eee
VP
1682
1683 /* Determine if the watchpoint is within scope. */
1684 if (b->exp_valid_block == NULL)
1685 within_current_scope = 1;
1686 else
1687 {
b5db5dfc
UW
1688 struct frame_info *fi = get_current_frame ();
1689 struct gdbarch *frame_arch = get_frame_arch (fi);
1690 CORE_ADDR frame_pc = get_frame_pc (fi);
1691
1692 /* If we're in a function epilogue, unwinding may not work
1693 properly, so do not attempt to recreate locations at this
1694 point. See similar comments in watchpoint_check. */
1695 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1696 return;
66076460
DJ
1697
1698 /* Save the current frame's ID so we can restore it after
1699 evaluating the watchpoint expression on its own frame. */
1700 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1701 took a frame parameter, so that we didn't have to change the
1702 selected frame. */
1703 frame_saved = 1;
1704 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1705
a5606eee
VP
1706 fi = frame_find_by_id (b->watchpoint_frame);
1707 within_current_scope = (fi != NULL);
1708 if (within_current_scope)
1709 select_frame (fi);
1710 }
1711
b5db5dfc
UW
1712 /* We don't free locations. They are stored in the bp_location array
1713 and update_global_location_list will eventually delete them and
1714 remove breakpoints if needed. */
3a5c3e22 1715 b->base.loc = NULL;
b5db5dfc 1716
a5606eee
VP
1717 if (within_current_scope && reparse)
1718 {
1719 char *s;
d63d0675 1720
a5606eee
VP
1721 if (b->exp)
1722 {
1723 xfree (b->exp);
1724 b->exp = NULL;
1725 }
d63d0675 1726 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1727 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1728 /* If the meaning of expression itself changed, the old value is
1729 no longer relevant. We don't want to report a watchpoint hit
1730 to the user when the old value and the new value may actually
1731 be completely different objects. */
1732 value_free (b->val);
fa4727a6
DJ
1733 b->val = NULL;
1734 b->val_valid = 0;
60e1c644
PA
1735
1736 /* Note that unlike with breakpoints, the watchpoint's condition
1737 expression is stored in the breakpoint object, not in the
1738 locations (re)created below. */
3a5c3e22 1739 if (b->base.cond_string != NULL)
60e1c644
PA
1740 {
1741 if (b->cond_exp != NULL)
1742 {
1743 xfree (b->cond_exp);
1744 b->cond_exp = NULL;
1745 }
1746
3a5c3e22 1747 s = b->base.cond_string;
1bb9788d 1748 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1749 }
a5606eee 1750 }
a5606eee
VP
1751
1752 /* If we failed to parse the expression, for example because
1753 it refers to a global variable in a not-yet-loaded shared library,
1754 don't try to insert watchpoint. We don't automatically delete
1755 such watchpoint, though, since failure to parse expression
1756 is different from out-of-scope watchpoint. */
2d134ed3
PA
1757 if ( !target_has_execution)
1758 {
1759 /* Without execution, memory can't change. No use to try and
1760 set watchpoint locations. The watchpoint will be reset when
1761 the target gains execution, through breakpoint_re_set. */
1762 }
1763 else if (within_current_scope && b->exp)
a5606eee 1764 {
0cf6dd15 1765 int pc = 0;
fa4727a6 1766 struct value *val_chain, *v, *result, *next;
2d134ed3 1767 struct program_space *frame_pspace;
a5606eee 1768
0cf6dd15 1769 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1770
a5606eee
VP
1771 /* Avoid setting b->val if it's already set. The meaning of
1772 b->val is 'the last value' user saw, and we should update
1773 it only if we reported that last value to user. As it
9c06b0b4
TJB
1774 happens, the code that reports it updates b->val directly.
1775 We don't keep track of the memory value for masked
1776 watchpoints. */
3a5c3e22 1777 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1778 {
1779 b->val = v;
1780 b->val_valid = 1;
1781 }
a5606eee 1782
2d134ed3
PA
1783 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1784
a5606eee 1785 /* Look at each value on the value chain. */
9fa40276 1786 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1787 {
1788 /* If it's a memory location, and GDB actually needed
1789 its contents to evaluate the expression, then we
fa4727a6
DJ
1790 must watch it. If the first value returned is
1791 still lazy, that means an error occurred reading it;
1792 watch it anyway in case it becomes readable. */
a5606eee 1793 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1794 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1795 {
1796 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1797
a5606eee
VP
1798 /* We only watch structs and arrays if user asked
1799 for it explicitly, never if they just happen to
1800 appear in the middle of some value chain. */
fa4727a6 1801 if (v == result
a5606eee
VP
1802 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1803 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1804 {
1805 CORE_ADDR addr;
1806 int len, type;
1807 struct bp_location *loc, **tmp;
1808
42ae5230 1809 addr = value_address (v);
a5606eee
VP
1810 len = TYPE_LENGTH (value_type (v));
1811 type = hw_write;
3a5c3e22 1812 if (b->base.type == bp_read_watchpoint)
a5606eee 1813 type = hw_read;
3a5c3e22 1814 else if (b->base.type == bp_access_watchpoint)
a5606eee 1815 type = hw_access;
3a5c3e22
PA
1816
1817 loc = allocate_bp_location (&b->base);
1818 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1819 ;
1820 *tmp = loc;
a6d9a66e 1821 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1822
1823 loc->pspace = frame_pspace;
a5606eee
VP
1824 loc->address = addr;
1825 loc->length = len;
1826 loc->watchpoint_type = type;
1827 }
1828 }
9fa40276
TJB
1829 }
1830
1831 /* Change the type of breakpoint between hardware assisted or
1832 an ordinary watchpoint depending on the hardware support
1833 and free hardware slots. REPARSE is set when the inferior
1834 is started. */
a9634178 1835 if (reparse)
9fa40276 1836 {
e09342b5 1837 int reg_cnt;
9fa40276
TJB
1838 enum bp_loc_type loc_type;
1839 struct bp_location *bl;
a5606eee 1840
a9634178 1841 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1842
1843 if (reg_cnt)
9fa40276
TJB
1844 {
1845 int i, target_resources_ok, other_type_used;
a1398e0c 1846 enum bptype type;
9fa40276 1847
a9634178
TJB
1848 /* Use an exact watchpoint when there's only one memory region to be
1849 watched, and only one debug register is needed to watch it. */
1850 b->exact = target_exact_watchpoints && reg_cnt == 1;
1851
9fa40276 1852 /* We need to determine how many resources are already
e09342b5
TJB
1853 used for all other hardware watchpoints plus this one
1854 to see if we still have enough resources to also fit
a1398e0c
PA
1855 this watchpoint in as well. */
1856
1857 /* If this is a software watchpoint, we try to turn it
1858 to a hardware one -- count resources as if B was of
1859 hardware watchpoint type. */
1860 type = b->base.type;
1861 if (type == bp_watchpoint)
1862 type = bp_hardware_watchpoint;
1863
1864 /* This watchpoint may or may not have been placed on
1865 the list yet at this point (it won't be in the list
1866 if we're trying to create it for the first time,
1867 through watch_command), so always account for it
1868 manually. */
1869
1870 /* Count resources used by all watchpoints except B. */
1871 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1872
1873 /* Add in the resources needed for B. */
1874 i += hw_watchpoint_use_count (&b->base);
1875
1876 target_resources_ok
1877 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1878 if (target_resources_ok <= 0)
a9634178 1879 {
3a5c3e22 1880 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1881
1882 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1883 error (_("Target does not support this type of "
1884 "hardware watchpoint."));
9c06b0b4
TJB
1885 else if (target_resources_ok < 0 && !sw_mode)
1886 error (_("There are not enough available hardware "
1887 "resources for this watchpoint."));
a1398e0c
PA
1888
1889 /* Downgrade to software watchpoint. */
1890 b->base.type = bp_watchpoint;
1891 }
1892 else
1893 {
1894 /* If this was a software watchpoint, we've just
1895 found we have enough resources to turn it to a
1896 hardware watchpoint. Otherwise, this is a
1897 nop. */
1898 b->base.type = type;
a9634178 1899 }
9fa40276 1900 }
3a5c3e22 1901 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1902 error (_("Expression cannot be implemented with "
1903 "read/access watchpoint."));
9fa40276 1904 else
3a5c3e22 1905 b->base.type = bp_watchpoint;
9fa40276 1906
3a5c3e22 1907 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1908 : bp_loc_hardware_watchpoint);
3a5c3e22 1909 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1910 bl->loc_type = loc_type;
1911 }
1912
1913 for (v = val_chain; v; v = next)
1914 {
a5606eee
VP
1915 next = value_next (v);
1916 if (v != b->val)
1917 value_free (v);
1918 }
1919
c7437ca6
PA
1920 /* If a software watchpoint is not watching any memory, then the
1921 above left it without any location set up. But,
1922 bpstat_stop_status requires a location to be able to report
1923 stops, so make sure there's at least a dummy one. */
3a5c3e22 1924 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1925 {
3a5c3e22
PA
1926 struct breakpoint *base = &b->base;
1927 base->loc = allocate_bp_location (base);
1928 base->loc->pspace = frame_pspace;
1929 base->loc->address = -1;
1930 base->loc->length = -1;
1931 base->loc->watchpoint_type = -1;
c7437ca6 1932 }
a5606eee
VP
1933 }
1934 else if (!within_current_scope)
7270d8f2 1935 {
ac74f770
MS
1936 printf_filtered (_("\
1937Watchpoint %d deleted because the program has left the block\n\
1938in which its expression is valid.\n"),
3a5c3e22 1939 b->base.number);
d0fb5eae 1940 watchpoint_del_at_next_stop (b);
7270d8f2 1941 }
a5606eee
VP
1942
1943 /* Restore the selected frame. */
66076460
DJ
1944 if (frame_saved)
1945 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1946}
1947
a5606eee 1948
74960c60 1949/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1950 inserted in the inferior. We don't differentiate the type of BL's owner
1951 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1952 breakpoint_ops is not defined, because in insert_bp_location,
1953 tracepoint's insert_location will not be called. */
74960c60 1954static int
35df4500 1955should_be_inserted (struct bp_location *bl)
74960c60 1956{
35df4500 1957 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1958 return 0;
1959
35df4500 1960 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1961 return 0;
1962
35df4500 1963 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1964 return 0;
1965
f8eba3c6
TT
1966 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1967 return 0;
1968
56710373
PA
1969 /* This is set for example, when we're attached to the parent of a
1970 vfork, and have detached from the child. The child is running
1971 free, and we expect it to do an exec or exit, at which point the
1972 OS makes the parent schedulable again (and the target reports
1973 that the vfork is done). Until the child is done with the shared
1974 memory region, do not insert breakpoints in the parent, otherwise
1975 the child could still trip on the parent's breakpoints. Since
1976 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1977 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1978 return 0;
1979
74960c60
VP
1980 return 1;
1981}
1982
934709f0
PW
1983/* Same as should_be_inserted but does the check assuming
1984 that the location is not duplicated. */
1985
1986static int
1987unduplicated_should_be_inserted (struct bp_location *bl)
1988{
1989 int result;
1990 const int save_duplicate = bl->duplicate;
1991
1992 bl->duplicate = 0;
1993 result = should_be_inserted (bl);
1994 bl->duplicate = save_duplicate;
1995 return result;
1996}
1997
b775012e
LM
1998/* Parses a conditional described by an expression COND into an
1999 agent expression bytecode suitable for evaluation
2000 by the bytecode interpreter. Return NULL if there was
2001 any error during parsing. */
2002
2003static struct agent_expr *
2004parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2005{
2006 struct agent_expr *aexpr = NULL;
2007 struct cleanup *old_chain = NULL;
2008 volatile struct gdb_exception ex;
2009
2010 if (!cond)
2011 return NULL;
2012
2013 /* We don't want to stop processing, so catch any errors
2014 that may show up. */
2015 TRY_CATCH (ex, RETURN_MASK_ERROR)
2016 {
2017 aexpr = gen_eval_for_expr (scope, cond);
2018 }
2019
2020 if (ex.reason < 0)
2021 {
2022 /* If we got here, it means the condition could not be parsed to a valid
2023 bytecode expression and thus can't be evaluated on the target's side.
2024 It's no use iterating through the conditions. */
2025 return NULL;
2026 }
2027
2028 /* We have a valid agent expression. */
2029 return aexpr;
2030}
2031
2032/* Based on location BL, create a list of breakpoint conditions to be
2033 passed on to the target. If we have duplicated locations with different
2034 conditions, we will add such conditions to the list. The idea is that the
2035 target will evaluate the list of conditions and will only notify GDB when
2036 one of them is true. */
2037
2038static void
2039build_target_condition_list (struct bp_location *bl)
2040{
2041 struct bp_location **locp = NULL, **loc2p;
2042 int null_condition_or_parse_error = 0;
2043 int modified = bl->needs_update;
2044 struct bp_location *loc;
2045
2046 /* This is only meaningful if the target is
2047 evaluating conditions and if the user has
2048 opted for condition evaluation on the target's
2049 side. */
2050 if (gdb_evaluates_breakpoint_condition_p ()
2051 || !target_supports_evaluation_of_breakpoint_conditions ())
2052 return;
2053
2054 /* Do a first pass to check for locations with no assigned
2055 conditions or conditions that fail to parse to a valid agent expression
2056 bytecode. If any of these happen, then it's no use to send conditions
2057 to the target since this location will always trigger and generate a
2058 response back to GDB. */
2059 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2060 {
2061 loc = (*loc2p);
2062 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2063 {
2064 if (modified)
2065 {
2066 struct agent_expr *aexpr;
2067
2068 /* Re-parse the conditions since something changed. In that
2069 case we already freed the condition bytecodes (see
2070 force_breakpoint_reinsertion). We just
2071 need to parse the condition to bytecodes again. */
2072 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2073 loc->cond_bytecode = aexpr;
2074
2075 /* Check if we managed to parse the conditional expression
2076 correctly. If not, we will not send this condition
2077 to the target. */
2078 if (aexpr)
2079 continue;
2080 }
2081
2082 /* If we have a NULL bytecode expression, it means something
2083 went wrong or we have a null condition expression. */
2084 if (!loc->cond_bytecode)
2085 {
2086 null_condition_or_parse_error = 1;
2087 break;
2088 }
2089 }
2090 }
2091
2092 /* If any of these happened, it means we will have to evaluate the conditions
2093 for the location's address on gdb's side. It is no use keeping bytecodes
2094 for all the other duplicate locations, thus we free all of them here.
2095
2096 This is so we have a finer control over which locations' conditions are
2097 being evaluated by GDB or the remote stub. */
2098 if (null_condition_or_parse_error)
2099 {
2100 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2101 {
2102 loc = (*loc2p);
2103 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2104 {
2105 /* Only go as far as the first NULL bytecode is
2106 located. */
2107 if (!loc->cond_bytecode)
2108 return;
2109
2110 free_agent_expr (loc->cond_bytecode);
2111 loc->cond_bytecode = NULL;
2112 }
2113 }
2114 }
2115
2116 /* No NULL conditions or failed bytecode generation. Build a condition list
2117 for this location's address. */
2118 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2119 {
2120 loc = (*loc2p);
2121 if (loc->cond
2122 && is_breakpoint (loc->owner)
2123 && loc->pspace->num == bl->pspace->num
2124 && loc->owner->enable_state == bp_enabled
2125 && loc->enabled)
2126 /* Add the condition to the vector. This will be used later to send the
2127 conditions to the target. */
2128 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2129 loc->cond_bytecode);
2130 }
2131
2132 return;
2133}
2134
35df4500
TJB
2135/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2136 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2137 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2138 Returns 0 for success, 1 if the bp_location type is not supported or
2139 -1 for failure.
879bfdc2 2140
4a64f543
MS
2141 NOTE drow/2003-09-09: This routine could be broken down to an
2142 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2143static int
35df4500 2144insert_bp_location (struct bp_location *bl,
26bb91f3 2145 struct ui_file *tmp_error_stream,
3fbb6ffa 2146 int *disabled_breaks,
26bb91f3 2147 int *hw_breakpoint_error)
879bfdc2
DJ
2148{
2149 int val = 0;
2150
b775012e 2151 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2152 return 0;
2153
35c63cd8
JB
2154 /* Note we don't initialize bl->target_info, as that wipes out
2155 the breakpoint location's shadow_contents if the breakpoint
2156 is still inserted at that location. This in turn breaks
2157 target_read_memory which depends on these buffers when
2158 a memory read is requested at the breakpoint location:
2159 Once the target_info has been wiped, we fail to see that
2160 we have a breakpoint inserted at that address and thus
2161 read the breakpoint instead of returning the data saved in
2162 the breakpoint location's shadow contents. */
35df4500
TJB
2163 bl->target_info.placed_address = bl->address;
2164 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2165 bl->target_info.length = bl->length;
8181d85f 2166
b775012e
LM
2167 /* When working with target-side conditions, we must pass all the conditions
2168 for the same breakpoint address down to the target since GDB will not
2169 insert those locations. With a list of breakpoint conditions, the target
2170 can decide when to stop and notify GDB. */
2171
2172 if (is_breakpoint (bl->owner))
2173 {
2174 build_target_condition_list (bl);
2175 /* Reset the condition modification marker. */
2176 bl->needs_update = 0;
2177 }
2178
35df4500
TJB
2179 if (bl->loc_type == bp_loc_software_breakpoint
2180 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2181 {
35df4500 2182 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2183 {
2184 /* If the explicitly specified breakpoint type
2185 is not hardware breakpoint, check the memory map to see
2186 if the breakpoint address is in read only memory or not.
4a64f543 2187
765dc015
VP
2188 Two important cases are:
2189 - location type is not hardware breakpoint, memory
2190 is readonly. We change the type of the location to
2191 hardware breakpoint.
4a64f543
MS
2192 - location type is hardware breakpoint, memory is
2193 read-write. This means we've previously made the
2194 location hardware one, but then the memory map changed,
2195 so we undo.
765dc015 2196
4a64f543
MS
2197 When breakpoints are removed, remove_breakpoints will use
2198 location types we've just set here, the only possible
2199 problem is that memory map has changed during running
2200 program, but it's not going to work anyway with current
2201 gdb. */
765dc015 2202 struct mem_region *mr
35df4500 2203 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2204
2205 if (mr)
2206 {
2207 if (automatic_hardware_breakpoints)
2208 {
765dc015
VP
2209 enum bp_loc_type new_type;
2210
2211 if (mr->attrib.mode != MEM_RW)
2212 new_type = bp_loc_hardware_breakpoint;
2213 else
2214 new_type = bp_loc_software_breakpoint;
2215
35df4500 2216 if (new_type != bl->loc_type)
765dc015
VP
2217 {
2218 static int said = 0;
cc59ec59 2219
35df4500 2220 bl->loc_type = new_type;
765dc015
VP
2221 if (!said)
2222 {
3e43a32a
MS
2223 fprintf_filtered (gdb_stdout,
2224 _("Note: automatically using "
2225 "hardware breakpoints for "
2226 "read-only addresses.\n"));
765dc015
VP
2227 said = 1;
2228 }
2229 }
2230 }
35df4500 2231 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 2232 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
2233 warning (_("cannot set software breakpoint "
2234 "at readonly address %s"),
35df4500 2235 paddress (bl->gdbarch, bl->address));
765dc015
VP
2236 }
2237 }
2238
879bfdc2
DJ
2239 /* First check to see if we have to handle an overlay. */
2240 if (overlay_debugging == ovly_off
35df4500
TJB
2241 || bl->section == NULL
2242 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2243 {
2244 /* No overlay handling: just set the breakpoint. */
2245
348d480f 2246 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
2247 }
2248 else
2249 {
4a64f543 2250 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2251 Shall we set a breakpoint at the LMA? */
2252 if (!overlay_events_enabled)
2253 {
2254 /* Yes -- overlay event support is not active,
2255 so we must try to set a breakpoint at the LMA.
2256 This will not work for a hardware breakpoint. */
35df4500 2257 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2258 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2259 bl->owner->number);
879bfdc2
DJ
2260 else
2261 {
35df4500
TJB
2262 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2263 bl->section);
879bfdc2 2264 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2265 bl->overlay_target_info = bl->target_info;
2266 bl->overlay_target_info.placed_address = addr;
2267 val = target_insert_breakpoint (bl->gdbarch,
2268 &bl->overlay_target_info);
879bfdc2 2269 if (val != 0)
99361f52 2270 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2271 "Overlay breakpoint %d "
2272 "failed: in ROM?\n",
35df4500 2273 bl->owner->number);
879bfdc2
DJ
2274 }
2275 }
2276 /* Shall we set a breakpoint at the VMA? */
35df4500 2277 if (section_is_mapped (bl->section))
879bfdc2
DJ
2278 {
2279 /* Yes. This overlay section is mapped into memory. */
348d480f 2280 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
2281 }
2282 else
2283 {
2284 /* No. This breakpoint will not be inserted.
2285 No error, but do not mark the bp as 'inserted'. */
2286 return 0;
2287 }
2288 }
2289
2290 if (val)
2291 {
2292 /* Can't set the breakpoint. */
35df4500 2293 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 2294 {
4a64f543 2295 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 2296 val = 0;
35df4500 2297 bl->shlib_disabled = 1;
8d3788bd 2298 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2299 if (!*disabled_breaks)
2300 {
2301 fprintf_unfiltered (tmp_error_stream,
2302 "Cannot insert breakpoint %d.\n",
2303 bl->owner->number);
2304 fprintf_unfiltered (tmp_error_stream,
2305 "Temporarily disabling shared "
2306 "library breakpoints:\n");
2307 }
2308 *disabled_breaks = 1;
879bfdc2 2309 fprintf_unfiltered (tmp_error_stream,
35df4500 2310 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
2311 }
2312 else
879bfdc2 2313 {
35df4500 2314 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
2315 {
2316 *hw_breakpoint_error = 1;
3e43a32a
MS
2317 fprintf_unfiltered (tmp_error_stream,
2318 "Cannot insert hardware "
2319 "breakpoint %d.\n",
35df4500 2320 bl->owner->number);
879bfdc2
DJ
2321 }
2322 else
2323 {
2324 fprintf_unfiltered (tmp_error_stream,
2325 "Cannot insert breakpoint %d.\n",
35df4500 2326 bl->owner->number);
879bfdc2
DJ
2327 fprintf_filtered (tmp_error_stream,
2328 "Error accessing memory address ");
35df4500 2329 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 2330 tmp_error_stream);
879bfdc2
DJ
2331 fprintf_filtered (tmp_error_stream, ": %s.\n",
2332 safe_strerror (val));
2333 }
2334
2335 }
2336 }
2337 else
35df4500 2338 bl->inserted = 1;
879bfdc2
DJ
2339
2340 return val;
2341 }
2342
35df4500 2343 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2344 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2345 watchpoints. It's not clear that it's necessary... */
35df4500 2346 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2347 {
77b06cd7
TJB
2348 gdb_assert (bl->owner->ops != NULL
2349 && bl->owner->ops->insert_location != NULL);
2350
2351 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2352
2353 /* If trying to set a read-watchpoint, and it turns out it's not
2354 supported, try emulating one with an access watchpoint. */
35df4500 2355 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2356 {
2357 struct bp_location *loc, **loc_temp;
2358
2359 /* But don't try to insert it, if there's already another
2360 hw_access location that would be considered a duplicate
2361 of this one. */
2362 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2363 if (loc != bl
85d721b8 2364 && loc->watchpoint_type == hw_access
35df4500 2365 && watchpoint_locations_match (bl, loc))
85d721b8 2366 {
35df4500
TJB
2367 bl->duplicate = 1;
2368 bl->inserted = 1;
2369 bl->target_info = loc->target_info;
2370 bl->watchpoint_type = hw_access;
85d721b8
PA
2371 val = 0;
2372 break;
2373 }
2374
2375 if (val == 1)
2376 {
77b06cd7
TJB
2377 bl->watchpoint_type = hw_access;
2378 val = bl->owner->ops->insert_location (bl);
2379
2380 if (val)
2381 /* Back to the original value. */
2382 bl->watchpoint_type = hw_read;
85d721b8
PA
2383 }
2384 }
2385
35df4500 2386 bl->inserted = (val == 0);
879bfdc2
DJ
2387 }
2388
35df4500 2389 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2390 {
77b06cd7
TJB
2391 gdb_assert (bl->owner->ops != NULL
2392 && bl->owner->ops->insert_location != NULL);
2393
2394 val = bl->owner->ops->insert_location (bl);
2395 if (val)
2396 {
2397 bl->owner->enable_state = bp_disabled;
2398
2399 if (val == 1)
2400 warning (_("\
2401Error inserting catchpoint %d: Your system does not support this type\n\
2402of catchpoint."), bl->owner->number);
2403 else
2404 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2405 }
2406
2407 bl->inserted = (val == 0);
1640b821
DJ
2408
2409 /* We've already printed an error message if there was a problem
2410 inserting this catchpoint, and we've disabled the catchpoint,
2411 so just return success. */
2412 return 0;
879bfdc2
DJ
2413 }
2414
2415 return 0;
2416}
2417
6c95b8df
PA
2418/* This function is called when program space PSPACE is about to be
2419 deleted. It takes care of updating breakpoints to not reference
2420 PSPACE anymore. */
2421
2422void
2423breakpoint_program_space_exit (struct program_space *pspace)
2424{
2425 struct breakpoint *b, *b_temp;
876fa593 2426 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2427
2428 /* Remove any breakpoint that was set through this program space. */
2429 ALL_BREAKPOINTS_SAFE (b, b_temp)
2430 {
2431 if (b->pspace == pspace)
2432 delete_breakpoint (b);
2433 }
2434
2435 /* Breakpoints set through other program spaces could have locations
2436 bound to PSPACE as well. Remove those. */
876fa593 2437 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2438 {
2439 struct bp_location *tmp;
2440
2441 if (loc->pspace == pspace)
2442 {
2bdf28a0 2443 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2444 if (loc->owner->loc == loc)
2445 loc->owner->loc = loc->next;
2446 else
2447 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2448 if (tmp->next == loc)
2449 {
2450 tmp->next = loc->next;
2451 break;
2452 }
2453 }
2454 }
2455
2456 /* Now update the global location list to permanently delete the
2457 removed locations above. */
2458 update_global_location_list (0);
2459}
2460
74960c60
VP
2461/* Make sure all breakpoints are inserted in inferior.
2462 Throws exception on any error.
2463 A breakpoint that is already inserted won't be inserted
2464 again, so calling this function twice is safe. */
2465void
2466insert_breakpoints (void)
2467{
2468 struct breakpoint *bpt;
2469
2470 ALL_BREAKPOINTS (bpt)
2471 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2472 {
2473 struct watchpoint *w = (struct watchpoint *) bpt;
2474
2475 update_watchpoint (w, 0 /* don't reparse. */);
2476 }
74960c60 2477
b60e7edf 2478 update_global_location_list (1);
74960c60 2479
c35b1492
PA
2480 /* update_global_location_list does not insert breakpoints when
2481 always_inserted_mode is not enabled. Explicitly insert them
2482 now. */
2483 if (!breakpoints_always_inserted_mode ())
74960c60
VP
2484 insert_breakpoint_locations ();
2485}
2486
20388dd6
YQ
2487/* Invoke CALLBACK for each of bp_location. */
2488
2489void
2490iterate_over_bp_locations (walk_bp_location_callback callback)
2491{
2492 struct bp_location *loc, **loc_tmp;
2493
2494 ALL_BP_LOCATIONS (loc, loc_tmp)
2495 {
2496 callback (loc, NULL);
2497 }
2498}
2499
b775012e
LM
2500/* This is used when we need to synch breakpoint conditions between GDB and the
2501 target. It is the case with deleting and disabling of breakpoints when using
2502 always-inserted mode. */
2503
2504static void
2505update_inserted_breakpoint_locations (void)
2506{
2507 struct bp_location *bl, **blp_tmp;
2508 int error_flag = 0;
2509 int val = 0;
2510 int disabled_breaks = 0;
2511 int hw_breakpoint_error = 0;
2512
2513 struct ui_file *tmp_error_stream = mem_fileopen ();
2514 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2515
2516 /* Explicitly mark the warning -- this will only be printed if
2517 there was an error. */
2518 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2519
2520 save_current_space_and_thread ();
2521
2522 ALL_BP_LOCATIONS (bl, blp_tmp)
2523 {
2524 /* We only want to update software breakpoints and hardware
2525 breakpoints. */
2526 if (!is_breakpoint (bl->owner))
2527 continue;
2528
2529 /* We only want to update locations that are already inserted
2530 and need updating. This is to avoid unwanted insertion during
2531 deletion of breakpoints. */
2532 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2533 continue;
2534
2535 switch_to_program_space_and_thread (bl->pspace);
2536
2537 /* For targets that support global breakpoints, there's no need
2538 to select an inferior to insert breakpoint to. In fact, even
2539 if we aren't attached to any process yet, we should still
2540 insert breakpoints. */
2541 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2542 && ptid_equal (inferior_ptid, null_ptid))
2543 continue;
2544
2545 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2546 &hw_breakpoint_error);
2547 if (val)
2548 error_flag = val;
2549 }
2550
2551 if (error_flag)
2552 {
2553 target_terminal_ours_for_output ();
2554 error_stream (tmp_error_stream);
2555 }
2556
2557 do_cleanups (cleanups);
2558}
2559
c30eee59 2560/* Used when starting or continuing the program. */
c906108c 2561
74960c60
VP
2562static void
2563insert_breakpoint_locations (void)
c906108c 2564{
a5606eee 2565 struct breakpoint *bpt;
35df4500 2566 struct bp_location *bl, **blp_tmp;
eacd795a 2567 int error_flag = 0;
c906108c 2568 int val = 0;
3fbb6ffa 2569 int disabled_breaks = 0;
81d0cc19 2570 int hw_breakpoint_error = 0;
c906108c 2571
81d0cc19 2572 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2573 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2574
81d0cc19
GS
2575 /* Explicitly mark the warning -- this will only be printed if
2576 there was an error. */
2577 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2578
2579 save_current_space_and_thread ();
2580
35df4500 2581 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2582 {
b775012e 2583 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2584 continue;
2585
4a64f543
MS
2586 /* There is no point inserting thread-specific breakpoints if
2587 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2588 has BL->OWNER always non-NULL. */
35df4500
TJB
2589 if (bl->owner->thread != -1
2590 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2591 continue;
2592
35df4500 2593 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2594
2595 /* For targets that support global breakpoints, there's no need
2596 to select an inferior to insert breakpoint to. In fact, even
2597 if we aren't attached to any process yet, we should still
2598 insert breakpoints. */
2599 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2600 && ptid_equal (inferior_ptid, null_ptid))
2601 continue;
2602
3fbb6ffa
TJB
2603 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2604 &hw_breakpoint_error);
879bfdc2 2605 if (val)
eacd795a 2606 error_flag = val;
879bfdc2 2607 }
c906108c 2608
4a64f543
MS
2609 /* If we failed to insert all locations of a watchpoint, remove
2610 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2611 ALL_BREAKPOINTS (bpt)
2612 {
2613 int some_failed = 0;
2614 struct bp_location *loc;
2615
2616 if (!is_hardware_watchpoint (bpt))
2617 continue;
2618
d6b74ac4 2619 if (!breakpoint_enabled (bpt))
a5606eee 2620 continue;
74960c60
VP
2621
2622 if (bpt->disposition == disp_del_at_next_stop)
2623 continue;
a5606eee
VP
2624
2625 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2626 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2627 {
2628 some_failed = 1;
2629 break;
2630 }
2631 if (some_failed)
2632 {
2633 for (loc = bpt->loc; loc; loc = loc->next)
2634 if (loc->inserted)
2635 remove_breakpoint (loc, mark_uninserted);
2636
2637 hw_breakpoint_error = 1;
2638 fprintf_unfiltered (tmp_error_stream,
2639 "Could not insert hardware watchpoint %d.\n",
2640 bpt->number);
eacd795a 2641 error_flag = -1;
a5606eee
VP
2642 }
2643 }
2644
eacd795a 2645 if (error_flag)
81d0cc19
GS
2646 {
2647 /* If a hardware breakpoint or watchpoint was inserted, add a
2648 message about possibly exhausted resources. */
879bfdc2 2649 if (hw_breakpoint_error)
81d0cc19 2650 {
c6510018
MS
2651 fprintf_unfiltered (tmp_error_stream,
2652 "Could not insert hardware breakpoints:\n\
2653You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2654 }
81d0cc19
GS
2655 target_terminal_ours_for_output ();
2656 error_stream (tmp_error_stream);
2657 }
f7545552
TT
2658
2659 do_cleanups (cleanups);
c906108c
SS
2660}
2661
c30eee59
TJB
2662/* Used when the program stops.
2663 Returns zero if successful, or non-zero if there was a problem
2664 removing a breakpoint location. */
2665
c906108c 2666int
fba45db2 2667remove_breakpoints (void)
c906108c 2668{
35df4500 2669 struct bp_location *bl, **blp_tmp;
3a1bae8e 2670 int val = 0;
c906108c 2671
35df4500 2672 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2673 {
1e4d1764 2674 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2675 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2676 }
3a1bae8e 2677 return val;
c906108c
SS
2678}
2679
6c95b8df
PA
2680/* Remove breakpoints of process PID. */
2681
2682int
2683remove_breakpoints_pid (int pid)
2684{
35df4500 2685 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2686 int val;
2687 struct inferior *inf = find_inferior_pid (pid);
2688
35df4500 2689 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2690 {
35df4500 2691 if (bl->pspace != inf->pspace)
6c95b8df
PA
2692 continue;
2693
35df4500 2694 if (bl->inserted)
6c95b8df 2695 {
35df4500 2696 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2697 if (val != 0)
2698 return val;
2699 }
2700 }
2701 return 0;
2702}
2703
c906108c 2704int
fba45db2 2705reattach_breakpoints (int pid)
c906108c 2706{
6c95b8df 2707 struct cleanup *old_chain;
35df4500 2708 struct bp_location *bl, **blp_tmp;
c906108c 2709 int val;
86b887df 2710 struct ui_file *tmp_error_stream;
3fbb6ffa 2711 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2712 struct inferior *inf;
2713 struct thread_info *tp;
2714
2715 tp = any_live_thread_of_process (pid);
2716 if (tp == NULL)
2717 return 1;
2718
2719 inf = find_inferior_pid (pid);
2720 old_chain = save_inferior_ptid ();
2721
2722 inferior_ptid = tp->ptid;
a4954f26 2723
86b887df 2724 tmp_error_stream = mem_fileopen ();
a4954f26 2725 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2726
35df4500 2727 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2728 {
35df4500 2729 if (bl->pspace != inf->pspace)
6c95b8df
PA
2730 continue;
2731
35df4500 2732 if (bl->inserted)
c5aa993b 2733 {
35df4500 2734 bl->inserted = 0;
3fbb6ffa 2735 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2736 if (val != 0)
2737 {
ce696e05 2738 do_cleanups (old_chain);
c5aa993b
JM
2739 return val;
2740 }
2741 }
2742 }
ce696e05 2743 do_cleanups (old_chain);
c906108c
SS
2744 return 0;
2745}
2746
e58b0e63
PA
2747static int internal_breakpoint_number = -1;
2748
84f4c1fe
PM
2749/* Set the breakpoint number of B, depending on the value of INTERNAL.
2750 If INTERNAL is non-zero, the breakpoint number will be populated
2751 from internal_breakpoint_number and that variable decremented.
e5dd4106 2752 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2753 breakpoint_count and that value incremented. Internal breakpoints
2754 do not set the internal var bpnum. */
2755static void
2756set_breakpoint_number (int internal, struct breakpoint *b)
2757{
2758 if (internal)
2759 b->number = internal_breakpoint_number--;
2760 else
2761 {
2762 set_breakpoint_count (breakpoint_count + 1);
2763 b->number = breakpoint_count;
2764 }
2765}
2766
e62c965a 2767static struct breakpoint *
a6d9a66e 2768create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2769 CORE_ADDR address, enum bptype type,
c0a91b2b 2770 const struct breakpoint_ops *ops)
e62c965a 2771{
e62c965a
PP
2772 struct symtab_and_line sal;
2773 struct breakpoint *b;
2774
4a64f543 2775 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2776
2777 sal.pc = address;
2778 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2779 sal.pspace = current_program_space;
e62c965a 2780
06edf0c0 2781 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2782 b->number = internal_breakpoint_number--;
2783 b->disposition = disp_donttouch;
2784
2785 return b;
2786}
2787
17450429
PP
2788static const char *const longjmp_names[] =
2789 {
2790 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2791 };
2792#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2793
2794/* Per-objfile data private to breakpoint.c. */
2795struct breakpoint_objfile_data
2796{
2797 /* Minimal symbol for "_ovly_debug_event" (if any). */
2798 struct minimal_symbol *overlay_msym;
2799
2800 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2801 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2802
28106bc2
SDJ
2803 /* True if we have looked for longjmp probes. */
2804 int longjmp_searched;
2805
2806 /* SystemTap probe points for longjmp (if any). */
2807 VEC (probe_p) *longjmp_probes;
2808
17450429
PP
2809 /* Minimal symbol for "std::terminate()" (if any). */
2810 struct minimal_symbol *terminate_msym;
2811
2812 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2813 struct minimal_symbol *exception_msym;
28106bc2
SDJ
2814
2815 /* True if we have looked for exception probes. */
2816 int exception_searched;
2817
2818 /* SystemTap probe points for unwinding (if any). */
2819 VEC (probe_p) *exception_probes;
17450429
PP
2820};
2821
2822static const struct objfile_data *breakpoint_objfile_key;
2823
2824/* Minimal symbol not found sentinel. */
2825static struct minimal_symbol msym_not_found;
2826
2827/* Returns TRUE if MSYM point to the "not found" sentinel. */
2828
2829static int
2830msym_not_found_p (const struct minimal_symbol *msym)
2831{
2832 return msym == &msym_not_found;
2833}
2834
2835/* Return per-objfile data needed by breakpoint.c.
2836 Allocate the data if necessary. */
2837
2838static struct breakpoint_objfile_data *
2839get_breakpoint_objfile_data (struct objfile *objfile)
2840{
2841 struct breakpoint_objfile_data *bp_objfile_data;
2842
2843 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2844 if (bp_objfile_data == NULL)
2845 {
2846 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2847 sizeof (*bp_objfile_data));
2848
2849 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2850 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2851 }
2852 return bp_objfile_data;
2853}
2854
28106bc2
SDJ
2855static void
2856free_breakpoint_probes (struct objfile *obj, void *data)
2857{
2858 struct breakpoint_objfile_data *bp_objfile_data = data;
2859
2860 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
2861 VEC_free (probe_p, bp_objfile_data->exception_probes);
2862}
2863
e62c965a 2864static void
af02033e 2865create_overlay_event_breakpoint (void)
e62c965a 2866{
69de3c6a 2867 struct objfile *objfile;
af02033e 2868 const char *const func_name = "_ovly_debug_event";
e62c965a 2869
69de3c6a
PP
2870 ALL_OBJFILES (objfile)
2871 {
2872 struct breakpoint *b;
17450429
PP
2873 struct breakpoint_objfile_data *bp_objfile_data;
2874 CORE_ADDR addr;
69de3c6a 2875
17450429
PP
2876 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2877
2878 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2879 continue;
2880
2881 if (bp_objfile_data->overlay_msym == NULL)
2882 {
2883 struct minimal_symbol *m;
2884
2885 m = lookup_minimal_symbol_text (func_name, objfile);
2886 if (m == NULL)
2887 {
2888 /* Avoid future lookups in this objfile. */
2889 bp_objfile_data->overlay_msym = &msym_not_found;
2890 continue;
2891 }
2892 bp_objfile_data->overlay_msym = m;
2893 }
e62c965a 2894
17450429
PP
2895 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2896 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2897 bp_overlay_event,
2898 &internal_breakpoint_ops);
69de3c6a 2899 b->addr_string = xstrdup (func_name);
e62c965a 2900
69de3c6a
PP
2901 if (overlay_debugging == ovly_auto)
2902 {
2903 b->enable_state = bp_enabled;
2904 overlay_events_enabled = 1;
2905 }
2906 else
2907 {
2908 b->enable_state = bp_disabled;
2909 overlay_events_enabled = 0;
2910 }
e62c965a
PP
2911 }
2912 update_global_location_list (1);
2913}
2914
0fd8e87f 2915static void
af02033e 2916create_longjmp_master_breakpoint (void)
0fd8e87f 2917{
6c95b8df 2918 struct program_space *pspace;
6c95b8df
PA
2919 struct cleanup *old_chain;
2920
2921 old_chain = save_current_program_space ();
0fd8e87f 2922
6c95b8df 2923 ALL_PSPACES (pspace)
af02033e
PP
2924 {
2925 struct objfile *objfile;
2926
2927 set_current_program_space (pspace);
2928
2929 ALL_OBJFILES (objfile)
0fd8e87f 2930 {
af02033e
PP
2931 int i;
2932 struct gdbarch *gdbarch;
17450429 2933 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2934
af02033e
PP
2935 gdbarch = get_objfile_arch (objfile);
2936 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2937 continue;
2938
17450429
PP
2939 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2940
28106bc2
SDJ
2941 if (!bp_objfile_data->longjmp_searched)
2942 {
2943 bp_objfile_data->longjmp_probes
2944 = find_probes_in_objfile (objfile, "libc", "longjmp");
2945 bp_objfile_data->longjmp_searched = 1;
2946 }
2947
2948 if (bp_objfile_data->longjmp_probes != NULL)
2949 {
2950 int i;
2951 struct probe *probe;
2952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2953
2954 for (i = 0;
2955 VEC_iterate (probe_p,
2956 bp_objfile_data->longjmp_probes,
2957 i, probe);
2958 ++i)
2959 {
2960 struct breakpoint *b;
2961
2962 b = create_internal_breakpoint (gdbarch, probe->address,
2963 bp_longjmp_master,
2964 &internal_breakpoint_ops);
2965 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
2966 b->enable_state = bp_disabled;
2967 }
2968
2969 continue;
2970 }
2971
17450429 2972 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2973 {
2974 struct breakpoint *b;
af02033e 2975 const char *func_name;
17450429 2976 CORE_ADDR addr;
6c95b8df 2977
17450429 2978 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2979 continue;
0fd8e87f 2980
17450429
PP
2981 func_name = longjmp_names[i];
2982 if (bp_objfile_data->longjmp_msym[i] == NULL)
2983 {
2984 struct minimal_symbol *m;
2985
2986 m = lookup_minimal_symbol_text (func_name, objfile);
2987 if (m == NULL)
2988 {
2989 /* Prevent future lookups in this objfile. */
2990 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2991 continue;
2992 }
2993 bp_objfile_data->longjmp_msym[i] = m;
2994 }
2995
2996 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2997 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2998 &internal_breakpoint_ops);
af02033e
PP
2999 b->addr_string = xstrdup (func_name);
3000 b->enable_state = bp_disabled;
3001 }
0fd8e87f 3002 }
af02033e 3003 }
0fd8e87f 3004 update_global_location_list (1);
6c95b8df
PA
3005
3006 do_cleanups (old_chain);
0fd8e87f
UW
3007}
3008
af02033e 3009/* Create a master std::terminate breakpoint. */
aa7d318d 3010static void
af02033e 3011create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3012{
3013 struct program_space *pspace;
aa7d318d 3014 struct cleanup *old_chain;
af02033e 3015 const char *const func_name = "std::terminate()";
aa7d318d
TT
3016
3017 old_chain = save_current_program_space ();
3018
3019 ALL_PSPACES (pspace)
17450429
PP
3020 {
3021 struct objfile *objfile;
3022 CORE_ADDR addr;
3023
3024 set_current_program_space (pspace);
3025
aa7d318d
TT
3026 ALL_OBJFILES (objfile)
3027 {
3028 struct breakpoint *b;
17450429 3029 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3030
17450429 3031 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3032
17450429
PP
3033 if (msym_not_found_p (bp_objfile_data->terminate_msym))
3034 continue;
3035
3036 if (bp_objfile_data->terminate_msym == NULL)
3037 {
3038 struct minimal_symbol *m;
3039
3040 m = lookup_minimal_symbol (func_name, NULL, objfile);
3041 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
3042 && MSYMBOL_TYPE (m) != mst_file_text))
3043 {
3044 /* Prevent future lookups in this objfile. */
3045 bp_objfile_data->terminate_msym = &msym_not_found;
3046 continue;
3047 }
3048 bp_objfile_data->terminate_msym = m;
3049 }
aa7d318d 3050
17450429
PP
3051 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3052 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3053 bp_std_terminate_master,
3054 &internal_breakpoint_ops);
aa7d318d
TT
3055 b->addr_string = xstrdup (func_name);
3056 b->enable_state = bp_disabled;
3057 }
17450429
PP
3058 }
3059
aa7d318d
TT
3060 update_global_location_list (1);
3061
3062 do_cleanups (old_chain);
3063}
3064
186c406b
TT
3065/* Install a master breakpoint on the unwinder's debug hook. */
3066
70221824 3067static void
186c406b
TT
3068create_exception_master_breakpoint (void)
3069{
3070 struct objfile *objfile;
17450429 3071 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3072
3073 ALL_OBJFILES (objfile)
3074 {
17450429
PP
3075 struct breakpoint *b;
3076 struct gdbarch *gdbarch;
3077 struct breakpoint_objfile_data *bp_objfile_data;
3078 CORE_ADDR addr;
3079
3080 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3081
28106bc2
SDJ
3082 /* We prefer the SystemTap probe point if it exists. */
3083 if (!bp_objfile_data->exception_searched)
3084 {
3085 bp_objfile_data->exception_probes
3086 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3087 bp_objfile_data->exception_searched = 1;
3088 }
3089
3090 if (bp_objfile_data->exception_probes != NULL)
3091 {
3092 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3093 int i;
3094 struct probe *probe;
3095
3096 for (i = 0;
3097 VEC_iterate (probe_p,
3098 bp_objfile_data->exception_probes,
3099 i, probe);
3100 ++i)
3101 {
3102 struct breakpoint *b;
3103
3104 b = create_internal_breakpoint (gdbarch, probe->address,
3105 bp_exception_master,
3106 &internal_breakpoint_ops);
3107 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3108 b->enable_state = bp_disabled;
3109 }
3110
3111 continue;
3112 }
3113
3114 /* Otherwise, try the hook function. */
3115
17450429
PP
3116 if (msym_not_found_p (bp_objfile_data->exception_msym))
3117 continue;
3118
3119 gdbarch = get_objfile_arch (objfile);
186c406b 3120
17450429 3121 if (bp_objfile_data->exception_msym == NULL)
186c406b 3122 {
17450429 3123 struct minimal_symbol *debug_hook;
186c406b 3124
17450429
PP
3125 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3126 if (debug_hook == NULL)
3127 {
3128 bp_objfile_data->exception_msym = &msym_not_found;
3129 continue;
3130 }
3131
3132 bp_objfile_data->exception_msym = debug_hook;
186c406b 3133 }
17450429
PP
3134
3135 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3136 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3137 &current_target);
06edf0c0
PA
3138 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3139 &internal_breakpoint_ops);
17450429
PP
3140 b->addr_string = xstrdup (func_name);
3141 b->enable_state = bp_disabled;
186c406b
TT
3142 }
3143
3144 update_global_location_list (1);
3145}
3146
c906108c 3147void
fba45db2 3148update_breakpoints_after_exec (void)
c906108c 3149{
35df4500 3150 struct breakpoint *b, *b_tmp;
876fa593 3151 struct bp_location *bploc, **bplocp_tmp;
c906108c 3152
25b22b0a
PA
3153 /* We're about to delete breakpoints from GDB's lists. If the
3154 INSERTED flag is true, GDB will try to lift the breakpoints by
3155 writing the breakpoints' "shadow contents" back into memory. The
3156 "shadow contents" are NOT valid after an exec, so GDB should not
3157 do that. Instead, the target is responsible from marking
3158 breakpoints out as soon as it detects an exec. We don't do that
3159 here instead, because there may be other attempts to delete
3160 breakpoints after detecting an exec and before reaching here. */
876fa593 3161 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3162 if (bploc->pspace == current_program_space)
3163 gdb_assert (!bploc->inserted);
c906108c 3164
35df4500 3165 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3166 {
6c95b8df
PA
3167 if (b->pspace != current_program_space)
3168 continue;
3169
4a64f543 3170 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3171 if (b->type == bp_shlib_event)
3172 {
3173 delete_breakpoint (b);
3174 continue;
3175 }
c906108c 3176
4a64f543 3177 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3178 if (b->type == bp_jit_event)
3179 {
3180 delete_breakpoint (b);
3181 continue;
3182 }
3183
1900040c 3184 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3185 as must overlay event and longjmp master breakpoints. */
3186 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3187 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3188 || b->type == bp_exception_master)
c4093a6a
JM
3189 {
3190 delete_breakpoint (b);
3191 continue;
3192 }
3193
4a64f543 3194 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3195 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3196 {
3197 delete_breakpoint (b);
3198 continue;
3199 }
3200
611c83ae
PA
3201 /* Longjmp and longjmp-resume breakpoints are also meaningless
3202 after an exec. */
186c406b 3203 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3204 || b->type == bp_longjmp_call_dummy
186c406b 3205 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3206 {
3207 delete_breakpoint (b);
3208 continue;
3209 }
3210
ce78b96d
JB
3211 if (b->type == bp_catchpoint)
3212 {
3213 /* For now, none of the bp_catchpoint breakpoints need to
3214 do anything at this point. In the future, if some of
3215 the catchpoints need to something, we will need to add
3216 a new method, and call this method from here. */
3217 continue;
3218 }
3219
c5aa993b
JM
3220 /* bp_finish is a special case. The only way we ought to be able
3221 to see one of these when an exec() has happened, is if the user
3222 caught a vfork, and then said "finish". Ordinarily a finish just
3223 carries them to the call-site of the current callee, by setting
3224 a temporary bp there and resuming. But in this case, the finish
3225 will carry them entirely through the vfork & exec.
3226
3227 We don't want to allow a bp_finish to remain inserted now. But
3228 we can't safely delete it, 'cause finish_command has a handle to
3229 the bp on a bpstat, and will later want to delete it. There's a
3230 chance (and I've seen it happen) that if we delete the bp_finish
3231 here, that its storage will get reused by the time finish_command
3232 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3233 We really must allow finish_command to delete a bp_finish.
3234
e5dd4106 3235 In the absence of a general solution for the "how do we know
53a5351d
JM
3236 it's safe to delete something others may have handles to?"
3237 problem, what we'll do here is just uninsert the bp_finish, and
3238 let finish_command delete it.
3239
3240 (We know the bp_finish is "doomed" in the sense that it's
3241 momentary, and will be deleted as soon as finish_command sees
3242 the inferior stopped. So it doesn't matter that the bp's
3243 address is probably bogus in the new a.out, unlike e.g., the
3244 solib breakpoints.) */
c5aa993b 3245
c5aa993b
JM
3246 if (b->type == bp_finish)
3247 {
3248 continue;
3249 }
3250
3251 /* Without a symbolic address, we have little hope of the
3252 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3253 a.out. */
c5aa993b
JM
3254 if (b->addr_string == NULL)
3255 {
3256 delete_breakpoint (b);
3257 continue;
3258 }
c5aa993b 3259 }
1900040c 3260 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3261 create_overlay_event_breakpoint ();
3262 create_longjmp_master_breakpoint ();
3263 create_std_terminate_master_breakpoint ();
186c406b 3264 create_exception_master_breakpoint ();
c906108c
SS
3265}
3266
3267int
fba45db2 3268detach_breakpoints (int pid)
c906108c 3269{
35df4500 3270 struct bp_location *bl, **blp_tmp;
3a1bae8e 3271 int val = 0;
ce696e05 3272 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3273 struct inferior *inf = current_inferior ();
c5aa993b 3274
39f77062 3275 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 3276 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3277
6c95b8df 3278 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 3279 inferior_ptid = pid_to_ptid (pid);
35df4500 3280 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3281 {
35df4500 3282 if (bl->pspace != inf->pspace)
6c95b8df
PA
3283 continue;
3284
35df4500
TJB
3285 if (bl->inserted)
3286 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3287 }
d03285ec
UW
3288
3289 /* Detach single-step breakpoints as well. */
3290 detach_single_step_breakpoints ();
3291
ce696e05 3292 do_cleanups (old_chain);
3a1bae8e 3293 return val;
c906108c
SS
3294}
3295
35df4500 3296/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3297 Note that this is used to detach breakpoints from a child fork.
3298 When we get here, the child isn't in the inferior list, and neither
3299 do we have objects to represent its address space --- we should
35df4500 3300 *not* look at bl->pspace->aspace here. */
6c95b8df 3301
c906108c 3302static int
35df4500 3303remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3304{
3305 int val;
c5aa993b 3306
35df4500
TJB
3307 /* BL is never in moribund_locations by our callers. */
3308 gdb_assert (bl->owner != NULL);
2bdf28a0 3309
35df4500 3310 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3311 /* Permanent breakpoints cannot be inserted or removed. */
3312 return 0;
3313
74960c60
VP
3314 /* The type of none suggests that owner is actually deleted.
3315 This should not ever happen. */
35df4500 3316 gdb_assert (bl->owner->type != bp_none);
0bde7532 3317
35df4500
TJB
3318 if (bl->loc_type == bp_loc_software_breakpoint
3319 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3320 {
c02f5703
MS
3321 /* "Normal" instruction breakpoint: either the standard
3322 trap-instruction bp (bp_breakpoint), or a
3323 bp_hardware_breakpoint. */
3324
3325 /* First check to see if we have to handle an overlay. */
3326 if (overlay_debugging == ovly_off
35df4500
TJB
3327 || bl->section == NULL
3328 || !(section_is_overlay (bl->section)))
c02f5703
MS
3329 {
3330 /* No overlay handling: just remove the breakpoint. */
348d480f 3331 val = bl->owner->ops->remove_location (bl);
c02f5703 3332 }
c906108c
SS
3333 else
3334 {
4a64f543 3335 /* This breakpoint is in an overlay section.
c02f5703
MS
3336 Did we set a breakpoint at the LMA? */
3337 if (!overlay_events_enabled)
3338 {
3339 /* Yes -- overlay event support is not active, so we
3340 should have set a breakpoint at the LMA. Remove it.
3341 */
c02f5703
MS
3342 /* Ignore any failures: if the LMA is in ROM, we will
3343 have already warned when we failed to insert it. */
35df4500
TJB
3344 if (bl->loc_type == bp_loc_hardware_breakpoint)
3345 target_remove_hw_breakpoint (bl->gdbarch,
3346 &bl->overlay_target_info);
c02f5703 3347 else
35df4500
TJB
3348 target_remove_breakpoint (bl->gdbarch,
3349 &bl->overlay_target_info);
c02f5703
MS
3350 }
3351 /* Did we set a breakpoint at the VMA?
3352 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3353 if (bl->inserted)
c906108c 3354 {
c02f5703
MS
3355 /* Yes -- remove it. Previously we did not bother to
3356 remove the breakpoint if the section had been
3357 unmapped, but let's not rely on that being safe. We
3358 don't know what the overlay manager might do. */
aa67235e
UW
3359
3360 /* However, we should remove *software* breakpoints only
3361 if the section is still mapped, or else we overwrite
3362 wrong code with the saved shadow contents. */
348d480f
PA
3363 if (bl->loc_type == bp_loc_hardware_breakpoint
3364 || section_is_mapped (bl->section))
3365 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3366 else
3367 val = 0;
c906108c 3368 }
c02f5703
MS
3369 else
3370 {
3371 /* No -- not inserted, so no need to remove. No error. */
3372 val = 0;
3373 }
c906108c 3374 }
879d1e6b
UW
3375
3376 /* In some cases, we might not be able to remove a breakpoint
3377 in a shared library that has already been removed, but we
3378 have not yet processed the shlib unload event. */
35df4500 3379 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
3380 val = 0;
3381
c906108c
SS
3382 if (val)
3383 return val;
35df4500 3384 bl->inserted = (is == mark_inserted);
c906108c 3385 }
35df4500 3386 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3387 {
77b06cd7
TJB
3388 gdb_assert (bl->owner->ops != NULL
3389 && bl->owner->ops->remove_location != NULL);
3390
35df4500 3391 bl->inserted = (is == mark_inserted);
77b06cd7 3392 bl->owner->ops->remove_location (bl);
2e70b7b9 3393
c906108c 3394 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3395 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 3396 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3397 bl->owner->number);
c906108c 3398 }
35df4500
TJB
3399 else if (bl->owner->type == bp_catchpoint
3400 && breakpoint_enabled (bl->owner)
3401 && !bl->duplicate)
ce78b96d 3402 {
77b06cd7
TJB
3403 gdb_assert (bl->owner->ops != NULL
3404 && bl->owner->ops->remove_location != NULL);
ce78b96d 3405
77b06cd7 3406 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
3407 if (val)
3408 return val;
77b06cd7 3409
35df4500 3410 bl->inserted = (is == mark_inserted);
ce78b96d 3411 }
c906108c
SS
3412
3413 return 0;
3414}
3415
6c95b8df 3416static int
35df4500 3417remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
3418{
3419 int ret;
3420 struct cleanup *old_chain;
3421
35df4500
TJB
3422 /* BL is never in moribund_locations by our callers. */
3423 gdb_assert (bl->owner != NULL);
2bdf28a0 3424
35df4500 3425 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
3426 /* Permanent breakpoints cannot be inserted or removed. */
3427 return 0;
3428
3429 /* The type of none suggests that owner is actually deleted.
3430 This should not ever happen. */
35df4500 3431 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
3432
3433 old_chain = save_current_space_and_thread ();
3434
35df4500 3435 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3436
35df4500 3437 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
3438
3439 do_cleanups (old_chain);
3440 return ret;
3441}
3442
c906108c
SS
3443/* Clear the "inserted" flag in all breakpoints. */
3444
25b22b0a 3445void
fba45db2 3446mark_breakpoints_out (void)
c906108c 3447{
35df4500 3448 struct bp_location *bl, **blp_tmp;
c906108c 3449
35df4500
TJB
3450 ALL_BP_LOCATIONS (bl, blp_tmp)
3451 if (bl->pspace == current_program_space)
3452 bl->inserted = 0;
c906108c
SS
3453}
3454
53a5351d
JM
3455/* Clear the "inserted" flag in all breakpoints and delete any
3456 breakpoints which should go away between runs of the program.
c906108c
SS
3457
3458 Plus other such housekeeping that has to be done for breakpoints
3459 between runs.
3460
53a5351d
JM
3461 Note: this function gets called at the end of a run (by
3462 generic_mourn_inferior) and when a run begins (by
4a64f543 3463 init_wait_for_inferior). */
c906108c
SS
3464
3465
3466
3467void
fba45db2 3468breakpoint_init_inferior (enum inf_context context)
c906108c 3469{
35df4500
TJB
3470 struct breakpoint *b, *b_tmp;
3471 struct bp_location *bl, **blp_tmp;
1c5cfe86 3472 int ix;
6c95b8df 3473 struct program_space *pspace = current_program_space;
c906108c 3474
50c71eaf
PA
3475 /* If breakpoint locations are shared across processes, then there's
3476 nothing to do. */
2567c7d9 3477 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
3478 return;
3479
35df4500 3480 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3481 {
35df4500
TJB
3482 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3483 if (bl->pspace == pspace
3484 && bl->owner->enable_state != bp_permanent)
3485 bl->inserted = 0;
6c95b8df 3486 }
075f6582 3487
35df4500 3488 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3489 {
6c95b8df
PA
3490 if (b->loc && b->loc->pspace != pspace)
3491 continue;
3492
c5aa993b
JM
3493 switch (b->type)
3494 {
3495 case bp_call_dummy:
e2e4d78b 3496 case bp_longjmp_call_dummy:
c906108c 3497
c5aa993b 3498 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3499 cause problems when the inferior is rerun, so we better get
3500 rid of it. */
3501
3502 case bp_watchpoint_scope:
3503
3504 /* Also get rid of scope breakpoints. */
3505
3506 case bp_shlib_event:
3507
3508 /* Also remove solib event breakpoints. Their addresses may
3509 have changed since the last time we ran the program.
3510 Actually we may now be debugging against different target;
3511 and so the solib backend that installed this breakpoint may
3512 not be used in by the target. E.g.,
3513
3514 (gdb) file prog-linux
3515 (gdb) run # native linux target
3516 ...
3517 (gdb) kill
3518 (gdb) file prog-win.exe
3519 (gdb) tar rem :9999 # remote Windows gdbserver.
3520 */
c906108c 3521
f59f708a
PA
3522 case bp_step_resume:
3523
3524 /* Also remove step-resume breakpoints. */
3525
c5aa993b
JM
3526 delete_breakpoint (b);
3527 break;
c906108c 3528
c5aa993b
JM
3529 case bp_watchpoint:
3530 case bp_hardware_watchpoint:
3531 case bp_read_watchpoint:
3532 case bp_access_watchpoint:
3a5c3e22
PA
3533 {
3534 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3535
3a5c3e22
PA
3536 /* Likewise for watchpoints on local expressions. */
3537 if (w->exp_valid_block != NULL)
3538 delete_breakpoint (b);
3539 else if (context == inf_starting)
3540 {
3541 /* Reset val field to force reread of starting value in
3542 insert_breakpoints. */
3543 if (w->val)
3544 value_free (w->val);
3545 w->val = NULL;
3546 w->val_valid = 0;
c860120c 3547 }
3a5c3e22 3548 }
c5aa993b
JM
3549 break;
3550 default:
c5aa993b
JM
3551 break;
3552 }
3553 }
1c5cfe86
PA
3554
3555 /* Get rid of the moribund locations. */
35df4500
TJB
3556 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3557 decref_bp_location (&bl);
1c5cfe86 3558 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
3559}
3560
6c95b8df
PA
3561/* These functions concern about actual breakpoints inserted in the
3562 target --- to e.g. check if we need to do decr_pc adjustment or if
3563 we need to hop over the bkpt --- so we check for address space
3564 match, not program space. */
3565
c2c6d25f
JM
3566/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3567 exists at PC. It returns ordinary_breakpoint_here if it's an
3568 ordinary breakpoint, or permanent_breakpoint_here if it's a
3569 permanent breakpoint.
3570 - When continuing from a location with an ordinary breakpoint, we
3571 actually single step once before calling insert_breakpoints.
e5dd4106 3572 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3573 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3574 the target, to advance the PC past the breakpoint. */
c906108c 3575
c2c6d25f 3576enum breakpoint_here
6c95b8df 3577breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 3578{
35df4500 3579 struct bp_location *bl, **blp_tmp;
c2c6d25f 3580 int any_breakpoint_here = 0;
c906108c 3581
35df4500 3582 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3583 {
35df4500
TJB
3584 if (bl->loc_type != bp_loc_software_breakpoint
3585 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3586 continue;
3587
f1310107 3588 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
3589 if ((breakpoint_enabled (bl->owner)
3590 || bl->owner->enable_state == bp_permanent)
f1310107 3591 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3592 {
3593 if (overlay_debugging
35df4500
TJB
3594 && section_is_overlay (bl->section)
3595 && !section_is_mapped (bl->section))
075f6582 3596 continue; /* unmapped overlay -- can't be a match */
35df4500 3597 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
3598 return permanent_breakpoint_here;
3599 else
3600 any_breakpoint_here = 1;
3601 }
3602 }
c906108c 3603
c2c6d25f 3604 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
3605}
3606
1c5cfe86
PA
3607/* Return true if there's a moribund breakpoint at PC. */
3608
3609int
6c95b8df 3610moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
3611{
3612 struct bp_location *loc;
3613 int ix;
3614
3615 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 3616 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
3617 return 1;
3618
3619 return 0;
3620}
c2c6d25f 3621
c36b740a 3622/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
3623 inserted using regular breakpoint_chain / bp_location array
3624 mechanism. This does not check for single-step breakpoints, which
3625 are inserted and removed using direct target manipulation. */
c906108c
SS
3626
3627int
4a64f543
MS
3628regular_breakpoint_inserted_here_p (struct address_space *aspace,
3629 CORE_ADDR pc)
c906108c 3630{
35df4500 3631 struct bp_location *bl, **blp_tmp;
c906108c 3632
35df4500 3633 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3634 {
35df4500
TJB
3635 if (bl->loc_type != bp_loc_software_breakpoint
3636 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3637 continue;
3638
35df4500 3639 if (bl->inserted
f1310107 3640 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3641 {
3642 if (overlay_debugging
35df4500
TJB
3643 && section_is_overlay (bl->section)
3644 && !section_is_mapped (bl->section))
075f6582
DJ
3645 continue; /* unmapped overlay -- can't be a match */
3646 else
3647 return 1;
3648 }
c5aa993b 3649 }
c36b740a
VP
3650 return 0;
3651}
3652
3653/* Returns non-zero iff there's either regular breakpoint
3654 or a single step breakpoint inserted at PC. */
3655
3656int
6c95b8df 3657breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 3658{
6c95b8df 3659 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 3660 return 1;
c906108c 3661
6c95b8df 3662 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3663 return 1;
3664
c906108c
SS
3665 return 0;
3666}
3667
4fa8626c
DJ
3668/* This function returns non-zero iff there is a software breakpoint
3669 inserted at PC. */
3670
3671int
3e43a32a
MS
3672software_breakpoint_inserted_here_p (struct address_space *aspace,
3673 CORE_ADDR pc)
4fa8626c 3674{
35df4500 3675 struct bp_location *bl, **blp_tmp;
4fa8626c 3676
35df4500 3677 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 3678 {
35df4500 3679 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
3680 continue;
3681
35df4500
TJB
3682 if (bl->inserted
3683 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 3684 aspace, pc))
4fa8626c
DJ
3685 {
3686 if (overlay_debugging
35df4500
TJB
3687 && section_is_overlay (bl->section)
3688 && !section_is_mapped (bl->section))
4fa8626c
DJ
3689 continue; /* unmapped overlay -- can't be a match */
3690 else
3691 return 1;
3692 }
3693 }
3694
1aafd4da 3695 /* Also check for software single-step breakpoints. */
6c95b8df 3696 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3697 return 1;
3698
4fa8626c
DJ
3699 return 0;
3700}
3701
9093389c
PA
3702int
3703hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3704 CORE_ADDR addr, ULONGEST len)
3705{
3706 struct breakpoint *bpt;
3707
3708 ALL_BREAKPOINTS (bpt)
3709 {
3710 struct bp_location *loc;
3711
3712 if (bpt->type != bp_hardware_watchpoint
3713 && bpt->type != bp_access_watchpoint)
3714 continue;
3715
3716 if (!breakpoint_enabled (bpt))
3717 continue;
3718
3719 for (loc = bpt->loc; loc; loc = loc->next)
3720 if (loc->pspace->aspace == aspace && loc->inserted)
3721 {
3722 CORE_ADDR l, h;
3723
3724 /* Check for intersection. */
3725 l = max (loc->address, addr);
3726 h = min (loc->address + loc->length, addr + len);
3727 if (l < h)
3728 return 1;
3729 }
3730 }
3731 return 0;
3732}
3733
075f6582
DJ
3734/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3735 PC is valid for process/thread PTID. */
c906108c
SS
3736
3737int
6c95b8df
PA
3738breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3739 ptid_t ptid)
c906108c 3740{
35df4500 3741 struct bp_location *bl, **blp_tmp;
4a306c9a 3742 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3743 int thread = -1;
4a306c9a 3744 int task = 0;
a6f1cd96 3745
35df4500 3746 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3747 {
35df4500
TJB
3748 if (bl->loc_type != bp_loc_software_breakpoint
3749 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3750 continue;
3751
35df4500
TJB
3752 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3753 if (!breakpoint_enabled (bl->owner)
3754 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3755 continue;
3756
f1310107 3757 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3758 continue;
3759
35df4500 3760 if (bl->owner->thread != -1)
075f6582 3761 {
a6f1cd96
JB
3762 /* This is a thread-specific breakpoint. Check that ptid
3763 matches that thread. If thread hasn't been computed yet,
3764 it is now time to do so. */
3765 if (thread == -1)
3766 thread = pid_to_thread_id (ptid);
35df4500 3767 if (bl->owner->thread != thread)
a6f1cd96 3768 continue;
075f6582 3769 }
a6f1cd96 3770
35df4500 3771 if (bl->owner->task != 0)
4a306c9a
JB
3772 {
3773 /* This is a task-specific breakpoint. Check that ptid
3774 matches that task. If task hasn't been computed yet,
3775 it is now time to do so. */
3776 if (task == 0)
3777 task = ada_get_task_number (ptid);
35df4500 3778 if (bl->owner->task != task)
4a306c9a
JB
3779 continue;
3780 }
3781
a6f1cd96 3782 if (overlay_debugging
35df4500
TJB
3783 && section_is_overlay (bl->section)
3784 && !section_is_mapped (bl->section))
a6f1cd96
JB
3785 continue; /* unmapped overlay -- can't be a match */
3786
3787 return 1;
c5aa993b 3788 }
c906108c
SS
3789
3790 return 0;
3791}
c906108c 3792\f
c5aa993b 3793
c906108c
SS
3794/* bpstat stuff. External routines' interfaces are documented
3795 in breakpoint.h. */
3796
3797int
c326b90e 3798is_catchpoint (struct breakpoint *ep)
c906108c 3799{
533be4dd 3800 return (ep->type == bp_catchpoint);
c906108c
SS
3801}
3802
f431efe5
PA
3803/* Frees any storage that is part of a bpstat. Does not walk the
3804 'next' chain. */
3805
3806static void
198757a8
VP
3807bpstat_free (bpstat bs)
3808{
3809 if (bs->old_val != NULL)
3810 value_free (bs->old_val);
9add0f1b 3811 decref_counted_command_line (&bs->commands);
f431efe5 3812 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3813 xfree (bs);
3814}
3815
c906108c
SS
3816/* Clear a bpstat so that it says we are not at any breakpoint.
3817 Also free any storage that is part of a bpstat. */
3818
3819void
fba45db2 3820bpstat_clear (bpstat *bsp)
c906108c
SS
3821{
3822 bpstat p;
3823 bpstat q;
3824
3825 if (bsp == 0)
3826 return;
3827 p = *bsp;
3828 while (p != NULL)
3829 {
3830 q = p->next;
198757a8 3831 bpstat_free (p);
c906108c
SS
3832 p = q;
3833 }
3834 *bsp = NULL;
3835}
3836
3837/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3838 is part of the bpstat is copied as well. */
3839
3840bpstat
fba45db2 3841bpstat_copy (bpstat bs)
c906108c
SS
3842{
3843 bpstat p = NULL;
3844 bpstat tmp;
3845 bpstat retval = NULL;
3846
3847 if (bs == NULL)
3848 return bs;
3849
3850 for (; bs != NULL; bs = bs->next)
3851 {
3852 tmp = (bpstat) xmalloc (sizeof (*tmp));
3853 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3854 incref_counted_command_line (tmp->commands);
f431efe5 3855 incref_bp_location (tmp->bp_location_at);
31cc81e9 3856 if (bs->old_val != NULL)
3c3185ac
JK
3857 {
3858 tmp->old_val = value_copy (bs->old_val);
3859 release_value (tmp->old_val);
3860 }
31cc81e9 3861
c906108c
SS
3862 if (p == NULL)
3863 /* This is the first thing in the chain. */
3864 retval = tmp;
3865 else
3866 p->next = tmp;
3867 p = tmp;
3868 }
3869 p->next = NULL;
3870 return retval;
3871}
3872
4a64f543 3873/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3874
3875bpstat
fba45db2 3876bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3877{
c5aa993b
JM
3878 if (bsp == NULL)
3879 return NULL;
c906108c 3880
c5aa993b
JM
3881 for (; bsp != NULL; bsp = bsp->next)
3882 {
f431efe5 3883 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3884 return bsp;
3885 }
c906108c
SS
3886 return NULL;
3887}
3888
4a64f543
MS
3889/* Put in *NUM the breakpoint number of the first breakpoint we are
3890 stopped at. *BSP upon return is a bpstat which points to the
3891 remaining breakpoints stopped at (but which is not guaranteed to be
3892 good for anything but further calls to bpstat_num).
3893
8671a17b
PA
3894 Return 0 if passed a bpstat which does not indicate any breakpoints.
3895 Return -1 if stopped at a breakpoint that has been deleted since
3896 we set it.
3897 Return 1 otherwise. */
c906108c
SS
3898
3899int
8671a17b 3900bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3901{
3902 struct breakpoint *b;
3903
3904 if ((*bsp) == NULL)
3905 return 0; /* No more breakpoint values */
8671a17b 3906
4a64f543
MS
3907 /* We assume we'll never have several bpstats that correspond to a
3908 single breakpoint -- otherwise, this function might return the
3909 same number more than once and this will look ugly. */
f431efe5 3910 b = (*bsp)->breakpoint_at;
8671a17b
PA
3911 *bsp = (*bsp)->next;
3912 if (b == NULL)
3913 return -1; /* breakpoint that's been deleted since */
3914
3915 *num = b->number; /* We have its number */
3916 return 1;
c906108c
SS
3917}
3918
e93ca019 3919/* See breakpoint.h. */
c906108c
SS
3920
3921void
e93ca019 3922bpstat_clear_actions (void)
c906108c 3923{
e93ca019
JK
3924 struct thread_info *tp;
3925 bpstat bs;
3926
3927 if (ptid_equal (inferior_ptid, null_ptid))
3928 return;
3929
3930 tp = find_thread_ptid (inferior_ptid);
3931 if (tp == NULL)
3932 return;
3933
3934 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 3935 {
9add0f1b 3936 decref_counted_command_line (&bs->commands);
abf85f46 3937
c906108c
SS
3938 if (bs->old_val != NULL)
3939 {
3940 value_free (bs->old_val);
3941 bs->old_val = NULL;
3942 }
3943 }
3944}
3945
f3b1572e
PA
3946/* Called when a command is about to proceed the inferior. */
3947
3948static void
3949breakpoint_about_to_proceed (void)
3950{
3951 if (!ptid_equal (inferior_ptid, null_ptid))
3952 {
3953 struct thread_info *tp = inferior_thread ();
3954
3955 /* Allow inferior function calls in breakpoint commands to not
3956 interrupt the command list. When the call finishes
3957 successfully, the inferior will be standing at the same
3958 breakpoint as if nothing happened. */
16c381f0 3959 if (tp->control.in_infcall)
f3b1572e
PA
3960 return;
3961 }
3962
3963 breakpoint_proceeded = 1;
3964}
3965
4a64f543
MS
3966/* Stub for cleaning up our state if we error-out of a breakpoint
3967 command. */
c906108c 3968static void
4efb68b1 3969cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3970{
3971 executing_breakpoint_commands = 0;
3972}
3973
abf85f46
JK
3974/* Return non-zero iff CMD as the first line of a command sequence is `silent'
3975 or its equivalent. */
3976
3977static int
3978command_line_is_silent (struct command_line *cmd)
3979{
3980 return cmd && (strcmp ("silent", cmd->line) == 0
3981 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3982}
3983
4a64f543
MS
3984/* Execute all the commands associated with all the breakpoints at
3985 this location. Any of these commands could cause the process to
3986 proceed beyond this point, etc. We look out for such changes by
3987 checking the global "breakpoint_proceeded" after each command.
c906108c 3988
347bddb7
PA
3989 Returns true if a breakpoint command resumed the inferior. In that
3990 case, it is the caller's responsibility to recall it again with the
3991 bpstat of the current thread. */
3992
3993static int
3994bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3995{
3996 bpstat bs;
3997 struct cleanup *old_chain;
347bddb7 3998 int again = 0;
c906108c
SS
3999
4000 /* Avoid endless recursion if a `source' command is contained
4001 in bs->commands. */
4002 if (executing_breakpoint_commands)
347bddb7 4003 return 0;
c906108c
SS
4004
4005 executing_breakpoint_commands = 1;
4006 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4007
cf6c5ffb
TT
4008 prevent_dont_repeat ();
4009
4a64f543 4010 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4011 bs = *bsp;
4012
4013 breakpoint_proceeded = 0;
4014 for (; bs != NULL; bs = bs->next)
4015 {
9add0f1b 4016 struct counted_command_line *ccmd;
6c50ab1c
JB
4017 struct command_line *cmd;
4018 struct cleanup *this_cmd_tree_chain;
4019
4020 /* Take ownership of the BSP's command tree, if it has one.
4021
4022 The command tree could legitimately contain commands like
4023 'step' and 'next', which call clear_proceed_status, which
4024 frees stop_bpstat's command tree. To make sure this doesn't
4025 free the tree we're executing out from under us, we need to
4026 take ownership of the tree ourselves. Since a given bpstat's
4027 commands are only executed once, we don't need to copy it; we
4028 can clear the pointer in the bpstat, and make sure we free
4029 the tree when we're done. */
9add0f1b
TT
4030 ccmd = bs->commands;
4031 bs->commands = NULL;
abf85f46
JK
4032 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4033 cmd = ccmd ? ccmd->commands : NULL;
4034 if (command_line_is_silent (cmd))
4035 {
4036 /* The action has been already done by bpstat_stop_status. */
4037 cmd = cmd->next;
4038 }
6c50ab1c 4039
c906108c
SS
4040 while (cmd != NULL)
4041 {
4042 execute_control_command (cmd);
4043
4044 if (breakpoint_proceeded)
4045 break;
4046 else
4047 cmd = cmd->next;
4048 }
6c50ab1c
JB
4049
4050 /* We can free this command tree now. */
4051 do_cleanups (this_cmd_tree_chain);
4052
c906108c 4053 if (breakpoint_proceeded)
32c1e744
VP
4054 {
4055 if (target_can_async_p ())
347bddb7
PA
4056 /* If we are in async mode, then the target might be still
4057 running, not stopped at any breakpoint, so nothing for
4058 us to do here -- just return to the event loop. */
4059 ;
32c1e744
VP
4060 else
4061 /* In sync mode, when execute_control_command returns
4062 we're already standing on the next breakpoint.
347bddb7
PA
4063 Breakpoint commands for that stop were not run, since
4064 execute_command does not run breakpoint commands --
4065 only command_line_handler does, but that one is not
4066 involved in execution of breakpoint commands. So, we
4067 can now execute breakpoint commands. It should be
4068 noted that making execute_command do bpstat actions is
4069 not an option -- in this case we'll have recursive
4070 invocation of bpstat for each breakpoint with a
4071 command, and can easily blow up GDB stack. Instead, we
4072 return true, which will trigger the caller to recall us
4073 with the new stop_bpstat. */
4074 again = 1;
4075 break;
32c1e744 4076 }
c906108c 4077 }
c2b8ed2c 4078 do_cleanups (old_chain);
347bddb7
PA
4079 return again;
4080}
4081
4082void
4083bpstat_do_actions (void)
4084{
353d1d73
JK
4085 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4086
347bddb7
PA
4087 /* Do any commands attached to breakpoint we are stopped at. */
4088 while (!ptid_equal (inferior_ptid, null_ptid)
4089 && target_has_execution
4090 && !is_exited (inferior_ptid)
4091 && !is_executing (inferior_ptid))
4092 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4093 and only return when it is stopped at the next breakpoint, we
4094 keep doing breakpoint actions until it returns false to
4095 indicate the inferior was not resumed. */
16c381f0 4096 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4097 break;
353d1d73
JK
4098
4099 discard_cleanups (cleanup_if_error);
c906108c
SS
4100}
4101
fa4727a6
DJ
4102/* Print out the (old or new) value associated with a watchpoint. */
4103
4104static void
4105watchpoint_value_print (struct value *val, struct ui_file *stream)
4106{
4107 if (val == NULL)
4108 fprintf_unfiltered (stream, _("<unreadable>"));
4109 else
79a45b7d
TT
4110 {
4111 struct value_print_options opts;
4112 get_user_print_options (&opts);
4113 value_print (val, stream, &opts);
4114 }
fa4727a6
DJ
4115}
4116
e514a9d6 4117/* Generic routine for printing messages indicating why we
4a64f543 4118 stopped. The behavior of this function depends on the value
e514a9d6
JM
4119 'print_it' in the bpstat structure. Under some circumstances we
4120 may decide not to print anything here and delegate the task to
4a64f543 4121 normal_stop(). */
e514a9d6
JM
4122
4123static enum print_stop_action
4124print_bp_stop_message (bpstat bs)
4125{
4126 switch (bs->print_it)
4127 {
4128 case print_it_noop:
4a64f543 4129 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4130 return PRINT_UNKNOWN;
4131 break;
4132
4133 case print_it_done:
4134 /* We still want to print the frame, but we already printed the
4a64f543 4135 relevant messages. */
e514a9d6
JM
4136 return PRINT_SRC_AND_LOC;
4137 break;
4138
4139 case print_it_normal:
4f8d1dc6 4140 {
f431efe5
PA
4141 struct breakpoint *b = bs->breakpoint_at;
4142
1a6a67de
TJB
4143 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4144 which has since been deleted. */
4145 if (b == NULL)
4146 return PRINT_UNKNOWN;
4147
348d480f
PA
4148 /* Normal case. Call the breakpoint's print_it method. */
4149 return b->ops->print_it (bs);
4f8d1dc6 4150 }
348d480f 4151 break;
3086aeae 4152
e514a9d6 4153 default:
8e65ff28 4154 internal_error (__FILE__, __LINE__,
e2e0b3e5 4155 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4156 break;
c906108c 4157 }
c906108c
SS
4158}
4159
edcc5120
TT
4160/* A helper function that prints a shared library stopped event. */
4161
4162static void
4163print_solib_event (int is_catchpoint)
4164{
4165 int any_deleted
4166 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4167 int any_added
4168 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4169
4170 if (!is_catchpoint)
4171 {
4172 if (any_added || any_deleted)
4173 ui_out_text (current_uiout,
4174 _("Stopped due to shared library event:\n"));
4175 else
4176 ui_out_text (current_uiout,
4177 _("Stopped due to shared library event (no "
4178 "libraries added or removed)\n"));
4179 }
4180
4181 if (ui_out_is_mi_like_p (current_uiout))
4182 ui_out_field_string (current_uiout, "reason",
4183 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4184
4185 if (any_deleted)
4186 {
4187 struct cleanup *cleanup;
4188 char *name;
4189 int ix;
4190
4191 ui_out_text (current_uiout, _(" Inferior unloaded "));
4192 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4193 "removed");
4194 for (ix = 0;
4195 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4196 ix, name);
4197 ++ix)
4198 {
4199 if (ix > 0)
4200 ui_out_text (current_uiout, " ");
4201 ui_out_field_string (current_uiout, "library", name);
4202 ui_out_text (current_uiout, "\n");
4203 }
4204
4205 do_cleanups (cleanup);
4206 }
4207
4208 if (any_added)
4209 {
4210 struct so_list *iter;
4211 int ix;
4212 struct cleanup *cleanup;
4213
4214 ui_out_text (current_uiout, _(" Inferior loaded "));
4215 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4216 "added");
4217 for (ix = 0;
4218 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4219 ix, iter);
4220 ++ix)
4221 {
4222 if (ix > 0)
4223 ui_out_text (current_uiout, " ");
4224 ui_out_field_string (current_uiout, "library", iter->so_name);
4225 ui_out_text (current_uiout, "\n");
4226 }
4227
4228 do_cleanups (cleanup);
4229 }
4230}
4231
e514a9d6
JM
4232/* Print a message indicating what happened. This is called from
4233 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4234 list - a list of the eventpoints that caused this stop. KIND is
4235 the target_waitkind for the stopping event. This
e514a9d6
JM
4236 routine calls the generic print routine for printing a message
4237 about reasons for stopping. This will print (for example) the
4238 "Breakpoint n," part of the output. The return value of this
4239 routine is one of:
c906108c 4240
4a64f543 4241 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4242 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4243 code to print the location. An example is
c5aa993b
JM
4244 "Breakpoint 1, " which should be followed by
4245 the location.
917317f4 4246 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4247 to also print the location part of the message.
4248 An example is the catch/throw messages, which
4a64f543 4249 don't require a location appended to the end.
917317f4 4250 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4251 further info to be printed. */
c906108c 4252
917317f4 4253enum print_stop_action
36dfb11c 4254bpstat_print (bpstat bs, int kind)
c906108c
SS
4255{
4256 int val;
c5aa993b 4257
c906108c 4258 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4259 (Currently all watchpoints go on the bpstat whether hit or not.
4260 That probably could (should) be changed, provided care is taken
c906108c 4261 with respect to bpstat_explains_signal). */
e514a9d6
JM
4262 for (; bs; bs = bs->next)
4263 {
4264 val = print_bp_stop_message (bs);
4265 if (val == PRINT_SRC_ONLY
4266 || val == PRINT_SRC_AND_LOC
4267 || val == PRINT_NOTHING)
4268 return val;
4269 }
c906108c 4270
36dfb11c
TT
4271 /* If we had hit a shared library event breakpoint,
4272 print_bp_stop_message would print out this message. If we hit an
4273 OS-level shared library event, do the same thing. */
4274 if (kind == TARGET_WAITKIND_LOADED)
4275 {
edcc5120 4276 print_solib_event (0);
36dfb11c
TT
4277 return PRINT_NOTHING;
4278 }
4279
e514a9d6 4280 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4281 with and nothing was printed. */
917317f4 4282 return PRINT_UNKNOWN;
c906108c
SS
4283}
4284
4a64f543
MS
4285/* Evaluate the expression EXP and return 1 if value is zero. This is
4286 used inside a catch_errors to evaluate the breakpoint condition.
4287 The argument is a "struct expression *" that has been cast to a
4288 "char *" to make it pass through catch_errors. */
c906108c
SS
4289
4290static int
4efb68b1 4291breakpoint_cond_eval (void *exp)
c906108c 4292{
278cd55f 4293 struct value *mark = value_mark ();
c5aa993b 4294 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4295
c906108c
SS
4296 value_free_to_mark (mark);
4297 return i;
4298}
4299
5760d0ab 4300/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4301
4302static bpstat
5760d0ab 4303bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4304{
4305 bpstat bs;
4306
4307 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4308 bs->next = NULL;
4309 **bs_link_pointer = bs;
4310 *bs_link_pointer = &bs->next;
f431efe5
PA
4311 bs->breakpoint_at = bl->owner;
4312 bs->bp_location_at = bl;
4313 incref_bp_location (bl);
c906108c
SS
4314 /* If the condition is false, etc., don't do the commands. */
4315 bs->commands = NULL;
4316 bs->old_val = NULL;
4317 bs->print_it = print_it_normal;
4318 return bs;
4319}
4320\f
d983da9c
DJ
4321/* The target has stopped with waitstatus WS. Check if any hardware
4322 watchpoints have triggered, according to the target. */
4323
4324int
4325watchpoints_triggered (struct target_waitstatus *ws)
4326{
d92524f1 4327 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4328 CORE_ADDR addr;
4329 struct breakpoint *b;
4330
4331 if (!stopped_by_watchpoint)
4332 {
4333 /* We were not stopped by a watchpoint. Mark all watchpoints
4334 as not triggered. */
4335 ALL_BREAKPOINTS (b)
cc60f2e3 4336 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4337 {
4338 struct watchpoint *w = (struct watchpoint *) b;
4339
4340 w->watchpoint_triggered = watch_triggered_no;
4341 }
d983da9c
DJ
4342
4343 return 0;
4344 }
4345
4346 if (!target_stopped_data_address (&current_target, &addr))
4347 {
4348 /* We were stopped by a watchpoint, but we don't know where.
4349 Mark all watchpoints as unknown. */
4350 ALL_BREAKPOINTS (b)
cc60f2e3 4351 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4352 {
4353 struct watchpoint *w = (struct watchpoint *) b;
4354
4355 w->watchpoint_triggered = watch_triggered_unknown;
4356 }
d983da9c
DJ
4357
4358 return stopped_by_watchpoint;
4359 }
4360
4361 /* The target could report the data address. Mark watchpoints
4362 affected by this data address as triggered, and all others as not
4363 triggered. */
4364
4365 ALL_BREAKPOINTS (b)
cc60f2e3 4366 if (is_hardware_watchpoint (b))
d983da9c 4367 {
3a5c3e22 4368 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4369 struct bp_location *loc;
d983da9c 4370
3a5c3e22 4371 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4372 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4373 {
3a5c3e22 4374 if (is_masked_watchpoint (b))
9c06b0b4 4375 {
3a5c3e22
PA
4376 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4377 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4378
4379 if (newaddr == start)
4380 {
3a5c3e22 4381 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4382 break;
4383 }
4384 }
4385 /* Exact match not required. Within range is sufficient. */
4386 else if (target_watchpoint_addr_within_range (&current_target,
4387 addr, loc->address,
4388 loc->length))
4389 {
3a5c3e22 4390 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4391 break;
4392 }
4393 }
d983da9c
DJ
4394 }
4395
4396 return 1;
4397}
4398
c906108c
SS
4399/* Possible return values for watchpoint_check (this can't be an enum
4400 because of check_errors). */
4401/* The watchpoint has been deleted. */
4402#define WP_DELETED 1
4403/* The value has changed. */
4404#define WP_VALUE_CHANGED 2
4405/* The value has not changed. */
4406#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4407/* Ignore this watchpoint, no matter if the value changed or not. */
4408#define WP_IGNORE 4
c906108c
SS
4409
4410#define BP_TEMPFLAG 1
4411#define BP_HARDWAREFLAG 2
4412
4a64f543
MS
4413/* Evaluate watchpoint condition expression and check if its value
4414 changed.
553e4c11
JB
4415
4416 P should be a pointer to struct bpstat, but is defined as a void *
4417 in order for this function to be usable with catch_errors. */
c906108c
SS
4418
4419static int
4efb68b1 4420watchpoint_check (void *p)
c906108c
SS
4421{
4422 bpstat bs = (bpstat) p;
3a5c3e22 4423 struct watchpoint *b;
c906108c
SS
4424 struct frame_info *fr;
4425 int within_current_scope;
4426
f431efe5 4427 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4428 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4429 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4430
f6bc2008
PA
4431 /* If this is a local watchpoint, we only want to check if the
4432 watchpoint frame is in scope if the current thread is the thread
4433 that was used to create the watchpoint. */
4434 if (!watchpoint_in_thread_scope (b))
60e1c644 4435 return WP_IGNORE;
f6bc2008 4436
c906108c
SS
4437 if (b->exp_valid_block == NULL)
4438 within_current_scope = 1;
4439 else
4440 {
edb3359d
DJ
4441 struct frame_info *frame = get_current_frame ();
4442 struct gdbarch *frame_arch = get_frame_arch (frame);
4443 CORE_ADDR frame_pc = get_frame_pc (frame);
4444
4a64f543
MS
4445 /* in_function_epilogue_p() returns a non-zero value if we're
4446 still in the function but the stack frame has already been
4447 invalidated. Since we can't rely on the values of local
4448 variables after the stack has been destroyed, we are treating
4449 the watchpoint in that state as `not changed' without further
4450 checking. Don't mark watchpoints as changed if the current
4451 frame is in an epilogue - even if they are in some other
4452 frame, our view of the stack is likely to be wrong and
4453 frame_find_by_id could error out. */
a0f49112 4454 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 4455 return WP_IGNORE;
a0f49112 4456
101dcfbe 4457 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4458 within_current_scope = (fr != NULL);
69fbadd5
DJ
4459
4460 /* If we've gotten confused in the unwinder, we might have
4461 returned a frame that can't describe this variable. */
edb3359d
DJ
4462 if (within_current_scope)
4463 {
4464 struct symbol *function;
4465
4466 function = get_frame_function (fr);
4467 if (function == NULL
4468 || !contained_in (b->exp_valid_block,
4469 SYMBOL_BLOCK_VALUE (function)))
4470 within_current_scope = 0;
4471 }
69fbadd5 4472
edb3359d 4473 if (within_current_scope)
c906108c
SS
4474 /* If we end up stopping, the current frame will get selected
4475 in normal_stop. So this call to select_frame won't affect
4476 the user. */
0f7d239c 4477 select_frame (fr);
c906108c 4478 }
c5aa993b 4479
c906108c
SS
4480 if (within_current_scope)
4481 {
4a64f543
MS
4482 /* We use value_{,free_to_}mark because it could be a *long*
4483 time before we return to the command level and call
4484 free_all_values. We can't call free_all_values because we
4485 might be in the middle of evaluating a function call. */
c906108c 4486
0cf6dd15 4487 int pc = 0;
9c06b0b4 4488 struct value *mark;
fa4727a6
DJ
4489 struct value *new_val;
4490
3a5c3e22 4491 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
4492 /* Since we don't know the exact trigger address (from
4493 stopped_data_address), just tell the user we've triggered
4494 a mask watchpoint. */
4495 return WP_VALUE_CHANGED;
4496
4497 mark = value_mark ();
0cf6dd15 4498 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 4499
4a64f543
MS
4500 /* We use value_equal_contents instead of value_equal because
4501 the latter coerces an array to a pointer, thus comparing just
4502 the address of the array instead of its contents. This is
4503 not what we want. */
fa4727a6 4504 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 4505 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 4506 {
fa4727a6
DJ
4507 if (new_val != NULL)
4508 {
4509 release_value (new_val);
4510 value_free_to_mark (mark);
4511 }
c906108c
SS
4512 bs->old_val = b->val;
4513 b->val = new_val;
fa4727a6 4514 b->val_valid = 1;
c906108c
SS
4515 return WP_VALUE_CHANGED;
4516 }
4517 else
4518 {
60e1c644 4519 /* Nothing changed. */
c906108c 4520 value_free_to_mark (mark);
c906108c
SS
4521 return WP_VALUE_NOT_CHANGED;
4522 }
4523 }
4524 else
4525 {
79a45e25
PA
4526 struct ui_out *uiout = current_uiout;
4527
c906108c 4528 /* This seems like the only logical thing to do because
c5aa993b
JM
4529 if we temporarily ignored the watchpoint, then when
4530 we reenter the block in which it is valid it contains
4531 garbage (in the case of a function, it may have two
4532 garbage values, one before and one after the prologue).
4533 So we can't even detect the first assignment to it and
4534 watch after that (since the garbage may or may not equal
4535 the first value assigned). */
348d480f
PA
4536 /* We print all the stop information in
4537 breakpoint_ops->print_it, but in this case, by the time we
4538 call breakpoint_ops->print_it this bp will be deleted
4539 already. So we have no choice but print the information
4540 here. */
9dc5e2a9 4541 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4542 ui_out_field_string
4543 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 4544 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 4545 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
4546 ui_out_text (uiout,
4547 " deleted because the program has left the block in\n\
8b93c638 4548which its expression is valid.\n");
4ce44c66 4549
cdac0397 4550 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 4551 decref_counted_command_line (&b->base.commands);
d0fb5eae 4552 watchpoint_del_at_next_stop (b);
c906108c
SS
4553
4554 return WP_DELETED;
4555 }
4556}
4557
18a18393 4558/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4559 breakpoint location BL. This function does not check if we should
4560 stop, only if BL explains the stop. */
4561
18a18393 4562static int
6c95b8df 4563bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
4564 struct address_space *aspace, CORE_ADDR bp_addr,
4565 const struct target_waitstatus *ws)
18a18393
VP
4566{
4567 struct breakpoint *b = bl->owner;
4568
348d480f 4569 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4570 gdb_assert (b != NULL);
4571
09ac7c10 4572 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4573}
4574
3a5c3e22
PA
4575/* Determine if the watched values have actually changed, and we
4576 should stop. If not, set BS->stop to 0. */
4577
18a18393
VP
4578static void
4579bpstat_check_watchpoint (bpstat bs)
4580{
2bdf28a0 4581 const struct bp_location *bl;
3a5c3e22 4582 struct watchpoint *b;
2bdf28a0
JK
4583
4584 /* BS is built for existing struct breakpoint. */
f431efe5 4585 bl = bs->bp_location_at;
2bdf28a0 4586 gdb_assert (bl != NULL);
3a5c3e22 4587 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 4588 gdb_assert (b != NULL);
18a18393 4589
18a18393 4590 {
18a18393
VP
4591 int must_check_value = 0;
4592
3a5c3e22 4593 if (b->base.type == bp_watchpoint)
18a18393
VP
4594 /* For a software watchpoint, we must always check the
4595 watched value. */
4596 must_check_value = 1;
4597 else if (b->watchpoint_triggered == watch_triggered_yes)
4598 /* We have a hardware watchpoint (read, write, or access)
4599 and the target earlier reported an address watched by
4600 this watchpoint. */
4601 must_check_value = 1;
4602 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 4603 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
4604 /* We were stopped by a hardware watchpoint, but the target could
4605 not report the data address. We must check the watchpoint's
4606 value. Access and read watchpoints are out of luck; without
4607 a data address, we can't figure it out. */
4608 must_check_value = 1;
3a5c3e22 4609
18a18393
VP
4610 if (must_check_value)
4611 {
3e43a32a
MS
4612 char *message
4613 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 4614 b->base.number);
18a18393
VP
4615 struct cleanup *cleanups = make_cleanup (xfree, message);
4616 int e = catch_errors (watchpoint_check, bs, message,
4617 RETURN_MASK_ALL);
4618 do_cleanups (cleanups);
4619 switch (e)
4620 {
4621 case WP_DELETED:
4622 /* We've already printed what needs to be printed. */
4623 bs->print_it = print_it_done;
4624 /* Stop. */
4625 break;
60e1c644
PA
4626 case WP_IGNORE:
4627 bs->print_it = print_it_noop;
4628 bs->stop = 0;
4629 break;
18a18393 4630 case WP_VALUE_CHANGED:
3a5c3e22 4631 if (b->base.type == bp_read_watchpoint)
18a18393 4632 {
85d721b8
PA
4633 /* There are two cases to consider here:
4634
4a64f543 4635 1. We're watching the triggered memory for reads.
85d721b8
PA
4636 In that case, trust the target, and always report
4637 the watchpoint hit to the user. Even though
4638 reads don't cause value changes, the value may
4639 have changed since the last time it was read, and
4640 since we're not trapping writes, we will not see
4641 those, and as such we should ignore our notion of
4642 old value.
4643
4a64f543 4644 2. We're watching the triggered memory for both
85d721b8
PA
4645 reads and writes. There are two ways this may
4646 happen:
4647
4a64f543 4648 2.1. This is a target that can't break on data
85d721b8
PA
4649 reads only, but can break on accesses (reads or
4650 writes), such as e.g., x86. We detect this case
4651 at the time we try to insert read watchpoints.
4652
4a64f543 4653 2.2. Otherwise, the target supports read
85d721b8
PA
4654 watchpoints, but, the user set an access or write
4655 watchpoint watching the same memory as this read
4656 watchpoint.
4657
4658 If we're watching memory writes as well as reads,
4659 ignore watchpoint hits when we find that the
4660 value hasn't changed, as reads don't cause
4661 changes. This still gives false positives when
4662 the program writes the same value to memory as
4663 what there was already in memory (we will confuse
4664 it for a read), but it's much better than
4665 nothing. */
4666
4667 int other_write_watchpoint = 0;
4668
4669 if (bl->watchpoint_type == hw_read)
4670 {
4671 struct breakpoint *other_b;
4672
4673 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
4674 if (other_b->type == bp_hardware_watchpoint
4675 || other_b->type == bp_access_watchpoint)
85d721b8 4676 {
3a5c3e22
PA
4677 struct watchpoint *other_w =
4678 (struct watchpoint *) other_b;
4679
4680 if (other_w->watchpoint_triggered
4681 == watch_triggered_yes)
4682 {
4683 other_write_watchpoint = 1;
4684 break;
4685 }
85d721b8
PA
4686 }
4687 }
4688
4689 if (other_write_watchpoint
4690 || bl->watchpoint_type == hw_access)
4691 {
4692 /* We're watching the same memory for writes,
4693 and the value changed since the last time we
4694 updated it, so this trap must be for a write.
4695 Ignore it. */
4696 bs->print_it = print_it_noop;
4697 bs->stop = 0;
4698 }
18a18393
VP
4699 }
4700 break;
4701 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
4702 if (b->base.type == bp_hardware_watchpoint
4703 || b->base.type == bp_watchpoint)
18a18393
VP
4704 {
4705 /* Don't stop: write watchpoints shouldn't fire if
4706 the value hasn't changed. */
4707 bs->print_it = print_it_noop;
4708 bs->stop = 0;
4709 }
4710 /* Stop. */
4711 break;
4712 default:
4713 /* Can't happen. */
4714 case 0:
4715 /* Error from catch_errors. */
3a5c3e22 4716 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 4717 watchpoint_del_at_next_stop (b);
18a18393
VP
4718 /* We've already printed what needs to be printed. */
4719 bs->print_it = print_it_done;
4720 break;
4721 }
4722 }
4723 else /* must_check_value == 0 */
4724 {
4725 /* This is a case where some watchpoint(s) triggered, but
4726 not at the address of this watchpoint, or else no
4727 watchpoint triggered after all. So don't print
4728 anything for this watchpoint. */
4729 bs->print_it = print_it_noop;
4730 bs->stop = 0;
4731 }
4732 }
4733}
4734
4735
4736/* Check conditions (condition proper, frame, thread and ignore count)
4737 of breakpoint referred to by BS. If we should not stop for this
4738 breakpoint, set BS->stop to 0. */
f431efe5 4739
18a18393
VP
4740static void
4741bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4742{
4743 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
4744 const struct bp_location *bl;
4745 struct breakpoint *b;
4746
4747 /* BS is built for existing struct breakpoint. */
f431efe5 4748 bl = bs->bp_location_at;
2bdf28a0 4749 gdb_assert (bl != NULL);
f431efe5 4750 b = bs->breakpoint_at;
2bdf28a0 4751 gdb_assert (b != NULL);
18a18393 4752
b775012e
LM
4753 /* Even if the target evaluated the condition on its end and notified GDB, we
4754 need to do so again since GDB does not know if we stopped due to a
4755 breakpoint or a single step breakpoint. */
4756
18a18393 4757 if (frame_id_p (b->frame_id)
edb3359d 4758 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4759 bs->stop = 0;
4760 else if (bs->stop)
4761 {
4762 int value_is_zero = 0;
60e1c644
PA
4763 struct expression *cond;
4764
7371cf6d
PM
4765 /* Evaluate Python breakpoints that have a "stop"
4766 method implemented. */
4767 if (b->py_bp_object)
4768 bs->stop = gdbpy_should_stop (b->py_bp_object);
4769
60e1c644 4770 if (is_watchpoint (b))
3a5c3e22
PA
4771 {
4772 struct watchpoint *w = (struct watchpoint *) b;
4773
4774 cond = w->cond_exp;
4775 }
60e1c644
PA
4776 else
4777 cond = bl->cond;
4778
f431efe5 4779 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4780 {
60e1c644 4781 int within_current_scope = 1;
3a5c3e22 4782 struct watchpoint * w;
60e1c644 4783
c5bc3a77
DJ
4784 /* We use value_mark and value_free_to_mark because it could
4785 be a long time before we return to the command level and
4786 call free_all_values. We can't call free_all_values
4787 because we might be in the middle of evaluating a
4788 function call. */
4789 struct value *mark = value_mark ();
4790
3a5c3e22
PA
4791 if (is_watchpoint (b))
4792 w = (struct watchpoint *) b;
4793 else
4794 w = NULL;
4795
edb3359d
DJ
4796 /* Need to select the frame, with all that implies so that
4797 the conditions will have the right context. Because we
4798 use the frame, we will not see an inlined function's
4799 variables when we arrive at a breakpoint at the start
4800 of the inlined function; the current frame will be the
4801 call site. */
3a5c3e22 4802 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
4803 select_frame (get_current_frame ());
4804 else
4805 {
4806 struct frame_info *frame;
4807
4808 /* For local watchpoint expressions, which particular
4809 instance of a local is being watched matters, so we
4810 keep track of the frame to evaluate the expression
4811 in. To evaluate the condition however, it doesn't
4812 really matter which instantiation of the function
4813 where the condition makes sense triggers the
4814 watchpoint. This allows an expression like "watch
4815 global if q > 10" set in `func', catch writes to
4816 global on all threads that call `func', or catch
4817 writes on all recursive calls of `func' by a single
4818 thread. We simply always evaluate the condition in
4819 the innermost frame that's executing where it makes
4820 sense to evaluate the condition. It seems
4821 intuitive. */
3a5c3e22 4822 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
4823 if (frame != NULL)
4824 select_frame (frame);
4825 else
4826 within_current_scope = 0;
4827 }
4828 if (within_current_scope)
4829 value_is_zero
4830 = catch_errors (breakpoint_cond_eval, cond,
4831 "Error in testing breakpoint condition:\n",
4832 RETURN_MASK_ALL);
4833 else
4834 {
4835 warning (_("Watchpoint condition cannot be tested "
4836 "in the current scope"));
4837 /* If we failed to set the right context for this
4838 watchpoint, unconditionally report it. */
4839 value_is_zero = 0;
4840 }
4a64f543 4841 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4842 value_free_to_mark (mark);
18a18393 4843 }
60e1c644
PA
4844
4845 if (cond && value_is_zero)
18a18393
VP
4846 {
4847 bs->stop = 0;
4848 }
4849 else if (b->thread != -1 && b->thread != thread_id)
4850 {
4851 bs->stop = 0;
4852 }
4853 else if (b->ignore_count > 0)
4854 {
4855 b->ignore_count--;
4856 annotate_ignore_count_change ();
4857 bs->stop = 0;
4a64f543 4858 /* Increase the hit count even though we don't stop. */
18a18393 4859 ++(b->hit_count);
8d3788bd 4860 observer_notify_breakpoint_modified (b);
18a18393
VP
4861 }
4862 }
4863}
4864
4865
9709f61c 4866/* Get a bpstat associated with having just stopped at address
d983da9c 4867 BP_ADDR in thread PTID.
c906108c 4868
d983da9c 4869 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4870 don't understand this stop. Result is a chain of bpstat's such
4871 that:
c906108c 4872
c5aa993b 4873 if we don't understand the stop, the result is a null pointer.
c906108c 4874
c5aa993b 4875 if we understand why we stopped, the result is not null.
c906108c 4876
c5aa993b
JM
4877 Each element of the chain refers to a particular breakpoint or
4878 watchpoint at which we have stopped. (We may have stopped for
4879 several reasons concurrently.)
c906108c 4880
c5aa993b
JM
4881 Each element of the chain has valid next, breakpoint_at,
4882 commands, FIXME??? fields. */
c906108c
SS
4883
4884bpstat
6c95b8df 4885bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
4886 CORE_ADDR bp_addr, ptid_t ptid,
4887 const struct target_waitstatus *ws)
c906108c 4888{
0d381245 4889 struct breakpoint *b = NULL;
afe38095 4890 struct bp_location *bl;
20874c92 4891 struct bp_location *loc;
5760d0ab
JK
4892 /* First item of allocated bpstat's. */
4893 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4894 /* Pointer to the last thing in the chain currently. */
5760d0ab 4895 bpstat bs;
20874c92 4896 int ix;
429374b8 4897 int need_remove_insert;
f431efe5 4898 int removed_any;
c906108c 4899
f431efe5
PA
4900 /* First, build the bpstat chain with locations that explain a
4901 target stop, while being careful to not set the target running,
4902 as that may invalidate locations (in particular watchpoint
4903 locations are recreated). Resuming will happen here with
4904 breakpoint conditions or watchpoint expressions that include
4905 inferior function calls. */
c5aa993b 4906
429374b8
JK
4907 ALL_BREAKPOINTS (b)
4908 {
4909 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4910 continue;
a5606eee 4911
429374b8
JK
4912 for (bl = b->loc; bl != NULL; bl = bl->next)
4913 {
4a64f543
MS
4914 /* For hardware watchpoints, we look only at the first
4915 location. The watchpoint_check function will work on the
4916 entire expression, not the individual locations. For
4917 read watchpoints, the watchpoints_triggered function has
4918 checked all locations already. */
429374b8
JK
4919 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4920 break;
18a18393 4921
429374b8
JK
4922 if (bl->shlib_disabled)
4923 continue;
c5aa993b 4924
09ac7c10 4925 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 4926 continue;
c5aa993b 4927
4a64f543
MS
4928 /* Come here if it's a watchpoint, or if the break address
4929 matches. */
c5aa993b 4930
4a64f543
MS
4931 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4932 explain stop. */
c5aa993b 4933
f431efe5
PA
4934 /* Assume we stop. Should we find a watchpoint that is not
4935 actually triggered, or if the condition of the breakpoint
4936 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4937 bs->stop = 1;
4938 bs->print = 1;
d983da9c 4939
f431efe5
PA
4940 /* If this is a scope breakpoint, mark the associated
4941 watchpoint as triggered so that we will handle the
4942 out-of-scope event. We'll get to the watchpoint next
4943 iteration. */
d0fb5eae 4944 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4945 {
4946 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4947
4948 w->watchpoint_triggered = watch_triggered_yes;
4949 }
f431efe5
PA
4950 }
4951 }
4952
4953 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4954 {
f1310107 4955 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4956 {
5760d0ab 4957 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4958 /* For hits of moribund locations, we should just proceed. */
4959 bs->stop = 0;
4960 bs->print = 0;
4961 bs->print_it = print_it_noop;
4962 }
4963 }
4964
edcc5120
TT
4965 /* A bit of special processing for shlib breakpoints. We need to
4966 process solib loading here, so that the lists of loaded and
4967 unloaded libraries are correct before we handle "catch load" and
4968 "catch unload". */
4969 for (bs = bs_head; bs != NULL; bs = bs->next)
4970 {
5d268276 4971 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
4972 {
4973 handle_solib_event ();
4974 break;
4975 }
4976 }
4977
f431efe5
PA
4978 /* Now go through the locations that caused the target to stop, and
4979 check whether we're interested in reporting this stop to higher
4980 layers, or whether we should resume the target transparently. */
4981
4982 removed_any = 0;
4983
5760d0ab 4984 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4985 {
4986 if (!bs->stop)
4987 continue;
4988
f431efe5 4989 b = bs->breakpoint_at;
348d480f
PA
4990 b->ops->check_status (bs);
4991 if (bs->stop)
28010a5d 4992 {
348d480f 4993 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4994
429374b8
JK
4995 if (bs->stop)
4996 {
4997 ++(b->hit_count);
8d3788bd 4998 observer_notify_breakpoint_modified (b);
c906108c 4999
4a64f543 5000 /* We will stop here. */
429374b8
JK
5001 if (b->disposition == disp_disable)
5002 {
816338b5
SS
5003 --(b->enable_count);
5004 if (b->enable_count <= 0
5005 && b->enable_state != bp_permanent)
429374b8 5006 b->enable_state = bp_disabled;
f431efe5 5007 removed_any = 1;
429374b8
JK
5008 }
5009 if (b->silent)
5010 bs->print = 0;
5011 bs->commands = b->commands;
9add0f1b 5012 incref_counted_command_line (bs->commands);
abf85f46
JK
5013 if (command_line_is_silent (bs->commands
5014 ? bs->commands->commands : NULL))
5015 bs->print = 0;
429374b8
JK
5016 }
5017
348d480f 5018 }
a9b3a50f
PA
5019
5020 /* Print nothing for this entry if we don't stop or don't
5021 print. */
5022 if (!bs->stop || !bs->print)
5023 bs->print_it = print_it_noop;
429374b8 5024 }
876fa593 5025
d983da9c
DJ
5026 /* If we aren't stopping, the value of some hardware watchpoint may
5027 not have changed, but the intermediate memory locations we are
5028 watching may have. Don't bother if we're stopping; this will get
5029 done later. */
d832cb68 5030 need_remove_insert = 0;
5760d0ab
JK
5031 if (! bpstat_causes_stop (bs_head))
5032 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5033 if (!bs->stop
f431efe5
PA
5034 && bs->breakpoint_at
5035 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5036 {
3a5c3e22
PA
5037 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5038
5039 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5040 need_remove_insert = 1;
d983da9c
DJ
5041 }
5042
d832cb68 5043 if (need_remove_insert)
2d134ed3 5044 update_global_location_list (1);
f431efe5
PA
5045 else if (removed_any)
5046 update_global_location_list (0);
d832cb68 5047
5760d0ab 5048 return bs_head;
c906108c 5049}
628fe4e4
JK
5050
5051static void
5052handle_jit_event (void)
5053{
5054 struct frame_info *frame;
5055 struct gdbarch *gdbarch;
5056
5057 /* Switch terminal for any messages produced by
5058 breakpoint_re_set. */
5059 target_terminal_ours_for_output ();
5060
5061 frame = get_current_frame ();
5062 gdbarch = get_frame_arch (frame);
5063
5064 jit_event_handler (gdbarch);
5065
5066 target_terminal_inferior ();
5067}
5068
edcc5120
TT
5069/* Handle an solib event by calling solib_add. */
5070
5071void
5072handle_solib_event (void)
5073{
5074 clear_program_space_solib_cache (current_inferior ()->pspace);
5075
5076 /* Check for any newly added shared libraries if we're supposed to
5077 be adding them automatically. Switch terminal for any messages
5078 produced by breakpoint_re_set. */
5079 target_terminal_ours_for_output ();
5080#ifdef SOLIB_ADD
5081 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
5082#else
5083 solib_add (NULL, 0, &current_target, auto_solib_add);
5084#endif
5085 target_terminal_inferior ();
5086}
5087
628fe4e4
JK
5088/* Prepare WHAT final decision for infrun. */
5089
5090/* Decide what infrun needs to do with this bpstat. */
5091
c906108c 5092struct bpstat_what
0e30163f 5093bpstat_what (bpstat bs_head)
c906108c 5094{
c906108c 5095 struct bpstat_what retval;
628fe4e4 5096 int jit_event = 0;
0e30163f 5097 bpstat bs;
c906108c 5098
628fe4e4 5099 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5100 retval.call_dummy = STOP_NONE;
186c406b 5101 retval.is_longjmp = 0;
628fe4e4 5102
0e30163f 5103 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5104 {
628fe4e4
JK
5105 /* Extract this BS's action. After processing each BS, we check
5106 if its action overrides all we've seem so far. */
5107 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5108 enum bptype bptype;
5109
c906108c 5110 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5111 {
5112 /* I suspect this can happen if it was a momentary
5113 breakpoint which has since been deleted. */
5114 bptype = bp_none;
5115 }
20874c92 5116 else
f431efe5 5117 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5118
5119 switch (bptype)
c906108c
SS
5120 {
5121 case bp_none:
628fe4e4 5122 break;
c906108c
SS
5123 case bp_breakpoint:
5124 case bp_hardware_breakpoint:
5125 case bp_until:
5126 case bp_finish:
a9b3a50f 5127 case bp_shlib_event:
c906108c
SS
5128 if (bs->stop)
5129 {
5130 if (bs->print)
628fe4e4 5131 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5132 else
628fe4e4 5133 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5134 }
5135 else
628fe4e4 5136 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5137 break;
5138 case bp_watchpoint:
5139 case bp_hardware_watchpoint:
5140 case bp_read_watchpoint:
5141 case bp_access_watchpoint:
5142 if (bs->stop)
5143 {
5144 if (bs->print)
628fe4e4 5145 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5146 else
628fe4e4 5147 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5148 }
5149 else
628fe4e4
JK
5150 {
5151 /* There was a watchpoint, but we're not stopping.
5152 This requires no further action. */
5153 }
c906108c
SS
5154 break;
5155 case bp_longjmp:
e2e4d78b 5156 case bp_longjmp_call_dummy:
186c406b 5157 case bp_exception:
628fe4e4 5158 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5159 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5160 break;
5161 case bp_longjmp_resume:
186c406b 5162 case bp_exception_resume:
628fe4e4 5163 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5164 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5165 break;
5166 case bp_step_resume:
5167 if (bs->stop)
628fe4e4
JK
5168 this_action = BPSTAT_WHAT_STEP_RESUME;
5169 else
c906108c 5170 {
628fe4e4
JK
5171 /* It is for the wrong frame. */
5172 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5173 }
c906108c 5174 break;
2c03e5be
PA
5175 case bp_hp_step_resume:
5176 if (bs->stop)
5177 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5178 else
5179 {
5180 /* It is for the wrong frame. */
5181 this_action = BPSTAT_WHAT_SINGLE;
5182 }
5183 break;
c906108c 5184 case bp_watchpoint_scope:
c4093a6a 5185 case bp_thread_event:
1900040c 5186 case bp_overlay_event:
0fd8e87f 5187 case bp_longjmp_master:
aa7d318d 5188 case bp_std_terminate_master:
186c406b 5189 case bp_exception_master:
628fe4e4 5190 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5191 break;
ce78b96d 5192 case bp_catchpoint:
c5aa993b
JM
5193 if (bs->stop)
5194 {
5195 if (bs->print)
628fe4e4 5196 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5197 else
628fe4e4 5198 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5199 }
5200 else
628fe4e4
JK
5201 {
5202 /* There was a catchpoint, but we're not stopping.
5203 This requires no further action. */
5204 }
5205 break;
628fe4e4
JK
5206 case bp_jit_event:
5207 jit_event = 1;
5208 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5209 break;
c906108c 5210 case bp_call_dummy:
53a5351d
JM
5211 /* Make sure the action is stop (silent or noisy),
5212 so infrun.c pops the dummy frame. */
aa7d318d 5213 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5214 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5215 break;
5216 case bp_std_terminate:
5217 /* Make sure the action is stop (silent or noisy),
5218 so infrun.c pops the dummy frame. */
aa7d318d 5219 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5220 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5221 break;
1042e4c0 5222 case bp_tracepoint:
7a697b8d 5223 case bp_fast_tracepoint:
0fb4aa4b 5224 case bp_static_tracepoint:
1042e4c0
SS
5225 /* Tracepoint hits should not be reported back to GDB, and
5226 if one got through somehow, it should have been filtered
5227 out already. */
5228 internal_error (__FILE__, __LINE__,
7a697b8d 5229 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5230 break;
5231 case bp_gnu_ifunc_resolver:
5232 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5233 this_action = BPSTAT_WHAT_SINGLE;
5234 break;
5235 case bp_gnu_ifunc_resolver_return:
5236 /* The breakpoint will be removed, execution will restart from the
5237 PC of the former breakpoint. */
5238 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5239 break;
e7e0cddf
SS
5240
5241 case bp_dprintf:
5242 this_action = BPSTAT_WHAT_STOP_SILENT;
5243 break;
5244
628fe4e4
JK
5245 default:
5246 internal_error (__FILE__, __LINE__,
5247 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5248 }
628fe4e4
JK
5249
5250 retval.main_action = max (retval.main_action, this_action);
c906108c 5251 }
628fe4e4 5252
0e30163f
JK
5253 /* These operations may affect the bs->breakpoint_at state so they are
5254 delayed after MAIN_ACTION is decided above. */
5255
628fe4e4
JK
5256 if (jit_event)
5257 {
5258 if (debug_infrun)
5259 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5260
5261 handle_jit_event ();
5262 }
5263
0e30163f
JK
5264 for (bs = bs_head; bs != NULL; bs = bs->next)
5265 {
5266 struct breakpoint *b = bs->breakpoint_at;
5267
5268 if (b == NULL)
5269 continue;
5270 switch (b->type)
5271 {
5272 case bp_gnu_ifunc_resolver:
5273 gnu_ifunc_resolver_stop (b);
5274 break;
5275 case bp_gnu_ifunc_resolver_return:
5276 gnu_ifunc_resolver_return_stop (b);
5277 break;
5278 }
5279 }
5280
c906108c
SS
5281 return retval;
5282}
5283
5284/* Nonzero if we should step constantly (e.g. watchpoints on machines
5285 without hardware support). This isn't related to a specific bpstat,
5286 just to things like whether watchpoints are set. */
5287
c5aa993b 5288int
fba45db2 5289bpstat_should_step (void)
c906108c
SS
5290{
5291 struct breakpoint *b;
cc59ec59 5292
c906108c 5293 ALL_BREAKPOINTS (b)
717a8278 5294 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5295 return 1;
c906108c
SS
5296 return 0;
5297}
5298
67822962
PA
5299int
5300bpstat_causes_stop (bpstat bs)
5301{
5302 for (; bs != NULL; bs = bs->next)
5303 if (bs->stop)
5304 return 1;
5305
5306 return 0;
5307}
5308
c906108c 5309\f
c5aa993b 5310
170b53b2
UW
5311/* Compute a string of spaces suitable to indent the next line
5312 so it starts at the position corresponding to the table column
5313 named COL_NAME in the currently active table of UIOUT. */
5314
5315static char *
5316wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5317{
5318 static char wrap_indent[80];
5319 int i, total_width, width, align;
5320 char *text;
5321
5322 total_width = 0;
5323 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5324 {
5325 if (strcmp (text, col_name) == 0)
5326 {
5327 gdb_assert (total_width < sizeof wrap_indent);
5328 memset (wrap_indent, ' ', total_width);
5329 wrap_indent[total_width] = 0;
5330
5331 return wrap_indent;
5332 }
5333
5334 total_width += width + 1;
5335 }
5336
5337 return NULL;
5338}
5339
b775012e
LM
5340/* Determine if the locations of this breakpoint will have their conditions
5341 evaluated by the target, host or a mix of both. Returns the following:
5342
5343 "host": Host evals condition.
5344 "host or target": Host or Target evals condition.
5345 "target": Target evals condition.
5346*/
5347
5348static const char *
5349bp_condition_evaluator (struct breakpoint *b)
5350{
5351 struct bp_location *bl;
5352 char host_evals = 0;
5353 char target_evals = 0;
5354
5355 if (!b)
5356 return NULL;
5357
5358 if (!is_breakpoint (b))
5359 return NULL;
5360
5361 if (gdb_evaluates_breakpoint_condition_p ()
5362 || !target_supports_evaluation_of_breakpoint_conditions ())
5363 return condition_evaluation_host;
5364
5365 for (bl = b->loc; bl; bl = bl->next)
5366 {
5367 if (bl->cond_bytecode)
5368 target_evals++;
5369 else
5370 host_evals++;
5371 }
5372
5373 if (host_evals && target_evals)
5374 return condition_evaluation_both;
5375 else if (target_evals)
5376 return condition_evaluation_target;
5377 else
5378 return condition_evaluation_host;
5379}
5380
5381/* Determine the breakpoint location's condition evaluator. This is
5382 similar to bp_condition_evaluator, but for locations. */
5383
5384static const char *
5385bp_location_condition_evaluator (struct bp_location *bl)
5386{
5387 if (bl && !is_breakpoint (bl->owner))
5388 return NULL;
5389
5390 if (gdb_evaluates_breakpoint_condition_p ()
5391 || !target_supports_evaluation_of_breakpoint_conditions ())
5392 return condition_evaluation_host;
5393
5394 if (bl && bl->cond_bytecode)
5395 return condition_evaluation_target;
5396 else
5397 return condition_evaluation_host;
5398}
5399
859825b8
JK
5400/* Print the LOC location out of the list of B->LOC locations. */
5401
170b53b2
UW
5402static void
5403print_breakpoint_location (struct breakpoint *b,
5404 struct bp_location *loc)
0d381245 5405{
79a45e25 5406 struct ui_out *uiout = current_uiout;
6c95b8df
PA
5407 struct cleanup *old_chain = save_current_program_space ();
5408
859825b8
JK
5409 if (loc != NULL && loc->shlib_disabled)
5410 loc = NULL;
5411
6c95b8df
PA
5412 if (loc != NULL)
5413 set_current_program_space (loc->pspace);
5414
56435ebe
TT
5415 if (b->display_canonical)
5416 ui_out_field_string (uiout, "what", b->addr_string);
f8eba3c6 5417 else if (loc && loc->source_file)
0d381245
VP
5418 {
5419 struct symbol *sym
5420 = find_pc_sect_function (loc->address, loc->section);
5421 if (sym)
5422 {
5423 ui_out_text (uiout, "in ");
5424 ui_out_field_string (uiout, "func",
5425 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
5426 ui_out_text (uiout, " ");
5427 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5428 ui_out_text (uiout, "at ");
0d381245 5429 }
f8eba3c6 5430 ui_out_field_string (uiout, "file", loc->source_file);
0d381245
VP
5431 ui_out_text (uiout, ":");
5432
5433 if (ui_out_is_mi_like_p (uiout))
5434 {
5435 struct symtab_and_line sal = find_pc_line (loc->address, 0);
5436 char *fullname = symtab_to_fullname (sal.symtab);
5437
5438 if (fullname)
5439 ui_out_field_string (uiout, "fullname", fullname);
5440 }
5441
f8eba3c6 5442 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 5443 }
859825b8 5444 else if (loc)
0d381245 5445 {
f99d8bf4
PA
5446 struct ui_file *stb = mem_fileopen ();
5447 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 5448
f99d8bf4 5449 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 5450 demangle, "");
0d381245 5451 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
5452
5453 do_cleanups (stb_chain);
0d381245 5454 }
859825b8
JK
5455 else
5456 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 5457
b775012e
LM
5458 if (loc && is_breakpoint (b)
5459 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5460 && bp_condition_evaluator (b) == condition_evaluation_both)
5461 {
5462 ui_out_text (uiout, " (");
5463 ui_out_field_string (uiout, "evaluated-by",
5464 bp_location_condition_evaluator (loc));
5465 ui_out_text (uiout, ")");
5466 }
5467
6c95b8df 5468 do_cleanups (old_chain);
0d381245
VP
5469}
5470
269b11a2
PA
5471static const char *
5472bptype_string (enum bptype type)
c906108c 5473{
c4093a6a
JM
5474 struct ep_type_description
5475 {
5476 enum bptype type;
5477 char *description;
5478 };
5479 static struct ep_type_description bptypes[] =
c906108c 5480 {
c5aa993b
JM
5481 {bp_none, "?deleted?"},
5482 {bp_breakpoint, "breakpoint"},
c906108c 5483 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
5484 {bp_until, "until"},
5485 {bp_finish, "finish"},
5486 {bp_watchpoint, "watchpoint"},
c906108c 5487 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5488 {bp_read_watchpoint, "read watchpoint"},
5489 {bp_access_watchpoint, "acc watchpoint"},
5490 {bp_longjmp, "longjmp"},
5491 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5492 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5493 {bp_exception, "exception"},
5494 {bp_exception_resume, "exception resume"},
c5aa993b 5495 {bp_step_resume, "step resume"},
2c03e5be 5496 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5497 {bp_watchpoint_scope, "watchpoint scope"},
5498 {bp_call_dummy, "call dummy"},
aa7d318d 5499 {bp_std_terminate, "std::terminate"},
c5aa993b 5500 {bp_shlib_event, "shlib events"},
c4093a6a 5501 {bp_thread_event, "thread events"},
1900040c 5502 {bp_overlay_event, "overlay events"},
0fd8e87f 5503 {bp_longjmp_master, "longjmp master"},
aa7d318d 5504 {bp_std_terminate_master, "std::terminate master"},
186c406b 5505 {bp_exception_master, "exception master"},
ce78b96d 5506 {bp_catchpoint, "catchpoint"},
1042e4c0 5507 {bp_tracepoint, "tracepoint"},
7a697b8d 5508 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5509 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5510 {bp_dprintf, "dprintf"},
4efc6507 5511 {bp_jit_event, "jit events"},
0e30163f
JK
5512 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5513 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5514 };
269b11a2
PA
5515
5516 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5517 || ((int) type != bptypes[(int) type].type))
5518 internal_error (__FILE__, __LINE__,
5519 _("bptypes table does not describe type #%d."),
5520 (int) type);
5521
5522 return bptypes[(int) type].description;
5523}
5524
5525/* Print B to gdb_stdout. */
5526
5527static void
5528print_one_breakpoint_location (struct breakpoint *b,
5529 struct bp_location *loc,
5530 int loc_number,
5531 struct bp_location **last_loc,
269b11a2
PA
5532 int allflag)
5533{
5534 struct command_line *l;
c2c6d25f 5535 static char bpenables[] = "nynny";
c906108c 5536
79a45e25 5537 struct ui_out *uiout = current_uiout;
0d381245
VP
5538 int header_of_multiple = 0;
5539 int part_of_multiple = (loc != NULL);
79a45b7d
TT
5540 struct value_print_options opts;
5541
5542 get_user_print_options (&opts);
0d381245
VP
5543
5544 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
5545 /* See comment in print_one_breakpoint concerning treatment of
5546 breakpoints with single disabled location. */
0d381245
VP
5547 if (loc == NULL
5548 && (b->loc != NULL
5549 && (b->loc->next != NULL || !b->loc->enabled)))
5550 header_of_multiple = 1;
5551 if (loc == NULL)
5552 loc = b->loc;
5553
c4093a6a
JM
5554 annotate_record ();
5555
5556 /* 1 */
5557 annotate_field (0);
0d381245
VP
5558 if (part_of_multiple)
5559 {
5560 char *formatted;
0c6773c1 5561 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
5562 ui_out_field_string (uiout, "number", formatted);
5563 xfree (formatted);
5564 }
5565 else
5566 {
5567 ui_out_field_int (uiout, "number", b->number);
5568 }
c4093a6a
JM
5569
5570 /* 2 */
5571 annotate_field (1);
0d381245
VP
5572 if (part_of_multiple)
5573 ui_out_field_skip (uiout, "type");
269b11a2
PA
5574 else
5575 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
5576
5577 /* 3 */
5578 annotate_field (2);
0d381245
VP
5579 if (part_of_multiple)
5580 ui_out_field_skip (uiout, "disp");
5581 else
2cec12e5 5582 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 5583
c4093a6a
JM
5584
5585 /* 4 */
5586 annotate_field (3);
0d381245 5587 if (part_of_multiple)
54e52265 5588 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 5589 else
4a64f543
MS
5590 ui_out_field_fmt (uiout, "enabled", "%c",
5591 bpenables[(int) b->enable_state]);
54e52265 5592 ui_out_spaces (uiout, 2);
0d381245 5593
c4093a6a
JM
5594
5595 /* 5 and 6 */
3086aeae 5596 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 5597 {
4a64f543
MS
5598 /* Although the print_one can possibly print all locations,
5599 calling it here is not likely to get any nice result. So,
5600 make sure there's just one location. */
0d381245 5601 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 5602 b->ops->print_one (b, last_loc);
0d381245 5603 }
3086aeae
DJ
5604 else
5605 switch (b->type)
5606 {
5607 case bp_none:
5608 internal_error (__FILE__, __LINE__,
e2e0b3e5 5609 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 5610 break;
c906108c 5611
3086aeae
DJ
5612 case bp_watchpoint:
5613 case bp_hardware_watchpoint:
5614 case bp_read_watchpoint:
5615 case bp_access_watchpoint:
3a5c3e22
PA
5616 {
5617 struct watchpoint *w = (struct watchpoint *) b;
5618
5619 /* Field 4, the address, is omitted (which makes the columns
5620 not line up too nicely with the headers, but the effect
5621 is relatively readable). */
5622 if (opts.addressprint)
5623 ui_out_field_skip (uiout, "addr");
5624 annotate_field (5);
5625 ui_out_field_string (uiout, "what", w->exp_string);
5626 }
3086aeae
DJ
5627 break;
5628
3086aeae
DJ
5629 case bp_breakpoint:
5630 case bp_hardware_breakpoint:
5631 case bp_until:
5632 case bp_finish:
5633 case bp_longjmp:
5634 case bp_longjmp_resume:
e2e4d78b 5635 case bp_longjmp_call_dummy:
186c406b
TT
5636 case bp_exception:
5637 case bp_exception_resume:
3086aeae 5638 case bp_step_resume:
2c03e5be 5639 case bp_hp_step_resume:
3086aeae
DJ
5640 case bp_watchpoint_scope:
5641 case bp_call_dummy:
aa7d318d 5642 case bp_std_terminate:
3086aeae
DJ
5643 case bp_shlib_event:
5644 case bp_thread_event:
5645 case bp_overlay_event:
0fd8e87f 5646 case bp_longjmp_master:
aa7d318d 5647 case bp_std_terminate_master:
186c406b 5648 case bp_exception_master:
1042e4c0 5649 case bp_tracepoint:
7a697b8d 5650 case bp_fast_tracepoint:
0fb4aa4b 5651 case bp_static_tracepoint:
e7e0cddf 5652 case bp_dprintf:
4efc6507 5653 case bp_jit_event:
0e30163f
JK
5654 case bp_gnu_ifunc_resolver:
5655 case bp_gnu_ifunc_resolver_return:
79a45b7d 5656 if (opts.addressprint)
3086aeae
DJ
5657 {
5658 annotate_field (4);
54e52265 5659 if (header_of_multiple)
0d381245 5660 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 5661 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 5662 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 5663 else
5af949e3
UW
5664 ui_out_field_core_addr (uiout, "addr",
5665 loc->gdbarch, loc->address);
3086aeae
DJ
5666 }
5667 annotate_field (5);
0d381245 5668 if (!header_of_multiple)
170b53b2 5669 print_breakpoint_location (b, loc);
0d381245 5670 if (b->loc)
a6d9a66e 5671 *last_loc = b->loc;
3086aeae
DJ
5672 break;
5673 }
c906108c 5674
6c95b8df
PA
5675
5676 /* For backward compatibility, don't display inferiors unless there
5677 are several. */
5678 if (loc != NULL
5679 && !header_of_multiple
5680 && (allflag
5681 || (!gdbarch_has_global_breakpoints (target_gdbarch)
5682 && (number_of_program_spaces () > 1
5683 || number_of_inferiors () > 1)
4a64f543
MS
5684 /* LOC is for existing B, it cannot be in
5685 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
5686 && loc->owner->type != bp_catchpoint)))
5687 {
5688 struct inferior *inf;
5689 int first = 1;
5690
5691 for (inf = inferior_list; inf != NULL; inf = inf->next)
5692 {
5693 if (inf->pspace == loc->pspace)
5694 {
5695 if (first)
5696 {
5697 first = 0;
5698 ui_out_text (uiout, " inf ");
5699 }
5700 else
5701 ui_out_text (uiout, ", ");
5702 ui_out_text (uiout, plongest (inf->num));
5703 }
5704 }
5705 }
5706
4a306c9a 5707 if (!part_of_multiple)
c4093a6a 5708 {
4a306c9a
JB
5709 if (b->thread != -1)
5710 {
5711 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 5712 "stop only in" line a little further down. */
4a306c9a
JB
5713 ui_out_text (uiout, " thread ");
5714 ui_out_field_int (uiout, "thread", b->thread);
5715 }
5716 else if (b->task != 0)
5717 {
5718 ui_out_text (uiout, " task ");
5719 ui_out_field_int (uiout, "task", b->task);
5720 }
c4093a6a 5721 }
f1310107 5722
8b93c638 5723 ui_out_text (uiout, "\n");
f1310107 5724
348d480f 5725 if (!part_of_multiple)
f1310107
TJB
5726 b->ops->print_one_detail (b, uiout);
5727
0d381245 5728 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
5729 {
5730 annotate_field (6);
8b93c638 5731 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 5732 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 5733 the frame ID. */
5af949e3
UW
5734 ui_out_field_core_addr (uiout, "frame",
5735 b->gdbarch, b->frame_id.stack_addr);
8b93c638 5736 ui_out_text (uiout, "\n");
c4093a6a
JM
5737 }
5738
28010a5d 5739 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
5740 {
5741 annotate_field (7);
d77f58be 5742 if (is_tracepoint (b))
1042e4c0
SS
5743 ui_out_text (uiout, "\ttrace only if ");
5744 else
5745 ui_out_text (uiout, "\tstop only if ");
0101ce28 5746 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
5747
5748 /* Print whether the target is doing the breakpoint's condition
5749 evaluation. If GDB is doing the evaluation, don't print anything. */
5750 if (is_breakpoint (b)
5751 && breakpoint_condition_evaluation_mode ()
5752 == condition_evaluation_target)
5753 {
5754 ui_out_text (uiout, " (");
5755 ui_out_field_string (uiout, "evaluated-by",
5756 bp_condition_evaluator (b));
5757 ui_out_text (uiout, " evals)");
5758 }
0101ce28
JJ
5759 ui_out_text (uiout, "\n");
5760 }
5761
0d381245 5762 if (!part_of_multiple && b->thread != -1)
c4093a6a 5763 {
4a64f543 5764 /* FIXME should make an annotation for this. */
8b93c638
JM
5765 ui_out_text (uiout, "\tstop only in thread ");
5766 ui_out_field_int (uiout, "thread", b->thread);
5767 ui_out_text (uiout, "\n");
c4093a6a
JM
5768 }
5769
63c715c6 5770 if (!part_of_multiple && b->hit_count)
c4093a6a 5771 {
4a64f543 5772 /* FIXME should make an annotation for this. */
c326b90e 5773 if (is_catchpoint (b))
8b93c638 5774 ui_out_text (uiout, "\tcatchpoint");
f196051f
SS
5775 else if (is_tracepoint (b))
5776 ui_out_text (uiout, "\ttracepoint");
8b93c638
JM
5777 else
5778 ui_out_text (uiout, "\tbreakpoint");
5779 ui_out_text (uiout, " already hit ");
5780 ui_out_field_int (uiout, "times", b->hit_count);
5781 if (b->hit_count == 1)
5782 ui_out_text (uiout, " time\n");
5783 else
5784 ui_out_text (uiout, " times\n");
c4093a6a
JM
5785 }
5786
4a64f543
MS
5787 /* Output the count also if it is zero, but only if this is mi.
5788 FIXME: Should have a better test for this. */
9dc5e2a9 5789 if (ui_out_is_mi_like_p (uiout))
63c715c6 5790 if (!part_of_multiple && b->hit_count == 0)
fb40c209 5791 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 5792
0d381245 5793 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
5794 {
5795 annotate_field (8);
8b93c638
JM
5796 ui_out_text (uiout, "\tignore next ");
5797 ui_out_field_int (uiout, "ignore", b->ignore_count);
5798 ui_out_text (uiout, " hits\n");
c4093a6a 5799 }
059fb39f 5800
816338b5
SS
5801 /* Note that an enable count of 1 corresponds to "enable once"
5802 behavior, which is reported by the combination of enablement and
5803 disposition, so we don't need to mention it here. */
5804 if (!part_of_multiple && b->enable_count > 1)
5805 {
5806 annotate_field (8);
5807 ui_out_text (uiout, "\tdisable after ");
5808 /* Tweak the wording to clarify that ignore and enable counts
5809 are distinct, and have additive effect. */
5810 if (b->ignore_count)
5811 ui_out_text (uiout, "additional ");
5812 else
5813 ui_out_text (uiout, "next ");
5814 ui_out_field_int (uiout, "enable", b->enable_count);
5815 ui_out_text (uiout, " hits\n");
5816 }
5817
f196051f
SS
5818 if (!part_of_multiple && is_tracepoint (b))
5819 {
5820 struct tracepoint *tp = (struct tracepoint *) b;
5821
5822 if (tp->traceframe_usage)
5823 {
5824 ui_out_text (uiout, "\ttrace buffer usage ");
5825 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
5826 ui_out_text (uiout, " bytes\n");
5827 }
5828 }
5829
9add0f1b 5830 l = b->commands ? b->commands->commands : NULL;
059fb39f 5831 if (!part_of_multiple && l)
c4093a6a 5832 {
3b31d625
EZ
5833 struct cleanup *script_chain;
5834
c4093a6a 5835 annotate_field (9);
3b31d625 5836 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 5837 print_command_lines (uiout, l, 4);
3b31d625 5838 do_cleanups (script_chain);
c4093a6a 5839 }
d24317b4 5840
d9b3f62e 5841 if (is_tracepoint (b))
1042e4c0 5842 {
d9b3f62e
PA
5843 struct tracepoint *t = (struct tracepoint *) b;
5844
5845 if (!part_of_multiple && t->pass_count)
5846 {
5847 annotate_field (10);
5848 ui_out_text (uiout, "\tpass count ");
5849 ui_out_field_int (uiout, "pass", t->pass_count);
5850 ui_out_text (uiout, " \n");
5851 }
1042e4c0
SS
5852 }
5853
d24317b4
VP
5854 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5855 {
3a5c3e22
PA
5856 if (is_watchpoint (b))
5857 {
5858 struct watchpoint *w = (struct watchpoint *) b;
5859
5860 ui_out_field_string (uiout, "original-location", w->exp_string);
5861 }
5862 else if (b->addr_string)
d24317b4 5863 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 5864 }
c4093a6a 5865}
c5aa993b 5866
0d381245
VP
5867static void
5868print_one_breakpoint (struct breakpoint *b,
4a64f543 5869 struct bp_location **last_loc,
6c95b8df 5870 int allflag)
0d381245 5871{
8d3788bd 5872 struct cleanup *bkpt_chain;
79a45e25 5873 struct ui_out *uiout = current_uiout;
8d3788bd
VP
5874
5875 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5876
12c5a436 5877 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 5878 do_cleanups (bkpt_chain);
0d381245
VP
5879
5880 /* If this breakpoint has custom print function,
5881 it's already printed. Otherwise, print individual
5882 locations, if any. */
5883 if (b->ops == NULL || b->ops->print_one == NULL)
5884 {
4a64f543
MS
5885 /* If breakpoint has a single location that is disabled, we
5886 print it as if it had several locations, since otherwise it's
5887 hard to represent "breakpoint enabled, location disabled"
5888 situation.
5889
5890 Note that while hardware watchpoints have several locations
a3be7890 5891 internally, that's not a property exposed to user. */
0d381245 5892 if (b->loc
a5606eee 5893 && !is_hardware_watchpoint (b)
8d3788bd 5894 && (b->loc->next || !b->loc->enabled))
0d381245
VP
5895 {
5896 struct bp_location *loc;
5897 int n = 1;
8d3788bd 5898
0d381245 5899 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5900 {
5901 struct cleanup *inner2 =
5902 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5903 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5904 do_cleanups (inner2);
5905 }
0d381245
VP
5906 }
5907 }
5908}
5909
a6d9a66e
UW
5910static int
5911breakpoint_address_bits (struct breakpoint *b)
5912{
5913 int print_address_bits = 0;
5914 struct bp_location *loc;
5915
5916 for (loc = b->loc; loc; loc = loc->next)
5917 {
c7437ca6
PA
5918 int addr_bit;
5919
5920 /* Software watchpoints that aren't watching memory don't have
5921 an address to print. */
5922 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5923 continue;
5924
5925 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5926 if (addr_bit > print_address_bits)
5927 print_address_bits = addr_bit;
5928 }
5929
5930 return print_address_bits;
5931}
0d381245 5932
c4093a6a
JM
5933struct captured_breakpoint_query_args
5934 {
5935 int bnum;
5936 };
c5aa993b 5937
c4093a6a 5938static int
2b65245e 5939do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5940{
5941 struct captured_breakpoint_query_args *args = data;
52f0bd74 5942 struct breakpoint *b;
a6d9a66e 5943 struct bp_location *dummy_loc = NULL;
cc59ec59 5944
c4093a6a
JM
5945 ALL_BREAKPOINTS (b)
5946 {
5947 if (args->bnum == b->number)
c5aa993b 5948 {
12c5a436 5949 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5950 return GDB_RC_OK;
c5aa993b 5951 }
c4093a6a
JM
5952 }
5953 return GDB_RC_NONE;
5954}
c5aa993b 5955
c4093a6a 5956enum gdb_rc
4a64f543
MS
5957gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5958 char **error_message)
c4093a6a
JM
5959{
5960 struct captured_breakpoint_query_args args;
cc59ec59 5961
c4093a6a
JM
5962 args.bnum = bnum;
5963 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5964 an error. */
b0b13bb4
DJ
5965 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5966 error_message, RETURN_MASK_ALL) < 0)
5967 return GDB_RC_FAIL;
5968 else
5969 return GDB_RC_OK;
c4093a6a 5970}
c5aa993b 5971
09d682a4
TT
5972/* Return true if this breakpoint was set by the user, false if it is
5973 internal or momentary. */
5974
5975int
5976user_breakpoint_p (struct breakpoint *b)
5977{
46c6471b 5978 return b->number > 0;
09d682a4
TT
5979}
5980
7f3b0473 5981/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5982 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5983 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5984 FILTER is non-NULL, call it on each breakpoint and only include the
5985 ones for which it returns non-zero. Return the total number of
5986 breakpoints listed. */
c906108c 5987
d77f58be 5988static int
e5a67952 5989breakpoint_1 (char *args, int allflag,
4a64f543 5990 int (*filter) (const struct breakpoint *))
c4093a6a 5991{
52f0bd74 5992 struct breakpoint *b;
a6d9a66e 5993 struct bp_location *last_loc = NULL;
7f3b0473 5994 int nr_printable_breakpoints;
3b31d625 5995 struct cleanup *bkpttbl_chain;
79a45b7d 5996 struct value_print_options opts;
a6d9a66e 5997 int print_address_bits = 0;
269b11a2 5998 int print_type_col_width = 14;
79a45e25 5999 struct ui_out *uiout = current_uiout;
269b11a2 6000
79a45b7d
TT
6001 get_user_print_options (&opts);
6002
4a64f543
MS
6003 /* Compute the number of rows in the table, as well as the size
6004 required for address fields. */
7f3b0473
AC
6005 nr_printable_breakpoints = 0;
6006 ALL_BREAKPOINTS (b)
e5a67952
MS
6007 {
6008 /* If we have a filter, only list the breakpoints it accepts. */
6009 if (filter && !filter (b))
6010 continue;
6011
6012 /* If we have an "args" string, it is a list of breakpoints to
6013 accept. Skip the others. */
6014 if (args != NULL && *args != '\0')
6015 {
6016 if (allflag && parse_and_eval_long (args) != b->number)
6017 continue;
6018 if (!allflag && !number_is_in_list (args, b->number))
6019 continue;
6020 }
269b11a2 6021
e5a67952
MS
6022 if (allflag || user_breakpoint_p (b))
6023 {
6024 int addr_bit, type_len;
a6d9a66e 6025
e5a67952
MS
6026 addr_bit = breakpoint_address_bits (b);
6027 if (addr_bit > print_address_bits)
6028 print_address_bits = addr_bit;
269b11a2 6029
e5a67952
MS
6030 type_len = strlen (bptype_string (b->type));
6031 if (type_len > print_type_col_width)
6032 print_type_col_width = type_len;
6033
6034 nr_printable_breakpoints++;
6035 }
6036 }
7f3b0473 6037
79a45b7d 6038 if (opts.addressprint)
3b31d625 6039 bkpttbl_chain
3e43a32a
MS
6040 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6041 nr_printable_breakpoints,
3b31d625 6042 "BreakpointTable");
8b93c638 6043 else
3b31d625 6044 bkpttbl_chain
3e43a32a
MS
6045 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6046 nr_printable_breakpoints,
3b31d625 6047 "BreakpointTable");
8b93c638 6048
7f3b0473 6049 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6050 annotate_breakpoints_headers ();
6051 if (nr_printable_breakpoints > 0)
6052 annotate_field (0);
4a64f543 6053 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6054 if (nr_printable_breakpoints > 0)
6055 annotate_field (1);
269b11a2 6056 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6057 "type", "Type"); /* 2 */
d7faa9e7
AC
6058 if (nr_printable_breakpoints > 0)
6059 annotate_field (2);
4a64f543 6060 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6061 if (nr_printable_breakpoints > 0)
6062 annotate_field (3);
54e52265 6063 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6064 if (opts.addressprint)
e5a67952
MS
6065 {
6066 if (nr_printable_breakpoints > 0)
6067 annotate_field (4);
6068 if (print_address_bits <= 32)
6069 ui_out_table_header (uiout, 10, ui_left,
6070 "addr", "Address"); /* 5 */
6071 else
6072 ui_out_table_header (uiout, 18, ui_left,
6073 "addr", "Address"); /* 5 */
6074 }
d7faa9e7
AC
6075 if (nr_printable_breakpoints > 0)
6076 annotate_field (5);
6077 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6078 ui_out_table_body (uiout);
6079 if (nr_printable_breakpoints > 0)
6080 annotate_breakpoints_table ();
7f3b0473 6081
c4093a6a 6082 ALL_BREAKPOINTS (b)
e5a67952
MS
6083 {
6084 QUIT;
6085 /* If we have a filter, only list the breakpoints it accepts. */
6086 if (filter && !filter (b))
6087 continue;
6088
6089 /* If we have an "args" string, it is a list of breakpoints to
6090 accept. Skip the others. */
6091
6092 if (args != NULL && *args != '\0')
6093 {
6094 if (allflag) /* maintenance info breakpoint */
6095 {
6096 if (parse_and_eval_long (args) != b->number)
6097 continue;
6098 }
6099 else /* all others */
6100 {
6101 if (!number_is_in_list (args, b->number))
6102 continue;
6103 }
6104 }
6105 /* We only print out user settable breakpoints unless the
6106 allflag is set. */
6107 if (allflag || user_breakpoint_p (b))
12c5a436 6108 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6109 }
6110
3b31d625 6111 do_cleanups (bkpttbl_chain);
698384cd 6112
7f3b0473 6113 if (nr_printable_breakpoints == 0)
c906108c 6114 {
4a64f543
MS
6115 /* If there's a filter, let the caller decide how to report
6116 empty list. */
d77f58be
SS
6117 if (!filter)
6118 {
e5a67952 6119 if (args == NULL || *args == '\0')
d77f58be
SS
6120 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6121 else
4a64f543 6122 ui_out_message (uiout, 0,
e5a67952
MS
6123 "No breakpoint or watchpoint matching '%s'.\n",
6124 args);
d77f58be 6125 }
c906108c
SS
6126 }
6127 else
c4093a6a 6128 {
a6d9a66e
UW
6129 if (last_loc && !server_command)
6130 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6131 }
c906108c 6132
4a64f543 6133 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6134 there have been breakpoints? */
c906108c 6135 annotate_breakpoints_table_end ();
d77f58be
SS
6136
6137 return nr_printable_breakpoints;
c906108c
SS
6138}
6139
ad443146
SS
6140/* Display the value of default-collect in a way that is generally
6141 compatible with the breakpoint list. */
6142
6143static void
6144default_collect_info (void)
6145{
79a45e25
PA
6146 struct ui_out *uiout = current_uiout;
6147
ad443146
SS
6148 /* If it has no value (which is frequently the case), say nothing; a
6149 message like "No default-collect." gets in user's face when it's
6150 not wanted. */
6151 if (!*default_collect)
6152 return;
6153
6154 /* The following phrase lines up nicely with per-tracepoint collect
6155 actions. */
6156 ui_out_text (uiout, "default collect ");
6157 ui_out_field_string (uiout, "default-collect", default_collect);
6158 ui_out_text (uiout, " \n");
6159}
6160
c906108c 6161static void
e5a67952 6162breakpoints_info (char *args, int from_tty)
c906108c 6163{
e5a67952 6164 breakpoint_1 (args, 0, NULL);
ad443146
SS
6165
6166 default_collect_info ();
d77f58be
SS
6167}
6168
6169static void
e5a67952 6170watchpoints_info (char *args, int from_tty)
d77f58be 6171{
e5a67952 6172 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6173 struct ui_out *uiout = current_uiout;
d77f58be
SS
6174
6175 if (num_printed == 0)
6176 {
e5a67952 6177 if (args == NULL || *args == '\0')
d77f58be
SS
6178 ui_out_message (uiout, 0, "No watchpoints.\n");
6179 else
e5a67952 6180 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6181 }
c906108c
SS
6182}
6183
7a292a7a 6184static void
e5a67952 6185maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6186{
e5a67952 6187 breakpoint_1 (args, 1, NULL);
ad443146
SS
6188
6189 default_collect_info ();
c906108c
SS
6190}
6191
0d381245 6192static int
714835d5 6193breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6194 struct program_space *pspace,
714835d5 6195 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6196{
6197 struct bp_location *bl = b->loc;
cc59ec59 6198
0d381245
VP
6199 for (; bl; bl = bl->next)
6200 {
6c95b8df
PA
6201 if (bl->pspace == pspace
6202 && bl->address == pc
0d381245
VP
6203 && (!overlay_debugging || bl->section == section))
6204 return 1;
6205 }
6206 return 0;
6207}
6208
672f9b60 6209/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6210 concerns with logical breakpoints, so we match program spaces, not
6211 address spaces. */
c906108c
SS
6212
6213static void
6c95b8df
PA
6214describe_other_breakpoints (struct gdbarch *gdbarch,
6215 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6216 struct obj_section *section, int thread)
c906108c 6217{
52f0bd74
AC
6218 int others = 0;
6219 struct breakpoint *b;
c906108c
SS
6220
6221 ALL_BREAKPOINTS (b)
672f9b60
KP
6222 others += (user_breakpoint_p (b)
6223 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6224 if (others > 0)
6225 {
a3f17187
AC
6226 if (others == 1)
6227 printf_filtered (_("Note: breakpoint "));
6228 else /* if (others == ???) */
6229 printf_filtered (_("Note: breakpoints "));
c906108c 6230 ALL_BREAKPOINTS (b)
672f9b60 6231 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6232 {
6233 others--;
6234 printf_filtered ("%d", b->number);
6235 if (b->thread == -1 && thread != -1)
6236 printf_filtered (" (all threads)");
6237 else if (b->thread != -1)
6238 printf_filtered (" (thread %d)", b->thread);
6239 printf_filtered ("%s%s ",
059fb39f 6240 ((b->enable_state == bp_disabled
f8eba3c6 6241 || b->enable_state == bp_call_disabled)
0d381245
VP
6242 ? " (disabled)"
6243 : b->enable_state == bp_permanent
6244 ? " (permanent)"
6245 : ""),
6246 (others > 1) ? ","
6247 : ((others == 1) ? " and" : ""));
6248 }
a3f17187 6249 printf_filtered (_("also set at pc "));
5af949e3 6250 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6251 printf_filtered (".\n");
6252 }
6253}
6254\f
c906108c 6255
e4f237da
KB
6256/* Return true iff it is meaningful to use the address member of
6257 BPT. For some breakpoint types, the address member is irrelevant
6258 and it makes no sense to attempt to compare it to other addresses
6259 (or use it for any other purpose either).
6260
4a64f543
MS
6261 More specifically, each of the following breakpoint types will
6262 always have a zero valued address and we don't want to mark
6263 breakpoints of any of these types to be a duplicate of an actual
6264 breakpoint at address zero:
e4f237da
KB
6265
6266 bp_watchpoint
2d134ed3
PA
6267 bp_catchpoint
6268
6269*/
e4f237da
KB
6270
6271static int
6272breakpoint_address_is_meaningful (struct breakpoint *bpt)
6273{
6274 enum bptype type = bpt->type;
6275
2d134ed3
PA
6276 return (type != bp_watchpoint && type != bp_catchpoint);
6277}
6278
6279/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6280 true if LOC1 and LOC2 represent the same watchpoint location. */
6281
6282static int
4a64f543
MS
6283watchpoint_locations_match (struct bp_location *loc1,
6284 struct bp_location *loc2)
2d134ed3 6285{
3a5c3e22
PA
6286 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6287 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6288
6289 /* Both of them must exist. */
6290 gdb_assert (w1 != NULL);
6291 gdb_assert (w2 != NULL);
2bdf28a0 6292
4a64f543
MS
6293 /* If the target can evaluate the condition expression in hardware,
6294 then we we need to insert both watchpoints even if they are at
6295 the same place. Otherwise the watchpoint will only trigger when
6296 the condition of whichever watchpoint was inserted evaluates to
6297 true, not giving a chance for GDB to check the condition of the
6298 other watchpoint. */
3a5c3e22 6299 if ((w1->cond_exp
4a64f543
MS
6300 && target_can_accel_watchpoint_condition (loc1->address,
6301 loc1->length,
0cf6dd15 6302 loc1->watchpoint_type,
3a5c3e22
PA
6303 w1->cond_exp))
6304 || (w2->cond_exp
4a64f543
MS
6305 && target_can_accel_watchpoint_condition (loc2->address,
6306 loc2->length,
0cf6dd15 6307 loc2->watchpoint_type,
3a5c3e22 6308 w2->cond_exp)))
0cf6dd15
TJB
6309 return 0;
6310
85d721b8
PA
6311 /* Note that this checks the owner's type, not the location's. In
6312 case the target does not support read watchpoints, but does
6313 support access watchpoints, we'll have bp_read_watchpoint
6314 watchpoints with hw_access locations. Those should be considered
6315 duplicates of hw_read locations. The hw_read locations will
6316 become hw_access locations later. */
2d134ed3
PA
6317 return (loc1->owner->type == loc2->owner->type
6318 && loc1->pspace->aspace == loc2->pspace->aspace
6319 && loc1->address == loc2->address
6320 && loc1->length == loc2->length);
e4f237da
KB
6321}
6322
6c95b8df
PA
6323/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6324 same breakpoint location. In most targets, this can only be true
6325 if ASPACE1 matches ASPACE2. On targets that have global
6326 breakpoints, the address space doesn't really matter. */
6327
6328static int
6329breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6330 struct address_space *aspace2, CORE_ADDR addr2)
6331{
6332 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6333 || aspace1 == aspace2)
6334 && addr1 == addr2);
6335}
6336
f1310107
TJB
6337/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6338 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6339 matches ASPACE2. On targets that have global breakpoints, the address
6340 space doesn't really matter. */
6341
6342static int
6343breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6344 int len1, struct address_space *aspace2,
6345 CORE_ADDR addr2)
6346{
6347 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6348 || aspace1 == aspace2)
6349 && addr2 >= addr1 && addr2 < addr1 + len1);
6350}
6351
6352/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6353 a ranged breakpoint. In most targets, a match happens only if ASPACE
6354 matches the breakpoint's address space. On targets that have global
6355 breakpoints, the address space doesn't really matter. */
6356
6357static int
6358breakpoint_location_address_match (struct bp_location *bl,
6359 struct address_space *aspace,
6360 CORE_ADDR addr)
6361{
6362 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6363 aspace, addr)
6364 || (bl->length
6365 && breakpoint_address_match_range (bl->pspace->aspace,
6366 bl->address, bl->length,
6367 aspace, addr)));
6368}
6369
1e4d1764
YQ
6370/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6371 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6372 true, otherwise returns false. */
6373
6374static int
6375tracepoint_locations_match (struct bp_location *loc1,
6376 struct bp_location *loc2)
6377{
6378 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6379 /* Since tracepoint locations are never duplicated with others', tracepoint
6380 locations at the same address of different tracepoints are regarded as
6381 different locations. */
6382 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6383 else
6384 return 0;
6385}
6386
2d134ed3
PA
6387/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6388 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6389 represent the same location. */
6390
6391static int
4a64f543
MS
6392breakpoint_locations_match (struct bp_location *loc1,
6393 struct bp_location *loc2)
2d134ed3 6394{
2bdf28a0
JK
6395 int hw_point1, hw_point2;
6396
6397 /* Both of them must not be in moribund_locations. */
6398 gdb_assert (loc1->owner != NULL);
6399 gdb_assert (loc2->owner != NULL);
6400
6401 hw_point1 = is_hardware_watchpoint (loc1->owner);
6402 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6403
6404 if (hw_point1 != hw_point2)
6405 return 0;
6406 else if (hw_point1)
6407 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6408 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6409 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6410 else
f1310107
TJB
6411 /* We compare bp_location.length in order to cover ranged breakpoints. */
6412 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6413 loc2->pspace->aspace, loc2->address)
6414 && loc1->length == loc2->length);
2d134ed3
PA
6415}
6416
76897487
KB
6417static void
6418breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6419 int bnum, int have_bnum)
6420{
f63fbe86
MS
6421 /* The longest string possibly returned by hex_string_custom
6422 is 50 chars. These must be at least that big for safety. */
6423 char astr1[64];
6424 char astr2[64];
76897487 6425
bb599908
PH
6426 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6427 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6428 if (have_bnum)
8a3fe4f8 6429 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6430 bnum, astr1, astr2);
6431 else
8a3fe4f8 6432 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6433}
6434
4a64f543
MS
6435/* Adjust a breakpoint's address to account for architectural
6436 constraints on breakpoint placement. Return the adjusted address.
6437 Note: Very few targets require this kind of adjustment. For most
6438 targets, this function is simply the identity function. */
76897487
KB
6439
6440static CORE_ADDR
a6d9a66e
UW
6441adjust_breakpoint_address (struct gdbarch *gdbarch,
6442 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6443{
a6d9a66e 6444 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
6445 {
6446 /* Very few targets need any kind of breakpoint adjustment. */
6447 return bpaddr;
6448 }
88f7da05
KB
6449 else if (bptype == bp_watchpoint
6450 || bptype == bp_hardware_watchpoint
6451 || bptype == bp_read_watchpoint
6452 || bptype == bp_access_watchpoint
fe798b75 6453 || bptype == bp_catchpoint)
88f7da05
KB
6454 {
6455 /* Watchpoints and the various bp_catch_* eventpoints should not
6456 have their addresses modified. */
6457 return bpaddr;
6458 }
76897487
KB
6459 else
6460 {
6461 CORE_ADDR adjusted_bpaddr;
6462
6463 /* Some targets have architectural constraints on the placement
6464 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 6465 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
6466
6467 /* An adjusted breakpoint address can significantly alter
6468 a user's expectations. Print a warning if an adjustment
6469 is required. */
6470 if (adjusted_bpaddr != bpaddr)
6471 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6472
6473 return adjusted_bpaddr;
6474 }
6475}
6476
28010a5d
PA
6477void
6478init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6479 struct breakpoint *owner)
7cc221ef 6480{
7cc221ef
DJ
6481 memset (loc, 0, sizeof (*loc));
6482
348d480f
PA
6483 gdb_assert (ops != NULL);
6484
28010a5d
PA
6485 loc->ops = ops;
6486 loc->owner = owner;
511a6cd4 6487 loc->cond = NULL;
b775012e 6488 loc->cond_bytecode = NULL;
0d381245
VP
6489 loc->shlib_disabled = 0;
6490 loc->enabled = 1;
e049a4b5 6491
28010a5d 6492 switch (owner->type)
e049a4b5
DJ
6493 {
6494 case bp_breakpoint:
6495 case bp_until:
6496 case bp_finish:
6497 case bp_longjmp:
6498 case bp_longjmp_resume:
e2e4d78b 6499 case bp_longjmp_call_dummy:
186c406b
TT
6500 case bp_exception:
6501 case bp_exception_resume:
e049a4b5 6502 case bp_step_resume:
2c03e5be 6503 case bp_hp_step_resume:
e049a4b5
DJ
6504 case bp_watchpoint_scope:
6505 case bp_call_dummy:
aa7d318d 6506 case bp_std_terminate:
e049a4b5
DJ
6507 case bp_shlib_event:
6508 case bp_thread_event:
6509 case bp_overlay_event:
4efc6507 6510 case bp_jit_event:
0fd8e87f 6511 case bp_longjmp_master:
aa7d318d 6512 case bp_std_terminate_master:
186c406b 6513 case bp_exception_master:
0e30163f
JK
6514 case bp_gnu_ifunc_resolver:
6515 case bp_gnu_ifunc_resolver_return:
e7e0cddf 6516 case bp_dprintf:
e049a4b5 6517 loc->loc_type = bp_loc_software_breakpoint;
b775012e 6518 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6519 break;
6520 case bp_hardware_breakpoint:
6521 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 6522 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6523 break;
6524 case bp_hardware_watchpoint:
6525 case bp_read_watchpoint:
6526 case bp_access_watchpoint:
6527 loc->loc_type = bp_loc_hardware_watchpoint;
6528 break;
6529 case bp_watchpoint:
ce78b96d 6530 case bp_catchpoint:
15c3d785
PA
6531 case bp_tracepoint:
6532 case bp_fast_tracepoint:
0fb4aa4b 6533 case bp_static_tracepoint:
e049a4b5
DJ
6534 loc->loc_type = bp_loc_other;
6535 break;
6536 default:
e2e0b3e5 6537 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
6538 }
6539
f431efe5 6540 loc->refc = 1;
28010a5d
PA
6541}
6542
6543/* Allocate a struct bp_location. */
6544
6545static struct bp_location *
6546allocate_bp_location (struct breakpoint *bpt)
6547{
348d480f
PA
6548 return bpt->ops->allocate_location (bpt);
6549}
7cc221ef 6550
f431efe5
PA
6551static void
6552free_bp_location (struct bp_location *loc)
fe3f5fa8 6553{
348d480f 6554 loc->ops->dtor (loc);
fe3f5fa8
VP
6555 xfree (loc);
6556}
6557
f431efe5
PA
6558/* Increment reference count. */
6559
6560static void
6561incref_bp_location (struct bp_location *bl)
6562{
6563 ++bl->refc;
6564}
6565
6566/* Decrement reference count. If the reference count reaches 0,
6567 destroy the bp_location. Sets *BLP to NULL. */
6568
6569static void
6570decref_bp_location (struct bp_location **blp)
6571{
0807b50c
PA
6572 gdb_assert ((*blp)->refc > 0);
6573
f431efe5
PA
6574 if (--(*blp)->refc == 0)
6575 free_bp_location (*blp);
6576 *blp = NULL;
6577}
6578
346774a9 6579/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 6580
346774a9
PA
6581static void
6582add_to_breakpoint_chain (struct breakpoint *b)
c906108c 6583{
346774a9 6584 struct breakpoint *b1;
c906108c 6585
346774a9
PA
6586 /* Add this breakpoint to the end of the chain so that a list of
6587 breakpoints will come out in order of increasing numbers. */
6588
6589 b1 = breakpoint_chain;
6590 if (b1 == 0)
6591 breakpoint_chain = b;
6592 else
6593 {
6594 while (b1->next)
6595 b1 = b1->next;
6596 b1->next = b;
6597 }
6598}
6599
6600/* Initializes breakpoint B with type BPTYPE and no locations yet. */
6601
6602static void
6603init_raw_breakpoint_without_location (struct breakpoint *b,
6604 struct gdbarch *gdbarch,
28010a5d 6605 enum bptype bptype,
c0a91b2b 6606 const struct breakpoint_ops *ops)
346774a9 6607{
c906108c 6608 memset (b, 0, sizeof (*b));
2219d63c 6609
348d480f
PA
6610 gdb_assert (ops != NULL);
6611
28010a5d 6612 b->ops = ops;
4d28f7a8 6613 b->type = bptype;
a6d9a66e 6614 b->gdbarch = gdbarch;
c906108c
SS
6615 b->language = current_language->la_language;
6616 b->input_radix = input_radix;
6617 b->thread = -1;
b5de0fa7 6618 b->enable_state = bp_enabled;
c906108c
SS
6619 b->next = 0;
6620 b->silent = 0;
6621 b->ignore_count = 0;
6622 b->commands = NULL;
818dd999 6623 b->frame_id = null_frame_id;
0d381245 6624 b->condition_not_parsed = 0;
84f4c1fe 6625 b->py_bp_object = NULL;
d0fb5eae 6626 b->related_breakpoint = b;
346774a9
PA
6627}
6628
6629/* Helper to set_raw_breakpoint below. Creates a breakpoint
6630 that has type BPTYPE and has no locations as yet. */
346774a9
PA
6631
6632static struct breakpoint *
6633set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 6634 enum bptype bptype,
c0a91b2b 6635 const struct breakpoint_ops *ops)
346774a9
PA
6636{
6637 struct breakpoint *b = XNEW (struct breakpoint);
6638
348d480f 6639 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 6640 add_to_breakpoint_chain (b);
0d381245
VP
6641 return b;
6642}
6643
0e30163f
JK
6644/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
6645 resolutions should be made as the user specified the location explicitly
6646 enough. */
6647
0d381245 6648static void
0e30163f 6649set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 6650{
2bdf28a0
JK
6651 gdb_assert (loc->owner != NULL);
6652
0d381245 6653 if (loc->owner->type == bp_breakpoint
1042e4c0 6654 || loc->owner->type == bp_hardware_breakpoint
d77f58be 6655 || is_tracepoint (loc->owner))
0d381245 6656 {
0e30163f 6657 int is_gnu_ifunc;
2c02bd72 6658 const char *function_name;
6a3a010b 6659 CORE_ADDR func_addr;
0e30163f 6660
2c02bd72 6661 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 6662 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
6663
6664 if (is_gnu_ifunc && !explicit_loc)
6665 {
6666 struct breakpoint *b = loc->owner;
6667
6668 gdb_assert (loc->pspace == current_program_space);
2c02bd72 6669 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
6670 &loc->requested_address))
6671 {
6672 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
6673 loc->address = adjust_breakpoint_address (loc->gdbarch,
6674 loc->requested_address,
6675 b->type);
6676 }
6677 else if (b->type == bp_breakpoint && b->loc == loc
6678 && loc->next == NULL && b->related_breakpoint == b)
6679 {
6680 /* Create only the whole new breakpoint of this type but do not
6681 mess more complicated breakpoints with multiple locations. */
6682 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
6683 /* Remember the resolver's address for use by the return
6684 breakpoint. */
6685 loc->related_address = func_addr;
0e30163f
JK
6686 }
6687 }
6688
2c02bd72
DE
6689 if (function_name)
6690 loc->function_name = xstrdup (function_name);
0d381245
VP
6691 }
6692}
6693
a6d9a66e 6694/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 6695struct gdbarch *
a6d9a66e
UW
6696get_sal_arch (struct symtab_and_line sal)
6697{
6698 if (sal.section)
6699 return get_objfile_arch (sal.section->objfile);
6700 if (sal.symtab)
6701 return get_objfile_arch (sal.symtab->objfile);
6702
6703 return NULL;
6704}
6705
346774a9
PA
6706/* Low level routine for partially initializing a breakpoint of type
6707 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 6708 file name, and line number are provided by SAL.
0d381245
VP
6709
6710 It is expected that the caller will complete the initialization of
6711 the newly created breakpoint struct as well as output any status
c56053d2 6712 information regarding the creation of a new breakpoint. */
0d381245 6713
346774a9
PA
6714static void
6715init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 6716 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 6717 const struct breakpoint_ops *ops)
0d381245 6718{
28010a5d 6719 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 6720
3742cc8b 6721 add_location_to_breakpoint (b, &sal);
0d381245 6722
6c95b8df
PA
6723 if (bptype != bp_catchpoint)
6724 gdb_assert (sal.pspace != NULL);
6725
f8eba3c6
TT
6726 /* Store the program space that was used to set the breakpoint,
6727 except for ordinary breakpoints, which are independent of the
6728 program space. */
6729 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
6730 b->pspace = sal.pspace;
0d381245 6731
c906108c 6732 breakpoints_changed ();
346774a9 6733}
c906108c 6734
346774a9
PA
6735/* set_raw_breakpoint is a low level routine for allocating and
6736 partially initializing a breakpoint of type BPTYPE. The newly
6737 created breakpoint's address, section, source file name, and line
6738 number are provided by SAL. The newly created and partially
6739 initialized breakpoint is added to the breakpoint chain and
6740 is also returned as the value of this function.
6741
6742 It is expected that the caller will complete the initialization of
6743 the newly created breakpoint struct as well as output any status
6744 information regarding the creation of a new breakpoint. In
6745 particular, set_raw_breakpoint does NOT set the breakpoint
6746 number! Care should be taken to not allow an error to occur
6747 prior to completing the initialization of the breakpoint. If this
6748 should happen, a bogus breakpoint will be left on the chain. */
6749
6750struct breakpoint *
6751set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 6752 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 6753 const struct breakpoint_ops *ops)
346774a9
PA
6754{
6755 struct breakpoint *b = XNEW (struct breakpoint);
6756
348d480f 6757 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 6758 add_to_breakpoint_chain (b);
c906108c
SS
6759 return b;
6760}
6761
c2c6d25f
JM
6762
6763/* Note that the breakpoint object B describes a permanent breakpoint
6764 instruction, hard-wired into the inferior's code. */
6765void
6766make_breakpoint_permanent (struct breakpoint *b)
6767{
0d381245 6768 struct bp_location *bl;
cc59ec59 6769
b5de0fa7 6770 b->enable_state = bp_permanent;
c2c6d25f 6771
4a64f543
MS
6772 /* By definition, permanent breakpoints are already present in the
6773 code. Mark all locations as inserted. For now,
6774 make_breakpoint_permanent is called in just one place, so it's
6775 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 6776 multiple locations or not, but it's easy to implement. */
0d381245
VP
6777 for (bl = b->loc; bl; bl = bl->next)
6778 bl->inserted = 1;
c2c6d25f
JM
6779}
6780
53a5351d 6781/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
6782 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
6783 initiated the operation. */
c906108c
SS
6784
6785void
186c406b 6786set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 6787{
35df4500 6788 struct breakpoint *b, *b_tmp;
186c406b 6789 int thread = tp->num;
0fd8e87f
UW
6790
6791 /* To avoid having to rescan all objfile symbols at every step,
6792 we maintain a list of continually-inserted but always disabled
6793 longjmp "master" breakpoints. Here, we simply create momentary
6794 clones of those and enable them for the requested thread. */
35df4500 6795 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 6796 if (b->pspace == current_program_space
186c406b
TT
6797 && (b->type == bp_longjmp_master
6798 || b->type == bp_exception_master))
0fd8e87f 6799 {
06edf0c0
PA
6800 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
6801 struct breakpoint *clone;
cc59ec59 6802
e2e4d78b
JK
6803 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
6804 after their removal. */
06edf0c0 6805 clone = momentary_breakpoint_from_master (b, type,
e2e4d78b 6806 &longjmp_breakpoint_ops);
0fd8e87f
UW
6807 clone->thread = thread;
6808 }
186c406b
TT
6809
6810 tp->initiating_frame = frame;
c906108c
SS
6811}
6812
611c83ae 6813/* Delete all longjmp breakpoints from THREAD. */
c906108c 6814void
611c83ae 6815delete_longjmp_breakpoint (int thread)
c906108c 6816{
35df4500 6817 struct breakpoint *b, *b_tmp;
c906108c 6818
35df4500 6819 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 6820 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
6821 {
6822 if (b->thread == thread)
6823 delete_breakpoint (b);
6824 }
c906108c
SS
6825}
6826
f59f708a
PA
6827void
6828delete_longjmp_breakpoint_at_next_stop (int thread)
6829{
6830 struct breakpoint *b, *b_tmp;
6831
6832 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6833 if (b->type == bp_longjmp || b->type == bp_exception)
6834 {
6835 if (b->thread == thread)
6836 b->disposition = disp_del_at_next_stop;
6837 }
6838}
6839
e2e4d78b
JK
6840/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
6841 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
6842 pointer to any of them. Return NULL if this system cannot place longjmp
6843 breakpoints. */
6844
6845struct breakpoint *
6846set_longjmp_breakpoint_for_call_dummy (void)
6847{
6848 struct breakpoint *b, *retval = NULL;
6849
6850 ALL_BREAKPOINTS (b)
6851 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
6852 {
6853 struct breakpoint *new_b;
6854
6855 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
6856 &momentary_breakpoint_ops);
6857 new_b->thread = pid_to_thread_id (inferior_ptid);
6858
6859 /* Link NEW_B into the chain of RETVAL breakpoints. */
6860
6861 gdb_assert (new_b->related_breakpoint == new_b);
6862 if (retval == NULL)
6863 retval = new_b;
6864 new_b->related_breakpoint = retval;
6865 while (retval->related_breakpoint != new_b->related_breakpoint)
6866 retval = retval->related_breakpoint;
6867 retval->related_breakpoint = new_b;
6868 }
6869
6870 return retval;
6871}
6872
6873/* Verify all existing dummy frames and their associated breakpoints for
6874 THREAD. Remove those which can no longer be found in the current frame
6875 stack.
6876
6877 You should call this function only at places where it is safe to currently
6878 unwind the whole stack. Failed stack unwind would discard live dummy
6879 frames. */
6880
6881void
6882check_longjmp_breakpoint_for_call_dummy (int thread)
6883{
6884 struct breakpoint *b, *b_tmp;
6885
6886 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6887 if (b->type == bp_longjmp_call_dummy && b->thread == thread)
6888 {
6889 struct breakpoint *dummy_b = b->related_breakpoint;
6890
6891 while (dummy_b != b && dummy_b->type != bp_call_dummy)
6892 dummy_b = dummy_b->related_breakpoint;
6893 if (dummy_b->type != bp_call_dummy
6894 || frame_find_by_id (dummy_b->frame_id) != NULL)
6895 continue;
6896
6897 dummy_frame_discard (dummy_b->frame_id);
6898
6899 while (b->related_breakpoint != b)
6900 {
6901 if (b_tmp == b->related_breakpoint)
6902 b_tmp = b->related_breakpoint->next;
6903 delete_breakpoint (b->related_breakpoint);
6904 }
6905 delete_breakpoint (b);
6906 }
6907}
6908
1900040c
MS
6909void
6910enable_overlay_breakpoints (void)
6911{
52f0bd74 6912 struct breakpoint *b;
1900040c
MS
6913
6914 ALL_BREAKPOINTS (b)
6915 if (b->type == bp_overlay_event)
6916 {
6917 b->enable_state = bp_enabled;
b60e7edf 6918 update_global_location_list (1);
c02f5703 6919 overlay_events_enabled = 1;
1900040c
MS
6920 }
6921}
6922
6923void
6924disable_overlay_breakpoints (void)
6925{
52f0bd74 6926 struct breakpoint *b;
1900040c
MS
6927
6928 ALL_BREAKPOINTS (b)
6929 if (b->type == bp_overlay_event)
6930 {
6931 b->enable_state = bp_disabled;
b60e7edf 6932 update_global_location_list (0);
c02f5703 6933 overlay_events_enabled = 0;
1900040c
MS
6934 }
6935}
6936
aa7d318d
TT
6937/* Set an active std::terminate breakpoint for each std::terminate
6938 master breakpoint. */
6939void
6940set_std_terminate_breakpoint (void)
6941{
35df4500 6942 struct breakpoint *b, *b_tmp;
aa7d318d 6943
35df4500 6944 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6945 if (b->pspace == current_program_space
6946 && b->type == bp_std_terminate_master)
6947 {
06edf0c0
PA
6948 momentary_breakpoint_from_master (b, bp_std_terminate,
6949 &momentary_breakpoint_ops);
aa7d318d
TT
6950 }
6951}
6952
6953/* Delete all the std::terminate breakpoints. */
6954void
6955delete_std_terminate_breakpoint (void)
6956{
35df4500 6957 struct breakpoint *b, *b_tmp;
aa7d318d 6958
35df4500 6959 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6960 if (b->type == bp_std_terminate)
6961 delete_breakpoint (b);
6962}
6963
c4093a6a 6964struct breakpoint *
a6d9a66e 6965create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6966{
6967 struct breakpoint *b;
c4093a6a 6968
06edf0c0
PA
6969 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
6970 &internal_breakpoint_ops);
6971
b5de0fa7 6972 b->enable_state = bp_enabled;
c4093a6a 6973 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6974 b->addr_string
6975 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6976
b60e7edf 6977 update_global_location_list_nothrow (1);
74960c60 6978
c4093a6a
JM
6979 return b;
6980}
6981
6982void
6983remove_thread_event_breakpoints (void)
6984{
35df4500 6985 struct breakpoint *b, *b_tmp;
c4093a6a 6986
35df4500 6987 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6988 if (b->type == bp_thread_event
6989 && b->loc->pspace == current_program_space)
c4093a6a
JM
6990 delete_breakpoint (b);
6991}
6992
0101ce28
JJ
6993struct lang_and_radix
6994 {
6995 enum language lang;
6996 int radix;
6997 };
6998
4efc6507
DE
6999/* Create a breakpoint for JIT code registration and unregistration. */
7000
7001struct breakpoint *
7002create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7003{
7004 struct breakpoint *b;
7005
06edf0c0
PA
7006 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7007 &internal_breakpoint_ops);
4efc6507
DE
7008 update_global_location_list_nothrow (1);
7009 return b;
7010}
0101ce28 7011
03673fc7
PP
7012/* Remove JIT code registration and unregistration breakpoint(s). */
7013
7014void
7015remove_jit_event_breakpoints (void)
7016{
7017 struct breakpoint *b, *b_tmp;
7018
7019 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7020 if (b->type == bp_jit_event
7021 && b->loc->pspace == current_program_space)
7022 delete_breakpoint (b);
7023}
7024
cae688ec
JJ
7025void
7026remove_solib_event_breakpoints (void)
7027{
35df4500 7028 struct breakpoint *b, *b_tmp;
cae688ec 7029
35df4500 7030 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7031 if (b->type == bp_shlib_event
7032 && b->loc->pspace == current_program_space)
cae688ec
JJ
7033 delete_breakpoint (b);
7034}
7035
7036struct breakpoint *
a6d9a66e 7037create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
7038{
7039 struct breakpoint *b;
7040
06edf0c0
PA
7041 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7042 &internal_breakpoint_ops);
b60e7edf 7043 update_global_location_list_nothrow (1);
cae688ec
JJ
7044 return b;
7045}
7046
7047/* Disable any breakpoints that are on code in shared libraries. Only
7048 apply to enabled breakpoints, disabled ones can just stay disabled. */
7049
7050void
cb851954 7051disable_breakpoints_in_shlibs (void)
cae688ec 7052{
876fa593 7053 struct bp_location *loc, **locp_tmp;
cae688ec 7054
876fa593 7055 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7056 {
2bdf28a0 7057 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7058 struct breakpoint *b = loc->owner;
2bdf28a0 7059
4a64f543
MS
7060 /* We apply the check to all breakpoints, including disabled for
7061 those with loc->duplicate set. This is so that when breakpoint
7062 becomes enabled, or the duplicate is removed, gdb will try to
7063 insert all breakpoints. If we don't set shlib_disabled here,
7064 we'll try to insert those breakpoints and fail. */
1042e4c0 7065 if (((b->type == bp_breakpoint)
508ccb1f 7066 || (b->type == bp_jit_event)
1042e4c0 7067 || (b->type == bp_hardware_breakpoint)
d77f58be 7068 || (is_tracepoint (b)))
6c95b8df 7069 && loc->pspace == current_program_space
0d381245 7070 && !loc->shlib_disabled
a77053c2 7071#ifdef PC_SOLIB
0d381245 7072 && PC_SOLIB (loc->address)
a77053c2 7073#else
6c95b8df 7074 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
7075#endif
7076 )
0d381245
VP
7077 {
7078 loc->shlib_disabled = 1;
7079 }
cae688ec
JJ
7080 }
7081}
7082
1e4d1764
YQ
7083/* Disable any breakpoints and tracepoints that are in an unloaded shared
7084 library. Only apply to enabled breakpoints, disabled ones can just stay
4a64f543 7085 disabled. */
84acb35a 7086
75149521 7087static void
84acb35a
JJ
7088disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7089{
876fa593 7090 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7091 int disabled_shlib_breaks = 0;
7092
c86cf029
VP
7093 /* SunOS a.out shared libraries are always mapped, so do not
7094 disable breakpoints; they will only be reported as unloaded
7095 through clear_solib when GDB discards its shared library
7096 list. See clear_solib for more information. */
7097 if (exec_bfd != NULL
7098 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7099 return;
7100
876fa593 7101 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7102 {
2bdf28a0 7103 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7104 struct breakpoint *b = loc->owner;
cc59ec59 7105
1e4d1764 7106 if (solib->pspace == loc->pspace
e2dd7057 7107 && !loc->shlib_disabled
1e4d1764
YQ
7108 && (((b->type == bp_breakpoint
7109 || b->type == bp_jit_event
7110 || b->type == bp_hardware_breakpoint)
7111 && (loc->loc_type == bp_loc_hardware_breakpoint
7112 || loc->loc_type == bp_loc_software_breakpoint))
7113 || is_tracepoint (b))
e2dd7057 7114 && solib_contains_address_p (solib, loc->address))
84acb35a 7115 {
e2dd7057
PP
7116 loc->shlib_disabled = 1;
7117 /* At this point, we cannot rely on remove_breakpoint
7118 succeeding so we must mark the breakpoint as not inserted
7119 to prevent future errors occurring in remove_breakpoints. */
7120 loc->inserted = 0;
8d3788bd
VP
7121
7122 /* This may cause duplicate notifications for the same breakpoint. */
7123 observer_notify_breakpoint_modified (b);
7124
e2dd7057
PP
7125 if (!disabled_shlib_breaks)
7126 {
7127 target_terminal_ours_for_output ();
3e43a32a
MS
7128 warning (_("Temporarily disabling breakpoints "
7129 "for unloaded shared library \"%s\""),
e2dd7057 7130 solib->so_name);
84acb35a 7131 }
e2dd7057 7132 disabled_shlib_breaks = 1;
84acb35a
JJ
7133 }
7134 }
84acb35a
JJ
7135}
7136
ce78b96d
JB
7137/* FORK & VFORK catchpoints. */
7138
e29a4733
PA
7139/* An instance of this type is used to represent a fork or vfork
7140 catchpoint. It includes a "struct breakpoint" as a kind of base
7141 class; users downcast to "struct breakpoint *" when needed. A
7142 breakpoint is really of this type iff its ops pointer points to
7143 CATCH_FORK_BREAKPOINT_OPS. */
7144
7145struct fork_catchpoint
7146{
7147 /* The base class. */
7148 struct breakpoint base;
7149
7150 /* Process id of a child process whose forking triggered this
7151 catchpoint. This field is only valid immediately after this
7152 catchpoint has triggered. */
7153 ptid_t forked_inferior_pid;
7154};
7155
4a64f543
MS
7156/* Implement the "insert" breakpoint_ops method for fork
7157 catchpoints. */
ce78b96d 7158
77b06cd7
TJB
7159static int
7160insert_catch_fork (struct bp_location *bl)
ce78b96d 7161{
77b06cd7 7162 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
7163}
7164
4a64f543
MS
7165/* Implement the "remove" breakpoint_ops method for fork
7166 catchpoints. */
ce78b96d
JB
7167
7168static int
77b06cd7 7169remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
7170{
7171 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
7172}
7173
7174/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7175 catchpoints. */
7176
7177static int
f1310107 7178breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7179 struct address_space *aspace, CORE_ADDR bp_addr,
7180 const struct target_waitstatus *ws)
ce78b96d 7181{
e29a4733
PA
7182 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7183
f90263c1
TT
7184 if (ws->kind != TARGET_WAITKIND_FORKED)
7185 return 0;
7186
7187 c->forked_inferior_pid = ws->value.related_pid;
7188 return 1;
ce78b96d
JB
7189}
7190
4a64f543
MS
7191/* Implement the "print_it" breakpoint_ops method for fork
7192 catchpoints. */
ce78b96d
JB
7193
7194static enum print_stop_action
348d480f 7195print_it_catch_fork (bpstat bs)
ce78b96d 7196{
36dfb11c 7197 struct ui_out *uiout = current_uiout;
348d480f
PA
7198 struct breakpoint *b = bs->breakpoint_at;
7199 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7200
ce78b96d 7201 annotate_catchpoint (b->number);
36dfb11c
TT
7202 if (b->disposition == disp_del)
7203 ui_out_text (uiout, "\nTemporary catchpoint ");
7204 else
7205 ui_out_text (uiout, "\nCatchpoint ");
7206 if (ui_out_is_mi_like_p (uiout))
7207 {
7208 ui_out_field_string (uiout, "reason",
7209 async_reason_lookup (EXEC_ASYNC_FORK));
7210 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7211 }
7212 ui_out_field_int (uiout, "bkptno", b->number);
7213 ui_out_text (uiout, " (forked process ");
7214 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7215 ui_out_text (uiout, "), ");
ce78b96d
JB
7216 return PRINT_SRC_AND_LOC;
7217}
7218
4a64f543
MS
7219/* Implement the "print_one" breakpoint_ops method for fork
7220 catchpoints. */
ce78b96d
JB
7221
7222static void
a6d9a66e 7223print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7224{
e29a4733 7225 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7226 struct value_print_options opts;
79a45e25 7227 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7228
7229 get_user_print_options (&opts);
7230
4a64f543
MS
7231 /* Field 4, the address, is omitted (which makes the columns not
7232 line up too nicely with the headers, but the effect is relatively
7233 readable). */
79a45b7d 7234 if (opts.addressprint)
ce78b96d
JB
7235 ui_out_field_skip (uiout, "addr");
7236 annotate_field (5);
7237 ui_out_text (uiout, "fork");
e29a4733 7238 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7239 {
7240 ui_out_text (uiout, ", process ");
7241 ui_out_field_int (uiout, "what",
e29a4733 7242 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7243 ui_out_spaces (uiout, 1);
7244 }
7245}
7246
7247/* Implement the "print_mention" breakpoint_ops method for fork
7248 catchpoints. */
7249
7250static void
7251print_mention_catch_fork (struct breakpoint *b)
7252{
7253 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7254}
7255
6149aea9
PA
7256/* Implement the "print_recreate" breakpoint_ops method for fork
7257 catchpoints. */
7258
7259static void
7260print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7261{
7262 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7263 print_recreate_thread (b, fp);
6149aea9
PA
7264}
7265
ce78b96d
JB
7266/* The breakpoint_ops structure to be used in fork catchpoints. */
7267
2060206e 7268static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7269
4a64f543
MS
7270/* Implement the "insert" breakpoint_ops method for vfork
7271 catchpoints. */
ce78b96d 7272
77b06cd7
TJB
7273static int
7274insert_catch_vfork (struct bp_location *bl)
ce78b96d 7275{
77b06cd7 7276 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
7277}
7278
4a64f543
MS
7279/* Implement the "remove" breakpoint_ops method for vfork
7280 catchpoints. */
ce78b96d
JB
7281
7282static int
77b06cd7 7283remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
7284{
7285 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
7286}
7287
7288/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7289 catchpoints. */
7290
7291static int
f1310107 7292breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
7293 struct address_space *aspace, CORE_ADDR bp_addr,
7294 const struct target_waitstatus *ws)
ce78b96d 7295{
e29a4733
PA
7296 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7297
f90263c1
TT
7298 if (ws->kind != TARGET_WAITKIND_VFORKED)
7299 return 0;
7300
7301 c->forked_inferior_pid = ws->value.related_pid;
7302 return 1;
ce78b96d
JB
7303}
7304
4a64f543
MS
7305/* Implement the "print_it" breakpoint_ops method for vfork
7306 catchpoints. */
ce78b96d
JB
7307
7308static enum print_stop_action
348d480f 7309print_it_catch_vfork (bpstat bs)
ce78b96d 7310{
36dfb11c 7311 struct ui_out *uiout = current_uiout;
348d480f 7312 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7313 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7314
ce78b96d 7315 annotate_catchpoint (b->number);
36dfb11c
TT
7316 if (b->disposition == disp_del)
7317 ui_out_text (uiout, "\nTemporary catchpoint ");
7318 else
7319 ui_out_text (uiout, "\nCatchpoint ");
7320 if (ui_out_is_mi_like_p (uiout))
7321 {
7322 ui_out_field_string (uiout, "reason",
7323 async_reason_lookup (EXEC_ASYNC_VFORK));
7324 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7325 }
7326 ui_out_field_int (uiout, "bkptno", b->number);
7327 ui_out_text (uiout, " (vforked process ");
7328 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7329 ui_out_text (uiout, "), ");
ce78b96d
JB
7330 return PRINT_SRC_AND_LOC;
7331}
7332
4a64f543
MS
7333/* Implement the "print_one" breakpoint_ops method for vfork
7334 catchpoints. */
ce78b96d
JB
7335
7336static void
a6d9a66e 7337print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7338{
e29a4733 7339 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7340 struct value_print_options opts;
79a45e25 7341 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7342
7343 get_user_print_options (&opts);
4a64f543
MS
7344 /* Field 4, the address, is omitted (which makes the columns not
7345 line up too nicely with the headers, but the effect is relatively
7346 readable). */
79a45b7d 7347 if (opts.addressprint)
ce78b96d
JB
7348 ui_out_field_skip (uiout, "addr");
7349 annotate_field (5);
7350 ui_out_text (uiout, "vfork");
e29a4733 7351 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7352 {
7353 ui_out_text (uiout, ", process ");
7354 ui_out_field_int (uiout, "what",
e29a4733 7355 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7356 ui_out_spaces (uiout, 1);
7357 }
7358}
7359
7360/* Implement the "print_mention" breakpoint_ops method for vfork
7361 catchpoints. */
7362
7363static void
7364print_mention_catch_vfork (struct breakpoint *b)
7365{
7366 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7367}
7368
6149aea9
PA
7369/* Implement the "print_recreate" breakpoint_ops method for vfork
7370 catchpoints. */
7371
7372static void
7373print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7374{
7375 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7376 print_recreate_thread (b, fp);
6149aea9
PA
7377}
7378
ce78b96d
JB
7379/* The breakpoint_ops structure to be used in vfork catchpoints. */
7380
2060206e 7381static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7382
edcc5120
TT
7383/* An instance of this type is used to represent an solib catchpoint.
7384 It includes a "struct breakpoint" as a kind of base class; users
7385 downcast to "struct breakpoint *" when needed. A breakpoint is
7386 really of this type iff its ops pointer points to
7387 CATCH_SOLIB_BREAKPOINT_OPS. */
7388
7389struct solib_catchpoint
7390{
7391 /* The base class. */
7392 struct breakpoint base;
7393
7394 /* True for "catch load", false for "catch unload". */
7395 unsigned char is_load;
7396
7397 /* Regular expression to match, if any. COMPILED is only valid when
7398 REGEX is non-NULL. */
7399 char *regex;
7400 regex_t compiled;
7401};
7402
7403static void
7404dtor_catch_solib (struct breakpoint *b)
7405{
7406 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7407
7408 if (self->regex)
7409 regfree (&self->compiled);
7410 xfree (self->regex);
7411
7412 base_breakpoint_ops.dtor (b);
7413}
7414
7415static int
7416insert_catch_solib (struct bp_location *ignore)
7417{
7418 return 0;
7419}
7420
7421static int
7422remove_catch_solib (struct bp_location *ignore)
7423{
7424 return 0;
7425}
7426
7427static int
7428breakpoint_hit_catch_solib (const struct bp_location *bl,
7429 struct address_space *aspace,
7430 CORE_ADDR bp_addr,
7431 const struct target_waitstatus *ws)
7432{
7433 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7434 struct breakpoint *other;
7435
7436 if (ws->kind == TARGET_WAITKIND_LOADED)
7437 return 1;
7438
7439 ALL_BREAKPOINTS (other)
7440 {
7441 struct bp_location *other_bl;
7442
7443 if (other == bl->owner)
7444 continue;
7445
7446 if (other->type != bp_shlib_event)
7447 continue;
7448
7449 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7450 continue;
7451
7452 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7453 {
7454 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7455 return 1;
7456 }
7457 }
7458
7459 return 0;
7460}
7461
7462static void
7463check_status_catch_solib (struct bpstats *bs)
7464{
7465 struct solib_catchpoint *self
7466 = (struct solib_catchpoint *) bs->breakpoint_at;
7467 int ix;
7468
7469 if (self->is_load)
7470 {
7471 struct so_list *iter;
7472
7473 for (ix = 0;
7474 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7475 ix, iter);
7476 ++ix)
7477 {
7478 if (!self->regex
7479 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7480 return;
7481 }
7482 }
7483 else
7484 {
7485 char *iter;
7486
7487 for (ix = 0;
7488 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7489 ix, iter);
7490 ++ix)
7491 {
7492 if (!self->regex
7493 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7494 return;
7495 }
7496 }
7497
7498 bs->stop = 0;
7499 bs->print_it = print_it_noop;
7500}
7501
7502static enum print_stop_action
7503print_it_catch_solib (bpstat bs)
7504{
7505 struct breakpoint *b = bs->breakpoint_at;
7506 struct ui_out *uiout = current_uiout;
7507
7508 annotate_catchpoint (b->number);
7509 if (b->disposition == disp_del)
7510 ui_out_text (uiout, "\nTemporary catchpoint ");
7511 else
7512 ui_out_text (uiout, "\nCatchpoint ");
7513 ui_out_field_int (uiout, "bkptno", b->number);
7514 ui_out_text (uiout, "\n");
7515 if (ui_out_is_mi_like_p (uiout))
7516 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7517 print_solib_event (1);
7518 return PRINT_SRC_AND_LOC;
7519}
7520
7521static void
7522print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7523{
7524 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7525 struct value_print_options opts;
7526 struct ui_out *uiout = current_uiout;
7527 char *msg;
7528
7529 get_user_print_options (&opts);
7530 /* Field 4, the address, is omitted (which makes the columns not
7531 line up too nicely with the headers, but the effect is relatively
7532 readable). */
7533 if (opts.addressprint)
7534 {
7535 annotate_field (4);
7536 ui_out_field_skip (uiout, "addr");
7537 }
7538
7539 annotate_field (5);
7540 if (self->is_load)
7541 {
7542 if (self->regex)
7543 msg = xstrprintf (_("load of library matching %s"), self->regex);
7544 else
7545 msg = xstrdup (_("load of library"));
7546 }
7547 else
7548 {
7549 if (self->regex)
7550 msg = xstrprintf (_("unload of library matching %s"), self->regex);
7551 else
7552 msg = xstrdup (_("unload of library"));
7553 }
7554 ui_out_field_string (uiout, "what", msg);
7555 xfree (msg);
7556}
7557
7558static void
7559print_mention_catch_solib (struct breakpoint *b)
7560{
7561 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7562
7563 printf_filtered (_("Catchpoint %d (%s)"), b->number,
7564 self->is_load ? "load" : "unload");
7565}
7566
7567static void
7568print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
7569{
7570 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7571
7572 fprintf_unfiltered (fp, "%s %s",
7573 b->disposition == disp_del ? "tcatch" : "catch",
7574 self->is_load ? "load" : "unload");
7575 if (self->regex)
7576 fprintf_unfiltered (fp, " %s", self->regex);
7577 fprintf_unfiltered (fp, "\n");
7578}
7579
7580static struct breakpoint_ops catch_solib_breakpoint_ops;
7581
7582/* A helper function that does all the work for "catch load" and
7583 "catch unload". */
7584
7585static void
7586catch_load_or_unload (char *arg, int from_tty, int is_load,
7587 struct cmd_list_element *command)
7588{
7589 struct solib_catchpoint *c;
7590 struct gdbarch *gdbarch = get_current_arch ();
7591 int tempflag;
edcc5120
TT
7592 struct cleanup *cleanup;
7593
7594 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7595
7596 if (!arg)
7597 arg = "";
7598 arg = skip_spaces (arg);
7599
7600 c = XCNEW (struct solib_catchpoint);
7601 cleanup = make_cleanup (xfree, c);
7602
7603 if (*arg != '\0')
7604 {
7605 int errcode;
7606
7607 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
7608 if (errcode != 0)
7609 {
7610 char *err = get_regcomp_error (errcode, &c->compiled);
7611
7612 make_cleanup (xfree, err);
7613 error (_("Invalid regexp (%s): %s"), err, arg);
7614 }
7615 c->regex = xstrdup (arg);
7616 }
7617
7618 c->is_load = is_load;
7619 init_catchpoint (&c->base, gdbarch, tempflag, NULL,
7620 &catch_solib_breakpoint_ops);
7621
7622 discard_cleanups (cleanup);
7623 install_breakpoint (0, &c->base, 1);
7624}
7625
7626static void
7627catch_load_command_1 (char *arg, int from_tty,
7628 struct cmd_list_element *command)
7629{
7630 catch_load_or_unload (arg, from_tty, 1, command);
7631}
7632
7633static void
7634catch_unload_command_1 (char *arg, int from_tty,
7635 struct cmd_list_element *command)
7636{
7637 catch_load_or_unload (arg, from_tty, 0, command);
7638}
7639
fa3064dd
YQ
7640DEF_VEC_I(int);
7641
be5c67c1
PA
7642/* An instance of this type is used to represent a syscall catchpoint.
7643 It includes a "struct breakpoint" as a kind of base class; users
7644 downcast to "struct breakpoint *" when needed. A breakpoint is
7645 really of this type iff its ops pointer points to
7646 CATCH_SYSCALL_BREAKPOINT_OPS. */
7647
7648struct syscall_catchpoint
7649{
7650 /* The base class. */
7651 struct breakpoint base;
7652
7653 /* Syscall numbers used for the 'catch syscall' feature. If no
7654 syscall has been specified for filtering, its value is NULL.
7655 Otherwise, it holds a list of all syscalls to be caught. The
7656 list elements are allocated with xmalloc. */
7657 VEC(int) *syscalls_to_be_caught;
7658};
7659
7660/* Implement the "dtor" breakpoint_ops method for syscall
7661 catchpoints. */
7662
7663static void
7664dtor_catch_syscall (struct breakpoint *b)
7665{
7666 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7667
7668 VEC_free (int, c->syscalls_to_be_caught);
348d480f 7669
2060206e 7670 base_breakpoint_ops.dtor (b);
be5c67c1
PA
7671}
7672
fa3064dd
YQ
7673static const struct inferior_data *catch_syscall_inferior_data = NULL;
7674
7675struct catch_syscall_inferior_data
7676{
7677 /* We keep a count of the number of times the user has requested a
7678 particular syscall to be tracked, and pass this information to the
7679 target. This lets capable targets implement filtering directly. */
7680
7681 /* Number of times that "any" syscall is requested. */
7682 int any_syscall_count;
7683
7684 /* Count of each system call. */
7685 VEC(int) *syscalls_counts;
7686
7687 /* This counts all syscall catch requests, so we can readily determine
7688 if any catching is necessary. */
7689 int total_syscalls_count;
7690};
7691
7692static struct catch_syscall_inferior_data*
7693get_catch_syscall_inferior_data (struct inferior *inf)
7694{
7695 struct catch_syscall_inferior_data *inf_data;
7696
7697 inf_data = inferior_data (inf, catch_syscall_inferior_data);
7698 if (inf_data == NULL)
7699 {
7700 inf_data = XZALLOC (struct catch_syscall_inferior_data);
7701 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
7702 }
7703
7704 return inf_data;
7705}
7706
7707static void
7708catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
7709{
7710 xfree (arg);
7711}
7712
7713
a96d9b2e
SDJ
7714/* Implement the "insert" breakpoint_ops method for syscall
7715 catchpoints. */
7716
77b06cd7
TJB
7717static int
7718insert_catch_syscall (struct bp_location *bl)
a96d9b2e 7719{
be5c67c1 7720 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 7721 struct inferior *inf = current_inferior ();
fa3064dd
YQ
7722 struct catch_syscall_inferior_data *inf_data
7723 = get_catch_syscall_inferior_data (inf);
a96d9b2e 7724
fa3064dd 7725 ++inf_data->total_syscalls_count;
be5c67c1 7726 if (!c->syscalls_to_be_caught)
fa3064dd 7727 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
7728 else
7729 {
7730 int i, iter;
cc59ec59 7731
a96d9b2e 7732 for (i = 0;
be5c67c1 7733 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7734 i++)
7735 {
7736 int elem;
cc59ec59 7737
fa3064dd 7738 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 7739 {
fa3064dd 7740 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
7741 uintptr_t vec_addr_offset
7742 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 7743 uintptr_t vec_addr;
fa3064dd
YQ
7744 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
7745 vec_addr = ((uintptr_t) VEC_address (int,
7746 inf_data->syscalls_counts)
7747 + vec_addr_offset);
a96d9b2e
SDJ
7748 memset ((void *) vec_addr, 0,
7749 (iter + 1 - old_size) * sizeof (int));
7750 }
fa3064dd
YQ
7751 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7752 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
7753 }
7754 }
7755
77b06cd7 7756 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
fa3064dd
YQ
7757 inf_data->total_syscalls_count != 0,
7758 inf_data->any_syscall_count,
7759 VEC_length (int,
7760 inf_data->syscalls_counts),
7761 VEC_address (int,
7762 inf_data->syscalls_counts));
a96d9b2e
SDJ
7763}
7764
7765/* Implement the "remove" breakpoint_ops method for syscall
7766 catchpoints. */
7767
7768static int
77b06cd7 7769remove_catch_syscall (struct bp_location *bl)
a96d9b2e 7770{
be5c67c1 7771 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 7772 struct inferior *inf = current_inferior ();
fa3064dd
YQ
7773 struct catch_syscall_inferior_data *inf_data
7774 = get_catch_syscall_inferior_data (inf);
a96d9b2e 7775
fa3064dd 7776 --inf_data->total_syscalls_count;
be5c67c1 7777 if (!c->syscalls_to_be_caught)
fa3064dd 7778 --inf_data->any_syscall_count;
a96d9b2e
SDJ
7779 else
7780 {
7781 int i, iter;
cc59ec59 7782
a96d9b2e 7783 for (i = 0;
be5c67c1 7784 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7785 i++)
7786 {
7787 int elem;
fa3064dd 7788 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
7789 /* Shouldn't happen. */
7790 continue;
fa3064dd
YQ
7791 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7792 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
7793 }
7794 }
7795
7796 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
fa3064dd
YQ
7797 inf_data->total_syscalls_count != 0,
7798 inf_data->any_syscall_count,
7799 VEC_length (int,
7800 inf_data->syscalls_counts),
3e43a32a 7801 VEC_address (int,
fa3064dd 7802 inf_data->syscalls_counts));
a96d9b2e
SDJ
7803}
7804
7805/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
7806 catchpoints. */
7807
7808static int
f1310107 7809breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
7810 struct address_space *aspace, CORE_ADDR bp_addr,
7811 const struct target_waitstatus *ws)
a96d9b2e 7812{
4a64f543
MS
7813 /* We must check if we are catching specific syscalls in this
7814 breakpoint. If we are, then we must guarantee that the called
7815 syscall is the same syscall we are catching. */
a96d9b2e 7816 int syscall_number = 0;
be5c67c1
PA
7817 const struct syscall_catchpoint *c
7818 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 7819
f90263c1
TT
7820 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
7821 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
7822 return 0;
7823
f90263c1
TT
7824 syscall_number = ws->value.syscall_number;
7825
a96d9b2e 7826 /* Now, checking if the syscall is the same. */
be5c67c1 7827 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7828 {
7829 int i, iter;
cc59ec59 7830
a96d9b2e 7831 for (i = 0;
be5c67c1 7832 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7833 i++)
7834 if (syscall_number == iter)
7835 break;
7836 /* Not the same. */
7837 if (!iter)
7838 return 0;
7839 }
7840
7841 return 1;
7842}
7843
7844/* Implement the "print_it" breakpoint_ops method for syscall
7845 catchpoints. */
7846
7847static enum print_stop_action
348d480f 7848print_it_catch_syscall (bpstat bs)
a96d9b2e 7849{
36dfb11c 7850 struct ui_out *uiout = current_uiout;
348d480f 7851 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
7852 /* These are needed because we want to know in which state a
7853 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
7854 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
7855 must print "called syscall" or "returned from syscall". */
7856 ptid_t ptid;
7857 struct target_waitstatus last;
7858 struct syscall s;
a96d9b2e
SDJ
7859
7860 get_last_target_status (&ptid, &last);
7861
7862 get_syscall_by_number (last.value.syscall_number, &s);
7863
7864 annotate_catchpoint (b->number);
7865
36dfb11c
TT
7866 if (b->disposition == disp_del)
7867 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 7868 else
36dfb11c
TT
7869 ui_out_text (uiout, "\nCatchpoint ");
7870 if (ui_out_is_mi_like_p (uiout))
7871 {
7872 ui_out_field_string (uiout, "reason",
7873 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
7874 ? EXEC_ASYNC_SYSCALL_ENTRY
7875 : EXEC_ASYNC_SYSCALL_RETURN));
7876 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7877 }
7878 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
7879
7880 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
7881 ui_out_text (uiout, " (call to syscall ");
7882 else
7883 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 7884
36dfb11c
TT
7885 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
7886 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
7887 if (s.name != NULL)
7888 ui_out_field_string (uiout, "syscall-name", s.name);
7889
7890 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
7891
7892 return PRINT_SRC_AND_LOC;
7893}
7894
7895/* Implement the "print_one" breakpoint_ops method for syscall
7896 catchpoints. */
7897
7898static void
7899print_one_catch_syscall (struct breakpoint *b,
f1310107 7900 struct bp_location **last_loc)
a96d9b2e 7901{
be5c67c1 7902 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 7903 struct value_print_options opts;
79a45e25 7904 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
7905
7906 get_user_print_options (&opts);
4a64f543
MS
7907 /* Field 4, the address, is omitted (which makes the columns not
7908 line up too nicely with the headers, but the effect is relatively
7909 readable). */
a96d9b2e
SDJ
7910 if (opts.addressprint)
7911 ui_out_field_skip (uiout, "addr");
7912 annotate_field (5);
7913
be5c67c1
PA
7914 if (c->syscalls_to_be_caught
7915 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
7916 ui_out_text (uiout, "syscalls \"");
7917 else
7918 ui_out_text (uiout, "syscall \"");
7919
be5c67c1 7920 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7921 {
7922 int i, iter;
7923 char *text = xstrprintf ("%s", "");
cc59ec59 7924
a96d9b2e 7925 for (i = 0;
be5c67c1 7926 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7927 i++)
7928 {
7929 char *x = text;
7930 struct syscall s;
7931 get_syscall_by_number (iter, &s);
7932
7933 if (s.name != NULL)
7934 text = xstrprintf ("%s%s, ", text, s.name);
7935 else
7936 text = xstrprintf ("%s%d, ", text, iter);
7937
7938 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 7939 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
7940 on every call. */
7941 xfree (x);
7942 }
7943 /* Remove the last comma. */
7944 text[strlen (text) - 2] = '\0';
7945 ui_out_field_string (uiout, "what", text);
7946 }
7947 else
7948 ui_out_field_string (uiout, "what", "<any syscall>");
7949 ui_out_text (uiout, "\" ");
7950}
7951
7952/* Implement the "print_mention" breakpoint_ops method for syscall
7953 catchpoints. */
7954
7955static void
7956print_mention_catch_syscall (struct breakpoint *b)
7957{
be5c67c1
PA
7958 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7959
7960 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7961 {
7962 int i, iter;
7963
be5c67c1 7964 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
7965 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
7966 else
7967 printf_filtered (_("Catchpoint %d (syscall"), b->number);
7968
7969 for (i = 0;
be5c67c1 7970 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7971 i++)
7972 {
7973 struct syscall s;
7974 get_syscall_by_number (iter, &s);
7975
7976 if (s.name)
7977 printf_filtered (" '%s' [%d]", s.name, s.number);
7978 else
7979 printf_filtered (" %d", s.number);
7980 }
7981 printf_filtered (")");
7982 }
7983 else
7984 printf_filtered (_("Catchpoint %d (any syscall)"),
7985 b->number);
7986}
7987
6149aea9
PA
7988/* Implement the "print_recreate" breakpoint_ops method for syscall
7989 catchpoints. */
7990
7991static void
7992print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
7993{
be5c67c1
PA
7994 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7995
6149aea9
PA
7996 fprintf_unfiltered (fp, "catch syscall");
7997
be5c67c1 7998 if (c->syscalls_to_be_caught)
6149aea9
PA
7999 {
8000 int i, iter;
8001
8002 for (i = 0;
be5c67c1 8003 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8004 i++)
8005 {
8006 struct syscall s;
8007
8008 get_syscall_by_number (iter, &s);
8009 if (s.name)
8010 fprintf_unfiltered (fp, " %s", s.name);
8011 else
8012 fprintf_unfiltered (fp, " %d", s.number);
8013 }
8014 }
d9b3f62e 8015 print_recreate_thread (b, fp);
6149aea9
PA
8016}
8017
a96d9b2e
SDJ
8018/* The breakpoint_ops structure to be used in syscall catchpoints. */
8019
2060206e 8020static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8021
8022/* Returns non-zero if 'b' is a syscall catchpoint. */
8023
8024static int
8025syscall_catchpoint_p (struct breakpoint *b)
8026{
8027 return (b->ops == &catch_syscall_breakpoint_ops);
8028}
8029
346774a9
PA
8030/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8031 is non-zero, then make the breakpoint temporary. If COND_STRING is
8032 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8033 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8034
346774a9
PA
8035static void
8036init_catchpoint (struct breakpoint *b,
8037 struct gdbarch *gdbarch, int tempflag,
8038 char *cond_string,
c0a91b2b 8039 const struct breakpoint_ops *ops)
c906108c 8040{
c5aa993b 8041 struct symtab_and_line sal;
346774a9 8042
fe39c653 8043 init_sal (&sal);
6c95b8df 8044 sal.pspace = current_program_space;
c5aa993b 8045
28010a5d 8046 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8047
1b36a34b 8048 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8049 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8050}
8051
28010a5d 8052void
3ea46bff 8053install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8054{
8055 add_to_breakpoint_chain (b);
3a5c3e22
PA
8056 set_breakpoint_number (internal, b);
8057 if (!internal)
8058 mention (b);
c56053d2 8059 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8060
8061 if (update_gll)
8062 update_global_location_list (1);
c56053d2
PA
8063}
8064
9b70b993 8065static void
a6d9a66e
UW
8066create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8067 int tempflag, char *cond_string,
c0a91b2b 8068 const struct breakpoint_ops *ops)
c906108c 8069{
e29a4733 8070 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8071
e29a4733
PA
8072 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8073
8074 c->forked_inferior_pid = null_ptid;
8075
3ea46bff 8076 install_breakpoint (0, &c->base, 1);
c906108c
SS
8077}
8078
fe798b75
JB
8079/* Exec catchpoints. */
8080
b4d90040
PA
8081/* An instance of this type is used to represent an exec catchpoint.
8082 It includes a "struct breakpoint" as a kind of base class; users
8083 downcast to "struct breakpoint *" when needed. A breakpoint is
8084 really of this type iff its ops pointer points to
8085 CATCH_EXEC_BREAKPOINT_OPS. */
8086
8087struct exec_catchpoint
8088{
8089 /* The base class. */
8090 struct breakpoint base;
8091
8092 /* Filename of a program whose exec triggered this catchpoint.
8093 This field is only valid immediately after this catchpoint has
8094 triggered. */
8095 char *exec_pathname;
8096};
8097
8098/* Implement the "dtor" breakpoint_ops method for exec
8099 catchpoints. */
8100
8101static void
8102dtor_catch_exec (struct breakpoint *b)
8103{
8104 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8105
8106 xfree (c->exec_pathname);
348d480f 8107
2060206e 8108 base_breakpoint_ops.dtor (b);
b4d90040
PA
8109}
8110
77b06cd7
TJB
8111static int
8112insert_catch_exec (struct bp_location *bl)
c906108c 8113{
77b06cd7 8114 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 8115}
c906108c 8116
fe798b75 8117static int
77b06cd7 8118remove_catch_exec (struct bp_location *bl)
fe798b75
JB
8119{
8120 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
8121}
c906108c 8122
fe798b75 8123static int
f1310107 8124breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8125 struct address_space *aspace, CORE_ADDR bp_addr,
8126 const struct target_waitstatus *ws)
fe798b75 8127{
b4d90040
PA
8128 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8129
f90263c1
TT
8130 if (ws->kind != TARGET_WAITKIND_EXECD)
8131 return 0;
8132
8133 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8134 return 1;
fe798b75 8135}
c906108c 8136
fe798b75 8137static enum print_stop_action
348d480f 8138print_it_catch_exec (bpstat bs)
fe798b75 8139{
36dfb11c 8140 struct ui_out *uiout = current_uiout;
348d480f 8141 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8142 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8143
fe798b75 8144 annotate_catchpoint (b->number);
36dfb11c
TT
8145 if (b->disposition == disp_del)
8146 ui_out_text (uiout, "\nTemporary catchpoint ");
8147 else
8148 ui_out_text (uiout, "\nCatchpoint ");
8149 if (ui_out_is_mi_like_p (uiout))
8150 {
8151 ui_out_field_string (uiout, "reason",
8152 async_reason_lookup (EXEC_ASYNC_EXEC));
8153 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8154 }
8155 ui_out_field_int (uiout, "bkptno", b->number);
8156 ui_out_text (uiout, " (exec'd ");
8157 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8158 ui_out_text (uiout, "), ");
8159
fe798b75 8160 return PRINT_SRC_AND_LOC;
c906108c
SS
8161}
8162
fe798b75 8163static void
a6d9a66e 8164print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8165{
b4d90040 8166 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8167 struct value_print_options opts;
79a45e25 8168 struct ui_out *uiout = current_uiout;
fe798b75
JB
8169
8170 get_user_print_options (&opts);
8171
8172 /* Field 4, the address, is omitted (which makes the columns
8173 not line up too nicely with the headers, but the effect
8174 is relatively readable). */
8175 if (opts.addressprint)
8176 ui_out_field_skip (uiout, "addr");
8177 annotate_field (5);
8178 ui_out_text (uiout, "exec");
b4d90040 8179 if (c->exec_pathname != NULL)
fe798b75
JB
8180 {
8181 ui_out_text (uiout, ", program \"");
b4d90040 8182 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8183 ui_out_text (uiout, "\" ");
8184 }
8185}
8186
8187static void
8188print_mention_catch_exec (struct breakpoint *b)
8189{
8190 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8191}
8192
6149aea9
PA
8193/* Implement the "print_recreate" breakpoint_ops method for exec
8194 catchpoints. */
8195
8196static void
8197print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8198{
8199 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8200 print_recreate_thread (b, fp);
6149aea9
PA
8201}
8202
2060206e 8203static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8204
a96d9b2e
SDJ
8205static void
8206create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 8207 const struct breakpoint_ops *ops)
a96d9b2e 8208{
be5c67c1 8209 struct syscall_catchpoint *c;
a96d9b2e 8210 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 8211
be5c67c1
PA
8212 c = XNEW (struct syscall_catchpoint);
8213 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8214 c->syscalls_to_be_caught = filter;
a96d9b2e 8215
3ea46bff 8216 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
8217}
8218
c906108c 8219static int
fba45db2 8220hw_breakpoint_used_count (void)
c906108c 8221{
c906108c 8222 int i = 0;
f1310107
TJB
8223 struct breakpoint *b;
8224 struct bp_location *bl;
c906108c
SS
8225
8226 ALL_BREAKPOINTS (b)
c5aa993b 8227 {
d6b74ac4 8228 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8229 for (bl = b->loc; bl; bl = bl->next)
8230 {
8231 /* Special types of hardware breakpoints may use more than
8232 one register. */
348d480f 8233 i += b->ops->resources_needed (bl);
f1310107 8234 }
c5aa993b 8235 }
c906108c
SS
8236
8237 return i;
8238}
8239
a1398e0c
PA
8240/* Returns the resources B would use if it were a hardware
8241 watchpoint. */
8242
c906108c 8243static int
a1398e0c 8244hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8245{
c906108c 8246 int i = 0;
e09342b5 8247 struct bp_location *bl;
c906108c 8248
a1398e0c
PA
8249 if (!breakpoint_enabled (b))
8250 return 0;
8251
8252 for (bl = b->loc; bl; bl = bl->next)
8253 {
8254 /* Special types of hardware watchpoints may use more than
8255 one register. */
8256 i += b->ops->resources_needed (bl);
8257 }
8258
8259 return i;
8260}
8261
8262/* Returns the sum the used resources of all hardware watchpoints of
8263 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8264 the sum of the used resources of all hardware watchpoints of other
8265 types _not_ TYPE. */
8266
8267static int
8268hw_watchpoint_used_count_others (struct breakpoint *except,
8269 enum bptype type, int *other_type_used)
8270{
8271 int i = 0;
8272 struct breakpoint *b;
8273
c906108c
SS
8274 *other_type_used = 0;
8275 ALL_BREAKPOINTS (b)
e09342b5 8276 {
a1398e0c
PA
8277 if (b == except)
8278 continue;
e09342b5
TJB
8279 if (!breakpoint_enabled (b))
8280 continue;
8281
a1398e0c
PA
8282 if (b->type == type)
8283 i += hw_watchpoint_use_count (b);
8284 else if (is_hardware_watchpoint (b))
8285 *other_type_used = 1;
e09342b5
TJB
8286 }
8287
c906108c
SS
8288 return i;
8289}
8290
c906108c 8291void
fba45db2 8292disable_watchpoints_before_interactive_call_start (void)
c906108c 8293{
c5aa993b 8294 struct breakpoint *b;
c906108c
SS
8295
8296 ALL_BREAKPOINTS (b)
c5aa993b 8297 {
cc60f2e3 8298 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8299 {
b5de0fa7 8300 b->enable_state = bp_call_disabled;
b60e7edf 8301 update_global_location_list (0);
c5aa993b
JM
8302 }
8303 }
c906108c
SS
8304}
8305
8306void
fba45db2 8307enable_watchpoints_after_interactive_call_stop (void)
c906108c 8308{
c5aa993b 8309 struct breakpoint *b;
c906108c
SS
8310
8311 ALL_BREAKPOINTS (b)
c5aa993b 8312 {
cc60f2e3 8313 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8314 {
b5de0fa7 8315 b->enable_state = bp_enabled;
b60e7edf 8316 update_global_location_list (1);
c5aa993b
JM
8317 }
8318 }
c906108c
SS
8319}
8320
8bea4e01
UW
8321void
8322disable_breakpoints_before_startup (void)
8323{
6c95b8df 8324 current_program_space->executing_startup = 1;
f8eba3c6 8325 update_global_location_list (0);
8bea4e01
UW
8326}
8327
8328void
8329enable_breakpoints_after_startup (void)
8330{
6c95b8df 8331 current_program_space->executing_startup = 0;
f8eba3c6 8332 breakpoint_re_set ();
8bea4e01
UW
8333}
8334
c906108c
SS
8335
8336/* Set a breakpoint that will evaporate an end of command
8337 at address specified by SAL.
8338 Restrict it to frame FRAME if FRAME is nonzero. */
8339
8340struct breakpoint *
a6d9a66e
UW
8341set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8342 struct frame_id frame_id, enum bptype type)
c906108c 8343{
52f0bd74 8344 struct breakpoint *b;
edb3359d
DJ
8345
8346 /* If FRAME_ID is valid, it should be a real frame, not an inlined
8347 one. */
8348 gdb_assert (!frame_id_inlined_p (frame_id));
8349
06edf0c0 8350 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8351 b->enable_state = bp_enabled;
8352 b->disposition = disp_donttouch;
818dd999 8353 b->frame_id = frame_id;
c906108c 8354
4a64f543
MS
8355 /* If we're debugging a multi-threaded program, then we want
8356 momentary breakpoints to be active in only a single thread of
8357 control. */
39f77062
KB
8358 if (in_thread_list (inferior_ptid))
8359 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8360
b60e7edf 8361 update_global_location_list_nothrow (1);
74960c60 8362
c906108c
SS
8363 return b;
8364}
611c83ae 8365
06edf0c0
PA
8366/* Make a momentary breakpoint based on the master breakpoint ORIG.
8367 The new breakpoint will have type TYPE, and use OPS as it
8368 breakpoint_ops. */
e58b0e63 8369
06edf0c0
PA
8370static struct breakpoint *
8371momentary_breakpoint_from_master (struct breakpoint *orig,
8372 enum bptype type,
c0a91b2b 8373 const struct breakpoint_ops *ops)
e58b0e63
PA
8374{
8375 struct breakpoint *copy;
8376
06edf0c0 8377 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8378 copy->loc = allocate_bp_location (copy);
0e30163f 8379 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8380
a6d9a66e 8381 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8382 copy->loc->requested_address = orig->loc->requested_address;
8383 copy->loc->address = orig->loc->address;
8384 copy->loc->section = orig->loc->section;
6c95b8df 8385 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8386 copy->loc->probe = orig->loc->probe;
e58b0e63 8387
f8eba3c6
TT
8388 if (orig->loc->source_file != NULL)
8389 copy->loc->source_file = xstrdup (orig->loc->source_file);
e58b0e63 8390
f8eba3c6 8391 copy->loc->line_number = orig->loc->line_number;
e58b0e63
PA
8392 copy->frame_id = orig->frame_id;
8393 copy->thread = orig->thread;
6c95b8df 8394 copy->pspace = orig->pspace;
e58b0e63
PA
8395
8396 copy->enable_state = bp_enabled;
8397 copy->disposition = disp_donttouch;
8398 copy->number = internal_breakpoint_number--;
8399
8400 update_global_location_list_nothrow (0);
8401 return copy;
8402}
8403
06edf0c0
PA
8404/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8405 ORIG is NULL. */
8406
8407struct breakpoint *
8408clone_momentary_breakpoint (struct breakpoint *orig)
8409{
8410 /* If there's nothing to clone, then return nothing. */
8411 if (orig == NULL)
8412 return NULL;
8413
8414 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8415}
8416
611c83ae 8417struct breakpoint *
a6d9a66e
UW
8418set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8419 enum bptype type)
611c83ae
PA
8420{
8421 struct symtab_and_line sal;
8422
8423 sal = find_pc_line (pc, 0);
8424 sal.pc = pc;
8425 sal.section = find_pc_overlay (pc);
8426 sal.explicit_pc = 1;
8427
a6d9a66e 8428 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8429}
c906108c 8430\f
c5aa993b 8431
c906108c
SS
8432/* Tell the user we have just set a breakpoint B. */
8433
8434static void
fba45db2 8435mention (struct breakpoint *b)
c906108c 8436{
348d480f 8437 b->ops->print_mention (b);
79a45e25 8438 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8439 return;
c906108c
SS
8440 printf_filtered ("\n");
8441}
c906108c 8442\f
c5aa993b 8443
0d381245 8444static struct bp_location *
39d61571 8445add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8446 const struct symtab_and_line *sal)
8447{
8448 struct bp_location *loc, **tmp;
3742cc8b
YQ
8449 CORE_ADDR adjusted_address;
8450 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8451
8452 if (loc_gdbarch == NULL)
8453 loc_gdbarch = b->gdbarch;
8454
8455 /* Adjust the breakpoint's address prior to allocating a location.
8456 Once we call allocate_bp_location(), that mostly uninitialized
8457 location will be placed on the location chain. Adjustment of the
8458 breakpoint may cause target_read_memory() to be called and we do
8459 not want its scan of the location chain to find a breakpoint and
8460 location that's only been partially initialized. */
8461 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8462 sal->pc, b->type);
0d381245 8463
39d61571 8464 loc = allocate_bp_location (b);
0d381245
VP
8465 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
8466 ;
8467 *tmp = loc;
3742cc8b 8468
0d381245 8469 loc->requested_address = sal->pc;
3742cc8b 8470 loc->address = adjusted_address;
6c95b8df 8471 loc->pspace = sal->pspace;
55aa24fb 8472 loc->probe = sal->probe;
6c95b8df 8473 gdb_assert (loc->pspace != NULL);
0d381245 8474 loc->section = sal->section;
3742cc8b 8475 loc->gdbarch = loc_gdbarch;
f8eba3c6
TT
8476
8477 if (sal->symtab != NULL)
8478 loc->source_file = xstrdup (sal->symtab->filename);
8479 loc->line_number = sal->line;
8480
0e30163f
JK
8481 set_breakpoint_location_function (loc,
8482 sal->explicit_pc || sal->explicit_line);
0d381245
VP
8483 return loc;
8484}
514f746b
AR
8485\f
8486
8487/* Return 1 if LOC is pointing to a permanent breakpoint,
8488 return 0 otherwise. */
8489
8490static int
8491bp_loc_is_permanent (struct bp_location *loc)
8492{
8493 int len;
8494 CORE_ADDR addr;
1afeeb75 8495 const gdb_byte *bpoint;
514f746b 8496 gdb_byte *target_mem;
939c61fa
JK
8497 struct cleanup *cleanup;
8498 int retval = 0;
514f746b
AR
8499
8500 gdb_assert (loc != NULL);
8501
8502 addr = loc->address;
1afeeb75 8503 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 8504
939c61fa 8505 /* Software breakpoints unsupported? */
1afeeb75 8506 if (bpoint == NULL)
939c61fa
JK
8507 return 0;
8508
514f746b
AR
8509 target_mem = alloca (len);
8510
939c61fa
JK
8511 /* Enable the automatic memory restoration from breakpoints while
8512 we read the memory. Otherwise we could say about our temporary
8513 breakpoints they are permanent. */
6c95b8df
PA
8514 cleanup = save_current_space_and_thread ();
8515
8516 switch_to_program_space_and_thread (loc->pspace);
8517 make_show_memory_breakpoints_cleanup (0);
939c61fa 8518
514f746b 8519 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 8520 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 8521 retval = 1;
514f746b 8522
939c61fa
JK
8523 do_cleanups (cleanup);
8524
8525 return retval;
514f746b
AR
8526}
8527
e7e0cddf
SS
8528/* The style in which to perform a dynamic printf. This is a user
8529 option because different output options have different tradeoffs;
8530 if GDB does the printing, there is better error handling if there
8531 is a problem with any of the arguments, but using an inferior
8532 function lets you have special-purpose printers and sending of
8533 output to the same place as compiled-in print functions. (Future
8534 styles may include the ability to do a target-side printf.) */
8535
8536static const char dprintf_style_gdb[] = "gdb";
8537static const char dprintf_style_call[] = "call";
8538static const char *const dprintf_style_enums[] = {
8539 dprintf_style_gdb,
8540 dprintf_style_call,
8541 NULL
8542};
8543static const char *dprintf_style = dprintf_style_gdb;
8544
8545/* The function to use for dynamic printf if the preferred style is to
8546 call into the inferior. The value is simply a string that is
8547 copied into the command, so it can be anything that GDB can
8548 evaluate to a callable address, not necessarily a function name. */
8549
8550static char *dprintf_function = "";
8551
8552/* The channel to use for dynamic printf if the preferred style is to
8553 call into the inferior; if a nonempty string, it will be passed to
8554 the call as the first argument, with the format string as the
8555 second. As with the dprintf function, this can be anything that
8556 GDB knows how to evaluate, so in addition to common choices like
8557 "stderr", this could be an app-specific expression like
8558 "mystreams[curlogger]". */
514f746b 8559
e7e0cddf
SS
8560static char *dprintf_channel = "";
8561
8562/* Build a command list for the dprintf corresponding to the current
8563 settings of the dprintf style options. */
8564
8565static void
8566update_dprintf_command_list (struct breakpoint *b)
8567{
8568 char *dprintf_args = b->extra_string;
8569 char *printf_line = NULL;
8570
8571 if (!dprintf_args)
8572 return;
8573
8574 dprintf_args = skip_spaces (dprintf_args);
8575
8576 /* Allow a comma, as it may have terminated a location, but don't
8577 insist on it. */
8578 if (*dprintf_args == ',')
8579 ++dprintf_args;
8580 dprintf_args = skip_spaces (dprintf_args);
8581
8582 if (*dprintf_args != '"')
8583 error (_("Bad format string, missing '\"'."));
8584
8585 if (strcmp (dprintf_style, "gdb") == 0)
8586 printf_line = xstrprintf ("printf %s", dprintf_args);
8587 else if (strcmp (dprintf_style, "call") == 0)
8588 {
8589 if (!dprintf_function)
8590 error (_("No function supplied for dprintf call"));
8591
8592 if (dprintf_channel && strlen (dprintf_channel) > 0)
8593 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8594 dprintf_function,
8595 dprintf_channel,
8596 dprintf_args);
8597 else
8598 printf_line = xstrprintf ("call (void) %s (%s)",
8599 dprintf_function,
8600 dprintf_args);
8601 }
8602 else
8603 internal_error (__FILE__, __LINE__,
8604 _("Invalid dprintf style."));
8605
8606 /* Manufacture a printf/continue sequence. */
8607 if (printf_line)
8608 {
8609 struct command_line *printf_cmd_line, *cont_cmd_line = NULL;
8610
8611 cont_cmd_line = xmalloc (sizeof (struct command_line));
8612 cont_cmd_line->control_type = simple_control;
8613 cont_cmd_line->body_count = 0;
8614 cont_cmd_line->body_list = NULL;
8615 cont_cmd_line->next = NULL;
8616 cont_cmd_line->line = xstrdup ("continue");
8617
8618 printf_cmd_line = xmalloc (sizeof (struct command_line));
8619 printf_cmd_line->control_type = simple_control;
8620 printf_cmd_line->body_count = 0;
8621 printf_cmd_line->body_list = NULL;
8622 printf_cmd_line->next = cont_cmd_line;
8623 printf_cmd_line->line = printf_line;
8624
8625 breakpoint_set_commands (b, printf_cmd_line);
8626 }
8627}
8628
8629/* Update all dprintf commands, making their command lists reflect
8630 current style settings. */
8631
8632static void
8633update_dprintf_commands (char *args, int from_tty,
8634 struct cmd_list_element *c)
8635{
8636 struct breakpoint *b;
8637
8638 ALL_BREAKPOINTS (b)
8639 {
8640 if (b->type == bp_dprintf)
8641 update_dprintf_command_list (b);
8642 }
8643}
c3f6f71d 8644
018d34a4
VP
8645/* Create a breakpoint with SAL as location. Use ADDR_STRING
8646 as textual description of the location, and COND_STRING
db107f19 8647 as condition expression. */
018d34a4
VP
8648
8649static void
d9b3f62e
PA
8650init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8651 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 8652 char *filter, char *cond_string,
e7e0cddf 8653 char *extra_string,
d9b3f62e
PA
8654 enum bptype type, enum bpdisp disposition,
8655 int thread, int task, int ignore_count,
c0a91b2b 8656 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8657 int enabled, int internal, unsigned flags,
8658 int display_canonical)
018d34a4 8659{
0d381245 8660 int i;
018d34a4
VP
8661
8662 if (type == bp_hardware_breakpoint)
8663 {
fbbd034e
AS
8664 int target_resources_ok;
8665
8666 i = hw_breakpoint_used_count ();
8667 target_resources_ok =
8668 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8669 i + 1, 0);
8670 if (target_resources_ok == 0)
8671 error (_("No hardware breakpoint support in the target."));
8672 else if (target_resources_ok < 0)
8673 error (_("Hardware breakpoints used exceeds limit."));
8674 }
8675
6c95b8df
PA
8676 gdb_assert (sals.nelts > 0);
8677
0d381245
VP
8678 for (i = 0; i < sals.nelts; ++i)
8679 {
8680 struct symtab_and_line sal = sals.sals[i];
8681 struct bp_location *loc;
8682
8683 if (from_tty)
5af949e3
UW
8684 {
8685 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8686 if (!loc_gdbarch)
8687 loc_gdbarch = gdbarch;
8688
8689 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8690 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8691 }
0d381245
VP
8692
8693 if (i == 0)
8694 {
d9b3f62e 8695 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8696 b->thread = thread;
4a306c9a 8697 b->task = task;
855a6e68 8698
0d381245 8699 b->cond_string = cond_string;
e7e0cddf 8700 b->extra_string = extra_string;
0d381245 8701 b->ignore_count = ignore_count;
41447f92 8702 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8703 b->disposition = disposition;
6c95b8df 8704
44f238bb
PA
8705 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8706 b->loc->inserted = 1;
8707
0fb4aa4b
PA
8708 if (type == bp_static_tracepoint)
8709 {
d9b3f62e 8710 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8711 struct static_tracepoint_marker marker;
8712
983af33b 8713 if (strace_marker_p (b))
0fb4aa4b
PA
8714 {
8715 /* We already know the marker exists, otherwise, we
8716 wouldn't see a sal for it. */
8717 char *p = &addr_string[3];
8718 char *endp;
8719 char *marker_str;
0fb4aa4b 8720
e9cafbcc 8721 p = skip_spaces (p);
0fb4aa4b 8722
e9cafbcc 8723 endp = skip_to_space (p);
0fb4aa4b
PA
8724
8725 marker_str = savestring (p, endp - p);
d9b3f62e 8726 t->static_trace_marker_id = marker_str;
0fb4aa4b 8727
3e43a32a
MS
8728 printf_filtered (_("Probed static tracepoint "
8729 "marker \"%s\"\n"),
d9b3f62e 8730 t->static_trace_marker_id);
0fb4aa4b
PA
8731 }
8732 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8733 {
d9b3f62e 8734 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
8735 release_static_tracepoint_marker (&marker);
8736
3e43a32a
MS
8737 printf_filtered (_("Probed static tracepoint "
8738 "marker \"%s\"\n"),
d9b3f62e 8739 t->static_trace_marker_id);
0fb4aa4b
PA
8740 }
8741 else
3e43a32a
MS
8742 warning (_("Couldn't determine the static "
8743 "tracepoint marker to probe"));
0fb4aa4b
PA
8744 }
8745
0d381245
VP
8746 loc = b->loc;
8747 }
8748 else
018d34a4 8749 {
39d61571 8750 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8751 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8752 loc->inserted = 1;
0d381245
VP
8753 }
8754
514f746b
AR
8755 if (bp_loc_is_permanent (loc))
8756 make_breakpoint_permanent (b);
8757
0d381245
VP
8758 if (b->cond_string)
8759 {
8760 char *arg = b->cond_string;
1bb9788d
TT
8761 loc->cond = parse_exp_1 (&arg, loc->address,
8762 block_for_pc (loc->address), 0);
0d381245 8763 if (*arg)
588ae58c 8764 error (_("Garbage '%s' follows condition"), arg);
018d34a4 8765 }
e7e0cddf
SS
8766
8767 /* Dynamic printf requires and uses additional arguments on the
8768 command line, otherwise it's an error. */
8769 if (type == bp_dprintf)
8770 {
8771 if (b->extra_string)
8772 update_dprintf_command_list (b);
8773 else
8774 error (_("Format string required"));
8775 }
8776 else if (b->extra_string)
588ae58c 8777 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8778 }
018d34a4 8779
56435ebe 8780 b->display_canonical = display_canonical;
018d34a4
VP
8781 if (addr_string)
8782 b->addr_string = addr_string;
8783 else
8784 /* addr_string has to be used or breakpoint_re_set will delete
8785 me. */
5af949e3
UW
8786 b->addr_string
8787 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 8788 b->filter = filter;
d9b3f62e 8789}
018d34a4 8790
d9b3f62e
PA
8791static void
8792create_breakpoint_sal (struct gdbarch *gdbarch,
8793 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 8794 char *filter, char *cond_string,
e7e0cddf 8795 char *extra_string,
d9b3f62e
PA
8796 enum bptype type, enum bpdisp disposition,
8797 int thread, int task, int ignore_count,
c0a91b2b 8798 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8799 int enabled, int internal, unsigned flags,
8800 int display_canonical)
d9b3f62e
PA
8801{
8802 struct breakpoint *b;
8803 struct cleanup *old_chain;
8804
8805 if (is_tracepoint_type (type))
8806 {
8807 struct tracepoint *t;
8808
8809 t = XCNEW (struct tracepoint);
8810 b = &t->base;
8811 }
8812 else
8813 b = XNEW (struct breakpoint);
8814
8815 old_chain = make_cleanup (xfree, b);
8816
8817 init_breakpoint_sal (b, gdbarch,
8818 sals, addr_string,
e7e0cddf 8819 filter, cond_string, extra_string,
d9b3f62e
PA
8820 type, disposition,
8821 thread, task, ignore_count,
8822 ops, from_tty,
44f238bb
PA
8823 enabled, internal, flags,
8824 display_canonical);
d9b3f62e
PA
8825 discard_cleanups (old_chain);
8826
3ea46bff 8827 install_breakpoint (internal, b, 0);
018d34a4
VP
8828}
8829
8830/* Add SALS.nelts breakpoints to the breakpoint table. For each
8831 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8832 value. COND_STRING, if not NULL, specified the condition to be
8833 used for all breakpoints. Essentially the only case where
8834 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8835 function. In that case, it's still not possible to specify
8836 separate conditions for different overloaded functions, so
8837 we take just a single condition string.
8838
c3f6f71d 8839 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8840 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8841 array contents). If the function fails (error() is called), the
8842 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8843 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8844
8845static void
8cdf0e15 8846create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8847 struct linespec_result *canonical,
e7e0cddf 8848 char *cond_string, char *extra_string,
8cdf0e15
VP
8849 enum bptype type, enum bpdisp disposition,
8850 int thread, int task, int ignore_count,
c0a91b2b 8851 const struct breakpoint_ops *ops, int from_tty,
44f238bb 8852 int enabled, int internal, unsigned flags)
c906108c 8853{
018d34a4 8854 int i;
f8eba3c6 8855 struct linespec_sals *lsal;
cc59ec59 8856
f8eba3c6
TT
8857 if (canonical->pre_expanded)
8858 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
8859
8860 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 8861 {
f8eba3c6
TT
8862 /* Note that 'addr_string' can be NULL in the case of a plain
8863 'break', without arguments. */
8864 char *addr_string = (canonical->addr_string
8865 ? xstrdup (canonical->addr_string)
8866 : NULL);
8867 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
8868 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 8869
f8eba3c6
TT
8870 make_cleanup (xfree, filter_string);
8871 create_breakpoint_sal (gdbarch, lsal->sals,
8872 addr_string,
8873 filter_string,
e7e0cddf
SS
8874 cond_string, extra_string,
8875 type, disposition,
84f4c1fe 8876 thread, task, ignore_count, ops,
44f238bb 8877 from_tty, enabled, internal, flags,
56435ebe 8878 canonical->special_display);
f8eba3c6 8879 discard_cleanups (inner);
c3f6f71d 8880 }
c3f6f71d 8881}
c906108c 8882
9998af43 8883/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 8884 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 8885 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
8886 address strings. ADDRESS points to the end of the SAL.
8887
8888 The array and the line spec strings are allocated on the heap, it is
8889 the caller's responsibility to free them. */
c906108c 8890
b9362cc7 8891static void
c3f6f71d 8892parse_breakpoint_sals (char **address,
58438ac1 8893 struct linespec_result *canonical)
c3f6f71d
JM
8894{
8895 char *addr_start = *address;
cc59ec59 8896
c3f6f71d 8897 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 8898 breakpoint. */
c3f6f71d
JM
8899 if ((*address) == NULL
8900 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 8901 {
1bfeeb0f
JL
8902 /* The last displayed codepoint, if it's valid, is our default breakpoint
8903 address. */
8904 if (last_displayed_sal_is_valid ())
c906108c 8905 {
f8eba3c6 8906 struct linespec_sals lsal;
c3f6f71d 8907 struct symtab_and_line sal;
cc59ec59 8908
4a64f543 8909 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 8910 lsal.sals.sals = (struct symtab_and_line *)
c906108c 8911 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
8912
8913 /* Set sal's pspace, pc, symtab, and line to the values
8914 corresponding to the last call to print_frame_info. */
8915 get_last_displayed_sal (&sal);
8916 sal.section = find_pc_overlay (sal.pc);
00903456 8917
4a64f543 8918 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
8919 where PC is the last displayed codepoint's address. So
8920 make sure to set sal.explicit_pc to prevent GDB from
8921 trying to expand the list of sals to include all other
8922 instances with the same symtab and line. */
00903456
JK
8923 sal.explicit_pc = 1;
8924
f8eba3c6
TT
8925 lsal.sals.sals[0] = sal;
8926 lsal.sals.nelts = 1;
8927 lsal.canonical = NULL;
8928
8929 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
8930 }
8931 else
8a3fe4f8 8932 error (_("No default breakpoint address now."));
c906108c
SS
8933 }
8934 else
8935 {
cc80f267
JK
8936 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
8937
c906108c 8938 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
8939 current_source_symtab (which is decode_line_1's default).
8940 This should produce the results we want almost all of the
cc80f267
JK
8941 time while leaving default_breakpoint_* alone.
8942
8943 ObjC: However, don't match an Objective-C method name which
8944 may have a '+' or '-' succeeded by a '['. */
8945 if (last_displayed_sal_is_valid ()
8946 && (!cursal.symtab
8947 || ((strchr ("+-", (*address)[0]) != NULL)
8948 && ((*address)[1] != '['))))
f8eba3c6
TT
8949 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8950 get_last_displayed_symtab (),
8951 get_last_displayed_line (),
8952 canonical, NULL, NULL);
c906108c 8953 else
f8eba3c6 8954 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 8955 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 8956 }
c3f6f71d 8957}
c906108c 8958
c906108c 8959
c3f6f71d 8960/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 8961 inserted as a breakpoint. If it can't throw an error. */
c906108c 8962
b9362cc7 8963static void
23e7acfb 8964breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
8965{
8966 int i;
cc59ec59 8967
c3f6f71d 8968 for (i = 0; i < sals->nelts; i++)
ee53e872 8969 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
8970}
8971
7a697b8d
SS
8972/* Fast tracepoints may have restrictions on valid locations. For
8973 instance, a fast tracepoint using a jump instead of a trap will
8974 likely have to overwrite more bytes than a trap would, and so can
8975 only be placed where the instruction is longer than the jump, or a
8976 multi-instruction sequence does not have a jump into the middle of
8977 it, etc. */
8978
8979static void
8980check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8981 struct symtabs_and_lines *sals)
8982{
8983 int i, rslt;
8984 struct symtab_and_line *sal;
8985 char *msg;
8986 struct cleanup *old_chain;
8987
8988 for (i = 0; i < sals->nelts; i++)
8989 {
f8eba3c6
TT
8990 struct gdbarch *sarch;
8991
7a697b8d
SS
8992 sal = &sals->sals[i];
8993
f8eba3c6
TT
8994 sarch = get_sal_arch (*sal);
8995 /* We fall back to GDBARCH if there is no architecture
8996 associated with SAL. */
8997 if (sarch == NULL)
8998 sarch = gdbarch;
8999 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9000 NULL, &msg);
9001 old_chain = make_cleanup (xfree, msg);
9002
9003 if (!rslt)
9004 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9005 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9006
9007 do_cleanups (old_chain);
9008 }
9009}
9010
018d34a4
VP
9011/* Given TOK, a string specification of condition and thread, as
9012 accepted by the 'break' command, extract the condition
9013 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9014 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9015 If no condition is found, *COND_STRING is set to NULL.
9016 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9017
9018static void
9019find_condition_and_thread (char *tok, CORE_ADDR pc,
e7e0cddf
SS
9020 char **cond_string, int *thread, int *task,
9021 char **rest)
018d34a4
VP
9022{
9023 *cond_string = NULL;
9024 *thread = -1;
9025 while (tok && *tok)
9026 {
9027 char *end_tok;
9028 int toklen;
9029 char *cond_start = NULL;
9030 char *cond_end = NULL;
cc59ec59 9031
e9cafbcc 9032 tok = skip_spaces (tok);
e7e0cddf
SS
9033
9034 if ((*tok == '"' || *tok == ',') && rest)
9035 {
9036 *rest = savestring (tok, strlen (tok));
9037 return;
9038 }
9039
e9cafbcc 9040 end_tok = skip_to_space (tok);
d634f2de 9041
018d34a4 9042 toklen = end_tok - tok;
d634f2de 9043
018d34a4
VP
9044 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9045 {
f7545552
TT
9046 struct expression *expr;
9047
018d34a4 9048 tok = cond_start = end_tok + 1;
1bb9788d 9049 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9050 xfree (expr);
018d34a4 9051 cond_end = tok;
d634f2de 9052 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9053 }
9054 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9055 {
9056 char *tmptok;
d634f2de 9057
018d34a4
VP
9058 tok = end_tok + 1;
9059 tmptok = tok;
9060 *thread = strtol (tok, &tok, 0);
9061 if (tok == tmptok)
9062 error (_("Junk after thread keyword."));
9063 if (!valid_thread_id (*thread))
9064 error (_("Unknown thread %d."), *thread);
9065 }
4a306c9a
JB
9066 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9067 {
9068 char *tmptok;
9069
9070 tok = end_tok + 1;
9071 tmptok = tok;
9072 *task = strtol (tok, &tok, 0);
9073 if (tok == tmptok)
9074 error (_("Junk after task keyword."));
9075 if (!valid_task_id (*task))
b6199126 9076 error (_("Unknown task %d."), *task);
4a306c9a 9077 }
e7e0cddf
SS
9078 else if (rest)
9079 {
9080 *rest = savestring (tok, strlen (tok));
ccab2054 9081 return;
e7e0cddf 9082 }
018d34a4
VP
9083 else
9084 error (_("Junk at end of arguments."));
9085 }
9086}
9087
0fb4aa4b
PA
9088/* Decode a static tracepoint marker spec. */
9089
9090static struct symtabs_and_lines
9091decode_static_tracepoint_spec (char **arg_p)
9092{
9093 VEC(static_tracepoint_marker_p) *markers = NULL;
9094 struct symtabs_and_lines sals;
0fb4aa4b
PA
9095 struct cleanup *old_chain;
9096 char *p = &(*arg_p)[3];
9097 char *endp;
9098 char *marker_str;
9099 int i;
9100
e9cafbcc 9101 p = skip_spaces (p);
0fb4aa4b 9102
e9cafbcc 9103 endp = skip_to_space (p);
0fb4aa4b
PA
9104
9105 marker_str = savestring (p, endp - p);
9106 old_chain = make_cleanup (xfree, marker_str);
9107
9108 markers = target_static_tracepoint_markers_by_strid (marker_str);
9109 if (VEC_empty(static_tracepoint_marker_p, markers))
9110 error (_("No known static tracepoint marker named %s"), marker_str);
9111
9112 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9113 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9114
9115 for (i = 0; i < sals.nelts; i++)
9116 {
9117 struct static_tracepoint_marker *marker;
9118
9119 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9120
9121 init_sal (&sals.sals[i]);
9122
9123 sals.sals[i] = find_pc_line (marker->address, 0);
9124 sals.sals[i].pc = marker->address;
9125
9126 release_static_tracepoint_marker (marker);
9127 }
9128
9129 do_cleanups (old_chain);
9130
9131 *arg_p = endp;
9132 return sals;
9133}
9134
fd9b8c24
PA
9135/* Set a breakpoint. This function is shared between CLI and MI
9136 functions for setting a breakpoint. This function has two major
9137 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
9138 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
9139 breakpoint location, address and thread. Otherwise, ARG is just
9140 the location of breakpoint, with condition and thread specified by
9141 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
9142 the breakpoint number will be allocated from the internal
9143 breakpoint count. Returns true if any breakpoint was created;
9144 false otherwise. */
0101ce28 9145
8cdf0e15
VP
9146int
9147create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9148 char *arg, char *cond_string,
9149 int thread, char *extra_string,
8cdf0e15 9150 int parse_condition_and_thread,
0fb4aa4b 9151 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9152 int ignore_count,
9153 enum auto_boolean pending_break_support,
c0a91b2b 9154 const struct breakpoint_ops *ops,
44f238bb
PA
9155 int from_tty, int enabled, int internal,
9156 unsigned flags)
c3f6f71d 9157{
b78a6381 9158 volatile struct gdb_exception e;
f8eba3c6 9159 char *copy_arg = NULL;
c3f6f71d 9160 char *addr_start = arg;
7efd8fc2 9161 struct linespec_result canonical;
c3f6f71d 9162 struct cleanup *old_chain;
80c99de1 9163 struct cleanup *bkpt_chain = NULL;
0101ce28 9164 int pending = 0;
4a306c9a 9165 int task = 0;
86b17b60 9166 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9167
348d480f
PA
9168 gdb_assert (ops != NULL);
9169
7efd8fc2 9170 init_linespec_result (&canonical);
c3f6f71d 9171
b78a6381
TT
9172 TRY_CATCH (e, RETURN_MASK_ALL)
9173 {
983af33b
SDJ
9174 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9175 addr_start, &copy_arg);
b78a6381 9176 }
0101ce28
JJ
9177
9178 /* If caller is interested in rc value from parse, set value. */
05ff989b 9179 switch (e.reason)
0101ce28 9180 {
983af33b
SDJ
9181 case GDB_NO_ERROR:
9182 if (VEC_empty (linespec_sals, canonical.sals))
9183 return 0;
9184 break;
05ff989b
AC
9185 case RETURN_ERROR:
9186 switch (e.error)
0101ce28 9187 {
05ff989b 9188 case NOT_FOUND_ERROR:
0101ce28 9189
05ff989b
AC
9190 /* If pending breakpoint support is turned off, throw
9191 error. */
fa8d40ab
JJ
9192
9193 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9194 throw_exception (e);
9195
9196 exception_print (gdb_stderr, e);
fa8d40ab 9197
05ff989b
AC
9198 /* If pending breakpoint support is auto query and the user
9199 selects no, then simply return the error code. */
059fb39f 9200 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9201 && !nquery (_("Make %s pending on future shared library load? "),
9202 bptype_string (type_wanted)))
fd9b8c24 9203 return 0;
fa8d40ab 9204
05ff989b
AC
9205 /* At this point, either the user was queried about setting
9206 a pending breakpoint and selected yes, or pending
9207 breakpoint behavior is on and thus a pending breakpoint
9208 is defaulted on behalf of the user. */
f8eba3c6
TT
9209 {
9210 struct linespec_sals lsal;
9211
9212 copy_arg = xstrdup (addr_start);
9213 lsal.canonical = xstrdup (copy_arg);
9214 lsal.sals.nelts = 1;
9215 lsal.sals.sals = XNEW (struct symtab_and_line);
9216 init_sal (&lsal.sals.sals[0]);
9217 pending = 1;
9218 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9219 }
05ff989b
AC
9220 break;
9221 default:
98deb0da 9222 throw_exception (e);
0101ce28 9223 }
2abae994 9224 break;
05ff989b 9225 default:
983af33b 9226 throw_exception (e);
0101ce28 9227 }
c3f6f71d 9228
4a64f543 9229 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9230 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9231
c3f6f71d
JM
9232 /* ----------------------------- SNIP -----------------------------
9233 Anything added to the cleanup chain beyond this point is assumed
9234 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9235 then the memory is not reclaimed. */
9236 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9237
c3f6f71d
JM
9238 /* Resolve all line numbers to PC's and verify that the addresses
9239 are ok for the target. */
0101ce28 9240 if (!pending)
f8eba3c6
TT
9241 {
9242 int ix;
9243 struct linespec_sals *iter;
9244
9245 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9246 breakpoint_sals_to_pc (&iter->sals);
9247 }
c3f6f71d 9248
7a697b8d 9249 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9250 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9251 {
9252 int ix;
9253 struct linespec_sals *iter;
9254
9255 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9256 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9257 }
7a697b8d 9258
c3f6f71d
JM
9259 /* Verify that condition can be parsed, before setting any
9260 breakpoints. Allocate a separate condition expression for each
4a64f543 9261 breakpoint. */
0101ce28 9262 if (!pending)
c3f6f71d 9263 {
f8eba3c6
TT
9264 struct linespec_sals *lsal;
9265
9266 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9267
2f069f6f 9268 if (parse_condition_and_thread)
72b2ff0e 9269 {
e7e0cddf 9270 char *rest;
72b2ff0e
VP
9271 /* Here we only parse 'arg' to separate condition
9272 from thread number, so parsing in context of first
9273 sal is OK. When setting the breakpoint we'll
9274 re-parse it in context of each sal. */
9275 cond_string = NULL;
9276 thread = -1;
e7e0cddf 9277 rest = NULL;
f8eba3c6 9278 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
e7e0cddf 9279 &thread, &task, &rest);
72b2ff0e
VP
9280 if (cond_string)
9281 make_cleanup (xfree, cond_string);
e7e0cddf
SS
9282 if (rest)
9283 make_cleanup (xfree, rest);
9284 if (rest)
9285 extra_string = rest;
72b2ff0e 9286 }
2f069f6f 9287 else
72b2ff0e
VP
9288 {
9289 /* Create a private copy of condition string. */
9290 if (cond_string)
9291 {
9292 cond_string = xstrdup (cond_string);
9293 make_cleanup (xfree, cond_string);
9294 }
e7e0cddf
SS
9295 /* Create a private copy of any extra string. */
9296 if (extra_string)
9297 {
9298 extra_string = xstrdup (extra_string);
9299 make_cleanup (xfree, extra_string);
9300 }
72b2ff0e 9301 }
0fb4aa4b 9302
983af33b 9303 ops->create_breakpoints_sal (gdbarch, &canonical, lsal,
e7e0cddf 9304 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9305 tempflag ? disp_del : disp_donttouch,
9306 thread, task, ignore_count, ops,
44f238bb 9307 from_tty, enabled, internal, flags);
c906108c 9308 }
0101ce28
JJ
9309 else
9310 {
0101ce28
JJ
9311 struct breakpoint *b;
9312
0101ce28
JJ
9313 make_cleanup (xfree, copy_arg);
9314
bfccc43c
YQ
9315 if (is_tracepoint_type (type_wanted))
9316 {
9317 struct tracepoint *t;
9318
9319 t = XCNEW (struct tracepoint);
9320 b = &t->base;
9321 }
9322 else
9323 b = XNEW (struct breakpoint);
9324
9325 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9326
f8eba3c6 9327 b->addr_string = copy_arg;
72b2ff0e 9328 b->cond_string = NULL;
e7e0cddf 9329 b->extra_string = NULL;
0101ce28 9330 b->ignore_count = ignore_count;
0101ce28 9331 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9332 b->condition_not_parsed = 1;
41447f92 9333 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9334 if ((type_wanted != bp_breakpoint
9335 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9336 b->pspace = current_program_space;
8bea4e01 9337
bfccc43c 9338 install_breakpoint (internal, b, 0);
0101ce28
JJ
9339 }
9340
f8eba3c6 9341 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9342 {
3e43a32a
MS
9343 warning (_("Multiple breakpoints were set.\nUse the "
9344 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9345 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9346 }
9347
80c99de1
PA
9348 /* That's it. Discard the cleanups for data inserted into the
9349 breakpoint. */
9350 discard_cleanups (bkpt_chain);
9351 /* But cleanup everything else. */
c3f6f71d 9352 do_cleanups (old_chain);
217dc9e2 9353
80c99de1 9354 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 9355 update_global_location_list (1);
fd9b8c24
PA
9356
9357 return 1;
c3f6f71d 9358}
c906108c 9359
348d480f 9360/* Set a breakpoint.
72b2ff0e
VP
9361 ARG is a string describing breakpoint address,
9362 condition, and thread.
9363 FLAG specifies if a breakpoint is hardware on,
9364 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9365 and BP_TEMPFLAG. */
348d480f 9366
98deb0da 9367static void
72b2ff0e 9368break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9369{
72b2ff0e 9370 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9371 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9372 ? bp_hardware_breakpoint
9373 : bp_breakpoint);
55aa24fb
SDJ
9374 struct breakpoint_ops *ops;
9375 const char *arg_cp = arg;
9376
9377 /* Matching breakpoints on probes. */
9378 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9379 ops = &bkpt_probe_breakpoint_ops;
9380 else
9381 ops = &bkpt_breakpoint_ops;
c3f6f71d 9382
8cdf0e15
VP
9383 create_breakpoint (get_current_arch (),
9384 arg,
e7e0cddf 9385 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 9386 tempflag, type_wanted,
8cdf0e15
VP
9387 0 /* Ignore count */,
9388 pending_break_support,
55aa24fb 9389 ops,
8cdf0e15 9390 from_tty,
84f4c1fe 9391 1 /* enabled */,
44f238bb
PA
9392 0 /* internal */,
9393 0);
c906108c
SS
9394}
9395
c906108c
SS
9396/* Helper function for break_command_1 and disassemble_command. */
9397
9398void
fba45db2 9399resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9400{
9401 CORE_ADDR pc;
9402
9403 if (sal->pc == 0 && sal->symtab != NULL)
9404 {
9405 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9406 error (_("No line %d in file \"%s\"."),
c906108c
SS
9407 sal->line, sal->symtab->filename);
9408 sal->pc = pc;
6a048695 9409
4a64f543
MS
9410 /* If this SAL corresponds to a breakpoint inserted using a line
9411 number, then skip the function prologue if necessary. */
6a048695 9412 if (sal->explicit_line)
059acae7 9413 skip_prologue_sal (sal);
c906108c
SS
9414 }
9415
9416 if (sal->section == 0 && sal->symtab != NULL)
9417 {
9418 struct blockvector *bv;
c5aa993b
JM
9419 struct block *b;
9420 struct symbol *sym;
c906108c 9421
801e3a5b 9422 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
9423 if (bv != NULL)
9424 {
7f0df278 9425 sym = block_linkage_function (b);
c906108c
SS
9426 if (sym != NULL)
9427 {
9428 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 9429 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
9430 }
9431 else
9432 {
4a64f543
MS
9433 /* It really is worthwhile to have the section, so we'll
9434 just have to look harder. This case can be executed
9435 if we have line numbers but no functions (as can
9436 happen in assembly source). */
c906108c 9437
c5aa993b 9438 struct minimal_symbol *msym;
6c95b8df
PA
9439 struct cleanup *old_chain = save_current_space_and_thread ();
9440
9441 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9442
9443 msym = lookup_minimal_symbol_by_pc (sal->pc);
9444 if (msym)
714835d5 9445 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
9446
9447 do_cleanups (old_chain);
c906108c
SS
9448 }
9449 }
9450 }
9451}
9452
9453void
fba45db2 9454break_command (char *arg, int from_tty)
c906108c 9455{
db107f19 9456 break_command_1 (arg, 0, from_tty);
c906108c
SS
9457}
9458
c906108c 9459void
fba45db2 9460tbreak_command (char *arg, int from_tty)
c906108c 9461{
db107f19 9462 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9463}
9464
c906108c 9465static void
fba45db2 9466hbreak_command (char *arg, int from_tty)
c906108c 9467{
db107f19 9468 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9469}
9470
9471static void
fba45db2 9472thbreak_command (char *arg, int from_tty)
c906108c 9473{
db107f19 9474 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9475}
9476
9477static void
fba45db2 9478stop_command (char *arg, int from_tty)
c906108c 9479{
a3f17187 9480 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9481Usage: stop in <function | address>\n\
a3f17187 9482 stop at <line>\n"));
c906108c
SS
9483}
9484
9485static void
fba45db2 9486stopin_command (char *arg, int from_tty)
c906108c
SS
9487{
9488 int badInput = 0;
9489
c5aa993b 9490 if (arg == (char *) NULL)
c906108c
SS
9491 badInput = 1;
9492 else if (*arg != '*')
9493 {
9494 char *argptr = arg;
9495 int hasColon = 0;
9496
4a64f543 9497 /* Look for a ':'. If this is a line number specification, then
53a5351d 9498 say it is bad, otherwise, it should be an address or
4a64f543 9499 function/method name. */
c906108c 9500 while (*argptr && !hasColon)
c5aa993b
JM
9501 {
9502 hasColon = (*argptr == ':');
9503 argptr++;
9504 }
c906108c
SS
9505
9506 if (hasColon)
c5aa993b 9507 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9508 else
c5aa993b 9509 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9510 }
9511
9512 if (badInput)
a3f17187 9513 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9514 else
db107f19 9515 break_command_1 (arg, 0, from_tty);
c906108c
SS
9516}
9517
9518static void
fba45db2 9519stopat_command (char *arg, int from_tty)
c906108c
SS
9520{
9521 int badInput = 0;
9522
c5aa993b 9523 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9524 badInput = 1;
9525 else
9526 {
9527 char *argptr = arg;
9528 int hasColon = 0;
9529
4a64f543
MS
9530 /* Look for a ':'. If there is a '::' then get out, otherwise
9531 it is probably a line number. */
c906108c 9532 while (*argptr && !hasColon)
c5aa993b
JM
9533 {
9534 hasColon = (*argptr == ':');
9535 argptr++;
9536 }
c906108c
SS
9537
9538 if (hasColon)
c5aa993b 9539 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9540 else
c5aa993b 9541 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9542 }
9543
9544 if (badInput)
a3f17187 9545 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9546 else
db107f19 9547 break_command_1 (arg, 0, from_tty);
c906108c
SS
9548}
9549
e7e0cddf
SS
9550void dprintf_command (char *arg, int from_tty);
9551
9552/* The dynamic printf command is mostly like a regular breakpoint, but
9553 with a prewired command list consisting of a single output command,
9554 built from extra arguments supplied on the dprintf command
9555 line. */
9556
9557void
9558dprintf_command (char *arg, int from_tty)
9559{
9560 create_breakpoint (get_current_arch (),
9561 arg,
9562 NULL, 0, NULL, 1 /* parse arg */,
9563 0, bp_dprintf,
9564 0 /* Ignore count */,
9565 pending_break_support,
9566 &dprintf_breakpoint_ops,
9567 from_tty,
9568 1 /* enabled */,
9569 0 /* internal */,
9570 0);
9571}
9572
f1310107
TJB
9573/* Implement the "breakpoint_hit" breakpoint_ops method for
9574 ranged breakpoints. */
9575
9576static int
9577breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9578 struct address_space *aspace,
09ac7c10
TT
9579 CORE_ADDR bp_addr,
9580 const struct target_waitstatus *ws)
f1310107 9581{
09ac7c10 9582 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9583 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9584 return 0;
9585
f1310107
TJB
9586 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9587 bl->length, aspace, bp_addr);
9588}
9589
9590/* Implement the "resources_needed" breakpoint_ops method for
9591 ranged breakpoints. */
9592
9593static int
9594resources_needed_ranged_breakpoint (const struct bp_location *bl)
9595{
9596 return target_ranged_break_num_registers ();
9597}
9598
9599/* Implement the "print_it" breakpoint_ops method for
9600 ranged breakpoints. */
9601
9602static enum print_stop_action
348d480f 9603print_it_ranged_breakpoint (bpstat bs)
f1310107 9604{
348d480f 9605 struct breakpoint *b = bs->breakpoint_at;
f1310107 9606 struct bp_location *bl = b->loc;
79a45e25 9607 struct ui_out *uiout = current_uiout;
f1310107
TJB
9608
9609 gdb_assert (b->type == bp_hardware_breakpoint);
9610
9611 /* Ranged breakpoints have only one location. */
9612 gdb_assert (bl && bl->next == NULL);
9613
9614 annotate_breakpoint (b->number);
9615 if (b->disposition == disp_del)
9616 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
9617 else
9618 ui_out_text (uiout, "\nRanged breakpoint ");
9619 if (ui_out_is_mi_like_p (uiout))
9620 {
9621 ui_out_field_string (uiout, "reason",
9622 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9623 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9624 }
9625 ui_out_field_int (uiout, "bkptno", b->number);
9626 ui_out_text (uiout, ", ");
9627
9628 return PRINT_SRC_AND_LOC;
9629}
9630
9631/* Implement the "print_one" breakpoint_ops method for
9632 ranged breakpoints. */
9633
9634static void
9635print_one_ranged_breakpoint (struct breakpoint *b,
9636 struct bp_location **last_loc)
9637{
9638 struct bp_location *bl = b->loc;
9639 struct value_print_options opts;
79a45e25 9640 struct ui_out *uiout = current_uiout;
f1310107
TJB
9641
9642 /* Ranged breakpoints have only one location. */
9643 gdb_assert (bl && bl->next == NULL);
9644
9645 get_user_print_options (&opts);
9646
9647 if (opts.addressprint)
9648 /* We don't print the address range here, it will be printed later
9649 by print_one_detail_ranged_breakpoint. */
9650 ui_out_field_skip (uiout, "addr");
9651 annotate_field (5);
9652 print_breakpoint_location (b, bl);
9653 *last_loc = bl;
9654}
9655
9656/* Implement the "print_one_detail" breakpoint_ops method for
9657 ranged breakpoints. */
9658
9659static void
9660print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9661 struct ui_out *uiout)
9662{
9663 CORE_ADDR address_start, address_end;
9664 struct bp_location *bl = b->loc;
f99d8bf4
PA
9665 struct ui_file *stb = mem_fileopen ();
9666 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
9667
9668 gdb_assert (bl);
9669
9670 address_start = bl->address;
9671 address_end = address_start + bl->length - 1;
9672
9673 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 9674 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
9675 print_core_address (bl->gdbarch, address_start),
9676 print_core_address (bl->gdbarch, address_end));
9677 ui_out_field_stream (uiout, "addr", stb);
9678 ui_out_text (uiout, "\n");
9679
9680 do_cleanups (cleanup);
9681}
9682
9683/* Implement the "print_mention" breakpoint_ops method for
9684 ranged breakpoints. */
9685
9686static void
9687print_mention_ranged_breakpoint (struct breakpoint *b)
9688{
9689 struct bp_location *bl = b->loc;
79a45e25 9690 struct ui_out *uiout = current_uiout;
f1310107
TJB
9691
9692 gdb_assert (bl);
9693 gdb_assert (b->type == bp_hardware_breakpoint);
9694
9695 if (ui_out_is_mi_like_p (uiout))
9696 return;
9697
9698 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9699 b->number, paddress (bl->gdbarch, bl->address),
9700 paddress (bl->gdbarch, bl->address + bl->length - 1));
9701}
9702
9703/* Implement the "print_recreate" breakpoint_ops method for
9704 ranged breakpoints. */
9705
9706static void
9707print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9708{
9709 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
9710 b->addr_string_range_end);
d9b3f62e 9711 print_recreate_thread (b, fp);
f1310107
TJB
9712}
9713
9714/* The breakpoint_ops structure to be used in ranged breakpoints. */
9715
2060206e 9716static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9717
9718/* Find the address where the end of the breakpoint range should be
9719 placed, given the SAL of the end of the range. This is so that if
9720 the user provides a line number, the end of the range is set to the
9721 last instruction of the given line. */
9722
9723static CORE_ADDR
9724find_breakpoint_range_end (struct symtab_and_line sal)
9725{
9726 CORE_ADDR end;
9727
9728 /* If the user provided a PC value, use it. Otherwise,
9729 find the address of the end of the given location. */
9730 if (sal.explicit_pc)
9731 end = sal.pc;
9732 else
9733 {
9734 int ret;
9735 CORE_ADDR start;
9736
9737 ret = find_line_pc_range (sal, &start, &end);
9738 if (!ret)
9739 error (_("Could not find location of the end of the range."));
9740
9741 /* find_line_pc_range returns the start of the next line. */
9742 end--;
9743 }
9744
9745 return end;
9746}
9747
9748/* Implement the "break-range" CLI command. */
9749
9750static void
9751break_range_command (char *arg, int from_tty)
9752{
9753 char *arg_start, *addr_string_start, *addr_string_end;
9754 struct linespec_result canonical_start, canonical_end;
9755 int bp_count, can_use_bp, length;
9756 CORE_ADDR end;
9757 struct breakpoint *b;
9758 struct symtab_and_line sal_start, sal_end;
f1310107 9759 struct cleanup *cleanup_bkpt;
f8eba3c6 9760 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
9761
9762 /* We don't support software ranged breakpoints. */
9763 if (target_ranged_break_num_registers () < 0)
9764 error (_("This target does not support hardware ranged breakpoints."));
9765
9766 bp_count = hw_breakpoint_used_count ();
9767 bp_count += target_ranged_break_num_registers ();
9768 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9769 bp_count, 0);
9770 if (can_use_bp < 0)
9771 error (_("Hardware breakpoints used exceeds limit."));
9772
f8eba3c6 9773 arg = skip_spaces (arg);
f1310107
TJB
9774 if (arg == NULL || arg[0] == '\0')
9775 error(_("No address range specified."));
9776
f1310107
TJB
9777 init_linespec_result (&canonical_start);
9778
f8eba3c6
TT
9779 arg_start = arg;
9780 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 9781
f8eba3c6 9782 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
9783
9784 if (arg[0] != ',')
9785 error (_("Too few arguments."));
f8eba3c6 9786 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 9787 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
9788
9789 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
9790
9791 if (VEC_length (linespec_sals, canonical_start.sals) > 1
9792 || lsal_start->sals.nelts != 1)
f1310107
TJB
9793 error (_("Cannot create a ranged breakpoint with multiple locations."));
9794
f8eba3c6
TT
9795 sal_start = lsal_start->sals.sals[0];
9796 addr_string_start = savestring (arg_start, arg - arg_start);
9797 make_cleanup (xfree, addr_string_start);
f1310107
TJB
9798
9799 arg++; /* Skip the comma. */
f8eba3c6 9800 arg = skip_spaces (arg);
f1310107
TJB
9801
9802 /* Parse the end location. */
9803
f1310107
TJB
9804 init_linespec_result (&canonical_end);
9805 arg_start = arg;
9806
f8eba3c6 9807 /* We call decode_line_full directly here instead of using
f1310107
TJB
9808 parse_breakpoint_sals because we need to specify the start location's
9809 symtab and line as the default symtab and line for the end of the
9810 range. This makes it possible to have ranges like "foo.c:27, +14",
9811 where +14 means 14 lines from the start location. */
f8eba3c6
TT
9812 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
9813 sal_start.symtab, sal_start.line,
9814 &canonical_end, NULL, NULL);
9815
9816 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 9817
f8eba3c6 9818 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 9819 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
9820
9821 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
9822 if (VEC_length (linespec_sals, canonical_end.sals) > 1
9823 || lsal_end->sals.nelts != 1)
f1310107
TJB
9824 error (_("Cannot create a ranged breakpoint with multiple locations."));
9825
f8eba3c6
TT
9826 sal_end = lsal_end->sals.sals[0];
9827 addr_string_end = savestring (arg_start, arg - arg_start);
9828 make_cleanup (xfree, addr_string_end);
f1310107
TJB
9829
9830 end = find_breakpoint_range_end (sal_end);
9831 if (sal_start.pc > end)
177b42fe 9832 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9833
9834 length = end - sal_start.pc + 1;
9835 if (length < 0)
9836 /* Length overflowed. */
9837 error (_("Address range too large."));
9838 else if (length == 1)
9839 {
9840 /* This range is simple enough to be handled by
9841 the `hbreak' command. */
9842 hbreak_command (addr_string_start, 1);
9843
9844 do_cleanups (cleanup_bkpt);
9845
9846 return;
9847 }
9848
9849 /* Now set up the breakpoint. */
9850 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 9851 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
9852 set_breakpoint_count (breakpoint_count + 1);
9853 b->number = breakpoint_count;
9854 b->disposition = disp_donttouch;
f8eba3c6
TT
9855 b->addr_string = xstrdup (addr_string_start);
9856 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
9857 b->loc->length = length;
9858
f8eba3c6 9859 do_cleanups (cleanup_bkpt);
f1310107
TJB
9860
9861 mention (b);
8d3788bd 9862 observer_notify_breakpoint_created (b);
f1310107
TJB
9863 update_global_location_list (1);
9864}
9865
4a64f543
MS
9866/* Return non-zero if EXP is verified as constant. Returned zero
9867 means EXP is variable. Also the constant detection may fail for
9868 some constant expressions and in such case still falsely return
9869 zero. */
2e6e3d9c 9870
65d79d4b
SDJ
9871static int
9872watchpoint_exp_is_const (const struct expression *exp)
9873{
9874 int i = exp->nelts;
9875
9876 while (i > 0)
9877 {
9878 int oplenp, argsp;
9879
9880 /* We are only interested in the descriptor of each element. */
9881 operator_length (exp, i, &oplenp, &argsp);
9882 i -= oplenp;
9883
9884 switch (exp->elts[i].opcode)
9885 {
9886 case BINOP_ADD:
9887 case BINOP_SUB:
9888 case BINOP_MUL:
9889 case BINOP_DIV:
9890 case BINOP_REM:
9891 case BINOP_MOD:
9892 case BINOP_LSH:
9893 case BINOP_RSH:
9894 case BINOP_LOGICAL_AND:
9895 case BINOP_LOGICAL_OR:
9896 case BINOP_BITWISE_AND:
9897 case BINOP_BITWISE_IOR:
9898 case BINOP_BITWISE_XOR:
9899 case BINOP_EQUAL:
9900 case BINOP_NOTEQUAL:
9901 case BINOP_LESS:
9902 case BINOP_GTR:
9903 case BINOP_LEQ:
9904 case BINOP_GEQ:
9905 case BINOP_REPEAT:
9906 case BINOP_COMMA:
9907 case BINOP_EXP:
9908 case BINOP_MIN:
9909 case BINOP_MAX:
9910 case BINOP_INTDIV:
9911 case BINOP_CONCAT:
9912 case BINOP_IN:
9913 case BINOP_RANGE:
9914 case TERNOP_COND:
9915 case TERNOP_SLICE:
9916 case TERNOP_SLICE_COUNT:
9917
9918 case OP_LONG:
9919 case OP_DOUBLE:
9920 case OP_DECFLOAT:
9921 case OP_LAST:
9922 case OP_COMPLEX:
9923 case OP_STRING:
9924 case OP_BITSTRING:
9925 case OP_ARRAY:
9926 case OP_TYPE:
9927 case OP_NAME:
9928 case OP_OBJC_NSSTRING:
9929
9930 case UNOP_NEG:
9931 case UNOP_LOGICAL_NOT:
9932 case UNOP_COMPLEMENT:
9933 case UNOP_ADDR:
9934 case UNOP_HIGH:
aeaa2474 9935 case UNOP_CAST:
4a64f543
MS
9936 /* Unary, binary and ternary operators: We have to check
9937 their operands. If they are constant, then so is the
9938 result of that operation. For instance, if A and B are
9939 determined to be constants, then so is "A + B".
9940
9941 UNOP_IND is one exception to the rule above, because the
9942 value of *ADDR is not necessarily a constant, even when
9943 ADDR is. */
65d79d4b
SDJ
9944 break;
9945
9946 case OP_VAR_VALUE:
9947 /* Check whether the associated symbol is a constant.
4a64f543 9948
65d79d4b 9949 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
9950 possible that a buggy compiler could mark a variable as
9951 constant even when it is not, and TYPE_CONST would return
9952 true in this case, while SYMBOL_CLASS wouldn't.
9953
9954 We also have to check for function symbols because they
9955 are always constant. */
65d79d4b
SDJ
9956 {
9957 struct symbol *s = exp->elts[i + 2].symbol;
9958
9959 if (SYMBOL_CLASS (s) != LOC_BLOCK
9960 && SYMBOL_CLASS (s) != LOC_CONST
9961 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9962 return 0;
9963 break;
9964 }
9965
9966 /* The default action is to return 0 because we are using
9967 the optimistic approach here: If we don't know something,
9968 then it is not a constant. */
9969 default:
9970 return 0;
9971 }
9972 }
9973
9974 return 1;
9975}
9976
3a5c3e22
PA
9977/* Implement the "dtor" breakpoint_ops method for watchpoints. */
9978
9979static void
9980dtor_watchpoint (struct breakpoint *self)
9981{
9982 struct watchpoint *w = (struct watchpoint *) self;
9983
9984 xfree (w->cond_exp);
9985 xfree (w->exp);
9986 xfree (w->exp_string);
9987 xfree (w->exp_string_reparse);
9988 value_free (w->val);
9989
9990 base_breakpoint_ops.dtor (self);
9991}
9992
348d480f
PA
9993/* Implement the "re_set" breakpoint_ops method for watchpoints. */
9994
9995static void
9996re_set_watchpoint (struct breakpoint *b)
9997{
3a5c3e22
PA
9998 struct watchpoint *w = (struct watchpoint *) b;
9999
348d480f
PA
10000 /* Watchpoint can be either on expression using entirely global
10001 variables, or it can be on local variables.
10002
10003 Watchpoints of the first kind are never auto-deleted, and even
10004 persist across program restarts. Since they can use variables
10005 from shared libraries, we need to reparse expression as libraries
10006 are loaded and unloaded.
10007
10008 Watchpoints on local variables can also change meaning as result
10009 of solib event. For example, if a watchpoint uses both a local
10010 and a global variables in expression, it's a local watchpoint,
10011 but unloading of a shared library will make the expression
10012 invalid. This is not a very common use case, but we still
10013 re-evaluate expression, to avoid surprises to the user.
10014
10015 Note that for local watchpoints, we re-evaluate it only if
10016 watchpoints frame id is still valid. If it's not, it means the
10017 watchpoint is out of scope and will be deleted soon. In fact,
10018 I'm not sure we'll ever be called in this case.
10019
10020 If a local watchpoint's frame id is still valid, then
3a5c3e22 10021 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10022
3a5c3e22
PA
10023 Don't do anything about disabled watchpoints, since they will be
10024 reevaluated again when enabled. */
10025 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10026}
10027
77b06cd7
TJB
10028/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10029
10030static int
10031insert_watchpoint (struct bp_location *bl)
10032{
3a5c3e22
PA
10033 struct watchpoint *w = (struct watchpoint *) bl->owner;
10034 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10035
10036 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10037 w->cond_exp);
77b06cd7
TJB
10038}
10039
10040/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10041
10042static int
10043remove_watchpoint (struct bp_location *bl)
10044{
3a5c3e22
PA
10045 struct watchpoint *w = (struct watchpoint *) bl->owner;
10046 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10047
10048 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10049 w->cond_exp);
e09342b5
TJB
10050}
10051
e09342b5 10052static int
348d480f 10053breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10054 struct address_space *aspace, CORE_ADDR bp_addr,
10055 const struct target_waitstatus *ws)
e09342b5 10056{
348d480f 10057 struct breakpoint *b = bl->owner;
3a5c3e22 10058 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10059
348d480f
PA
10060 /* Continuable hardware watchpoints are treated as non-existent if the
10061 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10062 some data address). Otherwise gdb won't stop on a break instruction
10063 in the code (not from a breakpoint) when a hardware watchpoint has
10064 been defined. Also skip watchpoints which we know did not trigger
10065 (did not match the data address). */
10066 if (is_hardware_watchpoint (b)
3a5c3e22 10067 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10068 return 0;
9c06b0b4 10069
348d480f 10070 return 1;
9c06b0b4
TJB
10071}
10072
348d480f
PA
10073static void
10074check_status_watchpoint (bpstat bs)
9c06b0b4 10075{
348d480f 10076 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10077
348d480f 10078 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10079}
10080
10081/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10082 hardware watchpoints. */
9c06b0b4
TJB
10083
10084static int
348d480f 10085resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10086{
3a5c3e22
PA
10087 struct watchpoint *w = (struct watchpoint *) bl->owner;
10088 int length = w->exact? 1 : bl->length;
348d480f
PA
10089
10090 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10091}
10092
10093/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10094 hardware watchpoints. */
9c06b0b4
TJB
10095
10096static int
348d480f 10097works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10098{
efa80663
PA
10099 /* Read and access watchpoints only work with hardware support. */
10100 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10101}
10102
9c06b0b4 10103static enum print_stop_action
348d480f 10104print_it_watchpoint (bpstat bs)
9c06b0b4 10105{
348d480f
PA
10106 struct cleanup *old_chain;
10107 struct breakpoint *b;
10108 const struct bp_location *bl;
f99d8bf4 10109 struct ui_file *stb;
348d480f 10110 enum print_stop_action result;
3a5c3e22 10111 struct watchpoint *w;
79a45e25 10112 struct ui_out *uiout = current_uiout;
348d480f
PA
10113
10114 gdb_assert (bs->bp_location_at != NULL);
10115
10116 bl = bs->bp_location_at;
10117 b = bs->breakpoint_at;
3a5c3e22 10118 w = (struct watchpoint *) b;
348d480f 10119
f99d8bf4
PA
10120 stb = mem_fileopen ();
10121 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10122
10123 switch (b->type)
10124 {
348d480f 10125 case bp_watchpoint:
9c06b0b4
TJB
10126 case bp_hardware_watchpoint:
10127 annotate_watchpoint (b->number);
10128 if (ui_out_is_mi_like_p (uiout))
10129 ui_out_field_string
10130 (uiout, "reason",
10131 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10132 mention (b);
10133 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10134 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10135 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10136 ui_out_field_stream (uiout, "old", stb);
10137 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10138 watchpoint_value_print (w->val, stb);
348d480f
PA
10139 ui_out_field_stream (uiout, "new", stb);
10140 ui_out_text (uiout, "\n");
10141 /* More than one watchpoint may have been triggered. */
10142 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10143 break;
10144
10145 case bp_read_watchpoint:
10146 if (ui_out_is_mi_like_p (uiout))
10147 ui_out_field_string
10148 (uiout, "reason",
10149 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10150 mention (b);
10151 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10152 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10153 watchpoint_value_print (w->val, stb);
348d480f
PA
10154 ui_out_field_stream (uiout, "value", stb);
10155 ui_out_text (uiout, "\n");
10156 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10157 break;
10158
10159 case bp_access_watchpoint:
348d480f
PA
10160 if (bs->old_val != NULL)
10161 {
10162 annotate_watchpoint (b->number);
10163 if (ui_out_is_mi_like_p (uiout))
10164 ui_out_field_string
10165 (uiout, "reason",
10166 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10167 mention (b);
10168 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10169 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10170 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10171 ui_out_field_stream (uiout, "old", stb);
10172 ui_out_text (uiout, "\nNew value = ");
10173 }
10174 else
10175 {
10176 mention (b);
10177 if (ui_out_is_mi_like_p (uiout))
10178 ui_out_field_string
10179 (uiout, "reason",
10180 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10181 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10182 ui_out_text (uiout, "\nValue = ");
10183 }
f99d8bf4 10184 watchpoint_value_print (w->val, stb);
348d480f
PA
10185 ui_out_field_stream (uiout, "new", stb);
10186 ui_out_text (uiout, "\n");
10187 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10188 break;
10189 default:
348d480f 10190 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10191 }
10192
348d480f
PA
10193 do_cleanups (old_chain);
10194 return result;
10195}
10196
10197/* Implement the "print_mention" breakpoint_ops method for hardware
10198 watchpoints. */
10199
10200static void
10201print_mention_watchpoint (struct breakpoint *b)
10202{
10203 struct cleanup *ui_out_chain;
3a5c3e22 10204 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10205 struct ui_out *uiout = current_uiout;
348d480f
PA
10206
10207 switch (b->type)
10208 {
10209 case bp_watchpoint:
10210 ui_out_text (uiout, "Watchpoint ");
10211 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10212 break;
10213 case bp_hardware_watchpoint:
10214 ui_out_text (uiout, "Hardware watchpoint ");
10215 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10216 break;
10217 case bp_read_watchpoint:
10218 ui_out_text (uiout, "Hardware read watchpoint ");
10219 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10220 break;
10221 case bp_access_watchpoint:
10222 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10223 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10224 break;
10225 default:
10226 internal_error (__FILE__, __LINE__,
10227 _("Invalid hardware watchpoint type."));
10228 }
10229
10230 ui_out_field_int (uiout, "number", b->number);
10231 ui_out_text (uiout, ": ");
3a5c3e22 10232 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10233 do_cleanups (ui_out_chain);
10234}
10235
10236/* Implement the "print_recreate" breakpoint_ops method for
10237 watchpoints. */
10238
10239static void
10240print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10241{
3a5c3e22
PA
10242 struct watchpoint *w = (struct watchpoint *) b;
10243
348d480f
PA
10244 switch (b->type)
10245 {
10246 case bp_watchpoint:
10247 case bp_hardware_watchpoint:
10248 fprintf_unfiltered (fp, "watch");
10249 break;
10250 case bp_read_watchpoint:
10251 fprintf_unfiltered (fp, "rwatch");
10252 break;
10253 case bp_access_watchpoint:
10254 fprintf_unfiltered (fp, "awatch");
10255 break;
10256 default:
10257 internal_error (__FILE__, __LINE__,
10258 _("Invalid watchpoint type."));
10259 }
10260
3a5c3e22 10261 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10262 print_recreate_thread (b, fp);
348d480f
PA
10263}
10264
10265/* The breakpoint_ops structure to be used in hardware watchpoints. */
10266
2060206e 10267static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10268
10269/* Implement the "insert" breakpoint_ops method for
10270 masked hardware watchpoints. */
10271
10272static int
10273insert_masked_watchpoint (struct bp_location *bl)
10274{
3a5c3e22
PA
10275 struct watchpoint *w = (struct watchpoint *) bl->owner;
10276
10277 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10278 bl->watchpoint_type);
10279}
10280
10281/* Implement the "remove" breakpoint_ops method for
10282 masked hardware watchpoints. */
10283
10284static int
10285remove_masked_watchpoint (struct bp_location *bl)
10286{
3a5c3e22
PA
10287 struct watchpoint *w = (struct watchpoint *) bl->owner;
10288
10289 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10290 bl->watchpoint_type);
10291}
10292
10293/* Implement the "resources_needed" breakpoint_ops method for
10294 masked hardware watchpoints. */
10295
10296static int
10297resources_needed_masked_watchpoint (const struct bp_location *bl)
10298{
3a5c3e22
PA
10299 struct watchpoint *w = (struct watchpoint *) bl->owner;
10300
10301 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10302}
10303
10304/* Implement the "works_in_software_mode" breakpoint_ops method for
10305 masked hardware watchpoints. */
10306
10307static int
10308works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10309{
10310 return 0;
10311}
10312
10313/* Implement the "print_it" breakpoint_ops method for
10314 masked hardware watchpoints. */
10315
10316static enum print_stop_action
10317print_it_masked_watchpoint (bpstat bs)
10318{
10319 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10320 struct ui_out *uiout = current_uiout;
348d480f
PA
10321
10322 /* Masked watchpoints have only one location. */
10323 gdb_assert (b->loc && b->loc->next == NULL);
10324
10325 switch (b->type)
10326 {
10327 case bp_hardware_watchpoint:
10328 annotate_watchpoint (b->number);
10329 if (ui_out_is_mi_like_p (uiout))
10330 ui_out_field_string
10331 (uiout, "reason",
10332 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10333 break;
10334
10335 case bp_read_watchpoint:
10336 if (ui_out_is_mi_like_p (uiout))
10337 ui_out_field_string
10338 (uiout, "reason",
10339 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10340 break;
10341
10342 case bp_access_watchpoint:
10343 if (ui_out_is_mi_like_p (uiout))
10344 ui_out_field_string
10345 (uiout, "reason",
10346 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10347 break;
10348 default:
10349 internal_error (__FILE__, __LINE__,
10350 _("Invalid hardware watchpoint type."));
10351 }
10352
10353 mention (b);
9c06b0b4
TJB
10354 ui_out_text (uiout, _("\n\
10355Check the underlying instruction at PC for the memory\n\
10356address and value which triggered this watchpoint.\n"));
10357 ui_out_text (uiout, "\n");
10358
10359 /* More than one watchpoint may have been triggered. */
10360 return PRINT_UNKNOWN;
10361}
10362
10363/* Implement the "print_one_detail" breakpoint_ops method for
10364 masked hardware watchpoints. */
10365
10366static void
10367print_one_detail_masked_watchpoint (const struct breakpoint *b,
10368 struct ui_out *uiout)
10369{
3a5c3e22
PA
10370 struct watchpoint *w = (struct watchpoint *) b;
10371
9c06b0b4
TJB
10372 /* Masked watchpoints have only one location. */
10373 gdb_assert (b->loc && b->loc->next == NULL);
10374
10375 ui_out_text (uiout, "\tmask ");
3a5c3e22 10376 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10377 ui_out_text (uiout, "\n");
10378}
10379
10380/* Implement the "print_mention" breakpoint_ops method for
10381 masked hardware watchpoints. */
10382
10383static void
10384print_mention_masked_watchpoint (struct breakpoint *b)
10385{
3a5c3e22 10386 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10387 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10388 struct cleanup *ui_out_chain;
10389
10390 switch (b->type)
10391 {
10392 case bp_hardware_watchpoint:
10393 ui_out_text (uiout, "Masked hardware watchpoint ");
10394 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10395 break;
10396 case bp_read_watchpoint:
10397 ui_out_text (uiout, "Masked hardware read watchpoint ");
10398 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10399 break;
10400 case bp_access_watchpoint:
10401 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10402 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10403 break;
10404 default:
10405 internal_error (__FILE__, __LINE__,
10406 _("Invalid hardware watchpoint type."));
10407 }
10408
10409 ui_out_field_int (uiout, "number", b->number);
10410 ui_out_text (uiout, ": ");
3a5c3e22 10411 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10412 do_cleanups (ui_out_chain);
10413}
10414
10415/* Implement the "print_recreate" breakpoint_ops method for
10416 masked hardware watchpoints. */
10417
10418static void
10419print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10420{
3a5c3e22 10421 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10422 char tmp[40];
10423
10424 switch (b->type)
10425 {
10426 case bp_hardware_watchpoint:
10427 fprintf_unfiltered (fp, "watch");
10428 break;
10429 case bp_read_watchpoint:
10430 fprintf_unfiltered (fp, "rwatch");
10431 break;
10432 case bp_access_watchpoint:
10433 fprintf_unfiltered (fp, "awatch");
10434 break;
10435 default:
10436 internal_error (__FILE__, __LINE__,
10437 _("Invalid hardware watchpoint type."));
10438 }
10439
3a5c3e22
PA
10440 sprintf_vma (tmp, w->hw_wp_mask);
10441 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10442 print_recreate_thread (b, fp);
9c06b0b4
TJB
10443}
10444
10445/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10446
2060206e 10447static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10448
10449/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10450
10451static int
10452is_masked_watchpoint (const struct breakpoint *b)
10453{
10454 return b->ops == &masked_watchpoint_breakpoint_ops;
10455}
10456
53a5351d
JM
10457/* accessflag: hw_write: watch write,
10458 hw_read: watch read,
10459 hw_access: watch access (read or write) */
c906108c 10460static void
84f4c1fe
PM
10461watch_command_1 (char *arg, int accessflag, int from_tty,
10462 int just_location, int internal)
c906108c 10463{
a9634178 10464 volatile struct gdb_exception e;
d983da9c 10465 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 10466 struct expression *exp;
60e1c644 10467 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10468 struct value *val, *mark, *result;
c906108c 10469 struct frame_info *frame;
c906108c
SS
10470 char *exp_start = NULL;
10471 char *exp_end = NULL;
9c06b0b4
TJB
10472 char *tok, *end_tok;
10473 int toklen = -1;
c906108c
SS
10474 char *cond_start = NULL;
10475 char *cond_end = NULL;
c906108c 10476 enum bptype bp_type;
37e4754d 10477 int thread = -1;
0cf6dd15 10478 int pc = 0;
9c06b0b4
TJB
10479 /* Flag to indicate whether we are going to use masks for
10480 the hardware watchpoint. */
10481 int use_mask = 0;
10482 CORE_ADDR mask = 0;
3a5c3e22 10483 struct watchpoint *w;
c906108c 10484
37e4754d
LM
10485 /* Make sure that we actually have parameters to parse. */
10486 if (arg != NULL && arg[0] != '\0')
10487 {
9c06b0b4 10488 char *value_start;
37e4754d 10489
9c06b0b4
TJB
10490 /* Look for "parameter value" pairs at the end
10491 of the arguments string. */
10492 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
10493 {
10494 /* Skip whitespace at the end of the argument list. */
10495 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10496 tok--;
10497
10498 /* Find the beginning of the last token.
10499 This is the value of the parameter. */
10500 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10501 tok--;
10502 value_start = tok + 1;
10503
10504 /* Skip whitespace. */
10505 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10506 tok--;
10507
10508 end_tok = tok;
10509
10510 /* Find the beginning of the second to last token.
10511 This is the parameter itself. */
10512 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10513 tok--;
10514 tok++;
10515 toklen = end_tok - tok + 1;
10516
10517 if (toklen == 6 && !strncmp (tok, "thread", 6))
10518 {
10519 /* At this point we've found a "thread" token, which means
10520 the user is trying to set a watchpoint that triggers
10521 only in a specific thread. */
10522 char *endp;
37e4754d 10523
9c06b0b4
TJB
10524 if (thread != -1)
10525 error(_("You can specify only one thread."));
37e4754d 10526
9c06b0b4
TJB
10527 /* Extract the thread ID from the next token. */
10528 thread = strtol (value_start, &endp, 0);
37e4754d 10529
9c06b0b4
TJB
10530 /* Check if the user provided a valid numeric value for the
10531 thread ID. */
10532 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10533 error (_("Invalid thread ID specification %s."), value_start);
10534
10535 /* Check if the thread actually exists. */
10536 if (!valid_thread_id (thread))
10537 error (_("Unknown thread %d."), thread);
10538 }
10539 else if (toklen == 4 && !strncmp (tok, "mask", 4))
10540 {
10541 /* We've found a "mask" token, which means the user wants to
10542 create a hardware watchpoint that is going to have the mask
10543 facility. */
10544 struct value *mask_value, *mark;
37e4754d 10545
9c06b0b4
TJB
10546 if (use_mask)
10547 error(_("You can specify only one mask."));
37e4754d 10548
9c06b0b4 10549 use_mask = just_location = 1;
37e4754d 10550
9c06b0b4
TJB
10551 mark = value_mark ();
10552 mask_value = parse_to_comma_and_eval (&value_start);
10553 mask = value_as_address (mask_value);
10554 value_free_to_mark (mark);
10555 }
10556 else
10557 /* We didn't recognize what we found. We should stop here. */
10558 break;
37e4754d 10559
9c06b0b4
TJB
10560 /* Truncate the string and get rid of the "parameter value" pair before
10561 the arguments string is parsed by the parse_exp_1 function. */
10562 *tok = '\0';
10563 }
37e4754d
LM
10564 }
10565
10566 /* Parse the rest of the arguments. */
c906108c
SS
10567 innermost_block = NULL;
10568 exp_start = arg;
1bb9788d 10569 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 10570 exp_end = arg;
fa8a61dc
TT
10571 /* Remove trailing whitespace from the expression before saving it.
10572 This makes the eventual display of the expression string a bit
10573 prettier. */
10574 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10575 --exp_end;
10576
65d79d4b
SDJ
10577 /* Checking if the expression is not constant. */
10578 if (watchpoint_exp_is_const (exp))
10579 {
10580 int len;
10581
10582 len = exp_end - exp_start;
10583 while (len > 0 && isspace (exp_start[len - 1]))
10584 len--;
10585 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10586 }
10587
c906108c
SS
10588 exp_valid_block = innermost_block;
10589 mark = value_mark ();
a1442452 10590 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
10591
10592 if (just_location)
10593 {
9c06b0b4
TJB
10594 int ret;
10595
06a64a0b 10596 exp_valid_block = NULL;
a1442452 10597 val = value_addr (result);
06a64a0b
TT
10598 release_value (val);
10599 value_free_to_mark (mark);
9c06b0b4
TJB
10600
10601 if (use_mask)
10602 {
10603 ret = target_masked_watch_num_registers (value_as_address (val),
10604 mask);
10605 if (ret == -1)
10606 error (_("This target does not support masked watchpoints."));
10607 else if (ret == -2)
10608 error (_("Invalid mask or memory region."));
10609 }
06a64a0b
TT
10610 }
10611 else if (val != NULL)
fa4727a6 10612 release_value (val);
c906108c 10613
e9cafbcc
TT
10614 tok = skip_spaces (arg);
10615 end_tok = skip_to_space (tok);
c906108c
SS
10616
10617 toklen = end_tok - tok;
10618 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10619 {
2d134ed3
PA
10620 struct expression *cond;
10621
60e1c644 10622 innermost_block = NULL;
c906108c 10623 tok = cond_start = end_tok + 1;
1bb9788d 10624 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
10625
10626 /* The watchpoint expression may not be local, but the condition
10627 may still be. E.g.: `watch global if local > 0'. */
10628 cond_exp_valid_block = innermost_block;
10629
2d134ed3 10630 xfree (cond);
c906108c
SS
10631 cond_end = tok;
10632 }
10633 if (*tok)
8a3fe4f8 10634 error (_("Junk at end of command."));
c906108c 10635
53a5351d 10636 if (accessflag == hw_read)
c5aa993b 10637 bp_type = bp_read_watchpoint;
53a5351d 10638 else if (accessflag == hw_access)
c5aa993b
JM
10639 bp_type = bp_access_watchpoint;
10640 else
10641 bp_type = bp_hardware_watchpoint;
c906108c 10642
d983da9c 10643 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
10644
10645 /* If the expression is "local", then set up a "watchpoint scope"
10646 breakpoint at the point where we've left the scope of the watchpoint
10647 expression. Create the scope breakpoint before the watchpoint, so
10648 that we will encounter it first in bpstat_stop_status. */
60e1c644 10649 if (exp_valid_block && frame)
d983da9c 10650 {
edb3359d
DJ
10651 if (frame_id_p (frame_unwind_caller_id (frame)))
10652 {
10653 scope_breakpoint
a6d9a66e
UW
10654 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
10655 frame_unwind_caller_pc (frame),
06edf0c0
PA
10656 bp_watchpoint_scope,
10657 &momentary_breakpoint_ops);
d983da9c 10658
edb3359d 10659 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10660
edb3359d
DJ
10661 /* Automatically delete the breakpoint when it hits. */
10662 scope_breakpoint->disposition = disp_del;
d983da9c 10663
edb3359d
DJ
10664 /* Only break in the proper frame (help with recursion). */
10665 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 10666
edb3359d 10667 /* Set the address at which we will stop. */
a6d9a66e
UW
10668 scope_breakpoint->loc->gdbarch
10669 = frame_unwind_caller_arch (frame);
edb3359d
DJ
10670 scope_breakpoint->loc->requested_address
10671 = frame_unwind_caller_pc (frame);
10672 scope_breakpoint->loc->address
a6d9a66e
UW
10673 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10674 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10675 scope_breakpoint->type);
10676 }
d983da9c
DJ
10677 }
10678
c906108c 10679 /* Now set up the breakpoint. */
3a5c3e22
PA
10680
10681 w = XCNEW (struct watchpoint);
10682 b = &w->base;
348d480f 10683 if (use_mask)
3a5c3e22
PA
10684 init_raw_breakpoint_without_location (b, NULL, bp_type,
10685 &masked_watchpoint_breakpoint_ops);
348d480f 10686 else
3a5c3e22
PA
10687 init_raw_breakpoint_without_location (b, NULL, bp_type,
10688 &watchpoint_breakpoint_ops);
37e4754d 10689 b->thread = thread;
b5de0fa7 10690 b->disposition = disp_donttouch;
348d480f 10691 b->pspace = current_program_space;
3a5c3e22
PA
10692 w->exp = exp;
10693 w->exp_valid_block = exp_valid_block;
10694 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10695 if (just_location)
10696 {
10697 struct type *t = value_type (val);
10698 CORE_ADDR addr = value_as_address (val);
10699 char *name;
10700
10701 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
10702 name = type_to_string (t);
10703
3a5c3e22 10704 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 10705 core_addr_to_string (addr));
06a64a0b
TT
10706 xfree (name);
10707
3a5c3e22 10708 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
10709 (int) (exp_end - exp_start), exp_start);
10710
06a64a0b
TT
10711 /* The above expression is in C. */
10712 b->language = language_c;
10713 }
10714 else
3a5c3e22 10715 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10716
10717 if (use_mask)
10718 {
3a5c3e22 10719 w->hw_wp_mask = mask;
9c06b0b4
TJB
10720 }
10721 else
10722 {
3a5c3e22
PA
10723 w->val = val;
10724 w->val_valid = 1;
9c06b0b4 10725 }
77b06cd7 10726
c906108c
SS
10727 if (cond_start)
10728 b->cond_string = savestring (cond_start, cond_end - cond_start);
10729 else
10730 b->cond_string = 0;
c5aa993b 10731
c906108c 10732 if (frame)
f6bc2008 10733 {
3a5c3e22
PA
10734 w->watchpoint_frame = get_frame_id (frame);
10735 w->watchpoint_thread = inferior_ptid;
f6bc2008 10736 }
c906108c 10737 else
f6bc2008 10738 {
3a5c3e22
PA
10739 w->watchpoint_frame = null_frame_id;
10740 w->watchpoint_thread = null_ptid;
f6bc2008 10741 }
c906108c 10742
d983da9c 10743 if (scope_breakpoint != NULL)
c906108c 10744 {
d983da9c
DJ
10745 /* The scope breakpoint is related to the watchpoint. We will
10746 need to act on them together. */
10747 b->related_breakpoint = scope_breakpoint;
10748 scope_breakpoint->related_breakpoint = b;
c906108c 10749 }
d983da9c 10750
06a64a0b
TT
10751 if (!just_location)
10752 value_free_to_mark (mark);
2d134ed3 10753
a9634178
TJB
10754 TRY_CATCH (e, RETURN_MASK_ALL)
10755 {
10756 /* Finally update the new watchpoint. This creates the locations
10757 that should be inserted. */
3a5c3e22 10758 update_watchpoint (w, 1);
a9634178
TJB
10759 }
10760 if (e.reason < 0)
10761 {
10762 delete_breakpoint (b);
10763 throw_exception (e);
10764 }
10765
3ea46bff 10766 install_breakpoint (internal, b, 1);
c906108c
SS
10767}
10768
e09342b5 10769/* Return count of debug registers needed to watch the given expression.
e09342b5 10770 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10771
c906108c 10772static int
a9634178 10773can_use_hardware_watchpoint (struct value *v)
c906108c
SS
10774{
10775 int found_memory_cnt = 0;
2e70b7b9 10776 struct value *head = v;
c906108c
SS
10777
10778 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10779 if (!can_use_hw_watchpoints)
c906108c 10780 return 0;
c5aa993b 10781
5c44784c
JM
10782 /* Make sure that the value of the expression depends only upon
10783 memory contents, and values computed from them within GDB. If we
10784 find any register references or function calls, we can't use a
10785 hardware watchpoint.
10786
10787 The idea here is that evaluating an expression generates a series
10788 of values, one holding the value of every subexpression. (The
10789 expression a*b+c has five subexpressions: a, b, a*b, c, and
10790 a*b+c.) GDB's values hold almost enough information to establish
10791 the criteria given above --- they identify memory lvalues,
10792 register lvalues, computed values, etcetera. So we can evaluate
10793 the expression, and then scan the chain of values that leaves
10794 behind to decide whether we can detect any possible change to the
10795 expression's final value using only hardware watchpoints.
10796
10797 However, I don't think that the values returned by inferior
10798 function calls are special in any way. So this function may not
10799 notice that an expression involving an inferior function call
10800 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 10801 for (; v; v = value_next (v))
c906108c 10802 {
5c44784c 10803 if (VALUE_LVAL (v) == lval_memory)
c906108c 10804 {
8464be76
DJ
10805 if (v != head && value_lazy (v))
10806 /* A lazy memory lvalue in the chain is one that GDB never
10807 needed to fetch; we either just used its address (e.g.,
10808 `a' in `a.b') or we never needed it at all (e.g., `a'
10809 in `a,b'). This doesn't apply to HEAD; if that is
10810 lazy then it was not readable, but watch it anyway. */
5c44784c 10811 ;
53a5351d 10812 else
5c44784c
JM
10813 {
10814 /* Ahh, memory we actually used! Check if we can cover
10815 it with hardware watchpoints. */
df407dfe 10816 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10817
10818 /* We only watch structs and arrays if user asked for it
10819 explicitly, never if they just happen to appear in a
10820 middle of some value chain. */
10821 if (v == head
10822 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10823 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10824 {
42ae5230 10825 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10826 int len;
10827 int num_regs;
10828
a9634178 10829 len = (target_exact_watchpoints
e09342b5
TJB
10830 && is_scalar_type_recursive (vtype))?
10831 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10832
e09342b5
TJB
10833 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10834 if (!num_regs)
2e70b7b9
MS
10835 return 0;
10836 else
e09342b5 10837 found_memory_cnt += num_regs;
2e70b7b9 10838 }
5c44784c 10839 }
c5aa993b 10840 }
5086187c
AC
10841 else if (VALUE_LVAL (v) != not_lval
10842 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10843 return 0; /* These are values from the history (e.g., $1). */
5086187c 10844 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10845 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10846 }
10847
10848 /* The expression itself looks suitable for using a hardware
10849 watchpoint, but give the target machine a chance to reject it. */
10850 return found_memory_cnt;
10851}
10852
8b93c638 10853void
84f4c1fe 10854watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10855{
84f4c1fe 10856 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10857}
10858
10859/* A helper function that looks for an argument at the start of a
10860 string. The argument must also either be at the end of the string,
10861 or be followed by whitespace. Returns 1 if it finds the argument,
10862 0 otherwise. If the argument is found, it updates *STR. */
10863
10864static int
10865check_for_argument (char **str, char *arg, int arg_len)
10866{
10867 if (strncmp (*str, arg, arg_len) == 0
10868 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
10869 {
10870 *str += arg_len;
10871 return 1;
10872 }
10873 return 0;
10874}
10875
10876/* A helper function that looks for the "-location" argument and then
10877 calls watch_command_1. */
10878
10879static void
10880watch_maybe_just_location (char *arg, int accessflag, int from_tty)
10881{
10882 int just_location = 0;
10883
10884 if (arg
10885 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10886 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10887 {
e9cafbcc 10888 arg = skip_spaces (arg);
06a64a0b
TT
10889 just_location = 1;
10890 }
10891
84f4c1fe 10892 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 10893}
8926118c 10894
c5aa993b 10895static void
fba45db2 10896watch_command (char *arg, int from_tty)
c906108c 10897{
06a64a0b 10898 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10899}
10900
8b93c638 10901void
84f4c1fe 10902rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10903{
84f4c1fe 10904 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10905}
8926118c 10906
c5aa993b 10907static void
fba45db2 10908rwatch_command (char *arg, int from_tty)
c906108c 10909{
06a64a0b 10910 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10911}
10912
8b93c638 10913void
84f4c1fe 10914awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10915{
84f4c1fe 10916 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10917}
8926118c 10918
c5aa993b 10919static void
fba45db2 10920awatch_command (char *arg, int from_tty)
c906108c 10921{
06a64a0b 10922 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10923}
c906108c 10924\f
c5aa993b 10925
43ff13b4 10926/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
10927 because it uses the mechanisms of breakpoints. */
10928
bfec99b2
PA
10929struct until_break_command_continuation_args
10930{
10931 struct breakpoint *breakpoint;
10932 struct breakpoint *breakpoint2;
186c406b 10933 int thread_num;
bfec99b2
PA
10934};
10935
43ff13b4 10936/* This function is called by fetch_inferior_event via the
4a64f543 10937 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 10938 care of cleaning up the temporary breakpoints set up by the until
4a64f543 10939 command. */
c2c6d25f 10940static void
fa4cd53f 10941until_break_command_continuation (void *arg, int err)
43ff13b4 10942{
bfec99b2
PA
10943 struct until_break_command_continuation_args *a = arg;
10944
10945 delete_breakpoint (a->breakpoint);
10946 if (a->breakpoint2)
10947 delete_breakpoint (a->breakpoint2);
186c406b 10948 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
10949}
10950
c906108c 10951void
ae66c1fc 10952until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
10953{
10954 struct symtabs_and_lines sals;
10955 struct symtab_and_line sal;
8556afb4
PA
10956 struct frame_info *frame;
10957 struct gdbarch *frame_gdbarch;
10958 struct frame_id stack_frame_id;
10959 struct frame_id caller_frame_id;
c906108c 10960 struct breakpoint *breakpoint;
f107f563 10961 struct breakpoint *breakpoint2 = NULL;
c906108c 10962 struct cleanup *old_chain;
186c406b
TT
10963 int thread;
10964 struct thread_info *tp;
c906108c
SS
10965
10966 clear_proceed_status ();
10967
10968 /* Set a breakpoint where the user wants it and at return from
4a64f543 10969 this function. */
c5aa993b 10970
1bfeeb0f 10971 if (last_displayed_sal_is_valid ())
f8eba3c6 10972 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 10973 get_last_displayed_symtab (),
f8eba3c6 10974 get_last_displayed_line ());
c906108c 10975 else
f8eba3c6
TT
10976 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
10977 (struct symtab *) NULL, 0);
c5aa993b 10978
c906108c 10979 if (sals.nelts != 1)
8a3fe4f8 10980 error (_("Couldn't get information on specified line."));
c5aa993b 10981
c906108c 10982 sal = sals.sals[0];
4a64f543 10983 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 10984
c906108c 10985 if (*arg)
8a3fe4f8 10986 error (_("Junk at end of arguments."));
c5aa993b 10987
c906108c 10988 resolve_sal_pc (&sal);
c5aa993b 10989
186c406b
TT
10990 tp = inferior_thread ();
10991 thread = tp->num;
10992
883bc8d1
PA
10993 old_chain = make_cleanup (null_cleanup, NULL);
10994
8556afb4
PA
10995 /* Note linespec handling above invalidates the frame chain.
10996 Installing a breakpoint also invalidates the frame chain (as it
10997 may need to switch threads), so do any frame handling before
10998 that. */
10999
11000 frame = get_selected_frame (NULL);
11001 frame_gdbarch = get_frame_arch (frame);
11002 stack_frame_id = get_stack_frame_id (frame);
11003 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11004
ae66c1fc
EZ
11005 /* Keep within the current frame, or in frames called by the current
11006 one. */
edb3359d 11007
883bc8d1 11008 if (frame_id_p (caller_frame_id))
c906108c 11009 {
883bc8d1
PA
11010 struct symtab_and_line sal2;
11011
11012 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11013 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11014 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11015 sal2,
11016 caller_frame_id,
f107f563
VP
11017 bp_until);
11018 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11019
883bc8d1 11020 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11021 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11022 }
c5aa993b 11023
c70a6932
JK
11024 /* set_momentary_breakpoint could invalidate FRAME. */
11025 frame = NULL;
11026
883bc8d1
PA
11027 if (anywhere)
11028 /* If the user told us to continue until a specified location,
11029 we don't specify a frame at which we need to stop. */
11030 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11031 null_frame_id, bp_until);
11032 else
11033 /* Otherwise, specify the selected frame, because we want to stop
11034 only at the very same frame. */
11035 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11036 stack_frame_id, bp_until);
11037 make_cleanup_delete_breakpoint (breakpoint);
11038
a493e3e2 11039 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11040
4a64f543
MS
11041 /* If we are running asynchronously, and proceed call above has
11042 actually managed to start the target, arrange for breakpoints to
11043 be deleted when the target stops. Otherwise, we're already
11044 stopped and delete breakpoints via cleanup chain. */
f107f563 11045
8ea051c5 11046 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11047 {
bfec99b2
PA
11048 struct until_break_command_continuation_args *args;
11049 args = xmalloc (sizeof (*args));
f107f563 11050
bfec99b2
PA
11051 args->breakpoint = breakpoint;
11052 args->breakpoint2 = breakpoint2;
186c406b 11053 args->thread_num = thread;
f107f563
VP
11054
11055 discard_cleanups (old_chain);
95e54da7
PA
11056 add_continuation (inferior_thread (),
11057 until_break_command_continuation, args,
604ead4a 11058 xfree);
f107f563
VP
11059 }
11060 else
c5aa993b 11061 do_cleanups (old_chain);
c906108c 11062}
ae66c1fc 11063
c906108c
SS
11064/* This function attempts to parse an optional "if <cond>" clause
11065 from the arg string. If one is not found, it returns NULL.
c5aa993b 11066
c906108c
SS
11067 Else, it returns a pointer to the condition string. (It does not
11068 attempt to evaluate the string against a particular block.) And,
11069 it updates arg to point to the first character following the parsed
4a64f543 11070 if clause in the arg string. */
53a5351d 11071
c906108c 11072static char *
fba45db2 11073ep_parse_optional_if_clause (char **arg)
c906108c 11074{
c5aa993b
JM
11075 char *cond_string;
11076
11077 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11078 return NULL;
c5aa993b 11079
4a64f543 11080 /* Skip the "if" keyword. */
c906108c 11081 (*arg) += 2;
c5aa993b 11082
c906108c 11083 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11084 condition string. */
e9cafbcc 11085 *arg = skip_spaces (*arg);
c906108c 11086 cond_string = *arg;
c5aa993b 11087
4a64f543
MS
11088 /* Assume that the condition occupies the remainder of the arg
11089 string. */
c906108c 11090 (*arg) += strlen (cond_string);
c5aa993b 11091
c906108c
SS
11092 return cond_string;
11093}
c5aa993b 11094
c906108c
SS
11095/* Commands to deal with catching events, such as signals, exceptions,
11096 process start/exit, etc. */
c5aa993b
JM
11097
11098typedef enum
11099{
44feb3ce
TT
11100 catch_fork_temporary, catch_vfork_temporary,
11101 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11102}
11103catch_fork_kind;
11104
c906108c 11105static void
cc59ec59
MS
11106catch_fork_command_1 (char *arg, int from_tty,
11107 struct cmd_list_element *command)
c906108c 11108{
a6d9a66e 11109 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11110 char *cond_string = NULL;
44feb3ce
TT
11111 catch_fork_kind fork_kind;
11112 int tempflag;
11113
11114 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11115 tempflag = (fork_kind == catch_fork_temporary
11116 || fork_kind == catch_vfork_temporary);
c5aa993b 11117
44feb3ce
TT
11118 if (!arg)
11119 arg = "";
e9cafbcc 11120 arg = skip_spaces (arg);
c5aa993b 11121
c906108c 11122 /* The allowed syntax is:
c5aa993b
JM
11123 catch [v]fork
11124 catch [v]fork if <cond>
11125
4a64f543 11126 First, check if there's an if clause. */
c906108c 11127 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11128
c906108c 11129 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11130 error (_("Junk at end of arguments."));
c5aa993b 11131
c906108c 11132 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11133 and enable reporting of such events. */
c5aa993b
JM
11134 switch (fork_kind)
11135 {
44feb3ce
TT
11136 case catch_fork_temporary:
11137 case catch_fork_permanent:
a6d9a66e 11138 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11139 &catch_fork_breakpoint_ops);
c906108c 11140 break;
44feb3ce
TT
11141 case catch_vfork_temporary:
11142 case catch_vfork_permanent:
a6d9a66e 11143 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11144 &catch_vfork_breakpoint_ops);
c906108c 11145 break;
c5aa993b 11146 default:
8a3fe4f8 11147 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11148 break;
c5aa993b 11149 }
c906108c
SS
11150}
11151
11152static void
cc59ec59
MS
11153catch_exec_command_1 (char *arg, int from_tty,
11154 struct cmd_list_element *command)
c906108c 11155{
b4d90040 11156 struct exec_catchpoint *c;
a6d9a66e 11157 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11158 int tempflag;
c5aa993b 11159 char *cond_string = NULL;
c906108c 11160
44feb3ce
TT
11161 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11162
11163 if (!arg)
11164 arg = "";
e9cafbcc 11165 arg = skip_spaces (arg);
c906108c
SS
11166
11167 /* The allowed syntax is:
c5aa993b
JM
11168 catch exec
11169 catch exec if <cond>
c906108c 11170
4a64f543 11171 First, check if there's an if clause. */
c906108c
SS
11172 cond_string = ep_parse_optional_if_clause (&arg);
11173
11174 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11175 error (_("Junk at end of arguments."));
c906108c 11176
b4d90040
PA
11177 c = XNEW (struct exec_catchpoint);
11178 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11179 &catch_exec_breakpoint_ops);
11180 c->exec_pathname = NULL;
11181
3ea46bff 11182 install_breakpoint (0, &c->base, 1);
c906108c 11183}
c5aa993b 11184
3086aeae 11185static enum print_stop_action
348d480f 11186print_it_exception_catchpoint (bpstat bs)
3086aeae 11187{
79a45e25 11188 struct ui_out *uiout = current_uiout;
348d480f 11189 struct breakpoint *b = bs->breakpoint_at;
ade92717 11190 int bp_temp, bp_throw;
3086aeae 11191
ade92717 11192 annotate_catchpoint (b->number);
3086aeae 11193
ade92717
AR
11194 bp_throw = strstr (b->addr_string, "throw") != NULL;
11195 if (b->loc->address != b->loc->requested_address)
11196 breakpoint_adjustment_warning (b->loc->requested_address,
11197 b->loc->address,
11198 b->number, 1);
df2b6d2d 11199 bp_temp = b->disposition == disp_del;
ade92717
AR
11200 ui_out_text (uiout,
11201 bp_temp ? "Temporary catchpoint "
11202 : "Catchpoint ");
11203 if (!ui_out_is_mi_like_p (uiout))
11204 ui_out_field_int (uiout, "bkptno", b->number);
11205 ui_out_text (uiout,
c0b37c48
AR
11206 bp_throw ? " (exception thrown), "
11207 : " (exception caught), ");
ade92717
AR
11208 if (ui_out_is_mi_like_p (uiout))
11209 {
11210 ui_out_field_string (uiout, "reason",
11211 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11212 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
11213 ui_out_field_int (uiout, "bkptno", b->number);
11214 }
3086aeae
DJ
11215 return PRINT_SRC_AND_LOC;
11216}
11217
11218static void
cc59ec59
MS
11219print_one_exception_catchpoint (struct breakpoint *b,
11220 struct bp_location **last_loc)
3086aeae 11221{
79a45b7d 11222 struct value_print_options opts;
79a45e25 11223 struct ui_out *uiout = current_uiout;
cc59ec59 11224
79a45b7d
TT
11225 get_user_print_options (&opts);
11226 if (opts.addressprint)
3086aeae
DJ
11227 {
11228 annotate_field (4);
604133b5
AR
11229 if (b->loc == NULL || b->loc->shlib_disabled)
11230 ui_out_field_string (uiout, "addr", "<PENDING>");
11231 else
5af949e3
UW
11232 ui_out_field_core_addr (uiout, "addr",
11233 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
11234 }
11235 annotate_field (5);
604133b5 11236 if (b->loc)
a6d9a66e 11237 *last_loc = b->loc;
3086aeae
DJ
11238 if (strstr (b->addr_string, "throw") != NULL)
11239 ui_out_field_string (uiout, "what", "exception throw");
11240 else
11241 ui_out_field_string (uiout, "what", "exception catch");
11242}
11243
11244static void
11245print_mention_exception_catchpoint (struct breakpoint *b)
11246{
79a45e25 11247 struct ui_out *uiout = current_uiout;
ade92717
AR
11248 int bp_temp;
11249 int bp_throw;
11250
df2b6d2d 11251 bp_temp = b->disposition == disp_del;
ade92717
AR
11252 bp_throw = strstr (b->addr_string, "throw") != NULL;
11253 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
11254 : _("Catchpoint "));
11255 ui_out_field_int (uiout, "bkptno", b->number);
11256 ui_out_text (uiout, bp_throw ? _(" (throw)")
11257 : _(" (catch)"));
3086aeae
DJ
11258}
11259
6149aea9
PA
11260/* Implement the "print_recreate" breakpoint_ops method for throw and
11261 catch catchpoints. */
11262
11263static void
4a64f543
MS
11264print_recreate_exception_catchpoint (struct breakpoint *b,
11265 struct ui_file *fp)
6149aea9
PA
11266{
11267 int bp_temp;
11268 int bp_throw;
11269
11270 bp_temp = b->disposition == disp_del;
11271 bp_throw = strstr (b->addr_string, "throw") != NULL;
11272 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
11273 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 11274 print_recreate_thread (b, fp);
6149aea9
PA
11275}
11276
2060206e 11277static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
11278
11279static int
11280handle_gnu_v3_exceptions (int tempflag, char *cond_string,
11281 enum exception_event_kind ex_event, int from_tty)
11282{
604133b5
AR
11283 char *trigger_func_name;
11284
3086aeae 11285 if (ex_event == EX_EVENT_CATCH)
604133b5 11286 trigger_func_name = "__cxa_begin_catch";
3086aeae 11287 else
604133b5 11288 trigger_func_name = "__cxa_throw";
3086aeae 11289
8cdf0e15 11290 create_breakpoint (get_current_arch (),
e7e0cddf 11291 trigger_func_name, cond_string, -1, NULL,
8cdf0e15 11292 0 /* condition and thread are valid. */,
0fb4aa4b 11293 tempflag, bp_breakpoint,
8cdf0e15
VP
11294 0,
11295 AUTO_BOOLEAN_TRUE /* pending */,
11296 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe 11297 1 /* enabled */,
44f238bb
PA
11298 0 /* internal */,
11299 0);
3086aeae 11300
3086aeae
DJ
11301 return 1;
11302}
11303
4a64f543 11304/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
11305
11306static void
fba45db2
KB
11307catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
11308 int tempflag, int from_tty)
c906108c 11309{
c5aa993b 11310 char *cond_string = NULL;
c5aa993b 11311
44feb3ce
TT
11312 if (!arg)
11313 arg = "";
e9cafbcc 11314 arg = skip_spaces (arg);
c5aa993b 11315
c906108c
SS
11316 cond_string = ep_parse_optional_if_clause (&arg);
11317
11318 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11319 error (_("Junk at end of arguments."));
c906108c 11320
059fb39f
PM
11321 if (ex_event != EX_EVENT_THROW
11322 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 11323 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 11324
3086aeae
DJ
11325 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
11326 return;
11327
8a3fe4f8 11328 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
11329}
11330
44feb3ce
TT
11331/* Implementation of "catch catch" command. */
11332
11333static void
11334catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
11335{
11336 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 11337
44feb3ce
TT
11338 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
11339}
11340
11341/* Implementation of "catch throw" command. */
11342
11343static void
11344catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
11345{
11346 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 11347
44feb3ce
TT
11348 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
11349}
11350
9ac4176b 11351void
28010a5d
PA
11352init_ada_exception_breakpoint (struct breakpoint *b,
11353 struct gdbarch *gdbarch,
11354 struct symtab_and_line sal,
11355 char *addr_string,
c0a91b2b 11356 const struct breakpoint_ops *ops,
28010a5d
PA
11357 int tempflag,
11358 int from_tty)
f7f9143b 11359{
f7f9143b
JB
11360 if (from_tty)
11361 {
5af949e3
UW
11362 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11363 if (!loc_gdbarch)
11364 loc_gdbarch = gdbarch;
11365
6c95b8df
PA
11366 describe_other_breakpoints (loc_gdbarch,
11367 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11368 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11369 version for exception catchpoints, because two catchpoints
11370 used for different exception names will use the same address.
11371 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11372 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11373 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11374 the user what type of catchpoint it is. The above is good
11375 enough for now, though. */
11376 }
11377
28010a5d 11378 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
11379
11380 b->enable_state = bp_enabled;
11381 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11382 b->addr_string = addr_string;
11383 b->language = language_ada;
f7f9143b
JB
11384}
11385
a96d9b2e
SDJ
11386/* Splits the argument using space as delimiter. Returns an xmalloc'd
11387 filter list, or NULL if no filtering is required. */
11388static VEC(int) *
11389catch_syscall_split_args (char *arg)
11390{
11391 VEC(int) *result = NULL;
29d0bb3d 11392 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
11393
11394 while (*arg != '\0')
11395 {
11396 int i, syscall_number;
11397 char *endptr;
11398 char cur_name[128];
11399 struct syscall s;
11400
11401 /* Skip whitespace. */
11402 while (isspace (*arg))
11403 arg++;
11404
11405 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11406 cur_name[i] = arg[i];
11407 cur_name[i] = '\0';
11408 arg += i;
11409
11410 /* Check if the user provided a syscall name or a number. */
11411 syscall_number = (int) strtol (cur_name, &endptr, 0);
11412 if (*endptr == '\0')
bccd0dd2 11413 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
11414 else
11415 {
11416 /* We have a name. Let's check if it's valid and convert it
11417 to a number. */
11418 get_syscall_by_name (cur_name, &s);
11419
11420 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
11421 /* Here we have to issue an error instead of a warning,
11422 because GDB cannot do anything useful if there's no
11423 syscall number to be caught. */
a96d9b2e
SDJ
11424 error (_("Unknown syscall name '%s'."), cur_name);
11425 }
11426
11427 /* Ok, it's valid. */
11428 VEC_safe_push (int, result, s.number);
11429 }
11430
11431 discard_cleanups (cleanup);
11432 return result;
11433}
11434
11435/* Implement the "catch syscall" command. */
11436
11437static void
cc59ec59
MS
11438catch_syscall_command_1 (char *arg, int from_tty,
11439 struct cmd_list_element *command)
a96d9b2e
SDJ
11440{
11441 int tempflag;
11442 VEC(int) *filter;
11443 struct syscall s;
11444 struct gdbarch *gdbarch = get_current_arch ();
11445
11446 /* Checking if the feature if supported. */
11447 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11448 error (_("The feature 'catch syscall' is not supported on \
ea666128 11449this architecture yet."));
a96d9b2e
SDJ
11450
11451 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11452
e9cafbcc 11453 arg = skip_spaces (arg);
a96d9b2e
SDJ
11454
11455 /* We need to do this first "dummy" translation in order
11456 to get the syscall XML file loaded or, most important,
11457 to display a warning to the user if there's no XML file
11458 for his/her architecture. */
11459 get_syscall_by_number (0, &s);
11460
11461 /* The allowed syntax is:
11462 catch syscall
11463 catch syscall <name | number> [<name | number> ... <name | number>]
11464
11465 Let's check if there's a syscall name. */
11466
11467 if (arg != NULL)
11468 filter = catch_syscall_split_args (arg);
11469 else
11470 filter = NULL;
11471
11472 create_syscall_event_catchpoint (tempflag, filter,
11473 &catch_syscall_breakpoint_ops);
11474}
11475
c906108c 11476static void
fba45db2 11477catch_command (char *arg, int from_tty)
c906108c 11478{
44feb3ce 11479 error (_("Catch requires an event name."));
c906108c
SS
11480}
11481\f
11482
11483static void
fba45db2 11484tcatch_command (char *arg, int from_tty)
c906108c 11485{
44feb3ce 11486 error (_("Catch requires an event name."));
c906108c
SS
11487}
11488
8a2c437b
TT
11489/* A qsort comparison function that sorts breakpoints in order. */
11490
11491static int
11492compare_breakpoints (const void *a, const void *b)
11493{
11494 const breakpoint_p *ba = a;
11495 uintptr_t ua = (uintptr_t) *ba;
11496 const breakpoint_p *bb = b;
11497 uintptr_t ub = (uintptr_t) *bb;
11498
11499 if ((*ba)->number < (*bb)->number)
11500 return -1;
11501 else if ((*ba)->number > (*bb)->number)
11502 return 1;
11503
11504 /* Now sort by address, in case we see, e..g, two breakpoints with
11505 the number 0. */
11506 if (ua < ub)
11507 return -1;
11508 return ub > ub ? 1 : 0;
11509}
11510
80f8a6eb 11511/* Delete breakpoints by address or line. */
c906108c
SS
11512
11513static void
fba45db2 11514clear_command (char *arg, int from_tty)
c906108c 11515{
8a2c437b 11516 struct breakpoint *b, *prev;
d6e956e5
VP
11517 VEC(breakpoint_p) *found = 0;
11518 int ix;
c906108c
SS
11519 int default_match;
11520 struct symtabs_and_lines sals;
11521 struct symtab_and_line sal;
c906108c 11522 int i;
8a2c437b 11523 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11524
11525 if (arg)
11526 {
39cf75f7
DE
11527 sals = decode_line_with_current_source (arg,
11528 (DECODE_LINE_FUNFIRSTLINE
11529 | DECODE_LINE_LIST_MODE));
c906108c
SS
11530 default_match = 0;
11531 }
11532 else
11533 {
c5aa993b 11534 sals.sals = (struct symtab_and_line *)
c906108c 11535 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 11536 make_cleanup (xfree, sals.sals);
4a64f543 11537 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11538
11539 /* Set sal's line, symtab, pc, and pspace to the values
11540 corresponding to the last call to print_frame_info. If the
11541 codepoint is not valid, this will set all the fields to 0. */
11542 get_last_displayed_sal (&sal);
c906108c 11543 if (sal.symtab == 0)
8a3fe4f8 11544 error (_("No source file specified."));
c906108c
SS
11545
11546 sals.sals[0] = sal;
11547 sals.nelts = 1;
11548
11549 default_match = 1;
11550 }
11551
4a64f543
MS
11552 /* We don't call resolve_sal_pc here. That's not as bad as it
11553 seems, because all existing breakpoints typically have both
11554 file/line and pc set. So, if clear is given file/line, we can
11555 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11556
11557 We only support clearing given the address explicitly
11558 present in breakpoint table. Say, we've set breakpoint
4a64f543 11559 at file:line. There were several PC values for that file:line,
ed0616c6 11560 due to optimization, all in one block.
4a64f543
MS
11561
11562 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11563 PC corresponding to the same file:line, the breakpoint won't
11564 be cleared. We probably can still clear the breakpoint, but
11565 since the other PC value is never presented to user, user
11566 can only find it by guessing, and it does not seem important
11567 to support that. */
11568
4a64f543
MS
11569 /* For each line spec given, delete bps which correspond to it. Do
11570 it in two passes, solely to preserve the current behavior that
11571 from_tty is forced true if we delete more than one
11572 breakpoint. */
c906108c 11573
80f8a6eb 11574 found = NULL;
8a2c437b 11575 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11576 for (i = 0; i < sals.nelts; i++)
11577 {
4aac40c8
TT
11578 int is_abs, sal_name_len;
11579
c906108c 11580 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11581 If line given (pc == 0), clear all bpts on specified line.
11582 If defaulting, clear all bpts on default line
c906108c 11583 or at default pc.
c5aa993b
JM
11584
11585 defaulting sal.pc != 0 tests to do
11586
11587 0 1 pc
11588 1 1 pc _and_ line
11589 0 0 line
11590 1 0 <can't happen> */
c906108c
SS
11591
11592 sal = sals.sals[i];
4aac40c8
TT
11593 is_abs = sal.symtab == NULL ? 1 : IS_ABSOLUTE_PATH (sal.symtab->filename);
11594 sal_name_len = is_abs ? 0 : strlen (sal.symtab->filename);
c906108c 11595
4a64f543 11596 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11597 ALL_BREAKPOINTS (b)
c5aa993b 11598 {
0d381245 11599 int match = 0;
4a64f543 11600 /* Are we going to delete b? */
cc60f2e3 11601 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11602 {
11603 struct bp_location *loc = b->loc;
11604 for (; loc; loc = loc->next)
11605 {
f8eba3c6
TT
11606 /* If the user specified file:line, don't allow a PC
11607 match. This matches historical gdb behavior. */
11608 int pc_match = (!sal.explicit_line
11609 && sal.pc
11610 && (loc->pspace == sal.pspace)
11611 && (loc->address == sal.pc)
11612 && (!section_is_overlay (loc->section)
11613 || loc->section == sal.section));
4aac40c8
TT
11614 int line_match = 0;
11615
11616 if ((default_match || sal.explicit_line)
11617 && loc->source_file != NULL
11618 && sal.symtab != NULL
11619 && sal.pspace == loc->pspace
11620 && loc->line_number == sal.line)
11621 {
11622 if (filename_cmp (loc->source_file,
11623 sal.symtab->filename) == 0)
11624 line_match = 1;
11625 else if (!IS_ABSOLUTE_PATH (sal.symtab->filename)
11626 && compare_filenames_for_search (loc->source_file,
11627 sal.symtab->filename,
11628 sal_name_len))
11629 line_match = 1;
11630 }
11631
0d381245
VP
11632 if (pc_match || line_match)
11633 {
11634 match = 1;
11635 break;
11636 }
11637 }
11638 }
11639
11640 if (match)
d6e956e5 11641 VEC_safe_push(breakpoint_p, found, b);
c906108c 11642 }
80f8a6eb 11643 }
8a2c437b 11644
80f8a6eb 11645 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11646 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11647 {
11648 if (arg)
8a3fe4f8 11649 error (_("No breakpoint at %s."), arg);
80f8a6eb 11650 else
8a3fe4f8 11651 error (_("No breakpoint at this line."));
80f8a6eb 11652 }
c906108c 11653
8a2c437b
TT
11654 /* Remove duplicates from the vec. */
11655 qsort (VEC_address (breakpoint_p, found),
11656 VEC_length (breakpoint_p, found),
11657 sizeof (breakpoint_p),
11658 compare_breakpoints);
11659 prev = VEC_index (breakpoint_p, found, 0);
11660 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11661 {
11662 if (b == prev)
11663 {
11664 VEC_ordered_remove (breakpoint_p, found, ix);
11665 --ix;
11666 }
11667 }
11668
d6e956e5 11669 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11670 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11671 if (from_tty)
a3f17187 11672 {
d6e956e5 11673 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11674 printf_unfiltered (_("Deleted breakpoint "));
11675 else
11676 printf_unfiltered (_("Deleted breakpoints "));
11677 }
80f8a6eb 11678 breakpoints_changed ();
d6e956e5
VP
11679
11680 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11681 {
c5aa993b 11682 if (from_tty)
d6e956e5
VP
11683 printf_unfiltered ("%d ", b->number);
11684 delete_breakpoint (b);
c906108c 11685 }
80f8a6eb
MS
11686 if (from_tty)
11687 putchar_unfiltered ('\n');
8a2c437b
TT
11688
11689 do_cleanups (cleanups);
c906108c
SS
11690}
11691\f
11692/* Delete breakpoint in BS if they are `delete' breakpoints and
11693 all breakpoints that are marked for deletion, whether hit or not.
11694 This is called after any breakpoint is hit, or after errors. */
11695
11696void
fba45db2 11697breakpoint_auto_delete (bpstat bs)
c906108c 11698{
35df4500 11699 struct breakpoint *b, *b_tmp;
c906108c
SS
11700
11701 for (; bs; bs = bs->next)
f431efe5
PA
11702 if (bs->breakpoint_at
11703 && bs->breakpoint_at->disposition == disp_del
c906108c 11704 && bs->stop)
f431efe5 11705 delete_breakpoint (bs->breakpoint_at);
c906108c 11706
35df4500 11707 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11708 {
b5de0fa7 11709 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11710 delete_breakpoint (b);
11711 }
c906108c
SS
11712}
11713
4a64f543
MS
11714/* A comparison function for bp_location AP and BP being interfaced to
11715 qsort. Sort elements primarily by their ADDRESS (no matter what
11716 does breakpoint_address_is_meaningful say for its OWNER),
11717 secondarily by ordering first bp_permanent OWNERed elements and
11718 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11719 qsort being an unstable algorithm. */
876fa593
JK
11720
11721static int
494cfb0f 11722bp_location_compare (const void *ap, const void *bp)
876fa593 11723{
494cfb0f
JK
11724 struct bp_location *a = *(void **) ap;
11725 struct bp_location *b = *(void **) bp;
2bdf28a0 11726 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
11727 int a_perm = a->owner->enable_state == bp_permanent;
11728 int b_perm = b->owner->enable_state == bp_permanent;
11729
11730 if (a->address != b->address)
11731 return (a->address > b->address) - (a->address < b->address);
11732
dea2aa5f
LM
11733 /* Sort locations at the same address by their pspace number, keeping
11734 locations of the same inferior (in a multi-inferior environment)
11735 grouped. */
11736
11737 if (a->pspace->num != b->pspace->num)
11738 return ((a->pspace->num > b->pspace->num)
11739 - (a->pspace->num < b->pspace->num));
11740
876fa593
JK
11741 /* Sort permanent breakpoints first. */
11742 if (a_perm != b_perm)
11743 return (a_perm < b_perm) - (a_perm > b_perm);
11744
c56a97f9
JK
11745 /* Make the internal GDB representation stable across GDB runs
11746 where A and B memory inside GDB can differ. Breakpoint locations of
11747 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11748
11749 if (a->owner->number != b->owner->number)
c56a97f9
JK
11750 return ((a->owner->number > b->owner->number)
11751 - (a->owner->number < b->owner->number));
876fa593
JK
11752
11753 return (a > b) - (a < b);
11754}
11755
876fa593 11756/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
11757 bp_location_shadow_len_after_address_max according to the current
11758 content of the bp_location array. */
f7545552
TT
11759
11760static void
876fa593 11761bp_location_target_extensions_update (void)
f7545552 11762{
876fa593
JK
11763 struct bp_location *bl, **blp_tmp;
11764
11765 bp_location_placed_address_before_address_max = 0;
11766 bp_location_shadow_len_after_address_max = 0;
11767
11768 ALL_BP_LOCATIONS (bl, blp_tmp)
11769 {
11770 CORE_ADDR start, end, addr;
11771
11772 if (!bp_location_has_shadow (bl))
11773 continue;
11774
11775 start = bl->target_info.placed_address;
11776 end = start + bl->target_info.shadow_len;
11777
11778 gdb_assert (bl->address >= start);
11779 addr = bl->address - start;
11780 if (addr > bp_location_placed_address_before_address_max)
11781 bp_location_placed_address_before_address_max = addr;
11782
11783 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11784
11785 gdb_assert (bl->address < end);
11786 addr = end - bl->address;
11787 if (addr > bp_location_shadow_len_after_address_max)
11788 bp_location_shadow_len_after_address_max = addr;
11789 }
f7545552
TT
11790}
11791
1e4d1764
YQ
11792/* Download tracepoint locations if they haven't been. */
11793
11794static void
11795download_tracepoint_locations (void)
11796{
11797 struct bp_location *bl, **blp_tmp;
11798 struct cleanup *old_chain;
11799
11800 if (!target_can_download_tracepoint ())
11801 return;
11802
11803 old_chain = save_current_space_and_thread ();
11804
11805 ALL_BP_LOCATIONS (bl, blp_tmp)
11806 {
11807 struct tracepoint *t;
11808
11809 if (!is_tracepoint (bl->owner))
11810 continue;
11811
11812 if ((bl->owner->type == bp_fast_tracepoint
11813 ? !may_insert_fast_tracepoints
11814 : !may_insert_tracepoints))
11815 continue;
11816
11817 /* In tracepoint, locations are _never_ duplicated, so
11818 should_be_inserted is equivalent to
11819 unduplicated_should_be_inserted. */
11820 if (!should_be_inserted (bl) || bl->inserted)
11821 continue;
11822
11823 switch_to_program_space_and_thread (bl->pspace);
11824
11825 target_download_tracepoint (bl);
11826
11827 bl->inserted = 1;
11828 t = (struct tracepoint *) bl->owner;
11829 t->number_on_target = bl->owner->number;
11830 }
11831
11832 do_cleanups (old_chain);
11833}
11834
934709f0
PW
11835/* Swap the insertion/duplication state between two locations. */
11836
11837static void
11838swap_insertion (struct bp_location *left, struct bp_location *right)
11839{
11840 const int left_inserted = left->inserted;
11841 const int left_duplicate = left->duplicate;
b775012e 11842 const int left_needs_update = left->needs_update;
934709f0
PW
11843 const struct bp_target_info left_target_info = left->target_info;
11844
1e4d1764
YQ
11845 /* Locations of tracepoints can never be duplicated. */
11846 if (is_tracepoint (left->owner))
11847 gdb_assert (!left->duplicate);
11848 if (is_tracepoint (right->owner))
11849 gdb_assert (!right->duplicate);
11850
934709f0
PW
11851 left->inserted = right->inserted;
11852 left->duplicate = right->duplicate;
b775012e 11853 left->needs_update = right->needs_update;
934709f0
PW
11854 left->target_info = right->target_info;
11855 right->inserted = left_inserted;
11856 right->duplicate = left_duplicate;
b775012e 11857 right->needs_update = left_needs_update;
934709f0
PW
11858 right->target_info = left_target_info;
11859}
11860
b775012e
LM
11861/* Force the re-insertion of the locations at ADDRESS. This is called
11862 once a new/deleted/modified duplicate location is found and we are evaluating
11863 conditions on the target's side. Such conditions need to be updated on
11864 the target. */
11865
11866static void
11867force_breakpoint_reinsertion (struct bp_location *bl)
11868{
11869 struct bp_location **locp = NULL, **loc2p;
11870 struct bp_location *loc;
11871 CORE_ADDR address = 0;
11872 int pspace_num;
11873
11874 address = bl->address;
11875 pspace_num = bl->pspace->num;
11876
11877 /* This is only meaningful if the target is
11878 evaluating conditions and if the user has
11879 opted for condition evaluation on the target's
11880 side. */
11881 if (gdb_evaluates_breakpoint_condition_p ()
11882 || !target_supports_evaluation_of_breakpoint_conditions ())
11883 return;
11884
11885 /* Flag all breakpoint locations with this address and
11886 the same program space as the location
11887 as "its condition has changed". We need to
11888 update the conditions on the target's side. */
11889 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11890 {
11891 loc = *loc2p;
11892
11893 if (!is_breakpoint (loc->owner)
11894 || pspace_num != loc->pspace->num)
11895 continue;
11896
11897 /* Flag the location appropriately. We use a different state to
11898 let everyone know that we already updated the set of locations
11899 with addr bl->address and program space bl->pspace. This is so
11900 we don't have to keep calling these functions just to mark locations
11901 that have already been marked. */
11902 loc->condition_changed = condition_updated;
11903
11904 /* Free the agent expression bytecode as well. We will compute
11905 it later on. */
11906 if (loc->cond_bytecode)
11907 {
11908 free_agent_expr (loc->cond_bytecode);
11909 loc->cond_bytecode = NULL;
11910 }
11911 }
11912}
11913
4cd9bd08 11914/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
11915 into the inferior, only remove already-inserted locations that no
11916 longer should be inserted. Functions that delete a breakpoint or
11917 breakpoints should pass false, so that deleting a breakpoint
11918 doesn't have the side effect of inserting the locations of other
11919 breakpoints that are marked not-inserted, but should_be_inserted
11920 returns true on them.
11921
11922 This behaviour is useful is situations close to tear-down -- e.g.,
11923 after an exec, while the target still has execution, but breakpoint
11924 shadows of the previous executable image should *NOT* be restored
11925 to the new image; or before detaching, where the target still has
11926 execution and wants to delete breakpoints from GDB's lists, and all
11927 breakpoints had already been removed from the inferior. */
11928
0d381245 11929static void
b60e7edf 11930update_global_location_list (int should_insert)
0d381245 11931{
74960c60 11932 struct breakpoint *b;
876fa593 11933 struct bp_location **locp, *loc;
f7545552 11934 struct cleanup *cleanups;
b775012e
LM
11935 /* Last breakpoint location address that was marked for update. */
11936 CORE_ADDR last_addr = 0;
11937 /* Last breakpoint location program space that was marked for update. */
11938 int last_pspace_num = -1;
f7545552 11939
2d134ed3
PA
11940 /* Used in the duplicates detection below. When iterating over all
11941 bp_locations, points to the first bp_location of a given address.
11942 Breakpoints and watchpoints of different types are never
11943 duplicates of each other. Keep one pointer for each type of
11944 breakpoint/watchpoint, so we only need to loop over all locations
11945 once. */
11946 struct bp_location *bp_loc_first; /* breakpoint */
11947 struct bp_location *wp_loc_first; /* hardware watchpoint */
11948 struct bp_location *awp_loc_first; /* access watchpoint */
11949 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11950
4a64f543
MS
11951 /* Saved former bp_location array which we compare against the newly
11952 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
11953 struct bp_location **old_location, **old_locp;
11954 unsigned old_location_count;
11955
11956 old_location = bp_location;
11957 old_location_count = bp_location_count;
11958 bp_location = NULL;
11959 bp_location_count = 0;
11960 cleanups = make_cleanup (xfree, old_location);
0d381245 11961
74960c60 11962 ALL_BREAKPOINTS (b)
876fa593
JK
11963 for (loc = b->loc; loc; loc = loc->next)
11964 bp_location_count++;
11965
11966 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
11967 locp = bp_location;
11968 ALL_BREAKPOINTS (b)
11969 for (loc = b->loc; loc; loc = loc->next)
11970 *locp++ = loc;
11971 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 11972 bp_location_compare);
876fa593
JK
11973
11974 bp_location_target_extensions_update ();
74960c60 11975
4a64f543
MS
11976 /* Identify bp_location instances that are no longer present in the
11977 new list, and therefore should be freed. Note that it's not
11978 necessary that those locations should be removed from inferior --
11979 if there's another location at the same address (previously
11980 marked as duplicate), we don't need to remove/insert the
11981 location.
876fa593 11982
4a64f543
MS
11983 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11984 and former bp_location array state respectively. */
876fa593
JK
11985
11986 locp = bp_location;
11987 for (old_locp = old_location; old_locp < old_location + old_location_count;
11988 old_locp++)
74960c60 11989 {
876fa593 11990 struct bp_location *old_loc = *old_locp;
c7d46a38 11991 struct bp_location **loc2p;
876fa593 11992
e5dd4106 11993 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11994 not, we have to free it. */
c7d46a38 11995 int found_object = 0;
20874c92
VP
11996 /* Tells if the location should remain inserted in the target. */
11997 int keep_in_target = 0;
11998 int removed = 0;
876fa593 11999
4a64f543
MS
12000 /* Skip LOCP entries which will definitely never be needed.
12001 Stop either at or being the one matching OLD_LOC. */
876fa593 12002 while (locp < bp_location + bp_location_count
c7d46a38 12003 && (*locp)->address < old_loc->address)
876fa593 12004 locp++;
c7d46a38
PA
12005
12006 for (loc2p = locp;
12007 (loc2p < bp_location + bp_location_count
12008 && (*loc2p)->address == old_loc->address);
12009 loc2p++)
12010 {
b775012e
LM
12011 /* Check if this is a new/duplicated location or a duplicated
12012 location that had its condition modified. If so, we want to send
12013 its condition to the target if evaluation of conditions is taking
12014 place there. */
12015 if ((*loc2p)->condition_changed == condition_modified
12016 && (last_addr != old_loc->address
12017 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12018 {
b775012e
LM
12019 force_breakpoint_reinsertion (*loc2p);
12020 last_pspace_num = old_loc->pspace->num;
c7d46a38 12021 }
b775012e
LM
12022
12023 if (*loc2p == old_loc)
12024 found_object = 1;
c7d46a38 12025 }
74960c60 12026
b775012e
LM
12027 /* We have already handled this address, update it so that we don't
12028 have to go through updates again. */
12029 last_addr = old_loc->address;
12030
12031 /* Target-side condition evaluation: Handle deleted locations. */
12032 if (!found_object)
12033 force_breakpoint_reinsertion (old_loc);
12034
4a64f543
MS
12035 /* If this location is no longer present, and inserted, look if
12036 there's maybe a new location at the same address. If so,
12037 mark that one inserted, and don't remove this one. This is
12038 needed so that we don't have a time window where a breakpoint
12039 at certain location is not inserted. */
74960c60 12040
876fa593 12041 if (old_loc->inserted)
0d381245 12042 {
4a64f543
MS
12043 /* If the location is inserted now, we might have to remove
12044 it. */
74960c60 12045
876fa593 12046 if (found_object && should_be_inserted (old_loc))
74960c60 12047 {
4a64f543
MS
12048 /* The location is still present in the location list,
12049 and still should be inserted. Don't do anything. */
20874c92 12050 keep_in_target = 1;
74960c60
VP
12051 }
12052 else
12053 {
b775012e
LM
12054 /* This location still exists, but it won't be kept in the
12055 target since it may have been disabled. We proceed to
12056 remove its target-side condition. */
12057
4a64f543
MS
12058 /* The location is either no longer present, or got
12059 disabled. See if there's another location at the
12060 same address, in which case we don't need to remove
12061 this one from the target. */
876fa593 12062
2bdf28a0 12063 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12064 if (breakpoint_address_is_meaningful (old_loc->owner))
12065 {
876fa593 12066 for (loc2p = locp;
c7d46a38
PA
12067 (loc2p < bp_location + bp_location_count
12068 && (*loc2p)->address == old_loc->address);
876fa593
JK
12069 loc2p++)
12070 {
12071 struct bp_location *loc2 = *loc2p;
12072
2d134ed3 12073 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12074 {
85d721b8
PA
12075 /* Read watchpoint locations are switched to
12076 access watchpoints, if the former are not
12077 supported, but the latter are. */
12078 if (is_hardware_watchpoint (old_loc->owner))
12079 {
12080 gdb_assert (is_hardware_watchpoint (loc2->owner));
12081 loc2->watchpoint_type = old_loc->watchpoint_type;
12082 }
12083
934709f0
PW
12084 /* loc2 is a duplicated location. We need to check
12085 if it should be inserted in case it will be
12086 unduplicated. */
12087 if (loc2 != old_loc
12088 && unduplicated_should_be_inserted (loc2))
c7d46a38 12089 {
934709f0 12090 swap_insertion (old_loc, loc2);
c7d46a38
PA
12091 keep_in_target = 1;
12092 break;
12093 }
876fa593
JK
12094 }
12095 }
12096 }
74960c60
VP
12097 }
12098
20874c92
VP
12099 if (!keep_in_target)
12100 {
876fa593 12101 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12102 {
4a64f543
MS
12103 /* This is just about all we can do. We could keep
12104 this location on the global list, and try to
12105 remove it next time, but there's no particular
12106 reason why we will succeed next time.
20874c92 12107
4a64f543
MS
12108 Note that at this point, old_loc->owner is still
12109 valid, as delete_breakpoint frees the breakpoint
12110 only after calling us. */
3e43a32a
MS
12111 printf_filtered (_("warning: Error removing "
12112 "breakpoint %d\n"),
876fa593 12113 old_loc->owner->number);
20874c92
VP
12114 }
12115 removed = 1;
12116 }
0d381245 12117 }
74960c60
VP
12118
12119 if (!found_object)
1c5cfe86 12120 {
db82e815
PA
12121 if (removed && non_stop
12122 && breakpoint_address_is_meaningful (old_loc->owner)
12123 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12124 {
db82e815
PA
12125 /* This location was removed from the target. In
12126 non-stop mode, a race condition is possible where
12127 we've removed a breakpoint, but stop events for that
12128 breakpoint are already queued and will arrive later.
12129 We apply an heuristic to be able to distinguish such
12130 SIGTRAPs from other random SIGTRAPs: we keep this
12131 breakpoint location for a bit, and will retire it
12132 after we see some number of events. The theory here
12133 is that reporting of events should, "on the average",
12134 be fair, so after a while we'll see events from all
12135 threads that have anything of interest, and no longer
12136 need to keep this breakpoint location around. We
12137 don't hold locations forever so to reduce chances of
12138 mistaking a non-breakpoint SIGTRAP for a breakpoint
12139 SIGTRAP.
12140
12141 The heuristic failing can be disastrous on
12142 decr_pc_after_break targets.
12143
12144 On decr_pc_after_break targets, like e.g., x86-linux,
12145 if we fail to recognize a late breakpoint SIGTRAP,
12146 because events_till_retirement has reached 0 too
12147 soon, we'll fail to do the PC adjustment, and report
12148 a random SIGTRAP to the user. When the user resumes
12149 the inferior, it will most likely immediately crash
2dec564e 12150 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12151 corrupted, because of being resumed e.g., in the
12152 middle of a multi-byte instruction, or skipped a
12153 one-byte instruction. This was actually seen happen
12154 on native x86-linux, and should be less rare on
12155 targets that do not support new thread events, like
12156 remote, due to the heuristic depending on
12157 thread_count.
12158
12159 Mistaking a random SIGTRAP for a breakpoint trap
12160 causes similar symptoms (PC adjustment applied when
12161 it shouldn't), but then again, playing with SIGTRAPs
12162 behind the debugger's back is asking for trouble.
12163
12164 Since hardware watchpoint traps are always
12165 distinguishable from other traps, so we don't need to
12166 apply keep hardware watchpoint moribund locations
12167 around. We simply always ignore hardware watchpoint
12168 traps we can no longer explain. */
12169
876fa593
JK
12170 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12171 old_loc->owner = NULL;
20874c92 12172
876fa593 12173 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12174 }
12175 else
f431efe5
PA
12176 {
12177 old_loc->owner = NULL;
12178 decref_bp_location (&old_loc);
12179 }
20874c92 12180 }
74960c60 12181 }
1c5cfe86 12182
348d480f
PA
12183 /* Rescan breakpoints at the same address and section, marking the
12184 first one as "first" and any others as "duplicates". This is so
12185 that the bpt instruction is only inserted once. If we have a
12186 permanent breakpoint at the same place as BPT, make that one the
12187 official one, and the rest as duplicates. Permanent breakpoints
12188 are sorted first for the same address.
12189
12190 Do the same for hardware watchpoints, but also considering the
12191 watchpoint's type (regular/access/read) and length. */
12192
12193 bp_loc_first = NULL;
12194 wp_loc_first = NULL;
12195 awp_loc_first = NULL;
12196 rwp_loc_first = NULL;
12197 ALL_BP_LOCATIONS (loc, locp)
12198 {
12199 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12200 non-NULL. */
348d480f 12201 struct bp_location **loc_first_p;
d3fbdd86 12202 b = loc->owner;
348d480f 12203
f8eba3c6 12204 if (!should_be_inserted (loc)
348d480f 12205 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12206 /* Don't detect duplicate for tracepoint locations because they are
12207 never duplicated. See the comments in field `duplicate' of
12208 `struct bp_location'. */
348d480f 12209 || is_tracepoint (b))
b775012e
LM
12210 {
12211 /* Clear the condition modification flag. */
12212 loc->condition_changed = condition_unchanged;
12213 continue;
12214 }
348d480f
PA
12215
12216 /* Permanent breakpoint should always be inserted. */
12217 if (b->enable_state == bp_permanent && ! loc->inserted)
12218 internal_error (__FILE__, __LINE__,
12219 _("allegedly permanent breakpoint is not "
12220 "actually inserted"));
12221
12222 if (b->type == bp_hardware_watchpoint)
12223 loc_first_p = &wp_loc_first;
12224 else if (b->type == bp_read_watchpoint)
12225 loc_first_p = &rwp_loc_first;
12226 else if (b->type == bp_access_watchpoint)
12227 loc_first_p = &awp_loc_first;
12228 else
12229 loc_first_p = &bp_loc_first;
12230
12231 if (*loc_first_p == NULL
12232 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12233 || !breakpoint_locations_match (loc, *loc_first_p))
12234 {
12235 *loc_first_p = loc;
12236 loc->duplicate = 0;
b775012e
LM
12237
12238 if (is_breakpoint (loc->owner) && loc->condition_changed)
12239 {
12240 loc->needs_update = 1;
12241 /* Clear the condition modification flag. */
12242 loc->condition_changed = condition_unchanged;
12243 }
348d480f
PA
12244 continue;
12245 }
12246
934709f0
PW
12247
12248 /* This and the above ensure the invariant that the first location
12249 is not duplicated, and is the inserted one.
12250 All following are marked as duplicated, and are not inserted. */
12251 if (loc->inserted)
12252 swap_insertion (loc, *loc_first_p);
348d480f
PA
12253 loc->duplicate = 1;
12254
b775012e
LM
12255 /* Clear the condition modification flag. */
12256 loc->condition_changed = condition_unchanged;
12257
348d480f
PA
12258 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12259 && b->enable_state != bp_permanent)
12260 internal_error (__FILE__, __LINE__,
12261 _("another breakpoint was inserted on top of "
12262 "a permanent breakpoint"));
12263 }
12264
b775012e 12265 if (breakpoints_always_inserted_mode ()
348d480f
PA
12266 && (have_live_inferiors ()
12267 || (gdbarch_has_global_breakpoints (target_gdbarch))))
b775012e
LM
12268 {
12269 if (should_insert)
12270 insert_breakpoint_locations ();
12271 else
12272 {
12273 /* Though should_insert is false, we may need to update conditions
12274 on the target's side if it is evaluating such conditions. We
12275 only update conditions for locations that are marked
12276 "needs_update". */
12277 update_inserted_breakpoint_locations ();
12278 }
12279 }
348d480f 12280
1e4d1764
YQ
12281 if (should_insert)
12282 download_tracepoint_locations ();
12283
348d480f
PA
12284 do_cleanups (cleanups);
12285}
12286
12287void
12288breakpoint_retire_moribund (void)
12289{
12290 struct bp_location *loc;
12291 int ix;
12292
12293 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12294 if (--(loc->events_till_retirement) == 0)
12295 {
12296 decref_bp_location (&loc);
12297 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12298 --ix;
12299 }
12300}
12301
12302static void
12303update_global_location_list_nothrow (int inserting)
12304{
bfd189b1 12305 volatile struct gdb_exception e;
348d480f
PA
12306
12307 TRY_CATCH (e, RETURN_MASK_ERROR)
12308 update_global_location_list (inserting);
12309}
12310
12311/* Clear BKP from a BPS. */
12312
12313static void
12314bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12315{
12316 bpstat bs;
12317
12318 for (bs = bps; bs; bs = bs->next)
12319 if (bs->breakpoint_at == bpt)
12320 {
12321 bs->breakpoint_at = NULL;
12322 bs->old_val = NULL;
12323 /* bs->commands will be freed later. */
12324 }
12325}
12326
12327/* Callback for iterate_over_threads. */
12328static int
12329bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12330{
12331 struct breakpoint *bpt = data;
12332
12333 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12334 return 0;
12335}
12336
12337/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12338 callbacks. */
12339
12340static void
12341say_where (struct breakpoint *b)
12342{
79a45e25 12343 struct ui_out *uiout = current_uiout;
348d480f
PA
12344 struct value_print_options opts;
12345
12346 get_user_print_options (&opts);
12347
12348 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12349 single string. */
12350 if (b->loc == NULL)
12351 {
12352 printf_filtered (_(" (%s) pending."), b->addr_string);
12353 }
12354 else
12355 {
f8eba3c6 12356 if (opts.addressprint || b->loc->source_file == NULL)
348d480f
PA
12357 {
12358 printf_filtered (" at ");
12359 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12360 gdb_stdout);
12361 }
f8eba3c6
TT
12362 if (b->loc->source_file)
12363 {
12364 /* If there is a single location, we can print the location
12365 more nicely. */
12366 if (b->loc->next == NULL)
12367 printf_filtered (": file %s, line %d.",
12368 b->loc->source_file, b->loc->line_number);
12369 else
12370 /* This is not ideal, but each location may have a
12371 different file name, and this at least reflects the
12372 real situation somewhat. */
12373 printf_filtered (": %s.", b->addr_string);
12374 }
348d480f
PA
12375
12376 if (b->loc->next)
12377 {
12378 struct bp_location *loc = b->loc;
12379 int n = 0;
12380 for (; loc; loc = loc->next)
12381 ++n;
12382 printf_filtered (" (%d locations)", n);
12383 }
12384 }
12385}
12386
348d480f
PA
12387/* Default bp_location_ops methods. */
12388
12389static void
12390bp_location_dtor (struct bp_location *self)
12391{
12392 xfree (self->cond);
b775012e
LM
12393 if (self->cond_bytecode)
12394 free_agent_expr (self->cond_bytecode);
348d480f 12395 xfree (self->function_name);
f8eba3c6 12396 xfree (self->source_file);
348d480f
PA
12397}
12398
12399static const struct bp_location_ops bp_location_ops =
12400{
12401 bp_location_dtor
12402};
12403
2060206e
PA
12404/* Default breakpoint_ops methods all breakpoint_ops ultimately
12405 inherit from. */
348d480f 12406
2060206e
PA
12407static void
12408base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12409{
12410 decref_counted_command_line (&self->commands);
12411 xfree (self->cond_string);
348d480f 12412 xfree (self->addr_string);
f8eba3c6 12413 xfree (self->filter);
348d480f 12414 xfree (self->addr_string_range_end);
348d480f
PA
12415}
12416
2060206e
PA
12417static struct bp_location *
12418base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12419{
12420 struct bp_location *loc;
12421
12422 loc = XNEW (struct bp_location);
12423 init_bp_location (loc, &bp_location_ops, self);
12424 return loc;
12425}
12426
2060206e
PA
12427static void
12428base_breakpoint_re_set (struct breakpoint *b)
12429{
12430 /* Nothing to re-set. */
12431}
12432
12433#define internal_error_pure_virtual_called() \
12434 gdb_assert_not_reached ("pure virtual function called")
12435
12436static int
12437base_breakpoint_insert_location (struct bp_location *bl)
12438{
12439 internal_error_pure_virtual_called ();
12440}
12441
12442static int
12443base_breakpoint_remove_location (struct bp_location *bl)
12444{
12445 internal_error_pure_virtual_called ();
12446}
12447
12448static int
12449base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12450 struct address_space *aspace,
09ac7c10
TT
12451 CORE_ADDR bp_addr,
12452 const struct target_waitstatus *ws)
2060206e
PA
12453{
12454 internal_error_pure_virtual_called ();
12455}
12456
12457static void
12458base_breakpoint_check_status (bpstat bs)
12459{
12460 /* Always stop. */
12461}
12462
12463/* A "works_in_software_mode" breakpoint_ops method that just internal
12464 errors. */
12465
12466static int
12467base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12468{
12469 internal_error_pure_virtual_called ();
12470}
12471
12472/* A "resources_needed" breakpoint_ops method that just internal
12473 errors. */
12474
12475static int
12476base_breakpoint_resources_needed (const struct bp_location *bl)
12477{
12478 internal_error_pure_virtual_called ();
12479}
12480
12481static enum print_stop_action
12482base_breakpoint_print_it (bpstat bs)
12483{
12484 internal_error_pure_virtual_called ();
12485}
12486
12487static void
12488base_breakpoint_print_one_detail (const struct breakpoint *self,
12489 struct ui_out *uiout)
12490{
12491 /* nothing */
12492}
12493
12494static void
12495base_breakpoint_print_mention (struct breakpoint *b)
12496{
12497 internal_error_pure_virtual_called ();
12498}
12499
12500static void
12501base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12502{
12503 internal_error_pure_virtual_called ();
12504}
12505
983af33b
SDJ
12506static void
12507base_breakpoint_create_sals_from_address (char **arg,
12508 struct linespec_result *canonical,
12509 enum bptype type_wanted,
12510 char *addr_start,
12511 char **copy_arg)
12512{
12513 internal_error_pure_virtual_called ();
12514}
12515
12516static void
12517base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12518 struct linespec_result *c,
12519 struct linespec_sals *lsal,
12520 char *cond_string,
e7e0cddf 12521 char *extra_string,
983af33b
SDJ
12522 enum bptype type_wanted,
12523 enum bpdisp disposition,
12524 int thread,
12525 int task, int ignore_count,
12526 const struct breakpoint_ops *o,
12527 int from_tty, int enabled,
44f238bb 12528 int internal, unsigned flags)
983af33b
SDJ
12529{
12530 internal_error_pure_virtual_called ();
12531}
12532
12533static void
12534base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12535 struct symtabs_and_lines *sals)
12536{
12537 internal_error_pure_virtual_called ();
12538}
12539
2060206e
PA
12540static struct breakpoint_ops base_breakpoint_ops =
12541{
12542 base_breakpoint_dtor,
12543 base_breakpoint_allocate_location,
12544 base_breakpoint_re_set,
12545 base_breakpoint_insert_location,
12546 base_breakpoint_remove_location,
12547 base_breakpoint_breakpoint_hit,
12548 base_breakpoint_check_status,
12549 base_breakpoint_resources_needed,
12550 base_breakpoint_works_in_software_mode,
12551 base_breakpoint_print_it,
12552 NULL,
12553 base_breakpoint_print_one_detail,
12554 base_breakpoint_print_mention,
983af33b
SDJ
12555 base_breakpoint_print_recreate,
12556 base_breakpoint_create_sals_from_address,
12557 base_breakpoint_create_breakpoints_sal,
12558 base_breakpoint_decode_linespec,
2060206e
PA
12559};
12560
12561/* Default breakpoint_ops methods. */
12562
12563static void
348d480f
PA
12564bkpt_re_set (struct breakpoint *b)
12565{
06edf0c0
PA
12566 /* FIXME: is this still reachable? */
12567 if (b->addr_string == NULL)
12568 {
12569 /* Anything without a string can't be re-set. */
348d480f 12570 delete_breakpoint (b);
06edf0c0 12571 return;
348d480f 12572 }
06edf0c0
PA
12573
12574 breakpoint_re_set_default (b);
348d480f
PA
12575}
12576
2060206e 12577static int
348d480f
PA
12578bkpt_insert_location (struct bp_location *bl)
12579{
12580 if (bl->loc_type == bp_loc_hardware_breakpoint)
12581 return target_insert_hw_breakpoint (bl->gdbarch,
12582 &bl->target_info);
12583 else
12584 return target_insert_breakpoint (bl->gdbarch,
12585 &bl->target_info);
12586}
12587
2060206e 12588static int
348d480f
PA
12589bkpt_remove_location (struct bp_location *bl)
12590{
12591 if (bl->loc_type == bp_loc_hardware_breakpoint)
12592 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12593 else
12594 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12595}
12596
2060206e 12597static int
348d480f 12598bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12599 struct address_space *aspace, CORE_ADDR bp_addr,
12600 const struct target_waitstatus *ws)
348d480f
PA
12601{
12602 struct breakpoint *b = bl->owner;
12603
09ac7c10 12604 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12605 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12606 return 0;
12607
348d480f
PA
12608 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12609 aspace, bp_addr))
12610 return 0;
12611
12612 if (overlay_debugging /* unmapped overlay section */
12613 && section_is_overlay (bl->section)
12614 && !section_is_mapped (bl->section))
12615 return 0;
12616
12617 return 1;
12618}
12619
2060206e 12620static int
348d480f
PA
12621bkpt_resources_needed (const struct bp_location *bl)
12622{
12623 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12624
12625 return 1;
12626}
12627
2060206e 12628static enum print_stop_action
348d480f
PA
12629bkpt_print_it (bpstat bs)
12630{
348d480f
PA
12631 struct breakpoint *b;
12632 const struct bp_location *bl;
001c8c33 12633 int bp_temp;
79a45e25 12634 struct ui_out *uiout = current_uiout;
348d480f
PA
12635
12636 gdb_assert (bs->bp_location_at != NULL);
12637
12638 bl = bs->bp_location_at;
12639 b = bs->breakpoint_at;
12640
001c8c33
PA
12641 bp_temp = b->disposition == disp_del;
12642 if (bl->address != bl->requested_address)
12643 breakpoint_adjustment_warning (bl->requested_address,
12644 bl->address,
12645 b->number, 1);
12646 annotate_breakpoint (b->number);
12647 if (bp_temp)
12648 ui_out_text (uiout, "\nTemporary breakpoint ");
12649 else
12650 ui_out_text (uiout, "\nBreakpoint ");
12651 if (ui_out_is_mi_like_p (uiout))
348d480f 12652 {
001c8c33
PA
12653 ui_out_field_string (uiout, "reason",
12654 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12655 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 12656 }
001c8c33
PA
12657 ui_out_field_int (uiout, "bkptno", b->number);
12658 ui_out_text (uiout, ", ");
06edf0c0 12659
001c8c33 12660 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12661}
12662
2060206e 12663static void
06edf0c0
PA
12664bkpt_print_mention (struct breakpoint *b)
12665{
79a45e25 12666 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
12667 return;
12668
12669 switch (b->type)
12670 {
12671 case bp_breakpoint:
12672 case bp_gnu_ifunc_resolver:
12673 if (b->disposition == disp_del)
12674 printf_filtered (_("Temporary breakpoint"));
12675 else
12676 printf_filtered (_("Breakpoint"));
12677 printf_filtered (_(" %d"), b->number);
12678 if (b->type == bp_gnu_ifunc_resolver)
12679 printf_filtered (_(" at gnu-indirect-function resolver"));
12680 break;
12681 case bp_hardware_breakpoint:
12682 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12683 break;
e7e0cddf
SS
12684 case bp_dprintf:
12685 printf_filtered (_("Dprintf %d"), b->number);
12686 break;
06edf0c0
PA
12687 }
12688
12689 say_where (b);
12690}
12691
2060206e 12692static void
06edf0c0
PA
12693bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12694{
12695 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12696 fprintf_unfiltered (fp, "tbreak");
12697 else if (tp->type == bp_breakpoint)
12698 fprintf_unfiltered (fp, "break");
12699 else if (tp->type == bp_hardware_breakpoint
12700 && tp->disposition == disp_del)
12701 fprintf_unfiltered (fp, "thbreak");
12702 else if (tp->type == bp_hardware_breakpoint)
12703 fprintf_unfiltered (fp, "hbreak");
12704 else
12705 internal_error (__FILE__, __LINE__,
12706 _("unhandled breakpoint type %d"), (int) tp->type);
12707
2060206e 12708 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 12709 print_recreate_thread (tp, fp);
06edf0c0
PA
12710}
12711
983af33b
SDJ
12712static void
12713bkpt_create_sals_from_address (char **arg,
12714 struct linespec_result *canonical,
12715 enum bptype type_wanted,
12716 char *addr_start, char **copy_arg)
12717{
12718 create_sals_from_address_default (arg, canonical, type_wanted,
12719 addr_start, copy_arg);
12720}
12721
12722static void
12723bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12724 struct linespec_result *canonical,
12725 struct linespec_sals *lsal,
12726 char *cond_string,
e7e0cddf 12727 char *extra_string,
983af33b
SDJ
12728 enum bptype type_wanted,
12729 enum bpdisp disposition,
12730 int thread,
12731 int task, int ignore_count,
12732 const struct breakpoint_ops *ops,
12733 int from_tty, int enabled,
44f238bb 12734 int internal, unsigned flags)
983af33b
SDJ
12735{
12736 create_breakpoints_sal_default (gdbarch, canonical, lsal,
e7e0cddf
SS
12737 cond_string, extra_string,
12738 type_wanted,
983af33b
SDJ
12739 disposition, thread, task,
12740 ignore_count, ops, from_tty,
44f238bb 12741 enabled, internal, flags);
983af33b
SDJ
12742}
12743
12744static void
12745bkpt_decode_linespec (struct breakpoint *b, char **s,
12746 struct symtabs_and_lines *sals)
12747{
12748 decode_linespec_default (b, s, sals);
12749}
12750
06edf0c0
PA
12751/* Virtual table for internal breakpoints. */
12752
12753static void
12754internal_bkpt_re_set (struct breakpoint *b)
12755{
12756 switch (b->type)
12757 {
12758 /* Delete overlay event and longjmp master breakpoints; they
12759 will be reset later by breakpoint_re_set. */
12760 case bp_overlay_event:
12761 case bp_longjmp_master:
12762 case bp_std_terminate_master:
12763 case bp_exception_master:
12764 delete_breakpoint (b);
12765 break;
12766
12767 /* This breakpoint is special, it's set up when the inferior
12768 starts and we really don't want to touch it. */
12769 case bp_shlib_event:
12770
12771 /* Like bp_shlib_event, this breakpoint type is special. Once
12772 it is set up, we do not want to touch it. */
12773 case bp_thread_event:
12774 break;
12775 }
12776}
12777
12778static void
12779internal_bkpt_check_status (bpstat bs)
12780{
a9b3a50f
PA
12781 if (bs->breakpoint_at->type == bp_shlib_event)
12782 {
12783 /* If requested, stop when the dynamic linker notifies GDB of
12784 events. This allows the user to get control and place
12785 breakpoints in initializer routines for dynamically loaded
12786 objects (among other things). */
12787 bs->stop = stop_on_solib_events;
12788 bs->print = stop_on_solib_events;
12789 }
12790 else
12791 bs->stop = 0;
06edf0c0
PA
12792}
12793
12794static enum print_stop_action
12795internal_bkpt_print_it (bpstat bs)
12796{
36dfb11c 12797 struct ui_out *uiout = current_uiout;
06edf0c0 12798 struct breakpoint *b;
06edf0c0 12799
06edf0c0
PA
12800 b = bs->breakpoint_at;
12801
06edf0c0
PA
12802 switch (b->type)
12803 {
348d480f
PA
12804 case bp_shlib_event:
12805 /* Did we stop because the user set the stop_on_solib_events
12806 variable? (If so, we report this as a generic, "Stopped due
12807 to shlib event" message.) */
edcc5120 12808 print_solib_event (0);
348d480f
PA
12809 break;
12810
12811 case bp_thread_event:
12812 /* Not sure how we will get here.
12813 GDB should not stop for these breakpoints. */
12814 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12815 break;
12816
12817 case bp_overlay_event:
12818 /* By analogy with the thread event, GDB should not stop for these. */
12819 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12820 break;
12821
12822 case bp_longjmp_master:
12823 /* These should never be enabled. */
12824 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12825 break;
12826
12827 case bp_std_terminate_master:
12828 /* These should never be enabled. */
12829 printf_filtered (_("std::terminate Master Breakpoint: "
12830 "gdb should not stop!\n"));
348d480f
PA
12831 break;
12832
12833 case bp_exception_master:
12834 /* These should never be enabled. */
12835 printf_filtered (_("Exception Master Breakpoint: "
12836 "gdb should not stop!\n"));
06edf0c0
PA
12837 break;
12838 }
12839
001c8c33 12840 return PRINT_NOTHING;
06edf0c0
PA
12841}
12842
12843static void
12844internal_bkpt_print_mention (struct breakpoint *b)
12845{
12846 /* Nothing to mention. These breakpoints are internal. */
12847}
12848
06edf0c0
PA
12849/* Virtual table for momentary breakpoints */
12850
12851static void
12852momentary_bkpt_re_set (struct breakpoint *b)
12853{
12854 /* Keep temporary breakpoints, which can be encountered when we step
12855 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
12856 Otherwise these should have been blown away via the cleanup chain
12857 or by breakpoint_init_inferior when we rerun the executable. */
12858}
12859
12860static void
12861momentary_bkpt_check_status (bpstat bs)
12862{
12863 /* Nothing. The point of these breakpoints is causing a stop. */
12864}
12865
12866static enum print_stop_action
12867momentary_bkpt_print_it (bpstat bs)
12868{
79a45e25
PA
12869 struct ui_out *uiout = current_uiout;
12870
001c8c33 12871 if (ui_out_is_mi_like_p (uiout))
06edf0c0 12872 {
001c8c33 12873 struct breakpoint *b = bs->breakpoint_at;
348d480f 12874
001c8c33
PA
12875 switch (b->type)
12876 {
12877 case bp_finish:
12878 ui_out_field_string
12879 (uiout, "reason",
12880 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
12881 break;
348d480f 12882
001c8c33
PA
12883 case bp_until:
12884 ui_out_field_string
12885 (uiout, "reason",
12886 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
12887 break;
12888 }
348d480f
PA
12889 }
12890
001c8c33 12891 return PRINT_UNKNOWN;
348d480f
PA
12892}
12893
06edf0c0
PA
12894static void
12895momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12896{
06edf0c0 12897 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12898}
12899
e2e4d78b
JK
12900/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12901
12902 It gets cleared already on the removal of the first one of such placed
12903 breakpoints. This is OK as they get all removed altogether. */
12904
12905static void
12906longjmp_bkpt_dtor (struct breakpoint *self)
12907{
12908 struct thread_info *tp = find_thread_id (self->thread);
12909
12910 if (tp)
12911 tp->initiating_frame = null_frame_id;
12912
12913 momentary_breakpoint_ops.dtor (self);
12914}
12915
55aa24fb
SDJ
12916/* Specific methods for probe breakpoints. */
12917
12918static int
12919bkpt_probe_insert_location (struct bp_location *bl)
12920{
12921 int v = bkpt_insert_location (bl);
12922
12923 if (v == 0)
12924 {
12925 /* The insertion was successful, now let's set the probe's semaphore
12926 if needed. */
12927 bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
12928 }
12929
12930 return v;
12931}
12932
12933static int
12934bkpt_probe_remove_location (struct bp_location *bl)
12935{
12936 /* Let's clear the semaphore before removing the location. */
12937 bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
12938
12939 return bkpt_remove_location (bl);
12940}
12941
12942static void
12943bkpt_probe_create_sals_from_address (char **arg,
12944 struct linespec_result *canonical,
12945 enum bptype type_wanted,
12946 char *addr_start, char **copy_arg)
12947{
12948 struct linespec_sals lsal;
12949
12950 lsal.sals = parse_probes (arg, canonical);
12951
12952 *copy_arg = xstrdup (canonical->addr_string);
12953 lsal.canonical = xstrdup (*copy_arg);
12954
12955 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
12956}
12957
12958static void
12959bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
12960 struct symtabs_and_lines *sals)
12961{
12962 *sals = parse_probes (s, NULL);
12963 if (!sals->sals)
12964 error (_("probe not found"));
12965}
12966
348d480f 12967/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12968
348d480f
PA
12969static void
12970tracepoint_re_set (struct breakpoint *b)
12971{
12972 breakpoint_re_set_default (b);
12973}
876fa593 12974
348d480f
PA
12975static int
12976tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12977 struct address_space *aspace, CORE_ADDR bp_addr,
12978 const struct target_waitstatus *ws)
348d480f
PA
12979{
12980 /* By definition, the inferior does not report stops at
12981 tracepoints. */
12982 return 0;
74960c60
VP
12983}
12984
12985static void
348d480f
PA
12986tracepoint_print_one_detail (const struct breakpoint *self,
12987 struct ui_out *uiout)
74960c60 12988{
d9b3f62e
PA
12989 struct tracepoint *tp = (struct tracepoint *) self;
12990 if (tp->static_trace_marker_id)
348d480f
PA
12991 {
12992 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12993
348d480f
PA
12994 ui_out_text (uiout, "\tmarker id is ");
12995 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 12996 tp->static_trace_marker_id);
348d480f
PA
12997 ui_out_text (uiout, "\n");
12998 }
0d381245
VP
12999}
13000
a474d7c2 13001static void
348d480f 13002tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13003{
79a45e25 13004 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13005 return;
cc59ec59 13006
348d480f
PA
13007 switch (b->type)
13008 {
13009 case bp_tracepoint:
13010 printf_filtered (_("Tracepoint"));
13011 printf_filtered (_(" %d"), b->number);
13012 break;
13013 case bp_fast_tracepoint:
13014 printf_filtered (_("Fast tracepoint"));
13015 printf_filtered (_(" %d"), b->number);
13016 break;
13017 case bp_static_tracepoint:
13018 printf_filtered (_("Static tracepoint"));
13019 printf_filtered (_(" %d"), b->number);
13020 break;
13021 default:
13022 internal_error (__FILE__, __LINE__,
13023 _("unhandled tracepoint type %d"), (int) b->type);
13024 }
13025
13026 say_where (b);
a474d7c2
PA
13027}
13028
348d480f 13029static void
d9b3f62e 13030tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13031{
d9b3f62e
PA
13032 struct tracepoint *tp = (struct tracepoint *) self;
13033
13034 if (self->type == bp_fast_tracepoint)
348d480f 13035 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13036 if (self->type == bp_static_tracepoint)
348d480f 13037 fprintf_unfiltered (fp, "strace");
d9b3f62e 13038 else if (self->type == bp_tracepoint)
348d480f
PA
13039 fprintf_unfiltered (fp, "trace");
13040 else
13041 internal_error (__FILE__, __LINE__,
d9b3f62e 13042 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13043
d9b3f62e
PA
13044 fprintf_unfiltered (fp, " %s", self->addr_string);
13045 print_recreate_thread (self, fp);
13046
13047 if (tp->pass_count)
13048 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13049}
13050
983af33b
SDJ
13051static void
13052tracepoint_create_sals_from_address (char **arg,
13053 struct linespec_result *canonical,
13054 enum bptype type_wanted,
13055 char *addr_start, char **copy_arg)
13056{
13057 create_sals_from_address_default (arg, canonical, type_wanted,
13058 addr_start, copy_arg);
13059}
13060
13061static void
13062tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13063 struct linespec_result *canonical,
13064 struct linespec_sals *lsal,
13065 char *cond_string,
e7e0cddf 13066 char *extra_string,
983af33b
SDJ
13067 enum bptype type_wanted,
13068 enum bpdisp disposition,
13069 int thread,
13070 int task, int ignore_count,
13071 const struct breakpoint_ops *ops,
13072 int from_tty, int enabled,
44f238bb 13073 int internal, unsigned flags)
983af33b
SDJ
13074{
13075 create_breakpoints_sal_default (gdbarch, canonical, lsal,
e7e0cddf
SS
13076 cond_string, extra_string,
13077 type_wanted,
983af33b
SDJ
13078 disposition, thread, task,
13079 ignore_count, ops, from_tty,
44f238bb 13080 enabled, internal, flags);
983af33b
SDJ
13081}
13082
13083static void
13084tracepoint_decode_linespec (struct breakpoint *b, char **s,
13085 struct symtabs_and_lines *sals)
13086{
13087 decode_linespec_default (b, s, sals);
13088}
13089
2060206e 13090struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13091
55aa24fb
SDJ
13092/* The breakpoint_ops structure to be use on tracepoints placed in a
13093 static probe. */
13094
13095static void
13096tracepoint_probe_create_sals_from_address (char **arg,
13097 struct linespec_result *canonical,
13098 enum bptype type_wanted,
13099 char *addr_start, char **copy_arg)
13100{
13101 /* We use the same method for breakpoint on probes. */
13102 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13103 addr_start, copy_arg);
13104}
13105
13106static void
13107tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13108 struct symtabs_and_lines *sals)
13109{
13110 /* We use the same method for breakpoint on probes. */
13111 bkpt_probe_decode_linespec (b, s, sals);
13112}
13113
13114static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13115
983af33b
SDJ
13116/* The breakpoint_ops structure to be used on static tracepoints with
13117 markers (`-m'). */
13118
13119static void
13120strace_marker_create_sals_from_address (char **arg,
13121 struct linespec_result *canonical,
13122 enum bptype type_wanted,
13123 char *addr_start, char **copy_arg)
13124{
13125 struct linespec_sals lsal;
13126
13127 lsal.sals = decode_static_tracepoint_spec (arg);
13128
13129 *copy_arg = savestring (addr_start, *arg - addr_start);
13130
13131 canonical->addr_string = xstrdup (*copy_arg);
13132 lsal.canonical = xstrdup (*copy_arg);
13133 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13134}
13135
13136static void
13137strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13138 struct linespec_result *canonical,
13139 struct linespec_sals *lsal,
13140 char *cond_string,
e7e0cddf 13141 char *extra_string,
983af33b
SDJ
13142 enum bptype type_wanted,
13143 enum bpdisp disposition,
13144 int thread,
13145 int task, int ignore_count,
13146 const struct breakpoint_ops *ops,
13147 int from_tty, int enabled,
44f238bb 13148 int internal, unsigned flags)
983af33b
SDJ
13149{
13150 int i;
13151
13152 /* If the user is creating a static tracepoint by marker id
13153 (strace -m MARKER_ID), then store the sals index, so that
13154 breakpoint_re_set can try to match up which of the newly
13155 found markers corresponds to this one, and, don't try to
13156 expand multiple locations for each sal, given than SALS
13157 already should contain all sals for MARKER_ID. */
13158
13159 for (i = 0; i < lsal->sals.nelts; ++i)
13160 {
13161 struct symtabs_and_lines expanded;
13162 struct tracepoint *tp;
13163 struct cleanup *old_chain;
13164 char *addr_string;
13165
13166 expanded.nelts = 1;
13167 expanded.sals = &lsal->sals.sals[i];
13168
13169 addr_string = xstrdup (canonical->addr_string);
13170 old_chain = make_cleanup (xfree, addr_string);
13171
13172 tp = XCNEW (struct tracepoint);
13173 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13174 addr_string, NULL,
e7e0cddf
SS
13175 cond_string, extra_string,
13176 type_wanted, disposition,
983af33b 13177 thread, task, ignore_count, ops,
44f238bb 13178 from_tty, enabled, internal, flags,
983af33b
SDJ
13179 canonical->special_display);
13180 /* Given that its possible to have multiple markers with
13181 the same string id, if the user is creating a static
13182 tracepoint by marker id ("strace -m MARKER_ID"), then
13183 store the sals index, so that breakpoint_re_set can
13184 try to match up which of the newly found markers
13185 corresponds to this one */
13186 tp->static_trace_marker_id_idx = i;
13187
13188 install_breakpoint (internal, &tp->base, 0);
13189
13190 discard_cleanups (old_chain);
13191 }
13192}
13193
13194static void
13195strace_marker_decode_linespec (struct breakpoint *b, char **s,
13196 struct symtabs_and_lines *sals)
13197{
13198 struct tracepoint *tp = (struct tracepoint *) b;
13199
13200 *sals = decode_static_tracepoint_spec (s);
13201 if (sals->nelts > tp->static_trace_marker_id_idx)
13202 {
13203 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13204 sals->nelts = 1;
13205 }
13206 else
13207 error (_("marker %s not found"), tp->static_trace_marker_id);
13208}
13209
13210static struct breakpoint_ops strace_marker_breakpoint_ops;
13211
13212static int
13213strace_marker_p (struct breakpoint *b)
13214{
13215 return b->ops == &strace_marker_breakpoint_ops;
13216}
13217
53a5351d 13218/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13219 structures. */
c906108c
SS
13220
13221void
fba45db2 13222delete_breakpoint (struct breakpoint *bpt)
c906108c 13223{
52f0bd74 13224 struct breakpoint *b;
c906108c 13225
8a3fe4f8 13226 gdb_assert (bpt != NULL);
c906108c 13227
4a64f543
MS
13228 /* Has this bp already been deleted? This can happen because
13229 multiple lists can hold pointers to bp's. bpstat lists are
13230 especial culprits.
13231
13232 One example of this happening is a watchpoint's scope bp. When
13233 the scope bp triggers, we notice that the watchpoint is out of
13234 scope, and delete it. We also delete its scope bp. But the
13235 scope bp is marked "auto-deleting", and is already on a bpstat.
13236 That bpstat is then checked for auto-deleting bp's, which are
13237 deleted.
13238
13239 A real solution to this problem might involve reference counts in
13240 bp's, and/or giving them pointers back to their referencing
13241 bpstat's, and teaching delete_breakpoint to only free a bp's
13242 storage when no more references were extent. A cheaper bandaid
13243 was chosen. */
c906108c
SS
13244 if (bpt->type == bp_none)
13245 return;
13246
4a64f543
MS
13247 /* At least avoid this stale reference until the reference counting
13248 of breakpoints gets resolved. */
d0fb5eae 13249 if (bpt->related_breakpoint != bpt)
e5a0a904 13250 {
d0fb5eae 13251 struct breakpoint *related;
3a5c3e22 13252 struct watchpoint *w;
d0fb5eae
JK
13253
13254 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13255 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13256 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13257 w = (struct watchpoint *) bpt;
13258 else
13259 w = NULL;
13260 if (w != NULL)
13261 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13262
13263 /* Unlink bpt from the bpt->related_breakpoint ring. */
13264 for (related = bpt; related->related_breakpoint != bpt;
13265 related = related->related_breakpoint);
13266 related->related_breakpoint = bpt->related_breakpoint;
13267 bpt->related_breakpoint = bpt;
e5a0a904
JK
13268 }
13269
a9634178
TJB
13270 /* watch_command_1 creates a watchpoint but only sets its number if
13271 update_watchpoint succeeds in creating its bp_locations. If there's
13272 a problem in that process, we'll be asked to delete the half-created
13273 watchpoint. In that case, don't announce the deletion. */
13274 if (bpt->number)
13275 observer_notify_breakpoint_deleted (bpt);
c906108c 13276
c906108c
SS
13277 if (breakpoint_chain == bpt)
13278 breakpoint_chain = bpt->next;
13279
c906108c
SS
13280 ALL_BREAKPOINTS (b)
13281 if (b->next == bpt)
c5aa993b
JM
13282 {
13283 b->next = bpt->next;
13284 break;
13285 }
c906108c 13286
f431efe5
PA
13287 /* Be sure no bpstat's are pointing at the breakpoint after it's
13288 been freed. */
13289 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13290 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13291 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13292 commands are associated with the bpstat; if we remove it here,
13293 then the later call to bpstat_do_actions (&stop_bpstat); in
13294 event-top.c won't do anything, and temporary breakpoints with
13295 commands won't work. */
13296
13297 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13298
4a64f543
MS
13299 /* Now that breakpoint is removed from breakpoint list, update the
13300 global location list. This will remove locations that used to
13301 belong to this breakpoint. Do this before freeing the breakpoint
13302 itself, since remove_breakpoint looks at location's owner. It
13303 might be better design to have location completely
13304 self-contained, but it's not the case now. */
b60e7edf 13305 update_global_location_list (0);
74960c60 13306
348d480f 13307 bpt->ops->dtor (bpt);
4a64f543
MS
13308 /* On the chance that someone will soon try again to delete this
13309 same bp, we mark it as deleted before freeing its storage. */
c906108c 13310 bpt->type = bp_none;
b8c9b27d 13311 xfree (bpt);
c906108c
SS
13312}
13313
4d6140d9
AC
13314static void
13315do_delete_breakpoint_cleanup (void *b)
13316{
13317 delete_breakpoint (b);
13318}
13319
13320struct cleanup *
13321make_cleanup_delete_breakpoint (struct breakpoint *b)
13322{
13323 return make_cleanup (do_delete_breakpoint_cleanup, b);
13324}
13325
51be5b68
PA
13326/* Iterator function to call a user-provided callback function once
13327 for each of B and its related breakpoints. */
13328
13329static void
13330iterate_over_related_breakpoints (struct breakpoint *b,
13331 void (*function) (struct breakpoint *,
13332 void *),
13333 void *data)
13334{
13335 struct breakpoint *related;
13336
13337 related = b;
13338 do
13339 {
13340 struct breakpoint *next;
13341
13342 /* FUNCTION may delete RELATED. */
13343 next = related->related_breakpoint;
13344
13345 if (next == related)
13346 {
13347 /* RELATED is the last ring entry. */
13348 function (related, data);
13349
13350 /* FUNCTION may have deleted it, so we'd never reach back to
13351 B. There's nothing left to do anyway, so just break
13352 out. */
13353 break;
13354 }
13355 else
13356 function (related, data);
13357
13358 related = next;
13359 }
13360 while (related != b);
13361}
95a42b64
TT
13362
13363static void
13364do_delete_breakpoint (struct breakpoint *b, void *ignore)
13365{
13366 delete_breakpoint (b);
13367}
13368
51be5b68
PA
13369/* A callback for map_breakpoint_numbers that calls
13370 delete_breakpoint. */
13371
13372static void
13373do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13374{
13375 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13376}
13377
c906108c 13378void
fba45db2 13379delete_command (char *arg, int from_tty)
c906108c 13380{
35df4500 13381 struct breakpoint *b, *b_tmp;
c906108c 13382
ea9365bb
TT
13383 dont_repeat ();
13384
c906108c
SS
13385 if (arg == 0)
13386 {
13387 int breaks_to_delete = 0;
13388
46c6471b
PA
13389 /* Delete all breakpoints if no argument. Do not delete
13390 internal breakpoints, these have to be deleted with an
13391 explicit breakpoint number argument. */
c5aa993b 13392 ALL_BREAKPOINTS (b)
46c6471b 13393 if (user_breakpoint_p (b))
973d738b
DJ
13394 {
13395 breaks_to_delete = 1;
13396 break;
13397 }
c906108c
SS
13398
13399 /* Ask user only if there are some breakpoints to delete. */
13400 if (!from_tty
e2e0b3e5 13401 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13402 {
35df4500 13403 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13404 if (user_breakpoint_p (b))
c5aa993b 13405 delete_breakpoint (b);
c906108c
SS
13406 }
13407 }
13408 else
51be5b68 13409 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13410}
13411
0d381245
VP
13412static int
13413all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13414{
0d381245 13415 for (; loc; loc = loc->next)
8645ff69
UW
13416 if (!loc->shlib_disabled
13417 && !loc->pspace->executing_startup)
0d381245
VP
13418 return 0;
13419 return 1;
fe3f5fa8
VP
13420}
13421
776592bf
DE
13422/* Subroutine of update_breakpoint_locations to simplify it.
13423 Return non-zero if multiple fns in list LOC have the same name.
13424 Null names are ignored. */
13425
13426static int
13427ambiguous_names_p (struct bp_location *loc)
13428{
13429 struct bp_location *l;
13430 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13431 (int (*) (const void *,
13432 const void *)) streq,
776592bf
DE
13433 NULL, xcalloc, xfree);
13434
13435 for (l = loc; l != NULL; l = l->next)
13436 {
13437 const char **slot;
13438 const char *name = l->function_name;
13439
13440 /* Allow for some names to be NULL, ignore them. */
13441 if (name == NULL)
13442 continue;
13443
13444 slot = (const char **) htab_find_slot (htab, (const void *) name,
13445 INSERT);
4a64f543
MS
13446 /* NOTE: We can assume slot != NULL here because xcalloc never
13447 returns NULL. */
776592bf
DE
13448 if (*slot != NULL)
13449 {
13450 htab_delete (htab);
13451 return 1;
13452 }
13453 *slot = name;
13454 }
13455
13456 htab_delete (htab);
13457 return 0;
13458}
13459
0fb4aa4b
PA
13460/* When symbols change, it probably means the sources changed as well,
13461 and it might mean the static tracepoint markers are no longer at
13462 the same address or line numbers they used to be at last we
13463 checked. Losing your static tracepoints whenever you rebuild is
13464 undesirable. This function tries to resync/rematch gdb static
13465 tracepoints with the markers on the target, for static tracepoints
13466 that have not been set by marker id. Static tracepoint that have
13467 been set by marker id are reset by marker id in breakpoint_re_set.
13468 The heuristic is:
13469
13470 1) For a tracepoint set at a specific address, look for a marker at
13471 the old PC. If one is found there, assume to be the same marker.
13472 If the name / string id of the marker found is different from the
13473 previous known name, assume that means the user renamed the marker
13474 in the sources, and output a warning.
13475
13476 2) For a tracepoint set at a given line number, look for a marker
13477 at the new address of the old line number. If one is found there,
13478 assume to be the same marker. If the name / string id of the
13479 marker found is different from the previous known name, assume that
13480 means the user renamed the marker in the sources, and output a
13481 warning.
13482
13483 3) If a marker is no longer found at the same address or line, it
13484 may mean the marker no longer exists. But it may also just mean
13485 the code changed a bit. Maybe the user added a few lines of code
13486 that made the marker move up or down (in line number terms). Ask
13487 the target for info about the marker with the string id as we knew
13488 it. If found, update line number and address in the matching
13489 static tracepoint. This will get confused if there's more than one
13490 marker with the same ID (possible in UST, although unadvised
13491 precisely because it confuses tools). */
13492
13493static struct symtab_and_line
13494update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13495{
d9b3f62e 13496 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13497 struct static_tracepoint_marker marker;
13498 CORE_ADDR pc;
0fb4aa4b
PA
13499
13500 pc = sal.pc;
13501 if (sal.line)
13502 find_line_pc (sal.symtab, sal.line, &pc);
13503
13504 if (target_static_tracepoint_marker_at (pc, &marker))
13505 {
d9b3f62e 13506 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13507 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13508 b->number,
d9b3f62e 13509 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13510
d9b3f62e
PA
13511 xfree (tp->static_trace_marker_id);
13512 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13513 release_static_tracepoint_marker (&marker);
13514
13515 return sal;
13516 }
13517
13518 /* Old marker wasn't found on target at lineno. Try looking it up
13519 by string ID. */
13520 if (!sal.explicit_pc
13521 && sal.line != 0
13522 && sal.symtab != NULL
d9b3f62e 13523 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13524 {
13525 VEC(static_tracepoint_marker_p) *markers;
13526
13527 markers
d9b3f62e 13528 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13529
13530 if (!VEC_empty(static_tracepoint_marker_p, markers))
13531 {
80e1d417 13532 struct symtab_and_line sal2;
0fb4aa4b 13533 struct symbol *sym;
80e1d417 13534 struct static_tracepoint_marker *tpmarker;
79a45e25 13535 struct ui_out *uiout = current_uiout;
0fb4aa4b 13536
80e1d417 13537 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13538
d9b3f62e 13539 xfree (tp->static_trace_marker_id);
80e1d417 13540 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13541
13542 warning (_("marker for static tracepoint %d (%s) not "
13543 "found at previous line number"),
d9b3f62e 13544 b->number, tp->static_trace_marker_id);
0fb4aa4b 13545
80e1d417 13546 init_sal (&sal2);
0fb4aa4b 13547
80e1d417 13548 sal2.pc = tpmarker->address;
0fb4aa4b 13549
80e1d417
AS
13550 sal2 = find_pc_line (tpmarker->address, 0);
13551 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
13552 ui_out_text (uiout, "Now in ");
13553 if (sym)
13554 {
13555 ui_out_field_string (uiout, "func",
13556 SYMBOL_PRINT_NAME (sym));
13557 ui_out_text (uiout, " at ");
13558 }
80e1d417 13559 ui_out_field_string (uiout, "file", sal2.symtab->filename);
0fb4aa4b
PA
13560 ui_out_text (uiout, ":");
13561
13562 if (ui_out_is_mi_like_p (uiout))
13563 {
80e1d417 13564 char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b
PA
13565
13566 if (fullname)
13567 ui_out_field_string (uiout, "fullname", fullname);
13568 }
13569
80e1d417 13570 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
13571 ui_out_text (uiout, "\n");
13572
80e1d417 13573 b->loc->line_number = sal2.line;
0fb4aa4b 13574
f8eba3c6 13575 xfree (b->loc->source_file);
0fb4aa4b 13576 if (sym)
80e1d417 13577 b->loc->source_file = xstrdup (sal2.symtab->filename);
0fb4aa4b 13578 else
f8eba3c6 13579 b->loc->source_file = NULL;
0fb4aa4b
PA
13580
13581 xfree (b->addr_string);
13582 b->addr_string = xstrprintf ("%s:%d",
80e1d417 13583 sal2.symtab->filename,
f8eba3c6 13584 b->loc->line_number);
0fb4aa4b
PA
13585
13586 /* Might be nice to check if function changed, and warn if
13587 so. */
13588
80e1d417 13589 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13590 }
13591 }
13592 return sal;
13593}
13594
8d3788bd
VP
13595/* Returns 1 iff locations A and B are sufficiently same that
13596 we don't need to report breakpoint as changed. */
13597
13598static int
13599locations_are_equal (struct bp_location *a, struct bp_location *b)
13600{
13601 while (a && b)
13602 {
13603 if (a->address != b->address)
13604 return 0;
13605
13606 if (a->shlib_disabled != b->shlib_disabled)
13607 return 0;
13608
13609 if (a->enabled != b->enabled)
13610 return 0;
13611
13612 a = a->next;
13613 b = b->next;
13614 }
13615
13616 if ((a == NULL) != (b == NULL))
13617 return 0;
13618
13619 return 1;
13620}
13621
f1310107
TJB
13622/* Create new breakpoint locations for B (a hardware or software breakpoint)
13623 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
13624 a ranged breakpoint. */
13625
0e30163f 13626void
0d381245 13627update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
13628 struct symtabs_and_lines sals,
13629 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
13630{
13631 int i;
0d381245
VP
13632 struct bp_location *existing_locations = b->loc;
13633
f8eba3c6
TT
13634 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13635 {
13636 /* Ranged breakpoints have only one start location and one end
13637 location. */
13638 b->enable_state = bp_disabled;
13639 update_global_location_list (1);
13640 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13641 "multiple locations found\n"),
13642 b->number);
13643 return;
13644 }
f1310107 13645
4a64f543
MS
13646 /* If there's no new locations, and all existing locations are
13647 pending, don't do anything. This optimizes the common case where
13648 all locations are in the same shared library, that was unloaded.
13649 We'd like to retain the location, so that when the library is
13650 loaded again, we don't loose the enabled/disabled status of the
13651 individual locations. */
0d381245 13652 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
13653 return;
13654
fe3f5fa8
VP
13655 b->loc = NULL;
13656
0d381245 13657 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 13658 {
f8eba3c6
TT
13659 struct bp_location *new_loc;
13660
13661 switch_to_program_space_and_thread (sals.sals[i].pspace);
13662
13663 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 13664
0d381245
VP
13665 /* Reparse conditions, they might contain references to the
13666 old symtab. */
13667 if (b->cond_string != NULL)
13668 {
f1310107 13669 char *s;
bfd189b1 13670 volatile struct gdb_exception e;
fe3f5fa8 13671
0d381245
VP
13672 s = b->cond_string;
13673 TRY_CATCH (e, RETURN_MASK_ERROR)
13674 {
1bb9788d
TT
13675 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
13676 block_for_pc (sals.sals[i].pc),
0d381245
VP
13677 0);
13678 }
13679 if (e.reason < 0)
13680 {
3e43a32a
MS
13681 warning (_("failed to reevaluate condition "
13682 "for breakpoint %d: %s"),
0d381245
VP
13683 b->number, e.message);
13684 new_loc->enabled = 0;
13685 }
13686 }
fe3f5fa8 13687
f1310107
TJB
13688 if (sals_end.nelts)
13689 {
13690 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
13691
13692 new_loc->length = end - sals.sals[0].pc + 1;
13693 }
0d381245 13694 }
fe3f5fa8 13695
514f746b
AR
13696 /* Update locations of permanent breakpoints. */
13697 if (b->enable_state == bp_permanent)
13698 make_breakpoint_permanent (b);
13699
4a64f543
MS
13700 /* If possible, carry over 'disable' status from existing
13701 breakpoints. */
0d381245
VP
13702 {
13703 struct bp_location *e = existing_locations;
776592bf
DE
13704 /* If there are multiple breakpoints with the same function name,
13705 e.g. for inline functions, comparing function names won't work.
13706 Instead compare pc addresses; this is just a heuristic as things
13707 may have moved, but in practice it gives the correct answer
13708 often enough until a better solution is found. */
13709 int have_ambiguous_names = ambiguous_names_p (b->loc);
13710
0d381245
VP
13711 for (; e; e = e->next)
13712 {
13713 if (!e->enabled && e->function_name)
13714 {
13715 struct bp_location *l = b->loc;
776592bf
DE
13716 if (have_ambiguous_names)
13717 {
13718 for (; l; l = l->next)
f1310107 13719 if (breakpoint_locations_match (e, l))
776592bf
DE
13720 {
13721 l->enabled = 0;
13722 break;
13723 }
13724 }
13725 else
13726 {
13727 for (; l; l = l->next)
13728 if (l->function_name
13729 && strcmp (e->function_name, l->function_name) == 0)
13730 {
13731 l->enabled = 0;
13732 break;
13733 }
13734 }
0d381245
VP
13735 }
13736 }
13737 }
fe3f5fa8 13738
8d3788bd
VP
13739 if (!locations_are_equal (existing_locations, b->loc))
13740 observer_notify_breakpoint_modified (b);
13741
b60e7edf 13742 update_global_location_list (1);
fe3f5fa8
VP
13743}
13744
ef23e705
TJB
13745/* Find the SaL locations corresponding to the given ADDR_STRING.
13746 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13747
13748static struct symtabs_and_lines
13749addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
13750{
13751 char *s;
02d20e4a 13752 struct symtabs_and_lines sals = {0};
f8eba3c6 13753 volatile struct gdb_exception e;
ef23e705 13754
983af33b 13755 gdb_assert (b->ops != NULL);
ef23e705 13756 s = addr_string;
ef23e705
TJB
13757
13758 TRY_CATCH (e, RETURN_MASK_ERROR)
13759 {
983af33b 13760 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
13761 }
13762 if (e.reason < 0)
13763 {
13764 int not_found_and_ok = 0;
13765 /* For pending breakpoints, it's expected that parsing will
13766 fail until the right shared library is loaded. User has
13767 already told to create pending breakpoints and don't need
13768 extra messages. If breakpoint is in bp_shlib_disabled
13769 state, then user already saw the message about that
13770 breakpoint being disabled, and don't want to see more
13771 errors. */
58438ac1 13772 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
13773 && (b->condition_not_parsed
13774 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13775 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13776 || b->enable_state == bp_disabled))
13777 not_found_and_ok = 1;
13778
13779 if (!not_found_and_ok)
13780 {
13781 /* We surely don't want to warn about the same breakpoint
13782 10 times. One solution, implemented here, is disable
13783 the breakpoint on error. Another solution would be to
13784 have separate 'warning emitted' flag. Since this
13785 happens only when a binary has changed, I don't know
13786 which approach is better. */
13787 b->enable_state = bp_disabled;
13788 throw_exception (e);
13789 }
13790 }
13791
58438ac1 13792 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 13793 {
f8eba3c6 13794 int i;
ef23e705 13795
f8eba3c6
TT
13796 for (i = 0; i < sals.nelts; ++i)
13797 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
13798 if (b->condition_not_parsed && s && s[0])
13799 {
13800 char *cond_string = 0;
13801 int thread = -1;
13802 int task = 0;
e7e0cddf 13803 char *extra_string = NULL;
ef23e705
TJB
13804
13805 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
13806 &cond_string, &thread, &task,
13807 &extra_string);
ef23e705
TJB
13808 if (cond_string)
13809 b->cond_string = cond_string;
13810 b->thread = thread;
13811 b->task = task;
e7e0cddf
SS
13812 if (extra_string)
13813 b->extra_string = extra_string;
ef23e705
TJB
13814 b->condition_not_parsed = 0;
13815 }
13816
983af33b 13817 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 13818 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 13819
58438ac1
TT
13820 *found = 1;
13821 }
13822 else
13823 *found = 0;
ef23e705
TJB
13824
13825 return sals;
13826}
13827
348d480f
PA
13828/* The default re_set method, for typical hardware or software
13829 breakpoints. Reevaluate the breakpoint and recreate its
13830 locations. */
13831
13832static void
28010a5d 13833breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
13834{
13835 int found;
f1310107 13836 struct symtabs_and_lines sals, sals_end;
ef23e705 13837 struct symtabs_and_lines expanded = {0};
f1310107 13838 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
13839
13840 sals = addr_string_to_sals (b, b->addr_string, &found);
13841 if (found)
13842 {
13843 make_cleanup (xfree, sals.sals);
f8eba3c6 13844 expanded = sals;
ef23e705
TJB
13845 }
13846
f1310107
TJB
13847 if (b->addr_string_range_end)
13848 {
13849 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
13850 if (found)
13851 {
13852 make_cleanup (xfree, sals_end.sals);
f8eba3c6 13853 expanded_end = sals_end;
f1310107
TJB
13854 }
13855 }
13856
13857 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
13858}
13859
983af33b
SDJ
13860/* Default method for creating SALs from an address string. It basically
13861 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13862
13863static void
13864create_sals_from_address_default (char **arg,
13865 struct linespec_result *canonical,
13866 enum bptype type_wanted,
13867 char *addr_start, char **copy_arg)
13868{
13869 parse_breakpoint_sals (arg, canonical);
13870}
13871
13872/* Call create_breakpoints_sal for the given arguments. This is the default
13873 function for the `create_breakpoints_sal' method of
13874 breakpoint_ops. */
13875
13876static void
13877create_breakpoints_sal_default (struct gdbarch *gdbarch,
13878 struct linespec_result *canonical,
13879 struct linespec_sals *lsal,
13880 char *cond_string,
e7e0cddf 13881 char *extra_string,
983af33b
SDJ
13882 enum bptype type_wanted,
13883 enum bpdisp disposition,
13884 int thread,
13885 int task, int ignore_count,
13886 const struct breakpoint_ops *ops,
13887 int from_tty, int enabled,
44f238bb 13888 int internal, unsigned flags)
983af33b
SDJ
13889{
13890 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 13891 extra_string,
983af33b
SDJ
13892 type_wanted, disposition,
13893 thread, task, ignore_count, ops, from_tty,
44f238bb 13894 enabled, internal, flags);
983af33b
SDJ
13895}
13896
13897/* Decode the line represented by S by calling decode_line_full. This is the
13898 default function for the `decode_linespec' method of breakpoint_ops. */
13899
13900static void
13901decode_linespec_default (struct breakpoint *b, char **s,
13902 struct symtabs_and_lines *sals)
13903{
13904 struct linespec_result canonical;
13905
13906 init_linespec_result (&canonical);
13907 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
13908 (struct symtab *) NULL, 0,
13909 &canonical, multiple_symbols_all,
13910 b->filter);
13911
13912 /* We should get 0 or 1 resulting SALs. */
13913 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
13914
13915 if (VEC_length (linespec_sals, canonical.sals) > 0)
13916 {
13917 struct linespec_sals *lsal;
13918
13919 lsal = VEC_index (linespec_sals, canonical.sals, 0);
13920 *sals = lsal->sals;
13921 /* Arrange it so the destructor does not free the
13922 contents. */
13923 lsal->sals.sals = NULL;
13924 }
13925
13926 destroy_linespec_result (&canonical);
13927}
13928
28010a5d
PA
13929/* Prepare the global context for a re-set of breakpoint B. */
13930
13931static struct cleanup *
13932prepare_re_set_context (struct breakpoint *b)
13933{
13934 struct cleanup *cleanups;
13935
13936 input_radix = b->input_radix;
13937 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
13938 if (b->pspace != NULL)
13939 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
13940 set_language (b->language);
13941
13942 return cleanups;
ef23e705
TJB
13943}
13944
c906108c
SS
13945/* Reset a breakpoint given it's struct breakpoint * BINT.
13946 The value we return ends up being the return value from catch_errors.
13947 Unused in this case. */
13948
13949static int
4efb68b1 13950breakpoint_re_set_one (void *bint)
c906108c 13951{
4a64f543 13952 /* Get past catch_errs. */
53a5351d 13953 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 13954 struct cleanup *cleanups;
c906108c 13955
348d480f
PA
13956 cleanups = prepare_re_set_context (b);
13957 b->ops->re_set (b);
13958 do_cleanups (cleanups);
c906108c
SS
13959 return 0;
13960}
13961
69de3c6a 13962/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 13963void
69de3c6a 13964breakpoint_re_set (void)
c906108c 13965{
35df4500 13966 struct breakpoint *b, *b_tmp;
c906108c
SS
13967 enum language save_language;
13968 int save_input_radix;
6c95b8df 13969 struct cleanup *old_chain;
c5aa993b 13970
c906108c
SS
13971 save_language = current_language->la_language;
13972 save_input_radix = input_radix;
6c95b8df
PA
13973 old_chain = save_current_program_space ();
13974
35df4500 13975 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 13976 {
4a64f543 13977 /* Format possible error msg. */
fe3f5fa8 13978 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
13979 b->number);
13980 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 13981 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 13982 do_cleanups (cleanups);
c5aa993b 13983 }
c906108c
SS
13984 set_language (save_language);
13985 input_radix = save_input_radix;
e62c965a 13986
0756c555 13987 jit_breakpoint_re_set ();
4efc6507 13988
6c95b8df
PA
13989 do_cleanups (old_chain);
13990
af02033e
PP
13991 create_overlay_event_breakpoint ();
13992 create_longjmp_master_breakpoint ();
13993 create_std_terminate_master_breakpoint ();
186c406b 13994 create_exception_master_breakpoint ();
1bfeeb0f
JL
13995
13996 /* While we're at it, reset the skip list too. */
13997 skip_re_set ();
c906108c
SS
13998}
13999\f
c906108c
SS
14000/* Reset the thread number of this breakpoint:
14001
14002 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14003 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14004void
fba45db2 14005breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14006{
14007 if (b->thread != -1)
14008 {
39f77062
KB
14009 if (in_thread_list (inferior_ptid))
14010 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14011
14012 /* We're being called after following a fork. The new fork is
14013 selected as current, and unless this was a vfork will have a
14014 different program space from the original thread. Reset that
14015 as well. */
14016 b->loc->pspace = current_program_space;
c906108c
SS
14017 }
14018}
14019
03ac34d5
MS
14020/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14021 If from_tty is nonzero, it prints a message to that effect,
14022 which ends with a period (no newline). */
14023
c906108c 14024void
fba45db2 14025set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14026{
52f0bd74 14027 struct breakpoint *b;
c906108c
SS
14028
14029 if (count < 0)
14030 count = 0;
14031
14032 ALL_BREAKPOINTS (b)
14033 if (b->number == bptnum)
c5aa993b 14034 {
d77f58be
SS
14035 if (is_tracepoint (b))
14036 {
14037 if (from_tty && count != 0)
14038 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14039 bptnum);
14040 return;
14041 }
14042
c5aa993b 14043 b->ignore_count = count;
221ea385
KS
14044 if (from_tty)
14045 {
14046 if (count == 0)
3e43a32a
MS
14047 printf_filtered (_("Will stop next time "
14048 "breakpoint %d is reached."),
221ea385
KS
14049 bptnum);
14050 else if (count == 1)
a3f17187 14051 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14052 bptnum);
14053 else
3e43a32a
MS
14054 printf_filtered (_("Will ignore next %d "
14055 "crossings of breakpoint %d."),
221ea385
KS
14056 count, bptnum);
14057 }
c5aa993b 14058 breakpoints_changed ();
8d3788bd 14059 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14060 return;
14061 }
c906108c 14062
8a3fe4f8 14063 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14064}
14065
c906108c
SS
14066/* Command to set ignore-count of breakpoint N to COUNT. */
14067
14068static void
fba45db2 14069ignore_command (char *args, int from_tty)
c906108c
SS
14070{
14071 char *p = args;
52f0bd74 14072 int num;
c906108c
SS
14073
14074 if (p == 0)
e2e0b3e5 14075 error_no_arg (_("a breakpoint number"));
c5aa993b 14076
c906108c 14077 num = get_number (&p);
5c44784c 14078 if (num == 0)
8a3fe4f8 14079 error (_("bad breakpoint number: '%s'"), args);
c906108c 14080 if (*p == 0)
8a3fe4f8 14081 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14082
14083 set_ignore_count (num,
14084 longest_to_int (value_as_long (parse_and_eval (p))),
14085 from_tty);
221ea385
KS
14086 if (from_tty)
14087 printf_filtered ("\n");
c906108c
SS
14088}
14089\f
14090/* Call FUNCTION on each of the breakpoints
14091 whose numbers are given in ARGS. */
14092
14093static void
95a42b64
TT
14094map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14095 void *),
14096 void *data)
c906108c 14097{
52f0bd74
AC
14098 int num;
14099 struct breakpoint *b, *tmp;
11cf8741 14100 int match;
197f0a60 14101 struct get_number_or_range_state state;
c906108c 14102
197f0a60 14103 if (args == 0)
e2e0b3e5 14104 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14105
197f0a60
TT
14106 init_number_or_range (&state, args);
14107
14108 while (!state.finished)
c906108c 14109 {
197f0a60
TT
14110 char *p = state.string;
14111
11cf8741 14112 match = 0;
c5aa993b 14113
197f0a60 14114 num = get_number_or_range (&state);
5c44784c 14115 if (num == 0)
c5aa993b 14116 {
8a3fe4f8 14117 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14118 }
14119 else
14120 {
14121 ALL_BREAKPOINTS_SAFE (b, tmp)
14122 if (b->number == num)
14123 {
11cf8741 14124 match = 1;
cdac0397 14125 function (b, data);
11cf8741 14126 break;
5c44784c 14127 }
11cf8741 14128 if (match == 0)
a3f17187 14129 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14130 }
c906108c
SS
14131 }
14132}
14133
0d381245
VP
14134static struct bp_location *
14135find_location_by_number (char *number)
14136{
14137 char *dot = strchr (number, '.');
14138 char *p1;
14139 int bp_num;
14140 int loc_num;
14141 struct breakpoint *b;
14142 struct bp_location *loc;
14143
14144 *dot = '\0';
14145
14146 p1 = number;
197f0a60 14147 bp_num = get_number (&p1);
0d381245
VP
14148 if (bp_num == 0)
14149 error (_("Bad breakpoint number '%s'"), number);
14150
14151 ALL_BREAKPOINTS (b)
14152 if (b->number == bp_num)
14153 {
14154 break;
14155 }
14156
14157 if (!b || b->number != bp_num)
14158 error (_("Bad breakpoint number '%s'"), number);
14159
14160 p1 = dot+1;
197f0a60 14161 loc_num = get_number (&p1);
0d381245
VP
14162 if (loc_num == 0)
14163 error (_("Bad breakpoint location number '%s'"), number);
14164
14165 --loc_num;
14166 loc = b->loc;
14167 for (;loc_num && loc; --loc_num, loc = loc->next)
14168 ;
14169 if (!loc)
14170 error (_("Bad breakpoint location number '%s'"), dot+1);
14171
14172 return loc;
14173}
14174
14175
1900040c
MS
14176/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14177 If from_tty is nonzero, it prints a message to that effect,
14178 which ends with a period (no newline). */
14179
c906108c 14180void
fba45db2 14181disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14182{
14183 /* Never disable a watchpoint scope breakpoint; we want to
14184 hit them when we leave scope so we can delete both the
14185 watchpoint and its scope breakpoint at that time. */
14186 if (bpt->type == bp_watchpoint_scope)
14187 return;
14188
c2c6d25f 14189 /* You can't disable permanent breakpoints. */
b5de0fa7 14190 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14191 return;
14192
b5de0fa7 14193 bpt->enable_state = bp_disabled;
c906108c 14194
b775012e
LM
14195 /* Mark breakpoint locations modified. */
14196 mark_breakpoint_modified (bpt);
14197
d248b706
KY
14198 if (target_supports_enable_disable_tracepoint ()
14199 && current_trace_status ()->running && is_tracepoint (bpt))
14200 {
14201 struct bp_location *location;
14202
14203 for (location = bpt->loc; location; location = location->next)
14204 target_disable_tracepoint (location);
14205 }
14206
b60e7edf 14207 update_global_location_list (0);
c906108c 14208
8d3788bd 14209 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14210}
14211
51be5b68
PA
14212/* A callback for iterate_over_related_breakpoints. */
14213
14214static void
14215do_disable_breakpoint (struct breakpoint *b, void *ignore)
14216{
14217 disable_breakpoint (b);
14218}
14219
95a42b64
TT
14220/* A callback for map_breakpoint_numbers that calls
14221 disable_breakpoint. */
14222
14223static void
14224do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14225{
51be5b68 14226 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14227}
14228
c906108c 14229static void
fba45db2 14230disable_command (char *args, int from_tty)
c906108c 14231{
c906108c 14232 if (args == 0)
46c6471b
PA
14233 {
14234 struct breakpoint *bpt;
14235
14236 ALL_BREAKPOINTS (bpt)
14237 if (user_breakpoint_p (bpt))
14238 disable_breakpoint (bpt);
14239 }
0d381245
VP
14240 else if (strchr (args, '.'))
14241 {
14242 struct bp_location *loc = find_location_by_number (args);
14243 if (loc)
d248b706 14244 {
b775012e
LM
14245 if (loc->enabled)
14246 {
14247 loc->enabled = 0;
14248 mark_breakpoint_location_modified (loc);
14249 }
d248b706
KY
14250 if (target_supports_enable_disable_tracepoint ()
14251 && current_trace_status ()->running && loc->owner
14252 && is_tracepoint (loc->owner))
14253 target_disable_tracepoint (loc);
14254 }
b60e7edf 14255 update_global_location_list (0);
0d381245 14256 }
c906108c 14257 else
95a42b64 14258 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
14259}
14260
14261static void
816338b5
SS
14262enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14263 int count)
c906108c 14264{
afe38095 14265 int target_resources_ok;
c906108c
SS
14266
14267 if (bpt->type == bp_hardware_breakpoint)
14268 {
14269 int i;
c5aa993b 14270 i = hw_breakpoint_used_count ();
53a5351d 14271 target_resources_ok =
d92524f1 14272 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14273 i + 1, 0);
c906108c 14274 if (target_resources_ok == 0)
8a3fe4f8 14275 error (_("No hardware breakpoint support in the target."));
c906108c 14276 else if (target_resources_ok < 0)
8a3fe4f8 14277 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14278 }
14279
cc60f2e3 14280 if (is_watchpoint (bpt))
c906108c 14281 {
d07205c2
JK
14282 /* Initialize it just to avoid a GCC false warning. */
14283 enum enable_state orig_enable_state = 0;
bfd189b1 14284 volatile struct gdb_exception e;
dde02812
ES
14285
14286 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 14287 {
3a5c3e22
PA
14288 struct watchpoint *w = (struct watchpoint *) bpt;
14289
1e718ff1
TJB
14290 orig_enable_state = bpt->enable_state;
14291 bpt->enable_state = bp_enabled;
3a5c3e22 14292 update_watchpoint (w, 1 /* reparse */);
c906108c 14293 }
dde02812 14294 if (e.reason < 0)
c5aa993b 14295 {
1e718ff1 14296 bpt->enable_state = orig_enable_state;
dde02812
ES
14297 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14298 bpt->number);
14299 return;
c5aa993b 14300 }
c906108c 14301 }
0101ce28 14302
b4c291bb
KH
14303 if (bpt->enable_state != bp_permanent)
14304 bpt->enable_state = bp_enabled;
d248b706 14305
b775012e
LM
14306 bpt->enable_state = bp_enabled;
14307
14308 /* Mark breakpoint locations modified. */
14309 mark_breakpoint_modified (bpt);
14310
d248b706
KY
14311 if (target_supports_enable_disable_tracepoint ()
14312 && current_trace_status ()->running && is_tracepoint (bpt))
14313 {
14314 struct bp_location *location;
14315
14316 for (location = bpt->loc; location; location = location->next)
14317 target_enable_tracepoint (location);
14318 }
14319
b4c291bb 14320 bpt->disposition = disposition;
816338b5 14321 bpt->enable_count = count;
b60e7edf 14322 update_global_location_list (1);
b4c291bb
KH
14323 breakpoints_changed ();
14324
8d3788bd 14325 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14326}
14327
fe3f5fa8 14328
c906108c 14329void
fba45db2 14330enable_breakpoint (struct breakpoint *bpt)
c906108c 14331{
816338b5 14332 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14333}
14334
14335static void
14336do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14337{
14338 enable_breakpoint (bpt);
c906108c
SS
14339}
14340
95a42b64
TT
14341/* A callback for map_breakpoint_numbers that calls
14342 enable_breakpoint. */
14343
14344static void
14345do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14346{
51be5b68 14347 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14348}
14349
c906108c
SS
14350/* The enable command enables the specified breakpoints (or all defined
14351 breakpoints) so they once again become (or continue to be) effective
1272ad14 14352 in stopping the inferior. */
c906108c 14353
c906108c 14354static void
fba45db2 14355enable_command (char *args, int from_tty)
c906108c 14356{
c906108c 14357 if (args == 0)
46c6471b
PA
14358 {
14359 struct breakpoint *bpt;
14360
14361 ALL_BREAKPOINTS (bpt)
14362 if (user_breakpoint_p (bpt))
14363 enable_breakpoint (bpt);
14364 }
0d381245
VP
14365 else if (strchr (args, '.'))
14366 {
14367 struct bp_location *loc = find_location_by_number (args);
14368 if (loc)
d248b706 14369 {
b775012e
LM
14370 if (!loc->enabled)
14371 {
14372 loc->enabled = 1;
14373 mark_breakpoint_location_modified (loc);
14374 }
d248b706
KY
14375 if (target_supports_enable_disable_tracepoint ()
14376 && current_trace_status ()->running && loc->owner
14377 && is_tracepoint (loc->owner))
14378 target_enable_tracepoint (loc);
14379 }
b60e7edf 14380 update_global_location_list (1);
0d381245 14381 }
c906108c 14382 else
95a42b64 14383 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
14384}
14385
816338b5
SS
14386/* This struct packages up disposition data for application to multiple
14387 breakpoints. */
14388
14389struct disp_data
14390{
14391 enum bpdisp disp;
14392 int count;
14393};
14394
c906108c 14395static void
51be5b68
PA
14396do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14397{
816338b5 14398 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14399
816338b5 14400 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14401}
14402
14403static void
14404do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14405{
816338b5 14406 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14407
14408 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14409}
14410
c906108c 14411static void
fba45db2 14412enable_once_command (char *args, int from_tty)
c906108c 14413{
51be5b68 14414 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14415}
14416
816338b5
SS
14417static void
14418do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14419{
14420 struct disp_data disp = { disp_disable, *(int *) countptr };
14421
14422 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14423}
14424
14425static void
14426enable_count_command (char *args, int from_tty)
14427{
14428 int count = get_number (&args);
14429
14430 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14431}
14432
c906108c 14433static void
51be5b68 14434do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14435{
816338b5 14436 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14437
14438 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14439}
14440
c906108c 14441static void
fba45db2 14442enable_delete_command (char *args, int from_tty)
c906108c 14443{
51be5b68 14444 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14445}
14446\f
fa8d40ab
JJ
14447static void
14448set_breakpoint_cmd (char *args, int from_tty)
14449{
14450}
14451
14452static void
14453show_breakpoint_cmd (char *args, int from_tty)
14454{
14455}
14456
1f3b5d1b
PP
14457/* Invalidate last known value of any hardware watchpoint if
14458 the memory which that value represents has been written to by
14459 GDB itself. */
14460
14461static void
14462invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
14463 const bfd_byte *data)
14464{
14465 struct breakpoint *bp;
14466
14467 ALL_BREAKPOINTS (bp)
14468 if (bp->enable_state == bp_enabled
3a5c3e22 14469 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14470 {
3a5c3e22 14471 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14472
3a5c3e22
PA
14473 if (wp->val_valid && wp->val)
14474 {
14475 struct bp_location *loc;
14476
14477 for (loc = bp->loc; loc != NULL; loc = loc->next)
14478 if (loc->loc_type == bp_loc_hardware_watchpoint
14479 && loc->address + loc->length > addr
14480 && addr + len > loc->address)
14481 {
14482 value_free (wp->val);
14483 wp->val = NULL;
14484 wp->val_valid = 0;
14485 }
14486 }
1f3b5d1b
PP
14487 }
14488}
14489
8181d85f
DJ
14490/* Create and insert a raw software breakpoint at PC. Return an
14491 identifier, which should be used to remove the breakpoint later.
14492 In general, places which call this should be using something on the
14493 breakpoint chain instead; this function should be eliminated
14494 someday. */
14495
14496void *
6c95b8df
PA
14497deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14498 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
14499{
14500 struct bp_target_info *bp_tgt;
14501
6c95b8df 14502 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 14503
6c95b8df 14504 bp_tgt->placed_address_space = aspace;
8181d85f 14505 bp_tgt->placed_address = pc;
6c95b8df 14506
a6d9a66e 14507 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
14508 {
14509 /* Could not insert the breakpoint. */
14510 xfree (bp_tgt);
14511 return NULL;
14512 }
14513
14514 return bp_tgt;
14515}
14516
4a64f543
MS
14517/* Remove a breakpoint BP inserted by
14518 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
14519
14520int
a6d9a66e 14521deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
14522{
14523 struct bp_target_info *bp_tgt = bp;
14524 int ret;
14525
a6d9a66e 14526 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
14527 xfree (bp_tgt);
14528
14529 return ret;
14530}
14531
4a64f543
MS
14532/* One (or perhaps two) breakpoints used for software single
14533 stepping. */
8181d85f
DJ
14534
14535static void *single_step_breakpoints[2];
a6d9a66e 14536static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
14537
14538/* Create and insert a breakpoint for software single step. */
14539
14540void
6c95b8df 14541insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14542 struct address_space *aspace,
14543 CORE_ADDR next_pc)
8181d85f
DJ
14544{
14545 void **bpt_p;
14546
14547 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
14548 {
14549 bpt_p = &single_step_breakpoints[0];
14550 single_step_gdbarch[0] = gdbarch;
14551 }
8181d85f
DJ
14552 else
14553 {
14554 gdb_assert (single_step_breakpoints[1] == NULL);
14555 bpt_p = &single_step_breakpoints[1];
a6d9a66e 14556 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
14557 }
14558
4a64f543
MS
14559 /* NOTE drow/2006-04-11: A future improvement to this function would
14560 be to only create the breakpoints once, and actually put them on
14561 the breakpoint chain. That would let us use set_raw_breakpoint.
14562 We could adjust the addresses each time they were needed. Doing
14563 this requires corresponding changes elsewhere where single step
14564 breakpoints are handled, however. So, for now, we use this. */
8181d85f 14565
6c95b8df 14566 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 14567 if (*bpt_p == NULL)
5af949e3
UW
14568 error (_("Could not insert single-step breakpoint at %s"),
14569 paddress (gdbarch, next_pc));
8181d85f
DJ
14570}
14571
f02253f1
HZ
14572/* Check if the breakpoints used for software single stepping
14573 were inserted or not. */
14574
14575int
14576single_step_breakpoints_inserted (void)
14577{
14578 return (single_step_breakpoints[0] != NULL
14579 || single_step_breakpoints[1] != NULL);
14580}
14581
8181d85f
DJ
14582/* Remove and delete any breakpoints used for software single step. */
14583
14584void
14585remove_single_step_breakpoints (void)
14586{
14587 gdb_assert (single_step_breakpoints[0] != NULL);
14588
14589 /* See insert_single_step_breakpoint for more about this deprecated
14590 call. */
a6d9a66e
UW
14591 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
14592 single_step_breakpoints[0]);
14593 single_step_gdbarch[0] = NULL;
8181d85f
DJ
14594 single_step_breakpoints[0] = NULL;
14595
14596 if (single_step_breakpoints[1] != NULL)
14597 {
a6d9a66e
UW
14598 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
14599 single_step_breakpoints[1]);
14600 single_step_gdbarch[1] = NULL;
8181d85f
DJ
14601 single_step_breakpoints[1] = NULL;
14602 }
14603}
14604
d03285ec
UW
14605/* Delete software single step breakpoints without removing them from
14606 the inferior. This is intended to be used if the inferior's address
14607 space where they were inserted is already gone, e.g. after exit or
14608 exec. */
14609
14610void
14611cancel_single_step_breakpoints (void)
14612{
14613 int i;
14614
14615 for (i = 0; i < 2; i++)
14616 if (single_step_breakpoints[i])
14617 {
14618 xfree (single_step_breakpoints[i]);
14619 single_step_breakpoints[i] = NULL;
14620 single_step_gdbarch[i] = NULL;
14621 }
14622}
14623
14624/* Detach software single-step breakpoints from INFERIOR_PTID without
14625 removing them. */
14626
14627static void
14628detach_single_step_breakpoints (void)
14629{
14630 int i;
14631
14632 for (i = 0; i < 2; i++)
14633 if (single_step_breakpoints[i])
14634 target_remove_breakpoint (single_step_gdbarch[i],
14635 single_step_breakpoints[i]);
14636}
14637
4a64f543
MS
14638/* Check whether a software single-step breakpoint is inserted at
14639 PC. */
1aafd4da
UW
14640
14641static int
cc59ec59
MS
14642single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14643 CORE_ADDR pc)
1aafd4da
UW
14644{
14645 int i;
14646
14647 for (i = 0; i < 2; i++)
14648 {
14649 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
14650 if (bp_tgt
14651 && breakpoint_address_match (bp_tgt->placed_address_space,
14652 bp_tgt->placed_address,
14653 aspace, pc))
1aafd4da
UW
14654 return 1;
14655 }
14656
14657 return 0;
14658}
14659
a96d9b2e
SDJ
14660/* Returns 0 if 'bp' is NOT a syscall catchpoint,
14661 non-zero otherwise. */
14662static int
14663is_syscall_catchpoint_enabled (struct breakpoint *bp)
14664{
14665 if (syscall_catchpoint_p (bp)
14666 && bp->enable_state != bp_disabled
14667 && bp->enable_state != bp_call_disabled)
14668 return 1;
14669 else
14670 return 0;
14671}
14672
14673int
14674catch_syscall_enabled (void)
14675{
fa3064dd
YQ
14676 struct catch_syscall_inferior_data *inf_data
14677 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 14678
fa3064dd 14679 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
14680}
14681
14682int
14683catching_syscall_number (int syscall_number)
14684{
14685 struct breakpoint *bp;
14686
14687 ALL_BREAKPOINTS (bp)
14688 if (is_syscall_catchpoint_enabled (bp))
14689 {
be5c67c1
PA
14690 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
14691
14692 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
14693 {
14694 int i, iter;
14695 for (i = 0;
be5c67c1 14696 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
14697 i++)
14698 if (syscall_number == iter)
14699 return 1;
14700 }
14701 else
14702 return 1;
14703 }
14704
14705 return 0;
14706}
14707
14708/* Complete syscall names. Used by "catch syscall". */
49c4e619 14709static VEC (char_ptr) *
a96d9b2e
SDJ
14710catch_syscall_completer (struct cmd_list_element *cmd,
14711 char *text, char *word)
14712{
14713 const char **list = get_syscall_names ();
49c4e619 14714 VEC (char_ptr) *retlist
c38eea1a 14715 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 14716
c38eea1a
MS
14717 xfree (list);
14718 return retlist;
a96d9b2e
SDJ
14719}
14720
1042e4c0
SS
14721/* Tracepoint-specific operations. */
14722
14723/* Set tracepoint count to NUM. */
14724static void
14725set_tracepoint_count (int num)
14726{
14727 tracepoint_count = num;
4fa62494 14728 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14729}
14730
70221824 14731static void
1042e4c0
SS
14732trace_command (char *arg, int from_tty)
14733{
55aa24fb
SDJ
14734 struct breakpoint_ops *ops;
14735 const char *arg_cp = arg;
14736
14737 if (arg && probe_linespec_to_ops (&arg_cp))
14738 ops = &tracepoint_probe_breakpoint_ops;
14739 else
14740 ops = &tracepoint_breakpoint_ops;
14741
8cdf0e15
VP
14742 if (create_breakpoint (get_current_arch (),
14743 arg,
e7e0cddf 14744 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14745 0 /* tempflag */,
14746 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
14747 0 /* Ignore count */,
14748 pending_break_support,
55aa24fb 14749 ops,
8cdf0e15 14750 from_tty,
84f4c1fe 14751 1 /* enabled */,
44f238bb 14752 0 /* internal */, 0))
fd9b8c24 14753 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
14754}
14755
70221824 14756static void
7a697b8d
SS
14757ftrace_command (char *arg, int from_tty)
14758{
8cdf0e15
VP
14759 if (create_breakpoint (get_current_arch (),
14760 arg,
e7e0cddf 14761 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14762 0 /* tempflag */,
14763 bp_fast_tracepoint /* type_wanted */,
14764 0 /* Ignore count */,
14765 pending_break_support,
348d480f 14766 &tracepoint_breakpoint_ops,
0fb4aa4b 14767 from_tty,
84f4c1fe 14768 1 /* enabled */,
44f238bb 14769 0 /* internal */, 0))
0fb4aa4b
PA
14770 set_tracepoint_count (breakpoint_count);
14771}
14772
14773/* strace command implementation. Creates a static tracepoint. */
14774
70221824 14775static void
0fb4aa4b
PA
14776strace_command (char *arg, int from_tty)
14777{
983af33b
SDJ
14778 struct breakpoint_ops *ops;
14779
14780 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14781 or with a normal static tracepoint. */
14782 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
14783 ops = &strace_marker_breakpoint_ops;
14784 else
14785 ops = &tracepoint_breakpoint_ops;
14786
0fb4aa4b
PA
14787 if (create_breakpoint (get_current_arch (),
14788 arg,
e7e0cddf 14789 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14790 0 /* tempflag */,
14791 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
14792 0 /* Ignore count */,
14793 pending_break_support,
983af33b 14794 ops,
8cdf0e15 14795 from_tty,
84f4c1fe 14796 1 /* enabled */,
44f238bb 14797 0 /* internal */, 0))
fd9b8c24 14798 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
14799}
14800
409873ef
SS
14801/* Set up a fake reader function that gets command lines from a linked
14802 list that was acquired during tracepoint uploading. */
14803
14804static struct uploaded_tp *this_utp;
3149d8c1 14805static int next_cmd;
409873ef
SS
14806
14807static char *
14808read_uploaded_action (void)
14809{
14810 char *rslt;
14811
3149d8c1 14812 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14813
3149d8c1 14814 next_cmd++;
409873ef
SS
14815
14816 return rslt;
14817}
14818
00bf0b85
SS
14819/* Given information about a tracepoint as recorded on a target (which
14820 can be either a live system or a trace file), attempt to create an
14821 equivalent GDB tracepoint. This is not a reliable process, since
14822 the target does not necessarily have all the information used when
14823 the tracepoint was originally defined. */
14824
d9b3f62e 14825struct tracepoint *
00bf0b85 14826create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14827{
409873ef 14828 char *addr_str, small_buf[100];
d9b3f62e 14829 struct tracepoint *tp;
fd9b8c24 14830
409873ef
SS
14831 if (utp->at_string)
14832 addr_str = utp->at_string;
14833 else
14834 {
14835 /* In the absence of a source location, fall back to raw
14836 address. Since there is no way to confirm that the address
14837 means the same thing as when the trace was started, warn the
14838 user. */
3e43a32a
MS
14839 warning (_("Uploaded tracepoint %d has no "
14840 "source location, using raw address"),
409873ef
SS
14841 utp->number);
14842 sprintf (small_buf, "*%s", hex_string (utp->addr));
14843 addr_str = small_buf;
14844 }
14845
14846 /* There's not much we can do with a sequence of bytecodes. */
14847 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14848 warning (_("Uploaded tracepoint %d condition "
14849 "has no source form, ignoring it"),
409873ef 14850 utp->number);
d5551862 14851
8cdf0e15 14852 if (!create_breakpoint (get_current_arch (),
409873ef 14853 addr_str,
e7e0cddf
SS
14854 utp->cond_string, -1, NULL,
14855 0 /* parse cond/thread */,
8cdf0e15 14856 0 /* tempflag */,
0fb4aa4b 14857 utp->type /* type_wanted */,
8cdf0e15
VP
14858 0 /* Ignore count */,
14859 pending_break_support,
348d480f 14860 &tracepoint_breakpoint_ops,
8cdf0e15 14861 0 /* from_tty */,
84f4c1fe 14862 utp->enabled /* enabled */,
44f238bb
PA
14863 0 /* internal */,
14864 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
14865 return NULL;
14866
00bf0b85
SS
14867 set_tracepoint_count (breakpoint_count);
14868
409873ef 14869 /* Get the tracepoint we just created. */
fd9b8c24
PA
14870 tp = get_tracepoint (tracepoint_count);
14871 gdb_assert (tp != NULL);
d5551862 14872
00bf0b85
SS
14873 if (utp->pass > 0)
14874 {
d9b3f62e 14875 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 14876
409873ef 14877 trace_pass_command (small_buf, 0);
00bf0b85
SS
14878 }
14879
409873ef
SS
14880 /* If we have uploaded versions of the original commands, set up a
14881 special-purpose "reader" function and call the usual command line
14882 reader, then pass the result to the breakpoint command-setting
14883 function. */
3149d8c1 14884 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 14885 {
409873ef 14886 struct command_line *cmd_list;
00bf0b85 14887
409873ef 14888 this_utp = utp;
3149d8c1 14889 next_cmd = 0;
d5551862 14890
409873ef
SS
14891 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14892
d9b3f62e 14893 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 14894 }
3149d8c1
SS
14895 else if (!VEC_empty (char_ptr, utp->actions)
14896 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
14897 warning (_("Uploaded tracepoint %d actions "
14898 "have no source form, ignoring them"),
409873ef 14899 utp->number);
00bf0b85 14900
f196051f
SS
14901 /* Copy any status information that might be available. */
14902 tp->base.hit_count = utp->hit_count;
14903 tp->traceframe_usage = utp->traceframe_usage;
14904
00bf0b85 14905 return tp;
d9b3f62e 14906}
00bf0b85 14907
1042e4c0
SS
14908/* Print information on tracepoint number TPNUM_EXP, or all if
14909 omitted. */
14910
14911static void
e5a67952 14912tracepoints_info (char *args, int from_tty)
1042e4c0 14913{
79a45e25 14914 struct ui_out *uiout = current_uiout;
e5a67952 14915 int num_printed;
1042e4c0 14916
e5a67952 14917 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14918
14919 if (num_printed == 0)
1042e4c0 14920 {
e5a67952 14921 if (args == NULL || *args == '\0')
d77f58be
SS
14922 ui_out_message (uiout, 0, "No tracepoints.\n");
14923 else
e5a67952 14924 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 14925 }
ad443146
SS
14926
14927 default_collect_info ();
1042e4c0
SS
14928}
14929
4a64f543 14930/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14931 Not supported by all targets. */
14932static void
14933enable_trace_command (char *args, int from_tty)
14934{
14935 enable_command (args, from_tty);
14936}
14937
4a64f543 14938/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14939 Not supported by all targets. */
14940static void
14941disable_trace_command (char *args, int from_tty)
14942{
14943 disable_command (args, from_tty);
14944}
14945
4a64f543 14946/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
14947static void
14948delete_trace_command (char *arg, int from_tty)
14949{
35df4500 14950 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14951
14952 dont_repeat ();
14953
14954 if (arg == 0)
14955 {
14956 int breaks_to_delete = 0;
14957
14958 /* Delete all breakpoints if no argument.
14959 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14960 have to be deleted with an explicit breakpoint number
14961 argument. */
1042e4c0 14962 ALL_TRACEPOINTS (b)
46c6471b 14963 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14964 {
14965 breaks_to_delete = 1;
14966 break;
14967 }
1042e4c0
SS
14968
14969 /* Ask user only if there are some breakpoints to delete. */
14970 if (!from_tty
14971 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14972 {
35df4500 14973 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14974 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14975 delete_breakpoint (b);
1042e4c0
SS
14976 }
14977 }
14978 else
51be5b68 14979 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
14980}
14981
197f0a60
TT
14982/* Helper function for trace_pass_command. */
14983
14984static void
d9b3f62e 14985trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14986{
d9b3f62e
PA
14987 tp->pass_count = count;
14988 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
14989 if (from_tty)
14990 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 14991 tp->base.number, count);
197f0a60
TT
14992}
14993
1042e4c0
SS
14994/* Set passcount for tracepoint.
14995
14996 First command argument is passcount, second is tracepoint number.
14997 If tracepoint number omitted, apply to most recently defined.
14998 Also accepts special argument "all". */
14999
15000static void
15001trace_pass_command (char *args, int from_tty)
15002{
d9b3f62e 15003 struct tracepoint *t1;
1042e4c0 15004 unsigned int count;
1042e4c0
SS
15005
15006 if (args == 0 || *args == 0)
3e43a32a
MS
15007 error (_("passcount command requires an "
15008 "argument (count + optional TP num)"));
1042e4c0 15009
4a64f543 15010 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
15011
15012 while (*args && isspace ((int) *args))
15013 args++;
15014
15015 if (*args && strncasecmp (args, "all", 3) == 0)
15016 {
d9b3f62e
PA
15017 struct breakpoint *b;
15018
1042e4c0 15019 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15020 if (*args)
15021 error (_("Junk at end of arguments."));
1042e4c0 15022
d9b3f62e 15023 ALL_TRACEPOINTS (b)
197f0a60 15024 {
d9b3f62e 15025 t1 = (struct tracepoint *) b;
197f0a60
TT
15026 trace_pass_set_count (t1, count, from_tty);
15027 }
15028 }
15029 else if (*args == '\0')
1042e4c0 15030 {
197f0a60 15031 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 15032 if (t1)
197f0a60
TT
15033 trace_pass_set_count (t1, count, from_tty);
15034 }
15035 else
15036 {
15037 struct get_number_or_range_state state;
15038
15039 init_number_or_range (&state, args);
15040 while (!state.finished)
1042e4c0 15041 {
197f0a60
TT
15042 t1 = get_tracepoint_by_number (&args, &state, 1);
15043 if (t1)
15044 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15045 }
15046 }
1042e4c0
SS
15047}
15048
d9b3f62e 15049struct tracepoint *
1042e4c0
SS
15050get_tracepoint (int num)
15051{
15052 struct breakpoint *t;
15053
15054 ALL_TRACEPOINTS (t)
15055 if (t->number == num)
d9b3f62e 15056 return (struct tracepoint *) t;
1042e4c0
SS
15057
15058 return NULL;
15059}
15060
d5551862
SS
15061/* Find the tracepoint with the given target-side number (which may be
15062 different from the tracepoint number after disconnecting and
15063 reconnecting). */
15064
d9b3f62e 15065struct tracepoint *
d5551862
SS
15066get_tracepoint_by_number_on_target (int num)
15067{
d9b3f62e 15068 struct breakpoint *b;
d5551862 15069
d9b3f62e
PA
15070 ALL_TRACEPOINTS (b)
15071 {
15072 struct tracepoint *t = (struct tracepoint *) b;
15073
15074 if (t->number_on_target == num)
15075 return t;
15076 }
d5551862
SS
15077
15078 return NULL;
15079}
15080
1042e4c0 15081/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
15082 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15083 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 15084 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 15085struct tracepoint *
197f0a60
TT
15086get_tracepoint_by_number (char **arg,
15087 struct get_number_or_range_state *state,
15088 int optional_p)
1042e4c0
SS
15089{
15090 extern int tracepoint_count;
15091 struct breakpoint *t;
15092 int tpnum;
15093 char *instring = arg == NULL ? NULL : *arg;
15094
197f0a60
TT
15095 if (state)
15096 {
15097 gdb_assert (!state->finished);
15098 tpnum = get_number_or_range (state);
15099 }
15100 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
15101 {
15102 if (optional_p)
15103 tpnum = tracepoint_count;
15104 else
15105 error_no_arg (_("tracepoint number"));
15106 }
15107 else
197f0a60 15108 tpnum = get_number (arg);
1042e4c0
SS
15109
15110 if (tpnum <= 0)
15111 {
15112 if (instring && *instring)
15113 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15114 instring);
15115 else
3e43a32a
MS
15116 printf_filtered (_("Tracepoint argument missing "
15117 "and no previous tracepoint\n"));
1042e4c0
SS
15118 return NULL;
15119 }
15120
15121 ALL_TRACEPOINTS (t)
15122 if (t->number == tpnum)
15123 {
d9b3f62e 15124 return (struct tracepoint *) t;
1042e4c0
SS
15125 }
15126
1042e4c0
SS
15127 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15128 return NULL;
15129}
15130
d9b3f62e
PA
15131void
15132print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15133{
15134 if (b->thread != -1)
15135 fprintf_unfiltered (fp, " thread %d", b->thread);
15136
15137 if (b->task != 0)
15138 fprintf_unfiltered (fp, " task %d", b->task);
15139
15140 fprintf_unfiltered (fp, "\n");
15141}
15142
6149aea9
PA
15143/* Save information on user settable breakpoints (watchpoints, etc) to
15144 a new script file named FILENAME. If FILTER is non-NULL, call it
15145 on each breakpoint and only include the ones for which it returns
15146 non-zero. */
15147
1042e4c0 15148static void
6149aea9
PA
15149save_breakpoints (char *filename, int from_tty,
15150 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15151{
15152 struct breakpoint *tp;
6149aea9 15153 int any = 0;
a7bdde9e 15154 char *pathname;
1042e4c0 15155 struct cleanup *cleanup;
a7bdde9e 15156 struct ui_file *fp;
6149aea9 15157 int extra_trace_bits = 0;
1042e4c0 15158
6149aea9
PA
15159 if (filename == 0 || *filename == 0)
15160 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15161
15162 /* See if we have anything to save. */
6149aea9 15163 ALL_BREAKPOINTS (tp)
1042e4c0 15164 {
6149aea9 15165 /* Skip internal and momentary breakpoints. */
09d682a4 15166 if (!user_breakpoint_p (tp))
6149aea9
PA
15167 continue;
15168
15169 /* If we have a filter, only save the breakpoints it accepts. */
15170 if (filter && !filter (tp))
15171 continue;
15172
15173 any = 1;
15174
15175 if (is_tracepoint (tp))
15176 {
15177 extra_trace_bits = 1;
15178
15179 /* We can stop searching. */
15180 break;
15181 }
1042e4c0 15182 }
6149aea9
PA
15183
15184 if (!any)
1042e4c0 15185 {
6149aea9 15186 warning (_("Nothing to save."));
1042e4c0
SS
15187 return;
15188 }
15189
6149aea9 15190 pathname = tilde_expand (filename);
1042e4c0 15191 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 15192 fp = gdb_fopen (pathname, "w");
059fb39f 15193 if (!fp)
6149aea9
PA
15194 error (_("Unable to open file '%s' for saving (%s)"),
15195 filename, safe_strerror (errno));
a7bdde9e 15196 make_cleanup_ui_file_delete (fp);
8bf6485c 15197
6149aea9
PA
15198 if (extra_trace_bits)
15199 save_trace_state_variables (fp);
8bf6485c 15200
6149aea9 15201 ALL_BREAKPOINTS (tp)
1042e4c0 15202 {
6149aea9 15203 /* Skip internal and momentary breakpoints. */
09d682a4 15204 if (!user_breakpoint_p (tp))
6149aea9 15205 continue;
8bf6485c 15206
6149aea9
PA
15207 /* If we have a filter, only save the breakpoints it accepts. */
15208 if (filter && !filter (tp))
15209 continue;
15210
348d480f 15211 tp->ops->print_recreate (tp, fp);
1042e4c0 15212
6149aea9
PA
15213 /* Note, we can't rely on tp->number for anything, as we can't
15214 assume the recreated breakpoint numbers will match. Use $bpnum
15215 instead. */
15216
15217 if (tp->cond_string)
15218 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15219
15220 if (tp->ignore_count)
15221 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15222
a7bdde9e 15223 if (tp->commands)
1042e4c0 15224 {
a7bdde9e
VP
15225 volatile struct gdb_exception ex;
15226
6149aea9 15227 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15228
79a45e25 15229 ui_out_redirect (current_uiout, fp);
14dba4b4 15230 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15231 {
79a45e25 15232 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15233 }
79a45e25 15234 ui_out_redirect (current_uiout, NULL);
1042e4c0 15235
a7bdde9e
VP
15236 if (ex.reason < 0)
15237 throw_exception (ex);
1042e4c0 15238
a7bdde9e 15239 fprintf_unfiltered (fp, " end\n");
1042e4c0 15240 }
6149aea9
PA
15241
15242 if (tp->enable_state == bp_disabled)
15243 fprintf_unfiltered (fp, "disable\n");
15244
15245 /* If this is a multi-location breakpoint, check if the locations
15246 should be individually disabled. Watchpoint locations are
15247 special, and not user visible. */
15248 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15249 {
15250 struct bp_location *loc;
15251 int n = 1;
15252
15253 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15254 if (!loc->enabled)
15255 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15256 }
1042e4c0 15257 }
8bf6485c 15258
6149aea9 15259 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15260 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15261
1042e4c0
SS
15262 do_cleanups (cleanup);
15263 if (from_tty)
6149aea9
PA
15264 printf_filtered (_("Saved to file '%s'.\n"), filename);
15265}
15266
15267/* The `save breakpoints' command. */
15268
15269static void
15270save_breakpoints_command (char *args, int from_tty)
15271{
15272 save_breakpoints (args, from_tty, NULL);
15273}
15274
15275/* The `save tracepoints' command. */
15276
15277static void
15278save_tracepoints_command (char *args, int from_tty)
15279{
15280 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15281}
15282
15283/* Create a vector of all tracepoints. */
15284
15285VEC(breakpoint_p) *
eeae04df 15286all_tracepoints (void)
1042e4c0
SS
15287{
15288 VEC(breakpoint_p) *tp_vec = 0;
15289 struct breakpoint *tp;
15290
15291 ALL_TRACEPOINTS (tp)
15292 {
15293 VEC_safe_push (breakpoint_p, tp_vec, tp);
15294 }
15295
15296 return tp_vec;
15297}
15298
c906108c 15299\f
4a64f543
MS
15300/* This help string is used for the break, hbreak, tbreak and thbreak
15301 commands. It is defined as a macro to prevent duplication.
15302 COMMAND should be a string constant containing the name of the
15303 command. */
31e2b00f 15304#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15305command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15306PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15307probe point. Accepted values are `-probe' (for a generic, automatically\n\
15308guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
15309LOCATION may be a line number, function name, or \"*\" and an address.\n\
15310If a line number is specified, break at start of code for that line.\n\
15311If a function is specified, break at start of code for that function.\n\
15312If an address is specified, break at that exact address.\n\
dc10affe
PA
15313With no LOCATION, uses current execution address of the selected\n\
15314stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15315\n\
15316THREADNUM is the number from \"info threads\".\n\
15317CONDITION is a boolean expression.\n\
15318\n\
d41c0fc8
PA
15319Multiple breakpoints at one place are permitted, and useful if their\n\
15320conditions are different.\n\
31e2b00f
AS
15321\n\
15322Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15323
44feb3ce
TT
15324/* List of subcommands for "catch". */
15325static struct cmd_list_element *catch_cmdlist;
15326
15327/* List of subcommands for "tcatch". */
15328static struct cmd_list_element *tcatch_cmdlist;
15329
9ac4176b 15330void
44feb3ce
TT
15331add_catch_command (char *name, char *docstring,
15332 void (*sfunc) (char *args, int from_tty,
15333 struct cmd_list_element *command),
625e8578 15334 completer_ftype *completer,
44feb3ce
TT
15335 void *user_data_catch,
15336 void *user_data_tcatch)
15337{
15338 struct cmd_list_element *command;
15339
15340 command = add_cmd (name, class_breakpoint, NULL, docstring,
15341 &catch_cmdlist);
15342 set_cmd_sfunc (command, sfunc);
15343 set_cmd_context (command, user_data_catch);
a96d9b2e 15344 set_cmd_completer (command, completer);
44feb3ce
TT
15345
15346 command = add_cmd (name, class_breakpoint, NULL, docstring,
15347 &tcatch_cmdlist);
15348 set_cmd_sfunc (command, sfunc);
15349 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15350 set_cmd_completer (command, completer);
44feb3ce
TT
15351}
15352
6c95b8df 15353static void
a79b8f6e 15354clear_syscall_counts (struct inferior *inf)
6c95b8df 15355{
fa3064dd
YQ
15356 struct catch_syscall_inferior_data *inf_data
15357 = get_catch_syscall_inferior_data (inf);
15358
15359 inf_data->total_syscalls_count = 0;
15360 inf_data->any_syscall_count = 0;
15361 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
15362}
15363
6149aea9
PA
15364static void
15365save_command (char *arg, int from_tty)
15366{
3e43a32a
MS
15367 printf_unfiltered (_("\"save\" must be followed by "
15368 "the name of a save subcommand.\n"));
6149aea9
PA
15369 help_list (save_cmdlist, "save ", -1, gdb_stdout);
15370}
15371
84f4c1fe
PM
15372struct breakpoint *
15373iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15374 void *data)
15375{
35df4500 15376 struct breakpoint *b, *b_tmp;
84f4c1fe 15377
35df4500 15378 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15379 {
15380 if ((*callback) (b, data))
15381 return b;
15382 }
15383
15384 return NULL;
15385}
15386
0574c78f
GB
15387/* Zero if any of the breakpoint's locations could be a location where
15388 functions have been inlined, nonzero otherwise. */
15389
15390static int
15391is_non_inline_function (struct breakpoint *b)
15392{
15393 /* The shared library event breakpoint is set on the address of a
15394 non-inline function. */
15395 if (b->type == bp_shlib_event)
15396 return 1;
15397
15398 return 0;
15399}
15400
15401/* Nonzero if the specified PC cannot be a location where functions
15402 have been inlined. */
15403
15404int
09ac7c10
TT
15405pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15406 const struct target_waitstatus *ws)
0574c78f
GB
15407{
15408 struct breakpoint *b;
15409 struct bp_location *bl;
15410
15411 ALL_BREAKPOINTS (b)
15412 {
15413 if (!is_non_inline_function (b))
15414 continue;
15415
15416 for (bl = b->loc; bl != NULL; bl = bl->next)
15417 {
15418 if (!bl->shlib_disabled
09ac7c10 15419 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15420 return 1;
15421 }
15422 }
15423
15424 return 0;
15425}
15426
2060206e
PA
15427void
15428initialize_breakpoint_ops (void)
15429{
15430 static int initialized = 0;
15431
15432 struct breakpoint_ops *ops;
15433
15434 if (initialized)
15435 return;
15436 initialized = 1;
15437
15438 /* The breakpoint_ops structure to be inherit by all kinds of
15439 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15440 internal and momentary breakpoints, etc.). */
15441 ops = &bkpt_base_breakpoint_ops;
15442 *ops = base_breakpoint_ops;
15443 ops->re_set = bkpt_re_set;
15444 ops->insert_location = bkpt_insert_location;
15445 ops->remove_location = bkpt_remove_location;
15446 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
15447 ops->create_sals_from_address = bkpt_create_sals_from_address;
15448 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15449 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
15450
15451 /* The breakpoint_ops structure to be used in regular breakpoints. */
15452 ops = &bkpt_breakpoint_ops;
15453 *ops = bkpt_base_breakpoint_ops;
15454 ops->re_set = bkpt_re_set;
15455 ops->resources_needed = bkpt_resources_needed;
15456 ops->print_it = bkpt_print_it;
15457 ops->print_mention = bkpt_print_mention;
15458 ops->print_recreate = bkpt_print_recreate;
15459
15460 /* Ranged breakpoints. */
15461 ops = &ranged_breakpoint_ops;
15462 *ops = bkpt_breakpoint_ops;
15463 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15464 ops->resources_needed = resources_needed_ranged_breakpoint;
15465 ops->print_it = print_it_ranged_breakpoint;
15466 ops->print_one = print_one_ranged_breakpoint;
15467 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15468 ops->print_mention = print_mention_ranged_breakpoint;
15469 ops->print_recreate = print_recreate_ranged_breakpoint;
15470
15471 /* Internal breakpoints. */
15472 ops = &internal_breakpoint_ops;
15473 *ops = bkpt_base_breakpoint_ops;
15474 ops->re_set = internal_bkpt_re_set;
15475 ops->check_status = internal_bkpt_check_status;
15476 ops->print_it = internal_bkpt_print_it;
15477 ops->print_mention = internal_bkpt_print_mention;
15478
15479 /* Momentary breakpoints. */
15480 ops = &momentary_breakpoint_ops;
15481 *ops = bkpt_base_breakpoint_ops;
15482 ops->re_set = momentary_bkpt_re_set;
15483 ops->check_status = momentary_bkpt_check_status;
15484 ops->print_it = momentary_bkpt_print_it;
15485 ops->print_mention = momentary_bkpt_print_mention;
15486
e2e4d78b
JK
15487 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15488 ops = &longjmp_breakpoint_ops;
15489 *ops = momentary_breakpoint_ops;
15490 ops->dtor = longjmp_bkpt_dtor;
15491
55aa24fb
SDJ
15492 /* Probe breakpoints. */
15493 ops = &bkpt_probe_breakpoint_ops;
15494 *ops = bkpt_breakpoint_ops;
15495 ops->insert_location = bkpt_probe_insert_location;
15496 ops->remove_location = bkpt_probe_remove_location;
15497 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15498 ops->decode_linespec = bkpt_probe_decode_linespec;
15499
2060206e
PA
15500 /* GNU v3 exception catchpoints. */
15501 ops = &gnu_v3_exception_catchpoint_ops;
15502 *ops = bkpt_breakpoint_ops;
15503 ops->print_it = print_it_exception_catchpoint;
15504 ops->print_one = print_one_exception_catchpoint;
15505 ops->print_mention = print_mention_exception_catchpoint;
15506 ops->print_recreate = print_recreate_exception_catchpoint;
15507
15508 /* Watchpoints. */
15509 ops = &watchpoint_breakpoint_ops;
15510 *ops = base_breakpoint_ops;
3a5c3e22 15511 ops->dtor = dtor_watchpoint;
2060206e
PA
15512 ops->re_set = re_set_watchpoint;
15513 ops->insert_location = insert_watchpoint;
15514 ops->remove_location = remove_watchpoint;
15515 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15516 ops->check_status = check_status_watchpoint;
15517 ops->resources_needed = resources_needed_watchpoint;
15518 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15519 ops->print_it = print_it_watchpoint;
15520 ops->print_mention = print_mention_watchpoint;
15521 ops->print_recreate = print_recreate_watchpoint;
15522
15523 /* Masked watchpoints. */
15524 ops = &masked_watchpoint_breakpoint_ops;
15525 *ops = watchpoint_breakpoint_ops;
15526 ops->insert_location = insert_masked_watchpoint;
15527 ops->remove_location = remove_masked_watchpoint;
15528 ops->resources_needed = resources_needed_masked_watchpoint;
15529 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15530 ops->print_it = print_it_masked_watchpoint;
15531 ops->print_one_detail = print_one_detail_masked_watchpoint;
15532 ops->print_mention = print_mention_masked_watchpoint;
15533 ops->print_recreate = print_recreate_masked_watchpoint;
15534
15535 /* Tracepoints. */
15536 ops = &tracepoint_breakpoint_ops;
15537 *ops = base_breakpoint_ops;
15538 ops->re_set = tracepoint_re_set;
15539 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15540 ops->print_one_detail = tracepoint_print_one_detail;
15541 ops->print_mention = tracepoint_print_mention;
15542 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
15543 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15544 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15545 ops->decode_linespec = tracepoint_decode_linespec;
15546
55aa24fb
SDJ
15547 /* Probe tracepoints. */
15548 ops = &tracepoint_probe_breakpoint_ops;
15549 *ops = tracepoint_breakpoint_ops;
15550 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15551 ops->decode_linespec = tracepoint_probe_decode_linespec;
15552
983af33b
SDJ
15553 /* Static tracepoints with marker (`-m'). */
15554 ops = &strace_marker_breakpoint_ops;
15555 *ops = tracepoint_breakpoint_ops;
15556 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15557 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15558 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
15559
15560 /* Fork catchpoints. */
15561 ops = &catch_fork_breakpoint_ops;
15562 *ops = base_breakpoint_ops;
15563 ops->insert_location = insert_catch_fork;
15564 ops->remove_location = remove_catch_fork;
15565 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15566 ops->print_it = print_it_catch_fork;
15567 ops->print_one = print_one_catch_fork;
15568 ops->print_mention = print_mention_catch_fork;
15569 ops->print_recreate = print_recreate_catch_fork;
15570
15571 /* Vfork catchpoints. */
15572 ops = &catch_vfork_breakpoint_ops;
15573 *ops = base_breakpoint_ops;
15574 ops->insert_location = insert_catch_vfork;
15575 ops->remove_location = remove_catch_vfork;
15576 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15577 ops->print_it = print_it_catch_vfork;
15578 ops->print_one = print_one_catch_vfork;
15579 ops->print_mention = print_mention_catch_vfork;
15580 ops->print_recreate = print_recreate_catch_vfork;
15581
15582 /* Exec catchpoints. */
15583 ops = &catch_exec_breakpoint_ops;
15584 *ops = base_breakpoint_ops;
15585 ops->dtor = dtor_catch_exec;
15586 ops->insert_location = insert_catch_exec;
15587 ops->remove_location = remove_catch_exec;
15588 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15589 ops->print_it = print_it_catch_exec;
15590 ops->print_one = print_one_catch_exec;
15591 ops->print_mention = print_mention_catch_exec;
15592 ops->print_recreate = print_recreate_catch_exec;
15593
15594 /* Syscall catchpoints. */
15595 ops = &catch_syscall_breakpoint_ops;
15596 *ops = base_breakpoint_ops;
15597 ops->dtor = dtor_catch_syscall;
15598 ops->insert_location = insert_catch_syscall;
15599 ops->remove_location = remove_catch_syscall;
15600 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
15601 ops->print_it = print_it_catch_syscall;
15602 ops->print_one = print_one_catch_syscall;
15603 ops->print_mention = print_mention_catch_syscall;
15604 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
15605
15606 /* Solib-related catchpoints. */
15607 ops = &catch_solib_breakpoint_ops;
15608 *ops = base_breakpoint_ops;
15609 ops->dtor = dtor_catch_solib;
15610 ops->insert_location = insert_catch_solib;
15611 ops->remove_location = remove_catch_solib;
15612 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15613 ops->check_status = check_status_catch_solib;
15614 ops->print_it = print_it_catch_solib;
15615 ops->print_one = print_one_catch_solib;
15616 ops->print_mention = print_mention_catch_solib;
15617 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15618
15619 ops = &dprintf_breakpoint_ops;
15620 *ops = bkpt_base_breakpoint_ops;
15621 ops->re_set = bkpt_re_set;
15622 ops->resources_needed = bkpt_resources_needed;
15623 ops->print_it = bkpt_print_it;
15624 ops->print_mention = bkpt_print_mention;
15625 ops->print_recreate = bkpt_print_recreate;
2060206e
PA
15626}
15627
c906108c 15628void
fba45db2 15629_initialize_breakpoint (void)
c906108c
SS
15630{
15631 struct cmd_list_element *c;
15632
2060206e
PA
15633 initialize_breakpoint_ops ();
15634
84acb35a 15635 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 15636 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 15637 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15638
55aa24fb
SDJ
15639 breakpoint_objfile_key
15640 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15641
fa3064dd
YQ
15642 catch_syscall_inferior_data
15643 = register_inferior_data_with_cleanup (catch_syscall_inferior_data_cleanup);
15644
c906108c
SS
15645 breakpoint_chain = 0;
15646 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15647 before a breakpoint is set. */
15648 breakpoint_count = 0;
15649
1042e4c0
SS
15650 tracepoint_count = 0;
15651
1bedd215
AC
15652 add_com ("ignore", class_breakpoint, ignore_command, _("\
15653Set ignore-count of breakpoint number N to COUNT.\n\
15654Usage is `ignore N COUNT'."));
c906108c 15655 if (xdb_commands)
c5aa993b 15656 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 15657
1bedd215
AC
15658 add_com ("commands", class_breakpoint, commands_command, _("\
15659Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
15660Give breakpoint number as argument after \"commands\".\n\
15661With no argument, the targeted breakpoint is the last one set.\n\
15662The commands themselves follow starting on the next line.\n\
15663Type a line containing \"end\" to indicate the end of them.\n\
15664Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15665then no output is printed when it is hit, except what the commands print."));
c906108c 15666
d55637df 15667 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15668Specify breakpoint number N to break only if COND is true.\n\
c906108c 15669Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15670expression to be evaluated whenever breakpoint N is reached."));
d55637df 15671 set_cmd_completer (c, condition_completer);
c906108c 15672
1bedd215 15673 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15674Set a temporary breakpoint.\n\
c906108c
SS
15675Like \"break\" except the breakpoint is only temporary,\n\
15676so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15677by using \"enable delete\" on the breakpoint number.\n\
15678\n"
15679BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15680 set_cmd_completer (c, location_completer);
c94fdfd0 15681
1bedd215 15682 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15683Set a hardware assisted breakpoint.\n\
c906108c 15684Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15685some target hardware may not have this support.\n\
15686\n"
15687BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15688 set_cmd_completer (c, location_completer);
c906108c 15689
1bedd215 15690 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15691Set a temporary hardware assisted breakpoint.\n\
c906108c 15692Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15693so it will be deleted when hit.\n\
15694\n"
15695BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15696 set_cmd_completer (c, location_completer);
c906108c 15697
1bedd215
AC
15698 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15699Enable some breakpoints.\n\
c906108c
SS
15700Give breakpoint numbers (separated by spaces) as arguments.\n\
15701With no subcommand, breakpoints are enabled until you command otherwise.\n\
15702This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15703With a subcommand you can enable temporarily."),
c906108c
SS
15704 &enablelist, "enable ", 1, &cmdlist);
15705 if (xdb_commands)
1bedd215
AC
15706 add_com ("ab", class_breakpoint, enable_command, _("\
15707Enable some breakpoints.\n\
c906108c
SS
15708Give breakpoint numbers (separated by spaces) as arguments.\n\
15709With no subcommand, breakpoints are enabled until you command otherwise.\n\
15710This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15711With a subcommand you can enable temporarily."));
c906108c
SS
15712
15713 add_com_alias ("en", "enable", class_breakpoint, 1);
15714
84951ab5 15715 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15716Enable some breakpoints.\n\
c906108c
SS
15717Give breakpoint numbers (separated by spaces) as arguments.\n\
15718This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15719May be abbreviated to simply \"enable\".\n"),
c5aa993b 15720 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15721
1a966eab
AC
15722 add_cmd ("once", no_class, enable_once_command, _("\
15723Enable breakpoints for one hit. Give breakpoint numbers.\n\
15724If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15725 &enablebreaklist);
15726
1a966eab
AC
15727 add_cmd ("delete", no_class, enable_delete_command, _("\
15728Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15729If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15730 &enablebreaklist);
15731
816338b5
SS
15732 add_cmd ("count", no_class, enable_count_command, _("\
15733Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15734If a breakpoint is hit while enabled in this fashion,\n\
15735the count is decremented; when it reaches zero, the breakpoint is disabled."),
15736 &enablebreaklist);
15737
1a966eab
AC
15738 add_cmd ("delete", no_class, enable_delete_command, _("\
15739Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15740If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15741 &enablelist);
15742
1a966eab
AC
15743 add_cmd ("once", no_class, enable_once_command, _("\
15744Enable breakpoints for one hit. Give breakpoint numbers.\n\
15745If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15746 &enablelist);
15747
15748 add_cmd ("count", no_class, enable_count_command, _("\
15749Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15750If a breakpoint is hit while enabled in this fashion,\n\
15751the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15752 &enablelist);
15753
1bedd215
AC
15754 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15755Disable some breakpoints.\n\
c906108c
SS
15756Arguments are breakpoint numbers with spaces in between.\n\
15757To disable all breakpoints, give no argument.\n\
64b9b334 15758A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15759 &disablelist, "disable ", 1, &cmdlist);
15760 add_com_alias ("dis", "disable", class_breakpoint, 1);
15761 add_com_alias ("disa", "disable", class_breakpoint, 1);
15762 if (xdb_commands)
1bedd215
AC
15763 add_com ("sb", class_breakpoint, disable_command, _("\
15764Disable some breakpoints.\n\
c906108c
SS
15765Arguments are breakpoint numbers with spaces in between.\n\
15766To disable all breakpoints, give no argument.\n\
64b9b334 15767A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 15768
1a966eab
AC
15769 add_cmd ("breakpoints", class_alias, disable_command, _("\
15770Disable some breakpoints.\n\
c906108c
SS
15771Arguments are breakpoint numbers with spaces in between.\n\
15772To disable all breakpoints, give no argument.\n\
64b9b334 15773A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15774This command may be abbreviated \"disable\"."),
c906108c
SS
15775 &disablelist);
15776
1bedd215
AC
15777 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15778Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15779Arguments are breakpoint numbers with spaces in between.\n\
15780To delete all breakpoints, give no argument.\n\
15781\n\
15782Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15783The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15784 &deletelist, "delete ", 1, &cmdlist);
15785 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15786 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15787 if (xdb_commands)
1bedd215
AC
15788 add_com ("db", class_breakpoint, delete_command, _("\
15789Delete some breakpoints.\n\
c906108c 15790Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 15791To delete all breakpoints, give no argument.\n"));
c906108c 15792
1a966eab
AC
15793 add_cmd ("breakpoints", class_alias, delete_command, _("\
15794Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15795Arguments are breakpoint numbers with spaces in between.\n\
15796To delete all breakpoints, give no argument.\n\
1a966eab 15797This command may be abbreviated \"delete\"."),
c906108c
SS
15798 &deletelist);
15799
1bedd215
AC
15800 add_com ("clear", class_breakpoint, clear_command, _("\
15801Clear breakpoint at specified line or function.\n\
c906108c
SS
15802Argument may be line number, function name, or \"*\" and an address.\n\
15803If line number is specified, all breakpoints in that line are cleared.\n\
15804If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
15805If an address is specified, breakpoints at that address are cleared.\n\
15806\n\
15807With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
15808is executing in.\n\
15809\n\
1bedd215 15810See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15811 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15812
1bedd215 15813 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
15814Set breakpoint at specified line or function.\n"
15815BREAK_ARGS_HELP ("break")));
5ba2abeb 15816 set_cmd_completer (c, location_completer);
c94fdfd0 15817
c906108c
SS
15818 add_com_alias ("b", "break", class_run, 1);
15819 add_com_alias ("br", "break", class_run, 1);
15820 add_com_alias ("bre", "break", class_run, 1);
15821 add_com_alias ("brea", "break", class_run, 1);
15822
7681d515
PM
15823 if (xdb_commands)
15824 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
15825
15826 if (dbx_commands)
15827 {
1bedd215
AC
15828 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15829Break in function/address or break at a line in the current file."),
c5aa993b
JM
15830 &stoplist, "stop ", 1, &cmdlist);
15831 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15832 _("Break in function or address."), &stoplist);
c5aa993b 15833 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15834 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
15835 add_com ("status", class_info, breakpoints_info, _("\
15836Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15837The \"Type\" column indicates one of:\n\
15838\tbreakpoint - normal breakpoint\n\
15839\twatchpoint - watchpoint\n\
15840The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15841the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15842breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15843address and file/line number respectively.\n\
15844\n\
15845Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15846are set to the address of the last breakpoint listed unless the command\n\
15847is prefixed with \"server \".\n\n\
c906108c 15848Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15849breakpoint set."));
c906108c
SS
15850 }
15851
1bedd215 15852 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 15853Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15854The \"Type\" column indicates one of:\n\
15855\tbreakpoint - normal breakpoint\n\
15856\twatchpoint - watchpoint\n\
15857The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15858the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15859breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15860address and file/line number respectively.\n\
15861\n\
15862Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15863are set to the address of the last breakpoint listed unless the command\n\
15864is prefixed with \"server \".\n\n\
c906108c 15865Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15866breakpoint set."));
c906108c 15867
6b04bdb7
MS
15868 add_info_alias ("b", "breakpoints", 1);
15869
c906108c 15870 if (xdb_commands)
1bedd215
AC
15871 add_com ("lb", class_breakpoint, breakpoints_info, _("\
15872Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15873The \"Type\" column indicates one of:\n\
15874\tbreakpoint - normal breakpoint\n\
15875\twatchpoint - watchpoint\n\
15876The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15877the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15878breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15879address and file/line number respectively.\n\
15880\n\
15881Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15882are set to the address of the last breakpoint listed unless the command\n\
15883is prefixed with \"server \".\n\n\
c906108c 15884Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15885breakpoint set."));
c906108c 15886
1a966eab
AC
15887 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15888Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15889The \"Type\" column indicates one of:\n\
15890\tbreakpoint - normal breakpoint\n\
15891\twatchpoint - watchpoint\n\
15892\tlongjmp - internal breakpoint used to step through longjmp()\n\
15893\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15894\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15895\tfinish - internal breakpoint used by the \"finish\" command\n\
15896The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15897the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15898breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15899address and file/line number respectively.\n\
15900\n\
15901Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15902are set to the address of the last breakpoint listed unless the command\n\
15903is prefixed with \"server \".\n\n\
c906108c 15904Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15905breakpoint set."),
c906108c
SS
15906 &maintenanceinfolist);
15907
44feb3ce
TT
15908 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15909Set catchpoints to catch events."),
15910 &catch_cmdlist, "catch ",
15911 0/*allow-unknown*/, &cmdlist);
15912
15913 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15914Set temporary catchpoints to catch events."),
15915 &tcatch_cmdlist, "tcatch ",
15916 0/*allow-unknown*/, &cmdlist);
15917
15918 /* Add catch and tcatch sub-commands. */
15919 add_catch_command ("catch", _("\
88e7d25d 15920Catch an exception, when caught."),
44feb3ce 15921 catch_catch_command,
a96d9b2e 15922 NULL,
44feb3ce
TT
15923 CATCH_PERMANENT,
15924 CATCH_TEMPORARY);
15925 add_catch_command ("throw", _("\
88e7d25d 15926Catch an exception, when thrown."),
44feb3ce 15927 catch_throw_command,
a96d9b2e 15928 NULL,
44feb3ce
TT
15929 CATCH_PERMANENT,
15930 CATCH_TEMPORARY);
15931 add_catch_command ("fork", _("Catch calls to fork."),
15932 catch_fork_command_1,
a96d9b2e 15933 NULL,
44feb3ce
TT
15934 (void *) (uintptr_t) catch_fork_permanent,
15935 (void *) (uintptr_t) catch_fork_temporary);
15936 add_catch_command ("vfork", _("Catch calls to vfork."),
15937 catch_fork_command_1,
a96d9b2e 15938 NULL,
44feb3ce
TT
15939 (void *) (uintptr_t) catch_vfork_permanent,
15940 (void *) (uintptr_t) catch_vfork_temporary);
15941 add_catch_command ("exec", _("Catch calls to exec."),
15942 catch_exec_command_1,
a96d9b2e
SDJ
15943 NULL,
15944 CATCH_PERMANENT,
15945 CATCH_TEMPORARY);
edcc5120
TT
15946 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15947Usage: catch load [REGEX]\n\
15948If REGEX is given, only stop for libraries matching the regular expression."),
15949 catch_load_command_1,
15950 NULL,
15951 CATCH_PERMANENT,
15952 CATCH_TEMPORARY);
15953 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15954Usage: catch unload [REGEX]\n\
15955If REGEX is given, only stop for libraries matching the regular expression."),
15956 catch_unload_command_1,
15957 NULL,
15958 CATCH_PERMANENT,
15959 CATCH_TEMPORARY);
a96d9b2e
SDJ
15960 add_catch_command ("syscall", _("\
15961Catch system calls by their names and/or numbers.\n\
15962Arguments say which system calls to catch. If no arguments\n\
15963are given, every system call will be caught.\n\
15964Arguments, if given, should be one or more system call names\n\
15965(if your system supports that), or system call numbers."),
15966 catch_syscall_command_1,
15967 catch_syscall_completer,
44feb3ce
TT
15968 CATCH_PERMANENT,
15969 CATCH_TEMPORARY);
c5aa993b 15970
1bedd215
AC
15971 c = add_com ("watch", class_breakpoint, watch_command, _("\
15972Set a watchpoint for an expression.\n\
06a64a0b 15973Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15974A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15975an expression changes.\n\
15976If -l or -location is given, this evaluates EXPRESSION and watches\n\
15977the memory to which it refers."));
65d12d83 15978 set_cmd_completer (c, expression_completer);
c906108c 15979
1bedd215
AC
15980 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15981Set a read watchpoint for an expression.\n\
06a64a0b 15982Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15983A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15984an expression is read.\n\
15985If -l or -location is given, this evaluates EXPRESSION and watches\n\
15986the memory to which it refers."));
65d12d83 15987 set_cmd_completer (c, expression_completer);
c906108c 15988
1bedd215
AC
15989 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15990Set a watchpoint for an expression.\n\
06a64a0b 15991Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15992A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15993an expression is either read or written.\n\
15994If -l or -location is given, this evaluates EXPRESSION and watches\n\
15995the memory to which it refers."));
65d12d83 15996 set_cmd_completer (c, expression_completer);
c906108c 15997
d77f58be 15998 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 15999Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16000
920d2a44
AC
16001 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16002 respond to changes - contrary to the description. */
85c07804
AC
16003 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16004 &can_use_hw_watchpoints, _("\
16005Set debugger's willingness to use watchpoint hardware."), _("\
16006Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16007If zero, gdb will not use hardware for new watchpoints, even if\n\
16008such is available. (However, any hardware watchpoints that were\n\
16009created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16010hardware.)"),
16011 NULL,
920d2a44 16012 show_can_use_hw_watchpoints,
85c07804 16013 &setlist, &showlist);
c906108c
SS
16014
16015 can_use_hw_watchpoints = 1;
fa8d40ab 16016
1042e4c0
SS
16017 /* Tracepoint manipulation commands. */
16018
16019 c = add_com ("trace", class_breakpoint, trace_command, _("\
16020Set a tracepoint at specified line or function.\n\
16021\n"
16022BREAK_ARGS_HELP ("trace") "\n\
16023Do \"help tracepoints\" for info on other tracepoint commands."));
16024 set_cmd_completer (c, location_completer);
16025
16026 add_com_alias ("tp", "trace", class_alias, 0);
16027 add_com_alias ("tr", "trace", class_alias, 1);
16028 add_com_alias ("tra", "trace", class_alias, 1);
16029 add_com_alias ("trac", "trace", class_alias, 1);
16030
7a697b8d
SS
16031 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16032Set a fast tracepoint at specified line or function.\n\
16033\n"
16034BREAK_ARGS_HELP ("ftrace") "\n\
16035Do \"help tracepoints\" for info on other tracepoint commands."));
16036 set_cmd_completer (c, location_completer);
16037
0fb4aa4b
PA
16038 c = add_com ("strace", class_breakpoint, strace_command, _("\
16039Set a static tracepoint at specified line, function or marker.\n\
16040\n\
16041strace [LOCATION] [if CONDITION]\n\
16042LOCATION may be a line number, function name, \"*\" and an address,\n\
16043or -m MARKER_ID.\n\
16044If a line number is specified, probe the marker at start of code\n\
16045for that line. If a function is specified, probe the marker at start\n\
16046of code for that function. If an address is specified, probe the marker\n\
16047at that exact address. If a marker id is specified, probe the marker\n\
16048with that name. With no LOCATION, uses current execution address of\n\
16049the selected stack frame.\n\
16050Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16051This collects arbitrary user data passed in the probe point call to the\n\
16052tracing library. You can inspect it when analyzing the trace buffer,\n\
16053by printing the $_sdata variable like any other convenience variable.\n\
16054\n\
16055CONDITION is a boolean expression.\n\
16056\n\
d41c0fc8
PA
16057Multiple tracepoints at one place are permitted, and useful if their\n\
16058conditions are different.\n\
0fb4aa4b
PA
16059\n\
16060Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16061Do \"help tracepoints\" for info on other tracepoint commands."));
16062 set_cmd_completer (c, location_completer);
16063
1042e4c0 16064 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16065Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16066Convenience variable \"$tpnum\" contains the number of the\n\
16067last tracepoint set."));
16068
16069 add_info_alias ("tp", "tracepoints", 1);
16070
16071 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16072Delete specified tracepoints.\n\
16073Arguments are tracepoint numbers, separated by spaces.\n\
16074No argument means delete all tracepoints."),
16075 &deletelist);
16076
16077 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16078Disable specified tracepoints.\n\
16079Arguments are tracepoint numbers, separated by spaces.\n\
16080No argument means disable all tracepoints."),
16081 &disablelist);
16082 deprecate_cmd (c, "disable");
16083
16084 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16085Enable specified tracepoints.\n\
16086Arguments are tracepoint numbers, separated by spaces.\n\
16087No argument means enable all tracepoints."),
16088 &enablelist);
16089 deprecate_cmd (c, "enable");
16090
16091 add_com ("passcount", class_trace, trace_pass_command, _("\
16092Set the passcount for a tracepoint.\n\
16093The trace will end when the tracepoint has been passed 'count' times.\n\
16094Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16095if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16096
6149aea9
PA
16097 add_prefix_cmd ("save", class_breakpoint, save_command,
16098 _("Save breakpoint definitions as a script."),
16099 &save_cmdlist, "save ",
16100 0/*allow-unknown*/, &cmdlist);
16101
16102 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16103Save current breakpoint definitions as a script.\n\
cce7e648 16104This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16105catchpoints, tracepoints). Use the 'source' command in another debug\n\
16106session to restore them."),
16107 &save_cmdlist);
16108 set_cmd_completer (c, filename_completer);
16109
16110 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16111Save current tracepoint definitions as a script.\n\
6149aea9
PA
16112Use the 'source' command in another debug session to restore them."),
16113 &save_cmdlist);
1042e4c0
SS
16114 set_cmd_completer (c, filename_completer);
16115
6149aea9
PA
16116 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16117 deprecate_cmd (c, "save tracepoints");
16118
1bedd215 16119 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16120Breakpoint specific settings\n\
16121Configure various breakpoint-specific variables such as\n\
1bedd215 16122pending breakpoint behavior"),
fa8d40ab
JJ
16123 &breakpoint_set_cmdlist, "set breakpoint ",
16124 0/*allow-unknown*/, &setlist);
1bedd215 16125 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16126Breakpoint specific settings\n\
16127Configure various breakpoint-specific variables such as\n\
1bedd215 16128pending breakpoint behavior"),
fa8d40ab
JJ
16129 &breakpoint_show_cmdlist, "show breakpoint ",
16130 0/*allow-unknown*/, &showlist);
16131
7915a72c
AC
16132 add_setshow_auto_boolean_cmd ("pending", no_class,
16133 &pending_break_support, _("\
16134Set debugger's behavior regarding pending breakpoints."), _("\
16135Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16136If on, an unrecognized breakpoint location will cause gdb to create a\n\
16137pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16138an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16139user-query to see if a pending breakpoint should be created."),
2c5b56ce 16140 NULL,
920d2a44 16141 show_pending_break_support,
6e1d7d6c
AC
16142 &breakpoint_set_cmdlist,
16143 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16144
16145 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16146
16147 add_setshow_boolean_cmd ("auto-hw", no_class,
16148 &automatic_hardware_breakpoints, _("\
16149Set automatic usage of hardware breakpoints."), _("\
16150Show automatic usage of hardware breakpoints."), _("\
16151If set, the debugger will automatically use hardware breakpoints for\n\
16152breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16153a warning will be emitted for such breakpoints."),
16154 NULL,
16155 show_automatic_hardware_breakpoints,
16156 &breakpoint_set_cmdlist,
16157 &breakpoint_show_cmdlist);
74960c60 16158
72d0e2c5
YQ
16159 add_setshow_auto_boolean_cmd ("always-inserted", class_support,
16160 &always_inserted_mode, _("\
74960c60
VP
16161Set mode for inserting breakpoints."), _("\
16162Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
16163When this mode is off, breakpoints are inserted in inferior when it is\n\
16164resumed, and removed when execution stops. When this mode is on,\n\
16165breakpoints are inserted immediately and removed only when the user\n\
16166deletes the breakpoint. When this mode is auto (which is the default),\n\
16167the behaviour depends on the non-stop setting (see help set non-stop).\n\
16168In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16169behaves as if always-inserted mode is on; if gdb is controlling the\n\
16170inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
72d0e2c5
YQ
16171 NULL,
16172 &show_always_inserted_mode,
16173 &breakpoint_set_cmdlist,
16174 &breakpoint_show_cmdlist);
f1310107 16175
b775012e
LM
16176 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16177 condition_evaluation_enums,
16178 &condition_evaluation_mode_1, _("\
16179Set mode of breakpoint condition evaluation."), _("\
16180Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16181When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16182evaluated on the host's side by GDB. When it is set to \"target\",\n\
16183breakpoint conditions will be downloaded to the target (if the target\n\
16184supports such feature) and conditions will be evaluated on the target's side.\n\
16185If this is set to \"auto\" (default), this will be automatically set to\n\
16186\"target\" if it supports condition evaluation, otherwise it will\n\
16187be set to \"gdb\""),
16188 &set_condition_evaluation_mode,
16189 &show_condition_evaluation_mode,
16190 &breakpoint_set_cmdlist,
16191 &breakpoint_show_cmdlist);
16192
f1310107
TJB
16193 add_com ("break-range", class_breakpoint, break_range_command, _("\
16194Set a breakpoint for an address range.\n\
16195break-range START-LOCATION, END-LOCATION\n\
16196where START-LOCATION and END-LOCATION can be one of the following:\n\
16197 LINENUM, for that line in the current file,\n\
16198 FILE:LINENUM, for that line in that file,\n\
16199 +OFFSET, for that number of lines after the current line\n\
16200 or the start of the range\n\
16201 FUNCTION, for the first line in that function,\n\
16202 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16203 *ADDRESS, for the instruction at that address.\n\
16204\n\
16205The breakpoint will stop execution of the inferior whenever it executes\n\
16206an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16207range (including START-LOCATION and END-LOCATION)."));
16208
e7e0cddf
SS
16209 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16210Set a dynamic printf at specified line or function.\n\
16211dprintf location,format string,arg1,arg2,...\n\
16212location may be a line number, function name, or \"*\" and an address.\n\
16213If a line number is specified, break at start of code for that line.\n\
16214If a function is specified, break at start of code for that function.\n\
16215"));
16216 set_cmd_completer (c, location_completer);
16217
16218 add_setshow_enum_cmd ("dprintf-style", class_support,
16219 dprintf_style_enums, &dprintf_style, _("\
16220Set the style of usage for dynamic printf."), _("\
16221Show the style of usage for dynamic printf."), _("\
16222This setting chooses how GDB will do a dynamic printf.\n\
16223If the value is \"gdb\", then the printing is done by GDB to its own\n\
16224console, as with the \"printf\" command.\n\
16225If the value is \"call\", the print is done by calling a function in your\n\
16226program; by default printf(), but you can choose a different function or\n\
16227output stream by setting dprintf-function and dprintf-channel."),
16228 update_dprintf_commands, NULL,
16229 &setlist, &showlist);
16230
16231 dprintf_function = xstrdup ("printf");
16232 add_setshow_string_cmd ("dprintf-function", class_support,
16233 &dprintf_function, _("\
16234Set the function to use for dynamic printf"), _("\
16235Show the function to use for dynamic printf"), NULL,
16236 update_dprintf_commands, NULL,
16237 &setlist, &showlist);
16238
16239 dprintf_channel = xstrdup ("");
16240 add_setshow_string_cmd ("dprintf-channel", class_support,
16241 &dprintf_channel, _("\
16242Set the channel to use for dynamic printf"), _("\
16243Show the channel to use for dynamic printf"), NULL,
16244 update_dprintf_commands, NULL,
16245 &setlist, &showlist);
16246
765dc015 16247 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16248
16249 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 16250}
This page took 2.341128 seconds and 4 git commands to generate.