Split TRY_CATCH into TRY + CATCH
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
32d0add0 3 Copyright (C) 1986-2015 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"
5589af0e 69#include "interps.h"
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 breakpoint_ops structure to be inherited by all breakpoint_ops
301 that are implemented on top of software or hardware breakpoints
302 (user breakpoints, internal and momentary breakpoints, etc.). */
303static struct breakpoint_ops bkpt_base_breakpoint_ops;
304
305/* Internal breakpoints class type. */
06edf0c0 306static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
307
308/* Momentary breakpoints class type. */
06edf0c0
PA
309static struct breakpoint_ops momentary_breakpoint_ops;
310
e2e4d78b
JK
311/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
312static struct breakpoint_ops longjmp_breakpoint_ops;
313
2060206e
PA
314/* The breakpoint_ops structure to be used in regular user created
315 breakpoints. */
316struct breakpoint_ops bkpt_breakpoint_ops;
317
55aa24fb
SDJ
318/* Breakpoints set on probes. */
319static struct breakpoint_ops bkpt_probe_breakpoint_ops;
320
e7e0cddf 321/* Dynamic printf class type. */
c5867ab6 322struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 323
d3ce09f5
SS
324/* The style in which to perform a dynamic printf. This is a user
325 option because different output options have different tradeoffs;
326 if GDB does the printing, there is better error handling if there
327 is a problem with any of the arguments, but using an inferior
328 function lets you have special-purpose printers and sending of
329 output to the same place as compiled-in print functions. */
330
331static const char dprintf_style_gdb[] = "gdb";
332static const char dprintf_style_call[] = "call";
333static const char dprintf_style_agent[] = "agent";
334static const char *const dprintf_style_enums[] = {
335 dprintf_style_gdb,
336 dprintf_style_call,
337 dprintf_style_agent,
338 NULL
339};
340static const char *dprintf_style = dprintf_style_gdb;
341
342/* The function to use for dynamic printf if the preferred style is to
343 call into the inferior. The value is simply a string that is
344 copied into the command, so it can be anything that GDB can
345 evaluate to a callable address, not necessarily a function name. */
346
347static char *dprintf_function = "";
348
349/* The channel to use for dynamic printf if the preferred style is to
350 call into the inferior; if a nonempty string, it will be passed to
351 the call as the first argument, with the format string as the
352 second. As with the dprintf function, this can be anything that
353 GDB knows how to evaluate, so in addition to common choices like
354 "stderr", this could be an app-specific expression like
355 "mystreams[curlogger]". */
356
357static char *dprintf_channel = "";
358
359/* True if dprintf commands should continue to operate even if GDB
360 has disconnected. */
361static int disconnected_dprintf = 1;
362
5cea2a26
PA
363/* A reference-counted struct command_line. This lets multiple
364 breakpoints share a single command list. */
365struct counted_command_line
366{
367 /* The reference count. */
368 int refc;
369
370 /* The command list. */
371 struct command_line *commands;
372};
373
374struct command_line *
375breakpoint_commands (struct breakpoint *b)
376{
377 return b->commands ? b->commands->commands : NULL;
378}
3daf8fe5 379
f3b1572e
PA
380/* Flag indicating that a command has proceeded the inferior past the
381 current breakpoint. */
382
383static int breakpoint_proceeded;
384
956a9fb9 385const char *
2cec12e5
AR
386bpdisp_text (enum bpdisp disp)
387{
4a64f543
MS
388 /* NOTE: the following values are a part of MI protocol and
389 represent values of 'disp' field returned when inferior stops at
390 a breakpoint. */
bc043ef3 391 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 392
2cec12e5
AR
393 return bpdisps[(int) disp];
394}
c906108c 395
4a64f543 396/* Prototypes for exported functions. */
c906108c 397/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 398 if such is available. */
c906108c
SS
399static int can_use_hw_watchpoints;
400
920d2a44
AC
401static void
402show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
403 struct cmd_list_element *c,
404 const char *value)
405{
3e43a32a
MS
406 fprintf_filtered (file,
407 _("Debugger's willingness to use "
408 "watchpoint hardware is %s.\n"),
920d2a44
AC
409 value);
410}
411
fa8d40ab
JJ
412/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
413 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 414 for unrecognized breakpoint locations.
fa8d40ab
JJ
415 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
416static enum auto_boolean pending_break_support;
920d2a44
AC
417static void
418show_pending_break_support (struct ui_file *file, int from_tty,
419 struct cmd_list_element *c,
420 const char *value)
421{
3e43a32a
MS
422 fprintf_filtered (file,
423 _("Debugger's behavior regarding "
424 "pending breakpoints is %s.\n"),
920d2a44
AC
425 value);
426}
fa8d40ab 427
765dc015 428/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 429 set with "break" but falling in read-only memory.
765dc015
VP
430 If 0, gdb will warn about such breakpoints, but won't automatically
431 use hardware breakpoints. */
432static int automatic_hardware_breakpoints;
433static void
434show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c,
436 const char *value)
437{
3e43a32a
MS
438 fprintf_filtered (file,
439 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
440 value);
441}
442
a25a5a45
PA
443/* If on, GDB keeps breakpoints inserted even if the inferior is
444 stopped, and immediately inserts any new breakpoints as soon as
445 they're created. If off (default), GDB keeps breakpoints off of
446 the target as long as possible. That is, it delays inserting
447 breakpoints until the next resume, and removes them again when the
448 target fully stops. This is a bit safer in case GDB crashes while
449 processing user input. */
450static int always_inserted_mode = 0;
72d0e2c5 451
33e5cbd6 452static void
74960c60 453show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 454 struct cmd_list_element *c, const char *value)
74960c60 455{
a25a5a45
PA
456 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
457 value);
74960c60
VP
458}
459
b57bacec
PA
460/* See breakpoint.h. */
461
33e5cbd6 462int
a25a5a45 463breakpoints_should_be_inserted_now (void)
33e5cbd6 464{
a25a5a45
PA
465 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
466 {
467 /* If breakpoints are global, they should be inserted even if no
468 thread under gdb's control is running, or even if there are
469 no threads under GDB's control yet. */
470 return 1;
471 }
472 else if (target_has_execution)
473 {
a25a5a45
PA
474 if (always_inserted_mode)
475 {
476 /* The user wants breakpoints inserted even if all threads
477 are stopped. */
478 return 1;
479 }
480
b57bacec
PA
481 if (threads_are_executing ())
482 return 1;
a25a5a45
PA
483 }
484 return 0;
33e5cbd6 485}
765dc015 486
b775012e
LM
487static const char condition_evaluation_both[] = "host or target";
488
489/* Modes for breakpoint condition evaluation. */
490static const char condition_evaluation_auto[] = "auto";
491static const char condition_evaluation_host[] = "host";
492static const char condition_evaluation_target[] = "target";
493static const char *const condition_evaluation_enums[] = {
494 condition_evaluation_auto,
495 condition_evaluation_host,
496 condition_evaluation_target,
497 NULL
498};
499
500/* Global that holds the current mode for breakpoint condition evaluation. */
501static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
502
503/* Global that we use to display information to the user (gets its value from
504 condition_evaluation_mode_1. */
505static const char *condition_evaluation_mode = condition_evaluation_auto;
506
507/* Translate a condition evaluation mode MODE into either "host"
508 or "target". This is used mostly to translate from "auto" to the
509 real setting that is being used. It returns the translated
510 evaluation mode. */
511
512static const char *
513translate_condition_evaluation_mode (const char *mode)
514{
515 if (mode == condition_evaluation_auto)
516 {
517 if (target_supports_evaluation_of_breakpoint_conditions ())
518 return condition_evaluation_target;
519 else
520 return condition_evaluation_host;
521 }
522 else
523 return mode;
524}
525
526/* Discovers what condition_evaluation_auto translates to. */
527
528static const char *
529breakpoint_condition_evaluation_mode (void)
530{
531 return translate_condition_evaluation_mode (condition_evaluation_mode);
532}
533
534/* Return true if GDB should evaluate breakpoint conditions or false
535 otherwise. */
536
537static int
538gdb_evaluates_breakpoint_condition_p (void)
539{
540 const char *mode = breakpoint_condition_evaluation_mode ();
541
542 return (mode == condition_evaluation_host);
543}
544
a14ed312 545void _initialize_breakpoint (void);
c906108c 546
c906108c
SS
547/* Are we executing breakpoint commands? */
548static int executing_breakpoint_commands;
549
c02f5703
MS
550/* Are overlay event breakpoints enabled? */
551static int overlay_events_enabled;
552
e09342b5
TJB
553/* See description in breakpoint.h. */
554int target_exact_watchpoints = 0;
555
c906108c 556/* Walk the following statement or block through all breakpoints.
e5dd4106 557 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 558 current breakpoint. */
c906108c 559
5c44784c 560#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 561
5c44784c
JM
562#define ALL_BREAKPOINTS_SAFE(B,TMP) \
563 for (B = breakpoint_chain; \
564 B ? (TMP=B->next, 1): 0; \
565 B = TMP)
c906108c 566
4a64f543
MS
567/* Similar iterator for the low-level breakpoints. SAFE variant is
568 not provided so update_global_location_list must not be called
569 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 570
876fa593
JK
571#define ALL_BP_LOCATIONS(B,BP_TMP) \
572 for (BP_TMP = bp_location; \
573 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
574 BP_TMP++)
7cc221ef 575
b775012e
LM
576/* Iterates through locations with address ADDRESS for the currently selected
577 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
578 to where the loop should start from.
579 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
580 appropriate location to start with. */
581
582#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
583 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
584 BP_LOCP_TMP = BP_LOCP_START; \
585 BP_LOCP_START \
586 && (BP_LOCP_TMP < bp_location + bp_location_count \
587 && (*BP_LOCP_TMP)->address == ADDRESS); \
588 BP_LOCP_TMP++)
589
1042e4c0
SS
590/* Iterator for tracepoints only. */
591
592#define ALL_TRACEPOINTS(B) \
593 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 594 if (is_tracepoint (B))
1042e4c0 595
7cc221ef 596/* Chains of all breakpoints defined. */
c906108c
SS
597
598struct breakpoint *breakpoint_chain;
599
876fa593
JK
600/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
601
602static struct bp_location **bp_location;
603
604/* Number of elements of BP_LOCATION. */
605
606static unsigned bp_location_count;
607
4a64f543
MS
608/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
609 ADDRESS for the current elements of BP_LOCATION which get a valid
610 result from bp_location_has_shadow. You can use it for roughly
611 limiting the subrange of BP_LOCATION to scan for shadow bytes for
612 an address you need to read. */
876fa593
JK
613
614static CORE_ADDR bp_location_placed_address_before_address_max;
615
4a64f543
MS
616/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
617 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
618 BP_LOCATION which get a valid result from bp_location_has_shadow.
619 You can use it for roughly limiting the subrange of BP_LOCATION to
620 scan for shadow bytes for an address you need to read. */
876fa593
JK
621
622static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 623
4a64f543
MS
624/* The locations that no longer correspond to any breakpoint, unlinked
625 from bp_location array, but for which a hit may still be reported
626 by a target. */
20874c92
VP
627VEC(bp_location_p) *moribund_locations = NULL;
628
c906108c
SS
629/* Number of last breakpoint made. */
630
95a42b64
TT
631static int breakpoint_count;
632
86b17b60
PA
633/* The value of `breakpoint_count' before the last command that
634 created breakpoints. If the last (break-like) command created more
635 than one breakpoint, then the difference between BREAKPOINT_COUNT
636 and PREV_BREAKPOINT_COUNT is more than one. */
637static int prev_breakpoint_count;
c906108c 638
1042e4c0
SS
639/* Number of last tracepoint made. */
640
95a42b64 641static int tracepoint_count;
1042e4c0 642
6149aea9
PA
643static struct cmd_list_element *breakpoint_set_cmdlist;
644static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 645struct cmd_list_element *save_cmdlist;
6149aea9 646
468d015d
JJ
647/* Return whether a breakpoint is an active enabled breakpoint. */
648static int
649breakpoint_enabled (struct breakpoint *b)
650{
0d381245 651 return (b->enable_state == bp_enabled);
468d015d
JJ
652}
653
c906108c
SS
654/* Set breakpoint count to NUM. */
655
95a42b64 656static void
fba45db2 657set_breakpoint_count (int num)
c906108c 658{
86b17b60 659 prev_breakpoint_count = breakpoint_count;
c906108c 660 breakpoint_count = num;
4fa62494 661 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
662}
663
86b17b60
PA
664/* Used by `start_rbreak_breakpoints' below, to record the current
665 breakpoint count before "rbreak" creates any breakpoint. */
666static int rbreak_start_breakpoint_count;
667
95a42b64
TT
668/* Called at the start an "rbreak" command to record the first
669 breakpoint made. */
86b17b60 670
95a42b64
TT
671void
672start_rbreak_breakpoints (void)
673{
86b17b60 674 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
675}
676
677/* Called at the end of an "rbreak" command to record the last
678 breakpoint made. */
86b17b60 679
95a42b64
TT
680void
681end_rbreak_breakpoints (void)
682{
86b17b60 683 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
684}
685
4a64f543 686/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
687
688void
fba45db2 689clear_breakpoint_hit_counts (void)
c906108c
SS
690{
691 struct breakpoint *b;
692
693 ALL_BREAKPOINTS (b)
694 b->hit_count = 0;
695}
696
9add0f1b
TT
697/* Allocate a new counted_command_line with reference count of 1.
698 The new structure owns COMMANDS. */
699
700static struct counted_command_line *
701alloc_counted_command_line (struct command_line *commands)
702{
703 struct counted_command_line *result
704 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 705
9add0f1b
TT
706 result->refc = 1;
707 result->commands = commands;
708 return result;
709}
710
711/* Increment reference count. This does nothing if CMD is NULL. */
712
713static void
714incref_counted_command_line (struct counted_command_line *cmd)
715{
716 if (cmd)
717 ++cmd->refc;
718}
719
720/* Decrement reference count. If the reference count reaches 0,
721 destroy the counted_command_line. Sets *CMDP to NULL. This does
722 nothing if *CMDP is NULL. */
723
724static void
725decref_counted_command_line (struct counted_command_line **cmdp)
726{
727 if (*cmdp)
728 {
729 if (--(*cmdp)->refc == 0)
730 {
731 free_command_lines (&(*cmdp)->commands);
732 xfree (*cmdp);
733 }
734 *cmdp = NULL;
735 }
736}
737
738/* A cleanup function that calls decref_counted_command_line. */
739
740static void
741do_cleanup_counted_command_line (void *arg)
742{
743 decref_counted_command_line (arg);
744}
745
746/* Create a cleanup that calls decref_counted_command_line on the
747 argument. */
748
749static struct cleanup *
750make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
751{
752 return make_cleanup (do_cleanup_counted_command_line, cmdp);
753}
754
c906108c 755\f
48cb2d85
VP
756/* Return the breakpoint with the specified number, or NULL
757 if the number does not refer to an existing breakpoint. */
758
759struct breakpoint *
760get_breakpoint (int num)
761{
762 struct breakpoint *b;
763
764 ALL_BREAKPOINTS (b)
765 if (b->number == num)
766 return b;
767
768 return NULL;
769}
5c44784c 770
c906108c 771\f
adc36818 772
b775012e
LM
773/* Mark locations as "conditions have changed" in case the target supports
774 evaluating conditions on its side. */
775
776static void
777mark_breakpoint_modified (struct breakpoint *b)
778{
779 struct bp_location *loc;
780
781 /* This is only meaningful if the target is
782 evaluating conditions and if the user has
783 opted for condition evaluation on the target's
784 side. */
785 if (gdb_evaluates_breakpoint_condition_p ()
786 || !target_supports_evaluation_of_breakpoint_conditions ())
787 return;
788
789 if (!is_breakpoint (b))
790 return;
791
792 for (loc = b->loc; loc; loc = loc->next)
793 loc->condition_changed = condition_modified;
794}
795
796/* Mark location as "conditions have changed" in case the target supports
797 evaluating conditions on its side. */
798
799static void
800mark_breakpoint_location_modified (struct bp_location *loc)
801{
802 /* This is only meaningful if the target is
803 evaluating conditions and if the user has
804 opted for condition evaluation on the target's
805 side. */
806 if (gdb_evaluates_breakpoint_condition_p ()
807 || !target_supports_evaluation_of_breakpoint_conditions ())
808
809 return;
810
811 if (!is_breakpoint (loc->owner))
812 return;
813
814 loc->condition_changed = condition_modified;
815}
816
817/* Sets the condition-evaluation mode using the static global
818 condition_evaluation_mode. */
819
820static void
821set_condition_evaluation_mode (char *args, int from_tty,
822 struct cmd_list_element *c)
823{
b775012e
LM
824 const char *old_mode, *new_mode;
825
826 if ((condition_evaluation_mode_1 == condition_evaluation_target)
827 && !target_supports_evaluation_of_breakpoint_conditions ())
828 {
829 condition_evaluation_mode_1 = condition_evaluation_mode;
830 warning (_("Target does not support breakpoint condition evaluation.\n"
831 "Using host evaluation mode instead."));
832 return;
833 }
834
835 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
836 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
837
abf1152a
JK
838 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
839 settings was "auto". */
840 condition_evaluation_mode = condition_evaluation_mode_1;
841
b775012e
LM
842 /* Only update the mode if the user picked a different one. */
843 if (new_mode != old_mode)
844 {
845 struct bp_location *loc, **loc_tmp;
846 /* If the user switched to a different evaluation mode, we
847 need to synch the changes with the target as follows:
848
849 "host" -> "target": Send all (valid) conditions to the target.
850 "target" -> "host": Remove all the conditions from the target.
851 */
852
b775012e
LM
853 if (new_mode == condition_evaluation_target)
854 {
855 /* Mark everything modified and synch conditions with the
856 target. */
857 ALL_BP_LOCATIONS (loc, loc_tmp)
858 mark_breakpoint_location_modified (loc);
859 }
860 else
861 {
862 /* Manually mark non-duplicate locations to synch conditions
863 with the target. We do this to remove all the conditions the
864 target knows about. */
865 ALL_BP_LOCATIONS (loc, loc_tmp)
866 if (is_breakpoint (loc->owner) && loc->inserted)
867 loc->needs_update = 1;
868 }
869
870 /* Do the update. */
44702360 871 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
872 }
873
874 return;
875}
876
877/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
878 what "auto" is translating to. */
879
880static void
881show_condition_evaluation_mode (struct ui_file *file, int from_tty,
882 struct cmd_list_element *c, const char *value)
883{
884 if (condition_evaluation_mode == condition_evaluation_auto)
885 fprintf_filtered (file,
886 _("Breakpoint condition evaluation "
887 "mode is %s (currently %s).\n"),
888 value,
889 breakpoint_condition_evaluation_mode ());
890 else
891 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
892 value);
893}
894
895/* A comparison function for bp_location AP and BP that is used by
896 bsearch. This comparison function only cares about addresses, unlike
897 the more general bp_location_compare function. */
898
899static int
900bp_location_compare_addrs (const void *ap, const void *bp)
901{
902 struct bp_location *a = *(void **) ap;
903 struct bp_location *b = *(void **) bp;
904
905 if (a->address == b->address)
906 return 0;
907 else
908 return ((a->address > b->address) - (a->address < b->address));
909}
910
911/* Helper function to skip all bp_locations with addresses
912 less than ADDRESS. It returns the first bp_location that
913 is greater than or equal to ADDRESS. If none is found, just
914 return NULL. */
915
916static struct bp_location **
917get_first_locp_gte_addr (CORE_ADDR address)
918{
919 struct bp_location dummy_loc;
920 struct bp_location *dummy_locp = &dummy_loc;
921 struct bp_location **locp_found = NULL;
922
923 /* Initialize the dummy location's address field. */
924 memset (&dummy_loc, 0, sizeof (struct bp_location));
925 dummy_loc.address = address;
926
927 /* Find a close match to the first location at ADDRESS. */
928 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
929 sizeof (struct bp_location **),
930 bp_location_compare_addrs);
931
932 /* Nothing was found, nothing left to do. */
933 if (locp_found == NULL)
934 return NULL;
935
936 /* We may have found a location that is at ADDRESS but is not the first in the
937 location's list. Go backwards (if possible) and locate the first one. */
938 while ((locp_found - 1) >= bp_location
939 && (*(locp_found - 1))->address == address)
940 locp_found--;
941
942 return locp_found;
943}
944
adc36818
PM
945void
946set_breakpoint_condition (struct breakpoint *b, char *exp,
947 int from_tty)
948{
3a5c3e22
PA
949 xfree (b->cond_string);
950 b->cond_string = NULL;
adc36818 951
3a5c3e22 952 if (is_watchpoint (b))
adc36818 953 {
3a5c3e22
PA
954 struct watchpoint *w = (struct watchpoint *) b;
955
956 xfree (w->cond_exp);
957 w->cond_exp = NULL;
958 }
959 else
960 {
961 struct bp_location *loc;
962
963 for (loc = b->loc; loc; loc = loc->next)
964 {
965 xfree (loc->cond);
966 loc->cond = NULL;
b775012e
LM
967
968 /* No need to free the condition agent expression
969 bytecode (if we have one). We will handle this
970 when we go through update_global_location_list. */
3a5c3e22 971 }
adc36818 972 }
adc36818
PM
973
974 if (*exp == 0)
975 {
976 if (from_tty)
977 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
978 }
979 else
980 {
bbc13ae3 981 const char *arg = exp;
cc59ec59 982
adc36818
PM
983 /* I don't know if it matters whether this is the string the user
984 typed in or the decompiled expression. */
985 b->cond_string = xstrdup (arg);
986 b->condition_not_parsed = 0;
987
988 if (is_watchpoint (b))
989 {
3a5c3e22
PA
990 struct watchpoint *w = (struct watchpoint *) b;
991
adc36818
PM
992 innermost_block = NULL;
993 arg = exp;
1bb9788d 994 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
995 if (*arg)
996 error (_("Junk at end of expression"));
3a5c3e22 997 w->cond_exp_valid_block = innermost_block;
adc36818
PM
998 }
999 else
1000 {
3a5c3e22
PA
1001 struct bp_location *loc;
1002
adc36818
PM
1003 for (loc = b->loc; loc; loc = loc->next)
1004 {
1005 arg = exp;
1006 loc->cond =
1bb9788d
TT
1007 parse_exp_1 (&arg, loc->address,
1008 block_for_pc (loc->address), 0);
adc36818
PM
1009 if (*arg)
1010 error (_("Junk at end of expression"));
1011 }
1012 }
1013 }
b775012e
LM
1014 mark_breakpoint_modified (b);
1015
8d3788bd 1016 observer_notify_breakpoint_modified (b);
adc36818
PM
1017}
1018
d55637df
TT
1019/* Completion for the "condition" command. */
1020
1021static VEC (char_ptr) *
6f937416
PA
1022condition_completer (struct cmd_list_element *cmd,
1023 const char *text, const char *word)
d55637df 1024{
6f937416 1025 const char *space;
d55637df 1026
6f937416
PA
1027 text = skip_spaces_const (text);
1028 space = skip_to_space_const (text);
d55637df
TT
1029 if (*space == '\0')
1030 {
1031 int len;
1032 struct breakpoint *b;
1033 VEC (char_ptr) *result = NULL;
1034
1035 if (text[0] == '$')
1036 {
1037 /* We don't support completion of history indices. */
1038 if (isdigit (text[1]))
1039 return NULL;
1040 return complete_internalvar (&text[1]);
1041 }
1042
1043 /* We're completing the breakpoint number. */
1044 len = strlen (text);
1045
1046 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1047 {
1048 char number[50];
1049
1050 xsnprintf (number, sizeof (number), "%d", b->number);
1051
1052 if (strncmp (number, text, len) == 0)
1053 VEC_safe_push (char_ptr, result, xstrdup (number));
1054 }
d55637df
TT
1055
1056 return result;
1057 }
1058
1059 /* We're completing the expression part. */
6f937416 1060 text = skip_spaces_const (space);
d55637df
TT
1061 return expression_completer (cmd, text, word);
1062}
1063
c906108c
SS
1064/* condition N EXP -- set break condition of breakpoint N to EXP. */
1065
1066static void
fba45db2 1067condition_command (char *arg, int from_tty)
c906108c 1068{
52f0bd74 1069 struct breakpoint *b;
c906108c 1070 char *p;
52f0bd74 1071 int bnum;
c906108c
SS
1072
1073 if (arg == 0)
e2e0b3e5 1074 error_no_arg (_("breakpoint number"));
c906108c
SS
1075
1076 p = arg;
1077 bnum = get_number (&p);
5c44784c 1078 if (bnum == 0)
8a3fe4f8 1079 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1080
1081 ALL_BREAKPOINTS (b)
1082 if (b->number == bnum)
2f069f6f 1083 {
6dddc817
DE
1084 /* Check if this breakpoint has a "stop" method implemented in an
1085 extension language. This method and conditions entered into GDB
1086 from the CLI are mutually exclusive. */
1087 const struct extension_language_defn *extlang
1088 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1089
1090 if (extlang != NULL)
1091 {
1092 error (_("Only one stop condition allowed. There is currently"
1093 " a %s stop condition defined for this breakpoint."),
1094 ext_lang_capitalized_name (extlang));
1095 }
2566ad2d 1096 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1097
1098 if (is_breakpoint (b))
44702360 1099 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1100
2f069f6f
JB
1101 return;
1102 }
c906108c 1103
8a3fe4f8 1104 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1105}
1106
a7bdde9e
VP
1107/* Check that COMMAND do not contain commands that are suitable
1108 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1109 Throw if any such commands is found. */
1110
a7bdde9e
VP
1111static void
1112check_no_tracepoint_commands (struct command_line *commands)
1113{
1114 struct command_line *c;
cc59ec59 1115
a7bdde9e
VP
1116 for (c = commands; c; c = c->next)
1117 {
1118 int i;
1119
1120 if (c->control_type == while_stepping_control)
3e43a32a
MS
1121 error (_("The 'while-stepping' command can "
1122 "only be used for tracepoints"));
a7bdde9e
VP
1123
1124 for (i = 0; i < c->body_count; ++i)
1125 check_no_tracepoint_commands ((c->body_list)[i]);
1126
1127 /* Not that command parsing removes leading whitespace and comment
4a64f543 1128 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1129 command directly. */
1130 if (strstr (c->line, "collect ") == c->line)
1131 error (_("The 'collect' command can only be used for tracepoints"));
1132
51661e93
VP
1133 if (strstr (c->line, "teval ") == c->line)
1134 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1135 }
1136}
1137
d77f58be
SS
1138/* Encapsulate tests for different types of tracepoints. */
1139
d9b3f62e
PA
1140static int
1141is_tracepoint_type (enum bptype type)
1142{
1143 return (type == bp_tracepoint
1144 || type == bp_fast_tracepoint
1145 || type == bp_static_tracepoint);
1146}
1147
a7bdde9e 1148int
d77f58be 1149is_tracepoint (const struct breakpoint *b)
a7bdde9e 1150{
d9b3f62e 1151 return is_tracepoint_type (b->type);
a7bdde9e 1152}
d9b3f62e 1153
e5dd4106 1154/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1155 breakpoint. This function will throw an exception if a problem is
1156 found. */
48cb2d85 1157
95a42b64
TT
1158static void
1159validate_commands_for_breakpoint (struct breakpoint *b,
1160 struct command_line *commands)
48cb2d85 1161{
d77f58be 1162 if (is_tracepoint (b))
a7bdde9e 1163 {
c9a6ce02 1164 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1165 struct command_line *c;
1166 struct command_line *while_stepping = 0;
c9a6ce02
PA
1167
1168 /* Reset the while-stepping step count. The previous commands
1169 might have included a while-stepping action, while the new
1170 ones might not. */
1171 t->step_count = 0;
1172
1173 /* We need to verify that each top-level element of commands is
1174 valid for tracepoints, that there's at most one
1175 while-stepping element, and that the while-stepping's body
1176 has valid tracing commands excluding nested while-stepping.
1177 We also need to validate the tracepoint action line in the
1178 context of the tracepoint --- validate_actionline actually
1179 has side effects, like setting the tracepoint's
1180 while-stepping STEP_COUNT, in addition to checking if the
1181 collect/teval actions parse and make sense in the
1182 tracepoint's context. */
a7bdde9e
VP
1183 for (c = commands; c; c = c->next)
1184 {
a7bdde9e
VP
1185 if (c->control_type == while_stepping_control)
1186 {
1187 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1188 error (_("The 'while-stepping' command "
1189 "cannot be used for fast tracepoint"));
0fb4aa4b 1190 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1191 error (_("The 'while-stepping' command "
1192 "cannot be used for static tracepoint"));
a7bdde9e
VP
1193
1194 if (while_stepping)
3e43a32a
MS
1195 error (_("The 'while-stepping' command "
1196 "can be used only once"));
a7bdde9e
VP
1197 else
1198 while_stepping = c;
1199 }
c9a6ce02
PA
1200
1201 validate_actionline (c->line, b);
a7bdde9e
VP
1202 }
1203 if (while_stepping)
1204 {
1205 struct command_line *c2;
1206
1207 gdb_assert (while_stepping->body_count == 1);
1208 c2 = while_stepping->body_list[0];
1209 for (; c2; c2 = c2->next)
1210 {
a7bdde9e
VP
1211 if (c2->control_type == while_stepping_control)
1212 error (_("The 'while-stepping' command cannot be nested"));
1213 }
1214 }
1215 }
1216 else
1217 {
1218 check_no_tracepoint_commands (commands);
1219 }
95a42b64
TT
1220}
1221
0fb4aa4b
PA
1222/* Return a vector of all the static tracepoints set at ADDR. The
1223 caller is responsible for releasing the vector. */
1224
1225VEC(breakpoint_p) *
1226static_tracepoints_here (CORE_ADDR addr)
1227{
1228 struct breakpoint *b;
1229 VEC(breakpoint_p) *found = 0;
1230 struct bp_location *loc;
1231
1232 ALL_BREAKPOINTS (b)
1233 if (b->type == bp_static_tracepoint)
1234 {
1235 for (loc = b->loc; loc; loc = loc->next)
1236 if (loc->address == addr)
1237 VEC_safe_push(breakpoint_p, found, b);
1238 }
1239
1240 return found;
1241}
1242
95a42b64 1243/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1244 validate that only allowed commands are included. */
95a42b64
TT
1245
1246void
4a64f543
MS
1247breakpoint_set_commands (struct breakpoint *b,
1248 struct command_line *commands)
95a42b64
TT
1249{
1250 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1251
9add0f1b
TT
1252 decref_counted_command_line (&b->commands);
1253 b->commands = alloc_counted_command_line (commands);
8d3788bd 1254 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1255}
1256
45a43567
TT
1257/* Set the internal `silent' flag on the breakpoint. Note that this
1258 is not the same as the "silent" that may appear in the breakpoint's
1259 commands. */
1260
1261void
1262breakpoint_set_silent (struct breakpoint *b, int silent)
1263{
1264 int old_silent = b->silent;
1265
1266 b->silent = silent;
1267 if (old_silent != silent)
8d3788bd 1268 observer_notify_breakpoint_modified (b);
45a43567
TT
1269}
1270
1271/* Set the thread for this breakpoint. If THREAD is -1, make the
1272 breakpoint work for any thread. */
1273
1274void
1275breakpoint_set_thread (struct breakpoint *b, int thread)
1276{
1277 int old_thread = b->thread;
1278
1279 b->thread = thread;
1280 if (old_thread != thread)
8d3788bd 1281 observer_notify_breakpoint_modified (b);
45a43567
TT
1282}
1283
1284/* Set the task for this breakpoint. If TASK is 0, make the
1285 breakpoint work for any task. */
1286
1287void
1288breakpoint_set_task (struct breakpoint *b, int task)
1289{
1290 int old_task = b->task;
1291
1292 b->task = task;
1293 if (old_task != task)
8d3788bd 1294 observer_notify_breakpoint_modified (b);
45a43567
TT
1295}
1296
95a42b64
TT
1297void
1298check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1299{
1300 struct breakpoint *b = closure;
cc59ec59 1301
6f937416 1302 validate_actionline (line, b);
a7bdde9e
VP
1303}
1304
95a42b64
TT
1305/* A structure used to pass information through
1306 map_breakpoint_numbers. */
1307
1308struct commands_info
1309{
1310 /* True if the command was typed at a tty. */
1311 int from_tty;
86b17b60
PA
1312
1313 /* The breakpoint range spec. */
1314 char *arg;
1315
95a42b64
TT
1316 /* Non-NULL if the body of the commands are being read from this
1317 already-parsed command. */
1318 struct command_line *control;
86b17b60 1319
95a42b64
TT
1320 /* The command lines read from the user, or NULL if they have not
1321 yet been read. */
1322 struct counted_command_line *cmd;
1323};
1324
1325/* A callback for map_breakpoint_numbers that sets the commands for
1326 commands_command. */
1327
c906108c 1328static void
95a42b64 1329do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1330{
95a42b64 1331 struct commands_info *info = data;
c906108c 1332
95a42b64
TT
1333 if (info->cmd == NULL)
1334 {
1335 struct command_line *l;
5c44784c 1336
95a42b64
TT
1337 if (info->control != NULL)
1338 l = copy_command_lines (info->control->body_list[0]);
1339 else
86b17b60
PA
1340 {
1341 struct cleanup *old_chain;
1342 char *str;
c5aa993b 1343
3e43a32a
MS
1344 str = xstrprintf (_("Type commands for breakpoint(s) "
1345 "%s, one per line."),
86b17b60
PA
1346 info->arg);
1347
1348 old_chain = make_cleanup (xfree, str);
1349
1350 l = read_command_lines (str,
1351 info->from_tty, 1,
d77f58be 1352 (is_tracepoint (b)
86b17b60
PA
1353 ? check_tracepoint_command : 0),
1354 b);
1355
1356 do_cleanups (old_chain);
1357 }
a7bdde9e 1358
95a42b64
TT
1359 info->cmd = alloc_counted_command_line (l);
1360 }
1361
1362 /* If a breakpoint was on the list more than once, we don't need to
1363 do anything. */
1364 if (b->commands != info->cmd)
1365 {
1366 validate_commands_for_breakpoint (b, info->cmd->commands);
1367 incref_counted_command_line (info->cmd);
1368 decref_counted_command_line (&b->commands);
1369 b->commands = info->cmd;
8d3788bd 1370 observer_notify_breakpoint_modified (b);
c5aa993b 1371 }
95a42b64
TT
1372}
1373
1374static void
4a64f543
MS
1375commands_command_1 (char *arg, int from_tty,
1376 struct command_line *control)
95a42b64
TT
1377{
1378 struct cleanup *cleanups;
1379 struct commands_info info;
1380
1381 info.from_tty = from_tty;
1382 info.control = control;
1383 info.cmd = NULL;
1384 /* If we read command lines from the user, then `info' will hold an
1385 extra reference to the commands that we must clean up. */
1386 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1387
1388 if (arg == NULL || !*arg)
1389 {
86b17b60 1390 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1391 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1392 breakpoint_count);
95a42b64
TT
1393 else if (breakpoint_count > 0)
1394 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1395 else
1396 {
1397 /* So that we don't try to free the incoming non-NULL
1398 argument in the cleanup below. Mapping breakpoint
1399 numbers will fail in this case. */
1400 arg = NULL;
1401 }
95a42b64 1402 }
9766ced4
SS
1403 else
1404 /* The command loop has some static state, so we need to preserve
1405 our argument. */
1406 arg = xstrdup (arg);
86b17b60
PA
1407
1408 if (arg != NULL)
1409 make_cleanup (xfree, arg);
1410
1411 info.arg = arg;
95a42b64
TT
1412
1413 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1414
1415 if (info.cmd == NULL)
1416 error (_("No breakpoints specified."));
1417
1418 do_cleanups (cleanups);
1419}
1420
1421static void
1422commands_command (char *arg, int from_tty)
1423{
1424 commands_command_1 (arg, from_tty, NULL);
c906108c 1425}
40c03ae8
EZ
1426
1427/* Like commands_command, but instead of reading the commands from
1428 input stream, takes them from an already parsed command structure.
1429
1430 This is used by cli-script.c to DTRT with breakpoint commands
1431 that are part of if and while bodies. */
1432enum command_control_type
1433commands_from_control_command (char *arg, struct command_line *cmd)
1434{
95a42b64
TT
1435 commands_command_1 (arg, 0, cmd);
1436 return simple_control;
40c03ae8 1437}
876fa593
JK
1438
1439/* Return non-zero if BL->TARGET_INFO contains valid information. */
1440
1441static int
1442bp_location_has_shadow (struct bp_location *bl)
1443{
1444 if (bl->loc_type != bp_loc_software_breakpoint)
1445 return 0;
1446 if (!bl->inserted)
1447 return 0;
1448 if (bl->target_info.shadow_len == 0)
e5dd4106 1449 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1450 return 0;
1451 return 1;
1452}
1453
9d497a19
PA
1454/* Update BUF, which is LEN bytes read from the target address
1455 MEMADDR, by replacing a memory breakpoint with its shadowed
1456 contents.
1457
1458 If READBUF is not NULL, this buffer must not overlap with the of
1459 the breakpoint location's shadow_contents buffer. Otherwise, a
1460 failed assertion internal error will be raised. */
1461
1462static void
1463one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1464 const gdb_byte *writebuf_org,
1465 ULONGEST memaddr, LONGEST len,
1466 struct bp_target_info *target_info,
1467 struct gdbarch *gdbarch)
1468{
1469 /* Now do full processing of the found relevant range of elements. */
1470 CORE_ADDR bp_addr = 0;
1471 int bp_size = 0;
1472 int bptoffset = 0;
1473
1474 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1475 current_program_space->aspace, 0))
1476 {
1477 /* The breakpoint is inserted in a different address space. */
1478 return;
1479 }
1480
1481 /* Addresses and length of the part of the breakpoint that
1482 we need to copy. */
1483 bp_addr = target_info->placed_address;
1484 bp_size = target_info->shadow_len;
1485
1486 if (bp_addr + bp_size <= memaddr)
1487 {
1488 /* The breakpoint is entirely before the chunk of memory we are
1489 reading. */
1490 return;
1491 }
1492
1493 if (bp_addr >= memaddr + len)
1494 {
1495 /* The breakpoint is entirely after the chunk of memory we are
1496 reading. */
1497 return;
1498 }
1499
1500 /* Offset within shadow_contents. */
1501 if (bp_addr < memaddr)
1502 {
1503 /* Only copy the second part of the breakpoint. */
1504 bp_size -= memaddr - bp_addr;
1505 bptoffset = memaddr - bp_addr;
1506 bp_addr = memaddr;
1507 }
1508
1509 if (bp_addr + bp_size > memaddr + len)
1510 {
1511 /* Only copy the first part of the breakpoint. */
1512 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1513 }
1514
1515 if (readbuf != NULL)
1516 {
1517 /* Verify that the readbuf buffer does not overlap with the
1518 shadow_contents buffer. */
1519 gdb_assert (target_info->shadow_contents >= readbuf + len
1520 || readbuf >= (target_info->shadow_contents
1521 + target_info->shadow_len));
1522
1523 /* Update the read buffer with this inserted breakpoint's
1524 shadow. */
1525 memcpy (readbuf + bp_addr - memaddr,
1526 target_info->shadow_contents + bptoffset, bp_size);
1527 }
1528 else
1529 {
1530 const unsigned char *bp;
0d5ed153
MR
1531 CORE_ADDR addr = target_info->reqstd_address;
1532 int placed_size;
9d497a19
PA
1533
1534 /* Update the shadow with what we want to write to memory. */
1535 memcpy (target_info->shadow_contents + bptoffset,
1536 writebuf_org + bp_addr - memaddr, bp_size);
1537
1538 /* Determine appropriate breakpoint contents and size for this
1539 address. */
0d5ed153 1540 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1541
1542 /* Update the final write buffer with this inserted
1543 breakpoint's INSN. */
1544 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1545 }
1546}
1547
8defab1a 1548/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1549 by replacing any memory breakpoints with their shadowed contents.
1550
35c63cd8
JB
1551 If READBUF is not NULL, this buffer must not overlap with any of
1552 the breakpoint location's shadow_contents buffers. Otherwise,
1553 a failed assertion internal error will be raised.
1554
876fa593 1555 The range of shadowed area by each bp_location is:
35df4500
TJB
1556 bl->address - bp_location_placed_address_before_address_max
1557 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1558 The range we were requested to resolve shadows for is:
1559 memaddr ... memaddr + len
1560 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1561 memaddr + len <= (bl->address
1562 - bp_location_placed_address_before_address_max)
876fa593 1563 and:
35df4500 1564 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1565
8defab1a 1566void
f0ba3972
PA
1567breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1568 const gdb_byte *writebuf_org,
1569 ULONGEST memaddr, LONGEST len)
c906108c 1570{
4a64f543
MS
1571 /* Left boundary, right boundary and median element of our binary
1572 search. */
876fa593 1573 unsigned bc_l, bc_r, bc;
9d497a19 1574 size_t i;
876fa593 1575
4a64f543
MS
1576 /* Find BC_L which is a leftmost element which may affect BUF
1577 content. It is safe to report lower value but a failure to
1578 report higher one. */
876fa593
JK
1579
1580 bc_l = 0;
1581 bc_r = bp_location_count;
1582 while (bc_l + 1 < bc_r)
1583 {
35df4500 1584 struct bp_location *bl;
876fa593
JK
1585
1586 bc = (bc_l + bc_r) / 2;
35df4500 1587 bl = bp_location[bc];
876fa593 1588
4a64f543
MS
1589 /* Check first BL->ADDRESS will not overflow due to the added
1590 constant. Then advance the left boundary only if we are sure
1591 the BC element can in no way affect the BUF content (MEMADDR
1592 to MEMADDR + LEN range).
876fa593 1593
4a64f543
MS
1594 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1595 offset so that we cannot miss a breakpoint with its shadow
1596 range tail still reaching MEMADDR. */
c5aa993b 1597
35df4500
TJB
1598 if ((bl->address + bp_location_shadow_len_after_address_max
1599 >= bl->address)
1600 && (bl->address + bp_location_shadow_len_after_address_max
1601 <= memaddr))
876fa593
JK
1602 bc_l = bc;
1603 else
1604 bc_r = bc;
1605 }
1606
128070bb
PA
1607 /* Due to the binary search above, we need to make sure we pick the
1608 first location that's at BC_L's address. E.g., if there are
1609 multiple locations at the same address, BC_L may end up pointing
1610 at a duplicate location, and miss the "master"/"inserted"
1611 location. Say, given locations L1, L2 and L3 at addresses A and
1612 B:
1613
1614 L1@A, L2@A, L3@B, ...
1615
1616 BC_L could end up pointing at location L2, while the "master"
1617 location could be L1. Since the `loc->inserted' flag is only set
1618 on "master" locations, we'd forget to restore the shadow of L1
1619 and L2. */
1620 while (bc_l > 0
1621 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1622 bc_l--;
1623
876fa593
JK
1624 /* Now do full processing of the found relevant range of elements. */
1625
1626 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1627 {
35df4500 1628 struct bp_location *bl = bp_location[bc];
876fa593
JK
1629 CORE_ADDR bp_addr = 0;
1630 int bp_size = 0;
1631 int bptoffset = 0;
1632
35df4500
TJB
1633 /* bp_location array has BL->OWNER always non-NULL. */
1634 if (bl->owner->type == bp_none)
8a3fe4f8 1635 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1636 bl->owner->number);
ffce0d52 1637
e5dd4106 1638 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1639 content. */
1640
35df4500
TJB
1641 if (bl->address >= bp_location_placed_address_before_address_max
1642 && memaddr + len <= (bl->address
1643 - bp_location_placed_address_before_address_max))
876fa593
JK
1644 break;
1645
35df4500 1646 if (!bp_location_has_shadow (bl))
c5aa993b 1647 continue;
6c95b8df 1648
9d497a19
PA
1649 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1650 memaddr, len, &bl->target_info, bl->gdbarch);
1651 }
c906108c 1652}
9d497a19 1653
c906108c 1654\f
c5aa993b 1655
b775012e
LM
1656/* Return true if BPT is either a software breakpoint or a hardware
1657 breakpoint. */
1658
1659int
1660is_breakpoint (const struct breakpoint *bpt)
1661{
1662 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1663 || bpt->type == bp_hardware_breakpoint
1664 || bpt->type == bp_dprintf);
b775012e
LM
1665}
1666
60e1c644
PA
1667/* Return true if BPT is of any hardware watchpoint kind. */
1668
a5606eee 1669static int
d77f58be 1670is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1671{
1672 return (bpt->type == bp_hardware_watchpoint
1673 || bpt->type == bp_read_watchpoint
1674 || bpt->type == bp_access_watchpoint);
1675}
7270d8f2 1676
60e1c644
PA
1677/* Return true if BPT is of any watchpoint kind, hardware or
1678 software. */
1679
3a5c3e22 1680int
d77f58be 1681is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1682{
1683 return (is_hardware_watchpoint (bpt)
1684 || bpt->type == bp_watchpoint);
1685}
1686
3a5c3e22
PA
1687/* Returns true if the current thread and its running state are safe
1688 to evaluate or update watchpoint B. Watchpoints on local
1689 expressions need to be evaluated in the context of the thread that
1690 was current when the watchpoint was created, and, that thread needs
1691 to be stopped to be able to select the correct frame context.
1692 Watchpoints on global expressions can be evaluated on any thread,
1693 and in any state. It is presently left to the target allowing
1694 memory accesses when threads are running. */
f6bc2008
PA
1695
1696static int
3a5c3e22 1697watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1698{
d0d8b0c6
JK
1699 return (b->base.pspace == current_program_space
1700 && (ptid_equal (b->watchpoint_thread, null_ptid)
1701 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1702 && !is_executing (inferior_ptid))));
f6bc2008
PA
1703}
1704
d0fb5eae
JK
1705/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1706 associated bp_watchpoint_scope breakpoint. */
1707
1708static void
3a5c3e22 1709watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1710{
3a5c3e22 1711 struct breakpoint *b = &w->base;
d0fb5eae
JK
1712
1713 if (b->related_breakpoint != b)
1714 {
1715 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1716 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1717 b->related_breakpoint->disposition = disp_del_at_next_stop;
1718 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1719 b->related_breakpoint = b;
1720 }
1721 b->disposition = disp_del_at_next_stop;
1722}
1723
bb9d5f81
PP
1724/* Extract a bitfield value from value VAL using the bit parameters contained in
1725 watchpoint W. */
1726
1727static struct value *
1728extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1729{
1730 struct value *bit_val;
1731
1732 if (val == NULL)
1733 return NULL;
1734
1735 bit_val = allocate_value (value_type (val));
1736
1737 unpack_value_bitfield (bit_val,
1738 w->val_bitpos,
1739 w->val_bitsize,
1740 value_contents_for_printing (val),
1741 value_offset (val),
1742 val);
1743
1744 return bit_val;
1745}
1746
567e1b4e
JB
1747/* Assuming that B is a watchpoint:
1748 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1749 - Evaluate expression and store the result in B->val
567e1b4e
JB
1750 - Evaluate the condition if there is one, and store the result
1751 in b->loc->cond.
a5606eee
VP
1752 - Update the list of values that must be watched in B->loc.
1753
4a64f543
MS
1754 If the watchpoint disposition is disp_del_at_next_stop, then do
1755 nothing. If this is local watchpoint that is out of scope, delete
1756 it.
1757
1758 Even with `set breakpoint always-inserted on' the watchpoints are
1759 removed + inserted on each stop here. Normal breakpoints must
1760 never be removed because they might be missed by a running thread
1761 when debugging in non-stop mode. On the other hand, hardware
1762 watchpoints (is_hardware_watchpoint; processed here) are specific
1763 to each LWP since they are stored in each LWP's hardware debug
1764 registers. Therefore, such LWP must be stopped first in order to
1765 be able to modify its hardware watchpoints.
1766
1767 Hardware watchpoints must be reset exactly once after being
1768 presented to the user. It cannot be done sooner, because it would
1769 reset the data used to present the watchpoint hit to the user. And
1770 it must not be done later because it could display the same single
1771 watchpoint hit during multiple GDB stops. Note that the latter is
1772 relevant only to the hardware watchpoint types bp_read_watchpoint
1773 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1774 not user-visible - its hit is suppressed if the memory content has
1775 not changed.
1776
1777 The following constraints influence the location where we can reset
1778 hardware watchpoints:
1779
1780 * target_stopped_by_watchpoint and target_stopped_data_address are
1781 called several times when GDB stops.
1782
1783 [linux]
1784 * Multiple hardware watchpoints can be hit at the same time,
1785 causing GDB to stop. GDB only presents one hardware watchpoint
1786 hit at a time as the reason for stopping, and all the other hits
1787 are presented later, one after the other, each time the user
1788 requests the execution to be resumed. Execution is not resumed
1789 for the threads still having pending hit event stored in
1790 LWP_INFO->STATUS. While the watchpoint is already removed from
1791 the inferior on the first stop the thread hit event is kept being
1792 reported from its cached value by linux_nat_stopped_data_address
1793 until the real thread resume happens after the watchpoint gets
1794 presented and thus its LWP_INFO->STATUS gets reset.
1795
1796 Therefore the hardware watchpoint hit can get safely reset on the
1797 watchpoint removal from inferior. */
a79d3c27 1798
b40ce68a 1799static void
3a5c3e22 1800update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1801{
a5606eee 1802 int within_current_scope;
a5606eee 1803 struct frame_id saved_frame_id;
66076460 1804 int frame_saved;
a5606eee 1805
f6bc2008
PA
1806 /* If this is a local watchpoint, we only want to check if the
1807 watchpoint frame is in scope if the current thread is the thread
1808 that was used to create the watchpoint. */
1809 if (!watchpoint_in_thread_scope (b))
1810 return;
1811
3a5c3e22 1812 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1813 return;
1814
66076460 1815 frame_saved = 0;
a5606eee
VP
1816
1817 /* Determine if the watchpoint is within scope. */
1818 if (b->exp_valid_block == NULL)
1819 within_current_scope = 1;
1820 else
1821 {
b5db5dfc
UW
1822 struct frame_info *fi = get_current_frame ();
1823 struct gdbarch *frame_arch = get_frame_arch (fi);
1824 CORE_ADDR frame_pc = get_frame_pc (fi);
1825
1826 /* If we're in a function epilogue, unwinding may not work
1827 properly, so do not attempt to recreate locations at this
1828 point. See similar comments in watchpoint_check. */
1829 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1830 return;
66076460
DJ
1831
1832 /* Save the current frame's ID so we can restore it after
1833 evaluating the watchpoint expression on its own frame. */
1834 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1835 took a frame parameter, so that we didn't have to change the
1836 selected frame. */
1837 frame_saved = 1;
1838 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1839
a5606eee
VP
1840 fi = frame_find_by_id (b->watchpoint_frame);
1841 within_current_scope = (fi != NULL);
1842 if (within_current_scope)
1843 select_frame (fi);
1844 }
1845
b5db5dfc
UW
1846 /* We don't free locations. They are stored in the bp_location array
1847 and update_global_location_list will eventually delete them and
1848 remove breakpoints if needed. */
3a5c3e22 1849 b->base.loc = NULL;
b5db5dfc 1850
a5606eee
VP
1851 if (within_current_scope && reparse)
1852 {
bbc13ae3 1853 const char *s;
d63d0675 1854
a5606eee
VP
1855 if (b->exp)
1856 {
1857 xfree (b->exp);
1858 b->exp = NULL;
1859 }
d63d0675 1860 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1861 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1862 /* If the meaning of expression itself changed, the old value is
1863 no longer relevant. We don't want to report a watchpoint hit
1864 to the user when the old value and the new value may actually
1865 be completely different objects. */
1866 value_free (b->val);
fa4727a6
DJ
1867 b->val = NULL;
1868 b->val_valid = 0;
60e1c644
PA
1869
1870 /* Note that unlike with breakpoints, the watchpoint's condition
1871 expression is stored in the breakpoint object, not in the
1872 locations (re)created below. */
3a5c3e22 1873 if (b->base.cond_string != NULL)
60e1c644
PA
1874 {
1875 if (b->cond_exp != NULL)
1876 {
1877 xfree (b->cond_exp);
1878 b->cond_exp = NULL;
1879 }
1880
3a5c3e22 1881 s = b->base.cond_string;
1bb9788d 1882 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1883 }
a5606eee 1884 }
a5606eee
VP
1885
1886 /* If we failed to parse the expression, for example because
1887 it refers to a global variable in a not-yet-loaded shared library,
1888 don't try to insert watchpoint. We don't automatically delete
1889 such watchpoint, though, since failure to parse expression
1890 is different from out-of-scope watchpoint. */
e8369a73 1891 if (!target_has_execution)
2d134ed3
PA
1892 {
1893 /* Without execution, memory can't change. No use to try and
1894 set watchpoint locations. The watchpoint will be reset when
1895 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1896 if (!can_use_hw_watchpoints)
1897 {
1898 if (b->base.ops->works_in_software_mode (&b->base))
1899 b->base.type = bp_watchpoint;
1900 else
638aa5a1
AB
1901 error (_("Can't set read/access watchpoint when "
1902 "hardware watchpoints are disabled."));
e8369a73 1903 }
2d134ed3
PA
1904 }
1905 else if (within_current_scope && b->exp)
a5606eee 1906 {
0cf6dd15 1907 int pc = 0;
fa4727a6 1908 struct value *val_chain, *v, *result, *next;
2d134ed3 1909 struct program_space *frame_pspace;
a5606eee 1910
3a1115a0 1911 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1912
a5606eee
VP
1913 /* Avoid setting b->val if it's already set. The meaning of
1914 b->val is 'the last value' user saw, and we should update
1915 it only if we reported that last value to user. As it
9c06b0b4
TJB
1916 happens, the code that reports it updates b->val directly.
1917 We don't keep track of the memory value for masked
1918 watchpoints. */
3a5c3e22 1919 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1920 {
bb9d5f81
PP
1921 if (b->val_bitsize != 0)
1922 {
1923 v = extract_bitfield_from_watchpoint_value (b, v);
1924 if (v != NULL)
1925 release_value (v);
1926 }
fa4727a6
DJ
1927 b->val = v;
1928 b->val_valid = 1;
1929 }
a5606eee 1930
2d134ed3
PA
1931 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1932
a5606eee 1933 /* Look at each value on the value chain. */
9fa40276 1934 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1935 {
1936 /* If it's a memory location, and GDB actually needed
1937 its contents to evaluate the expression, then we
fa4727a6
DJ
1938 must watch it. If the first value returned is
1939 still lazy, that means an error occurred reading it;
1940 watch it anyway in case it becomes readable. */
a5606eee 1941 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1942 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1943 {
1944 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1945
a5606eee
VP
1946 /* We only watch structs and arrays if user asked
1947 for it explicitly, never if they just happen to
1948 appear in the middle of some value chain. */
fa4727a6 1949 if (v == result
a5606eee
VP
1950 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1951 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1952 {
1953 CORE_ADDR addr;
744a8059 1954 int type;
a5606eee 1955 struct bp_location *loc, **tmp;
bb9d5f81
PP
1956 int bitpos = 0, bitsize = 0;
1957
1958 if (value_bitsize (v) != 0)
1959 {
1960 /* Extract the bit parameters out from the bitfield
1961 sub-expression. */
1962 bitpos = value_bitpos (v);
1963 bitsize = value_bitsize (v);
1964 }
1965 else if (v == result && b->val_bitsize != 0)
1966 {
1967 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1968 lvalue whose bit parameters are saved in the fields
1969 VAL_BITPOS and VAL_BITSIZE. */
1970 bitpos = b->val_bitpos;
1971 bitsize = b->val_bitsize;
1972 }
a5606eee 1973
42ae5230 1974 addr = value_address (v);
bb9d5f81
PP
1975 if (bitsize != 0)
1976 {
1977 /* Skip the bytes that don't contain the bitfield. */
1978 addr += bitpos / 8;
1979 }
1980
a5606eee 1981 type = hw_write;
3a5c3e22 1982 if (b->base.type == bp_read_watchpoint)
a5606eee 1983 type = hw_read;
3a5c3e22 1984 else if (b->base.type == bp_access_watchpoint)
a5606eee 1985 type = hw_access;
3a5c3e22
PA
1986
1987 loc = allocate_bp_location (&b->base);
1988 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1989 ;
1990 *tmp = loc;
a6d9a66e 1991 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1992
1993 loc->pspace = frame_pspace;
a5606eee 1994 loc->address = addr;
bb9d5f81
PP
1995
1996 if (bitsize != 0)
1997 {
1998 /* Just cover the bytes that make up the bitfield. */
1999 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2000 }
2001 else
2002 loc->length = TYPE_LENGTH (value_type (v));
2003
a5606eee
VP
2004 loc->watchpoint_type = type;
2005 }
2006 }
9fa40276
TJB
2007 }
2008
2009 /* Change the type of breakpoint between hardware assisted or
2010 an ordinary watchpoint depending on the hardware support
2011 and free hardware slots. REPARSE is set when the inferior
2012 is started. */
a9634178 2013 if (reparse)
9fa40276 2014 {
e09342b5 2015 int reg_cnt;
9fa40276
TJB
2016 enum bp_loc_type loc_type;
2017 struct bp_location *bl;
a5606eee 2018
a9634178 2019 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2020
2021 if (reg_cnt)
9fa40276
TJB
2022 {
2023 int i, target_resources_ok, other_type_used;
a1398e0c 2024 enum bptype type;
9fa40276 2025
a9634178
TJB
2026 /* Use an exact watchpoint when there's only one memory region to be
2027 watched, and only one debug register is needed to watch it. */
2028 b->exact = target_exact_watchpoints && reg_cnt == 1;
2029
9fa40276 2030 /* We need to determine how many resources are already
e09342b5
TJB
2031 used for all other hardware watchpoints plus this one
2032 to see if we still have enough resources to also fit
a1398e0c
PA
2033 this watchpoint in as well. */
2034
2035 /* If this is a software watchpoint, we try to turn it
2036 to a hardware one -- count resources as if B was of
2037 hardware watchpoint type. */
2038 type = b->base.type;
2039 if (type == bp_watchpoint)
2040 type = bp_hardware_watchpoint;
2041
2042 /* This watchpoint may or may not have been placed on
2043 the list yet at this point (it won't be in the list
2044 if we're trying to create it for the first time,
2045 through watch_command), so always account for it
2046 manually. */
2047
2048 /* Count resources used by all watchpoints except B. */
2049 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2050
2051 /* Add in the resources needed for B. */
2052 i += hw_watchpoint_use_count (&b->base);
2053
2054 target_resources_ok
2055 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2056 if (target_resources_ok <= 0)
a9634178 2057 {
3a5c3e22 2058 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2059
2060 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2061 error (_("Target does not support this type of "
2062 "hardware watchpoint."));
9c06b0b4
TJB
2063 else if (target_resources_ok < 0 && !sw_mode)
2064 error (_("There are not enough available hardware "
2065 "resources for this watchpoint."));
a1398e0c
PA
2066
2067 /* Downgrade to software watchpoint. */
2068 b->base.type = bp_watchpoint;
2069 }
2070 else
2071 {
2072 /* If this was a software watchpoint, we've just
2073 found we have enough resources to turn it to a
2074 hardware watchpoint. Otherwise, this is a
2075 nop. */
2076 b->base.type = type;
a9634178 2077 }
9fa40276 2078 }
3a5c3e22 2079 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2080 {
2081 if (!can_use_hw_watchpoints)
2082 error (_("Can't set read/access watchpoint when "
2083 "hardware watchpoints are disabled."));
2084 else
2085 error (_("Expression cannot be implemented with "
2086 "read/access watchpoint."));
2087 }
9fa40276 2088 else
3a5c3e22 2089 b->base.type = bp_watchpoint;
9fa40276 2090
3a5c3e22 2091 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2092 : bp_loc_hardware_watchpoint);
3a5c3e22 2093 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2094 bl->loc_type = loc_type;
2095 }
2096
2097 for (v = val_chain; v; v = next)
2098 {
a5606eee
VP
2099 next = value_next (v);
2100 if (v != b->val)
2101 value_free (v);
2102 }
2103
c7437ca6
PA
2104 /* If a software watchpoint is not watching any memory, then the
2105 above left it without any location set up. But,
2106 bpstat_stop_status requires a location to be able to report
2107 stops, so make sure there's at least a dummy one. */
3a5c3e22 2108 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2109 {
3a5c3e22
PA
2110 struct breakpoint *base = &b->base;
2111 base->loc = allocate_bp_location (base);
2112 base->loc->pspace = frame_pspace;
2113 base->loc->address = -1;
2114 base->loc->length = -1;
2115 base->loc->watchpoint_type = -1;
c7437ca6 2116 }
a5606eee
VP
2117 }
2118 else if (!within_current_scope)
7270d8f2 2119 {
ac74f770
MS
2120 printf_filtered (_("\
2121Watchpoint %d deleted because the program has left the block\n\
2122in which its expression is valid.\n"),
3a5c3e22 2123 b->base.number);
d0fb5eae 2124 watchpoint_del_at_next_stop (b);
7270d8f2 2125 }
a5606eee
VP
2126
2127 /* Restore the selected frame. */
66076460
DJ
2128 if (frame_saved)
2129 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2130}
2131
a5606eee 2132
74960c60 2133/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2134 inserted in the inferior. We don't differentiate the type of BL's owner
2135 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2136 breakpoint_ops is not defined, because in insert_bp_location,
2137 tracepoint's insert_location will not be called. */
74960c60 2138static int
35df4500 2139should_be_inserted (struct bp_location *bl)
74960c60 2140{
35df4500 2141 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2142 return 0;
2143
35df4500 2144 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2145 return 0;
2146
35df4500 2147 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2148 return 0;
2149
f8eba3c6
TT
2150 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2151 return 0;
2152
56710373
PA
2153 /* This is set for example, when we're attached to the parent of a
2154 vfork, and have detached from the child. The child is running
2155 free, and we expect it to do an exec or exit, at which point the
2156 OS makes the parent schedulable again (and the target reports
2157 that the vfork is done). Until the child is done with the shared
2158 memory region, do not insert breakpoints in the parent, otherwise
2159 the child could still trip on the parent's breakpoints. Since
2160 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2161 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2162 return 0;
2163
31e77af2
PA
2164 /* Don't insert a breakpoint if we're trying to step past its
2165 location. */
2166 if ((bl->loc_type == bp_loc_software_breakpoint
2167 || bl->loc_type == bp_loc_hardware_breakpoint)
2168 && stepping_past_instruction_at (bl->pspace->aspace,
2169 bl->address))
e558d7c1
PA
2170 {
2171 if (debug_infrun)
2172 {
2173 fprintf_unfiltered (gdb_stdlog,
2174 "infrun: skipping breakpoint: "
2175 "stepping past insn at: %s\n",
2176 paddress (bl->gdbarch, bl->address));
2177 }
2178 return 0;
2179 }
31e77af2 2180
963f9c80
PA
2181 /* Don't insert watchpoints if we're trying to step past the
2182 instruction that triggered one. */
2183 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2184 && stepping_past_nonsteppable_watchpoint ())
2185 {
2186 if (debug_infrun)
2187 {
2188 fprintf_unfiltered (gdb_stdlog,
2189 "infrun: stepping past non-steppable watchpoint. "
2190 "skipping watchpoint at %s:%d\n",
2191 paddress (bl->gdbarch, bl->address),
2192 bl->length);
2193 }
2194 return 0;
2195 }
2196
74960c60
VP
2197 return 1;
2198}
2199
934709f0
PW
2200/* Same as should_be_inserted but does the check assuming
2201 that the location is not duplicated. */
2202
2203static int
2204unduplicated_should_be_inserted (struct bp_location *bl)
2205{
2206 int result;
2207 const int save_duplicate = bl->duplicate;
2208
2209 bl->duplicate = 0;
2210 result = should_be_inserted (bl);
2211 bl->duplicate = save_duplicate;
2212 return result;
2213}
2214
b775012e
LM
2215/* Parses a conditional described by an expression COND into an
2216 agent expression bytecode suitable for evaluation
2217 by the bytecode interpreter. Return NULL if there was
2218 any error during parsing. */
2219
2220static struct agent_expr *
2221parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2222{
2223 struct agent_expr *aexpr = NULL;
b775012e
LM
2224
2225 if (!cond)
2226 return NULL;
2227
2228 /* We don't want to stop processing, so catch any errors
2229 that may show up. */
492d29ea 2230 TRY
b775012e
LM
2231 {
2232 aexpr = gen_eval_for_expr (scope, cond);
2233 }
2234
492d29ea 2235 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2236 {
2237 /* If we got here, it means the condition could not be parsed to a valid
2238 bytecode expression and thus can't be evaluated on the target's side.
2239 It's no use iterating through the conditions. */
2240 return NULL;
2241 }
492d29ea 2242 END_CATCH
b775012e
LM
2243
2244 /* We have a valid agent expression. */
2245 return aexpr;
2246}
2247
2248/* Based on location BL, create a list of breakpoint conditions to be
2249 passed on to the target. If we have duplicated locations with different
2250 conditions, we will add such conditions to the list. The idea is that the
2251 target will evaluate the list of conditions and will only notify GDB when
2252 one of them is true. */
2253
2254static void
2255build_target_condition_list (struct bp_location *bl)
2256{
2257 struct bp_location **locp = NULL, **loc2p;
2258 int null_condition_or_parse_error = 0;
2259 int modified = bl->needs_update;
2260 struct bp_location *loc;
2261
8b4f3082
PA
2262 /* Release conditions left over from a previous insert. */
2263 VEC_free (agent_expr_p, bl->target_info.conditions);
2264
b775012e
LM
2265 /* This is only meaningful if the target is
2266 evaluating conditions and if the user has
2267 opted for condition evaluation on the target's
2268 side. */
2269 if (gdb_evaluates_breakpoint_condition_p ()
2270 || !target_supports_evaluation_of_breakpoint_conditions ())
2271 return;
2272
2273 /* Do a first pass to check for locations with no assigned
2274 conditions or conditions that fail to parse to a valid agent expression
2275 bytecode. If any of these happen, then it's no use to send conditions
2276 to the target since this location will always trigger and generate a
2277 response back to GDB. */
2278 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2279 {
2280 loc = (*loc2p);
2281 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2282 {
2283 if (modified)
2284 {
2285 struct agent_expr *aexpr;
2286
2287 /* Re-parse the conditions since something changed. In that
2288 case we already freed the condition bytecodes (see
2289 force_breakpoint_reinsertion). We just
2290 need to parse the condition to bytecodes again. */
2291 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2292 loc->cond_bytecode = aexpr;
2293
2294 /* Check if we managed to parse the conditional expression
2295 correctly. If not, we will not send this condition
2296 to the target. */
2297 if (aexpr)
2298 continue;
2299 }
2300
2301 /* If we have a NULL bytecode expression, it means something
2302 went wrong or we have a null condition expression. */
2303 if (!loc->cond_bytecode)
2304 {
2305 null_condition_or_parse_error = 1;
2306 break;
2307 }
2308 }
2309 }
2310
2311 /* If any of these happened, it means we will have to evaluate the conditions
2312 for the location's address on gdb's side. It is no use keeping bytecodes
2313 for all the other duplicate locations, thus we free all of them here.
2314
2315 This is so we have a finer control over which locations' conditions are
2316 being evaluated by GDB or the remote stub. */
2317 if (null_condition_or_parse_error)
2318 {
2319 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2320 {
2321 loc = (*loc2p);
2322 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2323 {
2324 /* Only go as far as the first NULL bytecode is
2325 located. */
2326 if (!loc->cond_bytecode)
2327 return;
2328
2329 free_agent_expr (loc->cond_bytecode);
2330 loc->cond_bytecode = NULL;
2331 }
2332 }
2333 }
2334
2335 /* No NULL conditions or failed bytecode generation. Build a condition list
2336 for this location's address. */
2337 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2338 {
2339 loc = (*loc2p);
2340 if (loc->cond
2341 && is_breakpoint (loc->owner)
2342 && loc->pspace->num == bl->pspace->num
2343 && loc->owner->enable_state == bp_enabled
2344 && loc->enabled)
2345 /* Add the condition to the vector. This will be used later to send the
2346 conditions to the target. */
2347 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2348 loc->cond_bytecode);
2349 }
2350
2351 return;
2352}
2353
d3ce09f5
SS
2354/* Parses a command described by string CMD into an agent expression
2355 bytecode suitable for evaluation by the bytecode interpreter.
2356 Return NULL if there was any error during parsing. */
2357
2358static struct agent_expr *
2359parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2360{
2361 struct cleanup *old_cleanups = 0;
2362 struct expression *expr, **argvec;
2363 struct agent_expr *aexpr = NULL;
bbc13ae3
KS
2364 const char *cmdrest;
2365 const char *format_start, *format_end;
d3ce09f5
SS
2366 struct format_piece *fpieces;
2367 int nargs;
2368 struct gdbarch *gdbarch = get_current_arch ();
2369
2370 if (!cmd)
2371 return NULL;
2372
2373 cmdrest = cmd;
2374
2375 if (*cmdrest == ',')
2376 ++cmdrest;
bbc13ae3 2377 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2378
2379 if (*cmdrest++ != '"')
2380 error (_("No format string following the location"));
2381
2382 format_start = cmdrest;
2383
2384 fpieces = parse_format_string (&cmdrest);
2385
2386 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2387
2388 format_end = cmdrest;
2389
2390 if (*cmdrest++ != '"')
2391 error (_("Bad format string, non-terminated '\"'."));
2392
bbc13ae3 2393 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2394
2395 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2396 error (_("Invalid argument syntax"));
2397
2398 if (*cmdrest == ',')
2399 cmdrest++;
bbc13ae3 2400 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2401
2402 /* For each argument, make an expression. */
2403
2404 argvec = (struct expression **) alloca (strlen (cmd)
2405 * sizeof (struct expression *));
2406
2407 nargs = 0;
2408 while (*cmdrest != '\0')
2409 {
bbc13ae3 2410 const char *cmd1;
d3ce09f5
SS
2411
2412 cmd1 = cmdrest;
2413 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2414 argvec[nargs++] = expr;
2415 cmdrest = cmd1;
2416 if (*cmdrest == ',')
2417 ++cmdrest;
2418 }
2419
2420 /* We don't want to stop processing, so catch any errors
2421 that may show up. */
492d29ea 2422 TRY
d3ce09f5
SS
2423 {
2424 aexpr = gen_printf (scope, gdbarch, 0, 0,
2425 format_start, format_end - format_start,
2426 fpieces, nargs, argvec);
2427 }
492d29ea 2428 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2429 {
2430 /* If we got here, it means the command could not be parsed to a valid
2431 bytecode expression and thus can't be evaluated on the target's side.
2432 It's no use iterating through the other commands. */
492d29ea 2433 aexpr = NULL;
d3ce09f5 2434 }
492d29ea
PA
2435 END_CATCH
2436
2437 do_cleanups (old_cleanups);
d3ce09f5 2438
d3ce09f5
SS
2439 /* We have a valid agent expression, return it. */
2440 return aexpr;
2441}
2442
2443/* Based on location BL, create a list of breakpoint commands to be
2444 passed on to the target. If we have duplicated locations with
2445 different commands, we will add any such to the list. */
2446
2447static void
2448build_target_command_list (struct bp_location *bl)
2449{
2450 struct bp_location **locp = NULL, **loc2p;
2451 int null_command_or_parse_error = 0;
2452 int modified = bl->needs_update;
2453 struct bp_location *loc;
2454
8b4f3082
PA
2455 /* Release commands left over from a previous insert. */
2456 VEC_free (agent_expr_p, bl->target_info.tcommands);
2457
41fac0cf 2458 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2459 return;
2460
41fac0cf
PA
2461 /* For now, limit to agent-style dprintf breakpoints. */
2462 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2463 return;
2464
41fac0cf
PA
2465 /* For now, if we have any duplicate location that isn't a dprintf,
2466 don't install the target-side commands, as that would make the
2467 breakpoint not be reported to the core, and we'd lose
2468 control. */
2469 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2470 {
2471 loc = (*loc2p);
2472 if (is_breakpoint (loc->owner)
2473 && loc->pspace->num == bl->pspace->num
2474 && loc->owner->type != bp_dprintf)
2475 return;
2476 }
2477
d3ce09f5
SS
2478 /* Do a first pass to check for locations with no assigned
2479 conditions or conditions that fail to parse to a valid agent expression
2480 bytecode. If any of these happen, then it's no use to send conditions
2481 to the target since this location will always trigger and generate a
2482 response back to GDB. */
2483 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2484 {
2485 loc = (*loc2p);
2486 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2487 {
2488 if (modified)
2489 {
2490 struct agent_expr *aexpr;
2491
2492 /* Re-parse the commands since something changed. In that
2493 case we already freed the command bytecodes (see
2494 force_breakpoint_reinsertion). We just
2495 need to parse the command to bytecodes again. */
2496 aexpr = parse_cmd_to_aexpr (bl->address,
2497 loc->owner->extra_string);
2498 loc->cmd_bytecode = aexpr;
2499
2500 if (!aexpr)
2501 continue;
2502 }
2503
2504 /* If we have a NULL bytecode expression, it means something
2505 went wrong or we have a null command expression. */
2506 if (!loc->cmd_bytecode)
2507 {
2508 null_command_or_parse_error = 1;
2509 break;
2510 }
2511 }
2512 }
2513
2514 /* If anything failed, then we're not doing target-side commands,
2515 and so clean up. */
2516 if (null_command_or_parse_error)
2517 {
2518 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2519 {
2520 loc = (*loc2p);
2521 if (is_breakpoint (loc->owner)
2522 && loc->pspace->num == bl->pspace->num)
2523 {
2524 /* Only go as far as the first NULL bytecode is
2525 located. */
40fb6c5e 2526 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2527 return;
2528
40fb6c5e
HZ
2529 free_agent_expr (loc->cmd_bytecode);
2530 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2531 }
2532 }
2533 }
2534
2535 /* No NULL commands or failed bytecode generation. Build a command list
2536 for this location's address. */
2537 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2538 {
2539 loc = (*loc2p);
2540 if (loc->owner->extra_string
2541 && is_breakpoint (loc->owner)
2542 && loc->pspace->num == bl->pspace->num
2543 && loc->owner->enable_state == bp_enabled
2544 && loc->enabled)
2545 /* Add the command to the vector. This will be used later
2546 to send the commands to the target. */
2547 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2548 loc->cmd_bytecode);
2549 }
2550
2551 bl->target_info.persist = 0;
2552 /* Maybe flag this location as persistent. */
2553 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2554 bl->target_info.persist = 1;
2555}
2556
35df4500
TJB
2557/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2558 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2559 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2560 Returns 0 for success, 1 if the bp_location type is not supported or
2561 -1 for failure.
879bfdc2 2562
4a64f543
MS
2563 NOTE drow/2003-09-09: This routine could be broken down to an
2564 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2565static int
35df4500 2566insert_bp_location (struct bp_location *bl,
26bb91f3 2567 struct ui_file *tmp_error_stream,
3fbb6ffa 2568 int *disabled_breaks,
dd61ec5c
MW
2569 int *hw_breakpoint_error,
2570 int *hw_bp_error_explained_already)
879bfdc2 2571{
0000e5cc
PA
2572 enum errors bp_err = GDB_NO_ERROR;
2573 const char *bp_err_message = NULL;
879bfdc2 2574
b775012e 2575 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2576 return 0;
2577
35c63cd8
JB
2578 /* Note we don't initialize bl->target_info, as that wipes out
2579 the breakpoint location's shadow_contents if the breakpoint
2580 is still inserted at that location. This in turn breaks
2581 target_read_memory which depends on these buffers when
2582 a memory read is requested at the breakpoint location:
2583 Once the target_info has been wiped, we fail to see that
2584 we have a breakpoint inserted at that address and thus
2585 read the breakpoint instead of returning the data saved in
2586 the breakpoint location's shadow contents. */
0d5ed153 2587 bl->target_info.reqstd_address = bl->address;
35df4500 2588 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2589 bl->target_info.length = bl->length;
8181d85f 2590
b775012e
LM
2591 /* When working with target-side conditions, we must pass all the conditions
2592 for the same breakpoint address down to the target since GDB will not
2593 insert those locations. With a list of breakpoint conditions, the target
2594 can decide when to stop and notify GDB. */
2595
2596 if (is_breakpoint (bl->owner))
2597 {
2598 build_target_condition_list (bl);
d3ce09f5
SS
2599 build_target_command_list (bl);
2600 /* Reset the modification marker. */
b775012e
LM
2601 bl->needs_update = 0;
2602 }
2603
35df4500
TJB
2604 if (bl->loc_type == bp_loc_software_breakpoint
2605 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2606 {
35df4500 2607 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2608 {
2609 /* If the explicitly specified breakpoint type
2610 is not hardware breakpoint, check the memory map to see
2611 if the breakpoint address is in read only memory or not.
4a64f543 2612
765dc015
VP
2613 Two important cases are:
2614 - location type is not hardware breakpoint, memory
2615 is readonly. We change the type of the location to
2616 hardware breakpoint.
4a64f543
MS
2617 - location type is hardware breakpoint, memory is
2618 read-write. This means we've previously made the
2619 location hardware one, but then the memory map changed,
2620 so we undo.
765dc015 2621
4a64f543
MS
2622 When breakpoints are removed, remove_breakpoints will use
2623 location types we've just set here, the only possible
2624 problem is that memory map has changed during running
2625 program, but it's not going to work anyway with current
2626 gdb. */
765dc015 2627 struct mem_region *mr
0d5ed153 2628 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2629
2630 if (mr)
2631 {
2632 if (automatic_hardware_breakpoints)
2633 {
765dc015
VP
2634 enum bp_loc_type new_type;
2635
2636 if (mr->attrib.mode != MEM_RW)
2637 new_type = bp_loc_hardware_breakpoint;
2638 else
2639 new_type = bp_loc_software_breakpoint;
2640
35df4500 2641 if (new_type != bl->loc_type)
765dc015
VP
2642 {
2643 static int said = 0;
cc59ec59 2644
35df4500 2645 bl->loc_type = new_type;
765dc015
VP
2646 if (!said)
2647 {
3e43a32a
MS
2648 fprintf_filtered (gdb_stdout,
2649 _("Note: automatically using "
2650 "hardware breakpoints for "
2651 "read-only addresses.\n"));
765dc015
VP
2652 said = 1;
2653 }
2654 }
2655 }
35df4500 2656 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2657 && mr->attrib.mode != MEM_RW)
2658 {
2659 fprintf_unfiltered (tmp_error_stream,
2660 _("Cannot insert breakpoint %d.\n"
2661 "Cannot set software breakpoint "
2662 "at read-only address %s\n"),
2663 bl->owner->number,
2664 paddress (bl->gdbarch, bl->address));
2665 return 1;
2666 }
765dc015
VP
2667 }
2668 }
2669
879bfdc2
DJ
2670 /* First check to see if we have to handle an overlay. */
2671 if (overlay_debugging == ovly_off
35df4500
TJB
2672 || bl->section == NULL
2673 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2674 {
2675 /* No overlay handling: just set the breakpoint. */
492d29ea 2676 TRY
dd61ec5c 2677 {
0000e5cc
PA
2678 int val;
2679
dd61ec5c 2680 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2681 if (val)
2682 bp_err = GENERIC_ERROR;
dd61ec5c 2683 }
492d29ea 2684 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2685 {
0000e5cc
PA
2686 bp_err = e.error;
2687 bp_err_message = e.message;
dd61ec5c 2688 }
492d29ea 2689 END_CATCH
879bfdc2
DJ
2690 }
2691 else
2692 {
4a64f543 2693 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2694 Shall we set a breakpoint at the LMA? */
2695 if (!overlay_events_enabled)
2696 {
2697 /* Yes -- overlay event support is not active,
2698 so we must try to set a breakpoint at the LMA.
2699 This will not work for a hardware breakpoint. */
35df4500 2700 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2701 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2702 bl->owner->number);
879bfdc2
DJ
2703 else
2704 {
35df4500
TJB
2705 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2706 bl->section);
879bfdc2 2707 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2708 bl->overlay_target_info = bl->target_info;
0d5ed153 2709 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2710
2711 /* No overlay handling: just set the breakpoint. */
492d29ea 2712 TRY
0000e5cc
PA
2713 {
2714 int val;
2715
2716 val = target_insert_breakpoint (bl->gdbarch,
2717 &bl->overlay_target_info);
2718 if (val)
2719 bp_err = GENERIC_ERROR;
2720 }
492d29ea 2721 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2722 {
2723 bp_err = e.error;
2724 bp_err_message = e.message;
2725 }
492d29ea 2726 END_CATCH
0000e5cc
PA
2727
2728 if (bp_err != GDB_NO_ERROR)
99361f52 2729 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2730 "Overlay breakpoint %d "
2731 "failed: in ROM?\n",
35df4500 2732 bl->owner->number);
879bfdc2
DJ
2733 }
2734 }
2735 /* Shall we set a breakpoint at the VMA? */
35df4500 2736 if (section_is_mapped (bl->section))
879bfdc2
DJ
2737 {
2738 /* Yes. This overlay section is mapped into memory. */
492d29ea 2739 TRY
dd61ec5c 2740 {
0000e5cc
PA
2741 int val;
2742
dd61ec5c 2743 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2744 if (val)
2745 bp_err = GENERIC_ERROR;
dd61ec5c 2746 }
492d29ea 2747 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2748 {
0000e5cc
PA
2749 bp_err = e.error;
2750 bp_err_message = e.message;
dd61ec5c 2751 }
492d29ea 2752 END_CATCH
879bfdc2
DJ
2753 }
2754 else
2755 {
2756 /* No. This breakpoint will not be inserted.
2757 No error, but do not mark the bp as 'inserted'. */
2758 return 0;
2759 }
2760 }
2761
0000e5cc 2762 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2763 {
2764 /* Can't set the breakpoint. */
0000e5cc
PA
2765
2766 /* In some cases, we might not be able to insert a
2767 breakpoint in a shared library that has already been
2768 removed, but we have not yet processed the shlib unload
2769 event. Unfortunately, some targets that implement
076855f9
PA
2770 breakpoint insertion themselves can't tell why the
2771 breakpoint insertion failed (e.g., the remote target
2772 doesn't define error codes), so we must treat generic
2773 errors as memory errors. */
0000e5cc 2774 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2775 && bl->loc_type == bp_loc_software_breakpoint
08351840 2776 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2777 || shared_objfile_contains_address_p (bl->pspace,
2778 bl->address)))
879bfdc2 2779 {
4a64f543 2780 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2781 bl->shlib_disabled = 1;
8d3788bd 2782 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2783 if (!*disabled_breaks)
2784 {
2785 fprintf_unfiltered (tmp_error_stream,
2786 "Cannot insert breakpoint %d.\n",
2787 bl->owner->number);
2788 fprintf_unfiltered (tmp_error_stream,
2789 "Temporarily disabling shared "
2790 "library breakpoints:\n");
2791 }
2792 *disabled_breaks = 1;
879bfdc2 2793 fprintf_unfiltered (tmp_error_stream,
35df4500 2794 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2795 return 0;
879bfdc2
DJ
2796 }
2797 else
879bfdc2 2798 {
35df4500 2799 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2800 {
0000e5cc
PA
2801 *hw_breakpoint_error = 1;
2802 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2803 fprintf_unfiltered (tmp_error_stream,
2804 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2805 bl->owner->number, bp_err_message ? ":" : ".\n");
2806 if (bp_err_message != NULL)
2807 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2808 }
2809 else
2810 {
0000e5cc
PA
2811 if (bp_err_message == NULL)
2812 {
2813 char *message
2814 = memory_error_message (TARGET_XFER_E_IO,
2815 bl->gdbarch, bl->address);
2816 struct cleanup *old_chain = make_cleanup (xfree, message);
2817
2818 fprintf_unfiltered (tmp_error_stream,
2819 "Cannot insert breakpoint %d.\n"
2820 "%s\n",
2821 bl->owner->number, message);
2822 do_cleanups (old_chain);
2823 }
2824 else
2825 {
2826 fprintf_unfiltered (tmp_error_stream,
2827 "Cannot insert breakpoint %d: %s\n",
2828 bl->owner->number,
2829 bp_err_message);
2830 }
879bfdc2 2831 }
0000e5cc 2832 return 1;
879bfdc2
DJ
2833
2834 }
2835 }
2836 else
35df4500 2837 bl->inserted = 1;
879bfdc2 2838
0000e5cc 2839 return 0;
879bfdc2
DJ
2840 }
2841
35df4500 2842 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2843 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2844 watchpoints. It's not clear that it's necessary... */
35df4500 2845 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2846 {
0000e5cc
PA
2847 int val;
2848
77b06cd7
TJB
2849 gdb_assert (bl->owner->ops != NULL
2850 && bl->owner->ops->insert_location != NULL);
2851
2852 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2853
2854 /* If trying to set a read-watchpoint, and it turns out it's not
2855 supported, try emulating one with an access watchpoint. */
35df4500 2856 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2857 {
2858 struct bp_location *loc, **loc_temp;
2859
2860 /* But don't try to insert it, if there's already another
2861 hw_access location that would be considered a duplicate
2862 of this one. */
2863 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2864 if (loc != bl
85d721b8 2865 && loc->watchpoint_type == hw_access
35df4500 2866 && watchpoint_locations_match (bl, loc))
85d721b8 2867 {
35df4500
TJB
2868 bl->duplicate = 1;
2869 bl->inserted = 1;
2870 bl->target_info = loc->target_info;
2871 bl->watchpoint_type = hw_access;
85d721b8
PA
2872 val = 0;
2873 break;
2874 }
2875
2876 if (val == 1)
2877 {
77b06cd7
TJB
2878 bl->watchpoint_type = hw_access;
2879 val = bl->owner->ops->insert_location (bl);
2880
2881 if (val)
2882 /* Back to the original value. */
2883 bl->watchpoint_type = hw_read;
85d721b8
PA
2884 }
2885 }
2886
35df4500 2887 bl->inserted = (val == 0);
879bfdc2
DJ
2888 }
2889
35df4500 2890 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2891 {
0000e5cc
PA
2892 int val;
2893
77b06cd7
TJB
2894 gdb_assert (bl->owner->ops != NULL
2895 && bl->owner->ops->insert_location != NULL);
2896
2897 val = bl->owner->ops->insert_location (bl);
2898 if (val)
2899 {
2900 bl->owner->enable_state = bp_disabled;
2901
2902 if (val == 1)
2903 warning (_("\
2904Error inserting catchpoint %d: Your system does not support this type\n\
2905of catchpoint."), bl->owner->number);
2906 else
2907 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2908 }
2909
2910 bl->inserted = (val == 0);
1640b821
DJ
2911
2912 /* We've already printed an error message if there was a problem
2913 inserting this catchpoint, and we've disabled the catchpoint,
2914 so just return success. */
2915 return 0;
879bfdc2
DJ
2916 }
2917
2918 return 0;
2919}
2920
6c95b8df
PA
2921/* This function is called when program space PSPACE is about to be
2922 deleted. It takes care of updating breakpoints to not reference
2923 PSPACE anymore. */
2924
2925void
2926breakpoint_program_space_exit (struct program_space *pspace)
2927{
2928 struct breakpoint *b, *b_temp;
876fa593 2929 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2930
2931 /* Remove any breakpoint that was set through this program space. */
2932 ALL_BREAKPOINTS_SAFE (b, b_temp)
2933 {
2934 if (b->pspace == pspace)
2935 delete_breakpoint (b);
2936 }
2937
2938 /* Breakpoints set through other program spaces could have locations
2939 bound to PSPACE as well. Remove those. */
876fa593 2940 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2941 {
2942 struct bp_location *tmp;
2943
2944 if (loc->pspace == pspace)
2945 {
2bdf28a0 2946 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2947 if (loc->owner->loc == loc)
2948 loc->owner->loc = loc->next;
2949 else
2950 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2951 if (tmp->next == loc)
2952 {
2953 tmp->next = loc->next;
2954 break;
2955 }
2956 }
2957 }
2958
2959 /* Now update the global location list to permanently delete the
2960 removed locations above. */
44702360 2961 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2962}
2963
74960c60
VP
2964/* Make sure all breakpoints are inserted in inferior.
2965 Throws exception on any error.
2966 A breakpoint that is already inserted won't be inserted
2967 again, so calling this function twice is safe. */
2968void
2969insert_breakpoints (void)
2970{
2971 struct breakpoint *bpt;
2972
2973 ALL_BREAKPOINTS (bpt)
2974 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2975 {
2976 struct watchpoint *w = (struct watchpoint *) bpt;
2977
2978 update_watchpoint (w, 0 /* don't reparse. */);
2979 }
74960c60 2980
04086b45
PA
2981 /* Updating watchpoints creates new locations, so update the global
2982 location list. Explicitly tell ugll to insert locations and
2983 ignore breakpoints_always_inserted_mode. */
2984 update_global_location_list (UGLL_INSERT);
74960c60
VP
2985}
2986
20388dd6
YQ
2987/* Invoke CALLBACK for each of bp_location. */
2988
2989void
2990iterate_over_bp_locations (walk_bp_location_callback callback)
2991{
2992 struct bp_location *loc, **loc_tmp;
2993
2994 ALL_BP_LOCATIONS (loc, loc_tmp)
2995 {
2996 callback (loc, NULL);
2997 }
2998}
2999
b775012e
LM
3000/* This is used when we need to synch breakpoint conditions between GDB and the
3001 target. It is the case with deleting and disabling of breakpoints when using
3002 always-inserted mode. */
3003
3004static void
3005update_inserted_breakpoint_locations (void)
3006{
3007 struct bp_location *bl, **blp_tmp;
3008 int error_flag = 0;
3009 int val = 0;
3010 int disabled_breaks = 0;
3011 int hw_breakpoint_error = 0;
dd61ec5c 3012 int hw_bp_details_reported = 0;
b775012e
LM
3013
3014 struct ui_file *tmp_error_stream = mem_fileopen ();
3015 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3016
3017 /* Explicitly mark the warning -- this will only be printed if
3018 there was an error. */
3019 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3020
3021 save_current_space_and_thread ();
3022
3023 ALL_BP_LOCATIONS (bl, blp_tmp)
3024 {
3025 /* We only want to update software breakpoints and hardware
3026 breakpoints. */
3027 if (!is_breakpoint (bl->owner))
3028 continue;
3029
3030 /* We only want to update locations that are already inserted
3031 and need updating. This is to avoid unwanted insertion during
3032 deletion of breakpoints. */
3033 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3034 continue;
3035
3036 switch_to_program_space_and_thread (bl->pspace);
3037
3038 /* For targets that support global breakpoints, there's no need
3039 to select an inferior to insert breakpoint to. In fact, even
3040 if we aren't attached to any process yet, we should still
3041 insert breakpoints. */
f5656ead 3042 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3043 && ptid_equal (inferior_ptid, null_ptid))
3044 continue;
3045
3046 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3047 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3048 if (val)
3049 error_flag = val;
3050 }
3051
3052 if (error_flag)
3053 {
3054 target_terminal_ours_for_output ();
3055 error_stream (tmp_error_stream);
3056 }
3057
3058 do_cleanups (cleanups);
3059}
3060
c30eee59 3061/* Used when starting or continuing the program. */
c906108c 3062
74960c60
VP
3063static void
3064insert_breakpoint_locations (void)
c906108c 3065{
a5606eee 3066 struct breakpoint *bpt;
35df4500 3067 struct bp_location *bl, **blp_tmp;
eacd795a 3068 int error_flag = 0;
c906108c 3069 int val = 0;
3fbb6ffa 3070 int disabled_breaks = 0;
81d0cc19 3071 int hw_breakpoint_error = 0;
dd61ec5c 3072 int hw_bp_error_explained_already = 0;
c906108c 3073
81d0cc19 3074 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3075 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3076
81d0cc19
GS
3077 /* Explicitly mark the warning -- this will only be printed if
3078 there was an error. */
3079 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3080
3081 save_current_space_and_thread ();
3082
35df4500 3083 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3084 {
b775012e 3085 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3086 continue;
3087
4a64f543
MS
3088 /* There is no point inserting thread-specific breakpoints if
3089 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3090 has BL->OWNER always non-NULL. */
35df4500
TJB
3091 if (bl->owner->thread != -1
3092 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3093 continue;
3094
35df4500 3095 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3096
3097 /* For targets that support global breakpoints, there's no need
3098 to select an inferior to insert breakpoint to. In fact, even
3099 if we aren't attached to any process yet, we should still
3100 insert breakpoints. */
f5656ead 3101 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3102 && ptid_equal (inferior_ptid, null_ptid))
3103 continue;
3104
3fbb6ffa 3105 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3106 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3107 if (val)
eacd795a 3108 error_flag = val;
879bfdc2 3109 }
c906108c 3110
4a64f543
MS
3111 /* If we failed to insert all locations of a watchpoint, remove
3112 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3113 ALL_BREAKPOINTS (bpt)
3114 {
3115 int some_failed = 0;
3116 struct bp_location *loc;
3117
3118 if (!is_hardware_watchpoint (bpt))
3119 continue;
3120
d6b74ac4 3121 if (!breakpoint_enabled (bpt))
a5606eee 3122 continue;
74960c60
VP
3123
3124 if (bpt->disposition == disp_del_at_next_stop)
3125 continue;
a5606eee
VP
3126
3127 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3128 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3129 {
3130 some_failed = 1;
3131 break;
3132 }
3133 if (some_failed)
3134 {
3135 for (loc = bpt->loc; loc; loc = loc->next)
3136 if (loc->inserted)
3137 remove_breakpoint (loc, mark_uninserted);
3138
3139 hw_breakpoint_error = 1;
3140 fprintf_unfiltered (tmp_error_stream,
3141 "Could not insert hardware watchpoint %d.\n",
3142 bpt->number);
eacd795a 3143 error_flag = -1;
a5606eee
VP
3144 }
3145 }
3146
eacd795a 3147 if (error_flag)
81d0cc19
GS
3148 {
3149 /* If a hardware breakpoint or watchpoint was inserted, add a
3150 message about possibly exhausted resources. */
dd61ec5c 3151 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3152 {
c6510018
MS
3153 fprintf_unfiltered (tmp_error_stream,
3154 "Could not insert hardware breakpoints:\n\
3155You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3156 }
81d0cc19
GS
3157 target_terminal_ours_for_output ();
3158 error_stream (tmp_error_stream);
3159 }
f7545552
TT
3160
3161 do_cleanups (cleanups);
c906108c
SS
3162}
3163
c30eee59
TJB
3164/* Used when the program stops.
3165 Returns zero if successful, or non-zero if there was a problem
3166 removing a breakpoint location. */
3167
c906108c 3168int
fba45db2 3169remove_breakpoints (void)
c906108c 3170{
35df4500 3171 struct bp_location *bl, **blp_tmp;
3a1bae8e 3172 int val = 0;
c906108c 3173
35df4500 3174 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3175 {
1e4d1764 3176 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3177 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3178 }
3a1bae8e 3179 return val;
c906108c
SS
3180}
3181
49fa26b0
PA
3182/* When a thread exits, remove breakpoints that are related to
3183 that thread. */
3184
3185static void
3186remove_threaded_breakpoints (struct thread_info *tp, int silent)
3187{
3188 struct breakpoint *b, *b_tmp;
3189
3190 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3191 {
96181529 3192 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3193 {
3194 b->disposition = disp_del_at_next_stop;
3195
3196 printf_filtered (_("\
46ecd527 3197Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3198 b->number, tp->num);
3199
3200 /* Hide it from the user. */
3201 b->number = 0;
3202 }
3203 }
3204}
3205
6c95b8df
PA
3206/* Remove breakpoints of process PID. */
3207
3208int
3209remove_breakpoints_pid (int pid)
3210{
35df4500 3211 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3212 int val;
3213 struct inferior *inf = find_inferior_pid (pid);
3214
35df4500 3215 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3216 {
35df4500 3217 if (bl->pspace != inf->pspace)
6c95b8df
PA
3218 continue;
3219
fc126975 3220 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3221 {
35df4500 3222 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3223 if (val != 0)
3224 return val;
3225 }
3226 }
3227 return 0;
3228}
3229
c906108c 3230int
fba45db2 3231reattach_breakpoints (int pid)
c906108c 3232{
6c95b8df 3233 struct cleanup *old_chain;
35df4500 3234 struct bp_location *bl, **blp_tmp;
c906108c 3235 int val;
86b887df 3236 struct ui_file *tmp_error_stream;
dd61ec5c 3237 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3238 struct inferior *inf;
3239 struct thread_info *tp;
3240
3241 tp = any_live_thread_of_process (pid);
3242 if (tp == NULL)
3243 return 1;
3244
3245 inf = find_inferior_pid (pid);
3246 old_chain = save_inferior_ptid ();
3247
3248 inferior_ptid = tp->ptid;
a4954f26 3249
86b887df 3250 tmp_error_stream = mem_fileopen ();
a4954f26 3251 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3252
35df4500 3253 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3254 {
35df4500 3255 if (bl->pspace != inf->pspace)
6c95b8df
PA
3256 continue;
3257
35df4500 3258 if (bl->inserted)
c5aa993b 3259 {
35df4500 3260 bl->inserted = 0;
dd61ec5c 3261 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3262 if (val != 0)
3263 {
ce696e05 3264 do_cleanups (old_chain);
c5aa993b
JM
3265 return val;
3266 }
3267 }
3268 }
ce696e05 3269 do_cleanups (old_chain);
c906108c
SS
3270 return 0;
3271}
3272
e58b0e63
PA
3273static int internal_breakpoint_number = -1;
3274
84f4c1fe
PM
3275/* Set the breakpoint number of B, depending on the value of INTERNAL.
3276 If INTERNAL is non-zero, the breakpoint number will be populated
3277 from internal_breakpoint_number and that variable decremented.
e5dd4106 3278 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3279 breakpoint_count and that value incremented. Internal breakpoints
3280 do not set the internal var bpnum. */
3281static void
3282set_breakpoint_number (int internal, struct breakpoint *b)
3283{
3284 if (internal)
3285 b->number = internal_breakpoint_number--;
3286 else
3287 {
3288 set_breakpoint_count (breakpoint_count + 1);
3289 b->number = breakpoint_count;
3290 }
3291}
3292
e62c965a 3293static struct breakpoint *
a6d9a66e 3294create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3295 CORE_ADDR address, enum bptype type,
c0a91b2b 3296 const struct breakpoint_ops *ops)
e62c965a 3297{
e62c965a
PP
3298 struct symtab_and_line sal;
3299 struct breakpoint *b;
3300
4a64f543 3301 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3302
3303 sal.pc = address;
3304 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3305 sal.pspace = current_program_space;
e62c965a 3306
06edf0c0 3307 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3308 b->number = internal_breakpoint_number--;
3309 b->disposition = disp_donttouch;
3310
3311 return b;
3312}
3313
17450429
PP
3314static const char *const longjmp_names[] =
3315 {
3316 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3317 };
3318#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3319
3320/* Per-objfile data private to breakpoint.c. */
3321struct breakpoint_objfile_data
3322{
3323 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3324 struct bound_minimal_symbol overlay_msym;
17450429
PP
3325
3326 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3327 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3328
28106bc2
SDJ
3329 /* True if we have looked for longjmp probes. */
3330 int longjmp_searched;
3331
3332 /* SystemTap probe points for longjmp (if any). */
3333 VEC (probe_p) *longjmp_probes;
3334
17450429 3335 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3336 struct bound_minimal_symbol terminate_msym;
17450429
PP
3337
3338 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3339 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3340
3341 /* True if we have looked for exception probes. */
3342 int exception_searched;
3343
3344 /* SystemTap probe points for unwinding (if any). */
3345 VEC (probe_p) *exception_probes;
17450429
PP
3346};
3347
3348static const struct objfile_data *breakpoint_objfile_key;
3349
3350/* Minimal symbol not found sentinel. */
3351static struct minimal_symbol msym_not_found;
3352
3353/* Returns TRUE if MSYM point to the "not found" sentinel. */
3354
3355static int
3356msym_not_found_p (const struct minimal_symbol *msym)
3357{
3358 return msym == &msym_not_found;
3359}
3360
3361/* Return per-objfile data needed by breakpoint.c.
3362 Allocate the data if necessary. */
3363
3364static struct breakpoint_objfile_data *
3365get_breakpoint_objfile_data (struct objfile *objfile)
3366{
3367 struct breakpoint_objfile_data *bp_objfile_data;
3368
3369 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3370 if (bp_objfile_data == NULL)
3371 {
3372 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3373 sizeof (*bp_objfile_data));
3374
3375 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3376 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3377 }
3378 return bp_objfile_data;
3379}
3380
28106bc2
SDJ
3381static void
3382free_breakpoint_probes (struct objfile *obj, void *data)
3383{
3384 struct breakpoint_objfile_data *bp_objfile_data = data;
3385
3386 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3387 VEC_free (probe_p, bp_objfile_data->exception_probes);
3388}
3389
e62c965a 3390static void
af02033e 3391create_overlay_event_breakpoint (void)
e62c965a 3392{
69de3c6a 3393 struct objfile *objfile;
af02033e 3394 const char *const func_name = "_ovly_debug_event";
e62c965a 3395
69de3c6a
PP
3396 ALL_OBJFILES (objfile)
3397 {
3398 struct breakpoint *b;
17450429
PP
3399 struct breakpoint_objfile_data *bp_objfile_data;
3400 CORE_ADDR addr;
69de3c6a 3401
17450429
PP
3402 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3403
3b7344d5 3404 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3405 continue;
3406
3b7344d5 3407 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3408 {
3b7344d5 3409 struct bound_minimal_symbol m;
17450429
PP
3410
3411 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3412 if (m.minsym == NULL)
17450429
PP
3413 {
3414 /* Avoid future lookups in this objfile. */
3b7344d5 3415 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3416 continue;
3417 }
3418 bp_objfile_data->overlay_msym = m;
3419 }
e62c965a 3420
77e371c0 3421 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3422 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3423 bp_overlay_event,
3424 &internal_breakpoint_ops);
69de3c6a 3425 b->addr_string = xstrdup (func_name);
e62c965a 3426
69de3c6a
PP
3427 if (overlay_debugging == ovly_auto)
3428 {
3429 b->enable_state = bp_enabled;
3430 overlay_events_enabled = 1;
3431 }
3432 else
3433 {
3434 b->enable_state = bp_disabled;
3435 overlay_events_enabled = 0;
3436 }
e62c965a 3437 }
44702360 3438 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3439}
3440
0fd8e87f 3441static void
af02033e 3442create_longjmp_master_breakpoint (void)
0fd8e87f 3443{
6c95b8df 3444 struct program_space *pspace;
6c95b8df
PA
3445 struct cleanup *old_chain;
3446
3447 old_chain = save_current_program_space ();
0fd8e87f 3448
6c95b8df 3449 ALL_PSPACES (pspace)
af02033e
PP
3450 {
3451 struct objfile *objfile;
3452
3453 set_current_program_space (pspace);
3454
3455 ALL_OBJFILES (objfile)
0fd8e87f 3456 {
af02033e
PP
3457 int i;
3458 struct gdbarch *gdbarch;
17450429 3459 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3460
af02033e 3461 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3462
17450429
PP
3463 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3464
28106bc2
SDJ
3465 if (!bp_objfile_data->longjmp_searched)
3466 {
25f9533e
SDJ
3467 VEC (probe_p) *ret;
3468
3469 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3470 if (ret != NULL)
3471 {
3472 /* We are only interested in checking one element. */
3473 struct probe *p = VEC_index (probe_p, ret, 0);
3474
3475 if (!can_evaluate_probe_arguments (p))
3476 {
3477 /* We cannot use the probe interface here, because it does
3478 not know how to evaluate arguments. */
3479 VEC_free (probe_p, ret);
3480 ret = NULL;
3481 }
3482 }
3483 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3484 bp_objfile_data->longjmp_searched = 1;
3485 }
3486
3487 if (bp_objfile_data->longjmp_probes != NULL)
3488 {
3489 int i;
3490 struct probe *probe;
3491 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3492
3493 for (i = 0;
3494 VEC_iterate (probe_p,
3495 bp_objfile_data->longjmp_probes,
3496 i, probe);
3497 ++i)
3498 {
3499 struct breakpoint *b;
3500
729662a5
TT
3501 b = create_internal_breakpoint (gdbarch,
3502 get_probe_address (probe,
3503 objfile),
28106bc2
SDJ
3504 bp_longjmp_master,
3505 &internal_breakpoint_ops);
3506 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3507 b->enable_state = bp_disabled;
3508 }
3509
3510 continue;
3511 }
3512
0569175e
TSD
3513 if (!gdbarch_get_longjmp_target_p (gdbarch))
3514 continue;
3515
17450429 3516 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3517 {
3518 struct breakpoint *b;
af02033e 3519 const char *func_name;
17450429 3520 CORE_ADDR addr;
6c95b8df 3521
3b7344d5 3522 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3523 continue;
0fd8e87f 3524
17450429 3525 func_name = longjmp_names[i];
3b7344d5 3526 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3527 {
3b7344d5 3528 struct bound_minimal_symbol m;
17450429
PP
3529
3530 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3531 if (m.minsym == NULL)
17450429
PP
3532 {
3533 /* Prevent future lookups in this objfile. */
3b7344d5 3534 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3535 continue;
3536 }
3537 bp_objfile_data->longjmp_msym[i] = m;
3538 }
3539
77e371c0 3540 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3541 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3542 &internal_breakpoint_ops);
af02033e
PP
3543 b->addr_string = xstrdup (func_name);
3544 b->enable_state = bp_disabled;
3545 }
0fd8e87f 3546 }
af02033e 3547 }
44702360 3548 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3549
3550 do_cleanups (old_chain);
0fd8e87f
UW
3551}
3552
af02033e 3553/* Create a master std::terminate breakpoint. */
aa7d318d 3554static void
af02033e 3555create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3556{
3557 struct program_space *pspace;
aa7d318d 3558 struct cleanup *old_chain;
af02033e 3559 const char *const func_name = "std::terminate()";
aa7d318d
TT
3560
3561 old_chain = save_current_program_space ();
3562
3563 ALL_PSPACES (pspace)
17450429
PP
3564 {
3565 struct objfile *objfile;
3566 CORE_ADDR addr;
3567
3568 set_current_program_space (pspace);
3569
aa7d318d
TT
3570 ALL_OBJFILES (objfile)
3571 {
3572 struct breakpoint *b;
17450429 3573 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3574
17450429 3575 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3576
3b7344d5 3577 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3578 continue;
3579
3b7344d5 3580 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3581 {
3b7344d5 3582 struct bound_minimal_symbol m;
17450429
PP
3583
3584 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3585 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3586 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3587 {
3588 /* Prevent future lookups in this objfile. */
3b7344d5 3589 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3590 continue;
3591 }
3592 bp_objfile_data->terminate_msym = m;
3593 }
aa7d318d 3594
77e371c0 3595 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3596 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3597 bp_std_terminate_master,
3598 &internal_breakpoint_ops);
aa7d318d
TT
3599 b->addr_string = xstrdup (func_name);
3600 b->enable_state = bp_disabled;
3601 }
17450429
PP
3602 }
3603
44702360 3604 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3605
3606 do_cleanups (old_chain);
3607}
3608
186c406b
TT
3609/* Install a master breakpoint on the unwinder's debug hook. */
3610
70221824 3611static void
186c406b
TT
3612create_exception_master_breakpoint (void)
3613{
3614 struct objfile *objfile;
17450429 3615 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3616
3617 ALL_OBJFILES (objfile)
3618 {
17450429
PP
3619 struct breakpoint *b;
3620 struct gdbarch *gdbarch;
3621 struct breakpoint_objfile_data *bp_objfile_data;
3622 CORE_ADDR addr;
3623
3624 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3625
28106bc2
SDJ
3626 /* We prefer the SystemTap probe point if it exists. */
3627 if (!bp_objfile_data->exception_searched)
3628 {
25f9533e
SDJ
3629 VEC (probe_p) *ret;
3630
3631 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3632
3633 if (ret != NULL)
3634 {
3635 /* We are only interested in checking one element. */
3636 struct probe *p = VEC_index (probe_p, ret, 0);
3637
3638 if (!can_evaluate_probe_arguments (p))
3639 {
3640 /* We cannot use the probe interface here, because it does
3641 not know how to evaluate arguments. */
3642 VEC_free (probe_p, ret);
3643 ret = NULL;
3644 }
3645 }
3646 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3647 bp_objfile_data->exception_searched = 1;
3648 }
3649
3650 if (bp_objfile_data->exception_probes != NULL)
3651 {
3652 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3653 int i;
3654 struct probe *probe;
3655
3656 for (i = 0;
3657 VEC_iterate (probe_p,
3658 bp_objfile_data->exception_probes,
3659 i, probe);
3660 ++i)
3661 {
3662 struct breakpoint *b;
3663
729662a5
TT
3664 b = create_internal_breakpoint (gdbarch,
3665 get_probe_address (probe,
3666 objfile),
28106bc2
SDJ
3667 bp_exception_master,
3668 &internal_breakpoint_ops);
3669 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3670 b->enable_state = bp_disabled;
3671 }
3672
3673 continue;
3674 }
3675
3676 /* Otherwise, try the hook function. */
3677
3b7344d5 3678 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3679 continue;
3680
3681 gdbarch = get_objfile_arch (objfile);
186c406b 3682
3b7344d5 3683 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3684 {
3b7344d5 3685 struct bound_minimal_symbol debug_hook;
186c406b 3686
17450429 3687 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3688 if (debug_hook.minsym == NULL)
17450429 3689 {
3b7344d5 3690 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3691 continue;
3692 }
3693
3694 bp_objfile_data->exception_msym = debug_hook;
186c406b 3695 }
17450429 3696
77e371c0 3697 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3698 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3699 &current_target);
06edf0c0
PA
3700 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3701 &internal_breakpoint_ops);
17450429
PP
3702 b->addr_string = xstrdup (func_name);
3703 b->enable_state = bp_disabled;
186c406b
TT
3704 }
3705
44702360 3706 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3707}
3708
c906108c 3709void
fba45db2 3710update_breakpoints_after_exec (void)
c906108c 3711{
35df4500 3712 struct breakpoint *b, *b_tmp;
876fa593 3713 struct bp_location *bploc, **bplocp_tmp;
c906108c 3714
25b22b0a
PA
3715 /* We're about to delete breakpoints from GDB's lists. If the
3716 INSERTED flag is true, GDB will try to lift the breakpoints by
3717 writing the breakpoints' "shadow contents" back into memory. The
3718 "shadow contents" are NOT valid after an exec, so GDB should not
3719 do that. Instead, the target is responsible from marking
3720 breakpoints out as soon as it detects an exec. We don't do that
3721 here instead, because there may be other attempts to delete
3722 breakpoints after detecting an exec and before reaching here. */
876fa593 3723 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3724 if (bploc->pspace == current_program_space)
3725 gdb_assert (!bploc->inserted);
c906108c 3726
35df4500 3727 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3728 {
6c95b8df
PA
3729 if (b->pspace != current_program_space)
3730 continue;
3731
4a64f543 3732 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3733 if (b->type == bp_shlib_event)
3734 {
3735 delete_breakpoint (b);
3736 continue;
3737 }
c906108c 3738
4a64f543 3739 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3740 if (b->type == bp_jit_event)
3741 {
3742 delete_breakpoint (b);
3743 continue;
3744 }
3745
1900040c 3746 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3747 as must overlay event and longjmp master breakpoints. */
3748 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3749 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3750 || b->type == bp_exception_master)
c4093a6a
JM
3751 {
3752 delete_breakpoint (b);
3753 continue;
3754 }
3755
4a64f543 3756 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3757 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3758 {
3759 delete_breakpoint (b);
3760 continue;
3761 }
3762
7c16b83e
PA
3763 /* Just like single-step breakpoints. */
3764 if (b->type == bp_single_step)
3765 {
3766 delete_breakpoint (b);
3767 continue;
3768 }
3769
611c83ae
PA
3770 /* Longjmp and longjmp-resume breakpoints are also meaningless
3771 after an exec. */
186c406b 3772 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3773 || b->type == bp_longjmp_call_dummy
186c406b 3774 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3775 {
3776 delete_breakpoint (b);
3777 continue;
3778 }
3779
ce78b96d
JB
3780 if (b->type == bp_catchpoint)
3781 {
3782 /* For now, none of the bp_catchpoint breakpoints need to
3783 do anything at this point. In the future, if some of
3784 the catchpoints need to something, we will need to add
3785 a new method, and call this method from here. */
3786 continue;
3787 }
3788
c5aa993b
JM
3789 /* bp_finish is a special case. The only way we ought to be able
3790 to see one of these when an exec() has happened, is if the user
3791 caught a vfork, and then said "finish". Ordinarily a finish just
3792 carries them to the call-site of the current callee, by setting
3793 a temporary bp there and resuming. But in this case, the finish
3794 will carry them entirely through the vfork & exec.
3795
3796 We don't want to allow a bp_finish to remain inserted now. But
3797 we can't safely delete it, 'cause finish_command has a handle to
3798 the bp on a bpstat, and will later want to delete it. There's a
3799 chance (and I've seen it happen) that if we delete the bp_finish
3800 here, that its storage will get reused by the time finish_command
3801 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3802 We really must allow finish_command to delete a bp_finish.
3803
e5dd4106 3804 In the absence of a general solution for the "how do we know
53a5351d
JM
3805 it's safe to delete something others may have handles to?"
3806 problem, what we'll do here is just uninsert the bp_finish, and
3807 let finish_command delete it.
3808
3809 (We know the bp_finish is "doomed" in the sense that it's
3810 momentary, and will be deleted as soon as finish_command sees
3811 the inferior stopped. So it doesn't matter that the bp's
3812 address is probably bogus in the new a.out, unlike e.g., the
3813 solib breakpoints.) */
c5aa993b 3814
c5aa993b
JM
3815 if (b->type == bp_finish)
3816 {
3817 continue;
3818 }
3819
3820 /* Without a symbolic address, we have little hope of the
3821 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3822 a.out. */
c5aa993b
JM
3823 if (b->addr_string == NULL)
3824 {
3825 delete_breakpoint (b);
3826 continue;
3827 }
c5aa993b 3828 }
c906108c
SS
3829}
3830
3831int
d80ee84f 3832detach_breakpoints (ptid_t ptid)
c906108c 3833{
35df4500 3834 struct bp_location *bl, **blp_tmp;
3a1bae8e 3835 int val = 0;
ce696e05 3836 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3837 struct inferior *inf = current_inferior ();
c5aa993b 3838
dfd4cc63 3839 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3840 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3841
6c95b8df 3842 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3843 inferior_ptid = ptid;
35df4500 3844 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3845 {
35df4500 3846 if (bl->pspace != inf->pspace)
6c95b8df
PA
3847 continue;
3848
bd9673a4
PW
3849 /* This function must physically remove breakpoints locations
3850 from the specified ptid, without modifying the breakpoint
3851 package's state. Locations of type bp_loc_other are only
3852 maintained at GDB side. So, there is no need to remove
3853 these bp_loc_other locations. Moreover, removing these
3854 would modify the breakpoint package's state. */
3855 if (bl->loc_type == bp_loc_other)
3856 continue;
3857
35df4500
TJB
3858 if (bl->inserted)
3859 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3860 }
d03285ec 3861
ce696e05 3862 do_cleanups (old_chain);
3a1bae8e 3863 return val;
c906108c
SS
3864}
3865
35df4500 3866/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3867 Note that this is used to detach breakpoints from a child fork.
3868 When we get here, the child isn't in the inferior list, and neither
3869 do we have objects to represent its address space --- we should
35df4500 3870 *not* look at bl->pspace->aspace here. */
6c95b8df 3871
c906108c 3872static int
35df4500 3873remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3874{
3875 int val;
c5aa993b 3876
35df4500
TJB
3877 /* BL is never in moribund_locations by our callers. */
3878 gdb_assert (bl->owner != NULL);
2bdf28a0 3879
1a853c52 3880 if (bl->permanent)
c2c6d25f
JM
3881 /* Permanent breakpoints cannot be inserted or removed. */
3882 return 0;
3883
74960c60
VP
3884 /* The type of none suggests that owner is actually deleted.
3885 This should not ever happen. */
35df4500 3886 gdb_assert (bl->owner->type != bp_none);
0bde7532 3887
35df4500
TJB
3888 if (bl->loc_type == bp_loc_software_breakpoint
3889 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3890 {
c02f5703
MS
3891 /* "Normal" instruction breakpoint: either the standard
3892 trap-instruction bp (bp_breakpoint), or a
3893 bp_hardware_breakpoint. */
3894
3895 /* First check to see if we have to handle an overlay. */
3896 if (overlay_debugging == ovly_off
35df4500
TJB
3897 || bl->section == NULL
3898 || !(section_is_overlay (bl->section)))
c02f5703
MS
3899 {
3900 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3901
3902 /* If we're trying to uninsert a memory breakpoint that we
3903 know is set in a dynamic object that is marked
3904 shlib_disabled, then either the dynamic object was
3905 removed with "remove-symbol-file" or with
3906 "nosharedlibrary". In the former case, we don't know
3907 whether another dynamic object might have loaded over the
3908 breakpoint's address -- the user might well let us know
3909 about it next with add-symbol-file (the whole point of
d03de421 3910 add-symbol-file is letting the user manually maintain a
08351840
PA
3911 list of dynamically loaded objects). If we have the
3912 breakpoint's shadow memory, that is, this is a software
3913 breakpoint managed by GDB, check whether the breakpoint
3914 is still inserted in memory, to avoid overwriting wrong
3915 code with stale saved shadow contents. Note that HW
3916 breakpoints don't have shadow memory, as they're
3917 implemented using a mechanism that is not dependent on
3918 being able to modify the target's memory, and as such
3919 they should always be removed. */
3920 if (bl->shlib_disabled
3921 && bl->target_info.shadow_len != 0
3922 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3923 val = 0;
3924 else
3925 val = bl->owner->ops->remove_location (bl);
c02f5703 3926 }
c906108c
SS
3927 else
3928 {
4a64f543 3929 /* This breakpoint is in an overlay section.
c02f5703
MS
3930 Did we set a breakpoint at the LMA? */
3931 if (!overlay_events_enabled)
3932 {
3933 /* Yes -- overlay event support is not active, so we
3934 should have set a breakpoint at the LMA. Remove it.
3935 */
c02f5703
MS
3936 /* Ignore any failures: if the LMA is in ROM, we will
3937 have already warned when we failed to insert it. */
35df4500
TJB
3938 if (bl->loc_type == bp_loc_hardware_breakpoint)
3939 target_remove_hw_breakpoint (bl->gdbarch,
3940 &bl->overlay_target_info);
c02f5703 3941 else
35df4500
TJB
3942 target_remove_breakpoint (bl->gdbarch,
3943 &bl->overlay_target_info);
c02f5703
MS
3944 }
3945 /* Did we set a breakpoint at the VMA?
3946 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3947 if (bl->inserted)
c906108c 3948 {
c02f5703
MS
3949 /* Yes -- remove it. Previously we did not bother to
3950 remove the breakpoint if the section had been
3951 unmapped, but let's not rely on that being safe. We
3952 don't know what the overlay manager might do. */
aa67235e
UW
3953
3954 /* However, we should remove *software* breakpoints only
3955 if the section is still mapped, or else we overwrite
3956 wrong code with the saved shadow contents. */
348d480f
PA
3957 if (bl->loc_type == bp_loc_hardware_breakpoint
3958 || section_is_mapped (bl->section))
3959 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3960 else
3961 val = 0;
c906108c 3962 }
c02f5703
MS
3963 else
3964 {
3965 /* No -- not inserted, so no need to remove. No error. */
3966 val = 0;
3967 }
c906108c 3968 }
879d1e6b 3969
08351840
PA
3970 /* In some cases, we might not be able to remove a breakpoint in
3971 a shared library that has already been removed, but we have
3972 not yet processed the shlib unload event. Similarly for an
3973 unloaded add-symbol-file object - the user might not yet have
3974 had the chance to remove-symbol-file it. shlib_disabled will
3975 be set if the library/object has already been removed, but
3976 the breakpoint hasn't been uninserted yet, e.g., after
3977 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3978 always-inserted mode. */
076855f9 3979 if (val
08351840
PA
3980 && (bl->loc_type == bp_loc_software_breakpoint
3981 && (bl->shlib_disabled
3982 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3983 || shared_objfile_contains_address_p (bl->pspace,
3984 bl->address))))
879d1e6b
UW
3985 val = 0;
3986
c906108c
SS
3987 if (val)
3988 return val;
35df4500 3989 bl->inserted = (is == mark_inserted);
c906108c 3990 }
35df4500 3991 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3992 {
77b06cd7
TJB
3993 gdb_assert (bl->owner->ops != NULL
3994 && bl->owner->ops->remove_location != NULL);
3995
35df4500 3996 bl->inserted = (is == mark_inserted);
77b06cd7 3997 bl->owner->ops->remove_location (bl);
2e70b7b9 3998
c906108c 3999 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4000 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4001 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4002 bl->owner->number);
c906108c 4003 }
35df4500
TJB
4004 else if (bl->owner->type == bp_catchpoint
4005 && breakpoint_enabled (bl->owner)
4006 && !bl->duplicate)
ce78b96d 4007 {
77b06cd7
TJB
4008 gdb_assert (bl->owner->ops != NULL
4009 && bl->owner->ops->remove_location != NULL);
ce78b96d 4010
77b06cd7 4011 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4012 if (val)
4013 return val;
77b06cd7 4014
35df4500 4015 bl->inserted = (is == mark_inserted);
ce78b96d 4016 }
c906108c
SS
4017
4018 return 0;
4019}
4020
6c95b8df 4021static int
35df4500 4022remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4023{
4024 int ret;
4025 struct cleanup *old_chain;
4026
35df4500
TJB
4027 /* BL is never in moribund_locations by our callers. */
4028 gdb_assert (bl->owner != NULL);
2bdf28a0 4029
1a853c52 4030 if (bl->permanent)
6c95b8df
PA
4031 /* Permanent breakpoints cannot be inserted or removed. */
4032 return 0;
4033
4034 /* The type of none suggests that owner is actually deleted.
4035 This should not ever happen. */
35df4500 4036 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4037
4038 old_chain = save_current_space_and_thread ();
4039
35df4500 4040 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4041
35df4500 4042 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4043
4044 do_cleanups (old_chain);
4045 return ret;
4046}
4047
c906108c
SS
4048/* Clear the "inserted" flag in all breakpoints. */
4049
25b22b0a 4050void
fba45db2 4051mark_breakpoints_out (void)
c906108c 4052{
35df4500 4053 struct bp_location *bl, **blp_tmp;
c906108c 4054
35df4500 4055 ALL_BP_LOCATIONS (bl, blp_tmp)
1a853c52
PA
4056 if (bl->pspace == current_program_space
4057 && !bl->permanent)
35df4500 4058 bl->inserted = 0;
c906108c
SS
4059}
4060
53a5351d
JM
4061/* Clear the "inserted" flag in all breakpoints and delete any
4062 breakpoints which should go away between runs of the program.
c906108c
SS
4063
4064 Plus other such housekeeping that has to be done for breakpoints
4065 between runs.
4066
53a5351d
JM
4067 Note: this function gets called at the end of a run (by
4068 generic_mourn_inferior) and when a run begins (by
4a64f543 4069 init_wait_for_inferior). */
c906108c
SS
4070
4071
4072
4073void
fba45db2 4074breakpoint_init_inferior (enum inf_context context)
c906108c 4075{
35df4500
TJB
4076 struct breakpoint *b, *b_tmp;
4077 struct bp_location *bl, **blp_tmp;
1c5cfe86 4078 int ix;
6c95b8df 4079 struct program_space *pspace = current_program_space;
c906108c 4080
50c71eaf
PA
4081 /* If breakpoint locations are shared across processes, then there's
4082 nothing to do. */
f5656ead 4083 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4084 return;
4085
1a853c52 4086 mark_breakpoints_out ();
075f6582 4087
35df4500 4088 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4089 {
6c95b8df
PA
4090 if (b->loc && b->loc->pspace != pspace)
4091 continue;
4092
c5aa993b
JM
4093 switch (b->type)
4094 {
4095 case bp_call_dummy:
e2e4d78b 4096 case bp_longjmp_call_dummy:
c906108c 4097
c5aa993b 4098 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4099 cause problems when the inferior is rerun, so we better get
4100 rid of it. */
4101
4102 case bp_watchpoint_scope:
4103
4104 /* Also get rid of scope breakpoints. */
4105
4106 case bp_shlib_event:
4107
4108 /* Also remove solib event breakpoints. Their addresses may
4109 have changed since the last time we ran the program.
4110 Actually we may now be debugging against different target;
4111 and so the solib backend that installed this breakpoint may
4112 not be used in by the target. E.g.,
4113
4114 (gdb) file prog-linux
4115 (gdb) run # native linux target
4116 ...
4117 (gdb) kill
4118 (gdb) file prog-win.exe
4119 (gdb) tar rem :9999 # remote Windows gdbserver.
4120 */
c906108c 4121
f59f708a
PA
4122 case bp_step_resume:
4123
4124 /* Also remove step-resume breakpoints. */
4125
7c16b83e
PA
4126 case bp_single_step:
4127
4128 /* Also remove single-step breakpoints. */
4129
c5aa993b
JM
4130 delete_breakpoint (b);
4131 break;
c906108c 4132
c5aa993b
JM
4133 case bp_watchpoint:
4134 case bp_hardware_watchpoint:
4135 case bp_read_watchpoint:
4136 case bp_access_watchpoint:
3a5c3e22
PA
4137 {
4138 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4139
3a5c3e22
PA
4140 /* Likewise for watchpoints on local expressions. */
4141 if (w->exp_valid_block != NULL)
4142 delete_breakpoint (b);
4143 else if (context == inf_starting)
4144 {
4145 /* Reset val field to force reread of starting value in
4146 insert_breakpoints. */
4147 if (w->val)
4148 value_free (w->val);
4149 w->val = NULL;
4150 w->val_valid = 0;
c860120c 4151 }
3a5c3e22 4152 }
c5aa993b
JM
4153 break;
4154 default:
c5aa993b
JM
4155 break;
4156 }
4157 }
1c5cfe86
PA
4158
4159 /* Get rid of the moribund locations. */
35df4500
TJB
4160 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4161 decref_bp_location (&bl);
1c5cfe86 4162 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4163}
4164
6c95b8df
PA
4165/* These functions concern about actual breakpoints inserted in the
4166 target --- to e.g. check if we need to do decr_pc adjustment or if
4167 we need to hop over the bkpt --- so we check for address space
4168 match, not program space. */
4169
c2c6d25f
JM
4170/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4171 exists at PC. It returns ordinary_breakpoint_here if it's an
4172 ordinary breakpoint, or permanent_breakpoint_here if it's a
4173 permanent breakpoint.
4174 - When continuing from a location with an ordinary breakpoint, we
4175 actually single step once before calling insert_breakpoints.
e5dd4106 4176 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4177 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4178 the target, to advance the PC past the breakpoint. */
c906108c 4179
c2c6d25f 4180enum breakpoint_here
6c95b8df 4181breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4182{
35df4500 4183 struct bp_location *bl, **blp_tmp;
c2c6d25f 4184 int any_breakpoint_here = 0;
c906108c 4185
35df4500 4186 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4187 {
35df4500
TJB
4188 if (bl->loc_type != bp_loc_software_breakpoint
4189 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4190 continue;
4191
f1310107 4192 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4193 if ((breakpoint_enabled (bl->owner)
1a853c52 4194 || bl->permanent)
f1310107 4195 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4196 {
4197 if (overlay_debugging
35df4500
TJB
4198 && section_is_overlay (bl->section)
4199 && !section_is_mapped (bl->section))
075f6582 4200 continue; /* unmapped overlay -- can't be a match */
1a853c52 4201 else if (bl->permanent)
075f6582
DJ
4202 return permanent_breakpoint_here;
4203 else
4204 any_breakpoint_here = 1;
4205 }
4206 }
c906108c 4207
c2c6d25f 4208 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4209}
4210
1c5cfe86
PA
4211/* Return true if there's a moribund breakpoint at PC. */
4212
4213int
6c95b8df 4214moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4215{
4216 struct bp_location *loc;
4217 int ix;
4218
4219 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4220 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4221 return 1;
4222
4223 return 0;
4224}
c2c6d25f 4225
f7ce857f
PA
4226/* Returns non-zero iff BL is inserted at PC, in address space
4227 ASPACE. */
4228
4229static int
4230bp_location_inserted_here_p (struct bp_location *bl,
4231 struct address_space *aspace, CORE_ADDR pc)
4232{
4233 if (bl->inserted
4234 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4235 aspace, pc))
4236 {
4237 if (overlay_debugging
4238 && section_is_overlay (bl->section)
4239 && !section_is_mapped (bl->section))
4240 return 0; /* unmapped overlay -- can't be a match */
4241 else
4242 return 1;
4243 }
4244 return 0;
4245}
4246
a1fd2fa5 4247/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4248
4249int
a1fd2fa5 4250breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4251{
f7ce857f
PA
4252 struct bp_location **blp, **blp_tmp = NULL;
4253 struct bp_location *bl;
c906108c 4254
f7ce857f 4255 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4256 {
f7ce857f
PA
4257 struct bp_location *bl = *blp;
4258
35df4500
TJB
4259 if (bl->loc_type != bp_loc_software_breakpoint
4260 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4261 continue;
4262
f7ce857f
PA
4263 if (bp_location_inserted_here_p (bl, aspace, pc))
4264 return 1;
c5aa993b 4265 }
c36b740a
VP
4266 return 0;
4267}
4268
a1fd2fa5
PA
4269/* This function returns non-zero iff there is a software breakpoint
4270 inserted at PC. */
c36b740a
VP
4271
4272int
a1fd2fa5
PA
4273software_breakpoint_inserted_here_p (struct address_space *aspace,
4274 CORE_ADDR pc)
4fa8626c 4275{
f7ce857f
PA
4276 struct bp_location **blp, **blp_tmp = NULL;
4277 struct bp_location *bl;
4fa8626c 4278
f7ce857f 4279 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4280 {
f7ce857f
PA
4281 struct bp_location *bl = *blp;
4282
35df4500 4283 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4284 continue;
4285
f7ce857f
PA
4286 if (bp_location_inserted_here_p (bl, aspace, pc))
4287 return 1;
4fa8626c
DJ
4288 }
4289
4290 return 0;
9c02b525
PA
4291}
4292
4293/* See breakpoint.h. */
4294
4295int
4296hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4297 CORE_ADDR pc)
4298{
4299 struct bp_location **blp, **blp_tmp = NULL;
4300 struct bp_location *bl;
4301
4302 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4303 {
4304 struct bp_location *bl = *blp;
4305
4306 if (bl->loc_type != bp_loc_hardware_breakpoint)
4307 continue;
4308
4309 if (bp_location_inserted_here_p (bl, aspace, pc))
4310 return 1;
4311 }
4312
4313 return 0;
4fa8626c
DJ
4314}
4315
9093389c
PA
4316int
4317hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4318 CORE_ADDR addr, ULONGEST len)
4319{
4320 struct breakpoint *bpt;
4321
4322 ALL_BREAKPOINTS (bpt)
4323 {
4324 struct bp_location *loc;
4325
4326 if (bpt->type != bp_hardware_watchpoint
4327 && bpt->type != bp_access_watchpoint)
4328 continue;
4329
4330 if (!breakpoint_enabled (bpt))
4331 continue;
4332
4333 for (loc = bpt->loc; loc; loc = loc->next)
4334 if (loc->pspace->aspace == aspace && loc->inserted)
4335 {
4336 CORE_ADDR l, h;
4337
4338 /* Check for intersection. */
4339 l = max (loc->address, addr);
4340 h = min (loc->address + loc->length, addr + len);
4341 if (l < h)
4342 return 1;
4343 }
4344 }
4345 return 0;
4346}
c906108c 4347\f
c5aa993b 4348
c906108c
SS
4349/* bpstat stuff. External routines' interfaces are documented
4350 in breakpoint.h. */
4351
4352int
c326b90e 4353is_catchpoint (struct breakpoint *ep)
c906108c 4354{
533be4dd 4355 return (ep->type == bp_catchpoint);
c906108c
SS
4356}
4357
f431efe5
PA
4358/* Frees any storage that is part of a bpstat. Does not walk the
4359 'next' chain. */
4360
4361static void
198757a8
VP
4362bpstat_free (bpstat bs)
4363{
4364 if (bs->old_val != NULL)
4365 value_free (bs->old_val);
9add0f1b 4366 decref_counted_command_line (&bs->commands);
f431efe5 4367 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4368 xfree (bs);
4369}
4370
c906108c
SS
4371/* Clear a bpstat so that it says we are not at any breakpoint.
4372 Also free any storage that is part of a bpstat. */
4373
4374void
fba45db2 4375bpstat_clear (bpstat *bsp)
c906108c
SS
4376{
4377 bpstat p;
4378 bpstat q;
4379
4380 if (bsp == 0)
4381 return;
4382 p = *bsp;
4383 while (p != NULL)
4384 {
4385 q = p->next;
198757a8 4386 bpstat_free (p);
c906108c
SS
4387 p = q;
4388 }
4389 *bsp = NULL;
4390}
4391
4392/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4393 is part of the bpstat is copied as well. */
4394
4395bpstat
fba45db2 4396bpstat_copy (bpstat bs)
c906108c
SS
4397{
4398 bpstat p = NULL;
4399 bpstat tmp;
4400 bpstat retval = NULL;
4401
4402 if (bs == NULL)
4403 return bs;
4404
4405 for (; bs != NULL; bs = bs->next)
4406 {
4407 tmp = (bpstat) xmalloc (sizeof (*tmp));
4408 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4409 incref_counted_command_line (tmp->commands);
f431efe5 4410 incref_bp_location (tmp->bp_location_at);
31cc81e9 4411 if (bs->old_val != NULL)
3c3185ac
JK
4412 {
4413 tmp->old_val = value_copy (bs->old_val);
4414 release_value (tmp->old_val);
4415 }
31cc81e9 4416
c906108c
SS
4417 if (p == NULL)
4418 /* This is the first thing in the chain. */
4419 retval = tmp;
4420 else
4421 p->next = tmp;
4422 p = tmp;
4423 }
4424 p->next = NULL;
4425 return retval;
4426}
4427
4a64f543 4428/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4429
4430bpstat
fba45db2 4431bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4432{
c5aa993b
JM
4433 if (bsp == NULL)
4434 return NULL;
c906108c 4435
c5aa993b
JM
4436 for (; bsp != NULL; bsp = bsp->next)
4437 {
f431efe5 4438 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4439 return bsp;
4440 }
c906108c
SS
4441 return NULL;
4442}
4443
ab04a2af
TT
4444/* See breakpoint.h. */
4445
47591c29 4446int
427cd150 4447bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4448{
ab04a2af
TT
4449 for (; bsp != NULL; bsp = bsp->next)
4450 {
427cd150
TT
4451 if (bsp->breakpoint_at == NULL)
4452 {
4453 /* A moribund location can never explain a signal other than
4454 GDB_SIGNAL_TRAP. */
4455 if (sig == GDB_SIGNAL_TRAP)
47591c29 4456 return 1;
427cd150
TT
4457 }
4458 else
47591c29
PA
4459 {
4460 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4461 sig))
4462 return 1;
4463 }
ab04a2af
TT
4464 }
4465
47591c29 4466 return 0;
ab04a2af
TT
4467}
4468
4a64f543
MS
4469/* Put in *NUM the breakpoint number of the first breakpoint we are
4470 stopped at. *BSP upon return is a bpstat which points to the
4471 remaining breakpoints stopped at (but which is not guaranteed to be
4472 good for anything but further calls to bpstat_num).
4473
8671a17b
PA
4474 Return 0 if passed a bpstat which does not indicate any breakpoints.
4475 Return -1 if stopped at a breakpoint that has been deleted since
4476 we set it.
4477 Return 1 otherwise. */
c906108c
SS
4478
4479int
8671a17b 4480bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4481{
4482 struct breakpoint *b;
4483
4484 if ((*bsp) == NULL)
4485 return 0; /* No more breakpoint values */
8671a17b 4486
4a64f543
MS
4487 /* We assume we'll never have several bpstats that correspond to a
4488 single breakpoint -- otherwise, this function might return the
4489 same number more than once and this will look ugly. */
f431efe5 4490 b = (*bsp)->breakpoint_at;
8671a17b
PA
4491 *bsp = (*bsp)->next;
4492 if (b == NULL)
4493 return -1; /* breakpoint that's been deleted since */
4494
4495 *num = b->number; /* We have its number */
4496 return 1;
c906108c
SS
4497}
4498
e93ca019 4499/* See breakpoint.h. */
c906108c
SS
4500
4501void
e93ca019 4502bpstat_clear_actions (void)
c906108c 4503{
e93ca019
JK
4504 struct thread_info *tp;
4505 bpstat bs;
4506
4507 if (ptid_equal (inferior_ptid, null_ptid))
4508 return;
4509
4510 tp = find_thread_ptid (inferior_ptid);
4511 if (tp == NULL)
4512 return;
4513
4514 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4515 {
9add0f1b 4516 decref_counted_command_line (&bs->commands);
abf85f46 4517
c906108c
SS
4518 if (bs->old_val != NULL)
4519 {
4520 value_free (bs->old_val);
4521 bs->old_val = NULL;
4522 }
4523 }
4524}
4525
f3b1572e
PA
4526/* Called when a command is about to proceed the inferior. */
4527
4528static void
4529breakpoint_about_to_proceed (void)
4530{
4531 if (!ptid_equal (inferior_ptid, null_ptid))
4532 {
4533 struct thread_info *tp = inferior_thread ();
4534
4535 /* Allow inferior function calls in breakpoint commands to not
4536 interrupt the command list. When the call finishes
4537 successfully, the inferior will be standing at the same
4538 breakpoint as if nothing happened. */
16c381f0 4539 if (tp->control.in_infcall)
f3b1572e
PA
4540 return;
4541 }
4542
4543 breakpoint_proceeded = 1;
4544}
4545
4a64f543
MS
4546/* Stub for cleaning up our state if we error-out of a breakpoint
4547 command. */
c906108c 4548static void
4efb68b1 4549cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4550{
4551 executing_breakpoint_commands = 0;
4552}
4553
abf85f46
JK
4554/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4555 or its equivalent. */
4556
4557static int
4558command_line_is_silent (struct command_line *cmd)
4559{
4560 return cmd && (strcmp ("silent", cmd->line) == 0
4561 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4562}
4563
4a64f543
MS
4564/* Execute all the commands associated with all the breakpoints at
4565 this location. Any of these commands could cause the process to
4566 proceed beyond this point, etc. We look out for such changes by
4567 checking the global "breakpoint_proceeded" after each command.
c906108c 4568
347bddb7
PA
4569 Returns true if a breakpoint command resumed the inferior. In that
4570 case, it is the caller's responsibility to recall it again with the
4571 bpstat of the current thread. */
4572
4573static int
4574bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4575{
4576 bpstat bs;
4577 struct cleanup *old_chain;
347bddb7 4578 int again = 0;
c906108c
SS
4579
4580 /* Avoid endless recursion if a `source' command is contained
4581 in bs->commands. */
4582 if (executing_breakpoint_commands)
347bddb7 4583 return 0;
c906108c
SS
4584
4585 executing_breakpoint_commands = 1;
4586 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4587
cf6c5ffb
TT
4588 prevent_dont_repeat ();
4589
4a64f543 4590 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4591 bs = *bsp;
4592
4593 breakpoint_proceeded = 0;
4594 for (; bs != NULL; bs = bs->next)
4595 {
9add0f1b 4596 struct counted_command_line *ccmd;
6c50ab1c
JB
4597 struct command_line *cmd;
4598 struct cleanup *this_cmd_tree_chain;
4599
4600 /* Take ownership of the BSP's command tree, if it has one.
4601
4602 The command tree could legitimately contain commands like
4603 'step' and 'next', which call clear_proceed_status, which
4604 frees stop_bpstat's command tree. To make sure this doesn't
4605 free the tree we're executing out from under us, we need to
4606 take ownership of the tree ourselves. Since a given bpstat's
4607 commands are only executed once, we don't need to copy it; we
4608 can clear the pointer in the bpstat, and make sure we free
4609 the tree when we're done. */
9add0f1b
TT
4610 ccmd = bs->commands;
4611 bs->commands = NULL;
abf85f46
JK
4612 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4613 cmd = ccmd ? ccmd->commands : NULL;
4614 if (command_line_is_silent (cmd))
4615 {
4616 /* The action has been already done by bpstat_stop_status. */
4617 cmd = cmd->next;
4618 }
6c50ab1c 4619
c906108c
SS
4620 while (cmd != NULL)
4621 {
4622 execute_control_command (cmd);
4623
4624 if (breakpoint_proceeded)
4625 break;
4626 else
4627 cmd = cmd->next;
4628 }
6c50ab1c
JB
4629
4630 /* We can free this command tree now. */
4631 do_cleanups (this_cmd_tree_chain);
4632
c906108c 4633 if (breakpoint_proceeded)
32c1e744 4634 {
5589af0e 4635 if (interpreter_async && target_can_async_p ())
347bddb7
PA
4636 /* If we are in async mode, then the target might be still
4637 running, not stopped at any breakpoint, so nothing for
4638 us to do here -- just return to the event loop. */
4639 ;
32c1e744
VP
4640 else
4641 /* In sync mode, when execute_control_command returns
4642 we're already standing on the next breakpoint.
347bddb7
PA
4643 Breakpoint commands for that stop were not run, since
4644 execute_command does not run breakpoint commands --
4645 only command_line_handler does, but that one is not
4646 involved in execution of breakpoint commands. So, we
4647 can now execute breakpoint commands. It should be
4648 noted that making execute_command do bpstat actions is
4649 not an option -- in this case we'll have recursive
4650 invocation of bpstat for each breakpoint with a
4651 command, and can easily blow up GDB stack. Instead, we
4652 return true, which will trigger the caller to recall us
4653 with the new stop_bpstat. */
4654 again = 1;
4655 break;
32c1e744 4656 }
c906108c 4657 }
c2b8ed2c 4658 do_cleanups (old_chain);
347bddb7
PA
4659 return again;
4660}
4661
4662void
4663bpstat_do_actions (void)
4664{
353d1d73
JK
4665 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4666
347bddb7
PA
4667 /* Do any commands attached to breakpoint we are stopped at. */
4668 while (!ptid_equal (inferior_ptid, null_ptid)
4669 && target_has_execution
4670 && !is_exited (inferior_ptid)
4671 && !is_executing (inferior_ptid))
4672 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4673 and only return when it is stopped at the next breakpoint, we
4674 keep doing breakpoint actions until it returns false to
4675 indicate the inferior was not resumed. */
16c381f0 4676 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4677 break;
353d1d73
JK
4678
4679 discard_cleanups (cleanup_if_error);
c906108c
SS
4680}
4681
fa4727a6
DJ
4682/* Print out the (old or new) value associated with a watchpoint. */
4683
4684static void
4685watchpoint_value_print (struct value *val, struct ui_file *stream)
4686{
4687 if (val == NULL)
4688 fprintf_unfiltered (stream, _("<unreadable>"));
4689 else
79a45b7d
TT
4690 {
4691 struct value_print_options opts;
4692 get_user_print_options (&opts);
4693 value_print (val, stream, &opts);
4694 }
fa4727a6
DJ
4695}
4696
e514a9d6 4697/* Generic routine for printing messages indicating why we
4a64f543 4698 stopped. The behavior of this function depends on the value
e514a9d6
JM
4699 'print_it' in the bpstat structure. Under some circumstances we
4700 may decide not to print anything here and delegate the task to
4a64f543 4701 normal_stop(). */
e514a9d6
JM
4702
4703static enum print_stop_action
4704print_bp_stop_message (bpstat bs)
4705{
4706 switch (bs->print_it)
4707 {
4708 case print_it_noop:
4a64f543 4709 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4710 return PRINT_UNKNOWN;
4711 break;
4712
4713 case print_it_done:
4714 /* We still want to print the frame, but we already printed the
4a64f543 4715 relevant messages. */
e514a9d6
JM
4716 return PRINT_SRC_AND_LOC;
4717 break;
4718
4719 case print_it_normal:
4f8d1dc6 4720 {
f431efe5
PA
4721 struct breakpoint *b = bs->breakpoint_at;
4722
1a6a67de
TJB
4723 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4724 which has since been deleted. */
4725 if (b == NULL)
4726 return PRINT_UNKNOWN;
4727
348d480f
PA
4728 /* Normal case. Call the breakpoint's print_it method. */
4729 return b->ops->print_it (bs);
4f8d1dc6 4730 }
348d480f 4731 break;
3086aeae 4732
e514a9d6 4733 default:
8e65ff28 4734 internal_error (__FILE__, __LINE__,
e2e0b3e5 4735 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4736 break;
c906108c 4737 }
c906108c
SS
4738}
4739
edcc5120
TT
4740/* A helper function that prints a shared library stopped event. */
4741
4742static void
4743print_solib_event (int is_catchpoint)
4744{
4745 int any_deleted
4746 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4747 int any_added
4748 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4749
4750 if (!is_catchpoint)
4751 {
4752 if (any_added || any_deleted)
4753 ui_out_text (current_uiout,
4754 _("Stopped due to shared library event:\n"));
4755 else
4756 ui_out_text (current_uiout,
4757 _("Stopped due to shared library event (no "
4758 "libraries added or removed)\n"));
4759 }
4760
4761 if (ui_out_is_mi_like_p (current_uiout))
4762 ui_out_field_string (current_uiout, "reason",
4763 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4764
4765 if (any_deleted)
4766 {
4767 struct cleanup *cleanup;
4768 char *name;
4769 int ix;
4770
4771 ui_out_text (current_uiout, _(" Inferior unloaded "));
4772 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4773 "removed");
4774 for (ix = 0;
4775 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4776 ix, name);
4777 ++ix)
4778 {
4779 if (ix > 0)
4780 ui_out_text (current_uiout, " ");
4781 ui_out_field_string (current_uiout, "library", name);
4782 ui_out_text (current_uiout, "\n");
4783 }
4784
4785 do_cleanups (cleanup);
4786 }
4787
4788 if (any_added)
4789 {
4790 struct so_list *iter;
4791 int ix;
4792 struct cleanup *cleanup;
4793
4794 ui_out_text (current_uiout, _(" Inferior loaded "));
4795 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4796 "added");
4797 for (ix = 0;
4798 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4799 ix, iter);
4800 ++ix)
4801 {
4802 if (ix > 0)
4803 ui_out_text (current_uiout, " ");
4804 ui_out_field_string (current_uiout, "library", iter->so_name);
4805 ui_out_text (current_uiout, "\n");
4806 }
4807
4808 do_cleanups (cleanup);
4809 }
4810}
4811
e514a9d6
JM
4812/* Print a message indicating what happened. This is called from
4813 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4814 list - a list of the eventpoints that caused this stop. KIND is
4815 the target_waitkind for the stopping event. This
e514a9d6
JM
4816 routine calls the generic print routine for printing a message
4817 about reasons for stopping. This will print (for example) the
4818 "Breakpoint n," part of the output. The return value of this
4819 routine is one of:
c906108c 4820
4a64f543 4821 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4822 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4823 code to print the location. An example is
c5aa993b
JM
4824 "Breakpoint 1, " which should be followed by
4825 the location.
917317f4 4826 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4827 to also print the location part of the message.
4828 An example is the catch/throw messages, which
4a64f543 4829 don't require a location appended to the end.
917317f4 4830 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4831 further info to be printed. */
c906108c 4832
917317f4 4833enum print_stop_action
36dfb11c 4834bpstat_print (bpstat bs, int kind)
c906108c
SS
4835{
4836 int val;
c5aa993b 4837
c906108c 4838 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4839 (Currently all watchpoints go on the bpstat whether hit or not.
4840 That probably could (should) be changed, provided care is taken
c906108c 4841 with respect to bpstat_explains_signal). */
e514a9d6
JM
4842 for (; bs; bs = bs->next)
4843 {
4844 val = print_bp_stop_message (bs);
4845 if (val == PRINT_SRC_ONLY
4846 || val == PRINT_SRC_AND_LOC
4847 || val == PRINT_NOTHING)
4848 return val;
4849 }
c906108c 4850
36dfb11c
TT
4851 /* If we had hit a shared library event breakpoint,
4852 print_bp_stop_message would print out this message. If we hit an
4853 OS-level shared library event, do the same thing. */
4854 if (kind == TARGET_WAITKIND_LOADED)
4855 {
edcc5120 4856 print_solib_event (0);
36dfb11c
TT
4857 return PRINT_NOTHING;
4858 }
4859
e514a9d6 4860 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4861 with and nothing was printed. */
917317f4 4862 return PRINT_UNKNOWN;
c906108c
SS
4863}
4864
c42bd95a
DE
4865/* Evaluate the expression EXP and return 1 if value is zero.
4866 This returns the inverse of the condition because it is called
4867 from catch_errors which returns 0 if an exception happened, and if an
4868 exception happens we want execution to stop.
4a64f543 4869 The argument is a "struct expression *" that has been cast to a
c42bd95a 4870 "void *" to make it pass through catch_errors. */
c906108c
SS
4871
4872static int
4efb68b1 4873breakpoint_cond_eval (void *exp)
c906108c 4874{
278cd55f 4875 struct value *mark = value_mark ();
c5aa993b 4876 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4877
c906108c
SS
4878 value_free_to_mark (mark);
4879 return i;
4880}
4881
5760d0ab 4882/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4883
4884static bpstat
5760d0ab 4885bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4886{
4887 bpstat bs;
4888
4889 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4890 bs->next = NULL;
4891 **bs_link_pointer = bs;
4892 *bs_link_pointer = &bs->next;
f431efe5
PA
4893 bs->breakpoint_at = bl->owner;
4894 bs->bp_location_at = bl;
4895 incref_bp_location (bl);
c906108c
SS
4896 /* If the condition is false, etc., don't do the commands. */
4897 bs->commands = NULL;
4898 bs->old_val = NULL;
4899 bs->print_it = print_it_normal;
4900 return bs;
4901}
4902\f
d983da9c
DJ
4903/* The target has stopped with waitstatus WS. Check if any hardware
4904 watchpoints have triggered, according to the target. */
4905
4906int
4907watchpoints_triggered (struct target_waitstatus *ws)
4908{
d92524f1 4909 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4910 CORE_ADDR addr;
4911 struct breakpoint *b;
4912
4913 if (!stopped_by_watchpoint)
4914 {
4915 /* We were not stopped by a watchpoint. Mark all watchpoints
4916 as not triggered. */
4917 ALL_BREAKPOINTS (b)
cc60f2e3 4918 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4919 {
4920 struct watchpoint *w = (struct watchpoint *) b;
4921
4922 w->watchpoint_triggered = watch_triggered_no;
4923 }
d983da9c
DJ
4924
4925 return 0;
4926 }
4927
4928 if (!target_stopped_data_address (&current_target, &addr))
4929 {
4930 /* We were stopped by a watchpoint, but we don't know where.
4931 Mark all watchpoints as unknown. */
4932 ALL_BREAKPOINTS (b)
cc60f2e3 4933 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4934 {
4935 struct watchpoint *w = (struct watchpoint *) b;
4936
4937 w->watchpoint_triggered = watch_triggered_unknown;
4938 }
d983da9c 4939
3c4797ba 4940 return 1;
d983da9c
DJ
4941 }
4942
4943 /* The target could report the data address. Mark watchpoints
4944 affected by this data address as triggered, and all others as not
4945 triggered. */
4946
4947 ALL_BREAKPOINTS (b)
cc60f2e3 4948 if (is_hardware_watchpoint (b))
d983da9c 4949 {
3a5c3e22 4950 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4951 struct bp_location *loc;
d983da9c 4952
3a5c3e22 4953 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4954 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4955 {
3a5c3e22 4956 if (is_masked_watchpoint (b))
9c06b0b4 4957 {
3a5c3e22
PA
4958 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4959 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4960
4961 if (newaddr == start)
4962 {
3a5c3e22 4963 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4964 break;
4965 }
4966 }
4967 /* Exact match not required. Within range is sufficient. */
4968 else if (target_watchpoint_addr_within_range (&current_target,
4969 addr, loc->address,
4970 loc->length))
4971 {
3a5c3e22 4972 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4973 break;
4974 }
4975 }
d983da9c
DJ
4976 }
4977
4978 return 1;
4979}
4980
c906108c
SS
4981/* Possible return values for watchpoint_check (this can't be an enum
4982 because of check_errors). */
4983/* The watchpoint has been deleted. */
4984#define WP_DELETED 1
4985/* The value has changed. */
4986#define WP_VALUE_CHANGED 2
4987/* The value has not changed. */
4988#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4989/* Ignore this watchpoint, no matter if the value changed or not. */
4990#define WP_IGNORE 4
c906108c
SS
4991
4992#define BP_TEMPFLAG 1
4993#define BP_HARDWAREFLAG 2
4994
4a64f543
MS
4995/* Evaluate watchpoint condition expression and check if its value
4996 changed.
553e4c11
JB
4997
4998 P should be a pointer to struct bpstat, but is defined as a void *
4999 in order for this function to be usable with catch_errors. */
c906108c
SS
5000
5001static int
4efb68b1 5002watchpoint_check (void *p)
c906108c
SS
5003{
5004 bpstat bs = (bpstat) p;
3a5c3e22 5005 struct watchpoint *b;
c906108c
SS
5006 struct frame_info *fr;
5007 int within_current_scope;
5008
f431efe5 5009 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5010 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5011 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5012
f6bc2008
PA
5013 /* If this is a local watchpoint, we only want to check if the
5014 watchpoint frame is in scope if the current thread is the thread
5015 that was used to create the watchpoint. */
5016 if (!watchpoint_in_thread_scope (b))
60e1c644 5017 return WP_IGNORE;
f6bc2008 5018
c906108c
SS
5019 if (b->exp_valid_block == NULL)
5020 within_current_scope = 1;
5021 else
5022 {
edb3359d
DJ
5023 struct frame_info *frame = get_current_frame ();
5024 struct gdbarch *frame_arch = get_frame_arch (frame);
5025 CORE_ADDR frame_pc = get_frame_pc (frame);
5026
4a64f543
MS
5027 /* in_function_epilogue_p() returns a non-zero value if we're
5028 still in the function but the stack frame has already been
5029 invalidated. Since we can't rely on the values of local
5030 variables after the stack has been destroyed, we are treating
5031 the watchpoint in that state as `not changed' without further
5032 checking. Don't mark watchpoints as changed if the current
5033 frame is in an epilogue - even if they are in some other
5034 frame, our view of the stack is likely to be wrong and
5035 frame_find_by_id could error out. */
a0f49112 5036 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 5037 return WP_IGNORE;
a0f49112 5038
101dcfbe 5039 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5040 within_current_scope = (fr != NULL);
69fbadd5
DJ
5041
5042 /* If we've gotten confused in the unwinder, we might have
5043 returned a frame that can't describe this variable. */
edb3359d
DJ
5044 if (within_current_scope)
5045 {
5046 struct symbol *function;
5047
5048 function = get_frame_function (fr);
5049 if (function == NULL
5050 || !contained_in (b->exp_valid_block,
5051 SYMBOL_BLOCK_VALUE (function)))
5052 within_current_scope = 0;
5053 }
69fbadd5 5054
edb3359d 5055 if (within_current_scope)
c906108c
SS
5056 /* If we end up stopping, the current frame will get selected
5057 in normal_stop. So this call to select_frame won't affect
5058 the user. */
0f7d239c 5059 select_frame (fr);
c906108c 5060 }
c5aa993b 5061
c906108c
SS
5062 if (within_current_scope)
5063 {
4a64f543
MS
5064 /* We use value_{,free_to_}mark because it could be a *long*
5065 time before we return to the command level and call
5066 free_all_values. We can't call free_all_values because we
5067 might be in the middle of evaluating a function call. */
c906108c 5068
0cf6dd15 5069 int pc = 0;
9c06b0b4 5070 struct value *mark;
fa4727a6
DJ
5071 struct value *new_val;
5072
3a5c3e22 5073 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5074 /* Since we don't know the exact trigger address (from
5075 stopped_data_address), just tell the user we've triggered
5076 a mask watchpoint. */
5077 return WP_VALUE_CHANGED;
5078
5079 mark = value_mark ();
3a1115a0 5080 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5081
bb9d5f81
PP
5082 if (b->val_bitsize != 0)
5083 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5084
4a64f543
MS
5085 /* We use value_equal_contents instead of value_equal because
5086 the latter coerces an array to a pointer, thus comparing just
5087 the address of the array instead of its contents. This is
5088 not what we want. */
fa4727a6 5089 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5090 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5091 {
fa4727a6
DJ
5092 if (new_val != NULL)
5093 {
5094 release_value (new_val);
5095 value_free_to_mark (mark);
5096 }
c906108c
SS
5097 bs->old_val = b->val;
5098 b->val = new_val;
fa4727a6 5099 b->val_valid = 1;
c906108c
SS
5100 return WP_VALUE_CHANGED;
5101 }
5102 else
5103 {
60e1c644 5104 /* Nothing changed. */
c906108c 5105 value_free_to_mark (mark);
c906108c
SS
5106 return WP_VALUE_NOT_CHANGED;
5107 }
5108 }
5109 else
5110 {
79a45e25
PA
5111 struct ui_out *uiout = current_uiout;
5112
c906108c 5113 /* This seems like the only logical thing to do because
c5aa993b
JM
5114 if we temporarily ignored the watchpoint, then when
5115 we reenter the block in which it is valid it contains
5116 garbage (in the case of a function, it may have two
5117 garbage values, one before and one after the prologue).
5118 So we can't even detect the first assignment to it and
5119 watch after that (since the garbage may or may not equal
5120 the first value assigned). */
348d480f
PA
5121 /* We print all the stop information in
5122 breakpoint_ops->print_it, but in this case, by the time we
5123 call breakpoint_ops->print_it this bp will be deleted
5124 already. So we have no choice but print the information
5125 here. */
9dc5e2a9 5126 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5127 ui_out_field_string
5128 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5129 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5130 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5131 ui_out_text (uiout,
5132 " deleted because the program has left the block in\n\
8b93c638 5133which its expression is valid.\n");
4ce44c66 5134
cdac0397 5135 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5136 decref_counted_command_line (&b->base.commands);
d0fb5eae 5137 watchpoint_del_at_next_stop (b);
c906108c
SS
5138
5139 return WP_DELETED;
5140 }
5141}
5142
18a18393 5143/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5144 breakpoint location BL. This function does not check if we should
5145 stop, only if BL explains the stop. */
5146
18a18393 5147static int
6c95b8df 5148bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5149 struct address_space *aspace, CORE_ADDR bp_addr,
5150 const struct target_waitstatus *ws)
18a18393
VP
5151{
5152 struct breakpoint *b = bl->owner;
5153
348d480f 5154 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5155 gdb_assert (b != NULL);
5156
09ac7c10 5157 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5158}
5159
3a5c3e22
PA
5160/* Determine if the watched values have actually changed, and we
5161 should stop. If not, set BS->stop to 0. */
5162
18a18393
VP
5163static void
5164bpstat_check_watchpoint (bpstat bs)
5165{
2bdf28a0 5166 const struct bp_location *bl;
3a5c3e22 5167 struct watchpoint *b;
2bdf28a0
JK
5168
5169 /* BS is built for existing struct breakpoint. */
f431efe5 5170 bl = bs->bp_location_at;
2bdf28a0 5171 gdb_assert (bl != NULL);
3a5c3e22 5172 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5173 gdb_assert (b != NULL);
18a18393 5174
18a18393 5175 {
18a18393
VP
5176 int must_check_value = 0;
5177
3a5c3e22 5178 if (b->base.type == bp_watchpoint)
18a18393
VP
5179 /* For a software watchpoint, we must always check the
5180 watched value. */
5181 must_check_value = 1;
5182 else if (b->watchpoint_triggered == watch_triggered_yes)
5183 /* We have a hardware watchpoint (read, write, or access)
5184 and the target earlier reported an address watched by
5185 this watchpoint. */
5186 must_check_value = 1;
5187 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5188 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5189 /* We were stopped by a hardware watchpoint, but the target could
5190 not report the data address. We must check the watchpoint's
5191 value. Access and read watchpoints are out of luck; without
5192 a data address, we can't figure it out. */
5193 must_check_value = 1;
3a5c3e22 5194
18a18393
VP
5195 if (must_check_value)
5196 {
3e43a32a
MS
5197 char *message
5198 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5199 b->base.number);
18a18393
VP
5200 struct cleanup *cleanups = make_cleanup (xfree, message);
5201 int e = catch_errors (watchpoint_check, bs, message,
5202 RETURN_MASK_ALL);
5203 do_cleanups (cleanups);
5204 switch (e)
5205 {
5206 case WP_DELETED:
5207 /* We've already printed what needs to be printed. */
5208 bs->print_it = print_it_done;
5209 /* Stop. */
5210 break;
60e1c644
PA
5211 case WP_IGNORE:
5212 bs->print_it = print_it_noop;
5213 bs->stop = 0;
5214 break;
18a18393 5215 case WP_VALUE_CHANGED:
3a5c3e22 5216 if (b->base.type == bp_read_watchpoint)
18a18393 5217 {
85d721b8
PA
5218 /* There are two cases to consider here:
5219
4a64f543 5220 1. We're watching the triggered memory for reads.
85d721b8
PA
5221 In that case, trust the target, and always report
5222 the watchpoint hit to the user. Even though
5223 reads don't cause value changes, the value may
5224 have changed since the last time it was read, and
5225 since we're not trapping writes, we will not see
5226 those, and as such we should ignore our notion of
5227 old value.
5228
4a64f543 5229 2. We're watching the triggered memory for both
85d721b8
PA
5230 reads and writes. There are two ways this may
5231 happen:
5232
4a64f543 5233 2.1. This is a target that can't break on data
85d721b8
PA
5234 reads only, but can break on accesses (reads or
5235 writes), such as e.g., x86. We detect this case
5236 at the time we try to insert read watchpoints.
5237
4a64f543 5238 2.2. Otherwise, the target supports read
85d721b8
PA
5239 watchpoints, but, the user set an access or write
5240 watchpoint watching the same memory as this read
5241 watchpoint.
5242
5243 If we're watching memory writes as well as reads,
5244 ignore watchpoint hits when we find that the
5245 value hasn't changed, as reads don't cause
5246 changes. This still gives false positives when
5247 the program writes the same value to memory as
5248 what there was already in memory (we will confuse
5249 it for a read), but it's much better than
5250 nothing. */
5251
5252 int other_write_watchpoint = 0;
5253
5254 if (bl->watchpoint_type == hw_read)
5255 {
5256 struct breakpoint *other_b;
5257
5258 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5259 if (other_b->type == bp_hardware_watchpoint
5260 || other_b->type == bp_access_watchpoint)
85d721b8 5261 {
3a5c3e22
PA
5262 struct watchpoint *other_w =
5263 (struct watchpoint *) other_b;
5264
5265 if (other_w->watchpoint_triggered
5266 == watch_triggered_yes)
5267 {
5268 other_write_watchpoint = 1;
5269 break;
5270 }
85d721b8
PA
5271 }
5272 }
5273
5274 if (other_write_watchpoint
5275 || bl->watchpoint_type == hw_access)
5276 {
5277 /* We're watching the same memory for writes,
5278 and the value changed since the last time we
5279 updated it, so this trap must be for a write.
5280 Ignore it. */
5281 bs->print_it = print_it_noop;
5282 bs->stop = 0;
5283 }
18a18393
VP
5284 }
5285 break;
5286 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5287 if (b->base.type == bp_hardware_watchpoint
5288 || b->base.type == bp_watchpoint)
18a18393
VP
5289 {
5290 /* Don't stop: write watchpoints shouldn't fire if
5291 the value hasn't changed. */
5292 bs->print_it = print_it_noop;
5293 bs->stop = 0;
5294 }
5295 /* Stop. */
5296 break;
5297 default:
5298 /* Can't happen. */
5299 case 0:
5300 /* Error from catch_errors. */
3a5c3e22 5301 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5302 watchpoint_del_at_next_stop (b);
18a18393
VP
5303 /* We've already printed what needs to be printed. */
5304 bs->print_it = print_it_done;
5305 break;
5306 }
5307 }
5308 else /* must_check_value == 0 */
5309 {
5310 /* This is a case where some watchpoint(s) triggered, but
5311 not at the address of this watchpoint, or else no
5312 watchpoint triggered after all. So don't print
5313 anything for this watchpoint. */
5314 bs->print_it = print_it_noop;
5315 bs->stop = 0;
5316 }
5317 }
5318}
5319
7d4df6a4
DE
5320/* For breakpoints that are currently marked as telling gdb to stop,
5321 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5322 of breakpoint referred to by BS. If we should not stop for this
5323 breakpoint, set BS->stop to 0. */
f431efe5 5324
18a18393
VP
5325static void
5326bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5327{
2bdf28a0
JK
5328 const struct bp_location *bl;
5329 struct breakpoint *b;
7d4df6a4
DE
5330 int value_is_zero = 0;
5331 struct expression *cond;
5332
5333 gdb_assert (bs->stop);
2bdf28a0
JK
5334
5335 /* BS is built for existing struct breakpoint. */
f431efe5 5336 bl = bs->bp_location_at;
2bdf28a0 5337 gdb_assert (bl != NULL);
f431efe5 5338 b = bs->breakpoint_at;
2bdf28a0 5339 gdb_assert (b != NULL);
18a18393 5340
b775012e
LM
5341 /* Even if the target evaluated the condition on its end and notified GDB, we
5342 need to do so again since GDB does not know if we stopped due to a
5343 breakpoint or a single step breakpoint. */
5344
18a18393 5345 if (frame_id_p (b->frame_id)
edb3359d 5346 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5347 {
7d4df6a4
DE
5348 bs->stop = 0;
5349 return;
5350 }
60e1c644 5351
12ab52e9
PA
5352 /* If this is a thread/task-specific breakpoint, don't waste cpu
5353 evaluating the condition if this isn't the specified
5354 thread/task. */
5355 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5356 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5357
6c1b0f7b
DE
5358 {
5359 bs->stop = 0;
5360 return;
5361 }
5362
6dddc817
DE
5363 /* Evaluate extension language breakpoints that have a "stop" method
5364 implemented. */
5365 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5366
7d4df6a4
DE
5367 if (is_watchpoint (b))
5368 {
5369 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5370
7d4df6a4
DE
5371 cond = w->cond_exp;
5372 }
5373 else
5374 cond = bl->cond;
60e1c644 5375
7d4df6a4
DE
5376 if (cond && b->disposition != disp_del_at_next_stop)
5377 {
5378 int within_current_scope = 1;
5379 struct watchpoint * w;
60e1c644 5380
7d4df6a4
DE
5381 /* We use value_mark and value_free_to_mark because it could
5382 be a long time before we return to the command level and
5383 call free_all_values. We can't call free_all_values
5384 because we might be in the middle of evaluating a
5385 function call. */
5386 struct value *mark = value_mark ();
5387
5388 if (is_watchpoint (b))
5389 w = (struct watchpoint *) b;
5390 else
5391 w = NULL;
5392
5393 /* Need to select the frame, with all that implies so that
5394 the conditions will have the right context. Because we
5395 use the frame, we will not see an inlined function's
5396 variables when we arrive at a breakpoint at the start
5397 of the inlined function; the current frame will be the
5398 call site. */
5399 if (w == NULL || w->cond_exp_valid_block == NULL)
5400 select_frame (get_current_frame ());
5401 else
18a18393 5402 {
7d4df6a4
DE
5403 struct frame_info *frame;
5404
5405 /* For local watchpoint expressions, which particular
5406 instance of a local is being watched matters, so we
5407 keep track of the frame to evaluate the expression
5408 in. To evaluate the condition however, it doesn't
5409 really matter which instantiation of the function
5410 where the condition makes sense triggers the
5411 watchpoint. This allows an expression like "watch
5412 global if q > 10" set in `func', catch writes to
5413 global on all threads that call `func', or catch
5414 writes on all recursive calls of `func' by a single
5415 thread. We simply always evaluate the condition in
5416 the innermost frame that's executing where it makes
5417 sense to evaluate the condition. It seems
5418 intuitive. */
5419 frame = block_innermost_frame (w->cond_exp_valid_block);
5420 if (frame != NULL)
5421 select_frame (frame);
5422 else
5423 within_current_scope = 0;
18a18393 5424 }
7d4df6a4
DE
5425 if (within_current_scope)
5426 value_is_zero
5427 = catch_errors (breakpoint_cond_eval, cond,
5428 "Error in testing breakpoint condition:\n",
5429 RETURN_MASK_ALL);
5430 else
18a18393 5431 {
7d4df6a4
DE
5432 warning (_("Watchpoint condition cannot be tested "
5433 "in the current scope"));
5434 /* If we failed to set the right context for this
5435 watchpoint, unconditionally report it. */
5436 value_is_zero = 0;
18a18393 5437 }
7d4df6a4
DE
5438 /* FIXME-someday, should give breakpoint #. */
5439 value_free_to_mark (mark);
18a18393 5440 }
7d4df6a4
DE
5441
5442 if (cond && value_is_zero)
5443 {
5444 bs->stop = 0;
5445 }
7d4df6a4
DE
5446 else if (b->ignore_count > 0)
5447 {
5448 b->ignore_count--;
5449 bs->stop = 0;
5450 /* Increase the hit count even though we don't stop. */
5451 ++(b->hit_count);
5452 observer_notify_breakpoint_modified (b);
5453 }
18a18393
VP
5454}
5455
1cf4d951
PA
5456/* Returns true if we need to track moribund locations of LOC's type
5457 on the current target. */
5458
5459static int
5460need_moribund_for_location_type (struct bp_location *loc)
5461{
5462 return ((loc->loc_type == bp_loc_software_breakpoint
5463 && !target_supports_stopped_by_sw_breakpoint ())
5464 || (loc->loc_type == bp_loc_hardware_breakpoint
5465 && !target_supports_stopped_by_hw_breakpoint ()));
5466}
5467
18a18393 5468
9709f61c 5469/* Get a bpstat associated with having just stopped at address
d983da9c 5470 BP_ADDR in thread PTID.
c906108c 5471
d983da9c 5472 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5473 don't understand this stop. Result is a chain of bpstat's such
5474 that:
c906108c 5475
c5aa993b 5476 if we don't understand the stop, the result is a null pointer.
c906108c 5477
c5aa993b 5478 if we understand why we stopped, the result is not null.
c906108c 5479
c5aa993b
JM
5480 Each element of the chain refers to a particular breakpoint or
5481 watchpoint at which we have stopped. (We may have stopped for
5482 several reasons concurrently.)
c906108c 5483
c5aa993b
JM
5484 Each element of the chain has valid next, breakpoint_at,
5485 commands, FIXME??? fields. */
c906108c
SS
5486
5487bpstat
6c95b8df 5488bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5489 CORE_ADDR bp_addr, ptid_t ptid,
5490 const struct target_waitstatus *ws)
c906108c 5491{
0d381245 5492 struct breakpoint *b = NULL;
afe38095 5493 struct bp_location *bl;
20874c92 5494 struct bp_location *loc;
5760d0ab
JK
5495 /* First item of allocated bpstat's. */
5496 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5497 /* Pointer to the last thing in the chain currently. */
5760d0ab 5498 bpstat bs;
20874c92 5499 int ix;
429374b8 5500 int need_remove_insert;
f431efe5 5501 int removed_any;
c906108c 5502
f431efe5
PA
5503 /* First, build the bpstat chain with locations that explain a
5504 target stop, while being careful to not set the target running,
5505 as that may invalidate locations (in particular watchpoint
5506 locations are recreated). Resuming will happen here with
5507 breakpoint conditions or watchpoint expressions that include
5508 inferior function calls. */
c5aa993b 5509
429374b8
JK
5510 ALL_BREAKPOINTS (b)
5511 {
1a853c52 5512 if (!breakpoint_enabled (b))
429374b8 5513 continue;
a5606eee 5514
429374b8
JK
5515 for (bl = b->loc; bl != NULL; bl = bl->next)
5516 {
4a64f543
MS
5517 /* For hardware watchpoints, we look only at the first
5518 location. The watchpoint_check function will work on the
5519 entire expression, not the individual locations. For
5520 read watchpoints, the watchpoints_triggered function has
5521 checked all locations already. */
429374b8
JK
5522 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5523 break;
18a18393 5524
f6592439 5525 if (!bl->enabled || bl->shlib_disabled)
429374b8 5526 continue;
c5aa993b 5527
09ac7c10 5528 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5529 continue;
c5aa993b 5530
4a64f543
MS
5531 /* Come here if it's a watchpoint, or if the break address
5532 matches. */
c5aa993b 5533
4a64f543
MS
5534 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5535 explain stop. */
c5aa993b 5536
f431efe5
PA
5537 /* Assume we stop. Should we find a watchpoint that is not
5538 actually triggered, or if the condition of the breakpoint
5539 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5540 bs->stop = 1;
5541 bs->print = 1;
d983da9c 5542
f431efe5
PA
5543 /* If this is a scope breakpoint, mark the associated
5544 watchpoint as triggered so that we will handle the
5545 out-of-scope event. We'll get to the watchpoint next
5546 iteration. */
d0fb5eae 5547 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5548 {
5549 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5550
5551 w->watchpoint_triggered = watch_triggered_yes;
5552 }
f431efe5
PA
5553 }
5554 }
5555
7c16b83e 5556 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5557 if (!target_supports_stopped_by_sw_breakpoint ()
5558 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5559 {
1cf4d951 5560 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5561 {
1cf4d951
PA
5562 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5563 && need_moribund_for_location_type (loc))
5564 {
5565 bs = bpstat_alloc (loc, &bs_link);
5566 /* For hits of moribund locations, we should just proceed. */
5567 bs->stop = 0;
5568 bs->print = 0;
5569 bs->print_it = print_it_noop;
5570 }
f431efe5
PA
5571 }
5572 }
5573
edcc5120
TT
5574 /* A bit of special processing for shlib breakpoints. We need to
5575 process solib loading here, so that the lists of loaded and
5576 unloaded libraries are correct before we handle "catch load" and
5577 "catch unload". */
5578 for (bs = bs_head; bs != NULL; bs = bs->next)
5579 {
5d268276 5580 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5581 {
5582 handle_solib_event ();
5583 break;
5584 }
5585 }
5586
f431efe5
PA
5587 /* Now go through the locations that caused the target to stop, and
5588 check whether we're interested in reporting this stop to higher
5589 layers, or whether we should resume the target transparently. */
5590
5591 removed_any = 0;
5592
5760d0ab 5593 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5594 {
5595 if (!bs->stop)
5596 continue;
5597
f431efe5 5598 b = bs->breakpoint_at;
348d480f
PA
5599 b->ops->check_status (bs);
5600 if (bs->stop)
28010a5d 5601 {
348d480f 5602 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5603
429374b8
JK
5604 if (bs->stop)
5605 {
5606 ++(b->hit_count);
8d3788bd 5607 observer_notify_breakpoint_modified (b);
c906108c 5608
4a64f543 5609 /* We will stop here. */
429374b8
JK
5610 if (b->disposition == disp_disable)
5611 {
816338b5 5612 --(b->enable_count);
1a853c52 5613 if (b->enable_count <= 0)
429374b8 5614 b->enable_state = bp_disabled;
f431efe5 5615 removed_any = 1;
429374b8
JK
5616 }
5617 if (b->silent)
5618 bs->print = 0;
5619 bs->commands = b->commands;
9add0f1b 5620 incref_counted_command_line (bs->commands);
abf85f46
JK
5621 if (command_line_is_silent (bs->commands
5622 ? bs->commands->commands : NULL))
5623 bs->print = 0;
9d6e6e84
HZ
5624
5625 b->ops->after_condition_true (bs);
429374b8
JK
5626 }
5627
348d480f 5628 }
a9b3a50f
PA
5629
5630 /* Print nothing for this entry if we don't stop or don't
5631 print. */
5632 if (!bs->stop || !bs->print)
5633 bs->print_it = print_it_noop;
429374b8 5634 }
876fa593 5635
d983da9c
DJ
5636 /* If we aren't stopping, the value of some hardware watchpoint may
5637 not have changed, but the intermediate memory locations we are
5638 watching may have. Don't bother if we're stopping; this will get
5639 done later. */
d832cb68 5640 need_remove_insert = 0;
5760d0ab
JK
5641 if (! bpstat_causes_stop (bs_head))
5642 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5643 if (!bs->stop
f431efe5
PA
5644 && bs->breakpoint_at
5645 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5646 {
3a5c3e22
PA
5647 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5648
5649 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5650 need_remove_insert = 1;
d983da9c
DJ
5651 }
5652
d832cb68 5653 if (need_remove_insert)
44702360 5654 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5655 else if (removed_any)
44702360 5656 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5657
5760d0ab 5658 return bs_head;
c906108c 5659}
628fe4e4
JK
5660
5661static void
5662handle_jit_event (void)
5663{
5664 struct frame_info *frame;
5665 struct gdbarch *gdbarch;
5666
5667 /* Switch terminal for any messages produced by
5668 breakpoint_re_set. */
5669 target_terminal_ours_for_output ();
5670
5671 frame = get_current_frame ();
5672 gdbarch = get_frame_arch (frame);
5673
5674 jit_event_handler (gdbarch);
5675
5676 target_terminal_inferior ();
5677}
5678
5679/* Prepare WHAT final decision for infrun. */
5680
5681/* Decide what infrun needs to do with this bpstat. */
5682
c906108c 5683struct bpstat_what
0e30163f 5684bpstat_what (bpstat bs_head)
c906108c 5685{
c906108c 5686 struct bpstat_what retval;
628fe4e4 5687 int jit_event = 0;
0e30163f 5688 bpstat bs;
c906108c 5689
628fe4e4 5690 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5691 retval.call_dummy = STOP_NONE;
186c406b 5692 retval.is_longjmp = 0;
628fe4e4 5693
0e30163f 5694 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5695 {
628fe4e4
JK
5696 /* Extract this BS's action. After processing each BS, we check
5697 if its action overrides all we've seem so far. */
5698 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5699 enum bptype bptype;
5700
c906108c 5701 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5702 {
5703 /* I suspect this can happen if it was a momentary
5704 breakpoint which has since been deleted. */
5705 bptype = bp_none;
5706 }
20874c92 5707 else
f431efe5 5708 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5709
5710 switch (bptype)
c906108c
SS
5711 {
5712 case bp_none:
628fe4e4 5713 break;
c906108c
SS
5714 case bp_breakpoint:
5715 case bp_hardware_breakpoint:
7c16b83e 5716 case bp_single_step:
c906108c
SS
5717 case bp_until:
5718 case bp_finish:
a9b3a50f 5719 case bp_shlib_event:
c906108c
SS
5720 if (bs->stop)
5721 {
5722 if (bs->print)
628fe4e4 5723 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5724 else
628fe4e4 5725 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5726 }
5727 else
628fe4e4 5728 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5729 break;
5730 case bp_watchpoint:
5731 case bp_hardware_watchpoint:
5732 case bp_read_watchpoint:
5733 case bp_access_watchpoint:
5734 if (bs->stop)
5735 {
5736 if (bs->print)
628fe4e4 5737 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5738 else
628fe4e4 5739 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5740 }
5741 else
628fe4e4
JK
5742 {
5743 /* There was a watchpoint, but we're not stopping.
5744 This requires no further action. */
5745 }
c906108c
SS
5746 break;
5747 case bp_longjmp:
e2e4d78b 5748 case bp_longjmp_call_dummy:
186c406b 5749 case bp_exception:
628fe4e4 5750 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5751 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5752 break;
5753 case bp_longjmp_resume:
186c406b 5754 case bp_exception_resume:
628fe4e4 5755 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5756 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5757 break;
5758 case bp_step_resume:
5759 if (bs->stop)
628fe4e4
JK
5760 this_action = BPSTAT_WHAT_STEP_RESUME;
5761 else
c906108c 5762 {
628fe4e4
JK
5763 /* It is for the wrong frame. */
5764 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5765 }
c906108c 5766 break;
2c03e5be
PA
5767 case bp_hp_step_resume:
5768 if (bs->stop)
5769 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5770 else
5771 {
5772 /* It is for the wrong frame. */
5773 this_action = BPSTAT_WHAT_SINGLE;
5774 }
5775 break;
c906108c 5776 case bp_watchpoint_scope:
c4093a6a 5777 case bp_thread_event:
1900040c 5778 case bp_overlay_event:
0fd8e87f 5779 case bp_longjmp_master:
aa7d318d 5780 case bp_std_terminate_master:
186c406b 5781 case bp_exception_master:
628fe4e4 5782 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5783 break;
ce78b96d 5784 case bp_catchpoint:
c5aa993b
JM
5785 if (bs->stop)
5786 {
5787 if (bs->print)
628fe4e4 5788 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5789 else
628fe4e4 5790 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5791 }
5792 else
628fe4e4
JK
5793 {
5794 /* There was a catchpoint, but we're not stopping.
5795 This requires no further action. */
5796 }
5797 break;
628fe4e4
JK
5798 case bp_jit_event:
5799 jit_event = 1;
5800 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5801 break;
c906108c 5802 case bp_call_dummy:
53a5351d
JM
5803 /* Make sure the action is stop (silent or noisy),
5804 so infrun.c pops the dummy frame. */
aa7d318d 5805 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5806 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5807 break;
5808 case bp_std_terminate:
5809 /* Make sure the action is stop (silent or noisy),
5810 so infrun.c pops the dummy frame. */
aa7d318d 5811 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5812 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5813 break;
1042e4c0 5814 case bp_tracepoint:
7a697b8d 5815 case bp_fast_tracepoint:
0fb4aa4b 5816 case bp_static_tracepoint:
1042e4c0
SS
5817 /* Tracepoint hits should not be reported back to GDB, and
5818 if one got through somehow, it should have been filtered
5819 out already. */
5820 internal_error (__FILE__, __LINE__,
7a697b8d 5821 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5822 break;
5823 case bp_gnu_ifunc_resolver:
5824 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5825 this_action = BPSTAT_WHAT_SINGLE;
5826 break;
5827 case bp_gnu_ifunc_resolver_return:
5828 /* The breakpoint will be removed, execution will restart from the
5829 PC of the former breakpoint. */
5830 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5831 break;
e7e0cddf
SS
5832
5833 case bp_dprintf:
a11cfd87
HZ
5834 if (bs->stop)
5835 this_action = BPSTAT_WHAT_STOP_SILENT;
5836 else
5837 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5838 break;
5839
628fe4e4
JK
5840 default:
5841 internal_error (__FILE__, __LINE__,
5842 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5843 }
628fe4e4
JK
5844
5845 retval.main_action = max (retval.main_action, this_action);
c906108c 5846 }
628fe4e4 5847
0e30163f
JK
5848 /* These operations may affect the bs->breakpoint_at state so they are
5849 delayed after MAIN_ACTION is decided above. */
5850
628fe4e4
JK
5851 if (jit_event)
5852 {
5853 if (debug_infrun)
5854 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5855
5856 handle_jit_event ();
5857 }
5858
0e30163f
JK
5859 for (bs = bs_head; bs != NULL; bs = bs->next)
5860 {
5861 struct breakpoint *b = bs->breakpoint_at;
5862
5863 if (b == NULL)
5864 continue;
5865 switch (b->type)
5866 {
5867 case bp_gnu_ifunc_resolver:
5868 gnu_ifunc_resolver_stop (b);
5869 break;
5870 case bp_gnu_ifunc_resolver_return:
5871 gnu_ifunc_resolver_return_stop (b);
5872 break;
5873 }
5874 }
5875
c906108c
SS
5876 return retval;
5877}
5878
5879/* Nonzero if we should step constantly (e.g. watchpoints on machines
5880 without hardware support). This isn't related to a specific bpstat,
5881 just to things like whether watchpoints are set. */
5882
c5aa993b 5883int
fba45db2 5884bpstat_should_step (void)
c906108c
SS
5885{
5886 struct breakpoint *b;
cc59ec59 5887
c906108c 5888 ALL_BREAKPOINTS (b)
717a8278 5889 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5890 return 1;
c906108c
SS
5891 return 0;
5892}
5893
67822962
PA
5894int
5895bpstat_causes_stop (bpstat bs)
5896{
5897 for (; bs != NULL; bs = bs->next)
5898 if (bs->stop)
5899 return 1;
5900
5901 return 0;
5902}
5903
c906108c 5904\f
c5aa993b 5905
170b53b2
UW
5906/* Compute a string of spaces suitable to indent the next line
5907 so it starts at the position corresponding to the table column
5908 named COL_NAME in the currently active table of UIOUT. */
5909
5910static char *
5911wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5912{
5913 static char wrap_indent[80];
5914 int i, total_width, width, align;
5915 char *text;
5916
5917 total_width = 0;
5918 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5919 {
5920 if (strcmp (text, col_name) == 0)
5921 {
5922 gdb_assert (total_width < sizeof wrap_indent);
5923 memset (wrap_indent, ' ', total_width);
5924 wrap_indent[total_width] = 0;
5925
5926 return wrap_indent;
5927 }
5928
5929 total_width += width + 1;
5930 }
5931
5932 return NULL;
5933}
5934
b775012e
LM
5935/* Determine if the locations of this breakpoint will have their conditions
5936 evaluated by the target, host or a mix of both. Returns the following:
5937
5938 "host": Host evals condition.
5939 "host or target": Host or Target evals condition.
5940 "target": Target evals condition.
5941*/
5942
5943static const char *
5944bp_condition_evaluator (struct breakpoint *b)
5945{
5946 struct bp_location *bl;
5947 char host_evals = 0;
5948 char target_evals = 0;
5949
5950 if (!b)
5951 return NULL;
5952
5953 if (!is_breakpoint (b))
5954 return NULL;
5955
5956 if (gdb_evaluates_breakpoint_condition_p ()
5957 || !target_supports_evaluation_of_breakpoint_conditions ())
5958 return condition_evaluation_host;
5959
5960 for (bl = b->loc; bl; bl = bl->next)
5961 {
5962 if (bl->cond_bytecode)
5963 target_evals++;
5964 else
5965 host_evals++;
5966 }
5967
5968 if (host_evals && target_evals)
5969 return condition_evaluation_both;
5970 else if (target_evals)
5971 return condition_evaluation_target;
5972 else
5973 return condition_evaluation_host;
5974}
5975
5976/* Determine the breakpoint location's condition evaluator. This is
5977 similar to bp_condition_evaluator, but for locations. */
5978
5979static const char *
5980bp_location_condition_evaluator (struct bp_location *bl)
5981{
5982 if (bl && !is_breakpoint (bl->owner))
5983 return NULL;
5984
5985 if (gdb_evaluates_breakpoint_condition_p ()
5986 || !target_supports_evaluation_of_breakpoint_conditions ())
5987 return condition_evaluation_host;
5988
5989 if (bl && bl->cond_bytecode)
5990 return condition_evaluation_target;
5991 else
5992 return condition_evaluation_host;
5993}
5994
859825b8
JK
5995/* Print the LOC location out of the list of B->LOC locations. */
5996
170b53b2
UW
5997static void
5998print_breakpoint_location (struct breakpoint *b,
5999 struct bp_location *loc)
0d381245 6000{
79a45e25 6001 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6002 struct cleanup *old_chain = save_current_program_space ();
6003
859825b8
JK
6004 if (loc != NULL && loc->shlib_disabled)
6005 loc = NULL;
6006
6c95b8df
PA
6007 if (loc != NULL)
6008 set_current_program_space (loc->pspace);
6009
56435ebe
TT
6010 if (b->display_canonical)
6011 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 6012 else if (loc && loc->symtab)
0d381245
VP
6013 {
6014 struct symbol *sym
6015 = find_pc_sect_function (loc->address, loc->section);
6016 if (sym)
6017 {
6018 ui_out_text (uiout, "in ");
6019 ui_out_field_string (uiout, "func",
6020 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6021 ui_out_text (uiout, " ");
6022 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6023 ui_out_text (uiout, "at ");
0d381245 6024 }
05cba821
JK
6025 ui_out_field_string (uiout, "file",
6026 symtab_to_filename_for_display (loc->symtab));
0d381245 6027 ui_out_text (uiout, ":");
05cba821 6028
0d381245 6029 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6030 ui_out_field_string (uiout, "fullname",
6031 symtab_to_fullname (loc->symtab));
0d381245 6032
f8eba3c6 6033 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6034 }
859825b8 6035 else if (loc)
0d381245 6036 {
f99d8bf4
PA
6037 struct ui_file *stb = mem_fileopen ();
6038 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6039
f99d8bf4 6040 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6041 demangle, "");
0d381245 6042 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6043
6044 do_cleanups (stb_chain);
0d381245 6045 }
859825b8
JK
6046 else
6047 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 6048
b775012e
LM
6049 if (loc && is_breakpoint (b)
6050 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6051 && bp_condition_evaluator (b) == condition_evaluation_both)
6052 {
6053 ui_out_text (uiout, " (");
6054 ui_out_field_string (uiout, "evaluated-by",
6055 bp_location_condition_evaluator (loc));
6056 ui_out_text (uiout, ")");
6057 }
6058
6c95b8df 6059 do_cleanups (old_chain);
0d381245
VP
6060}
6061
269b11a2
PA
6062static const char *
6063bptype_string (enum bptype type)
c906108c 6064{
c4093a6a
JM
6065 struct ep_type_description
6066 {
6067 enum bptype type;
6068 char *description;
6069 };
6070 static struct ep_type_description bptypes[] =
c906108c 6071 {
c5aa993b
JM
6072 {bp_none, "?deleted?"},
6073 {bp_breakpoint, "breakpoint"},
c906108c 6074 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6075 {bp_single_step, "sw single-step"},
c5aa993b
JM
6076 {bp_until, "until"},
6077 {bp_finish, "finish"},
6078 {bp_watchpoint, "watchpoint"},
c906108c 6079 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6080 {bp_read_watchpoint, "read watchpoint"},
6081 {bp_access_watchpoint, "acc watchpoint"},
6082 {bp_longjmp, "longjmp"},
6083 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6084 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6085 {bp_exception, "exception"},
6086 {bp_exception_resume, "exception resume"},
c5aa993b 6087 {bp_step_resume, "step resume"},
2c03e5be 6088 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6089 {bp_watchpoint_scope, "watchpoint scope"},
6090 {bp_call_dummy, "call dummy"},
aa7d318d 6091 {bp_std_terminate, "std::terminate"},
c5aa993b 6092 {bp_shlib_event, "shlib events"},
c4093a6a 6093 {bp_thread_event, "thread events"},
1900040c 6094 {bp_overlay_event, "overlay events"},
0fd8e87f 6095 {bp_longjmp_master, "longjmp master"},
aa7d318d 6096 {bp_std_terminate_master, "std::terminate master"},
186c406b 6097 {bp_exception_master, "exception master"},
ce78b96d 6098 {bp_catchpoint, "catchpoint"},
1042e4c0 6099 {bp_tracepoint, "tracepoint"},
7a697b8d 6100 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6101 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6102 {bp_dprintf, "dprintf"},
4efc6507 6103 {bp_jit_event, "jit events"},
0e30163f
JK
6104 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6105 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6106 };
269b11a2
PA
6107
6108 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6109 || ((int) type != bptypes[(int) type].type))
6110 internal_error (__FILE__, __LINE__,
6111 _("bptypes table does not describe type #%d."),
6112 (int) type);
6113
6114 return bptypes[(int) type].description;
6115}
6116
998580f1
MK
6117/* For MI, output a field named 'thread-groups' with a list as the value.
6118 For CLI, prefix the list with the string 'inf'. */
6119
6120static void
6121output_thread_groups (struct ui_out *uiout,
6122 const char *field_name,
6123 VEC(int) *inf_num,
6124 int mi_only)
6125{
752eb8b4 6126 struct cleanup *back_to;
998580f1
MK
6127 int is_mi = ui_out_is_mi_like_p (uiout);
6128 int inf;
6129 int i;
6130
6131 /* For backward compatibility, don't display inferiors in CLI unless
6132 there are several. Always display them for MI. */
6133 if (!is_mi && mi_only)
6134 return;
6135
752eb8b4
TT
6136 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6137
998580f1
MK
6138 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6139 {
6140 if (is_mi)
6141 {
6142 char mi_group[10];
6143
6144 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6145 ui_out_field_string (uiout, NULL, mi_group);
6146 }
6147 else
6148 {
6149 if (i == 0)
6150 ui_out_text (uiout, " inf ");
6151 else
6152 ui_out_text (uiout, ", ");
6153
6154 ui_out_text (uiout, plongest (inf));
6155 }
6156 }
6157
6158 do_cleanups (back_to);
6159}
6160
269b11a2
PA
6161/* Print B to gdb_stdout. */
6162
6163static void
6164print_one_breakpoint_location (struct breakpoint *b,
6165 struct bp_location *loc,
6166 int loc_number,
6167 struct bp_location **last_loc,
269b11a2
PA
6168 int allflag)
6169{
6170 struct command_line *l;
c2c6d25f 6171 static char bpenables[] = "nynny";
c906108c 6172
79a45e25 6173 struct ui_out *uiout = current_uiout;
0d381245
VP
6174 int header_of_multiple = 0;
6175 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6176 struct value_print_options opts;
6177
6178 get_user_print_options (&opts);
0d381245
VP
6179
6180 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6181 /* See comment in print_one_breakpoint concerning treatment of
6182 breakpoints with single disabled location. */
0d381245
VP
6183 if (loc == NULL
6184 && (b->loc != NULL
6185 && (b->loc->next != NULL || !b->loc->enabled)))
6186 header_of_multiple = 1;
6187 if (loc == NULL)
6188 loc = b->loc;
6189
c4093a6a
JM
6190 annotate_record ();
6191
6192 /* 1 */
6193 annotate_field (0);
0d381245
VP
6194 if (part_of_multiple)
6195 {
6196 char *formatted;
0c6773c1 6197 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6198 ui_out_field_string (uiout, "number", formatted);
6199 xfree (formatted);
6200 }
6201 else
6202 {
6203 ui_out_field_int (uiout, "number", b->number);
6204 }
c4093a6a
JM
6205
6206 /* 2 */
6207 annotate_field (1);
0d381245
VP
6208 if (part_of_multiple)
6209 ui_out_field_skip (uiout, "type");
269b11a2
PA
6210 else
6211 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6212
6213 /* 3 */
6214 annotate_field (2);
0d381245
VP
6215 if (part_of_multiple)
6216 ui_out_field_skip (uiout, "disp");
6217 else
2cec12e5 6218 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6219
c4093a6a
JM
6220
6221 /* 4 */
6222 annotate_field (3);
0d381245 6223 if (part_of_multiple)
54e52265 6224 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6225 else
4a64f543
MS
6226 ui_out_field_fmt (uiout, "enabled", "%c",
6227 bpenables[(int) b->enable_state]);
54e52265 6228 ui_out_spaces (uiout, 2);
0d381245 6229
c4093a6a
JM
6230
6231 /* 5 and 6 */
3086aeae 6232 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6233 {
4a64f543
MS
6234 /* Although the print_one can possibly print all locations,
6235 calling it here is not likely to get any nice result. So,
6236 make sure there's just one location. */
0d381245 6237 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6238 b->ops->print_one (b, last_loc);
0d381245 6239 }
3086aeae
DJ
6240 else
6241 switch (b->type)
6242 {
6243 case bp_none:
6244 internal_error (__FILE__, __LINE__,
e2e0b3e5 6245 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6246 break;
c906108c 6247
3086aeae
DJ
6248 case bp_watchpoint:
6249 case bp_hardware_watchpoint:
6250 case bp_read_watchpoint:
6251 case bp_access_watchpoint:
3a5c3e22
PA
6252 {
6253 struct watchpoint *w = (struct watchpoint *) b;
6254
6255 /* Field 4, the address, is omitted (which makes the columns
6256 not line up too nicely with the headers, but the effect
6257 is relatively readable). */
6258 if (opts.addressprint)
6259 ui_out_field_skip (uiout, "addr");
6260 annotate_field (5);
6261 ui_out_field_string (uiout, "what", w->exp_string);
6262 }
3086aeae
DJ
6263 break;
6264
3086aeae
DJ
6265 case bp_breakpoint:
6266 case bp_hardware_breakpoint:
7c16b83e 6267 case bp_single_step:
3086aeae
DJ
6268 case bp_until:
6269 case bp_finish:
6270 case bp_longjmp:
6271 case bp_longjmp_resume:
e2e4d78b 6272 case bp_longjmp_call_dummy:
186c406b
TT
6273 case bp_exception:
6274 case bp_exception_resume:
3086aeae 6275 case bp_step_resume:
2c03e5be 6276 case bp_hp_step_resume:
3086aeae
DJ
6277 case bp_watchpoint_scope:
6278 case bp_call_dummy:
aa7d318d 6279 case bp_std_terminate:
3086aeae
DJ
6280 case bp_shlib_event:
6281 case bp_thread_event:
6282 case bp_overlay_event:
0fd8e87f 6283 case bp_longjmp_master:
aa7d318d 6284 case bp_std_terminate_master:
186c406b 6285 case bp_exception_master:
1042e4c0 6286 case bp_tracepoint:
7a697b8d 6287 case bp_fast_tracepoint:
0fb4aa4b 6288 case bp_static_tracepoint:
e7e0cddf 6289 case bp_dprintf:
4efc6507 6290 case bp_jit_event:
0e30163f
JK
6291 case bp_gnu_ifunc_resolver:
6292 case bp_gnu_ifunc_resolver_return:
79a45b7d 6293 if (opts.addressprint)
3086aeae
DJ
6294 {
6295 annotate_field (4);
54e52265 6296 if (header_of_multiple)
0d381245 6297 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6298 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6299 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6300 else
5af949e3
UW
6301 ui_out_field_core_addr (uiout, "addr",
6302 loc->gdbarch, loc->address);
3086aeae
DJ
6303 }
6304 annotate_field (5);
0d381245 6305 if (!header_of_multiple)
170b53b2 6306 print_breakpoint_location (b, loc);
0d381245 6307 if (b->loc)
a6d9a66e 6308 *last_loc = b->loc;
3086aeae
DJ
6309 break;
6310 }
c906108c 6311
6c95b8df 6312
998580f1 6313 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6314 {
6315 struct inferior *inf;
998580f1
MK
6316 VEC(int) *inf_num = NULL;
6317 int mi_only = 1;
6c95b8df 6318
998580f1 6319 ALL_INFERIORS (inf)
6c95b8df
PA
6320 {
6321 if (inf->pspace == loc->pspace)
998580f1 6322 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6323 }
998580f1
MK
6324
6325 /* For backward compatibility, don't display inferiors in CLI unless
6326 there are several. Always display for MI. */
6327 if (allflag
6328 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6329 && (number_of_program_spaces () > 1
6330 || number_of_inferiors () > 1)
6331 /* LOC is for existing B, it cannot be in
6332 moribund_locations and thus having NULL OWNER. */
6333 && loc->owner->type != bp_catchpoint))
6334 mi_only = 0;
6335 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6336 VEC_free (int, inf_num);
6c95b8df
PA
6337 }
6338
4a306c9a 6339 if (!part_of_multiple)
c4093a6a 6340 {
4a306c9a
JB
6341 if (b->thread != -1)
6342 {
6343 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6344 "stop only in" line a little further down. */
4a306c9a
JB
6345 ui_out_text (uiout, " thread ");
6346 ui_out_field_int (uiout, "thread", b->thread);
6347 }
6348 else if (b->task != 0)
6349 {
6350 ui_out_text (uiout, " task ");
6351 ui_out_field_int (uiout, "task", b->task);
6352 }
c4093a6a 6353 }
f1310107 6354
8b93c638 6355 ui_out_text (uiout, "\n");
f1310107 6356
348d480f 6357 if (!part_of_multiple)
f1310107
TJB
6358 b->ops->print_one_detail (b, uiout);
6359
0d381245 6360 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6361 {
6362 annotate_field (6);
8b93c638 6363 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6364 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6365 the frame ID. */
5af949e3
UW
6366 ui_out_field_core_addr (uiout, "frame",
6367 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6368 ui_out_text (uiout, "\n");
c4093a6a
JM
6369 }
6370
28010a5d 6371 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6372 {
6373 annotate_field (7);
d77f58be 6374 if (is_tracepoint (b))
1042e4c0
SS
6375 ui_out_text (uiout, "\ttrace only if ");
6376 else
6377 ui_out_text (uiout, "\tstop only if ");
0101ce28 6378 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6379
6380 /* Print whether the target is doing the breakpoint's condition
6381 evaluation. If GDB is doing the evaluation, don't print anything. */
6382 if (is_breakpoint (b)
6383 && breakpoint_condition_evaluation_mode ()
6384 == condition_evaluation_target)
6385 {
6386 ui_out_text (uiout, " (");
6387 ui_out_field_string (uiout, "evaluated-by",
6388 bp_condition_evaluator (b));
6389 ui_out_text (uiout, " evals)");
6390 }
0101ce28
JJ
6391 ui_out_text (uiout, "\n");
6392 }
6393
0d381245 6394 if (!part_of_multiple && b->thread != -1)
c4093a6a 6395 {
4a64f543 6396 /* FIXME should make an annotation for this. */
8b93c638
JM
6397 ui_out_text (uiout, "\tstop only in thread ");
6398 ui_out_field_int (uiout, "thread", b->thread);
6399 ui_out_text (uiout, "\n");
c4093a6a
JM
6400 }
6401
556ec64d
YQ
6402 if (!part_of_multiple)
6403 {
6404 if (b->hit_count)
31f56a27
YQ
6405 {
6406 /* FIXME should make an annotation for this. */
6407 if (is_catchpoint (b))
6408 ui_out_text (uiout, "\tcatchpoint");
6409 else if (is_tracepoint (b))
6410 ui_out_text (uiout, "\ttracepoint");
6411 else
6412 ui_out_text (uiout, "\tbreakpoint");
6413 ui_out_text (uiout, " already hit ");
6414 ui_out_field_int (uiout, "times", b->hit_count);
6415 if (b->hit_count == 1)
6416 ui_out_text (uiout, " time\n");
6417 else
6418 ui_out_text (uiout, " times\n");
6419 }
556ec64d
YQ
6420 else
6421 {
31f56a27
YQ
6422 /* Output the count also if it is zero, but only if this is mi. */
6423 if (ui_out_is_mi_like_p (uiout))
6424 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6425 }
6426 }
8b93c638 6427
0d381245 6428 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6429 {
6430 annotate_field (8);
8b93c638
JM
6431 ui_out_text (uiout, "\tignore next ");
6432 ui_out_field_int (uiout, "ignore", b->ignore_count);
6433 ui_out_text (uiout, " hits\n");
c4093a6a 6434 }
059fb39f 6435
816338b5
SS
6436 /* Note that an enable count of 1 corresponds to "enable once"
6437 behavior, which is reported by the combination of enablement and
6438 disposition, so we don't need to mention it here. */
6439 if (!part_of_multiple && b->enable_count > 1)
6440 {
6441 annotate_field (8);
6442 ui_out_text (uiout, "\tdisable after ");
6443 /* Tweak the wording to clarify that ignore and enable counts
6444 are distinct, and have additive effect. */
6445 if (b->ignore_count)
6446 ui_out_text (uiout, "additional ");
6447 else
6448 ui_out_text (uiout, "next ");
6449 ui_out_field_int (uiout, "enable", b->enable_count);
6450 ui_out_text (uiout, " hits\n");
6451 }
6452
f196051f
SS
6453 if (!part_of_multiple && is_tracepoint (b))
6454 {
6455 struct tracepoint *tp = (struct tracepoint *) b;
6456
6457 if (tp->traceframe_usage)
6458 {
6459 ui_out_text (uiout, "\ttrace buffer usage ");
6460 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6461 ui_out_text (uiout, " bytes\n");
6462 }
6463 }
d3ce09f5 6464
9add0f1b 6465 l = b->commands ? b->commands->commands : NULL;
059fb39f 6466 if (!part_of_multiple && l)
c4093a6a 6467 {
3b31d625
EZ
6468 struct cleanup *script_chain;
6469
c4093a6a 6470 annotate_field (9);
3b31d625 6471 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6472 print_command_lines (uiout, l, 4);
3b31d625 6473 do_cleanups (script_chain);
c4093a6a 6474 }
d24317b4 6475
d9b3f62e 6476 if (is_tracepoint (b))
1042e4c0 6477 {
d9b3f62e
PA
6478 struct tracepoint *t = (struct tracepoint *) b;
6479
6480 if (!part_of_multiple && t->pass_count)
6481 {
6482 annotate_field (10);
6483 ui_out_text (uiout, "\tpass count ");
6484 ui_out_field_int (uiout, "pass", t->pass_count);
6485 ui_out_text (uiout, " \n");
6486 }
f2a8bc8a
YQ
6487
6488 /* Don't display it when tracepoint or tracepoint location is
6489 pending. */
6490 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6491 {
6492 annotate_field (11);
6493
6494 if (ui_out_is_mi_like_p (uiout))
6495 ui_out_field_string (uiout, "installed",
6496 loc->inserted ? "y" : "n");
6497 else
6498 {
6499 if (loc->inserted)
6500 ui_out_text (uiout, "\t");
6501 else
6502 ui_out_text (uiout, "\tnot ");
6503 ui_out_text (uiout, "installed on target\n");
6504 }
6505 }
1042e4c0
SS
6506 }
6507
d24317b4
VP
6508 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6509 {
3a5c3e22
PA
6510 if (is_watchpoint (b))
6511 {
6512 struct watchpoint *w = (struct watchpoint *) b;
6513
6514 ui_out_field_string (uiout, "original-location", w->exp_string);
6515 }
6516 else if (b->addr_string)
d24317b4 6517 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6518 }
c4093a6a 6519}
c5aa993b 6520
0d381245
VP
6521static void
6522print_one_breakpoint (struct breakpoint *b,
4a64f543 6523 struct bp_location **last_loc,
6c95b8df 6524 int allflag)
0d381245 6525{
8d3788bd 6526 struct cleanup *bkpt_chain;
79a45e25 6527 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6528
6529 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6530
12c5a436 6531 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6532 do_cleanups (bkpt_chain);
0d381245
VP
6533
6534 /* If this breakpoint has custom print function,
6535 it's already printed. Otherwise, print individual
6536 locations, if any. */
6537 if (b->ops == NULL || b->ops->print_one == NULL)
6538 {
4a64f543
MS
6539 /* If breakpoint has a single location that is disabled, we
6540 print it as if it had several locations, since otherwise it's
6541 hard to represent "breakpoint enabled, location disabled"
6542 situation.
6543
6544 Note that while hardware watchpoints have several locations
a3be7890 6545 internally, that's not a property exposed to user. */
0d381245 6546 if (b->loc
a5606eee 6547 && !is_hardware_watchpoint (b)
8d3788bd 6548 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6549 {
6550 struct bp_location *loc;
6551 int n = 1;
8d3788bd 6552
0d381245 6553 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6554 {
6555 struct cleanup *inner2 =
6556 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6557 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6558 do_cleanups (inner2);
6559 }
0d381245
VP
6560 }
6561 }
6562}
6563
a6d9a66e
UW
6564static int
6565breakpoint_address_bits (struct breakpoint *b)
6566{
6567 int print_address_bits = 0;
6568 struct bp_location *loc;
6569
6570 for (loc = b->loc; loc; loc = loc->next)
6571 {
c7437ca6
PA
6572 int addr_bit;
6573
6574 /* Software watchpoints that aren't watching memory don't have
6575 an address to print. */
6576 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6577 continue;
6578
6579 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6580 if (addr_bit > print_address_bits)
6581 print_address_bits = addr_bit;
6582 }
6583
6584 return print_address_bits;
6585}
0d381245 6586
c4093a6a
JM
6587struct captured_breakpoint_query_args
6588 {
6589 int bnum;
6590 };
c5aa993b 6591
c4093a6a 6592static int
2b65245e 6593do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6594{
6595 struct captured_breakpoint_query_args *args = data;
52f0bd74 6596 struct breakpoint *b;
a6d9a66e 6597 struct bp_location *dummy_loc = NULL;
cc59ec59 6598
c4093a6a
JM
6599 ALL_BREAKPOINTS (b)
6600 {
6601 if (args->bnum == b->number)
c5aa993b 6602 {
12c5a436 6603 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6604 return GDB_RC_OK;
c5aa993b 6605 }
c4093a6a
JM
6606 }
6607 return GDB_RC_NONE;
6608}
c5aa993b 6609
c4093a6a 6610enum gdb_rc
4a64f543
MS
6611gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6612 char **error_message)
c4093a6a
JM
6613{
6614 struct captured_breakpoint_query_args args;
cc59ec59 6615
c4093a6a
JM
6616 args.bnum = bnum;
6617 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6618 an error. */
b0b13bb4
DJ
6619 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6620 error_message, RETURN_MASK_ALL) < 0)
6621 return GDB_RC_FAIL;
6622 else
6623 return GDB_RC_OK;
c4093a6a 6624}
c5aa993b 6625
09d682a4
TT
6626/* Return true if this breakpoint was set by the user, false if it is
6627 internal or momentary. */
6628
6629int
6630user_breakpoint_p (struct breakpoint *b)
6631{
46c6471b 6632 return b->number > 0;
09d682a4
TT
6633}
6634
7f3b0473 6635/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6636 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6637 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6638 FILTER is non-NULL, call it on each breakpoint and only include the
6639 ones for which it returns non-zero. Return the total number of
6640 breakpoints listed. */
c906108c 6641
d77f58be 6642static int
e5a67952 6643breakpoint_1 (char *args, int allflag,
4a64f543 6644 int (*filter) (const struct breakpoint *))
c4093a6a 6645{
52f0bd74 6646 struct breakpoint *b;
a6d9a66e 6647 struct bp_location *last_loc = NULL;
7f3b0473 6648 int nr_printable_breakpoints;
3b31d625 6649 struct cleanup *bkpttbl_chain;
79a45b7d 6650 struct value_print_options opts;
a6d9a66e 6651 int print_address_bits = 0;
269b11a2 6652 int print_type_col_width = 14;
79a45e25 6653 struct ui_out *uiout = current_uiout;
269b11a2 6654
79a45b7d
TT
6655 get_user_print_options (&opts);
6656
4a64f543
MS
6657 /* Compute the number of rows in the table, as well as the size
6658 required for address fields. */
7f3b0473
AC
6659 nr_printable_breakpoints = 0;
6660 ALL_BREAKPOINTS (b)
e5a67952
MS
6661 {
6662 /* If we have a filter, only list the breakpoints it accepts. */
6663 if (filter && !filter (b))
6664 continue;
6665
6666 /* If we have an "args" string, it is a list of breakpoints to
6667 accept. Skip the others. */
6668 if (args != NULL && *args != '\0')
6669 {
6670 if (allflag && parse_and_eval_long (args) != b->number)
6671 continue;
6672 if (!allflag && !number_is_in_list (args, b->number))
6673 continue;
6674 }
269b11a2 6675
e5a67952
MS
6676 if (allflag || user_breakpoint_p (b))
6677 {
6678 int addr_bit, type_len;
a6d9a66e 6679
e5a67952
MS
6680 addr_bit = breakpoint_address_bits (b);
6681 if (addr_bit > print_address_bits)
6682 print_address_bits = addr_bit;
269b11a2 6683
e5a67952
MS
6684 type_len = strlen (bptype_string (b->type));
6685 if (type_len > print_type_col_width)
6686 print_type_col_width = type_len;
6687
6688 nr_printable_breakpoints++;
6689 }
6690 }
7f3b0473 6691
79a45b7d 6692 if (opts.addressprint)
3b31d625 6693 bkpttbl_chain
3e43a32a
MS
6694 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6695 nr_printable_breakpoints,
3b31d625 6696 "BreakpointTable");
8b93c638 6697 else
3b31d625 6698 bkpttbl_chain
3e43a32a
MS
6699 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6700 nr_printable_breakpoints,
3b31d625 6701 "BreakpointTable");
8b93c638 6702
7f3b0473 6703 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6704 annotate_breakpoints_headers ();
6705 if (nr_printable_breakpoints > 0)
6706 annotate_field (0);
4a64f543 6707 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6708 if (nr_printable_breakpoints > 0)
6709 annotate_field (1);
269b11a2 6710 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6711 "type", "Type"); /* 2 */
d7faa9e7
AC
6712 if (nr_printable_breakpoints > 0)
6713 annotate_field (2);
4a64f543 6714 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6715 if (nr_printable_breakpoints > 0)
6716 annotate_field (3);
54e52265 6717 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6718 if (opts.addressprint)
e5a67952
MS
6719 {
6720 if (nr_printable_breakpoints > 0)
6721 annotate_field (4);
6722 if (print_address_bits <= 32)
6723 ui_out_table_header (uiout, 10, ui_left,
6724 "addr", "Address"); /* 5 */
6725 else
6726 ui_out_table_header (uiout, 18, ui_left,
6727 "addr", "Address"); /* 5 */
6728 }
d7faa9e7
AC
6729 if (nr_printable_breakpoints > 0)
6730 annotate_field (5);
6731 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6732 ui_out_table_body (uiout);
6733 if (nr_printable_breakpoints > 0)
6734 annotate_breakpoints_table ();
7f3b0473 6735
c4093a6a 6736 ALL_BREAKPOINTS (b)
e5a67952
MS
6737 {
6738 QUIT;
6739 /* If we have a filter, only list the breakpoints it accepts. */
6740 if (filter && !filter (b))
6741 continue;
6742
6743 /* If we have an "args" string, it is a list of breakpoints to
6744 accept. Skip the others. */
6745
6746 if (args != NULL && *args != '\0')
6747 {
6748 if (allflag) /* maintenance info breakpoint */
6749 {
6750 if (parse_and_eval_long (args) != b->number)
6751 continue;
6752 }
6753 else /* all others */
6754 {
6755 if (!number_is_in_list (args, b->number))
6756 continue;
6757 }
6758 }
6759 /* We only print out user settable breakpoints unless the
6760 allflag is set. */
6761 if (allflag || user_breakpoint_p (b))
12c5a436 6762 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6763 }
6764
3b31d625 6765 do_cleanups (bkpttbl_chain);
698384cd 6766
7f3b0473 6767 if (nr_printable_breakpoints == 0)
c906108c 6768 {
4a64f543
MS
6769 /* If there's a filter, let the caller decide how to report
6770 empty list. */
d77f58be
SS
6771 if (!filter)
6772 {
e5a67952 6773 if (args == NULL || *args == '\0')
d77f58be
SS
6774 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6775 else
4a64f543 6776 ui_out_message (uiout, 0,
e5a67952
MS
6777 "No breakpoint or watchpoint matching '%s'.\n",
6778 args);
d77f58be 6779 }
c906108c
SS
6780 }
6781 else
c4093a6a 6782 {
a6d9a66e
UW
6783 if (last_loc && !server_command)
6784 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6785 }
c906108c 6786
4a64f543 6787 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6788 there have been breakpoints? */
c906108c 6789 annotate_breakpoints_table_end ();
d77f58be
SS
6790
6791 return nr_printable_breakpoints;
c906108c
SS
6792}
6793
ad443146
SS
6794/* Display the value of default-collect in a way that is generally
6795 compatible with the breakpoint list. */
6796
6797static void
6798default_collect_info (void)
6799{
79a45e25
PA
6800 struct ui_out *uiout = current_uiout;
6801
ad443146
SS
6802 /* If it has no value (which is frequently the case), say nothing; a
6803 message like "No default-collect." gets in user's face when it's
6804 not wanted. */
6805 if (!*default_collect)
6806 return;
6807
6808 /* The following phrase lines up nicely with per-tracepoint collect
6809 actions. */
6810 ui_out_text (uiout, "default collect ");
6811 ui_out_field_string (uiout, "default-collect", default_collect);
6812 ui_out_text (uiout, " \n");
6813}
6814
c906108c 6815static void
e5a67952 6816breakpoints_info (char *args, int from_tty)
c906108c 6817{
e5a67952 6818 breakpoint_1 (args, 0, NULL);
ad443146
SS
6819
6820 default_collect_info ();
d77f58be
SS
6821}
6822
6823static void
e5a67952 6824watchpoints_info (char *args, int from_tty)
d77f58be 6825{
e5a67952 6826 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6827 struct ui_out *uiout = current_uiout;
d77f58be
SS
6828
6829 if (num_printed == 0)
6830 {
e5a67952 6831 if (args == NULL || *args == '\0')
d77f58be
SS
6832 ui_out_message (uiout, 0, "No watchpoints.\n");
6833 else
e5a67952 6834 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6835 }
c906108c
SS
6836}
6837
7a292a7a 6838static void
e5a67952 6839maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6840{
e5a67952 6841 breakpoint_1 (args, 1, NULL);
ad443146
SS
6842
6843 default_collect_info ();
c906108c
SS
6844}
6845
0d381245 6846static int
714835d5 6847breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6848 struct program_space *pspace,
714835d5 6849 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6850{
6851 struct bp_location *bl = b->loc;
cc59ec59 6852
0d381245
VP
6853 for (; bl; bl = bl->next)
6854 {
6c95b8df
PA
6855 if (bl->pspace == pspace
6856 && bl->address == pc
0d381245
VP
6857 && (!overlay_debugging || bl->section == section))
6858 return 1;
6859 }
6860 return 0;
6861}
6862
672f9b60 6863/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6864 concerns with logical breakpoints, so we match program spaces, not
6865 address spaces. */
c906108c
SS
6866
6867static void
6c95b8df
PA
6868describe_other_breakpoints (struct gdbarch *gdbarch,
6869 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6870 struct obj_section *section, int thread)
c906108c 6871{
52f0bd74
AC
6872 int others = 0;
6873 struct breakpoint *b;
c906108c
SS
6874
6875 ALL_BREAKPOINTS (b)
672f9b60
KP
6876 others += (user_breakpoint_p (b)
6877 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6878 if (others > 0)
6879 {
a3f17187
AC
6880 if (others == 1)
6881 printf_filtered (_("Note: breakpoint "));
6882 else /* if (others == ???) */
6883 printf_filtered (_("Note: breakpoints "));
c906108c 6884 ALL_BREAKPOINTS (b)
672f9b60 6885 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6886 {
6887 others--;
6888 printf_filtered ("%d", b->number);
6889 if (b->thread == -1 && thread != -1)
6890 printf_filtered (" (all threads)");
6891 else if (b->thread != -1)
6892 printf_filtered (" (thread %d)", b->thread);
6893 printf_filtered ("%s%s ",
059fb39f 6894 ((b->enable_state == bp_disabled
f8eba3c6 6895 || b->enable_state == bp_call_disabled)
0d381245 6896 ? " (disabled)"
0d381245
VP
6897 : ""),
6898 (others > 1) ? ","
6899 : ((others == 1) ? " and" : ""));
6900 }
a3f17187 6901 printf_filtered (_("also set at pc "));
5af949e3 6902 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6903 printf_filtered (".\n");
6904 }
6905}
6906\f
c906108c 6907
e4f237da
KB
6908/* Return true iff it is meaningful to use the address member of
6909 BPT. For some breakpoint types, the address member is irrelevant
6910 and it makes no sense to attempt to compare it to other addresses
6911 (or use it for any other purpose either).
6912
4a64f543
MS
6913 More specifically, each of the following breakpoint types will
6914 always have a zero valued address and we don't want to mark
6915 breakpoints of any of these types to be a duplicate of an actual
6916 breakpoint at address zero:
e4f237da
KB
6917
6918 bp_watchpoint
2d134ed3
PA
6919 bp_catchpoint
6920
6921*/
e4f237da
KB
6922
6923static int
6924breakpoint_address_is_meaningful (struct breakpoint *bpt)
6925{
6926 enum bptype type = bpt->type;
6927
2d134ed3
PA
6928 return (type != bp_watchpoint && type != bp_catchpoint);
6929}
6930
6931/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6932 true if LOC1 and LOC2 represent the same watchpoint location. */
6933
6934static int
4a64f543
MS
6935watchpoint_locations_match (struct bp_location *loc1,
6936 struct bp_location *loc2)
2d134ed3 6937{
3a5c3e22
PA
6938 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6939 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6940
6941 /* Both of them must exist. */
6942 gdb_assert (w1 != NULL);
6943 gdb_assert (w2 != NULL);
2bdf28a0 6944
4a64f543
MS
6945 /* If the target can evaluate the condition expression in hardware,
6946 then we we need to insert both watchpoints even if they are at
6947 the same place. Otherwise the watchpoint will only trigger when
6948 the condition of whichever watchpoint was inserted evaluates to
6949 true, not giving a chance for GDB to check the condition of the
6950 other watchpoint. */
3a5c3e22 6951 if ((w1->cond_exp
4a64f543
MS
6952 && target_can_accel_watchpoint_condition (loc1->address,
6953 loc1->length,
0cf6dd15 6954 loc1->watchpoint_type,
3a5c3e22
PA
6955 w1->cond_exp))
6956 || (w2->cond_exp
4a64f543
MS
6957 && target_can_accel_watchpoint_condition (loc2->address,
6958 loc2->length,
0cf6dd15 6959 loc2->watchpoint_type,
3a5c3e22 6960 w2->cond_exp)))
0cf6dd15
TJB
6961 return 0;
6962
85d721b8
PA
6963 /* Note that this checks the owner's type, not the location's. In
6964 case the target does not support read watchpoints, but does
6965 support access watchpoints, we'll have bp_read_watchpoint
6966 watchpoints with hw_access locations. Those should be considered
6967 duplicates of hw_read locations. The hw_read locations will
6968 become hw_access locations later. */
2d134ed3
PA
6969 return (loc1->owner->type == loc2->owner->type
6970 && loc1->pspace->aspace == loc2->pspace->aspace
6971 && loc1->address == loc2->address
6972 && loc1->length == loc2->length);
e4f237da
KB
6973}
6974
31e77af2 6975/* See breakpoint.h. */
6c95b8df 6976
31e77af2 6977int
6c95b8df
PA
6978breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6979 struct address_space *aspace2, CORE_ADDR addr2)
6980{
f5656ead 6981 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6982 || aspace1 == aspace2)
6983 && addr1 == addr2);
6984}
6985
f1310107
TJB
6986/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6987 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6988 matches ASPACE2. On targets that have global breakpoints, the address
6989 space doesn't really matter. */
6990
6991static int
6992breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6993 int len1, struct address_space *aspace2,
6994 CORE_ADDR addr2)
6995{
f5656ead 6996 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6997 || aspace1 == aspace2)
6998 && addr2 >= addr1 && addr2 < addr1 + len1);
6999}
7000
7001/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7002 a ranged breakpoint. In most targets, a match happens only if ASPACE
7003 matches the breakpoint's address space. On targets that have global
7004 breakpoints, the address space doesn't really matter. */
7005
7006static int
7007breakpoint_location_address_match (struct bp_location *bl,
7008 struct address_space *aspace,
7009 CORE_ADDR addr)
7010{
7011 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7012 aspace, addr)
7013 || (bl->length
7014 && breakpoint_address_match_range (bl->pspace->aspace,
7015 bl->address, bl->length,
7016 aspace, addr)));
7017}
7018
1e4d1764
YQ
7019/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7020 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7021 true, otherwise returns false. */
7022
7023static int
7024tracepoint_locations_match (struct bp_location *loc1,
7025 struct bp_location *loc2)
7026{
7027 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7028 /* Since tracepoint locations are never duplicated with others', tracepoint
7029 locations at the same address of different tracepoints are regarded as
7030 different locations. */
7031 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7032 else
7033 return 0;
7034}
7035
2d134ed3
PA
7036/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7037 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7038 represent the same location. */
7039
7040static int
4a64f543
MS
7041breakpoint_locations_match (struct bp_location *loc1,
7042 struct bp_location *loc2)
2d134ed3 7043{
2bdf28a0
JK
7044 int hw_point1, hw_point2;
7045
7046 /* Both of them must not be in moribund_locations. */
7047 gdb_assert (loc1->owner != NULL);
7048 gdb_assert (loc2->owner != NULL);
7049
7050 hw_point1 = is_hardware_watchpoint (loc1->owner);
7051 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7052
7053 if (hw_point1 != hw_point2)
7054 return 0;
7055 else if (hw_point1)
7056 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7057 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7058 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7059 else
f1310107
TJB
7060 /* We compare bp_location.length in order to cover ranged breakpoints. */
7061 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7062 loc2->pspace->aspace, loc2->address)
7063 && loc1->length == loc2->length);
2d134ed3
PA
7064}
7065
76897487
KB
7066static void
7067breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7068 int bnum, int have_bnum)
7069{
f63fbe86
MS
7070 /* The longest string possibly returned by hex_string_custom
7071 is 50 chars. These must be at least that big for safety. */
7072 char astr1[64];
7073 char astr2[64];
76897487 7074
bb599908
PH
7075 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7076 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7077 if (have_bnum)
8a3fe4f8 7078 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7079 bnum, astr1, astr2);
7080 else
8a3fe4f8 7081 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7082}
7083
4a64f543
MS
7084/* Adjust a breakpoint's address to account for architectural
7085 constraints on breakpoint placement. Return the adjusted address.
7086 Note: Very few targets require this kind of adjustment. For most
7087 targets, this function is simply the identity function. */
76897487
KB
7088
7089static CORE_ADDR
a6d9a66e
UW
7090adjust_breakpoint_address (struct gdbarch *gdbarch,
7091 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7092{
a6d9a66e 7093 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7094 {
7095 /* Very few targets need any kind of breakpoint adjustment. */
7096 return bpaddr;
7097 }
88f7da05
KB
7098 else if (bptype == bp_watchpoint
7099 || bptype == bp_hardware_watchpoint
7100 || bptype == bp_read_watchpoint
7101 || bptype == bp_access_watchpoint
fe798b75 7102 || bptype == bp_catchpoint)
88f7da05
KB
7103 {
7104 /* Watchpoints and the various bp_catch_* eventpoints should not
7105 have their addresses modified. */
7106 return bpaddr;
7107 }
7c16b83e
PA
7108 else if (bptype == bp_single_step)
7109 {
7110 /* Single-step breakpoints should not have their addresses
7111 modified. If there's any architectural constrain that
7112 applies to this address, then it should have already been
7113 taken into account when the breakpoint was created in the
7114 first place. If we didn't do this, stepping through e.g.,
7115 Thumb-2 IT blocks would break. */
7116 return bpaddr;
7117 }
76897487
KB
7118 else
7119 {
7120 CORE_ADDR adjusted_bpaddr;
7121
7122 /* Some targets have architectural constraints on the placement
7123 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7124 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7125
7126 /* An adjusted breakpoint address can significantly alter
7127 a user's expectations. Print a warning if an adjustment
7128 is required. */
7129 if (adjusted_bpaddr != bpaddr)
7130 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7131
7132 return adjusted_bpaddr;
7133 }
7134}
7135
28010a5d
PA
7136void
7137init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7138 struct breakpoint *owner)
7cc221ef 7139{
7cc221ef
DJ
7140 memset (loc, 0, sizeof (*loc));
7141
348d480f
PA
7142 gdb_assert (ops != NULL);
7143
28010a5d
PA
7144 loc->ops = ops;
7145 loc->owner = owner;
511a6cd4 7146 loc->cond = NULL;
b775012e 7147 loc->cond_bytecode = NULL;
0d381245
VP
7148 loc->shlib_disabled = 0;
7149 loc->enabled = 1;
e049a4b5 7150
28010a5d 7151 switch (owner->type)
e049a4b5
DJ
7152 {
7153 case bp_breakpoint:
7c16b83e 7154 case bp_single_step:
e049a4b5
DJ
7155 case bp_until:
7156 case bp_finish:
7157 case bp_longjmp:
7158 case bp_longjmp_resume:
e2e4d78b 7159 case bp_longjmp_call_dummy:
186c406b
TT
7160 case bp_exception:
7161 case bp_exception_resume:
e049a4b5 7162 case bp_step_resume:
2c03e5be 7163 case bp_hp_step_resume:
e049a4b5
DJ
7164 case bp_watchpoint_scope:
7165 case bp_call_dummy:
aa7d318d 7166 case bp_std_terminate:
e049a4b5
DJ
7167 case bp_shlib_event:
7168 case bp_thread_event:
7169 case bp_overlay_event:
4efc6507 7170 case bp_jit_event:
0fd8e87f 7171 case bp_longjmp_master:
aa7d318d 7172 case bp_std_terminate_master:
186c406b 7173 case bp_exception_master:
0e30163f
JK
7174 case bp_gnu_ifunc_resolver:
7175 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7176 case bp_dprintf:
e049a4b5 7177 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7178 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7179 break;
7180 case bp_hardware_breakpoint:
7181 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7182 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7183 break;
7184 case bp_hardware_watchpoint:
7185 case bp_read_watchpoint:
7186 case bp_access_watchpoint:
7187 loc->loc_type = bp_loc_hardware_watchpoint;
7188 break;
7189 case bp_watchpoint:
ce78b96d 7190 case bp_catchpoint:
15c3d785
PA
7191 case bp_tracepoint:
7192 case bp_fast_tracepoint:
0fb4aa4b 7193 case bp_static_tracepoint:
e049a4b5
DJ
7194 loc->loc_type = bp_loc_other;
7195 break;
7196 default:
e2e0b3e5 7197 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7198 }
7199
f431efe5 7200 loc->refc = 1;
28010a5d
PA
7201}
7202
7203/* Allocate a struct bp_location. */
7204
7205static struct bp_location *
7206allocate_bp_location (struct breakpoint *bpt)
7207{
348d480f
PA
7208 return bpt->ops->allocate_location (bpt);
7209}
7cc221ef 7210
f431efe5
PA
7211static void
7212free_bp_location (struct bp_location *loc)
fe3f5fa8 7213{
348d480f 7214 loc->ops->dtor (loc);
fe3f5fa8
VP
7215 xfree (loc);
7216}
7217
f431efe5
PA
7218/* Increment reference count. */
7219
7220static void
7221incref_bp_location (struct bp_location *bl)
7222{
7223 ++bl->refc;
7224}
7225
7226/* Decrement reference count. If the reference count reaches 0,
7227 destroy the bp_location. Sets *BLP to NULL. */
7228
7229static void
7230decref_bp_location (struct bp_location **blp)
7231{
0807b50c
PA
7232 gdb_assert ((*blp)->refc > 0);
7233
f431efe5
PA
7234 if (--(*blp)->refc == 0)
7235 free_bp_location (*blp);
7236 *blp = NULL;
7237}
7238
346774a9 7239/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7240
346774a9
PA
7241static void
7242add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7243{
346774a9 7244 struct breakpoint *b1;
c906108c 7245
346774a9
PA
7246 /* Add this breakpoint to the end of the chain so that a list of
7247 breakpoints will come out in order of increasing numbers. */
7248
7249 b1 = breakpoint_chain;
7250 if (b1 == 0)
7251 breakpoint_chain = b;
7252 else
7253 {
7254 while (b1->next)
7255 b1 = b1->next;
7256 b1->next = b;
7257 }
7258}
7259
7260/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7261
7262static void
7263init_raw_breakpoint_without_location (struct breakpoint *b,
7264 struct gdbarch *gdbarch,
28010a5d 7265 enum bptype bptype,
c0a91b2b 7266 const struct breakpoint_ops *ops)
346774a9 7267{
c906108c 7268 memset (b, 0, sizeof (*b));
2219d63c 7269
348d480f
PA
7270 gdb_assert (ops != NULL);
7271
28010a5d 7272 b->ops = ops;
4d28f7a8 7273 b->type = bptype;
a6d9a66e 7274 b->gdbarch = gdbarch;
c906108c
SS
7275 b->language = current_language->la_language;
7276 b->input_radix = input_radix;
7277 b->thread = -1;
b5de0fa7 7278 b->enable_state = bp_enabled;
c906108c
SS
7279 b->next = 0;
7280 b->silent = 0;
7281 b->ignore_count = 0;
7282 b->commands = NULL;
818dd999 7283 b->frame_id = null_frame_id;
0d381245 7284 b->condition_not_parsed = 0;
84f4c1fe 7285 b->py_bp_object = NULL;
d0fb5eae 7286 b->related_breakpoint = b;
346774a9
PA
7287}
7288
7289/* Helper to set_raw_breakpoint below. Creates a breakpoint
7290 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7291
7292static struct breakpoint *
7293set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7294 enum bptype bptype,
c0a91b2b 7295 const struct breakpoint_ops *ops)
346774a9
PA
7296{
7297 struct breakpoint *b = XNEW (struct breakpoint);
7298
348d480f 7299 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7300 add_to_breakpoint_chain (b);
0d381245
VP
7301 return b;
7302}
7303
0e30163f
JK
7304/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7305 resolutions should be made as the user specified the location explicitly
7306 enough. */
7307
0d381245 7308static void
0e30163f 7309set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7310{
2bdf28a0
JK
7311 gdb_assert (loc->owner != NULL);
7312
0d381245 7313 if (loc->owner->type == bp_breakpoint
1042e4c0 7314 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7315 || is_tracepoint (loc->owner))
0d381245 7316 {
0e30163f 7317 int is_gnu_ifunc;
2c02bd72 7318 const char *function_name;
6a3a010b 7319 CORE_ADDR func_addr;
0e30163f 7320
2c02bd72 7321 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7322 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7323
7324 if (is_gnu_ifunc && !explicit_loc)
7325 {
7326 struct breakpoint *b = loc->owner;
7327
7328 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7329 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7330 &loc->requested_address))
7331 {
7332 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7333 loc->address = adjust_breakpoint_address (loc->gdbarch,
7334 loc->requested_address,
7335 b->type);
7336 }
7337 else if (b->type == bp_breakpoint && b->loc == loc
7338 && loc->next == NULL && b->related_breakpoint == b)
7339 {
7340 /* Create only the whole new breakpoint of this type but do not
7341 mess more complicated breakpoints with multiple locations. */
7342 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7343 /* Remember the resolver's address for use by the return
7344 breakpoint. */
7345 loc->related_address = func_addr;
0e30163f
JK
7346 }
7347 }
7348
2c02bd72
DE
7349 if (function_name)
7350 loc->function_name = xstrdup (function_name);
0d381245
VP
7351 }
7352}
7353
a6d9a66e 7354/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7355struct gdbarch *
a6d9a66e
UW
7356get_sal_arch (struct symtab_and_line sal)
7357{
7358 if (sal.section)
7359 return get_objfile_arch (sal.section->objfile);
7360 if (sal.symtab)
eb822aa6 7361 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7362
7363 return NULL;
7364}
7365
346774a9
PA
7366/* Low level routine for partially initializing a breakpoint of type
7367 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7368 file name, and line number are provided by SAL.
0d381245
VP
7369
7370 It is expected that the caller will complete the initialization of
7371 the newly created breakpoint struct as well as output any status
c56053d2 7372 information regarding the creation of a new breakpoint. */
0d381245 7373
346774a9
PA
7374static void
7375init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7376 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7377 const struct breakpoint_ops *ops)
0d381245 7378{
28010a5d 7379 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7380
3742cc8b 7381 add_location_to_breakpoint (b, &sal);
0d381245 7382
6c95b8df
PA
7383 if (bptype != bp_catchpoint)
7384 gdb_assert (sal.pspace != NULL);
7385
f8eba3c6
TT
7386 /* Store the program space that was used to set the breakpoint,
7387 except for ordinary breakpoints, which are independent of the
7388 program space. */
7389 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7390 b->pspace = sal.pspace;
346774a9 7391}
c906108c 7392
346774a9
PA
7393/* set_raw_breakpoint is a low level routine for allocating and
7394 partially initializing a breakpoint of type BPTYPE. The newly
7395 created breakpoint's address, section, source file name, and line
7396 number are provided by SAL. The newly created and partially
7397 initialized breakpoint is added to the breakpoint chain and
7398 is also returned as the value of this function.
7399
7400 It is expected that the caller will complete the initialization of
7401 the newly created breakpoint struct as well as output any status
7402 information regarding the creation of a new breakpoint. In
7403 particular, set_raw_breakpoint does NOT set the breakpoint
7404 number! Care should be taken to not allow an error to occur
7405 prior to completing the initialization of the breakpoint. If this
7406 should happen, a bogus breakpoint will be left on the chain. */
7407
7408struct breakpoint *
7409set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7410 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7411 const struct breakpoint_ops *ops)
346774a9
PA
7412{
7413 struct breakpoint *b = XNEW (struct breakpoint);
7414
348d480f 7415 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7416 add_to_breakpoint_chain (b);
c906108c
SS
7417 return b;
7418}
7419
c2c6d25f
JM
7420
7421/* Note that the breakpoint object B describes a permanent breakpoint
7422 instruction, hard-wired into the inferior's code. */
7423void
7424make_breakpoint_permanent (struct breakpoint *b)
7425{
0d381245 7426 struct bp_location *bl;
cc59ec59 7427
4a64f543
MS
7428 /* By definition, permanent breakpoints are already present in the
7429 code. Mark all locations as inserted. For now,
7430 make_breakpoint_permanent is called in just one place, so it's
7431 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7432 multiple locations or not, but it's easy to implement. */
0d381245 7433 for (bl = b->loc; bl; bl = bl->next)
1a853c52
PA
7434 {
7435 bl->permanent = 1;
7436 bl->inserted = 1;
7437 }
c2c6d25f
JM
7438}
7439
53a5351d 7440/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7441 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7442 initiated the operation. */
c906108c
SS
7443
7444void
186c406b 7445set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7446{
35df4500 7447 struct breakpoint *b, *b_tmp;
186c406b 7448 int thread = tp->num;
0fd8e87f
UW
7449
7450 /* To avoid having to rescan all objfile symbols at every step,
7451 we maintain a list of continually-inserted but always disabled
7452 longjmp "master" breakpoints. Here, we simply create momentary
7453 clones of those and enable them for the requested thread. */
35df4500 7454 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7455 if (b->pspace == current_program_space
186c406b
TT
7456 && (b->type == bp_longjmp_master
7457 || b->type == bp_exception_master))
0fd8e87f 7458 {
06edf0c0
PA
7459 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7460 struct breakpoint *clone;
cc59ec59 7461
e2e4d78b
JK
7462 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7463 after their removal. */
06edf0c0 7464 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7465 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7466 clone->thread = thread;
7467 }
186c406b
TT
7468
7469 tp->initiating_frame = frame;
c906108c
SS
7470}
7471
611c83ae 7472/* Delete all longjmp breakpoints from THREAD. */
c906108c 7473void
611c83ae 7474delete_longjmp_breakpoint (int thread)
c906108c 7475{
35df4500 7476 struct breakpoint *b, *b_tmp;
c906108c 7477
35df4500 7478 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7479 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7480 {
7481 if (b->thread == thread)
7482 delete_breakpoint (b);
7483 }
c906108c
SS
7484}
7485
f59f708a
PA
7486void
7487delete_longjmp_breakpoint_at_next_stop (int thread)
7488{
7489 struct breakpoint *b, *b_tmp;
7490
7491 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7492 if (b->type == bp_longjmp || b->type == bp_exception)
7493 {
7494 if (b->thread == thread)
7495 b->disposition = disp_del_at_next_stop;
7496 }
7497}
7498
e2e4d78b
JK
7499/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7500 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7501 pointer to any of them. Return NULL if this system cannot place longjmp
7502 breakpoints. */
7503
7504struct breakpoint *
7505set_longjmp_breakpoint_for_call_dummy (void)
7506{
7507 struct breakpoint *b, *retval = NULL;
7508
7509 ALL_BREAKPOINTS (b)
7510 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7511 {
7512 struct breakpoint *new_b;
7513
7514 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7515 &momentary_breakpoint_ops,
7516 1);
e2e4d78b
JK
7517 new_b->thread = pid_to_thread_id (inferior_ptid);
7518
7519 /* Link NEW_B into the chain of RETVAL breakpoints. */
7520
7521 gdb_assert (new_b->related_breakpoint == new_b);
7522 if (retval == NULL)
7523 retval = new_b;
7524 new_b->related_breakpoint = retval;
7525 while (retval->related_breakpoint != new_b->related_breakpoint)
7526 retval = retval->related_breakpoint;
7527 retval->related_breakpoint = new_b;
7528 }
7529
7530 return retval;
7531}
7532
7533/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7534 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7535 stack.
7536
7537 You should call this function only at places where it is safe to currently
7538 unwind the whole stack. Failed stack unwind would discard live dummy
7539 frames. */
7540
7541void
b67a2c6f 7542check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7543{
7544 struct breakpoint *b, *b_tmp;
7545
7546 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7547 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7548 {
7549 struct breakpoint *dummy_b = b->related_breakpoint;
7550
7551 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7552 dummy_b = dummy_b->related_breakpoint;
7553 if (dummy_b->type != bp_call_dummy
7554 || frame_find_by_id (dummy_b->frame_id) != NULL)
7555 continue;
7556
b67a2c6f 7557 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7558
7559 while (b->related_breakpoint != b)
7560 {
7561 if (b_tmp == b->related_breakpoint)
7562 b_tmp = b->related_breakpoint->next;
7563 delete_breakpoint (b->related_breakpoint);
7564 }
7565 delete_breakpoint (b);
7566 }
7567}
7568
1900040c
MS
7569void
7570enable_overlay_breakpoints (void)
7571{
52f0bd74 7572 struct breakpoint *b;
1900040c
MS
7573
7574 ALL_BREAKPOINTS (b)
7575 if (b->type == bp_overlay_event)
7576 {
7577 b->enable_state = bp_enabled;
44702360 7578 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7579 overlay_events_enabled = 1;
1900040c
MS
7580 }
7581}
7582
7583void
7584disable_overlay_breakpoints (void)
7585{
52f0bd74 7586 struct breakpoint *b;
1900040c
MS
7587
7588 ALL_BREAKPOINTS (b)
7589 if (b->type == bp_overlay_event)
7590 {
7591 b->enable_state = bp_disabled;
44702360 7592 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7593 overlay_events_enabled = 0;
1900040c
MS
7594 }
7595}
7596
aa7d318d
TT
7597/* Set an active std::terminate breakpoint for each std::terminate
7598 master breakpoint. */
7599void
7600set_std_terminate_breakpoint (void)
7601{
35df4500 7602 struct breakpoint *b, *b_tmp;
aa7d318d 7603
35df4500 7604 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7605 if (b->pspace == current_program_space
7606 && b->type == bp_std_terminate_master)
7607 {
06edf0c0 7608 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7609 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7610 }
7611}
7612
7613/* Delete all the std::terminate breakpoints. */
7614void
7615delete_std_terminate_breakpoint (void)
7616{
35df4500 7617 struct breakpoint *b, *b_tmp;
aa7d318d 7618
35df4500 7619 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7620 if (b->type == bp_std_terminate)
7621 delete_breakpoint (b);
7622}
7623
c4093a6a 7624struct breakpoint *
a6d9a66e 7625create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7626{
7627 struct breakpoint *b;
c4093a6a 7628
06edf0c0
PA
7629 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7630 &internal_breakpoint_ops);
7631
b5de0fa7 7632 b->enable_state = bp_enabled;
c4093a6a 7633 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7634 b->addr_string
7635 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7636
44702360 7637 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7638
c4093a6a
JM
7639 return b;
7640}
7641
7642void
7643remove_thread_event_breakpoints (void)
7644{
35df4500 7645 struct breakpoint *b, *b_tmp;
c4093a6a 7646
35df4500 7647 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7648 if (b->type == bp_thread_event
7649 && b->loc->pspace == current_program_space)
c4093a6a
JM
7650 delete_breakpoint (b);
7651}
7652
0101ce28
JJ
7653struct lang_and_radix
7654 {
7655 enum language lang;
7656 int radix;
7657 };
7658
4efc6507
DE
7659/* Create a breakpoint for JIT code registration and unregistration. */
7660
7661struct breakpoint *
7662create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7663{
7664 struct breakpoint *b;
7665
06edf0c0
PA
7666 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7667 &internal_breakpoint_ops);
44702360 7668 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7669 return b;
7670}
0101ce28 7671
03673fc7
PP
7672/* Remove JIT code registration and unregistration breakpoint(s). */
7673
7674void
7675remove_jit_event_breakpoints (void)
7676{
7677 struct breakpoint *b, *b_tmp;
7678
7679 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7680 if (b->type == bp_jit_event
7681 && b->loc->pspace == current_program_space)
7682 delete_breakpoint (b);
7683}
7684
cae688ec
JJ
7685void
7686remove_solib_event_breakpoints (void)
7687{
35df4500 7688 struct breakpoint *b, *b_tmp;
cae688ec 7689
35df4500 7690 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7691 if (b->type == bp_shlib_event
7692 && b->loc->pspace == current_program_space)
cae688ec
JJ
7693 delete_breakpoint (b);
7694}
7695
f37f681c
PA
7696/* See breakpoint.h. */
7697
7698void
7699remove_solib_event_breakpoints_at_next_stop (void)
7700{
7701 struct breakpoint *b, *b_tmp;
7702
7703 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7704 if (b->type == bp_shlib_event
7705 && b->loc->pspace == current_program_space)
7706 b->disposition = disp_del_at_next_stop;
7707}
7708
04086b45
PA
7709/* Helper for create_solib_event_breakpoint /
7710 create_and_insert_solib_event_breakpoint. Allows specifying which
7711 INSERT_MODE to pass through to update_global_location_list. */
7712
7713static struct breakpoint *
7714create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7715 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7716{
7717 struct breakpoint *b;
7718
06edf0c0
PA
7719 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7720 &internal_breakpoint_ops);
04086b45 7721 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7722 return b;
7723}
7724
04086b45
PA
7725struct breakpoint *
7726create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7727{
7728 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7729}
7730
f37f681c
PA
7731/* See breakpoint.h. */
7732
7733struct breakpoint *
7734create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7735{
7736 struct breakpoint *b;
7737
04086b45
PA
7738 /* Explicitly tell update_global_location_list to insert
7739 locations. */
7740 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7741 if (!b->loc->inserted)
7742 {
7743 delete_breakpoint (b);
7744 return NULL;
7745 }
7746 return b;
7747}
7748
cae688ec
JJ
7749/* Disable any breakpoints that are on code in shared libraries. Only
7750 apply to enabled breakpoints, disabled ones can just stay disabled. */
7751
7752void
cb851954 7753disable_breakpoints_in_shlibs (void)
cae688ec 7754{
876fa593 7755 struct bp_location *loc, **locp_tmp;
cae688ec 7756
876fa593 7757 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7758 {
2bdf28a0 7759 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7760 struct breakpoint *b = loc->owner;
2bdf28a0 7761
4a64f543
MS
7762 /* We apply the check to all breakpoints, including disabled for
7763 those with loc->duplicate set. This is so that when breakpoint
7764 becomes enabled, or the duplicate is removed, gdb will try to
7765 insert all breakpoints. If we don't set shlib_disabled here,
7766 we'll try to insert those breakpoints and fail. */
1042e4c0 7767 if (((b->type == bp_breakpoint)
508ccb1f 7768 || (b->type == bp_jit_event)
1042e4c0 7769 || (b->type == bp_hardware_breakpoint)
d77f58be 7770 || (is_tracepoint (b)))
6c95b8df 7771 && loc->pspace == current_program_space
0d381245 7772 && !loc->shlib_disabled
6c95b8df 7773 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7774 )
0d381245
VP
7775 {
7776 loc->shlib_disabled = 1;
7777 }
cae688ec
JJ
7778 }
7779}
7780
63644780
NB
7781/* Disable any breakpoints and tracepoints that are in SOLIB upon
7782 notification of unloaded_shlib. Only apply to enabled breakpoints,
7783 disabled ones can just stay disabled. */
84acb35a 7784
75149521 7785static void
84acb35a
JJ
7786disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7787{
876fa593 7788 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7789 int disabled_shlib_breaks = 0;
7790
c86cf029
VP
7791 /* SunOS a.out shared libraries are always mapped, so do not
7792 disable breakpoints; they will only be reported as unloaded
7793 through clear_solib when GDB discards its shared library
7794 list. See clear_solib for more information. */
7795 if (exec_bfd != NULL
7796 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7797 return;
7798
876fa593 7799 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7800 {
2bdf28a0 7801 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7802 struct breakpoint *b = loc->owner;
cc59ec59 7803
1e4d1764 7804 if (solib->pspace == loc->pspace
e2dd7057 7805 && !loc->shlib_disabled
1e4d1764
YQ
7806 && (((b->type == bp_breakpoint
7807 || b->type == bp_jit_event
7808 || b->type == bp_hardware_breakpoint)
7809 && (loc->loc_type == bp_loc_hardware_breakpoint
7810 || loc->loc_type == bp_loc_software_breakpoint))
7811 || is_tracepoint (b))
e2dd7057 7812 && solib_contains_address_p (solib, loc->address))
84acb35a 7813 {
e2dd7057
PP
7814 loc->shlib_disabled = 1;
7815 /* At this point, we cannot rely on remove_breakpoint
7816 succeeding so we must mark the breakpoint as not inserted
7817 to prevent future errors occurring in remove_breakpoints. */
7818 loc->inserted = 0;
8d3788bd
VP
7819
7820 /* This may cause duplicate notifications for the same breakpoint. */
7821 observer_notify_breakpoint_modified (b);
7822
e2dd7057
PP
7823 if (!disabled_shlib_breaks)
7824 {
7825 target_terminal_ours_for_output ();
3e43a32a
MS
7826 warning (_("Temporarily disabling breakpoints "
7827 "for unloaded shared library \"%s\""),
e2dd7057 7828 solib->so_name);
84acb35a 7829 }
e2dd7057 7830 disabled_shlib_breaks = 1;
84acb35a
JJ
7831 }
7832 }
84acb35a
JJ
7833}
7834
63644780
NB
7835/* Disable any breakpoints and tracepoints in OBJFILE upon
7836 notification of free_objfile. Only apply to enabled breakpoints,
7837 disabled ones can just stay disabled. */
7838
7839static void
7840disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7841{
7842 struct breakpoint *b;
7843
7844 if (objfile == NULL)
7845 return;
7846
d03de421
PA
7847 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7848 managed by the user with add-symbol-file/remove-symbol-file.
7849 Similarly to how breakpoints in shared libraries are handled in
7850 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7851 shlib_disabled so they end up uninserted on the next global
7852 location list update. Shared libraries not loaded by the user
7853 aren't handled here -- they're already handled in
7854 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7855 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7856 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7857 main objfile). */
7858 if ((objfile->flags & OBJF_SHARED) == 0
7859 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7860 return;
7861
7862 ALL_BREAKPOINTS (b)
7863 {
7864 struct bp_location *loc;
7865 int bp_modified = 0;
7866
7867 if (!is_breakpoint (b) && !is_tracepoint (b))
7868 continue;
7869
7870 for (loc = b->loc; loc != NULL; loc = loc->next)
7871 {
7872 CORE_ADDR loc_addr = loc->address;
7873
7874 if (loc->loc_type != bp_loc_hardware_breakpoint
7875 && loc->loc_type != bp_loc_software_breakpoint)
7876 continue;
7877
7878 if (loc->shlib_disabled != 0)
7879 continue;
7880
7881 if (objfile->pspace != loc->pspace)
7882 continue;
7883
7884 if (loc->loc_type != bp_loc_hardware_breakpoint
7885 && loc->loc_type != bp_loc_software_breakpoint)
7886 continue;
7887
7888 if (is_addr_in_objfile (loc_addr, objfile))
7889 {
7890 loc->shlib_disabled = 1;
08351840
PA
7891 /* At this point, we don't know whether the object was
7892 unmapped from the inferior or not, so leave the
7893 inserted flag alone. We'll handle failure to
7894 uninsert quietly, in case the object was indeed
7895 unmapped. */
63644780
NB
7896
7897 mark_breakpoint_location_modified (loc);
7898
7899 bp_modified = 1;
7900 }
7901 }
7902
7903 if (bp_modified)
7904 observer_notify_breakpoint_modified (b);
7905 }
7906}
7907
ce78b96d
JB
7908/* FORK & VFORK catchpoints. */
7909
e29a4733
PA
7910/* An instance of this type is used to represent a fork or vfork
7911 catchpoint. It includes a "struct breakpoint" as a kind of base
7912 class; users downcast to "struct breakpoint *" when needed. A
7913 breakpoint is really of this type iff its ops pointer points to
7914 CATCH_FORK_BREAKPOINT_OPS. */
7915
7916struct fork_catchpoint
7917{
7918 /* The base class. */
7919 struct breakpoint base;
7920
7921 /* Process id of a child process whose forking triggered this
7922 catchpoint. This field is only valid immediately after this
7923 catchpoint has triggered. */
7924 ptid_t forked_inferior_pid;
7925};
7926
4a64f543
MS
7927/* Implement the "insert" breakpoint_ops method for fork
7928 catchpoints. */
ce78b96d 7929
77b06cd7
TJB
7930static int
7931insert_catch_fork (struct bp_location *bl)
ce78b96d 7932{
dfd4cc63 7933 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7934}
7935
4a64f543
MS
7936/* Implement the "remove" breakpoint_ops method for fork
7937 catchpoints. */
ce78b96d
JB
7938
7939static int
77b06cd7 7940remove_catch_fork (struct bp_location *bl)
ce78b96d 7941{
dfd4cc63 7942 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7943}
7944
7945/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7946 catchpoints. */
7947
7948static int
f1310107 7949breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7950 struct address_space *aspace, CORE_ADDR bp_addr,
7951 const struct target_waitstatus *ws)
ce78b96d 7952{
e29a4733
PA
7953 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7954
f90263c1
TT
7955 if (ws->kind != TARGET_WAITKIND_FORKED)
7956 return 0;
7957
7958 c->forked_inferior_pid = ws->value.related_pid;
7959 return 1;
ce78b96d
JB
7960}
7961
4a64f543
MS
7962/* Implement the "print_it" breakpoint_ops method for fork
7963 catchpoints. */
ce78b96d
JB
7964
7965static enum print_stop_action
348d480f 7966print_it_catch_fork (bpstat bs)
ce78b96d 7967{
36dfb11c 7968 struct ui_out *uiout = current_uiout;
348d480f
PA
7969 struct breakpoint *b = bs->breakpoint_at;
7970 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7971
ce78b96d 7972 annotate_catchpoint (b->number);
36dfb11c
TT
7973 if (b->disposition == disp_del)
7974 ui_out_text (uiout, "\nTemporary catchpoint ");
7975 else
7976 ui_out_text (uiout, "\nCatchpoint ");
7977 if (ui_out_is_mi_like_p (uiout))
7978 {
7979 ui_out_field_string (uiout, "reason",
7980 async_reason_lookup (EXEC_ASYNC_FORK));
7981 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7982 }
7983 ui_out_field_int (uiout, "bkptno", b->number);
7984 ui_out_text (uiout, " (forked process ");
7985 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7986 ui_out_text (uiout, "), ");
ce78b96d
JB
7987 return PRINT_SRC_AND_LOC;
7988}
7989
4a64f543
MS
7990/* Implement the "print_one" breakpoint_ops method for fork
7991 catchpoints. */
ce78b96d
JB
7992
7993static void
a6d9a66e 7994print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7995{
e29a4733 7996 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7997 struct value_print_options opts;
79a45e25 7998 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7999
8000 get_user_print_options (&opts);
8001
4a64f543
MS
8002 /* Field 4, the address, is omitted (which makes the columns not
8003 line up too nicely with the headers, but the effect is relatively
8004 readable). */
79a45b7d 8005 if (opts.addressprint)
ce78b96d
JB
8006 ui_out_field_skip (uiout, "addr");
8007 annotate_field (5);
8008 ui_out_text (uiout, "fork");
e29a4733 8009 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8010 {
8011 ui_out_text (uiout, ", process ");
8012 ui_out_field_int (uiout, "what",
e29a4733 8013 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8014 ui_out_spaces (uiout, 1);
8015 }
8ac3646f
TT
8016
8017 if (ui_out_is_mi_like_p (uiout))
8018 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8019}
8020
8021/* Implement the "print_mention" breakpoint_ops method for fork
8022 catchpoints. */
8023
8024static void
8025print_mention_catch_fork (struct breakpoint *b)
8026{
8027 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8028}
8029
6149aea9
PA
8030/* Implement the "print_recreate" breakpoint_ops method for fork
8031 catchpoints. */
8032
8033static void
8034print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8035{
8036 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8037 print_recreate_thread (b, fp);
6149aea9
PA
8038}
8039
ce78b96d
JB
8040/* The breakpoint_ops structure to be used in fork catchpoints. */
8041
2060206e 8042static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8043
4a64f543
MS
8044/* Implement the "insert" breakpoint_ops method for vfork
8045 catchpoints. */
ce78b96d 8046
77b06cd7
TJB
8047static int
8048insert_catch_vfork (struct bp_location *bl)
ce78b96d 8049{
dfd4cc63 8050 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8051}
8052
4a64f543
MS
8053/* Implement the "remove" breakpoint_ops method for vfork
8054 catchpoints. */
ce78b96d
JB
8055
8056static int
77b06cd7 8057remove_catch_vfork (struct bp_location *bl)
ce78b96d 8058{
dfd4cc63 8059 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8060}
8061
8062/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8063 catchpoints. */
8064
8065static int
f1310107 8066breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8067 struct address_space *aspace, CORE_ADDR bp_addr,
8068 const struct target_waitstatus *ws)
ce78b96d 8069{
e29a4733
PA
8070 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8071
f90263c1
TT
8072 if (ws->kind != TARGET_WAITKIND_VFORKED)
8073 return 0;
8074
8075 c->forked_inferior_pid = ws->value.related_pid;
8076 return 1;
ce78b96d
JB
8077}
8078
4a64f543
MS
8079/* Implement the "print_it" breakpoint_ops method for vfork
8080 catchpoints. */
ce78b96d
JB
8081
8082static enum print_stop_action
348d480f 8083print_it_catch_vfork (bpstat bs)
ce78b96d 8084{
36dfb11c 8085 struct ui_out *uiout = current_uiout;
348d480f 8086 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8087 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8088
ce78b96d 8089 annotate_catchpoint (b->number);
36dfb11c
TT
8090 if (b->disposition == disp_del)
8091 ui_out_text (uiout, "\nTemporary catchpoint ");
8092 else
8093 ui_out_text (uiout, "\nCatchpoint ");
8094 if (ui_out_is_mi_like_p (uiout))
8095 {
8096 ui_out_field_string (uiout, "reason",
8097 async_reason_lookup (EXEC_ASYNC_VFORK));
8098 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8099 }
8100 ui_out_field_int (uiout, "bkptno", b->number);
8101 ui_out_text (uiout, " (vforked process ");
8102 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8103 ui_out_text (uiout, "), ");
ce78b96d
JB
8104 return PRINT_SRC_AND_LOC;
8105}
8106
4a64f543
MS
8107/* Implement the "print_one" breakpoint_ops method for vfork
8108 catchpoints. */
ce78b96d
JB
8109
8110static void
a6d9a66e 8111print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8112{
e29a4733 8113 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8114 struct value_print_options opts;
79a45e25 8115 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8116
8117 get_user_print_options (&opts);
4a64f543
MS
8118 /* Field 4, the address, is omitted (which makes the columns not
8119 line up too nicely with the headers, but the effect is relatively
8120 readable). */
79a45b7d 8121 if (opts.addressprint)
ce78b96d
JB
8122 ui_out_field_skip (uiout, "addr");
8123 annotate_field (5);
8124 ui_out_text (uiout, "vfork");
e29a4733 8125 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8126 {
8127 ui_out_text (uiout, ", process ");
8128 ui_out_field_int (uiout, "what",
e29a4733 8129 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8130 ui_out_spaces (uiout, 1);
8131 }
8ac3646f
TT
8132
8133 if (ui_out_is_mi_like_p (uiout))
8134 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8135}
8136
8137/* Implement the "print_mention" breakpoint_ops method for vfork
8138 catchpoints. */
8139
8140static void
8141print_mention_catch_vfork (struct breakpoint *b)
8142{
8143 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8144}
8145
6149aea9
PA
8146/* Implement the "print_recreate" breakpoint_ops method for vfork
8147 catchpoints. */
8148
8149static void
8150print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8151{
8152 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8153 print_recreate_thread (b, fp);
6149aea9
PA
8154}
8155
ce78b96d
JB
8156/* The breakpoint_ops structure to be used in vfork catchpoints. */
8157
2060206e 8158static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8159
edcc5120
TT
8160/* An instance of this type is used to represent an solib catchpoint.
8161 It includes a "struct breakpoint" as a kind of base class; users
8162 downcast to "struct breakpoint *" when needed. A breakpoint is
8163 really of this type iff its ops pointer points to
8164 CATCH_SOLIB_BREAKPOINT_OPS. */
8165
8166struct solib_catchpoint
8167{
8168 /* The base class. */
8169 struct breakpoint base;
8170
8171 /* True for "catch load", false for "catch unload". */
8172 unsigned char is_load;
8173
8174 /* Regular expression to match, if any. COMPILED is only valid when
8175 REGEX is non-NULL. */
8176 char *regex;
8177 regex_t compiled;
8178};
8179
8180static void
8181dtor_catch_solib (struct breakpoint *b)
8182{
8183 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8184
8185 if (self->regex)
8186 regfree (&self->compiled);
8187 xfree (self->regex);
8188
8189 base_breakpoint_ops.dtor (b);
8190}
8191
8192static int
8193insert_catch_solib (struct bp_location *ignore)
8194{
8195 return 0;
8196}
8197
8198static int
8199remove_catch_solib (struct bp_location *ignore)
8200{
8201 return 0;
8202}
8203
8204static int
8205breakpoint_hit_catch_solib (const struct bp_location *bl,
8206 struct address_space *aspace,
8207 CORE_ADDR bp_addr,
8208 const struct target_waitstatus *ws)
8209{
8210 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8211 struct breakpoint *other;
8212
8213 if (ws->kind == TARGET_WAITKIND_LOADED)
8214 return 1;
8215
8216 ALL_BREAKPOINTS (other)
8217 {
8218 struct bp_location *other_bl;
8219
8220 if (other == bl->owner)
8221 continue;
8222
8223 if (other->type != bp_shlib_event)
8224 continue;
8225
8226 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8227 continue;
8228
8229 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8230 {
8231 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8232 return 1;
8233 }
8234 }
8235
8236 return 0;
8237}
8238
8239static void
8240check_status_catch_solib (struct bpstats *bs)
8241{
8242 struct solib_catchpoint *self
8243 = (struct solib_catchpoint *) bs->breakpoint_at;
8244 int ix;
8245
8246 if (self->is_load)
8247 {
8248 struct so_list *iter;
8249
8250 for (ix = 0;
8251 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8252 ix, iter);
8253 ++ix)
8254 {
8255 if (!self->regex
8256 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8257 return;
8258 }
8259 }
8260 else
8261 {
8262 char *iter;
8263
8264 for (ix = 0;
8265 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8266 ix, iter);
8267 ++ix)
8268 {
8269 if (!self->regex
8270 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8271 return;
8272 }
8273 }
8274
8275 bs->stop = 0;
8276 bs->print_it = print_it_noop;
8277}
8278
8279static enum print_stop_action
8280print_it_catch_solib (bpstat bs)
8281{
8282 struct breakpoint *b = bs->breakpoint_at;
8283 struct ui_out *uiout = current_uiout;
8284
8285 annotate_catchpoint (b->number);
8286 if (b->disposition == disp_del)
8287 ui_out_text (uiout, "\nTemporary catchpoint ");
8288 else
8289 ui_out_text (uiout, "\nCatchpoint ");
8290 ui_out_field_int (uiout, "bkptno", b->number);
8291 ui_out_text (uiout, "\n");
8292 if (ui_out_is_mi_like_p (uiout))
8293 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8294 print_solib_event (1);
8295 return PRINT_SRC_AND_LOC;
8296}
8297
8298static void
8299print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8300{
8301 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8302 struct value_print_options opts;
8303 struct ui_out *uiout = current_uiout;
8304 char *msg;
8305
8306 get_user_print_options (&opts);
8307 /* Field 4, the address, is omitted (which makes the columns not
8308 line up too nicely with the headers, but the effect is relatively
8309 readable). */
8310 if (opts.addressprint)
8311 {
8312 annotate_field (4);
8313 ui_out_field_skip (uiout, "addr");
8314 }
8315
8316 annotate_field (5);
8317 if (self->is_load)
8318 {
8319 if (self->regex)
8320 msg = xstrprintf (_("load of library matching %s"), self->regex);
8321 else
8322 msg = xstrdup (_("load of library"));
8323 }
8324 else
8325 {
8326 if (self->regex)
8327 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8328 else
8329 msg = xstrdup (_("unload of library"));
8330 }
8331 ui_out_field_string (uiout, "what", msg);
8332 xfree (msg);
8ac3646f
TT
8333
8334 if (ui_out_is_mi_like_p (uiout))
8335 ui_out_field_string (uiout, "catch-type",
8336 self->is_load ? "load" : "unload");
edcc5120
TT
8337}
8338
8339static void
8340print_mention_catch_solib (struct breakpoint *b)
8341{
8342 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8343
8344 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8345 self->is_load ? "load" : "unload");
8346}
8347
8348static void
8349print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8350{
8351 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8352
8353 fprintf_unfiltered (fp, "%s %s",
8354 b->disposition == disp_del ? "tcatch" : "catch",
8355 self->is_load ? "load" : "unload");
8356 if (self->regex)
8357 fprintf_unfiltered (fp, " %s", self->regex);
8358 fprintf_unfiltered (fp, "\n");
8359}
8360
8361static struct breakpoint_ops catch_solib_breakpoint_ops;
8362
91985142
MG
8363/* Shared helper function (MI and CLI) for creating and installing
8364 a shared object event catchpoint. If IS_LOAD is non-zero then
8365 the events to be caught are load events, otherwise they are
8366 unload events. If IS_TEMP is non-zero the catchpoint is a
8367 temporary one. If ENABLED is non-zero the catchpoint is
8368 created in an enabled state. */
edcc5120 8369
91985142
MG
8370void
8371add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8372{
8373 struct solib_catchpoint *c;
8374 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8375 struct cleanup *cleanup;
8376
edcc5120
TT
8377 if (!arg)
8378 arg = "";
8379 arg = skip_spaces (arg);
8380
8381 c = XCNEW (struct solib_catchpoint);
8382 cleanup = make_cleanup (xfree, c);
8383
8384 if (*arg != '\0')
8385 {
8386 int errcode;
8387
8388 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8389 if (errcode != 0)
8390 {
8391 char *err = get_regcomp_error (errcode, &c->compiled);
8392
8393 make_cleanup (xfree, err);
8394 error (_("Invalid regexp (%s): %s"), err, arg);
8395 }
8396 c->regex = xstrdup (arg);
8397 }
8398
8399 c->is_load = is_load;
91985142 8400 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8401 &catch_solib_breakpoint_ops);
8402
91985142
MG
8403 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8404
edcc5120
TT
8405 discard_cleanups (cleanup);
8406 install_breakpoint (0, &c->base, 1);
8407}
8408
91985142
MG
8409/* A helper function that does all the work for "catch load" and
8410 "catch unload". */
8411
8412static void
8413catch_load_or_unload (char *arg, int from_tty, int is_load,
8414 struct cmd_list_element *command)
8415{
8416 int tempflag;
8417 const int enabled = 1;
8418
8419 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8420
8421 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8422}
8423
edcc5120
TT
8424static void
8425catch_load_command_1 (char *arg, int from_tty,
8426 struct cmd_list_element *command)
8427{
8428 catch_load_or_unload (arg, from_tty, 1, command);
8429}
8430
8431static void
8432catch_unload_command_1 (char *arg, int from_tty,
8433 struct cmd_list_element *command)
8434{
8435 catch_load_or_unload (arg, from_tty, 0, command);
8436}
8437
be5c67c1
PA
8438/* An instance of this type is used to represent a syscall catchpoint.
8439 It includes a "struct breakpoint" as a kind of base class; users
8440 downcast to "struct breakpoint *" when needed. A breakpoint is
8441 really of this type iff its ops pointer points to
8442 CATCH_SYSCALL_BREAKPOINT_OPS. */
8443
8444struct syscall_catchpoint
8445{
8446 /* The base class. */
8447 struct breakpoint base;
8448
8449 /* Syscall numbers used for the 'catch syscall' feature. If no
8450 syscall has been specified for filtering, its value is NULL.
8451 Otherwise, it holds a list of all syscalls to be caught. The
8452 list elements are allocated with xmalloc. */
8453 VEC(int) *syscalls_to_be_caught;
8454};
8455
8456/* Implement the "dtor" breakpoint_ops method for syscall
8457 catchpoints. */
8458
8459static void
8460dtor_catch_syscall (struct breakpoint *b)
8461{
8462 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8463
8464 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8465
2060206e 8466 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8467}
8468
fa3064dd
YQ
8469static const struct inferior_data *catch_syscall_inferior_data = NULL;
8470
8471struct catch_syscall_inferior_data
8472{
8473 /* We keep a count of the number of times the user has requested a
8474 particular syscall to be tracked, and pass this information to the
8475 target. This lets capable targets implement filtering directly. */
8476
8477 /* Number of times that "any" syscall is requested. */
8478 int any_syscall_count;
8479
8480 /* Count of each system call. */
8481 VEC(int) *syscalls_counts;
8482
8483 /* This counts all syscall catch requests, so we can readily determine
8484 if any catching is necessary. */
8485 int total_syscalls_count;
8486};
8487
8488static struct catch_syscall_inferior_data*
8489get_catch_syscall_inferior_data (struct inferior *inf)
8490{
8491 struct catch_syscall_inferior_data *inf_data;
8492
8493 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8494 if (inf_data == NULL)
8495 {
41bf6aca 8496 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8497 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8498 }
8499
8500 return inf_data;
8501}
8502
8503static void
8504catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8505{
8506 xfree (arg);
8507}
8508
8509
a96d9b2e
SDJ
8510/* Implement the "insert" breakpoint_ops method for syscall
8511 catchpoints. */
8512
77b06cd7
TJB
8513static int
8514insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8515{
be5c67c1 8516 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8517 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8518 struct catch_syscall_inferior_data *inf_data
8519 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8520
fa3064dd 8521 ++inf_data->total_syscalls_count;
be5c67c1 8522 if (!c->syscalls_to_be_caught)
fa3064dd 8523 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8524 else
8525 {
8526 int i, iter;
cc59ec59 8527
a96d9b2e 8528 for (i = 0;
be5c67c1 8529 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8530 i++)
8531 {
8532 int elem;
cc59ec59 8533
fa3064dd 8534 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8535 {
fa3064dd 8536 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8537 uintptr_t vec_addr_offset
8538 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8539 uintptr_t vec_addr;
fa3064dd
YQ
8540 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8541 vec_addr = ((uintptr_t) VEC_address (int,
8542 inf_data->syscalls_counts)
8543 + vec_addr_offset);
a96d9b2e
SDJ
8544 memset ((void *) vec_addr, 0,
8545 (iter + 1 - old_size) * sizeof (int));
8546 }
fa3064dd
YQ
8547 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8548 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8549 }
8550 }
8551
dfd4cc63 8552 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8553 inf_data->total_syscalls_count != 0,
8554 inf_data->any_syscall_count,
8555 VEC_length (int,
8556 inf_data->syscalls_counts),
8557 VEC_address (int,
8558 inf_data->syscalls_counts));
a96d9b2e
SDJ
8559}
8560
8561/* Implement the "remove" breakpoint_ops method for syscall
8562 catchpoints. */
8563
8564static int
77b06cd7 8565remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8566{
be5c67c1 8567 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8568 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8569 struct catch_syscall_inferior_data *inf_data
8570 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8571
fa3064dd 8572 --inf_data->total_syscalls_count;
be5c67c1 8573 if (!c->syscalls_to_be_caught)
fa3064dd 8574 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8575 else
8576 {
8577 int i, iter;
cc59ec59 8578
a96d9b2e 8579 for (i = 0;
be5c67c1 8580 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8581 i++)
8582 {
8583 int elem;
fa3064dd 8584 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8585 /* Shouldn't happen. */
8586 continue;
fa3064dd
YQ
8587 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8588 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8589 }
8590 }
8591
dfd4cc63 8592 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8593 inf_data->total_syscalls_count != 0,
8594 inf_data->any_syscall_count,
8595 VEC_length (int,
8596 inf_data->syscalls_counts),
3e43a32a 8597 VEC_address (int,
fa3064dd 8598 inf_data->syscalls_counts));
a96d9b2e
SDJ
8599}
8600
8601/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8602 catchpoints. */
8603
8604static int
f1310107 8605breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8606 struct address_space *aspace, CORE_ADDR bp_addr,
8607 const struct target_waitstatus *ws)
a96d9b2e 8608{
4a64f543
MS
8609 /* We must check if we are catching specific syscalls in this
8610 breakpoint. If we are, then we must guarantee that the called
8611 syscall is the same syscall we are catching. */
a96d9b2e 8612 int syscall_number = 0;
be5c67c1
PA
8613 const struct syscall_catchpoint *c
8614 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8615
f90263c1
TT
8616 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8617 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8618 return 0;
8619
f90263c1
TT
8620 syscall_number = ws->value.syscall_number;
8621
a96d9b2e 8622 /* Now, checking if the syscall is the same. */
be5c67c1 8623 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8624 {
8625 int i, iter;
cc59ec59 8626
a96d9b2e 8627 for (i = 0;
be5c67c1 8628 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8629 i++)
8630 if (syscall_number == iter)
4924df79
GKB
8631 return 1;
8632
8633 return 0;
a96d9b2e
SDJ
8634 }
8635
8636 return 1;
8637}
8638
8639/* Implement the "print_it" breakpoint_ops method for syscall
8640 catchpoints. */
8641
8642static enum print_stop_action
348d480f 8643print_it_catch_syscall (bpstat bs)
a96d9b2e 8644{
36dfb11c 8645 struct ui_out *uiout = current_uiout;
348d480f 8646 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8647 /* These are needed because we want to know in which state a
8648 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8649 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8650 must print "called syscall" or "returned from syscall". */
8651 ptid_t ptid;
8652 struct target_waitstatus last;
8653 struct syscall s;
458c8db8 8654 struct gdbarch *gdbarch = bs->bp_location_at->gdbarch;
a96d9b2e
SDJ
8655
8656 get_last_target_status (&ptid, &last);
8657
458c8db8 8658 get_syscall_by_number (gdbarch, last.value.syscall_number, &s);
a96d9b2e
SDJ
8659
8660 annotate_catchpoint (b->number);
8661
36dfb11c
TT
8662 if (b->disposition == disp_del)
8663 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8664 else
36dfb11c
TT
8665 ui_out_text (uiout, "\nCatchpoint ");
8666 if (ui_out_is_mi_like_p (uiout))
8667 {
8668 ui_out_field_string (uiout, "reason",
8669 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8670 ? EXEC_ASYNC_SYSCALL_ENTRY
8671 : EXEC_ASYNC_SYSCALL_RETURN));
8672 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8673 }
8674 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8675
8676 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8677 ui_out_text (uiout, " (call to syscall ");
8678 else
8679 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8680
36dfb11c
TT
8681 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8682 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8683 if (s.name != NULL)
8684 ui_out_field_string (uiout, "syscall-name", s.name);
8685
8686 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8687
8688 return PRINT_SRC_AND_LOC;
8689}
8690
8691/* Implement the "print_one" breakpoint_ops method for syscall
8692 catchpoints. */
8693
8694static void
8695print_one_catch_syscall (struct breakpoint *b,
f1310107 8696 struct bp_location **last_loc)
a96d9b2e 8697{
be5c67c1 8698 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8699 struct value_print_options opts;
79a45e25 8700 struct ui_out *uiout = current_uiout;
458c8db8 8701 struct gdbarch *gdbarch = b->loc->gdbarch;
a96d9b2e
SDJ
8702
8703 get_user_print_options (&opts);
4a64f543
MS
8704 /* Field 4, the address, is omitted (which makes the columns not
8705 line up too nicely with the headers, but the effect is relatively
8706 readable). */
a96d9b2e
SDJ
8707 if (opts.addressprint)
8708 ui_out_field_skip (uiout, "addr");
8709 annotate_field (5);
8710
be5c67c1
PA
8711 if (c->syscalls_to_be_caught
8712 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8713 ui_out_text (uiout, "syscalls \"");
8714 else
8715 ui_out_text (uiout, "syscall \"");
8716
be5c67c1 8717 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8718 {
8719 int i, iter;
8720 char *text = xstrprintf ("%s", "");
cc59ec59 8721
a96d9b2e 8722 for (i = 0;
be5c67c1 8723 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8724 i++)
8725 {
8726 char *x = text;
8727 struct syscall s;
458c8db8 8728 get_syscall_by_number (gdbarch, iter, &s);
a96d9b2e
SDJ
8729
8730 if (s.name != NULL)
8731 text = xstrprintf ("%s%s, ", text, s.name);
8732 else
8733 text = xstrprintf ("%s%d, ", text, iter);
8734
8735 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8736 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8737 on every call. */
8738 xfree (x);
8739 }
8740 /* Remove the last comma. */
8741 text[strlen (text) - 2] = '\0';
8742 ui_out_field_string (uiout, "what", text);
8743 }
8744 else
8745 ui_out_field_string (uiout, "what", "<any syscall>");
8746 ui_out_text (uiout, "\" ");
8ac3646f
TT
8747
8748 if (ui_out_is_mi_like_p (uiout))
8749 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8750}
8751
8752/* Implement the "print_mention" breakpoint_ops method for syscall
8753 catchpoints. */
8754
8755static void
8756print_mention_catch_syscall (struct breakpoint *b)
8757{
be5c67c1 8758 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
458c8db8 8759 struct gdbarch *gdbarch = b->loc->gdbarch;
be5c67c1
PA
8760
8761 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8762 {
8763 int i, iter;
8764
be5c67c1 8765 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8766 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8767 else
8768 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8769
8770 for (i = 0;
be5c67c1 8771 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8772 i++)
8773 {
8774 struct syscall s;
458c8db8 8775 get_syscall_by_number (gdbarch, iter, &s);
a96d9b2e
SDJ
8776
8777 if (s.name)
8778 printf_filtered (" '%s' [%d]", s.name, s.number);
8779 else
8780 printf_filtered (" %d", s.number);
8781 }
8782 printf_filtered (")");
8783 }
8784 else
8785 printf_filtered (_("Catchpoint %d (any syscall)"),
8786 b->number);
8787}
8788
6149aea9
PA
8789/* Implement the "print_recreate" breakpoint_ops method for syscall
8790 catchpoints. */
8791
8792static void
8793print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8794{
be5c67c1 8795 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
458c8db8 8796 struct gdbarch *gdbarch = b->loc->gdbarch;
be5c67c1 8797
6149aea9
PA
8798 fprintf_unfiltered (fp, "catch syscall");
8799
be5c67c1 8800 if (c->syscalls_to_be_caught)
6149aea9
PA
8801 {
8802 int i, iter;
8803
8804 for (i = 0;
be5c67c1 8805 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8806 i++)
8807 {
8808 struct syscall s;
8809
458c8db8 8810 get_syscall_by_number (gdbarch, iter, &s);
6149aea9
PA
8811 if (s.name)
8812 fprintf_unfiltered (fp, " %s", s.name);
8813 else
8814 fprintf_unfiltered (fp, " %d", s.number);
8815 }
8816 }
d9b3f62e 8817 print_recreate_thread (b, fp);
6149aea9
PA
8818}
8819
a96d9b2e
SDJ
8820/* The breakpoint_ops structure to be used in syscall catchpoints. */
8821
2060206e 8822static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8823
8824/* Returns non-zero if 'b' is a syscall catchpoint. */
8825
8826static int
8827syscall_catchpoint_p (struct breakpoint *b)
8828{
8829 return (b->ops == &catch_syscall_breakpoint_ops);
8830}
8831
346774a9
PA
8832/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8833 is non-zero, then make the breakpoint temporary. If COND_STRING is
8834 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8835 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8836
ab04a2af 8837void
346774a9
PA
8838init_catchpoint (struct breakpoint *b,
8839 struct gdbarch *gdbarch, int tempflag,
8840 char *cond_string,
c0a91b2b 8841 const struct breakpoint_ops *ops)
c906108c 8842{
c5aa993b 8843 struct symtab_and_line sal;
346774a9 8844
fe39c653 8845 init_sal (&sal);
6c95b8df 8846 sal.pspace = current_program_space;
c5aa993b 8847
28010a5d 8848 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8849
1b36a34b 8850 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8851 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8852}
8853
28010a5d 8854void
3ea46bff 8855install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8856{
8857 add_to_breakpoint_chain (b);
3a5c3e22 8858 set_breakpoint_number (internal, b);
558a9d82
YQ
8859 if (is_tracepoint (b))
8860 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8861 if (!internal)
8862 mention (b);
c56053d2 8863 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8864
8865 if (update_gll)
44702360 8866 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8867}
8868
9b70b993 8869static void
a6d9a66e
UW
8870create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8871 int tempflag, char *cond_string,
c0a91b2b 8872 const struct breakpoint_ops *ops)
c906108c 8873{
e29a4733 8874 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8875
e29a4733
PA
8876 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8877
8878 c->forked_inferior_pid = null_ptid;
8879
3ea46bff 8880 install_breakpoint (0, &c->base, 1);
c906108c
SS
8881}
8882
fe798b75
JB
8883/* Exec catchpoints. */
8884
b4d90040
PA
8885/* An instance of this type is used to represent an exec catchpoint.
8886 It includes a "struct breakpoint" as a kind of base class; users
8887 downcast to "struct breakpoint *" when needed. A breakpoint is
8888 really of this type iff its ops pointer points to
8889 CATCH_EXEC_BREAKPOINT_OPS. */
8890
8891struct exec_catchpoint
8892{
8893 /* The base class. */
8894 struct breakpoint base;
8895
8896 /* Filename of a program whose exec triggered this catchpoint.
8897 This field is only valid immediately after this catchpoint has
8898 triggered. */
8899 char *exec_pathname;
8900};
8901
8902/* Implement the "dtor" breakpoint_ops method for exec
8903 catchpoints. */
8904
8905static void
8906dtor_catch_exec (struct breakpoint *b)
8907{
8908 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8909
8910 xfree (c->exec_pathname);
348d480f 8911
2060206e 8912 base_breakpoint_ops.dtor (b);
b4d90040
PA
8913}
8914
77b06cd7
TJB
8915static int
8916insert_catch_exec (struct bp_location *bl)
c906108c 8917{
dfd4cc63 8918 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8919}
c906108c 8920
fe798b75 8921static int
77b06cd7 8922remove_catch_exec (struct bp_location *bl)
fe798b75 8923{
dfd4cc63 8924 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8925}
c906108c 8926
fe798b75 8927static int
f1310107 8928breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8929 struct address_space *aspace, CORE_ADDR bp_addr,
8930 const struct target_waitstatus *ws)
fe798b75 8931{
b4d90040
PA
8932 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8933
f90263c1
TT
8934 if (ws->kind != TARGET_WAITKIND_EXECD)
8935 return 0;
8936
8937 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8938 return 1;
fe798b75 8939}
c906108c 8940
fe798b75 8941static enum print_stop_action
348d480f 8942print_it_catch_exec (bpstat bs)
fe798b75 8943{
36dfb11c 8944 struct ui_out *uiout = current_uiout;
348d480f 8945 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8946 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8947
fe798b75 8948 annotate_catchpoint (b->number);
36dfb11c
TT
8949 if (b->disposition == disp_del)
8950 ui_out_text (uiout, "\nTemporary catchpoint ");
8951 else
8952 ui_out_text (uiout, "\nCatchpoint ");
8953 if (ui_out_is_mi_like_p (uiout))
8954 {
8955 ui_out_field_string (uiout, "reason",
8956 async_reason_lookup (EXEC_ASYNC_EXEC));
8957 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8958 }
8959 ui_out_field_int (uiout, "bkptno", b->number);
8960 ui_out_text (uiout, " (exec'd ");
8961 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8962 ui_out_text (uiout, "), ");
8963
fe798b75 8964 return PRINT_SRC_AND_LOC;
c906108c
SS
8965}
8966
fe798b75 8967static void
a6d9a66e 8968print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8969{
b4d90040 8970 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8971 struct value_print_options opts;
79a45e25 8972 struct ui_out *uiout = current_uiout;
fe798b75
JB
8973
8974 get_user_print_options (&opts);
8975
8976 /* Field 4, the address, is omitted (which makes the columns
8977 not line up too nicely with the headers, but the effect
8978 is relatively readable). */
8979 if (opts.addressprint)
8980 ui_out_field_skip (uiout, "addr");
8981 annotate_field (5);
8982 ui_out_text (uiout, "exec");
b4d90040 8983 if (c->exec_pathname != NULL)
fe798b75
JB
8984 {
8985 ui_out_text (uiout, ", program \"");
b4d90040 8986 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8987 ui_out_text (uiout, "\" ");
8988 }
8ac3646f
TT
8989
8990 if (ui_out_is_mi_like_p (uiout))
8991 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8992}
8993
8994static void
8995print_mention_catch_exec (struct breakpoint *b)
8996{
8997 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8998}
8999
6149aea9
PA
9000/* Implement the "print_recreate" breakpoint_ops method for exec
9001 catchpoints. */
9002
9003static void
9004print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
9005{
9006 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 9007 print_recreate_thread (b, fp);
6149aea9
PA
9008}
9009
2060206e 9010static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 9011
a96d9b2e
SDJ
9012static void
9013create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 9014 const struct breakpoint_ops *ops)
a96d9b2e 9015{
be5c67c1 9016 struct syscall_catchpoint *c;
a96d9b2e 9017 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 9018
be5c67c1
PA
9019 c = XNEW (struct syscall_catchpoint);
9020 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
9021 c->syscalls_to_be_caught = filter;
a96d9b2e 9022
3ea46bff 9023 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
9024}
9025
c906108c 9026static int
fba45db2 9027hw_breakpoint_used_count (void)
c906108c 9028{
c906108c 9029 int i = 0;
f1310107
TJB
9030 struct breakpoint *b;
9031 struct bp_location *bl;
c906108c
SS
9032
9033 ALL_BREAKPOINTS (b)
c5aa993b 9034 {
d6b74ac4 9035 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
9036 for (bl = b->loc; bl; bl = bl->next)
9037 {
9038 /* Special types of hardware breakpoints may use more than
9039 one register. */
348d480f 9040 i += b->ops->resources_needed (bl);
f1310107 9041 }
c5aa993b 9042 }
c906108c
SS
9043
9044 return i;
9045}
9046
a1398e0c
PA
9047/* Returns the resources B would use if it were a hardware
9048 watchpoint. */
9049
c906108c 9050static int
a1398e0c 9051hw_watchpoint_use_count (struct breakpoint *b)
c906108c 9052{
c906108c 9053 int i = 0;
e09342b5 9054 struct bp_location *bl;
c906108c 9055
a1398e0c
PA
9056 if (!breakpoint_enabled (b))
9057 return 0;
9058
9059 for (bl = b->loc; bl; bl = bl->next)
9060 {
9061 /* Special types of hardware watchpoints may use more than
9062 one register. */
9063 i += b->ops->resources_needed (bl);
9064 }
9065
9066 return i;
9067}
9068
9069/* Returns the sum the used resources of all hardware watchpoints of
9070 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
9071 the sum of the used resources of all hardware watchpoints of other
9072 types _not_ TYPE. */
9073
9074static int
9075hw_watchpoint_used_count_others (struct breakpoint *except,
9076 enum bptype type, int *other_type_used)
9077{
9078 int i = 0;
9079 struct breakpoint *b;
9080
c906108c
SS
9081 *other_type_used = 0;
9082 ALL_BREAKPOINTS (b)
e09342b5 9083 {
a1398e0c
PA
9084 if (b == except)
9085 continue;
e09342b5
TJB
9086 if (!breakpoint_enabled (b))
9087 continue;
9088
a1398e0c
PA
9089 if (b->type == type)
9090 i += hw_watchpoint_use_count (b);
9091 else if (is_hardware_watchpoint (b))
9092 *other_type_used = 1;
e09342b5
TJB
9093 }
9094
c906108c
SS
9095 return i;
9096}
9097
c906108c 9098void
fba45db2 9099disable_watchpoints_before_interactive_call_start (void)
c906108c 9100{
c5aa993b 9101 struct breakpoint *b;
c906108c
SS
9102
9103 ALL_BREAKPOINTS (b)
c5aa993b 9104 {
cc60f2e3 9105 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 9106 {
b5de0fa7 9107 b->enable_state = bp_call_disabled;
44702360 9108 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
9109 }
9110 }
c906108c
SS
9111}
9112
9113void
fba45db2 9114enable_watchpoints_after_interactive_call_stop (void)
c906108c 9115{
c5aa993b 9116 struct breakpoint *b;
c906108c
SS
9117
9118 ALL_BREAKPOINTS (b)
c5aa993b 9119 {
cc60f2e3 9120 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 9121 {
b5de0fa7 9122 b->enable_state = bp_enabled;
44702360 9123 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
9124 }
9125 }
c906108c
SS
9126}
9127
8bea4e01
UW
9128void
9129disable_breakpoints_before_startup (void)
9130{
6c95b8df 9131 current_program_space->executing_startup = 1;
44702360 9132 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
9133}
9134
9135void
9136enable_breakpoints_after_startup (void)
9137{
6c95b8df 9138 current_program_space->executing_startup = 0;
f8eba3c6 9139 breakpoint_re_set ();
8bea4e01
UW
9140}
9141
7c16b83e
PA
9142/* Create a new single-step breakpoint for thread THREAD, with no
9143 locations. */
c906108c 9144
7c16b83e
PA
9145static struct breakpoint *
9146new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
9147{
9148 struct breakpoint *b = XNEW (struct breakpoint);
9149
9150 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
9151 &momentary_breakpoint_ops);
9152
9153 b->disposition = disp_donttouch;
9154 b->frame_id = null_frame_id;
9155
9156 b->thread = thread;
9157 gdb_assert (b->thread != 0);
9158
9159 add_to_breakpoint_chain (b);
9160
9161 return b;
9162}
9163
9164/* Set a momentary breakpoint of type TYPE at address specified by
9165 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
9166 frame. */
c906108c
SS
9167
9168struct breakpoint *
a6d9a66e
UW
9169set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9170 struct frame_id frame_id, enum bptype type)
c906108c 9171{
52f0bd74 9172 struct breakpoint *b;
edb3359d 9173
193facb3
JK
9174 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9175 tail-called one. */
9176 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 9177
06edf0c0 9178 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
9179 b->enable_state = bp_enabled;
9180 b->disposition = disp_donttouch;
818dd999 9181 b->frame_id = frame_id;
c906108c 9182
4a64f543
MS
9183 /* If we're debugging a multi-threaded program, then we want
9184 momentary breakpoints to be active in only a single thread of
9185 control. */
39f77062
KB
9186 if (in_thread_list (inferior_ptid))
9187 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 9188
44702360 9189 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 9190
c906108c
SS
9191 return b;
9192}
611c83ae 9193
06edf0c0 9194/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
9195 The new breakpoint will have type TYPE, use OPS as its
9196 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 9197
06edf0c0
PA
9198static struct breakpoint *
9199momentary_breakpoint_from_master (struct breakpoint *orig,
9200 enum bptype type,
a1aa2221
LM
9201 const struct breakpoint_ops *ops,
9202 int loc_enabled)
e58b0e63
PA
9203{
9204 struct breakpoint *copy;
9205
06edf0c0 9206 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 9207 copy->loc = allocate_bp_location (copy);
0e30163f 9208 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 9209
a6d9a66e 9210 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
9211 copy->loc->requested_address = orig->loc->requested_address;
9212 copy->loc->address = orig->loc->address;
9213 copy->loc->section = orig->loc->section;
6c95b8df 9214 copy->loc->pspace = orig->loc->pspace;
55aa24fb 9215 copy->loc->probe = orig->loc->probe;
f8eba3c6 9216 copy->loc->line_number = orig->loc->line_number;
2f202fde 9217 copy->loc->symtab = orig->loc->symtab;
a1aa2221 9218 copy->loc->enabled = loc_enabled;
e58b0e63
PA
9219 copy->frame_id = orig->frame_id;
9220 copy->thread = orig->thread;
6c95b8df 9221 copy->pspace = orig->pspace;
e58b0e63
PA
9222
9223 copy->enable_state = bp_enabled;
9224 copy->disposition = disp_donttouch;
9225 copy->number = internal_breakpoint_number--;
9226
44702360 9227 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
9228 return copy;
9229}
9230
06edf0c0
PA
9231/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9232 ORIG is NULL. */
9233
9234struct breakpoint *
9235clone_momentary_breakpoint (struct breakpoint *orig)
9236{
9237 /* If there's nothing to clone, then return nothing. */
9238 if (orig == NULL)
9239 return NULL;
9240
a1aa2221 9241 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
9242}
9243
611c83ae 9244struct breakpoint *
a6d9a66e
UW
9245set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9246 enum bptype type)
611c83ae
PA
9247{
9248 struct symtab_and_line sal;
9249
9250 sal = find_pc_line (pc, 0);
9251 sal.pc = pc;
9252 sal.section = find_pc_overlay (pc);
9253 sal.explicit_pc = 1;
9254
a6d9a66e 9255 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9256}
c906108c 9257\f
c5aa993b 9258
c906108c
SS
9259/* Tell the user we have just set a breakpoint B. */
9260
9261static void
fba45db2 9262mention (struct breakpoint *b)
c906108c 9263{
348d480f 9264 b->ops->print_mention (b);
79a45e25 9265 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9266 return;
c906108c
SS
9267 printf_filtered ("\n");
9268}
c906108c 9269\f
c5aa993b 9270
1a853c52
PA
9271static int bp_loc_is_permanent (struct bp_location *loc);
9272
0d381245 9273static struct bp_location *
39d61571 9274add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9275 const struct symtab_and_line *sal)
9276{
9277 struct bp_location *loc, **tmp;
3742cc8b
YQ
9278 CORE_ADDR adjusted_address;
9279 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9280
9281 if (loc_gdbarch == NULL)
9282 loc_gdbarch = b->gdbarch;
9283
9284 /* Adjust the breakpoint's address prior to allocating a location.
9285 Once we call allocate_bp_location(), that mostly uninitialized
9286 location will be placed on the location chain. Adjustment of the
9287 breakpoint may cause target_read_memory() to be called and we do
9288 not want its scan of the location chain to find a breakpoint and
9289 location that's only been partially initialized. */
9290 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9291 sal->pc, b->type);
0d381245 9292
d30113d4 9293 /* Sort the locations by their ADDRESS. */
39d61571 9294 loc = allocate_bp_location (b);
d30113d4
JK
9295 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9296 tmp = &((*tmp)->next))
0d381245 9297 ;
d30113d4 9298 loc->next = *tmp;
0d381245 9299 *tmp = loc;
3742cc8b 9300
0d381245 9301 loc->requested_address = sal->pc;
3742cc8b 9302 loc->address = adjusted_address;
6c95b8df 9303 loc->pspace = sal->pspace;
729662a5
TT
9304 loc->probe.probe = sal->probe;
9305 loc->probe.objfile = sal->objfile;
6c95b8df 9306 gdb_assert (loc->pspace != NULL);
0d381245 9307 loc->section = sal->section;
3742cc8b 9308 loc->gdbarch = loc_gdbarch;
f8eba3c6 9309 loc->line_number = sal->line;
2f202fde 9310 loc->symtab = sal->symtab;
f8eba3c6 9311
0e30163f
JK
9312 set_breakpoint_location_function (loc,
9313 sal->explicit_pc || sal->explicit_line);
1a853c52
PA
9314
9315 if (bp_loc_is_permanent (loc))
9316 {
9317 loc->inserted = 1;
9318 loc->permanent = 1;
9319 }
9320
0d381245
VP
9321 return loc;
9322}
514f746b
AR
9323\f
9324
1cf4d951 9325/* See breakpoint.h. */
514f746b 9326
1cf4d951
PA
9327int
9328program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9329{
9330 int len;
9331 CORE_ADDR addr;
1afeeb75 9332 const gdb_byte *bpoint;
514f746b 9333 gdb_byte *target_mem;
939c61fa
JK
9334 struct cleanup *cleanup;
9335 int retval = 0;
514f746b 9336
1cf4d951
PA
9337 addr = address;
9338 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9339
9340 /* Software breakpoints unsupported? */
9341 if (bpoint == NULL)
9342 return 0;
9343
9344 target_mem = alloca (len);
9345
9346 /* Enable the automatic memory restoration from breakpoints while
9347 we read the memory. Otherwise we could say about our temporary
9348 breakpoints they are permanent. */
9349 cleanup = make_show_memory_breakpoints_cleanup (0);
9350
9351 if (target_read_memory (address, target_mem, len) == 0
9352 && memcmp (target_mem, bpoint, len) == 0)
9353 retval = 1;
9354
9355 do_cleanups (cleanup);
9356
9357 return retval;
9358}
9359
9360/* Return 1 if LOC is pointing to a permanent breakpoint,
9361 return 0 otherwise. */
9362
9363static int
9364bp_loc_is_permanent (struct bp_location *loc)
9365{
9366 struct cleanup *cleanup;
9367 int retval;
9368
514f746b
AR
9369 gdb_assert (loc != NULL);
9370
e8af5d7a
JB
9371 /* bp_call_dummy breakpoint locations are usually memory locations
9372 where GDB just wrote a breakpoint instruction, making it look
9373 as if there is a permanent breakpoint at that location. Considering
9374 it permanent makes GDB rely on that breakpoint instruction to stop
9375 the program, thus removing the need to insert its own breakpoint
9376 there. This is normally expected to work, except that some versions
9377 of QEMU (Eg: QEMU 2.0.0 for SPARC) just report a fatal problem (Trap
9378 0x02 while interrupts disabled, Error state) instead of reporting
9379 a SIGTRAP. QEMU should probably be fixed, but in the interest of
9380 compatibility with versions that behave this way, we always consider
9381 bp_call_dummy breakpoint locations as non-permanent. */
9382 if (loc->owner->type == bp_call_dummy)
9383 return 0;
9384
6c95b8df 9385 cleanup = save_current_space_and_thread ();
6c95b8df 9386 switch_to_program_space_and_thread (loc->pspace);
939c61fa 9387
1cf4d951 9388 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 9389
939c61fa
JK
9390 do_cleanups (cleanup);
9391
9392 return retval;
514f746b
AR
9393}
9394
e7e0cddf
SS
9395/* Build a command list for the dprintf corresponding to the current
9396 settings of the dprintf style options. */
9397
9398static void
9399update_dprintf_command_list (struct breakpoint *b)
9400{
9401 char *dprintf_args = b->extra_string;
9402 char *printf_line = NULL;
9403
9404 if (!dprintf_args)
9405 return;
9406
9407 dprintf_args = skip_spaces (dprintf_args);
9408
9409 /* Allow a comma, as it may have terminated a location, but don't
9410 insist on it. */
9411 if (*dprintf_args == ',')
9412 ++dprintf_args;
9413 dprintf_args = skip_spaces (dprintf_args);
9414
9415 if (*dprintf_args != '"')
9416 error (_("Bad format string, missing '\"'."));
9417
d3ce09f5 9418 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9419 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9420 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9421 {
9422 if (!dprintf_function)
9423 error (_("No function supplied for dprintf call"));
9424
9425 if (dprintf_channel && strlen (dprintf_channel) > 0)
9426 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9427 dprintf_function,
9428 dprintf_channel,
9429 dprintf_args);
9430 else
9431 printf_line = xstrprintf ("call (void) %s (%s)",
9432 dprintf_function,
9433 dprintf_args);
9434 }
d3ce09f5
SS
9435 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9436 {
9437 if (target_can_run_breakpoint_commands ())
9438 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9439 else
9440 {
9441 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9442 printf_line = xstrprintf ("printf %s", dprintf_args);
9443 }
9444 }
e7e0cddf
SS
9445 else
9446 internal_error (__FILE__, __LINE__,
9447 _("Invalid dprintf style."));
9448
f28045c2 9449 gdb_assert (printf_line != NULL);
9d6e6e84 9450 /* Manufacture a printf sequence. */
f28045c2 9451 {
9d6e6e84
HZ
9452 struct command_line *printf_cmd_line
9453 = xmalloc (sizeof (struct command_line));
e7e0cddf 9454
f28045c2
YQ
9455 printf_cmd_line = xmalloc (sizeof (struct command_line));
9456 printf_cmd_line->control_type = simple_control;
9457 printf_cmd_line->body_count = 0;
9458 printf_cmd_line->body_list = NULL;
9d6e6e84 9459 printf_cmd_line->next = NULL;
f28045c2 9460 printf_cmd_line->line = printf_line;
e7e0cddf 9461
f28045c2
YQ
9462 breakpoint_set_commands (b, printf_cmd_line);
9463 }
e7e0cddf
SS
9464}
9465
9466/* Update all dprintf commands, making their command lists reflect
9467 current style settings. */
9468
9469static void
9470update_dprintf_commands (char *args, int from_tty,
9471 struct cmd_list_element *c)
9472{
9473 struct breakpoint *b;
9474
9475 ALL_BREAKPOINTS (b)
9476 {
9477 if (b->type == bp_dprintf)
9478 update_dprintf_command_list (b);
9479 }
9480}
c3f6f71d 9481
018d34a4
VP
9482/* Create a breakpoint with SAL as location. Use ADDR_STRING
9483 as textual description of the location, and COND_STRING
db107f19 9484 as condition expression. */
018d34a4
VP
9485
9486static void
d9b3f62e
PA
9487init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9488 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9489 char *filter, char *cond_string,
e7e0cddf 9490 char *extra_string,
d9b3f62e
PA
9491 enum bptype type, enum bpdisp disposition,
9492 int thread, int task, int ignore_count,
c0a91b2b 9493 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9494 int enabled, int internal, unsigned flags,
9495 int display_canonical)
018d34a4 9496{
0d381245 9497 int i;
018d34a4
VP
9498
9499 if (type == bp_hardware_breakpoint)
9500 {
fbbd034e
AS
9501 int target_resources_ok;
9502
9503 i = hw_breakpoint_used_count ();
9504 target_resources_ok =
9505 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9506 i + 1, 0);
9507 if (target_resources_ok == 0)
9508 error (_("No hardware breakpoint support in the target."));
9509 else if (target_resources_ok < 0)
9510 error (_("Hardware breakpoints used exceeds limit."));
9511 }
9512
6c95b8df
PA
9513 gdb_assert (sals.nelts > 0);
9514
0d381245
VP
9515 for (i = 0; i < sals.nelts; ++i)
9516 {
9517 struct symtab_and_line sal = sals.sals[i];
9518 struct bp_location *loc;
9519
9520 if (from_tty)
5af949e3
UW
9521 {
9522 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9523 if (!loc_gdbarch)
9524 loc_gdbarch = gdbarch;
9525
9526 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9527 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9528 }
0d381245
VP
9529
9530 if (i == 0)
9531 {
d9b3f62e 9532 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9533 b->thread = thread;
4a306c9a 9534 b->task = task;
855a6e68 9535
0d381245 9536 b->cond_string = cond_string;
e7e0cddf 9537 b->extra_string = extra_string;
0d381245 9538 b->ignore_count = ignore_count;
41447f92 9539 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9540 b->disposition = disposition;
6c95b8df 9541
44f238bb
PA
9542 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9543 b->loc->inserted = 1;
9544
0fb4aa4b
PA
9545 if (type == bp_static_tracepoint)
9546 {
d9b3f62e 9547 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9548 struct static_tracepoint_marker marker;
9549
983af33b 9550 if (strace_marker_p (b))
0fb4aa4b
PA
9551 {
9552 /* We already know the marker exists, otherwise, we
9553 wouldn't see a sal for it. */
9554 char *p = &addr_string[3];
9555 char *endp;
9556 char *marker_str;
0fb4aa4b 9557
e9cafbcc 9558 p = skip_spaces (p);
0fb4aa4b 9559
e9cafbcc 9560 endp = skip_to_space (p);
0fb4aa4b
PA
9561
9562 marker_str = savestring (p, endp - p);
d9b3f62e 9563 t->static_trace_marker_id = marker_str;
0fb4aa4b 9564
3e43a32a
MS
9565 printf_filtered (_("Probed static tracepoint "
9566 "marker \"%s\"\n"),
d9b3f62e 9567 t->static_trace_marker_id);
0fb4aa4b
PA
9568 }
9569 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9570 {
d9b3f62e 9571 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9572 release_static_tracepoint_marker (&marker);
9573
3e43a32a
MS
9574 printf_filtered (_("Probed static tracepoint "
9575 "marker \"%s\"\n"),
d9b3f62e 9576 t->static_trace_marker_id);
0fb4aa4b
PA
9577 }
9578 else
3e43a32a
MS
9579 warning (_("Couldn't determine the static "
9580 "tracepoint marker to probe"));
0fb4aa4b
PA
9581 }
9582
0d381245
VP
9583 loc = b->loc;
9584 }
9585 else
018d34a4 9586 {
39d61571 9587 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9588 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9589 loc->inserted = 1;
0d381245
VP
9590 }
9591
9592 if (b->cond_string)
9593 {
bbc13ae3
KS
9594 const char *arg = b->cond_string;
9595
1bb9788d
TT
9596 loc->cond = parse_exp_1 (&arg, loc->address,
9597 block_for_pc (loc->address), 0);
0d381245 9598 if (*arg)
588ae58c 9599 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9600 }
e7e0cddf
SS
9601
9602 /* Dynamic printf requires and uses additional arguments on the
9603 command line, otherwise it's an error. */
9604 if (type == bp_dprintf)
9605 {
9606 if (b->extra_string)
9607 update_dprintf_command_list (b);
9608 else
9609 error (_("Format string required"));
9610 }
9611 else if (b->extra_string)
588ae58c 9612 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9613 }
018d34a4 9614
56435ebe 9615 b->display_canonical = display_canonical;
018d34a4
VP
9616 if (addr_string)
9617 b->addr_string = addr_string;
9618 else
9619 /* addr_string has to be used or breakpoint_re_set will delete
9620 me. */
5af949e3
UW
9621 b->addr_string
9622 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9623 b->filter = filter;
d9b3f62e 9624}
018d34a4 9625
d9b3f62e
PA
9626static void
9627create_breakpoint_sal (struct gdbarch *gdbarch,
9628 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9629 char *filter, char *cond_string,
e7e0cddf 9630 char *extra_string,
d9b3f62e
PA
9631 enum bptype type, enum bpdisp disposition,
9632 int thread, int task, int ignore_count,
c0a91b2b 9633 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9634 int enabled, int internal, unsigned flags,
9635 int display_canonical)
d9b3f62e
PA
9636{
9637 struct breakpoint *b;
9638 struct cleanup *old_chain;
9639
9640 if (is_tracepoint_type (type))
9641 {
9642 struct tracepoint *t;
9643
9644 t = XCNEW (struct tracepoint);
9645 b = &t->base;
9646 }
9647 else
9648 b = XNEW (struct breakpoint);
9649
9650 old_chain = make_cleanup (xfree, b);
9651
9652 init_breakpoint_sal (b, gdbarch,
9653 sals, addr_string,
e7e0cddf 9654 filter, cond_string, extra_string,
d9b3f62e
PA
9655 type, disposition,
9656 thread, task, ignore_count,
9657 ops, from_tty,
44f238bb
PA
9658 enabled, internal, flags,
9659 display_canonical);
d9b3f62e
PA
9660 discard_cleanups (old_chain);
9661
3ea46bff 9662 install_breakpoint (internal, b, 0);
018d34a4
VP
9663}
9664
9665/* Add SALS.nelts breakpoints to the breakpoint table. For each
9666 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9667 value. COND_STRING, if not NULL, specified the condition to be
9668 used for all breakpoints. Essentially the only case where
9669 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9670 function. In that case, it's still not possible to specify
9671 separate conditions for different overloaded functions, so
9672 we take just a single condition string.
9673
c3f6f71d 9674 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9675 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9676 array contents). If the function fails (error() is called), the
9677 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9678 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9679
9680static void
8cdf0e15 9681create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9682 struct linespec_result *canonical,
e7e0cddf 9683 char *cond_string, char *extra_string,
8cdf0e15
VP
9684 enum bptype type, enum bpdisp disposition,
9685 int thread, int task, int ignore_count,
c0a91b2b 9686 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9687 int enabled, int internal, unsigned flags)
c906108c 9688{
018d34a4 9689 int i;
f8eba3c6 9690 struct linespec_sals *lsal;
cc59ec59 9691
f8eba3c6
TT
9692 if (canonical->pre_expanded)
9693 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9694
9695 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9696 {
f8eba3c6
TT
9697 /* Note that 'addr_string' can be NULL in the case of a plain
9698 'break', without arguments. */
9699 char *addr_string = (canonical->addr_string
9700 ? xstrdup (canonical->addr_string)
9701 : NULL);
9702 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9703 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9704
f8eba3c6
TT
9705 make_cleanup (xfree, filter_string);
9706 create_breakpoint_sal (gdbarch, lsal->sals,
9707 addr_string,
9708 filter_string,
e7e0cddf
SS
9709 cond_string, extra_string,
9710 type, disposition,
84f4c1fe 9711 thread, task, ignore_count, ops,
44f238bb 9712 from_tty, enabled, internal, flags,
56435ebe 9713 canonical->special_display);
f8eba3c6 9714 discard_cleanups (inner);
c3f6f71d 9715 }
c3f6f71d 9716}
c906108c 9717
9998af43 9718/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9719 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9720 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9721 address strings. ADDRESS points to the end of the SAL.
9722
9723 The array and the line spec strings are allocated on the heap, it is
9724 the caller's responsibility to free them. */
c906108c 9725
b9362cc7 9726static void
c3f6f71d 9727parse_breakpoint_sals (char **address,
58438ac1 9728 struct linespec_result *canonical)
c3f6f71d 9729{
c3f6f71d 9730 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9731 breakpoint. */
c3f6f71d 9732 if ((*address) == NULL
61012eef 9733 || (startswith ((*address), "if") && isspace ((*address)[2])))
c906108c 9734 {
1bfeeb0f
JL
9735 /* The last displayed codepoint, if it's valid, is our default breakpoint
9736 address. */
9737 if (last_displayed_sal_is_valid ())
c906108c 9738 {
f8eba3c6 9739 struct linespec_sals lsal;
c3f6f71d 9740 struct symtab_and_line sal;
1c8cdcb1 9741 CORE_ADDR pc;
cc59ec59 9742
4a64f543 9743 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9744 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9745 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9746
9747 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9748 corresponding to the last call to print_frame_info.
9749 Be sure to reinitialize LINE with NOTCURRENT == 0
9750 as the breakpoint line number is inappropriate otherwise.
9751 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9752 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9753 pc = sal.pc;
9754 sal = find_pc_line (pc, 0);
00903456 9755
4a64f543 9756 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9757 where PC is the last displayed codepoint's address. So
9758 make sure to set sal.explicit_pc to prevent GDB from
9759 trying to expand the list of sals to include all other
9760 instances with the same symtab and line. */
1c8cdcb1 9761 sal.pc = pc;
00903456
JK
9762 sal.explicit_pc = 1;
9763
f8eba3c6
TT
9764 lsal.sals.sals[0] = sal;
9765 lsal.sals.nelts = 1;
9766 lsal.canonical = NULL;
9767
9768 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9769 }
9770 else
8a3fe4f8 9771 error (_("No default breakpoint address now."));
c906108c
SS
9772 }
9773 else
9774 {
cc80f267
JK
9775 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9776
c906108c 9777 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9778 current_source_symtab (which is decode_line_1's default).
9779 This should produce the results we want almost all of the
cc80f267
JK
9780 time while leaving default_breakpoint_* alone.
9781
9782 ObjC: However, don't match an Objective-C method name which
9783 may have a '+' or '-' succeeded by a '['. */
9784 if (last_displayed_sal_is_valid ()
9785 && (!cursal.symtab
9786 || ((strchr ("+-", (*address)[0]) != NULL)
9787 && ((*address)[1] != '['))))
f8eba3c6
TT
9788 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9789 get_last_displayed_symtab (),
9790 get_last_displayed_line (),
9791 canonical, NULL, NULL);
c906108c 9792 else
f8eba3c6 9793 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9794 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9795 }
c3f6f71d 9796}
c906108c 9797
c906108c 9798
c3f6f71d 9799/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9800 inserted as a breakpoint. If it can't throw an error. */
c906108c 9801
b9362cc7 9802static void
23e7acfb 9803breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9804{
9805 int i;
cc59ec59 9806
c3f6f71d 9807 for (i = 0; i < sals->nelts; i++)
ee53e872 9808 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9809}
9810
7a697b8d
SS
9811/* Fast tracepoints may have restrictions on valid locations. For
9812 instance, a fast tracepoint using a jump instead of a trap will
9813 likely have to overwrite more bytes than a trap would, and so can
9814 only be placed where the instruction is longer than the jump, or a
9815 multi-instruction sequence does not have a jump into the middle of
9816 it, etc. */
9817
9818static void
9819check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9820 struct symtabs_and_lines *sals)
9821{
9822 int i, rslt;
9823 struct symtab_and_line *sal;
9824 char *msg;
9825 struct cleanup *old_chain;
9826
9827 for (i = 0; i < sals->nelts; i++)
9828 {
f8eba3c6
TT
9829 struct gdbarch *sarch;
9830
7a697b8d
SS
9831 sal = &sals->sals[i];
9832
f8eba3c6
TT
9833 sarch = get_sal_arch (*sal);
9834 /* We fall back to GDBARCH if there is no architecture
9835 associated with SAL. */
9836 if (sarch == NULL)
9837 sarch = gdbarch;
9838 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9839 NULL, &msg);
9840 old_chain = make_cleanup (xfree, msg);
9841
9842 if (!rslt)
9843 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9844 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9845
9846 do_cleanups (old_chain);
9847 }
9848}
9849
af4908ba
KS
9850/* Issue an invalid thread ID error. */
9851
9852static void ATTRIBUTE_NORETURN
9853invalid_thread_id_error (int id)
9854{
9855 error (_("Unknown thread %d."), id);
9856}
9857
018d34a4
VP
9858/* Given TOK, a string specification of condition and thread, as
9859 accepted by the 'break' command, extract the condition
9860 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9861 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9862 If no condition is found, *COND_STRING is set to NULL.
9863 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9864
9865static void
bbc13ae3 9866find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9867 char **cond_string, int *thread, int *task,
9868 char **rest)
018d34a4
VP
9869{
9870 *cond_string = NULL;
9871 *thread = -1;
ed1d1739
KS
9872 *task = 0;
9873 *rest = NULL;
9874
018d34a4
VP
9875 while (tok && *tok)
9876 {
bbc13ae3 9877 const char *end_tok;
018d34a4 9878 int toklen;
bbc13ae3
KS
9879 const char *cond_start = NULL;
9880 const char *cond_end = NULL;
cc59ec59 9881
bbc13ae3 9882 tok = skip_spaces_const (tok);
e7e0cddf
SS
9883
9884 if ((*tok == '"' || *tok == ',') && rest)
9885 {
9886 *rest = savestring (tok, strlen (tok));
9887 return;
9888 }
9889
bbc13ae3 9890 end_tok = skip_to_space_const (tok);
d634f2de 9891
018d34a4 9892 toklen = end_tok - tok;
d634f2de 9893
018d34a4
VP
9894 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9895 {
f7545552
TT
9896 struct expression *expr;
9897
018d34a4 9898 tok = cond_start = end_tok + 1;
1bb9788d 9899 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9900 xfree (expr);
018d34a4 9901 cond_end = tok;
d634f2de 9902 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9903 }
9904 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9905 {
9906 char *tmptok;
d634f2de 9907
018d34a4 9908 tok = end_tok + 1;
bbc13ae3 9909 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9910 if (tok == tmptok)
9911 error (_("Junk after thread keyword."));
9912 if (!valid_thread_id (*thread))
af4908ba 9913 invalid_thread_id_error (*thread);
bbc13ae3 9914 tok = tmptok;
018d34a4 9915 }
4a306c9a
JB
9916 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9917 {
9918 char *tmptok;
9919
9920 tok = end_tok + 1;
bbc13ae3 9921 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9922 if (tok == tmptok)
9923 error (_("Junk after task keyword."));
9924 if (!valid_task_id (*task))
b6199126 9925 error (_("Unknown task %d."), *task);
bbc13ae3 9926 tok = tmptok;
4a306c9a 9927 }
e7e0cddf
SS
9928 else if (rest)
9929 {
9930 *rest = savestring (tok, strlen (tok));
ccab2054 9931 return;
e7e0cddf 9932 }
018d34a4
VP
9933 else
9934 error (_("Junk at end of arguments."));
9935 }
9936}
9937
0fb4aa4b
PA
9938/* Decode a static tracepoint marker spec. */
9939
9940static struct symtabs_and_lines
9941decode_static_tracepoint_spec (char **arg_p)
9942{
9943 VEC(static_tracepoint_marker_p) *markers = NULL;
9944 struct symtabs_and_lines sals;
0fb4aa4b
PA
9945 struct cleanup *old_chain;
9946 char *p = &(*arg_p)[3];
9947 char *endp;
9948 char *marker_str;
9949 int i;
9950
e9cafbcc 9951 p = skip_spaces (p);
0fb4aa4b 9952
e9cafbcc 9953 endp = skip_to_space (p);
0fb4aa4b
PA
9954
9955 marker_str = savestring (p, endp - p);
9956 old_chain = make_cleanup (xfree, marker_str);
9957
9958 markers = target_static_tracepoint_markers_by_strid (marker_str);
9959 if (VEC_empty(static_tracepoint_marker_p, markers))
9960 error (_("No known static tracepoint marker named %s"), marker_str);
9961
9962 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9963 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9964
9965 for (i = 0; i < sals.nelts; i++)
9966 {
9967 struct static_tracepoint_marker *marker;
9968
9969 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9970
9971 init_sal (&sals.sals[i]);
9972
9973 sals.sals[i] = find_pc_line (marker->address, 0);
9974 sals.sals[i].pc = marker->address;
9975
9976 release_static_tracepoint_marker (marker);
9977 }
9978
9979 do_cleanups (old_chain);
9980
9981 *arg_p = endp;
9982 return sals;
9983}
9984
fd9b8c24
PA
9985/* Set a breakpoint. This function is shared between CLI and MI
9986 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9987 modes of operations, selected by the PARSE_ARG parameter. If
9988 non-zero, the function will parse ARG, extracting location,
9989 condition, thread and extra string. Otherwise, ARG is just the
9990 breakpoint's location, with condition, thread, and extra string
9991 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9992 If INTERNAL is non-zero, the breakpoint number will be allocated
9993 from the internal breakpoint count. Returns true if any breakpoint
9994 was created; false otherwise. */
0101ce28 9995
8cdf0e15
VP
9996int
9997create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9998 char *arg, char *cond_string,
9999 int thread, char *extra_string,
f6de8ec2 10000 int parse_arg,
0fb4aa4b 10001 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
10002 int ignore_count,
10003 enum auto_boolean pending_break_support,
c0a91b2b 10004 const struct breakpoint_ops *ops,
44f238bb
PA
10005 int from_tty, int enabled, int internal,
10006 unsigned flags)
c3f6f71d 10007{
f8eba3c6 10008 char *copy_arg = NULL;
c3f6f71d 10009 char *addr_start = arg;
7efd8fc2 10010 struct linespec_result canonical;
c3f6f71d 10011 struct cleanup *old_chain;
80c99de1 10012 struct cleanup *bkpt_chain = NULL;
0101ce28 10013 int pending = 0;
4a306c9a 10014 int task = 0;
86b17b60 10015 int prev_bkpt_count = breakpoint_count;
c3f6f71d 10016
348d480f
PA
10017 gdb_assert (ops != NULL);
10018
7efd8fc2 10019 init_linespec_result (&canonical);
c3f6f71d 10020
492d29ea 10021 TRY
b78a6381 10022 {
983af33b
SDJ
10023 ops->create_sals_from_address (&arg, &canonical, type_wanted,
10024 addr_start, &copy_arg);
b78a6381 10025 }
492d29ea 10026 CATCH (e, RETURN_MASK_ERROR)
0101ce28 10027 {
492d29ea
PA
10028 /* If caller is interested in rc value from parse, set
10029 value. */
10030 if (e.error == NOT_FOUND_ERROR)
0101ce28 10031 {
05ff989b
AC
10032 /* If pending breakpoint support is turned off, throw
10033 error. */
fa8d40ab
JJ
10034
10035 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
10036 throw_exception (e);
10037
10038 exception_print (gdb_stderr, e);
fa8d40ab 10039
05ff989b
AC
10040 /* If pending breakpoint support is auto query and the user
10041 selects no, then simply return the error code. */
059fb39f 10042 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
10043 && !nquery (_("Make %s pending on future shared library load? "),
10044 bptype_string (type_wanted)))
fd9b8c24 10045 return 0;
fa8d40ab 10046
05ff989b
AC
10047 /* At this point, either the user was queried about setting
10048 a pending breakpoint and selected yes, or pending
10049 breakpoint behavior is on and thus a pending breakpoint
10050 is defaulted on behalf of the user. */
f8eba3c6
TT
10051 {
10052 struct linespec_sals lsal;
10053
10054 copy_arg = xstrdup (addr_start);
10055 lsal.canonical = xstrdup (copy_arg);
10056 lsal.sals.nelts = 1;
10057 lsal.sals.sals = XNEW (struct symtab_and_line);
10058 init_sal (&lsal.sals.sals[0]);
10059 pending = 1;
10060 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
10061 }
0101ce28 10062 }
492d29ea
PA
10063 else
10064 throw_exception (e);
0101ce28 10065 }
492d29ea
PA
10066 END_CATCH
10067
10068 if (VEC_empty (linespec_sals, canonical.sals))
10069 return 0;
c3f6f71d 10070
4a64f543 10071 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 10072 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 10073
c3f6f71d
JM
10074 /* ----------------------------- SNIP -----------------------------
10075 Anything added to the cleanup chain beyond this point is assumed
10076 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
10077 then the memory is not reclaimed. */
10078 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 10079
c3f6f71d
JM
10080 /* Resolve all line numbers to PC's and verify that the addresses
10081 are ok for the target. */
0101ce28 10082 if (!pending)
f8eba3c6
TT
10083 {
10084 int ix;
10085 struct linespec_sals *iter;
10086
10087 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10088 breakpoint_sals_to_pc (&iter->sals);
10089 }
c3f6f71d 10090
7a697b8d 10091 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 10092 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
10093 {
10094 int ix;
10095 struct linespec_sals *iter;
10096
10097 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10098 check_fast_tracepoint_sals (gdbarch, &iter->sals);
10099 }
7a697b8d 10100
c3f6f71d
JM
10101 /* Verify that condition can be parsed, before setting any
10102 breakpoints. Allocate a separate condition expression for each
4a64f543 10103 breakpoint. */
0101ce28 10104 if (!pending)
c3f6f71d 10105 {
f6de8ec2 10106 if (parse_arg)
72b2ff0e 10107 {
0878d0fa 10108 char *rest;
52d361e1
YQ
10109 struct linespec_sals *lsal;
10110
10111 lsal = VEC_index (linespec_sals, canonical.sals, 0);
10112
0878d0fa
YQ
10113 /* Here we only parse 'arg' to separate condition
10114 from thread number, so parsing in context of first
10115 sal is OK. When setting the breakpoint we'll
10116 re-parse it in context of each sal. */
10117
10118 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
10119 &thread, &task, &rest);
10120 if (cond_string)
10121 make_cleanup (xfree, cond_string);
10122 if (rest)
10123 make_cleanup (xfree, rest);
10124 if (rest)
10125 extra_string = rest;
72b2ff0e 10126 }
2f069f6f 10127 else
72b2ff0e 10128 {
0878d0fa
YQ
10129 if (*arg != '\0')
10130 error (_("Garbage '%s' at end of location"), arg);
10131
10132 /* Create a private copy of condition string. */
10133 if (cond_string)
10134 {
10135 cond_string = xstrdup (cond_string);
10136 make_cleanup (xfree, cond_string);
10137 }
10138 /* Create a private copy of any extra string. */
10139 if (extra_string)
10140 {
10141 extra_string = xstrdup (extra_string);
10142 make_cleanup (xfree, extra_string);
10143 }
72b2ff0e 10144 }
0fb4aa4b 10145
52d361e1 10146 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 10147 cond_string, extra_string, type_wanted,
d9b3f62e
PA
10148 tempflag ? disp_del : disp_donttouch,
10149 thread, task, ignore_count, ops,
44f238bb 10150 from_tty, enabled, internal, flags);
c906108c 10151 }
0101ce28
JJ
10152 else
10153 {
0101ce28
JJ
10154 struct breakpoint *b;
10155
0101ce28
JJ
10156 make_cleanup (xfree, copy_arg);
10157
bfccc43c
YQ
10158 if (is_tracepoint_type (type_wanted))
10159 {
10160 struct tracepoint *t;
10161
10162 t = XCNEW (struct tracepoint);
10163 b = &t->base;
10164 }
10165 else
10166 b = XNEW (struct breakpoint);
10167
10168 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10169
f8eba3c6 10170 b->addr_string = copy_arg;
f6de8ec2 10171 if (parse_arg)
e12c7713
MK
10172 b->cond_string = NULL;
10173 else
10174 {
10175 /* Create a private copy of condition string. */
10176 if (cond_string)
10177 {
10178 cond_string = xstrdup (cond_string);
10179 make_cleanup (xfree, cond_string);
10180 }
10181 b->cond_string = cond_string;
10182 }
e7e0cddf 10183 b->extra_string = NULL;
0101ce28 10184 b->ignore_count = ignore_count;
0101ce28 10185 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 10186 b->condition_not_parsed = 1;
41447f92 10187 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
10188 if ((type_wanted != bp_breakpoint
10189 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 10190 b->pspace = current_program_space;
8bea4e01 10191
bfccc43c 10192 install_breakpoint (internal, b, 0);
0101ce28
JJ
10193 }
10194
f8eba3c6 10195 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 10196 {
3e43a32a
MS
10197 warning (_("Multiple breakpoints were set.\nUse the "
10198 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 10199 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
10200 }
10201
80c99de1
PA
10202 /* That's it. Discard the cleanups for data inserted into the
10203 breakpoint. */
10204 discard_cleanups (bkpt_chain);
10205 /* But cleanup everything else. */
c3f6f71d 10206 do_cleanups (old_chain);
217dc9e2 10207
80c99de1 10208 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 10209 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
10210
10211 return 1;
c3f6f71d 10212}
c906108c 10213
348d480f 10214/* Set a breakpoint.
72b2ff0e
VP
10215 ARG is a string describing breakpoint address,
10216 condition, and thread.
10217 FLAG specifies if a breakpoint is hardware on,
10218 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10219 and BP_TEMPFLAG. */
348d480f 10220
98deb0da 10221static void
72b2ff0e 10222break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10223{
72b2ff0e 10224 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10225 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10226 ? bp_hardware_breakpoint
10227 : bp_breakpoint);
55aa24fb
SDJ
10228 struct breakpoint_ops *ops;
10229 const char *arg_cp = arg;
10230
10231 /* Matching breakpoints on probes. */
10232 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10233 ops = &bkpt_probe_breakpoint_ops;
10234 else
10235 ops = &bkpt_breakpoint_ops;
c3f6f71d 10236
8cdf0e15
VP
10237 create_breakpoint (get_current_arch (),
10238 arg,
e7e0cddf 10239 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 10240 tempflag, type_wanted,
8cdf0e15
VP
10241 0 /* Ignore count */,
10242 pending_break_support,
55aa24fb 10243 ops,
8cdf0e15 10244 from_tty,
84f4c1fe 10245 1 /* enabled */,
44f238bb
PA
10246 0 /* internal */,
10247 0);
c906108c
SS
10248}
10249
c906108c
SS
10250/* Helper function for break_command_1 and disassemble_command. */
10251
10252void
fba45db2 10253resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10254{
10255 CORE_ADDR pc;
10256
10257 if (sal->pc == 0 && sal->symtab != NULL)
10258 {
10259 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10260 error (_("No line %d in file \"%s\"."),
05cba821 10261 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10262 sal->pc = pc;
6a048695 10263
4a64f543
MS
10264 /* If this SAL corresponds to a breakpoint inserted using a line
10265 number, then skip the function prologue if necessary. */
6a048695 10266 if (sal->explicit_line)
059acae7 10267 skip_prologue_sal (sal);
c906108c
SS
10268 }
10269
10270 if (sal->section == 0 && sal->symtab != NULL)
10271 {
346d1dfe 10272 const struct blockvector *bv;
3977b71f 10273 const struct block *b;
c5aa993b 10274 struct symbol *sym;
c906108c 10275
43f3e411
DE
10276 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10277 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
10278 if (bv != NULL)
10279 {
7f0df278 10280 sym = block_linkage_function (b);
c906108c
SS
10281 if (sym != NULL)
10282 {
eb822aa6
DE
10283 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10284 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10285 sym);
c906108c
SS
10286 }
10287 else
10288 {
4a64f543
MS
10289 /* It really is worthwhile to have the section, so we'll
10290 just have to look harder. This case can be executed
10291 if we have line numbers but no functions (as can
10292 happen in assembly source). */
c906108c 10293
7cbd4a93 10294 struct bound_minimal_symbol msym;
6c95b8df
PA
10295 struct cleanup *old_chain = save_current_space_and_thread ();
10296
10297 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10298
10299 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10300 if (msym.minsym)
efd66ac6 10301 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10302
10303 do_cleanups (old_chain);
c906108c
SS
10304 }
10305 }
10306 }
10307}
10308
10309void
fba45db2 10310break_command (char *arg, int from_tty)
c906108c 10311{
db107f19 10312 break_command_1 (arg, 0, from_tty);
c906108c
SS
10313}
10314
c906108c 10315void
fba45db2 10316tbreak_command (char *arg, int from_tty)
c906108c 10317{
db107f19 10318 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10319}
10320
c906108c 10321static void
fba45db2 10322hbreak_command (char *arg, int from_tty)
c906108c 10323{
db107f19 10324 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10325}
10326
10327static void
fba45db2 10328thbreak_command (char *arg, int from_tty)
c906108c 10329{
db107f19 10330 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10331}
10332
10333static void
fba45db2 10334stop_command (char *arg, int from_tty)
c906108c 10335{
a3f17187 10336 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10337Usage: stop in <function | address>\n\
a3f17187 10338 stop at <line>\n"));
c906108c
SS
10339}
10340
10341static void
fba45db2 10342stopin_command (char *arg, int from_tty)
c906108c
SS
10343{
10344 int badInput = 0;
10345
c5aa993b 10346 if (arg == (char *) NULL)
c906108c
SS
10347 badInput = 1;
10348 else if (*arg != '*')
10349 {
10350 char *argptr = arg;
10351 int hasColon = 0;
10352
4a64f543 10353 /* Look for a ':'. If this is a line number specification, then
53a5351d 10354 say it is bad, otherwise, it should be an address or
4a64f543 10355 function/method name. */
c906108c 10356 while (*argptr && !hasColon)
c5aa993b
JM
10357 {
10358 hasColon = (*argptr == ':');
10359 argptr++;
10360 }
c906108c
SS
10361
10362 if (hasColon)
c5aa993b 10363 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10364 else
c5aa993b 10365 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10366 }
10367
10368 if (badInput)
a3f17187 10369 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10370 else
db107f19 10371 break_command_1 (arg, 0, from_tty);
c906108c
SS
10372}
10373
10374static void
fba45db2 10375stopat_command (char *arg, int from_tty)
c906108c
SS
10376{
10377 int badInput = 0;
10378
c5aa993b 10379 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10380 badInput = 1;
10381 else
10382 {
10383 char *argptr = arg;
10384 int hasColon = 0;
10385
4a64f543
MS
10386 /* Look for a ':'. If there is a '::' then get out, otherwise
10387 it is probably a line number. */
c906108c 10388 while (*argptr && !hasColon)
c5aa993b
JM
10389 {
10390 hasColon = (*argptr == ':');
10391 argptr++;
10392 }
c906108c
SS
10393
10394 if (hasColon)
c5aa993b 10395 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10396 else
c5aa993b 10397 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10398 }
10399
10400 if (badInput)
a3f17187 10401 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10402 else
db107f19 10403 break_command_1 (arg, 0, from_tty);
c906108c
SS
10404}
10405
e7e0cddf
SS
10406/* The dynamic printf command is mostly like a regular breakpoint, but
10407 with a prewired command list consisting of a single output command,
10408 built from extra arguments supplied on the dprintf command
10409 line. */
10410
da821c7b 10411static void
e7e0cddf
SS
10412dprintf_command (char *arg, int from_tty)
10413{
10414 create_breakpoint (get_current_arch (),
10415 arg,
10416 NULL, 0, NULL, 1 /* parse arg */,
10417 0, bp_dprintf,
10418 0 /* Ignore count */,
10419 pending_break_support,
10420 &dprintf_breakpoint_ops,
10421 from_tty,
10422 1 /* enabled */,
10423 0 /* internal */,
10424 0);
10425}
10426
d3ce09f5
SS
10427static void
10428agent_printf_command (char *arg, int from_tty)
10429{
10430 error (_("May only run agent-printf on the target"));
10431}
10432
f1310107
TJB
10433/* Implement the "breakpoint_hit" breakpoint_ops method for
10434 ranged breakpoints. */
10435
10436static int
10437breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10438 struct address_space *aspace,
09ac7c10
TT
10439 CORE_ADDR bp_addr,
10440 const struct target_waitstatus *ws)
f1310107 10441{
09ac7c10 10442 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10443 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10444 return 0;
10445
f1310107
TJB
10446 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10447 bl->length, aspace, bp_addr);
10448}
10449
10450/* Implement the "resources_needed" breakpoint_ops method for
10451 ranged breakpoints. */
10452
10453static int
10454resources_needed_ranged_breakpoint (const struct bp_location *bl)
10455{
10456 return target_ranged_break_num_registers ();
10457}
10458
10459/* Implement the "print_it" breakpoint_ops method for
10460 ranged breakpoints. */
10461
10462static enum print_stop_action
348d480f 10463print_it_ranged_breakpoint (bpstat bs)
f1310107 10464{
348d480f 10465 struct breakpoint *b = bs->breakpoint_at;
f1310107 10466 struct bp_location *bl = b->loc;
79a45e25 10467 struct ui_out *uiout = current_uiout;
f1310107
TJB
10468
10469 gdb_assert (b->type == bp_hardware_breakpoint);
10470
10471 /* Ranged breakpoints have only one location. */
10472 gdb_assert (bl && bl->next == NULL);
10473
10474 annotate_breakpoint (b->number);
10475 if (b->disposition == disp_del)
10476 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10477 else
10478 ui_out_text (uiout, "\nRanged breakpoint ");
10479 if (ui_out_is_mi_like_p (uiout))
10480 {
10481 ui_out_field_string (uiout, "reason",
10482 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10483 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10484 }
10485 ui_out_field_int (uiout, "bkptno", b->number);
10486 ui_out_text (uiout, ", ");
10487
10488 return PRINT_SRC_AND_LOC;
10489}
10490
10491/* Implement the "print_one" breakpoint_ops method for
10492 ranged breakpoints. */
10493
10494static void
10495print_one_ranged_breakpoint (struct breakpoint *b,
10496 struct bp_location **last_loc)
10497{
10498 struct bp_location *bl = b->loc;
10499 struct value_print_options opts;
79a45e25 10500 struct ui_out *uiout = current_uiout;
f1310107
TJB
10501
10502 /* Ranged breakpoints have only one location. */
10503 gdb_assert (bl && bl->next == NULL);
10504
10505 get_user_print_options (&opts);
10506
10507 if (opts.addressprint)
10508 /* We don't print the address range here, it will be printed later
10509 by print_one_detail_ranged_breakpoint. */
10510 ui_out_field_skip (uiout, "addr");
10511 annotate_field (5);
10512 print_breakpoint_location (b, bl);
10513 *last_loc = bl;
10514}
10515
10516/* Implement the "print_one_detail" breakpoint_ops method for
10517 ranged breakpoints. */
10518
10519static void
10520print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10521 struct ui_out *uiout)
10522{
10523 CORE_ADDR address_start, address_end;
10524 struct bp_location *bl = b->loc;
f99d8bf4
PA
10525 struct ui_file *stb = mem_fileopen ();
10526 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10527
10528 gdb_assert (bl);
10529
10530 address_start = bl->address;
10531 address_end = address_start + bl->length - 1;
10532
10533 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10534 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10535 print_core_address (bl->gdbarch, address_start),
10536 print_core_address (bl->gdbarch, address_end));
10537 ui_out_field_stream (uiout, "addr", stb);
10538 ui_out_text (uiout, "\n");
10539
10540 do_cleanups (cleanup);
10541}
10542
10543/* Implement the "print_mention" breakpoint_ops method for
10544 ranged breakpoints. */
10545
10546static void
10547print_mention_ranged_breakpoint (struct breakpoint *b)
10548{
10549 struct bp_location *bl = b->loc;
79a45e25 10550 struct ui_out *uiout = current_uiout;
f1310107
TJB
10551
10552 gdb_assert (bl);
10553 gdb_assert (b->type == bp_hardware_breakpoint);
10554
10555 if (ui_out_is_mi_like_p (uiout))
10556 return;
10557
10558 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10559 b->number, paddress (bl->gdbarch, bl->address),
10560 paddress (bl->gdbarch, bl->address + bl->length - 1));
10561}
10562
10563/* Implement the "print_recreate" breakpoint_ops method for
10564 ranged breakpoints. */
10565
10566static void
10567print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10568{
10569 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10570 b->addr_string_range_end);
d9b3f62e 10571 print_recreate_thread (b, fp);
f1310107
TJB
10572}
10573
10574/* The breakpoint_ops structure to be used in ranged breakpoints. */
10575
2060206e 10576static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10577
10578/* Find the address where the end of the breakpoint range should be
10579 placed, given the SAL of the end of the range. This is so that if
10580 the user provides a line number, the end of the range is set to the
10581 last instruction of the given line. */
10582
10583static CORE_ADDR
10584find_breakpoint_range_end (struct symtab_and_line sal)
10585{
10586 CORE_ADDR end;
10587
10588 /* If the user provided a PC value, use it. Otherwise,
10589 find the address of the end of the given location. */
10590 if (sal.explicit_pc)
10591 end = sal.pc;
10592 else
10593 {
10594 int ret;
10595 CORE_ADDR start;
10596
10597 ret = find_line_pc_range (sal, &start, &end);
10598 if (!ret)
10599 error (_("Could not find location of the end of the range."));
10600
10601 /* find_line_pc_range returns the start of the next line. */
10602 end--;
10603 }
10604
10605 return end;
10606}
10607
10608/* Implement the "break-range" CLI command. */
10609
10610static void
10611break_range_command (char *arg, int from_tty)
10612{
10613 char *arg_start, *addr_string_start, *addr_string_end;
10614 struct linespec_result canonical_start, canonical_end;
10615 int bp_count, can_use_bp, length;
10616 CORE_ADDR end;
10617 struct breakpoint *b;
10618 struct symtab_and_line sal_start, sal_end;
f1310107 10619 struct cleanup *cleanup_bkpt;
f8eba3c6 10620 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10621
10622 /* We don't support software ranged breakpoints. */
10623 if (target_ranged_break_num_registers () < 0)
10624 error (_("This target does not support hardware ranged breakpoints."));
10625
10626 bp_count = hw_breakpoint_used_count ();
10627 bp_count += target_ranged_break_num_registers ();
10628 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10629 bp_count, 0);
10630 if (can_use_bp < 0)
10631 error (_("Hardware breakpoints used exceeds limit."));
10632
f8eba3c6 10633 arg = skip_spaces (arg);
f1310107
TJB
10634 if (arg == NULL || arg[0] == '\0')
10635 error(_("No address range specified."));
10636
f1310107
TJB
10637 init_linespec_result (&canonical_start);
10638
f8eba3c6
TT
10639 arg_start = arg;
10640 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10641
f8eba3c6 10642 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10643
10644 if (arg[0] != ',')
10645 error (_("Too few arguments."));
f8eba3c6 10646 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10647 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10648
10649 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10650
10651 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10652 || lsal_start->sals.nelts != 1)
f1310107
TJB
10653 error (_("Cannot create a ranged breakpoint with multiple locations."));
10654
f8eba3c6
TT
10655 sal_start = lsal_start->sals.sals[0];
10656 addr_string_start = savestring (arg_start, arg - arg_start);
10657 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10658
10659 arg++; /* Skip the comma. */
f8eba3c6 10660 arg = skip_spaces (arg);
f1310107
TJB
10661
10662 /* Parse the end location. */
10663
f1310107
TJB
10664 init_linespec_result (&canonical_end);
10665 arg_start = arg;
10666
f8eba3c6 10667 /* We call decode_line_full directly here instead of using
f1310107
TJB
10668 parse_breakpoint_sals because we need to specify the start location's
10669 symtab and line as the default symtab and line for the end of the
10670 range. This makes it possible to have ranges like "foo.c:27, +14",
10671 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10672 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10673 sal_start.symtab, sal_start.line,
10674 &canonical_end, NULL, NULL);
10675
10676 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10677
f8eba3c6 10678 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10679 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10680
10681 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10682 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10683 || lsal_end->sals.nelts != 1)
f1310107
TJB
10684 error (_("Cannot create a ranged breakpoint with multiple locations."));
10685
f8eba3c6
TT
10686 sal_end = lsal_end->sals.sals[0];
10687 addr_string_end = savestring (arg_start, arg - arg_start);
10688 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10689
10690 end = find_breakpoint_range_end (sal_end);
10691 if (sal_start.pc > end)
177b42fe 10692 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10693
10694 length = end - sal_start.pc + 1;
10695 if (length < 0)
10696 /* Length overflowed. */
10697 error (_("Address range too large."));
10698 else if (length == 1)
10699 {
10700 /* This range is simple enough to be handled by
10701 the `hbreak' command. */
10702 hbreak_command (addr_string_start, 1);
10703
10704 do_cleanups (cleanup_bkpt);
10705
10706 return;
10707 }
10708
10709 /* Now set up the breakpoint. */
10710 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10711 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10712 set_breakpoint_count (breakpoint_count + 1);
10713 b->number = breakpoint_count;
10714 b->disposition = disp_donttouch;
f8eba3c6
TT
10715 b->addr_string = xstrdup (addr_string_start);
10716 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10717 b->loc->length = length;
10718
f8eba3c6 10719 do_cleanups (cleanup_bkpt);
f1310107
TJB
10720
10721 mention (b);
8d3788bd 10722 observer_notify_breakpoint_created (b);
44702360 10723 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10724}
10725
4a64f543
MS
10726/* Return non-zero if EXP is verified as constant. Returned zero
10727 means EXP is variable. Also the constant detection may fail for
10728 some constant expressions and in such case still falsely return
10729 zero. */
2e6e3d9c 10730
65d79d4b
SDJ
10731static int
10732watchpoint_exp_is_const (const struct expression *exp)
10733{
10734 int i = exp->nelts;
10735
10736 while (i > 0)
10737 {
10738 int oplenp, argsp;
10739
10740 /* We are only interested in the descriptor of each element. */
10741 operator_length (exp, i, &oplenp, &argsp);
10742 i -= oplenp;
10743
10744 switch (exp->elts[i].opcode)
10745 {
10746 case BINOP_ADD:
10747 case BINOP_SUB:
10748 case BINOP_MUL:
10749 case BINOP_DIV:
10750 case BINOP_REM:
10751 case BINOP_MOD:
10752 case BINOP_LSH:
10753 case BINOP_RSH:
10754 case BINOP_LOGICAL_AND:
10755 case BINOP_LOGICAL_OR:
10756 case BINOP_BITWISE_AND:
10757 case BINOP_BITWISE_IOR:
10758 case BINOP_BITWISE_XOR:
10759 case BINOP_EQUAL:
10760 case BINOP_NOTEQUAL:
10761 case BINOP_LESS:
10762 case BINOP_GTR:
10763 case BINOP_LEQ:
10764 case BINOP_GEQ:
10765 case BINOP_REPEAT:
10766 case BINOP_COMMA:
10767 case BINOP_EXP:
10768 case BINOP_MIN:
10769 case BINOP_MAX:
10770 case BINOP_INTDIV:
10771 case BINOP_CONCAT:
65d79d4b
SDJ
10772 case TERNOP_COND:
10773 case TERNOP_SLICE:
65d79d4b
SDJ
10774
10775 case OP_LONG:
10776 case OP_DOUBLE:
10777 case OP_DECFLOAT:
10778 case OP_LAST:
10779 case OP_COMPLEX:
10780 case OP_STRING:
65d79d4b
SDJ
10781 case OP_ARRAY:
10782 case OP_TYPE:
608b4967
TT
10783 case OP_TYPEOF:
10784 case OP_DECLTYPE:
6e72ca20 10785 case OP_TYPEID:
65d79d4b
SDJ
10786 case OP_NAME:
10787 case OP_OBJC_NSSTRING:
10788
10789 case UNOP_NEG:
10790 case UNOP_LOGICAL_NOT:
10791 case UNOP_COMPLEMENT:
10792 case UNOP_ADDR:
10793 case UNOP_HIGH:
aeaa2474 10794 case UNOP_CAST:
9eaf6705
TT
10795
10796 case UNOP_CAST_TYPE:
10797 case UNOP_REINTERPRET_CAST:
10798 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10799 /* Unary, binary and ternary operators: We have to check
10800 their operands. If they are constant, then so is the
10801 result of that operation. For instance, if A and B are
10802 determined to be constants, then so is "A + B".
10803
10804 UNOP_IND is one exception to the rule above, because the
10805 value of *ADDR is not necessarily a constant, even when
10806 ADDR is. */
65d79d4b
SDJ
10807 break;
10808
10809 case OP_VAR_VALUE:
10810 /* Check whether the associated symbol is a constant.
4a64f543 10811
65d79d4b 10812 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10813 possible that a buggy compiler could mark a variable as
10814 constant even when it is not, and TYPE_CONST would return
10815 true in this case, while SYMBOL_CLASS wouldn't.
10816
10817 We also have to check for function symbols because they
10818 are always constant. */
65d79d4b
SDJ
10819 {
10820 struct symbol *s = exp->elts[i + 2].symbol;
10821
10822 if (SYMBOL_CLASS (s) != LOC_BLOCK
10823 && SYMBOL_CLASS (s) != LOC_CONST
10824 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10825 return 0;
10826 break;
10827 }
10828
10829 /* The default action is to return 0 because we are using
10830 the optimistic approach here: If we don't know something,
10831 then it is not a constant. */
10832 default:
10833 return 0;
10834 }
10835 }
10836
10837 return 1;
10838}
10839
3a5c3e22
PA
10840/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10841
10842static void
10843dtor_watchpoint (struct breakpoint *self)
10844{
10845 struct watchpoint *w = (struct watchpoint *) self;
10846
10847 xfree (w->cond_exp);
10848 xfree (w->exp);
10849 xfree (w->exp_string);
10850 xfree (w->exp_string_reparse);
10851 value_free (w->val);
10852
10853 base_breakpoint_ops.dtor (self);
10854}
10855
348d480f
PA
10856/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10857
10858static void
10859re_set_watchpoint (struct breakpoint *b)
10860{
3a5c3e22
PA
10861 struct watchpoint *w = (struct watchpoint *) b;
10862
348d480f
PA
10863 /* Watchpoint can be either on expression using entirely global
10864 variables, or it can be on local variables.
10865
10866 Watchpoints of the first kind are never auto-deleted, and even
10867 persist across program restarts. Since they can use variables
10868 from shared libraries, we need to reparse expression as libraries
10869 are loaded and unloaded.
10870
10871 Watchpoints on local variables can also change meaning as result
10872 of solib event. For example, if a watchpoint uses both a local
10873 and a global variables in expression, it's a local watchpoint,
10874 but unloading of a shared library will make the expression
10875 invalid. This is not a very common use case, but we still
10876 re-evaluate expression, to avoid surprises to the user.
10877
10878 Note that for local watchpoints, we re-evaluate it only if
10879 watchpoints frame id is still valid. If it's not, it means the
10880 watchpoint is out of scope and will be deleted soon. In fact,
10881 I'm not sure we'll ever be called in this case.
10882
10883 If a local watchpoint's frame id is still valid, then
3a5c3e22 10884 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10885
3a5c3e22
PA
10886 Don't do anything about disabled watchpoints, since they will be
10887 reevaluated again when enabled. */
10888 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10889}
10890
77b06cd7
TJB
10891/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10892
10893static int
10894insert_watchpoint (struct bp_location *bl)
10895{
3a5c3e22
PA
10896 struct watchpoint *w = (struct watchpoint *) bl->owner;
10897 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10898
10899 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10900 w->cond_exp);
77b06cd7
TJB
10901}
10902
10903/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10904
10905static int
10906remove_watchpoint (struct bp_location *bl)
10907{
3a5c3e22
PA
10908 struct watchpoint *w = (struct watchpoint *) bl->owner;
10909 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10910
10911 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10912 w->cond_exp);
e09342b5
TJB
10913}
10914
e09342b5 10915static int
348d480f 10916breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10917 struct address_space *aspace, CORE_ADDR bp_addr,
10918 const struct target_waitstatus *ws)
e09342b5 10919{
348d480f 10920 struct breakpoint *b = bl->owner;
3a5c3e22 10921 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10922
348d480f
PA
10923 /* Continuable hardware watchpoints are treated as non-existent if the
10924 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10925 some data address). Otherwise gdb won't stop on a break instruction
10926 in the code (not from a breakpoint) when a hardware watchpoint has
10927 been defined. Also skip watchpoints which we know did not trigger
10928 (did not match the data address). */
10929 if (is_hardware_watchpoint (b)
3a5c3e22 10930 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10931 return 0;
9c06b0b4 10932
348d480f 10933 return 1;
9c06b0b4
TJB
10934}
10935
348d480f
PA
10936static void
10937check_status_watchpoint (bpstat bs)
9c06b0b4 10938{
348d480f 10939 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10940
348d480f 10941 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10942}
10943
10944/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10945 hardware watchpoints. */
9c06b0b4
TJB
10946
10947static int
348d480f 10948resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10949{
3a5c3e22
PA
10950 struct watchpoint *w = (struct watchpoint *) bl->owner;
10951 int length = w->exact? 1 : bl->length;
348d480f
PA
10952
10953 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10954}
10955
10956/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10957 hardware watchpoints. */
9c06b0b4
TJB
10958
10959static int
348d480f 10960works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10961{
efa80663
PA
10962 /* Read and access watchpoints only work with hardware support. */
10963 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10964}
10965
9c06b0b4 10966static enum print_stop_action
348d480f 10967print_it_watchpoint (bpstat bs)
9c06b0b4 10968{
348d480f
PA
10969 struct cleanup *old_chain;
10970 struct breakpoint *b;
f99d8bf4 10971 struct ui_file *stb;
348d480f 10972 enum print_stop_action result;
3a5c3e22 10973 struct watchpoint *w;
79a45e25 10974 struct ui_out *uiout = current_uiout;
348d480f
PA
10975
10976 gdb_assert (bs->bp_location_at != NULL);
10977
348d480f 10978 b = bs->breakpoint_at;
3a5c3e22 10979 w = (struct watchpoint *) b;
348d480f 10980
f99d8bf4
PA
10981 stb = mem_fileopen ();
10982 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10983
10984 switch (b->type)
10985 {
348d480f 10986 case bp_watchpoint:
9c06b0b4
TJB
10987 case bp_hardware_watchpoint:
10988 annotate_watchpoint (b->number);
10989 if (ui_out_is_mi_like_p (uiout))
10990 ui_out_field_string
10991 (uiout, "reason",
10992 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10993 mention (b);
10994 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10995 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10996 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10997 ui_out_field_stream (uiout, "old", stb);
10998 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10999 watchpoint_value_print (w->val, stb);
348d480f
PA
11000 ui_out_field_stream (uiout, "new", stb);
11001 ui_out_text (uiout, "\n");
11002 /* More than one watchpoint may have been triggered. */
11003 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11004 break;
11005
11006 case bp_read_watchpoint:
11007 if (ui_out_is_mi_like_p (uiout))
11008 ui_out_field_string
11009 (uiout, "reason",
11010 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
11011 mention (b);
11012 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11013 ui_out_text (uiout, "\nValue = ");
f99d8bf4 11014 watchpoint_value_print (w->val, stb);
348d480f
PA
11015 ui_out_field_stream (uiout, "value", stb);
11016 ui_out_text (uiout, "\n");
11017 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11018 break;
11019
11020 case bp_access_watchpoint:
348d480f
PA
11021 if (bs->old_val != NULL)
11022 {
11023 annotate_watchpoint (b->number);
11024 if (ui_out_is_mi_like_p (uiout))
11025 ui_out_field_string
11026 (uiout, "reason",
11027 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11028 mention (b);
11029 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11030 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 11031 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
11032 ui_out_field_stream (uiout, "old", stb);
11033 ui_out_text (uiout, "\nNew value = ");
11034 }
11035 else
11036 {
11037 mention (b);
11038 if (ui_out_is_mi_like_p (uiout))
11039 ui_out_field_string
11040 (uiout, "reason",
11041 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11042 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11043 ui_out_text (uiout, "\nValue = ");
11044 }
f99d8bf4 11045 watchpoint_value_print (w->val, stb);
348d480f
PA
11046 ui_out_field_stream (uiout, "new", stb);
11047 ui_out_text (uiout, "\n");
11048 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11049 break;
11050 default:
348d480f 11051 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11052 }
11053
348d480f
PA
11054 do_cleanups (old_chain);
11055 return result;
11056}
11057
11058/* Implement the "print_mention" breakpoint_ops method for hardware
11059 watchpoints. */
11060
11061static void
11062print_mention_watchpoint (struct breakpoint *b)
11063{
11064 struct cleanup *ui_out_chain;
3a5c3e22 11065 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11066 struct ui_out *uiout = current_uiout;
348d480f
PA
11067
11068 switch (b->type)
11069 {
11070 case bp_watchpoint:
11071 ui_out_text (uiout, "Watchpoint ");
11072 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11073 break;
11074 case bp_hardware_watchpoint:
11075 ui_out_text (uiout, "Hardware watchpoint ");
11076 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11077 break;
11078 case bp_read_watchpoint:
11079 ui_out_text (uiout, "Hardware read watchpoint ");
11080 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11081 break;
11082 case bp_access_watchpoint:
11083 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
11084 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11085 break;
11086 default:
11087 internal_error (__FILE__, __LINE__,
11088 _("Invalid hardware watchpoint type."));
11089 }
11090
11091 ui_out_field_int (uiout, "number", b->number);
11092 ui_out_text (uiout, ": ");
3a5c3e22 11093 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
11094 do_cleanups (ui_out_chain);
11095}
11096
11097/* Implement the "print_recreate" breakpoint_ops method for
11098 watchpoints. */
11099
11100static void
11101print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
11102{
3a5c3e22
PA
11103 struct watchpoint *w = (struct watchpoint *) b;
11104
348d480f
PA
11105 switch (b->type)
11106 {
11107 case bp_watchpoint:
11108 case bp_hardware_watchpoint:
11109 fprintf_unfiltered (fp, "watch");
11110 break;
11111 case bp_read_watchpoint:
11112 fprintf_unfiltered (fp, "rwatch");
11113 break;
11114 case bp_access_watchpoint:
11115 fprintf_unfiltered (fp, "awatch");
11116 break;
11117 default:
11118 internal_error (__FILE__, __LINE__,
11119 _("Invalid watchpoint type."));
11120 }
11121
3a5c3e22 11122 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 11123 print_recreate_thread (b, fp);
348d480f
PA
11124}
11125
427cd150
TT
11126/* Implement the "explains_signal" breakpoint_ops method for
11127 watchpoints. */
11128
47591c29 11129static int
427cd150
TT
11130explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
11131{
11132 /* A software watchpoint cannot cause a signal other than
11133 GDB_SIGNAL_TRAP. */
11134 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 11135 return 0;
427cd150 11136
47591c29 11137 return 1;
427cd150
TT
11138}
11139
348d480f
PA
11140/* The breakpoint_ops structure to be used in hardware watchpoints. */
11141
2060206e 11142static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
11143
11144/* Implement the "insert" breakpoint_ops method for
11145 masked hardware watchpoints. */
11146
11147static int
11148insert_masked_watchpoint (struct bp_location *bl)
11149{
3a5c3e22
PA
11150 struct watchpoint *w = (struct watchpoint *) bl->owner;
11151
11152 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11153 bl->watchpoint_type);
11154}
11155
11156/* Implement the "remove" breakpoint_ops method for
11157 masked hardware watchpoints. */
11158
11159static int
11160remove_masked_watchpoint (struct bp_location *bl)
11161{
3a5c3e22
PA
11162 struct watchpoint *w = (struct watchpoint *) bl->owner;
11163
11164 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11165 bl->watchpoint_type);
11166}
11167
11168/* Implement the "resources_needed" breakpoint_ops method for
11169 masked hardware watchpoints. */
11170
11171static int
11172resources_needed_masked_watchpoint (const struct bp_location *bl)
11173{
3a5c3e22
PA
11174 struct watchpoint *w = (struct watchpoint *) bl->owner;
11175
11176 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
11177}
11178
11179/* Implement the "works_in_software_mode" breakpoint_ops method for
11180 masked hardware watchpoints. */
11181
11182static int
11183works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11184{
11185 return 0;
11186}
11187
11188/* Implement the "print_it" breakpoint_ops method for
11189 masked hardware watchpoints. */
11190
11191static enum print_stop_action
11192print_it_masked_watchpoint (bpstat bs)
11193{
11194 struct breakpoint *b = bs->breakpoint_at;
79a45e25 11195 struct ui_out *uiout = current_uiout;
348d480f
PA
11196
11197 /* Masked watchpoints have only one location. */
11198 gdb_assert (b->loc && b->loc->next == NULL);
11199
11200 switch (b->type)
11201 {
11202 case bp_hardware_watchpoint:
11203 annotate_watchpoint (b->number);
11204 if (ui_out_is_mi_like_p (uiout))
11205 ui_out_field_string
11206 (uiout, "reason",
11207 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11208 break;
11209
11210 case bp_read_watchpoint:
11211 if (ui_out_is_mi_like_p (uiout))
11212 ui_out_field_string
11213 (uiout, "reason",
11214 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11215 break;
11216
11217 case bp_access_watchpoint:
11218 if (ui_out_is_mi_like_p (uiout))
11219 ui_out_field_string
11220 (uiout, "reason",
11221 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11222 break;
11223 default:
11224 internal_error (__FILE__, __LINE__,
11225 _("Invalid hardware watchpoint type."));
11226 }
11227
11228 mention (b);
9c06b0b4
TJB
11229 ui_out_text (uiout, _("\n\
11230Check the underlying instruction at PC for the memory\n\
11231address and value which triggered this watchpoint.\n"));
11232 ui_out_text (uiout, "\n");
11233
11234 /* More than one watchpoint may have been triggered. */
11235 return PRINT_UNKNOWN;
11236}
11237
11238/* Implement the "print_one_detail" breakpoint_ops method for
11239 masked hardware watchpoints. */
11240
11241static void
11242print_one_detail_masked_watchpoint (const struct breakpoint *b,
11243 struct ui_out *uiout)
11244{
3a5c3e22
PA
11245 struct watchpoint *w = (struct watchpoint *) b;
11246
9c06b0b4
TJB
11247 /* Masked watchpoints have only one location. */
11248 gdb_assert (b->loc && b->loc->next == NULL);
11249
11250 ui_out_text (uiout, "\tmask ");
3a5c3e22 11251 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11252 ui_out_text (uiout, "\n");
11253}
11254
11255/* Implement the "print_mention" breakpoint_ops method for
11256 masked hardware watchpoints. */
11257
11258static void
11259print_mention_masked_watchpoint (struct breakpoint *b)
11260{
3a5c3e22 11261 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11262 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11263 struct cleanup *ui_out_chain;
11264
11265 switch (b->type)
11266 {
11267 case bp_hardware_watchpoint:
11268 ui_out_text (uiout, "Masked hardware watchpoint ");
11269 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11270 break;
11271 case bp_read_watchpoint:
11272 ui_out_text (uiout, "Masked hardware read watchpoint ");
11273 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11274 break;
11275 case bp_access_watchpoint:
11276 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11277 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11278 break;
11279 default:
11280 internal_error (__FILE__, __LINE__,
11281 _("Invalid hardware watchpoint type."));
11282 }
11283
11284 ui_out_field_int (uiout, "number", b->number);
11285 ui_out_text (uiout, ": ");
3a5c3e22 11286 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11287 do_cleanups (ui_out_chain);
11288}
11289
11290/* Implement the "print_recreate" breakpoint_ops method for
11291 masked hardware watchpoints. */
11292
11293static void
11294print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11295{
3a5c3e22 11296 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11297 char tmp[40];
11298
11299 switch (b->type)
11300 {
11301 case bp_hardware_watchpoint:
11302 fprintf_unfiltered (fp, "watch");
11303 break;
11304 case bp_read_watchpoint:
11305 fprintf_unfiltered (fp, "rwatch");
11306 break;
11307 case bp_access_watchpoint:
11308 fprintf_unfiltered (fp, "awatch");
11309 break;
11310 default:
11311 internal_error (__FILE__, __LINE__,
11312 _("Invalid hardware watchpoint type."));
11313 }
11314
3a5c3e22
PA
11315 sprintf_vma (tmp, w->hw_wp_mask);
11316 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11317 print_recreate_thread (b, fp);
9c06b0b4
TJB
11318}
11319
11320/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11321
2060206e 11322static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11323
11324/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11325
11326static int
11327is_masked_watchpoint (const struct breakpoint *b)
11328{
11329 return b->ops == &masked_watchpoint_breakpoint_ops;
11330}
11331
53a5351d
JM
11332/* accessflag: hw_write: watch write,
11333 hw_read: watch read,
11334 hw_access: watch access (read or write) */
c906108c 11335static void
bbc13ae3 11336watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11337 int just_location, int internal)
c906108c 11338{
d983da9c 11339 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11340 struct expression *exp;
270140bd 11341 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11342 struct value *val, *mark, *result;
bb9d5f81 11343 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11344 struct frame_info *frame;
bbc13ae3
KS
11345 const char *exp_start = NULL;
11346 const char *exp_end = NULL;
11347 const char *tok, *end_tok;
9c06b0b4 11348 int toklen = -1;
bbc13ae3
KS
11349 const char *cond_start = NULL;
11350 const char *cond_end = NULL;
c906108c 11351 enum bptype bp_type;
37e4754d 11352 int thread = -1;
0cf6dd15 11353 int pc = 0;
9c06b0b4
TJB
11354 /* Flag to indicate whether we are going to use masks for
11355 the hardware watchpoint. */
11356 int use_mask = 0;
11357 CORE_ADDR mask = 0;
3a5c3e22 11358 struct watchpoint *w;
bbc13ae3
KS
11359 char *expression;
11360 struct cleanup *back_to;
c906108c 11361
37e4754d
LM
11362 /* Make sure that we actually have parameters to parse. */
11363 if (arg != NULL && arg[0] != '\0')
11364 {
bbc13ae3
KS
11365 const char *value_start;
11366
11367 exp_end = arg + strlen (arg);
37e4754d 11368
9c06b0b4
TJB
11369 /* Look for "parameter value" pairs at the end
11370 of the arguments string. */
bbc13ae3 11371 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11372 {
11373 /* Skip whitespace at the end of the argument list. */
11374 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11375 tok--;
11376
11377 /* Find the beginning of the last token.
11378 This is the value of the parameter. */
11379 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11380 tok--;
11381 value_start = tok + 1;
11382
11383 /* Skip whitespace. */
11384 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11385 tok--;
11386
11387 end_tok = tok;
11388
11389 /* Find the beginning of the second to last token.
11390 This is the parameter itself. */
11391 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11392 tok--;
11393 tok++;
11394 toklen = end_tok - tok + 1;
11395
61012eef 11396 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4
TJB
11397 {
11398 /* At this point we've found a "thread" token, which means
11399 the user is trying to set a watchpoint that triggers
11400 only in a specific thread. */
11401 char *endp;
37e4754d 11402
9c06b0b4
TJB
11403 if (thread != -1)
11404 error(_("You can specify only one thread."));
37e4754d 11405
9c06b0b4
TJB
11406 /* Extract the thread ID from the next token. */
11407 thread = strtol (value_start, &endp, 0);
37e4754d 11408
9c06b0b4
TJB
11409 /* Check if the user provided a valid numeric value for the
11410 thread ID. */
11411 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11412 error (_("Invalid thread ID specification %s."), value_start);
11413
11414 /* Check if the thread actually exists. */
11415 if (!valid_thread_id (thread))
af4908ba 11416 invalid_thread_id_error (thread);
9c06b0b4 11417 }
61012eef 11418 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11419 {
11420 /* We've found a "mask" token, which means the user wants to
11421 create a hardware watchpoint that is going to have the mask
11422 facility. */
11423 struct value *mask_value, *mark;
37e4754d 11424
9c06b0b4
TJB
11425 if (use_mask)
11426 error(_("You can specify only one mask."));
37e4754d 11427
9c06b0b4 11428 use_mask = just_location = 1;
37e4754d 11429
9c06b0b4
TJB
11430 mark = value_mark ();
11431 mask_value = parse_to_comma_and_eval (&value_start);
11432 mask = value_as_address (mask_value);
11433 value_free_to_mark (mark);
11434 }
11435 else
11436 /* We didn't recognize what we found. We should stop here. */
11437 break;
37e4754d 11438
9c06b0b4
TJB
11439 /* Truncate the string and get rid of the "parameter value" pair before
11440 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11441 exp_end = tok;
9c06b0b4 11442 }
37e4754d 11443 }
bbc13ae3
KS
11444 else
11445 exp_end = arg;
37e4754d 11446
bbc13ae3
KS
11447 /* Parse the rest of the arguments. From here on out, everything
11448 is in terms of a newly allocated string instead of the original
11449 ARG. */
c906108c 11450 innermost_block = NULL;
bbc13ae3
KS
11451 expression = savestring (arg, exp_end - arg);
11452 back_to = make_cleanup (xfree, expression);
11453 exp_start = arg = expression;
1bb9788d 11454 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11455 exp_end = arg;
fa8a61dc
TT
11456 /* Remove trailing whitespace from the expression before saving it.
11457 This makes the eventual display of the expression string a bit
11458 prettier. */
11459 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11460 --exp_end;
11461
65d79d4b
SDJ
11462 /* Checking if the expression is not constant. */
11463 if (watchpoint_exp_is_const (exp))
11464 {
11465 int len;
11466
11467 len = exp_end - exp_start;
11468 while (len > 0 && isspace (exp_start[len - 1]))
11469 len--;
11470 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11471 }
11472
c906108c
SS
11473 exp_valid_block = innermost_block;
11474 mark = value_mark ();
3a1115a0 11475 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11476
bb9d5f81
PP
11477 if (val != NULL && just_location)
11478 {
11479 saved_bitpos = value_bitpos (val);
11480 saved_bitsize = value_bitsize (val);
11481 }
11482
06a64a0b
TT
11483 if (just_location)
11484 {
9c06b0b4
TJB
11485 int ret;
11486
06a64a0b 11487 exp_valid_block = NULL;
a1442452 11488 val = value_addr (result);
06a64a0b
TT
11489 release_value (val);
11490 value_free_to_mark (mark);
9c06b0b4
TJB
11491
11492 if (use_mask)
11493 {
11494 ret = target_masked_watch_num_registers (value_as_address (val),
11495 mask);
11496 if (ret == -1)
11497 error (_("This target does not support masked watchpoints."));
11498 else if (ret == -2)
11499 error (_("Invalid mask or memory region."));
11500 }
06a64a0b
TT
11501 }
11502 else if (val != NULL)
fa4727a6 11503 release_value (val);
c906108c 11504
bbc13ae3
KS
11505 tok = skip_spaces_const (arg);
11506 end_tok = skip_to_space_const (tok);
c906108c
SS
11507
11508 toklen = end_tok - tok;
11509 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11510 {
2d134ed3
PA
11511 struct expression *cond;
11512
60e1c644 11513 innermost_block = NULL;
c906108c 11514 tok = cond_start = end_tok + 1;
1bb9788d 11515 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11516
11517 /* The watchpoint expression may not be local, but the condition
11518 may still be. E.g.: `watch global if local > 0'. */
11519 cond_exp_valid_block = innermost_block;
11520
2d134ed3 11521 xfree (cond);
c906108c
SS
11522 cond_end = tok;
11523 }
11524 if (*tok)
8a3fe4f8 11525 error (_("Junk at end of command."));
c906108c 11526
d983da9c 11527 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11528
11529 /* If the expression is "local", then set up a "watchpoint scope"
11530 breakpoint at the point where we've left the scope of the watchpoint
11531 expression. Create the scope breakpoint before the watchpoint, so
11532 that we will encounter it first in bpstat_stop_status. */
60e1c644 11533 if (exp_valid_block && frame)
d983da9c 11534 {
edb3359d
DJ
11535 if (frame_id_p (frame_unwind_caller_id (frame)))
11536 {
11537 scope_breakpoint
a6d9a66e
UW
11538 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11539 frame_unwind_caller_pc (frame),
06edf0c0
PA
11540 bp_watchpoint_scope,
11541 &momentary_breakpoint_ops);
d983da9c 11542
edb3359d 11543 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11544
edb3359d
DJ
11545 /* Automatically delete the breakpoint when it hits. */
11546 scope_breakpoint->disposition = disp_del;
d983da9c 11547
edb3359d
DJ
11548 /* Only break in the proper frame (help with recursion). */
11549 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11550
edb3359d 11551 /* Set the address at which we will stop. */
a6d9a66e
UW
11552 scope_breakpoint->loc->gdbarch
11553 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11554 scope_breakpoint->loc->requested_address
11555 = frame_unwind_caller_pc (frame);
11556 scope_breakpoint->loc->address
a6d9a66e
UW
11557 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11558 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11559 scope_breakpoint->type);
11560 }
d983da9c
DJ
11561 }
11562
e8369a73
AB
11563 /* Now set up the breakpoint. We create all watchpoints as hardware
11564 watchpoints here even if hardware watchpoints are turned off, a call
11565 to update_watchpoint later in this function will cause the type to
11566 drop back to bp_watchpoint (software watchpoint) if required. */
11567
11568 if (accessflag == hw_read)
11569 bp_type = bp_read_watchpoint;
11570 else if (accessflag == hw_access)
11571 bp_type = bp_access_watchpoint;
11572 else
11573 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11574
11575 w = XCNEW (struct watchpoint);
11576 b = &w->base;
348d480f 11577 if (use_mask)
3a5c3e22
PA
11578 init_raw_breakpoint_without_location (b, NULL, bp_type,
11579 &masked_watchpoint_breakpoint_ops);
348d480f 11580 else
3a5c3e22
PA
11581 init_raw_breakpoint_without_location (b, NULL, bp_type,
11582 &watchpoint_breakpoint_ops);
37e4754d 11583 b->thread = thread;
b5de0fa7 11584 b->disposition = disp_donttouch;
348d480f 11585 b->pspace = current_program_space;
3a5c3e22
PA
11586 w->exp = exp;
11587 w->exp_valid_block = exp_valid_block;
11588 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11589 if (just_location)
11590 {
11591 struct type *t = value_type (val);
11592 CORE_ADDR addr = value_as_address (val);
11593 char *name;
11594
11595 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11596 name = type_to_string (t);
11597
3a5c3e22 11598 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11599 core_addr_to_string (addr));
06a64a0b
TT
11600 xfree (name);
11601
3a5c3e22 11602 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11603 (int) (exp_end - exp_start), exp_start);
11604
06a64a0b
TT
11605 /* The above expression is in C. */
11606 b->language = language_c;
11607 }
11608 else
3a5c3e22 11609 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11610
11611 if (use_mask)
11612 {
3a5c3e22 11613 w->hw_wp_mask = mask;
9c06b0b4
TJB
11614 }
11615 else
11616 {
3a5c3e22 11617 w->val = val;
bb9d5f81
PP
11618 w->val_bitpos = saved_bitpos;
11619 w->val_bitsize = saved_bitsize;
3a5c3e22 11620 w->val_valid = 1;
9c06b0b4 11621 }
77b06cd7 11622
c906108c
SS
11623 if (cond_start)
11624 b->cond_string = savestring (cond_start, cond_end - cond_start);
11625 else
11626 b->cond_string = 0;
c5aa993b 11627
c906108c 11628 if (frame)
f6bc2008 11629 {
3a5c3e22
PA
11630 w->watchpoint_frame = get_frame_id (frame);
11631 w->watchpoint_thread = inferior_ptid;
f6bc2008 11632 }
c906108c 11633 else
f6bc2008 11634 {
3a5c3e22
PA
11635 w->watchpoint_frame = null_frame_id;
11636 w->watchpoint_thread = null_ptid;
f6bc2008 11637 }
c906108c 11638
d983da9c 11639 if (scope_breakpoint != NULL)
c906108c 11640 {
d983da9c
DJ
11641 /* The scope breakpoint is related to the watchpoint. We will
11642 need to act on them together. */
11643 b->related_breakpoint = scope_breakpoint;
11644 scope_breakpoint->related_breakpoint = b;
c906108c 11645 }
d983da9c 11646
06a64a0b
TT
11647 if (!just_location)
11648 value_free_to_mark (mark);
2d134ed3 11649
492d29ea 11650 TRY
a9634178
TJB
11651 {
11652 /* Finally update the new watchpoint. This creates the locations
11653 that should be inserted. */
3a5c3e22 11654 update_watchpoint (w, 1);
a9634178 11655 }
492d29ea 11656 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11657 {
11658 delete_breakpoint (b);
11659 throw_exception (e);
11660 }
492d29ea 11661 END_CATCH
a9634178 11662
3ea46bff 11663 install_breakpoint (internal, b, 1);
bbc13ae3 11664 do_cleanups (back_to);
c906108c
SS
11665}
11666
e09342b5 11667/* Return count of debug registers needed to watch the given expression.
e09342b5 11668 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11669
c906108c 11670static int
a9634178 11671can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11672{
11673 int found_memory_cnt = 0;
2e70b7b9 11674 struct value *head = v;
c906108c
SS
11675
11676 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11677 if (!can_use_hw_watchpoints)
c906108c 11678 return 0;
c5aa993b 11679
5c44784c
JM
11680 /* Make sure that the value of the expression depends only upon
11681 memory contents, and values computed from them within GDB. If we
11682 find any register references or function calls, we can't use a
11683 hardware watchpoint.
11684
11685 The idea here is that evaluating an expression generates a series
11686 of values, one holding the value of every subexpression. (The
11687 expression a*b+c has five subexpressions: a, b, a*b, c, and
11688 a*b+c.) GDB's values hold almost enough information to establish
11689 the criteria given above --- they identify memory lvalues,
11690 register lvalues, computed values, etcetera. So we can evaluate
11691 the expression, and then scan the chain of values that leaves
11692 behind to decide whether we can detect any possible change to the
11693 expression's final value using only hardware watchpoints.
11694
11695 However, I don't think that the values returned by inferior
11696 function calls are special in any way. So this function may not
11697 notice that an expression involving an inferior function call
11698 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11699 for (; v; v = value_next (v))
c906108c 11700 {
5c44784c 11701 if (VALUE_LVAL (v) == lval_memory)
c906108c 11702 {
8464be76
DJ
11703 if (v != head && value_lazy (v))
11704 /* A lazy memory lvalue in the chain is one that GDB never
11705 needed to fetch; we either just used its address (e.g.,
11706 `a' in `a.b') or we never needed it at all (e.g., `a'
11707 in `a,b'). This doesn't apply to HEAD; if that is
11708 lazy then it was not readable, but watch it anyway. */
5c44784c 11709 ;
53a5351d 11710 else
5c44784c
JM
11711 {
11712 /* Ahh, memory we actually used! Check if we can cover
11713 it with hardware watchpoints. */
df407dfe 11714 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11715
11716 /* We only watch structs and arrays if user asked for it
11717 explicitly, never if they just happen to appear in a
11718 middle of some value chain. */
11719 if (v == head
11720 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11721 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11722 {
42ae5230 11723 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11724 int len;
11725 int num_regs;
11726
a9634178 11727 len = (target_exact_watchpoints
e09342b5
TJB
11728 && is_scalar_type_recursive (vtype))?
11729 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11730
e09342b5
TJB
11731 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11732 if (!num_regs)
2e70b7b9
MS
11733 return 0;
11734 else
e09342b5 11735 found_memory_cnt += num_regs;
2e70b7b9 11736 }
5c44784c 11737 }
c5aa993b 11738 }
5086187c
AC
11739 else if (VALUE_LVAL (v) != not_lval
11740 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11741 return 0; /* These are values from the history (e.g., $1). */
5086187c 11742 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11743 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11744 }
11745
11746 /* The expression itself looks suitable for using a hardware
11747 watchpoint, but give the target machine a chance to reject it. */
11748 return found_memory_cnt;
11749}
11750
8b93c638 11751void
84f4c1fe 11752watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11753{
84f4c1fe 11754 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11755}
11756
06a64a0b
TT
11757/* A helper function that looks for the "-location" argument and then
11758 calls watch_command_1. */
11759
11760static void
11761watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11762{
11763 int just_location = 0;
11764
11765 if (arg
11766 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11767 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11768 {
e9cafbcc 11769 arg = skip_spaces (arg);
06a64a0b
TT
11770 just_location = 1;
11771 }
11772
84f4c1fe 11773 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11774}
8926118c 11775
c5aa993b 11776static void
fba45db2 11777watch_command (char *arg, int from_tty)
c906108c 11778{
06a64a0b 11779 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11780}
11781
8b93c638 11782void
84f4c1fe 11783rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11784{
84f4c1fe 11785 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11786}
8926118c 11787
c5aa993b 11788static void
fba45db2 11789rwatch_command (char *arg, int from_tty)
c906108c 11790{
06a64a0b 11791 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11792}
11793
8b93c638 11794void
84f4c1fe 11795awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11796{
84f4c1fe 11797 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11798}
8926118c 11799
c5aa993b 11800static void
fba45db2 11801awatch_command (char *arg, int from_tty)
c906108c 11802{
06a64a0b 11803 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11804}
c906108c 11805\f
c5aa993b 11806
43ff13b4 11807/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11808 because it uses the mechanisms of breakpoints. */
11809
bfec99b2
PA
11810struct until_break_command_continuation_args
11811{
11812 struct breakpoint *breakpoint;
11813 struct breakpoint *breakpoint2;
186c406b 11814 int thread_num;
bfec99b2
PA
11815};
11816
43ff13b4 11817/* This function is called by fetch_inferior_event via the
4a64f543 11818 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11819 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11820 command. */
c2c6d25f 11821static void
fa4cd53f 11822until_break_command_continuation (void *arg, int err)
43ff13b4 11823{
bfec99b2
PA
11824 struct until_break_command_continuation_args *a = arg;
11825
11826 delete_breakpoint (a->breakpoint);
11827 if (a->breakpoint2)
11828 delete_breakpoint (a->breakpoint2);
186c406b 11829 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11830}
11831
c906108c 11832void
ae66c1fc 11833until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11834{
11835 struct symtabs_and_lines sals;
11836 struct symtab_and_line sal;
8556afb4
PA
11837 struct frame_info *frame;
11838 struct gdbarch *frame_gdbarch;
11839 struct frame_id stack_frame_id;
11840 struct frame_id caller_frame_id;
c906108c 11841 struct breakpoint *breakpoint;
f107f563 11842 struct breakpoint *breakpoint2 = NULL;
c906108c 11843 struct cleanup *old_chain;
186c406b
TT
11844 int thread;
11845 struct thread_info *tp;
c906108c 11846
70509625 11847 clear_proceed_status (0);
c906108c
SS
11848
11849 /* Set a breakpoint where the user wants it and at return from
4a64f543 11850 this function. */
c5aa993b 11851
1bfeeb0f 11852 if (last_displayed_sal_is_valid ())
f8eba3c6 11853 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11854 get_last_displayed_symtab (),
f8eba3c6 11855 get_last_displayed_line ());
c906108c 11856 else
f8eba3c6
TT
11857 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11858 (struct symtab *) NULL, 0);
c5aa993b 11859
c906108c 11860 if (sals.nelts != 1)
8a3fe4f8 11861 error (_("Couldn't get information on specified line."));
c5aa993b 11862
c906108c 11863 sal = sals.sals[0];
4a64f543 11864 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11865
c906108c 11866 if (*arg)
8a3fe4f8 11867 error (_("Junk at end of arguments."));
c5aa993b 11868
c906108c 11869 resolve_sal_pc (&sal);
c5aa993b 11870
186c406b
TT
11871 tp = inferior_thread ();
11872 thread = tp->num;
11873
883bc8d1
PA
11874 old_chain = make_cleanup (null_cleanup, NULL);
11875
8556afb4
PA
11876 /* Note linespec handling above invalidates the frame chain.
11877 Installing a breakpoint also invalidates the frame chain (as it
11878 may need to switch threads), so do any frame handling before
11879 that. */
11880
11881 frame = get_selected_frame (NULL);
11882 frame_gdbarch = get_frame_arch (frame);
11883 stack_frame_id = get_stack_frame_id (frame);
11884 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11885
ae66c1fc
EZ
11886 /* Keep within the current frame, or in frames called by the current
11887 one. */
edb3359d 11888
883bc8d1 11889 if (frame_id_p (caller_frame_id))
c906108c 11890 {
883bc8d1
PA
11891 struct symtab_and_line sal2;
11892
11893 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11894 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11895 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11896 sal2,
11897 caller_frame_id,
f107f563
VP
11898 bp_until);
11899 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11900
883bc8d1 11901 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11902 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11903 }
c5aa993b 11904
c70a6932
JK
11905 /* set_momentary_breakpoint could invalidate FRAME. */
11906 frame = NULL;
11907
883bc8d1
PA
11908 if (anywhere)
11909 /* If the user told us to continue until a specified location,
11910 we don't specify a frame at which we need to stop. */
11911 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11912 null_frame_id, bp_until);
11913 else
11914 /* Otherwise, specify the selected frame, because we want to stop
11915 only at the very same frame. */
11916 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11917 stack_frame_id, bp_until);
11918 make_cleanup_delete_breakpoint (breakpoint);
11919
a493e3e2 11920 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11921
4a64f543
MS
11922 /* If we are running asynchronously, and proceed call above has
11923 actually managed to start the target, arrange for breakpoints to
11924 be deleted when the target stops. Otherwise, we're already
11925 stopped and delete breakpoints via cleanup chain. */
f107f563 11926
8ea051c5 11927 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11928 {
bfec99b2
PA
11929 struct until_break_command_continuation_args *args;
11930 args = xmalloc (sizeof (*args));
f107f563 11931
bfec99b2
PA
11932 args->breakpoint = breakpoint;
11933 args->breakpoint2 = breakpoint2;
186c406b 11934 args->thread_num = thread;
f107f563
VP
11935
11936 discard_cleanups (old_chain);
95e54da7
PA
11937 add_continuation (inferior_thread (),
11938 until_break_command_continuation, args,
604ead4a 11939 xfree);
f107f563
VP
11940 }
11941 else
c5aa993b 11942 do_cleanups (old_chain);
c906108c 11943}
ae66c1fc 11944
c906108c
SS
11945/* This function attempts to parse an optional "if <cond>" clause
11946 from the arg string. If one is not found, it returns NULL.
c5aa993b 11947
c906108c
SS
11948 Else, it returns a pointer to the condition string. (It does not
11949 attempt to evaluate the string against a particular block.) And,
11950 it updates arg to point to the first character following the parsed
4a64f543 11951 if clause in the arg string. */
53a5351d 11952
916703c0 11953char *
fba45db2 11954ep_parse_optional_if_clause (char **arg)
c906108c 11955{
c5aa993b
JM
11956 char *cond_string;
11957
11958 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11959 return NULL;
c5aa993b 11960
4a64f543 11961 /* Skip the "if" keyword. */
c906108c 11962 (*arg) += 2;
c5aa993b 11963
c906108c 11964 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11965 condition string. */
e9cafbcc 11966 *arg = skip_spaces (*arg);
c906108c 11967 cond_string = *arg;
c5aa993b 11968
4a64f543
MS
11969 /* Assume that the condition occupies the remainder of the arg
11970 string. */
c906108c 11971 (*arg) += strlen (cond_string);
c5aa993b 11972
c906108c
SS
11973 return cond_string;
11974}
c5aa993b 11975
c906108c
SS
11976/* Commands to deal with catching events, such as signals, exceptions,
11977 process start/exit, etc. */
c5aa993b
JM
11978
11979typedef enum
11980{
44feb3ce
TT
11981 catch_fork_temporary, catch_vfork_temporary,
11982 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11983}
11984catch_fork_kind;
11985
c906108c 11986static void
cc59ec59
MS
11987catch_fork_command_1 (char *arg, int from_tty,
11988 struct cmd_list_element *command)
c906108c 11989{
a6d9a66e 11990 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11991 char *cond_string = NULL;
44feb3ce
TT
11992 catch_fork_kind fork_kind;
11993 int tempflag;
11994
11995 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11996 tempflag = (fork_kind == catch_fork_temporary
11997 || fork_kind == catch_vfork_temporary);
c5aa993b 11998
44feb3ce
TT
11999 if (!arg)
12000 arg = "";
e9cafbcc 12001 arg = skip_spaces (arg);
c5aa993b 12002
c906108c 12003 /* The allowed syntax is:
c5aa993b
JM
12004 catch [v]fork
12005 catch [v]fork if <cond>
12006
4a64f543 12007 First, check if there's an if clause. */
c906108c 12008 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 12009
c906108c 12010 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12011 error (_("Junk at end of arguments."));
c5aa993b 12012
c906108c 12013 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 12014 and enable reporting of such events. */
c5aa993b
JM
12015 switch (fork_kind)
12016 {
44feb3ce
TT
12017 case catch_fork_temporary:
12018 case catch_fork_permanent:
a6d9a66e 12019 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12020 &catch_fork_breakpoint_ops);
c906108c 12021 break;
44feb3ce
TT
12022 case catch_vfork_temporary:
12023 case catch_vfork_permanent:
a6d9a66e 12024 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12025 &catch_vfork_breakpoint_ops);
c906108c 12026 break;
c5aa993b 12027 default:
8a3fe4f8 12028 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 12029 break;
c5aa993b 12030 }
c906108c
SS
12031}
12032
12033static void
cc59ec59
MS
12034catch_exec_command_1 (char *arg, int from_tty,
12035 struct cmd_list_element *command)
c906108c 12036{
b4d90040 12037 struct exec_catchpoint *c;
a6d9a66e 12038 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 12039 int tempflag;
c5aa993b 12040 char *cond_string = NULL;
c906108c 12041
44feb3ce
TT
12042 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12043
12044 if (!arg)
12045 arg = "";
e9cafbcc 12046 arg = skip_spaces (arg);
c906108c
SS
12047
12048 /* The allowed syntax is:
c5aa993b
JM
12049 catch exec
12050 catch exec if <cond>
c906108c 12051
4a64f543 12052 First, check if there's an if clause. */
c906108c
SS
12053 cond_string = ep_parse_optional_if_clause (&arg);
12054
12055 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12056 error (_("Junk at end of arguments."));
c906108c 12057
b4d90040
PA
12058 c = XNEW (struct exec_catchpoint);
12059 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
12060 &catch_exec_breakpoint_ops);
12061 c->exec_pathname = NULL;
12062
3ea46bff 12063 install_breakpoint (0, &c->base, 1);
c906108c 12064}
c5aa993b 12065
9ac4176b 12066void
28010a5d
PA
12067init_ada_exception_breakpoint (struct breakpoint *b,
12068 struct gdbarch *gdbarch,
12069 struct symtab_and_line sal,
12070 char *addr_string,
c0a91b2b 12071 const struct breakpoint_ops *ops,
28010a5d 12072 int tempflag,
349774ef 12073 int enabled,
28010a5d 12074 int from_tty)
f7f9143b 12075{
f7f9143b
JB
12076 if (from_tty)
12077 {
5af949e3
UW
12078 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12079 if (!loc_gdbarch)
12080 loc_gdbarch = gdbarch;
12081
6c95b8df
PA
12082 describe_other_breakpoints (loc_gdbarch,
12083 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
12084 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12085 version for exception catchpoints, because two catchpoints
12086 used for different exception names will use the same address.
12087 In this case, a "breakpoint ... also set at..." warning is
4a64f543 12088 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 12089 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
12090 the user what type of catchpoint it is. The above is good
12091 enough for now, though. */
12092 }
12093
28010a5d 12094 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 12095
349774ef 12096 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 12097 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
12098 b->addr_string = addr_string;
12099 b->language = language_ada;
f7f9143b
JB
12100}
12101
a96d9b2e
SDJ
12102/* Splits the argument using space as delimiter. Returns an xmalloc'd
12103 filter list, or NULL if no filtering is required. */
12104static VEC(int) *
12105catch_syscall_split_args (char *arg)
12106{
12107 VEC(int) *result = NULL;
29d0bb3d 12108 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
458c8db8 12109 struct gdbarch *gdbarch = target_gdbarch ();
a96d9b2e
SDJ
12110
12111 while (*arg != '\0')
12112 {
12113 int i, syscall_number;
12114 char *endptr;
12115 char cur_name[128];
12116 struct syscall s;
12117
12118 /* Skip whitespace. */
529480d0 12119 arg = skip_spaces (arg);
a96d9b2e
SDJ
12120
12121 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
12122 cur_name[i] = arg[i];
12123 cur_name[i] = '\0';
12124 arg += i;
12125
12126 /* Check if the user provided a syscall name or a number. */
12127 syscall_number = (int) strtol (cur_name, &endptr, 0);
12128 if (*endptr == '\0')
458c8db8 12129 get_syscall_by_number (gdbarch, syscall_number, &s);
a96d9b2e
SDJ
12130 else
12131 {
12132 /* We have a name. Let's check if it's valid and convert it
12133 to a number. */
458c8db8 12134 get_syscall_by_name (gdbarch, cur_name, &s);
a96d9b2e
SDJ
12135
12136 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
12137 /* Here we have to issue an error instead of a warning,
12138 because GDB cannot do anything useful if there's no
12139 syscall number to be caught. */
a96d9b2e
SDJ
12140 error (_("Unknown syscall name '%s'."), cur_name);
12141 }
12142
12143 /* Ok, it's valid. */
12144 VEC_safe_push (int, result, s.number);
12145 }
12146
12147 discard_cleanups (cleanup);
12148 return result;
12149}
12150
12151/* Implement the "catch syscall" command. */
12152
12153static void
cc59ec59
MS
12154catch_syscall_command_1 (char *arg, int from_tty,
12155 struct cmd_list_element *command)
a96d9b2e
SDJ
12156{
12157 int tempflag;
12158 VEC(int) *filter;
12159 struct syscall s;
12160 struct gdbarch *gdbarch = get_current_arch ();
12161
12162 /* Checking if the feature if supported. */
12163 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
12164 error (_("The feature 'catch syscall' is not supported on \
ea666128 12165this architecture yet."));
a96d9b2e
SDJ
12166
12167 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12168
e9cafbcc 12169 arg = skip_spaces (arg);
a96d9b2e
SDJ
12170
12171 /* We need to do this first "dummy" translation in order
12172 to get the syscall XML file loaded or, most important,
12173 to display a warning to the user if there's no XML file
12174 for his/her architecture. */
458c8db8 12175 get_syscall_by_number (gdbarch, 0, &s);
a96d9b2e
SDJ
12176
12177 /* The allowed syntax is:
12178 catch syscall
12179 catch syscall <name | number> [<name | number> ... <name | number>]
12180
12181 Let's check if there's a syscall name. */
12182
12183 if (arg != NULL)
12184 filter = catch_syscall_split_args (arg);
12185 else
12186 filter = NULL;
12187
12188 create_syscall_event_catchpoint (tempflag, filter,
12189 &catch_syscall_breakpoint_ops);
12190}
12191
c906108c 12192static void
fba45db2 12193catch_command (char *arg, int from_tty)
c906108c 12194{
44feb3ce 12195 error (_("Catch requires an event name."));
c906108c
SS
12196}
12197\f
12198
12199static void
fba45db2 12200tcatch_command (char *arg, int from_tty)
c906108c 12201{
44feb3ce 12202 error (_("Catch requires an event name."));
c906108c
SS
12203}
12204
8a2c437b
TT
12205/* A qsort comparison function that sorts breakpoints in order. */
12206
12207static int
12208compare_breakpoints (const void *a, const void *b)
12209{
12210 const breakpoint_p *ba = a;
12211 uintptr_t ua = (uintptr_t) *ba;
12212 const breakpoint_p *bb = b;
12213 uintptr_t ub = (uintptr_t) *bb;
12214
12215 if ((*ba)->number < (*bb)->number)
12216 return -1;
12217 else if ((*ba)->number > (*bb)->number)
12218 return 1;
12219
12220 /* Now sort by address, in case we see, e..g, two breakpoints with
12221 the number 0. */
12222 if (ua < ub)
12223 return -1;
94b0e70d 12224 return ua > ub ? 1 : 0;
8a2c437b
TT
12225}
12226
80f8a6eb 12227/* Delete breakpoints by address or line. */
c906108c
SS
12228
12229static void
fba45db2 12230clear_command (char *arg, int from_tty)
c906108c 12231{
8a2c437b 12232 struct breakpoint *b, *prev;
d6e956e5
VP
12233 VEC(breakpoint_p) *found = 0;
12234 int ix;
c906108c
SS
12235 int default_match;
12236 struct symtabs_and_lines sals;
12237 struct symtab_and_line sal;
c906108c 12238 int i;
8a2c437b 12239 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12240
12241 if (arg)
12242 {
39cf75f7
DE
12243 sals = decode_line_with_current_source (arg,
12244 (DECODE_LINE_FUNFIRSTLINE
12245 | DECODE_LINE_LIST_MODE));
cf4ded82 12246 make_cleanup (xfree, sals.sals);
c906108c
SS
12247 default_match = 0;
12248 }
12249 else
12250 {
c5aa993b 12251 sals.sals = (struct symtab_and_line *)
c906108c 12252 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 12253 make_cleanup (xfree, sals.sals);
4a64f543 12254 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12255
12256 /* Set sal's line, symtab, pc, and pspace to the values
12257 corresponding to the last call to print_frame_info. If the
12258 codepoint is not valid, this will set all the fields to 0. */
12259 get_last_displayed_sal (&sal);
c906108c 12260 if (sal.symtab == 0)
8a3fe4f8 12261 error (_("No source file specified."));
c906108c
SS
12262
12263 sals.sals[0] = sal;
12264 sals.nelts = 1;
12265
12266 default_match = 1;
12267 }
12268
4a64f543
MS
12269 /* We don't call resolve_sal_pc here. That's not as bad as it
12270 seems, because all existing breakpoints typically have both
12271 file/line and pc set. So, if clear is given file/line, we can
12272 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12273
12274 We only support clearing given the address explicitly
12275 present in breakpoint table. Say, we've set breakpoint
4a64f543 12276 at file:line. There were several PC values for that file:line,
ed0616c6 12277 due to optimization, all in one block.
4a64f543
MS
12278
12279 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12280 PC corresponding to the same file:line, the breakpoint won't
12281 be cleared. We probably can still clear the breakpoint, but
12282 since the other PC value is never presented to user, user
12283 can only find it by guessing, and it does not seem important
12284 to support that. */
12285
4a64f543
MS
12286 /* For each line spec given, delete bps which correspond to it. Do
12287 it in two passes, solely to preserve the current behavior that
12288 from_tty is forced true if we delete more than one
12289 breakpoint. */
c906108c 12290
80f8a6eb 12291 found = NULL;
8a2c437b 12292 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12293 for (i = 0; i < sals.nelts; i++)
12294 {
05cba821
JK
12295 const char *sal_fullname;
12296
c906108c 12297 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12298 If line given (pc == 0), clear all bpts on specified line.
12299 If defaulting, clear all bpts on default line
c906108c 12300 or at default pc.
c5aa993b
JM
12301
12302 defaulting sal.pc != 0 tests to do
12303
12304 0 1 pc
12305 1 1 pc _and_ line
12306 0 0 line
12307 1 0 <can't happen> */
c906108c
SS
12308
12309 sal = sals.sals[i];
05cba821
JK
12310 sal_fullname = (sal.symtab == NULL
12311 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12312
4a64f543 12313 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12314 ALL_BREAKPOINTS (b)
c5aa993b 12315 {
0d381245 12316 int match = 0;
4a64f543 12317 /* Are we going to delete b? */
cc60f2e3 12318 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12319 {
12320 struct bp_location *loc = b->loc;
12321 for (; loc; loc = loc->next)
12322 {
f8eba3c6
TT
12323 /* If the user specified file:line, don't allow a PC
12324 match. This matches historical gdb behavior. */
12325 int pc_match = (!sal.explicit_line
12326 && sal.pc
12327 && (loc->pspace == sal.pspace)
12328 && (loc->address == sal.pc)
12329 && (!section_is_overlay (loc->section)
12330 || loc->section == sal.section));
4aac40c8
TT
12331 int line_match = 0;
12332
12333 if ((default_match || sal.explicit_line)
2f202fde 12334 && loc->symtab != NULL
05cba821 12335 && sal_fullname != NULL
4aac40c8 12336 && sal.pspace == loc->pspace
05cba821
JK
12337 && loc->line_number == sal.line
12338 && filename_cmp (symtab_to_fullname (loc->symtab),
12339 sal_fullname) == 0)
12340 line_match = 1;
4aac40c8 12341
0d381245
VP
12342 if (pc_match || line_match)
12343 {
12344 match = 1;
12345 break;
12346 }
12347 }
12348 }
12349
12350 if (match)
d6e956e5 12351 VEC_safe_push(breakpoint_p, found, b);
c906108c 12352 }
80f8a6eb 12353 }
8a2c437b 12354
80f8a6eb 12355 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12356 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12357 {
12358 if (arg)
8a3fe4f8 12359 error (_("No breakpoint at %s."), arg);
80f8a6eb 12360 else
8a3fe4f8 12361 error (_("No breakpoint at this line."));
80f8a6eb 12362 }
c906108c 12363
8a2c437b
TT
12364 /* Remove duplicates from the vec. */
12365 qsort (VEC_address (breakpoint_p, found),
12366 VEC_length (breakpoint_p, found),
12367 sizeof (breakpoint_p),
12368 compare_breakpoints);
12369 prev = VEC_index (breakpoint_p, found, 0);
12370 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12371 {
12372 if (b == prev)
12373 {
12374 VEC_ordered_remove (breakpoint_p, found, ix);
12375 --ix;
12376 }
12377 }
12378
d6e956e5 12379 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12380 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12381 if (from_tty)
a3f17187 12382 {
d6e956e5 12383 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12384 printf_unfiltered (_("Deleted breakpoint "));
12385 else
12386 printf_unfiltered (_("Deleted breakpoints "));
12387 }
d6e956e5
VP
12388
12389 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12390 {
c5aa993b 12391 if (from_tty)
d6e956e5
VP
12392 printf_unfiltered ("%d ", b->number);
12393 delete_breakpoint (b);
c906108c 12394 }
80f8a6eb
MS
12395 if (from_tty)
12396 putchar_unfiltered ('\n');
8a2c437b
TT
12397
12398 do_cleanups (cleanups);
c906108c
SS
12399}
12400\f
12401/* Delete breakpoint in BS if they are `delete' breakpoints and
12402 all breakpoints that are marked for deletion, whether hit or not.
12403 This is called after any breakpoint is hit, or after errors. */
12404
12405void
fba45db2 12406breakpoint_auto_delete (bpstat bs)
c906108c 12407{
35df4500 12408 struct breakpoint *b, *b_tmp;
c906108c
SS
12409
12410 for (; bs; bs = bs->next)
f431efe5
PA
12411 if (bs->breakpoint_at
12412 && bs->breakpoint_at->disposition == disp_del
c906108c 12413 && bs->stop)
f431efe5 12414 delete_breakpoint (bs->breakpoint_at);
c906108c 12415
35df4500 12416 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12417 {
b5de0fa7 12418 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12419 delete_breakpoint (b);
12420 }
c906108c
SS
12421}
12422
4a64f543
MS
12423/* A comparison function for bp_location AP and BP being interfaced to
12424 qsort. Sort elements primarily by their ADDRESS (no matter what
12425 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12426 secondarily by ordering first permanent elements and
4a64f543 12427 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12428 qsort being an unstable algorithm. */
876fa593
JK
12429
12430static int
494cfb0f 12431bp_location_compare (const void *ap, const void *bp)
876fa593 12432{
494cfb0f
JK
12433 struct bp_location *a = *(void **) ap;
12434 struct bp_location *b = *(void **) bp;
876fa593
JK
12435
12436 if (a->address != b->address)
12437 return (a->address > b->address) - (a->address < b->address);
12438
dea2aa5f
LM
12439 /* Sort locations at the same address by their pspace number, keeping
12440 locations of the same inferior (in a multi-inferior environment)
12441 grouped. */
12442
12443 if (a->pspace->num != b->pspace->num)
12444 return ((a->pspace->num > b->pspace->num)
12445 - (a->pspace->num < b->pspace->num));
12446
876fa593 12447 /* Sort permanent breakpoints first. */
1a853c52
PA
12448 if (a->permanent != b->permanent)
12449 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12450
c56a97f9
JK
12451 /* Make the internal GDB representation stable across GDB runs
12452 where A and B memory inside GDB can differ. Breakpoint locations of
12453 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12454
12455 if (a->owner->number != b->owner->number)
c56a97f9
JK
12456 return ((a->owner->number > b->owner->number)
12457 - (a->owner->number < b->owner->number));
876fa593
JK
12458
12459 return (a > b) - (a < b);
12460}
12461
876fa593 12462/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12463 bp_location_shadow_len_after_address_max according to the current
12464 content of the bp_location array. */
f7545552
TT
12465
12466static void
876fa593 12467bp_location_target_extensions_update (void)
f7545552 12468{
876fa593
JK
12469 struct bp_location *bl, **blp_tmp;
12470
12471 bp_location_placed_address_before_address_max = 0;
12472 bp_location_shadow_len_after_address_max = 0;
12473
12474 ALL_BP_LOCATIONS (bl, blp_tmp)
12475 {
12476 CORE_ADDR start, end, addr;
12477
12478 if (!bp_location_has_shadow (bl))
12479 continue;
12480
12481 start = bl->target_info.placed_address;
12482 end = start + bl->target_info.shadow_len;
12483
12484 gdb_assert (bl->address >= start);
12485 addr = bl->address - start;
12486 if (addr > bp_location_placed_address_before_address_max)
12487 bp_location_placed_address_before_address_max = addr;
12488
12489 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12490
12491 gdb_assert (bl->address < end);
12492 addr = end - bl->address;
12493 if (addr > bp_location_shadow_len_after_address_max)
12494 bp_location_shadow_len_after_address_max = addr;
12495 }
f7545552
TT
12496}
12497
1e4d1764
YQ
12498/* Download tracepoint locations if they haven't been. */
12499
12500static void
12501download_tracepoint_locations (void)
12502{
7ed2c994 12503 struct breakpoint *b;
1e4d1764
YQ
12504 struct cleanup *old_chain;
12505
12506 if (!target_can_download_tracepoint ())
12507 return;
12508
12509 old_chain = save_current_space_and_thread ();
12510
7ed2c994 12511 ALL_TRACEPOINTS (b)
1e4d1764 12512 {
7ed2c994 12513 struct bp_location *bl;
1e4d1764 12514 struct tracepoint *t;
f2a8bc8a 12515 int bp_location_downloaded = 0;
1e4d1764 12516
7ed2c994 12517 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12518 ? !may_insert_fast_tracepoints
12519 : !may_insert_tracepoints))
12520 continue;
12521
7ed2c994
YQ
12522 for (bl = b->loc; bl; bl = bl->next)
12523 {
12524 /* In tracepoint, locations are _never_ duplicated, so
12525 should_be_inserted is equivalent to
12526 unduplicated_should_be_inserted. */
12527 if (!should_be_inserted (bl) || bl->inserted)
12528 continue;
1e4d1764 12529
7ed2c994 12530 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12531
7ed2c994 12532 target_download_tracepoint (bl);
1e4d1764 12533
7ed2c994 12534 bl->inserted = 1;
f2a8bc8a 12535 bp_location_downloaded = 1;
7ed2c994
YQ
12536 }
12537 t = (struct tracepoint *) b;
12538 t->number_on_target = b->number;
f2a8bc8a
YQ
12539 if (bp_location_downloaded)
12540 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12541 }
12542
12543 do_cleanups (old_chain);
12544}
12545
934709f0
PW
12546/* Swap the insertion/duplication state between two locations. */
12547
12548static void
12549swap_insertion (struct bp_location *left, struct bp_location *right)
12550{
12551 const int left_inserted = left->inserted;
12552 const int left_duplicate = left->duplicate;
b775012e 12553 const int left_needs_update = left->needs_update;
934709f0
PW
12554 const struct bp_target_info left_target_info = left->target_info;
12555
1e4d1764
YQ
12556 /* Locations of tracepoints can never be duplicated. */
12557 if (is_tracepoint (left->owner))
12558 gdb_assert (!left->duplicate);
12559 if (is_tracepoint (right->owner))
12560 gdb_assert (!right->duplicate);
12561
934709f0
PW
12562 left->inserted = right->inserted;
12563 left->duplicate = right->duplicate;
b775012e 12564 left->needs_update = right->needs_update;
934709f0
PW
12565 left->target_info = right->target_info;
12566 right->inserted = left_inserted;
12567 right->duplicate = left_duplicate;
b775012e 12568 right->needs_update = left_needs_update;
934709f0
PW
12569 right->target_info = left_target_info;
12570}
12571
b775012e
LM
12572/* Force the re-insertion of the locations at ADDRESS. This is called
12573 once a new/deleted/modified duplicate location is found and we are evaluating
12574 conditions on the target's side. Such conditions need to be updated on
12575 the target. */
12576
12577static void
12578force_breakpoint_reinsertion (struct bp_location *bl)
12579{
12580 struct bp_location **locp = NULL, **loc2p;
12581 struct bp_location *loc;
12582 CORE_ADDR address = 0;
12583 int pspace_num;
12584
12585 address = bl->address;
12586 pspace_num = bl->pspace->num;
12587
12588 /* This is only meaningful if the target is
12589 evaluating conditions and if the user has
12590 opted for condition evaluation on the target's
12591 side. */
12592 if (gdb_evaluates_breakpoint_condition_p ()
12593 || !target_supports_evaluation_of_breakpoint_conditions ())
12594 return;
12595
12596 /* Flag all breakpoint locations with this address and
12597 the same program space as the location
12598 as "its condition has changed". We need to
12599 update the conditions on the target's side. */
12600 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12601 {
12602 loc = *loc2p;
12603
12604 if (!is_breakpoint (loc->owner)
12605 || pspace_num != loc->pspace->num)
12606 continue;
12607
12608 /* Flag the location appropriately. We use a different state to
12609 let everyone know that we already updated the set of locations
12610 with addr bl->address and program space bl->pspace. This is so
12611 we don't have to keep calling these functions just to mark locations
12612 that have already been marked. */
12613 loc->condition_changed = condition_updated;
12614
12615 /* Free the agent expression bytecode as well. We will compute
12616 it later on. */
12617 if (loc->cond_bytecode)
12618 {
12619 free_agent_expr (loc->cond_bytecode);
12620 loc->cond_bytecode = NULL;
12621 }
12622 }
12623}
44702360
PA
12624/* Called whether new breakpoints are created, or existing breakpoints
12625 deleted, to update the global location list and recompute which
12626 locations are duplicate of which.
b775012e 12627
04086b45
PA
12628 The INSERT_MODE flag determines whether locations may not, may, or
12629 shall be inserted now. See 'enum ugll_insert_mode' for more
12630 info. */
b60e7edf 12631
0d381245 12632static void
44702360 12633update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12634{
74960c60 12635 struct breakpoint *b;
876fa593 12636 struct bp_location **locp, *loc;
f7545552 12637 struct cleanup *cleanups;
b775012e
LM
12638 /* Last breakpoint location address that was marked for update. */
12639 CORE_ADDR last_addr = 0;
12640 /* Last breakpoint location program space that was marked for update. */
12641 int last_pspace_num = -1;
f7545552 12642
2d134ed3
PA
12643 /* Used in the duplicates detection below. When iterating over all
12644 bp_locations, points to the first bp_location of a given address.
12645 Breakpoints and watchpoints of different types are never
12646 duplicates of each other. Keep one pointer for each type of
12647 breakpoint/watchpoint, so we only need to loop over all locations
12648 once. */
12649 struct bp_location *bp_loc_first; /* breakpoint */
12650 struct bp_location *wp_loc_first; /* hardware watchpoint */
12651 struct bp_location *awp_loc_first; /* access watchpoint */
12652 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12653
4a64f543
MS
12654 /* Saved former bp_location array which we compare against the newly
12655 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12656 struct bp_location **old_location, **old_locp;
12657 unsigned old_location_count;
12658
12659 old_location = bp_location;
12660 old_location_count = bp_location_count;
12661 bp_location = NULL;
12662 bp_location_count = 0;
12663 cleanups = make_cleanup (xfree, old_location);
0d381245 12664
74960c60 12665 ALL_BREAKPOINTS (b)
876fa593
JK
12666 for (loc = b->loc; loc; loc = loc->next)
12667 bp_location_count++;
12668
12669 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12670 locp = bp_location;
12671 ALL_BREAKPOINTS (b)
12672 for (loc = b->loc; loc; loc = loc->next)
12673 *locp++ = loc;
12674 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12675 bp_location_compare);
876fa593
JK
12676
12677 bp_location_target_extensions_update ();
74960c60 12678
4a64f543
MS
12679 /* Identify bp_location instances that are no longer present in the
12680 new list, and therefore should be freed. Note that it's not
12681 necessary that those locations should be removed from inferior --
12682 if there's another location at the same address (previously
12683 marked as duplicate), we don't need to remove/insert the
12684 location.
876fa593 12685
4a64f543
MS
12686 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12687 and former bp_location array state respectively. */
876fa593
JK
12688
12689 locp = bp_location;
12690 for (old_locp = old_location; old_locp < old_location + old_location_count;
12691 old_locp++)
74960c60 12692 {
876fa593 12693 struct bp_location *old_loc = *old_locp;
c7d46a38 12694 struct bp_location **loc2p;
876fa593 12695
e5dd4106 12696 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12697 not, we have to free it. */
c7d46a38 12698 int found_object = 0;
20874c92
VP
12699 /* Tells if the location should remain inserted in the target. */
12700 int keep_in_target = 0;
12701 int removed = 0;
876fa593 12702
4a64f543
MS
12703 /* Skip LOCP entries which will definitely never be needed.
12704 Stop either at or being the one matching OLD_LOC. */
876fa593 12705 while (locp < bp_location + bp_location_count
c7d46a38 12706 && (*locp)->address < old_loc->address)
876fa593 12707 locp++;
c7d46a38
PA
12708
12709 for (loc2p = locp;
12710 (loc2p < bp_location + bp_location_count
12711 && (*loc2p)->address == old_loc->address);
12712 loc2p++)
12713 {
b775012e
LM
12714 /* Check if this is a new/duplicated location or a duplicated
12715 location that had its condition modified. If so, we want to send
12716 its condition to the target if evaluation of conditions is taking
12717 place there. */
12718 if ((*loc2p)->condition_changed == condition_modified
12719 && (last_addr != old_loc->address
12720 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12721 {
b775012e
LM
12722 force_breakpoint_reinsertion (*loc2p);
12723 last_pspace_num = old_loc->pspace->num;
c7d46a38 12724 }
b775012e
LM
12725
12726 if (*loc2p == old_loc)
12727 found_object = 1;
c7d46a38 12728 }
74960c60 12729
b775012e
LM
12730 /* We have already handled this address, update it so that we don't
12731 have to go through updates again. */
12732 last_addr = old_loc->address;
12733
12734 /* Target-side condition evaluation: Handle deleted locations. */
12735 if (!found_object)
12736 force_breakpoint_reinsertion (old_loc);
12737
4a64f543
MS
12738 /* If this location is no longer present, and inserted, look if
12739 there's maybe a new location at the same address. If so,
12740 mark that one inserted, and don't remove this one. This is
12741 needed so that we don't have a time window where a breakpoint
12742 at certain location is not inserted. */
74960c60 12743
876fa593 12744 if (old_loc->inserted)
0d381245 12745 {
4a64f543
MS
12746 /* If the location is inserted now, we might have to remove
12747 it. */
74960c60 12748
876fa593 12749 if (found_object && should_be_inserted (old_loc))
74960c60 12750 {
4a64f543
MS
12751 /* The location is still present in the location list,
12752 and still should be inserted. Don't do anything. */
20874c92 12753 keep_in_target = 1;
74960c60
VP
12754 }
12755 else
12756 {
b775012e
LM
12757 /* This location still exists, but it won't be kept in the
12758 target since it may have been disabled. We proceed to
12759 remove its target-side condition. */
12760
4a64f543
MS
12761 /* The location is either no longer present, or got
12762 disabled. See if there's another location at the
12763 same address, in which case we don't need to remove
12764 this one from the target. */
876fa593 12765
2bdf28a0 12766 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12767 if (breakpoint_address_is_meaningful (old_loc->owner))
12768 {
876fa593 12769 for (loc2p = locp;
c7d46a38
PA
12770 (loc2p < bp_location + bp_location_count
12771 && (*loc2p)->address == old_loc->address);
876fa593
JK
12772 loc2p++)
12773 {
12774 struct bp_location *loc2 = *loc2p;
12775
2d134ed3 12776 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12777 {
85d721b8
PA
12778 /* Read watchpoint locations are switched to
12779 access watchpoints, if the former are not
12780 supported, but the latter are. */
12781 if (is_hardware_watchpoint (old_loc->owner))
12782 {
12783 gdb_assert (is_hardware_watchpoint (loc2->owner));
12784 loc2->watchpoint_type = old_loc->watchpoint_type;
12785 }
12786
934709f0
PW
12787 /* loc2 is a duplicated location. We need to check
12788 if it should be inserted in case it will be
12789 unduplicated. */
12790 if (loc2 != old_loc
12791 && unduplicated_should_be_inserted (loc2))
c7d46a38 12792 {
934709f0 12793 swap_insertion (old_loc, loc2);
c7d46a38
PA
12794 keep_in_target = 1;
12795 break;
12796 }
876fa593
JK
12797 }
12798 }
12799 }
74960c60
VP
12800 }
12801
20874c92
VP
12802 if (!keep_in_target)
12803 {
876fa593 12804 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12805 {
4a64f543
MS
12806 /* This is just about all we can do. We could keep
12807 this location on the global list, and try to
12808 remove it next time, but there's no particular
12809 reason why we will succeed next time.
20874c92 12810
4a64f543
MS
12811 Note that at this point, old_loc->owner is still
12812 valid, as delete_breakpoint frees the breakpoint
12813 only after calling us. */
3e43a32a
MS
12814 printf_filtered (_("warning: Error removing "
12815 "breakpoint %d\n"),
876fa593 12816 old_loc->owner->number);
20874c92
VP
12817 }
12818 removed = 1;
12819 }
0d381245 12820 }
74960c60
VP
12821
12822 if (!found_object)
1c5cfe86 12823 {
db82e815 12824 if (removed && non_stop
1cf4d951 12825 && need_moribund_for_location_type (old_loc))
20874c92 12826 {
db82e815
PA
12827 /* This location was removed from the target. In
12828 non-stop mode, a race condition is possible where
12829 we've removed a breakpoint, but stop events for that
12830 breakpoint are already queued and will arrive later.
12831 We apply an heuristic to be able to distinguish such
12832 SIGTRAPs from other random SIGTRAPs: we keep this
12833 breakpoint location for a bit, and will retire it
12834 after we see some number of events. The theory here
12835 is that reporting of events should, "on the average",
12836 be fair, so after a while we'll see events from all
12837 threads that have anything of interest, and no longer
12838 need to keep this breakpoint location around. We
12839 don't hold locations forever so to reduce chances of
12840 mistaking a non-breakpoint SIGTRAP for a breakpoint
12841 SIGTRAP.
12842
12843 The heuristic failing can be disastrous on
12844 decr_pc_after_break targets.
12845
12846 On decr_pc_after_break targets, like e.g., x86-linux,
12847 if we fail to recognize a late breakpoint SIGTRAP,
12848 because events_till_retirement has reached 0 too
12849 soon, we'll fail to do the PC adjustment, and report
12850 a random SIGTRAP to the user. When the user resumes
12851 the inferior, it will most likely immediately crash
2dec564e 12852 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12853 corrupted, because of being resumed e.g., in the
12854 middle of a multi-byte instruction, or skipped a
12855 one-byte instruction. This was actually seen happen
12856 on native x86-linux, and should be less rare on
12857 targets that do not support new thread events, like
12858 remote, due to the heuristic depending on
12859 thread_count.
12860
12861 Mistaking a random SIGTRAP for a breakpoint trap
12862 causes similar symptoms (PC adjustment applied when
12863 it shouldn't), but then again, playing with SIGTRAPs
12864 behind the debugger's back is asking for trouble.
12865
12866 Since hardware watchpoint traps are always
12867 distinguishable from other traps, so we don't need to
12868 apply keep hardware watchpoint moribund locations
12869 around. We simply always ignore hardware watchpoint
12870 traps we can no longer explain. */
12871
876fa593
JK
12872 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12873 old_loc->owner = NULL;
20874c92 12874
876fa593 12875 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12876 }
12877 else
f431efe5
PA
12878 {
12879 old_loc->owner = NULL;
12880 decref_bp_location (&old_loc);
12881 }
20874c92 12882 }
74960c60 12883 }
1c5cfe86 12884
348d480f
PA
12885 /* Rescan breakpoints at the same address and section, marking the
12886 first one as "first" and any others as "duplicates". This is so
12887 that the bpt instruction is only inserted once. If we have a
12888 permanent breakpoint at the same place as BPT, make that one the
12889 official one, and the rest as duplicates. Permanent breakpoints
12890 are sorted first for the same address.
12891
12892 Do the same for hardware watchpoints, but also considering the
12893 watchpoint's type (regular/access/read) and length. */
12894
12895 bp_loc_first = NULL;
12896 wp_loc_first = NULL;
12897 awp_loc_first = NULL;
12898 rwp_loc_first = NULL;
12899 ALL_BP_LOCATIONS (loc, locp)
12900 {
12901 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12902 non-NULL. */
348d480f 12903 struct bp_location **loc_first_p;
d3fbdd86 12904 b = loc->owner;
348d480f 12905
6f380991 12906 if (!unduplicated_should_be_inserted (loc)
348d480f 12907 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12908 /* Don't detect duplicate for tracepoint locations because they are
12909 never duplicated. See the comments in field `duplicate' of
12910 `struct bp_location'. */
348d480f 12911 || is_tracepoint (b))
b775012e
LM
12912 {
12913 /* Clear the condition modification flag. */
12914 loc->condition_changed = condition_unchanged;
12915 continue;
12916 }
348d480f
PA
12917
12918 /* Permanent breakpoint should always be inserted. */
1a853c52 12919 if (loc->permanent && ! loc->inserted)
348d480f
PA
12920 internal_error (__FILE__, __LINE__,
12921 _("allegedly permanent breakpoint is not "
12922 "actually inserted"));
12923
12924 if (b->type == bp_hardware_watchpoint)
12925 loc_first_p = &wp_loc_first;
12926 else if (b->type == bp_read_watchpoint)
12927 loc_first_p = &rwp_loc_first;
12928 else if (b->type == bp_access_watchpoint)
12929 loc_first_p = &awp_loc_first;
12930 else
12931 loc_first_p = &bp_loc_first;
12932
12933 if (*loc_first_p == NULL
12934 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12935 || !breakpoint_locations_match (loc, *loc_first_p))
12936 {
12937 *loc_first_p = loc;
12938 loc->duplicate = 0;
b775012e
LM
12939
12940 if (is_breakpoint (loc->owner) && loc->condition_changed)
12941 {
12942 loc->needs_update = 1;
12943 /* Clear the condition modification flag. */
12944 loc->condition_changed = condition_unchanged;
12945 }
348d480f
PA
12946 continue;
12947 }
12948
934709f0
PW
12949
12950 /* This and the above ensure the invariant that the first location
12951 is not duplicated, and is the inserted one.
12952 All following are marked as duplicated, and are not inserted. */
12953 if (loc->inserted)
12954 swap_insertion (loc, *loc_first_p);
348d480f
PA
12955 loc->duplicate = 1;
12956
b775012e
LM
12957 /* Clear the condition modification flag. */
12958 loc->condition_changed = condition_unchanged;
12959
1a853c52
PA
12960 if (loc->inserted && !loc->permanent
12961 && (*loc_first_p)->permanent)
348d480f
PA
12962 internal_error (__FILE__, __LINE__,
12963 _("another breakpoint was inserted on top of "
12964 "a permanent breakpoint"));
12965 }
12966
a25a5a45 12967 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12968 {
04086b45 12969 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12970 insert_breakpoint_locations ();
12971 else
12972 {
44702360
PA
12973 /* Even though the caller told us to not insert new
12974 locations, we may still need to update conditions on the
12975 target's side of breakpoints that were already inserted
12976 if the target is evaluating breakpoint conditions. We
b775012e
LM
12977 only update conditions for locations that are marked
12978 "needs_update". */
12979 update_inserted_breakpoint_locations ();
12980 }
12981 }
348d480f 12982
04086b45 12983 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12984 download_tracepoint_locations ();
12985
348d480f
PA
12986 do_cleanups (cleanups);
12987}
12988
12989void
12990breakpoint_retire_moribund (void)
12991{
12992 struct bp_location *loc;
12993 int ix;
12994
12995 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12996 if (--(loc->events_till_retirement) == 0)
12997 {
12998 decref_bp_location (&loc);
12999 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
13000 --ix;
13001 }
13002}
13003
13004static void
44702360 13005update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 13006{
348d480f 13007
492d29ea
PA
13008 TRY
13009 {
13010 update_global_location_list (insert_mode);
13011 }
13012 CATCH (e, RETURN_MASK_ERROR)
13013 {
13014 }
13015 END_CATCH
348d480f
PA
13016}
13017
13018/* Clear BKP from a BPS. */
13019
13020static void
13021bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
13022{
13023 bpstat bs;
13024
13025 for (bs = bps; bs; bs = bs->next)
13026 if (bs->breakpoint_at == bpt)
13027 {
13028 bs->breakpoint_at = NULL;
13029 bs->old_val = NULL;
13030 /* bs->commands will be freed later. */
13031 }
13032}
13033
13034/* Callback for iterate_over_threads. */
13035static int
13036bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
13037{
13038 struct breakpoint *bpt = data;
13039
13040 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
13041 return 0;
13042}
13043
13044/* Helper for breakpoint and tracepoint breakpoint_ops->mention
13045 callbacks. */
13046
13047static void
13048say_where (struct breakpoint *b)
13049{
13050 struct value_print_options opts;
13051
13052 get_user_print_options (&opts);
13053
13054 /* i18n: cagney/2005-02-11: Below needs to be merged into a
13055 single string. */
13056 if (b->loc == NULL)
13057 {
13058 printf_filtered (_(" (%s) pending."), b->addr_string);
13059 }
13060 else
13061 {
2f202fde 13062 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
13063 {
13064 printf_filtered (" at ");
13065 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
13066 gdb_stdout);
13067 }
2f202fde 13068 if (b->loc->symtab != NULL)
f8eba3c6
TT
13069 {
13070 /* If there is a single location, we can print the location
13071 more nicely. */
13072 if (b->loc->next == NULL)
13073 printf_filtered (": file %s, line %d.",
05cba821
JK
13074 symtab_to_filename_for_display (b->loc->symtab),
13075 b->loc->line_number);
f8eba3c6
TT
13076 else
13077 /* This is not ideal, but each location may have a
13078 different file name, and this at least reflects the
13079 real situation somewhat. */
13080 printf_filtered (": %s.", b->addr_string);
13081 }
348d480f
PA
13082
13083 if (b->loc->next)
13084 {
13085 struct bp_location *loc = b->loc;
13086 int n = 0;
13087 for (; loc; loc = loc->next)
13088 ++n;
13089 printf_filtered (" (%d locations)", n);
13090 }
13091 }
13092}
13093
348d480f
PA
13094/* Default bp_location_ops methods. */
13095
13096static void
13097bp_location_dtor (struct bp_location *self)
13098{
13099 xfree (self->cond);
b775012e
LM
13100 if (self->cond_bytecode)
13101 free_agent_expr (self->cond_bytecode);
348d480f 13102 xfree (self->function_name);
8b4f3082
PA
13103
13104 VEC_free (agent_expr_p, self->target_info.conditions);
13105 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
13106}
13107
13108static const struct bp_location_ops bp_location_ops =
13109{
13110 bp_location_dtor
13111};
13112
2060206e
PA
13113/* Default breakpoint_ops methods all breakpoint_ops ultimately
13114 inherit from. */
348d480f 13115
2060206e
PA
13116static void
13117base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
13118{
13119 decref_counted_command_line (&self->commands);
13120 xfree (self->cond_string);
fb81d016 13121 xfree (self->extra_string);
348d480f 13122 xfree (self->addr_string);
f8eba3c6 13123 xfree (self->filter);
348d480f 13124 xfree (self->addr_string_range_end);
348d480f
PA
13125}
13126
2060206e
PA
13127static struct bp_location *
13128base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
13129{
13130 struct bp_location *loc;
13131
13132 loc = XNEW (struct bp_location);
13133 init_bp_location (loc, &bp_location_ops, self);
13134 return loc;
13135}
13136
2060206e
PA
13137static void
13138base_breakpoint_re_set (struct breakpoint *b)
13139{
13140 /* Nothing to re-set. */
13141}
13142
13143#define internal_error_pure_virtual_called() \
13144 gdb_assert_not_reached ("pure virtual function called")
13145
13146static int
13147base_breakpoint_insert_location (struct bp_location *bl)
13148{
13149 internal_error_pure_virtual_called ();
13150}
13151
13152static int
13153base_breakpoint_remove_location (struct bp_location *bl)
13154{
13155 internal_error_pure_virtual_called ();
13156}
13157
13158static int
13159base_breakpoint_breakpoint_hit (const struct bp_location *bl,
13160 struct address_space *aspace,
09ac7c10
TT
13161 CORE_ADDR bp_addr,
13162 const struct target_waitstatus *ws)
2060206e
PA
13163{
13164 internal_error_pure_virtual_called ();
13165}
13166
13167static void
13168base_breakpoint_check_status (bpstat bs)
13169{
13170 /* Always stop. */
13171}
13172
13173/* A "works_in_software_mode" breakpoint_ops method that just internal
13174 errors. */
13175
13176static int
13177base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13178{
13179 internal_error_pure_virtual_called ();
13180}
13181
13182/* A "resources_needed" breakpoint_ops method that just internal
13183 errors. */
13184
13185static int
13186base_breakpoint_resources_needed (const struct bp_location *bl)
13187{
13188 internal_error_pure_virtual_called ();
13189}
13190
13191static enum print_stop_action
13192base_breakpoint_print_it (bpstat bs)
13193{
13194 internal_error_pure_virtual_called ();
13195}
13196
13197static void
13198base_breakpoint_print_one_detail (const struct breakpoint *self,
13199 struct ui_out *uiout)
13200{
13201 /* nothing */
13202}
13203
13204static void
13205base_breakpoint_print_mention (struct breakpoint *b)
13206{
13207 internal_error_pure_virtual_called ();
13208}
13209
13210static void
13211base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13212{
13213 internal_error_pure_virtual_called ();
13214}
13215
983af33b
SDJ
13216static void
13217base_breakpoint_create_sals_from_address (char **arg,
13218 struct linespec_result *canonical,
13219 enum bptype type_wanted,
13220 char *addr_start,
13221 char **copy_arg)
13222{
13223 internal_error_pure_virtual_called ();
13224}
13225
13226static void
13227base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13228 struct linespec_result *c,
983af33b 13229 char *cond_string,
e7e0cddf 13230 char *extra_string,
983af33b
SDJ
13231 enum bptype type_wanted,
13232 enum bpdisp disposition,
13233 int thread,
13234 int task, int ignore_count,
13235 const struct breakpoint_ops *o,
13236 int from_tty, int enabled,
44f238bb 13237 int internal, unsigned flags)
983af33b
SDJ
13238{
13239 internal_error_pure_virtual_called ();
13240}
13241
13242static void
13243base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13244 struct symtabs_and_lines *sals)
13245{
13246 internal_error_pure_virtual_called ();
13247}
13248
ab04a2af
TT
13249/* The default 'explains_signal' method. */
13250
47591c29 13251static int
427cd150 13252base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13253{
47591c29 13254 return 1;
ab04a2af
TT
13255}
13256
9d6e6e84
HZ
13257/* The default "after_condition_true" method. */
13258
13259static void
13260base_breakpoint_after_condition_true (struct bpstats *bs)
13261{
13262 /* Nothing to do. */
13263}
13264
ab04a2af 13265struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13266{
13267 base_breakpoint_dtor,
13268 base_breakpoint_allocate_location,
13269 base_breakpoint_re_set,
13270 base_breakpoint_insert_location,
13271 base_breakpoint_remove_location,
13272 base_breakpoint_breakpoint_hit,
13273 base_breakpoint_check_status,
13274 base_breakpoint_resources_needed,
13275 base_breakpoint_works_in_software_mode,
13276 base_breakpoint_print_it,
13277 NULL,
13278 base_breakpoint_print_one_detail,
13279 base_breakpoint_print_mention,
983af33b
SDJ
13280 base_breakpoint_print_recreate,
13281 base_breakpoint_create_sals_from_address,
13282 base_breakpoint_create_breakpoints_sal,
13283 base_breakpoint_decode_linespec,
9d6e6e84
HZ
13284 base_breakpoint_explains_signal,
13285 base_breakpoint_after_condition_true,
2060206e
PA
13286};
13287
13288/* Default breakpoint_ops methods. */
13289
13290static void
348d480f
PA
13291bkpt_re_set (struct breakpoint *b)
13292{
06edf0c0
PA
13293 /* FIXME: is this still reachable? */
13294 if (b->addr_string == NULL)
13295 {
13296 /* Anything without a string can't be re-set. */
348d480f 13297 delete_breakpoint (b);
06edf0c0 13298 return;
348d480f 13299 }
06edf0c0
PA
13300
13301 breakpoint_re_set_default (b);
348d480f
PA
13302}
13303
2060206e 13304static int
348d480f
PA
13305bkpt_insert_location (struct bp_location *bl)
13306{
13307 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 13308 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 13309 else
7c16b83e 13310 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13311}
13312
2060206e 13313static int
348d480f
PA
13314bkpt_remove_location (struct bp_location *bl)
13315{
13316 if (bl->loc_type == bp_loc_hardware_breakpoint)
13317 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13318 else
7c16b83e 13319 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13320}
13321
2060206e 13322static int
348d480f 13323bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13324 struct address_space *aspace, CORE_ADDR bp_addr,
13325 const struct target_waitstatus *ws)
348d480f 13326{
09ac7c10 13327 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13328 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13329 return 0;
13330
348d480f
PA
13331 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13332 aspace, bp_addr))
13333 return 0;
13334
13335 if (overlay_debugging /* unmapped overlay section */
13336 && section_is_overlay (bl->section)
13337 && !section_is_mapped (bl->section))
13338 return 0;
13339
13340 return 1;
13341}
13342
cd1608cc
PA
13343static int
13344dprintf_breakpoint_hit (const struct bp_location *bl,
13345 struct address_space *aspace, CORE_ADDR bp_addr,
13346 const struct target_waitstatus *ws)
13347{
13348 if (dprintf_style == dprintf_style_agent
13349 && target_can_run_breakpoint_commands ())
13350 {
13351 /* An agent-style dprintf never causes a stop. If we see a trap
13352 for this address it must be for a breakpoint that happens to
13353 be set at the same address. */
13354 return 0;
13355 }
13356
13357 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13358}
13359
2060206e 13360static int
348d480f
PA
13361bkpt_resources_needed (const struct bp_location *bl)
13362{
13363 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13364
13365 return 1;
13366}
13367
2060206e 13368static enum print_stop_action
348d480f
PA
13369bkpt_print_it (bpstat bs)
13370{
348d480f
PA
13371 struct breakpoint *b;
13372 const struct bp_location *bl;
001c8c33 13373 int bp_temp;
79a45e25 13374 struct ui_out *uiout = current_uiout;
348d480f
PA
13375
13376 gdb_assert (bs->bp_location_at != NULL);
13377
13378 bl = bs->bp_location_at;
13379 b = bs->breakpoint_at;
13380
001c8c33
PA
13381 bp_temp = b->disposition == disp_del;
13382 if (bl->address != bl->requested_address)
13383 breakpoint_adjustment_warning (bl->requested_address,
13384 bl->address,
13385 b->number, 1);
13386 annotate_breakpoint (b->number);
13387 if (bp_temp)
13388 ui_out_text (uiout, "\nTemporary breakpoint ");
13389 else
13390 ui_out_text (uiout, "\nBreakpoint ");
13391 if (ui_out_is_mi_like_p (uiout))
348d480f 13392 {
001c8c33
PA
13393 ui_out_field_string (uiout, "reason",
13394 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13395 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13396 }
001c8c33
PA
13397 ui_out_field_int (uiout, "bkptno", b->number);
13398 ui_out_text (uiout, ", ");
06edf0c0 13399
001c8c33 13400 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13401}
13402
2060206e 13403static void
06edf0c0
PA
13404bkpt_print_mention (struct breakpoint *b)
13405{
79a45e25 13406 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13407 return;
13408
13409 switch (b->type)
13410 {
13411 case bp_breakpoint:
13412 case bp_gnu_ifunc_resolver:
13413 if (b->disposition == disp_del)
13414 printf_filtered (_("Temporary breakpoint"));
13415 else
13416 printf_filtered (_("Breakpoint"));
13417 printf_filtered (_(" %d"), b->number);
13418 if (b->type == bp_gnu_ifunc_resolver)
13419 printf_filtered (_(" at gnu-indirect-function resolver"));
13420 break;
13421 case bp_hardware_breakpoint:
13422 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13423 break;
e7e0cddf
SS
13424 case bp_dprintf:
13425 printf_filtered (_("Dprintf %d"), b->number);
13426 break;
06edf0c0
PA
13427 }
13428
13429 say_where (b);
13430}
13431
2060206e 13432static void
06edf0c0
PA
13433bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13434{
13435 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13436 fprintf_unfiltered (fp, "tbreak");
13437 else if (tp->type == bp_breakpoint)
13438 fprintf_unfiltered (fp, "break");
13439 else if (tp->type == bp_hardware_breakpoint
13440 && tp->disposition == disp_del)
13441 fprintf_unfiltered (fp, "thbreak");
13442 else if (tp->type == bp_hardware_breakpoint)
13443 fprintf_unfiltered (fp, "hbreak");
13444 else
13445 internal_error (__FILE__, __LINE__,
13446 _("unhandled breakpoint type %d"), (int) tp->type);
13447
2060206e 13448 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13449 print_recreate_thread (tp, fp);
06edf0c0
PA
13450}
13451
983af33b
SDJ
13452static void
13453bkpt_create_sals_from_address (char **arg,
13454 struct linespec_result *canonical,
13455 enum bptype type_wanted,
13456 char *addr_start, char **copy_arg)
13457{
13458 create_sals_from_address_default (arg, canonical, type_wanted,
13459 addr_start, copy_arg);
13460}
13461
13462static void
13463bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13464 struct linespec_result *canonical,
983af33b 13465 char *cond_string,
e7e0cddf 13466 char *extra_string,
983af33b
SDJ
13467 enum bptype type_wanted,
13468 enum bpdisp disposition,
13469 int thread,
13470 int task, int ignore_count,
13471 const struct breakpoint_ops *ops,
13472 int from_tty, int enabled,
44f238bb 13473 int internal, unsigned flags)
983af33b 13474{
023fa29b 13475 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13476 cond_string, extra_string,
13477 type_wanted,
983af33b
SDJ
13478 disposition, thread, task,
13479 ignore_count, ops, from_tty,
44f238bb 13480 enabled, internal, flags);
983af33b
SDJ
13481}
13482
13483static void
13484bkpt_decode_linespec (struct breakpoint *b, char **s,
13485 struct symtabs_and_lines *sals)
13486{
13487 decode_linespec_default (b, s, sals);
13488}
13489
06edf0c0
PA
13490/* Virtual table for internal breakpoints. */
13491
13492static void
13493internal_bkpt_re_set (struct breakpoint *b)
13494{
13495 switch (b->type)
13496 {
13497 /* Delete overlay event and longjmp master breakpoints; they
13498 will be reset later by breakpoint_re_set. */
13499 case bp_overlay_event:
13500 case bp_longjmp_master:
13501 case bp_std_terminate_master:
13502 case bp_exception_master:
13503 delete_breakpoint (b);
13504 break;
13505
13506 /* This breakpoint is special, it's set up when the inferior
13507 starts and we really don't want to touch it. */
13508 case bp_shlib_event:
13509
13510 /* Like bp_shlib_event, this breakpoint type is special. Once
13511 it is set up, we do not want to touch it. */
13512 case bp_thread_event:
13513 break;
13514 }
13515}
13516
13517static void
13518internal_bkpt_check_status (bpstat bs)
13519{
a9b3a50f
PA
13520 if (bs->breakpoint_at->type == bp_shlib_event)
13521 {
13522 /* If requested, stop when the dynamic linker notifies GDB of
13523 events. This allows the user to get control and place
13524 breakpoints in initializer routines for dynamically loaded
13525 objects (among other things). */
13526 bs->stop = stop_on_solib_events;
13527 bs->print = stop_on_solib_events;
13528 }
13529 else
13530 bs->stop = 0;
06edf0c0
PA
13531}
13532
13533static enum print_stop_action
13534internal_bkpt_print_it (bpstat bs)
13535{
06edf0c0 13536 struct breakpoint *b;
06edf0c0 13537
06edf0c0
PA
13538 b = bs->breakpoint_at;
13539
06edf0c0
PA
13540 switch (b->type)
13541 {
348d480f
PA
13542 case bp_shlib_event:
13543 /* Did we stop because the user set the stop_on_solib_events
13544 variable? (If so, we report this as a generic, "Stopped due
13545 to shlib event" message.) */
edcc5120 13546 print_solib_event (0);
348d480f
PA
13547 break;
13548
13549 case bp_thread_event:
13550 /* Not sure how we will get here.
13551 GDB should not stop for these breakpoints. */
13552 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13553 break;
13554
13555 case bp_overlay_event:
13556 /* By analogy with the thread event, GDB should not stop for these. */
13557 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13558 break;
13559
13560 case bp_longjmp_master:
13561 /* These should never be enabled. */
13562 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13563 break;
13564
13565 case bp_std_terminate_master:
13566 /* These should never be enabled. */
13567 printf_filtered (_("std::terminate Master Breakpoint: "
13568 "gdb should not stop!\n"));
348d480f
PA
13569 break;
13570
13571 case bp_exception_master:
13572 /* These should never be enabled. */
13573 printf_filtered (_("Exception Master Breakpoint: "
13574 "gdb should not stop!\n"));
06edf0c0
PA
13575 break;
13576 }
13577
001c8c33 13578 return PRINT_NOTHING;
06edf0c0
PA
13579}
13580
13581static void
13582internal_bkpt_print_mention (struct breakpoint *b)
13583{
13584 /* Nothing to mention. These breakpoints are internal. */
13585}
13586
06edf0c0
PA
13587/* Virtual table for momentary breakpoints */
13588
13589static void
13590momentary_bkpt_re_set (struct breakpoint *b)
13591{
13592 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13593 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13594 Otherwise these should have been blown away via the cleanup chain
13595 or by breakpoint_init_inferior when we rerun the executable. */
13596}
13597
13598static void
13599momentary_bkpt_check_status (bpstat bs)
13600{
13601 /* Nothing. The point of these breakpoints is causing a stop. */
13602}
13603
13604static enum print_stop_action
13605momentary_bkpt_print_it (bpstat bs)
13606{
79a45e25
PA
13607 struct ui_out *uiout = current_uiout;
13608
001c8c33 13609 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13610 {
001c8c33 13611 struct breakpoint *b = bs->breakpoint_at;
348d480f 13612
001c8c33
PA
13613 switch (b->type)
13614 {
13615 case bp_finish:
13616 ui_out_field_string
13617 (uiout, "reason",
13618 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13619 break;
348d480f 13620
001c8c33
PA
13621 case bp_until:
13622 ui_out_field_string
13623 (uiout, "reason",
13624 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13625 break;
13626 }
348d480f
PA
13627 }
13628
001c8c33 13629 return PRINT_UNKNOWN;
348d480f
PA
13630}
13631
06edf0c0
PA
13632static void
13633momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13634{
06edf0c0 13635 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13636}
13637
e2e4d78b
JK
13638/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13639
13640 It gets cleared already on the removal of the first one of such placed
13641 breakpoints. This is OK as they get all removed altogether. */
13642
13643static void
13644longjmp_bkpt_dtor (struct breakpoint *self)
13645{
13646 struct thread_info *tp = find_thread_id (self->thread);
13647
13648 if (tp)
13649 tp->initiating_frame = null_frame_id;
13650
13651 momentary_breakpoint_ops.dtor (self);
13652}
13653
55aa24fb
SDJ
13654/* Specific methods for probe breakpoints. */
13655
13656static int
13657bkpt_probe_insert_location (struct bp_location *bl)
13658{
13659 int v = bkpt_insert_location (bl);
13660
13661 if (v == 0)
13662 {
13663 /* The insertion was successful, now let's set the probe's semaphore
13664 if needed. */
0ea5cda8
SDJ
13665 if (bl->probe.probe->pops->set_semaphore != NULL)
13666 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13667 bl->probe.objfile,
13668 bl->gdbarch);
55aa24fb
SDJ
13669 }
13670
13671 return v;
13672}
13673
13674static int
13675bkpt_probe_remove_location (struct bp_location *bl)
13676{
13677 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13678 if (bl->probe.probe->pops->clear_semaphore != NULL)
13679 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13680 bl->probe.objfile,
13681 bl->gdbarch);
55aa24fb
SDJ
13682
13683 return bkpt_remove_location (bl);
13684}
13685
13686static void
13687bkpt_probe_create_sals_from_address (char **arg,
13688 struct linespec_result *canonical,
13689 enum bptype type_wanted,
13690 char *addr_start, char **copy_arg)
13691{
13692 struct linespec_sals lsal;
13693
13694 lsal.sals = parse_probes (arg, canonical);
13695
13696 *copy_arg = xstrdup (canonical->addr_string);
13697 lsal.canonical = xstrdup (*copy_arg);
13698
13699 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13700}
13701
13702static void
13703bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13704 struct symtabs_and_lines *sals)
13705{
13706 *sals = parse_probes (s, NULL);
13707 if (!sals->sals)
13708 error (_("probe not found"));
13709}
13710
348d480f 13711/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13712
348d480f
PA
13713static void
13714tracepoint_re_set (struct breakpoint *b)
13715{
13716 breakpoint_re_set_default (b);
13717}
876fa593 13718
348d480f
PA
13719static int
13720tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13721 struct address_space *aspace, CORE_ADDR bp_addr,
13722 const struct target_waitstatus *ws)
348d480f
PA
13723{
13724 /* By definition, the inferior does not report stops at
13725 tracepoints. */
13726 return 0;
74960c60
VP
13727}
13728
13729static void
348d480f
PA
13730tracepoint_print_one_detail (const struct breakpoint *self,
13731 struct ui_out *uiout)
74960c60 13732{
d9b3f62e
PA
13733 struct tracepoint *tp = (struct tracepoint *) self;
13734 if (tp->static_trace_marker_id)
348d480f
PA
13735 {
13736 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13737
348d480f
PA
13738 ui_out_text (uiout, "\tmarker id is ");
13739 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13740 tp->static_trace_marker_id);
348d480f
PA
13741 ui_out_text (uiout, "\n");
13742 }
0d381245
VP
13743}
13744
a474d7c2 13745static void
348d480f 13746tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13747{
79a45e25 13748 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13749 return;
cc59ec59 13750
348d480f
PA
13751 switch (b->type)
13752 {
13753 case bp_tracepoint:
13754 printf_filtered (_("Tracepoint"));
13755 printf_filtered (_(" %d"), b->number);
13756 break;
13757 case bp_fast_tracepoint:
13758 printf_filtered (_("Fast tracepoint"));
13759 printf_filtered (_(" %d"), b->number);
13760 break;
13761 case bp_static_tracepoint:
13762 printf_filtered (_("Static tracepoint"));
13763 printf_filtered (_(" %d"), b->number);
13764 break;
13765 default:
13766 internal_error (__FILE__, __LINE__,
13767 _("unhandled tracepoint type %d"), (int) b->type);
13768 }
13769
13770 say_where (b);
a474d7c2
PA
13771}
13772
348d480f 13773static void
d9b3f62e 13774tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13775{
d9b3f62e
PA
13776 struct tracepoint *tp = (struct tracepoint *) self;
13777
13778 if (self->type == bp_fast_tracepoint)
348d480f 13779 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13780 if (self->type == bp_static_tracepoint)
348d480f 13781 fprintf_unfiltered (fp, "strace");
d9b3f62e 13782 else if (self->type == bp_tracepoint)
348d480f
PA
13783 fprintf_unfiltered (fp, "trace");
13784 else
13785 internal_error (__FILE__, __LINE__,
d9b3f62e 13786 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13787
d9b3f62e
PA
13788 fprintf_unfiltered (fp, " %s", self->addr_string);
13789 print_recreate_thread (self, fp);
13790
13791 if (tp->pass_count)
13792 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13793}
13794
983af33b
SDJ
13795static void
13796tracepoint_create_sals_from_address (char **arg,
13797 struct linespec_result *canonical,
13798 enum bptype type_wanted,
13799 char *addr_start, char **copy_arg)
13800{
13801 create_sals_from_address_default (arg, canonical, type_wanted,
13802 addr_start, copy_arg);
13803}
13804
13805static void
13806tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13807 struct linespec_result *canonical,
983af33b 13808 char *cond_string,
e7e0cddf 13809 char *extra_string,
983af33b
SDJ
13810 enum bptype type_wanted,
13811 enum bpdisp disposition,
13812 int thread,
13813 int task, int ignore_count,
13814 const struct breakpoint_ops *ops,
13815 int from_tty, int enabled,
44f238bb 13816 int internal, unsigned flags)
983af33b 13817{
023fa29b 13818 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13819 cond_string, extra_string,
13820 type_wanted,
983af33b
SDJ
13821 disposition, thread, task,
13822 ignore_count, ops, from_tty,
44f238bb 13823 enabled, internal, flags);
983af33b
SDJ
13824}
13825
13826static void
13827tracepoint_decode_linespec (struct breakpoint *b, char **s,
13828 struct symtabs_and_lines *sals)
13829{
13830 decode_linespec_default (b, s, sals);
13831}
13832
2060206e 13833struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13834
55aa24fb
SDJ
13835/* The breakpoint_ops structure to be use on tracepoints placed in a
13836 static probe. */
13837
13838static void
13839tracepoint_probe_create_sals_from_address (char **arg,
13840 struct linespec_result *canonical,
13841 enum bptype type_wanted,
13842 char *addr_start, char **copy_arg)
13843{
13844 /* We use the same method for breakpoint on probes. */
13845 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13846 addr_start, copy_arg);
13847}
13848
13849static void
13850tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13851 struct symtabs_and_lines *sals)
13852{
13853 /* We use the same method for breakpoint on probes. */
13854 bkpt_probe_decode_linespec (b, s, sals);
13855}
13856
13857static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13858
5c2b4418
HZ
13859/* Dprintf breakpoint_ops methods. */
13860
13861static void
13862dprintf_re_set (struct breakpoint *b)
13863{
13864 breakpoint_re_set_default (b);
13865
13866 /* This breakpoint could have been pending, and be resolved now, and
13867 if so, we should now have the extra string. If we don't, the
13868 dprintf was malformed when created, but we couldn't tell because
13869 we can't extract the extra string until the location is
13870 resolved. */
13871 if (b->loc != NULL && b->extra_string == NULL)
13872 error (_("Format string required"));
13873
13874 /* 1 - connect to target 1, that can run breakpoint commands.
13875 2 - create a dprintf, which resolves fine.
13876 3 - disconnect from target 1
13877 4 - connect to target 2, that can NOT run breakpoint commands.
13878
13879 After steps #3/#4, you'll want the dprintf command list to
13880 be updated, because target 1 and 2 may well return different
13881 answers for target_can_run_breakpoint_commands().
13882 Given absence of finer grained resetting, we get to do
13883 it all the time. */
13884 if (b->extra_string != NULL)
13885 update_dprintf_command_list (b);
13886}
13887
2d9442cc
HZ
13888/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13889
13890static void
13891dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13892{
13893 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13894 tp->extra_string);
13895 print_recreate_thread (tp, fp);
13896}
13897
9d6e6e84
HZ
13898/* Implement the "after_condition_true" breakpoint_ops method for
13899 dprintf.
13900
13901 dprintf's are implemented with regular commands in their command
13902 list, but we run the commands here instead of before presenting the
13903 stop to the user, as dprintf's don't actually cause a stop. This
13904 also makes it so that the commands of multiple dprintfs at the same
13905 address are all handled. */
13906
13907static void
13908dprintf_after_condition_true (struct bpstats *bs)
13909{
13910 struct cleanup *old_chain;
13911 struct bpstats tmp_bs = { NULL };
13912 struct bpstats *tmp_bs_p = &tmp_bs;
13913
13914 /* dprintf's never cause a stop. This wasn't set in the
13915 check_status hook instead because that would make the dprintf's
13916 condition not be evaluated. */
13917 bs->stop = 0;
13918
13919 /* Run the command list here. Take ownership of it instead of
13920 copying. We never want these commands to run later in
13921 bpstat_do_actions, if a breakpoint that causes a stop happens to
13922 be set at same address as this dprintf, or even if running the
13923 commands here throws. */
13924 tmp_bs.commands = bs->commands;
13925 bs->commands = NULL;
13926 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13927
13928 bpstat_do_actions_1 (&tmp_bs_p);
13929
13930 /* 'tmp_bs.commands' will usually be NULL by now, but
13931 bpstat_do_actions_1 may return early without processing the whole
13932 list. */
13933 do_cleanups (old_chain);
13934}
13935
983af33b
SDJ
13936/* The breakpoint_ops structure to be used on static tracepoints with
13937 markers (`-m'). */
13938
13939static void
13940strace_marker_create_sals_from_address (char **arg,
13941 struct linespec_result *canonical,
13942 enum bptype type_wanted,
13943 char *addr_start, char **copy_arg)
13944{
13945 struct linespec_sals lsal;
13946
13947 lsal.sals = decode_static_tracepoint_spec (arg);
13948
13949 *copy_arg = savestring (addr_start, *arg - addr_start);
13950
13951 canonical->addr_string = xstrdup (*copy_arg);
13952 lsal.canonical = xstrdup (*copy_arg);
13953 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13954}
13955
13956static void
13957strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13958 struct linespec_result *canonical,
983af33b 13959 char *cond_string,
e7e0cddf 13960 char *extra_string,
983af33b
SDJ
13961 enum bptype type_wanted,
13962 enum bpdisp disposition,
13963 int thread,
13964 int task, int ignore_count,
13965 const struct breakpoint_ops *ops,
13966 int from_tty, int enabled,
44f238bb 13967 int internal, unsigned flags)
983af33b
SDJ
13968{
13969 int i;
52d361e1
YQ
13970 struct linespec_sals *lsal = VEC_index (linespec_sals,
13971 canonical->sals, 0);
983af33b
SDJ
13972
13973 /* If the user is creating a static tracepoint by marker id
13974 (strace -m MARKER_ID), then store the sals index, so that
13975 breakpoint_re_set can try to match up which of the newly
13976 found markers corresponds to this one, and, don't try to
13977 expand multiple locations for each sal, given than SALS
13978 already should contain all sals for MARKER_ID. */
13979
13980 for (i = 0; i < lsal->sals.nelts; ++i)
13981 {
13982 struct symtabs_and_lines expanded;
13983 struct tracepoint *tp;
13984 struct cleanup *old_chain;
13985 char *addr_string;
13986
13987 expanded.nelts = 1;
13988 expanded.sals = &lsal->sals.sals[i];
13989
13990 addr_string = xstrdup (canonical->addr_string);
13991 old_chain = make_cleanup (xfree, addr_string);
13992
13993 tp = XCNEW (struct tracepoint);
13994 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13995 addr_string, NULL,
e7e0cddf
SS
13996 cond_string, extra_string,
13997 type_wanted, disposition,
983af33b 13998 thread, task, ignore_count, ops,
44f238bb 13999 from_tty, enabled, internal, flags,
983af33b
SDJ
14000 canonical->special_display);
14001 /* Given that its possible to have multiple markers with
14002 the same string id, if the user is creating a static
14003 tracepoint by marker id ("strace -m MARKER_ID"), then
14004 store the sals index, so that breakpoint_re_set can
14005 try to match up which of the newly found markers
14006 corresponds to this one */
14007 tp->static_trace_marker_id_idx = i;
14008
14009 install_breakpoint (internal, &tp->base, 0);
14010
14011 discard_cleanups (old_chain);
14012 }
14013}
14014
14015static void
14016strace_marker_decode_linespec (struct breakpoint *b, char **s,
14017 struct symtabs_and_lines *sals)
14018{
14019 struct tracepoint *tp = (struct tracepoint *) b;
14020
14021 *sals = decode_static_tracepoint_spec (s);
14022 if (sals->nelts > tp->static_trace_marker_id_idx)
14023 {
14024 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
14025 sals->nelts = 1;
14026 }
14027 else
14028 error (_("marker %s not found"), tp->static_trace_marker_id);
14029}
14030
14031static struct breakpoint_ops strace_marker_breakpoint_ops;
14032
14033static int
14034strace_marker_p (struct breakpoint *b)
14035{
14036 return b->ops == &strace_marker_breakpoint_ops;
14037}
14038
53a5351d 14039/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 14040 structures. */
c906108c
SS
14041
14042void
fba45db2 14043delete_breakpoint (struct breakpoint *bpt)
c906108c 14044{
52f0bd74 14045 struct breakpoint *b;
c906108c 14046
8a3fe4f8 14047 gdb_assert (bpt != NULL);
c906108c 14048
4a64f543
MS
14049 /* Has this bp already been deleted? This can happen because
14050 multiple lists can hold pointers to bp's. bpstat lists are
14051 especial culprits.
14052
14053 One example of this happening is a watchpoint's scope bp. When
14054 the scope bp triggers, we notice that the watchpoint is out of
14055 scope, and delete it. We also delete its scope bp. But the
14056 scope bp is marked "auto-deleting", and is already on a bpstat.
14057 That bpstat is then checked for auto-deleting bp's, which are
14058 deleted.
14059
14060 A real solution to this problem might involve reference counts in
14061 bp's, and/or giving them pointers back to their referencing
14062 bpstat's, and teaching delete_breakpoint to only free a bp's
14063 storage when no more references were extent. A cheaper bandaid
14064 was chosen. */
c906108c
SS
14065 if (bpt->type == bp_none)
14066 return;
14067
4a64f543
MS
14068 /* At least avoid this stale reference until the reference counting
14069 of breakpoints gets resolved. */
d0fb5eae 14070 if (bpt->related_breakpoint != bpt)
e5a0a904 14071 {
d0fb5eae 14072 struct breakpoint *related;
3a5c3e22 14073 struct watchpoint *w;
d0fb5eae
JK
14074
14075 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 14076 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 14077 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
14078 w = (struct watchpoint *) bpt;
14079 else
14080 w = NULL;
14081 if (w != NULL)
14082 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
14083
14084 /* Unlink bpt from the bpt->related_breakpoint ring. */
14085 for (related = bpt; related->related_breakpoint != bpt;
14086 related = related->related_breakpoint);
14087 related->related_breakpoint = bpt->related_breakpoint;
14088 bpt->related_breakpoint = bpt;
e5a0a904
JK
14089 }
14090
a9634178
TJB
14091 /* watch_command_1 creates a watchpoint but only sets its number if
14092 update_watchpoint succeeds in creating its bp_locations. If there's
14093 a problem in that process, we'll be asked to delete the half-created
14094 watchpoint. In that case, don't announce the deletion. */
14095 if (bpt->number)
14096 observer_notify_breakpoint_deleted (bpt);
c906108c 14097
c906108c
SS
14098 if (breakpoint_chain == bpt)
14099 breakpoint_chain = bpt->next;
14100
c906108c
SS
14101 ALL_BREAKPOINTS (b)
14102 if (b->next == bpt)
c5aa993b
JM
14103 {
14104 b->next = bpt->next;
14105 break;
14106 }
c906108c 14107
f431efe5
PA
14108 /* Be sure no bpstat's are pointing at the breakpoint after it's
14109 been freed. */
14110 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 14111 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
14112 pointing at bpt from the stop_bpstat list entirely, as breakpoint
14113 commands are associated with the bpstat; if we remove it here,
14114 then the later call to bpstat_do_actions (&stop_bpstat); in
14115 event-top.c won't do anything, and temporary breakpoints with
14116 commands won't work. */
14117
14118 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
14119
4a64f543
MS
14120 /* Now that breakpoint is removed from breakpoint list, update the
14121 global location list. This will remove locations that used to
14122 belong to this breakpoint. Do this before freeing the breakpoint
14123 itself, since remove_breakpoint looks at location's owner. It
14124 might be better design to have location completely
14125 self-contained, but it's not the case now. */
44702360 14126 update_global_location_list (UGLL_DONT_INSERT);
74960c60 14127
348d480f 14128 bpt->ops->dtor (bpt);
4a64f543
MS
14129 /* On the chance that someone will soon try again to delete this
14130 same bp, we mark it as deleted before freeing its storage. */
c906108c 14131 bpt->type = bp_none;
b8c9b27d 14132 xfree (bpt);
c906108c
SS
14133}
14134
4d6140d9
AC
14135static void
14136do_delete_breakpoint_cleanup (void *b)
14137{
14138 delete_breakpoint (b);
14139}
14140
14141struct cleanup *
14142make_cleanup_delete_breakpoint (struct breakpoint *b)
14143{
14144 return make_cleanup (do_delete_breakpoint_cleanup, b);
14145}
14146
51be5b68
PA
14147/* Iterator function to call a user-provided callback function once
14148 for each of B and its related breakpoints. */
14149
14150static void
14151iterate_over_related_breakpoints (struct breakpoint *b,
14152 void (*function) (struct breakpoint *,
14153 void *),
14154 void *data)
14155{
14156 struct breakpoint *related;
14157
14158 related = b;
14159 do
14160 {
14161 struct breakpoint *next;
14162
14163 /* FUNCTION may delete RELATED. */
14164 next = related->related_breakpoint;
14165
14166 if (next == related)
14167 {
14168 /* RELATED is the last ring entry. */
14169 function (related, data);
14170
14171 /* FUNCTION may have deleted it, so we'd never reach back to
14172 B. There's nothing left to do anyway, so just break
14173 out. */
14174 break;
14175 }
14176 else
14177 function (related, data);
14178
14179 related = next;
14180 }
14181 while (related != b);
14182}
95a42b64
TT
14183
14184static void
14185do_delete_breakpoint (struct breakpoint *b, void *ignore)
14186{
14187 delete_breakpoint (b);
14188}
14189
51be5b68
PA
14190/* A callback for map_breakpoint_numbers that calls
14191 delete_breakpoint. */
14192
14193static void
14194do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14195{
14196 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14197}
14198
c906108c 14199void
fba45db2 14200delete_command (char *arg, int from_tty)
c906108c 14201{
35df4500 14202 struct breakpoint *b, *b_tmp;
c906108c 14203
ea9365bb
TT
14204 dont_repeat ();
14205
c906108c
SS
14206 if (arg == 0)
14207 {
14208 int breaks_to_delete = 0;
14209
46c6471b
PA
14210 /* Delete all breakpoints if no argument. Do not delete
14211 internal breakpoints, these have to be deleted with an
14212 explicit breakpoint number argument. */
c5aa993b 14213 ALL_BREAKPOINTS (b)
46c6471b 14214 if (user_breakpoint_p (b))
973d738b
DJ
14215 {
14216 breaks_to_delete = 1;
14217 break;
14218 }
c906108c
SS
14219
14220 /* Ask user only if there are some breakpoints to delete. */
14221 if (!from_tty
e2e0b3e5 14222 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14223 {
35df4500 14224 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14225 if (user_breakpoint_p (b))
c5aa993b 14226 delete_breakpoint (b);
c906108c
SS
14227 }
14228 }
14229 else
51be5b68 14230 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14231}
14232
0d381245
VP
14233static int
14234all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 14235{
0d381245 14236 for (; loc; loc = loc->next)
8645ff69
UW
14237 if (!loc->shlib_disabled
14238 && !loc->pspace->executing_startup)
0d381245
VP
14239 return 0;
14240 return 1;
fe3f5fa8
VP
14241}
14242
776592bf
DE
14243/* Subroutine of update_breakpoint_locations to simplify it.
14244 Return non-zero if multiple fns in list LOC have the same name.
14245 Null names are ignored. */
14246
14247static int
14248ambiguous_names_p (struct bp_location *loc)
14249{
14250 struct bp_location *l;
14251 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14252 (int (*) (const void *,
14253 const void *)) streq,
776592bf
DE
14254 NULL, xcalloc, xfree);
14255
14256 for (l = loc; l != NULL; l = l->next)
14257 {
14258 const char **slot;
14259 const char *name = l->function_name;
14260
14261 /* Allow for some names to be NULL, ignore them. */
14262 if (name == NULL)
14263 continue;
14264
14265 slot = (const char **) htab_find_slot (htab, (const void *) name,
14266 INSERT);
4a64f543
MS
14267 /* NOTE: We can assume slot != NULL here because xcalloc never
14268 returns NULL. */
776592bf
DE
14269 if (*slot != NULL)
14270 {
14271 htab_delete (htab);
14272 return 1;
14273 }
14274 *slot = name;
14275 }
14276
14277 htab_delete (htab);
14278 return 0;
14279}
14280
0fb4aa4b
PA
14281/* When symbols change, it probably means the sources changed as well,
14282 and it might mean the static tracepoint markers are no longer at
14283 the same address or line numbers they used to be at last we
14284 checked. Losing your static tracepoints whenever you rebuild is
14285 undesirable. This function tries to resync/rematch gdb static
14286 tracepoints with the markers on the target, for static tracepoints
14287 that have not been set by marker id. Static tracepoint that have
14288 been set by marker id are reset by marker id in breakpoint_re_set.
14289 The heuristic is:
14290
14291 1) For a tracepoint set at a specific address, look for a marker at
14292 the old PC. If one is found there, assume to be the same marker.
14293 If the name / string id of the marker found is different from the
14294 previous known name, assume that means the user renamed the marker
14295 in the sources, and output a warning.
14296
14297 2) For a tracepoint set at a given line number, look for a marker
14298 at the new address of the old line number. If one is found there,
14299 assume to be the same marker. If the name / string id of the
14300 marker found is different from the previous known name, assume that
14301 means the user renamed the marker in the sources, and output a
14302 warning.
14303
14304 3) If a marker is no longer found at the same address or line, it
14305 may mean the marker no longer exists. But it may also just mean
14306 the code changed a bit. Maybe the user added a few lines of code
14307 that made the marker move up or down (in line number terms). Ask
14308 the target for info about the marker with the string id as we knew
14309 it. If found, update line number and address in the matching
14310 static tracepoint. This will get confused if there's more than one
14311 marker with the same ID (possible in UST, although unadvised
14312 precisely because it confuses tools). */
14313
14314static struct symtab_and_line
14315update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14316{
d9b3f62e 14317 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14318 struct static_tracepoint_marker marker;
14319 CORE_ADDR pc;
0fb4aa4b
PA
14320
14321 pc = sal.pc;
14322 if (sal.line)
14323 find_line_pc (sal.symtab, sal.line, &pc);
14324
14325 if (target_static_tracepoint_marker_at (pc, &marker))
14326 {
d9b3f62e 14327 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14328 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14329 b->number,
d9b3f62e 14330 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14331
d9b3f62e
PA
14332 xfree (tp->static_trace_marker_id);
14333 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14334 release_static_tracepoint_marker (&marker);
14335
14336 return sal;
14337 }
14338
14339 /* Old marker wasn't found on target at lineno. Try looking it up
14340 by string ID. */
14341 if (!sal.explicit_pc
14342 && sal.line != 0
14343 && sal.symtab != NULL
d9b3f62e 14344 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14345 {
14346 VEC(static_tracepoint_marker_p) *markers;
14347
14348 markers
d9b3f62e 14349 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14350
14351 if (!VEC_empty(static_tracepoint_marker_p, markers))
14352 {
80e1d417 14353 struct symtab_and_line sal2;
0fb4aa4b 14354 struct symbol *sym;
80e1d417 14355 struct static_tracepoint_marker *tpmarker;
79a45e25 14356 struct ui_out *uiout = current_uiout;
0fb4aa4b 14357
80e1d417 14358 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14359
d9b3f62e 14360 xfree (tp->static_trace_marker_id);
80e1d417 14361 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14362
14363 warning (_("marker for static tracepoint %d (%s) not "
14364 "found at previous line number"),
d9b3f62e 14365 b->number, tp->static_trace_marker_id);
0fb4aa4b 14366
80e1d417 14367 init_sal (&sal2);
0fb4aa4b 14368
80e1d417 14369 sal2.pc = tpmarker->address;
0fb4aa4b 14370
80e1d417
AS
14371 sal2 = find_pc_line (tpmarker->address, 0);
14372 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14373 ui_out_text (uiout, "Now in ");
14374 if (sym)
14375 {
14376 ui_out_field_string (uiout, "func",
14377 SYMBOL_PRINT_NAME (sym));
14378 ui_out_text (uiout, " at ");
14379 }
05cba821
JK
14380 ui_out_field_string (uiout, "file",
14381 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14382 ui_out_text (uiout, ":");
14383
14384 if (ui_out_is_mi_like_p (uiout))
14385 {
0b0865da 14386 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14387
f35a17b5 14388 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14389 }
14390
80e1d417 14391 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14392 ui_out_text (uiout, "\n");
14393
80e1d417 14394 b->loc->line_number = sal2.line;
2f202fde 14395 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14396
14397 xfree (b->addr_string);
14398 b->addr_string = xstrprintf ("%s:%d",
05cba821 14399 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14400 b->loc->line_number);
0fb4aa4b
PA
14401
14402 /* Might be nice to check if function changed, and warn if
14403 so. */
14404
80e1d417 14405 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14406 }
14407 }
14408 return sal;
14409}
14410
8d3788bd
VP
14411/* Returns 1 iff locations A and B are sufficiently same that
14412 we don't need to report breakpoint as changed. */
14413
14414static int
14415locations_are_equal (struct bp_location *a, struct bp_location *b)
14416{
14417 while (a && b)
14418 {
14419 if (a->address != b->address)
14420 return 0;
14421
14422 if (a->shlib_disabled != b->shlib_disabled)
14423 return 0;
14424
14425 if (a->enabled != b->enabled)
14426 return 0;
14427
14428 a = a->next;
14429 b = b->next;
14430 }
14431
14432 if ((a == NULL) != (b == NULL))
14433 return 0;
14434
14435 return 1;
14436}
14437
f1310107
TJB
14438/* Create new breakpoint locations for B (a hardware or software breakpoint)
14439 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14440 a ranged breakpoint. */
14441
0e30163f 14442void
0d381245 14443update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14444 struct symtabs_and_lines sals,
14445 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14446{
14447 int i;
0d381245
VP
14448 struct bp_location *existing_locations = b->loc;
14449
f8eba3c6
TT
14450 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14451 {
14452 /* Ranged breakpoints have only one start location and one end
14453 location. */
14454 b->enable_state = bp_disabled;
44702360 14455 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14456 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14457 "multiple locations found\n"),
14458 b->number);
14459 return;
14460 }
f1310107 14461
4a64f543
MS
14462 /* If there's no new locations, and all existing locations are
14463 pending, don't do anything. This optimizes the common case where
14464 all locations are in the same shared library, that was unloaded.
14465 We'd like to retain the location, so that when the library is
14466 loaded again, we don't loose the enabled/disabled status of the
14467 individual locations. */
0d381245 14468 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14469 return;
14470
fe3f5fa8
VP
14471 b->loc = NULL;
14472
0d381245 14473 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14474 {
f8eba3c6
TT
14475 struct bp_location *new_loc;
14476
14477 switch_to_program_space_and_thread (sals.sals[i].pspace);
14478
14479 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14480
0d381245
VP
14481 /* Reparse conditions, they might contain references to the
14482 old symtab. */
14483 if (b->cond_string != NULL)
14484 {
bbc13ae3 14485 const char *s;
fe3f5fa8 14486
0d381245 14487 s = b->cond_string;
492d29ea 14488 TRY
0d381245 14489 {
1bb9788d
TT
14490 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14491 block_for_pc (sals.sals[i].pc),
0d381245
VP
14492 0);
14493 }
492d29ea 14494 CATCH (e, RETURN_MASK_ERROR)
0d381245 14495 {
3e43a32a
MS
14496 warning (_("failed to reevaluate condition "
14497 "for breakpoint %d: %s"),
0d381245
VP
14498 b->number, e.message);
14499 new_loc->enabled = 0;
14500 }
492d29ea 14501 END_CATCH
0d381245 14502 }
fe3f5fa8 14503
f1310107
TJB
14504 if (sals_end.nelts)
14505 {
14506 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14507
14508 new_loc->length = end - sals.sals[0].pc + 1;
14509 }
0d381245 14510 }
fe3f5fa8 14511
4a64f543
MS
14512 /* If possible, carry over 'disable' status from existing
14513 breakpoints. */
0d381245
VP
14514 {
14515 struct bp_location *e = existing_locations;
776592bf
DE
14516 /* If there are multiple breakpoints with the same function name,
14517 e.g. for inline functions, comparing function names won't work.
14518 Instead compare pc addresses; this is just a heuristic as things
14519 may have moved, but in practice it gives the correct answer
14520 often enough until a better solution is found. */
14521 int have_ambiguous_names = ambiguous_names_p (b->loc);
14522
0d381245
VP
14523 for (; e; e = e->next)
14524 {
14525 if (!e->enabled && e->function_name)
14526 {
14527 struct bp_location *l = b->loc;
776592bf
DE
14528 if (have_ambiguous_names)
14529 {
14530 for (; l; l = l->next)
f1310107 14531 if (breakpoint_locations_match (e, l))
776592bf
DE
14532 {
14533 l->enabled = 0;
14534 break;
14535 }
14536 }
14537 else
14538 {
14539 for (; l; l = l->next)
14540 if (l->function_name
14541 && strcmp (e->function_name, l->function_name) == 0)
14542 {
14543 l->enabled = 0;
14544 break;
14545 }
14546 }
0d381245
VP
14547 }
14548 }
14549 }
fe3f5fa8 14550
8d3788bd
VP
14551 if (!locations_are_equal (existing_locations, b->loc))
14552 observer_notify_breakpoint_modified (b);
14553
44702360 14554 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14555}
14556
ef23e705
TJB
14557/* Find the SaL locations corresponding to the given ADDR_STRING.
14558 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14559
14560static struct symtabs_and_lines
14561addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14562{
14563 char *s;
02d20e4a 14564 struct symtabs_and_lines sals = {0};
492d29ea 14565 struct gdb_exception exception = exception_none;
ef23e705 14566
983af33b 14567 gdb_assert (b->ops != NULL);
ef23e705 14568 s = addr_string;
ef23e705 14569
492d29ea 14570 TRY
ef23e705 14571 {
983af33b 14572 b->ops->decode_linespec (b, &s, &sals);
ef23e705 14573 }
492d29ea 14574 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14575 {
14576 int not_found_and_ok = 0;
492d29ea
PA
14577
14578 exception = e;
14579
ef23e705
TJB
14580 /* For pending breakpoints, it's expected that parsing will
14581 fail until the right shared library is loaded. User has
14582 already told to create pending breakpoints and don't need
14583 extra messages. If breakpoint is in bp_shlib_disabled
14584 state, then user already saw the message about that
14585 breakpoint being disabled, and don't want to see more
14586 errors. */
58438ac1 14587 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14588 && (b->condition_not_parsed
14589 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14590 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14591 || b->enable_state == bp_disabled))
14592 not_found_and_ok = 1;
14593
14594 if (!not_found_and_ok)
14595 {
14596 /* We surely don't want to warn about the same breakpoint
14597 10 times. One solution, implemented here, is disable
14598 the breakpoint on error. Another solution would be to
14599 have separate 'warning emitted' flag. Since this
14600 happens only when a binary has changed, I don't know
14601 which approach is better. */
14602 b->enable_state = bp_disabled;
14603 throw_exception (e);
14604 }
14605 }
492d29ea 14606 END_CATCH
ef23e705 14607
492d29ea 14608 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14609 {
f8eba3c6 14610 int i;
ef23e705 14611
f8eba3c6
TT
14612 for (i = 0; i < sals.nelts; ++i)
14613 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14614 if (b->condition_not_parsed && s && s[0])
14615 {
ed1d1739
KS
14616 char *cond_string, *extra_string;
14617 int thread, task;
ef23e705
TJB
14618
14619 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14620 &cond_string, &thread, &task,
14621 &extra_string);
ef23e705
TJB
14622 if (cond_string)
14623 b->cond_string = cond_string;
14624 b->thread = thread;
14625 b->task = task;
e7e0cddf
SS
14626 if (extra_string)
14627 b->extra_string = extra_string;
ef23e705
TJB
14628 b->condition_not_parsed = 0;
14629 }
14630
983af33b 14631 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14632 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14633
58438ac1
TT
14634 *found = 1;
14635 }
14636 else
14637 *found = 0;
ef23e705
TJB
14638
14639 return sals;
14640}
14641
348d480f
PA
14642/* The default re_set method, for typical hardware or software
14643 breakpoints. Reevaluate the breakpoint and recreate its
14644 locations. */
14645
14646static void
28010a5d 14647breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14648{
14649 int found;
f1310107 14650 struct symtabs_and_lines sals, sals_end;
ef23e705 14651 struct symtabs_and_lines expanded = {0};
f1310107 14652 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14653
14654 sals = addr_string_to_sals (b, b->addr_string, &found);
14655 if (found)
14656 {
14657 make_cleanup (xfree, sals.sals);
f8eba3c6 14658 expanded = sals;
ef23e705
TJB
14659 }
14660
f1310107
TJB
14661 if (b->addr_string_range_end)
14662 {
14663 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14664 if (found)
14665 {
14666 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14667 expanded_end = sals_end;
f1310107
TJB
14668 }
14669 }
14670
14671 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14672}
14673
983af33b
SDJ
14674/* Default method for creating SALs from an address string. It basically
14675 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14676
14677static void
14678create_sals_from_address_default (char **arg,
14679 struct linespec_result *canonical,
14680 enum bptype type_wanted,
14681 char *addr_start, char **copy_arg)
14682{
14683 parse_breakpoint_sals (arg, canonical);
14684}
14685
14686/* Call create_breakpoints_sal for the given arguments. This is the default
14687 function for the `create_breakpoints_sal' method of
14688 breakpoint_ops. */
14689
14690static void
14691create_breakpoints_sal_default (struct gdbarch *gdbarch,
14692 struct linespec_result *canonical,
983af33b 14693 char *cond_string,
e7e0cddf 14694 char *extra_string,
983af33b
SDJ
14695 enum bptype type_wanted,
14696 enum bpdisp disposition,
14697 int thread,
14698 int task, int ignore_count,
14699 const struct breakpoint_ops *ops,
14700 int from_tty, int enabled,
44f238bb 14701 int internal, unsigned flags)
983af33b
SDJ
14702{
14703 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14704 extra_string,
983af33b
SDJ
14705 type_wanted, disposition,
14706 thread, task, ignore_count, ops, from_tty,
44f238bb 14707 enabled, internal, flags);
983af33b
SDJ
14708}
14709
14710/* Decode the line represented by S by calling decode_line_full. This is the
14711 default function for the `decode_linespec' method of breakpoint_ops. */
14712
14713static void
14714decode_linespec_default (struct breakpoint *b, char **s,
14715 struct symtabs_and_lines *sals)
14716{
14717 struct linespec_result canonical;
14718
14719 init_linespec_result (&canonical);
14720 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14721 (struct symtab *) NULL, 0,
14722 &canonical, multiple_symbols_all,
14723 b->filter);
14724
14725 /* We should get 0 or 1 resulting SALs. */
14726 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14727
14728 if (VEC_length (linespec_sals, canonical.sals) > 0)
14729 {
14730 struct linespec_sals *lsal;
14731
14732 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14733 *sals = lsal->sals;
14734 /* Arrange it so the destructor does not free the
14735 contents. */
14736 lsal->sals.sals = NULL;
14737 }
14738
14739 destroy_linespec_result (&canonical);
14740}
14741
28010a5d
PA
14742/* Prepare the global context for a re-set of breakpoint B. */
14743
14744static struct cleanup *
14745prepare_re_set_context (struct breakpoint *b)
14746{
14747 struct cleanup *cleanups;
14748
14749 input_radix = b->input_radix;
14750 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14751 if (b->pspace != NULL)
14752 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14753 set_language (b->language);
14754
14755 return cleanups;
ef23e705
TJB
14756}
14757
c906108c
SS
14758/* Reset a breakpoint given it's struct breakpoint * BINT.
14759 The value we return ends up being the return value from catch_errors.
14760 Unused in this case. */
14761
14762static int
4efb68b1 14763breakpoint_re_set_one (void *bint)
c906108c 14764{
4a64f543 14765 /* Get past catch_errs. */
53a5351d 14766 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14767 struct cleanup *cleanups;
c906108c 14768
348d480f
PA
14769 cleanups = prepare_re_set_context (b);
14770 b->ops->re_set (b);
14771 do_cleanups (cleanups);
c906108c
SS
14772 return 0;
14773}
14774
69de3c6a 14775/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14776void
69de3c6a 14777breakpoint_re_set (void)
c906108c 14778{
35df4500 14779 struct breakpoint *b, *b_tmp;
c906108c
SS
14780 enum language save_language;
14781 int save_input_radix;
6c95b8df 14782 struct cleanup *old_chain;
c5aa993b 14783
c906108c
SS
14784 save_language = current_language->la_language;
14785 save_input_radix = input_radix;
6c95b8df
PA
14786 old_chain = save_current_program_space ();
14787
35df4500 14788 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14789 {
4a64f543 14790 /* Format possible error msg. */
fe3f5fa8 14791 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14792 b->number);
14793 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14794 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14795 do_cleanups (cleanups);
c5aa993b 14796 }
c906108c
SS
14797 set_language (save_language);
14798 input_radix = save_input_radix;
e62c965a 14799
0756c555 14800 jit_breakpoint_re_set ();
4efc6507 14801
6c95b8df
PA
14802 do_cleanups (old_chain);
14803
af02033e
PP
14804 create_overlay_event_breakpoint ();
14805 create_longjmp_master_breakpoint ();
14806 create_std_terminate_master_breakpoint ();
186c406b 14807 create_exception_master_breakpoint ();
c906108c
SS
14808}
14809\f
c906108c
SS
14810/* Reset the thread number of this breakpoint:
14811
14812 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14813 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14814void
fba45db2 14815breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14816{
14817 if (b->thread != -1)
14818 {
39f77062
KB
14819 if (in_thread_list (inferior_ptid))
14820 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14821
14822 /* We're being called after following a fork. The new fork is
14823 selected as current, and unless this was a vfork will have a
14824 different program space from the original thread. Reset that
14825 as well. */
14826 b->loc->pspace = current_program_space;
c906108c
SS
14827 }
14828}
14829
03ac34d5
MS
14830/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14831 If from_tty is nonzero, it prints a message to that effect,
14832 which ends with a period (no newline). */
14833
c906108c 14834void
fba45db2 14835set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14836{
52f0bd74 14837 struct breakpoint *b;
c906108c
SS
14838
14839 if (count < 0)
14840 count = 0;
14841
14842 ALL_BREAKPOINTS (b)
14843 if (b->number == bptnum)
c5aa993b 14844 {
d77f58be
SS
14845 if (is_tracepoint (b))
14846 {
14847 if (from_tty && count != 0)
14848 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14849 bptnum);
14850 return;
14851 }
14852
c5aa993b 14853 b->ignore_count = count;
221ea385
KS
14854 if (from_tty)
14855 {
14856 if (count == 0)
3e43a32a
MS
14857 printf_filtered (_("Will stop next time "
14858 "breakpoint %d is reached."),
221ea385
KS
14859 bptnum);
14860 else if (count == 1)
a3f17187 14861 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14862 bptnum);
14863 else
3e43a32a
MS
14864 printf_filtered (_("Will ignore next %d "
14865 "crossings of breakpoint %d."),
221ea385
KS
14866 count, bptnum);
14867 }
8d3788bd 14868 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14869 return;
14870 }
c906108c 14871
8a3fe4f8 14872 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14873}
14874
c906108c
SS
14875/* Command to set ignore-count of breakpoint N to COUNT. */
14876
14877static void
fba45db2 14878ignore_command (char *args, int from_tty)
c906108c
SS
14879{
14880 char *p = args;
52f0bd74 14881 int num;
c906108c
SS
14882
14883 if (p == 0)
e2e0b3e5 14884 error_no_arg (_("a breakpoint number"));
c5aa993b 14885
c906108c 14886 num = get_number (&p);
5c44784c 14887 if (num == 0)
8a3fe4f8 14888 error (_("bad breakpoint number: '%s'"), args);
c906108c 14889 if (*p == 0)
8a3fe4f8 14890 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14891
14892 set_ignore_count (num,
14893 longest_to_int (value_as_long (parse_and_eval (p))),
14894 from_tty);
221ea385
KS
14895 if (from_tty)
14896 printf_filtered ("\n");
c906108c
SS
14897}
14898\f
14899/* Call FUNCTION on each of the breakpoints
14900 whose numbers are given in ARGS. */
14901
14902static void
95a42b64
TT
14903map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14904 void *),
14905 void *data)
c906108c 14906{
52f0bd74
AC
14907 int num;
14908 struct breakpoint *b, *tmp;
11cf8741 14909 int match;
197f0a60 14910 struct get_number_or_range_state state;
c906108c 14911
b9d61307 14912 if (args == 0 || *args == '\0')
e2e0b3e5 14913 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14914
197f0a60
TT
14915 init_number_or_range (&state, args);
14916
14917 while (!state.finished)
c906108c 14918 {
e799154c 14919 const char *p = state.string;
197f0a60 14920
11cf8741 14921 match = 0;
c5aa993b 14922
197f0a60 14923 num = get_number_or_range (&state);
5c44784c 14924 if (num == 0)
c5aa993b 14925 {
8a3fe4f8 14926 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14927 }
14928 else
14929 {
14930 ALL_BREAKPOINTS_SAFE (b, tmp)
14931 if (b->number == num)
14932 {
11cf8741 14933 match = 1;
cdac0397 14934 function (b, data);
11cf8741 14935 break;
5c44784c 14936 }
11cf8741 14937 if (match == 0)
a3f17187 14938 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14939 }
c906108c
SS
14940 }
14941}
14942
0d381245
VP
14943static struct bp_location *
14944find_location_by_number (char *number)
14945{
14946 char *dot = strchr (number, '.');
14947 char *p1;
14948 int bp_num;
14949 int loc_num;
14950 struct breakpoint *b;
14951 struct bp_location *loc;
14952
14953 *dot = '\0';
14954
14955 p1 = number;
197f0a60 14956 bp_num = get_number (&p1);
0d381245
VP
14957 if (bp_num == 0)
14958 error (_("Bad breakpoint number '%s'"), number);
14959
14960 ALL_BREAKPOINTS (b)
14961 if (b->number == bp_num)
14962 {
14963 break;
14964 }
14965
14966 if (!b || b->number != bp_num)
14967 error (_("Bad breakpoint number '%s'"), number);
14968
14969 p1 = dot+1;
197f0a60 14970 loc_num = get_number (&p1);
0d381245
VP
14971 if (loc_num == 0)
14972 error (_("Bad breakpoint location number '%s'"), number);
14973
14974 --loc_num;
14975 loc = b->loc;
14976 for (;loc_num && loc; --loc_num, loc = loc->next)
14977 ;
14978 if (!loc)
14979 error (_("Bad breakpoint location number '%s'"), dot+1);
14980
14981 return loc;
14982}
14983
14984
1900040c
MS
14985/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14986 If from_tty is nonzero, it prints a message to that effect,
14987 which ends with a period (no newline). */
14988
c906108c 14989void
fba45db2 14990disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14991{
14992 /* Never disable a watchpoint scope breakpoint; we want to
14993 hit them when we leave scope so we can delete both the
14994 watchpoint and its scope breakpoint at that time. */
14995 if (bpt->type == bp_watchpoint_scope)
14996 return;
14997
b5de0fa7 14998 bpt->enable_state = bp_disabled;
c906108c 14999
b775012e
LM
15000 /* Mark breakpoint locations modified. */
15001 mark_breakpoint_modified (bpt);
15002
d248b706
KY
15003 if (target_supports_enable_disable_tracepoint ()
15004 && current_trace_status ()->running && is_tracepoint (bpt))
15005 {
15006 struct bp_location *location;
15007
15008 for (location = bpt->loc; location; location = location->next)
15009 target_disable_tracepoint (location);
15010 }
15011
44702360 15012 update_global_location_list (UGLL_DONT_INSERT);
c906108c 15013
8d3788bd 15014 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15015}
15016
51be5b68
PA
15017/* A callback for iterate_over_related_breakpoints. */
15018
15019static void
15020do_disable_breakpoint (struct breakpoint *b, void *ignore)
15021{
15022 disable_breakpoint (b);
15023}
15024
95a42b64
TT
15025/* A callback for map_breakpoint_numbers that calls
15026 disable_breakpoint. */
15027
15028static void
15029do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
15030{
51be5b68 15031 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
15032}
15033
c906108c 15034static void
fba45db2 15035disable_command (char *args, int from_tty)
c906108c 15036{
c906108c 15037 if (args == 0)
46c6471b
PA
15038 {
15039 struct breakpoint *bpt;
15040
15041 ALL_BREAKPOINTS (bpt)
15042 if (user_breakpoint_p (bpt))
15043 disable_breakpoint (bpt);
15044 }
9eaabc75 15045 else
0d381245 15046 {
9eaabc75
MW
15047 char *num = extract_arg (&args);
15048
15049 while (num)
d248b706 15050 {
9eaabc75 15051 if (strchr (num, '.'))
b775012e 15052 {
9eaabc75
MW
15053 struct bp_location *loc = find_location_by_number (num);
15054
15055 if (loc)
15056 {
15057 if (loc->enabled)
15058 {
15059 loc->enabled = 0;
15060 mark_breakpoint_location_modified (loc);
15061 }
15062 if (target_supports_enable_disable_tracepoint ()
15063 && current_trace_status ()->running && loc->owner
15064 && is_tracepoint (loc->owner))
15065 target_disable_tracepoint (loc);
15066 }
44702360 15067 update_global_location_list (UGLL_DONT_INSERT);
b775012e 15068 }
9eaabc75
MW
15069 else
15070 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
15071 num = extract_arg (&args);
d248b706 15072 }
0d381245 15073 }
c906108c
SS
15074}
15075
15076static void
816338b5
SS
15077enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
15078 int count)
c906108c 15079{
afe38095 15080 int target_resources_ok;
c906108c
SS
15081
15082 if (bpt->type == bp_hardware_breakpoint)
15083 {
15084 int i;
c5aa993b 15085 i = hw_breakpoint_used_count ();
53a5351d 15086 target_resources_ok =
d92524f1 15087 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 15088 i + 1, 0);
c906108c 15089 if (target_resources_ok == 0)
8a3fe4f8 15090 error (_("No hardware breakpoint support in the target."));
c906108c 15091 else if (target_resources_ok < 0)
8a3fe4f8 15092 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
15093 }
15094
cc60f2e3 15095 if (is_watchpoint (bpt))
c906108c 15096 {
d07205c2
JK
15097 /* Initialize it just to avoid a GCC false warning. */
15098 enum enable_state orig_enable_state = 0;
dde02812 15099
492d29ea 15100 TRY
c906108c 15101 {
3a5c3e22
PA
15102 struct watchpoint *w = (struct watchpoint *) bpt;
15103
1e718ff1
TJB
15104 orig_enable_state = bpt->enable_state;
15105 bpt->enable_state = bp_enabled;
3a5c3e22 15106 update_watchpoint (w, 1 /* reparse */);
c906108c 15107 }
492d29ea 15108 CATCH (e, RETURN_MASK_ALL)
c5aa993b 15109 {
1e718ff1 15110 bpt->enable_state = orig_enable_state;
dde02812
ES
15111 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
15112 bpt->number);
15113 return;
c5aa993b 15114 }
492d29ea 15115 END_CATCH
c906108c 15116 }
0101ce28 15117
b775012e
LM
15118 bpt->enable_state = bp_enabled;
15119
15120 /* Mark breakpoint locations modified. */
15121 mark_breakpoint_modified (bpt);
15122
d248b706
KY
15123 if (target_supports_enable_disable_tracepoint ()
15124 && current_trace_status ()->running && is_tracepoint (bpt))
15125 {
15126 struct bp_location *location;
15127
15128 for (location = bpt->loc; location; location = location->next)
15129 target_enable_tracepoint (location);
15130 }
15131
b4c291bb 15132 bpt->disposition = disposition;
816338b5 15133 bpt->enable_count = count;
44702360 15134 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 15135
8d3788bd 15136 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15137}
15138
fe3f5fa8 15139
c906108c 15140void
fba45db2 15141enable_breakpoint (struct breakpoint *bpt)
c906108c 15142{
816338b5 15143 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
15144}
15145
15146static void
15147do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15148{
15149 enable_breakpoint (bpt);
c906108c
SS
15150}
15151
95a42b64
TT
15152/* A callback for map_breakpoint_numbers that calls
15153 enable_breakpoint. */
15154
15155static void
15156do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15157{
51be5b68 15158 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15159}
15160
c906108c
SS
15161/* The enable command enables the specified breakpoints (or all defined
15162 breakpoints) so they once again become (or continue to be) effective
1272ad14 15163 in stopping the inferior. */
c906108c 15164
c906108c 15165static void
fba45db2 15166enable_command (char *args, int from_tty)
c906108c 15167{
c906108c 15168 if (args == 0)
46c6471b
PA
15169 {
15170 struct breakpoint *bpt;
15171
15172 ALL_BREAKPOINTS (bpt)
15173 if (user_breakpoint_p (bpt))
15174 enable_breakpoint (bpt);
15175 }
9eaabc75 15176 else
0d381245 15177 {
9eaabc75
MW
15178 char *num = extract_arg (&args);
15179
15180 while (num)
d248b706 15181 {
9eaabc75 15182 if (strchr (num, '.'))
b775012e 15183 {
9eaabc75
MW
15184 struct bp_location *loc = find_location_by_number (num);
15185
15186 if (loc)
15187 {
15188 if (!loc->enabled)
15189 {
15190 loc->enabled = 1;
15191 mark_breakpoint_location_modified (loc);
15192 }
15193 if (target_supports_enable_disable_tracepoint ()
15194 && current_trace_status ()->running && loc->owner
15195 && is_tracepoint (loc->owner))
15196 target_enable_tracepoint (loc);
15197 }
44702360 15198 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15199 }
9eaabc75
MW
15200 else
15201 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15202 num = extract_arg (&args);
d248b706 15203 }
0d381245 15204 }
c906108c
SS
15205}
15206
816338b5
SS
15207/* This struct packages up disposition data for application to multiple
15208 breakpoints. */
15209
15210struct disp_data
15211{
15212 enum bpdisp disp;
15213 int count;
15214};
15215
c906108c 15216static void
51be5b68
PA
15217do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15218{
816338b5 15219 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15220
816338b5 15221 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15222}
15223
15224static void
15225do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15226{
816338b5 15227 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15228
15229 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15230}
15231
c906108c 15232static void
fba45db2 15233enable_once_command (char *args, int from_tty)
c906108c 15234{
51be5b68 15235 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15236}
15237
816338b5
SS
15238static void
15239do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15240{
15241 struct disp_data disp = { disp_disable, *(int *) countptr };
15242
15243 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15244}
15245
15246static void
15247enable_count_command (char *args, int from_tty)
15248{
b9d61307
SM
15249 int count;
15250
15251 if (args == NULL)
15252 error_no_arg (_("hit count"));
15253
15254 count = get_number (&args);
816338b5
SS
15255
15256 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15257}
15258
c906108c 15259static void
51be5b68 15260do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15261{
816338b5 15262 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15263
15264 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15265}
15266
c906108c 15267static void
fba45db2 15268enable_delete_command (char *args, int from_tty)
c906108c 15269{
51be5b68 15270 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15271}
15272\f
fa8d40ab
JJ
15273static void
15274set_breakpoint_cmd (char *args, int from_tty)
15275{
15276}
15277
15278static void
15279show_breakpoint_cmd (char *args, int from_tty)
15280{
15281}
15282
1f3b5d1b
PP
15283/* Invalidate last known value of any hardware watchpoint if
15284 the memory which that value represents has been written to by
15285 GDB itself. */
15286
15287static void
8de0566d
YQ
15288invalidate_bp_value_on_memory_change (struct inferior *inferior,
15289 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15290 const bfd_byte *data)
15291{
15292 struct breakpoint *bp;
15293
15294 ALL_BREAKPOINTS (bp)
15295 if (bp->enable_state == bp_enabled
3a5c3e22 15296 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15297 {
3a5c3e22 15298 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15299
3a5c3e22
PA
15300 if (wp->val_valid && wp->val)
15301 {
15302 struct bp_location *loc;
15303
15304 for (loc = bp->loc; loc != NULL; loc = loc->next)
15305 if (loc->loc_type == bp_loc_hardware_watchpoint
15306 && loc->address + loc->length > addr
15307 && addr + len > loc->address)
15308 {
15309 value_free (wp->val);
15310 wp->val = NULL;
15311 wp->val_valid = 0;
15312 }
15313 }
1f3b5d1b
PP
15314 }
15315}
15316
8181d85f
DJ
15317/* Create and insert a breakpoint for software single step. */
15318
15319void
6c95b8df 15320insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15321 struct address_space *aspace,
15322 CORE_ADDR next_pc)
8181d85f 15323{
7c16b83e
PA
15324 struct thread_info *tp = inferior_thread ();
15325 struct symtab_and_line sal;
15326 CORE_ADDR pc = next_pc;
8181d85f 15327
34b7e8a6
PA
15328 if (tp->control.single_step_breakpoints == NULL)
15329 {
15330 tp->control.single_step_breakpoints
15331 = new_single_step_breakpoint (tp->num, gdbarch);
15332 }
8181d85f 15333
7c16b83e
PA
15334 sal = find_pc_line (pc, 0);
15335 sal.pc = pc;
15336 sal.section = find_pc_overlay (pc);
15337 sal.explicit_pc = 1;
34b7e8a6 15338 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15339
7c16b83e 15340 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15341}
15342
34b7e8a6 15343/* See breakpoint.h. */
f02253f1
HZ
15344
15345int
7c16b83e
PA
15346breakpoint_has_location_inserted_here (struct breakpoint *bp,
15347 struct address_space *aspace,
15348 CORE_ADDR pc)
1aafd4da 15349{
7c16b83e 15350 struct bp_location *loc;
1aafd4da 15351
7c16b83e
PA
15352 for (loc = bp->loc; loc != NULL; loc = loc->next)
15353 if (loc->inserted
15354 && breakpoint_location_address_match (loc, aspace, pc))
15355 return 1;
1aafd4da 15356
7c16b83e 15357 return 0;
ef370185
JB
15358}
15359
15360/* Check whether a software single-step breakpoint is inserted at
15361 PC. */
15362
15363int
15364single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15365 CORE_ADDR pc)
15366{
34b7e8a6
PA
15367 struct breakpoint *bpt;
15368
15369 ALL_BREAKPOINTS (bpt)
15370 {
15371 if (bpt->type == bp_single_step
15372 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15373 return 1;
15374 }
15375 return 0;
1aafd4da
UW
15376}
15377
a96d9b2e
SDJ
15378/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15379 non-zero otherwise. */
15380static int
15381is_syscall_catchpoint_enabled (struct breakpoint *bp)
15382{
15383 if (syscall_catchpoint_p (bp)
15384 && bp->enable_state != bp_disabled
15385 && bp->enable_state != bp_call_disabled)
15386 return 1;
15387 else
15388 return 0;
15389}
15390
15391int
15392catch_syscall_enabled (void)
15393{
fa3064dd
YQ
15394 struct catch_syscall_inferior_data *inf_data
15395 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15396
fa3064dd 15397 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15398}
15399
15400int
15401catching_syscall_number (int syscall_number)
15402{
15403 struct breakpoint *bp;
15404
15405 ALL_BREAKPOINTS (bp)
15406 if (is_syscall_catchpoint_enabled (bp))
15407 {
be5c67c1
PA
15408 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15409
15410 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15411 {
15412 int i, iter;
15413 for (i = 0;
be5c67c1 15414 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15415 i++)
15416 if (syscall_number == iter)
15417 return 1;
15418 }
15419 else
15420 return 1;
15421 }
15422
15423 return 0;
15424}
15425
15426/* Complete syscall names. Used by "catch syscall". */
49c4e619 15427static VEC (char_ptr) *
a96d9b2e 15428catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15429 const char *text, const char *word)
a96d9b2e 15430{
458c8db8 15431 const char **list = get_syscall_names (get_current_arch ());
49c4e619 15432 VEC (char_ptr) *retlist
b45627a0 15433 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15434
c38eea1a
MS
15435 xfree (list);
15436 return retlist;
a96d9b2e
SDJ
15437}
15438
1042e4c0
SS
15439/* Tracepoint-specific operations. */
15440
15441/* Set tracepoint count to NUM. */
15442static void
15443set_tracepoint_count (int num)
15444{
15445 tracepoint_count = num;
4fa62494 15446 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15447}
15448
70221824 15449static void
1042e4c0
SS
15450trace_command (char *arg, int from_tty)
15451{
55aa24fb
SDJ
15452 struct breakpoint_ops *ops;
15453 const char *arg_cp = arg;
15454
15455 if (arg && probe_linespec_to_ops (&arg_cp))
15456 ops = &tracepoint_probe_breakpoint_ops;
15457 else
15458 ops = &tracepoint_breakpoint_ops;
15459
558a9d82
YQ
15460 create_breakpoint (get_current_arch (),
15461 arg,
15462 NULL, 0, NULL, 1 /* parse arg */,
15463 0 /* tempflag */,
15464 bp_tracepoint /* type_wanted */,
15465 0 /* Ignore count */,
15466 pending_break_support,
15467 ops,
15468 from_tty,
15469 1 /* enabled */,
15470 0 /* internal */, 0);
1042e4c0
SS
15471}
15472
70221824 15473static void
7a697b8d
SS
15474ftrace_command (char *arg, int from_tty)
15475{
558a9d82
YQ
15476 create_breakpoint (get_current_arch (),
15477 arg,
15478 NULL, 0, NULL, 1 /* parse arg */,
15479 0 /* tempflag */,
15480 bp_fast_tracepoint /* type_wanted */,
15481 0 /* Ignore count */,
15482 pending_break_support,
15483 &tracepoint_breakpoint_ops,
15484 from_tty,
15485 1 /* enabled */,
15486 0 /* internal */, 0);
0fb4aa4b
PA
15487}
15488
15489/* strace command implementation. Creates a static tracepoint. */
15490
70221824 15491static void
0fb4aa4b
PA
15492strace_command (char *arg, int from_tty)
15493{
983af33b
SDJ
15494 struct breakpoint_ops *ops;
15495
15496 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15497 or with a normal static tracepoint. */
61012eef 15498 if (arg && startswith (arg, "-m") && isspace (arg[2]))
983af33b
SDJ
15499 ops = &strace_marker_breakpoint_ops;
15500 else
15501 ops = &tracepoint_breakpoint_ops;
15502
558a9d82
YQ
15503 create_breakpoint (get_current_arch (),
15504 arg,
15505 NULL, 0, NULL, 1 /* parse arg */,
15506 0 /* tempflag */,
15507 bp_static_tracepoint /* type_wanted */,
15508 0 /* Ignore count */,
15509 pending_break_support,
15510 ops,
15511 from_tty,
15512 1 /* enabled */,
15513 0 /* internal */, 0);
7a697b8d
SS
15514}
15515
409873ef
SS
15516/* Set up a fake reader function that gets command lines from a linked
15517 list that was acquired during tracepoint uploading. */
15518
15519static struct uploaded_tp *this_utp;
3149d8c1 15520static int next_cmd;
409873ef
SS
15521
15522static char *
15523read_uploaded_action (void)
15524{
15525 char *rslt;
15526
3149d8c1 15527 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15528
3149d8c1 15529 next_cmd++;
409873ef
SS
15530
15531 return rslt;
15532}
15533
00bf0b85
SS
15534/* Given information about a tracepoint as recorded on a target (which
15535 can be either a live system or a trace file), attempt to create an
15536 equivalent GDB tracepoint. This is not a reliable process, since
15537 the target does not necessarily have all the information used when
15538 the tracepoint was originally defined. */
15539
d9b3f62e 15540struct tracepoint *
00bf0b85 15541create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15542{
409873ef 15543 char *addr_str, small_buf[100];
d9b3f62e 15544 struct tracepoint *tp;
fd9b8c24 15545
409873ef
SS
15546 if (utp->at_string)
15547 addr_str = utp->at_string;
15548 else
15549 {
15550 /* In the absence of a source location, fall back to raw
15551 address. Since there is no way to confirm that the address
15552 means the same thing as when the trace was started, warn the
15553 user. */
3e43a32a
MS
15554 warning (_("Uploaded tracepoint %d has no "
15555 "source location, using raw address"),
409873ef 15556 utp->number);
8c042590 15557 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15558 addr_str = small_buf;
15559 }
15560
15561 /* There's not much we can do with a sequence of bytecodes. */
15562 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15563 warning (_("Uploaded tracepoint %d condition "
15564 "has no source form, ignoring it"),
409873ef 15565 utp->number);
d5551862 15566
8cdf0e15 15567 if (!create_breakpoint (get_current_arch (),
409873ef 15568 addr_str,
e7e0cddf
SS
15569 utp->cond_string, -1, NULL,
15570 0 /* parse cond/thread */,
8cdf0e15 15571 0 /* tempflag */,
0fb4aa4b 15572 utp->type /* type_wanted */,
8cdf0e15
VP
15573 0 /* Ignore count */,
15574 pending_break_support,
348d480f 15575 &tracepoint_breakpoint_ops,
8cdf0e15 15576 0 /* from_tty */,
84f4c1fe 15577 utp->enabled /* enabled */,
44f238bb
PA
15578 0 /* internal */,
15579 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15580 return NULL;
15581
409873ef 15582 /* Get the tracepoint we just created. */
fd9b8c24
PA
15583 tp = get_tracepoint (tracepoint_count);
15584 gdb_assert (tp != NULL);
d5551862 15585
00bf0b85
SS
15586 if (utp->pass > 0)
15587 {
8c042590
PM
15588 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15589 tp->base.number);
00bf0b85 15590
409873ef 15591 trace_pass_command (small_buf, 0);
00bf0b85
SS
15592 }
15593
409873ef
SS
15594 /* If we have uploaded versions of the original commands, set up a
15595 special-purpose "reader" function and call the usual command line
15596 reader, then pass the result to the breakpoint command-setting
15597 function. */
3149d8c1 15598 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15599 {
409873ef 15600 struct command_line *cmd_list;
00bf0b85 15601
409873ef 15602 this_utp = utp;
3149d8c1 15603 next_cmd = 0;
d5551862 15604
409873ef
SS
15605 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15606
d9b3f62e 15607 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15608 }
3149d8c1
SS
15609 else if (!VEC_empty (char_ptr, utp->actions)
15610 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15611 warning (_("Uploaded tracepoint %d actions "
15612 "have no source form, ignoring them"),
409873ef 15613 utp->number);
00bf0b85 15614
f196051f
SS
15615 /* Copy any status information that might be available. */
15616 tp->base.hit_count = utp->hit_count;
15617 tp->traceframe_usage = utp->traceframe_usage;
15618
00bf0b85 15619 return tp;
d9b3f62e 15620}
00bf0b85 15621
1042e4c0
SS
15622/* Print information on tracepoint number TPNUM_EXP, or all if
15623 omitted. */
15624
15625static void
e5a67952 15626tracepoints_info (char *args, int from_tty)
1042e4c0 15627{
79a45e25 15628 struct ui_out *uiout = current_uiout;
e5a67952 15629 int num_printed;
1042e4c0 15630
e5a67952 15631 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15632
15633 if (num_printed == 0)
1042e4c0 15634 {
e5a67952 15635 if (args == NULL || *args == '\0')
d77f58be
SS
15636 ui_out_message (uiout, 0, "No tracepoints.\n");
15637 else
e5a67952 15638 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15639 }
ad443146
SS
15640
15641 default_collect_info ();
1042e4c0
SS
15642}
15643
4a64f543 15644/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15645 Not supported by all targets. */
15646static void
15647enable_trace_command (char *args, int from_tty)
15648{
15649 enable_command (args, from_tty);
15650}
15651
4a64f543 15652/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15653 Not supported by all targets. */
15654static void
15655disable_trace_command (char *args, int from_tty)
15656{
15657 disable_command (args, from_tty);
15658}
15659
4a64f543 15660/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15661static void
15662delete_trace_command (char *arg, int from_tty)
15663{
35df4500 15664 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15665
15666 dont_repeat ();
15667
15668 if (arg == 0)
15669 {
15670 int breaks_to_delete = 0;
15671
15672 /* Delete all breakpoints if no argument.
15673 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15674 have to be deleted with an explicit breakpoint number
15675 argument. */
1042e4c0 15676 ALL_TRACEPOINTS (b)
46c6471b 15677 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15678 {
15679 breaks_to_delete = 1;
15680 break;
15681 }
1042e4c0
SS
15682
15683 /* Ask user only if there are some breakpoints to delete. */
15684 if (!from_tty
15685 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15686 {
35df4500 15687 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15688 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15689 delete_breakpoint (b);
1042e4c0
SS
15690 }
15691 }
15692 else
51be5b68 15693 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15694}
15695
197f0a60
TT
15696/* Helper function for trace_pass_command. */
15697
15698static void
d9b3f62e 15699trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15700{
d9b3f62e 15701 tp->pass_count = count;
6f6484cd 15702 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15703 if (from_tty)
15704 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15705 tp->base.number, count);
197f0a60
TT
15706}
15707
1042e4c0
SS
15708/* Set passcount for tracepoint.
15709
15710 First command argument is passcount, second is tracepoint number.
15711 If tracepoint number omitted, apply to most recently defined.
15712 Also accepts special argument "all". */
15713
15714static void
15715trace_pass_command (char *args, int from_tty)
15716{
d9b3f62e 15717 struct tracepoint *t1;
1042e4c0 15718 unsigned int count;
1042e4c0
SS
15719
15720 if (args == 0 || *args == 0)
3e43a32a
MS
15721 error (_("passcount command requires an "
15722 "argument (count + optional TP num)"));
1042e4c0 15723
4a64f543 15724 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15725
529480d0 15726 args = skip_spaces (args);
1042e4c0
SS
15727 if (*args && strncasecmp (args, "all", 3) == 0)
15728 {
d9b3f62e
PA
15729 struct breakpoint *b;
15730
1042e4c0 15731 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15732 if (*args)
15733 error (_("Junk at end of arguments."));
1042e4c0 15734
d9b3f62e 15735 ALL_TRACEPOINTS (b)
197f0a60 15736 {
d9b3f62e 15737 t1 = (struct tracepoint *) b;
197f0a60
TT
15738 trace_pass_set_count (t1, count, from_tty);
15739 }
15740 }
15741 else if (*args == '\0')
1042e4c0 15742 {
5fa1d40e 15743 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15744 if (t1)
197f0a60
TT
15745 trace_pass_set_count (t1, count, from_tty);
15746 }
15747 else
15748 {
15749 struct get_number_or_range_state state;
15750
15751 init_number_or_range (&state, args);
15752 while (!state.finished)
1042e4c0 15753 {
5fa1d40e 15754 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15755 if (t1)
15756 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15757 }
15758 }
1042e4c0
SS
15759}
15760
d9b3f62e 15761struct tracepoint *
1042e4c0
SS
15762get_tracepoint (int num)
15763{
15764 struct breakpoint *t;
15765
15766 ALL_TRACEPOINTS (t)
15767 if (t->number == num)
d9b3f62e 15768 return (struct tracepoint *) t;
1042e4c0
SS
15769
15770 return NULL;
15771}
15772
d5551862
SS
15773/* Find the tracepoint with the given target-side number (which may be
15774 different from the tracepoint number after disconnecting and
15775 reconnecting). */
15776
d9b3f62e 15777struct tracepoint *
d5551862
SS
15778get_tracepoint_by_number_on_target (int num)
15779{
d9b3f62e 15780 struct breakpoint *b;
d5551862 15781
d9b3f62e
PA
15782 ALL_TRACEPOINTS (b)
15783 {
15784 struct tracepoint *t = (struct tracepoint *) b;
15785
15786 if (t->number_on_target == num)
15787 return t;
15788 }
d5551862
SS
15789
15790 return NULL;
15791}
15792
1042e4c0 15793/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15794 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15795 If the argument is missing, the most recent tracepoint
15796 (tracepoint_count) is returned. */
15797
d9b3f62e 15798struct tracepoint *
197f0a60 15799get_tracepoint_by_number (char **arg,
5fa1d40e 15800 struct get_number_or_range_state *state)
1042e4c0 15801{
1042e4c0
SS
15802 struct breakpoint *t;
15803 int tpnum;
15804 char *instring = arg == NULL ? NULL : *arg;
15805
197f0a60
TT
15806 if (state)
15807 {
15808 gdb_assert (!state->finished);
15809 tpnum = get_number_or_range (state);
15810 }
15811 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15812 tpnum = tracepoint_count;
1042e4c0 15813 else
197f0a60 15814 tpnum = get_number (arg);
1042e4c0
SS
15815
15816 if (tpnum <= 0)
15817 {
15818 if (instring && *instring)
15819 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15820 instring);
15821 else
5fa1d40e 15822 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15823 return NULL;
15824 }
15825
15826 ALL_TRACEPOINTS (t)
15827 if (t->number == tpnum)
15828 {
d9b3f62e 15829 return (struct tracepoint *) t;
1042e4c0
SS
15830 }
15831
1042e4c0
SS
15832 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15833 return NULL;
15834}
15835
d9b3f62e
PA
15836void
15837print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15838{
15839 if (b->thread != -1)
15840 fprintf_unfiltered (fp, " thread %d", b->thread);
15841
15842 if (b->task != 0)
15843 fprintf_unfiltered (fp, " task %d", b->task);
15844
15845 fprintf_unfiltered (fp, "\n");
15846}
15847
6149aea9
PA
15848/* Save information on user settable breakpoints (watchpoints, etc) to
15849 a new script file named FILENAME. If FILTER is non-NULL, call it
15850 on each breakpoint and only include the ones for which it returns
15851 non-zero. */
15852
1042e4c0 15853static void
6149aea9
PA
15854save_breakpoints (char *filename, int from_tty,
15855 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15856{
15857 struct breakpoint *tp;
6149aea9 15858 int any = 0;
1042e4c0 15859 struct cleanup *cleanup;
a7bdde9e 15860 struct ui_file *fp;
6149aea9 15861 int extra_trace_bits = 0;
1042e4c0 15862
6149aea9
PA
15863 if (filename == 0 || *filename == 0)
15864 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15865
15866 /* See if we have anything to save. */
6149aea9 15867 ALL_BREAKPOINTS (tp)
1042e4c0 15868 {
6149aea9 15869 /* Skip internal and momentary breakpoints. */
09d682a4 15870 if (!user_breakpoint_p (tp))
6149aea9
PA
15871 continue;
15872
15873 /* If we have a filter, only save the breakpoints it accepts. */
15874 if (filter && !filter (tp))
15875 continue;
15876
15877 any = 1;
15878
15879 if (is_tracepoint (tp))
15880 {
15881 extra_trace_bits = 1;
15882
15883 /* We can stop searching. */
15884 break;
15885 }
1042e4c0 15886 }
6149aea9
PA
15887
15888 if (!any)
1042e4c0 15889 {
6149aea9 15890 warning (_("Nothing to save."));
1042e4c0
SS
15891 return;
15892 }
15893
c718be47
PA
15894 filename = tilde_expand (filename);
15895 cleanup = make_cleanup (xfree, filename);
15896 fp = gdb_fopen (filename, "w");
059fb39f 15897 if (!fp)
6149aea9
PA
15898 error (_("Unable to open file '%s' for saving (%s)"),
15899 filename, safe_strerror (errno));
a7bdde9e 15900 make_cleanup_ui_file_delete (fp);
8bf6485c 15901
6149aea9
PA
15902 if (extra_trace_bits)
15903 save_trace_state_variables (fp);
8bf6485c 15904
6149aea9 15905 ALL_BREAKPOINTS (tp)
1042e4c0 15906 {
6149aea9 15907 /* Skip internal and momentary breakpoints. */
09d682a4 15908 if (!user_breakpoint_p (tp))
6149aea9 15909 continue;
8bf6485c 15910
6149aea9
PA
15911 /* If we have a filter, only save the breakpoints it accepts. */
15912 if (filter && !filter (tp))
15913 continue;
15914
348d480f 15915 tp->ops->print_recreate (tp, fp);
1042e4c0 15916
6149aea9
PA
15917 /* Note, we can't rely on tp->number for anything, as we can't
15918 assume the recreated breakpoint numbers will match. Use $bpnum
15919 instead. */
15920
15921 if (tp->cond_string)
15922 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15923
15924 if (tp->ignore_count)
15925 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15926
2d9442cc 15927 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15928 {
a7bdde9e 15929
6149aea9 15930 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15931
79a45e25 15932 ui_out_redirect (current_uiout, fp);
492d29ea 15933 TRY
1042e4c0 15934 {
79a45e25 15935 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15936 }
79a45e25 15937 ui_out_redirect (current_uiout, NULL);
1042e4c0 15938
492d29ea
PA
15939 CATCH (ex, RETURN_MASK_ALL)
15940 {
15941 throw_exception (ex);
15942 }
15943 END_CATCH
1042e4c0 15944
a7bdde9e 15945 fprintf_unfiltered (fp, " end\n");
1042e4c0 15946 }
6149aea9
PA
15947
15948 if (tp->enable_state == bp_disabled)
99894e11 15949 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15950
15951 /* If this is a multi-location breakpoint, check if the locations
15952 should be individually disabled. Watchpoint locations are
15953 special, and not user visible. */
15954 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15955 {
15956 struct bp_location *loc;
15957 int n = 1;
15958
15959 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15960 if (!loc->enabled)
15961 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15962 }
1042e4c0 15963 }
8bf6485c 15964
6149aea9 15965 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15966 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15967
1042e4c0 15968 if (from_tty)
6149aea9 15969 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15970 do_cleanups (cleanup);
6149aea9
PA
15971}
15972
15973/* The `save breakpoints' command. */
15974
15975static void
15976save_breakpoints_command (char *args, int from_tty)
15977{
15978 save_breakpoints (args, from_tty, NULL);
15979}
15980
15981/* The `save tracepoints' command. */
15982
15983static void
15984save_tracepoints_command (char *args, int from_tty)
15985{
15986 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15987}
15988
15989/* Create a vector of all tracepoints. */
15990
15991VEC(breakpoint_p) *
eeae04df 15992all_tracepoints (void)
1042e4c0
SS
15993{
15994 VEC(breakpoint_p) *tp_vec = 0;
15995 struct breakpoint *tp;
15996
15997 ALL_TRACEPOINTS (tp)
15998 {
15999 VEC_safe_push (breakpoint_p, tp_vec, tp);
16000 }
16001
16002 return tp_vec;
16003}
16004
c906108c 16005\f
4a64f543
MS
16006/* This help string is used for the break, hbreak, tbreak and thbreak
16007 commands. It is defined as a macro to prevent duplication.
16008 COMMAND should be a string constant containing the name of the
16009 command. */
31e2b00f 16010#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
16011command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
16012PROBE_MODIFIER shall be present if the command is to be placed in a\n\
16013probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
16014guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
16015`-probe-dtrace' (for a DTrace probe).\n\
31e2b00f
AS
16016LOCATION may be a line number, function name, or \"*\" and an address.\n\
16017If a line number is specified, break at start of code for that line.\n\
16018If a function is specified, break at start of code for that function.\n\
16019If an address is specified, break at that exact address.\n\
dc10affe
PA
16020With no LOCATION, uses current execution address of the selected\n\
16021stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
16022\n\
16023THREADNUM is the number from \"info threads\".\n\
16024CONDITION is a boolean expression.\n\
16025\n\
d41c0fc8
PA
16026Multiple breakpoints at one place are permitted, and useful if their\n\
16027conditions are different.\n\
31e2b00f
AS
16028\n\
16029Do \"help breakpoints\" for info on other commands dealing with breakpoints."
16030
44feb3ce
TT
16031/* List of subcommands for "catch". */
16032static struct cmd_list_element *catch_cmdlist;
16033
16034/* List of subcommands for "tcatch". */
16035static struct cmd_list_element *tcatch_cmdlist;
16036
9ac4176b 16037void
44feb3ce 16038add_catch_command (char *name, char *docstring,
82ae6c8d 16039 cmd_sfunc_ftype *sfunc,
625e8578 16040 completer_ftype *completer,
44feb3ce
TT
16041 void *user_data_catch,
16042 void *user_data_tcatch)
16043{
16044 struct cmd_list_element *command;
16045
16046 command = add_cmd (name, class_breakpoint, NULL, docstring,
16047 &catch_cmdlist);
16048 set_cmd_sfunc (command, sfunc);
16049 set_cmd_context (command, user_data_catch);
a96d9b2e 16050 set_cmd_completer (command, completer);
44feb3ce
TT
16051
16052 command = add_cmd (name, class_breakpoint, NULL, docstring,
16053 &tcatch_cmdlist);
16054 set_cmd_sfunc (command, sfunc);
16055 set_cmd_context (command, user_data_tcatch);
a96d9b2e 16056 set_cmd_completer (command, completer);
44feb3ce
TT
16057}
16058
6c95b8df 16059static void
a79b8f6e 16060clear_syscall_counts (struct inferior *inf)
6c95b8df 16061{
fa3064dd
YQ
16062 struct catch_syscall_inferior_data *inf_data
16063 = get_catch_syscall_inferior_data (inf);
16064
16065 inf_data->total_syscalls_count = 0;
16066 inf_data->any_syscall_count = 0;
16067 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
16068}
16069
6149aea9
PA
16070static void
16071save_command (char *arg, int from_tty)
16072{
3e43a32a
MS
16073 printf_unfiltered (_("\"save\" must be followed by "
16074 "the name of a save subcommand.\n"));
635c7e8a 16075 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
16076}
16077
84f4c1fe
PM
16078struct breakpoint *
16079iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16080 void *data)
16081{
35df4500 16082 struct breakpoint *b, *b_tmp;
84f4c1fe 16083
35df4500 16084 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
16085 {
16086 if ((*callback) (b, data))
16087 return b;
16088 }
16089
16090 return NULL;
16091}
16092
0574c78f
GB
16093/* Zero if any of the breakpoint's locations could be a location where
16094 functions have been inlined, nonzero otherwise. */
16095
16096static int
16097is_non_inline_function (struct breakpoint *b)
16098{
16099 /* The shared library event breakpoint is set on the address of a
16100 non-inline function. */
16101 if (b->type == bp_shlib_event)
16102 return 1;
16103
16104 return 0;
16105}
16106
16107/* Nonzero if the specified PC cannot be a location where functions
16108 have been inlined. */
16109
16110int
09ac7c10
TT
16111pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16112 const struct target_waitstatus *ws)
0574c78f
GB
16113{
16114 struct breakpoint *b;
16115 struct bp_location *bl;
16116
16117 ALL_BREAKPOINTS (b)
16118 {
16119 if (!is_non_inline_function (b))
16120 continue;
16121
16122 for (bl = b->loc; bl != NULL; bl = bl->next)
16123 {
16124 if (!bl->shlib_disabled
09ac7c10 16125 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
16126 return 1;
16127 }
16128 }
16129
16130 return 0;
16131}
16132
2f202fde
JK
16133/* Remove any references to OBJFILE which is going to be freed. */
16134
16135void
16136breakpoint_free_objfile (struct objfile *objfile)
16137{
16138 struct bp_location **locp, *loc;
16139
16140 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 16141 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
16142 loc->symtab = NULL;
16143}
16144
2060206e
PA
16145void
16146initialize_breakpoint_ops (void)
16147{
16148 static int initialized = 0;
16149
16150 struct breakpoint_ops *ops;
16151
16152 if (initialized)
16153 return;
16154 initialized = 1;
16155
16156 /* The breakpoint_ops structure to be inherit by all kinds of
16157 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16158 internal and momentary breakpoints, etc.). */
16159 ops = &bkpt_base_breakpoint_ops;
16160 *ops = base_breakpoint_ops;
16161 ops->re_set = bkpt_re_set;
16162 ops->insert_location = bkpt_insert_location;
16163 ops->remove_location = bkpt_remove_location;
16164 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
16165 ops->create_sals_from_address = bkpt_create_sals_from_address;
16166 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16167 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
16168
16169 /* The breakpoint_ops structure to be used in regular breakpoints. */
16170 ops = &bkpt_breakpoint_ops;
16171 *ops = bkpt_base_breakpoint_ops;
16172 ops->re_set = bkpt_re_set;
16173 ops->resources_needed = bkpt_resources_needed;
16174 ops->print_it = bkpt_print_it;
16175 ops->print_mention = bkpt_print_mention;
16176 ops->print_recreate = bkpt_print_recreate;
16177
16178 /* Ranged breakpoints. */
16179 ops = &ranged_breakpoint_ops;
16180 *ops = bkpt_breakpoint_ops;
16181 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16182 ops->resources_needed = resources_needed_ranged_breakpoint;
16183 ops->print_it = print_it_ranged_breakpoint;
16184 ops->print_one = print_one_ranged_breakpoint;
16185 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16186 ops->print_mention = print_mention_ranged_breakpoint;
16187 ops->print_recreate = print_recreate_ranged_breakpoint;
16188
16189 /* Internal breakpoints. */
16190 ops = &internal_breakpoint_ops;
16191 *ops = bkpt_base_breakpoint_ops;
16192 ops->re_set = internal_bkpt_re_set;
16193 ops->check_status = internal_bkpt_check_status;
16194 ops->print_it = internal_bkpt_print_it;
16195 ops->print_mention = internal_bkpt_print_mention;
16196
16197 /* Momentary breakpoints. */
16198 ops = &momentary_breakpoint_ops;
16199 *ops = bkpt_base_breakpoint_ops;
16200 ops->re_set = momentary_bkpt_re_set;
16201 ops->check_status = momentary_bkpt_check_status;
16202 ops->print_it = momentary_bkpt_print_it;
16203 ops->print_mention = momentary_bkpt_print_mention;
16204
e2e4d78b
JK
16205 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16206 ops = &longjmp_breakpoint_ops;
16207 *ops = momentary_breakpoint_ops;
16208 ops->dtor = longjmp_bkpt_dtor;
16209
55aa24fb
SDJ
16210 /* Probe breakpoints. */
16211 ops = &bkpt_probe_breakpoint_ops;
16212 *ops = bkpt_breakpoint_ops;
16213 ops->insert_location = bkpt_probe_insert_location;
16214 ops->remove_location = bkpt_probe_remove_location;
16215 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16216 ops->decode_linespec = bkpt_probe_decode_linespec;
16217
2060206e
PA
16218 /* Watchpoints. */
16219 ops = &watchpoint_breakpoint_ops;
16220 *ops = base_breakpoint_ops;
3a5c3e22 16221 ops->dtor = dtor_watchpoint;
2060206e
PA
16222 ops->re_set = re_set_watchpoint;
16223 ops->insert_location = insert_watchpoint;
16224 ops->remove_location = remove_watchpoint;
16225 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16226 ops->check_status = check_status_watchpoint;
16227 ops->resources_needed = resources_needed_watchpoint;
16228 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16229 ops->print_it = print_it_watchpoint;
16230 ops->print_mention = print_mention_watchpoint;
16231 ops->print_recreate = print_recreate_watchpoint;
427cd150 16232 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16233
16234 /* Masked watchpoints. */
16235 ops = &masked_watchpoint_breakpoint_ops;
16236 *ops = watchpoint_breakpoint_ops;
16237 ops->insert_location = insert_masked_watchpoint;
16238 ops->remove_location = remove_masked_watchpoint;
16239 ops->resources_needed = resources_needed_masked_watchpoint;
16240 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16241 ops->print_it = print_it_masked_watchpoint;
16242 ops->print_one_detail = print_one_detail_masked_watchpoint;
16243 ops->print_mention = print_mention_masked_watchpoint;
16244 ops->print_recreate = print_recreate_masked_watchpoint;
16245
16246 /* Tracepoints. */
16247 ops = &tracepoint_breakpoint_ops;
16248 *ops = base_breakpoint_ops;
16249 ops->re_set = tracepoint_re_set;
16250 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16251 ops->print_one_detail = tracepoint_print_one_detail;
16252 ops->print_mention = tracepoint_print_mention;
16253 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16254 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16255 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16256 ops->decode_linespec = tracepoint_decode_linespec;
16257
55aa24fb
SDJ
16258 /* Probe tracepoints. */
16259 ops = &tracepoint_probe_breakpoint_ops;
16260 *ops = tracepoint_breakpoint_ops;
16261 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16262 ops->decode_linespec = tracepoint_probe_decode_linespec;
16263
983af33b
SDJ
16264 /* Static tracepoints with marker (`-m'). */
16265 ops = &strace_marker_breakpoint_ops;
16266 *ops = tracepoint_breakpoint_ops;
16267 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16268 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16269 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16270
16271 /* Fork catchpoints. */
16272 ops = &catch_fork_breakpoint_ops;
16273 *ops = base_breakpoint_ops;
16274 ops->insert_location = insert_catch_fork;
16275 ops->remove_location = remove_catch_fork;
16276 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16277 ops->print_it = print_it_catch_fork;
16278 ops->print_one = print_one_catch_fork;
16279 ops->print_mention = print_mention_catch_fork;
16280 ops->print_recreate = print_recreate_catch_fork;
16281
16282 /* Vfork catchpoints. */
16283 ops = &catch_vfork_breakpoint_ops;
16284 *ops = base_breakpoint_ops;
16285 ops->insert_location = insert_catch_vfork;
16286 ops->remove_location = remove_catch_vfork;
16287 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16288 ops->print_it = print_it_catch_vfork;
16289 ops->print_one = print_one_catch_vfork;
16290 ops->print_mention = print_mention_catch_vfork;
16291 ops->print_recreate = print_recreate_catch_vfork;
16292
16293 /* Exec catchpoints. */
16294 ops = &catch_exec_breakpoint_ops;
16295 *ops = base_breakpoint_ops;
16296 ops->dtor = dtor_catch_exec;
16297 ops->insert_location = insert_catch_exec;
16298 ops->remove_location = remove_catch_exec;
16299 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16300 ops->print_it = print_it_catch_exec;
16301 ops->print_one = print_one_catch_exec;
16302 ops->print_mention = print_mention_catch_exec;
16303 ops->print_recreate = print_recreate_catch_exec;
16304
16305 /* Syscall catchpoints. */
16306 ops = &catch_syscall_breakpoint_ops;
16307 *ops = base_breakpoint_ops;
16308 ops->dtor = dtor_catch_syscall;
16309 ops->insert_location = insert_catch_syscall;
16310 ops->remove_location = remove_catch_syscall;
16311 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16312 ops->print_it = print_it_catch_syscall;
16313 ops->print_one = print_one_catch_syscall;
16314 ops->print_mention = print_mention_catch_syscall;
16315 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16316
16317 /* Solib-related catchpoints. */
16318 ops = &catch_solib_breakpoint_ops;
16319 *ops = base_breakpoint_ops;
16320 ops->dtor = dtor_catch_solib;
16321 ops->insert_location = insert_catch_solib;
16322 ops->remove_location = remove_catch_solib;
16323 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16324 ops->check_status = check_status_catch_solib;
16325 ops->print_it = print_it_catch_solib;
16326 ops->print_one = print_one_catch_solib;
16327 ops->print_mention = print_mention_catch_solib;
16328 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16329
16330 ops = &dprintf_breakpoint_ops;
16331 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16332 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16333 ops->resources_needed = bkpt_resources_needed;
16334 ops->print_it = bkpt_print_it;
16335 ops->print_mention = bkpt_print_mention;
2d9442cc 16336 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16337 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16338 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16339}
16340
8bfd80db
YQ
16341/* Chain containing all defined "enable breakpoint" subcommands. */
16342
16343static struct cmd_list_element *enablebreaklist = NULL;
16344
c906108c 16345void
fba45db2 16346_initialize_breakpoint (void)
c906108c
SS
16347{
16348 struct cmd_list_element *c;
16349
2060206e
PA
16350 initialize_breakpoint_ops ();
16351
84acb35a 16352 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16353 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16354 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16355 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16356
55aa24fb
SDJ
16357 breakpoint_objfile_key
16358 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16359
fa3064dd 16360 catch_syscall_inferior_data
8e260fc0
TT
16361 = register_inferior_data_with_cleanup (NULL,
16362 catch_syscall_inferior_data_cleanup);
fa3064dd 16363
c906108c
SS
16364 breakpoint_chain = 0;
16365 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16366 before a breakpoint is set. */
16367 breakpoint_count = 0;
16368
1042e4c0
SS
16369 tracepoint_count = 0;
16370
1bedd215
AC
16371 add_com ("ignore", class_breakpoint, ignore_command, _("\
16372Set ignore-count of breakpoint number N to COUNT.\n\
16373Usage is `ignore N COUNT'."));
c906108c 16374 if (xdb_commands)
c5aa993b 16375 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16376
1bedd215
AC
16377 add_com ("commands", class_breakpoint, commands_command, _("\
16378Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16379Give breakpoint number as argument after \"commands\".\n\
16380With no argument, the targeted breakpoint is the last one set.\n\
16381The commands themselves follow starting on the next line.\n\
16382Type a line containing \"end\" to indicate the end of them.\n\
16383Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16384then no output is printed when it is hit, except what the commands print."));
c906108c 16385
d55637df 16386 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16387Specify breakpoint number N to break only if COND is true.\n\
c906108c 16388Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16389expression to be evaluated whenever breakpoint N is reached."));
d55637df 16390 set_cmd_completer (c, condition_completer);
c906108c 16391
1bedd215 16392 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16393Set a temporary breakpoint.\n\
c906108c
SS
16394Like \"break\" except the breakpoint is only temporary,\n\
16395so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16396by using \"enable delete\" on the breakpoint number.\n\
16397\n"
16398BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16399 set_cmd_completer (c, location_completer);
c94fdfd0 16400
1bedd215 16401 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16402Set a hardware assisted breakpoint.\n\
c906108c 16403Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16404some target hardware may not have this support.\n\
16405\n"
16406BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16407 set_cmd_completer (c, location_completer);
c906108c 16408
1bedd215 16409 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16410Set a temporary hardware assisted breakpoint.\n\
c906108c 16411Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16412so it will be deleted when hit.\n\
16413\n"
16414BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16415 set_cmd_completer (c, location_completer);
c906108c 16416
1bedd215
AC
16417 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16418Enable some breakpoints.\n\
c906108c
SS
16419Give breakpoint numbers (separated by spaces) as arguments.\n\
16420With no subcommand, breakpoints are enabled until you command otherwise.\n\
16421This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16422With a subcommand you can enable temporarily."),
c906108c
SS
16423 &enablelist, "enable ", 1, &cmdlist);
16424 if (xdb_commands)
1bedd215
AC
16425 add_com ("ab", class_breakpoint, enable_command, _("\
16426Enable some breakpoints.\n\
c906108c
SS
16427Give breakpoint numbers (separated by spaces) as arguments.\n\
16428With no subcommand, breakpoints are enabled until you command otherwise.\n\
16429This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16430With a subcommand you can enable temporarily."));
c906108c
SS
16431
16432 add_com_alias ("en", "enable", class_breakpoint, 1);
16433
84951ab5 16434 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16435Enable some breakpoints.\n\
c906108c
SS
16436Give breakpoint numbers (separated by spaces) as arguments.\n\
16437This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16438May be abbreviated to simply \"enable\".\n"),
c5aa993b 16439 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16440
1a966eab
AC
16441 add_cmd ("once", no_class, enable_once_command, _("\
16442Enable breakpoints for one hit. Give breakpoint numbers.\n\
16443If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16444 &enablebreaklist);
16445
1a966eab
AC
16446 add_cmd ("delete", no_class, enable_delete_command, _("\
16447Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16448If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16449 &enablebreaklist);
16450
816338b5
SS
16451 add_cmd ("count", no_class, enable_count_command, _("\
16452Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16453If a breakpoint is hit while enabled in this fashion,\n\
16454the count is decremented; when it reaches zero, the breakpoint is disabled."),
16455 &enablebreaklist);
16456
1a966eab
AC
16457 add_cmd ("delete", no_class, enable_delete_command, _("\
16458Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16459If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16460 &enablelist);
16461
1a966eab
AC
16462 add_cmd ("once", no_class, enable_once_command, _("\
16463Enable breakpoints for one hit. Give breakpoint numbers.\n\
16464If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16465 &enablelist);
16466
16467 add_cmd ("count", no_class, enable_count_command, _("\
16468Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16469If a breakpoint is hit while enabled in this fashion,\n\
16470the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16471 &enablelist);
16472
1bedd215
AC
16473 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16474Disable some breakpoints.\n\
c906108c
SS
16475Arguments are breakpoint numbers with spaces in between.\n\
16476To disable all breakpoints, give no argument.\n\
64b9b334 16477A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16478 &disablelist, "disable ", 1, &cmdlist);
16479 add_com_alias ("dis", "disable", class_breakpoint, 1);
16480 add_com_alias ("disa", "disable", class_breakpoint, 1);
16481 if (xdb_commands)
1bedd215
AC
16482 add_com ("sb", class_breakpoint, disable_command, _("\
16483Disable some breakpoints.\n\
c906108c
SS
16484Arguments are breakpoint numbers with spaces in between.\n\
16485To disable all breakpoints, give no argument.\n\
64b9b334 16486A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16487
1a966eab
AC
16488 add_cmd ("breakpoints", class_alias, disable_command, _("\
16489Disable some breakpoints.\n\
c906108c
SS
16490Arguments are breakpoint numbers with spaces in between.\n\
16491To disable all breakpoints, give no argument.\n\
64b9b334 16492A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16493This command may be abbreviated \"disable\"."),
c906108c
SS
16494 &disablelist);
16495
1bedd215
AC
16496 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16497Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16498Arguments are breakpoint numbers with spaces in between.\n\
16499To delete all breakpoints, give no argument.\n\
16500\n\
16501Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16502The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16503 &deletelist, "delete ", 1, &cmdlist);
16504 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16505 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16506 if (xdb_commands)
1bedd215
AC
16507 add_com ("db", class_breakpoint, delete_command, _("\
16508Delete some breakpoints.\n\
c906108c 16509Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16510To delete all breakpoints, give no argument.\n"));
c906108c 16511
1a966eab
AC
16512 add_cmd ("breakpoints", class_alias, delete_command, _("\
16513Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16514Arguments are breakpoint numbers with spaces in between.\n\
16515To delete all breakpoints, give no argument.\n\
1a966eab 16516This command may be abbreviated \"delete\"."),
c906108c
SS
16517 &deletelist);
16518
1bedd215
AC
16519 add_com ("clear", class_breakpoint, clear_command, _("\
16520Clear breakpoint at specified line or function.\n\
c906108c
SS
16521Argument may be line number, function name, or \"*\" and an address.\n\
16522If line number is specified, all breakpoints in that line are cleared.\n\
16523If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16524If an address is specified, breakpoints at that address are cleared.\n\
16525\n\
16526With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16527is executing in.\n\
16528\n\
1bedd215 16529See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16530 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16531
1bedd215 16532 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16533Set breakpoint at specified line or function.\n"
16534BREAK_ARGS_HELP ("break")));
5ba2abeb 16535 set_cmd_completer (c, location_completer);
c94fdfd0 16536
c906108c
SS
16537 add_com_alias ("b", "break", class_run, 1);
16538 add_com_alias ("br", "break", class_run, 1);
16539 add_com_alias ("bre", "break", class_run, 1);
16540 add_com_alias ("brea", "break", class_run, 1);
16541
7681d515
PM
16542 if (xdb_commands)
16543 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16544
16545 if (dbx_commands)
16546 {
1bedd215
AC
16547 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16548Break in function/address or break at a line in the current file."),
c5aa993b
JM
16549 &stoplist, "stop ", 1, &cmdlist);
16550 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16551 _("Break in function or address."), &stoplist);
c5aa993b 16552 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16553 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16554 add_com ("status", class_info, breakpoints_info, _("\
16555Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16556The \"Type\" column indicates one of:\n\
16557\tbreakpoint - normal breakpoint\n\
16558\twatchpoint - watchpoint\n\
16559The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16560the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16561breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16562address and file/line number respectively.\n\
16563\n\
16564Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16565are set to the address of the last breakpoint listed unless the command\n\
16566is prefixed with \"server \".\n\n\
c906108c 16567Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16568breakpoint set."));
c906108c
SS
16569 }
16570
1bedd215 16571 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16572Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16573The \"Type\" column indicates one of:\n\
16574\tbreakpoint - normal breakpoint\n\
16575\twatchpoint - watchpoint\n\
16576The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16577the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16578breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16579address and file/line number respectively.\n\
16580\n\
16581Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16582are set to the address of the last breakpoint listed unless the command\n\
16583is prefixed with \"server \".\n\n\
c906108c 16584Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16585breakpoint set."));
c906108c 16586
6b04bdb7
MS
16587 add_info_alias ("b", "breakpoints", 1);
16588
c906108c 16589 if (xdb_commands)
1bedd215
AC
16590 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16591Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16592The \"Type\" column indicates one of:\n\
16593\tbreakpoint - normal breakpoint\n\
16594\twatchpoint - watchpoint\n\
16595The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16596the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16597breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16598address and file/line number respectively.\n\
16599\n\
16600Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16601are set to the address of the last breakpoint listed unless the command\n\
16602is prefixed with \"server \".\n\n\
c906108c 16603Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16604breakpoint set."));
c906108c 16605
1a966eab
AC
16606 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16607Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16608The \"Type\" column indicates one of:\n\
16609\tbreakpoint - normal breakpoint\n\
16610\twatchpoint - watchpoint\n\
16611\tlongjmp - internal breakpoint used to step through longjmp()\n\
16612\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16613\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16614\tfinish - internal breakpoint used by the \"finish\" command\n\
16615The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16616the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16617breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16618address and file/line number respectively.\n\
16619\n\
16620Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16621are set to the address of the last breakpoint listed unless the command\n\
16622is prefixed with \"server \".\n\n\
c906108c 16623Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16624breakpoint set."),
c906108c
SS
16625 &maintenanceinfolist);
16626
44feb3ce
TT
16627 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16628Set catchpoints to catch events."),
16629 &catch_cmdlist, "catch ",
16630 0/*allow-unknown*/, &cmdlist);
16631
16632 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16633Set temporary catchpoints to catch events."),
16634 &tcatch_cmdlist, "tcatch ",
16635 0/*allow-unknown*/, &cmdlist);
16636
44feb3ce
TT
16637 add_catch_command ("fork", _("Catch calls to fork."),
16638 catch_fork_command_1,
a96d9b2e 16639 NULL,
44feb3ce
TT
16640 (void *) (uintptr_t) catch_fork_permanent,
16641 (void *) (uintptr_t) catch_fork_temporary);
16642 add_catch_command ("vfork", _("Catch calls to vfork."),
16643 catch_fork_command_1,
a96d9b2e 16644 NULL,
44feb3ce
TT
16645 (void *) (uintptr_t) catch_vfork_permanent,
16646 (void *) (uintptr_t) catch_vfork_temporary);
16647 add_catch_command ("exec", _("Catch calls to exec."),
16648 catch_exec_command_1,
a96d9b2e
SDJ
16649 NULL,
16650 CATCH_PERMANENT,
16651 CATCH_TEMPORARY);
edcc5120
TT
16652 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16653Usage: catch load [REGEX]\n\
16654If REGEX is given, only stop for libraries matching the regular expression."),
16655 catch_load_command_1,
16656 NULL,
16657 CATCH_PERMANENT,
16658 CATCH_TEMPORARY);
16659 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16660Usage: catch unload [REGEX]\n\
16661If REGEX is given, only stop for libraries matching the regular expression."),
16662 catch_unload_command_1,
16663 NULL,
16664 CATCH_PERMANENT,
16665 CATCH_TEMPORARY);
a96d9b2e
SDJ
16666 add_catch_command ("syscall", _("\
16667Catch system calls by their names and/or numbers.\n\
16668Arguments say which system calls to catch. If no arguments\n\
16669are given, every system call will be caught.\n\
16670Arguments, if given, should be one or more system call names\n\
16671(if your system supports that), or system call numbers."),
16672 catch_syscall_command_1,
16673 catch_syscall_completer,
44feb3ce
TT
16674 CATCH_PERMANENT,
16675 CATCH_TEMPORARY);
c5aa993b 16676
1bedd215
AC
16677 c = add_com ("watch", class_breakpoint, watch_command, _("\
16678Set a watchpoint for an expression.\n\
06a64a0b 16679Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16680A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16681an expression changes.\n\
16682If -l or -location is given, this evaluates EXPRESSION and watches\n\
16683the memory to which it refers."));
65d12d83 16684 set_cmd_completer (c, expression_completer);
c906108c 16685
1bedd215
AC
16686 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16687Set a read watchpoint for an expression.\n\
06a64a0b 16688Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16689A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16690an expression is read.\n\
16691If -l or -location is given, this evaluates EXPRESSION and watches\n\
16692the memory to which it refers."));
65d12d83 16693 set_cmd_completer (c, expression_completer);
c906108c 16694
1bedd215
AC
16695 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16696Set a watchpoint for an expression.\n\
06a64a0b 16697Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16698A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16699an expression is either read or written.\n\
16700If -l or -location is given, this evaluates EXPRESSION and watches\n\
16701the memory to which it refers."));
65d12d83 16702 set_cmd_completer (c, expression_completer);
c906108c 16703
d77f58be 16704 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16705Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16706
920d2a44
AC
16707 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16708 respond to changes - contrary to the description. */
85c07804
AC
16709 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16710 &can_use_hw_watchpoints, _("\
16711Set debugger's willingness to use watchpoint hardware."), _("\
16712Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16713If zero, gdb will not use hardware for new watchpoints, even if\n\
16714such is available. (However, any hardware watchpoints that were\n\
16715created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16716hardware.)"),
16717 NULL,
920d2a44 16718 show_can_use_hw_watchpoints,
85c07804 16719 &setlist, &showlist);
c906108c
SS
16720
16721 can_use_hw_watchpoints = 1;
fa8d40ab 16722
1042e4c0
SS
16723 /* Tracepoint manipulation commands. */
16724
16725 c = add_com ("trace", class_breakpoint, trace_command, _("\
16726Set a tracepoint at specified line or function.\n\
16727\n"
16728BREAK_ARGS_HELP ("trace") "\n\
16729Do \"help tracepoints\" for info on other tracepoint commands."));
16730 set_cmd_completer (c, location_completer);
16731
16732 add_com_alias ("tp", "trace", class_alias, 0);
16733 add_com_alias ("tr", "trace", class_alias, 1);
16734 add_com_alias ("tra", "trace", class_alias, 1);
16735 add_com_alias ("trac", "trace", class_alias, 1);
16736
7a697b8d
SS
16737 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16738Set a fast tracepoint at specified line or function.\n\
16739\n"
16740BREAK_ARGS_HELP ("ftrace") "\n\
16741Do \"help tracepoints\" for info on other tracepoint commands."));
16742 set_cmd_completer (c, location_completer);
16743
0fb4aa4b
PA
16744 c = add_com ("strace", class_breakpoint, strace_command, _("\
16745Set a static tracepoint at specified line, function or marker.\n\
16746\n\
16747strace [LOCATION] [if CONDITION]\n\
16748LOCATION may be a line number, function name, \"*\" and an address,\n\
16749or -m MARKER_ID.\n\
16750If a line number is specified, probe the marker at start of code\n\
16751for that line. If a function is specified, probe the marker at start\n\
16752of code for that function. If an address is specified, probe the marker\n\
16753at that exact address. If a marker id is specified, probe the marker\n\
16754with that name. With no LOCATION, uses current execution address of\n\
16755the selected stack frame.\n\
16756Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16757This collects arbitrary user data passed in the probe point call to the\n\
16758tracing library. You can inspect it when analyzing the trace buffer,\n\
16759by printing the $_sdata variable like any other convenience variable.\n\
16760\n\
16761CONDITION is a boolean expression.\n\
16762\n\
d41c0fc8
PA
16763Multiple tracepoints at one place are permitted, and useful if their\n\
16764conditions are different.\n\
0fb4aa4b
PA
16765\n\
16766Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16767Do \"help tracepoints\" for info on other tracepoint commands."));
16768 set_cmd_completer (c, location_completer);
16769
1042e4c0 16770 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16771Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16772Convenience variable \"$tpnum\" contains the number of the\n\
16773last tracepoint set."));
16774
16775 add_info_alias ("tp", "tracepoints", 1);
16776
16777 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16778Delete specified tracepoints.\n\
16779Arguments are tracepoint numbers, separated by spaces.\n\
16780No argument means delete all tracepoints."),
16781 &deletelist);
7e20dfcd 16782 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16783
16784 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16785Disable specified tracepoints.\n\
16786Arguments are tracepoint numbers, separated by spaces.\n\
16787No argument means disable all tracepoints."),
16788 &disablelist);
16789 deprecate_cmd (c, "disable");
16790
16791 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16792Enable specified tracepoints.\n\
16793Arguments are tracepoint numbers, separated by spaces.\n\
16794No argument means enable all tracepoints."),
16795 &enablelist);
16796 deprecate_cmd (c, "enable");
16797
16798 add_com ("passcount", class_trace, trace_pass_command, _("\
16799Set the passcount for a tracepoint.\n\
16800The trace will end when the tracepoint has been passed 'count' times.\n\
16801Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16802if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16803
6149aea9
PA
16804 add_prefix_cmd ("save", class_breakpoint, save_command,
16805 _("Save breakpoint definitions as a script."),
16806 &save_cmdlist, "save ",
16807 0/*allow-unknown*/, &cmdlist);
16808
16809 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16810Save current breakpoint definitions as a script.\n\
cce7e648 16811This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16812catchpoints, tracepoints). Use the 'source' command in another debug\n\
16813session to restore them."),
16814 &save_cmdlist);
16815 set_cmd_completer (c, filename_completer);
16816
16817 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16818Save current tracepoint definitions as a script.\n\
6149aea9
PA
16819Use the 'source' command in another debug session to restore them."),
16820 &save_cmdlist);
1042e4c0
SS
16821 set_cmd_completer (c, filename_completer);
16822
6149aea9
PA
16823 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16824 deprecate_cmd (c, "save tracepoints");
16825
1bedd215 16826 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16827Breakpoint specific settings\n\
16828Configure various breakpoint-specific variables such as\n\
1bedd215 16829pending breakpoint behavior"),
fa8d40ab
JJ
16830 &breakpoint_set_cmdlist, "set breakpoint ",
16831 0/*allow-unknown*/, &setlist);
1bedd215 16832 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16833Breakpoint specific settings\n\
16834Configure various breakpoint-specific variables such as\n\
1bedd215 16835pending breakpoint behavior"),
fa8d40ab
JJ
16836 &breakpoint_show_cmdlist, "show breakpoint ",
16837 0/*allow-unknown*/, &showlist);
16838
7915a72c
AC
16839 add_setshow_auto_boolean_cmd ("pending", no_class,
16840 &pending_break_support, _("\
16841Set debugger's behavior regarding pending breakpoints."), _("\
16842Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16843If on, an unrecognized breakpoint location will cause gdb to create a\n\
16844pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16845an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16846user-query to see if a pending breakpoint should be created."),
2c5b56ce 16847 NULL,
920d2a44 16848 show_pending_break_support,
6e1d7d6c
AC
16849 &breakpoint_set_cmdlist,
16850 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16851
16852 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16853
16854 add_setshow_boolean_cmd ("auto-hw", no_class,
16855 &automatic_hardware_breakpoints, _("\
16856Set automatic usage of hardware breakpoints."), _("\
16857Show automatic usage of hardware breakpoints."), _("\
16858If set, the debugger will automatically use hardware breakpoints for\n\
16859breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16860a warning will be emitted for such breakpoints."),
16861 NULL,
16862 show_automatic_hardware_breakpoints,
16863 &breakpoint_set_cmdlist,
16864 &breakpoint_show_cmdlist);
74960c60 16865
a25a5a45
PA
16866 add_setshow_boolean_cmd ("always-inserted", class_support,
16867 &always_inserted_mode, _("\
74960c60
VP
16868Set mode for inserting breakpoints."), _("\
16869Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16870When this mode is on, breakpoints are inserted immediately as soon as\n\
16871they're created, kept inserted even when execution stops, and removed\n\
16872only when the user deletes them. When this mode is off (the default),\n\
16873breakpoints are inserted only when execution continues, and removed\n\
16874when execution stops."),
72d0e2c5
YQ
16875 NULL,
16876 &show_always_inserted_mode,
16877 &breakpoint_set_cmdlist,
16878 &breakpoint_show_cmdlist);
f1310107 16879
b775012e
LM
16880 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16881 condition_evaluation_enums,
16882 &condition_evaluation_mode_1, _("\
16883Set mode of breakpoint condition evaluation."), _("\
16884Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16885When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16886evaluated on the host's side by GDB. When it is set to \"target\",\n\
16887breakpoint conditions will be downloaded to the target (if the target\n\
16888supports such feature) and conditions will be evaluated on the target's side.\n\
16889If this is set to \"auto\" (default), this will be automatically set to\n\
16890\"target\" if it supports condition evaluation, otherwise it will\n\
16891be set to \"gdb\""),
16892 &set_condition_evaluation_mode,
16893 &show_condition_evaluation_mode,
16894 &breakpoint_set_cmdlist,
16895 &breakpoint_show_cmdlist);
16896
f1310107
TJB
16897 add_com ("break-range", class_breakpoint, break_range_command, _("\
16898Set a breakpoint for an address range.\n\
16899break-range START-LOCATION, END-LOCATION\n\
16900where START-LOCATION and END-LOCATION can be one of the following:\n\
16901 LINENUM, for that line in the current file,\n\
16902 FILE:LINENUM, for that line in that file,\n\
16903 +OFFSET, for that number of lines after the current line\n\
16904 or the start of the range\n\
16905 FUNCTION, for the first line in that function,\n\
16906 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16907 *ADDRESS, for the instruction at that address.\n\
16908\n\
16909The breakpoint will stop execution of the inferior whenever it executes\n\
16910an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16911range (including START-LOCATION and END-LOCATION)."));
16912
e7e0cddf
SS
16913 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16914Set a dynamic printf at specified line or function.\n\
16915dprintf location,format string,arg1,arg2,...\n\
16916location may be a line number, function name, or \"*\" and an address.\n\
16917If a line number is specified, break at start of code for that line.\n\
0e4777df 16918If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16919 set_cmd_completer (c, location_completer);
16920
16921 add_setshow_enum_cmd ("dprintf-style", class_support,
16922 dprintf_style_enums, &dprintf_style, _("\
16923Set the style of usage for dynamic printf."), _("\
16924Show the style of usage for dynamic printf."), _("\
16925This setting chooses how GDB will do a dynamic printf.\n\
16926If the value is \"gdb\", then the printing is done by GDB to its own\n\
16927console, as with the \"printf\" command.\n\
16928If the value is \"call\", the print is done by calling a function in your\n\
16929program; by default printf(), but you can choose a different function or\n\
16930output stream by setting dprintf-function and dprintf-channel."),
16931 update_dprintf_commands, NULL,
16932 &setlist, &showlist);
16933
16934 dprintf_function = xstrdup ("printf");
16935 add_setshow_string_cmd ("dprintf-function", class_support,
16936 &dprintf_function, _("\
16937Set the function to use for dynamic printf"), _("\
16938Show the function to use for dynamic printf"), NULL,
16939 update_dprintf_commands, NULL,
16940 &setlist, &showlist);
16941
16942 dprintf_channel = xstrdup ("");
16943 add_setshow_string_cmd ("dprintf-channel", class_support,
16944 &dprintf_channel, _("\
16945Set the channel to use for dynamic printf"), _("\
16946Show the channel to use for dynamic printf"), NULL,
16947 update_dprintf_commands, NULL,
16948 &setlist, &showlist);
16949
d3ce09f5
SS
16950 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16951 &disconnected_dprintf, _("\
16952Set whether dprintf continues after GDB disconnects."), _("\
16953Show whether dprintf continues after GDB disconnects."), _("\
16954Use this to let dprintf commands continue to hit and produce output\n\
16955even if GDB disconnects or detaches from the target."),
16956 NULL,
16957 NULL,
16958 &setlist, &showlist);
16959
16960 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16961agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16962(target agent only) This is useful for formatted output in user-defined commands."));
16963
765dc015 16964 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16965
16966 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16967 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16968}
This page took 2.932648 seconds and 4 git commands to generate.