Automatic date update in version.in
[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 volatile struct gdb_exception ex;
2225
2226 if (!cond)
2227 return NULL;
2228
2229 /* We don't want to stop processing, so catch any errors
2230 that may show up. */
2231 TRY_CATCH (ex, RETURN_MASK_ERROR)
2232 {
2233 aexpr = gen_eval_for_expr (scope, cond);
2234 }
2235
2236 if (ex.reason < 0)
2237 {
2238 /* If we got here, it means the condition could not be parsed to a valid
2239 bytecode expression and thus can't be evaluated on the target's side.
2240 It's no use iterating through the conditions. */
2241 return NULL;
2242 }
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;
d3ce09f5 2364 volatile struct gdb_exception ex;
bbc13ae3
KS
2365 const char *cmdrest;
2366 const char *format_start, *format_end;
d3ce09f5
SS
2367 struct format_piece *fpieces;
2368 int nargs;
2369 struct gdbarch *gdbarch = get_current_arch ();
2370
2371 if (!cmd)
2372 return NULL;
2373
2374 cmdrest = cmd;
2375
2376 if (*cmdrest == ',')
2377 ++cmdrest;
bbc13ae3 2378 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2379
2380 if (*cmdrest++ != '"')
2381 error (_("No format string following the location"));
2382
2383 format_start = cmdrest;
2384
2385 fpieces = parse_format_string (&cmdrest);
2386
2387 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2388
2389 format_end = cmdrest;
2390
2391 if (*cmdrest++ != '"')
2392 error (_("Bad format string, non-terminated '\"'."));
2393
bbc13ae3 2394 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2395
2396 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2397 error (_("Invalid argument syntax"));
2398
2399 if (*cmdrest == ',')
2400 cmdrest++;
bbc13ae3 2401 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2402
2403 /* For each argument, make an expression. */
2404
2405 argvec = (struct expression **) alloca (strlen (cmd)
2406 * sizeof (struct expression *));
2407
2408 nargs = 0;
2409 while (*cmdrest != '\0')
2410 {
bbc13ae3 2411 const char *cmd1;
d3ce09f5
SS
2412
2413 cmd1 = cmdrest;
2414 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2415 argvec[nargs++] = expr;
2416 cmdrest = cmd1;
2417 if (*cmdrest == ',')
2418 ++cmdrest;
2419 }
2420
2421 /* We don't want to stop processing, so catch any errors
2422 that may show up. */
2423 TRY_CATCH (ex, RETURN_MASK_ERROR)
2424 {
2425 aexpr = gen_printf (scope, gdbarch, 0, 0,
2426 format_start, format_end - format_start,
2427 fpieces, nargs, argvec);
2428 }
2429
752eb8b4
TT
2430 do_cleanups (old_cleanups);
2431
d3ce09f5
SS
2432 if (ex.reason < 0)
2433 {
2434 /* If we got here, it means the command could not be parsed to a valid
2435 bytecode expression and thus can't be evaluated on the target's side.
2436 It's no use iterating through the other commands. */
2437 return NULL;
2438 }
2439
d3ce09f5
SS
2440 /* We have a valid agent expression, return it. */
2441 return aexpr;
2442}
2443
2444/* Based on location BL, create a list of breakpoint commands to be
2445 passed on to the target. If we have duplicated locations with
2446 different commands, we will add any such to the list. */
2447
2448static void
2449build_target_command_list (struct bp_location *bl)
2450{
2451 struct bp_location **locp = NULL, **loc2p;
2452 int null_command_or_parse_error = 0;
2453 int modified = bl->needs_update;
2454 struct bp_location *loc;
2455
8b4f3082
PA
2456 /* Release commands left over from a previous insert. */
2457 VEC_free (agent_expr_p, bl->target_info.tcommands);
2458
41fac0cf 2459 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2460 return;
2461
41fac0cf
PA
2462 /* For now, limit to agent-style dprintf breakpoints. */
2463 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2464 return;
2465
41fac0cf
PA
2466 /* For now, if we have any duplicate location that isn't a dprintf,
2467 don't install the target-side commands, as that would make the
2468 breakpoint not be reported to the core, and we'd lose
2469 control. */
2470 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2471 {
2472 loc = (*loc2p);
2473 if (is_breakpoint (loc->owner)
2474 && loc->pspace->num == bl->pspace->num
2475 && loc->owner->type != bp_dprintf)
2476 return;
2477 }
2478
d3ce09f5
SS
2479 /* Do a first pass to check for locations with no assigned
2480 conditions or conditions that fail to parse to a valid agent expression
2481 bytecode. If any of these happen, then it's no use to send conditions
2482 to the target since this location will always trigger and generate a
2483 response back to GDB. */
2484 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2485 {
2486 loc = (*loc2p);
2487 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2488 {
2489 if (modified)
2490 {
2491 struct agent_expr *aexpr;
2492
2493 /* Re-parse the commands since something changed. In that
2494 case we already freed the command bytecodes (see
2495 force_breakpoint_reinsertion). We just
2496 need to parse the command to bytecodes again. */
2497 aexpr = parse_cmd_to_aexpr (bl->address,
2498 loc->owner->extra_string);
2499 loc->cmd_bytecode = aexpr;
2500
2501 if (!aexpr)
2502 continue;
2503 }
2504
2505 /* If we have a NULL bytecode expression, it means something
2506 went wrong or we have a null command expression. */
2507 if (!loc->cmd_bytecode)
2508 {
2509 null_command_or_parse_error = 1;
2510 break;
2511 }
2512 }
2513 }
2514
2515 /* If anything failed, then we're not doing target-side commands,
2516 and so clean up. */
2517 if (null_command_or_parse_error)
2518 {
2519 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2520 {
2521 loc = (*loc2p);
2522 if (is_breakpoint (loc->owner)
2523 && loc->pspace->num == bl->pspace->num)
2524 {
2525 /* Only go as far as the first NULL bytecode is
2526 located. */
40fb6c5e 2527 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2528 return;
2529
40fb6c5e
HZ
2530 free_agent_expr (loc->cmd_bytecode);
2531 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2532 }
2533 }
2534 }
2535
2536 /* No NULL commands or failed bytecode generation. Build a command list
2537 for this location's address. */
2538 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2539 {
2540 loc = (*loc2p);
2541 if (loc->owner->extra_string
2542 && is_breakpoint (loc->owner)
2543 && loc->pspace->num == bl->pspace->num
2544 && loc->owner->enable_state == bp_enabled
2545 && loc->enabled)
2546 /* Add the command to the vector. This will be used later
2547 to send the commands to the target. */
2548 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2549 loc->cmd_bytecode);
2550 }
2551
2552 bl->target_info.persist = 0;
2553 /* Maybe flag this location as persistent. */
2554 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2555 bl->target_info.persist = 1;
2556}
2557
35df4500
TJB
2558/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2559 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2560 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2561 Returns 0 for success, 1 if the bp_location type is not supported or
2562 -1 for failure.
879bfdc2 2563
4a64f543
MS
2564 NOTE drow/2003-09-09: This routine could be broken down to an
2565 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2566static int
35df4500 2567insert_bp_location (struct bp_location *bl,
26bb91f3 2568 struct ui_file *tmp_error_stream,
3fbb6ffa 2569 int *disabled_breaks,
dd61ec5c
MW
2570 int *hw_breakpoint_error,
2571 int *hw_bp_error_explained_already)
879bfdc2 2572{
0000e5cc
PA
2573 enum errors bp_err = GDB_NO_ERROR;
2574 const char *bp_err_message = NULL;
c90a6fb7 2575 volatile struct gdb_exception e;
879bfdc2 2576
b775012e 2577 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2578 return 0;
2579
35c63cd8
JB
2580 /* Note we don't initialize bl->target_info, as that wipes out
2581 the breakpoint location's shadow_contents if the breakpoint
2582 is still inserted at that location. This in turn breaks
2583 target_read_memory which depends on these buffers when
2584 a memory read is requested at the breakpoint location:
2585 Once the target_info has been wiped, we fail to see that
2586 we have a breakpoint inserted at that address and thus
2587 read the breakpoint instead of returning the data saved in
2588 the breakpoint location's shadow contents. */
0d5ed153 2589 bl->target_info.reqstd_address = bl->address;
35df4500 2590 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2591 bl->target_info.length = bl->length;
8181d85f 2592
b775012e
LM
2593 /* When working with target-side conditions, we must pass all the conditions
2594 for the same breakpoint address down to the target since GDB will not
2595 insert those locations. With a list of breakpoint conditions, the target
2596 can decide when to stop and notify GDB. */
2597
2598 if (is_breakpoint (bl->owner))
2599 {
2600 build_target_condition_list (bl);
d3ce09f5
SS
2601 build_target_command_list (bl);
2602 /* Reset the modification marker. */
b775012e
LM
2603 bl->needs_update = 0;
2604 }
2605
35df4500
TJB
2606 if (bl->loc_type == bp_loc_software_breakpoint
2607 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2608 {
35df4500 2609 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2610 {
2611 /* If the explicitly specified breakpoint type
2612 is not hardware breakpoint, check the memory map to see
2613 if the breakpoint address is in read only memory or not.
4a64f543 2614
765dc015
VP
2615 Two important cases are:
2616 - location type is not hardware breakpoint, memory
2617 is readonly. We change the type of the location to
2618 hardware breakpoint.
4a64f543
MS
2619 - location type is hardware breakpoint, memory is
2620 read-write. This means we've previously made the
2621 location hardware one, but then the memory map changed,
2622 so we undo.
765dc015 2623
4a64f543
MS
2624 When breakpoints are removed, remove_breakpoints will use
2625 location types we've just set here, the only possible
2626 problem is that memory map has changed during running
2627 program, but it's not going to work anyway with current
2628 gdb. */
765dc015 2629 struct mem_region *mr
0d5ed153 2630 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2631
2632 if (mr)
2633 {
2634 if (automatic_hardware_breakpoints)
2635 {
765dc015
VP
2636 enum bp_loc_type new_type;
2637
2638 if (mr->attrib.mode != MEM_RW)
2639 new_type = bp_loc_hardware_breakpoint;
2640 else
2641 new_type = bp_loc_software_breakpoint;
2642
35df4500 2643 if (new_type != bl->loc_type)
765dc015
VP
2644 {
2645 static int said = 0;
cc59ec59 2646
35df4500 2647 bl->loc_type = new_type;
765dc015
VP
2648 if (!said)
2649 {
3e43a32a
MS
2650 fprintf_filtered (gdb_stdout,
2651 _("Note: automatically using "
2652 "hardware breakpoints for "
2653 "read-only addresses.\n"));
765dc015
VP
2654 said = 1;
2655 }
2656 }
2657 }
35df4500 2658 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2659 && mr->attrib.mode != MEM_RW)
2660 {
2661 fprintf_unfiltered (tmp_error_stream,
2662 _("Cannot insert breakpoint %d.\n"
2663 "Cannot set software breakpoint "
2664 "at read-only address %s\n"),
2665 bl->owner->number,
2666 paddress (bl->gdbarch, bl->address));
2667 return 1;
2668 }
765dc015
VP
2669 }
2670 }
2671
879bfdc2
DJ
2672 /* First check to see if we have to handle an overlay. */
2673 if (overlay_debugging == ovly_off
35df4500
TJB
2674 || bl->section == NULL
2675 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2676 {
2677 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2678 TRY_CATCH (e, RETURN_MASK_ALL)
2679 {
0000e5cc
PA
2680 int val;
2681
dd61ec5c 2682 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2683 if (val)
2684 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2685 }
2686 if (e.reason < 0)
2687 {
0000e5cc
PA
2688 bp_err = e.error;
2689 bp_err_message = e.message;
dd61ec5c 2690 }
879bfdc2
DJ
2691 }
2692 else
2693 {
4a64f543 2694 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2695 Shall we set a breakpoint at the LMA? */
2696 if (!overlay_events_enabled)
2697 {
2698 /* Yes -- overlay event support is not active,
2699 so we must try to set a breakpoint at the LMA.
2700 This will not work for a hardware breakpoint. */
35df4500 2701 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2702 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2703 bl->owner->number);
879bfdc2
DJ
2704 else
2705 {
35df4500
TJB
2706 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2707 bl->section);
879bfdc2 2708 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2709 bl->overlay_target_info = bl->target_info;
0d5ed153 2710 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2711
2712 /* No overlay handling: just set the breakpoint. */
2713 TRY_CATCH (e, RETURN_MASK_ALL)
2714 {
2715 int val;
2716
2717 val = target_insert_breakpoint (bl->gdbarch,
2718 &bl->overlay_target_info);
2719 if (val)
2720 bp_err = GENERIC_ERROR;
2721 }
2722 if (e.reason < 0)
2723 {
2724 bp_err = e.error;
2725 bp_err_message = e.message;
2726 }
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. */
dd61ec5c
MW
2739 TRY_CATCH (e, RETURN_MASK_ALL)
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
MW
2746 }
2747 if (e.reason < 0)
2748 {
0000e5cc
PA
2749 bp_err = e.error;
2750 bp_err_message = e.message;
dd61ec5c 2751 }
879bfdc2
DJ
2752 }
2753 else
2754 {
2755 /* No. This breakpoint will not be inserted.
2756 No error, but do not mark the bp as 'inserted'. */
2757 return 0;
2758 }
2759 }
2760
0000e5cc 2761 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2762 {
2763 /* Can't set the breakpoint. */
0000e5cc
PA
2764
2765 /* In some cases, we might not be able to insert a
2766 breakpoint in a shared library that has already been
2767 removed, but we have not yet processed the shlib unload
2768 event. Unfortunately, some targets that implement
076855f9
PA
2769 breakpoint insertion themselves can't tell why the
2770 breakpoint insertion failed (e.g., the remote target
2771 doesn't define error codes), so we must treat generic
2772 errors as memory errors. */
0000e5cc 2773 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2774 && bl->loc_type == bp_loc_software_breakpoint
08351840 2775 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2776 || shared_objfile_contains_address_p (bl->pspace,
2777 bl->address)))
879bfdc2 2778 {
4a64f543 2779 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2780 bl->shlib_disabled = 1;
8d3788bd 2781 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2782 if (!*disabled_breaks)
2783 {
2784 fprintf_unfiltered (tmp_error_stream,
2785 "Cannot insert breakpoint %d.\n",
2786 bl->owner->number);
2787 fprintf_unfiltered (tmp_error_stream,
2788 "Temporarily disabling shared "
2789 "library breakpoints:\n");
2790 }
2791 *disabled_breaks = 1;
879bfdc2 2792 fprintf_unfiltered (tmp_error_stream,
35df4500 2793 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2794 return 0;
879bfdc2
DJ
2795 }
2796 else
879bfdc2 2797 {
35df4500 2798 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2799 {
0000e5cc
PA
2800 *hw_breakpoint_error = 1;
2801 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2802 fprintf_unfiltered (tmp_error_stream,
2803 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2804 bl->owner->number, bp_err_message ? ":" : ".\n");
2805 if (bp_err_message != NULL)
2806 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2807 }
2808 else
2809 {
0000e5cc
PA
2810 if (bp_err_message == NULL)
2811 {
2812 char *message
2813 = memory_error_message (TARGET_XFER_E_IO,
2814 bl->gdbarch, bl->address);
2815 struct cleanup *old_chain = make_cleanup (xfree, message);
2816
2817 fprintf_unfiltered (tmp_error_stream,
2818 "Cannot insert breakpoint %d.\n"
2819 "%s\n",
2820 bl->owner->number, message);
2821 do_cleanups (old_chain);
2822 }
2823 else
2824 {
2825 fprintf_unfiltered (tmp_error_stream,
2826 "Cannot insert breakpoint %d: %s\n",
2827 bl->owner->number,
2828 bp_err_message);
2829 }
879bfdc2 2830 }
0000e5cc 2831 return 1;
879bfdc2
DJ
2832
2833 }
2834 }
2835 else
35df4500 2836 bl->inserted = 1;
879bfdc2 2837
0000e5cc 2838 return 0;
879bfdc2
DJ
2839 }
2840
35df4500 2841 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2842 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2843 watchpoints. It's not clear that it's necessary... */
35df4500 2844 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2845 {
0000e5cc
PA
2846 int val;
2847
77b06cd7
TJB
2848 gdb_assert (bl->owner->ops != NULL
2849 && bl->owner->ops->insert_location != NULL);
2850
2851 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2852
2853 /* If trying to set a read-watchpoint, and it turns out it's not
2854 supported, try emulating one with an access watchpoint. */
35df4500 2855 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2856 {
2857 struct bp_location *loc, **loc_temp;
2858
2859 /* But don't try to insert it, if there's already another
2860 hw_access location that would be considered a duplicate
2861 of this one. */
2862 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2863 if (loc != bl
85d721b8 2864 && loc->watchpoint_type == hw_access
35df4500 2865 && watchpoint_locations_match (bl, loc))
85d721b8 2866 {
35df4500
TJB
2867 bl->duplicate = 1;
2868 bl->inserted = 1;
2869 bl->target_info = loc->target_info;
2870 bl->watchpoint_type = hw_access;
85d721b8
PA
2871 val = 0;
2872 break;
2873 }
2874
2875 if (val == 1)
2876 {
77b06cd7
TJB
2877 bl->watchpoint_type = hw_access;
2878 val = bl->owner->ops->insert_location (bl);
2879
2880 if (val)
2881 /* Back to the original value. */
2882 bl->watchpoint_type = hw_read;
85d721b8
PA
2883 }
2884 }
2885
35df4500 2886 bl->inserted = (val == 0);
879bfdc2
DJ
2887 }
2888
35df4500 2889 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2890 {
0000e5cc
PA
2891 int val;
2892
77b06cd7
TJB
2893 gdb_assert (bl->owner->ops != NULL
2894 && bl->owner->ops->insert_location != NULL);
2895
2896 val = bl->owner->ops->insert_location (bl);
2897 if (val)
2898 {
2899 bl->owner->enable_state = bp_disabled;
2900
2901 if (val == 1)
2902 warning (_("\
2903Error inserting catchpoint %d: Your system does not support this type\n\
2904of catchpoint."), bl->owner->number);
2905 else
2906 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2907 }
2908
2909 bl->inserted = (val == 0);
1640b821
DJ
2910
2911 /* We've already printed an error message if there was a problem
2912 inserting this catchpoint, and we've disabled the catchpoint,
2913 so just return success. */
2914 return 0;
879bfdc2
DJ
2915 }
2916
2917 return 0;
2918}
2919
6c95b8df
PA
2920/* This function is called when program space PSPACE is about to be
2921 deleted. It takes care of updating breakpoints to not reference
2922 PSPACE anymore. */
2923
2924void
2925breakpoint_program_space_exit (struct program_space *pspace)
2926{
2927 struct breakpoint *b, *b_temp;
876fa593 2928 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2929
2930 /* Remove any breakpoint that was set through this program space. */
2931 ALL_BREAKPOINTS_SAFE (b, b_temp)
2932 {
2933 if (b->pspace == pspace)
2934 delete_breakpoint (b);
2935 }
2936
2937 /* Breakpoints set through other program spaces could have locations
2938 bound to PSPACE as well. Remove those. */
876fa593 2939 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2940 {
2941 struct bp_location *tmp;
2942
2943 if (loc->pspace == pspace)
2944 {
2bdf28a0 2945 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2946 if (loc->owner->loc == loc)
2947 loc->owner->loc = loc->next;
2948 else
2949 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2950 if (tmp->next == loc)
2951 {
2952 tmp->next = loc->next;
2953 break;
2954 }
2955 }
2956 }
2957
2958 /* Now update the global location list to permanently delete the
2959 removed locations above. */
44702360 2960 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2961}
2962
74960c60
VP
2963/* Make sure all breakpoints are inserted in inferior.
2964 Throws exception on any error.
2965 A breakpoint that is already inserted won't be inserted
2966 again, so calling this function twice is safe. */
2967void
2968insert_breakpoints (void)
2969{
2970 struct breakpoint *bpt;
2971
2972 ALL_BREAKPOINTS (bpt)
2973 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2974 {
2975 struct watchpoint *w = (struct watchpoint *) bpt;
2976
2977 update_watchpoint (w, 0 /* don't reparse. */);
2978 }
74960c60 2979
04086b45
PA
2980 /* Updating watchpoints creates new locations, so update the global
2981 location list. Explicitly tell ugll to insert locations and
2982 ignore breakpoints_always_inserted_mode. */
2983 update_global_location_list (UGLL_INSERT);
74960c60
VP
2984}
2985
20388dd6
YQ
2986/* Invoke CALLBACK for each of bp_location. */
2987
2988void
2989iterate_over_bp_locations (walk_bp_location_callback callback)
2990{
2991 struct bp_location *loc, **loc_tmp;
2992
2993 ALL_BP_LOCATIONS (loc, loc_tmp)
2994 {
2995 callback (loc, NULL);
2996 }
2997}
2998
b775012e
LM
2999/* This is used when we need to synch breakpoint conditions between GDB and the
3000 target. It is the case with deleting and disabling of breakpoints when using
3001 always-inserted mode. */
3002
3003static void
3004update_inserted_breakpoint_locations (void)
3005{
3006 struct bp_location *bl, **blp_tmp;
3007 int error_flag = 0;
3008 int val = 0;
3009 int disabled_breaks = 0;
3010 int hw_breakpoint_error = 0;
dd61ec5c 3011 int hw_bp_details_reported = 0;
b775012e
LM
3012
3013 struct ui_file *tmp_error_stream = mem_fileopen ();
3014 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3015
3016 /* Explicitly mark the warning -- this will only be printed if
3017 there was an error. */
3018 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3019
3020 save_current_space_and_thread ();
3021
3022 ALL_BP_LOCATIONS (bl, blp_tmp)
3023 {
3024 /* We only want to update software breakpoints and hardware
3025 breakpoints. */
3026 if (!is_breakpoint (bl->owner))
3027 continue;
3028
3029 /* We only want to update locations that are already inserted
3030 and need updating. This is to avoid unwanted insertion during
3031 deletion of breakpoints. */
3032 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3033 continue;
3034
3035 switch_to_program_space_and_thread (bl->pspace);
3036
3037 /* For targets that support global breakpoints, there's no need
3038 to select an inferior to insert breakpoint to. In fact, even
3039 if we aren't attached to any process yet, we should still
3040 insert breakpoints. */
f5656ead 3041 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3042 && ptid_equal (inferior_ptid, null_ptid))
3043 continue;
3044
3045 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3046 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3047 if (val)
3048 error_flag = val;
3049 }
3050
3051 if (error_flag)
3052 {
3053 target_terminal_ours_for_output ();
3054 error_stream (tmp_error_stream);
3055 }
3056
3057 do_cleanups (cleanups);
3058}
3059
c30eee59 3060/* Used when starting or continuing the program. */
c906108c 3061
74960c60
VP
3062static void
3063insert_breakpoint_locations (void)
c906108c 3064{
a5606eee 3065 struct breakpoint *bpt;
35df4500 3066 struct bp_location *bl, **blp_tmp;
eacd795a 3067 int error_flag = 0;
c906108c 3068 int val = 0;
3fbb6ffa 3069 int disabled_breaks = 0;
81d0cc19 3070 int hw_breakpoint_error = 0;
dd61ec5c 3071 int hw_bp_error_explained_already = 0;
c906108c 3072
81d0cc19 3073 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3074 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3075
81d0cc19
GS
3076 /* Explicitly mark the warning -- this will only be printed if
3077 there was an error. */
3078 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3079
3080 save_current_space_and_thread ();
3081
35df4500 3082 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3083 {
b775012e 3084 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3085 continue;
3086
4a64f543
MS
3087 /* There is no point inserting thread-specific breakpoints if
3088 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3089 has BL->OWNER always non-NULL. */
35df4500
TJB
3090 if (bl->owner->thread != -1
3091 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3092 continue;
3093
35df4500 3094 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3095
3096 /* For targets that support global breakpoints, there's no need
3097 to select an inferior to insert breakpoint to. In fact, even
3098 if we aren't attached to any process yet, we should still
3099 insert breakpoints. */
f5656ead 3100 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3101 && ptid_equal (inferior_ptid, null_ptid))
3102 continue;
3103
3fbb6ffa 3104 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3105 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3106 if (val)
eacd795a 3107 error_flag = val;
879bfdc2 3108 }
c906108c 3109
4a64f543
MS
3110 /* If we failed to insert all locations of a watchpoint, remove
3111 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3112 ALL_BREAKPOINTS (bpt)
3113 {
3114 int some_failed = 0;
3115 struct bp_location *loc;
3116
3117 if (!is_hardware_watchpoint (bpt))
3118 continue;
3119
d6b74ac4 3120 if (!breakpoint_enabled (bpt))
a5606eee 3121 continue;
74960c60
VP
3122
3123 if (bpt->disposition == disp_del_at_next_stop)
3124 continue;
a5606eee
VP
3125
3126 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3127 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3128 {
3129 some_failed = 1;
3130 break;
3131 }
3132 if (some_failed)
3133 {
3134 for (loc = bpt->loc; loc; loc = loc->next)
3135 if (loc->inserted)
3136 remove_breakpoint (loc, mark_uninserted);
3137
3138 hw_breakpoint_error = 1;
3139 fprintf_unfiltered (tmp_error_stream,
3140 "Could not insert hardware watchpoint %d.\n",
3141 bpt->number);
eacd795a 3142 error_flag = -1;
a5606eee
VP
3143 }
3144 }
3145
eacd795a 3146 if (error_flag)
81d0cc19
GS
3147 {
3148 /* If a hardware breakpoint or watchpoint was inserted, add a
3149 message about possibly exhausted resources. */
dd61ec5c 3150 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3151 {
c6510018
MS
3152 fprintf_unfiltered (tmp_error_stream,
3153 "Could not insert hardware breakpoints:\n\
3154You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3155 }
81d0cc19
GS
3156 target_terminal_ours_for_output ();
3157 error_stream (tmp_error_stream);
3158 }
f7545552
TT
3159
3160 do_cleanups (cleanups);
c906108c
SS
3161}
3162
c30eee59
TJB
3163/* Used when the program stops.
3164 Returns zero if successful, or non-zero if there was a problem
3165 removing a breakpoint location. */
3166
c906108c 3167int
fba45db2 3168remove_breakpoints (void)
c906108c 3169{
35df4500 3170 struct bp_location *bl, **blp_tmp;
3a1bae8e 3171 int val = 0;
c906108c 3172
35df4500 3173 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3174 {
1e4d1764 3175 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3176 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3177 }
3a1bae8e 3178 return val;
c906108c
SS
3179}
3180
49fa26b0
PA
3181/* When a thread exits, remove breakpoints that are related to
3182 that thread. */
3183
3184static void
3185remove_threaded_breakpoints (struct thread_info *tp, int silent)
3186{
3187 struct breakpoint *b, *b_tmp;
3188
3189 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3190 {
96181529 3191 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3192 {
3193 b->disposition = disp_del_at_next_stop;
3194
3195 printf_filtered (_("\
46ecd527 3196Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3197 b->number, tp->num);
3198
3199 /* Hide it from the user. */
3200 b->number = 0;
3201 }
3202 }
3203}
3204
6c95b8df
PA
3205/* Remove breakpoints of process PID. */
3206
3207int
3208remove_breakpoints_pid (int pid)
3209{
35df4500 3210 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3211 int val;
3212 struct inferior *inf = find_inferior_pid (pid);
3213
35df4500 3214 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3215 {
35df4500 3216 if (bl->pspace != inf->pspace)
6c95b8df
PA
3217 continue;
3218
fc126975 3219 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3220 {
35df4500 3221 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3222 if (val != 0)
3223 return val;
3224 }
3225 }
3226 return 0;
3227}
3228
c906108c 3229int
fba45db2 3230reattach_breakpoints (int pid)
c906108c 3231{
6c95b8df 3232 struct cleanup *old_chain;
35df4500 3233 struct bp_location *bl, **blp_tmp;
c906108c 3234 int val;
86b887df 3235 struct ui_file *tmp_error_stream;
dd61ec5c 3236 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3237 struct inferior *inf;
3238 struct thread_info *tp;
3239
3240 tp = any_live_thread_of_process (pid);
3241 if (tp == NULL)
3242 return 1;
3243
3244 inf = find_inferior_pid (pid);
3245 old_chain = save_inferior_ptid ();
3246
3247 inferior_ptid = tp->ptid;
a4954f26 3248
86b887df 3249 tmp_error_stream = mem_fileopen ();
a4954f26 3250 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3251
35df4500 3252 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3253 {
35df4500 3254 if (bl->pspace != inf->pspace)
6c95b8df
PA
3255 continue;
3256
35df4500 3257 if (bl->inserted)
c5aa993b 3258 {
35df4500 3259 bl->inserted = 0;
dd61ec5c 3260 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3261 if (val != 0)
3262 {
ce696e05 3263 do_cleanups (old_chain);
c5aa993b
JM
3264 return val;
3265 }
3266 }
3267 }
ce696e05 3268 do_cleanups (old_chain);
c906108c
SS
3269 return 0;
3270}
3271
e58b0e63
PA
3272static int internal_breakpoint_number = -1;
3273
84f4c1fe
PM
3274/* Set the breakpoint number of B, depending on the value of INTERNAL.
3275 If INTERNAL is non-zero, the breakpoint number will be populated
3276 from internal_breakpoint_number and that variable decremented.
e5dd4106 3277 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3278 breakpoint_count and that value incremented. Internal breakpoints
3279 do not set the internal var bpnum. */
3280static void
3281set_breakpoint_number (int internal, struct breakpoint *b)
3282{
3283 if (internal)
3284 b->number = internal_breakpoint_number--;
3285 else
3286 {
3287 set_breakpoint_count (breakpoint_count + 1);
3288 b->number = breakpoint_count;
3289 }
3290}
3291
e62c965a 3292static struct breakpoint *
a6d9a66e 3293create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3294 CORE_ADDR address, enum bptype type,
c0a91b2b 3295 const struct breakpoint_ops *ops)
e62c965a 3296{
e62c965a
PP
3297 struct symtab_and_line sal;
3298 struct breakpoint *b;
3299
4a64f543 3300 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3301
3302 sal.pc = address;
3303 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3304 sal.pspace = current_program_space;
e62c965a 3305
06edf0c0 3306 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3307 b->number = internal_breakpoint_number--;
3308 b->disposition = disp_donttouch;
3309
3310 return b;
3311}
3312
17450429
PP
3313static const char *const longjmp_names[] =
3314 {
3315 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3316 };
3317#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3318
3319/* Per-objfile data private to breakpoint.c. */
3320struct breakpoint_objfile_data
3321{
3322 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3323 struct bound_minimal_symbol overlay_msym;
17450429
PP
3324
3325 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3326 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3327
28106bc2
SDJ
3328 /* True if we have looked for longjmp probes. */
3329 int longjmp_searched;
3330
3331 /* SystemTap probe points for longjmp (if any). */
3332 VEC (probe_p) *longjmp_probes;
3333
17450429 3334 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3335 struct bound_minimal_symbol terminate_msym;
17450429
PP
3336
3337 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3338 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3339
3340 /* True if we have looked for exception probes. */
3341 int exception_searched;
3342
3343 /* SystemTap probe points for unwinding (if any). */
3344 VEC (probe_p) *exception_probes;
17450429
PP
3345};
3346
3347static const struct objfile_data *breakpoint_objfile_key;
3348
3349/* Minimal symbol not found sentinel. */
3350static struct minimal_symbol msym_not_found;
3351
3352/* Returns TRUE if MSYM point to the "not found" sentinel. */
3353
3354static int
3355msym_not_found_p (const struct minimal_symbol *msym)
3356{
3357 return msym == &msym_not_found;
3358}
3359
3360/* Return per-objfile data needed by breakpoint.c.
3361 Allocate the data if necessary. */
3362
3363static struct breakpoint_objfile_data *
3364get_breakpoint_objfile_data (struct objfile *objfile)
3365{
3366 struct breakpoint_objfile_data *bp_objfile_data;
3367
3368 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3369 if (bp_objfile_data == NULL)
3370 {
3371 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3372 sizeof (*bp_objfile_data));
3373
3374 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3375 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3376 }
3377 return bp_objfile_data;
3378}
3379
28106bc2
SDJ
3380static void
3381free_breakpoint_probes (struct objfile *obj, void *data)
3382{
3383 struct breakpoint_objfile_data *bp_objfile_data = data;
3384
3385 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3386 VEC_free (probe_p, bp_objfile_data->exception_probes);
3387}
3388
e62c965a 3389static void
af02033e 3390create_overlay_event_breakpoint (void)
e62c965a 3391{
69de3c6a 3392 struct objfile *objfile;
af02033e 3393 const char *const func_name = "_ovly_debug_event";
e62c965a 3394
69de3c6a
PP
3395 ALL_OBJFILES (objfile)
3396 {
3397 struct breakpoint *b;
17450429
PP
3398 struct breakpoint_objfile_data *bp_objfile_data;
3399 CORE_ADDR addr;
69de3c6a 3400
17450429
PP
3401 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3402
3b7344d5 3403 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3404 continue;
3405
3b7344d5 3406 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3407 {
3b7344d5 3408 struct bound_minimal_symbol m;
17450429
PP
3409
3410 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3411 if (m.minsym == NULL)
17450429
PP
3412 {
3413 /* Avoid future lookups in this objfile. */
3b7344d5 3414 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3415 continue;
3416 }
3417 bp_objfile_data->overlay_msym = m;
3418 }
e62c965a 3419
77e371c0 3420 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3421 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3422 bp_overlay_event,
3423 &internal_breakpoint_ops);
69de3c6a 3424 b->addr_string = xstrdup (func_name);
e62c965a 3425
69de3c6a
PP
3426 if (overlay_debugging == ovly_auto)
3427 {
3428 b->enable_state = bp_enabled;
3429 overlay_events_enabled = 1;
3430 }
3431 else
3432 {
3433 b->enable_state = bp_disabled;
3434 overlay_events_enabled = 0;
3435 }
e62c965a 3436 }
44702360 3437 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3438}
3439
0fd8e87f 3440static void
af02033e 3441create_longjmp_master_breakpoint (void)
0fd8e87f 3442{
6c95b8df 3443 struct program_space *pspace;
6c95b8df
PA
3444 struct cleanup *old_chain;
3445
3446 old_chain = save_current_program_space ();
0fd8e87f 3447
6c95b8df 3448 ALL_PSPACES (pspace)
af02033e
PP
3449 {
3450 struct objfile *objfile;
3451
3452 set_current_program_space (pspace);
3453
3454 ALL_OBJFILES (objfile)
0fd8e87f 3455 {
af02033e
PP
3456 int i;
3457 struct gdbarch *gdbarch;
17450429 3458 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3459
af02033e 3460 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3461
17450429
PP
3462 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3463
28106bc2
SDJ
3464 if (!bp_objfile_data->longjmp_searched)
3465 {
25f9533e
SDJ
3466 VEC (probe_p) *ret;
3467
3468 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3469 if (ret != NULL)
3470 {
3471 /* We are only interested in checking one element. */
3472 struct probe *p = VEC_index (probe_p, ret, 0);
3473
3474 if (!can_evaluate_probe_arguments (p))
3475 {
3476 /* We cannot use the probe interface here, because it does
3477 not know how to evaluate arguments. */
3478 VEC_free (probe_p, ret);
3479 ret = NULL;
3480 }
3481 }
3482 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3483 bp_objfile_data->longjmp_searched = 1;
3484 }
3485
3486 if (bp_objfile_data->longjmp_probes != NULL)
3487 {
3488 int i;
3489 struct probe *probe;
3490 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3491
3492 for (i = 0;
3493 VEC_iterate (probe_p,
3494 bp_objfile_data->longjmp_probes,
3495 i, probe);
3496 ++i)
3497 {
3498 struct breakpoint *b;
3499
729662a5
TT
3500 b = create_internal_breakpoint (gdbarch,
3501 get_probe_address (probe,
3502 objfile),
28106bc2
SDJ
3503 bp_longjmp_master,
3504 &internal_breakpoint_ops);
3505 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3506 b->enable_state = bp_disabled;
3507 }
3508
3509 continue;
3510 }
3511
0569175e
TSD
3512 if (!gdbarch_get_longjmp_target_p (gdbarch))
3513 continue;
3514
17450429 3515 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3516 {
3517 struct breakpoint *b;
af02033e 3518 const char *func_name;
17450429 3519 CORE_ADDR addr;
6c95b8df 3520
3b7344d5 3521 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3522 continue;
0fd8e87f 3523
17450429 3524 func_name = longjmp_names[i];
3b7344d5 3525 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3526 {
3b7344d5 3527 struct bound_minimal_symbol m;
17450429
PP
3528
3529 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3530 if (m.minsym == NULL)
17450429
PP
3531 {
3532 /* Prevent future lookups in this objfile. */
3b7344d5 3533 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3534 continue;
3535 }
3536 bp_objfile_data->longjmp_msym[i] = m;
3537 }
3538
77e371c0 3539 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3540 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3541 &internal_breakpoint_ops);
af02033e
PP
3542 b->addr_string = xstrdup (func_name);
3543 b->enable_state = bp_disabled;
3544 }
0fd8e87f 3545 }
af02033e 3546 }
44702360 3547 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3548
3549 do_cleanups (old_chain);
0fd8e87f
UW
3550}
3551
af02033e 3552/* Create a master std::terminate breakpoint. */
aa7d318d 3553static void
af02033e 3554create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3555{
3556 struct program_space *pspace;
aa7d318d 3557 struct cleanup *old_chain;
af02033e 3558 const char *const func_name = "std::terminate()";
aa7d318d
TT
3559
3560 old_chain = save_current_program_space ();
3561
3562 ALL_PSPACES (pspace)
17450429
PP
3563 {
3564 struct objfile *objfile;
3565 CORE_ADDR addr;
3566
3567 set_current_program_space (pspace);
3568
aa7d318d
TT
3569 ALL_OBJFILES (objfile)
3570 {
3571 struct breakpoint *b;
17450429 3572 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3573
17450429 3574 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3575
3b7344d5 3576 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3577 continue;
3578
3b7344d5 3579 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3580 {
3b7344d5 3581 struct bound_minimal_symbol m;
17450429
PP
3582
3583 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3584 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3585 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3586 {
3587 /* Prevent future lookups in this objfile. */
3b7344d5 3588 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3589 continue;
3590 }
3591 bp_objfile_data->terminate_msym = m;
3592 }
aa7d318d 3593
77e371c0 3594 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3595 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3596 bp_std_terminate_master,
3597 &internal_breakpoint_ops);
aa7d318d
TT
3598 b->addr_string = xstrdup (func_name);
3599 b->enable_state = bp_disabled;
3600 }
17450429
PP
3601 }
3602
44702360 3603 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3604
3605 do_cleanups (old_chain);
3606}
3607
186c406b
TT
3608/* Install a master breakpoint on the unwinder's debug hook. */
3609
70221824 3610static void
186c406b
TT
3611create_exception_master_breakpoint (void)
3612{
3613 struct objfile *objfile;
17450429 3614 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3615
3616 ALL_OBJFILES (objfile)
3617 {
17450429
PP
3618 struct breakpoint *b;
3619 struct gdbarch *gdbarch;
3620 struct breakpoint_objfile_data *bp_objfile_data;
3621 CORE_ADDR addr;
3622
3623 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3624
28106bc2
SDJ
3625 /* We prefer the SystemTap probe point if it exists. */
3626 if (!bp_objfile_data->exception_searched)
3627 {
25f9533e
SDJ
3628 VEC (probe_p) *ret;
3629
3630 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3631
3632 if (ret != NULL)
3633 {
3634 /* We are only interested in checking one element. */
3635 struct probe *p = VEC_index (probe_p, ret, 0);
3636
3637 if (!can_evaluate_probe_arguments (p))
3638 {
3639 /* We cannot use the probe interface here, because it does
3640 not know how to evaluate arguments. */
3641 VEC_free (probe_p, ret);
3642 ret = NULL;
3643 }
3644 }
3645 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3646 bp_objfile_data->exception_searched = 1;
3647 }
3648
3649 if (bp_objfile_data->exception_probes != NULL)
3650 {
3651 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3652 int i;
3653 struct probe *probe;
3654
3655 for (i = 0;
3656 VEC_iterate (probe_p,
3657 bp_objfile_data->exception_probes,
3658 i, probe);
3659 ++i)
3660 {
3661 struct breakpoint *b;
3662
729662a5
TT
3663 b = create_internal_breakpoint (gdbarch,
3664 get_probe_address (probe,
3665 objfile),
28106bc2
SDJ
3666 bp_exception_master,
3667 &internal_breakpoint_ops);
3668 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3669 b->enable_state = bp_disabled;
3670 }
3671
3672 continue;
3673 }
3674
3675 /* Otherwise, try the hook function. */
3676
3b7344d5 3677 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3678 continue;
3679
3680 gdbarch = get_objfile_arch (objfile);
186c406b 3681
3b7344d5 3682 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3683 {
3b7344d5 3684 struct bound_minimal_symbol debug_hook;
186c406b 3685
17450429 3686 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3687 if (debug_hook.minsym == NULL)
17450429 3688 {
3b7344d5 3689 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3690 continue;
3691 }
3692
3693 bp_objfile_data->exception_msym = debug_hook;
186c406b 3694 }
17450429 3695
77e371c0 3696 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3697 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3698 &current_target);
06edf0c0
PA
3699 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3700 &internal_breakpoint_ops);
17450429
PP
3701 b->addr_string = xstrdup (func_name);
3702 b->enable_state = bp_disabled;
186c406b
TT
3703 }
3704
44702360 3705 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3706}
3707
c906108c 3708void
fba45db2 3709update_breakpoints_after_exec (void)
c906108c 3710{
35df4500 3711 struct breakpoint *b, *b_tmp;
876fa593 3712 struct bp_location *bploc, **bplocp_tmp;
c906108c 3713
25b22b0a
PA
3714 /* We're about to delete breakpoints from GDB's lists. If the
3715 INSERTED flag is true, GDB will try to lift the breakpoints by
3716 writing the breakpoints' "shadow contents" back into memory. The
3717 "shadow contents" are NOT valid after an exec, so GDB should not
3718 do that. Instead, the target is responsible from marking
3719 breakpoints out as soon as it detects an exec. We don't do that
3720 here instead, because there may be other attempts to delete
3721 breakpoints after detecting an exec and before reaching here. */
876fa593 3722 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3723 if (bploc->pspace == current_program_space)
3724 gdb_assert (!bploc->inserted);
c906108c 3725
35df4500 3726 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3727 {
6c95b8df
PA
3728 if (b->pspace != current_program_space)
3729 continue;
3730
4a64f543 3731 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3732 if (b->type == bp_shlib_event)
3733 {
3734 delete_breakpoint (b);
3735 continue;
3736 }
c906108c 3737
4a64f543 3738 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3739 if (b->type == bp_jit_event)
3740 {
3741 delete_breakpoint (b);
3742 continue;
3743 }
3744
1900040c 3745 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3746 as must overlay event and longjmp master breakpoints. */
3747 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3748 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3749 || b->type == bp_exception_master)
c4093a6a
JM
3750 {
3751 delete_breakpoint (b);
3752 continue;
3753 }
3754
4a64f543 3755 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3756 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3757 {
3758 delete_breakpoint (b);
3759 continue;
3760 }
3761
7c16b83e
PA
3762 /* Just like single-step breakpoints. */
3763 if (b->type == bp_single_step)
3764 {
3765 delete_breakpoint (b);
3766 continue;
3767 }
3768
611c83ae
PA
3769 /* Longjmp and longjmp-resume breakpoints are also meaningless
3770 after an exec. */
186c406b 3771 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3772 || b->type == bp_longjmp_call_dummy
186c406b 3773 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3774 {
3775 delete_breakpoint (b);
3776 continue;
3777 }
3778
ce78b96d
JB
3779 if (b->type == bp_catchpoint)
3780 {
3781 /* For now, none of the bp_catchpoint breakpoints need to
3782 do anything at this point. In the future, if some of
3783 the catchpoints need to something, we will need to add
3784 a new method, and call this method from here. */
3785 continue;
3786 }
3787
c5aa993b
JM
3788 /* bp_finish is a special case. The only way we ought to be able
3789 to see one of these when an exec() has happened, is if the user
3790 caught a vfork, and then said "finish". Ordinarily a finish just
3791 carries them to the call-site of the current callee, by setting
3792 a temporary bp there and resuming. But in this case, the finish
3793 will carry them entirely through the vfork & exec.
3794
3795 We don't want to allow a bp_finish to remain inserted now. But
3796 we can't safely delete it, 'cause finish_command has a handle to
3797 the bp on a bpstat, and will later want to delete it. There's a
3798 chance (and I've seen it happen) that if we delete the bp_finish
3799 here, that its storage will get reused by the time finish_command
3800 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3801 We really must allow finish_command to delete a bp_finish.
3802
e5dd4106 3803 In the absence of a general solution for the "how do we know
53a5351d
JM
3804 it's safe to delete something others may have handles to?"
3805 problem, what we'll do here is just uninsert the bp_finish, and
3806 let finish_command delete it.
3807
3808 (We know the bp_finish is "doomed" in the sense that it's
3809 momentary, and will be deleted as soon as finish_command sees
3810 the inferior stopped. So it doesn't matter that the bp's
3811 address is probably bogus in the new a.out, unlike e.g., the
3812 solib breakpoints.) */
c5aa993b 3813
c5aa993b
JM
3814 if (b->type == bp_finish)
3815 {
3816 continue;
3817 }
3818
3819 /* Without a symbolic address, we have little hope of the
3820 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3821 a.out. */
c5aa993b
JM
3822 if (b->addr_string == NULL)
3823 {
3824 delete_breakpoint (b);
3825 continue;
3826 }
c5aa993b 3827 }
c906108c
SS
3828}
3829
3830int
d80ee84f 3831detach_breakpoints (ptid_t ptid)
c906108c 3832{
35df4500 3833 struct bp_location *bl, **blp_tmp;
3a1bae8e 3834 int val = 0;
ce696e05 3835 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3836 struct inferior *inf = current_inferior ();
c5aa993b 3837
dfd4cc63 3838 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3839 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3840
6c95b8df 3841 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3842 inferior_ptid = ptid;
35df4500 3843 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3844 {
35df4500 3845 if (bl->pspace != inf->pspace)
6c95b8df
PA
3846 continue;
3847
bd9673a4
PW
3848 /* This function must physically remove breakpoints locations
3849 from the specified ptid, without modifying the breakpoint
3850 package's state. Locations of type bp_loc_other are only
3851 maintained at GDB side. So, there is no need to remove
3852 these bp_loc_other locations. Moreover, removing these
3853 would modify the breakpoint package's state. */
3854 if (bl->loc_type == bp_loc_other)
3855 continue;
3856
35df4500
TJB
3857 if (bl->inserted)
3858 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3859 }
d03285ec 3860
ce696e05 3861 do_cleanups (old_chain);
3a1bae8e 3862 return val;
c906108c
SS
3863}
3864
35df4500 3865/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3866 Note that this is used to detach breakpoints from a child fork.
3867 When we get here, the child isn't in the inferior list, and neither
3868 do we have objects to represent its address space --- we should
35df4500 3869 *not* look at bl->pspace->aspace here. */
6c95b8df 3870
c906108c 3871static int
35df4500 3872remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3873{
3874 int val;
c5aa993b 3875
35df4500
TJB
3876 /* BL is never in moribund_locations by our callers. */
3877 gdb_assert (bl->owner != NULL);
2bdf28a0 3878
1a853c52 3879 if (bl->permanent)
c2c6d25f
JM
3880 /* Permanent breakpoints cannot be inserted or removed. */
3881 return 0;
3882
74960c60
VP
3883 /* The type of none suggests that owner is actually deleted.
3884 This should not ever happen. */
35df4500 3885 gdb_assert (bl->owner->type != bp_none);
0bde7532 3886
35df4500
TJB
3887 if (bl->loc_type == bp_loc_software_breakpoint
3888 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3889 {
c02f5703
MS
3890 /* "Normal" instruction breakpoint: either the standard
3891 trap-instruction bp (bp_breakpoint), or a
3892 bp_hardware_breakpoint. */
3893
3894 /* First check to see if we have to handle an overlay. */
3895 if (overlay_debugging == ovly_off
35df4500
TJB
3896 || bl->section == NULL
3897 || !(section_is_overlay (bl->section)))
c02f5703
MS
3898 {
3899 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3900
3901 /* If we're trying to uninsert a memory breakpoint that we
3902 know is set in a dynamic object that is marked
3903 shlib_disabled, then either the dynamic object was
3904 removed with "remove-symbol-file" or with
3905 "nosharedlibrary". In the former case, we don't know
3906 whether another dynamic object might have loaded over the
3907 breakpoint's address -- the user might well let us know
3908 about it next with add-symbol-file (the whole point of
d03de421 3909 add-symbol-file is letting the user manually maintain a
08351840
PA
3910 list of dynamically loaded objects). If we have the
3911 breakpoint's shadow memory, that is, this is a software
3912 breakpoint managed by GDB, check whether the breakpoint
3913 is still inserted in memory, to avoid overwriting wrong
3914 code with stale saved shadow contents. Note that HW
3915 breakpoints don't have shadow memory, as they're
3916 implemented using a mechanism that is not dependent on
3917 being able to modify the target's memory, and as such
3918 they should always be removed. */
3919 if (bl->shlib_disabled
3920 && bl->target_info.shadow_len != 0
3921 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3922 val = 0;
3923 else
3924 val = bl->owner->ops->remove_location (bl);
c02f5703 3925 }
c906108c
SS
3926 else
3927 {
4a64f543 3928 /* This breakpoint is in an overlay section.
c02f5703
MS
3929 Did we set a breakpoint at the LMA? */
3930 if (!overlay_events_enabled)
3931 {
3932 /* Yes -- overlay event support is not active, so we
3933 should have set a breakpoint at the LMA. Remove it.
3934 */
c02f5703
MS
3935 /* Ignore any failures: if the LMA is in ROM, we will
3936 have already warned when we failed to insert it. */
35df4500
TJB
3937 if (bl->loc_type == bp_loc_hardware_breakpoint)
3938 target_remove_hw_breakpoint (bl->gdbarch,
3939 &bl->overlay_target_info);
c02f5703 3940 else
35df4500
TJB
3941 target_remove_breakpoint (bl->gdbarch,
3942 &bl->overlay_target_info);
c02f5703
MS
3943 }
3944 /* Did we set a breakpoint at the VMA?
3945 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3946 if (bl->inserted)
c906108c 3947 {
c02f5703
MS
3948 /* Yes -- remove it. Previously we did not bother to
3949 remove the breakpoint if the section had been
3950 unmapped, but let's not rely on that being safe. We
3951 don't know what the overlay manager might do. */
aa67235e
UW
3952
3953 /* However, we should remove *software* breakpoints only
3954 if the section is still mapped, or else we overwrite
3955 wrong code with the saved shadow contents. */
348d480f
PA
3956 if (bl->loc_type == bp_loc_hardware_breakpoint
3957 || section_is_mapped (bl->section))
3958 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3959 else
3960 val = 0;
c906108c 3961 }
c02f5703
MS
3962 else
3963 {
3964 /* No -- not inserted, so no need to remove. No error. */
3965 val = 0;
3966 }
c906108c 3967 }
879d1e6b 3968
08351840
PA
3969 /* In some cases, we might not be able to remove a breakpoint in
3970 a shared library that has already been removed, but we have
3971 not yet processed the shlib unload event. Similarly for an
3972 unloaded add-symbol-file object - the user might not yet have
3973 had the chance to remove-symbol-file it. shlib_disabled will
3974 be set if the library/object has already been removed, but
3975 the breakpoint hasn't been uninserted yet, e.g., after
3976 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3977 always-inserted mode. */
076855f9 3978 if (val
08351840
PA
3979 && (bl->loc_type == bp_loc_software_breakpoint
3980 && (bl->shlib_disabled
3981 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3982 || shared_objfile_contains_address_p (bl->pspace,
3983 bl->address))))
879d1e6b
UW
3984 val = 0;
3985
c906108c
SS
3986 if (val)
3987 return val;
35df4500 3988 bl->inserted = (is == mark_inserted);
c906108c 3989 }
35df4500 3990 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3991 {
77b06cd7
TJB
3992 gdb_assert (bl->owner->ops != NULL
3993 && bl->owner->ops->remove_location != NULL);
3994
35df4500 3995 bl->inserted = (is == mark_inserted);
77b06cd7 3996 bl->owner->ops->remove_location (bl);
2e70b7b9 3997
c906108c 3998 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3999 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4000 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4001 bl->owner->number);
c906108c 4002 }
35df4500
TJB
4003 else if (bl->owner->type == bp_catchpoint
4004 && breakpoint_enabled (bl->owner)
4005 && !bl->duplicate)
ce78b96d 4006 {
77b06cd7
TJB
4007 gdb_assert (bl->owner->ops != NULL
4008 && bl->owner->ops->remove_location != NULL);
ce78b96d 4009
77b06cd7 4010 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4011 if (val)
4012 return val;
77b06cd7 4013
35df4500 4014 bl->inserted = (is == mark_inserted);
ce78b96d 4015 }
c906108c
SS
4016
4017 return 0;
4018}
4019
6c95b8df 4020static int
35df4500 4021remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4022{
4023 int ret;
4024 struct cleanup *old_chain;
4025
35df4500
TJB
4026 /* BL is never in moribund_locations by our callers. */
4027 gdb_assert (bl->owner != NULL);
2bdf28a0 4028
1a853c52 4029 if (bl->permanent)
6c95b8df
PA
4030 /* Permanent breakpoints cannot be inserted or removed. */
4031 return 0;
4032
4033 /* The type of none suggests that owner is actually deleted.
4034 This should not ever happen. */
35df4500 4035 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4036
4037 old_chain = save_current_space_and_thread ();
4038
35df4500 4039 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4040
35df4500 4041 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4042
4043 do_cleanups (old_chain);
4044 return ret;
4045}
4046
c906108c
SS
4047/* Clear the "inserted" flag in all breakpoints. */
4048
25b22b0a 4049void
fba45db2 4050mark_breakpoints_out (void)
c906108c 4051{
35df4500 4052 struct bp_location *bl, **blp_tmp;
c906108c 4053
35df4500 4054 ALL_BP_LOCATIONS (bl, blp_tmp)
1a853c52
PA
4055 if (bl->pspace == current_program_space
4056 && !bl->permanent)
35df4500 4057 bl->inserted = 0;
c906108c
SS
4058}
4059
53a5351d
JM
4060/* Clear the "inserted" flag in all breakpoints and delete any
4061 breakpoints which should go away between runs of the program.
c906108c
SS
4062
4063 Plus other such housekeeping that has to be done for breakpoints
4064 between runs.
4065
53a5351d
JM
4066 Note: this function gets called at the end of a run (by
4067 generic_mourn_inferior) and when a run begins (by
4a64f543 4068 init_wait_for_inferior). */
c906108c
SS
4069
4070
4071
4072void
fba45db2 4073breakpoint_init_inferior (enum inf_context context)
c906108c 4074{
35df4500
TJB
4075 struct breakpoint *b, *b_tmp;
4076 struct bp_location *bl, **blp_tmp;
1c5cfe86 4077 int ix;
6c95b8df 4078 struct program_space *pspace = current_program_space;
c906108c 4079
50c71eaf
PA
4080 /* If breakpoint locations are shared across processes, then there's
4081 nothing to do. */
f5656ead 4082 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4083 return;
4084
1a853c52 4085 mark_breakpoints_out ();
075f6582 4086
35df4500 4087 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4088 {
6c95b8df
PA
4089 if (b->loc && b->loc->pspace != pspace)
4090 continue;
4091
c5aa993b
JM
4092 switch (b->type)
4093 {
4094 case bp_call_dummy:
e2e4d78b 4095 case bp_longjmp_call_dummy:
c906108c 4096
c5aa993b 4097 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4098 cause problems when the inferior is rerun, so we better get
4099 rid of it. */
4100
4101 case bp_watchpoint_scope:
4102
4103 /* Also get rid of scope breakpoints. */
4104
4105 case bp_shlib_event:
4106
4107 /* Also remove solib event breakpoints. Their addresses may
4108 have changed since the last time we ran the program.
4109 Actually we may now be debugging against different target;
4110 and so the solib backend that installed this breakpoint may
4111 not be used in by the target. E.g.,
4112
4113 (gdb) file prog-linux
4114 (gdb) run # native linux target
4115 ...
4116 (gdb) kill
4117 (gdb) file prog-win.exe
4118 (gdb) tar rem :9999 # remote Windows gdbserver.
4119 */
c906108c 4120
f59f708a
PA
4121 case bp_step_resume:
4122
4123 /* Also remove step-resume breakpoints. */
4124
7c16b83e
PA
4125 case bp_single_step:
4126
4127 /* Also remove single-step breakpoints. */
4128
c5aa993b
JM
4129 delete_breakpoint (b);
4130 break;
c906108c 4131
c5aa993b
JM
4132 case bp_watchpoint:
4133 case bp_hardware_watchpoint:
4134 case bp_read_watchpoint:
4135 case bp_access_watchpoint:
3a5c3e22
PA
4136 {
4137 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4138
3a5c3e22
PA
4139 /* Likewise for watchpoints on local expressions. */
4140 if (w->exp_valid_block != NULL)
4141 delete_breakpoint (b);
4142 else if (context == inf_starting)
4143 {
4144 /* Reset val field to force reread of starting value in
4145 insert_breakpoints. */
4146 if (w->val)
4147 value_free (w->val);
4148 w->val = NULL;
4149 w->val_valid = 0;
c860120c 4150 }
3a5c3e22 4151 }
c5aa993b
JM
4152 break;
4153 default:
c5aa993b
JM
4154 break;
4155 }
4156 }
1c5cfe86
PA
4157
4158 /* Get rid of the moribund locations. */
35df4500
TJB
4159 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4160 decref_bp_location (&bl);
1c5cfe86 4161 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4162}
4163
6c95b8df
PA
4164/* These functions concern about actual breakpoints inserted in the
4165 target --- to e.g. check if we need to do decr_pc adjustment or if
4166 we need to hop over the bkpt --- so we check for address space
4167 match, not program space. */
4168
c2c6d25f
JM
4169/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4170 exists at PC. It returns ordinary_breakpoint_here if it's an
4171 ordinary breakpoint, or permanent_breakpoint_here if it's a
4172 permanent breakpoint.
4173 - When continuing from a location with an ordinary breakpoint, we
4174 actually single step once before calling insert_breakpoints.
e5dd4106 4175 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4176 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4177 the target, to advance the PC past the breakpoint. */
c906108c 4178
c2c6d25f 4179enum breakpoint_here
6c95b8df 4180breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4181{
35df4500 4182 struct bp_location *bl, **blp_tmp;
c2c6d25f 4183 int any_breakpoint_here = 0;
c906108c 4184
35df4500 4185 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4186 {
35df4500
TJB
4187 if (bl->loc_type != bp_loc_software_breakpoint
4188 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4189 continue;
4190
f1310107 4191 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4192 if ((breakpoint_enabled (bl->owner)
1a853c52 4193 || bl->permanent)
f1310107 4194 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4195 {
4196 if (overlay_debugging
35df4500
TJB
4197 && section_is_overlay (bl->section)
4198 && !section_is_mapped (bl->section))
075f6582 4199 continue; /* unmapped overlay -- can't be a match */
1a853c52 4200 else if (bl->permanent)
075f6582
DJ
4201 return permanent_breakpoint_here;
4202 else
4203 any_breakpoint_here = 1;
4204 }
4205 }
c906108c 4206
c2c6d25f 4207 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4208}
4209
1c5cfe86
PA
4210/* Return true if there's a moribund breakpoint at PC. */
4211
4212int
6c95b8df 4213moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4214{
4215 struct bp_location *loc;
4216 int ix;
4217
4218 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4219 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4220 return 1;
4221
4222 return 0;
4223}
c2c6d25f 4224
f7ce857f
PA
4225/* Returns non-zero iff BL is inserted at PC, in address space
4226 ASPACE. */
4227
4228static int
4229bp_location_inserted_here_p (struct bp_location *bl,
4230 struct address_space *aspace, CORE_ADDR pc)
4231{
4232 if (bl->inserted
4233 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4234 aspace, pc))
4235 {
4236 if (overlay_debugging
4237 && section_is_overlay (bl->section)
4238 && !section_is_mapped (bl->section))
4239 return 0; /* unmapped overlay -- can't be a match */
4240 else
4241 return 1;
4242 }
4243 return 0;
4244}
4245
a1fd2fa5 4246/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4247
4248int
a1fd2fa5 4249breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4250{
f7ce857f
PA
4251 struct bp_location **blp, **blp_tmp = NULL;
4252 struct bp_location *bl;
c906108c 4253
f7ce857f 4254 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4255 {
f7ce857f
PA
4256 struct bp_location *bl = *blp;
4257
35df4500
TJB
4258 if (bl->loc_type != bp_loc_software_breakpoint
4259 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4260 continue;
4261
f7ce857f
PA
4262 if (bp_location_inserted_here_p (bl, aspace, pc))
4263 return 1;
c5aa993b 4264 }
c36b740a
VP
4265 return 0;
4266}
4267
a1fd2fa5
PA
4268/* This function returns non-zero iff there is a software breakpoint
4269 inserted at PC. */
c36b740a
VP
4270
4271int
a1fd2fa5
PA
4272software_breakpoint_inserted_here_p (struct address_space *aspace,
4273 CORE_ADDR pc)
4fa8626c 4274{
f7ce857f
PA
4275 struct bp_location **blp, **blp_tmp = NULL;
4276 struct bp_location *bl;
4fa8626c 4277
f7ce857f 4278 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4279 {
f7ce857f
PA
4280 struct bp_location *bl = *blp;
4281
35df4500 4282 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4283 continue;
4284
f7ce857f
PA
4285 if (bp_location_inserted_here_p (bl, aspace, pc))
4286 return 1;
4fa8626c
DJ
4287 }
4288
4289 return 0;
9c02b525
PA
4290}
4291
4292/* See breakpoint.h. */
4293
4294int
4295hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4296 CORE_ADDR pc)
4297{
4298 struct bp_location **blp, **blp_tmp = NULL;
4299 struct bp_location *bl;
4300
4301 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4302 {
4303 struct bp_location *bl = *blp;
4304
4305 if (bl->loc_type != bp_loc_hardware_breakpoint)
4306 continue;
4307
4308 if (bp_location_inserted_here_p (bl, aspace, pc))
4309 return 1;
4310 }
4311
4312 return 0;
4fa8626c
DJ
4313}
4314
9093389c
PA
4315int
4316hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4317 CORE_ADDR addr, ULONGEST len)
4318{
4319 struct breakpoint *bpt;
4320
4321 ALL_BREAKPOINTS (bpt)
4322 {
4323 struct bp_location *loc;
4324
4325 if (bpt->type != bp_hardware_watchpoint
4326 && bpt->type != bp_access_watchpoint)
4327 continue;
4328
4329 if (!breakpoint_enabled (bpt))
4330 continue;
4331
4332 for (loc = bpt->loc; loc; loc = loc->next)
4333 if (loc->pspace->aspace == aspace && loc->inserted)
4334 {
4335 CORE_ADDR l, h;
4336
4337 /* Check for intersection. */
4338 l = max (loc->address, addr);
4339 h = min (loc->address + loc->length, addr + len);
4340 if (l < h)
4341 return 1;
4342 }
4343 }
4344 return 0;
4345}
c906108c 4346\f
c5aa993b 4347
c906108c
SS
4348/* bpstat stuff. External routines' interfaces are documented
4349 in breakpoint.h. */
4350
4351int
c326b90e 4352is_catchpoint (struct breakpoint *ep)
c906108c 4353{
533be4dd 4354 return (ep->type == bp_catchpoint);
c906108c
SS
4355}
4356
f431efe5
PA
4357/* Frees any storage that is part of a bpstat. Does not walk the
4358 'next' chain. */
4359
4360static void
198757a8
VP
4361bpstat_free (bpstat bs)
4362{
4363 if (bs->old_val != NULL)
4364 value_free (bs->old_val);
9add0f1b 4365 decref_counted_command_line (&bs->commands);
f431efe5 4366 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4367 xfree (bs);
4368}
4369
c906108c
SS
4370/* Clear a bpstat so that it says we are not at any breakpoint.
4371 Also free any storage that is part of a bpstat. */
4372
4373void
fba45db2 4374bpstat_clear (bpstat *bsp)
c906108c
SS
4375{
4376 bpstat p;
4377 bpstat q;
4378
4379 if (bsp == 0)
4380 return;
4381 p = *bsp;
4382 while (p != NULL)
4383 {
4384 q = p->next;
198757a8 4385 bpstat_free (p);
c906108c
SS
4386 p = q;
4387 }
4388 *bsp = NULL;
4389}
4390
4391/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4392 is part of the bpstat is copied as well. */
4393
4394bpstat
fba45db2 4395bpstat_copy (bpstat bs)
c906108c
SS
4396{
4397 bpstat p = NULL;
4398 bpstat tmp;
4399 bpstat retval = NULL;
4400
4401 if (bs == NULL)
4402 return bs;
4403
4404 for (; bs != NULL; bs = bs->next)
4405 {
4406 tmp = (bpstat) xmalloc (sizeof (*tmp));
4407 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4408 incref_counted_command_line (tmp->commands);
f431efe5 4409 incref_bp_location (tmp->bp_location_at);
31cc81e9 4410 if (bs->old_val != NULL)
3c3185ac
JK
4411 {
4412 tmp->old_val = value_copy (bs->old_val);
4413 release_value (tmp->old_val);
4414 }
31cc81e9 4415
c906108c
SS
4416 if (p == NULL)
4417 /* This is the first thing in the chain. */
4418 retval = tmp;
4419 else
4420 p->next = tmp;
4421 p = tmp;
4422 }
4423 p->next = NULL;
4424 return retval;
4425}
4426
4a64f543 4427/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4428
4429bpstat
fba45db2 4430bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4431{
c5aa993b
JM
4432 if (bsp == NULL)
4433 return NULL;
c906108c 4434
c5aa993b
JM
4435 for (; bsp != NULL; bsp = bsp->next)
4436 {
f431efe5 4437 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4438 return bsp;
4439 }
c906108c
SS
4440 return NULL;
4441}
4442
ab04a2af
TT
4443/* See breakpoint.h. */
4444
47591c29 4445int
427cd150 4446bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4447{
ab04a2af
TT
4448 for (; bsp != NULL; bsp = bsp->next)
4449 {
427cd150
TT
4450 if (bsp->breakpoint_at == NULL)
4451 {
4452 /* A moribund location can never explain a signal other than
4453 GDB_SIGNAL_TRAP. */
4454 if (sig == GDB_SIGNAL_TRAP)
47591c29 4455 return 1;
427cd150
TT
4456 }
4457 else
47591c29
PA
4458 {
4459 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4460 sig))
4461 return 1;
4462 }
ab04a2af
TT
4463 }
4464
47591c29 4465 return 0;
ab04a2af
TT
4466}
4467
4a64f543
MS
4468/* Put in *NUM the breakpoint number of the first breakpoint we are
4469 stopped at. *BSP upon return is a bpstat which points to the
4470 remaining breakpoints stopped at (but which is not guaranteed to be
4471 good for anything but further calls to bpstat_num).
4472
8671a17b
PA
4473 Return 0 if passed a bpstat which does not indicate any breakpoints.
4474 Return -1 if stopped at a breakpoint that has been deleted since
4475 we set it.
4476 Return 1 otherwise. */
c906108c
SS
4477
4478int
8671a17b 4479bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4480{
4481 struct breakpoint *b;
4482
4483 if ((*bsp) == NULL)
4484 return 0; /* No more breakpoint values */
8671a17b 4485
4a64f543
MS
4486 /* We assume we'll never have several bpstats that correspond to a
4487 single breakpoint -- otherwise, this function might return the
4488 same number more than once and this will look ugly. */
f431efe5 4489 b = (*bsp)->breakpoint_at;
8671a17b
PA
4490 *bsp = (*bsp)->next;
4491 if (b == NULL)
4492 return -1; /* breakpoint that's been deleted since */
4493
4494 *num = b->number; /* We have its number */
4495 return 1;
c906108c
SS
4496}
4497
e93ca019 4498/* See breakpoint.h. */
c906108c
SS
4499
4500void
e93ca019 4501bpstat_clear_actions (void)
c906108c 4502{
e93ca019
JK
4503 struct thread_info *tp;
4504 bpstat bs;
4505
4506 if (ptid_equal (inferior_ptid, null_ptid))
4507 return;
4508
4509 tp = find_thread_ptid (inferior_ptid);
4510 if (tp == NULL)
4511 return;
4512
4513 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4514 {
9add0f1b 4515 decref_counted_command_line (&bs->commands);
abf85f46 4516
c906108c
SS
4517 if (bs->old_val != NULL)
4518 {
4519 value_free (bs->old_val);
4520 bs->old_val = NULL;
4521 }
4522 }
4523}
4524
f3b1572e
PA
4525/* Called when a command is about to proceed the inferior. */
4526
4527static void
4528breakpoint_about_to_proceed (void)
4529{
4530 if (!ptid_equal (inferior_ptid, null_ptid))
4531 {
4532 struct thread_info *tp = inferior_thread ();
4533
4534 /* Allow inferior function calls in breakpoint commands to not
4535 interrupt the command list. When the call finishes
4536 successfully, the inferior will be standing at the same
4537 breakpoint as if nothing happened. */
16c381f0 4538 if (tp->control.in_infcall)
f3b1572e
PA
4539 return;
4540 }
4541
4542 breakpoint_proceeded = 1;
4543}
4544
4a64f543
MS
4545/* Stub for cleaning up our state if we error-out of a breakpoint
4546 command. */
c906108c 4547static void
4efb68b1 4548cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4549{
4550 executing_breakpoint_commands = 0;
4551}
4552
abf85f46
JK
4553/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4554 or its equivalent. */
4555
4556static int
4557command_line_is_silent (struct command_line *cmd)
4558{
4559 return cmd && (strcmp ("silent", cmd->line) == 0
4560 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4561}
4562
4a64f543
MS
4563/* Execute all the commands associated with all the breakpoints at
4564 this location. Any of these commands could cause the process to
4565 proceed beyond this point, etc. We look out for such changes by
4566 checking the global "breakpoint_proceeded" after each command.
c906108c 4567
347bddb7
PA
4568 Returns true if a breakpoint command resumed the inferior. In that
4569 case, it is the caller's responsibility to recall it again with the
4570 bpstat of the current thread. */
4571
4572static int
4573bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4574{
4575 bpstat bs;
4576 struct cleanup *old_chain;
347bddb7 4577 int again = 0;
c906108c
SS
4578
4579 /* Avoid endless recursion if a `source' command is contained
4580 in bs->commands. */
4581 if (executing_breakpoint_commands)
347bddb7 4582 return 0;
c906108c
SS
4583
4584 executing_breakpoint_commands = 1;
4585 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4586
cf6c5ffb
TT
4587 prevent_dont_repeat ();
4588
4a64f543 4589 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4590 bs = *bsp;
4591
4592 breakpoint_proceeded = 0;
4593 for (; bs != NULL; bs = bs->next)
4594 {
9add0f1b 4595 struct counted_command_line *ccmd;
6c50ab1c
JB
4596 struct command_line *cmd;
4597 struct cleanup *this_cmd_tree_chain;
4598
4599 /* Take ownership of the BSP's command tree, if it has one.
4600
4601 The command tree could legitimately contain commands like
4602 'step' and 'next', which call clear_proceed_status, which
4603 frees stop_bpstat's command tree. To make sure this doesn't
4604 free the tree we're executing out from under us, we need to
4605 take ownership of the tree ourselves. Since a given bpstat's
4606 commands are only executed once, we don't need to copy it; we
4607 can clear the pointer in the bpstat, and make sure we free
4608 the tree when we're done. */
9add0f1b
TT
4609 ccmd = bs->commands;
4610 bs->commands = NULL;
abf85f46
JK
4611 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4612 cmd = ccmd ? ccmd->commands : NULL;
4613 if (command_line_is_silent (cmd))
4614 {
4615 /* The action has been already done by bpstat_stop_status. */
4616 cmd = cmd->next;
4617 }
6c50ab1c 4618
c906108c
SS
4619 while (cmd != NULL)
4620 {
4621 execute_control_command (cmd);
4622
4623 if (breakpoint_proceeded)
4624 break;
4625 else
4626 cmd = cmd->next;
4627 }
6c50ab1c
JB
4628
4629 /* We can free this command tree now. */
4630 do_cleanups (this_cmd_tree_chain);
4631
c906108c 4632 if (breakpoint_proceeded)
32c1e744 4633 {
5589af0e 4634 if (interpreter_async && target_can_async_p ())
347bddb7
PA
4635 /* If we are in async mode, then the target might be still
4636 running, not stopped at any breakpoint, so nothing for
4637 us to do here -- just return to the event loop. */
4638 ;
32c1e744
VP
4639 else
4640 /* In sync mode, when execute_control_command returns
4641 we're already standing on the next breakpoint.
347bddb7
PA
4642 Breakpoint commands for that stop were not run, since
4643 execute_command does not run breakpoint commands --
4644 only command_line_handler does, but that one is not
4645 involved in execution of breakpoint commands. So, we
4646 can now execute breakpoint commands. It should be
4647 noted that making execute_command do bpstat actions is
4648 not an option -- in this case we'll have recursive
4649 invocation of bpstat for each breakpoint with a
4650 command, and can easily blow up GDB stack. Instead, we
4651 return true, which will trigger the caller to recall us
4652 with the new stop_bpstat. */
4653 again = 1;
4654 break;
32c1e744 4655 }
c906108c 4656 }
c2b8ed2c 4657 do_cleanups (old_chain);
347bddb7
PA
4658 return again;
4659}
4660
4661void
4662bpstat_do_actions (void)
4663{
353d1d73
JK
4664 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4665
347bddb7
PA
4666 /* Do any commands attached to breakpoint we are stopped at. */
4667 while (!ptid_equal (inferior_ptid, null_ptid)
4668 && target_has_execution
4669 && !is_exited (inferior_ptid)
4670 && !is_executing (inferior_ptid))
4671 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4672 and only return when it is stopped at the next breakpoint, we
4673 keep doing breakpoint actions until it returns false to
4674 indicate the inferior was not resumed. */
16c381f0 4675 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4676 break;
353d1d73
JK
4677
4678 discard_cleanups (cleanup_if_error);
c906108c
SS
4679}
4680
fa4727a6
DJ
4681/* Print out the (old or new) value associated with a watchpoint. */
4682
4683static void
4684watchpoint_value_print (struct value *val, struct ui_file *stream)
4685{
4686 if (val == NULL)
4687 fprintf_unfiltered (stream, _("<unreadable>"));
4688 else
79a45b7d
TT
4689 {
4690 struct value_print_options opts;
4691 get_user_print_options (&opts);
4692 value_print (val, stream, &opts);
4693 }
fa4727a6
DJ
4694}
4695
e514a9d6 4696/* Generic routine for printing messages indicating why we
4a64f543 4697 stopped. The behavior of this function depends on the value
e514a9d6
JM
4698 'print_it' in the bpstat structure. Under some circumstances we
4699 may decide not to print anything here and delegate the task to
4a64f543 4700 normal_stop(). */
e514a9d6
JM
4701
4702static enum print_stop_action
4703print_bp_stop_message (bpstat bs)
4704{
4705 switch (bs->print_it)
4706 {
4707 case print_it_noop:
4a64f543 4708 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4709 return PRINT_UNKNOWN;
4710 break;
4711
4712 case print_it_done:
4713 /* We still want to print the frame, but we already printed the
4a64f543 4714 relevant messages. */
e514a9d6
JM
4715 return PRINT_SRC_AND_LOC;
4716 break;
4717
4718 case print_it_normal:
4f8d1dc6 4719 {
f431efe5
PA
4720 struct breakpoint *b = bs->breakpoint_at;
4721
1a6a67de
TJB
4722 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4723 which has since been deleted. */
4724 if (b == NULL)
4725 return PRINT_UNKNOWN;
4726
348d480f
PA
4727 /* Normal case. Call the breakpoint's print_it method. */
4728 return b->ops->print_it (bs);
4f8d1dc6 4729 }
348d480f 4730 break;
3086aeae 4731
e514a9d6 4732 default:
8e65ff28 4733 internal_error (__FILE__, __LINE__,
e2e0b3e5 4734 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4735 break;
c906108c 4736 }
c906108c
SS
4737}
4738
edcc5120
TT
4739/* A helper function that prints a shared library stopped event. */
4740
4741static void
4742print_solib_event (int is_catchpoint)
4743{
4744 int any_deleted
4745 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4746 int any_added
4747 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4748
4749 if (!is_catchpoint)
4750 {
4751 if (any_added || any_deleted)
4752 ui_out_text (current_uiout,
4753 _("Stopped due to shared library event:\n"));
4754 else
4755 ui_out_text (current_uiout,
4756 _("Stopped due to shared library event (no "
4757 "libraries added or removed)\n"));
4758 }
4759
4760 if (ui_out_is_mi_like_p (current_uiout))
4761 ui_out_field_string (current_uiout, "reason",
4762 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4763
4764 if (any_deleted)
4765 {
4766 struct cleanup *cleanup;
4767 char *name;
4768 int ix;
4769
4770 ui_out_text (current_uiout, _(" Inferior unloaded "));
4771 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4772 "removed");
4773 for (ix = 0;
4774 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4775 ix, name);
4776 ++ix)
4777 {
4778 if (ix > 0)
4779 ui_out_text (current_uiout, " ");
4780 ui_out_field_string (current_uiout, "library", name);
4781 ui_out_text (current_uiout, "\n");
4782 }
4783
4784 do_cleanups (cleanup);
4785 }
4786
4787 if (any_added)
4788 {
4789 struct so_list *iter;
4790 int ix;
4791 struct cleanup *cleanup;
4792
4793 ui_out_text (current_uiout, _(" Inferior loaded "));
4794 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4795 "added");
4796 for (ix = 0;
4797 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4798 ix, iter);
4799 ++ix)
4800 {
4801 if (ix > 0)
4802 ui_out_text (current_uiout, " ");
4803 ui_out_field_string (current_uiout, "library", iter->so_name);
4804 ui_out_text (current_uiout, "\n");
4805 }
4806
4807 do_cleanups (cleanup);
4808 }
4809}
4810
e514a9d6
JM
4811/* Print a message indicating what happened. This is called from
4812 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4813 list - a list of the eventpoints that caused this stop. KIND is
4814 the target_waitkind for the stopping event. This
e514a9d6
JM
4815 routine calls the generic print routine for printing a message
4816 about reasons for stopping. This will print (for example) the
4817 "Breakpoint n," part of the output. The return value of this
4818 routine is one of:
c906108c 4819
4a64f543 4820 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4821 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4822 code to print the location. An example is
c5aa993b
JM
4823 "Breakpoint 1, " which should be followed by
4824 the location.
917317f4 4825 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4826 to also print the location part of the message.
4827 An example is the catch/throw messages, which
4a64f543 4828 don't require a location appended to the end.
917317f4 4829 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4830 further info to be printed. */
c906108c 4831
917317f4 4832enum print_stop_action
36dfb11c 4833bpstat_print (bpstat bs, int kind)
c906108c
SS
4834{
4835 int val;
c5aa993b 4836
c906108c 4837 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4838 (Currently all watchpoints go on the bpstat whether hit or not.
4839 That probably could (should) be changed, provided care is taken
c906108c 4840 with respect to bpstat_explains_signal). */
e514a9d6
JM
4841 for (; bs; bs = bs->next)
4842 {
4843 val = print_bp_stop_message (bs);
4844 if (val == PRINT_SRC_ONLY
4845 || val == PRINT_SRC_AND_LOC
4846 || val == PRINT_NOTHING)
4847 return val;
4848 }
c906108c 4849
36dfb11c
TT
4850 /* If we had hit a shared library event breakpoint,
4851 print_bp_stop_message would print out this message. If we hit an
4852 OS-level shared library event, do the same thing. */
4853 if (kind == TARGET_WAITKIND_LOADED)
4854 {
edcc5120 4855 print_solib_event (0);
36dfb11c
TT
4856 return PRINT_NOTHING;
4857 }
4858
e514a9d6 4859 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4860 with and nothing was printed. */
917317f4 4861 return PRINT_UNKNOWN;
c906108c
SS
4862}
4863
c42bd95a
DE
4864/* Evaluate the expression EXP and return 1 if value is zero.
4865 This returns the inverse of the condition because it is called
4866 from catch_errors which returns 0 if an exception happened, and if an
4867 exception happens we want execution to stop.
4a64f543 4868 The argument is a "struct expression *" that has been cast to a
c42bd95a 4869 "void *" to make it pass through catch_errors. */
c906108c
SS
4870
4871static int
4efb68b1 4872breakpoint_cond_eval (void *exp)
c906108c 4873{
278cd55f 4874 struct value *mark = value_mark ();
c5aa993b 4875 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4876
c906108c
SS
4877 value_free_to_mark (mark);
4878 return i;
4879}
4880
5760d0ab 4881/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4882
4883static bpstat
5760d0ab 4884bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4885{
4886 bpstat bs;
4887
4888 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4889 bs->next = NULL;
4890 **bs_link_pointer = bs;
4891 *bs_link_pointer = &bs->next;
f431efe5
PA
4892 bs->breakpoint_at = bl->owner;
4893 bs->bp_location_at = bl;
4894 incref_bp_location (bl);
c906108c
SS
4895 /* If the condition is false, etc., don't do the commands. */
4896 bs->commands = NULL;
4897 bs->old_val = NULL;
4898 bs->print_it = print_it_normal;
4899 return bs;
4900}
4901\f
d983da9c
DJ
4902/* The target has stopped with waitstatus WS. Check if any hardware
4903 watchpoints have triggered, according to the target. */
4904
4905int
4906watchpoints_triggered (struct target_waitstatus *ws)
4907{
d92524f1 4908 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4909 CORE_ADDR addr;
4910 struct breakpoint *b;
4911
4912 if (!stopped_by_watchpoint)
4913 {
4914 /* We were not stopped by a watchpoint. Mark all watchpoints
4915 as not triggered. */
4916 ALL_BREAKPOINTS (b)
cc60f2e3 4917 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4918 {
4919 struct watchpoint *w = (struct watchpoint *) b;
4920
4921 w->watchpoint_triggered = watch_triggered_no;
4922 }
d983da9c
DJ
4923
4924 return 0;
4925 }
4926
4927 if (!target_stopped_data_address (&current_target, &addr))
4928 {
4929 /* We were stopped by a watchpoint, but we don't know where.
4930 Mark all watchpoints as unknown. */
4931 ALL_BREAKPOINTS (b)
cc60f2e3 4932 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4933 {
4934 struct watchpoint *w = (struct watchpoint *) b;
4935
4936 w->watchpoint_triggered = watch_triggered_unknown;
4937 }
d983da9c 4938
3c4797ba 4939 return 1;
d983da9c
DJ
4940 }
4941
4942 /* The target could report the data address. Mark watchpoints
4943 affected by this data address as triggered, and all others as not
4944 triggered. */
4945
4946 ALL_BREAKPOINTS (b)
cc60f2e3 4947 if (is_hardware_watchpoint (b))
d983da9c 4948 {
3a5c3e22 4949 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4950 struct bp_location *loc;
d983da9c 4951
3a5c3e22 4952 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4953 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4954 {
3a5c3e22 4955 if (is_masked_watchpoint (b))
9c06b0b4 4956 {
3a5c3e22
PA
4957 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4958 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4959
4960 if (newaddr == start)
4961 {
3a5c3e22 4962 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4963 break;
4964 }
4965 }
4966 /* Exact match not required. Within range is sufficient. */
4967 else if (target_watchpoint_addr_within_range (&current_target,
4968 addr, loc->address,
4969 loc->length))
4970 {
3a5c3e22 4971 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4972 break;
4973 }
4974 }
d983da9c
DJ
4975 }
4976
4977 return 1;
4978}
4979
c906108c
SS
4980/* Possible return values for watchpoint_check (this can't be an enum
4981 because of check_errors). */
4982/* The watchpoint has been deleted. */
4983#define WP_DELETED 1
4984/* The value has changed. */
4985#define WP_VALUE_CHANGED 2
4986/* The value has not changed. */
4987#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4988/* Ignore this watchpoint, no matter if the value changed or not. */
4989#define WP_IGNORE 4
c906108c
SS
4990
4991#define BP_TEMPFLAG 1
4992#define BP_HARDWAREFLAG 2
4993
4a64f543
MS
4994/* Evaluate watchpoint condition expression and check if its value
4995 changed.
553e4c11
JB
4996
4997 P should be a pointer to struct bpstat, but is defined as a void *
4998 in order for this function to be usable with catch_errors. */
c906108c
SS
4999
5000static int
4efb68b1 5001watchpoint_check (void *p)
c906108c
SS
5002{
5003 bpstat bs = (bpstat) p;
3a5c3e22 5004 struct watchpoint *b;
c906108c
SS
5005 struct frame_info *fr;
5006 int within_current_scope;
5007
f431efe5 5008 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5009 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5010 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5011
f6bc2008
PA
5012 /* If this is a local watchpoint, we only want to check if the
5013 watchpoint frame is in scope if the current thread is the thread
5014 that was used to create the watchpoint. */
5015 if (!watchpoint_in_thread_scope (b))
60e1c644 5016 return WP_IGNORE;
f6bc2008 5017
c906108c
SS
5018 if (b->exp_valid_block == NULL)
5019 within_current_scope = 1;
5020 else
5021 {
edb3359d
DJ
5022 struct frame_info *frame = get_current_frame ();
5023 struct gdbarch *frame_arch = get_frame_arch (frame);
5024 CORE_ADDR frame_pc = get_frame_pc (frame);
5025
4a64f543
MS
5026 /* in_function_epilogue_p() returns a non-zero value if we're
5027 still in the function but the stack frame has already been
5028 invalidated. Since we can't rely on the values of local
5029 variables after the stack has been destroyed, we are treating
5030 the watchpoint in that state as `not changed' without further
5031 checking. Don't mark watchpoints as changed if the current
5032 frame is in an epilogue - even if they are in some other
5033 frame, our view of the stack is likely to be wrong and
5034 frame_find_by_id could error out. */
a0f49112 5035 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 5036 return WP_IGNORE;
a0f49112 5037
101dcfbe 5038 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5039 within_current_scope = (fr != NULL);
69fbadd5
DJ
5040
5041 /* If we've gotten confused in the unwinder, we might have
5042 returned a frame that can't describe this variable. */
edb3359d
DJ
5043 if (within_current_scope)
5044 {
5045 struct symbol *function;
5046
5047 function = get_frame_function (fr);
5048 if (function == NULL
5049 || !contained_in (b->exp_valid_block,
5050 SYMBOL_BLOCK_VALUE (function)))
5051 within_current_scope = 0;
5052 }
69fbadd5 5053
edb3359d 5054 if (within_current_scope)
c906108c
SS
5055 /* If we end up stopping, the current frame will get selected
5056 in normal_stop. So this call to select_frame won't affect
5057 the user. */
0f7d239c 5058 select_frame (fr);
c906108c 5059 }
c5aa993b 5060
c906108c
SS
5061 if (within_current_scope)
5062 {
4a64f543
MS
5063 /* We use value_{,free_to_}mark because it could be a *long*
5064 time before we return to the command level and call
5065 free_all_values. We can't call free_all_values because we
5066 might be in the middle of evaluating a function call. */
c906108c 5067
0cf6dd15 5068 int pc = 0;
9c06b0b4 5069 struct value *mark;
fa4727a6
DJ
5070 struct value *new_val;
5071
3a5c3e22 5072 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5073 /* Since we don't know the exact trigger address (from
5074 stopped_data_address), just tell the user we've triggered
5075 a mask watchpoint. */
5076 return WP_VALUE_CHANGED;
5077
5078 mark = value_mark ();
3a1115a0 5079 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5080
bb9d5f81
PP
5081 if (b->val_bitsize != 0)
5082 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5083
4a64f543
MS
5084 /* We use value_equal_contents instead of value_equal because
5085 the latter coerces an array to a pointer, thus comparing just
5086 the address of the array instead of its contents. This is
5087 not what we want. */
fa4727a6 5088 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5089 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5090 {
fa4727a6
DJ
5091 if (new_val != NULL)
5092 {
5093 release_value (new_val);
5094 value_free_to_mark (mark);
5095 }
c906108c
SS
5096 bs->old_val = b->val;
5097 b->val = new_val;
fa4727a6 5098 b->val_valid = 1;
c906108c
SS
5099 return WP_VALUE_CHANGED;
5100 }
5101 else
5102 {
60e1c644 5103 /* Nothing changed. */
c906108c 5104 value_free_to_mark (mark);
c906108c
SS
5105 return WP_VALUE_NOT_CHANGED;
5106 }
5107 }
5108 else
5109 {
79a45e25
PA
5110 struct ui_out *uiout = current_uiout;
5111
c906108c 5112 /* This seems like the only logical thing to do because
c5aa993b
JM
5113 if we temporarily ignored the watchpoint, then when
5114 we reenter the block in which it is valid it contains
5115 garbage (in the case of a function, it may have two
5116 garbage values, one before and one after the prologue).
5117 So we can't even detect the first assignment to it and
5118 watch after that (since the garbage may or may not equal
5119 the first value assigned). */
348d480f
PA
5120 /* We print all the stop information in
5121 breakpoint_ops->print_it, but in this case, by the time we
5122 call breakpoint_ops->print_it this bp will be deleted
5123 already. So we have no choice but print the information
5124 here. */
9dc5e2a9 5125 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5126 ui_out_field_string
5127 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5128 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5129 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5130 ui_out_text (uiout,
5131 " deleted because the program has left the block in\n\
8b93c638 5132which its expression is valid.\n");
4ce44c66 5133
cdac0397 5134 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5135 decref_counted_command_line (&b->base.commands);
d0fb5eae 5136 watchpoint_del_at_next_stop (b);
c906108c
SS
5137
5138 return WP_DELETED;
5139 }
5140}
5141
18a18393 5142/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5143 breakpoint location BL. This function does not check if we should
5144 stop, only if BL explains the stop. */
5145
18a18393 5146static int
6c95b8df 5147bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5148 struct address_space *aspace, CORE_ADDR bp_addr,
5149 const struct target_waitstatus *ws)
18a18393
VP
5150{
5151 struct breakpoint *b = bl->owner;
5152
348d480f 5153 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5154 gdb_assert (b != NULL);
5155
09ac7c10 5156 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5157}
5158
3a5c3e22
PA
5159/* Determine if the watched values have actually changed, and we
5160 should stop. If not, set BS->stop to 0. */
5161
18a18393
VP
5162static void
5163bpstat_check_watchpoint (bpstat bs)
5164{
2bdf28a0 5165 const struct bp_location *bl;
3a5c3e22 5166 struct watchpoint *b;
2bdf28a0
JK
5167
5168 /* BS is built for existing struct breakpoint. */
f431efe5 5169 bl = bs->bp_location_at;
2bdf28a0 5170 gdb_assert (bl != NULL);
3a5c3e22 5171 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5172 gdb_assert (b != NULL);
18a18393 5173
18a18393 5174 {
18a18393
VP
5175 int must_check_value = 0;
5176
3a5c3e22 5177 if (b->base.type == bp_watchpoint)
18a18393
VP
5178 /* For a software watchpoint, we must always check the
5179 watched value. */
5180 must_check_value = 1;
5181 else if (b->watchpoint_triggered == watch_triggered_yes)
5182 /* We have a hardware watchpoint (read, write, or access)
5183 and the target earlier reported an address watched by
5184 this watchpoint. */
5185 must_check_value = 1;
5186 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5187 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5188 /* We were stopped by a hardware watchpoint, but the target could
5189 not report the data address. We must check the watchpoint's
5190 value. Access and read watchpoints are out of luck; without
5191 a data address, we can't figure it out. */
5192 must_check_value = 1;
3a5c3e22 5193
18a18393
VP
5194 if (must_check_value)
5195 {
3e43a32a
MS
5196 char *message
5197 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5198 b->base.number);
18a18393
VP
5199 struct cleanup *cleanups = make_cleanup (xfree, message);
5200 int e = catch_errors (watchpoint_check, bs, message,
5201 RETURN_MASK_ALL);
5202 do_cleanups (cleanups);
5203 switch (e)
5204 {
5205 case WP_DELETED:
5206 /* We've already printed what needs to be printed. */
5207 bs->print_it = print_it_done;
5208 /* Stop. */
5209 break;
60e1c644
PA
5210 case WP_IGNORE:
5211 bs->print_it = print_it_noop;
5212 bs->stop = 0;
5213 break;
18a18393 5214 case WP_VALUE_CHANGED:
3a5c3e22 5215 if (b->base.type == bp_read_watchpoint)
18a18393 5216 {
85d721b8
PA
5217 /* There are two cases to consider here:
5218
4a64f543 5219 1. We're watching the triggered memory for reads.
85d721b8
PA
5220 In that case, trust the target, and always report
5221 the watchpoint hit to the user. Even though
5222 reads don't cause value changes, the value may
5223 have changed since the last time it was read, and
5224 since we're not trapping writes, we will not see
5225 those, and as such we should ignore our notion of
5226 old value.
5227
4a64f543 5228 2. We're watching the triggered memory for both
85d721b8
PA
5229 reads and writes. There are two ways this may
5230 happen:
5231
4a64f543 5232 2.1. This is a target that can't break on data
85d721b8
PA
5233 reads only, but can break on accesses (reads or
5234 writes), such as e.g., x86. We detect this case
5235 at the time we try to insert read watchpoints.
5236
4a64f543 5237 2.2. Otherwise, the target supports read
85d721b8
PA
5238 watchpoints, but, the user set an access or write
5239 watchpoint watching the same memory as this read
5240 watchpoint.
5241
5242 If we're watching memory writes as well as reads,
5243 ignore watchpoint hits when we find that the
5244 value hasn't changed, as reads don't cause
5245 changes. This still gives false positives when
5246 the program writes the same value to memory as
5247 what there was already in memory (we will confuse
5248 it for a read), but it's much better than
5249 nothing. */
5250
5251 int other_write_watchpoint = 0;
5252
5253 if (bl->watchpoint_type == hw_read)
5254 {
5255 struct breakpoint *other_b;
5256
5257 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5258 if (other_b->type == bp_hardware_watchpoint
5259 || other_b->type == bp_access_watchpoint)
85d721b8 5260 {
3a5c3e22
PA
5261 struct watchpoint *other_w =
5262 (struct watchpoint *) other_b;
5263
5264 if (other_w->watchpoint_triggered
5265 == watch_triggered_yes)
5266 {
5267 other_write_watchpoint = 1;
5268 break;
5269 }
85d721b8
PA
5270 }
5271 }
5272
5273 if (other_write_watchpoint
5274 || bl->watchpoint_type == hw_access)
5275 {
5276 /* We're watching the same memory for writes,
5277 and the value changed since the last time we
5278 updated it, so this trap must be for a write.
5279 Ignore it. */
5280 bs->print_it = print_it_noop;
5281 bs->stop = 0;
5282 }
18a18393
VP
5283 }
5284 break;
5285 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5286 if (b->base.type == bp_hardware_watchpoint
5287 || b->base.type == bp_watchpoint)
18a18393
VP
5288 {
5289 /* Don't stop: write watchpoints shouldn't fire if
5290 the value hasn't changed. */
5291 bs->print_it = print_it_noop;
5292 bs->stop = 0;
5293 }
5294 /* Stop. */
5295 break;
5296 default:
5297 /* Can't happen. */
5298 case 0:
5299 /* Error from catch_errors. */
3a5c3e22 5300 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5301 watchpoint_del_at_next_stop (b);
18a18393
VP
5302 /* We've already printed what needs to be printed. */
5303 bs->print_it = print_it_done;
5304 break;
5305 }
5306 }
5307 else /* must_check_value == 0 */
5308 {
5309 /* This is a case where some watchpoint(s) triggered, but
5310 not at the address of this watchpoint, or else no
5311 watchpoint triggered after all. So don't print
5312 anything for this watchpoint. */
5313 bs->print_it = print_it_noop;
5314 bs->stop = 0;
5315 }
5316 }
5317}
5318
7d4df6a4
DE
5319/* For breakpoints that are currently marked as telling gdb to stop,
5320 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5321 of breakpoint referred to by BS. If we should not stop for this
5322 breakpoint, set BS->stop to 0. */
f431efe5 5323
18a18393
VP
5324static void
5325bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5326{
2bdf28a0
JK
5327 const struct bp_location *bl;
5328 struct breakpoint *b;
7d4df6a4
DE
5329 int value_is_zero = 0;
5330 struct expression *cond;
5331
5332 gdb_assert (bs->stop);
2bdf28a0
JK
5333
5334 /* BS is built for existing struct breakpoint. */
f431efe5 5335 bl = bs->bp_location_at;
2bdf28a0 5336 gdb_assert (bl != NULL);
f431efe5 5337 b = bs->breakpoint_at;
2bdf28a0 5338 gdb_assert (b != NULL);
18a18393 5339
b775012e
LM
5340 /* Even if the target evaluated the condition on its end and notified GDB, we
5341 need to do so again since GDB does not know if we stopped due to a
5342 breakpoint or a single step breakpoint. */
5343
18a18393 5344 if (frame_id_p (b->frame_id)
edb3359d 5345 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5346 {
7d4df6a4
DE
5347 bs->stop = 0;
5348 return;
5349 }
60e1c644 5350
12ab52e9
PA
5351 /* If this is a thread/task-specific breakpoint, don't waste cpu
5352 evaluating the condition if this isn't the specified
5353 thread/task. */
5354 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5355 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5356
6c1b0f7b
DE
5357 {
5358 bs->stop = 0;
5359 return;
5360 }
5361
6dddc817
DE
5362 /* Evaluate extension language breakpoints that have a "stop" method
5363 implemented. */
5364 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5365
7d4df6a4
DE
5366 if (is_watchpoint (b))
5367 {
5368 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5369
7d4df6a4
DE
5370 cond = w->cond_exp;
5371 }
5372 else
5373 cond = bl->cond;
60e1c644 5374
7d4df6a4
DE
5375 if (cond && b->disposition != disp_del_at_next_stop)
5376 {
5377 int within_current_scope = 1;
5378 struct watchpoint * w;
60e1c644 5379
7d4df6a4
DE
5380 /* We use value_mark and value_free_to_mark because it could
5381 be a long time before we return to the command level and
5382 call free_all_values. We can't call free_all_values
5383 because we might be in the middle of evaluating a
5384 function call. */
5385 struct value *mark = value_mark ();
5386
5387 if (is_watchpoint (b))
5388 w = (struct watchpoint *) b;
5389 else
5390 w = NULL;
5391
5392 /* Need to select the frame, with all that implies so that
5393 the conditions will have the right context. Because we
5394 use the frame, we will not see an inlined function's
5395 variables when we arrive at a breakpoint at the start
5396 of the inlined function; the current frame will be the
5397 call site. */
5398 if (w == NULL || w->cond_exp_valid_block == NULL)
5399 select_frame (get_current_frame ());
5400 else
18a18393 5401 {
7d4df6a4
DE
5402 struct frame_info *frame;
5403
5404 /* For local watchpoint expressions, which particular
5405 instance of a local is being watched matters, so we
5406 keep track of the frame to evaluate the expression
5407 in. To evaluate the condition however, it doesn't
5408 really matter which instantiation of the function
5409 where the condition makes sense triggers the
5410 watchpoint. This allows an expression like "watch
5411 global if q > 10" set in `func', catch writes to
5412 global on all threads that call `func', or catch
5413 writes on all recursive calls of `func' by a single
5414 thread. We simply always evaluate the condition in
5415 the innermost frame that's executing where it makes
5416 sense to evaluate the condition. It seems
5417 intuitive. */
5418 frame = block_innermost_frame (w->cond_exp_valid_block);
5419 if (frame != NULL)
5420 select_frame (frame);
5421 else
5422 within_current_scope = 0;
18a18393 5423 }
7d4df6a4
DE
5424 if (within_current_scope)
5425 value_is_zero
5426 = catch_errors (breakpoint_cond_eval, cond,
5427 "Error in testing breakpoint condition:\n",
5428 RETURN_MASK_ALL);
5429 else
18a18393 5430 {
7d4df6a4
DE
5431 warning (_("Watchpoint condition cannot be tested "
5432 "in the current scope"));
5433 /* If we failed to set the right context for this
5434 watchpoint, unconditionally report it. */
5435 value_is_zero = 0;
18a18393 5436 }
7d4df6a4
DE
5437 /* FIXME-someday, should give breakpoint #. */
5438 value_free_to_mark (mark);
18a18393 5439 }
7d4df6a4
DE
5440
5441 if (cond && value_is_zero)
5442 {
5443 bs->stop = 0;
5444 }
7d4df6a4
DE
5445 else if (b->ignore_count > 0)
5446 {
5447 b->ignore_count--;
5448 bs->stop = 0;
5449 /* Increase the hit count even though we don't stop. */
5450 ++(b->hit_count);
5451 observer_notify_breakpoint_modified (b);
5452 }
18a18393
VP
5453}
5454
1cf4d951
PA
5455/* Returns true if we need to track moribund locations of LOC's type
5456 on the current target. */
5457
5458static int
5459need_moribund_for_location_type (struct bp_location *loc)
5460{
5461 return ((loc->loc_type == bp_loc_software_breakpoint
5462 && !target_supports_stopped_by_sw_breakpoint ())
5463 || (loc->loc_type == bp_loc_hardware_breakpoint
5464 && !target_supports_stopped_by_hw_breakpoint ()));
5465}
5466
18a18393 5467
9709f61c 5468/* Get a bpstat associated with having just stopped at address
d983da9c 5469 BP_ADDR in thread PTID.
c906108c 5470
d983da9c 5471 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5472 don't understand this stop. Result is a chain of bpstat's such
5473 that:
c906108c 5474
c5aa993b 5475 if we don't understand the stop, the result is a null pointer.
c906108c 5476
c5aa993b 5477 if we understand why we stopped, the result is not null.
c906108c 5478
c5aa993b
JM
5479 Each element of the chain refers to a particular breakpoint or
5480 watchpoint at which we have stopped. (We may have stopped for
5481 several reasons concurrently.)
c906108c 5482
c5aa993b
JM
5483 Each element of the chain has valid next, breakpoint_at,
5484 commands, FIXME??? fields. */
c906108c
SS
5485
5486bpstat
6c95b8df 5487bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5488 CORE_ADDR bp_addr, ptid_t ptid,
5489 const struct target_waitstatus *ws)
c906108c 5490{
0d381245 5491 struct breakpoint *b = NULL;
afe38095 5492 struct bp_location *bl;
20874c92 5493 struct bp_location *loc;
5760d0ab
JK
5494 /* First item of allocated bpstat's. */
5495 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5496 /* Pointer to the last thing in the chain currently. */
5760d0ab 5497 bpstat bs;
20874c92 5498 int ix;
429374b8 5499 int need_remove_insert;
f431efe5 5500 int removed_any;
c906108c 5501
f431efe5
PA
5502 /* First, build the bpstat chain with locations that explain a
5503 target stop, while being careful to not set the target running,
5504 as that may invalidate locations (in particular watchpoint
5505 locations are recreated). Resuming will happen here with
5506 breakpoint conditions or watchpoint expressions that include
5507 inferior function calls. */
c5aa993b 5508
429374b8
JK
5509 ALL_BREAKPOINTS (b)
5510 {
1a853c52 5511 if (!breakpoint_enabled (b))
429374b8 5512 continue;
a5606eee 5513
429374b8
JK
5514 for (bl = b->loc; bl != NULL; bl = bl->next)
5515 {
4a64f543
MS
5516 /* For hardware watchpoints, we look only at the first
5517 location. The watchpoint_check function will work on the
5518 entire expression, not the individual locations. For
5519 read watchpoints, the watchpoints_triggered function has
5520 checked all locations already. */
429374b8
JK
5521 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5522 break;
18a18393 5523
f6592439 5524 if (!bl->enabled || bl->shlib_disabled)
429374b8 5525 continue;
c5aa993b 5526
09ac7c10 5527 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5528 continue;
c5aa993b 5529
4a64f543
MS
5530 /* Come here if it's a watchpoint, or if the break address
5531 matches. */
c5aa993b 5532
4a64f543
MS
5533 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5534 explain stop. */
c5aa993b 5535
f431efe5
PA
5536 /* Assume we stop. Should we find a watchpoint that is not
5537 actually triggered, or if the condition of the breakpoint
5538 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5539 bs->stop = 1;
5540 bs->print = 1;
d983da9c 5541
f431efe5
PA
5542 /* If this is a scope breakpoint, mark the associated
5543 watchpoint as triggered so that we will handle the
5544 out-of-scope event. We'll get to the watchpoint next
5545 iteration. */
d0fb5eae 5546 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5547 {
5548 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5549
5550 w->watchpoint_triggered = watch_triggered_yes;
5551 }
f431efe5
PA
5552 }
5553 }
5554
7c16b83e 5555 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5556 if (!target_supports_stopped_by_sw_breakpoint ()
5557 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5558 {
1cf4d951 5559 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5560 {
1cf4d951
PA
5561 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5562 && need_moribund_for_location_type (loc))
5563 {
5564 bs = bpstat_alloc (loc, &bs_link);
5565 /* For hits of moribund locations, we should just proceed. */
5566 bs->stop = 0;
5567 bs->print = 0;
5568 bs->print_it = print_it_noop;
5569 }
f431efe5
PA
5570 }
5571 }
5572
edcc5120
TT
5573 /* A bit of special processing for shlib breakpoints. We need to
5574 process solib loading here, so that the lists of loaded and
5575 unloaded libraries are correct before we handle "catch load" and
5576 "catch unload". */
5577 for (bs = bs_head; bs != NULL; bs = bs->next)
5578 {
5d268276 5579 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5580 {
5581 handle_solib_event ();
5582 break;
5583 }
5584 }
5585
f431efe5
PA
5586 /* Now go through the locations that caused the target to stop, and
5587 check whether we're interested in reporting this stop to higher
5588 layers, or whether we should resume the target transparently. */
5589
5590 removed_any = 0;
5591
5760d0ab 5592 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5593 {
5594 if (!bs->stop)
5595 continue;
5596
f431efe5 5597 b = bs->breakpoint_at;
348d480f
PA
5598 b->ops->check_status (bs);
5599 if (bs->stop)
28010a5d 5600 {
348d480f 5601 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5602
429374b8
JK
5603 if (bs->stop)
5604 {
5605 ++(b->hit_count);
8d3788bd 5606 observer_notify_breakpoint_modified (b);
c906108c 5607
4a64f543 5608 /* We will stop here. */
429374b8
JK
5609 if (b->disposition == disp_disable)
5610 {
816338b5 5611 --(b->enable_count);
1a853c52 5612 if (b->enable_count <= 0)
429374b8 5613 b->enable_state = bp_disabled;
f431efe5 5614 removed_any = 1;
429374b8
JK
5615 }
5616 if (b->silent)
5617 bs->print = 0;
5618 bs->commands = b->commands;
9add0f1b 5619 incref_counted_command_line (bs->commands);
abf85f46
JK
5620 if (command_line_is_silent (bs->commands
5621 ? bs->commands->commands : NULL))
5622 bs->print = 0;
9d6e6e84
HZ
5623
5624 b->ops->after_condition_true (bs);
429374b8
JK
5625 }
5626
348d480f 5627 }
a9b3a50f
PA
5628
5629 /* Print nothing for this entry if we don't stop or don't
5630 print. */
5631 if (!bs->stop || !bs->print)
5632 bs->print_it = print_it_noop;
429374b8 5633 }
876fa593 5634
d983da9c
DJ
5635 /* If we aren't stopping, the value of some hardware watchpoint may
5636 not have changed, but the intermediate memory locations we are
5637 watching may have. Don't bother if we're stopping; this will get
5638 done later. */
d832cb68 5639 need_remove_insert = 0;
5760d0ab
JK
5640 if (! bpstat_causes_stop (bs_head))
5641 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5642 if (!bs->stop
f431efe5
PA
5643 && bs->breakpoint_at
5644 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5645 {
3a5c3e22
PA
5646 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5647
5648 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5649 need_remove_insert = 1;
d983da9c
DJ
5650 }
5651
d832cb68 5652 if (need_remove_insert)
44702360 5653 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5654 else if (removed_any)
44702360 5655 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5656
5760d0ab 5657 return bs_head;
c906108c 5658}
628fe4e4
JK
5659
5660static void
5661handle_jit_event (void)
5662{
5663 struct frame_info *frame;
5664 struct gdbarch *gdbarch;
5665
5666 /* Switch terminal for any messages produced by
5667 breakpoint_re_set. */
5668 target_terminal_ours_for_output ();
5669
5670 frame = get_current_frame ();
5671 gdbarch = get_frame_arch (frame);
5672
5673 jit_event_handler (gdbarch);
5674
5675 target_terminal_inferior ();
5676}
5677
5678/* Prepare WHAT final decision for infrun. */
5679
5680/* Decide what infrun needs to do with this bpstat. */
5681
c906108c 5682struct bpstat_what
0e30163f 5683bpstat_what (bpstat bs_head)
c906108c 5684{
c906108c 5685 struct bpstat_what retval;
628fe4e4 5686 int jit_event = 0;
0e30163f 5687 bpstat bs;
c906108c 5688
628fe4e4 5689 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5690 retval.call_dummy = STOP_NONE;
186c406b 5691 retval.is_longjmp = 0;
628fe4e4 5692
0e30163f 5693 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5694 {
628fe4e4
JK
5695 /* Extract this BS's action. After processing each BS, we check
5696 if its action overrides all we've seem so far. */
5697 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5698 enum bptype bptype;
5699
c906108c 5700 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5701 {
5702 /* I suspect this can happen if it was a momentary
5703 breakpoint which has since been deleted. */
5704 bptype = bp_none;
5705 }
20874c92 5706 else
f431efe5 5707 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5708
5709 switch (bptype)
c906108c
SS
5710 {
5711 case bp_none:
628fe4e4 5712 break;
c906108c
SS
5713 case bp_breakpoint:
5714 case bp_hardware_breakpoint:
7c16b83e 5715 case bp_single_step:
c906108c
SS
5716 case bp_until:
5717 case bp_finish:
a9b3a50f 5718 case bp_shlib_event:
c906108c
SS
5719 if (bs->stop)
5720 {
5721 if (bs->print)
628fe4e4 5722 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5723 else
628fe4e4 5724 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5725 }
5726 else
628fe4e4 5727 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5728 break;
5729 case bp_watchpoint:
5730 case bp_hardware_watchpoint:
5731 case bp_read_watchpoint:
5732 case bp_access_watchpoint:
5733 if (bs->stop)
5734 {
5735 if (bs->print)
628fe4e4 5736 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5737 else
628fe4e4 5738 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5739 }
5740 else
628fe4e4
JK
5741 {
5742 /* There was a watchpoint, but we're not stopping.
5743 This requires no further action. */
5744 }
c906108c
SS
5745 break;
5746 case bp_longjmp:
e2e4d78b 5747 case bp_longjmp_call_dummy:
186c406b 5748 case bp_exception:
628fe4e4 5749 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5750 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5751 break;
5752 case bp_longjmp_resume:
186c406b 5753 case bp_exception_resume:
628fe4e4 5754 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5755 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5756 break;
5757 case bp_step_resume:
5758 if (bs->stop)
628fe4e4
JK
5759 this_action = BPSTAT_WHAT_STEP_RESUME;
5760 else
c906108c 5761 {
628fe4e4
JK
5762 /* It is for the wrong frame. */
5763 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5764 }
c906108c 5765 break;
2c03e5be
PA
5766 case bp_hp_step_resume:
5767 if (bs->stop)
5768 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5769 else
5770 {
5771 /* It is for the wrong frame. */
5772 this_action = BPSTAT_WHAT_SINGLE;
5773 }
5774 break;
c906108c 5775 case bp_watchpoint_scope:
c4093a6a 5776 case bp_thread_event:
1900040c 5777 case bp_overlay_event:
0fd8e87f 5778 case bp_longjmp_master:
aa7d318d 5779 case bp_std_terminate_master:
186c406b 5780 case bp_exception_master:
628fe4e4 5781 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5782 break;
ce78b96d 5783 case bp_catchpoint:
c5aa993b
JM
5784 if (bs->stop)
5785 {
5786 if (bs->print)
628fe4e4 5787 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5788 else
628fe4e4 5789 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5790 }
5791 else
628fe4e4
JK
5792 {
5793 /* There was a catchpoint, but we're not stopping.
5794 This requires no further action. */
5795 }
5796 break;
628fe4e4
JK
5797 case bp_jit_event:
5798 jit_event = 1;
5799 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5800 break;
c906108c 5801 case bp_call_dummy:
53a5351d
JM
5802 /* Make sure the action is stop (silent or noisy),
5803 so infrun.c pops the dummy frame. */
aa7d318d 5804 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5805 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5806 break;
5807 case bp_std_terminate:
5808 /* Make sure the action is stop (silent or noisy),
5809 so infrun.c pops the dummy frame. */
aa7d318d 5810 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5811 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5812 break;
1042e4c0 5813 case bp_tracepoint:
7a697b8d 5814 case bp_fast_tracepoint:
0fb4aa4b 5815 case bp_static_tracepoint:
1042e4c0
SS
5816 /* Tracepoint hits should not be reported back to GDB, and
5817 if one got through somehow, it should have been filtered
5818 out already. */
5819 internal_error (__FILE__, __LINE__,
7a697b8d 5820 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5821 break;
5822 case bp_gnu_ifunc_resolver:
5823 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5824 this_action = BPSTAT_WHAT_SINGLE;
5825 break;
5826 case bp_gnu_ifunc_resolver_return:
5827 /* The breakpoint will be removed, execution will restart from the
5828 PC of the former breakpoint. */
5829 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5830 break;
e7e0cddf
SS
5831
5832 case bp_dprintf:
a11cfd87
HZ
5833 if (bs->stop)
5834 this_action = BPSTAT_WHAT_STOP_SILENT;
5835 else
5836 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5837 break;
5838
628fe4e4
JK
5839 default:
5840 internal_error (__FILE__, __LINE__,
5841 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5842 }
628fe4e4
JK
5843
5844 retval.main_action = max (retval.main_action, this_action);
c906108c 5845 }
628fe4e4 5846
0e30163f
JK
5847 /* These operations may affect the bs->breakpoint_at state so they are
5848 delayed after MAIN_ACTION is decided above. */
5849
628fe4e4
JK
5850 if (jit_event)
5851 {
5852 if (debug_infrun)
5853 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5854
5855 handle_jit_event ();
5856 }
5857
0e30163f
JK
5858 for (bs = bs_head; bs != NULL; bs = bs->next)
5859 {
5860 struct breakpoint *b = bs->breakpoint_at;
5861
5862 if (b == NULL)
5863 continue;
5864 switch (b->type)
5865 {
5866 case bp_gnu_ifunc_resolver:
5867 gnu_ifunc_resolver_stop (b);
5868 break;
5869 case bp_gnu_ifunc_resolver_return:
5870 gnu_ifunc_resolver_return_stop (b);
5871 break;
5872 }
5873 }
5874
c906108c
SS
5875 return retval;
5876}
5877
5878/* Nonzero if we should step constantly (e.g. watchpoints on machines
5879 without hardware support). This isn't related to a specific bpstat,
5880 just to things like whether watchpoints are set. */
5881
c5aa993b 5882int
fba45db2 5883bpstat_should_step (void)
c906108c
SS
5884{
5885 struct breakpoint *b;
cc59ec59 5886
c906108c 5887 ALL_BREAKPOINTS (b)
717a8278 5888 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5889 return 1;
c906108c
SS
5890 return 0;
5891}
5892
67822962
PA
5893int
5894bpstat_causes_stop (bpstat bs)
5895{
5896 for (; bs != NULL; bs = bs->next)
5897 if (bs->stop)
5898 return 1;
5899
5900 return 0;
5901}
5902
c906108c 5903\f
c5aa993b 5904
170b53b2
UW
5905/* Compute a string of spaces suitable to indent the next line
5906 so it starts at the position corresponding to the table column
5907 named COL_NAME in the currently active table of UIOUT. */
5908
5909static char *
5910wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5911{
5912 static char wrap_indent[80];
5913 int i, total_width, width, align;
5914 char *text;
5915
5916 total_width = 0;
5917 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5918 {
5919 if (strcmp (text, col_name) == 0)
5920 {
5921 gdb_assert (total_width < sizeof wrap_indent);
5922 memset (wrap_indent, ' ', total_width);
5923 wrap_indent[total_width] = 0;
5924
5925 return wrap_indent;
5926 }
5927
5928 total_width += width + 1;
5929 }
5930
5931 return NULL;
5932}
5933
b775012e
LM
5934/* Determine if the locations of this breakpoint will have their conditions
5935 evaluated by the target, host or a mix of both. Returns the following:
5936
5937 "host": Host evals condition.
5938 "host or target": Host or Target evals condition.
5939 "target": Target evals condition.
5940*/
5941
5942static const char *
5943bp_condition_evaluator (struct breakpoint *b)
5944{
5945 struct bp_location *bl;
5946 char host_evals = 0;
5947 char target_evals = 0;
5948
5949 if (!b)
5950 return NULL;
5951
5952 if (!is_breakpoint (b))
5953 return NULL;
5954
5955 if (gdb_evaluates_breakpoint_condition_p ()
5956 || !target_supports_evaluation_of_breakpoint_conditions ())
5957 return condition_evaluation_host;
5958
5959 for (bl = b->loc; bl; bl = bl->next)
5960 {
5961 if (bl->cond_bytecode)
5962 target_evals++;
5963 else
5964 host_evals++;
5965 }
5966
5967 if (host_evals && target_evals)
5968 return condition_evaluation_both;
5969 else if (target_evals)
5970 return condition_evaluation_target;
5971 else
5972 return condition_evaluation_host;
5973}
5974
5975/* Determine the breakpoint location's condition evaluator. This is
5976 similar to bp_condition_evaluator, but for locations. */
5977
5978static const char *
5979bp_location_condition_evaluator (struct bp_location *bl)
5980{
5981 if (bl && !is_breakpoint (bl->owner))
5982 return NULL;
5983
5984 if (gdb_evaluates_breakpoint_condition_p ()
5985 || !target_supports_evaluation_of_breakpoint_conditions ())
5986 return condition_evaluation_host;
5987
5988 if (bl && bl->cond_bytecode)
5989 return condition_evaluation_target;
5990 else
5991 return condition_evaluation_host;
5992}
5993
859825b8
JK
5994/* Print the LOC location out of the list of B->LOC locations. */
5995
170b53b2
UW
5996static void
5997print_breakpoint_location (struct breakpoint *b,
5998 struct bp_location *loc)
0d381245 5999{
79a45e25 6000 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6001 struct cleanup *old_chain = save_current_program_space ();
6002
859825b8
JK
6003 if (loc != NULL && loc->shlib_disabled)
6004 loc = NULL;
6005
6c95b8df
PA
6006 if (loc != NULL)
6007 set_current_program_space (loc->pspace);
6008
56435ebe
TT
6009 if (b->display_canonical)
6010 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 6011 else if (loc && loc->symtab)
0d381245
VP
6012 {
6013 struct symbol *sym
6014 = find_pc_sect_function (loc->address, loc->section);
6015 if (sym)
6016 {
6017 ui_out_text (uiout, "in ");
6018 ui_out_field_string (uiout, "func",
6019 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6020 ui_out_text (uiout, " ");
6021 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6022 ui_out_text (uiout, "at ");
0d381245 6023 }
05cba821
JK
6024 ui_out_field_string (uiout, "file",
6025 symtab_to_filename_for_display (loc->symtab));
0d381245 6026 ui_out_text (uiout, ":");
05cba821 6027
0d381245 6028 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6029 ui_out_field_string (uiout, "fullname",
6030 symtab_to_fullname (loc->symtab));
0d381245 6031
f8eba3c6 6032 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6033 }
859825b8 6034 else if (loc)
0d381245 6035 {
f99d8bf4
PA
6036 struct ui_file *stb = mem_fileopen ();
6037 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6038
f99d8bf4 6039 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6040 demangle, "");
0d381245 6041 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6042
6043 do_cleanups (stb_chain);
0d381245 6044 }
859825b8
JK
6045 else
6046 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 6047
b775012e
LM
6048 if (loc && is_breakpoint (b)
6049 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6050 && bp_condition_evaluator (b) == condition_evaluation_both)
6051 {
6052 ui_out_text (uiout, " (");
6053 ui_out_field_string (uiout, "evaluated-by",
6054 bp_location_condition_evaluator (loc));
6055 ui_out_text (uiout, ")");
6056 }
6057
6c95b8df 6058 do_cleanups (old_chain);
0d381245
VP
6059}
6060
269b11a2
PA
6061static const char *
6062bptype_string (enum bptype type)
c906108c 6063{
c4093a6a
JM
6064 struct ep_type_description
6065 {
6066 enum bptype type;
6067 char *description;
6068 };
6069 static struct ep_type_description bptypes[] =
c906108c 6070 {
c5aa993b
JM
6071 {bp_none, "?deleted?"},
6072 {bp_breakpoint, "breakpoint"},
c906108c 6073 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6074 {bp_single_step, "sw single-step"},
c5aa993b
JM
6075 {bp_until, "until"},
6076 {bp_finish, "finish"},
6077 {bp_watchpoint, "watchpoint"},
c906108c 6078 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6079 {bp_read_watchpoint, "read watchpoint"},
6080 {bp_access_watchpoint, "acc watchpoint"},
6081 {bp_longjmp, "longjmp"},
6082 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6083 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6084 {bp_exception, "exception"},
6085 {bp_exception_resume, "exception resume"},
c5aa993b 6086 {bp_step_resume, "step resume"},
2c03e5be 6087 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6088 {bp_watchpoint_scope, "watchpoint scope"},
6089 {bp_call_dummy, "call dummy"},
aa7d318d 6090 {bp_std_terminate, "std::terminate"},
c5aa993b 6091 {bp_shlib_event, "shlib events"},
c4093a6a 6092 {bp_thread_event, "thread events"},
1900040c 6093 {bp_overlay_event, "overlay events"},
0fd8e87f 6094 {bp_longjmp_master, "longjmp master"},
aa7d318d 6095 {bp_std_terminate_master, "std::terminate master"},
186c406b 6096 {bp_exception_master, "exception master"},
ce78b96d 6097 {bp_catchpoint, "catchpoint"},
1042e4c0 6098 {bp_tracepoint, "tracepoint"},
7a697b8d 6099 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6100 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6101 {bp_dprintf, "dprintf"},
4efc6507 6102 {bp_jit_event, "jit events"},
0e30163f
JK
6103 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6104 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6105 };
269b11a2
PA
6106
6107 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6108 || ((int) type != bptypes[(int) type].type))
6109 internal_error (__FILE__, __LINE__,
6110 _("bptypes table does not describe type #%d."),
6111 (int) type);
6112
6113 return bptypes[(int) type].description;
6114}
6115
998580f1
MK
6116/* For MI, output a field named 'thread-groups' with a list as the value.
6117 For CLI, prefix the list with the string 'inf'. */
6118
6119static void
6120output_thread_groups (struct ui_out *uiout,
6121 const char *field_name,
6122 VEC(int) *inf_num,
6123 int mi_only)
6124{
752eb8b4 6125 struct cleanup *back_to;
998580f1
MK
6126 int is_mi = ui_out_is_mi_like_p (uiout);
6127 int inf;
6128 int i;
6129
6130 /* For backward compatibility, don't display inferiors in CLI unless
6131 there are several. Always display them for MI. */
6132 if (!is_mi && mi_only)
6133 return;
6134
752eb8b4
TT
6135 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6136
998580f1
MK
6137 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6138 {
6139 if (is_mi)
6140 {
6141 char mi_group[10];
6142
6143 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6144 ui_out_field_string (uiout, NULL, mi_group);
6145 }
6146 else
6147 {
6148 if (i == 0)
6149 ui_out_text (uiout, " inf ");
6150 else
6151 ui_out_text (uiout, ", ");
6152
6153 ui_out_text (uiout, plongest (inf));
6154 }
6155 }
6156
6157 do_cleanups (back_to);
6158}
6159
269b11a2
PA
6160/* Print B to gdb_stdout. */
6161
6162static void
6163print_one_breakpoint_location (struct breakpoint *b,
6164 struct bp_location *loc,
6165 int loc_number,
6166 struct bp_location **last_loc,
269b11a2
PA
6167 int allflag)
6168{
6169 struct command_line *l;
c2c6d25f 6170 static char bpenables[] = "nynny";
c906108c 6171
79a45e25 6172 struct ui_out *uiout = current_uiout;
0d381245
VP
6173 int header_of_multiple = 0;
6174 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6175 struct value_print_options opts;
6176
6177 get_user_print_options (&opts);
0d381245
VP
6178
6179 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6180 /* See comment in print_one_breakpoint concerning treatment of
6181 breakpoints with single disabled location. */
0d381245
VP
6182 if (loc == NULL
6183 && (b->loc != NULL
6184 && (b->loc->next != NULL || !b->loc->enabled)))
6185 header_of_multiple = 1;
6186 if (loc == NULL)
6187 loc = b->loc;
6188
c4093a6a
JM
6189 annotate_record ();
6190
6191 /* 1 */
6192 annotate_field (0);
0d381245
VP
6193 if (part_of_multiple)
6194 {
6195 char *formatted;
0c6773c1 6196 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6197 ui_out_field_string (uiout, "number", formatted);
6198 xfree (formatted);
6199 }
6200 else
6201 {
6202 ui_out_field_int (uiout, "number", b->number);
6203 }
c4093a6a
JM
6204
6205 /* 2 */
6206 annotate_field (1);
0d381245
VP
6207 if (part_of_multiple)
6208 ui_out_field_skip (uiout, "type");
269b11a2
PA
6209 else
6210 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6211
6212 /* 3 */
6213 annotate_field (2);
0d381245
VP
6214 if (part_of_multiple)
6215 ui_out_field_skip (uiout, "disp");
6216 else
2cec12e5 6217 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6218
c4093a6a
JM
6219
6220 /* 4 */
6221 annotate_field (3);
0d381245 6222 if (part_of_multiple)
54e52265 6223 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6224 else
4a64f543
MS
6225 ui_out_field_fmt (uiout, "enabled", "%c",
6226 bpenables[(int) b->enable_state]);
54e52265 6227 ui_out_spaces (uiout, 2);
0d381245 6228
c4093a6a
JM
6229
6230 /* 5 and 6 */
3086aeae 6231 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6232 {
4a64f543
MS
6233 /* Although the print_one can possibly print all locations,
6234 calling it here is not likely to get any nice result. So,
6235 make sure there's just one location. */
0d381245 6236 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6237 b->ops->print_one (b, last_loc);
0d381245 6238 }
3086aeae
DJ
6239 else
6240 switch (b->type)
6241 {
6242 case bp_none:
6243 internal_error (__FILE__, __LINE__,
e2e0b3e5 6244 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6245 break;
c906108c 6246
3086aeae
DJ
6247 case bp_watchpoint:
6248 case bp_hardware_watchpoint:
6249 case bp_read_watchpoint:
6250 case bp_access_watchpoint:
3a5c3e22
PA
6251 {
6252 struct watchpoint *w = (struct watchpoint *) b;
6253
6254 /* Field 4, the address, is omitted (which makes the columns
6255 not line up too nicely with the headers, but the effect
6256 is relatively readable). */
6257 if (opts.addressprint)
6258 ui_out_field_skip (uiout, "addr");
6259 annotate_field (5);
6260 ui_out_field_string (uiout, "what", w->exp_string);
6261 }
3086aeae
DJ
6262 break;
6263
3086aeae
DJ
6264 case bp_breakpoint:
6265 case bp_hardware_breakpoint:
7c16b83e 6266 case bp_single_step:
3086aeae
DJ
6267 case bp_until:
6268 case bp_finish:
6269 case bp_longjmp:
6270 case bp_longjmp_resume:
e2e4d78b 6271 case bp_longjmp_call_dummy:
186c406b
TT
6272 case bp_exception:
6273 case bp_exception_resume:
3086aeae 6274 case bp_step_resume:
2c03e5be 6275 case bp_hp_step_resume:
3086aeae
DJ
6276 case bp_watchpoint_scope:
6277 case bp_call_dummy:
aa7d318d 6278 case bp_std_terminate:
3086aeae
DJ
6279 case bp_shlib_event:
6280 case bp_thread_event:
6281 case bp_overlay_event:
0fd8e87f 6282 case bp_longjmp_master:
aa7d318d 6283 case bp_std_terminate_master:
186c406b 6284 case bp_exception_master:
1042e4c0 6285 case bp_tracepoint:
7a697b8d 6286 case bp_fast_tracepoint:
0fb4aa4b 6287 case bp_static_tracepoint:
e7e0cddf 6288 case bp_dprintf:
4efc6507 6289 case bp_jit_event:
0e30163f
JK
6290 case bp_gnu_ifunc_resolver:
6291 case bp_gnu_ifunc_resolver_return:
79a45b7d 6292 if (opts.addressprint)
3086aeae
DJ
6293 {
6294 annotate_field (4);
54e52265 6295 if (header_of_multiple)
0d381245 6296 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6297 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6298 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6299 else
5af949e3
UW
6300 ui_out_field_core_addr (uiout, "addr",
6301 loc->gdbarch, loc->address);
3086aeae
DJ
6302 }
6303 annotate_field (5);
0d381245 6304 if (!header_of_multiple)
170b53b2 6305 print_breakpoint_location (b, loc);
0d381245 6306 if (b->loc)
a6d9a66e 6307 *last_loc = b->loc;
3086aeae
DJ
6308 break;
6309 }
c906108c 6310
6c95b8df 6311
998580f1 6312 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6313 {
6314 struct inferior *inf;
998580f1
MK
6315 VEC(int) *inf_num = NULL;
6316 int mi_only = 1;
6c95b8df 6317
998580f1 6318 ALL_INFERIORS (inf)
6c95b8df
PA
6319 {
6320 if (inf->pspace == loc->pspace)
998580f1 6321 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6322 }
998580f1
MK
6323
6324 /* For backward compatibility, don't display inferiors in CLI unless
6325 there are several. Always display for MI. */
6326 if (allflag
6327 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6328 && (number_of_program_spaces () > 1
6329 || number_of_inferiors () > 1)
6330 /* LOC is for existing B, it cannot be in
6331 moribund_locations and thus having NULL OWNER. */
6332 && loc->owner->type != bp_catchpoint))
6333 mi_only = 0;
6334 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6335 VEC_free (int, inf_num);
6c95b8df
PA
6336 }
6337
4a306c9a 6338 if (!part_of_multiple)
c4093a6a 6339 {
4a306c9a
JB
6340 if (b->thread != -1)
6341 {
6342 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6343 "stop only in" line a little further down. */
4a306c9a
JB
6344 ui_out_text (uiout, " thread ");
6345 ui_out_field_int (uiout, "thread", b->thread);
6346 }
6347 else if (b->task != 0)
6348 {
6349 ui_out_text (uiout, " task ");
6350 ui_out_field_int (uiout, "task", b->task);
6351 }
c4093a6a 6352 }
f1310107 6353
8b93c638 6354 ui_out_text (uiout, "\n");
f1310107 6355
348d480f 6356 if (!part_of_multiple)
f1310107
TJB
6357 b->ops->print_one_detail (b, uiout);
6358
0d381245 6359 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6360 {
6361 annotate_field (6);
8b93c638 6362 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6363 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6364 the frame ID. */
5af949e3
UW
6365 ui_out_field_core_addr (uiout, "frame",
6366 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6367 ui_out_text (uiout, "\n");
c4093a6a
JM
6368 }
6369
28010a5d 6370 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6371 {
6372 annotate_field (7);
d77f58be 6373 if (is_tracepoint (b))
1042e4c0
SS
6374 ui_out_text (uiout, "\ttrace only if ");
6375 else
6376 ui_out_text (uiout, "\tstop only if ");
0101ce28 6377 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6378
6379 /* Print whether the target is doing the breakpoint's condition
6380 evaluation. If GDB is doing the evaluation, don't print anything. */
6381 if (is_breakpoint (b)
6382 && breakpoint_condition_evaluation_mode ()
6383 == condition_evaluation_target)
6384 {
6385 ui_out_text (uiout, " (");
6386 ui_out_field_string (uiout, "evaluated-by",
6387 bp_condition_evaluator (b));
6388 ui_out_text (uiout, " evals)");
6389 }
0101ce28
JJ
6390 ui_out_text (uiout, "\n");
6391 }
6392
0d381245 6393 if (!part_of_multiple && b->thread != -1)
c4093a6a 6394 {
4a64f543 6395 /* FIXME should make an annotation for this. */
8b93c638
JM
6396 ui_out_text (uiout, "\tstop only in thread ");
6397 ui_out_field_int (uiout, "thread", b->thread);
6398 ui_out_text (uiout, "\n");
c4093a6a
JM
6399 }
6400
556ec64d
YQ
6401 if (!part_of_multiple)
6402 {
6403 if (b->hit_count)
31f56a27
YQ
6404 {
6405 /* FIXME should make an annotation for this. */
6406 if (is_catchpoint (b))
6407 ui_out_text (uiout, "\tcatchpoint");
6408 else if (is_tracepoint (b))
6409 ui_out_text (uiout, "\ttracepoint");
6410 else
6411 ui_out_text (uiout, "\tbreakpoint");
6412 ui_out_text (uiout, " already hit ");
6413 ui_out_field_int (uiout, "times", b->hit_count);
6414 if (b->hit_count == 1)
6415 ui_out_text (uiout, " time\n");
6416 else
6417 ui_out_text (uiout, " times\n");
6418 }
556ec64d
YQ
6419 else
6420 {
31f56a27
YQ
6421 /* Output the count also if it is zero, but only if this is mi. */
6422 if (ui_out_is_mi_like_p (uiout))
6423 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6424 }
6425 }
8b93c638 6426
0d381245 6427 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6428 {
6429 annotate_field (8);
8b93c638
JM
6430 ui_out_text (uiout, "\tignore next ");
6431 ui_out_field_int (uiout, "ignore", b->ignore_count);
6432 ui_out_text (uiout, " hits\n");
c4093a6a 6433 }
059fb39f 6434
816338b5
SS
6435 /* Note that an enable count of 1 corresponds to "enable once"
6436 behavior, which is reported by the combination of enablement and
6437 disposition, so we don't need to mention it here. */
6438 if (!part_of_multiple && b->enable_count > 1)
6439 {
6440 annotate_field (8);
6441 ui_out_text (uiout, "\tdisable after ");
6442 /* Tweak the wording to clarify that ignore and enable counts
6443 are distinct, and have additive effect. */
6444 if (b->ignore_count)
6445 ui_out_text (uiout, "additional ");
6446 else
6447 ui_out_text (uiout, "next ");
6448 ui_out_field_int (uiout, "enable", b->enable_count);
6449 ui_out_text (uiout, " hits\n");
6450 }
6451
f196051f
SS
6452 if (!part_of_multiple && is_tracepoint (b))
6453 {
6454 struct tracepoint *tp = (struct tracepoint *) b;
6455
6456 if (tp->traceframe_usage)
6457 {
6458 ui_out_text (uiout, "\ttrace buffer usage ");
6459 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6460 ui_out_text (uiout, " bytes\n");
6461 }
6462 }
d3ce09f5 6463
9add0f1b 6464 l = b->commands ? b->commands->commands : NULL;
059fb39f 6465 if (!part_of_multiple && l)
c4093a6a 6466 {
3b31d625
EZ
6467 struct cleanup *script_chain;
6468
c4093a6a 6469 annotate_field (9);
3b31d625 6470 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6471 print_command_lines (uiout, l, 4);
3b31d625 6472 do_cleanups (script_chain);
c4093a6a 6473 }
d24317b4 6474
d9b3f62e 6475 if (is_tracepoint (b))
1042e4c0 6476 {
d9b3f62e
PA
6477 struct tracepoint *t = (struct tracepoint *) b;
6478
6479 if (!part_of_multiple && t->pass_count)
6480 {
6481 annotate_field (10);
6482 ui_out_text (uiout, "\tpass count ");
6483 ui_out_field_int (uiout, "pass", t->pass_count);
6484 ui_out_text (uiout, " \n");
6485 }
f2a8bc8a
YQ
6486
6487 /* Don't display it when tracepoint or tracepoint location is
6488 pending. */
6489 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6490 {
6491 annotate_field (11);
6492
6493 if (ui_out_is_mi_like_p (uiout))
6494 ui_out_field_string (uiout, "installed",
6495 loc->inserted ? "y" : "n");
6496 else
6497 {
6498 if (loc->inserted)
6499 ui_out_text (uiout, "\t");
6500 else
6501 ui_out_text (uiout, "\tnot ");
6502 ui_out_text (uiout, "installed on target\n");
6503 }
6504 }
1042e4c0
SS
6505 }
6506
d24317b4
VP
6507 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6508 {
3a5c3e22
PA
6509 if (is_watchpoint (b))
6510 {
6511 struct watchpoint *w = (struct watchpoint *) b;
6512
6513 ui_out_field_string (uiout, "original-location", w->exp_string);
6514 }
6515 else if (b->addr_string)
d24317b4 6516 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6517 }
c4093a6a 6518}
c5aa993b 6519
0d381245
VP
6520static void
6521print_one_breakpoint (struct breakpoint *b,
4a64f543 6522 struct bp_location **last_loc,
6c95b8df 6523 int allflag)
0d381245 6524{
8d3788bd 6525 struct cleanup *bkpt_chain;
79a45e25 6526 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6527
6528 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6529
12c5a436 6530 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6531 do_cleanups (bkpt_chain);
0d381245
VP
6532
6533 /* If this breakpoint has custom print function,
6534 it's already printed. Otherwise, print individual
6535 locations, if any. */
6536 if (b->ops == NULL || b->ops->print_one == NULL)
6537 {
4a64f543
MS
6538 /* If breakpoint has a single location that is disabled, we
6539 print it as if it had several locations, since otherwise it's
6540 hard to represent "breakpoint enabled, location disabled"
6541 situation.
6542
6543 Note that while hardware watchpoints have several locations
a3be7890 6544 internally, that's not a property exposed to user. */
0d381245 6545 if (b->loc
a5606eee 6546 && !is_hardware_watchpoint (b)
8d3788bd 6547 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6548 {
6549 struct bp_location *loc;
6550 int n = 1;
8d3788bd 6551
0d381245 6552 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6553 {
6554 struct cleanup *inner2 =
6555 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6556 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6557 do_cleanups (inner2);
6558 }
0d381245
VP
6559 }
6560 }
6561}
6562
a6d9a66e
UW
6563static int
6564breakpoint_address_bits (struct breakpoint *b)
6565{
6566 int print_address_bits = 0;
6567 struct bp_location *loc;
6568
6569 for (loc = b->loc; loc; loc = loc->next)
6570 {
c7437ca6
PA
6571 int addr_bit;
6572
6573 /* Software watchpoints that aren't watching memory don't have
6574 an address to print. */
6575 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6576 continue;
6577
6578 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6579 if (addr_bit > print_address_bits)
6580 print_address_bits = addr_bit;
6581 }
6582
6583 return print_address_bits;
6584}
0d381245 6585
c4093a6a
JM
6586struct captured_breakpoint_query_args
6587 {
6588 int bnum;
6589 };
c5aa993b 6590
c4093a6a 6591static int
2b65245e 6592do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6593{
6594 struct captured_breakpoint_query_args *args = data;
52f0bd74 6595 struct breakpoint *b;
a6d9a66e 6596 struct bp_location *dummy_loc = NULL;
cc59ec59 6597
c4093a6a
JM
6598 ALL_BREAKPOINTS (b)
6599 {
6600 if (args->bnum == b->number)
c5aa993b 6601 {
12c5a436 6602 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6603 return GDB_RC_OK;
c5aa993b 6604 }
c4093a6a
JM
6605 }
6606 return GDB_RC_NONE;
6607}
c5aa993b 6608
c4093a6a 6609enum gdb_rc
4a64f543
MS
6610gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6611 char **error_message)
c4093a6a
JM
6612{
6613 struct captured_breakpoint_query_args args;
cc59ec59 6614
c4093a6a
JM
6615 args.bnum = bnum;
6616 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6617 an error. */
b0b13bb4
DJ
6618 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6619 error_message, RETURN_MASK_ALL) < 0)
6620 return GDB_RC_FAIL;
6621 else
6622 return GDB_RC_OK;
c4093a6a 6623}
c5aa993b 6624
09d682a4
TT
6625/* Return true if this breakpoint was set by the user, false if it is
6626 internal or momentary. */
6627
6628int
6629user_breakpoint_p (struct breakpoint *b)
6630{
46c6471b 6631 return b->number > 0;
09d682a4
TT
6632}
6633
7f3b0473 6634/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6635 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6636 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6637 FILTER is non-NULL, call it on each breakpoint and only include the
6638 ones for which it returns non-zero. Return the total number of
6639 breakpoints listed. */
c906108c 6640
d77f58be 6641static int
e5a67952 6642breakpoint_1 (char *args, int allflag,
4a64f543 6643 int (*filter) (const struct breakpoint *))
c4093a6a 6644{
52f0bd74 6645 struct breakpoint *b;
a6d9a66e 6646 struct bp_location *last_loc = NULL;
7f3b0473 6647 int nr_printable_breakpoints;
3b31d625 6648 struct cleanup *bkpttbl_chain;
79a45b7d 6649 struct value_print_options opts;
a6d9a66e 6650 int print_address_bits = 0;
269b11a2 6651 int print_type_col_width = 14;
79a45e25 6652 struct ui_out *uiout = current_uiout;
269b11a2 6653
79a45b7d
TT
6654 get_user_print_options (&opts);
6655
4a64f543
MS
6656 /* Compute the number of rows in the table, as well as the size
6657 required for address fields. */
7f3b0473
AC
6658 nr_printable_breakpoints = 0;
6659 ALL_BREAKPOINTS (b)
e5a67952
MS
6660 {
6661 /* If we have a filter, only list the breakpoints it accepts. */
6662 if (filter && !filter (b))
6663 continue;
6664
6665 /* If we have an "args" string, it is a list of breakpoints to
6666 accept. Skip the others. */
6667 if (args != NULL && *args != '\0')
6668 {
6669 if (allflag && parse_and_eval_long (args) != b->number)
6670 continue;
6671 if (!allflag && !number_is_in_list (args, b->number))
6672 continue;
6673 }
269b11a2 6674
e5a67952
MS
6675 if (allflag || user_breakpoint_p (b))
6676 {
6677 int addr_bit, type_len;
a6d9a66e 6678
e5a67952
MS
6679 addr_bit = breakpoint_address_bits (b);
6680 if (addr_bit > print_address_bits)
6681 print_address_bits = addr_bit;
269b11a2 6682
e5a67952
MS
6683 type_len = strlen (bptype_string (b->type));
6684 if (type_len > print_type_col_width)
6685 print_type_col_width = type_len;
6686
6687 nr_printable_breakpoints++;
6688 }
6689 }
7f3b0473 6690
79a45b7d 6691 if (opts.addressprint)
3b31d625 6692 bkpttbl_chain
3e43a32a
MS
6693 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6694 nr_printable_breakpoints,
3b31d625 6695 "BreakpointTable");
8b93c638 6696 else
3b31d625 6697 bkpttbl_chain
3e43a32a
MS
6698 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6699 nr_printable_breakpoints,
3b31d625 6700 "BreakpointTable");
8b93c638 6701
7f3b0473 6702 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6703 annotate_breakpoints_headers ();
6704 if (nr_printable_breakpoints > 0)
6705 annotate_field (0);
4a64f543 6706 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6707 if (nr_printable_breakpoints > 0)
6708 annotate_field (1);
269b11a2 6709 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6710 "type", "Type"); /* 2 */
d7faa9e7
AC
6711 if (nr_printable_breakpoints > 0)
6712 annotate_field (2);
4a64f543 6713 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6714 if (nr_printable_breakpoints > 0)
6715 annotate_field (3);
54e52265 6716 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6717 if (opts.addressprint)
e5a67952
MS
6718 {
6719 if (nr_printable_breakpoints > 0)
6720 annotate_field (4);
6721 if (print_address_bits <= 32)
6722 ui_out_table_header (uiout, 10, ui_left,
6723 "addr", "Address"); /* 5 */
6724 else
6725 ui_out_table_header (uiout, 18, ui_left,
6726 "addr", "Address"); /* 5 */
6727 }
d7faa9e7
AC
6728 if (nr_printable_breakpoints > 0)
6729 annotate_field (5);
6730 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6731 ui_out_table_body (uiout);
6732 if (nr_printable_breakpoints > 0)
6733 annotate_breakpoints_table ();
7f3b0473 6734
c4093a6a 6735 ALL_BREAKPOINTS (b)
e5a67952
MS
6736 {
6737 QUIT;
6738 /* If we have a filter, only list the breakpoints it accepts. */
6739 if (filter && !filter (b))
6740 continue;
6741
6742 /* If we have an "args" string, it is a list of breakpoints to
6743 accept. Skip the others. */
6744
6745 if (args != NULL && *args != '\0')
6746 {
6747 if (allflag) /* maintenance info breakpoint */
6748 {
6749 if (parse_and_eval_long (args) != b->number)
6750 continue;
6751 }
6752 else /* all others */
6753 {
6754 if (!number_is_in_list (args, b->number))
6755 continue;
6756 }
6757 }
6758 /* We only print out user settable breakpoints unless the
6759 allflag is set. */
6760 if (allflag || user_breakpoint_p (b))
12c5a436 6761 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6762 }
6763
3b31d625 6764 do_cleanups (bkpttbl_chain);
698384cd 6765
7f3b0473 6766 if (nr_printable_breakpoints == 0)
c906108c 6767 {
4a64f543
MS
6768 /* If there's a filter, let the caller decide how to report
6769 empty list. */
d77f58be
SS
6770 if (!filter)
6771 {
e5a67952 6772 if (args == NULL || *args == '\0')
d77f58be
SS
6773 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6774 else
4a64f543 6775 ui_out_message (uiout, 0,
e5a67952
MS
6776 "No breakpoint or watchpoint matching '%s'.\n",
6777 args);
d77f58be 6778 }
c906108c
SS
6779 }
6780 else
c4093a6a 6781 {
a6d9a66e
UW
6782 if (last_loc && !server_command)
6783 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6784 }
c906108c 6785
4a64f543 6786 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6787 there have been breakpoints? */
c906108c 6788 annotate_breakpoints_table_end ();
d77f58be
SS
6789
6790 return nr_printable_breakpoints;
c906108c
SS
6791}
6792
ad443146
SS
6793/* Display the value of default-collect in a way that is generally
6794 compatible with the breakpoint list. */
6795
6796static void
6797default_collect_info (void)
6798{
79a45e25
PA
6799 struct ui_out *uiout = current_uiout;
6800
ad443146
SS
6801 /* If it has no value (which is frequently the case), say nothing; a
6802 message like "No default-collect." gets in user's face when it's
6803 not wanted. */
6804 if (!*default_collect)
6805 return;
6806
6807 /* The following phrase lines up nicely with per-tracepoint collect
6808 actions. */
6809 ui_out_text (uiout, "default collect ");
6810 ui_out_field_string (uiout, "default-collect", default_collect);
6811 ui_out_text (uiout, " \n");
6812}
6813
c906108c 6814static void
e5a67952 6815breakpoints_info (char *args, int from_tty)
c906108c 6816{
e5a67952 6817 breakpoint_1 (args, 0, NULL);
ad443146
SS
6818
6819 default_collect_info ();
d77f58be
SS
6820}
6821
6822static void
e5a67952 6823watchpoints_info (char *args, int from_tty)
d77f58be 6824{
e5a67952 6825 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6826 struct ui_out *uiout = current_uiout;
d77f58be
SS
6827
6828 if (num_printed == 0)
6829 {
e5a67952 6830 if (args == NULL || *args == '\0')
d77f58be
SS
6831 ui_out_message (uiout, 0, "No watchpoints.\n");
6832 else
e5a67952 6833 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6834 }
c906108c
SS
6835}
6836
7a292a7a 6837static void
e5a67952 6838maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6839{
e5a67952 6840 breakpoint_1 (args, 1, NULL);
ad443146
SS
6841
6842 default_collect_info ();
c906108c
SS
6843}
6844
0d381245 6845static int
714835d5 6846breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6847 struct program_space *pspace,
714835d5 6848 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6849{
6850 struct bp_location *bl = b->loc;
cc59ec59 6851
0d381245
VP
6852 for (; bl; bl = bl->next)
6853 {
6c95b8df
PA
6854 if (bl->pspace == pspace
6855 && bl->address == pc
0d381245
VP
6856 && (!overlay_debugging || bl->section == section))
6857 return 1;
6858 }
6859 return 0;
6860}
6861
672f9b60 6862/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6863 concerns with logical breakpoints, so we match program spaces, not
6864 address spaces. */
c906108c
SS
6865
6866static void
6c95b8df
PA
6867describe_other_breakpoints (struct gdbarch *gdbarch,
6868 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6869 struct obj_section *section, int thread)
c906108c 6870{
52f0bd74
AC
6871 int others = 0;
6872 struct breakpoint *b;
c906108c
SS
6873
6874 ALL_BREAKPOINTS (b)
672f9b60
KP
6875 others += (user_breakpoint_p (b)
6876 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6877 if (others > 0)
6878 {
a3f17187
AC
6879 if (others == 1)
6880 printf_filtered (_("Note: breakpoint "));
6881 else /* if (others == ???) */
6882 printf_filtered (_("Note: breakpoints "));
c906108c 6883 ALL_BREAKPOINTS (b)
672f9b60 6884 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6885 {
6886 others--;
6887 printf_filtered ("%d", b->number);
6888 if (b->thread == -1 && thread != -1)
6889 printf_filtered (" (all threads)");
6890 else if (b->thread != -1)
6891 printf_filtered (" (thread %d)", b->thread);
6892 printf_filtered ("%s%s ",
059fb39f 6893 ((b->enable_state == bp_disabled
f8eba3c6 6894 || b->enable_state == bp_call_disabled)
0d381245 6895 ? " (disabled)"
0d381245
VP
6896 : ""),
6897 (others > 1) ? ","
6898 : ((others == 1) ? " and" : ""));
6899 }
a3f17187 6900 printf_filtered (_("also set at pc "));
5af949e3 6901 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6902 printf_filtered (".\n");
6903 }
6904}
6905\f
c906108c 6906
e4f237da
KB
6907/* Return true iff it is meaningful to use the address member of
6908 BPT. For some breakpoint types, the address member is irrelevant
6909 and it makes no sense to attempt to compare it to other addresses
6910 (or use it for any other purpose either).
6911
4a64f543
MS
6912 More specifically, each of the following breakpoint types will
6913 always have a zero valued address and we don't want to mark
6914 breakpoints of any of these types to be a duplicate of an actual
6915 breakpoint at address zero:
e4f237da
KB
6916
6917 bp_watchpoint
2d134ed3
PA
6918 bp_catchpoint
6919
6920*/
e4f237da
KB
6921
6922static int
6923breakpoint_address_is_meaningful (struct breakpoint *bpt)
6924{
6925 enum bptype type = bpt->type;
6926
2d134ed3
PA
6927 return (type != bp_watchpoint && type != bp_catchpoint);
6928}
6929
6930/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6931 true if LOC1 and LOC2 represent the same watchpoint location. */
6932
6933static int
4a64f543
MS
6934watchpoint_locations_match (struct bp_location *loc1,
6935 struct bp_location *loc2)
2d134ed3 6936{
3a5c3e22
PA
6937 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6938 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6939
6940 /* Both of them must exist. */
6941 gdb_assert (w1 != NULL);
6942 gdb_assert (w2 != NULL);
2bdf28a0 6943
4a64f543
MS
6944 /* If the target can evaluate the condition expression in hardware,
6945 then we we need to insert both watchpoints even if they are at
6946 the same place. Otherwise the watchpoint will only trigger when
6947 the condition of whichever watchpoint was inserted evaluates to
6948 true, not giving a chance for GDB to check the condition of the
6949 other watchpoint. */
3a5c3e22 6950 if ((w1->cond_exp
4a64f543
MS
6951 && target_can_accel_watchpoint_condition (loc1->address,
6952 loc1->length,
0cf6dd15 6953 loc1->watchpoint_type,
3a5c3e22
PA
6954 w1->cond_exp))
6955 || (w2->cond_exp
4a64f543
MS
6956 && target_can_accel_watchpoint_condition (loc2->address,
6957 loc2->length,
0cf6dd15 6958 loc2->watchpoint_type,
3a5c3e22 6959 w2->cond_exp)))
0cf6dd15
TJB
6960 return 0;
6961
85d721b8
PA
6962 /* Note that this checks the owner's type, not the location's. In
6963 case the target does not support read watchpoints, but does
6964 support access watchpoints, we'll have bp_read_watchpoint
6965 watchpoints with hw_access locations. Those should be considered
6966 duplicates of hw_read locations. The hw_read locations will
6967 become hw_access locations later. */
2d134ed3
PA
6968 return (loc1->owner->type == loc2->owner->type
6969 && loc1->pspace->aspace == loc2->pspace->aspace
6970 && loc1->address == loc2->address
6971 && loc1->length == loc2->length);
e4f237da
KB
6972}
6973
31e77af2 6974/* See breakpoint.h. */
6c95b8df 6975
31e77af2 6976int
6c95b8df
PA
6977breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6978 struct address_space *aspace2, CORE_ADDR addr2)
6979{
f5656ead 6980 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6981 || aspace1 == aspace2)
6982 && addr1 == addr2);
6983}
6984
f1310107
TJB
6985/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6986 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6987 matches ASPACE2. On targets that have global breakpoints, the address
6988 space doesn't really matter. */
6989
6990static int
6991breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6992 int len1, struct address_space *aspace2,
6993 CORE_ADDR addr2)
6994{
f5656ead 6995 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6996 || aspace1 == aspace2)
6997 && addr2 >= addr1 && addr2 < addr1 + len1);
6998}
6999
7000/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7001 a ranged breakpoint. In most targets, a match happens only if ASPACE
7002 matches the breakpoint's address space. On targets that have global
7003 breakpoints, the address space doesn't really matter. */
7004
7005static int
7006breakpoint_location_address_match (struct bp_location *bl,
7007 struct address_space *aspace,
7008 CORE_ADDR addr)
7009{
7010 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7011 aspace, addr)
7012 || (bl->length
7013 && breakpoint_address_match_range (bl->pspace->aspace,
7014 bl->address, bl->length,
7015 aspace, addr)));
7016}
7017
1e4d1764
YQ
7018/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7019 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7020 true, otherwise returns false. */
7021
7022static int
7023tracepoint_locations_match (struct bp_location *loc1,
7024 struct bp_location *loc2)
7025{
7026 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7027 /* Since tracepoint locations are never duplicated with others', tracepoint
7028 locations at the same address of different tracepoints are regarded as
7029 different locations. */
7030 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7031 else
7032 return 0;
7033}
7034
2d134ed3
PA
7035/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7036 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7037 represent the same location. */
7038
7039static int
4a64f543
MS
7040breakpoint_locations_match (struct bp_location *loc1,
7041 struct bp_location *loc2)
2d134ed3 7042{
2bdf28a0
JK
7043 int hw_point1, hw_point2;
7044
7045 /* Both of them must not be in moribund_locations. */
7046 gdb_assert (loc1->owner != NULL);
7047 gdb_assert (loc2->owner != NULL);
7048
7049 hw_point1 = is_hardware_watchpoint (loc1->owner);
7050 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7051
7052 if (hw_point1 != hw_point2)
7053 return 0;
7054 else if (hw_point1)
7055 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7056 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7057 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7058 else
f1310107
TJB
7059 /* We compare bp_location.length in order to cover ranged breakpoints. */
7060 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7061 loc2->pspace->aspace, loc2->address)
7062 && loc1->length == loc2->length);
2d134ed3
PA
7063}
7064
76897487
KB
7065static void
7066breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7067 int bnum, int have_bnum)
7068{
f63fbe86
MS
7069 /* The longest string possibly returned by hex_string_custom
7070 is 50 chars. These must be at least that big for safety. */
7071 char astr1[64];
7072 char astr2[64];
76897487 7073
bb599908
PH
7074 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7075 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7076 if (have_bnum)
8a3fe4f8 7077 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7078 bnum, astr1, astr2);
7079 else
8a3fe4f8 7080 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7081}
7082
4a64f543
MS
7083/* Adjust a breakpoint's address to account for architectural
7084 constraints on breakpoint placement. Return the adjusted address.
7085 Note: Very few targets require this kind of adjustment. For most
7086 targets, this function is simply the identity function. */
76897487
KB
7087
7088static CORE_ADDR
a6d9a66e
UW
7089adjust_breakpoint_address (struct gdbarch *gdbarch,
7090 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7091{
a6d9a66e 7092 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7093 {
7094 /* Very few targets need any kind of breakpoint adjustment. */
7095 return bpaddr;
7096 }
88f7da05
KB
7097 else if (bptype == bp_watchpoint
7098 || bptype == bp_hardware_watchpoint
7099 || bptype == bp_read_watchpoint
7100 || bptype == bp_access_watchpoint
fe798b75 7101 || bptype == bp_catchpoint)
88f7da05
KB
7102 {
7103 /* Watchpoints and the various bp_catch_* eventpoints should not
7104 have their addresses modified. */
7105 return bpaddr;
7106 }
7c16b83e
PA
7107 else if (bptype == bp_single_step)
7108 {
7109 /* Single-step breakpoints should not have their addresses
7110 modified. If there's any architectural constrain that
7111 applies to this address, then it should have already been
7112 taken into account when the breakpoint was created in the
7113 first place. If we didn't do this, stepping through e.g.,
7114 Thumb-2 IT blocks would break. */
7115 return bpaddr;
7116 }
76897487
KB
7117 else
7118 {
7119 CORE_ADDR adjusted_bpaddr;
7120
7121 /* Some targets have architectural constraints on the placement
7122 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7123 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7124
7125 /* An adjusted breakpoint address can significantly alter
7126 a user's expectations. Print a warning if an adjustment
7127 is required. */
7128 if (adjusted_bpaddr != bpaddr)
7129 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7130
7131 return adjusted_bpaddr;
7132 }
7133}
7134
28010a5d
PA
7135void
7136init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7137 struct breakpoint *owner)
7cc221ef 7138{
7cc221ef
DJ
7139 memset (loc, 0, sizeof (*loc));
7140
348d480f
PA
7141 gdb_assert (ops != NULL);
7142
28010a5d
PA
7143 loc->ops = ops;
7144 loc->owner = owner;
511a6cd4 7145 loc->cond = NULL;
b775012e 7146 loc->cond_bytecode = NULL;
0d381245
VP
7147 loc->shlib_disabled = 0;
7148 loc->enabled = 1;
e049a4b5 7149
28010a5d 7150 switch (owner->type)
e049a4b5
DJ
7151 {
7152 case bp_breakpoint:
7c16b83e 7153 case bp_single_step:
e049a4b5
DJ
7154 case bp_until:
7155 case bp_finish:
7156 case bp_longjmp:
7157 case bp_longjmp_resume:
e2e4d78b 7158 case bp_longjmp_call_dummy:
186c406b
TT
7159 case bp_exception:
7160 case bp_exception_resume:
e049a4b5 7161 case bp_step_resume:
2c03e5be 7162 case bp_hp_step_resume:
e049a4b5
DJ
7163 case bp_watchpoint_scope:
7164 case bp_call_dummy:
aa7d318d 7165 case bp_std_terminate:
e049a4b5
DJ
7166 case bp_shlib_event:
7167 case bp_thread_event:
7168 case bp_overlay_event:
4efc6507 7169 case bp_jit_event:
0fd8e87f 7170 case bp_longjmp_master:
aa7d318d 7171 case bp_std_terminate_master:
186c406b 7172 case bp_exception_master:
0e30163f
JK
7173 case bp_gnu_ifunc_resolver:
7174 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7175 case bp_dprintf:
e049a4b5 7176 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7177 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7178 break;
7179 case bp_hardware_breakpoint:
7180 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7181 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7182 break;
7183 case bp_hardware_watchpoint:
7184 case bp_read_watchpoint:
7185 case bp_access_watchpoint:
7186 loc->loc_type = bp_loc_hardware_watchpoint;
7187 break;
7188 case bp_watchpoint:
ce78b96d 7189 case bp_catchpoint:
15c3d785
PA
7190 case bp_tracepoint:
7191 case bp_fast_tracepoint:
0fb4aa4b 7192 case bp_static_tracepoint:
e049a4b5
DJ
7193 loc->loc_type = bp_loc_other;
7194 break;
7195 default:
e2e0b3e5 7196 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7197 }
7198
f431efe5 7199 loc->refc = 1;
28010a5d
PA
7200}
7201
7202/* Allocate a struct bp_location. */
7203
7204static struct bp_location *
7205allocate_bp_location (struct breakpoint *bpt)
7206{
348d480f
PA
7207 return bpt->ops->allocate_location (bpt);
7208}
7cc221ef 7209
f431efe5
PA
7210static void
7211free_bp_location (struct bp_location *loc)
fe3f5fa8 7212{
348d480f 7213 loc->ops->dtor (loc);
fe3f5fa8
VP
7214 xfree (loc);
7215}
7216
f431efe5
PA
7217/* Increment reference count. */
7218
7219static void
7220incref_bp_location (struct bp_location *bl)
7221{
7222 ++bl->refc;
7223}
7224
7225/* Decrement reference count. If the reference count reaches 0,
7226 destroy the bp_location. Sets *BLP to NULL. */
7227
7228static void
7229decref_bp_location (struct bp_location **blp)
7230{
0807b50c
PA
7231 gdb_assert ((*blp)->refc > 0);
7232
f431efe5
PA
7233 if (--(*blp)->refc == 0)
7234 free_bp_location (*blp);
7235 *blp = NULL;
7236}
7237
346774a9 7238/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7239
346774a9
PA
7240static void
7241add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7242{
346774a9 7243 struct breakpoint *b1;
c906108c 7244
346774a9
PA
7245 /* Add this breakpoint to the end of the chain so that a list of
7246 breakpoints will come out in order of increasing numbers. */
7247
7248 b1 = breakpoint_chain;
7249 if (b1 == 0)
7250 breakpoint_chain = b;
7251 else
7252 {
7253 while (b1->next)
7254 b1 = b1->next;
7255 b1->next = b;
7256 }
7257}
7258
7259/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7260
7261static void
7262init_raw_breakpoint_without_location (struct breakpoint *b,
7263 struct gdbarch *gdbarch,
28010a5d 7264 enum bptype bptype,
c0a91b2b 7265 const struct breakpoint_ops *ops)
346774a9 7266{
c906108c 7267 memset (b, 0, sizeof (*b));
2219d63c 7268
348d480f
PA
7269 gdb_assert (ops != NULL);
7270
28010a5d 7271 b->ops = ops;
4d28f7a8 7272 b->type = bptype;
a6d9a66e 7273 b->gdbarch = gdbarch;
c906108c
SS
7274 b->language = current_language->la_language;
7275 b->input_radix = input_radix;
7276 b->thread = -1;
b5de0fa7 7277 b->enable_state = bp_enabled;
c906108c
SS
7278 b->next = 0;
7279 b->silent = 0;
7280 b->ignore_count = 0;
7281 b->commands = NULL;
818dd999 7282 b->frame_id = null_frame_id;
0d381245 7283 b->condition_not_parsed = 0;
84f4c1fe 7284 b->py_bp_object = NULL;
d0fb5eae 7285 b->related_breakpoint = b;
346774a9
PA
7286}
7287
7288/* Helper to set_raw_breakpoint below. Creates a breakpoint
7289 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7290
7291static struct breakpoint *
7292set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7293 enum bptype bptype,
c0a91b2b 7294 const struct breakpoint_ops *ops)
346774a9
PA
7295{
7296 struct breakpoint *b = XNEW (struct breakpoint);
7297
348d480f 7298 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7299 add_to_breakpoint_chain (b);
0d381245
VP
7300 return b;
7301}
7302
0e30163f
JK
7303/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7304 resolutions should be made as the user specified the location explicitly
7305 enough. */
7306
0d381245 7307static void
0e30163f 7308set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7309{
2bdf28a0
JK
7310 gdb_assert (loc->owner != NULL);
7311
0d381245 7312 if (loc->owner->type == bp_breakpoint
1042e4c0 7313 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7314 || is_tracepoint (loc->owner))
0d381245 7315 {
0e30163f 7316 int is_gnu_ifunc;
2c02bd72 7317 const char *function_name;
6a3a010b 7318 CORE_ADDR func_addr;
0e30163f 7319
2c02bd72 7320 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7321 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7322
7323 if (is_gnu_ifunc && !explicit_loc)
7324 {
7325 struct breakpoint *b = loc->owner;
7326
7327 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7328 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7329 &loc->requested_address))
7330 {
7331 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7332 loc->address = adjust_breakpoint_address (loc->gdbarch,
7333 loc->requested_address,
7334 b->type);
7335 }
7336 else if (b->type == bp_breakpoint && b->loc == loc
7337 && loc->next == NULL && b->related_breakpoint == b)
7338 {
7339 /* Create only the whole new breakpoint of this type but do not
7340 mess more complicated breakpoints with multiple locations. */
7341 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7342 /* Remember the resolver's address for use by the return
7343 breakpoint. */
7344 loc->related_address = func_addr;
0e30163f
JK
7345 }
7346 }
7347
2c02bd72
DE
7348 if (function_name)
7349 loc->function_name = xstrdup (function_name);
0d381245
VP
7350 }
7351}
7352
a6d9a66e 7353/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7354struct gdbarch *
a6d9a66e
UW
7355get_sal_arch (struct symtab_and_line sal)
7356{
7357 if (sal.section)
7358 return get_objfile_arch (sal.section->objfile);
7359 if (sal.symtab)
eb822aa6 7360 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7361
7362 return NULL;
7363}
7364
346774a9
PA
7365/* Low level routine for partially initializing a breakpoint of type
7366 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7367 file name, and line number are provided by SAL.
0d381245
VP
7368
7369 It is expected that the caller will complete the initialization of
7370 the newly created breakpoint struct as well as output any status
c56053d2 7371 information regarding the creation of a new breakpoint. */
0d381245 7372
346774a9
PA
7373static void
7374init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7375 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7376 const struct breakpoint_ops *ops)
0d381245 7377{
28010a5d 7378 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7379
3742cc8b 7380 add_location_to_breakpoint (b, &sal);
0d381245 7381
6c95b8df
PA
7382 if (bptype != bp_catchpoint)
7383 gdb_assert (sal.pspace != NULL);
7384
f8eba3c6
TT
7385 /* Store the program space that was used to set the breakpoint,
7386 except for ordinary breakpoints, which are independent of the
7387 program space. */
7388 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7389 b->pspace = sal.pspace;
346774a9 7390}
c906108c 7391
346774a9
PA
7392/* set_raw_breakpoint is a low level routine for allocating and
7393 partially initializing a breakpoint of type BPTYPE. The newly
7394 created breakpoint's address, section, source file name, and line
7395 number are provided by SAL. The newly created and partially
7396 initialized breakpoint is added to the breakpoint chain and
7397 is also returned as the value of this function.
7398
7399 It is expected that the caller will complete the initialization of
7400 the newly created breakpoint struct as well as output any status
7401 information regarding the creation of a new breakpoint. In
7402 particular, set_raw_breakpoint does NOT set the breakpoint
7403 number! Care should be taken to not allow an error to occur
7404 prior to completing the initialization of the breakpoint. If this
7405 should happen, a bogus breakpoint will be left on the chain. */
7406
7407struct breakpoint *
7408set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7409 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7410 const struct breakpoint_ops *ops)
346774a9
PA
7411{
7412 struct breakpoint *b = XNEW (struct breakpoint);
7413
348d480f 7414 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7415 add_to_breakpoint_chain (b);
c906108c
SS
7416 return b;
7417}
7418
c2c6d25f
JM
7419
7420/* Note that the breakpoint object B describes a permanent breakpoint
7421 instruction, hard-wired into the inferior's code. */
7422void
7423make_breakpoint_permanent (struct breakpoint *b)
7424{
0d381245 7425 struct bp_location *bl;
cc59ec59 7426
4a64f543
MS
7427 /* By definition, permanent breakpoints are already present in the
7428 code. Mark all locations as inserted. For now,
7429 make_breakpoint_permanent is called in just one place, so it's
7430 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7431 multiple locations or not, but it's easy to implement. */
0d381245 7432 for (bl = b->loc; bl; bl = bl->next)
1a853c52
PA
7433 {
7434 bl->permanent = 1;
7435 bl->inserted = 1;
7436 }
c2c6d25f
JM
7437}
7438
53a5351d 7439/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7440 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7441 initiated the operation. */
c906108c
SS
7442
7443void
186c406b 7444set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7445{
35df4500 7446 struct breakpoint *b, *b_tmp;
186c406b 7447 int thread = tp->num;
0fd8e87f
UW
7448
7449 /* To avoid having to rescan all objfile symbols at every step,
7450 we maintain a list of continually-inserted but always disabled
7451 longjmp "master" breakpoints. Here, we simply create momentary
7452 clones of those and enable them for the requested thread. */
35df4500 7453 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7454 if (b->pspace == current_program_space
186c406b
TT
7455 && (b->type == bp_longjmp_master
7456 || b->type == bp_exception_master))
0fd8e87f 7457 {
06edf0c0
PA
7458 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7459 struct breakpoint *clone;
cc59ec59 7460
e2e4d78b
JK
7461 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7462 after their removal. */
06edf0c0 7463 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7464 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7465 clone->thread = thread;
7466 }
186c406b
TT
7467
7468 tp->initiating_frame = frame;
c906108c
SS
7469}
7470
611c83ae 7471/* Delete all longjmp breakpoints from THREAD. */
c906108c 7472void
611c83ae 7473delete_longjmp_breakpoint (int thread)
c906108c 7474{
35df4500 7475 struct breakpoint *b, *b_tmp;
c906108c 7476
35df4500 7477 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7478 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7479 {
7480 if (b->thread == thread)
7481 delete_breakpoint (b);
7482 }
c906108c
SS
7483}
7484
f59f708a
PA
7485void
7486delete_longjmp_breakpoint_at_next_stop (int thread)
7487{
7488 struct breakpoint *b, *b_tmp;
7489
7490 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7491 if (b->type == bp_longjmp || b->type == bp_exception)
7492 {
7493 if (b->thread == thread)
7494 b->disposition = disp_del_at_next_stop;
7495 }
7496}
7497
e2e4d78b
JK
7498/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7499 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7500 pointer to any of them. Return NULL if this system cannot place longjmp
7501 breakpoints. */
7502
7503struct breakpoint *
7504set_longjmp_breakpoint_for_call_dummy (void)
7505{
7506 struct breakpoint *b, *retval = NULL;
7507
7508 ALL_BREAKPOINTS (b)
7509 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7510 {
7511 struct breakpoint *new_b;
7512
7513 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7514 &momentary_breakpoint_ops,
7515 1);
e2e4d78b
JK
7516 new_b->thread = pid_to_thread_id (inferior_ptid);
7517
7518 /* Link NEW_B into the chain of RETVAL breakpoints. */
7519
7520 gdb_assert (new_b->related_breakpoint == new_b);
7521 if (retval == NULL)
7522 retval = new_b;
7523 new_b->related_breakpoint = retval;
7524 while (retval->related_breakpoint != new_b->related_breakpoint)
7525 retval = retval->related_breakpoint;
7526 retval->related_breakpoint = new_b;
7527 }
7528
7529 return retval;
7530}
7531
7532/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7533 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7534 stack.
7535
7536 You should call this function only at places where it is safe to currently
7537 unwind the whole stack. Failed stack unwind would discard live dummy
7538 frames. */
7539
7540void
b67a2c6f 7541check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7542{
7543 struct breakpoint *b, *b_tmp;
7544
7545 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7546 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7547 {
7548 struct breakpoint *dummy_b = b->related_breakpoint;
7549
7550 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7551 dummy_b = dummy_b->related_breakpoint;
7552 if (dummy_b->type != bp_call_dummy
7553 || frame_find_by_id (dummy_b->frame_id) != NULL)
7554 continue;
7555
b67a2c6f 7556 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7557
7558 while (b->related_breakpoint != b)
7559 {
7560 if (b_tmp == b->related_breakpoint)
7561 b_tmp = b->related_breakpoint->next;
7562 delete_breakpoint (b->related_breakpoint);
7563 }
7564 delete_breakpoint (b);
7565 }
7566}
7567
1900040c
MS
7568void
7569enable_overlay_breakpoints (void)
7570{
52f0bd74 7571 struct breakpoint *b;
1900040c
MS
7572
7573 ALL_BREAKPOINTS (b)
7574 if (b->type == bp_overlay_event)
7575 {
7576 b->enable_state = bp_enabled;
44702360 7577 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7578 overlay_events_enabled = 1;
1900040c
MS
7579 }
7580}
7581
7582void
7583disable_overlay_breakpoints (void)
7584{
52f0bd74 7585 struct breakpoint *b;
1900040c
MS
7586
7587 ALL_BREAKPOINTS (b)
7588 if (b->type == bp_overlay_event)
7589 {
7590 b->enable_state = bp_disabled;
44702360 7591 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7592 overlay_events_enabled = 0;
1900040c
MS
7593 }
7594}
7595
aa7d318d
TT
7596/* Set an active std::terminate breakpoint for each std::terminate
7597 master breakpoint. */
7598void
7599set_std_terminate_breakpoint (void)
7600{
35df4500 7601 struct breakpoint *b, *b_tmp;
aa7d318d 7602
35df4500 7603 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7604 if (b->pspace == current_program_space
7605 && b->type == bp_std_terminate_master)
7606 {
06edf0c0 7607 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7608 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7609 }
7610}
7611
7612/* Delete all the std::terminate breakpoints. */
7613void
7614delete_std_terminate_breakpoint (void)
7615{
35df4500 7616 struct breakpoint *b, *b_tmp;
aa7d318d 7617
35df4500 7618 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7619 if (b->type == bp_std_terminate)
7620 delete_breakpoint (b);
7621}
7622
c4093a6a 7623struct breakpoint *
a6d9a66e 7624create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7625{
7626 struct breakpoint *b;
c4093a6a 7627
06edf0c0
PA
7628 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7629 &internal_breakpoint_ops);
7630
b5de0fa7 7631 b->enable_state = bp_enabled;
c4093a6a 7632 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7633 b->addr_string
7634 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7635
44702360 7636 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7637
c4093a6a
JM
7638 return b;
7639}
7640
7641void
7642remove_thread_event_breakpoints (void)
7643{
35df4500 7644 struct breakpoint *b, *b_tmp;
c4093a6a 7645
35df4500 7646 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7647 if (b->type == bp_thread_event
7648 && b->loc->pspace == current_program_space)
c4093a6a
JM
7649 delete_breakpoint (b);
7650}
7651
0101ce28
JJ
7652struct lang_and_radix
7653 {
7654 enum language lang;
7655 int radix;
7656 };
7657
4efc6507
DE
7658/* Create a breakpoint for JIT code registration and unregistration. */
7659
7660struct breakpoint *
7661create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7662{
7663 struct breakpoint *b;
7664
06edf0c0
PA
7665 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7666 &internal_breakpoint_ops);
44702360 7667 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7668 return b;
7669}
0101ce28 7670
03673fc7
PP
7671/* Remove JIT code registration and unregistration breakpoint(s). */
7672
7673void
7674remove_jit_event_breakpoints (void)
7675{
7676 struct breakpoint *b, *b_tmp;
7677
7678 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7679 if (b->type == bp_jit_event
7680 && b->loc->pspace == current_program_space)
7681 delete_breakpoint (b);
7682}
7683
cae688ec
JJ
7684void
7685remove_solib_event_breakpoints (void)
7686{
35df4500 7687 struct breakpoint *b, *b_tmp;
cae688ec 7688
35df4500 7689 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7690 if (b->type == bp_shlib_event
7691 && b->loc->pspace == current_program_space)
cae688ec
JJ
7692 delete_breakpoint (b);
7693}
7694
f37f681c
PA
7695/* See breakpoint.h. */
7696
7697void
7698remove_solib_event_breakpoints_at_next_stop (void)
7699{
7700 struct breakpoint *b, *b_tmp;
7701
7702 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7703 if (b->type == bp_shlib_event
7704 && b->loc->pspace == current_program_space)
7705 b->disposition = disp_del_at_next_stop;
7706}
7707
04086b45
PA
7708/* Helper for create_solib_event_breakpoint /
7709 create_and_insert_solib_event_breakpoint. Allows specifying which
7710 INSERT_MODE to pass through to update_global_location_list. */
7711
7712static struct breakpoint *
7713create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7714 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7715{
7716 struct breakpoint *b;
7717
06edf0c0
PA
7718 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7719 &internal_breakpoint_ops);
04086b45 7720 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7721 return b;
7722}
7723
04086b45
PA
7724struct breakpoint *
7725create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7726{
7727 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7728}
7729
f37f681c
PA
7730/* See breakpoint.h. */
7731
7732struct breakpoint *
7733create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7734{
7735 struct breakpoint *b;
7736
04086b45
PA
7737 /* Explicitly tell update_global_location_list to insert
7738 locations. */
7739 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7740 if (!b->loc->inserted)
7741 {
7742 delete_breakpoint (b);
7743 return NULL;
7744 }
7745 return b;
7746}
7747
cae688ec
JJ
7748/* Disable any breakpoints that are on code in shared libraries. Only
7749 apply to enabled breakpoints, disabled ones can just stay disabled. */
7750
7751void
cb851954 7752disable_breakpoints_in_shlibs (void)
cae688ec 7753{
876fa593 7754 struct bp_location *loc, **locp_tmp;
cae688ec 7755
876fa593 7756 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7757 {
2bdf28a0 7758 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7759 struct breakpoint *b = loc->owner;
2bdf28a0 7760
4a64f543
MS
7761 /* We apply the check to all breakpoints, including disabled for
7762 those with loc->duplicate set. This is so that when breakpoint
7763 becomes enabled, or the duplicate is removed, gdb will try to
7764 insert all breakpoints. If we don't set shlib_disabled here,
7765 we'll try to insert those breakpoints and fail. */
1042e4c0 7766 if (((b->type == bp_breakpoint)
508ccb1f 7767 || (b->type == bp_jit_event)
1042e4c0 7768 || (b->type == bp_hardware_breakpoint)
d77f58be 7769 || (is_tracepoint (b)))
6c95b8df 7770 && loc->pspace == current_program_space
0d381245 7771 && !loc->shlib_disabled
6c95b8df 7772 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7773 )
0d381245
VP
7774 {
7775 loc->shlib_disabled = 1;
7776 }
cae688ec
JJ
7777 }
7778}
7779
63644780
NB
7780/* Disable any breakpoints and tracepoints that are in SOLIB upon
7781 notification of unloaded_shlib. Only apply to enabled breakpoints,
7782 disabled ones can just stay disabled. */
84acb35a 7783
75149521 7784static void
84acb35a
JJ
7785disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7786{
876fa593 7787 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7788 int disabled_shlib_breaks = 0;
7789
c86cf029
VP
7790 /* SunOS a.out shared libraries are always mapped, so do not
7791 disable breakpoints; they will only be reported as unloaded
7792 through clear_solib when GDB discards its shared library
7793 list. See clear_solib for more information. */
7794 if (exec_bfd != NULL
7795 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7796 return;
7797
876fa593 7798 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7799 {
2bdf28a0 7800 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7801 struct breakpoint *b = loc->owner;
cc59ec59 7802
1e4d1764 7803 if (solib->pspace == loc->pspace
e2dd7057 7804 && !loc->shlib_disabled
1e4d1764
YQ
7805 && (((b->type == bp_breakpoint
7806 || b->type == bp_jit_event
7807 || b->type == bp_hardware_breakpoint)
7808 && (loc->loc_type == bp_loc_hardware_breakpoint
7809 || loc->loc_type == bp_loc_software_breakpoint))
7810 || is_tracepoint (b))
e2dd7057 7811 && solib_contains_address_p (solib, loc->address))
84acb35a 7812 {
e2dd7057
PP
7813 loc->shlib_disabled = 1;
7814 /* At this point, we cannot rely on remove_breakpoint
7815 succeeding so we must mark the breakpoint as not inserted
7816 to prevent future errors occurring in remove_breakpoints. */
7817 loc->inserted = 0;
8d3788bd
VP
7818
7819 /* This may cause duplicate notifications for the same breakpoint. */
7820 observer_notify_breakpoint_modified (b);
7821
e2dd7057
PP
7822 if (!disabled_shlib_breaks)
7823 {
7824 target_terminal_ours_for_output ();
3e43a32a
MS
7825 warning (_("Temporarily disabling breakpoints "
7826 "for unloaded shared library \"%s\""),
e2dd7057 7827 solib->so_name);
84acb35a 7828 }
e2dd7057 7829 disabled_shlib_breaks = 1;
84acb35a
JJ
7830 }
7831 }
84acb35a
JJ
7832}
7833
63644780
NB
7834/* Disable any breakpoints and tracepoints in OBJFILE upon
7835 notification of free_objfile. Only apply to enabled breakpoints,
7836 disabled ones can just stay disabled. */
7837
7838static void
7839disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7840{
7841 struct breakpoint *b;
7842
7843 if (objfile == NULL)
7844 return;
7845
d03de421
PA
7846 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7847 managed by the user with add-symbol-file/remove-symbol-file.
7848 Similarly to how breakpoints in shared libraries are handled in
7849 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7850 shlib_disabled so they end up uninserted on the next global
7851 location list update. Shared libraries not loaded by the user
7852 aren't handled here -- they're already handled in
7853 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7854 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7855 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7856 main objfile). */
7857 if ((objfile->flags & OBJF_SHARED) == 0
7858 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7859 return;
7860
7861 ALL_BREAKPOINTS (b)
7862 {
7863 struct bp_location *loc;
7864 int bp_modified = 0;
7865
7866 if (!is_breakpoint (b) && !is_tracepoint (b))
7867 continue;
7868
7869 for (loc = b->loc; loc != NULL; loc = loc->next)
7870 {
7871 CORE_ADDR loc_addr = loc->address;
7872
7873 if (loc->loc_type != bp_loc_hardware_breakpoint
7874 && loc->loc_type != bp_loc_software_breakpoint)
7875 continue;
7876
7877 if (loc->shlib_disabled != 0)
7878 continue;
7879
7880 if (objfile->pspace != loc->pspace)
7881 continue;
7882
7883 if (loc->loc_type != bp_loc_hardware_breakpoint
7884 && loc->loc_type != bp_loc_software_breakpoint)
7885 continue;
7886
7887 if (is_addr_in_objfile (loc_addr, objfile))
7888 {
7889 loc->shlib_disabled = 1;
08351840
PA
7890 /* At this point, we don't know whether the object was
7891 unmapped from the inferior or not, so leave the
7892 inserted flag alone. We'll handle failure to
7893 uninsert quietly, in case the object was indeed
7894 unmapped. */
63644780
NB
7895
7896 mark_breakpoint_location_modified (loc);
7897
7898 bp_modified = 1;
7899 }
7900 }
7901
7902 if (bp_modified)
7903 observer_notify_breakpoint_modified (b);
7904 }
7905}
7906
ce78b96d
JB
7907/* FORK & VFORK catchpoints. */
7908
e29a4733
PA
7909/* An instance of this type is used to represent a fork or vfork
7910 catchpoint. It includes a "struct breakpoint" as a kind of base
7911 class; users downcast to "struct breakpoint *" when needed. A
7912 breakpoint is really of this type iff its ops pointer points to
7913 CATCH_FORK_BREAKPOINT_OPS. */
7914
7915struct fork_catchpoint
7916{
7917 /* The base class. */
7918 struct breakpoint base;
7919
7920 /* Process id of a child process whose forking triggered this
7921 catchpoint. This field is only valid immediately after this
7922 catchpoint has triggered. */
7923 ptid_t forked_inferior_pid;
7924};
7925
4a64f543
MS
7926/* Implement the "insert" breakpoint_ops method for fork
7927 catchpoints. */
ce78b96d 7928
77b06cd7
TJB
7929static int
7930insert_catch_fork (struct bp_location *bl)
ce78b96d 7931{
dfd4cc63 7932 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7933}
7934
4a64f543
MS
7935/* Implement the "remove" breakpoint_ops method for fork
7936 catchpoints. */
ce78b96d
JB
7937
7938static int
77b06cd7 7939remove_catch_fork (struct bp_location *bl)
ce78b96d 7940{
dfd4cc63 7941 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7942}
7943
7944/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7945 catchpoints. */
7946
7947static int
f1310107 7948breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7949 struct address_space *aspace, CORE_ADDR bp_addr,
7950 const struct target_waitstatus *ws)
ce78b96d 7951{
e29a4733
PA
7952 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7953
f90263c1
TT
7954 if (ws->kind != TARGET_WAITKIND_FORKED)
7955 return 0;
7956
7957 c->forked_inferior_pid = ws->value.related_pid;
7958 return 1;
ce78b96d
JB
7959}
7960
4a64f543
MS
7961/* Implement the "print_it" breakpoint_ops method for fork
7962 catchpoints. */
ce78b96d
JB
7963
7964static enum print_stop_action
348d480f 7965print_it_catch_fork (bpstat bs)
ce78b96d 7966{
36dfb11c 7967 struct ui_out *uiout = current_uiout;
348d480f
PA
7968 struct breakpoint *b = bs->breakpoint_at;
7969 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7970
ce78b96d 7971 annotate_catchpoint (b->number);
36dfb11c
TT
7972 if (b->disposition == disp_del)
7973 ui_out_text (uiout, "\nTemporary catchpoint ");
7974 else
7975 ui_out_text (uiout, "\nCatchpoint ");
7976 if (ui_out_is_mi_like_p (uiout))
7977 {
7978 ui_out_field_string (uiout, "reason",
7979 async_reason_lookup (EXEC_ASYNC_FORK));
7980 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7981 }
7982 ui_out_field_int (uiout, "bkptno", b->number);
7983 ui_out_text (uiout, " (forked process ");
7984 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7985 ui_out_text (uiout, "), ");
ce78b96d
JB
7986 return PRINT_SRC_AND_LOC;
7987}
7988
4a64f543
MS
7989/* Implement the "print_one" breakpoint_ops method for fork
7990 catchpoints. */
ce78b96d
JB
7991
7992static void
a6d9a66e 7993print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7994{
e29a4733 7995 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7996 struct value_print_options opts;
79a45e25 7997 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7998
7999 get_user_print_options (&opts);
8000
4a64f543
MS
8001 /* Field 4, the address, is omitted (which makes the columns not
8002 line up too nicely with the headers, but the effect is relatively
8003 readable). */
79a45b7d 8004 if (opts.addressprint)
ce78b96d
JB
8005 ui_out_field_skip (uiout, "addr");
8006 annotate_field (5);
8007 ui_out_text (uiout, "fork");
e29a4733 8008 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8009 {
8010 ui_out_text (uiout, ", process ");
8011 ui_out_field_int (uiout, "what",
e29a4733 8012 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8013 ui_out_spaces (uiout, 1);
8014 }
8ac3646f
TT
8015
8016 if (ui_out_is_mi_like_p (uiout))
8017 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8018}
8019
8020/* Implement the "print_mention" breakpoint_ops method for fork
8021 catchpoints. */
8022
8023static void
8024print_mention_catch_fork (struct breakpoint *b)
8025{
8026 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8027}
8028
6149aea9
PA
8029/* Implement the "print_recreate" breakpoint_ops method for fork
8030 catchpoints. */
8031
8032static void
8033print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8034{
8035 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8036 print_recreate_thread (b, fp);
6149aea9
PA
8037}
8038
ce78b96d
JB
8039/* The breakpoint_ops structure to be used in fork catchpoints. */
8040
2060206e 8041static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8042
4a64f543
MS
8043/* Implement the "insert" breakpoint_ops method for vfork
8044 catchpoints. */
ce78b96d 8045
77b06cd7
TJB
8046static int
8047insert_catch_vfork (struct bp_location *bl)
ce78b96d 8048{
dfd4cc63 8049 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8050}
8051
4a64f543
MS
8052/* Implement the "remove" breakpoint_ops method for vfork
8053 catchpoints. */
ce78b96d
JB
8054
8055static int
77b06cd7 8056remove_catch_vfork (struct bp_location *bl)
ce78b96d 8057{
dfd4cc63 8058 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8059}
8060
8061/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8062 catchpoints. */
8063
8064static int
f1310107 8065breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8066 struct address_space *aspace, CORE_ADDR bp_addr,
8067 const struct target_waitstatus *ws)
ce78b96d 8068{
e29a4733
PA
8069 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8070
f90263c1
TT
8071 if (ws->kind != TARGET_WAITKIND_VFORKED)
8072 return 0;
8073
8074 c->forked_inferior_pid = ws->value.related_pid;
8075 return 1;
ce78b96d
JB
8076}
8077
4a64f543
MS
8078/* Implement the "print_it" breakpoint_ops method for vfork
8079 catchpoints. */
ce78b96d
JB
8080
8081static enum print_stop_action
348d480f 8082print_it_catch_vfork (bpstat bs)
ce78b96d 8083{
36dfb11c 8084 struct ui_out *uiout = current_uiout;
348d480f 8085 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8086 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8087
ce78b96d 8088 annotate_catchpoint (b->number);
36dfb11c
TT
8089 if (b->disposition == disp_del)
8090 ui_out_text (uiout, "\nTemporary catchpoint ");
8091 else
8092 ui_out_text (uiout, "\nCatchpoint ");
8093 if (ui_out_is_mi_like_p (uiout))
8094 {
8095 ui_out_field_string (uiout, "reason",
8096 async_reason_lookup (EXEC_ASYNC_VFORK));
8097 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8098 }
8099 ui_out_field_int (uiout, "bkptno", b->number);
8100 ui_out_text (uiout, " (vforked process ");
8101 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8102 ui_out_text (uiout, "), ");
ce78b96d
JB
8103 return PRINT_SRC_AND_LOC;
8104}
8105
4a64f543
MS
8106/* Implement the "print_one" breakpoint_ops method for vfork
8107 catchpoints. */
ce78b96d
JB
8108
8109static void
a6d9a66e 8110print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8111{
e29a4733 8112 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8113 struct value_print_options opts;
79a45e25 8114 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8115
8116 get_user_print_options (&opts);
4a64f543
MS
8117 /* Field 4, the address, is omitted (which makes the columns not
8118 line up too nicely with the headers, but the effect is relatively
8119 readable). */
79a45b7d 8120 if (opts.addressprint)
ce78b96d
JB
8121 ui_out_field_skip (uiout, "addr");
8122 annotate_field (5);
8123 ui_out_text (uiout, "vfork");
e29a4733 8124 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8125 {
8126 ui_out_text (uiout, ", process ");
8127 ui_out_field_int (uiout, "what",
e29a4733 8128 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8129 ui_out_spaces (uiout, 1);
8130 }
8ac3646f
TT
8131
8132 if (ui_out_is_mi_like_p (uiout))
8133 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8134}
8135
8136/* Implement the "print_mention" breakpoint_ops method for vfork
8137 catchpoints. */
8138
8139static void
8140print_mention_catch_vfork (struct breakpoint *b)
8141{
8142 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8143}
8144
6149aea9
PA
8145/* Implement the "print_recreate" breakpoint_ops method for vfork
8146 catchpoints. */
8147
8148static void
8149print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8150{
8151 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8152 print_recreate_thread (b, fp);
6149aea9
PA
8153}
8154
ce78b96d
JB
8155/* The breakpoint_ops structure to be used in vfork catchpoints. */
8156
2060206e 8157static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8158
edcc5120
TT
8159/* An instance of this type is used to represent an solib catchpoint.
8160 It includes a "struct breakpoint" as a kind of base class; users
8161 downcast to "struct breakpoint *" when needed. A breakpoint is
8162 really of this type iff its ops pointer points to
8163 CATCH_SOLIB_BREAKPOINT_OPS. */
8164
8165struct solib_catchpoint
8166{
8167 /* The base class. */
8168 struct breakpoint base;
8169
8170 /* True for "catch load", false for "catch unload". */
8171 unsigned char is_load;
8172
8173 /* Regular expression to match, if any. COMPILED is only valid when
8174 REGEX is non-NULL. */
8175 char *regex;
8176 regex_t compiled;
8177};
8178
8179static void
8180dtor_catch_solib (struct breakpoint *b)
8181{
8182 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8183
8184 if (self->regex)
8185 regfree (&self->compiled);
8186 xfree (self->regex);
8187
8188 base_breakpoint_ops.dtor (b);
8189}
8190
8191static int
8192insert_catch_solib (struct bp_location *ignore)
8193{
8194 return 0;
8195}
8196
8197static int
8198remove_catch_solib (struct bp_location *ignore)
8199{
8200 return 0;
8201}
8202
8203static int
8204breakpoint_hit_catch_solib (const struct bp_location *bl,
8205 struct address_space *aspace,
8206 CORE_ADDR bp_addr,
8207 const struct target_waitstatus *ws)
8208{
8209 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8210 struct breakpoint *other;
8211
8212 if (ws->kind == TARGET_WAITKIND_LOADED)
8213 return 1;
8214
8215 ALL_BREAKPOINTS (other)
8216 {
8217 struct bp_location *other_bl;
8218
8219 if (other == bl->owner)
8220 continue;
8221
8222 if (other->type != bp_shlib_event)
8223 continue;
8224
8225 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8226 continue;
8227
8228 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8229 {
8230 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8231 return 1;
8232 }
8233 }
8234
8235 return 0;
8236}
8237
8238static void
8239check_status_catch_solib (struct bpstats *bs)
8240{
8241 struct solib_catchpoint *self
8242 = (struct solib_catchpoint *) bs->breakpoint_at;
8243 int ix;
8244
8245 if (self->is_load)
8246 {
8247 struct so_list *iter;
8248
8249 for (ix = 0;
8250 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8251 ix, iter);
8252 ++ix)
8253 {
8254 if (!self->regex
8255 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8256 return;
8257 }
8258 }
8259 else
8260 {
8261 char *iter;
8262
8263 for (ix = 0;
8264 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8265 ix, iter);
8266 ++ix)
8267 {
8268 if (!self->regex
8269 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8270 return;
8271 }
8272 }
8273
8274 bs->stop = 0;
8275 bs->print_it = print_it_noop;
8276}
8277
8278static enum print_stop_action
8279print_it_catch_solib (bpstat bs)
8280{
8281 struct breakpoint *b = bs->breakpoint_at;
8282 struct ui_out *uiout = current_uiout;
8283
8284 annotate_catchpoint (b->number);
8285 if (b->disposition == disp_del)
8286 ui_out_text (uiout, "\nTemporary catchpoint ");
8287 else
8288 ui_out_text (uiout, "\nCatchpoint ");
8289 ui_out_field_int (uiout, "bkptno", b->number);
8290 ui_out_text (uiout, "\n");
8291 if (ui_out_is_mi_like_p (uiout))
8292 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8293 print_solib_event (1);
8294 return PRINT_SRC_AND_LOC;
8295}
8296
8297static void
8298print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8299{
8300 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8301 struct value_print_options opts;
8302 struct ui_out *uiout = current_uiout;
8303 char *msg;
8304
8305 get_user_print_options (&opts);
8306 /* Field 4, the address, is omitted (which makes the columns not
8307 line up too nicely with the headers, but the effect is relatively
8308 readable). */
8309 if (opts.addressprint)
8310 {
8311 annotate_field (4);
8312 ui_out_field_skip (uiout, "addr");
8313 }
8314
8315 annotate_field (5);
8316 if (self->is_load)
8317 {
8318 if (self->regex)
8319 msg = xstrprintf (_("load of library matching %s"), self->regex);
8320 else
8321 msg = xstrdup (_("load of library"));
8322 }
8323 else
8324 {
8325 if (self->regex)
8326 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8327 else
8328 msg = xstrdup (_("unload of library"));
8329 }
8330 ui_out_field_string (uiout, "what", msg);
8331 xfree (msg);
8ac3646f
TT
8332
8333 if (ui_out_is_mi_like_p (uiout))
8334 ui_out_field_string (uiout, "catch-type",
8335 self->is_load ? "load" : "unload");
edcc5120
TT
8336}
8337
8338static void
8339print_mention_catch_solib (struct breakpoint *b)
8340{
8341 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8342
8343 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8344 self->is_load ? "load" : "unload");
8345}
8346
8347static void
8348print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8349{
8350 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8351
8352 fprintf_unfiltered (fp, "%s %s",
8353 b->disposition == disp_del ? "tcatch" : "catch",
8354 self->is_load ? "load" : "unload");
8355 if (self->regex)
8356 fprintf_unfiltered (fp, " %s", self->regex);
8357 fprintf_unfiltered (fp, "\n");
8358}
8359
8360static struct breakpoint_ops catch_solib_breakpoint_ops;
8361
91985142
MG
8362/* Shared helper function (MI and CLI) for creating and installing
8363 a shared object event catchpoint. If IS_LOAD is non-zero then
8364 the events to be caught are load events, otherwise they are
8365 unload events. If IS_TEMP is non-zero the catchpoint is a
8366 temporary one. If ENABLED is non-zero the catchpoint is
8367 created in an enabled state. */
edcc5120 8368
91985142
MG
8369void
8370add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8371{
8372 struct solib_catchpoint *c;
8373 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8374 struct cleanup *cleanup;
8375
edcc5120
TT
8376 if (!arg)
8377 arg = "";
8378 arg = skip_spaces (arg);
8379
8380 c = XCNEW (struct solib_catchpoint);
8381 cleanup = make_cleanup (xfree, c);
8382
8383 if (*arg != '\0')
8384 {
8385 int errcode;
8386
8387 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8388 if (errcode != 0)
8389 {
8390 char *err = get_regcomp_error (errcode, &c->compiled);
8391
8392 make_cleanup (xfree, err);
8393 error (_("Invalid regexp (%s): %s"), err, arg);
8394 }
8395 c->regex = xstrdup (arg);
8396 }
8397
8398 c->is_load = is_load;
91985142 8399 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8400 &catch_solib_breakpoint_ops);
8401
91985142
MG
8402 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8403
edcc5120
TT
8404 discard_cleanups (cleanup);
8405 install_breakpoint (0, &c->base, 1);
8406}
8407
91985142
MG
8408/* A helper function that does all the work for "catch load" and
8409 "catch unload". */
8410
8411static void
8412catch_load_or_unload (char *arg, int from_tty, int is_load,
8413 struct cmd_list_element *command)
8414{
8415 int tempflag;
8416 const int enabled = 1;
8417
8418 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8419
8420 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8421}
8422
edcc5120
TT
8423static void
8424catch_load_command_1 (char *arg, int from_tty,
8425 struct cmd_list_element *command)
8426{
8427 catch_load_or_unload (arg, from_tty, 1, command);
8428}
8429
8430static void
8431catch_unload_command_1 (char *arg, int from_tty,
8432 struct cmd_list_element *command)
8433{
8434 catch_load_or_unload (arg, from_tty, 0, command);
8435}
8436
be5c67c1
PA
8437/* An instance of this type is used to represent a syscall catchpoint.
8438 It includes a "struct breakpoint" as a kind of base class; users
8439 downcast to "struct breakpoint *" when needed. A breakpoint is
8440 really of this type iff its ops pointer points to
8441 CATCH_SYSCALL_BREAKPOINT_OPS. */
8442
8443struct syscall_catchpoint
8444{
8445 /* The base class. */
8446 struct breakpoint base;
8447
8448 /* Syscall numbers used for the 'catch syscall' feature. If no
8449 syscall has been specified for filtering, its value is NULL.
8450 Otherwise, it holds a list of all syscalls to be caught. The
8451 list elements are allocated with xmalloc. */
8452 VEC(int) *syscalls_to_be_caught;
8453};
8454
8455/* Implement the "dtor" breakpoint_ops method for syscall
8456 catchpoints. */
8457
8458static void
8459dtor_catch_syscall (struct breakpoint *b)
8460{
8461 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8462
8463 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8464
2060206e 8465 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8466}
8467
fa3064dd
YQ
8468static const struct inferior_data *catch_syscall_inferior_data = NULL;
8469
8470struct catch_syscall_inferior_data
8471{
8472 /* We keep a count of the number of times the user has requested a
8473 particular syscall to be tracked, and pass this information to the
8474 target. This lets capable targets implement filtering directly. */
8475
8476 /* Number of times that "any" syscall is requested. */
8477 int any_syscall_count;
8478
8479 /* Count of each system call. */
8480 VEC(int) *syscalls_counts;
8481
8482 /* This counts all syscall catch requests, so we can readily determine
8483 if any catching is necessary. */
8484 int total_syscalls_count;
8485};
8486
8487static struct catch_syscall_inferior_data*
8488get_catch_syscall_inferior_data (struct inferior *inf)
8489{
8490 struct catch_syscall_inferior_data *inf_data;
8491
8492 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8493 if (inf_data == NULL)
8494 {
41bf6aca 8495 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8496 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8497 }
8498
8499 return inf_data;
8500}
8501
8502static void
8503catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8504{
8505 xfree (arg);
8506}
8507
8508
a96d9b2e
SDJ
8509/* Implement the "insert" breakpoint_ops method for syscall
8510 catchpoints. */
8511
77b06cd7
TJB
8512static int
8513insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8514{
be5c67c1 8515 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8516 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8517 struct catch_syscall_inferior_data *inf_data
8518 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8519
fa3064dd 8520 ++inf_data->total_syscalls_count;
be5c67c1 8521 if (!c->syscalls_to_be_caught)
fa3064dd 8522 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8523 else
8524 {
8525 int i, iter;
cc59ec59 8526
a96d9b2e 8527 for (i = 0;
be5c67c1 8528 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8529 i++)
8530 {
8531 int elem;
cc59ec59 8532
fa3064dd 8533 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8534 {
fa3064dd 8535 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8536 uintptr_t vec_addr_offset
8537 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8538 uintptr_t vec_addr;
fa3064dd
YQ
8539 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8540 vec_addr = ((uintptr_t) VEC_address (int,
8541 inf_data->syscalls_counts)
8542 + vec_addr_offset);
a96d9b2e
SDJ
8543 memset ((void *) vec_addr, 0,
8544 (iter + 1 - old_size) * sizeof (int));
8545 }
fa3064dd
YQ
8546 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8547 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8548 }
8549 }
8550
dfd4cc63 8551 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8552 inf_data->total_syscalls_count != 0,
8553 inf_data->any_syscall_count,
8554 VEC_length (int,
8555 inf_data->syscalls_counts),
8556 VEC_address (int,
8557 inf_data->syscalls_counts));
a96d9b2e
SDJ
8558}
8559
8560/* Implement the "remove" breakpoint_ops method for syscall
8561 catchpoints. */
8562
8563static int
77b06cd7 8564remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8565{
be5c67c1 8566 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8567 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8568 struct catch_syscall_inferior_data *inf_data
8569 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8570
fa3064dd 8571 --inf_data->total_syscalls_count;
be5c67c1 8572 if (!c->syscalls_to_be_caught)
fa3064dd 8573 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8574 else
8575 {
8576 int i, iter;
cc59ec59 8577
a96d9b2e 8578 for (i = 0;
be5c67c1 8579 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8580 i++)
8581 {
8582 int elem;
fa3064dd 8583 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8584 /* Shouldn't happen. */
8585 continue;
fa3064dd
YQ
8586 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8587 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8588 }
8589 }
8590
dfd4cc63 8591 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8592 inf_data->total_syscalls_count != 0,
8593 inf_data->any_syscall_count,
8594 VEC_length (int,
8595 inf_data->syscalls_counts),
3e43a32a 8596 VEC_address (int,
fa3064dd 8597 inf_data->syscalls_counts));
a96d9b2e
SDJ
8598}
8599
8600/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8601 catchpoints. */
8602
8603static int
f1310107 8604breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8605 struct address_space *aspace, CORE_ADDR bp_addr,
8606 const struct target_waitstatus *ws)
a96d9b2e 8607{
4a64f543
MS
8608 /* We must check if we are catching specific syscalls in this
8609 breakpoint. If we are, then we must guarantee that the called
8610 syscall is the same syscall we are catching. */
a96d9b2e 8611 int syscall_number = 0;
be5c67c1
PA
8612 const struct syscall_catchpoint *c
8613 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8614
f90263c1
TT
8615 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8616 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8617 return 0;
8618
f90263c1
TT
8619 syscall_number = ws->value.syscall_number;
8620
a96d9b2e 8621 /* Now, checking if the syscall is the same. */
be5c67c1 8622 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8623 {
8624 int i, iter;
cc59ec59 8625
a96d9b2e 8626 for (i = 0;
be5c67c1 8627 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8628 i++)
8629 if (syscall_number == iter)
4924df79
GKB
8630 return 1;
8631
8632 return 0;
a96d9b2e
SDJ
8633 }
8634
8635 return 1;
8636}
8637
8638/* Implement the "print_it" breakpoint_ops method for syscall
8639 catchpoints. */
8640
8641static enum print_stop_action
348d480f 8642print_it_catch_syscall (bpstat bs)
a96d9b2e 8643{
36dfb11c 8644 struct ui_out *uiout = current_uiout;
348d480f 8645 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8646 /* These are needed because we want to know in which state a
8647 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8648 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8649 must print "called syscall" or "returned from syscall". */
8650 ptid_t ptid;
8651 struct target_waitstatus last;
8652 struct syscall s;
458c8db8 8653 struct gdbarch *gdbarch = bs->bp_location_at->gdbarch;
a96d9b2e
SDJ
8654
8655 get_last_target_status (&ptid, &last);
8656
458c8db8 8657 get_syscall_by_number (gdbarch, last.value.syscall_number, &s);
a96d9b2e
SDJ
8658
8659 annotate_catchpoint (b->number);
8660
36dfb11c
TT
8661 if (b->disposition == disp_del)
8662 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8663 else
36dfb11c
TT
8664 ui_out_text (uiout, "\nCatchpoint ");
8665 if (ui_out_is_mi_like_p (uiout))
8666 {
8667 ui_out_field_string (uiout, "reason",
8668 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8669 ? EXEC_ASYNC_SYSCALL_ENTRY
8670 : EXEC_ASYNC_SYSCALL_RETURN));
8671 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8672 }
8673 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8674
8675 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8676 ui_out_text (uiout, " (call to syscall ");
8677 else
8678 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8679
36dfb11c
TT
8680 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8681 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8682 if (s.name != NULL)
8683 ui_out_field_string (uiout, "syscall-name", s.name);
8684
8685 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8686
8687 return PRINT_SRC_AND_LOC;
8688}
8689
8690/* Implement the "print_one" breakpoint_ops method for syscall
8691 catchpoints. */
8692
8693static void
8694print_one_catch_syscall (struct breakpoint *b,
f1310107 8695 struct bp_location **last_loc)
a96d9b2e 8696{
be5c67c1 8697 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8698 struct value_print_options opts;
79a45e25 8699 struct ui_out *uiout = current_uiout;
458c8db8 8700 struct gdbarch *gdbarch = b->loc->gdbarch;
a96d9b2e
SDJ
8701
8702 get_user_print_options (&opts);
4a64f543
MS
8703 /* Field 4, the address, is omitted (which makes the columns not
8704 line up too nicely with the headers, but the effect is relatively
8705 readable). */
a96d9b2e
SDJ
8706 if (opts.addressprint)
8707 ui_out_field_skip (uiout, "addr");
8708 annotate_field (5);
8709
be5c67c1
PA
8710 if (c->syscalls_to_be_caught
8711 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8712 ui_out_text (uiout, "syscalls \"");
8713 else
8714 ui_out_text (uiout, "syscall \"");
8715
be5c67c1 8716 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8717 {
8718 int i, iter;
8719 char *text = xstrprintf ("%s", "");
cc59ec59 8720
a96d9b2e 8721 for (i = 0;
be5c67c1 8722 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8723 i++)
8724 {
8725 char *x = text;
8726 struct syscall s;
458c8db8 8727 get_syscall_by_number (gdbarch, iter, &s);
a96d9b2e
SDJ
8728
8729 if (s.name != NULL)
8730 text = xstrprintf ("%s%s, ", text, s.name);
8731 else
8732 text = xstrprintf ("%s%d, ", text, iter);
8733
8734 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8735 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8736 on every call. */
8737 xfree (x);
8738 }
8739 /* Remove the last comma. */
8740 text[strlen (text) - 2] = '\0';
8741 ui_out_field_string (uiout, "what", text);
8742 }
8743 else
8744 ui_out_field_string (uiout, "what", "<any syscall>");
8745 ui_out_text (uiout, "\" ");
8ac3646f
TT
8746
8747 if (ui_out_is_mi_like_p (uiout))
8748 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8749}
8750
8751/* Implement the "print_mention" breakpoint_ops method for syscall
8752 catchpoints. */
8753
8754static void
8755print_mention_catch_syscall (struct breakpoint *b)
8756{
be5c67c1 8757 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
458c8db8 8758 struct gdbarch *gdbarch = b->loc->gdbarch;
be5c67c1
PA
8759
8760 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8761 {
8762 int i, iter;
8763
be5c67c1 8764 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8765 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8766 else
8767 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8768
8769 for (i = 0;
be5c67c1 8770 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8771 i++)
8772 {
8773 struct syscall s;
458c8db8 8774 get_syscall_by_number (gdbarch, iter, &s);
a96d9b2e
SDJ
8775
8776 if (s.name)
8777 printf_filtered (" '%s' [%d]", s.name, s.number);
8778 else
8779 printf_filtered (" %d", s.number);
8780 }
8781 printf_filtered (")");
8782 }
8783 else
8784 printf_filtered (_("Catchpoint %d (any syscall)"),
8785 b->number);
8786}
8787
6149aea9
PA
8788/* Implement the "print_recreate" breakpoint_ops method for syscall
8789 catchpoints. */
8790
8791static void
8792print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8793{
be5c67c1 8794 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
458c8db8 8795 struct gdbarch *gdbarch = b->loc->gdbarch;
be5c67c1 8796
6149aea9
PA
8797 fprintf_unfiltered (fp, "catch syscall");
8798
be5c67c1 8799 if (c->syscalls_to_be_caught)
6149aea9
PA
8800 {
8801 int i, iter;
8802
8803 for (i = 0;
be5c67c1 8804 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8805 i++)
8806 {
8807 struct syscall s;
8808
458c8db8 8809 get_syscall_by_number (gdbarch, iter, &s);
6149aea9
PA
8810 if (s.name)
8811 fprintf_unfiltered (fp, " %s", s.name);
8812 else
8813 fprintf_unfiltered (fp, " %d", s.number);
8814 }
8815 }
d9b3f62e 8816 print_recreate_thread (b, fp);
6149aea9
PA
8817}
8818
a96d9b2e
SDJ
8819/* The breakpoint_ops structure to be used in syscall catchpoints. */
8820
2060206e 8821static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8822
8823/* Returns non-zero if 'b' is a syscall catchpoint. */
8824
8825static int
8826syscall_catchpoint_p (struct breakpoint *b)
8827{
8828 return (b->ops == &catch_syscall_breakpoint_ops);
8829}
8830
346774a9
PA
8831/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8832 is non-zero, then make the breakpoint temporary. If COND_STRING is
8833 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8834 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8835
ab04a2af 8836void
346774a9
PA
8837init_catchpoint (struct breakpoint *b,
8838 struct gdbarch *gdbarch, int tempflag,
8839 char *cond_string,
c0a91b2b 8840 const struct breakpoint_ops *ops)
c906108c 8841{
c5aa993b 8842 struct symtab_and_line sal;
346774a9 8843
fe39c653 8844 init_sal (&sal);
6c95b8df 8845 sal.pspace = current_program_space;
c5aa993b 8846
28010a5d 8847 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8848
1b36a34b 8849 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8850 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8851}
8852
28010a5d 8853void
3ea46bff 8854install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8855{
8856 add_to_breakpoint_chain (b);
3a5c3e22 8857 set_breakpoint_number (internal, b);
558a9d82
YQ
8858 if (is_tracepoint (b))
8859 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8860 if (!internal)
8861 mention (b);
c56053d2 8862 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8863
8864 if (update_gll)
44702360 8865 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8866}
8867
9b70b993 8868static void
a6d9a66e
UW
8869create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8870 int tempflag, char *cond_string,
c0a91b2b 8871 const struct breakpoint_ops *ops)
c906108c 8872{
e29a4733 8873 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8874
e29a4733
PA
8875 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8876
8877 c->forked_inferior_pid = null_ptid;
8878
3ea46bff 8879 install_breakpoint (0, &c->base, 1);
c906108c
SS
8880}
8881
fe798b75
JB
8882/* Exec catchpoints. */
8883
b4d90040
PA
8884/* An instance of this type is used to represent an exec catchpoint.
8885 It includes a "struct breakpoint" as a kind of base class; users
8886 downcast to "struct breakpoint *" when needed. A breakpoint is
8887 really of this type iff its ops pointer points to
8888 CATCH_EXEC_BREAKPOINT_OPS. */
8889
8890struct exec_catchpoint
8891{
8892 /* The base class. */
8893 struct breakpoint base;
8894
8895 /* Filename of a program whose exec triggered this catchpoint.
8896 This field is only valid immediately after this catchpoint has
8897 triggered. */
8898 char *exec_pathname;
8899};
8900
8901/* Implement the "dtor" breakpoint_ops method for exec
8902 catchpoints. */
8903
8904static void
8905dtor_catch_exec (struct breakpoint *b)
8906{
8907 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8908
8909 xfree (c->exec_pathname);
348d480f 8910
2060206e 8911 base_breakpoint_ops.dtor (b);
b4d90040
PA
8912}
8913
77b06cd7
TJB
8914static int
8915insert_catch_exec (struct bp_location *bl)
c906108c 8916{
dfd4cc63 8917 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8918}
c906108c 8919
fe798b75 8920static int
77b06cd7 8921remove_catch_exec (struct bp_location *bl)
fe798b75 8922{
dfd4cc63 8923 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8924}
c906108c 8925
fe798b75 8926static int
f1310107 8927breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8928 struct address_space *aspace, CORE_ADDR bp_addr,
8929 const struct target_waitstatus *ws)
fe798b75 8930{
b4d90040
PA
8931 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8932
f90263c1
TT
8933 if (ws->kind != TARGET_WAITKIND_EXECD)
8934 return 0;
8935
8936 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8937 return 1;
fe798b75 8938}
c906108c 8939
fe798b75 8940static enum print_stop_action
348d480f 8941print_it_catch_exec (bpstat bs)
fe798b75 8942{
36dfb11c 8943 struct ui_out *uiout = current_uiout;
348d480f 8944 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8945 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8946
fe798b75 8947 annotate_catchpoint (b->number);
36dfb11c
TT
8948 if (b->disposition == disp_del)
8949 ui_out_text (uiout, "\nTemporary catchpoint ");
8950 else
8951 ui_out_text (uiout, "\nCatchpoint ");
8952 if (ui_out_is_mi_like_p (uiout))
8953 {
8954 ui_out_field_string (uiout, "reason",
8955 async_reason_lookup (EXEC_ASYNC_EXEC));
8956 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8957 }
8958 ui_out_field_int (uiout, "bkptno", b->number);
8959 ui_out_text (uiout, " (exec'd ");
8960 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8961 ui_out_text (uiout, "), ");
8962
fe798b75 8963 return PRINT_SRC_AND_LOC;
c906108c
SS
8964}
8965
fe798b75 8966static void
a6d9a66e 8967print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8968{
b4d90040 8969 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8970 struct value_print_options opts;
79a45e25 8971 struct ui_out *uiout = current_uiout;
fe798b75
JB
8972
8973 get_user_print_options (&opts);
8974
8975 /* Field 4, the address, is omitted (which makes the columns
8976 not line up too nicely with the headers, but the effect
8977 is relatively readable). */
8978 if (opts.addressprint)
8979 ui_out_field_skip (uiout, "addr");
8980 annotate_field (5);
8981 ui_out_text (uiout, "exec");
b4d90040 8982 if (c->exec_pathname != NULL)
fe798b75
JB
8983 {
8984 ui_out_text (uiout, ", program \"");
b4d90040 8985 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8986 ui_out_text (uiout, "\" ");
8987 }
8ac3646f
TT
8988
8989 if (ui_out_is_mi_like_p (uiout))
8990 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8991}
8992
8993static void
8994print_mention_catch_exec (struct breakpoint *b)
8995{
8996 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8997}
8998
6149aea9
PA
8999/* Implement the "print_recreate" breakpoint_ops method for exec
9000 catchpoints. */
9001
9002static void
9003print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
9004{
9005 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 9006 print_recreate_thread (b, fp);
6149aea9
PA
9007}
9008
2060206e 9009static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 9010
a96d9b2e
SDJ
9011static void
9012create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 9013 const struct breakpoint_ops *ops)
a96d9b2e 9014{
be5c67c1 9015 struct syscall_catchpoint *c;
a96d9b2e 9016 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 9017
be5c67c1
PA
9018 c = XNEW (struct syscall_catchpoint);
9019 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
9020 c->syscalls_to_be_caught = filter;
a96d9b2e 9021
3ea46bff 9022 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
9023}
9024
c906108c 9025static int
fba45db2 9026hw_breakpoint_used_count (void)
c906108c 9027{
c906108c 9028 int i = 0;
f1310107
TJB
9029 struct breakpoint *b;
9030 struct bp_location *bl;
c906108c
SS
9031
9032 ALL_BREAKPOINTS (b)
c5aa993b 9033 {
d6b74ac4 9034 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
9035 for (bl = b->loc; bl; bl = bl->next)
9036 {
9037 /* Special types of hardware breakpoints may use more than
9038 one register. */
348d480f 9039 i += b->ops->resources_needed (bl);
f1310107 9040 }
c5aa993b 9041 }
c906108c
SS
9042
9043 return i;
9044}
9045
a1398e0c
PA
9046/* Returns the resources B would use if it were a hardware
9047 watchpoint. */
9048
c906108c 9049static int
a1398e0c 9050hw_watchpoint_use_count (struct breakpoint *b)
c906108c 9051{
c906108c 9052 int i = 0;
e09342b5 9053 struct bp_location *bl;
c906108c 9054
a1398e0c
PA
9055 if (!breakpoint_enabled (b))
9056 return 0;
9057
9058 for (bl = b->loc; bl; bl = bl->next)
9059 {
9060 /* Special types of hardware watchpoints may use more than
9061 one register. */
9062 i += b->ops->resources_needed (bl);
9063 }
9064
9065 return i;
9066}
9067
9068/* Returns the sum the used resources of all hardware watchpoints of
9069 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
9070 the sum of the used resources of all hardware watchpoints of other
9071 types _not_ TYPE. */
9072
9073static int
9074hw_watchpoint_used_count_others (struct breakpoint *except,
9075 enum bptype type, int *other_type_used)
9076{
9077 int i = 0;
9078 struct breakpoint *b;
9079
c906108c
SS
9080 *other_type_used = 0;
9081 ALL_BREAKPOINTS (b)
e09342b5 9082 {
a1398e0c
PA
9083 if (b == except)
9084 continue;
e09342b5
TJB
9085 if (!breakpoint_enabled (b))
9086 continue;
9087
a1398e0c
PA
9088 if (b->type == type)
9089 i += hw_watchpoint_use_count (b);
9090 else if (is_hardware_watchpoint (b))
9091 *other_type_used = 1;
e09342b5
TJB
9092 }
9093
c906108c
SS
9094 return i;
9095}
9096
c906108c 9097void
fba45db2 9098disable_watchpoints_before_interactive_call_start (void)
c906108c 9099{
c5aa993b 9100 struct breakpoint *b;
c906108c
SS
9101
9102 ALL_BREAKPOINTS (b)
c5aa993b 9103 {
cc60f2e3 9104 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 9105 {
b5de0fa7 9106 b->enable_state = bp_call_disabled;
44702360 9107 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
9108 }
9109 }
c906108c
SS
9110}
9111
9112void
fba45db2 9113enable_watchpoints_after_interactive_call_stop (void)
c906108c 9114{
c5aa993b 9115 struct breakpoint *b;
c906108c
SS
9116
9117 ALL_BREAKPOINTS (b)
c5aa993b 9118 {
cc60f2e3 9119 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 9120 {
b5de0fa7 9121 b->enable_state = bp_enabled;
44702360 9122 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
9123 }
9124 }
c906108c
SS
9125}
9126
8bea4e01
UW
9127void
9128disable_breakpoints_before_startup (void)
9129{
6c95b8df 9130 current_program_space->executing_startup = 1;
44702360 9131 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
9132}
9133
9134void
9135enable_breakpoints_after_startup (void)
9136{
6c95b8df 9137 current_program_space->executing_startup = 0;
f8eba3c6 9138 breakpoint_re_set ();
8bea4e01
UW
9139}
9140
7c16b83e
PA
9141/* Create a new single-step breakpoint for thread THREAD, with no
9142 locations. */
c906108c 9143
7c16b83e
PA
9144static struct breakpoint *
9145new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
9146{
9147 struct breakpoint *b = XNEW (struct breakpoint);
9148
9149 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
9150 &momentary_breakpoint_ops);
9151
9152 b->disposition = disp_donttouch;
9153 b->frame_id = null_frame_id;
9154
9155 b->thread = thread;
9156 gdb_assert (b->thread != 0);
9157
9158 add_to_breakpoint_chain (b);
9159
9160 return b;
9161}
9162
9163/* Set a momentary breakpoint of type TYPE at address specified by
9164 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
9165 frame. */
c906108c
SS
9166
9167struct breakpoint *
a6d9a66e
UW
9168set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9169 struct frame_id frame_id, enum bptype type)
c906108c 9170{
52f0bd74 9171 struct breakpoint *b;
edb3359d 9172
193facb3
JK
9173 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9174 tail-called one. */
9175 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 9176
06edf0c0 9177 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
9178 b->enable_state = bp_enabled;
9179 b->disposition = disp_donttouch;
818dd999 9180 b->frame_id = frame_id;
c906108c 9181
4a64f543
MS
9182 /* If we're debugging a multi-threaded program, then we want
9183 momentary breakpoints to be active in only a single thread of
9184 control. */
39f77062
KB
9185 if (in_thread_list (inferior_ptid))
9186 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 9187
44702360 9188 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 9189
c906108c
SS
9190 return b;
9191}
611c83ae 9192
06edf0c0 9193/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
9194 The new breakpoint will have type TYPE, use OPS as its
9195 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 9196
06edf0c0
PA
9197static struct breakpoint *
9198momentary_breakpoint_from_master (struct breakpoint *orig,
9199 enum bptype type,
a1aa2221
LM
9200 const struct breakpoint_ops *ops,
9201 int loc_enabled)
e58b0e63
PA
9202{
9203 struct breakpoint *copy;
9204
06edf0c0 9205 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 9206 copy->loc = allocate_bp_location (copy);
0e30163f 9207 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 9208
a6d9a66e 9209 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
9210 copy->loc->requested_address = orig->loc->requested_address;
9211 copy->loc->address = orig->loc->address;
9212 copy->loc->section = orig->loc->section;
6c95b8df 9213 copy->loc->pspace = orig->loc->pspace;
55aa24fb 9214 copy->loc->probe = orig->loc->probe;
f8eba3c6 9215 copy->loc->line_number = orig->loc->line_number;
2f202fde 9216 copy->loc->symtab = orig->loc->symtab;
a1aa2221 9217 copy->loc->enabled = loc_enabled;
e58b0e63
PA
9218 copy->frame_id = orig->frame_id;
9219 copy->thread = orig->thread;
6c95b8df 9220 copy->pspace = orig->pspace;
e58b0e63
PA
9221
9222 copy->enable_state = bp_enabled;
9223 copy->disposition = disp_donttouch;
9224 copy->number = internal_breakpoint_number--;
9225
44702360 9226 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
9227 return copy;
9228}
9229
06edf0c0
PA
9230/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9231 ORIG is NULL. */
9232
9233struct breakpoint *
9234clone_momentary_breakpoint (struct breakpoint *orig)
9235{
9236 /* If there's nothing to clone, then return nothing. */
9237 if (orig == NULL)
9238 return NULL;
9239
a1aa2221 9240 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
9241}
9242
611c83ae 9243struct breakpoint *
a6d9a66e
UW
9244set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9245 enum bptype type)
611c83ae
PA
9246{
9247 struct symtab_and_line sal;
9248
9249 sal = find_pc_line (pc, 0);
9250 sal.pc = pc;
9251 sal.section = find_pc_overlay (pc);
9252 sal.explicit_pc = 1;
9253
a6d9a66e 9254 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9255}
c906108c 9256\f
c5aa993b 9257
c906108c
SS
9258/* Tell the user we have just set a breakpoint B. */
9259
9260static void
fba45db2 9261mention (struct breakpoint *b)
c906108c 9262{
348d480f 9263 b->ops->print_mention (b);
79a45e25 9264 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9265 return;
c906108c
SS
9266 printf_filtered ("\n");
9267}
c906108c 9268\f
c5aa993b 9269
1a853c52
PA
9270static int bp_loc_is_permanent (struct bp_location *loc);
9271
0d381245 9272static struct bp_location *
39d61571 9273add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9274 const struct symtab_and_line *sal)
9275{
9276 struct bp_location *loc, **tmp;
3742cc8b
YQ
9277 CORE_ADDR adjusted_address;
9278 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9279
9280 if (loc_gdbarch == NULL)
9281 loc_gdbarch = b->gdbarch;
9282
9283 /* Adjust the breakpoint's address prior to allocating a location.
9284 Once we call allocate_bp_location(), that mostly uninitialized
9285 location will be placed on the location chain. Adjustment of the
9286 breakpoint may cause target_read_memory() to be called and we do
9287 not want its scan of the location chain to find a breakpoint and
9288 location that's only been partially initialized. */
9289 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9290 sal->pc, b->type);
0d381245 9291
d30113d4 9292 /* Sort the locations by their ADDRESS. */
39d61571 9293 loc = allocate_bp_location (b);
d30113d4
JK
9294 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9295 tmp = &((*tmp)->next))
0d381245 9296 ;
d30113d4 9297 loc->next = *tmp;
0d381245 9298 *tmp = loc;
3742cc8b 9299
0d381245 9300 loc->requested_address = sal->pc;
3742cc8b 9301 loc->address = adjusted_address;
6c95b8df 9302 loc->pspace = sal->pspace;
729662a5
TT
9303 loc->probe.probe = sal->probe;
9304 loc->probe.objfile = sal->objfile;
6c95b8df 9305 gdb_assert (loc->pspace != NULL);
0d381245 9306 loc->section = sal->section;
3742cc8b 9307 loc->gdbarch = loc_gdbarch;
f8eba3c6 9308 loc->line_number = sal->line;
2f202fde 9309 loc->symtab = sal->symtab;
f8eba3c6 9310
0e30163f
JK
9311 set_breakpoint_location_function (loc,
9312 sal->explicit_pc || sal->explicit_line);
1a853c52
PA
9313
9314 if (bp_loc_is_permanent (loc))
9315 {
9316 loc->inserted = 1;
9317 loc->permanent = 1;
9318 }
9319
0d381245
VP
9320 return loc;
9321}
514f746b
AR
9322\f
9323
1cf4d951 9324/* See breakpoint.h. */
514f746b 9325
1cf4d951
PA
9326int
9327program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9328{
9329 int len;
9330 CORE_ADDR addr;
1afeeb75 9331 const gdb_byte *bpoint;
514f746b 9332 gdb_byte *target_mem;
939c61fa
JK
9333 struct cleanup *cleanup;
9334 int retval = 0;
514f746b 9335
1cf4d951
PA
9336 addr = address;
9337 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9338
9339 /* Software breakpoints unsupported? */
9340 if (bpoint == NULL)
9341 return 0;
9342
9343 target_mem = alloca (len);
9344
9345 /* Enable the automatic memory restoration from breakpoints while
9346 we read the memory. Otherwise we could say about our temporary
9347 breakpoints they are permanent. */
9348 cleanup = make_show_memory_breakpoints_cleanup (0);
9349
9350 if (target_read_memory (address, target_mem, len) == 0
9351 && memcmp (target_mem, bpoint, len) == 0)
9352 retval = 1;
9353
9354 do_cleanups (cleanup);
9355
9356 return retval;
9357}
9358
9359/* Return 1 if LOC is pointing to a permanent breakpoint,
9360 return 0 otherwise. */
9361
9362static int
9363bp_loc_is_permanent (struct bp_location *loc)
9364{
9365 struct cleanup *cleanup;
9366 int retval;
9367
514f746b
AR
9368 gdb_assert (loc != NULL);
9369
e8af5d7a
JB
9370 /* bp_call_dummy breakpoint locations are usually memory locations
9371 where GDB just wrote a breakpoint instruction, making it look
9372 as if there is a permanent breakpoint at that location. Considering
9373 it permanent makes GDB rely on that breakpoint instruction to stop
9374 the program, thus removing the need to insert its own breakpoint
9375 there. This is normally expected to work, except that some versions
9376 of QEMU (Eg: QEMU 2.0.0 for SPARC) just report a fatal problem (Trap
9377 0x02 while interrupts disabled, Error state) instead of reporting
9378 a SIGTRAP. QEMU should probably be fixed, but in the interest of
9379 compatibility with versions that behave this way, we always consider
9380 bp_call_dummy breakpoint locations as non-permanent. */
9381 if (loc->owner->type == bp_call_dummy)
9382 return 0;
9383
6c95b8df 9384 cleanup = save_current_space_and_thread ();
6c95b8df 9385 switch_to_program_space_and_thread (loc->pspace);
939c61fa 9386
1cf4d951 9387 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 9388
939c61fa
JK
9389 do_cleanups (cleanup);
9390
9391 return retval;
514f746b
AR
9392}
9393
e7e0cddf
SS
9394/* Build a command list for the dprintf corresponding to the current
9395 settings of the dprintf style options. */
9396
9397static void
9398update_dprintf_command_list (struct breakpoint *b)
9399{
9400 char *dprintf_args = b->extra_string;
9401 char *printf_line = NULL;
9402
9403 if (!dprintf_args)
9404 return;
9405
9406 dprintf_args = skip_spaces (dprintf_args);
9407
9408 /* Allow a comma, as it may have terminated a location, but don't
9409 insist on it. */
9410 if (*dprintf_args == ',')
9411 ++dprintf_args;
9412 dprintf_args = skip_spaces (dprintf_args);
9413
9414 if (*dprintf_args != '"')
9415 error (_("Bad format string, missing '\"'."));
9416
d3ce09f5 9417 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9418 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9419 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9420 {
9421 if (!dprintf_function)
9422 error (_("No function supplied for dprintf call"));
9423
9424 if (dprintf_channel && strlen (dprintf_channel) > 0)
9425 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9426 dprintf_function,
9427 dprintf_channel,
9428 dprintf_args);
9429 else
9430 printf_line = xstrprintf ("call (void) %s (%s)",
9431 dprintf_function,
9432 dprintf_args);
9433 }
d3ce09f5
SS
9434 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9435 {
9436 if (target_can_run_breakpoint_commands ())
9437 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9438 else
9439 {
9440 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9441 printf_line = xstrprintf ("printf %s", dprintf_args);
9442 }
9443 }
e7e0cddf
SS
9444 else
9445 internal_error (__FILE__, __LINE__,
9446 _("Invalid dprintf style."));
9447
f28045c2 9448 gdb_assert (printf_line != NULL);
9d6e6e84 9449 /* Manufacture a printf sequence. */
f28045c2 9450 {
9d6e6e84
HZ
9451 struct command_line *printf_cmd_line
9452 = xmalloc (sizeof (struct command_line));
e7e0cddf 9453
f28045c2
YQ
9454 printf_cmd_line = xmalloc (sizeof (struct command_line));
9455 printf_cmd_line->control_type = simple_control;
9456 printf_cmd_line->body_count = 0;
9457 printf_cmd_line->body_list = NULL;
9d6e6e84 9458 printf_cmd_line->next = NULL;
f28045c2 9459 printf_cmd_line->line = printf_line;
e7e0cddf 9460
f28045c2
YQ
9461 breakpoint_set_commands (b, printf_cmd_line);
9462 }
e7e0cddf
SS
9463}
9464
9465/* Update all dprintf commands, making their command lists reflect
9466 current style settings. */
9467
9468static void
9469update_dprintf_commands (char *args, int from_tty,
9470 struct cmd_list_element *c)
9471{
9472 struct breakpoint *b;
9473
9474 ALL_BREAKPOINTS (b)
9475 {
9476 if (b->type == bp_dprintf)
9477 update_dprintf_command_list (b);
9478 }
9479}
c3f6f71d 9480
018d34a4
VP
9481/* Create a breakpoint with SAL as location. Use ADDR_STRING
9482 as textual description of the location, and COND_STRING
db107f19 9483 as condition expression. */
018d34a4
VP
9484
9485static void
d9b3f62e
PA
9486init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9487 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9488 char *filter, char *cond_string,
e7e0cddf 9489 char *extra_string,
d9b3f62e
PA
9490 enum bptype type, enum bpdisp disposition,
9491 int thread, int task, int ignore_count,
c0a91b2b 9492 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9493 int enabled, int internal, unsigned flags,
9494 int display_canonical)
018d34a4 9495{
0d381245 9496 int i;
018d34a4
VP
9497
9498 if (type == bp_hardware_breakpoint)
9499 {
fbbd034e
AS
9500 int target_resources_ok;
9501
9502 i = hw_breakpoint_used_count ();
9503 target_resources_ok =
9504 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9505 i + 1, 0);
9506 if (target_resources_ok == 0)
9507 error (_("No hardware breakpoint support in the target."));
9508 else if (target_resources_ok < 0)
9509 error (_("Hardware breakpoints used exceeds limit."));
9510 }
9511
6c95b8df
PA
9512 gdb_assert (sals.nelts > 0);
9513
0d381245
VP
9514 for (i = 0; i < sals.nelts; ++i)
9515 {
9516 struct symtab_and_line sal = sals.sals[i];
9517 struct bp_location *loc;
9518
9519 if (from_tty)
5af949e3
UW
9520 {
9521 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9522 if (!loc_gdbarch)
9523 loc_gdbarch = gdbarch;
9524
9525 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9526 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9527 }
0d381245
VP
9528
9529 if (i == 0)
9530 {
d9b3f62e 9531 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9532 b->thread = thread;
4a306c9a 9533 b->task = task;
855a6e68 9534
0d381245 9535 b->cond_string = cond_string;
e7e0cddf 9536 b->extra_string = extra_string;
0d381245 9537 b->ignore_count = ignore_count;
41447f92 9538 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9539 b->disposition = disposition;
6c95b8df 9540
44f238bb
PA
9541 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9542 b->loc->inserted = 1;
9543
0fb4aa4b
PA
9544 if (type == bp_static_tracepoint)
9545 {
d9b3f62e 9546 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9547 struct static_tracepoint_marker marker;
9548
983af33b 9549 if (strace_marker_p (b))
0fb4aa4b
PA
9550 {
9551 /* We already know the marker exists, otherwise, we
9552 wouldn't see a sal for it. */
9553 char *p = &addr_string[3];
9554 char *endp;
9555 char *marker_str;
0fb4aa4b 9556
e9cafbcc 9557 p = skip_spaces (p);
0fb4aa4b 9558
e9cafbcc 9559 endp = skip_to_space (p);
0fb4aa4b
PA
9560
9561 marker_str = savestring (p, endp - p);
d9b3f62e 9562 t->static_trace_marker_id = marker_str;
0fb4aa4b 9563
3e43a32a
MS
9564 printf_filtered (_("Probed static tracepoint "
9565 "marker \"%s\"\n"),
d9b3f62e 9566 t->static_trace_marker_id);
0fb4aa4b
PA
9567 }
9568 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9569 {
d9b3f62e 9570 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9571 release_static_tracepoint_marker (&marker);
9572
3e43a32a
MS
9573 printf_filtered (_("Probed static tracepoint "
9574 "marker \"%s\"\n"),
d9b3f62e 9575 t->static_trace_marker_id);
0fb4aa4b
PA
9576 }
9577 else
3e43a32a
MS
9578 warning (_("Couldn't determine the static "
9579 "tracepoint marker to probe"));
0fb4aa4b
PA
9580 }
9581
0d381245
VP
9582 loc = b->loc;
9583 }
9584 else
018d34a4 9585 {
39d61571 9586 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9587 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9588 loc->inserted = 1;
0d381245
VP
9589 }
9590
9591 if (b->cond_string)
9592 {
bbc13ae3
KS
9593 const char *arg = b->cond_string;
9594
1bb9788d
TT
9595 loc->cond = parse_exp_1 (&arg, loc->address,
9596 block_for_pc (loc->address), 0);
0d381245 9597 if (*arg)
588ae58c 9598 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9599 }
e7e0cddf
SS
9600
9601 /* Dynamic printf requires and uses additional arguments on the
9602 command line, otherwise it's an error. */
9603 if (type == bp_dprintf)
9604 {
9605 if (b->extra_string)
9606 update_dprintf_command_list (b);
9607 else
9608 error (_("Format string required"));
9609 }
9610 else if (b->extra_string)
588ae58c 9611 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9612 }
018d34a4 9613
56435ebe 9614 b->display_canonical = display_canonical;
018d34a4
VP
9615 if (addr_string)
9616 b->addr_string = addr_string;
9617 else
9618 /* addr_string has to be used or breakpoint_re_set will delete
9619 me. */
5af949e3
UW
9620 b->addr_string
9621 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9622 b->filter = filter;
d9b3f62e 9623}
018d34a4 9624
d9b3f62e
PA
9625static void
9626create_breakpoint_sal (struct gdbarch *gdbarch,
9627 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9628 char *filter, char *cond_string,
e7e0cddf 9629 char *extra_string,
d9b3f62e
PA
9630 enum bptype type, enum bpdisp disposition,
9631 int thread, int task, int ignore_count,
c0a91b2b 9632 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9633 int enabled, int internal, unsigned flags,
9634 int display_canonical)
d9b3f62e
PA
9635{
9636 struct breakpoint *b;
9637 struct cleanup *old_chain;
9638
9639 if (is_tracepoint_type (type))
9640 {
9641 struct tracepoint *t;
9642
9643 t = XCNEW (struct tracepoint);
9644 b = &t->base;
9645 }
9646 else
9647 b = XNEW (struct breakpoint);
9648
9649 old_chain = make_cleanup (xfree, b);
9650
9651 init_breakpoint_sal (b, gdbarch,
9652 sals, addr_string,
e7e0cddf 9653 filter, cond_string, extra_string,
d9b3f62e
PA
9654 type, disposition,
9655 thread, task, ignore_count,
9656 ops, from_tty,
44f238bb
PA
9657 enabled, internal, flags,
9658 display_canonical);
d9b3f62e
PA
9659 discard_cleanups (old_chain);
9660
3ea46bff 9661 install_breakpoint (internal, b, 0);
018d34a4
VP
9662}
9663
9664/* Add SALS.nelts breakpoints to the breakpoint table. For each
9665 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9666 value. COND_STRING, if not NULL, specified the condition to be
9667 used for all breakpoints. Essentially the only case where
9668 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9669 function. In that case, it's still not possible to specify
9670 separate conditions for different overloaded functions, so
9671 we take just a single condition string.
9672
c3f6f71d 9673 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9674 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9675 array contents). If the function fails (error() is called), the
9676 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9677 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9678
9679static void
8cdf0e15 9680create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9681 struct linespec_result *canonical,
e7e0cddf 9682 char *cond_string, char *extra_string,
8cdf0e15
VP
9683 enum bptype type, enum bpdisp disposition,
9684 int thread, int task, int ignore_count,
c0a91b2b 9685 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9686 int enabled, int internal, unsigned flags)
c906108c 9687{
018d34a4 9688 int i;
f8eba3c6 9689 struct linespec_sals *lsal;
cc59ec59 9690
f8eba3c6
TT
9691 if (canonical->pre_expanded)
9692 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9693
9694 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9695 {
f8eba3c6
TT
9696 /* Note that 'addr_string' can be NULL in the case of a plain
9697 'break', without arguments. */
9698 char *addr_string = (canonical->addr_string
9699 ? xstrdup (canonical->addr_string)
9700 : NULL);
9701 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9702 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9703
f8eba3c6
TT
9704 make_cleanup (xfree, filter_string);
9705 create_breakpoint_sal (gdbarch, lsal->sals,
9706 addr_string,
9707 filter_string,
e7e0cddf
SS
9708 cond_string, extra_string,
9709 type, disposition,
84f4c1fe 9710 thread, task, ignore_count, ops,
44f238bb 9711 from_tty, enabled, internal, flags,
56435ebe 9712 canonical->special_display);
f8eba3c6 9713 discard_cleanups (inner);
c3f6f71d 9714 }
c3f6f71d 9715}
c906108c 9716
9998af43 9717/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9718 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9719 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9720 address strings. ADDRESS points to the end of the SAL.
9721
9722 The array and the line spec strings are allocated on the heap, it is
9723 the caller's responsibility to free them. */
c906108c 9724
b9362cc7 9725static void
c3f6f71d 9726parse_breakpoint_sals (char **address,
58438ac1 9727 struct linespec_result *canonical)
c3f6f71d 9728{
c3f6f71d 9729 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9730 breakpoint. */
c3f6f71d
JM
9731 if ((*address) == NULL
9732 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9733 {
1bfeeb0f
JL
9734 /* The last displayed codepoint, if it's valid, is our default breakpoint
9735 address. */
9736 if (last_displayed_sal_is_valid ())
c906108c 9737 {
f8eba3c6 9738 struct linespec_sals lsal;
c3f6f71d 9739 struct symtab_and_line sal;
1c8cdcb1 9740 CORE_ADDR pc;
cc59ec59 9741
4a64f543 9742 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9743 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9744 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9745
9746 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9747 corresponding to the last call to print_frame_info.
9748 Be sure to reinitialize LINE with NOTCURRENT == 0
9749 as the breakpoint line number is inappropriate otherwise.
9750 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9751 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9752 pc = sal.pc;
9753 sal = find_pc_line (pc, 0);
00903456 9754
4a64f543 9755 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9756 where PC is the last displayed codepoint's address. So
9757 make sure to set sal.explicit_pc to prevent GDB from
9758 trying to expand the list of sals to include all other
9759 instances with the same symtab and line. */
1c8cdcb1 9760 sal.pc = pc;
00903456
JK
9761 sal.explicit_pc = 1;
9762
f8eba3c6
TT
9763 lsal.sals.sals[0] = sal;
9764 lsal.sals.nelts = 1;
9765 lsal.canonical = NULL;
9766
9767 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9768 }
9769 else
8a3fe4f8 9770 error (_("No default breakpoint address now."));
c906108c
SS
9771 }
9772 else
9773 {
cc80f267
JK
9774 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9775
c906108c 9776 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9777 current_source_symtab (which is decode_line_1's default).
9778 This should produce the results we want almost all of the
cc80f267
JK
9779 time while leaving default_breakpoint_* alone.
9780
9781 ObjC: However, don't match an Objective-C method name which
9782 may have a '+' or '-' succeeded by a '['. */
9783 if (last_displayed_sal_is_valid ()
9784 && (!cursal.symtab
9785 || ((strchr ("+-", (*address)[0]) != NULL)
9786 && ((*address)[1] != '['))))
f8eba3c6
TT
9787 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9788 get_last_displayed_symtab (),
9789 get_last_displayed_line (),
9790 canonical, NULL, NULL);
c906108c 9791 else
f8eba3c6 9792 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9793 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9794 }
c3f6f71d 9795}
c906108c 9796
c906108c 9797
c3f6f71d 9798/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9799 inserted as a breakpoint. If it can't throw an error. */
c906108c 9800
b9362cc7 9801static void
23e7acfb 9802breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9803{
9804 int i;
cc59ec59 9805
c3f6f71d 9806 for (i = 0; i < sals->nelts; i++)
ee53e872 9807 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9808}
9809
7a697b8d
SS
9810/* Fast tracepoints may have restrictions on valid locations. For
9811 instance, a fast tracepoint using a jump instead of a trap will
9812 likely have to overwrite more bytes than a trap would, and so can
9813 only be placed where the instruction is longer than the jump, or a
9814 multi-instruction sequence does not have a jump into the middle of
9815 it, etc. */
9816
9817static void
9818check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9819 struct symtabs_and_lines *sals)
9820{
9821 int i, rslt;
9822 struct symtab_and_line *sal;
9823 char *msg;
9824 struct cleanup *old_chain;
9825
9826 for (i = 0; i < sals->nelts; i++)
9827 {
f8eba3c6
TT
9828 struct gdbarch *sarch;
9829
7a697b8d
SS
9830 sal = &sals->sals[i];
9831
f8eba3c6
TT
9832 sarch = get_sal_arch (*sal);
9833 /* We fall back to GDBARCH if there is no architecture
9834 associated with SAL. */
9835 if (sarch == NULL)
9836 sarch = gdbarch;
9837 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9838 NULL, &msg);
9839 old_chain = make_cleanup (xfree, msg);
9840
9841 if (!rslt)
9842 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9843 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9844
9845 do_cleanups (old_chain);
9846 }
9847}
9848
af4908ba
KS
9849/* Issue an invalid thread ID error. */
9850
9851static void ATTRIBUTE_NORETURN
9852invalid_thread_id_error (int id)
9853{
9854 error (_("Unknown thread %d."), id);
9855}
9856
018d34a4
VP
9857/* Given TOK, a string specification of condition and thread, as
9858 accepted by the 'break' command, extract the condition
9859 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9860 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9861 If no condition is found, *COND_STRING is set to NULL.
9862 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9863
9864static void
bbc13ae3 9865find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9866 char **cond_string, int *thread, int *task,
9867 char **rest)
018d34a4
VP
9868{
9869 *cond_string = NULL;
9870 *thread = -1;
ed1d1739
KS
9871 *task = 0;
9872 *rest = NULL;
9873
018d34a4
VP
9874 while (tok && *tok)
9875 {
bbc13ae3 9876 const char *end_tok;
018d34a4 9877 int toklen;
bbc13ae3
KS
9878 const char *cond_start = NULL;
9879 const char *cond_end = NULL;
cc59ec59 9880
bbc13ae3 9881 tok = skip_spaces_const (tok);
e7e0cddf
SS
9882
9883 if ((*tok == '"' || *tok == ',') && rest)
9884 {
9885 *rest = savestring (tok, strlen (tok));
9886 return;
9887 }
9888
bbc13ae3 9889 end_tok = skip_to_space_const (tok);
d634f2de 9890
018d34a4 9891 toklen = end_tok - tok;
d634f2de 9892
018d34a4
VP
9893 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9894 {
f7545552
TT
9895 struct expression *expr;
9896
018d34a4 9897 tok = cond_start = end_tok + 1;
1bb9788d 9898 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9899 xfree (expr);
018d34a4 9900 cond_end = tok;
d634f2de 9901 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9902 }
9903 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9904 {
9905 char *tmptok;
d634f2de 9906
018d34a4 9907 tok = end_tok + 1;
bbc13ae3 9908 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9909 if (tok == tmptok)
9910 error (_("Junk after thread keyword."));
9911 if (!valid_thread_id (*thread))
af4908ba 9912 invalid_thread_id_error (*thread);
bbc13ae3 9913 tok = tmptok;
018d34a4 9914 }
4a306c9a
JB
9915 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9916 {
9917 char *tmptok;
9918
9919 tok = end_tok + 1;
bbc13ae3 9920 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9921 if (tok == tmptok)
9922 error (_("Junk after task keyword."));
9923 if (!valid_task_id (*task))
b6199126 9924 error (_("Unknown task %d."), *task);
bbc13ae3 9925 tok = tmptok;
4a306c9a 9926 }
e7e0cddf
SS
9927 else if (rest)
9928 {
9929 *rest = savestring (tok, strlen (tok));
ccab2054 9930 return;
e7e0cddf 9931 }
018d34a4
VP
9932 else
9933 error (_("Junk at end of arguments."));
9934 }
9935}
9936
0fb4aa4b
PA
9937/* Decode a static tracepoint marker spec. */
9938
9939static struct symtabs_and_lines
9940decode_static_tracepoint_spec (char **arg_p)
9941{
9942 VEC(static_tracepoint_marker_p) *markers = NULL;
9943 struct symtabs_and_lines sals;
0fb4aa4b
PA
9944 struct cleanup *old_chain;
9945 char *p = &(*arg_p)[3];
9946 char *endp;
9947 char *marker_str;
9948 int i;
9949
e9cafbcc 9950 p = skip_spaces (p);
0fb4aa4b 9951
e9cafbcc 9952 endp = skip_to_space (p);
0fb4aa4b
PA
9953
9954 marker_str = savestring (p, endp - p);
9955 old_chain = make_cleanup (xfree, marker_str);
9956
9957 markers = target_static_tracepoint_markers_by_strid (marker_str);
9958 if (VEC_empty(static_tracepoint_marker_p, markers))
9959 error (_("No known static tracepoint marker named %s"), marker_str);
9960
9961 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9962 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9963
9964 for (i = 0; i < sals.nelts; i++)
9965 {
9966 struct static_tracepoint_marker *marker;
9967
9968 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9969
9970 init_sal (&sals.sals[i]);
9971
9972 sals.sals[i] = find_pc_line (marker->address, 0);
9973 sals.sals[i].pc = marker->address;
9974
9975 release_static_tracepoint_marker (marker);
9976 }
9977
9978 do_cleanups (old_chain);
9979
9980 *arg_p = endp;
9981 return sals;
9982}
9983
fd9b8c24
PA
9984/* Set a breakpoint. This function is shared between CLI and MI
9985 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9986 modes of operations, selected by the PARSE_ARG parameter. If
9987 non-zero, the function will parse ARG, extracting location,
9988 condition, thread and extra string. Otherwise, ARG is just the
9989 breakpoint's location, with condition, thread, and extra string
9990 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9991 If INTERNAL is non-zero, the breakpoint number will be allocated
9992 from the internal breakpoint count. Returns true if any breakpoint
9993 was created; false otherwise. */
0101ce28 9994
8cdf0e15
VP
9995int
9996create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9997 char *arg, char *cond_string,
9998 int thread, char *extra_string,
f6de8ec2 9999 int parse_arg,
0fb4aa4b 10000 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
10001 int ignore_count,
10002 enum auto_boolean pending_break_support,
c0a91b2b 10003 const struct breakpoint_ops *ops,
44f238bb
PA
10004 int from_tty, int enabled, int internal,
10005 unsigned flags)
c3f6f71d 10006{
b78a6381 10007 volatile struct gdb_exception e;
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
b78a6381
TT
10021 TRY_CATCH (e, RETURN_MASK_ALL)
10022 {
983af33b
SDJ
10023 ops->create_sals_from_address (&arg, &canonical, type_wanted,
10024 addr_start, &copy_arg);
b78a6381 10025 }
0101ce28
JJ
10026
10027 /* If caller is interested in rc value from parse, set value. */
05ff989b 10028 switch (e.reason)
0101ce28 10029 {
983af33b
SDJ
10030 case GDB_NO_ERROR:
10031 if (VEC_empty (linespec_sals, canonical.sals))
10032 return 0;
10033 break;
05ff989b
AC
10034 case RETURN_ERROR:
10035 switch (e.error)
0101ce28 10036 {
05ff989b 10037 case NOT_FOUND_ERROR:
0101ce28 10038
05ff989b
AC
10039 /* If pending breakpoint support is turned off, throw
10040 error. */
fa8d40ab
JJ
10041
10042 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
10043 throw_exception (e);
10044
10045 exception_print (gdb_stderr, e);
fa8d40ab 10046
05ff989b
AC
10047 /* If pending breakpoint support is auto query and the user
10048 selects no, then simply return the error code. */
059fb39f 10049 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
10050 && !nquery (_("Make %s pending on future shared library load? "),
10051 bptype_string (type_wanted)))
fd9b8c24 10052 return 0;
fa8d40ab 10053
05ff989b
AC
10054 /* At this point, either the user was queried about setting
10055 a pending breakpoint and selected yes, or pending
10056 breakpoint behavior is on and thus a pending breakpoint
10057 is defaulted on behalf of the user. */
f8eba3c6
TT
10058 {
10059 struct linespec_sals lsal;
10060
10061 copy_arg = xstrdup (addr_start);
10062 lsal.canonical = xstrdup (copy_arg);
10063 lsal.sals.nelts = 1;
10064 lsal.sals.sals = XNEW (struct symtab_and_line);
10065 init_sal (&lsal.sals.sals[0]);
10066 pending = 1;
10067 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
10068 }
05ff989b
AC
10069 break;
10070 default:
98deb0da 10071 throw_exception (e);
0101ce28 10072 }
2abae994 10073 break;
05ff989b 10074 default:
983af33b 10075 throw_exception (e);
0101ce28 10076 }
c3f6f71d 10077
4a64f543 10078 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 10079 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 10080
c3f6f71d
JM
10081 /* ----------------------------- SNIP -----------------------------
10082 Anything added to the cleanup chain beyond this point is assumed
10083 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
10084 then the memory is not reclaimed. */
10085 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 10086
c3f6f71d
JM
10087 /* Resolve all line numbers to PC's and verify that the addresses
10088 are ok for the target. */
0101ce28 10089 if (!pending)
f8eba3c6
TT
10090 {
10091 int ix;
10092 struct linespec_sals *iter;
10093
10094 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10095 breakpoint_sals_to_pc (&iter->sals);
10096 }
c3f6f71d 10097
7a697b8d 10098 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 10099 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
10100 {
10101 int ix;
10102 struct linespec_sals *iter;
10103
10104 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10105 check_fast_tracepoint_sals (gdbarch, &iter->sals);
10106 }
7a697b8d 10107
c3f6f71d
JM
10108 /* Verify that condition can be parsed, before setting any
10109 breakpoints. Allocate a separate condition expression for each
4a64f543 10110 breakpoint. */
0101ce28 10111 if (!pending)
c3f6f71d 10112 {
f6de8ec2 10113 if (parse_arg)
72b2ff0e 10114 {
0878d0fa 10115 char *rest;
52d361e1
YQ
10116 struct linespec_sals *lsal;
10117
10118 lsal = VEC_index (linespec_sals, canonical.sals, 0);
10119
0878d0fa
YQ
10120 /* Here we only parse 'arg' to separate condition
10121 from thread number, so parsing in context of first
10122 sal is OK. When setting the breakpoint we'll
10123 re-parse it in context of each sal. */
10124
10125 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
10126 &thread, &task, &rest);
10127 if (cond_string)
10128 make_cleanup (xfree, cond_string);
10129 if (rest)
10130 make_cleanup (xfree, rest);
10131 if (rest)
10132 extra_string = rest;
72b2ff0e 10133 }
2f069f6f 10134 else
72b2ff0e 10135 {
0878d0fa
YQ
10136 if (*arg != '\0')
10137 error (_("Garbage '%s' at end of location"), arg);
10138
10139 /* Create a private copy of condition string. */
10140 if (cond_string)
10141 {
10142 cond_string = xstrdup (cond_string);
10143 make_cleanup (xfree, cond_string);
10144 }
10145 /* Create a private copy of any extra string. */
10146 if (extra_string)
10147 {
10148 extra_string = xstrdup (extra_string);
10149 make_cleanup (xfree, extra_string);
10150 }
72b2ff0e 10151 }
0fb4aa4b 10152
52d361e1 10153 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 10154 cond_string, extra_string, type_wanted,
d9b3f62e
PA
10155 tempflag ? disp_del : disp_donttouch,
10156 thread, task, ignore_count, ops,
44f238bb 10157 from_tty, enabled, internal, flags);
c906108c 10158 }
0101ce28
JJ
10159 else
10160 {
0101ce28
JJ
10161 struct breakpoint *b;
10162
0101ce28
JJ
10163 make_cleanup (xfree, copy_arg);
10164
bfccc43c
YQ
10165 if (is_tracepoint_type (type_wanted))
10166 {
10167 struct tracepoint *t;
10168
10169 t = XCNEW (struct tracepoint);
10170 b = &t->base;
10171 }
10172 else
10173 b = XNEW (struct breakpoint);
10174
10175 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10176
f8eba3c6 10177 b->addr_string = copy_arg;
f6de8ec2 10178 if (parse_arg)
e12c7713
MK
10179 b->cond_string = NULL;
10180 else
10181 {
10182 /* Create a private copy of condition string. */
10183 if (cond_string)
10184 {
10185 cond_string = xstrdup (cond_string);
10186 make_cleanup (xfree, cond_string);
10187 }
10188 b->cond_string = cond_string;
10189 }
e7e0cddf 10190 b->extra_string = NULL;
0101ce28 10191 b->ignore_count = ignore_count;
0101ce28 10192 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 10193 b->condition_not_parsed = 1;
41447f92 10194 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
10195 if ((type_wanted != bp_breakpoint
10196 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 10197 b->pspace = current_program_space;
8bea4e01 10198
bfccc43c 10199 install_breakpoint (internal, b, 0);
0101ce28
JJ
10200 }
10201
f8eba3c6 10202 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 10203 {
3e43a32a
MS
10204 warning (_("Multiple breakpoints were set.\nUse the "
10205 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 10206 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
10207 }
10208
80c99de1
PA
10209 /* That's it. Discard the cleanups for data inserted into the
10210 breakpoint. */
10211 discard_cleanups (bkpt_chain);
10212 /* But cleanup everything else. */
c3f6f71d 10213 do_cleanups (old_chain);
217dc9e2 10214
80c99de1 10215 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 10216 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
10217
10218 return 1;
c3f6f71d 10219}
c906108c 10220
348d480f 10221/* Set a breakpoint.
72b2ff0e
VP
10222 ARG is a string describing breakpoint address,
10223 condition, and thread.
10224 FLAG specifies if a breakpoint is hardware on,
10225 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10226 and BP_TEMPFLAG. */
348d480f 10227
98deb0da 10228static void
72b2ff0e 10229break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10230{
72b2ff0e 10231 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10232 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10233 ? bp_hardware_breakpoint
10234 : bp_breakpoint);
55aa24fb
SDJ
10235 struct breakpoint_ops *ops;
10236 const char *arg_cp = arg;
10237
10238 /* Matching breakpoints on probes. */
10239 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10240 ops = &bkpt_probe_breakpoint_ops;
10241 else
10242 ops = &bkpt_breakpoint_ops;
c3f6f71d 10243
8cdf0e15
VP
10244 create_breakpoint (get_current_arch (),
10245 arg,
e7e0cddf 10246 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 10247 tempflag, type_wanted,
8cdf0e15
VP
10248 0 /* Ignore count */,
10249 pending_break_support,
55aa24fb 10250 ops,
8cdf0e15 10251 from_tty,
84f4c1fe 10252 1 /* enabled */,
44f238bb
PA
10253 0 /* internal */,
10254 0);
c906108c
SS
10255}
10256
c906108c
SS
10257/* Helper function for break_command_1 and disassemble_command. */
10258
10259void
fba45db2 10260resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10261{
10262 CORE_ADDR pc;
10263
10264 if (sal->pc == 0 && sal->symtab != NULL)
10265 {
10266 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10267 error (_("No line %d in file \"%s\"."),
05cba821 10268 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10269 sal->pc = pc;
6a048695 10270
4a64f543
MS
10271 /* If this SAL corresponds to a breakpoint inserted using a line
10272 number, then skip the function prologue if necessary. */
6a048695 10273 if (sal->explicit_line)
059acae7 10274 skip_prologue_sal (sal);
c906108c
SS
10275 }
10276
10277 if (sal->section == 0 && sal->symtab != NULL)
10278 {
346d1dfe 10279 const struct blockvector *bv;
3977b71f 10280 const struct block *b;
c5aa993b 10281 struct symbol *sym;
c906108c 10282
43f3e411
DE
10283 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10284 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
10285 if (bv != NULL)
10286 {
7f0df278 10287 sym = block_linkage_function (b);
c906108c
SS
10288 if (sym != NULL)
10289 {
eb822aa6
DE
10290 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10291 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10292 sym);
c906108c
SS
10293 }
10294 else
10295 {
4a64f543
MS
10296 /* It really is worthwhile to have the section, so we'll
10297 just have to look harder. This case can be executed
10298 if we have line numbers but no functions (as can
10299 happen in assembly source). */
c906108c 10300
7cbd4a93 10301 struct bound_minimal_symbol msym;
6c95b8df
PA
10302 struct cleanup *old_chain = save_current_space_and_thread ();
10303
10304 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10305
10306 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10307 if (msym.minsym)
efd66ac6 10308 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10309
10310 do_cleanups (old_chain);
c906108c
SS
10311 }
10312 }
10313 }
10314}
10315
10316void
fba45db2 10317break_command (char *arg, int from_tty)
c906108c 10318{
db107f19 10319 break_command_1 (arg, 0, from_tty);
c906108c
SS
10320}
10321
c906108c 10322void
fba45db2 10323tbreak_command (char *arg, int from_tty)
c906108c 10324{
db107f19 10325 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10326}
10327
c906108c 10328static void
fba45db2 10329hbreak_command (char *arg, int from_tty)
c906108c 10330{
db107f19 10331 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10332}
10333
10334static void
fba45db2 10335thbreak_command (char *arg, int from_tty)
c906108c 10336{
db107f19 10337 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10338}
10339
10340static void
fba45db2 10341stop_command (char *arg, int from_tty)
c906108c 10342{
a3f17187 10343 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10344Usage: stop in <function | address>\n\
a3f17187 10345 stop at <line>\n"));
c906108c
SS
10346}
10347
10348static void
fba45db2 10349stopin_command (char *arg, int from_tty)
c906108c
SS
10350{
10351 int badInput = 0;
10352
c5aa993b 10353 if (arg == (char *) NULL)
c906108c
SS
10354 badInput = 1;
10355 else if (*arg != '*')
10356 {
10357 char *argptr = arg;
10358 int hasColon = 0;
10359
4a64f543 10360 /* Look for a ':'. If this is a line number specification, then
53a5351d 10361 say it is bad, otherwise, it should be an address or
4a64f543 10362 function/method name. */
c906108c 10363 while (*argptr && !hasColon)
c5aa993b
JM
10364 {
10365 hasColon = (*argptr == ':');
10366 argptr++;
10367 }
c906108c
SS
10368
10369 if (hasColon)
c5aa993b 10370 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10371 else
c5aa993b 10372 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10373 }
10374
10375 if (badInput)
a3f17187 10376 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10377 else
db107f19 10378 break_command_1 (arg, 0, from_tty);
c906108c
SS
10379}
10380
10381static void
fba45db2 10382stopat_command (char *arg, int from_tty)
c906108c
SS
10383{
10384 int badInput = 0;
10385
c5aa993b 10386 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10387 badInput = 1;
10388 else
10389 {
10390 char *argptr = arg;
10391 int hasColon = 0;
10392
4a64f543
MS
10393 /* Look for a ':'. If there is a '::' then get out, otherwise
10394 it is probably a line number. */
c906108c 10395 while (*argptr && !hasColon)
c5aa993b
JM
10396 {
10397 hasColon = (*argptr == ':');
10398 argptr++;
10399 }
c906108c
SS
10400
10401 if (hasColon)
c5aa993b 10402 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10403 else
c5aa993b 10404 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10405 }
10406
10407 if (badInput)
a3f17187 10408 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10409 else
db107f19 10410 break_command_1 (arg, 0, from_tty);
c906108c
SS
10411}
10412
e7e0cddf
SS
10413/* The dynamic printf command is mostly like a regular breakpoint, but
10414 with a prewired command list consisting of a single output command,
10415 built from extra arguments supplied on the dprintf command
10416 line. */
10417
da821c7b 10418static void
e7e0cddf
SS
10419dprintf_command (char *arg, int from_tty)
10420{
10421 create_breakpoint (get_current_arch (),
10422 arg,
10423 NULL, 0, NULL, 1 /* parse arg */,
10424 0, bp_dprintf,
10425 0 /* Ignore count */,
10426 pending_break_support,
10427 &dprintf_breakpoint_ops,
10428 from_tty,
10429 1 /* enabled */,
10430 0 /* internal */,
10431 0);
10432}
10433
d3ce09f5
SS
10434static void
10435agent_printf_command (char *arg, int from_tty)
10436{
10437 error (_("May only run agent-printf on the target"));
10438}
10439
f1310107
TJB
10440/* Implement the "breakpoint_hit" breakpoint_ops method for
10441 ranged breakpoints. */
10442
10443static int
10444breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10445 struct address_space *aspace,
09ac7c10
TT
10446 CORE_ADDR bp_addr,
10447 const struct target_waitstatus *ws)
f1310107 10448{
09ac7c10 10449 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10450 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10451 return 0;
10452
f1310107
TJB
10453 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10454 bl->length, aspace, bp_addr);
10455}
10456
10457/* Implement the "resources_needed" breakpoint_ops method for
10458 ranged breakpoints. */
10459
10460static int
10461resources_needed_ranged_breakpoint (const struct bp_location *bl)
10462{
10463 return target_ranged_break_num_registers ();
10464}
10465
10466/* Implement the "print_it" breakpoint_ops method for
10467 ranged breakpoints. */
10468
10469static enum print_stop_action
348d480f 10470print_it_ranged_breakpoint (bpstat bs)
f1310107 10471{
348d480f 10472 struct breakpoint *b = bs->breakpoint_at;
f1310107 10473 struct bp_location *bl = b->loc;
79a45e25 10474 struct ui_out *uiout = current_uiout;
f1310107
TJB
10475
10476 gdb_assert (b->type == bp_hardware_breakpoint);
10477
10478 /* Ranged breakpoints have only one location. */
10479 gdb_assert (bl && bl->next == NULL);
10480
10481 annotate_breakpoint (b->number);
10482 if (b->disposition == disp_del)
10483 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10484 else
10485 ui_out_text (uiout, "\nRanged breakpoint ");
10486 if (ui_out_is_mi_like_p (uiout))
10487 {
10488 ui_out_field_string (uiout, "reason",
10489 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10490 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10491 }
10492 ui_out_field_int (uiout, "bkptno", b->number);
10493 ui_out_text (uiout, ", ");
10494
10495 return PRINT_SRC_AND_LOC;
10496}
10497
10498/* Implement the "print_one" breakpoint_ops method for
10499 ranged breakpoints. */
10500
10501static void
10502print_one_ranged_breakpoint (struct breakpoint *b,
10503 struct bp_location **last_loc)
10504{
10505 struct bp_location *bl = b->loc;
10506 struct value_print_options opts;
79a45e25 10507 struct ui_out *uiout = current_uiout;
f1310107
TJB
10508
10509 /* Ranged breakpoints have only one location. */
10510 gdb_assert (bl && bl->next == NULL);
10511
10512 get_user_print_options (&opts);
10513
10514 if (opts.addressprint)
10515 /* We don't print the address range here, it will be printed later
10516 by print_one_detail_ranged_breakpoint. */
10517 ui_out_field_skip (uiout, "addr");
10518 annotate_field (5);
10519 print_breakpoint_location (b, bl);
10520 *last_loc = bl;
10521}
10522
10523/* Implement the "print_one_detail" breakpoint_ops method for
10524 ranged breakpoints. */
10525
10526static void
10527print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10528 struct ui_out *uiout)
10529{
10530 CORE_ADDR address_start, address_end;
10531 struct bp_location *bl = b->loc;
f99d8bf4
PA
10532 struct ui_file *stb = mem_fileopen ();
10533 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10534
10535 gdb_assert (bl);
10536
10537 address_start = bl->address;
10538 address_end = address_start + bl->length - 1;
10539
10540 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10541 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10542 print_core_address (bl->gdbarch, address_start),
10543 print_core_address (bl->gdbarch, address_end));
10544 ui_out_field_stream (uiout, "addr", stb);
10545 ui_out_text (uiout, "\n");
10546
10547 do_cleanups (cleanup);
10548}
10549
10550/* Implement the "print_mention" breakpoint_ops method for
10551 ranged breakpoints. */
10552
10553static void
10554print_mention_ranged_breakpoint (struct breakpoint *b)
10555{
10556 struct bp_location *bl = b->loc;
79a45e25 10557 struct ui_out *uiout = current_uiout;
f1310107
TJB
10558
10559 gdb_assert (bl);
10560 gdb_assert (b->type == bp_hardware_breakpoint);
10561
10562 if (ui_out_is_mi_like_p (uiout))
10563 return;
10564
10565 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10566 b->number, paddress (bl->gdbarch, bl->address),
10567 paddress (bl->gdbarch, bl->address + bl->length - 1));
10568}
10569
10570/* Implement the "print_recreate" breakpoint_ops method for
10571 ranged breakpoints. */
10572
10573static void
10574print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10575{
10576 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10577 b->addr_string_range_end);
d9b3f62e 10578 print_recreate_thread (b, fp);
f1310107
TJB
10579}
10580
10581/* The breakpoint_ops structure to be used in ranged breakpoints. */
10582
2060206e 10583static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10584
10585/* Find the address where the end of the breakpoint range should be
10586 placed, given the SAL of the end of the range. This is so that if
10587 the user provides a line number, the end of the range is set to the
10588 last instruction of the given line. */
10589
10590static CORE_ADDR
10591find_breakpoint_range_end (struct symtab_and_line sal)
10592{
10593 CORE_ADDR end;
10594
10595 /* If the user provided a PC value, use it. Otherwise,
10596 find the address of the end of the given location. */
10597 if (sal.explicit_pc)
10598 end = sal.pc;
10599 else
10600 {
10601 int ret;
10602 CORE_ADDR start;
10603
10604 ret = find_line_pc_range (sal, &start, &end);
10605 if (!ret)
10606 error (_("Could not find location of the end of the range."));
10607
10608 /* find_line_pc_range returns the start of the next line. */
10609 end--;
10610 }
10611
10612 return end;
10613}
10614
10615/* Implement the "break-range" CLI command. */
10616
10617static void
10618break_range_command (char *arg, int from_tty)
10619{
10620 char *arg_start, *addr_string_start, *addr_string_end;
10621 struct linespec_result canonical_start, canonical_end;
10622 int bp_count, can_use_bp, length;
10623 CORE_ADDR end;
10624 struct breakpoint *b;
10625 struct symtab_and_line sal_start, sal_end;
f1310107 10626 struct cleanup *cleanup_bkpt;
f8eba3c6 10627 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10628
10629 /* We don't support software ranged breakpoints. */
10630 if (target_ranged_break_num_registers () < 0)
10631 error (_("This target does not support hardware ranged breakpoints."));
10632
10633 bp_count = hw_breakpoint_used_count ();
10634 bp_count += target_ranged_break_num_registers ();
10635 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10636 bp_count, 0);
10637 if (can_use_bp < 0)
10638 error (_("Hardware breakpoints used exceeds limit."));
10639
f8eba3c6 10640 arg = skip_spaces (arg);
f1310107
TJB
10641 if (arg == NULL || arg[0] == '\0')
10642 error(_("No address range specified."));
10643
f1310107
TJB
10644 init_linespec_result (&canonical_start);
10645
f8eba3c6
TT
10646 arg_start = arg;
10647 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10648
f8eba3c6 10649 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10650
10651 if (arg[0] != ',')
10652 error (_("Too few arguments."));
f8eba3c6 10653 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10654 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10655
10656 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10657
10658 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10659 || lsal_start->sals.nelts != 1)
f1310107
TJB
10660 error (_("Cannot create a ranged breakpoint with multiple locations."));
10661
f8eba3c6
TT
10662 sal_start = lsal_start->sals.sals[0];
10663 addr_string_start = savestring (arg_start, arg - arg_start);
10664 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10665
10666 arg++; /* Skip the comma. */
f8eba3c6 10667 arg = skip_spaces (arg);
f1310107
TJB
10668
10669 /* Parse the end location. */
10670
f1310107
TJB
10671 init_linespec_result (&canonical_end);
10672 arg_start = arg;
10673
f8eba3c6 10674 /* We call decode_line_full directly here instead of using
f1310107
TJB
10675 parse_breakpoint_sals because we need to specify the start location's
10676 symtab and line as the default symtab and line for the end of the
10677 range. This makes it possible to have ranges like "foo.c:27, +14",
10678 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10679 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10680 sal_start.symtab, sal_start.line,
10681 &canonical_end, NULL, NULL);
10682
10683 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10684
f8eba3c6 10685 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10686 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10687
10688 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10689 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10690 || lsal_end->sals.nelts != 1)
f1310107
TJB
10691 error (_("Cannot create a ranged breakpoint with multiple locations."));
10692
f8eba3c6
TT
10693 sal_end = lsal_end->sals.sals[0];
10694 addr_string_end = savestring (arg_start, arg - arg_start);
10695 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10696
10697 end = find_breakpoint_range_end (sal_end);
10698 if (sal_start.pc > end)
177b42fe 10699 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10700
10701 length = end - sal_start.pc + 1;
10702 if (length < 0)
10703 /* Length overflowed. */
10704 error (_("Address range too large."));
10705 else if (length == 1)
10706 {
10707 /* This range is simple enough to be handled by
10708 the `hbreak' command. */
10709 hbreak_command (addr_string_start, 1);
10710
10711 do_cleanups (cleanup_bkpt);
10712
10713 return;
10714 }
10715
10716 /* Now set up the breakpoint. */
10717 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10718 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10719 set_breakpoint_count (breakpoint_count + 1);
10720 b->number = breakpoint_count;
10721 b->disposition = disp_donttouch;
f8eba3c6
TT
10722 b->addr_string = xstrdup (addr_string_start);
10723 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10724 b->loc->length = length;
10725
f8eba3c6 10726 do_cleanups (cleanup_bkpt);
f1310107
TJB
10727
10728 mention (b);
8d3788bd 10729 observer_notify_breakpoint_created (b);
44702360 10730 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10731}
10732
4a64f543
MS
10733/* Return non-zero if EXP is verified as constant. Returned zero
10734 means EXP is variable. Also the constant detection may fail for
10735 some constant expressions and in such case still falsely return
10736 zero. */
2e6e3d9c 10737
65d79d4b
SDJ
10738static int
10739watchpoint_exp_is_const (const struct expression *exp)
10740{
10741 int i = exp->nelts;
10742
10743 while (i > 0)
10744 {
10745 int oplenp, argsp;
10746
10747 /* We are only interested in the descriptor of each element. */
10748 operator_length (exp, i, &oplenp, &argsp);
10749 i -= oplenp;
10750
10751 switch (exp->elts[i].opcode)
10752 {
10753 case BINOP_ADD:
10754 case BINOP_SUB:
10755 case BINOP_MUL:
10756 case BINOP_DIV:
10757 case BINOP_REM:
10758 case BINOP_MOD:
10759 case BINOP_LSH:
10760 case BINOP_RSH:
10761 case BINOP_LOGICAL_AND:
10762 case BINOP_LOGICAL_OR:
10763 case BINOP_BITWISE_AND:
10764 case BINOP_BITWISE_IOR:
10765 case BINOP_BITWISE_XOR:
10766 case BINOP_EQUAL:
10767 case BINOP_NOTEQUAL:
10768 case BINOP_LESS:
10769 case BINOP_GTR:
10770 case BINOP_LEQ:
10771 case BINOP_GEQ:
10772 case BINOP_REPEAT:
10773 case BINOP_COMMA:
10774 case BINOP_EXP:
10775 case BINOP_MIN:
10776 case BINOP_MAX:
10777 case BINOP_INTDIV:
10778 case BINOP_CONCAT:
65d79d4b
SDJ
10779 case TERNOP_COND:
10780 case TERNOP_SLICE:
65d79d4b
SDJ
10781
10782 case OP_LONG:
10783 case OP_DOUBLE:
10784 case OP_DECFLOAT:
10785 case OP_LAST:
10786 case OP_COMPLEX:
10787 case OP_STRING:
65d79d4b
SDJ
10788 case OP_ARRAY:
10789 case OP_TYPE:
608b4967
TT
10790 case OP_TYPEOF:
10791 case OP_DECLTYPE:
6e72ca20 10792 case OP_TYPEID:
65d79d4b
SDJ
10793 case OP_NAME:
10794 case OP_OBJC_NSSTRING:
10795
10796 case UNOP_NEG:
10797 case UNOP_LOGICAL_NOT:
10798 case UNOP_COMPLEMENT:
10799 case UNOP_ADDR:
10800 case UNOP_HIGH:
aeaa2474 10801 case UNOP_CAST:
9eaf6705
TT
10802
10803 case UNOP_CAST_TYPE:
10804 case UNOP_REINTERPRET_CAST:
10805 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10806 /* Unary, binary and ternary operators: We have to check
10807 their operands. If they are constant, then so is the
10808 result of that operation. For instance, if A and B are
10809 determined to be constants, then so is "A + B".
10810
10811 UNOP_IND is one exception to the rule above, because the
10812 value of *ADDR is not necessarily a constant, even when
10813 ADDR is. */
65d79d4b
SDJ
10814 break;
10815
10816 case OP_VAR_VALUE:
10817 /* Check whether the associated symbol is a constant.
4a64f543 10818
65d79d4b 10819 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10820 possible that a buggy compiler could mark a variable as
10821 constant even when it is not, and TYPE_CONST would return
10822 true in this case, while SYMBOL_CLASS wouldn't.
10823
10824 We also have to check for function symbols because they
10825 are always constant. */
65d79d4b
SDJ
10826 {
10827 struct symbol *s = exp->elts[i + 2].symbol;
10828
10829 if (SYMBOL_CLASS (s) != LOC_BLOCK
10830 && SYMBOL_CLASS (s) != LOC_CONST
10831 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10832 return 0;
10833 break;
10834 }
10835
10836 /* The default action is to return 0 because we are using
10837 the optimistic approach here: If we don't know something,
10838 then it is not a constant. */
10839 default:
10840 return 0;
10841 }
10842 }
10843
10844 return 1;
10845}
10846
3a5c3e22
PA
10847/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10848
10849static void
10850dtor_watchpoint (struct breakpoint *self)
10851{
10852 struct watchpoint *w = (struct watchpoint *) self;
10853
10854 xfree (w->cond_exp);
10855 xfree (w->exp);
10856 xfree (w->exp_string);
10857 xfree (w->exp_string_reparse);
10858 value_free (w->val);
10859
10860 base_breakpoint_ops.dtor (self);
10861}
10862
348d480f
PA
10863/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10864
10865static void
10866re_set_watchpoint (struct breakpoint *b)
10867{
3a5c3e22
PA
10868 struct watchpoint *w = (struct watchpoint *) b;
10869
348d480f
PA
10870 /* Watchpoint can be either on expression using entirely global
10871 variables, or it can be on local variables.
10872
10873 Watchpoints of the first kind are never auto-deleted, and even
10874 persist across program restarts. Since they can use variables
10875 from shared libraries, we need to reparse expression as libraries
10876 are loaded and unloaded.
10877
10878 Watchpoints on local variables can also change meaning as result
10879 of solib event. For example, if a watchpoint uses both a local
10880 and a global variables in expression, it's a local watchpoint,
10881 but unloading of a shared library will make the expression
10882 invalid. This is not a very common use case, but we still
10883 re-evaluate expression, to avoid surprises to the user.
10884
10885 Note that for local watchpoints, we re-evaluate it only if
10886 watchpoints frame id is still valid. If it's not, it means the
10887 watchpoint is out of scope and will be deleted soon. In fact,
10888 I'm not sure we'll ever be called in this case.
10889
10890 If a local watchpoint's frame id is still valid, then
3a5c3e22 10891 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10892
3a5c3e22
PA
10893 Don't do anything about disabled watchpoints, since they will be
10894 reevaluated again when enabled. */
10895 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10896}
10897
77b06cd7
TJB
10898/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10899
10900static int
10901insert_watchpoint (struct bp_location *bl)
10902{
3a5c3e22
PA
10903 struct watchpoint *w = (struct watchpoint *) bl->owner;
10904 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10905
10906 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10907 w->cond_exp);
77b06cd7
TJB
10908}
10909
10910/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10911
10912static int
10913remove_watchpoint (struct bp_location *bl)
10914{
3a5c3e22
PA
10915 struct watchpoint *w = (struct watchpoint *) bl->owner;
10916 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10917
10918 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10919 w->cond_exp);
e09342b5
TJB
10920}
10921
e09342b5 10922static int
348d480f 10923breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10924 struct address_space *aspace, CORE_ADDR bp_addr,
10925 const struct target_waitstatus *ws)
e09342b5 10926{
348d480f 10927 struct breakpoint *b = bl->owner;
3a5c3e22 10928 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10929
348d480f
PA
10930 /* Continuable hardware watchpoints are treated as non-existent if the
10931 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10932 some data address). Otherwise gdb won't stop on a break instruction
10933 in the code (not from a breakpoint) when a hardware watchpoint has
10934 been defined. Also skip watchpoints which we know did not trigger
10935 (did not match the data address). */
10936 if (is_hardware_watchpoint (b)
3a5c3e22 10937 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10938 return 0;
9c06b0b4 10939
348d480f 10940 return 1;
9c06b0b4
TJB
10941}
10942
348d480f
PA
10943static void
10944check_status_watchpoint (bpstat bs)
9c06b0b4 10945{
348d480f 10946 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10947
348d480f 10948 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10949}
10950
10951/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10952 hardware watchpoints. */
9c06b0b4
TJB
10953
10954static int
348d480f 10955resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10956{
3a5c3e22
PA
10957 struct watchpoint *w = (struct watchpoint *) bl->owner;
10958 int length = w->exact? 1 : bl->length;
348d480f
PA
10959
10960 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10961}
10962
10963/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10964 hardware watchpoints. */
9c06b0b4
TJB
10965
10966static int
348d480f 10967works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10968{
efa80663
PA
10969 /* Read and access watchpoints only work with hardware support. */
10970 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10971}
10972
9c06b0b4 10973static enum print_stop_action
348d480f 10974print_it_watchpoint (bpstat bs)
9c06b0b4 10975{
348d480f
PA
10976 struct cleanup *old_chain;
10977 struct breakpoint *b;
f99d8bf4 10978 struct ui_file *stb;
348d480f 10979 enum print_stop_action result;
3a5c3e22 10980 struct watchpoint *w;
79a45e25 10981 struct ui_out *uiout = current_uiout;
348d480f
PA
10982
10983 gdb_assert (bs->bp_location_at != NULL);
10984
348d480f 10985 b = bs->breakpoint_at;
3a5c3e22 10986 w = (struct watchpoint *) b;
348d480f 10987
f99d8bf4
PA
10988 stb = mem_fileopen ();
10989 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10990
10991 switch (b->type)
10992 {
348d480f 10993 case bp_watchpoint:
9c06b0b4
TJB
10994 case bp_hardware_watchpoint:
10995 annotate_watchpoint (b->number);
10996 if (ui_out_is_mi_like_p (uiout))
10997 ui_out_field_string
10998 (uiout, "reason",
10999 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
11000 mention (b);
11001 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11002 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 11003 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
11004 ui_out_field_stream (uiout, "old", stb);
11005 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 11006 watchpoint_value_print (w->val, stb);
348d480f
PA
11007 ui_out_field_stream (uiout, "new", stb);
11008 ui_out_text (uiout, "\n");
11009 /* More than one watchpoint may have been triggered. */
11010 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11011 break;
11012
11013 case bp_read_watchpoint:
11014 if (ui_out_is_mi_like_p (uiout))
11015 ui_out_field_string
11016 (uiout, "reason",
11017 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
11018 mention (b);
11019 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11020 ui_out_text (uiout, "\nValue = ");
f99d8bf4 11021 watchpoint_value_print (w->val, stb);
348d480f
PA
11022 ui_out_field_stream (uiout, "value", stb);
11023 ui_out_text (uiout, "\n");
11024 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11025 break;
11026
11027 case bp_access_watchpoint:
348d480f
PA
11028 if (bs->old_val != NULL)
11029 {
11030 annotate_watchpoint (b->number);
11031 if (ui_out_is_mi_like_p (uiout))
11032 ui_out_field_string
11033 (uiout, "reason",
11034 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11035 mention (b);
11036 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11037 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 11038 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
11039 ui_out_field_stream (uiout, "old", stb);
11040 ui_out_text (uiout, "\nNew value = ");
11041 }
11042 else
11043 {
11044 mention (b);
11045 if (ui_out_is_mi_like_p (uiout))
11046 ui_out_field_string
11047 (uiout, "reason",
11048 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11049 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11050 ui_out_text (uiout, "\nValue = ");
11051 }
f99d8bf4 11052 watchpoint_value_print (w->val, stb);
348d480f
PA
11053 ui_out_field_stream (uiout, "new", stb);
11054 ui_out_text (uiout, "\n");
11055 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11056 break;
11057 default:
348d480f 11058 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11059 }
11060
348d480f
PA
11061 do_cleanups (old_chain);
11062 return result;
11063}
11064
11065/* Implement the "print_mention" breakpoint_ops method for hardware
11066 watchpoints. */
11067
11068static void
11069print_mention_watchpoint (struct breakpoint *b)
11070{
11071 struct cleanup *ui_out_chain;
3a5c3e22 11072 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11073 struct ui_out *uiout = current_uiout;
348d480f
PA
11074
11075 switch (b->type)
11076 {
11077 case bp_watchpoint:
11078 ui_out_text (uiout, "Watchpoint ");
11079 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11080 break;
11081 case bp_hardware_watchpoint:
11082 ui_out_text (uiout, "Hardware watchpoint ");
11083 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11084 break;
11085 case bp_read_watchpoint:
11086 ui_out_text (uiout, "Hardware read watchpoint ");
11087 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11088 break;
11089 case bp_access_watchpoint:
11090 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
11091 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11092 break;
11093 default:
11094 internal_error (__FILE__, __LINE__,
11095 _("Invalid hardware watchpoint type."));
11096 }
11097
11098 ui_out_field_int (uiout, "number", b->number);
11099 ui_out_text (uiout, ": ");
3a5c3e22 11100 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
11101 do_cleanups (ui_out_chain);
11102}
11103
11104/* Implement the "print_recreate" breakpoint_ops method for
11105 watchpoints. */
11106
11107static void
11108print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
11109{
3a5c3e22
PA
11110 struct watchpoint *w = (struct watchpoint *) b;
11111
348d480f
PA
11112 switch (b->type)
11113 {
11114 case bp_watchpoint:
11115 case bp_hardware_watchpoint:
11116 fprintf_unfiltered (fp, "watch");
11117 break;
11118 case bp_read_watchpoint:
11119 fprintf_unfiltered (fp, "rwatch");
11120 break;
11121 case bp_access_watchpoint:
11122 fprintf_unfiltered (fp, "awatch");
11123 break;
11124 default:
11125 internal_error (__FILE__, __LINE__,
11126 _("Invalid watchpoint type."));
11127 }
11128
3a5c3e22 11129 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 11130 print_recreate_thread (b, fp);
348d480f
PA
11131}
11132
427cd150
TT
11133/* Implement the "explains_signal" breakpoint_ops method for
11134 watchpoints. */
11135
47591c29 11136static int
427cd150
TT
11137explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
11138{
11139 /* A software watchpoint cannot cause a signal other than
11140 GDB_SIGNAL_TRAP. */
11141 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 11142 return 0;
427cd150 11143
47591c29 11144 return 1;
427cd150
TT
11145}
11146
348d480f
PA
11147/* The breakpoint_ops structure to be used in hardware watchpoints. */
11148
2060206e 11149static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
11150
11151/* Implement the "insert" breakpoint_ops method for
11152 masked hardware watchpoints. */
11153
11154static int
11155insert_masked_watchpoint (struct bp_location *bl)
11156{
3a5c3e22
PA
11157 struct watchpoint *w = (struct watchpoint *) bl->owner;
11158
11159 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11160 bl->watchpoint_type);
11161}
11162
11163/* Implement the "remove" breakpoint_ops method for
11164 masked hardware watchpoints. */
11165
11166static int
11167remove_masked_watchpoint (struct bp_location *bl)
11168{
3a5c3e22
PA
11169 struct watchpoint *w = (struct watchpoint *) bl->owner;
11170
11171 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11172 bl->watchpoint_type);
11173}
11174
11175/* Implement the "resources_needed" breakpoint_ops method for
11176 masked hardware watchpoints. */
11177
11178static int
11179resources_needed_masked_watchpoint (const struct bp_location *bl)
11180{
3a5c3e22
PA
11181 struct watchpoint *w = (struct watchpoint *) bl->owner;
11182
11183 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
11184}
11185
11186/* Implement the "works_in_software_mode" breakpoint_ops method for
11187 masked hardware watchpoints. */
11188
11189static int
11190works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11191{
11192 return 0;
11193}
11194
11195/* Implement the "print_it" breakpoint_ops method for
11196 masked hardware watchpoints. */
11197
11198static enum print_stop_action
11199print_it_masked_watchpoint (bpstat bs)
11200{
11201 struct breakpoint *b = bs->breakpoint_at;
79a45e25 11202 struct ui_out *uiout = current_uiout;
348d480f
PA
11203
11204 /* Masked watchpoints have only one location. */
11205 gdb_assert (b->loc && b->loc->next == NULL);
11206
11207 switch (b->type)
11208 {
11209 case bp_hardware_watchpoint:
11210 annotate_watchpoint (b->number);
11211 if (ui_out_is_mi_like_p (uiout))
11212 ui_out_field_string
11213 (uiout, "reason",
11214 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11215 break;
11216
11217 case bp_read_watchpoint:
11218 if (ui_out_is_mi_like_p (uiout))
11219 ui_out_field_string
11220 (uiout, "reason",
11221 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11222 break;
11223
11224 case bp_access_watchpoint:
11225 if (ui_out_is_mi_like_p (uiout))
11226 ui_out_field_string
11227 (uiout, "reason",
11228 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11229 break;
11230 default:
11231 internal_error (__FILE__, __LINE__,
11232 _("Invalid hardware watchpoint type."));
11233 }
11234
11235 mention (b);
9c06b0b4
TJB
11236 ui_out_text (uiout, _("\n\
11237Check the underlying instruction at PC for the memory\n\
11238address and value which triggered this watchpoint.\n"));
11239 ui_out_text (uiout, "\n");
11240
11241 /* More than one watchpoint may have been triggered. */
11242 return PRINT_UNKNOWN;
11243}
11244
11245/* Implement the "print_one_detail" breakpoint_ops method for
11246 masked hardware watchpoints. */
11247
11248static void
11249print_one_detail_masked_watchpoint (const struct breakpoint *b,
11250 struct ui_out *uiout)
11251{
3a5c3e22
PA
11252 struct watchpoint *w = (struct watchpoint *) b;
11253
9c06b0b4
TJB
11254 /* Masked watchpoints have only one location. */
11255 gdb_assert (b->loc && b->loc->next == NULL);
11256
11257 ui_out_text (uiout, "\tmask ");
3a5c3e22 11258 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11259 ui_out_text (uiout, "\n");
11260}
11261
11262/* Implement the "print_mention" breakpoint_ops method for
11263 masked hardware watchpoints. */
11264
11265static void
11266print_mention_masked_watchpoint (struct breakpoint *b)
11267{
3a5c3e22 11268 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11269 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11270 struct cleanup *ui_out_chain;
11271
11272 switch (b->type)
11273 {
11274 case bp_hardware_watchpoint:
11275 ui_out_text (uiout, "Masked hardware watchpoint ");
11276 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11277 break;
11278 case bp_read_watchpoint:
11279 ui_out_text (uiout, "Masked hardware read watchpoint ");
11280 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11281 break;
11282 case bp_access_watchpoint:
11283 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11284 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11285 break;
11286 default:
11287 internal_error (__FILE__, __LINE__,
11288 _("Invalid hardware watchpoint type."));
11289 }
11290
11291 ui_out_field_int (uiout, "number", b->number);
11292 ui_out_text (uiout, ": ");
3a5c3e22 11293 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11294 do_cleanups (ui_out_chain);
11295}
11296
11297/* Implement the "print_recreate" breakpoint_ops method for
11298 masked hardware watchpoints. */
11299
11300static void
11301print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11302{
3a5c3e22 11303 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11304 char tmp[40];
11305
11306 switch (b->type)
11307 {
11308 case bp_hardware_watchpoint:
11309 fprintf_unfiltered (fp, "watch");
11310 break;
11311 case bp_read_watchpoint:
11312 fprintf_unfiltered (fp, "rwatch");
11313 break;
11314 case bp_access_watchpoint:
11315 fprintf_unfiltered (fp, "awatch");
11316 break;
11317 default:
11318 internal_error (__FILE__, __LINE__,
11319 _("Invalid hardware watchpoint type."));
11320 }
11321
3a5c3e22
PA
11322 sprintf_vma (tmp, w->hw_wp_mask);
11323 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11324 print_recreate_thread (b, fp);
9c06b0b4
TJB
11325}
11326
11327/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11328
2060206e 11329static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11330
11331/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11332
11333static int
11334is_masked_watchpoint (const struct breakpoint *b)
11335{
11336 return b->ops == &masked_watchpoint_breakpoint_ops;
11337}
11338
53a5351d
JM
11339/* accessflag: hw_write: watch write,
11340 hw_read: watch read,
11341 hw_access: watch access (read or write) */
c906108c 11342static void
bbc13ae3 11343watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11344 int just_location, int internal)
c906108c 11345{
a9634178 11346 volatile struct gdb_exception e;
d983da9c 11347 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11348 struct expression *exp;
270140bd 11349 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11350 struct value *val, *mark, *result;
bb9d5f81 11351 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11352 struct frame_info *frame;
bbc13ae3
KS
11353 const char *exp_start = NULL;
11354 const char *exp_end = NULL;
11355 const char *tok, *end_tok;
9c06b0b4 11356 int toklen = -1;
bbc13ae3
KS
11357 const char *cond_start = NULL;
11358 const char *cond_end = NULL;
c906108c 11359 enum bptype bp_type;
37e4754d 11360 int thread = -1;
0cf6dd15 11361 int pc = 0;
9c06b0b4
TJB
11362 /* Flag to indicate whether we are going to use masks for
11363 the hardware watchpoint. */
11364 int use_mask = 0;
11365 CORE_ADDR mask = 0;
3a5c3e22 11366 struct watchpoint *w;
bbc13ae3
KS
11367 char *expression;
11368 struct cleanup *back_to;
c906108c 11369
37e4754d
LM
11370 /* Make sure that we actually have parameters to parse. */
11371 if (arg != NULL && arg[0] != '\0')
11372 {
bbc13ae3
KS
11373 const char *value_start;
11374
11375 exp_end = arg + strlen (arg);
37e4754d 11376
9c06b0b4
TJB
11377 /* Look for "parameter value" pairs at the end
11378 of the arguments string. */
bbc13ae3 11379 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11380 {
11381 /* Skip whitespace at the end of the argument list. */
11382 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11383 tok--;
11384
11385 /* Find the beginning of the last token.
11386 This is the value of the parameter. */
11387 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11388 tok--;
11389 value_start = tok + 1;
11390
11391 /* Skip whitespace. */
11392 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11393 tok--;
11394
11395 end_tok = tok;
11396
11397 /* Find the beginning of the second to last token.
11398 This is the parameter itself. */
11399 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11400 tok--;
11401 tok++;
11402 toklen = end_tok - tok + 1;
11403
11404 if (toklen == 6 && !strncmp (tok, "thread", 6))
11405 {
11406 /* At this point we've found a "thread" token, which means
11407 the user is trying to set a watchpoint that triggers
11408 only in a specific thread. */
11409 char *endp;
37e4754d 11410
9c06b0b4
TJB
11411 if (thread != -1)
11412 error(_("You can specify only one thread."));
37e4754d 11413
9c06b0b4
TJB
11414 /* Extract the thread ID from the next token. */
11415 thread = strtol (value_start, &endp, 0);
37e4754d 11416
9c06b0b4
TJB
11417 /* Check if the user provided a valid numeric value for the
11418 thread ID. */
11419 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11420 error (_("Invalid thread ID specification %s."), value_start);
11421
11422 /* Check if the thread actually exists. */
11423 if (!valid_thread_id (thread))
af4908ba 11424 invalid_thread_id_error (thread);
9c06b0b4
TJB
11425 }
11426 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11427 {
11428 /* We've found a "mask" token, which means the user wants to
11429 create a hardware watchpoint that is going to have the mask
11430 facility. */
11431 struct value *mask_value, *mark;
37e4754d 11432
9c06b0b4
TJB
11433 if (use_mask)
11434 error(_("You can specify only one mask."));
37e4754d 11435
9c06b0b4 11436 use_mask = just_location = 1;
37e4754d 11437
9c06b0b4
TJB
11438 mark = value_mark ();
11439 mask_value = parse_to_comma_and_eval (&value_start);
11440 mask = value_as_address (mask_value);
11441 value_free_to_mark (mark);
11442 }
11443 else
11444 /* We didn't recognize what we found. We should stop here. */
11445 break;
37e4754d 11446
9c06b0b4
TJB
11447 /* Truncate the string and get rid of the "parameter value" pair before
11448 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11449 exp_end = tok;
9c06b0b4 11450 }
37e4754d 11451 }
bbc13ae3
KS
11452 else
11453 exp_end = arg;
37e4754d 11454
bbc13ae3
KS
11455 /* Parse the rest of the arguments. From here on out, everything
11456 is in terms of a newly allocated string instead of the original
11457 ARG. */
c906108c 11458 innermost_block = NULL;
bbc13ae3
KS
11459 expression = savestring (arg, exp_end - arg);
11460 back_to = make_cleanup (xfree, expression);
11461 exp_start = arg = expression;
1bb9788d 11462 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11463 exp_end = arg;
fa8a61dc
TT
11464 /* Remove trailing whitespace from the expression before saving it.
11465 This makes the eventual display of the expression string a bit
11466 prettier. */
11467 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11468 --exp_end;
11469
65d79d4b
SDJ
11470 /* Checking if the expression is not constant. */
11471 if (watchpoint_exp_is_const (exp))
11472 {
11473 int len;
11474
11475 len = exp_end - exp_start;
11476 while (len > 0 && isspace (exp_start[len - 1]))
11477 len--;
11478 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11479 }
11480
c906108c
SS
11481 exp_valid_block = innermost_block;
11482 mark = value_mark ();
3a1115a0 11483 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11484
bb9d5f81
PP
11485 if (val != NULL && just_location)
11486 {
11487 saved_bitpos = value_bitpos (val);
11488 saved_bitsize = value_bitsize (val);
11489 }
11490
06a64a0b
TT
11491 if (just_location)
11492 {
9c06b0b4
TJB
11493 int ret;
11494
06a64a0b 11495 exp_valid_block = NULL;
a1442452 11496 val = value_addr (result);
06a64a0b
TT
11497 release_value (val);
11498 value_free_to_mark (mark);
9c06b0b4
TJB
11499
11500 if (use_mask)
11501 {
11502 ret = target_masked_watch_num_registers (value_as_address (val),
11503 mask);
11504 if (ret == -1)
11505 error (_("This target does not support masked watchpoints."));
11506 else if (ret == -2)
11507 error (_("Invalid mask or memory region."));
11508 }
06a64a0b
TT
11509 }
11510 else if (val != NULL)
fa4727a6 11511 release_value (val);
c906108c 11512
bbc13ae3
KS
11513 tok = skip_spaces_const (arg);
11514 end_tok = skip_to_space_const (tok);
c906108c
SS
11515
11516 toklen = end_tok - tok;
11517 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11518 {
2d134ed3
PA
11519 struct expression *cond;
11520
60e1c644 11521 innermost_block = NULL;
c906108c 11522 tok = cond_start = end_tok + 1;
1bb9788d 11523 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11524
11525 /* The watchpoint expression may not be local, but the condition
11526 may still be. E.g.: `watch global if local > 0'. */
11527 cond_exp_valid_block = innermost_block;
11528
2d134ed3 11529 xfree (cond);
c906108c
SS
11530 cond_end = tok;
11531 }
11532 if (*tok)
8a3fe4f8 11533 error (_("Junk at end of command."));
c906108c 11534
d983da9c 11535 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11536
11537 /* If the expression is "local", then set up a "watchpoint scope"
11538 breakpoint at the point where we've left the scope of the watchpoint
11539 expression. Create the scope breakpoint before the watchpoint, so
11540 that we will encounter it first in bpstat_stop_status. */
60e1c644 11541 if (exp_valid_block && frame)
d983da9c 11542 {
edb3359d
DJ
11543 if (frame_id_p (frame_unwind_caller_id (frame)))
11544 {
11545 scope_breakpoint
a6d9a66e
UW
11546 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11547 frame_unwind_caller_pc (frame),
06edf0c0
PA
11548 bp_watchpoint_scope,
11549 &momentary_breakpoint_ops);
d983da9c 11550
edb3359d 11551 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11552
edb3359d
DJ
11553 /* Automatically delete the breakpoint when it hits. */
11554 scope_breakpoint->disposition = disp_del;
d983da9c 11555
edb3359d
DJ
11556 /* Only break in the proper frame (help with recursion). */
11557 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11558
edb3359d 11559 /* Set the address at which we will stop. */
a6d9a66e
UW
11560 scope_breakpoint->loc->gdbarch
11561 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11562 scope_breakpoint->loc->requested_address
11563 = frame_unwind_caller_pc (frame);
11564 scope_breakpoint->loc->address
a6d9a66e
UW
11565 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11566 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11567 scope_breakpoint->type);
11568 }
d983da9c
DJ
11569 }
11570
e8369a73
AB
11571 /* Now set up the breakpoint. We create all watchpoints as hardware
11572 watchpoints here even if hardware watchpoints are turned off, a call
11573 to update_watchpoint later in this function will cause the type to
11574 drop back to bp_watchpoint (software watchpoint) if required. */
11575
11576 if (accessflag == hw_read)
11577 bp_type = bp_read_watchpoint;
11578 else if (accessflag == hw_access)
11579 bp_type = bp_access_watchpoint;
11580 else
11581 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11582
11583 w = XCNEW (struct watchpoint);
11584 b = &w->base;
348d480f 11585 if (use_mask)
3a5c3e22
PA
11586 init_raw_breakpoint_without_location (b, NULL, bp_type,
11587 &masked_watchpoint_breakpoint_ops);
348d480f 11588 else
3a5c3e22
PA
11589 init_raw_breakpoint_without_location (b, NULL, bp_type,
11590 &watchpoint_breakpoint_ops);
37e4754d 11591 b->thread = thread;
b5de0fa7 11592 b->disposition = disp_donttouch;
348d480f 11593 b->pspace = current_program_space;
3a5c3e22
PA
11594 w->exp = exp;
11595 w->exp_valid_block = exp_valid_block;
11596 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11597 if (just_location)
11598 {
11599 struct type *t = value_type (val);
11600 CORE_ADDR addr = value_as_address (val);
11601 char *name;
11602
11603 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11604 name = type_to_string (t);
11605
3a5c3e22 11606 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11607 core_addr_to_string (addr));
06a64a0b
TT
11608 xfree (name);
11609
3a5c3e22 11610 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11611 (int) (exp_end - exp_start), exp_start);
11612
06a64a0b
TT
11613 /* The above expression is in C. */
11614 b->language = language_c;
11615 }
11616 else
3a5c3e22 11617 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11618
11619 if (use_mask)
11620 {
3a5c3e22 11621 w->hw_wp_mask = mask;
9c06b0b4
TJB
11622 }
11623 else
11624 {
3a5c3e22 11625 w->val = val;
bb9d5f81
PP
11626 w->val_bitpos = saved_bitpos;
11627 w->val_bitsize = saved_bitsize;
3a5c3e22 11628 w->val_valid = 1;
9c06b0b4 11629 }
77b06cd7 11630
c906108c
SS
11631 if (cond_start)
11632 b->cond_string = savestring (cond_start, cond_end - cond_start);
11633 else
11634 b->cond_string = 0;
c5aa993b 11635
c906108c 11636 if (frame)
f6bc2008 11637 {
3a5c3e22
PA
11638 w->watchpoint_frame = get_frame_id (frame);
11639 w->watchpoint_thread = inferior_ptid;
f6bc2008 11640 }
c906108c 11641 else
f6bc2008 11642 {
3a5c3e22
PA
11643 w->watchpoint_frame = null_frame_id;
11644 w->watchpoint_thread = null_ptid;
f6bc2008 11645 }
c906108c 11646
d983da9c 11647 if (scope_breakpoint != NULL)
c906108c 11648 {
d983da9c
DJ
11649 /* The scope breakpoint is related to the watchpoint. We will
11650 need to act on them together. */
11651 b->related_breakpoint = scope_breakpoint;
11652 scope_breakpoint->related_breakpoint = b;
c906108c 11653 }
d983da9c 11654
06a64a0b
TT
11655 if (!just_location)
11656 value_free_to_mark (mark);
2d134ed3 11657
a9634178
TJB
11658 TRY_CATCH (e, RETURN_MASK_ALL)
11659 {
11660 /* Finally update the new watchpoint. This creates the locations
11661 that should be inserted. */
3a5c3e22 11662 update_watchpoint (w, 1);
a9634178
TJB
11663 }
11664 if (e.reason < 0)
11665 {
11666 delete_breakpoint (b);
11667 throw_exception (e);
11668 }
11669
3ea46bff 11670 install_breakpoint (internal, b, 1);
bbc13ae3 11671 do_cleanups (back_to);
c906108c
SS
11672}
11673
e09342b5 11674/* Return count of debug registers needed to watch the given expression.
e09342b5 11675 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11676
c906108c 11677static int
a9634178 11678can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11679{
11680 int found_memory_cnt = 0;
2e70b7b9 11681 struct value *head = v;
c906108c
SS
11682
11683 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11684 if (!can_use_hw_watchpoints)
c906108c 11685 return 0;
c5aa993b 11686
5c44784c
JM
11687 /* Make sure that the value of the expression depends only upon
11688 memory contents, and values computed from them within GDB. If we
11689 find any register references or function calls, we can't use a
11690 hardware watchpoint.
11691
11692 The idea here is that evaluating an expression generates a series
11693 of values, one holding the value of every subexpression. (The
11694 expression a*b+c has five subexpressions: a, b, a*b, c, and
11695 a*b+c.) GDB's values hold almost enough information to establish
11696 the criteria given above --- they identify memory lvalues,
11697 register lvalues, computed values, etcetera. So we can evaluate
11698 the expression, and then scan the chain of values that leaves
11699 behind to decide whether we can detect any possible change to the
11700 expression's final value using only hardware watchpoints.
11701
11702 However, I don't think that the values returned by inferior
11703 function calls are special in any way. So this function may not
11704 notice that an expression involving an inferior function call
11705 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11706 for (; v; v = value_next (v))
c906108c 11707 {
5c44784c 11708 if (VALUE_LVAL (v) == lval_memory)
c906108c 11709 {
8464be76
DJ
11710 if (v != head && value_lazy (v))
11711 /* A lazy memory lvalue in the chain is one that GDB never
11712 needed to fetch; we either just used its address (e.g.,
11713 `a' in `a.b') or we never needed it at all (e.g., `a'
11714 in `a,b'). This doesn't apply to HEAD; if that is
11715 lazy then it was not readable, but watch it anyway. */
5c44784c 11716 ;
53a5351d 11717 else
5c44784c
JM
11718 {
11719 /* Ahh, memory we actually used! Check if we can cover
11720 it with hardware watchpoints. */
df407dfe 11721 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11722
11723 /* We only watch structs and arrays if user asked for it
11724 explicitly, never if they just happen to appear in a
11725 middle of some value chain. */
11726 if (v == head
11727 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11728 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11729 {
42ae5230 11730 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11731 int len;
11732 int num_regs;
11733
a9634178 11734 len = (target_exact_watchpoints
e09342b5
TJB
11735 && is_scalar_type_recursive (vtype))?
11736 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11737
e09342b5
TJB
11738 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11739 if (!num_regs)
2e70b7b9
MS
11740 return 0;
11741 else
e09342b5 11742 found_memory_cnt += num_regs;
2e70b7b9 11743 }
5c44784c 11744 }
c5aa993b 11745 }
5086187c
AC
11746 else if (VALUE_LVAL (v) != not_lval
11747 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11748 return 0; /* These are values from the history (e.g., $1). */
5086187c 11749 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11750 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11751 }
11752
11753 /* The expression itself looks suitable for using a hardware
11754 watchpoint, but give the target machine a chance to reject it. */
11755 return found_memory_cnt;
11756}
11757
8b93c638 11758void
84f4c1fe 11759watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11760{
84f4c1fe 11761 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11762}
11763
06a64a0b
TT
11764/* A helper function that looks for the "-location" argument and then
11765 calls watch_command_1. */
11766
11767static void
11768watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11769{
11770 int just_location = 0;
11771
11772 if (arg
11773 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11774 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11775 {
e9cafbcc 11776 arg = skip_spaces (arg);
06a64a0b
TT
11777 just_location = 1;
11778 }
11779
84f4c1fe 11780 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11781}
8926118c 11782
c5aa993b 11783static void
fba45db2 11784watch_command (char *arg, int from_tty)
c906108c 11785{
06a64a0b 11786 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11787}
11788
8b93c638 11789void
84f4c1fe 11790rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11791{
84f4c1fe 11792 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11793}
8926118c 11794
c5aa993b 11795static void
fba45db2 11796rwatch_command (char *arg, int from_tty)
c906108c 11797{
06a64a0b 11798 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11799}
11800
8b93c638 11801void
84f4c1fe 11802awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11803{
84f4c1fe 11804 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11805}
8926118c 11806
c5aa993b 11807static void
fba45db2 11808awatch_command (char *arg, int from_tty)
c906108c 11809{
06a64a0b 11810 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11811}
c906108c 11812\f
c5aa993b 11813
43ff13b4 11814/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11815 because it uses the mechanisms of breakpoints. */
11816
bfec99b2
PA
11817struct until_break_command_continuation_args
11818{
11819 struct breakpoint *breakpoint;
11820 struct breakpoint *breakpoint2;
186c406b 11821 int thread_num;
bfec99b2
PA
11822};
11823
43ff13b4 11824/* This function is called by fetch_inferior_event via the
4a64f543 11825 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11826 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11827 command. */
c2c6d25f 11828static void
fa4cd53f 11829until_break_command_continuation (void *arg, int err)
43ff13b4 11830{
bfec99b2
PA
11831 struct until_break_command_continuation_args *a = arg;
11832
11833 delete_breakpoint (a->breakpoint);
11834 if (a->breakpoint2)
11835 delete_breakpoint (a->breakpoint2);
186c406b 11836 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11837}
11838
c906108c 11839void
ae66c1fc 11840until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11841{
11842 struct symtabs_and_lines sals;
11843 struct symtab_and_line sal;
8556afb4
PA
11844 struct frame_info *frame;
11845 struct gdbarch *frame_gdbarch;
11846 struct frame_id stack_frame_id;
11847 struct frame_id caller_frame_id;
c906108c 11848 struct breakpoint *breakpoint;
f107f563 11849 struct breakpoint *breakpoint2 = NULL;
c906108c 11850 struct cleanup *old_chain;
186c406b
TT
11851 int thread;
11852 struct thread_info *tp;
c906108c 11853
70509625 11854 clear_proceed_status (0);
c906108c
SS
11855
11856 /* Set a breakpoint where the user wants it and at return from
4a64f543 11857 this function. */
c5aa993b 11858
1bfeeb0f 11859 if (last_displayed_sal_is_valid ())
f8eba3c6 11860 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11861 get_last_displayed_symtab (),
f8eba3c6 11862 get_last_displayed_line ());
c906108c 11863 else
f8eba3c6
TT
11864 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11865 (struct symtab *) NULL, 0);
c5aa993b 11866
c906108c 11867 if (sals.nelts != 1)
8a3fe4f8 11868 error (_("Couldn't get information on specified line."));
c5aa993b 11869
c906108c 11870 sal = sals.sals[0];
4a64f543 11871 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11872
c906108c 11873 if (*arg)
8a3fe4f8 11874 error (_("Junk at end of arguments."));
c5aa993b 11875
c906108c 11876 resolve_sal_pc (&sal);
c5aa993b 11877
186c406b
TT
11878 tp = inferior_thread ();
11879 thread = tp->num;
11880
883bc8d1
PA
11881 old_chain = make_cleanup (null_cleanup, NULL);
11882
8556afb4
PA
11883 /* Note linespec handling above invalidates the frame chain.
11884 Installing a breakpoint also invalidates the frame chain (as it
11885 may need to switch threads), so do any frame handling before
11886 that. */
11887
11888 frame = get_selected_frame (NULL);
11889 frame_gdbarch = get_frame_arch (frame);
11890 stack_frame_id = get_stack_frame_id (frame);
11891 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11892
ae66c1fc
EZ
11893 /* Keep within the current frame, or in frames called by the current
11894 one. */
edb3359d 11895
883bc8d1 11896 if (frame_id_p (caller_frame_id))
c906108c 11897 {
883bc8d1
PA
11898 struct symtab_and_line sal2;
11899
11900 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11901 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11902 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11903 sal2,
11904 caller_frame_id,
f107f563
VP
11905 bp_until);
11906 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11907
883bc8d1 11908 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11909 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11910 }
c5aa993b 11911
c70a6932
JK
11912 /* set_momentary_breakpoint could invalidate FRAME. */
11913 frame = NULL;
11914
883bc8d1
PA
11915 if (anywhere)
11916 /* If the user told us to continue until a specified location,
11917 we don't specify a frame at which we need to stop. */
11918 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11919 null_frame_id, bp_until);
11920 else
11921 /* Otherwise, specify the selected frame, because we want to stop
11922 only at the very same frame. */
11923 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11924 stack_frame_id, bp_until);
11925 make_cleanup_delete_breakpoint (breakpoint);
11926
a493e3e2 11927 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11928
4a64f543
MS
11929 /* If we are running asynchronously, and proceed call above has
11930 actually managed to start the target, arrange for breakpoints to
11931 be deleted when the target stops. Otherwise, we're already
11932 stopped and delete breakpoints via cleanup chain. */
f107f563 11933
8ea051c5 11934 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11935 {
bfec99b2
PA
11936 struct until_break_command_continuation_args *args;
11937 args = xmalloc (sizeof (*args));
f107f563 11938
bfec99b2
PA
11939 args->breakpoint = breakpoint;
11940 args->breakpoint2 = breakpoint2;
186c406b 11941 args->thread_num = thread;
f107f563
VP
11942
11943 discard_cleanups (old_chain);
95e54da7
PA
11944 add_continuation (inferior_thread (),
11945 until_break_command_continuation, args,
604ead4a 11946 xfree);
f107f563
VP
11947 }
11948 else
c5aa993b 11949 do_cleanups (old_chain);
c906108c 11950}
ae66c1fc 11951
c906108c
SS
11952/* This function attempts to parse an optional "if <cond>" clause
11953 from the arg string. If one is not found, it returns NULL.
c5aa993b 11954
c906108c
SS
11955 Else, it returns a pointer to the condition string. (It does not
11956 attempt to evaluate the string against a particular block.) And,
11957 it updates arg to point to the first character following the parsed
4a64f543 11958 if clause in the arg string. */
53a5351d 11959
916703c0 11960char *
fba45db2 11961ep_parse_optional_if_clause (char **arg)
c906108c 11962{
c5aa993b
JM
11963 char *cond_string;
11964
11965 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11966 return NULL;
c5aa993b 11967
4a64f543 11968 /* Skip the "if" keyword. */
c906108c 11969 (*arg) += 2;
c5aa993b 11970
c906108c 11971 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11972 condition string. */
e9cafbcc 11973 *arg = skip_spaces (*arg);
c906108c 11974 cond_string = *arg;
c5aa993b 11975
4a64f543
MS
11976 /* Assume that the condition occupies the remainder of the arg
11977 string. */
c906108c 11978 (*arg) += strlen (cond_string);
c5aa993b 11979
c906108c
SS
11980 return cond_string;
11981}
c5aa993b 11982
c906108c
SS
11983/* Commands to deal with catching events, such as signals, exceptions,
11984 process start/exit, etc. */
c5aa993b
JM
11985
11986typedef enum
11987{
44feb3ce
TT
11988 catch_fork_temporary, catch_vfork_temporary,
11989 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11990}
11991catch_fork_kind;
11992
c906108c 11993static void
cc59ec59
MS
11994catch_fork_command_1 (char *arg, int from_tty,
11995 struct cmd_list_element *command)
c906108c 11996{
a6d9a66e 11997 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11998 char *cond_string = NULL;
44feb3ce
TT
11999 catch_fork_kind fork_kind;
12000 int tempflag;
12001
12002 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
12003 tempflag = (fork_kind == catch_fork_temporary
12004 || fork_kind == catch_vfork_temporary);
c5aa993b 12005
44feb3ce
TT
12006 if (!arg)
12007 arg = "";
e9cafbcc 12008 arg = skip_spaces (arg);
c5aa993b 12009
c906108c 12010 /* The allowed syntax is:
c5aa993b
JM
12011 catch [v]fork
12012 catch [v]fork if <cond>
12013
4a64f543 12014 First, check if there's an if clause. */
c906108c 12015 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 12016
c906108c 12017 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12018 error (_("Junk at end of arguments."));
c5aa993b 12019
c906108c 12020 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 12021 and enable reporting of such events. */
c5aa993b
JM
12022 switch (fork_kind)
12023 {
44feb3ce
TT
12024 case catch_fork_temporary:
12025 case catch_fork_permanent:
a6d9a66e 12026 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12027 &catch_fork_breakpoint_ops);
c906108c 12028 break;
44feb3ce
TT
12029 case catch_vfork_temporary:
12030 case catch_vfork_permanent:
a6d9a66e 12031 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12032 &catch_vfork_breakpoint_ops);
c906108c 12033 break;
c5aa993b 12034 default:
8a3fe4f8 12035 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 12036 break;
c5aa993b 12037 }
c906108c
SS
12038}
12039
12040static void
cc59ec59
MS
12041catch_exec_command_1 (char *arg, int from_tty,
12042 struct cmd_list_element *command)
c906108c 12043{
b4d90040 12044 struct exec_catchpoint *c;
a6d9a66e 12045 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 12046 int tempflag;
c5aa993b 12047 char *cond_string = NULL;
c906108c 12048
44feb3ce
TT
12049 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12050
12051 if (!arg)
12052 arg = "";
e9cafbcc 12053 arg = skip_spaces (arg);
c906108c
SS
12054
12055 /* The allowed syntax is:
c5aa993b
JM
12056 catch exec
12057 catch exec if <cond>
c906108c 12058
4a64f543 12059 First, check if there's an if clause. */
c906108c
SS
12060 cond_string = ep_parse_optional_if_clause (&arg);
12061
12062 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12063 error (_("Junk at end of arguments."));
c906108c 12064
b4d90040
PA
12065 c = XNEW (struct exec_catchpoint);
12066 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
12067 &catch_exec_breakpoint_ops);
12068 c->exec_pathname = NULL;
12069
3ea46bff 12070 install_breakpoint (0, &c->base, 1);
c906108c 12071}
c5aa993b 12072
9ac4176b 12073void
28010a5d
PA
12074init_ada_exception_breakpoint (struct breakpoint *b,
12075 struct gdbarch *gdbarch,
12076 struct symtab_and_line sal,
12077 char *addr_string,
c0a91b2b 12078 const struct breakpoint_ops *ops,
28010a5d 12079 int tempflag,
349774ef 12080 int enabled,
28010a5d 12081 int from_tty)
f7f9143b 12082{
f7f9143b
JB
12083 if (from_tty)
12084 {
5af949e3
UW
12085 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12086 if (!loc_gdbarch)
12087 loc_gdbarch = gdbarch;
12088
6c95b8df
PA
12089 describe_other_breakpoints (loc_gdbarch,
12090 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
12091 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12092 version for exception catchpoints, because two catchpoints
12093 used for different exception names will use the same address.
12094 In this case, a "breakpoint ... also set at..." warning is
4a64f543 12095 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 12096 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
12097 the user what type of catchpoint it is. The above is good
12098 enough for now, though. */
12099 }
12100
28010a5d 12101 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 12102
349774ef 12103 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 12104 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
12105 b->addr_string = addr_string;
12106 b->language = language_ada;
f7f9143b
JB
12107}
12108
a96d9b2e
SDJ
12109/* Splits the argument using space as delimiter. Returns an xmalloc'd
12110 filter list, or NULL if no filtering is required. */
12111static VEC(int) *
12112catch_syscall_split_args (char *arg)
12113{
12114 VEC(int) *result = NULL;
29d0bb3d 12115 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
458c8db8 12116 struct gdbarch *gdbarch = target_gdbarch ();
a96d9b2e
SDJ
12117
12118 while (*arg != '\0')
12119 {
12120 int i, syscall_number;
12121 char *endptr;
12122 char cur_name[128];
12123 struct syscall s;
12124
12125 /* Skip whitespace. */
529480d0 12126 arg = skip_spaces (arg);
a96d9b2e
SDJ
12127
12128 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
12129 cur_name[i] = arg[i];
12130 cur_name[i] = '\0';
12131 arg += i;
12132
12133 /* Check if the user provided a syscall name or a number. */
12134 syscall_number = (int) strtol (cur_name, &endptr, 0);
12135 if (*endptr == '\0')
458c8db8 12136 get_syscall_by_number (gdbarch, syscall_number, &s);
a96d9b2e
SDJ
12137 else
12138 {
12139 /* We have a name. Let's check if it's valid and convert it
12140 to a number. */
458c8db8 12141 get_syscall_by_name (gdbarch, cur_name, &s);
a96d9b2e
SDJ
12142
12143 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
12144 /* Here we have to issue an error instead of a warning,
12145 because GDB cannot do anything useful if there's no
12146 syscall number to be caught. */
a96d9b2e
SDJ
12147 error (_("Unknown syscall name '%s'."), cur_name);
12148 }
12149
12150 /* Ok, it's valid. */
12151 VEC_safe_push (int, result, s.number);
12152 }
12153
12154 discard_cleanups (cleanup);
12155 return result;
12156}
12157
12158/* Implement the "catch syscall" command. */
12159
12160static void
cc59ec59
MS
12161catch_syscall_command_1 (char *arg, int from_tty,
12162 struct cmd_list_element *command)
a96d9b2e
SDJ
12163{
12164 int tempflag;
12165 VEC(int) *filter;
12166 struct syscall s;
12167 struct gdbarch *gdbarch = get_current_arch ();
12168
12169 /* Checking if the feature if supported. */
12170 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
12171 error (_("The feature 'catch syscall' is not supported on \
ea666128 12172this architecture yet."));
a96d9b2e
SDJ
12173
12174 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12175
e9cafbcc 12176 arg = skip_spaces (arg);
a96d9b2e
SDJ
12177
12178 /* We need to do this first "dummy" translation in order
12179 to get the syscall XML file loaded or, most important,
12180 to display a warning to the user if there's no XML file
12181 for his/her architecture. */
458c8db8 12182 get_syscall_by_number (gdbarch, 0, &s);
a96d9b2e
SDJ
12183
12184 /* The allowed syntax is:
12185 catch syscall
12186 catch syscall <name | number> [<name | number> ... <name | number>]
12187
12188 Let's check if there's a syscall name. */
12189
12190 if (arg != NULL)
12191 filter = catch_syscall_split_args (arg);
12192 else
12193 filter = NULL;
12194
12195 create_syscall_event_catchpoint (tempflag, filter,
12196 &catch_syscall_breakpoint_ops);
12197}
12198
c906108c 12199static void
fba45db2 12200catch_command (char *arg, int from_tty)
c906108c 12201{
44feb3ce 12202 error (_("Catch requires an event name."));
c906108c
SS
12203}
12204\f
12205
12206static void
fba45db2 12207tcatch_command (char *arg, int from_tty)
c906108c 12208{
44feb3ce 12209 error (_("Catch requires an event name."));
c906108c
SS
12210}
12211
8a2c437b
TT
12212/* A qsort comparison function that sorts breakpoints in order. */
12213
12214static int
12215compare_breakpoints (const void *a, const void *b)
12216{
12217 const breakpoint_p *ba = a;
12218 uintptr_t ua = (uintptr_t) *ba;
12219 const breakpoint_p *bb = b;
12220 uintptr_t ub = (uintptr_t) *bb;
12221
12222 if ((*ba)->number < (*bb)->number)
12223 return -1;
12224 else if ((*ba)->number > (*bb)->number)
12225 return 1;
12226
12227 /* Now sort by address, in case we see, e..g, two breakpoints with
12228 the number 0. */
12229 if (ua < ub)
12230 return -1;
94b0e70d 12231 return ua > ub ? 1 : 0;
8a2c437b
TT
12232}
12233
80f8a6eb 12234/* Delete breakpoints by address or line. */
c906108c
SS
12235
12236static void
fba45db2 12237clear_command (char *arg, int from_tty)
c906108c 12238{
8a2c437b 12239 struct breakpoint *b, *prev;
d6e956e5
VP
12240 VEC(breakpoint_p) *found = 0;
12241 int ix;
c906108c
SS
12242 int default_match;
12243 struct symtabs_and_lines sals;
12244 struct symtab_and_line sal;
c906108c 12245 int i;
8a2c437b 12246 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12247
12248 if (arg)
12249 {
39cf75f7
DE
12250 sals = decode_line_with_current_source (arg,
12251 (DECODE_LINE_FUNFIRSTLINE
12252 | DECODE_LINE_LIST_MODE));
cf4ded82 12253 make_cleanup (xfree, sals.sals);
c906108c
SS
12254 default_match = 0;
12255 }
12256 else
12257 {
c5aa993b 12258 sals.sals = (struct symtab_and_line *)
c906108c 12259 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 12260 make_cleanup (xfree, sals.sals);
4a64f543 12261 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12262
12263 /* Set sal's line, symtab, pc, and pspace to the values
12264 corresponding to the last call to print_frame_info. If the
12265 codepoint is not valid, this will set all the fields to 0. */
12266 get_last_displayed_sal (&sal);
c906108c 12267 if (sal.symtab == 0)
8a3fe4f8 12268 error (_("No source file specified."));
c906108c
SS
12269
12270 sals.sals[0] = sal;
12271 sals.nelts = 1;
12272
12273 default_match = 1;
12274 }
12275
4a64f543
MS
12276 /* We don't call resolve_sal_pc here. That's not as bad as it
12277 seems, because all existing breakpoints typically have both
12278 file/line and pc set. So, if clear is given file/line, we can
12279 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12280
12281 We only support clearing given the address explicitly
12282 present in breakpoint table. Say, we've set breakpoint
4a64f543 12283 at file:line. There were several PC values for that file:line,
ed0616c6 12284 due to optimization, all in one block.
4a64f543
MS
12285
12286 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12287 PC corresponding to the same file:line, the breakpoint won't
12288 be cleared. We probably can still clear the breakpoint, but
12289 since the other PC value is never presented to user, user
12290 can only find it by guessing, and it does not seem important
12291 to support that. */
12292
4a64f543
MS
12293 /* For each line spec given, delete bps which correspond to it. Do
12294 it in two passes, solely to preserve the current behavior that
12295 from_tty is forced true if we delete more than one
12296 breakpoint. */
c906108c 12297
80f8a6eb 12298 found = NULL;
8a2c437b 12299 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12300 for (i = 0; i < sals.nelts; i++)
12301 {
05cba821
JK
12302 const char *sal_fullname;
12303
c906108c 12304 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12305 If line given (pc == 0), clear all bpts on specified line.
12306 If defaulting, clear all bpts on default line
c906108c 12307 or at default pc.
c5aa993b
JM
12308
12309 defaulting sal.pc != 0 tests to do
12310
12311 0 1 pc
12312 1 1 pc _and_ line
12313 0 0 line
12314 1 0 <can't happen> */
c906108c
SS
12315
12316 sal = sals.sals[i];
05cba821
JK
12317 sal_fullname = (sal.symtab == NULL
12318 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12319
4a64f543 12320 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12321 ALL_BREAKPOINTS (b)
c5aa993b 12322 {
0d381245 12323 int match = 0;
4a64f543 12324 /* Are we going to delete b? */
cc60f2e3 12325 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12326 {
12327 struct bp_location *loc = b->loc;
12328 for (; loc; loc = loc->next)
12329 {
f8eba3c6
TT
12330 /* If the user specified file:line, don't allow a PC
12331 match. This matches historical gdb behavior. */
12332 int pc_match = (!sal.explicit_line
12333 && sal.pc
12334 && (loc->pspace == sal.pspace)
12335 && (loc->address == sal.pc)
12336 && (!section_is_overlay (loc->section)
12337 || loc->section == sal.section));
4aac40c8
TT
12338 int line_match = 0;
12339
12340 if ((default_match || sal.explicit_line)
2f202fde 12341 && loc->symtab != NULL
05cba821 12342 && sal_fullname != NULL
4aac40c8 12343 && sal.pspace == loc->pspace
05cba821
JK
12344 && loc->line_number == sal.line
12345 && filename_cmp (symtab_to_fullname (loc->symtab),
12346 sal_fullname) == 0)
12347 line_match = 1;
4aac40c8 12348
0d381245
VP
12349 if (pc_match || line_match)
12350 {
12351 match = 1;
12352 break;
12353 }
12354 }
12355 }
12356
12357 if (match)
d6e956e5 12358 VEC_safe_push(breakpoint_p, found, b);
c906108c 12359 }
80f8a6eb 12360 }
8a2c437b 12361
80f8a6eb 12362 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12363 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12364 {
12365 if (arg)
8a3fe4f8 12366 error (_("No breakpoint at %s."), arg);
80f8a6eb 12367 else
8a3fe4f8 12368 error (_("No breakpoint at this line."));
80f8a6eb 12369 }
c906108c 12370
8a2c437b
TT
12371 /* Remove duplicates from the vec. */
12372 qsort (VEC_address (breakpoint_p, found),
12373 VEC_length (breakpoint_p, found),
12374 sizeof (breakpoint_p),
12375 compare_breakpoints);
12376 prev = VEC_index (breakpoint_p, found, 0);
12377 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12378 {
12379 if (b == prev)
12380 {
12381 VEC_ordered_remove (breakpoint_p, found, ix);
12382 --ix;
12383 }
12384 }
12385
d6e956e5 12386 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12387 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12388 if (from_tty)
a3f17187 12389 {
d6e956e5 12390 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12391 printf_unfiltered (_("Deleted breakpoint "));
12392 else
12393 printf_unfiltered (_("Deleted breakpoints "));
12394 }
d6e956e5
VP
12395
12396 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12397 {
c5aa993b 12398 if (from_tty)
d6e956e5
VP
12399 printf_unfiltered ("%d ", b->number);
12400 delete_breakpoint (b);
c906108c 12401 }
80f8a6eb
MS
12402 if (from_tty)
12403 putchar_unfiltered ('\n');
8a2c437b
TT
12404
12405 do_cleanups (cleanups);
c906108c
SS
12406}
12407\f
12408/* Delete breakpoint in BS if they are `delete' breakpoints and
12409 all breakpoints that are marked for deletion, whether hit or not.
12410 This is called after any breakpoint is hit, or after errors. */
12411
12412void
fba45db2 12413breakpoint_auto_delete (bpstat bs)
c906108c 12414{
35df4500 12415 struct breakpoint *b, *b_tmp;
c906108c
SS
12416
12417 for (; bs; bs = bs->next)
f431efe5
PA
12418 if (bs->breakpoint_at
12419 && bs->breakpoint_at->disposition == disp_del
c906108c 12420 && bs->stop)
f431efe5 12421 delete_breakpoint (bs->breakpoint_at);
c906108c 12422
35df4500 12423 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12424 {
b5de0fa7 12425 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12426 delete_breakpoint (b);
12427 }
c906108c
SS
12428}
12429
4a64f543
MS
12430/* A comparison function for bp_location AP and BP being interfaced to
12431 qsort. Sort elements primarily by their ADDRESS (no matter what
12432 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12433 secondarily by ordering first permanent elements and
4a64f543 12434 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12435 qsort being an unstable algorithm. */
876fa593
JK
12436
12437static int
494cfb0f 12438bp_location_compare (const void *ap, const void *bp)
876fa593 12439{
494cfb0f
JK
12440 struct bp_location *a = *(void **) ap;
12441 struct bp_location *b = *(void **) bp;
876fa593
JK
12442
12443 if (a->address != b->address)
12444 return (a->address > b->address) - (a->address < b->address);
12445
dea2aa5f
LM
12446 /* Sort locations at the same address by their pspace number, keeping
12447 locations of the same inferior (in a multi-inferior environment)
12448 grouped. */
12449
12450 if (a->pspace->num != b->pspace->num)
12451 return ((a->pspace->num > b->pspace->num)
12452 - (a->pspace->num < b->pspace->num));
12453
876fa593 12454 /* Sort permanent breakpoints first. */
1a853c52
PA
12455 if (a->permanent != b->permanent)
12456 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12457
c56a97f9
JK
12458 /* Make the internal GDB representation stable across GDB runs
12459 where A and B memory inside GDB can differ. Breakpoint locations of
12460 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12461
12462 if (a->owner->number != b->owner->number)
c56a97f9
JK
12463 return ((a->owner->number > b->owner->number)
12464 - (a->owner->number < b->owner->number));
876fa593
JK
12465
12466 return (a > b) - (a < b);
12467}
12468
876fa593 12469/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12470 bp_location_shadow_len_after_address_max according to the current
12471 content of the bp_location array. */
f7545552
TT
12472
12473static void
876fa593 12474bp_location_target_extensions_update (void)
f7545552 12475{
876fa593
JK
12476 struct bp_location *bl, **blp_tmp;
12477
12478 bp_location_placed_address_before_address_max = 0;
12479 bp_location_shadow_len_after_address_max = 0;
12480
12481 ALL_BP_LOCATIONS (bl, blp_tmp)
12482 {
12483 CORE_ADDR start, end, addr;
12484
12485 if (!bp_location_has_shadow (bl))
12486 continue;
12487
12488 start = bl->target_info.placed_address;
12489 end = start + bl->target_info.shadow_len;
12490
12491 gdb_assert (bl->address >= start);
12492 addr = bl->address - start;
12493 if (addr > bp_location_placed_address_before_address_max)
12494 bp_location_placed_address_before_address_max = addr;
12495
12496 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12497
12498 gdb_assert (bl->address < end);
12499 addr = end - bl->address;
12500 if (addr > bp_location_shadow_len_after_address_max)
12501 bp_location_shadow_len_after_address_max = addr;
12502 }
f7545552
TT
12503}
12504
1e4d1764
YQ
12505/* Download tracepoint locations if they haven't been. */
12506
12507static void
12508download_tracepoint_locations (void)
12509{
7ed2c994 12510 struct breakpoint *b;
1e4d1764
YQ
12511 struct cleanup *old_chain;
12512
12513 if (!target_can_download_tracepoint ())
12514 return;
12515
12516 old_chain = save_current_space_and_thread ();
12517
7ed2c994 12518 ALL_TRACEPOINTS (b)
1e4d1764 12519 {
7ed2c994 12520 struct bp_location *bl;
1e4d1764 12521 struct tracepoint *t;
f2a8bc8a 12522 int bp_location_downloaded = 0;
1e4d1764 12523
7ed2c994 12524 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12525 ? !may_insert_fast_tracepoints
12526 : !may_insert_tracepoints))
12527 continue;
12528
7ed2c994
YQ
12529 for (bl = b->loc; bl; bl = bl->next)
12530 {
12531 /* In tracepoint, locations are _never_ duplicated, so
12532 should_be_inserted is equivalent to
12533 unduplicated_should_be_inserted. */
12534 if (!should_be_inserted (bl) || bl->inserted)
12535 continue;
1e4d1764 12536
7ed2c994 12537 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12538
7ed2c994 12539 target_download_tracepoint (bl);
1e4d1764 12540
7ed2c994 12541 bl->inserted = 1;
f2a8bc8a 12542 bp_location_downloaded = 1;
7ed2c994
YQ
12543 }
12544 t = (struct tracepoint *) b;
12545 t->number_on_target = b->number;
f2a8bc8a
YQ
12546 if (bp_location_downloaded)
12547 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12548 }
12549
12550 do_cleanups (old_chain);
12551}
12552
934709f0
PW
12553/* Swap the insertion/duplication state between two locations. */
12554
12555static void
12556swap_insertion (struct bp_location *left, struct bp_location *right)
12557{
12558 const int left_inserted = left->inserted;
12559 const int left_duplicate = left->duplicate;
b775012e 12560 const int left_needs_update = left->needs_update;
934709f0
PW
12561 const struct bp_target_info left_target_info = left->target_info;
12562
1e4d1764
YQ
12563 /* Locations of tracepoints can never be duplicated. */
12564 if (is_tracepoint (left->owner))
12565 gdb_assert (!left->duplicate);
12566 if (is_tracepoint (right->owner))
12567 gdb_assert (!right->duplicate);
12568
934709f0
PW
12569 left->inserted = right->inserted;
12570 left->duplicate = right->duplicate;
b775012e 12571 left->needs_update = right->needs_update;
934709f0
PW
12572 left->target_info = right->target_info;
12573 right->inserted = left_inserted;
12574 right->duplicate = left_duplicate;
b775012e 12575 right->needs_update = left_needs_update;
934709f0
PW
12576 right->target_info = left_target_info;
12577}
12578
b775012e
LM
12579/* Force the re-insertion of the locations at ADDRESS. This is called
12580 once a new/deleted/modified duplicate location is found and we are evaluating
12581 conditions on the target's side. Such conditions need to be updated on
12582 the target. */
12583
12584static void
12585force_breakpoint_reinsertion (struct bp_location *bl)
12586{
12587 struct bp_location **locp = NULL, **loc2p;
12588 struct bp_location *loc;
12589 CORE_ADDR address = 0;
12590 int pspace_num;
12591
12592 address = bl->address;
12593 pspace_num = bl->pspace->num;
12594
12595 /* This is only meaningful if the target is
12596 evaluating conditions and if the user has
12597 opted for condition evaluation on the target's
12598 side. */
12599 if (gdb_evaluates_breakpoint_condition_p ()
12600 || !target_supports_evaluation_of_breakpoint_conditions ())
12601 return;
12602
12603 /* Flag all breakpoint locations with this address and
12604 the same program space as the location
12605 as "its condition has changed". We need to
12606 update the conditions on the target's side. */
12607 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12608 {
12609 loc = *loc2p;
12610
12611 if (!is_breakpoint (loc->owner)
12612 || pspace_num != loc->pspace->num)
12613 continue;
12614
12615 /* Flag the location appropriately. We use a different state to
12616 let everyone know that we already updated the set of locations
12617 with addr bl->address and program space bl->pspace. This is so
12618 we don't have to keep calling these functions just to mark locations
12619 that have already been marked. */
12620 loc->condition_changed = condition_updated;
12621
12622 /* Free the agent expression bytecode as well. We will compute
12623 it later on. */
12624 if (loc->cond_bytecode)
12625 {
12626 free_agent_expr (loc->cond_bytecode);
12627 loc->cond_bytecode = NULL;
12628 }
12629 }
12630}
44702360
PA
12631/* Called whether new breakpoints are created, or existing breakpoints
12632 deleted, to update the global location list and recompute which
12633 locations are duplicate of which.
b775012e 12634
04086b45
PA
12635 The INSERT_MODE flag determines whether locations may not, may, or
12636 shall be inserted now. See 'enum ugll_insert_mode' for more
12637 info. */
b60e7edf 12638
0d381245 12639static void
44702360 12640update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12641{
74960c60 12642 struct breakpoint *b;
876fa593 12643 struct bp_location **locp, *loc;
f7545552 12644 struct cleanup *cleanups;
b775012e
LM
12645 /* Last breakpoint location address that was marked for update. */
12646 CORE_ADDR last_addr = 0;
12647 /* Last breakpoint location program space that was marked for update. */
12648 int last_pspace_num = -1;
f7545552 12649
2d134ed3
PA
12650 /* Used in the duplicates detection below. When iterating over all
12651 bp_locations, points to the first bp_location of a given address.
12652 Breakpoints and watchpoints of different types are never
12653 duplicates of each other. Keep one pointer for each type of
12654 breakpoint/watchpoint, so we only need to loop over all locations
12655 once. */
12656 struct bp_location *bp_loc_first; /* breakpoint */
12657 struct bp_location *wp_loc_first; /* hardware watchpoint */
12658 struct bp_location *awp_loc_first; /* access watchpoint */
12659 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12660
4a64f543
MS
12661 /* Saved former bp_location array which we compare against the newly
12662 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12663 struct bp_location **old_location, **old_locp;
12664 unsigned old_location_count;
12665
12666 old_location = bp_location;
12667 old_location_count = bp_location_count;
12668 bp_location = NULL;
12669 bp_location_count = 0;
12670 cleanups = make_cleanup (xfree, old_location);
0d381245 12671
74960c60 12672 ALL_BREAKPOINTS (b)
876fa593
JK
12673 for (loc = b->loc; loc; loc = loc->next)
12674 bp_location_count++;
12675
12676 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12677 locp = bp_location;
12678 ALL_BREAKPOINTS (b)
12679 for (loc = b->loc; loc; loc = loc->next)
12680 *locp++ = loc;
12681 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12682 bp_location_compare);
876fa593
JK
12683
12684 bp_location_target_extensions_update ();
74960c60 12685
4a64f543
MS
12686 /* Identify bp_location instances that are no longer present in the
12687 new list, and therefore should be freed. Note that it's not
12688 necessary that those locations should be removed from inferior --
12689 if there's another location at the same address (previously
12690 marked as duplicate), we don't need to remove/insert the
12691 location.
876fa593 12692
4a64f543
MS
12693 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12694 and former bp_location array state respectively. */
876fa593
JK
12695
12696 locp = bp_location;
12697 for (old_locp = old_location; old_locp < old_location + old_location_count;
12698 old_locp++)
74960c60 12699 {
876fa593 12700 struct bp_location *old_loc = *old_locp;
c7d46a38 12701 struct bp_location **loc2p;
876fa593 12702
e5dd4106 12703 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12704 not, we have to free it. */
c7d46a38 12705 int found_object = 0;
20874c92
VP
12706 /* Tells if the location should remain inserted in the target. */
12707 int keep_in_target = 0;
12708 int removed = 0;
876fa593 12709
4a64f543
MS
12710 /* Skip LOCP entries which will definitely never be needed.
12711 Stop either at or being the one matching OLD_LOC. */
876fa593 12712 while (locp < bp_location + bp_location_count
c7d46a38 12713 && (*locp)->address < old_loc->address)
876fa593 12714 locp++;
c7d46a38
PA
12715
12716 for (loc2p = locp;
12717 (loc2p < bp_location + bp_location_count
12718 && (*loc2p)->address == old_loc->address);
12719 loc2p++)
12720 {
b775012e
LM
12721 /* Check if this is a new/duplicated location or a duplicated
12722 location that had its condition modified. If so, we want to send
12723 its condition to the target if evaluation of conditions is taking
12724 place there. */
12725 if ((*loc2p)->condition_changed == condition_modified
12726 && (last_addr != old_loc->address
12727 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12728 {
b775012e
LM
12729 force_breakpoint_reinsertion (*loc2p);
12730 last_pspace_num = old_loc->pspace->num;
c7d46a38 12731 }
b775012e
LM
12732
12733 if (*loc2p == old_loc)
12734 found_object = 1;
c7d46a38 12735 }
74960c60 12736
b775012e
LM
12737 /* We have already handled this address, update it so that we don't
12738 have to go through updates again. */
12739 last_addr = old_loc->address;
12740
12741 /* Target-side condition evaluation: Handle deleted locations. */
12742 if (!found_object)
12743 force_breakpoint_reinsertion (old_loc);
12744
4a64f543
MS
12745 /* If this location is no longer present, and inserted, look if
12746 there's maybe a new location at the same address. If so,
12747 mark that one inserted, and don't remove this one. This is
12748 needed so that we don't have a time window where a breakpoint
12749 at certain location is not inserted. */
74960c60 12750
876fa593 12751 if (old_loc->inserted)
0d381245 12752 {
4a64f543
MS
12753 /* If the location is inserted now, we might have to remove
12754 it. */
74960c60 12755
876fa593 12756 if (found_object && should_be_inserted (old_loc))
74960c60 12757 {
4a64f543
MS
12758 /* The location is still present in the location list,
12759 and still should be inserted. Don't do anything. */
20874c92 12760 keep_in_target = 1;
74960c60
VP
12761 }
12762 else
12763 {
b775012e
LM
12764 /* This location still exists, but it won't be kept in the
12765 target since it may have been disabled. We proceed to
12766 remove its target-side condition. */
12767
4a64f543
MS
12768 /* The location is either no longer present, or got
12769 disabled. See if there's another location at the
12770 same address, in which case we don't need to remove
12771 this one from the target. */
876fa593 12772
2bdf28a0 12773 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12774 if (breakpoint_address_is_meaningful (old_loc->owner))
12775 {
876fa593 12776 for (loc2p = locp;
c7d46a38
PA
12777 (loc2p < bp_location + bp_location_count
12778 && (*loc2p)->address == old_loc->address);
876fa593
JK
12779 loc2p++)
12780 {
12781 struct bp_location *loc2 = *loc2p;
12782
2d134ed3 12783 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12784 {
85d721b8
PA
12785 /* Read watchpoint locations are switched to
12786 access watchpoints, if the former are not
12787 supported, but the latter are. */
12788 if (is_hardware_watchpoint (old_loc->owner))
12789 {
12790 gdb_assert (is_hardware_watchpoint (loc2->owner));
12791 loc2->watchpoint_type = old_loc->watchpoint_type;
12792 }
12793
934709f0
PW
12794 /* loc2 is a duplicated location. We need to check
12795 if it should be inserted in case it will be
12796 unduplicated. */
12797 if (loc2 != old_loc
12798 && unduplicated_should_be_inserted (loc2))
c7d46a38 12799 {
934709f0 12800 swap_insertion (old_loc, loc2);
c7d46a38
PA
12801 keep_in_target = 1;
12802 break;
12803 }
876fa593
JK
12804 }
12805 }
12806 }
74960c60
VP
12807 }
12808
20874c92
VP
12809 if (!keep_in_target)
12810 {
876fa593 12811 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12812 {
4a64f543
MS
12813 /* This is just about all we can do. We could keep
12814 this location on the global list, and try to
12815 remove it next time, but there's no particular
12816 reason why we will succeed next time.
20874c92 12817
4a64f543
MS
12818 Note that at this point, old_loc->owner is still
12819 valid, as delete_breakpoint frees the breakpoint
12820 only after calling us. */
3e43a32a
MS
12821 printf_filtered (_("warning: Error removing "
12822 "breakpoint %d\n"),
876fa593 12823 old_loc->owner->number);
20874c92
VP
12824 }
12825 removed = 1;
12826 }
0d381245 12827 }
74960c60
VP
12828
12829 if (!found_object)
1c5cfe86 12830 {
db82e815 12831 if (removed && non_stop
1cf4d951 12832 && need_moribund_for_location_type (old_loc))
20874c92 12833 {
db82e815
PA
12834 /* This location was removed from the target. In
12835 non-stop mode, a race condition is possible where
12836 we've removed a breakpoint, but stop events for that
12837 breakpoint are already queued and will arrive later.
12838 We apply an heuristic to be able to distinguish such
12839 SIGTRAPs from other random SIGTRAPs: we keep this
12840 breakpoint location for a bit, and will retire it
12841 after we see some number of events. The theory here
12842 is that reporting of events should, "on the average",
12843 be fair, so after a while we'll see events from all
12844 threads that have anything of interest, and no longer
12845 need to keep this breakpoint location around. We
12846 don't hold locations forever so to reduce chances of
12847 mistaking a non-breakpoint SIGTRAP for a breakpoint
12848 SIGTRAP.
12849
12850 The heuristic failing can be disastrous on
12851 decr_pc_after_break targets.
12852
12853 On decr_pc_after_break targets, like e.g., x86-linux,
12854 if we fail to recognize a late breakpoint SIGTRAP,
12855 because events_till_retirement has reached 0 too
12856 soon, we'll fail to do the PC adjustment, and report
12857 a random SIGTRAP to the user. When the user resumes
12858 the inferior, it will most likely immediately crash
2dec564e 12859 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12860 corrupted, because of being resumed e.g., in the
12861 middle of a multi-byte instruction, or skipped a
12862 one-byte instruction. This was actually seen happen
12863 on native x86-linux, and should be less rare on
12864 targets that do not support new thread events, like
12865 remote, due to the heuristic depending on
12866 thread_count.
12867
12868 Mistaking a random SIGTRAP for a breakpoint trap
12869 causes similar symptoms (PC adjustment applied when
12870 it shouldn't), but then again, playing with SIGTRAPs
12871 behind the debugger's back is asking for trouble.
12872
12873 Since hardware watchpoint traps are always
12874 distinguishable from other traps, so we don't need to
12875 apply keep hardware watchpoint moribund locations
12876 around. We simply always ignore hardware watchpoint
12877 traps we can no longer explain. */
12878
876fa593
JK
12879 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12880 old_loc->owner = NULL;
20874c92 12881
876fa593 12882 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12883 }
12884 else
f431efe5
PA
12885 {
12886 old_loc->owner = NULL;
12887 decref_bp_location (&old_loc);
12888 }
20874c92 12889 }
74960c60 12890 }
1c5cfe86 12891
348d480f
PA
12892 /* Rescan breakpoints at the same address and section, marking the
12893 first one as "first" and any others as "duplicates". This is so
12894 that the bpt instruction is only inserted once. If we have a
12895 permanent breakpoint at the same place as BPT, make that one the
12896 official one, and the rest as duplicates. Permanent breakpoints
12897 are sorted first for the same address.
12898
12899 Do the same for hardware watchpoints, but also considering the
12900 watchpoint's type (regular/access/read) and length. */
12901
12902 bp_loc_first = NULL;
12903 wp_loc_first = NULL;
12904 awp_loc_first = NULL;
12905 rwp_loc_first = NULL;
12906 ALL_BP_LOCATIONS (loc, locp)
12907 {
12908 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12909 non-NULL. */
348d480f 12910 struct bp_location **loc_first_p;
d3fbdd86 12911 b = loc->owner;
348d480f 12912
6f380991 12913 if (!unduplicated_should_be_inserted (loc)
348d480f 12914 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12915 /* Don't detect duplicate for tracepoint locations because they are
12916 never duplicated. See the comments in field `duplicate' of
12917 `struct bp_location'. */
348d480f 12918 || is_tracepoint (b))
b775012e
LM
12919 {
12920 /* Clear the condition modification flag. */
12921 loc->condition_changed = condition_unchanged;
12922 continue;
12923 }
348d480f
PA
12924
12925 /* Permanent breakpoint should always be inserted. */
1a853c52 12926 if (loc->permanent && ! loc->inserted)
348d480f
PA
12927 internal_error (__FILE__, __LINE__,
12928 _("allegedly permanent breakpoint is not "
12929 "actually inserted"));
12930
12931 if (b->type == bp_hardware_watchpoint)
12932 loc_first_p = &wp_loc_first;
12933 else if (b->type == bp_read_watchpoint)
12934 loc_first_p = &rwp_loc_first;
12935 else if (b->type == bp_access_watchpoint)
12936 loc_first_p = &awp_loc_first;
12937 else
12938 loc_first_p = &bp_loc_first;
12939
12940 if (*loc_first_p == NULL
12941 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12942 || !breakpoint_locations_match (loc, *loc_first_p))
12943 {
12944 *loc_first_p = loc;
12945 loc->duplicate = 0;
b775012e
LM
12946
12947 if (is_breakpoint (loc->owner) && loc->condition_changed)
12948 {
12949 loc->needs_update = 1;
12950 /* Clear the condition modification flag. */
12951 loc->condition_changed = condition_unchanged;
12952 }
348d480f
PA
12953 continue;
12954 }
12955
934709f0
PW
12956
12957 /* This and the above ensure the invariant that the first location
12958 is not duplicated, and is the inserted one.
12959 All following are marked as duplicated, and are not inserted. */
12960 if (loc->inserted)
12961 swap_insertion (loc, *loc_first_p);
348d480f
PA
12962 loc->duplicate = 1;
12963
b775012e
LM
12964 /* Clear the condition modification flag. */
12965 loc->condition_changed = condition_unchanged;
12966
1a853c52
PA
12967 if (loc->inserted && !loc->permanent
12968 && (*loc_first_p)->permanent)
348d480f
PA
12969 internal_error (__FILE__, __LINE__,
12970 _("another breakpoint was inserted on top of "
12971 "a permanent breakpoint"));
12972 }
12973
a25a5a45 12974 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12975 {
04086b45 12976 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12977 insert_breakpoint_locations ();
12978 else
12979 {
44702360
PA
12980 /* Even though the caller told us to not insert new
12981 locations, we may still need to update conditions on the
12982 target's side of breakpoints that were already inserted
12983 if the target is evaluating breakpoint conditions. We
b775012e
LM
12984 only update conditions for locations that are marked
12985 "needs_update". */
12986 update_inserted_breakpoint_locations ();
12987 }
12988 }
348d480f 12989
04086b45 12990 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12991 download_tracepoint_locations ();
12992
348d480f
PA
12993 do_cleanups (cleanups);
12994}
12995
12996void
12997breakpoint_retire_moribund (void)
12998{
12999 struct bp_location *loc;
13000 int ix;
13001
13002 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
13003 if (--(loc->events_till_retirement) == 0)
13004 {
13005 decref_bp_location (&loc);
13006 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
13007 --ix;
13008 }
13009}
13010
13011static void
44702360 13012update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 13013{
bfd189b1 13014 volatile struct gdb_exception e;
348d480f
PA
13015
13016 TRY_CATCH (e, RETURN_MASK_ERROR)
44702360 13017 update_global_location_list (insert_mode);
348d480f
PA
13018}
13019
13020/* Clear BKP from a BPS. */
13021
13022static void
13023bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
13024{
13025 bpstat bs;
13026
13027 for (bs = bps; bs; bs = bs->next)
13028 if (bs->breakpoint_at == bpt)
13029 {
13030 bs->breakpoint_at = NULL;
13031 bs->old_val = NULL;
13032 /* bs->commands will be freed later. */
13033 }
13034}
13035
13036/* Callback for iterate_over_threads. */
13037static int
13038bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
13039{
13040 struct breakpoint *bpt = data;
13041
13042 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
13043 return 0;
13044}
13045
13046/* Helper for breakpoint and tracepoint breakpoint_ops->mention
13047 callbacks. */
13048
13049static void
13050say_where (struct breakpoint *b)
13051{
13052 struct value_print_options opts;
13053
13054 get_user_print_options (&opts);
13055
13056 /* i18n: cagney/2005-02-11: Below needs to be merged into a
13057 single string. */
13058 if (b->loc == NULL)
13059 {
13060 printf_filtered (_(" (%s) pending."), b->addr_string);
13061 }
13062 else
13063 {
2f202fde 13064 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
13065 {
13066 printf_filtered (" at ");
13067 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
13068 gdb_stdout);
13069 }
2f202fde 13070 if (b->loc->symtab != NULL)
f8eba3c6
TT
13071 {
13072 /* If there is a single location, we can print the location
13073 more nicely. */
13074 if (b->loc->next == NULL)
13075 printf_filtered (": file %s, line %d.",
05cba821
JK
13076 symtab_to_filename_for_display (b->loc->symtab),
13077 b->loc->line_number);
f8eba3c6
TT
13078 else
13079 /* This is not ideal, but each location may have a
13080 different file name, and this at least reflects the
13081 real situation somewhat. */
13082 printf_filtered (": %s.", b->addr_string);
13083 }
348d480f
PA
13084
13085 if (b->loc->next)
13086 {
13087 struct bp_location *loc = b->loc;
13088 int n = 0;
13089 for (; loc; loc = loc->next)
13090 ++n;
13091 printf_filtered (" (%d locations)", n);
13092 }
13093 }
13094}
13095
348d480f
PA
13096/* Default bp_location_ops methods. */
13097
13098static void
13099bp_location_dtor (struct bp_location *self)
13100{
13101 xfree (self->cond);
b775012e
LM
13102 if (self->cond_bytecode)
13103 free_agent_expr (self->cond_bytecode);
348d480f 13104 xfree (self->function_name);
8b4f3082
PA
13105
13106 VEC_free (agent_expr_p, self->target_info.conditions);
13107 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
13108}
13109
13110static const struct bp_location_ops bp_location_ops =
13111{
13112 bp_location_dtor
13113};
13114
2060206e
PA
13115/* Default breakpoint_ops methods all breakpoint_ops ultimately
13116 inherit from. */
348d480f 13117
2060206e
PA
13118static void
13119base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
13120{
13121 decref_counted_command_line (&self->commands);
13122 xfree (self->cond_string);
fb81d016 13123 xfree (self->extra_string);
348d480f 13124 xfree (self->addr_string);
f8eba3c6 13125 xfree (self->filter);
348d480f 13126 xfree (self->addr_string_range_end);
348d480f
PA
13127}
13128
2060206e
PA
13129static struct bp_location *
13130base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
13131{
13132 struct bp_location *loc;
13133
13134 loc = XNEW (struct bp_location);
13135 init_bp_location (loc, &bp_location_ops, self);
13136 return loc;
13137}
13138
2060206e
PA
13139static void
13140base_breakpoint_re_set (struct breakpoint *b)
13141{
13142 /* Nothing to re-set. */
13143}
13144
13145#define internal_error_pure_virtual_called() \
13146 gdb_assert_not_reached ("pure virtual function called")
13147
13148static int
13149base_breakpoint_insert_location (struct bp_location *bl)
13150{
13151 internal_error_pure_virtual_called ();
13152}
13153
13154static int
13155base_breakpoint_remove_location (struct bp_location *bl)
13156{
13157 internal_error_pure_virtual_called ();
13158}
13159
13160static int
13161base_breakpoint_breakpoint_hit (const struct bp_location *bl,
13162 struct address_space *aspace,
09ac7c10
TT
13163 CORE_ADDR bp_addr,
13164 const struct target_waitstatus *ws)
2060206e
PA
13165{
13166 internal_error_pure_virtual_called ();
13167}
13168
13169static void
13170base_breakpoint_check_status (bpstat bs)
13171{
13172 /* Always stop. */
13173}
13174
13175/* A "works_in_software_mode" breakpoint_ops method that just internal
13176 errors. */
13177
13178static int
13179base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13180{
13181 internal_error_pure_virtual_called ();
13182}
13183
13184/* A "resources_needed" breakpoint_ops method that just internal
13185 errors. */
13186
13187static int
13188base_breakpoint_resources_needed (const struct bp_location *bl)
13189{
13190 internal_error_pure_virtual_called ();
13191}
13192
13193static enum print_stop_action
13194base_breakpoint_print_it (bpstat bs)
13195{
13196 internal_error_pure_virtual_called ();
13197}
13198
13199static void
13200base_breakpoint_print_one_detail (const struct breakpoint *self,
13201 struct ui_out *uiout)
13202{
13203 /* nothing */
13204}
13205
13206static void
13207base_breakpoint_print_mention (struct breakpoint *b)
13208{
13209 internal_error_pure_virtual_called ();
13210}
13211
13212static void
13213base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13214{
13215 internal_error_pure_virtual_called ();
13216}
13217
983af33b
SDJ
13218static void
13219base_breakpoint_create_sals_from_address (char **arg,
13220 struct linespec_result *canonical,
13221 enum bptype type_wanted,
13222 char *addr_start,
13223 char **copy_arg)
13224{
13225 internal_error_pure_virtual_called ();
13226}
13227
13228static void
13229base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13230 struct linespec_result *c,
983af33b 13231 char *cond_string,
e7e0cddf 13232 char *extra_string,
983af33b
SDJ
13233 enum bptype type_wanted,
13234 enum bpdisp disposition,
13235 int thread,
13236 int task, int ignore_count,
13237 const struct breakpoint_ops *o,
13238 int from_tty, int enabled,
44f238bb 13239 int internal, unsigned flags)
983af33b
SDJ
13240{
13241 internal_error_pure_virtual_called ();
13242}
13243
13244static void
13245base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13246 struct symtabs_and_lines *sals)
13247{
13248 internal_error_pure_virtual_called ();
13249}
13250
ab04a2af
TT
13251/* The default 'explains_signal' method. */
13252
47591c29 13253static int
427cd150 13254base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13255{
47591c29 13256 return 1;
ab04a2af
TT
13257}
13258
9d6e6e84
HZ
13259/* The default "after_condition_true" method. */
13260
13261static void
13262base_breakpoint_after_condition_true (struct bpstats *bs)
13263{
13264 /* Nothing to do. */
13265}
13266
ab04a2af 13267struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13268{
13269 base_breakpoint_dtor,
13270 base_breakpoint_allocate_location,
13271 base_breakpoint_re_set,
13272 base_breakpoint_insert_location,
13273 base_breakpoint_remove_location,
13274 base_breakpoint_breakpoint_hit,
13275 base_breakpoint_check_status,
13276 base_breakpoint_resources_needed,
13277 base_breakpoint_works_in_software_mode,
13278 base_breakpoint_print_it,
13279 NULL,
13280 base_breakpoint_print_one_detail,
13281 base_breakpoint_print_mention,
983af33b
SDJ
13282 base_breakpoint_print_recreate,
13283 base_breakpoint_create_sals_from_address,
13284 base_breakpoint_create_breakpoints_sal,
13285 base_breakpoint_decode_linespec,
9d6e6e84
HZ
13286 base_breakpoint_explains_signal,
13287 base_breakpoint_after_condition_true,
2060206e
PA
13288};
13289
13290/* Default breakpoint_ops methods. */
13291
13292static void
348d480f
PA
13293bkpt_re_set (struct breakpoint *b)
13294{
06edf0c0
PA
13295 /* FIXME: is this still reachable? */
13296 if (b->addr_string == NULL)
13297 {
13298 /* Anything without a string can't be re-set. */
348d480f 13299 delete_breakpoint (b);
06edf0c0 13300 return;
348d480f 13301 }
06edf0c0
PA
13302
13303 breakpoint_re_set_default (b);
348d480f
PA
13304}
13305
2060206e 13306static int
348d480f
PA
13307bkpt_insert_location (struct bp_location *bl)
13308{
13309 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 13310 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 13311 else
7c16b83e 13312 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13313}
13314
2060206e 13315static int
348d480f
PA
13316bkpt_remove_location (struct bp_location *bl)
13317{
13318 if (bl->loc_type == bp_loc_hardware_breakpoint)
13319 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13320 else
7c16b83e 13321 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13322}
13323
2060206e 13324static int
348d480f 13325bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13326 struct address_space *aspace, CORE_ADDR bp_addr,
13327 const struct target_waitstatus *ws)
348d480f 13328{
09ac7c10 13329 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13330 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13331 return 0;
13332
348d480f
PA
13333 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13334 aspace, bp_addr))
13335 return 0;
13336
13337 if (overlay_debugging /* unmapped overlay section */
13338 && section_is_overlay (bl->section)
13339 && !section_is_mapped (bl->section))
13340 return 0;
13341
13342 return 1;
13343}
13344
cd1608cc
PA
13345static int
13346dprintf_breakpoint_hit (const struct bp_location *bl,
13347 struct address_space *aspace, CORE_ADDR bp_addr,
13348 const struct target_waitstatus *ws)
13349{
13350 if (dprintf_style == dprintf_style_agent
13351 && target_can_run_breakpoint_commands ())
13352 {
13353 /* An agent-style dprintf never causes a stop. If we see a trap
13354 for this address it must be for a breakpoint that happens to
13355 be set at the same address. */
13356 return 0;
13357 }
13358
13359 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13360}
13361
2060206e 13362static int
348d480f
PA
13363bkpt_resources_needed (const struct bp_location *bl)
13364{
13365 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13366
13367 return 1;
13368}
13369
2060206e 13370static enum print_stop_action
348d480f
PA
13371bkpt_print_it (bpstat bs)
13372{
348d480f
PA
13373 struct breakpoint *b;
13374 const struct bp_location *bl;
001c8c33 13375 int bp_temp;
79a45e25 13376 struct ui_out *uiout = current_uiout;
348d480f
PA
13377
13378 gdb_assert (bs->bp_location_at != NULL);
13379
13380 bl = bs->bp_location_at;
13381 b = bs->breakpoint_at;
13382
001c8c33
PA
13383 bp_temp = b->disposition == disp_del;
13384 if (bl->address != bl->requested_address)
13385 breakpoint_adjustment_warning (bl->requested_address,
13386 bl->address,
13387 b->number, 1);
13388 annotate_breakpoint (b->number);
13389 if (bp_temp)
13390 ui_out_text (uiout, "\nTemporary breakpoint ");
13391 else
13392 ui_out_text (uiout, "\nBreakpoint ");
13393 if (ui_out_is_mi_like_p (uiout))
348d480f 13394 {
001c8c33
PA
13395 ui_out_field_string (uiout, "reason",
13396 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13397 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13398 }
001c8c33
PA
13399 ui_out_field_int (uiout, "bkptno", b->number);
13400 ui_out_text (uiout, ", ");
06edf0c0 13401
001c8c33 13402 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13403}
13404
2060206e 13405static void
06edf0c0
PA
13406bkpt_print_mention (struct breakpoint *b)
13407{
79a45e25 13408 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13409 return;
13410
13411 switch (b->type)
13412 {
13413 case bp_breakpoint:
13414 case bp_gnu_ifunc_resolver:
13415 if (b->disposition == disp_del)
13416 printf_filtered (_("Temporary breakpoint"));
13417 else
13418 printf_filtered (_("Breakpoint"));
13419 printf_filtered (_(" %d"), b->number);
13420 if (b->type == bp_gnu_ifunc_resolver)
13421 printf_filtered (_(" at gnu-indirect-function resolver"));
13422 break;
13423 case bp_hardware_breakpoint:
13424 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13425 break;
e7e0cddf
SS
13426 case bp_dprintf:
13427 printf_filtered (_("Dprintf %d"), b->number);
13428 break;
06edf0c0
PA
13429 }
13430
13431 say_where (b);
13432}
13433
2060206e 13434static void
06edf0c0
PA
13435bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13436{
13437 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13438 fprintf_unfiltered (fp, "tbreak");
13439 else if (tp->type == bp_breakpoint)
13440 fprintf_unfiltered (fp, "break");
13441 else if (tp->type == bp_hardware_breakpoint
13442 && tp->disposition == disp_del)
13443 fprintf_unfiltered (fp, "thbreak");
13444 else if (tp->type == bp_hardware_breakpoint)
13445 fprintf_unfiltered (fp, "hbreak");
13446 else
13447 internal_error (__FILE__, __LINE__,
13448 _("unhandled breakpoint type %d"), (int) tp->type);
13449
2060206e 13450 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13451 print_recreate_thread (tp, fp);
06edf0c0
PA
13452}
13453
983af33b
SDJ
13454static void
13455bkpt_create_sals_from_address (char **arg,
13456 struct linespec_result *canonical,
13457 enum bptype type_wanted,
13458 char *addr_start, char **copy_arg)
13459{
13460 create_sals_from_address_default (arg, canonical, type_wanted,
13461 addr_start, copy_arg);
13462}
13463
13464static void
13465bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13466 struct linespec_result *canonical,
983af33b 13467 char *cond_string,
e7e0cddf 13468 char *extra_string,
983af33b
SDJ
13469 enum bptype type_wanted,
13470 enum bpdisp disposition,
13471 int thread,
13472 int task, int ignore_count,
13473 const struct breakpoint_ops *ops,
13474 int from_tty, int enabled,
44f238bb 13475 int internal, unsigned flags)
983af33b 13476{
023fa29b 13477 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13478 cond_string, extra_string,
13479 type_wanted,
983af33b
SDJ
13480 disposition, thread, task,
13481 ignore_count, ops, from_tty,
44f238bb 13482 enabled, internal, flags);
983af33b
SDJ
13483}
13484
13485static void
13486bkpt_decode_linespec (struct breakpoint *b, char **s,
13487 struct symtabs_and_lines *sals)
13488{
13489 decode_linespec_default (b, s, sals);
13490}
13491
06edf0c0
PA
13492/* Virtual table for internal breakpoints. */
13493
13494static void
13495internal_bkpt_re_set (struct breakpoint *b)
13496{
13497 switch (b->type)
13498 {
13499 /* Delete overlay event and longjmp master breakpoints; they
13500 will be reset later by breakpoint_re_set. */
13501 case bp_overlay_event:
13502 case bp_longjmp_master:
13503 case bp_std_terminate_master:
13504 case bp_exception_master:
13505 delete_breakpoint (b);
13506 break;
13507
13508 /* This breakpoint is special, it's set up when the inferior
13509 starts and we really don't want to touch it. */
13510 case bp_shlib_event:
13511
13512 /* Like bp_shlib_event, this breakpoint type is special. Once
13513 it is set up, we do not want to touch it. */
13514 case bp_thread_event:
13515 break;
13516 }
13517}
13518
13519static void
13520internal_bkpt_check_status (bpstat bs)
13521{
a9b3a50f
PA
13522 if (bs->breakpoint_at->type == bp_shlib_event)
13523 {
13524 /* If requested, stop when the dynamic linker notifies GDB of
13525 events. This allows the user to get control and place
13526 breakpoints in initializer routines for dynamically loaded
13527 objects (among other things). */
13528 bs->stop = stop_on_solib_events;
13529 bs->print = stop_on_solib_events;
13530 }
13531 else
13532 bs->stop = 0;
06edf0c0
PA
13533}
13534
13535static enum print_stop_action
13536internal_bkpt_print_it (bpstat bs)
13537{
06edf0c0 13538 struct breakpoint *b;
06edf0c0 13539
06edf0c0
PA
13540 b = bs->breakpoint_at;
13541
06edf0c0
PA
13542 switch (b->type)
13543 {
348d480f
PA
13544 case bp_shlib_event:
13545 /* Did we stop because the user set the stop_on_solib_events
13546 variable? (If so, we report this as a generic, "Stopped due
13547 to shlib event" message.) */
edcc5120 13548 print_solib_event (0);
348d480f
PA
13549 break;
13550
13551 case bp_thread_event:
13552 /* Not sure how we will get here.
13553 GDB should not stop for these breakpoints. */
13554 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13555 break;
13556
13557 case bp_overlay_event:
13558 /* By analogy with the thread event, GDB should not stop for these. */
13559 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13560 break;
13561
13562 case bp_longjmp_master:
13563 /* These should never be enabled. */
13564 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13565 break;
13566
13567 case bp_std_terminate_master:
13568 /* These should never be enabled. */
13569 printf_filtered (_("std::terminate Master Breakpoint: "
13570 "gdb should not stop!\n"));
348d480f
PA
13571 break;
13572
13573 case bp_exception_master:
13574 /* These should never be enabled. */
13575 printf_filtered (_("Exception Master Breakpoint: "
13576 "gdb should not stop!\n"));
06edf0c0
PA
13577 break;
13578 }
13579
001c8c33 13580 return PRINT_NOTHING;
06edf0c0
PA
13581}
13582
13583static void
13584internal_bkpt_print_mention (struct breakpoint *b)
13585{
13586 /* Nothing to mention. These breakpoints are internal. */
13587}
13588
06edf0c0
PA
13589/* Virtual table for momentary breakpoints */
13590
13591static void
13592momentary_bkpt_re_set (struct breakpoint *b)
13593{
13594 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13595 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13596 Otherwise these should have been blown away via the cleanup chain
13597 or by breakpoint_init_inferior when we rerun the executable. */
13598}
13599
13600static void
13601momentary_bkpt_check_status (bpstat bs)
13602{
13603 /* Nothing. The point of these breakpoints is causing a stop. */
13604}
13605
13606static enum print_stop_action
13607momentary_bkpt_print_it (bpstat bs)
13608{
79a45e25
PA
13609 struct ui_out *uiout = current_uiout;
13610
001c8c33 13611 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13612 {
001c8c33 13613 struct breakpoint *b = bs->breakpoint_at;
348d480f 13614
001c8c33
PA
13615 switch (b->type)
13616 {
13617 case bp_finish:
13618 ui_out_field_string
13619 (uiout, "reason",
13620 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13621 break;
348d480f 13622
001c8c33
PA
13623 case bp_until:
13624 ui_out_field_string
13625 (uiout, "reason",
13626 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13627 break;
13628 }
348d480f
PA
13629 }
13630
001c8c33 13631 return PRINT_UNKNOWN;
348d480f
PA
13632}
13633
06edf0c0
PA
13634static void
13635momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13636{
06edf0c0 13637 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13638}
13639
e2e4d78b
JK
13640/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13641
13642 It gets cleared already on the removal of the first one of such placed
13643 breakpoints. This is OK as they get all removed altogether. */
13644
13645static void
13646longjmp_bkpt_dtor (struct breakpoint *self)
13647{
13648 struct thread_info *tp = find_thread_id (self->thread);
13649
13650 if (tp)
13651 tp->initiating_frame = null_frame_id;
13652
13653 momentary_breakpoint_ops.dtor (self);
13654}
13655
55aa24fb
SDJ
13656/* Specific methods for probe breakpoints. */
13657
13658static int
13659bkpt_probe_insert_location (struct bp_location *bl)
13660{
13661 int v = bkpt_insert_location (bl);
13662
13663 if (v == 0)
13664 {
13665 /* The insertion was successful, now let's set the probe's semaphore
13666 if needed. */
0ea5cda8
SDJ
13667 if (bl->probe.probe->pops->set_semaphore != NULL)
13668 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13669 bl->probe.objfile,
13670 bl->gdbarch);
55aa24fb
SDJ
13671 }
13672
13673 return v;
13674}
13675
13676static int
13677bkpt_probe_remove_location (struct bp_location *bl)
13678{
13679 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13680 if (bl->probe.probe->pops->clear_semaphore != NULL)
13681 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13682 bl->probe.objfile,
13683 bl->gdbarch);
55aa24fb
SDJ
13684
13685 return bkpt_remove_location (bl);
13686}
13687
13688static void
13689bkpt_probe_create_sals_from_address (char **arg,
13690 struct linespec_result *canonical,
13691 enum bptype type_wanted,
13692 char *addr_start, char **copy_arg)
13693{
13694 struct linespec_sals lsal;
13695
13696 lsal.sals = parse_probes (arg, canonical);
13697
13698 *copy_arg = xstrdup (canonical->addr_string);
13699 lsal.canonical = xstrdup (*copy_arg);
13700
13701 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13702}
13703
13704static void
13705bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13706 struct symtabs_and_lines *sals)
13707{
13708 *sals = parse_probes (s, NULL);
13709 if (!sals->sals)
13710 error (_("probe not found"));
13711}
13712
348d480f 13713/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13714
348d480f
PA
13715static void
13716tracepoint_re_set (struct breakpoint *b)
13717{
13718 breakpoint_re_set_default (b);
13719}
876fa593 13720
348d480f
PA
13721static int
13722tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13723 struct address_space *aspace, CORE_ADDR bp_addr,
13724 const struct target_waitstatus *ws)
348d480f
PA
13725{
13726 /* By definition, the inferior does not report stops at
13727 tracepoints. */
13728 return 0;
74960c60
VP
13729}
13730
13731static void
348d480f
PA
13732tracepoint_print_one_detail (const struct breakpoint *self,
13733 struct ui_out *uiout)
74960c60 13734{
d9b3f62e
PA
13735 struct tracepoint *tp = (struct tracepoint *) self;
13736 if (tp->static_trace_marker_id)
348d480f
PA
13737 {
13738 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13739
348d480f
PA
13740 ui_out_text (uiout, "\tmarker id is ");
13741 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13742 tp->static_trace_marker_id);
348d480f
PA
13743 ui_out_text (uiout, "\n");
13744 }
0d381245
VP
13745}
13746
a474d7c2 13747static void
348d480f 13748tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13749{
79a45e25 13750 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13751 return;
cc59ec59 13752
348d480f
PA
13753 switch (b->type)
13754 {
13755 case bp_tracepoint:
13756 printf_filtered (_("Tracepoint"));
13757 printf_filtered (_(" %d"), b->number);
13758 break;
13759 case bp_fast_tracepoint:
13760 printf_filtered (_("Fast tracepoint"));
13761 printf_filtered (_(" %d"), b->number);
13762 break;
13763 case bp_static_tracepoint:
13764 printf_filtered (_("Static tracepoint"));
13765 printf_filtered (_(" %d"), b->number);
13766 break;
13767 default:
13768 internal_error (__FILE__, __LINE__,
13769 _("unhandled tracepoint type %d"), (int) b->type);
13770 }
13771
13772 say_where (b);
a474d7c2
PA
13773}
13774
348d480f 13775static void
d9b3f62e 13776tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13777{
d9b3f62e
PA
13778 struct tracepoint *tp = (struct tracepoint *) self;
13779
13780 if (self->type == bp_fast_tracepoint)
348d480f 13781 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13782 if (self->type == bp_static_tracepoint)
348d480f 13783 fprintf_unfiltered (fp, "strace");
d9b3f62e 13784 else if (self->type == bp_tracepoint)
348d480f
PA
13785 fprintf_unfiltered (fp, "trace");
13786 else
13787 internal_error (__FILE__, __LINE__,
d9b3f62e 13788 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13789
d9b3f62e
PA
13790 fprintf_unfiltered (fp, " %s", self->addr_string);
13791 print_recreate_thread (self, fp);
13792
13793 if (tp->pass_count)
13794 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13795}
13796
983af33b
SDJ
13797static void
13798tracepoint_create_sals_from_address (char **arg,
13799 struct linespec_result *canonical,
13800 enum bptype type_wanted,
13801 char *addr_start, char **copy_arg)
13802{
13803 create_sals_from_address_default (arg, canonical, type_wanted,
13804 addr_start, copy_arg);
13805}
13806
13807static void
13808tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13809 struct linespec_result *canonical,
983af33b 13810 char *cond_string,
e7e0cddf 13811 char *extra_string,
983af33b
SDJ
13812 enum bptype type_wanted,
13813 enum bpdisp disposition,
13814 int thread,
13815 int task, int ignore_count,
13816 const struct breakpoint_ops *ops,
13817 int from_tty, int enabled,
44f238bb 13818 int internal, unsigned flags)
983af33b 13819{
023fa29b 13820 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13821 cond_string, extra_string,
13822 type_wanted,
983af33b
SDJ
13823 disposition, thread, task,
13824 ignore_count, ops, from_tty,
44f238bb 13825 enabled, internal, flags);
983af33b
SDJ
13826}
13827
13828static void
13829tracepoint_decode_linespec (struct breakpoint *b, char **s,
13830 struct symtabs_and_lines *sals)
13831{
13832 decode_linespec_default (b, s, sals);
13833}
13834
2060206e 13835struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13836
55aa24fb
SDJ
13837/* The breakpoint_ops structure to be use on tracepoints placed in a
13838 static probe. */
13839
13840static void
13841tracepoint_probe_create_sals_from_address (char **arg,
13842 struct linespec_result *canonical,
13843 enum bptype type_wanted,
13844 char *addr_start, char **copy_arg)
13845{
13846 /* We use the same method for breakpoint on probes. */
13847 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13848 addr_start, copy_arg);
13849}
13850
13851static void
13852tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13853 struct symtabs_and_lines *sals)
13854{
13855 /* We use the same method for breakpoint on probes. */
13856 bkpt_probe_decode_linespec (b, s, sals);
13857}
13858
13859static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13860
5c2b4418
HZ
13861/* Dprintf breakpoint_ops methods. */
13862
13863static void
13864dprintf_re_set (struct breakpoint *b)
13865{
13866 breakpoint_re_set_default (b);
13867
13868 /* This breakpoint could have been pending, and be resolved now, and
13869 if so, we should now have the extra string. If we don't, the
13870 dprintf was malformed when created, but we couldn't tell because
13871 we can't extract the extra string until the location is
13872 resolved. */
13873 if (b->loc != NULL && b->extra_string == NULL)
13874 error (_("Format string required"));
13875
13876 /* 1 - connect to target 1, that can run breakpoint commands.
13877 2 - create a dprintf, which resolves fine.
13878 3 - disconnect from target 1
13879 4 - connect to target 2, that can NOT run breakpoint commands.
13880
13881 After steps #3/#4, you'll want the dprintf command list to
13882 be updated, because target 1 and 2 may well return different
13883 answers for target_can_run_breakpoint_commands().
13884 Given absence of finer grained resetting, we get to do
13885 it all the time. */
13886 if (b->extra_string != NULL)
13887 update_dprintf_command_list (b);
13888}
13889
2d9442cc
HZ
13890/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13891
13892static void
13893dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13894{
13895 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13896 tp->extra_string);
13897 print_recreate_thread (tp, fp);
13898}
13899
9d6e6e84
HZ
13900/* Implement the "after_condition_true" breakpoint_ops method for
13901 dprintf.
13902
13903 dprintf's are implemented with regular commands in their command
13904 list, but we run the commands here instead of before presenting the
13905 stop to the user, as dprintf's don't actually cause a stop. This
13906 also makes it so that the commands of multiple dprintfs at the same
13907 address are all handled. */
13908
13909static void
13910dprintf_after_condition_true (struct bpstats *bs)
13911{
13912 struct cleanup *old_chain;
13913 struct bpstats tmp_bs = { NULL };
13914 struct bpstats *tmp_bs_p = &tmp_bs;
13915
13916 /* dprintf's never cause a stop. This wasn't set in the
13917 check_status hook instead because that would make the dprintf's
13918 condition not be evaluated. */
13919 bs->stop = 0;
13920
13921 /* Run the command list here. Take ownership of it instead of
13922 copying. We never want these commands to run later in
13923 bpstat_do_actions, if a breakpoint that causes a stop happens to
13924 be set at same address as this dprintf, or even if running the
13925 commands here throws. */
13926 tmp_bs.commands = bs->commands;
13927 bs->commands = NULL;
13928 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13929
13930 bpstat_do_actions_1 (&tmp_bs_p);
13931
13932 /* 'tmp_bs.commands' will usually be NULL by now, but
13933 bpstat_do_actions_1 may return early without processing the whole
13934 list. */
13935 do_cleanups (old_chain);
13936}
13937
983af33b
SDJ
13938/* The breakpoint_ops structure to be used on static tracepoints with
13939 markers (`-m'). */
13940
13941static void
13942strace_marker_create_sals_from_address (char **arg,
13943 struct linespec_result *canonical,
13944 enum bptype type_wanted,
13945 char *addr_start, char **copy_arg)
13946{
13947 struct linespec_sals lsal;
13948
13949 lsal.sals = decode_static_tracepoint_spec (arg);
13950
13951 *copy_arg = savestring (addr_start, *arg - addr_start);
13952
13953 canonical->addr_string = xstrdup (*copy_arg);
13954 lsal.canonical = xstrdup (*copy_arg);
13955 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13956}
13957
13958static void
13959strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13960 struct linespec_result *canonical,
983af33b 13961 char *cond_string,
e7e0cddf 13962 char *extra_string,
983af33b
SDJ
13963 enum bptype type_wanted,
13964 enum bpdisp disposition,
13965 int thread,
13966 int task, int ignore_count,
13967 const struct breakpoint_ops *ops,
13968 int from_tty, int enabled,
44f238bb 13969 int internal, unsigned flags)
983af33b
SDJ
13970{
13971 int i;
52d361e1
YQ
13972 struct linespec_sals *lsal = VEC_index (linespec_sals,
13973 canonical->sals, 0);
983af33b
SDJ
13974
13975 /* If the user is creating a static tracepoint by marker id
13976 (strace -m MARKER_ID), then store the sals index, so that
13977 breakpoint_re_set can try to match up which of the newly
13978 found markers corresponds to this one, and, don't try to
13979 expand multiple locations for each sal, given than SALS
13980 already should contain all sals for MARKER_ID. */
13981
13982 for (i = 0; i < lsal->sals.nelts; ++i)
13983 {
13984 struct symtabs_and_lines expanded;
13985 struct tracepoint *tp;
13986 struct cleanup *old_chain;
13987 char *addr_string;
13988
13989 expanded.nelts = 1;
13990 expanded.sals = &lsal->sals.sals[i];
13991
13992 addr_string = xstrdup (canonical->addr_string);
13993 old_chain = make_cleanup (xfree, addr_string);
13994
13995 tp = XCNEW (struct tracepoint);
13996 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13997 addr_string, NULL,
e7e0cddf
SS
13998 cond_string, extra_string,
13999 type_wanted, disposition,
983af33b 14000 thread, task, ignore_count, ops,
44f238bb 14001 from_tty, enabled, internal, flags,
983af33b
SDJ
14002 canonical->special_display);
14003 /* Given that its possible to have multiple markers with
14004 the same string id, if the user is creating a static
14005 tracepoint by marker id ("strace -m MARKER_ID"), then
14006 store the sals index, so that breakpoint_re_set can
14007 try to match up which of the newly found markers
14008 corresponds to this one */
14009 tp->static_trace_marker_id_idx = i;
14010
14011 install_breakpoint (internal, &tp->base, 0);
14012
14013 discard_cleanups (old_chain);
14014 }
14015}
14016
14017static void
14018strace_marker_decode_linespec (struct breakpoint *b, char **s,
14019 struct symtabs_and_lines *sals)
14020{
14021 struct tracepoint *tp = (struct tracepoint *) b;
14022
14023 *sals = decode_static_tracepoint_spec (s);
14024 if (sals->nelts > tp->static_trace_marker_id_idx)
14025 {
14026 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
14027 sals->nelts = 1;
14028 }
14029 else
14030 error (_("marker %s not found"), tp->static_trace_marker_id);
14031}
14032
14033static struct breakpoint_ops strace_marker_breakpoint_ops;
14034
14035static int
14036strace_marker_p (struct breakpoint *b)
14037{
14038 return b->ops == &strace_marker_breakpoint_ops;
14039}
14040
53a5351d 14041/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 14042 structures. */
c906108c
SS
14043
14044void
fba45db2 14045delete_breakpoint (struct breakpoint *bpt)
c906108c 14046{
52f0bd74 14047 struct breakpoint *b;
c906108c 14048
8a3fe4f8 14049 gdb_assert (bpt != NULL);
c906108c 14050
4a64f543
MS
14051 /* Has this bp already been deleted? This can happen because
14052 multiple lists can hold pointers to bp's. bpstat lists are
14053 especial culprits.
14054
14055 One example of this happening is a watchpoint's scope bp. When
14056 the scope bp triggers, we notice that the watchpoint is out of
14057 scope, and delete it. We also delete its scope bp. But the
14058 scope bp is marked "auto-deleting", and is already on a bpstat.
14059 That bpstat is then checked for auto-deleting bp's, which are
14060 deleted.
14061
14062 A real solution to this problem might involve reference counts in
14063 bp's, and/or giving them pointers back to their referencing
14064 bpstat's, and teaching delete_breakpoint to only free a bp's
14065 storage when no more references were extent. A cheaper bandaid
14066 was chosen. */
c906108c
SS
14067 if (bpt->type == bp_none)
14068 return;
14069
4a64f543
MS
14070 /* At least avoid this stale reference until the reference counting
14071 of breakpoints gets resolved. */
d0fb5eae 14072 if (bpt->related_breakpoint != bpt)
e5a0a904 14073 {
d0fb5eae 14074 struct breakpoint *related;
3a5c3e22 14075 struct watchpoint *w;
d0fb5eae
JK
14076
14077 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 14078 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 14079 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
14080 w = (struct watchpoint *) bpt;
14081 else
14082 w = NULL;
14083 if (w != NULL)
14084 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
14085
14086 /* Unlink bpt from the bpt->related_breakpoint ring. */
14087 for (related = bpt; related->related_breakpoint != bpt;
14088 related = related->related_breakpoint);
14089 related->related_breakpoint = bpt->related_breakpoint;
14090 bpt->related_breakpoint = bpt;
e5a0a904
JK
14091 }
14092
a9634178
TJB
14093 /* watch_command_1 creates a watchpoint but only sets its number if
14094 update_watchpoint succeeds in creating its bp_locations. If there's
14095 a problem in that process, we'll be asked to delete the half-created
14096 watchpoint. In that case, don't announce the deletion. */
14097 if (bpt->number)
14098 observer_notify_breakpoint_deleted (bpt);
c906108c 14099
c906108c
SS
14100 if (breakpoint_chain == bpt)
14101 breakpoint_chain = bpt->next;
14102
c906108c
SS
14103 ALL_BREAKPOINTS (b)
14104 if (b->next == bpt)
c5aa993b
JM
14105 {
14106 b->next = bpt->next;
14107 break;
14108 }
c906108c 14109
f431efe5
PA
14110 /* Be sure no bpstat's are pointing at the breakpoint after it's
14111 been freed. */
14112 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 14113 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
14114 pointing at bpt from the stop_bpstat list entirely, as breakpoint
14115 commands are associated with the bpstat; if we remove it here,
14116 then the later call to bpstat_do_actions (&stop_bpstat); in
14117 event-top.c won't do anything, and temporary breakpoints with
14118 commands won't work. */
14119
14120 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
14121
4a64f543
MS
14122 /* Now that breakpoint is removed from breakpoint list, update the
14123 global location list. This will remove locations that used to
14124 belong to this breakpoint. Do this before freeing the breakpoint
14125 itself, since remove_breakpoint looks at location's owner. It
14126 might be better design to have location completely
14127 self-contained, but it's not the case now. */
44702360 14128 update_global_location_list (UGLL_DONT_INSERT);
74960c60 14129
348d480f 14130 bpt->ops->dtor (bpt);
4a64f543
MS
14131 /* On the chance that someone will soon try again to delete this
14132 same bp, we mark it as deleted before freeing its storage. */
c906108c 14133 bpt->type = bp_none;
b8c9b27d 14134 xfree (bpt);
c906108c
SS
14135}
14136
4d6140d9
AC
14137static void
14138do_delete_breakpoint_cleanup (void *b)
14139{
14140 delete_breakpoint (b);
14141}
14142
14143struct cleanup *
14144make_cleanup_delete_breakpoint (struct breakpoint *b)
14145{
14146 return make_cleanup (do_delete_breakpoint_cleanup, b);
14147}
14148
51be5b68
PA
14149/* Iterator function to call a user-provided callback function once
14150 for each of B and its related breakpoints. */
14151
14152static void
14153iterate_over_related_breakpoints (struct breakpoint *b,
14154 void (*function) (struct breakpoint *,
14155 void *),
14156 void *data)
14157{
14158 struct breakpoint *related;
14159
14160 related = b;
14161 do
14162 {
14163 struct breakpoint *next;
14164
14165 /* FUNCTION may delete RELATED. */
14166 next = related->related_breakpoint;
14167
14168 if (next == related)
14169 {
14170 /* RELATED is the last ring entry. */
14171 function (related, data);
14172
14173 /* FUNCTION may have deleted it, so we'd never reach back to
14174 B. There's nothing left to do anyway, so just break
14175 out. */
14176 break;
14177 }
14178 else
14179 function (related, data);
14180
14181 related = next;
14182 }
14183 while (related != b);
14184}
95a42b64
TT
14185
14186static void
14187do_delete_breakpoint (struct breakpoint *b, void *ignore)
14188{
14189 delete_breakpoint (b);
14190}
14191
51be5b68
PA
14192/* A callback for map_breakpoint_numbers that calls
14193 delete_breakpoint. */
14194
14195static void
14196do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14197{
14198 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14199}
14200
c906108c 14201void
fba45db2 14202delete_command (char *arg, int from_tty)
c906108c 14203{
35df4500 14204 struct breakpoint *b, *b_tmp;
c906108c 14205
ea9365bb
TT
14206 dont_repeat ();
14207
c906108c
SS
14208 if (arg == 0)
14209 {
14210 int breaks_to_delete = 0;
14211
46c6471b
PA
14212 /* Delete all breakpoints if no argument. Do not delete
14213 internal breakpoints, these have to be deleted with an
14214 explicit breakpoint number argument. */
c5aa993b 14215 ALL_BREAKPOINTS (b)
46c6471b 14216 if (user_breakpoint_p (b))
973d738b
DJ
14217 {
14218 breaks_to_delete = 1;
14219 break;
14220 }
c906108c
SS
14221
14222 /* Ask user only if there are some breakpoints to delete. */
14223 if (!from_tty
e2e0b3e5 14224 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14225 {
35df4500 14226 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14227 if (user_breakpoint_p (b))
c5aa993b 14228 delete_breakpoint (b);
c906108c
SS
14229 }
14230 }
14231 else
51be5b68 14232 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14233}
14234
0d381245
VP
14235static int
14236all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 14237{
0d381245 14238 for (; loc; loc = loc->next)
8645ff69
UW
14239 if (!loc->shlib_disabled
14240 && !loc->pspace->executing_startup)
0d381245
VP
14241 return 0;
14242 return 1;
fe3f5fa8
VP
14243}
14244
776592bf
DE
14245/* Subroutine of update_breakpoint_locations to simplify it.
14246 Return non-zero if multiple fns in list LOC have the same name.
14247 Null names are ignored. */
14248
14249static int
14250ambiguous_names_p (struct bp_location *loc)
14251{
14252 struct bp_location *l;
14253 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14254 (int (*) (const void *,
14255 const void *)) streq,
776592bf
DE
14256 NULL, xcalloc, xfree);
14257
14258 for (l = loc; l != NULL; l = l->next)
14259 {
14260 const char **slot;
14261 const char *name = l->function_name;
14262
14263 /* Allow for some names to be NULL, ignore them. */
14264 if (name == NULL)
14265 continue;
14266
14267 slot = (const char **) htab_find_slot (htab, (const void *) name,
14268 INSERT);
4a64f543
MS
14269 /* NOTE: We can assume slot != NULL here because xcalloc never
14270 returns NULL. */
776592bf
DE
14271 if (*slot != NULL)
14272 {
14273 htab_delete (htab);
14274 return 1;
14275 }
14276 *slot = name;
14277 }
14278
14279 htab_delete (htab);
14280 return 0;
14281}
14282
0fb4aa4b
PA
14283/* When symbols change, it probably means the sources changed as well,
14284 and it might mean the static tracepoint markers are no longer at
14285 the same address or line numbers they used to be at last we
14286 checked. Losing your static tracepoints whenever you rebuild is
14287 undesirable. This function tries to resync/rematch gdb static
14288 tracepoints with the markers on the target, for static tracepoints
14289 that have not been set by marker id. Static tracepoint that have
14290 been set by marker id are reset by marker id in breakpoint_re_set.
14291 The heuristic is:
14292
14293 1) For a tracepoint set at a specific address, look for a marker at
14294 the old PC. If one is found there, assume to be the same marker.
14295 If the name / string id of the marker found is different from the
14296 previous known name, assume that means the user renamed the marker
14297 in the sources, and output a warning.
14298
14299 2) For a tracepoint set at a given line number, look for a marker
14300 at the new address of the old line number. If one is found there,
14301 assume to be the same marker. If the name / string id of the
14302 marker found is different from the previous known name, assume that
14303 means the user renamed the marker in the sources, and output a
14304 warning.
14305
14306 3) If a marker is no longer found at the same address or line, it
14307 may mean the marker no longer exists. But it may also just mean
14308 the code changed a bit. Maybe the user added a few lines of code
14309 that made the marker move up or down (in line number terms). Ask
14310 the target for info about the marker with the string id as we knew
14311 it. If found, update line number and address in the matching
14312 static tracepoint. This will get confused if there's more than one
14313 marker with the same ID (possible in UST, although unadvised
14314 precisely because it confuses tools). */
14315
14316static struct symtab_and_line
14317update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14318{
d9b3f62e 14319 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14320 struct static_tracepoint_marker marker;
14321 CORE_ADDR pc;
0fb4aa4b
PA
14322
14323 pc = sal.pc;
14324 if (sal.line)
14325 find_line_pc (sal.symtab, sal.line, &pc);
14326
14327 if (target_static_tracepoint_marker_at (pc, &marker))
14328 {
d9b3f62e 14329 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14330 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14331 b->number,
d9b3f62e 14332 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14333
d9b3f62e
PA
14334 xfree (tp->static_trace_marker_id);
14335 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14336 release_static_tracepoint_marker (&marker);
14337
14338 return sal;
14339 }
14340
14341 /* Old marker wasn't found on target at lineno. Try looking it up
14342 by string ID. */
14343 if (!sal.explicit_pc
14344 && sal.line != 0
14345 && sal.symtab != NULL
d9b3f62e 14346 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14347 {
14348 VEC(static_tracepoint_marker_p) *markers;
14349
14350 markers
d9b3f62e 14351 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14352
14353 if (!VEC_empty(static_tracepoint_marker_p, markers))
14354 {
80e1d417 14355 struct symtab_and_line sal2;
0fb4aa4b 14356 struct symbol *sym;
80e1d417 14357 struct static_tracepoint_marker *tpmarker;
79a45e25 14358 struct ui_out *uiout = current_uiout;
0fb4aa4b 14359
80e1d417 14360 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14361
d9b3f62e 14362 xfree (tp->static_trace_marker_id);
80e1d417 14363 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14364
14365 warning (_("marker for static tracepoint %d (%s) not "
14366 "found at previous line number"),
d9b3f62e 14367 b->number, tp->static_trace_marker_id);
0fb4aa4b 14368
80e1d417 14369 init_sal (&sal2);
0fb4aa4b 14370
80e1d417 14371 sal2.pc = tpmarker->address;
0fb4aa4b 14372
80e1d417
AS
14373 sal2 = find_pc_line (tpmarker->address, 0);
14374 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14375 ui_out_text (uiout, "Now in ");
14376 if (sym)
14377 {
14378 ui_out_field_string (uiout, "func",
14379 SYMBOL_PRINT_NAME (sym));
14380 ui_out_text (uiout, " at ");
14381 }
05cba821
JK
14382 ui_out_field_string (uiout, "file",
14383 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14384 ui_out_text (uiout, ":");
14385
14386 if (ui_out_is_mi_like_p (uiout))
14387 {
0b0865da 14388 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14389
f35a17b5 14390 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14391 }
14392
80e1d417 14393 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14394 ui_out_text (uiout, "\n");
14395
80e1d417 14396 b->loc->line_number = sal2.line;
2f202fde 14397 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14398
14399 xfree (b->addr_string);
14400 b->addr_string = xstrprintf ("%s:%d",
05cba821 14401 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14402 b->loc->line_number);
0fb4aa4b
PA
14403
14404 /* Might be nice to check if function changed, and warn if
14405 so. */
14406
80e1d417 14407 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14408 }
14409 }
14410 return sal;
14411}
14412
8d3788bd
VP
14413/* Returns 1 iff locations A and B are sufficiently same that
14414 we don't need to report breakpoint as changed. */
14415
14416static int
14417locations_are_equal (struct bp_location *a, struct bp_location *b)
14418{
14419 while (a && b)
14420 {
14421 if (a->address != b->address)
14422 return 0;
14423
14424 if (a->shlib_disabled != b->shlib_disabled)
14425 return 0;
14426
14427 if (a->enabled != b->enabled)
14428 return 0;
14429
14430 a = a->next;
14431 b = b->next;
14432 }
14433
14434 if ((a == NULL) != (b == NULL))
14435 return 0;
14436
14437 return 1;
14438}
14439
f1310107
TJB
14440/* Create new breakpoint locations for B (a hardware or software breakpoint)
14441 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14442 a ranged breakpoint. */
14443
0e30163f 14444void
0d381245 14445update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14446 struct symtabs_and_lines sals,
14447 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14448{
14449 int i;
0d381245
VP
14450 struct bp_location *existing_locations = b->loc;
14451
f8eba3c6
TT
14452 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14453 {
14454 /* Ranged breakpoints have only one start location and one end
14455 location. */
14456 b->enable_state = bp_disabled;
44702360 14457 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14458 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14459 "multiple locations found\n"),
14460 b->number);
14461 return;
14462 }
f1310107 14463
4a64f543
MS
14464 /* If there's no new locations, and all existing locations are
14465 pending, don't do anything. This optimizes the common case where
14466 all locations are in the same shared library, that was unloaded.
14467 We'd like to retain the location, so that when the library is
14468 loaded again, we don't loose the enabled/disabled status of the
14469 individual locations. */
0d381245 14470 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14471 return;
14472
fe3f5fa8
VP
14473 b->loc = NULL;
14474
0d381245 14475 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14476 {
f8eba3c6
TT
14477 struct bp_location *new_loc;
14478
14479 switch_to_program_space_and_thread (sals.sals[i].pspace);
14480
14481 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14482
0d381245
VP
14483 /* Reparse conditions, they might contain references to the
14484 old symtab. */
14485 if (b->cond_string != NULL)
14486 {
bbc13ae3 14487 const char *s;
bfd189b1 14488 volatile struct gdb_exception e;
fe3f5fa8 14489
0d381245
VP
14490 s = b->cond_string;
14491 TRY_CATCH (e, RETURN_MASK_ERROR)
14492 {
1bb9788d
TT
14493 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14494 block_for_pc (sals.sals[i].pc),
0d381245
VP
14495 0);
14496 }
14497 if (e.reason < 0)
14498 {
3e43a32a
MS
14499 warning (_("failed to reevaluate condition "
14500 "for breakpoint %d: %s"),
0d381245
VP
14501 b->number, e.message);
14502 new_loc->enabled = 0;
14503 }
14504 }
fe3f5fa8 14505
f1310107
TJB
14506 if (sals_end.nelts)
14507 {
14508 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14509
14510 new_loc->length = end - sals.sals[0].pc + 1;
14511 }
0d381245 14512 }
fe3f5fa8 14513
4a64f543
MS
14514 /* If possible, carry over 'disable' status from existing
14515 breakpoints. */
0d381245
VP
14516 {
14517 struct bp_location *e = existing_locations;
776592bf
DE
14518 /* If there are multiple breakpoints with the same function name,
14519 e.g. for inline functions, comparing function names won't work.
14520 Instead compare pc addresses; this is just a heuristic as things
14521 may have moved, but in practice it gives the correct answer
14522 often enough until a better solution is found. */
14523 int have_ambiguous_names = ambiguous_names_p (b->loc);
14524
0d381245
VP
14525 for (; e; e = e->next)
14526 {
14527 if (!e->enabled && e->function_name)
14528 {
14529 struct bp_location *l = b->loc;
776592bf
DE
14530 if (have_ambiguous_names)
14531 {
14532 for (; l; l = l->next)
f1310107 14533 if (breakpoint_locations_match (e, l))
776592bf
DE
14534 {
14535 l->enabled = 0;
14536 break;
14537 }
14538 }
14539 else
14540 {
14541 for (; l; l = l->next)
14542 if (l->function_name
14543 && strcmp (e->function_name, l->function_name) == 0)
14544 {
14545 l->enabled = 0;
14546 break;
14547 }
14548 }
0d381245
VP
14549 }
14550 }
14551 }
fe3f5fa8 14552
8d3788bd
VP
14553 if (!locations_are_equal (existing_locations, b->loc))
14554 observer_notify_breakpoint_modified (b);
14555
44702360 14556 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14557}
14558
ef23e705
TJB
14559/* Find the SaL locations corresponding to the given ADDR_STRING.
14560 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14561
14562static struct symtabs_and_lines
14563addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14564{
14565 char *s;
02d20e4a 14566 struct symtabs_and_lines sals = {0};
f8eba3c6 14567 volatile struct gdb_exception e;
ef23e705 14568
983af33b 14569 gdb_assert (b->ops != NULL);
ef23e705 14570 s = addr_string;
ef23e705
TJB
14571
14572 TRY_CATCH (e, RETURN_MASK_ERROR)
14573 {
983af33b 14574 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14575 }
14576 if (e.reason < 0)
14577 {
14578 int not_found_and_ok = 0;
14579 /* For pending breakpoints, it's expected that parsing will
14580 fail until the right shared library is loaded. User has
14581 already told to create pending breakpoints and don't need
14582 extra messages. If breakpoint is in bp_shlib_disabled
14583 state, then user already saw the message about that
14584 breakpoint being disabled, and don't want to see more
14585 errors. */
58438ac1 14586 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14587 && (b->condition_not_parsed
14588 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14589 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14590 || b->enable_state == bp_disabled))
14591 not_found_and_ok = 1;
14592
14593 if (!not_found_and_ok)
14594 {
14595 /* We surely don't want to warn about the same breakpoint
14596 10 times. One solution, implemented here, is disable
14597 the breakpoint on error. Another solution would be to
14598 have separate 'warning emitted' flag. Since this
14599 happens only when a binary has changed, I don't know
14600 which approach is better. */
14601 b->enable_state = bp_disabled;
14602 throw_exception (e);
14603 }
14604 }
14605
58438ac1 14606 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14607 {
f8eba3c6 14608 int i;
ef23e705 14609
f8eba3c6
TT
14610 for (i = 0; i < sals.nelts; ++i)
14611 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14612 if (b->condition_not_parsed && s && s[0])
14613 {
ed1d1739
KS
14614 char *cond_string, *extra_string;
14615 int thread, task;
ef23e705
TJB
14616
14617 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14618 &cond_string, &thread, &task,
14619 &extra_string);
ef23e705
TJB
14620 if (cond_string)
14621 b->cond_string = cond_string;
14622 b->thread = thread;
14623 b->task = task;
e7e0cddf
SS
14624 if (extra_string)
14625 b->extra_string = extra_string;
ef23e705
TJB
14626 b->condition_not_parsed = 0;
14627 }
14628
983af33b 14629 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14630 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14631
58438ac1
TT
14632 *found = 1;
14633 }
14634 else
14635 *found = 0;
ef23e705
TJB
14636
14637 return sals;
14638}
14639
348d480f
PA
14640/* The default re_set method, for typical hardware or software
14641 breakpoints. Reevaluate the breakpoint and recreate its
14642 locations. */
14643
14644static void
28010a5d 14645breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14646{
14647 int found;
f1310107 14648 struct symtabs_and_lines sals, sals_end;
ef23e705 14649 struct symtabs_and_lines expanded = {0};
f1310107 14650 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14651
14652 sals = addr_string_to_sals (b, b->addr_string, &found);
14653 if (found)
14654 {
14655 make_cleanup (xfree, sals.sals);
f8eba3c6 14656 expanded = sals;
ef23e705
TJB
14657 }
14658
f1310107
TJB
14659 if (b->addr_string_range_end)
14660 {
14661 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14662 if (found)
14663 {
14664 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14665 expanded_end = sals_end;
f1310107
TJB
14666 }
14667 }
14668
14669 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14670}
14671
983af33b
SDJ
14672/* Default method for creating SALs from an address string. It basically
14673 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14674
14675static void
14676create_sals_from_address_default (char **arg,
14677 struct linespec_result *canonical,
14678 enum bptype type_wanted,
14679 char *addr_start, char **copy_arg)
14680{
14681 parse_breakpoint_sals (arg, canonical);
14682}
14683
14684/* Call create_breakpoints_sal for the given arguments. This is the default
14685 function for the `create_breakpoints_sal' method of
14686 breakpoint_ops. */
14687
14688static void
14689create_breakpoints_sal_default (struct gdbarch *gdbarch,
14690 struct linespec_result *canonical,
983af33b 14691 char *cond_string,
e7e0cddf 14692 char *extra_string,
983af33b
SDJ
14693 enum bptype type_wanted,
14694 enum bpdisp disposition,
14695 int thread,
14696 int task, int ignore_count,
14697 const struct breakpoint_ops *ops,
14698 int from_tty, int enabled,
44f238bb 14699 int internal, unsigned flags)
983af33b
SDJ
14700{
14701 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14702 extra_string,
983af33b
SDJ
14703 type_wanted, disposition,
14704 thread, task, ignore_count, ops, from_tty,
44f238bb 14705 enabled, internal, flags);
983af33b
SDJ
14706}
14707
14708/* Decode the line represented by S by calling decode_line_full. This is the
14709 default function for the `decode_linespec' method of breakpoint_ops. */
14710
14711static void
14712decode_linespec_default (struct breakpoint *b, char **s,
14713 struct symtabs_and_lines *sals)
14714{
14715 struct linespec_result canonical;
14716
14717 init_linespec_result (&canonical);
14718 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14719 (struct symtab *) NULL, 0,
14720 &canonical, multiple_symbols_all,
14721 b->filter);
14722
14723 /* We should get 0 or 1 resulting SALs. */
14724 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14725
14726 if (VEC_length (linespec_sals, canonical.sals) > 0)
14727 {
14728 struct linespec_sals *lsal;
14729
14730 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14731 *sals = lsal->sals;
14732 /* Arrange it so the destructor does not free the
14733 contents. */
14734 lsal->sals.sals = NULL;
14735 }
14736
14737 destroy_linespec_result (&canonical);
14738}
14739
28010a5d
PA
14740/* Prepare the global context for a re-set of breakpoint B. */
14741
14742static struct cleanup *
14743prepare_re_set_context (struct breakpoint *b)
14744{
14745 struct cleanup *cleanups;
14746
14747 input_radix = b->input_radix;
14748 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14749 if (b->pspace != NULL)
14750 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14751 set_language (b->language);
14752
14753 return cleanups;
ef23e705
TJB
14754}
14755
c906108c
SS
14756/* Reset a breakpoint given it's struct breakpoint * BINT.
14757 The value we return ends up being the return value from catch_errors.
14758 Unused in this case. */
14759
14760static int
4efb68b1 14761breakpoint_re_set_one (void *bint)
c906108c 14762{
4a64f543 14763 /* Get past catch_errs. */
53a5351d 14764 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14765 struct cleanup *cleanups;
c906108c 14766
348d480f
PA
14767 cleanups = prepare_re_set_context (b);
14768 b->ops->re_set (b);
14769 do_cleanups (cleanups);
c906108c
SS
14770 return 0;
14771}
14772
69de3c6a 14773/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14774void
69de3c6a 14775breakpoint_re_set (void)
c906108c 14776{
35df4500 14777 struct breakpoint *b, *b_tmp;
c906108c
SS
14778 enum language save_language;
14779 int save_input_radix;
6c95b8df 14780 struct cleanup *old_chain;
c5aa993b 14781
c906108c
SS
14782 save_language = current_language->la_language;
14783 save_input_radix = input_radix;
6c95b8df
PA
14784 old_chain = save_current_program_space ();
14785
35df4500 14786 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14787 {
4a64f543 14788 /* Format possible error msg. */
fe3f5fa8 14789 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14790 b->number);
14791 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14792 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14793 do_cleanups (cleanups);
c5aa993b 14794 }
c906108c
SS
14795 set_language (save_language);
14796 input_radix = save_input_radix;
e62c965a 14797
0756c555 14798 jit_breakpoint_re_set ();
4efc6507 14799
6c95b8df
PA
14800 do_cleanups (old_chain);
14801
af02033e
PP
14802 create_overlay_event_breakpoint ();
14803 create_longjmp_master_breakpoint ();
14804 create_std_terminate_master_breakpoint ();
186c406b 14805 create_exception_master_breakpoint ();
c906108c
SS
14806}
14807\f
c906108c
SS
14808/* Reset the thread number of this breakpoint:
14809
14810 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14811 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14812void
fba45db2 14813breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14814{
14815 if (b->thread != -1)
14816 {
39f77062
KB
14817 if (in_thread_list (inferior_ptid))
14818 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14819
14820 /* We're being called after following a fork. The new fork is
14821 selected as current, and unless this was a vfork will have a
14822 different program space from the original thread. Reset that
14823 as well. */
14824 b->loc->pspace = current_program_space;
c906108c
SS
14825 }
14826}
14827
03ac34d5
MS
14828/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14829 If from_tty is nonzero, it prints a message to that effect,
14830 which ends with a period (no newline). */
14831
c906108c 14832void
fba45db2 14833set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14834{
52f0bd74 14835 struct breakpoint *b;
c906108c
SS
14836
14837 if (count < 0)
14838 count = 0;
14839
14840 ALL_BREAKPOINTS (b)
14841 if (b->number == bptnum)
c5aa993b 14842 {
d77f58be
SS
14843 if (is_tracepoint (b))
14844 {
14845 if (from_tty && count != 0)
14846 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14847 bptnum);
14848 return;
14849 }
14850
c5aa993b 14851 b->ignore_count = count;
221ea385
KS
14852 if (from_tty)
14853 {
14854 if (count == 0)
3e43a32a
MS
14855 printf_filtered (_("Will stop next time "
14856 "breakpoint %d is reached."),
221ea385
KS
14857 bptnum);
14858 else if (count == 1)
a3f17187 14859 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14860 bptnum);
14861 else
3e43a32a
MS
14862 printf_filtered (_("Will ignore next %d "
14863 "crossings of breakpoint %d."),
221ea385
KS
14864 count, bptnum);
14865 }
8d3788bd 14866 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14867 return;
14868 }
c906108c 14869
8a3fe4f8 14870 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14871}
14872
c906108c
SS
14873/* Command to set ignore-count of breakpoint N to COUNT. */
14874
14875static void
fba45db2 14876ignore_command (char *args, int from_tty)
c906108c
SS
14877{
14878 char *p = args;
52f0bd74 14879 int num;
c906108c
SS
14880
14881 if (p == 0)
e2e0b3e5 14882 error_no_arg (_("a breakpoint number"));
c5aa993b 14883
c906108c 14884 num = get_number (&p);
5c44784c 14885 if (num == 0)
8a3fe4f8 14886 error (_("bad breakpoint number: '%s'"), args);
c906108c 14887 if (*p == 0)
8a3fe4f8 14888 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14889
14890 set_ignore_count (num,
14891 longest_to_int (value_as_long (parse_and_eval (p))),
14892 from_tty);
221ea385
KS
14893 if (from_tty)
14894 printf_filtered ("\n");
c906108c
SS
14895}
14896\f
14897/* Call FUNCTION on each of the breakpoints
14898 whose numbers are given in ARGS. */
14899
14900static void
95a42b64
TT
14901map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14902 void *),
14903 void *data)
c906108c 14904{
52f0bd74
AC
14905 int num;
14906 struct breakpoint *b, *tmp;
11cf8741 14907 int match;
197f0a60 14908 struct get_number_or_range_state state;
c906108c 14909
b9d61307 14910 if (args == 0 || *args == '\0')
e2e0b3e5 14911 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14912
197f0a60
TT
14913 init_number_or_range (&state, args);
14914
14915 while (!state.finished)
c906108c 14916 {
e799154c 14917 const char *p = state.string;
197f0a60 14918
11cf8741 14919 match = 0;
c5aa993b 14920
197f0a60 14921 num = get_number_or_range (&state);
5c44784c 14922 if (num == 0)
c5aa993b 14923 {
8a3fe4f8 14924 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14925 }
14926 else
14927 {
14928 ALL_BREAKPOINTS_SAFE (b, tmp)
14929 if (b->number == num)
14930 {
11cf8741 14931 match = 1;
cdac0397 14932 function (b, data);
11cf8741 14933 break;
5c44784c 14934 }
11cf8741 14935 if (match == 0)
a3f17187 14936 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14937 }
c906108c
SS
14938 }
14939}
14940
0d381245
VP
14941static struct bp_location *
14942find_location_by_number (char *number)
14943{
14944 char *dot = strchr (number, '.');
14945 char *p1;
14946 int bp_num;
14947 int loc_num;
14948 struct breakpoint *b;
14949 struct bp_location *loc;
14950
14951 *dot = '\0';
14952
14953 p1 = number;
197f0a60 14954 bp_num = get_number (&p1);
0d381245
VP
14955 if (bp_num == 0)
14956 error (_("Bad breakpoint number '%s'"), number);
14957
14958 ALL_BREAKPOINTS (b)
14959 if (b->number == bp_num)
14960 {
14961 break;
14962 }
14963
14964 if (!b || b->number != bp_num)
14965 error (_("Bad breakpoint number '%s'"), number);
14966
14967 p1 = dot+1;
197f0a60 14968 loc_num = get_number (&p1);
0d381245
VP
14969 if (loc_num == 0)
14970 error (_("Bad breakpoint location number '%s'"), number);
14971
14972 --loc_num;
14973 loc = b->loc;
14974 for (;loc_num && loc; --loc_num, loc = loc->next)
14975 ;
14976 if (!loc)
14977 error (_("Bad breakpoint location number '%s'"), dot+1);
14978
14979 return loc;
14980}
14981
14982
1900040c
MS
14983/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14984 If from_tty is nonzero, it prints a message to that effect,
14985 which ends with a period (no newline). */
14986
c906108c 14987void
fba45db2 14988disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14989{
14990 /* Never disable a watchpoint scope breakpoint; we want to
14991 hit them when we leave scope so we can delete both the
14992 watchpoint and its scope breakpoint at that time. */
14993 if (bpt->type == bp_watchpoint_scope)
14994 return;
14995
b5de0fa7 14996 bpt->enable_state = bp_disabled;
c906108c 14997
b775012e
LM
14998 /* Mark breakpoint locations modified. */
14999 mark_breakpoint_modified (bpt);
15000
d248b706
KY
15001 if (target_supports_enable_disable_tracepoint ()
15002 && current_trace_status ()->running && is_tracepoint (bpt))
15003 {
15004 struct bp_location *location;
15005
15006 for (location = bpt->loc; location; location = location->next)
15007 target_disable_tracepoint (location);
15008 }
15009
44702360 15010 update_global_location_list (UGLL_DONT_INSERT);
c906108c 15011
8d3788bd 15012 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15013}
15014
51be5b68
PA
15015/* A callback for iterate_over_related_breakpoints. */
15016
15017static void
15018do_disable_breakpoint (struct breakpoint *b, void *ignore)
15019{
15020 disable_breakpoint (b);
15021}
15022
95a42b64
TT
15023/* A callback for map_breakpoint_numbers that calls
15024 disable_breakpoint. */
15025
15026static void
15027do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
15028{
51be5b68 15029 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
15030}
15031
c906108c 15032static void
fba45db2 15033disable_command (char *args, int from_tty)
c906108c 15034{
c906108c 15035 if (args == 0)
46c6471b
PA
15036 {
15037 struct breakpoint *bpt;
15038
15039 ALL_BREAKPOINTS (bpt)
15040 if (user_breakpoint_p (bpt))
15041 disable_breakpoint (bpt);
15042 }
9eaabc75 15043 else
0d381245 15044 {
9eaabc75
MW
15045 char *num = extract_arg (&args);
15046
15047 while (num)
d248b706 15048 {
9eaabc75 15049 if (strchr (num, '.'))
b775012e 15050 {
9eaabc75
MW
15051 struct bp_location *loc = find_location_by_number (num);
15052
15053 if (loc)
15054 {
15055 if (loc->enabled)
15056 {
15057 loc->enabled = 0;
15058 mark_breakpoint_location_modified (loc);
15059 }
15060 if (target_supports_enable_disable_tracepoint ()
15061 && current_trace_status ()->running && loc->owner
15062 && is_tracepoint (loc->owner))
15063 target_disable_tracepoint (loc);
15064 }
44702360 15065 update_global_location_list (UGLL_DONT_INSERT);
b775012e 15066 }
9eaabc75
MW
15067 else
15068 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
15069 num = extract_arg (&args);
d248b706 15070 }
0d381245 15071 }
c906108c
SS
15072}
15073
15074static void
816338b5
SS
15075enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
15076 int count)
c906108c 15077{
afe38095 15078 int target_resources_ok;
c906108c
SS
15079
15080 if (bpt->type == bp_hardware_breakpoint)
15081 {
15082 int i;
c5aa993b 15083 i = hw_breakpoint_used_count ();
53a5351d 15084 target_resources_ok =
d92524f1 15085 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 15086 i + 1, 0);
c906108c 15087 if (target_resources_ok == 0)
8a3fe4f8 15088 error (_("No hardware breakpoint support in the target."));
c906108c 15089 else if (target_resources_ok < 0)
8a3fe4f8 15090 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
15091 }
15092
cc60f2e3 15093 if (is_watchpoint (bpt))
c906108c 15094 {
d07205c2
JK
15095 /* Initialize it just to avoid a GCC false warning. */
15096 enum enable_state orig_enable_state = 0;
bfd189b1 15097 volatile struct gdb_exception e;
dde02812
ES
15098
15099 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 15100 {
3a5c3e22
PA
15101 struct watchpoint *w = (struct watchpoint *) bpt;
15102
1e718ff1
TJB
15103 orig_enable_state = bpt->enable_state;
15104 bpt->enable_state = bp_enabled;
3a5c3e22 15105 update_watchpoint (w, 1 /* reparse */);
c906108c 15106 }
dde02812 15107 if (e.reason < 0)
c5aa993b 15108 {
1e718ff1 15109 bpt->enable_state = orig_enable_state;
dde02812
ES
15110 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
15111 bpt->number);
15112 return;
c5aa993b 15113 }
c906108c 15114 }
0101ce28 15115
b775012e
LM
15116 bpt->enable_state = bp_enabled;
15117
15118 /* Mark breakpoint locations modified. */
15119 mark_breakpoint_modified (bpt);
15120
d248b706
KY
15121 if (target_supports_enable_disable_tracepoint ()
15122 && current_trace_status ()->running && is_tracepoint (bpt))
15123 {
15124 struct bp_location *location;
15125
15126 for (location = bpt->loc; location; location = location->next)
15127 target_enable_tracepoint (location);
15128 }
15129
b4c291bb 15130 bpt->disposition = disposition;
816338b5 15131 bpt->enable_count = count;
44702360 15132 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 15133
8d3788bd 15134 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15135}
15136
fe3f5fa8 15137
c906108c 15138void
fba45db2 15139enable_breakpoint (struct breakpoint *bpt)
c906108c 15140{
816338b5 15141 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
15142}
15143
15144static void
15145do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15146{
15147 enable_breakpoint (bpt);
c906108c
SS
15148}
15149
95a42b64
TT
15150/* A callback for map_breakpoint_numbers that calls
15151 enable_breakpoint. */
15152
15153static void
15154do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15155{
51be5b68 15156 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15157}
15158
c906108c
SS
15159/* The enable command enables the specified breakpoints (or all defined
15160 breakpoints) so they once again become (or continue to be) effective
1272ad14 15161 in stopping the inferior. */
c906108c 15162
c906108c 15163static void
fba45db2 15164enable_command (char *args, int from_tty)
c906108c 15165{
c906108c 15166 if (args == 0)
46c6471b
PA
15167 {
15168 struct breakpoint *bpt;
15169
15170 ALL_BREAKPOINTS (bpt)
15171 if (user_breakpoint_p (bpt))
15172 enable_breakpoint (bpt);
15173 }
9eaabc75 15174 else
0d381245 15175 {
9eaabc75
MW
15176 char *num = extract_arg (&args);
15177
15178 while (num)
d248b706 15179 {
9eaabc75 15180 if (strchr (num, '.'))
b775012e 15181 {
9eaabc75
MW
15182 struct bp_location *loc = find_location_by_number (num);
15183
15184 if (loc)
15185 {
15186 if (!loc->enabled)
15187 {
15188 loc->enabled = 1;
15189 mark_breakpoint_location_modified (loc);
15190 }
15191 if (target_supports_enable_disable_tracepoint ()
15192 && current_trace_status ()->running && loc->owner
15193 && is_tracepoint (loc->owner))
15194 target_enable_tracepoint (loc);
15195 }
44702360 15196 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15197 }
9eaabc75
MW
15198 else
15199 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15200 num = extract_arg (&args);
d248b706 15201 }
0d381245 15202 }
c906108c
SS
15203}
15204
816338b5
SS
15205/* This struct packages up disposition data for application to multiple
15206 breakpoints. */
15207
15208struct disp_data
15209{
15210 enum bpdisp disp;
15211 int count;
15212};
15213
c906108c 15214static void
51be5b68
PA
15215do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15216{
816338b5 15217 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15218
816338b5 15219 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15220}
15221
15222static void
15223do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15224{
816338b5 15225 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15226
15227 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15228}
15229
c906108c 15230static void
fba45db2 15231enable_once_command (char *args, int from_tty)
c906108c 15232{
51be5b68 15233 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15234}
15235
816338b5
SS
15236static void
15237do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15238{
15239 struct disp_data disp = { disp_disable, *(int *) countptr };
15240
15241 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15242}
15243
15244static void
15245enable_count_command (char *args, int from_tty)
15246{
b9d61307
SM
15247 int count;
15248
15249 if (args == NULL)
15250 error_no_arg (_("hit count"));
15251
15252 count = get_number (&args);
816338b5
SS
15253
15254 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15255}
15256
c906108c 15257static void
51be5b68 15258do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15259{
816338b5 15260 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15261
15262 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15263}
15264
c906108c 15265static void
fba45db2 15266enable_delete_command (char *args, int from_tty)
c906108c 15267{
51be5b68 15268 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15269}
15270\f
fa8d40ab
JJ
15271static void
15272set_breakpoint_cmd (char *args, int from_tty)
15273{
15274}
15275
15276static void
15277show_breakpoint_cmd (char *args, int from_tty)
15278{
15279}
15280
1f3b5d1b
PP
15281/* Invalidate last known value of any hardware watchpoint if
15282 the memory which that value represents has been written to by
15283 GDB itself. */
15284
15285static void
8de0566d
YQ
15286invalidate_bp_value_on_memory_change (struct inferior *inferior,
15287 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15288 const bfd_byte *data)
15289{
15290 struct breakpoint *bp;
15291
15292 ALL_BREAKPOINTS (bp)
15293 if (bp->enable_state == bp_enabled
3a5c3e22 15294 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15295 {
3a5c3e22 15296 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15297
3a5c3e22
PA
15298 if (wp->val_valid && wp->val)
15299 {
15300 struct bp_location *loc;
15301
15302 for (loc = bp->loc; loc != NULL; loc = loc->next)
15303 if (loc->loc_type == bp_loc_hardware_watchpoint
15304 && loc->address + loc->length > addr
15305 && addr + len > loc->address)
15306 {
15307 value_free (wp->val);
15308 wp->val = NULL;
15309 wp->val_valid = 0;
15310 }
15311 }
1f3b5d1b
PP
15312 }
15313}
15314
8181d85f
DJ
15315/* Create and insert a breakpoint for software single step. */
15316
15317void
6c95b8df 15318insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15319 struct address_space *aspace,
15320 CORE_ADDR next_pc)
8181d85f 15321{
7c16b83e
PA
15322 struct thread_info *tp = inferior_thread ();
15323 struct symtab_and_line sal;
15324 CORE_ADDR pc = next_pc;
8181d85f 15325
34b7e8a6
PA
15326 if (tp->control.single_step_breakpoints == NULL)
15327 {
15328 tp->control.single_step_breakpoints
15329 = new_single_step_breakpoint (tp->num, gdbarch);
15330 }
8181d85f 15331
7c16b83e
PA
15332 sal = find_pc_line (pc, 0);
15333 sal.pc = pc;
15334 sal.section = find_pc_overlay (pc);
15335 sal.explicit_pc = 1;
34b7e8a6 15336 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15337
7c16b83e 15338 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15339}
15340
34b7e8a6 15341/* See breakpoint.h. */
f02253f1
HZ
15342
15343int
7c16b83e
PA
15344breakpoint_has_location_inserted_here (struct breakpoint *bp,
15345 struct address_space *aspace,
15346 CORE_ADDR pc)
1aafd4da 15347{
7c16b83e 15348 struct bp_location *loc;
1aafd4da 15349
7c16b83e
PA
15350 for (loc = bp->loc; loc != NULL; loc = loc->next)
15351 if (loc->inserted
15352 && breakpoint_location_address_match (loc, aspace, pc))
15353 return 1;
1aafd4da 15354
7c16b83e 15355 return 0;
ef370185
JB
15356}
15357
15358/* Check whether a software single-step breakpoint is inserted at
15359 PC. */
15360
15361int
15362single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15363 CORE_ADDR pc)
15364{
34b7e8a6
PA
15365 struct breakpoint *bpt;
15366
15367 ALL_BREAKPOINTS (bpt)
15368 {
15369 if (bpt->type == bp_single_step
15370 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15371 return 1;
15372 }
15373 return 0;
1aafd4da
UW
15374}
15375
a96d9b2e
SDJ
15376/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15377 non-zero otherwise. */
15378static int
15379is_syscall_catchpoint_enabled (struct breakpoint *bp)
15380{
15381 if (syscall_catchpoint_p (bp)
15382 && bp->enable_state != bp_disabled
15383 && bp->enable_state != bp_call_disabled)
15384 return 1;
15385 else
15386 return 0;
15387}
15388
15389int
15390catch_syscall_enabled (void)
15391{
fa3064dd
YQ
15392 struct catch_syscall_inferior_data *inf_data
15393 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15394
fa3064dd 15395 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15396}
15397
15398int
15399catching_syscall_number (int syscall_number)
15400{
15401 struct breakpoint *bp;
15402
15403 ALL_BREAKPOINTS (bp)
15404 if (is_syscall_catchpoint_enabled (bp))
15405 {
be5c67c1
PA
15406 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15407
15408 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15409 {
15410 int i, iter;
15411 for (i = 0;
be5c67c1 15412 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15413 i++)
15414 if (syscall_number == iter)
15415 return 1;
15416 }
15417 else
15418 return 1;
15419 }
15420
15421 return 0;
15422}
15423
15424/* Complete syscall names. Used by "catch syscall". */
49c4e619 15425static VEC (char_ptr) *
a96d9b2e 15426catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15427 const char *text, const char *word)
a96d9b2e 15428{
458c8db8 15429 const char **list = get_syscall_names (get_current_arch ());
49c4e619 15430 VEC (char_ptr) *retlist
b45627a0 15431 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15432
c38eea1a
MS
15433 xfree (list);
15434 return retlist;
a96d9b2e
SDJ
15435}
15436
1042e4c0
SS
15437/* Tracepoint-specific operations. */
15438
15439/* Set tracepoint count to NUM. */
15440static void
15441set_tracepoint_count (int num)
15442{
15443 tracepoint_count = num;
4fa62494 15444 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15445}
15446
70221824 15447static void
1042e4c0
SS
15448trace_command (char *arg, int from_tty)
15449{
55aa24fb
SDJ
15450 struct breakpoint_ops *ops;
15451 const char *arg_cp = arg;
15452
15453 if (arg && probe_linespec_to_ops (&arg_cp))
15454 ops = &tracepoint_probe_breakpoint_ops;
15455 else
15456 ops = &tracepoint_breakpoint_ops;
15457
558a9d82
YQ
15458 create_breakpoint (get_current_arch (),
15459 arg,
15460 NULL, 0, NULL, 1 /* parse arg */,
15461 0 /* tempflag */,
15462 bp_tracepoint /* type_wanted */,
15463 0 /* Ignore count */,
15464 pending_break_support,
15465 ops,
15466 from_tty,
15467 1 /* enabled */,
15468 0 /* internal */, 0);
1042e4c0
SS
15469}
15470
70221824 15471static void
7a697b8d
SS
15472ftrace_command (char *arg, int from_tty)
15473{
558a9d82
YQ
15474 create_breakpoint (get_current_arch (),
15475 arg,
15476 NULL, 0, NULL, 1 /* parse arg */,
15477 0 /* tempflag */,
15478 bp_fast_tracepoint /* type_wanted */,
15479 0 /* Ignore count */,
15480 pending_break_support,
15481 &tracepoint_breakpoint_ops,
15482 from_tty,
15483 1 /* enabled */,
15484 0 /* internal */, 0);
0fb4aa4b
PA
15485}
15486
15487/* strace command implementation. Creates a static tracepoint. */
15488
70221824 15489static void
0fb4aa4b
PA
15490strace_command (char *arg, int from_tty)
15491{
983af33b
SDJ
15492 struct breakpoint_ops *ops;
15493
15494 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15495 or with a normal static tracepoint. */
15496 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15497 ops = &strace_marker_breakpoint_ops;
15498 else
15499 ops = &tracepoint_breakpoint_ops;
15500
558a9d82
YQ
15501 create_breakpoint (get_current_arch (),
15502 arg,
15503 NULL, 0, NULL, 1 /* parse arg */,
15504 0 /* tempflag */,
15505 bp_static_tracepoint /* type_wanted */,
15506 0 /* Ignore count */,
15507 pending_break_support,
15508 ops,
15509 from_tty,
15510 1 /* enabled */,
15511 0 /* internal */, 0);
7a697b8d
SS
15512}
15513
409873ef
SS
15514/* Set up a fake reader function that gets command lines from a linked
15515 list that was acquired during tracepoint uploading. */
15516
15517static struct uploaded_tp *this_utp;
3149d8c1 15518static int next_cmd;
409873ef
SS
15519
15520static char *
15521read_uploaded_action (void)
15522{
15523 char *rslt;
15524
3149d8c1 15525 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15526
3149d8c1 15527 next_cmd++;
409873ef
SS
15528
15529 return rslt;
15530}
15531
00bf0b85
SS
15532/* Given information about a tracepoint as recorded on a target (which
15533 can be either a live system or a trace file), attempt to create an
15534 equivalent GDB tracepoint. This is not a reliable process, since
15535 the target does not necessarily have all the information used when
15536 the tracepoint was originally defined. */
15537
d9b3f62e 15538struct tracepoint *
00bf0b85 15539create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15540{
409873ef 15541 char *addr_str, small_buf[100];
d9b3f62e 15542 struct tracepoint *tp;
fd9b8c24 15543
409873ef
SS
15544 if (utp->at_string)
15545 addr_str = utp->at_string;
15546 else
15547 {
15548 /* In the absence of a source location, fall back to raw
15549 address. Since there is no way to confirm that the address
15550 means the same thing as when the trace was started, warn the
15551 user. */
3e43a32a
MS
15552 warning (_("Uploaded tracepoint %d has no "
15553 "source location, using raw address"),
409873ef 15554 utp->number);
8c042590 15555 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15556 addr_str = small_buf;
15557 }
15558
15559 /* There's not much we can do with a sequence of bytecodes. */
15560 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15561 warning (_("Uploaded tracepoint %d condition "
15562 "has no source form, ignoring it"),
409873ef 15563 utp->number);
d5551862 15564
8cdf0e15 15565 if (!create_breakpoint (get_current_arch (),
409873ef 15566 addr_str,
e7e0cddf
SS
15567 utp->cond_string, -1, NULL,
15568 0 /* parse cond/thread */,
8cdf0e15 15569 0 /* tempflag */,
0fb4aa4b 15570 utp->type /* type_wanted */,
8cdf0e15
VP
15571 0 /* Ignore count */,
15572 pending_break_support,
348d480f 15573 &tracepoint_breakpoint_ops,
8cdf0e15 15574 0 /* from_tty */,
84f4c1fe 15575 utp->enabled /* enabled */,
44f238bb
PA
15576 0 /* internal */,
15577 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15578 return NULL;
15579
409873ef 15580 /* Get the tracepoint we just created. */
fd9b8c24
PA
15581 tp = get_tracepoint (tracepoint_count);
15582 gdb_assert (tp != NULL);
d5551862 15583
00bf0b85
SS
15584 if (utp->pass > 0)
15585 {
8c042590
PM
15586 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15587 tp->base.number);
00bf0b85 15588
409873ef 15589 trace_pass_command (small_buf, 0);
00bf0b85
SS
15590 }
15591
409873ef
SS
15592 /* If we have uploaded versions of the original commands, set up a
15593 special-purpose "reader" function and call the usual command line
15594 reader, then pass the result to the breakpoint command-setting
15595 function. */
3149d8c1 15596 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15597 {
409873ef 15598 struct command_line *cmd_list;
00bf0b85 15599
409873ef 15600 this_utp = utp;
3149d8c1 15601 next_cmd = 0;
d5551862 15602
409873ef
SS
15603 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15604
d9b3f62e 15605 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15606 }
3149d8c1
SS
15607 else if (!VEC_empty (char_ptr, utp->actions)
15608 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15609 warning (_("Uploaded tracepoint %d actions "
15610 "have no source form, ignoring them"),
409873ef 15611 utp->number);
00bf0b85 15612
f196051f
SS
15613 /* Copy any status information that might be available. */
15614 tp->base.hit_count = utp->hit_count;
15615 tp->traceframe_usage = utp->traceframe_usage;
15616
00bf0b85 15617 return tp;
d9b3f62e 15618}
00bf0b85 15619
1042e4c0
SS
15620/* Print information on tracepoint number TPNUM_EXP, or all if
15621 omitted. */
15622
15623static void
e5a67952 15624tracepoints_info (char *args, int from_tty)
1042e4c0 15625{
79a45e25 15626 struct ui_out *uiout = current_uiout;
e5a67952 15627 int num_printed;
1042e4c0 15628
e5a67952 15629 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15630
15631 if (num_printed == 0)
1042e4c0 15632 {
e5a67952 15633 if (args == NULL || *args == '\0')
d77f58be
SS
15634 ui_out_message (uiout, 0, "No tracepoints.\n");
15635 else
e5a67952 15636 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15637 }
ad443146
SS
15638
15639 default_collect_info ();
1042e4c0
SS
15640}
15641
4a64f543 15642/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15643 Not supported by all targets. */
15644static void
15645enable_trace_command (char *args, int from_tty)
15646{
15647 enable_command (args, from_tty);
15648}
15649
4a64f543 15650/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15651 Not supported by all targets. */
15652static void
15653disable_trace_command (char *args, int from_tty)
15654{
15655 disable_command (args, from_tty);
15656}
15657
4a64f543 15658/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15659static void
15660delete_trace_command (char *arg, int from_tty)
15661{
35df4500 15662 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15663
15664 dont_repeat ();
15665
15666 if (arg == 0)
15667 {
15668 int breaks_to_delete = 0;
15669
15670 /* Delete all breakpoints if no argument.
15671 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15672 have to be deleted with an explicit breakpoint number
15673 argument. */
1042e4c0 15674 ALL_TRACEPOINTS (b)
46c6471b 15675 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15676 {
15677 breaks_to_delete = 1;
15678 break;
15679 }
1042e4c0
SS
15680
15681 /* Ask user only if there are some breakpoints to delete. */
15682 if (!from_tty
15683 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15684 {
35df4500 15685 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15686 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15687 delete_breakpoint (b);
1042e4c0
SS
15688 }
15689 }
15690 else
51be5b68 15691 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15692}
15693
197f0a60
TT
15694/* Helper function for trace_pass_command. */
15695
15696static void
d9b3f62e 15697trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15698{
d9b3f62e 15699 tp->pass_count = count;
6f6484cd 15700 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15701 if (from_tty)
15702 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15703 tp->base.number, count);
197f0a60
TT
15704}
15705
1042e4c0
SS
15706/* Set passcount for tracepoint.
15707
15708 First command argument is passcount, second is tracepoint number.
15709 If tracepoint number omitted, apply to most recently defined.
15710 Also accepts special argument "all". */
15711
15712static void
15713trace_pass_command (char *args, int from_tty)
15714{
d9b3f62e 15715 struct tracepoint *t1;
1042e4c0 15716 unsigned int count;
1042e4c0
SS
15717
15718 if (args == 0 || *args == 0)
3e43a32a
MS
15719 error (_("passcount command requires an "
15720 "argument (count + optional TP num)"));
1042e4c0 15721
4a64f543 15722 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15723
529480d0 15724 args = skip_spaces (args);
1042e4c0
SS
15725 if (*args && strncasecmp (args, "all", 3) == 0)
15726 {
d9b3f62e
PA
15727 struct breakpoint *b;
15728
1042e4c0 15729 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15730 if (*args)
15731 error (_("Junk at end of arguments."));
1042e4c0 15732
d9b3f62e 15733 ALL_TRACEPOINTS (b)
197f0a60 15734 {
d9b3f62e 15735 t1 = (struct tracepoint *) b;
197f0a60
TT
15736 trace_pass_set_count (t1, count, from_tty);
15737 }
15738 }
15739 else if (*args == '\0')
1042e4c0 15740 {
5fa1d40e 15741 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15742 if (t1)
197f0a60
TT
15743 trace_pass_set_count (t1, count, from_tty);
15744 }
15745 else
15746 {
15747 struct get_number_or_range_state state;
15748
15749 init_number_or_range (&state, args);
15750 while (!state.finished)
1042e4c0 15751 {
5fa1d40e 15752 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15753 if (t1)
15754 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15755 }
15756 }
1042e4c0
SS
15757}
15758
d9b3f62e 15759struct tracepoint *
1042e4c0
SS
15760get_tracepoint (int num)
15761{
15762 struct breakpoint *t;
15763
15764 ALL_TRACEPOINTS (t)
15765 if (t->number == num)
d9b3f62e 15766 return (struct tracepoint *) t;
1042e4c0
SS
15767
15768 return NULL;
15769}
15770
d5551862
SS
15771/* Find the tracepoint with the given target-side number (which may be
15772 different from the tracepoint number after disconnecting and
15773 reconnecting). */
15774
d9b3f62e 15775struct tracepoint *
d5551862
SS
15776get_tracepoint_by_number_on_target (int num)
15777{
d9b3f62e 15778 struct breakpoint *b;
d5551862 15779
d9b3f62e
PA
15780 ALL_TRACEPOINTS (b)
15781 {
15782 struct tracepoint *t = (struct tracepoint *) b;
15783
15784 if (t->number_on_target == num)
15785 return t;
15786 }
d5551862
SS
15787
15788 return NULL;
15789}
15790
1042e4c0 15791/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15792 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15793 If the argument is missing, the most recent tracepoint
15794 (tracepoint_count) is returned. */
15795
d9b3f62e 15796struct tracepoint *
197f0a60 15797get_tracepoint_by_number (char **arg,
5fa1d40e 15798 struct get_number_or_range_state *state)
1042e4c0 15799{
1042e4c0
SS
15800 struct breakpoint *t;
15801 int tpnum;
15802 char *instring = arg == NULL ? NULL : *arg;
15803
197f0a60
TT
15804 if (state)
15805 {
15806 gdb_assert (!state->finished);
15807 tpnum = get_number_or_range (state);
15808 }
15809 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15810 tpnum = tracepoint_count;
1042e4c0 15811 else
197f0a60 15812 tpnum = get_number (arg);
1042e4c0
SS
15813
15814 if (tpnum <= 0)
15815 {
15816 if (instring && *instring)
15817 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15818 instring);
15819 else
5fa1d40e 15820 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15821 return NULL;
15822 }
15823
15824 ALL_TRACEPOINTS (t)
15825 if (t->number == tpnum)
15826 {
d9b3f62e 15827 return (struct tracepoint *) t;
1042e4c0
SS
15828 }
15829
1042e4c0
SS
15830 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15831 return NULL;
15832}
15833
d9b3f62e
PA
15834void
15835print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15836{
15837 if (b->thread != -1)
15838 fprintf_unfiltered (fp, " thread %d", b->thread);
15839
15840 if (b->task != 0)
15841 fprintf_unfiltered (fp, " task %d", b->task);
15842
15843 fprintf_unfiltered (fp, "\n");
15844}
15845
6149aea9
PA
15846/* Save information on user settable breakpoints (watchpoints, etc) to
15847 a new script file named FILENAME. If FILTER is non-NULL, call it
15848 on each breakpoint and only include the ones for which it returns
15849 non-zero. */
15850
1042e4c0 15851static void
6149aea9
PA
15852save_breakpoints (char *filename, int from_tty,
15853 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15854{
15855 struct breakpoint *tp;
6149aea9 15856 int any = 0;
1042e4c0 15857 struct cleanup *cleanup;
a7bdde9e 15858 struct ui_file *fp;
6149aea9 15859 int extra_trace_bits = 0;
1042e4c0 15860
6149aea9
PA
15861 if (filename == 0 || *filename == 0)
15862 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15863
15864 /* See if we have anything to save. */
6149aea9 15865 ALL_BREAKPOINTS (tp)
1042e4c0 15866 {
6149aea9 15867 /* Skip internal and momentary breakpoints. */
09d682a4 15868 if (!user_breakpoint_p (tp))
6149aea9
PA
15869 continue;
15870
15871 /* If we have a filter, only save the breakpoints it accepts. */
15872 if (filter && !filter (tp))
15873 continue;
15874
15875 any = 1;
15876
15877 if (is_tracepoint (tp))
15878 {
15879 extra_trace_bits = 1;
15880
15881 /* We can stop searching. */
15882 break;
15883 }
1042e4c0 15884 }
6149aea9
PA
15885
15886 if (!any)
1042e4c0 15887 {
6149aea9 15888 warning (_("Nothing to save."));
1042e4c0
SS
15889 return;
15890 }
15891
c718be47
PA
15892 filename = tilde_expand (filename);
15893 cleanup = make_cleanup (xfree, filename);
15894 fp = gdb_fopen (filename, "w");
059fb39f 15895 if (!fp)
6149aea9
PA
15896 error (_("Unable to open file '%s' for saving (%s)"),
15897 filename, safe_strerror (errno));
a7bdde9e 15898 make_cleanup_ui_file_delete (fp);
8bf6485c 15899
6149aea9
PA
15900 if (extra_trace_bits)
15901 save_trace_state_variables (fp);
8bf6485c 15902
6149aea9 15903 ALL_BREAKPOINTS (tp)
1042e4c0 15904 {
6149aea9 15905 /* Skip internal and momentary breakpoints. */
09d682a4 15906 if (!user_breakpoint_p (tp))
6149aea9 15907 continue;
8bf6485c 15908
6149aea9
PA
15909 /* If we have a filter, only save the breakpoints it accepts. */
15910 if (filter && !filter (tp))
15911 continue;
15912
348d480f 15913 tp->ops->print_recreate (tp, fp);
1042e4c0 15914
6149aea9
PA
15915 /* Note, we can't rely on tp->number for anything, as we can't
15916 assume the recreated breakpoint numbers will match. Use $bpnum
15917 instead. */
15918
15919 if (tp->cond_string)
15920 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15921
15922 if (tp->ignore_count)
15923 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15924
2d9442cc 15925 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15926 {
a7bdde9e
VP
15927 volatile struct gdb_exception ex;
15928
6149aea9 15929 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15930
79a45e25 15931 ui_out_redirect (current_uiout, fp);
14dba4b4 15932 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15933 {
79a45e25 15934 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15935 }
79a45e25 15936 ui_out_redirect (current_uiout, NULL);
1042e4c0 15937
a7bdde9e
VP
15938 if (ex.reason < 0)
15939 throw_exception (ex);
1042e4c0 15940
a7bdde9e 15941 fprintf_unfiltered (fp, " end\n");
1042e4c0 15942 }
6149aea9
PA
15943
15944 if (tp->enable_state == bp_disabled)
99894e11 15945 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15946
15947 /* If this is a multi-location breakpoint, check if the locations
15948 should be individually disabled. Watchpoint locations are
15949 special, and not user visible. */
15950 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15951 {
15952 struct bp_location *loc;
15953 int n = 1;
15954
15955 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15956 if (!loc->enabled)
15957 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15958 }
1042e4c0 15959 }
8bf6485c 15960
6149aea9 15961 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15962 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15963
1042e4c0 15964 if (from_tty)
6149aea9 15965 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15966 do_cleanups (cleanup);
6149aea9
PA
15967}
15968
15969/* The `save breakpoints' command. */
15970
15971static void
15972save_breakpoints_command (char *args, int from_tty)
15973{
15974 save_breakpoints (args, from_tty, NULL);
15975}
15976
15977/* The `save tracepoints' command. */
15978
15979static void
15980save_tracepoints_command (char *args, int from_tty)
15981{
15982 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15983}
15984
15985/* Create a vector of all tracepoints. */
15986
15987VEC(breakpoint_p) *
eeae04df 15988all_tracepoints (void)
1042e4c0
SS
15989{
15990 VEC(breakpoint_p) *tp_vec = 0;
15991 struct breakpoint *tp;
15992
15993 ALL_TRACEPOINTS (tp)
15994 {
15995 VEC_safe_push (breakpoint_p, tp_vec, tp);
15996 }
15997
15998 return tp_vec;
15999}
16000
c906108c 16001\f
4a64f543
MS
16002/* This help string is used for the break, hbreak, tbreak and thbreak
16003 commands. It is defined as a macro to prevent duplication.
16004 COMMAND should be a string constant containing the name of the
16005 command. */
31e2b00f 16006#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
16007command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
16008PROBE_MODIFIER shall be present if the command is to be placed in a\n\
16009probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
16010guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
16011`-probe-dtrace' (for a DTrace probe).\n\
31e2b00f
AS
16012LOCATION may be a line number, function name, or \"*\" and an address.\n\
16013If a line number is specified, break at start of code for that line.\n\
16014If a function is specified, break at start of code for that function.\n\
16015If an address is specified, break at that exact address.\n\
dc10affe
PA
16016With no LOCATION, uses current execution address of the selected\n\
16017stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
16018\n\
16019THREADNUM is the number from \"info threads\".\n\
16020CONDITION is a boolean expression.\n\
16021\n\
d41c0fc8
PA
16022Multiple breakpoints at one place are permitted, and useful if their\n\
16023conditions are different.\n\
31e2b00f
AS
16024\n\
16025Do \"help breakpoints\" for info on other commands dealing with breakpoints."
16026
44feb3ce
TT
16027/* List of subcommands for "catch". */
16028static struct cmd_list_element *catch_cmdlist;
16029
16030/* List of subcommands for "tcatch". */
16031static struct cmd_list_element *tcatch_cmdlist;
16032
9ac4176b 16033void
44feb3ce 16034add_catch_command (char *name, char *docstring,
82ae6c8d 16035 cmd_sfunc_ftype *sfunc,
625e8578 16036 completer_ftype *completer,
44feb3ce
TT
16037 void *user_data_catch,
16038 void *user_data_tcatch)
16039{
16040 struct cmd_list_element *command;
16041
16042 command = add_cmd (name, class_breakpoint, NULL, docstring,
16043 &catch_cmdlist);
16044 set_cmd_sfunc (command, sfunc);
16045 set_cmd_context (command, user_data_catch);
a96d9b2e 16046 set_cmd_completer (command, completer);
44feb3ce
TT
16047
16048 command = add_cmd (name, class_breakpoint, NULL, docstring,
16049 &tcatch_cmdlist);
16050 set_cmd_sfunc (command, sfunc);
16051 set_cmd_context (command, user_data_tcatch);
a96d9b2e 16052 set_cmd_completer (command, completer);
44feb3ce
TT
16053}
16054
6c95b8df 16055static void
a79b8f6e 16056clear_syscall_counts (struct inferior *inf)
6c95b8df 16057{
fa3064dd
YQ
16058 struct catch_syscall_inferior_data *inf_data
16059 = get_catch_syscall_inferior_data (inf);
16060
16061 inf_data->total_syscalls_count = 0;
16062 inf_data->any_syscall_count = 0;
16063 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
16064}
16065
6149aea9
PA
16066static void
16067save_command (char *arg, int from_tty)
16068{
3e43a32a
MS
16069 printf_unfiltered (_("\"save\" must be followed by "
16070 "the name of a save subcommand.\n"));
635c7e8a 16071 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
16072}
16073
84f4c1fe
PM
16074struct breakpoint *
16075iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16076 void *data)
16077{
35df4500 16078 struct breakpoint *b, *b_tmp;
84f4c1fe 16079
35df4500 16080 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
16081 {
16082 if ((*callback) (b, data))
16083 return b;
16084 }
16085
16086 return NULL;
16087}
16088
0574c78f
GB
16089/* Zero if any of the breakpoint's locations could be a location where
16090 functions have been inlined, nonzero otherwise. */
16091
16092static int
16093is_non_inline_function (struct breakpoint *b)
16094{
16095 /* The shared library event breakpoint is set on the address of a
16096 non-inline function. */
16097 if (b->type == bp_shlib_event)
16098 return 1;
16099
16100 return 0;
16101}
16102
16103/* Nonzero if the specified PC cannot be a location where functions
16104 have been inlined. */
16105
16106int
09ac7c10
TT
16107pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16108 const struct target_waitstatus *ws)
0574c78f
GB
16109{
16110 struct breakpoint *b;
16111 struct bp_location *bl;
16112
16113 ALL_BREAKPOINTS (b)
16114 {
16115 if (!is_non_inline_function (b))
16116 continue;
16117
16118 for (bl = b->loc; bl != NULL; bl = bl->next)
16119 {
16120 if (!bl->shlib_disabled
09ac7c10 16121 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
16122 return 1;
16123 }
16124 }
16125
16126 return 0;
16127}
16128
2f202fde
JK
16129/* Remove any references to OBJFILE which is going to be freed. */
16130
16131void
16132breakpoint_free_objfile (struct objfile *objfile)
16133{
16134 struct bp_location **locp, *loc;
16135
16136 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 16137 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
16138 loc->symtab = NULL;
16139}
16140
2060206e
PA
16141void
16142initialize_breakpoint_ops (void)
16143{
16144 static int initialized = 0;
16145
16146 struct breakpoint_ops *ops;
16147
16148 if (initialized)
16149 return;
16150 initialized = 1;
16151
16152 /* The breakpoint_ops structure to be inherit by all kinds of
16153 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16154 internal and momentary breakpoints, etc.). */
16155 ops = &bkpt_base_breakpoint_ops;
16156 *ops = base_breakpoint_ops;
16157 ops->re_set = bkpt_re_set;
16158 ops->insert_location = bkpt_insert_location;
16159 ops->remove_location = bkpt_remove_location;
16160 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
16161 ops->create_sals_from_address = bkpt_create_sals_from_address;
16162 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16163 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
16164
16165 /* The breakpoint_ops structure to be used in regular breakpoints. */
16166 ops = &bkpt_breakpoint_ops;
16167 *ops = bkpt_base_breakpoint_ops;
16168 ops->re_set = bkpt_re_set;
16169 ops->resources_needed = bkpt_resources_needed;
16170 ops->print_it = bkpt_print_it;
16171 ops->print_mention = bkpt_print_mention;
16172 ops->print_recreate = bkpt_print_recreate;
16173
16174 /* Ranged breakpoints. */
16175 ops = &ranged_breakpoint_ops;
16176 *ops = bkpt_breakpoint_ops;
16177 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16178 ops->resources_needed = resources_needed_ranged_breakpoint;
16179 ops->print_it = print_it_ranged_breakpoint;
16180 ops->print_one = print_one_ranged_breakpoint;
16181 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16182 ops->print_mention = print_mention_ranged_breakpoint;
16183 ops->print_recreate = print_recreate_ranged_breakpoint;
16184
16185 /* Internal breakpoints. */
16186 ops = &internal_breakpoint_ops;
16187 *ops = bkpt_base_breakpoint_ops;
16188 ops->re_set = internal_bkpt_re_set;
16189 ops->check_status = internal_bkpt_check_status;
16190 ops->print_it = internal_bkpt_print_it;
16191 ops->print_mention = internal_bkpt_print_mention;
16192
16193 /* Momentary breakpoints. */
16194 ops = &momentary_breakpoint_ops;
16195 *ops = bkpt_base_breakpoint_ops;
16196 ops->re_set = momentary_bkpt_re_set;
16197 ops->check_status = momentary_bkpt_check_status;
16198 ops->print_it = momentary_bkpt_print_it;
16199 ops->print_mention = momentary_bkpt_print_mention;
16200
e2e4d78b
JK
16201 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16202 ops = &longjmp_breakpoint_ops;
16203 *ops = momentary_breakpoint_ops;
16204 ops->dtor = longjmp_bkpt_dtor;
16205
55aa24fb
SDJ
16206 /* Probe breakpoints. */
16207 ops = &bkpt_probe_breakpoint_ops;
16208 *ops = bkpt_breakpoint_ops;
16209 ops->insert_location = bkpt_probe_insert_location;
16210 ops->remove_location = bkpt_probe_remove_location;
16211 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16212 ops->decode_linespec = bkpt_probe_decode_linespec;
16213
2060206e
PA
16214 /* Watchpoints. */
16215 ops = &watchpoint_breakpoint_ops;
16216 *ops = base_breakpoint_ops;
3a5c3e22 16217 ops->dtor = dtor_watchpoint;
2060206e
PA
16218 ops->re_set = re_set_watchpoint;
16219 ops->insert_location = insert_watchpoint;
16220 ops->remove_location = remove_watchpoint;
16221 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16222 ops->check_status = check_status_watchpoint;
16223 ops->resources_needed = resources_needed_watchpoint;
16224 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16225 ops->print_it = print_it_watchpoint;
16226 ops->print_mention = print_mention_watchpoint;
16227 ops->print_recreate = print_recreate_watchpoint;
427cd150 16228 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16229
16230 /* Masked watchpoints. */
16231 ops = &masked_watchpoint_breakpoint_ops;
16232 *ops = watchpoint_breakpoint_ops;
16233 ops->insert_location = insert_masked_watchpoint;
16234 ops->remove_location = remove_masked_watchpoint;
16235 ops->resources_needed = resources_needed_masked_watchpoint;
16236 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16237 ops->print_it = print_it_masked_watchpoint;
16238 ops->print_one_detail = print_one_detail_masked_watchpoint;
16239 ops->print_mention = print_mention_masked_watchpoint;
16240 ops->print_recreate = print_recreate_masked_watchpoint;
16241
16242 /* Tracepoints. */
16243 ops = &tracepoint_breakpoint_ops;
16244 *ops = base_breakpoint_ops;
16245 ops->re_set = tracepoint_re_set;
16246 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16247 ops->print_one_detail = tracepoint_print_one_detail;
16248 ops->print_mention = tracepoint_print_mention;
16249 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16250 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16251 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16252 ops->decode_linespec = tracepoint_decode_linespec;
16253
55aa24fb
SDJ
16254 /* Probe tracepoints. */
16255 ops = &tracepoint_probe_breakpoint_ops;
16256 *ops = tracepoint_breakpoint_ops;
16257 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16258 ops->decode_linespec = tracepoint_probe_decode_linespec;
16259
983af33b
SDJ
16260 /* Static tracepoints with marker (`-m'). */
16261 ops = &strace_marker_breakpoint_ops;
16262 *ops = tracepoint_breakpoint_ops;
16263 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16264 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16265 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16266
16267 /* Fork catchpoints. */
16268 ops = &catch_fork_breakpoint_ops;
16269 *ops = base_breakpoint_ops;
16270 ops->insert_location = insert_catch_fork;
16271 ops->remove_location = remove_catch_fork;
16272 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16273 ops->print_it = print_it_catch_fork;
16274 ops->print_one = print_one_catch_fork;
16275 ops->print_mention = print_mention_catch_fork;
16276 ops->print_recreate = print_recreate_catch_fork;
16277
16278 /* Vfork catchpoints. */
16279 ops = &catch_vfork_breakpoint_ops;
16280 *ops = base_breakpoint_ops;
16281 ops->insert_location = insert_catch_vfork;
16282 ops->remove_location = remove_catch_vfork;
16283 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16284 ops->print_it = print_it_catch_vfork;
16285 ops->print_one = print_one_catch_vfork;
16286 ops->print_mention = print_mention_catch_vfork;
16287 ops->print_recreate = print_recreate_catch_vfork;
16288
16289 /* Exec catchpoints. */
16290 ops = &catch_exec_breakpoint_ops;
16291 *ops = base_breakpoint_ops;
16292 ops->dtor = dtor_catch_exec;
16293 ops->insert_location = insert_catch_exec;
16294 ops->remove_location = remove_catch_exec;
16295 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16296 ops->print_it = print_it_catch_exec;
16297 ops->print_one = print_one_catch_exec;
16298 ops->print_mention = print_mention_catch_exec;
16299 ops->print_recreate = print_recreate_catch_exec;
16300
16301 /* Syscall catchpoints. */
16302 ops = &catch_syscall_breakpoint_ops;
16303 *ops = base_breakpoint_ops;
16304 ops->dtor = dtor_catch_syscall;
16305 ops->insert_location = insert_catch_syscall;
16306 ops->remove_location = remove_catch_syscall;
16307 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16308 ops->print_it = print_it_catch_syscall;
16309 ops->print_one = print_one_catch_syscall;
16310 ops->print_mention = print_mention_catch_syscall;
16311 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16312
16313 /* Solib-related catchpoints. */
16314 ops = &catch_solib_breakpoint_ops;
16315 *ops = base_breakpoint_ops;
16316 ops->dtor = dtor_catch_solib;
16317 ops->insert_location = insert_catch_solib;
16318 ops->remove_location = remove_catch_solib;
16319 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16320 ops->check_status = check_status_catch_solib;
16321 ops->print_it = print_it_catch_solib;
16322 ops->print_one = print_one_catch_solib;
16323 ops->print_mention = print_mention_catch_solib;
16324 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16325
16326 ops = &dprintf_breakpoint_ops;
16327 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16328 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16329 ops->resources_needed = bkpt_resources_needed;
16330 ops->print_it = bkpt_print_it;
16331 ops->print_mention = bkpt_print_mention;
2d9442cc 16332 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16333 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16334 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16335}
16336
8bfd80db
YQ
16337/* Chain containing all defined "enable breakpoint" subcommands. */
16338
16339static struct cmd_list_element *enablebreaklist = NULL;
16340
c906108c 16341void
fba45db2 16342_initialize_breakpoint (void)
c906108c
SS
16343{
16344 struct cmd_list_element *c;
16345
2060206e
PA
16346 initialize_breakpoint_ops ();
16347
84acb35a 16348 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16349 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16350 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16351 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16352
55aa24fb
SDJ
16353 breakpoint_objfile_key
16354 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16355
fa3064dd 16356 catch_syscall_inferior_data
8e260fc0
TT
16357 = register_inferior_data_with_cleanup (NULL,
16358 catch_syscall_inferior_data_cleanup);
fa3064dd 16359
c906108c
SS
16360 breakpoint_chain = 0;
16361 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16362 before a breakpoint is set. */
16363 breakpoint_count = 0;
16364
1042e4c0
SS
16365 tracepoint_count = 0;
16366
1bedd215
AC
16367 add_com ("ignore", class_breakpoint, ignore_command, _("\
16368Set ignore-count of breakpoint number N to COUNT.\n\
16369Usage is `ignore N COUNT'."));
c906108c 16370 if (xdb_commands)
c5aa993b 16371 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16372
1bedd215
AC
16373 add_com ("commands", class_breakpoint, commands_command, _("\
16374Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16375Give breakpoint number as argument after \"commands\".\n\
16376With no argument, the targeted breakpoint is the last one set.\n\
16377The commands themselves follow starting on the next line.\n\
16378Type a line containing \"end\" to indicate the end of them.\n\
16379Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16380then no output is printed when it is hit, except what the commands print."));
c906108c 16381
d55637df 16382 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16383Specify breakpoint number N to break only if COND is true.\n\
c906108c 16384Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16385expression to be evaluated whenever breakpoint N is reached."));
d55637df 16386 set_cmd_completer (c, condition_completer);
c906108c 16387
1bedd215 16388 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16389Set a temporary breakpoint.\n\
c906108c
SS
16390Like \"break\" except the breakpoint is only temporary,\n\
16391so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16392by using \"enable delete\" on the breakpoint number.\n\
16393\n"
16394BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16395 set_cmd_completer (c, location_completer);
c94fdfd0 16396
1bedd215 16397 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16398Set a hardware assisted breakpoint.\n\
c906108c 16399Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16400some target hardware may not have this support.\n\
16401\n"
16402BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16403 set_cmd_completer (c, location_completer);
c906108c 16404
1bedd215 16405 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16406Set a temporary hardware assisted breakpoint.\n\
c906108c 16407Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16408so it will be deleted when hit.\n\
16409\n"
16410BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16411 set_cmd_completer (c, location_completer);
c906108c 16412
1bedd215
AC
16413 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16414Enable some breakpoints.\n\
c906108c
SS
16415Give breakpoint numbers (separated by spaces) as arguments.\n\
16416With no subcommand, breakpoints are enabled until you command otherwise.\n\
16417This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16418With a subcommand you can enable temporarily."),
c906108c
SS
16419 &enablelist, "enable ", 1, &cmdlist);
16420 if (xdb_commands)
1bedd215
AC
16421 add_com ("ab", class_breakpoint, enable_command, _("\
16422Enable some breakpoints.\n\
c906108c
SS
16423Give breakpoint numbers (separated by spaces) as arguments.\n\
16424With no subcommand, breakpoints are enabled until you command otherwise.\n\
16425This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16426With a subcommand you can enable temporarily."));
c906108c
SS
16427
16428 add_com_alias ("en", "enable", class_breakpoint, 1);
16429
84951ab5 16430 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16431Enable some breakpoints.\n\
c906108c
SS
16432Give breakpoint numbers (separated by spaces) as arguments.\n\
16433This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16434May be abbreviated to simply \"enable\".\n"),
c5aa993b 16435 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16436
1a966eab
AC
16437 add_cmd ("once", no_class, enable_once_command, _("\
16438Enable breakpoints for one hit. Give breakpoint numbers.\n\
16439If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16440 &enablebreaklist);
16441
1a966eab
AC
16442 add_cmd ("delete", no_class, enable_delete_command, _("\
16443Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16444If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16445 &enablebreaklist);
16446
816338b5
SS
16447 add_cmd ("count", no_class, enable_count_command, _("\
16448Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16449If a breakpoint is hit while enabled in this fashion,\n\
16450the count is decremented; when it reaches zero, the breakpoint is disabled."),
16451 &enablebreaklist);
16452
1a966eab
AC
16453 add_cmd ("delete", no_class, enable_delete_command, _("\
16454Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16455If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16456 &enablelist);
16457
1a966eab
AC
16458 add_cmd ("once", no_class, enable_once_command, _("\
16459Enable breakpoints for one hit. Give breakpoint numbers.\n\
16460If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16461 &enablelist);
16462
16463 add_cmd ("count", no_class, enable_count_command, _("\
16464Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16465If a breakpoint is hit while enabled in this fashion,\n\
16466the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16467 &enablelist);
16468
1bedd215
AC
16469 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16470Disable some breakpoints.\n\
c906108c
SS
16471Arguments are breakpoint numbers with spaces in between.\n\
16472To disable all breakpoints, give no argument.\n\
64b9b334 16473A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16474 &disablelist, "disable ", 1, &cmdlist);
16475 add_com_alias ("dis", "disable", class_breakpoint, 1);
16476 add_com_alias ("disa", "disable", class_breakpoint, 1);
16477 if (xdb_commands)
1bedd215
AC
16478 add_com ("sb", class_breakpoint, disable_command, _("\
16479Disable some breakpoints.\n\
c906108c
SS
16480Arguments are breakpoint numbers with spaces in between.\n\
16481To disable all breakpoints, give no argument.\n\
64b9b334 16482A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16483
1a966eab
AC
16484 add_cmd ("breakpoints", class_alias, disable_command, _("\
16485Disable some breakpoints.\n\
c906108c
SS
16486Arguments are breakpoint numbers with spaces in between.\n\
16487To disable all breakpoints, give no argument.\n\
64b9b334 16488A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16489This command may be abbreviated \"disable\"."),
c906108c
SS
16490 &disablelist);
16491
1bedd215
AC
16492 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16493Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16494Arguments are breakpoint numbers with spaces in between.\n\
16495To delete all breakpoints, give no argument.\n\
16496\n\
16497Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16498The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16499 &deletelist, "delete ", 1, &cmdlist);
16500 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16501 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16502 if (xdb_commands)
1bedd215
AC
16503 add_com ("db", class_breakpoint, delete_command, _("\
16504Delete some breakpoints.\n\
c906108c 16505Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16506To delete all breakpoints, give no argument.\n"));
c906108c 16507
1a966eab
AC
16508 add_cmd ("breakpoints", class_alias, delete_command, _("\
16509Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16510Arguments are breakpoint numbers with spaces in between.\n\
16511To delete all breakpoints, give no argument.\n\
1a966eab 16512This command may be abbreviated \"delete\"."),
c906108c
SS
16513 &deletelist);
16514
1bedd215
AC
16515 add_com ("clear", class_breakpoint, clear_command, _("\
16516Clear breakpoint at specified line or function.\n\
c906108c
SS
16517Argument may be line number, function name, or \"*\" and an address.\n\
16518If line number is specified, all breakpoints in that line are cleared.\n\
16519If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16520If an address is specified, breakpoints at that address are cleared.\n\
16521\n\
16522With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16523is executing in.\n\
16524\n\
1bedd215 16525See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16526 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16527
1bedd215 16528 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16529Set breakpoint at specified line or function.\n"
16530BREAK_ARGS_HELP ("break")));
5ba2abeb 16531 set_cmd_completer (c, location_completer);
c94fdfd0 16532
c906108c
SS
16533 add_com_alias ("b", "break", class_run, 1);
16534 add_com_alias ("br", "break", class_run, 1);
16535 add_com_alias ("bre", "break", class_run, 1);
16536 add_com_alias ("brea", "break", class_run, 1);
16537
7681d515
PM
16538 if (xdb_commands)
16539 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16540
16541 if (dbx_commands)
16542 {
1bedd215
AC
16543 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16544Break in function/address or break at a line in the current file."),
c5aa993b
JM
16545 &stoplist, "stop ", 1, &cmdlist);
16546 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16547 _("Break in function or address."), &stoplist);
c5aa993b 16548 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16549 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16550 add_com ("status", class_info, breakpoints_info, _("\
16551Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16552The \"Type\" column indicates one of:\n\
16553\tbreakpoint - normal breakpoint\n\
16554\twatchpoint - watchpoint\n\
16555The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16556the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16557breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16558address and file/line number respectively.\n\
16559\n\
16560Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16561are set to the address of the last breakpoint listed unless the command\n\
16562is prefixed with \"server \".\n\n\
c906108c 16563Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16564breakpoint set."));
c906108c
SS
16565 }
16566
1bedd215 16567 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16568Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16569The \"Type\" column indicates one of:\n\
16570\tbreakpoint - normal breakpoint\n\
16571\twatchpoint - watchpoint\n\
16572The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16573the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16574breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16575address and file/line number respectively.\n\
16576\n\
16577Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16578are set to the address of the last breakpoint listed unless the command\n\
16579is prefixed with \"server \".\n\n\
c906108c 16580Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16581breakpoint set."));
c906108c 16582
6b04bdb7
MS
16583 add_info_alias ("b", "breakpoints", 1);
16584
c906108c 16585 if (xdb_commands)
1bedd215
AC
16586 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16587Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16588The \"Type\" column indicates one of:\n\
16589\tbreakpoint - normal breakpoint\n\
16590\twatchpoint - watchpoint\n\
16591The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16592the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16593breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16594address and file/line number respectively.\n\
16595\n\
16596Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16597are set to the address of the last breakpoint listed unless the command\n\
16598is prefixed with \"server \".\n\n\
c906108c 16599Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16600breakpoint set."));
c906108c 16601
1a966eab
AC
16602 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16603Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16604The \"Type\" column indicates one of:\n\
16605\tbreakpoint - normal breakpoint\n\
16606\twatchpoint - watchpoint\n\
16607\tlongjmp - internal breakpoint used to step through longjmp()\n\
16608\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16609\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16610\tfinish - internal breakpoint used by the \"finish\" command\n\
16611The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16612the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16613breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16614address and file/line number respectively.\n\
16615\n\
16616Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16617are set to the address of the last breakpoint listed unless the command\n\
16618is prefixed with \"server \".\n\n\
c906108c 16619Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16620breakpoint set."),
c906108c
SS
16621 &maintenanceinfolist);
16622
44feb3ce
TT
16623 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16624Set catchpoints to catch events."),
16625 &catch_cmdlist, "catch ",
16626 0/*allow-unknown*/, &cmdlist);
16627
16628 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16629Set temporary catchpoints to catch events."),
16630 &tcatch_cmdlist, "tcatch ",
16631 0/*allow-unknown*/, &cmdlist);
16632
44feb3ce
TT
16633 add_catch_command ("fork", _("Catch calls to fork."),
16634 catch_fork_command_1,
a96d9b2e 16635 NULL,
44feb3ce
TT
16636 (void *) (uintptr_t) catch_fork_permanent,
16637 (void *) (uintptr_t) catch_fork_temporary);
16638 add_catch_command ("vfork", _("Catch calls to vfork."),
16639 catch_fork_command_1,
a96d9b2e 16640 NULL,
44feb3ce
TT
16641 (void *) (uintptr_t) catch_vfork_permanent,
16642 (void *) (uintptr_t) catch_vfork_temporary);
16643 add_catch_command ("exec", _("Catch calls to exec."),
16644 catch_exec_command_1,
a96d9b2e
SDJ
16645 NULL,
16646 CATCH_PERMANENT,
16647 CATCH_TEMPORARY);
edcc5120
TT
16648 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16649Usage: catch load [REGEX]\n\
16650If REGEX is given, only stop for libraries matching the regular expression."),
16651 catch_load_command_1,
16652 NULL,
16653 CATCH_PERMANENT,
16654 CATCH_TEMPORARY);
16655 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16656Usage: catch unload [REGEX]\n\
16657If REGEX is given, only stop for libraries matching the regular expression."),
16658 catch_unload_command_1,
16659 NULL,
16660 CATCH_PERMANENT,
16661 CATCH_TEMPORARY);
a96d9b2e
SDJ
16662 add_catch_command ("syscall", _("\
16663Catch system calls by their names and/or numbers.\n\
16664Arguments say which system calls to catch. If no arguments\n\
16665are given, every system call will be caught.\n\
16666Arguments, if given, should be one or more system call names\n\
16667(if your system supports that), or system call numbers."),
16668 catch_syscall_command_1,
16669 catch_syscall_completer,
44feb3ce
TT
16670 CATCH_PERMANENT,
16671 CATCH_TEMPORARY);
c5aa993b 16672
1bedd215
AC
16673 c = add_com ("watch", class_breakpoint, watch_command, _("\
16674Set a watchpoint for an expression.\n\
06a64a0b 16675Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16676A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16677an expression changes.\n\
16678If -l or -location is given, this evaluates EXPRESSION and watches\n\
16679the memory to which it refers."));
65d12d83 16680 set_cmd_completer (c, expression_completer);
c906108c 16681
1bedd215
AC
16682 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16683Set a read watchpoint for an expression.\n\
06a64a0b 16684Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16685A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16686an expression is read.\n\
16687If -l or -location is given, this evaluates EXPRESSION and watches\n\
16688the memory to which it refers."));
65d12d83 16689 set_cmd_completer (c, expression_completer);
c906108c 16690
1bedd215
AC
16691 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16692Set a watchpoint for an expression.\n\
06a64a0b 16693Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16694A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16695an expression is either read or written.\n\
16696If -l or -location is given, this evaluates EXPRESSION and watches\n\
16697the memory to which it refers."));
65d12d83 16698 set_cmd_completer (c, expression_completer);
c906108c 16699
d77f58be 16700 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16701Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16702
920d2a44
AC
16703 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16704 respond to changes - contrary to the description. */
85c07804
AC
16705 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16706 &can_use_hw_watchpoints, _("\
16707Set debugger's willingness to use watchpoint hardware."), _("\
16708Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16709If zero, gdb will not use hardware for new watchpoints, even if\n\
16710such is available. (However, any hardware watchpoints that were\n\
16711created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16712hardware.)"),
16713 NULL,
920d2a44 16714 show_can_use_hw_watchpoints,
85c07804 16715 &setlist, &showlist);
c906108c
SS
16716
16717 can_use_hw_watchpoints = 1;
fa8d40ab 16718
1042e4c0
SS
16719 /* Tracepoint manipulation commands. */
16720
16721 c = add_com ("trace", class_breakpoint, trace_command, _("\
16722Set a tracepoint at specified line or function.\n\
16723\n"
16724BREAK_ARGS_HELP ("trace") "\n\
16725Do \"help tracepoints\" for info on other tracepoint commands."));
16726 set_cmd_completer (c, location_completer);
16727
16728 add_com_alias ("tp", "trace", class_alias, 0);
16729 add_com_alias ("tr", "trace", class_alias, 1);
16730 add_com_alias ("tra", "trace", class_alias, 1);
16731 add_com_alias ("trac", "trace", class_alias, 1);
16732
7a697b8d
SS
16733 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16734Set a fast tracepoint at specified line or function.\n\
16735\n"
16736BREAK_ARGS_HELP ("ftrace") "\n\
16737Do \"help tracepoints\" for info on other tracepoint commands."));
16738 set_cmd_completer (c, location_completer);
16739
0fb4aa4b
PA
16740 c = add_com ("strace", class_breakpoint, strace_command, _("\
16741Set a static tracepoint at specified line, function or marker.\n\
16742\n\
16743strace [LOCATION] [if CONDITION]\n\
16744LOCATION may be a line number, function name, \"*\" and an address,\n\
16745or -m MARKER_ID.\n\
16746If a line number is specified, probe the marker at start of code\n\
16747for that line. If a function is specified, probe the marker at start\n\
16748of code for that function. If an address is specified, probe the marker\n\
16749at that exact address. If a marker id is specified, probe the marker\n\
16750with that name. With no LOCATION, uses current execution address of\n\
16751the selected stack frame.\n\
16752Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16753This collects arbitrary user data passed in the probe point call to the\n\
16754tracing library. You can inspect it when analyzing the trace buffer,\n\
16755by printing the $_sdata variable like any other convenience variable.\n\
16756\n\
16757CONDITION is a boolean expression.\n\
16758\n\
d41c0fc8
PA
16759Multiple tracepoints at one place are permitted, and useful if their\n\
16760conditions are different.\n\
0fb4aa4b
PA
16761\n\
16762Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16763Do \"help tracepoints\" for info on other tracepoint commands."));
16764 set_cmd_completer (c, location_completer);
16765
1042e4c0 16766 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16767Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16768Convenience variable \"$tpnum\" contains the number of the\n\
16769last tracepoint set."));
16770
16771 add_info_alias ("tp", "tracepoints", 1);
16772
16773 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16774Delete specified tracepoints.\n\
16775Arguments are tracepoint numbers, separated by spaces.\n\
16776No argument means delete all tracepoints."),
16777 &deletelist);
7e20dfcd 16778 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16779
16780 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16781Disable specified tracepoints.\n\
16782Arguments are tracepoint numbers, separated by spaces.\n\
16783No argument means disable all tracepoints."),
16784 &disablelist);
16785 deprecate_cmd (c, "disable");
16786
16787 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16788Enable specified tracepoints.\n\
16789Arguments are tracepoint numbers, separated by spaces.\n\
16790No argument means enable all tracepoints."),
16791 &enablelist);
16792 deprecate_cmd (c, "enable");
16793
16794 add_com ("passcount", class_trace, trace_pass_command, _("\
16795Set the passcount for a tracepoint.\n\
16796The trace will end when the tracepoint has been passed 'count' times.\n\
16797Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16798if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16799
6149aea9
PA
16800 add_prefix_cmd ("save", class_breakpoint, save_command,
16801 _("Save breakpoint definitions as a script."),
16802 &save_cmdlist, "save ",
16803 0/*allow-unknown*/, &cmdlist);
16804
16805 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16806Save current breakpoint definitions as a script.\n\
cce7e648 16807This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16808catchpoints, tracepoints). Use the 'source' command in another debug\n\
16809session to restore them."),
16810 &save_cmdlist);
16811 set_cmd_completer (c, filename_completer);
16812
16813 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16814Save current tracepoint definitions as a script.\n\
6149aea9
PA
16815Use the 'source' command in another debug session to restore them."),
16816 &save_cmdlist);
1042e4c0
SS
16817 set_cmd_completer (c, filename_completer);
16818
6149aea9
PA
16819 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16820 deprecate_cmd (c, "save tracepoints");
16821
1bedd215 16822 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16823Breakpoint specific settings\n\
16824Configure various breakpoint-specific variables such as\n\
1bedd215 16825pending breakpoint behavior"),
fa8d40ab
JJ
16826 &breakpoint_set_cmdlist, "set breakpoint ",
16827 0/*allow-unknown*/, &setlist);
1bedd215 16828 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16829Breakpoint specific settings\n\
16830Configure various breakpoint-specific variables such as\n\
1bedd215 16831pending breakpoint behavior"),
fa8d40ab
JJ
16832 &breakpoint_show_cmdlist, "show breakpoint ",
16833 0/*allow-unknown*/, &showlist);
16834
7915a72c
AC
16835 add_setshow_auto_boolean_cmd ("pending", no_class,
16836 &pending_break_support, _("\
16837Set debugger's behavior regarding pending breakpoints."), _("\
16838Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16839If on, an unrecognized breakpoint location will cause gdb to create a\n\
16840pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16841an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16842user-query to see if a pending breakpoint should be created."),
2c5b56ce 16843 NULL,
920d2a44 16844 show_pending_break_support,
6e1d7d6c
AC
16845 &breakpoint_set_cmdlist,
16846 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16847
16848 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16849
16850 add_setshow_boolean_cmd ("auto-hw", no_class,
16851 &automatic_hardware_breakpoints, _("\
16852Set automatic usage of hardware breakpoints."), _("\
16853Show automatic usage of hardware breakpoints."), _("\
16854If set, the debugger will automatically use hardware breakpoints for\n\
16855breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16856a warning will be emitted for such breakpoints."),
16857 NULL,
16858 show_automatic_hardware_breakpoints,
16859 &breakpoint_set_cmdlist,
16860 &breakpoint_show_cmdlist);
74960c60 16861
a25a5a45
PA
16862 add_setshow_boolean_cmd ("always-inserted", class_support,
16863 &always_inserted_mode, _("\
74960c60
VP
16864Set mode for inserting breakpoints."), _("\
16865Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16866When this mode is on, breakpoints are inserted immediately as soon as\n\
16867they're created, kept inserted even when execution stops, and removed\n\
16868only when the user deletes them. When this mode is off (the default),\n\
16869breakpoints are inserted only when execution continues, and removed\n\
16870when execution stops."),
72d0e2c5
YQ
16871 NULL,
16872 &show_always_inserted_mode,
16873 &breakpoint_set_cmdlist,
16874 &breakpoint_show_cmdlist);
f1310107 16875
b775012e
LM
16876 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16877 condition_evaluation_enums,
16878 &condition_evaluation_mode_1, _("\
16879Set mode of breakpoint condition evaluation."), _("\
16880Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16881When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16882evaluated on the host's side by GDB. When it is set to \"target\",\n\
16883breakpoint conditions will be downloaded to the target (if the target\n\
16884supports such feature) and conditions will be evaluated on the target's side.\n\
16885If this is set to \"auto\" (default), this will be automatically set to\n\
16886\"target\" if it supports condition evaluation, otherwise it will\n\
16887be set to \"gdb\""),
16888 &set_condition_evaluation_mode,
16889 &show_condition_evaluation_mode,
16890 &breakpoint_set_cmdlist,
16891 &breakpoint_show_cmdlist);
16892
f1310107
TJB
16893 add_com ("break-range", class_breakpoint, break_range_command, _("\
16894Set a breakpoint for an address range.\n\
16895break-range START-LOCATION, END-LOCATION\n\
16896where START-LOCATION and END-LOCATION can be one of the following:\n\
16897 LINENUM, for that line in the current file,\n\
16898 FILE:LINENUM, for that line in that file,\n\
16899 +OFFSET, for that number of lines after the current line\n\
16900 or the start of the range\n\
16901 FUNCTION, for the first line in that function,\n\
16902 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16903 *ADDRESS, for the instruction at that address.\n\
16904\n\
16905The breakpoint will stop execution of the inferior whenever it executes\n\
16906an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16907range (including START-LOCATION and END-LOCATION)."));
16908
e7e0cddf
SS
16909 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16910Set a dynamic printf at specified line or function.\n\
16911dprintf location,format string,arg1,arg2,...\n\
16912location may be a line number, function name, or \"*\" and an address.\n\
16913If a line number is specified, break at start of code for that line.\n\
0e4777df 16914If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16915 set_cmd_completer (c, location_completer);
16916
16917 add_setshow_enum_cmd ("dprintf-style", class_support,
16918 dprintf_style_enums, &dprintf_style, _("\
16919Set the style of usage for dynamic printf."), _("\
16920Show the style of usage for dynamic printf."), _("\
16921This setting chooses how GDB will do a dynamic printf.\n\
16922If the value is \"gdb\", then the printing is done by GDB to its own\n\
16923console, as with the \"printf\" command.\n\
16924If the value is \"call\", the print is done by calling a function in your\n\
16925program; by default printf(), but you can choose a different function or\n\
16926output stream by setting dprintf-function and dprintf-channel."),
16927 update_dprintf_commands, NULL,
16928 &setlist, &showlist);
16929
16930 dprintf_function = xstrdup ("printf");
16931 add_setshow_string_cmd ("dprintf-function", class_support,
16932 &dprintf_function, _("\
16933Set the function to use for dynamic printf"), _("\
16934Show the function to use for dynamic printf"), NULL,
16935 update_dprintf_commands, NULL,
16936 &setlist, &showlist);
16937
16938 dprintf_channel = xstrdup ("");
16939 add_setshow_string_cmd ("dprintf-channel", class_support,
16940 &dprintf_channel, _("\
16941Set the channel to use for dynamic printf"), _("\
16942Show the channel to use for dynamic printf"), NULL,
16943 update_dprintf_commands, NULL,
16944 &setlist, &showlist);
16945
d3ce09f5
SS
16946 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16947 &disconnected_dprintf, _("\
16948Set whether dprintf continues after GDB disconnects."), _("\
16949Show whether dprintf continues after GDB disconnects."), _("\
16950Use this to let dprintf commands continue to hit and produce output\n\
16951even if GDB disconnects or detaches from the target."),
16952 NULL,
16953 NULL,
16954 &setlist, &showlist);
16955
16956 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16957agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16958(target agent only) This is useful for formatted output in user-defined commands."));
16959
765dc015 16960 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16961
16962 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16963 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16964}
This page took 3.709672 seconds and 4 git commands to generate.