Put single-step breakpoints on the bp_location chain
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
ecd75fc8 3 Copyright (C) 1986-2014 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
45741a9c 35#include "infrun.h"
c906108c
SS
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
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"
fe898f56 50#include "block.h"
a77053c2 51#include "solib.h"
84acb35a
JJ
52#include "solist.h"
53#include "observer.h"
765dc015 54#include "memattr.h"
f7f9143b 55#include "ada-lang.h"
d1aa2f50 56#include "top.h"
79a45b7d 57#include "valprint.h"
4efc6507 58#include "jit.h"
a96d9b2e 59#include "xml-syscall.h"
65d79d4b 60#include "parser-defs.h"
55aa24fb
SDJ
61#include "gdb_regex.h"
62#include "probe.h"
e9cafbcc 63#include "cli/cli-utils.h"
be34f849 64#include "continuations.h"
1bfeeb0f
JL
65#include "stack.h"
66#include "skip.h"
b775012e 67#include "ax-gdb.h"
e2e4d78b 68#include "dummy-frame.h"
c906108c 69
d3ce09f5
SS
70#include "format.h"
71
1042e4c0
SS
72/* readline include files */
73#include "readline/readline.h"
74#include "readline/history.h"
75
76/* readline defines this. */
77#undef savestring
78
034dad6f 79#include "mi/mi-common.h"
6dddc817 80#include "extension.h"
104c1213 81
e7e8980f
YQ
82/* Enums for exception-handling support. */
83enum exception_event_kind
84{
85 EX_EVENT_THROW,
591f19e8 86 EX_EVENT_RETHROW,
e7e8980f
YQ
87 EX_EVENT_CATCH
88};
89
4a64f543 90/* Prototypes for local functions. */
c906108c 91
a14ed312 92static void enable_delete_command (char *, int);
c906108c 93
a14ed312 94static void enable_once_command (char *, int);
c906108c 95
816338b5
SS
96static void enable_count_command (char *, int);
97
a14ed312 98static void disable_command (char *, int);
c906108c 99
a14ed312 100static void enable_command (char *, int);
c906108c 101
95a42b64
TT
102static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
103 void *),
104 void *);
c906108c 105
a14ed312 106static void ignore_command (char *, int);
c906108c 107
4efb68b1 108static int breakpoint_re_set_one (void *);
c906108c 109
348d480f
PA
110static void breakpoint_re_set_default (struct breakpoint *);
111
983af33b
SDJ
112static void create_sals_from_address_default (char **,
113 struct linespec_result *,
114 enum bptype, char *,
115 char **);
116
117static void create_breakpoints_sal_default (struct gdbarch *,
118 struct linespec_result *,
e7e0cddf 119 char *, char *, enum bptype,
983af33b
SDJ
120 enum bpdisp, int, int,
121 int,
122 const struct breakpoint_ops *,
44f238bb 123 int, int, int, unsigned);
983af33b
SDJ
124
125static void decode_linespec_default (struct breakpoint *, char **,
126 struct symtabs_and_lines *);
127
a14ed312 128static void clear_command (char *, int);
c906108c 129
a14ed312 130static void catch_command (char *, int);
c906108c 131
a9634178 132static int can_use_hardware_watchpoint (struct value *);
c906108c 133
98deb0da 134static void break_command_1 (char *, int, int);
c906108c 135
a14ed312 136static void mention (struct breakpoint *);
c906108c 137
348d480f
PA
138static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
139 enum bptype,
c0a91b2b 140 const struct breakpoint_ops *);
3742cc8b
YQ
141static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
142 const struct symtab_and_line *);
143
4a64f543
MS
144/* This function is used in gdbtk sources and thus can not be made
145 static. */
63c252f8 146struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 147 struct symtab_and_line,
c0a91b2b
TT
148 enum bptype,
149 const struct breakpoint_ops *);
c906108c 150
06edf0c0
PA
151static struct breakpoint *
152 momentary_breakpoint_from_master (struct breakpoint *orig,
153 enum bptype type,
a1aa2221
LM
154 const struct breakpoint_ops *ops,
155 int loc_enabled);
06edf0c0 156
76897487
KB
157static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
158
a6d9a66e
UW
159static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
160 CORE_ADDR bpaddr,
88f7da05 161 enum bptype bptype);
76897487 162
6c95b8df
PA
163static void describe_other_breakpoints (struct gdbarch *,
164 struct program_space *, CORE_ADDR,
5af949e3 165 struct obj_section *, int);
c906108c 166
85d721b8
PA
167static int watchpoint_locations_match (struct bp_location *loc1,
168 struct bp_location *loc2);
169
f1310107
TJB
170static int breakpoint_location_address_match (struct bp_location *bl,
171 struct address_space *aspace,
172 CORE_ADDR addr);
173
a14ed312 174static void breakpoints_info (char *, int);
c906108c 175
d77f58be
SS
176static void watchpoints_info (char *, int);
177
e5a67952
MS
178static int breakpoint_1 (char *, int,
179 int (*) (const struct breakpoint *));
c906108c 180
4efb68b1 181static int breakpoint_cond_eval (void *);
c906108c 182
4efb68b1 183static void cleanup_executing_breakpoints (void *);
c906108c 184
a14ed312 185static void commands_command (char *, int);
c906108c 186
a14ed312 187static void condition_command (char *, int);
c906108c 188
c5aa993b
JM
189typedef enum
190 {
191 mark_inserted,
192 mark_uninserted
193 }
194insertion_state_t;
c906108c 195
0bde7532 196static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 197static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 198
e514a9d6 199static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 200
4efb68b1 201static int watchpoint_check (void *);
c906108c 202
a14ed312 203static void maintenance_info_breakpoints (char *, int);
c906108c 204
a14ed312 205static int hw_breakpoint_used_count (void);
c906108c 206
a1398e0c
PA
207static int hw_watchpoint_use_count (struct breakpoint *);
208
209static int hw_watchpoint_used_count_others (struct breakpoint *except,
210 enum bptype type,
211 int *other_type_used);
c906108c 212
a14ed312 213static void hbreak_command (char *, int);
c906108c 214
a14ed312 215static void thbreak_command (char *, int);
c906108c 216
816338b5
SS
217static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
218 int count);
c906108c 219
a14ed312 220static void stop_command (char *arg, int from_tty);
7a292a7a 221
a14ed312 222static void stopin_command (char *arg, int from_tty);
7a292a7a 223
a14ed312 224static void stopat_command (char *arg, int from_tty);
7a292a7a 225
a14ed312 226static void tcatch_command (char *arg, int from_tty);
7a292a7a 227
fe3f5fa8 228static void free_bp_location (struct bp_location *loc);
f431efe5
PA
229static void incref_bp_location (struct bp_location *loc);
230static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 231
39d61571 232static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 233
44702360
PA
234/* update_global_location_list's modes of operation wrt to whether to
235 insert locations now. */
236enum ugll_insert_mode
237{
238 /* Don't insert any breakpoint locations into the inferior, only
239 remove already-inserted locations that no longer should be
240 inserted. Functions that delete a breakpoint or breakpoints
241 should specify this mode, so that deleting a breakpoint doesn't
242 have the side effect of inserting the locations of other
243 breakpoints that are marked not-inserted, but should_be_inserted
244 returns true on them.
245
246 This behavior is useful is situations close to tear-down -- e.g.,
247 after an exec, while the target still has execution, but
248 breakpoint shadows of the previous executable image should *NOT*
249 be restored to the new image; or before detaching, where the
250 target still has execution and wants to delete breakpoints from
251 GDB's lists, and all breakpoints had already been removed from
252 the inferior. */
253 UGLL_DONT_INSERT,
254
a25a5a45
PA
255 /* May insert breakpoints iff breakpoints_should_be_inserted_now
256 claims breakpoints should be inserted now. */
04086b45
PA
257 UGLL_MAY_INSERT,
258
a25a5a45
PA
259 /* Insert locations now, irrespective of
260 breakpoints_should_be_inserted_now. E.g., say all threads are
261 stopped right now, and the user did "continue". We need to
262 insert breakpoints _before_ resuming the target, but
263 UGLL_MAY_INSERT wouldn't insert them, because
264 breakpoints_should_be_inserted_now returns false at that point,
265 as no thread is running yet. */
04086b45 266 UGLL_INSERT
44702360
PA
267};
268
269static void update_global_location_list (enum ugll_insert_mode);
a5606eee 270
44702360 271static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 272
d77f58be 273static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
274
275static void insert_breakpoint_locations (void);
a5606eee 276
a96d9b2e
SDJ
277static int syscall_catchpoint_p (struct breakpoint *b);
278
1042e4c0
SS
279static void tracepoints_info (char *, int);
280
281static void delete_trace_command (char *, int);
282
283static void enable_trace_command (char *, int);
284
285static void disable_trace_command (char *, int);
286
287static void trace_pass_command (char *, int);
288
558a9d82
YQ
289static void set_tracepoint_count (int num);
290
9c06b0b4
TJB
291static int is_masked_watchpoint (const struct breakpoint *b);
292
b775012e
LM
293static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
294
983af33b
SDJ
295/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
296 otherwise. */
297
298static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 299
2060206e
PA
300/* The abstract base class all breakpoint_ops structures inherit
301 from. */
ab04a2af 302struct breakpoint_ops base_breakpoint_ops;
2060206e
PA
303
304/* The breakpoint_ops structure to be inherited by all breakpoint_ops
305 that are implemented on top of software or hardware breakpoints
306 (user breakpoints, internal and momentary breakpoints, etc.). */
307static struct breakpoint_ops bkpt_base_breakpoint_ops;
308
309/* Internal breakpoints class type. */
06edf0c0 310static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
311
312/* Momentary breakpoints class type. */
06edf0c0
PA
313static struct breakpoint_ops momentary_breakpoint_ops;
314
e2e4d78b
JK
315/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
316static struct breakpoint_ops longjmp_breakpoint_ops;
317
2060206e
PA
318/* The breakpoint_ops structure to be used in regular user created
319 breakpoints. */
320struct breakpoint_ops bkpt_breakpoint_ops;
321
55aa24fb
SDJ
322/* Breakpoints set on probes. */
323static struct breakpoint_ops bkpt_probe_breakpoint_ops;
324
e7e0cddf 325/* Dynamic printf class type. */
c5867ab6 326struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 327
9d497a19
PA
328/* One (or perhaps two) breakpoints used for software single
329 stepping. */
330
7c16b83e 331static struct breakpoint *single_step_breakpoints;
9d497a19 332
d3ce09f5
SS
333/* The style in which to perform a dynamic printf. This is a user
334 option because different output options have different tradeoffs;
335 if GDB does the printing, there is better error handling if there
336 is a problem with any of the arguments, but using an inferior
337 function lets you have special-purpose printers and sending of
338 output to the same place as compiled-in print functions. */
339
340static const char dprintf_style_gdb[] = "gdb";
341static const char dprintf_style_call[] = "call";
342static const char dprintf_style_agent[] = "agent";
343static const char *const dprintf_style_enums[] = {
344 dprintf_style_gdb,
345 dprintf_style_call,
346 dprintf_style_agent,
347 NULL
348};
349static const char *dprintf_style = dprintf_style_gdb;
350
351/* The function to use for dynamic printf if the preferred style is to
352 call into the inferior. The value is simply a string that is
353 copied into the command, so it can be anything that GDB can
354 evaluate to a callable address, not necessarily a function name. */
355
356static char *dprintf_function = "";
357
358/* The channel to use for dynamic printf if the preferred style is to
359 call into the inferior; if a nonempty string, it will be passed to
360 the call as the first argument, with the format string as the
361 second. As with the dprintf function, this can be anything that
362 GDB knows how to evaluate, so in addition to common choices like
363 "stderr", this could be an app-specific expression like
364 "mystreams[curlogger]". */
365
366static char *dprintf_channel = "";
367
368/* True if dprintf commands should continue to operate even if GDB
369 has disconnected. */
370static int disconnected_dprintf = 1;
371
5cea2a26
PA
372/* A reference-counted struct command_line. This lets multiple
373 breakpoints share a single command list. */
374struct counted_command_line
375{
376 /* The reference count. */
377 int refc;
378
379 /* The command list. */
380 struct command_line *commands;
381};
382
383struct command_line *
384breakpoint_commands (struct breakpoint *b)
385{
386 return b->commands ? b->commands->commands : NULL;
387}
3daf8fe5 388
f3b1572e
PA
389/* Flag indicating that a command has proceeded the inferior past the
390 current breakpoint. */
391
392static int breakpoint_proceeded;
393
956a9fb9 394const char *
2cec12e5
AR
395bpdisp_text (enum bpdisp disp)
396{
4a64f543
MS
397 /* NOTE: the following values are a part of MI protocol and
398 represent values of 'disp' field returned when inferior stops at
399 a breakpoint. */
bc043ef3 400 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 401
2cec12e5
AR
402 return bpdisps[(int) disp];
403}
c906108c 404
4a64f543 405/* Prototypes for exported functions. */
c906108c 406/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 407 if such is available. */
c906108c
SS
408static int can_use_hw_watchpoints;
409
920d2a44
AC
410static void
411show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
412 struct cmd_list_element *c,
413 const char *value)
414{
3e43a32a
MS
415 fprintf_filtered (file,
416 _("Debugger's willingness to use "
417 "watchpoint hardware is %s.\n"),
920d2a44
AC
418 value);
419}
420
fa8d40ab
JJ
421/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
422 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 423 for unrecognized breakpoint locations.
fa8d40ab
JJ
424 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
425static enum auto_boolean pending_break_support;
920d2a44
AC
426static void
427show_pending_break_support (struct ui_file *file, int from_tty,
428 struct cmd_list_element *c,
429 const char *value)
430{
3e43a32a
MS
431 fprintf_filtered (file,
432 _("Debugger's behavior regarding "
433 "pending breakpoints is %s.\n"),
920d2a44
AC
434 value);
435}
fa8d40ab 436
765dc015 437/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 438 set with "break" but falling in read-only memory.
765dc015
VP
439 If 0, gdb will warn about such breakpoints, but won't automatically
440 use hardware breakpoints. */
441static int automatic_hardware_breakpoints;
442static void
443show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
444 struct cmd_list_element *c,
445 const char *value)
446{
3e43a32a
MS
447 fprintf_filtered (file,
448 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
449 value);
450}
451
a25a5a45
PA
452/* If on, GDB keeps breakpoints inserted even if the inferior is
453 stopped, and immediately inserts any new breakpoints as soon as
454 they're created. If off (default), GDB keeps breakpoints off of
455 the target as long as possible. That is, it delays inserting
456 breakpoints until the next resume, and removes them again when the
457 target fully stops. This is a bit safer in case GDB crashes while
458 processing user input. */
459static int always_inserted_mode = 0;
72d0e2c5 460
33e5cbd6 461static void
74960c60 462show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 463 struct cmd_list_element *c, const char *value)
74960c60 464{
a25a5a45
PA
465 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
466 value);
74960c60
VP
467}
468
b57bacec
PA
469/* See breakpoint.h. */
470
33e5cbd6 471int
a25a5a45 472breakpoints_should_be_inserted_now (void)
33e5cbd6 473{
a25a5a45
PA
474 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
475 {
476 /* If breakpoints are global, they should be inserted even if no
477 thread under gdb's control is running, or even if there are
478 no threads under GDB's control yet. */
479 return 1;
480 }
481 else if (target_has_execution)
482 {
a25a5a45
PA
483 if (always_inserted_mode)
484 {
485 /* The user wants breakpoints inserted even if all threads
486 are stopped. */
487 return 1;
488 }
489
b57bacec
PA
490 if (threads_are_executing ())
491 return 1;
a25a5a45
PA
492 }
493 return 0;
33e5cbd6 494}
765dc015 495
b775012e
LM
496static const char condition_evaluation_both[] = "host or target";
497
498/* Modes for breakpoint condition evaluation. */
499static const char condition_evaluation_auto[] = "auto";
500static const char condition_evaluation_host[] = "host";
501static const char condition_evaluation_target[] = "target";
502static const char *const condition_evaluation_enums[] = {
503 condition_evaluation_auto,
504 condition_evaluation_host,
505 condition_evaluation_target,
506 NULL
507};
508
509/* Global that holds the current mode for breakpoint condition evaluation. */
510static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
511
512/* Global that we use to display information to the user (gets its value from
513 condition_evaluation_mode_1. */
514static const char *condition_evaluation_mode = condition_evaluation_auto;
515
516/* Translate a condition evaluation mode MODE into either "host"
517 or "target". This is used mostly to translate from "auto" to the
518 real setting that is being used. It returns the translated
519 evaluation mode. */
520
521static const char *
522translate_condition_evaluation_mode (const char *mode)
523{
524 if (mode == condition_evaluation_auto)
525 {
526 if (target_supports_evaluation_of_breakpoint_conditions ())
527 return condition_evaluation_target;
528 else
529 return condition_evaluation_host;
530 }
531 else
532 return mode;
533}
534
535/* Discovers what condition_evaluation_auto translates to. */
536
537static const char *
538breakpoint_condition_evaluation_mode (void)
539{
540 return translate_condition_evaluation_mode (condition_evaluation_mode);
541}
542
543/* Return true if GDB should evaluate breakpoint conditions or false
544 otherwise. */
545
546static int
547gdb_evaluates_breakpoint_condition_p (void)
548{
549 const char *mode = breakpoint_condition_evaluation_mode ();
550
551 return (mode == condition_evaluation_host);
552}
553
a14ed312 554void _initialize_breakpoint (void);
c906108c 555
c906108c
SS
556/* Are we executing breakpoint commands? */
557static int executing_breakpoint_commands;
558
c02f5703
MS
559/* Are overlay event breakpoints enabled? */
560static int overlay_events_enabled;
561
e09342b5
TJB
562/* See description in breakpoint.h. */
563int target_exact_watchpoints = 0;
564
c906108c 565/* Walk the following statement or block through all breakpoints.
e5dd4106 566 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 567 current breakpoint. */
c906108c 568
5c44784c 569#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 570
5c44784c
JM
571#define ALL_BREAKPOINTS_SAFE(B,TMP) \
572 for (B = breakpoint_chain; \
573 B ? (TMP=B->next, 1): 0; \
574 B = TMP)
c906108c 575
4a64f543
MS
576/* Similar iterator for the low-level breakpoints. SAFE variant is
577 not provided so update_global_location_list must not be called
578 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 579
876fa593
JK
580#define ALL_BP_LOCATIONS(B,BP_TMP) \
581 for (BP_TMP = bp_location; \
582 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
583 BP_TMP++)
7cc221ef 584
b775012e
LM
585/* Iterates through locations with address ADDRESS for the currently selected
586 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
587 to where the loop should start from.
588 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
589 appropriate location to start with. */
590
591#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
592 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
593 BP_LOCP_TMP = BP_LOCP_START; \
594 BP_LOCP_START \
595 && (BP_LOCP_TMP < bp_location + bp_location_count \
596 && (*BP_LOCP_TMP)->address == ADDRESS); \
597 BP_LOCP_TMP++)
598
1042e4c0
SS
599/* Iterator for tracepoints only. */
600
601#define ALL_TRACEPOINTS(B) \
602 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 603 if (is_tracepoint (B))
1042e4c0 604
7cc221ef 605/* Chains of all breakpoints defined. */
c906108c
SS
606
607struct breakpoint *breakpoint_chain;
608
876fa593
JK
609/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
610
611static struct bp_location **bp_location;
612
613/* Number of elements of BP_LOCATION. */
614
615static unsigned bp_location_count;
616
4a64f543
MS
617/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
618 ADDRESS for the current elements of BP_LOCATION which get a valid
619 result from bp_location_has_shadow. You can use it for roughly
620 limiting the subrange of BP_LOCATION to scan for shadow bytes for
621 an address you need to read. */
876fa593
JK
622
623static CORE_ADDR bp_location_placed_address_before_address_max;
624
4a64f543
MS
625/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
626 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
627 BP_LOCATION which get a valid result from bp_location_has_shadow.
628 You can use it for roughly limiting the subrange of BP_LOCATION to
629 scan for shadow bytes for an address you need to read. */
876fa593
JK
630
631static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 632
4a64f543
MS
633/* The locations that no longer correspond to any breakpoint, unlinked
634 from bp_location array, but for which a hit may still be reported
635 by a target. */
20874c92
VP
636VEC(bp_location_p) *moribund_locations = NULL;
637
c906108c
SS
638/* Number of last breakpoint made. */
639
95a42b64
TT
640static int breakpoint_count;
641
86b17b60
PA
642/* The value of `breakpoint_count' before the last command that
643 created breakpoints. If the last (break-like) command created more
644 than one breakpoint, then the difference between BREAKPOINT_COUNT
645 and PREV_BREAKPOINT_COUNT is more than one. */
646static int prev_breakpoint_count;
c906108c 647
1042e4c0
SS
648/* Number of last tracepoint made. */
649
95a42b64 650static int tracepoint_count;
1042e4c0 651
6149aea9
PA
652static struct cmd_list_element *breakpoint_set_cmdlist;
653static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 654struct cmd_list_element *save_cmdlist;
6149aea9 655
468d015d
JJ
656/* Return whether a breakpoint is an active enabled breakpoint. */
657static int
658breakpoint_enabled (struct breakpoint *b)
659{
0d381245 660 return (b->enable_state == bp_enabled);
468d015d
JJ
661}
662
c906108c
SS
663/* Set breakpoint count to NUM. */
664
95a42b64 665static void
fba45db2 666set_breakpoint_count (int num)
c906108c 667{
86b17b60 668 prev_breakpoint_count = breakpoint_count;
c906108c 669 breakpoint_count = num;
4fa62494 670 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
671}
672
86b17b60
PA
673/* Used by `start_rbreak_breakpoints' below, to record the current
674 breakpoint count before "rbreak" creates any breakpoint. */
675static int rbreak_start_breakpoint_count;
676
95a42b64
TT
677/* Called at the start an "rbreak" command to record the first
678 breakpoint made. */
86b17b60 679
95a42b64
TT
680void
681start_rbreak_breakpoints (void)
682{
86b17b60 683 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
684}
685
686/* Called at the end of an "rbreak" command to record the last
687 breakpoint made. */
86b17b60 688
95a42b64
TT
689void
690end_rbreak_breakpoints (void)
691{
86b17b60 692 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
693}
694
4a64f543 695/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
696
697void
fba45db2 698clear_breakpoint_hit_counts (void)
c906108c
SS
699{
700 struct breakpoint *b;
701
702 ALL_BREAKPOINTS (b)
703 b->hit_count = 0;
704}
705
9add0f1b
TT
706/* Allocate a new counted_command_line with reference count of 1.
707 The new structure owns COMMANDS. */
708
709static struct counted_command_line *
710alloc_counted_command_line (struct command_line *commands)
711{
712 struct counted_command_line *result
713 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 714
9add0f1b
TT
715 result->refc = 1;
716 result->commands = commands;
717 return result;
718}
719
720/* Increment reference count. This does nothing if CMD is NULL. */
721
722static void
723incref_counted_command_line (struct counted_command_line *cmd)
724{
725 if (cmd)
726 ++cmd->refc;
727}
728
729/* Decrement reference count. If the reference count reaches 0,
730 destroy the counted_command_line. Sets *CMDP to NULL. This does
731 nothing if *CMDP is NULL. */
732
733static void
734decref_counted_command_line (struct counted_command_line **cmdp)
735{
736 if (*cmdp)
737 {
738 if (--(*cmdp)->refc == 0)
739 {
740 free_command_lines (&(*cmdp)->commands);
741 xfree (*cmdp);
742 }
743 *cmdp = NULL;
744 }
745}
746
747/* A cleanup function that calls decref_counted_command_line. */
748
749static void
750do_cleanup_counted_command_line (void *arg)
751{
752 decref_counted_command_line (arg);
753}
754
755/* Create a cleanup that calls decref_counted_command_line on the
756 argument. */
757
758static struct cleanup *
759make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
760{
761 return make_cleanup (do_cleanup_counted_command_line, cmdp);
762}
763
c906108c 764\f
48cb2d85
VP
765/* Return the breakpoint with the specified number, or NULL
766 if the number does not refer to an existing breakpoint. */
767
768struct breakpoint *
769get_breakpoint (int num)
770{
771 struct breakpoint *b;
772
773 ALL_BREAKPOINTS (b)
774 if (b->number == num)
775 return b;
776
777 return NULL;
778}
5c44784c 779
c906108c 780\f
adc36818 781
b775012e
LM
782/* Mark locations as "conditions have changed" in case the target supports
783 evaluating conditions on its side. */
784
785static void
786mark_breakpoint_modified (struct breakpoint *b)
787{
788 struct bp_location *loc;
789
790 /* This is only meaningful if the target is
791 evaluating conditions and if the user has
792 opted for condition evaluation on the target's
793 side. */
794 if (gdb_evaluates_breakpoint_condition_p ()
795 || !target_supports_evaluation_of_breakpoint_conditions ())
796 return;
797
798 if (!is_breakpoint (b))
799 return;
800
801 for (loc = b->loc; loc; loc = loc->next)
802 loc->condition_changed = condition_modified;
803}
804
805/* Mark location as "conditions have changed" in case the target supports
806 evaluating conditions on its side. */
807
808static void
809mark_breakpoint_location_modified (struct bp_location *loc)
810{
811 /* This is only meaningful if the target is
812 evaluating conditions and if the user has
813 opted for condition evaluation on the target's
814 side. */
815 if (gdb_evaluates_breakpoint_condition_p ()
816 || !target_supports_evaluation_of_breakpoint_conditions ())
817
818 return;
819
820 if (!is_breakpoint (loc->owner))
821 return;
822
823 loc->condition_changed = condition_modified;
824}
825
826/* Sets the condition-evaluation mode using the static global
827 condition_evaluation_mode. */
828
829static void
830set_condition_evaluation_mode (char *args, int from_tty,
831 struct cmd_list_element *c)
832{
b775012e
LM
833 const char *old_mode, *new_mode;
834
835 if ((condition_evaluation_mode_1 == condition_evaluation_target)
836 && !target_supports_evaluation_of_breakpoint_conditions ())
837 {
838 condition_evaluation_mode_1 = condition_evaluation_mode;
839 warning (_("Target does not support breakpoint condition evaluation.\n"
840 "Using host evaluation mode instead."));
841 return;
842 }
843
844 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
845 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
846
abf1152a
JK
847 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
848 settings was "auto". */
849 condition_evaluation_mode = condition_evaluation_mode_1;
850
b775012e
LM
851 /* Only update the mode if the user picked a different one. */
852 if (new_mode != old_mode)
853 {
854 struct bp_location *loc, **loc_tmp;
855 /* If the user switched to a different evaluation mode, we
856 need to synch the changes with the target as follows:
857
858 "host" -> "target": Send all (valid) conditions to the target.
859 "target" -> "host": Remove all the conditions from the target.
860 */
861
b775012e
LM
862 if (new_mode == condition_evaluation_target)
863 {
864 /* Mark everything modified and synch conditions with the
865 target. */
866 ALL_BP_LOCATIONS (loc, loc_tmp)
867 mark_breakpoint_location_modified (loc);
868 }
869 else
870 {
871 /* Manually mark non-duplicate locations to synch conditions
872 with the target. We do this to remove all the conditions the
873 target knows about. */
874 ALL_BP_LOCATIONS (loc, loc_tmp)
875 if (is_breakpoint (loc->owner) && loc->inserted)
876 loc->needs_update = 1;
877 }
878
879 /* Do the update. */
44702360 880 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
881 }
882
883 return;
884}
885
886/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
887 what "auto" is translating to. */
888
889static void
890show_condition_evaluation_mode (struct ui_file *file, int from_tty,
891 struct cmd_list_element *c, const char *value)
892{
893 if (condition_evaluation_mode == condition_evaluation_auto)
894 fprintf_filtered (file,
895 _("Breakpoint condition evaluation "
896 "mode is %s (currently %s).\n"),
897 value,
898 breakpoint_condition_evaluation_mode ());
899 else
900 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
901 value);
902}
903
904/* A comparison function for bp_location AP and BP that is used by
905 bsearch. This comparison function only cares about addresses, unlike
906 the more general bp_location_compare function. */
907
908static int
909bp_location_compare_addrs (const void *ap, const void *bp)
910{
911 struct bp_location *a = *(void **) ap;
912 struct bp_location *b = *(void **) bp;
913
914 if (a->address == b->address)
915 return 0;
916 else
917 return ((a->address > b->address) - (a->address < b->address));
918}
919
920/* Helper function to skip all bp_locations with addresses
921 less than ADDRESS. It returns the first bp_location that
922 is greater than or equal to ADDRESS. If none is found, just
923 return NULL. */
924
925static struct bp_location **
926get_first_locp_gte_addr (CORE_ADDR address)
927{
928 struct bp_location dummy_loc;
929 struct bp_location *dummy_locp = &dummy_loc;
930 struct bp_location **locp_found = NULL;
931
932 /* Initialize the dummy location's address field. */
933 memset (&dummy_loc, 0, sizeof (struct bp_location));
934 dummy_loc.address = address;
935
936 /* Find a close match to the first location at ADDRESS. */
937 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
938 sizeof (struct bp_location **),
939 bp_location_compare_addrs);
940
941 /* Nothing was found, nothing left to do. */
942 if (locp_found == NULL)
943 return NULL;
944
945 /* We may have found a location that is at ADDRESS but is not the first in the
946 location's list. Go backwards (if possible) and locate the first one. */
947 while ((locp_found - 1) >= bp_location
948 && (*(locp_found - 1))->address == address)
949 locp_found--;
950
951 return locp_found;
952}
953
adc36818
PM
954void
955set_breakpoint_condition (struct breakpoint *b, char *exp,
956 int from_tty)
957{
3a5c3e22
PA
958 xfree (b->cond_string);
959 b->cond_string = NULL;
adc36818 960
3a5c3e22 961 if (is_watchpoint (b))
adc36818 962 {
3a5c3e22
PA
963 struct watchpoint *w = (struct watchpoint *) b;
964
965 xfree (w->cond_exp);
966 w->cond_exp = NULL;
967 }
968 else
969 {
970 struct bp_location *loc;
971
972 for (loc = b->loc; loc; loc = loc->next)
973 {
974 xfree (loc->cond);
975 loc->cond = NULL;
b775012e
LM
976
977 /* No need to free the condition agent expression
978 bytecode (if we have one). We will handle this
979 when we go through update_global_location_list. */
3a5c3e22 980 }
adc36818 981 }
adc36818
PM
982
983 if (*exp == 0)
984 {
985 if (from_tty)
986 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
987 }
988 else
989 {
bbc13ae3 990 const char *arg = exp;
cc59ec59 991
adc36818
PM
992 /* I don't know if it matters whether this is the string the user
993 typed in or the decompiled expression. */
994 b->cond_string = xstrdup (arg);
995 b->condition_not_parsed = 0;
996
997 if (is_watchpoint (b))
998 {
3a5c3e22
PA
999 struct watchpoint *w = (struct watchpoint *) b;
1000
adc36818
PM
1001 innermost_block = NULL;
1002 arg = exp;
1bb9788d 1003 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1004 if (*arg)
1005 error (_("Junk at end of expression"));
3a5c3e22 1006 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1007 }
1008 else
1009 {
3a5c3e22
PA
1010 struct bp_location *loc;
1011
adc36818
PM
1012 for (loc = b->loc; loc; loc = loc->next)
1013 {
1014 arg = exp;
1015 loc->cond =
1bb9788d
TT
1016 parse_exp_1 (&arg, loc->address,
1017 block_for_pc (loc->address), 0);
adc36818
PM
1018 if (*arg)
1019 error (_("Junk at end of expression"));
1020 }
1021 }
1022 }
b775012e
LM
1023 mark_breakpoint_modified (b);
1024
8d3788bd 1025 observer_notify_breakpoint_modified (b);
adc36818
PM
1026}
1027
d55637df
TT
1028/* Completion for the "condition" command. */
1029
1030static VEC (char_ptr) *
6f937416
PA
1031condition_completer (struct cmd_list_element *cmd,
1032 const char *text, const char *word)
d55637df 1033{
6f937416 1034 const char *space;
d55637df 1035
6f937416
PA
1036 text = skip_spaces_const (text);
1037 space = skip_to_space_const (text);
d55637df
TT
1038 if (*space == '\0')
1039 {
1040 int len;
1041 struct breakpoint *b;
1042 VEC (char_ptr) *result = NULL;
1043
1044 if (text[0] == '$')
1045 {
1046 /* We don't support completion of history indices. */
1047 if (isdigit (text[1]))
1048 return NULL;
1049 return complete_internalvar (&text[1]);
1050 }
1051
1052 /* We're completing the breakpoint number. */
1053 len = strlen (text);
1054
1055 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1056 {
1057 char number[50];
1058
1059 xsnprintf (number, sizeof (number), "%d", b->number);
1060
1061 if (strncmp (number, text, len) == 0)
1062 VEC_safe_push (char_ptr, result, xstrdup (number));
1063 }
d55637df
TT
1064
1065 return result;
1066 }
1067
1068 /* We're completing the expression part. */
6f937416 1069 text = skip_spaces_const (space);
d55637df
TT
1070 return expression_completer (cmd, text, word);
1071}
1072
c906108c
SS
1073/* condition N EXP -- set break condition of breakpoint N to EXP. */
1074
1075static void
fba45db2 1076condition_command (char *arg, int from_tty)
c906108c 1077{
52f0bd74 1078 struct breakpoint *b;
c906108c 1079 char *p;
52f0bd74 1080 int bnum;
c906108c
SS
1081
1082 if (arg == 0)
e2e0b3e5 1083 error_no_arg (_("breakpoint number"));
c906108c
SS
1084
1085 p = arg;
1086 bnum = get_number (&p);
5c44784c 1087 if (bnum == 0)
8a3fe4f8 1088 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1089
1090 ALL_BREAKPOINTS (b)
1091 if (b->number == bnum)
2f069f6f 1092 {
6dddc817
DE
1093 /* Check if this breakpoint has a "stop" method implemented in an
1094 extension language. This method and conditions entered into GDB
1095 from the CLI are mutually exclusive. */
1096 const struct extension_language_defn *extlang
1097 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1098
1099 if (extlang != NULL)
1100 {
1101 error (_("Only one stop condition allowed. There is currently"
1102 " a %s stop condition defined for this breakpoint."),
1103 ext_lang_capitalized_name (extlang));
1104 }
2566ad2d 1105 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1106
1107 if (is_breakpoint (b))
44702360 1108 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1109
2f069f6f
JB
1110 return;
1111 }
c906108c 1112
8a3fe4f8 1113 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1114}
1115
a7bdde9e
VP
1116/* Check that COMMAND do not contain commands that are suitable
1117 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1118 Throw if any such commands is found. */
1119
a7bdde9e
VP
1120static void
1121check_no_tracepoint_commands (struct command_line *commands)
1122{
1123 struct command_line *c;
cc59ec59 1124
a7bdde9e
VP
1125 for (c = commands; c; c = c->next)
1126 {
1127 int i;
1128
1129 if (c->control_type == while_stepping_control)
3e43a32a
MS
1130 error (_("The 'while-stepping' command can "
1131 "only be used for tracepoints"));
a7bdde9e
VP
1132
1133 for (i = 0; i < c->body_count; ++i)
1134 check_no_tracepoint_commands ((c->body_list)[i]);
1135
1136 /* Not that command parsing removes leading whitespace and comment
4a64f543 1137 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1138 command directly. */
1139 if (strstr (c->line, "collect ") == c->line)
1140 error (_("The 'collect' command can only be used for tracepoints"));
1141
51661e93
VP
1142 if (strstr (c->line, "teval ") == c->line)
1143 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1144 }
1145}
1146
d77f58be
SS
1147/* Encapsulate tests for different types of tracepoints. */
1148
d9b3f62e
PA
1149static int
1150is_tracepoint_type (enum bptype type)
1151{
1152 return (type == bp_tracepoint
1153 || type == bp_fast_tracepoint
1154 || type == bp_static_tracepoint);
1155}
1156
a7bdde9e 1157int
d77f58be 1158is_tracepoint (const struct breakpoint *b)
a7bdde9e 1159{
d9b3f62e 1160 return is_tracepoint_type (b->type);
a7bdde9e 1161}
d9b3f62e 1162
e5dd4106 1163/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1164 breakpoint. This function will throw an exception if a problem is
1165 found. */
48cb2d85 1166
95a42b64
TT
1167static void
1168validate_commands_for_breakpoint (struct breakpoint *b,
1169 struct command_line *commands)
48cb2d85 1170{
d77f58be 1171 if (is_tracepoint (b))
a7bdde9e 1172 {
c9a6ce02 1173 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1174 struct command_line *c;
1175 struct command_line *while_stepping = 0;
c9a6ce02
PA
1176
1177 /* Reset the while-stepping step count. The previous commands
1178 might have included a while-stepping action, while the new
1179 ones might not. */
1180 t->step_count = 0;
1181
1182 /* We need to verify that each top-level element of commands is
1183 valid for tracepoints, that there's at most one
1184 while-stepping element, and that the while-stepping's body
1185 has valid tracing commands excluding nested while-stepping.
1186 We also need to validate the tracepoint action line in the
1187 context of the tracepoint --- validate_actionline actually
1188 has side effects, like setting the tracepoint's
1189 while-stepping STEP_COUNT, in addition to checking if the
1190 collect/teval actions parse and make sense in the
1191 tracepoint's context. */
a7bdde9e
VP
1192 for (c = commands; c; c = c->next)
1193 {
a7bdde9e
VP
1194 if (c->control_type == while_stepping_control)
1195 {
1196 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1197 error (_("The 'while-stepping' command "
1198 "cannot be used for fast tracepoint"));
0fb4aa4b 1199 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1200 error (_("The 'while-stepping' command "
1201 "cannot be used for static tracepoint"));
a7bdde9e
VP
1202
1203 if (while_stepping)
3e43a32a
MS
1204 error (_("The 'while-stepping' command "
1205 "can be used only once"));
a7bdde9e
VP
1206 else
1207 while_stepping = c;
1208 }
c9a6ce02
PA
1209
1210 validate_actionline (c->line, b);
a7bdde9e
VP
1211 }
1212 if (while_stepping)
1213 {
1214 struct command_line *c2;
1215
1216 gdb_assert (while_stepping->body_count == 1);
1217 c2 = while_stepping->body_list[0];
1218 for (; c2; c2 = c2->next)
1219 {
a7bdde9e
VP
1220 if (c2->control_type == while_stepping_control)
1221 error (_("The 'while-stepping' command cannot be nested"));
1222 }
1223 }
1224 }
1225 else
1226 {
1227 check_no_tracepoint_commands (commands);
1228 }
95a42b64
TT
1229}
1230
0fb4aa4b
PA
1231/* Return a vector of all the static tracepoints set at ADDR. The
1232 caller is responsible for releasing the vector. */
1233
1234VEC(breakpoint_p) *
1235static_tracepoints_here (CORE_ADDR addr)
1236{
1237 struct breakpoint *b;
1238 VEC(breakpoint_p) *found = 0;
1239 struct bp_location *loc;
1240
1241 ALL_BREAKPOINTS (b)
1242 if (b->type == bp_static_tracepoint)
1243 {
1244 for (loc = b->loc; loc; loc = loc->next)
1245 if (loc->address == addr)
1246 VEC_safe_push(breakpoint_p, found, b);
1247 }
1248
1249 return found;
1250}
1251
95a42b64 1252/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1253 validate that only allowed commands are included. */
95a42b64
TT
1254
1255void
4a64f543
MS
1256breakpoint_set_commands (struct breakpoint *b,
1257 struct command_line *commands)
95a42b64
TT
1258{
1259 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1260
9add0f1b
TT
1261 decref_counted_command_line (&b->commands);
1262 b->commands = alloc_counted_command_line (commands);
8d3788bd 1263 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1264}
1265
45a43567
TT
1266/* Set the internal `silent' flag on the breakpoint. Note that this
1267 is not the same as the "silent" that may appear in the breakpoint's
1268 commands. */
1269
1270void
1271breakpoint_set_silent (struct breakpoint *b, int silent)
1272{
1273 int old_silent = b->silent;
1274
1275 b->silent = silent;
1276 if (old_silent != silent)
8d3788bd 1277 observer_notify_breakpoint_modified (b);
45a43567
TT
1278}
1279
1280/* Set the thread for this breakpoint. If THREAD is -1, make the
1281 breakpoint work for any thread. */
1282
1283void
1284breakpoint_set_thread (struct breakpoint *b, int thread)
1285{
1286 int old_thread = b->thread;
1287
1288 b->thread = thread;
1289 if (old_thread != thread)
8d3788bd 1290 observer_notify_breakpoint_modified (b);
45a43567
TT
1291}
1292
1293/* Set the task for this breakpoint. If TASK is 0, make the
1294 breakpoint work for any task. */
1295
1296void
1297breakpoint_set_task (struct breakpoint *b, int task)
1298{
1299 int old_task = b->task;
1300
1301 b->task = task;
1302 if (old_task != task)
8d3788bd 1303 observer_notify_breakpoint_modified (b);
45a43567
TT
1304}
1305
95a42b64
TT
1306void
1307check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1308{
1309 struct breakpoint *b = closure;
cc59ec59 1310
6f937416 1311 validate_actionline (line, b);
a7bdde9e
VP
1312}
1313
95a42b64
TT
1314/* A structure used to pass information through
1315 map_breakpoint_numbers. */
1316
1317struct commands_info
1318{
1319 /* True if the command was typed at a tty. */
1320 int from_tty;
86b17b60
PA
1321
1322 /* The breakpoint range spec. */
1323 char *arg;
1324
95a42b64
TT
1325 /* Non-NULL if the body of the commands are being read from this
1326 already-parsed command. */
1327 struct command_line *control;
86b17b60 1328
95a42b64
TT
1329 /* The command lines read from the user, or NULL if they have not
1330 yet been read. */
1331 struct counted_command_line *cmd;
1332};
1333
1334/* A callback for map_breakpoint_numbers that sets the commands for
1335 commands_command. */
1336
c906108c 1337static void
95a42b64 1338do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1339{
95a42b64 1340 struct commands_info *info = data;
c906108c 1341
95a42b64
TT
1342 if (info->cmd == NULL)
1343 {
1344 struct command_line *l;
5c44784c 1345
95a42b64
TT
1346 if (info->control != NULL)
1347 l = copy_command_lines (info->control->body_list[0]);
1348 else
86b17b60
PA
1349 {
1350 struct cleanup *old_chain;
1351 char *str;
c5aa993b 1352
3e43a32a
MS
1353 str = xstrprintf (_("Type commands for breakpoint(s) "
1354 "%s, one per line."),
86b17b60
PA
1355 info->arg);
1356
1357 old_chain = make_cleanup (xfree, str);
1358
1359 l = read_command_lines (str,
1360 info->from_tty, 1,
d77f58be 1361 (is_tracepoint (b)
86b17b60
PA
1362 ? check_tracepoint_command : 0),
1363 b);
1364
1365 do_cleanups (old_chain);
1366 }
a7bdde9e 1367
95a42b64
TT
1368 info->cmd = alloc_counted_command_line (l);
1369 }
1370
1371 /* If a breakpoint was on the list more than once, we don't need to
1372 do anything. */
1373 if (b->commands != info->cmd)
1374 {
1375 validate_commands_for_breakpoint (b, info->cmd->commands);
1376 incref_counted_command_line (info->cmd);
1377 decref_counted_command_line (&b->commands);
1378 b->commands = info->cmd;
8d3788bd 1379 observer_notify_breakpoint_modified (b);
c5aa993b 1380 }
95a42b64
TT
1381}
1382
1383static void
4a64f543
MS
1384commands_command_1 (char *arg, int from_tty,
1385 struct command_line *control)
95a42b64
TT
1386{
1387 struct cleanup *cleanups;
1388 struct commands_info info;
1389
1390 info.from_tty = from_tty;
1391 info.control = control;
1392 info.cmd = NULL;
1393 /* If we read command lines from the user, then `info' will hold an
1394 extra reference to the commands that we must clean up. */
1395 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1396
1397 if (arg == NULL || !*arg)
1398 {
86b17b60 1399 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1400 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1401 breakpoint_count);
95a42b64
TT
1402 else if (breakpoint_count > 0)
1403 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1404 else
1405 {
1406 /* So that we don't try to free the incoming non-NULL
1407 argument in the cleanup below. Mapping breakpoint
1408 numbers will fail in this case. */
1409 arg = NULL;
1410 }
95a42b64 1411 }
9766ced4
SS
1412 else
1413 /* The command loop has some static state, so we need to preserve
1414 our argument. */
1415 arg = xstrdup (arg);
86b17b60
PA
1416
1417 if (arg != NULL)
1418 make_cleanup (xfree, arg);
1419
1420 info.arg = arg;
95a42b64
TT
1421
1422 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1423
1424 if (info.cmd == NULL)
1425 error (_("No breakpoints specified."));
1426
1427 do_cleanups (cleanups);
1428}
1429
1430static void
1431commands_command (char *arg, int from_tty)
1432{
1433 commands_command_1 (arg, from_tty, NULL);
c906108c 1434}
40c03ae8
EZ
1435
1436/* Like commands_command, but instead of reading the commands from
1437 input stream, takes them from an already parsed command structure.
1438
1439 This is used by cli-script.c to DTRT with breakpoint commands
1440 that are part of if and while bodies. */
1441enum command_control_type
1442commands_from_control_command (char *arg, struct command_line *cmd)
1443{
95a42b64
TT
1444 commands_command_1 (arg, 0, cmd);
1445 return simple_control;
40c03ae8 1446}
876fa593
JK
1447
1448/* Return non-zero if BL->TARGET_INFO contains valid information. */
1449
1450static int
1451bp_location_has_shadow (struct bp_location *bl)
1452{
1453 if (bl->loc_type != bp_loc_software_breakpoint)
1454 return 0;
1455 if (!bl->inserted)
1456 return 0;
1457 if (bl->target_info.shadow_len == 0)
e5dd4106 1458 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1459 return 0;
1460 return 1;
1461}
1462
9d497a19
PA
1463/* Update BUF, which is LEN bytes read from the target address
1464 MEMADDR, by replacing a memory breakpoint with its shadowed
1465 contents.
1466
1467 If READBUF is not NULL, this buffer must not overlap with the of
1468 the breakpoint location's shadow_contents buffer. Otherwise, a
1469 failed assertion internal error will be raised. */
1470
1471static void
1472one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1473 const gdb_byte *writebuf_org,
1474 ULONGEST memaddr, LONGEST len,
1475 struct bp_target_info *target_info,
1476 struct gdbarch *gdbarch)
1477{
1478 /* Now do full processing of the found relevant range of elements. */
1479 CORE_ADDR bp_addr = 0;
1480 int bp_size = 0;
1481 int bptoffset = 0;
1482
1483 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1484 current_program_space->aspace, 0))
1485 {
1486 /* The breakpoint is inserted in a different address space. */
1487 return;
1488 }
1489
1490 /* Addresses and length of the part of the breakpoint that
1491 we need to copy. */
1492 bp_addr = target_info->placed_address;
1493 bp_size = target_info->shadow_len;
1494
1495 if (bp_addr + bp_size <= memaddr)
1496 {
1497 /* The breakpoint is entirely before the chunk of memory we are
1498 reading. */
1499 return;
1500 }
1501
1502 if (bp_addr >= memaddr + len)
1503 {
1504 /* The breakpoint is entirely after the chunk of memory we are
1505 reading. */
1506 return;
1507 }
1508
1509 /* Offset within shadow_contents. */
1510 if (bp_addr < memaddr)
1511 {
1512 /* Only copy the second part of the breakpoint. */
1513 bp_size -= memaddr - bp_addr;
1514 bptoffset = memaddr - bp_addr;
1515 bp_addr = memaddr;
1516 }
1517
1518 if (bp_addr + bp_size > memaddr + len)
1519 {
1520 /* Only copy the first part of the breakpoint. */
1521 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1522 }
1523
1524 if (readbuf != NULL)
1525 {
1526 /* Verify that the readbuf buffer does not overlap with the
1527 shadow_contents buffer. */
1528 gdb_assert (target_info->shadow_contents >= readbuf + len
1529 || readbuf >= (target_info->shadow_contents
1530 + target_info->shadow_len));
1531
1532 /* Update the read buffer with this inserted breakpoint's
1533 shadow. */
1534 memcpy (readbuf + bp_addr - memaddr,
1535 target_info->shadow_contents + bptoffset, bp_size);
1536 }
1537 else
1538 {
1539 const unsigned char *bp;
0d5ed153
MR
1540 CORE_ADDR addr = target_info->reqstd_address;
1541 int placed_size;
9d497a19
PA
1542
1543 /* Update the shadow with what we want to write to memory. */
1544 memcpy (target_info->shadow_contents + bptoffset,
1545 writebuf_org + bp_addr - memaddr, bp_size);
1546
1547 /* Determine appropriate breakpoint contents and size for this
1548 address. */
0d5ed153 1549 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1550
1551 /* Update the final write buffer with this inserted
1552 breakpoint's INSN. */
1553 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1554 }
1555}
1556
8defab1a 1557/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1558 by replacing any memory breakpoints with their shadowed contents.
1559
35c63cd8
JB
1560 If READBUF is not NULL, this buffer must not overlap with any of
1561 the breakpoint location's shadow_contents buffers. Otherwise,
1562 a failed assertion internal error will be raised.
1563
876fa593 1564 The range of shadowed area by each bp_location is:
35df4500
TJB
1565 bl->address - bp_location_placed_address_before_address_max
1566 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1567 The range we were requested to resolve shadows for is:
1568 memaddr ... memaddr + len
1569 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1570 memaddr + len <= (bl->address
1571 - bp_location_placed_address_before_address_max)
876fa593 1572 and:
35df4500 1573 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1574
8defab1a 1575void
f0ba3972
PA
1576breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1577 const gdb_byte *writebuf_org,
1578 ULONGEST memaddr, LONGEST len)
c906108c 1579{
4a64f543
MS
1580 /* Left boundary, right boundary and median element of our binary
1581 search. */
876fa593 1582 unsigned bc_l, bc_r, bc;
9d497a19 1583 size_t i;
876fa593 1584
4a64f543
MS
1585 /* Find BC_L which is a leftmost element which may affect BUF
1586 content. It is safe to report lower value but a failure to
1587 report higher one. */
876fa593
JK
1588
1589 bc_l = 0;
1590 bc_r = bp_location_count;
1591 while (bc_l + 1 < bc_r)
1592 {
35df4500 1593 struct bp_location *bl;
876fa593
JK
1594
1595 bc = (bc_l + bc_r) / 2;
35df4500 1596 bl = bp_location[bc];
876fa593 1597
4a64f543
MS
1598 /* Check first BL->ADDRESS will not overflow due to the added
1599 constant. Then advance the left boundary only if we are sure
1600 the BC element can in no way affect the BUF content (MEMADDR
1601 to MEMADDR + LEN range).
876fa593 1602
4a64f543
MS
1603 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1604 offset so that we cannot miss a breakpoint with its shadow
1605 range tail still reaching MEMADDR. */
c5aa993b 1606
35df4500
TJB
1607 if ((bl->address + bp_location_shadow_len_after_address_max
1608 >= bl->address)
1609 && (bl->address + bp_location_shadow_len_after_address_max
1610 <= memaddr))
876fa593
JK
1611 bc_l = bc;
1612 else
1613 bc_r = bc;
1614 }
1615
128070bb
PA
1616 /* Due to the binary search above, we need to make sure we pick the
1617 first location that's at BC_L's address. E.g., if there are
1618 multiple locations at the same address, BC_L may end up pointing
1619 at a duplicate location, and miss the "master"/"inserted"
1620 location. Say, given locations L1, L2 and L3 at addresses A and
1621 B:
1622
1623 L1@A, L2@A, L3@B, ...
1624
1625 BC_L could end up pointing at location L2, while the "master"
1626 location could be L1. Since the `loc->inserted' flag is only set
1627 on "master" locations, we'd forget to restore the shadow of L1
1628 and L2. */
1629 while (bc_l > 0
1630 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1631 bc_l--;
1632
876fa593
JK
1633 /* Now do full processing of the found relevant range of elements. */
1634
1635 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1636 {
35df4500 1637 struct bp_location *bl = bp_location[bc];
876fa593
JK
1638 CORE_ADDR bp_addr = 0;
1639 int bp_size = 0;
1640 int bptoffset = 0;
1641
35df4500
TJB
1642 /* bp_location array has BL->OWNER always non-NULL. */
1643 if (bl->owner->type == bp_none)
8a3fe4f8 1644 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1645 bl->owner->number);
ffce0d52 1646
e5dd4106 1647 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1648 content. */
1649
35df4500
TJB
1650 if (bl->address >= bp_location_placed_address_before_address_max
1651 && memaddr + len <= (bl->address
1652 - bp_location_placed_address_before_address_max))
876fa593
JK
1653 break;
1654
35df4500 1655 if (!bp_location_has_shadow (bl))
c5aa993b 1656 continue;
6c95b8df 1657
9d497a19
PA
1658 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1659 memaddr, len, &bl->target_info, bl->gdbarch);
1660 }
c906108c 1661}
9d497a19 1662
c906108c 1663\f
c5aa993b 1664
b775012e
LM
1665/* Return true if BPT is either a software breakpoint or a hardware
1666 breakpoint. */
1667
1668int
1669is_breakpoint (const struct breakpoint *bpt)
1670{
1671 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1672 || bpt->type == bp_hardware_breakpoint
1673 || bpt->type == bp_dprintf);
b775012e
LM
1674}
1675
60e1c644
PA
1676/* Return true if BPT is of any hardware watchpoint kind. */
1677
a5606eee 1678static int
d77f58be 1679is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1680{
1681 return (bpt->type == bp_hardware_watchpoint
1682 || bpt->type == bp_read_watchpoint
1683 || bpt->type == bp_access_watchpoint);
1684}
7270d8f2 1685
60e1c644
PA
1686/* Return true if BPT is of any watchpoint kind, hardware or
1687 software. */
1688
3a5c3e22 1689int
d77f58be 1690is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1691{
1692 return (is_hardware_watchpoint (bpt)
1693 || bpt->type == bp_watchpoint);
1694}
1695
3a5c3e22
PA
1696/* Returns true if the current thread and its running state are safe
1697 to evaluate or update watchpoint B. Watchpoints on local
1698 expressions need to be evaluated in the context of the thread that
1699 was current when the watchpoint was created, and, that thread needs
1700 to be stopped to be able to select the correct frame context.
1701 Watchpoints on global expressions can be evaluated on any thread,
1702 and in any state. It is presently left to the target allowing
1703 memory accesses when threads are running. */
f6bc2008
PA
1704
1705static int
3a5c3e22 1706watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1707{
d0d8b0c6
JK
1708 return (b->base.pspace == current_program_space
1709 && (ptid_equal (b->watchpoint_thread, null_ptid)
1710 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1711 && !is_executing (inferior_ptid))));
f6bc2008
PA
1712}
1713
d0fb5eae
JK
1714/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1715 associated bp_watchpoint_scope breakpoint. */
1716
1717static void
3a5c3e22 1718watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1719{
3a5c3e22 1720 struct breakpoint *b = &w->base;
d0fb5eae
JK
1721
1722 if (b->related_breakpoint != b)
1723 {
1724 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1725 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1726 b->related_breakpoint->disposition = disp_del_at_next_stop;
1727 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1728 b->related_breakpoint = b;
1729 }
1730 b->disposition = disp_del_at_next_stop;
1731}
1732
bb9d5f81
PP
1733/* Extract a bitfield value from value VAL using the bit parameters contained in
1734 watchpoint W. */
1735
1736static struct value *
1737extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1738{
1739 struct value *bit_val;
1740
1741 if (val == NULL)
1742 return NULL;
1743
1744 bit_val = allocate_value (value_type (val));
1745
1746 unpack_value_bitfield (bit_val,
1747 w->val_bitpos,
1748 w->val_bitsize,
1749 value_contents_for_printing (val),
1750 value_offset (val),
1751 val);
1752
1753 return bit_val;
1754}
1755
567e1b4e
JB
1756/* Assuming that B is a watchpoint:
1757 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1758 - Evaluate expression and store the result in B->val
567e1b4e
JB
1759 - Evaluate the condition if there is one, and store the result
1760 in b->loc->cond.
a5606eee
VP
1761 - Update the list of values that must be watched in B->loc.
1762
4a64f543
MS
1763 If the watchpoint disposition is disp_del_at_next_stop, then do
1764 nothing. If this is local watchpoint that is out of scope, delete
1765 it.
1766
1767 Even with `set breakpoint always-inserted on' the watchpoints are
1768 removed + inserted on each stop here. Normal breakpoints must
1769 never be removed because they might be missed by a running thread
1770 when debugging in non-stop mode. On the other hand, hardware
1771 watchpoints (is_hardware_watchpoint; processed here) are specific
1772 to each LWP since they are stored in each LWP's hardware debug
1773 registers. Therefore, such LWP must be stopped first in order to
1774 be able to modify its hardware watchpoints.
1775
1776 Hardware watchpoints must be reset exactly once after being
1777 presented to the user. It cannot be done sooner, because it would
1778 reset the data used to present the watchpoint hit to the user. And
1779 it must not be done later because it could display the same single
1780 watchpoint hit during multiple GDB stops. Note that the latter is
1781 relevant only to the hardware watchpoint types bp_read_watchpoint
1782 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1783 not user-visible - its hit is suppressed if the memory content has
1784 not changed.
1785
1786 The following constraints influence the location where we can reset
1787 hardware watchpoints:
1788
1789 * target_stopped_by_watchpoint and target_stopped_data_address are
1790 called several times when GDB stops.
1791
1792 [linux]
1793 * Multiple hardware watchpoints can be hit at the same time,
1794 causing GDB to stop. GDB only presents one hardware watchpoint
1795 hit at a time as the reason for stopping, and all the other hits
1796 are presented later, one after the other, each time the user
1797 requests the execution to be resumed. Execution is not resumed
1798 for the threads still having pending hit event stored in
1799 LWP_INFO->STATUS. While the watchpoint is already removed from
1800 the inferior on the first stop the thread hit event is kept being
1801 reported from its cached value by linux_nat_stopped_data_address
1802 until the real thread resume happens after the watchpoint gets
1803 presented and thus its LWP_INFO->STATUS gets reset.
1804
1805 Therefore the hardware watchpoint hit can get safely reset on the
1806 watchpoint removal from inferior. */
a79d3c27 1807
b40ce68a 1808static void
3a5c3e22 1809update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1810{
a5606eee 1811 int within_current_scope;
a5606eee 1812 struct frame_id saved_frame_id;
66076460 1813 int frame_saved;
a5606eee 1814
f6bc2008
PA
1815 /* If this is a local watchpoint, we only want to check if the
1816 watchpoint frame is in scope if the current thread is the thread
1817 that was used to create the watchpoint. */
1818 if (!watchpoint_in_thread_scope (b))
1819 return;
1820
3a5c3e22 1821 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1822 return;
1823
66076460 1824 frame_saved = 0;
a5606eee
VP
1825
1826 /* Determine if the watchpoint is within scope. */
1827 if (b->exp_valid_block == NULL)
1828 within_current_scope = 1;
1829 else
1830 {
b5db5dfc
UW
1831 struct frame_info *fi = get_current_frame ();
1832 struct gdbarch *frame_arch = get_frame_arch (fi);
1833 CORE_ADDR frame_pc = get_frame_pc (fi);
1834
1835 /* If we're in a function epilogue, unwinding may not work
1836 properly, so do not attempt to recreate locations at this
1837 point. See similar comments in watchpoint_check. */
1838 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1839 return;
66076460
DJ
1840
1841 /* Save the current frame's ID so we can restore it after
1842 evaluating the watchpoint expression on its own frame. */
1843 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1844 took a frame parameter, so that we didn't have to change the
1845 selected frame. */
1846 frame_saved = 1;
1847 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1848
a5606eee
VP
1849 fi = frame_find_by_id (b->watchpoint_frame);
1850 within_current_scope = (fi != NULL);
1851 if (within_current_scope)
1852 select_frame (fi);
1853 }
1854
b5db5dfc
UW
1855 /* We don't free locations. They are stored in the bp_location array
1856 and update_global_location_list will eventually delete them and
1857 remove breakpoints if needed. */
3a5c3e22 1858 b->base.loc = NULL;
b5db5dfc 1859
a5606eee
VP
1860 if (within_current_scope && reparse)
1861 {
bbc13ae3 1862 const char *s;
d63d0675 1863
a5606eee
VP
1864 if (b->exp)
1865 {
1866 xfree (b->exp);
1867 b->exp = NULL;
1868 }
d63d0675 1869 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1870 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1871 /* If the meaning of expression itself changed, the old value is
1872 no longer relevant. We don't want to report a watchpoint hit
1873 to the user when the old value and the new value may actually
1874 be completely different objects. */
1875 value_free (b->val);
fa4727a6
DJ
1876 b->val = NULL;
1877 b->val_valid = 0;
60e1c644
PA
1878
1879 /* Note that unlike with breakpoints, the watchpoint's condition
1880 expression is stored in the breakpoint object, not in the
1881 locations (re)created below. */
3a5c3e22 1882 if (b->base.cond_string != NULL)
60e1c644
PA
1883 {
1884 if (b->cond_exp != NULL)
1885 {
1886 xfree (b->cond_exp);
1887 b->cond_exp = NULL;
1888 }
1889
3a5c3e22 1890 s = b->base.cond_string;
1bb9788d 1891 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1892 }
a5606eee 1893 }
a5606eee
VP
1894
1895 /* If we failed to parse the expression, for example because
1896 it refers to a global variable in a not-yet-loaded shared library,
1897 don't try to insert watchpoint. We don't automatically delete
1898 such watchpoint, though, since failure to parse expression
1899 is different from out-of-scope watchpoint. */
e8369a73 1900 if (!target_has_execution)
2d134ed3
PA
1901 {
1902 /* Without execution, memory can't change. No use to try and
1903 set watchpoint locations. The watchpoint will be reset when
1904 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1905 if (!can_use_hw_watchpoints)
1906 {
1907 if (b->base.ops->works_in_software_mode (&b->base))
1908 b->base.type = bp_watchpoint;
1909 else
638aa5a1
AB
1910 error (_("Can't set read/access watchpoint when "
1911 "hardware watchpoints are disabled."));
e8369a73 1912 }
2d134ed3
PA
1913 }
1914 else if (within_current_scope && b->exp)
a5606eee 1915 {
0cf6dd15 1916 int pc = 0;
fa4727a6 1917 struct value *val_chain, *v, *result, *next;
2d134ed3 1918 struct program_space *frame_pspace;
a5606eee 1919
3a1115a0 1920 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1921
a5606eee
VP
1922 /* Avoid setting b->val if it's already set. The meaning of
1923 b->val is 'the last value' user saw, and we should update
1924 it only if we reported that last value to user. As it
9c06b0b4
TJB
1925 happens, the code that reports it updates b->val directly.
1926 We don't keep track of the memory value for masked
1927 watchpoints. */
3a5c3e22 1928 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1929 {
bb9d5f81
PP
1930 if (b->val_bitsize != 0)
1931 {
1932 v = extract_bitfield_from_watchpoint_value (b, v);
1933 if (v != NULL)
1934 release_value (v);
1935 }
fa4727a6
DJ
1936 b->val = v;
1937 b->val_valid = 1;
1938 }
a5606eee 1939
2d134ed3
PA
1940 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1941
a5606eee 1942 /* Look at each value on the value chain. */
9fa40276 1943 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1944 {
1945 /* If it's a memory location, and GDB actually needed
1946 its contents to evaluate the expression, then we
fa4727a6
DJ
1947 must watch it. If the first value returned is
1948 still lazy, that means an error occurred reading it;
1949 watch it anyway in case it becomes readable. */
a5606eee 1950 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1951 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1952 {
1953 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1954
a5606eee
VP
1955 /* We only watch structs and arrays if user asked
1956 for it explicitly, never if they just happen to
1957 appear in the middle of some value chain. */
fa4727a6 1958 if (v == result
a5606eee
VP
1959 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1960 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1961 {
1962 CORE_ADDR addr;
744a8059 1963 int type;
a5606eee 1964 struct bp_location *loc, **tmp;
bb9d5f81
PP
1965 int bitpos = 0, bitsize = 0;
1966
1967 if (value_bitsize (v) != 0)
1968 {
1969 /* Extract the bit parameters out from the bitfield
1970 sub-expression. */
1971 bitpos = value_bitpos (v);
1972 bitsize = value_bitsize (v);
1973 }
1974 else if (v == result && b->val_bitsize != 0)
1975 {
1976 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1977 lvalue whose bit parameters are saved in the fields
1978 VAL_BITPOS and VAL_BITSIZE. */
1979 bitpos = b->val_bitpos;
1980 bitsize = b->val_bitsize;
1981 }
a5606eee 1982
42ae5230 1983 addr = value_address (v);
bb9d5f81
PP
1984 if (bitsize != 0)
1985 {
1986 /* Skip the bytes that don't contain the bitfield. */
1987 addr += bitpos / 8;
1988 }
1989
a5606eee 1990 type = hw_write;
3a5c3e22 1991 if (b->base.type == bp_read_watchpoint)
a5606eee 1992 type = hw_read;
3a5c3e22 1993 else if (b->base.type == bp_access_watchpoint)
a5606eee 1994 type = hw_access;
3a5c3e22
PA
1995
1996 loc = allocate_bp_location (&b->base);
1997 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1998 ;
1999 *tmp = loc;
a6d9a66e 2000 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2001
2002 loc->pspace = frame_pspace;
a5606eee 2003 loc->address = addr;
bb9d5f81
PP
2004
2005 if (bitsize != 0)
2006 {
2007 /* Just cover the bytes that make up the bitfield. */
2008 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2009 }
2010 else
2011 loc->length = TYPE_LENGTH (value_type (v));
2012
a5606eee
VP
2013 loc->watchpoint_type = type;
2014 }
2015 }
9fa40276
TJB
2016 }
2017
2018 /* Change the type of breakpoint between hardware assisted or
2019 an ordinary watchpoint depending on the hardware support
2020 and free hardware slots. REPARSE is set when the inferior
2021 is started. */
a9634178 2022 if (reparse)
9fa40276 2023 {
e09342b5 2024 int reg_cnt;
9fa40276
TJB
2025 enum bp_loc_type loc_type;
2026 struct bp_location *bl;
a5606eee 2027
a9634178 2028 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2029
2030 if (reg_cnt)
9fa40276
TJB
2031 {
2032 int i, target_resources_ok, other_type_used;
a1398e0c 2033 enum bptype type;
9fa40276 2034
a9634178
TJB
2035 /* Use an exact watchpoint when there's only one memory region to be
2036 watched, and only one debug register is needed to watch it. */
2037 b->exact = target_exact_watchpoints && reg_cnt == 1;
2038
9fa40276 2039 /* We need to determine how many resources are already
e09342b5
TJB
2040 used for all other hardware watchpoints plus this one
2041 to see if we still have enough resources to also fit
a1398e0c
PA
2042 this watchpoint in as well. */
2043
2044 /* If this is a software watchpoint, we try to turn it
2045 to a hardware one -- count resources as if B was of
2046 hardware watchpoint type. */
2047 type = b->base.type;
2048 if (type == bp_watchpoint)
2049 type = bp_hardware_watchpoint;
2050
2051 /* This watchpoint may or may not have been placed on
2052 the list yet at this point (it won't be in the list
2053 if we're trying to create it for the first time,
2054 through watch_command), so always account for it
2055 manually. */
2056
2057 /* Count resources used by all watchpoints except B. */
2058 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2059
2060 /* Add in the resources needed for B. */
2061 i += hw_watchpoint_use_count (&b->base);
2062
2063 target_resources_ok
2064 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2065 if (target_resources_ok <= 0)
a9634178 2066 {
3a5c3e22 2067 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2068
2069 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2070 error (_("Target does not support this type of "
2071 "hardware watchpoint."));
9c06b0b4
TJB
2072 else if (target_resources_ok < 0 && !sw_mode)
2073 error (_("There are not enough available hardware "
2074 "resources for this watchpoint."));
a1398e0c
PA
2075
2076 /* Downgrade to software watchpoint. */
2077 b->base.type = bp_watchpoint;
2078 }
2079 else
2080 {
2081 /* If this was a software watchpoint, we've just
2082 found we have enough resources to turn it to a
2083 hardware watchpoint. Otherwise, this is a
2084 nop. */
2085 b->base.type = type;
a9634178 2086 }
9fa40276 2087 }
3a5c3e22 2088 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2089 {
2090 if (!can_use_hw_watchpoints)
2091 error (_("Can't set read/access watchpoint when "
2092 "hardware watchpoints are disabled."));
2093 else
2094 error (_("Expression cannot be implemented with "
2095 "read/access watchpoint."));
2096 }
9fa40276 2097 else
3a5c3e22 2098 b->base.type = bp_watchpoint;
9fa40276 2099
3a5c3e22 2100 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2101 : bp_loc_hardware_watchpoint);
3a5c3e22 2102 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2103 bl->loc_type = loc_type;
2104 }
2105
2106 for (v = val_chain; v; v = next)
2107 {
a5606eee
VP
2108 next = value_next (v);
2109 if (v != b->val)
2110 value_free (v);
2111 }
2112
c7437ca6
PA
2113 /* If a software watchpoint is not watching any memory, then the
2114 above left it without any location set up. But,
2115 bpstat_stop_status requires a location to be able to report
2116 stops, so make sure there's at least a dummy one. */
3a5c3e22 2117 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2118 {
3a5c3e22
PA
2119 struct breakpoint *base = &b->base;
2120 base->loc = allocate_bp_location (base);
2121 base->loc->pspace = frame_pspace;
2122 base->loc->address = -1;
2123 base->loc->length = -1;
2124 base->loc->watchpoint_type = -1;
c7437ca6 2125 }
a5606eee
VP
2126 }
2127 else if (!within_current_scope)
7270d8f2 2128 {
ac74f770
MS
2129 printf_filtered (_("\
2130Watchpoint %d deleted because the program has left the block\n\
2131in which its expression is valid.\n"),
3a5c3e22 2132 b->base.number);
d0fb5eae 2133 watchpoint_del_at_next_stop (b);
7270d8f2 2134 }
a5606eee
VP
2135
2136 /* Restore the selected frame. */
66076460
DJ
2137 if (frame_saved)
2138 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2139}
2140
a5606eee 2141
74960c60 2142/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2143 inserted in the inferior. We don't differentiate the type of BL's owner
2144 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2145 breakpoint_ops is not defined, because in insert_bp_location,
2146 tracepoint's insert_location will not be called. */
74960c60 2147static int
35df4500 2148should_be_inserted (struct bp_location *bl)
74960c60 2149{
35df4500 2150 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2151 return 0;
2152
35df4500 2153 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2154 return 0;
2155
35df4500 2156 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2157 return 0;
2158
f8eba3c6
TT
2159 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2160 return 0;
2161
56710373
PA
2162 /* This is set for example, when we're attached to the parent of a
2163 vfork, and have detached from the child. The child is running
2164 free, and we expect it to do an exec or exit, at which point the
2165 OS makes the parent schedulable again (and the target reports
2166 that the vfork is done). Until the child is done with the shared
2167 memory region, do not insert breakpoints in the parent, otherwise
2168 the child could still trip on the parent's breakpoints. Since
2169 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2170 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2171 return 0;
2172
31e77af2
PA
2173 /* Don't insert a breakpoint if we're trying to step past its
2174 location. */
2175 if ((bl->loc_type == bp_loc_software_breakpoint
2176 || bl->loc_type == bp_loc_hardware_breakpoint)
2177 && stepping_past_instruction_at (bl->pspace->aspace,
2178 bl->address))
e558d7c1
PA
2179 {
2180 if (debug_infrun)
2181 {
2182 fprintf_unfiltered (gdb_stdlog,
2183 "infrun: skipping breakpoint: "
2184 "stepping past insn at: %s\n",
2185 paddress (bl->gdbarch, bl->address));
2186 }
2187 return 0;
2188 }
31e77af2 2189
963f9c80
PA
2190 /* Don't insert watchpoints if we're trying to step past the
2191 instruction that triggered one. */
2192 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2193 && stepping_past_nonsteppable_watchpoint ())
2194 {
2195 if (debug_infrun)
2196 {
2197 fprintf_unfiltered (gdb_stdlog,
2198 "infrun: stepping past non-steppable watchpoint. "
2199 "skipping watchpoint at %s:%d\n",
2200 paddress (bl->gdbarch, bl->address),
2201 bl->length);
2202 }
2203 return 0;
2204 }
2205
74960c60
VP
2206 return 1;
2207}
2208
934709f0
PW
2209/* Same as should_be_inserted but does the check assuming
2210 that the location is not duplicated. */
2211
2212static int
2213unduplicated_should_be_inserted (struct bp_location *bl)
2214{
2215 int result;
2216 const int save_duplicate = bl->duplicate;
2217
2218 bl->duplicate = 0;
2219 result = should_be_inserted (bl);
2220 bl->duplicate = save_duplicate;
2221 return result;
2222}
2223
b775012e
LM
2224/* Parses a conditional described by an expression COND into an
2225 agent expression bytecode suitable for evaluation
2226 by the bytecode interpreter. Return NULL if there was
2227 any error during parsing. */
2228
2229static struct agent_expr *
2230parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2231{
2232 struct agent_expr *aexpr = NULL;
b775012e
LM
2233 volatile struct gdb_exception ex;
2234
2235 if (!cond)
2236 return NULL;
2237
2238 /* We don't want to stop processing, so catch any errors
2239 that may show up. */
2240 TRY_CATCH (ex, RETURN_MASK_ERROR)
2241 {
2242 aexpr = gen_eval_for_expr (scope, cond);
2243 }
2244
2245 if (ex.reason < 0)
2246 {
2247 /* If we got here, it means the condition could not be parsed to a valid
2248 bytecode expression and thus can't be evaluated on the target's side.
2249 It's no use iterating through the conditions. */
2250 return NULL;
2251 }
2252
2253 /* We have a valid agent expression. */
2254 return aexpr;
2255}
2256
2257/* Based on location BL, create a list of breakpoint conditions to be
2258 passed on to the target. If we have duplicated locations with different
2259 conditions, we will add such conditions to the list. The idea is that the
2260 target will evaluate the list of conditions and will only notify GDB when
2261 one of them is true. */
2262
2263static void
2264build_target_condition_list (struct bp_location *bl)
2265{
2266 struct bp_location **locp = NULL, **loc2p;
2267 int null_condition_or_parse_error = 0;
2268 int modified = bl->needs_update;
2269 struct bp_location *loc;
2270
8b4f3082
PA
2271 /* Release conditions left over from a previous insert. */
2272 VEC_free (agent_expr_p, bl->target_info.conditions);
2273
b775012e
LM
2274 /* This is only meaningful if the target is
2275 evaluating conditions and if the user has
2276 opted for condition evaluation on the target's
2277 side. */
2278 if (gdb_evaluates_breakpoint_condition_p ()
2279 || !target_supports_evaluation_of_breakpoint_conditions ())
2280 return;
2281
2282 /* Do a first pass to check for locations with no assigned
2283 conditions or conditions that fail to parse to a valid agent expression
2284 bytecode. If any of these happen, then it's no use to send conditions
2285 to the target since this location will always trigger and generate a
2286 response back to GDB. */
2287 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2288 {
2289 loc = (*loc2p);
2290 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2291 {
2292 if (modified)
2293 {
2294 struct agent_expr *aexpr;
2295
2296 /* Re-parse the conditions since something changed. In that
2297 case we already freed the condition bytecodes (see
2298 force_breakpoint_reinsertion). We just
2299 need to parse the condition to bytecodes again. */
2300 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2301 loc->cond_bytecode = aexpr;
2302
2303 /* Check if we managed to parse the conditional expression
2304 correctly. If not, we will not send this condition
2305 to the target. */
2306 if (aexpr)
2307 continue;
2308 }
2309
2310 /* If we have a NULL bytecode expression, it means something
2311 went wrong or we have a null condition expression. */
2312 if (!loc->cond_bytecode)
2313 {
2314 null_condition_or_parse_error = 1;
2315 break;
2316 }
2317 }
2318 }
2319
2320 /* If any of these happened, it means we will have to evaluate the conditions
2321 for the location's address on gdb's side. It is no use keeping bytecodes
2322 for all the other duplicate locations, thus we free all of them here.
2323
2324 This is so we have a finer control over which locations' conditions are
2325 being evaluated by GDB or the remote stub. */
2326 if (null_condition_or_parse_error)
2327 {
2328 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2329 {
2330 loc = (*loc2p);
2331 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2332 {
2333 /* Only go as far as the first NULL bytecode is
2334 located. */
2335 if (!loc->cond_bytecode)
2336 return;
2337
2338 free_agent_expr (loc->cond_bytecode);
2339 loc->cond_bytecode = NULL;
2340 }
2341 }
2342 }
2343
2344 /* No NULL conditions or failed bytecode generation. Build a condition list
2345 for this location's address. */
2346 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2347 {
2348 loc = (*loc2p);
2349 if (loc->cond
2350 && is_breakpoint (loc->owner)
2351 && loc->pspace->num == bl->pspace->num
2352 && loc->owner->enable_state == bp_enabled
2353 && loc->enabled)
2354 /* Add the condition to the vector. This will be used later to send the
2355 conditions to the target. */
2356 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2357 loc->cond_bytecode);
2358 }
2359
2360 return;
2361}
2362
d3ce09f5
SS
2363/* Parses a command described by string CMD into an agent expression
2364 bytecode suitable for evaluation by the bytecode interpreter.
2365 Return NULL if there was any error during parsing. */
2366
2367static struct agent_expr *
2368parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2369{
2370 struct cleanup *old_cleanups = 0;
2371 struct expression *expr, **argvec;
2372 struct agent_expr *aexpr = NULL;
d3ce09f5 2373 volatile struct gdb_exception ex;
bbc13ae3
KS
2374 const char *cmdrest;
2375 const char *format_start, *format_end;
d3ce09f5
SS
2376 struct format_piece *fpieces;
2377 int nargs;
2378 struct gdbarch *gdbarch = get_current_arch ();
2379
2380 if (!cmd)
2381 return NULL;
2382
2383 cmdrest = cmd;
2384
2385 if (*cmdrest == ',')
2386 ++cmdrest;
bbc13ae3 2387 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2388
2389 if (*cmdrest++ != '"')
2390 error (_("No format string following the location"));
2391
2392 format_start = cmdrest;
2393
2394 fpieces = parse_format_string (&cmdrest);
2395
2396 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2397
2398 format_end = cmdrest;
2399
2400 if (*cmdrest++ != '"')
2401 error (_("Bad format string, non-terminated '\"'."));
2402
bbc13ae3 2403 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2404
2405 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2406 error (_("Invalid argument syntax"));
2407
2408 if (*cmdrest == ',')
2409 cmdrest++;
bbc13ae3 2410 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2411
2412 /* For each argument, make an expression. */
2413
2414 argvec = (struct expression **) alloca (strlen (cmd)
2415 * sizeof (struct expression *));
2416
2417 nargs = 0;
2418 while (*cmdrest != '\0')
2419 {
bbc13ae3 2420 const char *cmd1;
d3ce09f5
SS
2421
2422 cmd1 = cmdrest;
2423 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2424 argvec[nargs++] = expr;
2425 cmdrest = cmd1;
2426 if (*cmdrest == ',')
2427 ++cmdrest;
2428 }
2429
2430 /* We don't want to stop processing, so catch any errors
2431 that may show up. */
2432 TRY_CATCH (ex, RETURN_MASK_ERROR)
2433 {
2434 aexpr = gen_printf (scope, gdbarch, 0, 0,
2435 format_start, format_end - format_start,
2436 fpieces, nargs, argvec);
2437 }
2438
752eb8b4
TT
2439 do_cleanups (old_cleanups);
2440
d3ce09f5
SS
2441 if (ex.reason < 0)
2442 {
2443 /* If we got here, it means the command could not be parsed to a valid
2444 bytecode expression and thus can't be evaluated on the target's side.
2445 It's no use iterating through the other commands. */
2446 return NULL;
2447 }
2448
d3ce09f5
SS
2449 /* We have a valid agent expression, return it. */
2450 return aexpr;
2451}
2452
2453/* Based on location BL, create a list of breakpoint commands to be
2454 passed on to the target. If we have duplicated locations with
2455 different commands, we will add any such to the list. */
2456
2457static void
2458build_target_command_list (struct bp_location *bl)
2459{
2460 struct bp_location **locp = NULL, **loc2p;
2461 int null_command_or_parse_error = 0;
2462 int modified = bl->needs_update;
2463 struct bp_location *loc;
2464
8b4f3082
PA
2465 /* Release commands left over from a previous insert. */
2466 VEC_free (agent_expr_p, bl->target_info.tcommands);
2467
41fac0cf 2468 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2469 return;
2470
41fac0cf
PA
2471 /* For now, limit to agent-style dprintf breakpoints. */
2472 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2473 return;
2474
41fac0cf
PA
2475 /* For now, if we have any duplicate location that isn't a dprintf,
2476 don't install the target-side commands, as that would make the
2477 breakpoint not be reported to the core, and we'd lose
2478 control. */
2479 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2480 {
2481 loc = (*loc2p);
2482 if (is_breakpoint (loc->owner)
2483 && loc->pspace->num == bl->pspace->num
2484 && loc->owner->type != bp_dprintf)
2485 return;
2486 }
2487
d3ce09f5
SS
2488 /* Do a first pass to check for locations with no assigned
2489 conditions or conditions that fail to parse to a valid agent expression
2490 bytecode. If any of these happen, then it's no use to send conditions
2491 to the target since this location will always trigger and generate a
2492 response back to GDB. */
2493 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2494 {
2495 loc = (*loc2p);
2496 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2497 {
2498 if (modified)
2499 {
2500 struct agent_expr *aexpr;
2501
2502 /* Re-parse the commands since something changed. In that
2503 case we already freed the command bytecodes (see
2504 force_breakpoint_reinsertion). We just
2505 need to parse the command to bytecodes again. */
2506 aexpr = parse_cmd_to_aexpr (bl->address,
2507 loc->owner->extra_string);
2508 loc->cmd_bytecode = aexpr;
2509
2510 if (!aexpr)
2511 continue;
2512 }
2513
2514 /* If we have a NULL bytecode expression, it means something
2515 went wrong or we have a null command expression. */
2516 if (!loc->cmd_bytecode)
2517 {
2518 null_command_or_parse_error = 1;
2519 break;
2520 }
2521 }
2522 }
2523
2524 /* If anything failed, then we're not doing target-side commands,
2525 and so clean up. */
2526 if (null_command_or_parse_error)
2527 {
2528 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2529 {
2530 loc = (*loc2p);
2531 if (is_breakpoint (loc->owner)
2532 && loc->pspace->num == bl->pspace->num)
2533 {
2534 /* Only go as far as the first NULL bytecode is
2535 located. */
40fb6c5e 2536 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2537 return;
2538
40fb6c5e
HZ
2539 free_agent_expr (loc->cmd_bytecode);
2540 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2541 }
2542 }
2543 }
2544
2545 /* No NULL commands or failed bytecode generation. Build a command list
2546 for this location's address. */
2547 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2548 {
2549 loc = (*loc2p);
2550 if (loc->owner->extra_string
2551 && is_breakpoint (loc->owner)
2552 && loc->pspace->num == bl->pspace->num
2553 && loc->owner->enable_state == bp_enabled
2554 && loc->enabled)
2555 /* Add the command to the vector. This will be used later
2556 to send the commands to the target. */
2557 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2558 loc->cmd_bytecode);
2559 }
2560
2561 bl->target_info.persist = 0;
2562 /* Maybe flag this location as persistent. */
2563 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2564 bl->target_info.persist = 1;
2565}
2566
35df4500
TJB
2567/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2568 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2569 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2570 Returns 0 for success, 1 if the bp_location type is not supported or
2571 -1 for failure.
879bfdc2 2572
4a64f543
MS
2573 NOTE drow/2003-09-09: This routine could be broken down to an
2574 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2575static int
35df4500 2576insert_bp_location (struct bp_location *bl,
26bb91f3 2577 struct ui_file *tmp_error_stream,
3fbb6ffa 2578 int *disabled_breaks,
dd61ec5c
MW
2579 int *hw_breakpoint_error,
2580 int *hw_bp_error_explained_already)
879bfdc2 2581{
0000e5cc
PA
2582 enum errors bp_err = GDB_NO_ERROR;
2583 const char *bp_err_message = NULL;
c90a6fb7 2584 volatile struct gdb_exception e;
879bfdc2 2585
b775012e 2586 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2587 return 0;
2588
35c63cd8
JB
2589 /* Note we don't initialize bl->target_info, as that wipes out
2590 the breakpoint location's shadow_contents if the breakpoint
2591 is still inserted at that location. This in turn breaks
2592 target_read_memory which depends on these buffers when
2593 a memory read is requested at the breakpoint location:
2594 Once the target_info has been wiped, we fail to see that
2595 we have a breakpoint inserted at that address and thus
2596 read the breakpoint instead of returning the data saved in
2597 the breakpoint location's shadow contents. */
0d5ed153 2598 bl->target_info.reqstd_address = bl->address;
35df4500 2599 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2600 bl->target_info.length = bl->length;
8181d85f 2601
b775012e
LM
2602 /* When working with target-side conditions, we must pass all the conditions
2603 for the same breakpoint address down to the target since GDB will not
2604 insert those locations. With a list of breakpoint conditions, the target
2605 can decide when to stop and notify GDB. */
2606
2607 if (is_breakpoint (bl->owner))
2608 {
2609 build_target_condition_list (bl);
d3ce09f5
SS
2610 build_target_command_list (bl);
2611 /* Reset the modification marker. */
b775012e
LM
2612 bl->needs_update = 0;
2613 }
2614
35df4500
TJB
2615 if (bl->loc_type == bp_loc_software_breakpoint
2616 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2617 {
35df4500 2618 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2619 {
2620 /* If the explicitly specified breakpoint type
2621 is not hardware breakpoint, check the memory map to see
2622 if the breakpoint address is in read only memory or not.
4a64f543 2623
765dc015
VP
2624 Two important cases are:
2625 - location type is not hardware breakpoint, memory
2626 is readonly. We change the type of the location to
2627 hardware breakpoint.
4a64f543
MS
2628 - location type is hardware breakpoint, memory is
2629 read-write. This means we've previously made the
2630 location hardware one, but then the memory map changed,
2631 so we undo.
765dc015 2632
4a64f543
MS
2633 When breakpoints are removed, remove_breakpoints will use
2634 location types we've just set here, the only possible
2635 problem is that memory map has changed during running
2636 program, but it's not going to work anyway with current
2637 gdb. */
765dc015 2638 struct mem_region *mr
0d5ed153 2639 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2640
2641 if (mr)
2642 {
2643 if (automatic_hardware_breakpoints)
2644 {
765dc015
VP
2645 enum bp_loc_type new_type;
2646
2647 if (mr->attrib.mode != MEM_RW)
2648 new_type = bp_loc_hardware_breakpoint;
2649 else
2650 new_type = bp_loc_software_breakpoint;
2651
35df4500 2652 if (new_type != bl->loc_type)
765dc015
VP
2653 {
2654 static int said = 0;
cc59ec59 2655
35df4500 2656 bl->loc_type = new_type;
765dc015
VP
2657 if (!said)
2658 {
3e43a32a
MS
2659 fprintf_filtered (gdb_stdout,
2660 _("Note: automatically using "
2661 "hardware breakpoints for "
2662 "read-only addresses.\n"));
765dc015
VP
2663 said = 1;
2664 }
2665 }
2666 }
35df4500 2667 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2668 && mr->attrib.mode != MEM_RW)
2669 {
2670 fprintf_unfiltered (tmp_error_stream,
2671 _("Cannot insert breakpoint %d.\n"
2672 "Cannot set software breakpoint "
2673 "at read-only address %s\n"),
2674 bl->owner->number,
2675 paddress (bl->gdbarch, bl->address));
2676 return 1;
2677 }
765dc015
VP
2678 }
2679 }
2680
879bfdc2
DJ
2681 /* First check to see if we have to handle an overlay. */
2682 if (overlay_debugging == ovly_off
35df4500
TJB
2683 || bl->section == NULL
2684 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2685 {
2686 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2687 TRY_CATCH (e, RETURN_MASK_ALL)
2688 {
0000e5cc
PA
2689 int val;
2690
dd61ec5c 2691 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2692 if (val)
2693 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2694 }
2695 if (e.reason < 0)
2696 {
0000e5cc
PA
2697 bp_err = e.error;
2698 bp_err_message = e.message;
dd61ec5c 2699 }
879bfdc2
DJ
2700 }
2701 else
2702 {
4a64f543 2703 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2704 Shall we set a breakpoint at the LMA? */
2705 if (!overlay_events_enabled)
2706 {
2707 /* Yes -- overlay event support is not active,
2708 so we must try to set a breakpoint at the LMA.
2709 This will not work for a hardware breakpoint. */
35df4500 2710 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2711 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2712 bl->owner->number);
879bfdc2
DJ
2713 else
2714 {
35df4500
TJB
2715 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2716 bl->section);
879bfdc2 2717 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2718 bl->overlay_target_info = bl->target_info;
0d5ed153 2719 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2720
2721 /* No overlay handling: just set the breakpoint. */
2722 TRY_CATCH (e, RETURN_MASK_ALL)
2723 {
2724 int val;
2725
2726 val = target_insert_breakpoint (bl->gdbarch,
2727 &bl->overlay_target_info);
2728 if (val)
2729 bp_err = GENERIC_ERROR;
2730 }
2731 if (e.reason < 0)
2732 {
2733 bp_err = e.error;
2734 bp_err_message = e.message;
2735 }
2736
2737 if (bp_err != GDB_NO_ERROR)
99361f52 2738 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2739 "Overlay breakpoint %d "
2740 "failed: in ROM?\n",
35df4500 2741 bl->owner->number);
879bfdc2
DJ
2742 }
2743 }
2744 /* Shall we set a breakpoint at the VMA? */
35df4500 2745 if (section_is_mapped (bl->section))
879bfdc2
DJ
2746 {
2747 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2748 TRY_CATCH (e, RETURN_MASK_ALL)
2749 {
0000e5cc
PA
2750 int val;
2751
dd61ec5c 2752 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2753 if (val)
2754 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2755 }
2756 if (e.reason < 0)
2757 {
0000e5cc
PA
2758 bp_err = e.error;
2759 bp_err_message = e.message;
dd61ec5c 2760 }
879bfdc2
DJ
2761 }
2762 else
2763 {
2764 /* No. This breakpoint will not be inserted.
2765 No error, but do not mark the bp as 'inserted'. */
2766 return 0;
2767 }
2768 }
2769
0000e5cc 2770 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2771 {
2772 /* Can't set the breakpoint. */
0000e5cc
PA
2773
2774 /* In some cases, we might not be able to insert a
2775 breakpoint in a shared library that has already been
2776 removed, but we have not yet processed the shlib unload
2777 event. Unfortunately, some targets that implement
076855f9
PA
2778 breakpoint insertion themselves can't tell why the
2779 breakpoint insertion failed (e.g., the remote target
2780 doesn't define error codes), so we must treat generic
2781 errors as memory errors. */
0000e5cc 2782 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2783 && bl->loc_type == bp_loc_software_breakpoint
08351840 2784 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2785 || shared_objfile_contains_address_p (bl->pspace,
2786 bl->address)))
879bfdc2 2787 {
4a64f543 2788 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2789 bl->shlib_disabled = 1;
8d3788bd 2790 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2791 if (!*disabled_breaks)
2792 {
2793 fprintf_unfiltered (tmp_error_stream,
2794 "Cannot insert breakpoint %d.\n",
2795 bl->owner->number);
2796 fprintf_unfiltered (tmp_error_stream,
2797 "Temporarily disabling shared "
2798 "library breakpoints:\n");
2799 }
2800 *disabled_breaks = 1;
879bfdc2 2801 fprintf_unfiltered (tmp_error_stream,
35df4500 2802 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2803 return 0;
879bfdc2
DJ
2804 }
2805 else
879bfdc2 2806 {
35df4500 2807 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2808 {
0000e5cc
PA
2809 *hw_breakpoint_error = 1;
2810 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2811 fprintf_unfiltered (tmp_error_stream,
2812 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2813 bl->owner->number, bp_err_message ? ":" : ".\n");
2814 if (bp_err_message != NULL)
2815 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2816 }
2817 else
2818 {
0000e5cc
PA
2819 if (bp_err_message == NULL)
2820 {
2821 char *message
2822 = memory_error_message (TARGET_XFER_E_IO,
2823 bl->gdbarch, bl->address);
2824 struct cleanup *old_chain = make_cleanup (xfree, message);
2825
2826 fprintf_unfiltered (tmp_error_stream,
2827 "Cannot insert breakpoint %d.\n"
2828 "%s\n",
2829 bl->owner->number, message);
2830 do_cleanups (old_chain);
2831 }
2832 else
2833 {
2834 fprintf_unfiltered (tmp_error_stream,
2835 "Cannot insert breakpoint %d: %s\n",
2836 bl->owner->number,
2837 bp_err_message);
2838 }
879bfdc2 2839 }
0000e5cc 2840 return 1;
879bfdc2
DJ
2841
2842 }
2843 }
2844 else
35df4500 2845 bl->inserted = 1;
879bfdc2 2846
0000e5cc 2847 return 0;
879bfdc2
DJ
2848 }
2849
35df4500 2850 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2851 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2852 watchpoints. It's not clear that it's necessary... */
35df4500 2853 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2854 {
0000e5cc
PA
2855 int val;
2856
77b06cd7
TJB
2857 gdb_assert (bl->owner->ops != NULL
2858 && bl->owner->ops->insert_location != NULL);
2859
2860 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2861
2862 /* If trying to set a read-watchpoint, and it turns out it's not
2863 supported, try emulating one with an access watchpoint. */
35df4500 2864 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2865 {
2866 struct bp_location *loc, **loc_temp;
2867
2868 /* But don't try to insert it, if there's already another
2869 hw_access location that would be considered a duplicate
2870 of this one. */
2871 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2872 if (loc != bl
85d721b8 2873 && loc->watchpoint_type == hw_access
35df4500 2874 && watchpoint_locations_match (bl, loc))
85d721b8 2875 {
35df4500
TJB
2876 bl->duplicate = 1;
2877 bl->inserted = 1;
2878 bl->target_info = loc->target_info;
2879 bl->watchpoint_type = hw_access;
85d721b8
PA
2880 val = 0;
2881 break;
2882 }
2883
2884 if (val == 1)
2885 {
77b06cd7
TJB
2886 bl->watchpoint_type = hw_access;
2887 val = bl->owner->ops->insert_location (bl);
2888
2889 if (val)
2890 /* Back to the original value. */
2891 bl->watchpoint_type = hw_read;
85d721b8
PA
2892 }
2893 }
2894
35df4500 2895 bl->inserted = (val == 0);
879bfdc2
DJ
2896 }
2897
35df4500 2898 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2899 {
0000e5cc
PA
2900 int val;
2901
77b06cd7
TJB
2902 gdb_assert (bl->owner->ops != NULL
2903 && bl->owner->ops->insert_location != NULL);
2904
2905 val = bl->owner->ops->insert_location (bl);
2906 if (val)
2907 {
2908 bl->owner->enable_state = bp_disabled;
2909
2910 if (val == 1)
2911 warning (_("\
2912Error inserting catchpoint %d: Your system does not support this type\n\
2913of catchpoint."), bl->owner->number);
2914 else
2915 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2916 }
2917
2918 bl->inserted = (val == 0);
1640b821
DJ
2919
2920 /* We've already printed an error message if there was a problem
2921 inserting this catchpoint, and we've disabled the catchpoint,
2922 so just return success. */
2923 return 0;
879bfdc2
DJ
2924 }
2925
2926 return 0;
2927}
2928
6c95b8df
PA
2929/* This function is called when program space PSPACE is about to be
2930 deleted. It takes care of updating breakpoints to not reference
2931 PSPACE anymore. */
2932
2933void
2934breakpoint_program_space_exit (struct program_space *pspace)
2935{
2936 struct breakpoint *b, *b_temp;
876fa593 2937 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2938
2939 /* Remove any breakpoint that was set through this program space. */
2940 ALL_BREAKPOINTS_SAFE (b, b_temp)
2941 {
2942 if (b->pspace == pspace)
2943 delete_breakpoint (b);
2944 }
2945
2946 /* Breakpoints set through other program spaces could have locations
2947 bound to PSPACE as well. Remove those. */
876fa593 2948 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2949 {
2950 struct bp_location *tmp;
2951
2952 if (loc->pspace == pspace)
2953 {
2bdf28a0 2954 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2955 if (loc->owner->loc == loc)
2956 loc->owner->loc = loc->next;
2957 else
2958 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2959 if (tmp->next == loc)
2960 {
2961 tmp->next = loc->next;
2962 break;
2963 }
2964 }
2965 }
2966
2967 /* Now update the global location list to permanently delete the
2968 removed locations above. */
44702360 2969 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2970}
2971
74960c60
VP
2972/* Make sure all breakpoints are inserted in inferior.
2973 Throws exception on any error.
2974 A breakpoint that is already inserted won't be inserted
2975 again, so calling this function twice is safe. */
2976void
2977insert_breakpoints (void)
2978{
2979 struct breakpoint *bpt;
2980
2981 ALL_BREAKPOINTS (bpt)
2982 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2983 {
2984 struct watchpoint *w = (struct watchpoint *) bpt;
2985
2986 update_watchpoint (w, 0 /* don't reparse. */);
2987 }
74960c60 2988
04086b45
PA
2989 /* Updating watchpoints creates new locations, so update the global
2990 location list. Explicitly tell ugll to insert locations and
2991 ignore breakpoints_always_inserted_mode. */
2992 update_global_location_list (UGLL_INSERT);
74960c60
VP
2993}
2994
20388dd6
YQ
2995/* Invoke CALLBACK for each of bp_location. */
2996
2997void
2998iterate_over_bp_locations (walk_bp_location_callback callback)
2999{
3000 struct bp_location *loc, **loc_tmp;
3001
3002 ALL_BP_LOCATIONS (loc, loc_tmp)
3003 {
3004 callback (loc, NULL);
3005 }
3006}
3007
b775012e
LM
3008/* This is used when we need to synch breakpoint conditions between GDB and the
3009 target. It is the case with deleting and disabling of breakpoints when using
3010 always-inserted mode. */
3011
3012static void
3013update_inserted_breakpoint_locations (void)
3014{
3015 struct bp_location *bl, **blp_tmp;
3016 int error_flag = 0;
3017 int val = 0;
3018 int disabled_breaks = 0;
3019 int hw_breakpoint_error = 0;
dd61ec5c 3020 int hw_bp_details_reported = 0;
b775012e
LM
3021
3022 struct ui_file *tmp_error_stream = mem_fileopen ();
3023 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3024
3025 /* Explicitly mark the warning -- this will only be printed if
3026 there was an error. */
3027 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3028
3029 save_current_space_and_thread ();
3030
3031 ALL_BP_LOCATIONS (bl, blp_tmp)
3032 {
3033 /* We only want to update software breakpoints and hardware
3034 breakpoints. */
3035 if (!is_breakpoint (bl->owner))
3036 continue;
3037
3038 /* We only want to update locations that are already inserted
3039 and need updating. This is to avoid unwanted insertion during
3040 deletion of breakpoints. */
3041 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3042 continue;
3043
3044 switch_to_program_space_and_thread (bl->pspace);
3045
3046 /* For targets that support global breakpoints, there's no need
3047 to select an inferior to insert breakpoint to. In fact, even
3048 if we aren't attached to any process yet, we should still
3049 insert breakpoints. */
f5656ead 3050 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3051 && ptid_equal (inferior_ptid, null_ptid))
3052 continue;
3053
3054 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3055 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3056 if (val)
3057 error_flag = val;
3058 }
3059
3060 if (error_flag)
3061 {
3062 target_terminal_ours_for_output ();
3063 error_stream (tmp_error_stream);
3064 }
3065
3066 do_cleanups (cleanups);
3067}
3068
c30eee59 3069/* Used when starting or continuing the program. */
c906108c 3070
74960c60
VP
3071static void
3072insert_breakpoint_locations (void)
c906108c 3073{
a5606eee 3074 struct breakpoint *bpt;
35df4500 3075 struct bp_location *bl, **blp_tmp;
eacd795a 3076 int error_flag = 0;
c906108c 3077 int val = 0;
3fbb6ffa 3078 int disabled_breaks = 0;
81d0cc19 3079 int hw_breakpoint_error = 0;
dd61ec5c 3080 int hw_bp_error_explained_already = 0;
c906108c 3081
81d0cc19 3082 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3083 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3084
81d0cc19
GS
3085 /* Explicitly mark the warning -- this will only be printed if
3086 there was an error. */
3087 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3088
3089 save_current_space_and_thread ();
3090
35df4500 3091 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3092 {
b775012e 3093 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3094 continue;
3095
4a64f543
MS
3096 /* There is no point inserting thread-specific breakpoints if
3097 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3098 has BL->OWNER always non-NULL. */
35df4500
TJB
3099 if (bl->owner->thread != -1
3100 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3101 continue;
3102
35df4500 3103 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3104
3105 /* For targets that support global breakpoints, there's no need
3106 to select an inferior to insert breakpoint to. In fact, even
3107 if we aren't attached to any process yet, we should still
3108 insert breakpoints. */
f5656ead 3109 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3110 && ptid_equal (inferior_ptid, null_ptid))
3111 continue;
3112
3fbb6ffa 3113 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3114 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3115 if (val)
eacd795a 3116 error_flag = val;
879bfdc2 3117 }
c906108c 3118
4a64f543
MS
3119 /* If we failed to insert all locations of a watchpoint, remove
3120 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3121 ALL_BREAKPOINTS (bpt)
3122 {
3123 int some_failed = 0;
3124 struct bp_location *loc;
3125
3126 if (!is_hardware_watchpoint (bpt))
3127 continue;
3128
d6b74ac4 3129 if (!breakpoint_enabled (bpt))
a5606eee 3130 continue;
74960c60
VP
3131
3132 if (bpt->disposition == disp_del_at_next_stop)
3133 continue;
a5606eee
VP
3134
3135 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3136 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3137 {
3138 some_failed = 1;
3139 break;
3140 }
3141 if (some_failed)
3142 {
3143 for (loc = bpt->loc; loc; loc = loc->next)
3144 if (loc->inserted)
3145 remove_breakpoint (loc, mark_uninserted);
3146
3147 hw_breakpoint_error = 1;
3148 fprintf_unfiltered (tmp_error_stream,
3149 "Could not insert hardware watchpoint %d.\n",
3150 bpt->number);
eacd795a 3151 error_flag = -1;
a5606eee
VP
3152 }
3153 }
3154
eacd795a 3155 if (error_flag)
81d0cc19
GS
3156 {
3157 /* If a hardware breakpoint or watchpoint was inserted, add a
3158 message about possibly exhausted resources. */
dd61ec5c 3159 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3160 {
c6510018
MS
3161 fprintf_unfiltered (tmp_error_stream,
3162 "Could not insert hardware breakpoints:\n\
3163You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3164 }
81d0cc19
GS
3165 target_terminal_ours_for_output ();
3166 error_stream (tmp_error_stream);
3167 }
f7545552
TT
3168
3169 do_cleanups (cleanups);
c906108c
SS
3170}
3171
c30eee59
TJB
3172/* Used when the program stops.
3173 Returns zero if successful, or non-zero if there was a problem
3174 removing a breakpoint location. */
3175
c906108c 3176int
fba45db2 3177remove_breakpoints (void)
c906108c 3178{
35df4500 3179 struct bp_location *bl, **blp_tmp;
3a1bae8e 3180 int val = 0;
c906108c 3181
35df4500 3182 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3183 {
1e4d1764 3184 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3185 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3186 }
3a1bae8e 3187 return val;
c906108c
SS
3188}
3189
49fa26b0
PA
3190/* When a thread exits, remove breakpoints that are related to
3191 that thread. */
3192
3193static void
3194remove_threaded_breakpoints (struct thread_info *tp, int silent)
3195{
3196 struct breakpoint *b, *b_tmp;
3197
3198 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3199 {
96181529 3200 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3201 {
3202 b->disposition = disp_del_at_next_stop;
3203
3204 printf_filtered (_("\
46ecd527 3205Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3206 b->number, tp->num);
3207
3208 /* Hide it from the user. */
3209 b->number = 0;
3210 }
3211 }
3212}
3213
6c95b8df
PA
3214/* Remove breakpoints of process PID. */
3215
3216int
3217remove_breakpoints_pid (int pid)
3218{
35df4500 3219 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3220 int val;
3221 struct inferior *inf = find_inferior_pid (pid);
3222
35df4500 3223 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3224 {
35df4500 3225 if (bl->pspace != inf->pspace)
6c95b8df
PA
3226 continue;
3227
d3ce09f5
SS
3228 if (bl->owner->type == bp_dprintf)
3229 continue;
3230
35df4500 3231 if (bl->inserted)
6c95b8df 3232 {
35df4500 3233 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3234 if (val != 0)
3235 return val;
3236 }
3237 }
3238 return 0;
3239}
3240
c906108c 3241int
fba45db2 3242reattach_breakpoints (int pid)
c906108c 3243{
6c95b8df 3244 struct cleanup *old_chain;
35df4500 3245 struct bp_location *bl, **blp_tmp;
c906108c 3246 int val;
86b887df 3247 struct ui_file *tmp_error_stream;
dd61ec5c 3248 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3249 struct inferior *inf;
3250 struct thread_info *tp;
3251
3252 tp = any_live_thread_of_process (pid);
3253 if (tp == NULL)
3254 return 1;
3255
3256 inf = find_inferior_pid (pid);
3257 old_chain = save_inferior_ptid ();
3258
3259 inferior_ptid = tp->ptid;
a4954f26 3260
86b887df 3261 tmp_error_stream = mem_fileopen ();
a4954f26 3262 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3263
35df4500 3264 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3265 {
35df4500 3266 if (bl->pspace != inf->pspace)
6c95b8df
PA
3267 continue;
3268
35df4500 3269 if (bl->inserted)
c5aa993b 3270 {
35df4500 3271 bl->inserted = 0;
dd61ec5c 3272 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3273 if (val != 0)
3274 {
ce696e05 3275 do_cleanups (old_chain);
c5aa993b
JM
3276 return val;
3277 }
3278 }
3279 }
ce696e05 3280 do_cleanups (old_chain);
c906108c
SS
3281 return 0;
3282}
3283
e58b0e63
PA
3284static int internal_breakpoint_number = -1;
3285
84f4c1fe
PM
3286/* Set the breakpoint number of B, depending on the value of INTERNAL.
3287 If INTERNAL is non-zero, the breakpoint number will be populated
3288 from internal_breakpoint_number and that variable decremented.
e5dd4106 3289 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3290 breakpoint_count and that value incremented. Internal breakpoints
3291 do not set the internal var bpnum. */
3292static void
3293set_breakpoint_number (int internal, struct breakpoint *b)
3294{
3295 if (internal)
3296 b->number = internal_breakpoint_number--;
3297 else
3298 {
3299 set_breakpoint_count (breakpoint_count + 1);
3300 b->number = breakpoint_count;
3301 }
3302}
3303
e62c965a 3304static struct breakpoint *
a6d9a66e 3305create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3306 CORE_ADDR address, enum bptype type,
c0a91b2b 3307 const struct breakpoint_ops *ops)
e62c965a 3308{
e62c965a
PP
3309 struct symtab_and_line sal;
3310 struct breakpoint *b;
3311
4a64f543 3312 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3313
3314 sal.pc = address;
3315 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3316 sal.pspace = current_program_space;
e62c965a 3317
06edf0c0 3318 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3319 b->number = internal_breakpoint_number--;
3320 b->disposition = disp_donttouch;
3321
3322 return b;
3323}
3324
17450429
PP
3325static const char *const longjmp_names[] =
3326 {
3327 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3328 };
3329#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3330
3331/* Per-objfile data private to breakpoint.c. */
3332struct breakpoint_objfile_data
3333{
3334 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3335 struct bound_minimal_symbol overlay_msym;
17450429
PP
3336
3337 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3338 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3339
28106bc2
SDJ
3340 /* True if we have looked for longjmp probes. */
3341 int longjmp_searched;
3342
3343 /* SystemTap probe points for longjmp (if any). */
3344 VEC (probe_p) *longjmp_probes;
3345
17450429 3346 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3347 struct bound_minimal_symbol terminate_msym;
17450429
PP
3348
3349 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3350 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3351
3352 /* True if we have looked for exception probes. */
3353 int exception_searched;
3354
3355 /* SystemTap probe points for unwinding (if any). */
3356 VEC (probe_p) *exception_probes;
17450429
PP
3357};
3358
3359static const struct objfile_data *breakpoint_objfile_key;
3360
3361/* Minimal symbol not found sentinel. */
3362static struct minimal_symbol msym_not_found;
3363
3364/* Returns TRUE if MSYM point to the "not found" sentinel. */
3365
3366static int
3367msym_not_found_p (const struct minimal_symbol *msym)
3368{
3369 return msym == &msym_not_found;
3370}
3371
3372/* Return per-objfile data needed by breakpoint.c.
3373 Allocate the data if necessary. */
3374
3375static struct breakpoint_objfile_data *
3376get_breakpoint_objfile_data (struct objfile *objfile)
3377{
3378 struct breakpoint_objfile_data *bp_objfile_data;
3379
3380 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3381 if (bp_objfile_data == NULL)
3382 {
3383 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3384 sizeof (*bp_objfile_data));
3385
3386 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3387 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3388 }
3389 return bp_objfile_data;
3390}
3391
28106bc2
SDJ
3392static void
3393free_breakpoint_probes (struct objfile *obj, void *data)
3394{
3395 struct breakpoint_objfile_data *bp_objfile_data = data;
3396
3397 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3398 VEC_free (probe_p, bp_objfile_data->exception_probes);
3399}
3400
e62c965a 3401static void
af02033e 3402create_overlay_event_breakpoint (void)
e62c965a 3403{
69de3c6a 3404 struct objfile *objfile;
af02033e 3405 const char *const func_name = "_ovly_debug_event";
e62c965a 3406
69de3c6a
PP
3407 ALL_OBJFILES (objfile)
3408 {
3409 struct breakpoint *b;
17450429
PP
3410 struct breakpoint_objfile_data *bp_objfile_data;
3411 CORE_ADDR addr;
69de3c6a 3412
17450429
PP
3413 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3414
3b7344d5 3415 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3416 continue;
3417
3b7344d5 3418 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3419 {
3b7344d5 3420 struct bound_minimal_symbol m;
17450429
PP
3421
3422 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3423 if (m.minsym == NULL)
17450429
PP
3424 {
3425 /* Avoid future lookups in this objfile. */
3b7344d5 3426 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3427 continue;
3428 }
3429 bp_objfile_data->overlay_msym = m;
3430 }
e62c965a 3431
77e371c0 3432 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3433 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3434 bp_overlay_event,
3435 &internal_breakpoint_ops);
69de3c6a 3436 b->addr_string = xstrdup (func_name);
e62c965a 3437
69de3c6a
PP
3438 if (overlay_debugging == ovly_auto)
3439 {
3440 b->enable_state = bp_enabled;
3441 overlay_events_enabled = 1;
3442 }
3443 else
3444 {
3445 b->enable_state = bp_disabled;
3446 overlay_events_enabled = 0;
3447 }
e62c965a 3448 }
44702360 3449 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3450}
3451
0fd8e87f 3452static void
af02033e 3453create_longjmp_master_breakpoint (void)
0fd8e87f 3454{
6c95b8df 3455 struct program_space *pspace;
6c95b8df
PA
3456 struct cleanup *old_chain;
3457
3458 old_chain = save_current_program_space ();
0fd8e87f 3459
6c95b8df 3460 ALL_PSPACES (pspace)
af02033e
PP
3461 {
3462 struct objfile *objfile;
3463
3464 set_current_program_space (pspace);
3465
3466 ALL_OBJFILES (objfile)
0fd8e87f 3467 {
af02033e
PP
3468 int i;
3469 struct gdbarch *gdbarch;
17450429 3470 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3471
af02033e 3472 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3473
17450429
PP
3474 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3475
28106bc2
SDJ
3476 if (!bp_objfile_data->longjmp_searched)
3477 {
25f9533e
SDJ
3478 VEC (probe_p) *ret;
3479
3480 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3481 if (ret != NULL)
3482 {
3483 /* We are only interested in checking one element. */
3484 struct probe *p = VEC_index (probe_p, ret, 0);
3485
3486 if (!can_evaluate_probe_arguments (p))
3487 {
3488 /* We cannot use the probe interface here, because it does
3489 not know how to evaluate arguments. */
3490 VEC_free (probe_p, ret);
3491 ret = NULL;
3492 }
3493 }
3494 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3495 bp_objfile_data->longjmp_searched = 1;
3496 }
3497
3498 if (bp_objfile_data->longjmp_probes != NULL)
3499 {
3500 int i;
3501 struct probe *probe;
3502 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3503
3504 for (i = 0;
3505 VEC_iterate (probe_p,
3506 bp_objfile_data->longjmp_probes,
3507 i, probe);
3508 ++i)
3509 {
3510 struct breakpoint *b;
3511
729662a5
TT
3512 b = create_internal_breakpoint (gdbarch,
3513 get_probe_address (probe,
3514 objfile),
28106bc2
SDJ
3515 bp_longjmp_master,
3516 &internal_breakpoint_ops);
3517 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3518 b->enable_state = bp_disabled;
3519 }
3520
3521 continue;
3522 }
3523
0569175e
TSD
3524 if (!gdbarch_get_longjmp_target_p (gdbarch))
3525 continue;
3526
17450429 3527 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3528 {
3529 struct breakpoint *b;
af02033e 3530 const char *func_name;
17450429 3531 CORE_ADDR addr;
6c95b8df 3532
3b7344d5 3533 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3534 continue;
0fd8e87f 3535
17450429 3536 func_name = longjmp_names[i];
3b7344d5 3537 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3538 {
3b7344d5 3539 struct bound_minimal_symbol m;
17450429
PP
3540
3541 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3542 if (m.minsym == NULL)
17450429
PP
3543 {
3544 /* Prevent future lookups in this objfile. */
3b7344d5 3545 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3546 continue;
3547 }
3548 bp_objfile_data->longjmp_msym[i] = m;
3549 }
3550
77e371c0 3551 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3552 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3553 &internal_breakpoint_ops);
af02033e
PP
3554 b->addr_string = xstrdup (func_name);
3555 b->enable_state = bp_disabled;
3556 }
0fd8e87f 3557 }
af02033e 3558 }
44702360 3559 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3560
3561 do_cleanups (old_chain);
0fd8e87f
UW
3562}
3563
af02033e 3564/* Create a master std::terminate breakpoint. */
aa7d318d 3565static void
af02033e 3566create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3567{
3568 struct program_space *pspace;
aa7d318d 3569 struct cleanup *old_chain;
af02033e 3570 const char *const func_name = "std::terminate()";
aa7d318d
TT
3571
3572 old_chain = save_current_program_space ();
3573
3574 ALL_PSPACES (pspace)
17450429
PP
3575 {
3576 struct objfile *objfile;
3577 CORE_ADDR addr;
3578
3579 set_current_program_space (pspace);
3580
aa7d318d
TT
3581 ALL_OBJFILES (objfile)
3582 {
3583 struct breakpoint *b;
17450429 3584 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3585
17450429 3586 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3587
3b7344d5 3588 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3589 continue;
3590
3b7344d5 3591 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3592 {
3b7344d5 3593 struct bound_minimal_symbol m;
17450429
PP
3594
3595 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3596 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3597 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3598 {
3599 /* Prevent future lookups in this objfile. */
3b7344d5 3600 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3601 continue;
3602 }
3603 bp_objfile_data->terminate_msym = m;
3604 }
aa7d318d 3605
77e371c0 3606 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3607 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3608 bp_std_terminate_master,
3609 &internal_breakpoint_ops);
aa7d318d
TT
3610 b->addr_string = xstrdup (func_name);
3611 b->enable_state = bp_disabled;
3612 }
17450429
PP
3613 }
3614
44702360 3615 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3616
3617 do_cleanups (old_chain);
3618}
3619
186c406b
TT
3620/* Install a master breakpoint on the unwinder's debug hook. */
3621
70221824 3622static void
186c406b
TT
3623create_exception_master_breakpoint (void)
3624{
3625 struct objfile *objfile;
17450429 3626 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3627
3628 ALL_OBJFILES (objfile)
3629 {
17450429
PP
3630 struct breakpoint *b;
3631 struct gdbarch *gdbarch;
3632 struct breakpoint_objfile_data *bp_objfile_data;
3633 CORE_ADDR addr;
3634
3635 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3636
28106bc2
SDJ
3637 /* We prefer the SystemTap probe point if it exists. */
3638 if (!bp_objfile_data->exception_searched)
3639 {
25f9533e
SDJ
3640 VEC (probe_p) *ret;
3641
3642 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3643
3644 if (ret != NULL)
3645 {
3646 /* We are only interested in checking one element. */
3647 struct probe *p = VEC_index (probe_p, ret, 0);
3648
3649 if (!can_evaluate_probe_arguments (p))
3650 {
3651 /* We cannot use the probe interface here, because it does
3652 not know how to evaluate arguments. */
3653 VEC_free (probe_p, ret);
3654 ret = NULL;
3655 }
3656 }
3657 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3658 bp_objfile_data->exception_searched = 1;
3659 }
3660
3661 if (bp_objfile_data->exception_probes != NULL)
3662 {
3663 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3664 int i;
3665 struct probe *probe;
3666
3667 for (i = 0;
3668 VEC_iterate (probe_p,
3669 bp_objfile_data->exception_probes,
3670 i, probe);
3671 ++i)
3672 {
3673 struct breakpoint *b;
3674
729662a5
TT
3675 b = create_internal_breakpoint (gdbarch,
3676 get_probe_address (probe,
3677 objfile),
28106bc2
SDJ
3678 bp_exception_master,
3679 &internal_breakpoint_ops);
3680 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3681 b->enable_state = bp_disabled;
3682 }
3683
3684 continue;
3685 }
3686
3687 /* Otherwise, try the hook function. */
3688
3b7344d5 3689 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3690 continue;
3691
3692 gdbarch = get_objfile_arch (objfile);
186c406b 3693
3b7344d5 3694 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3695 {
3b7344d5 3696 struct bound_minimal_symbol debug_hook;
186c406b 3697
17450429 3698 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3699 if (debug_hook.minsym == NULL)
17450429 3700 {
3b7344d5 3701 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3702 continue;
3703 }
3704
3705 bp_objfile_data->exception_msym = debug_hook;
186c406b 3706 }
17450429 3707
77e371c0 3708 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3709 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3710 &current_target);
06edf0c0
PA
3711 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3712 &internal_breakpoint_ops);
17450429
PP
3713 b->addr_string = xstrdup (func_name);
3714 b->enable_state = bp_disabled;
186c406b
TT
3715 }
3716
44702360 3717 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3718}
3719
c906108c 3720void
fba45db2 3721update_breakpoints_after_exec (void)
c906108c 3722{
35df4500 3723 struct breakpoint *b, *b_tmp;
876fa593 3724 struct bp_location *bploc, **bplocp_tmp;
c906108c 3725
25b22b0a
PA
3726 /* We're about to delete breakpoints from GDB's lists. If the
3727 INSERTED flag is true, GDB will try to lift the breakpoints by
3728 writing the breakpoints' "shadow contents" back into memory. The
3729 "shadow contents" are NOT valid after an exec, so GDB should not
3730 do that. Instead, the target is responsible from marking
3731 breakpoints out as soon as it detects an exec. We don't do that
3732 here instead, because there may be other attempts to delete
3733 breakpoints after detecting an exec and before reaching here. */
876fa593 3734 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3735 if (bploc->pspace == current_program_space)
3736 gdb_assert (!bploc->inserted);
c906108c 3737
35df4500 3738 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3739 {
6c95b8df
PA
3740 if (b->pspace != current_program_space)
3741 continue;
3742
4a64f543 3743 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3744 if (b->type == bp_shlib_event)
3745 {
3746 delete_breakpoint (b);
3747 continue;
3748 }
c906108c 3749
4a64f543 3750 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3751 if (b->type == bp_jit_event)
3752 {
3753 delete_breakpoint (b);
3754 continue;
3755 }
3756
1900040c 3757 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3758 as must overlay event and longjmp master breakpoints. */
3759 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3760 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3761 || b->type == bp_exception_master)
c4093a6a
JM
3762 {
3763 delete_breakpoint (b);
3764 continue;
3765 }
3766
4a64f543 3767 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3768 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3769 {
3770 delete_breakpoint (b);
3771 continue;
3772 }
3773
7c16b83e
PA
3774 /* Just like single-step breakpoints. */
3775 if (b->type == bp_single_step)
3776 {
3777 delete_breakpoint (b);
3778 continue;
3779 }
3780
611c83ae
PA
3781 /* Longjmp and longjmp-resume breakpoints are also meaningless
3782 after an exec. */
186c406b 3783 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3784 || b->type == bp_longjmp_call_dummy
186c406b 3785 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3786 {
3787 delete_breakpoint (b);
3788 continue;
3789 }
3790
ce78b96d
JB
3791 if (b->type == bp_catchpoint)
3792 {
3793 /* For now, none of the bp_catchpoint breakpoints need to
3794 do anything at this point. In the future, if some of
3795 the catchpoints need to something, we will need to add
3796 a new method, and call this method from here. */
3797 continue;
3798 }
3799
c5aa993b
JM
3800 /* bp_finish is a special case. The only way we ought to be able
3801 to see one of these when an exec() has happened, is if the user
3802 caught a vfork, and then said "finish". Ordinarily a finish just
3803 carries them to the call-site of the current callee, by setting
3804 a temporary bp there and resuming. But in this case, the finish
3805 will carry them entirely through the vfork & exec.
3806
3807 We don't want to allow a bp_finish to remain inserted now. But
3808 we can't safely delete it, 'cause finish_command has a handle to
3809 the bp on a bpstat, and will later want to delete it. There's a
3810 chance (and I've seen it happen) that if we delete the bp_finish
3811 here, that its storage will get reused by the time finish_command
3812 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3813 We really must allow finish_command to delete a bp_finish.
3814
e5dd4106 3815 In the absence of a general solution for the "how do we know
53a5351d
JM
3816 it's safe to delete something others may have handles to?"
3817 problem, what we'll do here is just uninsert the bp_finish, and
3818 let finish_command delete it.
3819
3820 (We know the bp_finish is "doomed" in the sense that it's
3821 momentary, and will be deleted as soon as finish_command sees
3822 the inferior stopped. So it doesn't matter that the bp's
3823 address is probably bogus in the new a.out, unlike e.g., the
3824 solib breakpoints.) */
c5aa993b 3825
c5aa993b
JM
3826 if (b->type == bp_finish)
3827 {
3828 continue;
3829 }
3830
3831 /* Without a symbolic address, we have little hope of the
3832 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3833 a.out. */
c5aa993b
JM
3834 if (b->addr_string == NULL)
3835 {
3836 delete_breakpoint (b);
3837 continue;
3838 }
c5aa993b 3839 }
c906108c
SS
3840}
3841
3842int
d80ee84f 3843detach_breakpoints (ptid_t ptid)
c906108c 3844{
35df4500 3845 struct bp_location *bl, **blp_tmp;
3a1bae8e 3846 int val = 0;
ce696e05 3847 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3848 struct inferior *inf = current_inferior ();
c5aa993b 3849
dfd4cc63 3850 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3851 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3852
6c95b8df 3853 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3854 inferior_ptid = ptid;
35df4500 3855 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3856 {
35df4500 3857 if (bl->pspace != inf->pspace)
6c95b8df
PA
3858 continue;
3859
bd9673a4
PW
3860 /* This function must physically remove breakpoints locations
3861 from the specified ptid, without modifying the breakpoint
3862 package's state. Locations of type bp_loc_other are only
3863 maintained at GDB side. So, there is no need to remove
3864 these bp_loc_other locations. Moreover, removing these
3865 would modify the breakpoint package's state. */
3866 if (bl->loc_type == bp_loc_other)
3867 continue;
3868
35df4500
TJB
3869 if (bl->inserted)
3870 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3871 }
d03285ec 3872
ce696e05 3873 do_cleanups (old_chain);
3a1bae8e 3874 return val;
c906108c
SS
3875}
3876
35df4500 3877/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3878 Note that this is used to detach breakpoints from a child fork.
3879 When we get here, the child isn't in the inferior list, and neither
3880 do we have objects to represent its address space --- we should
35df4500 3881 *not* look at bl->pspace->aspace here. */
6c95b8df 3882
c906108c 3883static int
35df4500 3884remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3885{
3886 int val;
c5aa993b 3887
35df4500
TJB
3888 /* BL is never in moribund_locations by our callers. */
3889 gdb_assert (bl->owner != NULL);
2bdf28a0 3890
35df4500 3891 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3892 /* Permanent breakpoints cannot be inserted or removed. */
3893 return 0;
3894
74960c60
VP
3895 /* The type of none suggests that owner is actually deleted.
3896 This should not ever happen. */
35df4500 3897 gdb_assert (bl->owner->type != bp_none);
0bde7532 3898
35df4500
TJB
3899 if (bl->loc_type == bp_loc_software_breakpoint
3900 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3901 {
c02f5703
MS
3902 /* "Normal" instruction breakpoint: either the standard
3903 trap-instruction bp (bp_breakpoint), or a
3904 bp_hardware_breakpoint. */
3905
3906 /* First check to see if we have to handle an overlay. */
3907 if (overlay_debugging == ovly_off
35df4500
TJB
3908 || bl->section == NULL
3909 || !(section_is_overlay (bl->section)))
c02f5703
MS
3910 {
3911 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3912
3913 /* If we're trying to uninsert a memory breakpoint that we
3914 know is set in a dynamic object that is marked
3915 shlib_disabled, then either the dynamic object was
3916 removed with "remove-symbol-file" or with
3917 "nosharedlibrary". In the former case, we don't know
3918 whether another dynamic object might have loaded over the
3919 breakpoint's address -- the user might well let us know
3920 about it next with add-symbol-file (the whole point of
d03de421 3921 add-symbol-file is letting the user manually maintain a
08351840
PA
3922 list of dynamically loaded objects). If we have the
3923 breakpoint's shadow memory, that is, this is a software
3924 breakpoint managed by GDB, check whether the breakpoint
3925 is still inserted in memory, to avoid overwriting wrong
3926 code with stale saved shadow contents. Note that HW
3927 breakpoints don't have shadow memory, as they're
3928 implemented using a mechanism that is not dependent on
3929 being able to modify the target's memory, and as such
3930 they should always be removed. */
3931 if (bl->shlib_disabled
3932 && bl->target_info.shadow_len != 0
3933 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3934 val = 0;
3935 else
3936 val = bl->owner->ops->remove_location (bl);
c02f5703 3937 }
c906108c
SS
3938 else
3939 {
4a64f543 3940 /* This breakpoint is in an overlay section.
c02f5703
MS
3941 Did we set a breakpoint at the LMA? */
3942 if (!overlay_events_enabled)
3943 {
3944 /* Yes -- overlay event support is not active, so we
3945 should have set a breakpoint at the LMA. Remove it.
3946 */
c02f5703
MS
3947 /* Ignore any failures: if the LMA is in ROM, we will
3948 have already warned when we failed to insert it. */
35df4500
TJB
3949 if (bl->loc_type == bp_loc_hardware_breakpoint)
3950 target_remove_hw_breakpoint (bl->gdbarch,
3951 &bl->overlay_target_info);
c02f5703 3952 else
35df4500
TJB
3953 target_remove_breakpoint (bl->gdbarch,
3954 &bl->overlay_target_info);
c02f5703
MS
3955 }
3956 /* Did we set a breakpoint at the VMA?
3957 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3958 if (bl->inserted)
c906108c 3959 {
c02f5703
MS
3960 /* Yes -- remove it. Previously we did not bother to
3961 remove the breakpoint if the section had been
3962 unmapped, but let's not rely on that being safe. We
3963 don't know what the overlay manager might do. */
aa67235e
UW
3964
3965 /* However, we should remove *software* breakpoints only
3966 if the section is still mapped, or else we overwrite
3967 wrong code with the saved shadow contents. */
348d480f
PA
3968 if (bl->loc_type == bp_loc_hardware_breakpoint
3969 || section_is_mapped (bl->section))
3970 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3971 else
3972 val = 0;
c906108c 3973 }
c02f5703
MS
3974 else
3975 {
3976 /* No -- not inserted, so no need to remove. No error. */
3977 val = 0;
3978 }
c906108c 3979 }
879d1e6b 3980
08351840
PA
3981 /* In some cases, we might not be able to remove a breakpoint in
3982 a shared library that has already been removed, but we have
3983 not yet processed the shlib unload event. Similarly for an
3984 unloaded add-symbol-file object - the user might not yet have
3985 had the chance to remove-symbol-file it. shlib_disabled will
3986 be set if the library/object has already been removed, but
3987 the breakpoint hasn't been uninserted yet, e.g., after
3988 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3989 always-inserted mode. */
076855f9 3990 if (val
08351840
PA
3991 && (bl->loc_type == bp_loc_software_breakpoint
3992 && (bl->shlib_disabled
3993 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3994 || shared_objfile_contains_address_p (bl->pspace,
3995 bl->address))))
879d1e6b
UW
3996 val = 0;
3997
c906108c
SS
3998 if (val)
3999 return val;
35df4500 4000 bl->inserted = (is == mark_inserted);
c906108c 4001 }
35df4500 4002 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4003 {
77b06cd7
TJB
4004 gdb_assert (bl->owner->ops != NULL
4005 && bl->owner->ops->remove_location != NULL);
4006
35df4500 4007 bl->inserted = (is == mark_inserted);
77b06cd7 4008 bl->owner->ops->remove_location (bl);
2e70b7b9 4009
c906108c 4010 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4011 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4012 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4013 bl->owner->number);
c906108c 4014 }
35df4500
TJB
4015 else if (bl->owner->type == bp_catchpoint
4016 && breakpoint_enabled (bl->owner)
4017 && !bl->duplicate)
ce78b96d 4018 {
77b06cd7
TJB
4019 gdb_assert (bl->owner->ops != NULL
4020 && bl->owner->ops->remove_location != NULL);
ce78b96d 4021
77b06cd7 4022 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4023 if (val)
4024 return val;
77b06cd7 4025
35df4500 4026 bl->inserted = (is == mark_inserted);
ce78b96d 4027 }
c906108c
SS
4028
4029 return 0;
4030}
4031
6c95b8df 4032static int
35df4500 4033remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4034{
4035 int ret;
4036 struct cleanup *old_chain;
4037
35df4500
TJB
4038 /* BL is never in moribund_locations by our callers. */
4039 gdb_assert (bl->owner != NULL);
2bdf28a0 4040
35df4500 4041 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
4042 /* Permanent breakpoints cannot be inserted or removed. */
4043 return 0;
4044
4045 /* The type of none suggests that owner is actually deleted.
4046 This should not ever happen. */
35df4500 4047 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4048
4049 old_chain = save_current_space_and_thread ();
4050
35df4500 4051 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4052
35df4500 4053 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4054
4055 do_cleanups (old_chain);
4056 return ret;
4057}
4058
c906108c
SS
4059/* Clear the "inserted" flag in all breakpoints. */
4060
25b22b0a 4061void
fba45db2 4062mark_breakpoints_out (void)
c906108c 4063{
35df4500 4064 struct bp_location *bl, **blp_tmp;
c906108c 4065
35df4500
TJB
4066 ALL_BP_LOCATIONS (bl, blp_tmp)
4067 if (bl->pspace == current_program_space)
4068 bl->inserted = 0;
c906108c
SS
4069}
4070
53a5351d
JM
4071/* Clear the "inserted" flag in all breakpoints and delete any
4072 breakpoints which should go away between runs of the program.
c906108c
SS
4073
4074 Plus other such housekeeping that has to be done for breakpoints
4075 between runs.
4076
53a5351d
JM
4077 Note: this function gets called at the end of a run (by
4078 generic_mourn_inferior) and when a run begins (by
4a64f543 4079 init_wait_for_inferior). */
c906108c
SS
4080
4081
4082
4083void
fba45db2 4084breakpoint_init_inferior (enum inf_context context)
c906108c 4085{
35df4500
TJB
4086 struct breakpoint *b, *b_tmp;
4087 struct bp_location *bl, **blp_tmp;
1c5cfe86 4088 int ix;
6c95b8df 4089 struct program_space *pspace = current_program_space;
c906108c 4090
50c71eaf
PA
4091 /* If breakpoint locations are shared across processes, then there's
4092 nothing to do. */
f5656ead 4093 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4094 return;
4095
35df4500 4096 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 4097 {
35df4500
TJB
4098 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4099 if (bl->pspace == pspace
4100 && bl->owner->enable_state != bp_permanent)
4101 bl->inserted = 0;
6c95b8df 4102 }
075f6582 4103
35df4500 4104 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4105 {
6c95b8df
PA
4106 if (b->loc && b->loc->pspace != pspace)
4107 continue;
4108
c5aa993b
JM
4109 switch (b->type)
4110 {
4111 case bp_call_dummy:
e2e4d78b 4112 case bp_longjmp_call_dummy:
c906108c 4113
c5aa993b 4114 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4115 cause problems when the inferior is rerun, so we better get
4116 rid of it. */
4117
4118 case bp_watchpoint_scope:
4119
4120 /* Also get rid of scope breakpoints. */
4121
4122 case bp_shlib_event:
4123
4124 /* Also remove solib event breakpoints. Their addresses may
4125 have changed since the last time we ran the program.
4126 Actually we may now be debugging against different target;
4127 and so the solib backend that installed this breakpoint may
4128 not be used in by the target. E.g.,
4129
4130 (gdb) file prog-linux
4131 (gdb) run # native linux target
4132 ...
4133 (gdb) kill
4134 (gdb) file prog-win.exe
4135 (gdb) tar rem :9999 # remote Windows gdbserver.
4136 */
c906108c 4137
f59f708a
PA
4138 case bp_step_resume:
4139
4140 /* Also remove step-resume breakpoints. */
4141
7c16b83e
PA
4142 case bp_single_step:
4143
4144 /* Also remove single-step breakpoints. */
4145
c5aa993b
JM
4146 delete_breakpoint (b);
4147 break;
c906108c 4148
c5aa993b
JM
4149 case bp_watchpoint:
4150 case bp_hardware_watchpoint:
4151 case bp_read_watchpoint:
4152 case bp_access_watchpoint:
3a5c3e22
PA
4153 {
4154 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4155
3a5c3e22
PA
4156 /* Likewise for watchpoints on local expressions. */
4157 if (w->exp_valid_block != NULL)
4158 delete_breakpoint (b);
4159 else if (context == inf_starting)
4160 {
4161 /* Reset val field to force reread of starting value in
4162 insert_breakpoints. */
4163 if (w->val)
4164 value_free (w->val);
4165 w->val = NULL;
4166 w->val_valid = 0;
c860120c 4167 }
3a5c3e22 4168 }
c5aa993b
JM
4169 break;
4170 default:
c5aa993b
JM
4171 break;
4172 }
4173 }
1c5cfe86
PA
4174
4175 /* Get rid of the moribund locations. */
35df4500
TJB
4176 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4177 decref_bp_location (&bl);
1c5cfe86 4178 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4179}
4180
6c95b8df
PA
4181/* These functions concern about actual breakpoints inserted in the
4182 target --- to e.g. check if we need to do decr_pc adjustment or if
4183 we need to hop over the bkpt --- so we check for address space
4184 match, not program space. */
4185
c2c6d25f
JM
4186/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4187 exists at PC. It returns ordinary_breakpoint_here if it's an
4188 ordinary breakpoint, or permanent_breakpoint_here if it's a
4189 permanent breakpoint.
4190 - When continuing from a location with an ordinary breakpoint, we
4191 actually single step once before calling insert_breakpoints.
e5dd4106 4192 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4193 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4194 the target, to advance the PC past the breakpoint. */
c906108c 4195
c2c6d25f 4196enum breakpoint_here
6c95b8df 4197breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4198{
35df4500 4199 struct bp_location *bl, **blp_tmp;
c2c6d25f 4200 int any_breakpoint_here = 0;
c906108c 4201
35df4500 4202 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4203 {
35df4500
TJB
4204 if (bl->loc_type != bp_loc_software_breakpoint
4205 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4206 continue;
4207
f1310107 4208 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
4209 if ((breakpoint_enabled (bl->owner)
4210 || bl->owner->enable_state == bp_permanent)
f1310107 4211 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4212 {
4213 if (overlay_debugging
35df4500
TJB
4214 && section_is_overlay (bl->section)
4215 && !section_is_mapped (bl->section))
075f6582 4216 continue; /* unmapped overlay -- can't be a match */
35df4500 4217 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
4218 return permanent_breakpoint_here;
4219 else
4220 any_breakpoint_here = 1;
4221 }
4222 }
c906108c 4223
c2c6d25f 4224 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4225}
4226
1c5cfe86
PA
4227/* Return true if there's a moribund breakpoint at PC. */
4228
4229int
6c95b8df 4230moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4231{
4232 struct bp_location *loc;
4233 int ix;
4234
4235 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4236 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4237 return 1;
4238
4239 return 0;
4240}
c2c6d25f 4241
c36b740a 4242/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
4243 inserted using regular breakpoint_chain / bp_location array
4244 mechanism. This does not check for single-step breakpoints, which
4245 are inserted and removed using direct target manipulation. */
c906108c
SS
4246
4247int
4a64f543
MS
4248regular_breakpoint_inserted_here_p (struct address_space *aspace,
4249 CORE_ADDR pc)
c906108c 4250{
35df4500 4251 struct bp_location *bl, **blp_tmp;
c906108c 4252
35df4500 4253 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4254 {
35df4500
TJB
4255 if (bl->loc_type != bp_loc_software_breakpoint
4256 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4257 continue;
4258
35df4500 4259 if (bl->inserted
f1310107 4260 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4261 {
4262 if (overlay_debugging
35df4500
TJB
4263 && section_is_overlay (bl->section)
4264 && !section_is_mapped (bl->section))
075f6582
DJ
4265 continue; /* unmapped overlay -- can't be a match */
4266 else
4267 return 1;
4268 }
c5aa993b 4269 }
c36b740a
VP
4270 return 0;
4271}
4272
4273/* Returns non-zero iff there's either regular breakpoint
4274 or a single step breakpoint inserted at PC. */
4275
4276int
6c95b8df 4277breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 4278{
6c95b8df 4279 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 4280 return 1;
c906108c 4281
6c95b8df 4282 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4283 return 1;
4284
c906108c
SS
4285 return 0;
4286}
4287
ef370185
JB
4288/* Ignoring deprecated raw breakpoints, return non-zero iff there is a
4289 software breakpoint inserted at PC. */
4fa8626c 4290
ef370185
JB
4291static struct bp_location *
4292find_non_raw_software_breakpoint_inserted_here (struct address_space *aspace,
4293 CORE_ADDR pc)
4fa8626c 4294{
35df4500 4295 struct bp_location *bl, **blp_tmp;
4fa8626c 4296
35df4500 4297 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 4298 {
35df4500 4299 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4300 continue;
4301
35df4500
TJB
4302 if (bl->inserted
4303 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 4304 aspace, pc))
4fa8626c
DJ
4305 {
4306 if (overlay_debugging
35df4500
TJB
4307 && section_is_overlay (bl->section)
4308 && !section_is_mapped (bl->section))
4fa8626c
DJ
4309 continue; /* unmapped overlay -- can't be a match */
4310 else
ef370185 4311 return bl;
4fa8626c
DJ
4312 }
4313 }
4314
ef370185
JB
4315 return NULL;
4316}
4317
4318/* This function returns non-zero iff there is a software breakpoint
4319 inserted at PC. */
4320
4321int
4322software_breakpoint_inserted_here_p (struct address_space *aspace,
4323 CORE_ADDR pc)
4324{
4325 if (find_non_raw_software_breakpoint_inserted_here (aspace, pc) != NULL)
4326 return 1;
4327
1aafd4da 4328 /* Also check for software single-step breakpoints. */
6c95b8df 4329 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4330 return 1;
4331
4fa8626c
DJ
4332 return 0;
4333}
4334
9093389c
PA
4335int
4336hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4337 CORE_ADDR addr, ULONGEST len)
4338{
4339 struct breakpoint *bpt;
4340
4341 ALL_BREAKPOINTS (bpt)
4342 {
4343 struct bp_location *loc;
4344
4345 if (bpt->type != bp_hardware_watchpoint
4346 && bpt->type != bp_access_watchpoint)
4347 continue;
4348
4349 if (!breakpoint_enabled (bpt))
4350 continue;
4351
4352 for (loc = bpt->loc; loc; loc = loc->next)
4353 if (loc->pspace->aspace == aspace && loc->inserted)
4354 {
4355 CORE_ADDR l, h;
4356
4357 /* Check for intersection. */
4358 l = max (loc->address, addr);
4359 h = min (loc->address + loc->length, addr + len);
4360 if (l < h)
4361 return 1;
4362 }
4363 }
4364 return 0;
4365}
4366
075f6582
DJ
4367/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4368 PC is valid for process/thread PTID. */
c906108c
SS
4369
4370int
6c95b8df
PA
4371breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4372 ptid_t ptid)
c906108c 4373{
35df4500 4374 struct bp_location *bl, **blp_tmp;
4a306c9a 4375 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 4376 int thread = -1;
4a306c9a 4377 int task = 0;
a6f1cd96 4378
35df4500 4379 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4380 {
35df4500
TJB
4381 if (bl->loc_type != bp_loc_software_breakpoint
4382 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4383 continue;
4384
35df4500
TJB
4385 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4386 if (!breakpoint_enabled (bl->owner)
4387 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
4388 continue;
4389
f1310107 4390 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
4391 continue;
4392
35df4500 4393 if (bl->owner->thread != -1)
075f6582 4394 {
a6f1cd96
JB
4395 /* This is a thread-specific breakpoint. Check that ptid
4396 matches that thread. If thread hasn't been computed yet,
4397 it is now time to do so. */
4398 if (thread == -1)
4399 thread = pid_to_thread_id (ptid);
35df4500 4400 if (bl->owner->thread != thread)
a6f1cd96 4401 continue;
075f6582 4402 }
a6f1cd96 4403
35df4500 4404 if (bl->owner->task != 0)
4a306c9a
JB
4405 {
4406 /* This is a task-specific breakpoint. Check that ptid
4407 matches that task. If task hasn't been computed yet,
4408 it is now time to do so. */
4409 if (task == 0)
4410 task = ada_get_task_number (ptid);
35df4500 4411 if (bl->owner->task != task)
4a306c9a
JB
4412 continue;
4413 }
4414
a6f1cd96 4415 if (overlay_debugging
35df4500
TJB
4416 && section_is_overlay (bl->section)
4417 && !section_is_mapped (bl->section))
a6f1cd96
JB
4418 continue; /* unmapped overlay -- can't be a match */
4419
4420 return 1;
c5aa993b 4421 }
c906108c
SS
4422
4423 return 0;
4424}
c906108c 4425\f
c5aa993b 4426
c906108c
SS
4427/* bpstat stuff. External routines' interfaces are documented
4428 in breakpoint.h. */
4429
4430int
c326b90e 4431is_catchpoint (struct breakpoint *ep)
c906108c 4432{
533be4dd 4433 return (ep->type == bp_catchpoint);
c906108c
SS
4434}
4435
f431efe5
PA
4436/* Frees any storage that is part of a bpstat. Does not walk the
4437 'next' chain. */
4438
4439static void
198757a8
VP
4440bpstat_free (bpstat bs)
4441{
4442 if (bs->old_val != NULL)
4443 value_free (bs->old_val);
9add0f1b 4444 decref_counted_command_line (&bs->commands);
f431efe5 4445 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4446 xfree (bs);
4447}
4448
c906108c
SS
4449/* Clear a bpstat so that it says we are not at any breakpoint.
4450 Also free any storage that is part of a bpstat. */
4451
4452void
fba45db2 4453bpstat_clear (bpstat *bsp)
c906108c
SS
4454{
4455 bpstat p;
4456 bpstat q;
4457
4458 if (bsp == 0)
4459 return;
4460 p = *bsp;
4461 while (p != NULL)
4462 {
4463 q = p->next;
198757a8 4464 bpstat_free (p);
c906108c
SS
4465 p = q;
4466 }
4467 *bsp = NULL;
4468}
4469
4470/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4471 is part of the bpstat is copied as well. */
4472
4473bpstat
fba45db2 4474bpstat_copy (bpstat bs)
c906108c
SS
4475{
4476 bpstat p = NULL;
4477 bpstat tmp;
4478 bpstat retval = NULL;
4479
4480 if (bs == NULL)
4481 return bs;
4482
4483 for (; bs != NULL; bs = bs->next)
4484 {
4485 tmp = (bpstat) xmalloc (sizeof (*tmp));
4486 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4487 incref_counted_command_line (tmp->commands);
f431efe5 4488 incref_bp_location (tmp->bp_location_at);
31cc81e9 4489 if (bs->old_val != NULL)
3c3185ac
JK
4490 {
4491 tmp->old_val = value_copy (bs->old_val);
4492 release_value (tmp->old_val);
4493 }
31cc81e9 4494
c906108c
SS
4495 if (p == NULL)
4496 /* This is the first thing in the chain. */
4497 retval = tmp;
4498 else
4499 p->next = tmp;
4500 p = tmp;
4501 }
4502 p->next = NULL;
4503 return retval;
4504}
4505
4a64f543 4506/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4507
4508bpstat
fba45db2 4509bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4510{
c5aa993b
JM
4511 if (bsp == NULL)
4512 return NULL;
c906108c 4513
c5aa993b
JM
4514 for (; bsp != NULL; bsp = bsp->next)
4515 {
f431efe5 4516 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4517 return bsp;
4518 }
c906108c
SS
4519 return NULL;
4520}
4521
ab04a2af
TT
4522/* See breakpoint.h. */
4523
47591c29 4524int
427cd150 4525bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4526{
ab04a2af
TT
4527 for (; bsp != NULL; bsp = bsp->next)
4528 {
427cd150
TT
4529 if (bsp->breakpoint_at == NULL)
4530 {
4531 /* A moribund location can never explain a signal other than
4532 GDB_SIGNAL_TRAP. */
4533 if (sig == GDB_SIGNAL_TRAP)
47591c29 4534 return 1;
427cd150
TT
4535 }
4536 else
47591c29
PA
4537 {
4538 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4539 sig))
4540 return 1;
4541 }
ab04a2af
TT
4542 }
4543
47591c29 4544 return 0;
ab04a2af
TT
4545}
4546
4a64f543
MS
4547/* Put in *NUM the breakpoint number of the first breakpoint we are
4548 stopped at. *BSP upon return is a bpstat which points to the
4549 remaining breakpoints stopped at (but which is not guaranteed to be
4550 good for anything but further calls to bpstat_num).
4551
8671a17b
PA
4552 Return 0 if passed a bpstat which does not indicate any breakpoints.
4553 Return -1 if stopped at a breakpoint that has been deleted since
4554 we set it.
4555 Return 1 otherwise. */
c906108c
SS
4556
4557int
8671a17b 4558bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4559{
4560 struct breakpoint *b;
4561
4562 if ((*bsp) == NULL)
4563 return 0; /* No more breakpoint values */
8671a17b 4564
4a64f543
MS
4565 /* We assume we'll never have several bpstats that correspond to a
4566 single breakpoint -- otherwise, this function might return the
4567 same number more than once and this will look ugly. */
f431efe5 4568 b = (*bsp)->breakpoint_at;
8671a17b
PA
4569 *bsp = (*bsp)->next;
4570 if (b == NULL)
4571 return -1; /* breakpoint that's been deleted since */
4572
4573 *num = b->number; /* We have its number */
4574 return 1;
c906108c
SS
4575}
4576
e93ca019 4577/* See breakpoint.h. */
c906108c
SS
4578
4579void
e93ca019 4580bpstat_clear_actions (void)
c906108c 4581{
e93ca019
JK
4582 struct thread_info *tp;
4583 bpstat bs;
4584
4585 if (ptid_equal (inferior_ptid, null_ptid))
4586 return;
4587
4588 tp = find_thread_ptid (inferior_ptid);
4589 if (tp == NULL)
4590 return;
4591
4592 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4593 {
9add0f1b 4594 decref_counted_command_line (&bs->commands);
abf85f46 4595
c906108c
SS
4596 if (bs->old_val != NULL)
4597 {
4598 value_free (bs->old_val);
4599 bs->old_val = NULL;
4600 }
4601 }
4602}
4603
f3b1572e
PA
4604/* Called when a command is about to proceed the inferior. */
4605
4606static void
4607breakpoint_about_to_proceed (void)
4608{
4609 if (!ptid_equal (inferior_ptid, null_ptid))
4610 {
4611 struct thread_info *tp = inferior_thread ();
4612
4613 /* Allow inferior function calls in breakpoint commands to not
4614 interrupt the command list. When the call finishes
4615 successfully, the inferior will be standing at the same
4616 breakpoint as if nothing happened. */
16c381f0 4617 if (tp->control.in_infcall)
f3b1572e
PA
4618 return;
4619 }
4620
4621 breakpoint_proceeded = 1;
4622}
4623
4a64f543
MS
4624/* Stub for cleaning up our state if we error-out of a breakpoint
4625 command. */
c906108c 4626static void
4efb68b1 4627cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4628{
4629 executing_breakpoint_commands = 0;
4630}
4631
abf85f46
JK
4632/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4633 or its equivalent. */
4634
4635static int
4636command_line_is_silent (struct command_line *cmd)
4637{
4638 return cmd && (strcmp ("silent", cmd->line) == 0
4639 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4640}
4641
4a64f543
MS
4642/* Execute all the commands associated with all the breakpoints at
4643 this location. Any of these commands could cause the process to
4644 proceed beyond this point, etc. We look out for such changes by
4645 checking the global "breakpoint_proceeded" after each command.
c906108c 4646
347bddb7
PA
4647 Returns true if a breakpoint command resumed the inferior. In that
4648 case, it is the caller's responsibility to recall it again with the
4649 bpstat of the current thread. */
4650
4651static int
4652bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4653{
4654 bpstat bs;
4655 struct cleanup *old_chain;
347bddb7 4656 int again = 0;
c906108c
SS
4657
4658 /* Avoid endless recursion if a `source' command is contained
4659 in bs->commands. */
4660 if (executing_breakpoint_commands)
347bddb7 4661 return 0;
c906108c
SS
4662
4663 executing_breakpoint_commands = 1;
4664 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4665
cf6c5ffb
TT
4666 prevent_dont_repeat ();
4667
4a64f543 4668 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4669 bs = *bsp;
4670
4671 breakpoint_proceeded = 0;
4672 for (; bs != NULL; bs = bs->next)
4673 {
9add0f1b 4674 struct counted_command_line *ccmd;
6c50ab1c
JB
4675 struct command_line *cmd;
4676 struct cleanup *this_cmd_tree_chain;
4677
4678 /* Take ownership of the BSP's command tree, if it has one.
4679
4680 The command tree could legitimately contain commands like
4681 'step' and 'next', which call clear_proceed_status, which
4682 frees stop_bpstat's command tree. To make sure this doesn't
4683 free the tree we're executing out from under us, we need to
4684 take ownership of the tree ourselves. Since a given bpstat's
4685 commands are only executed once, we don't need to copy it; we
4686 can clear the pointer in the bpstat, and make sure we free
4687 the tree when we're done. */
9add0f1b
TT
4688 ccmd = bs->commands;
4689 bs->commands = NULL;
abf85f46
JK
4690 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4691 cmd = ccmd ? ccmd->commands : NULL;
4692 if (command_line_is_silent (cmd))
4693 {
4694 /* The action has been already done by bpstat_stop_status. */
4695 cmd = cmd->next;
4696 }
6c50ab1c 4697
c906108c
SS
4698 while (cmd != NULL)
4699 {
4700 execute_control_command (cmd);
4701
4702 if (breakpoint_proceeded)
4703 break;
4704 else
4705 cmd = cmd->next;
4706 }
6c50ab1c
JB
4707
4708 /* We can free this command tree now. */
4709 do_cleanups (this_cmd_tree_chain);
4710
c906108c 4711 if (breakpoint_proceeded)
32c1e744
VP
4712 {
4713 if (target_can_async_p ())
347bddb7
PA
4714 /* If we are in async mode, then the target might be still
4715 running, not stopped at any breakpoint, so nothing for
4716 us to do here -- just return to the event loop. */
4717 ;
32c1e744
VP
4718 else
4719 /* In sync mode, when execute_control_command returns
4720 we're already standing on the next breakpoint.
347bddb7
PA
4721 Breakpoint commands for that stop were not run, since
4722 execute_command does not run breakpoint commands --
4723 only command_line_handler does, but that one is not
4724 involved in execution of breakpoint commands. So, we
4725 can now execute breakpoint commands. It should be
4726 noted that making execute_command do bpstat actions is
4727 not an option -- in this case we'll have recursive
4728 invocation of bpstat for each breakpoint with a
4729 command, and can easily blow up GDB stack. Instead, we
4730 return true, which will trigger the caller to recall us
4731 with the new stop_bpstat. */
4732 again = 1;
4733 break;
32c1e744 4734 }
c906108c 4735 }
c2b8ed2c 4736 do_cleanups (old_chain);
347bddb7
PA
4737 return again;
4738}
4739
4740void
4741bpstat_do_actions (void)
4742{
353d1d73
JK
4743 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4744
347bddb7
PA
4745 /* Do any commands attached to breakpoint we are stopped at. */
4746 while (!ptid_equal (inferior_ptid, null_ptid)
4747 && target_has_execution
4748 && !is_exited (inferior_ptid)
4749 && !is_executing (inferior_ptid))
4750 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4751 and only return when it is stopped at the next breakpoint, we
4752 keep doing breakpoint actions until it returns false to
4753 indicate the inferior was not resumed. */
16c381f0 4754 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4755 break;
353d1d73
JK
4756
4757 discard_cleanups (cleanup_if_error);
c906108c
SS
4758}
4759
fa4727a6
DJ
4760/* Print out the (old or new) value associated with a watchpoint. */
4761
4762static void
4763watchpoint_value_print (struct value *val, struct ui_file *stream)
4764{
4765 if (val == NULL)
4766 fprintf_unfiltered (stream, _("<unreadable>"));
4767 else
79a45b7d
TT
4768 {
4769 struct value_print_options opts;
4770 get_user_print_options (&opts);
4771 value_print (val, stream, &opts);
4772 }
fa4727a6
DJ
4773}
4774
e514a9d6 4775/* Generic routine for printing messages indicating why we
4a64f543 4776 stopped. The behavior of this function depends on the value
e514a9d6
JM
4777 'print_it' in the bpstat structure. Under some circumstances we
4778 may decide not to print anything here and delegate the task to
4a64f543 4779 normal_stop(). */
e514a9d6
JM
4780
4781static enum print_stop_action
4782print_bp_stop_message (bpstat bs)
4783{
4784 switch (bs->print_it)
4785 {
4786 case print_it_noop:
4a64f543 4787 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4788 return PRINT_UNKNOWN;
4789 break;
4790
4791 case print_it_done:
4792 /* We still want to print the frame, but we already printed the
4a64f543 4793 relevant messages. */
e514a9d6
JM
4794 return PRINT_SRC_AND_LOC;
4795 break;
4796
4797 case print_it_normal:
4f8d1dc6 4798 {
f431efe5
PA
4799 struct breakpoint *b = bs->breakpoint_at;
4800
1a6a67de
TJB
4801 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4802 which has since been deleted. */
4803 if (b == NULL)
4804 return PRINT_UNKNOWN;
4805
348d480f
PA
4806 /* Normal case. Call the breakpoint's print_it method. */
4807 return b->ops->print_it (bs);
4f8d1dc6 4808 }
348d480f 4809 break;
3086aeae 4810
e514a9d6 4811 default:
8e65ff28 4812 internal_error (__FILE__, __LINE__,
e2e0b3e5 4813 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4814 break;
c906108c 4815 }
c906108c
SS
4816}
4817
edcc5120
TT
4818/* A helper function that prints a shared library stopped event. */
4819
4820static void
4821print_solib_event (int is_catchpoint)
4822{
4823 int any_deleted
4824 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4825 int any_added
4826 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4827
4828 if (!is_catchpoint)
4829 {
4830 if (any_added || any_deleted)
4831 ui_out_text (current_uiout,
4832 _("Stopped due to shared library event:\n"));
4833 else
4834 ui_out_text (current_uiout,
4835 _("Stopped due to shared library event (no "
4836 "libraries added or removed)\n"));
4837 }
4838
4839 if (ui_out_is_mi_like_p (current_uiout))
4840 ui_out_field_string (current_uiout, "reason",
4841 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4842
4843 if (any_deleted)
4844 {
4845 struct cleanup *cleanup;
4846 char *name;
4847 int ix;
4848
4849 ui_out_text (current_uiout, _(" Inferior unloaded "));
4850 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4851 "removed");
4852 for (ix = 0;
4853 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4854 ix, name);
4855 ++ix)
4856 {
4857 if (ix > 0)
4858 ui_out_text (current_uiout, " ");
4859 ui_out_field_string (current_uiout, "library", name);
4860 ui_out_text (current_uiout, "\n");
4861 }
4862
4863 do_cleanups (cleanup);
4864 }
4865
4866 if (any_added)
4867 {
4868 struct so_list *iter;
4869 int ix;
4870 struct cleanup *cleanup;
4871
4872 ui_out_text (current_uiout, _(" Inferior loaded "));
4873 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4874 "added");
4875 for (ix = 0;
4876 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4877 ix, iter);
4878 ++ix)
4879 {
4880 if (ix > 0)
4881 ui_out_text (current_uiout, " ");
4882 ui_out_field_string (current_uiout, "library", iter->so_name);
4883 ui_out_text (current_uiout, "\n");
4884 }
4885
4886 do_cleanups (cleanup);
4887 }
4888}
4889
e514a9d6
JM
4890/* Print a message indicating what happened. This is called from
4891 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4892 list - a list of the eventpoints that caused this stop. KIND is
4893 the target_waitkind for the stopping event. This
e514a9d6
JM
4894 routine calls the generic print routine for printing a message
4895 about reasons for stopping. This will print (for example) the
4896 "Breakpoint n," part of the output. The return value of this
4897 routine is one of:
c906108c 4898
4a64f543 4899 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4900 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4901 code to print the location. An example is
c5aa993b
JM
4902 "Breakpoint 1, " which should be followed by
4903 the location.
917317f4 4904 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4905 to also print the location part of the message.
4906 An example is the catch/throw messages, which
4a64f543 4907 don't require a location appended to the end.
917317f4 4908 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4909 further info to be printed. */
c906108c 4910
917317f4 4911enum print_stop_action
36dfb11c 4912bpstat_print (bpstat bs, int kind)
c906108c
SS
4913{
4914 int val;
c5aa993b 4915
c906108c 4916 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4917 (Currently all watchpoints go on the bpstat whether hit or not.
4918 That probably could (should) be changed, provided care is taken
c906108c 4919 with respect to bpstat_explains_signal). */
e514a9d6
JM
4920 for (; bs; bs = bs->next)
4921 {
4922 val = print_bp_stop_message (bs);
4923 if (val == PRINT_SRC_ONLY
4924 || val == PRINT_SRC_AND_LOC
4925 || val == PRINT_NOTHING)
4926 return val;
4927 }
c906108c 4928
36dfb11c
TT
4929 /* If we had hit a shared library event breakpoint,
4930 print_bp_stop_message would print out this message. If we hit an
4931 OS-level shared library event, do the same thing. */
4932 if (kind == TARGET_WAITKIND_LOADED)
4933 {
edcc5120 4934 print_solib_event (0);
36dfb11c
TT
4935 return PRINT_NOTHING;
4936 }
4937
e514a9d6 4938 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4939 with and nothing was printed. */
917317f4 4940 return PRINT_UNKNOWN;
c906108c
SS
4941}
4942
c42bd95a
DE
4943/* Evaluate the expression EXP and return 1 if value is zero.
4944 This returns the inverse of the condition because it is called
4945 from catch_errors which returns 0 if an exception happened, and if an
4946 exception happens we want execution to stop.
4a64f543 4947 The argument is a "struct expression *" that has been cast to a
c42bd95a 4948 "void *" to make it pass through catch_errors. */
c906108c
SS
4949
4950static int
4efb68b1 4951breakpoint_cond_eval (void *exp)
c906108c 4952{
278cd55f 4953 struct value *mark = value_mark ();
c5aa993b 4954 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4955
c906108c
SS
4956 value_free_to_mark (mark);
4957 return i;
4958}
4959
5760d0ab 4960/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4961
4962static bpstat
5760d0ab 4963bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4964{
4965 bpstat bs;
4966
4967 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4968 bs->next = NULL;
4969 **bs_link_pointer = bs;
4970 *bs_link_pointer = &bs->next;
f431efe5
PA
4971 bs->breakpoint_at = bl->owner;
4972 bs->bp_location_at = bl;
4973 incref_bp_location (bl);
c906108c
SS
4974 /* If the condition is false, etc., don't do the commands. */
4975 bs->commands = NULL;
4976 bs->old_val = NULL;
4977 bs->print_it = print_it_normal;
4978 return bs;
4979}
4980\f
d983da9c
DJ
4981/* The target has stopped with waitstatus WS. Check if any hardware
4982 watchpoints have triggered, according to the target. */
4983
4984int
4985watchpoints_triggered (struct target_waitstatus *ws)
4986{
d92524f1 4987 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4988 CORE_ADDR addr;
4989 struct breakpoint *b;
4990
4991 if (!stopped_by_watchpoint)
4992 {
4993 /* We were not stopped by a watchpoint. Mark all watchpoints
4994 as not triggered. */
4995 ALL_BREAKPOINTS (b)
cc60f2e3 4996 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4997 {
4998 struct watchpoint *w = (struct watchpoint *) b;
4999
5000 w->watchpoint_triggered = watch_triggered_no;
5001 }
d983da9c
DJ
5002
5003 return 0;
5004 }
5005
5006 if (!target_stopped_data_address (&current_target, &addr))
5007 {
5008 /* We were stopped by a watchpoint, but we don't know where.
5009 Mark all watchpoints as unknown. */
5010 ALL_BREAKPOINTS (b)
cc60f2e3 5011 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5012 {
5013 struct watchpoint *w = (struct watchpoint *) b;
5014
5015 w->watchpoint_triggered = watch_triggered_unknown;
5016 }
d983da9c 5017
3c4797ba 5018 return 1;
d983da9c
DJ
5019 }
5020
5021 /* The target could report the data address. Mark watchpoints
5022 affected by this data address as triggered, and all others as not
5023 triggered. */
5024
5025 ALL_BREAKPOINTS (b)
cc60f2e3 5026 if (is_hardware_watchpoint (b))
d983da9c 5027 {
3a5c3e22 5028 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5029 struct bp_location *loc;
d983da9c 5030
3a5c3e22 5031 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5032 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5033 {
3a5c3e22 5034 if (is_masked_watchpoint (b))
9c06b0b4 5035 {
3a5c3e22
PA
5036 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5037 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5038
5039 if (newaddr == start)
5040 {
3a5c3e22 5041 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5042 break;
5043 }
5044 }
5045 /* Exact match not required. Within range is sufficient. */
5046 else if (target_watchpoint_addr_within_range (&current_target,
5047 addr, loc->address,
5048 loc->length))
5049 {
3a5c3e22 5050 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5051 break;
5052 }
5053 }
d983da9c
DJ
5054 }
5055
5056 return 1;
5057}
5058
c906108c
SS
5059/* Possible return values for watchpoint_check (this can't be an enum
5060 because of check_errors). */
5061/* The watchpoint has been deleted. */
5062#define WP_DELETED 1
5063/* The value has changed. */
5064#define WP_VALUE_CHANGED 2
5065/* The value has not changed. */
5066#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5067/* Ignore this watchpoint, no matter if the value changed or not. */
5068#define WP_IGNORE 4
c906108c
SS
5069
5070#define BP_TEMPFLAG 1
5071#define BP_HARDWAREFLAG 2
5072
4a64f543
MS
5073/* Evaluate watchpoint condition expression and check if its value
5074 changed.
553e4c11
JB
5075
5076 P should be a pointer to struct bpstat, but is defined as a void *
5077 in order for this function to be usable with catch_errors. */
c906108c
SS
5078
5079static int
4efb68b1 5080watchpoint_check (void *p)
c906108c
SS
5081{
5082 bpstat bs = (bpstat) p;
3a5c3e22 5083 struct watchpoint *b;
c906108c
SS
5084 struct frame_info *fr;
5085 int within_current_scope;
5086
f431efe5 5087 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5088 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5089 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5090
f6bc2008
PA
5091 /* If this is a local watchpoint, we only want to check if the
5092 watchpoint frame is in scope if the current thread is the thread
5093 that was used to create the watchpoint. */
5094 if (!watchpoint_in_thread_scope (b))
60e1c644 5095 return WP_IGNORE;
f6bc2008 5096
c906108c
SS
5097 if (b->exp_valid_block == NULL)
5098 within_current_scope = 1;
5099 else
5100 {
edb3359d
DJ
5101 struct frame_info *frame = get_current_frame ();
5102 struct gdbarch *frame_arch = get_frame_arch (frame);
5103 CORE_ADDR frame_pc = get_frame_pc (frame);
5104
4a64f543
MS
5105 /* in_function_epilogue_p() returns a non-zero value if we're
5106 still in the function but the stack frame has already been
5107 invalidated. Since we can't rely on the values of local
5108 variables after the stack has been destroyed, we are treating
5109 the watchpoint in that state as `not changed' without further
5110 checking. Don't mark watchpoints as changed if the current
5111 frame is in an epilogue - even if they are in some other
5112 frame, our view of the stack is likely to be wrong and
5113 frame_find_by_id could error out. */
a0f49112 5114 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 5115 return WP_IGNORE;
a0f49112 5116
101dcfbe 5117 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5118 within_current_scope = (fr != NULL);
69fbadd5
DJ
5119
5120 /* If we've gotten confused in the unwinder, we might have
5121 returned a frame that can't describe this variable. */
edb3359d
DJ
5122 if (within_current_scope)
5123 {
5124 struct symbol *function;
5125
5126 function = get_frame_function (fr);
5127 if (function == NULL
5128 || !contained_in (b->exp_valid_block,
5129 SYMBOL_BLOCK_VALUE (function)))
5130 within_current_scope = 0;
5131 }
69fbadd5 5132
edb3359d 5133 if (within_current_scope)
c906108c
SS
5134 /* If we end up stopping, the current frame will get selected
5135 in normal_stop. So this call to select_frame won't affect
5136 the user. */
0f7d239c 5137 select_frame (fr);
c906108c 5138 }
c5aa993b 5139
c906108c
SS
5140 if (within_current_scope)
5141 {
4a64f543
MS
5142 /* We use value_{,free_to_}mark because it could be a *long*
5143 time before we return to the command level and call
5144 free_all_values. We can't call free_all_values because we
5145 might be in the middle of evaluating a function call. */
c906108c 5146
0cf6dd15 5147 int pc = 0;
9c06b0b4 5148 struct value *mark;
fa4727a6
DJ
5149 struct value *new_val;
5150
3a5c3e22 5151 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5152 /* Since we don't know the exact trigger address (from
5153 stopped_data_address), just tell the user we've triggered
5154 a mask watchpoint. */
5155 return WP_VALUE_CHANGED;
5156
5157 mark = value_mark ();
3a1115a0 5158 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5159
bb9d5f81
PP
5160 if (b->val_bitsize != 0)
5161 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5162
4a64f543
MS
5163 /* We use value_equal_contents instead of value_equal because
5164 the latter coerces an array to a pointer, thus comparing just
5165 the address of the array instead of its contents. This is
5166 not what we want. */
fa4727a6 5167 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5168 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5169 {
fa4727a6
DJ
5170 if (new_val != NULL)
5171 {
5172 release_value (new_val);
5173 value_free_to_mark (mark);
5174 }
c906108c
SS
5175 bs->old_val = b->val;
5176 b->val = new_val;
fa4727a6 5177 b->val_valid = 1;
c906108c
SS
5178 return WP_VALUE_CHANGED;
5179 }
5180 else
5181 {
60e1c644 5182 /* Nothing changed. */
c906108c 5183 value_free_to_mark (mark);
c906108c
SS
5184 return WP_VALUE_NOT_CHANGED;
5185 }
5186 }
5187 else
5188 {
79a45e25
PA
5189 struct ui_out *uiout = current_uiout;
5190
c906108c 5191 /* This seems like the only logical thing to do because
c5aa993b
JM
5192 if we temporarily ignored the watchpoint, then when
5193 we reenter the block in which it is valid it contains
5194 garbage (in the case of a function, it may have two
5195 garbage values, one before and one after the prologue).
5196 So we can't even detect the first assignment to it and
5197 watch after that (since the garbage may or may not equal
5198 the first value assigned). */
348d480f
PA
5199 /* We print all the stop information in
5200 breakpoint_ops->print_it, but in this case, by the time we
5201 call breakpoint_ops->print_it this bp will be deleted
5202 already. So we have no choice but print the information
5203 here. */
9dc5e2a9 5204 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5205 ui_out_field_string
5206 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5207 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5208 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5209 ui_out_text (uiout,
5210 " deleted because the program has left the block in\n\
8b93c638 5211which its expression is valid.\n");
4ce44c66 5212
cdac0397 5213 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5214 decref_counted_command_line (&b->base.commands);
d0fb5eae 5215 watchpoint_del_at_next_stop (b);
c906108c
SS
5216
5217 return WP_DELETED;
5218 }
5219}
5220
18a18393 5221/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5222 breakpoint location BL. This function does not check if we should
5223 stop, only if BL explains the stop. */
5224
18a18393 5225static int
6c95b8df 5226bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5227 struct address_space *aspace, CORE_ADDR bp_addr,
5228 const struct target_waitstatus *ws)
18a18393
VP
5229{
5230 struct breakpoint *b = bl->owner;
5231
348d480f 5232 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5233 gdb_assert (b != NULL);
5234
09ac7c10 5235 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5236}
5237
3a5c3e22
PA
5238/* Determine if the watched values have actually changed, and we
5239 should stop. If not, set BS->stop to 0. */
5240
18a18393
VP
5241static void
5242bpstat_check_watchpoint (bpstat bs)
5243{
2bdf28a0 5244 const struct bp_location *bl;
3a5c3e22 5245 struct watchpoint *b;
2bdf28a0
JK
5246
5247 /* BS is built for existing struct breakpoint. */
f431efe5 5248 bl = bs->bp_location_at;
2bdf28a0 5249 gdb_assert (bl != NULL);
3a5c3e22 5250 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5251 gdb_assert (b != NULL);
18a18393 5252
18a18393 5253 {
18a18393
VP
5254 int must_check_value = 0;
5255
3a5c3e22 5256 if (b->base.type == bp_watchpoint)
18a18393
VP
5257 /* For a software watchpoint, we must always check the
5258 watched value. */
5259 must_check_value = 1;
5260 else if (b->watchpoint_triggered == watch_triggered_yes)
5261 /* We have a hardware watchpoint (read, write, or access)
5262 and the target earlier reported an address watched by
5263 this watchpoint. */
5264 must_check_value = 1;
5265 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5266 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5267 /* We were stopped by a hardware watchpoint, but the target could
5268 not report the data address. We must check the watchpoint's
5269 value. Access and read watchpoints are out of luck; without
5270 a data address, we can't figure it out. */
5271 must_check_value = 1;
3a5c3e22 5272
18a18393
VP
5273 if (must_check_value)
5274 {
3e43a32a
MS
5275 char *message
5276 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5277 b->base.number);
18a18393
VP
5278 struct cleanup *cleanups = make_cleanup (xfree, message);
5279 int e = catch_errors (watchpoint_check, bs, message,
5280 RETURN_MASK_ALL);
5281 do_cleanups (cleanups);
5282 switch (e)
5283 {
5284 case WP_DELETED:
5285 /* We've already printed what needs to be printed. */
5286 bs->print_it = print_it_done;
5287 /* Stop. */
5288 break;
60e1c644
PA
5289 case WP_IGNORE:
5290 bs->print_it = print_it_noop;
5291 bs->stop = 0;
5292 break;
18a18393 5293 case WP_VALUE_CHANGED:
3a5c3e22 5294 if (b->base.type == bp_read_watchpoint)
18a18393 5295 {
85d721b8
PA
5296 /* There are two cases to consider here:
5297
4a64f543 5298 1. We're watching the triggered memory for reads.
85d721b8
PA
5299 In that case, trust the target, and always report
5300 the watchpoint hit to the user. Even though
5301 reads don't cause value changes, the value may
5302 have changed since the last time it was read, and
5303 since we're not trapping writes, we will not see
5304 those, and as such we should ignore our notion of
5305 old value.
5306
4a64f543 5307 2. We're watching the triggered memory for both
85d721b8
PA
5308 reads and writes. There are two ways this may
5309 happen:
5310
4a64f543 5311 2.1. This is a target that can't break on data
85d721b8
PA
5312 reads only, but can break on accesses (reads or
5313 writes), such as e.g., x86. We detect this case
5314 at the time we try to insert read watchpoints.
5315
4a64f543 5316 2.2. Otherwise, the target supports read
85d721b8
PA
5317 watchpoints, but, the user set an access or write
5318 watchpoint watching the same memory as this read
5319 watchpoint.
5320
5321 If we're watching memory writes as well as reads,
5322 ignore watchpoint hits when we find that the
5323 value hasn't changed, as reads don't cause
5324 changes. This still gives false positives when
5325 the program writes the same value to memory as
5326 what there was already in memory (we will confuse
5327 it for a read), but it's much better than
5328 nothing. */
5329
5330 int other_write_watchpoint = 0;
5331
5332 if (bl->watchpoint_type == hw_read)
5333 {
5334 struct breakpoint *other_b;
5335
5336 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5337 if (other_b->type == bp_hardware_watchpoint
5338 || other_b->type == bp_access_watchpoint)
85d721b8 5339 {
3a5c3e22
PA
5340 struct watchpoint *other_w =
5341 (struct watchpoint *) other_b;
5342
5343 if (other_w->watchpoint_triggered
5344 == watch_triggered_yes)
5345 {
5346 other_write_watchpoint = 1;
5347 break;
5348 }
85d721b8
PA
5349 }
5350 }
5351
5352 if (other_write_watchpoint
5353 || bl->watchpoint_type == hw_access)
5354 {
5355 /* We're watching the same memory for writes,
5356 and the value changed since the last time we
5357 updated it, so this trap must be for a write.
5358 Ignore it. */
5359 bs->print_it = print_it_noop;
5360 bs->stop = 0;
5361 }
18a18393
VP
5362 }
5363 break;
5364 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5365 if (b->base.type == bp_hardware_watchpoint
5366 || b->base.type == bp_watchpoint)
18a18393
VP
5367 {
5368 /* Don't stop: write watchpoints shouldn't fire if
5369 the value hasn't changed. */
5370 bs->print_it = print_it_noop;
5371 bs->stop = 0;
5372 }
5373 /* Stop. */
5374 break;
5375 default:
5376 /* Can't happen. */
5377 case 0:
5378 /* Error from catch_errors. */
3a5c3e22 5379 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5380 watchpoint_del_at_next_stop (b);
18a18393
VP
5381 /* We've already printed what needs to be printed. */
5382 bs->print_it = print_it_done;
5383 break;
5384 }
5385 }
5386 else /* must_check_value == 0 */
5387 {
5388 /* This is a case where some watchpoint(s) triggered, but
5389 not at the address of this watchpoint, or else no
5390 watchpoint triggered after all. So don't print
5391 anything for this watchpoint. */
5392 bs->print_it = print_it_noop;
5393 bs->stop = 0;
5394 }
5395 }
5396}
5397
7d4df6a4
DE
5398/* For breakpoints that are currently marked as telling gdb to stop,
5399 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5400 of breakpoint referred to by BS. If we should not stop for this
5401 breakpoint, set BS->stop to 0. */
f431efe5 5402
18a18393
VP
5403static void
5404bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5405{
2bdf28a0
JK
5406 const struct bp_location *bl;
5407 struct breakpoint *b;
7d4df6a4
DE
5408 int value_is_zero = 0;
5409 struct expression *cond;
5410
5411 gdb_assert (bs->stop);
2bdf28a0
JK
5412
5413 /* BS is built for existing struct breakpoint. */
f431efe5 5414 bl = bs->bp_location_at;
2bdf28a0 5415 gdb_assert (bl != NULL);
f431efe5 5416 b = bs->breakpoint_at;
2bdf28a0 5417 gdb_assert (b != NULL);
18a18393 5418
b775012e
LM
5419 /* Even if the target evaluated the condition on its end and notified GDB, we
5420 need to do so again since GDB does not know if we stopped due to a
5421 breakpoint or a single step breakpoint. */
5422
18a18393 5423 if (frame_id_p (b->frame_id)
edb3359d 5424 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5425 {
7d4df6a4
DE
5426 bs->stop = 0;
5427 return;
5428 }
60e1c644 5429
12ab52e9
PA
5430 /* If this is a thread/task-specific breakpoint, don't waste cpu
5431 evaluating the condition if this isn't the specified
5432 thread/task. */
5433 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5434 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5435
6c1b0f7b
DE
5436 {
5437 bs->stop = 0;
5438 return;
5439 }
5440
6dddc817
DE
5441 /* Evaluate extension language breakpoints that have a "stop" method
5442 implemented. */
5443 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5444
7d4df6a4
DE
5445 if (is_watchpoint (b))
5446 {
5447 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5448
7d4df6a4
DE
5449 cond = w->cond_exp;
5450 }
5451 else
5452 cond = bl->cond;
60e1c644 5453
7d4df6a4
DE
5454 if (cond && b->disposition != disp_del_at_next_stop)
5455 {
5456 int within_current_scope = 1;
5457 struct watchpoint * w;
60e1c644 5458
7d4df6a4
DE
5459 /* We use value_mark and value_free_to_mark because it could
5460 be a long time before we return to the command level and
5461 call free_all_values. We can't call free_all_values
5462 because we might be in the middle of evaluating a
5463 function call. */
5464 struct value *mark = value_mark ();
5465
5466 if (is_watchpoint (b))
5467 w = (struct watchpoint *) b;
5468 else
5469 w = NULL;
5470
5471 /* Need to select the frame, with all that implies so that
5472 the conditions will have the right context. Because we
5473 use the frame, we will not see an inlined function's
5474 variables when we arrive at a breakpoint at the start
5475 of the inlined function; the current frame will be the
5476 call site. */
5477 if (w == NULL || w->cond_exp_valid_block == NULL)
5478 select_frame (get_current_frame ());
5479 else
18a18393 5480 {
7d4df6a4
DE
5481 struct frame_info *frame;
5482
5483 /* For local watchpoint expressions, which particular
5484 instance of a local is being watched matters, so we
5485 keep track of the frame to evaluate the expression
5486 in. To evaluate the condition however, it doesn't
5487 really matter which instantiation of the function
5488 where the condition makes sense triggers the
5489 watchpoint. This allows an expression like "watch
5490 global if q > 10" set in `func', catch writes to
5491 global on all threads that call `func', or catch
5492 writes on all recursive calls of `func' by a single
5493 thread. We simply always evaluate the condition in
5494 the innermost frame that's executing where it makes
5495 sense to evaluate the condition. It seems
5496 intuitive. */
5497 frame = block_innermost_frame (w->cond_exp_valid_block);
5498 if (frame != NULL)
5499 select_frame (frame);
5500 else
5501 within_current_scope = 0;
18a18393 5502 }
7d4df6a4
DE
5503 if (within_current_scope)
5504 value_is_zero
5505 = catch_errors (breakpoint_cond_eval, cond,
5506 "Error in testing breakpoint condition:\n",
5507 RETURN_MASK_ALL);
5508 else
18a18393 5509 {
7d4df6a4
DE
5510 warning (_("Watchpoint condition cannot be tested "
5511 "in the current scope"));
5512 /* If we failed to set the right context for this
5513 watchpoint, unconditionally report it. */
5514 value_is_zero = 0;
18a18393 5515 }
7d4df6a4
DE
5516 /* FIXME-someday, should give breakpoint #. */
5517 value_free_to_mark (mark);
18a18393 5518 }
7d4df6a4
DE
5519
5520 if (cond && value_is_zero)
5521 {
5522 bs->stop = 0;
5523 }
7d4df6a4
DE
5524 else if (b->ignore_count > 0)
5525 {
5526 b->ignore_count--;
5527 bs->stop = 0;
5528 /* Increase the hit count even though we don't stop. */
5529 ++(b->hit_count);
5530 observer_notify_breakpoint_modified (b);
5531 }
18a18393
VP
5532}
5533
5534
9709f61c 5535/* Get a bpstat associated with having just stopped at address
d983da9c 5536 BP_ADDR in thread PTID.
c906108c 5537
d983da9c 5538 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5539 don't understand this stop. Result is a chain of bpstat's such
5540 that:
c906108c 5541
c5aa993b 5542 if we don't understand the stop, the result is a null pointer.
c906108c 5543
c5aa993b 5544 if we understand why we stopped, the result is not null.
c906108c 5545
c5aa993b
JM
5546 Each element of the chain refers to a particular breakpoint or
5547 watchpoint at which we have stopped. (We may have stopped for
5548 several reasons concurrently.)
c906108c 5549
c5aa993b
JM
5550 Each element of the chain has valid next, breakpoint_at,
5551 commands, FIXME??? fields. */
c906108c
SS
5552
5553bpstat
6c95b8df 5554bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5555 CORE_ADDR bp_addr, ptid_t ptid,
5556 const struct target_waitstatus *ws)
c906108c 5557{
0d381245 5558 struct breakpoint *b = NULL;
afe38095 5559 struct bp_location *bl;
20874c92 5560 struct bp_location *loc;
5760d0ab
JK
5561 /* First item of allocated bpstat's. */
5562 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5563 /* Pointer to the last thing in the chain currently. */
5760d0ab 5564 bpstat bs;
20874c92 5565 int ix;
429374b8 5566 int need_remove_insert;
f431efe5 5567 int removed_any;
c906108c 5568
f431efe5
PA
5569 /* First, build the bpstat chain with locations that explain a
5570 target stop, while being careful to not set the target running,
5571 as that may invalidate locations (in particular watchpoint
5572 locations are recreated). Resuming will happen here with
5573 breakpoint conditions or watchpoint expressions that include
5574 inferior function calls. */
c5aa993b 5575
429374b8
JK
5576 ALL_BREAKPOINTS (b)
5577 {
5578 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5579 continue;
a5606eee 5580
429374b8
JK
5581 for (bl = b->loc; bl != NULL; bl = bl->next)
5582 {
4a64f543
MS
5583 /* For hardware watchpoints, we look only at the first
5584 location. The watchpoint_check function will work on the
5585 entire expression, not the individual locations. For
5586 read watchpoints, the watchpoints_triggered function has
5587 checked all locations already. */
429374b8
JK
5588 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5589 break;
18a18393 5590
f6592439 5591 if (!bl->enabled || bl->shlib_disabled)
429374b8 5592 continue;
c5aa993b 5593
09ac7c10 5594 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5595 continue;
c5aa993b 5596
4a64f543
MS
5597 /* Come here if it's a watchpoint, or if the break address
5598 matches. */
c5aa993b 5599
4a64f543
MS
5600 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5601 explain stop. */
c5aa993b 5602
f431efe5
PA
5603 /* Assume we stop. Should we find a watchpoint that is not
5604 actually triggered, or if the condition of the breakpoint
5605 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5606 bs->stop = 1;
5607 bs->print = 1;
d983da9c 5608
f431efe5
PA
5609 /* If this is a scope breakpoint, mark the associated
5610 watchpoint as triggered so that we will handle the
5611 out-of-scope event. We'll get to the watchpoint next
5612 iteration. */
d0fb5eae 5613 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5614 {
5615 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5616
5617 w->watchpoint_triggered = watch_triggered_yes;
5618 }
f431efe5
PA
5619 }
5620 }
5621
7c16b83e 5622 /* Check if a moribund breakpoint explains the stop. */
f431efe5
PA
5623 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5624 {
f1310107 5625 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5626 {
5760d0ab 5627 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5628 /* For hits of moribund locations, we should just proceed. */
5629 bs->stop = 0;
5630 bs->print = 0;
5631 bs->print_it = print_it_noop;
5632 }
5633 }
5634
edcc5120
TT
5635 /* A bit of special processing for shlib breakpoints. We need to
5636 process solib loading here, so that the lists of loaded and
5637 unloaded libraries are correct before we handle "catch load" and
5638 "catch unload". */
5639 for (bs = bs_head; bs != NULL; bs = bs->next)
5640 {
5d268276 5641 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5642 {
5643 handle_solib_event ();
5644 break;
5645 }
5646 }
5647
f431efe5
PA
5648 /* Now go through the locations that caused the target to stop, and
5649 check whether we're interested in reporting this stop to higher
5650 layers, or whether we should resume the target transparently. */
5651
5652 removed_any = 0;
5653
5760d0ab 5654 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5655 {
5656 if (!bs->stop)
5657 continue;
5658
f431efe5 5659 b = bs->breakpoint_at;
348d480f
PA
5660 b->ops->check_status (bs);
5661 if (bs->stop)
28010a5d 5662 {
348d480f 5663 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5664
429374b8
JK
5665 if (bs->stop)
5666 {
5667 ++(b->hit_count);
8d3788bd 5668 observer_notify_breakpoint_modified (b);
c906108c 5669
4a64f543 5670 /* We will stop here. */
429374b8
JK
5671 if (b->disposition == disp_disable)
5672 {
816338b5
SS
5673 --(b->enable_count);
5674 if (b->enable_count <= 0
5675 && b->enable_state != bp_permanent)
429374b8 5676 b->enable_state = bp_disabled;
f431efe5 5677 removed_any = 1;
429374b8
JK
5678 }
5679 if (b->silent)
5680 bs->print = 0;
5681 bs->commands = b->commands;
9add0f1b 5682 incref_counted_command_line (bs->commands);
abf85f46
JK
5683 if (command_line_is_silent (bs->commands
5684 ? bs->commands->commands : NULL))
5685 bs->print = 0;
9d6e6e84
HZ
5686
5687 b->ops->after_condition_true (bs);
429374b8
JK
5688 }
5689
348d480f 5690 }
a9b3a50f
PA
5691
5692 /* Print nothing for this entry if we don't stop or don't
5693 print. */
5694 if (!bs->stop || !bs->print)
5695 bs->print_it = print_it_noop;
429374b8 5696 }
876fa593 5697
d983da9c
DJ
5698 /* If we aren't stopping, the value of some hardware watchpoint may
5699 not have changed, but the intermediate memory locations we are
5700 watching may have. Don't bother if we're stopping; this will get
5701 done later. */
d832cb68 5702 need_remove_insert = 0;
5760d0ab
JK
5703 if (! bpstat_causes_stop (bs_head))
5704 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5705 if (!bs->stop
f431efe5
PA
5706 && bs->breakpoint_at
5707 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5708 {
3a5c3e22
PA
5709 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5710
5711 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5712 need_remove_insert = 1;
d983da9c
DJ
5713 }
5714
d832cb68 5715 if (need_remove_insert)
44702360 5716 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5717 else if (removed_any)
44702360 5718 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5719
5760d0ab 5720 return bs_head;
c906108c 5721}
628fe4e4
JK
5722
5723static void
5724handle_jit_event (void)
5725{
5726 struct frame_info *frame;
5727 struct gdbarch *gdbarch;
5728
5729 /* Switch terminal for any messages produced by
5730 breakpoint_re_set. */
5731 target_terminal_ours_for_output ();
5732
5733 frame = get_current_frame ();
5734 gdbarch = get_frame_arch (frame);
5735
5736 jit_event_handler (gdbarch);
5737
5738 target_terminal_inferior ();
5739}
5740
5741/* Prepare WHAT final decision for infrun. */
5742
5743/* Decide what infrun needs to do with this bpstat. */
5744
c906108c 5745struct bpstat_what
0e30163f 5746bpstat_what (bpstat bs_head)
c906108c 5747{
c906108c 5748 struct bpstat_what retval;
628fe4e4 5749 int jit_event = 0;
0e30163f 5750 bpstat bs;
c906108c 5751
628fe4e4 5752 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5753 retval.call_dummy = STOP_NONE;
186c406b 5754 retval.is_longjmp = 0;
628fe4e4 5755
0e30163f 5756 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5757 {
628fe4e4
JK
5758 /* Extract this BS's action. After processing each BS, we check
5759 if its action overrides all we've seem so far. */
5760 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5761 enum bptype bptype;
5762
c906108c 5763 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5764 {
5765 /* I suspect this can happen if it was a momentary
5766 breakpoint which has since been deleted. */
5767 bptype = bp_none;
5768 }
20874c92 5769 else
f431efe5 5770 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5771
5772 switch (bptype)
c906108c
SS
5773 {
5774 case bp_none:
628fe4e4 5775 break;
c906108c
SS
5776 case bp_breakpoint:
5777 case bp_hardware_breakpoint:
7c16b83e 5778 case bp_single_step:
c906108c
SS
5779 case bp_until:
5780 case bp_finish:
a9b3a50f 5781 case bp_shlib_event:
c906108c
SS
5782 if (bs->stop)
5783 {
5784 if (bs->print)
628fe4e4 5785 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5786 else
628fe4e4 5787 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5788 }
5789 else
628fe4e4 5790 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5791 break;
5792 case bp_watchpoint:
5793 case bp_hardware_watchpoint:
5794 case bp_read_watchpoint:
5795 case bp_access_watchpoint:
5796 if (bs->stop)
5797 {
5798 if (bs->print)
628fe4e4 5799 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5800 else
628fe4e4 5801 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5802 }
5803 else
628fe4e4
JK
5804 {
5805 /* There was a watchpoint, but we're not stopping.
5806 This requires no further action. */
5807 }
c906108c
SS
5808 break;
5809 case bp_longjmp:
e2e4d78b 5810 case bp_longjmp_call_dummy:
186c406b 5811 case bp_exception:
628fe4e4 5812 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5813 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5814 break;
5815 case bp_longjmp_resume:
186c406b 5816 case bp_exception_resume:
628fe4e4 5817 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5818 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5819 break;
5820 case bp_step_resume:
5821 if (bs->stop)
628fe4e4
JK
5822 this_action = BPSTAT_WHAT_STEP_RESUME;
5823 else
c906108c 5824 {
628fe4e4
JK
5825 /* It is for the wrong frame. */
5826 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5827 }
c906108c 5828 break;
2c03e5be
PA
5829 case bp_hp_step_resume:
5830 if (bs->stop)
5831 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5832 else
5833 {
5834 /* It is for the wrong frame. */
5835 this_action = BPSTAT_WHAT_SINGLE;
5836 }
5837 break;
c906108c 5838 case bp_watchpoint_scope:
c4093a6a 5839 case bp_thread_event:
1900040c 5840 case bp_overlay_event:
0fd8e87f 5841 case bp_longjmp_master:
aa7d318d 5842 case bp_std_terminate_master:
186c406b 5843 case bp_exception_master:
628fe4e4 5844 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5845 break;
ce78b96d 5846 case bp_catchpoint:
c5aa993b
JM
5847 if (bs->stop)
5848 {
5849 if (bs->print)
628fe4e4 5850 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5851 else
628fe4e4 5852 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5853 }
5854 else
628fe4e4
JK
5855 {
5856 /* There was a catchpoint, but we're not stopping.
5857 This requires no further action. */
5858 }
5859 break;
628fe4e4
JK
5860 case bp_jit_event:
5861 jit_event = 1;
5862 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5863 break;
c906108c 5864 case bp_call_dummy:
53a5351d
JM
5865 /* Make sure the action is stop (silent or noisy),
5866 so infrun.c pops the dummy frame. */
aa7d318d 5867 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5868 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5869 break;
5870 case bp_std_terminate:
5871 /* Make sure the action is stop (silent or noisy),
5872 so infrun.c pops the dummy frame. */
aa7d318d 5873 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5874 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5875 break;
1042e4c0 5876 case bp_tracepoint:
7a697b8d 5877 case bp_fast_tracepoint:
0fb4aa4b 5878 case bp_static_tracepoint:
1042e4c0
SS
5879 /* Tracepoint hits should not be reported back to GDB, and
5880 if one got through somehow, it should have been filtered
5881 out already. */
5882 internal_error (__FILE__, __LINE__,
7a697b8d 5883 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5884 break;
5885 case bp_gnu_ifunc_resolver:
5886 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5887 this_action = BPSTAT_WHAT_SINGLE;
5888 break;
5889 case bp_gnu_ifunc_resolver_return:
5890 /* The breakpoint will be removed, execution will restart from the
5891 PC of the former breakpoint. */
5892 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5893 break;
e7e0cddf
SS
5894
5895 case bp_dprintf:
a11cfd87
HZ
5896 if (bs->stop)
5897 this_action = BPSTAT_WHAT_STOP_SILENT;
5898 else
5899 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5900 break;
5901
628fe4e4
JK
5902 default:
5903 internal_error (__FILE__, __LINE__,
5904 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5905 }
628fe4e4
JK
5906
5907 retval.main_action = max (retval.main_action, this_action);
c906108c 5908 }
628fe4e4 5909
0e30163f
JK
5910 /* These operations may affect the bs->breakpoint_at state so they are
5911 delayed after MAIN_ACTION is decided above. */
5912
628fe4e4
JK
5913 if (jit_event)
5914 {
5915 if (debug_infrun)
5916 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5917
5918 handle_jit_event ();
5919 }
5920
0e30163f
JK
5921 for (bs = bs_head; bs != NULL; bs = bs->next)
5922 {
5923 struct breakpoint *b = bs->breakpoint_at;
5924
5925 if (b == NULL)
5926 continue;
5927 switch (b->type)
5928 {
5929 case bp_gnu_ifunc_resolver:
5930 gnu_ifunc_resolver_stop (b);
5931 break;
5932 case bp_gnu_ifunc_resolver_return:
5933 gnu_ifunc_resolver_return_stop (b);
5934 break;
5935 }
5936 }
5937
c906108c
SS
5938 return retval;
5939}
5940
5941/* Nonzero if we should step constantly (e.g. watchpoints on machines
5942 without hardware support). This isn't related to a specific bpstat,
5943 just to things like whether watchpoints are set. */
5944
c5aa993b 5945int
fba45db2 5946bpstat_should_step (void)
c906108c
SS
5947{
5948 struct breakpoint *b;
cc59ec59 5949
c906108c 5950 ALL_BREAKPOINTS (b)
717a8278 5951 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5952 return 1;
c906108c
SS
5953 return 0;
5954}
5955
67822962
PA
5956int
5957bpstat_causes_stop (bpstat bs)
5958{
5959 for (; bs != NULL; bs = bs->next)
5960 if (bs->stop)
5961 return 1;
5962
5963 return 0;
5964}
5965
c906108c 5966\f
c5aa993b 5967
170b53b2
UW
5968/* Compute a string of spaces suitable to indent the next line
5969 so it starts at the position corresponding to the table column
5970 named COL_NAME in the currently active table of UIOUT. */
5971
5972static char *
5973wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5974{
5975 static char wrap_indent[80];
5976 int i, total_width, width, align;
5977 char *text;
5978
5979 total_width = 0;
5980 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5981 {
5982 if (strcmp (text, col_name) == 0)
5983 {
5984 gdb_assert (total_width < sizeof wrap_indent);
5985 memset (wrap_indent, ' ', total_width);
5986 wrap_indent[total_width] = 0;
5987
5988 return wrap_indent;
5989 }
5990
5991 total_width += width + 1;
5992 }
5993
5994 return NULL;
5995}
5996
b775012e
LM
5997/* Determine if the locations of this breakpoint will have their conditions
5998 evaluated by the target, host or a mix of both. Returns the following:
5999
6000 "host": Host evals condition.
6001 "host or target": Host or Target evals condition.
6002 "target": Target evals condition.
6003*/
6004
6005static const char *
6006bp_condition_evaluator (struct breakpoint *b)
6007{
6008 struct bp_location *bl;
6009 char host_evals = 0;
6010 char target_evals = 0;
6011
6012 if (!b)
6013 return NULL;
6014
6015 if (!is_breakpoint (b))
6016 return NULL;
6017
6018 if (gdb_evaluates_breakpoint_condition_p ()
6019 || !target_supports_evaluation_of_breakpoint_conditions ())
6020 return condition_evaluation_host;
6021
6022 for (bl = b->loc; bl; bl = bl->next)
6023 {
6024 if (bl->cond_bytecode)
6025 target_evals++;
6026 else
6027 host_evals++;
6028 }
6029
6030 if (host_evals && target_evals)
6031 return condition_evaluation_both;
6032 else if (target_evals)
6033 return condition_evaluation_target;
6034 else
6035 return condition_evaluation_host;
6036}
6037
6038/* Determine the breakpoint location's condition evaluator. This is
6039 similar to bp_condition_evaluator, but for locations. */
6040
6041static const char *
6042bp_location_condition_evaluator (struct bp_location *bl)
6043{
6044 if (bl && !is_breakpoint (bl->owner))
6045 return NULL;
6046
6047 if (gdb_evaluates_breakpoint_condition_p ()
6048 || !target_supports_evaluation_of_breakpoint_conditions ())
6049 return condition_evaluation_host;
6050
6051 if (bl && bl->cond_bytecode)
6052 return condition_evaluation_target;
6053 else
6054 return condition_evaluation_host;
6055}
6056
859825b8
JK
6057/* Print the LOC location out of the list of B->LOC locations. */
6058
170b53b2
UW
6059static void
6060print_breakpoint_location (struct breakpoint *b,
6061 struct bp_location *loc)
0d381245 6062{
79a45e25 6063 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6064 struct cleanup *old_chain = save_current_program_space ();
6065
859825b8
JK
6066 if (loc != NULL && loc->shlib_disabled)
6067 loc = NULL;
6068
6c95b8df
PA
6069 if (loc != NULL)
6070 set_current_program_space (loc->pspace);
6071
56435ebe
TT
6072 if (b->display_canonical)
6073 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 6074 else if (loc && loc->symtab)
0d381245
VP
6075 {
6076 struct symbol *sym
6077 = find_pc_sect_function (loc->address, loc->section);
6078 if (sym)
6079 {
6080 ui_out_text (uiout, "in ");
6081 ui_out_field_string (uiout, "func",
6082 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6083 ui_out_text (uiout, " ");
6084 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6085 ui_out_text (uiout, "at ");
0d381245 6086 }
05cba821
JK
6087 ui_out_field_string (uiout, "file",
6088 symtab_to_filename_for_display (loc->symtab));
0d381245 6089 ui_out_text (uiout, ":");
05cba821 6090
0d381245 6091 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6092 ui_out_field_string (uiout, "fullname",
6093 symtab_to_fullname (loc->symtab));
0d381245 6094
f8eba3c6 6095 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6096 }
859825b8 6097 else if (loc)
0d381245 6098 {
f99d8bf4
PA
6099 struct ui_file *stb = mem_fileopen ();
6100 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6101
f99d8bf4 6102 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6103 demangle, "");
0d381245 6104 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6105
6106 do_cleanups (stb_chain);
0d381245 6107 }
859825b8
JK
6108 else
6109 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 6110
b775012e
LM
6111 if (loc && is_breakpoint (b)
6112 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6113 && bp_condition_evaluator (b) == condition_evaluation_both)
6114 {
6115 ui_out_text (uiout, " (");
6116 ui_out_field_string (uiout, "evaluated-by",
6117 bp_location_condition_evaluator (loc));
6118 ui_out_text (uiout, ")");
6119 }
6120
6c95b8df 6121 do_cleanups (old_chain);
0d381245
VP
6122}
6123
269b11a2
PA
6124static const char *
6125bptype_string (enum bptype type)
c906108c 6126{
c4093a6a
JM
6127 struct ep_type_description
6128 {
6129 enum bptype type;
6130 char *description;
6131 };
6132 static struct ep_type_description bptypes[] =
c906108c 6133 {
c5aa993b
JM
6134 {bp_none, "?deleted?"},
6135 {bp_breakpoint, "breakpoint"},
c906108c 6136 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6137 {bp_single_step, "sw single-step"},
c5aa993b
JM
6138 {bp_until, "until"},
6139 {bp_finish, "finish"},
6140 {bp_watchpoint, "watchpoint"},
c906108c 6141 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6142 {bp_read_watchpoint, "read watchpoint"},
6143 {bp_access_watchpoint, "acc watchpoint"},
6144 {bp_longjmp, "longjmp"},
6145 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6146 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6147 {bp_exception, "exception"},
6148 {bp_exception_resume, "exception resume"},
c5aa993b 6149 {bp_step_resume, "step resume"},
2c03e5be 6150 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6151 {bp_watchpoint_scope, "watchpoint scope"},
6152 {bp_call_dummy, "call dummy"},
aa7d318d 6153 {bp_std_terminate, "std::terminate"},
c5aa993b 6154 {bp_shlib_event, "shlib events"},
c4093a6a 6155 {bp_thread_event, "thread events"},
1900040c 6156 {bp_overlay_event, "overlay events"},
0fd8e87f 6157 {bp_longjmp_master, "longjmp master"},
aa7d318d 6158 {bp_std_terminate_master, "std::terminate master"},
186c406b 6159 {bp_exception_master, "exception master"},
ce78b96d 6160 {bp_catchpoint, "catchpoint"},
1042e4c0 6161 {bp_tracepoint, "tracepoint"},
7a697b8d 6162 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6163 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6164 {bp_dprintf, "dprintf"},
4efc6507 6165 {bp_jit_event, "jit events"},
0e30163f
JK
6166 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6167 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6168 };
269b11a2
PA
6169
6170 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6171 || ((int) type != bptypes[(int) type].type))
6172 internal_error (__FILE__, __LINE__,
6173 _("bptypes table does not describe type #%d."),
6174 (int) type);
6175
6176 return bptypes[(int) type].description;
6177}
6178
998580f1
MK
6179/* For MI, output a field named 'thread-groups' with a list as the value.
6180 For CLI, prefix the list with the string 'inf'. */
6181
6182static void
6183output_thread_groups (struct ui_out *uiout,
6184 const char *field_name,
6185 VEC(int) *inf_num,
6186 int mi_only)
6187{
752eb8b4 6188 struct cleanup *back_to;
998580f1
MK
6189 int is_mi = ui_out_is_mi_like_p (uiout);
6190 int inf;
6191 int i;
6192
6193 /* For backward compatibility, don't display inferiors in CLI unless
6194 there are several. Always display them for MI. */
6195 if (!is_mi && mi_only)
6196 return;
6197
752eb8b4
TT
6198 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6199
998580f1
MK
6200 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6201 {
6202 if (is_mi)
6203 {
6204 char mi_group[10];
6205
6206 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6207 ui_out_field_string (uiout, NULL, mi_group);
6208 }
6209 else
6210 {
6211 if (i == 0)
6212 ui_out_text (uiout, " inf ");
6213 else
6214 ui_out_text (uiout, ", ");
6215
6216 ui_out_text (uiout, plongest (inf));
6217 }
6218 }
6219
6220 do_cleanups (back_to);
6221}
6222
269b11a2
PA
6223/* Print B to gdb_stdout. */
6224
6225static void
6226print_one_breakpoint_location (struct breakpoint *b,
6227 struct bp_location *loc,
6228 int loc_number,
6229 struct bp_location **last_loc,
269b11a2
PA
6230 int allflag)
6231{
6232 struct command_line *l;
c2c6d25f 6233 static char bpenables[] = "nynny";
c906108c 6234
79a45e25 6235 struct ui_out *uiout = current_uiout;
0d381245
VP
6236 int header_of_multiple = 0;
6237 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6238 struct value_print_options opts;
6239
6240 get_user_print_options (&opts);
0d381245
VP
6241
6242 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6243 /* See comment in print_one_breakpoint concerning treatment of
6244 breakpoints with single disabled location. */
0d381245
VP
6245 if (loc == NULL
6246 && (b->loc != NULL
6247 && (b->loc->next != NULL || !b->loc->enabled)))
6248 header_of_multiple = 1;
6249 if (loc == NULL)
6250 loc = b->loc;
6251
c4093a6a
JM
6252 annotate_record ();
6253
6254 /* 1 */
6255 annotate_field (0);
0d381245
VP
6256 if (part_of_multiple)
6257 {
6258 char *formatted;
0c6773c1 6259 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6260 ui_out_field_string (uiout, "number", formatted);
6261 xfree (formatted);
6262 }
6263 else
6264 {
6265 ui_out_field_int (uiout, "number", b->number);
6266 }
c4093a6a
JM
6267
6268 /* 2 */
6269 annotate_field (1);
0d381245
VP
6270 if (part_of_multiple)
6271 ui_out_field_skip (uiout, "type");
269b11a2
PA
6272 else
6273 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6274
6275 /* 3 */
6276 annotate_field (2);
0d381245
VP
6277 if (part_of_multiple)
6278 ui_out_field_skip (uiout, "disp");
6279 else
2cec12e5 6280 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6281
c4093a6a
JM
6282
6283 /* 4 */
6284 annotate_field (3);
0d381245 6285 if (part_of_multiple)
54e52265 6286 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6287 else
4a64f543
MS
6288 ui_out_field_fmt (uiout, "enabled", "%c",
6289 bpenables[(int) b->enable_state]);
54e52265 6290 ui_out_spaces (uiout, 2);
0d381245 6291
c4093a6a
JM
6292
6293 /* 5 and 6 */
3086aeae 6294 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6295 {
4a64f543
MS
6296 /* Although the print_one can possibly print all locations,
6297 calling it here is not likely to get any nice result. So,
6298 make sure there's just one location. */
0d381245 6299 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6300 b->ops->print_one (b, last_loc);
0d381245 6301 }
3086aeae
DJ
6302 else
6303 switch (b->type)
6304 {
6305 case bp_none:
6306 internal_error (__FILE__, __LINE__,
e2e0b3e5 6307 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6308 break;
c906108c 6309
3086aeae
DJ
6310 case bp_watchpoint:
6311 case bp_hardware_watchpoint:
6312 case bp_read_watchpoint:
6313 case bp_access_watchpoint:
3a5c3e22
PA
6314 {
6315 struct watchpoint *w = (struct watchpoint *) b;
6316
6317 /* Field 4, the address, is omitted (which makes the columns
6318 not line up too nicely with the headers, but the effect
6319 is relatively readable). */
6320 if (opts.addressprint)
6321 ui_out_field_skip (uiout, "addr");
6322 annotate_field (5);
6323 ui_out_field_string (uiout, "what", w->exp_string);
6324 }
3086aeae
DJ
6325 break;
6326
3086aeae
DJ
6327 case bp_breakpoint:
6328 case bp_hardware_breakpoint:
7c16b83e 6329 case bp_single_step:
3086aeae
DJ
6330 case bp_until:
6331 case bp_finish:
6332 case bp_longjmp:
6333 case bp_longjmp_resume:
e2e4d78b 6334 case bp_longjmp_call_dummy:
186c406b
TT
6335 case bp_exception:
6336 case bp_exception_resume:
3086aeae 6337 case bp_step_resume:
2c03e5be 6338 case bp_hp_step_resume:
3086aeae
DJ
6339 case bp_watchpoint_scope:
6340 case bp_call_dummy:
aa7d318d 6341 case bp_std_terminate:
3086aeae
DJ
6342 case bp_shlib_event:
6343 case bp_thread_event:
6344 case bp_overlay_event:
0fd8e87f 6345 case bp_longjmp_master:
aa7d318d 6346 case bp_std_terminate_master:
186c406b 6347 case bp_exception_master:
1042e4c0 6348 case bp_tracepoint:
7a697b8d 6349 case bp_fast_tracepoint:
0fb4aa4b 6350 case bp_static_tracepoint:
e7e0cddf 6351 case bp_dprintf:
4efc6507 6352 case bp_jit_event:
0e30163f
JK
6353 case bp_gnu_ifunc_resolver:
6354 case bp_gnu_ifunc_resolver_return:
79a45b7d 6355 if (opts.addressprint)
3086aeae
DJ
6356 {
6357 annotate_field (4);
54e52265 6358 if (header_of_multiple)
0d381245 6359 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6360 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6361 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6362 else
5af949e3
UW
6363 ui_out_field_core_addr (uiout, "addr",
6364 loc->gdbarch, loc->address);
3086aeae
DJ
6365 }
6366 annotate_field (5);
0d381245 6367 if (!header_of_multiple)
170b53b2 6368 print_breakpoint_location (b, loc);
0d381245 6369 if (b->loc)
a6d9a66e 6370 *last_loc = b->loc;
3086aeae
DJ
6371 break;
6372 }
c906108c 6373
6c95b8df 6374
998580f1 6375 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6376 {
6377 struct inferior *inf;
998580f1
MK
6378 VEC(int) *inf_num = NULL;
6379 int mi_only = 1;
6c95b8df 6380
998580f1 6381 ALL_INFERIORS (inf)
6c95b8df
PA
6382 {
6383 if (inf->pspace == loc->pspace)
998580f1 6384 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6385 }
998580f1
MK
6386
6387 /* For backward compatibility, don't display inferiors in CLI unless
6388 there are several. Always display for MI. */
6389 if (allflag
6390 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6391 && (number_of_program_spaces () > 1
6392 || number_of_inferiors () > 1)
6393 /* LOC is for existing B, it cannot be in
6394 moribund_locations and thus having NULL OWNER. */
6395 && loc->owner->type != bp_catchpoint))
6396 mi_only = 0;
6397 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6398 VEC_free (int, inf_num);
6c95b8df
PA
6399 }
6400
4a306c9a 6401 if (!part_of_multiple)
c4093a6a 6402 {
4a306c9a
JB
6403 if (b->thread != -1)
6404 {
6405 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6406 "stop only in" line a little further down. */
4a306c9a
JB
6407 ui_out_text (uiout, " thread ");
6408 ui_out_field_int (uiout, "thread", b->thread);
6409 }
6410 else if (b->task != 0)
6411 {
6412 ui_out_text (uiout, " task ");
6413 ui_out_field_int (uiout, "task", b->task);
6414 }
c4093a6a 6415 }
f1310107 6416
8b93c638 6417 ui_out_text (uiout, "\n");
f1310107 6418
348d480f 6419 if (!part_of_multiple)
f1310107
TJB
6420 b->ops->print_one_detail (b, uiout);
6421
0d381245 6422 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6423 {
6424 annotate_field (6);
8b93c638 6425 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6426 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6427 the frame ID. */
5af949e3
UW
6428 ui_out_field_core_addr (uiout, "frame",
6429 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6430 ui_out_text (uiout, "\n");
c4093a6a
JM
6431 }
6432
28010a5d 6433 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6434 {
6435 annotate_field (7);
d77f58be 6436 if (is_tracepoint (b))
1042e4c0
SS
6437 ui_out_text (uiout, "\ttrace only if ");
6438 else
6439 ui_out_text (uiout, "\tstop only if ");
0101ce28 6440 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6441
6442 /* Print whether the target is doing the breakpoint's condition
6443 evaluation. If GDB is doing the evaluation, don't print anything. */
6444 if (is_breakpoint (b)
6445 && breakpoint_condition_evaluation_mode ()
6446 == condition_evaluation_target)
6447 {
6448 ui_out_text (uiout, " (");
6449 ui_out_field_string (uiout, "evaluated-by",
6450 bp_condition_evaluator (b));
6451 ui_out_text (uiout, " evals)");
6452 }
0101ce28
JJ
6453 ui_out_text (uiout, "\n");
6454 }
6455
0d381245 6456 if (!part_of_multiple && b->thread != -1)
c4093a6a 6457 {
4a64f543 6458 /* FIXME should make an annotation for this. */
8b93c638
JM
6459 ui_out_text (uiout, "\tstop only in thread ");
6460 ui_out_field_int (uiout, "thread", b->thread);
6461 ui_out_text (uiout, "\n");
c4093a6a
JM
6462 }
6463
556ec64d
YQ
6464 if (!part_of_multiple)
6465 {
6466 if (b->hit_count)
31f56a27
YQ
6467 {
6468 /* FIXME should make an annotation for this. */
6469 if (is_catchpoint (b))
6470 ui_out_text (uiout, "\tcatchpoint");
6471 else if (is_tracepoint (b))
6472 ui_out_text (uiout, "\ttracepoint");
6473 else
6474 ui_out_text (uiout, "\tbreakpoint");
6475 ui_out_text (uiout, " already hit ");
6476 ui_out_field_int (uiout, "times", b->hit_count);
6477 if (b->hit_count == 1)
6478 ui_out_text (uiout, " time\n");
6479 else
6480 ui_out_text (uiout, " times\n");
6481 }
556ec64d
YQ
6482 else
6483 {
31f56a27
YQ
6484 /* Output the count also if it is zero, but only if this is mi. */
6485 if (ui_out_is_mi_like_p (uiout))
6486 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6487 }
6488 }
8b93c638 6489
0d381245 6490 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6491 {
6492 annotate_field (8);
8b93c638
JM
6493 ui_out_text (uiout, "\tignore next ");
6494 ui_out_field_int (uiout, "ignore", b->ignore_count);
6495 ui_out_text (uiout, " hits\n");
c4093a6a 6496 }
059fb39f 6497
816338b5
SS
6498 /* Note that an enable count of 1 corresponds to "enable once"
6499 behavior, which is reported by the combination of enablement and
6500 disposition, so we don't need to mention it here. */
6501 if (!part_of_multiple && b->enable_count > 1)
6502 {
6503 annotate_field (8);
6504 ui_out_text (uiout, "\tdisable after ");
6505 /* Tweak the wording to clarify that ignore and enable counts
6506 are distinct, and have additive effect. */
6507 if (b->ignore_count)
6508 ui_out_text (uiout, "additional ");
6509 else
6510 ui_out_text (uiout, "next ");
6511 ui_out_field_int (uiout, "enable", b->enable_count);
6512 ui_out_text (uiout, " hits\n");
6513 }
6514
f196051f
SS
6515 if (!part_of_multiple && is_tracepoint (b))
6516 {
6517 struct tracepoint *tp = (struct tracepoint *) b;
6518
6519 if (tp->traceframe_usage)
6520 {
6521 ui_out_text (uiout, "\ttrace buffer usage ");
6522 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6523 ui_out_text (uiout, " bytes\n");
6524 }
6525 }
d3ce09f5 6526
9add0f1b 6527 l = b->commands ? b->commands->commands : NULL;
059fb39f 6528 if (!part_of_multiple && l)
c4093a6a 6529 {
3b31d625
EZ
6530 struct cleanup *script_chain;
6531
c4093a6a 6532 annotate_field (9);
3b31d625 6533 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6534 print_command_lines (uiout, l, 4);
3b31d625 6535 do_cleanups (script_chain);
c4093a6a 6536 }
d24317b4 6537
d9b3f62e 6538 if (is_tracepoint (b))
1042e4c0 6539 {
d9b3f62e
PA
6540 struct tracepoint *t = (struct tracepoint *) b;
6541
6542 if (!part_of_multiple && t->pass_count)
6543 {
6544 annotate_field (10);
6545 ui_out_text (uiout, "\tpass count ");
6546 ui_out_field_int (uiout, "pass", t->pass_count);
6547 ui_out_text (uiout, " \n");
6548 }
f2a8bc8a
YQ
6549
6550 /* Don't display it when tracepoint or tracepoint location is
6551 pending. */
6552 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6553 {
6554 annotate_field (11);
6555
6556 if (ui_out_is_mi_like_p (uiout))
6557 ui_out_field_string (uiout, "installed",
6558 loc->inserted ? "y" : "n");
6559 else
6560 {
6561 if (loc->inserted)
6562 ui_out_text (uiout, "\t");
6563 else
6564 ui_out_text (uiout, "\tnot ");
6565 ui_out_text (uiout, "installed on target\n");
6566 }
6567 }
1042e4c0
SS
6568 }
6569
d24317b4
VP
6570 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6571 {
3a5c3e22
PA
6572 if (is_watchpoint (b))
6573 {
6574 struct watchpoint *w = (struct watchpoint *) b;
6575
6576 ui_out_field_string (uiout, "original-location", w->exp_string);
6577 }
6578 else if (b->addr_string)
d24317b4 6579 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6580 }
c4093a6a 6581}
c5aa993b 6582
0d381245
VP
6583static void
6584print_one_breakpoint (struct breakpoint *b,
4a64f543 6585 struct bp_location **last_loc,
6c95b8df 6586 int allflag)
0d381245 6587{
8d3788bd 6588 struct cleanup *bkpt_chain;
79a45e25 6589 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6590
6591 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6592
12c5a436 6593 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6594 do_cleanups (bkpt_chain);
0d381245
VP
6595
6596 /* If this breakpoint has custom print function,
6597 it's already printed. Otherwise, print individual
6598 locations, if any. */
6599 if (b->ops == NULL || b->ops->print_one == NULL)
6600 {
4a64f543
MS
6601 /* If breakpoint has a single location that is disabled, we
6602 print it as if it had several locations, since otherwise it's
6603 hard to represent "breakpoint enabled, location disabled"
6604 situation.
6605
6606 Note that while hardware watchpoints have several locations
a3be7890 6607 internally, that's not a property exposed to user. */
0d381245 6608 if (b->loc
a5606eee 6609 && !is_hardware_watchpoint (b)
8d3788bd 6610 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6611 {
6612 struct bp_location *loc;
6613 int n = 1;
8d3788bd 6614
0d381245 6615 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6616 {
6617 struct cleanup *inner2 =
6618 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6619 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6620 do_cleanups (inner2);
6621 }
0d381245
VP
6622 }
6623 }
6624}
6625
a6d9a66e
UW
6626static int
6627breakpoint_address_bits (struct breakpoint *b)
6628{
6629 int print_address_bits = 0;
6630 struct bp_location *loc;
6631
6632 for (loc = b->loc; loc; loc = loc->next)
6633 {
c7437ca6
PA
6634 int addr_bit;
6635
6636 /* Software watchpoints that aren't watching memory don't have
6637 an address to print. */
6638 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6639 continue;
6640
6641 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6642 if (addr_bit > print_address_bits)
6643 print_address_bits = addr_bit;
6644 }
6645
6646 return print_address_bits;
6647}
0d381245 6648
c4093a6a
JM
6649struct captured_breakpoint_query_args
6650 {
6651 int bnum;
6652 };
c5aa993b 6653
c4093a6a 6654static int
2b65245e 6655do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6656{
6657 struct captured_breakpoint_query_args *args = data;
52f0bd74 6658 struct breakpoint *b;
a6d9a66e 6659 struct bp_location *dummy_loc = NULL;
cc59ec59 6660
c4093a6a
JM
6661 ALL_BREAKPOINTS (b)
6662 {
6663 if (args->bnum == b->number)
c5aa993b 6664 {
12c5a436 6665 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6666 return GDB_RC_OK;
c5aa993b 6667 }
c4093a6a
JM
6668 }
6669 return GDB_RC_NONE;
6670}
c5aa993b 6671
c4093a6a 6672enum gdb_rc
4a64f543
MS
6673gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6674 char **error_message)
c4093a6a
JM
6675{
6676 struct captured_breakpoint_query_args args;
cc59ec59 6677
c4093a6a
JM
6678 args.bnum = bnum;
6679 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6680 an error. */
b0b13bb4
DJ
6681 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6682 error_message, RETURN_MASK_ALL) < 0)
6683 return GDB_RC_FAIL;
6684 else
6685 return GDB_RC_OK;
c4093a6a 6686}
c5aa993b 6687
09d682a4
TT
6688/* Return true if this breakpoint was set by the user, false if it is
6689 internal or momentary. */
6690
6691int
6692user_breakpoint_p (struct breakpoint *b)
6693{
46c6471b 6694 return b->number > 0;
09d682a4
TT
6695}
6696
7f3b0473 6697/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6698 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6699 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6700 FILTER is non-NULL, call it on each breakpoint and only include the
6701 ones for which it returns non-zero. Return the total number of
6702 breakpoints listed. */
c906108c 6703
d77f58be 6704static int
e5a67952 6705breakpoint_1 (char *args, int allflag,
4a64f543 6706 int (*filter) (const struct breakpoint *))
c4093a6a 6707{
52f0bd74 6708 struct breakpoint *b;
a6d9a66e 6709 struct bp_location *last_loc = NULL;
7f3b0473 6710 int nr_printable_breakpoints;
3b31d625 6711 struct cleanup *bkpttbl_chain;
79a45b7d 6712 struct value_print_options opts;
a6d9a66e 6713 int print_address_bits = 0;
269b11a2 6714 int print_type_col_width = 14;
79a45e25 6715 struct ui_out *uiout = current_uiout;
269b11a2 6716
79a45b7d
TT
6717 get_user_print_options (&opts);
6718
4a64f543
MS
6719 /* Compute the number of rows in the table, as well as the size
6720 required for address fields. */
7f3b0473
AC
6721 nr_printable_breakpoints = 0;
6722 ALL_BREAKPOINTS (b)
e5a67952
MS
6723 {
6724 /* If we have a filter, only list the breakpoints it accepts. */
6725 if (filter && !filter (b))
6726 continue;
6727
6728 /* If we have an "args" string, it is a list of breakpoints to
6729 accept. Skip the others. */
6730 if (args != NULL && *args != '\0')
6731 {
6732 if (allflag && parse_and_eval_long (args) != b->number)
6733 continue;
6734 if (!allflag && !number_is_in_list (args, b->number))
6735 continue;
6736 }
269b11a2 6737
e5a67952
MS
6738 if (allflag || user_breakpoint_p (b))
6739 {
6740 int addr_bit, type_len;
a6d9a66e 6741
e5a67952
MS
6742 addr_bit = breakpoint_address_bits (b);
6743 if (addr_bit > print_address_bits)
6744 print_address_bits = addr_bit;
269b11a2 6745
e5a67952
MS
6746 type_len = strlen (bptype_string (b->type));
6747 if (type_len > print_type_col_width)
6748 print_type_col_width = type_len;
6749
6750 nr_printable_breakpoints++;
6751 }
6752 }
7f3b0473 6753
79a45b7d 6754 if (opts.addressprint)
3b31d625 6755 bkpttbl_chain
3e43a32a
MS
6756 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6757 nr_printable_breakpoints,
3b31d625 6758 "BreakpointTable");
8b93c638 6759 else
3b31d625 6760 bkpttbl_chain
3e43a32a
MS
6761 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6762 nr_printable_breakpoints,
3b31d625 6763 "BreakpointTable");
8b93c638 6764
7f3b0473 6765 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6766 annotate_breakpoints_headers ();
6767 if (nr_printable_breakpoints > 0)
6768 annotate_field (0);
4a64f543 6769 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6770 if (nr_printable_breakpoints > 0)
6771 annotate_field (1);
269b11a2 6772 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6773 "type", "Type"); /* 2 */
d7faa9e7
AC
6774 if (nr_printable_breakpoints > 0)
6775 annotate_field (2);
4a64f543 6776 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6777 if (nr_printable_breakpoints > 0)
6778 annotate_field (3);
54e52265 6779 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6780 if (opts.addressprint)
e5a67952
MS
6781 {
6782 if (nr_printable_breakpoints > 0)
6783 annotate_field (4);
6784 if (print_address_bits <= 32)
6785 ui_out_table_header (uiout, 10, ui_left,
6786 "addr", "Address"); /* 5 */
6787 else
6788 ui_out_table_header (uiout, 18, ui_left,
6789 "addr", "Address"); /* 5 */
6790 }
d7faa9e7
AC
6791 if (nr_printable_breakpoints > 0)
6792 annotate_field (5);
6793 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6794 ui_out_table_body (uiout);
6795 if (nr_printable_breakpoints > 0)
6796 annotate_breakpoints_table ();
7f3b0473 6797
c4093a6a 6798 ALL_BREAKPOINTS (b)
e5a67952
MS
6799 {
6800 QUIT;
6801 /* If we have a filter, only list the breakpoints it accepts. */
6802 if (filter && !filter (b))
6803 continue;
6804
6805 /* If we have an "args" string, it is a list of breakpoints to
6806 accept. Skip the others. */
6807
6808 if (args != NULL && *args != '\0')
6809 {
6810 if (allflag) /* maintenance info breakpoint */
6811 {
6812 if (parse_and_eval_long (args) != b->number)
6813 continue;
6814 }
6815 else /* all others */
6816 {
6817 if (!number_is_in_list (args, b->number))
6818 continue;
6819 }
6820 }
6821 /* We only print out user settable breakpoints unless the
6822 allflag is set. */
6823 if (allflag || user_breakpoint_p (b))
12c5a436 6824 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6825 }
6826
3b31d625 6827 do_cleanups (bkpttbl_chain);
698384cd 6828
7f3b0473 6829 if (nr_printable_breakpoints == 0)
c906108c 6830 {
4a64f543
MS
6831 /* If there's a filter, let the caller decide how to report
6832 empty list. */
d77f58be
SS
6833 if (!filter)
6834 {
e5a67952 6835 if (args == NULL || *args == '\0')
d77f58be
SS
6836 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6837 else
4a64f543 6838 ui_out_message (uiout, 0,
e5a67952
MS
6839 "No breakpoint or watchpoint matching '%s'.\n",
6840 args);
d77f58be 6841 }
c906108c
SS
6842 }
6843 else
c4093a6a 6844 {
a6d9a66e
UW
6845 if (last_loc && !server_command)
6846 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6847 }
c906108c 6848
4a64f543 6849 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6850 there have been breakpoints? */
c906108c 6851 annotate_breakpoints_table_end ();
d77f58be
SS
6852
6853 return nr_printable_breakpoints;
c906108c
SS
6854}
6855
ad443146
SS
6856/* Display the value of default-collect in a way that is generally
6857 compatible with the breakpoint list. */
6858
6859static void
6860default_collect_info (void)
6861{
79a45e25
PA
6862 struct ui_out *uiout = current_uiout;
6863
ad443146
SS
6864 /* If it has no value (which is frequently the case), say nothing; a
6865 message like "No default-collect." gets in user's face when it's
6866 not wanted. */
6867 if (!*default_collect)
6868 return;
6869
6870 /* The following phrase lines up nicely with per-tracepoint collect
6871 actions. */
6872 ui_out_text (uiout, "default collect ");
6873 ui_out_field_string (uiout, "default-collect", default_collect);
6874 ui_out_text (uiout, " \n");
6875}
6876
c906108c 6877static void
e5a67952 6878breakpoints_info (char *args, int from_tty)
c906108c 6879{
e5a67952 6880 breakpoint_1 (args, 0, NULL);
ad443146
SS
6881
6882 default_collect_info ();
d77f58be
SS
6883}
6884
6885static void
e5a67952 6886watchpoints_info (char *args, int from_tty)
d77f58be 6887{
e5a67952 6888 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6889 struct ui_out *uiout = current_uiout;
d77f58be
SS
6890
6891 if (num_printed == 0)
6892 {
e5a67952 6893 if (args == NULL || *args == '\0')
d77f58be
SS
6894 ui_out_message (uiout, 0, "No watchpoints.\n");
6895 else
e5a67952 6896 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6897 }
c906108c
SS
6898}
6899
7a292a7a 6900static void
e5a67952 6901maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6902{
e5a67952 6903 breakpoint_1 (args, 1, NULL);
ad443146
SS
6904
6905 default_collect_info ();
c906108c
SS
6906}
6907
0d381245 6908static int
714835d5 6909breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6910 struct program_space *pspace,
714835d5 6911 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6912{
6913 struct bp_location *bl = b->loc;
cc59ec59 6914
0d381245
VP
6915 for (; bl; bl = bl->next)
6916 {
6c95b8df
PA
6917 if (bl->pspace == pspace
6918 && bl->address == pc
0d381245
VP
6919 && (!overlay_debugging || bl->section == section))
6920 return 1;
6921 }
6922 return 0;
6923}
6924
672f9b60 6925/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6926 concerns with logical breakpoints, so we match program spaces, not
6927 address spaces. */
c906108c
SS
6928
6929static void
6c95b8df
PA
6930describe_other_breakpoints (struct gdbarch *gdbarch,
6931 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6932 struct obj_section *section, int thread)
c906108c 6933{
52f0bd74
AC
6934 int others = 0;
6935 struct breakpoint *b;
c906108c
SS
6936
6937 ALL_BREAKPOINTS (b)
672f9b60
KP
6938 others += (user_breakpoint_p (b)
6939 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6940 if (others > 0)
6941 {
a3f17187
AC
6942 if (others == 1)
6943 printf_filtered (_("Note: breakpoint "));
6944 else /* if (others == ???) */
6945 printf_filtered (_("Note: breakpoints "));
c906108c 6946 ALL_BREAKPOINTS (b)
672f9b60 6947 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6948 {
6949 others--;
6950 printf_filtered ("%d", b->number);
6951 if (b->thread == -1 && thread != -1)
6952 printf_filtered (" (all threads)");
6953 else if (b->thread != -1)
6954 printf_filtered (" (thread %d)", b->thread);
6955 printf_filtered ("%s%s ",
059fb39f 6956 ((b->enable_state == bp_disabled
f8eba3c6 6957 || b->enable_state == bp_call_disabled)
0d381245
VP
6958 ? " (disabled)"
6959 : b->enable_state == bp_permanent
6960 ? " (permanent)"
6961 : ""),
6962 (others > 1) ? ","
6963 : ((others == 1) ? " and" : ""));
6964 }
a3f17187 6965 printf_filtered (_("also set at pc "));
5af949e3 6966 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6967 printf_filtered (".\n");
6968 }
6969}
6970\f
c906108c 6971
e4f237da
KB
6972/* Return true iff it is meaningful to use the address member of
6973 BPT. For some breakpoint types, the address member is irrelevant
6974 and it makes no sense to attempt to compare it to other addresses
6975 (or use it for any other purpose either).
6976
4a64f543
MS
6977 More specifically, each of the following breakpoint types will
6978 always have a zero valued address and we don't want to mark
6979 breakpoints of any of these types to be a duplicate of an actual
6980 breakpoint at address zero:
e4f237da
KB
6981
6982 bp_watchpoint
2d134ed3
PA
6983 bp_catchpoint
6984
6985*/
e4f237da
KB
6986
6987static int
6988breakpoint_address_is_meaningful (struct breakpoint *bpt)
6989{
6990 enum bptype type = bpt->type;
6991
2d134ed3
PA
6992 return (type != bp_watchpoint && type != bp_catchpoint);
6993}
6994
6995/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6996 true if LOC1 and LOC2 represent the same watchpoint location. */
6997
6998static int
4a64f543
MS
6999watchpoint_locations_match (struct bp_location *loc1,
7000 struct bp_location *loc2)
2d134ed3 7001{
3a5c3e22
PA
7002 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7003 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7004
7005 /* Both of them must exist. */
7006 gdb_assert (w1 != NULL);
7007 gdb_assert (w2 != NULL);
2bdf28a0 7008
4a64f543
MS
7009 /* If the target can evaluate the condition expression in hardware,
7010 then we we need to insert both watchpoints even if they are at
7011 the same place. Otherwise the watchpoint will only trigger when
7012 the condition of whichever watchpoint was inserted evaluates to
7013 true, not giving a chance for GDB to check the condition of the
7014 other watchpoint. */
3a5c3e22 7015 if ((w1->cond_exp
4a64f543
MS
7016 && target_can_accel_watchpoint_condition (loc1->address,
7017 loc1->length,
0cf6dd15 7018 loc1->watchpoint_type,
3a5c3e22
PA
7019 w1->cond_exp))
7020 || (w2->cond_exp
4a64f543
MS
7021 && target_can_accel_watchpoint_condition (loc2->address,
7022 loc2->length,
0cf6dd15 7023 loc2->watchpoint_type,
3a5c3e22 7024 w2->cond_exp)))
0cf6dd15
TJB
7025 return 0;
7026
85d721b8
PA
7027 /* Note that this checks the owner's type, not the location's. In
7028 case the target does not support read watchpoints, but does
7029 support access watchpoints, we'll have bp_read_watchpoint
7030 watchpoints with hw_access locations. Those should be considered
7031 duplicates of hw_read locations. The hw_read locations will
7032 become hw_access locations later. */
2d134ed3
PA
7033 return (loc1->owner->type == loc2->owner->type
7034 && loc1->pspace->aspace == loc2->pspace->aspace
7035 && loc1->address == loc2->address
7036 && loc1->length == loc2->length);
e4f237da
KB
7037}
7038
31e77af2 7039/* See breakpoint.h. */
6c95b8df 7040
31e77af2 7041int
6c95b8df
PA
7042breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7043 struct address_space *aspace2, CORE_ADDR addr2)
7044{
f5656ead 7045 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7046 || aspace1 == aspace2)
7047 && addr1 == addr2);
7048}
7049
f1310107
TJB
7050/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7051 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7052 matches ASPACE2. On targets that have global breakpoints, the address
7053 space doesn't really matter. */
7054
7055static int
7056breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7057 int len1, struct address_space *aspace2,
7058 CORE_ADDR addr2)
7059{
f5656ead 7060 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7061 || aspace1 == aspace2)
7062 && addr2 >= addr1 && addr2 < addr1 + len1);
7063}
7064
7065/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7066 a ranged breakpoint. In most targets, a match happens only if ASPACE
7067 matches the breakpoint's address space. On targets that have global
7068 breakpoints, the address space doesn't really matter. */
7069
7070static int
7071breakpoint_location_address_match (struct bp_location *bl,
7072 struct address_space *aspace,
7073 CORE_ADDR addr)
7074{
7075 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7076 aspace, addr)
7077 || (bl->length
7078 && breakpoint_address_match_range (bl->pspace->aspace,
7079 bl->address, bl->length,
7080 aspace, addr)));
7081}
7082
1e4d1764
YQ
7083/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7084 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7085 true, otherwise returns false. */
7086
7087static int
7088tracepoint_locations_match (struct bp_location *loc1,
7089 struct bp_location *loc2)
7090{
7091 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7092 /* Since tracepoint locations are never duplicated with others', tracepoint
7093 locations at the same address of different tracepoints are regarded as
7094 different locations. */
7095 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7096 else
7097 return 0;
7098}
7099
2d134ed3
PA
7100/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7101 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7102 represent the same location. */
7103
7104static int
4a64f543
MS
7105breakpoint_locations_match (struct bp_location *loc1,
7106 struct bp_location *loc2)
2d134ed3 7107{
2bdf28a0
JK
7108 int hw_point1, hw_point2;
7109
7110 /* Both of them must not be in moribund_locations. */
7111 gdb_assert (loc1->owner != NULL);
7112 gdb_assert (loc2->owner != NULL);
7113
7114 hw_point1 = is_hardware_watchpoint (loc1->owner);
7115 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7116
7117 if (hw_point1 != hw_point2)
7118 return 0;
7119 else if (hw_point1)
7120 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7121 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7122 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7123 else
f1310107
TJB
7124 /* We compare bp_location.length in order to cover ranged breakpoints. */
7125 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7126 loc2->pspace->aspace, loc2->address)
7127 && loc1->length == loc2->length);
2d134ed3
PA
7128}
7129
76897487
KB
7130static void
7131breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7132 int bnum, int have_bnum)
7133{
f63fbe86
MS
7134 /* The longest string possibly returned by hex_string_custom
7135 is 50 chars. These must be at least that big for safety. */
7136 char astr1[64];
7137 char astr2[64];
76897487 7138
bb599908
PH
7139 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7140 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7141 if (have_bnum)
8a3fe4f8 7142 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7143 bnum, astr1, astr2);
7144 else
8a3fe4f8 7145 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7146}
7147
4a64f543
MS
7148/* Adjust a breakpoint's address to account for architectural
7149 constraints on breakpoint placement. Return the adjusted address.
7150 Note: Very few targets require this kind of adjustment. For most
7151 targets, this function is simply the identity function. */
76897487
KB
7152
7153static CORE_ADDR
a6d9a66e
UW
7154adjust_breakpoint_address (struct gdbarch *gdbarch,
7155 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7156{
a6d9a66e 7157 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7158 {
7159 /* Very few targets need any kind of breakpoint adjustment. */
7160 return bpaddr;
7161 }
88f7da05
KB
7162 else if (bptype == bp_watchpoint
7163 || bptype == bp_hardware_watchpoint
7164 || bptype == bp_read_watchpoint
7165 || bptype == bp_access_watchpoint
fe798b75 7166 || bptype == bp_catchpoint)
88f7da05
KB
7167 {
7168 /* Watchpoints and the various bp_catch_* eventpoints should not
7169 have their addresses modified. */
7170 return bpaddr;
7171 }
7c16b83e
PA
7172 else if (bptype == bp_single_step)
7173 {
7174 /* Single-step breakpoints should not have their addresses
7175 modified. If there's any architectural constrain that
7176 applies to this address, then it should have already been
7177 taken into account when the breakpoint was created in the
7178 first place. If we didn't do this, stepping through e.g.,
7179 Thumb-2 IT blocks would break. */
7180 return bpaddr;
7181 }
76897487
KB
7182 else
7183 {
7184 CORE_ADDR adjusted_bpaddr;
7185
7186 /* Some targets have architectural constraints on the placement
7187 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7188 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7189
7190 /* An adjusted breakpoint address can significantly alter
7191 a user's expectations. Print a warning if an adjustment
7192 is required. */
7193 if (adjusted_bpaddr != bpaddr)
7194 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7195
7196 return adjusted_bpaddr;
7197 }
7198}
7199
28010a5d
PA
7200void
7201init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7202 struct breakpoint *owner)
7cc221ef 7203{
7cc221ef
DJ
7204 memset (loc, 0, sizeof (*loc));
7205
348d480f
PA
7206 gdb_assert (ops != NULL);
7207
28010a5d
PA
7208 loc->ops = ops;
7209 loc->owner = owner;
511a6cd4 7210 loc->cond = NULL;
b775012e 7211 loc->cond_bytecode = NULL;
0d381245
VP
7212 loc->shlib_disabled = 0;
7213 loc->enabled = 1;
e049a4b5 7214
28010a5d 7215 switch (owner->type)
e049a4b5
DJ
7216 {
7217 case bp_breakpoint:
7c16b83e 7218 case bp_single_step:
e049a4b5
DJ
7219 case bp_until:
7220 case bp_finish:
7221 case bp_longjmp:
7222 case bp_longjmp_resume:
e2e4d78b 7223 case bp_longjmp_call_dummy:
186c406b
TT
7224 case bp_exception:
7225 case bp_exception_resume:
e049a4b5 7226 case bp_step_resume:
2c03e5be 7227 case bp_hp_step_resume:
e049a4b5
DJ
7228 case bp_watchpoint_scope:
7229 case bp_call_dummy:
aa7d318d 7230 case bp_std_terminate:
e049a4b5
DJ
7231 case bp_shlib_event:
7232 case bp_thread_event:
7233 case bp_overlay_event:
4efc6507 7234 case bp_jit_event:
0fd8e87f 7235 case bp_longjmp_master:
aa7d318d 7236 case bp_std_terminate_master:
186c406b 7237 case bp_exception_master:
0e30163f
JK
7238 case bp_gnu_ifunc_resolver:
7239 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7240 case bp_dprintf:
e049a4b5 7241 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7242 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7243 break;
7244 case bp_hardware_breakpoint:
7245 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7246 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7247 break;
7248 case bp_hardware_watchpoint:
7249 case bp_read_watchpoint:
7250 case bp_access_watchpoint:
7251 loc->loc_type = bp_loc_hardware_watchpoint;
7252 break;
7253 case bp_watchpoint:
ce78b96d 7254 case bp_catchpoint:
15c3d785
PA
7255 case bp_tracepoint:
7256 case bp_fast_tracepoint:
0fb4aa4b 7257 case bp_static_tracepoint:
e049a4b5
DJ
7258 loc->loc_type = bp_loc_other;
7259 break;
7260 default:
e2e0b3e5 7261 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7262 }
7263
f431efe5 7264 loc->refc = 1;
28010a5d
PA
7265}
7266
7267/* Allocate a struct bp_location. */
7268
7269static struct bp_location *
7270allocate_bp_location (struct breakpoint *bpt)
7271{
348d480f
PA
7272 return bpt->ops->allocate_location (bpt);
7273}
7cc221ef 7274
f431efe5
PA
7275static void
7276free_bp_location (struct bp_location *loc)
fe3f5fa8 7277{
348d480f 7278 loc->ops->dtor (loc);
fe3f5fa8
VP
7279 xfree (loc);
7280}
7281
f431efe5
PA
7282/* Increment reference count. */
7283
7284static void
7285incref_bp_location (struct bp_location *bl)
7286{
7287 ++bl->refc;
7288}
7289
7290/* Decrement reference count. If the reference count reaches 0,
7291 destroy the bp_location. Sets *BLP to NULL. */
7292
7293static void
7294decref_bp_location (struct bp_location **blp)
7295{
0807b50c
PA
7296 gdb_assert ((*blp)->refc > 0);
7297
f431efe5
PA
7298 if (--(*blp)->refc == 0)
7299 free_bp_location (*blp);
7300 *blp = NULL;
7301}
7302
346774a9 7303/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7304
346774a9
PA
7305static void
7306add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7307{
346774a9 7308 struct breakpoint *b1;
c906108c 7309
346774a9
PA
7310 /* Add this breakpoint to the end of the chain so that a list of
7311 breakpoints will come out in order of increasing numbers. */
7312
7313 b1 = breakpoint_chain;
7314 if (b1 == 0)
7315 breakpoint_chain = b;
7316 else
7317 {
7318 while (b1->next)
7319 b1 = b1->next;
7320 b1->next = b;
7321 }
7322}
7323
7324/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7325
7326static void
7327init_raw_breakpoint_without_location (struct breakpoint *b,
7328 struct gdbarch *gdbarch,
28010a5d 7329 enum bptype bptype,
c0a91b2b 7330 const struct breakpoint_ops *ops)
346774a9 7331{
c906108c 7332 memset (b, 0, sizeof (*b));
2219d63c 7333
348d480f
PA
7334 gdb_assert (ops != NULL);
7335
28010a5d 7336 b->ops = ops;
4d28f7a8 7337 b->type = bptype;
a6d9a66e 7338 b->gdbarch = gdbarch;
c906108c
SS
7339 b->language = current_language->la_language;
7340 b->input_radix = input_radix;
7341 b->thread = -1;
b5de0fa7 7342 b->enable_state = bp_enabled;
c906108c
SS
7343 b->next = 0;
7344 b->silent = 0;
7345 b->ignore_count = 0;
7346 b->commands = NULL;
818dd999 7347 b->frame_id = null_frame_id;
0d381245 7348 b->condition_not_parsed = 0;
84f4c1fe 7349 b->py_bp_object = NULL;
d0fb5eae 7350 b->related_breakpoint = b;
346774a9
PA
7351}
7352
7353/* Helper to set_raw_breakpoint below. Creates a breakpoint
7354 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7355
7356static struct breakpoint *
7357set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7358 enum bptype bptype,
c0a91b2b 7359 const struct breakpoint_ops *ops)
346774a9
PA
7360{
7361 struct breakpoint *b = XNEW (struct breakpoint);
7362
348d480f 7363 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7364 add_to_breakpoint_chain (b);
0d381245
VP
7365 return b;
7366}
7367
0e30163f
JK
7368/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7369 resolutions should be made as the user specified the location explicitly
7370 enough. */
7371
0d381245 7372static void
0e30163f 7373set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7374{
2bdf28a0
JK
7375 gdb_assert (loc->owner != NULL);
7376
0d381245 7377 if (loc->owner->type == bp_breakpoint
1042e4c0 7378 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7379 || is_tracepoint (loc->owner))
0d381245 7380 {
0e30163f 7381 int is_gnu_ifunc;
2c02bd72 7382 const char *function_name;
6a3a010b 7383 CORE_ADDR func_addr;
0e30163f 7384
2c02bd72 7385 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7386 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7387
7388 if (is_gnu_ifunc && !explicit_loc)
7389 {
7390 struct breakpoint *b = loc->owner;
7391
7392 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7393 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7394 &loc->requested_address))
7395 {
7396 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7397 loc->address = adjust_breakpoint_address (loc->gdbarch,
7398 loc->requested_address,
7399 b->type);
7400 }
7401 else if (b->type == bp_breakpoint && b->loc == loc
7402 && loc->next == NULL && b->related_breakpoint == b)
7403 {
7404 /* Create only the whole new breakpoint of this type but do not
7405 mess more complicated breakpoints with multiple locations. */
7406 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7407 /* Remember the resolver's address for use by the return
7408 breakpoint. */
7409 loc->related_address = func_addr;
0e30163f
JK
7410 }
7411 }
7412
2c02bd72
DE
7413 if (function_name)
7414 loc->function_name = xstrdup (function_name);
0d381245
VP
7415 }
7416}
7417
a6d9a66e 7418/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7419struct gdbarch *
a6d9a66e
UW
7420get_sal_arch (struct symtab_and_line sal)
7421{
7422 if (sal.section)
7423 return get_objfile_arch (sal.section->objfile);
7424 if (sal.symtab)
7425 return get_objfile_arch (sal.symtab->objfile);
7426
7427 return NULL;
7428}
7429
346774a9
PA
7430/* Low level routine for partially initializing a breakpoint of type
7431 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7432 file name, and line number are provided by SAL.
0d381245
VP
7433
7434 It is expected that the caller will complete the initialization of
7435 the newly created breakpoint struct as well as output any status
c56053d2 7436 information regarding the creation of a new breakpoint. */
0d381245 7437
346774a9
PA
7438static void
7439init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7440 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7441 const struct breakpoint_ops *ops)
0d381245 7442{
28010a5d 7443 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7444
3742cc8b 7445 add_location_to_breakpoint (b, &sal);
0d381245 7446
6c95b8df
PA
7447 if (bptype != bp_catchpoint)
7448 gdb_assert (sal.pspace != NULL);
7449
f8eba3c6
TT
7450 /* Store the program space that was used to set the breakpoint,
7451 except for ordinary breakpoints, which are independent of the
7452 program space. */
7453 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7454 b->pspace = sal.pspace;
346774a9 7455}
c906108c 7456
346774a9
PA
7457/* set_raw_breakpoint is a low level routine for allocating and
7458 partially initializing a breakpoint of type BPTYPE. The newly
7459 created breakpoint's address, section, source file name, and line
7460 number are provided by SAL. The newly created and partially
7461 initialized breakpoint is added to the breakpoint chain and
7462 is also returned as the value of this function.
7463
7464 It is expected that the caller will complete the initialization of
7465 the newly created breakpoint struct as well as output any status
7466 information regarding the creation of a new breakpoint. In
7467 particular, set_raw_breakpoint does NOT set the breakpoint
7468 number! Care should be taken to not allow an error to occur
7469 prior to completing the initialization of the breakpoint. If this
7470 should happen, a bogus breakpoint will be left on the chain. */
7471
7472struct breakpoint *
7473set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7474 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7475 const struct breakpoint_ops *ops)
346774a9
PA
7476{
7477 struct breakpoint *b = XNEW (struct breakpoint);
7478
348d480f 7479 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7480 add_to_breakpoint_chain (b);
c906108c
SS
7481 return b;
7482}
7483
c2c6d25f
JM
7484
7485/* Note that the breakpoint object B describes a permanent breakpoint
7486 instruction, hard-wired into the inferior's code. */
7487void
7488make_breakpoint_permanent (struct breakpoint *b)
7489{
0d381245 7490 struct bp_location *bl;
cc59ec59 7491
b5de0fa7 7492 b->enable_state = bp_permanent;
c2c6d25f 7493
4a64f543
MS
7494 /* By definition, permanent breakpoints are already present in the
7495 code. Mark all locations as inserted. For now,
7496 make_breakpoint_permanent is called in just one place, so it's
7497 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7498 multiple locations or not, but it's easy to implement. */
0d381245
VP
7499 for (bl = b->loc; bl; bl = bl->next)
7500 bl->inserted = 1;
c2c6d25f
JM
7501}
7502
53a5351d 7503/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7504 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7505 initiated the operation. */
c906108c
SS
7506
7507void
186c406b 7508set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7509{
35df4500 7510 struct breakpoint *b, *b_tmp;
186c406b 7511 int thread = tp->num;
0fd8e87f
UW
7512
7513 /* To avoid having to rescan all objfile symbols at every step,
7514 we maintain a list of continually-inserted but always disabled
7515 longjmp "master" breakpoints. Here, we simply create momentary
7516 clones of those and enable them for the requested thread. */
35df4500 7517 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7518 if (b->pspace == current_program_space
186c406b
TT
7519 && (b->type == bp_longjmp_master
7520 || b->type == bp_exception_master))
0fd8e87f 7521 {
06edf0c0
PA
7522 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7523 struct breakpoint *clone;
cc59ec59 7524
e2e4d78b
JK
7525 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7526 after their removal. */
06edf0c0 7527 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7528 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7529 clone->thread = thread;
7530 }
186c406b
TT
7531
7532 tp->initiating_frame = frame;
c906108c
SS
7533}
7534
611c83ae 7535/* Delete all longjmp breakpoints from THREAD. */
c906108c 7536void
611c83ae 7537delete_longjmp_breakpoint (int thread)
c906108c 7538{
35df4500 7539 struct breakpoint *b, *b_tmp;
c906108c 7540
35df4500 7541 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7542 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7543 {
7544 if (b->thread == thread)
7545 delete_breakpoint (b);
7546 }
c906108c
SS
7547}
7548
f59f708a
PA
7549void
7550delete_longjmp_breakpoint_at_next_stop (int thread)
7551{
7552 struct breakpoint *b, *b_tmp;
7553
7554 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7555 if (b->type == bp_longjmp || b->type == bp_exception)
7556 {
7557 if (b->thread == thread)
7558 b->disposition = disp_del_at_next_stop;
7559 }
7560}
7561
e2e4d78b
JK
7562/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7563 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7564 pointer to any of them. Return NULL if this system cannot place longjmp
7565 breakpoints. */
7566
7567struct breakpoint *
7568set_longjmp_breakpoint_for_call_dummy (void)
7569{
7570 struct breakpoint *b, *retval = NULL;
7571
7572 ALL_BREAKPOINTS (b)
7573 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7574 {
7575 struct breakpoint *new_b;
7576
7577 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7578 &momentary_breakpoint_ops,
7579 1);
e2e4d78b
JK
7580 new_b->thread = pid_to_thread_id (inferior_ptid);
7581
7582 /* Link NEW_B into the chain of RETVAL breakpoints. */
7583
7584 gdb_assert (new_b->related_breakpoint == new_b);
7585 if (retval == NULL)
7586 retval = new_b;
7587 new_b->related_breakpoint = retval;
7588 while (retval->related_breakpoint != new_b->related_breakpoint)
7589 retval = retval->related_breakpoint;
7590 retval->related_breakpoint = new_b;
7591 }
7592
7593 return retval;
7594}
7595
7596/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7597 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7598 stack.
7599
7600 You should call this function only at places where it is safe to currently
7601 unwind the whole stack. Failed stack unwind would discard live dummy
7602 frames. */
7603
7604void
b67a2c6f 7605check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7606{
7607 struct breakpoint *b, *b_tmp;
7608
7609 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7610 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7611 {
7612 struct breakpoint *dummy_b = b->related_breakpoint;
7613
7614 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7615 dummy_b = dummy_b->related_breakpoint;
7616 if (dummy_b->type != bp_call_dummy
7617 || frame_find_by_id (dummy_b->frame_id) != NULL)
7618 continue;
7619
b67a2c6f 7620 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7621
7622 while (b->related_breakpoint != b)
7623 {
7624 if (b_tmp == b->related_breakpoint)
7625 b_tmp = b->related_breakpoint->next;
7626 delete_breakpoint (b->related_breakpoint);
7627 }
7628 delete_breakpoint (b);
7629 }
7630}
7631
1900040c
MS
7632void
7633enable_overlay_breakpoints (void)
7634{
52f0bd74 7635 struct breakpoint *b;
1900040c
MS
7636
7637 ALL_BREAKPOINTS (b)
7638 if (b->type == bp_overlay_event)
7639 {
7640 b->enable_state = bp_enabled;
44702360 7641 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7642 overlay_events_enabled = 1;
1900040c
MS
7643 }
7644}
7645
7646void
7647disable_overlay_breakpoints (void)
7648{
52f0bd74 7649 struct breakpoint *b;
1900040c
MS
7650
7651 ALL_BREAKPOINTS (b)
7652 if (b->type == bp_overlay_event)
7653 {
7654 b->enable_state = bp_disabled;
44702360 7655 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7656 overlay_events_enabled = 0;
1900040c
MS
7657 }
7658}
7659
aa7d318d
TT
7660/* Set an active std::terminate breakpoint for each std::terminate
7661 master breakpoint. */
7662void
7663set_std_terminate_breakpoint (void)
7664{
35df4500 7665 struct breakpoint *b, *b_tmp;
aa7d318d 7666
35df4500 7667 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7668 if (b->pspace == current_program_space
7669 && b->type == bp_std_terminate_master)
7670 {
06edf0c0 7671 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7672 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7673 }
7674}
7675
7676/* Delete all the std::terminate breakpoints. */
7677void
7678delete_std_terminate_breakpoint (void)
7679{
35df4500 7680 struct breakpoint *b, *b_tmp;
aa7d318d 7681
35df4500 7682 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7683 if (b->type == bp_std_terminate)
7684 delete_breakpoint (b);
7685}
7686
c4093a6a 7687struct breakpoint *
a6d9a66e 7688create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7689{
7690 struct breakpoint *b;
c4093a6a 7691
06edf0c0
PA
7692 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7693 &internal_breakpoint_ops);
7694
b5de0fa7 7695 b->enable_state = bp_enabled;
c4093a6a 7696 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7697 b->addr_string
7698 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7699
44702360 7700 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7701
c4093a6a
JM
7702 return b;
7703}
7704
7705void
7706remove_thread_event_breakpoints (void)
7707{
35df4500 7708 struct breakpoint *b, *b_tmp;
c4093a6a 7709
35df4500 7710 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7711 if (b->type == bp_thread_event
7712 && b->loc->pspace == current_program_space)
c4093a6a
JM
7713 delete_breakpoint (b);
7714}
7715
0101ce28
JJ
7716struct lang_and_radix
7717 {
7718 enum language lang;
7719 int radix;
7720 };
7721
4efc6507
DE
7722/* Create a breakpoint for JIT code registration and unregistration. */
7723
7724struct breakpoint *
7725create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7726{
7727 struct breakpoint *b;
7728
06edf0c0
PA
7729 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7730 &internal_breakpoint_ops);
44702360 7731 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7732 return b;
7733}
0101ce28 7734
03673fc7
PP
7735/* Remove JIT code registration and unregistration breakpoint(s). */
7736
7737void
7738remove_jit_event_breakpoints (void)
7739{
7740 struct breakpoint *b, *b_tmp;
7741
7742 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7743 if (b->type == bp_jit_event
7744 && b->loc->pspace == current_program_space)
7745 delete_breakpoint (b);
7746}
7747
cae688ec
JJ
7748void
7749remove_solib_event_breakpoints (void)
7750{
35df4500 7751 struct breakpoint *b, *b_tmp;
cae688ec 7752
35df4500 7753 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7754 if (b->type == bp_shlib_event
7755 && b->loc->pspace == current_program_space)
cae688ec
JJ
7756 delete_breakpoint (b);
7757}
7758
f37f681c
PA
7759/* See breakpoint.h. */
7760
7761void
7762remove_solib_event_breakpoints_at_next_stop (void)
7763{
7764 struct breakpoint *b, *b_tmp;
7765
7766 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7767 if (b->type == bp_shlib_event
7768 && b->loc->pspace == current_program_space)
7769 b->disposition = disp_del_at_next_stop;
7770}
7771
04086b45
PA
7772/* Helper for create_solib_event_breakpoint /
7773 create_and_insert_solib_event_breakpoint. Allows specifying which
7774 INSERT_MODE to pass through to update_global_location_list. */
7775
7776static struct breakpoint *
7777create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7778 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7779{
7780 struct breakpoint *b;
7781
06edf0c0
PA
7782 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7783 &internal_breakpoint_ops);
04086b45 7784 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7785 return b;
7786}
7787
04086b45
PA
7788struct breakpoint *
7789create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7790{
7791 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7792}
7793
f37f681c
PA
7794/* See breakpoint.h. */
7795
7796struct breakpoint *
7797create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7798{
7799 struct breakpoint *b;
7800
04086b45
PA
7801 /* Explicitly tell update_global_location_list to insert
7802 locations. */
7803 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7804 if (!b->loc->inserted)
7805 {
7806 delete_breakpoint (b);
7807 return NULL;
7808 }
7809 return b;
7810}
7811
cae688ec
JJ
7812/* Disable any breakpoints that are on code in shared libraries. Only
7813 apply to enabled breakpoints, disabled ones can just stay disabled. */
7814
7815void
cb851954 7816disable_breakpoints_in_shlibs (void)
cae688ec 7817{
876fa593 7818 struct bp_location *loc, **locp_tmp;
cae688ec 7819
876fa593 7820 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7821 {
2bdf28a0 7822 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7823 struct breakpoint *b = loc->owner;
2bdf28a0 7824
4a64f543
MS
7825 /* We apply the check to all breakpoints, including disabled for
7826 those with loc->duplicate set. This is so that when breakpoint
7827 becomes enabled, or the duplicate is removed, gdb will try to
7828 insert all breakpoints. If we don't set shlib_disabled here,
7829 we'll try to insert those breakpoints and fail. */
1042e4c0 7830 if (((b->type == bp_breakpoint)
508ccb1f 7831 || (b->type == bp_jit_event)
1042e4c0 7832 || (b->type == bp_hardware_breakpoint)
d77f58be 7833 || (is_tracepoint (b)))
6c95b8df 7834 && loc->pspace == current_program_space
0d381245 7835 && !loc->shlib_disabled
6c95b8df 7836 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7837 )
0d381245
VP
7838 {
7839 loc->shlib_disabled = 1;
7840 }
cae688ec
JJ
7841 }
7842}
7843
63644780
NB
7844/* Disable any breakpoints and tracepoints that are in SOLIB upon
7845 notification of unloaded_shlib. Only apply to enabled breakpoints,
7846 disabled ones can just stay disabled. */
84acb35a 7847
75149521 7848static void
84acb35a
JJ
7849disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7850{
876fa593 7851 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7852 int disabled_shlib_breaks = 0;
7853
c86cf029
VP
7854 /* SunOS a.out shared libraries are always mapped, so do not
7855 disable breakpoints; they will only be reported as unloaded
7856 through clear_solib when GDB discards its shared library
7857 list. See clear_solib for more information. */
7858 if (exec_bfd != NULL
7859 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7860 return;
7861
876fa593 7862 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7863 {
2bdf28a0 7864 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7865 struct breakpoint *b = loc->owner;
cc59ec59 7866
1e4d1764 7867 if (solib->pspace == loc->pspace
e2dd7057 7868 && !loc->shlib_disabled
1e4d1764
YQ
7869 && (((b->type == bp_breakpoint
7870 || b->type == bp_jit_event
7871 || b->type == bp_hardware_breakpoint)
7872 && (loc->loc_type == bp_loc_hardware_breakpoint
7873 || loc->loc_type == bp_loc_software_breakpoint))
7874 || is_tracepoint (b))
e2dd7057 7875 && solib_contains_address_p (solib, loc->address))
84acb35a 7876 {
e2dd7057
PP
7877 loc->shlib_disabled = 1;
7878 /* At this point, we cannot rely on remove_breakpoint
7879 succeeding so we must mark the breakpoint as not inserted
7880 to prevent future errors occurring in remove_breakpoints. */
7881 loc->inserted = 0;
8d3788bd
VP
7882
7883 /* This may cause duplicate notifications for the same breakpoint. */
7884 observer_notify_breakpoint_modified (b);
7885
e2dd7057
PP
7886 if (!disabled_shlib_breaks)
7887 {
7888 target_terminal_ours_for_output ();
3e43a32a
MS
7889 warning (_("Temporarily disabling breakpoints "
7890 "for unloaded shared library \"%s\""),
e2dd7057 7891 solib->so_name);
84acb35a 7892 }
e2dd7057 7893 disabled_shlib_breaks = 1;
84acb35a
JJ
7894 }
7895 }
84acb35a
JJ
7896}
7897
63644780
NB
7898/* Disable any breakpoints and tracepoints in OBJFILE upon
7899 notification of free_objfile. Only apply to enabled breakpoints,
7900 disabled ones can just stay disabled. */
7901
7902static void
7903disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7904{
7905 struct breakpoint *b;
7906
7907 if (objfile == NULL)
7908 return;
7909
d03de421
PA
7910 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7911 managed by the user with add-symbol-file/remove-symbol-file.
7912 Similarly to how breakpoints in shared libraries are handled in
7913 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7914 shlib_disabled so they end up uninserted on the next global
7915 location list update. Shared libraries not loaded by the user
7916 aren't handled here -- they're already handled in
7917 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7918 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7919 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7920 main objfile). */
7921 if ((objfile->flags & OBJF_SHARED) == 0
7922 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7923 return;
7924
7925 ALL_BREAKPOINTS (b)
7926 {
7927 struct bp_location *loc;
7928 int bp_modified = 0;
7929
7930 if (!is_breakpoint (b) && !is_tracepoint (b))
7931 continue;
7932
7933 for (loc = b->loc; loc != NULL; loc = loc->next)
7934 {
7935 CORE_ADDR loc_addr = loc->address;
7936
7937 if (loc->loc_type != bp_loc_hardware_breakpoint
7938 && loc->loc_type != bp_loc_software_breakpoint)
7939 continue;
7940
7941 if (loc->shlib_disabled != 0)
7942 continue;
7943
7944 if (objfile->pspace != loc->pspace)
7945 continue;
7946
7947 if (loc->loc_type != bp_loc_hardware_breakpoint
7948 && loc->loc_type != bp_loc_software_breakpoint)
7949 continue;
7950
7951 if (is_addr_in_objfile (loc_addr, objfile))
7952 {
7953 loc->shlib_disabled = 1;
08351840
PA
7954 /* At this point, we don't know whether the object was
7955 unmapped from the inferior or not, so leave the
7956 inserted flag alone. We'll handle failure to
7957 uninsert quietly, in case the object was indeed
7958 unmapped. */
63644780
NB
7959
7960 mark_breakpoint_location_modified (loc);
7961
7962 bp_modified = 1;
7963 }
7964 }
7965
7966 if (bp_modified)
7967 observer_notify_breakpoint_modified (b);
7968 }
7969}
7970
ce78b96d
JB
7971/* FORK & VFORK catchpoints. */
7972
e29a4733
PA
7973/* An instance of this type is used to represent a fork or vfork
7974 catchpoint. It includes a "struct breakpoint" as a kind of base
7975 class; users downcast to "struct breakpoint *" when needed. A
7976 breakpoint is really of this type iff its ops pointer points to
7977 CATCH_FORK_BREAKPOINT_OPS. */
7978
7979struct fork_catchpoint
7980{
7981 /* The base class. */
7982 struct breakpoint base;
7983
7984 /* Process id of a child process whose forking triggered this
7985 catchpoint. This field is only valid immediately after this
7986 catchpoint has triggered. */
7987 ptid_t forked_inferior_pid;
7988};
7989
4a64f543
MS
7990/* Implement the "insert" breakpoint_ops method for fork
7991 catchpoints. */
ce78b96d 7992
77b06cd7
TJB
7993static int
7994insert_catch_fork (struct bp_location *bl)
ce78b96d 7995{
dfd4cc63 7996 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7997}
7998
4a64f543
MS
7999/* Implement the "remove" breakpoint_ops method for fork
8000 catchpoints. */
ce78b96d
JB
8001
8002static int
77b06cd7 8003remove_catch_fork (struct bp_location *bl)
ce78b96d 8004{
dfd4cc63 8005 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8006}
8007
8008/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8009 catchpoints. */
8010
8011static int
f1310107 8012breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8013 struct address_space *aspace, CORE_ADDR bp_addr,
8014 const struct target_waitstatus *ws)
ce78b96d 8015{
e29a4733
PA
8016 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8017
f90263c1
TT
8018 if (ws->kind != TARGET_WAITKIND_FORKED)
8019 return 0;
8020
8021 c->forked_inferior_pid = ws->value.related_pid;
8022 return 1;
ce78b96d
JB
8023}
8024
4a64f543
MS
8025/* Implement the "print_it" breakpoint_ops method for fork
8026 catchpoints. */
ce78b96d
JB
8027
8028static enum print_stop_action
348d480f 8029print_it_catch_fork (bpstat bs)
ce78b96d 8030{
36dfb11c 8031 struct ui_out *uiout = current_uiout;
348d480f
PA
8032 struct breakpoint *b = bs->breakpoint_at;
8033 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8034
ce78b96d 8035 annotate_catchpoint (b->number);
36dfb11c
TT
8036 if (b->disposition == disp_del)
8037 ui_out_text (uiout, "\nTemporary catchpoint ");
8038 else
8039 ui_out_text (uiout, "\nCatchpoint ");
8040 if (ui_out_is_mi_like_p (uiout))
8041 {
8042 ui_out_field_string (uiout, "reason",
8043 async_reason_lookup (EXEC_ASYNC_FORK));
8044 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8045 }
8046 ui_out_field_int (uiout, "bkptno", b->number);
8047 ui_out_text (uiout, " (forked process ");
8048 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8049 ui_out_text (uiout, "), ");
ce78b96d
JB
8050 return PRINT_SRC_AND_LOC;
8051}
8052
4a64f543
MS
8053/* Implement the "print_one" breakpoint_ops method for fork
8054 catchpoints. */
ce78b96d
JB
8055
8056static void
a6d9a66e 8057print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8058{
e29a4733 8059 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8060 struct value_print_options opts;
79a45e25 8061 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8062
8063 get_user_print_options (&opts);
8064
4a64f543
MS
8065 /* Field 4, the address, is omitted (which makes the columns not
8066 line up too nicely with the headers, but the effect is relatively
8067 readable). */
79a45b7d 8068 if (opts.addressprint)
ce78b96d
JB
8069 ui_out_field_skip (uiout, "addr");
8070 annotate_field (5);
8071 ui_out_text (uiout, "fork");
e29a4733 8072 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8073 {
8074 ui_out_text (uiout, ", process ");
8075 ui_out_field_int (uiout, "what",
e29a4733 8076 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8077 ui_out_spaces (uiout, 1);
8078 }
8ac3646f
TT
8079
8080 if (ui_out_is_mi_like_p (uiout))
8081 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8082}
8083
8084/* Implement the "print_mention" breakpoint_ops method for fork
8085 catchpoints. */
8086
8087static void
8088print_mention_catch_fork (struct breakpoint *b)
8089{
8090 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8091}
8092
6149aea9
PA
8093/* Implement the "print_recreate" breakpoint_ops method for fork
8094 catchpoints. */
8095
8096static void
8097print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8098{
8099 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8100 print_recreate_thread (b, fp);
6149aea9
PA
8101}
8102
ce78b96d
JB
8103/* The breakpoint_ops structure to be used in fork catchpoints. */
8104
2060206e 8105static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8106
4a64f543
MS
8107/* Implement the "insert" breakpoint_ops method for vfork
8108 catchpoints. */
ce78b96d 8109
77b06cd7
TJB
8110static int
8111insert_catch_vfork (struct bp_location *bl)
ce78b96d 8112{
dfd4cc63 8113 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8114}
8115
4a64f543
MS
8116/* Implement the "remove" breakpoint_ops method for vfork
8117 catchpoints. */
ce78b96d
JB
8118
8119static int
77b06cd7 8120remove_catch_vfork (struct bp_location *bl)
ce78b96d 8121{
dfd4cc63 8122 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8123}
8124
8125/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8126 catchpoints. */
8127
8128static int
f1310107 8129breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8130 struct address_space *aspace, CORE_ADDR bp_addr,
8131 const struct target_waitstatus *ws)
ce78b96d 8132{
e29a4733
PA
8133 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8134
f90263c1
TT
8135 if (ws->kind != TARGET_WAITKIND_VFORKED)
8136 return 0;
8137
8138 c->forked_inferior_pid = ws->value.related_pid;
8139 return 1;
ce78b96d
JB
8140}
8141
4a64f543
MS
8142/* Implement the "print_it" breakpoint_ops method for vfork
8143 catchpoints. */
ce78b96d
JB
8144
8145static enum print_stop_action
348d480f 8146print_it_catch_vfork (bpstat bs)
ce78b96d 8147{
36dfb11c 8148 struct ui_out *uiout = current_uiout;
348d480f 8149 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8150 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8151
ce78b96d 8152 annotate_catchpoint (b->number);
36dfb11c
TT
8153 if (b->disposition == disp_del)
8154 ui_out_text (uiout, "\nTemporary catchpoint ");
8155 else
8156 ui_out_text (uiout, "\nCatchpoint ");
8157 if (ui_out_is_mi_like_p (uiout))
8158 {
8159 ui_out_field_string (uiout, "reason",
8160 async_reason_lookup (EXEC_ASYNC_VFORK));
8161 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8162 }
8163 ui_out_field_int (uiout, "bkptno", b->number);
8164 ui_out_text (uiout, " (vforked process ");
8165 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8166 ui_out_text (uiout, "), ");
ce78b96d
JB
8167 return PRINT_SRC_AND_LOC;
8168}
8169
4a64f543
MS
8170/* Implement the "print_one" breakpoint_ops method for vfork
8171 catchpoints. */
ce78b96d
JB
8172
8173static void
a6d9a66e 8174print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8175{
e29a4733 8176 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8177 struct value_print_options opts;
79a45e25 8178 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8179
8180 get_user_print_options (&opts);
4a64f543
MS
8181 /* Field 4, the address, is omitted (which makes the columns not
8182 line up too nicely with the headers, but the effect is relatively
8183 readable). */
79a45b7d 8184 if (opts.addressprint)
ce78b96d
JB
8185 ui_out_field_skip (uiout, "addr");
8186 annotate_field (5);
8187 ui_out_text (uiout, "vfork");
e29a4733 8188 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8189 {
8190 ui_out_text (uiout, ", process ");
8191 ui_out_field_int (uiout, "what",
e29a4733 8192 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8193 ui_out_spaces (uiout, 1);
8194 }
8ac3646f
TT
8195
8196 if (ui_out_is_mi_like_p (uiout))
8197 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8198}
8199
8200/* Implement the "print_mention" breakpoint_ops method for vfork
8201 catchpoints. */
8202
8203static void
8204print_mention_catch_vfork (struct breakpoint *b)
8205{
8206 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8207}
8208
6149aea9
PA
8209/* Implement the "print_recreate" breakpoint_ops method for vfork
8210 catchpoints. */
8211
8212static void
8213print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8214{
8215 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8216 print_recreate_thread (b, fp);
6149aea9
PA
8217}
8218
ce78b96d
JB
8219/* The breakpoint_ops structure to be used in vfork catchpoints. */
8220
2060206e 8221static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8222
edcc5120
TT
8223/* An instance of this type is used to represent an solib catchpoint.
8224 It includes a "struct breakpoint" as a kind of base class; users
8225 downcast to "struct breakpoint *" when needed. A breakpoint is
8226 really of this type iff its ops pointer points to
8227 CATCH_SOLIB_BREAKPOINT_OPS. */
8228
8229struct solib_catchpoint
8230{
8231 /* The base class. */
8232 struct breakpoint base;
8233
8234 /* True for "catch load", false for "catch unload". */
8235 unsigned char is_load;
8236
8237 /* Regular expression to match, if any. COMPILED is only valid when
8238 REGEX is non-NULL. */
8239 char *regex;
8240 regex_t compiled;
8241};
8242
8243static void
8244dtor_catch_solib (struct breakpoint *b)
8245{
8246 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8247
8248 if (self->regex)
8249 regfree (&self->compiled);
8250 xfree (self->regex);
8251
8252 base_breakpoint_ops.dtor (b);
8253}
8254
8255static int
8256insert_catch_solib (struct bp_location *ignore)
8257{
8258 return 0;
8259}
8260
8261static int
8262remove_catch_solib (struct bp_location *ignore)
8263{
8264 return 0;
8265}
8266
8267static int
8268breakpoint_hit_catch_solib (const struct bp_location *bl,
8269 struct address_space *aspace,
8270 CORE_ADDR bp_addr,
8271 const struct target_waitstatus *ws)
8272{
8273 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8274 struct breakpoint *other;
8275
8276 if (ws->kind == TARGET_WAITKIND_LOADED)
8277 return 1;
8278
8279 ALL_BREAKPOINTS (other)
8280 {
8281 struct bp_location *other_bl;
8282
8283 if (other == bl->owner)
8284 continue;
8285
8286 if (other->type != bp_shlib_event)
8287 continue;
8288
8289 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8290 continue;
8291
8292 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8293 {
8294 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8295 return 1;
8296 }
8297 }
8298
8299 return 0;
8300}
8301
8302static void
8303check_status_catch_solib (struct bpstats *bs)
8304{
8305 struct solib_catchpoint *self
8306 = (struct solib_catchpoint *) bs->breakpoint_at;
8307 int ix;
8308
8309 if (self->is_load)
8310 {
8311 struct so_list *iter;
8312
8313 for (ix = 0;
8314 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8315 ix, iter);
8316 ++ix)
8317 {
8318 if (!self->regex
8319 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8320 return;
8321 }
8322 }
8323 else
8324 {
8325 char *iter;
8326
8327 for (ix = 0;
8328 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8329 ix, iter);
8330 ++ix)
8331 {
8332 if (!self->regex
8333 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8334 return;
8335 }
8336 }
8337
8338 bs->stop = 0;
8339 bs->print_it = print_it_noop;
8340}
8341
8342static enum print_stop_action
8343print_it_catch_solib (bpstat bs)
8344{
8345 struct breakpoint *b = bs->breakpoint_at;
8346 struct ui_out *uiout = current_uiout;
8347
8348 annotate_catchpoint (b->number);
8349 if (b->disposition == disp_del)
8350 ui_out_text (uiout, "\nTemporary catchpoint ");
8351 else
8352 ui_out_text (uiout, "\nCatchpoint ");
8353 ui_out_field_int (uiout, "bkptno", b->number);
8354 ui_out_text (uiout, "\n");
8355 if (ui_out_is_mi_like_p (uiout))
8356 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8357 print_solib_event (1);
8358 return PRINT_SRC_AND_LOC;
8359}
8360
8361static void
8362print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8363{
8364 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8365 struct value_print_options opts;
8366 struct ui_out *uiout = current_uiout;
8367 char *msg;
8368
8369 get_user_print_options (&opts);
8370 /* Field 4, the address, is omitted (which makes the columns not
8371 line up too nicely with the headers, but the effect is relatively
8372 readable). */
8373 if (opts.addressprint)
8374 {
8375 annotate_field (4);
8376 ui_out_field_skip (uiout, "addr");
8377 }
8378
8379 annotate_field (5);
8380 if (self->is_load)
8381 {
8382 if (self->regex)
8383 msg = xstrprintf (_("load of library matching %s"), self->regex);
8384 else
8385 msg = xstrdup (_("load of library"));
8386 }
8387 else
8388 {
8389 if (self->regex)
8390 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8391 else
8392 msg = xstrdup (_("unload of library"));
8393 }
8394 ui_out_field_string (uiout, "what", msg);
8395 xfree (msg);
8ac3646f
TT
8396
8397 if (ui_out_is_mi_like_p (uiout))
8398 ui_out_field_string (uiout, "catch-type",
8399 self->is_load ? "load" : "unload");
edcc5120
TT
8400}
8401
8402static void
8403print_mention_catch_solib (struct breakpoint *b)
8404{
8405 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8406
8407 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8408 self->is_load ? "load" : "unload");
8409}
8410
8411static void
8412print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8413{
8414 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8415
8416 fprintf_unfiltered (fp, "%s %s",
8417 b->disposition == disp_del ? "tcatch" : "catch",
8418 self->is_load ? "load" : "unload");
8419 if (self->regex)
8420 fprintf_unfiltered (fp, " %s", self->regex);
8421 fprintf_unfiltered (fp, "\n");
8422}
8423
8424static struct breakpoint_ops catch_solib_breakpoint_ops;
8425
91985142
MG
8426/* Shared helper function (MI and CLI) for creating and installing
8427 a shared object event catchpoint. If IS_LOAD is non-zero then
8428 the events to be caught are load events, otherwise they are
8429 unload events. If IS_TEMP is non-zero the catchpoint is a
8430 temporary one. If ENABLED is non-zero the catchpoint is
8431 created in an enabled state. */
edcc5120 8432
91985142
MG
8433void
8434add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8435{
8436 struct solib_catchpoint *c;
8437 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8438 struct cleanup *cleanup;
8439
edcc5120
TT
8440 if (!arg)
8441 arg = "";
8442 arg = skip_spaces (arg);
8443
8444 c = XCNEW (struct solib_catchpoint);
8445 cleanup = make_cleanup (xfree, c);
8446
8447 if (*arg != '\0')
8448 {
8449 int errcode;
8450
8451 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8452 if (errcode != 0)
8453 {
8454 char *err = get_regcomp_error (errcode, &c->compiled);
8455
8456 make_cleanup (xfree, err);
8457 error (_("Invalid regexp (%s): %s"), err, arg);
8458 }
8459 c->regex = xstrdup (arg);
8460 }
8461
8462 c->is_load = is_load;
91985142 8463 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8464 &catch_solib_breakpoint_ops);
8465
91985142
MG
8466 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8467
edcc5120
TT
8468 discard_cleanups (cleanup);
8469 install_breakpoint (0, &c->base, 1);
8470}
8471
91985142
MG
8472/* A helper function that does all the work for "catch load" and
8473 "catch unload". */
8474
8475static void
8476catch_load_or_unload (char *arg, int from_tty, int is_load,
8477 struct cmd_list_element *command)
8478{
8479 int tempflag;
8480 const int enabled = 1;
8481
8482 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8483
8484 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8485}
8486
edcc5120
TT
8487static void
8488catch_load_command_1 (char *arg, int from_tty,
8489 struct cmd_list_element *command)
8490{
8491 catch_load_or_unload (arg, from_tty, 1, command);
8492}
8493
8494static void
8495catch_unload_command_1 (char *arg, int from_tty,
8496 struct cmd_list_element *command)
8497{
8498 catch_load_or_unload (arg, from_tty, 0, command);
8499}
8500
be5c67c1
PA
8501/* An instance of this type is used to represent a syscall catchpoint.
8502 It includes a "struct breakpoint" as a kind of base class; users
8503 downcast to "struct breakpoint *" when needed. A breakpoint is
8504 really of this type iff its ops pointer points to
8505 CATCH_SYSCALL_BREAKPOINT_OPS. */
8506
8507struct syscall_catchpoint
8508{
8509 /* The base class. */
8510 struct breakpoint base;
8511
8512 /* Syscall numbers used for the 'catch syscall' feature. If no
8513 syscall has been specified for filtering, its value is NULL.
8514 Otherwise, it holds a list of all syscalls to be caught. The
8515 list elements are allocated with xmalloc. */
8516 VEC(int) *syscalls_to_be_caught;
8517};
8518
8519/* Implement the "dtor" breakpoint_ops method for syscall
8520 catchpoints. */
8521
8522static void
8523dtor_catch_syscall (struct breakpoint *b)
8524{
8525 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8526
8527 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8528
2060206e 8529 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8530}
8531
fa3064dd
YQ
8532static const struct inferior_data *catch_syscall_inferior_data = NULL;
8533
8534struct catch_syscall_inferior_data
8535{
8536 /* We keep a count of the number of times the user has requested a
8537 particular syscall to be tracked, and pass this information to the
8538 target. This lets capable targets implement filtering directly. */
8539
8540 /* Number of times that "any" syscall is requested. */
8541 int any_syscall_count;
8542
8543 /* Count of each system call. */
8544 VEC(int) *syscalls_counts;
8545
8546 /* This counts all syscall catch requests, so we can readily determine
8547 if any catching is necessary. */
8548 int total_syscalls_count;
8549};
8550
8551static struct catch_syscall_inferior_data*
8552get_catch_syscall_inferior_data (struct inferior *inf)
8553{
8554 struct catch_syscall_inferior_data *inf_data;
8555
8556 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8557 if (inf_data == NULL)
8558 {
41bf6aca 8559 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8560 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8561 }
8562
8563 return inf_data;
8564}
8565
8566static void
8567catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8568{
8569 xfree (arg);
8570}
8571
8572
a96d9b2e
SDJ
8573/* Implement the "insert" breakpoint_ops method for syscall
8574 catchpoints. */
8575
77b06cd7
TJB
8576static int
8577insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8578{
be5c67c1 8579 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8580 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8581 struct catch_syscall_inferior_data *inf_data
8582 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8583
fa3064dd 8584 ++inf_data->total_syscalls_count;
be5c67c1 8585 if (!c->syscalls_to_be_caught)
fa3064dd 8586 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8587 else
8588 {
8589 int i, iter;
cc59ec59 8590
a96d9b2e 8591 for (i = 0;
be5c67c1 8592 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8593 i++)
8594 {
8595 int elem;
cc59ec59 8596
fa3064dd 8597 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8598 {
fa3064dd 8599 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8600 uintptr_t vec_addr_offset
8601 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8602 uintptr_t vec_addr;
fa3064dd
YQ
8603 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8604 vec_addr = ((uintptr_t) VEC_address (int,
8605 inf_data->syscalls_counts)
8606 + vec_addr_offset);
a96d9b2e
SDJ
8607 memset ((void *) vec_addr, 0,
8608 (iter + 1 - old_size) * sizeof (int));
8609 }
fa3064dd
YQ
8610 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8611 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8612 }
8613 }
8614
dfd4cc63 8615 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8616 inf_data->total_syscalls_count != 0,
8617 inf_data->any_syscall_count,
8618 VEC_length (int,
8619 inf_data->syscalls_counts),
8620 VEC_address (int,
8621 inf_data->syscalls_counts));
a96d9b2e
SDJ
8622}
8623
8624/* Implement the "remove" breakpoint_ops method for syscall
8625 catchpoints. */
8626
8627static int
77b06cd7 8628remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8629{
be5c67c1 8630 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8631 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8632 struct catch_syscall_inferior_data *inf_data
8633 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8634
fa3064dd 8635 --inf_data->total_syscalls_count;
be5c67c1 8636 if (!c->syscalls_to_be_caught)
fa3064dd 8637 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8638 else
8639 {
8640 int i, iter;
cc59ec59 8641
a96d9b2e 8642 for (i = 0;
be5c67c1 8643 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8644 i++)
8645 {
8646 int elem;
fa3064dd 8647 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8648 /* Shouldn't happen. */
8649 continue;
fa3064dd
YQ
8650 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8651 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8652 }
8653 }
8654
dfd4cc63 8655 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8656 inf_data->total_syscalls_count != 0,
8657 inf_data->any_syscall_count,
8658 VEC_length (int,
8659 inf_data->syscalls_counts),
3e43a32a 8660 VEC_address (int,
fa3064dd 8661 inf_data->syscalls_counts));
a96d9b2e
SDJ
8662}
8663
8664/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8665 catchpoints. */
8666
8667static int
f1310107 8668breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8669 struct address_space *aspace, CORE_ADDR bp_addr,
8670 const struct target_waitstatus *ws)
a96d9b2e 8671{
4a64f543
MS
8672 /* We must check if we are catching specific syscalls in this
8673 breakpoint. If we are, then we must guarantee that the called
8674 syscall is the same syscall we are catching. */
a96d9b2e 8675 int syscall_number = 0;
be5c67c1
PA
8676 const struct syscall_catchpoint *c
8677 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8678
f90263c1
TT
8679 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8680 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8681 return 0;
8682
f90263c1
TT
8683 syscall_number = ws->value.syscall_number;
8684
a96d9b2e 8685 /* Now, checking if the syscall is the same. */
be5c67c1 8686 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8687 {
8688 int i, iter;
cc59ec59 8689
a96d9b2e 8690 for (i = 0;
be5c67c1 8691 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8692 i++)
8693 if (syscall_number == iter)
4924df79
GKB
8694 return 1;
8695
8696 return 0;
a96d9b2e
SDJ
8697 }
8698
8699 return 1;
8700}
8701
8702/* Implement the "print_it" breakpoint_ops method for syscall
8703 catchpoints. */
8704
8705static enum print_stop_action
348d480f 8706print_it_catch_syscall (bpstat bs)
a96d9b2e 8707{
36dfb11c 8708 struct ui_out *uiout = current_uiout;
348d480f 8709 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8710 /* These are needed because we want to know in which state a
8711 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8712 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8713 must print "called syscall" or "returned from syscall". */
8714 ptid_t ptid;
8715 struct target_waitstatus last;
8716 struct syscall s;
a96d9b2e
SDJ
8717
8718 get_last_target_status (&ptid, &last);
8719
8720 get_syscall_by_number (last.value.syscall_number, &s);
8721
8722 annotate_catchpoint (b->number);
8723
36dfb11c
TT
8724 if (b->disposition == disp_del)
8725 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8726 else
36dfb11c
TT
8727 ui_out_text (uiout, "\nCatchpoint ");
8728 if (ui_out_is_mi_like_p (uiout))
8729 {
8730 ui_out_field_string (uiout, "reason",
8731 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8732 ? EXEC_ASYNC_SYSCALL_ENTRY
8733 : EXEC_ASYNC_SYSCALL_RETURN));
8734 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8735 }
8736 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8737
8738 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8739 ui_out_text (uiout, " (call to syscall ");
8740 else
8741 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8742
36dfb11c
TT
8743 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8744 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8745 if (s.name != NULL)
8746 ui_out_field_string (uiout, "syscall-name", s.name);
8747
8748 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8749
8750 return PRINT_SRC_AND_LOC;
8751}
8752
8753/* Implement the "print_one" breakpoint_ops method for syscall
8754 catchpoints. */
8755
8756static void
8757print_one_catch_syscall (struct breakpoint *b,
f1310107 8758 struct bp_location **last_loc)
a96d9b2e 8759{
be5c67c1 8760 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8761 struct value_print_options opts;
79a45e25 8762 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
8763
8764 get_user_print_options (&opts);
4a64f543
MS
8765 /* Field 4, the address, is omitted (which makes the columns not
8766 line up too nicely with the headers, but the effect is relatively
8767 readable). */
a96d9b2e
SDJ
8768 if (opts.addressprint)
8769 ui_out_field_skip (uiout, "addr");
8770 annotate_field (5);
8771
be5c67c1
PA
8772 if (c->syscalls_to_be_caught
8773 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8774 ui_out_text (uiout, "syscalls \"");
8775 else
8776 ui_out_text (uiout, "syscall \"");
8777
be5c67c1 8778 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8779 {
8780 int i, iter;
8781 char *text = xstrprintf ("%s", "");
cc59ec59 8782
a96d9b2e 8783 for (i = 0;
be5c67c1 8784 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8785 i++)
8786 {
8787 char *x = text;
8788 struct syscall s;
8789 get_syscall_by_number (iter, &s);
8790
8791 if (s.name != NULL)
8792 text = xstrprintf ("%s%s, ", text, s.name);
8793 else
8794 text = xstrprintf ("%s%d, ", text, iter);
8795
8796 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8797 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8798 on every call. */
8799 xfree (x);
8800 }
8801 /* Remove the last comma. */
8802 text[strlen (text) - 2] = '\0';
8803 ui_out_field_string (uiout, "what", text);
8804 }
8805 else
8806 ui_out_field_string (uiout, "what", "<any syscall>");
8807 ui_out_text (uiout, "\" ");
8ac3646f
TT
8808
8809 if (ui_out_is_mi_like_p (uiout))
8810 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8811}
8812
8813/* Implement the "print_mention" breakpoint_ops method for syscall
8814 catchpoints. */
8815
8816static void
8817print_mention_catch_syscall (struct breakpoint *b)
8818{
be5c67c1
PA
8819 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8820
8821 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8822 {
8823 int i, iter;
8824
be5c67c1 8825 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8826 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8827 else
8828 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8829
8830 for (i = 0;
be5c67c1 8831 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8832 i++)
8833 {
8834 struct syscall s;
8835 get_syscall_by_number (iter, &s);
8836
8837 if (s.name)
8838 printf_filtered (" '%s' [%d]", s.name, s.number);
8839 else
8840 printf_filtered (" %d", s.number);
8841 }
8842 printf_filtered (")");
8843 }
8844 else
8845 printf_filtered (_("Catchpoint %d (any syscall)"),
8846 b->number);
8847}
8848
6149aea9
PA
8849/* Implement the "print_recreate" breakpoint_ops method for syscall
8850 catchpoints. */
8851
8852static void
8853print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8854{
be5c67c1
PA
8855 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8856
6149aea9
PA
8857 fprintf_unfiltered (fp, "catch syscall");
8858
be5c67c1 8859 if (c->syscalls_to_be_caught)
6149aea9
PA
8860 {
8861 int i, iter;
8862
8863 for (i = 0;
be5c67c1 8864 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8865 i++)
8866 {
8867 struct syscall s;
8868
8869 get_syscall_by_number (iter, &s);
8870 if (s.name)
8871 fprintf_unfiltered (fp, " %s", s.name);
8872 else
8873 fprintf_unfiltered (fp, " %d", s.number);
8874 }
8875 }
d9b3f62e 8876 print_recreate_thread (b, fp);
6149aea9
PA
8877}
8878
a96d9b2e
SDJ
8879/* The breakpoint_ops structure to be used in syscall catchpoints. */
8880
2060206e 8881static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8882
8883/* Returns non-zero if 'b' is a syscall catchpoint. */
8884
8885static int
8886syscall_catchpoint_p (struct breakpoint *b)
8887{
8888 return (b->ops == &catch_syscall_breakpoint_ops);
8889}
8890
346774a9
PA
8891/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8892 is non-zero, then make the breakpoint temporary. If COND_STRING is
8893 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8894 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8895
ab04a2af 8896void
346774a9
PA
8897init_catchpoint (struct breakpoint *b,
8898 struct gdbarch *gdbarch, int tempflag,
8899 char *cond_string,
c0a91b2b 8900 const struct breakpoint_ops *ops)
c906108c 8901{
c5aa993b 8902 struct symtab_and_line sal;
346774a9 8903
fe39c653 8904 init_sal (&sal);
6c95b8df 8905 sal.pspace = current_program_space;
c5aa993b 8906
28010a5d 8907 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8908
1b36a34b 8909 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8910 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8911}
8912
28010a5d 8913void
3ea46bff 8914install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8915{
8916 add_to_breakpoint_chain (b);
3a5c3e22 8917 set_breakpoint_number (internal, b);
558a9d82
YQ
8918 if (is_tracepoint (b))
8919 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8920 if (!internal)
8921 mention (b);
c56053d2 8922 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8923
8924 if (update_gll)
44702360 8925 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8926}
8927
9b70b993 8928static void
a6d9a66e
UW
8929create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8930 int tempflag, char *cond_string,
c0a91b2b 8931 const struct breakpoint_ops *ops)
c906108c 8932{
e29a4733 8933 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8934
e29a4733
PA
8935 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8936
8937 c->forked_inferior_pid = null_ptid;
8938
3ea46bff 8939 install_breakpoint (0, &c->base, 1);
c906108c
SS
8940}
8941
fe798b75
JB
8942/* Exec catchpoints. */
8943
b4d90040
PA
8944/* An instance of this type is used to represent an exec catchpoint.
8945 It includes a "struct breakpoint" as a kind of base class; users
8946 downcast to "struct breakpoint *" when needed. A breakpoint is
8947 really of this type iff its ops pointer points to
8948 CATCH_EXEC_BREAKPOINT_OPS. */
8949
8950struct exec_catchpoint
8951{
8952 /* The base class. */
8953 struct breakpoint base;
8954
8955 /* Filename of a program whose exec triggered this catchpoint.
8956 This field is only valid immediately after this catchpoint has
8957 triggered. */
8958 char *exec_pathname;
8959};
8960
8961/* Implement the "dtor" breakpoint_ops method for exec
8962 catchpoints. */
8963
8964static void
8965dtor_catch_exec (struct breakpoint *b)
8966{
8967 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8968
8969 xfree (c->exec_pathname);
348d480f 8970
2060206e 8971 base_breakpoint_ops.dtor (b);
b4d90040
PA
8972}
8973
77b06cd7
TJB
8974static int
8975insert_catch_exec (struct bp_location *bl)
c906108c 8976{
dfd4cc63 8977 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8978}
c906108c 8979
fe798b75 8980static int
77b06cd7 8981remove_catch_exec (struct bp_location *bl)
fe798b75 8982{
dfd4cc63 8983 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8984}
c906108c 8985
fe798b75 8986static int
f1310107 8987breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8988 struct address_space *aspace, CORE_ADDR bp_addr,
8989 const struct target_waitstatus *ws)
fe798b75 8990{
b4d90040
PA
8991 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8992
f90263c1
TT
8993 if (ws->kind != TARGET_WAITKIND_EXECD)
8994 return 0;
8995
8996 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8997 return 1;
fe798b75 8998}
c906108c 8999
fe798b75 9000static enum print_stop_action
348d480f 9001print_it_catch_exec (bpstat bs)
fe798b75 9002{
36dfb11c 9003 struct ui_out *uiout = current_uiout;
348d480f 9004 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
9005 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
9006
fe798b75 9007 annotate_catchpoint (b->number);
36dfb11c
TT
9008 if (b->disposition == disp_del)
9009 ui_out_text (uiout, "\nTemporary catchpoint ");
9010 else
9011 ui_out_text (uiout, "\nCatchpoint ");
9012 if (ui_out_is_mi_like_p (uiout))
9013 {
9014 ui_out_field_string (uiout, "reason",
9015 async_reason_lookup (EXEC_ASYNC_EXEC));
9016 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9017 }
9018 ui_out_field_int (uiout, "bkptno", b->number);
9019 ui_out_text (uiout, " (exec'd ");
9020 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
9021 ui_out_text (uiout, "), ");
9022
fe798b75 9023 return PRINT_SRC_AND_LOC;
c906108c
SS
9024}
9025
fe798b75 9026static void
a6d9a66e 9027print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 9028{
b4d90040 9029 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 9030 struct value_print_options opts;
79a45e25 9031 struct ui_out *uiout = current_uiout;
fe798b75
JB
9032
9033 get_user_print_options (&opts);
9034
9035 /* Field 4, the address, is omitted (which makes the columns
9036 not line up too nicely with the headers, but the effect
9037 is relatively readable). */
9038 if (opts.addressprint)
9039 ui_out_field_skip (uiout, "addr");
9040 annotate_field (5);
9041 ui_out_text (uiout, "exec");
b4d90040 9042 if (c->exec_pathname != NULL)
fe798b75
JB
9043 {
9044 ui_out_text (uiout, ", program \"");
b4d90040 9045 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
9046 ui_out_text (uiout, "\" ");
9047 }
8ac3646f
TT
9048
9049 if (ui_out_is_mi_like_p (uiout))
9050 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
9051}
9052
9053static void
9054print_mention_catch_exec (struct breakpoint *b)
9055{
9056 printf_filtered (_("Catchpoint %d (exec)"), b->number);
9057}
9058
6149aea9
PA
9059/* Implement the "print_recreate" breakpoint_ops method for exec
9060 catchpoints. */
9061
9062static void
9063print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
9064{
9065 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 9066 print_recreate_thread (b, fp);
6149aea9
PA
9067}
9068
2060206e 9069static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 9070
a96d9b2e
SDJ
9071static void
9072create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 9073 const struct breakpoint_ops *ops)
a96d9b2e 9074{
be5c67c1 9075 struct syscall_catchpoint *c;
a96d9b2e 9076 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 9077
be5c67c1
PA
9078 c = XNEW (struct syscall_catchpoint);
9079 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
9080 c->syscalls_to_be_caught = filter;
a96d9b2e 9081
3ea46bff 9082 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
9083}
9084
c906108c 9085static int
fba45db2 9086hw_breakpoint_used_count (void)
c906108c 9087{
c906108c 9088 int i = 0;
f1310107
TJB
9089 struct breakpoint *b;
9090 struct bp_location *bl;
c906108c
SS
9091
9092 ALL_BREAKPOINTS (b)
c5aa993b 9093 {
d6b74ac4 9094 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
9095 for (bl = b->loc; bl; bl = bl->next)
9096 {
9097 /* Special types of hardware breakpoints may use more than
9098 one register. */
348d480f 9099 i += b->ops->resources_needed (bl);
f1310107 9100 }
c5aa993b 9101 }
c906108c
SS
9102
9103 return i;
9104}
9105
a1398e0c
PA
9106/* Returns the resources B would use if it were a hardware
9107 watchpoint. */
9108
c906108c 9109static int
a1398e0c 9110hw_watchpoint_use_count (struct breakpoint *b)
c906108c 9111{
c906108c 9112 int i = 0;
e09342b5 9113 struct bp_location *bl;
c906108c 9114
a1398e0c
PA
9115 if (!breakpoint_enabled (b))
9116 return 0;
9117
9118 for (bl = b->loc; bl; bl = bl->next)
9119 {
9120 /* Special types of hardware watchpoints may use more than
9121 one register. */
9122 i += b->ops->resources_needed (bl);
9123 }
9124
9125 return i;
9126}
9127
9128/* Returns the sum the used resources of all hardware watchpoints of
9129 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
9130 the sum of the used resources of all hardware watchpoints of other
9131 types _not_ TYPE. */
9132
9133static int
9134hw_watchpoint_used_count_others (struct breakpoint *except,
9135 enum bptype type, int *other_type_used)
9136{
9137 int i = 0;
9138 struct breakpoint *b;
9139
c906108c
SS
9140 *other_type_used = 0;
9141 ALL_BREAKPOINTS (b)
e09342b5 9142 {
a1398e0c
PA
9143 if (b == except)
9144 continue;
e09342b5
TJB
9145 if (!breakpoint_enabled (b))
9146 continue;
9147
a1398e0c
PA
9148 if (b->type == type)
9149 i += hw_watchpoint_use_count (b);
9150 else if (is_hardware_watchpoint (b))
9151 *other_type_used = 1;
e09342b5
TJB
9152 }
9153
c906108c
SS
9154 return i;
9155}
9156
c906108c 9157void
fba45db2 9158disable_watchpoints_before_interactive_call_start (void)
c906108c 9159{
c5aa993b 9160 struct breakpoint *b;
c906108c
SS
9161
9162 ALL_BREAKPOINTS (b)
c5aa993b 9163 {
cc60f2e3 9164 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 9165 {
b5de0fa7 9166 b->enable_state = bp_call_disabled;
44702360 9167 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
9168 }
9169 }
c906108c
SS
9170}
9171
9172void
fba45db2 9173enable_watchpoints_after_interactive_call_stop (void)
c906108c 9174{
c5aa993b 9175 struct breakpoint *b;
c906108c
SS
9176
9177 ALL_BREAKPOINTS (b)
c5aa993b 9178 {
cc60f2e3 9179 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 9180 {
b5de0fa7 9181 b->enable_state = bp_enabled;
44702360 9182 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
9183 }
9184 }
c906108c
SS
9185}
9186
8bea4e01
UW
9187void
9188disable_breakpoints_before_startup (void)
9189{
6c95b8df 9190 current_program_space->executing_startup = 1;
44702360 9191 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
9192}
9193
9194void
9195enable_breakpoints_after_startup (void)
9196{
6c95b8df 9197 current_program_space->executing_startup = 0;
f8eba3c6 9198 breakpoint_re_set ();
8bea4e01
UW
9199}
9200
7c16b83e
PA
9201/* Create a new single-step breakpoint for thread THREAD, with no
9202 locations. */
c906108c 9203
7c16b83e
PA
9204static struct breakpoint *
9205new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
9206{
9207 struct breakpoint *b = XNEW (struct breakpoint);
9208
9209 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
9210 &momentary_breakpoint_ops);
9211
9212 b->disposition = disp_donttouch;
9213 b->frame_id = null_frame_id;
9214
9215 b->thread = thread;
9216 gdb_assert (b->thread != 0);
9217
9218 add_to_breakpoint_chain (b);
9219
9220 return b;
9221}
9222
9223/* Set a momentary breakpoint of type TYPE at address specified by
9224 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
9225 frame. */
c906108c
SS
9226
9227struct breakpoint *
a6d9a66e
UW
9228set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9229 struct frame_id frame_id, enum bptype type)
c906108c 9230{
52f0bd74 9231 struct breakpoint *b;
edb3359d 9232
193facb3
JK
9233 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9234 tail-called one. */
9235 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 9236
06edf0c0 9237 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
9238 b->enable_state = bp_enabled;
9239 b->disposition = disp_donttouch;
818dd999 9240 b->frame_id = frame_id;
c906108c 9241
4a64f543
MS
9242 /* If we're debugging a multi-threaded program, then we want
9243 momentary breakpoints to be active in only a single thread of
9244 control. */
39f77062
KB
9245 if (in_thread_list (inferior_ptid))
9246 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 9247
44702360 9248 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 9249
c906108c
SS
9250 return b;
9251}
611c83ae 9252
06edf0c0 9253/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
9254 The new breakpoint will have type TYPE, use OPS as its
9255 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 9256
06edf0c0
PA
9257static struct breakpoint *
9258momentary_breakpoint_from_master (struct breakpoint *orig,
9259 enum bptype type,
a1aa2221
LM
9260 const struct breakpoint_ops *ops,
9261 int loc_enabled)
e58b0e63
PA
9262{
9263 struct breakpoint *copy;
9264
06edf0c0 9265 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 9266 copy->loc = allocate_bp_location (copy);
0e30163f 9267 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 9268
a6d9a66e 9269 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
9270 copy->loc->requested_address = orig->loc->requested_address;
9271 copy->loc->address = orig->loc->address;
9272 copy->loc->section = orig->loc->section;
6c95b8df 9273 copy->loc->pspace = orig->loc->pspace;
55aa24fb 9274 copy->loc->probe = orig->loc->probe;
f8eba3c6 9275 copy->loc->line_number = orig->loc->line_number;
2f202fde 9276 copy->loc->symtab = orig->loc->symtab;
a1aa2221 9277 copy->loc->enabled = loc_enabled;
e58b0e63
PA
9278 copy->frame_id = orig->frame_id;
9279 copy->thread = orig->thread;
6c95b8df 9280 copy->pspace = orig->pspace;
e58b0e63
PA
9281
9282 copy->enable_state = bp_enabled;
9283 copy->disposition = disp_donttouch;
9284 copy->number = internal_breakpoint_number--;
9285
44702360 9286 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
9287 return copy;
9288}
9289
06edf0c0
PA
9290/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9291 ORIG is NULL. */
9292
9293struct breakpoint *
9294clone_momentary_breakpoint (struct breakpoint *orig)
9295{
9296 /* If there's nothing to clone, then return nothing. */
9297 if (orig == NULL)
9298 return NULL;
9299
a1aa2221 9300 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
9301}
9302
611c83ae 9303struct breakpoint *
a6d9a66e
UW
9304set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9305 enum bptype type)
611c83ae
PA
9306{
9307 struct symtab_and_line sal;
9308
9309 sal = find_pc_line (pc, 0);
9310 sal.pc = pc;
9311 sal.section = find_pc_overlay (pc);
9312 sal.explicit_pc = 1;
9313
a6d9a66e 9314 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9315}
c906108c 9316\f
c5aa993b 9317
c906108c
SS
9318/* Tell the user we have just set a breakpoint B. */
9319
9320static void
fba45db2 9321mention (struct breakpoint *b)
c906108c 9322{
348d480f 9323 b->ops->print_mention (b);
79a45e25 9324 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9325 return;
c906108c
SS
9326 printf_filtered ("\n");
9327}
c906108c 9328\f
c5aa993b 9329
0d381245 9330static struct bp_location *
39d61571 9331add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9332 const struct symtab_and_line *sal)
9333{
9334 struct bp_location *loc, **tmp;
3742cc8b
YQ
9335 CORE_ADDR adjusted_address;
9336 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9337
9338 if (loc_gdbarch == NULL)
9339 loc_gdbarch = b->gdbarch;
9340
9341 /* Adjust the breakpoint's address prior to allocating a location.
9342 Once we call allocate_bp_location(), that mostly uninitialized
9343 location will be placed on the location chain. Adjustment of the
9344 breakpoint may cause target_read_memory() to be called and we do
9345 not want its scan of the location chain to find a breakpoint and
9346 location that's only been partially initialized. */
9347 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9348 sal->pc, b->type);
0d381245 9349
d30113d4 9350 /* Sort the locations by their ADDRESS. */
39d61571 9351 loc = allocate_bp_location (b);
d30113d4
JK
9352 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9353 tmp = &((*tmp)->next))
0d381245 9354 ;
d30113d4 9355 loc->next = *tmp;
0d381245 9356 *tmp = loc;
3742cc8b 9357
0d381245 9358 loc->requested_address = sal->pc;
3742cc8b 9359 loc->address = adjusted_address;
6c95b8df 9360 loc->pspace = sal->pspace;
729662a5
TT
9361 loc->probe.probe = sal->probe;
9362 loc->probe.objfile = sal->objfile;
6c95b8df 9363 gdb_assert (loc->pspace != NULL);
0d381245 9364 loc->section = sal->section;
3742cc8b 9365 loc->gdbarch = loc_gdbarch;
f8eba3c6 9366 loc->line_number = sal->line;
2f202fde 9367 loc->symtab = sal->symtab;
f8eba3c6 9368
0e30163f
JK
9369 set_breakpoint_location_function (loc,
9370 sal->explicit_pc || sal->explicit_line);
0d381245
VP
9371 return loc;
9372}
514f746b
AR
9373\f
9374
9375/* Return 1 if LOC is pointing to a permanent breakpoint,
9376 return 0 otherwise. */
9377
9378static int
9379bp_loc_is_permanent (struct bp_location *loc)
9380{
9381 int len;
9382 CORE_ADDR addr;
1afeeb75 9383 const gdb_byte *bpoint;
514f746b 9384 gdb_byte *target_mem;
939c61fa
JK
9385 struct cleanup *cleanup;
9386 int retval = 0;
514f746b
AR
9387
9388 gdb_assert (loc != NULL);
9389
9390 addr = loc->address;
1afeeb75 9391 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 9392
939c61fa 9393 /* Software breakpoints unsupported? */
1afeeb75 9394 if (bpoint == NULL)
939c61fa
JK
9395 return 0;
9396
514f746b
AR
9397 target_mem = alloca (len);
9398
939c61fa
JK
9399 /* Enable the automatic memory restoration from breakpoints while
9400 we read the memory. Otherwise we could say about our temporary
9401 breakpoints they are permanent. */
6c95b8df
PA
9402 cleanup = save_current_space_and_thread ();
9403
9404 switch_to_program_space_and_thread (loc->pspace);
9405 make_show_memory_breakpoints_cleanup (0);
939c61fa 9406
514f746b 9407 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 9408 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 9409 retval = 1;
514f746b 9410
939c61fa
JK
9411 do_cleanups (cleanup);
9412
9413 return retval;
514f746b
AR
9414}
9415
e7e0cddf
SS
9416/* Build a command list for the dprintf corresponding to the current
9417 settings of the dprintf style options. */
9418
9419static void
9420update_dprintf_command_list (struct breakpoint *b)
9421{
9422 char *dprintf_args = b->extra_string;
9423 char *printf_line = NULL;
9424
9425 if (!dprintf_args)
9426 return;
9427
9428 dprintf_args = skip_spaces (dprintf_args);
9429
9430 /* Allow a comma, as it may have terminated a location, but don't
9431 insist on it. */
9432 if (*dprintf_args == ',')
9433 ++dprintf_args;
9434 dprintf_args = skip_spaces (dprintf_args);
9435
9436 if (*dprintf_args != '"')
9437 error (_("Bad format string, missing '\"'."));
9438
d3ce09f5 9439 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9440 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9441 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9442 {
9443 if (!dprintf_function)
9444 error (_("No function supplied for dprintf call"));
9445
9446 if (dprintf_channel && strlen (dprintf_channel) > 0)
9447 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9448 dprintf_function,
9449 dprintf_channel,
9450 dprintf_args);
9451 else
9452 printf_line = xstrprintf ("call (void) %s (%s)",
9453 dprintf_function,
9454 dprintf_args);
9455 }
d3ce09f5
SS
9456 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9457 {
9458 if (target_can_run_breakpoint_commands ())
9459 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9460 else
9461 {
9462 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9463 printf_line = xstrprintf ("printf %s", dprintf_args);
9464 }
9465 }
e7e0cddf
SS
9466 else
9467 internal_error (__FILE__, __LINE__,
9468 _("Invalid dprintf style."));
9469
f28045c2 9470 gdb_assert (printf_line != NULL);
9d6e6e84 9471 /* Manufacture a printf sequence. */
f28045c2 9472 {
9d6e6e84
HZ
9473 struct command_line *printf_cmd_line
9474 = xmalloc (sizeof (struct command_line));
e7e0cddf 9475
f28045c2
YQ
9476 printf_cmd_line = xmalloc (sizeof (struct command_line));
9477 printf_cmd_line->control_type = simple_control;
9478 printf_cmd_line->body_count = 0;
9479 printf_cmd_line->body_list = NULL;
9d6e6e84 9480 printf_cmd_line->next = NULL;
f28045c2 9481 printf_cmd_line->line = printf_line;
e7e0cddf 9482
f28045c2
YQ
9483 breakpoint_set_commands (b, printf_cmd_line);
9484 }
e7e0cddf
SS
9485}
9486
9487/* Update all dprintf commands, making their command lists reflect
9488 current style settings. */
9489
9490static void
9491update_dprintf_commands (char *args, int from_tty,
9492 struct cmd_list_element *c)
9493{
9494 struct breakpoint *b;
9495
9496 ALL_BREAKPOINTS (b)
9497 {
9498 if (b->type == bp_dprintf)
9499 update_dprintf_command_list (b);
9500 }
9501}
c3f6f71d 9502
018d34a4
VP
9503/* Create a breakpoint with SAL as location. Use ADDR_STRING
9504 as textual description of the location, and COND_STRING
db107f19 9505 as condition expression. */
018d34a4
VP
9506
9507static void
d9b3f62e
PA
9508init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9509 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9510 char *filter, char *cond_string,
e7e0cddf 9511 char *extra_string,
d9b3f62e
PA
9512 enum bptype type, enum bpdisp disposition,
9513 int thread, int task, int ignore_count,
c0a91b2b 9514 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9515 int enabled, int internal, unsigned flags,
9516 int display_canonical)
018d34a4 9517{
0d381245 9518 int i;
018d34a4
VP
9519
9520 if (type == bp_hardware_breakpoint)
9521 {
fbbd034e
AS
9522 int target_resources_ok;
9523
9524 i = hw_breakpoint_used_count ();
9525 target_resources_ok =
9526 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9527 i + 1, 0);
9528 if (target_resources_ok == 0)
9529 error (_("No hardware breakpoint support in the target."));
9530 else if (target_resources_ok < 0)
9531 error (_("Hardware breakpoints used exceeds limit."));
9532 }
9533
6c95b8df
PA
9534 gdb_assert (sals.nelts > 0);
9535
0d381245
VP
9536 for (i = 0; i < sals.nelts; ++i)
9537 {
9538 struct symtab_and_line sal = sals.sals[i];
9539 struct bp_location *loc;
9540
9541 if (from_tty)
5af949e3
UW
9542 {
9543 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9544 if (!loc_gdbarch)
9545 loc_gdbarch = gdbarch;
9546
9547 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9548 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9549 }
0d381245
VP
9550
9551 if (i == 0)
9552 {
d9b3f62e 9553 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9554 b->thread = thread;
4a306c9a 9555 b->task = task;
855a6e68 9556
0d381245 9557 b->cond_string = cond_string;
e7e0cddf 9558 b->extra_string = extra_string;
0d381245 9559 b->ignore_count = ignore_count;
41447f92 9560 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9561 b->disposition = disposition;
6c95b8df 9562
44f238bb
PA
9563 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9564 b->loc->inserted = 1;
9565
0fb4aa4b
PA
9566 if (type == bp_static_tracepoint)
9567 {
d9b3f62e 9568 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9569 struct static_tracepoint_marker marker;
9570
983af33b 9571 if (strace_marker_p (b))
0fb4aa4b
PA
9572 {
9573 /* We already know the marker exists, otherwise, we
9574 wouldn't see a sal for it. */
9575 char *p = &addr_string[3];
9576 char *endp;
9577 char *marker_str;
0fb4aa4b 9578
e9cafbcc 9579 p = skip_spaces (p);
0fb4aa4b 9580
e9cafbcc 9581 endp = skip_to_space (p);
0fb4aa4b
PA
9582
9583 marker_str = savestring (p, endp - p);
d9b3f62e 9584 t->static_trace_marker_id = marker_str;
0fb4aa4b 9585
3e43a32a
MS
9586 printf_filtered (_("Probed static tracepoint "
9587 "marker \"%s\"\n"),
d9b3f62e 9588 t->static_trace_marker_id);
0fb4aa4b
PA
9589 }
9590 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9591 {
d9b3f62e 9592 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9593 release_static_tracepoint_marker (&marker);
9594
3e43a32a
MS
9595 printf_filtered (_("Probed static tracepoint "
9596 "marker \"%s\"\n"),
d9b3f62e 9597 t->static_trace_marker_id);
0fb4aa4b
PA
9598 }
9599 else
3e43a32a
MS
9600 warning (_("Couldn't determine the static "
9601 "tracepoint marker to probe"));
0fb4aa4b
PA
9602 }
9603
0d381245
VP
9604 loc = b->loc;
9605 }
9606 else
018d34a4 9607 {
39d61571 9608 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9609 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9610 loc->inserted = 1;
0d381245
VP
9611 }
9612
514f746b
AR
9613 if (bp_loc_is_permanent (loc))
9614 make_breakpoint_permanent (b);
9615
0d381245
VP
9616 if (b->cond_string)
9617 {
bbc13ae3
KS
9618 const char *arg = b->cond_string;
9619
1bb9788d
TT
9620 loc->cond = parse_exp_1 (&arg, loc->address,
9621 block_for_pc (loc->address), 0);
0d381245 9622 if (*arg)
588ae58c 9623 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9624 }
e7e0cddf
SS
9625
9626 /* Dynamic printf requires and uses additional arguments on the
9627 command line, otherwise it's an error. */
9628 if (type == bp_dprintf)
9629 {
9630 if (b->extra_string)
9631 update_dprintf_command_list (b);
9632 else
9633 error (_("Format string required"));
9634 }
9635 else if (b->extra_string)
588ae58c 9636 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9637 }
018d34a4 9638
56435ebe 9639 b->display_canonical = display_canonical;
018d34a4
VP
9640 if (addr_string)
9641 b->addr_string = addr_string;
9642 else
9643 /* addr_string has to be used or breakpoint_re_set will delete
9644 me. */
5af949e3
UW
9645 b->addr_string
9646 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9647 b->filter = filter;
d9b3f62e 9648}
018d34a4 9649
d9b3f62e
PA
9650static void
9651create_breakpoint_sal (struct gdbarch *gdbarch,
9652 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9653 char *filter, char *cond_string,
e7e0cddf 9654 char *extra_string,
d9b3f62e
PA
9655 enum bptype type, enum bpdisp disposition,
9656 int thread, int task, int ignore_count,
c0a91b2b 9657 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9658 int enabled, int internal, unsigned flags,
9659 int display_canonical)
d9b3f62e
PA
9660{
9661 struct breakpoint *b;
9662 struct cleanup *old_chain;
9663
9664 if (is_tracepoint_type (type))
9665 {
9666 struct tracepoint *t;
9667
9668 t = XCNEW (struct tracepoint);
9669 b = &t->base;
9670 }
9671 else
9672 b = XNEW (struct breakpoint);
9673
9674 old_chain = make_cleanup (xfree, b);
9675
9676 init_breakpoint_sal (b, gdbarch,
9677 sals, addr_string,
e7e0cddf 9678 filter, cond_string, extra_string,
d9b3f62e
PA
9679 type, disposition,
9680 thread, task, ignore_count,
9681 ops, from_tty,
44f238bb
PA
9682 enabled, internal, flags,
9683 display_canonical);
d9b3f62e
PA
9684 discard_cleanups (old_chain);
9685
3ea46bff 9686 install_breakpoint (internal, b, 0);
018d34a4
VP
9687}
9688
9689/* Add SALS.nelts breakpoints to the breakpoint table. For each
9690 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9691 value. COND_STRING, if not NULL, specified the condition to be
9692 used for all breakpoints. Essentially the only case where
9693 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9694 function. In that case, it's still not possible to specify
9695 separate conditions for different overloaded functions, so
9696 we take just a single condition string.
9697
c3f6f71d 9698 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9699 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9700 array contents). If the function fails (error() is called), the
9701 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9702 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9703
9704static void
8cdf0e15 9705create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9706 struct linespec_result *canonical,
e7e0cddf 9707 char *cond_string, char *extra_string,
8cdf0e15
VP
9708 enum bptype type, enum bpdisp disposition,
9709 int thread, int task, int ignore_count,
c0a91b2b 9710 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9711 int enabled, int internal, unsigned flags)
c906108c 9712{
018d34a4 9713 int i;
f8eba3c6 9714 struct linespec_sals *lsal;
cc59ec59 9715
f8eba3c6
TT
9716 if (canonical->pre_expanded)
9717 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9718
9719 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9720 {
f8eba3c6
TT
9721 /* Note that 'addr_string' can be NULL in the case of a plain
9722 'break', without arguments. */
9723 char *addr_string = (canonical->addr_string
9724 ? xstrdup (canonical->addr_string)
9725 : NULL);
9726 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9727 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9728
f8eba3c6
TT
9729 make_cleanup (xfree, filter_string);
9730 create_breakpoint_sal (gdbarch, lsal->sals,
9731 addr_string,
9732 filter_string,
e7e0cddf
SS
9733 cond_string, extra_string,
9734 type, disposition,
84f4c1fe 9735 thread, task, ignore_count, ops,
44f238bb 9736 from_tty, enabled, internal, flags,
56435ebe 9737 canonical->special_display);
f8eba3c6 9738 discard_cleanups (inner);
c3f6f71d 9739 }
c3f6f71d 9740}
c906108c 9741
9998af43 9742/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9743 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9744 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9745 address strings. ADDRESS points to the end of the SAL.
9746
9747 The array and the line spec strings are allocated on the heap, it is
9748 the caller's responsibility to free them. */
c906108c 9749
b9362cc7 9750static void
c3f6f71d 9751parse_breakpoint_sals (char **address,
58438ac1 9752 struct linespec_result *canonical)
c3f6f71d 9753{
c3f6f71d 9754 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9755 breakpoint. */
c3f6f71d
JM
9756 if ((*address) == NULL
9757 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9758 {
1bfeeb0f
JL
9759 /* The last displayed codepoint, if it's valid, is our default breakpoint
9760 address. */
9761 if (last_displayed_sal_is_valid ())
c906108c 9762 {
f8eba3c6 9763 struct linespec_sals lsal;
c3f6f71d 9764 struct symtab_and_line sal;
1c8cdcb1 9765 CORE_ADDR pc;
cc59ec59 9766
4a64f543 9767 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9768 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9769 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9770
9771 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9772 corresponding to the last call to print_frame_info.
9773 Be sure to reinitialize LINE with NOTCURRENT == 0
9774 as the breakpoint line number is inappropriate otherwise.
9775 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9776 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9777 pc = sal.pc;
9778 sal = find_pc_line (pc, 0);
00903456 9779
4a64f543 9780 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9781 where PC is the last displayed codepoint's address. So
9782 make sure to set sal.explicit_pc to prevent GDB from
9783 trying to expand the list of sals to include all other
9784 instances with the same symtab and line. */
1c8cdcb1 9785 sal.pc = pc;
00903456
JK
9786 sal.explicit_pc = 1;
9787
f8eba3c6
TT
9788 lsal.sals.sals[0] = sal;
9789 lsal.sals.nelts = 1;
9790 lsal.canonical = NULL;
9791
9792 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9793 }
9794 else
8a3fe4f8 9795 error (_("No default breakpoint address now."));
c906108c
SS
9796 }
9797 else
9798 {
cc80f267
JK
9799 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9800
c906108c 9801 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9802 current_source_symtab (which is decode_line_1's default).
9803 This should produce the results we want almost all of the
cc80f267
JK
9804 time while leaving default_breakpoint_* alone.
9805
9806 ObjC: However, don't match an Objective-C method name which
9807 may have a '+' or '-' succeeded by a '['. */
9808 if (last_displayed_sal_is_valid ()
9809 && (!cursal.symtab
9810 || ((strchr ("+-", (*address)[0]) != NULL)
9811 && ((*address)[1] != '['))))
f8eba3c6
TT
9812 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9813 get_last_displayed_symtab (),
9814 get_last_displayed_line (),
9815 canonical, NULL, NULL);
c906108c 9816 else
f8eba3c6 9817 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9818 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9819 }
c3f6f71d 9820}
c906108c 9821
c906108c 9822
c3f6f71d 9823/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9824 inserted as a breakpoint. If it can't throw an error. */
c906108c 9825
b9362cc7 9826static void
23e7acfb 9827breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9828{
9829 int i;
cc59ec59 9830
c3f6f71d 9831 for (i = 0; i < sals->nelts; i++)
ee53e872 9832 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9833}
9834
7a697b8d
SS
9835/* Fast tracepoints may have restrictions on valid locations. For
9836 instance, a fast tracepoint using a jump instead of a trap will
9837 likely have to overwrite more bytes than a trap would, and so can
9838 only be placed where the instruction is longer than the jump, or a
9839 multi-instruction sequence does not have a jump into the middle of
9840 it, etc. */
9841
9842static void
9843check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9844 struct symtabs_and_lines *sals)
9845{
9846 int i, rslt;
9847 struct symtab_and_line *sal;
9848 char *msg;
9849 struct cleanup *old_chain;
9850
9851 for (i = 0; i < sals->nelts; i++)
9852 {
f8eba3c6
TT
9853 struct gdbarch *sarch;
9854
7a697b8d
SS
9855 sal = &sals->sals[i];
9856
f8eba3c6
TT
9857 sarch = get_sal_arch (*sal);
9858 /* We fall back to GDBARCH if there is no architecture
9859 associated with SAL. */
9860 if (sarch == NULL)
9861 sarch = gdbarch;
9862 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9863 NULL, &msg);
9864 old_chain = make_cleanup (xfree, msg);
9865
9866 if (!rslt)
9867 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9868 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9869
9870 do_cleanups (old_chain);
9871 }
9872}
9873
af4908ba
KS
9874/* Issue an invalid thread ID error. */
9875
9876static void ATTRIBUTE_NORETURN
9877invalid_thread_id_error (int id)
9878{
9879 error (_("Unknown thread %d."), id);
9880}
9881
018d34a4
VP
9882/* Given TOK, a string specification of condition and thread, as
9883 accepted by the 'break' command, extract the condition
9884 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9885 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9886 If no condition is found, *COND_STRING is set to NULL.
9887 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9888
9889static void
bbc13ae3 9890find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9891 char **cond_string, int *thread, int *task,
9892 char **rest)
018d34a4
VP
9893{
9894 *cond_string = NULL;
9895 *thread = -1;
ed1d1739
KS
9896 *task = 0;
9897 *rest = NULL;
9898
018d34a4
VP
9899 while (tok && *tok)
9900 {
bbc13ae3 9901 const char *end_tok;
018d34a4 9902 int toklen;
bbc13ae3
KS
9903 const char *cond_start = NULL;
9904 const char *cond_end = NULL;
cc59ec59 9905
bbc13ae3 9906 tok = skip_spaces_const (tok);
e7e0cddf
SS
9907
9908 if ((*tok == '"' || *tok == ',') && rest)
9909 {
9910 *rest = savestring (tok, strlen (tok));
9911 return;
9912 }
9913
bbc13ae3 9914 end_tok = skip_to_space_const (tok);
d634f2de 9915
018d34a4 9916 toklen = end_tok - tok;
d634f2de 9917
018d34a4
VP
9918 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9919 {
f7545552
TT
9920 struct expression *expr;
9921
018d34a4 9922 tok = cond_start = end_tok + 1;
1bb9788d 9923 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9924 xfree (expr);
018d34a4 9925 cond_end = tok;
d634f2de 9926 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9927 }
9928 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9929 {
9930 char *tmptok;
d634f2de 9931
018d34a4 9932 tok = end_tok + 1;
bbc13ae3 9933 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9934 if (tok == tmptok)
9935 error (_("Junk after thread keyword."));
9936 if (!valid_thread_id (*thread))
af4908ba 9937 invalid_thread_id_error (*thread);
bbc13ae3 9938 tok = tmptok;
018d34a4 9939 }
4a306c9a
JB
9940 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9941 {
9942 char *tmptok;
9943
9944 tok = end_tok + 1;
bbc13ae3 9945 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9946 if (tok == tmptok)
9947 error (_("Junk after task keyword."));
9948 if (!valid_task_id (*task))
b6199126 9949 error (_("Unknown task %d."), *task);
bbc13ae3 9950 tok = tmptok;
4a306c9a 9951 }
e7e0cddf
SS
9952 else if (rest)
9953 {
9954 *rest = savestring (tok, strlen (tok));
ccab2054 9955 return;
e7e0cddf 9956 }
018d34a4
VP
9957 else
9958 error (_("Junk at end of arguments."));
9959 }
9960}
9961
0fb4aa4b
PA
9962/* Decode a static tracepoint marker spec. */
9963
9964static struct symtabs_and_lines
9965decode_static_tracepoint_spec (char **arg_p)
9966{
9967 VEC(static_tracepoint_marker_p) *markers = NULL;
9968 struct symtabs_and_lines sals;
0fb4aa4b
PA
9969 struct cleanup *old_chain;
9970 char *p = &(*arg_p)[3];
9971 char *endp;
9972 char *marker_str;
9973 int i;
9974
e9cafbcc 9975 p = skip_spaces (p);
0fb4aa4b 9976
e9cafbcc 9977 endp = skip_to_space (p);
0fb4aa4b
PA
9978
9979 marker_str = savestring (p, endp - p);
9980 old_chain = make_cleanup (xfree, marker_str);
9981
9982 markers = target_static_tracepoint_markers_by_strid (marker_str);
9983 if (VEC_empty(static_tracepoint_marker_p, markers))
9984 error (_("No known static tracepoint marker named %s"), marker_str);
9985
9986 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9987 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9988
9989 for (i = 0; i < sals.nelts; i++)
9990 {
9991 struct static_tracepoint_marker *marker;
9992
9993 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9994
9995 init_sal (&sals.sals[i]);
9996
9997 sals.sals[i] = find_pc_line (marker->address, 0);
9998 sals.sals[i].pc = marker->address;
9999
10000 release_static_tracepoint_marker (marker);
10001 }
10002
10003 do_cleanups (old_chain);
10004
10005 *arg_p = endp;
10006 return sals;
10007}
10008
fd9b8c24
PA
10009/* Set a breakpoint. This function is shared between CLI and MI
10010 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
10011 modes of operations, selected by the PARSE_ARG parameter. If
10012 non-zero, the function will parse ARG, extracting location,
10013 condition, thread and extra string. Otherwise, ARG is just the
10014 breakpoint's location, with condition, thread, and extra string
10015 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
10016 If INTERNAL is non-zero, the breakpoint number will be allocated
10017 from the internal breakpoint count. Returns true if any breakpoint
10018 was created; false otherwise. */
0101ce28 10019
8cdf0e15
VP
10020int
10021create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
10022 char *arg, char *cond_string,
10023 int thread, char *extra_string,
f6de8ec2 10024 int parse_arg,
0fb4aa4b 10025 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
10026 int ignore_count,
10027 enum auto_boolean pending_break_support,
c0a91b2b 10028 const struct breakpoint_ops *ops,
44f238bb
PA
10029 int from_tty, int enabled, int internal,
10030 unsigned flags)
c3f6f71d 10031{
b78a6381 10032 volatile struct gdb_exception e;
f8eba3c6 10033 char *copy_arg = NULL;
c3f6f71d 10034 char *addr_start = arg;
7efd8fc2 10035 struct linespec_result canonical;
c3f6f71d 10036 struct cleanup *old_chain;
80c99de1 10037 struct cleanup *bkpt_chain = NULL;
0101ce28 10038 int pending = 0;
4a306c9a 10039 int task = 0;
86b17b60 10040 int prev_bkpt_count = breakpoint_count;
c3f6f71d 10041
348d480f
PA
10042 gdb_assert (ops != NULL);
10043
7efd8fc2 10044 init_linespec_result (&canonical);
c3f6f71d 10045
b78a6381
TT
10046 TRY_CATCH (e, RETURN_MASK_ALL)
10047 {
983af33b
SDJ
10048 ops->create_sals_from_address (&arg, &canonical, type_wanted,
10049 addr_start, &copy_arg);
b78a6381 10050 }
0101ce28
JJ
10051
10052 /* If caller is interested in rc value from parse, set value. */
05ff989b 10053 switch (e.reason)
0101ce28 10054 {
983af33b
SDJ
10055 case GDB_NO_ERROR:
10056 if (VEC_empty (linespec_sals, canonical.sals))
10057 return 0;
10058 break;
05ff989b
AC
10059 case RETURN_ERROR:
10060 switch (e.error)
0101ce28 10061 {
05ff989b 10062 case NOT_FOUND_ERROR:
0101ce28 10063
05ff989b
AC
10064 /* If pending breakpoint support is turned off, throw
10065 error. */
fa8d40ab
JJ
10066
10067 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
10068 throw_exception (e);
10069
10070 exception_print (gdb_stderr, e);
fa8d40ab 10071
05ff989b
AC
10072 /* If pending breakpoint support is auto query and the user
10073 selects no, then simply return the error code. */
059fb39f 10074 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
10075 && !nquery (_("Make %s pending on future shared library load? "),
10076 bptype_string (type_wanted)))
fd9b8c24 10077 return 0;
fa8d40ab 10078
05ff989b
AC
10079 /* At this point, either the user was queried about setting
10080 a pending breakpoint and selected yes, or pending
10081 breakpoint behavior is on and thus a pending breakpoint
10082 is defaulted on behalf of the user. */
f8eba3c6
TT
10083 {
10084 struct linespec_sals lsal;
10085
10086 copy_arg = xstrdup (addr_start);
10087 lsal.canonical = xstrdup (copy_arg);
10088 lsal.sals.nelts = 1;
10089 lsal.sals.sals = XNEW (struct symtab_and_line);
10090 init_sal (&lsal.sals.sals[0]);
10091 pending = 1;
10092 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
10093 }
05ff989b
AC
10094 break;
10095 default:
98deb0da 10096 throw_exception (e);
0101ce28 10097 }
2abae994 10098 break;
05ff989b 10099 default:
983af33b 10100 throw_exception (e);
0101ce28 10101 }
c3f6f71d 10102
4a64f543 10103 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 10104 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 10105
c3f6f71d
JM
10106 /* ----------------------------- SNIP -----------------------------
10107 Anything added to the cleanup chain beyond this point is assumed
10108 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
10109 then the memory is not reclaimed. */
10110 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 10111
c3f6f71d
JM
10112 /* Resolve all line numbers to PC's and verify that the addresses
10113 are ok for the target. */
0101ce28 10114 if (!pending)
f8eba3c6
TT
10115 {
10116 int ix;
10117 struct linespec_sals *iter;
10118
10119 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10120 breakpoint_sals_to_pc (&iter->sals);
10121 }
c3f6f71d 10122
7a697b8d 10123 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 10124 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
10125 {
10126 int ix;
10127 struct linespec_sals *iter;
10128
10129 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10130 check_fast_tracepoint_sals (gdbarch, &iter->sals);
10131 }
7a697b8d 10132
c3f6f71d
JM
10133 /* Verify that condition can be parsed, before setting any
10134 breakpoints. Allocate a separate condition expression for each
4a64f543 10135 breakpoint. */
0101ce28 10136 if (!pending)
c3f6f71d 10137 {
f6de8ec2 10138 if (parse_arg)
72b2ff0e 10139 {
0878d0fa 10140 char *rest;
52d361e1
YQ
10141 struct linespec_sals *lsal;
10142
10143 lsal = VEC_index (linespec_sals, canonical.sals, 0);
10144
0878d0fa
YQ
10145 /* Here we only parse 'arg' to separate condition
10146 from thread number, so parsing in context of first
10147 sal is OK. When setting the breakpoint we'll
10148 re-parse it in context of each sal. */
10149
10150 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
10151 &thread, &task, &rest);
10152 if (cond_string)
10153 make_cleanup (xfree, cond_string);
10154 if (rest)
10155 make_cleanup (xfree, rest);
10156 if (rest)
10157 extra_string = rest;
72b2ff0e 10158 }
2f069f6f 10159 else
72b2ff0e 10160 {
0878d0fa
YQ
10161 if (*arg != '\0')
10162 error (_("Garbage '%s' at end of location"), arg);
10163
10164 /* Create a private copy of condition string. */
10165 if (cond_string)
10166 {
10167 cond_string = xstrdup (cond_string);
10168 make_cleanup (xfree, cond_string);
10169 }
10170 /* Create a private copy of any extra string. */
10171 if (extra_string)
10172 {
10173 extra_string = xstrdup (extra_string);
10174 make_cleanup (xfree, extra_string);
10175 }
72b2ff0e 10176 }
0fb4aa4b 10177
52d361e1 10178 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 10179 cond_string, extra_string, type_wanted,
d9b3f62e
PA
10180 tempflag ? disp_del : disp_donttouch,
10181 thread, task, ignore_count, ops,
44f238bb 10182 from_tty, enabled, internal, flags);
c906108c 10183 }
0101ce28
JJ
10184 else
10185 {
0101ce28
JJ
10186 struct breakpoint *b;
10187
0101ce28
JJ
10188 make_cleanup (xfree, copy_arg);
10189
bfccc43c
YQ
10190 if (is_tracepoint_type (type_wanted))
10191 {
10192 struct tracepoint *t;
10193
10194 t = XCNEW (struct tracepoint);
10195 b = &t->base;
10196 }
10197 else
10198 b = XNEW (struct breakpoint);
10199
10200 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10201
f8eba3c6 10202 b->addr_string = copy_arg;
f6de8ec2 10203 if (parse_arg)
e12c7713
MK
10204 b->cond_string = NULL;
10205 else
10206 {
10207 /* Create a private copy of condition string. */
10208 if (cond_string)
10209 {
10210 cond_string = xstrdup (cond_string);
10211 make_cleanup (xfree, cond_string);
10212 }
10213 b->cond_string = cond_string;
10214 }
e7e0cddf 10215 b->extra_string = NULL;
0101ce28 10216 b->ignore_count = ignore_count;
0101ce28 10217 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 10218 b->condition_not_parsed = 1;
41447f92 10219 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
10220 if ((type_wanted != bp_breakpoint
10221 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 10222 b->pspace = current_program_space;
8bea4e01 10223
bfccc43c 10224 install_breakpoint (internal, b, 0);
0101ce28
JJ
10225 }
10226
f8eba3c6 10227 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 10228 {
3e43a32a
MS
10229 warning (_("Multiple breakpoints were set.\nUse the "
10230 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 10231 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
10232 }
10233
80c99de1
PA
10234 /* That's it. Discard the cleanups for data inserted into the
10235 breakpoint. */
10236 discard_cleanups (bkpt_chain);
10237 /* But cleanup everything else. */
c3f6f71d 10238 do_cleanups (old_chain);
217dc9e2 10239
80c99de1 10240 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 10241 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
10242
10243 return 1;
c3f6f71d 10244}
c906108c 10245
348d480f 10246/* Set a breakpoint.
72b2ff0e
VP
10247 ARG is a string describing breakpoint address,
10248 condition, and thread.
10249 FLAG specifies if a breakpoint is hardware on,
10250 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10251 and BP_TEMPFLAG. */
348d480f 10252
98deb0da 10253static void
72b2ff0e 10254break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10255{
72b2ff0e 10256 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10257 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10258 ? bp_hardware_breakpoint
10259 : bp_breakpoint);
55aa24fb
SDJ
10260 struct breakpoint_ops *ops;
10261 const char *arg_cp = arg;
10262
10263 /* Matching breakpoints on probes. */
10264 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10265 ops = &bkpt_probe_breakpoint_ops;
10266 else
10267 ops = &bkpt_breakpoint_ops;
c3f6f71d 10268
8cdf0e15
VP
10269 create_breakpoint (get_current_arch (),
10270 arg,
e7e0cddf 10271 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 10272 tempflag, type_wanted,
8cdf0e15
VP
10273 0 /* Ignore count */,
10274 pending_break_support,
55aa24fb 10275 ops,
8cdf0e15 10276 from_tty,
84f4c1fe 10277 1 /* enabled */,
44f238bb
PA
10278 0 /* internal */,
10279 0);
c906108c
SS
10280}
10281
c906108c
SS
10282/* Helper function for break_command_1 and disassemble_command. */
10283
10284void
fba45db2 10285resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10286{
10287 CORE_ADDR pc;
10288
10289 if (sal->pc == 0 && sal->symtab != NULL)
10290 {
10291 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10292 error (_("No line %d in file \"%s\"."),
05cba821 10293 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10294 sal->pc = pc;
6a048695 10295
4a64f543
MS
10296 /* If this SAL corresponds to a breakpoint inserted using a line
10297 number, then skip the function prologue if necessary. */
6a048695 10298 if (sal->explicit_line)
059acae7 10299 skip_prologue_sal (sal);
c906108c
SS
10300 }
10301
10302 if (sal->section == 0 && sal->symtab != NULL)
10303 {
346d1dfe 10304 const struct blockvector *bv;
3977b71f 10305 const struct block *b;
c5aa993b 10306 struct symbol *sym;
c906108c 10307
801e3a5b 10308 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
10309 if (bv != NULL)
10310 {
7f0df278 10311 sym = block_linkage_function (b);
c906108c
SS
10312 if (sym != NULL)
10313 {
10314 fixup_symbol_section (sym, sal->symtab->objfile);
e27d198c 10315 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
c906108c
SS
10316 }
10317 else
10318 {
4a64f543
MS
10319 /* It really is worthwhile to have the section, so we'll
10320 just have to look harder. This case can be executed
10321 if we have line numbers but no functions (as can
10322 happen in assembly source). */
c906108c 10323
7cbd4a93 10324 struct bound_minimal_symbol msym;
6c95b8df
PA
10325 struct cleanup *old_chain = save_current_space_and_thread ();
10326
10327 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10328
10329 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10330 if (msym.minsym)
efd66ac6 10331 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10332
10333 do_cleanups (old_chain);
c906108c
SS
10334 }
10335 }
10336 }
10337}
10338
10339void
fba45db2 10340break_command (char *arg, int from_tty)
c906108c 10341{
db107f19 10342 break_command_1 (arg, 0, from_tty);
c906108c
SS
10343}
10344
c906108c 10345void
fba45db2 10346tbreak_command (char *arg, int from_tty)
c906108c 10347{
db107f19 10348 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10349}
10350
c906108c 10351static void
fba45db2 10352hbreak_command (char *arg, int from_tty)
c906108c 10353{
db107f19 10354 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10355}
10356
10357static void
fba45db2 10358thbreak_command (char *arg, int from_tty)
c906108c 10359{
db107f19 10360 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10361}
10362
10363static void
fba45db2 10364stop_command (char *arg, int from_tty)
c906108c 10365{
a3f17187 10366 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10367Usage: stop in <function | address>\n\
a3f17187 10368 stop at <line>\n"));
c906108c
SS
10369}
10370
10371static void
fba45db2 10372stopin_command (char *arg, int from_tty)
c906108c
SS
10373{
10374 int badInput = 0;
10375
c5aa993b 10376 if (arg == (char *) NULL)
c906108c
SS
10377 badInput = 1;
10378 else if (*arg != '*')
10379 {
10380 char *argptr = arg;
10381 int hasColon = 0;
10382
4a64f543 10383 /* Look for a ':'. If this is a line number specification, then
53a5351d 10384 say it is bad, otherwise, it should be an address or
4a64f543 10385 function/method name. */
c906108c 10386 while (*argptr && !hasColon)
c5aa993b
JM
10387 {
10388 hasColon = (*argptr == ':');
10389 argptr++;
10390 }
c906108c
SS
10391
10392 if (hasColon)
c5aa993b 10393 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10394 else
c5aa993b 10395 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10396 }
10397
10398 if (badInput)
a3f17187 10399 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10400 else
db107f19 10401 break_command_1 (arg, 0, from_tty);
c906108c
SS
10402}
10403
10404static void
fba45db2 10405stopat_command (char *arg, int from_tty)
c906108c
SS
10406{
10407 int badInput = 0;
10408
c5aa993b 10409 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10410 badInput = 1;
10411 else
10412 {
10413 char *argptr = arg;
10414 int hasColon = 0;
10415
4a64f543
MS
10416 /* Look for a ':'. If there is a '::' then get out, otherwise
10417 it is probably a line number. */
c906108c 10418 while (*argptr && !hasColon)
c5aa993b
JM
10419 {
10420 hasColon = (*argptr == ':');
10421 argptr++;
10422 }
c906108c
SS
10423
10424 if (hasColon)
c5aa993b 10425 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10426 else
c5aa993b 10427 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10428 }
10429
10430 if (badInput)
a3f17187 10431 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10432 else
db107f19 10433 break_command_1 (arg, 0, from_tty);
c906108c
SS
10434}
10435
e7e0cddf
SS
10436/* The dynamic printf command is mostly like a regular breakpoint, but
10437 with a prewired command list consisting of a single output command,
10438 built from extra arguments supplied on the dprintf command
10439 line. */
10440
da821c7b 10441static void
e7e0cddf
SS
10442dprintf_command (char *arg, int from_tty)
10443{
10444 create_breakpoint (get_current_arch (),
10445 arg,
10446 NULL, 0, NULL, 1 /* parse arg */,
10447 0, bp_dprintf,
10448 0 /* Ignore count */,
10449 pending_break_support,
10450 &dprintf_breakpoint_ops,
10451 from_tty,
10452 1 /* enabled */,
10453 0 /* internal */,
10454 0);
10455}
10456
d3ce09f5
SS
10457static void
10458agent_printf_command (char *arg, int from_tty)
10459{
10460 error (_("May only run agent-printf on the target"));
10461}
10462
f1310107
TJB
10463/* Implement the "breakpoint_hit" breakpoint_ops method for
10464 ranged breakpoints. */
10465
10466static int
10467breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10468 struct address_space *aspace,
09ac7c10
TT
10469 CORE_ADDR bp_addr,
10470 const struct target_waitstatus *ws)
f1310107 10471{
09ac7c10 10472 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10473 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10474 return 0;
10475
f1310107
TJB
10476 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10477 bl->length, aspace, bp_addr);
10478}
10479
10480/* Implement the "resources_needed" breakpoint_ops method for
10481 ranged breakpoints. */
10482
10483static int
10484resources_needed_ranged_breakpoint (const struct bp_location *bl)
10485{
10486 return target_ranged_break_num_registers ();
10487}
10488
10489/* Implement the "print_it" breakpoint_ops method for
10490 ranged breakpoints. */
10491
10492static enum print_stop_action
348d480f 10493print_it_ranged_breakpoint (bpstat bs)
f1310107 10494{
348d480f 10495 struct breakpoint *b = bs->breakpoint_at;
f1310107 10496 struct bp_location *bl = b->loc;
79a45e25 10497 struct ui_out *uiout = current_uiout;
f1310107
TJB
10498
10499 gdb_assert (b->type == bp_hardware_breakpoint);
10500
10501 /* Ranged breakpoints have only one location. */
10502 gdb_assert (bl && bl->next == NULL);
10503
10504 annotate_breakpoint (b->number);
10505 if (b->disposition == disp_del)
10506 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10507 else
10508 ui_out_text (uiout, "\nRanged breakpoint ");
10509 if (ui_out_is_mi_like_p (uiout))
10510 {
10511 ui_out_field_string (uiout, "reason",
10512 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10513 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10514 }
10515 ui_out_field_int (uiout, "bkptno", b->number);
10516 ui_out_text (uiout, ", ");
10517
10518 return PRINT_SRC_AND_LOC;
10519}
10520
10521/* Implement the "print_one" breakpoint_ops method for
10522 ranged breakpoints. */
10523
10524static void
10525print_one_ranged_breakpoint (struct breakpoint *b,
10526 struct bp_location **last_loc)
10527{
10528 struct bp_location *bl = b->loc;
10529 struct value_print_options opts;
79a45e25 10530 struct ui_out *uiout = current_uiout;
f1310107
TJB
10531
10532 /* Ranged breakpoints have only one location. */
10533 gdb_assert (bl && bl->next == NULL);
10534
10535 get_user_print_options (&opts);
10536
10537 if (opts.addressprint)
10538 /* We don't print the address range here, it will be printed later
10539 by print_one_detail_ranged_breakpoint. */
10540 ui_out_field_skip (uiout, "addr");
10541 annotate_field (5);
10542 print_breakpoint_location (b, bl);
10543 *last_loc = bl;
10544}
10545
10546/* Implement the "print_one_detail" breakpoint_ops method for
10547 ranged breakpoints. */
10548
10549static void
10550print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10551 struct ui_out *uiout)
10552{
10553 CORE_ADDR address_start, address_end;
10554 struct bp_location *bl = b->loc;
f99d8bf4
PA
10555 struct ui_file *stb = mem_fileopen ();
10556 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10557
10558 gdb_assert (bl);
10559
10560 address_start = bl->address;
10561 address_end = address_start + bl->length - 1;
10562
10563 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10564 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10565 print_core_address (bl->gdbarch, address_start),
10566 print_core_address (bl->gdbarch, address_end));
10567 ui_out_field_stream (uiout, "addr", stb);
10568 ui_out_text (uiout, "\n");
10569
10570 do_cleanups (cleanup);
10571}
10572
10573/* Implement the "print_mention" breakpoint_ops method for
10574 ranged breakpoints. */
10575
10576static void
10577print_mention_ranged_breakpoint (struct breakpoint *b)
10578{
10579 struct bp_location *bl = b->loc;
79a45e25 10580 struct ui_out *uiout = current_uiout;
f1310107
TJB
10581
10582 gdb_assert (bl);
10583 gdb_assert (b->type == bp_hardware_breakpoint);
10584
10585 if (ui_out_is_mi_like_p (uiout))
10586 return;
10587
10588 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10589 b->number, paddress (bl->gdbarch, bl->address),
10590 paddress (bl->gdbarch, bl->address + bl->length - 1));
10591}
10592
10593/* Implement the "print_recreate" breakpoint_ops method for
10594 ranged breakpoints. */
10595
10596static void
10597print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10598{
10599 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10600 b->addr_string_range_end);
d9b3f62e 10601 print_recreate_thread (b, fp);
f1310107
TJB
10602}
10603
10604/* The breakpoint_ops structure to be used in ranged breakpoints. */
10605
2060206e 10606static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10607
10608/* Find the address where the end of the breakpoint range should be
10609 placed, given the SAL of the end of the range. This is so that if
10610 the user provides a line number, the end of the range is set to the
10611 last instruction of the given line. */
10612
10613static CORE_ADDR
10614find_breakpoint_range_end (struct symtab_and_line sal)
10615{
10616 CORE_ADDR end;
10617
10618 /* If the user provided a PC value, use it. Otherwise,
10619 find the address of the end of the given location. */
10620 if (sal.explicit_pc)
10621 end = sal.pc;
10622 else
10623 {
10624 int ret;
10625 CORE_ADDR start;
10626
10627 ret = find_line_pc_range (sal, &start, &end);
10628 if (!ret)
10629 error (_("Could not find location of the end of the range."));
10630
10631 /* find_line_pc_range returns the start of the next line. */
10632 end--;
10633 }
10634
10635 return end;
10636}
10637
10638/* Implement the "break-range" CLI command. */
10639
10640static void
10641break_range_command (char *arg, int from_tty)
10642{
10643 char *arg_start, *addr_string_start, *addr_string_end;
10644 struct linespec_result canonical_start, canonical_end;
10645 int bp_count, can_use_bp, length;
10646 CORE_ADDR end;
10647 struct breakpoint *b;
10648 struct symtab_and_line sal_start, sal_end;
f1310107 10649 struct cleanup *cleanup_bkpt;
f8eba3c6 10650 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10651
10652 /* We don't support software ranged breakpoints. */
10653 if (target_ranged_break_num_registers () < 0)
10654 error (_("This target does not support hardware ranged breakpoints."));
10655
10656 bp_count = hw_breakpoint_used_count ();
10657 bp_count += target_ranged_break_num_registers ();
10658 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10659 bp_count, 0);
10660 if (can_use_bp < 0)
10661 error (_("Hardware breakpoints used exceeds limit."));
10662
f8eba3c6 10663 arg = skip_spaces (arg);
f1310107
TJB
10664 if (arg == NULL || arg[0] == '\0')
10665 error(_("No address range specified."));
10666
f1310107
TJB
10667 init_linespec_result (&canonical_start);
10668
f8eba3c6
TT
10669 arg_start = arg;
10670 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10671
f8eba3c6 10672 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10673
10674 if (arg[0] != ',')
10675 error (_("Too few arguments."));
f8eba3c6 10676 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10677 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10678
10679 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10680
10681 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10682 || lsal_start->sals.nelts != 1)
f1310107
TJB
10683 error (_("Cannot create a ranged breakpoint with multiple locations."));
10684
f8eba3c6
TT
10685 sal_start = lsal_start->sals.sals[0];
10686 addr_string_start = savestring (arg_start, arg - arg_start);
10687 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10688
10689 arg++; /* Skip the comma. */
f8eba3c6 10690 arg = skip_spaces (arg);
f1310107
TJB
10691
10692 /* Parse the end location. */
10693
f1310107
TJB
10694 init_linespec_result (&canonical_end);
10695 arg_start = arg;
10696
f8eba3c6 10697 /* We call decode_line_full directly here instead of using
f1310107
TJB
10698 parse_breakpoint_sals because we need to specify the start location's
10699 symtab and line as the default symtab and line for the end of the
10700 range. This makes it possible to have ranges like "foo.c:27, +14",
10701 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10702 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10703 sal_start.symtab, sal_start.line,
10704 &canonical_end, NULL, NULL);
10705
10706 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10707
f8eba3c6 10708 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10709 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10710
10711 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10712 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10713 || lsal_end->sals.nelts != 1)
f1310107
TJB
10714 error (_("Cannot create a ranged breakpoint with multiple locations."));
10715
f8eba3c6
TT
10716 sal_end = lsal_end->sals.sals[0];
10717 addr_string_end = savestring (arg_start, arg - arg_start);
10718 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10719
10720 end = find_breakpoint_range_end (sal_end);
10721 if (sal_start.pc > end)
177b42fe 10722 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10723
10724 length = end - sal_start.pc + 1;
10725 if (length < 0)
10726 /* Length overflowed. */
10727 error (_("Address range too large."));
10728 else if (length == 1)
10729 {
10730 /* This range is simple enough to be handled by
10731 the `hbreak' command. */
10732 hbreak_command (addr_string_start, 1);
10733
10734 do_cleanups (cleanup_bkpt);
10735
10736 return;
10737 }
10738
10739 /* Now set up the breakpoint. */
10740 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10741 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10742 set_breakpoint_count (breakpoint_count + 1);
10743 b->number = breakpoint_count;
10744 b->disposition = disp_donttouch;
f8eba3c6
TT
10745 b->addr_string = xstrdup (addr_string_start);
10746 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10747 b->loc->length = length;
10748
f8eba3c6 10749 do_cleanups (cleanup_bkpt);
f1310107
TJB
10750
10751 mention (b);
8d3788bd 10752 observer_notify_breakpoint_created (b);
44702360 10753 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10754}
10755
4a64f543
MS
10756/* Return non-zero if EXP is verified as constant. Returned zero
10757 means EXP is variable. Also the constant detection may fail for
10758 some constant expressions and in such case still falsely return
10759 zero. */
2e6e3d9c 10760
65d79d4b
SDJ
10761static int
10762watchpoint_exp_is_const (const struct expression *exp)
10763{
10764 int i = exp->nelts;
10765
10766 while (i > 0)
10767 {
10768 int oplenp, argsp;
10769
10770 /* We are only interested in the descriptor of each element. */
10771 operator_length (exp, i, &oplenp, &argsp);
10772 i -= oplenp;
10773
10774 switch (exp->elts[i].opcode)
10775 {
10776 case BINOP_ADD:
10777 case BINOP_SUB:
10778 case BINOP_MUL:
10779 case BINOP_DIV:
10780 case BINOP_REM:
10781 case BINOP_MOD:
10782 case BINOP_LSH:
10783 case BINOP_RSH:
10784 case BINOP_LOGICAL_AND:
10785 case BINOP_LOGICAL_OR:
10786 case BINOP_BITWISE_AND:
10787 case BINOP_BITWISE_IOR:
10788 case BINOP_BITWISE_XOR:
10789 case BINOP_EQUAL:
10790 case BINOP_NOTEQUAL:
10791 case BINOP_LESS:
10792 case BINOP_GTR:
10793 case BINOP_LEQ:
10794 case BINOP_GEQ:
10795 case BINOP_REPEAT:
10796 case BINOP_COMMA:
10797 case BINOP_EXP:
10798 case BINOP_MIN:
10799 case BINOP_MAX:
10800 case BINOP_INTDIV:
10801 case BINOP_CONCAT:
65d79d4b
SDJ
10802 case TERNOP_COND:
10803 case TERNOP_SLICE:
65d79d4b
SDJ
10804
10805 case OP_LONG:
10806 case OP_DOUBLE:
10807 case OP_DECFLOAT:
10808 case OP_LAST:
10809 case OP_COMPLEX:
10810 case OP_STRING:
65d79d4b
SDJ
10811 case OP_ARRAY:
10812 case OP_TYPE:
608b4967
TT
10813 case OP_TYPEOF:
10814 case OP_DECLTYPE:
6e72ca20 10815 case OP_TYPEID:
65d79d4b
SDJ
10816 case OP_NAME:
10817 case OP_OBJC_NSSTRING:
10818
10819 case UNOP_NEG:
10820 case UNOP_LOGICAL_NOT:
10821 case UNOP_COMPLEMENT:
10822 case UNOP_ADDR:
10823 case UNOP_HIGH:
aeaa2474 10824 case UNOP_CAST:
9eaf6705
TT
10825
10826 case UNOP_CAST_TYPE:
10827 case UNOP_REINTERPRET_CAST:
10828 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10829 /* Unary, binary and ternary operators: We have to check
10830 their operands. If they are constant, then so is the
10831 result of that operation. For instance, if A and B are
10832 determined to be constants, then so is "A + B".
10833
10834 UNOP_IND is one exception to the rule above, because the
10835 value of *ADDR is not necessarily a constant, even when
10836 ADDR is. */
65d79d4b
SDJ
10837 break;
10838
10839 case OP_VAR_VALUE:
10840 /* Check whether the associated symbol is a constant.
4a64f543 10841
65d79d4b 10842 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10843 possible that a buggy compiler could mark a variable as
10844 constant even when it is not, and TYPE_CONST would return
10845 true in this case, while SYMBOL_CLASS wouldn't.
10846
10847 We also have to check for function symbols because they
10848 are always constant. */
65d79d4b
SDJ
10849 {
10850 struct symbol *s = exp->elts[i + 2].symbol;
10851
10852 if (SYMBOL_CLASS (s) != LOC_BLOCK
10853 && SYMBOL_CLASS (s) != LOC_CONST
10854 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10855 return 0;
10856 break;
10857 }
10858
10859 /* The default action is to return 0 because we are using
10860 the optimistic approach here: If we don't know something,
10861 then it is not a constant. */
10862 default:
10863 return 0;
10864 }
10865 }
10866
10867 return 1;
10868}
10869
3a5c3e22
PA
10870/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10871
10872static void
10873dtor_watchpoint (struct breakpoint *self)
10874{
10875 struct watchpoint *w = (struct watchpoint *) self;
10876
10877 xfree (w->cond_exp);
10878 xfree (w->exp);
10879 xfree (w->exp_string);
10880 xfree (w->exp_string_reparse);
10881 value_free (w->val);
10882
10883 base_breakpoint_ops.dtor (self);
10884}
10885
348d480f
PA
10886/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10887
10888static void
10889re_set_watchpoint (struct breakpoint *b)
10890{
3a5c3e22
PA
10891 struct watchpoint *w = (struct watchpoint *) b;
10892
348d480f
PA
10893 /* Watchpoint can be either on expression using entirely global
10894 variables, or it can be on local variables.
10895
10896 Watchpoints of the first kind are never auto-deleted, and even
10897 persist across program restarts. Since they can use variables
10898 from shared libraries, we need to reparse expression as libraries
10899 are loaded and unloaded.
10900
10901 Watchpoints on local variables can also change meaning as result
10902 of solib event. For example, if a watchpoint uses both a local
10903 and a global variables in expression, it's a local watchpoint,
10904 but unloading of a shared library will make the expression
10905 invalid. This is not a very common use case, but we still
10906 re-evaluate expression, to avoid surprises to the user.
10907
10908 Note that for local watchpoints, we re-evaluate it only if
10909 watchpoints frame id is still valid. If it's not, it means the
10910 watchpoint is out of scope and will be deleted soon. In fact,
10911 I'm not sure we'll ever be called in this case.
10912
10913 If a local watchpoint's frame id is still valid, then
3a5c3e22 10914 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10915
3a5c3e22
PA
10916 Don't do anything about disabled watchpoints, since they will be
10917 reevaluated again when enabled. */
10918 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10919}
10920
77b06cd7
TJB
10921/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10922
10923static int
10924insert_watchpoint (struct bp_location *bl)
10925{
3a5c3e22
PA
10926 struct watchpoint *w = (struct watchpoint *) bl->owner;
10927 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10928
10929 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10930 w->cond_exp);
77b06cd7
TJB
10931}
10932
10933/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10934
10935static int
10936remove_watchpoint (struct bp_location *bl)
10937{
3a5c3e22
PA
10938 struct watchpoint *w = (struct watchpoint *) bl->owner;
10939 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10940
10941 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10942 w->cond_exp);
e09342b5
TJB
10943}
10944
e09342b5 10945static int
348d480f 10946breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10947 struct address_space *aspace, CORE_ADDR bp_addr,
10948 const struct target_waitstatus *ws)
e09342b5 10949{
348d480f 10950 struct breakpoint *b = bl->owner;
3a5c3e22 10951 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10952
348d480f
PA
10953 /* Continuable hardware watchpoints are treated as non-existent if the
10954 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10955 some data address). Otherwise gdb won't stop on a break instruction
10956 in the code (not from a breakpoint) when a hardware watchpoint has
10957 been defined. Also skip watchpoints which we know did not trigger
10958 (did not match the data address). */
10959 if (is_hardware_watchpoint (b)
3a5c3e22 10960 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10961 return 0;
9c06b0b4 10962
348d480f 10963 return 1;
9c06b0b4
TJB
10964}
10965
348d480f
PA
10966static void
10967check_status_watchpoint (bpstat bs)
9c06b0b4 10968{
348d480f 10969 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10970
348d480f 10971 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10972}
10973
10974/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10975 hardware watchpoints. */
9c06b0b4
TJB
10976
10977static int
348d480f 10978resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10979{
3a5c3e22
PA
10980 struct watchpoint *w = (struct watchpoint *) bl->owner;
10981 int length = w->exact? 1 : bl->length;
348d480f
PA
10982
10983 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10984}
10985
10986/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10987 hardware watchpoints. */
9c06b0b4
TJB
10988
10989static int
348d480f 10990works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10991{
efa80663
PA
10992 /* Read and access watchpoints only work with hardware support. */
10993 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10994}
10995
9c06b0b4 10996static enum print_stop_action
348d480f 10997print_it_watchpoint (bpstat bs)
9c06b0b4 10998{
348d480f
PA
10999 struct cleanup *old_chain;
11000 struct breakpoint *b;
f99d8bf4 11001 struct ui_file *stb;
348d480f 11002 enum print_stop_action result;
3a5c3e22 11003 struct watchpoint *w;
79a45e25 11004 struct ui_out *uiout = current_uiout;
348d480f
PA
11005
11006 gdb_assert (bs->bp_location_at != NULL);
11007
348d480f 11008 b = bs->breakpoint_at;
3a5c3e22 11009 w = (struct watchpoint *) b;
348d480f 11010
f99d8bf4
PA
11011 stb = mem_fileopen ();
11012 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
11013
11014 switch (b->type)
11015 {
348d480f 11016 case bp_watchpoint:
9c06b0b4
TJB
11017 case bp_hardware_watchpoint:
11018 annotate_watchpoint (b->number);
11019 if (ui_out_is_mi_like_p (uiout))
11020 ui_out_field_string
11021 (uiout, "reason",
11022 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
11023 mention (b);
11024 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11025 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 11026 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
11027 ui_out_field_stream (uiout, "old", stb);
11028 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 11029 watchpoint_value_print (w->val, stb);
348d480f
PA
11030 ui_out_field_stream (uiout, "new", stb);
11031 ui_out_text (uiout, "\n");
11032 /* More than one watchpoint may have been triggered. */
11033 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11034 break;
11035
11036 case bp_read_watchpoint:
11037 if (ui_out_is_mi_like_p (uiout))
11038 ui_out_field_string
11039 (uiout, "reason",
11040 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
11041 mention (b);
11042 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11043 ui_out_text (uiout, "\nValue = ");
f99d8bf4 11044 watchpoint_value_print (w->val, stb);
348d480f
PA
11045 ui_out_field_stream (uiout, "value", stb);
11046 ui_out_text (uiout, "\n");
11047 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11048 break;
11049
11050 case bp_access_watchpoint:
348d480f
PA
11051 if (bs->old_val != NULL)
11052 {
11053 annotate_watchpoint (b->number);
11054 if (ui_out_is_mi_like_p (uiout))
11055 ui_out_field_string
11056 (uiout, "reason",
11057 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11058 mention (b);
11059 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11060 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 11061 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
11062 ui_out_field_stream (uiout, "old", stb);
11063 ui_out_text (uiout, "\nNew value = ");
11064 }
11065 else
11066 {
11067 mention (b);
11068 if (ui_out_is_mi_like_p (uiout))
11069 ui_out_field_string
11070 (uiout, "reason",
11071 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11072 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11073 ui_out_text (uiout, "\nValue = ");
11074 }
f99d8bf4 11075 watchpoint_value_print (w->val, stb);
348d480f
PA
11076 ui_out_field_stream (uiout, "new", stb);
11077 ui_out_text (uiout, "\n");
11078 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11079 break;
11080 default:
348d480f 11081 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11082 }
11083
348d480f
PA
11084 do_cleanups (old_chain);
11085 return result;
11086}
11087
11088/* Implement the "print_mention" breakpoint_ops method for hardware
11089 watchpoints. */
11090
11091static void
11092print_mention_watchpoint (struct breakpoint *b)
11093{
11094 struct cleanup *ui_out_chain;
3a5c3e22 11095 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11096 struct ui_out *uiout = current_uiout;
348d480f
PA
11097
11098 switch (b->type)
11099 {
11100 case bp_watchpoint:
11101 ui_out_text (uiout, "Watchpoint ");
11102 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11103 break;
11104 case bp_hardware_watchpoint:
11105 ui_out_text (uiout, "Hardware watchpoint ");
11106 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11107 break;
11108 case bp_read_watchpoint:
11109 ui_out_text (uiout, "Hardware read watchpoint ");
11110 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11111 break;
11112 case bp_access_watchpoint:
11113 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
11114 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11115 break;
11116 default:
11117 internal_error (__FILE__, __LINE__,
11118 _("Invalid hardware watchpoint type."));
11119 }
11120
11121 ui_out_field_int (uiout, "number", b->number);
11122 ui_out_text (uiout, ": ");
3a5c3e22 11123 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
11124 do_cleanups (ui_out_chain);
11125}
11126
11127/* Implement the "print_recreate" breakpoint_ops method for
11128 watchpoints. */
11129
11130static void
11131print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
11132{
3a5c3e22
PA
11133 struct watchpoint *w = (struct watchpoint *) b;
11134
348d480f
PA
11135 switch (b->type)
11136 {
11137 case bp_watchpoint:
11138 case bp_hardware_watchpoint:
11139 fprintf_unfiltered (fp, "watch");
11140 break;
11141 case bp_read_watchpoint:
11142 fprintf_unfiltered (fp, "rwatch");
11143 break;
11144 case bp_access_watchpoint:
11145 fprintf_unfiltered (fp, "awatch");
11146 break;
11147 default:
11148 internal_error (__FILE__, __LINE__,
11149 _("Invalid watchpoint type."));
11150 }
11151
3a5c3e22 11152 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 11153 print_recreate_thread (b, fp);
348d480f
PA
11154}
11155
427cd150
TT
11156/* Implement the "explains_signal" breakpoint_ops method for
11157 watchpoints. */
11158
47591c29 11159static int
427cd150
TT
11160explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
11161{
11162 /* A software watchpoint cannot cause a signal other than
11163 GDB_SIGNAL_TRAP. */
11164 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 11165 return 0;
427cd150 11166
47591c29 11167 return 1;
427cd150
TT
11168}
11169
348d480f
PA
11170/* The breakpoint_ops structure to be used in hardware watchpoints. */
11171
2060206e 11172static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
11173
11174/* Implement the "insert" breakpoint_ops method for
11175 masked hardware watchpoints. */
11176
11177static int
11178insert_masked_watchpoint (struct bp_location *bl)
11179{
3a5c3e22
PA
11180 struct watchpoint *w = (struct watchpoint *) bl->owner;
11181
11182 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11183 bl->watchpoint_type);
11184}
11185
11186/* Implement the "remove" breakpoint_ops method for
11187 masked hardware watchpoints. */
11188
11189static int
11190remove_masked_watchpoint (struct bp_location *bl)
11191{
3a5c3e22
PA
11192 struct watchpoint *w = (struct watchpoint *) bl->owner;
11193
11194 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11195 bl->watchpoint_type);
11196}
11197
11198/* Implement the "resources_needed" breakpoint_ops method for
11199 masked hardware watchpoints. */
11200
11201static int
11202resources_needed_masked_watchpoint (const struct bp_location *bl)
11203{
3a5c3e22
PA
11204 struct watchpoint *w = (struct watchpoint *) bl->owner;
11205
11206 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
11207}
11208
11209/* Implement the "works_in_software_mode" breakpoint_ops method for
11210 masked hardware watchpoints. */
11211
11212static int
11213works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11214{
11215 return 0;
11216}
11217
11218/* Implement the "print_it" breakpoint_ops method for
11219 masked hardware watchpoints. */
11220
11221static enum print_stop_action
11222print_it_masked_watchpoint (bpstat bs)
11223{
11224 struct breakpoint *b = bs->breakpoint_at;
79a45e25 11225 struct ui_out *uiout = current_uiout;
348d480f
PA
11226
11227 /* Masked watchpoints have only one location. */
11228 gdb_assert (b->loc && b->loc->next == NULL);
11229
11230 switch (b->type)
11231 {
11232 case bp_hardware_watchpoint:
11233 annotate_watchpoint (b->number);
11234 if (ui_out_is_mi_like_p (uiout))
11235 ui_out_field_string
11236 (uiout, "reason",
11237 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11238 break;
11239
11240 case bp_read_watchpoint:
11241 if (ui_out_is_mi_like_p (uiout))
11242 ui_out_field_string
11243 (uiout, "reason",
11244 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11245 break;
11246
11247 case bp_access_watchpoint:
11248 if (ui_out_is_mi_like_p (uiout))
11249 ui_out_field_string
11250 (uiout, "reason",
11251 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11252 break;
11253 default:
11254 internal_error (__FILE__, __LINE__,
11255 _("Invalid hardware watchpoint type."));
11256 }
11257
11258 mention (b);
9c06b0b4
TJB
11259 ui_out_text (uiout, _("\n\
11260Check the underlying instruction at PC for the memory\n\
11261address and value which triggered this watchpoint.\n"));
11262 ui_out_text (uiout, "\n");
11263
11264 /* More than one watchpoint may have been triggered. */
11265 return PRINT_UNKNOWN;
11266}
11267
11268/* Implement the "print_one_detail" breakpoint_ops method for
11269 masked hardware watchpoints. */
11270
11271static void
11272print_one_detail_masked_watchpoint (const struct breakpoint *b,
11273 struct ui_out *uiout)
11274{
3a5c3e22
PA
11275 struct watchpoint *w = (struct watchpoint *) b;
11276
9c06b0b4
TJB
11277 /* Masked watchpoints have only one location. */
11278 gdb_assert (b->loc && b->loc->next == NULL);
11279
11280 ui_out_text (uiout, "\tmask ");
3a5c3e22 11281 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11282 ui_out_text (uiout, "\n");
11283}
11284
11285/* Implement the "print_mention" breakpoint_ops method for
11286 masked hardware watchpoints. */
11287
11288static void
11289print_mention_masked_watchpoint (struct breakpoint *b)
11290{
3a5c3e22 11291 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11292 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11293 struct cleanup *ui_out_chain;
11294
11295 switch (b->type)
11296 {
11297 case bp_hardware_watchpoint:
11298 ui_out_text (uiout, "Masked hardware watchpoint ");
11299 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11300 break;
11301 case bp_read_watchpoint:
11302 ui_out_text (uiout, "Masked hardware read watchpoint ");
11303 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11304 break;
11305 case bp_access_watchpoint:
11306 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11307 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11308 break;
11309 default:
11310 internal_error (__FILE__, __LINE__,
11311 _("Invalid hardware watchpoint type."));
11312 }
11313
11314 ui_out_field_int (uiout, "number", b->number);
11315 ui_out_text (uiout, ": ");
3a5c3e22 11316 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11317 do_cleanups (ui_out_chain);
11318}
11319
11320/* Implement the "print_recreate" breakpoint_ops method for
11321 masked hardware watchpoints. */
11322
11323static void
11324print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11325{
3a5c3e22 11326 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11327 char tmp[40];
11328
11329 switch (b->type)
11330 {
11331 case bp_hardware_watchpoint:
11332 fprintf_unfiltered (fp, "watch");
11333 break;
11334 case bp_read_watchpoint:
11335 fprintf_unfiltered (fp, "rwatch");
11336 break;
11337 case bp_access_watchpoint:
11338 fprintf_unfiltered (fp, "awatch");
11339 break;
11340 default:
11341 internal_error (__FILE__, __LINE__,
11342 _("Invalid hardware watchpoint type."));
11343 }
11344
3a5c3e22
PA
11345 sprintf_vma (tmp, w->hw_wp_mask);
11346 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11347 print_recreate_thread (b, fp);
9c06b0b4
TJB
11348}
11349
11350/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11351
2060206e 11352static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11353
11354/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11355
11356static int
11357is_masked_watchpoint (const struct breakpoint *b)
11358{
11359 return b->ops == &masked_watchpoint_breakpoint_ops;
11360}
11361
53a5351d
JM
11362/* accessflag: hw_write: watch write,
11363 hw_read: watch read,
11364 hw_access: watch access (read or write) */
c906108c 11365static void
bbc13ae3 11366watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11367 int just_location, int internal)
c906108c 11368{
a9634178 11369 volatile struct gdb_exception e;
d983da9c 11370 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11371 struct expression *exp;
270140bd 11372 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11373 struct value *val, *mark, *result;
bb9d5f81 11374 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11375 struct frame_info *frame;
bbc13ae3
KS
11376 const char *exp_start = NULL;
11377 const char *exp_end = NULL;
11378 const char *tok, *end_tok;
9c06b0b4 11379 int toklen = -1;
bbc13ae3
KS
11380 const char *cond_start = NULL;
11381 const char *cond_end = NULL;
c906108c 11382 enum bptype bp_type;
37e4754d 11383 int thread = -1;
0cf6dd15 11384 int pc = 0;
9c06b0b4
TJB
11385 /* Flag to indicate whether we are going to use masks for
11386 the hardware watchpoint. */
11387 int use_mask = 0;
11388 CORE_ADDR mask = 0;
3a5c3e22 11389 struct watchpoint *w;
bbc13ae3
KS
11390 char *expression;
11391 struct cleanup *back_to;
c906108c 11392
37e4754d
LM
11393 /* Make sure that we actually have parameters to parse. */
11394 if (arg != NULL && arg[0] != '\0')
11395 {
bbc13ae3
KS
11396 const char *value_start;
11397
11398 exp_end = arg + strlen (arg);
37e4754d 11399
9c06b0b4
TJB
11400 /* Look for "parameter value" pairs at the end
11401 of the arguments string. */
bbc13ae3 11402 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11403 {
11404 /* Skip whitespace at the end of the argument list. */
11405 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11406 tok--;
11407
11408 /* Find the beginning of the last token.
11409 This is the value of the parameter. */
11410 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11411 tok--;
11412 value_start = tok + 1;
11413
11414 /* Skip whitespace. */
11415 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11416 tok--;
11417
11418 end_tok = tok;
11419
11420 /* Find the beginning of the second to last token.
11421 This is the parameter itself. */
11422 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11423 tok--;
11424 tok++;
11425 toklen = end_tok - tok + 1;
11426
11427 if (toklen == 6 && !strncmp (tok, "thread", 6))
11428 {
11429 /* At this point we've found a "thread" token, which means
11430 the user is trying to set a watchpoint that triggers
11431 only in a specific thread. */
11432 char *endp;
37e4754d 11433
9c06b0b4
TJB
11434 if (thread != -1)
11435 error(_("You can specify only one thread."));
37e4754d 11436
9c06b0b4
TJB
11437 /* Extract the thread ID from the next token. */
11438 thread = strtol (value_start, &endp, 0);
37e4754d 11439
9c06b0b4
TJB
11440 /* Check if the user provided a valid numeric value for the
11441 thread ID. */
11442 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11443 error (_("Invalid thread ID specification %s."), value_start);
11444
11445 /* Check if the thread actually exists. */
11446 if (!valid_thread_id (thread))
af4908ba 11447 invalid_thread_id_error (thread);
9c06b0b4
TJB
11448 }
11449 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11450 {
11451 /* We've found a "mask" token, which means the user wants to
11452 create a hardware watchpoint that is going to have the mask
11453 facility. */
11454 struct value *mask_value, *mark;
37e4754d 11455
9c06b0b4
TJB
11456 if (use_mask)
11457 error(_("You can specify only one mask."));
37e4754d 11458
9c06b0b4 11459 use_mask = just_location = 1;
37e4754d 11460
9c06b0b4
TJB
11461 mark = value_mark ();
11462 mask_value = parse_to_comma_and_eval (&value_start);
11463 mask = value_as_address (mask_value);
11464 value_free_to_mark (mark);
11465 }
11466 else
11467 /* We didn't recognize what we found. We should stop here. */
11468 break;
37e4754d 11469
9c06b0b4
TJB
11470 /* Truncate the string and get rid of the "parameter value" pair before
11471 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11472 exp_end = tok;
9c06b0b4 11473 }
37e4754d 11474 }
bbc13ae3
KS
11475 else
11476 exp_end = arg;
37e4754d 11477
bbc13ae3
KS
11478 /* Parse the rest of the arguments. From here on out, everything
11479 is in terms of a newly allocated string instead of the original
11480 ARG. */
c906108c 11481 innermost_block = NULL;
bbc13ae3
KS
11482 expression = savestring (arg, exp_end - arg);
11483 back_to = make_cleanup (xfree, expression);
11484 exp_start = arg = expression;
1bb9788d 11485 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11486 exp_end = arg;
fa8a61dc
TT
11487 /* Remove trailing whitespace from the expression before saving it.
11488 This makes the eventual display of the expression string a bit
11489 prettier. */
11490 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11491 --exp_end;
11492
65d79d4b
SDJ
11493 /* Checking if the expression is not constant. */
11494 if (watchpoint_exp_is_const (exp))
11495 {
11496 int len;
11497
11498 len = exp_end - exp_start;
11499 while (len > 0 && isspace (exp_start[len - 1]))
11500 len--;
11501 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11502 }
11503
c906108c
SS
11504 exp_valid_block = innermost_block;
11505 mark = value_mark ();
3a1115a0 11506 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11507
bb9d5f81
PP
11508 if (val != NULL && just_location)
11509 {
11510 saved_bitpos = value_bitpos (val);
11511 saved_bitsize = value_bitsize (val);
11512 }
11513
06a64a0b
TT
11514 if (just_location)
11515 {
9c06b0b4
TJB
11516 int ret;
11517
06a64a0b 11518 exp_valid_block = NULL;
a1442452 11519 val = value_addr (result);
06a64a0b
TT
11520 release_value (val);
11521 value_free_to_mark (mark);
9c06b0b4
TJB
11522
11523 if (use_mask)
11524 {
11525 ret = target_masked_watch_num_registers (value_as_address (val),
11526 mask);
11527 if (ret == -1)
11528 error (_("This target does not support masked watchpoints."));
11529 else if (ret == -2)
11530 error (_("Invalid mask or memory region."));
11531 }
06a64a0b
TT
11532 }
11533 else if (val != NULL)
fa4727a6 11534 release_value (val);
c906108c 11535
bbc13ae3
KS
11536 tok = skip_spaces_const (arg);
11537 end_tok = skip_to_space_const (tok);
c906108c
SS
11538
11539 toklen = end_tok - tok;
11540 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11541 {
2d134ed3
PA
11542 struct expression *cond;
11543
60e1c644 11544 innermost_block = NULL;
c906108c 11545 tok = cond_start = end_tok + 1;
1bb9788d 11546 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11547
11548 /* The watchpoint expression may not be local, but the condition
11549 may still be. E.g.: `watch global if local > 0'. */
11550 cond_exp_valid_block = innermost_block;
11551
2d134ed3 11552 xfree (cond);
c906108c
SS
11553 cond_end = tok;
11554 }
11555 if (*tok)
8a3fe4f8 11556 error (_("Junk at end of command."));
c906108c 11557
d983da9c 11558 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11559
11560 /* If the expression is "local", then set up a "watchpoint scope"
11561 breakpoint at the point where we've left the scope of the watchpoint
11562 expression. Create the scope breakpoint before the watchpoint, so
11563 that we will encounter it first in bpstat_stop_status. */
60e1c644 11564 if (exp_valid_block && frame)
d983da9c 11565 {
edb3359d
DJ
11566 if (frame_id_p (frame_unwind_caller_id (frame)))
11567 {
11568 scope_breakpoint
a6d9a66e
UW
11569 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11570 frame_unwind_caller_pc (frame),
06edf0c0
PA
11571 bp_watchpoint_scope,
11572 &momentary_breakpoint_ops);
d983da9c 11573
edb3359d 11574 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11575
edb3359d
DJ
11576 /* Automatically delete the breakpoint when it hits. */
11577 scope_breakpoint->disposition = disp_del;
d983da9c 11578
edb3359d
DJ
11579 /* Only break in the proper frame (help with recursion). */
11580 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11581
edb3359d 11582 /* Set the address at which we will stop. */
a6d9a66e
UW
11583 scope_breakpoint->loc->gdbarch
11584 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11585 scope_breakpoint->loc->requested_address
11586 = frame_unwind_caller_pc (frame);
11587 scope_breakpoint->loc->address
a6d9a66e
UW
11588 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11589 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11590 scope_breakpoint->type);
11591 }
d983da9c
DJ
11592 }
11593
e8369a73
AB
11594 /* Now set up the breakpoint. We create all watchpoints as hardware
11595 watchpoints here even if hardware watchpoints are turned off, a call
11596 to update_watchpoint later in this function will cause the type to
11597 drop back to bp_watchpoint (software watchpoint) if required. */
11598
11599 if (accessflag == hw_read)
11600 bp_type = bp_read_watchpoint;
11601 else if (accessflag == hw_access)
11602 bp_type = bp_access_watchpoint;
11603 else
11604 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11605
11606 w = XCNEW (struct watchpoint);
11607 b = &w->base;
348d480f 11608 if (use_mask)
3a5c3e22
PA
11609 init_raw_breakpoint_without_location (b, NULL, bp_type,
11610 &masked_watchpoint_breakpoint_ops);
348d480f 11611 else
3a5c3e22
PA
11612 init_raw_breakpoint_without_location (b, NULL, bp_type,
11613 &watchpoint_breakpoint_ops);
37e4754d 11614 b->thread = thread;
b5de0fa7 11615 b->disposition = disp_donttouch;
348d480f 11616 b->pspace = current_program_space;
3a5c3e22
PA
11617 w->exp = exp;
11618 w->exp_valid_block = exp_valid_block;
11619 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11620 if (just_location)
11621 {
11622 struct type *t = value_type (val);
11623 CORE_ADDR addr = value_as_address (val);
11624 char *name;
11625
11626 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11627 name = type_to_string (t);
11628
3a5c3e22 11629 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11630 core_addr_to_string (addr));
06a64a0b
TT
11631 xfree (name);
11632
3a5c3e22 11633 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11634 (int) (exp_end - exp_start), exp_start);
11635
06a64a0b
TT
11636 /* The above expression is in C. */
11637 b->language = language_c;
11638 }
11639 else
3a5c3e22 11640 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11641
11642 if (use_mask)
11643 {
3a5c3e22 11644 w->hw_wp_mask = mask;
9c06b0b4
TJB
11645 }
11646 else
11647 {
3a5c3e22 11648 w->val = val;
bb9d5f81
PP
11649 w->val_bitpos = saved_bitpos;
11650 w->val_bitsize = saved_bitsize;
3a5c3e22 11651 w->val_valid = 1;
9c06b0b4 11652 }
77b06cd7 11653
c906108c
SS
11654 if (cond_start)
11655 b->cond_string = savestring (cond_start, cond_end - cond_start);
11656 else
11657 b->cond_string = 0;
c5aa993b 11658
c906108c 11659 if (frame)
f6bc2008 11660 {
3a5c3e22
PA
11661 w->watchpoint_frame = get_frame_id (frame);
11662 w->watchpoint_thread = inferior_ptid;
f6bc2008 11663 }
c906108c 11664 else
f6bc2008 11665 {
3a5c3e22
PA
11666 w->watchpoint_frame = null_frame_id;
11667 w->watchpoint_thread = null_ptid;
f6bc2008 11668 }
c906108c 11669
d983da9c 11670 if (scope_breakpoint != NULL)
c906108c 11671 {
d983da9c
DJ
11672 /* The scope breakpoint is related to the watchpoint. We will
11673 need to act on them together. */
11674 b->related_breakpoint = scope_breakpoint;
11675 scope_breakpoint->related_breakpoint = b;
c906108c 11676 }
d983da9c 11677
06a64a0b
TT
11678 if (!just_location)
11679 value_free_to_mark (mark);
2d134ed3 11680
a9634178
TJB
11681 TRY_CATCH (e, RETURN_MASK_ALL)
11682 {
11683 /* Finally update the new watchpoint. This creates the locations
11684 that should be inserted. */
3a5c3e22 11685 update_watchpoint (w, 1);
a9634178
TJB
11686 }
11687 if (e.reason < 0)
11688 {
11689 delete_breakpoint (b);
11690 throw_exception (e);
11691 }
11692
3ea46bff 11693 install_breakpoint (internal, b, 1);
bbc13ae3 11694 do_cleanups (back_to);
c906108c
SS
11695}
11696
e09342b5 11697/* Return count of debug registers needed to watch the given expression.
e09342b5 11698 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11699
c906108c 11700static int
a9634178 11701can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11702{
11703 int found_memory_cnt = 0;
2e70b7b9 11704 struct value *head = v;
c906108c
SS
11705
11706 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11707 if (!can_use_hw_watchpoints)
c906108c 11708 return 0;
c5aa993b 11709
5c44784c
JM
11710 /* Make sure that the value of the expression depends only upon
11711 memory contents, and values computed from them within GDB. If we
11712 find any register references or function calls, we can't use a
11713 hardware watchpoint.
11714
11715 The idea here is that evaluating an expression generates a series
11716 of values, one holding the value of every subexpression. (The
11717 expression a*b+c has five subexpressions: a, b, a*b, c, and
11718 a*b+c.) GDB's values hold almost enough information to establish
11719 the criteria given above --- they identify memory lvalues,
11720 register lvalues, computed values, etcetera. So we can evaluate
11721 the expression, and then scan the chain of values that leaves
11722 behind to decide whether we can detect any possible change to the
11723 expression's final value using only hardware watchpoints.
11724
11725 However, I don't think that the values returned by inferior
11726 function calls are special in any way. So this function may not
11727 notice that an expression involving an inferior function call
11728 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11729 for (; v; v = value_next (v))
c906108c 11730 {
5c44784c 11731 if (VALUE_LVAL (v) == lval_memory)
c906108c 11732 {
8464be76
DJ
11733 if (v != head && value_lazy (v))
11734 /* A lazy memory lvalue in the chain is one that GDB never
11735 needed to fetch; we either just used its address (e.g.,
11736 `a' in `a.b') or we never needed it at all (e.g., `a'
11737 in `a,b'). This doesn't apply to HEAD; if that is
11738 lazy then it was not readable, but watch it anyway. */
5c44784c 11739 ;
53a5351d 11740 else
5c44784c
JM
11741 {
11742 /* Ahh, memory we actually used! Check if we can cover
11743 it with hardware watchpoints. */
df407dfe 11744 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11745
11746 /* We only watch structs and arrays if user asked for it
11747 explicitly, never if they just happen to appear in a
11748 middle of some value chain. */
11749 if (v == head
11750 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11751 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11752 {
42ae5230 11753 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11754 int len;
11755 int num_regs;
11756
a9634178 11757 len = (target_exact_watchpoints
e09342b5
TJB
11758 && is_scalar_type_recursive (vtype))?
11759 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11760
e09342b5
TJB
11761 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11762 if (!num_regs)
2e70b7b9
MS
11763 return 0;
11764 else
e09342b5 11765 found_memory_cnt += num_regs;
2e70b7b9 11766 }
5c44784c 11767 }
c5aa993b 11768 }
5086187c
AC
11769 else if (VALUE_LVAL (v) != not_lval
11770 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11771 return 0; /* These are values from the history (e.g., $1). */
5086187c 11772 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11773 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11774 }
11775
11776 /* The expression itself looks suitable for using a hardware
11777 watchpoint, but give the target machine a chance to reject it. */
11778 return found_memory_cnt;
11779}
11780
8b93c638 11781void
84f4c1fe 11782watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11783{
84f4c1fe 11784 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11785}
11786
06a64a0b
TT
11787/* A helper function that looks for the "-location" argument and then
11788 calls watch_command_1. */
11789
11790static void
11791watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11792{
11793 int just_location = 0;
11794
11795 if (arg
11796 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11797 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11798 {
e9cafbcc 11799 arg = skip_spaces (arg);
06a64a0b
TT
11800 just_location = 1;
11801 }
11802
84f4c1fe 11803 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11804}
8926118c 11805
c5aa993b 11806static void
fba45db2 11807watch_command (char *arg, int from_tty)
c906108c 11808{
06a64a0b 11809 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11810}
11811
8b93c638 11812void
84f4c1fe 11813rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11814{
84f4c1fe 11815 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11816}
8926118c 11817
c5aa993b 11818static void
fba45db2 11819rwatch_command (char *arg, int from_tty)
c906108c 11820{
06a64a0b 11821 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11822}
11823
8b93c638 11824void
84f4c1fe 11825awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11826{
84f4c1fe 11827 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11828}
8926118c 11829
c5aa993b 11830static void
fba45db2 11831awatch_command (char *arg, int from_tty)
c906108c 11832{
06a64a0b 11833 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11834}
c906108c 11835\f
c5aa993b 11836
43ff13b4 11837/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11838 because it uses the mechanisms of breakpoints. */
11839
bfec99b2
PA
11840struct until_break_command_continuation_args
11841{
11842 struct breakpoint *breakpoint;
11843 struct breakpoint *breakpoint2;
186c406b 11844 int thread_num;
bfec99b2
PA
11845};
11846
43ff13b4 11847/* This function is called by fetch_inferior_event via the
4a64f543 11848 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11849 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11850 command. */
c2c6d25f 11851static void
fa4cd53f 11852until_break_command_continuation (void *arg, int err)
43ff13b4 11853{
bfec99b2
PA
11854 struct until_break_command_continuation_args *a = arg;
11855
11856 delete_breakpoint (a->breakpoint);
11857 if (a->breakpoint2)
11858 delete_breakpoint (a->breakpoint2);
186c406b 11859 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11860}
11861
c906108c 11862void
ae66c1fc 11863until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11864{
11865 struct symtabs_and_lines sals;
11866 struct symtab_and_line sal;
8556afb4
PA
11867 struct frame_info *frame;
11868 struct gdbarch *frame_gdbarch;
11869 struct frame_id stack_frame_id;
11870 struct frame_id caller_frame_id;
c906108c 11871 struct breakpoint *breakpoint;
f107f563 11872 struct breakpoint *breakpoint2 = NULL;
c906108c 11873 struct cleanup *old_chain;
186c406b
TT
11874 int thread;
11875 struct thread_info *tp;
c906108c 11876
70509625 11877 clear_proceed_status (0);
c906108c
SS
11878
11879 /* Set a breakpoint where the user wants it and at return from
4a64f543 11880 this function. */
c5aa993b 11881
1bfeeb0f 11882 if (last_displayed_sal_is_valid ())
f8eba3c6 11883 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11884 get_last_displayed_symtab (),
f8eba3c6 11885 get_last_displayed_line ());
c906108c 11886 else
f8eba3c6
TT
11887 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11888 (struct symtab *) NULL, 0);
c5aa993b 11889
c906108c 11890 if (sals.nelts != 1)
8a3fe4f8 11891 error (_("Couldn't get information on specified line."));
c5aa993b 11892
c906108c 11893 sal = sals.sals[0];
4a64f543 11894 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11895
c906108c 11896 if (*arg)
8a3fe4f8 11897 error (_("Junk at end of arguments."));
c5aa993b 11898
c906108c 11899 resolve_sal_pc (&sal);
c5aa993b 11900
186c406b
TT
11901 tp = inferior_thread ();
11902 thread = tp->num;
11903
883bc8d1
PA
11904 old_chain = make_cleanup (null_cleanup, NULL);
11905
8556afb4
PA
11906 /* Note linespec handling above invalidates the frame chain.
11907 Installing a breakpoint also invalidates the frame chain (as it
11908 may need to switch threads), so do any frame handling before
11909 that. */
11910
11911 frame = get_selected_frame (NULL);
11912 frame_gdbarch = get_frame_arch (frame);
11913 stack_frame_id = get_stack_frame_id (frame);
11914 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11915
ae66c1fc
EZ
11916 /* Keep within the current frame, or in frames called by the current
11917 one. */
edb3359d 11918
883bc8d1 11919 if (frame_id_p (caller_frame_id))
c906108c 11920 {
883bc8d1
PA
11921 struct symtab_and_line sal2;
11922
11923 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11924 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11925 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11926 sal2,
11927 caller_frame_id,
f107f563
VP
11928 bp_until);
11929 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11930
883bc8d1 11931 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11932 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11933 }
c5aa993b 11934
c70a6932
JK
11935 /* set_momentary_breakpoint could invalidate FRAME. */
11936 frame = NULL;
11937
883bc8d1
PA
11938 if (anywhere)
11939 /* If the user told us to continue until a specified location,
11940 we don't specify a frame at which we need to stop. */
11941 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11942 null_frame_id, bp_until);
11943 else
11944 /* Otherwise, specify the selected frame, because we want to stop
11945 only at the very same frame. */
11946 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11947 stack_frame_id, bp_until);
11948 make_cleanup_delete_breakpoint (breakpoint);
11949
a493e3e2 11950 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11951
4a64f543
MS
11952 /* If we are running asynchronously, and proceed call above has
11953 actually managed to start the target, arrange for breakpoints to
11954 be deleted when the target stops. Otherwise, we're already
11955 stopped and delete breakpoints via cleanup chain. */
f107f563 11956
8ea051c5 11957 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11958 {
bfec99b2
PA
11959 struct until_break_command_continuation_args *args;
11960 args = xmalloc (sizeof (*args));
f107f563 11961
bfec99b2
PA
11962 args->breakpoint = breakpoint;
11963 args->breakpoint2 = breakpoint2;
186c406b 11964 args->thread_num = thread;
f107f563
VP
11965
11966 discard_cleanups (old_chain);
95e54da7
PA
11967 add_continuation (inferior_thread (),
11968 until_break_command_continuation, args,
604ead4a 11969 xfree);
f107f563
VP
11970 }
11971 else
c5aa993b 11972 do_cleanups (old_chain);
c906108c 11973}
ae66c1fc 11974
c906108c
SS
11975/* This function attempts to parse an optional "if <cond>" clause
11976 from the arg string. If one is not found, it returns NULL.
c5aa993b 11977
c906108c
SS
11978 Else, it returns a pointer to the condition string. (It does not
11979 attempt to evaluate the string against a particular block.) And,
11980 it updates arg to point to the first character following the parsed
4a64f543 11981 if clause in the arg string. */
53a5351d 11982
916703c0 11983char *
fba45db2 11984ep_parse_optional_if_clause (char **arg)
c906108c 11985{
c5aa993b
JM
11986 char *cond_string;
11987
11988 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11989 return NULL;
c5aa993b 11990
4a64f543 11991 /* Skip the "if" keyword. */
c906108c 11992 (*arg) += 2;
c5aa993b 11993
c906108c 11994 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11995 condition string. */
e9cafbcc 11996 *arg = skip_spaces (*arg);
c906108c 11997 cond_string = *arg;
c5aa993b 11998
4a64f543
MS
11999 /* Assume that the condition occupies the remainder of the arg
12000 string. */
c906108c 12001 (*arg) += strlen (cond_string);
c5aa993b 12002
c906108c
SS
12003 return cond_string;
12004}
c5aa993b 12005
c906108c
SS
12006/* Commands to deal with catching events, such as signals, exceptions,
12007 process start/exit, etc. */
c5aa993b
JM
12008
12009typedef enum
12010{
44feb3ce
TT
12011 catch_fork_temporary, catch_vfork_temporary,
12012 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
12013}
12014catch_fork_kind;
12015
c906108c 12016static void
cc59ec59
MS
12017catch_fork_command_1 (char *arg, int from_tty,
12018 struct cmd_list_element *command)
c906108c 12019{
a6d9a66e 12020 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 12021 char *cond_string = NULL;
44feb3ce
TT
12022 catch_fork_kind fork_kind;
12023 int tempflag;
12024
12025 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
12026 tempflag = (fork_kind == catch_fork_temporary
12027 || fork_kind == catch_vfork_temporary);
c5aa993b 12028
44feb3ce
TT
12029 if (!arg)
12030 arg = "";
e9cafbcc 12031 arg = skip_spaces (arg);
c5aa993b 12032
c906108c 12033 /* The allowed syntax is:
c5aa993b
JM
12034 catch [v]fork
12035 catch [v]fork if <cond>
12036
4a64f543 12037 First, check if there's an if clause. */
c906108c 12038 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 12039
c906108c 12040 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12041 error (_("Junk at end of arguments."));
c5aa993b 12042
c906108c 12043 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 12044 and enable reporting of such events. */
c5aa993b
JM
12045 switch (fork_kind)
12046 {
44feb3ce
TT
12047 case catch_fork_temporary:
12048 case catch_fork_permanent:
a6d9a66e 12049 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12050 &catch_fork_breakpoint_ops);
c906108c 12051 break;
44feb3ce
TT
12052 case catch_vfork_temporary:
12053 case catch_vfork_permanent:
a6d9a66e 12054 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12055 &catch_vfork_breakpoint_ops);
c906108c 12056 break;
c5aa993b 12057 default:
8a3fe4f8 12058 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 12059 break;
c5aa993b 12060 }
c906108c
SS
12061}
12062
12063static void
cc59ec59
MS
12064catch_exec_command_1 (char *arg, int from_tty,
12065 struct cmd_list_element *command)
c906108c 12066{
b4d90040 12067 struct exec_catchpoint *c;
a6d9a66e 12068 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 12069 int tempflag;
c5aa993b 12070 char *cond_string = NULL;
c906108c 12071
44feb3ce
TT
12072 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12073
12074 if (!arg)
12075 arg = "";
e9cafbcc 12076 arg = skip_spaces (arg);
c906108c
SS
12077
12078 /* The allowed syntax is:
c5aa993b
JM
12079 catch exec
12080 catch exec if <cond>
c906108c 12081
4a64f543 12082 First, check if there's an if clause. */
c906108c
SS
12083 cond_string = ep_parse_optional_if_clause (&arg);
12084
12085 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12086 error (_("Junk at end of arguments."));
c906108c 12087
b4d90040
PA
12088 c = XNEW (struct exec_catchpoint);
12089 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
12090 &catch_exec_breakpoint_ops);
12091 c->exec_pathname = NULL;
12092
3ea46bff 12093 install_breakpoint (0, &c->base, 1);
c906108c 12094}
c5aa993b 12095
9ac4176b 12096void
28010a5d
PA
12097init_ada_exception_breakpoint (struct breakpoint *b,
12098 struct gdbarch *gdbarch,
12099 struct symtab_and_line sal,
12100 char *addr_string,
c0a91b2b 12101 const struct breakpoint_ops *ops,
28010a5d 12102 int tempflag,
349774ef 12103 int enabled,
28010a5d 12104 int from_tty)
f7f9143b 12105{
f7f9143b
JB
12106 if (from_tty)
12107 {
5af949e3
UW
12108 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12109 if (!loc_gdbarch)
12110 loc_gdbarch = gdbarch;
12111
6c95b8df
PA
12112 describe_other_breakpoints (loc_gdbarch,
12113 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
12114 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12115 version for exception catchpoints, because two catchpoints
12116 used for different exception names will use the same address.
12117 In this case, a "breakpoint ... also set at..." warning is
4a64f543 12118 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 12119 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
12120 the user what type of catchpoint it is. The above is good
12121 enough for now, though. */
12122 }
12123
28010a5d 12124 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 12125
349774ef 12126 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 12127 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
12128 b->addr_string = addr_string;
12129 b->language = language_ada;
f7f9143b
JB
12130}
12131
a96d9b2e
SDJ
12132/* Splits the argument using space as delimiter. Returns an xmalloc'd
12133 filter list, or NULL if no filtering is required. */
12134static VEC(int) *
12135catch_syscall_split_args (char *arg)
12136{
12137 VEC(int) *result = NULL;
29d0bb3d 12138 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
12139
12140 while (*arg != '\0')
12141 {
12142 int i, syscall_number;
12143 char *endptr;
12144 char cur_name[128];
12145 struct syscall s;
12146
12147 /* Skip whitespace. */
529480d0 12148 arg = skip_spaces (arg);
a96d9b2e
SDJ
12149
12150 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
12151 cur_name[i] = arg[i];
12152 cur_name[i] = '\0';
12153 arg += i;
12154
12155 /* Check if the user provided a syscall name or a number. */
12156 syscall_number = (int) strtol (cur_name, &endptr, 0);
12157 if (*endptr == '\0')
bccd0dd2 12158 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
12159 else
12160 {
12161 /* We have a name. Let's check if it's valid and convert it
12162 to a number. */
12163 get_syscall_by_name (cur_name, &s);
12164
12165 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
12166 /* Here we have to issue an error instead of a warning,
12167 because GDB cannot do anything useful if there's no
12168 syscall number to be caught. */
a96d9b2e
SDJ
12169 error (_("Unknown syscall name '%s'."), cur_name);
12170 }
12171
12172 /* Ok, it's valid. */
12173 VEC_safe_push (int, result, s.number);
12174 }
12175
12176 discard_cleanups (cleanup);
12177 return result;
12178}
12179
12180/* Implement the "catch syscall" command. */
12181
12182static void
cc59ec59
MS
12183catch_syscall_command_1 (char *arg, int from_tty,
12184 struct cmd_list_element *command)
a96d9b2e
SDJ
12185{
12186 int tempflag;
12187 VEC(int) *filter;
12188 struct syscall s;
12189 struct gdbarch *gdbarch = get_current_arch ();
12190
12191 /* Checking if the feature if supported. */
12192 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
12193 error (_("The feature 'catch syscall' is not supported on \
ea666128 12194this architecture yet."));
a96d9b2e
SDJ
12195
12196 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12197
e9cafbcc 12198 arg = skip_spaces (arg);
a96d9b2e
SDJ
12199
12200 /* We need to do this first "dummy" translation in order
12201 to get the syscall XML file loaded or, most important,
12202 to display a warning to the user if there's no XML file
12203 for his/her architecture. */
12204 get_syscall_by_number (0, &s);
12205
12206 /* The allowed syntax is:
12207 catch syscall
12208 catch syscall <name | number> [<name | number> ... <name | number>]
12209
12210 Let's check if there's a syscall name. */
12211
12212 if (arg != NULL)
12213 filter = catch_syscall_split_args (arg);
12214 else
12215 filter = NULL;
12216
12217 create_syscall_event_catchpoint (tempflag, filter,
12218 &catch_syscall_breakpoint_ops);
12219}
12220
c906108c 12221static void
fba45db2 12222catch_command (char *arg, int from_tty)
c906108c 12223{
44feb3ce 12224 error (_("Catch requires an event name."));
c906108c
SS
12225}
12226\f
12227
12228static void
fba45db2 12229tcatch_command (char *arg, int from_tty)
c906108c 12230{
44feb3ce 12231 error (_("Catch requires an event name."));
c906108c
SS
12232}
12233
8a2c437b
TT
12234/* A qsort comparison function that sorts breakpoints in order. */
12235
12236static int
12237compare_breakpoints (const void *a, const void *b)
12238{
12239 const breakpoint_p *ba = a;
12240 uintptr_t ua = (uintptr_t) *ba;
12241 const breakpoint_p *bb = b;
12242 uintptr_t ub = (uintptr_t) *bb;
12243
12244 if ((*ba)->number < (*bb)->number)
12245 return -1;
12246 else if ((*ba)->number > (*bb)->number)
12247 return 1;
12248
12249 /* Now sort by address, in case we see, e..g, two breakpoints with
12250 the number 0. */
12251 if (ua < ub)
12252 return -1;
94b0e70d 12253 return ua > ub ? 1 : 0;
8a2c437b
TT
12254}
12255
80f8a6eb 12256/* Delete breakpoints by address or line. */
c906108c
SS
12257
12258static void
fba45db2 12259clear_command (char *arg, int from_tty)
c906108c 12260{
8a2c437b 12261 struct breakpoint *b, *prev;
d6e956e5
VP
12262 VEC(breakpoint_p) *found = 0;
12263 int ix;
c906108c
SS
12264 int default_match;
12265 struct symtabs_and_lines sals;
12266 struct symtab_and_line sal;
c906108c 12267 int i;
8a2c437b 12268 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12269
12270 if (arg)
12271 {
39cf75f7
DE
12272 sals = decode_line_with_current_source (arg,
12273 (DECODE_LINE_FUNFIRSTLINE
12274 | DECODE_LINE_LIST_MODE));
cf4ded82 12275 make_cleanup (xfree, sals.sals);
c906108c
SS
12276 default_match = 0;
12277 }
12278 else
12279 {
c5aa993b 12280 sals.sals = (struct symtab_and_line *)
c906108c 12281 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 12282 make_cleanup (xfree, sals.sals);
4a64f543 12283 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12284
12285 /* Set sal's line, symtab, pc, and pspace to the values
12286 corresponding to the last call to print_frame_info. If the
12287 codepoint is not valid, this will set all the fields to 0. */
12288 get_last_displayed_sal (&sal);
c906108c 12289 if (sal.symtab == 0)
8a3fe4f8 12290 error (_("No source file specified."));
c906108c
SS
12291
12292 sals.sals[0] = sal;
12293 sals.nelts = 1;
12294
12295 default_match = 1;
12296 }
12297
4a64f543
MS
12298 /* We don't call resolve_sal_pc here. That's not as bad as it
12299 seems, because all existing breakpoints typically have both
12300 file/line and pc set. So, if clear is given file/line, we can
12301 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12302
12303 We only support clearing given the address explicitly
12304 present in breakpoint table. Say, we've set breakpoint
4a64f543 12305 at file:line. There were several PC values for that file:line,
ed0616c6 12306 due to optimization, all in one block.
4a64f543
MS
12307
12308 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12309 PC corresponding to the same file:line, the breakpoint won't
12310 be cleared. We probably can still clear the breakpoint, but
12311 since the other PC value is never presented to user, user
12312 can only find it by guessing, and it does not seem important
12313 to support that. */
12314
4a64f543
MS
12315 /* For each line spec given, delete bps which correspond to it. Do
12316 it in two passes, solely to preserve the current behavior that
12317 from_tty is forced true if we delete more than one
12318 breakpoint. */
c906108c 12319
80f8a6eb 12320 found = NULL;
8a2c437b 12321 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12322 for (i = 0; i < sals.nelts; i++)
12323 {
05cba821
JK
12324 const char *sal_fullname;
12325
c906108c 12326 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12327 If line given (pc == 0), clear all bpts on specified line.
12328 If defaulting, clear all bpts on default line
c906108c 12329 or at default pc.
c5aa993b
JM
12330
12331 defaulting sal.pc != 0 tests to do
12332
12333 0 1 pc
12334 1 1 pc _and_ line
12335 0 0 line
12336 1 0 <can't happen> */
c906108c
SS
12337
12338 sal = sals.sals[i];
05cba821
JK
12339 sal_fullname = (sal.symtab == NULL
12340 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12341
4a64f543 12342 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12343 ALL_BREAKPOINTS (b)
c5aa993b 12344 {
0d381245 12345 int match = 0;
4a64f543 12346 /* Are we going to delete b? */
cc60f2e3 12347 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12348 {
12349 struct bp_location *loc = b->loc;
12350 for (; loc; loc = loc->next)
12351 {
f8eba3c6
TT
12352 /* If the user specified file:line, don't allow a PC
12353 match. This matches historical gdb behavior. */
12354 int pc_match = (!sal.explicit_line
12355 && sal.pc
12356 && (loc->pspace == sal.pspace)
12357 && (loc->address == sal.pc)
12358 && (!section_is_overlay (loc->section)
12359 || loc->section == sal.section));
4aac40c8
TT
12360 int line_match = 0;
12361
12362 if ((default_match || sal.explicit_line)
2f202fde 12363 && loc->symtab != NULL
05cba821 12364 && sal_fullname != NULL
4aac40c8 12365 && sal.pspace == loc->pspace
05cba821
JK
12366 && loc->line_number == sal.line
12367 && filename_cmp (symtab_to_fullname (loc->symtab),
12368 sal_fullname) == 0)
12369 line_match = 1;
4aac40c8 12370
0d381245
VP
12371 if (pc_match || line_match)
12372 {
12373 match = 1;
12374 break;
12375 }
12376 }
12377 }
12378
12379 if (match)
d6e956e5 12380 VEC_safe_push(breakpoint_p, found, b);
c906108c 12381 }
80f8a6eb 12382 }
8a2c437b 12383
80f8a6eb 12384 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12385 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12386 {
12387 if (arg)
8a3fe4f8 12388 error (_("No breakpoint at %s."), arg);
80f8a6eb 12389 else
8a3fe4f8 12390 error (_("No breakpoint at this line."));
80f8a6eb 12391 }
c906108c 12392
8a2c437b
TT
12393 /* Remove duplicates from the vec. */
12394 qsort (VEC_address (breakpoint_p, found),
12395 VEC_length (breakpoint_p, found),
12396 sizeof (breakpoint_p),
12397 compare_breakpoints);
12398 prev = VEC_index (breakpoint_p, found, 0);
12399 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12400 {
12401 if (b == prev)
12402 {
12403 VEC_ordered_remove (breakpoint_p, found, ix);
12404 --ix;
12405 }
12406 }
12407
d6e956e5 12408 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12409 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12410 if (from_tty)
a3f17187 12411 {
d6e956e5 12412 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12413 printf_unfiltered (_("Deleted breakpoint "));
12414 else
12415 printf_unfiltered (_("Deleted breakpoints "));
12416 }
d6e956e5
VP
12417
12418 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12419 {
c5aa993b 12420 if (from_tty)
d6e956e5
VP
12421 printf_unfiltered ("%d ", b->number);
12422 delete_breakpoint (b);
c906108c 12423 }
80f8a6eb
MS
12424 if (from_tty)
12425 putchar_unfiltered ('\n');
8a2c437b
TT
12426
12427 do_cleanups (cleanups);
c906108c
SS
12428}
12429\f
12430/* Delete breakpoint in BS if they are `delete' breakpoints and
12431 all breakpoints that are marked for deletion, whether hit or not.
12432 This is called after any breakpoint is hit, or after errors. */
12433
12434void
fba45db2 12435breakpoint_auto_delete (bpstat bs)
c906108c 12436{
35df4500 12437 struct breakpoint *b, *b_tmp;
c906108c
SS
12438
12439 for (; bs; bs = bs->next)
f431efe5
PA
12440 if (bs->breakpoint_at
12441 && bs->breakpoint_at->disposition == disp_del
c906108c 12442 && bs->stop)
f431efe5 12443 delete_breakpoint (bs->breakpoint_at);
c906108c 12444
35df4500 12445 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12446 {
b5de0fa7 12447 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12448 delete_breakpoint (b);
12449 }
c906108c
SS
12450}
12451
4a64f543
MS
12452/* A comparison function for bp_location AP and BP being interfaced to
12453 qsort. Sort elements primarily by their ADDRESS (no matter what
12454 does breakpoint_address_is_meaningful say for its OWNER),
12455 secondarily by ordering first bp_permanent OWNERed elements and
12456 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12457 qsort being an unstable algorithm. */
876fa593
JK
12458
12459static int
494cfb0f 12460bp_location_compare (const void *ap, const void *bp)
876fa593 12461{
494cfb0f
JK
12462 struct bp_location *a = *(void **) ap;
12463 struct bp_location *b = *(void **) bp;
2bdf28a0 12464 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
12465 int a_perm = a->owner->enable_state == bp_permanent;
12466 int b_perm = b->owner->enable_state == bp_permanent;
12467
12468 if (a->address != b->address)
12469 return (a->address > b->address) - (a->address < b->address);
12470
dea2aa5f
LM
12471 /* Sort locations at the same address by their pspace number, keeping
12472 locations of the same inferior (in a multi-inferior environment)
12473 grouped. */
12474
12475 if (a->pspace->num != b->pspace->num)
12476 return ((a->pspace->num > b->pspace->num)
12477 - (a->pspace->num < b->pspace->num));
12478
876fa593
JK
12479 /* Sort permanent breakpoints first. */
12480 if (a_perm != b_perm)
12481 return (a_perm < b_perm) - (a_perm > b_perm);
12482
c56a97f9
JK
12483 /* Make the internal GDB representation stable across GDB runs
12484 where A and B memory inside GDB can differ. Breakpoint locations of
12485 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12486
12487 if (a->owner->number != b->owner->number)
c56a97f9
JK
12488 return ((a->owner->number > b->owner->number)
12489 - (a->owner->number < b->owner->number));
876fa593
JK
12490
12491 return (a > b) - (a < b);
12492}
12493
876fa593 12494/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12495 bp_location_shadow_len_after_address_max according to the current
12496 content of the bp_location array. */
f7545552
TT
12497
12498static void
876fa593 12499bp_location_target_extensions_update (void)
f7545552 12500{
876fa593
JK
12501 struct bp_location *bl, **blp_tmp;
12502
12503 bp_location_placed_address_before_address_max = 0;
12504 bp_location_shadow_len_after_address_max = 0;
12505
12506 ALL_BP_LOCATIONS (bl, blp_tmp)
12507 {
12508 CORE_ADDR start, end, addr;
12509
12510 if (!bp_location_has_shadow (bl))
12511 continue;
12512
12513 start = bl->target_info.placed_address;
12514 end = start + bl->target_info.shadow_len;
12515
12516 gdb_assert (bl->address >= start);
12517 addr = bl->address - start;
12518 if (addr > bp_location_placed_address_before_address_max)
12519 bp_location_placed_address_before_address_max = addr;
12520
12521 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12522
12523 gdb_assert (bl->address < end);
12524 addr = end - bl->address;
12525 if (addr > bp_location_shadow_len_after_address_max)
12526 bp_location_shadow_len_after_address_max = addr;
12527 }
f7545552
TT
12528}
12529
1e4d1764
YQ
12530/* Download tracepoint locations if they haven't been. */
12531
12532static void
12533download_tracepoint_locations (void)
12534{
7ed2c994 12535 struct breakpoint *b;
1e4d1764
YQ
12536 struct cleanup *old_chain;
12537
12538 if (!target_can_download_tracepoint ())
12539 return;
12540
12541 old_chain = save_current_space_and_thread ();
12542
7ed2c994 12543 ALL_TRACEPOINTS (b)
1e4d1764 12544 {
7ed2c994 12545 struct bp_location *bl;
1e4d1764 12546 struct tracepoint *t;
f2a8bc8a 12547 int bp_location_downloaded = 0;
1e4d1764 12548
7ed2c994 12549 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12550 ? !may_insert_fast_tracepoints
12551 : !may_insert_tracepoints))
12552 continue;
12553
7ed2c994
YQ
12554 for (bl = b->loc; bl; bl = bl->next)
12555 {
12556 /* In tracepoint, locations are _never_ duplicated, so
12557 should_be_inserted is equivalent to
12558 unduplicated_should_be_inserted. */
12559 if (!should_be_inserted (bl) || bl->inserted)
12560 continue;
1e4d1764 12561
7ed2c994 12562 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12563
7ed2c994 12564 target_download_tracepoint (bl);
1e4d1764 12565
7ed2c994 12566 bl->inserted = 1;
f2a8bc8a 12567 bp_location_downloaded = 1;
7ed2c994
YQ
12568 }
12569 t = (struct tracepoint *) b;
12570 t->number_on_target = b->number;
f2a8bc8a
YQ
12571 if (bp_location_downloaded)
12572 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12573 }
12574
12575 do_cleanups (old_chain);
12576}
12577
934709f0
PW
12578/* Swap the insertion/duplication state between two locations. */
12579
12580static void
12581swap_insertion (struct bp_location *left, struct bp_location *right)
12582{
12583 const int left_inserted = left->inserted;
12584 const int left_duplicate = left->duplicate;
b775012e 12585 const int left_needs_update = left->needs_update;
934709f0
PW
12586 const struct bp_target_info left_target_info = left->target_info;
12587
1e4d1764
YQ
12588 /* Locations of tracepoints can never be duplicated. */
12589 if (is_tracepoint (left->owner))
12590 gdb_assert (!left->duplicate);
12591 if (is_tracepoint (right->owner))
12592 gdb_assert (!right->duplicate);
12593
934709f0
PW
12594 left->inserted = right->inserted;
12595 left->duplicate = right->duplicate;
b775012e 12596 left->needs_update = right->needs_update;
934709f0
PW
12597 left->target_info = right->target_info;
12598 right->inserted = left_inserted;
12599 right->duplicate = left_duplicate;
b775012e 12600 right->needs_update = left_needs_update;
934709f0
PW
12601 right->target_info = left_target_info;
12602}
12603
b775012e
LM
12604/* Force the re-insertion of the locations at ADDRESS. This is called
12605 once a new/deleted/modified duplicate location is found and we are evaluating
12606 conditions on the target's side. Such conditions need to be updated on
12607 the target. */
12608
12609static void
12610force_breakpoint_reinsertion (struct bp_location *bl)
12611{
12612 struct bp_location **locp = NULL, **loc2p;
12613 struct bp_location *loc;
12614 CORE_ADDR address = 0;
12615 int pspace_num;
12616
12617 address = bl->address;
12618 pspace_num = bl->pspace->num;
12619
12620 /* This is only meaningful if the target is
12621 evaluating conditions and if the user has
12622 opted for condition evaluation on the target's
12623 side. */
12624 if (gdb_evaluates_breakpoint_condition_p ()
12625 || !target_supports_evaluation_of_breakpoint_conditions ())
12626 return;
12627
12628 /* Flag all breakpoint locations with this address and
12629 the same program space as the location
12630 as "its condition has changed". We need to
12631 update the conditions on the target's side. */
12632 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12633 {
12634 loc = *loc2p;
12635
12636 if (!is_breakpoint (loc->owner)
12637 || pspace_num != loc->pspace->num)
12638 continue;
12639
12640 /* Flag the location appropriately. We use a different state to
12641 let everyone know that we already updated the set of locations
12642 with addr bl->address and program space bl->pspace. This is so
12643 we don't have to keep calling these functions just to mark locations
12644 that have already been marked. */
12645 loc->condition_changed = condition_updated;
12646
12647 /* Free the agent expression bytecode as well. We will compute
12648 it later on. */
12649 if (loc->cond_bytecode)
12650 {
12651 free_agent_expr (loc->cond_bytecode);
12652 loc->cond_bytecode = NULL;
12653 }
12654 }
12655}
44702360
PA
12656/* Called whether new breakpoints are created, or existing breakpoints
12657 deleted, to update the global location list and recompute which
12658 locations are duplicate of which.
b775012e 12659
04086b45
PA
12660 The INSERT_MODE flag determines whether locations may not, may, or
12661 shall be inserted now. See 'enum ugll_insert_mode' for more
12662 info. */
b60e7edf 12663
0d381245 12664static void
44702360 12665update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12666{
74960c60 12667 struct breakpoint *b;
876fa593 12668 struct bp_location **locp, *loc;
f7545552 12669 struct cleanup *cleanups;
b775012e
LM
12670 /* Last breakpoint location address that was marked for update. */
12671 CORE_ADDR last_addr = 0;
12672 /* Last breakpoint location program space that was marked for update. */
12673 int last_pspace_num = -1;
f7545552 12674
2d134ed3
PA
12675 /* Used in the duplicates detection below. When iterating over all
12676 bp_locations, points to the first bp_location of a given address.
12677 Breakpoints and watchpoints of different types are never
12678 duplicates of each other. Keep one pointer for each type of
12679 breakpoint/watchpoint, so we only need to loop over all locations
12680 once. */
12681 struct bp_location *bp_loc_first; /* breakpoint */
12682 struct bp_location *wp_loc_first; /* hardware watchpoint */
12683 struct bp_location *awp_loc_first; /* access watchpoint */
12684 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12685
4a64f543
MS
12686 /* Saved former bp_location array which we compare against the newly
12687 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12688 struct bp_location **old_location, **old_locp;
12689 unsigned old_location_count;
12690
12691 old_location = bp_location;
12692 old_location_count = bp_location_count;
12693 bp_location = NULL;
12694 bp_location_count = 0;
12695 cleanups = make_cleanup (xfree, old_location);
0d381245 12696
74960c60 12697 ALL_BREAKPOINTS (b)
876fa593
JK
12698 for (loc = b->loc; loc; loc = loc->next)
12699 bp_location_count++;
12700
12701 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12702 locp = bp_location;
12703 ALL_BREAKPOINTS (b)
12704 for (loc = b->loc; loc; loc = loc->next)
12705 *locp++ = loc;
12706 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12707 bp_location_compare);
876fa593
JK
12708
12709 bp_location_target_extensions_update ();
74960c60 12710
4a64f543
MS
12711 /* Identify bp_location instances that are no longer present in the
12712 new list, and therefore should be freed. Note that it's not
12713 necessary that those locations should be removed from inferior --
12714 if there's another location at the same address (previously
12715 marked as duplicate), we don't need to remove/insert the
12716 location.
876fa593 12717
4a64f543
MS
12718 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12719 and former bp_location array state respectively. */
876fa593
JK
12720
12721 locp = bp_location;
12722 for (old_locp = old_location; old_locp < old_location + old_location_count;
12723 old_locp++)
74960c60 12724 {
876fa593 12725 struct bp_location *old_loc = *old_locp;
c7d46a38 12726 struct bp_location **loc2p;
876fa593 12727
e5dd4106 12728 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12729 not, we have to free it. */
c7d46a38 12730 int found_object = 0;
20874c92
VP
12731 /* Tells if the location should remain inserted in the target. */
12732 int keep_in_target = 0;
12733 int removed = 0;
876fa593 12734
4a64f543
MS
12735 /* Skip LOCP entries which will definitely never be needed.
12736 Stop either at or being the one matching OLD_LOC. */
876fa593 12737 while (locp < bp_location + bp_location_count
c7d46a38 12738 && (*locp)->address < old_loc->address)
876fa593 12739 locp++;
c7d46a38
PA
12740
12741 for (loc2p = locp;
12742 (loc2p < bp_location + bp_location_count
12743 && (*loc2p)->address == old_loc->address);
12744 loc2p++)
12745 {
b775012e
LM
12746 /* Check if this is a new/duplicated location or a duplicated
12747 location that had its condition modified. If so, we want to send
12748 its condition to the target if evaluation of conditions is taking
12749 place there. */
12750 if ((*loc2p)->condition_changed == condition_modified
12751 && (last_addr != old_loc->address
12752 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12753 {
b775012e
LM
12754 force_breakpoint_reinsertion (*loc2p);
12755 last_pspace_num = old_loc->pspace->num;
c7d46a38 12756 }
b775012e
LM
12757
12758 if (*loc2p == old_loc)
12759 found_object = 1;
c7d46a38 12760 }
74960c60 12761
b775012e
LM
12762 /* We have already handled this address, update it so that we don't
12763 have to go through updates again. */
12764 last_addr = old_loc->address;
12765
12766 /* Target-side condition evaluation: Handle deleted locations. */
12767 if (!found_object)
12768 force_breakpoint_reinsertion (old_loc);
12769
4a64f543
MS
12770 /* If this location is no longer present, and inserted, look if
12771 there's maybe a new location at the same address. If so,
12772 mark that one inserted, and don't remove this one. This is
12773 needed so that we don't have a time window where a breakpoint
12774 at certain location is not inserted. */
74960c60 12775
876fa593 12776 if (old_loc->inserted)
0d381245 12777 {
4a64f543
MS
12778 /* If the location is inserted now, we might have to remove
12779 it. */
74960c60 12780
876fa593 12781 if (found_object && should_be_inserted (old_loc))
74960c60 12782 {
4a64f543
MS
12783 /* The location is still present in the location list,
12784 and still should be inserted. Don't do anything. */
20874c92 12785 keep_in_target = 1;
74960c60
VP
12786 }
12787 else
12788 {
b775012e
LM
12789 /* This location still exists, but it won't be kept in the
12790 target since it may have been disabled. We proceed to
12791 remove its target-side condition. */
12792
4a64f543
MS
12793 /* The location is either no longer present, or got
12794 disabled. See if there's another location at the
12795 same address, in which case we don't need to remove
12796 this one from the target. */
876fa593 12797
2bdf28a0 12798 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12799 if (breakpoint_address_is_meaningful (old_loc->owner))
12800 {
876fa593 12801 for (loc2p = locp;
c7d46a38
PA
12802 (loc2p < bp_location + bp_location_count
12803 && (*loc2p)->address == old_loc->address);
876fa593
JK
12804 loc2p++)
12805 {
12806 struct bp_location *loc2 = *loc2p;
12807
2d134ed3 12808 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12809 {
85d721b8
PA
12810 /* Read watchpoint locations are switched to
12811 access watchpoints, if the former are not
12812 supported, but the latter are. */
12813 if (is_hardware_watchpoint (old_loc->owner))
12814 {
12815 gdb_assert (is_hardware_watchpoint (loc2->owner));
12816 loc2->watchpoint_type = old_loc->watchpoint_type;
12817 }
12818
934709f0
PW
12819 /* loc2 is a duplicated location. We need to check
12820 if it should be inserted in case it will be
12821 unduplicated. */
12822 if (loc2 != old_loc
12823 && unduplicated_should_be_inserted (loc2))
c7d46a38 12824 {
934709f0 12825 swap_insertion (old_loc, loc2);
c7d46a38
PA
12826 keep_in_target = 1;
12827 break;
12828 }
876fa593
JK
12829 }
12830 }
12831 }
74960c60
VP
12832 }
12833
20874c92
VP
12834 if (!keep_in_target)
12835 {
876fa593 12836 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12837 {
4a64f543
MS
12838 /* This is just about all we can do. We could keep
12839 this location on the global list, and try to
12840 remove it next time, but there's no particular
12841 reason why we will succeed next time.
20874c92 12842
4a64f543
MS
12843 Note that at this point, old_loc->owner is still
12844 valid, as delete_breakpoint frees the breakpoint
12845 only after calling us. */
3e43a32a
MS
12846 printf_filtered (_("warning: Error removing "
12847 "breakpoint %d\n"),
876fa593 12848 old_loc->owner->number);
20874c92
VP
12849 }
12850 removed = 1;
12851 }
0d381245 12852 }
74960c60
VP
12853
12854 if (!found_object)
1c5cfe86 12855 {
db82e815
PA
12856 if (removed && non_stop
12857 && breakpoint_address_is_meaningful (old_loc->owner)
12858 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12859 {
db82e815
PA
12860 /* This location was removed from the target. In
12861 non-stop mode, a race condition is possible where
12862 we've removed a breakpoint, but stop events for that
12863 breakpoint are already queued and will arrive later.
12864 We apply an heuristic to be able to distinguish such
12865 SIGTRAPs from other random SIGTRAPs: we keep this
12866 breakpoint location for a bit, and will retire it
12867 after we see some number of events. The theory here
12868 is that reporting of events should, "on the average",
12869 be fair, so after a while we'll see events from all
12870 threads that have anything of interest, and no longer
12871 need to keep this breakpoint location around. We
12872 don't hold locations forever so to reduce chances of
12873 mistaking a non-breakpoint SIGTRAP for a breakpoint
12874 SIGTRAP.
12875
12876 The heuristic failing can be disastrous on
12877 decr_pc_after_break targets.
12878
12879 On decr_pc_after_break targets, like e.g., x86-linux,
12880 if we fail to recognize a late breakpoint SIGTRAP,
12881 because events_till_retirement has reached 0 too
12882 soon, we'll fail to do the PC adjustment, and report
12883 a random SIGTRAP to the user. When the user resumes
12884 the inferior, it will most likely immediately crash
2dec564e 12885 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12886 corrupted, because of being resumed e.g., in the
12887 middle of a multi-byte instruction, or skipped a
12888 one-byte instruction. This was actually seen happen
12889 on native x86-linux, and should be less rare on
12890 targets that do not support new thread events, like
12891 remote, due to the heuristic depending on
12892 thread_count.
12893
12894 Mistaking a random SIGTRAP for a breakpoint trap
12895 causes similar symptoms (PC adjustment applied when
12896 it shouldn't), but then again, playing with SIGTRAPs
12897 behind the debugger's back is asking for trouble.
12898
12899 Since hardware watchpoint traps are always
12900 distinguishable from other traps, so we don't need to
12901 apply keep hardware watchpoint moribund locations
12902 around. We simply always ignore hardware watchpoint
12903 traps we can no longer explain. */
12904
876fa593
JK
12905 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12906 old_loc->owner = NULL;
20874c92 12907
876fa593 12908 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12909 }
12910 else
f431efe5
PA
12911 {
12912 old_loc->owner = NULL;
12913 decref_bp_location (&old_loc);
12914 }
20874c92 12915 }
74960c60 12916 }
1c5cfe86 12917
348d480f
PA
12918 /* Rescan breakpoints at the same address and section, marking the
12919 first one as "first" and any others as "duplicates". This is so
12920 that the bpt instruction is only inserted once. If we have a
12921 permanent breakpoint at the same place as BPT, make that one the
12922 official one, and the rest as duplicates. Permanent breakpoints
12923 are sorted first for the same address.
12924
12925 Do the same for hardware watchpoints, but also considering the
12926 watchpoint's type (regular/access/read) and length. */
12927
12928 bp_loc_first = NULL;
12929 wp_loc_first = NULL;
12930 awp_loc_first = NULL;
12931 rwp_loc_first = NULL;
12932 ALL_BP_LOCATIONS (loc, locp)
12933 {
12934 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12935 non-NULL. */
348d480f 12936 struct bp_location **loc_first_p;
d3fbdd86 12937 b = loc->owner;
348d480f 12938
6f380991 12939 if (!unduplicated_should_be_inserted (loc)
348d480f 12940 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12941 /* Don't detect duplicate for tracepoint locations because they are
12942 never duplicated. See the comments in field `duplicate' of
12943 `struct bp_location'. */
348d480f 12944 || is_tracepoint (b))
b775012e
LM
12945 {
12946 /* Clear the condition modification flag. */
12947 loc->condition_changed = condition_unchanged;
12948 continue;
12949 }
348d480f
PA
12950
12951 /* Permanent breakpoint should always be inserted. */
12952 if (b->enable_state == bp_permanent && ! loc->inserted)
12953 internal_error (__FILE__, __LINE__,
12954 _("allegedly permanent breakpoint is not "
12955 "actually inserted"));
12956
12957 if (b->type == bp_hardware_watchpoint)
12958 loc_first_p = &wp_loc_first;
12959 else if (b->type == bp_read_watchpoint)
12960 loc_first_p = &rwp_loc_first;
12961 else if (b->type == bp_access_watchpoint)
12962 loc_first_p = &awp_loc_first;
12963 else
12964 loc_first_p = &bp_loc_first;
12965
12966 if (*loc_first_p == NULL
12967 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12968 || !breakpoint_locations_match (loc, *loc_first_p))
12969 {
12970 *loc_first_p = loc;
12971 loc->duplicate = 0;
b775012e
LM
12972
12973 if (is_breakpoint (loc->owner) && loc->condition_changed)
12974 {
12975 loc->needs_update = 1;
12976 /* Clear the condition modification flag. */
12977 loc->condition_changed = condition_unchanged;
12978 }
348d480f
PA
12979 continue;
12980 }
12981
934709f0
PW
12982
12983 /* This and the above ensure the invariant that the first location
12984 is not duplicated, and is the inserted one.
12985 All following are marked as duplicated, and are not inserted. */
12986 if (loc->inserted)
12987 swap_insertion (loc, *loc_first_p);
348d480f
PA
12988 loc->duplicate = 1;
12989
b775012e
LM
12990 /* Clear the condition modification flag. */
12991 loc->condition_changed = condition_unchanged;
12992
348d480f
PA
12993 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12994 && b->enable_state != bp_permanent)
12995 internal_error (__FILE__, __LINE__,
12996 _("another breakpoint was inserted on top of "
12997 "a permanent breakpoint"));
12998 }
12999
a25a5a45 13000 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 13001 {
04086b45 13002 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
13003 insert_breakpoint_locations ();
13004 else
13005 {
44702360
PA
13006 /* Even though the caller told us to not insert new
13007 locations, we may still need to update conditions on the
13008 target's side of breakpoints that were already inserted
13009 if the target is evaluating breakpoint conditions. We
b775012e
LM
13010 only update conditions for locations that are marked
13011 "needs_update". */
13012 update_inserted_breakpoint_locations ();
13013 }
13014 }
348d480f 13015
04086b45 13016 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
13017 download_tracepoint_locations ();
13018
348d480f
PA
13019 do_cleanups (cleanups);
13020}
13021
13022void
13023breakpoint_retire_moribund (void)
13024{
13025 struct bp_location *loc;
13026 int ix;
13027
13028 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
13029 if (--(loc->events_till_retirement) == 0)
13030 {
13031 decref_bp_location (&loc);
13032 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
13033 --ix;
13034 }
13035}
13036
13037static void
44702360 13038update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 13039{
bfd189b1 13040 volatile struct gdb_exception e;
348d480f
PA
13041
13042 TRY_CATCH (e, RETURN_MASK_ERROR)
44702360 13043 update_global_location_list (insert_mode);
348d480f
PA
13044}
13045
13046/* Clear BKP from a BPS. */
13047
13048static void
13049bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
13050{
13051 bpstat bs;
13052
13053 for (bs = bps; bs; bs = bs->next)
13054 if (bs->breakpoint_at == bpt)
13055 {
13056 bs->breakpoint_at = NULL;
13057 bs->old_val = NULL;
13058 /* bs->commands will be freed later. */
13059 }
13060}
13061
13062/* Callback for iterate_over_threads. */
13063static int
13064bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
13065{
13066 struct breakpoint *bpt = data;
13067
13068 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
13069 return 0;
13070}
13071
13072/* Helper for breakpoint and tracepoint breakpoint_ops->mention
13073 callbacks. */
13074
13075static void
13076say_where (struct breakpoint *b)
13077{
13078 struct value_print_options opts;
13079
13080 get_user_print_options (&opts);
13081
13082 /* i18n: cagney/2005-02-11: Below needs to be merged into a
13083 single string. */
13084 if (b->loc == NULL)
13085 {
13086 printf_filtered (_(" (%s) pending."), b->addr_string);
13087 }
13088 else
13089 {
2f202fde 13090 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
13091 {
13092 printf_filtered (" at ");
13093 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
13094 gdb_stdout);
13095 }
2f202fde 13096 if (b->loc->symtab != NULL)
f8eba3c6
TT
13097 {
13098 /* If there is a single location, we can print the location
13099 more nicely. */
13100 if (b->loc->next == NULL)
13101 printf_filtered (": file %s, line %d.",
05cba821
JK
13102 symtab_to_filename_for_display (b->loc->symtab),
13103 b->loc->line_number);
f8eba3c6
TT
13104 else
13105 /* This is not ideal, but each location may have a
13106 different file name, and this at least reflects the
13107 real situation somewhat. */
13108 printf_filtered (": %s.", b->addr_string);
13109 }
348d480f
PA
13110
13111 if (b->loc->next)
13112 {
13113 struct bp_location *loc = b->loc;
13114 int n = 0;
13115 for (; loc; loc = loc->next)
13116 ++n;
13117 printf_filtered (" (%d locations)", n);
13118 }
13119 }
13120}
13121
348d480f
PA
13122/* Default bp_location_ops methods. */
13123
13124static void
13125bp_location_dtor (struct bp_location *self)
13126{
13127 xfree (self->cond);
b775012e
LM
13128 if (self->cond_bytecode)
13129 free_agent_expr (self->cond_bytecode);
348d480f 13130 xfree (self->function_name);
8b4f3082
PA
13131
13132 VEC_free (agent_expr_p, self->target_info.conditions);
13133 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
13134}
13135
13136static const struct bp_location_ops bp_location_ops =
13137{
13138 bp_location_dtor
13139};
13140
2060206e
PA
13141/* Default breakpoint_ops methods all breakpoint_ops ultimately
13142 inherit from. */
348d480f 13143
2060206e
PA
13144static void
13145base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
13146{
13147 decref_counted_command_line (&self->commands);
13148 xfree (self->cond_string);
fb81d016 13149 xfree (self->extra_string);
348d480f 13150 xfree (self->addr_string);
f8eba3c6 13151 xfree (self->filter);
348d480f 13152 xfree (self->addr_string_range_end);
348d480f
PA
13153}
13154
2060206e
PA
13155static struct bp_location *
13156base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
13157{
13158 struct bp_location *loc;
13159
13160 loc = XNEW (struct bp_location);
13161 init_bp_location (loc, &bp_location_ops, self);
13162 return loc;
13163}
13164
2060206e
PA
13165static void
13166base_breakpoint_re_set (struct breakpoint *b)
13167{
13168 /* Nothing to re-set. */
13169}
13170
13171#define internal_error_pure_virtual_called() \
13172 gdb_assert_not_reached ("pure virtual function called")
13173
13174static int
13175base_breakpoint_insert_location (struct bp_location *bl)
13176{
13177 internal_error_pure_virtual_called ();
13178}
13179
13180static int
13181base_breakpoint_remove_location (struct bp_location *bl)
13182{
13183 internal_error_pure_virtual_called ();
13184}
13185
13186static int
13187base_breakpoint_breakpoint_hit (const struct bp_location *bl,
13188 struct address_space *aspace,
09ac7c10
TT
13189 CORE_ADDR bp_addr,
13190 const struct target_waitstatus *ws)
2060206e
PA
13191{
13192 internal_error_pure_virtual_called ();
13193}
13194
13195static void
13196base_breakpoint_check_status (bpstat bs)
13197{
13198 /* Always stop. */
13199}
13200
13201/* A "works_in_software_mode" breakpoint_ops method that just internal
13202 errors. */
13203
13204static int
13205base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13206{
13207 internal_error_pure_virtual_called ();
13208}
13209
13210/* A "resources_needed" breakpoint_ops method that just internal
13211 errors. */
13212
13213static int
13214base_breakpoint_resources_needed (const struct bp_location *bl)
13215{
13216 internal_error_pure_virtual_called ();
13217}
13218
13219static enum print_stop_action
13220base_breakpoint_print_it (bpstat bs)
13221{
13222 internal_error_pure_virtual_called ();
13223}
13224
13225static void
13226base_breakpoint_print_one_detail (const struct breakpoint *self,
13227 struct ui_out *uiout)
13228{
13229 /* nothing */
13230}
13231
13232static void
13233base_breakpoint_print_mention (struct breakpoint *b)
13234{
13235 internal_error_pure_virtual_called ();
13236}
13237
13238static void
13239base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13240{
13241 internal_error_pure_virtual_called ();
13242}
13243
983af33b
SDJ
13244static void
13245base_breakpoint_create_sals_from_address (char **arg,
13246 struct linespec_result *canonical,
13247 enum bptype type_wanted,
13248 char *addr_start,
13249 char **copy_arg)
13250{
13251 internal_error_pure_virtual_called ();
13252}
13253
13254static void
13255base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13256 struct linespec_result *c,
983af33b 13257 char *cond_string,
e7e0cddf 13258 char *extra_string,
983af33b
SDJ
13259 enum bptype type_wanted,
13260 enum bpdisp disposition,
13261 int thread,
13262 int task, int ignore_count,
13263 const struct breakpoint_ops *o,
13264 int from_tty, int enabled,
44f238bb 13265 int internal, unsigned flags)
983af33b
SDJ
13266{
13267 internal_error_pure_virtual_called ();
13268}
13269
13270static void
13271base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13272 struct symtabs_and_lines *sals)
13273{
13274 internal_error_pure_virtual_called ();
13275}
13276
ab04a2af
TT
13277/* The default 'explains_signal' method. */
13278
47591c29 13279static int
427cd150 13280base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13281{
47591c29 13282 return 1;
ab04a2af
TT
13283}
13284
9d6e6e84
HZ
13285/* The default "after_condition_true" method. */
13286
13287static void
13288base_breakpoint_after_condition_true (struct bpstats *bs)
13289{
13290 /* Nothing to do. */
13291}
13292
ab04a2af 13293struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13294{
13295 base_breakpoint_dtor,
13296 base_breakpoint_allocate_location,
13297 base_breakpoint_re_set,
13298 base_breakpoint_insert_location,
13299 base_breakpoint_remove_location,
13300 base_breakpoint_breakpoint_hit,
13301 base_breakpoint_check_status,
13302 base_breakpoint_resources_needed,
13303 base_breakpoint_works_in_software_mode,
13304 base_breakpoint_print_it,
13305 NULL,
13306 base_breakpoint_print_one_detail,
13307 base_breakpoint_print_mention,
983af33b
SDJ
13308 base_breakpoint_print_recreate,
13309 base_breakpoint_create_sals_from_address,
13310 base_breakpoint_create_breakpoints_sal,
13311 base_breakpoint_decode_linespec,
9d6e6e84
HZ
13312 base_breakpoint_explains_signal,
13313 base_breakpoint_after_condition_true,
2060206e
PA
13314};
13315
13316/* Default breakpoint_ops methods. */
13317
13318static void
348d480f
PA
13319bkpt_re_set (struct breakpoint *b)
13320{
06edf0c0
PA
13321 /* FIXME: is this still reachable? */
13322 if (b->addr_string == NULL)
13323 {
13324 /* Anything without a string can't be re-set. */
348d480f 13325 delete_breakpoint (b);
06edf0c0 13326 return;
348d480f 13327 }
06edf0c0
PA
13328
13329 breakpoint_re_set_default (b);
348d480f
PA
13330}
13331
ef370185
JB
13332/* Copy SRC's shadow buffer and whatever else we'd set if we actually
13333 inserted DEST, so we can remove it later, in case SRC is removed
13334 first. */
13335
13336static void
13337bp_target_info_copy_insertion_state (struct bp_target_info *dest,
13338 const struct bp_target_info *src)
13339{
13340 dest->shadow_len = src->shadow_len;
13341 memcpy (dest->shadow_contents, src->shadow_contents, src->shadow_len);
0d5ed153 13342 dest->placed_address = src->placed_address;
ef370185
JB
13343 dest->placed_size = src->placed_size;
13344}
13345
2060206e 13346static int
348d480f
PA
13347bkpt_insert_location (struct bp_location *bl)
13348{
13349 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 13350 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 13351 else
7c16b83e 13352 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13353}
13354
2060206e 13355static int
348d480f
PA
13356bkpt_remove_location (struct bp_location *bl)
13357{
13358 if (bl->loc_type == bp_loc_hardware_breakpoint)
13359 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13360 else
7c16b83e 13361 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13362}
13363
2060206e 13364static int
348d480f 13365bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13366 struct address_space *aspace, CORE_ADDR bp_addr,
13367 const struct target_waitstatus *ws)
348d480f 13368{
09ac7c10 13369 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13370 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13371 return 0;
13372
348d480f
PA
13373 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13374 aspace, bp_addr))
13375 return 0;
13376
13377 if (overlay_debugging /* unmapped overlay section */
13378 && section_is_overlay (bl->section)
13379 && !section_is_mapped (bl->section))
13380 return 0;
13381
13382 return 1;
13383}
13384
cd1608cc
PA
13385static int
13386dprintf_breakpoint_hit (const struct bp_location *bl,
13387 struct address_space *aspace, CORE_ADDR bp_addr,
13388 const struct target_waitstatus *ws)
13389{
13390 if (dprintf_style == dprintf_style_agent
13391 && target_can_run_breakpoint_commands ())
13392 {
13393 /* An agent-style dprintf never causes a stop. If we see a trap
13394 for this address it must be for a breakpoint that happens to
13395 be set at the same address. */
13396 return 0;
13397 }
13398
13399 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13400}
13401
2060206e 13402static int
348d480f
PA
13403bkpt_resources_needed (const struct bp_location *bl)
13404{
13405 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13406
13407 return 1;
13408}
13409
2060206e 13410static enum print_stop_action
348d480f
PA
13411bkpt_print_it (bpstat bs)
13412{
348d480f
PA
13413 struct breakpoint *b;
13414 const struct bp_location *bl;
001c8c33 13415 int bp_temp;
79a45e25 13416 struct ui_out *uiout = current_uiout;
348d480f
PA
13417
13418 gdb_assert (bs->bp_location_at != NULL);
13419
13420 bl = bs->bp_location_at;
13421 b = bs->breakpoint_at;
13422
001c8c33
PA
13423 bp_temp = b->disposition == disp_del;
13424 if (bl->address != bl->requested_address)
13425 breakpoint_adjustment_warning (bl->requested_address,
13426 bl->address,
13427 b->number, 1);
13428 annotate_breakpoint (b->number);
13429 if (bp_temp)
13430 ui_out_text (uiout, "\nTemporary breakpoint ");
13431 else
13432 ui_out_text (uiout, "\nBreakpoint ");
13433 if (ui_out_is_mi_like_p (uiout))
348d480f 13434 {
001c8c33
PA
13435 ui_out_field_string (uiout, "reason",
13436 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13437 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13438 }
001c8c33
PA
13439 ui_out_field_int (uiout, "bkptno", b->number);
13440 ui_out_text (uiout, ", ");
06edf0c0 13441
001c8c33 13442 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13443}
13444
2060206e 13445static void
06edf0c0
PA
13446bkpt_print_mention (struct breakpoint *b)
13447{
79a45e25 13448 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13449 return;
13450
13451 switch (b->type)
13452 {
13453 case bp_breakpoint:
13454 case bp_gnu_ifunc_resolver:
13455 if (b->disposition == disp_del)
13456 printf_filtered (_("Temporary breakpoint"));
13457 else
13458 printf_filtered (_("Breakpoint"));
13459 printf_filtered (_(" %d"), b->number);
13460 if (b->type == bp_gnu_ifunc_resolver)
13461 printf_filtered (_(" at gnu-indirect-function resolver"));
13462 break;
13463 case bp_hardware_breakpoint:
13464 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13465 break;
e7e0cddf
SS
13466 case bp_dprintf:
13467 printf_filtered (_("Dprintf %d"), b->number);
13468 break;
06edf0c0
PA
13469 }
13470
13471 say_where (b);
13472}
13473
2060206e 13474static void
06edf0c0
PA
13475bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13476{
13477 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13478 fprintf_unfiltered (fp, "tbreak");
13479 else if (tp->type == bp_breakpoint)
13480 fprintf_unfiltered (fp, "break");
13481 else if (tp->type == bp_hardware_breakpoint
13482 && tp->disposition == disp_del)
13483 fprintf_unfiltered (fp, "thbreak");
13484 else if (tp->type == bp_hardware_breakpoint)
13485 fprintf_unfiltered (fp, "hbreak");
13486 else
13487 internal_error (__FILE__, __LINE__,
13488 _("unhandled breakpoint type %d"), (int) tp->type);
13489
2060206e 13490 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13491 print_recreate_thread (tp, fp);
06edf0c0
PA
13492}
13493
983af33b
SDJ
13494static void
13495bkpt_create_sals_from_address (char **arg,
13496 struct linespec_result *canonical,
13497 enum bptype type_wanted,
13498 char *addr_start, char **copy_arg)
13499{
13500 create_sals_from_address_default (arg, canonical, type_wanted,
13501 addr_start, copy_arg);
13502}
13503
13504static void
13505bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13506 struct linespec_result *canonical,
983af33b 13507 char *cond_string,
e7e0cddf 13508 char *extra_string,
983af33b
SDJ
13509 enum bptype type_wanted,
13510 enum bpdisp disposition,
13511 int thread,
13512 int task, int ignore_count,
13513 const struct breakpoint_ops *ops,
13514 int from_tty, int enabled,
44f238bb 13515 int internal, unsigned flags)
983af33b 13516{
023fa29b 13517 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13518 cond_string, extra_string,
13519 type_wanted,
983af33b
SDJ
13520 disposition, thread, task,
13521 ignore_count, ops, from_tty,
44f238bb 13522 enabled, internal, flags);
983af33b
SDJ
13523}
13524
13525static void
13526bkpt_decode_linespec (struct breakpoint *b, char **s,
13527 struct symtabs_and_lines *sals)
13528{
13529 decode_linespec_default (b, s, sals);
13530}
13531
06edf0c0
PA
13532/* Virtual table for internal breakpoints. */
13533
13534static void
13535internal_bkpt_re_set (struct breakpoint *b)
13536{
13537 switch (b->type)
13538 {
13539 /* Delete overlay event and longjmp master breakpoints; they
13540 will be reset later by breakpoint_re_set. */
13541 case bp_overlay_event:
13542 case bp_longjmp_master:
13543 case bp_std_terminate_master:
13544 case bp_exception_master:
13545 delete_breakpoint (b);
13546 break;
13547
13548 /* This breakpoint is special, it's set up when the inferior
13549 starts and we really don't want to touch it. */
13550 case bp_shlib_event:
13551
13552 /* Like bp_shlib_event, this breakpoint type is special. Once
13553 it is set up, we do not want to touch it. */
13554 case bp_thread_event:
13555 break;
13556 }
13557}
13558
13559static void
13560internal_bkpt_check_status (bpstat bs)
13561{
a9b3a50f
PA
13562 if (bs->breakpoint_at->type == bp_shlib_event)
13563 {
13564 /* If requested, stop when the dynamic linker notifies GDB of
13565 events. This allows the user to get control and place
13566 breakpoints in initializer routines for dynamically loaded
13567 objects (among other things). */
13568 bs->stop = stop_on_solib_events;
13569 bs->print = stop_on_solib_events;
13570 }
13571 else
13572 bs->stop = 0;
06edf0c0
PA
13573}
13574
13575static enum print_stop_action
13576internal_bkpt_print_it (bpstat bs)
13577{
06edf0c0 13578 struct breakpoint *b;
06edf0c0 13579
06edf0c0
PA
13580 b = bs->breakpoint_at;
13581
06edf0c0
PA
13582 switch (b->type)
13583 {
348d480f
PA
13584 case bp_shlib_event:
13585 /* Did we stop because the user set the stop_on_solib_events
13586 variable? (If so, we report this as a generic, "Stopped due
13587 to shlib event" message.) */
edcc5120 13588 print_solib_event (0);
348d480f
PA
13589 break;
13590
13591 case bp_thread_event:
13592 /* Not sure how we will get here.
13593 GDB should not stop for these breakpoints. */
13594 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13595 break;
13596
13597 case bp_overlay_event:
13598 /* By analogy with the thread event, GDB should not stop for these. */
13599 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13600 break;
13601
13602 case bp_longjmp_master:
13603 /* These should never be enabled. */
13604 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13605 break;
13606
13607 case bp_std_terminate_master:
13608 /* These should never be enabled. */
13609 printf_filtered (_("std::terminate Master Breakpoint: "
13610 "gdb should not stop!\n"));
348d480f
PA
13611 break;
13612
13613 case bp_exception_master:
13614 /* These should never be enabled. */
13615 printf_filtered (_("Exception Master Breakpoint: "
13616 "gdb should not stop!\n"));
06edf0c0
PA
13617 break;
13618 }
13619
001c8c33 13620 return PRINT_NOTHING;
06edf0c0
PA
13621}
13622
13623static void
13624internal_bkpt_print_mention (struct breakpoint *b)
13625{
13626 /* Nothing to mention. These breakpoints are internal. */
13627}
13628
06edf0c0
PA
13629/* Virtual table for momentary breakpoints */
13630
13631static void
13632momentary_bkpt_re_set (struct breakpoint *b)
13633{
13634 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13635 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13636 Otherwise these should have been blown away via the cleanup chain
13637 or by breakpoint_init_inferior when we rerun the executable. */
13638}
13639
13640static void
13641momentary_bkpt_check_status (bpstat bs)
13642{
13643 /* Nothing. The point of these breakpoints is causing a stop. */
13644}
13645
13646static enum print_stop_action
13647momentary_bkpt_print_it (bpstat bs)
13648{
79a45e25
PA
13649 struct ui_out *uiout = current_uiout;
13650
001c8c33 13651 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13652 {
001c8c33 13653 struct breakpoint *b = bs->breakpoint_at;
348d480f 13654
001c8c33
PA
13655 switch (b->type)
13656 {
13657 case bp_finish:
13658 ui_out_field_string
13659 (uiout, "reason",
13660 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13661 break;
348d480f 13662
001c8c33
PA
13663 case bp_until:
13664 ui_out_field_string
13665 (uiout, "reason",
13666 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13667 break;
13668 }
348d480f
PA
13669 }
13670
001c8c33 13671 return PRINT_UNKNOWN;
348d480f
PA
13672}
13673
06edf0c0
PA
13674static void
13675momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13676{
06edf0c0 13677 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13678}
13679
e2e4d78b
JK
13680/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13681
13682 It gets cleared already on the removal of the first one of such placed
13683 breakpoints. This is OK as they get all removed altogether. */
13684
13685static void
13686longjmp_bkpt_dtor (struct breakpoint *self)
13687{
13688 struct thread_info *tp = find_thread_id (self->thread);
13689
13690 if (tp)
13691 tp->initiating_frame = null_frame_id;
13692
13693 momentary_breakpoint_ops.dtor (self);
13694}
13695
55aa24fb
SDJ
13696/* Specific methods for probe breakpoints. */
13697
13698static int
13699bkpt_probe_insert_location (struct bp_location *bl)
13700{
13701 int v = bkpt_insert_location (bl);
13702
13703 if (v == 0)
13704 {
13705 /* The insertion was successful, now let's set the probe's semaphore
13706 if needed. */
0ea5cda8
SDJ
13707 if (bl->probe.probe->pops->set_semaphore != NULL)
13708 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13709 bl->probe.objfile,
13710 bl->gdbarch);
55aa24fb
SDJ
13711 }
13712
13713 return v;
13714}
13715
13716static int
13717bkpt_probe_remove_location (struct bp_location *bl)
13718{
13719 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13720 if (bl->probe.probe->pops->clear_semaphore != NULL)
13721 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13722 bl->probe.objfile,
13723 bl->gdbarch);
55aa24fb
SDJ
13724
13725 return bkpt_remove_location (bl);
13726}
13727
13728static void
13729bkpt_probe_create_sals_from_address (char **arg,
13730 struct linespec_result *canonical,
13731 enum bptype type_wanted,
13732 char *addr_start, char **copy_arg)
13733{
13734 struct linespec_sals lsal;
13735
13736 lsal.sals = parse_probes (arg, canonical);
13737
13738 *copy_arg = xstrdup (canonical->addr_string);
13739 lsal.canonical = xstrdup (*copy_arg);
13740
13741 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13742}
13743
13744static void
13745bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13746 struct symtabs_and_lines *sals)
13747{
13748 *sals = parse_probes (s, NULL);
13749 if (!sals->sals)
13750 error (_("probe not found"));
13751}
13752
348d480f 13753/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13754
348d480f
PA
13755static void
13756tracepoint_re_set (struct breakpoint *b)
13757{
13758 breakpoint_re_set_default (b);
13759}
876fa593 13760
348d480f
PA
13761static int
13762tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13763 struct address_space *aspace, CORE_ADDR bp_addr,
13764 const struct target_waitstatus *ws)
348d480f
PA
13765{
13766 /* By definition, the inferior does not report stops at
13767 tracepoints. */
13768 return 0;
74960c60
VP
13769}
13770
13771static void
348d480f
PA
13772tracepoint_print_one_detail (const struct breakpoint *self,
13773 struct ui_out *uiout)
74960c60 13774{
d9b3f62e
PA
13775 struct tracepoint *tp = (struct tracepoint *) self;
13776 if (tp->static_trace_marker_id)
348d480f
PA
13777 {
13778 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13779
348d480f
PA
13780 ui_out_text (uiout, "\tmarker id is ");
13781 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13782 tp->static_trace_marker_id);
348d480f
PA
13783 ui_out_text (uiout, "\n");
13784 }
0d381245
VP
13785}
13786
a474d7c2 13787static void
348d480f 13788tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13789{
79a45e25 13790 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13791 return;
cc59ec59 13792
348d480f
PA
13793 switch (b->type)
13794 {
13795 case bp_tracepoint:
13796 printf_filtered (_("Tracepoint"));
13797 printf_filtered (_(" %d"), b->number);
13798 break;
13799 case bp_fast_tracepoint:
13800 printf_filtered (_("Fast tracepoint"));
13801 printf_filtered (_(" %d"), b->number);
13802 break;
13803 case bp_static_tracepoint:
13804 printf_filtered (_("Static tracepoint"));
13805 printf_filtered (_(" %d"), b->number);
13806 break;
13807 default:
13808 internal_error (__FILE__, __LINE__,
13809 _("unhandled tracepoint type %d"), (int) b->type);
13810 }
13811
13812 say_where (b);
a474d7c2
PA
13813}
13814
348d480f 13815static void
d9b3f62e 13816tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13817{
d9b3f62e
PA
13818 struct tracepoint *tp = (struct tracepoint *) self;
13819
13820 if (self->type == bp_fast_tracepoint)
348d480f 13821 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13822 if (self->type == bp_static_tracepoint)
348d480f 13823 fprintf_unfiltered (fp, "strace");
d9b3f62e 13824 else if (self->type == bp_tracepoint)
348d480f
PA
13825 fprintf_unfiltered (fp, "trace");
13826 else
13827 internal_error (__FILE__, __LINE__,
d9b3f62e 13828 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13829
d9b3f62e
PA
13830 fprintf_unfiltered (fp, " %s", self->addr_string);
13831 print_recreate_thread (self, fp);
13832
13833 if (tp->pass_count)
13834 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13835}
13836
983af33b
SDJ
13837static void
13838tracepoint_create_sals_from_address (char **arg,
13839 struct linespec_result *canonical,
13840 enum bptype type_wanted,
13841 char *addr_start, char **copy_arg)
13842{
13843 create_sals_from_address_default (arg, canonical, type_wanted,
13844 addr_start, copy_arg);
13845}
13846
13847static void
13848tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13849 struct linespec_result *canonical,
983af33b 13850 char *cond_string,
e7e0cddf 13851 char *extra_string,
983af33b
SDJ
13852 enum bptype type_wanted,
13853 enum bpdisp disposition,
13854 int thread,
13855 int task, int ignore_count,
13856 const struct breakpoint_ops *ops,
13857 int from_tty, int enabled,
44f238bb 13858 int internal, unsigned flags)
983af33b 13859{
023fa29b 13860 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13861 cond_string, extra_string,
13862 type_wanted,
983af33b
SDJ
13863 disposition, thread, task,
13864 ignore_count, ops, from_tty,
44f238bb 13865 enabled, internal, flags);
983af33b
SDJ
13866}
13867
13868static void
13869tracepoint_decode_linespec (struct breakpoint *b, char **s,
13870 struct symtabs_and_lines *sals)
13871{
13872 decode_linespec_default (b, s, sals);
13873}
13874
2060206e 13875struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13876
55aa24fb
SDJ
13877/* The breakpoint_ops structure to be use on tracepoints placed in a
13878 static probe. */
13879
13880static void
13881tracepoint_probe_create_sals_from_address (char **arg,
13882 struct linespec_result *canonical,
13883 enum bptype type_wanted,
13884 char *addr_start, char **copy_arg)
13885{
13886 /* We use the same method for breakpoint on probes. */
13887 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13888 addr_start, copy_arg);
13889}
13890
13891static void
13892tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13893 struct symtabs_and_lines *sals)
13894{
13895 /* We use the same method for breakpoint on probes. */
13896 bkpt_probe_decode_linespec (b, s, sals);
13897}
13898
13899static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13900
5c2b4418
HZ
13901/* Dprintf breakpoint_ops methods. */
13902
13903static void
13904dprintf_re_set (struct breakpoint *b)
13905{
13906 breakpoint_re_set_default (b);
13907
13908 /* This breakpoint could have been pending, and be resolved now, and
13909 if so, we should now have the extra string. If we don't, the
13910 dprintf was malformed when created, but we couldn't tell because
13911 we can't extract the extra string until the location is
13912 resolved. */
13913 if (b->loc != NULL && b->extra_string == NULL)
13914 error (_("Format string required"));
13915
13916 /* 1 - connect to target 1, that can run breakpoint commands.
13917 2 - create a dprintf, which resolves fine.
13918 3 - disconnect from target 1
13919 4 - connect to target 2, that can NOT run breakpoint commands.
13920
13921 After steps #3/#4, you'll want the dprintf command list to
13922 be updated, because target 1 and 2 may well return different
13923 answers for target_can_run_breakpoint_commands().
13924 Given absence of finer grained resetting, we get to do
13925 it all the time. */
13926 if (b->extra_string != NULL)
13927 update_dprintf_command_list (b);
13928}
13929
2d9442cc
HZ
13930/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13931
13932static void
13933dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13934{
13935 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13936 tp->extra_string);
13937 print_recreate_thread (tp, fp);
13938}
13939
9d6e6e84
HZ
13940/* Implement the "after_condition_true" breakpoint_ops method for
13941 dprintf.
13942
13943 dprintf's are implemented with regular commands in their command
13944 list, but we run the commands here instead of before presenting the
13945 stop to the user, as dprintf's don't actually cause a stop. This
13946 also makes it so that the commands of multiple dprintfs at the same
13947 address are all handled. */
13948
13949static void
13950dprintf_after_condition_true (struct bpstats *bs)
13951{
13952 struct cleanup *old_chain;
13953 struct bpstats tmp_bs = { NULL };
13954 struct bpstats *tmp_bs_p = &tmp_bs;
13955
13956 /* dprintf's never cause a stop. This wasn't set in the
13957 check_status hook instead because that would make the dprintf's
13958 condition not be evaluated. */
13959 bs->stop = 0;
13960
13961 /* Run the command list here. Take ownership of it instead of
13962 copying. We never want these commands to run later in
13963 bpstat_do_actions, if a breakpoint that causes a stop happens to
13964 be set at same address as this dprintf, or even if running the
13965 commands here throws. */
13966 tmp_bs.commands = bs->commands;
13967 bs->commands = NULL;
13968 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13969
13970 bpstat_do_actions_1 (&tmp_bs_p);
13971
13972 /* 'tmp_bs.commands' will usually be NULL by now, but
13973 bpstat_do_actions_1 may return early without processing the whole
13974 list. */
13975 do_cleanups (old_chain);
13976}
13977
983af33b
SDJ
13978/* The breakpoint_ops structure to be used on static tracepoints with
13979 markers (`-m'). */
13980
13981static void
13982strace_marker_create_sals_from_address (char **arg,
13983 struct linespec_result *canonical,
13984 enum bptype type_wanted,
13985 char *addr_start, char **copy_arg)
13986{
13987 struct linespec_sals lsal;
13988
13989 lsal.sals = decode_static_tracepoint_spec (arg);
13990
13991 *copy_arg = savestring (addr_start, *arg - addr_start);
13992
13993 canonical->addr_string = xstrdup (*copy_arg);
13994 lsal.canonical = xstrdup (*copy_arg);
13995 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13996}
13997
13998static void
13999strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
14000 struct linespec_result *canonical,
983af33b 14001 char *cond_string,
e7e0cddf 14002 char *extra_string,
983af33b
SDJ
14003 enum bptype type_wanted,
14004 enum bpdisp disposition,
14005 int thread,
14006 int task, int ignore_count,
14007 const struct breakpoint_ops *ops,
14008 int from_tty, int enabled,
44f238bb 14009 int internal, unsigned flags)
983af33b
SDJ
14010{
14011 int i;
52d361e1
YQ
14012 struct linespec_sals *lsal = VEC_index (linespec_sals,
14013 canonical->sals, 0);
983af33b
SDJ
14014
14015 /* If the user is creating a static tracepoint by marker id
14016 (strace -m MARKER_ID), then store the sals index, so that
14017 breakpoint_re_set can try to match up which of the newly
14018 found markers corresponds to this one, and, don't try to
14019 expand multiple locations for each sal, given than SALS
14020 already should contain all sals for MARKER_ID. */
14021
14022 for (i = 0; i < lsal->sals.nelts; ++i)
14023 {
14024 struct symtabs_and_lines expanded;
14025 struct tracepoint *tp;
14026 struct cleanup *old_chain;
14027 char *addr_string;
14028
14029 expanded.nelts = 1;
14030 expanded.sals = &lsal->sals.sals[i];
14031
14032 addr_string = xstrdup (canonical->addr_string);
14033 old_chain = make_cleanup (xfree, addr_string);
14034
14035 tp = XCNEW (struct tracepoint);
14036 init_breakpoint_sal (&tp->base, gdbarch, expanded,
14037 addr_string, NULL,
e7e0cddf
SS
14038 cond_string, extra_string,
14039 type_wanted, disposition,
983af33b 14040 thread, task, ignore_count, ops,
44f238bb 14041 from_tty, enabled, internal, flags,
983af33b
SDJ
14042 canonical->special_display);
14043 /* Given that its possible to have multiple markers with
14044 the same string id, if the user is creating a static
14045 tracepoint by marker id ("strace -m MARKER_ID"), then
14046 store the sals index, so that breakpoint_re_set can
14047 try to match up which of the newly found markers
14048 corresponds to this one */
14049 tp->static_trace_marker_id_idx = i;
14050
14051 install_breakpoint (internal, &tp->base, 0);
14052
14053 discard_cleanups (old_chain);
14054 }
14055}
14056
14057static void
14058strace_marker_decode_linespec (struct breakpoint *b, char **s,
14059 struct symtabs_and_lines *sals)
14060{
14061 struct tracepoint *tp = (struct tracepoint *) b;
14062
14063 *sals = decode_static_tracepoint_spec (s);
14064 if (sals->nelts > tp->static_trace_marker_id_idx)
14065 {
14066 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
14067 sals->nelts = 1;
14068 }
14069 else
14070 error (_("marker %s not found"), tp->static_trace_marker_id);
14071}
14072
14073static struct breakpoint_ops strace_marker_breakpoint_ops;
14074
14075static int
14076strace_marker_p (struct breakpoint *b)
14077{
14078 return b->ops == &strace_marker_breakpoint_ops;
14079}
14080
53a5351d 14081/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 14082 structures. */
c906108c
SS
14083
14084void
fba45db2 14085delete_breakpoint (struct breakpoint *bpt)
c906108c 14086{
52f0bd74 14087 struct breakpoint *b;
c906108c 14088
8a3fe4f8 14089 gdb_assert (bpt != NULL);
c906108c 14090
4a64f543
MS
14091 /* Has this bp already been deleted? This can happen because
14092 multiple lists can hold pointers to bp's. bpstat lists are
14093 especial culprits.
14094
14095 One example of this happening is a watchpoint's scope bp. When
14096 the scope bp triggers, we notice that the watchpoint is out of
14097 scope, and delete it. We also delete its scope bp. But the
14098 scope bp is marked "auto-deleting", and is already on a bpstat.
14099 That bpstat is then checked for auto-deleting bp's, which are
14100 deleted.
14101
14102 A real solution to this problem might involve reference counts in
14103 bp's, and/or giving them pointers back to their referencing
14104 bpstat's, and teaching delete_breakpoint to only free a bp's
14105 storage when no more references were extent. A cheaper bandaid
14106 was chosen. */
c906108c
SS
14107 if (bpt->type == bp_none)
14108 return;
14109
4a64f543
MS
14110 /* At least avoid this stale reference until the reference counting
14111 of breakpoints gets resolved. */
d0fb5eae 14112 if (bpt->related_breakpoint != bpt)
e5a0a904 14113 {
d0fb5eae 14114 struct breakpoint *related;
3a5c3e22 14115 struct watchpoint *w;
d0fb5eae
JK
14116
14117 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 14118 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 14119 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
14120 w = (struct watchpoint *) bpt;
14121 else
14122 w = NULL;
14123 if (w != NULL)
14124 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
14125
14126 /* Unlink bpt from the bpt->related_breakpoint ring. */
14127 for (related = bpt; related->related_breakpoint != bpt;
14128 related = related->related_breakpoint);
14129 related->related_breakpoint = bpt->related_breakpoint;
14130 bpt->related_breakpoint = bpt;
e5a0a904
JK
14131 }
14132
a9634178
TJB
14133 /* watch_command_1 creates a watchpoint but only sets its number if
14134 update_watchpoint succeeds in creating its bp_locations. If there's
14135 a problem in that process, we'll be asked to delete the half-created
14136 watchpoint. In that case, don't announce the deletion. */
14137 if (bpt->number)
14138 observer_notify_breakpoint_deleted (bpt);
c906108c 14139
c906108c
SS
14140 if (breakpoint_chain == bpt)
14141 breakpoint_chain = bpt->next;
14142
c906108c
SS
14143 ALL_BREAKPOINTS (b)
14144 if (b->next == bpt)
c5aa993b
JM
14145 {
14146 b->next = bpt->next;
14147 break;
14148 }
c906108c 14149
f431efe5
PA
14150 /* Be sure no bpstat's are pointing at the breakpoint after it's
14151 been freed. */
14152 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 14153 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
14154 pointing at bpt from the stop_bpstat list entirely, as breakpoint
14155 commands are associated with the bpstat; if we remove it here,
14156 then the later call to bpstat_do_actions (&stop_bpstat); in
14157 event-top.c won't do anything, and temporary breakpoints with
14158 commands won't work. */
14159
14160 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
14161
4a64f543
MS
14162 /* Now that breakpoint is removed from breakpoint list, update the
14163 global location list. This will remove locations that used to
14164 belong to this breakpoint. Do this before freeing the breakpoint
14165 itself, since remove_breakpoint looks at location's owner. It
14166 might be better design to have location completely
14167 self-contained, but it's not the case now. */
44702360 14168 update_global_location_list (UGLL_DONT_INSERT);
74960c60 14169
348d480f 14170 bpt->ops->dtor (bpt);
4a64f543
MS
14171 /* On the chance that someone will soon try again to delete this
14172 same bp, we mark it as deleted before freeing its storage. */
c906108c 14173 bpt->type = bp_none;
b8c9b27d 14174 xfree (bpt);
c906108c
SS
14175}
14176
4d6140d9
AC
14177static void
14178do_delete_breakpoint_cleanup (void *b)
14179{
14180 delete_breakpoint (b);
14181}
14182
14183struct cleanup *
14184make_cleanup_delete_breakpoint (struct breakpoint *b)
14185{
14186 return make_cleanup (do_delete_breakpoint_cleanup, b);
14187}
14188
51be5b68
PA
14189/* Iterator function to call a user-provided callback function once
14190 for each of B and its related breakpoints. */
14191
14192static void
14193iterate_over_related_breakpoints (struct breakpoint *b,
14194 void (*function) (struct breakpoint *,
14195 void *),
14196 void *data)
14197{
14198 struct breakpoint *related;
14199
14200 related = b;
14201 do
14202 {
14203 struct breakpoint *next;
14204
14205 /* FUNCTION may delete RELATED. */
14206 next = related->related_breakpoint;
14207
14208 if (next == related)
14209 {
14210 /* RELATED is the last ring entry. */
14211 function (related, data);
14212
14213 /* FUNCTION may have deleted it, so we'd never reach back to
14214 B. There's nothing left to do anyway, so just break
14215 out. */
14216 break;
14217 }
14218 else
14219 function (related, data);
14220
14221 related = next;
14222 }
14223 while (related != b);
14224}
95a42b64
TT
14225
14226static void
14227do_delete_breakpoint (struct breakpoint *b, void *ignore)
14228{
14229 delete_breakpoint (b);
14230}
14231
51be5b68
PA
14232/* A callback for map_breakpoint_numbers that calls
14233 delete_breakpoint. */
14234
14235static void
14236do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14237{
14238 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14239}
14240
c906108c 14241void
fba45db2 14242delete_command (char *arg, int from_tty)
c906108c 14243{
35df4500 14244 struct breakpoint *b, *b_tmp;
c906108c 14245
ea9365bb
TT
14246 dont_repeat ();
14247
c906108c
SS
14248 if (arg == 0)
14249 {
14250 int breaks_to_delete = 0;
14251
46c6471b
PA
14252 /* Delete all breakpoints if no argument. Do not delete
14253 internal breakpoints, these have to be deleted with an
14254 explicit breakpoint number argument. */
c5aa993b 14255 ALL_BREAKPOINTS (b)
46c6471b 14256 if (user_breakpoint_p (b))
973d738b
DJ
14257 {
14258 breaks_to_delete = 1;
14259 break;
14260 }
c906108c
SS
14261
14262 /* Ask user only if there are some breakpoints to delete. */
14263 if (!from_tty
e2e0b3e5 14264 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14265 {
35df4500 14266 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14267 if (user_breakpoint_p (b))
c5aa993b 14268 delete_breakpoint (b);
c906108c
SS
14269 }
14270 }
14271 else
51be5b68 14272 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14273}
14274
0d381245
VP
14275static int
14276all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 14277{
0d381245 14278 for (; loc; loc = loc->next)
8645ff69
UW
14279 if (!loc->shlib_disabled
14280 && !loc->pspace->executing_startup)
0d381245
VP
14281 return 0;
14282 return 1;
fe3f5fa8
VP
14283}
14284
776592bf
DE
14285/* Subroutine of update_breakpoint_locations to simplify it.
14286 Return non-zero if multiple fns in list LOC have the same name.
14287 Null names are ignored. */
14288
14289static int
14290ambiguous_names_p (struct bp_location *loc)
14291{
14292 struct bp_location *l;
14293 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14294 (int (*) (const void *,
14295 const void *)) streq,
776592bf
DE
14296 NULL, xcalloc, xfree);
14297
14298 for (l = loc; l != NULL; l = l->next)
14299 {
14300 const char **slot;
14301 const char *name = l->function_name;
14302
14303 /* Allow for some names to be NULL, ignore them. */
14304 if (name == NULL)
14305 continue;
14306
14307 slot = (const char **) htab_find_slot (htab, (const void *) name,
14308 INSERT);
4a64f543
MS
14309 /* NOTE: We can assume slot != NULL here because xcalloc never
14310 returns NULL. */
776592bf
DE
14311 if (*slot != NULL)
14312 {
14313 htab_delete (htab);
14314 return 1;
14315 }
14316 *slot = name;
14317 }
14318
14319 htab_delete (htab);
14320 return 0;
14321}
14322
0fb4aa4b
PA
14323/* When symbols change, it probably means the sources changed as well,
14324 and it might mean the static tracepoint markers are no longer at
14325 the same address or line numbers they used to be at last we
14326 checked. Losing your static tracepoints whenever you rebuild is
14327 undesirable. This function tries to resync/rematch gdb static
14328 tracepoints with the markers on the target, for static tracepoints
14329 that have not been set by marker id. Static tracepoint that have
14330 been set by marker id are reset by marker id in breakpoint_re_set.
14331 The heuristic is:
14332
14333 1) For a tracepoint set at a specific address, look for a marker at
14334 the old PC. If one is found there, assume to be the same marker.
14335 If the name / string id of the marker found is different from the
14336 previous known name, assume that means the user renamed the marker
14337 in the sources, and output a warning.
14338
14339 2) For a tracepoint set at a given line number, look for a marker
14340 at the new address of the old line number. If one is found there,
14341 assume to be the same marker. If the name / string id of the
14342 marker found is different from the previous known name, assume that
14343 means the user renamed the marker in the sources, and output a
14344 warning.
14345
14346 3) If a marker is no longer found at the same address or line, it
14347 may mean the marker no longer exists. But it may also just mean
14348 the code changed a bit. Maybe the user added a few lines of code
14349 that made the marker move up or down (in line number terms). Ask
14350 the target for info about the marker with the string id as we knew
14351 it. If found, update line number and address in the matching
14352 static tracepoint. This will get confused if there's more than one
14353 marker with the same ID (possible in UST, although unadvised
14354 precisely because it confuses tools). */
14355
14356static struct symtab_and_line
14357update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14358{
d9b3f62e 14359 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14360 struct static_tracepoint_marker marker;
14361 CORE_ADDR pc;
0fb4aa4b
PA
14362
14363 pc = sal.pc;
14364 if (sal.line)
14365 find_line_pc (sal.symtab, sal.line, &pc);
14366
14367 if (target_static_tracepoint_marker_at (pc, &marker))
14368 {
d9b3f62e 14369 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14370 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14371 b->number,
d9b3f62e 14372 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14373
d9b3f62e
PA
14374 xfree (tp->static_trace_marker_id);
14375 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14376 release_static_tracepoint_marker (&marker);
14377
14378 return sal;
14379 }
14380
14381 /* Old marker wasn't found on target at lineno. Try looking it up
14382 by string ID. */
14383 if (!sal.explicit_pc
14384 && sal.line != 0
14385 && sal.symtab != NULL
d9b3f62e 14386 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14387 {
14388 VEC(static_tracepoint_marker_p) *markers;
14389
14390 markers
d9b3f62e 14391 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14392
14393 if (!VEC_empty(static_tracepoint_marker_p, markers))
14394 {
80e1d417 14395 struct symtab_and_line sal2;
0fb4aa4b 14396 struct symbol *sym;
80e1d417 14397 struct static_tracepoint_marker *tpmarker;
79a45e25 14398 struct ui_out *uiout = current_uiout;
0fb4aa4b 14399
80e1d417 14400 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14401
d9b3f62e 14402 xfree (tp->static_trace_marker_id);
80e1d417 14403 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14404
14405 warning (_("marker for static tracepoint %d (%s) not "
14406 "found at previous line number"),
d9b3f62e 14407 b->number, tp->static_trace_marker_id);
0fb4aa4b 14408
80e1d417 14409 init_sal (&sal2);
0fb4aa4b 14410
80e1d417 14411 sal2.pc = tpmarker->address;
0fb4aa4b 14412
80e1d417
AS
14413 sal2 = find_pc_line (tpmarker->address, 0);
14414 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14415 ui_out_text (uiout, "Now in ");
14416 if (sym)
14417 {
14418 ui_out_field_string (uiout, "func",
14419 SYMBOL_PRINT_NAME (sym));
14420 ui_out_text (uiout, " at ");
14421 }
05cba821
JK
14422 ui_out_field_string (uiout, "file",
14423 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14424 ui_out_text (uiout, ":");
14425
14426 if (ui_out_is_mi_like_p (uiout))
14427 {
0b0865da 14428 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14429
f35a17b5 14430 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14431 }
14432
80e1d417 14433 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14434 ui_out_text (uiout, "\n");
14435
80e1d417 14436 b->loc->line_number = sal2.line;
2f202fde 14437 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14438
14439 xfree (b->addr_string);
14440 b->addr_string = xstrprintf ("%s:%d",
05cba821 14441 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14442 b->loc->line_number);
0fb4aa4b
PA
14443
14444 /* Might be nice to check if function changed, and warn if
14445 so. */
14446
80e1d417 14447 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14448 }
14449 }
14450 return sal;
14451}
14452
8d3788bd
VP
14453/* Returns 1 iff locations A and B are sufficiently same that
14454 we don't need to report breakpoint as changed. */
14455
14456static int
14457locations_are_equal (struct bp_location *a, struct bp_location *b)
14458{
14459 while (a && b)
14460 {
14461 if (a->address != b->address)
14462 return 0;
14463
14464 if (a->shlib_disabled != b->shlib_disabled)
14465 return 0;
14466
14467 if (a->enabled != b->enabled)
14468 return 0;
14469
14470 a = a->next;
14471 b = b->next;
14472 }
14473
14474 if ((a == NULL) != (b == NULL))
14475 return 0;
14476
14477 return 1;
14478}
14479
f1310107
TJB
14480/* Create new breakpoint locations for B (a hardware or software breakpoint)
14481 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14482 a ranged breakpoint. */
14483
0e30163f 14484void
0d381245 14485update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14486 struct symtabs_and_lines sals,
14487 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14488{
14489 int i;
0d381245
VP
14490 struct bp_location *existing_locations = b->loc;
14491
f8eba3c6
TT
14492 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14493 {
14494 /* Ranged breakpoints have only one start location and one end
14495 location. */
14496 b->enable_state = bp_disabled;
44702360 14497 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14498 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14499 "multiple locations found\n"),
14500 b->number);
14501 return;
14502 }
f1310107 14503
4a64f543
MS
14504 /* If there's no new locations, and all existing locations are
14505 pending, don't do anything. This optimizes the common case where
14506 all locations are in the same shared library, that was unloaded.
14507 We'd like to retain the location, so that when the library is
14508 loaded again, we don't loose the enabled/disabled status of the
14509 individual locations. */
0d381245 14510 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14511 return;
14512
fe3f5fa8
VP
14513 b->loc = NULL;
14514
0d381245 14515 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14516 {
f8eba3c6
TT
14517 struct bp_location *new_loc;
14518
14519 switch_to_program_space_and_thread (sals.sals[i].pspace);
14520
14521 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14522
0d381245
VP
14523 /* Reparse conditions, they might contain references to the
14524 old symtab. */
14525 if (b->cond_string != NULL)
14526 {
bbc13ae3 14527 const char *s;
bfd189b1 14528 volatile struct gdb_exception e;
fe3f5fa8 14529
0d381245
VP
14530 s = b->cond_string;
14531 TRY_CATCH (e, RETURN_MASK_ERROR)
14532 {
1bb9788d
TT
14533 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14534 block_for_pc (sals.sals[i].pc),
0d381245
VP
14535 0);
14536 }
14537 if (e.reason < 0)
14538 {
3e43a32a
MS
14539 warning (_("failed to reevaluate condition "
14540 "for breakpoint %d: %s"),
0d381245
VP
14541 b->number, e.message);
14542 new_loc->enabled = 0;
14543 }
14544 }
fe3f5fa8 14545
f1310107
TJB
14546 if (sals_end.nelts)
14547 {
14548 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14549
14550 new_loc->length = end - sals.sals[0].pc + 1;
14551 }
0d381245 14552 }
fe3f5fa8 14553
514f746b
AR
14554 /* Update locations of permanent breakpoints. */
14555 if (b->enable_state == bp_permanent)
14556 make_breakpoint_permanent (b);
14557
4a64f543
MS
14558 /* If possible, carry over 'disable' status from existing
14559 breakpoints. */
0d381245
VP
14560 {
14561 struct bp_location *e = existing_locations;
776592bf
DE
14562 /* If there are multiple breakpoints with the same function name,
14563 e.g. for inline functions, comparing function names won't work.
14564 Instead compare pc addresses; this is just a heuristic as things
14565 may have moved, but in practice it gives the correct answer
14566 often enough until a better solution is found. */
14567 int have_ambiguous_names = ambiguous_names_p (b->loc);
14568
0d381245
VP
14569 for (; e; e = e->next)
14570 {
14571 if (!e->enabled && e->function_name)
14572 {
14573 struct bp_location *l = b->loc;
776592bf
DE
14574 if (have_ambiguous_names)
14575 {
14576 for (; l; l = l->next)
f1310107 14577 if (breakpoint_locations_match (e, l))
776592bf
DE
14578 {
14579 l->enabled = 0;
14580 break;
14581 }
14582 }
14583 else
14584 {
14585 for (; l; l = l->next)
14586 if (l->function_name
14587 && strcmp (e->function_name, l->function_name) == 0)
14588 {
14589 l->enabled = 0;
14590 break;
14591 }
14592 }
0d381245
VP
14593 }
14594 }
14595 }
fe3f5fa8 14596
8d3788bd
VP
14597 if (!locations_are_equal (existing_locations, b->loc))
14598 observer_notify_breakpoint_modified (b);
14599
44702360 14600 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14601}
14602
ef23e705
TJB
14603/* Find the SaL locations corresponding to the given ADDR_STRING.
14604 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14605
14606static struct symtabs_and_lines
14607addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14608{
14609 char *s;
02d20e4a 14610 struct symtabs_and_lines sals = {0};
f8eba3c6 14611 volatile struct gdb_exception e;
ef23e705 14612
983af33b 14613 gdb_assert (b->ops != NULL);
ef23e705 14614 s = addr_string;
ef23e705
TJB
14615
14616 TRY_CATCH (e, RETURN_MASK_ERROR)
14617 {
983af33b 14618 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14619 }
14620 if (e.reason < 0)
14621 {
14622 int not_found_and_ok = 0;
14623 /* For pending breakpoints, it's expected that parsing will
14624 fail until the right shared library is loaded. User has
14625 already told to create pending breakpoints and don't need
14626 extra messages. If breakpoint is in bp_shlib_disabled
14627 state, then user already saw the message about that
14628 breakpoint being disabled, and don't want to see more
14629 errors. */
58438ac1 14630 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14631 && (b->condition_not_parsed
14632 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14633 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14634 || b->enable_state == bp_disabled))
14635 not_found_and_ok = 1;
14636
14637 if (!not_found_and_ok)
14638 {
14639 /* We surely don't want to warn about the same breakpoint
14640 10 times. One solution, implemented here, is disable
14641 the breakpoint on error. Another solution would be to
14642 have separate 'warning emitted' flag. Since this
14643 happens only when a binary has changed, I don't know
14644 which approach is better. */
14645 b->enable_state = bp_disabled;
14646 throw_exception (e);
14647 }
14648 }
14649
58438ac1 14650 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14651 {
f8eba3c6 14652 int i;
ef23e705 14653
f8eba3c6
TT
14654 for (i = 0; i < sals.nelts; ++i)
14655 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14656 if (b->condition_not_parsed && s && s[0])
14657 {
ed1d1739
KS
14658 char *cond_string, *extra_string;
14659 int thread, task;
ef23e705
TJB
14660
14661 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14662 &cond_string, &thread, &task,
14663 &extra_string);
ef23e705
TJB
14664 if (cond_string)
14665 b->cond_string = cond_string;
14666 b->thread = thread;
14667 b->task = task;
e7e0cddf
SS
14668 if (extra_string)
14669 b->extra_string = extra_string;
ef23e705
TJB
14670 b->condition_not_parsed = 0;
14671 }
14672
983af33b 14673 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14674 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14675
58438ac1
TT
14676 *found = 1;
14677 }
14678 else
14679 *found = 0;
ef23e705
TJB
14680
14681 return sals;
14682}
14683
348d480f
PA
14684/* The default re_set method, for typical hardware or software
14685 breakpoints. Reevaluate the breakpoint and recreate its
14686 locations. */
14687
14688static void
28010a5d 14689breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14690{
14691 int found;
f1310107 14692 struct symtabs_and_lines sals, sals_end;
ef23e705 14693 struct symtabs_and_lines expanded = {0};
f1310107 14694 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14695
14696 sals = addr_string_to_sals (b, b->addr_string, &found);
14697 if (found)
14698 {
14699 make_cleanup (xfree, sals.sals);
f8eba3c6 14700 expanded = sals;
ef23e705
TJB
14701 }
14702
f1310107
TJB
14703 if (b->addr_string_range_end)
14704 {
14705 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14706 if (found)
14707 {
14708 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14709 expanded_end = sals_end;
f1310107
TJB
14710 }
14711 }
14712
14713 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14714}
14715
983af33b
SDJ
14716/* Default method for creating SALs from an address string. It basically
14717 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14718
14719static void
14720create_sals_from_address_default (char **arg,
14721 struct linespec_result *canonical,
14722 enum bptype type_wanted,
14723 char *addr_start, char **copy_arg)
14724{
14725 parse_breakpoint_sals (arg, canonical);
14726}
14727
14728/* Call create_breakpoints_sal for the given arguments. This is the default
14729 function for the `create_breakpoints_sal' method of
14730 breakpoint_ops. */
14731
14732static void
14733create_breakpoints_sal_default (struct gdbarch *gdbarch,
14734 struct linespec_result *canonical,
983af33b 14735 char *cond_string,
e7e0cddf 14736 char *extra_string,
983af33b
SDJ
14737 enum bptype type_wanted,
14738 enum bpdisp disposition,
14739 int thread,
14740 int task, int ignore_count,
14741 const struct breakpoint_ops *ops,
14742 int from_tty, int enabled,
44f238bb 14743 int internal, unsigned flags)
983af33b
SDJ
14744{
14745 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14746 extra_string,
983af33b
SDJ
14747 type_wanted, disposition,
14748 thread, task, ignore_count, ops, from_tty,
44f238bb 14749 enabled, internal, flags);
983af33b
SDJ
14750}
14751
14752/* Decode the line represented by S by calling decode_line_full. This is the
14753 default function for the `decode_linespec' method of breakpoint_ops. */
14754
14755static void
14756decode_linespec_default (struct breakpoint *b, char **s,
14757 struct symtabs_and_lines *sals)
14758{
14759 struct linespec_result canonical;
14760
14761 init_linespec_result (&canonical);
14762 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14763 (struct symtab *) NULL, 0,
14764 &canonical, multiple_symbols_all,
14765 b->filter);
14766
14767 /* We should get 0 or 1 resulting SALs. */
14768 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14769
14770 if (VEC_length (linespec_sals, canonical.sals) > 0)
14771 {
14772 struct linespec_sals *lsal;
14773
14774 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14775 *sals = lsal->sals;
14776 /* Arrange it so the destructor does not free the
14777 contents. */
14778 lsal->sals.sals = NULL;
14779 }
14780
14781 destroy_linespec_result (&canonical);
14782}
14783
28010a5d
PA
14784/* Prepare the global context for a re-set of breakpoint B. */
14785
14786static struct cleanup *
14787prepare_re_set_context (struct breakpoint *b)
14788{
14789 struct cleanup *cleanups;
14790
14791 input_radix = b->input_radix;
14792 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14793 if (b->pspace != NULL)
14794 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14795 set_language (b->language);
14796
14797 return cleanups;
ef23e705
TJB
14798}
14799
c906108c
SS
14800/* Reset a breakpoint given it's struct breakpoint * BINT.
14801 The value we return ends up being the return value from catch_errors.
14802 Unused in this case. */
14803
14804static int
4efb68b1 14805breakpoint_re_set_one (void *bint)
c906108c 14806{
4a64f543 14807 /* Get past catch_errs. */
53a5351d 14808 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14809 struct cleanup *cleanups;
c906108c 14810
348d480f
PA
14811 cleanups = prepare_re_set_context (b);
14812 b->ops->re_set (b);
14813 do_cleanups (cleanups);
c906108c
SS
14814 return 0;
14815}
14816
69de3c6a 14817/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14818void
69de3c6a 14819breakpoint_re_set (void)
c906108c 14820{
35df4500 14821 struct breakpoint *b, *b_tmp;
c906108c
SS
14822 enum language save_language;
14823 int save_input_radix;
6c95b8df 14824 struct cleanup *old_chain;
c5aa993b 14825
c906108c
SS
14826 save_language = current_language->la_language;
14827 save_input_radix = input_radix;
6c95b8df
PA
14828 old_chain = save_current_program_space ();
14829
35df4500 14830 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14831 {
4a64f543 14832 /* Format possible error msg. */
fe3f5fa8 14833 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14834 b->number);
14835 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14836 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14837 do_cleanups (cleanups);
c5aa993b 14838 }
c906108c
SS
14839 set_language (save_language);
14840 input_radix = save_input_radix;
e62c965a 14841
0756c555 14842 jit_breakpoint_re_set ();
4efc6507 14843
6c95b8df
PA
14844 do_cleanups (old_chain);
14845
af02033e
PP
14846 create_overlay_event_breakpoint ();
14847 create_longjmp_master_breakpoint ();
14848 create_std_terminate_master_breakpoint ();
186c406b 14849 create_exception_master_breakpoint ();
c906108c
SS
14850}
14851\f
c906108c
SS
14852/* Reset the thread number of this breakpoint:
14853
14854 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14855 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14856void
fba45db2 14857breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14858{
14859 if (b->thread != -1)
14860 {
39f77062
KB
14861 if (in_thread_list (inferior_ptid))
14862 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14863
14864 /* We're being called after following a fork. The new fork is
14865 selected as current, and unless this was a vfork will have a
14866 different program space from the original thread. Reset that
14867 as well. */
14868 b->loc->pspace = current_program_space;
c906108c
SS
14869 }
14870}
14871
03ac34d5
MS
14872/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14873 If from_tty is nonzero, it prints a message to that effect,
14874 which ends with a period (no newline). */
14875
c906108c 14876void
fba45db2 14877set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14878{
52f0bd74 14879 struct breakpoint *b;
c906108c
SS
14880
14881 if (count < 0)
14882 count = 0;
14883
14884 ALL_BREAKPOINTS (b)
14885 if (b->number == bptnum)
c5aa993b 14886 {
d77f58be
SS
14887 if (is_tracepoint (b))
14888 {
14889 if (from_tty && count != 0)
14890 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14891 bptnum);
14892 return;
14893 }
14894
c5aa993b 14895 b->ignore_count = count;
221ea385
KS
14896 if (from_tty)
14897 {
14898 if (count == 0)
3e43a32a
MS
14899 printf_filtered (_("Will stop next time "
14900 "breakpoint %d is reached."),
221ea385
KS
14901 bptnum);
14902 else if (count == 1)
a3f17187 14903 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14904 bptnum);
14905 else
3e43a32a
MS
14906 printf_filtered (_("Will ignore next %d "
14907 "crossings of breakpoint %d."),
221ea385
KS
14908 count, bptnum);
14909 }
8d3788bd 14910 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14911 return;
14912 }
c906108c 14913
8a3fe4f8 14914 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14915}
14916
c906108c
SS
14917/* Command to set ignore-count of breakpoint N to COUNT. */
14918
14919static void
fba45db2 14920ignore_command (char *args, int from_tty)
c906108c
SS
14921{
14922 char *p = args;
52f0bd74 14923 int num;
c906108c
SS
14924
14925 if (p == 0)
e2e0b3e5 14926 error_no_arg (_("a breakpoint number"));
c5aa993b 14927
c906108c 14928 num = get_number (&p);
5c44784c 14929 if (num == 0)
8a3fe4f8 14930 error (_("bad breakpoint number: '%s'"), args);
c906108c 14931 if (*p == 0)
8a3fe4f8 14932 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14933
14934 set_ignore_count (num,
14935 longest_to_int (value_as_long (parse_and_eval (p))),
14936 from_tty);
221ea385
KS
14937 if (from_tty)
14938 printf_filtered ("\n");
c906108c
SS
14939}
14940\f
14941/* Call FUNCTION on each of the breakpoints
14942 whose numbers are given in ARGS. */
14943
14944static void
95a42b64
TT
14945map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14946 void *),
14947 void *data)
c906108c 14948{
52f0bd74
AC
14949 int num;
14950 struct breakpoint *b, *tmp;
11cf8741 14951 int match;
197f0a60 14952 struct get_number_or_range_state state;
c906108c 14953
197f0a60 14954 if (args == 0)
e2e0b3e5 14955 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14956
197f0a60
TT
14957 init_number_or_range (&state, args);
14958
14959 while (!state.finished)
c906108c 14960 {
e799154c 14961 const char *p = state.string;
197f0a60 14962
11cf8741 14963 match = 0;
c5aa993b 14964
197f0a60 14965 num = get_number_or_range (&state);
5c44784c 14966 if (num == 0)
c5aa993b 14967 {
8a3fe4f8 14968 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14969 }
14970 else
14971 {
14972 ALL_BREAKPOINTS_SAFE (b, tmp)
14973 if (b->number == num)
14974 {
11cf8741 14975 match = 1;
cdac0397 14976 function (b, data);
11cf8741 14977 break;
5c44784c 14978 }
11cf8741 14979 if (match == 0)
a3f17187 14980 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14981 }
c906108c
SS
14982 }
14983}
14984
0d381245
VP
14985static struct bp_location *
14986find_location_by_number (char *number)
14987{
14988 char *dot = strchr (number, '.');
14989 char *p1;
14990 int bp_num;
14991 int loc_num;
14992 struct breakpoint *b;
14993 struct bp_location *loc;
14994
14995 *dot = '\0';
14996
14997 p1 = number;
197f0a60 14998 bp_num = get_number (&p1);
0d381245
VP
14999 if (bp_num == 0)
15000 error (_("Bad breakpoint number '%s'"), number);
15001
15002 ALL_BREAKPOINTS (b)
15003 if (b->number == bp_num)
15004 {
15005 break;
15006 }
15007
15008 if (!b || b->number != bp_num)
15009 error (_("Bad breakpoint number '%s'"), number);
15010
15011 p1 = dot+1;
197f0a60 15012 loc_num = get_number (&p1);
0d381245
VP
15013 if (loc_num == 0)
15014 error (_("Bad breakpoint location number '%s'"), number);
15015
15016 --loc_num;
15017 loc = b->loc;
15018 for (;loc_num && loc; --loc_num, loc = loc->next)
15019 ;
15020 if (!loc)
15021 error (_("Bad breakpoint location number '%s'"), dot+1);
15022
15023 return loc;
15024}
15025
15026
1900040c
MS
15027/* Set ignore-count of breakpoint number BPTNUM to COUNT.
15028 If from_tty is nonzero, it prints a message to that effect,
15029 which ends with a period (no newline). */
15030
c906108c 15031void
fba45db2 15032disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
15033{
15034 /* Never disable a watchpoint scope breakpoint; we want to
15035 hit them when we leave scope so we can delete both the
15036 watchpoint and its scope breakpoint at that time. */
15037 if (bpt->type == bp_watchpoint_scope)
15038 return;
15039
c2c6d25f 15040 /* You can't disable permanent breakpoints. */
b5de0fa7 15041 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
15042 return;
15043
b5de0fa7 15044 bpt->enable_state = bp_disabled;
c906108c 15045
b775012e
LM
15046 /* Mark breakpoint locations modified. */
15047 mark_breakpoint_modified (bpt);
15048
d248b706
KY
15049 if (target_supports_enable_disable_tracepoint ()
15050 && current_trace_status ()->running && is_tracepoint (bpt))
15051 {
15052 struct bp_location *location;
15053
15054 for (location = bpt->loc; location; location = location->next)
15055 target_disable_tracepoint (location);
15056 }
15057
44702360 15058 update_global_location_list (UGLL_DONT_INSERT);
c906108c 15059
8d3788bd 15060 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15061}
15062
51be5b68
PA
15063/* A callback for iterate_over_related_breakpoints. */
15064
15065static void
15066do_disable_breakpoint (struct breakpoint *b, void *ignore)
15067{
15068 disable_breakpoint (b);
15069}
15070
95a42b64
TT
15071/* A callback for map_breakpoint_numbers that calls
15072 disable_breakpoint. */
15073
15074static void
15075do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
15076{
51be5b68 15077 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
15078}
15079
c906108c 15080static void
fba45db2 15081disable_command (char *args, int from_tty)
c906108c 15082{
c906108c 15083 if (args == 0)
46c6471b
PA
15084 {
15085 struct breakpoint *bpt;
15086
15087 ALL_BREAKPOINTS (bpt)
15088 if (user_breakpoint_p (bpt))
15089 disable_breakpoint (bpt);
15090 }
9eaabc75 15091 else
0d381245 15092 {
9eaabc75
MW
15093 char *num = extract_arg (&args);
15094
15095 while (num)
d248b706 15096 {
9eaabc75 15097 if (strchr (num, '.'))
b775012e 15098 {
9eaabc75
MW
15099 struct bp_location *loc = find_location_by_number (num);
15100
15101 if (loc)
15102 {
15103 if (loc->enabled)
15104 {
15105 loc->enabled = 0;
15106 mark_breakpoint_location_modified (loc);
15107 }
15108 if (target_supports_enable_disable_tracepoint ()
15109 && current_trace_status ()->running && loc->owner
15110 && is_tracepoint (loc->owner))
15111 target_disable_tracepoint (loc);
15112 }
44702360 15113 update_global_location_list (UGLL_DONT_INSERT);
b775012e 15114 }
9eaabc75
MW
15115 else
15116 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
15117 num = extract_arg (&args);
d248b706 15118 }
0d381245 15119 }
c906108c
SS
15120}
15121
15122static void
816338b5
SS
15123enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
15124 int count)
c906108c 15125{
afe38095 15126 int target_resources_ok;
c906108c
SS
15127
15128 if (bpt->type == bp_hardware_breakpoint)
15129 {
15130 int i;
c5aa993b 15131 i = hw_breakpoint_used_count ();
53a5351d 15132 target_resources_ok =
d92524f1 15133 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 15134 i + 1, 0);
c906108c 15135 if (target_resources_ok == 0)
8a3fe4f8 15136 error (_("No hardware breakpoint support in the target."));
c906108c 15137 else if (target_resources_ok < 0)
8a3fe4f8 15138 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
15139 }
15140
cc60f2e3 15141 if (is_watchpoint (bpt))
c906108c 15142 {
d07205c2
JK
15143 /* Initialize it just to avoid a GCC false warning. */
15144 enum enable_state orig_enable_state = 0;
bfd189b1 15145 volatile struct gdb_exception e;
dde02812
ES
15146
15147 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 15148 {
3a5c3e22
PA
15149 struct watchpoint *w = (struct watchpoint *) bpt;
15150
1e718ff1
TJB
15151 orig_enable_state = bpt->enable_state;
15152 bpt->enable_state = bp_enabled;
3a5c3e22 15153 update_watchpoint (w, 1 /* reparse */);
c906108c 15154 }
dde02812 15155 if (e.reason < 0)
c5aa993b 15156 {
1e718ff1 15157 bpt->enable_state = orig_enable_state;
dde02812
ES
15158 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
15159 bpt->number);
15160 return;
c5aa993b 15161 }
c906108c 15162 }
0101ce28 15163
b4c291bb
KH
15164 if (bpt->enable_state != bp_permanent)
15165 bpt->enable_state = bp_enabled;
d248b706 15166
b775012e
LM
15167 bpt->enable_state = bp_enabled;
15168
15169 /* Mark breakpoint locations modified. */
15170 mark_breakpoint_modified (bpt);
15171
d248b706
KY
15172 if (target_supports_enable_disable_tracepoint ()
15173 && current_trace_status ()->running && is_tracepoint (bpt))
15174 {
15175 struct bp_location *location;
15176
15177 for (location = bpt->loc; location; location = location->next)
15178 target_enable_tracepoint (location);
15179 }
15180
b4c291bb 15181 bpt->disposition = disposition;
816338b5 15182 bpt->enable_count = count;
44702360 15183 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 15184
8d3788bd 15185 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15186}
15187
fe3f5fa8 15188
c906108c 15189void
fba45db2 15190enable_breakpoint (struct breakpoint *bpt)
c906108c 15191{
816338b5 15192 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
15193}
15194
15195static void
15196do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15197{
15198 enable_breakpoint (bpt);
c906108c
SS
15199}
15200
95a42b64
TT
15201/* A callback for map_breakpoint_numbers that calls
15202 enable_breakpoint. */
15203
15204static void
15205do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15206{
51be5b68 15207 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15208}
15209
c906108c
SS
15210/* The enable command enables the specified breakpoints (or all defined
15211 breakpoints) so they once again become (or continue to be) effective
1272ad14 15212 in stopping the inferior. */
c906108c 15213
c906108c 15214static void
fba45db2 15215enable_command (char *args, int from_tty)
c906108c 15216{
c906108c 15217 if (args == 0)
46c6471b
PA
15218 {
15219 struct breakpoint *bpt;
15220
15221 ALL_BREAKPOINTS (bpt)
15222 if (user_breakpoint_p (bpt))
15223 enable_breakpoint (bpt);
15224 }
9eaabc75 15225 else
0d381245 15226 {
9eaabc75
MW
15227 char *num = extract_arg (&args);
15228
15229 while (num)
d248b706 15230 {
9eaabc75 15231 if (strchr (num, '.'))
b775012e 15232 {
9eaabc75
MW
15233 struct bp_location *loc = find_location_by_number (num);
15234
15235 if (loc)
15236 {
15237 if (!loc->enabled)
15238 {
15239 loc->enabled = 1;
15240 mark_breakpoint_location_modified (loc);
15241 }
15242 if (target_supports_enable_disable_tracepoint ()
15243 && current_trace_status ()->running && loc->owner
15244 && is_tracepoint (loc->owner))
15245 target_enable_tracepoint (loc);
15246 }
44702360 15247 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15248 }
9eaabc75
MW
15249 else
15250 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15251 num = extract_arg (&args);
d248b706 15252 }
0d381245 15253 }
c906108c
SS
15254}
15255
816338b5
SS
15256/* This struct packages up disposition data for application to multiple
15257 breakpoints. */
15258
15259struct disp_data
15260{
15261 enum bpdisp disp;
15262 int count;
15263};
15264
c906108c 15265static void
51be5b68
PA
15266do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15267{
816338b5 15268 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15269
816338b5 15270 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15271}
15272
15273static void
15274do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15275{
816338b5 15276 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15277
15278 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15279}
15280
c906108c 15281static void
fba45db2 15282enable_once_command (char *args, int from_tty)
c906108c 15283{
51be5b68 15284 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15285}
15286
816338b5
SS
15287static void
15288do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15289{
15290 struct disp_data disp = { disp_disable, *(int *) countptr };
15291
15292 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15293}
15294
15295static void
15296enable_count_command (char *args, int from_tty)
15297{
15298 int count = get_number (&args);
15299
15300 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15301}
15302
c906108c 15303static void
51be5b68 15304do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15305{
816338b5 15306 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15307
15308 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15309}
15310
c906108c 15311static void
fba45db2 15312enable_delete_command (char *args, int from_tty)
c906108c 15313{
51be5b68 15314 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15315}
15316\f
fa8d40ab
JJ
15317static void
15318set_breakpoint_cmd (char *args, int from_tty)
15319{
15320}
15321
15322static void
15323show_breakpoint_cmd (char *args, int from_tty)
15324{
15325}
15326
1f3b5d1b
PP
15327/* Invalidate last known value of any hardware watchpoint if
15328 the memory which that value represents has been written to by
15329 GDB itself. */
15330
15331static void
8de0566d
YQ
15332invalidate_bp_value_on_memory_change (struct inferior *inferior,
15333 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15334 const bfd_byte *data)
15335{
15336 struct breakpoint *bp;
15337
15338 ALL_BREAKPOINTS (bp)
15339 if (bp->enable_state == bp_enabled
3a5c3e22 15340 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15341 {
3a5c3e22 15342 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15343
3a5c3e22
PA
15344 if (wp->val_valid && wp->val)
15345 {
15346 struct bp_location *loc;
15347
15348 for (loc = bp->loc; loc != NULL; loc = loc->next)
15349 if (loc->loc_type == bp_loc_hardware_watchpoint
15350 && loc->address + loc->length > addr
15351 && addr + len > loc->address)
15352 {
15353 value_free (wp->val);
15354 wp->val = NULL;
15355 wp->val_valid = 0;
15356 }
15357 }
1f3b5d1b
PP
15358 }
15359}
15360
8181d85f
DJ
15361/* Create and insert a raw software breakpoint at PC. Return an
15362 identifier, which should be used to remove the breakpoint later.
15363 In general, places which call this should be using something on the
15364 breakpoint chain instead; this function should be eliminated
15365 someday. */
15366
15367void *
6c95b8df
PA
15368deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
15369 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
15370{
15371 struct bp_target_info *bp_tgt;
ef370185 15372 struct bp_location *bl;
8181d85f 15373
41bf6aca 15374 bp_tgt = XCNEW (struct bp_target_info);
8181d85f 15375
6c95b8df 15376 bp_tgt->placed_address_space = aspace;
0d5ed153 15377 bp_tgt->reqstd_address = pc;
6c95b8df 15378
ef370185
JB
15379 /* If an unconditional non-raw breakpoint is already inserted at
15380 that location, there's no need to insert another. However, with
15381 target-side evaluation of breakpoint conditions, if the
15382 breakpoint that is currently inserted on the target is
15383 conditional, we need to make it unconditional. Note that a
15384 breakpoint with target-side commands is not reported even if
15385 unconditional, so we need to remove the commands from the target
15386 as well. */
15387 bl = find_non_raw_software_breakpoint_inserted_here (aspace, pc);
15388 if (bl != NULL
15389 && VEC_empty (agent_expr_p, bl->target_info.conditions)
15390 && VEC_empty (agent_expr_p, bl->target_info.tcommands))
15391 {
15392 bp_target_info_copy_insertion_state (bp_tgt, &bl->target_info);
15393 return bp_tgt;
15394 }
15395
a6d9a66e 15396 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
15397 {
15398 /* Could not insert the breakpoint. */
15399 xfree (bp_tgt);
15400 return NULL;
15401 }
15402
15403 return bp_tgt;
15404}
15405
4a64f543
MS
15406/* Remove a breakpoint BP inserted by
15407 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
15408
15409int
a6d9a66e 15410deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
15411{
15412 struct bp_target_info *bp_tgt = bp;
ef370185 15413 struct address_space *aspace = bp_tgt->placed_address_space;
0d5ed153 15414 CORE_ADDR address = bp_tgt->reqstd_address;
ef370185 15415 struct bp_location *bl;
8181d85f
DJ
15416 int ret;
15417
ef370185
JB
15418 bl = find_non_raw_software_breakpoint_inserted_here (aspace, address);
15419
15420 /* Only remove the raw breakpoint if there are no other non-raw
15421 breakpoints still inserted at this location. Otherwise, we would
15422 be effectively disabling those breakpoints. */
15423 if (bl == NULL)
15424 ret = target_remove_breakpoint (gdbarch, bp_tgt);
15425 else if (!VEC_empty (agent_expr_p, bl->target_info.conditions)
15426 || !VEC_empty (agent_expr_p, bl->target_info.tcommands))
15427 {
15428 /* The target is evaluating conditions, and when we inserted the
15429 software single-step breakpoint, we had made the breakpoint
15430 unconditional and command-less on the target side. Reinsert
15431 to restore the conditions/commands. */
15432 ret = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
15433 }
15434 else
15435 ret = 0;
15436
8181d85f
DJ
15437 xfree (bp_tgt);
15438
15439 return ret;
15440}
15441
8181d85f
DJ
15442/* Create and insert a breakpoint for software single step. */
15443
15444void
6c95b8df 15445insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15446 struct address_space *aspace,
15447 CORE_ADDR next_pc)
8181d85f 15448{
7c16b83e
PA
15449 struct thread_info *tp = inferior_thread ();
15450 struct symtab_and_line sal;
15451 CORE_ADDR pc = next_pc;
8181d85f 15452
7c16b83e
PA
15453 if (single_step_breakpoints == NULL)
15454 single_step_breakpoints = new_single_step_breakpoint (tp->num, gdbarch);
8181d85f 15455
7c16b83e
PA
15456 sal = find_pc_line (pc, 0);
15457 sal.pc = pc;
15458 sal.section = find_pc_overlay (pc);
15459 sal.explicit_pc = 1;
15460 add_location_to_breakpoint (single_step_breakpoints, &sal);
8181d85f 15461
7c16b83e 15462 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15463}
15464
f02253f1
HZ
15465/* Check if the breakpoints used for software single stepping
15466 were inserted or not. */
15467
15468int
15469single_step_breakpoints_inserted (void)
15470{
7c16b83e 15471 return (single_step_breakpoints != NULL);
f02253f1
HZ
15472}
15473
8181d85f
DJ
15474/* Remove and delete any breakpoints used for software single step. */
15475
15476void
15477remove_single_step_breakpoints (void)
15478{
7c16b83e 15479 gdb_assert (single_step_breakpoints != NULL);
8181d85f 15480
7c16b83e 15481 delete_breakpoint (single_step_breakpoints);
8181d85f 15482
7c16b83e 15483 single_step_breakpoints = NULL;
8181d85f
DJ
15484}
15485
d03285ec
UW
15486/* Delete software single step breakpoints without removing them from
15487 the inferior. This is intended to be used if the inferior's address
15488 space where they were inserted is already gone, e.g. after exit or
15489 exec. */
15490
15491void
15492cancel_single_step_breakpoints (void)
15493{
7c16b83e
PA
15494 /* We don't really need to (or should) delete them here. After an
15495 exit, breakpoint_init_inferior deletes it. After an exec,
15496 update_breakpoints_after_exec does it. Just clear our
15497 reference. */
15498 single_step_breakpoints = NULL;
d03285ec
UW
15499}
15500
7c16b83e 15501/* Check whether any location of BP is inserted at PC. */
1aafd4da 15502
ef370185 15503static int
7c16b83e
PA
15504breakpoint_has_location_inserted_here (struct breakpoint *bp,
15505 struct address_space *aspace,
15506 CORE_ADDR pc)
1aafd4da 15507{
7c16b83e 15508 struct bp_location *loc;
1aafd4da 15509
7c16b83e
PA
15510 for (loc = bp->loc; loc != NULL; loc = loc->next)
15511 if (loc->inserted
15512 && breakpoint_location_address_match (loc, aspace, pc))
15513 return 1;
1aafd4da 15514
7c16b83e 15515 return 0;
ef370185
JB
15516}
15517
15518/* Check whether a software single-step breakpoint is inserted at
15519 PC. */
15520
15521int
15522single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15523 CORE_ADDR pc)
15524{
7c16b83e
PA
15525 return (single_step_breakpoints != NULL
15526 && breakpoint_has_location_inserted_here (single_step_breakpoints,
15527 aspace, pc));
1aafd4da
UW
15528}
15529
a96d9b2e
SDJ
15530/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15531 non-zero otherwise. */
15532static int
15533is_syscall_catchpoint_enabled (struct breakpoint *bp)
15534{
15535 if (syscall_catchpoint_p (bp)
15536 && bp->enable_state != bp_disabled
15537 && bp->enable_state != bp_call_disabled)
15538 return 1;
15539 else
15540 return 0;
15541}
15542
15543int
15544catch_syscall_enabled (void)
15545{
fa3064dd
YQ
15546 struct catch_syscall_inferior_data *inf_data
15547 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15548
fa3064dd 15549 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15550}
15551
15552int
15553catching_syscall_number (int syscall_number)
15554{
15555 struct breakpoint *bp;
15556
15557 ALL_BREAKPOINTS (bp)
15558 if (is_syscall_catchpoint_enabled (bp))
15559 {
be5c67c1
PA
15560 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15561
15562 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15563 {
15564 int i, iter;
15565 for (i = 0;
be5c67c1 15566 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15567 i++)
15568 if (syscall_number == iter)
15569 return 1;
15570 }
15571 else
15572 return 1;
15573 }
15574
15575 return 0;
15576}
15577
15578/* Complete syscall names. Used by "catch syscall". */
49c4e619 15579static VEC (char_ptr) *
a96d9b2e 15580catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15581 const char *text, const char *word)
a96d9b2e
SDJ
15582{
15583 const char **list = get_syscall_names ();
49c4e619 15584 VEC (char_ptr) *retlist
b45627a0 15585 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15586
c38eea1a
MS
15587 xfree (list);
15588 return retlist;
a96d9b2e
SDJ
15589}
15590
1042e4c0
SS
15591/* Tracepoint-specific operations. */
15592
15593/* Set tracepoint count to NUM. */
15594static void
15595set_tracepoint_count (int num)
15596{
15597 tracepoint_count = num;
4fa62494 15598 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15599}
15600
70221824 15601static void
1042e4c0
SS
15602trace_command (char *arg, int from_tty)
15603{
55aa24fb
SDJ
15604 struct breakpoint_ops *ops;
15605 const char *arg_cp = arg;
15606
15607 if (arg && probe_linespec_to_ops (&arg_cp))
15608 ops = &tracepoint_probe_breakpoint_ops;
15609 else
15610 ops = &tracepoint_breakpoint_ops;
15611
558a9d82
YQ
15612 create_breakpoint (get_current_arch (),
15613 arg,
15614 NULL, 0, NULL, 1 /* parse arg */,
15615 0 /* tempflag */,
15616 bp_tracepoint /* type_wanted */,
15617 0 /* Ignore count */,
15618 pending_break_support,
15619 ops,
15620 from_tty,
15621 1 /* enabled */,
15622 0 /* internal */, 0);
1042e4c0
SS
15623}
15624
70221824 15625static void
7a697b8d
SS
15626ftrace_command (char *arg, int from_tty)
15627{
558a9d82
YQ
15628 create_breakpoint (get_current_arch (),
15629 arg,
15630 NULL, 0, NULL, 1 /* parse arg */,
15631 0 /* tempflag */,
15632 bp_fast_tracepoint /* type_wanted */,
15633 0 /* Ignore count */,
15634 pending_break_support,
15635 &tracepoint_breakpoint_ops,
15636 from_tty,
15637 1 /* enabled */,
15638 0 /* internal */, 0);
0fb4aa4b
PA
15639}
15640
15641/* strace command implementation. Creates a static tracepoint. */
15642
70221824 15643static void
0fb4aa4b
PA
15644strace_command (char *arg, int from_tty)
15645{
983af33b
SDJ
15646 struct breakpoint_ops *ops;
15647
15648 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15649 or with a normal static tracepoint. */
15650 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15651 ops = &strace_marker_breakpoint_ops;
15652 else
15653 ops = &tracepoint_breakpoint_ops;
15654
558a9d82
YQ
15655 create_breakpoint (get_current_arch (),
15656 arg,
15657 NULL, 0, NULL, 1 /* parse arg */,
15658 0 /* tempflag */,
15659 bp_static_tracepoint /* type_wanted */,
15660 0 /* Ignore count */,
15661 pending_break_support,
15662 ops,
15663 from_tty,
15664 1 /* enabled */,
15665 0 /* internal */, 0);
7a697b8d
SS
15666}
15667
409873ef
SS
15668/* Set up a fake reader function that gets command lines from a linked
15669 list that was acquired during tracepoint uploading. */
15670
15671static struct uploaded_tp *this_utp;
3149d8c1 15672static int next_cmd;
409873ef
SS
15673
15674static char *
15675read_uploaded_action (void)
15676{
15677 char *rslt;
15678
3149d8c1 15679 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15680
3149d8c1 15681 next_cmd++;
409873ef
SS
15682
15683 return rslt;
15684}
15685
00bf0b85
SS
15686/* Given information about a tracepoint as recorded on a target (which
15687 can be either a live system or a trace file), attempt to create an
15688 equivalent GDB tracepoint. This is not a reliable process, since
15689 the target does not necessarily have all the information used when
15690 the tracepoint was originally defined. */
15691
d9b3f62e 15692struct tracepoint *
00bf0b85 15693create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15694{
409873ef 15695 char *addr_str, small_buf[100];
d9b3f62e 15696 struct tracepoint *tp;
fd9b8c24 15697
409873ef
SS
15698 if (utp->at_string)
15699 addr_str = utp->at_string;
15700 else
15701 {
15702 /* In the absence of a source location, fall back to raw
15703 address. Since there is no way to confirm that the address
15704 means the same thing as when the trace was started, warn the
15705 user. */
3e43a32a
MS
15706 warning (_("Uploaded tracepoint %d has no "
15707 "source location, using raw address"),
409873ef 15708 utp->number);
8c042590 15709 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15710 addr_str = small_buf;
15711 }
15712
15713 /* There's not much we can do with a sequence of bytecodes. */
15714 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15715 warning (_("Uploaded tracepoint %d condition "
15716 "has no source form, ignoring it"),
409873ef 15717 utp->number);
d5551862 15718
8cdf0e15 15719 if (!create_breakpoint (get_current_arch (),
409873ef 15720 addr_str,
e7e0cddf
SS
15721 utp->cond_string, -1, NULL,
15722 0 /* parse cond/thread */,
8cdf0e15 15723 0 /* tempflag */,
0fb4aa4b 15724 utp->type /* type_wanted */,
8cdf0e15
VP
15725 0 /* Ignore count */,
15726 pending_break_support,
348d480f 15727 &tracepoint_breakpoint_ops,
8cdf0e15 15728 0 /* from_tty */,
84f4c1fe 15729 utp->enabled /* enabled */,
44f238bb
PA
15730 0 /* internal */,
15731 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15732 return NULL;
15733
409873ef 15734 /* Get the tracepoint we just created. */
fd9b8c24
PA
15735 tp = get_tracepoint (tracepoint_count);
15736 gdb_assert (tp != NULL);
d5551862 15737
00bf0b85
SS
15738 if (utp->pass > 0)
15739 {
8c042590
PM
15740 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15741 tp->base.number);
00bf0b85 15742
409873ef 15743 trace_pass_command (small_buf, 0);
00bf0b85
SS
15744 }
15745
409873ef
SS
15746 /* If we have uploaded versions of the original commands, set up a
15747 special-purpose "reader" function and call the usual command line
15748 reader, then pass the result to the breakpoint command-setting
15749 function. */
3149d8c1 15750 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15751 {
409873ef 15752 struct command_line *cmd_list;
00bf0b85 15753
409873ef 15754 this_utp = utp;
3149d8c1 15755 next_cmd = 0;
d5551862 15756
409873ef
SS
15757 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15758
d9b3f62e 15759 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15760 }
3149d8c1
SS
15761 else if (!VEC_empty (char_ptr, utp->actions)
15762 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15763 warning (_("Uploaded tracepoint %d actions "
15764 "have no source form, ignoring them"),
409873ef 15765 utp->number);
00bf0b85 15766
f196051f
SS
15767 /* Copy any status information that might be available. */
15768 tp->base.hit_count = utp->hit_count;
15769 tp->traceframe_usage = utp->traceframe_usage;
15770
00bf0b85 15771 return tp;
d9b3f62e 15772}
00bf0b85 15773
1042e4c0
SS
15774/* Print information on tracepoint number TPNUM_EXP, or all if
15775 omitted. */
15776
15777static void
e5a67952 15778tracepoints_info (char *args, int from_tty)
1042e4c0 15779{
79a45e25 15780 struct ui_out *uiout = current_uiout;
e5a67952 15781 int num_printed;
1042e4c0 15782
e5a67952 15783 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15784
15785 if (num_printed == 0)
1042e4c0 15786 {
e5a67952 15787 if (args == NULL || *args == '\0')
d77f58be
SS
15788 ui_out_message (uiout, 0, "No tracepoints.\n");
15789 else
e5a67952 15790 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15791 }
ad443146
SS
15792
15793 default_collect_info ();
1042e4c0
SS
15794}
15795
4a64f543 15796/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15797 Not supported by all targets. */
15798static void
15799enable_trace_command (char *args, int from_tty)
15800{
15801 enable_command (args, from_tty);
15802}
15803
4a64f543 15804/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15805 Not supported by all targets. */
15806static void
15807disable_trace_command (char *args, int from_tty)
15808{
15809 disable_command (args, from_tty);
15810}
15811
4a64f543 15812/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15813static void
15814delete_trace_command (char *arg, int from_tty)
15815{
35df4500 15816 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15817
15818 dont_repeat ();
15819
15820 if (arg == 0)
15821 {
15822 int breaks_to_delete = 0;
15823
15824 /* Delete all breakpoints if no argument.
15825 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15826 have to be deleted with an explicit breakpoint number
15827 argument. */
1042e4c0 15828 ALL_TRACEPOINTS (b)
46c6471b 15829 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15830 {
15831 breaks_to_delete = 1;
15832 break;
15833 }
1042e4c0
SS
15834
15835 /* Ask user only if there are some breakpoints to delete. */
15836 if (!from_tty
15837 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15838 {
35df4500 15839 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15840 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15841 delete_breakpoint (b);
1042e4c0
SS
15842 }
15843 }
15844 else
51be5b68 15845 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15846}
15847
197f0a60
TT
15848/* Helper function for trace_pass_command. */
15849
15850static void
d9b3f62e 15851trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15852{
d9b3f62e 15853 tp->pass_count = count;
6f6484cd 15854 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15855 if (from_tty)
15856 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15857 tp->base.number, count);
197f0a60
TT
15858}
15859
1042e4c0
SS
15860/* Set passcount for tracepoint.
15861
15862 First command argument is passcount, second is tracepoint number.
15863 If tracepoint number omitted, apply to most recently defined.
15864 Also accepts special argument "all". */
15865
15866static void
15867trace_pass_command (char *args, int from_tty)
15868{
d9b3f62e 15869 struct tracepoint *t1;
1042e4c0 15870 unsigned int count;
1042e4c0
SS
15871
15872 if (args == 0 || *args == 0)
3e43a32a
MS
15873 error (_("passcount command requires an "
15874 "argument (count + optional TP num)"));
1042e4c0 15875
4a64f543 15876 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15877
529480d0 15878 args = skip_spaces (args);
1042e4c0
SS
15879 if (*args && strncasecmp (args, "all", 3) == 0)
15880 {
d9b3f62e
PA
15881 struct breakpoint *b;
15882
1042e4c0 15883 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15884 if (*args)
15885 error (_("Junk at end of arguments."));
1042e4c0 15886
d9b3f62e 15887 ALL_TRACEPOINTS (b)
197f0a60 15888 {
d9b3f62e 15889 t1 = (struct tracepoint *) b;
197f0a60
TT
15890 trace_pass_set_count (t1, count, from_tty);
15891 }
15892 }
15893 else if (*args == '\0')
1042e4c0 15894 {
5fa1d40e 15895 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15896 if (t1)
197f0a60
TT
15897 trace_pass_set_count (t1, count, from_tty);
15898 }
15899 else
15900 {
15901 struct get_number_or_range_state state;
15902
15903 init_number_or_range (&state, args);
15904 while (!state.finished)
1042e4c0 15905 {
5fa1d40e 15906 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15907 if (t1)
15908 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15909 }
15910 }
1042e4c0
SS
15911}
15912
d9b3f62e 15913struct tracepoint *
1042e4c0
SS
15914get_tracepoint (int num)
15915{
15916 struct breakpoint *t;
15917
15918 ALL_TRACEPOINTS (t)
15919 if (t->number == num)
d9b3f62e 15920 return (struct tracepoint *) t;
1042e4c0
SS
15921
15922 return NULL;
15923}
15924
d5551862
SS
15925/* Find the tracepoint with the given target-side number (which may be
15926 different from the tracepoint number after disconnecting and
15927 reconnecting). */
15928
d9b3f62e 15929struct tracepoint *
d5551862
SS
15930get_tracepoint_by_number_on_target (int num)
15931{
d9b3f62e 15932 struct breakpoint *b;
d5551862 15933
d9b3f62e
PA
15934 ALL_TRACEPOINTS (b)
15935 {
15936 struct tracepoint *t = (struct tracepoint *) b;
15937
15938 if (t->number_on_target == num)
15939 return t;
15940 }
d5551862
SS
15941
15942 return NULL;
15943}
15944
1042e4c0 15945/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15946 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15947 If the argument is missing, the most recent tracepoint
15948 (tracepoint_count) is returned. */
15949
d9b3f62e 15950struct tracepoint *
197f0a60 15951get_tracepoint_by_number (char **arg,
5fa1d40e 15952 struct get_number_or_range_state *state)
1042e4c0 15953{
1042e4c0
SS
15954 struct breakpoint *t;
15955 int tpnum;
15956 char *instring = arg == NULL ? NULL : *arg;
15957
197f0a60
TT
15958 if (state)
15959 {
15960 gdb_assert (!state->finished);
15961 tpnum = get_number_or_range (state);
15962 }
15963 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15964 tpnum = tracepoint_count;
1042e4c0 15965 else
197f0a60 15966 tpnum = get_number (arg);
1042e4c0
SS
15967
15968 if (tpnum <= 0)
15969 {
15970 if (instring && *instring)
15971 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15972 instring);
15973 else
5fa1d40e 15974 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15975 return NULL;
15976 }
15977
15978 ALL_TRACEPOINTS (t)
15979 if (t->number == tpnum)
15980 {
d9b3f62e 15981 return (struct tracepoint *) t;
1042e4c0
SS
15982 }
15983
1042e4c0
SS
15984 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15985 return NULL;
15986}
15987
d9b3f62e
PA
15988void
15989print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15990{
15991 if (b->thread != -1)
15992 fprintf_unfiltered (fp, " thread %d", b->thread);
15993
15994 if (b->task != 0)
15995 fprintf_unfiltered (fp, " task %d", b->task);
15996
15997 fprintf_unfiltered (fp, "\n");
15998}
15999
6149aea9
PA
16000/* Save information on user settable breakpoints (watchpoints, etc) to
16001 a new script file named FILENAME. If FILTER is non-NULL, call it
16002 on each breakpoint and only include the ones for which it returns
16003 non-zero. */
16004
1042e4c0 16005static void
6149aea9
PA
16006save_breakpoints (char *filename, int from_tty,
16007 int (*filter) (const struct breakpoint *))
1042e4c0
SS
16008{
16009 struct breakpoint *tp;
6149aea9 16010 int any = 0;
1042e4c0 16011 struct cleanup *cleanup;
a7bdde9e 16012 struct ui_file *fp;
6149aea9 16013 int extra_trace_bits = 0;
1042e4c0 16014
6149aea9
PA
16015 if (filename == 0 || *filename == 0)
16016 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
16017
16018 /* See if we have anything to save. */
6149aea9 16019 ALL_BREAKPOINTS (tp)
1042e4c0 16020 {
6149aea9 16021 /* Skip internal and momentary breakpoints. */
09d682a4 16022 if (!user_breakpoint_p (tp))
6149aea9
PA
16023 continue;
16024
16025 /* If we have a filter, only save the breakpoints it accepts. */
16026 if (filter && !filter (tp))
16027 continue;
16028
16029 any = 1;
16030
16031 if (is_tracepoint (tp))
16032 {
16033 extra_trace_bits = 1;
16034
16035 /* We can stop searching. */
16036 break;
16037 }
1042e4c0 16038 }
6149aea9
PA
16039
16040 if (!any)
1042e4c0 16041 {
6149aea9 16042 warning (_("Nothing to save."));
1042e4c0
SS
16043 return;
16044 }
16045
c718be47
PA
16046 filename = tilde_expand (filename);
16047 cleanup = make_cleanup (xfree, filename);
16048 fp = gdb_fopen (filename, "w");
059fb39f 16049 if (!fp)
6149aea9
PA
16050 error (_("Unable to open file '%s' for saving (%s)"),
16051 filename, safe_strerror (errno));
a7bdde9e 16052 make_cleanup_ui_file_delete (fp);
8bf6485c 16053
6149aea9
PA
16054 if (extra_trace_bits)
16055 save_trace_state_variables (fp);
8bf6485c 16056
6149aea9 16057 ALL_BREAKPOINTS (tp)
1042e4c0 16058 {
6149aea9 16059 /* Skip internal and momentary breakpoints. */
09d682a4 16060 if (!user_breakpoint_p (tp))
6149aea9 16061 continue;
8bf6485c 16062
6149aea9
PA
16063 /* If we have a filter, only save the breakpoints it accepts. */
16064 if (filter && !filter (tp))
16065 continue;
16066
348d480f 16067 tp->ops->print_recreate (tp, fp);
1042e4c0 16068
6149aea9
PA
16069 /* Note, we can't rely on tp->number for anything, as we can't
16070 assume the recreated breakpoint numbers will match. Use $bpnum
16071 instead. */
16072
16073 if (tp->cond_string)
16074 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
16075
16076 if (tp->ignore_count)
16077 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
16078
2d9442cc 16079 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 16080 {
a7bdde9e
VP
16081 volatile struct gdb_exception ex;
16082
6149aea9 16083 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 16084
79a45e25 16085 ui_out_redirect (current_uiout, fp);
14dba4b4 16086 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 16087 {
79a45e25 16088 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 16089 }
79a45e25 16090 ui_out_redirect (current_uiout, NULL);
1042e4c0 16091
a7bdde9e
VP
16092 if (ex.reason < 0)
16093 throw_exception (ex);
1042e4c0 16094
a7bdde9e 16095 fprintf_unfiltered (fp, " end\n");
1042e4c0 16096 }
6149aea9
PA
16097
16098 if (tp->enable_state == bp_disabled)
99894e11 16099 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
16100
16101 /* If this is a multi-location breakpoint, check if the locations
16102 should be individually disabled. Watchpoint locations are
16103 special, and not user visible. */
16104 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
16105 {
16106 struct bp_location *loc;
16107 int n = 1;
16108
16109 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
16110 if (!loc->enabled)
16111 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
16112 }
1042e4c0 16113 }
8bf6485c 16114
6149aea9 16115 if (extra_trace_bits && *default_collect)
8bf6485c
SS
16116 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
16117
1042e4c0 16118 if (from_tty)
6149aea9 16119 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 16120 do_cleanups (cleanup);
6149aea9
PA
16121}
16122
16123/* The `save breakpoints' command. */
16124
16125static void
16126save_breakpoints_command (char *args, int from_tty)
16127{
16128 save_breakpoints (args, from_tty, NULL);
16129}
16130
16131/* The `save tracepoints' command. */
16132
16133static void
16134save_tracepoints_command (char *args, int from_tty)
16135{
16136 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
16137}
16138
16139/* Create a vector of all tracepoints. */
16140
16141VEC(breakpoint_p) *
eeae04df 16142all_tracepoints (void)
1042e4c0
SS
16143{
16144 VEC(breakpoint_p) *tp_vec = 0;
16145 struct breakpoint *tp;
16146
16147 ALL_TRACEPOINTS (tp)
16148 {
16149 VEC_safe_push (breakpoint_p, tp_vec, tp);
16150 }
16151
16152 return tp_vec;
16153}
16154
c906108c 16155\f
4a64f543
MS
16156/* This help string is used for the break, hbreak, tbreak and thbreak
16157 commands. It is defined as a macro to prevent duplication.
16158 COMMAND should be a string constant containing the name of the
16159 command. */
31e2b00f 16160#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
16161command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
16162PROBE_MODIFIER shall be present if the command is to be placed in a\n\
16163probe point. Accepted values are `-probe' (for a generic, automatically\n\
16164guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
16165LOCATION may be a line number, function name, or \"*\" and an address.\n\
16166If a line number is specified, break at start of code for that line.\n\
16167If a function is specified, break at start of code for that function.\n\
16168If an address is specified, break at that exact address.\n\
dc10affe
PA
16169With no LOCATION, uses current execution address of the selected\n\
16170stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
16171\n\
16172THREADNUM is the number from \"info threads\".\n\
16173CONDITION is a boolean expression.\n\
16174\n\
d41c0fc8
PA
16175Multiple breakpoints at one place are permitted, and useful if their\n\
16176conditions are different.\n\
31e2b00f
AS
16177\n\
16178Do \"help breakpoints\" for info on other commands dealing with breakpoints."
16179
44feb3ce
TT
16180/* List of subcommands for "catch". */
16181static struct cmd_list_element *catch_cmdlist;
16182
16183/* List of subcommands for "tcatch". */
16184static struct cmd_list_element *tcatch_cmdlist;
16185
9ac4176b 16186void
44feb3ce 16187add_catch_command (char *name, char *docstring,
82ae6c8d 16188 cmd_sfunc_ftype *sfunc,
625e8578 16189 completer_ftype *completer,
44feb3ce
TT
16190 void *user_data_catch,
16191 void *user_data_tcatch)
16192{
16193 struct cmd_list_element *command;
16194
16195 command = add_cmd (name, class_breakpoint, NULL, docstring,
16196 &catch_cmdlist);
16197 set_cmd_sfunc (command, sfunc);
16198 set_cmd_context (command, user_data_catch);
a96d9b2e 16199 set_cmd_completer (command, completer);
44feb3ce
TT
16200
16201 command = add_cmd (name, class_breakpoint, NULL, docstring,
16202 &tcatch_cmdlist);
16203 set_cmd_sfunc (command, sfunc);
16204 set_cmd_context (command, user_data_tcatch);
a96d9b2e 16205 set_cmd_completer (command, completer);
44feb3ce
TT
16206}
16207
6c95b8df 16208static void
a79b8f6e 16209clear_syscall_counts (struct inferior *inf)
6c95b8df 16210{
fa3064dd
YQ
16211 struct catch_syscall_inferior_data *inf_data
16212 = get_catch_syscall_inferior_data (inf);
16213
16214 inf_data->total_syscalls_count = 0;
16215 inf_data->any_syscall_count = 0;
16216 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
16217}
16218
6149aea9
PA
16219static void
16220save_command (char *arg, int from_tty)
16221{
3e43a32a
MS
16222 printf_unfiltered (_("\"save\" must be followed by "
16223 "the name of a save subcommand.\n"));
635c7e8a 16224 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
16225}
16226
84f4c1fe
PM
16227struct breakpoint *
16228iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16229 void *data)
16230{
35df4500 16231 struct breakpoint *b, *b_tmp;
84f4c1fe 16232
35df4500 16233 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
16234 {
16235 if ((*callback) (b, data))
16236 return b;
16237 }
16238
16239 return NULL;
16240}
16241
0574c78f
GB
16242/* Zero if any of the breakpoint's locations could be a location where
16243 functions have been inlined, nonzero otherwise. */
16244
16245static int
16246is_non_inline_function (struct breakpoint *b)
16247{
16248 /* The shared library event breakpoint is set on the address of a
16249 non-inline function. */
16250 if (b->type == bp_shlib_event)
16251 return 1;
16252
16253 return 0;
16254}
16255
16256/* Nonzero if the specified PC cannot be a location where functions
16257 have been inlined. */
16258
16259int
09ac7c10
TT
16260pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16261 const struct target_waitstatus *ws)
0574c78f
GB
16262{
16263 struct breakpoint *b;
16264 struct bp_location *bl;
16265
16266 ALL_BREAKPOINTS (b)
16267 {
16268 if (!is_non_inline_function (b))
16269 continue;
16270
16271 for (bl = b->loc; bl != NULL; bl = bl->next)
16272 {
16273 if (!bl->shlib_disabled
09ac7c10 16274 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
16275 return 1;
16276 }
16277 }
16278
16279 return 0;
16280}
16281
2f202fde
JK
16282/* Remove any references to OBJFILE which is going to be freed. */
16283
16284void
16285breakpoint_free_objfile (struct objfile *objfile)
16286{
16287 struct bp_location **locp, *loc;
16288
16289 ALL_BP_LOCATIONS (loc, locp)
16290 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
16291 loc->symtab = NULL;
16292}
16293
2060206e
PA
16294void
16295initialize_breakpoint_ops (void)
16296{
16297 static int initialized = 0;
16298
16299 struct breakpoint_ops *ops;
16300
16301 if (initialized)
16302 return;
16303 initialized = 1;
16304
16305 /* The breakpoint_ops structure to be inherit by all kinds of
16306 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16307 internal and momentary breakpoints, etc.). */
16308 ops = &bkpt_base_breakpoint_ops;
16309 *ops = base_breakpoint_ops;
16310 ops->re_set = bkpt_re_set;
16311 ops->insert_location = bkpt_insert_location;
16312 ops->remove_location = bkpt_remove_location;
16313 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
16314 ops->create_sals_from_address = bkpt_create_sals_from_address;
16315 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16316 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
16317
16318 /* The breakpoint_ops structure to be used in regular breakpoints. */
16319 ops = &bkpt_breakpoint_ops;
16320 *ops = bkpt_base_breakpoint_ops;
16321 ops->re_set = bkpt_re_set;
16322 ops->resources_needed = bkpt_resources_needed;
16323 ops->print_it = bkpt_print_it;
16324 ops->print_mention = bkpt_print_mention;
16325 ops->print_recreate = bkpt_print_recreate;
16326
16327 /* Ranged breakpoints. */
16328 ops = &ranged_breakpoint_ops;
16329 *ops = bkpt_breakpoint_ops;
16330 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16331 ops->resources_needed = resources_needed_ranged_breakpoint;
16332 ops->print_it = print_it_ranged_breakpoint;
16333 ops->print_one = print_one_ranged_breakpoint;
16334 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16335 ops->print_mention = print_mention_ranged_breakpoint;
16336 ops->print_recreate = print_recreate_ranged_breakpoint;
16337
16338 /* Internal breakpoints. */
16339 ops = &internal_breakpoint_ops;
16340 *ops = bkpt_base_breakpoint_ops;
16341 ops->re_set = internal_bkpt_re_set;
16342 ops->check_status = internal_bkpt_check_status;
16343 ops->print_it = internal_bkpt_print_it;
16344 ops->print_mention = internal_bkpt_print_mention;
16345
16346 /* Momentary breakpoints. */
16347 ops = &momentary_breakpoint_ops;
16348 *ops = bkpt_base_breakpoint_ops;
16349 ops->re_set = momentary_bkpt_re_set;
16350 ops->check_status = momentary_bkpt_check_status;
16351 ops->print_it = momentary_bkpt_print_it;
16352 ops->print_mention = momentary_bkpt_print_mention;
16353
e2e4d78b
JK
16354 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16355 ops = &longjmp_breakpoint_ops;
16356 *ops = momentary_breakpoint_ops;
16357 ops->dtor = longjmp_bkpt_dtor;
16358
55aa24fb
SDJ
16359 /* Probe breakpoints. */
16360 ops = &bkpt_probe_breakpoint_ops;
16361 *ops = bkpt_breakpoint_ops;
16362 ops->insert_location = bkpt_probe_insert_location;
16363 ops->remove_location = bkpt_probe_remove_location;
16364 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16365 ops->decode_linespec = bkpt_probe_decode_linespec;
16366
2060206e
PA
16367 /* Watchpoints. */
16368 ops = &watchpoint_breakpoint_ops;
16369 *ops = base_breakpoint_ops;
3a5c3e22 16370 ops->dtor = dtor_watchpoint;
2060206e
PA
16371 ops->re_set = re_set_watchpoint;
16372 ops->insert_location = insert_watchpoint;
16373 ops->remove_location = remove_watchpoint;
16374 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16375 ops->check_status = check_status_watchpoint;
16376 ops->resources_needed = resources_needed_watchpoint;
16377 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16378 ops->print_it = print_it_watchpoint;
16379 ops->print_mention = print_mention_watchpoint;
16380 ops->print_recreate = print_recreate_watchpoint;
427cd150 16381 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16382
16383 /* Masked watchpoints. */
16384 ops = &masked_watchpoint_breakpoint_ops;
16385 *ops = watchpoint_breakpoint_ops;
16386 ops->insert_location = insert_masked_watchpoint;
16387 ops->remove_location = remove_masked_watchpoint;
16388 ops->resources_needed = resources_needed_masked_watchpoint;
16389 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16390 ops->print_it = print_it_masked_watchpoint;
16391 ops->print_one_detail = print_one_detail_masked_watchpoint;
16392 ops->print_mention = print_mention_masked_watchpoint;
16393 ops->print_recreate = print_recreate_masked_watchpoint;
16394
16395 /* Tracepoints. */
16396 ops = &tracepoint_breakpoint_ops;
16397 *ops = base_breakpoint_ops;
16398 ops->re_set = tracepoint_re_set;
16399 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16400 ops->print_one_detail = tracepoint_print_one_detail;
16401 ops->print_mention = tracepoint_print_mention;
16402 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16403 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16404 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16405 ops->decode_linespec = tracepoint_decode_linespec;
16406
55aa24fb
SDJ
16407 /* Probe tracepoints. */
16408 ops = &tracepoint_probe_breakpoint_ops;
16409 *ops = tracepoint_breakpoint_ops;
16410 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16411 ops->decode_linespec = tracepoint_probe_decode_linespec;
16412
983af33b
SDJ
16413 /* Static tracepoints with marker (`-m'). */
16414 ops = &strace_marker_breakpoint_ops;
16415 *ops = tracepoint_breakpoint_ops;
16416 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16417 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16418 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16419
16420 /* Fork catchpoints. */
16421 ops = &catch_fork_breakpoint_ops;
16422 *ops = base_breakpoint_ops;
16423 ops->insert_location = insert_catch_fork;
16424 ops->remove_location = remove_catch_fork;
16425 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16426 ops->print_it = print_it_catch_fork;
16427 ops->print_one = print_one_catch_fork;
16428 ops->print_mention = print_mention_catch_fork;
16429 ops->print_recreate = print_recreate_catch_fork;
16430
16431 /* Vfork catchpoints. */
16432 ops = &catch_vfork_breakpoint_ops;
16433 *ops = base_breakpoint_ops;
16434 ops->insert_location = insert_catch_vfork;
16435 ops->remove_location = remove_catch_vfork;
16436 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16437 ops->print_it = print_it_catch_vfork;
16438 ops->print_one = print_one_catch_vfork;
16439 ops->print_mention = print_mention_catch_vfork;
16440 ops->print_recreate = print_recreate_catch_vfork;
16441
16442 /* Exec catchpoints. */
16443 ops = &catch_exec_breakpoint_ops;
16444 *ops = base_breakpoint_ops;
16445 ops->dtor = dtor_catch_exec;
16446 ops->insert_location = insert_catch_exec;
16447 ops->remove_location = remove_catch_exec;
16448 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16449 ops->print_it = print_it_catch_exec;
16450 ops->print_one = print_one_catch_exec;
16451 ops->print_mention = print_mention_catch_exec;
16452 ops->print_recreate = print_recreate_catch_exec;
16453
16454 /* Syscall catchpoints. */
16455 ops = &catch_syscall_breakpoint_ops;
16456 *ops = base_breakpoint_ops;
16457 ops->dtor = dtor_catch_syscall;
16458 ops->insert_location = insert_catch_syscall;
16459 ops->remove_location = remove_catch_syscall;
16460 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16461 ops->print_it = print_it_catch_syscall;
16462 ops->print_one = print_one_catch_syscall;
16463 ops->print_mention = print_mention_catch_syscall;
16464 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16465
16466 /* Solib-related catchpoints. */
16467 ops = &catch_solib_breakpoint_ops;
16468 *ops = base_breakpoint_ops;
16469 ops->dtor = dtor_catch_solib;
16470 ops->insert_location = insert_catch_solib;
16471 ops->remove_location = remove_catch_solib;
16472 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16473 ops->check_status = check_status_catch_solib;
16474 ops->print_it = print_it_catch_solib;
16475 ops->print_one = print_one_catch_solib;
16476 ops->print_mention = print_mention_catch_solib;
16477 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16478
16479 ops = &dprintf_breakpoint_ops;
16480 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16481 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16482 ops->resources_needed = bkpt_resources_needed;
16483 ops->print_it = bkpt_print_it;
16484 ops->print_mention = bkpt_print_mention;
2d9442cc 16485 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16486 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16487 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16488}
16489
8bfd80db
YQ
16490/* Chain containing all defined "enable breakpoint" subcommands. */
16491
16492static struct cmd_list_element *enablebreaklist = NULL;
16493
c906108c 16494void
fba45db2 16495_initialize_breakpoint (void)
c906108c
SS
16496{
16497 struct cmd_list_element *c;
16498
2060206e
PA
16499 initialize_breakpoint_ops ();
16500
84acb35a 16501 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16502 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16503 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16504 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16505
55aa24fb
SDJ
16506 breakpoint_objfile_key
16507 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16508
fa3064dd 16509 catch_syscall_inferior_data
8e260fc0
TT
16510 = register_inferior_data_with_cleanup (NULL,
16511 catch_syscall_inferior_data_cleanup);
fa3064dd 16512
c906108c
SS
16513 breakpoint_chain = 0;
16514 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16515 before a breakpoint is set. */
16516 breakpoint_count = 0;
16517
1042e4c0
SS
16518 tracepoint_count = 0;
16519
1bedd215
AC
16520 add_com ("ignore", class_breakpoint, ignore_command, _("\
16521Set ignore-count of breakpoint number N to COUNT.\n\
16522Usage is `ignore N COUNT'."));
c906108c 16523 if (xdb_commands)
c5aa993b 16524 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16525
1bedd215
AC
16526 add_com ("commands", class_breakpoint, commands_command, _("\
16527Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16528Give breakpoint number as argument after \"commands\".\n\
16529With no argument, the targeted breakpoint is the last one set.\n\
16530The commands themselves follow starting on the next line.\n\
16531Type a line containing \"end\" to indicate the end of them.\n\
16532Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16533then no output is printed when it is hit, except what the commands print."));
c906108c 16534
d55637df 16535 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16536Specify breakpoint number N to break only if COND is true.\n\
c906108c 16537Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16538expression to be evaluated whenever breakpoint N is reached."));
d55637df 16539 set_cmd_completer (c, condition_completer);
c906108c 16540
1bedd215 16541 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16542Set a temporary breakpoint.\n\
c906108c
SS
16543Like \"break\" except the breakpoint is only temporary,\n\
16544so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16545by using \"enable delete\" on the breakpoint number.\n\
16546\n"
16547BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16548 set_cmd_completer (c, location_completer);
c94fdfd0 16549
1bedd215 16550 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16551Set a hardware assisted breakpoint.\n\
c906108c 16552Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16553some target hardware may not have this support.\n\
16554\n"
16555BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16556 set_cmd_completer (c, location_completer);
c906108c 16557
1bedd215 16558 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16559Set a temporary hardware assisted breakpoint.\n\
c906108c 16560Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16561so it will be deleted when hit.\n\
16562\n"
16563BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16564 set_cmd_completer (c, location_completer);
c906108c 16565
1bedd215
AC
16566 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16567Enable some breakpoints.\n\
c906108c
SS
16568Give breakpoint numbers (separated by spaces) as arguments.\n\
16569With no subcommand, breakpoints are enabled until you command otherwise.\n\
16570This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16571With a subcommand you can enable temporarily."),
c906108c
SS
16572 &enablelist, "enable ", 1, &cmdlist);
16573 if (xdb_commands)
1bedd215
AC
16574 add_com ("ab", class_breakpoint, enable_command, _("\
16575Enable some breakpoints.\n\
c906108c
SS
16576Give breakpoint numbers (separated by spaces) as arguments.\n\
16577With no subcommand, breakpoints are enabled until you command otherwise.\n\
16578This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16579With a subcommand you can enable temporarily."));
c906108c
SS
16580
16581 add_com_alias ("en", "enable", class_breakpoint, 1);
16582
84951ab5 16583 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16584Enable some breakpoints.\n\
c906108c
SS
16585Give breakpoint numbers (separated by spaces) as arguments.\n\
16586This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16587May be abbreviated to simply \"enable\".\n"),
c5aa993b 16588 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16589
1a966eab
AC
16590 add_cmd ("once", no_class, enable_once_command, _("\
16591Enable breakpoints for one hit. Give breakpoint numbers.\n\
16592If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16593 &enablebreaklist);
16594
1a966eab
AC
16595 add_cmd ("delete", no_class, enable_delete_command, _("\
16596Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16597If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16598 &enablebreaklist);
16599
816338b5
SS
16600 add_cmd ("count", no_class, enable_count_command, _("\
16601Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16602If a breakpoint is hit while enabled in this fashion,\n\
16603the count is decremented; when it reaches zero, the breakpoint is disabled."),
16604 &enablebreaklist);
16605
1a966eab
AC
16606 add_cmd ("delete", no_class, enable_delete_command, _("\
16607Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16608If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16609 &enablelist);
16610
1a966eab
AC
16611 add_cmd ("once", no_class, enable_once_command, _("\
16612Enable breakpoints for one hit. Give breakpoint numbers.\n\
16613If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16614 &enablelist);
16615
16616 add_cmd ("count", no_class, enable_count_command, _("\
16617Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16618If a breakpoint is hit while enabled in this fashion,\n\
16619the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16620 &enablelist);
16621
1bedd215
AC
16622 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16623Disable some breakpoints.\n\
c906108c
SS
16624Arguments are breakpoint numbers with spaces in between.\n\
16625To disable all breakpoints, give no argument.\n\
64b9b334 16626A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16627 &disablelist, "disable ", 1, &cmdlist);
16628 add_com_alias ("dis", "disable", class_breakpoint, 1);
16629 add_com_alias ("disa", "disable", class_breakpoint, 1);
16630 if (xdb_commands)
1bedd215
AC
16631 add_com ("sb", class_breakpoint, disable_command, _("\
16632Disable some breakpoints.\n\
c906108c
SS
16633Arguments are breakpoint numbers with spaces in between.\n\
16634To disable all breakpoints, give no argument.\n\
64b9b334 16635A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16636
1a966eab
AC
16637 add_cmd ("breakpoints", class_alias, disable_command, _("\
16638Disable some breakpoints.\n\
c906108c
SS
16639Arguments are breakpoint numbers with spaces in between.\n\
16640To disable all breakpoints, give no argument.\n\
64b9b334 16641A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16642This command may be abbreviated \"disable\"."),
c906108c
SS
16643 &disablelist);
16644
1bedd215
AC
16645 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16646Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16647Arguments are breakpoint numbers with spaces in between.\n\
16648To delete all breakpoints, give no argument.\n\
16649\n\
16650Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16651The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16652 &deletelist, "delete ", 1, &cmdlist);
16653 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16654 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16655 if (xdb_commands)
1bedd215
AC
16656 add_com ("db", class_breakpoint, delete_command, _("\
16657Delete some breakpoints.\n\
c906108c 16658Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16659To delete all breakpoints, give no argument.\n"));
c906108c 16660
1a966eab
AC
16661 add_cmd ("breakpoints", class_alias, delete_command, _("\
16662Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16663Arguments are breakpoint numbers with spaces in between.\n\
16664To delete all breakpoints, give no argument.\n\
1a966eab 16665This command may be abbreviated \"delete\"."),
c906108c
SS
16666 &deletelist);
16667
1bedd215
AC
16668 add_com ("clear", class_breakpoint, clear_command, _("\
16669Clear breakpoint at specified line or function.\n\
c906108c
SS
16670Argument may be line number, function name, or \"*\" and an address.\n\
16671If line number is specified, all breakpoints in that line are cleared.\n\
16672If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16673If an address is specified, breakpoints at that address are cleared.\n\
16674\n\
16675With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16676is executing in.\n\
16677\n\
1bedd215 16678See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16679 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16680
1bedd215 16681 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16682Set breakpoint at specified line or function.\n"
16683BREAK_ARGS_HELP ("break")));
5ba2abeb 16684 set_cmd_completer (c, location_completer);
c94fdfd0 16685
c906108c
SS
16686 add_com_alias ("b", "break", class_run, 1);
16687 add_com_alias ("br", "break", class_run, 1);
16688 add_com_alias ("bre", "break", class_run, 1);
16689 add_com_alias ("brea", "break", class_run, 1);
16690
7681d515
PM
16691 if (xdb_commands)
16692 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16693
16694 if (dbx_commands)
16695 {
1bedd215
AC
16696 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16697Break in function/address or break at a line in the current file."),
c5aa993b
JM
16698 &stoplist, "stop ", 1, &cmdlist);
16699 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16700 _("Break in function or address."), &stoplist);
c5aa993b 16701 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16702 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16703 add_com ("status", class_info, breakpoints_info, _("\
16704Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16705The \"Type\" column indicates one of:\n\
16706\tbreakpoint - normal breakpoint\n\
16707\twatchpoint - watchpoint\n\
16708The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16709the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16710breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16711address and file/line number respectively.\n\
16712\n\
16713Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16714are set to the address of the last breakpoint listed unless the command\n\
16715is prefixed with \"server \".\n\n\
c906108c 16716Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16717breakpoint set."));
c906108c
SS
16718 }
16719
1bedd215 16720 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16721Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16722The \"Type\" column indicates one of:\n\
16723\tbreakpoint - normal breakpoint\n\
16724\twatchpoint - watchpoint\n\
16725The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16726the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16727breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16728address and file/line number respectively.\n\
16729\n\
16730Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16731are set to the address of the last breakpoint listed unless the command\n\
16732is prefixed with \"server \".\n\n\
c906108c 16733Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16734breakpoint set."));
c906108c 16735
6b04bdb7
MS
16736 add_info_alias ("b", "breakpoints", 1);
16737
c906108c 16738 if (xdb_commands)
1bedd215
AC
16739 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16740Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16741The \"Type\" column indicates one of:\n\
16742\tbreakpoint - normal breakpoint\n\
16743\twatchpoint - watchpoint\n\
16744The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16745the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16746breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16747address and file/line number respectively.\n\
16748\n\
16749Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16750are set to the address of the last breakpoint listed unless the command\n\
16751is prefixed with \"server \".\n\n\
c906108c 16752Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16753breakpoint set."));
c906108c 16754
1a966eab
AC
16755 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16756Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16757The \"Type\" column indicates one of:\n\
16758\tbreakpoint - normal breakpoint\n\
16759\twatchpoint - watchpoint\n\
16760\tlongjmp - internal breakpoint used to step through longjmp()\n\
16761\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16762\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16763\tfinish - internal breakpoint used by the \"finish\" command\n\
16764The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16765the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16766breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16767address and file/line number respectively.\n\
16768\n\
16769Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16770are set to the address of the last breakpoint listed unless the command\n\
16771is prefixed with \"server \".\n\n\
c906108c 16772Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16773breakpoint set."),
c906108c
SS
16774 &maintenanceinfolist);
16775
44feb3ce
TT
16776 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16777Set catchpoints to catch events."),
16778 &catch_cmdlist, "catch ",
16779 0/*allow-unknown*/, &cmdlist);
16780
16781 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16782Set temporary catchpoints to catch events."),
16783 &tcatch_cmdlist, "tcatch ",
16784 0/*allow-unknown*/, &cmdlist);
16785
44feb3ce
TT
16786 add_catch_command ("fork", _("Catch calls to fork."),
16787 catch_fork_command_1,
a96d9b2e 16788 NULL,
44feb3ce
TT
16789 (void *) (uintptr_t) catch_fork_permanent,
16790 (void *) (uintptr_t) catch_fork_temporary);
16791 add_catch_command ("vfork", _("Catch calls to vfork."),
16792 catch_fork_command_1,
a96d9b2e 16793 NULL,
44feb3ce
TT
16794 (void *) (uintptr_t) catch_vfork_permanent,
16795 (void *) (uintptr_t) catch_vfork_temporary);
16796 add_catch_command ("exec", _("Catch calls to exec."),
16797 catch_exec_command_1,
a96d9b2e
SDJ
16798 NULL,
16799 CATCH_PERMANENT,
16800 CATCH_TEMPORARY);
edcc5120
TT
16801 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16802Usage: catch load [REGEX]\n\
16803If REGEX is given, only stop for libraries matching the regular expression."),
16804 catch_load_command_1,
16805 NULL,
16806 CATCH_PERMANENT,
16807 CATCH_TEMPORARY);
16808 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16809Usage: catch unload [REGEX]\n\
16810If REGEX is given, only stop for libraries matching the regular expression."),
16811 catch_unload_command_1,
16812 NULL,
16813 CATCH_PERMANENT,
16814 CATCH_TEMPORARY);
a96d9b2e
SDJ
16815 add_catch_command ("syscall", _("\
16816Catch system calls by their names and/or numbers.\n\
16817Arguments say which system calls to catch. If no arguments\n\
16818are given, every system call will be caught.\n\
16819Arguments, if given, should be one or more system call names\n\
16820(if your system supports that), or system call numbers."),
16821 catch_syscall_command_1,
16822 catch_syscall_completer,
44feb3ce
TT
16823 CATCH_PERMANENT,
16824 CATCH_TEMPORARY);
c5aa993b 16825
1bedd215
AC
16826 c = add_com ("watch", class_breakpoint, watch_command, _("\
16827Set a watchpoint for an expression.\n\
06a64a0b 16828Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16829A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16830an expression changes.\n\
16831If -l or -location is given, this evaluates EXPRESSION and watches\n\
16832the memory to which it refers."));
65d12d83 16833 set_cmd_completer (c, expression_completer);
c906108c 16834
1bedd215
AC
16835 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16836Set a read watchpoint for an expression.\n\
06a64a0b 16837Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16838A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16839an expression is read.\n\
16840If -l or -location is given, this evaluates EXPRESSION and watches\n\
16841the memory to which it refers."));
65d12d83 16842 set_cmd_completer (c, expression_completer);
c906108c 16843
1bedd215
AC
16844 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16845Set a watchpoint for an expression.\n\
06a64a0b 16846Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16847A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16848an expression is either read or written.\n\
16849If -l or -location is given, this evaluates EXPRESSION and watches\n\
16850the memory to which it refers."));
65d12d83 16851 set_cmd_completer (c, expression_completer);
c906108c 16852
d77f58be 16853 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16854Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16855
920d2a44
AC
16856 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16857 respond to changes - contrary to the description. */
85c07804
AC
16858 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16859 &can_use_hw_watchpoints, _("\
16860Set debugger's willingness to use watchpoint hardware."), _("\
16861Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16862If zero, gdb will not use hardware for new watchpoints, even if\n\
16863such is available. (However, any hardware watchpoints that were\n\
16864created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16865hardware.)"),
16866 NULL,
920d2a44 16867 show_can_use_hw_watchpoints,
85c07804 16868 &setlist, &showlist);
c906108c
SS
16869
16870 can_use_hw_watchpoints = 1;
fa8d40ab 16871
1042e4c0
SS
16872 /* Tracepoint manipulation commands. */
16873
16874 c = add_com ("trace", class_breakpoint, trace_command, _("\
16875Set a tracepoint at specified line or function.\n\
16876\n"
16877BREAK_ARGS_HELP ("trace") "\n\
16878Do \"help tracepoints\" for info on other tracepoint commands."));
16879 set_cmd_completer (c, location_completer);
16880
16881 add_com_alias ("tp", "trace", class_alias, 0);
16882 add_com_alias ("tr", "trace", class_alias, 1);
16883 add_com_alias ("tra", "trace", class_alias, 1);
16884 add_com_alias ("trac", "trace", class_alias, 1);
16885
7a697b8d
SS
16886 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16887Set a fast tracepoint at specified line or function.\n\
16888\n"
16889BREAK_ARGS_HELP ("ftrace") "\n\
16890Do \"help tracepoints\" for info on other tracepoint commands."));
16891 set_cmd_completer (c, location_completer);
16892
0fb4aa4b
PA
16893 c = add_com ("strace", class_breakpoint, strace_command, _("\
16894Set a static tracepoint at specified line, function or marker.\n\
16895\n\
16896strace [LOCATION] [if CONDITION]\n\
16897LOCATION may be a line number, function name, \"*\" and an address,\n\
16898or -m MARKER_ID.\n\
16899If a line number is specified, probe the marker at start of code\n\
16900for that line. If a function is specified, probe the marker at start\n\
16901of code for that function. If an address is specified, probe the marker\n\
16902at that exact address. If a marker id is specified, probe the marker\n\
16903with that name. With no LOCATION, uses current execution address of\n\
16904the selected stack frame.\n\
16905Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16906This collects arbitrary user data passed in the probe point call to the\n\
16907tracing library. You can inspect it when analyzing the trace buffer,\n\
16908by printing the $_sdata variable like any other convenience variable.\n\
16909\n\
16910CONDITION is a boolean expression.\n\
16911\n\
d41c0fc8
PA
16912Multiple tracepoints at one place are permitted, and useful if their\n\
16913conditions are different.\n\
0fb4aa4b
PA
16914\n\
16915Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16916Do \"help tracepoints\" for info on other tracepoint commands."));
16917 set_cmd_completer (c, location_completer);
16918
1042e4c0 16919 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16920Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16921Convenience variable \"$tpnum\" contains the number of the\n\
16922last tracepoint set."));
16923
16924 add_info_alias ("tp", "tracepoints", 1);
16925
16926 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16927Delete specified tracepoints.\n\
16928Arguments are tracepoint numbers, separated by spaces.\n\
16929No argument means delete all tracepoints."),
16930 &deletelist);
7e20dfcd 16931 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16932
16933 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16934Disable specified tracepoints.\n\
16935Arguments are tracepoint numbers, separated by spaces.\n\
16936No argument means disable all tracepoints."),
16937 &disablelist);
16938 deprecate_cmd (c, "disable");
16939
16940 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16941Enable specified tracepoints.\n\
16942Arguments are tracepoint numbers, separated by spaces.\n\
16943No argument means enable all tracepoints."),
16944 &enablelist);
16945 deprecate_cmd (c, "enable");
16946
16947 add_com ("passcount", class_trace, trace_pass_command, _("\
16948Set the passcount for a tracepoint.\n\
16949The trace will end when the tracepoint has been passed 'count' times.\n\
16950Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16951if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16952
6149aea9
PA
16953 add_prefix_cmd ("save", class_breakpoint, save_command,
16954 _("Save breakpoint definitions as a script."),
16955 &save_cmdlist, "save ",
16956 0/*allow-unknown*/, &cmdlist);
16957
16958 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16959Save current breakpoint definitions as a script.\n\
cce7e648 16960This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16961catchpoints, tracepoints). Use the 'source' command in another debug\n\
16962session to restore them."),
16963 &save_cmdlist);
16964 set_cmd_completer (c, filename_completer);
16965
16966 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16967Save current tracepoint definitions as a script.\n\
6149aea9
PA
16968Use the 'source' command in another debug session to restore them."),
16969 &save_cmdlist);
1042e4c0
SS
16970 set_cmd_completer (c, filename_completer);
16971
6149aea9
PA
16972 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16973 deprecate_cmd (c, "save tracepoints");
16974
1bedd215 16975 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16976Breakpoint specific settings\n\
16977Configure various breakpoint-specific variables such as\n\
1bedd215 16978pending breakpoint behavior"),
fa8d40ab
JJ
16979 &breakpoint_set_cmdlist, "set breakpoint ",
16980 0/*allow-unknown*/, &setlist);
1bedd215 16981 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16982Breakpoint specific settings\n\
16983Configure various breakpoint-specific variables such as\n\
1bedd215 16984pending breakpoint behavior"),
fa8d40ab
JJ
16985 &breakpoint_show_cmdlist, "show breakpoint ",
16986 0/*allow-unknown*/, &showlist);
16987
7915a72c
AC
16988 add_setshow_auto_boolean_cmd ("pending", no_class,
16989 &pending_break_support, _("\
16990Set debugger's behavior regarding pending breakpoints."), _("\
16991Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16992If on, an unrecognized breakpoint location will cause gdb to create a\n\
16993pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16994an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16995user-query to see if a pending breakpoint should be created."),
2c5b56ce 16996 NULL,
920d2a44 16997 show_pending_break_support,
6e1d7d6c
AC
16998 &breakpoint_set_cmdlist,
16999 &breakpoint_show_cmdlist);
fa8d40ab
JJ
17000
17001 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
17002
17003 add_setshow_boolean_cmd ("auto-hw", no_class,
17004 &automatic_hardware_breakpoints, _("\
17005Set automatic usage of hardware breakpoints."), _("\
17006Show automatic usage of hardware breakpoints."), _("\
17007If set, the debugger will automatically use hardware breakpoints for\n\
17008breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
17009a warning will be emitted for such breakpoints."),
17010 NULL,
17011 show_automatic_hardware_breakpoints,
17012 &breakpoint_set_cmdlist,
17013 &breakpoint_show_cmdlist);
74960c60 17014
a25a5a45
PA
17015 add_setshow_boolean_cmd ("always-inserted", class_support,
17016 &always_inserted_mode, _("\
74960c60
VP
17017Set mode for inserting breakpoints."), _("\
17018Show mode for inserting breakpoints."), _("\
a25a5a45
PA
17019When this mode is on, breakpoints are inserted immediately as soon as\n\
17020they're created, kept inserted even when execution stops, and removed\n\
17021only when the user deletes them. When this mode is off (the default),\n\
17022breakpoints are inserted only when execution continues, and removed\n\
17023when execution stops."),
72d0e2c5
YQ
17024 NULL,
17025 &show_always_inserted_mode,
17026 &breakpoint_set_cmdlist,
17027 &breakpoint_show_cmdlist);
f1310107 17028
b775012e
LM
17029 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
17030 condition_evaluation_enums,
17031 &condition_evaluation_mode_1, _("\
17032Set mode of breakpoint condition evaluation."), _("\
17033Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 17034When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
17035evaluated on the host's side by GDB. When it is set to \"target\",\n\
17036breakpoint conditions will be downloaded to the target (if the target\n\
17037supports such feature) and conditions will be evaluated on the target's side.\n\
17038If this is set to \"auto\" (default), this will be automatically set to\n\
17039\"target\" if it supports condition evaluation, otherwise it will\n\
17040be set to \"gdb\""),
17041 &set_condition_evaluation_mode,
17042 &show_condition_evaluation_mode,
17043 &breakpoint_set_cmdlist,
17044 &breakpoint_show_cmdlist);
17045
f1310107
TJB
17046 add_com ("break-range", class_breakpoint, break_range_command, _("\
17047Set a breakpoint for an address range.\n\
17048break-range START-LOCATION, END-LOCATION\n\
17049where START-LOCATION and END-LOCATION can be one of the following:\n\
17050 LINENUM, for that line in the current file,\n\
17051 FILE:LINENUM, for that line in that file,\n\
17052 +OFFSET, for that number of lines after the current line\n\
17053 or the start of the range\n\
17054 FUNCTION, for the first line in that function,\n\
17055 FILE:FUNCTION, to distinguish among like-named static functions.\n\
17056 *ADDRESS, for the instruction at that address.\n\
17057\n\
17058The breakpoint will stop execution of the inferior whenever it executes\n\
17059an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
17060range (including START-LOCATION and END-LOCATION)."));
17061
e7e0cddf
SS
17062 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
17063Set a dynamic printf at specified line or function.\n\
17064dprintf location,format string,arg1,arg2,...\n\
17065location may be a line number, function name, or \"*\" and an address.\n\
17066If a line number is specified, break at start of code for that line.\n\
0e4777df 17067If a function is specified, break at start of code for that function."));
e7e0cddf
SS
17068 set_cmd_completer (c, location_completer);
17069
17070 add_setshow_enum_cmd ("dprintf-style", class_support,
17071 dprintf_style_enums, &dprintf_style, _("\
17072Set the style of usage for dynamic printf."), _("\
17073Show the style of usage for dynamic printf."), _("\
17074This setting chooses how GDB will do a dynamic printf.\n\
17075If the value is \"gdb\", then the printing is done by GDB to its own\n\
17076console, as with the \"printf\" command.\n\
17077If the value is \"call\", the print is done by calling a function in your\n\
17078program; by default printf(), but you can choose a different function or\n\
17079output stream by setting dprintf-function and dprintf-channel."),
17080 update_dprintf_commands, NULL,
17081 &setlist, &showlist);
17082
17083 dprintf_function = xstrdup ("printf");
17084 add_setshow_string_cmd ("dprintf-function", class_support,
17085 &dprintf_function, _("\
17086Set the function to use for dynamic printf"), _("\
17087Show the function to use for dynamic printf"), NULL,
17088 update_dprintf_commands, NULL,
17089 &setlist, &showlist);
17090
17091 dprintf_channel = xstrdup ("");
17092 add_setshow_string_cmd ("dprintf-channel", class_support,
17093 &dprintf_channel, _("\
17094Set the channel to use for dynamic printf"), _("\
17095Show the channel to use for dynamic printf"), NULL,
17096 update_dprintf_commands, NULL,
17097 &setlist, &showlist);
17098
d3ce09f5
SS
17099 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
17100 &disconnected_dprintf, _("\
17101Set whether dprintf continues after GDB disconnects."), _("\
17102Show whether dprintf continues after GDB disconnects."), _("\
17103Use this to let dprintf commands continue to hit and produce output\n\
17104even if GDB disconnects or detaches from the target."),
17105 NULL,
17106 NULL,
17107 &setlist, &showlist);
17108
17109 add_com ("agent-printf", class_vars, agent_printf_command, _("\
17110agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
17111(target agent only) This is useful for formatted output in user-defined commands."));
17112
765dc015 17113 automatic_hardware_breakpoints = 1;
f3b1572e
PA
17114
17115 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 17116 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 17117}
This page took 2.772124 seconds and 4 git commands to generate.