]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
gas/testsuite:
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
0b302171 3 Copyright (C) 1986-2012 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
35#include "gdbthread.h"
36#include "target.h"
37#include "language.h"
38#include "gdb_string.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
0225421b 50#include "gdb_assert.h"
fe898f56 51#include "block.h"
a77053c2 52#include "solib.h"
84acb35a
JJ
53#include "solist.h"
54#include "observer.h"
60250e8b 55#include "exceptions.h"
765dc015 56#include "memattr.h"
f7f9143b 57#include "ada-lang.h"
d1aa2f50 58#include "top.h"
fa4727a6 59#include "wrapper.h"
79a45b7d 60#include "valprint.h"
4efc6507 61#include "jit.h"
a96d9b2e 62#include "xml-syscall.h"
65d79d4b 63#include "parser-defs.h"
e9cafbcc 64#include "cli/cli-utils.h"
be34f849 65#include "continuations.h"
1bfeeb0f
JL
66#include "stack.h"
67#include "skip.h"
049e32d3 68#include "record.h"
c906108c 69
1042e4c0
SS
70/* readline include files */
71#include "readline/readline.h"
72#include "readline/history.h"
73
74/* readline defines this. */
75#undef savestring
76
034dad6f 77#include "mi/mi-common.h"
7371cf6d 78#include "python/python.h"
104c1213 79
4a64f543 80/* Prototypes for local functions. */
c906108c 81
a14ed312 82static void enable_delete_command (char *, int);
c906108c 83
a14ed312 84static void enable_once_command (char *, int);
c906108c 85
a14ed312 86static void disable_command (char *, int);
c906108c 87
a14ed312 88static void enable_command (char *, int);
c906108c 89
95a42b64
TT
90static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
91 void *),
92 void *);
c906108c 93
a14ed312 94static void ignore_command (char *, int);
c906108c 95
4efb68b1 96static int breakpoint_re_set_one (void *);
c906108c 97
348d480f
PA
98static void breakpoint_re_set_default (struct breakpoint *);
99
a14ed312 100static void clear_command (char *, int);
c906108c 101
a14ed312 102static void catch_command (char *, int);
c906108c 103
a9634178 104static int can_use_hardware_watchpoint (struct value *);
c906108c 105
98deb0da 106static void break_command_1 (char *, int, int);
c906108c 107
a14ed312 108static void mention (struct breakpoint *);
c906108c 109
348d480f
PA
110static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
111 enum bptype,
c0a91b2b 112 const struct breakpoint_ops *);
3742cc8b
YQ
113static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
114 const struct symtab_and_line *);
115
4a64f543
MS
116/* This function is used in gdbtk sources and thus can not be made
117 static. */
63c252f8 118struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 119 struct symtab_and_line,
c0a91b2b
TT
120 enum bptype,
121 const struct breakpoint_ops *);
c906108c 122
06edf0c0
PA
123static struct breakpoint *
124 momentary_breakpoint_from_master (struct breakpoint *orig,
125 enum bptype type,
c0a91b2b 126 const struct breakpoint_ops *ops);
06edf0c0 127
76897487
KB
128static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
129
a6d9a66e
UW
130static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
131 CORE_ADDR bpaddr,
88f7da05 132 enum bptype bptype);
76897487 133
6c95b8df
PA
134static void describe_other_breakpoints (struct gdbarch *,
135 struct program_space *, CORE_ADDR,
5af949e3 136 struct obj_section *, int);
c906108c 137
6c95b8df
PA
138static int breakpoint_address_match (struct address_space *aspace1,
139 CORE_ADDR addr1,
140 struct address_space *aspace2,
141 CORE_ADDR addr2);
142
85d721b8
PA
143static int watchpoint_locations_match (struct bp_location *loc1,
144 struct bp_location *loc2);
145
f1310107
TJB
146static int breakpoint_location_address_match (struct bp_location *bl,
147 struct address_space *aspace,
148 CORE_ADDR addr);
149
a14ed312 150static void breakpoints_info (char *, int);
c906108c 151
d77f58be
SS
152static void watchpoints_info (char *, int);
153
e5a67952
MS
154static int breakpoint_1 (char *, int,
155 int (*) (const struct breakpoint *));
c906108c 156
4efb68b1 157static int breakpoint_cond_eval (void *);
c906108c 158
4efb68b1 159static void cleanup_executing_breakpoints (void *);
c906108c 160
a14ed312 161static void commands_command (char *, int);
c906108c 162
a14ed312 163static void condition_command (char *, int);
c906108c 164
c5aa993b
JM
165typedef enum
166 {
167 mark_inserted,
168 mark_uninserted
169 }
170insertion_state_t;
c906108c 171
0bde7532 172static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 173static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 174
e514a9d6 175static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 176
4efb68b1 177static int watchpoint_check (void *);
c906108c 178
a14ed312 179static void maintenance_info_breakpoints (char *, int);
c906108c 180
a14ed312 181static int hw_breakpoint_used_count (void);
c906108c 182
a1398e0c
PA
183static int hw_watchpoint_use_count (struct breakpoint *);
184
185static int hw_watchpoint_used_count_others (struct breakpoint *except,
186 enum bptype type,
187 int *other_type_used);
c906108c 188
a14ed312 189static void hbreak_command (char *, int);
c906108c 190
a14ed312 191static void thbreak_command (char *, int);
c906108c 192
51be5b68 193static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp);
c906108c 194
a14ed312 195static void stop_command (char *arg, int from_tty);
7a292a7a 196
a14ed312 197static void stopin_command (char *arg, int from_tty);
7a292a7a 198
a14ed312 199static void stopat_command (char *arg, int from_tty);
7a292a7a 200
a14ed312 201static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 202
d85310f7
MS
203static void catch_exception_command_1 (enum exception_event_kind ex_event,
204 char *arg, int tempflag, int from_tty);
7a292a7a 205
a14ed312 206static void tcatch_command (char *arg, int from_tty);
7a292a7a 207
d03285ec
UW
208static void detach_single_step_breakpoints (void);
209
6c95b8df
PA
210static int single_step_breakpoint_inserted_here_p (struct address_space *,
211 CORE_ADDR pc);
1aafd4da 212
fe3f5fa8 213static void free_bp_location (struct bp_location *loc);
f431efe5
PA
214static void incref_bp_location (struct bp_location *loc);
215static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 216
39d61571 217static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 218
b60e7edf 219static void update_global_location_list (int);
a5606eee 220
b60e7edf 221static void update_global_location_list_nothrow (int);
74960c60 222
d77f58be 223static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
224
225static void insert_breakpoint_locations (void);
a5606eee 226
a96d9b2e
SDJ
227static int syscall_catchpoint_p (struct breakpoint *b);
228
1042e4c0
SS
229static void tracepoints_info (char *, int);
230
231static void delete_trace_command (char *, int);
232
233static void enable_trace_command (char *, int);
234
235static void disable_trace_command (char *, int);
236
237static void trace_pass_command (char *, int);
238
9c06b0b4
TJB
239static int is_masked_watchpoint (const struct breakpoint *b);
240
0fb4aa4b
PA
241/* Assuming we're creating a static tracepoint, does S look like a
242 static tracepoint marker spec ("-m MARKER_ID")? */
243#define is_marker_spec(s) \
f5a8e22b 244 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 245
2060206e
PA
246/* The abstract base class all breakpoint_ops structures inherit
247 from. */
248static struct breakpoint_ops base_breakpoint_ops;
249
250/* The breakpoint_ops structure to be inherited by all breakpoint_ops
251 that are implemented on top of software or hardware breakpoints
252 (user breakpoints, internal and momentary breakpoints, etc.). */
253static struct breakpoint_ops bkpt_base_breakpoint_ops;
254
255/* Internal breakpoints class type. */
06edf0c0 256static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
257
258/* Momentary breakpoints class type. */
06edf0c0
PA
259static struct breakpoint_ops momentary_breakpoint_ops;
260
2060206e
PA
261/* The breakpoint_ops structure to be used in regular user created
262 breakpoints. */
263struct breakpoint_ops bkpt_breakpoint_ops;
264
5cea2a26
PA
265/* A reference-counted struct command_line. This lets multiple
266 breakpoints share a single command list. */
267struct counted_command_line
268{
269 /* The reference count. */
270 int refc;
271
272 /* The command list. */
273 struct command_line *commands;
274};
275
276struct command_line *
277breakpoint_commands (struct breakpoint *b)
278{
279 return b->commands ? b->commands->commands : NULL;
280}
3daf8fe5 281
f3b1572e
PA
282/* Flag indicating that a command has proceeded the inferior past the
283 current breakpoint. */
284
285static int breakpoint_proceeded;
286
956a9fb9 287const char *
2cec12e5
AR
288bpdisp_text (enum bpdisp disp)
289{
4a64f543
MS
290 /* NOTE: the following values are a part of MI protocol and
291 represent values of 'disp' field returned when inferior stops at
292 a breakpoint. */
bc043ef3 293 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 294
2cec12e5
AR
295 return bpdisps[(int) disp];
296}
c906108c 297
4a64f543 298/* Prototypes for exported functions. */
c906108c 299/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 300 if such is available. */
c906108c
SS
301static int can_use_hw_watchpoints;
302
920d2a44
AC
303static void
304show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
305 struct cmd_list_element *c,
306 const char *value)
307{
3e43a32a
MS
308 fprintf_filtered (file,
309 _("Debugger's willingness to use "
310 "watchpoint hardware is %s.\n"),
920d2a44
AC
311 value);
312}
313
fa8d40ab
JJ
314/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
315 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 316 for unrecognized breakpoint locations.
fa8d40ab
JJ
317 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
318static enum auto_boolean pending_break_support;
920d2a44
AC
319static void
320show_pending_break_support (struct ui_file *file, int from_tty,
321 struct cmd_list_element *c,
322 const char *value)
323{
3e43a32a
MS
324 fprintf_filtered (file,
325 _("Debugger's behavior regarding "
326 "pending breakpoints is %s.\n"),
920d2a44
AC
327 value);
328}
fa8d40ab 329
765dc015 330/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 331 set with "break" but falling in read-only memory.
765dc015
VP
332 If 0, gdb will warn about such breakpoints, but won't automatically
333 use hardware breakpoints. */
334static int automatic_hardware_breakpoints;
335static void
336show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
337 struct cmd_list_element *c,
338 const char *value)
339{
3e43a32a
MS
340 fprintf_filtered (file,
341 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
342 value);
343}
344
33e5cbd6
PA
345/* If on, gdb will keep breakpoints inserted even as inferior is
346 stopped, and immediately insert any new breakpoints. If off, gdb
347 will insert breakpoints into inferior only when resuming it, and
348 will remove breakpoints upon stop. If auto, GDB will behave as ON
349 if in non-stop mode, and as OFF if all-stop mode.*/
350
351static const char always_inserted_auto[] = "auto";
352static const char always_inserted_on[] = "on";
353static const char always_inserted_off[] = "off";
354static const char *always_inserted_enums[] = {
355 always_inserted_auto,
356 always_inserted_off,
357 always_inserted_on,
358 NULL
359};
360static const char *always_inserted_mode = always_inserted_auto;
361static void
74960c60 362show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 363 struct cmd_list_element *c, const char *value)
74960c60 364{
33e5cbd6 365 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
366 fprintf_filtered (file,
367 _("Always inserted breakpoint "
368 "mode is %s (currently %s).\n"),
33e5cbd6
PA
369 value,
370 breakpoints_always_inserted_mode () ? "on" : "off");
371 else
3e43a32a
MS
372 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
373 value);
74960c60
VP
374}
375
33e5cbd6
PA
376int
377breakpoints_always_inserted_mode (void)
378{
049e32d3
PA
379 return ((always_inserted_mode == always_inserted_on
380 || (always_inserted_mode == always_inserted_auto && non_stop))
381 && !RECORD_IS_USED);
33e5cbd6 382}
765dc015 383
a14ed312 384void _initialize_breakpoint (void);
c906108c 385
c906108c
SS
386/* Are we executing breakpoint commands? */
387static int executing_breakpoint_commands;
388
c02f5703
MS
389/* Are overlay event breakpoints enabled? */
390static int overlay_events_enabled;
391
e09342b5
TJB
392/* See description in breakpoint.h. */
393int target_exact_watchpoints = 0;
394
c906108c 395/* Walk the following statement or block through all breakpoints.
e5dd4106 396 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 397 current breakpoint. */
c906108c 398
5c44784c 399#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 400
5c44784c
JM
401#define ALL_BREAKPOINTS_SAFE(B,TMP) \
402 for (B = breakpoint_chain; \
403 B ? (TMP=B->next, 1): 0; \
404 B = TMP)
c906108c 405
4a64f543
MS
406/* Similar iterator for the low-level breakpoints. SAFE variant is
407 not provided so update_global_location_list must not be called
408 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 409
876fa593
JK
410#define ALL_BP_LOCATIONS(B,BP_TMP) \
411 for (BP_TMP = bp_location; \
412 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
413 BP_TMP++)
7cc221ef 414
1042e4c0
SS
415/* Iterator for tracepoints only. */
416
417#define ALL_TRACEPOINTS(B) \
418 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 419 if (is_tracepoint (B))
1042e4c0 420
7cc221ef 421/* Chains of all breakpoints defined. */
c906108c
SS
422
423struct breakpoint *breakpoint_chain;
424
876fa593
JK
425/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
426
427static struct bp_location **bp_location;
428
429/* Number of elements of BP_LOCATION. */
430
431static unsigned bp_location_count;
432
4a64f543
MS
433/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
434 ADDRESS for the current elements of BP_LOCATION which get a valid
435 result from bp_location_has_shadow. You can use it for roughly
436 limiting the subrange of BP_LOCATION to scan for shadow bytes for
437 an address you need to read. */
876fa593
JK
438
439static CORE_ADDR bp_location_placed_address_before_address_max;
440
4a64f543
MS
441/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
442 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
443 BP_LOCATION which get a valid result from bp_location_has_shadow.
444 You can use it for roughly limiting the subrange of BP_LOCATION to
445 scan for shadow bytes for an address you need to read. */
876fa593
JK
446
447static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 448
4a64f543
MS
449/* The locations that no longer correspond to any breakpoint, unlinked
450 from bp_location array, but for which a hit may still be reported
451 by a target. */
20874c92
VP
452VEC(bp_location_p) *moribund_locations = NULL;
453
c906108c
SS
454/* Number of last breakpoint made. */
455
95a42b64
TT
456static int breakpoint_count;
457
86b17b60
PA
458/* The value of `breakpoint_count' before the last command that
459 created breakpoints. If the last (break-like) command created more
460 than one breakpoint, then the difference between BREAKPOINT_COUNT
461 and PREV_BREAKPOINT_COUNT is more than one. */
462static int prev_breakpoint_count;
c906108c 463
1042e4c0
SS
464/* Number of last tracepoint made. */
465
95a42b64 466static int tracepoint_count;
1042e4c0 467
6149aea9
PA
468static struct cmd_list_element *breakpoint_set_cmdlist;
469static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 470struct cmd_list_element *save_cmdlist;
6149aea9 471
468d015d
JJ
472/* Return whether a breakpoint is an active enabled breakpoint. */
473static int
474breakpoint_enabled (struct breakpoint *b)
475{
0d381245 476 return (b->enable_state == bp_enabled);
468d015d
JJ
477}
478
c906108c
SS
479/* Set breakpoint count to NUM. */
480
95a42b64 481static void
fba45db2 482set_breakpoint_count (int num)
c906108c 483{
86b17b60 484 prev_breakpoint_count = breakpoint_count;
c906108c 485 breakpoint_count = num;
4fa62494 486 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
487}
488
86b17b60
PA
489/* Used by `start_rbreak_breakpoints' below, to record the current
490 breakpoint count before "rbreak" creates any breakpoint. */
491static int rbreak_start_breakpoint_count;
492
95a42b64
TT
493/* Called at the start an "rbreak" command to record the first
494 breakpoint made. */
86b17b60 495
95a42b64
TT
496void
497start_rbreak_breakpoints (void)
498{
86b17b60 499 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
500}
501
502/* Called at the end of an "rbreak" command to record the last
503 breakpoint made. */
86b17b60 504
95a42b64
TT
505void
506end_rbreak_breakpoints (void)
507{
86b17b60 508 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
509}
510
4a64f543 511/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
512
513void
fba45db2 514clear_breakpoint_hit_counts (void)
c906108c
SS
515{
516 struct breakpoint *b;
517
518 ALL_BREAKPOINTS (b)
519 b->hit_count = 0;
520}
521
9add0f1b
TT
522/* Allocate a new counted_command_line with reference count of 1.
523 The new structure owns COMMANDS. */
524
525static struct counted_command_line *
526alloc_counted_command_line (struct command_line *commands)
527{
528 struct counted_command_line *result
529 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 530
9add0f1b
TT
531 result->refc = 1;
532 result->commands = commands;
533 return result;
534}
535
536/* Increment reference count. This does nothing if CMD is NULL. */
537
538static void
539incref_counted_command_line (struct counted_command_line *cmd)
540{
541 if (cmd)
542 ++cmd->refc;
543}
544
545/* Decrement reference count. If the reference count reaches 0,
546 destroy the counted_command_line. Sets *CMDP to NULL. This does
547 nothing if *CMDP is NULL. */
548
549static void
550decref_counted_command_line (struct counted_command_line **cmdp)
551{
552 if (*cmdp)
553 {
554 if (--(*cmdp)->refc == 0)
555 {
556 free_command_lines (&(*cmdp)->commands);
557 xfree (*cmdp);
558 }
559 *cmdp = NULL;
560 }
561}
562
563/* A cleanup function that calls decref_counted_command_line. */
564
565static void
566do_cleanup_counted_command_line (void *arg)
567{
568 decref_counted_command_line (arg);
569}
570
571/* Create a cleanup that calls decref_counted_command_line on the
572 argument. */
573
574static struct cleanup *
575make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
576{
577 return make_cleanup (do_cleanup_counted_command_line, cmdp);
578}
579
c906108c 580\f
48cb2d85
VP
581/* Return the breakpoint with the specified number, or NULL
582 if the number does not refer to an existing breakpoint. */
583
584struct breakpoint *
585get_breakpoint (int num)
586{
587 struct breakpoint *b;
588
589 ALL_BREAKPOINTS (b)
590 if (b->number == num)
591 return b;
592
593 return NULL;
594}
5c44784c 595
c906108c 596\f
adc36818
PM
597
598void
599set_breakpoint_condition (struct breakpoint *b, char *exp,
600 int from_tty)
601{
3a5c3e22
PA
602 xfree (b->cond_string);
603 b->cond_string = NULL;
adc36818 604
3a5c3e22 605 if (is_watchpoint (b))
adc36818 606 {
3a5c3e22
PA
607 struct watchpoint *w = (struct watchpoint *) b;
608
609 xfree (w->cond_exp);
610 w->cond_exp = NULL;
611 }
612 else
613 {
614 struct bp_location *loc;
615
616 for (loc = b->loc; loc; loc = loc->next)
617 {
618 xfree (loc->cond);
619 loc->cond = NULL;
620 }
adc36818 621 }
adc36818
PM
622
623 if (*exp == 0)
624 {
625 if (from_tty)
626 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
627 }
628 else
629 {
630 char *arg = exp;
cc59ec59 631
adc36818
PM
632 /* I don't know if it matters whether this is the string the user
633 typed in or the decompiled expression. */
634 b->cond_string = xstrdup (arg);
635 b->condition_not_parsed = 0;
636
637 if (is_watchpoint (b))
638 {
3a5c3e22
PA
639 struct watchpoint *w = (struct watchpoint *) b;
640
adc36818
PM
641 innermost_block = NULL;
642 arg = exp;
3a5c3e22 643 w->cond_exp = parse_exp_1 (&arg, 0, 0);
adc36818
PM
644 if (*arg)
645 error (_("Junk at end of expression"));
3a5c3e22 646 w->cond_exp_valid_block = innermost_block;
adc36818
PM
647 }
648 else
649 {
3a5c3e22
PA
650 struct bp_location *loc;
651
adc36818
PM
652 for (loc = b->loc; loc; loc = loc->next)
653 {
654 arg = exp;
655 loc->cond =
656 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
657 if (*arg)
658 error (_("Junk at end of expression"));
659 }
660 }
661 }
662 breakpoints_changed ();
8d3788bd 663 observer_notify_breakpoint_modified (b);
adc36818
PM
664}
665
c906108c
SS
666/* condition N EXP -- set break condition of breakpoint N to EXP. */
667
668static void
fba45db2 669condition_command (char *arg, int from_tty)
c906108c 670{
52f0bd74 671 struct breakpoint *b;
c906108c 672 char *p;
52f0bd74 673 int bnum;
c906108c
SS
674
675 if (arg == 0)
e2e0b3e5 676 error_no_arg (_("breakpoint number"));
c906108c
SS
677
678 p = arg;
679 bnum = get_number (&p);
5c44784c 680 if (bnum == 0)
8a3fe4f8 681 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
682
683 ALL_BREAKPOINTS (b)
684 if (b->number == bnum)
2f069f6f 685 {
7371cf6d
PM
686 /* Check if this breakpoint has a Python object assigned to
687 it, and if it has a definition of the "stop"
688 method. This method and conditions entered into GDB from
689 the CLI are mutually exclusive. */
690 if (b->py_bp_object
691 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
692 error (_("Cannot set a condition where a Python 'stop' "
693 "method has been defined in the breakpoint."));
2566ad2d 694 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
695 return;
696 }
c906108c 697
8a3fe4f8 698 error (_("No breakpoint number %d."), bnum);
c906108c
SS
699}
700
a7bdde9e
VP
701/* Check that COMMAND do not contain commands that are suitable
702 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
703 Throw if any such commands is found. */
704
a7bdde9e
VP
705static void
706check_no_tracepoint_commands (struct command_line *commands)
707{
708 struct command_line *c;
cc59ec59 709
a7bdde9e
VP
710 for (c = commands; c; c = c->next)
711 {
712 int i;
713
714 if (c->control_type == while_stepping_control)
3e43a32a
MS
715 error (_("The 'while-stepping' command can "
716 "only be used for tracepoints"));
a7bdde9e
VP
717
718 for (i = 0; i < c->body_count; ++i)
719 check_no_tracepoint_commands ((c->body_list)[i]);
720
721 /* Not that command parsing removes leading whitespace and comment
4a64f543 722 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
723 command directly. */
724 if (strstr (c->line, "collect ") == c->line)
725 error (_("The 'collect' command can only be used for tracepoints"));
726
51661e93
VP
727 if (strstr (c->line, "teval ") == c->line)
728 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
729 }
730}
731
d77f58be
SS
732/* Encapsulate tests for different types of tracepoints. */
733
d9b3f62e
PA
734static int
735is_tracepoint_type (enum bptype type)
736{
737 return (type == bp_tracepoint
738 || type == bp_fast_tracepoint
739 || type == bp_static_tracepoint);
740}
741
a7bdde9e 742int
d77f58be 743is_tracepoint (const struct breakpoint *b)
a7bdde9e 744{
d9b3f62e 745 return is_tracepoint_type (b->type);
a7bdde9e 746}
d9b3f62e 747
e5dd4106 748/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
749 breakpoint. This function will throw an exception if a problem is
750 found. */
48cb2d85 751
95a42b64
TT
752static void
753validate_commands_for_breakpoint (struct breakpoint *b,
754 struct command_line *commands)
48cb2d85 755{
d77f58be 756 if (is_tracepoint (b))
a7bdde9e 757 {
4a64f543
MS
758 /* We need to verify that each top-level element of commands is
759 valid for tracepoints, that there's at most one
760 while-stepping element, and that while-stepping's body has
761 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
762 struct command_line *c;
763 struct command_line *while_stepping = 0;
764 for (c = commands; c; c = c->next)
765 {
a7bdde9e
VP
766 if (c->control_type == while_stepping_control)
767 {
768 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
769 error (_("The 'while-stepping' command "
770 "cannot be used for fast tracepoint"));
0fb4aa4b 771 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
772 error (_("The 'while-stepping' command "
773 "cannot be used for static tracepoint"));
a7bdde9e
VP
774
775 if (while_stepping)
3e43a32a
MS
776 error (_("The 'while-stepping' command "
777 "can be used only once"));
a7bdde9e
VP
778 else
779 while_stepping = c;
780 }
781 }
782 if (while_stepping)
783 {
784 struct command_line *c2;
785
786 gdb_assert (while_stepping->body_count == 1);
787 c2 = while_stepping->body_list[0];
788 for (; c2; c2 = c2->next)
789 {
a7bdde9e
VP
790 if (c2->control_type == while_stepping_control)
791 error (_("The 'while-stepping' command cannot be nested"));
792 }
793 }
794 }
795 else
796 {
797 check_no_tracepoint_commands (commands);
798 }
95a42b64
TT
799}
800
0fb4aa4b
PA
801/* Return a vector of all the static tracepoints set at ADDR. The
802 caller is responsible for releasing the vector. */
803
804VEC(breakpoint_p) *
805static_tracepoints_here (CORE_ADDR addr)
806{
807 struct breakpoint *b;
808 VEC(breakpoint_p) *found = 0;
809 struct bp_location *loc;
810
811 ALL_BREAKPOINTS (b)
812 if (b->type == bp_static_tracepoint)
813 {
814 for (loc = b->loc; loc; loc = loc->next)
815 if (loc->address == addr)
816 VEC_safe_push(breakpoint_p, found, b);
817 }
818
819 return found;
820}
821
95a42b64 822/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 823 validate that only allowed commands are included. */
95a42b64
TT
824
825void
4a64f543
MS
826breakpoint_set_commands (struct breakpoint *b,
827 struct command_line *commands)
95a42b64
TT
828{
829 validate_commands_for_breakpoint (b, commands);
a7bdde9e 830
9add0f1b
TT
831 decref_counted_command_line (&b->commands);
832 b->commands = alloc_counted_command_line (commands);
48cb2d85 833 breakpoints_changed ();
8d3788bd 834 observer_notify_breakpoint_modified (b);
48cb2d85
VP
835}
836
45a43567
TT
837/* Set the internal `silent' flag on the breakpoint. Note that this
838 is not the same as the "silent" that may appear in the breakpoint's
839 commands. */
840
841void
842breakpoint_set_silent (struct breakpoint *b, int silent)
843{
844 int old_silent = b->silent;
845
846 b->silent = silent;
847 if (old_silent != silent)
8d3788bd 848 observer_notify_breakpoint_modified (b);
45a43567
TT
849}
850
851/* Set the thread for this breakpoint. If THREAD is -1, make the
852 breakpoint work for any thread. */
853
854void
855breakpoint_set_thread (struct breakpoint *b, int thread)
856{
857 int old_thread = b->thread;
858
859 b->thread = thread;
860 if (old_thread != thread)
8d3788bd 861 observer_notify_breakpoint_modified (b);
45a43567
TT
862}
863
864/* Set the task for this breakpoint. If TASK is 0, make the
865 breakpoint work for any task. */
866
867void
868breakpoint_set_task (struct breakpoint *b, int task)
869{
870 int old_task = b->task;
871
872 b->task = task;
873 if (old_task != task)
8d3788bd 874 observer_notify_breakpoint_modified (b);
45a43567
TT
875}
876
95a42b64
TT
877void
878check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
879{
880 struct breakpoint *b = closure;
cc59ec59 881
a7bdde9e
VP
882 validate_actionline (&line, b);
883}
884
95a42b64
TT
885/* A structure used to pass information through
886 map_breakpoint_numbers. */
887
888struct commands_info
889{
890 /* True if the command was typed at a tty. */
891 int from_tty;
86b17b60
PA
892
893 /* The breakpoint range spec. */
894 char *arg;
895
95a42b64
TT
896 /* Non-NULL if the body of the commands are being read from this
897 already-parsed command. */
898 struct command_line *control;
86b17b60 899
95a42b64
TT
900 /* The command lines read from the user, or NULL if they have not
901 yet been read. */
902 struct counted_command_line *cmd;
903};
904
905/* A callback for map_breakpoint_numbers that sets the commands for
906 commands_command. */
907
c906108c 908static void
95a42b64 909do_map_commands_command (struct breakpoint *b, void *data)
c906108c 910{
95a42b64 911 struct commands_info *info = data;
c906108c 912
95a42b64
TT
913 if (info->cmd == NULL)
914 {
915 struct command_line *l;
5c44784c 916
95a42b64
TT
917 if (info->control != NULL)
918 l = copy_command_lines (info->control->body_list[0]);
919 else
86b17b60
PA
920 {
921 struct cleanup *old_chain;
922 char *str;
c5aa993b 923
3e43a32a
MS
924 str = xstrprintf (_("Type commands for breakpoint(s) "
925 "%s, one per line."),
86b17b60
PA
926 info->arg);
927
928 old_chain = make_cleanup (xfree, str);
929
930 l = read_command_lines (str,
931 info->from_tty, 1,
d77f58be 932 (is_tracepoint (b)
86b17b60
PA
933 ? check_tracepoint_command : 0),
934 b);
935
936 do_cleanups (old_chain);
937 }
a7bdde9e 938
95a42b64
TT
939 info->cmd = alloc_counted_command_line (l);
940 }
941
942 /* If a breakpoint was on the list more than once, we don't need to
943 do anything. */
944 if (b->commands != info->cmd)
945 {
946 validate_commands_for_breakpoint (b, info->cmd->commands);
947 incref_counted_command_line (info->cmd);
948 decref_counted_command_line (&b->commands);
949 b->commands = info->cmd;
950 breakpoints_changed ();
8d3788bd 951 observer_notify_breakpoint_modified (b);
c5aa993b 952 }
95a42b64
TT
953}
954
955static void
4a64f543
MS
956commands_command_1 (char *arg, int from_tty,
957 struct command_line *control)
95a42b64
TT
958{
959 struct cleanup *cleanups;
960 struct commands_info info;
961
962 info.from_tty = from_tty;
963 info.control = control;
964 info.cmd = NULL;
965 /* If we read command lines from the user, then `info' will hold an
966 extra reference to the commands that we must clean up. */
967 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
968
969 if (arg == NULL || !*arg)
970 {
86b17b60 971 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
972 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
973 breakpoint_count);
95a42b64
TT
974 else if (breakpoint_count > 0)
975 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
976 else
977 {
978 /* So that we don't try to free the incoming non-NULL
979 argument in the cleanup below. Mapping breakpoint
980 numbers will fail in this case. */
981 arg = NULL;
982 }
95a42b64 983 }
9766ced4
SS
984 else
985 /* The command loop has some static state, so we need to preserve
986 our argument. */
987 arg = xstrdup (arg);
86b17b60
PA
988
989 if (arg != NULL)
990 make_cleanup (xfree, arg);
991
992 info.arg = arg;
95a42b64
TT
993
994 map_breakpoint_numbers (arg, do_map_commands_command, &info);
995
996 if (info.cmd == NULL)
997 error (_("No breakpoints specified."));
998
999 do_cleanups (cleanups);
1000}
1001
1002static void
1003commands_command (char *arg, int from_tty)
1004{
1005 commands_command_1 (arg, from_tty, NULL);
c906108c 1006}
40c03ae8
EZ
1007
1008/* Like commands_command, but instead of reading the commands from
1009 input stream, takes them from an already parsed command structure.
1010
1011 This is used by cli-script.c to DTRT with breakpoint commands
1012 that are part of if and while bodies. */
1013enum command_control_type
1014commands_from_control_command (char *arg, struct command_line *cmd)
1015{
95a42b64
TT
1016 commands_command_1 (arg, 0, cmd);
1017 return simple_control;
40c03ae8 1018}
876fa593
JK
1019
1020/* Return non-zero if BL->TARGET_INFO contains valid information. */
1021
1022static int
1023bp_location_has_shadow (struct bp_location *bl)
1024{
1025 if (bl->loc_type != bp_loc_software_breakpoint)
1026 return 0;
1027 if (!bl->inserted)
1028 return 0;
1029 if (bl->target_info.shadow_len == 0)
e5dd4106 1030 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1031 return 0;
1032 return 1;
1033}
1034
8defab1a 1035/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1036 by replacing any memory breakpoints with their shadowed contents.
1037
1038 The range of shadowed area by each bp_location is:
35df4500
TJB
1039 bl->address - bp_location_placed_address_before_address_max
1040 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1041 The range we were requested to resolve shadows for is:
1042 memaddr ... memaddr + len
1043 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1044 memaddr + len <= (bl->address
1045 - bp_location_placed_address_before_address_max)
876fa593 1046 and:
35df4500 1047 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1048
8defab1a 1049void
f0ba3972
PA
1050breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1051 const gdb_byte *writebuf_org,
1052 ULONGEST memaddr, LONGEST len)
c906108c 1053{
4a64f543
MS
1054 /* Left boundary, right boundary and median element of our binary
1055 search. */
876fa593
JK
1056 unsigned bc_l, bc_r, bc;
1057
4a64f543
MS
1058 /* Find BC_L which is a leftmost element which may affect BUF
1059 content. It is safe to report lower value but a failure to
1060 report higher one. */
876fa593
JK
1061
1062 bc_l = 0;
1063 bc_r = bp_location_count;
1064 while (bc_l + 1 < bc_r)
1065 {
35df4500 1066 struct bp_location *bl;
876fa593
JK
1067
1068 bc = (bc_l + bc_r) / 2;
35df4500 1069 bl = bp_location[bc];
876fa593 1070
4a64f543
MS
1071 /* Check first BL->ADDRESS will not overflow due to the added
1072 constant. Then advance the left boundary only if we are sure
1073 the BC element can in no way affect the BUF content (MEMADDR
1074 to MEMADDR + LEN range).
876fa593 1075
4a64f543
MS
1076 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1077 offset so that we cannot miss a breakpoint with its shadow
1078 range tail still reaching MEMADDR. */
c5aa993b 1079
35df4500
TJB
1080 if ((bl->address + bp_location_shadow_len_after_address_max
1081 >= bl->address)
1082 && (bl->address + bp_location_shadow_len_after_address_max
1083 <= memaddr))
876fa593
JK
1084 bc_l = bc;
1085 else
1086 bc_r = bc;
1087 }
1088
128070bb
PA
1089 /* Due to the binary search above, we need to make sure we pick the
1090 first location that's at BC_L's address. E.g., if there are
1091 multiple locations at the same address, BC_L may end up pointing
1092 at a duplicate location, and miss the "master"/"inserted"
1093 location. Say, given locations L1, L2 and L3 at addresses A and
1094 B:
1095
1096 L1@A, L2@A, L3@B, ...
1097
1098 BC_L could end up pointing at location L2, while the "master"
1099 location could be L1. Since the `loc->inserted' flag is only set
1100 on "master" locations, we'd forget to restore the shadow of L1
1101 and L2. */
1102 while (bc_l > 0
1103 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1104 bc_l--;
1105
876fa593
JK
1106 /* Now do full processing of the found relevant range of elements. */
1107
1108 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1109 {
35df4500 1110 struct bp_location *bl = bp_location[bc];
876fa593
JK
1111 CORE_ADDR bp_addr = 0;
1112 int bp_size = 0;
1113 int bptoffset = 0;
1114
35df4500
TJB
1115 /* bp_location array has BL->OWNER always non-NULL. */
1116 if (bl->owner->type == bp_none)
8a3fe4f8 1117 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1118 bl->owner->number);
ffce0d52 1119
e5dd4106 1120 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1121 content. */
1122
35df4500
TJB
1123 if (bl->address >= bp_location_placed_address_before_address_max
1124 && memaddr + len <= (bl->address
1125 - bp_location_placed_address_before_address_max))
876fa593
JK
1126 break;
1127
35df4500 1128 if (!bp_location_has_shadow (bl))
c5aa993b 1129 continue;
35df4500 1130 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1131 current_program_space->aspace, 0))
1132 continue;
1133
c5aa993b
JM
1134 /* Addresses and length of the part of the breakpoint that
1135 we need to copy. */
35df4500
TJB
1136 bp_addr = bl->target_info.placed_address;
1137 bp_size = bl->target_info.shadow_len;
8defab1a 1138
c5aa993b
JM
1139 if (bp_addr + bp_size <= memaddr)
1140 /* The breakpoint is entirely before the chunk of memory we
1141 are reading. */
1142 continue;
8defab1a 1143
c5aa993b
JM
1144 if (bp_addr >= memaddr + len)
1145 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1146 reading. */
c5aa993b 1147 continue;
c5aa993b 1148
8defab1a
DJ
1149 /* Offset within shadow_contents. */
1150 if (bp_addr < memaddr)
1151 {
1152 /* Only copy the second part of the breakpoint. */
1153 bp_size -= memaddr - bp_addr;
1154 bptoffset = memaddr - bp_addr;
1155 bp_addr = memaddr;
1156 }
c5aa993b 1157
8defab1a
DJ
1158 if (bp_addr + bp_size > memaddr + len)
1159 {
1160 /* Only copy the first part of the breakpoint. */
1161 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1162 }
c5aa993b 1163
f0ba3972
PA
1164 if (readbuf != NULL)
1165 {
1166 /* Update the read buffer with this inserted breakpoint's
1167 shadow. */
1168 memcpy (readbuf + bp_addr - memaddr,
1169 bl->target_info.shadow_contents + bptoffset, bp_size);
1170 }
1171 else
1172 {
1173 struct gdbarch *gdbarch = bl->gdbarch;
1174 const unsigned char *bp;
1175 CORE_ADDR placed_address = bl->target_info.placed_address;
1176 unsigned placed_size = bl->target_info.placed_size;
1177
1178 /* Update the shadow with what we want to write to memory. */
1179 memcpy (bl->target_info.shadow_contents + bptoffset,
1180 writebuf_org + bp_addr - memaddr, bp_size);
1181
1182 /* Determine appropriate breakpoint contents and size for this
1183 address. */
1184 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1185
1186 /* Update the final write buffer with this inserted
1187 breakpoint's INSN. */
1188 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1189 }
c5aa993b 1190 }
c906108c 1191}
c906108c 1192\f
c5aa993b 1193
60e1c644
PA
1194/* Return true if BPT is of any hardware watchpoint kind. */
1195
a5606eee 1196static int
d77f58be 1197is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1198{
1199 return (bpt->type == bp_hardware_watchpoint
1200 || bpt->type == bp_read_watchpoint
1201 || bpt->type == bp_access_watchpoint);
1202}
7270d8f2 1203
60e1c644
PA
1204/* Return true if BPT is of any watchpoint kind, hardware or
1205 software. */
1206
3a5c3e22 1207int
d77f58be 1208is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1209{
1210 return (is_hardware_watchpoint (bpt)
1211 || bpt->type == bp_watchpoint);
1212}
1213
3a5c3e22
PA
1214/* Returns true if the current thread and its running state are safe
1215 to evaluate or update watchpoint B. Watchpoints on local
1216 expressions need to be evaluated in the context of the thread that
1217 was current when the watchpoint was created, and, that thread needs
1218 to be stopped to be able to select the correct frame context.
1219 Watchpoints on global expressions can be evaluated on any thread,
1220 and in any state. It is presently left to the target allowing
1221 memory accesses when threads are running. */
f6bc2008
PA
1222
1223static int
3a5c3e22 1224watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008
PA
1225{
1226 return (ptid_equal (b->watchpoint_thread, null_ptid)
1227 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1228 && !is_executing (inferior_ptid)));
1229}
1230
d0fb5eae
JK
1231/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1232 associated bp_watchpoint_scope breakpoint. */
1233
1234static void
3a5c3e22 1235watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1236{
3a5c3e22 1237 struct breakpoint *b = &w->base;
d0fb5eae
JK
1238
1239 if (b->related_breakpoint != b)
1240 {
1241 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1242 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1243 b->related_breakpoint->disposition = disp_del_at_next_stop;
1244 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1245 b->related_breakpoint = b;
1246 }
1247 b->disposition = disp_del_at_next_stop;
1248}
1249
567e1b4e
JB
1250/* Assuming that B is a watchpoint:
1251 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1252 - Evaluate expression and store the result in B->val
567e1b4e
JB
1253 - Evaluate the condition if there is one, and store the result
1254 in b->loc->cond.
a5606eee
VP
1255 - Update the list of values that must be watched in B->loc.
1256
4a64f543
MS
1257 If the watchpoint disposition is disp_del_at_next_stop, then do
1258 nothing. If this is local watchpoint that is out of scope, delete
1259 it.
1260
1261 Even with `set breakpoint always-inserted on' the watchpoints are
1262 removed + inserted on each stop here. Normal breakpoints must
1263 never be removed because they might be missed by a running thread
1264 when debugging in non-stop mode. On the other hand, hardware
1265 watchpoints (is_hardware_watchpoint; processed here) are specific
1266 to each LWP since they are stored in each LWP's hardware debug
1267 registers. Therefore, such LWP must be stopped first in order to
1268 be able to modify its hardware watchpoints.
1269
1270 Hardware watchpoints must be reset exactly once after being
1271 presented to the user. It cannot be done sooner, because it would
1272 reset the data used to present the watchpoint hit to the user. And
1273 it must not be done later because it could display the same single
1274 watchpoint hit during multiple GDB stops. Note that the latter is
1275 relevant only to the hardware watchpoint types bp_read_watchpoint
1276 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1277 not user-visible - its hit is suppressed if the memory content has
1278 not changed.
1279
1280 The following constraints influence the location where we can reset
1281 hardware watchpoints:
1282
1283 * target_stopped_by_watchpoint and target_stopped_data_address are
1284 called several times when GDB stops.
1285
1286 [linux]
1287 * Multiple hardware watchpoints can be hit at the same time,
1288 causing GDB to stop. GDB only presents one hardware watchpoint
1289 hit at a time as the reason for stopping, and all the other hits
1290 are presented later, one after the other, each time the user
1291 requests the execution to be resumed. Execution is not resumed
1292 for the threads still having pending hit event stored in
1293 LWP_INFO->STATUS. While the watchpoint is already removed from
1294 the inferior on the first stop the thread hit event is kept being
1295 reported from its cached value by linux_nat_stopped_data_address
1296 until the real thread resume happens after the watchpoint gets
1297 presented and thus its LWP_INFO->STATUS gets reset.
1298
1299 Therefore the hardware watchpoint hit can get safely reset on the
1300 watchpoint removal from inferior. */
a79d3c27 1301
b40ce68a 1302static void
3a5c3e22 1303update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1304{
a5606eee 1305 int within_current_scope;
a5606eee 1306 struct frame_id saved_frame_id;
66076460 1307 int frame_saved;
a5606eee 1308
f6bc2008
PA
1309 /* If this is a local watchpoint, we only want to check if the
1310 watchpoint frame is in scope if the current thread is the thread
1311 that was used to create the watchpoint. */
1312 if (!watchpoint_in_thread_scope (b))
1313 return;
1314
3a5c3e22 1315 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1316 return;
1317
66076460 1318 frame_saved = 0;
a5606eee
VP
1319
1320 /* Determine if the watchpoint is within scope. */
1321 if (b->exp_valid_block == NULL)
1322 within_current_scope = 1;
1323 else
1324 {
b5db5dfc
UW
1325 struct frame_info *fi = get_current_frame ();
1326 struct gdbarch *frame_arch = get_frame_arch (fi);
1327 CORE_ADDR frame_pc = get_frame_pc (fi);
1328
1329 /* If we're in a function epilogue, unwinding may not work
1330 properly, so do not attempt to recreate locations at this
1331 point. See similar comments in watchpoint_check. */
1332 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1333 return;
66076460
DJ
1334
1335 /* Save the current frame's ID so we can restore it after
1336 evaluating the watchpoint expression on its own frame. */
1337 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1338 took a frame parameter, so that we didn't have to change the
1339 selected frame. */
1340 frame_saved = 1;
1341 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1342
a5606eee
VP
1343 fi = frame_find_by_id (b->watchpoint_frame);
1344 within_current_scope = (fi != NULL);
1345 if (within_current_scope)
1346 select_frame (fi);
1347 }
1348
b5db5dfc
UW
1349 /* We don't free locations. They are stored in the bp_location array
1350 and update_global_location_list will eventually delete them and
1351 remove breakpoints if needed. */
3a5c3e22 1352 b->base.loc = NULL;
b5db5dfc 1353
a5606eee
VP
1354 if (within_current_scope && reparse)
1355 {
1356 char *s;
d63d0675 1357
a5606eee
VP
1358 if (b->exp)
1359 {
1360 xfree (b->exp);
1361 b->exp = NULL;
1362 }
d63d0675 1363 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1364 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1365 /* If the meaning of expression itself changed, the old value is
1366 no longer relevant. We don't want to report a watchpoint hit
1367 to the user when the old value and the new value may actually
1368 be completely different objects. */
1369 value_free (b->val);
fa4727a6
DJ
1370 b->val = NULL;
1371 b->val_valid = 0;
60e1c644
PA
1372
1373 /* Note that unlike with breakpoints, the watchpoint's condition
1374 expression is stored in the breakpoint object, not in the
1375 locations (re)created below. */
3a5c3e22 1376 if (b->base.cond_string != NULL)
60e1c644
PA
1377 {
1378 if (b->cond_exp != NULL)
1379 {
1380 xfree (b->cond_exp);
1381 b->cond_exp = NULL;
1382 }
1383
3a5c3e22 1384 s = b->base.cond_string;
60e1c644
PA
1385 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1386 }
a5606eee 1387 }
a5606eee
VP
1388
1389 /* If we failed to parse the expression, for example because
1390 it refers to a global variable in a not-yet-loaded shared library,
1391 don't try to insert watchpoint. We don't automatically delete
1392 such watchpoint, though, since failure to parse expression
1393 is different from out-of-scope watchpoint. */
2d134ed3
PA
1394 if ( !target_has_execution)
1395 {
1396 /* Without execution, memory can't change. No use to try and
1397 set watchpoint locations. The watchpoint will be reset when
1398 the target gains execution, through breakpoint_re_set. */
1399 }
1400 else if (within_current_scope && b->exp)
a5606eee 1401 {
0cf6dd15 1402 int pc = 0;
fa4727a6 1403 struct value *val_chain, *v, *result, *next;
2d134ed3 1404 struct program_space *frame_pspace;
a5606eee 1405
0cf6dd15 1406 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1407
a5606eee
VP
1408 /* Avoid setting b->val if it's already set. The meaning of
1409 b->val is 'the last value' user saw, and we should update
1410 it only if we reported that last value to user. As it
9c06b0b4
TJB
1411 happens, the code that reports it updates b->val directly.
1412 We don't keep track of the memory value for masked
1413 watchpoints. */
3a5c3e22 1414 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1415 {
1416 b->val = v;
1417 b->val_valid = 1;
1418 }
a5606eee 1419
2d134ed3
PA
1420 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1421
a5606eee 1422 /* Look at each value on the value chain. */
9fa40276 1423 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1424 {
1425 /* If it's a memory location, and GDB actually needed
1426 its contents to evaluate the expression, then we
fa4727a6
DJ
1427 must watch it. If the first value returned is
1428 still lazy, that means an error occurred reading it;
1429 watch it anyway in case it becomes readable. */
a5606eee 1430 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1431 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1432 {
1433 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1434
a5606eee
VP
1435 /* We only watch structs and arrays if user asked
1436 for it explicitly, never if they just happen to
1437 appear in the middle of some value chain. */
fa4727a6 1438 if (v == result
a5606eee
VP
1439 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1440 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1441 {
1442 CORE_ADDR addr;
1443 int len, type;
1444 struct bp_location *loc, **tmp;
1445
42ae5230 1446 addr = value_address (v);
a5606eee
VP
1447 len = TYPE_LENGTH (value_type (v));
1448 type = hw_write;
3a5c3e22 1449 if (b->base.type == bp_read_watchpoint)
a5606eee 1450 type = hw_read;
3a5c3e22 1451 else if (b->base.type == bp_access_watchpoint)
a5606eee 1452 type = hw_access;
3a5c3e22
PA
1453
1454 loc = allocate_bp_location (&b->base);
1455 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1456 ;
1457 *tmp = loc;
a6d9a66e 1458 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1459
1460 loc->pspace = frame_pspace;
a5606eee
VP
1461 loc->address = addr;
1462 loc->length = len;
1463 loc->watchpoint_type = type;
1464 }
1465 }
9fa40276
TJB
1466 }
1467
1468 /* Change the type of breakpoint between hardware assisted or
1469 an ordinary watchpoint depending on the hardware support
1470 and free hardware slots. REPARSE is set when the inferior
1471 is started. */
a9634178 1472 if (reparse)
9fa40276 1473 {
e09342b5 1474 int reg_cnt;
9fa40276
TJB
1475 enum bp_loc_type loc_type;
1476 struct bp_location *bl;
a5606eee 1477
a9634178 1478 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1479
1480 if (reg_cnt)
9fa40276
TJB
1481 {
1482 int i, target_resources_ok, other_type_used;
a1398e0c 1483 enum bptype type;
9fa40276 1484
a9634178
TJB
1485 /* Use an exact watchpoint when there's only one memory region to be
1486 watched, and only one debug register is needed to watch it. */
1487 b->exact = target_exact_watchpoints && reg_cnt == 1;
1488
9fa40276 1489 /* We need to determine how many resources are already
e09342b5
TJB
1490 used for all other hardware watchpoints plus this one
1491 to see if we still have enough resources to also fit
a1398e0c
PA
1492 this watchpoint in as well. */
1493
1494 /* If this is a software watchpoint, we try to turn it
1495 to a hardware one -- count resources as if B was of
1496 hardware watchpoint type. */
1497 type = b->base.type;
1498 if (type == bp_watchpoint)
1499 type = bp_hardware_watchpoint;
1500
1501 /* This watchpoint may or may not have been placed on
1502 the list yet at this point (it won't be in the list
1503 if we're trying to create it for the first time,
1504 through watch_command), so always account for it
1505 manually. */
1506
1507 /* Count resources used by all watchpoints except B. */
1508 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1509
1510 /* Add in the resources needed for B. */
1511 i += hw_watchpoint_use_count (&b->base);
1512
1513 target_resources_ok
1514 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1515 if (target_resources_ok <= 0)
a9634178 1516 {
3a5c3e22 1517 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1518
1519 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1520 error (_("Target does not support this type of "
1521 "hardware watchpoint."));
9c06b0b4
TJB
1522 else if (target_resources_ok < 0 && !sw_mode)
1523 error (_("There are not enough available hardware "
1524 "resources for this watchpoint."));
a1398e0c
PA
1525
1526 /* Downgrade to software watchpoint. */
1527 b->base.type = bp_watchpoint;
1528 }
1529 else
1530 {
1531 /* If this was a software watchpoint, we've just
1532 found we have enough resources to turn it to a
1533 hardware watchpoint. Otherwise, this is a
1534 nop. */
1535 b->base.type = type;
a9634178 1536 }
9fa40276 1537 }
3a5c3e22 1538 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1539 error (_("Expression cannot be implemented with "
1540 "read/access watchpoint."));
9fa40276 1541 else
3a5c3e22 1542 b->base.type = bp_watchpoint;
9fa40276 1543
3a5c3e22 1544 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1545 : bp_loc_hardware_watchpoint);
3a5c3e22 1546 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1547 bl->loc_type = loc_type;
1548 }
1549
1550 for (v = val_chain; v; v = next)
1551 {
a5606eee
VP
1552 next = value_next (v);
1553 if (v != b->val)
1554 value_free (v);
1555 }
1556
c7437ca6
PA
1557 /* If a software watchpoint is not watching any memory, then the
1558 above left it without any location set up. But,
1559 bpstat_stop_status requires a location to be able to report
1560 stops, so make sure there's at least a dummy one. */
3a5c3e22 1561 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1562 {
3a5c3e22
PA
1563 struct breakpoint *base = &b->base;
1564 base->loc = allocate_bp_location (base);
1565 base->loc->pspace = frame_pspace;
1566 base->loc->address = -1;
1567 base->loc->length = -1;
1568 base->loc->watchpoint_type = -1;
c7437ca6 1569 }
a5606eee
VP
1570 }
1571 else if (!within_current_scope)
7270d8f2 1572 {
ac74f770
MS
1573 printf_filtered (_("\
1574Watchpoint %d deleted because the program has left the block\n\
1575in which its expression is valid.\n"),
3a5c3e22 1576 b->base.number);
d0fb5eae 1577 watchpoint_del_at_next_stop (b);
7270d8f2 1578 }
a5606eee
VP
1579
1580 /* Restore the selected frame. */
66076460
DJ
1581 if (frame_saved)
1582 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1583}
1584
a5606eee 1585
74960c60 1586/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1587 inserted in the inferior. We don't differentiate the type of BL's owner
1588 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1589 breakpoint_ops is not defined, because in insert_bp_location,
1590 tracepoint's insert_location will not be called. */
74960c60 1591static int
35df4500 1592should_be_inserted (struct bp_location *bl)
74960c60 1593{
35df4500 1594 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1595 return 0;
1596
35df4500 1597 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1598 return 0;
1599
35df4500 1600 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1601 return 0;
1602
f8eba3c6
TT
1603 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1604 return 0;
1605
56710373
PA
1606 /* This is set for example, when we're attached to the parent of a
1607 vfork, and have detached from the child. The child is running
1608 free, and we expect it to do an exec or exit, at which point the
1609 OS makes the parent schedulable again (and the target reports
1610 that the vfork is done). Until the child is done with the shared
1611 memory region, do not insert breakpoints in the parent, otherwise
1612 the child could still trip on the parent's breakpoints. Since
1613 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1614 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1615 return 0;
1616
74960c60
VP
1617 return 1;
1618}
1619
934709f0
PW
1620/* Same as should_be_inserted but does the check assuming
1621 that the location is not duplicated. */
1622
1623static int
1624unduplicated_should_be_inserted (struct bp_location *bl)
1625{
1626 int result;
1627 const int save_duplicate = bl->duplicate;
1628
1629 bl->duplicate = 0;
1630 result = should_be_inserted (bl);
1631 bl->duplicate = save_duplicate;
1632 return result;
1633}
1634
35df4500
TJB
1635/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1636 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 1637 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
1638 Returns 0 for success, 1 if the bp_location type is not supported or
1639 -1 for failure.
879bfdc2 1640
4a64f543
MS
1641 NOTE drow/2003-09-09: This routine could be broken down to an
1642 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1643static int
35df4500 1644insert_bp_location (struct bp_location *bl,
26bb91f3 1645 struct ui_file *tmp_error_stream,
3fbb6ffa 1646 int *disabled_breaks,
26bb91f3 1647 int *hw_breakpoint_error)
879bfdc2
DJ
1648{
1649 int val = 0;
1650
35df4500 1651 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1652 return 0;
1653
8181d85f 1654 /* Initialize the target-specific information. */
35df4500
TJB
1655 memset (&bl->target_info, 0, sizeof (bl->target_info));
1656 bl->target_info.placed_address = bl->address;
1657 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1658 bl->target_info.length = bl->length;
8181d85f 1659
35df4500
TJB
1660 if (bl->loc_type == bp_loc_software_breakpoint
1661 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1662 {
35df4500 1663 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1664 {
1665 /* If the explicitly specified breakpoint type
1666 is not hardware breakpoint, check the memory map to see
1667 if the breakpoint address is in read only memory or not.
4a64f543 1668
765dc015
VP
1669 Two important cases are:
1670 - location type is not hardware breakpoint, memory
1671 is readonly. We change the type of the location to
1672 hardware breakpoint.
4a64f543
MS
1673 - location type is hardware breakpoint, memory is
1674 read-write. This means we've previously made the
1675 location hardware one, but then the memory map changed,
1676 so we undo.
765dc015 1677
4a64f543
MS
1678 When breakpoints are removed, remove_breakpoints will use
1679 location types we've just set here, the only possible
1680 problem is that memory map has changed during running
1681 program, but it's not going to work anyway with current
1682 gdb. */
765dc015 1683 struct mem_region *mr
35df4500 1684 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1685
1686 if (mr)
1687 {
1688 if (automatic_hardware_breakpoints)
1689 {
765dc015
VP
1690 enum bp_loc_type new_type;
1691
1692 if (mr->attrib.mode != MEM_RW)
1693 new_type = bp_loc_hardware_breakpoint;
1694 else
1695 new_type = bp_loc_software_breakpoint;
1696
35df4500 1697 if (new_type != bl->loc_type)
765dc015
VP
1698 {
1699 static int said = 0;
cc59ec59 1700
35df4500 1701 bl->loc_type = new_type;
765dc015
VP
1702 if (!said)
1703 {
3e43a32a
MS
1704 fprintf_filtered (gdb_stdout,
1705 _("Note: automatically using "
1706 "hardware breakpoints for "
1707 "read-only addresses.\n"));
765dc015
VP
1708 said = 1;
1709 }
1710 }
1711 }
35df4500 1712 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1713 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1714 warning (_("cannot set software breakpoint "
1715 "at readonly address %s"),
35df4500 1716 paddress (bl->gdbarch, bl->address));
765dc015
VP
1717 }
1718 }
1719
879bfdc2
DJ
1720 /* First check to see if we have to handle an overlay. */
1721 if (overlay_debugging == ovly_off
35df4500
TJB
1722 || bl->section == NULL
1723 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1724 {
1725 /* No overlay handling: just set the breakpoint. */
1726
348d480f 1727 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1728 }
1729 else
1730 {
4a64f543 1731 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1732 Shall we set a breakpoint at the LMA? */
1733 if (!overlay_events_enabled)
1734 {
1735 /* Yes -- overlay event support is not active,
1736 so we must try to set a breakpoint at the LMA.
1737 This will not work for a hardware breakpoint. */
35df4500 1738 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1739 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1740 bl->owner->number);
879bfdc2
DJ
1741 else
1742 {
35df4500
TJB
1743 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1744 bl->section);
879bfdc2 1745 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1746 bl->overlay_target_info = bl->target_info;
1747 bl->overlay_target_info.placed_address = addr;
1748 val = target_insert_breakpoint (bl->gdbarch,
1749 &bl->overlay_target_info);
879bfdc2 1750 if (val != 0)
99361f52 1751 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1752 "Overlay breakpoint %d "
1753 "failed: in ROM?\n",
35df4500 1754 bl->owner->number);
879bfdc2
DJ
1755 }
1756 }
1757 /* Shall we set a breakpoint at the VMA? */
35df4500 1758 if (section_is_mapped (bl->section))
879bfdc2
DJ
1759 {
1760 /* Yes. This overlay section is mapped into memory. */
348d480f 1761 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1762 }
1763 else
1764 {
1765 /* No. This breakpoint will not be inserted.
1766 No error, but do not mark the bp as 'inserted'. */
1767 return 0;
1768 }
1769 }
1770
1771 if (val)
1772 {
1773 /* Can't set the breakpoint. */
35df4500 1774 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1775 {
4a64f543 1776 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1777 val = 0;
35df4500 1778 bl->shlib_disabled = 1;
8d3788bd 1779 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
1780 if (!*disabled_breaks)
1781 {
1782 fprintf_unfiltered (tmp_error_stream,
1783 "Cannot insert breakpoint %d.\n",
1784 bl->owner->number);
1785 fprintf_unfiltered (tmp_error_stream,
1786 "Temporarily disabling shared "
1787 "library breakpoints:\n");
1788 }
1789 *disabled_breaks = 1;
879bfdc2 1790 fprintf_unfiltered (tmp_error_stream,
35df4500 1791 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1792 }
1793 else
879bfdc2 1794 {
35df4500 1795 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1796 {
1797 *hw_breakpoint_error = 1;
3e43a32a
MS
1798 fprintf_unfiltered (tmp_error_stream,
1799 "Cannot insert hardware "
1800 "breakpoint %d.\n",
35df4500 1801 bl->owner->number);
879bfdc2
DJ
1802 }
1803 else
1804 {
1805 fprintf_unfiltered (tmp_error_stream,
1806 "Cannot insert breakpoint %d.\n",
35df4500 1807 bl->owner->number);
879bfdc2
DJ
1808 fprintf_filtered (tmp_error_stream,
1809 "Error accessing memory address ");
35df4500 1810 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1811 tmp_error_stream);
879bfdc2
DJ
1812 fprintf_filtered (tmp_error_stream, ": %s.\n",
1813 safe_strerror (val));
1814 }
1815
1816 }
1817 }
1818 else
35df4500 1819 bl->inserted = 1;
879bfdc2
DJ
1820
1821 return val;
1822 }
1823
35df4500 1824 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1825 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1826 watchpoints. It's not clear that it's necessary... */
35df4500 1827 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1828 {
77b06cd7
TJB
1829 gdb_assert (bl->owner->ops != NULL
1830 && bl->owner->ops->insert_location != NULL);
1831
1832 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1833
1834 /* If trying to set a read-watchpoint, and it turns out it's not
1835 supported, try emulating one with an access watchpoint. */
35df4500 1836 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1837 {
1838 struct bp_location *loc, **loc_temp;
1839
1840 /* But don't try to insert it, if there's already another
1841 hw_access location that would be considered a duplicate
1842 of this one. */
1843 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1844 if (loc != bl
85d721b8 1845 && loc->watchpoint_type == hw_access
35df4500 1846 && watchpoint_locations_match (bl, loc))
85d721b8 1847 {
35df4500
TJB
1848 bl->duplicate = 1;
1849 bl->inserted = 1;
1850 bl->target_info = loc->target_info;
1851 bl->watchpoint_type = hw_access;
85d721b8
PA
1852 val = 0;
1853 break;
1854 }
1855
1856 if (val == 1)
1857 {
77b06cd7
TJB
1858 bl->watchpoint_type = hw_access;
1859 val = bl->owner->ops->insert_location (bl);
1860
1861 if (val)
1862 /* Back to the original value. */
1863 bl->watchpoint_type = hw_read;
85d721b8
PA
1864 }
1865 }
1866
35df4500 1867 bl->inserted = (val == 0);
879bfdc2
DJ
1868 }
1869
35df4500 1870 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1871 {
77b06cd7
TJB
1872 gdb_assert (bl->owner->ops != NULL
1873 && bl->owner->ops->insert_location != NULL);
1874
1875 val = bl->owner->ops->insert_location (bl);
1876 if (val)
1877 {
1878 bl->owner->enable_state = bp_disabled;
1879
1880 if (val == 1)
1881 warning (_("\
1882Error inserting catchpoint %d: Your system does not support this type\n\
1883of catchpoint."), bl->owner->number);
1884 else
1885 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1886 }
1887
1888 bl->inserted = (val == 0);
1640b821
DJ
1889
1890 /* We've already printed an error message if there was a problem
1891 inserting this catchpoint, and we've disabled the catchpoint,
1892 so just return success. */
1893 return 0;
879bfdc2
DJ
1894 }
1895
1896 return 0;
1897}
1898
6c95b8df
PA
1899/* This function is called when program space PSPACE is about to be
1900 deleted. It takes care of updating breakpoints to not reference
1901 PSPACE anymore. */
1902
1903void
1904breakpoint_program_space_exit (struct program_space *pspace)
1905{
1906 struct breakpoint *b, *b_temp;
876fa593 1907 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1908
1909 /* Remove any breakpoint that was set through this program space. */
1910 ALL_BREAKPOINTS_SAFE (b, b_temp)
1911 {
1912 if (b->pspace == pspace)
1913 delete_breakpoint (b);
1914 }
1915
1916 /* Breakpoints set through other program spaces could have locations
1917 bound to PSPACE as well. Remove those. */
876fa593 1918 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1919 {
1920 struct bp_location *tmp;
1921
1922 if (loc->pspace == pspace)
1923 {
2bdf28a0 1924 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1925 if (loc->owner->loc == loc)
1926 loc->owner->loc = loc->next;
1927 else
1928 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1929 if (tmp->next == loc)
1930 {
1931 tmp->next = loc->next;
1932 break;
1933 }
1934 }
1935 }
1936
1937 /* Now update the global location list to permanently delete the
1938 removed locations above. */
1939 update_global_location_list (0);
1940}
1941
74960c60
VP
1942/* Make sure all breakpoints are inserted in inferior.
1943 Throws exception on any error.
1944 A breakpoint that is already inserted won't be inserted
1945 again, so calling this function twice is safe. */
1946void
1947insert_breakpoints (void)
1948{
1949 struct breakpoint *bpt;
1950
1951 ALL_BREAKPOINTS (bpt)
1952 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
1953 {
1954 struct watchpoint *w = (struct watchpoint *) bpt;
1955
1956 update_watchpoint (w, 0 /* don't reparse. */);
1957 }
74960c60 1958
b60e7edf 1959 update_global_location_list (1);
74960c60 1960
c35b1492
PA
1961 /* update_global_location_list does not insert breakpoints when
1962 always_inserted_mode is not enabled. Explicitly insert them
1963 now. */
1964 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1965 insert_breakpoint_locations ();
1966}
1967
c30eee59 1968/* Used when starting or continuing the program. */
c906108c 1969
74960c60
VP
1970static void
1971insert_breakpoint_locations (void)
c906108c 1972{
a5606eee 1973 struct breakpoint *bpt;
35df4500 1974 struct bp_location *bl, **blp_tmp;
eacd795a 1975 int error_flag = 0;
c906108c 1976 int val = 0;
3fbb6ffa 1977 int disabled_breaks = 0;
81d0cc19 1978 int hw_breakpoint_error = 0;
c906108c 1979
81d0cc19 1980 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1981 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1982
81d0cc19
GS
1983 /* Explicitly mark the warning -- this will only be printed if
1984 there was an error. */
1985 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1986
1987 save_current_space_and_thread ();
1988
35df4500 1989 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1990 {
35df4500 1991 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1992 continue;
1993
4a64f543
MS
1994 /* There is no point inserting thread-specific breakpoints if
1995 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1996 has BL->OWNER always non-NULL. */
35df4500
TJB
1997 if (bl->owner->thread != -1
1998 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1999 continue;
2000
35df4500 2001 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2002
2003 /* For targets that support global breakpoints, there's no need
2004 to select an inferior to insert breakpoint to. In fact, even
2005 if we aren't attached to any process yet, we should still
2006 insert breakpoints. */
2007 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2008 && ptid_equal (inferior_ptid, null_ptid))
2009 continue;
2010
3fbb6ffa
TJB
2011 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2012 &hw_breakpoint_error);
879bfdc2 2013 if (val)
eacd795a 2014 error_flag = val;
879bfdc2 2015 }
c906108c 2016
4a64f543
MS
2017 /* If we failed to insert all locations of a watchpoint, remove
2018 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2019 ALL_BREAKPOINTS (bpt)
2020 {
2021 int some_failed = 0;
2022 struct bp_location *loc;
2023
2024 if (!is_hardware_watchpoint (bpt))
2025 continue;
2026
d6b74ac4 2027 if (!breakpoint_enabled (bpt))
a5606eee 2028 continue;
74960c60
VP
2029
2030 if (bpt->disposition == disp_del_at_next_stop)
2031 continue;
a5606eee
VP
2032
2033 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2034 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2035 {
2036 some_failed = 1;
2037 break;
2038 }
2039 if (some_failed)
2040 {
2041 for (loc = bpt->loc; loc; loc = loc->next)
2042 if (loc->inserted)
2043 remove_breakpoint (loc, mark_uninserted);
2044
2045 hw_breakpoint_error = 1;
2046 fprintf_unfiltered (tmp_error_stream,
2047 "Could not insert hardware watchpoint %d.\n",
2048 bpt->number);
eacd795a 2049 error_flag = -1;
a5606eee
VP
2050 }
2051 }
2052
eacd795a 2053 if (error_flag)
81d0cc19
GS
2054 {
2055 /* If a hardware breakpoint or watchpoint was inserted, add a
2056 message about possibly exhausted resources. */
879bfdc2 2057 if (hw_breakpoint_error)
81d0cc19 2058 {
c6510018
MS
2059 fprintf_unfiltered (tmp_error_stream,
2060 "Could not insert hardware breakpoints:\n\
2061You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2062 }
81d0cc19
GS
2063 target_terminal_ours_for_output ();
2064 error_stream (tmp_error_stream);
2065 }
f7545552
TT
2066
2067 do_cleanups (cleanups);
c906108c
SS
2068}
2069
c30eee59
TJB
2070/* Used when the program stops.
2071 Returns zero if successful, or non-zero if there was a problem
2072 removing a breakpoint location. */
2073
c906108c 2074int
fba45db2 2075remove_breakpoints (void)
c906108c 2076{
35df4500 2077 struct bp_location *bl, **blp_tmp;
3a1bae8e 2078 int val = 0;
c906108c 2079
35df4500 2080 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2081 {
1e4d1764 2082 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2083 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2084 }
3a1bae8e 2085 return val;
c906108c
SS
2086}
2087
6c95b8df
PA
2088/* Remove breakpoints of process PID. */
2089
2090int
2091remove_breakpoints_pid (int pid)
2092{
35df4500 2093 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2094 int val;
2095 struct inferior *inf = find_inferior_pid (pid);
2096
35df4500 2097 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2098 {
35df4500 2099 if (bl->pspace != inf->pspace)
6c95b8df
PA
2100 continue;
2101
35df4500 2102 if (bl->inserted)
6c95b8df 2103 {
35df4500 2104 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2105 if (val != 0)
2106 return val;
2107 }
2108 }
2109 return 0;
2110}
2111
c906108c 2112int
fba45db2 2113reattach_breakpoints (int pid)
c906108c 2114{
6c95b8df 2115 struct cleanup *old_chain;
35df4500 2116 struct bp_location *bl, **blp_tmp;
c906108c 2117 int val;
86b887df 2118 struct ui_file *tmp_error_stream;
3fbb6ffa 2119 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2120 struct inferior *inf;
2121 struct thread_info *tp;
2122
2123 tp = any_live_thread_of_process (pid);
2124 if (tp == NULL)
2125 return 1;
2126
2127 inf = find_inferior_pid (pid);
2128 old_chain = save_inferior_ptid ();
2129
2130 inferior_ptid = tp->ptid;
a4954f26 2131
86b887df 2132 tmp_error_stream = mem_fileopen ();
a4954f26 2133 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2134
35df4500 2135 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2136 {
35df4500 2137 if (bl->pspace != inf->pspace)
6c95b8df
PA
2138 continue;
2139
35df4500 2140 if (bl->inserted)
c5aa993b 2141 {
35df4500 2142 bl->inserted = 0;
3fbb6ffa 2143 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2144 if (val != 0)
2145 {
ce696e05 2146 do_cleanups (old_chain);
c5aa993b
JM
2147 return val;
2148 }
2149 }
2150 }
ce696e05 2151 do_cleanups (old_chain);
c906108c
SS
2152 return 0;
2153}
2154
e58b0e63
PA
2155static int internal_breakpoint_number = -1;
2156
84f4c1fe
PM
2157/* Set the breakpoint number of B, depending on the value of INTERNAL.
2158 If INTERNAL is non-zero, the breakpoint number will be populated
2159 from internal_breakpoint_number and that variable decremented.
e5dd4106 2160 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2161 breakpoint_count and that value incremented. Internal breakpoints
2162 do not set the internal var bpnum. */
2163static void
2164set_breakpoint_number (int internal, struct breakpoint *b)
2165{
2166 if (internal)
2167 b->number = internal_breakpoint_number--;
2168 else
2169 {
2170 set_breakpoint_count (breakpoint_count + 1);
2171 b->number = breakpoint_count;
2172 }
2173}
2174
e62c965a 2175static struct breakpoint *
a6d9a66e 2176create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2177 CORE_ADDR address, enum bptype type,
c0a91b2b 2178 const struct breakpoint_ops *ops)
e62c965a 2179{
e62c965a
PP
2180 struct symtab_and_line sal;
2181 struct breakpoint *b;
2182
4a64f543 2183 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2184
2185 sal.pc = address;
2186 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2187 sal.pspace = current_program_space;
e62c965a 2188
06edf0c0 2189 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2190 b->number = internal_breakpoint_number--;
2191 b->disposition = disp_donttouch;
2192
2193 return b;
2194}
2195
17450429
PP
2196static const char *const longjmp_names[] =
2197 {
2198 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2199 };
2200#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2201
2202/* Per-objfile data private to breakpoint.c. */
2203struct breakpoint_objfile_data
2204{
2205 /* Minimal symbol for "_ovly_debug_event" (if any). */
2206 struct minimal_symbol *overlay_msym;
2207
2208 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2209 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2210
2211 /* Minimal symbol for "std::terminate()" (if any). */
2212 struct minimal_symbol *terminate_msym;
2213
2214 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2215 struct minimal_symbol *exception_msym;
2216};
2217
2218static const struct objfile_data *breakpoint_objfile_key;
2219
2220/* Minimal symbol not found sentinel. */
2221static struct minimal_symbol msym_not_found;
2222
2223/* Returns TRUE if MSYM point to the "not found" sentinel. */
2224
2225static int
2226msym_not_found_p (const struct minimal_symbol *msym)
2227{
2228 return msym == &msym_not_found;
2229}
2230
2231/* Return per-objfile data needed by breakpoint.c.
2232 Allocate the data if necessary. */
2233
2234static struct breakpoint_objfile_data *
2235get_breakpoint_objfile_data (struct objfile *objfile)
2236{
2237 struct breakpoint_objfile_data *bp_objfile_data;
2238
2239 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2240 if (bp_objfile_data == NULL)
2241 {
2242 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2243 sizeof (*bp_objfile_data));
2244
2245 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2246 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2247 }
2248 return bp_objfile_data;
2249}
2250
e62c965a 2251static void
af02033e 2252create_overlay_event_breakpoint (void)
e62c965a 2253{
69de3c6a 2254 struct objfile *objfile;
af02033e 2255 const char *const func_name = "_ovly_debug_event";
e62c965a 2256
69de3c6a
PP
2257 ALL_OBJFILES (objfile)
2258 {
2259 struct breakpoint *b;
17450429
PP
2260 struct breakpoint_objfile_data *bp_objfile_data;
2261 CORE_ADDR addr;
69de3c6a 2262
17450429
PP
2263 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2264
2265 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2266 continue;
2267
2268 if (bp_objfile_data->overlay_msym == NULL)
2269 {
2270 struct minimal_symbol *m;
2271
2272 m = lookup_minimal_symbol_text (func_name, objfile);
2273 if (m == NULL)
2274 {
2275 /* Avoid future lookups in this objfile. */
2276 bp_objfile_data->overlay_msym = &msym_not_found;
2277 continue;
2278 }
2279 bp_objfile_data->overlay_msym = m;
2280 }
e62c965a 2281
17450429
PP
2282 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2283 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2284 bp_overlay_event,
2285 &internal_breakpoint_ops);
69de3c6a 2286 b->addr_string = xstrdup (func_name);
e62c965a 2287
69de3c6a
PP
2288 if (overlay_debugging == ovly_auto)
2289 {
2290 b->enable_state = bp_enabled;
2291 overlay_events_enabled = 1;
2292 }
2293 else
2294 {
2295 b->enable_state = bp_disabled;
2296 overlay_events_enabled = 0;
2297 }
e62c965a
PP
2298 }
2299 update_global_location_list (1);
2300}
2301
0fd8e87f 2302static void
af02033e 2303create_longjmp_master_breakpoint (void)
0fd8e87f 2304{
6c95b8df 2305 struct program_space *pspace;
6c95b8df
PA
2306 struct cleanup *old_chain;
2307
2308 old_chain = save_current_program_space ();
0fd8e87f 2309
6c95b8df 2310 ALL_PSPACES (pspace)
af02033e
PP
2311 {
2312 struct objfile *objfile;
2313
2314 set_current_program_space (pspace);
2315
2316 ALL_OBJFILES (objfile)
0fd8e87f 2317 {
af02033e
PP
2318 int i;
2319 struct gdbarch *gdbarch;
17450429 2320 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2321
af02033e
PP
2322 gdbarch = get_objfile_arch (objfile);
2323 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2324 continue;
2325
17450429
PP
2326 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2327
2328 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2329 {
2330 struct breakpoint *b;
af02033e 2331 const char *func_name;
17450429 2332 CORE_ADDR addr;
6c95b8df 2333
17450429 2334 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2335 continue;
0fd8e87f 2336
17450429
PP
2337 func_name = longjmp_names[i];
2338 if (bp_objfile_data->longjmp_msym[i] == NULL)
2339 {
2340 struct minimal_symbol *m;
2341
2342 m = lookup_minimal_symbol_text (func_name, objfile);
2343 if (m == NULL)
2344 {
2345 /* Prevent future lookups in this objfile. */
2346 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2347 continue;
2348 }
2349 bp_objfile_data->longjmp_msym[i] = m;
2350 }
2351
2352 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2353 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2354 &internal_breakpoint_ops);
af02033e
PP
2355 b->addr_string = xstrdup (func_name);
2356 b->enable_state = bp_disabled;
2357 }
0fd8e87f 2358 }
af02033e 2359 }
0fd8e87f 2360 update_global_location_list (1);
6c95b8df
PA
2361
2362 do_cleanups (old_chain);
0fd8e87f
UW
2363}
2364
af02033e 2365/* Create a master std::terminate breakpoint. */
aa7d318d 2366static void
af02033e 2367create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2368{
2369 struct program_space *pspace;
aa7d318d 2370 struct cleanup *old_chain;
af02033e 2371 const char *const func_name = "std::terminate()";
aa7d318d
TT
2372
2373 old_chain = save_current_program_space ();
2374
2375 ALL_PSPACES (pspace)
17450429
PP
2376 {
2377 struct objfile *objfile;
2378 CORE_ADDR addr;
2379
2380 set_current_program_space (pspace);
2381
aa7d318d
TT
2382 ALL_OBJFILES (objfile)
2383 {
2384 struct breakpoint *b;
17450429 2385 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2386
17450429 2387 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2388
17450429
PP
2389 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2390 continue;
2391
2392 if (bp_objfile_data->terminate_msym == NULL)
2393 {
2394 struct minimal_symbol *m;
2395
2396 m = lookup_minimal_symbol (func_name, NULL, objfile);
2397 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2398 && MSYMBOL_TYPE (m) != mst_file_text))
2399 {
2400 /* Prevent future lookups in this objfile. */
2401 bp_objfile_data->terminate_msym = &msym_not_found;
2402 continue;
2403 }
2404 bp_objfile_data->terminate_msym = m;
2405 }
aa7d318d 2406
17450429
PP
2407 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2408 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2409 bp_std_terminate_master,
2410 &internal_breakpoint_ops);
aa7d318d
TT
2411 b->addr_string = xstrdup (func_name);
2412 b->enable_state = bp_disabled;
2413 }
17450429
PP
2414 }
2415
aa7d318d
TT
2416 update_global_location_list (1);
2417
2418 do_cleanups (old_chain);
2419}
2420
186c406b
TT
2421/* Install a master breakpoint on the unwinder's debug hook. */
2422
2423void
2424create_exception_master_breakpoint (void)
2425{
2426 struct objfile *objfile;
17450429 2427 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2428
2429 ALL_OBJFILES (objfile)
2430 {
17450429
PP
2431 struct breakpoint *b;
2432 struct gdbarch *gdbarch;
2433 struct breakpoint_objfile_data *bp_objfile_data;
2434 CORE_ADDR addr;
2435
2436 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2437
2438 if (msym_not_found_p (bp_objfile_data->exception_msym))
2439 continue;
2440
2441 gdbarch = get_objfile_arch (objfile);
186c406b 2442
17450429 2443 if (bp_objfile_data->exception_msym == NULL)
186c406b 2444 {
17450429 2445 struct minimal_symbol *debug_hook;
186c406b 2446
17450429
PP
2447 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2448 if (debug_hook == NULL)
2449 {
2450 bp_objfile_data->exception_msym = &msym_not_found;
2451 continue;
2452 }
2453
2454 bp_objfile_data->exception_msym = debug_hook;
186c406b 2455 }
17450429
PP
2456
2457 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2458 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2459 &current_target);
06edf0c0
PA
2460 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2461 &internal_breakpoint_ops);
17450429
PP
2462 b->addr_string = xstrdup (func_name);
2463 b->enable_state = bp_disabled;
186c406b
TT
2464 }
2465
2466 update_global_location_list (1);
2467}
2468
c906108c 2469void
fba45db2 2470update_breakpoints_after_exec (void)
c906108c 2471{
35df4500 2472 struct breakpoint *b, *b_tmp;
876fa593 2473 struct bp_location *bploc, **bplocp_tmp;
c906108c 2474
25b22b0a
PA
2475 /* We're about to delete breakpoints from GDB's lists. If the
2476 INSERTED flag is true, GDB will try to lift the breakpoints by
2477 writing the breakpoints' "shadow contents" back into memory. The
2478 "shadow contents" are NOT valid after an exec, so GDB should not
2479 do that. Instead, the target is responsible from marking
2480 breakpoints out as soon as it detects an exec. We don't do that
2481 here instead, because there may be other attempts to delete
2482 breakpoints after detecting an exec and before reaching here. */
876fa593 2483 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2484 if (bploc->pspace == current_program_space)
2485 gdb_assert (!bploc->inserted);
c906108c 2486
35df4500 2487 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2488 {
6c95b8df
PA
2489 if (b->pspace != current_program_space)
2490 continue;
2491
4a64f543 2492 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2493 if (b->type == bp_shlib_event)
2494 {
2495 delete_breakpoint (b);
2496 continue;
2497 }
c906108c 2498
4a64f543 2499 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2500 if (b->type == bp_jit_event)
2501 {
2502 delete_breakpoint (b);
2503 continue;
2504 }
2505
1900040c 2506 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2507 as must overlay event and longjmp master breakpoints. */
2508 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2509 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2510 || b->type == bp_exception_master)
c4093a6a
JM
2511 {
2512 delete_breakpoint (b);
2513 continue;
2514 }
2515
4a64f543 2516 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 2517 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
2518 {
2519 delete_breakpoint (b);
2520 continue;
2521 }
2522
611c83ae
PA
2523 /* Longjmp and longjmp-resume breakpoints are also meaningless
2524 after an exec. */
186c406b
TT
2525 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2526 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2527 {
2528 delete_breakpoint (b);
2529 continue;
2530 }
2531
ce78b96d
JB
2532 if (b->type == bp_catchpoint)
2533 {
2534 /* For now, none of the bp_catchpoint breakpoints need to
2535 do anything at this point. In the future, if some of
2536 the catchpoints need to something, we will need to add
2537 a new method, and call this method from here. */
2538 continue;
2539 }
2540
c5aa993b
JM
2541 /* bp_finish is a special case. The only way we ought to be able
2542 to see one of these when an exec() has happened, is if the user
2543 caught a vfork, and then said "finish". Ordinarily a finish just
2544 carries them to the call-site of the current callee, by setting
2545 a temporary bp there and resuming. But in this case, the finish
2546 will carry them entirely through the vfork & exec.
2547
2548 We don't want to allow a bp_finish to remain inserted now. But
2549 we can't safely delete it, 'cause finish_command has a handle to
2550 the bp on a bpstat, and will later want to delete it. There's a
2551 chance (and I've seen it happen) that if we delete the bp_finish
2552 here, that its storage will get reused by the time finish_command
2553 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2554 We really must allow finish_command to delete a bp_finish.
2555
e5dd4106 2556 In the absence of a general solution for the "how do we know
53a5351d
JM
2557 it's safe to delete something others may have handles to?"
2558 problem, what we'll do here is just uninsert the bp_finish, and
2559 let finish_command delete it.
2560
2561 (We know the bp_finish is "doomed" in the sense that it's
2562 momentary, and will be deleted as soon as finish_command sees
2563 the inferior stopped. So it doesn't matter that the bp's
2564 address is probably bogus in the new a.out, unlike e.g., the
2565 solib breakpoints.) */
c5aa993b 2566
c5aa993b
JM
2567 if (b->type == bp_finish)
2568 {
2569 continue;
2570 }
2571
2572 /* Without a symbolic address, we have little hope of the
2573 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2574 a.out. */
c5aa993b
JM
2575 if (b->addr_string == NULL)
2576 {
2577 delete_breakpoint (b);
2578 continue;
2579 }
c5aa993b 2580 }
1900040c 2581 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2582 create_overlay_event_breakpoint ();
2583 create_longjmp_master_breakpoint ();
2584 create_std_terminate_master_breakpoint ();
186c406b 2585 create_exception_master_breakpoint ();
c906108c
SS
2586}
2587
2588int
fba45db2 2589detach_breakpoints (int pid)
c906108c 2590{
35df4500 2591 struct bp_location *bl, **blp_tmp;
3a1bae8e 2592 int val = 0;
ce696e05 2593 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2594 struct inferior *inf = current_inferior ();
c5aa993b 2595
39f77062 2596 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2597 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2598
6c95b8df 2599 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2600 inferior_ptid = pid_to_ptid (pid);
35df4500 2601 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2602 {
35df4500 2603 if (bl->pspace != inf->pspace)
6c95b8df
PA
2604 continue;
2605
35df4500
TJB
2606 if (bl->inserted)
2607 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2608 }
d03285ec
UW
2609
2610 /* Detach single-step breakpoints as well. */
2611 detach_single_step_breakpoints ();
2612
ce696e05 2613 do_cleanups (old_chain);
3a1bae8e 2614 return val;
c906108c
SS
2615}
2616
35df4500 2617/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2618 Note that this is used to detach breakpoints from a child fork.
2619 When we get here, the child isn't in the inferior list, and neither
2620 do we have objects to represent its address space --- we should
35df4500 2621 *not* look at bl->pspace->aspace here. */
6c95b8df 2622
c906108c 2623static int
35df4500 2624remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2625{
2626 int val;
c5aa993b 2627
35df4500
TJB
2628 /* BL is never in moribund_locations by our callers. */
2629 gdb_assert (bl->owner != NULL);
2bdf28a0 2630
35df4500 2631 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2632 /* Permanent breakpoints cannot be inserted or removed. */
2633 return 0;
2634
74960c60
VP
2635 /* The type of none suggests that owner is actually deleted.
2636 This should not ever happen. */
35df4500 2637 gdb_assert (bl->owner->type != bp_none);
0bde7532 2638
35df4500
TJB
2639 if (bl->loc_type == bp_loc_software_breakpoint
2640 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2641 {
c02f5703
MS
2642 /* "Normal" instruction breakpoint: either the standard
2643 trap-instruction bp (bp_breakpoint), or a
2644 bp_hardware_breakpoint. */
2645
2646 /* First check to see if we have to handle an overlay. */
2647 if (overlay_debugging == ovly_off
35df4500
TJB
2648 || bl->section == NULL
2649 || !(section_is_overlay (bl->section)))
c02f5703
MS
2650 {
2651 /* No overlay handling: just remove the breakpoint. */
348d480f 2652 val = bl->owner->ops->remove_location (bl);
c02f5703 2653 }
c906108c
SS
2654 else
2655 {
4a64f543 2656 /* This breakpoint is in an overlay section.
c02f5703
MS
2657 Did we set a breakpoint at the LMA? */
2658 if (!overlay_events_enabled)
2659 {
2660 /* Yes -- overlay event support is not active, so we
2661 should have set a breakpoint at the LMA. Remove it.
2662 */
c02f5703
MS
2663 /* Ignore any failures: if the LMA is in ROM, we will
2664 have already warned when we failed to insert it. */
35df4500
TJB
2665 if (bl->loc_type == bp_loc_hardware_breakpoint)
2666 target_remove_hw_breakpoint (bl->gdbarch,
2667 &bl->overlay_target_info);
c02f5703 2668 else
35df4500
TJB
2669 target_remove_breakpoint (bl->gdbarch,
2670 &bl->overlay_target_info);
c02f5703
MS
2671 }
2672 /* Did we set a breakpoint at the VMA?
2673 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2674 if (bl->inserted)
c906108c 2675 {
c02f5703
MS
2676 /* Yes -- remove it. Previously we did not bother to
2677 remove the breakpoint if the section had been
2678 unmapped, but let's not rely on that being safe. We
2679 don't know what the overlay manager might do. */
aa67235e
UW
2680
2681 /* However, we should remove *software* breakpoints only
2682 if the section is still mapped, or else we overwrite
2683 wrong code with the saved shadow contents. */
348d480f
PA
2684 if (bl->loc_type == bp_loc_hardware_breakpoint
2685 || section_is_mapped (bl->section))
2686 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
2687 else
2688 val = 0;
c906108c 2689 }
c02f5703
MS
2690 else
2691 {
2692 /* No -- not inserted, so no need to remove. No error. */
2693 val = 0;
2694 }
c906108c 2695 }
879d1e6b
UW
2696
2697 /* In some cases, we might not be able to remove a breakpoint
2698 in a shared library that has already been removed, but we
2699 have not yet processed the shlib unload event. */
35df4500 2700 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2701 val = 0;
2702
c906108c
SS
2703 if (val)
2704 return val;
35df4500 2705 bl->inserted = (is == mark_inserted);
c906108c 2706 }
35df4500 2707 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2708 {
77b06cd7
TJB
2709 gdb_assert (bl->owner->ops != NULL
2710 && bl->owner->ops->remove_location != NULL);
2711
35df4500 2712 bl->inserted = (is == mark_inserted);
77b06cd7 2713 bl->owner->ops->remove_location (bl);
2e70b7b9 2714
c906108c 2715 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2716 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2717 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2718 bl->owner->number);
c906108c 2719 }
35df4500
TJB
2720 else if (bl->owner->type == bp_catchpoint
2721 && breakpoint_enabled (bl->owner)
2722 && !bl->duplicate)
ce78b96d 2723 {
77b06cd7
TJB
2724 gdb_assert (bl->owner->ops != NULL
2725 && bl->owner->ops->remove_location != NULL);
ce78b96d 2726
77b06cd7 2727 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2728 if (val)
2729 return val;
77b06cd7 2730
35df4500 2731 bl->inserted = (is == mark_inserted);
ce78b96d 2732 }
c906108c
SS
2733
2734 return 0;
2735}
2736
6c95b8df 2737static int
35df4500 2738remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2739{
2740 int ret;
2741 struct cleanup *old_chain;
2742
35df4500
TJB
2743 /* BL is never in moribund_locations by our callers. */
2744 gdb_assert (bl->owner != NULL);
2bdf28a0 2745
35df4500 2746 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2747 /* Permanent breakpoints cannot be inserted or removed. */
2748 return 0;
2749
2750 /* The type of none suggests that owner is actually deleted.
2751 This should not ever happen. */
35df4500 2752 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2753
2754 old_chain = save_current_space_and_thread ();
2755
35df4500 2756 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2757
35df4500 2758 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2759
2760 do_cleanups (old_chain);
2761 return ret;
2762}
2763
c906108c
SS
2764/* Clear the "inserted" flag in all breakpoints. */
2765
25b22b0a 2766void
fba45db2 2767mark_breakpoints_out (void)
c906108c 2768{
35df4500 2769 struct bp_location *bl, **blp_tmp;
c906108c 2770
35df4500
TJB
2771 ALL_BP_LOCATIONS (bl, blp_tmp)
2772 if (bl->pspace == current_program_space)
2773 bl->inserted = 0;
c906108c
SS
2774}
2775
53a5351d
JM
2776/* Clear the "inserted" flag in all breakpoints and delete any
2777 breakpoints which should go away between runs of the program.
c906108c
SS
2778
2779 Plus other such housekeeping that has to be done for breakpoints
2780 between runs.
2781
53a5351d
JM
2782 Note: this function gets called at the end of a run (by
2783 generic_mourn_inferior) and when a run begins (by
4a64f543 2784 init_wait_for_inferior). */
c906108c
SS
2785
2786
2787
2788void
fba45db2 2789breakpoint_init_inferior (enum inf_context context)
c906108c 2790{
35df4500
TJB
2791 struct breakpoint *b, *b_tmp;
2792 struct bp_location *bl, **blp_tmp;
1c5cfe86 2793 int ix;
6c95b8df 2794 struct program_space *pspace = current_program_space;
c906108c 2795
50c71eaf
PA
2796 /* If breakpoint locations are shared across processes, then there's
2797 nothing to do. */
2567c7d9 2798 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2799 return;
2800
35df4500 2801 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2802 {
35df4500
TJB
2803 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2804 if (bl->pspace == pspace
2805 && bl->owner->enable_state != bp_permanent)
2806 bl->inserted = 0;
6c95b8df 2807 }
075f6582 2808
35df4500 2809 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2810 {
6c95b8df
PA
2811 if (b->loc && b->loc->pspace != pspace)
2812 continue;
2813
c5aa993b
JM
2814 switch (b->type)
2815 {
2816 case bp_call_dummy:
c906108c 2817
c5aa993b 2818 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2819 cause problems when the inferior is rerun, so we better get
2820 rid of it. */
2821
2822 case bp_watchpoint_scope:
2823
2824 /* Also get rid of scope breakpoints. */
2825
2826 case bp_shlib_event:
2827
2828 /* Also remove solib event breakpoints. Their addresses may
2829 have changed since the last time we ran the program.
2830 Actually we may now be debugging against different target;
2831 and so the solib backend that installed this breakpoint may
2832 not be used in by the target. E.g.,
2833
2834 (gdb) file prog-linux
2835 (gdb) run # native linux target
2836 ...
2837 (gdb) kill
2838 (gdb) file prog-win.exe
2839 (gdb) tar rem :9999 # remote Windows gdbserver.
2840 */
c906108c 2841
c5aa993b
JM
2842 delete_breakpoint (b);
2843 break;
c906108c 2844
c5aa993b
JM
2845 case bp_watchpoint:
2846 case bp_hardware_watchpoint:
2847 case bp_read_watchpoint:
2848 case bp_access_watchpoint:
3a5c3e22
PA
2849 {
2850 struct watchpoint *w = (struct watchpoint *) b;
c906108c 2851
3a5c3e22
PA
2852 /* Likewise for watchpoints on local expressions. */
2853 if (w->exp_valid_block != NULL)
2854 delete_breakpoint (b);
2855 else if (context == inf_starting)
2856 {
2857 /* Reset val field to force reread of starting value in
2858 insert_breakpoints. */
2859 if (w->val)
2860 value_free (w->val);
2861 w->val = NULL;
2862 w->val_valid = 0;
c860120c 2863 }
3a5c3e22 2864 }
c5aa993b
JM
2865 break;
2866 default:
c5aa993b
JM
2867 break;
2868 }
2869 }
1c5cfe86
PA
2870
2871 /* Get rid of the moribund locations. */
35df4500
TJB
2872 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2873 decref_bp_location (&bl);
1c5cfe86 2874 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2875}
2876
6c95b8df
PA
2877/* These functions concern about actual breakpoints inserted in the
2878 target --- to e.g. check if we need to do decr_pc adjustment or if
2879 we need to hop over the bkpt --- so we check for address space
2880 match, not program space. */
2881
c2c6d25f
JM
2882/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2883 exists at PC. It returns ordinary_breakpoint_here if it's an
2884 ordinary breakpoint, or permanent_breakpoint_here if it's a
2885 permanent breakpoint.
2886 - When continuing from a location with an ordinary breakpoint, we
2887 actually single step once before calling insert_breakpoints.
e5dd4106 2888 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
2889 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2890 the target, to advance the PC past the breakpoint. */
c906108c 2891
c2c6d25f 2892enum breakpoint_here
6c95b8df 2893breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2894{
35df4500 2895 struct bp_location *bl, **blp_tmp;
c2c6d25f 2896 int any_breakpoint_here = 0;
c906108c 2897
35df4500 2898 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2899 {
35df4500
TJB
2900 if (bl->loc_type != bp_loc_software_breakpoint
2901 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2902 continue;
2903
f1310107 2904 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2905 if ((breakpoint_enabled (bl->owner)
2906 || bl->owner->enable_state == bp_permanent)
f1310107 2907 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2908 {
2909 if (overlay_debugging
35df4500
TJB
2910 && section_is_overlay (bl->section)
2911 && !section_is_mapped (bl->section))
075f6582 2912 continue; /* unmapped overlay -- can't be a match */
35df4500 2913 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2914 return permanent_breakpoint_here;
2915 else
2916 any_breakpoint_here = 1;
2917 }
2918 }
c906108c 2919
c2c6d25f 2920 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2921}
2922
1c5cfe86
PA
2923/* Return true if there's a moribund breakpoint at PC. */
2924
2925int
6c95b8df 2926moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2927{
2928 struct bp_location *loc;
2929 int ix;
2930
2931 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2932 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2933 return 1;
2934
2935 return 0;
2936}
c2c6d25f 2937
c36b740a 2938/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2939 inserted using regular breakpoint_chain / bp_location array
2940 mechanism. This does not check for single-step breakpoints, which
2941 are inserted and removed using direct target manipulation. */
c906108c
SS
2942
2943int
4a64f543
MS
2944regular_breakpoint_inserted_here_p (struct address_space *aspace,
2945 CORE_ADDR pc)
c906108c 2946{
35df4500 2947 struct bp_location *bl, **blp_tmp;
c906108c 2948
35df4500 2949 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2950 {
35df4500
TJB
2951 if (bl->loc_type != bp_loc_software_breakpoint
2952 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2953 continue;
2954
35df4500 2955 if (bl->inserted
f1310107 2956 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2957 {
2958 if (overlay_debugging
35df4500
TJB
2959 && section_is_overlay (bl->section)
2960 && !section_is_mapped (bl->section))
075f6582
DJ
2961 continue; /* unmapped overlay -- can't be a match */
2962 else
2963 return 1;
2964 }
c5aa993b 2965 }
c36b740a
VP
2966 return 0;
2967}
2968
2969/* Returns non-zero iff there's either regular breakpoint
2970 or a single step breakpoint inserted at PC. */
2971
2972int
6c95b8df 2973breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2974{
6c95b8df 2975 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2976 return 1;
c906108c 2977
6c95b8df 2978 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2979 return 1;
2980
c906108c
SS
2981 return 0;
2982}
2983
4fa8626c
DJ
2984/* This function returns non-zero iff there is a software breakpoint
2985 inserted at PC. */
2986
2987int
3e43a32a
MS
2988software_breakpoint_inserted_here_p (struct address_space *aspace,
2989 CORE_ADDR pc)
4fa8626c 2990{
35df4500 2991 struct bp_location *bl, **blp_tmp;
4fa8626c 2992
35df4500 2993 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2994 {
35df4500 2995 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2996 continue;
2997
35df4500
TJB
2998 if (bl->inserted
2999 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 3000 aspace, pc))
4fa8626c
DJ
3001 {
3002 if (overlay_debugging
35df4500
TJB
3003 && section_is_overlay (bl->section)
3004 && !section_is_mapped (bl->section))
4fa8626c
DJ
3005 continue; /* unmapped overlay -- can't be a match */
3006 else
3007 return 1;
3008 }
3009 }
3010
1aafd4da 3011 /* Also check for software single-step breakpoints. */
6c95b8df 3012 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3013 return 1;
3014
4fa8626c
DJ
3015 return 0;
3016}
3017
9093389c
PA
3018int
3019hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3020 CORE_ADDR addr, ULONGEST len)
3021{
3022 struct breakpoint *bpt;
3023
3024 ALL_BREAKPOINTS (bpt)
3025 {
3026 struct bp_location *loc;
3027
3028 if (bpt->type != bp_hardware_watchpoint
3029 && bpt->type != bp_access_watchpoint)
3030 continue;
3031
3032 if (!breakpoint_enabled (bpt))
3033 continue;
3034
3035 for (loc = bpt->loc; loc; loc = loc->next)
3036 if (loc->pspace->aspace == aspace && loc->inserted)
3037 {
3038 CORE_ADDR l, h;
3039
3040 /* Check for intersection. */
3041 l = max (loc->address, addr);
3042 h = min (loc->address + loc->length, addr + len);
3043 if (l < h)
3044 return 1;
3045 }
3046 }
3047 return 0;
3048}
3049
075f6582
DJ
3050/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3051 PC is valid for process/thread PTID. */
c906108c
SS
3052
3053int
6c95b8df
PA
3054breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3055 ptid_t ptid)
c906108c 3056{
35df4500 3057 struct bp_location *bl, **blp_tmp;
4a306c9a 3058 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3059 int thread = -1;
4a306c9a 3060 int task = 0;
a6f1cd96 3061
35df4500 3062 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3063 {
35df4500
TJB
3064 if (bl->loc_type != bp_loc_software_breakpoint
3065 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3066 continue;
3067
35df4500
TJB
3068 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3069 if (!breakpoint_enabled (bl->owner)
3070 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3071 continue;
3072
f1310107 3073 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3074 continue;
3075
35df4500 3076 if (bl->owner->thread != -1)
075f6582 3077 {
a6f1cd96
JB
3078 /* This is a thread-specific breakpoint. Check that ptid
3079 matches that thread. If thread hasn't been computed yet,
3080 it is now time to do so. */
3081 if (thread == -1)
3082 thread = pid_to_thread_id (ptid);
35df4500 3083 if (bl->owner->thread != thread)
a6f1cd96 3084 continue;
075f6582 3085 }
a6f1cd96 3086
35df4500 3087 if (bl->owner->task != 0)
4a306c9a
JB
3088 {
3089 /* This is a task-specific breakpoint. Check that ptid
3090 matches that task. If task hasn't been computed yet,
3091 it is now time to do so. */
3092 if (task == 0)
3093 task = ada_get_task_number (ptid);
35df4500 3094 if (bl->owner->task != task)
4a306c9a
JB
3095 continue;
3096 }
3097
a6f1cd96 3098 if (overlay_debugging
35df4500
TJB
3099 && section_is_overlay (bl->section)
3100 && !section_is_mapped (bl->section))
a6f1cd96
JB
3101 continue; /* unmapped overlay -- can't be a match */
3102
3103 return 1;
c5aa993b 3104 }
c906108c
SS
3105
3106 return 0;
3107}
c906108c 3108\f
c5aa993b 3109
c906108c
SS
3110/* bpstat stuff. External routines' interfaces are documented
3111 in breakpoint.h. */
3112
3113int
fba45db2 3114ep_is_catchpoint (struct breakpoint *ep)
c906108c 3115{
533be4dd 3116 return (ep->type == bp_catchpoint);
c906108c
SS
3117}
3118
f431efe5
PA
3119/* Frees any storage that is part of a bpstat. Does not walk the
3120 'next' chain. */
3121
3122static void
198757a8
VP
3123bpstat_free (bpstat bs)
3124{
3125 if (bs->old_val != NULL)
3126 value_free (bs->old_val);
9add0f1b 3127 decref_counted_command_line (&bs->commands);
f431efe5 3128 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3129 xfree (bs);
3130}
3131
c906108c
SS
3132/* Clear a bpstat so that it says we are not at any breakpoint.
3133 Also free any storage that is part of a bpstat. */
3134
3135void
fba45db2 3136bpstat_clear (bpstat *bsp)
c906108c
SS
3137{
3138 bpstat p;
3139 bpstat q;
3140
3141 if (bsp == 0)
3142 return;
3143 p = *bsp;
3144 while (p != NULL)
3145 {
3146 q = p->next;
198757a8 3147 bpstat_free (p);
c906108c
SS
3148 p = q;
3149 }
3150 *bsp = NULL;
3151}
3152
3153/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3154 is part of the bpstat is copied as well. */
3155
3156bpstat
fba45db2 3157bpstat_copy (bpstat bs)
c906108c
SS
3158{
3159 bpstat p = NULL;
3160 bpstat tmp;
3161 bpstat retval = NULL;
3162
3163 if (bs == NULL)
3164 return bs;
3165
3166 for (; bs != NULL; bs = bs->next)
3167 {
3168 tmp = (bpstat) xmalloc (sizeof (*tmp));
3169 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3170 incref_counted_command_line (tmp->commands);
f431efe5 3171 incref_bp_location (tmp->bp_location_at);
31cc81e9 3172 if (bs->old_val != NULL)
3c3185ac
JK
3173 {
3174 tmp->old_val = value_copy (bs->old_val);
3175 release_value (tmp->old_val);
3176 }
31cc81e9 3177
c906108c
SS
3178 if (p == NULL)
3179 /* This is the first thing in the chain. */
3180 retval = tmp;
3181 else
3182 p->next = tmp;
3183 p = tmp;
3184 }
3185 p->next = NULL;
3186 return retval;
3187}
3188
4a64f543 3189/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3190
3191bpstat
fba45db2 3192bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3193{
c5aa993b
JM
3194 if (bsp == NULL)
3195 return NULL;
c906108c 3196
c5aa993b
JM
3197 for (; bsp != NULL; bsp = bsp->next)
3198 {
f431efe5 3199 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3200 return bsp;
3201 }
c906108c
SS
3202 return NULL;
3203}
3204
4a64f543
MS
3205/* Put in *NUM the breakpoint number of the first breakpoint we are
3206 stopped at. *BSP upon return is a bpstat which points to the
3207 remaining breakpoints stopped at (but which is not guaranteed to be
3208 good for anything but further calls to bpstat_num).
3209
8671a17b
PA
3210 Return 0 if passed a bpstat which does not indicate any breakpoints.
3211 Return -1 if stopped at a breakpoint that has been deleted since
3212 we set it.
3213 Return 1 otherwise. */
c906108c
SS
3214
3215int
8671a17b 3216bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3217{
3218 struct breakpoint *b;
3219
3220 if ((*bsp) == NULL)
3221 return 0; /* No more breakpoint values */
8671a17b 3222
4a64f543
MS
3223 /* We assume we'll never have several bpstats that correspond to a
3224 single breakpoint -- otherwise, this function might return the
3225 same number more than once and this will look ugly. */
f431efe5 3226 b = (*bsp)->breakpoint_at;
8671a17b
PA
3227 *bsp = (*bsp)->next;
3228 if (b == NULL)
3229 return -1; /* breakpoint that's been deleted since */
3230
3231 *num = b->number; /* We have its number */
3232 return 1;
c906108c
SS
3233}
3234
e93ca019 3235/* See breakpoint.h. */
c906108c
SS
3236
3237void
e93ca019 3238bpstat_clear_actions (void)
c906108c 3239{
e93ca019
JK
3240 struct thread_info *tp;
3241 bpstat bs;
3242
3243 if (ptid_equal (inferior_ptid, null_ptid))
3244 return;
3245
3246 tp = find_thread_ptid (inferior_ptid);
3247 if (tp == NULL)
3248 return;
3249
3250 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 3251 {
9add0f1b 3252 decref_counted_command_line (&bs->commands);
abf85f46 3253
c906108c
SS
3254 if (bs->old_val != NULL)
3255 {
3256 value_free (bs->old_val);
3257 bs->old_val = NULL;
3258 }
3259 }
3260}
3261
f3b1572e
PA
3262/* Called when a command is about to proceed the inferior. */
3263
3264static void
3265breakpoint_about_to_proceed (void)
3266{
3267 if (!ptid_equal (inferior_ptid, null_ptid))
3268 {
3269 struct thread_info *tp = inferior_thread ();
3270
3271 /* Allow inferior function calls in breakpoint commands to not
3272 interrupt the command list. When the call finishes
3273 successfully, the inferior will be standing at the same
3274 breakpoint as if nothing happened. */
16c381f0 3275 if (tp->control.in_infcall)
f3b1572e
PA
3276 return;
3277 }
3278
3279 breakpoint_proceeded = 1;
3280}
3281
4a64f543
MS
3282/* Stub for cleaning up our state if we error-out of a breakpoint
3283 command. */
c906108c 3284static void
4efb68b1 3285cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3286{
3287 executing_breakpoint_commands = 0;
3288}
3289
abf85f46
JK
3290/* Return non-zero iff CMD as the first line of a command sequence is `silent'
3291 or its equivalent. */
3292
3293static int
3294command_line_is_silent (struct command_line *cmd)
3295{
3296 return cmd && (strcmp ("silent", cmd->line) == 0
3297 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3298}
3299
4a64f543
MS
3300/* Execute all the commands associated with all the breakpoints at
3301 this location. Any of these commands could cause the process to
3302 proceed beyond this point, etc. We look out for such changes by
3303 checking the global "breakpoint_proceeded" after each command.
c906108c 3304
347bddb7
PA
3305 Returns true if a breakpoint command resumed the inferior. In that
3306 case, it is the caller's responsibility to recall it again with the
3307 bpstat of the current thread. */
3308
3309static int
3310bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3311{
3312 bpstat bs;
3313 struct cleanup *old_chain;
347bddb7 3314 int again = 0;
c906108c
SS
3315
3316 /* Avoid endless recursion if a `source' command is contained
3317 in bs->commands. */
3318 if (executing_breakpoint_commands)
347bddb7 3319 return 0;
c906108c
SS
3320
3321 executing_breakpoint_commands = 1;
3322 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3323
cf6c5ffb
TT
3324 prevent_dont_repeat ();
3325
4a64f543 3326 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3327 bs = *bsp;
3328
3329 breakpoint_proceeded = 0;
3330 for (; bs != NULL; bs = bs->next)
3331 {
9add0f1b 3332 struct counted_command_line *ccmd;
6c50ab1c
JB
3333 struct command_line *cmd;
3334 struct cleanup *this_cmd_tree_chain;
3335
3336 /* Take ownership of the BSP's command tree, if it has one.
3337
3338 The command tree could legitimately contain commands like
3339 'step' and 'next', which call clear_proceed_status, which
3340 frees stop_bpstat's command tree. To make sure this doesn't
3341 free the tree we're executing out from under us, we need to
3342 take ownership of the tree ourselves. Since a given bpstat's
3343 commands are only executed once, we don't need to copy it; we
3344 can clear the pointer in the bpstat, and make sure we free
3345 the tree when we're done. */
9add0f1b
TT
3346 ccmd = bs->commands;
3347 bs->commands = NULL;
abf85f46
JK
3348 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
3349 cmd = ccmd ? ccmd->commands : NULL;
3350 if (command_line_is_silent (cmd))
3351 {
3352 /* The action has been already done by bpstat_stop_status. */
3353 cmd = cmd->next;
3354 }
6c50ab1c 3355
c906108c
SS
3356 while (cmd != NULL)
3357 {
3358 execute_control_command (cmd);
3359
3360 if (breakpoint_proceeded)
3361 break;
3362 else
3363 cmd = cmd->next;
3364 }
6c50ab1c
JB
3365
3366 /* We can free this command tree now. */
3367 do_cleanups (this_cmd_tree_chain);
3368
c906108c 3369 if (breakpoint_proceeded)
32c1e744
VP
3370 {
3371 if (target_can_async_p ())
347bddb7
PA
3372 /* If we are in async mode, then the target might be still
3373 running, not stopped at any breakpoint, so nothing for
3374 us to do here -- just return to the event loop. */
3375 ;
32c1e744
VP
3376 else
3377 /* In sync mode, when execute_control_command returns
3378 we're already standing on the next breakpoint.
347bddb7
PA
3379 Breakpoint commands for that stop were not run, since
3380 execute_command does not run breakpoint commands --
3381 only command_line_handler does, but that one is not
3382 involved in execution of breakpoint commands. So, we
3383 can now execute breakpoint commands. It should be
3384 noted that making execute_command do bpstat actions is
3385 not an option -- in this case we'll have recursive
3386 invocation of bpstat for each breakpoint with a
3387 command, and can easily blow up GDB stack. Instead, we
3388 return true, which will trigger the caller to recall us
3389 with the new stop_bpstat. */
3390 again = 1;
3391 break;
32c1e744 3392 }
c906108c 3393 }
c2b8ed2c 3394 do_cleanups (old_chain);
347bddb7
PA
3395 return again;
3396}
3397
3398void
3399bpstat_do_actions (void)
3400{
353d1d73
JK
3401 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
3402
347bddb7
PA
3403 /* Do any commands attached to breakpoint we are stopped at. */
3404 while (!ptid_equal (inferior_ptid, null_ptid)
3405 && target_has_execution
3406 && !is_exited (inferior_ptid)
3407 && !is_executing (inferior_ptid))
3408 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3409 and only return when it is stopped at the next breakpoint, we
3410 keep doing breakpoint actions until it returns false to
3411 indicate the inferior was not resumed. */
16c381f0 3412 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3413 break;
353d1d73
JK
3414
3415 discard_cleanups (cleanup_if_error);
c906108c
SS
3416}
3417
fa4727a6
DJ
3418/* Print out the (old or new) value associated with a watchpoint. */
3419
3420static void
3421watchpoint_value_print (struct value *val, struct ui_file *stream)
3422{
3423 if (val == NULL)
3424 fprintf_unfiltered (stream, _("<unreadable>"));
3425 else
79a45b7d
TT
3426 {
3427 struct value_print_options opts;
3428 get_user_print_options (&opts);
3429 value_print (val, stream, &opts);
3430 }
fa4727a6
DJ
3431}
3432
e514a9d6 3433/* Generic routine for printing messages indicating why we
4a64f543 3434 stopped. The behavior of this function depends on the value
e514a9d6
JM
3435 'print_it' in the bpstat structure. Under some circumstances we
3436 may decide not to print anything here and delegate the task to
4a64f543 3437 normal_stop(). */
e514a9d6
JM
3438
3439static enum print_stop_action
3440print_bp_stop_message (bpstat bs)
3441{
3442 switch (bs->print_it)
3443 {
3444 case print_it_noop:
4a64f543 3445 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3446 return PRINT_UNKNOWN;
3447 break;
3448
3449 case print_it_done:
3450 /* We still want to print the frame, but we already printed the
4a64f543 3451 relevant messages. */
e514a9d6
JM
3452 return PRINT_SRC_AND_LOC;
3453 break;
3454
3455 case print_it_normal:
4f8d1dc6 3456 {
f431efe5
PA
3457 struct breakpoint *b = bs->breakpoint_at;
3458
1a6a67de
TJB
3459 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3460 which has since been deleted. */
3461 if (b == NULL)
3462 return PRINT_UNKNOWN;
3463
348d480f
PA
3464 /* Normal case. Call the breakpoint's print_it method. */
3465 return b->ops->print_it (bs);
4f8d1dc6 3466 }
348d480f 3467 break;
3086aeae 3468
e514a9d6 3469 default:
8e65ff28 3470 internal_error (__FILE__, __LINE__,
e2e0b3e5 3471 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3472 break;
c906108c 3473 }
c906108c
SS
3474}
3475
e514a9d6
JM
3476/* Print a message indicating what happened. This is called from
3477 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
3478 list - a list of the eventpoints that caused this stop. KIND is
3479 the target_waitkind for the stopping event. This
e514a9d6
JM
3480 routine calls the generic print routine for printing a message
3481 about reasons for stopping. This will print (for example) the
3482 "Breakpoint n," part of the output. The return value of this
3483 routine is one of:
c906108c 3484
4a64f543 3485 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3486 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3487 code to print the location. An example is
c5aa993b
JM
3488 "Breakpoint 1, " which should be followed by
3489 the location.
917317f4 3490 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3491 to also print the location part of the message.
3492 An example is the catch/throw messages, which
4a64f543 3493 don't require a location appended to the end.
917317f4 3494 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3495 further info to be printed. */
c906108c 3496
917317f4 3497enum print_stop_action
36dfb11c 3498bpstat_print (bpstat bs, int kind)
c906108c
SS
3499{
3500 int val;
c5aa993b 3501
c906108c 3502 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3503 (Currently all watchpoints go on the bpstat whether hit or not.
3504 That probably could (should) be changed, provided care is taken
c906108c 3505 with respect to bpstat_explains_signal). */
e514a9d6
JM
3506 for (; bs; bs = bs->next)
3507 {
3508 val = print_bp_stop_message (bs);
3509 if (val == PRINT_SRC_ONLY
3510 || val == PRINT_SRC_AND_LOC
3511 || val == PRINT_NOTHING)
3512 return val;
3513 }
c906108c 3514
36dfb11c
TT
3515 /* If we had hit a shared library event breakpoint,
3516 print_bp_stop_message would print out this message. If we hit an
3517 OS-level shared library event, do the same thing. */
3518 if (kind == TARGET_WAITKIND_LOADED)
3519 {
3520 ui_out_text (current_uiout, _("Stopped due to shared library event\n"));
3521 if (ui_out_is_mi_like_p (current_uiout))
3522 ui_out_field_string (current_uiout, "reason",
3523 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
3524 return PRINT_NOTHING;
3525 }
3526
e514a9d6 3527 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3528 with and nothing was printed. */
917317f4 3529 return PRINT_UNKNOWN;
c906108c
SS
3530}
3531
4a64f543
MS
3532/* Evaluate the expression EXP and return 1 if value is zero. This is
3533 used inside a catch_errors to evaluate the breakpoint condition.
3534 The argument is a "struct expression *" that has been cast to a
3535 "char *" to make it pass through catch_errors. */
c906108c
SS
3536
3537static int
4efb68b1 3538breakpoint_cond_eval (void *exp)
c906108c 3539{
278cd55f 3540 struct value *mark = value_mark ();
c5aa993b 3541 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3542
c906108c
SS
3543 value_free_to_mark (mark);
3544 return i;
3545}
3546
5760d0ab 3547/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3548
3549static bpstat
5760d0ab 3550bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3551{
3552 bpstat bs;
3553
3554 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3555 bs->next = NULL;
3556 **bs_link_pointer = bs;
3557 *bs_link_pointer = &bs->next;
f431efe5
PA
3558 bs->breakpoint_at = bl->owner;
3559 bs->bp_location_at = bl;
3560 incref_bp_location (bl);
c906108c
SS
3561 /* If the condition is false, etc., don't do the commands. */
3562 bs->commands = NULL;
3563 bs->old_val = NULL;
3564 bs->print_it = print_it_normal;
3565 return bs;
3566}
3567\f
d983da9c
DJ
3568/* The target has stopped with waitstatus WS. Check if any hardware
3569 watchpoints have triggered, according to the target. */
3570
3571int
3572watchpoints_triggered (struct target_waitstatus *ws)
3573{
d92524f1 3574 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3575 CORE_ADDR addr;
3576 struct breakpoint *b;
3577
3578 if (!stopped_by_watchpoint)
3579 {
3580 /* We were not stopped by a watchpoint. Mark all watchpoints
3581 as not triggered. */
3582 ALL_BREAKPOINTS (b)
cc60f2e3 3583 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3584 {
3585 struct watchpoint *w = (struct watchpoint *) b;
3586
3587 w->watchpoint_triggered = watch_triggered_no;
3588 }
d983da9c
DJ
3589
3590 return 0;
3591 }
3592
3593 if (!target_stopped_data_address (&current_target, &addr))
3594 {
3595 /* We were stopped by a watchpoint, but we don't know where.
3596 Mark all watchpoints as unknown. */
3597 ALL_BREAKPOINTS (b)
cc60f2e3 3598 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3599 {
3600 struct watchpoint *w = (struct watchpoint *) b;
3601
3602 w->watchpoint_triggered = watch_triggered_unknown;
3603 }
d983da9c
DJ
3604
3605 return stopped_by_watchpoint;
3606 }
3607
3608 /* The target could report the data address. Mark watchpoints
3609 affected by this data address as triggered, and all others as not
3610 triggered. */
3611
3612 ALL_BREAKPOINTS (b)
cc60f2e3 3613 if (is_hardware_watchpoint (b))
d983da9c 3614 {
3a5c3e22 3615 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 3616 struct bp_location *loc;
d983da9c 3617
3a5c3e22 3618 w->watchpoint_triggered = watch_triggered_no;
a5606eee 3619 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 3620 {
3a5c3e22 3621 if (is_masked_watchpoint (b))
9c06b0b4 3622 {
3a5c3e22
PA
3623 CORE_ADDR newaddr = addr & w->hw_wp_mask;
3624 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
3625
3626 if (newaddr == start)
3627 {
3a5c3e22 3628 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3629 break;
3630 }
3631 }
3632 /* Exact match not required. Within range is sufficient. */
3633 else if (target_watchpoint_addr_within_range (&current_target,
3634 addr, loc->address,
3635 loc->length))
3636 {
3a5c3e22 3637 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3638 break;
3639 }
3640 }
d983da9c
DJ
3641 }
3642
3643 return 1;
3644}
3645
c906108c
SS
3646/* Possible return values for watchpoint_check (this can't be an enum
3647 because of check_errors). */
3648/* The watchpoint has been deleted. */
3649#define WP_DELETED 1
3650/* The value has changed. */
3651#define WP_VALUE_CHANGED 2
3652/* The value has not changed. */
3653#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3654/* Ignore this watchpoint, no matter if the value changed or not. */
3655#define WP_IGNORE 4
c906108c
SS
3656
3657#define BP_TEMPFLAG 1
3658#define BP_HARDWAREFLAG 2
3659
4a64f543
MS
3660/* Evaluate watchpoint condition expression and check if its value
3661 changed.
553e4c11
JB
3662
3663 P should be a pointer to struct bpstat, but is defined as a void *
3664 in order for this function to be usable with catch_errors. */
c906108c
SS
3665
3666static int
4efb68b1 3667watchpoint_check (void *p)
c906108c
SS
3668{
3669 bpstat bs = (bpstat) p;
3a5c3e22 3670 struct watchpoint *b;
c906108c
SS
3671 struct frame_info *fr;
3672 int within_current_scope;
3673
f431efe5 3674 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3675 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 3676 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 3677
f6bc2008
PA
3678 /* If this is a local watchpoint, we only want to check if the
3679 watchpoint frame is in scope if the current thread is the thread
3680 that was used to create the watchpoint. */
3681 if (!watchpoint_in_thread_scope (b))
60e1c644 3682 return WP_IGNORE;
f6bc2008 3683
c906108c
SS
3684 if (b->exp_valid_block == NULL)
3685 within_current_scope = 1;
3686 else
3687 {
edb3359d
DJ
3688 struct frame_info *frame = get_current_frame ();
3689 struct gdbarch *frame_arch = get_frame_arch (frame);
3690 CORE_ADDR frame_pc = get_frame_pc (frame);
3691
4a64f543
MS
3692 /* in_function_epilogue_p() returns a non-zero value if we're
3693 still in the function but the stack frame has already been
3694 invalidated. Since we can't rely on the values of local
3695 variables after the stack has been destroyed, we are treating
3696 the watchpoint in that state as `not changed' without further
3697 checking. Don't mark watchpoints as changed if the current
3698 frame is in an epilogue - even if they are in some other
3699 frame, our view of the stack is likely to be wrong and
3700 frame_find_by_id could error out. */
a0f49112 3701 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3702 return WP_IGNORE;
a0f49112 3703
101dcfbe 3704 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3705 within_current_scope = (fr != NULL);
69fbadd5
DJ
3706
3707 /* If we've gotten confused in the unwinder, we might have
3708 returned a frame that can't describe this variable. */
edb3359d
DJ
3709 if (within_current_scope)
3710 {
3711 struct symbol *function;
3712
3713 function = get_frame_function (fr);
3714 if (function == NULL
3715 || !contained_in (b->exp_valid_block,
3716 SYMBOL_BLOCK_VALUE (function)))
3717 within_current_scope = 0;
3718 }
69fbadd5 3719
edb3359d 3720 if (within_current_scope)
c906108c
SS
3721 /* If we end up stopping, the current frame will get selected
3722 in normal_stop. So this call to select_frame won't affect
3723 the user. */
0f7d239c 3724 select_frame (fr);
c906108c 3725 }
c5aa993b 3726
c906108c
SS
3727 if (within_current_scope)
3728 {
4a64f543
MS
3729 /* We use value_{,free_to_}mark because it could be a *long*
3730 time before we return to the command level and call
3731 free_all_values. We can't call free_all_values because we
3732 might be in the middle of evaluating a function call. */
c906108c 3733
0cf6dd15 3734 int pc = 0;
9c06b0b4 3735 struct value *mark;
fa4727a6
DJ
3736 struct value *new_val;
3737
3a5c3e22 3738 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
3739 /* Since we don't know the exact trigger address (from
3740 stopped_data_address), just tell the user we've triggered
3741 a mask watchpoint. */
3742 return WP_VALUE_CHANGED;
3743
3744 mark = value_mark ();
0cf6dd15 3745 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3746
4a64f543
MS
3747 /* We use value_equal_contents instead of value_equal because
3748 the latter coerces an array to a pointer, thus comparing just
3749 the address of the array instead of its contents. This is
3750 not what we want. */
fa4727a6 3751 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3752 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3753 {
fa4727a6
DJ
3754 if (new_val != NULL)
3755 {
3756 release_value (new_val);
3757 value_free_to_mark (mark);
3758 }
c906108c
SS
3759 bs->old_val = b->val;
3760 b->val = new_val;
fa4727a6 3761 b->val_valid = 1;
c906108c
SS
3762 return WP_VALUE_CHANGED;
3763 }
3764 else
3765 {
60e1c644 3766 /* Nothing changed. */
c906108c 3767 value_free_to_mark (mark);
c906108c
SS
3768 return WP_VALUE_NOT_CHANGED;
3769 }
3770 }
3771 else
3772 {
79a45e25
PA
3773 struct ui_out *uiout = current_uiout;
3774
c906108c 3775 /* This seems like the only logical thing to do because
c5aa993b
JM
3776 if we temporarily ignored the watchpoint, then when
3777 we reenter the block in which it is valid it contains
3778 garbage (in the case of a function, it may have two
3779 garbage values, one before and one after the prologue).
3780 So we can't even detect the first assignment to it and
3781 watch after that (since the garbage may or may not equal
3782 the first value assigned). */
348d480f
PA
3783 /* We print all the stop information in
3784 breakpoint_ops->print_it, but in this case, by the time we
3785 call breakpoint_ops->print_it this bp will be deleted
3786 already. So we have no choice but print the information
3787 here. */
9dc5e2a9 3788 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3789 ui_out_field_string
3790 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3791 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 3792 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
3793 ui_out_text (uiout,
3794 " deleted because the program has left the block in\n\
8b93c638 3795which its expression is valid.\n");
4ce44c66 3796
cdac0397 3797 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 3798 decref_counted_command_line (&b->base.commands);
d0fb5eae 3799 watchpoint_del_at_next_stop (b);
c906108c
SS
3800
3801 return WP_DELETED;
3802 }
3803}
3804
18a18393 3805/* Return true if it looks like target has stopped due to hitting
348d480f
PA
3806 breakpoint location BL. This function does not check if we should
3807 stop, only if BL explains the stop. */
3808
18a18393 3809static int
6c95b8df
PA
3810bpstat_check_location (const struct bp_location *bl,
3811 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3812{
3813 struct breakpoint *b = bl->owner;
3814
348d480f 3815 /* BL is from an existing breakpoint. */
2bdf28a0
JK
3816 gdb_assert (b != NULL);
3817
348d480f 3818 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
18a18393
VP
3819}
3820
3a5c3e22
PA
3821/* Determine if the watched values have actually changed, and we
3822 should stop. If not, set BS->stop to 0. */
3823
18a18393
VP
3824static void
3825bpstat_check_watchpoint (bpstat bs)
3826{
2bdf28a0 3827 const struct bp_location *bl;
3a5c3e22 3828 struct watchpoint *b;
2bdf28a0
JK
3829
3830 /* BS is built for existing struct breakpoint. */
f431efe5 3831 bl = bs->bp_location_at;
2bdf28a0 3832 gdb_assert (bl != NULL);
3a5c3e22 3833 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 3834 gdb_assert (b != NULL);
18a18393 3835
18a18393 3836 {
18a18393
VP
3837 int must_check_value = 0;
3838
3a5c3e22 3839 if (b->base.type == bp_watchpoint)
18a18393
VP
3840 /* For a software watchpoint, we must always check the
3841 watched value. */
3842 must_check_value = 1;
3843 else if (b->watchpoint_triggered == watch_triggered_yes)
3844 /* We have a hardware watchpoint (read, write, or access)
3845 and the target earlier reported an address watched by
3846 this watchpoint. */
3847 must_check_value = 1;
3848 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 3849 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
3850 /* We were stopped by a hardware watchpoint, but the target could
3851 not report the data address. We must check the watchpoint's
3852 value. Access and read watchpoints are out of luck; without
3853 a data address, we can't figure it out. */
3854 must_check_value = 1;
3a5c3e22 3855
18a18393
VP
3856 if (must_check_value)
3857 {
3e43a32a
MS
3858 char *message
3859 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 3860 b->base.number);
18a18393
VP
3861 struct cleanup *cleanups = make_cleanup (xfree, message);
3862 int e = catch_errors (watchpoint_check, bs, message,
3863 RETURN_MASK_ALL);
3864 do_cleanups (cleanups);
3865 switch (e)
3866 {
3867 case WP_DELETED:
3868 /* We've already printed what needs to be printed. */
3869 bs->print_it = print_it_done;
3870 /* Stop. */
3871 break;
60e1c644
PA
3872 case WP_IGNORE:
3873 bs->print_it = print_it_noop;
3874 bs->stop = 0;
3875 break;
18a18393 3876 case WP_VALUE_CHANGED:
3a5c3e22 3877 if (b->base.type == bp_read_watchpoint)
18a18393 3878 {
85d721b8
PA
3879 /* There are two cases to consider here:
3880
4a64f543 3881 1. We're watching the triggered memory for reads.
85d721b8
PA
3882 In that case, trust the target, and always report
3883 the watchpoint hit to the user. Even though
3884 reads don't cause value changes, the value may
3885 have changed since the last time it was read, and
3886 since we're not trapping writes, we will not see
3887 those, and as such we should ignore our notion of
3888 old value.
3889
4a64f543 3890 2. We're watching the triggered memory for both
85d721b8
PA
3891 reads and writes. There are two ways this may
3892 happen:
3893
4a64f543 3894 2.1. This is a target that can't break on data
85d721b8
PA
3895 reads only, but can break on accesses (reads or
3896 writes), such as e.g., x86. We detect this case
3897 at the time we try to insert read watchpoints.
3898
4a64f543 3899 2.2. Otherwise, the target supports read
85d721b8
PA
3900 watchpoints, but, the user set an access or write
3901 watchpoint watching the same memory as this read
3902 watchpoint.
3903
3904 If we're watching memory writes as well as reads,
3905 ignore watchpoint hits when we find that the
3906 value hasn't changed, as reads don't cause
3907 changes. This still gives false positives when
3908 the program writes the same value to memory as
3909 what there was already in memory (we will confuse
3910 it for a read), but it's much better than
3911 nothing. */
3912
3913 int other_write_watchpoint = 0;
3914
3915 if (bl->watchpoint_type == hw_read)
3916 {
3917 struct breakpoint *other_b;
3918
3919 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
3920 if (other_b->type == bp_hardware_watchpoint
3921 || other_b->type == bp_access_watchpoint)
85d721b8 3922 {
3a5c3e22
PA
3923 struct watchpoint *other_w =
3924 (struct watchpoint *) other_b;
3925
3926 if (other_w->watchpoint_triggered
3927 == watch_triggered_yes)
3928 {
3929 other_write_watchpoint = 1;
3930 break;
3931 }
85d721b8
PA
3932 }
3933 }
3934
3935 if (other_write_watchpoint
3936 || bl->watchpoint_type == hw_access)
3937 {
3938 /* We're watching the same memory for writes,
3939 and the value changed since the last time we
3940 updated it, so this trap must be for a write.
3941 Ignore it. */
3942 bs->print_it = print_it_noop;
3943 bs->stop = 0;
3944 }
18a18393
VP
3945 }
3946 break;
3947 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
3948 if (b->base.type == bp_hardware_watchpoint
3949 || b->base.type == bp_watchpoint)
18a18393
VP
3950 {
3951 /* Don't stop: write watchpoints shouldn't fire if
3952 the value hasn't changed. */
3953 bs->print_it = print_it_noop;
3954 bs->stop = 0;
3955 }
3956 /* Stop. */
3957 break;
3958 default:
3959 /* Can't happen. */
3960 case 0:
3961 /* Error from catch_errors. */
3a5c3e22 3962 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 3963 watchpoint_del_at_next_stop (b);
18a18393
VP
3964 /* We've already printed what needs to be printed. */
3965 bs->print_it = print_it_done;
3966 break;
3967 }
3968 }
3969 else /* must_check_value == 0 */
3970 {
3971 /* This is a case where some watchpoint(s) triggered, but
3972 not at the address of this watchpoint, or else no
3973 watchpoint triggered after all. So don't print
3974 anything for this watchpoint. */
3975 bs->print_it = print_it_noop;
3976 bs->stop = 0;
3977 }
3978 }
3979}
3980
3981
3982/* Check conditions (condition proper, frame, thread and ignore count)
3983 of breakpoint referred to by BS. If we should not stop for this
3984 breakpoint, set BS->stop to 0. */
f431efe5 3985
18a18393
VP
3986static void
3987bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3988{
3989 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
3990 const struct bp_location *bl;
3991 struct breakpoint *b;
3992
3993 /* BS is built for existing struct breakpoint. */
f431efe5 3994 bl = bs->bp_location_at;
2bdf28a0 3995 gdb_assert (bl != NULL);
f431efe5 3996 b = bs->breakpoint_at;
2bdf28a0 3997 gdb_assert (b != NULL);
18a18393
VP
3998
3999 if (frame_id_p (b->frame_id)
edb3359d 4000 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4001 bs->stop = 0;
4002 else if (bs->stop)
4003 {
4004 int value_is_zero = 0;
60e1c644
PA
4005 struct expression *cond;
4006
7371cf6d
PM
4007 /* Evaluate Python breakpoints that have a "stop"
4008 method implemented. */
4009 if (b->py_bp_object)
4010 bs->stop = gdbpy_should_stop (b->py_bp_object);
4011
60e1c644 4012 if (is_watchpoint (b))
3a5c3e22
PA
4013 {
4014 struct watchpoint *w = (struct watchpoint *) b;
4015
4016 cond = w->cond_exp;
4017 }
60e1c644
PA
4018 else
4019 cond = bl->cond;
4020
f431efe5 4021 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4022 {
60e1c644 4023 int within_current_scope = 1;
3a5c3e22 4024 struct watchpoint * w;
60e1c644 4025
c5bc3a77
DJ
4026 /* We use value_mark and value_free_to_mark because it could
4027 be a long time before we return to the command level and
4028 call free_all_values. We can't call free_all_values
4029 because we might be in the middle of evaluating a
4030 function call. */
4031 struct value *mark = value_mark ();
4032
3a5c3e22
PA
4033 if (is_watchpoint (b))
4034 w = (struct watchpoint *) b;
4035 else
4036 w = NULL;
4037
edb3359d
DJ
4038 /* Need to select the frame, with all that implies so that
4039 the conditions will have the right context. Because we
4040 use the frame, we will not see an inlined function's
4041 variables when we arrive at a breakpoint at the start
4042 of the inlined function; the current frame will be the
4043 call site. */
3a5c3e22 4044 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
4045 select_frame (get_current_frame ());
4046 else
4047 {
4048 struct frame_info *frame;
4049
4050 /* For local watchpoint expressions, which particular
4051 instance of a local is being watched matters, so we
4052 keep track of the frame to evaluate the expression
4053 in. To evaluate the condition however, it doesn't
4054 really matter which instantiation of the function
4055 where the condition makes sense triggers the
4056 watchpoint. This allows an expression like "watch
4057 global if q > 10" set in `func', catch writes to
4058 global on all threads that call `func', or catch
4059 writes on all recursive calls of `func' by a single
4060 thread. We simply always evaluate the condition in
4061 the innermost frame that's executing where it makes
4062 sense to evaluate the condition. It seems
4063 intuitive. */
3a5c3e22 4064 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
4065 if (frame != NULL)
4066 select_frame (frame);
4067 else
4068 within_current_scope = 0;
4069 }
4070 if (within_current_scope)
4071 value_is_zero
4072 = catch_errors (breakpoint_cond_eval, cond,
4073 "Error in testing breakpoint condition:\n",
4074 RETURN_MASK_ALL);
4075 else
4076 {
4077 warning (_("Watchpoint condition cannot be tested "
4078 "in the current scope"));
4079 /* If we failed to set the right context for this
4080 watchpoint, unconditionally report it. */
4081 value_is_zero = 0;
4082 }
4a64f543 4083 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4084 value_free_to_mark (mark);
18a18393 4085 }
60e1c644
PA
4086
4087 if (cond && value_is_zero)
18a18393
VP
4088 {
4089 bs->stop = 0;
4090 }
4091 else if (b->thread != -1 && b->thread != thread_id)
4092 {
4093 bs->stop = 0;
4094 }
4095 else if (b->ignore_count > 0)
4096 {
4097 b->ignore_count--;
4098 annotate_ignore_count_change ();
4099 bs->stop = 0;
4a64f543 4100 /* Increase the hit count even though we don't stop. */
18a18393 4101 ++(b->hit_count);
8d3788bd 4102 observer_notify_breakpoint_modified (b);
18a18393
VP
4103 }
4104 }
4105}
4106
4107
9709f61c 4108/* Get a bpstat associated with having just stopped at address
d983da9c 4109 BP_ADDR in thread PTID.
c906108c 4110
d983da9c 4111 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4112 don't understand this stop. Result is a chain of bpstat's such
4113 that:
c906108c 4114
c5aa993b 4115 if we don't understand the stop, the result is a null pointer.
c906108c 4116
c5aa993b 4117 if we understand why we stopped, the result is not null.
c906108c 4118
c5aa993b
JM
4119 Each element of the chain refers to a particular breakpoint or
4120 watchpoint at which we have stopped. (We may have stopped for
4121 several reasons concurrently.)
c906108c 4122
c5aa993b
JM
4123 Each element of the chain has valid next, breakpoint_at,
4124 commands, FIXME??? fields. */
c906108c
SS
4125
4126bpstat
6c95b8df
PA
4127bpstat_stop_status (struct address_space *aspace,
4128 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4129{
0d381245 4130 struct breakpoint *b = NULL;
afe38095 4131 struct bp_location *bl;
20874c92 4132 struct bp_location *loc;
5760d0ab
JK
4133 /* First item of allocated bpstat's. */
4134 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4135 /* Pointer to the last thing in the chain currently. */
5760d0ab 4136 bpstat bs;
20874c92 4137 int ix;
429374b8 4138 int need_remove_insert;
f431efe5 4139 int removed_any;
c906108c 4140
f431efe5
PA
4141 /* First, build the bpstat chain with locations that explain a
4142 target stop, while being careful to not set the target running,
4143 as that may invalidate locations (in particular watchpoint
4144 locations are recreated). Resuming will happen here with
4145 breakpoint conditions or watchpoint expressions that include
4146 inferior function calls. */
c5aa993b 4147
429374b8
JK
4148 ALL_BREAKPOINTS (b)
4149 {
4150 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4151 continue;
a5606eee 4152
429374b8
JK
4153 for (bl = b->loc; bl != NULL; bl = bl->next)
4154 {
4a64f543
MS
4155 /* For hardware watchpoints, we look only at the first
4156 location. The watchpoint_check function will work on the
4157 entire expression, not the individual locations. For
4158 read watchpoints, the watchpoints_triggered function has
4159 checked all locations already. */
429374b8
JK
4160 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4161 break;
18a18393 4162
429374b8
JK
4163 if (bl->shlib_disabled)
4164 continue;
c5aa993b 4165
429374b8
JK
4166 if (!bpstat_check_location (bl, aspace, bp_addr))
4167 continue;
c5aa993b 4168
4a64f543
MS
4169 /* Come here if it's a watchpoint, or if the break address
4170 matches. */
c5aa993b 4171
4a64f543
MS
4172 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4173 explain stop. */
c5aa993b 4174
f431efe5
PA
4175 /* Assume we stop. Should we find a watchpoint that is not
4176 actually triggered, or if the condition of the breakpoint
4177 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4178 bs->stop = 1;
4179 bs->print = 1;
d983da9c 4180
f431efe5
PA
4181 /* If this is a scope breakpoint, mark the associated
4182 watchpoint as triggered so that we will handle the
4183 out-of-scope event. We'll get to the watchpoint next
4184 iteration. */
d0fb5eae 4185 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4186 {
4187 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4188
4189 w->watchpoint_triggered = watch_triggered_yes;
4190 }
f431efe5
PA
4191 }
4192 }
4193
4194 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4195 {
f1310107 4196 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4197 {
5760d0ab 4198 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4199 /* For hits of moribund locations, we should just proceed. */
4200 bs->stop = 0;
4201 bs->print = 0;
4202 bs->print_it = print_it_noop;
4203 }
4204 }
4205
f431efe5
PA
4206 /* Now go through the locations that caused the target to stop, and
4207 check whether we're interested in reporting this stop to higher
4208 layers, or whether we should resume the target transparently. */
4209
4210 removed_any = 0;
4211
5760d0ab 4212 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4213 {
4214 if (!bs->stop)
4215 continue;
4216
f431efe5 4217 b = bs->breakpoint_at;
348d480f
PA
4218 b->ops->check_status (bs);
4219 if (bs->stop)
28010a5d 4220 {
348d480f 4221 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4222
429374b8
JK
4223 if (bs->stop)
4224 {
4225 ++(b->hit_count);
8d3788bd 4226 observer_notify_breakpoint_modified (b);
c906108c 4227
4a64f543 4228 /* We will stop here. */
429374b8
JK
4229 if (b->disposition == disp_disable)
4230 {
4231 if (b->enable_state != bp_permanent)
4232 b->enable_state = bp_disabled;
f431efe5 4233 removed_any = 1;
429374b8
JK
4234 }
4235 if (b->silent)
4236 bs->print = 0;
4237 bs->commands = b->commands;
9add0f1b 4238 incref_counted_command_line (bs->commands);
abf85f46
JK
4239 if (command_line_is_silent (bs->commands
4240 ? bs->commands->commands : NULL))
4241 bs->print = 0;
429374b8
JK
4242 }
4243
e5dd4106 4244 /* Print nothing for this entry if we don't stop or don't print. */
429374b8
JK
4245 if (bs->stop == 0 || bs->print == 0)
4246 bs->print_it = print_it_noop;
348d480f 4247 }
429374b8 4248 }
876fa593 4249
d983da9c
DJ
4250 /* If we aren't stopping, the value of some hardware watchpoint may
4251 not have changed, but the intermediate memory locations we are
4252 watching may have. Don't bother if we're stopping; this will get
4253 done later. */
d832cb68 4254 need_remove_insert = 0;
5760d0ab
JK
4255 if (! bpstat_causes_stop (bs_head))
4256 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4257 if (!bs->stop
f431efe5
PA
4258 && bs->breakpoint_at
4259 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4260 {
3a5c3e22
PA
4261 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4262
4263 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 4264 need_remove_insert = 1;
d983da9c
DJ
4265 }
4266
d832cb68 4267 if (need_remove_insert)
2d134ed3 4268 update_global_location_list (1);
f431efe5
PA
4269 else if (removed_any)
4270 update_global_location_list (0);
d832cb68 4271
5760d0ab 4272 return bs_head;
c906108c 4273}
628fe4e4
JK
4274
4275static void
4276handle_jit_event (void)
4277{
4278 struct frame_info *frame;
4279 struct gdbarch *gdbarch;
4280
4281 /* Switch terminal for any messages produced by
4282 breakpoint_re_set. */
4283 target_terminal_ours_for_output ();
4284
4285 frame = get_current_frame ();
4286 gdbarch = get_frame_arch (frame);
4287
4288 jit_event_handler (gdbarch);
4289
4290 target_terminal_inferior ();
4291}
4292
4293/* Prepare WHAT final decision for infrun. */
4294
4295/* Decide what infrun needs to do with this bpstat. */
4296
c906108c 4297struct bpstat_what
0e30163f 4298bpstat_what (bpstat bs_head)
c906108c 4299{
c906108c 4300 struct bpstat_what retval;
628fe4e4
JK
4301 /* We need to defer calling `solib_add', as adding new symbols
4302 resets breakpoints, which in turn deletes breakpoint locations,
4303 and hence may clear unprocessed entries in the BS chain. */
4304 int shlib_event = 0;
4305 int jit_event = 0;
0e30163f 4306 bpstat bs;
c906108c 4307
628fe4e4 4308 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4309 retval.call_dummy = STOP_NONE;
186c406b 4310 retval.is_longjmp = 0;
628fe4e4 4311
0e30163f 4312 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4313 {
628fe4e4
JK
4314 /* Extract this BS's action. After processing each BS, we check
4315 if its action overrides all we've seem so far. */
4316 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4317 enum bptype bptype;
4318
c906108c 4319 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4320 {
4321 /* I suspect this can happen if it was a momentary
4322 breakpoint which has since been deleted. */
4323 bptype = bp_none;
4324 }
20874c92 4325 else
f431efe5 4326 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4327
4328 switch (bptype)
c906108c
SS
4329 {
4330 case bp_none:
628fe4e4 4331 break;
c906108c
SS
4332 case bp_breakpoint:
4333 case bp_hardware_breakpoint:
4334 case bp_until:
4335 case bp_finish:
4336 if (bs->stop)
4337 {
4338 if (bs->print)
628fe4e4 4339 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4340 else
628fe4e4 4341 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4342 }
4343 else
628fe4e4 4344 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4345 break;
4346 case bp_watchpoint:
4347 case bp_hardware_watchpoint:
4348 case bp_read_watchpoint:
4349 case bp_access_watchpoint:
4350 if (bs->stop)
4351 {
4352 if (bs->print)
628fe4e4 4353 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4354 else
628fe4e4 4355 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4356 }
4357 else
628fe4e4
JK
4358 {
4359 /* There was a watchpoint, but we're not stopping.
4360 This requires no further action. */
4361 }
c906108c
SS
4362 break;
4363 case bp_longjmp:
186c406b 4364 case bp_exception:
628fe4e4 4365 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4366 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4367 break;
4368 case bp_longjmp_resume:
186c406b 4369 case bp_exception_resume:
628fe4e4 4370 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4371 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4372 break;
4373 case bp_step_resume:
4374 if (bs->stop)
628fe4e4
JK
4375 this_action = BPSTAT_WHAT_STEP_RESUME;
4376 else
c906108c 4377 {
628fe4e4
JK
4378 /* It is for the wrong frame. */
4379 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4380 }
c906108c 4381 break;
2c03e5be
PA
4382 case bp_hp_step_resume:
4383 if (bs->stop)
4384 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4385 else
4386 {
4387 /* It is for the wrong frame. */
4388 this_action = BPSTAT_WHAT_SINGLE;
4389 }
4390 break;
c906108c 4391 case bp_watchpoint_scope:
c4093a6a 4392 case bp_thread_event:
1900040c 4393 case bp_overlay_event:
0fd8e87f 4394 case bp_longjmp_master:
aa7d318d 4395 case bp_std_terminate_master:
186c406b 4396 case bp_exception_master:
628fe4e4 4397 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4398 break;
ce78b96d 4399 case bp_catchpoint:
c5aa993b
JM
4400 if (bs->stop)
4401 {
4402 if (bs->print)
628fe4e4 4403 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4404 else
628fe4e4 4405 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4406 }
4407 else
628fe4e4
JK
4408 {
4409 /* There was a catchpoint, but we're not stopping.
4410 This requires no further action. */
4411 }
4412 break;
4413 case bp_shlib_event:
4414 shlib_event = 1;
4415
4416 /* If requested, stop when the dynamic linker notifies GDB
4417 of events. This allows the user to get control and place
4418 breakpoints in initializer routines for dynamically
4419 loaded objects (among other things). */
4420 if (stop_on_solib_events)
4421 this_action = BPSTAT_WHAT_STOP_NOISY;
4422 else
4423 this_action = BPSTAT_WHAT_SINGLE;
4424 break;
4425 case bp_jit_event:
4426 jit_event = 1;
4427 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4428 break;
c906108c 4429 case bp_call_dummy:
53a5351d
JM
4430 /* Make sure the action is stop (silent or noisy),
4431 so infrun.c pops the dummy frame. */
aa7d318d 4432 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4433 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4434 break;
4435 case bp_std_terminate:
4436 /* Make sure the action is stop (silent or noisy),
4437 so infrun.c pops the dummy frame. */
aa7d318d 4438 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4439 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4440 break;
1042e4c0 4441 case bp_tracepoint:
7a697b8d 4442 case bp_fast_tracepoint:
0fb4aa4b 4443 case bp_static_tracepoint:
1042e4c0
SS
4444 /* Tracepoint hits should not be reported back to GDB, and
4445 if one got through somehow, it should have been filtered
4446 out already. */
4447 internal_error (__FILE__, __LINE__,
7a697b8d 4448 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4449 break;
4450 case bp_gnu_ifunc_resolver:
4451 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4452 this_action = BPSTAT_WHAT_SINGLE;
4453 break;
4454 case bp_gnu_ifunc_resolver_return:
4455 /* The breakpoint will be removed, execution will restart from the
4456 PC of the former breakpoint. */
4457 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4458 break;
628fe4e4
JK
4459 default:
4460 internal_error (__FILE__, __LINE__,
4461 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4462 }
628fe4e4
JK
4463
4464 retval.main_action = max (retval.main_action, this_action);
c906108c 4465 }
628fe4e4 4466
0e30163f
JK
4467 /* These operations may affect the bs->breakpoint_at state so they are
4468 delayed after MAIN_ACTION is decided above. */
4469
628fe4e4
JK
4470 if (shlib_event)
4471 {
4472 if (debug_infrun)
4473 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4474
4475 /* Check for any newly added shared libraries if we're supposed
4476 to be adding them automatically. */
4477
4478 /* Switch terminal for any messages produced by
4479 breakpoint_re_set. */
4480 target_terminal_ours_for_output ();
4481
4482#ifdef SOLIB_ADD
4483 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4484#else
4485 solib_add (NULL, 0, &current_target, auto_solib_add);
4486#endif
4487
4488 target_terminal_inferior ();
4489 }
4490
4491 if (jit_event)
4492 {
4493 if (debug_infrun)
4494 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4495
4496 handle_jit_event ();
4497 }
4498
0e30163f
JK
4499 for (bs = bs_head; bs != NULL; bs = bs->next)
4500 {
4501 struct breakpoint *b = bs->breakpoint_at;
4502
4503 if (b == NULL)
4504 continue;
4505 switch (b->type)
4506 {
4507 case bp_gnu_ifunc_resolver:
4508 gnu_ifunc_resolver_stop (b);
4509 break;
4510 case bp_gnu_ifunc_resolver_return:
4511 gnu_ifunc_resolver_return_stop (b);
4512 break;
4513 }
4514 }
4515
c906108c
SS
4516 return retval;
4517}
4518
4519/* Nonzero if we should step constantly (e.g. watchpoints on machines
4520 without hardware support). This isn't related to a specific bpstat,
4521 just to things like whether watchpoints are set. */
4522
c5aa993b 4523int
fba45db2 4524bpstat_should_step (void)
c906108c
SS
4525{
4526 struct breakpoint *b;
cc59ec59 4527
c906108c 4528 ALL_BREAKPOINTS (b)
717a8278 4529 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4530 return 1;
c906108c
SS
4531 return 0;
4532}
4533
67822962
PA
4534int
4535bpstat_causes_stop (bpstat bs)
4536{
4537 for (; bs != NULL; bs = bs->next)
4538 if (bs->stop)
4539 return 1;
4540
4541 return 0;
4542}
4543
c906108c 4544\f
c5aa993b 4545
170b53b2
UW
4546/* Compute a string of spaces suitable to indent the next line
4547 so it starts at the position corresponding to the table column
4548 named COL_NAME in the currently active table of UIOUT. */
4549
4550static char *
4551wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4552{
4553 static char wrap_indent[80];
4554 int i, total_width, width, align;
4555 char *text;
4556
4557 total_width = 0;
4558 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4559 {
4560 if (strcmp (text, col_name) == 0)
4561 {
4562 gdb_assert (total_width < sizeof wrap_indent);
4563 memset (wrap_indent, ' ', total_width);
4564 wrap_indent[total_width] = 0;
4565
4566 return wrap_indent;
4567 }
4568
4569 total_width += width + 1;
4570 }
4571
4572 return NULL;
4573}
4574
859825b8
JK
4575/* Print the LOC location out of the list of B->LOC locations. */
4576
170b53b2
UW
4577static void
4578print_breakpoint_location (struct breakpoint *b,
4579 struct bp_location *loc)
0d381245 4580{
79a45e25 4581 struct ui_out *uiout = current_uiout;
6c95b8df
PA
4582 struct cleanup *old_chain = save_current_program_space ();
4583
859825b8
JK
4584 if (loc != NULL && loc->shlib_disabled)
4585 loc = NULL;
4586
6c95b8df
PA
4587 if (loc != NULL)
4588 set_current_program_space (loc->pspace);
4589
56435ebe
TT
4590 if (b->display_canonical)
4591 ui_out_field_string (uiout, "what", b->addr_string);
f8eba3c6 4592 else if (loc && loc->source_file)
0d381245
VP
4593 {
4594 struct symbol *sym
4595 = find_pc_sect_function (loc->address, loc->section);
4596 if (sym)
4597 {
4598 ui_out_text (uiout, "in ");
4599 ui_out_field_string (uiout, "func",
4600 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4601 ui_out_text (uiout, " ");
4602 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4603 ui_out_text (uiout, "at ");
0d381245 4604 }
f8eba3c6 4605 ui_out_field_string (uiout, "file", loc->source_file);
0d381245
VP
4606 ui_out_text (uiout, ":");
4607
4608 if (ui_out_is_mi_like_p (uiout))
4609 {
4610 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4611 char *fullname = symtab_to_fullname (sal.symtab);
4612
4613 if (fullname)
4614 ui_out_field_string (uiout, "fullname", fullname);
4615 }
4616
f8eba3c6 4617 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 4618 }
859825b8 4619 else if (loc)
0d381245 4620 {
170b53b2
UW
4621 struct ui_stream *stb = ui_out_stream_new (uiout);
4622 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4623
22e722e1
DJ
4624 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4625 demangle, "");
0d381245 4626 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4627
4628 do_cleanups (stb_chain);
0d381245 4629 }
859825b8
JK
4630 else
4631 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4632
4633 do_cleanups (old_chain);
0d381245
VP
4634}
4635
269b11a2
PA
4636static const char *
4637bptype_string (enum bptype type)
c906108c 4638{
c4093a6a
JM
4639 struct ep_type_description
4640 {
4641 enum bptype type;
4642 char *description;
4643 };
4644 static struct ep_type_description bptypes[] =
c906108c 4645 {
c5aa993b
JM
4646 {bp_none, "?deleted?"},
4647 {bp_breakpoint, "breakpoint"},
c906108c 4648 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4649 {bp_until, "until"},
4650 {bp_finish, "finish"},
4651 {bp_watchpoint, "watchpoint"},
c906108c 4652 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4653 {bp_read_watchpoint, "read watchpoint"},
4654 {bp_access_watchpoint, "acc watchpoint"},
4655 {bp_longjmp, "longjmp"},
4656 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4657 {bp_exception, "exception"},
4658 {bp_exception_resume, "exception resume"},
c5aa993b 4659 {bp_step_resume, "step resume"},
2c03e5be 4660 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
4661 {bp_watchpoint_scope, "watchpoint scope"},
4662 {bp_call_dummy, "call dummy"},
aa7d318d 4663 {bp_std_terminate, "std::terminate"},
c5aa993b 4664 {bp_shlib_event, "shlib events"},
c4093a6a 4665 {bp_thread_event, "thread events"},
1900040c 4666 {bp_overlay_event, "overlay events"},
0fd8e87f 4667 {bp_longjmp_master, "longjmp master"},
aa7d318d 4668 {bp_std_terminate_master, "std::terminate master"},
186c406b 4669 {bp_exception_master, "exception master"},
ce78b96d 4670 {bp_catchpoint, "catchpoint"},
1042e4c0 4671 {bp_tracepoint, "tracepoint"},
7a697b8d 4672 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4673 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4674 {bp_jit_event, "jit events"},
0e30163f
JK
4675 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4676 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4677 };
269b11a2
PA
4678
4679 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4680 || ((int) type != bptypes[(int) type].type))
4681 internal_error (__FILE__, __LINE__,
4682 _("bptypes table does not describe type #%d."),
4683 (int) type);
4684
4685 return bptypes[(int) type].description;
4686}
4687
4688/* Print B to gdb_stdout. */
4689
4690static void
4691print_one_breakpoint_location (struct breakpoint *b,
4692 struct bp_location *loc,
4693 int loc_number,
4694 struct bp_location **last_loc,
269b11a2
PA
4695 int allflag)
4696{
4697 struct command_line *l;
c2c6d25f 4698 static char bpenables[] = "nynny";
c906108c 4699
79a45e25 4700 struct ui_out *uiout = current_uiout;
0d381245
VP
4701 int header_of_multiple = 0;
4702 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4703 struct value_print_options opts;
4704
4705 get_user_print_options (&opts);
0d381245
VP
4706
4707 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4708 /* See comment in print_one_breakpoint concerning treatment of
4709 breakpoints with single disabled location. */
0d381245
VP
4710 if (loc == NULL
4711 && (b->loc != NULL
4712 && (b->loc->next != NULL || !b->loc->enabled)))
4713 header_of_multiple = 1;
4714 if (loc == NULL)
4715 loc = b->loc;
4716
c4093a6a
JM
4717 annotate_record ();
4718
4719 /* 1 */
4720 annotate_field (0);
0d381245
VP
4721 if (part_of_multiple)
4722 {
4723 char *formatted;
0c6773c1 4724 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4725 ui_out_field_string (uiout, "number", formatted);
4726 xfree (formatted);
4727 }
4728 else
4729 {
4730 ui_out_field_int (uiout, "number", b->number);
4731 }
c4093a6a
JM
4732
4733 /* 2 */
4734 annotate_field (1);
0d381245
VP
4735 if (part_of_multiple)
4736 ui_out_field_skip (uiout, "type");
269b11a2
PA
4737 else
4738 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4739
4740 /* 3 */
4741 annotate_field (2);
0d381245
VP
4742 if (part_of_multiple)
4743 ui_out_field_skip (uiout, "disp");
4744 else
2cec12e5 4745 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4746
c4093a6a
JM
4747
4748 /* 4 */
4749 annotate_field (3);
0d381245 4750 if (part_of_multiple)
54e52265 4751 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4752 else
4a64f543
MS
4753 ui_out_field_fmt (uiout, "enabled", "%c",
4754 bpenables[(int) b->enable_state]);
54e52265 4755 ui_out_spaces (uiout, 2);
0d381245 4756
c4093a6a
JM
4757
4758 /* 5 and 6 */
3086aeae 4759 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4760 {
4a64f543
MS
4761 /* Although the print_one can possibly print all locations,
4762 calling it here is not likely to get any nice result. So,
4763 make sure there's just one location. */
0d381245 4764 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4765 b->ops->print_one (b, last_loc);
0d381245 4766 }
3086aeae
DJ
4767 else
4768 switch (b->type)
4769 {
4770 case bp_none:
4771 internal_error (__FILE__, __LINE__,
e2e0b3e5 4772 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4773 break;
c906108c 4774
3086aeae
DJ
4775 case bp_watchpoint:
4776 case bp_hardware_watchpoint:
4777 case bp_read_watchpoint:
4778 case bp_access_watchpoint:
3a5c3e22
PA
4779 {
4780 struct watchpoint *w = (struct watchpoint *) b;
4781
4782 /* Field 4, the address, is omitted (which makes the columns
4783 not line up too nicely with the headers, but the effect
4784 is relatively readable). */
4785 if (opts.addressprint)
4786 ui_out_field_skip (uiout, "addr");
4787 annotate_field (5);
4788 ui_out_field_string (uiout, "what", w->exp_string);
4789 }
3086aeae
DJ
4790 break;
4791
3086aeae
DJ
4792 case bp_breakpoint:
4793 case bp_hardware_breakpoint:
4794 case bp_until:
4795 case bp_finish:
4796 case bp_longjmp:
4797 case bp_longjmp_resume:
186c406b
TT
4798 case bp_exception:
4799 case bp_exception_resume:
3086aeae 4800 case bp_step_resume:
2c03e5be 4801 case bp_hp_step_resume:
3086aeae
DJ
4802 case bp_watchpoint_scope:
4803 case bp_call_dummy:
aa7d318d 4804 case bp_std_terminate:
3086aeae
DJ
4805 case bp_shlib_event:
4806 case bp_thread_event:
4807 case bp_overlay_event:
0fd8e87f 4808 case bp_longjmp_master:
aa7d318d 4809 case bp_std_terminate_master:
186c406b 4810 case bp_exception_master:
1042e4c0 4811 case bp_tracepoint:
7a697b8d 4812 case bp_fast_tracepoint:
0fb4aa4b 4813 case bp_static_tracepoint:
4efc6507 4814 case bp_jit_event:
0e30163f
JK
4815 case bp_gnu_ifunc_resolver:
4816 case bp_gnu_ifunc_resolver_return:
79a45b7d 4817 if (opts.addressprint)
3086aeae
DJ
4818 {
4819 annotate_field (4);
54e52265 4820 if (header_of_multiple)
0d381245 4821 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4822 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4823 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4824 else
5af949e3
UW
4825 ui_out_field_core_addr (uiout, "addr",
4826 loc->gdbarch, loc->address);
3086aeae
DJ
4827 }
4828 annotate_field (5);
0d381245 4829 if (!header_of_multiple)
170b53b2 4830 print_breakpoint_location (b, loc);
0d381245 4831 if (b->loc)
a6d9a66e 4832 *last_loc = b->loc;
3086aeae
DJ
4833 break;
4834 }
c906108c 4835
6c95b8df
PA
4836
4837 /* For backward compatibility, don't display inferiors unless there
4838 are several. */
4839 if (loc != NULL
4840 && !header_of_multiple
4841 && (allflag
4842 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4843 && (number_of_program_spaces () > 1
4844 || number_of_inferiors () > 1)
4a64f543
MS
4845 /* LOC is for existing B, it cannot be in
4846 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4847 && loc->owner->type != bp_catchpoint)))
4848 {
4849 struct inferior *inf;
4850 int first = 1;
4851
4852 for (inf = inferior_list; inf != NULL; inf = inf->next)
4853 {
4854 if (inf->pspace == loc->pspace)
4855 {
4856 if (first)
4857 {
4858 first = 0;
4859 ui_out_text (uiout, " inf ");
4860 }
4861 else
4862 ui_out_text (uiout, ", ");
4863 ui_out_text (uiout, plongest (inf->num));
4864 }
4865 }
4866 }
4867
4a306c9a 4868 if (!part_of_multiple)
c4093a6a 4869 {
4a306c9a
JB
4870 if (b->thread != -1)
4871 {
4872 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4873 "stop only in" line a little further down. */
4a306c9a
JB
4874 ui_out_text (uiout, " thread ");
4875 ui_out_field_int (uiout, "thread", b->thread);
4876 }
4877 else if (b->task != 0)
4878 {
4879 ui_out_text (uiout, " task ");
4880 ui_out_field_int (uiout, "task", b->task);
4881 }
c4093a6a 4882 }
f1310107 4883
8b93c638 4884 ui_out_text (uiout, "\n");
f1310107 4885
348d480f 4886 if (!part_of_multiple)
f1310107
TJB
4887 b->ops->print_one_detail (b, uiout);
4888
0d381245 4889 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4890 {
4891 annotate_field (6);
8b93c638 4892 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 4893 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 4894 the frame ID. */
5af949e3
UW
4895 ui_out_field_core_addr (uiout, "frame",
4896 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4897 ui_out_text (uiout, "\n");
c4093a6a
JM
4898 }
4899
28010a5d 4900 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
4901 {
4902 annotate_field (7);
d77f58be 4903 if (is_tracepoint (b))
1042e4c0
SS
4904 ui_out_text (uiout, "\ttrace only if ");
4905 else
4906 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4907 ui_out_field_string (uiout, "cond", b->cond_string);
4908 ui_out_text (uiout, "\n");
4909 }
4910
0d381245 4911 if (!part_of_multiple && b->thread != -1)
c4093a6a 4912 {
4a64f543 4913 /* FIXME should make an annotation for this. */
8b93c638
JM
4914 ui_out_text (uiout, "\tstop only in thread ");
4915 ui_out_field_int (uiout, "thread", b->thread);
4916 ui_out_text (uiout, "\n");
c4093a6a
JM
4917 }
4918
63c715c6 4919 if (!part_of_multiple && b->hit_count)
c4093a6a 4920 {
4a64f543 4921 /* FIXME should make an annotation for this. */
8b93c638
JM
4922 if (ep_is_catchpoint (b))
4923 ui_out_text (uiout, "\tcatchpoint");
f196051f
SS
4924 else if (is_tracepoint (b))
4925 ui_out_text (uiout, "\ttracepoint");
8b93c638
JM
4926 else
4927 ui_out_text (uiout, "\tbreakpoint");
4928 ui_out_text (uiout, " already hit ");
4929 ui_out_field_int (uiout, "times", b->hit_count);
4930 if (b->hit_count == 1)
4931 ui_out_text (uiout, " time\n");
4932 else
4933 ui_out_text (uiout, " times\n");
c4093a6a
JM
4934 }
4935
4a64f543
MS
4936 /* Output the count also if it is zero, but only if this is mi.
4937 FIXME: Should have a better test for this. */
9dc5e2a9 4938 if (ui_out_is_mi_like_p (uiout))
63c715c6 4939 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4940 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4941
0d381245 4942 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4943 {
4944 annotate_field (8);
8b93c638
JM
4945 ui_out_text (uiout, "\tignore next ");
4946 ui_out_field_int (uiout, "ignore", b->ignore_count);
4947 ui_out_text (uiout, " hits\n");
c4093a6a 4948 }
059fb39f 4949
f196051f
SS
4950 if (!part_of_multiple && is_tracepoint (b))
4951 {
4952 struct tracepoint *tp = (struct tracepoint *) b;
4953
4954 if (tp->traceframe_usage)
4955 {
4956 ui_out_text (uiout, "\ttrace buffer usage ");
4957 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
4958 ui_out_text (uiout, " bytes\n");
4959 }
4960 }
4961
9add0f1b 4962 l = b->commands ? b->commands->commands : NULL;
059fb39f 4963 if (!part_of_multiple && l)
c4093a6a 4964 {
3b31d625
EZ
4965 struct cleanup *script_chain;
4966
c4093a6a 4967 annotate_field (9);
3b31d625 4968 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4969 print_command_lines (uiout, l, 4);
3b31d625 4970 do_cleanups (script_chain);
c4093a6a 4971 }
d24317b4 4972
d9b3f62e 4973 if (is_tracepoint (b))
1042e4c0 4974 {
d9b3f62e
PA
4975 struct tracepoint *t = (struct tracepoint *) b;
4976
4977 if (!part_of_multiple && t->pass_count)
4978 {
4979 annotate_field (10);
4980 ui_out_text (uiout, "\tpass count ");
4981 ui_out_field_int (uiout, "pass", t->pass_count);
4982 ui_out_text (uiout, " \n");
4983 }
1042e4c0
SS
4984 }
4985
d24317b4
VP
4986 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4987 {
3a5c3e22
PA
4988 if (is_watchpoint (b))
4989 {
4990 struct watchpoint *w = (struct watchpoint *) b;
4991
4992 ui_out_field_string (uiout, "original-location", w->exp_string);
4993 }
4994 else if (b->addr_string)
d24317b4 4995 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 4996 }
c4093a6a 4997}
c5aa993b 4998
0d381245
VP
4999static void
5000print_one_breakpoint (struct breakpoint *b,
4a64f543 5001 struct bp_location **last_loc,
6c95b8df 5002 int allflag)
0d381245 5003{
8d3788bd 5004 struct cleanup *bkpt_chain;
79a45e25 5005 struct ui_out *uiout = current_uiout;
8d3788bd
VP
5006
5007 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5008
12c5a436 5009 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 5010 do_cleanups (bkpt_chain);
0d381245
VP
5011
5012 /* If this breakpoint has custom print function,
5013 it's already printed. Otherwise, print individual
5014 locations, if any. */
5015 if (b->ops == NULL || b->ops->print_one == NULL)
5016 {
4a64f543
MS
5017 /* If breakpoint has a single location that is disabled, we
5018 print it as if it had several locations, since otherwise it's
5019 hard to represent "breakpoint enabled, location disabled"
5020 situation.
5021
5022 Note that while hardware watchpoints have several locations
a3be7890 5023 internally, that's not a property exposed to user. */
0d381245 5024 if (b->loc
a5606eee 5025 && !is_hardware_watchpoint (b)
8d3788bd 5026 && (b->loc->next || !b->loc->enabled))
0d381245
VP
5027 {
5028 struct bp_location *loc;
5029 int n = 1;
8d3788bd 5030
0d381245 5031 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5032 {
5033 struct cleanup *inner2 =
5034 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5035 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5036 do_cleanups (inner2);
5037 }
0d381245
VP
5038 }
5039 }
5040}
5041
a6d9a66e
UW
5042static int
5043breakpoint_address_bits (struct breakpoint *b)
5044{
5045 int print_address_bits = 0;
5046 struct bp_location *loc;
5047
5048 for (loc = b->loc; loc; loc = loc->next)
5049 {
c7437ca6
PA
5050 int addr_bit;
5051
5052 /* Software watchpoints that aren't watching memory don't have
5053 an address to print. */
5054 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5055 continue;
5056
5057 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5058 if (addr_bit > print_address_bits)
5059 print_address_bits = addr_bit;
5060 }
5061
5062 return print_address_bits;
5063}
0d381245 5064
c4093a6a
JM
5065struct captured_breakpoint_query_args
5066 {
5067 int bnum;
5068 };
c5aa993b 5069
c4093a6a 5070static int
2b65245e 5071do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5072{
5073 struct captured_breakpoint_query_args *args = data;
52f0bd74 5074 struct breakpoint *b;
a6d9a66e 5075 struct bp_location *dummy_loc = NULL;
cc59ec59 5076
c4093a6a
JM
5077 ALL_BREAKPOINTS (b)
5078 {
5079 if (args->bnum == b->number)
c5aa993b 5080 {
12c5a436 5081 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5082 return GDB_RC_OK;
c5aa993b 5083 }
c4093a6a
JM
5084 }
5085 return GDB_RC_NONE;
5086}
c5aa993b 5087
c4093a6a 5088enum gdb_rc
4a64f543
MS
5089gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5090 char **error_message)
c4093a6a
JM
5091{
5092 struct captured_breakpoint_query_args args;
cc59ec59 5093
c4093a6a
JM
5094 args.bnum = bnum;
5095 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5096 an error. */
b0b13bb4
DJ
5097 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5098 error_message, RETURN_MASK_ALL) < 0)
5099 return GDB_RC_FAIL;
5100 else
5101 return GDB_RC_OK;
c4093a6a 5102}
c5aa993b 5103
09d682a4
TT
5104/* Return true if this breakpoint was set by the user, false if it is
5105 internal or momentary. */
5106
5107int
5108user_breakpoint_p (struct breakpoint *b)
5109{
46c6471b 5110 return b->number > 0;
09d682a4
TT
5111}
5112
7f3b0473 5113/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5114 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5115 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5116 FILTER is non-NULL, call it on each breakpoint and only include the
5117 ones for which it returns non-zero. Return the total number of
5118 breakpoints listed. */
c906108c 5119
d77f58be 5120static int
e5a67952 5121breakpoint_1 (char *args, int allflag,
4a64f543 5122 int (*filter) (const struct breakpoint *))
c4093a6a 5123{
52f0bd74 5124 struct breakpoint *b;
a6d9a66e 5125 struct bp_location *last_loc = NULL;
7f3b0473 5126 int nr_printable_breakpoints;
3b31d625 5127 struct cleanup *bkpttbl_chain;
79a45b7d 5128 struct value_print_options opts;
a6d9a66e 5129 int print_address_bits = 0;
269b11a2 5130 int print_type_col_width = 14;
79a45e25 5131 struct ui_out *uiout = current_uiout;
269b11a2 5132
79a45b7d
TT
5133 get_user_print_options (&opts);
5134
4a64f543
MS
5135 /* Compute the number of rows in the table, as well as the size
5136 required for address fields. */
7f3b0473
AC
5137 nr_printable_breakpoints = 0;
5138 ALL_BREAKPOINTS (b)
e5a67952
MS
5139 {
5140 /* If we have a filter, only list the breakpoints it accepts. */
5141 if (filter && !filter (b))
5142 continue;
5143
5144 /* If we have an "args" string, it is a list of breakpoints to
5145 accept. Skip the others. */
5146 if (args != NULL && *args != '\0')
5147 {
5148 if (allflag && parse_and_eval_long (args) != b->number)
5149 continue;
5150 if (!allflag && !number_is_in_list (args, b->number))
5151 continue;
5152 }
269b11a2 5153
e5a67952
MS
5154 if (allflag || user_breakpoint_p (b))
5155 {
5156 int addr_bit, type_len;
a6d9a66e 5157
e5a67952
MS
5158 addr_bit = breakpoint_address_bits (b);
5159 if (addr_bit > print_address_bits)
5160 print_address_bits = addr_bit;
269b11a2 5161
e5a67952
MS
5162 type_len = strlen (bptype_string (b->type));
5163 if (type_len > print_type_col_width)
5164 print_type_col_width = type_len;
5165
5166 nr_printable_breakpoints++;
5167 }
5168 }
7f3b0473 5169
79a45b7d 5170 if (opts.addressprint)
3b31d625 5171 bkpttbl_chain
3e43a32a
MS
5172 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5173 nr_printable_breakpoints,
3b31d625 5174 "BreakpointTable");
8b93c638 5175 else
3b31d625 5176 bkpttbl_chain
3e43a32a
MS
5177 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5178 nr_printable_breakpoints,
3b31d625 5179 "BreakpointTable");
8b93c638 5180
7f3b0473 5181 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5182 annotate_breakpoints_headers ();
5183 if (nr_printable_breakpoints > 0)
5184 annotate_field (0);
4a64f543 5185 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5186 if (nr_printable_breakpoints > 0)
5187 annotate_field (1);
269b11a2 5188 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5189 "type", "Type"); /* 2 */
d7faa9e7
AC
5190 if (nr_printable_breakpoints > 0)
5191 annotate_field (2);
4a64f543 5192 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5193 if (nr_printable_breakpoints > 0)
5194 annotate_field (3);
54e52265 5195 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5196 if (opts.addressprint)
e5a67952
MS
5197 {
5198 if (nr_printable_breakpoints > 0)
5199 annotate_field (4);
5200 if (print_address_bits <= 32)
5201 ui_out_table_header (uiout, 10, ui_left,
5202 "addr", "Address"); /* 5 */
5203 else
5204 ui_out_table_header (uiout, 18, ui_left,
5205 "addr", "Address"); /* 5 */
5206 }
d7faa9e7
AC
5207 if (nr_printable_breakpoints > 0)
5208 annotate_field (5);
5209 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5210 ui_out_table_body (uiout);
5211 if (nr_printable_breakpoints > 0)
5212 annotate_breakpoints_table ();
7f3b0473 5213
c4093a6a 5214 ALL_BREAKPOINTS (b)
e5a67952
MS
5215 {
5216 QUIT;
5217 /* If we have a filter, only list the breakpoints it accepts. */
5218 if (filter && !filter (b))
5219 continue;
5220
5221 /* If we have an "args" string, it is a list of breakpoints to
5222 accept. Skip the others. */
5223
5224 if (args != NULL && *args != '\0')
5225 {
5226 if (allflag) /* maintenance info breakpoint */
5227 {
5228 if (parse_and_eval_long (args) != b->number)
5229 continue;
5230 }
5231 else /* all others */
5232 {
5233 if (!number_is_in_list (args, b->number))
5234 continue;
5235 }
5236 }
5237 /* We only print out user settable breakpoints unless the
5238 allflag is set. */
5239 if (allflag || user_breakpoint_p (b))
12c5a436 5240 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5241 }
5242
3b31d625 5243 do_cleanups (bkpttbl_chain);
698384cd 5244
7f3b0473 5245 if (nr_printable_breakpoints == 0)
c906108c 5246 {
4a64f543
MS
5247 /* If there's a filter, let the caller decide how to report
5248 empty list. */
d77f58be
SS
5249 if (!filter)
5250 {
e5a67952 5251 if (args == NULL || *args == '\0')
d77f58be
SS
5252 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5253 else
4a64f543 5254 ui_out_message (uiout, 0,
e5a67952
MS
5255 "No breakpoint or watchpoint matching '%s'.\n",
5256 args);
d77f58be 5257 }
c906108c
SS
5258 }
5259 else
c4093a6a 5260 {
a6d9a66e
UW
5261 if (last_loc && !server_command)
5262 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5263 }
c906108c 5264
4a64f543 5265 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5266 there have been breakpoints? */
c906108c 5267 annotate_breakpoints_table_end ();
d77f58be
SS
5268
5269 return nr_printable_breakpoints;
c906108c
SS
5270}
5271
ad443146
SS
5272/* Display the value of default-collect in a way that is generally
5273 compatible with the breakpoint list. */
5274
5275static void
5276default_collect_info (void)
5277{
79a45e25
PA
5278 struct ui_out *uiout = current_uiout;
5279
ad443146
SS
5280 /* If it has no value (which is frequently the case), say nothing; a
5281 message like "No default-collect." gets in user's face when it's
5282 not wanted. */
5283 if (!*default_collect)
5284 return;
5285
5286 /* The following phrase lines up nicely with per-tracepoint collect
5287 actions. */
5288 ui_out_text (uiout, "default collect ");
5289 ui_out_field_string (uiout, "default-collect", default_collect);
5290 ui_out_text (uiout, " \n");
5291}
5292
c906108c 5293static void
e5a67952 5294breakpoints_info (char *args, int from_tty)
c906108c 5295{
e5a67952 5296 breakpoint_1 (args, 0, NULL);
ad443146
SS
5297
5298 default_collect_info ();
d77f58be
SS
5299}
5300
5301static void
e5a67952 5302watchpoints_info (char *args, int from_tty)
d77f58be 5303{
e5a67952 5304 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 5305 struct ui_out *uiout = current_uiout;
d77f58be
SS
5306
5307 if (num_printed == 0)
5308 {
e5a67952 5309 if (args == NULL || *args == '\0')
d77f58be
SS
5310 ui_out_message (uiout, 0, "No watchpoints.\n");
5311 else
e5a67952 5312 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5313 }
c906108c
SS
5314}
5315
7a292a7a 5316static void
e5a67952 5317maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5318{
e5a67952 5319 breakpoint_1 (args, 1, NULL);
ad443146
SS
5320
5321 default_collect_info ();
c906108c
SS
5322}
5323
0d381245 5324static int
714835d5 5325breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5326 struct program_space *pspace,
714835d5 5327 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5328{
5329 struct bp_location *bl = b->loc;
cc59ec59 5330
0d381245
VP
5331 for (; bl; bl = bl->next)
5332 {
6c95b8df
PA
5333 if (bl->pspace == pspace
5334 && bl->address == pc
0d381245
VP
5335 && (!overlay_debugging || bl->section == section))
5336 return 1;
5337 }
5338 return 0;
5339}
5340
672f9b60 5341/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
5342 concerns with logical breakpoints, so we match program spaces, not
5343 address spaces. */
c906108c
SS
5344
5345static void
6c95b8df
PA
5346describe_other_breakpoints (struct gdbarch *gdbarch,
5347 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5348 struct obj_section *section, int thread)
c906108c 5349{
52f0bd74
AC
5350 int others = 0;
5351 struct breakpoint *b;
c906108c
SS
5352
5353 ALL_BREAKPOINTS (b)
672f9b60
KP
5354 others += (user_breakpoint_p (b)
5355 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
5356 if (others > 0)
5357 {
a3f17187
AC
5358 if (others == 1)
5359 printf_filtered (_("Note: breakpoint "));
5360 else /* if (others == ???) */
5361 printf_filtered (_("Note: breakpoints "));
c906108c 5362 ALL_BREAKPOINTS (b)
672f9b60 5363 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5364 {
5365 others--;
5366 printf_filtered ("%d", b->number);
5367 if (b->thread == -1 && thread != -1)
5368 printf_filtered (" (all threads)");
5369 else if (b->thread != -1)
5370 printf_filtered (" (thread %d)", b->thread);
5371 printf_filtered ("%s%s ",
059fb39f 5372 ((b->enable_state == bp_disabled
f8eba3c6 5373 || b->enable_state == bp_call_disabled)
0d381245
VP
5374 ? " (disabled)"
5375 : b->enable_state == bp_permanent
5376 ? " (permanent)"
5377 : ""),
5378 (others > 1) ? ","
5379 : ((others == 1) ? " and" : ""));
5380 }
a3f17187 5381 printf_filtered (_("also set at pc "));
5af949e3 5382 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5383 printf_filtered (".\n");
5384 }
5385}
5386\f
c906108c 5387
e4f237da
KB
5388/* Return true iff it is meaningful to use the address member of
5389 BPT. For some breakpoint types, the address member is irrelevant
5390 and it makes no sense to attempt to compare it to other addresses
5391 (or use it for any other purpose either).
5392
4a64f543
MS
5393 More specifically, each of the following breakpoint types will
5394 always have a zero valued address and we don't want to mark
5395 breakpoints of any of these types to be a duplicate of an actual
5396 breakpoint at address zero:
e4f237da
KB
5397
5398 bp_watchpoint
2d134ed3
PA
5399 bp_catchpoint
5400
5401*/
e4f237da
KB
5402
5403static int
5404breakpoint_address_is_meaningful (struct breakpoint *bpt)
5405{
5406 enum bptype type = bpt->type;
5407
2d134ed3
PA
5408 return (type != bp_watchpoint && type != bp_catchpoint);
5409}
5410
5411/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5412 true if LOC1 and LOC2 represent the same watchpoint location. */
5413
5414static int
4a64f543
MS
5415watchpoint_locations_match (struct bp_location *loc1,
5416 struct bp_location *loc2)
2d134ed3 5417{
3a5c3e22
PA
5418 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
5419 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
5420
5421 /* Both of them must exist. */
5422 gdb_assert (w1 != NULL);
5423 gdb_assert (w2 != NULL);
2bdf28a0 5424
4a64f543
MS
5425 /* If the target can evaluate the condition expression in hardware,
5426 then we we need to insert both watchpoints even if they are at
5427 the same place. Otherwise the watchpoint will only trigger when
5428 the condition of whichever watchpoint was inserted evaluates to
5429 true, not giving a chance for GDB to check the condition of the
5430 other watchpoint. */
3a5c3e22 5431 if ((w1->cond_exp
4a64f543
MS
5432 && target_can_accel_watchpoint_condition (loc1->address,
5433 loc1->length,
0cf6dd15 5434 loc1->watchpoint_type,
3a5c3e22
PA
5435 w1->cond_exp))
5436 || (w2->cond_exp
4a64f543
MS
5437 && target_can_accel_watchpoint_condition (loc2->address,
5438 loc2->length,
0cf6dd15 5439 loc2->watchpoint_type,
3a5c3e22 5440 w2->cond_exp)))
0cf6dd15
TJB
5441 return 0;
5442
85d721b8
PA
5443 /* Note that this checks the owner's type, not the location's. In
5444 case the target does not support read watchpoints, but does
5445 support access watchpoints, we'll have bp_read_watchpoint
5446 watchpoints with hw_access locations. Those should be considered
5447 duplicates of hw_read locations. The hw_read locations will
5448 become hw_access locations later. */
2d134ed3
PA
5449 return (loc1->owner->type == loc2->owner->type
5450 && loc1->pspace->aspace == loc2->pspace->aspace
5451 && loc1->address == loc2->address
5452 && loc1->length == loc2->length);
e4f237da
KB
5453}
5454
6c95b8df
PA
5455/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5456 same breakpoint location. In most targets, this can only be true
5457 if ASPACE1 matches ASPACE2. On targets that have global
5458 breakpoints, the address space doesn't really matter. */
5459
5460static int
5461breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5462 struct address_space *aspace2, CORE_ADDR addr2)
5463{
5464 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5465 || aspace1 == aspace2)
5466 && addr1 == addr2);
5467}
5468
f1310107
TJB
5469/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5470 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5471 matches ASPACE2. On targets that have global breakpoints, the address
5472 space doesn't really matter. */
5473
5474static int
5475breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5476 int len1, struct address_space *aspace2,
5477 CORE_ADDR addr2)
5478{
5479 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5480 || aspace1 == aspace2)
5481 && addr2 >= addr1 && addr2 < addr1 + len1);
5482}
5483
5484/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5485 a ranged breakpoint. In most targets, a match happens only if ASPACE
5486 matches the breakpoint's address space. On targets that have global
5487 breakpoints, the address space doesn't really matter. */
5488
5489static int
5490breakpoint_location_address_match (struct bp_location *bl,
5491 struct address_space *aspace,
5492 CORE_ADDR addr)
5493{
5494 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5495 aspace, addr)
5496 || (bl->length
5497 && breakpoint_address_match_range (bl->pspace->aspace,
5498 bl->address, bl->length,
5499 aspace, addr)));
5500}
5501
1e4d1764
YQ
5502/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
5503 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
5504 true, otherwise returns false. */
5505
5506static int
5507tracepoint_locations_match (struct bp_location *loc1,
5508 struct bp_location *loc2)
5509{
5510 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
5511 /* Since tracepoint locations are never duplicated with others', tracepoint
5512 locations at the same address of different tracepoints are regarded as
5513 different locations. */
5514 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
5515 else
5516 return 0;
5517}
5518
2d134ed3
PA
5519/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5520 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5521 represent the same location. */
5522
5523static int
4a64f543
MS
5524breakpoint_locations_match (struct bp_location *loc1,
5525 struct bp_location *loc2)
2d134ed3 5526{
2bdf28a0
JK
5527 int hw_point1, hw_point2;
5528
5529 /* Both of them must not be in moribund_locations. */
5530 gdb_assert (loc1->owner != NULL);
5531 gdb_assert (loc2->owner != NULL);
5532
5533 hw_point1 = is_hardware_watchpoint (loc1->owner);
5534 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5535
5536 if (hw_point1 != hw_point2)
5537 return 0;
5538 else if (hw_point1)
5539 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
5540 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
5541 return tracepoint_locations_match (loc1, loc2);
2d134ed3 5542 else
f1310107
TJB
5543 /* We compare bp_location.length in order to cover ranged breakpoints. */
5544 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5545 loc2->pspace->aspace, loc2->address)
5546 && loc1->length == loc2->length);
2d134ed3
PA
5547}
5548
76897487
KB
5549static void
5550breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5551 int bnum, int have_bnum)
5552{
f63fbe86
MS
5553 /* The longest string possibly returned by hex_string_custom
5554 is 50 chars. These must be at least that big for safety. */
5555 char astr1[64];
5556 char astr2[64];
76897487 5557
bb599908
PH
5558 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5559 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5560 if (have_bnum)
8a3fe4f8 5561 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5562 bnum, astr1, astr2);
5563 else
8a3fe4f8 5564 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5565}
5566
4a64f543
MS
5567/* Adjust a breakpoint's address to account for architectural
5568 constraints on breakpoint placement. Return the adjusted address.
5569 Note: Very few targets require this kind of adjustment. For most
5570 targets, this function is simply the identity function. */
76897487
KB
5571
5572static CORE_ADDR
a6d9a66e
UW
5573adjust_breakpoint_address (struct gdbarch *gdbarch,
5574 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5575{
a6d9a66e 5576 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5577 {
5578 /* Very few targets need any kind of breakpoint adjustment. */
5579 return bpaddr;
5580 }
88f7da05
KB
5581 else if (bptype == bp_watchpoint
5582 || bptype == bp_hardware_watchpoint
5583 || bptype == bp_read_watchpoint
5584 || bptype == bp_access_watchpoint
fe798b75 5585 || bptype == bp_catchpoint)
88f7da05
KB
5586 {
5587 /* Watchpoints and the various bp_catch_* eventpoints should not
5588 have their addresses modified. */
5589 return bpaddr;
5590 }
76897487
KB
5591 else
5592 {
5593 CORE_ADDR adjusted_bpaddr;
5594
5595 /* Some targets have architectural constraints on the placement
5596 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5597 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5598
5599 /* An adjusted breakpoint address can significantly alter
5600 a user's expectations. Print a warning if an adjustment
5601 is required. */
5602 if (adjusted_bpaddr != bpaddr)
5603 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5604
5605 return adjusted_bpaddr;
5606 }
5607}
5608
28010a5d
PA
5609void
5610init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5611 struct breakpoint *owner)
7cc221ef 5612{
7cc221ef
DJ
5613 memset (loc, 0, sizeof (*loc));
5614
348d480f
PA
5615 gdb_assert (ops != NULL);
5616
28010a5d
PA
5617 loc->ops = ops;
5618 loc->owner = owner;
511a6cd4 5619 loc->cond = NULL;
0d381245
VP
5620 loc->shlib_disabled = 0;
5621 loc->enabled = 1;
e049a4b5 5622
28010a5d 5623 switch (owner->type)
e049a4b5
DJ
5624 {
5625 case bp_breakpoint:
5626 case bp_until:
5627 case bp_finish:
5628 case bp_longjmp:
5629 case bp_longjmp_resume:
186c406b
TT
5630 case bp_exception:
5631 case bp_exception_resume:
e049a4b5 5632 case bp_step_resume:
2c03e5be 5633 case bp_hp_step_resume:
e049a4b5
DJ
5634 case bp_watchpoint_scope:
5635 case bp_call_dummy:
aa7d318d 5636 case bp_std_terminate:
e049a4b5
DJ
5637 case bp_shlib_event:
5638 case bp_thread_event:
5639 case bp_overlay_event:
4efc6507 5640 case bp_jit_event:
0fd8e87f 5641 case bp_longjmp_master:
aa7d318d 5642 case bp_std_terminate_master:
186c406b 5643 case bp_exception_master:
0e30163f
JK
5644 case bp_gnu_ifunc_resolver:
5645 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5646 loc->loc_type = bp_loc_software_breakpoint;
5647 break;
5648 case bp_hardware_breakpoint:
5649 loc->loc_type = bp_loc_hardware_breakpoint;
5650 break;
5651 case bp_hardware_watchpoint:
5652 case bp_read_watchpoint:
5653 case bp_access_watchpoint:
5654 loc->loc_type = bp_loc_hardware_watchpoint;
5655 break;
5656 case bp_watchpoint:
ce78b96d 5657 case bp_catchpoint:
15c3d785
PA
5658 case bp_tracepoint:
5659 case bp_fast_tracepoint:
0fb4aa4b 5660 case bp_static_tracepoint:
e049a4b5
DJ
5661 loc->loc_type = bp_loc_other;
5662 break;
5663 default:
e2e0b3e5 5664 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5665 }
5666
f431efe5 5667 loc->refc = 1;
28010a5d
PA
5668}
5669
5670/* Allocate a struct bp_location. */
5671
5672static struct bp_location *
5673allocate_bp_location (struct breakpoint *bpt)
5674{
348d480f
PA
5675 return bpt->ops->allocate_location (bpt);
5676}
7cc221ef 5677
f431efe5
PA
5678static void
5679free_bp_location (struct bp_location *loc)
fe3f5fa8 5680{
348d480f 5681 loc->ops->dtor (loc);
fe3f5fa8
VP
5682 xfree (loc);
5683}
5684
f431efe5
PA
5685/* Increment reference count. */
5686
5687static void
5688incref_bp_location (struct bp_location *bl)
5689{
5690 ++bl->refc;
5691}
5692
5693/* Decrement reference count. If the reference count reaches 0,
5694 destroy the bp_location. Sets *BLP to NULL. */
5695
5696static void
5697decref_bp_location (struct bp_location **blp)
5698{
0807b50c
PA
5699 gdb_assert ((*blp)->refc > 0);
5700
f431efe5
PA
5701 if (--(*blp)->refc == 0)
5702 free_bp_location (*blp);
5703 *blp = NULL;
5704}
5705
346774a9 5706/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 5707
346774a9
PA
5708static void
5709add_to_breakpoint_chain (struct breakpoint *b)
c906108c 5710{
346774a9 5711 struct breakpoint *b1;
c906108c 5712
346774a9
PA
5713 /* Add this breakpoint to the end of the chain so that a list of
5714 breakpoints will come out in order of increasing numbers. */
5715
5716 b1 = breakpoint_chain;
5717 if (b1 == 0)
5718 breakpoint_chain = b;
5719 else
5720 {
5721 while (b1->next)
5722 b1 = b1->next;
5723 b1->next = b;
5724 }
5725}
5726
5727/* Initializes breakpoint B with type BPTYPE and no locations yet. */
5728
5729static void
5730init_raw_breakpoint_without_location (struct breakpoint *b,
5731 struct gdbarch *gdbarch,
28010a5d 5732 enum bptype bptype,
c0a91b2b 5733 const struct breakpoint_ops *ops)
346774a9 5734{
c906108c 5735 memset (b, 0, sizeof (*b));
2219d63c 5736
348d480f
PA
5737 gdb_assert (ops != NULL);
5738
28010a5d 5739 b->ops = ops;
4d28f7a8 5740 b->type = bptype;
a6d9a66e 5741 b->gdbarch = gdbarch;
c906108c
SS
5742 b->language = current_language->la_language;
5743 b->input_radix = input_radix;
5744 b->thread = -1;
b5de0fa7 5745 b->enable_state = bp_enabled;
c906108c
SS
5746 b->next = 0;
5747 b->silent = 0;
5748 b->ignore_count = 0;
5749 b->commands = NULL;
818dd999 5750 b->frame_id = null_frame_id;
0d381245 5751 b->condition_not_parsed = 0;
84f4c1fe 5752 b->py_bp_object = NULL;
d0fb5eae 5753 b->related_breakpoint = b;
346774a9
PA
5754}
5755
5756/* Helper to set_raw_breakpoint below. Creates a breakpoint
5757 that has type BPTYPE and has no locations as yet. */
346774a9
PA
5758
5759static struct breakpoint *
5760set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 5761 enum bptype bptype,
c0a91b2b 5762 const struct breakpoint_ops *ops)
346774a9
PA
5763{
5764 struct breakpoint *b = XNEW (struct breakpoint);
5765
348d480f 5766 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 5767 add_to_breakpoint_chain (b);
0d381245
VP
5768 return b;
5769}
5770
0e30163f
JK
5771/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5772 resolutions should be made as the user specified the location explicitly
5773 enough. */
5774
0d381245 5775static void
0e30163f 5776set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5777{
2bdf28a0
JK
5778 gdb_assert (loc->owner != NULL);
5779
0d381245 5780 if (loc->owner->type == bp_breakpoint
1042e4c0 5781 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5782 || is_tracepoint (loc->owner))
0d381245 5783 {
0e30163f
JK
5784 int is_gnu_ifunc;
5785
5786 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5787 NULL, NULL, &is_gnu_ifunc);
5788
5789 if (is_gnu_ifunc && !explicit_loc)
5790 {
5791 struct breakpoint *b = loc->owner;
5792
5793 gdb_assert (loc->pspace == current_program_space);
5794 if (gnu_ifunc_resolve_name (loc->function_name,
5795 &loc->requested_address))
5796 {
5797 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5798 loc->address = adjust_breakpoint_address (loc->gdbarch,
5799 loc->requested_address,
5800 b->type);
5801 }
5802 else if (b->type == bp_breakpoint && b->loc == loc
5803 && loc->next == NULL && b->related_breakpoint == b)
5804 {
5805 /* Create only the whole new breakpoint of this type but do not
5806 mess more complicated breakpoints with multiple locations. */
5807 b->type = bp_gnu_ifunc_resolver;
5808 }
5809 }
5810
0d381245
VP
5811 if (loc->function_name)
5812 loc->function_name = xstrdup (loc->function_name);
5813 }
5814}
5815
a6d9a66e 5816/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 5817struct gdbarch *
a6d9a66e
UW
5818get_sal_arch (struct symtab_and_line sal)
5819{
5820 if (sal.section)
5821 return get_objfile_arch (sal.section->objfile);
5822 if (sal.symtab)
5823 return get_objfile_arch (sal.symtab->objfile);
5824
5825 return NULL;
5826}
5827
346774a9
PA
5828/* Low level routine for partially initializing a breakpoint of type
5829 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 5830 file name, and line number are provided by SAL.
0d381245
VP
5831
5832 It is expected that the caller will complete the initialization of
5833 the newly created breakpoint struct as well as output any status
c56053d2 5834 information regarding the creation of a new breakpoint. */
0d381245 5835
346774a9
PA
5836static void
5837init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 5838 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5839 const struct breakpoint_ops *ops)
0d381245 5840{
28010a5d 5841 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 5842
3742cc8b 5843 add_location_to_breakpoint (b, &sal);
0d381245 5844
6c95b8df
PA
5845 if (bptype != bp_catchpoint)
5846 gdb_assert (sal.pspace != NULL);
5847
f8eba3c6
TT
5848 /* Store the program space that was used to set the breakpoint,
5849 except for ordinary breakpoints, which are independent of the
5850 program space. */
5851 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
5852 b->pspace = sal.pspace;
0d381245 5853
c906108c 5854 breakpoints_changed ();
346774a9 5855}
c906108c 5856
346774a9
PA
5857/* set_raw_breakpoint is a low level routine for allocating and
5858 partially initializing a breakpoint of type BPTYPE. The newly
5859 created breakpoint's address, section, source file name, and line
5860 number are provided by SAL. The newly created and partially
5861 initialized breakpoint is added to the breakpoint chain and
5862 is also returned as the value of this function.
5863
5864 It is expected that the caller will complete the initialization of
5865 the newly created breakpoint struct as well as output any status
5866 information regarding the creation of a new breakpoint. In
5867 particular, set_raw_breakpoint does NOT set the breakpoint
5868 number! Care should be taken to not allow an error to occur
5869 prior to completing the initialization of the breakpoint. If this
5870 should happen, a bogus breakpoint will be left on the chain. */
5871
5872struct breakpoint *
5873set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 5874 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5875 const struct breakpoint_ops *ops)
346774a9
PA
5876{
5877 struct breakpoint *b = XNEW (struct breakpoint);
5878
348d480f 5879 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 5880 add_to_breakpoint_chain (b);
c906108c
SS
5881 return b;
5882}
5883
c2c6d25f
JM
5884
5885/* Note that the breakpoint object B describes a permanent breakpoint
5886 instruction, hard-wired into the inferior's code. */
5887void
5888make_breakpoint_permanent (struct breakpoint *b)
5889{
0d381245 5890 struct bp_location *bl;
cc59ec59 5891
b5de0fa7 5892 b->enable_state = bp_permanent;
c2c6d25f 5893
4a64f543
MS
5894 /* By definition, permanent breakpoints are already present in the
5895 code. Mark all locations as inserted. For now,
5896 make_breakpoint_permanent is called in just one place, so it's
5897 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 5898 multiple locations or not, but it's easy to implement. */
0d381245
VP
5899 for (bl = b->loc; bl; bl = bl->next)
5900 bl->inserted = 1;
c2c6d25f
JM
5901}
5902
53a5351d 5903/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5904 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5905 initiated the operation. */
c906108c
SS
5906
5907void
186c406b 5908set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5909{
35df4500 5910 struct breakpoint *b, *b_tmp;
186c406b 5911 int thread = tp->num;
0fd8e87f
UW
5912
5913 /* To avoid having to rescan all objfile symbols at every step,
5914 we maintain a list of continually-inserted but always disabled
5915 longjmp "master" breakpoints. Here, we simply create momentary
5916 clones of those and enable them for the requested thread. */
35df4500 5917 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5918 if (b->pspace == current_program_space
186c406b
TT
5919 && (b->type == bp_longjmp_master
5920 || b->type == bp_exception_master))
0fd8e87f 5921 {
06edf0c0
PA
5922 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5923 struct breakpoint *clone;
cc59ec59 5924
06edf0c0
PA
5925 clone = momentary_breakpoint_from_master (b, type,
5926 &momentary_breakpoint_ops);
0fd8e87f
UW
5927 clone->thread = thread;
5928 }
186c406b
TT
5929
5930 tp->initiating_frame = frame;
c906108c
SS
5931}
5932
611c83ae 5933/* Delete all longjmp breakpoints from THREAD. */
c906108c 5934void
611c83ae 5935delete_longjmp_breakpoint (int thread)
c906108c 5936{
35df4500 5937 struct breakpoint *b, *b_tmp;
c906108c 5938
35df4500 5939 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5940 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5941 {
5942 if (b->thread == thread)
5943 delete_breakpoint (b);
5944 }
c906108c
SS
5945}
5946
1900040c
MS
5947void
5948enable_overlay_breakpoints (void)
5949{
52f0bd74 5950 struct breakpoint *b;
1900040c
MS
5951
5952 ALL_BREAKPOINTS (b)
5953 if (b->type == bp_overlay_event)
5954 {
5955 b->enable_state = bp_enabled;
b60e7edf 5956 update_global_location_list (1);
c02f5703 5957 overlay_events_enabled = 1;
1900040c
MS
5958 }
5959}
5960
5961void
5962disable_overlay_breakpoints (void)
5963{
52f0bd74 5964 struct breakpoint *b;
1900040c
MS
5965
5966 ALL_BREAKPOINTS (b)
5967 if (b->type == bp_overlay_event)
5968 {
5969 b->enable_state = bp_disabled;
b60e7edf 5970 update_global_location_list (0);
c02f5703 5971 overlay_events_enabled = 0;
1900040c
MS
5972 }
5973}
5974
aa7d318d
TT
5975/* Set an active std::terminate breakpoint for each std::terminate
5976 master breakpoint. */
5977void
5978set_std_terminate_breakpoint (void)
5979{
35df4500 5980 struct breakpoint *b, *b_tmp;
aa7d318d 5981
35df4500 5982 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5983 if (b->pspace == current_program_space
5984 && b->type == bp_std_terminate_master)
5985 {
06edf0c0
PA
5986 momentary_breakpoint_from_master (b, bp_std_terminate,
5987 &momentary_breakpoint_ops);
aa7d318d
TT
5988 }
5989}
5990
5991/* Delete all the std::terminate breakpoints. */
5992void
5993delete_std_terminate_breakpoint (void)
5994{
35df4500 5995 struct breakpoint *b, *b_tmp;
aa7d318d 5996
35df4500 5997 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5998 if (b->type == bp_std_terminate)
5999 delete_breakpoint (b);
6000}
6001
c4093a6a 6002struct breakpoint *
a6d9a66e 6003create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6004{
6005 struct breakpoint *b;
c4093a6a 6006
06edf0c0
PA
6007 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
6008 &internal_breakpoint_ops);
6009
b5de0fa7 6010 b->enable_state = bp_enabled;
c4093a6a 6011 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6012 b->addr_string
6013 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6014
b60e7edf 6015 update_global_location_list_nothrow (1);
74960c60 6016
c4093a6a
JM
6017 return b;
6018}
6019
6020void
6021remove_thread_event_breakpoints (void)
6022{
35df4500 6023 struct breakpoint *b, *b_tmp;
c4093a6a 6024
35df4500 6025 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6026 if (b->type == bp_thread_event
6027 && b->loc->pspace == current_program_space)
c4093a6a
JM
6028 delete_breakpoint (b);
6029}
6030
0101ce28
JJ
6031struct lang_and_radix
6032 {
6033 enum language lang;
6034 int radix;
6035 };
6036
4efc6507
DE
6037/* Create a breakpoint for JIT code registration and unregistration. */
6038
6039struct breakpoint *
6040create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6041{
6042 struct breakpoint *b;
6043
06edf0c0
PA
6044 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
6045 &internal_breakpoint_ops);
4efc6507
DE
6046 update_global_location_list_nothrow (1);
6047 return b;
6048}
0101ce28 6049
03673fc7
PP
6050/* Remove JIT code registration and unregistration breakpoint(s). */
6051
6052void
6053remove_jit_event_breakpoints (void)
6054{
6055 struct breakpoint *b, *b_tmp;
6056
6057 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6058 if (b->type == bp_jit_event
6059 && b->loc->pspace == current_program_space)
6060 delete_breakpoint (b);
6061}
6062
cae688ec
JJ
6063void
6064remove_solib_event_breakpoints (void)
6065{
35df4500 6066 struct breakpoint *b, *b_tmp;
cae688ec 6067
35df4500 6068 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6069 if (b->type == bp_shlib_event
6070 && b->loc->pspace == current_program_space)
cae688ec
JJ
6071 delete_breakpoint (b);
6072}
6073
6074struct breakpoint *
a6d9a66e 6075create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6076{
6077 struct breakpoint *b;
6078
06edf0c0
PA
6079 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6080 &internal_breakpoint_ops);
b60e7edf 6081 update_global_location_list_nothrow (1);
cae688ec
JJ
6082 return b;
6083}
6084
6085/* Disable any breakpoints that are on code in shared libraries. Only
6086 apply to enabled breakpoints, disabled ones can just stay disabled. */
6087
6088void
cb851954 6089disable_breakpoints_in_shlibs (void)
cae688ec 6090{
876fa593 6091 struct bp_location *loc, **locp_tmp;
cae688ec 6092
876fa593 6093 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6094 {
2bdf28a0 6095 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6096 struct breakpoint *b = loc->owner;
2bdf28a0 6097
4a64f543
MS
6098 /* We apply the check to all breakpoints, including disabled for
6099 those with loc->duplicate set. This is so that when breakpoint
6100 becomes enabled, or the duplicate is removed, gdb will try to
6101 insert all breakpoints. If we don't set shlib_disabled here,
6102 we'll try to insert those breakpoints and fail. */
1042e4c0 6103 if (((b->type == bp_breakpoint)
508ccb1f 6104 || (b->type == bp_jit_event)
1042e4c0 6105 || (b->type == bp_hardware_breakpoint)
d77f58be 6106 || (is_tracepoint (b)))
6c95b8df 6107 && loc->pspace == current_program_space
0d381245 6108 && !loc->shlib_disabled
a77053c2 6109#ifdef PC_SOLIB
0d381245 6110 && PC_SOLIB (loc->address)
a77053c2 6111#else
6c95b8df 6112 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6113#endif
6114 )
0d381245
VP
6115 {
6116 loc->shlib_disabled = 1;
6117 }
cae688ec
JJ
6118 }
6119}
6120
1e4d1764
YQ
6121/* Disable any breakpoints and tracepoints that are in an unloaded shared
6122 library. Only apply to enabled breakpoints, disabled ones can just stay
4a64f543 6123 disabled. */
84acb35a 6124
75149521 6125static void
84acb35a
JJ
6126disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6127{
876fa593 6128 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6129 int disabled_shlib_breaks = 0;
6130
c86cf029
VP
6131 /* SunOS a.out shared libraries are always mapped, so do not
6132 disable breakpoints; they will only be reported as unloaded
6133 through clear_solib when GDB discards its shared library
6134 list. See clear_solib for more information. */
6135 if (exec_bfd != NULL
6136 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6137 return;
6138
876fa593 6139 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6140 {
2bdf28a0 6141 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6142 struct breakpoint *b = loc->owner;
cc59ec59 6143
1e4d1764 6144 if (solib->pspace == loc->pspace
e2dd7057 6145 && !loc->shlib_disabled
1e4d1764
YQ
6146 && (((b->type == bp_breakpoint
6147 || b->type == bp_jit_event
6148 || b->type == bp_hardware_breakpoint)
6149 && (loc->loc_type == bp_loc_hardware_breakpoint
6150 || loc->loc_type == bp_loc_software_breakpoint))
6151 || is_tracepoint (b))
e2dd7057 6152 && solib_contains_address_p (solib, loc->address))
84acb35a 6153 {
e2dd7057
PP
6154 loc->shlib_disabled = 1;
6155 /* At this point, we cannot rely on remove_breakpoint
6156 succeeding so we must mark the breakpoint as not inserted
6157 to prevent future errors occurring in remove_breakpoints. */
6158 loc->inserted = 0;
8d3788bd
VP
6159
6160 /* This may cause duplicate notifications for the same breakpoint. */
6161 observer_notify_breakpoint_modified (b);
6162
e2dd7057
PP
6163 if (!disabled_shlib_breaks)
6164 {
6165 target_terminal_ours_for_output ();
3e43a32a
MS
6166 warning (_("Temporarily disabling breakpoints "
6167 "for unloaded shared library \"%s\""),
e2dd7057 6168 solib->so_name);
84acb35a 6169 }
e2dd7057 6170 disabled_shlib_breaks = 1;
84acb35a
JJ
6171 }
6172 }
84acb35a
JJ
6173}
6174
ce78b96d
JB
6175/* FORK & VFORK catchpoints. */
6176
e29a4733
PA
6177/* An instance of this type is used to represent a fork or vfork
6178 catchpoint. It includes a "struct breakpoint" as a kind of base
6179 class; users downcast to "struct breakpoint *" when needed. A
6180 breakpoint is really of this type iff its ops pointer points to
6181 CATCH_FORK_BREAKPOINT_OPS. */
6182
6183struct fork_catchpoint
6184{
6185 /* The base class. */
6186 struct breakpoint base;
6187
6188 /* Process id of a child process whose forking triggered this
6189 catchpoint. This field is only valid immediately after this
6190 catchpoint has triggered. */
6191 ptid_t forked_inferior_pid;
6192};
6193
4a64f543
MS
6194/* Implement the "insert" breakpoint_ops method for fork
6195 catchpoints. */
ce78b96d 6196
77b06cd7
TJB
6197static int
6198insert_catch_fork (struct bp_location *bl)
ce78b96d 6199{
77b06cd7 6200 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6201}
6202
4a64f543
MS
6203/* Implement the "remove" breakpoint_ops method for fork
6204 catchpoints. */
ce78b96d
JB
6205
6206static int
77b06cd7 6207remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6208{
6209 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6210}
6211
6212/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6213 catchpoints. */
6214
6215static int
f1310107
TJB
6216breakpoint_hit_catch_fork (const struct bp_location *bl,
6217 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6218{
e29a4733
PA
6219 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6220
6221 return inferior_has_forked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6222}
6223
4a64f543
MS
6224/* Implement the "print_it" breakpoint_ops method for fork
6225 catchpoints. */
ce78b96d
JB
6226
6227static enum print_stop_action
348d480f 6228print_it_catch_fork (bpstat bs)
ce78b96d 6229{
36dfb11c 6230 struct ui_out *uiout = current_uiout;
348d480f
PA
6231 struct breakpoint *b = bs->breakpoint_at;
6232 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 6233
ce78b96d 6234 annotate_catchpoint (b->number);
36dfb11c
TT
6235 if (b->disposition == disp_del)
6236 ui_out_text (uiout, "\nTemporary catchpoint ");
6237 else
6238 ui_out_text (uiout, "\nCatchpoint ");
6239 if (ui_out_is_mi_like_p (uiout))
6240 {
6241 ui_out_field_string (uiout, "reason",
6242 async_reason_lookup (EXEC_ASYNC_FORK));
6243 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6244 }
6245 ui_out_field_int (uiout, "bkptno", b->number);
6246 ui_out_text (uiout, " (forked process ");
6247 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6248 ui_out_text (uiout, "), ");
ce78b96d
JB
6249 return PRINT_SRC_AND_LOC;
6250}
6251
4a64f543
MS
6252/* Implement the "print_one" breakpoint_ops method for fork
6253 catchpoints. */
ce78b96d
JB
6254
6255static void
a6d9a66e 6256print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6257{
e29a4733 6258 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6259 struct value_print_options opts;
79a45e25 6260 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6261
6262 get_user_print_options (&opts);
6263
4a64f543
MS
6264 /* Field 4, the address, is omitted (which makes the columns not
6265 line up too nicely with the headers, but the effect is relatively
6266 readable). */
79a45b7d 6267 if (opts.addressprint)
ce78b96d
JB
6268 ui_out_field_skip (uiout, "addr");
6269 annotate_field (5);
6270 ui_out_text (uiout, "fork");
e29a4733 6271 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6272 {
6273 ui_out_text (uiout, ", process ");
6274 ui_out_field_int (uiout, "what",
e29a4733 6275 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6276 ui_out_spaces (uiout, 1);
6277 }
6278}
6279
6280/* Implement the "print_mention" breakpoint_ops method for fork
6281 catchpoints. */
6282
6283static void
6284print_mention_catch_fork (struct breakpoint *b)
6285{
6286 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6287}
6288
6149aea9
PA
6289/* Implement the "print_recreate" breakpoint_ops method for fork
6290 catchpoints. */
6291
6292static void
6293print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6294{
6295 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 6296 print_recreate_thread (b, fp);
6149aea9
PA
6297}
6298
ce78b96d
JB
6299/* The breakpoint_ops structure to be used in fork catchpoints. */
6300
2060206e 6301static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 6302
4a64f543
MS
6303/* Implement the "insert" breakpoint_ops method for vfork
6304 catchpoints. */
ce78b96d 6305
77b06cd7
TJB
6306static int
6307insert_catch_vfork (struct bp_location *bl)
ce78b96d 6308{
77b06cd7 6309 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6310}
6311
4a64f543
MS
6312/* Implement the "remove" breakpoint_ops method for vfork
6313 catchpoints. */
ce78b96d
JB
6314
6315static int
77b06cd7 6316remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6317{
6318 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6319}
6320
6321/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6322 catchpoints. */
6323
6324static int
f1310107
TJB
6325breakpoint_hit_catch_vfork (const struct bp_location *bl,
6326 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6327{
e29a4733
PA
6328 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6329
6330 return inferior_has_vforked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6331}
6332
4a64f543
MS
6333/* Implement the "print_it" breakpoint_ops method for vfork
6334 catchpoints. */
ce78b96d
JB
6335
6336static enum print_stop_action
348d480f 6337print_it_catch_vfork (bpstat bs)
ce78b96d 6338{
36dfb11c 6339 struct ui_out *uiout = current_uiout;
348d480f 6340 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
6341 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6342
ce78b96d 6343 annotate_catchpoint (b->number);
36dfb11c
TT
6344 if (b->disposition == disp_del)
6345 ui_out_text (uiout, "\nTemporary catchpoint ");
6346 else
6347 ui_out_text (uiout, "\nCatchpoint ");
6348 if (ui_out_is_mi_like_p (uiout))
6349 {
6350 ui_out_field_string (uiout, "reason",
6351 async_reason_lookup (EXEC_ASYNC_VFORK));
6352 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6353 }
6354 ui_out_field_int (uiout, "bkptno", b->number);
6355 ui_out_text (uiout, " (vforked process ");
6356 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6357 ui_out_text (uiout, "), ");
ce78b96d
JB
6358 return PRINT_SRC_AND_LOC;
6359}
6360
4a64f543
MS
6361/* Implement the "print_one" breakpoint_ops method for vfork
6362 catchpoints. */
ce78b96d
JB
6363
6364static void
a6d9a66e 6365print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6366{
e29a4733 6367 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6368 struct value_print_options opts;
79a45e25 6369 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6370
6371 get_user_print_options (&opts);
4a64f543
MS
6372 /* Field 4, the address, is omitted (which makes the columns not
6373 line up too nicely with the headers, but the effect is relatively
6374 readable). */
79a45b7d 6375 if (opts.addressprint)
ce78b96d
JB
6376 ui_out_field_skip (uiout, "addr");
6377 annotate_field (5);
6378 ui_out_text (uiout, "vfork");
e29a4733 6379 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6380 {
6381 ui_out_text (uiout, ", process ");
6382 ui_out_field_int (uiout, "what",
e29a4733 6383 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6384 ui_out_spaces (uiout, 1);
6385 }
6386}
6387
6388/* Implement the "print_mention" breakpoint_ops method for vfork
6389 catchpoints. */
6390
6391static void
6392print_mention_catch_vfork (struct breakpoint *b)
6393{
6394 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6395}
6396
6149aea9
PA
6397/* Implement the "print_recreate" breakpoint_ops method for vfork
6398 catchpoints. */
6399
6400static void
6401print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6402{
6403 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 6404 print_recreate_thread (b, fp);
6149aea9
PA
6405}
6406
ce78b96d
JB
6407/* The breakpoint_ops structure to be used in vfork catchpoints. */
6408
2060206e 6409static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 6410
be5c67c1
PA
6411/* An instance of this type is used to represent a syscall catchpoint.
6412 It includes a "struct breakpoint" as a kind of base class; users
6413 downcast to "struct breakpoint *" when needed. A breakpoint is
6414 really of this type iff its ops pointer points to
6415 CATCH_SYSCALL_BREAKPOINT_OPS. */
6416
6417struct syscall_catchpoint
6418{
6419 /* The base class. */
6420 struct breakpoint base;
6421
6422 /* Syscall numbers used for the 'catch syscall' feature. If no
6423 syscall has been specified for filtering, its value is NULL.
6424 Otherwise, it holds a list of all syscalls to be caught. The
6425 list elements are allocated with xmalloc. */
6426 VEC(int) *syscalls_to_be_caught;
6427};
6428
6429/* Implement the "dtor" breakpoint_ops method for syscall
6430 catchpoints. */
6431
6432static void
6433dtor_catch_syscall (struct breakpoint *b)
6434{
6435 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6436
6437 VEC_free (int, c->syscalls_to_be_caught);
348d480f 6438
2060206e 6439 base_breakpoint_ops.dtor (b);
be5c67c1
PA
6440}
6441
a96d9b2e
SDJ
6442/* Implement the "insert" breakpoint_ops method for syscall
6443 catchpoints. */
6444
77b06cd7
TJB
6445static int
6446insert_catch_syscall (struct bp_location *bl)
a96d9b2e 6447{
be5c67c1 6448 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6449 struct inferior *inf = current_inferior ();
6450
6451 ++inf->total_syscalls_count;
be5c67c1 6452 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6453 ++inf->any_syscall_count;
6454 else
6455 {
6456 int i, iter;
cc59ec59 6457
a96d9b2e 6458 for (i = 0;
be5c67c1 6459 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6460 i++)
6461 {
6462 int elem;
cc59ec59 6463
a96d9b2e
SDJ
6464 if (iter >= VEC_length (int, inf->syscalls_counts))
6465 {
6466 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6467 uintptr_t vec_addr_offset
6468 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6469 uintptr_t vec_addr;
6470 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6471 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6472 vec_addr_offset;
6473 memset ((void *) vec_addr, 0,
6474 (iter + 1 - old_size) * sizeof (int));
6475 }
6476 elem = VEC_index (int, inf->syscalls_counts, iter);
6477 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6478 }
6479 }
6480
77b06cd7
TJB
6481 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6482 inf->total_syscalls_count != 0,
6483 inf->any_syscall_count,
6484 VEC_length (int, inf->syscalls_counts),
6485 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6486}
6487
6488/* Implement the "remove" breakpoint_ops method for syscall
6489 catchpoints. */
6490
6491static int
77b06cd7 6492remove_catch_syscall (struct bp_location *bl)
a96d9b2e 6493{
be5c67c1 6494 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6495 struct inferior *inf = current_inferior ();
6496
6497 --inf->total_syscalls_count;
be5c67c1 6498 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6499 --inf->any_syscall_count;
6500 else
6501 {
6502 int i, iter;
cc59ec59 6503
a96d9b2e 6504 for (i = 0;
be5c67c1 6505 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6506 i++)
6507 {
6508 int elem;
6509 if (iter >= VEC_length (int, inf->syscalls_counts))
6510 /* Shouldn't happen. */
6511 continue;
6512 elem = VEC_index (int, inf->syscalls_counts, iter);
6513 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6514 }
6515 }
6516
6517 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6518 inf->total_syscalls_count != 0,
6519 inf->any_syscall_count,
6520 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6521 VEC_address (int,
6522 inf->syscalls_counts));
a96d9b2e
SDJ
6523}
6524
6525/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6526 catchpoints. */
6527
6528static int
f1310107
TJB
6529breakpoint_hit_catch_syscall (const struct bp_location *bl,
6530 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6531{
4a64f543
MS
6532 /* We must check if we are catching specific syscalls in this
6533 breakpoint. If we are, then we must guarantee that the called
6534 syscall is the same syscall we are catching. */
a96d9b2e 6535 int syscall_number = 0;
be5c67c1
PA
6536 const struct syscall_catchpoint *c
6537 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6538
6539 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6540 return 0;
6541
6542 /* Now, checking if the syscall is the same. */
be5c67c1 6543 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6544 {
6545 int i, iter;
cc59ec59 6546
a96d9b2e 6547 for (i = 0;
be5c67c1 6548 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6549 i++)
6550 if (syscall_number == iter)
6551 break;
6552 /* Not the same. */
6553 if (!iter)
6554 return 0;
6555 }
6556
6557 return 1;
6558}
6559
6560/* Implement the "print_it" breakpoint_ops method for syscall
6561 catchpoints. */
6562
6563static enum print_stop_action
348d480f 6564print_it_catch_syscall (bpstat bs)
a96d9b2e 6565{
36dfb11c 6566 struct ui_out *uiout = current_uiout;
348d480f 6567 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
6568 /* These are needed because we want to know in which state a
6569 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6570 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6571 must print "called syscall" or "returned from syscall". */
6572 ptid_t ptid;
6573 struct target_waitstatus last;
6574 struct syscall s;
a96d9b2e
SDJ
6575 char *syscall_id;
6576
6577 get_last_target_status (&ptid, &last);
6578
6579 get_syscall_by_number (last.value.syscall_number, &s);
6580
6581 annotate_catchpoint (b->number);
6582
36dfb11c
TT
6583 if (b->disposition == disp_del)
6584 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 6585 else
36dfb11c
TT
6586 ui_out_text (uiout, "\nCatchpoint ");
6587 if (ui_out_is_mi_like_p (uiout))
6588 {
6589 ui_out_field_string (uiout, "reason",
6590 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
6591 ? EXEC_ASYNC_SYSCALL_ENTRY
6592 : EXEC_ASYNC_SYSCALL_RETURN));
6593 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6594 }
6595 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
6596
6597 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
6598 ui_out_text (uiout, " (call to syscall ");
6599 else
6600 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 6601
36dfb11c
TT
6602 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
6603 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
6604 if (s.name != NULL)
6605 ui_out_field_string (uiout, "syscall-name", s.name);
6606
6607 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
6608
6609 return PRINT_SRC_AND_LOC;
6610}
6611
6612/* Implement the "print_one" breakpoint_ops method for syscall
6613 catchpoints. */
6614
6615static void
6616print_one_catch_syscall (struct breakpoint *b,
f1310107 6617 struct bp_location **last_loc)
a96d9b2e 6618{
be5c67c1 6619 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 6620 struct value_print_options opts;
79a45e25 6621 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
6622
6623 get_user_print_options (&opts);
4a64f543
MS
6624 /* Field 4, the address, is omitted (which makes the columns not
6625 line up too nicely with the headers, but the effect is relatively
6626 readable). */
a96d9b2e
SDJ
6627 if (opts.addressprint)
6628 ui_out_field_skip (uiout, "addr");
6629 annotate_field (5);
6630
be5c67c1
PA
6631 if (c->syscalls_to_be_caught
6632 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6633 ui_out_text (uiout, "syscalls \"");
6634 else
6635 ui_out_text (uiout, "syscall \"");
6636
be5c67c1 6637 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6638 {
6639 int i, iter;
6640 char *text = xstrprintf ("%s", "");
cc59ec59 6641
a96d9b2e 6642 for (i = 0;
be5c67c1 6643 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6644 i++)
6645 {
6646 char *x = text;
6647 struct syscall s;
6648 get_syscall_by_number (iter, &s);
6649
6650 if (s.name != NULL)
6651 text = xstrprintf ("%s%s, ", text, s.name);
6652 else
6653 text = xstrprintf ("%s%d, ", text, iter);
6654
6655 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 6656 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
6657 on every call. */
6658 xfree (x);
6659 }
6660 /* Remove the last comma. */
6661 text[strlen (text) - 2] = '\0';
6662 ui_out_field_string (uiout, "what", text);
6663 }
6664 else
6665 ui_out_field_string (uiout, "what", "<any syscall>");
6666 ui_out_text (uiout, "\" ");
6667}
6668
6669/* Implement the "print_mention" breakpoint_ops method for syscall
6670 catchpoints. */
6671
6672static void
6673print_mention_catch_syscall (struct breakpoint *b)
6674{
be5c67c1
PA
6675 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6676
6677 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6678 {
6679 int i, iter;
6680
be5c67c1 6681 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6682 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6683 else
6684 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6685
6686 for (i = 0;
be5c67c1 6687 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6688 i++)
6689 {
6690 struct syscall s;
6691 get_syscall_by_number (iter, &s);
6692
6693 if (s.name)
6694 printf_filtered (" '%s' [%d]", s.name, s.number);
6695 else
6696 printf_filtered (" %d", s.number);
6697 }
6698 printf_filtered (")");
6699 }
6700 else
6701 printf_filtered (_("Catchpoint %d (any syscall)"),
6702 b->number);
6703}
6704
6149aea9
PA
6705/* Implement the "print_recreate" breakpoint_ops method for syscall
6706 catchpoints. */
6707
6708static void
6709print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6710{
be5c67c1
PA
6711 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6712
6149aea9
PA
6713 fprintf_unfiltered (fp, "catch syscall");
6714
be5c67c1 6715 if (c->syscalls_to_be_caught)
6149aea9
PA
6716 {
6717 int i, iter;
6718
6719 for (i = 0;
be5c67c1 6720 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
6721 i++)
6722 {
6723 struct syscall s;
6724
6725 get_syscall_by_number (iter, &s);
6726 if (s.name)
6727 fprintf_unfiltered (fp, " %s", s.name);
6728 else
6729 fprintf_unfiltered (fp, " %d", s.number);
6730 }
6731 }
d9b3f62e 6732 print_recreate_thread (b, fp);
6149aea9
PA
6733}
6734
a96d9b2e
SDJ
6735/* The breakpoint_ops structure to be used in syscall catchpoints. */
6736
2060206e 6737static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
6738
6739/* Returns non-zero if 'b' is a syscall catchpoint. */
6740
6741static int
6742syscall_catchpoint_p (struct breakpoint *b)
6743{
6744 return (b->ops == &catch_syscall_breakpoint_ops);
6745}
6746
346774a9
PA
6747/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
6748 is non-zero, then make the breakpoint temporary. If COND_STRING is
6749 not NULL, then store it in the breakpoint. OPS, if not NULL, is
6750 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 6751
346774a9
PA
6752static void
6753init_catchpoint (struct breakpoint *b,
6754 struct gdbarch *gdbarch, int tempflag,
6755 char *cond_string,
c0a91b2b 6756 const struct breakpoint_ops *ops)
c906108c 6757{
c5aa993b 6758 struct symtab_and_line sal;
346774a9 6759
fe39c653 6760 init_sal (&sal);
6c95b8df 6761 sal.pspace = current_program_space;
c5aa993b 6762
28010a5d 6763 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 6764
1b36a34b 6765 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 6766 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
6767}
6768
28010a5d 6769void
3ea46bff 6770install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
6771{
6772 add_to_breakpoint_chain (b);
3a5c3e22
PA
6773 set_breakpoint_number (internal, b);
6774 if (!internal)
6775 mention (b);
c56053d2 6776 observer_notify_breakpoint_created (b);
3ea46bff
YQ
6777
6778 if (update_gll)
6779 update_global_location_list (1);
c56053d2
PA
6780}
6781
9b70b993 6782static void
a6d9a66e
UW
6783create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6784 int tempflag, char *cond_string,
c0a91b2b 6785 const struct breakpoint_ops *ops)
c906108c 6786{
e29a4733 6787 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 6788
e29a4733
PA
6789 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
6790
6791 c->forked_inferior_pid = null_ptid;
6792
3ea46bff 6793 install_breakpoint (0, &c->base, 1);
c906108c
SS
6794}
6795
fe798b75
JB
6796/* Exec catchpoints. */
6797
b4d90040
PA
6798/* An instance of this type is used to represent an exec catchpoint.
6799 It includes a "struct breakpoint" as a kind of base class; users
6800 downcast to "struct breakpoint *" when needed. A breakpoint is
6801 really of this type iff its ops pointer points to
6802 CATCH_EXEC_BREAKPOINT_OPS. */
6803
6804struct exec_catchpoint
6805{
6806 /* The base class. */
6807 struct breakpoint base;
6808
6809 /* Filename of a program whose exec triggered this catchpoint.
6810 This field is only valid immediately after this catchpoint has
6811 triggered. */
6812 char *exec_pathname;
6813};
6814
6815/* Implement the "dtor" breakpoint_ops method for exec
6816 catchpoints. */
6817
6818static void
6819dtor_catch_exec (struct breakpoint *b)
6820{
6821 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6822
6823 xfree (c->exec_pathname);
348d480f 6824
2060206e 6825 base_breakpoint_ops.dtor (b);
b4d90040
PA
6826}
6827
77b06cd7
TJB
6828static int
6829insert_catch_exec (struct bp_location *bl)
c906108c 6830{
77b06cd7 6831 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6832}
c906108c 6833
fe798b75 6834static int
77b06cd7 6835remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6836{
6837 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6838}
c906108c 6839
fe798b75 6840static int
f1310107
TJB
6841breakpoint_hit_catch_exec (const struct bp_location *bl,
6842 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6843{
b4d90040
PA
6844 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
6845
6846 return inferior_has_execd (inferior_ptid, &c->exec_pathname);
fe798b75 6847}
c906108c 6848
fe798b75 6849static enum print_stop_action
348d480f 6850print_it_catch_exec (bpstat bs)
fe798b75 6851{
36dfb11c 6852 struct ui_out *uiout = current_uiout;
348d480f 6853 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
6854 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6855
fe798b75 6856 annotate_catchpoint (b->number);
36dfb11c
TT
6857 if (b->disposition == disp_del)
6858 ui_out_text (uiout, "\nTemporary catchpoint ");
6859 else
6860 ui_out_text (uiout, "\nCatchpoint ");
6861 if (ui_out_is_mi_like_p (uiout))
6862 {
6863 ui_out_field_string (uiout, "reason",
6864 async_reason_lookup (EXEC_ASYNC_EXEC));
6865 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6866 }
6867 ui_out_field_int (uiout, "bkptno", b->number);
6868 ui_out_text (uiout, " (exec'd ");
6869 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
6870 ui_out_text (uiout, "), ");
6871
fe798b75 6872 return PRINT_SRC_AND_LOC;
c906108c
SS
6873}
6874
fe798b75 6875static void
a6d9a66e 6876print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 6877{
b4d90040 6878 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 6879 struct value_print_options opts;
79a45e25 6880 struct ui_out *uiout = current_uiout;
fe798b75
JB
6881
6882 get_user_print_options (&opts);
6883
6884 /* Field 4, the address, is omitted (which makes the columns
6885 not line up too nicely with the headers, but the effect
6886 is relatively readable). */
6887 if (opts.addressprint)
6888 ui_out_field_skip (uiout, "addr");
6889 annotate_field (5);
6890 ui_out_text (uiout, "exec");
b4d90040 6891 if (c->exec_pathname != NULL)
fe798b75
JB
6892 {
6893 ui_out_text (uiout, ", program \"");
b4d90040 6894 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
6895 ui_out_text (uiout, "\" ");
6896 }
6897}
6898
6899static void
6900print_mention_catch_exec (struct breakpoint *b)
6901{
6902 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6903}
6904
6149aea9
PA
6905/* Implement the "print_recreate" breakpoint_ops method for exec
6906 catchpoints. */
6907
6908static void
6909print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6910{
6911 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 6912 print_recreate_thread (b, fp);
6149aea9
PA
6913}
6914
2060206e 6915static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 6916
a96d9b2e
SDJ
6917static void
6918create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 6919 const struct breakpoint_ops *ops)
a96d9b2e 6920{
be5c67c1 6921 struct syscall_catchpoint *c;
a96d9b2e 6922 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 6923
be5c67c1
PA
6924 c = XNEW (struct syscall_catchpoint);
6925 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
6926 c->syscalls_to_be_caught = filter;
a96d9b2e 6927
3ea46bff 6928 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
6929}
6930
c906108c 6931static int
fba45db2 6932hw_breakpoint_used_count (void)
c906108c 6933{
c906108c 6934 int i = 0;
f1310107
TJB
6935 struct breakpoint *b;
6936 struct bp_location *bl;
c906108c
SS
6937
6938 ALL_BREAKPOINTS (b)
c5aa993b 6939 {
d6b74ac4 6940 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
6941 for (bl = b->loc; bl; bl = bl->next)
6942 {
6943 /* Special types of hardware breakpoints may use more than
6944 one register. */
348d480f 6945 i += b->ops->resources_needed (bl);
f1310107 6946 }
c5aa993b 6947 }
c906108c
SS
6948
6949 return i;
6950}
6951
a1398e0c
PA
6952/* Returns the resources B would use if it were a hardware
6953 watchpoint. */
6954
c906108c 6955static int
a1398e0c 6956hw_watchpoint_use_count (struct breakpoint *b)
c906108c 6957{
c906108c 6958 int i = 0;
e09342b5 6959 struct bp_location *bl;
c906108c 6960
a1398e0c
PA
6961 if (!breakpoint_enabled (b))
6962 return 0;
6963
6964 for (bl = b->loc; bl; bl = bl->next)
6965 {
6966 /* Special types of hardware watchpoints may use more than
6967 one register. */
6968 i += b->ops->resources_needed (bl);
6969 }
6970
6971 return i;
6972}
6973
6974/* Returns the sum the used resources of all hardware watchpoints of
6975 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
6976 the sum of the used resources of all hardware watchpoints of other
6977 types _not_ TYPE. */
6978
6979static int
6980hw_watchpoint_used_count_others (struct breakpoint *except,
6981 enum bptype type, int *other_type_used)
6982{
6983 int i = 0;
6984 struct breakpoint *b;
6985
c906108c
SS
6986 *other_type_used = 0;
6987 ALL_BREAKPOINTS (b)
e09342b5 6988 {
a1398e0c
PA
6989 if (b == except)
6990 continue;
e09342b5
TJB
6991 if (!breakpoint_enabled (b))
6992 continue;
6993
a1398e0c
PA
6994 if (b->type == type)
6995 i += hw_watchpoint_use_count (b);
6996 else if (is_hardware_watchpoint (b))
6997 *other_type_used = 1;
e09342b5
TJB
6998 }
6999
c906108c
SS
7000 return i;
7001}
7002
c906108c 7003void
fba45db2 7004disable_watchpoints_before_interactive_call_start (void)
c906108c 7005{
c5aa993b 7006 struct breakpoint *b;
c906108c
SS
7007
7008 ALL_BREAKPOINTS (b)
c5aa993b 7009 {
cc60f2e3 7010 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 7011 {
b5de0fa7 7012 b->enable_state = bp_call_disabled;
b60e7edf 7013 update_global_location_list (0);
c5aa993b
JM
7014 }
7015 }
c906108c
SS
7016}
7017
7018void
fba45db2 7019enable_watchpoints_after_interactive_call_stop (void)
c906108c 7020{
c5aa993b 7021 struct breakpoint *b;
c906108c
SS
7022
7023 ALL_BREAKPOINTS (b)
c5aa993b 7024 {
cc60f2e3 7025 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 7026 {
b5de0fa7 7027 b->enable_state = bp_enabled;
b60e7edf 7028 update_global_location_list (1);
c5aa993b
JM
7029 }
7030 }
c906108c
SS
7031}
7032
8bea4e01
UW
7033void
7034disable_breakpoints_before_startup (void)
7035{
6c95b8df 7036 current_program_space->executing_startup = 1;
f8eba3c6 7037 update_global_location_list (0);
8bea4e01
UW
7038}
7039
7040void
7041enable_breakpoints_after_startup (void)
7042{
6c95b8df 7043 current_program_space->executing_startup = 0;
f8eba3c6 7044 breakpoint_re_set ();
8bea4e01
UW
7045}
7046
c906108c
SS
7047
7048/* Set a breakpoint that will evaporate an end of command
7049 at address specified by SAL.
7050 Restrict it to frame FRAME if FRAME is nonzero. */
7051
7052struct breakpoint *
a6d9a66e
UW
7053set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7054 struct frame_id frame_id, enum bptype type)
c906108c 7055{
52f0bd74 7056 struct breakpoint *b;
edb3359d
DJ
7057
7058 /* If FRAME_ID is valid, it should be a real frame, not an inlined
7059 one. */
7060 gdb_assert (!frame_id_inlined_p (frame_id));
7061
06edf0c0 7062 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
7063 b->enable_state = bp_enabled;
7064 b->disposition = disp_donttouch;
818dd999 7065 b->frame_id = frame_id;
c906108c 7066
4a64f543
MS
7067 /* If we're debugging a multi-threaded program, then we want
7068 momentary breakpoints to be active in only a single thread of
7069 control. */
39f77062
KB
7070 if (in_thread_list (inferior_ptid))
7071 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 7072
b60e7edf 7073 update_global_location_list_nothrow (1);
74960c60 7074
c906108c
SS
7075 return b;
7076}
611c83ae 7077
06edf0c0
PA
7078/* Make a momentary breakpoint based on the master breakpoint ORIG.
7079 The new breakpoint will have type TYPE, and use OPS as it
7080 breakpoint_ops. */
e58b0e63 7081
06edf0c0
PA
7082static struct breakpoint *
7083momentary_breakpoint_from_master (struct breakpoint *orig,
7084 enum bptype type,
c0a91b2b 7085 const struct breakpoint_ops *ops)
e58b0e63
PA
7086{
7087 struct breakpoint *copy;
7088
06edf0c0 7089 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 7090 copy->loc = allocate_bp_location (copy);
0e30163f 7091 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 7092
a6d9a66e 7093 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7094 copy->loc->requested_address = orig->loc->requested_address;
7095 copy->loc->address = orig->loc->address;
7096 copy->loc->section = orig->loc->section;
6c95b8df 7097 copy->loc->pspace = orig->loc->pspace;
e58b0e63 7098
f8eba3c6
TT
7099 if (orig->loc->source_file != NULL)
7100 copy->loc->source_file = xstrdup (orig->loc->source_file);
e58b0e63 7101
f8eba3c6 7102 copy->loc->line_number = orig->loc->line_number;
e58b0e63
PA
7103 copy->frame_id = orig->frame_id;
7104 copy->thread = orig->thread;
6c95b8df 7105 copy->pspace = orig->pspace;
e58b0e63
PA
7106
7107 copy->enable_state = bp_enabled;
7108 copy->disposition = disp_donttouch;
7109 copy->number = internal_breakpoint_number--;
7110
7111 update_global_location_list_nothrow (0);
7112 return copy;
7113}
7114
06edf0c0
PA
7115/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7116 ORIG is NULL. */
7117
7118struct breakpoint *
7119clone_momentary_breakpoint (struct breakpoint *orig)
7120{
7121 /* If there's nothing to clone, then return nothing. */
7122 if (orig == NULL)
7123 return NULL;
7124
7125 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
7126}
7127
611c83ae 7128struct breakpoint *
a6d9a66e
UW
7129set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7130 enum bptype type)
611c83ae
PA
7131{
7132 struct symtab_and_line sal;
7133
7134 sal = find_pc_line (pc, 0);
7135 sal.pc = pc;
7136 sal.section = find_pc_overlay (pc);
7137 sal.explicit_pc = 1;
7138
a6d9a66e 7139 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7140}
c906108c 7141\f
c5aa993b 7142
c906108c
SS
7143/* Tell the user we have just set a breakpoint B. */
7144
7145static void
fba45db2 7146mention (struct breakpoint *b)
c906108c 7147{
348d480f 7148 b->ops->print_mention (b);
79a45e25 7149 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 7150 return;
c906108c
SS
7151 printf_filtered ("\n");
7152}
c906108c 7153\f
c5aa993b 7154
0d381245 7155static struct bp_location *
39d61571 7156add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7157 const struct symtab_and_line *sal)
7158{
7159 struct bp_location *loc, **tmp;
3742cc8b
YQ
7160 CORE_ADDR adjusted_address;
7161 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
7162
7163 if (loc_gdbarch == NULL)
7164 loc_gdbarch = b->gdbarch;
7165
7166 /* Adjust the breakpoint's address prior to allocating a location.
7167 Once we call allocate_bp_location(), that mostly uninitialized
7168 location will be placed on the location chain. Adjustment of the
7169 breakpoint may cause target_read_memory() to be called and we do
7170 not want its scan of the location chain to find a breakpoint and
7171 location that's only been partially initialized. */
7172 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
7173 sal->pc, b->type);
0d381245 7174
39d61571 7175 loc = allocate_bp_location (b);
0d381245
VP
7176 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7177 ;
7178 *tmp = loc;
3742cc8b 7179
0d381245 7180 loc->requested_address = sal->pc;
3742cc8b 7181 loc->address = adjusted_address;
6c95b8df
PA
7182 loc->pspace = sal->pspace;
7183 gdb_assert (loc->pspace != NULL);
0d381245 7184 loc->section = sal->section;
3742cc8b 7185 loc->gdbarch = loc_gdbarch;
f8eba3c6
TT
7186
7187 if (sal->symtab != NULL)
7188 loc->source_file = xstrdup (sal->symtab->filename);
7189 loc->line_number = sal->line;
7190
0e30163f
JK
7191 set_breakpoint_location_function (loc,
7192 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7193 return loc;
7194}
514f746b
AR
7195\f
7196
7197/* Return 1 if LOC is pointing to a permanent breakpoint,
7198 return 0 otherwise. */
7199
7200static int
7201bp_loc_is_permanent (struct bp_location *loc)
7202{
7203 int len;
7204 CORE_ADDR addr;
1afeeb75 7205 const gdb_byte *bpoint;
514f746b 7206 gdb_byte *target_mem;
939c61fa
JK
7207 struct cleanup *cleanup;
7208 int retval = 0;
514f746b
AR
7209
7210 gdb_assert (loc != NULL);
7211
7212 addr = loc->address;
1afeeb75 7213 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7214
939c61fa 7215 /* Software breakpoints unsupported? */
1afeeb75 7216 if (bpoint == NULL)
939c61fa
JK
7217 return 0;
7218
514f746b
AR
7219 target_mem = alloca (len);
7220
939c61fa
JK
7221 /* Enable the automatic memory restoration from breakpoints while
7222 we read the memory. Otherwise we could say about our temporary
7223 breakpoints they are permanent. */
6c95b8df
PA
7224 cleanup = save_current_space_and_thread ();
7225
7226 switch_to_program_space_and_thread (loc->pspace);
7227 make_show_memory_breakpoints_cleanup (0);
939c61fa 7228
514f746b 7229 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 7230 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 7231 retval = 1;
514f746b 7232
939c61fa
JK
7233 do_cleanups (cleanup);
7234
7235 return retval;
514f746b
AR
7236}
7237
7238
c3f6f71d 7239
018d34a4
VP
7240/* Create a breakpoint with SAL as location. Use ADDR_STRING
7241 as textual description of the location, and COND_STRING
db107f19 7242 as condition expression. */
018d34a4
VP
7243
7244static void
d9b3f62e
PA
7245init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
7246 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 7247 char *filter, char *cond_string,
d9b3f62e
PA
7248 enum bptype type, enum bpdisp disposition,
7249 int thread, int task, int ignore_count,
c0a91b2b 7250 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e 7251 int enabled, int internal, int display_canonical)
018d34a4 7252{
0d381245 7253 int i;
018d34a4
VP
7254
7255 if (type == bp_hardware_breakpoint)
7256 {
fbbd034e
AS
7257 int target_resources_ok;
7258
7259 i = hw_breakpoint_used_count ();
7260 target_resources_ok =
7261 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7262 i + 1, 0);
7263 if (target_resources_ok == 0)
7264 error (_("No hardware breakpoint support in the target."));
7265 else if (target_resources_ok < 0)
7266 error (_("Hardware breakpoints used exceeds limit."));
7267 }
7268
6c95b8df
PA
7269 gdb_assert (sals.nelts > 0);
7270
0d381245
VP
7271 for (i = 0; i < sals.nelts; ++i)
7272 {
7273 struct symtab_and_line sal = sals.sals[i];
7274 struct bp_location *loc;
7275
7276 if (from_tty)
5af949e3
UW
7277 {
7278 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7279 if (!loc_gdbarch)
7280 loc_gdbarch = gdbarch;
7281
7282 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7283 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7284 }
0d381245
VP
7285
7286 if (i == 0)
7287 {
d9b3f62e 7288 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 7289 b->thread = thread;
4a306c9a 7290 b->task = task;
018d34a4 7291
0d381245
VP
7292 b->cond_string = cond_string;
7293 b->ignore_count = ignore_count;
41447f92 7294 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7295 b->disposition = disposition;
6c95b8df 7296
0fb4aa4b
PA
7297 if (type == bp_static_tracepoint)
7298 {
d9b3f62e 7299 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
7300 struct static_tracepoint_marker marker;
7301
7302 if (is_marker_spec (addr_string))
7303 {
7304 /* We already know the marker exists, otherwise, we
7305 wouldn't see a sal for it. */
7306 char *p = &addr_string[3];
7307 char *endp;
7308 char *marker_str;
0fb4aa4b 7309
e9cafbcc 7310 p = skip_spaces (p);
0fb4aa4b 7311
e9cafbcc 7312 endp = skip_to_space (p);
0fb4aa4b
PA
7313
7314 marker_str = savestring (p, endp - p);
d9b3f62e 7315 t->static_trace_marker_id = marker_str;
0fb4aa4b 7316
3e43a32a
MS
7317 printf_filtered (_("Probed static tracepoint "
7318 "marker \"%s\"\n"),
d9b3f62e 7319 t->static_trace_marker_id);
0fb4aa4b
PA
7320 }
7321 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7322 {
d9b3f62e 7323 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
7324 release_static_tracepoint_marker (&marker);
7325
3e43a32a
MS
7326 printf_filtered (_("Probed static tracepoint "
7327 "marker \"%s\"\n"),
d9b3f62e 7328 t->static_trace_marker_id);
0fb4aa4b
PA
7329 }
7330 else
3e43a32a
MS
7331 warning (_("Couldn't determine the static "
7332 "tracepoint marker to probe"));
0fb4aa4b
PA
7333 }
7334
0d381245
VP
7335 loc = b->loc;
7336 }
7337 else
018d34a4 7338 {
39d61571 7339 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7340 }
7341
514f746b
AR
7342 if (bp_loc_is_permanent (loc))
7343 make_breakpoint_permanent (b);
7344
0d381245
VP
7345 if (b->cond_string)
7346 {
7347 char *arg = b->cond_string;
d32a6982 7348 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7349 if (*arg)
db107f19 7350 error (_("Garbage %s follows condition"), arg);
018d34a4 7351 }
0d381245 7352 }
018d34a4 7353
56435ebe 7354 b->display_canonical = display_canonical;
018d34a4
VP
7355 if (addr_string)
7356 b->addr_string = addr_string;
7357 else
7358 /* addr_string has to be used or breakpoint_re_set will delete
7359 me. */
5af949e3
UW
7360 b->addr_string
7361 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 7362 b->filter = filter;
d9b3f62e 7363}
018d34a4 7364
d9b3f62e
PA
7365static void
7366create_breakpoint_sal (struct gdbarch *gdbarch,
7367 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 7368 char *filter, char *cond_string,
d9b3f62e
PA
7369 enum bptype type, enum bpdisp disposition,
7370 int thread, int task, int ignore_count,
c0a91b2b 7371 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e
PA
7372 int enabled, int internal, int display_canonical)
7373{
7374 struct breakpoint *b;
7375 struct cleanup *old_chain;
7376
7377 if (is_tracepoint_type (type))
7378 {
7379 struct tracepoint *t;
7380
7381 t = XCNEW (struct tracepoint);
7382 b = &t->base;
7383 }
7384 else
7385 b = XNEW (struct breakpoint);
7386
7387 old_chain = make_cleanup (xfree, b);
7388
7389 init_breakpoint_sal (b, gdbarch,
7390 sals, addr_string,
f8eba3c6 7391 filter, cond_string,
d9b3f62e
PA
7392 type, disposition,
7393 thread, task, ignore_count,
7394 ops, from_tty,
7395 enabled, internal, display_canonical);
7396 discard_cleanups (old_chain);
7397
3ea46bff 7398 install_breakpoint (internal, b, 0);
018d34a4
VP
7399}
7400
7401/* Add SALS.nelts breakpoints to the breakpoint table. For each
7402 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7403 value. COND_STRING, if not NULL, specified the condition to be
7404 used for all breakpoints. Essentially the only case where
7405 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7406 function. In that case, it's still not possible to specify
7407 separate conditions for different overloaded functions, so
7408 we take just a single condition string.
7409
c3f6f71d 7410 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7411 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7412 array contents). If the function fails (error() is called), the
7413 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7414 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7415
7416static void
8cdf0e15 7417create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 7418 struct linespec_result *canonical,
8cdf0e15
VP
7419 char *cond_string,
7420 enum bptype type, enum bpdisp disposition,
7421 int thread, int task, int ignore_count,
c0a91b2b 7422 const struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7423 int enabled, int internal)
c906108c 7424{
018d34a4 7425 int i;
f8eba3c6 7426 struct linespec_sals *lsal;
cc59ec59 7427
f8eba3c6
TT
7428 if (canonical->pre_expanded)
7429 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
7430
7431 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 7432 {
f8eba3c6
TT
7433 /* Note that 'addr_string' can be NULL in the case of a plain
7434 'break', without arguments. */
7435 char *addr_string = (canonical->addr_string
7436 ? xstrdup (canonical->addr_string)
7437 : NULL);
7438 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
7439 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 7440
f8eba3c6
TT
7441 make_cleanup (xfree, filter_string);
7442 create_breakpoint_sal (gdbarch, lsal->sals,
7443 addr_string,
7444 filter_string,
8cdf0e15 7445 cond_string, type, disposition,
84f4c1fe 7446 thread, task, ignore_count, ops,
56435ebe
TT
7447 from_tty, enabled, internal,
7448 canonical->special_display);
f8eba3c6 7449 discard_cleanups (inner);
c3f6f71d 7450 }
c3f6f71d 7451}
c906108c 7452
9998af43 7453/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7454 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7455 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7456 address strings. ADDRESS points to the end of the SAL.
7457
7458 The array and the line spec strings are allocated on the heap, it is
7459 the caller's responsibility to free them. */
c906108c 7460
b9362cc7 7461static void
c3f6f71d 7462parse_breakpoint_sals (char **address,
58438ac1 7463 struct linespec_result *canonical)
c3f6f71d
JM
7464{
7465 char *addr_start = *address;
cc59ec59 7466
c3f6f71d 7467 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7468 breakpoint. */
c3f6f71d
JM
7469 if ((*address) == NULL
7470 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 7471 {
1bfeeb0f
JL
7472 /* The last displayed codepoint, if it's valid, is our default breakpoint
7473 address. */
7474 if (last_displayed_sal_is_valid ())
c906108c 7475 {
f8eba3c6 7476 struct linespec_sals lsal;
c3f6f71d 7477 struct symtab_and_line sal;
cc59ec59 7478
4a64f543 7479 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 7480 lsal.sals.sals = (struct symtab_and_line *)
c906108c 7481 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
7482
7483 /* Set sal's pspace, pc, symtab, and line to the values
7484 corresponding to the last call to print_frame_info. */
7485 get_last_displayed_sal (&sal);
7486 sal.section = find_pc_overlay (sal.pc);
00903456 7487
4a64f543 7488 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
7489 where PC is the last displayed codepoint's address. So
7490 make sure to set sal.explicit_pc to prevent GDB from
7491 trying to expand the list of sals to include all other
7492 instances with the same symtab and line. */
00903456
JK
7493 sal.explicit_pc = 1;
7494
f8eba3c6
TT
7495 lsal.sals.sals[0] = sal;
7496 lsal.sals.nelts = 1;
7497 lsal.canonical = NULL;
7498
7499 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
7500 }
7501 else
8a3fe4f8 7502 error (_("No default breakpoint address now."));
c906108c
SS
7503 }
7504 else
7505 {
c906108c 7506 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7507 current_source_symtab (which is decode_line_1's default).
7508 This should produce the results we want almost all of the
f8eba3c6
TT
7509 time while leaving default_breakpoint_* alone. */
7510 if (last_displayed_sal_is_valid ())
7511 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
7512 get_last_displayed_symtab (),
7513 get_last_displayed_line (),
7514 canonical, NULL, NULL);
c906108c 7515 else
f8eba3c6
TT
7516 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
7517 (struct symtab *) NULL, 0,
7518 canonical, NULL, NULL);
c906108c 7519 }
c3f6f71d 7520}
c906108c 7521
c906108c 7522
c3f6f71d 7523/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7524 inserted as a breakpoint. If it can't throw an error. */
c906108c 7525
b9362cc7 7526static void
23e7acfb 7527breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7528{
7529 int i;
cc59ec59 7530
c3f6f71d 7531 for (i = 0; i < sals->nelts; i++)
ee53e872 7532 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7533}
7534
7a697b8d
SS
7535/* Fast tracepoints may have restrictions on valid locations. For
7536 instance, a fast tracepoint using a jump instead of a trap will
7537 likely have to overwrite more bytes than a trap would, and so can
7538 only be placed where the instruction is longer than the jump, or a
7539 multi-instruction sequence does not have a jump into the middle of
7540 it, etc. */
7541
7542static void
7543check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7544 struct symtabs_and_lines *sals)
7545{
7546 int i, rslt;
7547 struct symtab_and_line *sal;
7548 char *msg;
7549 struct cleanup *old_chain;
7550
7551 for (i = 0; i < sals->nelts; i++)
7552 {
f8eba3c6
TT
7553 struct gdbarch *sarch;
7554
7a697b8d
SS
7555 sal = &sals->sals[i];
7556
f8eba3c6
TT
7557 sarch = get_sal_arch (*sal);
7558 /* We fall back to GDBARCH if there is no architecture
7559 associated with SAL. */
7560 if (sarch == NULL)
7561 sarch = gdbarch;
7562 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
7563 NULL, &msg);
7564 old_chain = make_cleanup (xfree, msg);
7565
7566 if (!rslt)
7567 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 7568 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
7569
7570 do_cleanups (old_chain);
7571 }
7572}
7573
018d34a4
VP
7574/* Given TOK, a string specification of condition and thread, as
7575 accepted by the 'break' command, extract the condition
7576 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7577 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7578 If no condition is found, *COND_STRING is set to NULL.
7579 If no thread is found, *THREAD is set to -1. */
7580static void
7581find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7582 char **cond_string, int *thread, int *task)
018d34a4
VP
7583{
7584 *cond_string = NULL;
7585 *thread = -1;
7586 while (tok && *tok)
7587 {
7588 char *end_tok;
7589 int toklen;
7590 char *cond_start = NULL;
7591 char *cond_end = NULL;
cc59ec59 7592
e9cafbcc 7593 tok = skip_spaces (tok);
018d34a4 7594
e9cafbcc 7595 end_tok = skip_to_space (tok);
018d34a4
VP
7596
7597 toklen = end_tok - tok;
7598
7599 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7600 {
f7545552
TT
7601 struct expression *expr;
7602
018d34a4 7603 tok = cond_start = end_tok + 1;
f7545552
TT
7604 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7605 xfree (expr);
018d34a4
VP
7606 cond_end = tok;
7607 *cond_string = savestring (cond_start,
7608 cond_end - cond_start);
7609 }
7610 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7611 {
7612 char *tmptok;
7613
7614 tok = end_tok + 1;
7615 tmptok = tok;
7616 *thread = strtol (tok, &tok, 0);
7617 if (tok == tmptok)
7618 error (_("Junk after thread keyword."));
7619 if (!valid_thread_id (*thread))
7620 error (_("Unknown thread %d."), *thread);
7621 }
4a306c9a
JB
7622 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7623 {
7624 char *tmptok;
7625
7626 tok = end_tok + 1;
7627 tmptok = tok;
7628 *task = strtol (tok, &tok, 0);
7629 if (tok == tmptok)
7630 error (_("Junk after task keyword."));
7631 if (!valid_task_id (*task))
b6199126 7632 error (_("Unknown task %d."), *task);
4a306c9a 7633 }
018d34a4
VP
7634 else
7635 error (_("Junk at end of arguments."));
7636 }
7637}
7638
0fb4aa4b
PA
7639/* Decode a static tracepoint marker spec. */
7640
7641static struct symtabs_and_lines
7642decode_static_tracepoint_spec (char **arg_p)
7643{
7644 VEC(static_tracepoint_marker_p) *markers = NULL;
7645 struct symtabs_and_lines sals;
7646 struct symtab_and_line sal;
7647 struct symbol *sym;
7648 struct cleanup *old_chain;
7649 char *p = &(*arg_p)[3];
7650 char *endp;
7651 char *marker_str;
7652 int i;
7653
e9cafbcc 7654 p = skip_spaces (p);
0fb4aa4b 7655
e9cafbcc 7656 endp = skip_to_space (p);
0fb4aa4b
PA
7657
7658 marker_str = savestring (p, endp - p);
7659 old_chain = make_cleanup (xfree, marker_str);
7660
7661 markers = target_static_tracepoint_markers_by_strid (marker_str);
7662 if (VEC_empty(static_tracepoint_marker_p, markers))
7663 error (_("No known static tracepoint marker named %s"), marker_str);
7664
7665 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7666 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7667
7668 for (i = 0; i < sals.nelts; i++)
7669 {
7670 struct static_tracepoint_marker *marker;
7671
7672 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7673
7674 init_sal (&sals.sals[i]);
7675
7676 sals.sals[i] = find_pc_line (marker->address, 0);
7677 sals.sals[i].pc = marker->address;
7678
7679 release_static_tracepoint_marker (marker);
7680 }
7681
7682 do_cleanups (old_chain);
7683
7684 *arg_p = endp;
7685 return sals;
7686}
7687
fd9b8c24
PA
7688/* Set a breakpoint. This function is shared between CLI and MI
7689 functions for setting a breakpoint. This function has two major
7690 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7691 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7692 breakpoint location, address and thread. Otherwise, ARG is just
7693 the location of breakpoint, with condition and thread specified by
7694 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7695 the breakpoint number will be allocated from the internal
7696 breakpoint count. Returns true if any breakpoint was created;
7697 false otherwise. */
0101ce28 7698
8cdf0e15
VP
7699int
7700create_breakpoint (struct gdbarch *gdbarch,
7701 char *arg, char *cond_string, int thread,
7702 int parse_condition_and_thread,
0fb4aa4b 7703 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7704 int ignore_count,
7705 enum auto_boolean pending_break_support,
c0a91b2b 7706 const struct breakpoint_ops *ops,
84f4c1fe 7707 int from_tty, int enabled, int internal)
c3f6f71d 7708{
b78a6381 7709 volatile struct gdb_exception e;
f8eba3c6 7710 char *copy_arg = NULL;
c3f6f71d 7711 char *addr_start = arg;
7efd8fc2 7712 struct linespec_result canonical;
c3f6f71d 7713 struct cleanup *old_chain;
80c99de1 7714 struct cleanup *bkpt_chain = NULL;
05ff989b 7715 int i;
0101ce28 7716 int pending = 0;
4a306c9a 7717 int task = 0;
86b17b60 7718 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7719
348d480f
PA
7720 gdb_assert (ops != NULL);
7721
7efd8fc2 7722 init_linespec_result (&canonical);
c3f6f71d 7723
0fb4aa4b
PA
7724 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7725 {
7726 int i;
f8eba3c6 7727 struct linespec_sals lsal;
0fb4aa4b 7728
f8eba3c6 7729 lsal.sals = decode_static_tracepoint_spec (&arg);
0fb4aa4b
PA
7730
7731 copy_arg = savestring (addr_start, arg - addr_start);
f8eba3c6 7732
d14c4eb7 7733 canonical.addr_string = xstrdup (copy_arg);
f8eba3c6
TT
7734 lsal.canonical = xstrdup (copy_arg);
7735 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
7736
0fb4aa4b
PA
7737 goto done;
7738 }
7739
b78a6381
TT
7740 TRY_CATCH (e, RETURN_MASK_ALL)
7741 {
f8eba3c6 7742 parse_breakpoint_sals (&arg, &canonical);
b78a6381 7743 }
0101ce28
JJ
7744
7745 /* If caller is interested in rc value from parse, set value. */
05ff989b 7746 switch (e.reason)
0101ce28 7747 {
05ff989b 7748 case RETURN_QUIT:
98deb0da 7749 throw_exception (e);
05ff989b
AC
7750 case RETURN_ERROR:
7751 switch (e.error)
0101ce28 7752 {
05ff989b 7753 case NOT_FOUND_ERROR:
0101ce28 7754
05ff989b
AC
7755 /* If pending breakpoint support is turned off, throw
7756 error. */
fa8d40ab
JJ
7757
7758 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7759 throw_exception (e);
7760
7761 exception_print (gdb_stderr, e);
fa8d40ab 7762
05ff989b
AC
7763 /* If pending breakpoint support is auto query and the user
7764 selects no, then simply return the error code. */
059fb39f 7765 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
7766 && !nquery (_("Make %s pending on future shared library load? "),
7767 bptype_string (type_wanted)))
fd9b8c24 7768 return 0;
fa8d40ab 7769
05ff989b
AC
7770 /* At this point, either the user was queried about setting
7771 a pending breakpoint and selected yes, or pending
7772 breakpoint behavior is on and thus a pending breakpoint
7773 is defaulted on behalf of the user. */
f8eba3c6
TT
7774 {
7775 struct linespec_sals lsal;
7776
7777 copy_arg = xstrdup (addr_start);
7778 lsal.canonical = xstrdup (copy_arg);
7779 lsal.sals.nelts = 1;
7780 lsal.sals.sals = XNEW (struct symtab_and_line);
7781 init_sal (&lsal.sals.sals[0]);
7782 pending = 1;
7783 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
7784 }
05ff989b
AC
7785 break;
7786 default:
98deb0da 7787 throw_exception (e);
0101ce28 7788 }
2abae994 7789 break;
05ff989b 7790 default:
f8eba3c6 7791 if (VEC_empty (linespec_sals, canonical.sals))
fd9b8c24 7792 return 0;
0101ce28 7793 }
c3f6f71d 7794
0fb4aa4b
PA
7795 done:
7796
4a64f543 7797 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 7798 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 7799
c3f6f71d
JM
7800 /* ----------------------------- SNIP -----------------------------
7801 Anything added to the cleanup chain beyond this point is assumed
7802 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7803 then the memory is not reclaimed. */
7804 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 7805
c3f6f71d
JM
7806 /* Resolve all line numbers to PC's and verify that the addresses
7807 are ok for the target. */
0101ce28 7808 if (!pending)
f8eba3c6
TT
7809 {
7810 int ix;
7811 struct linespec_sals *iter;
7812
7813 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
7814 breakpoint_sals_to_pc (&iter->sals);
7815 }
c3f6f71d 7816
7a697b8d 7817 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 7818 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
7819 {
7820 int ix;
7821 struct linespec_sals *iter;
7822
7823 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
7824 check_fast_tracepoint_sals (gdbarch, &iter->sals);
7825 }
7a697b8d 7826
c3f6f71d
JM
7827 /* Verify that condition can be parsed, before setting any
7828 breakpoints. Allocate a separate condition expression for each
4a64f543 7829 breakpoint. */
0101ce28 7830 if (!pending)
c3f6f71d 7831 {
f8eba3c6
TT
7832 struct linespec_sals *lsal;
7833
7834 lsal = VEC_index (linespec_sals, canonical.sals, 0);
7835
2f069f6f 7836 if (parse_condition_and_thread)
72b2ff0e
VP
7837 {
7838 /* Here we only parse 'arg' to separate condition
7839 from thread number, so parsing in context of first
7840 sal is OK. When setting the breakpoint we'll
7841 re-parse it in context of each sal. */
7842 cond_string = NULL;
7843 thread = -1;
f8eba3c6 7844 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
4a306c9a 7845 &thread, &task);
72b2ff0e
VP
7846 if (cond_string)
7847 make_cleanup (xfree, cond_string);
7848 }
2f069f6f 7849 else
72b2ff0e
VP
7850 {
7851 /* Create a private copy of condition string. */
7852 if (cond_string)
7853 {
7854 cond_string = xstrdup (cond_string);
7855 make_cleanup (xfree, cond_string);
7856 }
7857 }
0fb4aa4b
PA
7858
7859 /* If the user is creating a static tracepoint by marker id
7860 (strace -m MARKER_ID), then store the sals index, so that
7861 breakpoint_re_set can try to match up which of the newly
7862 found markers corresponds to this one, and, don't try to
7863 expand multiple locations for each sal, given than SALS
7864 already should contain all sals for MARKER_ID. */
7865 if (type_wanted == bp_static_tracepoint
f8eba3c6 7866 && is_marker_spec (copy_arg))
0fb4aa4b
PA
7867 {
7868 int i;
7869
f8eba3c6 7870 for (i = 0; i < lsal->sals.nelts; ++i)
0fb4aa4b
PA
7871 {
7872 struct symtabs_and_lines expanded;
d9b3f62e 7873 struct tracepoint *tp;
0fb4aa4b 7874 struct cleanup *old_chain;
f8eba3c6 7875 char *addr_string;
0fb4aa4b
PA
7876
7877 expanded.nelts = 1;
f8eba3c6
TT
7878 expanded.sals = &lsal->sals.sals[i];
7879
7880 addr_string = xstrdup (canonical.addr_string);
7881 old_chain = make_cleanup (xfree, addr_string);
0fb4aa4b 7882
d9b3f62e
PA
7883 tp = XCNEW (struct tracepoint);
7884 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f8eba3c6 7885 addr_string, NULL,
d9b3f62e
PA
7886 cond_string, type_wanted,
7887 tempflag ? disp_del : disp_donttouch,
7888 thread, task, ignore_count, ops,
7889 from_tty, enabled, internal,
7890 canonical.special_display);
0fb4aa4b
PA
7891 /* Given that its possible to have multiple markers with
7892 the same string id, if the user is creating a static
7893 tracepoint by marker id ("strace -m MARKER_ID"), then
7894 store the sals index, so that breakpoint_re_set can
7895 try to match up which of the newly found markers
7896 corresponds to this one */
7897 tp->static_trace_marker_id_idx = i;
d9b3f62e 7898
3ea46bff 7899 install_breakpoint (internal, &tp->base, 0);
d9b3f62e 7900
f8eba3c6 7901 discard_cleanups (old_chain);
0fb4aa4b
PA
7902 }
7903 }
7904 else
f8eba3c6 7905 create_breakpoints_sal (gdbarch, &canonical, cond_string,
3e43a32a
MS
7906 type_wanted,
7907 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 7908 thread, task, ignore_count, ops, from_tty,
84f4c1fe 7909 enabled, internal);
c906108c 7910 }
0101ce28
JJ
7911 else
7912 {
0101ce28
JJ
7913 struct breakpoint *b;
7914
0101ce28
JJ
7915 make_cleanup (xfree, copy_arg);
7916
bfccc43c
YQ
7917 if (is_tracepoint_type (type_wanted))
7918 {
7919 struct tracepoint *t;
7920
7921 t = XCNEW (struct tracepoint);
7922 b = &t->base;
7923 }
7924 else
7925 b = XNEW (struct breakpoint);
7926
7927 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
7928
f8eba3c6 7929 b->addr_string = copy_arg;
72b2ff0e 7930 b->cond_string = NULL;
0101ce28 7931 b->ignore_count = ignore_count;
0101ce28 7932 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 7933 b->condition_not_parsed = 1;
41447f92 7934 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
7935 if ((type_wanted != bp_breakpoint
7936 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 7937 b->pspace = current_program_space;
8bea4e01 7938
bfccc43c 7939 install_breakpoint (internal, b, 0);
0101ce28
JJ
7940 }
7941
f8eba3c6 7942 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 7943 {
3e43a32a
MS
7944 warning (_("Multiple breakpoints were set.\nUse the "
7945 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 7946 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
7947 }
7948
80c99de1
PA
7949 /* That's it. Discard the cleanups for data inserted into the
7950 breakpoint. */
7951 discard_cleanups (bkpt_chain);
7952 /* But cleanup everything else. */
c3f6f71d 7953 do_cleanups (old_chain);
217dc9e2 7954
80c99de1 7955 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 7956 update_global_location_list (1);
fd9b8c24
PA
7957
7958 return 1;
c3f6f71d 7959}
c906108c 7960
348d480f 7961/* Set a breakpoint.
72b2ff0e
VP
7962 ARG is a string describing breakpoint address,
7963 condition, and thread.
7964 FLAG specifies if a breakpoint is hardware on,
7965 and if breakpoint is temporary, using BP_HARDWARE_FLAG
7966 and BP_TEMPFLAG. */
348d480f 7967
98deb0da 7968static void
72b2ff0e 7969break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 7970{
72b2ff0e 7971 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
7972 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
7973 ? bp_hardware_breakpoint
7974 : bp_breakpoint);
c3f6f71d 7975
8cdf0e15
VP
7976 create_breakpoint (get_current_arch (),
7977 arg,
7978 NULL, 0, 1 /* parse arg */,
0fb4aa4b 7979 tempflag, type_wanted,
8cdf0e15
VP
7980 0 /* Ignore count */,
7981 pending_break_support,
348d480f 7982 &bkpt_breakpoint_ops,
8cdf0e15 7983 from_tty,
84f4c1fe
PM
7984 1 /* enabled */,
7985 0 /* internal */);
c906108c
SS
7986}
7987
c906108c
SS
7988/* Helper function for break_command_1 and disassemble_command. */
7989
7990void
fba45db2 7991resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
7992{
7993 CORE_ADDR pc;
7994
7995 if (sal->pc == 0 && sal->symtab != NULL)
7996 {
7997 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 7998 error (_("No line %d in file \"%s\"."),
c906108c
SS
7999 sal->line, sal->symtab->filename);
8000 sal->pc = pc;
6a048695 8001
4a64f543
MS
8002 /* If this SAL corresponds to a breakpoint inserted using a line
8003 number, then skip the function prologue if necessary. */
6a048695 8004 if (sal->explicit_line)
059acae7 8005 skip_prologue_sal (sal);
c906108c
SS
8006 }
8007
8008 if (sal->section == 0 && sal->symtab != NULL)
8009 {
8010 struct blockvector *bv;
c5aa993b
JM
8011 struct block *b;
8012 struct symbol *sym;
c906108c 8013
801e3a5b 8014 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
8015 if (bv != NULL)
8016 {
7f0df278 8017 sym = block_linkage_function (b);
c906108c
SS
8018 if (sym != NULL)
8019 {
8020 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 8021 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
8022 }
8023 else
8024 {
4a64f543
MS
8025 /* It really is worthwhile to have the section, so we'll
8026 just have to look harder. This case can be executed
8027 if we have line numbers but no functions (as can
8028 happen in assembly source). */
c906108c 8029
c5aa993b 8030 struct minimal_symbol *msym;
6c95b8df
PA
8031 struct cleanup *old_chain = save_current_space_and_thread ();
8032
8033 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8034
8035 msym = lookup_minimal_symbol_by_pc (sal->pc);
8036 if (msym)
714835d5 8037 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8038
8039 do_cleanups (old_chain);
c906108c
SS
8040 }
8041 }
8042 }
8043}
8044
8045void
fba45db2 8046break_command (char *arg, int from_tty)
c906108c 8047{
db107f19 8048 break_command_1 (arg, 0, from_tty);
c906108c
SS
8049}
8050
c906108c 8051void
fba45db2 8052tbreak_command (char *arg, int from_tty)
c906108c 8053{
db107f19 8054 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8055}
8056
c906108c 8057static void
fba45db2 8058hbreak_command (char *arg, int from_tty)
c906108c 8059{
db107f19 8060 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8061}
8062
8063static void
fba45db2 8064thbreak_command (char *arg, int from_tty)
c906108c 8065{
db107f19 8066 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8067}
8068
8069static void
fba45db2 8070stop_command (char *arg, int from_tty)
c906108c 8071{
a3f17187 8072 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8073Usage: stop in <function | address>\n\
a3f17187 8074 stop at <line>\n"));
c906108c
SS
8075}
8076
8077static void
fba45db2 8078stopin_command (char *arg, int from_tty)
c906108c
SS
8079{
8080 int badInput = 0;
8081
c5aa993b 8082 if (arg == (char *) NULL)
c906108c
SS
8083 badInput = 1;
8084 else if (*arg != '*')
8085 {
8086 char *argptr = arg;
8087 int hasColon = 0;
8088
4a64f543 8089 /* Look for a ':'. If this is a line number specification, then
53a5351d 8090 say it is bad, otherwise, it should be an address or
4a64f543 8091 function/method name. */
c906108c 8092 while (*argptr && !hasColon)
c5aa993b
JM
8093 {
8094 hasColon = (*argptr == ':');
8095 argptr++;
8096 }
c906108c
SS
8097
8098 if (hasColon)
c5aa993b 8099 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8100 else
c5aa993b 8101 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8102 }
8103
8104 if (badInput)
a3f17187 8105 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8106 else
db107f19 8107 break_command_1 (arg, 0, from_tty);
c906108c
SS
8108}
8109
8110static void
fba45db2 8111stopat_command (char *arg, int from_tty)
c906108c
SS
8112{
8113 int badInput = 0;
8114
c5aa993b 8115 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8116 badInput = 1;
8117 else
8118 {
8119 char *argptr = arg;
8120 int hasColon = 0;
8121
4a64f543
MS
8122 /* Look for a ':'. If there is a '::' then get out, otherwise
8123 it is probably a line number. */
c906108c 8124 while (*argptr && !hasColon)
c5aa993b
JM
8125 {
8126 hasColon = (*argptr == ':');
8127 argptr++;
8128 }
c906108c
SS
8129
8130 if (hasColon)
c5aa993b 8131 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8132 else
c5aa993b 8133 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8134 }
8135
8136 if (badInput)
a3f17187 8137 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8138 else
db107f19 8139 break_command_1 (arg, 0, from_tty);
c906108c
SS
8140}
8141
f1310107
TJB
8142/* Implement the "breakpoint_hit" breakpoint_ops method for
8143 ranged breakpoints. */
8144
8145static int
8146breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8147 struct address_space *aspace,
8148 CORE_ADDR bp_addr)
8149{
8150 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8151 bl->length, aspace, bp_addr);
8152}
8153
8154/* Implement the "resources_needed" breakpoint_ops method for
8155 ranged breakpoints. */
8156
8157static int
8158resources_needed_ranged_breakpoint (const struct bp_location *bl)
8159{
8160 return target_ranged_break_num_registers ();
8161}
8162
8163/* Implement the "print_it" breakpoint_ops method for
8164 ranged breakpoints. */
8165
8166static enum print_stop_action
348d480f 8167print_it_ranged_breakpoint (bpstat bs)
f1310107 8168{
348d480f 8169 struct breakpoint *b = bs->breakpoint_at;
f1310107 8170 struct bp_location *bl = b->loc;
79a45e25 8171 struct ui_out *uiout = current_uiout;
f1310107
TJB
8172
8173 gdb_assert (b->type == bp_hardware_breakpoint);
8174
8175 /* Ranged breakpoints have only one location. */
8176 gdb_assert (bl && bl->next == NULL);
8177
8178 annotate_breakpoint (b->number);
8179 if (b->disposition == disp_del)
8180 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8181 else
8182 ui_out_text (uiout, "\nRanged breakpoint ");
8183 if (ui_out_is_mi_like_p (uiout))
8184 {
8185 ui_out_field_string (uiout, "reason",
8186 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8187 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8188 }
8189 ui_out_field_int (uiout, "bkptno", b->number);
8190 ui_out_text (uiout, ", ");
8191
8192 return PRINT_SRC_AND_LOC;
8193}
8194
8195/* Implement the "print_one" breakpoint_ops method for
8196 ranged breakpoints. */
8197
8198static void
8199print_one_ranged_breakpoint (struct breakpoint *b,
8200 struct bp_location **last_loc)
8201{
8202 struct bp_location *bl = b->loc;
8203 struct value_print_options opts;
79a45e25 8204 struct ui_out *uiout = current_uiout;
f1310107
TJB
8205
8206 /* Ranged breakpoints have only one location. */
8207 gdb_assert (bl && bl->next == NULL);
8208
8209 get_user_print_options (&opts);
8210
8211 if (opts.addressprint)
8212 /* We don't print the address range here, it will be printed later
8213 by print_one_detail_ranged_breakpoint. */
8214 ui_out_field_skip (uiout, "addr");
8215 annotate_field (5);
8216 print_breakpoint_location (b, bl);
8217 *last_loc = bl;
8218}
8219
8220/* Implement the "print_one_detail" breakpoint_ops method for
8221 ranged breakpoints. */
8222
8223static void
8224print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8225 struct ui_out *uiout)
8226{
8227 CORE_ADDR address_start, address_end;
8228 struct bp_location *bl = b->loc;
8229 struct ui_stream *stb = ui_out_stream_new (uiout);
8230 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8231
8232 gdb_assert (bl);
8233
8234 address_start = bl->address;
8235 address_end = address_start + bl->length - 1;
8236
8237 ui_out_text (uiout, "\taddress range: ");
8238 fprintf_unfiltered (stb->stream, "[%s, %s]",
8239 print_core_address (bl->gdbarch, address_start),
8240 print_core_address (bl->gdbarch, address_end));
8241 ui_out_field_stream (uiout, "addr", stb);
8242 ui_out_text (uiout, "\n");
8243
8244 do_cleanups (cleanup);
8245}
8246
8247/* Implement the "print_mention" breakpoint_ops method for
8248 ranged breakpoints. */
8249
8250static void
8251print_mention_ranged_breakpoint (struct breakpoint *b)
8252{
8253 struct bp_location *bl = b->loc;
79a45e25 8254 struct ui_out *uiout = current_uiout;
f1310107
TJB
8255
8256 gdb_assert (bl);
8257 gdb_assert (b->type == bp_hardware_breakpoint);
8258
8259 if (ui_out_is_mi_like_p (uiout))
8260 return;
8261
8262 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8263 b->number, paddress (bl->gdbarch, bl->address),
8264 paddress (bl->gdbarch, bl->address + bl->length - 1));
8265}
8266
8267/* Implement the "print_recreate" breakpoint_ops method for
8268 ranged breakpoints. */
8269
8270static void
8271print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8272{
8273 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8274 b->addr_string_range_end);
d9b3f62e 8275 print_recreate_thread (b, fp);
f1310107
TJB
8276}
8277
8278/* The breakpoint_ops structure to be used in ranged breakpoints. */
8279
2060206e 8280static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
8281
8282/* Find the address where the end of the breakpoint range should be
8283 placed, given the SAL of the end of the range. This is so that if
8284 the user provides a line number, the end of the range is set to the
8285 last instruction of the given line. */
8286
8287static CORE_ADDR
8288find_breakpoint_range_end (struct symtab_and_line sal)
8289{
8290 CORE_ADDR end;
8291
8292 /* If the user provided a PC value, use it. Otherwise,
8293 find the address of the end of the given location. */
8294 if (sal.explicit_pc)
8295 end = sal.pc;
8296 else
8297 {
8298 int ret;
8299 CORE_ADDR start;
8300
8301 ret = find_line_pc_range (sal, &start, &end);
8302 if (!ret)
8303 error (_("Could not find location of the end of the range."));
8304
8305 /* find_line_pc_range returns the start of the next line. */
8306 end--;
8307 }
8308
8309 return end;
8310}
8311
8312/* Implement the "break-range" CLI command. */
8313
8314static void
8315break_range_command (char *arg, int from_tty)
8316{
8317 char *arg_start, *addr_string_start, *addr_string_end;
8318 struct linespec_result canonical_start, canonical_end;
8319 int bp_count, can_use_bp, length;
8320 CORE_ADDR end;
8321 struct breakpoint *b;
8322 struct symtab_and_line sal_start, sal_end;
f1310107 8323 struct cleanup *cleanup_bkpt;
f8eba3c6 8324 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
8325
8326 /* We don't support software ranged breakpoints. */
8327 if (target_ranged_break_num_registers () < 0)
8328 error (_("This target does not support hardware ranged breakpoints."));
8329
8330 bp_count = hw_breakpoint_used_count ();
8331 bp_count += target_ranged_break_num_registers ();
8332 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8333 bp_count, 0);
8334 if (can_use_bp < 0)
8335 error (_("Hardware breakpoints used exceeds limit."));
8336
f8eba3c6 8337 arg = skip_spaces (arg);
f1310107
TJB
8338 if (arg == NULL || arg[0] == '\0')
8339 error(_("No address range specified."));
8340
f1310107
TJB
8341 init_linespec_result (&canonical_start);
8342
f8eba3c6
TT
8343 arg_start = arg;
8344 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 8345
f8eba3c6 8346 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
8347
8348 if (arg[0] != ',')
8349 error (_("Too few arguments."));
f8eba3c6 8350 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 8351 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
8352
8353 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
8354
8355 if (VEC_length (linespec_sals, canonical_start.sals) > 1
8356 || lsal_start->sals.nelts != 1)
f1310107
TJB
8357 error (_("Cannot create a ranged breakpoint with multiple locations."));
8358
f8eba3c6
TT
8359 sal_start = lsal_start->sals.sals[0];
8360 addr_string_start = savestring (arg_start, arg - arg_start);
8361 make_cleanup (xfree, addr_string_start);
f1310107
TJB
8362
8363 arg++; /* Skip the comma. */
f8eba3c6 8364 arg = skip_spaces (arg);
f1310107
TJB
8365
8366 /* Parse the end location. */
8367
f1310107
TJB
8368 init_linespec_result (&canonical_end);
8369 arg_start = arg;
8370
f8eba3c6 8371 /* We call decode_line_full directly here instead of using
f1310107
TJB
8372 parse_breakpoint_sals because we need to specify the start location's
8373 symtab and line as the default symtab and line for the end of the
8374 range. This makes it possible to have ranges like "foo.c:27, +14",
8375 where +14 means 14 lines from the start location. */
f8eba3c6
TT
8376 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
8377 sal_start.symtab, sal_start.line,
8378 &canonical_end, NULL, NULL);
8379
8380 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 8381
f8eba3c6 8382 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 8383 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
8384
8385 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
8386 if (VEC_length (linespec_sals, canonical_end.sals) > 1
8387 || lsal_end->sals.nelts != 1)
f1310107
TJB
8388 error (_("Cannot create a ranged breakpoint with multiple locations."));
8389
f8eba3c6
TT
8390 sal_end = lsal_end->sals.sals[0];
8391 addr_string_end = savestring (arg_start, arg - arg_start);
8392 make_cleanup (xfree, addr_string_end);
f1310107
TJB
8393
8394 end = find_breakpoint_range_end (sal_end);
8395 if (sal_start.pc > end)
177b42fe 8396 error (_("Invalid address range, end precedes start."));
f1310107
TJB
8397
8398 length = end - sal_start.pc + 1;
8399 if (length < 0)
8400 /* Length overflowed. */
8401 error (_("Address range too large."));
8402 else if (length == 1)
8403 {
8404 /* This range is simple enough to be handled by
8405 the `hbreak' command. */
8406 hbreak_command (addr_string_start, 1);
8407
8408 do_cleanups (cleanup_bkpt);
8409
8410 return;
8411 }
8412
8413 /* Now set up the breakpoint. */
8414 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 8415 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
8416 set_breakpoint_count (breakpoint_count + 1);
8417 b->number = breakpoint_count;
8418 b->disposition = disp_donttouch;
f8eba3c6
TT
8419 b->addr_string = xstrdup (addr_string_start);
8420 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
8421 b->loc->length = length;
8422
f8eba3c6 8423 do_cleanups (cleanup_bkpt);
f1310107
TJB
8424
8425 mention (b);
8d3788bd 8426 observer_notify_breakpoint_created (b);
f1310107
TJB
8427 update_global_location_list (1);
8428}
8429
4a64f543
MS
8430/* Return non-zero if EXP is verified as constant. Returned zero
8431 means EXP is variable. Also the constant detection may fail for
8432 some constant expressions and in such case still falsely return
8433 zero. */
65d79d4b
SDJ
8434static int
8435watchpoint_exp_is_const (const struct expression *exp)
8436{
8437 int i = exp->nelts;
8438
8439 while (i > 0)
8440 {
8441 int oplenp, argsp;
8442
8443 /* We are only interested in the descriptor of each element. */
8444 operator_length (exp, i, &oplenp, &argsp);
8445 i -= oplenp;
8446
8447 switch (exp->elts[i].opcode)
8448 {
8449 case BINOP_ADD:
8450 case BINOP_SUB:
8451 case BINOP_MUL:
8452 case BINOP_DIV:
8453 case BINOP_REM:
8454 case BINOP_MOD:
8455 case BINOP_LSH:
8456 case BINOP_RSH:
8457 case BINOP_LOGICAL_AND:
8458 case BINOP_LOGICAL_OR:
8459 case BINOP_BITWISE_AND:
8460 case BINOP_BITWISE_IOR:
8461 case BINOP_BITWISE_XOR:
8462 case BINOP_EQUAL:
8463 case BINOP_NOTEQUAL:
8464 case BINOP_LESS:
8465 case BINOP_GTR:
8466 case BINOP_LEQ:
8467 case BINOP_GEQ:
8468 case BINOP_REPEAT:
8469 case BINOP_COMMA:
8470 case BINOP_EXP:
8471 case BINOP_MIN:
8472 case BINOP_MAX:
8473 case BINOP_INTDIV:
8474 case BINOP_CONCAT:
8475 case BINOP_IN:
8476 case BINOP_RANGE:
8477 case TERNOP_COND:
8478 case TERNOP_SLICE:
8479 case TERNOP_SLICE_COUNT:
8480
8481 case OP_LONG:
8482 case OP_DOUBLE:
8483 case OP_DECFLOAT:
8484 case OP_LAST:
8485 case OP_COMPLEX:
8486 case OP_STRING:
8487 case OP_BITSTRING:
8488 case OP_ARRAY:
8489 case OP_TYPE:
8490 case OP_NAME:
8491 case OP_OBJC_NSSTRING:
8492
8493 case UNOP_NEG:
8494 case UNOP_LOGICAL_NOT:
8495 case UNOP_COMPLEMENT:
8496 case UNOP_ADDR:
8497 case UNOP_HIGH:
4a64f543
MS
8498 /* Unary, binary and ternary operators: We have to check
8499 their operands. If they are constant, then so is the
8500 result of that operation. For instance, if A and B are
8501 determined to be constants, then so is "A + B".
8502
8503 UNOP_IND is one exception to the rule above, because the
8504 value of *ADDR is not necessarily a constant, even when
8505 ADDR is. */
65d79d4b
SDJ
8506 break;
8507
8508 case OP_VAR_VALUE:
8509 /* Check whether the associated symbol is a constant.
4a64f543 8510
65d79d4b 8511 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8512 possible that a buggy compiler could mark a variable as
8513 constant even when it is not, and TYPE_CONST would return
8514 true in this case, while SYMBOL_CLASS wouldn't.
8515
8516 We also have to check for function symbols because they
8517 are always constant. */
65d79d4b
SDJ
8518 {
8519 struct symbol *s = exp->elts[i + 2].symbol;
8520
8521 if (SYMBOL_CLASS (s) != LOC_BLOCK
8522 && SYMBOL_CLASS (s) != LOC_CONST
8523 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8524 return 0;
8525 break;
8526 }
8527
8528 /* The default action is to return 0 because we are using
8529 the optimistic approach here: If we don't know something,
8530 then it is not a constant. */
8531 default:
8532 return 0;
8533 }
8534 }
8535
8536 return 1;
8537}
8538
3a5c3e22
PA
8539/* Implement the "dtor" breakpoint_ops method for watchpoints. */
8540
8541static void
8542dtor_watchpoint (struct breakpoint *self)
8543{
8544 struct watchpoint *w = (struct watchpoint *) self;
8545
8546 xfree (w->cond_exp);
8547 xfree (w->exp);
8548 xfree (w->exp_string);
8549 xfree (w->exp_string_reparse);
8550 value_free (w->val);
8551
8552 base_breakpoint_ops.dtor (self);
8553}
8554
348d480f
PA
8555/* Implement the "re_set" breakpoint_ops method for watchpoints. */
8556
8557static void
8558re_set_watchpoint (struct breakpoint *b)
8559{
3a5c3e22
PA
8560 struct watchpoint *w = (struct watchpoint *) b;
8561
348d480f
PA
8562 /* Watchpoint can be either on expression using entirely global
8563 variables, or it can be on local variables.
8564
8565 Watchpoints of the first kind are never auto-deleted, and even
8566 persist across program restarts. Since they can use variables
8567 from shared libraries, we need to reparse expression as libraries
8568 are loaded and unloaded.
8569
8570 Watchpoints on local variables can also change meaning as result
8571 of solib event. For example, if a watchpoint uses both a local
8572 and a global variables in expression, it's a local watchpoint,
8573 but unloading of a shared library will make the expression
8574 invalid. This is not a very common use case, but we still
8575 re-evaluate expression, to avoid surprises to the user.
8576
8577 Note that for local watchpoints, we re-evaluate it only if
8578 watchpoints frame id is still valid. If it's not, it means the
8579 watchpoint is out of scope and will be deleted soon. In fact,
8580 I'm not sure we'll ever be called in this case.
8581
8582 If a local watchpoint's frame id is still valid, then
3a5c3e22 8583 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 8584
3a5c3e22
PA
8585 Don't do anything about disabled watchpoints, since they will be
8586 reevaluated again when enabled. */
8587 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
8588}
8589
77b06cd7
TJB
8590/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8591
8592static int
8593insert_watchpoint (struct bp_location *bl)
8594{
3a5c3e22
PA
8595 struct watchpoint *w = (struct watchpoint *) bl->owner;
8596 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8597
8598 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8599 w->cond_exp);
77b06cd7
TJB
8600}
8601
8602/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8603
8604static int
8605remove_watchpoint (struct bp_location *bl)
8606{
3a5c3e22
PA
8607 struct watchpoint *w = (struct watchpoint *) bl->owner;
8608 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8609
8610 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8611 w->cond_exp);
e09342b5
TJB
8612}
8613
e09342b5 8614static int
348d480f
PA
8615breakpoint_hit_watchpoint (const struct bp_location *bl,
8616 struct address_space *aspace, CORE_ADDR bp_addr)
e09342b5 8617{
348d480f 8618 struct breakpoint *b = bl->owner;
3a5c3e22 8619 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 8620
348d480f
PA
8621 /* Continuable hardware watchpoints are treated as non-existent if the
8622 reason we stopped wasn't a hardware watchpoint (we didn't stop on
8623 some data address). Otherwise gdb won't stop on a break instruction
8624 in the code (not from a breakpoint) when a hardware watchpoint has
8625 been defined. Also skip watchpoints which we know did not trigger
8626 (did not match the data address). */
8627 if (is_hardware_watchpoint (b)
3a5c3e22 8628 && w->watchpoint_triggered == watch_triggered_no)
348d480f 8629 return 0;
9c06b0b4 8630
348d480f 8631 return 1;
9c06b0b4
TJB
8632}
8633
348d480f
PA
8634static void
8635check_status_watchpoint (bpstat bs)
9c06b0b4 8636{
348d480f 8637 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 8638
348d480f 8639 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
8640}
8641
8642/* Implement the "resources_needed" breakpoint_ops method for
348d480f 8643 hardware watchpoints. */
9c06b0b4
TJB
8644
8645static int
348d480f 8646resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 8647{
3a5c3e22
PA
8648 struct watchpoint *w = (struct watchpoint *) bl->owner;
8649 int length = w->exact? 1 : bl->length;
348d480f
PA
8650
8651 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
8652}
8653
8654/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 8655 hardware watchpoints. */
9c06b0b4
TJB
8656
8657static int
348d480f 8658works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 8659{
efa80663
PA
8660 /* Read and access watchpoints only work with hardware support. */
8661 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
8662}
8663
9c06b0b4 8664static enum print_stop_action
348d480f 8665print_it_watchpoint (bpstat bs)
9c06b0b4 8666{
348d480f
PA
8667 struct cleanup *old_chain;
8668 struct breakpoint *b;
8669 const struct bp_location *bl;
8670 struct ui_stream *stb;
8671 enum print_stop_action result;
3a5c3e22 8672 struct watchpoint *w;
79a45e25 8673 struct ui_out *uiout = current_uiout;
348d480f
PA
8674
8675 gdb_assert (bs->bp_location_at != NULL);
8676
8677 bl = bs->bp_location_at;
8678 b = bs->breakpoint_at;
3a5c3e22 8679 w = (struct watchpoint *) b;
348d480f
PA
8680
8681 stb = ui_out_stream_new (uiout);
8682 old_chain = make_cleanup_ui_out_stream_delete (stb);
9c06b0b4
TJB
8683
8684 switch (b->type)
8685 {
348d480f 8686 case bp_watchpoint:
9c06b0b4
TJB
8687 case bp_hardware_watchpoint:
8688 annotate_watchpoint (b->number);
8689 if (ui_out_is_mi_like_p (uiout))
8690 ui_out_field_string
8691 (uiout, "reason",
8692 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
8693 mention (b);
8694 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8695 ui_out_text (uiout, "\nOld value = ");
8696 watchpoint_value_print (bs->old_val, stb->stream);
8697 ui_out_field_stream (uiout, "old", stb);
8698 ui_out_text (uiout, "\nNew value = ");
3a5c3e22 8699 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8700 ui_out_field_stream (uiout, "new", stb);
8701 ui_out_text (uiout, "\n");
8702 /* More than one watchpoint may have been triggered. */
8703 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8704 break;
8705
8706 case bp_read_watchpoint:
8707 if (ui_out_is_mi_like_p (uiout))
8708 ui_out_field_string
8709 (uiout, "reason",
8710 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
8711 mention (b);
8712 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8713 ui_out_text (uiout, "\nValue = ");
3a5c3e22 8714 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8715 ui_out_field_stream (uiout, "value", stb);
8716 ui_out_text (uiout, "\n");
8717 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8718 break;
8719
8720 case bp_access_watchpoint:
348d480f
PA
8721 if (bs->old_val != NULL)
8722 {
8723 annotate_watchpoint (b->number);
8724 if (ui_out_is_mi_like_p (uiout))
8725 ui_out_field_string
8726 (uiout, "reason",
8727 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8728 mention (b);
8729 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8730 ui_out_text (uiout, "\nOld value = ");
8731 watchpoint_value_print (bs->old_val, stb->stream);
8732 ui_out_field_stream (uiout, "old", stb);
8733 ui_out_text (uiout, "\nNew value = ");
8734 }
8735 else
8736 {
8737 mention (b);
8738 if (ui_out_is_mi_like_p (uiout))
8739 ui_out_field_string
8740 (uiout, "reason",
8741 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8742 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8743 ui_out_text (uiout, "\nValue = ");
8744 }
3a5c3e22 8745 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8746 ui_out_field_stream (uiout, "new", stb);
8747 ui_out_text (uiout, "\n");
8748 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8749 break;
8750 default:
348d480f 8751 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8752 }
8753
348d480f
PA
8754 do_cleanups (old_chain);
8755 return result;
8756}
8757
8758/* Implement the "print_mention" breakpoint_ops method for hardware
8759 watchpoints. */
8760
8761static void
8762print_mention_watchpoint (struct breakpoint *b)
8763{
8764 struct cleanup *ui_out_chain;
3a5c3e22 8765 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 8766 struct ui_out *uiout = current_uiout;
348d480f
PA
8767
8768 switch (b->type)
8769 {
8770 case bp_watchpoint:
8771 ui_out_text (uiout, "Watchpoint ");
8772 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8773 break;
8774 case bp_hardware_watchpoint:
8775 ui_out_text (uiout, "Hardware watchpoint ");
8776 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8777 break;
8778 case bp_read_watchpoint:
8779 ui_out_text (uiout, "Hardware read watchpoint ");
8780 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8781 break;
8782 case bp_access_watchpoint:
8783 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
8784 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8785 break;
8786 default:
8787 internal_error (__FILE__, __LINE__,
8788 _("Invalid hardware watchpoint type."));
8789 }
8790
8791 ui_out_field_int (uiout, "number", b->number);
8792 ui_out_text (uiout, ": ");
3a5c3e22 8793 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
8794 do_cleanups (ui_out_chain);
8795}
8796
8797/* Implement the "print_recreate" breakpoint_ops method for
8798 watchpoints. */
8799
8800static void
8801print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
8802{
3a5c3e22
PA
8803 struct watchpoint *w = (struct watchpoint *) b;
8804
348d480f
PA
8805 switch (b->type)
8806 {
8807 case bp_watchpoint:
8808 case bp_hardware_watchpoint:
8809 fprintf_unfiltered (fp, "watch");
8810 break;
8811 case bp_read_watchpoint:
8812 fprintf_unfiltered (fp, "rwatch");
8813 break;
8814 case bp_access_watchpoint:
8815 fprintf_unfiltered (fp, "awatch");
8816 break;
8817 default:
8818 internal_error (__FILE__, __LINE__,
8819 _("Invalid watchpoint type."));
8820 }
8821
3a5c3e22 8822 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 8823 print_recreate_thread (b, fp);
348d480f
PA
8824}
8825
8826/* The breakpoint_ops structure to be used in hardware watchpoints. */
8827
2060206e 8828static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
8829
8830/* Implement the "insert" breakpoint_ops method for
8831 masked hardware watchpoints. */
8832
8833static int
8834insert_masked_watchpoint (struct bp_location *bl)
8835{
3a5c3e22
PA
8836 struct watchpoint *w = (struct watchpoint *) bl->owner;
8837
8838 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8839 bl->watchpoint_type);
8840}
8841
8842/* Implement the "remove" breakpoint_ops method for
8843 masked hardware watchpoints. */
8844
8845static int
8846remove_masked_watchpoint (struct bp_location *bl)
8847{
3a5c3e22
PA
8848 struct watchpoint *w = (struct watchpoint *) bl->owner;
8849
8850 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8851 bl->watchpoint_type);
8852}
8853
8854/* Implement the "resources_needed" breakpoint_ops method for
8855 masked hardware watchpoints. */
8856
8857static int
8858resources_needed_masked_watchpoint (const struct bp_location *bl)
8859{
3a5c3e22
PA
8860 struct watchpoint *w = (struct watchpoint *) bl->owner;
8861
8862 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
8863}
8864
8865/* Implement the "works_in_software_mode" breakpoint_ops method for
8866 masked hardware watchpoints. */
8867
8868static int
8869works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
8870{
8871 return 0;
8872}
8873
8874/* Implement the "print_it" breakpoint_ops method for
8875 masked hardware watchpoints. */
8876
8877static enum print_stop_action
8878print_it_masked_watchpoint (bpstat bs)
8879{
8880 struct breakpoint *b = bs->breakpoint_at;
79a45e25 8881 struct ui_out *uiout = current_uiout;
348d480f
PA
8882
8883 /* Masked watchpoints have only one location. */
8884 gdb_assert (b->loc && b->loc->next == NULL);
8885
8886 switch (b->type)
8887 {
8888 case bp_hardware_watchpoint:
8889 annotate_watchpoint (b->number);
8890 if (ui_out_is_mi_like_p (uiout))
8891 ui_out_field_string
8892 (uiout, "reason",
8893 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
8894 break;
8895
8896 case bp_read_watchpoint:
8897 if (ui_out_is_mi_like_p (uiout))
8898 ui_out_field_string
8899 (uiout, "reason",
8900 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
8901 break;
8902
8903 case bp_access_watchpoint:
8904 if (ui_out_is_mi_like_p (uiout))
8905 ui_out_field_string
8906 (uiout, "reason",
8907 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8908 break;
8909 default:
8910 internal_error (__FILE__, __LINE__,
8911 _("Invalid hardware watchpoint type."));
8912 }
8913
8914 mention (b);
9c06b0b4
TJB
8915 ui_out_text (uiout, _("\n\
8916Check the underlying instruction at PC for the memory\n\
8917address and value which triggered this watchpoint.\n"));
8918 ui_out_text (uiout, "\n");
8919
8920 /* More than one watchpoint may have been triggered. */
8921 return PRINT_UNKNOWN;
8922}
8923
8924/* Implement the "print_one_detail" breakpoint_ops method for
8925 masked hardware watchpoints. */
8926
8927static void
8928print_one_detail_masked_watchpoint (const struct breakpoint *b,
8929 struct ui_out *uiout)
8930{
3a5c3e22
PA
8931 struct watchpoint *w = (struct watchpoint *) b;
8932
9c06b0b4
TJB
8933 /* Masked watchpoints have only one location. */
8934 gdb_assert (b->loc && b->loc->next == NULL);
8935
8936 ui_out_text (uiout, "\tmask ");
3a5c3e22 8937 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
8938 ui_out_text (uiout, "\n");
8939}
8940
8941/* Implement the "print_mention" breakpoint_ops method for
8942 masked hardware watchpoints. */
8943
8944static void
8945print_mention_masked_watchpoint (struct breakpoint *b)
8946{
3a5c3e22 8947 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 8948 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
8949 struct cleanup *ui_out_chain;
8950
8951 switch (b->type)
8952 {
8953 case bp_hardware_watchpoint:
8954 ui_out_text (uiout, "Masked hardware watchpoint ");
8955 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8956 break;
8957 case bp_read_watchpoint:
8958 ui_out_text (uiout, "Masked hardware read watchpoint ");
8959 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8960 break;
8961 case bp_access_watchpoint:
8962 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
8963 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8964 break;
8965 default:
8966 internal_error (__FILE__, __LINE__,
8967 _("Invalid hardware watchpoint type."));
8968 }
8969
8970 ui_out_field_int (uiout, "number", b->number);
8971 ui_out_text (uiout, ": ");
3a5c3e22 8972 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
8973 do_cleanups (ui_out_chain);
8974}
8975
8976/* Implement the "print_recreate" breakpoint_ops method for
8977 masked hardware watchpoints. */
8978
8979static void
8980print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
8981{
3a5c3e22 8982 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
8983 char tmp[40];
8984
8985 switch (b->type)
8986 {
8987 case bp_hardware_watchpoint:
8988 fprintf_unfiltered (fp, "watch");
8989 break;
8990 case bp_read_watchpoint:
8991 fprintf_unfiltered (fp, "rwatch");
8992 break;
8993 case bp_access_watchpoint:
8994 fprintf_unfiltered (fp, "awatch");
8995 break;
8996 default:
8997 internal_error (__FILE__, __LINE__,
8998 _("Invalid hardware watchpoint type."));
8999 }
9000
3a5c3e22
PA
9001 sprintf_vma (tmp, w->hw_wp_mask);
9002 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 9003 print_recreate_thread (b, fp);
9c06b0b4
TJB
9004}
9005
9006/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
9007
2060206e 9008static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
9009
9010/* Tell whether the given watchpoint is a masked hardware watchpoint. */
9011
9012static int
9013is_masked_watchpoint (const struct breakpoint *b)
9014{
9015 return b->ops == &masked_watchpoint_breakpoint_ops;
9016}
9017
53a5351d
JM
9018/* accessflag: hw_write: watch write,
9019 hw_read: watch read,
9020 hw_access: watch access (read or write) */
c906108c 9021static void
84f4c1fe
PM
9022watch_command_1 (char *arg, int accessflag, int from_tty,
9023 int just_location, int internal)
c906108c 9024{
a9634178 9025 volatile struct gdb_exception e;
d983da9c 9026 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 9027 struct expression *exp;
60e1c644 9028 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 9029 struct value *val, *mark, *result;
c906108c 9030 struct frame_info *frame;
c906108c
SS
9031 char *exp_start = NULL;
9032 char *exp_end = NULL;
9c06b0b4
TJB
9033 char *tok, *end_tok;
9034 int toklen = -1;
c906108c
SS
9035 char *cond_start = NULL;
9036 char *cond_end = NULL;
c906108c 9037 enum bptype bp_type;
37e4754d 9038 int thread = -1;
0cf6dd15 9039 int pc = 0;
9c06b0b4
TJB
9040 /* Flag to indicate whether we are going to use masks for
9041 the hardware watchpoint. */
9042 int use_mask = 0;
9043 CORE_ADDR mask = 0;
3a5c3e22 9044 struct watchpoint *w;
c906108c 9045
37e4754d
LM
9046 /* Make sure that we actually have parameters to parse. */
9047 if (arg != NULL && arg[0] != '\0')
9048 {
9c06b0b4 9049 char *value_start;
37e4754d 9050
9c06b0b4
TJB
9051 /* Look for "parameter value" pairs at the end
9052 of the arguments string. */
9053 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
9054 {
9055 /* Skip whitespace at the end of the argument list. */
9056 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9057 tok--;
9058
9059 /* Find the beginning of the last token.
9060 This is the value of the parameter. */
9061 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9062 tok--;
9063 value_start = tok + 1;
9064
9065 /* Skip whitespace. */
9066 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9067 tok--;
9068
9069 end_tok = tok;
9070
9071 /* Find the beginning of the second to last token.
9072 This is the parameter itself. */
9073 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9074 tok--;
9075 tok++;
9076 toklen = end_tok - tok + 1;
9077
9078 if (toklen == 6 && !strncmp (tok, "thread", 6))
9079 {
9080 /* At this point we've found a "thread" token, which means
9081 the user is trying to set a watchpoint that triggers
9082 only in a specific thread. */
9083 char *endp;
37e4754d 9084
9c06b0b4
TJB
9085 if (thread != -1)
9086 error(_("You can specify only one thread."));
37e4754d 9087
9c06b0b4
TJB
9088 /* Extract the thread ID from the next token. */
9089 thread = strtol (value_start, &endp, 0);
37e4754d 9090
9c06b0b4
TJB
9091 /* Check if the user provided a valid numeric value for the
9092 thread ID. */
9093 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9094 error (_("Invalid thread ID specification %s."), value_start);
9095
9096 /* Check if the thread actually exists. */
9097 if (!valid_thread_id (thread))
9098 error (_("Unknown thread %d."), thread);
9099 }
9100 else if (toklen == 4 && !strncmp (tok, "mask", 4))
9101 {
9102 /* We've found a "mask" token, which means the user wants to
9103 create a hardware watchpoint that is going to have the mask
9104 facility. */
9105 struct value *mask_value, *mark;
37e4754d 9106
9c06b0b4
TJB
9107 if (use_mask)
9108 error(_("You can specify only one mask."));
37e4754d 9109
9c06b0b4 9110 use_mask = just_location = 1;
37e4754d 9111
9c06b0b4
TJB
9112 mark = value_mark ();
9113 mask_value = parse_to_comma_and_eval (&value_start);
9114 mask = value_as_address (mask_value);
9115 value_free_to_mark (mark);
9116 }
9117 else
9118 /* We didn't recognize what we found. We should stop here. */
9119 break;
37e4754d 9120
9c06b0b4
TJB
9121 /* Truncate the string and get rid of the "parameter value" pair before
9122 the arguments string is parsed by the parse_exp_1 function. */
9123 *tok = '\0';
9124 }
37e4754d
LM
9125 }
9126
9127 /* Parse the rest of the arguments. */
c906108c
SS
9128 innermost_block = NULL;
9129 exp_start = arg;
9130 exp = parse_exp_1 (&arg, 0, 0);
9131 exp_end = arg;
fa8a61dc
TT
9132 /* Remove trailing whitespace from the expression before saving it.
9133 This makes the eventual display of the expression string a bit
9134 prettier. */
9135 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9136 --exp_end;
9137
65d79d4b
SDJ
9138 /* Checking if the expression is not constant. */
9139 if (watchpoint_exp_is_const (exp))
9140 {
9141 int len;
9142
9143 len = exp_end - exp_start;
9144 while (len > 0 && isspace (exp_start[len - 1]))
9145 len--;
9146 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9147 }
9148
c906108c
SS
9149 exp_valid_block = innermost_block;
9150 mark = value_mark ();
a1442452 9151 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
9152
9153 if (just_location)
9154 {
9c06b0b4
TJB
9155 int ret;
9156
06a64a0b 9157 exp_valid_block = NULL;
a1442452 9158 val = value_addr (result);
06a64a0b
TT
9159 release_value (val);
9160 value_free_to_mark (mark);
9c06b0b4
TJB
9161
9162 if (use_mask)
9163 {
9164 ret = target_masked_watch_num_registers (value_as_address (val),
9165 mask);
9166 if (ret == -1)
9167 error (_("This target does not support masked watchpoints."));
9168 else if (ret == -2)
9169 error (_("Invalid mask or memory region."));
9170 }
06a64a0b
TT
9171 }
9172 else if (val != NULL)
fa4727a6 9173 release_value (val);
c906108c 9174
e9cafbcc
TT
9175 tok = skip_spaces (arg);
9176 end_tok = skip_to_space (tok);
c906108c
SS
9177
9178 toklen = end_tok - tok;
9179 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9180 {
2d134ed3
PA
9181 struct expression *cond;
9182
60e1c644 9183 innermost_block = NULL;
c906108c
SS
9184 tok = cond_start = end_tok + 1;
9185 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
9186
9187 /* The watchpoint expression may not be local, but the condition
9188 may still be. E.g.: `watch global if local > 0'. */
9189 cond_exp_valid_block = innermost_block;
9190
2d134ed3 9191 xfree (cond);
c906108c
SS
9192 cond_end = tok;
9193 }
9194 if (*tok)
8a3fe4f8 9195 error (_("Junk at end of command."));
c906108c 9196
53a5351d 9197 if (accessflag == hw_read)
c5aa993b 9198 bp_type = bp_read_watchpoint;
53a5351d 9199 else if (accessflag == hw_access)
c5aa993b
JM
9200 bp_type = bp_access_watchpoint;
9201 else
9202 bp_type = bp_hardware_watchpoint;
c906108c 9203
d983da9c 9204 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
9205
9206 /* If the expression is "local", then set up a "watchpoint scope"
9207 breakpoint at the point where we've left the scope of the watchpoint
9208 expression. Create the scope breakpoint before the watchpoint, so
9209 that we will encounter it first in bpstat_stop_status. */
60e1c644 9210 if (exp_valid_block && frame)
d983da9c 9211 {
edb3359d
DJ
9212 if (frame_id_p (frame_unwind_caller_id (frame)))
9213 {
9214 scope_breakpoint
a6d9a66e
UW
9215 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
9216 frame_unwind_caller_pc (frame),
06edf0c0
PA
9217 bp_watchpoint_scope,
9218 &momentary_breakpoint_ops);
d983da9c 9219
edb3359d 9220 scope_breakpoint->enable_state = bp_enabled;
d983da9c 9221
edb3359d
DJ
9222 /* Automatically delete the breakpoint when it hits. */
9223 scope_breakpoint->disposition = disp_del;
d983da9c 9224
edb3359d
DJ
9225 /* Only break in the proper frame (help with recursion). */
9226 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 9227
edb3359d 9228 /* Set the address at which we will stop. */
a6d9a66e
UW
9229 scope_breakpoint->loc->gdbarch
9230 = frame_unwind_caller_arch (frame);
edb3359d
DJ
9231 scope_breakpoint->loc->requested_address
9232 = frame_unwind_caller_pc (frame);
9233 scope_breakpoint->loc->address
a6d9a66e
UW
9234 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9235 scope_breakpoint->loc->requested_address,
edb3359d
DJ
9236 scope_breakpoint->type);
9237 }
d983da9c
DJ
9238 }
9239
c906108c 9240 /* Now set up the breakpoint. */
3a5c3e22
PA
9241
9242 w = XCNEW (struct watchpoint);
9243 b = &w->base;
348d480f 9244 if (use_mask)
3a5c3e22
PA
9245 init_raw_breakpoint_without_location (b, NULL, bp_type,
9246 &masked_watchpoint_breakpoint_ops);
348d480f 9247 else
3a5c3e22
PA
9248 init_raw_breakpoint_without_location (b, NULL, bp_type,
9249 &watchpoint_breakpoint_ops);
37e4754d 9250 b->thread = thread;
b5de0fa7 9251 b->disposition = disp_donttouch;
348d480f 9252 b->pspace = current_program_space;
3a5c3e22
PA
9253 w->exp = exp;
9254 w->exp_valid_block = exp_valid_block;
9255 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
9256 if (just_location)
9257 {
9258 struct type *t = value_type (val);
9259 CORE_ADDR addr = value_as_address (val);
9260 char *name;
9261
9262 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9263 name = type_to_string (t);
9264
3a5c3e22 9265 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 9266 core_addr_to_string (addr));
06a64a0b
TT
9267 xfree (name);
9268
3a5c3e22 9269 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
9270 (int) (exp_end - exp_start), exp_start);
9271
06a64a0b
TT
9272 /* The above expression is in C. */
9273 b->language = language_c;
9274 }
9275 else
3a5c3e22 9276 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
9277
9278 if (use_mask)
9279 {
3a5c3e22 9280 w->hw_wp_mask = mask;
9c06b0b4
TJB
9281 }
9282 else
9283 {
3a5c3e22
PA
9284 w->val = val;
9285 w->val_valid = 1;
9c06b0b4 9286 }
77b06cd7 9287
c906108c
SS
9288 if (cond_start)
9289 b->cond_string = savestring (cond_start, cond_end - cond_start);
9290 else
9291 b->cond_string = 0;
c5aa993b 9292
c906108c 9293 if (frame)
f6bc2008 9294 {
3a5c3e22
PA
9295 w->watchpoint_frame = get_frame_id (frame);
9296 w->watchpoint_thread = inferior_ptid;
f6bc2008 9297 }
c906108c 9298 else
f6bc2008 9299 {
3a5c3e22
PA
9300 w->watchpoint_frame = null_frame_id;
9301 w->watchpoint_thread = null_ptid;
f6bc2008 9302 }
c906108c 9303
d983da9c 9304 if (scope_breakpoint != NULL)
c906108c 9305 {
d983da9c
DJ
9306 /* The scope breakpoint is related to the watchpoint. We will
9307 need to act on them together. */
9308 b->related_breakpoint = scope_breakpoint;
9309 scope_breakpoint->related_breakpoint = b;
c906108c 9310 }
d983da9c 9311
06a64a0b
TT
9312 if (!just_location)
9313 value_free_to_mark (mark);
2d134ed3 9314
a9634178
TJB
9315 TRY_CATCH (e, RETURN_MASK_ALL)
9316 {
9317 /* Finally update the new watchpoint. This creates the locations
9318 that should be inserted. */
3a5c3e22 9319 update_watchpoint (w, 1);
a9634178
TJB
9320 }
9321 if (e.reason < 0)
9322 {
9323 delete_breakpoint (b);
9324 throw_exception (e);
9325 }
9326
3ea46bff 9327 install_breakpoint (internal, b, 1);
c906108c
SS
9328}
9329
e09342b5 9330/* Return count of debug registers needed to watch the given expression.
e09342b5 9331 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9332
c906108c 9333static int
a9634178 9334can_use_hardware_watchpoint (struct value *v)
c906108c
SS
9335{
9336 int found_memory_cnt = 0;
2e70b7b9 9337 struct value *head = v;
c906108c
SS
9338
9339 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9340 if (!can_use_hw_watchpoints)
c906108c 9341 return 0;
c5aa993b 9342
5c44784c
JM
9343 /* Make sure that the value of the expression depends only upon
9344 memory contents, and values computed from them within GDB. If we
9345 find any register references or function calls, we can't use a
9346 hardware watchpoint.
9347
9348 The idea here is that evaluating an expression generates a series
9349 of values, one holding the value of every subexpression. (The
9350 expression a*b+c has five subexpressions: a, b, a*b, c, and
9351 a*b+c.) GDB's values hold almost enough information to establish
9352 the criteria given above --- they identify memory lvalues,
9353 register lvalues, computed values, etcetera. So we can evaluate
9354 the expression, and then scan the chain of values that leaves
9355 behind to decide whether we can detect any possible change to the
9356 expression's final value using only hardware watchpoints.
9357
9358 However, I don't think that the values returned by inferior
9359 function calls are special in any way. So this function may not
9360 notice that an expression involving an inferior function call
9361 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9362 for (; v; v = value_next (v))
c906108c 9363 {
5c44784c 9364 if (VALUE_LVAL (v) == lval_memory)
c906108c 9365 {
8464be76
DJ
9366 if (v != head && value_lazy (v))
9367 /* A lazy memory lvalue in the chain is one that GDB never
9368 needed to fetch; we either just used its address (e.g.,
9369 `a' in `a.b') or we never needed it at all (e.g., `a'
9370 in `a,b'). This doesn't apply to HEAD; if that is
9371 lazy then it was not readable, but watch it anyway. */
5c44784c 9372 ;
53a5351d 9373 else
5c44784c
JM
9374 {
9375 /* Ahh, memory we actually used! Check if we can cover
9376 it with hardware watchpoints. */
df407dfe 9377 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9378
9379 /* We only watch structs and arrays if user asked for it
9380 explicitly, never if they just happen to appear in a
9381 middle of some value chain. */
9382 if (v == head
9383 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9384 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9385 {
42ae5230 9386 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9387 int len;
9388 int num_regs;
9389
a9634178 9390 len = (target_exact_watchpoints
e09342b5
TJB
9391 && is_scalar_type_recursive (vtype))?
9392 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9393
e09342b5
TJB
9394 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9395 if (!num_regs)
2e70b7b9
MS
9396 return 0;
9397 else
e09342b5 9398 found_memory_cnt += num_regs;
2e70b7b9 9399 }
5c44784c 9400 }
c5aa993b 9401 }
5086187c
AC
9402 else if (VALUE_LVAL (v) != not_lval
9403 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9404 return 0; /* These are values from the history (e.g., $1). */
5086187c 9405 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9406 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9407 }
9408
9409 /* The expression itself looks suitable for using a hardware
9410 watchpoint, but give the target machine a chance to reject it. */
9411 return found_memory_cnt;
9412}
9413
8b93c638 9414void
84f4c1fe 9415watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9416{
84f4c1fe 9417 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9418}
9419
9420/* A helper function that looks for an argument at the start of a
9421 string. The argument must also either be at the end of the string,
9422 or be followed by whitespace. Returns 1 if it finds the argument,
9423 0 otherwise. If the argument is found, it updates *STR. */
9424
9425static int
9426check_for_argument (char **str, char *arg, int arg_len)
9427{
9428 if (strncmp (*str, arg, arg_len) == 0
9429 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9430 {
9431 *str += arg_len;
9432 return 1;
9433 }
9434 return 0;
9435}
9436
9437/* A helper function that looks for the "-location" argument and then
9438 calls watch_command_1. */
9439
9440static void
9441watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9442{
9443 int just_location = 0;
9444
9445 if (arg
9446 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9447 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9448 {
e9cafbcc 9449 arg = skip_spaces (arg);
06a64a0b
TT
9450 just_location = 1;
9451 }
9452
84f4c1fe 9453 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9454}
8926118c 9455
c5aa993b 9456static void
fba45db2 9457watch_command (char *arg, int from_tty)
c906108c 9458{
06a64a0b 9459 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9460}
9461
8b93c638 9462void
84f4c1fe 9463rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9464{
84f4c1fe 9465 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9466}
8926118c 9467
c5aa993b 9468static void
fba45db2 9469rwatch_command (char *arg, int from_tty)
c906108c 9470{
06a64a0b 9471 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9472}
9473
8b93c638 9474void
84f4c1fe 9475awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9476{
84f4c1fe 9477 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9478}
8926118c 9479
c5aa993b 9480static void
fba45db2 9481awatch_command (char *arg, int from_tty)
c906108c 9482{
06a64a0b 9483 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9484}
c906108c 9485\f
c5aa993b 9486
43ff13b4 9487/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9488 because it uses the mechanisms of breakpoints. */
9489
bfec99b2
PA
9490struct until_break_command_continuation_args
9491{
9492 struct breakpoint *breakpoint;
9493 struct breakpoint *breakpoint2;
186c406b 9494 int thread_num;
bfec99b2
PA
9495};
9496
43ff13b4 9497/* This function is called by fetch_inferior_event via the
4a64f543 9498 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9499 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9500 command. */
c2c6d25f 9501static void
fa4cd53f 9502until_break_command_continuation (void *arg, int err)
43ff13b4 9503{
bfec99b2
PA
9504 struct until_break_command_continuation_args *a = arg;
9505
9506 delete_breakpoint (a->breakpoint);
9507 if (a->breakpoint2)
9508 delete_breakpoint (a->breakpoint2);
186c406b 9509 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9510}
9511
c906108c 9512void
ae66c1fc 9513until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9514{
9515 struct symtabs_and_lines sals;
9516 struct symtab_and_line sal;
206415a3 9517 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9518 struct breakpoint *breakpoint;
f107f563 9519 struct breakpoint *breakpoint2 = NULL;
c906108c 9520 struct cleanup *old_chain;
186c406b
TT
9521 int thread;
9522 struct thread_info *tp;
c906108c
SS
9523
9524 clear_proceed_status ();
9525
9526 /* Set a breakpoint where the user wants it and at return from
4a64f543 9527 this function. */
c5aa993b 9528
1bfeeb0f 9529 if (last_displayed_sal_is_valid ())
f8eba3c6 9530 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 9531 get_last_displayed_symtab (),
f8eba3c6 9532 get_last_displayed_line ());
c906108c 9533 else
f8eba3c6
TT
9534 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
9535 (struct symtab *) NULL, 0);
c5aa993b 9536
c906108c 9537 if (sals.nelts != 1)
8a3fe4f8 9538 error (_("Couldn't get information on specified line."));
c5aa993b 9539
c906108c 9540 sal = sals.sals[0];
4a64f543 9541 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9542
c906108c 9543 if (*arg)
8a3fe4f8 9544 error (_("Junk at end of arguments."));
c5aa993b 9545
c906108c 9546 resolve_sal_pc (&sal);
c5aa993b 9547
ae66c1fc
EZ
9548 if (anywhere)
9549 /* If the user told us to continue until a specified location,
9550 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9551 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9552 null_frame_id, bp_until);
ae66c1fc 9553 else
4a64f543
MS
9554 /* Otherwise, specify the selected frame, because we want to stop
9555 only at the very same frame. */
a6d9a66e
UW
9556 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9557 get_stack_frame_id (frame),
ae66c1fc 9558 bp_until);
c5aa993b 9559
f107f563 9560 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9561
186c406b
TT
9562 tp = inferior_thread ();
9563 thread = tp->num;
9564
ae66c1fc
EZ
9565 /* Keep within the current frame, or in frames called by the current
9566 one. */
edb3359d
DJ
9567
9568 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9569 {
edb3359d
DJ
9570 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9571 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9572 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9573 sal,
edb3359d 9574 frame_unwind_caller_id (frame),
f107f563
VP
9575 bp_until);
9576 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9577
9578 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9579 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9580 }
c5aa993b 9581
c906108c 9582 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9583
4a64f543
MS
9584 /* If we are running asynchronously, and proceed call above has
9585 actually managed to start the target, arrange for breakpoints to
9586 be deleted when the target stops. Otherwise, we're already
9587 stopped and delete breakpoints via cleanup chain. */
f107f563 9588
8ea051c5 9589 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9590 {
bfec99b2
PA
9591 struct until_break_command_continuation_args *args;
9592 args = xmalloc (sizeof (*args));
f107f563 9593
bfec99b2
PA
9594 args->breakpoint = breakpoint;
9595 args->breakpoint2 = breakpoint2;
186c406b 9596 args->thread_num = thread;
f107f563
VP
9597
9598 discard_cleanups (old_chain);
95e54da7
PA
9599 add_continuation (inferior_thread (),
9600 until_break_command_continuation, args,
604ead4a 9601 xfree);
f107f563
VP
9602 }
9603 else
c5aa993b 9604 do_cleanups (old_chain);
c906108c 9605}
ae66c1fc 9606
c906108c
SS
9607/* This function attempts to parse an optional "if <cond>" clause
9608 from the arg string. If one is not found, it returns NULL.
c5aa993b 9609
c906108c
SS
9610 Else, it returns a pointer to the condition string. (It does not
9611 attempt to evaluate the string against a particular block.) And,
9612 it updates arg to point to the first character following the parsed
4a64f543 9613 if clause in the arg string. */
53a5351d 9614
c906108c 9615static char *
fba45db2 9616ep_parse_optional_if_clause (char **arg)
c906108c 9617{
c5aa993b
JM
9618 char *cond_string;
9619
9620 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9621 return NULL;
c5aa993b 9622
4a64f543 9623 /* Skip the "if" keyword. */
c906108c 9624 (*arg) += 2;
c5aa993b 9625
c906108c 9626 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9627 condition string. */
e9cafbcc 9628 *arg = skip_spaces (*arg);
c906108c 9629 cond_string = *arg;
c5aa993b 9630
4a64f543
MS
9631 /* Assume that the condition occupies the remainder of the arg
9632 string. */
c906108c 9633 (*arg) += strlen (cond_string);
c5aa993b 9634
c906108c
SS
9635 return cond_string;
9636}
c5aa993b 9637
c906108c
SS
9638/* Commands to deal with catching events, such as signals, exceptions,
9639 process start/exit, etc. */
c5aa993b
JM
9640
9641typedef enum
9642{
44feb3ce
TT
9643 catch_fork_temporary, catch_vfork_temporary,
9644 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9645}
9646catch_fork_kind;
9647
c906108c 9648static void
cc59ec59
MS
9649catch_fork_command_1 (char *arg, int from_tty,
9650 struct cmd_list_element *command)
c906108c 9651{
a6d9a66e 9652 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9653 char *cond_string = NULL;
44feb3ce
TT
9654 catch_fork_kind fork_kind;
9655 int tempflag;
9656
9657 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9658 tempflag = (fork_kind == catch_fork_temporary
9659 || fork_kind == catch_vfork_temporary);
c5aa993b 9660
44feb3ce
TT
9661 if (!arg)
9662 arg = "";
e9cafbcc 9663 arg = skip_spaces (arg);
c5aa993b 9664
c906108c 9665 /* The allowed syntax is:
c5aa993b
JM
9666 catch [v]fork
9667 catch [v]fork if <cond>
9668
4a64f543 9669 First, check if there's an if clause. */
c906108c 9670 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9671
c906108c 9672 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9673 error (_("Junk at end of arguments."));
c5aa993b 9674
c906108c 9675 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9676 and enable reporting of such events. */
c5aa993b
JM
9677 switch (fork_kind)
9678 {
44feb3ce
TT
9679 case catch_fork_temporary:
9680 case catch_fork_permanent:
a6d9a66e 9681 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9682 &catch_fork_breakpoint_ops);
c906108c 9683 break;
44feb3ce
TT
9684 case catch_vfork_temporary:
9685 case catch_vfork_permanent:
a6d9a66e 9686 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9687 &catch_vfork_breakpoint_ops);
c906108c 9688 break;
c5aa993b 9689 default:
8a3fe4f8 9690 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9691 break;
c5aa993b 9692 }
c906108c
SS
9693}
9694
9695static void
cc59ec59
MS
9696catch_exec_command_1 (char *arg, int from_tty,
9697 struct cmd_list_element *command)
c906108c 9698{
b4d90040 9699 struct exec_catchpoint *c;
a6d9a66e 9700 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9701 int tempflag;
c5aa993b 9702 char *cond_string = NULL;
c906108c 9703
44feb3ce
TT
9704 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9705
9706 if (!arg)
9707 arg = "";
e9cafbcc 9708 arg = skip_spaces (arg);
c906108c
SS
9709
9710 /* The allowed syntax is:
c5aa993b
JM
9711 catch exec
9712 catch exec if <cond>
c906108c 9713
4a64f543 9714 First, check if there's an if clause. */
c906108c
SS
9715 cond_string = ep_parse_optional_if_clause (&arg);
9716
9717 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9718 error (_("Junk at end of arguments."));
c906108c 9719
b4d90040
PA
9720 c = XNEW (struct exec_catchpoint);
9721 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
9722 &catch_exec_breakpoint_ops);
9723 c->exec_pathname = NULL;
9724
3ea46bff 9725 install_breakpoint (0, &c->base, 1);
c906108c 9726}
c5aa993b 9727
3086aeae 9728static enum print_stop_action
348d480f 9729print_it_exception_catchpoint (bpstat bs)
3086aeae 9730{
79a45e25 9731 struct ui_out *uiout = current_uiout;
348d480f 9732 struct breakpoint *b = bs->breakpoint_at;
ade92717 9733 int bp_temp, bp_throw;
3086aeae 9734
ade92717 9735 annotate_catchpoint (b->number);
3086aeae 9736
ade92717
AR
9737 bp_throw = strstr (b->addr_string, "throw") != NULL;
9738 if (b->loc->address != b->loc->requested_address)
9739 breakpoint_adjustment_warning (b->loc->requested_address,
9740 b->loc->address,
9741 b->number, 1);
df2b6d2d 9742 bp_temp = b->disposition == disp_del;
ade92717
AR
9743 ui_out_text (uiout,
9744 bp_temp ? "Temporary catchpoint "
9745 : "Catchpoint ");
9746 if (!ui_out_is_mi_like_p (uiout))
9747 ui_out_field_int (uiout, "bkptno", b->number);
9748 ui_out_text (uiout,
c0b37c48
AR
9749 bp_throw ? " (exception thrown), "
9750 : " (exception caught), ");
ade92717
AR
9751 if (ui_out_is_mi_like_p (uiout))
9752 {
9753 ui_out_field_string (uiout, "reason",
9754 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9755 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9756 ui_out_field_int (uiout, "bkptno", b->number);
9757 }
3086aeae
DJ
9758 return PRINT_SRC_AND_LOC;
9759}
9760
9761static void
cc59ec59
MS
9762print_one_exception_catchpoint (struct breakpoint *b,
9763 struct bp_location **last_loc)
3086aeae 9764{
79a45b7d 9765 struct value_print_options opts;
79a45e25 9766 struct ui_out *uiout = current_uiout;
cc59ec59 9767
79a45b7d
TT
9768 get_user_print_options (&opts);
9769 if (opts.addressprint)
3086aeae
DJ
9770 {
9771 annotate_field (4);
604133b5
AR
9772 if (b->loc == NULL || b->loc->shlib_disabled)
9773 ui_out_field_string (uiout, "addr", "<PENDING>");
9774 else
5af949e3
UW
9775 ui_out_field_core_addr (uiout, "addr",
9776 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9777 }
9778 annotate_field (5);
604133b5 9779 if (b->loc)
a6d9a66e 9780 *last_loc = b->loc;
3086aeae
DJ
9781 if (strstr (b->addr_string, "throw") != NULL)
9782 ui_out_field_string (uiout, "what", "exception throw");
9783 else
9784 ui_out_field_string (uiout, "what", "exception catch");
9785}
9786
9787static void
9788print_mention_exception_catchpoint (struct breakpoint *b)
9789{
79a45e25 9790 struct ui_out *uiout = current_uiout;
ade92717
AR
9791 int bp_temp;
9792 int bp_throw;
9793
df2b6d2d 9794 bp_temp = b->disposition == disp_del;
ade92717
AR
9795 bp_throw = strstr (b->addr_string, "throw") != NULL;
9796 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9797 : _("Catchpoint "));
9798 ui_out_field_int (uiout, "bkptno", b->number);
9799 ui_out_text (uiout, bp_throw ? _(" (throw)")
9800 : _(" (catch)"));
3086aeae
DJ
9801}
9802
6149aea9
PA
9803/* Implement the "print_recreate" breakpoint_ops method for throw and
9804 catch catchpoints. */
9805
9806static void
4a64f543
MS
9807print_recreate_exception_catchpoint (struct breakpoint *b,
9808 struct ui_file *fp)
6149aea9
PA
9809{
9810 int bp_temp;
9811 int bp_throw;
9812
9813 bp_temp = b->disposition == disp_del;
9814 bp_throw = strstr (b->addr_string, "throw") != NULL;
9815 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9816 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 9817 print_recreate_thread (b, fp);
6149aea9
PA
9818}
9819
2060206e 9820static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
9821
9822static int
9823handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9824 enum exception_event_kind ex_event, int from_tty)
9825{
604133b5
AR
9826 char *trigger_func_name;
9827
3086aeae 9828 if (ex_event == EX_EVENT_CATCH)
604133b5 9829 trigger_func_name = "__cxa_begin_catch";
3086aeae 9830 else
604133b5 9831 trigger_func_name = "__cxa_throw";
3086aeae 9832
8cdf0e15
VP
9833 create_breakpoint (get_current_arch (),
9834 trigger_func_name, cond_string, -1,
9835 0 /* condition and thread are valid. */,
0fb4aa4b 9836 tempflag, bp_breakpoint,
8cdf0e15
VP
9837 0,
9838 AUTO_BOOLEAN_TRUE /* pending */,
9839 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9840 1 /* enabled */,
9841 0 /* internal */);
3086aeae 9842
3086aeae
DJ
9843 return 1;
9844}
9845
4a64f543 9846/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9847
9848static void
fba45db2
KB
9849catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9850 int tempflag, int from_tty)
c906108c 9851{
c5aa993b 9852 char *cond_string = NULL;
c5aa993b 9853
44feb3ce
TT
9854 if (!arg)
9855 arg = "";
e9cafbcc 9856 arg = skip_spaces (arg);
c5aa993b 9857
c906108c
SS
9858 cond_string = ep_parse_optional_if_clause (&arg);
9859
9860 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9861 error (_("Junk at end of arguments."));
c906108c 9862
059fb39f
PM
9863 if (ex_event != EX_EVENT_THROW
9864 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9865 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9866
3086aeae
DJ
9867 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9868 return;
9869
8a3fe4f8 9870 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9871}
9872
44feb3ce
TT
9873/* Implementation of "catch catch" command. */
9874
9875static void
9876catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9877{
9878 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9879
44feb3ce
TT
9880 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9881}
9882
9883/* Implementation of "catch throw" command. */
9884
9885static void
9886catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9887{
9888 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9889
44feb3ce
TT
9890 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9891}
9892
9ac4176b 9893void
28010a5d
PA
9894init_ada_exception_breakpoint (struct breakpoint *b,
9895 struct gdbarch *gdbarch,
9896 struct symtab_and_line sal,
9897 char *addr_string,
c0a91b2b 9898 const struct breakpoint_ops *ops,
28010a5d
PA
9899 int tempflag,
9900 int from_tty)
f7f9143b 9901{
f7f9143b
JB
9902 if (from_tty)
9903 {
5af949e3
UW
9904 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9905 if (!loc_gdbarch)
9906 loc_gdbarch = gdbarch;
9907
6c95b8df
PA
9908 describe_other_breakpoints (loc_gdbarch,
9909 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9910 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9911 version for exception catchpoints, because two catchpoints
9912 used for different exception names will use the same address.
9913 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9914 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 9915 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
9916 the user what type of catchpoint it is. The above is good
9917 enough for now, though. */
9918 }
9919
28010a5d 9920 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
9921
9922 b->enable_state = bp_enabled;
9923 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
9924 b->addr_string = addr_string;
9925 b->language = language_ada;
f7f9143b
JB
9926}
9927
a96d9b2e
SDJ
9928/* Splits the argument using space as delimiter. Returns an xmalloc'd
9929 filter list, or NULL if no filtering is required. */
9930static VEC(int) *
9931catch_syscall_split_args (char *arg)
9932{
9933 VEC(int) *result = NULL;
29d0bb3d 9934 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
9935
9936 while (*arg != '\0')
9937 {
9938 int i, syscall_number;
9939 char *endptr;
9940 char cur_name[128];
9941 struct syscall s;
9942
9943 /* Skip whitespace. */
9944 while (isspace (*arg))
9945 arg++;
9946
9947 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9948 cur_name[i] = arg[i];
9949 cur_name[i] = '\0';
9950 arg += i;
9951
9952 /* Check if the user provided a syscall name or a number. */
9953 syscall_number = (int) strtol (cur_name, &endptr, 0);
9954 if (*endptr == '\0')
bccd0dd2 9955 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9956 else
9957 {
9958 /* We have a name. Let's check if it's valid and convert it
9959 to a number. */
9960 get_syscall_by_name (cur_name, &s);
9961
9962 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
9963 /* Here we have to issue an error instead of a warning,
9964 because GDB cannot do anything useful if there's no
9965 syscall number to be caught. */
a96d9b2e
SDJ
9966 error (_("Unknown syscall name '%s'."), cur_name);
9967 }
9968
9969 /* Ok, it's valid. */
9970 VEC_safe_push (int, result, s.number);
9971 }
9972
9973 discard_cleanups (cleanup);
9974 return result;
9975}
9976
9977/* Implement the "catch syscall" command. */
9978
9979static void
cc59ec59
MS
9980catch_syscall_command_1 (char *arg, int from_tty,
9981 struct cmd_list_element *command)
a96d9b2e
SDJ
9982{
9983 int tempflag;
9984 VEC(int) *filter;
9985 struct syscall s;
9986 struct gdbarch *gdbarch = get_current_arch ();
9987
9988 /* Checking if the feature if supported. */
9989 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9990 error (_("The feature 'catch syscall' is not supported on \
ea666128 9991this architecture yet."));
a96d9b2e
SDJ
9992
9993 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9994
e9cafbcc 9995 arg = skip_spaces (arg);
a96d9b2e
SDJ
9996
9997 /* We need to do this first "dummy" translation in order
9998 to get the syscall XML file loaded or, most important,
9999 to display a warning to the user if there's no XML file
10000 for his/her architecture. */
10001 get_syscall_by_number (0, &s);
10002
10003 /* The allowed syntax is:
10004 catch syscall
10005 catch syscall <name | number> [<name | number> ... <name | number>]
10006
10007 Let's check if there's a syscall name. */
10008
10009 if (arg != NULL)
10010 filter = catch_syscall_split_args (arg);
10011 else
10012 filter = NULL;
10013
10014 create_syscall_event_catchpoint (tempflag, filter,
10015 &catch_syscall_breakpoint_ops);
10016}
10017
c906108c 10018static void
fba45db2 10019catch_command (char *arg, int from_tty)
c906108c 10020{
44feb3ce 10021 error (_("Catch requires an event name."));
c906108c
SS
10022}
10023\f
10024
10025static void
fba45db2 10026tcatch_command (char *arg, int from_tty)
c906108c 10027{
44feb3ce 10028 error (_("Catch requires an event name."));
c906108c
SS
10029}
10030
8a2c437b
TT
10031/* A qsort comparison function that sorts breakpoints in order. */
10032
10033static int
10034compare_breakpoints (const void *a, const void *b)
10035{
10036 const breakpoint_p *ba = a;
10037 uintptr_t ua = (uintptr_t) *ba;
10038 const breakpoint_p *bb = b;
10039 uintptr_t ub = (uintptr_t) *bb;
10040
10041 if ((*ba)->number < (*bb)->number)
10042 return -1;
10043 else if ((*ba)->number > (*bb)->number)
10044 return 1;
10045
10046 /* Now sort by address, in case we see, e..g, two breakpoints with
10047 the number 0. */
10048 if (ua < ub)
10049 return -1;
10050 return ub > ub ? 1 : 0;
10051}
10052
80f8a6eb 10053/* Delete breakpoints by address or line. */
c906108c
SS
10054
10055static void
fba45db2 10056clear_command (char *arg, int from_tty)
c906108c 10057{
8a2c437b 10058 struct breakpoint *b, *prev;
d6e956e5
VP
10059 VEC(breakpoint_p) *found = 0;
10060 int ix;
c906108c
SS
10061 int default_match;
10062 struct symtabs_and_lines sals;
10063 struct symtab_and_line sal;
c906108c 10064 int i;
8a2c437b 10065 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
10066
10067 if (arg)
10068 {
f8eba3c6
TT
10069 sals = decode_line_spec (arg, (DECODE_LINE_FUNFIRSTLINE
10070 | DECODE_LINE_LIST_MODE));
c906108c
SS
10071 default_match = 0;
10072 }
10073 else
10074 {
c5aa993b 10075 sals.sals = (struct symtab_and_line *)
c906108c 10076 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 10077 make_cleanup (xfree, sals.sals);
4a64f543 10078 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
10079
10080 /* Set sal's line, symtab, pc, and pspace to the values
10081 corresponding to the last call to print_frame_info. If the
10082 codepoint is not valid, this will set all the fields to 0. */
10083 get_last_displayed_sal (&sal);
c906108c 10084 if (sal.symtab == 0)
8a3fe4f8 10085 error (_("No source file specified."));
c906108c
SS
10086
10087 sals.sals[0] = sal;
10088 sals.nelts = 1;
10089
10090 default_match = 1;
10091 }
10092
4a64f543
MS
10093 /* We don't call resolve_sal_pc here. That's not as bad as it
10094 seems, because all existing breakpoints typically have both
10095 file/line and pc set. So, if clear is given file/line, we can
10096 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10097
10098 We only support clearing given the address explicitly
10099 present in breakpoint table. Say, we've set breakpoint
4a64f543 10100 at file:line. There were several PC values for that file:line,
ed0616c6 10101 due to optimization, all in one block.
4a64f543
MS
10102
10103 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10104 PC corresponding to the same file:line, the breakpoint won't
10105 be cleared. We probably can still clear the breakpoint, but
10106 since the other PC value is never presented to user, user
10107 can only find it by guessing, and it does not seem important
10108 to support that. */
10109
4a64f543
MS
10110 /* For each line spec given, delete bps which correspond to it. Do
10111 it in two passes, solely to preserve the current behavior that
10112 from_tty is forced true if we delete more than one
10113 breakpoint. */
c906108c 10114
80f8a6eb 10115 found = NULL;
8a2c437b 10116 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
10117 for (i = 0; i < sals.nelts; i++)
10118 {
10119 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
10120 If line given (pc == 0), clear all bpts on specified line.
10121 If defaulting, clear all bpts on default line
c906108c 10122 or at default pc.
c5aa993b
JM
10123
10124 defaulting sal.pc != 0 tests to do
10125
10126 0 1 pc
10127 1 1 pc _and_ line
10128 0 0 line
10129 1 0 <can't happen> */
c906108c
SS
10130
10131 sal = sals.sals[i];
c906108c 10132
4a64f543 10133 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 10134 ALL_BREAKPOINTS (b)
c5aa993b 10135 {
0d381245 10136 int match = 0;
4a64f543 10137 /* Are we going to delete b? */
cc60f2e3 10138 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
10139 {
10140 struct bp_location *loc = b->loc;
10141 for (; loc; loc = loc->next)
10142 {
f8eba3c6
TT
10143 /* If the user specified file:line, don't allow a PC
10144 match. This matches historical gdb behavior. */
10145 int pc_match = (!sal.explicit_line
10146 && sal.pc
10147 && (loc->pspace == sal.pspace)
10148 && (loc->address == sal.pc)
10149 && (!section_is_overlay (loc->section)
10150 || loc->section == sal.section));
10151 int line_match = ((default_match || sal.explicit_line)
10152 && loc->source_file != NULL
0d381245 10153 && sal.symtab != NULL
6c95b8df 10154 && sal.pspace == loc->pspace
f8eba3c6 10155 && filename_cmp (loc->source_file,
0ba1096a 10156 sal.symtab->filename) == 0
f8eba3c6 10157 && loc->line_number == sal.line);
0d381245
VP
10158 if (pc_match || line_match)
10159 {
10160 match = 1;
10161 break;
10162 }
10163 }
10164 }
10165
10166 if (match)
d6e956e5 10167 VEC_safe_push(breakpoint_p, found, b);
c906108c 10168 }
80f8a6eb 10169 }
8a2c437b 10170
80f8a6eb 10171 /* Now go thru the 'found' chain and delete them. */
d6e956e5 10172 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
10173 {
10174 if (arg)
8a3fe4f8 10175 error (_("No breakpoint at %s."), arg);
80f8a6eb 10176 else
8a3fe4f8 10177 error (_("No breakpoint at this line."));
80f8a6eb 10178 }
c906108c 10179
8a2c437b
TT
10180 /* Remove duplicates from the vec. */
10181 qsort (VEC_address (breakpoint_p, found),
10182 VEC_length (breakpoint_p, found),
10183 sizeof (breakpoint_p),
10184 compare_breakpoints);
10185 prev = VEC_index (breakpoint_p, found, 0);
10186 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
10187 {
10188 if (b == prev)
10189 {
10190 VEC_ordered_remove (breakpoint_p, found, ix);
10191 --ix;
10192 }
10193 }
10194
d6e956e5 10195 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 10196 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10197 if (from_tty)
a3f17187 10198 {
d6e956e5 10199 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
10200 printf_unfiltered (_("Deleted breakpoint "));
10201 else
10202 printf_unfiltered (_("Deleted breakpoints "));
10203 }
80f8a6eb 10204 breakpoints_changed ();
d6e956e5
VP
10205
10206 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 10207 {
c5aa993b 10208 if (from_tty)
d6e956e5
VP
10209 printf_unfiltered ("%d ", b->number);
10210 delete_breakpoint (b);
c906108c 10211 }
80f8a6eb
MS
10212 if (from_tty)
10213 putchar_unfiltered ('\n');
8a2c437b
TT
10214
10215 do_cleanups (cleanups);
c906108c
SS
10216}
10217\f
10218/* Delete breakpoint in BS if they are `delete' breakpoints and
10219 all breakpoints that are marked for deletion, whether hit or not.
10220 This is called after any breakpoint is hit, or after errors. */
10221
10222void
fba45db2 10223breakpoint_auto_delete (bpstat bs)
c906108c 10224{
35df4500 10225 struct breakpoint *b, *b_tmp;
c906108c
SS
10226
10227 for (; bs; bs = bs->next)
f431efe5
PA
10228 if (bs->breakpoint_at
10229 && bs->breakpoint_at->disposition == disp_del
c906108c 10230 && bs->stop)
f431efe5 10231 delete_breakpoint (bs->breakpoint_at);
c906108c 10232
35df4500 10233 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10234 {
b5de0fa7 10235 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10236 delete_breakpoint (b);
10237 }
c906108c
SS
10238}
10239
4a64f543
MS
10240/* A comparison function for bp_location AP and BP being interfaced to
10241 qsort. Sort elements primarily by their ADDRESS (no matter what
10242 does breakpoint_address_is_meaningful say for its OWNER),
10243 secondarily by ordering first bp_permanent OWNERed elements and
10244 terciarily just ensuring the array is sorted stable way despite
e5dd4106 10245 qsort being an unstable algorithm. */
876fa593
JK
10246
10247static int
494cfb0f 10248bp_location_compare (const void *ap, const void *bp)
876fa593 10249{
494cfb0f
JK
10250 struct bp_location *a = *(void **) ap;
10251 struct bp_location *b = *(void **) bp;
2bdf28a0 10252 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10253 int a_perm = a->owner->enable_state == bp_permanent;
10254 int b_perm = b->owner->enable_state == bp_permanent;
10255
10256 if (a->address != b->address)
10257 return (a->address > b->address) - (a->address < b->address);
10258
10259 /* Sort permanent breakpoints first. */
10260 if (a_perm != b_perm)
10261 return (a_perm < b_perm) - (a_perm > b_perm);
10262
4a64f543
MS
10263 /* Make the user-visible order stable across GDB runs. Locations of
10264 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10265
10266 if (a->owner->number != b->owner->number)
10267 return (a->owner->number > b->owner->number)
10268 - (a->owner->number < b->owner->number);
10269
10270 return (a > b) - (a < b);
10271}
10272
876fa593 10273/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10274 bp_location_shadow_len_after_address_max according to the current
10275 content of the bp_location array. */
f7545552
TT
10276
10277static void
876fa593 10278bp_location_target_extensions_update (void)
f7545552 10279{
876fa593
JK
10280 struct bp_location *bl, **blp_tmp;
10281
10282 bp_location_placed_address_before_address_max = 0;
10283 bp_location_shadow_len_after_address_max = 0;
10284
10285 ALL_BP_LOCATIONS (bl, blp_tmp)
10286 {
10287 CORE_ADDR start, end, addr;
10288
10289 if (!bp_location_has_shadow (bl))
10290 continue;
10291
10292 start = bl->target_info.placed_address;
10293 end = start + bl->target_info.shadow_len;
10294
10295 gdb_assert (bl->address >= start);
10296 addr = bl->address - start;
10297 if (addr > bp_location_placed_address_before_address_max)
10298 bp_location_placed_address_before_address_max = addr;
10299
10300 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10301
10302 gdb_assert (bl->address < end);
10303 addr = end - bl->address;
10304 if (addr > bp_location_shadow_len_after_address_max)
10305 bp_location_shadow_len_after_address_max = addr;
10306 }
f7545552
TT
10307}
10308
1e4d1764
YQ
10309/* Download tracepoint locations if they haven't been. */
10310
10311static void
10312download_tracepoint_locations (void)
10313{
10314 struct bp_location *bl, **blp_tmp;
10315 struct cleanup *old_chain;
10316
10317 if (!target_can_download_tracepoint ())
10318 return;
10319
10320 old_chain = save_current_space_and_thread ();
10321
10322 ALL_BP_LOCATIONS (bl, blp_tmp)
10323 {
10324 struct tracepoint *t;
10325
10326 if (!is_tracepoint (bl->owner))
10327 continue;
10328
10329 if ((bl->owner->type == bp_fast_tracepoint
10330 ? !may_insert_fast_tracepoints
10331 : !may_insert_tracepoints))
10332 continue;
10333
10334 /* In tracepoint, locations are _never_ duplicated, so
10335 should_be_inserted is equivalent to
10336 unduplicated_should_be_inserted. */
10337 if (!should_be_inserted (bl) || bl->inserted)
10338 continue;
10339
10340 switch_to_program_space_and_thread (bl->pspace);
10341
10342 target_download_tracepoint (bl);
10343
10344 bl->inserted = 1;
10345 t = (struct tracepoint *) bl->owner;
10346 t->number_on_target = bl->owner->number;
10347 }
10348
10349 do_cleanups (old_chain);
10350}
10351
934709f0
PW
10352/* Swap the insertion/duplication state between two locations. */
10353
10354static void
10355swap_insertion (struct bp_location *left, struct bp_location *right)
10356{
10357 const int left_inserted = left->inserted;
10358 const int left_duplicate = left->duplicate;
10359 const struct bp_target_info left_target_info = left->target_info;
10360
1e4d1764
YQ
10361 /* Locations of tracepoints can never be duplicated. */
10362 if (is_tracepoint (left->owner))
10363 gdb_assert (!left->duplicate);
10364 if (is_tracepoint (right->owner))
10365 gdb_assert (!right->duplicate);
10366
934709f0
PW
10367 left->inserted = right->inserted;
10368 left->duplicate = right->duplicate;
10369 left->target_info = right->target_info;
10370 right->inserted = left_inserted;
10371 right->duplicate = left_duplicate;
10372 right->target_info = left_target_info;
10373}
10374
4cd9bd08 10375/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10376 into the inferior, only remove already-inserted locations that no
10377 longer should be inserted. Functions that delete a breakpoint or
10378 breakpoints should pass false, so that deleting a breakpoint
10379 doesn't have the side effect of inserting the locations of other
10380 breakpoints that are marked not-inserted, but should_be_inserted
10381 returns true on them.
10382
10383 This behaviour is useful is situations close to tear-down -- e.g.,
10384 after an exec, while the target still has execution, but breakpoint
10385 shadows of the previous executable image should *NOT* be restored
10386 to the new image; or before detaching, where the target still has
10387 execution and wants to delete breakpoints from GDB's lists, and all
10388 breakpoints had already been removed from the inferior. */
10389
0d381245 10390static void
b60e7edf 10391update_global_location_list (int should_insert)
0d381245 10392{
74960c60 10393 struct breakpoint *b;
876fa593 10394 struct bp_location **locp, *loc;
f7545552
TT
10395 struct cleanup *cleanups;
10396
2d134ed3
PA
10397 /* Used in the duplicates detection below. When iterating over all
10398 bp_locations, points to the first bp_location of a given address.
10399 Breakpoints and watchpoints of different types are never
10400 duplicates of each other. Keep one pointer for each type of
10401 breakpoint/watchpoint, so we only need to loop over all locations
10402 once. */
10403 struct bp_location *bp_loc_first; /* breakpoint */
10404 struct bp_location *wp_loc_first; /* hardware watchpoint */
10405 struct bp_location *awp_loc_first; /* access watchpoint */
10406 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10407
4a64f543
MS
10408 /* Saved former bp_location array which we compare against the newly
10409 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10410 struct bp_location **old_location, **old_locp;
10411 unsigned old_location_count;
10412
10413 old_location = bp_location;
10414 old_location_count = bp_location_count;
10415 bp_location = NULL;
10416 bp_location_count = 0;
10417 cleanups = make_cleanup (xfree, old_location);
0d381245 10418
74960c60 10419 ALL_BREAKPOINTS (b)
876fa593
JK
10420 for (loc = b->loc; loc; loc = loc->next)
10421 bp_location_count++;
10422
10423 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10424 locp = bp_location;
10425 ALL_BREAKPOINTS (b)
10426 for (loc = b->loc; loc; loc = loc->next)
10427 *locp++ = loc;
10428 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10429 bp_location_compare);
876fa593
JK
10430
10431 bp_location_target_extensions_update ();
74960c60 10432
4a64f543
MS
10433 /* Identify bp_location instances that are no longer present in the
10434 new list, and therefore should be freed. Note that it's not
10435 necessary that those locations should be removed from inferior --
10436 if there's another location at the same address (previously
10437 marked as duplicate), we don't need to remove/insert the
10438 location.
876fa593 10439
4a64f543
MS
10440 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10441 and former bp_location array state respectively. */
876fa593
JK
10442
10443 locp = bp_location;
10444 for (old_locp = old_location; old_locp < old_location + old_location_count;
10445 old_locp++)
74960c60 10446 {
876fa593 10447 struct bp_location *old_loc = *old_locp;
c7d46a38 10448 struct bp_location **loc2p;
876fa593 10449
e5dd4106 10450 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 10451 not, we have to free it. */
c7d46a38 10452 int found_object = 0;
20874c92
VP
10453 /* Tells if the location should remain inserted in the target. */
10454 int keep_in_target = 0;
10455 int removed = 0;
876fa593 10456
4a64f543
MS
10457 /* Skip LOCP entries which will definitely never be needed.
10458 Stop either at or being the one matching OLD_LOC. */
876fa593 10459 while (locp < bp_location + bp_location_count
c7d46a38 10460 && (*locp)->address < old_loc->address)
876fa593 10461 locp++;
c7d46a38
PA
10462
10463 for (loc2p = locp;
10464 (loc2p < bp_location + bp_location_count
10465 && (*loc2p)->address == old_loc->address);
10466 loc2p++)
10467 {
10468 if (*loc2p == old_loc)
10469 {
10470 found_object = 1;
10471 break;
10472 }
10473 }
74960c60 10474
4a64f543
MS
10475 /* If this location is no longer present, and inserted, look if
10476 there's maybe a new location at the same address. If so,
10477 mark that one inserted, and don't remove this one. This is
10478 needed so that we don't have a time window where a breakpoint
10479 at certain location is not inserted. */
74960c60 10480
876fa593 10481 if (old_loc->inserted)
0d381245 10482 {
4a64f543
MS
10483 /* If the location is inserted now, we might have to remove
10484 it. */
74960c60 10485
876fa593 10486 if (found_object && should_be_inserted (old_loc))
74960c60 10487 {
4a64f543
MS
10488 /* The location is still present in the location list,
10489 and still should be inserted. Don't do anything. */
20874c92 10490 keep_in_target = 1;
74960c60
VP
10491 }
10492 else
10493 {
4a64f543
MS
10494 /* The location is either no longer present, or got
10495 disabled. See if there's another location at the
10496 same address, in which case we don't need to remove
10497 this one from the target. */
876fa593 10498
2bdf28a0 10499 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10500 if (breakpoint_address_is_meaningful (old_loc->owner))
10501 {
876fa593 10502 for (loc2p = locp;
c7d46a38
PA
10503 (loc2p < bp_location + bp_location_count
10504 && (*loc2p)->address == old_loc->address);
876fa593
JK
10505 loc2p++)
10506 {
10507 struct bp_location *loc2 = *loc2p;
10508
2d134ed3 10509 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 10510 {
85d721b8
PA
10511 /* Read watchpoint locations are switched to
10512 access watchpoints, if the former are not
10513 supported, but the latter are. */
10514 if (is_hardware_watchpoint (old_loc->owner))
10515 {
10516 gdb_assert (is_hardware_watchpoint (loc2->owner));
10517 loc2->watchpoint_type = old_loc->watchpoint_type;
10518 }
10519
934709f0
PW
10520 /* loc2 is a duplicated location. We need to check
10521 if it should be inserted in case it will be
10522 unduplicated. */
10523 if (loc2 != old_loc
10524 && unduplicated_should_be_inserted (loc2))
c7d46a38 10525 {
934709f0 10526 swap_insertion (old_loc, loc2);
c7d46a38
PA
10527 keep_in_target = 1;
10528 break;
10529 }
876fa593
JK
10530 }
10531 }
10532 }
74960c60
VP
10533 }
10534
20874c92
VP
10535 if (!keep_in_target)
10536 {
876fa593 10537 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10538 {
4a64f543
MS
10539 /* This is just about all we can do. We could keep
10540 this location on the global list, and try to
10541 remove it next time, but there's no particular
10542 reason why we will succeed next time.
20874c92 10543
4a64f543
MS
10544 Note that at this point, old_loc->owner is still
10545 valid, as delete_breakpoint frees the breakpoint
10546 only after calling us. */
3e43a32a
MS
10547 printf_filtered (_("warning: Error removing "
10548 "breakpoint %d\n"),
876fa593 10549 old_loc->owner->number);
20874c92
VP
10550 }
10551 removed = 1;
10552 }
0d381245 10553 }
74960c60
VP
10554
10555 if (!found_object)
1c5cfe86 10556 {
db82e815
PA
10557 if (removed && non_stop
10558 && breakpoint_address_is_meaningful (old_loc->owner)
10559 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10560 {
db82e815
PA
10561 /* This location was removed from the target. In
10562 non-stop mode, a race condition is possible where
10563 we've removed a breakpoint, but stop events for that
10564 breakpoint are already queued and will arrive later.
10565 We apply an heuristic to be able to distinguish such
10566 SIGTRAPs from other random SIGTRAPs: we keep this
10567 breakpoint location for a bit, and will retire it
10568 after we see some number of events. The theory here
10569 is that reporting of events should, "on the average",
10570 be fair, so after a while we'll see events from all
10571 threads that have anything of interest, and no longer
10572 need to keep this breakpoint location around. We
10573 don't hold locations forever so to reduce chances of
10574 mistaking a non-breakpoint SIGTRAP for a breakpoint
10575 SIGTRAP.
10576
10577 The heuristic failing can be disastrous on
10578 decr_pc_after_break targets.
10579
10580 On decr_pc_after_break targets, like e.g., x86-linux,
10581 if we fail to recognize a late breakpoint SIGTRAP,
10582 because events_till_retirement has reached 0 too
10583 soon, we'll fail to do the PC adjustment, and report
10584 a random SIGTRAP to the user. When the user resumes
10585 the inferior, it will most likely immediately crash
2dec564e 10586 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10587 corrupted, because of being resumed e.g., in the
10588 middle of a multi-byte instruction, or skipped a
10589 one-byte instruction. This was actually seen happen
10590 on native x86-linux, and should be less rare on
10591 targets that do not support new thread events, like
10592 remote, due to the heuristic depending on
10593 thread_count.
10594
10595 Mistaking a random SIGTRAP for a breakpoint trap
10596 causes similar symptoms (PC adjustment applied when
10597 it shouldn't), but then again, playing with SIGTRAPs
10598 behind the debugger's back is asking for trouble.
10599
10600 Since hardware watchpoint traps are always
10601 distinguishable from other traps, so we don't need to
10602 apply keep hardware watchpoint moribund locations
10603 around. We simply always ignore hardware watchpoint
10604 traps we can no longer explain. */
10605
876fa593
JK
10606 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10607 old_loc->owner = NULL;
20874c92 10608
876fa593 10609 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10610 }
10611 else
f431efe5
PA
10612 {
10613 old_loc->owner = NULL;
10614 decref_bp_location (&old_loc);
10615 }
20874c92 10616 }
74960c60 10617 }
1c5cfe86 10618
348d480f
PA
10619 /* Rescan breakpoints at the same address and section, marking the
10620 first one as "first" and any others as "duplicates". This is so
10621 that the bpt instruction is only inserted once. If we have a
10622 permanent breakpoint at the same place as BPT, make that one the
10623 official one, and the rest as duplicates. Permanent breakpoints
10624 are sorted first for the same address.
10625
10626 Do the same for hardware watchpoints, but also considering the
10627 watchpoint's type (regular/access/read) and length. */
10628
10629 bp_loc_first = NULL;
10630 wp_loc_first = NULL;
10631 awp_loc_first = NULL;
10632 rwp_loc_first = NULL;
10633 ALL_BP_LOCATIONS (loc, locp)
10634 {
10635 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10636 non-NULL. */
348d480f 10637 struct bp_location **loc_first_p;
d3fbdd86 10638 b = loc->owner;
348d480f 10639
f8eba3c6 10640 if (!should_be_inserted (loc)
348d480f 10641 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
10642 /* Don't detect duplicate for tracepoint locations because they are
10643 never duplicated. See the comments in field `duplicate' of
10644 `struct bp_location'. */
348d480f
PA
10645 || is_tracepoint (b))
10646 continue;
10647
10648 /* Permanent breakpoint should always be inserted. */
10649 if (b->enable_state == bp_permanent && ! loc->inserted)
10650 internal_error (__FILE__, __LINE__,
10651 _("allegedly permanent breakpoint is not "
10652 "actually inserted"));
10653
10654 if (b->type == bp_hardware_watchpoint)
10655 loc_first_p = &wp_loc_first;
10656 else if (b->type == bp_read_watchpoint)
10657 loc_first_p = &rwp_loc_first;
10658 else if (b->type == bp_access_watchpoint)
10659 loc_first_p = &awp_loc_first;
10660 else
10661 loc_first_p = &bp_loc_first;
10662
10663 if (*loc_first_p == NULL
10664 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10665 || !breakpoint_locations_match (loc, *loc_first_p))
10666 {
10667 *loc_first_p = loc;
10668 loc->duplicate = 0;
10669 continue;
10670 }
10671
934709f0
PW
10672
10673 /* This and the above ensure the invariant that the first location
10674 is not duplicated, and is the inserted one.
10675 All following are marked as duplicated, and are not inserted. */
10676 if (loc->inserted)
10677 swap_insertion (loc, *loc_first_p);
348d480f
PA
10678 loc->duplicate = 1;
10679
10680 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10681 && b->enable_state != bp_permanent)
10682 internal_error (__FILE__, __LINE__,
10683 _("another breakpoint was inserted on top of "
10684 "a permanent breakpoint"));
10685 }
10686
10687 if (breakpoints_always_inserted_mode () && should_insert
10688 && (have_live_inferiors ()
10689 || (gdbarch_has_global_breakpoints (target_gdbarch))))
10690 insert_breakpoint_locations ();
10691
1e4d1764
YQ
10692 if (should_insert)
10693 download_tracepoint_locations ();
10694
348d480f
PA
10695 do_cleanups (cleanups);
10696}
10697
10698void
10699breakpoint_retire_moribund (void)
10700{
10701 struct bp_location *loc;
10702 int ix;
10703
10704 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10705 if (--(loc->events_till_retirement) == 0)
10706 {
10707 decref_bp_location (&loc);
10708 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10709 --ix;
10710 }
10711}
10712
10713static void
10714update_global_location_list_nothrow (int inserting)
10715{
10716 struct gdb_exception e;
10717
10718 TRY_CATCH (e, RETURN_MASK_ERROR)
10719 update_global_location_list (inserting);
10720}
10721
10722/* Clear BKP from a BPS. */
10723
10724static void
10725bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10726{
10727 bpstat bs;
10728
10729 for (bs = bps; bs; bs = bs->next)
10730 if (bs->breakpoint_at == bpt)
10731 {
10732 bs->breakpoint_at = NULL;
10733 bs->old_val = NULL;
10734 /* bs->commands will be freed later. */
10735 }
10736}
10737
10738/* Callback for iterate_over_threads. */
10739static int
10740bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10741{
10742 struct breakpoint *bpt = data;
10743
10744 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10745 return 0;
10746}
10747
10748/* Helper for breakpoint and tracepoint breakpoint_ops->mention
10749 callbacks. */
10750
10751static void
10752say_where (struct breakpoint *b)
10753{
79a45e25 10754 struct ui_out *uiout = current_uiout;
348d480f
PA
10755 struct value_print_options opts;
10756
10757 get_user_print_options (&opts);
10758
10759 /* i18n: cagney/2005-02-11: Below needs to be merged into a
10760 single string. */
10761 if (b->loc == NULL)
10762 {
10763 printf_filtered (_(" (%s) pending."), b->addr_string);
10764 }
10765 else
10766 {
f8eba3c6 10767 if (opts.addressprint || b->loc->source_file == NULL)
348d480f
PA
10768 {
10769 printf_filtered (" at ");
10770 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
10771 gdb_stdout);
10772 }
f8eba3c6
TT
10773 if (b->loc->source_file)
10774 {
10775 /* If there is a single location, we can print the location
10776 more nicely. */
10777 if (b->loc->next == NULL)
10778 printf_filtered (": file %s, line %d.",
10779 b->loc->source_file, b->loc->line_number);
10780 else
10781 /* This is not ideal, but each location may have a
10782 different file name, and this at least reflects the
10783 real situation somewhat. */
10784 printf_filtered (": %s.", b->addr_string);
10785 }
348d480f
PA
10786
10787 if (b->loc->next)
10788 {
10789 struct bp_location *loc = b->loc;
10790 int n = 0;
10791 for (; loc; loc = loc->next)
10792 ++n;
10793 printf_filtered (" (%d locations)", n);
10794 }
10795 }
10796}
10797
348d480f
PA
10798/* Default bp_location_ops methods. */
10799
10800static void
10801bp_location_dtor (struct bp_location *self)
10802{
10803 xfree (self->cond);
10804 xfree (self->function_name);
f8eba3c6 10805 xfree (self->source_file);
348d480f
PA
10806}
10807
10808static const struct bp_location_ops bp_location_ops =
10809{
10810 bp_location_dtor
10811};
10812
2060206e
PA
10813/* Default breakpoint_ops methods all breakpoint_ops ultimately
10814 inherit from. */
348d480f 10815
2060206e
PA
10816static void
10817base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
10818{
10819 decref_counted_command_line (&self->commands);
10820 xfree (self->cond_string);
348d480f 10821 xfree (self->addr_string);
f8eba3c6 10822 xfree (self->filter);
348d480f 10823 xfree (self->addr_string_range_end);
348d480f
PA
10824}
10825
2060206e
PA
10826static struct bp_location *
10827base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
10828{
10829 struct bp_location *loc;
10830
10831 loc = XNEW (struct bp_location);
10832 init_bp_location (loc, &bp_location_ops, self);
10833 return loc;
10834}
10835
2060206e
PA
10836static void
10837base_breakpoint_re_set (struct breakpoint *b)
10838{
10839 /* Nothing to re-set. */
10840}
10841
10842#define internal_error_pure_virtual_called() \
10843 gdb_assert_not_reached ("pure virtual function called")
10844
10845static int
10846base_breakpoint_insert_location (struct bp_location *bl)
10847{
10848 internal_error_pure_virtual_called ();
10849}
10850
10851static int
10852base_breakpoint_remove_location (struct bp_location *bl)
10853{
10854 internal_error_pure_virtual_called ();
10855}
10856
10857static int
10858base_breakpoint_breakpoint_hit (const struct bp_location *bl,
10859 struct address_space *aspace,
10860 CORE_ADDR bp_addr)
10861{
10862 internal_error_pure_virtual_called ();
10863}
10864
10865static void
10866base_breakpoint_check_status (bpstat bs)
10867{
10868 /* Always stop. */
10869}
10870
10871/* A "works_in_software_mode" breakpoint_ops method that just internal
10872 errors. */
10873
10874static int
10875base_breakpoint_works_in_software_mode (const struct breakpoint *b)
10876{
10877 internal_error_pure_virtual_called ();
10878}
10879
10880/* A "resources_needed" breakpoint_ops method that just internal
10881 errors. */
10882
10883static int
10884base_breakpoint_resources_needed (const struct bp_location *bl)
10885{
10886 internal_error_pure_virtual_called ();
10887}
10888
10889static enum print_stop_action
10890base_breakpoint_print_it (bpstat bs)
10891{
10892 internal_error_pure_virtual_called ();
10893}
10894
10895static void
10896base_breakpoint_print_one_detail (const struct breakpoint *self,
10897 struct ui_out *uiout)
10898{
10899 /* nothing */
10900}
10901
10902static void
10903base_breakpoint_print_mention (struct breakpoint *b)
10904{
10905 internal_error_pure_virtual_called ();
10906}
10907
10908static void
10909base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
10910{
10911 internal_error_pure_virtual_called ();
10912}
10913
10914static struct breakpoint_ops base_breakpoint_ops =
10915{
10916 base_breakpoint_dtor,
10917 base_breakpoint_allocate_location,
10918 base_breakpoint_re_set,
10919 base_breakpoint_insert_location,
10920 base_breakpoint_remove_location,
10921 base_breakpoint_breakpoint_hit,
10922 base_breakpoint_check_status,
10923 base_breakpoint_resources_needed,
10924 base_breakpoint_works_in_software_mode,
10925 base_breakpoint_print_it,
10926 NULL,
10927 base_breakpoint_print_one_detail,
10928 base_breakpoint_print_mention,
10929 base_breakpoint_print_recreate
10930};
10931
10932/* Default breakpoint_ops methods. */
10933
10934static void
348d480f
PA
10935bkpt_re_set (struct breakpoint *b)
10936{
06edf0c0
PA
10937 /* FIXME: is this still reachable? */
10938 if (b->addr_string == NULL)
10939 {
10940 /* Anything without a string can't be re-set. */
348d480f 10941 delete_breakpoint (b);
06edf0c0 10942 return;
348d480f 10943 }
06edf0c0
PA
10944
10945 breakpoint_re_set_default (b);
348d480f
PA
10946}
10947
2060206e 10948static int
348d480f
PA
10949bkpt_insert_location (struct bp_location *bl)
10950{
10951 if (bl->loc_type == bp_loc_hardware_breakpoint)
10952 return target_insert_hw_breakpoint (bl->gdbarch,
10953 &bl->target_info);
10954 else
10955 return target_insert_breakpoint (bl->gdbarch,
10956 &bl->target_info);
10957}
10958
2060206e 10959static int
348d480f
PA
10960bkpt_remove_location (struct bp_location *bl)
10961{
10962 if (bl->loc_type == bp_loc_hardware_breakpoint)
10963 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
10964 else
10965 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
10966}
10967
2060206e 10968static int
348d480f
PA
10969bkpt_breakpoint_hit (const struct bp_location *bl,
10970 struct address_space *aspace, CORE_ADDR bp_addr)
10971{
10972 struct breakpoint *b = bl->owner;
10973
10974 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
10975 aspace, bp_addr))
10976 return 0;
10977
10978 if (overlay_debugging /* unmapped overlay section */
10979 && section_is_overlay (bl->section)
10980 && !section_is_mapped (bl->section))
10981 return 0;
10982
10983 return 1;
10984}
10985
2060206e 10986static int
348d480f
PA
10987bkpt_resources_needed (const struct bp_location *bl)
10988{
10989 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
10990
10991 return 1;
10992}
10993
2060206e 10994static enum print_stop_action
348d480f
PA
10995bkpt_print_it (bpstat bs)
10996{
348d480f
PA
10997 struct breakpoint *b;
10998 const struct bp_location *bl;
001c8c33 10999 int bp_temp;
79a45e25 11000 struct ui_out *uiout = current_uiout;
348d480f
PA
11001
11002 gdb_assert (bs->bp_location_at != NULL);
11003
11004 bl = bs->bp_location_at;
11005 b = bs->breakpoint_at;
11006
001c8c33
PA
11007 bp_temp = b->disposition == disp_del;
11008 if (bl->address != bl->requested_address)
11009 breakpoint_adjustment_warning (bl->requested_address,
11010 bl->address,
11011 b->number, 1);
11012 annotate_breakpoint (b->number);
11013 if (bp_temp)
11014 ui_out_text (uiout, "\nTemporary breakpoint ");
11015 else
11016 ui_out_text (uiout, "\nBreakpoint ");
11017 if (ui_out_is_mi_like_p (uiout))
348d480f 11018 {
001c8c33
PA
11019 ui_out_field_string (uiout, "reason",
11020 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11021 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 11022 }
001c8c33
PA
11023 ui_out_field_int (uiout, "bkptno", b->number);
11024 ui_out_text (uiout, ", ");
06edf0c0 11025
001c8c33 11026 return PRINT_SRC_AND_LOC;
06edf0c0
PA
11027}
11028
2060206e 11029static void
06edf0c0
PA
11030bkpt_print_mention (struct breakpoint *b)
11031{
79a45e25 11032 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
11033 return;
11034
11035 switch (b->type)
11036 {
11037 case bp_breakpoint:
11038 case bp_gnu_ifunc_resolver:
11039 if (b->disposition == disp_del)
11040 printf_filtered (_("Temporary breakpoint"));
11041 else
11042 printf_filtered (_("Breakpoint"));
11043 printf_filtered (_(" %d"), b->number);
11044 if (b->type == bp_gnu_ifunc_resolver)
11045 printf_filtered (_(" at gnu-indirect-function resolver"));
11046 break;
11047 case bp_hardware_breakpoint:
11048 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
11049 break;
11050 }
11051
11052 say_where (b);
11053}
11054
2060206e 11055static void
06edf0c0
PA
11056bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
11057{
11058 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11059 fprintf_unfiltered (fp, "tbreak");
11060 else if (tp->type == bp_breakpoint)
11061 fprintf_unfiltered (fp, "break");
11062 else if (tp->type == bp_hardware_breakpoint
11063 && tp->disposition == disp_del)
11064 fprintf_unfiltered (fp, "thbreak");
11065 else if (tp->type == bp_hardware_breakpoint)
11066 fprintf_unfiltered (fp, "hbreak");
11067 else
11068 internal_error (__FILE__, __LINE__,
11069 _("unhandled breakpoint type %d"), (int) tp->type);
11070
2060206e 11071 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 11072 print_recreate_thread (tp, fp);
06edf0c0
PA
11073}
11074
06edf0c0
PA
11075/* Virtual table for internal breakpoints. */
11076
11077static void
11078internal_bkpt_re_set (struct breakpoint *b)
11079{
11080 switch (b->type)
11081 {
11082 /* Delete overlay event and longjmp master breakpoints; they
11083 will be reset later by breakpoint_re_set. */
11084 case bp_overlay_event:
11085 case bp_longjmp_master:
11086 case bp_std_terminate_master:
11087 case bp_exception_master:
11088 delete_breakpoint (b);
11089 break;
11090
11091 /* This breakpoint is special, it's set up when the inferior
11092 starts and we really don't want to touch it. */
11093 case bp_shlib_event:
11094
11095 /* Like bp_shlib_event, this breakpoint type is special. Once
11096 it is set up, we do not want to touch it. */
11097 case bp_thread_event:
11098 break;
11099 }
11100}
11101
11102static void
11103internal_bkpt_check_status (bpstat bs)
11104{
11105 /* We do not stop for these. */
11106 bs->stop = 0;
11107}
11108
11109static enum print_stop_action
11110internal_bkpt_print_it (bpstat bs)
11111{
36dfb11c 11112 struct ui_out *uiout = current_uiout;
06edf0c0 11113 struct breakpoint *b;
06edf0c0 11114
06edf0c0
PA
11115 b = bs->breakpoint_at;
11116
06edf0c0
PA
11117 switch (b->type)
11118 {
348d480f
PA
11119 case bp_shlib_event:
11120 /* Did we stop because the user set the stop_on_solib_events
11121 variable? (If so, we report this as a generic, "Stopped due
11122 to shlib event" message.) */
36dfb11c
TT
11123 ui_out_text (uiout, _("Stopped due to shared library event\n"));
11124 if (ui_out_is_mi_like_p (uiout))
11125 ui_out_field_string (uiout, "reason",
11126 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
348d480f
PA
11127 break;
11128
11129 case bp_thread_event:
11130 /* Not sure how we will get here.
11131 GDB should not stop for these breakpoints. */
11132 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11133 break;
11134
11135 case bp_overlay_event:
11136 /* By analogy with the thread event, GDB should not stop for these. */
11137 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11138 break;
11139
11140 case bp_longjmp_master:
11141 /* These should never be enabled. */
11142 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11143 break;
11144
11145 case bp_std_terminate_master:
11146 /* These should never be enabled. */
11147 printf_filtered (_("std::terminate Master Breakpoint: "
11148 "gdb should not stop!\n"));
348d480f
PA
11149 break;
11150
11151 case bp_exception_master:
11152 /* These should never be enabled. */
11153 printf_filtered (_("Exception Master Breakpoint: "
11154 "gdb should not stop!\n"));
06edf0c0
PA
11155 break;
11156 }
11157
001c8c33 11158 return PRINT_NOTHING;
06edf0c0
PA
11159}
11160
11161static void
11162internal_bkpt_print_mention (struct breakpoint *b)
11163{
11164 /* Nothing to mention. These breakpoints are internal. */
11165}
11166
06edf0c0
PA
11167/* Virtual table for momentary breakpoints */
11168
11169static void
11170momentary_bkpt_re_set (struct breakpoint *b)
11171{
11172 /* Keep temporary breakpoints, which can be encountered when we step
11173 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
11174 Otherwise these should have been blown away via the cleanup chain
11175 or by breakpoint_init_inferior when we rerun the executable. */
11176}
11177
11178static void
11179momentary_bkpt_check_status (bpstat bs)
11180{
11181 /* Nothing. The point of these breakpoints is causing a stop. */
11182}
11183
11184static enum print_stop_action
11185momentary_bkpt_print_it (bpstat bs)
11186{
79a45e25
PA
11187 struct ui_out *uiout = current_uiout;
11188
001c8c33 11189 if (ui_out_is_mi_like_p (uiout))
06edf0c0 11190 {
001c8c33 11191 struct breakpoint *b = bs->breakpoint_at;
348d480f 11192
001c8c33
PA
11193 switch (b->type)
11194 {
11195 case bp_finish:
11196 ui_out_field_string
11197 (uiout, "reason",
11198 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
11199 break;
348d480f 11200
001c8c33
PA
11201 case bp_until:
11202 ui_out_field_string
11203 (uiout, "reason",
11204 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
11205 break;
11206 }
348d480f
PA
11207 }
11208
001c8c33 11209 return PRINT_UNKNOWN;
348d480f
PA
11210}
11211
06edf0c0
PA
11212static void
11213momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 11214{
06edf0c0 11215 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11216}
11217
348d480f 11218/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 11219
348d480f
PA
11220static void
11221tracepoint_re_set (struct breakpoint *b)
11222{
11223 breakpoint_re_set_default (b);
11224}
876fa593 11225
348d480f
PA
11226static int
11227tracepoint_breakpoint_hit (const struct bp_location *bl,
11228 struct address_space *aspace, CORE_ADDR bp_addr)
11229{
11230 /* By definition, the inferior does not report stops at
11231 tracepoints. */
11232 return 0;
74960c60
VP
11233}
11234
11235static void
348d480f
PA
11236tracepoint_print_one_detail (const struct breakpoint *self,
11237 struct ui_out *uiout)
74960c60 11238{
d9b3f62e
PA
11239 struct tracepoint *tp = (struct tracepoint *) self;
11240 if (tp->static_trace_marker_id)
348d480f
PA
11241 {
11242 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 11243
348d480f
PA
11244 ui_out_text (uiout, "\tmarker id is ");
11245 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 11246 tp->static_trace_marker_id);
348d480f
PA
11247 ui_out_text (uiout, "\n");
11248 }
0d381245
VP
11249}
11250
a474d7c2 11251static void
348d480f 11252tracepoint_print_mention (struct breakpoint *b)
a474d7c2 11253{
79a45e25 11254 if (ui_out_is_mi_like_p (current_uiout))
348d480f 11255 return;
cc59ec59 11256
348d480f
PA
11257 switch (b->type)
11258 {
11259 case bp_tracepoint:
11260 printf_filtered (_("Tracepoint"));
11261 printf_filtered (_(" %d"), b->number);
11262 break;
11263 case bp_fast_tracepoint:
11264 printf_filtered (_("Fast tracepoint"));
11265 printf_filtered (_(" %d"), b->number);
11266 break;
11267 case bp_static_tracepoint:
11268 printf_filtered (_("Static tracepoint"));
11269 printf_filtered (_(" %d"), b->number);
11270 break;
11271 default:
11272 internal_error (__FILE__, __LINE__,
11273 _("unhandled tracepoint type %d"), (int) b->type);
11274 }
11275
11276 say_where (b);
a474d7c2
PA
11277}
11278
348d480f 11279static void
d9b3f62e 11280tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 11281{
d9b3f62e
PA
11282 struct tracepoint *tp = (struct tracepoint *) self;
11283
11284 if (self->type == bp_fast_tracepoint)
348d480f 11285 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 11286 if (self->type == bp_static_tracepoint)
348d480f 11287 fprintf_unfiltered (fp, "strace");
d9b3f62e 11288 else if (self->type == bp_tracepoint)
348d480f
PA
11289 fprintf_unfiltered (fp, "trace");
11290 else
11291 internal_error (__FILE__, __LINE__,
d9b3f62e 11292 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 11293
d9b3f62e
PA
11294 fprintf_unfiltered (fp, " %s", self->addr_string);
11295 print_recreate_thread (self, fp);
11296
11297 if (tp->pass_count)
11298 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
11299}
11300
2060206e 11301struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 11302
53a5351d 11303/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 11304 structures. */
c906108c
SS
11305
11306void
fba45db2 11307delete_breakpoint (struct breakpoint *bpt)
c906108c 11308{
52f0bd74 11309 struct breakpoint *b;
c906108c 11310
8a3fe4f8 11311 gdb_assert (bpt != NULL);
c906108c 11312
4a64f543
MS
11313 /* Has this bp already been deleted? This can happen because
11314 multiple lists can hold pointers to bp's. bpstat lists are
11315 especial culprits.
11316
11317 One example of this happening is a watchpoint's scope bp. When
11318 the scope bp triggers, we notice that the watchpoint is out of
11319 scope, and delete it. We also delete its scope bp. But the
11320 scope bp is marked "auto-deleting", and is already on a bpstat.
11321 That bpstat is then checked for auto-deleting bp's, which are
11322 deleted.
11323
11324 A real solution to this problem might involve reference counts in
11325 bp's, and/or giving them pointers back to their referencing
11326 bpstat's, and teaching delete_breakpoint to only free a bp's
11327 storage when no more references were extent. A cheaper bandaid
11328 was chosen. */
c906108c
SS
11329 if (bpt->type == bp_none)
11330 return;
11331
4a64f543
MS
11332 /* At least avoid this stale reference until the reference counting
11333 of breakpoints gets resolved. */
d0fb5eae 11334 if (bpt->related_breakpoint != bpt)
e5a0a904 11335 {
d0fb5eae 11336 struct breakpoint *related;
3a5c3e22 11337 struct watchpoint *w;
d0fb5eae
JK
11338
11339 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 11340 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 11341 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
11342 w = (struct watchpoint *) bpt;
11343 else
11344 w = NULL;
11345 if (w != NULL)
11346 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
11347
11348 /* Unlink bpt from the bpt->related_breakpoint ring. */
11349 for (related = bpt; related->related_breakpoint != bpt;
11350 related = related->related_breakpoint);
11351 related->related_breakpoint = bpt->related_breakpoint;
11352 bpt->related_breakpoint = bpt;
e5a0a904
JK
11353 }
11354
a9634178
TJB
11355 /* watch_command_1 creates a watchpoint but only sets its number if
11356 update_watchpoint succeeds in creating its bp_locations. If there's
11357 a problem in that process, we'll be asked to delete the half-created
11358 watchpoint. In that case, don't announce the deletion. */
11359 if (bpt->number)
11360 observer_notify_breakpoint_deleted (bpt);
c906108c 11361
c906108c
SS
11362 if (breakpoint_chain == bpt)
11363 breakpoint_chain = bpt->next;
11364
c906108c
SS
11365 ALL_BREAKPOINTS (b)
11366 if (b->next == bpt)
c5aa993b
JM
11367 {
11368 b->next = bpt->next;
11369 break;
11370 }
c906108c 11371
f431efe5
PA
11372 /* Be sure no bpstat's are pointing at the breakpoint after it's
11373 been freed. */
11374 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 11375 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
11376 pointing at bpt from the stop_bpstat list entirely, as breakpoint
11377 commands are associated with the bpstat; if we remove it here,
11378 then the later call to bpstat_do_actions (&stop_bpstat); in
11379 event-top.c won't do anything, and temporary breakpoints with
11380 commands won't work. */
11381
11382 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11383
4a64f543
MS
11384 /* Now that breakpoint is removed from breakpoint list, update the
11385 global location list. This will remove locations that used to
11386 belong to this breakpoint. Do this before freeing the breakpoint
11387 itself, since remove_breakpoint looks at location's owner. It
11388 might be better design to have location completely
11389 self-contained, but it's not the case now. */
b60e7edf 11390 update_global_location_list (0);
74960c60 11391
348d480f 11392 bpt->ops->dtor (bpt);
4a64f543
MS
11393 /* On the chance that someone will soon try again to delete this
11394 same bp, we mark it as deleted before freeing its storage. */
c906108c 11395 bpt->type = bp_none;
b8c9b27d 11396 xfree (bpt);
c906108c
SS
11397}
11398
4d6140d9
AC
11399static void
11400do_delete_breakpoint_cleanup (void *b)
11401{
11402 delete_breakpoint (b);
11403}
11404
11405struct cleanup *
11406make_cleanup_delete_breakpoint (struct breakpoint *b)
11407{
11408 return make_cleanup (do_delete_breakpoint_cleanup, b);
11409}
11410
51be5b68
PA
11411/* Iterator function to call a user-provided callback function once
11412 for each of B and its related breakpoints. */
11413
11414static void
11415iterate_over_related_breakpoints (struct breakpoint *b,
11416 void (*function) (struct breakpoint *,
11417 void *),
11418 void *data)
11419{
11420 struct breakpoint *related;
11421
11422 related = b;
11423 do
11424 {
11425 struct breakpoint *next;
11426
11427 /* FUNCTION may delete RELATED. */
11428 next = related->related_breakpoint;
11429
11430 if (next == related)
11431 {
11432 /* RELATED is the last ring entry. */
11433 function (related, data);
11434
11435 /* FUNCTION may have deleted it, so we'd never reach back to
11436 B. There's nothing left to do anyway, so just break
11437 out. */
11438 break;
11439 }
11440 else
11441 function (related, data);
11442
11443 related = next;
11444 }
11445 while (related != b);
11446}
95a42b64
TT
11447
11448static void
11449do_delete_breakpoint (struct breakpoint *b, void *ignore)
11450{
11451 delete_breakpoint (b);
11452}
11453
51be5b68
PA
11454/* A callback for map_breakpoint_numbers that calls
11455 delete_breakpoint. */
11456
11457static void
11458do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
11459{
11460 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
11461}
11462
c906108c 11463void
fba45db2 11464delete_command (char *arg, int from_tty)
c906108c 11465{
35df4500 11466 struct breakpoint *b, *b_tmp;
c906108c 11467
ea9365bb
TT
11468 dont_repeat ();
11469
c906108c
SS
11470 if (arg == 0)
11471 {
11472 int breaks_to_delete = 0;
11473
46c6471b
PA
11474 /* Delete all breakpoints if no argument. Do not delete
11475 internal breakpoints, these have to be deleted with an
11476 explicit breakpoint number argument. */
c5aa993b 11477 ALL_BREAKPOINTS (b)
46c6471b 11478 if (user_breakpoint_p (b))
973d738b
DJ
11479 {
11480 breaks_to_delete = 1;
11481 break;
11482 }
c906108c
SS
11483
11484 /* Ask user only if there are some breakpoints to delete. */
11485 if (!from_tty
e2e0b3e5 11486 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 11487 {
35df4500 11488 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 11489 if (user_breakpoint_p (b))
c5aa993b 11490 delete_breakpoint (b);
c906108c
SS
11491 }
11492 }
11493 else
51be5b68 11494 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
11495}
11496
0d381245
VP
11497static int
11498all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 11499{
0d381245
VP
11500 for (; loc; loc = loc->next)
11501 if (!loc->shlib_disabled)
11502 return 0;
11503 return 1;
fe3f5fa8
VP
11504}
11505
776592bf
DE
11506/* Subroutine of update_breakpoint_locations to simplify it.
11507 Return non-zero if multiple fns in list LOC have the same name.
11508 Null names are ignored. */
11509
11510static int
11511ambiguous_names_p (struct bp_location *loc)
11512{
11513 struct bp_location *l;
11514 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
11515 (int (*) (const void *,
11516 const void *)) streq,
776592bf
DE
11517 NULL, xcalloc, xfree);
11518
11519 for (l = loc; l != NULL; l = l->next)
11520 {
11521 const char **slot;
11522 const char *name = l->function_name;
11523
11524 /* Allow for some names to be NULL, ignore them. */
11525 if (name == NULL)
11526 continue;
11527
11528 slot = (const char **) htab_find_slot (htab, (const void *) name,
11529 INSERT);
4a64f543
MS
11530 /* NOTE: We can assume slot != NULL here because xcalloc never
11531 returns NULL. */
776592bf
DE
11532 if (*slot != NULL)
11533 {
11534 htab_delete (htab);
11535 return 1;
11536 }
11537 *slot = name;
11538 }
11539
11540 htab_delete (htab);
11541 return 0;
11542}
11543
0fb4aa4b
PA
11544/* When symbols change, it probably means the sources changed as well,
11545 and it might mean the static tracepoint markers are no longer at
11546 the same address or line numbers they used to be at last we
11547 checked. Losing your static tracepoints whenever you rebuild is
11548 undesirable. This function tries to resync/rematch gdb static
11549 tracepoints with the markers on the target, for static tracepoints
11550 that have not been set by marker id. Static tracepoint that have
11551 been set by marker id are reset by marker id in breakpoint_re_set.
11552 The heuristic is:
11553
11554 1) For a tracepoint set at a specific address, look for a marker at
11555 the old PC. If one is found there, assume to be the same marker.
11556 If the name / string id of the marker found is different from the
11557 previous known name, assume that means the user renamed the marker
11558 in the sources, and output a warning.
11559
11560 2) For a tracepoint set at a given line number, look for a marker
11561 at the new address of the old line number. If one is found there,
11562 assume to be the same marker. If the name / string id of the
11563 marker found is different from the previous known name, assume that
11564 means the user renamed the marker in the sources, and output a
11565 warning.
11566
11567 3) If a marker is no longer found at the same address or line, it
11568 may mean the marker no longer exists. But it may also just mean
11569 the code changed a bit. Maybe the user added a few lines of code
11570 that made the marker move up or down (in line number terms). Ask
11571 the target for info about the marker with the string id as we knew
11572 it. If found, update line number and address in the matching
11573 static tracepoint. This will get confused if there's more than one
11574 marker with the same ID (possible in UST, although unadvised
11575 precisely because it confuses tools). */
11576
11577static struct symtab_and_line
11578update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11579{
d9b3f62e 11580 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
11581 struct static_tracepoint_marker marker;
11582 CORE_ADDR pc;
11583 int i;
11584
11585 pc = sal.pc;
11586 if (sal.line)
11587 find_line_pc (sal.symtab, sal.line, &pc);
11588
11589 if (target_static_tracepoint_marker_at (pc, &marker))
11590 {
d9b3f62e 11591 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
11592 warning (_("static tracepoint %d changed probed marker from %s to %s"),
11593 b->number,
d9b3f62e 11594 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 11595
d9b3f62e
PA
11596 xfree (tp->static_trace_marker_id);
11597 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
11598 release_static_tracepoint_marker (&marker);
11599
11600 return sal;
11601 }
11602
11603 /* Old marker wasn't found on target at lineno. Try looking it up
11604 by string ID. */
11605 if (!sal.explicit_pc
11606 && sal.line != 0
11607 && sal.symtab != NULL
d9b3f62e 11608 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
11609 {
11610 VEC(static_tracepoint_marker_p) *markers;
11611
11612 markers
d9b3f62e 11613 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
11614
11615 if (!VEC_empty(static_tracepoint_marker_p, markers))
11616 {
80e1d417 11617 struct symtab_and_line sal2;
0fb4aa4b 11618 struct symbol *sym;
80e1d417 11619 struct static_tracepoint_marker *tpmarker;
79a45e25 11620 struct ui_out *uiout = current_uiout;
0fb4aa4b 11621
80e1d417 11622 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 11623
d9b3f62e 11624 xfree (tp->static_trace_marker_id);
80e1d417 11625 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
11626
11627 warning (_("marker for static tracepoint %d (%s) not "
11628 "found at previous line number"),
d9b3f62e 11629 b->number, tp->static_trace_marker_id);
0fb4aa4b 11630
80e1d417 11631 init_sal (&sal2);
0fb4aa4b 11632
80e1d417 11633 sal2.pc = tpmarker->address;
0fb4aa4b 11634
80e1d417
AS
11635 sal2 = find_pc_line (tpmarker->address, 0);
11636 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
11637 ui_out_text (uiout, "Now in ");
11638 if (sym)
11639 {
11640 ui_out_field_string (uiout, "func",
11641 SYMBOL_PRINT_NAME (sym));
11642 ui_out_text (uiout, " at ");
11643 }
80e1d417 11644 ui_out_field_string (uiout, "file", sal2.symtab->filename);
0fb4aa4b
PA
11645 ui_out_text (uiout, ":");
11646
11647 if (ui_out_is_mi_like_p (uiout))
11648 {
80e1d417 11649 char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b
PA
11650
11651 if (fullname)
11652 ui_out_field_string (uiout, "fullname", fullname);
11653 }
11654
80e1d417 11655 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
11656 ui_out_text (uiout, "\n");
11657
80e1d417 11658 b->loc->line_number = sal2.line;
0fb4aa4b 11659
f8eba3c6 11660 xfree (b->loc->source_file);
0fb4aa4b 11661 if (sym)
80e1d417 11662 b->loc->source_file = xstrdup (sal2.symtab->filename);
0fb4aa4b 11663 else
f8eba3c6 11664 b->loc->source_file = NULL;
0fb4aa4b
PA
11665
11666 xfree (b->addr_string);
11667 b->addr_string = xstrprintf ("%s:%d",
80e1d417 11668 sal2.symtab->filename,
f8eba3c6 11669 b->loc->line_number);
0fb4aa4b
PA
11670
11671 /* Might be nice to check if function changed, and warn if
11672 so. */
11673
80e1d417 11674 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
11675 }
11676 }
11677 return sal;
11678}
11679
8d3788bd
VP
11680/* Returns 1 iff locations A and B are sufficiently same that
11681 we don't need to report breakpoint as changed. */
11682
11683static int
11684locations_are_equal (struct bp_location *a, struct bp_location *b)
11685{
11686 while (a && b)
11687 {
11688 if (a->address != b->address)
11689 return 0;
11690
11691 if (a->shlib_disabled != b->shlib_disabled)
11692 return 0;
11693
11694 if (a->enabled != b->enabled)
11695 return 0;
11696
11697 a = a->next;
11698 b = b->next;
11699 }
11700
11701 if ((a == NULL) != (b == NULL))
11702 return 0;
11703
11704 return 1;
11705}
11706
f1310107
TJB
11707/* Create new breakpoint locations for B (a hardware or software breakpoint)
11708 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
11709 a ranged breakpoint. */
11710
0e30163f 11711void
0d381245 11712update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
11713 struct symtabs_and_lines sals,
11714 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
11715{
11716 int i;
0d381245
VP
11717 struct bp_location *existing_locations = b->loc;
11718
f8eba3c6
TT
11719 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
11720 {
11721 /* Ranged breakpoints have only one start location and one end
11722 location. */
11723 b->enable_state = bp_disabled;
11724 update_global_location_list (1);
11725 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
11726 "multiple locations found\n"),
11727 b->number);
11728 return;
11729 }
f1310107 11730
4a64f543
MS
11731 /* If there's no new locations, and all existing locations are
11732 pending, don't do anything. This optimizes the common case where
11733 all locations are in the same shared library, that was unloaded.
11734 We'd like to retain the location, so that when the library is
11735 loaded again, we don't loose the enabled/disabled status of the
11736 individual locations. */
0d381245 11737 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
11738 return;
11739
fe3f5fa8
VP
11740 b->loc = NULL;
11741
0d381245 11742 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 11743 {
f8eba3c6
TT
11744 struct bp_location *new_loc;
11745
11746 switch_to_program_space_and_thread (sals.sals[i].pspace);
11747
11748 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 11749
0d381245
VP
11750 /* Reparse conditions, they might contain references to the
11751 old symtab. */
11752 if (b->cond_string != NULL)
11753 {
f1310107 11754 char *s;
0d381245 11755 struct gdb_exception e;
fe3f5fa8 11756
0d381245
VP
11757 s = b->cond_string;
11758 TRY_CATCH (e, RETURN_MASK_ERROR)
11759 {
11760 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
11761 0);
11762 }
11763 if (e.reason < 0)
11764 {
3e43a32a
MS
11765 warning (_("failed to reevaluate condition "
11766 "for breakpoint %d: %s"),
0d381245
VP
11767 b->number, e.message);
11768 new_loc->enabled = 0;
11769 }
11770 }
fe3f5fa8 11771
f1310107
TJB
11772 if (sals_end.nelts)
11773 {
11774 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11775
11776 new_loc->length = end - sals.sals[0].pc + 1;
11777 }
0d381245 11778 }
fe3f5fa8 11779
514f746b
AR
11780 /* Update locations of permanent breakpoints. */
11781 if (b->enable_state == bp_permanent)
11782 make_breakpoint_permanent (b);
11783
4a64f543
MS
11784 /* If possible, carry over 'disable' status from existing
11785 breakpoints. */
0d381245
VP
11786 {
11787 struct bp_location *e = existing_locations;
776592bf
DE
11788 /* If there are multiple breakpoints with the same function name,
11789 e.g. for inline functions, comparing function names won't work.
11790 Instead compare pc addresses; this is just a heuristic as things
11791 may have moved, but in practice it gives the correct answer
11792 often enough until a better solution is found. */
11793 int have_ambiguous_names = ambiguous_names_p (b->loc);
11794
0d381245
VP
11795 for (; e; e = e->next)
11796 {
11797 if (!e->enabled && e->function_name)
11798 {
11799 struct bp_location *l = b->loc;
776592bf
DE
11800 if (have_ambiguous_names)
11801 {
11802 for (; l; l = l->next)
f1310107 11803 if (breakpoint_locations_match (e, l))
776592bf
DE
11804 {
11805 l->enabled = 0;
11806 break;
11807 }
11808 }
11809 else
11810 {
11811 for (; l; l = l->next)
11812 if (l->function_name
11813 && strcmp (e->function_name, l->function_name) == 0)
11814 {
11815 l->enabled = 0;
11816 break;
11817 }
11818 }
0d381245
VP
11819 }
11820 }
11821 }
fe3f5fa8 11822
8d3788bd
VP
11823 if (!locations_are_equal (existing_locations, b->loc))
11824 observer_notify_breakpoint_modified (b);
11825
b60e7edf 11826 update_global_location_list (1);
fe3f5fa8
VP
11827}
11828
ef23e705
TJB
11829/* Find the SaL locations corresponding to the given ADDR_STRING.
11830 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
11831
11832static struct symtabs_and_lines
11833addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11834{
11835 char *s;
58438ac1 11836 int marker_spec;
02d20e4a 11837 struct symtabs_and_lines sals = {0};
f8eba3c6 11838 volatile struct gdb_exception e;
ef23e705
TJB
11839
11840 s = addr_string;
11841 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11842
11843 TRY_CATCH (e, RETURN_MASK_ERROR)
11844 {
11845 if (marker_spec)
11846 {
d9b3f62e
PA
11847 struct tracepoint *tp = (struct tracepoint *) b;
11848
ef23e705 11849 sals = decode_static_tracepoint_spec (&s);
d9b3f62e 11850 if (sals.nelts > tp->static_trace_marker_id_idx)
ef23e705 11851 {
d9b3f62e 11852 sals.sals[0] = sals.sals[tp->static_trace_marker_id_idx];
ef23e705
TJB
11853 sals.nelts = 1;
11854 }
11855 else
d9b3f62e 11856 error (_("marker %s not found"), tp->static_trace_marker_id);
ef23e705
TJB
11857 }
11858 else
f8eba3c6
TT
11859 {
11860 struct linespec_result canonical;
11861
11862 init_linespec_result (&canonical);
11863 decode_line_full (&s, DECODE_LINE_FUNFIRSTLINE,
11864 (struct symtab *) NULL, 0,
11865 &canonical, multiple_symbols_all,
11866 b->filter);
11867
11868 /* We should get 0 or 1 resulting SALs. */
11869 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
11870
11871 if (VEC_length (linespec_sals, canonical.sals) > 0)
11872 {
11873 struct linespec_sals *lsal;
11874
11875 lsal = VEC_index (linespec_sals, canonical.sals, 0);
11876 sals = lsal->sals;
11877 /* Arrange it so the destructor does not free the
11878 contents. */
11879 lsal->sals.sals = NULL;
11880 }
11881
11882 destroy_linespec_result (&canonical);
11883 }
ef23e705
TJB
11884 }
11885 if (e.reason < 0)
11886 {
11887 int not_found_and_ok = 0;
11888 /* For pending breakpoints, it's expected that parsing will
11889 fail until the right shared library is loaded. User has
11890 already told to create pending breakpoints and don't need
11891 extra messages. If breakpoint is in bp_shlib_disabled
11892 state, then user already saw the message about that
11893 breakpoint being disabled, and don't want to see more
11894 errors. */
58438ac1 11895 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
11896 && (b->condition_not_parsed
11897 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 11898 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
11899 || b->enable_state == bp_disabled))
11900 not_found_and_ok = 1;
11901
11902 if (!not_found_and_ok)
11903 {
11904 /* We surely don't want to warn about the same breakpoint
11905 10 times. One solution, implemented here, is disable
11906 the breakpoint on error. Another solution would be to
11907 have separate 'warning emitted' flag. Since this
11908 happens only when a binary has changed, I don't know
11909 which approach is better. */
11910 b->enable_state = bp_disabled;
11911 throw_exception (e);
11912 }
11913 }
11914
58438ac1 11915 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 11916 {
f8eba3c6 11917 int i;
ef23e705 11918
f8eba3c6
TT
11919 for (i = 0; i < sals.nelts; ++i)
11920 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
11921 if (b->condition_not_parsed && s && s[0])
11922 {
11923 char *cond_string = 0;
11924 int thread = -1;
11925 int task = 0;
11926
11927 find_condition_and_thread (s, sals.sals[0].pc,
11928 &cond_string, &thread, &task);
11929 if (cond_string)
11930 b->cond_string = cond_string;
11931 b->thread = thread;
11932 b->task = task;
11933 b->condition_not_parsed = 0;
11934 }
11935
11936 if (b->type == bp_static_tracepoint && !marker_spec)
11937 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 11938
58438ac1
TT
11939 *found = 1;
11940 }
11941 else
11942 *found = 0;
ef23e705
TJB
11943
11944 return sals;
11945}
11946
348d480f
PA
11947/* The default re_set method, for typical hardware or software
11948 breakpoints. Reevaluate the breakpoint and recreate its
11949 locations. */
11950
11951static void
28010a5d 11952breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
11953{
11954 int found;
f1310107 11955 struct symtabs_and_lines sals, sals_end;
ef23e705 11956 struct symtabs_and_lines expanded = {0};
f1310107 11957 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
11958
11959 sals = addr_string_to_sals (b, b->addr_string, &found);
11960 if (found)
11961 {
11962 make_cleanup (xfree, sals.sals);
f8eba3c6 11963 expanded = sals;
ef23e705
TJB
11964 }
11965
f1310107
TJB
11966 if (b->addr_string_range_end)
11967 {
11968 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11969 if (found)
11970 {
11971 make_cleanup (xfree, sals_end.sals);
f8eba3c6 11972 expanded_end = sals_end;
f1310107
TJB
11973 }
11974 }
11975
11976 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
11977}
11978
11979/* Prepare the global context for a re-set of breakpoint B. */
11980
11981static struct cleanup *
11982prepare_re_set_context (struct breakpoint *b)
11983{
11984 struct cleanup *cleanups;
11985
11986 input_radix = b->input_radix;
11987 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
11988 if (b->pspace != NULL)
11989 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
11990 set_language (b->language);
11991
11992 return cleanups;
ef23e705
TJB
11993}
11994
c906108c
SS
11995/* Reset a breakpoint given it's struct breakpoint * BINT.
11996 The value we return ends up being the return value from catch_errors.
11997 Unused in this case. */
11998
11999static int
4efb68b1 12000breakpoint_re_set_one (void *bint)
c906108c 12001{
4a64f543 12002 /* Get past catch_errs. */
53a5351d 12003 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 12004 struct cleanup *cleanups;
c906108c 12005
348d480f
PA
12006 cleanups = prepare_re_set_context (b);
12007 b->ops->re_set (b);
12008 do_cleanups (cleanups);
c906108c
SS
12009 return 0;
12010}
12011
69de3c6a 12012/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 12013void
69de3c6a 12014breakpoint_re_set (void)
c906108c 12015{
35df4500 12016 struct breakpoint *b, *b_tmp;
c906108c
SS
12017 enum language save_language;
12018 int save_input_radix;
6c95b8df 12019 struct cleanup *old_chain;
c5aa993b 12020
c906108c
SS
12021 save_language = current_language->la_language;
12022 save_input_radix = input_radix;
6c95b8df
PA
12023 old_chain = save_current_program_space ();
12024
35df4500 12025 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12026 {
4a64f543 12027 /* Format possible error msg. */
fe3f5fa8 12028 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
12029 b->number);
12030 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 12031 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 12032 do_cleanups (cleanups);
c5aa993b 12033 }
c906108c
SS
12034 set_language (save_language);
12035 input_radix = save_input_radix;
e62c965a 12036
0756c555 12037 jit_breakpoint_re_set ();
4efc6507 12038
6c95b8df
PA
12039 do_cleanups (old_chain);
12040
af02033e
PP
12041 create_overlay_event_breakpoint ();
12042 create_longjmp_master_breakpoint ();
12043 create_std_terminate_master_breakpoint ();
186c406b 12044 create_exception_master_breakpoint ();
1bfeeb0f
JL
12045
12046 /* While we're at it, reset the skip list too. */
12047 skip_re_set ();
c906108c
SS
12048}
12049\f
c906108c
SS
12050/* Reset the thread number of this breakpoint:
12051
12052 - If the breakpoint is for all threads, leave it as-is.
4a64f543 12053 - Else, reset it to the current thread for inferior_ptid. */
c906108c 12054void
fba45db2 12055breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
12056{
12057 if (b->thread != -1)
12058 {
39f77062
KB
12059 if (in_thread_list (inferior_ptid))
12060 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
12061
12062 /* We're being called after following a fork. The new fork is
12063 selected as current, and unless this was a vfork will have a
12064 different program space from the original thread. Reset that
12065 as well. */
12066 b->loc->pspace = current_program_space;
c906108c
SS
12067 }
12068}
12069
03ac34d5
MS
12070/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12071 If from_tty is nonzero, it prints a message to that effect,
12072 which ends with a period (no newline). */
12073
c906108c 12074void
fba45db2 12075set_ignore_count (int bptnum, int count, int from_tty)
c906108c 12076{
52f0bd74 12077 struct breakpoint *b;
c906108c
SS
12078
12079 if (count < 0)
12080 count = 0;
12081
12082 ALL_BREAKPOINTS (b)
12083 if (b->number == bptnum)
c5aa993b 12084 {
d77f58be
SS
12085 if (is_tracepoint (b))
12086 {
12087 if (from_tty && count != 0)
12088 printf_filtered (_("Ignore count ignored for tracepoint %d."),
12089 bptnum);
12090 return;
12091 }
12092
c5aa993b 12093 b->ignore_count = count;
221ea385
KS
12094 if (from_tty)
12095 {
12096 if (count == 0)
3e43a32a
MS
12097 printf_filtered (_("Will stop next time "
12098 "breakpoint %d is reached."),
221ea385
KS
12099 bptnum);
12100 else if (count == 1)
a3f17187 12101 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
12102 bptnum);
12103 else
3e43a32a
MS
12104 printf_filtered (_("Will ignore next %d "
12105 "crossings of breakpoint %d."),
221ea385
KS
12106 count, bptnum);
12107 }
c5aa993b 12108 breakpoints_changed ();
8d3788bd 12109 observer_notify_breakpoint_modified (b);
c5aa993b
JM
12110 return;
12111 }
c906108c 12112
8a3fe4f8 12113 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
12114}
12115
c906108c
SS
12116/* Command to set ignore-count of breakpoint N to COUNT. */
12117
12118static void
fba45db2 12119ignore_command (char *args, int from_tty)
c906108c
SS
12120{
12121 char *p = args;
52f0bd74 12122 int num;
c906108c
SS
12123
12124 if (p == 0)
e2e0b3e5 12125 error_no_arg (_("a breakpoint number"));
c5aa993b 12126
c906108c 12127 num = get_number (&p);
5c44784c 12128 if (num == 0)
8a3fe4f8 12129 error (_("bad breakpoint number: '%s'"), args);
c906108c 12130 if (*p == 0)
8a3fe4f8 12131 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
12132
12133 set_ignore_count (num,
12134 longest_to_int (value_as_long (parse_and_eval (p))),
12135 from_tty);
221ea385
KS
12136 if (from_tty)
12137 printf_filtered ("\n");
c906108c
SS
12138}
12139\f
12140/* Call FUNCTION on each of the breakpoints
12141 whose numbers are given in ARGS. */
12142
12143static void
95a42b64
TT
12144map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
12145 void *),
12146 void *data)
c906108c 12147{
52f0bd74
AC
12148 int num;
12149 struct breakpoint *b, *tmp;
11cf8741 12150 int match;
197f0a60 12151 struct get_number_or_range_state state;
c906108c 12152
197f0a60 12153 if (args == 0)
e2e0b3e5 12154 error_no_arg (_("one or more breakpoint numbers"));
c906108c 12155
197f0a60
TT
12156 init_number_or_range (&state, args);
12157
12158 while (!state.finished)
c906108c 12159 {
197f0a60
TT
12160 char *p = state.string;
12161
11cf8741 12162 match = 0;
c5aa993b 12163
197f0a60 12164 num = get_number_or_range (&state);
5c44784c 12165 if (num == 0)
c5aa993b 12166 {
8a3fe4f8 12167 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
12168 }
12169 else
12170 {
12171 ALL_BREAKPOINTS_SAFE (b, tmp)
12172 if (b->number == num)
12173 {
11cf8741 12174 match = 1;
cdac0397 12175 function (b, data);
11cf8741 12176 break;
5c44784c 12177 }
11cf8741 12178 if (match == 0)
a3f17187 12179 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 12180 }
c906108c
SS
12181 }
12182}
12183
0d381245
VP
12184static struct bp_location *
12185find_location_by_number (char *number)
12186{
12187 char *dot = strchr (number, '.');
12188 char *p1;
12189 int bp_num;
12190 int loc_num;
12191 struct breakpoint *b;
12192 struct bp_location *loc;
12193
12194 *dot = '\0';
12195
12196 p1 = number;
197f0a60 12197 bp_num = get_number (&p1);
0d381245
VP
12198 if (bp_num == 0)
12199 error (_("Bad breakpoint number '%s'"), number);
12200
12201 ALL_BREAKPOINTS (b)
12202 if (b->number == bp_num)
12203 {
12204 break;
12205 }
12206
12207 if (!b || b->number != bp_num)
12208 error (_("Bad breakpoint number '%s'"), number);
12209
12210 p1 = dot+1;
197f0a60 12211 loc_num = get_number (&p1);
0d381245
VP
12212 if (loc_num == 0)
12213 error (_("Bad breakpoint location number '%s'"), number);
12214
12215 --loc_num;
12216 loc = b->loc;
12217 for (;loc_num && loc; --loc_num, loc = loc->next)
12218 ;
12219 if (!loc)
12220 error (_("Bad breakpoint location number '%s'"), dot+1);
12221
12222 return loc;
12223}
12224
12225
1900040c
MS
12226/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12227 If from_tty is nonzero, it prints a message to that effect,
12228 which ends with a period (no newline). */
12229
c906108c 12230void
fba45db2 12231disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
12232{
12233 /* Never disable a watchpoint scope breakpoint; we want to
12234 hit them when we leave scope so we can delete both the
12235 watchpoint and its scope breakpoint at that time. */
12236 if (bpt->type == bp_watchpoint_scope)
12237 return;
12238
c2c6d25f 12239 /* You can't disable permanent breakpoints. */
b5de0fa7 12240 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
12241 return;
12242
b5de0fa7 12243 bpt->enable_state = bp_disabled;
c906108c 12244
d248b706
KY
12245 if (target_supports_enable_disable_tracepoint ()
12246 && current_trace_status ()->running && is_tracepoint (bpt))
12247 {
12248 struct bp_location *location;
12249
12250 for (location = bpt->loc; location; location = location->next)
12251 target_disable_tracepoint (location);
12252 }
12253
b60e7edf 12254 update_global_location_list (0);
c906108c 12255
8d3788bd 12256 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12257}
12258
51be5b68
PA
12259/* A callback for iterate_over_related_breakpoints. */
12260
12261static void
12262do_disable_breakpoint (struct breakpoint *b, void *ignore)
12263{
12264 disable_breakpoint (b);
12265}
12266
95a42b64
TT
12267/* A callback for map_breakpoint_numbers that calls
12268 disable_breakpoint. */
12269
12270static void
12271do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
12272{
51be5b68 12273 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
12274}
12275
c906108c 12276static void
fba45db2 12277disable_command (char *args, int from_tty)
c906108c 12278{
c906108c 12279 if (args == 0)
46c6471b
PA
12280 {
12281 struct breakpoint *bpt;
12282
12283 ALL_BREAKPOINTS (bpt)
12284 if (user_breakpoint_p (bpt))
12285 disable_breakpoint (bpt);
12286 }
0d381245
VP
12287 else if (strchr (args, '.'))
12288 {
12289 struct bp_location *loc = find_location_by_number (args);
12290 if (loc)
d248b706
KY
12291 {
12292 loc->enabled = 0;
12293 if (target_supports_enable_disable_tracepoint ()
12294 && current_trace_status ()->running && loc->owner
12295 && is_tracepoint (loc->owner))
12296 target_disable_tracepoint (loc);
12297 }
b60e7edf 12298 update_global_location_list (0);
0d381245 12299 }
c906108c 12300 else
95a42b64 12301 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
12302}
12303
12304static void
51be5b68 12305enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 12306{
afe38095 12307 int target_resources_ok;
c906108c
SS
12308
12309 if (bpt->type == bp_hardware_breakpoint)
12310 {
12311 int i;
c5aa993b 12312 i = hw_breakpoint_used_count ();
53a5351d 12313 target_resources_ok =
d92524f1 12314 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 12315 i + 1, 0);
c906108c 12316 if (target_resources_ok == 0)
8a3fe4f8 12317 error (_("No hardware breakpoint support in the target."));
c906108c 12318 else if (target_resources_ok < 0)
8a3fe4f8 12319 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
12320 }
12321
cc60f2e3 12322 if (is_watchpoint (bpt))
c906108c 12323 {
d07205c2
JK
12324 /* Initialize it just to avoid a GCC false warning. */
12325 enum enable_state orig_enable_state = 0;
dde02812
ES
12326 struct gdb_exception e;
12327
12328 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 12329 {
3a5c3e22
PA
12330 struct watchpoint *w = (struct watchpoint *) bpt;
12331
1e718ff1
TJB
12332 orig_enable_state = bpt->enable_state;
12333 bpt->enable_state = bp_enabled;
3a5c3e22 12334 update_watchpoint (w, 1 /* reparse */);
c906108c 12335 }
dde02812 12336 if (e.reason < 0)
c5aa993b 12337 {
1e718ff1 12338 bpt->enable_state = orig_enable_state;
dde02812
ES
12339 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12340 bpt->number);
12341 return;
c5aa993b 12342 }
c906108c 12343 }
0101ce28 12344
b4c291bb
KH
12345 if (bpt->enable_state != bp_permanent)
12346 bpt->enable_state = bp_enabled;
d248b706
KY
12347
12348 if (target_supports_enable_disable_tracepoint ()
12349 && current_trace_status ()->running && is_tracepoint (bpt))
12350 {
12351 struct bp_location *location;
12352
12353 for (location = bpt->loc; location; location = location->next)
12354 target_enable_tracepoint (location);
12355 }
12356
b4c291bb 12357 bpt->disposition = disposition;
b60e7edf 12358 update_global_location_list (1);
b4c291bb
KH
12359 breakpoints_changed ();
12360
8d3788bd 12361 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12362}
12363
fe3f5fa8 12364
c906108c 12365void
fba45db2 12366enable_breakpoint (struct breakpoint *bpt)
c906108c 12367{
51be5b68
PA
12368 enable_breakpoint_disp (bpt, bpt->disposition);
12369}
12370
12371static void
12372do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12373{
12374 enable_breakpoint (bpt);
c906108c
SS
12375}
12376
95a42b64
TT
12377/* A callback for map_breakpoint_numbers that calls
12378 enable_breakpoint. */
12379
12380static void
12381do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12382{
51be5b68 12383 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
12384}
12385
c906108c
SS
12386/* The enable command enables the specified breakpoints (or all defined
12387 breakpoints) so they once again become (or continue to be) effective
1272ad14 12388 in stopping the inferior. */
c906108c 12389
c906108c 12390static void
fba45db2 12391enable_command (char *args, int from_tty)
c906108c 12392{
c906108c 12393 if (args == 0)
46c6471b
PA
12394 {
12395 struct breakpoint *bpt;
12396
12397 ALL_BREAKPOINTS (bpt)
12398 if (user_breakpoint_p (bpt))
12399 enable_breakpoint (bpt);
12400 }
0d381245
VP
12401 else if (strchr (args, '.'))
12402 {
12403 struct bp_location *loc = find_location_by_number (args);
12404 if (loc)
d248b706
KY
12405 {
12406 loc->enabled = 1;
12407 if (target_supports_enable_disable_tracepoint ()
12408 && current_trace_status ()->running && loc->owner
12409 && is_tracepoint (loc->owner))
12410 target_enable_tracepoint (loc);
12411 }
b60e7edf 12412 update_global_location_list (1);
0d381245 12413 }
c906108c 12414 else
95a42b64 12415 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
12416}
12417
12418static void
51be5b68
PA
12419do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12420{
12421 enum bpdisp disp = *(enum bpdisp *) arg;
12422
12423 enable_breakpoint_disp (bpt, disp);
12424}
12425
12426static void
12427do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12428{
51be5b68
PA
12429 enum bpdisp disp = disp_disable;
12430
12431 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12432}
12433
c906108c 12434static void
fba45db2 12435enable_once_command (char *args, int from_tty)
c906108c 12436{
51be5b68 12437 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
12438}
12439
12440static void
51be5b68 12441do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12442{
51be5b68
PA
12443 enum bpdisp disp = disp_del;
12444
12445 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12446}
12447
c906108c 12448static void
fba45db2 12449enable_delete_command (char *args, int from_tty)
c906108c 12450{
51be5b68 12451 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
12452}
12453\f
fa8d40ab
JJ
12454static void
12455set_breakpoint_cmd (char *args, int from_tty)
12456{
12457}
12458
12459static void
12460show_breakpoint_cmd (char *args, int from_tty)
12461{
12462}
12463
1f3b5d1b
PP
12464/* Invalidate last known value of any hardware watchpoint if
12465 the memory which that value represents has been written to by
12466 GDB itself. */
12467
12468static void
12469invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12470 const bfd_byte *data)
12471{
12472 struct breakpoint *bp;
12473
12474 ALL_BREAKPOINTS (bp)
12475 if (bp->enable_state == bp_enabled
3a5c3e22 12476 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 12477 {
3a5c3e22 12478 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 12479
3a5c3e22
PA
12480 if (wp->val_valid && wp->val)
12481 {
12482 struct bp_location *loc;
12483
12484 for (loc = bp->loc; loc != NULL; loc = loc->next)
12485 if (loc->loc_type == bp_loc_hardware_watchpoint
12486 && loc->address + loc->length > addr
12487 && addr + len > loc->address)
12488 {
12489 value_free (wp->val);
12490 wp->val = NULL;
12491 wp->val_valid = 0;
12492 }
12493 }
1f3b5d1b
PP
12494 }
12495}
12496
1bfeeb0f
JL
12497/* Use the last displayed codepoint's values, or nothing
12498 if they aren't valid. */
c906108c
SS
12499
12500struct symtabs_and_lines
f8eba3c6 12501decode_line_spec_1 (char *string, int flags)
c906108c
SS
12502{
12503 struct symtabs_and_lines sals;
cc59ec59 12504
c906108c 12505 if (string == 0)
8a3fe4f8 12506 error (_("Empty line specification."));
1bfeeb0f 12507 if (last_displayed_sal_is_valid ())
f8eba3c6 12508 sals = decode_line_1 (&string, flags,
1bfeeb0f 12509 get_last_displayed_symtab (),
f8eba3c6 12510 get_last_displayed_line ());
c906108c 12511 else
f8eba3c6 12512 sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
c906108c 12513 if (*string)
8a3fe4f8 12514 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
12515 return sals;
12516}
8181d85f
DJ
12517
12518/* Create and insert a raw software breakpoint at PC. Return an
12519 identifier, which should be used to remove the breakpoint later.
12520 In general, places which call this should be using something on the
12521 breakpoint chain instead; this function should be eliminated
12522 someday. */
12523
12524void *
6c95b8df
PA
12525deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12526 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
12527{
12528 struct bp_target_info *bp_tgt;
12529
6c95b8df 12530 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 12531
6c95b8df 12532 bp_tgt->placed_address_space = aspace;
8181d85f 12533 bp_tgt->placed_address = pc;
6c95b8df 12534
a6d9a66e 12535 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
12536 {
12537 /* Could not insert the breakpoint. */
12538 xfree (bp_tgt);
12539 return NULL;
12540 }
12541
12542 return bp_tgt;
12543}
12544
4a64f543
MS
12545/* Remove a breakpoint BP inserted by
12546 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
12547
12548int
a6d9a66e 12549deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
12550{
12551 struct bp_target_info *bp_tgt = bp;
12552 int ret;
12553
a6d9a66e 12554 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
12555 xfree (bp_tgt);
12556
12557 return ret;
12558}
12559
4a64f543
MS
12560/* One (or perhaps two) breakpoints used for software single
12561 stepping. */
8181d85f
DJ
12562
12563static void *single_step_breakpoints[2];
a6d9a66e 12564static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
12565
12566/* Create and insert a breakpoint for software single step. */
12567
12568void
6c95b8df 12569insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
12570 struct address_space *aspace,
12571 CORE_ADDR next_pc)
8181d85f
DJ
12572{
12573 void **bpt_p;
12574
12575 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
12576 {
12577 bpt_p = &single_step_breakpoints[0];
12578 single_step_gdbarch[0] = gdbarch;
12579 }
8181d85f
DJ
12580 else
12581 {
12582 gdb_assert (single_step_breakpoints[1] == NULL);
12583 bpt_p = &single_step_breakpoints[1];
a6d9a66e 12584 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
12585 }
12586
4a64f543
MS
12587 /* NOTE drow/2006-04-11: A future improvement to this function would
12588 be to only create the breakpoints once, and actually put them on
12589 the breakpoint chain. That would let us use set_raw_breakpoint.
12590 We could adjust the addresses each time they were needed. Doing
12591 this requires corresponding changes elsewhere where single step
12592 breakpoints are handled, however. So, for now, we use this. */
8181d85f 12593
6c95b8df 12594 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 12595 if (*bpt_p == NULL)
5af949e3
UW
12596 error (_("Could not insert single-step breakpoint at %s"),
12597 paddress (gdbarch, next_pc));
8181d85f
DJ
12598}
12599
f02253f1
HZ
12600/* Check if the breakpoints used for software single stepping
12601 were inserted or not. */
12602
12603int
12604single_step_breakpoints_inserted (void)
12605{
12606 return (single_step_breakpoints[0] != NULL
12607 || single_step_breakpoints[1] != NULL);
12608}
12609
8181d85f
DJ
12610/* Remove and delete any breakpoints used for software single step. */
12611
12612void
12613remove_single_step_breakpoints (void)
12614{
12615 gdb_assert (single_step_breakpoints[0] != NULL);
12616
12617 /* See insert_single_step_breakpoint for more about this deprecated
12618 call. */
a6d9a66e
UW
12619 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12620 single_step_breakpoints[0]);
12621 single_step_gdbarch[0] = NULL;
8181d85f
DJ
12622 single_step_breakpoints[0] = NULL;
12623
12624 if (single_step_breakpoints[1] != NULL)
12625 {
a6d9a66e
UW
12626 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12627 single_step_breakpoints[1]);
12628 single_step_gdbarch[1] = NULL;
8181d85f
DJ
12629 single_step_breakpoints[1] = NULL;
12630 }
12631}
12632
d03285ec
UW
12633/* Delete software single step breakpoints without removing them from
12634 the inferior. This is intended to be used if the inferior's address
12635 space where they were inserted is already gone, e.g. after exit or
12636 exec. */
12637
12638void
12639cancel_single_step_breakpoints (void)
12640{
12641 int i;
12642
12643 for (i = 0; i < 2; i++)
12644 if (single_step_breakpoints[i])
12645 {
12646 xfree (single_step_breakpoints[i]);
12647 single_step_breakpoints[i] = NULL;
12648 single_step_gdbarch[i] = NULL;
12649 }
12650}
12651
12652/* Detach software single-step breakpoints from INFERIOR_PTID without
12653 removing them. */
12654
12655static void
12656detach_single_step_breakpoints (void)
12657{
12658 int i;
12659
12660 for (i = 0; i < 2; i++)
12661 if (single_step_breakpoints[i])
12662 target_remove_breakpoint (single_step_gdbarch[i],
12663 single_step_breakpoints[i]);
12664}
12665
4a64f543
MS
12666/* Check whether a software single-step breakpoint is inserted at
12667 PC. */
1aafd4da
UW
12668
12669static int
cc59ec59
MS
12670single_step_breakpoint_inserted_here_p (struct address_space *aspace,
12671 CORE_ADDR pc)
1aafd4da
UW
12672{
12673 int i;
12674
12675 for (i = 0; i < 2; i++)
12676 {
12677 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
12678 if (bp_tgt
12679 && breakpoint_address_match (bp_tgt->placed_address_space,
12680 bp_tgt->placed_address,
12681 aspace, pc))
1aafd4da
UW
12682 return 1;
12683 }
12684
12685 return 0;
12686}
12687
a96d9b2e
SDJ
12688/* Returns 0 if 'bp' is NOT a syscall catchpoint,
12689 non-zero otherwise. */
12690static int
12691is_syscall_catchpoint_enabled (struct breakpoint *bp)
12692{
12693 if (syscall_catchpoint_p (bp)
12694 && bp->enable_state != bp_disabled
12695 && bp->enable_state != bp_call_disabled)
12696 return 1;
12697 else
12698 return 0;
12699}
12700
12701int
12702catch_syscall_enabled (void)
12703{
12704 struct inferior *inf = current_inferior ();
12705
12706 return inf->total_syscalls_count != 0;
12707}
12708
12709int
12710catching_syscall_number (int syscall_number)
12711{
12712 struct breakpoint *bp;
12713
12714 ALL_BREAKPOINTS (bp)
12715 if (is_syscall_catchpoint_enabled (bp))
12716 {
be5c67c1
PA
12717 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12718
12719 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
12720 {
12721 int i, iter;
12722 for (i = 0;
be5c67c1 12723 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
12724 i++)
12725 if (syscall_number == iter)
12726 return 1;
12727 }
12728 else
12729 return 1;
12730 }
12731
12732 return 0;
12733}
12734
12735/* Complete syscall names. Used by "catch syscall". */
12736static char **
12737catch_syscall_completer (struct cmd_list_element *cmd,
12738 char *text, char *word)
12739{
12740 const char **list = get_syscall_names ();
c38eea1a
MS
12741 char **retlist
12742 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 12743
c38eea1a
MS
12744 xfree (list);
12745 return retlist;
a96d9b2e
SDJ
12746}
12747
1042e4c0
SS
12748/* Tracepoint-specific operations. */
12749
12750/* Set tracepoint count to NUM. */
12751static void
12752set_tracepoint_count (int num)
12753{
12754 tracepoint_count = num;
4fa62494 12755 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
12756}
12757
12758void
12759trace_command (char *arg, int from_tty)
12760{
8cdf0e15
VP
12761 if (create_breakpoint (get_current_arch (),
12762 arg,
12763 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12764 0 /* tempflag */,
12765 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
12766 0 /* Ignore count */,
12767 pending_break_support,
348d480f 12768 &tracepoint_breakpoint_ops,
8cdf0e15 12769 from_tty,
84f4c1fe
PM
12770 1 /* enabled */,
12771 0 /* internal */))
fd9b8c24 12772 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
12773}
12774
7a697b8d
SS
12775void
12776ftrace_command (char *arg, int from_tty)
12777{
8cdf0e15
VP
12778 if (create_breakpoint (get_current_arch (),
12779 arg,
12780 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12781 0 /* tempflag */,
12782 bp_fast_tracepoint /* type_wanted */,
12783 0 /* Ignore count */,
12784 pending_break_support,
348d480f 12785 &tracepoint_breakpoint_ops,
0fb4aa4b 12786 from_tty,
84f4c1fe
PM
12787 1 /* enabled */,
12788 0 /* internal */))
0fb4aa4b
PA
12789 set_tracepoint_count (breakpoint_count);
12790}
12791
12792/* strace command implementation. Creates a static tracepoint. */
12793
12794void
12795strace_command (char *arg, int from_tty)
12796{
12797 if (create_breakpoint (get_current_arch (),
12798 arg,
12799 NULL, 0, 1 /* parse arg */,
12800 0 /* tempflag */,
12801 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
12802 0 /* Ignore count */,
12803 pending_break_support,
348d480f 12804 &tracepoint_breakpoint_ops,
8cdf0e15 12805 from_tty,
84f4c1fe
PM
12806 1 /* enabled */,
12807 0 /* internal */))
fd9b8c24 12808 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
12809}
12810
409873ef
SS
12811/* Set up a fake reader function that gets command lines from a linked
12812 list that was acquired during tracepoint uploading. */
12813
12814static struct uploaded_tp *this_utp;
3149d8c1 12815static int next_cmd;
409873ef
SS
12816
12817static char *
12818read_uploaded_action (void)
12819{
12820 char *rslt;
12821
3149d8c1 12822 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 12823
3149d8c1 12824 next_cmd++;
409873ef
SS
12825
12826 return rslt;
12827}
12828
00bf0b85
SS
12829/* Given information about a tracepoint as recorded on a target (which
12830 can be either a live system or a trace file), attempt to create an
12831 equivalent GDB tracepoint. This is not a reliable process, since
12832 the target does not necessarily have all the information used when
12833 the tracepoint was originally defined. */
12834
d9b3f62e 12835struct tracepoint *
00bf0b85 12836create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 12837{
409873ef 12838 char *addr_str, small_buf[100];
d9b3f62e 12839 struct tracepoint *tp;
fd9b8c24 12840
409873ef
SS
12841 if (utp->at_string)
12842 addr_str = utp->at_string;
12843 else
12844 {
12845 /* In the absence of a source location, fall back to raw
12846 address. Since there is no way to confirm that the address
12847 means the same thing as when the trace was started, warn the
12848 user. */
3e43a32a
MS
12849 warning (_("Uploaded tracepoint %d has no "
12850 "source location, using raw address"),
409873ef
SS
12851 utp->number);
12852 sprintf (small_buf, "*%s", hex_string (utp->addr));
12853 addr_str = small_buf;
12854 }
12855
12856 /* There's not much we can do with a sequence of bytecodes. */
12857 if (utp->cond && !utp->cond_string)
3e43a32a
MS
12858 warning (_("Uploaded tracepoint %d condition "
12859 "has no source form, ignoring it"),
409873ef 12860 utp->number);
d5551862 12861
8cdf0e15 12862 if (!create_breakpoint (get_current_arch (),
409873ef
SS
12863 addr_str,
12864 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 12865 0 /* tempflag */,
0fb4aa4b 12866 utp->type /* type_wanted */,
8cdf0e15
VP
12867 0 /* Ignore count */,
12868 pending_break_support,
348d480f 12869 &tracepoint_breakpoint_ops,
8cdf0e15 12870 0 /* from_tty */,
84f4c1fe
PM
12871 utp->enabled /* enabled */,
12872 0 /* internal */))
fd9b8c24
PA
12873 return NULL;
12874
00bf0b85
SS
12875 set_tracepoint_count (breakpoint_count);
12876
409873ef 12877 /* Get the tracepoint we just created. */
fd9b8c24
PA
12878 tp = get_tracepoint (tracepoint_count);
12879 gdb_assert (tp != NULL);
d5551862 12880
00bf0b85
SS
12881 if (utp->pass > 0)
12882 {
d9b3f62e 12883 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 12884
409873ef 12885 trace_pass_command (small_buf, 0);
00bf0b85
SS
12886 }
12887
409873ef
SS
12888 /* If we have uploaded versions of the original commands, set up a
12889 special-purpose "reader" function and call the usual command line
12890 reader, then pass the result to the breakpoint command-setting
12891 function. */
3149d8c1 12892 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 12893 {
409873ef 12894 struct command_line *cmd_list;
00bf0b85 12895
409873ef 12896 this_utp = utp;
3149d8c1 12897 next_cmd = 0;
d5551862 12898
409873ef
SS
12899 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12900
d9b3f62e 12901 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 12902 }
3149d8c1
SS
12903 else if (!VEC_empty (char_ptr, utp->actions)
12904 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12905 warning (_("Uploaded tracepoint %d actions "
12906 "have no source form, ignoring them"),
409873ef 12907 utp->number);
00bf0b85 12908
f196051f
SS
12909 /* Copy any status information that might be available. */
12910 tp->base.hit_count = utp->hit_count;
12911 tp->traceframe_usage = utp->traceframe_usage;
12912
00bf0b85 12913 return tp;
d9b3f62e 12914}
00bf0b85 12915
1042e4c0
SS
12916/* Print information on tracepoint number TPNUM_EXP, or all if
12917 omitted. */
12918
12919static void
e5a67952 12920tracepoints_info (char *args, int from_tty)
1042e4c0 12921{
79a45e25 12922 struct ui_out *uiout = current_uiout;
e5a67952 12923 int num_printed;
1042e4c0 12924
e5a67952 12925 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
12926
12927 if (num_printed == 0)
1042e4c0 12928 {
e5a67952 12929 if (args == NULL || *args == '\0')
d77f58be
SS
12930 ui_out_message (uiout, 0, "No tracepoints.\n");
12931 else
e5a67952 12932 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 12933 }
ad443146
SS
12934
12935 default_collect_info ();
1042e4c0
SS
12936}
12937
4a64f543 12938/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
12939 Not supported by all targets. */
12940static void
12941enable_trace_command (char *args, int from_tty)
12942{
12943 enable_command (args, from_tty);
12944}
12945
4a64f543 12946/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
12947 Not supported by all targets. */
12948static void
12949disable_trace_command (char *args, int from_tty)
12950{
12951 disable_command (args, from_tty);
12952}
12953
4a64f543 12954/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
12955static void
12956delete_trace_command (char *arg, int from_tty)
12957{
35df4500 12958 struct breakpoint *b, *b_tmp;
1042e4c0
SS
12959
12960 dont_repeat ();
12961
12962 if (arg == 0)
12963 {
12964 int breaks_to_delete = 0;
12965
12966 /* Delete all breakpoints if no argument.
12967 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
12968 have to be deleted with an explicit breakpoint number
12969 argument. */
1042e4c0 12970 ALL_TRACEPOINTS (b)
46c6471b 12971 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
12972 {
12973 breaks_to_delete = 1;
12974 break;
12975 }
1042e4c0
SS
12976
12977 /* Ask user only if there are some breakpoints to delete. */
12978 if (!from_tty
12979 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12980 {
35df4500 12981 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 12982 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 12983 delete_breakpoint (b);
1042e4c0
SS
12984 }
12985 }
12986 else
51be5b68 12987 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
12988}
12989
197f0a60
TT
12990/* Helper function for trace_pass_command. */
12991
12992static void
d9b3f62e 12993trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 12994{
d9b3f62e
PA
12995 tp->pass_count = count;
12996 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
12997 if (from_tty)
12998 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 12999 tp->base.number, count);
197f0a60
TT
13000}
13001
1042e4c0
SS
13002/* Set passcount for tracepoint.
13003
13004 First command argument is passcount, second is tracepoint number.
13005 If tracepoint number omitted, apply to most recently defined.
13006 Also accepts special argument "all". */
13007
13008static void
13009trace_pass_command (char *args, int from_tty)
13010{
d9b3f62e 13011 struct tracepoint *t1;
1042e4c0 13012 unsigned int count;
1042e4c0
SS
13013
13014 if (args == 0 || *args == 0)
3e43a32a
MS
13015 error (_("passcount command requires an "
13016 "argument (count + optional TP num)"));
1042e4c0 13017
4a64f543 13018 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
13019
13020 while (*args && isspace ((int) *args))
13021 args++;
13022
13023 if (*args && strncasecmp (args, "all", 3) == 0)
13024 {
d9b3f62e
PA
13025 struct breakpoint *b;
13026
1042e4c0 13027 args += 3; /* Skip special argument "all". */
1042e4c0
SS
13028 if (*args)
13029 error (_("Junk at end of arguments."));
1042e4c0 13030
d9b3f62e 13031 ALL_TRACEPOINTS (b)
197f0a60 13032 {
d9b3f62e 13033 t1 = (struct tracepoint *) b;
197f0a60
TT
13034 trace_pass_set_count (t1, count, from_tty);
13035 }
13036 }
13037 else if (*args == '\0')
1042e4c0 13038 {
197f0a60 13039 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 13040 if (t1)
197f0a60
TT
13041 trace_pass_set_count (t1, count, from_tty);
13042 }
13043 else
13044 {
13045 struct get_number_or_range_state state;
13046
13047 init_number_or_range (&state, args);
13048 while (!state.finished)
1042e4c0 13049 {
197f0a60
TT
13050 t1 = get_tracepoint_by_number (&args, &state, 1);
13051 if (t1)
13052 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
13053 }
13054 }
1042e4c0
SS
13055}
13056
d9b3f62e 13057struct tracepoint *
1042e4c0
SS
13058get_tracepoint (int num)
13059{
13060 struct breakpoint *t;
13061
13062 ALL_TRACEPOINTS (t)
13063 if (t->number == num)
d9b3f62e 13064 return (struct tracepoint *) t;
1042e4c0
SS
13065
13066 return NULL;
13067}
13068
d5551862
SS
13069/* Find the tracepoint with the given target-side number (which may be
13070 different from the tracepoint number after disconnecting and
13071 reconnecting). */
13072
d9b3f62e 13073struct tracepoint *
d5551862
SS
13074get_tracepoint_by_number_on_target (int num)
13075{
d9b3f62e 13076 struct breakpoint *b;
d5551862 13077
d9b3f62e
PA
13078 ALL_TRACEPOINTS (b)
13079 {
13080 struct tracepoint *t = (struct tracepoint *) b;
13081
13082 if (t->number_on_target == num)
13083 return t;
13084 }
d5551862
SS
13085
13086 return NULL;
13087}
13088
1042e4c0 13089/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
13090 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13091 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 13092 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 13093struct tracepoint *
197f0a60
TT
13094get_tracepoint_by_number (char **arg,
13095 struct get_number_or_range_state *state,
13096 int optional_p)
1042e4c0
SS
13097{
13098 extern int tracepoint_count;
13099 struct breakpoint *t;
13100 int tpnum;
13101 char *instring = arg == NULL ? NULL : *arg;
13102
197f0a60
TT
13103 if (state)
13104 {
13105 gdb_assert (!state->finished);
13106 tpnum = get_number_or_range (state);
13107 }
13108 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
13109 {
13110 if (optional_p)
13111 tpnum = tracepoint_count;
13112 else
13113 error_no_arg (_("tracepoint number"));
13114 }
13115 else
197f0a60 13116 tpnum = get_number (arg);
1042e4c0
SS
13117
13118 if (tpnum <= 0)
13119 {
13120 if (instring && *instring)
13121 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
13122 instring);
13123 else
3e43a32a
MS
13124 printf_filtered (_("Tracepoint argument missing "
13125 "and no previous tracepoint\n"));
1042e4c0
SS
13126 return NULL;
13127 }
13128
13129 ALL_TRACEPOINTS (t)
13130 if (t->number == tpnum)
13131 {
d9b3f62e 13132 return (struct tracepoint *) t;
1042e4c0
SS
13133 }
13134
1042e4c0
SS
13135 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
13136 return NULL;
13137}
13138
d9b3f62e
PA
13139void
13140print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
13141{
13142 if (b->thread != -1)
13143 fprintf_unfiltered (fp, " thread %d", b->thread);
13144
13145 if (b->task != 0)
13146 fprintf_unfiltered (fp, " task %d", b->task);
13147
13148 fprintf_unfiltered (fp, "\n");
13149}
13150
6149aea9
PA
13151/* Save information on user settable breakpoints (watchpoints, etc) to
13152 a new script file named FILENAME. If FILTER is non-NULL, call it
13153 on each breakpoint and only include the ones for which it returns
13154 non-zero. */
13155
1042e4c0 13156static void
6149aea9
PA
13157save_breakpoints (char *filename, int from_tty,
13158 int (*filter) (const struct breakpoint *))
1042e4c0
SS
13159{
13160 struct breakpoint *tp;
6149aea9 13161 int any = 0;
a7bdde9e 13162 char *pathname;
1042e4c0 13163 struct cleanup *cleanup;
a7bdde9e 13164 struct ui_file *fp;
6149aea9 13165 int extra_trace_bits = 0;
1042e4c0 13166
6149aea9
PA
13167 if (filename == 0 || *filename == 0)
13168 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
13169
13170 /* See if we have anything to save. */
6149aea9 13171 ALL_BREAKPOINTS (tp)
1042e4c0 13172 {
6149aea9 13173 /* Skip internal and momentary breakpoints. */
09d682a4 13174 if (!user_breakpoint_p (tp))
6149aea9
PA
13175 continue;
13176
13177 /* If we have a filter, only save the breakpoints it accepts. */
13178 if (filter && !filter (tp))
13179 continue;
13180
13181 any = 1;
13182
13183 if (is_tracepoint (tp))
13184 {
13185 extra_trace_bits = 1;
13186
13187 /* We can stop searching. */
13188 break;
13189 }
1042e4c0 13190 }
6149aea9
PA
13191
13192 if (!any)
1042e4c0 13193 {
6149aea9 13194 warning (_("Nothing to save."));
1042e4c0
SS
13195 return;
13196 }
13197
6149aea9 13198 pathname = tilde_expand (filename);
1042e4c0 13199 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 13200 fp = gdb_fopen (pathname, "w");
059fb39f 13201 if (!fp)
6149aea9
PA
13202 error (_("Unable to open file '%s' for saving (%s)"),
13203 filename, safe_strerror (errno));
a7bdde9e 13204 make_cleanup_ui_file_delete (fp);
8bf6485c 13205
6149aea9
PA
13206 if (extra_trace_bits)
13207 save_trace_state_variables (fp);
8bf6485c 13208
6149aea9 13209 ALL_BREAKPOINTS (tp)
1042e4c0 13210 {
6149aea9 13211 /* Skip internal and momentary breakpoints. */
09d682a4 13212 if (!user_breakpoint_p (tp))
6149aea9 13213 continue;
8bf6485c 13214
6149aea9
PA
13215 /* If we have a filter, only save the breakpoints it accepts. */
13216 if (filter && !filter (tp))
13217 continue;
13218
348d480f 13219 tp->ops->print_recreate (tp, fp);
1042e4c0 13220
6149aea9
PA
13221 /* Note, we can't rely on tp->number for anything, as we can't
13222 assume the recreated breakpoint numbers will match. Use $bpnum
13223 instead. */
13224
13225 if (tp->cond_string)
13226 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
13227
13228 if (tp->ignore_count)
13229 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
13230
a7bdde9e 13231 if (tp->commands)
1042e4c0 13232 {
a7bdde9e
VP
13233 volatile struct gdb_exception ex;
13234
6149aea9 13235 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 13236
79a45e25 13237 ui_out_redirect (current_uiout, fp);
14dba4b4 13238 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 13239 {
79a45e25 13240 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 13241 }
79a45e25 13242 ui_out_redirect (current_uiout, NULL);
1042e4c0 13243
a7bdde9e
VP
13244 if (ex.reason < 0)
13245 throw_exception (ex);
1042e4c0 13246
a7bdde9e 13247 fprintf_unfiltered (fp, " end\n");
1042e4c0 13248 }
6149aea9
PA
13249
13250 if (tp->enable_state == bp_disabled)
13251 fprintf_unfiltered (fp, "disable\n");
13252
13253 /* If this is a multi-location breakpoint, check if the locations
13254 should be individually disabled. Watchpoint locations are
13255 special, and not user visible. */
13256 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13257 {
13258 struct bp_location *loc;
13259 int n = 1;
13260
13261 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
13262 if (!loc->enabled)
13263 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
13264 }
1042e4c0 13265 }
8bf6485c 13266
6149aea9 13267 if (extra_trace_bits && *default_collect)
8bf6485c
SS
13268 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
13269
1042e4c0
SS
13270 do_cleanups (cleanup);
13271 if (from_tty)
6149aea9
PA
13272 printf_filtered (_("Saved to file '%s'.\n"), filename);
13273}
13274
13275/* The `save breakpoints' command. */
13276
13277static void
13278save_breakpoints_command (char *args, int from_tty)
13279{
13280 save_breakpoints (args, from_tty, NULL);
13281}
13282
13283/* The `save tracepoints' command. */
13284
13285static void
13286save_tracepoints_command (char *args, int from_tty)
13287{
13288 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
13289}
13290
13291/* Create a vector of all tracepoints. */
13292
13293VEC(breakpoint_p) *
eeae04df 13294all_tracepoints (void)
1042e4c0
SS
13295{
13296 VEC(breakpoint_p) *tp_vec = 0;
13297 struct breakpoint *tp;
13298
13299 ALL_TRACEPOINTS (tp)
13300 {
13301 VEC_safe_push (breakpoint_p, tp_vec, tp);
13302 }
13303
13304 return tp_vec;
13305}
13306
c906108c 13307\f
4a64f543
MS
13308/* This help string is used for the break, hbreak, tbreak and thbreak
13309 commands. It is defined as a macro to prevent duplication.
13310 COMMAND should be a string constant containing the name of the
13311 command. */
31e2b00f
AS
13312#define BREAK_ARGS_HELP(command) \
13313command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
13314LOCATION may be a line number, function name, or \"*\" and an address.\n\
13315If a line number is specified, break at start of code for that line.\n\
13316If a function is specified, break at start of code for that function.\n\
13317If an address is specified, break at that exact address.\n\
dc10affe
PA
13318With no LOCATION, uses current execution address of the selected\n\
13319stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
13320\n\
13321THREADNUM is the number from \"info threads\".\n\
13322CONDITION is a boolean expression.\n\
13323\n\
d41c0fc8
PA
13324Multiple breakpoints at one place are permitted, and useful if their\n\
13325conditions are different.\n\
31e2b00f
AS
13326\n\
13327Do \"help breakpoints\" for info on other commands dealing with breakpoints."
13328
44feb3ce
TT
13329/* List of subcommands for "catch". */
13330static struct cmd_list_element *catch_cmdlist;
13331
13332/* List of subcommands for "tcatch". */
13333static struct cmd_list_element *tcatch_cmdlist;
13334
9ac4176b 13335void
44feb3ce
TT
13336add_catch_command (char *name, char *docstring,
13337 void (*sfunc) (char *args, int from_tty,
13338 struct cmd_list_element *command),
a96d9b2e
SDJ
13339 char **(*completer) (struct cmd_list_element *cmd,
13340 char *text, char *word),
44feb3ce
TT
13341 void *user_data_catch,
13342 void *user_data_tcatch)
13343{
13344 struct cmd_list_element *command;
13345
13346 command = add_cmd (name, class_breakpoint, NULL, docstring,
13347 &catch_cmdlist);
13348 set_cmd_sfunc (command, sfunc);
13349 set_cmd_context (command, user_data_catch);
a96d9b2e 13350 set_cmd_completer (command, completer);
44feb3ce
TT
13351
13352 command = add_cmd (name, class_breakpoint, NULL, docstring,
13353 &tcatch_cmdlist);
13354 set_cmd_sfunc (command, sfunc);
13355 set_cmd_context (command, user_data_tcatch);
a96d9b2e 13356 set_cmd_completer (command, completer);
44feb3ce
TT
13357}
13358
6c95b8df 13359static void
a79b8f6e 13360clear_syscall_counts (struct inferior *inf)
6c95b8df 13361{
6c95b8df
PA
13362 inf->total_syscalls_count = 0;
13363 inf->any_syscall_count = 0;
13364 VEC_free (int, inf->syscalls_counts);
13365}
13366
6149aea9
PA
13367static void
13368save_command (char *arg, int from_tty)
13369{
3e43a32a
MS
13370 printf_unfiltered (_("\"save\" must be followed by "
13371 "the name of a save subcommand.\n"));
6149aea9
PA
13372 help_list (save_cmdlist, "save ", -1, gdb_stdout);
13373}
13374
84f4c1fe
PM
13375struct breakpoint *
13376iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
13377 void *data)
13378{
35df4500 13379 struct breakpoint *b, *b_tmp;
84f4c1fe 13380
35df4500 13381 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
13382 {
13383 if ((*callback) (b, data))
13384 return b;
13385 }
13386
13387 return NULL;
13388}
13389
0574c78f
GB
13390/* Zero if any of the breakpoint's locations could be a location where
13391 functions have been inlined, nonzero otherwise. */
13392
13393static int
13394is_non_inline_function (struct breakpoint *b)
13395{
13396 /* The shared library event breakpoint is set on the address of a
13397 non-inline function. */
13398 if (b->type == bp_shlib_event)
13399 return 1;
13400
13401 return 0;
13402}
13403
13404/* Nonzero if the specified PC cannot be a location where functions
13405 have been inlined. */
13406
13407int
13408pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc)
13409{
13410 struct breakpoint *b;
13411 struct bp_location *bl;
13412
13413 ALL_BREAKPOINTS (b)
13414 {
13415 if (!is_non_inline_function (b))
13416 continue;
13417
13418 for (bl = b->loc; bl != NULL; bl = bl->next)
13419 {
13420 if (!bl->shlib_disabled
13421 && bpstat_check_location (bl, aspace, pc))
13422 return 1;
13423 }
13424 }
13425
13426 return 0;
13427}
13428
2060206e
PA
13429void
13430initialize_breakpoint_ops (void)
13431{
13432 static int initialized = 0;
13433
13434 struct breakpoint_ops *ops;
13435
13436 if (initialized)
13437 return;
13438 initialized = 1;
13439
13440 /* The breakpoint_ops structure to be inherit by all kinds of
13441 breakpoints (real breakpoints, i.e., user "break" breakpoints,
13442 internal and momentary breakpoints, etc.). */
13443 ops = &bkpt_base_breakpoint_ops;
13444 *ops = base_breakpoint_ops;
13445 ops->re_set = bkpt_re_set;
13446 ops->insert_location = bkpt_insert_location;
13447 ops->remove_location = bkpt_remove_location;
13448 ops->breakpoint_hit = bkpt_breakpoint_hit;
13449
13450 /* The breakpoint_ops structure to be used in regular breakpoints. */
13451 ops = &bkpt_breakpoint_ops;
13452 *ops = bkpt_base_breakpoint_ops;
13453 ops->re_set = bkpt_re_set;
13454 ops->resources_needed = bkpt_resources_needed;
13455 ops->print_it = bkpt_print_it;
13456 ops->print_mention = bkpt_print_mention;
13457 ops->print_recreate = bkpt_print_recreate;
13458
13459 /* Ranged breakpoints. */
13460 ops = &ranged_breakpoint_ops;
13461 *ops = bkpt_breakpoint_ops;
13462 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
13463 ops->resources_needed = resources_needed_ranged_breakpoint;
13464 ops->print_it = print_it_ranged_breakpoint;
13465 ops->print_one = print_one_ranged_breakpoint;
13466 ops->print_one_detail = print_one_detail_ranged_breakpoint;
13467 ops->print_mention = print_mention_ranged_breakpoint;
13468 ops->print_recreate = print_recreate_ranged_breakpoint;
13469
13470 /* Internal breakpoints. */
13471 ops = &internal_breakpoint_ops;
13472 *ops = bkpt_base_breakpoint_ops;
13473 ops->re_set = internal_bkpt_re_set;
13474 ops->check_status = internal_bkpt_check_status;
13475 ops->print_it = internal_bkpt_print_it;
13476 ops->print_mention = internal_bkpt_print_mention;
13477
13478 /* Momentary breakpoints. */
13479 ops = &momentary_breakpoint_ops;
13480 *ops = bkpt_base_breakpoint_ops;
13481 ops->re_set = momentary_bkpt_re_set;
13482 ops->check_status = momentary_bkpt_check_status;
13483 ops->print_it = momentary_bkpt_print_it;
13484 ops->print_mention = momentary_bkpt_print_mention;
13485
13486 /* GNU v3 exception catchpoints. */
13487 ops = &gnu_v3_exception_catchpoint_ops;
13488 *ops = bkpt_breakpoint_ops;
13489 ops->print_it = print_it_exception_catchpoint;
13490 ops->print_one = print_one_exception_catchpoint;
13491 ops->print_mention = print_mention_exception_catchpoint;
13492 ops->print_recreate = print_recreate_exception_catchpoint;
13493
13494 /* Watchpoints. */
13495 ops = &watchpoint_breakpoint_ops;
13496 *ops = base_breakpoint_ops;
3a5c3e22 13497 ops->dtor = dtor_watchpoint;
2060206e
PA
13498 ops->re_set = re_set_watchpoint;
13499 ops->insert_location = insert_watchpoint;
13500 ops->remove_location = remove_watchpoint;
13501 ops->breakpoint_hit = breakpoint_hit_watchpoint;
13502 ops->check_status = check_status_watchpoint;
13503 ops->resources_needed = resources_needed_watchpoint;
13504 ops->works_in_software_mode = works_in_software_mode_watchpoint;
13505 ops->print_it = print_it_watchpoint;
13506 ops->print_mention = print_mention_watchpoint;
13507 ops->print_recreate = print_recreate_watchpoint;
13508
13509 /* Masked watchpoints. */
13510 ops = &masked_watchpoint_breakpoint_ops;
13511 *ops = watchpoint_breakpoint_ops;
13512 ops->insert_location = insert_masked_watchpoint;
13513 ops->remove_location = remove_masked_watchpoint;
13514 ops->resources_needed = resources_needed_masked_watchpoint;
13515 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
13516 ops->print_it = print_it_masked_watchpoint;
13517 ops->print_one_detail = print_one_detail_masked_watchpoint;
13518 ops->print_mention = print_mention_masked_watchpoint;
13519 ops->print_recreate = print_recreate_masked_watchpoint;
13520
13521 /* Tracepoints. */
13522 ops = &tracepoint_breakpoint_ops;
13523 *ops = base_breakpoint_ops;
13524 ops->re_set = tracepoint_re_set;
13525 ops->breakpoint_hit = tracepoint_breakpoint_hit;
13526 ops->print_one_detail = tracepoint_print_one_detail;
13527 ops->print_mention = tracepoint_print_mention;
13528 ops->print_recreate = tracepoint_print_recreate;
13529
13530 /* Fork catchpoints. */
13531 ops = &catch_fork_breakpoint_ops;
13532 *ops = base_breakpoint_ops;
13533 ops->insert_location = insert_catch_fork;
13534 ops->remove_location = remove_catch_fork;
13535 ops->breakpoint_hit = breakpoint_hit_catch_fork;
13536 ops->print_it = print_it_catch_fork;
13537 ops->print_one = print_one_catch_fork;
13538 ops->print_mention = print_mention_catch_fork;
13539 ops->print_recreate = print_recreate_catch_fork;
13540
13541 /* Vfork catchpoints. */
13542 ops = &catch_vfork_breakpoint_ops;
13543 *ops = base_breakpoint_ops;
13544 ops->insert_location = insert_catch_vfork;
13545 ops->remove_location = remove_catch_vfork;
13546 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
13547 ops->print_it = print_it_catch_vfork;
13548 ops->print_one = print_one_catch_vfork;
13549 ops->print_mention = print_mention_catch_vfork;
13550 ops->print_recreate = print_recreate_catch_vfork;
13551
13552 /* Exec catchpoints. */
13553 ops = &catch_exec_breakpoint_ops;
13554 *ops = base_breakpoint_ops;
13555 ops->dtor = dtor_catch_exec;
13556 ops->insert_location = insert_catch_exec;
13557 ops->remove_location = remove_catch_exec;
13558 ops->breakpoint_hit = breakpoint_hit_catch_exec;
13559 ops->print_it = print_it_catch_exec;
13560 ops->print_one = print_one_catch_exec;
13561 ops->print_mention = print_mention_catch_exec;
13562 ops->print_recreate = print_recreate_catch_exec;
13563
13564 /* Syscall catchpoints. */
13565 ops = &catch_syscall_breakpoint_ops;
13566 *ops = base_breakpoint_ops;
13567 ops->dtor = dtor_catch_syscall;
13568 ops->insert_location = insert_catch_syscall;
13569 ops->remove_location = remove_catch_syscall;
13570 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
13571 ops->print_it = print_it_catch_syscall;
13572 ops->print_one = print_one_catch_syscall;
13573 ops->print_mention = print_mention_catch_syscall;
13574 ops->print_recreate = print_recreate_catch_syscall;
13575}
13576
c906108c 13577void
fba45db2 13578_initialize_breakpoint (void)
c906108c
SS
13579{
13580 struct cmd_list_element *c;
13581
2060206e
PA
13582 initialize_breakpoint_ops ();
13583
84acb35a 13584 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 13585 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 13586 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 13587
17450429
PP
13588 breakpoint_objfile_key = register_objfile_data ();
13589
c906108c
SS
13590 breakpoint_chain = 0;
13591 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
13592 before a breakpoint is set. */
13593 breakpoint_count = 0;
13594
1042e4c0
SS
13595 tracepoint_count = 0;
13596
1bedd215
AC
13597 add_com ("ignore", class_breakpoint, ignore_command, _("\
13598Set ignore-count of breakpoint number N to COUNT.\n\
13599Usage is `ignore N COUNT'."));
c906108c 13600 if (xdb_commands)
c5aa993b 13601 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 13602
1bedd215
AC
13603 add_com ("commands", class_breakpoint, commands_command, _("\
13604Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
13605Give breakpoint number as argument after \"commands\".\n\
13606With no argument, the targeted breakpoint is the last one set.\n\
13607The commands themselves follow starting on the next line.\n\
13608Type a line containing \"end\" to indicate the end of them.\n\
13609Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 13610then no output is printed when it is hit, except what the commands print."));
c906108c 13611
1bedd215
AC
13612 add_com ("condition", class_breakpoint, condition_command, _("\
13613Specify breakpoint number N to break only if COND is true.\n\
c906108c 13614Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 13615expression to be evaluated whenever breakpoint N is reached."));
c906108c 13616
1bedd215 13617 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 13618Set a temporary breakpoint.\n\
c906108c
SS
13619Like \"break\" except the breakpoint is only temporary,\n\
13620so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
13621by using \"enable delete\" on the breakpoint number.\n\
13622\n"
13623BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 13624 set_cmd_completer (c, location_completer);
c94fdfd0 13625
1bedd215 13626 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 13627Set a hardware assisted breakpoint.\n\
c906108c 13628Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
13629some target hardware may not have this support.\n\
13630\n"
13631BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 13632 set_cmd_completer (c, location_completer);
c906108c 13633
1bedd215 13634 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 13635Set a temporary hardware assisted breakpoint.\n\
c906108c 13636Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
13637so it will be deleted when hit.\n\
13638\n"
13639BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 13640 set_cmd_completer (c, location_completer);
c906108c 13641
1bedd215
AC
13642 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
13643Enable some breakpoints.\n\
c906108c
SS
13644Give breakpoint numbers (separated by spaces) as arguments.\n\
13645With no subcommand, breakpoints are enabled until you command otherwise.\n\
13646This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13647With a subcommand you can enable temporarily."),
c906108c
SS
13648 &enablelist, "enable ", 1, &cmdlist);
13649 if (xdb_commands)
1bedd215
AC
13650 add_com ("ab", class_breakpoint, enable_command, _("\
13651Enable some breakpoints.\n\
c906108c
SS
13652Give breakpoint numbers (separated by spaces) as arguments.\n\
13653With no subcommand, breakpoints are enabled until you command otherwise.\n\
13654This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13655With a subcommand you can enable temporarily."));
c906108c
SS
13656
13657 add_com_alias ("en", "enable", class_breakpoint, 1);
13658
84951ab5 13659 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 13660Enable some breakpoints.\n\
c906108c
SS
13661Give breakpoint numbers (separated by spaces) as arguments.\n\
13662This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13663May be abbreviated to simply \"enable\".\n"),
c5aa993b 13664 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 13665
1a966eab
AC
13666 add_cmd ("once", no_class, enable_once_command, _("\
13667Enable breakpoints for one hit. Give breakpoint numbers.\n\
13668If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13669 &enablebreaklist);
13670
1a966eab
AC
13671 add_cmd ("delete", no_class, enable_delete_command, _("\
13672Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13673If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13674 &enablebreaklist);
13675
1a966eab
AC
13676 add_cmd ("delete", no_class, enable_delete_command, _("\
13677Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13678If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13679 &enablelist);
13680
1a966eab
AC
13681 add_cmd ("once", no_class, enable_once_command, _("\
13682Enable breakpoints for one hit. Give breakpoint numbers.\n\
13683If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13684 &enablelist);
13685
1bedd215
AC
13686 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13687Disable some breakpoints.\n\
c906108c
SS
13688Arguments are breakpoint numbers with spaces in between.\n\
13689To disable all breakpoints, give no argument.\n\
64b9b334 13690A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
13691 &disablelist, "disable ", 1, &cmdlist);
13692 add_com_alias ("dis", "disable", class_breakpoint, 1);
13693 add_com_alias ("disa", "disable", class_breakpoint, 1);
13694 if (xdb_commands)
1bedd215
AC
13695 add_com ("sb", class_breakpoint, disable_command, _("\
13696Disable some breakpoints.\n\
c906108c
SS
13697Arguments are breakpoint numbers with spaces in between.\n\
13698To disable all breakpoints, give no argument.\n\
64b9b334 13699A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 13700
1a966eab
AC
13701 add_cmd ("breakpoints", class_alias, disable_command, _("\
13702Disable some breakpoints.\n\
c906108c
SS
13703Arguments are breakpoint numbers with spaces in between.\n\
13704To disable all breakpoints, give no argument.\n\
64b9b334 13705A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 13706This command may be abbreviated \"disable\"."),
c906108c
SS
13707 &disablelist);
13708
1bedd215
AC
13709 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13710Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13711Arguments are breakpoint numbers with spaces in between.\n\
13712To delete all breakpoints, give no argument.\n\
13713\n\
13714Also a prefix command for deletion of other GDB objects.\n\
1bedd215 13715The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
13716 &deletelist, "delete ", 1, &cmdlist);
13717 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 13718 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 13719 if (xdb_commands)
1bedd215
AC
13720 add_com ("db", class_breakpoint, delete_command, _("\
13721Delete some breakpoints.\n\
c906108c 13722Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 13723To delete all breakpoints, give no argument.\n"));
c906108c 13724
1a966eab
AC
13725 add_cmd ("breakpoints", class_alias, delete_command, _("\
13726Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13727Arguments are breakpoint numbers with spaces in between.\n\
13728To delete all breakpoints, give no argument.\n\
1a966eab 13729This command may be abbreviated \"delete\"."),
c906108c
SS
13730 &deletelist);
13731
1bedd215
AC
13732 add_com ("clear", class_breakpoint, clear_command, _("\
13733Clear breakpoint at specified line or function.\n\
c906108c
SS
13734Argument may be line number, function name, or \"*\" and an address.\n\
13735If line number is specified, all breakpoints in that line are cleared.\n\
13736If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
13737If an address is specified, breakpoints at that address are cleared.\n\
13738\n\
13739With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
13740is executing in.\n\
13741\n\
1bedd215 13742See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 13743 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 13744
1bedd215 13745 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
13746Set breakpoint at specified line or function.\n"
13747BREAK_ARGS_HELP ("break")));
5ba2abeb 13748 set_cmd_completer (c, location_completer);
c94fdfd0 13749
c906108c
SS
13750 add_com_alias ("b", "break", class_run, 1);
13751 add_com_alias ("br", "break", class_run, 1);
13752 add_com_alias ("bre", "break", class_run, 1);
13753 add_com_alias ("brea", "break", class_run, 1);
13754
7681d515
PM
13755 if (xdb_commands)
13756 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
13757
13758 if (dbx_commands)
13759 {
1bedd215
AC
13760 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13761Break in function/address or break at a line in the current file."),
c5aa993b
JM
13762 &stoplist, "stop ", 1, &cmdlist);
13763 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 13764 _("Break in function or address."), &stoplist);
c5aa993b 13765 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 13766 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
13767 add_com ("status", class_info, breakpoints_info, _("\
13768Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13769The \"Type\" column indicates one of:\n\
13770\tbreakpoint - normal breakpoint\n\
13771\twatchpoint - watchpoint\n\
13772The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13773the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13774breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13775address and file/line number respectively.\n\
13776\n\
13777Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13778are set to the address of the last breakpoint listed unless the command\n\
13779is prefixed with \"server \".\n\n\
c906108c 13780Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13781breakpoint set."));
c906108c
SS
13782 }
13783
1bedd215 13784 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 13785Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
13786The \"Type\" column indicates one of:\n\
13787\tbreakpoint - normal breakpoint\n\
13788\twatchpoint - watchpoint\n\
13789The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13790the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13791breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13792address and file/line number respectively.\n\
13793\n\
13794Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13795are set to the address of the last breakpoint listed unless the command\n\
13796is prefixed with \"server \".\n\n\
c906108c 13797Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13798breakpoint set."));
c906108c 13799
6b04bdb7
MS
13800 add_info_alias ("b", "breakpoints", 1);
13801
c906108c 13802 if (xdb_commands)
1bedd215
AC
13803 add_com ("lb", class_breakpoint, breakpoints_info, _("\
13804Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13805The \"Type\" column indicates one of:\n\
13806\tbreakpoint - normal breakpoint\n\
13807\twatchpoint - watchpoint\n\
13808The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13809the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13810breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13811address and file/line number respectively.\n\
13812\n\
13813Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13814are set to the address of the last breakpoint listed unless the command\n\
13815is prefixed with \"server \".\n\n\
c906108c 13816Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13817breakpoint set."));
c906108c 13818
1a966eab
AC
13819 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13820Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13821The \"Type\" column indicates one of:\n\
13822\tbreakpoint - normal breakpoint\n\
13823\twatchpoint - watchpoint\n\
13824\tlongjmp - internal breakpoint used to step through longjmp()\n\
13825\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13826\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
13827\tfinish - internal breakpoint used by the \"finish\" command\n\
13828The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
13829the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13830breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
13831address and file/line number respectively.\n\
13832\n\
13833Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13834are set to the address of the last breakpoint listed unless the command\n\
13835is prefixed with \"server \".\n\n\
c906108c 13836Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 13837breakpoint set."),
c906108c
SS
13838 &maintenanceinfolist);
13839
44feb3ce
TT
13840 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13841Set catchpoints to catch events."),
13842 &catch_cmdlist, "catch ",
13843 0/*allow-unknown*/, &cmdlist);
13844
13845 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13846Set temporary catchpoints to catch events."),
13847 &tcatch_cmdlist, "tcatch ",
13848 0/*allow-unknown*/, &cmdlist);
13849
13850 /* Add catch and tcatch sub-commands. */
13851 add_catch_command ("catch", _("\
13852Catch an exception, when caught.\n\
13853With an argument, catch only exceptions with the given name."),
13854 catch_catch_command,
a96d9b2e 13855 NULL,
44feb3ce
TT
13856 CATCH_PERMANENT,
13857 CATCH_TEMPORARY);
13858 add_catch_command ("throw", _("\
13859Catch an exception, when thrown.\n\
13860With an argument, catch only exceptions with the given name."),
13861 catch_throw_command,
a96d9b2e 13862 NULL,
44feb3ce
TT
13863 CATCH_PERMANENT,
13864 CATCH_TEMPORARY);
13865 add_catch_command ("fork", _("Catch calls to fork."),
13866 catch_fork_command_1,
a96d9b2e 13867 NULL,
44feb3ce
TT
13868 (void *) (uintptr_t) catch_fork_permanent,
13869 (void *) (uintptr_t) catch_fork_temporary);
13870 add_catch_command ("vfork", _("Catch calls to vfork."),
13871 catch_fork_command_1,
a96d9b2e 13872 NULL,
44feb3ce
TT
13873 (void *) (uintptr_t) catch_vfork_permanent,
13874 (void *) (uintptr_t) catch_vfork_temporary);
13875 add_catch_command ("exec", _("Catch calls to exec."),
13876 catch_exec_command_1,
a96d9b2e
SDJ
13877 NULL,
13878 CATCH_PERMANENT,
13879 CATCH_TEMPORARY);
13880 add_catch_command ("syscall", _("\
13881Catch system calls by their names and/or numbers.\n\
13882Arguments say which system calls to catch. If no arguments\n\
13883are given, every system call will be caught.\n\
13884Arguments, if given, should be one or more system call names\n\
13885(if your system supports that), or system call numbers."),
13886 catch_syscall_command_1,
13887 catch_syscall_completer,
44feb3ce
TT
13888 CATCH_PERMANENT,
13889 CATCH_TEMPORARY);
c5aa993b 13890
1bedd215
AC
13891 c = add_com ("watch", class_breakpoint, watch_command, _("\
13892Set a watchpoint for an expression.\n\
06a64a0b 13893Usage: watch [-l|-location] EXPRESSION\n\
c906108c 13894A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13895an expression changes.\n\
13896If -l or -location is given, this evaluates EXPRESSION and watches\n\
13897the memory to which it refers."));
65d12d83 13898 set_cmd_completer (c, expression_completer);
c906108c 13899
1bedd215
AC
13900 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13901Set a read watchpoint for an expression.\n\
06a64a0b 13902Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 13903A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13904an expression is read.\n\
13905If -l or -location is given, this evaluates EXPRESSION and watches\n\
13906the memory to which it refers."));
65d12d83 13907 set_cmd_completer (c, expression_completer);
c906108c 13908
1bedd215
AC
13909 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13910Set a watchpoint for an expression.\n\
06a64a0b 13911Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 13912A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13913an expression is either read or written.\n\
13914If -l or -location is given, this evaluates EXPRESSION and watches\n\
13915the memory to which it refers."));
65d12d83 13916 set_cmd_completer (c, expression_completer);
c906108c 13917
d77f58be 13918 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 13919Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 13920
920d2a44
AC
13921 /* XXX: cagney/2005-02-23: This should be a boolean, and should
13922 respond to changes - contrary to the description. */
85c07804
AC
13923 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
13924 &can_use_hw_watchpoints, _("\
13925Set debugger's willingness to use watchpoint hardware."), _("\
13926Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
13927If zero, gdb will not use hardware for new watchpoints, even if\n\
13928such is available. (However, any hardware watchpoints that were\n\
13929created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
13930hardware.)"),
13931 NULL,
920d2a44 13932 show_can_use_hw_watchpoints,
85c07804 13933 &setlist, &showlist);
c906108c
SS
13934
13935 can_use_hw_watchpoints = 1;
fa8d40ab 13936
1042e4c0
SS
13937 /* Tracepoint manipulation commands. */
13938
13939 c = add_com ("trace", class_breakpoint, trace_command, _("\
13940Set a tracepoint at specified line or function.\n\
13941\n"
13942BREAK_ARGS_HELP ("trace") "\n\
13943Do \"help tracepoints\" for info on other tracepoint commands."));
13944 set_cmd_completer (c, location_completer);
13945
13946 add_com_alias ("tp", "trace", class_alias, 0);
13947 add_com_alias ("tr", "trace", class_alias, 1);
13948 add_com_alias ("tra", "trace", class_alias, 1);
13949 add_com_alias ("trac", "trace", class_alias, 1);
13950
7a697b8d
SS
13951 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
13952Set a fast tracepoint at specified line or function.\n\
13953\n"
13954BREAK_ARGS_HELP ("ftrace") "\n\
13955Do \"help tracepoints\" for info on other tracepoint commands."));
13956 set_cmd_completer (c, location_completer);
13957
0fb4aa4b
PA
13958 c = add_com ("strace", class_breakpoint, strace_command, _("\
13959Set a static tracepoint at specified line, function or marker.\n\
13960\n\
13961strace [LOCATION] [if CONDITION]\n\
13962LOCATION may be a line number, function name, \"*\" and an address,\n\
13963or -m MARKER_ID.\n\
13964If a line number is specified, probe the marker at start of code\n\
13965for that line. If a function is specified, probe the marker at start\n\
13966of code for that function. If an address is specified, probe the marker\n\
13967at that exact address. If a marker id is specified, probe the marker\n\
13968with that name. With no LOCATION, uses current execution address of\n\
13969the selected stack frame.\n\
13970Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
13971This collects arbitrary user data passed in the probe point call to the\n\
13972tracing library. You can inspect it when analyzing the trace buffer,\n\
13973by printing the $_sdata variable like any other convenience variable.\n\
13974\n\
13975CONDITION is a boolean expression.\n\
13976\n\
d41c0fc8
PA
13977Multiple tracepoints at one place are permitted, and useful if their\n\
13978conditions are different.\n\
0fb4aa4b
PA
13979\n\
13980Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
13981Do \"help tracepoints\" for info on other tracepoint commands."));
13982 set_cmd_completer (c, location_completer);
13983
1042e4c0 13984 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 13985Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
13986Convenience variable \"$tpnum\" contains the number of the\n\
13987last tracepoint set."));
13988
13989 add_info_alias ("tp", "tracepoints", 1);
13990
13991 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
13992Delete specified tracepoints.\n\
13993Arguments are tracepoint numbers, separated by spaces.\n\
13994No argument means delete all tracepoints."),
13995 &deletelist);
13996
13997 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13998Disable specified tracepoints.\n\
13999Arguments are tracepoint numbers, separated by spaces.\n\
14000No argument means disable all tracepoints."),
14001 &disablelist);
14002 deprecate_cmd (c, "disable");
14003
14004 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14005Enable specified tracepoints.\n\
14006Arguments are tracepoint numbers, separated by spaces.\n\
14007No argument means enable all tracepoints."),
14008 &enablelist);
14009 deprecate_cmd (c, "enable");
14010
14011 add_com ("passcount", class_trace, trace_pass_command, _("\
14012Set the passcount for a tracepoint.\n\
14013The trace will end when the tracepoint has been passed 'count' times.\n\
14014Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14015if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14016
6149aea9
PA
14017 add_prefix_cmd ("save", class_breakpoint, save_command,
14018 _("Save breakpoint definitions as a script."),
14019 &save_cmdlist, "save ",
14020 0/*allow-unknown*/, &cmdlist);
14021
14022 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14023Save current breakpoint definitions as a script.\n\
cce7e648 14024This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14025catchpoints, tracepoints). Use the 'source' command in another debug\n\
14026session to restore them."),
14027 &save_cmdlist);
14028 set_cmd_completer (c, filename_completer);
14029
14030 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14031Save current tracepoint definitions as a script.\n\
6149aea9
PA
14032Use the 'source' command in another debug session to restore them."),
14033 &save_cmdlist);
1042e4c0
SS
14034 set_cmd_completer (c, filename_completer);
14035
6149aea9
PA
14036 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
14037 deprecate_cmd (c, "save tracepoints");
14038
1bedd215 14039 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
14040Breakpoint specific settings\n\
14041Configure various breakpoint-specific variables such as\n\
1bedd215 14042pending breakpoint behavior"),
fa8d40ab
JJ
14043 &breakpoint_set_cmdlist, "set breakpoint ",
14044 0/*allow-unknown*/, &setlist);
1bedd215 14045 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
14046Breakpoint specific settings\n\
14047Configure various breakpoint-specific variables such as\n\
1bedd215 14048pending breakpoint behavior"),
fa8d40ab
JJ
14049 &breakpoint_show_cmdlist, "show breakpoint ",
14050 0/*allow-unknown*/, &showlist);
14051
7915a72c
AC
14052 add_setshow_auto_boolean_cmd ("pending", no_class,
14053 &pending_break_support, _("\
14054Set debugger's behavior regarding pending breakpoints."), _("\
14055Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14056If on, an unrecognized breakpoint location will cause gdb to create a\n\
14057pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14058an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14059user-query to see if a pending breakpoint should be created."),
2c5b56ce 14060 NULL,
920d2a44 14061 show_pending_break_support,
6e1d7d6c
AC
14062 &breakpoint_set_cmdlist,
14063 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14064
14065 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14066
14067 add_setshow_boolean_cmd ("auto-hw", no_class,
14068 &automatic_hardware_breakpoints, _("\
14069Set automatic usage of hardware breakpoints."), _("\
14070Show automatic usage of hardware breakpoints."), _("\
14071If set, the debugger will automatically use hardware breakpoints for\n\
14072breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14073a warning will be emitted for such breakpoints."),
14074 NULL,
14075 show_automatic_hardware_breakpoints,
14076 &breakpoint_set_cmdlist,
14077 &breakpoint_show_cmdlist);
74960c60 14078
33e5cbd6
PA
14079 add_setshow_enum_cmd ("always-inserted", class_support,
14080 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
14081Set mode for inserting breakpoints."), _("\
14082Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
14083When this mode is off, breakpoints are inserted in inferior when it is\n\
14084resumed, and removed when execution stops. When this mode is on,\n\
14085breakpoints are inserted immediately and removed only when the user\n\
14086deletes the breakpoint. When this mode is auto (which is the default),\n\
14087the behaviour depends on the non-stop setting (see help set non-stop).\n\
14088In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
14089behaves as if always-inserted mode is on; if gdb is controlling the\n\
14090inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
14091 NULL,
14092 &show_always_inserted_mode,
14093 &breakpoint_set_cmdlist,
14094 &breakpoint_show_cmdlist);
f1310107
TJB
14095
14096 add_com ("break-range", class_breakpoint, break_range_command, _("\
14097Set a breakpoint for an address range.\n\
14098break-range START-LOCATION, END-LOCATION\n\
14099where START-LOCATION and END-LOCATION can be one of the following:\n\
14100 LINENUM, for that line in the current file,\n\
14101 FILE:LINENUM, for that line in that file,\n\
14102 +OFFSET, for that number of lines after the current line\n\
14103 or the start of the range\n\
14104 FUNCTION, for the first line in that function,\n\
14105 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14106 *ADDRESS, for the instruction at that address.\n\
14107\n\
14108The breakpoint will stop execution of the inferior whenever it executes\n\
14109an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14110range (including START-LOCATION and END-LOCATION)."));
14111
765dc015 14112 automatic_hardware_breakpoints = 1;
f3b1572e
PA
14113
14114 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 14115}