* bcache.c (bcache): Rename `bcache' to `cache'(-Wshadow).
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1 4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7b6bb8da 5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
a6d9a66e 23#include "arch-utils.h"
c906108c 24#include <ctype.h>
776592bf 25#include "hashtab.h"
c906108c
SS
26#include "symtab.h"
27#include "frame.h"
28#include "breakpoint.h"
1042e4c0 29#include "tracepoint.h"
c906108c
SS
30#include "gdbtypes.h"
31#include "expression.h"
32#include "gdbcore.h"
33#include "gdbcmd.h"
34#include "value.h"
35#include "command.h"
36#include "inferior.h"
37#include "gdbthread.h"
38#include "target.h"
39#include "language.h"
40#include "gdb_string.h"
50f182aa 41#include "gdb-demangle.h"
0ba1096a 42#include "filenames.h"
c906108c
SS
43#include "annotate.h"
44#include "symfile.h"
45#include "objfiles.h"
0378c332 46#include "source.h"
c5f0f3d0 47#include "linespec.h"
c94fdfd0 48#include "completer.h"
5b7f31a4 49#include "gdb.h"
8b93c638 50#include "ui-out.h"
e1507482 51#include "cli/cli-script.h"
0225421b 52#include "gdb_assert.h"
fe898f56 53#include "block.h"
a77053c2 54#include "solib.h"
84acb35a
JJ
55#include "solist.h"
56#include "observer.h"
60250e8b 57#include "exceptions.h"
765dc015 58#include "memattr.h"
f7f9143b 59#include "ada-lang.h"
d1aa2f50 60#include "top.h"
fa4727a6 61#include "wrapper.h"
79a45b7d 62#include "valprint.h"
4efc6507 63#include "jit.h"
a96d9b2e 64#include "xml-syscall.h"
65d79d4b 65#include "parser-defs.h"
e9cafbcc 66#include "cli/cli-utils.h"
be34f849 67#include "continuations.h"
1bfeeb0f
JL
68#include "stack.h"
69#include "skip.h"
c906108c 70
1042e4c0
SS
71/* readline include files */
72#include "readline/readline.h"
73#include "readline/history.h"
74
75/* readline defines this. */
76#undef savestring
77
034dad6f 78#include "mi/mi-common.h"
7371cf6d 79#include "python/python.h"
104c1213 80
4a64f543 81/* Prototypes for local functions. */
c906108c 82
a14ed312 83static void enable_delete_command (char *, int);
c906108c 84
a14ed312 85static void enable_once_command (char *, int);
c906108c 86
a14ed312 87static void disable_command (char *, int);
c906108c 88
a14ed312 89static void enable_command (char *, int);
c906108c 90
95a42b64
TT
91static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
92 void *),
93 void *);
c906108c 94
a14ed312 95static void ignore_command (char *, int);
c906108c 96
4efb68b1 97static int breakpoint_re_set_one (void *);
c906108c 98
348d480f
PA
99static void breakpoint_re_set_default (struct breakpoint *);
100
a14ed312 101static void clear_command (char *, int);
c906108c 102
a14ed312 103static void catch_command (char *, int);
c906108c 104
a9634178 105static int can_use_hardware_watchpoint (struct value *);
c906108c 106
98deb0da 107static void break_command_1 (char *, int, int);
c906108c 108
a14ed312 109static void mention (struct breakpoint *);
c906108c 110
348d480f
PA
111static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
112 enum bptype,
c0a91b2b 113 const struct breakpoint_ops *);
3742cc8b
YQ
114static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
115 const struct symtab_and_line *);
116
4a64f543
MS
117/* This function is used in gdbtk sources and thus can not be made
118 static. */
63c252f8 119struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 120 struct symtab_and_line,
c0a91b2b
TT
121 enum bptype,
122 const struct breakpoint_ops *);
c906108c 123
06edf0c0
PA
124static struct breakpoint *
125 momentary_breakpoint_from_master (struct breakpoint *orig,
126 enum bptype type,
c0a91b2b 127 const struct breakpoint_ops *ops);
06edf0c0 128
76897487
KB
129static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
130
a6d9a66e
UW
131static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
132 CORE_ADDR bpaddr,
88f7da05 133 enum bptype bptype);
76897487 134
6c95b8df
PA
135static void describe_other_breakpoints (struct gdbarch *,
136 struct program_space *, CORE_ADDR,
5af949e3 137 struct obj_section *, int);
c906108c 138
6c95b8df
PA
139static int breakpoint_address_match (struct address_space *aspace1,
140 CORE_ADDR addr1,
141 struct address_space *aspace2,
142 CORE_ADDR addr2);
143
85d721b8
PA
144static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
f1310107
TJB
147static int breakpoint_location_address_match (struct bp_location *bl,
148 struct address_space *aspace,
149 CORE_ADDR addr);
150
a14ed312 151static void breakpoints_info (char *, int);
c906108c 152
d77f58be
SS
153static void watchpoints_info (char *, int);
154
e5a67952
MS
155static int breakpoint_1 (char *, int,
156 int (*) (const struct breakpoint *));
c906108c 157
4efb68b1 158static int breakpoint_cond_eval (void *);
c906108c 159
4efb68b1 160static void cleanup_executing_breakpoints (void *);
c906108c 161
a14ed312 162static void commands_command (char *, int);
c906108c 163
a14ed312 164static void condition_command (char *, int);
c906108c 165
c5aa993b
JM
166typedef enum
167 {
168 mark_inserted,
169 mark_uninserted
170 }
171insertion_state_t;
c906108c 172
0bde7532 173static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 174static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 175
e514a9d6 176static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 177
4efb68b1 178static int watchpoint_check (void *);
c906108c 179
a14ed312 180static void maintenance_info_breakpoints (char *, int);
c906108c 181
a14ed312 182static int hw_breakpoint_used_count (void);
c906108c 183
a1398e0c
PA
184static int hw_watchpoint_use_count (struct breakpoint *);
185
186static int hw_watchpoint_used_count_others (struct breakpoint *except,
187 enum bptype type,
188 int *other_type_used);
c906108c 189
a14ed312 190static void hbreak_command (char *, int);
c906108c 191
a14ed312 192static void thbreak_command (char *, int);
c906108c 193
51be5b68 194static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp);
c906108c 195
a14ed312 196static void stop_command (char *arg, int from_tty);
7a292a7a 197
a14ed312 198static void stopin_command (char *arg, int from_tty);
7a292a7a 199
a14ed312 200static void stopat_command (char *arg, int from_tty);
7a292a7a 201
a14ed312 202static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 203
d85310f7
MS
204static void catch_exception_command_1 (enum exception_event_kind ex_event,
205 char *arg, int tempflag, int from_tty);
7a292a7a 206
a14ed312 207static void tcatch_command (char *arg, int from_tty);
7a292a7a 208
d03285ec
UW
209static void detach_single_step_breakpoints (void);
210
6c95b8df
PA
211static int single_step_breakpoint_inserted_here_p (struct address_space *,
212 CORE_ADDR pc);
1aafd4da 213
fe3f5fa8 214static void free_bp_location (struct bp_location *loc);
f431efe5
PA
215static void incref_bp_location (struct bp_location *loc);
216static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 217
39d61571 218static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 219
b60e7edf 220static void update_global_location_list (int);
a5606eee 221
b60e7edf 222static void update_global_location_list_nothrow (int);
74960c60 223
d77f58be 224static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
225
226static void insert_breakpoint_locations (void);
a5606eee 227
a96d9b2e
SDJ
228static int syscall_catchpoint_p (struct breakpoint *b);
229
1042e4c0
SS
230static void tracepoints_info (char *, int);
231
232static void delete_trace_command (char *, int);
233
234static void enable_trace_command (char *, int);
235
236static void disable_trace_command (char *, int);
237
238static void trace_pass_command (char *, int);
239
9c06b0b4
TJB
240static int is_masked_watchpoint (const struct breakpoint *b);
241
0fb4aa4b
PA
242/* Assuming we're creating a static tracepoint, does S look like a
243 static tracepoint marker spec ("-m MARKER_ID")? */
244#define is_marker_spec(s) \
f5a8e22b 245 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 246
2060206e
PA
247/* The abstract base class all breakpoint_ops structures inherit
248 from. */
249static struct breakpoint_ops base_breakpoint_ops;
250
251/* The breakpoint_ops structure to be inherited by all breakpoint_ops
252 that are implemented on top of software or hardware breakpoints
253 (user breakpoints, internal and momentary breakpoints, etc.). */
254static struct breakpoint_ops bkpt_base_breakpoint_ops;
255
256/* Internal breakpoints class type. */
06edf0c0 257static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
258
259/* Momentary breakpoints class type. */
06edf0c0
PA
260static struct breakpoint_ops momentary_breakpoint_ops;
261
2060206e
PA
262/* The breakpoint_ops structure to be used in regular user created
263 breakpoints. */
264struct breakpoint_ops bkpt_breakpoint_ops;
265
5cea2a26
PA
266/* A reference-counted struct command_line. This lets multiple
267 breakpoints share a single command list. */
268struct counted_command_line
269{
270 /* The reference count. */
271 int refc;
272
273 /* The command list. */
274 struct command_line *commands;
275};
276
277struct command_line *
278breakpoint_commands (struct breakpoint *b)
279{
280 return b->commands ? b->commands->commands : NULL;
281}
3daf8fe5 282
f3b1572e
PA
283/* Flag indicating that a command has proceeded the inferior past the
284 current breakpoint. */
285
286static int breakpoint_proceeded;
287
956a9fb9 288const char *
2cec12e5
AR
289bpdisp_text (enum bpdisp disp)
290{
4a64f543
MS
291 /* NOTE: the following values are a part of MI protocol and
292 represent values of 'disp' field returned when inferior stops at
293 a breakpoint. */
bc043ef3 294 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 295
2cec12e5
AR
296 return bpdisps[(int) disp];
297}
c906108c 298
4a64f543 299/* Prototypes for exported functions. */
c906108c 300/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 301 if such is available. */
c906108c
SS
302static int can_use_hw_watchpoints;
303
920d2a44
AC
304static void
305show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
306 struct cmd_list_element *c,
307 const char *value)
308{
3e43a32a
MS
309 fprintf_filtered (file,
310 _("Debugger's willingness to use "
311 "watchpoint hardware is %s.\n"),
920d2a44
AC
312 value);
313}
314
fa8d40ab
JJ
315/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
316 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 317 for unrecognized breakpoint locations.
fa8d40ab
JJ
318 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
319static enum auto_boolean pending_break_support;
920d2a44
AC
320static void
321show_pending_break_support (struct ui_file *file, int from_tty,
322 struct cmd_list_element *c,
323 const char *value)
324{
3e43a32a
MS
325 fprintf_filtered (file,
326 _("Debugger's behavior regarding "
327 "pending breakpoints is %s.\n"),
920d2a44
AC
328 value);
329}
fa8d40ab 330
765dc015 331/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 332 set with "break" but falling in read-only memory.
765dc015
VP
333 If 0, gdb will warn about such breakpoints, but won't automatically
334 use hardware breakpoints. */
335static int automatic_hardware_breakpoints;
336static void
337show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
338 struct cmd_list_element *c,
339 const char *value)
340{
3e43a32a
MS
341 fprintf_filtered (file,
342 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
343 value);
344}
345
33e5cbd6
PA
346/* If on, gdb will keep breakpoints inserted even as inferior is
347 stopped, and immediately insert any new breakpoints. If off, gdb
348 will insert breakpoints into inferior only when resuming it, and
349 will remove breakpoints upon stop. If auto, GDB will behave as ON
350 if in non-stop mode, and as OFF if all-stop mode.*/
351
352static const char always_inserted_auto[] = "auto";
353static const char always_inserted_on[] = "on";
354static const char always_inserted_off[] = "off";
355static const char *always_inserted_enums[] = {
356 always_inserted_auto,
357 always_inserted_off,
358 always_inserted_on,
359 NULL
360};
361static const char *always_inserted_mode = always_inserted_auto;
362static void
74960c60 363show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 364 struct cmd_list_element *c, const char *value)
74960c60 365{
33e5cbd6 366 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
367 fprintf_filtered (file,
368 _("Always inserted breakpoint "
369 "mode is %s (currently %s).\n"),
33e5cbd6
PA
370 value,
371 breakpoints_always_inserted_mode () ? "on" : "off");
372 else
3e43a32a
MS
373 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
374 value);
74960c60
VP
375}
376
33e5cbd6
PA
377int
378breakpoints_always_inserted_mode (void)
379{
380 return (always_inserted_mode == always_inserted_on
381 || (always_inserted_mode == always_inserted_auto && non_stop));
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
DJ
1049void
1050breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1051{
4a64f543
MS
1052 /* Left boundary, right boundary and median element of our binary
1053 search. */
876fa593
JK
1054 unsigned bc_l, bc_r, bc;
1055
4a64f543
MS
1056 /* Find BC_L which is a leftmost element which may affect BUF
1057 content. It is safe to report lower value but a failure to
1058 report higher one. */
876fa593
JK
1059
1060 bc_l = 0;
1061 bc_r = bp_location_count;
1062 while (bc_l + 1 < bc_r)
1063 {
35df4500 1064 struct bp_location *bl;
876fa593
JK
1065
1066 bc = (bc_l + bc_r) / 2;
35df4500 1067 bl = bp_location[bc];
876fa593 1068
4a64f543
MS
1069 /* Check first BL->ADDRESS will not overflow due to the added
1070 constant. Then advance the left boundary only if we are sure
1071 the BC element can in no way affect the BUF content (MEMADDR
1072 to MEMADDR + LEN range).
876fa593 1073
4a64f543
MS
1074 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1075 offset so that we cannot miss a breakpoint with its shadow
1076 range tail still reaching MEMADDR. */
c5aa993b 1077
35df4500
TJB
1078 if ((bl->address + bp_location_shadow_len_after_address_max
1079 >= bl->address)
1080 && (bl->address + bp_location_shadow_len_after_address_max
1081 <= memaddr))
876fa593
JK
1082 bc_l = bc;
1083 else
1084 bc_r = bc;
1085 }
1086
128070bb
PA
1087 /* Due to the binary search above, we need to make sure we pick the
1088 first location that's at BC_L's address. E.g., if there are
1089 multiple locations at the same address, BC_L may end up pointing
1090 at a duplicate location, and miss the "master"/"inserted"
1091 location. Say, given locations L1, L2 and L3 at addresses A and
1092 B:
1093
1094 L1@A, L2@A, L3@B, ...
1095
1096 BC_L could end up pointing at location L2, while the "master"
1097 location could be L1. Since the `loc->inserted' flag is only set
1098 on "master" locations, we'd forget to restore the shadow of L1
1099 and L2. */
1100 while (bc_l > 0
1101 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1102 bc_l--;
1103
876fa593
JK
1104 /* Now do full processing of the found relevant range of elements. */
1105
1106 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1107 {
35df4500 1108 struct bp_location *bl = bp_location[bc];
876fa593
JK
1109 CORE_ADDR bp_addr = 0;
1110 int bp_size = 0;
1111 int bptoffset = 0;
1112
35df4500
TJB
1113 /* bp_location array has BL->OWNER always non-NULL. */
1114 if (bl->owner->type == bp_none)
8a3fe4f8 1115 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1116 bl->owner->number);
ffce0d52 1117
e5dd4106 1118 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1119 content. */
1120
35df4500
TJB
1121 if (bl->address >= bp_location_placed_address_before_address_max
1122 && memaddr + len <= (bl->address
1123 - bp_location_placed_address_before_address_max))
876fa593
JK
1124 break;
1125
35df4500 1126 if (!bp_location_has_shadow (bl))
c5aa993b 1127 continue;
35df4500 1128 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1129 current_program_space->aspace, 0))
1130 continue;
1131
c5aa993b
JM
1132 /* Addresses and length of the part of the breakpoint that
1133 we need to copy. */
35df4500
TJB
1134 bp_addr = bl->target_info.placed_address;
1135 bp_size = bl->target_info.shadow_len;
8defab1a 1136
c5aa993b
JM
1137 if (bp_addr + bp_size <= memaddr)
1138 /* The breakpoint is entirely before the chunk of memory we
1139 are reading. */
1140 continue;
8defab1a 1141
c5aa993b
JM
1142 if (bp_addr >= memaddr + len)
1143 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1144 reading. */
c5aa993b 1145 continue;
c5aa993b 1146
8defab1a
DJ
1147 /* Offset within shadow_contents. */
1148 if (bp_addr < memaddr)
1149 {
1150 /* Only copy the second part of the breakpoint. */
1151 bp_size -= memaddr - bp_addr;
1152 bptoffset = memaddr - bp_addr;
1153 bp_addr = memaddr;
1154 }
c5aa993b 1155
8defab1a
DJ
1156 if (bp_addr + bp_size > memaddr + len)
1157 {
1158 /* Only copy the first part of the breakpoint. */
1159 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1160 }
c5aa993b 1161
8defab1a 1162 memcpy (buf + bp_addr - memaddr,
35df4500 1163 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1164 }
c906108c 1165}
c906108c 1166\f
c5aa993b 1167
60e1c644
PA
1168/* Return true if BPT is of any hardware watchpoint kind. */
1169
a5606eee 1170static int
d77f58be 1171is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1172{
1173 return (bpt->type == bp_hardware_watchpoint
1174 || bpt->type == bp_read_watchpoint
1175 || bpt->type == bp_access_watchpoint);
1176}
7270d8f2 1177
60e1c644
PA
1178/* Return true if BPT is of any watchpoint kind, hardware or
1179 software. */
1180
3a5c3e22 1181int
d77f58be 1182is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1183{
1184 return (is_hardware_watchpoint (bpt)
1185 || bpt->type == bp_watchpoint);
1186}
1187
3a5c3e22
PA
1188/* Returns true if the current thread and its running state are safe
1189 to evaluate or update watchpoint B. Watchpoints on local
1190 expressions need to be evaluated in the context of the thread that
1191 was current when the watchpoint was created, and, that thread needs
1192 to be stopped to be able to select the correct frame context.
1193 Watchpoints on global expressions can be evaluated on any thread,
1194 and in any state. It is presently left to the target allowing
1195 memory accesses when threads are running. */
f6bc2008
PA
1196
1197static int
3a5c3e22 1198watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008
PA
1199{
1200 return (ptid_equal (b->watchpoint_thread, null_ptid)
1201 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1202 && !is_executing (inferior_ptid)));
1203}
1204
d0fb5eae
JK
1205/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1206 associated bp_watchpoint_scope breakpoint. */
1207
1208static void
3a5c3e22 1209watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1210{
3a5c3e22 1211 struct breakpoint *b = &w->base;
d0fb5eae
JK
1212
1213 if (b->related_breakpoint != b)
1214 {
1215 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1216 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1217 b->related_breakpoint->disposition = disp_del_at_next_stop;
1218 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1219 b->related_breakpoint = b;
1220 }
1221 b->disposition = disp_del_at_next_stop;
1222}
1223
567e1b4e
JB
1224/* Assuming that B is a watchpoint:
1225 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1226 - Evaluate expression and store the result in B->val
567e1b4e
JB
1227 - Evaluate the condition if there is one, and store the result
1228 in b->loc->cond.
a5606eee
VP
1229 - Update the list of values that must be watched in B->loc.
1230
4a64f543
MS
1231 If the watchpoint disposition is disp_del_at_next_stop, then do
1232 nothing. If this is local watchpoint that is out of scope, delete
1233 it.
1234
1235 Even with `set breakpoint always-inserted on' the watchpoints are
1236 removed + inserted on each stop here. Normal breakpoints must
1237 never be removed because they might be missed by a running thread
1238 when debugging in non-stop mode. On the other hand, hardware
1239 watchpoints (is_hardware_watchpoint; processed here) are specific
1240 to each LWP since they are stored in each LWP's hardware debug
1241 registers. Therefore, such LWP must be stopped first in order to
1242 be able to modify its hardware watchpoints.
1243
1244 Hardware watchpoints must be reset exactly once after being
1245 presented to the user. It cannot be done sooner, because it would
1246 reset the data used to present the watchpoint hit to the user. And
1247 it must not be done later because it could display the same single
1248 watchpoint hit during multiple GDB stops. Note that the latter is
1249 relevant only to the hardware watchpoint types bp_read_watchpoint
1250 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1251 not user-visible - its hit is suppressed if the memory content has
1252 not changed.
1253
1254 The following constraints influence the location where we can reset
1255 hardware watchpoints:
1256
1257 * target_stopped_by_watchpoint and target_stopped_data_address are
1258 called several times when GDB stops.
1259
1260 [linux]
1261 * Multiple hardware watchpoints can be hit at the same time,
1262 causing GDB to stop. GDB only presents one hardware watchpoint
1263 hit at a time as the reason for stopping, and all the other hits
1264 are presented later, one after the other, each time the user
1265 requests the execution to be resumed. Execution is not resumed
1266 for the threads still having pending hit event stored in
1267 LWP_INFO->STATUS. While the watchpoint is already removed from
1268 the inferior on the first stop the thread hit event is kept being
1269 reported from its cached value by linux_nat_stopped_data_address
1270 until the real thread resume happens after the watchpoint gets
1271 presented and thus its LWP_INFO->STATUS gets reset.
1272
1273 Therefore the hardware watchpoint hit can get safely reset on the
1274 watchpoint removal from inferior. */
a79d3c27 1275
b40ce68a 1276static void
3a5c3e22 1277update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1278{
a5606eee 1279 int within_current_scope;
a5606eee 1280 struct frame_id saved_frame_id;
66076460 1281 int frame_saved;
a5606eee 1282
f6bc2008
PA
1283 /* If this is a local watchpoint, we only want to check if the
1284 watchpoint frame is in scope if the current thread is the thread
1285 that was used to create the watchpoint. */
1286 if (!watchpoint_in_thread_scope (b))
1287 return;
1288
3a5c3e22 1289 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1290 return;
1291
66076460 1292 frame_saved = 0;
a5606eee
VP
1293
1294 /* Determine if the watchpoint is within scope. */
1295 if (b->exp_valid_block == NULL)
1296 within_current_scope = 1;
1297 else
1298 {
b5db5dfc
UW
1299 struct frame_info *fi = get_current_frame ();
1300 struct gdbarch *frame_arch = get_frame_arch (fi);
1301 CORE_ADDR frame_pc = get_frame_pc (fi);
1302
1303 /* If we're in a function epilogue, unwinding may not work
1304 properly, so do not attempt to recreate locations at this
1305 point. See similar comments in watchpoint_check. */
1306 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1307 return;
66076460
DJ
1308
1309 /* Save the current frame's ID so we can restore it after
1310 evaluating the watchpoint expression on its own frame. */
1311 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1312 took a frame parameter, so that we didn't have to change the
1313 selected frame. */
1314 frame_saved = 1;
1315 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1316
a5606eee
VP
1317 fi = frame_find_by_id (b->watchpoint_frame);
1318 within_current_scope = (fi != NULL);
1319 if (within_current_scope)
1320 select_frame (fi);
1321 }
1322
b5db5dfc
UW
1323 /* We don't free locations. They are stored in the bp_location array
1324 and update_global_location_list will eventually delete them and
1325 remove breakpoints if needed. */
3a5c3e22 1326 b->base.loc = NULL;
b5db5dfc 1327
a5606eee
VP
1328 if (within_current_scope && reparse)
1329 {
1330 char *s;
d63d0675 1331
a5606eee
VP
1332 if (b->exp)
1333 {
1334 xfree (b->exp);
1335 b->exp = NULL;
1336 }
d63d0675 1337 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1338 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1339 /* If the meaning of expression itself changed, the old value is
1340 no longer relevant. We don't want to report a watchpoint hit
1341 to the user when the old value and the new value may actually
1342 be completely different objects. */
1343 value_free (b->val);
fa4727a6
DJ
1344 b->val = NULL;
1345 b->val_valid = 0;
60e1c644
PA
1346
1347 /* Note that unlike with breakpoints, the watchpoint's condition
1348 expression is stored in the breakpoint object, not in the
1349 locations (re)created below. */
3a5c3e22 1350 if (b->base.cond_string != NULL)
60e1c644
PA
1351 {
1352 if (b->cond_exp != NULL)
1353 {
1354 xfree (b->cond_exp);
1355 b->cond_exp = NULL;
1356 }
1357
3a5c3e22 1358 s = b->base.cond_string;
60e1c644
PA
1359 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1360 }
a5606eee 1361 }
a5606eee
VP
1362
1363 /* If we failed to parse the expression, for example because
1364 it refers to a global variable in a not-yet-loaded shared library,
1365 don't try to insert watchpoint. We don't automatically delete
1366 such watchpoint, though, since failure to parse expression
1367 is different from out-of-scope watchpoint. */
2d134ed3
PA
1368 if ( !target_has_execution)
1369 {
1370 /* Without execution, memory can't change. No use to try and
1371 set watchpoint locations. The watchpoint will be reset when
1372 the target gains execution, through breakpoint_re_set. */
1373 }
1374 else if (within_current_scope && b->exp)
a5606eee 1375 {
0cf6dd15 1376 int pc = 0;
fa4727a6 1377 struct value *val_chain, *v, *result, *next;
2d134ed3 1378 struct program_space *frame_pspace;
a5606eee 1379
0cf6dd15 1380 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1381
a5606eee
VP
1382 /* Avoid setting b->val if it's already set. The meaning of
1383 b->val is 'the last value' user saw, and we should update
1384 it only if we reported that last value to user. As it
9c06b0b4
TJB
1385 happens, the code that reports it updates b->val directly.
1386 We don't keep track of the memory value for masked
1387 watchpoints. */
3a5c3e22 1388 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1389 {
1390 b->val = v;
1391 b->val_valid = 1;
1392 }
a5606eee 1393
2d134ed3
PA
1394 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1395
a5606eee 1396 /* Look at each value on the value chain. */
9fa40276 1397 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1398 {
1399 /* If it's a memory location, and GDB actually needed
1400 its contents to evaluate the expression, then we
fa4727a6
DJ
1401 must watch it. If the first value returned is
1402 still lazy, that means an error occurred reading it;
1403 watch it anyway in case it becomes readable. */
a5606eee 1404 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1405 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1406 {
1407 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1408
a5606eee
VP
1409 /* We only watch structs and arrays if user asked
1410 for it explicitly, never if they just happen to
1411 appear in the middle of some value chain. */
fa4727a6 1412 if (v == result
a5606eee
VP
1413 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1414 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1415 {
1416 CORE_ADDR addr;
1417 int len, type;
1418 struct bp_location *loc, **tmp;
1419
42ae5230 1420 addr = value_address (v);
a5606eee
VP
1421 len = TYPE_LENGTH (value_type (v));
1422 type = hw_write;
3a5c3e22 1423 if (b->base.type == bp_read_watchpoint)
a5606eee 1424 type = hw_read;
3a5c3e22 1425 else if (b->base.type == bp_access_watchpoint)
a5606eee 1426 type = hw_access;
3a5c3e22
PA
1427
1428 loc = allocate_bp_location (&b->base);
1429 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1430 ;
1431 *tmp = loc;
a6d9a66e 1432 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1433
1434 loc->pspace = frame_pspace;
a5606eee
VP
1435 loc->address = addr;
1436 loc->length = len;
1437 loc->watchpoint_type = type;
1438 }
1439 }
9fa40276
TJB
1440 }
1441
1442 /* Change the type of breakpoint between hardware assisted or
1443 an ordinary watchpoint depending on the hardware support
1444 and free hardware slots. REPARSE is set when the inferior
1445 is started. */
a9634178 1446 if (reparse)
9fa40276 1447 {
e09342b5 1448 int reg_cnt;
9fa40276
TJB
1449 enum bp_loc_type loc_type;
1450 struct bp_location *bl;
a5606eee 1451
a9634178 1452 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1453
1454 if (reg_cnt)
9fa40276
TJB
1455 {
1456 int i, target_resources_ok, other_type_used;
a1398e0c 1457 enum bptype type;
9fa40276 1458
a9634178
TJB
1459 /* Use an exact watchpoint when there's only one memory region to be
1460 watched, and only one debug register is needed to watch it. */
1461 b->exact = target_exact_watchpoints && reg_cnt == 1;
1462
9fa40276 1463 /* We need to determine how many resources are already
e09342b5
TJB
1464 used for all other hardware watchpoints plus this one
1465 to see if we still have enough resources to also fit
a1398e0c
PA
1466 this watchpoint in as well. */
1467
1468 /* If this is a software watchpoint, we try to turn it
1469 to a hardware one -- count resources as if B was of
1470 hardware watchpoint type. */
1471 type = b->base.type;
1472 if (type == bp_watchpoint)
1473 type = bp_hardware_watchpoint;
1474
1475 /* This watchpoint may or may not have been placed on
1476 the list yet at this point (it won't be in the list
1477 if we're trying to create it for the first time,
1478 through watch_command), so always account for it
1479 manually. */
1480
1481 /* Count resources used by all watchpoints except B. */
1482 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1483
1484 /* Add in the resources needed for B. */
1485 i += hw_watchpoint_use_count (&b->base);
1486
1487 target_resources_ok
1488 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1489 if (target_resources_ok <= 0)
a9634178 1490 {
3a5c3e22 1491 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1492
1493 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1494 error (_("Target does not support this type of "
1495 "hardware watchpoint."));
9c06b0b4
TJB
1496 else if (target_resources_ok < 0 && !sw_mode)
1497 error (_("There are not enough available hardware "
1498 "resources for this watchpoint."));
a1398e0c
PA
1499
1500 /* Downgrade to software watchpoint. */
1501 b->base.type = bp_watchpoint;
1502 }
1503 else
1504 {
1505 /* If this was a software watchpoint, we've just
1506 found we have enough resources to turn it to a
1507 hardware watchpoint. Otherwise, this is a
1508 nop. */
1509 b->base.type = type;
a9634178 1510 }
9fa40276 1511 }
3a5c3e22 1512 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1513 error (_("Expression cannot be implemented with "
1514 "read/access watchpoint."));
9fa40276 1515 else
3a5c3e22 1516 b->base.type = bp_watchpoint;
9fa40276 1517
3a5c3e22 1518 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1519 : bp_loc_hardware_watchpoint);
3a5c3e22 1520 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1521 bl->loc_type = loc_type;
1522 }
1523
1524 for (v = val_chain; v; v = next)
1525 {
a5606eee
VP
1526 next = value_next (v);
1527 if (v != b->val)
1528 value_free (v);
1529 }
1530
c7437ca6
PA
1531 /* If a software watchpoint is not watching any memory, then the
1532 above left it without any location set up. But,
1533 bpstat_stop_status requires a location to be able to report
1534 stops, so make sure there's at least a dummy one. */
3a5c3e22 1535 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1536 {
3a5c3e22
PA
1537 struct breakpoint *base = &b->base;
1538 base->loc = allocate_bp_location (base);
1539 base->loc->pspace = frame_pspace;
1540 base->loc->address = -1;
1541 base->loc->length = -1;
1542 base->loc->watchpoint_type = -1;
c7437ca6 1543 }
a5606eee
VP
1544 }
1545 else if (!within_current_scope)
7270d8f2 1546 {
ac74f770
MS
1547 printf_filtered (_("\
1548Watchpoint %d deleted because the program has left the block\n\
1549in which its expression is valid.\n"),
3a5c3e22 1550 b->base.number);
d0fb5eae 1551 watchpoint_del_at_next_stop (b);
7270d8f2 1552 }
a5606eee
VP
1553
1554 /* Restore the selected frame. */
66076460
DJ
1555 if (frame_saved)
1556 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1557}
1558
a5606eee 1559
74960c60 1560/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1561 inserted in the inferior. We don't differentiate the type of BL's owner
1562 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1563 breakpoint_ops is not defined, because in insert_bp_location,
1564 tracepoint's insert_location will not be called. */
74960c60 1565static int
35df4500 1566should_be_inserted (struct bp_location *bl)
74960c60 1567{
35df4500 1568 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1569 return 0;
1570
35df4500 1571 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1572 return 0;
1573
35df4500 1574 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1575 return 0;
1576
56710373
PA
1577 /* This is set for example, when we're attached to the parent of a
1578 vfork, and have detached from the child. The child is running
1579 free, and we expect it to do an exec or exit, at which point the
1580 OS makes the parent schedulable again (and the target reports
1581 that the vfork is done). Until the child is done with the shared
1582 memory region, do not insert breakpoints in the parent, otherwise
1583 the child could still trip on the parent's breakpoints. Since
1584 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1585 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1586 return 0;
1587
74960c60
VP
1588 return 1;
1589}
1590
934709f0
PW
1591/* Same as should_be_inserted but does the check assuming
1592 that the location is not duplicated. */
1593
1594static int
1595unduplicated_should_be_inserted (struct bp_location *bl)
1596{
1597 int result;
1598 const int save_duplicate = bl->duplicate;
1599
1600 bl->duplicate = 0;
1601 result = should_be_inserted (bl);
1602 bl->duplicate = save_duplicate;
1603 return result;
1604}
1605
35df4500
TJB
1606/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1607 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 1608 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
1609 Returns 0 for success, 1 if the bp_location type is not supported or
1610 -1 for failure.
879bfdc2 1611
4a64f543
MS
1612 NOTE drow/2003-09-09: This routine could be broken down to an
1613 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1614static int
35df4500 1615insert_bp_location (struct bp_location *bl,
26bb91f3 1616 struct ui_file *tmp_error_stream,
3fbb6ffa 1617 int *disabled_breaks,
26bb91f3 1618 int *hw_breakpoint_error)
879bfdc2
DJ
1619{
1620 int val = 0;
1621
35df4500 1622 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1623 return 0;
1624
8181d85f 1625 /* Initialize the target-specific information. */
35df4500
TJB
1626 memset (&bl->target_info, 0, sizeof (bl->target_info));
1627 bl->target_info.placed_address = bl->address;
1628 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1629 bl->target_info.length = bl->length;
8181d85f 1630
35df4500
TJB
1631 if (bl->loc_type == bp_loc_software_breakpoint
1632 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1633 {
35df4500 1634 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1635 {
1636 /* If the explicitly specified breakpoint type
1637 is not hardware breakpoint, check the memory map to see
1638 if the breakpoint address is in read only memory or not.
4a64f543 1639
765dc015
VP
1640 Two important cases are:
1641 - location type is not hardware breakpoint, memory
1642 is readonly. We change the type of the location to
1643 hardware breakpoint.
4a64f543
MS
1644 - location type is hardware breakpoint, memory is
1645 read-write. This means we've previously made the
1646 location hardware one, but then the memory map changed,
1647 so we undo.
765dc015 1648
4a64f543
MS
1649 When breakpoints are removed, remove_breakpoints will use
1650 location types we've just set here, the only possible
1651 problem is that memory map has changed during running
1652 program, but it's not going to work anyway with current
1653 gdb. */
765dc015 1654 struct mem_region *mr
35df4500 1655 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1656
1657 if (mr)
1658 {
1659 if (automatic_hardware_breakpoints)
1660 {
765dc015
VP
1661 enum bp_loc_type new_type;
1662
1663 if (mr->attrib.mode != MEM_RW)
1664 new_type = bp_loc_hardware_breakpoint;
1665 else
1666 new_type = bp_loc_software_breakpoint;
1667
35df4500 1668 if (new_type != bl->loc_type)
765dc015
VP
1669 {
1670 static int said = 0;
cc59ec59 1671
35df4500 1672 bl->loc_type = new_type;
765dc015
VP
1673 if (!said)
1674 {
3e43a32a
MS
1675 fprintf_filtered (gdb_stdout,
1676 _("Note: automatically using "
1677 "hardware breakpoints for "
1678 "read-only addresses.\n"));
765dc015
VP
1679 said = 1;
1680 }
1681 }
1682 }
35df4500 1683 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1684 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1685 warning (_("cannot set software breakpoint "
1686 "at readonly address %s"),
35df4500 1687 paddress (bl->gdbarch, bl->address));
765dc015
VP
1688 }
1689 }
1690
879bfdc2
DJ
1691 /* First check to see if we have to handle an overlay. */
1692 if (overlay_debugging == ovly_off
35df4500
TJB
1693 || bl->section == NULL
1694 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1695 {
1696 /* No overlay handling: just set the breakpoint. */
1697
348d480f 1698 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1699 }
1700 else
1701 {
4a64f543 1702 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1703 Shall we set a breakpoint at the LMA? */
1704 if (!overlay_events_enabled)
1705 {
1706 /* Yes -- overlay event support is not active,
1707 so we must try to set a breakpoint at the LMA.
1708 This will not work for a hardware breakpoint. */
35df4500 1709 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1710 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1711 bl->owner->number);
879bfdc2
DJ
1712 else
1713 {
35df4500
TJB
1714 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1715 bl->section);
879bfdc2 1716 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1717 bl->overlay_target_info = bl->target_info;
1718 bl->overlay_target_info.placed_address = addr;
1719 val = target_insert_breakpoint (bl->gdbarch,
1720 &bl->overlay_target_info);
879bfdc2 1721 if (val != 0)
99361f52 1722 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1723 "Overlay breakpoint %d "
1724 "failed: in ROM?\n",
35df4500 1725 bl->owner->number);
879bfdc2
DJ
1726 }
1727 }
1728 /* Shall we set a breakpoint at the VMA? */
35df4500 1729 if (section_is_mapped (bl->section))
879bfdc2
DJ
1730 {
1731 /* Yes. This overlay section is mapped into memory. */
348d480f 1732 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1733 }
1734 else
1735 {
1736 /* No. This breakpoint will not be inserted.
1737 No error, but do not mark the bp as 'inserted'. */
1738 return 0;
1739 }
1740 }
1741
1742 if (val)
1743 {
1744 /* Can't set the breakpoint. */
35df4500 1745 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1746 {
4a64f543 1747 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1748 val = 0;
35df4500 1749 bl->shlib_disabled = 1;
8d3788bd 1750 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
1751 if (!*disabled_breaks)
1752 {
1753 fprintf_unfiltered (tmp_error_stream,
1754 "Cannot insert breakpoint %d.\n",
1755 bl->owner->number);
1756 fprintf_unfiltered (tmp_error_stream,
1757 "Temporarily disabling shared "
1758 "library breakpoints:\n");
1759 }
1760 *disabled_breaks = 1;
879bfdc2 1761 fprintf_unfiltered (tmp_error_stream,
35df4500 1762 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1763 }
1764 else
879bfdc2 1765 {
35df4500 1766 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1767 {
1768 *hw_breakpoint_error = 1;
3e43a32a
MS
1769 fprintf_unfiltered (tmp_error_stream,
1770 "Cannot insert hardware "
1771 "breakpoint %d.\n",
35df4500 1772 bl->owner->number);
879bfdc2
DJ
1773 }
1774 else
1775 {
1776 fprintf_unfiltered (tmp_error_stream,
1777 "Cannot insert breakpoint %d.\n",
35df4500 1778 bl->owner->number);
879bfdc2
DJ
1779 fprintf_filtered (tmp_error_stream,
1780 "Error accessing memory address ");
35df4500 1781 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1782 tmp_error_stream);
879bfdc2
DJ
1783 fprintf_filtered (tmp_error_stream, ": %s.\n",
1784 safe_strerror (val));
1785 }
1786
1787 }
1788 }
1789 else
35df4500 1790 bl->inserted = 1;
879bfdc2
DJ
1791
1792 return val;
1793 }
1794
35df4500 1795 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1796 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1797 watchpoints. It's not clear that it's necessary... */
35df4500 1798 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1799 {
77b06cd7
TJB
1800 gdb_assert (bl->owner->ops != NULL
1801 && bl->owner->ops->insert_location != NULL);
1802
1803 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1804
1805 /* If trying to set a read-watchpoint, and it turns out it's not
1806 supported, try emulating one with an access watchpoint. */
35df4500 1807 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1808 {
1809 struct bp_location *loc, **loc_temp;
1810
1811 /* But don't try to insert it, if there's already another
1812 hw_access location that would be considered a duplicate
1813 of this one. */
1814 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1815 if (loc != bl
85d721b8 1816 && loc->watchpoint_type == hw_access
35df4500 1817 && watchpoint_locations_match (bl, loc))
85d721b8 1818 {
35df4500
TJB
1819 bl->duplicate = 1;
1820 bl->inserted = 1;
1821 bl->target_info = loc->target_info;
1822 bl->watchpoint_type = hw_access;
85d721b8
PA
1823 val = 0;
1824 break;
1825 }
1826
1827 if (val == 1)
1828 {
77b06cd7
TJB
1829 bl->watchpoint_type = hw_access;
1830 val = bl->owner->ops->insert_location (bl);
1831
1832 if (val)
1833 /* Back to the original value. */
1834 bl->watchpoint_type = hw_read;
85d721b8
PA
1835 }
1836 }
1837
35df4500 1838 bl->inserted = (val == 0);
879bfdc2
DJ
1839 }
1840
35df4500 1841 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1842 {
77b06cd7
TJB
1843 gdb_assert (bl->owner->ops != NULL
1844 && bl->owner->ops->insert_location != NULL);
1845
1846 val = bl->owner->ops->insert_location (bl);
1847 if (val)
1848 {
1849 bl->owner->enable_state = bp_disabled;
1850
1851 if (val == 1)
1852 warning (_("\
1853Error inserting catchpoint %d: Your system does not support this type\n\
1854of catchpoint."), bl->owner->number);
1855 else
1856 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1857 }
1858
1859 bl->inserted = (val == 0);
1640b821
DJ
1860
1861 /* We've already printed an error message if there was a problem
1862 inserting this catchpoint, and we've disabled the catchpoint,
1863 so just return success. */
1864 return 0;
879bfdc2
DJ
1865 }
1866
1867 return 0;
1868}
1869
6c95b8df
PA
1870/* This function is called when program space PSPACE is about to be
1871 deleted. It takes care of updating breakpoints to not reference
1872 PSPACE anymore. */
1873
1874void
1875breakpoint_program_space_exit (struct program_space *pspace)
1876{
1877 struct breakpoint *b, *b_temp;
876fa593 1878 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1879
1880 /* Remove any breakpoint that was set through this program space. */
1881 ALL_BREAKPOINTS_SAFE (b, b_temp)
1882 {
1883 if (b->pspace == pspace)
1884 delete_breakpoint (b);
1885 }
1886
1887 /* Breakpoints set through other program spaces could have locations
1888 bound to PSPACE as well. Remove those. */
876fa593 1889 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1890 {
1891 struct bp_location *tmp;
1892
1893 if (loc->pspace == pspace)
1894 {
2bdf28a0 1895 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1896 if (loc->owner->loc == loc)
1897 loc->owner->loc = loc->next;
1898 else
1899 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1900 if (tmp->next == loc)
1901 {
1902 tmp->next = loc->next;
1903 break;
1904 }
1905 }
1906 }
1907
1908 /* Now update the global location list to permanently delete the
1909 removed locations above. */
1910 update_global_location_list (0);
1911}
1912
74960c60
VP
1913/* Make sure all breakpoints are inserted in inferior.
1914 Throws exception on any error.
1915 A breakpoint that is already inserted won't be inserted
1916 again, so calling this function twice is safe. */
1917void
1918insert_breakpoints (void)
1919{
1920 struct breakpoint *bpt;
1921
1922 ALL_BREAKPOINTS (bpt)
1923 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
1924 {
1925 struct watchpoint *w = (struct watchpoint *) bpt;
1926
1927 update_watchpoint (w, 0 /* don't reparse. */);
1928 }
74960c60 1929
b60e7edf 1930 update_global_location_list (1);
74960c60 1931
c35b1492
PA
1932 /* update_global_location_list does not insert breakpoints when
1933 always_inserted_mode is not enabled. Explicitly insert them
1934 now. */
1935 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1936 insert_breakpoint_locations ();
1937}
1938
c30eee59 1939/* Used when starting or continuing the program. */
c906108c 1940
74960c60
VP
1941static void
1942insert_breakpoint_locations (void)
c906108c 1943{
a5606eee 1944 struct breakpoint *bpt;
35df4500 1945 struct bp_location *bl, **blp_tmp;
e236ba44 1946 int error = 0;
c906108c 1947 int val = 0;
3fbb6ffa 1948 int disabled_breaks = 0;
81d0cc19 1949 int hw_breakpoint_error = 0;
c906108c 1950
81d0cc19 1951 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1952 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1953
81d0cc19
GS
1954 /* Explicitly mark the warning -- this will only be printed if
1955 there was an error. */
1956 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1957
1958 save_current_space_and_thread ();
1959
35df4500 1960 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1961 {
35df4500 1962 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1963 continue;
1964
4a64f543
MS
1965 /* There is no point inserting thread-specific breakpoints if
1966 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1967 has BL->OWNER always non-NULL. */
35df4500
TJB
1968 if (bl->owner->thread != -1
1969 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1970 continue;
1971
35df4500 1972 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1973
1974 /* For targets that support global breakpoints, there's no need
1975 to select an inferior to insert breakpoint to. In fact, even
1976 if we aren't attached to any process yet, we should still
1977 insert breakpoints. */
1978 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1979 && ptid_equal (inferior_ptid, null_ptid))
1980 continue;
1981
3fbb6ffa
TJB
1982 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
1983 &hw_breakpoint_error);
879bfdc2 1984 if (val)
e236ba44 1985 error = val;
879bfdc2 1986 }
c906108c 1987
4a64f543
MS
1988 /* If we failed to insert all locations of a watchpoint, remove
1989 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
1990 ALL_BREAKPOINTS (bpt)
1991 {
1992 int some_failed = 0;
1993 struct bp_location *loc;
1994
1995 if (!is_hardware_watchpoint (bpt))
1996 continue;
1997
d6b74ac4 1998 if (!breakpoint_enabled (bpt))
a5606eee 1999 continue;
74960c60
VP
2000
2001 if (bpt->disposition == disp_del_at_next_stop)
2002 continue;
a5606eee
VP
2003
2004 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2005 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2006 {
2007 some_failed = 1;
2008 break;
2009 }
2010 if (some_failed)
2011 {
2012 for (loc = bpt->loc; loc; loc = loc->next)
2013 if (loc->inserted)
2014 remove_breakpoint (loc, mark_uninserted);
2015
2016 hw_breakpoint_error = 1;
2017 fprintf_unfiltered (tmp_error_stream,
2018 "Could not insert hardware watchpoint %d.\n",
2019 bpt->number);
2020 error = -1;
2021 }
2022 }
2023
e236ba44 2024 if (error)
81d0cc19
GS
2025 {
2026 /* If a hardware breakpoint or watchpoint was inserted, add a
2027 message about possibly exhausted resources. */
879bfdc2 2028 if (hw_breakpoint_error)
81d0cc19 2029 {
c6510018
MS
2030 fprintf_unfiltered (tmp_error_stream,
2031 "Could not insert hardware breakpoints:\n\
2032You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2033 }
81d0cc19
GS
2034 target_terminal_ours_for_output ();
2035 error_stream (tmp_error_stream);
2036 }
f7545552
TT
2037
2038 do_cleanups (cleanups);
c906108c
SS
2039}
2040
c30eee59
TJB
2041/* Used when the program stops.
2042 Returns zero if successful, or non-zero if there was a problem
2043 removing a breakpoint location. */
2044
c906108c 2045int
fba45db2 2046remove_breakpoints (void)
c906108c 2047{
35df4500 2048 struct bp_location *bl, **blp_tmp;
3a1bae8e 2049 int val = 0;
c906108c 2050
35df4500 2051 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2052 {
1e4d1764 2053 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2054 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2055 }
3a1bae8e 2056 return val;
c906108c
SS
2057}
2058
6c95b8df
PA
2059/* Remove breakpoints of process PID. */
2060
2061int
2062remove_breakpoints_pid (int pid)
2063{
35df4500 2064 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2065 int val;
2066 struct inferior *inf = find_inferior_pid (pid);
2067
35df4500 2068 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2069 {
35df4500 2070 if (bl->pspace != inf->pspace)
6c95b8df
PA
2071 continue;
2072
35df4500 2073 if (bl->inserted)
6c95b8df 2074 {
35df4500 2075 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2076 if (val != 0)
2077 return val;
2078 }
2079 }
2080 return 0;
2081}
2082
c906108c 2083int
fba45db2 2084reattach_breakpoints (int pid)
c906108c 2085{
6c95b8df 2086 struct cleanup *old_chain;
35df4500 2087 struct bp_location *bl, **blp_tmp;
c906108c 2088 int val;
86b887df 2089 struct ui_file *tmp_error_stream;
3fbb6ffa 2090 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2091 struct inferior *inf;
2092 struct thread_info *tp;
2093
2094 tp = any_live_thread_of_process (pid);
2095 if (tp == NULL)
2096 return 1;
2097
2098 inf = find_inferior_pid (pid);
2099 old_chain = save_inferior_ptid ();
2100
2101 inferior_ptid = tp->ptid;
a4954f26 2102
86b887df 2103 tmp_error_stream = mem_fileopen ();
a4954f26 2104 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2105
35df4500 2106 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2107 {
35df4500 2108 if (bl->pspace != inf->pspace)
6c95b8df
PA
2109 continue;
2110
35df4500 2111 if (bl->inserted)
c5aa993b 2112 {
35df4500 2113 bl->inserted = 0;
3fbb6ffa 2114 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2115 if (val != 0)
2116 {
ce696e05 2117 do_cleanups (old_chain);
c5aa993b
JM
2118 return val;
2119 }
2120 }
2121 }
ce696e05 2122 do_cleanups (old_chain);
c906108c
SS
2123 return 0;
2124}
2125
e58b0e63
PA
2126static int internal_breakpoint_number = -1;
2127
84f4c1fe
PM
2128/* Set the breakpoint number of B, depending on the value of INTERNAL.
2129 If INTERNAL is non-zero, the breakpoint number will be populated
2130 from internal_breakpoint_number and that variable decremented.
e5dd4106 2131 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2132 breakpoint_count and that value incremented. Internal breakpoints
2133 do not set the internal var bpnum. */
2134static void
2135set_breakpoint_number (int internal, struct breakpoint *b)
2136{
2137 if (internal)
2138 b->number = internal_breakpoint_number--;
2139 else
2140 {
2141 set_breakpoint_count (breakpoint_count + 1);
2142 b->number = breakpoint_count;
2143 }
2144}
2145
e62c965a 2146static struct breakpoint *
a6d9a66e 2147create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2148 CORE_ADDR address, enum bptype type,
c0a91b2b 2149 const struct breakpoint_ops *ops)
e62c965a 2150{
e62c965a
PP
2151 struct symtab_and_line sal;
2152 struct breakpoint *b;
2153
4a64f543 2154 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2155
2156 sal.pc = address;
2157 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2158 sal.pspace = current_program_space;
e62c965a 2159
06edf0c0 2160 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2161 b->number = internal_breakpoint_number--;
2162 b->disposition = disp_donttouch;
2163
2164 return b;
2165}
2166
17450429
PP
2167static const char *const longjmp_names[] =
2168 {
2169 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2170 };
2171#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2172
2173/* Per-objfile data private to breakpoint.c. */
2174struct breakpoint_objfile_data
2175{
2176 /* Minimal symbol for "_ovly_debug_event" (if any). */
2177 struct minimal_symbol *overlay_msym;
2178
2179 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2180 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2181
2182 /* Minimal symbol for "std::terminate()" (if any). */
2183 struct minimal_symbol *terminate_msym;
2184
2185 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2186 struct minimal_symbol *exception_msym;
2187};
2188
2189static const struct objfile_data *breakpoint_objfile_key;
2190
2191/* Minimal symbol not found sentinel. */
2192static struct minimal_symbol msym_not_found;
2193
2194/* Returns TRUE if MSYM point to the "not found" sentinel. */
2195
2196static int
2197msym_not_found_p (const struct minimal_symbol *msym)
2198{
2199 return msym == &msym_not_found;
2200}
2201
2202/* Return per-objfile data needed by breakpoint.c.
2203 Allocate the data if necessary. */
2204
2205static struct breakpoint_objfile_data *
2206get_breakpoint_objfile_data (struct objfile *objfile)
2207{
2208 struct breakpoint_objfile_data *bp_objfile_data;
2209
2210 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2211 if (bp_objfile_data == NULL)
2212 {
2213 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2214 sizeof (*bp_objfile_data));
2215
2216 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2217 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2218 }
2219 return bp_objfile_data;
2220}
2221
e62c965a 2222static void
af02033e 2223create_overlay_event_breakpoint (void)
e62c965a 2224{
69de3c6a 2225 struct objfile *objfile;
af02033e 2226 const char *const func_name = "_ovly_debug_event";
e62c965a 2227
69de3c6a
PP
2228 ALL_OBJFILES (objfile)
2229 {
2230 struct breakpoint *b;
17450429
PP
2231 struct breakpoint_objfile_data *bp_objfile_data;
2232 CORE_ADDR addr;
69de3c6a 2233
17450429
PP
2234 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2235
2236 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2237 continue;
2238
2239 if (bp_objfile_data->overlay_msym == NULL)
2240 {
2241 struct minimal_symbol *m;
2242
2243 m = lookup_minimal_symbol_text (func_name, objfile);
2244 if (m == NULL)
2245 {
2246 /* Avoid future lookups in this objfile. */
2247 bp_objfile_data->overlay_msym = &msym_not_found;
2248 continue;
2249 }
2250 bp_objfile_data->overlay_msym = m;
2251 }
e62c965a 2252
17450429
PP
2253 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2254 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2255 bp_overlay_event,
2256 &internal_breakpoint_ops);
69de3c6a 2257 b->addr_string = xstrdup (func_name);
e62c965a 2258
69de3c6a
PP
2259 if (overlay_debugging == ovly_auto)
2260 {
2261 b->enable_state = bp_enabled;
2262 overlay_events_enabled = 1;
2263 }
2264 else
2265 {
2266 b->enable_state = bp_disabled;
2267 overlay_events_enabled = 0;
2268 }
e62c965a
PP
2269 }
2270 update_global_location_list (1);
2271}
2272
0fd8e87f 2273static void
af02033e 2274create_longjmp_master_breakpoint (void)
0fd8e87f 2275{
6c95b8df 2276 struct program_space *pspace;
6c95b8df
PA
2277 struct cleanup *old_chain;
2278
2279 old_chain = save_current_program_space ();
0fd8e87f 2280
6c95b8df 2281 ALL_PSPACES (pspace)
af02033e
PP
2282 {
2283 struct objfile *objfile;
2284
2285 set_current_program_space (pspace);
2286
2287 ALL_OBJFILES (objfile)
0fd8e87f 2288 {
af02033e
PP
2289 int i;
2290 struct gdbarch *gdbarch;
17450429 2291 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2292
af02033e
PP
2293 gdbarch = get_objfile_arch (objfile);
2294 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2295 continue;
2296
17450429
PP
2297 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2298
2299 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2300 {
2301 struct breakpoint *b;
af02033e 2302 const char *func_name;
17450429 2303 CORE_ADDR addr;
6c95b8df 2304
17450429 2305 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2306 continue;
0fd8e87f 2307
17450429
PP
2308 func_name = longjmp_names[i];
2309 if (bp_objfile_data->longjmp_msym[i] == NULL)
2310 {
2311 struct minimal_symbol *m;
2312
2313 m = lookup_minimal_symbol_text (func_name, objfile);
2314 if (m == NULL)
2315 {
2316 /* Prevent future lookups in this objfile. */
2317 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2318 continue;
2319 }
2320 bp_objfile_data->longjmp_msym[i] = m;
2321 }
2322
2323 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2324 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2325 &internal_breakpoint_ops);
af02033e
PP
2326 b->addr_string = xstrdup (func_name);
2327 b->enable_state = bp_disabled;
2328 }
0fd8e87f 2329 }
af02033e 2330 }
0fd8e87f 2331 update_global_location_list (1);
6c95b8df
PA
2332
2333 do_cleanups (old_chain);
0fd8e87f
UW
2334}
2335
af02033e 2336/* Create a master std::terminate breakpoint. */
aa7d318d 2337static void
af02033e 2338create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2339{
2340 struct program_space *pspace;
aa7d318d 2341 struct cleanup *old_chain;
af02033e 2342 const char *const func_name = "std::terminate()";
aa7d318d
TT
2343
2344 old_chain = save_current_program_space ();
2345
2346 ALL_PSPACES (pspace)
17450429
PP
2347 {
2348 struct objfile *objfile;
2349 CORE_ADDR addr;
2350
2351 set_current_program_space (pspace);
2352
aa7d318d
TT
2353 ALL_OBJFILES (objfile)
2354 {
2355 struct breakpoint *b;
17450429 2356 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2357
17450429 2358 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2359
17450429
PP
2360 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2361 continue;
2362
2363 if (bp_objfile_data->terminate_msym == NULL)
2364 {
2365 struct minimal_symbol *m;
2366
2367 m = lookup_minimal_symbol (func_name, NULL, objfile);
2368 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2369 && MSYMBOL_TYPE (m) != mst_file_text))
2370 {
2371 /* Prevent future lookups in this objfile. */
2372 bp_objfile_data->terminate_msym = &msym_not_found;
2373 continue;
2374 }
2375 bp_objfile_data->terminate_msym = m;
2376 }
aa7d318d 2377
17450429
PP
2378 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2379 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2380 bp_std_terminate_master,
2381 &internal_breakpoint_ops);
aa7d318d
TT
2382 b->addr_string = xstrdup (func_name);
2383 b->enable_state = bp_disabled;
2384 }
17450429
PP
2385 }
2386
aa7d318d
TT
2387 update_global_location_list (1);
2388
2389 do_cleanups (old_chain);
2390}
2391
186c406b
TT
2392/* Install a master breakpoint on the unwinder's debug hook. */
2393
2394void
2395create_exception_master_breakpoint (void)
2396{
2397 struct objfile *objfile;
17450429 2398 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2399
2400 ALL_OBJFILES (objfile)
2401 {
17450429
PP
2402 struct breakpoint *b;
2403 struct gdbarch *gdbarch;
2404 struct breakpoint_objfile_data *bp_objfile_data;
2405 CORE_ADDR addr;
2406
2407 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2408
2409 if (msym_not_found_p (bp_objfile_data->exception_msym))
2410 continue;
2411
2412 gdbarch = get_objfile_arch (objfile);
186c406b 2413
17450429 2414 if (bp_objfile_data->exception_msym == NULL)
186c406b 2415 {
17450429 2416 struct minimal_symbol *debug_hook;
186c406b 2417
17450429
PP
2418 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2419 if (debug_hook == NULL)
2420 {
2421 bp_objfile_data->exception_msym = &msym_not_found;
2422 continue;
2423 }
2424
2425 bp_objfile_data->exception_msym = debug_hook;
186c406b 2426 }
17450429
PP
2427
2428 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2429 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2430 &current_target);
06edf0c0
PA
2431 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2432 &internal_breakpoint_ops);
17450429
PP
2433 b->addr_string = xstrdup (func_name);
2434 b->enable_state = bp_disabled;
186c406b
TT
2435 }
2436
2437 update_global_location_list (1);
2438}
2439
c906108c 2440void
fba45db2 2441update_breakpoints_after_exec (void)
c906108c 2442{
35df4500 2443 struct breakpoint *b, *b_tmp;
876fa593 2444 struct bp_location *bploc, **bplocp_tmp;
c906108c 2445
25b22b0a
PA
2446 /* We're about to delete breakpoints from GDB's lists. If the
2447 INSERTED flag is true, GDB will try to lift the breakpoints by
2448 writing the breakpoints' "shadow contents" back into memory. The
2449 "shadow contents" are NOT valid after an exec, so GDB should not
2450 do that. Instead, the target is responsible from marking
2451 breakpoints out as soon as it detects an exec. We don't do that
2452 here instead, because there may be other attempts to delete
2453 breakpoints after detecting an exec and before reaching here. */
876fa593 2454 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2455 if (bploc->pspace == current_program_space)
2456 gdb_assert (!bploc->inserted);
c906108c 2457
35df4500 2458 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2459 {
6c95b8df
PA
2460 if (b->pspace != current_program_space)
2461 continue;
2462
4a64f543 2463 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2464 if (b->type == bp_shlib_event)
2465 {
2466 delete_breakpoint (b);
2467 continue;
2468 }
c906108c 2469
4a64f543 2470 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2471 if (b->type == bp_jit_event)
2472 {
2473 delete_breakpoint (b);
2474 continue;
2475 }
2476
1900040c 2477 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2478 as must overlay event and longjmp master breakpoints. */
2479 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2480 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2481 || b->type == bp_exception_master)
c4093a6a
JM
2482 {
2483 delete_breakpoint (b);
2484 continue;
2485 }
2486
4a64f543 2487 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 2488 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
2489 {
2490 delete_breakpoint (b);
2491 continue;
2492 }
2493
611c83ae
PA
2494 /* Longjmp and longjmp-resume breakpoints are also meaningless
2495 after an exec. */
186c406b
TT
2496 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2497 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2498 {
2499 delete_breakpoint (b);
2500 continue;
2501 }
2502
ce78b96d
JB
2503 if (b->type == bp_catchpoint)
2504 {
2505 /* For now, none of the bp_catchpoint breakpoints need to
2506 do anything at this point. In the future, if some of
2507 the catchpoints need to something, we will need to add
2508 a new method, and call this method from here. */
2509 continue;
2510 }
2511
c5aa993b
JM
2512 /* bp_finish is a special case. The only way we ought to be able
2513 to see one of these when an exec() has happened, is if the user
2514 caught a vfork, and then said "finish". Ordinarily a finish just
2515 carries them to the call-site of the current callee, by setting
2516 a temporary bp there and resuming. But in this case, the finish
2517 will carry them entirely through the vfork & exec.
2518
2519 We don't want to allow a bp_finish to remain inserted now. But
2520 we can't safely delete it, 'cause finish_command has a handle to
2521 the bp on a bpstat, and will later want to delete it. There's a
2522 chance (and I've seen it happen) that if we delete the bp_finish
2523 here, that its storage will get reused by the time finish_command
2524 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2525 We really must allow finish_command to delete a bp_finish.
2526
e5dd4106 2527 In the absence of a general solution for the "how do we know
53a5351d
JM
2528 it's safe to delete something others may have handles to?"
2529 problem, what we'll do here is just uninsert the bp_finish, and
2530 let finish_command delete it.
2531
2532 (We know the bp_finish is "doomed" in the sense that it's
2533 momentary, and will be deleted as soon as finish_command sees
2534 the inferior stopped. So it doesn't matter that the bp's
2535 address is probably bogus in the new a.out, unlike e.g., the
2536 solib breakpoints.) */
c5aa993b 2537
c5aa993b
JM
2538 if (b->type == bp_finish)
2539 {
2540 continue;
2541 }
2542
2543 /* Without a symbolic address, we have little hope of the
2544 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2545 a.out. */
c5aa993b
JM
2546 if (b->addr_string == NULL)
2547 {
2548 delete_breakpoint (b);
2549 continue;
2550 }
c5aa993b 2551 }
1900040c 2552 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2553 create_overlay_event_breakpoint ();
2554 create_longjmp_master_breakpoint ();
2555 create_std_terminate_master_breakpoint ();
186c406b 2556 create_exception_master_breakpoint ();
c906108c
SS
2557}
2558
2559int
fba45db2 2560detach_breakpoints (int pid)
c906108c 2561{
35df4500 2562 struct bp_location *bl, **blp_tmp;
3a1bae8e 2563 int val = 0;
ce696e05 2564 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2565 struct inferior *inf = current_inferior ();
c5aa993b 2566
39f77062 2567 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2568 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2569
6c95b8df 2570 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2571 inferior_ptid = pid_to_ptid (pid);
35df4500 2572 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2573 {
35df4500 2574 if (bl->pspace != inf->pspace)
6c95b8df
PA
2575 continue;
2576
35df4500
TJB
2577 if (bl->inserted)
2578 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2579 }
d03285ec
UW
2580
2581 /* Detach single-step breakpoints as well. */
2582 detach_single_step_breakpoints ();
2583
ce696e05 2584 do_cleanups (old_chain);
3a1bae8e 2585 return val;
c906108c
SS
2586}
2587
35df4500 2588/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2589 Note that this is used to detach breakpoints from a child fork.
2590 When we get here, the child isn't in the inferior list, and neither
2591 do we have objects to represent its address space --- we should
35df4500 2592 *not* look at bl->pspace->aspace here. */
6c95b8df 2593
c906108c 2594static int
35df4500 2595remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2596{
2597 int val;
c5aa993b 2598
35df4500
TJB
2599 /* BL is never in moribund_locations by our callers. */
2600 gdb_assert (bl->owner != NULL);
2bdf28a0 2601
35df4500 2602 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2603 /* Permanent breakpoints cannot be inserted or removed. */
2604 return 0;
2605
74960c60
VP
2606 /* The type of none suggests that owner is actually deleted.
2607 This should not ever happen. */
35df4500 2608 gdb_assert (bl->owner->type != bp_none);
0bde7532 2609
35df4500
TJB
2610 if (bl->loc_type == bp_loc_software_breakpoint
2611 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2612 {
c02f5703
MS
2613 /* "Normal" instruction breakpoint: either the standard
2614 trap-instruction bp (bp_breakpoint), or a
2615 bp_hardware_breakpoint. */
2616
2617 /* First check to see if we have to handle an overlay. */
2618 if (overlay_debugging == ovly_off
35df4500
TJB
2619 || bl->section == NULL
2620 || !(section_is_overlay (bl->section)))
c02f5703
MS
2621 {
2622 /* No overlay handling: just remove the breakpoint. */
348d480f 2623 val = bl->owner->ops->remove_location (bl);
c02f5703 2624 }
c906108c
SS
2625 else
2626 {
4a64f543 2627 /* This breakpoint is in an overlay section.
c02f5703
MS
2628 Did we set a breakpoint at the LMA? */
2629 if (!overlay_events_enabled)
2630 {
2631 /* Yes -- overlay event support is not active, so we
2632 should have set a breakpoint at the LMA. Remove it.
2633 */
c02f5703
MS
2634 /* Ignore any failures: if the LMA is in ROM, we will
2635 have already warned when we failed to insert it. */
35df4500
TJB
2636 if (bl->loc_type == bp_loc_hardware_breakpoint)
2637 target_remove_hw_breakpoint (bl->gdbarch,
2638 &bl->overlay_target_info);
c02f5703 2639 else
35df4500
TJB
2640 target_remove_breakpoint (bl->gdbarch,
2641 &bl->overlay_target_info);
c02f5703
MS
2642 }
2643 /* Did we set a breakpoint at the VMA?
2644 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2645 if (bl->inserted)
c906108c 2646 {
c02f5703
MS
2647 /* Yes -- remove it. Previously we did not bother to
2648 remove the breakpoint if the section had been
2649 unmapped, but let's not rely on that being safe. We
2650 don't know what the overlay manager might do. */
aa67235e
UW
2651
2652 /* However, we should remove *software* breakpoints only
2653 if the section is still mapped, or else we overwrite
2654 wrong code with the saved shadow contents. */
348d480f
PA
2655 if (bl->loc_type == bp_loc_hardware_breakpoint
2656 || section_is_mapped (bl->section))
2657 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
2658 else
2659 val = 0;
c906108c 2660 }
c02f5703
MS
2661 else
2662 {
2663 /* No -- not inserted, so no need to remove. No error. */
2664 val = 0;
2665 }
c906108c 2666 }
879d1e6b
UW
2667
2668 /* In some cases, we might not be able to remove a breakpoint
2669 in a shared library that has already been removed, but we
2670 have not yet processed the shlib unload event. */
35df4500 2671 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2672 val = 0;
2673
c906108c
SS
2674 if (val)
2675 return val;
35df4500 2676 bl->inserted = (is == mark_inserted);
c906108c 2677 }
35df4500 2678 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2679 {
77b06cd7
TJB
2680 gdb_assert (bl->owner->ops != NULL
2681 && bl->owner->ops->remove_location != NULL);
2682
35df4500 2683 bl->inserted = (is == mark_inserted);
77b06cd7 2684 bl->owner->ops->remove_location (bl);
2e70b7b9 2685
c906108c 2686 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2687 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2688 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2689 bl->owner->number);
c906108c 2690 }
35df4500
TJB
2691 else if (bl->owner->type == bp_catchpoint
2692 && breakpoint_enabled (bl->owner)
2693 && !bl->duplicate)
ce78b96d 2694 {
77b06cd7
TJB
2695 gdb_assert (bl->owner->ops != NULL
2696 && bl->owner->ops->remove_location != NULL);
ce78b96d 2697
77b06cd7 2698 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2699 if (val)
2700 return val;
77b06cd7 2701
35df4500 2702 bl->inserted = (is == mark_inserted);
ce78b96d 2703 }
c906108c
SS
2704
2705 return 0;
2706}
2707
6c95b8df 2708static int
35df4500 2709remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2710{
2711 int ret;
2712 struct cleanup *old_chain;
2713
35df4500
TJB
2714 /* BL is never in moribund_locations by our callers. */
2715 gdb_assert (bl->owner != NULL);
2bdf28a0 2716
35df4500 2717 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2718 /* Permanent breakpoints cannot be inserted or removed. */
2719 return 0;
2720
2721 /* The type of none suggests that owner is actually deleted.
2722 This should not ever happen. */
35df4500 2723 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2724
2725 old_chain = save_current_space_and_thread ();
2726
35df4500 2727 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2728
35df4500 2729 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2730
2731 do_cleanups (old_chain);
2732 return ret;
2733}
2734
c906108c
SS
2735/* Clear the "inserted" flag in all breakpoints. */
2736
25b22b0a 2737void
fba45db2 2738mark_breakpoints_out (void)
c906108c 2739{
35df4500 2740 struct bp_location *bl, **blp_tmp;
c906108c 2741
35df4500
TJB
2742 ALL_BP_LOCATIONS (bl, blp_tmp)
2743 if (bl->pspace == current_program_space)
2744 bl->inserted = 0;
c906108c
SS
2745}
2746
53a5351d
JM
2747/* Clear the "inserted" flag in all breakpoints and delete any
2748 breakpoints which should go away between runs of the program.
c906108c
SS
2749
2750 Plus other such housekeeping that has to be done for breakpoints
2751 between runs.
2752
53a5351d
JM
2753 Note: this function gets called at the end of a run (by
2754 generic_mourn_inferior) and when a run begins (by
4a64f543 2755 init_wait_for_inferior). */
c906108c
SS
2756
2757
2758
2759void
fba45db2 2760breakpoint_init_inferior (enum inf_context context)
c906108c 2761{
35df4500
TJB
2762 struct breakpoint *b, *b_tmp;
2763 struct bp_location *bl, **blp_tmp;
1c5cfe86 2764 int ix;
6c95b8df 2765 struct program_space *pspace = current_program_space;
c906108c 2766
50c71eaf
PA
2767 /* If breakpoint locations are shared across processes, then there's
2768 nothing to do. */
2567c7d9 2769 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2770 return;
2771
35df4500 2772 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2773 {
35df4500
TJB
2774 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2775 if (bl->pspace == pspace
2776 && bl->owner->enable_state != bp_permanent)
2777 bl->inserted = 0;
6c95b8df 2778 }
075f6582 2779
35df4500 2780 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2781 {
6c95b8df
PA
2782 if (b->loc && b->loc->pspace != pspace)
2783 continue;
2784
c5aa993b
JM
2785 switch (b->type)
2786 {
2787 case bp_call_dummy:
c906108c 2788
c5aa993b 2789 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2790 cause problems when the inferior is rerun, so we better get
2791 rid of it. */
2792
2793 case bp_watchpoint_scope:
2794
2795 /* Also get rid of scope breakpoints. */
2796
2797 case bp_shlib_event:
2798
2799 /* Also remove solib event breakpoints. Their addresses may
2800 have changed since the last time we ran the program.
2801 Actually we may now be debugging against different target;
2802 and so the solib backend that installed this breakpoint may
2803 not be used in by the target. E.g.,
2804
2805 (gdb) file prog-linux
2806 (gdb) run # native linux target
2807 ...
2808 (gdb) kill
2809 (gdb) file prog-win.exe
2810 (gdb) tar rem :9999 # remote Windows gdbserver.
2811 */
c906108c 2812
c5aa993b
JM
2813 delete_breakpoint (b);
2814 break;
c906108c 2815
c5aa993b
JM
2816 case bp_watchpoint:
2817 case bp_hardware_watchpoint:
2818 case bp_read_watchpoint:
2819 case bp_access_watchpoint:
3a5c3e22
PA
2820 {
2821 struct watchpoint *w = (struct watchpoint *) b;
c906108c 2822
3a5c3e22
PA
2823 /* Likewise for watchpoints on local expressions. */
2824 if (w->exp_valid_block != NULL)
2825 delete_breakpoint (b);
2826 else if (context == inf_starting)
2827 {
2828 /* Reset val field to force reread of starting value in
2829 insert_breakpoints. */
2830 if (w->val)
2831 value_free (w->val);
2832 w->val = NULL;
2833 w->val_valid = 0;
c860120c 2834 }
3a5c3e22 2835 }
c5aa993b
JM
2836 break;
2837 default:
c5aa993b
JM
2838 break;
2839 }
2840 }
1c5cfe86
PA
2841
2842 /* Get rid of the moribund locations. */
35df4500
TJB
2843 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2844 decref_bp_location (&bl);
1c5cfe86 2845 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2846}
2847
6c95b8df
PA
2848/* These functions concern about actual breakpoints inserted in the
2849 target --- to e.g. check if we need to do decr_pc adjustment or if
2850 we need to hop over the bkpt --- so we check for address space
2851 match, not program space. */
2852
c2c6d25f
JM
2853/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2854 exists at PC. It returns ordinary_breakpoint_here if it's an
2855 ordinary breakpoint, or permanent_breakpoint_here if it's a
2856 permanent breakpoint.
2857 - When continuing from a location with an ordinary breakpoint, we
2858 actually single step once before calling insert_breakpoints.
e5dd4106 2859 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
2860 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2861 the target, to advance the PC past the breakpoint. */
c906108c 2862
c2c6d25f 2863enum breakpoint_here
6c95b8df 2864breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2865{
35df4500 2866 struct bp_location *bl, **blp_tmp;
c2c6d25f 2867 int any_breakpoint_here = 0;
c906108c 2868
35df4500 2869 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2870 {
35df4500
TJB
2871 if (bl->loc_type != bp_loc_software_breakpoint
2872 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2873 continue;
2874
f1310107 2875 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2876 if ((breakpoint_enabled (bl->owner)
2877 || bl->owner->enable_state == bp_permanent)
f1310107 2878 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2879 {
2880 if (overlay_debugging
35df4500
TJB
2881 && section_is_overlay (bl->section)
2882 && !section_is_mapped (bl->section))
075f6582 2883 continue; /* unmapped overlay -- can't be a match */
35df4500 2884 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2885 return permanent_breakpoint_here;
2886 else
2887 any_breakpoint_here = 1;
2888 }
2889 }
c906108c 2890
c2c6d25f 2891 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2892}
2893
1c5cfe86
PA
2894/* Return true if there's a moribund breakpoint at PC. */
2895
2896int
6c95b8df 2897moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2898{
2899 struct bp_location *loc;
2900 int ix;
2901
2902 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2903 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2904 return 1;
2905
2906 return 0;
2907}
c2c6d25f 2908
c36b740a 2909/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2910 inserted using regular breakpoint_chain / bp_location array
2911 mechanism. This does not check for single-step breakpoints, which
2912 are inserted and removed using direct target manipulation. */
c906108c
SS
2913
2914int
4a64f543
MS
2915regular_breakpoint_inserted_here_p (struct address_space *aspace,
2916 CORE_ADDR pc)
c906108c 2917{
35df4500 2918 struct bp_location *bl, **blp_tmp;
c906108c 2919
35df4500 2920 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2921 {
35df4500
TJB
2922 if (bl->loc_type != bp_loc_software_breakpoint
2923 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2924 continue;
2925
35df4500 2926 if (bl->inserted
f1310107 2927 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2928 {
2929 if (overlay_debugging
35df4500
TJB
2930 && section_is_overlay (bl->section)
2931 && !section_is_mapped (bl->section))
075f6582
DJ
2932 continue; /* unmapped overlay -- can't be a match */
2933 else
2934 return 1;
2935 }
c5aa993b 2936 }
c36b740a
VP
2937 return 0;
2938}
2939
2940/* Returns non-zero iff there's either regular breakpoint
2941 or a single step breakpoint inserted at PC. */
2942
2943int
6c95b8df 2944breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2945{
6c95b8df 2946 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2947 return 1;
c906108c 2948
6c95b8df 2949 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2950 return 1;
2951
c906108c
SS
2952 return 0;
2953}
2954
4fa8626c
DJ
2955/* This function returns non-zero iff there is a software breakpoint
2956 inserted at PC. */
2957
2958int
3e43a32a
MS
2959software_breakpoint_inserted_here_p (struct address_space *aspace,
2960 CORE_ADDR pc)
4fa8626c 2961{
35df4500 2962 struct bp_location *bl, **blp_tmp;
4fa8626c 2963
35df4500 2964 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2965 {
35df4500 2966 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2967 continue;
2968
35df4500
TJB
2969 if (bl->inserted
2970 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2971 aspace, pc))
4fa8626c
DJ
2972 {
2973 if (overlay_debugging
35df4500
TJB
2974 && section_is_overlay (bl->section)
2975 && !section_is_mapped (bl->section))
4fa8626c
DJ
2976 continue; /* unmapped overlay -- can't be a match */
2977 else
2978 return 1;
2979 }
2980 }
2981
1aafd4da 2982 /* Also check for software single-step breakpoints. */
6c95b8df 2983 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2984 return 1;
2985
4fa8626c
DJ
2986 return 0;
2987}
2988
9093389c
PA
2989int
2990hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2991 CORE_ADDR addr, ULONGEST len)
2992{
2993 struct breakpoint *bpt;
2994
2995 ALL_BREAKPOINTS (bpt)
2996 {
2997 struct bp_location *loc;
2998
2999 if (bpt->type != bp_hardware_watchpoint
3000 && bpt->type != bp_access_watchpoint)
3001 continue;
3002
3003 if (!breakpoint_enabled (bpt))
3004 continue;
3005
3006 for (loc = bpt->loc; loc; loc = loc->next)
3007 if (loc->pspace->aspace == aspace && loc->inserted)
3008 {
3009 CORE_ADDR l, h;
3010
3011 /* Check for intersection. */
3012 l = max (loc->address, addr);
3013 h = min (loc->address + loc->length, addr + len);
3014 if (l < h)
3015 return 1;
3016 }
3017 }
3018 return 0;
3019}
3020
075f6582
DJ
3021/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3022 PC is valid for process/thread PTID. */
c906108c
SS
3023
3024int
6c95b8df
PA
3025breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3026 ptid_t ptid)
c906108c 3027{
35df4500 3028 struct bp_location *bl, **blp_tmp;
4a306c9a 3029 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3030 int thread = -1;
4a306c9a 3031 int task = 0;
a6f1cd96 3032
35df4500 3033 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3034 {
35df4500
TJB
3035 if (bl->loc_type != bp_loc_software_breakpoint
3036 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3037 continue;
3038
35df4500
TJB
3039 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3040 if (!breakpoint_enabled (bl->owner)
3041 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3042 continue;
3043
f1310107 3044 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3045 continue;
3046
35df4500 3047 if (bl->owner->thread != -1)
075f6582 3048 {
a6f1cd96
JB
3049 /* This is a thread-specific breakpoint. Check that ptid
3050 matches that thread. If thread hasn't been computed yet,
3051 it is now time to do so. */
3052 if (thread == -1)
3053 thread = pid_to_thread_id (ptid);
35df4500 3054 if (bl->owner->thread != thread)
a6f1cd96 3055 continue;
075f6582 3056 }
a6f1cd96 3057
35df4500 3058 if (bl->owner->task != 0)
4a306c9a
JB
3059 {
3060 /* This is a task-specific breakpoint. Check that ptid
3061 matches that task. If task hasn't been computed yet,
3062 it is now time to do so. */
3063 if (task == 0)
3064 task = ada_get_task_number (ptid);
35df4500 3065 if (bl->owner->task != task)
4a306c9a
JB
3066 continue;
3067 }
3068
a6f1cd96 3069 if (overlay_debugging
35df4500
TJB
3070 && section_is_overlay (bl->section)
3071 && !section_is_mapped (bl->section))
a6f1cd96
JB
3072 continue; /* unmapped overlay -- can't be a match */
3073
3074 return 1;
c5aa993b 3075 }
c906108c
SS
3076
3077 return 0;
3078}
c906108c 3079\f
c5aa993b 3080
c906108c
SS
3081/* bpstat stuff. External routines' interfaces are documented
3082 in breakpoint.h. */
3083
3084int
fba45db2 3085ep_is_catchpoint (struct breakpoint *ep)
c906108c 3086{
533be4dd 3087 return (ep->type == bp_catchpoint);
c906108c
SS
3088}
3089
f431efe5
PA
3090/* Frees any storage that is part of a bpstat. Does not walk the
3091 'next' chain. */
3092
3093static void
198757a8
VP
3094bpstat_free (bpstat bs)
3095{
3096 if (bs->old_val != NULL)
3097 value_free (bs->old_val);
9add0f1b 3098 decref_counted_command_line (&bs->commands);
f431efe5 3099 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3100 xfree (bs);
3101}
3102
c906108c
SS
3103/* Clear a bpstat so that it says we are not at any breakpoint.
3104 Also free any storage that is part of a bpstat. */
3105
3106void
fba45db2 3107bpstat_clear (bpstat *bsp)
c906108c
SS
3108{
3109 bpstat p;
3110 bpstat q;
3111
3112 if (bsp == 0)
3113 return;
3114 p = *bsp;
3115 while (p != NULL)
3116 {
3117 q = p->next;
198757a8 3118 bpstat_free (p);
c906108c
SS
3119 p = q;
3120 }
3121 *bsp = NULL;
3122}
3123
3124/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3125 is part of the bpstat is copied as well. */
3126
3127bpstat
fba45db2 3128bpstat_copy (bpstat bs)
c906108c
SS
3129{
3130 bpstat p = NULL;
3131 bpstat tmp;
3132 bpstat retval = NULL;
3133
3134 if (bs == NULL)
3135 return bs;
3136
3137 for (; bs != NULL; bs = bs->next)
3138 {
3139 tmp = (bpstat) xmalloc (sizeof (*tmp));
3140 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3141 incref_counted_command_line (tmp->commands);
f431efe5 3142 incref_bp_location (tmp->bp_location_at);
31cc81e9 3143 if (bs->old_val != NULL)
3c3185ac
JK
3144 {
3145 tmp->old_val = value_copy (bs->old_val);
3146 release_value (tmp->old_val);
3147 }
31cc81e9 3148
c906108c
SS
3149 if (p == NULL)
3150 /* This is the first thing in the chain. */
3151 retval = tmp;
3152 else
3153 p->next = tmp;
3154 p = tmp;
3155 }
3156 p->next = NULL;
3157 return retval;
3158}
3159
4a64f543 3160/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3161
3162bpstat
fba45db2 3163bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3164{
c5aa993b
JM
3165 if (bsp == NULL)
3166 return NULL;
c906108c 3167
c5aa993b
JM
3168 for (; bsp != NULL; bsp = bsp->next)
3169 {
f431efe5 3170 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3171 return bsp;
3172 }
c906108c
SS
3173 return NULL;
3174}
3175
4a64f543
MS
3176/* Put in *NUM the breakpoint number of the first breakpoint we are
3177 stopped at. *BSP upon return is a bpstat which points to the
3178 remaining breakpoints stopped at (but which is not guaranteed to be
3179 good for anything but further calls to bpstat_num).
3180
8671a17b
PA
3181 Return 0 if passed a bpstat which does not indicate any breakpoints.
3182 Return -1 if stopped at a breakpoint that has been deleted since
3183 we set it.
3184 Return 1 otherwise. */
c906108c
SS
3185
3186int
8671a17b 3187bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3188{
3189 struct breakpoint *b;
3190
3191 if ((*bsp) == NULL)
3192 return 0; /* No more breakpoint values */
8671a17b 3193
4a64f543
MS
3194 /* We assume we'll never have several bpstats that correspond to a
3195 single breakpoint -- otherwise, this function might return the
3196 same number more than once and this will look ugly. */
f431efe5 3197 b = (*bsp)->breakpoint_at;
8671a17b
PA
3198 *bsp = (*bsp)->next;
3199 if (b == NULL)
3200 return -1; /* breakpoint that's been deleted since */
3201
3202 *num = b->number; /* We have its number */
3203 return 1;
c906108c
SS
3204}
3205
e93ca019 3206/* See breakpoint.h. */
c906108c
SS
3207
3208void
e93ca019 3209bpstat_clear_actions (void)
c906108c 3210{
e93ca019
JK
3211 struct thread_info *tp;
3212 bpstat bs;
3213
3214 if (ptid_equal (inferior_ptid, null_ptid))
3215 return;
3216
3217 tp = find_thread_ptid (inferior_ptid);
3218 if (tp == NULL)
3219 return;
3220
3221 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 3222 {
9add0f1b 3223 decref_counted_command_line (&bs->commands);
abf85f46 3224
c906108c
SS
3225 if (bs->old_val != NULL)
3226 {
3227 value_free (bs->old_val);
3228 bs->old_val = NULL;
3229 }
3230 }
3231}
3232
f3b1572e
PA
3233/* Called when a command is about to proceed the inferior. */
3234
3235static void
3236breakpoint_about_to_proceed (void)
3237{
3238 if (!ptid_equal (inferior_ptid, null_ptid))
3239 {
3240 struct thread_info *tp = inferior_thread ();
3241
3242 /* Allow inferior function calls in breakpoint commands to not
3243 interrupt the command list. When the call finishes
3244 successfully, the inferior will be standing at the same
3245 breakpoint as if nothing happened. */
16c381f0 3246 if (tp->control.in_infcall)
f3b1572e
PA
3247 return;
3248 }
3249
3250 breakpoint_proceeded = 1;
3251}
3252
4a64f543
MS
3253/* Stub for cleaning up our state if we error-out of a breakpoint
3254 command. */
c906108c 3255static void
4efb68b1 3256cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3257{
3258 executing_breakpoint_commands = 0;
3259}
3260
abf85f46
JK
3261/* Return non-zero iff CMD as the first line of a command sequence is `silent'
3262 or its equivalent. */
3263
3264static int
3265command_line_is_silent (struct command_line *cmd)
3266{
3267 return cmd && (strcmp ("silent", cmd->line) == 0
3268 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3269}
3270
4a64f543
MS
3271/* Execute all the commands associated with all the breakpoints at
3272 this location. Any of these commands could cause the process to
3273 proceed beyond this point, etc. We look out for such changes by
3274 checking the global "breakpoint_proceeded" after each command.
c906108c 3275
347bddb7
PA
3276 Returns true if a breakpoint command resumed the inferior. In that
3277 case, it is the caller's responsibility to recall it again with the
3278 bpstat of the current thread. */
3279
3280static int
3281bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3282{
3283 bpstat bs;
3284 struct cleanup *old_chain;
347bddb7 3285 int again = 0;
c906108c
SS
3286
3287 /* Avoid endless recursion if a `source' command is contained
3288 in bs->commands. */
3289 if (executing_breakpoint_commands)
347bddb7 3290 return 0;
c906108c
SS
3291
3292 executing_breakpoint_commands = 1;
3293 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3294
cf6c5ffb
TT
3295 prevent_dont_repeat ();
3296
4a64f543 3297 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3298 bs = *bsp;
3299
3300 breakpoint_proceeded = 0;
3301 for (; bs != NULL; bs = bs->next)
3302 {
9add0f1b 3303 struct counted_command_line *ccmd;
6c50ab1c
JB
3304 struct command_line *cmd;
3305 struct cleanup *this_cmd_tree_chain;
3306
3307 /* Take ownership of the BSP's command tree, if it has one.
3308
3309 The command tree could legitimately contain commands like
3310 'step' and 'next', which call clear_proceed_status, which
3311 frees stop_bpstat's command tree. To make sure this doesn't
3312 free the tree we're executing out from under us, we need to
3313 take ownership of the tree ourselves. Since a given bpstat's
3314 commands are only executed once, we don't need to copy it; we
3315 can clear the pointer in the bpstat, and make sure we free
3316 the tree when we're done. */
9add0f1b
TT
3317 ccmd = bs->commands;
3318 bs->commands = NULL;
abf85f46
JK
3319 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
3320 cmd = ccmd ? ccmd->commands : NULL;
3321 if (command_line_is_silent (cmd))
3322 {
3323 /* The action has been already done by bpstat_stop_status. */
3324 cmd = cmd->next;
3325 }
6c50ab1c 3326
c906108c
SS
3327 while (cmd != NULL)
3328 {
3329 execute_control_command (cmd);
3330
3331 if (breakpoint_proceeded)
3332 break;
3333 else
3334 cmd = cmd->next;
3335 }
6c50ab1c
JB
3336
3337 /* We can free this command tree now. */
3338 do_cleanups (this_cmd_tree_chain);
3339
c906108c 3340 if (breakpoint_proceeded)
32c1e744
VP
3341 {
3342 if (target_can_async_p ())
347bddb7
PA
3343 /* If we are in async mode, then the target might be still
3344 running, not stopped at any breakpoint, so nothing for
3345 us to do here -- just return to the event loop. */
3346 ;
32c1e744
VP
3347 else
3348 /* In sync mode, when execute_control_command returns
3349 we're already standing on the next breakpoint.
347bddb7
PA
3350 Breakpoint commands for that stop were not run, since
3351 execute_command does not run breakpoint commands --
3352 only command_line_handler does, but that one is not
3353 involved in execution of breakpoint commands. So, we
3354 can now execute breakpoint commands. It should be
3355 noted that making execute_command do bpstat actions is
3356 not an option -- in this case we'll have recursive
3357 invocation of bpstat for each breakpoint with a
3358 command, and can easily blow up GDB stack. Instead, we
3359 return true, which will trigger the caller to recall us
3360 with the new stop_bpstat. */
3361 again = 1;
3362 break;
32c1e744 3363 }
c906108c 3364 }
c2b8ed2c 3365 do_cleanups (old_chain);
347bddb7
PA
3366 return again;
3367}
3368
3369void
3370bpstat_do_actions (void)
3371{
353d1d73
JK
3372 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
3373
347bddb7
PA
3374 /* Do any commands attached to breakpoint we are stopped at. */
3375 while (!ptid_equal (inferior_ptid, null_ptid)
3376 && target_has_execution
3377 && !is_exited (inferior_ptid)
3378 && !is_executing (inferior_ptid))
3379 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3380 and only return when it is stopped at the next breakpoint, we
3381 keep doing breakpoint actions until it returns false to
3382 indicate the inferior was not resumed. */
16c381f0 3383 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3384 break;
353d1d73
JK
3385
3386 discard_cleanups (cleanup_if_error);
c906108c
SS
3387}
3388
fa4727a6
DJ
3389/* Print out the (old or new) value associated with a watchpoint. */
3390
3391static void
3392watchpoint_value_print (struct value *val, struct ui_file *stream)
3393{
3394 if (val == NULL)
3395 fprintf_unfiltered (stream, _("<unreadable>"));
3396 else
79a45b7d
TT
3397 {
3398 struct value_print_options opts;
3399 get_user_print_options (&opts);
3400 value_print (val, stream, &opts);
3401 }
fa4727a6
DJ
3402}
3403
e514a9d6 3404/* Generic routine for printing messages indicating why we
4a64f543 3405 stopped. The behavior of this function depends on the value
e514a9d6
JM
3406 'print_it' in the bpstat structure. Under some circumstances we
3407 may decide not to print anything here and delegate the task to
4a64f543 3408 normal_stop(). */
e514a9d6
JM
3409
3410static enum print_stop_action
3411print_bp_stop_message (bpstat bs)
3412{
3413 switch (bs->print_it)
3414 {
3415 case print_it_noop:
4a64f543 3416 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3417 return PRINT_UNKNOWN;
3418 break;
3419
3420 case print_it_done:
3421 /* We still want to print the frame, but we already printed the
4a64f543 3422 relevant messages. */
e514a9d6
JM
3423 return PRINT_SRC_AND_LOC;
3424 break;
3425
3426 case print_it_normal:
4f8d1dc6 3427 {
f431efe5
PA
3428 struct breakpoint *b = bs->breakpoint_at;
3429
1a6a67de
TJB
3430 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3431 which has since been deleted. */
3432 if (b == NULL)
3433 return PRINT_UNKNOWN;
3434
348d480f
PA
3435 /* Normal case. Call the breakpoint's print_it method. */
3436 return b->ops->print_it (bs);
4f8d1dc6 3437 }
348d480f 3438 break;
3086aeae 3439
e514a9d6 3440 default:
8e65ff28 3441 internal_error (__FILE__, __LINE__,
e2e0b3e5 3442 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3443 break;
c906108c 3444 }
c906108c
SS
3445}
3446
e514a9d6
JM
3447/* Print a message indicating what happened. This is called from
3448 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
3449 list - a list of the eventpoints that caused this stop. KIND is
3450 the target_waitkind for the stopping event. This
e514a9d6
JM
3451 routine calls the generic print routine for printing a message
3452 about reasons for stopping. This will print (for example) the
3453 "Breakpoint n," part of the output. The return value of this
3454 routine is one of:
c906108c 3455
4a64f543 3456 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3457 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3458 code to print the location. An example is
c5aa993b
JM
3459 "Breakpoint 1, " which should be followed by
3460 the location.
917317f4 3461 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3462 to also print the location part of the message.
3463 An example is the catch/throw messages, which
4a64f543 3464 don't require a location appended to the end.
917317f4 3465 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3466 further info to be printed. */
c906108c 3467
917317f4 3468enum print_stop_action
36dfb11c 3469bpstat_print (bpstat bs, int kind)
c906108c
SS
3470{
3471 int val;
c5aa993b 3472
c906108c 3473 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3474 (Currently all watchpoints go on the bpstat whether hit or not.
3475 That probably could (should) be changed, provided care is taken
c906108c 3476 with respect to bpstat_explains_signal). */
e514a9d6
JM
3477 for (; bs; bs = bs->next)
3478 {
3479 val = print_bp_stop_message (bs);
3480 if (val == PRINT_SRC_ONLY
3481 || val == PRINT_SRC_AND_LOC
3482 || val == PRINT_NOTHING)
3483 return val;
3484 }
c906108c 3485
36dfb11c
TT
3486 /* If we had hit a shared library event breakpoint,
3487 print_bp_stop_message would print out this message. If we hit an
3488 OS-level shared library event, do the same thing. */
3489 if (kind == TARGET_WAITKIND_LOADED)
3490 {
3491 ui_out_text (current_uiout, _("Stopped due to shared library event\n"));
3492 if (ui_out_is_mi_like_p (current_uiout))
3493 ui_out_field_string (current_uiout, "reason",
3494 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
3495 return PRINT_NOTHING;
3496 }
3497
e514a9d6 3498 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3499 with and nothing was printed. */
917317f4 3500 return PRINT_UNKNOWN;
c906108c
SS
3501}
3502
4a64f543
MS
3503/* Evaluate the expression EXP and return 1 if value is zero. This is
3504 used inside a catch_errors to evaluate the breakpoint condition.
3505 The argument is a "struct expression *" that has been cast to a
3506 "char *" to make it pass through catch_errors. */
c906108c
SS
3507
3508static int
4efb68b1 3509breakpoint_cond_eval (void *exp)
c906108c 3510{
278cd55f 3511 struct value *mark = value_mark ();
c5aa993b 3512 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3513
c906108c
SS
3514 value_free_to_mark (mark);
3515 return i;
3516}
3517
5760d0ab 3518/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3519
3520static bpstat
5760d0ab 3521bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3522{
3523 bpstat bs;
3524
3525 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3526 bs->next = NULL;
3527 **bs_link_pointer = bs;
3528 *bs_link_pointer = &bs->next;
f431efe5
PA
3529 bs->breakpoint_at = bl->owner;
3530 bs->bp_location_at = bl;
3531 incref_bp_location (bl);
c906108c
SS
3532 /* If the condition is false, etc., don't do the commands. */
3533 bs->commands = NULL;
3534 bs->old_val = NULL;
3535 bs->print_it = print_it_normal;
3536 return bs;
3537}
3538\f
d983da9c
DJ
3539/* The target has stopped with waitstatus WS. Check if any hardware
3540 watchpoints have triggered, according to the target. */
3541
3542int
3543watchpoints_triggered (struct target_waitstatus *ws)
3544{
d92524f1 3545 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3546 CORE_ADDR addr;
3547 struct breakpoint *b;
3548
3549 if (!stopped_by_watchpoint)
3550 {
3551 /* We were not stopped by a watchpoint. Mark all watchpoints
3552 as not triggered. */
3553 ALL_BREAKPOINTS (b)
cc60f2e3 3554 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3555 {
3556 struct watchpoint *w = (struct watchpoint *) b;
3557
3558 w->watchpoint_triggered = watch_triggered_no;
3559 }
d983da9c
DJ
3560
3561 return 0;
3562 }
3563
3564 if (!target_stopped_data_address (&current_target, &addr))
3565 {
3566 /* We were stopped by a watchpoint, but we don't know where.
3567 Mark all watchpoints as unknown. */
3568 ALL_BREAKPOINTS (b)
cc60f2e3 3569 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3570 {
3571 struct watchpoint *w = (struct watchpoint *) b;
3572
3573 w->watchpoint_triggered = watch_triggered_unknown;
3574 }
d983da9c
DJ
3575
3576 return stopped_by_watchpoint;
3577 }
3578
3579 /* The target could report the data address. Mark watchpoints
3580 affected by this data address as triggered, and all others as not
3581 triggered. */
3582
3583 ALL_BREAKPOINTS (b)
cc60f2e3 3584 if (is_hardware_watchpoint (b))
d983da9c 3585 {
3a5c3e22 3586 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 3587 struct bp_location *loc;
d983da9c 3588
3a5c3e22 3589 w->watchpoint_triggered = watch_triggered_no;
a5606eee 3590 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 3591 {
3a5c3e22 3592 if (is_masked_watchpoint (b))
9c06b0b4 3593 {
3a5c3e22
PA
3594 CORE_ADDR newaddr = addr & w->hw_wp_mask;
3595 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
3596
3597 if (newaddr == start)
3598 {
3a5c3e22 3599 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3600 break;
3601 }
3602 }
3603 /* Exact match not required. Within range is sufficient. */
3604 else if (target_watchpoint_addr_within_range (&current_target,
3605 addr, loc->address,
3606 loc->length))
3607 {
3a5c3e22 3608 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3609 break;
3610 }
3611 }
d983da9c
DJ
3612 }
3613
3614 return 1;
3615}
3616
c906108c
SS
3617/* Possible return values for watchpoint_check (this can't be an enum
3618 because of check_errors). */
3619/* The watchpoint has been deleted. */
3620#define WP_DELETED 1
3621/* The value has changed. */
3622#define WP_VALUE_CHANGED 2
3623/* The value has not changed. */
3624#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3625/* Ignore this watchpoint, no matter if the value changed or not. */
3626#define WP_IGNORE 4
c906108c
SS
3627
3628#define BP_TEMPFLAG 1
3629#define BP_HARDWAREFLAG 2
3630
4a64f543
MS
3631/* Evaluate watchpoint condition expression and check if its value
3632 changed.
553e4c11
JB
3633
3634 P should be a pointer to struct bpstat, but is defined as a void *
3635 in order for this function to be usable with catch_errors. */
c906108c
SS
3636
3637static int
4efb68b1 3638watchpoint_check (void *p)
c906108c
SS
3639{
3640 bpstat bs = (bpstat) p;
3a5c3e22 3641 struct watchpoint *b;
c906108c
SS
3642 struct frame_info *fr;
3643 int within_current_scope;
3644
f431efe5 3645 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3646 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 3647 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 3648
f6bc2008
PA
3649 /* If this is a local watchpoint, we only want to check if the
3650 watchpoint frame is in scope if the current thread is the thread
3651 that was used to create the watchpoint. */
3652 if (!watchpoint_in_thread_scope (b))
60e1c644 3653 return WP_IGNORE;
f6bc2008 3654
c906108c
SS
3655 if (b->exp_valid_block == NULL)
3656 within_current_scope = 1;
3657 else
3658 {
edb3359d
DJ
3659 struct frame_info *frame = get_current_frame ();
3660 struct gdbarch *frame_arch = get_frame_arch (frame);
3661 CORE_ADDR frame_pc = get_frame_pc (frame);
3662
4a64f543
MS
3663 /* in_function_epilogue_p() returns a non-zero value if we're
3664 still in the function but the stack frame has already been
3665 invalidated. Since we can't rely on the values of local
3666 variables after the stack has been destroyed, we are treating
3667 the watchpoint in that state as `not changed' without further
3668 checking. Don't mark watchpoints as changed if the current
3669 frame is in an epilogue - even if they are in some other
3670 frame, our view of the stack is likely to be wrong and
3671 frame_find_by_id could error out. */
a0f49112 3672 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3673 return WP_IGNORE;
a0f49112 3674
101dcfbe 3675 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3676 within_current_scope = (fr != NULL);
69fbadd5
DJ
3677
3678 /* If we've gotten confused in the unwinder, we might have
3679 returned a frame that can't describe this variable. */
edb3359d
DJ
3680 if (within_current_scope)
3681 {
3682 struct symbol *function;
3683
3684 function = get_frame_function (fr);
3685 if (function == NULL
3686 || !contained_in (b->exp_valid_block,
3687 SYMBOL_BLOCK_VALUE (function)))
3688 within_current_scope = 0;
3689 }
69fbadd5 3690
edb3359d 3691 if (within_current_scope)
c906108c
SS
3692 /* If we end up stopping, the current frame will get selected
3693 in normal_stop. So this call to select_frame won't affect
3694 the user. */
0f7d239c 3695 select_frame (fr);
c906108c 3696 }
c5aa993b 3697
c906108c
SS
3698 if (within_current_scope)
3699 {
4a64f543
MS
3700 /* We use value_{,free_to_}mark because it could be a *long*
3701 time before we return to the command level and call
3702 free_all_values. We can't call free_all_values because we
3703 might be in the middle of evaluating a function call. */
c906108c 3704
0cf6dd15 3705 int pc = 0;
9c06b0b4 3706 struct value *mark;
fa4727a6
DJ
3707 struct value *new_val;
3708
3a5c3e22 3709 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
3710 /* Since we don't know the exact trigger address (from
3711 stopped_data_address), just tell the user we've triggered
3712 a mask watchpoint. */
3713 return WP_VALUE_CHANGED;
3714
3715 mark = value_mark ();
0cf6dd15 3716 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3717
4a64f543
MS
3718 /* We use value_equal_contents instead of value_equal because
3719 the latter coerces an array to a pointer, thus comparing just
3720 the address of the array instead of its contents. This is
3721 not what we want. */
fa4727a6 3722 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3723 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3724 {
fa4727a6
DJ
3725 if (new_val != NULL)
3726 {
3727 release_value (new_val);
3728 value_free_to_mark (mark);
3729 }
c906108c
SS
3730 bs->old_val = b->val;
3731 b->val = new_val;
fa4727a6 3732 b->val_valid = 1;
c906108c
SS
3733 return WP_VALUE_CHANGED;
3734 }
3735 else
3736 {
60e1c644 3737 /* Nothing changed. */
c906108c 3738 value_free_to_mark (mark);
c906108c
SS
3739 return WP_VALUE_NOT_CHANGED;
3740 }
3741 }
3742 else
3743 {
79a45e25
PA
3744 struct ui_out *uiout = current_uiout;
3745
c906108c 3746 /* This seems like the only logical thing to do because
c5aa993b
JM
3747 if we temporarily ignored the watchpoint, then when
3748 we reenter the block in which it is valid it contains
3749 garbage (in the case of a function, it may have two
3750 garbage values, one before and one after the prologue).
3751 So we can't even detect the first assignment to it and
3752 watch after that (since the garbage may or may not equal
3753 the first value assigned). */
348d480f
PA
3754 /* We print all the stop information in
3755 breakpoint_ops->print_it, but in this case, by the time we
3756 call breakpoint_ops->print_it this bp will be deleted
3757 already. So we have no choice but print the information
3758 here. */
9dc5e2a9 3759 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3760 ui_out_field_string
3761 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3762 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 3763 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
3764 ui_out_text (uiout,
3765 " deleted because the program has left the block in\n\
8b93c638 3766which its expression is valid.\n");
4ce44c66 3767
cdac0397 3768 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 3769 decref_counted_command_line (&b->base.commands);
d0fb5eae 3770 watchpoint_del_at_next_stop (b);
c906108c
SS
3771
3772 return WP_DELETED;
3773 }
3774}
3775
18a18393 3776/* Return true if it looks like target has stopped due to hitting
348d480f
PA
3777 breakpoint location BL. This function does not check if we should
3778 stop, only if BL explains the stop. */
3779
18a18393 3780static int
6c95b8df
PA
3781bpstat_check_location (const struct bp_location *bl,
3782 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3783{
3784 struct breakpoint *b = bl->owner;
3785
348d480f 3786 /* BL is from an existing breakpoint. */
2bdf28a0
JK
3787 gdb_assert (b != NULL);
3788
348d480f 3789 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
18a18393
VP
3790}
3791
3a5c3e22
PA
3792/* Determine if the watched values have actually changed, and we
3793 should stop. If not, set BS->stop to 0. */
3794
18a18393
VP
3795static void
3796bpstat_check_watchpoint (bpstat bs)
3797{
2bdf28a0 3798 const struct bp_location *bl;
3a5c3e22 3799 struct watchpoint *b;
2bdf28a0
JK
3800
3801 /* BS is built for existing struct breakpoint. */
f431efe5 3802 bl = bs->bp_location_at;
2bdf28a0 3803 gdb_assert (bl != NULL);
3a5c3e22 3804 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 3805 gdb_assert (b != NULL);
18a18393 3806
18a18393 3807 {
18a18393
VP
3808 int must_check_value = 0;
3809
3a5c3e22 3810 if (b->base.type == bp_watchpoint)
18a18393
VP
3811 /* For a software watchpoint, we must always check the
3812 watched value. */
3813 must_check_value = 1;
3814 else if (b->watchpoint_triggered == watch_triggered_yes)
3815 /* We have a hardware watchpoint (read, write, or access)
3816 and the target earlier reported an address watched by
3817 this watchpoint. */
3818 must_check_value = 1;
3819 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 3820 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
3821 /* We were stopped by a hardware watchpoint, but the target could
3822 not report the data address. We must check the watchpoint's
3823 value. Access and read watchpoints are out of luck; without
3824 a data address, we can't figure it out. */
3825 must_check_value = 1;
3a5c3e22 3826
18a18393
VP
3827 if (must_check_value)
3828 {
3e43a32a
MS
3829 char *message
3830 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 3831 b->base.number);
18a18393
VP
3832 struct cleanup *cleanups = make_cleanup (xfree, message);
3833 int e = catch_errors (watchpoint_check, bs, message,
3834 RETURN_MASK_ALL);
3835 do_cleanups (cleanups);
3836 switch (e)
3837 {
3838 case WP_DELETED:
3839 /* We've already printed what needs to be printed. */
3840 bs->print_it = print_it_done;
3841 /* Stop. */
3842 break;
60e1c644
PA
3843 case WP_IGNORE:
3844 bs->print_it = print_it_noop;
3845 bs->stop = 0;
3846 break;
18a18393 3847 case WP_VALUE_CHANGED:
3a5c3e22 3848 if (b->base.type == bp_read_watchpoint)
18a18393 3849 {
85d721b8
PA
3850 /* There are two cases to consider here:
3851
4a64f543 3852 1. We're watching the triggered memory for reads.
85d721b8
PA
3853 In that case, trust the target, and always report
3854 the watchpoint hit to the user. Even though
3855 reads don't cause value changes, the value may
3856 have changed since the last time it was read, and
3857 since we're not trapping writes, we will not see
3858 those, and as such we should ignore our notion of
3859 old value.
3860
4a64f543 3861 2. We're watching the triggered memory for both
85d721b8
PA
3862 reads and writes. There are two ways this may
3863 happen:
3864
4a64f543 3865 2.1. This is a target that can't break on data
85d721b8
PA
3866 reads only, but can break on accesses (reads or
3867 writes), such as e.g., x86. We detect this case
3868 at the time we try to insert read watchpoints.
3869
4a64f543 3870 2.2. Otherwise, the target supports read
85d721b8
PA
3871 watchpoints, but, the user set an access or write
3872 watchpoint watching the same memory as this read
3873 watchpoint.
3874
3875 If we're watching memory writes as well as reads,
3876 ignore watchpoint hits when we find that the
3877 value hasn't changed, as reads don't cause
3878 changes. This still gives false positives when
3879 the program writes the same value to memory as
3880 what there was already in memory (we will confuse
3881 it for a read), but it's much better than
3882 nothing. */
3883
3884 int other_write_watchpoint = 0;
3885
3886 if (bl->watchpoint_type == hw_read)
3887 {
3888 struct breakpoint *other_b;
3889
3890 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
3891 if (other_b->type == bp_hardware_watchpoint
3892 || other_b->type == bp_access_watchpoint)
85d721b8 3893 {
3a5c3e22
PA
3894 struct watchpoint *other_w =
3895 (struct watchpoint *) other_b;
3896
3897 if (other_w->watchpoint_triggered
3898 == watch_triggered_yes)
3899 {
3900 other_write_watchpoint = 1;
3901 break;
3902 }
85d721b8
PA
3903 }
3904 }
3905
3906 if (other_write_watchpoint
3907 || bl->watchpoint_type == hw_access)
3908 {
3909 /* We're watching the same memory for writes,
3910 and the value changed since the last time we
3911 updated it, so this trap must be for a write.
3912 Ignore it. */
3913 bs->print_it = print_it_noop;
3914 bs->stop = 0;
3915 }
18a18393
VP
3916 }
3917 break;
3918 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
3919 if (b->base.type == bp_hardware_watchpoint
3920 || b->base.type == bp_watchpoint)
18a18393
VP
3921 {
3922 /* Don't stop: write watchpoints shouldn't fire if
3923 the value hasn't changed. */
3924 bs->print_it = print_it_noop;
3925 bs->stop = 0;
3926 }
3927 /* Stop. */
3928 break;
3929 default:
3930 /* Can't happen. */
3931 case 0:
3932 /* Error from catch_errors. */
3a5c3e22 3933 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 3934 watchpoint_del_at_next_stop (b);
18a18393
VP
3935 /* We've already printed what needs to be printed. */
3936 bs->print_it = print_it_done;
3937 break;
3938 }
3939 }
3940 else /* must_check_value == 0 */
3941 {
3942 /* This is a case where some watchpoint(s) triggered, but
3943 not at the address of this watchpoint, or else no
3944 watchpoint triggered after all. So don't print
3945 anything for this watchpoint. */
3946 bs->print_it = print_it_noop;
3947 bs->stop = 0;
3948 }
3949 }
3950}
3951
3952
3953/* Check conditions (condition proper, frame, thread and ignore count)
3954 of breakpoint referred to by BS. If we should not stop for this
3955 breakpoint, set BS->stop to 0. */
f431efe5 3956
18a18393
VP
3957static void
3958bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3959{
3960 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
3961 const struct bp_location *bl;
3962 struct breakpoint *b;
3963
3964 /* BS is built for existing struct breakpoint. */
f431efe5 3965 bl = bs->bp_location_at;
2bdf28a0 3966 gdb_assert (bl != NULL);
f431efe5 3967 b = bs->breakpoint_at;
2bdf28a0 3968 gdb_assert (b != NULL);
18a18393
VP
3969
3970 if (frame_id_p (b->frame_id)
edb3359d 3971 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
3972 bs->stop = 0;
3973 else if (bs->stop)
3974 {
3975 int value_is_zero = 0;
60e1c644
PA
3976 struct expression *cond;
3977
7371cf6d
PM
3978 /* Evaluate Python breakpoints that have a "stop"
3979 method implemented. */
3980 if (b->py_bp_object)
3981 bs->stop = gdbpy_should_stop (b->py_bp_object);
3982
60e1c644 3983 if (is_watchpoint (b))
3a5c3e22
PA
3984 {
3985 struct watchpoint *w = (struct watchpoint *) b;
3986
3987 cond = w->cond_exp;
3988 }
60e1c644
PA
3989 else
3990 cond = bl->cond;
3991
f431efe5 3992 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 3993 {
60e1c644 3994 int within_current_scope = 1;
3a5c3e22 3995 struct watchpoint * w;
60e1c644 3996
c5bc3a77
DJ
3997 /* We use value_mark and value_free_to_mark because it could
3998 be a long time before we return to the command level and
3999 call free_all_values. We can't call free_all_values
4000 because we might be in the middle of evaluating a
4001 function call. */
4002 struct value *mark = value_mark ();
4003
3a5c3e22
PA
4004 if (is_watchpoint (b))
4005 w = (struct watchpoint *) b;
4006 else
4007 w = NULL;
4008
edb3359d
DJ
4009 /* Need to select the frame, with all that implies so that
4010 the conditions will have the right context. Because we
4011 use the frame, we will not see an inlined function's
4012 variables when we arrive at a breakpoint at the start
4013 of the inlined function; the current frame will be the
4014 call site. */
3a5c3e22 4015 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
4016 select_frame (get_current_frame ());
4017 else
4018 {
4019 struct frame_info *frame;
4020
4021 /* For local watchpoint expressions, which particular
4022 instance of a local is being watched matters, so we
4023 keep track of the frame to evaluate the expression
4024 in. To evaluate the condition however, it doesn't
4025 really matter which instantiation of the function
4026 where the condition makes sense triggers the
4027 watchpoint. This allows an expression like "watch
4028 global if q > 10" set in `func', catch writes to
4029 global on all threads that call `func', or catch
4030 writes on all recursive calls of `func' by a single
4031 thread. We simply always evaluate the condition in
4032 the innermost frame that's executing where it makes
4033 sense to evaluate the condition. It seems
4034 intuitive. */
3a5c3e22 4035 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
4036 if (frame != NULL)
4037 select_frame (frame);
4038 else
4039 within_current_scope = 0;
4040 }
4041 if (within_current_scope)
4042 value_is_zero
4043 = catch_errors (breakpoint_cond_eval, cond,
4044 "Error in testing breakpoint condition:\n",
4045 RETURN_MASK_ALL);
4046 else
4047 {
4048 warning (_("Watchpoint condition cannot be tested "
4049 "in the current scope"));
4050 /* If we failed to set the right context for this
4051 watchpoint, unconditionally report it. */
4052 value_is_zero = 0;
4053 }
4a64f543 4054 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4055 value_free_to_mark (mark);
18a18393 4056 }
60e1c644
PA
4057
4058 if (cond && value_is_zero)
18a18393
VP
4059 {
4060 bs->stop = 0;
4061 }
4062 else if (b->thread != -1 && b->thread != thread_id)
4063 {
4064 bs->stop = 0;
4065 }
4066 else if (b->ignore_count > 0)
4067 {
4068 b->ignore_count--;
4069 annotate_ignore_count_change ();
4070 bs->stop = 0;
4a64f543 4071 /* Increase the hit count even though we don't stop. */
18a18393 4072 ++(b->hit_count);
8d3788bd 4073 observer_notify_breakpoint_modified (b);
18a18393
VP
4074 }
4075 }
4076}
4077
4078
9709f61c 4079/* Get a bpstat associated with having just stopped at address
d983da9c 4080 BP_ADDR in thread PTID.
c906108c 4081
d983da9c 4082 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4083 don't understand this stop. Result is a chain of bpstat's such
4084 that:
c906108c 4085
c5aa993b 4086 if we don't understand the stop, the result is a null pointer.
c906108c 4087
c5aa993b 4088 if we understand why we stopped, the result is not null.
c906108c 4089
c5aa993b
JM
4090 Each element of the chain refers to a particular breakpoint or
4091 watchpoint at which we have stopped. (We may have stopped for
4092 several reasons concurrently.)
c906108c 4093
c5aa993b
JM
4094 Each element of the chain has valid next, breakpoint_at,
4095 commands, FIXME??? fields. */
c906108c
SS
4096
4097bpstat
6c95b8df
PA
4098bpstat_stop_status (struct address_space *aspace,
4099 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4100{
0d381245 4101 struct breakpoint *b = NULL;
afe38095 4102 struct bp_location *bl;
20874c92 4103 struct bp_location *loc;
5760d0ab
JK
4104 /* First item of allocated bpstat's. */
4105 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4106 /* Pointer to the last thing in the chain currently. */
5760d0ab 4107 bpstat bs;
20874c92 4108 int ix;
429374b8 4109 int need_remove_insert;
f431efe5 4110 int removed_any;
c906108c 4111
f431efe5
PA
4112 /* First, build the bpstat chain with locations that explain a
4113 target stop, while being careful to not set the target running,
4114 as that may invalidate locations (in particular watchpoint
4115 locations are recreated). Resuming will happen here with
4116 breakpoint conditions or watchpoint expressions that include
4117 inferior function calls. */
c5aa993b 4118
429374b8
JK
4119 ALL_BREAKPOINTS (b)
4120 {
4121 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4122 continue;
a5606eee 4123
429374b8
JK
4124 for (bl = b->loc; bl != NULL; bl = bl->next)
4125 {
4a64f543
MS
4126 /* For hardware watchpoints, we look only at the first
4127 location. The watchpoint_check function will work on the
4128 entire expression, not the individual locations. For
4129 read watchpoints, the watchpoints_triggered function has
4130 checked all locations already. */
429374b8
JK
4131 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4132 break;
18a18393 4133
429374b8
JK
4134 if (bl->shlib_disabled)
4135 continue;
c5aa993b 4136
429374b8
JK
4137 if (!bpstat_check_location (bl, aspace, bp_addr))
4138 continue;
c5aa993b 4139
4a64f543
MS
4140 /* Come here if it's a watchpoint, or if the break address
4141 matches. */
c5aa993b 4142
4a64f543
MS
4143 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4144 explain stop. */
c5aa993b 4145
f431efe5
PA
4146 /* Assume we stop. Should we find a watchpoint that is not
4147 actually triggered, or if the condition of the breakpoint
4148 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4149 bs->stop = 1;
4150 bs->print = 1;
d983da9c 4151
f431efe5
PA
4152 /* If this is a scope breakpoint, mark the associated
4153 watchpoint as triggered so that we will handle the
4154 out-of-scope event. We'll get to the watchpoint next
4155 iteration. */
d0fb5eae 4156 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4157 {
4158 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4159
4160 w->watchpoint_triggered = watch_triggered_yes;
4161 }
f431efe5
PA
4162 }
4163 }
4164
4165 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4166 {
f1310107 4167 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4168 {
5760d0ab 4169 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4170 /* For hits of moribund locations, we should just proceed. */
4171 bs->stop = 0;
4172 bs->print = 0;
4173 bs->print_it = print_it_noop;
4174 }
4175 }
4176
f431efe5
PA
4177 /* Now go through the locations that caused the target to stop, and
4178 check whether we're interested in reporting this stop to higher
4179 layers, or whether we should resume the target transparently. */
4180
4181 removed_any = 0;
4182
5760d0ab 4183 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4184 {
4185 if (!bs->stop)
4186 continue;
4187
f431efe5 4188 b = bs->breakpoint_at;
348d480f
PA
4189 b->ops->check_status (bs);
4190 if (bs->stop)
28010a5d 4191 {
348d480f 4192 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4193
429374b8
JK
4194 if (bs->stop)
4195 {
4196 ++(b->hit_count);
8d3788bd 4197 observer_notify_breakpoint_modified (b);
c906108c 4198
4a64f543 4199 /* We will stop here. */
429374b8
JK
4200 if (b->disposition == disp_disable)
4201 {
4202 if (b->enable_state != bp_permanent)
4203 b->enable_state = bp_disabled;
f431efe5 4204 removed_any = 1;
429374b8
JK
4205 }
4206 if (b->silent)
4207 bs->print = 0;
4208 bs->commands = b->commands;
9add0f1b 4209 incref_counted_command_line (bs->commands);
abf85f46
JK
4210 if (command_line_is_silent (bs->commands
4211 ? bs->commands->commands : NULL))
4212 bs->print = 0;
429374b8
JK
4213 }
4214
e5dd4106 4215 /* Print nothing for this entry if we don't stop or don't print. */
429374b8
JK
4216 if (bs->stop == 0 || bs->print == 0)
4217 bs->print_it = print_it_noop;
348d480f 4218 }
429374b8 4219 }
876fa593 4220
d983da9c
DJ
4221 /* If we aren't stopping, the value of some hardware watchpoint may
4222 not have changed, but the intermediate memory locations we are
4223 watching may have. Don't bother if we're stopping; this will get
4224 done later. */
d832cb68 4225 need_remove_insert = 0;
5760d0ab
JK
4226 if (! bpstat_causes_stop (bs_head))
4227 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4228 if (!bs->stop
f431efe5
PA
4229 && bs->breakpoint_at
4230 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4231 {
3a5c3e22
PA
4232 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4233
4234 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 4235 need_remove_insert = 1;
d983da9c
DJ
4236 }
4237
d832cb68 4238 if (need_remove_insert)
2d134ed3 4239 update_global_location_list (1);
f431efe5
PA
4240 else if (removed_any)
4241 update_global_location_list (0);
d832cb68 4242
5760d0ab 4243 return bs_head;
c906108c 4244}
628fe4e4
JK
4245
4246static void
4247handle_jit_event (void)
4248{
4249 struct frame_info *frame;
4250 struct gdbarch *gdbarch;
4251
4252 /* Switch terminal for any messages produced by
4253 breakpoint_re_set. */
4254 target_terminal_ours_for_output ();
4255
4256 frame = get_current_frame ();
4257 gdbarch = get_frame_arch (frame);
4258
4259 jit_event_handler (gdbarch);
4260
4261 target_terminal_inferior ();
4262}
4263
4264/* Prepare WHAT final decision for infrun. */
4265
4266/* Decide what infrun needs to do with this bpstat. */
4267
c906108c 4268struct bpstat_what
0e30163f 4269bpstat_what (bpstat bs_head)
c906108c 4270{
c906108c 4271 struct bpstat_what retval;
628fe4e4
JK
4272 /* We need to defer calling `solib_add', as adding new symbols
4273 resets breakpoints, which in turn deletes breakpoint locations,
4274 and hence may clear unprocessed entries in the BS chain. */
4275 int shlib_event = 0;
4276 int jit_event = 0;
0e30163f 4277 bpstat bs;
c906108c 4278
628fe4e4 4279 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4280 retval.call_dummy = STOP_NONE;
186c406b 4281 retval.is_longjmp = 0;
628fe4e4 4282
0e30163f 4283 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4284 {
628fe4e4
JK
4285 /* Extract this BS's action. After processing each BS, we check
4286 if its action overrides all we've seem so far. */
4287 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4288 enum bptype bptype;
4289
c906108c 4290 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4291 {
4292 /* I suspect this can happen if it was a momentary
4293 breakpoint which has since been deleted. */
4294 bptype = bp_none;
4295 }
20874c92 4296 else
f431efe5 4297 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4298
4299 switch (bptype)
c906108c
SS
4300 {
4301 case bp_none:
628fe4e4 4302 break;
c906108c
SS
4303 case bp_breakpoint:
4304 case bp_hardware_breakpoint:
4305 case bp_until:
4306 case bp_finish:
4307 if (bs->stop)
4308 {
4309 if (bs->print)
628fe4e4 4310 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4311 else
628fe4e4 4312 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4313 }
4314 else
628fe4e4 4315 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4316 break;
4317 case bp_watchpoint:
4318 case bp_hardware_watchpoint:
4319 case bp_read_watchpoint:
4320 case bp_access_watchpoint:
4321 if (bs->stop)
4322 {
4323 if (bs->print)
628fe4e4 4324 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4325 else
628fe4e4 4326 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4327 }
4328 else
628fe4e4
JK
4329 {
4330 /* There was a watchpoint, but we're not stopping.
4331 This requires no further action. */
4332 }
c906108c
SS
4333 break;
4334 case bp_longjmp:
186c406b 4335 case bp_exception:
628fe4e4 4336 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4337 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4338 break;
4339 case bp_longjmp_resume:
186c406b 4340 case bp_exception_resume:
628fe4e4 4341 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4342 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4343 break;
4344 case bp_step_resume:
4345 if (bs->stop)
628fe4e4
JK
4346 this_action = BPSTAT_WHAT_STEP_RESUME;
4347 else
c906108c 4348 {
628fe4e4
JK
4349 /* It is for the wrong frame. */
4350 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4351 }
c906108c 4352 break;
2c03e5be
PA
4353 case bp_hp_step_resume:
4354 if (bs->stop)
4355 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4356 else
4357 {
4358 /* It is for the wrong frame. */
4359 this_action = BPSTAT_WHAT_SINGLE;
4360 }
4361 break;
c906108c 4362 case bp_watchpoint_scope:
c4093a6a 4363 case bp_thread_event:
1900040c 4364 case bp_overlay_event:
0fd8e87f 4365 case bp_longjmp_master:
aa7d318d 4366 case bp_std_terminate_master:
186c406b 4367 case bp_exception_master:
628fe4e4 4368 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4369 break;
ce78b96d 4370 case bp_catchpoint:
c5aa993b
JM
4371 if (bs->stop)
4372 {
4373 if (bs->print)
628fe4e4 4374 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4375 else
628fe4e4 4376 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4377 }
4378 else
628fe4e4
JK
4379 {
4380 /* There was a catchpoint, but we're not stopping.
4381 This requires no further action. */
4382 }
4383 break;
4384 case bp_shlib_event:
4385 shlib_event = 1;
4386
4387 /* If requested, stop when the dynamic linker notifies GDB
4388 of events. This allows the user to get control and place
4389 breakpoints in initializer routines for dynamically
4390 loaded objects (among other things). */
4391 if (stop_on_solib_events)
4392 this_action = BPSTAT_WHAT_STOP_NOISY;
4393 else
4394 this_action = BPSTAT_WHAT_SINGLE;
4395 break;
4396 case bp_jit_event:
4397 jit_event = 1;
4398 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4399 break;
c906108c 4400 case bp_call_dummy:
53a5351d
JM
4401 /* Make sure the action is stop (silent or noisy),
4402 so infrun.c pops the dummy frame. */
aa7d318d 4403 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4404 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4405 break;
4406 case bp_std_terminate:
4407 /* Make sure the action is stop (silent or noisy),
4408 so infrun.c pops the dummy frame. */
aa7d318d 4409 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4410 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4411 break;
1042e4c0 4412 case bp_tracepoint:
7a697b8d 4413 case bp_fast_tracepoint:
0fb4aa4b 4414 case bp_static_tracepoint:
1042e4c0
SS
4415 /* Tracepoint hits should not be reported back to GDB, and
4416 if one got through somehow, it should have been filtered
4417 out already. */
4418 internal_error (__FILE__, __LINE__,
7a697b8d 4419 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4420 break;
4421 case bp_gnu_ifunc_resolver:
4422 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4423 this_action = BPSTAT_WHAT_SINGLE;
4424 break;
4425 case bp_gnu_ifunc_resolver_return:
4426 /* The breakpoint will be removed, execution will restart from the
4427 PC of the former breakpoint. */
4428 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4429 break;
628fe4e4
JK
4430 default:
4431 internal_error (__FILE__, __LINE__,
4432 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4433 }
628fe4e4
JK
4434
4435 retval.main_action = max (retval.main_action, this_action);
c906108c 4436 }
628fe4e4 4437
0e30163f
JK
4438 /* These operations may affect the bs->breakpoint_at state so they are
4439 delayed after MAIN_ACTION is decided above. */
4440
628fe4e4
JK
4441 if (shlib_event)
4442 {
4443 if (debug_infrun)
4444 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4445
4446 /* Check for any newly added shared libraries if we're supposed
4447 to be adding them automatically. */
4448
4449 /* Switch terminal for any messages produced by
4450 breakpoint_re_set. */
4451 target_terminal_ours_for_output ();
4452
4453#ifdef SOLIB_ADD
4454 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4455#else
4456 solib_add (NULL, 0, &current_target, auto_solib_add);
4457#endif
4458
4459 target_terminal_inferior ();
4460 }
4461
4462 if (jit_event)
4463 {
4464 if (debug_infrun)
4465 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4466
4467 handle_jit_event ();
4468 }
4469
0e30163f
JK
4470 for (bs = bs_head; bs != NULL; bs = bs->next)
4471 {
4472 struct breakpoint *b = bs->breakpoint_at;
4473
4474 if (b == NULL)
4475 continue;
4476 switch (b->type)
4477 {
4478 case bp_gnu_ifunc_resolver:
4479 gnu_ifunc_resolver_stop (b);
4480 break;
4481 case bp_gnu_ifunc_resolver_return:
4482 gnu_ifunc_resolver_return_stop (b);
4483 break;
4484 }
4485 }
4486
c906108c
SS
4487 return retval;
4488}
4489
4490/* Nonzero if we should step constantly (e.g. watchpoints on machines
4491 without hardware support). This isn't related to a specific bpstat,
4492 just to things like whether watchpoints are set. */
4493
c5aa993b 4494int
fba45db2 4495bpstat_should_step (void)
c906108c
SS
4496{
4497 struct breakpoint *b;
cc59ec59 4498
c906108c 4499 ALL_BREAKPOINTS (b)
717a8278 4500 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4501 return 1;
c906108c
SS
4502 return 0;
4503}
4504
67822962
PA
4505int
4506bpstat_causes_stop (bpstat bs)
4507{
4508 for (; bs != NULL; bs = bs->next)
4509 if (bs->stop)
4510 return 1;
4511
4512 return 0;
4513}
4514
c906108c 4515\f
c5aa993b 4516
170b53b2
UW
4517/* Compute a string of spaces suitable to indent the next line
4518 so it starts at the position corresponding to the table column
4519 named COL_NAME in the currently active table of UIOUT. */
4520
4521static char *
4522wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4523{
4524 static char wrap_indent[80];
4525 int i, total_width, width, align;
4526 char *text;
4527
4528 total_width = 0;
4529 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4530 {
4531 if (strcmp (text, col_name) == 0)
4532 {
4533 gdb_assert (total_width < sizeof wrap_indent);
4534 memset (wrap_indent, ' ', total_width);
4535 wrap_indent[total_width] = 0;
4536
4537 return wrap_indent;
4538 }
4539
4540 total_width += width + 1;
4541 }
4542
4543 return NULL;
4544}
4545
859825b8
JK
4546/* Print the LOC location out of the list of B->LOC locations. */
4547
170b53b2
UW
4548static void
4549print_breakpoint_location (struct breakpoint *b,
4550 struct bp_location *loc)
0d381245 4551{
79a45e25 4552 struct ui_out *uiout = current_uiout;
6c95b8df
PA
4553 struct cleanup *old_chain = save_current_program_space ();
4554
859825b8
JK
4555 if (loc != NULL && loc->shlib_disabled)
4556 loc = NULL;
4557
6c95b8df
PA
4558 if (loc != NULL)
4559 set_current_program_space (loc->pspace);
4560
56435ebe
TT
4561 if (b->display_canonical)
4562 ui_out_field_string (uiout, "what", b->addr_string);
4563 else if (b->source_file && loc)
0d381245
VP
4564 {
4565 struct symbol *sym
4566 = find_pc_sect_function (loc->address, loc->section);
4567 if (sym)
4568 {
4569 ui_out_text (uiout, "in ");
4570 ui_out_field_string (uiout, "func",
4571 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4572 ui_out_text (uiout, " ");
4573 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4574 ui_out_text (uiout, "at ");
0d381245
VP
4575 }
4576 ui_out_field_string (uiout, "file", b->source_file);
4577 ui_out_text (uiout, ":");
4578
4579 if (ui_out_is_mi_like_p (uiout))
4580 {
4581 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4582 char *fullname = symtab_to_fullname (sal.symtab);
4583
4584 if (fullname)
4585 ui_out_field_string (uiout, "fullname", fullname);
4586 }
4587
4588 ui_out_field_int (uiout, "line", b->line_number);
4589 }
859825b8 4590 else if (loc)
0d381245 4591 {
170b53b2
UW
4592 struct ui_stream *stb = ui_out_stream_new (uiout);
4593 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4594
22e722e1
DJ
4595 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4596 demangle, "");
0d381245 4597 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4598
4599 do_cleanups (stb_chain);
0d381245 4600 }
859825b8
JK
4601 else
4602 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4603
4604 do_cleanups (old_chain);
0d381245
VP
4605}
4606
269b11a2
PA
4607static const char *
4608bptype_string (enum bptype type)
c906108c 4609{
c4093a6a
JM
4610 struct ep_type_description
4611 {
4612 enum bptype type;
4613 char *description;
4614 };
4615 static struct ep_type_description bptypes[] =
c906108c 4616 {
c5aa993b
JM
4617 {bp_none, "?deleted?"},
4618 {bp_breakpoint, "breakpoint"},
c906108c 4619 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4620 {bp_until, "until"},
4621 {bp_finish, "finish"},
4622 {bp_watchpoint, "watchpoint"},
c906108c 4623 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4624 {bp_read_watchpoint, "read watchpoint"},
4625 {bp_access_watchpoint, "acc watchpoint"},
4626 {bp_longjmp, "longjmp"},
4627 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4628 {bp_exception, "exception"},
4629 {bp_exception_resume, "exception resume"},
c5aa993b 4630 {bp_step_resume, "step resume"},
2c03e5be 4631 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
4632 {bp_watchpoint_scope, "watchpoint scope"},
4633 {bp_call_dummy, "call dummy"},
aa7d318d 4634 {bp_std_terminate, "std::terminate"},
c5aa993b 4635 {bp_shlib_event, "shlib events"},
c4093a6a 4636 {bp_thread_event, "thread events"},
1900040c 4637 {bp_overlay_event, "overlay events"},
0fd8e87f 4638 {bp_longjmp_master, "longjmp master"},
aa7d318d 4639 {bp_std_terminate_master, "std::terminate master"},
186c406b 4640 {bp_exception_master, "exception master"},
ce78b96d 4641 {bp_catchpoint, "catchpoint"},
1042e4c0 4642 {bp_tracepoint, "tracepoint"},
7a697b8d 4643 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4644 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4645 {bp_jit_event, "jit events"},
0e30163f
JK
4646 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4647 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4648 };
269b11a2
PA
4649
4650 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4651 || ((int) type != bptypes[(int) type].type))
4652 internal_error (__FILE__, __LINE__,
4653 _("bptypes table does not describe type #%d."),
4654 (int) type);
4655
4656 return bptypes[(int) type].description;
4657}
4658
4659/* Print B to gdb_stdout. */
4660
4661static void
4662print_one_breakpoint_location (struct breakpoint *b,
4663 struct bp_location *loc,
4664 int loc_number,
4665 struct bp_location **last_loc,
269b11a2
PA
4666 int allflag)
4667{
4668 struct command_line *l;
c2c6d25f 4669 static char bpenables[] = "nynny";
c906108c 4670
79a45e25 4671 struct ui_out *uiout = current_uiout;
0d381245
VP
4672 int header_of_multiple = 0;
4673 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4674 struct value_print_options opts;
4675
4676 get_user_print_options (&opts);
0d381245
VP
4677
4678 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4679 /* See comment in print_one_breakpoint concerning treatment of
4680 breakpoints with single disabled location. */
0d381245
VP
4681 if (loc == NULL
4682 && (b->loc != NULL
4683 && (b->loc->next != NULL || !b->loc->enabled)))
4684 header_of_multiple = 1;
4685 if (loc == NULL)
4686 loc = b->loc;
4687
c4093a6a
JM
4688 annotate_record ();
4689
4690 /* 1 */
4691 annotate_field (0);
0d381245
VP
4692 if (part_of_multiple)
4693 {
4694 char *formatted;
0c6773c1 4695 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4696 ui_out_field_string (uiout, "number", formatted);
4697 xfree (formatted);
4698 }
4699 else
4700 {
4701 ui_out_field_int (uiout, "number", b->number);
4702 }
c4093a6a
JM
4703
4704 /* 2 */
4705 annotate_field (1);
0d381245
VP
4706 if (part_of_multiple)
4707 ui_out_field_skip (uiout, "type");
269b11a2
PA
4708 else
4709 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4710
4711 /* 3 */
4712 annotate_field (2);
0d381245
VP
4713 if (part_of_multiple)
4714 ui_out_field_skip (uiout, "disp");
4715 else
2cec12e5 4716 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4717
c4093a6a
JM
4718
4719 /* 4 */
4720 annotate_field (3);
0d381245 4721 if (part_of_multiple)
54e52265 4722 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4723 else
4a64f543
MS
4724 ui_out_field_fmt (uiout, "enabled", "%c",
4725 bpenables[(int) b->enable_state]);
54e52265 4726 ui_out_spaces (uiout, 2);
0d381245 4727
c4093a6a
JM
4728
4729 /* 5 and 6 */
3086aeae 4730 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4731 {
4a64f543
MS
4732 /* Although the print_one can possibly print all locations,
4733 calling it here is not likely to get any nice result. So,
4734 make sure there's just one location. */
0d381245 4735 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4736 b->ops->print_one (b, last_loc);
0d381245 4737 }
3086aeae
DJ
4738 else
4739 switch (b->type)
4740 {
4741 case bp_none:
4742 internal_error (__FILE__, __LINE__,
e2e0b3e5 4743 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4744 break;
c906108c 4745
3086aeae
DJ
4746 case bp_watchpoint:
4747 case bp_hardware_watchpoint:
4748 case bp_read_watchpoint:
4749 case bp_access_watchpoint:
3a5c3e22
PA
4750 {
4751 struct watchpoint *w = (struct watchpoint *) b;
4752
4753 /* Field 4, the address, is omitted (which makes the columns
4754 not line up too nicely with the headers, but the effect
4755 is relatively readable). */
4756 if (opts.addressprint)
4757 ui_out_field_skip (uiout, "addr");
4758 annotate_field (5);
4759 ui_out_field_string (uiout, "what", w->exp_string);
4760 }
3086aeae
DJ
4761 break;
4762
3086aeae
DJ
4763 case bp_breakpoint:
4764 case bp_hardware_breakpoint:
4765 case bp_until:
4766 case bp_finish:
4767 case bp_longjmp:
4768 case bp_longjmp_resume:
186c406b
TT
4769 case bp_exception:
4770 case bp_exception_resume:
3086aeae 4771 case bp_step_resume:
2c03e5be 4772 case bp_hp_step_resume:
3086aeae
DJ
4773 case bp_watchpoint_scope:
4774 case bp_call_dummy:
aa7d318d 4775 case bp_std_terminate:
3086aeae
DJ
4776 case bp_shlib_event:
4777 case bp_thread_event:
4778 case bp_overlay_event:
0fd8e87f 4779 case bp_longjmp_master:
aa7d318d 4780 case bp_std_terminate_master:
186c406b 4781 case bp_exception_master:
1042e4c0 4782 case bp_tracepoint:
7a697b8d 4783 case bp_fast_tracepoint:
0fb4aa4b 4784 case bp_static_tracepoint:
4efc6507 4785 case bp_jit_event:
0e30163f
JK
4786 case bp_gnu_ifunc_resolver:
4787 case bp_gnu_ifunc_resolver_return:
79a45b7d 4788 if (opts.addressprint)
3086aeae
DJ
4789 {
4790 annotate_field (4);
54e52265 4791 if (header_of_multiple)
0d381245 4792 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4793 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4794 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4795 else
5af949e3
UW
4796 ui_out_field_core_addr (uiout, "addr",
4797 loc->gdbarch, loc->address);
3086aeae
DJ
4798 }
4799 annotate_field (5);
0d381245 4800 if (!header_of_multiple)
170b53b2 4801 print_breakpoint_location (b, loc);
0d381245 4802 if (b->loc)
a6d9a66e 4803 *last_loc = b->loc;
3086aeae
DJ
4804 break;
4805 }
c906108c 4806
6c95b8df
PA
4807
4808 /* For backward compatibility, don't display inferiors unless there
4809 are several. */
4810 if (loc != NULL
4811 && !header_of_multiple
4812 && (allflag
4813 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4814 && (number_of_program_spaces () > 1
4815 || number_of_inferiors () > 1)
4a64f543
MS
4816 /* LOC is for existing B, it cannot be in
4817 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4818 && loc->owner->type != bp_catchpoint)))
4819 {
4820 struct inferior *inf;
4821 int first = 1;
4822
4823 for (inf = inferior_list; inf != NULL; inf = inf->next)
4824 {
4825 if (inf->pspace == loc->pspace)
4826 {
4827 if (first)
4828 {
4829 first = 0;
4830 ui_out_text (uiout, " inf ");
4831 }
4832 else
4833 ui_out_text (uiout, ", ");
4834 ui_out_text (uiout, plongest (inf->num));
4835 }
4836 }
4837 }
4838
4a306c9a 4839 if (!part_of_multiple)
c4093a6a 4840 {
4a306c9a
JB
4841 if (b->thread != -1)
4842 {
4843 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4844 "stop only in" line a little further down. */
4a306c9a
JB
4845 ui_out_text (uiout, " thread ");
4846 ui_out_field_int (uiout, "thread", b->thread);
4847 }
4848 else if (b->task != 0)
4849 {
4850 ui_out_text (uiout, " task ");
4851 ui_out_field_int (uiout, "task", b->task);
4852 }
c4093a6a 4853 }
f1310107 4854
8b93c638 4855 ui_out_text (uiout, "\n");
f1310107 4856
348d480f 4857 if (!part_of_multiple)
f1310107
TJB
4858 b->ops->print_one_detail (b, uiout);
4859
0d381245 4860 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4861 {
4862 annotate_field (6);
8b93c638 4863 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 4864 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 4865 the frame ID. */
5af949e3
UW
4866 ui_out_field_core_addr (uiout, "frame",
4867 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4868 ui_out_text (uiout, "\n");
c4093a6a
JM
4869 }
4870
28010a5d 4871 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
4872 {
4873 annotate_field (7);
d77f58be 4874 if (is_tracepoint (b))
1042e4c0
SS
4875 ui_out_text (uiout, "\ttrace only if ");
4876 else
4877 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4878 ui_out_field_string (uiout, "cond", b->cond_string);
4879 ui_out_text (uiout, "\n");
4880 }
4881
0d381245 4882 if (!part_of_multiple && b->thread != -1)
c4093a6a 4883 {
4a64f543 4884 /* FIXME should make an annotation for this. */
8b93c638
JM
4885 ui_out_text (uiout, "\tstop only in thread ");
4886 ui_out_field_int (uiout, "thread", b->thread);
4887 ui_out_text (uiout, "\n");
c4093a6a
JM
4888 }
4889
63c715c6 4890 if (!part_of_multiple && b->hit_count)
c4093a6a 4891 {
4a64f543 4892 /* FIXME should make an annotation for this. */
8b93c638
JM
4893 if (ep_is_catchpoint (b))
4894 ui_out_text (uiout, "\tcatchpoint");
f196051f
SS
4895 else if (is_tracepoint (b))
4896 ui_out_text (uiout, "\ttracepoint");
8b93c638
JM
4897 else
4898 ui_out_text (uiout, "\tbreakpoint");
4899 ui_out_text (uiout, " already hit ");
4900 ui_out_field_int (uiout, "times", b->hit_count);
4901 if (b->hit_count == 1)
4902 ui_out_text (uiout, " time\n");
4903 else
4904 ui_out_text (uiout, " times\n");
c4093a6a
JM
4905 }
4906
4a64f543
MS
4907 /* Output the count also if it is zero, but only if this is mi.
4908 FIXME: Should have a better test for this. */
9dc5e2a9 4909 if (ui_out_is_mi_like_p (uiout))
63c715c6 4910 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4911 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4912
0d381245 4913 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4914 {
4915 annotate_field (8);
8b93c638
JM
4916 ui_out_text (uiout, "\tignore next ");
4917 ui_out_field_int (uiout, "ignore", b->ignore_count);
4918 ui_out_text (uiout, " hits\n");
c4093a6a 4919 }
059fb39f 4920
f196051f
SS
4921 if (!part_of_multiple && is_tracepoint (b))
4922 {
4923 struct tracepoint *tp = (struct tracepoint *) b;
4924
4925 if (tp->traceframe_usage)
4926 {
4927 ui_out_text (uiout, "\ttrace buffer usage ");
4928 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
4929 ui_out_text (uiout, " bytes\n");
4930 }
4931 }
4932
9add0f1b 4933 l = b->commands ? b->commands->commands : NULL;
059fb39f 4934 if (!part_of_multiple && l)
c4093a6a 4935 {
3b31d625
EZ
4936 struct cleanup *script_chain;
4937
c4093a6a 4938 annotate_field (9);
3b31d625 4939 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4940 print_command_lines (uiout, l, 4);
3b31d625 4941 do_cleanups (script_chain);
c4093a6a 4942 }
d24317b4 4943
d9b3f62e 4944 if (is_tracepoint (b))
1042e4c0 4945 {
d9b3f62e
PA
4946 struct tracepoint *t = (struct tracepoint *) b;
4947
4948 if (!part_of_multiple && t->pass_count)
4949 {
4950 annotate_field (10);
4951 ui_out_text (uiout, "\tpass count ");
4952 ui_out_field_int (uiout, "pass", t->pass_count);
4953 ui_out_text (uiout, " \n");
4954 }
1042e4c0
SS
4955 }
4956
d24317b4
VP
4957 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4958 {
3a5c3e22
PA
4959 if (is_watchpoint (b))
4960 {
4961 struct watchpoint *w = (struct watchpoint *) b;
4962
4963 ui_out_field_string (uiout, "original-location", w->exp_string);
4964 }
4965 else if (b->addr_string)
d24317b4 4966 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 4967 }
c4093a6a 4968}
c5aa993b 4969
0d381245
VP
4970static void
4971print_one_breakpoint (struct breakpoint *b,
4a64f543 4972 struct bp_location **last_loc,
6c95b8df 4973 int allflag)
0d381245 4974{
8d3788bd 4975 struct cleanup *bkpt_chain;
79a45e25 4976 struct ui_out *uiout = current_uiout;
8d3788bd
VP
4977
4978 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
4979
12c5a436 4980 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 4981 do_cleanups (bkpt_chain);
0d381245
VP
4982
4983 /* If this breakpoint has custom print function,
4984 it's already printed. Otherwise, print individual
4985 locations, if any. */
4986 if (b->ops == NULL || b->ops->print_one == NULL)
4987 {
4a64f543
MS
4988 /* If breakpoint has a single location that is disabled, we
4989 print it as if it had several locations, since otherwise it's
4990 hard to represent "breakpoint enabled, location disabled"
4991 situation.
4992
4993 Note that while hardware watchpoints have several locations
a3be7890 4994 internally, that's not a property exposed to user. */
0d381245 4995 if (b->loc
a5606eee 4996 && !is_hardware_watchpoint (b)
8d3788bd 4997 && (b->loc->next || !b->loc->enabled))
0d381245
VP
4998 {
4999 struct bp_location *loc;
5000 int n = 1;
8d3788bd 5001
0d381245 5002 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5003 {
5004 struct cleanup *inner2 =
5005 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5006 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5007 do_cleanups (inner2);
5008 }
0d381245
VP
5009 }
5010 }
5011}
5012
a6d9a66e
UW
5013static int
5014breakpoint_address_bits (struct breakpoint *b)
5015{
5016 int print_address_bits = 0;
5017 struct bp_location *loc;
5018
5019 for (loc = b->loc; loc; loc = loc->next)
5020 {
c7437ca6
PA
5021 int addr_bit;
5022
5023 /* Software watchpoints that aren't watching memory don't have
5024 an address to print. */
5025 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5026 continue;
5027
5028 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5029 if (addr_bit > print_address_bits)
5030 print_address_bits = addr_bit;
5031 }
5032
5033 return print_address_bits;
5034}
0d381245 5035
c4093a6a
JM
5036struct captured_breakpoint_query_args
5037 {
5038 int bnum;
5039 };
c5aa993b 5040
c4093a6a 5041static int
2b65245e 5042do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5043{
5044 struct captured_breakpoint_query_args *args = data;
52f0bd74 5045 struct breakpoint *b;
a6d9a66e 5046 struct bp_location *dummy_loc = NULL;
cc59ec59 5047
c4093a6a
JM
5048 ALL_BREAKPOINTS (b)
5049 {
5050 if (args->bnum == b->number)
c5aa993b 5051 {
12c5a436 5052 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5053 return GDB_RC_OK;
c5aa993b 5054 }
c4093a6a
JM
5055 }
5056 return GDB_RC_NONE;
5057}
c5aa993b 5058
c4093a6a 5059enum gdb_rc
4a64f543
MS
5060gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5061 char **error_message)
c4093a6a
JM
5062{
5063 struct captured_breakpoint_query_args args;
cc59ec59 5064
c4093a6a
JM
5065 args.bnum = bnum;
5066 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5067 an error. */
b0b13bb4
DJ
5068 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5069 error_message, RETURN_MASK_ALL) < 0)
5070 return GDB_RC_FAIL;
5071 else
5072 return GDB_RC_OK;
c4093a6a 5073}
c5aa993b 5074
09d682a4
TT
5075/* Return true if this breakpoint was set by the user, false if it is
5076 internal or momentary. */
5077
5078int
5079user_breakpoint_p (struct breakpoint *b)
5080{
46c6471b 5081 return b->number > 0;
09d682a4
TT
5082}
5083
7f3b0473 5084/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5085 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5086 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5087 FILTER is non-NULL, call it on each breakpoint and only include the
5088 ones for which it returns non-zero. Return the total number of
5089 breakpoints listed. */
c906108c 5090
d77f58be 5091static int
e5a67952 5092breakpoint_1 (char *args, int allflag,
4a64f543 5093 int (*filter) (const struct breakpoint *))
c4093a6a 5094{
52f0bd74 5095 struct breakpoint *b;
a6d9a66e 5096 struct bp_location *last_loc = NULL;
7f3b0473 5097 int nr_printable_breakpoints;
3b31d625 5098 struct cleanup *bkpttbl_chain;
79a45b7d 5099 struct value_print_options opts;
a6d9a66e 5100 int print_address_bits = 0;
269b11a2 5101 int print_type_col_width = 14;
79a45e25 5102 struct ui_out *uiout = current_uiout;
269b11a2 5103
79a45b7d
TT
5104 get_user_print_options (&opts);
5105
4a64f543
MS
5106 /* Compute the number of rows in the table, as well as the size
5107 required for address fields. */
7f3b0473
AC
5108 nr_printable_breakpoints = 0;
5109 ALL_BREAKPOINTS (b)
e5a67952
MS
5110 {
5111 /* If we have a filter, only list the breakpoints it accepts. */
5112 if (filter && !filter (b))
5113 continue;
5114
5115 /* If we have an "args" string, it is a list of breakpoints to
5116 accept. Skip the others. */
5117 if (args != NULL && *args != '\0')
5118 {
5119 if (allflag && parse_and_eval_long (args) != b->number)
5120 continue;
5121 if (!allflag && !number_is_in_list (args, b->number))
5122 continue;
5123 }
269b11a2 5124
e5a67952
MS
5125 if (allflag || user_breakpoint_p (b))
5126 {
5127 int addr_bit, type_len;
a6d9a66e 5128
e5a67952
MS
5129 addr_bit = breakpoint_address_bits (b);
5130 if (addr_bit > print_address_bits)
5131 print_address_bits = addr_bit;
269b11a2 5132
e5a67952
MS
5133 type_len = strlen (bptype_string (b->type));
5134 if (type_len > print_type_col_width)
5135 print_type_col_width = type_len;
5136
5137 nr_printable_breakpoints++;
5138 }
5139 }
7f3b0473 5140
79a45b7d 5141 if (opts.addressprint)
3b31d625 5142 bkpttbl_chain
3e43a32a
MS
5143 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5144 nr_printable_breakpoints,
3b31d625 5145 "BreakpointTable");
8b93c638 5146 else
3b31d625 5147 bkpttbl_chain
3e43a32a
MS
5148 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5149 nr_printable_breakpoints,
3b31d625 5150 "BreakpointTable");
8b93c638 5151
7f3b0473 5152 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5153 annotate_breakpoints_headers ();
5154 if (nr_printable_breakpoints > 0)
5155 annotate_field (0);
4a64f543 5156 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5157 if (nr_printable_breakpoints > 0)
5158 annotate_field (1);
269b11a2 5159 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5160 "type", "Type"); /* 2 */
d7faa9e7
AC
5161 if (nr_printable_breakpoints > 0)
5162 annotate_field (2);
4a64f543 5163 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5164 if (nr_printable_breakpoints > 0)
5165 annotate_field (3);
54e52265 5166 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5167 if (opts.addressprint)
e5a67952
MS
5168 {
5169 if (nr_printable_breakpoints > 0)
5170 annotate_field (4);
5171 if (print_address_bits <= 32)
5172 ui_out_table_header (uiout, 10, ui_left,
5173 "addr", "Address"); /* 5 */
5174 else
5175 ui_out_table_header (uiout, 18, ui_left,
5176 "addr", "Address"); /* 5 */
5177 }
d7faa9e7
AC
5178 if (nr_printable_breakpoints > 0)
5179 annotate_field (5);
5180 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5181 ui_out_table_body (uiout);
5182 if (nr_printable_breakpoints > 0)
5183 annotate_breakpoints_table ();
7f3b0473 5184
c4093a6a 5185 ALL_BREAKPOINTS (b)
e5a67952
MS
5186 {
5187 QUIT;
5188 /* If we have a filter, only list the breakpoints it accepts. */
5189 if (filter && !filter (b))
5190 continue;
5191
5192 /* If we have an "args" string, it is a list of breakpoints to
5193 accept. Skip the others. */
5194
5195 if (args != NULL && *args != '\0')
5196 {
5197 if (allflag) /* maintenance info breakpoint */
5198 {
5199 if (parse_and_eval_long (args) != b->number)
5200 continue;
5201 }
5202 else /* all others */
5203 {
5204 if (!number_is_in_list (args, b->number))
5205 continue;
5206 }
5207 }
5208 /* We only print out user settable breakpoints unless the
5209 allflag is set. */
5210 if (allflag || user_breakpoint_p (b))
12c5a436 5211 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5212 }
5213
3b31d625 5214 do_cleanups (bkpttbl_chain);
698384cd 5215
7f3b0473 5216 if (nr_printable_breakpoints == 0)
c906108c 5217 {
4a64f543
MS
5218 /* If there's a filter, let the caller decide how to report
5219 empty list. */
d77f58be
SS
5220 if (!filter)
5221 {
e5a67952 5222 if (args == NULL || *args == '\0')
d77f58be
SS
5223 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5224 else
4a64f543 5225 ui_out_message (uiout, 0,
e5a67952
MS
5226 "No breakpoint or watchpoint matching '%s'.\n",
5227 args);
d77f58be 5228 }
c906108c
SS
5229 }
5230 else
c4093a6a 5231 {
a6d9a66e
UW
5232 if (last_loc && !server_command)
5233 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5234 }
c906108c 5235
4a64f543 5236 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5237 there have been breakpoints? */
c906108c 5238 annotate_breakpoints_table_end ();
d77f58be
SS
5239
5240 return nr_printable_breakpoints;
c906108c
SS
5241}
5242
ad443146
SS
5243/* Display the value of default-collect in a way that is generally
5244 compatible with the breakpoint list. */
5245
5246static void
5247default_collect_info (void)
5248{
79a45e25
PA
5249 struct ui_out *uiout = current_uiout;
5250
ad443146
SS
5251 /* If it has no value (which is frequently the case), say nothing; a
5252 message like "No default-collect." gets in user's face when it's
5253 not wanted. */
5254 if (!*default_collect)
5255 return;
5256
5257 /* The following phrase lines up nicely with per-tracepoint collect
5258 actions. */
5259 ui_out_text (uiout, "default collect ");
5260 ui_out_field_string (uiout, "default-collect", default_collect);
5261 ui_out_text (uiout, " \n");
5262}
5263
c906108c 5264static void
e5a67952 5265breakpoints_info (char *args, int from_tty)
c906108c 5266{
e5a67952 5267 breakpoint_1 (args, 0, NULL);
ad443146
SS
5268
5269 default_collect_info ();
d77f58be
SS
5270}
5271
5272static void
e5a67952 5273watchpoints_info (char *args, int from_tty)
d77f58be 5274{
e5a67952 5275 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 5276 struct ui_out *uiout = current_uiout;
d77f58be
SS
5277
5278 if (num_printed == 0)
5279 {
e5a67952 5280 if (args == NULL || *args == '\0')
d77f58be
SS
5281 ui_out_message (uiout, 0, "No watchpoints.\n");
5282 else
e5a67952 5283 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5284 }
c906108c
SS
5285}
5286
7a292a7a 5287static void
e5a67952 5288maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5289{
e5a67952 5290 breakpoint_1 (args, 1, NULL);
ad443146
SS
5291
5292 default_collect_info ();
c906108c
SS
5293}
5294
0d381245 5295static int
714835d5 5296breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5297 struct program_space *pspace,
714835d5 5298 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5299{
5300 struct bp_location *bl = b->loc;
cc59ec59 5301
0d381245
VP
5302 for (; bl; bl = bl->next)
5303 {
6c95b8df
PA
5304 if (bl->pspace == pspace
5305 && bl->address == pc
0d381245
VP
5306 && (!overlay_debugging || bl->section == section))
5307 return 1;
5308 }
5309 return 0;
5310}
5311
672f9b60 5312/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
5313 concerns with logical breakpoints, so we match program spaces, not
5314 address spaces. */
c906108c
SS
5315
5316static void
6c95b8df
PA
5317describe_other_breakpoints (struct gdbarch *gdbarch,
5318 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5319 struct obj_section *section, int thread)
c906108c 5320{
52f0bd74
AC
5321 int others = 0;
5322 struct breakpoint *b;
c906108c
SS
5323
5324 ALL_BREAKPOINTS (b)
672f9b60
KP
5325 others += (user_breakpoint_p (b)
5326 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
5327 if (others > 0)
5328 {
a3f17187
AC
5329 if (others == 1)
5330 printf_filtered (_("Note: breakpoint "));
5331 else /* if (others == ???) */
5332 printf_filtered (_("Note: breakpoints "));
c906108c 5333 ALL_BREAKPOINTS (b)
672f9b60 5334 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5335 {
5336 others--;
5337 printf_filtered ("%d", b->number);
5338 if (b->thread == -1 && thread != -1)
5339 printf_filtered (" (all threads)");
5340 else if (b->thread != -1)
5341 printf_filtered (" (thread %d)", b->thread);
5342 printf_filtered ("%s%s ",
059fb39f 5343 ((b->enable_state == bp_disabled
8bea4e01
UW
5344 || b->enable_state == bp_call_disabled
5345 || b->enable_state == bp_startup_disabled)
0d381245
VP
5346 ? " (disabled)"
5347 : b->enable_state == bp_permanent
5348 ? " (permanent)"
5349 : ""),
5350 (others > 1) ? ","
5351 : ((others == 1) ? " and" : ""));
5352 }
a3f17187 5353 printf_filtered (_("also set at pc "));
5af949e3 5354 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5355 printf_filtered (".\n");
5356 }
5357}
5358\f
c906108c 5359
e4f237da
KB
5360/* Return true iff it is meaningful to use the address member of
5361 BPT. For some breakpoint types, the address member is irrelevant
5362 and it makes no sense to attempt to compare it to other addresses
5363 (or use it for any other purpose either).
5364
4a64f543
MS
5365 More specifically, each of the following breakpoint types will
5366 always have a zero valued address and we don't want to mark
5367 breakpoints of any of these types to be a duplicate of an actual
5368 breakpoint at address zero:
e4f237da
KB
5369
5370 bp_watchpoint
2d134ed3
PA
5371 bp_catchpoint
5372
5373*/
e4f237da
KB
5374
5375static int
5376breakpoint_address_is_meaningful (struct breakpoint *bpt)
5377{
5378 enum bptype type = bpt->type;
5379
2d134ed3
PA
5380 return (type != bp_watchpoint && type != bp_catchpoint);
5381}
5382
5383/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5384 true if LOC1 and LOC2 represent the same watchpoint location. */
5385
5386static int
4a64f543
MS
5387watchpoint_locations_match (struct bp_location *loc1,
5388 struct bp_location *loc2)
2d134ed3 5389{
3a5c3e22
PA
5390 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
5391 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
5392
5393 /* Both of them must exist. */
5394 gdb_assert (w1 != NULL);
5395 gdb_assert (w2 != NULL);
2bdf28a0 5396
4a64f543
MS
5397 /* If the target can evaluate the condition expression in hardware,
5398 then we we need to insert both watchpoints even if they are at
5399 the same place. Otherwise the watchpoint will only trigger when
5400 the condition of whichever watchpoint was inserted evaluates to
5401 true, not giving a chance for GDB to check the condition of the
5402 other watchpoint. */
3a5c3e22 5403 if ((w1->cond_exp
4a64f543
MS
5404 && target_can_accel_watchpoint_condition (loc1->address,
5405 loc1->length,
0cf6dd15 5406 loc1->watchpoint_type,
3a5c3e22
PA
5407 w1->cond_exp))
5408 || (w2->cond_exp
4a64f543
MS
5409 && target_can_accel_watchpoint_condition (loc2->address,
5410 loc2->length,
0cf6dd15 5411 loc2->watchpoint_type,
3a5c3e22 5412 w2->cond_exp)))
0cf6dd15
TJB
5413 return 0;
5414
85d721b8
PA
5415 /* Note that this checks the owner's type, not the location's. In
5416 case the target does not support read watchpoints, but does
5417 support access watchpoints, we'll have bp_read_watchpoint
5418 watchpoints with hw_access locations. Those should be considered
5419 duplicates of hw_read locations. The hw_read locations will
5420 become hw_access locations later. */
2d134ed3
PA
5421 return (loc1->owner->type == loc2->owner->type
5422 && loc1->pspace->aspace == loc2->pspace->aspace
5423 && loc1->address == loc2->address
5424 && loc1->length == loc2->length);
e4f237da
KB
5425}
5426
6c95b8df
PA
5427/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5428 same breakpoint location. In most targets, this can only be true
5429 if ASPACE1 matches ASPACE2. On targets that have global
5430 breakpoints, the address space doesn't really matter. */
5431
5432static int
5433breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5434 struct address_space *aspace2, CORE_ADDR addr2)
5435{
5436 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5437 || aspace1 == aspace2)
5438 && addr1 == addr2);
5439}
5440
f1310107
TJB
5441/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5442 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5443 matches ASPACE2. On targets that have global breakpoints, the address
5444 space doesn't really matter. */
5445
5446static int
5447breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5448 int len1, struct address_space *aspace2,
5449 CORE_ADDR addr2)
5450{
5451 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5452 || aspace1 == aspace2)
5453 && addr2 >= addr1 && addr2 < addr1 + len1);
5454}
5455
5456/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5457 a ranged breakpoint. In most targets, a match happens only if ASPACE
5458 matches the breakpoint's address space. On targets that have global
5459 breakpoints, the address space doesn't really matter. */
5460
5461static int
5462breakpoint_location_address_match (struct bp_location *bl,
5463 struct address_space *aspace,
5464 CORE_ADDR addr)
5465{
5466 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5467 aspace, addr)
5468 || (bl->length
5469 && breakpoint_address_match_range (bl->pspace->aspace,
5470 bl->address, bl->length,
5471 aspace, addr)));
5472}
5473
1e4d1764
YQ
5474/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
5475 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
5476 true, otherwise returns false. */
5477
5478static int
5479tracepoint_locations_match (struct bp_location *loc1,
5480 struct bp_location *loc2)
5481{
5482 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
5483 /* Since tracepoint locations are never duplicated with others', tracepoint
5484 locations at the same address of different tracepoints are regarded as
5485 different locations. */
5486 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
5487 else
5488 return 0;
5489}
5490
2d134ed3
PA
5491/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5492 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5493 represent the same location. */
5494
5495static int
4a64f543
MS
5496breakpoint_locations_match (struct bp_location *loc1,
5497 struct bp_location *loc2)
2d134ed3 5498{
2bdf28a0
JK
5499 int hw_point1, hw_point2;
5500
5501 /* Both of them must not be in moribund_locations. */
5502 gdb_assert (loc1->owner != NULL);
5503 gdb_assert (loc2->owner != NULL);
5504
5505 hw_point1 = is_hardware_watchpoint (loc1->owner);
5506 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5507
5508 if (hw_point1 != hw_point2)
5509 return 0;
5510 else if (hw_point1)
5511 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
5512 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
5513 return tracepoint_locations_match (loc1, loc2);
2d134ed3 5514 else
f1310107
TJB
5515 /* We compare bp_location.length in order to cover ranged breakpoints. */
5516 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5517 loc2->pspace->aspace, loc2->address)
5518 && loc1->length == loc2->length);
2d134ed3
PA
5519}
5520
76897487
KB
5521static void
5522breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5523 int bnum, int have_bnum)
5524{
f63fbe86
MS
5525 /* The longest string possibly returned by hex_string_custom
5526 is 50 chars. These must be at least that big for safety. */
5527 char astr1[64];
5528 char astr2[64];
76897487 5529
bb599908
PH
5530 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5531 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5532 if (have_bnum)
8a3fe4f8 5533 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5534 bnum, astr1, astr2);
5535 else
8a3fe4f8 5536 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5537}
5538
4a64f543
MS
5539/* Adjust a breakpoint's address to account for architectural
5540 constraints on breakpoint placement. Return the adjusted address.
5541 Note: Very few targets require this kind of adjustment. For most
5542 targets, this function is simply the identity function. */
76897487
KB
5543
5544static CORE_ADDR
a6d9a66e
UW
5545adjust_breakpoint_address (struct gdbarch *gdbarch,
5546 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5547{
a6d9a66e 5548 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5549 {
5550 /* Very few targets need any kind of breakpoint adjustment. */
5551 return bpaddr;
5552 }
88f7da05
KB
5553 else if (bptype == bp_watchpoint
5554 || bptype == bp_hardware_watchpoint
5555 || bptype == bp_read_watchpoint
5556 || bptype == bp_access_watchpoint
fe798b75 5557 || bptype == bp_catchpoint)
88f7da05
KB
5558 {
5559 /* Watchpoints and the various bp_catch_* eventpoints should not
5560 have their addresses modified. */
5561 return bpaddr;
5562 }
76897487
KB
5563 else
5564 {
5565 CORE_ADDR adjusted_bpaddr;
5566
5567 /* Some targets have architectural constraints on the placement
5568 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5569 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5570
5571 /* An adjusted breakpoint address can significantly alter
5572 a user's expectations. Print a warning if an adjustment
5573 is required. */
5574 if (adjusted_bpaddr != bpaddr)
5575 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5576
5577 return adjusted_bpaddr;
5578 }
5579}
5580
28010a5d
PA
5581void
5582init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5583 struct breakpoint *owner)
7cc221ef 5584{
7cc221ef
DJ
5585 memset (loc, 0, sizeof (*loc));
5586
348d480f
PA
5587 gdb_assert (ops != NULL);
5588
28010a5d
PA
5589 loc->ops = ops;
5590 loc->owner = owner;
511a6cd4 5591 loc->cond = NULL;
0d381245
VP
5592 loc->shlib_disabled = 0;
5593 loc->enabled = 1;
e049a4b5 5594
28010a5d 5595 switch (owner->type)
e049a4b5
DJ
5596 {
5597 case bp_breakpoint:
5598 case bp_until:
5599 case bp_finish:
5600 case bp_longjmp:
5601 case bp_longjmp_resume:
186c406b
TT
5602 case bp_exception:
5603 case bp_exception_resume:
e049a4b5 5604 case bp_step_resume:
2c03e5be 5605 case bp_hp_step_resume:
e049a4b5
DJ
5606 case bp_watchpoint_scope:
5607 case bp_call_dummy:
aa7d318d 5608 case bp_std_terminate:
e049a4b5
DJ
5609 case bp_shlib_event:
5610 case bp_thread_event:
5611 case bp_overlay_event:
4efc6507 5612 case bp_jit_event:
0fd8e87f 5613 case bp_longjmp_master:
aa7d318d 5614 case bp_std_terminate_master:
186c406b 5615 case bp_exception_master:
0e30163f
JK
5616 case bp_gnu_ifunc_resolver:
5617 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5618 loc->loc_type = bp_loc_software_breakpoint;
5619 break;
5620 case bp_hardware_breakpoint:
5621 loc->loc_type = bp_loc_hardware_breakpoint;
5622 break;
5623 case bp_hardware_watchpoint:
5624 case bp_read_watchpoint:
5625 case bp_access_watchpoint:
5626 loc->loc_type = bp_loc_hardware_watchpoint;
5627 break;
5628 case bp_watchpoint:
ce78b96d 5629 case bp_catchpoint:
15c3d785
PA
5630 case bp_tracepoint:
5631 case bp_fast_tracepoint:
0fb4aa4b 5632 case bp_static_tracepoint:
e049a4b5
DJ
5633 loc->loc_type = bp_loc_other;
5634 break;
5635 default:
e2e0b3e5 5636 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5637 }
5638
f431efe5 5639 loc->refc = 1;
28010a5d
PA
5640}
5641
5642/* Allocate a struct bp_location. */
5643
5644static struct bp_location *
5645allocate_bp_location (struct breakpoint *bpt)
5646{
348d480f
PA
5647 return bpt->ops->allocate_location (bpt);
5648}
7cc221ef 5649
f431efe5
PA
5650static void
5651free_bp_location (struct bp_location *loc)
fe3f5fa8 5652{
348d480f 5653 loc->ops->dtor (loc);
fe3f5fa8
VP
5654 xfree (loc);
5655}
5656
f431efe5
PA
5657/* Increment reference count. */
5658
5659static void
5660incref_bp_location (struct bp_location *bl)
5661{
5662 ++bl->refc;
5663}
5664
5665/* Decrement reference count. If the reference count reaches 0,
5666 destroy the bp_location. Sets *BLP to NULL. */
5667
5668static void
5669decref_bp_location (struct bp_location **blp)
5670{
0807b50c
PA
5671 gdb_assert ((*blp)->refc > 0);
5672
f431efe5
PA
5673 if (--(*blp)->refc == 0)
5674 free_bp_location (*blp);
5675 *blp = NULL;
5676}
5677
346774a9 5678/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 5679
346774a9
PA
5680static void
5681add_to_breakpoint_chain (struct breakpoint *b)
c906108c 5682{
346774a9 5683 struct breakpoint *b1;
c906108c 5684
346774a9
PA
5685 /* Add this breakpoint to the end of the chain so that a list of
5686 breakpoints will come out in order of increasing numbers. */
5687
5688 b1 = breakpoint_chain;
5689 if (b1 == 0)
5690 breakpoint_chain = b;
5691 else
5692 {
5693 while (b1->next)
5694 b1 = b1->next;
5695 b1->next = b;
5696 }
5697}
5698
5699/* Initializes breakpoint B with type BPTYPE and no locations yet. */
5700
5701static void
5702init_raw_breakpoint_without_location (struct breakpoint *b,
5703 struct gdbarch *gdbarch,
28010a5d 5704 enum bptype bptype,
c0a91b2b 5705 const struct breakpoint_ops *ops)
346774a9 5706{
c906108c 5707 memset (b, 0, sizeof (*b));
2219d63c 5708
348d480f
PA
5709 gdb_assert (ops != NULL);
5710
28010a5d 5711 b->ops = ops;
4d28f7a8 5712 b->type = bptype;
a6d9a66e 5713 b->gdbarch = gdbarch;
c906108c
SS
5714 b->language = current_language->la_language;
5715 b->input_radix = input_radix;
5716 b->thread = -1;
b5de0fa7 5717 b->enable_state = bp_enabled;
c906108c
SS
5718 b->next = 0;
5719 b->silent = 0;
5720 b->ignore_count = 0;
5721 b->commands = NULL;
818dd999 5722 b->frame_id = null_frame_id;
0d381245 5723 b->condition_not_parsed = 0;
84f4c1fe 5724 b->py_bp_object = NULL;
d0fb5eae 5725 b->related_breakpoint = b;
346774a9
PA
5726}
5727
5728/* Helper to set_raw_breakpoint below. Creates a breakpoint
5729 that has type BPTYPE and has no locations as yet. */
346774a9
PA
5730
5731static struct breakpoint *
5732set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 5733 enum bptype bptype,
c0a91b2b 5734 const struct breakpoint_ops *ops)
346774a9
PA
5735{
5736 struct breakpoint *b = XNEW (struct breakpoint);
5737
348d480f 5738 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 5739 add_to_breakpoint_chain (b);
0d381245
VP
5740 return b;
5741}
5742
0e30163f
JK
5743/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5744 resolutions should be made as the user specified the location explicitly
5745 enough. */
5746
0d381245 5747static void
0e30163f 5748set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5749{
2bdf28a0
JK
5750 gdb_assert (loc->owner != NULL);
5751
0d381245 5752 if (loc->owner->type == bp_breakpoint
1042e4c0 5753 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5754 || is_tracepoint (loc->owner))
0d381245 5755 {
0e30163f
JK
5756 int is_gnu_ifunc;
5757
5758 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5759 NULL, NULL, &is_gnu_ifunc);
5760
5761 if (is_gnu_ifunc && !explicit_loc)
5762 {
5763 struct breakpoint *b = loc->owner;
5764
5765 gdb_assert (loc->pspace == current_program_space);
5766 if (gnu_ifunc_resolve_name (loc->function_name,
5767 &loc->requested_address))
5768 {
5769 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5770 loc->address = adjust_breakpoint_address (loc->gdbarch,
5771 loc->requested_address,
5772 b->type);
5773 }
5774 else if (b->type == bp_breakpoint && b->loc == loc
5775 && loc->next == NULL && b->related_breakpoint == b)
5776 {
5777 /* Create only the whole new breakpoint of this type but do not
5778 mess more complicated breakpoints with multiple locations. */
5779 b->type = bp_gnu_ifunc_resolver;
5780 }
5781 }
5782
0d381245
VP
5783 if (loc->function_name)
5784 loc->function_name = xstrdup (loc->function_name);
5785 }
5786}
5787
a6d9a66e 5788/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 5789struct gdbarch *
a6d9a66e
UW
5790get_sal_arch (struct symtab_and_line sal)
5791{
5792 if (sal.section)
5793 return get_objfile_arch (sal.section->objfile);
5794 if (sal.symtab)
5795 return get_objfile_arch (sal.symtab->objfile);
5796
5797 return NULL;
5798}
5799
346774a9
PA
5800/* Low level routine for partially initializing a breakpoint of type
5801 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 5802 file name, and line number are provided by SAL.
0d381245
VP
5803
5804 It is expected that the caller will complete the initialization of
5805 the newly created breakpoint struct as well as output any status
c56053d2 5806 information regarding the creation of a new breakpoint. */
0d381245 5807
346774a9
PA
5808static void
5809init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 5810 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5811 const struct breakpoint_ops *ops)
0d381245 5812{
28010a5d 5813 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 5814
3742cc8b 5815 add_location_to_breakpoint (b, &sal);
0d381245 5816
6c95b8df
PA
5817 if (bptype != bp_catchpoint)
5818 gdb_assert (sal.pspace != NULL);
5819
6c95b8df
PA
5820 /* Store the program space that was used to set the breakpoint, for
5821 breakpoint resetting. */
5822 b->pspace = sal.pspace;
0d381245
VP
5823
5824 if (sal.symtab == NULL)
5825 b->source_file = NULL;
5826 else
1b36a34b 5827 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5828 b->line_number = sal.line;
5829
c906108c 5830 breakpoints_changed ();
346774a9 5831}
c906108c 5832
346774a9
PA
5833/* set_raw_breakpoint is a low level routine for allocating and
5834 partially initializing a breakpoint of type BPTYPE. The newly
5835 created breakpoint's address, section, source file name, and line
5836 number are provided by SAL. The newly created and partially
5837 initialized breakpoint is added to the breakpoint chain and
5838 is also returned as the value of this function.
5839
5840 It is expected that the caller will complete the initialization of
5841 the newly created breakpoint struct as well as output any status
5842 information regarding the creation of a new breakpoint. In
5843 particular, set_raw_breakpoint does NOT set the breakpoint
5844 number! Care should be taken to not allow an error to occur
5845 prior to completing the initialization of the breakpoint. If this
5846 should happen, a bogus breakpoint will be left on the chain. */
5847
5848struct breakpoint *
5849set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 5850 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5851 const struct breakpoint_ops *ops)
346774a9
PA
5852{
5853 struct breakpoint *b = XNEW (struct breakpoint);
5854
348d480f 5855 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 5856 add_to_breakpoint_chain (b);
c906108c
SS
5857 return b;
5858}
5859
c2c6d25f
JM
5860
5861/* Note that the breakpoint object B describes a permanent breakpoint
5862 instruction, hard-wired into the inferior's code. */
5863void
5864make_breakpoint_permanent (struct breakpoint *b)
5865{
0d381245 5866 struct bp_location *bl;
cc59ec59 5867
b5de0fa7 5868 b->enable_state = bp_permanent;
c2c6d25f 5869
4a64f543
MS
5870 /* By definition, permanent breakpoints are already present in the
5871 code. Mark all locations as inserted. For now,
5872 make_breakpoint_permanent is called in just one place, so it's
5873 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 5874 multiple locations or not, but it's easy to implement. */
0d381245
VP
5875 for (bl = b->loc; bl; bl = bl->next)
5876 bl->inserted = 1;
c2c6d25f
JM
5877}
5878
53a5351d 5879/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5880 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5881 initiated the operation. */
c906108c
SS
5882
5883void
186c406b 5884set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5885{
35df4500 5886 struct breakpoint *b, *b_tmp;
186c406b 5887 int thread = tp->num;
0fd8e87f
UW
5888
5889 /* To avoid having to rescan all objfile symbols at every step,
5890 we maintain a list of continually-inserted but always disabled
5891 longjmp "master" breakpoints. Here, we simply create momentary
5892 clones of those and enable them for the requested thread. */
35df4500 5893 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5894 if (b->pspace == current_program_space
186c406b
TT
5895 && (b->type == bp_longjmp_master
5896 || b->type == bp_exception_master))
0fd8e87f 5897 {
06edf0c0
PA
5898 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5899 struct breakpoint *clone;
cc59ec59 5900
06edf0c0
PA
5901 clone = momentary_breakpoint_from_master (b, type,
5902 &momentary_breakpoint_ops);
0fd8e87f
UW
5903 clone->thread = thread;
5904 }
186c406b
TT
5905
5906 tp->initiating_frame = frame;
c906108c
SS
5907}
5908
611c83ae 5909/* Delete all longjmp breakpoints from THREAD. */
c906108c 5910void
611c83ae 5911delete_longjmp_breakpoint (int thread)
c906108c 5912{
35df4500 5913 struct breakpoint *b, *b_tmp;
c906108c 5914
35df4500 5915 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5916 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5917 {
5918 if (b->thread == thread)
5919 delete_breakpoint (b);
5920 }
c906108c
SS
5921}
5922
1900040c
MS
5923void
5924enable_overlay_breakpoints (void)
5925{
52f0bd74 5926 struct breakpoint *b;
1900040c
MS
5927
5928 ALL_BREAKPOINTS (b)
5929 if (b->type == bp_overlay_event)
5930 {
5931 b->enable_state = bp_enabled;
b60e7edf 5932 update_global_location_list (1);
c02f5703 5933 overlay_events_enabled = 1;
1900040c
MS
5934 }
5935}
5936
5937void
5938disable_overlay_breakpoints (void)
5939{
52f0bd74 5940 struct breakpoint *b;
1900040c
MS
5941
5942 ALL_BREAKPOINTS (b)
5943 if (b->type == bp_overlay_event)
5944 {
5945 b->enable_state = bp_disabled;
b60e7edf 5946 update_global_location_list (0);
c02f5703 5947 overlay_events_enabled = 0;
1900040c
MS
5948 }
5949}
5950
aa7d318d
TT
5951/* Set an active std::terminate breakpoint for each std::terminate
5952 master breakpoint. */
5953void
5954set_std_terminate_breakpoint (void)
5955{
35df4500 5956 struct breakpoint *b, *b_tmp;
aa7d318d 5957
35df4500 5958 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5959 if (b->pspace == current_program_space
5960 && b->type == bp_std_terminate_master)
5961 {
06edf0c0
PA
5962 momentary_breakpoint_from_master (b, bp_std_terminate,
5963 &momentary_breakpoint_ops);
aa7d318d
TT
5964 }
5965}
5966
5967/* Delete all the std::terminate breakpoints. */
5968void
5969delete_std_terminate_breakpoint (void)
5970{
35df4500 5971 struct breakpoint *b, *b_tmp;
aa7d318d 5972
35df4500 5973 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5974 if (b->type == bp_std_terminate)
5975 delete_breakpoint (b);
5976}
5977
c4093a6a 5978struct breakpoint *
a6d9a66e 5979create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
5980{
5981 struct breakpoint *b;
c4093a6a 5982
06edf0c0
PA
5983 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
5984 &internal_breakpoint_ops);
5985
b5de0fa7 5986 b->enable_state = bp_enabled;
c4093a6a 5987 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
5988 b->addr_string
5989 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 5990
b60e7edf 5991 update_global_location_list_nothrow (1);
74960c60 5992
c4093a6a
JM
5993 return b;
5994}
5995
5996void
5997remove_thread_event_breakpoints (void)
5998{
35df4500 5999 struct breakpoint *b, *b_tmp;
c4093a6a 6000
35df4500 6001 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6002 if (b->type == bp_thread_event
6003 && b->loc->pspace == current_program_space)
c4093a6a
JM
6004 delete_breakpoint (b);
6005}
6006
0101ce28
JJ
6007struct lang_and_radix
6008 {
6009 enum language lang;
6010 int radix;
6011 };
6012
4efc6507
DE
6013/* Create a breakpoint for JIT code registration and unregistration. */
6014
6015struct breakpoint *
6016create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6017{
6018 struct breakpoint *b;
6019
06edf0c0
PA
6020 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
6021 &internal_breakpoint_ops);
4efc6507
DE
6022 update_global_location_list_nothrow (1);
6023 return b;
6024}
0101ce28 6025
03673fc7
PP
6026/* Remove JIT code registration and unregistration breakpoint(s). */
6027
6028void
6029remove_jit_event_breakpoints (void)
6030{
6031 struct breakpoint *b, *b_tmp;
6032
6033 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6034 if (b->type == bp_jit_event
6035 && b->loc->pspace == current_program_space)
6036 delete_breakpoint (b);
6037}
6038
cae688ec
JJ
6039void
6040remove_solib_event_breakpoints (void)
6041{
35df4500 6042 struct breakpoint *b, *b_tmp;
cae688ec 6043
35df4500 6044 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6045 if (b->type == bp_shlib_event
6046 && b->loc->pspace == current_program_space)
cae688ec
JJ
6047 delete_breakpoint (b);
6048}
6049
6050struct breakpoint *
a6d9a66e 6051create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6052{
6053 struct breakpoint *b;
6054
06edf0c0
PA
6055 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6056 &internal_breakpoint_ops);
b60e7edf 6057 update_global_location_list_nothrow (1);
cae688ec
JJ
6058 return b;
6059}
6060
6061/* Disable any breakpoints that are on code in shared libraries. Only
6062 apply to enabled breakpoints, disabled ones can just stay disabled. */
6063
6064void
cb851954 6065disable_breakpoints_in_shlibs (void)
cae688ec 6066{
876fa593 6067 struct bp_location *loc, **locp_tmp;
cae688ec 6068
876fa593 6069 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6070 {
2bdf28a0 6071 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6072 struct breakpoint *b = loc->owner;
2bdf28a0 6073
4a64f543
MS
6074 /* We apply the check to all breakpoints, including disabled for
6075 those with loc->duplicate set. This is so that when breakpoint
6076 becomes enabled, or the duplicate is removed, gdb will try to
6077 insert all breakpoints. If we don't set shlib_disabled here,
6078 we'll try to insert those breakpoints and fail. */
1042e4c0 6079 if (((b->type == bp_breakpoint)
508ccb1f 6080 || (b->type == bp_jit_event)
1042e4c0 6081 || (b->type == bp_hardware_breakpoint)
d77f58be 6082 || (is_tracepoint (b)))
6c95b8df 6083 && loc->pspace == current_program_space
0d381245 6084 && !loc->shlib_disabled
a77053c2 6085#ifdef PC_SOLIB
0d381245 6086 && PC_SOLIB (loc->address)
a77053c2 6087#else
6c95b8df 6088 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6089#endif
6090 )
0d381245
VP
6091 {
6092 loc->shlib_disabled = 1;
6093 }
cae688ec
JJ
6094 }
6095}
6096
1e4d1764
YQ
6097/* Disable any breakpoints and tracepoints that are in an unloaded shared
6098 library. Only apply to enabled breakpoints, disabled ones can just stay
4a64f543 6099 disabled. */
84acb35a 6100
75149521 6101static void
84acb35a
JJ
6102disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6103{
876fa593 6104 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6105 int disabled_shlib_breaks = 0;
6106
c86cf029
VP
6107 /* SunOS a.out shared libraries are always mapped, so do not
6108 disable breakpoints; they will only be reported as unloaded
6109 through clear_solib when GDB discards its shared library
6110 list. See clear_solib for more information. */
6111 if (exec_bfd != NULL
6112 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6113 return;
6114
876fa593 6115 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6116 {
2bdf28a0 6117 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6118 struct breakpoint *b = loc->owner;
cc59ec59 6119
1e4d1764 6120 if (solib->pspace == loc->pspace
e2dd7057 6121 && !loc->shlib_disabled
1e4d1764
YQ
6122 && (((b->type == bp_breakpoint
6123 || b->type == bp_jit_event
6124 || b->type == bp_hardware_breakpoint)
6125 && (loc->loc_type == bp_loc_hardware_breakpoint
6126 || loc->loc_type == bp_loc_software_breakpoint))
6127 || is_tracepoint (b))
e2dd7057 6128 && solib_contains_address_p (solib, loc->address))
84acb35a 6129 {
e2dd7057
PP
6130 loc->shlib_disabled = 1;
6131 /* At this point, we cannot rely on remove_breakpoint
6132 succeeding so we must mark the breakpoint as not inserted
6133 to prevent future errors occurring in remove_breakpoints. */
6134 loc->inserted = 0;
8d3788bd
VP
6135
6136 /* This may cause duplicate notifications for the same breakpoint. */
6137 observer_notify_breakpoint_modified (b);
6138
e2dd7057
PP
6139 if (!disabled_shlib_breaks)
6140 {
6141 target_terminal_ours_for_output ();
3e43a32a
MS
6142 warning (_("Temporarily disabling breakpoints "
6143 "for unloaded shared library \"%s\""),
e2dd7057 6144 solib->so_name);
84acb35a 6145 }
e2dd7057 6146 disabled_shlib_breaks = 1;
84acb35a
JJ
6147 }
6148 }
84acb35a
JJ
6149}
6150
ce78b96d
JB
6151/* FORK & VFORK catchpoints. */
6152
e29a4733
PA
6153/* An instance of this type is used to represent a fork or vfork
6154 catchpoint. It includes a "struct breakpoint" as a kind of base
6155 class; users downcast to "struct breakpoint *" when needed. A
6156 breakpoint is really of this type iff its ops pointer points to
6157 CATCH_FORK_BREAKPOINT_OPS. */
6158
6159struct fork_catchpoint
6160{
6161 /* The base class. */
6162 struct breakpoint base;
6163
6164 /* Process id of a child process whose forking triggered this
6165 catchpoint. This field is only valid immediately after this
6166 catchpoint has triggered. */
6167 ptid_t forked_inferior_pid;
6168};
6169
4a64f543
MS
6170/* Implement the "insert" breakpoint_ops method for fork
6171 catchpoints. */
ce78b96d 6172
77b06cd7
TJB
6173static int
6174insert_catch_fork (struct bp_location *bl)
ce78b96d 6175{
77b06cd7 6176 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6177}
6178
4a64f543
MS
6179/* Implement the "remove" breakpoint_ops method for fork
6180 catchpoints. */
ce78b96d
JB
6181
6182static int
77b06cd7 6183remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6184{
6185 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6186}
6187
6188/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6189 catchpoints. */
6190
6191static int
f1310107
TJB
6192breakpoint_hit_catch_fork (const struct bp_location *bl,
6193 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6194{
e29a4733
PA
6195 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6196
6197 return inferior_has_forked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6198}
6199
4a64f543
MS
6200/* Implement the "print_it" breakpoint_ops method for fork
6201 catchpoints. */
ce78b96d
JB
6202
6203static enum print_stop_action
348d480f 6204print_it_catch_fork (bpstat bs)
ce78b96d 6205{
36dfb11c 6206 struct ui_out *uiout = current_uiout;
348d480f
PA
6207 struct breakpoint *b = bs->breakpoint_at;
6208 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 6209
ce78b96d 6210 annotate_catchpoint (b->number);
36dfb11c
TT
6211 if (b->disposition == disp_del)
6212 ui_out_text (uiout, "\nTemporary catchpoint ");
6213 else
6214 ui_out_text (uiout, "\nCatchpoint ");
6215 if (ui_out_is_mi_like_p (uiout))
6216 {
6217 ui_out_field_string (uiout, "reason",
6218 async_reason_lookup (EXEC_ASYNC_FORK));
6219 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6220 }
6221 ui_out_field_int (uiout, "bkptno", b->number);
6222 ui_out_text (uiout, " (forked process ");
6223 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6224 ui_out_text (uiout, "), ");
ce78b96d
JB
6225 return PRINT_SRC_AND_LOC;
6226}
6227
4a64f543
MS
6228/* Implement the "print_one" breakpoint_ops method for fork
6229 catchpoints. */
ce78b96d
JB
6230
6231static void
a6d9a66e 6232print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6233{
e29a4733 6234 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6235 struct value_print_options opts;
79a45e25 6236 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6237
6238 get_user_print_options (&opts);
6239
4a64f543
MS
6240 /* Field 4, the address, is omitted (which makes the columns not
6241 line up too nicely with the headers, but the effect is relatively
6242 readable). */
79a45b7d 6243 if (opts.addressprint)
ce78b96d
JB
6244 ui_out_field_skip (uiout, "addr");
6245 annotate_field (5);
6246 ui_out_text (uiout, "fork");
e29a4733 6247 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6248 {
6249 ui_out_text (uiout, ", process ");
6250 ui_out_field_int (uiout, "what",
e29a4733 6251 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6252 ui_out_spaces (uiout, 1);
6253 }
6254}
6255
6256/* Implement the "print_mention" breakpoint_ops method for fork
6257 catchpoints. */
6258
6259static void
6260print_mention_catch_fork (struct breakpoint *b)
6261{
6262 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6263}
6264
6149aea9
PA
6265/* Implement the "print_recreate" breakpoint_ops method for fork
6266 catchpoints. */
6267
6268static void
6269print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6270{
6271 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 6272 print_recreate_thread (b, fp);
6149aea9
PA
6273}
6274
ce78b96d
JB
6275/* The breakpoint_ops structure to be used in fork catchpoints. */
6276
2060206e 6277static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 6278
4a64f543
MS
6279/* Implement the "insert" breakpoint_ops method for vfork
6280 catchpoints. */
ce78b96d 6281
77b06cd7
TJB
6282static int
6283insert_catch_vfork (struct bp_location *bl)
ce78b96d 6284{
77b06cd7 6285 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6286}
6287
4a64f543
MS
6288/* Implement the "remove" breakpoint_ops method for vfork
6289 catchpoints. */
ce78b96d
JB
6290
6291static int
77b06cd7 6292remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6293{
6294 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6295}
6296
6297/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6298 catchpoints. */
6299
6300static int
f1310107
TJB
6301breakpoint_hit_catch_vfork (const struct bp_location *bl,
6302 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6303{
e29a4733
PA
6304 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6305
6306 return inferior_has_vforked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6307}
6308
4a64f543
MS
6309/* Implement the "print_it" breakpoint_ops method for vfork
6310 catchpoints. */
ce78b96d
JB
6311
6312static enum print_stop_action
348d480f 6313print_it_catch_vfork (bpstat bs)
ce78b96d 6314{
36dfb11c 6315 struct ui_out *uiout = current_uiout;
348d480f 6316 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
6317 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6318
ce78b96d 6319 annotate_catchpoint (b->number);
36dfb11c
TT
6320 if (b->disposition == disp_del)
6321 ui_out_text (uiout, "\nTemporary catchpoint ");
6322 else
6323 ui_out_text (uiout, "\nCatchpoint ");
6324 if (ui_out_is_mi_like_p (uiout))
6325 {
6326 ui_out_field_string (uiout, "reason",
6327 async_reason_lookup (EXEC_ASYNC_VFORK));
6328 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6329 }
6330 ui_out_field_int (uiout, "bkptno", b->number);
6331 ui_out_text (uiout, " (vforked process ");
6332 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6333 ui_out_text (uiout, "), ");
ce78b96d
JB
6334 return PRINT_SRC_AND_LOC;
6335}
6336
4a64f543
MS
6337/* Implement the "print_one" breakpoint_ops method for vfork
6338 catchpoints. */
ce78b96d
JB
6339
6340static void
a6d9a66e 6341print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6342{
e29a4733 6343 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6344 struct value_print_options opts;
79a45e25 6345 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6346
6347 get_user_print_options (&opts);
4a64f543
MS
6348 /* Field 4, the address, is omitted (which makes the columns not
6349 line up too nicely with the headers, but the effect is relatively
6350 readable). */
79a45b7d 6351 if (opts.addressprint)
ce78b96d
JB
6352 ui_out_field_skip (uiout, "addr");
6353 annotate_field (5);
6354 ui_out_text (uiout, "vfork");
e29a4733 6355 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6356 {
6357 ui_out_text (uiout, ", process ");
6358 ui_out_field_int (uiout, "what",
e29a4733 6359 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6360 ui_out_spaces (uiout, 1);
6361 }
6362}
6363
6364/* Implement the "print_mention" breakpoint_ops method for vfork
6365 catchpoints. */
6366
6367static void
6368print_mention_catch_vfork (struct breakpoint *b)
6369{
6370 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6371}
6372
6149aea9
PA
6373/* Implement the "print_recreate" breakpoint_ops method for vfork
6374 catchpoints. */
6375
6376static void
6377print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6378{
6379 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 6380 print_recreate_thread (b, fp);
6149aea9
PA
6381}
6382
ce78b96d
JB
6383/* The breakpoint_ops structure to be used in vfork catchpoints. */
6384
2060206e 6385static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 6386
be5c67c1
PA
6387/* An instance of this type is used to represent a syscall catchpoint.
6388 It includes a "struct breakpoint" as a kind of base class; users
6389 downcast to "struct breakpoint *" when needed. A breakpoint is
6390 really of this type iff its ops pointer points to
6391 CATCH_SYSCALL_BREAKPOINT_OPS. */
6392
6393struct syscall_catchpoint
6394{
6395 /* The base class. */
6396 struct breakpoint base;
6397
6398 /* Syscall numbers used for the 'catch syscall' feature. If no
6399 syscall has been specified for filtering, its value is NULL.
6400 Otherwise, it holds a list of all syscalls to be caught. The
6401 list elements are allocated with xmalloc. */
6402 VEC(int) *syscalls_to_be_caught;
6403};
6404
6405/* Implement the "dtor" breakpoint_ops method for syscall
6406 catchpoints. */
6407
6408static void
6409dtor_catch_syscall (struct breakpoint *b)
6410{
6411 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6412
6413 VEC_free (int, c->syscalls_to_be_caught);
348d480f 6414
2060206e 6415 base_breakpoint_ops.dtor (b);
be5c67c1
PA
6416}
6417
a96d9b2e
SDJ
6418/* Implement the "insert" breakpoint_ops method for syscall
6419 catchpoints. */
6420
77b06cd7
TJB
6421static int
6422insert_catch_syscall (struct bp_location *bl)
a96d9b2e 6423{
be5c67c1 6424 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6425 struct inferior *inf = current_inferior ();
6426
6427 ++inf->total_syscalls_count;
be5c67c1 6428 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6429 ++inf->any_syscall_count;
6430 else
6431 {
6432 int i, iter;
cc59ec59 6433
a96d9b2e 6434 for (i = 0;
be5c67c1 6435 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6436 i++)
6437 {
6438 int elem;
cc59ec59 6439
a96d9b2e
SDJ
6440 if (iter >= VEC_length (int, inf->syscalls_counts))
6441 {
6442 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6443 uintptr_t vec_addr_offset
6444 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6445 uintptr_t vec_addr;
6446 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6447 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6448 vec_addr_offset;
6449 memset ((void *) vec_addr, 0,
6450 (iter + 1 - old_size) * sizeof (int));
6451 }
6452 elem = VEC_index (int, inf->syscalls_counts, iter);
6453 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6454 }
6455 }
6456
77b06cd7
TJB
6457 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6458 inf->total_syscalls_count != 0,
6459 inf->any_syscall_count,
6460 VEC_length (int, inf->syscalls_counts),
6461 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6462}
6463
6464/* Implement the "remove" breakpoint_ops method for syscall
6465 catchpoints. */
6466
6467static int
77b06cd7 6468remove_catch_syscall (struct bp_location *bl)
a96d9b2e 6469{
be5c67c1 6470 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6471 struct inferior *inf = current_inferior ();
6472
6473 --inf->total_syscalls_count;
be5c67c1 6474 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6475 --inf->any_syscall_count;
6476 else
6477 {
6478 int i, iter;
cc59ec59 6479
a96d9b2e 6480 for (i = 0;
be5c67c1 6481 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6482 i++)
6483 {
6484 int elem;
6485 if (iter >= VEC_length (int, inf->syscalls_counts))
6486 /* Shouldn't happen. */
6487 continue;
6488 elem = VEC_index (int, inf->syscalls_counts, iter);
6489 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6490 }
6491 }
6492
6493 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6494 inf->total_syscalls_count != 0,
6495 inf->any_syscall_count,
6496 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6497 VEC_address (int,
6498 inf->syscalls_counts));
a96d9b2e
SDJ
6499}
6500
6501/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6502 catchpoints. */
6503
6504static int
f1310107
TJB
6505breakpoint_hit_catch_syscall (const struct bp_location *bl,
6506 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6507{
4a64f543
MS
6508 /* We must check if we are catching specific syscalls in this
6509 breakpoint. If we are, then we must guarantee that the called
6510 syscall is the same syscall we are catching. */
a96d9b2e 6511 int syscall_number = 0;
be5c67c1
PA
6512 const struct syscall_catchpoint *c
6513 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6514
6515 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6516 return 0;
6517
6518 /* Now, checking if the syscall is the same. */
be5c67c1 6519 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6520 {
6521 int i, iter;
cc59ec59 6522
a96d9b2e 6523 for (i = 0;
be5c67c1 6524 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6525 i++)
6526 if (syscall_number == iter)
6527 break;
6528 /* Not the same. */
6529 if (!iter)
6530 return 0;
6531 }
6532
6533 return 1;
6534}
6535
6536/* Implement the "print_it" breakpoint_ops method for syscall
6537 catchpoints. */
6538
6539static enum print_stop_action
348d480f 6540print_it_catch_syscall (bpstat bs)
a96d9b2e 6541{
36dfb11c 6542 struct ui_out *uiout = current_uiout;
348d480f 6543 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
6544 /* These are needed because we want to know in which state a
6545 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6546 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6547 must print "called syscall" or "returned from syscall". */
6548 ptid_t ptid;
6549 struct target_waitstatus last;
6550 struct syscall s;
a96d9b2e
SDJ
6551 char *syscall_id;
6552
6553 get_last_target_status (&ptid, &last);
6554
6555 get_syscall_by_number (last.value.syscall_number, &s);
6556
6557 annotate_catchpoint (b->number);
6558
36dfb11c
TT
6559 if (b->disposition == disp_del)
6560 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 6561 else
36dfb11c
TT
6562 ui_out_text (uiout, "\nCatchpoint ");
6563 if (ui_out_is_mi_like_p (uiout))
6564 {
6565 ui_out_field_string (uiout, "reason",
6566 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
6567 ? EXEC_ASYNC_SYSCALL_ENTRY
6568 : EXEC_ASYNC_SYSCALL_RETURN));
6569 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6570 }
6571 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
6572
6573 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
6574 ui_out_text (uiout, " (call to syscall ");
6575 else
6576 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 6577
36dfb11c
TT
6578 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
6579 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
6580 if (s.name != NULL)
6581 ui_out_field_string (uiout, "syscall-name", s.name);
6582
6583 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
6584
6585 return PRINT_SRC_AND_LOC;
6586}
6587
6588/* Implement the "print_one" breakpoint_ops method for syscall
6589 catchpoints. */
6590
6591static void
6592print_one_catch_syscall (struct breakpoint *b,
f1310107 6593 struct bp_location **last_loc)
a96d9b2e 6594{
be5c67c1 6595 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 6596 struct value_print_options opts;
79a45e25 6597 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
6598
6599 get_user_print_options (&opts);
4a64f543
MS
6600 /* Field 4, the address, is omitted (which makes the columns not
6601 line up too nicely with the headers, but the effect is relatively
6602 readable). */
a96d9b2e
SDJ
6603 if (opts.addressprint)
6604 ui_out_field_skip (uiout, "addr");
6605 annotate_field (5);
6606
be5c67c1
PA
6607 if (c->syscalls_to_be_caught
6608 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6609 ui_out_text (uiout, "syscalls \"");
6610 else
6611 ui_out_text (uiout, "syscall \"");
6612
be5c67c1 6613 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6614 {
6615 int i, iter;
6616 char *text = xstrprintf ("%s", "");
cc59ec59 6617
a96d9b2e 6618 for (i = 0;
be5c67c1 6619 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6620 i++)
6621 {
6622 char *x = text;
6623 struct syscall s;
6624 get_syscall_by_number (iter, &s);
6625
6626 if (s.name != NULL)
6627 text = xstrprintf ("%s%s, ", text, s.name);
6628 else
6629 text = xstrprintf ("%s%d, ", text, iter);
6630
6631 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 6632 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
6633 on every call. */
6634 xfree (x);
6635 }
6636 /* Remove the last comma. */
6637 text[strlen (text) - 2] = '\0';
6638 ui_out_field_string (uiout, "what", text);
6639 }
6640 else
6641 ui_out_field_string (uiout, "what", "<any syscall>");
6642 ui_out_text (uiout, "\" ");
6643}
6644
6645/* Implement the "print_mention" breakpoint_ops method for syscall
6646 catchpoints. */
6647
6648static void
6649print_mention_catch_syscall (struct breakpoint *b)
6650{
be5c67c1
PA
6651 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6652
6653 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6654 {
6655 int i, iter;
6656
be5c67c1 6657 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6658 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6659 else
6660 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6661
6662 for (i = 0;
be5c67c1 6663 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6664 i++)
6665 {
6666 struct syscall s;
6667 get_syscall_by_number (iter, &s);
6668
6669 if (s.name)
6670 printf_filtered (" '%s' [%d]", s.name, s.number);
6671 else
6672 printf_filtered (" %d", s.number);
6673 }
6674 printf_filtered (")");
6675 }
6676 else
6677 printf_filtered (_("Catchpoint %d (any syscall)"),
6678 b->number);
6679}
6680
6149aea9
PA
6681/* Implement the "print_recreate" breakpoint_ops method for syscall
6682 catchpoints. */
6683
6684static void
6685print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6686{
be5c67c1
PA
6687 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6688
6149aea9
PA
6689 fprintf_unfiltered (fp, "catch syscall");
6690
be5c67c1 6691 if (c->syscalls_to_be_caught)
6149aea9
PA
6692 {
6693 int i, iter;
6694
6695 for (i = 0;
be5c67c1 6696 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
6697 i++)
6698 {
6699 struct syscall s;
6700
6701 get_syscall_by_number (iter, &s);
6702 if (s.name)
6703 fprintf_unfiltered (fp, " %s", s.name);
6704 else
6705 fprintf_unfiltered (fp, " %d", s.number);
6706 }
6707 }
d9b3f62e 6708 print_recreate_thread (b, fp);
6149aea9
PA
6709}
6710
a96d9b2e
SDJ
6711/* The breakpoint_ops structure to be used in syscall catchpoints. */
6712
2060206e 6713static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
6714
6715/* Returns non-zero if 'b' is a syscall catchpoint. */
6716
6717static int
6718syscall_catchpoint_p (struct breakpoint *b)
6719{
6720 return (b->ops == &catch_syscall_breakpoint_ops);
6721}
6722
346774a9
PA
6723/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
6724 is non-zero, then make the breakpoint temporary. If COND_STRING is
6725 not NULL, then store it in the breakpoint. OPS, if not NULL, is
6726 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 6727
346774a9
PA
6728static void
6729init_catchpoint (struct breakpoint *b,
6730 struct gdbarch *gdbarch, int tempflag,
6731 char *cond_string,
c0a91b2b 6732 const struct breakpoint_ops *ops)
c906108c 6733{
c5aa993b 6734 struct symtab_and_line sal;
346774a9 6735
fe39c653 6736 init_sal (&sal);
6c95b8df 6737 sal.pspace = current_program_space;
c5aa993b 6738
28010a5d 6739 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 6740
1b36a34b 6741 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 6742 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
6743}
6744
28010a5d 6745void
3ea46bff 6746install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
6747{
6748 add_to_breakpoint_chain (b);
3a5c3e22
PA
6749 set_breakpoint_number (internal, b);
6750 if (!internal)
6751 mention (b);
c56053d2 6752 observer_notify_breakpoint_created (b);
3ea46bff
YQ
6753
6754 if (update_gll)
6755 update_global_location_list (1);
c56053d2
PA
6756}
6757
9b70b993 6758static void
a6d9a66e
UW
6759create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6760 int tempflag, char *cond_string,
c0a91b2b 6761 const struct breakpoint_ops *ops)
c906108c 6762{
e29a4733 6763 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 6764
e29a4733
PA
6765 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
6766
6767 c->forked_inferior_pid = null_ptid;
6768
3ea46bff 6769 install_breakpoint (0, &c->base, 1);
c906108c
SS
6770}
6771
fe798b75
JB
6772/* Exec catchpoints. */
6773
b4d90040
PA
6774/* An instance of this type is used to represent an exec catchpoint.
6775 It includes a "struct breakpoint" as a kind of base class; users
6776 downcast to "struct breakpoint *" when needed. A breakpoint is
6777 really of this type iff its ops pointer points to
6778 CATCH_EXEC_BREAKPOINT_OPS. */
6779
6780struct exec_catchpoint
6781{
6782 /* The base class. */
6783 struct breakpoint base;
6784
6785 /* Filename of a program whose exec triggered this catchpoint.
6786 This field is only valid immediately after this catchpoint has
6787 triggered. */
6788 char *exec_pathname;
6789};
6790
6791/* Implement the "dtor" breakpoint_ops method for exec
6792 catchpoints. */
6793
6794static void
6795dtor_catch_exec (struct breakpoint *b)
6796{
6797 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6798
6799 xfree (c->exec_pathname);
348d480f 6800
2060206e 6801 base_breakpoint_ops.dtor (b);
b4d90040
PA
6802}
6803
77b06cd7
TJB
6804static int
6805insert_catch_exec (struct bp_location *bl)
c906108c 6806{
77b06cd7 6807 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6808}
c906108c 6809
fe798b75 6810static int
77b06cd7 6811remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6812{
6813 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6814}
c906108c 6815
fe798b75 6816static int
f1310107
TJB
6817breakpoint_hit_catch_exec (const struct bp_location *bl,
6818 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6819{
b4d90040
PA
6820 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
6821
6822 return inferior_has_execd (inferior_ptid, &c->exec_pathname);
fe798b75 6823}
c906108c 6824
fe798b75 6825static enum print_stop_action
348d480f 6826print_it_catch_exec (bpstat bs)
fe798b75 6827{
36dfb11c 6828 struct ui_out *uiout = current_uiout;
348d480f 6829 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
6830 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6831
fe798b75 6832 annotate_catchpoint (b->number);
36dfb11c
TT
6833 if (b->disposition == disp_del)
6834 ui_out_text (uiout, "\nTemporary catchpoint ");
6835 else
6836 ui_out_text (uiout, "\nCatchpoint ");
6837 if (ui_out_is_mi_like_p (uiout))
6838 {
6839 ui_out_field_string (uiout, "reason",
6840 async_reason_lookup (EXEC_ASYNC_EXEC));
6841 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6842 }
6843 ui_out_field_int (uiout, "bkptno", b->number);
6844 ui_out_text (uiout, " (exec'd ");
6845 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
6846 ui_out_text (uiout, "), ");
6847
fe798b75 6848 return PRINT_SRC_AND_LOC;
c906108c
SS
6849}
6850
fe798b75 6851static void
a6d9a66e 6852print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 6853{
b4d90040 6854 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 6855 struct value_print_options opts;
79a45e25 6856 struct ui_out *uiout = current_uiout;
fe798b75
JB
6857
6858 get_user_print_options (&opts);
6859
6860 /* Field 4, the address, is omitted (which makes the columns
6861 not line up too nicely with the headers, but the effect
6862 is relatively readable). */
6863 if (opts.addressprint)
6864 ui_out_field_skip (uiout, "addr");
6865 annotate_field (5);
6866 ui_out_text (uiout, "exec");
b4d90040 6867 if (c->exec_pathname != NULL)
fe798b75
JB
6868 {
6869 ui_out_text (uiout, ", program \"");
b4d90040 6870 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
6871 ui_out_text (uiout, "\" ");
6872 }
6873}
6874
6875static void
6876print_mention_catch_exec (struct breakpoint *b)
6877{
6878 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6879}
6880
6149aea9
PA
6881/* Implement the "print_recreate" breakpoint_ops method for exec
6882 catchpoints. */
6883
6884static void
6885print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6886{
6887 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 6888 print_recreate_thread (b, fp);
6149aea9
PA
6889}
6890
2060206e 6891static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 6892
a96d9b2e
SDJ
6893static void
6894create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 6895 const struct breakpoint_ops *ops)
a96d9b2e 6896{
be5c67c1 6897 struct syscall_catchpoint *c;
a96d9b2e 6898 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 6899
be5c67c1
PA
6900 c = XNEW (struct syscall_catchpoint);
6901 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
6902 c->syscalls_to_be_caught = filter;
a96d9b2e 6903
3ea46bff 6904 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
6905}
6906
c906108c 6907static int
fba45db2 6908hw_breakpoint_used_count (void)
c906108c 6909{
c906108c 6910 int i = 0;
f1310107
TJB
6911 struct breakpoint *b;
6912 struct bp_location *bl;
c906108c
SS
6913
6914 ALL_BREAKPOINTS (b)
c5aa993b 6915 {
d6b74ac4 6916 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
6917 for (bl = b->loc; bl; bl = bl->next)
6918 {
6919 /* Special types of hardware breakpoints may use more than
6920 one register. */
348d480f 6921 i += b->ops->resources_needed (bl);
f1310107 6922 }
c5aa993b 6923 }
c906108c
SS
6924
6925 return i;
6926}
6927
a1398e0c
PA
6928/* Returns the resources B would use if it were a hardware
6929 watchpoint. */
6930
c906108c 6931static int
a1398e0c 6932hw_watchpoint_use_count (struct breakpoint *b)
c906108c 6933{
c906108c 6934 int i = 0;
e09342b5 6935 struct bp_location *bl;
c906108c 6936
a1398e0c
PA
6937 if (!breakpoint_enabled (b))
6938 return 0;
6939
6940 for (bl = b->loc; bl; bl = bl->next)
6941 {
6942 /* Special types of hardware watchpoints may use more than
6943 one register. */
6944 i += b->ops->resources_needed (bl);
6945 }
6946
6947 return i;
6948}
6949
6950/* Returns the sum the used resources of all hardware watchpoints of
6951 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
6952 the sum of the used resources of all hardware watchpoints of other
6953 types _not_ TYPE. */
6954
6955static int
6956hw_watchpoint_used_count_others (struct breakpoint *except,
6957 enum bptype type, int *other_type_used)
6958{
6959 int i = 0;
6960 struct breakpoint *b;
6961
c906108c
SS
6962 *other_type_used = 0;
6963 ALL_BREAKPOINTS (b)
e09342b5 6964 {
a1398e0c
PA
6965 if (b == except)
6966 continue;
e09342b5
TJB
6967 if (!breakpoint_enabled (b))
6968 continue;
6969
a1398e0c
PA
6970 if (b->type == type)
6971 i += hw_watchpoint_use_count (b);
6972 else if (is_hardware_watchpoint (b))
6973 *other_type_used = 1;
e09342b5
TJB
6974 }
6975
c906108c
SS
6976 return i;
6977}
6978
c906108c 6979void
fba45db2 6980disable_watchpoints_before_interactive_call_start (void)
c906108c 6981{
c5aa993b 6982 struct breakpoint *b;
c906108c
SS
6983
6984 ALL_BREAKPOINTS (b)
c5aa993b 6985 {
cc60f2e3 6986 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6987 {
b5de0fa7 6988 b->enable_state = bp_call_disabled;
b60e7edf 6989 update_global_location_list (0);
c5aa993b
JM
6990 }
6991 }
c906108c
SS
6992}
6993
6994void
fba45db2 6995enable_watchpoints_after_interactive_call_stop (void)
c906108c 6996{
c5aa993b 6997 struct breakpoint *b;
c906108c
SS
6998
6999 ALL_BREAKPOINTS (b)
c5aa993b 7000 {
cc60f2e3 7001 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 7002 {
b5de0fa7 7003 b->enable_state = bp_enabled;
b60e7edf 7004 update_global_location_list (1);
c5aa993b
JM
7005 }
7006 }
c906108c
SS
7007}
7008
8bea4e01
UW
7009void
7010disable_breakpoints_before_startup (void)
7011{
7012 struct breakpoint *b;
7013 int found = 0;
7014
7015 ALL_BREAKPOINTS (b)
7016 {
6c95b8df
PA
7017 if (b->pspace != current_program_space)
7018 continue;
7019
8bea4e01
UW
7020 if ((b->type == bp_breakpoint
7021 || b->type == bp_hardware_breakpoint)
7022 && breakpoint_enabled (b))
7023 {
7024 b->enable_state = bp_startup_disabled;
7025 found = 1;
7026 }
7027 }
7028
7029 if (found)
7030 update_global_location_list (0);
7031
6c95b8df 7032 current_program_space->executing_startup = 1;
8bea4e01
UW
7033}
7034
7035void
7036enable_breakpoints_after_startup (void)
7037{
7038 struct breakpoint *b;
7039 int found = 0;
7040
6c95b8df 7041 current_program_space->executing_startup = 0;
8bea4e01
UW
7042
7043 ALL_BREAKPOINTS (b)
7044 {
6c95b8df
PA
7045 if (b->pspace != current_program_space)
7046 continue;
7047
8bea4e01
UW
7048 if ((b->type == bp_breakpoint
7049 || b->type == bp_hardware_breakpoint)
7050 && b->enable_state == bp_startup_disabled)
7051 {
7052 b->enable_state = bp_enabled;
7053 found = 1;
7054 }
7055 }
7056
7057 if (found)
7058 breakpoint_re_set ();
7059}
7060
c906108c
SS
7061
7062/* Set a breakpoint that will evaporate an end of command
7063 at address specified by SAL.
7064 Restrict it to frame FRAME if FRAME is nonzero. */
7065
7066struct breakpoint *
a6d9a66e
UW
7067set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7068 struct frame_id frame_id, enum bptype type)
c906108c 7069{
52f0bd74 7070 struct breakpoint *b;
edb3359d
DJ
7071
7072 /* If FRAME_ID is valid, it should be a real frame, not an inlined
7073 one. */
7074 gdb_assert (!frame_id_inlined_p (frame_id));
7075
06edf0c0 7076 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
7077 b->enable_state = bp_enabled;
7078 b->disposition = disp_donttouch;
818dd999 7079 b->frame_id = frame_id;
c906108c 7080
4a64f543
MS
7081 /* If we're debugging a multi-threaded program, then we want
7082 momentary breakpoints to be active in only a single thread of
7083 control. */
39f77062
KB
7084 if (in_thread_list (inferior_ptid))
7085 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 7086
b60e7edf 7087 update_global_location_list_nothrow (1);
74960c60 7088
c906108c
SS
7089 return b;
7090}
611c83ae 7091
06edf0c0
PA
7092/* Make a momentary breakpoint based on the master breakpoint ORIG.
7093 The new breakpoint will have type TYPE, and use OPS as it
7094 breakpoint_ops. */
e58b0e63 7095
06edf0c0
PA
7096static struct breakpoint *
7097momentary_breakpoint_from_master (struct breakpoint *orig,
7098 enum bptype type,
c0a91b2b 7099 const struct breakpoint_ops *ops)
e58b0e63
PA
7100{
7101 struct breakpoint *copy;
7102
06edf0c0 7103 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 7104 copy->loc = allocate_bp_location (copy);
0e30163f 7105 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 7106
a6d9a66e 7107 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7108 copy->loc->requested_address = orig->loc->requested_address;
7109 copy->loc->address = orig->loc->address;
7110 copy->loc->section = orig->loc->section;
6c95b8df 7111 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
7112
7113 if (orig->source_file == NULL)
7114 copy->source_file = NULL;
7115 else
7116 copy->source_file = xstrdup (orig->source_file);
7117
7118 copy->line_number = orig->line_number;
7119 copy->frame_id = orig->frame_id;
7120 copy->thread = orig->thread;
6c95b8df 7121 copy->pspace = orig->pspace;
e58b0e63
PA
7122
7123 copy->enable_state = bp_enabled;
7124 copy->disposition = disp_donttouch;
7125 copy->number = internal_breakpoint_number--;
7126
7127 update_global_location_list_nothrow (0);
7128 return copy;
7129}
7130
06edf0c0
PA
7131/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7132 ORIG is NULL. */
7133
7134struct breakpoint *
7135clone_momentary_breakpoint (struct breakpoint *orig)
7136{
7137 /* If there's nothing to clone, then return nothing. */
7138 if (orig == NULL)
7139 return NULL;
7140
7141 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
7142}
7143
611c83ae 7144struct breakpoint *
a6d9a66e
UW
7145set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7146 enum bptype type)
611c83ae
PA
7147{
7148 struct symtab_and_line sal;
7149
7150 sal = find_pc_line (pc, 0);
7151 sal.pc = pc;
7152 sal.section = find_pc_overlay (pc);
7153 sal.explicit_pc = 1;
7154
a6d9a66e 7155 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7156}
c906108c 7157\f
c5aa993b 7158
c906108c
SS
7159/* Tell the user we have just set a breakpoint B. */
7160
7161static void
fba45db2 7162mention (struct breakpoint *b)
c906108c 7163{
348d480f 7164 b->ops->print_mention (b);
79a45e25 7165 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 7166 return;
c906108c
SS
7167 printf_filtered ("\n");
7168}
c906108c 7169\f
c5aa993b 7170
0d381245 7171static struct bp_location *
39d61571 7172add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7173 const struct symtab_and_line *sal)
7174{
7175 struct bp_location *loc, **tmp;
3742cc8b
YQ
7176 CORE_ADDR adjusted_address;
7177 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
7178
7179 if (loc_gdbarch == NULL)
7180 loc_gdbarch = b->gdbarch;
7181
7182 /* Adjust the breakpoint's address prior to allocating a location.
7183 Once we call allocate_bp_location(), that mostly uninitialized
7184 location will be placed on the location chain. Adjustment of the
7185 breakpoint may cause target_read_memory() to be called and we do
7186 not want its scan of the location chain to find a breakpoint and
7187 location that's only been partially initialized. */
7188 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
7189 sal->pc, b->type);
0d381245 7190
39d61571 7191 loc = allocate_bp_location (b);
0d381245
VP
7192 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7193 ;
7194 *tmp = loc;
3742cc8b 7195
0d381245 7196 loc->requested_address = sal->pc;
3742cc8b 7197 loc->address = adjusted_address;
6c95b8df
PA
7198 loc->pspace = sal->pspace;
7199 gdb_assert (loc->pspace != NULL);
0d381245 7200 loc->section = sal->section;
3742cc8b 7201 loc->gdbarch = loc_gdbarch;
0e30163f
JK
7202 set_breakpoint_location_function (loc,
7203 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7204 return loc;
7205}
514f746b
AR
7206\f
7207
7208/* Return 1 if LOC is pointing to a permanent breakpoint,
7209 return 0 otherwise. */
7210
7211static int
7212bp_loc_is_permanent (struct bp_location *loc)
7213{
7214 int len;
7215 CORE_ADDR addr;
7216 const gdb_byte *brk;
7217 gdb_byte *target_mem;
939c61fa
JK
7218 struct cleanup *cleanup;
7219 int retval = 0;
514f746b
AR
7220
7221 gdb_assert (loc != NULL);
7222
7223 addr = loc->address;
a6d9a66e 7224 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7225
939c61fa
JK
7226 /* Software breakpoints unsupported? */
7227 if (brk == NULL)
7228 return 0;
7229
514f746b
AR
7230 target_mem = alloca (len);
7231
939c61fa
JK
7232 /* Enable the automatic memory restoration from breakpoints while
7233 we read the memory. Otherwise we could say about our temporary
7234 breakpoints they are permanent. */
6c95b8df
PA
7235 cleanup = save_current_space_and_thread ();
7236
7237 switch_to_program_space_and_thread (loc->pspace);
7238 make_show_memory_breakpoints_cleanup (0);
939c61fa 7239
514f746b
AR
7240 if (target_read_memory (loc->address, target_mem, len) == 0
7241 && memcmp (target_mem, brk, len) == 0)
939c61fa 7242 retval = 1;
514f746b 7243
939c61fa
JK
7244 do_cleanups (cleanup);
7245
7246 return retval;
514f746b
AR
7247}
7248
7249
c3f6f71d 7250
018d34a4
VP
7251/* Create a breakpoint with SAL as location. Use ADDR_STRING
7252 as textual description of the location, and COND_STRING
db107f19 7253 as condition expression. */
018d34a4
VP
7254
7255static void
d9b3f62e
PA
7256init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
7257 struct symtabs_and_lines sals, char *addr_string,
7258 char *cond_string,
7259 enum bptype type, enum bpdisp disposition,
7260 int thread, int task, int ignore_count,
c0a91b2b 7261 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e 7262 int enabled, int internal, int display_canonical)
018d34a4 7263{
0d381245 7264 int i;
018d34a4
VP
7265
7266 if (type == bp_hardware_breakpoint)
7267 {
7268 int i = hw_breakpoint_used_count ();
7269 int target_resources_ok =
d92524f1 7270 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7271 i + 1, 0);
7272 if (target_resources_ok == 0)
7273 error (_("No hardware breakpoint support in the target."));
7274 else if (target_resources_ok < 0)
7275 error (_("Hardware breakpoints used exceeds limit."));
7276 }
7277
6c95b8df
PA
7278 gdb_assert (sals.nelts > 0);
7279
0d381245
VP
7280 for (i = 0; i < sals.nelts; ++i)
7281 {
7282 struct symtab_and_line sal = sals.sals[i];
7283 struct bp_location *loc;
7284
7285 if (from_tty)
5af949e3
UW
7286 {
7287 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7288 if (!loc_gdbarch)
7289 loc_gdbarch = gdbarch;
7290
7291 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7292 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7293 }
0d381245
VP
7294
7295 if (i == 0)
7296 {
d9b3f62e 7297 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 7298 b->thread = thread;
4a306c9a 7299 b->task = task;
018d34a4 7300
0d381245
VP
7301 b->cond_string = cond_string;
7302 b->ignore_count = ignore_count;
41447f92 7303 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7304 b->disposition = disposition;
6c95b8df
PA
7305 b->pspace = sals.sals[0].pspace;
7306
0fb4aa4b
PA
7307 if (type == bp_static_tracepoint)
7308 {
d9b3f62e 7309 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
7310 struct static_tracepoint_marker marker;
7311
7312 if (is_marker_spec (addr_string))
7313 {
7314 /* We already know the marker exists, otherwise, we
7315 wouldn't see a sal for it. */
7316 char *p = &addr_string[3];
7317 char *endp;
7318 char *marker_str;
7319 int i;
7320
e9cafbcc 7321 p = skip_spaces (p);
0fb4aa4b 7322
e9cafbcc 7323 endp = skip_to_space (p);
0fb4aa4b
PA
7324
7325 marker_str = savestring (p, endp - p);
d9b3f62e 7326 t->static_trace_marker_id = marker_str;
0fb4aa4b 7327
3e43a32a
MS
7328 printf_filtered (_("Probed static tracepoint "
7329 "marker \"%s\"\n"),
d9b3f62e 7330 t->static_trace_marker_id);
0fb4aa4b
PA
7331 }
7332 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7333 {
d9b3f62e 7334 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
7335 release_static_tracepoint_marker (&marker);
7336
3e43a32a
MS
7337 printf_filtered (_("Probed static tracepoint "
7338 "marker \"%s\"\n"),
d9b3f62e 7339 t->static_trace_marker_id);
0fb4aa4b
PA
7340 }
7341 else
3e43a32a
MS
7342 warning (_("Couldn't determine the static "
7343 "tracepoint marker to probe"));
0fb4aa4b
PA
7344 }
7345
6c95b8df 7346 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7347 && (b->type == bp_breakpoint
7348 || b->type == bp_hardware_breakpoint))
7349 b->enable_state = bp_startup_disabled;
7350
0d381245
VP
7351 loc = b->loc;
7352 }
7353 else
018d34a4 7354 {
39d61571 7355 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7356 }
7357
514f746b
AR
7358 if (bp_loc_is_permanent (loc))
7359 make_breakpoint_permanent (b);
7360
0d381245
VP
7361 if (b->cond_string)
7362 {
7363 char *arg = b->cond_string;
d32a6982 7364 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7365 if (*arg)
db107f19 7366 error (_("Garbage %s follows condition"), arg);
018d34a4 7367 }
0d381245 7368 }
018d34a4 7369
56435ebe 7370 b->display_canonical = display_canonical;
018d34a4
VP
7371 if (addr_string)
7372 b->addr_string = addr_string;
7373 else
7374 /* addr_string has to be used or breakpoint_re_set will delete
7375 me. */
5af949e3
UW
7376 b->addr_string
7377 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
d9b3f62e 7378}
018d34a4 7379
d9b3f62e
PA
7380static void
7381create_breakpoint_sal (struct gdbarch *gdbarch,
7382 struct symtabs_and_lines sals, char *addr_string,
7383 char *cond_string,
7384 enum bptype type, enum bpdisp disposition,
7385 int thread, int task, int ignore_count,
c0a91b2b 7386 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e
PA
7387 int enabled, int internal, int display_canonical)
7388{
7389 struct breakpoint *b;
7390 struct cleanup *old_chain;
7391
7392 if (is_tracepoint_type (type))
7393 {
7394 struct tracepoint *t;
7395
7396 t = XCNEW (struct tracepoint);
7397 b = &t->base;
7398 }
7399 else
7400 b = XNEW (struct breakpoint);
7401
7402 old_chain = make_cleanup (xfree, b);
7403
7404 init_breakpoint_sal (b, gdbarch,
7405 sals, addr_string,
7406 cond_string,
7407 type, disposition,
7408 thread, task, ignore_count,
7409 ops, from_tty,
7410 enabled, internal, display_canonical);
7411 discard_cleanups (old_chain);
7412
3ea46bff 7413 install_breakpoint (internal, b, 0);
018d34a4
VP
7414}
7415
ed0616c6
VP
7416/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7417 elements to fill the void space. */
2c0b251b
PA
7418static void
7419remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7420{
7421 int i = index_to_remove+1;
7422 int last_index = sal->nelts-1;
7423
7424 for (;i <= last_index; ++i)
7425 sal->sals[i-1] = sal->sals[i];
7426
7427 --(sal->nelts);
7428}
7429
6c95b8df
PA
7430/* If appropriate, obtains all sals that correspond to the same file
7431 and line as SAL, in all program spaces. Users debugging with IDEs,
7432 will want to set a breakpoint at foo.c:line, and not really care
7433 about program spaces. This is done only if SAL does not have
7434 explicit PC and has line and file information. If we got just a
7435 single expanded sal, return the original.
ed0616c6 7436
6c95b8df
PA
7437 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7438 which the name of enclosing function is different from SAL. This
7439 makes sure that if we have breakpoint originally set in template
7440 instantiation, say foo<int>(), we won't expand SAL to locations at
7441 the same line in all existing instantiations of 'foo'. */
ed0616c6 7442
2c0b251b 7443static struct symtabs_and_lines
ed0616c6
VP
7444expand_line_sal_maybe (struct symtab_and_line sal)
7445{
7446 struct symtabs_and_lines expanded;
7447 CORE_ADDR original_pc = sal.pc;
7448 char *original_function = NULL;
7449 int found;
7450 int i;
6c95b8df 7451 struct cleanup *old_chain;
ed0616c6
VP
7452
7453 /* If we have explicit pc, don't expand.
7454 If we have no line number, we can't expand. */
7455 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7456 {
7457 expanded.nelts = 1;
7458 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7459 expanded.sals[0] = sal;
7460 return expanded;
7461 }
7462
7463 sal.pc = 0;
6c95b8df
PA
7464
7465 old_chain = save_current_space_and_thread ();
7466
7467 switch_to_program_space_and_thread (sal.pspace);
7468
ed0616c6 7469 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7470
7471 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7472 expanded = expand_line_sal (sal);
6c95b8df 7473
ed0616c6
VP
7474 if (expanded.nelts == 1)
7475 {
3dba1c98
JB
7476 /* We had one sal, we got one sal. Return that sal, adjusting it
7477 past the function prologue if necessary. */
ed0616c6
VP
7478 xfree (expanded.sals);
7479 expanded.nelts = 1;
7480 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7481 sal.pc = original_pc;
7482 expanded.sals[0] = sal;
3dba1c98 7483 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7484 do_cleanups (old_chain);
ed0616c6
VP
7485 return expanded;
7486 }
7487
7488 if (!sal.explicit_line)
7489 {
7490 CORE_ADDR func_addr, func_end;
7491 for (i = 0; i < expanded.nelts; ++i)
7492 {
7493 CORE_ADDR pc = expanded.sals[i].pc;
7494 char *this_function;
6c95b8df
PA
7495
7496 /* We need to switch threads as well since we're about to
7497 read memory. */
7498 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7499
ed0616c6
VP
7500 if (find_pc_partial_function (pc, &this_function,
7501 &func_addr, &func_end))
7502 {
059fb39f
PM
7503 if (this_function
7504 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7505 {
7506 remove_sal (&expanded, i);
7507 --i;
7508 }
ed0616c6
VP
7509 }
7510 }
7511 }
059acae7
UW
7512
7513 /* Skip the function prologue if necessary. */
7514 for (i = 0; i < expanded.nelts; ++i)
7515 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7516
6c95b8df
PA
7517 do_cleanups (old_chain);
7518
ed0616c6
VP
7519 if (expanded.nelts <= 1)
7520 {
e5dd4106 7521 /* This is an ugly workaround. If we get zero expanded sals
4a64f543
MS
7522 then something is really wrong. Fix that by returning the
7523 original sal. */
7524
ed0616c6
VP
7525 xfree (expanded.sals);
7526 expanded.nelts = 1;
7527 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7528 sal.pc = original_pc;
7529 expanded.sals[0] = sal;
7530 return expanded;
7531 }
7532
7533 if (original_pc)
7534 {
7535 found = 0;
7536 for (i = 0; i < expanded.nelts; ++i)
7537 if (expanded.sals[i].pc == original_pc)
7538 {
7539 found = 1;
7540 break;
7541 }
7542 gdb_assert (found);
7543 }
7544
7545 return expanded;
7546}
7547
018d34a4
VP
7548/* Add SALS.nelts breakpoints to the breakpoint table. For each
7549 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7550 value. COND_STRING, if not NULL, specified the condition to be
7551 used for all breakpoints. Essentially the only case where
7552 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7553 function. In that case, it's still not possible to specify
7554 separate conditions for different overloaded functions, so
7555 we take just a single condition string.
7556
c3f6f71d 7557 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7558 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7559 array contents). If the function fails (error() is called), the
7560 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7561 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7562
7563static void
8cdf0e15 7564create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2
TT
7565 struct symtabs_and_lines sals,
7566 struct linespec_result *canonical,
8cdf0e15
VP
7567 char *cond_string,
7568 enum bptype type, enum bpdisp disposition,
7569 int thread, int task, int ignore_count,
c0a91b2b 7570 const struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7571 int enabled, int internal)
c906108c 7572{
018d34a4 7573 int i;
cc59ec59 7574
018d34a4 7575 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7576 {
ed0616c6
VP
7577 struct symtabs_and_lines expanded =
7578 expand_line_sal_maybe (sals.sals[i]);
0d381245 7579
7efd8fc2 7580 create_breakpoint_sal (gdbarch, expanded, canonical->canonical[i],
8cdf0e15 7581 cond_string, type, disposition,
84f4c1fe 7582 thread, task, ignore_count, ops,
56435ebe
TT
7583 from_tty, enabled, internal,
7584 canonical->special_display);
c3f6f71d 7585 }
c3f6f71d 7586}
c906108c 7587
9998af43 7588/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7589 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7590 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7591 address strings. ADDRESS points to the end of the SAL.
7592
7593 The array and the line spec strings are allocated on the heap, it is
7594 the caller's responsibility to free them. */
c906108c 7595
b9362cc7 7596static void
c3f6f71d
JM
7597parse_breakpoint_sals (char **address,
7598 struct symtabs_and_lines *sals,
58438ac1 7599 struct linespec_result *canonical)
c3f6f71d
JM
7600{
7601 char *addr_start = *address;
cc59ec59 7602
c3f6f71d 7603 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7604 breakpoint. */
c3f6f71d
JM
7605 if ((*address) == NULL
7606 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 7607 {
1bfeeb0f
JL
7608 /* The last displayed codepoint, if it's valid, is our default breakpoint
7609 address. */
7610 if (last_displayed_sal_is_valid ())
c906108c 7611 {
c3f6f71d 7612 struct symtab_and_line sal;
cc59ec59 7613
4a64f543 7614 init_sal (&sal); /* Initialize to zeroes. */
c3f6f71d 7615 sals->sals = (struct symtab_and_line *)
c906108c 7616 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
7617
7618 /* Set sal's pspace, pc, symtab, and line to the values
7619 corresponding to the last call to print_frame_info. */
7620 get_last_displayed_sal (&sal);
7621 sal.section = find_pc_overlay (sal.pc);
00903456 7622
4a64f543 7623 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
7624 where PC is the last displayed codepoint's address. So
7625 make sure to set sal.explicit_pc to prevent GDB from
7626 trying to expand the list of sals to include all other
7627 instances with the same symtab and line. */
00903456
JK
7628 sal.explicit_pc = 1;
7629
c3f6f71d
JM
7630 sals->sals[0] = sal;
7631 sals->nelts = 1;
c906108c
SS
7632 }
7633 else
8a3fe4f8 7634 error (_("No default breakpoint address now."));
c906108c
SS
7635 }
7636 else
7637 {
c906108c 7638 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7639 current_source_symtab (which is decode_line_1's default).
7640 This should produce the results we want almost all of the
1bfeeb0f
JL
7641 time while leaving the last displayed codepoint pointers
7642 alone.
4a64f543 7643
1aeae86e
AF
7644 ObjC: However, don't match an Objective-C method name which
7645 may have a '+' or '-' succeeded by a '[' */
0378c332 7646
c214a6fd 7647 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 7648
1bfeeb0f 7649 if (last_displayed_sal_is_valid ()
0378c332 7650 && (!cursal.symtab
1aeae86e
AF
7651 || ((strchr ("+-", (*address)[0]) != NULL)
7652 && ((*address)[1] != '['))))
1bfeeb0f
JL
7653 *sals = decode_line_1 (address, 1,
7654 get_last_displayed_symtab (),
7655 get_last_displayed_line (),
7656 canonical);
c906108c 7657 else
0101ce28 7658 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
58438ac1 7659 canonical);
c906108c 7660 }
4a64f543 7661 /* For any SAL that didn't have a canonical string, fill one in. */
7efd8fc2 7662 if (sals->nelts > 0 && canonical->canonical == NULL)
38a714bb 7663 canonical->canonical = xcalloc (sals->nelts, sizeof (char *));
c3f6f71d 7664 if (addr_start != (*address))
c906108c 7665 {
c3f6f71d 7666 int i;
cc59ec59 7667
c3f6f71d 7668 for (i = 0; i < sals->nelts; i++)
c906108c 7669 {
4a64f543 7670 /* Add the string if not present. */
7efd8fc2
TT
7671 if (canonical->canonical[i] == NULL)
7672 canonical->canonical[i] = savestring (addr_start,
7673 (*address) - addr_start);
c906108c
SS
7674 }
7675 }
c3f6f71d 7676}
c906108c 7677
c906108c 7678
c3f6f71d 7679/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7680 inserted as a breakpoint. If it can't throw an error. */
c906108c 7681
b9362cc7 7682static void
23e7acfb 7683breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7684{
7685 int i;
cc59ec59 7686
c3f6f71d 7687 for (i = 0; i < sals->nelts; i++)
ee53e872 7688 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7689}
7690
7a697b8d
SS
7691/* Fast tracepoints may have restrictions on valid locations. For
7692 instance, a fast tracepoint using a jump instead of a trap will
7693 likely have to overwrite more bytes than a trap would, and so can
7694 only be placed where the instruction is longer than the jump, or a
7695 multi-instruction sequence does not have a jump into the middle of
7696 it, etc. */
7697
7698static void
7699check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7700 struct symtabs_and_lines *sals)
7701{
7702 int i, rslt;
7703 struct symtab_and_line *sal;
7704 char *msg;
7705 struct cleanup *old_chain;
7706
7707 for (i = 0; i < sals->nelts; i++)
7708 {
7709 sal = &sals->sals[i];
7710
7711 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7712 NULL, &msg);
7713 old_chain = make_cleanup (xfree, msg);
7714
7715 if (!rslt)
7716 error (_("May not have a fast tracepoint at 0x%s%s"),
7717 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7718
7719 do_cleanups (old_chain);
7720 }
7721}
7722
018d34a4
VP
7723/* Given TOK, a string specification of condition and thread, as
7724 accepted by the 'break' command, extract the condition
7725 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7726 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7727 If no condition is found, *COND_STRING is set to NULL.
7728 If no thread is found, *THREAD is set to -1. */
7729static void
7730find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7731 char **cond_string, int *thread, int *task)
018d34a4
VP
7732{
7733 *cond_string = NULL;
7734 *thread = -1;
7735 while (tok && *tok)
7736 {
7737 char *end_tok;
7738 int toklen;
7739 char *cond_start = NULL;
7740 char *cond_end = NULL;
cc59ec59 7741
e9cafbcc 7742 tok = skip_spaces (tok);
018d34a4 7743
e9cafbcc 7744 end_tok = skip_to_space (tok);
018d34a4
VP
7745
7746 toklen = end_tok - tok;
7747
7748 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7749 {
f7545552
TT
7750 struct expression *expr;
7751
018d34a4 7752 tok = cond_start = end_tok + 1;
f7545552
TT
7753 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7754 xfree (expr);
018d34a4
VP
7755 cond_end = tok;
7756 *cond_string = savestring (cond_start,
7757 cond_end - cond_start);
7758 }
7759 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7760 {
7761 char *tmptok;
7762
7763 tok = end_tok + 1;
7764 tmptok = tok;
7765 *thread = strtol (tok, &tok, 0);
7766 if (tok == tmptok)
7767 error (_("Junk after thread keyword."));
7768 if (!valid_thread_id (*thread))
7769 error (_("Unknown thread %d."), *thread);
7770 }
4a306c9a
JB
7771 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7772 {
7773 char *tmptok;
7774
7775 tok = end_tok + 1;
7776 tmptok = tok;
7777 *task = strtol (tok, &tok, 0);
7778 if (tok == tmptok)
7779 error (_("Junk after task keyword."));
7780 if (!valid_task_id (*task))
b6199126 7781 error (_("Unknown task %d."), *task);
4a306c9a 7782 }
018d34a4
VP
7783 else
7784 error (_("Junk at end of arguments."));
7785 }
7786}
7787
0fb4aa4b
PA
7788/* Decode a static tracepoint marker spec. */
7789
7790static struct symtabs_and_lines
7791decode_static_tracepoint_spec (char **arg_p)
7792{
7793 VEC(static_tracepoint_marker_p) *markers = NULL;
7794 struct symtabs_and_lines sals;
7795 struct symtab_and_line sal;
7796 struct symbol *sym;
7797 struct cleanup *old_chain;
7798 char *p = &(*arg_p)[3];
7799 char *endp;
7800 char *marker_str;
7801 int i;
7802
e9cafbcc 7803 p = skip_spaces (p);
0fb4aa4b 7804
e9cafbcc 7805 endp = skip_to_space (p);
0fb4aa4b
PA
7806
7807 marker_str = savestring (p, endp - p);
7808 old_chain = make_cleanup (xfree, marker_str);
7809
7810 markers = target_static_tracepoint_markers_by_strid (marker_str);
7811 if (VEC_empty(static_tracepoint_marker_p, markers))
7812 error (_("No known static tracepoint marker named %s"), marker_str);
7813
7814 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7815 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7816
7817 for (i = 0; i < sals.nelts; i++)
7818 {
7819 struct static_tracepoint_marker *marker;
7820
7821 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7822
7823 init_sal (&sals.sals[i]);
7824
7825 sals.sals[i] = find_pc_line (marker->address, 0);
7826 sals.sals[i].pc = marker->address;
7827
7828 release_static_tracepoint_marker (marker);
7829 }
7830
7831 do_cleanups (old_chain);
7832
7833 *arg_p = endp;
7834 return sals;
7835}
7836
fd9b8c24
PA
7837/* Set a breakpoint. This function is shared between CLI and MI
7838 functions for setting a breakpoint. This function has two major
7839 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7840 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7841 breakpoint location, address and thread. Otherwise, ARG is just
7842 the location of breakpoint, with condition and thread specified by
7843 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7844 the breakpoint number will be allocated from the internal
7845 breakpoint count. Returns true if any breakpoint was created;
7846 false otherwise. */
0101ce28 7847
8cdf0e15
VP
7848int
7849create_breakpoint (struct gdbarch *gdbarch,
7850 char *arg, char *cond_string, int thread,
7851 int parse_condition_and_thread,
0fb4aa4b 7852 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7853 int ignore_count,
7854 enum auto_boolean pending_break_support,
c0a91b2b 7855 const struct breakpoint_ops *ops,
84f4c1fe 7856 int from_tty, int enabled, int internal)
c3f6f71d 7857{
b78a6381 7858 volatile struct gdb_exception e;
c3f6f71d 7859 struct symtabs_and_lines sals;
0101ce28 7860 struct symtab_and_line pending_sal;
0101ce28 7861 char *copy_arg;
c3f6f71d 7862 char *addr_start = arg;
7efd8fc2 7863 struct linespec_result canonical;
c3f6f71d 7864 struct cleanup *old_chain;
80c99de1 7865 struct cleanup *bkpt_chain = NULL;
05ff989b 7866 int i;
0101ce28 7867 int pending = 0;
4a306c9a 7868 int task = 0;
86b17b60 7869 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7870
348d480f
PA
7871 gdb_assert (ops != NULL);
7872
c3f6f71d
JM
7873 sals.sals = NULL;
7874 sals.nelts = 0;
7efd8fc2 7875 init_linespec_result (&canonical);
c3f6f71d 7876
0fb4aa4b
PA
7877 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7878 {
7879 int i;
7880
7881 sals = decode_static_tracepoint_spec (&arg);
7882
7883 copy_arg = savestring (addr_start, arg - addr_start);
38a714bb 7884 canonical.canonical = xcalloc (sals.nelts, sizeof (char *));
0fb4aa4b 7885 for (i = 0; i < sals.nelts; i++)
7efd8fc2 7886 canonical.canonical[i] = xstrdup (copy_arg);
0fb4aa4b
PA
7887 goto done;
7888 }
7889
b78a6381
TT
7890 TRY_CATCH (e, RETURN_MASK_ALL)
7891 {
58438ac1 7892 parse_breakpoint_sals (&arg, &sals, &canonical);
b78a6381 7893 }
0101ce28
JJ
7894
7895 /* If caller is interested in rc value from parse, set value. */
05ff989b 7896 switch (e.reason)
0101ce28 7897 {
05ff989b 7898 case RETURN_QUIT:
98deb0da 7899 throw_exception (e);
05ff989b
AC
7900 case RETURN_ERROR:
7901 switch (e.error)
0101ce28 7902 {
05ff989b 7903 case NOT_FOUND_ERROR:
0101ce28 7904
05ff989b
AC
7905 /* If pending breakpoint support is turned off, throw
7906 error. */
fa8d40ab
JJ
7907
7908 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7909 throw_exception (e);
7910
7911 exception_print (gdb_stderr, e);
fa8d40ab 7912
05ff989b
AC
7913 /* If pending breakpoint support is auto query and the user
7914 selects no, then simply return the error code. */
059fb39f 7915 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
7916 && !nquery (_("Make %s pending on future shared library load? "),
7917 bptype_string (type_wanted)))
fd9b8c24 7918 return 0;
fa8d40ab 7919
05ff989b
AC
7920 /* At this point, either the user was queried about setting
7921 a pending breakpoint and selected yes, or pending
7922 breakpoint behavior is on and thus a pending breakpoint
7923 is defaulted on behalf of the user. */
0101ce28 7924 copy_arg = xstrdup (addr_start);
7efd8fc2 7925 canonical.canonical = &copy_arg;
0101ce28
JJ
7926 sals.nelts = 1;
7927 sals.sals = &pending_sal;
7928 pending_sal.pc = 0;
7929 pending = 1;
05ff989b
AC
7930 break;
7931 default:
98deb0da 7932 throw_exception (e);
0101ce28 7933 }
2abae994 7934 break;
05ff989b
AC
7935 default:
7936 if (!sals.nelts)
fd9b8c24 7937 return 0;
0101ce28 7938 }
c3f6f71d 7939
0fb4aa4b
PA
7940 done:
7941
4a64f543 7942 /* Create a chain of things that always need to be cleaned up. */
c3f6f71d
JM
7943 old_chain = make_cleanup (null_cleanup, 0);
7944
0101ce28
JJ
7945 if (!pending)
7946 {
7947 /* Make sure that all storage allocated to SALS gets freed. */
7948 make_cleanup (xfree, sals.sals);
7949
7efd8fc2
TT
7950 /* Cleanup the canonical array but not its contents. */
7951 make_cleanup (xfree, canonical.canonical);
0101ce28 7952 }
c3f6f71d 7953
c3f6f71d
JM
7954 /* ----------------------------- SNIP -----------------------------
7955 Anything added to the cleanup chain beyond this point is assumed
7956 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7957 then the memory is not reclaimed. */
7958 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 7959
7efd8fc2 7960 /* Mark the contents of the canonical for cleanup. These go on
80c99de1 7961 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
7962 for (i = 0; i < sals.nelts; i++)
7963 {
7efd8fc2
TT
7964 if (canonical.canonical[i] != NULL)
7965 make_cleanup (xfree, canonical.canonical[i]);
c3f6f71d
JM
7966 }
7967
7968 /* Resolve all line numbers to PC's and verify that the addresses
7969 are ok for the target. */
0101ce28 7970 if (!pending)
23e7acfb 7971 breakpoint_sals_to_pc (&sals);
c3f6f71d 7972
7a697b8d 7973 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 7974 if (!pending && type_wanted == bp_fast_tracepoint)
7a697b8d
SS
7975 check_fast_tracepoint_sals (gdbarch, &sals);
7976
c3f6f71d
JM
7977 /* Verify that condition can be parsed, before setting any
7978 breakpoints. Allocate a separate condition expression for each
4a64f543 7979 breakpoint. */
0101ce28 7980 if (!pending)
c3f6f71d 7981 {
2f069f6f 7982 if (parse_condition_and_thread)
72b2ff0e
VP
7983 {
7984 /* Here we only parse 'arg' to separate condition
7985 from thread number, so parsing in context of first
7986 sal is OK. When setting the breakpoint we'll
7987 re-parse it in context of each sal. */
7988 cond_string = NULL;
7989 thread = -1;
4a306c9a
JB
7990 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
7991 &thread, &task);
72b2ff0e
VP
7992 if (cond_string)
7993 make_cleanup (xfree, cond_string);
7994 }
2f069f6f 7995 else
72b2ff0e
VP
7996 {
7997 /* Create a private copy of condition string. */
7998 if (cond_string)
7999 {
8000 cond_string = xstrdup (cond_string);
8001 make_cleanup (xfree, cond_string);
8002 }
8003 }
0fb4aa4b
PA
8004
8005 /* If the user is creating a static tracepoint by marker id
8006 (strace -m MARKER_ID), then store the sals index, so that
8007 breakpoint_re_set can try to match up which of the newly
8008 found markers corresponds to this one, and, don't try to
8009 expand multiple locations for each sal, given than SALS
8010 already should contain all sals for MARKER_ID. */
8011 if (type_wanted == bp_static_tracepoint
7efd8fc2 8012 && is_marker_spec (canonical.canonical[0]))
0fb4aa4b
PA
8013 {
8014 int i;
8015
8016 for (i = 0; i < sals.nelts; ++i)
8017 {
8018 struct symtabs_and_lines expanded;
d9b3f62e 8019 struct tracepoint *tp;
0fb4aa4b
PA
8020 struct cleanup *old_chain;
8021
8022 expanded.nelts = 1;
8023 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
8024 expanded.sals[0] = sals.sals[i];
8025 old_chain = make_cleanup (xfree, expanded.sals);
8026
d9b3f62e
PA
8027 tp = XCNEW (struct tracepoint);
8028 init_breakpoint_sal (&tp->base, gdbarch, expanded,
8029 canonical.canonical[i],
8030 cond_string, type_wanted,
8031 tempflag ? disp_del : disp_donttouch,
8032 thread, task, ignore_count, ops,
8033 from_tty, enabled, internal,
8034 canonical.special_display);
0fb4aa4b
PA
8035 /* Given that its possible to have multiple markers with
8036 the same string id, if the user is creating a static
8037 tracepoint by marker id ("strace -m MARKER_ID"), then
8038 store the sals index, so that breakpoint_re_set can
8039 try to match up which of the newly found markers
8040 corresponds to this one */
8041 tp->static_trace_marker_id_idx = i;
d9b3f62e 8042
3ea46bff 8043 install_breakpoint (internal, &tp->base, 0);
d9b3f62e
PA
8044
8045 do_cleanups (old_chain);
0fb4aa4b
PA
8046 }
8047 }
8048 else
7efd8fc2 8049 create_breakpoints_sal (gdbarch, sals, &canonical, cond_string,
3e43a32a
MS
8050 type_wanted,
8051 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 8052 thread, task, ignore_count, ops, from_tty,
84f4c1fe 8053 enabled, internal);
c906108c 8054 }
0101ce28
JJ
8055 else
8056 {
0101ce28
JJ
8057 struct breakpoint *b;
8058
0101ce28
JJ
8059 make_cleanup (xfree, copy_arg);
8060
bfccc43c
YQ
8061 if (is_tracepoint_type (type_wanted))
8062 {
8063 struct tracepoint *t;
8064
8065 t = XCNEW (struct tracepoint);
8066 b = &t->base;
8067 }
8068 else
8069 b = XNEW (struct breakpoint);
8070
8071 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
8072
7efd8fc2 8073 b->addr_string = canonical.canonical[0];
72b2ff0e 8074 b->cond_string = NULL;
0101ce28 8075 b->ignore_count = ignore_count;
0101ce28 8076 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 8077 b->condition_not_parsed = 1;
41447f92 8078 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 8079 b->pspace = current_program_space;
74960c60 8080
6c95b8df 8081 if (enabled && b->pspace->executing_startup
8bea4e01
UW
8082 && (b->type == bp_breakpoint
8083 || b->type == bp_hardware_breakpoint))
8084 b->enable_state = bp_startup_disabled;
8085
bfccc43c 8086 install_breakpoint (internal, b, 0);
0101ce28
JJ
8087 }
8088
c3f6f71d 8089 if (sals.nelts > 1)
95a42b64 8090 {
3e43a32a
MS
8091 warning (_("Multiple breakpoints were set.\nUse the "
8092 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 8093 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
8094 }
8095
80c99de1
PA
8096 /* That's it. Discard the cleanups for data inserted into the
8097 breakpoint. */
8098 discard_cleanups (bkpt_chain);
8099 /* But cleanup everything else. */
c3f6f71d 8100 do_cleanups (old_chain);
217dc9e2 8101
80c99de1 8102 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 8103 update_global_location_list (1);
fd9b8c24
PA
8104
8105 return 1;
c3f6f71d 8106}
c906108c 8107
348d480f 8108/* Set a breakpoint.
72b2ff0e
VP
8109 ARG is a string describing breakpoint address,
8110 condition, and thread.
8111 FLAG specifies if a breakpoint is hardware on,
8112 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8113 and BP_TEMPFLAG. */
348d480f 8114
98deb0da 8115static void
72b2ff0e 8116break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 8117{
72b2ff0e 8118 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
8119 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8120 ? bp_hardware_breakpoint
8121 : bp_breakpoint);
c3f6f71d 8122
8cdf0e15
VP
8123 create_breakpoint (get_current_arch (),
8124 arg,
8125 NULL, 0, 1 /* parse arg */,
0fb4aa4b 8126 tempflag, type_wanted,
8cdf0e15
VP
8127 0 /* Ignore count */,
8128 pending_break_support,
348d480f 8129 &bkpt_breakpoint_ops,
8cdf0e15 8130 from_tty,
84f4c1fe
PM
8131 1 /* enabled */,
8132 0 /* internal */);
c906108c
SS
8133}
8134
c906108c
SS
8135/* Helper function for break_command_1 and disassemble_command. */
8136
8137void
fba45db2 8138resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
8139{
8140 CORE_ADDR pc;
8141
8142 if (sal->pc == 0 && sal->symtab != NULL)
8143 {
8144 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 8145 error (_("No line %d in file \"%s\"."),
c906108c
SS
8146 sal->line, sal->symtab->filename);
8147 sal->pc = pc;
6a048695 8148
4a64f543
MS
8149 /* If this SAL corresponds to a breakpoint inserted using a line
8150 number, then skip the function prologue if necessary. */
6a048695 8151 if (sal->explicit_line)
059acae7 8152 skip_prologue_sal (sal);
c906108c
SS
8153 }
8154
8155 if (sal->section == 0 && sal->symtab != NULL)
8156 {
8157 struct blockvector *bv;
c5aa993b
JM
8158 struct block *b;
8159 struct symbol *sym;
c906108c 8160
801e3a5b 8161 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
8162 if (bv != NULL)
8163 {
7f0df278 8164 sym = block_linkage_function (b);
c906108c
SS
8165 if (sym != NULL)
8166 {
8167 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 8168 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
8169 }
8170 else
8171 {
4a64f543
MS
8172 /* It really is worthwhile to have the section, so we'll
8173 just have to look harder. This case can be executed
8174 if we have line numbers but no functions (as can
8175 happen in assembly source). */
c906108c 8176
c5aa993b 8177 struct minimal_symbol *msym;
6c95b8df
PA
8178 struct cleanup *old_chain = save_current_space_and_thread ();
8179
8180 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8181
8182 msym = lookup_minimal_symbol_by_pc (sal->pc);
8183 if (msym)
714835d5 8184 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8185
8186 do_cleanups (old_chain);
c906108c
SS
8187 }
8188 }
8189 }
8190}
8191
8192void
fba45db2 8193break_command (char *arg, int from_tty)
c906108c 8194{
db107f19 8195 break_command_1 (arg, 0, from_tty);
c906108c
SS
8196}
8197
c906108c 8198void
fba45db2 8199tbreak_command (char *arg, int from_tty)
c906108c 8200{
db107f19 8201 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8202}
8203
c906108c 8204static void
fba45db2 8205hbreak_command (char *arg, int from_tty)
c906108c 8206{
db107f19 8207 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8208}
8209
8210static void
fba45db2 8211thbreak_command (char *arg, int from_tty)
c906108c 8212{
db107f19 8213 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8214}
8215
8216static void
fba45db2 8217stop_command (char *arg, int from_tty)
c906108c 8218{
a3f17187 8219 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8220Usage: stop in <function | address>\n\
a3f17187 8221 stop at <line>\n"));
c906108c
SS
8222}
8223
8224static void
fba45db2 8225stopin_command (char *arg, int from_tty)
c906108c
SS
8226{
8227 int badInput = 0;
8228
c5aa993b 8229 if (arg == (char *) NULL)
c906108c
SS
8230 badInput = 1;
8231 else if (*arg != '*')
8232 {
8233 char *argptr = arg;
8234 int hasColon = 0;
8235
4a64f543 8236 /* Look for a ':'. If this is a line number specification, then
53a5351d 8237 say it is bad, otherwise, it should be an address or
4a64f543 8238 function/method name. */
c906108c 8239 while (*argptr && !hasColon)
c5aa993b
JM
8240 {
8241 hasColon = (*argptr == ':');
8242 argptr++;
8243 }
c906108c
SS
8244
8245 if (hasColon)
c5aa993b 8246 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8247 else
c5aa993b 8248 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8249 }
8250
8251 if (badInput)
a3f17187 8252 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8253 else
db107f19 8254 break_command_1 (arg, 0, from_tty);
c906108c
SS
8255}
8256
8257static void
fba45db2 8258stopat_command (char *arg, int from_tty)
c906108c
SS
8259{
8260 int badInput = 0;
8261
c5aa993b 8262 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8263 badInput = 1;
8264 else
8265 {
8266 char *argptr = arg;
8267 int hasColon = 0;
8268
4a64f543
MS
8269 /* Look for a ':'. If there is a '::' then get out, otherwise
8270 it is probably a line number. */
c906108c 8271 while (*argptr && !hasColon)
c5aa993b
JM
8272 {
8273 hasColon = (*argptr == ':');
8274 argptr++;
8275 }
c906108c
SS
8276
8277 if (hasColon)
c5aa993b 8278 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8279 else
c5aa993b 8280 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8281 }
8282
8283 if (badInput)
a3f17187 8284 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8285 else
db107f19 8286 break_command_1 (arg, 0, from_tty);
c906108c
SS
8287}
8288
f1310107
TJB
8289/* Implement the "breakpoint_hit" breakpoint_ops method for
8290 ranged breakpoints. */
8291
8292static int
8293breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8294 struct address_space *aspace,
8295 CORE_ADDR bp_addr)
8296{
8297 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8298 bl->length, aspace, bp_addr);
8299}
8300
8301/* Implement the "resources_needed" breakpoint_ops method for
8302 ranged breakpoints. */
8303
8304static int
8305resources_needed_ranged_breakpoint (const struct bp_location *bl)
8306{
8307 return target_ranged_break_num_registers ();
8308}
8309
8310/* Implement the "print_it" breakpoint_ops method for
8311 ranged breakpoints. */
8312
8313static enum print_stop_action
348d480f 8314print_it_ranged_breakpoint (bpstat bs)
f1310107 8315{
348d480f 8316 struct breakpoint *b = bs->breakpoint_at;
f1310107 8317 struct bp_location *bl = b->loc;
79a45e25 8318 struct ui_out *uiout = current_uiout;
f1310107
TJB
8319
8320 gdb_assert (b->type == bp_hardware_breakpoint);
8321
8322 /* Ranged breakpoints have only one location. */
8323 gdb_assert (bl && bl->next == NULL);
8324
8325 annotate_breakpoint (b->number);
8326 if (b->disposition == disp_del)
8327 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8328 else
8329 ui_out_text (uiout, "\nRanged breakpoint ");
8330 if (ui_out_is_mi_like_p (uiout))
8331 {
8332 ui_out_field_string (uiout, "reason",
8333 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8334 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8335 }
8336 ui_out_field_int (uiout, "bkptno", b->number);
8337 ui_out_text (uiout, ", ");
8338
8339 return PRINT_SRC_AND_LOC;
8340}
8341
8342/* Implement the "print_one" breakpoint_ops method for
8343 ranged breakpoints. */
8344
8345static void
8346print_one_ranged_breakpoint (struct breakpoint *b,
8347 struct bp_location **last_loc)
8348{
8349 struct bp_location *bl = b->loc;
8350 struct value_print_options opts;
79a45e25 8351 struct ui_out *uiout = current_uiout;
f1310107
TJB
8352
8353 /* Ranged breakpoints have only one location. */
8354 gdb_assert (bl && bl->next == NULL);
8355
8356 get_user_print_options (&opts);
8357
8358 if (opts.addressprint)
8359 /* We don't print the address range here, it will be printed later
8360 by print_one_detail_ranged_breakpoint. */
8361 ui_out_field_skip (uiout, "addr");
8362 annotate_field (5);
8363 print_breakpoint_location (b, bl);
8364 *last_loc = bl;
8365}
8366
8367/* Implement the "print_one_detail" breakpoint_ops method for
8368 ranged breakpoints. */
8369
8370static void
8371print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8372 struct ui_out *uiout)
8373{
8374 CORE_ADDR address_start, address_end;
8375 struct bp_location *bl = b->loc;
8376 struct ui_stream *stb = ui_out_stream_new (uiout);
8377 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8378
8379 gdb_assert (bl);
8380
8381 address_start = bl->address;
8382 address_end = address_start + bl->length - 1;
8383
8384 ui_out_text (uiout, "\taddress range: ");
8385 fprintf_unfiltered (stb->stream, "[%s, %s]",
8386 print_core_address (bl->gdbarch, address_start),
8387 print_core_address (bl->gdbarch, address_end));
8388 ui_out_field_stream (uiout, "addr", stb);
8389 ui_out_text (uiout, "\n");
8390
8391 do_cleanups (cleanup);
8392}
8393
8394/* Implement the "print_mention" breakpoint_ops method for
8395 ranged breakpoints. */
8396
8397static void
8398print_mention_ranged_breakpoint (struct breakpoint *b)
8399{
8400 struct bp_location *bl = b->loc;
79a45e25 8401 struct ui_out *uiout = current_uiout;
f1310107
TJB
8402
8403 gdb_assert (bl);
8404 gdb_assert (b->type == bp_hardware_breakpoint);
8405
8406 if (ui_out_is_mi_like_p (uiout))
8407 return;
8408
8409 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8410 b->number, paddress (bl->gdbarch, bl->address),
8411 paddress (bl->gdbarch, bl->address + bl->length - 1));
8412}
8413
8414/* Implement the "print_recreate" breakpoint_ops method for
8415 ranged breakpoints. */
8416
8417static void
8418print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8419{
8420 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8421 b->addr_string_range_end);
d9b3f62e 8422 print_recreate_thread (b, fp);
f1310107
TJB
8423}
8424
8425/* The breakpoint_ops structure to be used in ranged breakpoints. */
8426
2060206e 8427static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
8428
8429/* Find the address where the end of the breakpoint range should be
8430 placed, given the SAL of the end of the range. This is so that if
8431 the user provides a line number, the end of the range is set to the
8432 last instruction of the given line. */
8433
8434static CORE_ADDR
8435find_breakpoint_range_end (struct symtab_and_line sal)
8436{
8437 CORE_ADDR end;
8438
8439 /* If the user provided a PC value, use it. Otherwise,
8440 find the address of the end of the given location. */
8441 if (sal.explicit_pc)
8442 end = sal.pc;
8443 else
8444 {
8445 int ret;
8446 CORE_ADDR start;
8447
8448 ret = find_line_pc_range (sal, &start, &end);
8449 if (!ret)
8450 error (_("Could not find location of the end of the range."));
8451
8452 /* find_line_pc_range returns the start of the next line. */
8453 end--;
8454 }
8455
8456 return end;
8457}
8458
8459/* Implement the "break-range" CLI command. */
8460
8461static void
8462break_range_command (char *arg, int from_tty)
8463{
8464 char *arg_start, *addr_string_start, *addr_string_end;
8465 struct linespec_result canonical_start, canonical_end;
8466 int bp_count, can_use_bp, length;
8467 CORE_ADDR end;
8468 struct breakpoint *b;
8469 struct symtab_and_line sal_start, sal_end;
8470 struct symtabs_and_lines sals_start, sals_end;
8471 struct cleanup *cleanup_bkpt;
8472
8473 /* We don't support software ranged breakpoints. */
8474 if (target_ranged_break_num_registers () < 0)
8475 error (_("This target does not support hardware ranged breakpoints."));
8476
8477 bp_count = hw_breakpoint_used_count ();
8478 bp_count += target_ranged_break_num_registers ();
8479 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8480 bp_count, 0);
8481 if (can_use_bp < 0)
8482 error (_("Hardware breakpoints used exceeds limit."));
8483
8484 if (arg == NULL || arg[0] == '\0')
8485 error(_("No address range specified."));
8486
8487 sals_start.sals = NULL;
8488 sals_start.nelts = 0;
8489 init_linespec_result (&canonical_start);
8490
8491 while (*arg == ' ' || *arg == '\t')
8492 arg++;
8493
58438ac1 8494 parse_breakpoint_sals (&arg, &sals_start, &canonical_start);
f1310107
TJB
8495
8496 sal_start = sals_start.sals[0];
8497 addr_string_start = canonical_start.canonical[0];
8498 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
8499 xfree (sals_start.sals);
8500 xfree (canonical_start.canonical);
8501
8502 if (arg[0] != ',')
8503 error (_("Too few arguments."));
8504 else if (sals_start.nelts == 0)
8505 error (_("Could not find location of the beginning of the range."));
8506 else if (sals_start.nelts != 1)
8507 error (_("Cannot create a ranged breakpoint with multiple locations."));
8508
8509 resolve_sal_pc (&sal_start);
8510
8511 arg++; /* Skip the comma. */
8512 while (*arg == ' ' || *arg == '\t')
8513 arg++;
8514
8515 /* Parse the end location. */
8516
8517 sals_end.sals = NULL;
8518 sals_end.nelts = 0;
8519 init_linespec_result (&canonical_end);
8520 arg_start = arg;
8521
423f41a5 8522 /* We call decode_line_1 directly here instead of using
f1310107
TJB
8523 parse_breakpoint_sals because we need to specify the start location's
8524 symtab and line as the default symtab and line for the end of the
8525 range. This makes it possible to have ranges like "foo.c:27, +14",
8526 where +14 means 14 lines from the start location. */
8527 sals_end = decode_line_1 (&arg, 1, sal_start.symtab, sal_start.line,
58438ac1 8528 &canonical_end);
f1310107
TJB
8529
8530 /* canonical_end can be NULL if it was of the form "*0xdeadbeef". */
8531 if (canonical_end.canonical == NULL)
38a714bb 8532 canonical_end.canonical = xcalloc (1, sizeof (char *));
f1310107
TJB
8533 /* Add the string if not present. */
8534 if (arg_start != arg && canonical_end.canonical[0] == NULL)
8535 canonical_end.canonical[0] = savestring (arg_start, arg - arg_start);
8536
8537 sal_end = sals_end.sals[0];
8538 addr_string_end = canonical_end.canonical[0];
8539 make_cleanup (xfree, addr_string_end);
8540 xfree (sals_end.sals);
8541 xfree (canonical_end.canonical);
8542
8543 if (sals_end.nelts == 0)
8544 error (_("Could not find location of the end of the range."));
8545 else if (sals_end.nelts != 1)
8546 error (_("Cannot create a ranged breakpoint with multiple locations."));
8547
8548 resolve_sal_pc (&sal_end);
8549
8550 end = find_breakpoint_range_end (sal_end);
8551 if (sal_start.pc > end)
177b42fe 8552 error (_("Invalid address range, end precedes start."));
f1310107
TJB
8553
8554 length = end - sal_start.pc + 1;
8555 if (length < 0)
8556 /* Length overflowed. */
8557 error (_("Address range too large."));
8558 else if (length == 1)
8559 {
8560 /* This range is simple enough to be handled by
8561 the `hbreak' command. */
8562 hbreak_command (addr_string_start, 1);
8563
8564 do_cleanups (cleanup_bkpt);
8565
8566 return;
8567 }
8568
8569 /* Now set up the breakpoint. */
8570 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 8571 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
8572 set_breakpoint_count (breakpoint_count + 1);
8573 b->number = breakpoint_count;
8574 b->disposition = disp_donttouch;
8575 b->addr_string = addr_string_start;
8576 b->addr_string_range_end = addr_string_end;
f1310107
TJB
8577 b->loc->length = length;
8578
8579 discard_cleanups (cleanup_bkpt);
8580
8581 mention (b);
8d3788bd 8582 observer_notify_breakpoint_created (b);
f1310107
TJB
8583 update_global_location_list (1);
8584}
8585
4a64f543
MS
8586/* Return non-zero if EXP is verified as constant. Returned zero
8587 means EXP is variable. Also the constant detection may fail for
8588 some constant expressions and in such case still falsely return
8589 zero. */
65d79d4b
SDJ
8590static int
8591watchpoint_exp_is_const (const struct expression *exp)
8592{
8593 int i = exp->nelts;
8594
8595 while (i > 0)
8596 {
8597 int oplenp, argsp;
8598
8599 /* We are only interested in the descriptor of each element. */
8600 operator_length (exp, i, &oplenp, &argsp);
8601 i -= oplenp;
8602
8603 switch (exp->elts[i].opcode)
8604 {
8605 case BINOP_ADD:
8606 case BINOP_SUB:
8607 case BINOP_MUL:
8608 case BINOP_DIV:
8609 case BINOP_REM:
8610 case BINOP_MOD:
8611 case BINOP_LSH:
8612 case BINOP_RSH:
8613 case BINOP_LOGICAL_AND:
8614 case BINOP_LOGICAL_OR:
8615 case BINOP_BITWISE_AND:
8616 case BINOP_BITWISE_IOR:
8617 case BINOP_BITWISE_XOR:
8618 case BINOP_EQUAL:
8619 case BINOP_NOTEQUAL:
8620 case BINOP_LESS:
8621 case BINOP_GTR:
8622 case BINOP_LEQ:
8623 case BINOP_GEQ:
8624 case BINOP_REPEAT:
8625 case BINOP_COMMA:
8626 case BINOP_EXP:
8627 case BINOP_MIN:
8628 case BINOP_MAX:
8629 case BINOP_INTDIV:
8630 case BINOP_CONCAT:
8631 case BINOP_IN:
8632 case BINOP_RANGE:
8633 case TERNOP_COND:
8634 case TERNOP_SLICE:
8635 case TERNOP_SLICE_COUNT:
8636
8637 case OP_LONG:
8638 case OP_DOUBLE:
8639 case OP_DECFLOAT:
8640 case OP_LAST:
8641 case OP_COMPLEX:
8642 case OP_STRING:
8643 case OP_BITSTRING:
8644 case OP_ARRAY:
8645 case OP_TYPE:
8646 case OP_NAME:
8647 case OP_OBJC_NSSTRING:
8648
8649 case UNOP_NEG:
8650 case UNOP_LOGICAL_NOT:
8651 case UNOP_COMPLEMENT:
8652 case UNOP_ADDR:
8653 case UNOP_HIGH:
4a64f543
MS
8654 /* Unary, binary and ternary operators: We have to check
8655 their operands. If they are constant, then so is the
8656 result of that operation. For instance, if A and B are
8657 determined to be constants, then so is "A + B".
8658
8659 UNOP_IND is one exception to the rule above, because the
8660 value of *ADDR is not necessarily a constant, even when
8661 ADDR is. */
65d79d4b
SDJ
8662 break;
8663
8664 case OP_VAR_VALUE:
8665 /* Check whether the associated symbol is a constant.
4a64f543 8666
65d79d4b 8667 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8668 possible that a buggy compiler could mark a variable as
8669 constant even when it is not, and TYPE_CONST would return
8670 true in this case, while SYMBOL_CLASS wouldn't.
8671
8672 We also have to check for function symbols because they
8673 are always constant. */
65d79d4b
SDJ
8674 {
8675 struct symbol *s = exp->elts[i + 2].symbol;
8676
8677 if (SYMBOL_CLASS (s) != LOC_BLOCK
8678 && SYMBOL_CLASS (s) != LOC_CONST
8679 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8680 return 0;
8681 break;
8682 }
8683
8684 /* The default action is to return 0 because we are using
8685 the optimistic approach here: If we don't know something,
8686 then it is not a constant. */
8687 default:
8688 return 0;
8689 }
8690 }
8691
8692 return 1;
8693}
8694
3a5c3e22
PA
8695/* Implement the "dtor" breakpoint_ops method for watchpoints. */
8696
8697static void
8698dtor_watchpoint (struct breakpoint *self)
8699{
8700 struct watchpoint *w = (struct watchpoint *) self;
8701
8702 xfree (w->cond_exp);
8703 xfree (w->exp);
8704 xfree (w->exp_string);
8705 xfree (w->exp_string_reparse);
8706 value_free (w->val);
8707
8708 base_breakpoint_ops.dtor (self);
8709}
8710
348d480f
PA
8711/* Implement the "re_set" breakpoint_ops method for watchpoints. */
8712
8713static void
8714re_set_watchpoint (struct breakpoint *b)
8715{
3a5c3e22
PA
8716 struct watchpoint *w = (struct watchpoint *) b;
8717
348d480f
PA
8718 /* Watchpoint can be either on expression using entirely global
8719 variables, or it can be on local variables.
8720
8721 Watchpoints of the first kind are never auto-deleted, and even
8722 persist across program restarts. Since they can use variables
8723 from shared libraries, we need to reparse expression as libraries
8724 are loaded and unloaded.
8725
8726 Watchpoints on local variables can also change meaning as result
8727 of solib event. For example, if a watchpoint uses both a local
8728 and a global variables in expression, it's a local watchpoint,
8729 but unloading of a shared library will make the expression
8730 invalid. This is not a very common use case, but we still
8731 re-evaluate expression, to avoid surprises to the user.
8732
8733 Note that for local watchpoints, we re-evaluate it only if
8734 watchpoints frame id is still valid. If it's not, it means the
8735 watchpoint is out of scope and will be deleted soon. In fact,
8736 I'm not sure we'll ever be called in this case.
8737
8738 If a local watchpoint's frame id is still valid, then
3a5c3e22 8739 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 8740
3a5c3e22
PA
8741 Don't do anything about disabled watchpoints, since they will be
8742 reevaluated again when enabled. */
8743 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
8744}
8745
77b06cd7
TJB
8746/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8747
8748static int
8749insert_watchpoint (struct bp_location *bl)
8750{
3a5c3e22
PA
8751 struct watchpoint *w = (struct watchpoint *) bl->owner;
8752 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8753
8754 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8755 w->cond_exp);
77b06cd7
TJB
8756}
8757
8758/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8759
8760static int
8761remove_watchpoint (struct bp_location *bl)
8762{
3a5c3e22
PA
8763 struct watchpoint *w = (struct watchpoint *) bl->owner;
8764 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8765
8766 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8767 w->cond_exp);
e09342b5
TJB
8768}
8769
e09342b5 8770static int
348d480f
PA
8771breakpoint_hit_watchpoint (const struct bp_location *bl,
8772 struct address_space *aspace, CORE_ADDR bp_addr)
e09342b5 8773{
348d480f 8774 struct breakpoint *b = bl->owner;
3a5c3e22 8775 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 8776
348d480f
PA
8777 /* Continuable hardware watchpoints are treated as non-existent if the
8778 reason we stopped wasn't a hardware watchpoint (we didn't stop on
8779 some data address). Otherwise gdb won't stop on a break instruction
8780 in the code (not from a breakpoint) when a hardware watchpoint has
8781 been defined. Also skip watchpoints which we know did not trigger
8782 (did not match the data address). */
8783 if (is_hardware_watchpoint (b)
3a5c3e22 8784 && w->watchpoint_triggered == watch_triggered_no)
348d480f 8785 return 0;
9c06b0b4 8786
348d480f 8787 return 1;
9c06b0b4
TJB
8788}
8789
348d480f
PA
8790static void
8791check_status_watchpoint (bpstat bs)
9c06b0b4 8792{
348d480f 8793 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 8794
348d480f 8795 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
8796}
8797
8798/* Implement the "resources_needed" breakpoint_ops method for
348d480f 8799 hardware watchpoints. */
9c06b0b4
TJB
8800
8801static int
348d480f 8802resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 8803{
3a5c3e22
PA
8804 struct watchpoint *w = (struct watchpoint *) bl->owner;
8805 int length = w->exact? 1 : bl->length;
348d480f
PA
8806
8807 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
8808}
8809
8810/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 8811 hardware watchpoints. */
9c06b0b4
TJB
8812
8813static int
348d480f 8814works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 8815{
efa80663
PA
8816 /* Read and access watchpoints only work with hardware support. */
8817 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
8818}
8819
9c06b0b4 8820static enum print_stop_action
348d480f 8821print_it_watchpoint (bpstat bs)
9c06b0b4 8822{
348d480f
PA
8823 struct cleanup *old_chain;
8824 struct breakpoint *b;
8825 const struct bp_location *bl;
8826 struct ui_stream *stb;
8827 enum print_stop_action result;
3a5c3e22 8828 struct watchpoint *w;
79a45e25 8829 struct ui_out *uiout = current_uiout;
348d480f
PA
8830
8831 gdb_assert (bs->bp_location_at != NULL);
8832
8833 bl = bs->bp_location_at;
8834 b = bs->breakpoint_at;
3a5c3e22 8835 w = (struct watchpoint *) b;
348d480f
PA
8836
8837 stb = ui_out_stream_new (uiout);
8838 old_chain = make_cleanup_ui_out_stream_delete (stb);
9c06b0b4
TJB
8839
8840 switch (b->type)
8841 {
348d480f 8842 case bp_watchpoint:
9c06b0b4
TJB
8843 case bp_hardware_watchpoint:
8844 annotate_watchpoint (b->number);
8845 if (ui_out_is_mi_like_p (uiout))
8846 ui_out_field_string
8847 (uiout, "reason",
8848 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
8849 mention (b);
8850 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8851 ui_out_text (uiout, "\nOld value = ");
8852 watchpoint_value_print (bs->old_val, stb->stream);
8853 ui_out_field_stream (uiout, "old", stb);
8854 ui_out_text (uiout, "\nNew value = ");
3a5c3e22 8855 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8856 ui_out_field_stream (uiout, "new", stb);
8857 ui_out_text (uiout, "\n");
8858 /* More than one watchpoint may have been triggered. */
8859 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8860 break;
8861
8862 case bp_read_watchpoint:
8863 if (ui_out_is_mi_like_p (uiout))
8864 ui_out_field_string
8865 (uiout, "reason",
8866 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
8867 mention (b);
8868 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8869 ui_out_text (uiout, "\nValue = ");
3a5c3e22 8870 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8871 ui_out_field_stream (uiout, "value", stb);
8872 ui_out_text (uiout, "\n");
8873 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8874 break;
8875
8876 case bp_access_watchpoint:
348d480f
PA
8877 if (bs->old_val != NULL)
8878 {
8879 annotate_watchpoint (b->number);
8880 if (ui_out_is_mi_like_p (uiout))
8881 ui_out_field_string
8882 (uiout, "reason",
8883 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8884 mention (b);
8885 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8886 ui_out_text (uiout, "\nOld value = ");
8887 watchpoint_value_print (bs->old_val, stb->stream);
8888 ui_out_field_stream (uiout, "old", stb);
8889 ui_out_text (uiout, "\nNew value = ");
8890 }
8891 else
8892 {
8893 mention (b);
8894 if (ui_out_is_mi_like_p (uiout))
8895 ui_out_field_string
8896 (uiout, "reason",
8897 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8898 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8899 ui_out_text (uiout, "\nValue = ");
8900 }
3a5c3e22 8901 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8902 ui_out_field_stream (uiout, "new", stb);
8903 ui_out_text (uiout, "\n");
8904 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8905 break;
8906 default:
348d480f 8907 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8908 }
8909
348d480f
PA
8910 do_cleanups (old_chain);
8911 return result;
8912}
8913
8914/* Implement the "print_mention" breakpoint_ops method for hardware
8915 watchpoints. */
8916
8917static void
8918print_mention_watchpoint (struct breakpoint *b)
8919{
8920 struct cleanup *ui_out_chain;
3a5c3e22 8921 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 8922 struct ui_out *uiout = current_uiout;
348d480f
PA
8923
8924 switch (b->type)
8925 {
8926 case bp_watchpoint:
8927 ui_out_text (uiout, "Watchpoint ");
8928 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8929 break;
8930 case bp_hardware_watchpoint:
8931 ui_out_text (uiout, "Hardware watchpoint ");
8932 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8933 break;
8934 case bp_read_watchpoint:
8935 ui_out_text (uiout, "Hardware read watchpoint ");
8936 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8937 break;
8938 case bp_access_watchpoint:
8939 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
8940 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8941 break;
8942 default:
8943 internal_error (__FILE__, __LINE__,
8944 _("Invalid hardware watchpoint type."));
8945 }
8946
8947 ui_out_field_int (uiout, "number", b->number);
8948 ui_out_text (uiout, ": ");
3a5c3e22 8949 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
8950 do_cleanups (ui_out_chain);
8951}
8952
8953/* Implement the "print_recreate" breakpoint_ops method for
8954 watchpoints. */
8955
8956static void
8957print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
8958{
3a5c3e22
PA
8959 struct watchpoint *w = (struct watchpoint *) b;
8960
348d480f
PA
8961 switch (b->type)
8962 {
8963 case bp_watchpoint:
8964 case bp_hardware_watchpoint:
8965 fprintf_unfiltered (fp, "watch");
8966 break;
8967 case bp_read_watchpoint:
8968 fprintf_unfiltered (fp, "rwatch");
8969 break;
8970 case bp_access_watchpoint:
8971 fprintf_unfiltered (fp, "awatch");
8972 break;
8973 default:
8974 internal_error (__FILE__, __LINE__,
8975 _("Invalid watchpoint type."));
8976 }
8977
3a5c3e22 8978 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 8979 print_recreate_thread (b, fp);
348d480f
PA
8980}
8981
8982/* The breakpoint_ops structure to be used in hardware watchpoints. */
8983
2060206e 8984static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
8985
8986/* Implement the "insert" breakpoint_ops method for
8987 masked hardware watchpoints. */
8988
8989static int
8990insert_masked_watchpoint (struct bp_location *bl)
8991{
3a5c3e22
PA
8992 struct watchpoint *w = (struct watchpoint *) bl->owner;
8993
8994 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8995 bl->watchpoint_type);
8996}
8997
8998/* Implement the "remove" breakpoint_ops method for
8999 masked hardware watchpoints. */
9000
9001static int
9002remove_masked_watchpoint (struct bp_location *bl)
9003{
3a5c3e22
PA
9004 struct watchpoint *w = (struct watchpoint *) bl->owner;
9005
9006 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
9007 bl->watchpoint_type);
9008}
9009
9010/* Implement the "resources_needed" breakpoint_ops method for
9011 masked hardware watchpoints. */
9012
9013static int
9014resources_needed_masked_watchpoint (const struct bp_location *bl)
9015{
3a5c3e22
PA
9016 struct watchpoint *w = (struct watchpoint *) bl->owner;
9017
9018 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
9019}
9020
9021/* Implement the "works_in_software_mode" breakpoint_ops method for
9022 masked hardware watchpoints. */
9023
9024static int
9025works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
9026{
9027 return 0;
9028}
9029
9030/* Implement the "print_it" breakpoint_ops method for
9031 masked hardware watchpoints. */
9032
9033static enum print_stop_action
9034print_it_masked_watchpoint (bpstat bs)
9035{
9036 struct breakpoint *b = bs->breakpoint_at;
79a45e25 9037 struct ui_out *uiout = current_uiout;
348d480f
PA
9038
9039 /* Masked watchpoints have only one location. */
9040 gdb_assert (b->loc && b->loc->next == NULL);
9041
9042 switch (b->type)
9043 {
9044 case bp_hardware_watchpoint:
9045 annotate_watchpoint (b->number);
9046 if (ui_out_is_mi_like_p (uiout))
9047 ui_out_field_string
9048 (uiout, "reason",
9049 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9050 break;
9051
9052 case bp_read_watchpoint:
9053 if (ui_out_is_mi_like_p (uiout))
9054 ui_out_field_string
9055 (uiout, "reason",
9056 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9057 break;
9058
9059 case bp_access_watchpoint:
9060 if (ui_out_is_mi_like_p (uiout))
9061 ui_out_field_string
9062 (uiout, "reason",
9063 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9064 break;
9065 default:
9066 internal_error (__FILE__, __LINE__,
9067 _("Invalid hardware watchpoint type."));
9068 }
9069
9070 mention (b);
9c06b0b4
TJB
9071 ui_out_text (uiout, _("\n\
9072Check the underlying instruction at PC for the memory\n\
9073address and value which triggered this watchpoint.\n"));
9074 ui_out_text (uiout, "\n");
9075
9076 /* More than one watchpoint may have been triggered. */
9077 return PRINT_UNKNOWN;
9078}
9079
9080/* Implement the "print_one_detail" breakpoint_ops method for
9081 masked hardware watchpoints. */
9082
9083static void
9084print_one_detail_masked_watchpoint (const struct breakpoint *b,
9085 struct ui_out *uiout)
9086{
3a5c3e22
PA
9087 struct watchpoint *w = (struct watchpoint *) b;
9088
9c06b0b4
TJB
9089 /* Masked watchpoints have only one location. */
9090 gdb_assert (b->loc && b->loc->next == NULL);
9091
9092 ui_out_text (uiout, "\tmask ");
3a5c3e22 9093 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
9094 ui_out_text (uiout, "\n");
9095}
9096
9097/* Implement the "print_mention" breakpoint_ops method for
9098 masked hardware watchpoints. */
9099
9100static void
9101print_mention_masked_watchpoint (struct breakpoint *b)
9102{
3a5c3e22 9103 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 9104 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
9105 struct cleanup *ui_out_chain;
9106
9107 switch (b->type)
9108 {
9109 case bp_hardware_watchpoint:
9110 ui_out_text (uiout, "Masked hardware watchpoint ");
9111 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
9112 break;
9113 case bp_read_watchpoint:
9114 ui_out_text (uiout, "Masked hardware read watchpoint ");
9115 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
9116 break;
9117 case bp_access_watchpoint:
9118 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
9119 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
9120 break;
9121 default:
9122 internal_error (__FILE__, __LINE__,
9123 _("Invalid hardware watchpoint type."));
9124 }
9125
9126 ui_out_field_int (uiout, "number", b->number);
9127 ui_out_text (uiout, ": ");
3a5c3e22 9128 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
9129 do_cleanups (ui_out_chain);
9130}
9131
9132/* Implement the "print_recreate" breakpoint_ops method for
9133 masked hardware watchpoints. */
9134
9135static void
9136print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
9137{
3a5c3e22 9138 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
9139 char tmp[40];
9140
9141 switch (b->type)
9142 {
9143 case bp_hardware_watchpoint:
9144 fprintf_unfiltered (fp, "watch");
9145 break;
9146 case bp_read_watchpoint:
9147 fprintf_unfiltered (fp, "rwatch");
9148 break;
9149 case bp_access_watchpoint:
9150 fprintf_unfiltered (fp, "awatch");
9151 break;
9152 default:
9153 internal_error (__FILE__, __LINE__,
9154 _("Invalid hardware watchpoint type."));
9155 }
9156
3a5c3e22
PA
9157 sprintf_vma (tmp, w->hw_wp_mask);
9158 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 9159 print_recreate_thread (b, fp);
9c06b0b4
TJB
9160}
9161
9162/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
9163
2060206e 9164static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
9165
9166/* Tell whether the given watchpoint is a masked hardware watchpoint. */
9167
9168static int
9169is_masked_watchpoint (const struct breakpoint *b)
9170{
9171 return b->ops == &masked_watchpoint_breakpoint_ops;
9172}
9173
53a5351d
JM
9174/* accessflag: hw_write: watch write,
9175 hw_read: watch read,
9176 hw_access: watch access (read or write) */
c906108c 9177static void
84f4c1fe
PM
9178watch_command_1 (char *arg, int accessflag, int from_tty,
9179 int just_location, int internal)
c906108c 9180{
a9634178 9181 volatile struct gdb_exception e;
d983da9c 9182 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 9183 struct expression *exp;
60e1c644 9184 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 9185 struct value *val, *mark, *result;
c906108c 9186 struct frame_info *frame;
c906108c
SS
9187 char *exp_start = NULL;
9188 char *exp_end = NULL;
9c06b0b4
TJB
9189 char *tok, *end_tok;
9190 int toklen = -1;
c906108c
SS
9191 char *cond_start = NULL;
9192 char *cond_end = NULL;
c906108c 9193 enum bptype bp_type;
37e4754d 9194 int thread = -1;
0cf6dd15 9195 int pc = 0;
9c06b0b4
TJB
9196 /* Flag to indicate whether we are going to use masks for
9197 the hardware watchpoint. */
9198 int use_mask = 0;
9199 CORE_ADDR mask = 0;
3a5c3e22 9200 struct watchpoint *w;
c906108c 9201
37e4754d
LM
9202 /* Make sure that we actually have parameters to parse. */
9203 if (arg != NULL && arg[0] != '\0')
9204 {
9c06b0b4 9205 char *value_start;
37e4754d 9206
9c06b0b4
TJB
9207 /* Look for "parameter value" pairs at the end
9208 of the arguments string. */
9209 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
9210 {
9211 /* Skip whitespace at the end of the argument list. */
9212 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9213 tok--;
9214
9215 /* Find the beginning of the last token.
9216 This is the value of the parameter. */
9217 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9218 tok--;
9219 value_start = tok + 1;
9220
9221 /* Skip whitespace. */
9222 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9223 tok--;
9224
9225 end_tok = tok;
9226
9227 /* Find the beginning of the second to last token.
9228 This is the parameter itself. */
9229 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9230 tok--;
9231 tok++;
9232 toklen = end_tok - tok + 1;
9233
9234 if (toklen == 6 && !strncmp (tok, "thread", 6))
9235 {
9236 /* At this point we've found a "thread" token, which means
9237 the user is trying to set a watchpoint that triggers
9238 only in a specific thread. */
9239 char *endp;
37e4754d 9240
9c06b0b4
TJB
9241 if (thread != -1)
9242 error(_("You can specify only one thread."));
37e4754d 9243
9c06b0b4
TJB
9244 /* Extract the thread ID from the next token. */
9245 thread = strtol (value_start, &endp, 0);
37e4754d 9246
9c06b0b4
TJB
9247 /* Check if the user provided a valid numeric value for the
9248 thread ID. */
9249 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9250 error (_("Invalid thread ID specification %s."), value_start);
9251
9252 /* Check if the thread actually exists. */
9253 if (!valid_thread_id (thread))
9254 error (_("Unknown thread %d."), thread);
9255 }
9256 else if (toklen == 4 && !strncmp (tok, "mask", 4))
9257 {
9258 /* We've found a "mask" token, which means the user wants to
9259 create a hardware watchpoint that is going to have the mask
9260 facility. */
9261 struct value *mask_value, *mark;
37e4754d 9262
9c06b0b4
TJB
9263 if (use_mask)
9264 error(_("You can specify only one mask."));
37e4754d 9265
9c06b0b4 9266 use_mask = just_location = 1;
37e4754d 9267
9c06b0b4
TJB
9268 mark = value_mark ();
9269 mask_value = parse_to_comma_and_eval (&value_start);
9270 mask = value_as_address (mask_value);
9271 value_free_to_mark (mark);
9272 }
9273 else
9274 /* We didn't recognize what we found. We should stop here. */
9275 break;
37e4754d 9276
9c06b0b4
TJB
9277 /* Truncate the string and get rid of the "parameter value" pair before
9278 the arguments string is parsed by the parse_exp_1 function. */
9279 *tok = '\0';
9280 }
37e4754d
LM
9281 }
9282
9283 /* Parse the rest of the arguments. */
c906108c
SS
9284 innermost_block = NULL;
9285 exp_start = arg;
9286 exp = parse_exp_1 (&arg, 0, 0);
9287 exp_end = arg;
fa8a61dc
TT
9288 /* Remove trailing whitespace from the expression before saving it.
9289 This makes the eventual display of the expression string a bit
9290 prettier. */
9291 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9292 --exp_end;
9293
65d79d4b
SDJ
9294 /* Checking if the expression is not constant. */
9295 if (watchpoint_exp_is_const (exp))
9296 {
9297 int len;
9298
9299 len = exp_end - exp_start;
9300 while (len > 0 && isspace (exp_start[len - 1]))
9301 len--;
9302 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9303 }
9304
c906108c
SS
9305 exp_valid_block = innermost_block;
9306 mark = value_mark ();
a1442452 9307 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
9308
9309 if (just_location)
9310 {
9c06b0b4
TJB
9311 int ret;
9312
06a64a0b 9313 exp_valid_block = NULL;
a1442452 9314 val = value_addr (result);
06a64a0b
TT
9315 release_value (val);
9316 value_free_to_mark (mark);
9c06b0b4
TJB
9317
9318 if (use_mask)
9319 {
9320 ret = target_masked_watch_num_registers (value_as_address (val),
9321 mask);
9322 if (ret == -1)
9323 error (_("This target does not support masked watchpoints."));
9324 else if (ret == -2)
9325 error (_("Invalid mask or memory region."));
9326 }
06a64a0b
TT
9327 }
9328 else if (val != NULL)
fa4727a6 9329 release_value (val);
c906108c 9330
e9cafbcc
TT
9331 tok = skip_spaces (arg);
9332 end_tok = skip_to_space (tok);
c906108c
SS
9333
9334 toklen = end_tok - tok;
9335 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9336 {
2d134ed3
PA
9337 struct expression *cond;
9338
60e1c644 9339 innermost_block = NULL;
c906108c
SS
9340 tok = cond_start = end_tok + 1;
9341 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
9342
9343 /* The watchpoint expression may not be local, but the condition
9344 may still be. E.g.: `watch global if local > 0'. */
9345 cond_exp_valid_block = innermost_block;
9346
2d134ed3 9347 xfree (cond);
c906108c
SS
9348 cond_end = tok;
9349 }
9350 if (*tok)
8a3fe4f8 9351 error (_("Junk at end of command."));
c906108c 9352
53a5351d 9353 if (accessflag == hw_read)
c5aa993b 9354 bp_type = bp_read_watchpoint;
53a5351d 9355 else if (accessflag == hw_access)
c5aa993b
JM
9356 bp_type = bp_access_watchpoint;
9357 else
9358 bp_type = bp_hardware_watchpoint;
c906108c 9359
d983da9c 9360 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
9361
9362 /* If the expression is "local", then set up a "watchpoint scope"
9363 breakpoint at the point where we've left the scope of the watchpoint
9364 expression. Create the scope breakpoint before the watchpoint, so
9365 that we will encounter it first in bpstat_stop_status. */
60e1c644 9366 if (exp_valid_block && frame)
d983da9c 9367 {
edb3359d
DJ
9368 if (frame_id_p (frame_unwind_caller_id (frame)))
9369 {
9370 scope_breakpoint
a6d9a66e
UW
9371 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
9372 frame_unwind_caller_pc (frame),
06edf0c0
PA
9373 bp_watchpoint_scope,
9374 &momentary_breakpoint_ops);
d983da9c 9375
edb3359d 9376 scope_breakpoint->enable_state = bp_enabled;
d983da9c 9377
edb3359d
DJ
9378 /* Automatically delete the breakpoint when it hits. */
9379 scope_breakpoint->disposition = disp_del;
d983da9c 9380
edb3359d
DJ
9381 /* Only break in the proper frame (help with recursion). */
9382 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 9383
edb3359d 9384 /* Set the address at which we will stop. */
a6d9a66e
UW
9385 scope_breakpoint->loc->gdbarch
9386 = frame_unwind_caller_arch (frame);
edb3359d
DJ
9387 scope_breakpoint->loc->requested_address
9388 = frame_unwind_caller_pc (frame);
9389 scope_breakpoint->loc->address
a6d9a66e
UW
9390 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9391 scope_breakpoint->loc->requested_address,
edb3359d
DJ
9392 scope_breakpoint->type);
9393 }
d983da9c
DJ
9394 }
9395
c906108c 9396 /* Now set up the breakpoint. */
3a5c3e22
PA
9397
9398 w = XCNEW (struct watchpoint);
9399 b = &w->base;
348d480f 9400 if (use_mask)
3a5c3e22
PA
9401 init_raw_breakpoint_without_location (b, NULL, bp_type,
9402 &masked_watchpoint_breakpoint_ops);
348d480f 9403 else
3a5c3e22
PA
9404 init_raw_breakpoint_without_location (b, NULL, bp_type,
9405 &watchpoint_breakpoint_ops);
37e4754d 9406 b->thread = thread;
b5de0fa7 9407 b->disposition = disp_donttouch;
348d480f 9408 b->pspace = current_program_space;
3a5c3e22
PA
9409 w->exp = exp;
9410 w->exp_valid_block = exp_valid_block;
9411 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
9412 if (just_location)
9413 {
9414 struct type *t = value_type (val);
9415 CORE_ADDR addr = value_as_address (val);
9416 char *name;
9417
9418 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9419 name = type_to_string (t);
9420
3a5c3e22 9421 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 9422 core_addr_to_string (addr));
06a64a0b
TT
9423 xfree (name);
9424
3a5c3e22 9425 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
9426 (int) (exp_end - exp_start), exp_start);
9427
06a64a0b
TT
9428 /* The above expression is in C. */
9429 b->language = language_c;
9430 }
9431 else
3a5c3e22 9432 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
9433
9434 if (use_mask)
9435 {
3a5c3e22 9436 w->hw_wp_mask = mask;
9c06b0b4
TJB
9437 }
9438 else
9439 {
3a5c3e22
PA
9440 w->val = val;
9441 w->val_valid = 1;
9c06b0b4 9442 }
77b06cd7 9443
c906108c
SS
9444 if (cond_start)
9445 b->cond_string = savestring (cond_start, cond_end - cond_start);
9446 else
9447 b->cond_string = 0;
c5aa993b 9448
c906108c 9449 if (frame)
f6bc2008 9450 {
3a5c3e22
PA
9451 w->watchpoint_frame = get_frame_id (frame);
9452 w->watchpoint_thread = inferior_ptid;
f6bc2008 9453 }
c906108c 9454 else
f6bc2008 9455 {
3a5c3e22
PA
9456 w->watchpoint_frame = null_frame_id;
9457 w->watchpoint_thread = null_ptid;
f6bc2008 9458 }
c906108c 9459
d983da9c 9460 if (scope_breakpoint != NULL)
c906108c 9461 {
d983da9c
DJ
9462 /* The scope breakpoint is related to the watchpoint. We will
9463 need to act on them together. */
9464 b->related_breakpoint = scope_breakpoint;
9465 scope_breakpoint->related_breakpoint = b;
c906108c 9466 }
d983da9c 9467
06a64a0b
TT
9468 if (!just_location)
9469 value_free_to_mark (mark);
2d134ed3 9470
a9634178
TJB
9471 TRY_CATCH (e, RETURN_MASK_ALL)
9472 {
9473 /* Finally update the new watchpoint. This creates the locations
9474 that should be inserted. */
3a5c3e22 9475 update_watchpoint (w, 1);
a9634178
TJB
9476 }
9477 if (e.reason < 0)
9478 {
9479 delete_breakpoint (b);
9480 throw_exception (e);
9481 }
9482
3ea46bff 9483 install_breakpoint (internal, b, 1);
c906108c
SS
9484}
9485
e09342b5 9486/* Return count of debug registers needed to watch the given expression.
e09342b5 9487 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9488
c906108c 9489static int
a9634178 9490can_use_hardware_watchpoint (struct value *v)
c906108c
SS
9491{
9492 int found_memory_cnt = 0;
2e70b7b9 9493 struct value *head = v;
c906108c
SS
9494
9495 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9496 if (!can_use_hw_watchpoints)
c906108c 9497 return 0;
c5aa993b 9498
5c44784c
JM
9499 /* Make sure that the value of the expression depends only upon
9500 memory contents, and values computed from them within GDB. If we
9501 find any register references or function calls, we can't use a
9502 hardware watchpoint.
9503
9504 The idea here is that evaluating an expression generates a series
9505 of values, one holding the value of every subexpression. (The
9506 expression a*b+c has five subexpressions: a, b, a*b, c, and
9507 a*b+c.) GDB's values hold almost enough information to establish
9508 the criteria given above --- they identify memory lvalues,
9509 register lvalues, computed values, etcetera. So we can evaluate
9510 the expression, and then scan the chain of values that leaves
9511 behind to decide whether we can detect any possible change to the
9512 expression's final value using only hardware watchpoints.
9513
9514 However, I don't think that the values returned by inferior
9515 function calls are special in any way. So this function may not
9516 notice that an expression involving an inferior function call
9517 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9518 for (; v; v = value_next (v))
c906108c 9519 {
5c44784c 9520 if (VALUE_LVAL (v) == lval_memory)
c906108c 9521 {
8464be76
DJ
9522 if (v != head && value_lazy (v))
9523 /* A lazy memory lvalue in the chain is one that GDB never
9524 needed to fetch; we either just used its address (e.g.,
9525 `a' in `a.b') or we never needed it at all (e.g., `a'
9526 in `a,b'). This doesn't apply to HEAD; if that is
9527 lazy then it was not readable, but watch it anyway. */
5c44784c 9528 ;
53a5351d 9529 else
5c44784c
JM
9530 {
9531 /* Ahh, memory we actually used! Check if we can cover
9532 it with hardware watchpoints. */
df407dfe 9533 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9534
9535 /* We only watch structs and arrays if user asked for it
9536 explicitly, never if they just happen to appear in a
9537 middle of some value chain. */
9538 if (v == head
9539 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9540 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9541 {
42ae5230 9542 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9543 int len;
9544 int num_regs;
9545
a9634178 9546 len = (target_exact_watchpoints
e09342b5
TJB
9547 && is_scalar_type_recursive (vtype))?
9548 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9549
e09342b5
TJB
9550 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9551 if (!num_regs)
2e70b7b9
MS
9552 return 0;
9553 else
e09342b5 9554 found_memory_cnt += num_regs;
2e70b7b9 9555 }
5c44784c 9556 }
c5aa993b 9557 }
5086187c
AC
9558 else if (VALUE_LVAL (v) != not_lval
9559 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9560 return 0; /* These are values from the history (e.g., $1). */
5086187c 9561 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9562 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9563 }
9564
9565 /* The expression itself looks suitable for using a hardware
9566 watchpoint, but give the target machine a chance to reject it. */
9567 return found_memory_cnt;
9568}
9569
8b93c638 9570void
84f4c1fe 9571watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9572{
84f4c1fe 9573 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9574}
9575
9576/* A helper function that looks for an argument at the start of a
9577 string. The argument must also either be at the end of the string,
9578 or be followed by whitespace. Returns 1 if it finds the argument,
9579 0 otherwise. If the argument is found, it updates *STR. */
9580
9581static int
9582check_for_argument (char **str, char *arg, int arg_len)
9583{
9584 if (strncmp (*str, arg, arg_len) == 0
9585 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9586 {
9587 *str += arg_len;
9588 return 1;
9589 }
9590 return 0;
9591}
9592
9593/* A helper function that looks for the "-location" argument and then
9594 calls watch_command_1. */
9595
9596static void
9597watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9598{
9599 int just_location = 0;
9600
9601 if (arg
9602 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9603 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9604 {
e9cafbcc 9605 arg = skip_spaces (arg);
06a64a0b
TT
9606 just_location = 1;
9607 }
9608
84f4c1fe 9609 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9610}
8926118c 9611
c5aa993b 9612static void
fba45db2 9613watch_command (char *arg, int from_tty)
c906108c 9614{
06a64a0b 9615 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9616}
9617
8b93c638 9618void
84f4c1fe 9619rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9620{
84f4c1fe 9621 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9622}
8926118c 9623
c5aa993b 9624static void
fba45db2 9625rwatch_command (char *arg, int from_tty)
c906108c 9626{
06a64a0b 9627 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9628}
9629
8b93c638 9630void
84f4c1fe 9631awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9632{
84f4c1fe 9633 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9634}
8926118c 9635
c5aa993b 9636static void
fba45db2 9637awatch_command (char *arg, int from_tty)
c906108c 9638{
06a64a0b 9639 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9640}
c906108c 9641\f
c5aa993b 9642
43ff13b4 9643/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9644 because it uses the mechanisms of breakpoints. */
9645
bfec99b2
PA
9646struct until_break_command_continuation_args
9647{
9648 struct breakpoint *breakpoint;
9649 struct breakpoint *breakpoint2;
186c406b 9650 int thread_num;
bfec99b2
PA
9651};
9652
43ff13b4 9653/* This function is called by fetch_inferior_event via the
4a64f543 9654 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9655 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9656 command. */
c2c6d25f 9657static void
fa4cd53f 9658until_break_command_continuation (void *arg, int err)
43ff13b4 9659{
bfec99b2
PA
9660 struct until_break_command_continuation_args *a = arg;
9661
9662 delete_breakpoint (a->breakpoint);
9663 if (a->breakpoint2)
9664 delete_breakpoint (a->breakpoint2);
186c406b 9665 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9666}
9667
c906108c 9668void
ae66c1fc 9669until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9670{
9671 struct symtabs_and_lines sals;
9672 struct symtab_and_line sal;
206415a3 9673 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9674 struct breakpoint *breakpoint;
f107f563 9675 struct breakpoint *breakpoint2 = NULL;
c906108c 9676 struct cleanup *old_chain;
186c406b
TT
9677 int thread;
9678 struct thread_info *tp;
c906108c
SS
9679
9680 clear_proceed_status ();
9681
9682 /* Set a breakpoint where the user wants it and at return from
4a64f543 9683 this function. */
c5aa993b 9684
1bfeeb0f
JL
9685 if (last_displayed_sal_is_valid ())
9686 sals = decode_line_1 (&arg, 1,
9687 get_last_displayed_symtab (),
9688 get_last_displayed_line (),
9689 NULL);
c906108c 9690 else
58438ac1 9691 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, NULL);
c5aa993b 9692
c906108c 9693 if (sals.nelts != 1)
8a3fe4f8 9694 error (_("Couldn't get information on specified line."));
c5aa993b 9695
c906108c 9696 sal = sals.sals[0];
4a64f543 9697 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9698
c906108c 9699 if (*arg)
8a3fe4f8 9700 error (_("Junk at end of arguments."));
c5aa993b 9701
c906108c 9702 resolve_sal_pc (&sal);
c5aa993b 9703
ae66c1fc
EZ
9704 if (anywhere)
9705 /* If the user told us to continue until a specified location,
9706 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9707 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9708 null_frame_id, bp_until);
ae66c1fc 9709 else
4a64f543
MS
9710 /* Otherwise, specify the selected frame, because we want to stop
9711 only at the very same frame. */
a6d9a66e
UW
9712 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9713 get_stack_frame_id (frame),
ae66c1fc 9714 bp_until);
c5aa993b 9715
f107f563 9716 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9717
186c406b
TT
9718 tp = inferior_thread ();
9719 thread = tp->num;
9720
ae66c1fc
EZ
9721 /* Keep within the current frame, or in frames called by the current
9722 one. */
edb3359d
DJ
9723
9724 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9725 {
edb3359d
DJ
9726 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9727 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9728 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9729 sal,
edb3359d 9730 frame_unwind_caller_id (frame),
f107f563
VP
9731 bp_until);
9732 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9733
9734 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9735 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9736 }
c5aa993b 9737
c906108c 9738 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9739
4a64f543
MS
9740 /* If we are running asynchronously, and proceed call above has
9741 actually managed to start the target, arrange for breakpoints to
9742 be deleted when the target stops. Otherwise, we're already
9743 stopped and delete breakpoints via cleanup chain. */
f107f563 9744
8ea051c5 9745 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9746 {
bfec99b2
PA
9747 struct until_break_command_continuation_args *args;
9748 args = xmalloc (sizeof (*args));
f107f563 9749
bfec99b2
PA
9750 args->breakpoint = breakpoint;
9751 args->breakpoint2 = breakpoint2;
186c406b 9752 args->thread_num = thread;
f107f563
VP
9753
9754 discard_cleanups (old_chain);
95e54da7
PA
9755 add_continuation (inferior_thread (),
9756 until_break_command_continuation, args,
604ead4a 9757 xfree);
f107f563
VP
9758 }
9759 else
c5aa993b 9760 do_cleanups (old_chain);
c906108c 9761}
ae66c1fc 9762
c906108c
SS
9763/* This function attempts to parse an optional "if <cond>" clause
9764 from the arg string. If one is not found, it returns NULL.
c5aa993b 9765
c906108c
SS
9766 Else, it returns a pointer to the condition string. (It does not
9767 attempt to evaluate the string against a particular block.) And,
9768 it updates arg to point to the first character following the parsed
4a64f543 9769 if clause in the arg string. */
53a5351d 9770
c906108c 9771static char *
fba45db2 9772ep_parse_optional_if_clause (char **arg)
c906108c 9773{
c5aa993b
JM
9774 char *cond_string;
9775
9776 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9777 return NULL;
c5aa993b 9778
4a64f543 9779 /* Skip the "if" keyword. */
c906108c 9780 (*arg) += 2;
c5aa993b 9781
c906108c 9782 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9783 condition string. */
e9cafbcc 9784 *arg = skip_spaces (*arg);
c906108c 9785 cond_string = *arg;
c5aa993b 9786
4a64f543
MS
9787 /* Assume that the condition occupies the remainder of the arg
9788 string. */
c906108c 9789 (*arg) += strlen (cond_string);
c5aa993b 9790
c906108c
SS
9791 return cond_string;
9792}
c5aa993b 9793
c906108c
SS
9794/* Commands to deal with catching events, such as signals, exceptions,
9795 process start/exit, etc. */
c5aa993b
JM
9796
9797typedef enum
9798{
44feb3ce
TT
9799 catch_fork_temporary, catch_vfork_temporary,
9800 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9801}
9802catch_fork_kind;
9803
c906108c 9804static void
cc59ec59
MS
9805catch_fork_command_1 (char *arg, int from_tty,
9806 struct cmd_list_element *command)
c906108c 9807{
a6d9a66e 9808 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9809 char *cond_string = NULL;
44feb3ce
TT
9810 catch_fork_kind fork_kind;
9811 int tempflag;
9812
9813 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9814 tempflag = (fork_kind == catch_fork_temporary
9815 || fork_kind == catch_vfork_temporary);
c5aa993b 9816
44feb3ce
TT
9817 if (!arg)
9818 arg = "";
e9cafbcc 9819 arg = skip_spaces (arg);
c5aa993b 9820
c906108c 9821 /* The allowed syntax is:
c5aa993b
JM
9822 catch [v]fork
9823 catch [v]fork if <cond>
9824
4a64f543 9825 First, check if there's an if clause. */
c906108c 9826 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9827
c906108c 9828 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9829 error (_("Junk at end of arguments."));
c5aa993b 9830
c906108c 9831 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9832 and enable reporting of such events. */
c5aa993b
JM
9833 switch (fork_kind)
9834 {
44feb3ce
TT
9835 case catch_fork_temporary:
9836 case catch_fork_permanent:
a6d9a66e 9837 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9838 &catch_fork_breakpoint_ops);
c906108c 9839 break;
44feb3ce
TT
9840 case catch_vfork_temporary:
9841 case catch_vfork_permanent:
a6d9a66e 9842 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9843 &catch_vfork_breakpoint_ops);
c906108c 9844 break;
c5aa993b 9845 default:
8a3fe4f8 9846 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9847 break;
c5aa993b 9848 }
c906108c
SS
9849}
9850
9851static void
cc59ec59
MS
9852catch_exec_command_1 (char *arg, int from_tty,
9853 struct cmd_list_element *command)
c906108c 9854{
b4d90040 9855 struct exec_catchpoint *c;
a6d9a66e 9856 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9857 int tempflag;
c5aa993b 9858 char *cond_string = NULL;
c906108c 9859
44feb3ce
TT
9860 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9861
9862 if (!arg)
9863 arg = "";
e9cafbcc 9864 arg = skip_spaces (arg);
c906108c
SS
9865
9866 /* The allowed syntax is:
c5aa993b
JM
9867 catch exec
9868 catch exec if <cond>
c906108c 9869
4a64f543 9870 First, check if there's an if clause. */
c906108c
SS
9871 cond_string = ep_parse_optional_if_clause (&arg);
9872
9873 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9874 error (_("Junk at end of arguments."));
c906108c 9875
b4d90040
PA
9876 c = XNEW (struct exec_catchpoint);
9877 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
9878 &catch_exec_breakpoint_ops);
9879 c->exec_pathname = NULL;
9880
3ea46bff 9881 install_breakpoint (0, &c->base, 1);
c906108c 9882}
c5aa993b 9883
3086aeae 9884static enum print_stop_action
348d480f 9885print_it_exception_catchpoint (bpstat bs)
3086aeae 9886{
79a45e25 9887 struct ui_out *uiout = current_uiout;
348d480f 9888 struct breakpoint *b = bs->breakpoint_at;
ade92717 9889 int bp_temp, bp_throw;
3086aeae 9890
ade92717 9891 annotate_catchpoint (b->number);
3086aeae 9892
ade92717
AR
9893 bp_throw = strstr (b->addr_string, "throw") != NULL;
9894 if (b->loc->address != b->loc->requested_address)
9895 breakpoint_adjustment_warning (b->loc->requested_address,
9896 b->loc->address,
9897 b->number, 1);
df2b6d2d 9898 bp_temp = b->disposition == disp_del;
ade92717
AR
9899 ui_out_text (uiout,
9900 bp_temp ? "Temporary catchpoint "
9901 : "Catchpoint ");
9902 if (!ui_out_is_mi_like_p (uiout))
9903 ui_out_field_int (uiout, "bkptno", b->number);
9904 ui_out_text (uiout,
c0b37c48
AR
9905 bp_throw ? " (exception thrown), "
9906 : " (exception caught), ");
ade92717
AR
9907 if (ui_out_is_mi_like_p (uiout))
9908 {
9909 ui_out_field_string (uiout, "reason",
9910 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9911 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9912 ui_out_field_int (uiout, "bkptno", b->number);
9913 }
3086aeae
DJ
9914 return PRINT_SRC_AND_LOC;
9915}
9916
9917static void
cc59ec59
MS
9918print_one_exception_catchpoint (struct breakpoint *b,
9919 struct bp_location **last_loc)
3086aeae 9920{
79a45b7d 9921 struct value_print_options opts;
79a45e25 9922 struct ui_out *uiout = current_uiout;
cc59ec59 9923
79a45b7d
TT
9924 get_user_print_options (&opts);
9925 if (opts.addressprint)
3086aeae
DJ
9926 {
9927 annotate_field (4);
604133b5
AR
9928 if (b->loc == NULL || b->loc->shlib_disabled)
9929 ui_out_field_string (uiout, "addr", "<PENDING>");
9930 else
5af949e3
UW
9931 ui_out_field_core_addr (uiout, "addr",
9932 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9933 }
9934 annotate_field (5);
604133b5 9935 if (b->loc)
a6d9a66e 9936 *last_loc = b->loc;
3086aeae
DJ
9937 if (strstr (b->addr_string, "throw") != NULL)
9938 ui_out_field_string (uiout, "what", "exception throw");
9939 else
9940 ui_out_field_string (uiout, "what", "exception catch");
9941}
9942
9943static void
9944print_mention_exception_catchpoint (struct breakpoint *b)
9945{
79a45e25 9946 struct ui_out *uiout = current_uiout;
ade92717
AR
9947 int bp_temp;
9948 int bp_throw;
9949
df2b6d2d 9950 bp_temp = b->disposition == disp_del;
ade92717
AR
9951 bp_throw = strstr (b->addr_string, "throw") != NULL;
9952 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9953 : _("Catchpoint "));
9954 ui_out_field_int (uiout, "bkptno", b->number);
9955 ui_out_text (uiout, bp_throw ? _(" (throw)")
9956 : _(" (catch)"));
3086aeae
DJ
9957}
9958
6149aea9
PA
9959/* Implement the "print_recreate" breakpoint_ops method for throw and
9960 catch catchpoints. */
9961
9962static void
4a64f543
MS
9963print_recreate_exception_catchpoint (struct breakpoint *b,
9964 struct ui_file *fp)
6149aea9
PA
9965{
9966 int bp_temp;
9967 int bp_throw;
9968
9969 bp_temp = b->disposition == disp_del;
9970 bp_throw = strstr (b->addr_string, "throw") != NULL;
9971 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9972 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 9973 print_recreate_thread (b, fp);
6149aea9
PA
9974}
9975
2060206e 9976static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
9977
9978static int
9979handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9980 enum exception_event_kind ex_event, int from_tty)
9981{
604133b5
AR
9982 char *trigger_func_name;
9983
3086aeae 9984 if (ex_event == EX_EVENT_CATCH)
604133b5 9985 trigger_func_name = "__cxa_begin_catch";
3086aeae 9986 else
604133b5 9987 trigger_func_name = "__cxa_throw";
3086aeae 9988
8cdf0e15
VP
9989 create_breakpoint (get_current_arch (),
9990 trigger_func_name, cond_string, -1,
9991 0 /* condition and thread are valid. */,
0fb4aa4b 9992 tempflag, bp_breakpoint,
8cdf0e15
VP
9993 0,
9994 AUTO_BOOLEAN_TRUE /* pending */,
9995 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9996 1 /* enabled */,
9997 0 /* internal */);
3086aeae 9998
3086aeae
DJ
9999 return 1;
10000}
10001
4a64f543 10002/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
10003
10004static void
fba45db2
KB
10005catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
10006 int tempflag, int from_tty)
c906108c 10007{
c5aa993b 10008 char *cond_string = NULL;
c5aa993b 10009
44feb3ce
TT
10010 if (!arg)
10011 arg = "";
e9cafbcc 10012 arg = skip_spaces (arg);
c5aa993b 10013
c906108c
SS
10014 cond_string = ep_parse_optional_if_clause (&arg);
10015
10016 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 10017 error (_("Junk at end of arguments."));
c906108c 10018
059fb39f
PM
10019 if (ex_event != EX_EVENT_THROW
10020 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 10021 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 10022
3086aeae
DJ
10023 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
10024 return;
10025
8a3fe4f8 10026 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
10027}
10028
44feb3ce
TT
10029/* Implementation of "catch catch" command. */
10030
10031static void
10032catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
10033{
10034 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 10035
44feb3ce
TT
10036 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
10037}
10038
10039/* Implementation of "catch throw" command. */
10040
10041static void
10042catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
10043{
10044 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 10045
44feb3ce
TT
10046 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
10047}
10048
9ac4176b 10049void
28010a5d
PA
10050init_ada_exception_breakpoint (struct breakpoint *b,
10051 struct gdbarch *gdbarch,
10052 struct symtab_and_line sal,
10053 char *addr_string,
c0a91b2b 10054 const struct breakpoint_ops *ops,
28010a5d
PA
10055 int tempflag,
10056 int from_tty)
f7f9143b 10057{
f7f9143b
JB
10058 if (from_tty)
10059 {
5af949e3
UW
10060 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
10061 if (!loc_gdbarch)
10062 loc_gdbarch = gdbarch;
10063
6c95b8df
PA
10064 describe_other_breakpoints (loc_gdbarch,
10065 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
10066 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
10067 version for exception catchpoints, because two catchpoints
10068 used for different exception names will use the same address.
10069 In this case, a "breakpoint ... also set at..." warning is
4a64f543 10070 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 10071 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
10072 the user what type of catchpoint it is. The above is good
10073 enough for now, though. */
10074 }
10075
28010a5d 10076 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
10077
10078 b->enable_state = bp_enabled;
10079 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
10080 b->addr_string = addr_string;
10081 b->language = language_ada;
f7f9143b
JB
10082}
10083
a96d9b2e
SDJ
10084/* Splits the argument using space as delimiter. Returns an xmalloc'd
10085 filter list, or NULL if no filtering is required. */
10086static VEC(int) *
10087catch_syscall_split_args (char *arg)
10088{
10089 VEC(int) *result = NULL;
29d0bb3d 10090 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
10091
10092 while (*arg != '\0')
10093 {
10094 int i, syscall_number;
10095 char *endptr;
10096 char cur_name[128];
10097 struct syscall s;
10098
10099 /* Skip whitespace. */
10100 while (isspace (*arg))
10101 arg++;
10102
10103 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
10104 cur_name[i] = arg[i];
10105 cur_name[i] = '\0';
10106 arg += i;
10107
10108 /* Check if the user provided a syscall name or a number. */
10109 syscall_number = (int) strtol (cur_name, &endptr, 0);
10110 if (*endptr == '\0')
bccd0dd2 10111 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
10112 else
10113 {
10114 /* We have a name. Let's check if it's valid and convert it
10115 to a number. */
10116 get_syscall_by_name (cur_name, &s);
10117
10118 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
10119 /* Here we have to issue an error instead of a warning,
10120 because GDB cannot do anything useful if there's no
10121 syscall number to be caught. */
a96d9b2e
SDJ
10122 error (_("Unknown syscall name '%s'."), cur_name);
10123 }
10124
10125 /* Ok, it's valid. */
10126 VEC_safe_push (int, result, s.number);
10127 }
10128
10129 discard_cleanups (cleanup);
10130 return result;
10131}
10132
10133/* Implement the "catch syscall" command. */
10134
10135static void
cc59ec59
MS
10136catch_syscall_command_1 (char *arg, int from_tty,
10137 struct cmd_list_element *command)
a96d9b2e
SDJ
10138{
10139 int tempflag;
10140 VEC(int) *filter;
10141 struct syscall s;
10142 struct gdbarch *gdbarch = get_current_arch ();
10143
10144 /* Checking if the feature if supported. */
10145 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
10146 error (_("The feature 'catch syscall' is not supported on \
ea666128 10147this architecture yet."));
a96d9b2e
SDJ
10148
10149 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
10150
e9cafbcc 10151 arg = skip_spaces (arg);
a96d9b2e
SDJ
10152
10153 /* We need to do this first "dummy" translation in order
10154 to get the syscall XML file loaded or, most important,
10155 to display a warning to the user if there's no XML file
10156 for his/her architecture. */
10157 get_syscall_by_number (0, &s);
10158
10159 /* The allowed syntax is:
10160 catch syscall
10161 catch syscall <name | number> [<name | number> ... <name | number>]
10162
10163 Let's check if there's a syscall name. */
10164
10165 if (arg != NULL)
10166 filter = catch_syscall_split_args (arg);
10167 else
10168 filter = NULL;
10169
10170 create_syscall_event_catchpoint (tempflag, filter,
10171 &catch_syscall_breakpoint_ops);
10172}
10173
c906108c 10174static void
fba45db2 10175catch_command (char *arg, int from_tty)
c906108c 10176{
44feb3ce 10177 error (_("Catch requires an event name."));
c906108c
SS
10178}
10179\f
10180
10181static void
fba45db2 10182tcatch_command (char *arg, int from_tty)
c906108c 10183{
44feb3ce 10184 error (_("Catch requires an event name."));
c906108c
SS
10185}
10186
80f8a6eb 10187/* Delete breakpoints by address or line. */
c906108c
SS
10188
10189static void
fba45db2 10190clear_command (char *arg, int from_tty)
c906108c 10191{
d6e956e5
VP
10192 struct breakpoint *b;
10193 VEC(breakpoint_p) *found = 0;
10194 int ix;
c906108c
SS
10195 int default_match;
10196 struct symtabs_and_lines sals;
10197 struct symtab_and_line sal;
c906108c
SS
10198 int i;
10199
10200 if (arg)
10201 {
10202 sals = decode_line_spec (arg, 1);
10203 default_match = 0;
10204 }
10205 else
10206 {
c5aa993b 10207 sals.sals = (struct symtab_and_line *)
c906108c 10208 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 10209 make_cleanup (xfree, sals.sals);
4a64f543 10210 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
10211
10212 /* Set sal's line, symtab, pc, and pspace to the values
10213 corresponding to the last call to print_frame_info. If the
10214 codepoint is not valid, this will set all the fields to 0. */
10215 get_last_displayed_sal (&sal);
c906108c 10216 if (sal.symtab == 0)
8a3fe4f8 10217 error (_("No source file specified."));
c906108c
SS
10218
10219 sals.sals[0] = sal;
10220 sals.nelts = 1;
10221
10222 default_match = 1;
10223 }
10224
4a64f543
MS
10225 /* We don't call resolve_sal_pc here. That's not as bad as it
10226 seems, because all existing breakpoints typically have both
10227 file/line and pc set. So, if clear is given file/line, we can
10228 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10229
10230 We only support clearing given the address explicitly
10231 present in breakpoint table. Say, we've set breakpoint
4a64f543 10232 at file:line. There were several PC values for that file:line,
ed0616c6 10233 due to optimization, all in one block.
4a64f543
MS
10234
10235 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10236 PC corresponding to the same file:line, the breakpoint won't
10237 be cleared. We probably can still clear the breakpoint, but
10238 since the other PC value is never presented to user, user
10239 can only find it by guessing, and it does not seem important
10240 to support that. */
10241
4a64f543
MS
10242 /* For each line spec given, delete bps which correspond to it. Do
10243 it in two passes, solely to preserve the current behavior that
10244 from_tty is forced true if we delete more than one
10245 breakpoint. */
c906108c 10246
80f8a6eb 10247 found = NULL;
c906108c
SS
10248 for (i = 0; i < sals.nelts; i++)
10249 {
10250 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
10251 If line given (pc == 0), clear all bpts on specified line.
10252 If defaulting, clear all bpts on default line
c906108c 10253 or at default pc.
c5aa993b
JM
10254
10255 defaulting sal.pc != 0 tests to do
10256
10257 0 1 pc
10258 1 1 pc _and_ line
10259 0 0 line
10260 1 0 <can't happen> */
c906108c
SS
10261
10262 sal = sals.sals[i];
c906108c 10263
4a64f543 10264 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 10265 ALL_BREAKPOINTS (b)
c5aa993b 10266 {
0d381245 10267 int match = 0;
4a64f543 10268 /* Are we going to delete b? */
cc60f2e3 10269 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
10270 {
10271 struct bp_location *loc = b->loc;
10272 for (; loc; loc = loc->next)
10273 {
6c95b8df
PA
10274 int pc_match = sal.pc
10275 && (loc->pspace == sal.pspace)
0d381245
VP
10276 && (loc->address == sal.pc)
10277 && (!section_is_overlay (loc->section)
10278 || loc->section == sal.section);
10279 int line_match = ((default_match || (0 == sal.pc))
10280 && b->source_file != NULL
10281 && sal.symtab != NULL
6c95b8df 10282 && sal.pspace == loc->pspace
0ba1096a
KT
10283 && filename_cmp (b->source_file,
10284 sal.symtab->filename) == 0
0d381245
VP
10285 && b->line_number == sal.line);
10286 if (pc_match || line_match)
10287 {
10288 match = 1;
10289 break;
10290 }
10291 }
10292 }
10293
10294 if (match)
d6e956e5 10295 VEC_safe_push(breakpoint_p, found, b);
c906108c 10296 }
80f8a6eb
MS
10297 }
10298 /* Now go thru the 'found' chain and delete them. */
d6e956e5 10299 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
10300 {
10301 if (arg)
8a3fe4f8 10302 error (_("No breakpoint at %s."), arg);
80f8a6eb 10303 else
8a3fe4f8 10304 error (_("No breakpoint at this line."));
80f8a6eb 10305 }
c906108c 10306
d6e956e5 10307 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 10308 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10309 if (from_tty)
a3f17187 10310 {
d6e956e5 10311 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
10312 printf_unfiltered (_("Deleted breakpoint "));
10313 else
10314 printf_unfiltered (_("Deleted breakpoints "));
10315 }
80f8a6eb 10316 breakpoints_changed ();
d6e956e5
VP
10317
10318 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 10319 {
c5aa993b 10320 if (from_tty)
d6e956e5
VP
10321 printf_unfiltered ("%d ", b->number);
10322 delete_breakpoint (b);
c906108c 10323 }
80f8a6eb
MS
10324 if (from_tty)
10325 putchar_unfiltered ('\n');
c906108c
SS
10326}
10327\f
10328/* Delete breakpoint in BS if they are `delete' breakpoints and
10329 all breakpoints that are marked for deletion, whether hit or not.
10330 This is called after any breakpoint is hit, or after errors. */
10331
10332void
fba45db2 10333breakpoint_auto_delete (bpstat bs)
c906108c 10334{
35df4500 10335 struct breakpoint *b, *b_tmp;
c906108c
SS
10336
10337 for (; bs; bs = bs->next)
f431efe5
PA
10338 if (bs->breakpoint_at
10339 && bs->breakpoint_at->disposition == disp_del
c906108c 10340 && bs->stop)
f431efe5 10341 delete_breakpoint (bs->breakpoint_at);
c906108c 10342
35df4500 10343 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10344 {
b5de0fa7 10345 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10346 delete_breakpoint (b);
10347 }
c906108c
SS
10348}
10349
4a64f543
MS
10350/* A comparison function for bp_location AP and BP being interfaced to
10351 qsort. Sort elements primarily by their ADDRESS (no matter what
10352 does breakpoint_address_is_meaningful say for its OWNER),
10353 secondarily by ordering first bp_permanent OWNERed elements and
10354 terciarily just ensuring the array is sorted stable way despite
e5dd4106 10355 qsort being an unstable algorithm. */
876fa593
JK
10356
10357static int
494cfb0f 10358bp_location_compare (const void *ap, const void *bp)
876fa593 10359{
494cfb0f
JK
10360 struct bp_location *a = *(void **) ap;
10361 struct bp_location *b = *(void **) bp;
2bdf28a0 10362 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10363 int a_perm = a->owner->enable_state == bp_permanent;
10364 int b_perm = b->owner->enable_state == bp_permanent;
10365
10366 if (a->address != b->address)
10367 return (a->address > b->address) - (a->address < b->address);
10368
10369 /* Sort permanent breakpoints first. */
10370 if (a_perm != b_perm)
10371 return (a_perm < b_perm) - (a_perm > b_perm);
10372
4a64f543
MS
10373 /* Make the user-visible order stable across GDB runs. Locations of
10374 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10375
10376 if (a->owner->number != b->owner->number)
10377 return (a->owner->number > b->owner->number)
10378 - (a->owner->number < b->owner->number);
10379
10380 return (a > b) - (a < b);
10381}
10382
876fa593 10383/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10384 bp_location_shadow_len_after_address_max according to the current
10385 content of the bp_location array. */
f7545552
TT
10386
10387static void
876fa593 10388bp_location_target_extensions_update (void)
f7545552 10389{
876fa593
JK
10390 struct bp_location *bl, **blp_tmp;
10391
10392 bp_location_placed_address_before_address_max = 0;
10393 bp_location_shadow_len_after_address_max = 0;
10394
10395 ALL_BP_LOCATIONS (bl, blp_tmp)
10396 {
10397 CORE_ADDR start, end, addr;
10398
10399 if (!bp_location_has_shadow (bl))
10400 continue;
10401
10402 start = bl->target_info.placed_address;
10403 end = start + bl->target_info.shadow_len;
10404
10405 gdb_assert (bl->address >= start);
10406 addr = bl->address - start;
10407 if (addr > bp_location_placed_address_before_address_max)
10408 bp_location_placed_address_before_address_max = addr;
10409
10410 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10411
10412 gdb_assert (bl->address < end);
10413 addr = end - bl->address;
10414 if (addr > bp_location_shadow_len_after_address_max)
10415 bp_location_shadow_len_after_address_max = addr;
10416 }
f7545552
TT
10417}
10418
1e4d1764
YQ
10419/* Download tracepoint locations if they haven't been. */
10420
10421static void
10422download_tracepoint_locations (void)
10423{
10424 struct bp_location *bl, **blp_tmp;
10425 struct cleanup *old_chain;
10426
10427 if (!target_can_download_tracepoint ())
10428 return;
10429
10430 old_chain = save_current_space_and_thread ();
10431
10432 ALL_BP_LOCATIONS (bl, blp_tmp)
10433 {
10434 struct tracepoint *t;
10435
10436 if (!is_tracepoint (bl->owner))
10437 continue;
10438
10439 if ((bl->owner->type == bp_fast_tracepoint
10440 ? !may_insert_fast_tracepoints
10441 : !may_insert_tracepoints))
10442 continue;
10443
10444 /* In tracepoint, locations are _never_ duplicated, so
10445 should_be_inserted is equivalent to
10446 unduplicated_should_be_inserted. */
10447 if (!should_be_inserted (bl) || bl->inserted)
10448 continue;
10449
10450 switch_to_program_space_and_thread (bl->pspace);
10451
10452 target_download_tracepoint (bl);
10453
10454 bl->inserted = 1;
10455 t = (struct tracepoint *) bl->owner;
10456 t->number_on_target = bl->owner->number;
10457 }
10458
10459 do_cleanups (old_chain);
10460}
10461
934709f0
PW
10462/* Swap the insertion/duplication state between two locations. */
10463
10464static void
10465swap_insertion (struct bp_location *left, struct bp_location *right)
10466{
10467 const int left_inserted = left->inserted;
10468 const int left_duplicate = left->duplicate;
10469 const struct bp_target_info left_target_info = left->target_info;
10470
1e4d1764
YQ
10471 /* Locations of tracepoints can never be duplicated. */
10472 if (is_tracepoint (left->owner))
10473 gdb_assert (!left->duplicate);
10474 if (is_tracepoint (right->owner))
10475 gdb_assert (!right->duplicate);
10476
934709f0
PW
10477 left->inserted = right->inserted;
10478 left->duplicate = right->duplicate;
10479 left->target_info = right->target_info;
10480 right->inserted = left_inserted;
10481 right->duplicate = left_duplicate;
10482 right->target_info = left_target_info;
10483}
10484
4cd9bd08 10485/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10486 into the inferior, only remove already-inserted locations that no
10487 longer should be inserted. Functions that delete a breakpoint or
10488 breakpoints should pass false, so that deleting a breakpoint
10489 doesn't have the side effect of inserting the locations of other
10490 breakpoints that are marked not-inserted, but should_be_inserted
10491 returns true on them.
10492
10493 This behaviour is useful is situations close to tear-down -- e.g.,
10494 after an exec, while the target still has execution, but breakpoint
10495 shadows of the previous executable image should *NOT* be restored
10496 to the new image; or before detaching, where the target still has
10497 execution and wants to delete breakpoints from GDB's lists, and all
10498 breakpoints had already been removed from the inferior. */
10499
0d381245 10500static void
b60e7edf 10501update_global_location_list (int should_insert)
0d381245 10502{
74960c60 10503 struct breakpoint *b;
876fa593 10504 struct bp_location **locp, *loc;
f7545552
TT
10505 struct cleanup *cleanups;
10506
2d134ed3
PA
10507 /* Used in the duplicates detection below. When iterating over all
10508 bp_locations, points to the first bp_location of a given address.
10509 Breakpoints and watchpoints of different types are never
10510 duplicates of each other. Keep one pointer for each type of
10511 breakpoint/watchpoint, so we only need to loop over all locations
10512 once. */
10513 struct bp_location *bp_loc_first; /* breakpoint */
10514 struct bp_location *wp_loc_first; /* hardware watchpoint */
10515 struct bp_location *awp_loc_first; /* access watchpoint */
10516 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10517
4a64f543
MS
10518 /* Saved former bp_location array which we compare against the newly
10519 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10520 struct bp_location **old_location, **old_locp;
10521 unsigned old_location_count;
10522
10523 old_location = bp_location;
10524 old_location_count = bp_location_count;
10525 bp_location = NULL;
10526 bp_location_count = 0;
10527 cleanups = make_cleanup (xfree, old_location);
0d381245 10528
74960c60 10529 ALL_BREAKPOINTS (b)
876fa593
JK
10530 for (loc = b->loc; loc; loc = loc->next)
10531 bp_location_count++;
10532
10533 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10534 locp = bp_location;
10535 ALL_BREAKPOINTS (b)
10536 for (loc = b->loc; loc; loc = loc->next)
10537 *locp++ = loc;
10538 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10539 bp_location_compare);
876fa593
JK
10540
10541 bp_location_target_extensions_update ();
74960c60 10542
4a64f543
MS
10543 /* Identify bp_location instances that are no longer present in the
10544 new list, and therefore should be freed. Note that it's not
10545 necessary that those locations should be removed from inferior --
10546 if there's another location at the same address (previously
10547 marked as duplicate), we don't need to remove/insert the
10548 location.
876fa593 10549
4a64f543
MS
10550 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10551 and former bp_location array state respectively. */
876fa593
JK
10552
10553 locp = bp_location;
10554 for (old_locp = old_location; old_locp < old_location + old_location_count;
10555 old_locp++)
74960c60 10556 {
876fa593 10557 struct bp_location *old_loc = *old_locp;
c7d46a38 10558 struct bp_location **loc2p;
876fa593 10559
e5dd4106 10560 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 10561 not, we have to free it. */
c7d46a38 10562 int found_object = 0;
20874c92
VP
10563 /* Tells if the location should remain inserted in the target. */
10564 int keep_in_target = 0;
10565 int removed = 0;
876fa593 10566
4a64f543
MS
10567 /* Skip LOCP entries which will definitely never be needed.
10568 Stop either at or being the one matching OLD_LOC. */
876fa593 10569 while (locp < bp_location + bp_location_count
c7d46a38 10570 && (*locp)->address < old_loc->address)
876fa593 10571 locp++;
c7d46a38
PA
10572
10573 for (loc2p = locp;
10574 (loc2p < bp_location + bp_location_count
10575 && (*loc2p)->address == old_loc->address);
10576 loc2p++)
10577 {
10578 if (*loc2p == old_loc)
10579 {
10580 found_object = 1;
10581 break;
10582 }
10583 }
74960c60 10584
4a64f543
MS
10585 /* If this location is no longer present, and inserted, look if
10586 there's maybe a new location at the same address. If so,
10587 mark that one inserted, and don't remove this one. This is
10588 needed so that we don't have a time window where a breakpoint
10589 at certain location is not inserted. */
74960c60 10590
876fa593 10591 if (old_loc->inserted)
0d381245 10592 {
4a64f543
MS
10593 /* If the location is inserted now, we might have to remove
10594 it. */
74960c60 10595
876fa593 10596 if (found_object && should_be_inserted (old_loc))
74960c60 10597 {
4a64f543
MS
10598 /* The location is still present in the location list,
10599 and still should be inserted. Don't do anything. */
20874c92 10600 keep_in_target = 1;
74960c60
VP
10601 }
10602 else
10603 {
4a64f543
MS
10604 /* The location is either no longer present, or got
10605 disabled. See if there's another location at the
10606 same address, in which case we don't need to remove
10607 this one from the target. */
876fa593 10608
2bdf28a0 10609 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10610 if (breakpoint_address_is_meaningful (old_loc->owner))
10611 {
876fa593 10612 for (loc2p = locp;
c7d46a38
PA
10613 (loc2p < bp_location + bp_location_count
10614 && (*loc2p)->address == old_loc->address);
876fa593
JK
10615 loc2p++)
10616 {
10617 struct bp_location *loc2 = *loc2p;
10618
2d134ed3 10619 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 10620 {
85d721b8
PA
10621 /* Read watchpoint locations are switched to
10622 access watchpoints, if the former are not
10623 supported, but the latter are. */
10624 if (is_hardware_watchpoint (old_loc->owner))
10625 {
10626 gdb_assert (is_hardware_watchpoint (loc2->owner));
10627 loc2->watchpoint_type = old_loc->watchpoint_type;
10628 }
10629
934709f0
PW
10630 /* loc2 is a duplicated location. We need to check
10631 if it should be inserted in case it will be
10632 unduplicated. */
10633 if (loc2 != old_loc
10634 && unduplicated_should_be_inserted (loc2))
c7d46a38 10635 {
934709f0 10636 swap_insertion (old_loc, loc2);
c7d46a38
PA
10637 keep_in_target = 1;
10638 break;
10639 }
876fa593
JK
10640 }
10641 }
10642 }
74960c60
VP
10643 }
10644
20874c92
VP
10645 if (!keep_in_target)
10646 {
876fa593 10647 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10648 {
4a64f543
MS
10649 /* This is just about all we can do. We could keep
10650 this location on the global list, and try to
10651 remove it next time, but there's no particular
10652 reason why we will succeed next time.
20874c92 10653
4a64f543
MS
10654 Note that at this point, old_loc->owner is still
10655 valid, as delete_breakpoint frees the breakpoint
10656 only after calling us. */
3e43a32a
MS
10657 printf_filtered (_("warning: Error removing "
10658 "breakpoint %d\n"),
876fa593 10659 old_loc->owner->number);
20874c92
VP
10660 }
10661 removed = 1;
10662 }
0d381245 10663 }
74960c60
VP
10664
10665 if (!found_object)
1c5cfe86 10666 {
db82e815
PA
10667 if (removed && non_stop
10668 && breakpoint_address_is_meaningful (old_loc->owner)
10669 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10670 {
db82e815
PA
10671 /* This location was removed from the target. In
10672 non-stop mode, a race condition is possible where
10673 we've removed a breakpoint, but stop events for that
10674 breakpoint are already queued and will arrive later.
10675 We apply an heuristic to be able to distinguish such
10676 SIGTRAPs from other random SIGTRAPs: we keep this
10677 breakpoint location for a bit, and will retire it
10678 after we see some number of events. The theory here
10679 is that reporting of events should, "on the average",
10680 be fair, so after a while we'll see events from all
10681 threads that have anything of interest, and no longer
10682 need to keep this breakpoint location around. We
10683 don't hold locations forever so to reduce chances of
10684 mistaking a non-breakpoint SIGTRAP for a breakpoint
10685 SIGTRAP.
10686
10687 The heuristic failing can be disastrous on
10688 decr_pc_after_break targets.
10689
10690 On decr_pc_after_break targets, like e.g., x86-linux,
10691 if we fail to recognize a late breakpoint SIGTRAP,
10692 because events_till_retirement has reached 0 too
10693 soon, we'll fail to do the PC adjustment, and report
10694 a random SIGTRAP to the user. When the user resumes
10695 the inferior, it will most likely immediately crash
2dec564e 10696 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10697 corrupted, because of being resumed e.g., in the
10698 middle of a multi-byte instruction, or skipped a
10699 one-byte instruction. This was actually seen happen
10700 on native x86-linux, and should be less rare on
10701 targets that do not support new thread events, like
10702 remote, due to the heuristic depending on
10703 thread_count.
10704
10705 Mistaking a random SIGTRAP for a breakpoint trap
10706 causes similar symptoms (PC adjustment applied when
10707 it shouldn't), but then again, playing with SIGTRAPs
10708 behind the debugger's back is asking for trouble.
10709
10710 Since hardware watchpoint traps are always
10711 distinguishable from other traps, so we don't need to
10712 apply keep hardware watchpoint moribund locations
10713 around. We simply always ignore hardware watchpoint
10714 traps we can no longer explain. */
10715
876fa593
JK
10716 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10717 old_loc->owner = NULL;
20874c92 10718
876fa593 10719 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10720 }
10721 else
f431efe5
PA
10722 {
10723 old_loc->owner = NULL;
10724 decref_bp_location (&old_loc);
10725 }
20874c92 10726 }
74960c60 10727 }
1c5cfe86 10728
348d480f
PA
10729 /* Rescan breakpoints at the same address and section, marking the
10730 first one as "first" and any others as "duplicates". This is so
10731 that the bpt instruction is only inserted once. If we have a
10732 permanent breakpoint at the same place as BPT, make that one the
10733 official one, and the rest as duplicates. Permanent breakpoints
10734 are sorted first for the same address.
10735
10736 Do the same for hardware watchpoints, but also considering the
10737 watchpoint's type (regular/access/read) and length. */
10738
10739 bp_loc_first = NULL;
10740 wp_loc_first = NULL;
10741 awp_loc_first = NULL;
10742 rwp_loc_first = NULL;
10743 ALL_BP_LOCATIONS (loc, locp)
10744 {
10745 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10746 non-NULL. */
10747 struct breakpoint *b = loc->owner;
10748 struct bp_location **loc_first_p;
10749
10750 if (b->enable_state == bp_disabled
10751 || b->enable_state == bp_call_disabled
10752 || b->enable_state == bp_startup_disabled
10753 || !loc->enabled
10754 || loc->shlib_disabled
10755 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
10756 /* Don't detect duplicate for tracepoint locations because they are
10757 never duplicated. See the comments in field `duplicate' of
10758 `struct bp_location'. */
348d480f
PA
10759 || is_tracepoint (b))
10760 continue;
10761
10762 /* Permanent breakpoint should always be inserted. */
10763 if (b->enable_state == bp_permanent && ! loc->inserted)
10764 internal_error (__FILE__, __LINE__,
10765 _("allegedly permanent breakpoint is not "
10766 "actually inserted"));
10767
10768 if (b->type == bp_hardware_watchpoint)
10769 loc_first_p = &wp_loc_first;
10770 else if (b->type == bp_read_watchpoint)
10771 loc_first_p = &rwp_loc_first;
10772 else if (b->type == bp_access_watchpoint)
10773 loc_first_p = &awp_loc_first;
10774 else
10775 loc_first_p = &bp_loc_first;
10776
10777 if (*loc_first_p == NULL
10778 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10779 || !breakpoint_locations_match (loc, *loc_first_p))
10780 {
10781 *loc_first_p = loc;
10782 loc->duplicate = 0;
10783 continue;
10784 }
10785
934709f0
PW
10786
10787 /* This and the above ensure the invariant that the first location
10788 is not duplicated, and is the inserted one.
10789 All following are marked as duplicated, and are not inserted. */
10790 if (loc->inserted)
10791 swap_insertion (loc, *loc_first_p);
348d480f
PA
10792 loc->duplicate = 1;
10793
10794 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10795 && b->enable_state != bp_permanent)
10796 internal_error (__FILE__, __LINE__,
10797 _("another breakpoint was inserted on top of "
10798 "a permanent breakpoint"));
10799 }
10800
10801 if (breakpoints_always_inserted_mode () && should_insert
10802 && (have_live_inferiors ()
10803 || (gdbarch_has_global_breakpoints (target_gdbarch))))
10804 insert_breakpoint_locations ();
10805
1e4d1764
YQ
10806 if (should_insert)
10807 download_tracepoint_locations ();
10808
348d480f
PA
10809 do_cleanups (cleanups);
10810}
10811
10812void
10813breakpoint_retire_moribund (void)
10814{
10815 struct bp_location *loc;
10816 int ix;
10817
10818 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10819 if (--(loc->events_till_retirement) == 0)
10820 {
10821 decref_bp_location (&loc);
10822 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10823 --ix;
10824 }
10825}
10826
10827static void
10828update_global_location_list_nothrow (int inserting)
10829{
10830 struct gdb_exception e;
10831
10832 TRY_CATCH (e, RETURN_MASK_ERROR)
10833 update_global_location_list (inserting);
10834}
10835
10836/* Clear BKP from a BPS. */
10837
10838static void
10839bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10840{
10841 bpstat bs;
10842
10843 for (bs = bps; bs; bs = bs->next)
10844 if (bs->breakpoint_at == bpt)
10845 {
10846 bs->breakpoint_at = NULL;
10847 bs->old_val = NULL;
10848 /* bs->commands will be freed later. */
10849 }
10850}
10851
10852/* Callback for iterate_over_threads. */
10853static int
10854bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10855{
10856 struct breakpoint *bpt = data;
10857
10858 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10859 return 0;
10860}
10861
10862/* Helper for breakpoint and tracepoint breakpoint_ops->mention
10863 callbacks. */
10864
10865static void
10866say_where (struct breakpoint *b)
10867{
79a45e25 10868 struct ui_out *uiout = current_uiout;
348d480f
PA
10869 struct value_print_options opts;
10870
10871 get_user_print_options (&opts);
10872
10873 /* i18n: cagney/2005-02-11: Below needs to be merged into a
10874 single string. */
10875 if (b->loc == NULL)
10876 {
10877 printf_filtered (_(" (%s) pending."), b->addr_string);
10878 }
10879 else
10880 {
10881 if (opts.addressprint || b->source_file == NULL)
10882 {
10883 printf_filtered (" at ");
10884 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
10885 gdb_stdout);
10886 }
10887 if (b->source_file)
10888 printf_filtered (": file %s, line %d.",
10889 b->source_file, b->line_number);
10890
10891 if (b->loc->next)
10892 {
10893 struct bp_location *loc = b->loc;
10894 int n = 0;
10895 for (; loc; loc = loc->next)
10896 ++n;
10897 printf_filtered (" (%d locations)", n);
10898 }
10899 }
10900}
10901
348d480f
PA
10902/* Default bp_location_ops methods. */
10903
10904static void
10905bp_location_dtor (struct bp_location *self)
10906{
10907 xfree (self->cond);
10908 xfree (self->function_name);
10909}
10910
10911static const struct bp_location_ops bp_location_ops =
10912{
10913 bp_location_dtor
10914};
10915
2060206e
PA
10916/* Default breakpoint_ops methods all breakpoint_ops ultimately
10917 inherit from. */
348d480f 10918
2060206e
PA
10919static void
10920base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
10921{
10922 decref_counted_command_line (&self->commands);
10923 xfree (self->cond_string);
348d480f
PA
10924 xfree (self->addr_string);
10925 xfree (self->addr_string_range_end);
348d480f
PA
10926 xfree (self->source_file);
10927}
10928
2060206e
PA
10929static struct bp_location *
10930base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
10931{
10932 struct bp_location *loc;
10933
10934 loc = XNEW (struct bp_location);
10935 init_bp_location (loc, &bp_location_ops, self);
10936 return loc;
10937}
10938
2060206e
PA
10939static void
10940base_breakpoint_re_set (struct breakpoint *b)
10941{
10942 /* Nothing to re-set. */
10943}
10944
10945#define internal_error_pure_virtual_called() \
10946 gdb_assert_not_reached ("pure virtual function called")
10947
10948static int
10949base_breakpoint_insert_location (struct bp_location *bl)
10950{
10951 internal_error_pure_virtual_called ();
10952}
10953
10954static int
10955base_breakpoint_remove_location (struct bp_location *bl)
10956{
10957 internal_error_pure_virtual_called ();
10958}
10959
10960static int
10961base_breakpoint_breakpoint_hit (const struct bp_location *bl,
10962 struct address_space *aspace,
10963 CORE_ADDR bp_addr)
10964{
10965 internal_error_pure_virtual_called ();
10966}
10967
10968static void
10969base_breakpoint_check_status (bpstat bs)
10970{
10971 /* Always stop. */
10972}
10973
10974/* A "works_in_software_mode" breakpoint_ops method that just internal
10975 errors. */
10976
10977static int
10978base_breakpoint_works_in_software_mode (const struct breakpoint *b)
10979{
10980 internal_error_pure_virtual_called ();
10981}
10982
10983/* A "resources_needed" breakpoint_ops method that just internal
10984 errors. */
10985
10986static int
10987base_breakpoint_resources_needed (const struct bp_location *bl)
10988{
10989 internal_error_pure_virtual_called ();
10990}
10991
10992static enum print_stop_action
10993base_breakpoint_print_it (bpstat bs)
10994{
10995 internal_error_pure_virtual_called ();
10996}
10997
10998static void
10999base_breakpoint_print_one_detail (const struct breakpoint *self,
11000 struct ui_out *uiout)
11001{
11002 /* nothing */
11003}
11004
11005static void
11006base_breakpoint_print_mention (struct breakpoint *b)
11007{
11008 internal_error_pure_virtual_called ();
11009}
11010
11011static void
11012base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
11013{
11014 internal_error_pure_virtual_called ();
11015}
11016
11017static struct breakpoint_ops base_breakpoint_ops =
11018{
11019 base_breakpoint_dtor,
11020 base_breakpoint_allocate_location,
11021 base_breakpoint_re_set,
11022 base_breakpoint_insert_location,
11023 base_breakpoint_remove_location,
11024 base_breakpoint_breakpoint_hit,
11025 base_breakpoint_check_status,
11026 base_breakpoint_resources_needed,
11027 base_breakpoint_works_in_software_mode,
11028 base_breakpoint_print_it,
11029 NULL,
11030 base_breakpoint_print_one_detail,
11031 base_breakpoint_print_mention,
11032 base_breakpoint_print_recreate
11033};
11034
11035/* Default breakpoint_ops methods. */
11036
11037static void
348d480f
PA
11038bkpt_re_set (struct breakpoint *b)
11039{
06edf0c0
PA
11040 /* Do not attempt to re-set breakpoints disabled during startup. */
11041 if (b->enable_state == bp_startup_disabled)
11042 return;
348d480f 11043
06edf0c0
PA
11044 /* FIXME: is this still reachable? */
11045 if (b->addr_string == NULL)
11046 {
11047 /* Anything without a string can't be re-set. */
348d480f 11048 delete_breakpoint (b);
06edf0c0 11049 return;
348d480f 11050 }
06edf0c0
PA
11051
11052 breakpoint_re_set_default (b);
348d480f
PA
11053}
11054
2060206e 11055static int
348d480f
PA
11056bkpt_insert_location (struct bp_location *bl)
11057{
11058 if (bl->loc_type == bp_loc_hardware_breakpoint)
11059 return target_insert_hw_breakpoint (bl->gdbarch,
11060 &bl->target_info);
11061 else
11062 return target_insert_breakpoint (bl->gdbarch,
11063 &bl->target_info);
11064}
11065
2060206e 11066static int
348d480f
PA
11067bkpt_remove_location (struct bp_location *bl)
11068{
11069 if (bl->loc_type == bp_loc_hardware_breakpoint)
11070 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11071 else
11072 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
11073}
11074
2060206e 11075static int
348d480f
PA
11076bkpt_breakpoint_hit (const struct bp_location *bl,
11077 struct address_space *aspace, CORE_ADDR bp_addr)
11078{
11079 struct breakpoint *b = bl->owner;
11080
11081 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11082 aspace, bp_addr))
11083 return 0;
11084
11085 if (overlay_debugging /* unmapped overlay section */
11086 && section_is_overlay (bl->section)
11087 && !section_is_mapped (bl->section))
11088 return 0;
11089
11090 return 1;
11091}
11092
2060206e 11093static int
348d480f
PA
11094bkpt_resources_needed (const struct bp_location *bl)
11095{
11096 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
11097
11098 return 1;
11099}
11100
2060206e 11101static enum print_stop_action
348d480f
PA
11102bkpt_print_it (bpstat bs)
11103{
348d480f
PA
11104 struct breakpoint *b;
11105 const struct bp_location *bl;
001c8c33 11106 int bp_temp;
79a45e25 11107 struct ui_out *uiout = current_uiout;
348d480f
PA
11108
11109 gdb_assert (bs->bp_location_at != NULL);
11110
11111 bl = bs->bp_location_at;
11112 b = bs->breakpoint_at;
11113
001c8c33
PA
11114 bp_temp = b->disposition == disp_del;
11115 if (bl->address != bl->requested_address)
11116 breakpoint_adjustment_warning (bl->requested_address,
11117 bl->address,
11118 b->number, 1);
11119 annotate_breakpoint (b->number);
11120 if (bp_temp)
11121 ui_out_text (uiout, "\nTemporary breakpoint ");
11122 else
11123 ui_out_text (uiout, "\nBreakpoint ");
11124 if (ui_out_is_mi_like_p (uiout))
348d480f 11125 {
001c8c33
PA
11126 ui_out_field_string (uiout, "reason",
11127 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11128 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 11129 }
001c8c33
PA
11130 ui_out_field_int (uiout, "bkptno", b->number);
11131 ui_out_text (uiout, ", ");
06edf0c0 11132
001c8c33 11133 return PRINT_SRC_AND_LOC;
06edf0c0
PA
11134}
11135
2060206e 11136static void
06edf0c0
PA
11137bkpt_print_mention (struct breakpoint *b)
11138{
79a45e25 11139 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
11140 return;
11141
11142 switch (b->type)
11143 {
11144 case bp_breakpoint:
11145 case bp_gnu_ifunc_resolver:
11146 if (b->disposition == disp_del)
11147 printf_filtered (_("Temporary breakpoint"));
11148 else
11149 printf_filtered (_("Breakpoint"));
11150 printf_filtered (_(" %d"), b->number);
11151 if (b->type == bp_gnu_ifunc_resolver)
11152 printf_filtered (_(" at gnu-indirect-function resolver"));
11153 break;
11154 case bp_hardware_breakpoint:
11155 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
11156 break;
11157 }
11158
11159 say_where (b);
11160}
11161
2060206e 11162static void
06edf0c0
PA
11163bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
11164{
11165 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11166 fprintf_unfiltered (fp, "tbreak");
11167 else if (tp->type == bp_breakpoint)
11168 fprintf_unfiltered (fp, "break");
11169 else if (tp->type == bp_hardware_breakpoint
11170 && tp->disposition == disp_del)
11171 fprintf_unfiltered (fp, "thbreak");
11172 else if (tp->type == bp_hardware_breakpoint)
11173 fprintf_unfiltered (fp, "hbreak");
11174 else
11175 internal_error (__FILE__, __LINE__,
11176 _("unhandled breakpoint type %d"), (int) tp->type);
11177
2060206e 11178 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 11179 print_recreate_thread (tp, fp);
06edf0c0
PA
11180}
11181
06edf0c0
PA
11182/* Virtual table for internal breakpoints. */
11183
11184static void
11185internal_bkpt_re_set (struct breakpoint *b)
11186{
11187 switch (b->type)
11188 {
11189 /* Delete overlay event and longjmp master breakpoints; they
11190 will be reset later by breakpoint_re_set. */
11191 case bp_overlay_event:
11192 case bp_longjmp_master:
11193 case bp_std_terminate_master:
11194 case bp_exception_master:
11195 delete_breakpoint (b);
11196 break;
11197
11198 /* This breakpoint is special, it's set up when the inferior
11199 starts and we really don't want to touch it. */
11200 case bp_shlib_event:
11201
11202 /* Like bp_shlib_event, this breakpoint type is special. Once
11203 it is set up, we do not want to touch it. */
11204 case bp_thread_event:
11205 break;
11206 }
11207}
11208
11209static void
11210internal_bkpt_check_status (bpstat bs)
11211{
11212 /* We do not stop for these. */
11213 bs->stop = 0;
11214}
11215
11216static enum print_stop_action
11217internal_bkpt_print_it (bpstat bs)
11218{
36dfb11c 11219 struct ui_out *uiout = current_uiout;
06edf0c0 11220 struct breakpoint *b;
06edf0c0 11221
06edf0c0
PA
11222 b = bs->breakpoint_at;
11223
06edf0c0
PA
11224 switch (b->type)
11225 {
348d480f
PA
11226 case bp_shlib_event:
11227 /* Did we stop because the user set the stop_on_solib_events
11228 variable? (If so, we report this as a generic, "Stopped due
11229 to shlib event" message.) */
36dfb11c
TT
11230 ui_out_text (uiout, _("Stopped due to shared library event\n"));
11231 if (ui_out_is_mi_like_p (uiout))
11232 ui_out_field_string (uiout, "reason",
11233 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
348d480f
PA
11234 break;
11235
11236 case bp_thread_event:
11237 /* Not sure how we will get here.
11238 GDB should not stop for these breakpoints. */
11239 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11240 break;
11241
11242 case bp_overlay_event:
11243 /* By analogy with the thread event, GDB should not stop for these. */
11244 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11245 break;
11246
11247 case bp_longjmp_master:
11248 /* These should never be enabled. */
11249 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11250 break;
11251
11252 case bp_std_terminate_master:
11253 /* These should never be enabled. */
11254 printf_filtered (_("std::terminate Master Breakpoint: "
11255 "gdb should not stop!\n"));
348d480f
PA
11256 break;
11257
11258 case bp_exception_master:
11259 /* These should never be enabled. */
11260 printf_filtered (_("Exception Master Breakpoint: "
11261 "gdb should not stop!\n"));
06edf0c0
PA
11262 break;
11263 }
11264
001c8c33 11265 return PRINT_NOTHING;
06edf0c0
PA
11266}
11267
11268static void
11269internal_bkpt_print_mention (struct breakpoint *b)
11270{
11271 /* Nothing to mention. These breakpoints are internal. */
11272}
11273
06edf0c0
PA
11274/* Virtual table for momentary breakpoints */
11275
11276static void
11277momentary_bkpt_re_set (struct breakpoint *b)
11278{
11279 /* Keep temporary breakpoints, which can be encountered when we step
11280 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
11281 Otherwise these should have been blown away via the cleanup chain
11282 or by breakpoint_init_inferior when we rerun the executable. */
11283}
11284
11285static void
11286momentary_bkpt_check_status (bpstat bs)
11287{
11288 /* Nothing. The point of these breakpoints is causing a stop. */
11289}
11290
11291static enum print_stop_action
11292momentary_bkpt_print_it (bpstat bs)
11293{
79a45e25
PA
11294 struct ui_out *uiout = current_uiout;
11295
001c8c33 11296 if (ui_out_is_mi_like_p (uiout))
06edf0c0 11297 {
001c8c33 11298 struct breakpoint *b = bs->breakpoint_at;
348d480f 11299
001c8c33
PA
11300 switch (b->type)
11301 {
11302 case bp_finish:
11303 ui_out_field_string
11304 (uiout, "reason",
11305 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
11306 break;
348d480f 11307
001c8c33
PA
11308 case bp_until:
11309 ui_out_field_string
11310 (uiout, "reason",
11311 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
11312 break;
11313 }
348d480f
PA
11314 }
11315
001c8c33 11316 return PRINT_UNKNOWN;
348d480f
PA
11317}
11318
06edf0c0
PA
11319static void
11320momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 11321{
06edf0c0 11322 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11323}
11324
348d480f 11325/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 11326
348d480f
PA
11327static void
11328tracepoint_re_set (struct breakpoint *b)
11329{
11330 breakpoint_re_set_default (b);
11331}
876fa593 11332
348d480f
PA
11333static int
11334tracepoint_breakpoint_hit (const struct bp_location *bl,
11335 struct address_space *aspace, CORE_ADDR bp_addr)
11336{
11337 /* By definition, the inferior does not report stops at
11338 tracepoints. */
11339 return 0;
74960c60
VP
11340}
11341
11342static void
348d480f
PA
11343tracepoint_print_one_detail (const struct breakpoint *self,
11344 struct ui_out *uiout)
74960c60 11345{
d9b3f62e
PA
11346 struct tracepoint *tp = (struct tracepoint *) self;
11347 if (tp->static_trace_marker_id)
348d480f
PA
11348 {
11349 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 11350
348d480f
PA
11351 ui_out_text (uiout, "\tmarker id is ");
11352 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 11353 tp->static_trace_marker_id);
348d480f
PA
11354 ui_out_text (uiout, "\n");
11355 }
0d381245
VP
11356}
11357
a474d7c2 11358static void
348d480f 11359tracepoint_print_mention (struct breakpoint *b)
a474d7c2 11360{
79a45e25 11361 if (ui_out_is_mi_like_p (current_uiout))
348d480f 11362 return;
cc59ec59 11363
348d480f
PA
11364 switch (b->type)
11365 {
11366 case bp_tracepoint:
11367 printf_filtered (_("Tracepoint"));
11368 printf_filtered (_(" %d"), b->number);
11369 break;
11370 case bp_fast_tracepoint:
11371 printf_filtered (_("Fast tracepoint"));
11372 printf_filtered (_(" %d"), b->number);
11373 break;
11374 case bp_static_tracepoint:
11375 printf_filtered (_("Static tracepoint"));
11376 printf_filtered (_(" %d"), b->number);
11377 break;
11378 default:
11379 internal_error (__FILE__, __LINE__,
11380 _("unhandled tracepoint type %d"), (int) b->type);
11381 }
11382
11383 say_where (b);
a474d7c2
PA
11384}
11385
348d480f 11386static void
d9b3f62e 11387tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 11388{
d9b3f62e
PA
11389 struct tracepoint *tp = (struct tracepoint *) self;
11390
11391 if (self->type == bp_fast_tracepoint)
348d480f 11392 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 11393 if (self->type == bp_static_tracepoint)
348d480f 11394 fprintf_unfiltered (fp, "strace");
d9b3f62e 11395 else if (self->type == bp_tracepoint)
348d480f
PA
11396 fprintf_unfiltered (fp, "trace");
11397 else
11398 internal_error (__FILE__, __LINE__,
d9b3f62e 11399 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 11400
d9b3f62e
PA
11401 fprintf_unfiltered (fp, " %s", self->addr_string);
11402 print_recreate_thread (self, fp);
11403
11404 if (tp->pass_count)
11405 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
11406}
11407
2060206e 11408struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 11409
53a5351d 11410/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 11411 structures. */
c906108c
SS
11412
11413void
fba45db2 11414delete_breakpoint (struct breakpoint *bpt)
c906108c 11415{
52f0bd74 11416 struct breakpoint *b;
c906108c 11417
8a3fe4f8 11418 gdb_assert (bpt != NULL);
c906108c 11419
4a64f543
MS
11420 /* Has this bp already been deleted? This can happen because
11421 multiple lists can hold pointers to bp's. bpstat lists are
11422 especial culprits.
11423
11424 One example of this happening is a watchpoint's scope bp. When
11425 the scope bp triggers, we notice that the watchpoint is out of
11426 scope, and delete it. We also delete its scope bp. But the
11427 scope bp is marked "auto-deleting", and is already on a bpstat.
11428 That bpstat is then checked for auto-deleting bp's, which are
11429 deleted.
11430
11431 A real solution to this problem might involve reference counts in
11432 bp's, and/or giving them pointers back to their referencing
11433 bpstat's, and teaching delete_breakpoint to only free a bp's
11434 storage when no more references were extent. A cheaper bandaid
11435 was chosen. */
c906108c
SS
11436 if (bpt->type == bp_none)
11437 return;
11438
4a64f543
MS
11439 /* At least avoid this stale reference until the reference counting
11440 of breakpoints gets resolved. */
d0fb5eae 11441 if (bpt->related_breakpoint != bpt)
e5a0a904 11442 {
d0fb5eae 11443 struct breakpoint *related;
3a5c3e22 11444 struct watchpoint *w;
d0fb5eae
JK
11445
11446 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 11447 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 11448 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
11449 w = (struct watchpoint *) bpt;
11450 else
11451 w = NULL;
11452 if (w != NULL)
11453 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
11454
11455 /* Unlink bpt from the bpt->related_breakpoint ring. */
11456 for (related = bpt; related->related_breakpoint != bpt;
11457 related = related->related_breakpoint);
11458 related->related_breakpoint = bpt->related_breakpoint;
11459 bpt->related_breakpoint = bpt;
e5a0a904
JK
11460 }
11461
a9634178
TJB
11462 /* watch_command_1 creates a watchpoint but only sets its number if
11463 update_watchpoint succeeds in creating its bp_locations. If there's
11464 a problem in that process, we'll be asked to delete the half-created
11465 watchpoint. In that case, don't announce the deletion. */
11466 if (bpt->number)
11467 observer_notify_breakpoint_deleted (bpt);
c906108c 11468
c906108c
SS
11469 if (breakpoint_chain == bpt)
11470 breakpoint_chain = bpt->next;
11471
c906108c
SS
11472 ALL_BREAKPOINTS (b)
11473 if (b->next == bpt)
c5aa993b
JM
11474 {
11475 b->next = bpt->next;
11476 break;
11477 }
c906108c 11478
f431efe5
PA
11479 /* Be sure no bpstat's are pointing at the breakpoint after it's
11480 been freed. */
11481 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 11482 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
11483 pointing at bpt from the stop_bpstat list entirely, as breakpoint
11484 commands are associated with the bpstat; if we remove it here,
11485 then the later call to bpstat_do_actions (&stop_bpstat); in
11486 event-top.c won't do anything, and temporary breakpoints with
11487 commands won't work. */
11488
11489 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11490
4a64f543
MS
11491 /* Now that breakpoint is removed from breakpoint list, update the
11492 global location list. This will remove locations that used to
11493 belong to this breakpoint. Do this before freeing the breakpoint
11494 itself, since remove_breakpoint looks at location's owner. It
11495 might be better design to have location completely
11496 self-contained, but it's not the case now. */
b60e7edf 11497 update_global_location_list (0);
74960c60 11498
348d480f 11499 bpt->ops->dtor (bpt);
4a64f543
MS
11500 /* On the chance that someone will soon try again to delete this
11501 same bp, we mark it as deleted before freeing its storage. */
c906108c 11502 bpt->type = bp_none;
b8c9b27d 11503 xfree (bpt);
c906108c
SS
11504}
11505
4d6140d9
AC
11506static void
11507do_delete_breakpoint_cleanup (void *b)
11508{
11509 delete_breakpoint (b);
11510}
11511
11512struct cleanup *
11513make_cleanup_delete_breakpoint (struct breakpoint *b)
11514{
11515 return make_cleanup (do_delete_breakpoint_cleanup, b);
11516}
11517
51be5b68
PA
11518/* Iterator function to call a user-provided callback function once
11519 for each of B and its related breakpoints. */
11520
11521static void
11522iterate_over_related_breakpoints (struct breakpoint *b,
11523 void (*function) (struct breakpoint *,
11524 void *),
11525 void *data)
11526{
11527 struct breakpoint *related;
11528
11529 related = b;
11530 do
11531 {
11532 struct breakpoint *next;
11533
11534 /* FUNCTION may delete RELATED. */
11535 next = related->related_breakpoint;
11536
11537 if (next == related)
11538 {
11539 /* RELATED is the last ring entry. */
11540 function (related, data);
11541
11542 /* FUNCTION may have deleted it, so we'd never reach back to
11543 B. There's nothing left to do anyway, so just break
11544 out. */
11545 break;
11546 }
11547 else
11548 function (related, data);
11549
11550 related = next;
11551 }
11552 while (related != b);
11553}
95a42b64
TT
11554
11555static void
11556do_delete_breakpoint (struct breakpoint *b, void *ignore)
11557{
11558 delete_breakpoint (b);
11559}
11560
51be5b68
PA
11561/* A callback for map_breakpoint_numbers that calls
11562 delete_breakpoint. */
11563
11564static void
11565do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
11566{
11567 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
11568}
11569
c906108c 11570void
fba45db2 11571delete_command (char *arg, int from_tty)
c906108c 11572{
35df4500 11573 struct breakpoint *b, *b_tmp;
c906108c 11574
ea9365bb
TT
11575 dont_repeat ();
11576
c906108c
SS
11577 if (arg == 0)
11578 {
11579 int breaks_to_delete = 0;
11580
46c6471b
PA
11581 /* Delete all breakpoints if no argument. Do not delete
11582 internal breakpoints, these have to be deleted with an
11583 explicit breakpoint number argument. */
c5aa993b 11584 ALL_BREAKPOINTS (b)
46c6471b 11585 if (user_breakpoint_p (b))
973d738b
DJ
11586 {
11587 breaks_to_delete = 1;
11588 break;
11589 }
c906108c
SS
11590
11591 /* Ask user only if there are some breakpoints to delete. */
11592 if (!from_tty
e2e0b3e5 11593 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 11594 {
35df4500 11595 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 11596 if (user_breakpoint_p (b))
c5aa993b 11597 delete_breakpoint (b);
c906108c
SS
11598 }
11599 }
11600 else
51be5b68 11601 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
11602}
11603
0d381245
VP
11604static int
11605all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 11606{
0d381245
VP
11607 for (; loc; loc = loc->next)
11608 if (!loc->shlib_disabled)
11609 return 0;
11610 return 1;
fe3f5fa8
VP
11611}
11612
776592bf
DE
11613/* Subroutine of update_breakpoint_locations to simplify it.
11614 Return non-zero if multiple fns in list LOC have the same name.
11615 Null names are ignored. */
11616
11617static int
11618ambiguous_names_p (struct bp_location *loc)
11619{
11620 struct bp_location *l;
11621 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
11622 (int (*) (const void *,
11623 const void *)) streq,
776592bf
DE
11624 NULL, xcalloc, xfree);
11625
11626 for (l = loc; l != NULL; l = l->next)
11627 {
11628 const char **slot;
11629 const char *name = l->function_name;
11630
11631 /* Allow for some names to be NULL, ignore them. */
11632 if (name == NULL)
11633 continue;
11634
11635 slot = (const char **) htab_find_slot (htab, (const void *) name,
11636 INSERT);
4a64f543
MS
11637 /* NOTE: We can assume slot != NULL here because xcalloc never
11638 returns NULL. */
776592bf
DE
11639 if (*slot != NULL)
11640 {
11641 htab_delete (htab);
11642 return 1;
11643 }
11644 *slot = name;
11645 }
11646
11647 htab_delete (htab);
11648 return 0;
11649}
11650
0fb4aa4b
PA
11651/* When symbols change, it probably means the sources changed as well,
11652 and it might mean the static tracepoint markers are no longer at
11653 the same address or line numbers they used to be at last we
11654 checked. Losing your static tracepoints whenever you rebuild is
11655 undesirable. This function tries to resync/rematch gdb static
11656 tracepoints with the markers on the target, for static tracepoints
11657 that have not been set by marker id. Static tracepoint that have
11658 been set by marker id are reset by marker id in breakpoint_re_set.
11659 The heuristic is:
11660
11661 1) For a tracepoint set at a specific address, look for a marker at
11662 the old PC. If one is found there, assume to be the same marker.
11663 If the name / string id of the marker found is different from the
11664 previous known name, assume that means the user renamed the marker
11665 in the sources, and output a warning.
11666
11667 2) For a tracepoint set at a given line number, look for a marker
11668 at the new address of the old line number. If one is found there,
11669 assume to be the same marker. If the name / string id of the
11670 marker found is different from the previous known name, assume that
11671 means the user renamed the marker in the sources, and output a
11672 warning.
11673
11674 3) If a marker is no longer found at the same address or line, it
11675 may mean the marker no longer exists. But it may also just mean
11676 the code changed a bit. Maybe the user added a few lines of code
11677 that made the marker move up or down (in line number terms). Ask
11678 the target for info about the marker with the string id as we knew
11679 it. If found, update line number and address in the matching
11680 static tracepoint. This will get confused if there's more than one
11681 marker with the same ID (possible in UST, although unadvised
11682 precisely because it confuses tools). */
11683
11684static struct symtab_and_line
11685update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11686{
d9b3f62e 11687 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
11688 struct static_tracepoint_marker marker;
11689 CORE_ADDR pc;
11690 int i;
11691
11692 pc = sal.pc;
11693 if (sal.line)
11694 find_line_pc (sal.symtab, sal.line, &pc);
11695
11696 if (target_static_tracepoint_marker_at (pc, &marker))
11697 {
d9b3f62e 11698 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
11699 warning (_("static tracepoint %d changed probed marker from %s to %s"),
11700 b->number,
d9b3f62e 11701 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 11702
d9b3f62e
PA
11703 xfree (tp->static_trace_marker_id);
11704 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
11705 release_static_tracepoint_marker (&marker);
11706
11707 return sal;
11708 }
11709
11710 /* Old marker wasn't found on target at lineno. Try looking it up
11711 by string ID. */
11712 if (!sal.explicit_pc
11713 && sal.line != 0
11714 && sal.symtab != NULL
d9b3f62e 11715 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
11716 {
11717 VEC(static_tracepoint_marker_p) *markers;
11718
11719 markers
d9b3f62e 11720 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
11721
11722 if (!VEC_empty(static_tracepoint_marker_p, markers))
11723 {
11724 struct symtab_and_line sal;
11725 struct symbol *sym;
11726 struct static_tracepoint_marker *marker;
79a45e25 11727 struct ui_out *uiout = current_uiout;
0fb4aa4b
PA
11728
11729 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
11730
d9b3f62e
PA
11731 xfree (tp->static_trace_marker_id);
11732 tp->static_trace_marker_id = xstrdup (marker->str_id);
0fb4aa4b
PA
11733
11734 warning (_("marker for static tracepoint %d (%s) not "
11735 "found at previous line number"),
d9b3f62e 11736 b->number, tp->static_trace_marker_id);
0fb4aa4b
PA
11737
11738 init_sal (&sal);
11739
11740 sal.pc = marker->address;
11741
11742 sal = find_pc_line (marker->address, 0);
11743 sym = find_pc_sect_function (marker->address, NULL);
11744 ui_out_text (uiout, "Now in ");
11745 if (sym)
11746 {
11747 ui_out_field_string (uiout, "func",
11748 SYMBOL_PRINT_NAME (sym));
11749 ui_out_text (uiout, " at ");
11750 }
11751 ui_out_field_string (uiout, "file", sal.symtab->filename);
11752 ui_out_text (uiout, ":");
11753
11754 if (ui_out_is_mi_like_p (uiout))
11755 {
11756 char *fullname = symtab_to_fullname (sal.symtab);
11757
11758 if (fullname)
11759 ui_out_field_string (uiout, "fullname", fullname);
11760 }
11761
11762 ui_out_field_int (uiout, "line", sal.line);
11763 ui_out_text (uiout, "\n");
11764
11765 b->line_number = sal.line;
11766
11767 xfree (b->source_file);
11768 if (sym)
11769 b->source_file = xstrdup (sal.symtab->filename);
11770 else
11771 b->source_file = NULL;
11772
11773 xfree (b->addr_string);
11774 b->addr_string = xstrprintf ("%s:%d",
11775 sal.symtab->filename, b->line_number);
11776
11777 /* Might be nice to check if function changed, and warn if
11778 so. */
11779
11780 release_static_tracepoint_marker (marker);
11781 }
11782 }
11783 return sal;
11784}
11785
8d3788bd
VP
11786/* Returns 1 iff locations A and B are sufficiently same that
11787 we don't need to report breakpoint as changed. */
11788
11789static int
11790locations_are_equal (struct bp_location *a, struct bp_location *b)
11791{
11792 while (a && b)
11793 {
11794 if (a->address != b->address)
11795 return 0;
11796
11797 if (a->shlib_disabled != b->shlib_disabled)
11798 return 0;
11799
11800 if (a->enabled != b->enabled)
11801 return 0;
11802
11803 a = a->next;
11804 b = b->next;
11805 }
11806
11807 if ((a == NULL) != (b == NULL))
11808 return 0;
11809
11810 return 1;
11811}
11812
f1310107
TJB
11813/* Create new breakpoint locations for B (a hardware or software breakpoint)
11814 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
11815 a ranged breakpoint. */
11816
0e30163f 11817void
0d381245 11818update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
11819 struct symtabs_and_lines sals,
11820 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
11821{
11822 int i;
0d381245
VP
11823 struct bp_location *existing_locations = b->loc;
11824
f1310107
TJB
11825 /* Ranged breakpoints have only one start location and one end location. */
11826 gdb_assert (sals_end.nelts == 0 || (sals.nelts == 1 && sals_end.nelts == 1));
11827
4a64f543
MS
11828 /* If there's no new locations, and all existing locations are
11829 pending, don't do anything. This optimizes the common case where
11830 all locations are in the same shared library, that was unloaded.
11831 We'd like to retain the location, so that when the library is
11832 loaded again, we don't loose the enabled/disabled status of the
11833 individual locations. */
0d381245 11834 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
11835 return;
11836
fe3f5fa8
VP
11837 b->loc = NULL;
11838
0d381245 11839 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 11840 {
0d381245 11841 struct bp_location *new_loc =
39d61571 11842 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 11843
0d381245
VP
11844 /* Reparse conditions, they might contain references to the
11845 old symtab. */
11846 if (b->cond_string != NULL)
11847 {
f1310107 11848 char *s;
0d381245 11849 struct gdb_exception e;
fe3f5fa8 11850
0d381245
VP
11851 s = b->cond_string;
11852 TRY_CATCH (e, RETURN_MASK_ERROR)
11853 {
11854 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
11855 0);
11856 }
11857 if (e.reason < 0)
11858 {
3e43a32a
MS
11859 warning (_("failed to reevaluate condition "
11860 "for breakpoint %d: %s"),
0d381245
VP
11861 b->number, e.message);
11862 new_loc->enabled = 0;
11863 }
11864 }
fe3f5fa8 11865
0d381245
VP
11866 if (b->source_file != NULL)
11867 xfree (b->source_file);
11868 if (sals.sals[i].symtab == NULL)
11869 b->source_file = NULL;
11870 else
1b36a34b 11871 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 11872
0d381245
VP
11873 if (b->line_number == 0)
11874 b->line_number = sals.sals[i].line;
f1310107
TJB
11875
11876 if (sals_end.nelts)
11877 {
11878 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11879
11880 new_loc->length = end - sals.sals[0].pc + 1;
11881 }
0d381245 11882 }
fe3f5fa8 11883
514f746b
AR
11884 /* Update locations of permanent breakpoints. */
11885 if (b->enable_state == bp_permanent)
11886 make_breakpoint_permanent (b);
11887
4a64f543
MS
11888 /* If possible, carry over 'disable' status from existing
11889 breakpoints. */
0d381245
VP
11890 {
11891 struct bp_location *e = existing_locations;
776592bf
DE
11892 /* If there are multiple breakpoints with the same function name,
11893 e.g. for inline functions, comparing function names won't work.
11894 Instead compare pc addresses; this is just a heuristic as things
11895 may have moved, but in practice it gives the correct answer
11896 often enough until a better solution is found. */
11897 int have_ambiguous_names = ambiguous_names_p (b->loc);
11898
0d381245
VP
11899 for (; e; e = e->next)
11900 {
11901 if (!e->enabled && e->function_name)
11902 {
11903 struct bp_location *l = b->loc;
776592bf
DE
11904 if (have_ambiguous_names)
11905 {
11906 for (; l; l = l->next)
f1310107 11907 if (breakpoint_locations_match (e, l))
776592bf
DE
11908 {
11909 l->enabled = 0;
11910 break;
11911 }
11912 }
11913 else
11914 {
11915 for (; l; l = l->next)
11916 if (l->function_name
11917 && strcmp (e->function_name, l->function_name) == 0)
11918 {
11919 l->enabled = 0;
11920 break;
11921 }
11922 }
0d381245
VP
11923 }
11924 }
11925 }
fe3f5fa8 11926
8d3788bd
VP
11927 if (!locations_are_equal (existing_locations, b->loc))
11928 observer_notify_breakpoint_modified (b);
11929
b60e7edf 11930 update_global_location_list (1);
fe3f5fa8
VP
11931}
11932
ef23e705
TJB
11933/* Find the SaL locations corresponding to the given ADDR_STRING.
11934 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
11935
11936static struct symtabs_and_lines
11937addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11938{
11939 char *s;
58438ac1 11940 int marker_spec;
02d20e4a 11941 struct symtabs_and_lines sals = {0};
ef23e705
TJB
11942 struct gdb_exception e;
11943
11944 s = addr_string;
11945 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11946
11947 TRY_CATCH (e, RETURN_MASK_ERROR)
11948 {
11949 if (marker_spec)
11950 {
d9b3f62e
PA
11951 struct tracepoint *tp = (struct tracepoint *) b;
11952
ef23e705 11953 sals = decode_static_tracepoint_spec (&s);
d9b3f62e 11954 if (sals.nelts > tp->static_trace_marker_id_idx)
ef23e705 11955 {
d9b3f62e 11956 sals.sals[0] = sals.sals[tp->static_trace_marker_id_idx];
ef23e705
TJB
11957 sals.nelts = 1;
11958 }
11959 else
d9b3f62e 11960 error (_("marker %s not found"), tp->static_trace_marker_id);
ef23e705
TJB
11961 }
11962 else
58438ac1 11963 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, NULL);
ef23e705
TJB
11964 }
11965 if (e.reason < 0)
11966 {
11967 int not_found_and_ok = 0;
11968 /* For pending breakpoints, it's expected that parsing will
11969 fail until the right shared library is loaded. User has
11970 already told to create pending breakpoints and don't need
11971 extra messages. If breakpoint is in bp_shlib_disabled
11972 state, then user already saw the message about that
11973 breakpoint being disabled, and don't want to see more
11974 errors. */
58438ac1 11975 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
11976 && (b->condition_not_parsed
11977 || (b->loc && b->loc->shlib_disabled)
11978 || b->enable_state == bp_disabled))
11979 not_found_and_ok = 1;
11980
11981 if (!not_found_and_ok)
11982 {
11983 /* We surely don't want to warn about the same breakpoint
11984 10 times. One solution, implemented here, is disable
11985 the breakpoint on error. Another solution would be to
11986 have separate 'warning emitted' flag. Since this
11987 happens only when a binary has changed, I don't know
11988 which approach is better. */
11989 b->enable_state = bp_disabled;
11990 throw_exception (e);
11991 }
11992 }
11993
58438ac1 11994 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705
TJB
11995 {
11996 gdb_assert (sals.nelts == 1);
11997
11998 resolve_sal_pc (&sals.sals[0]);
11999 if (b->condition_not_parsed && s && s[0])
12000 {
12001 char *cond_string = 0;
12002 int thread = -1;
12003 int task = 0;
12004
12005 find_condition_and_thread (s, sals.sals[0].pc,
12006 &cond_string, &thread, &task);
12007 if (cond_string)
12008 b->cond_string = cond_string;
12009 b->thread = thread;
12010 b->task = task;
12011 b->condition_not_parsed = 0;
12012 }
12013
12014 if (b->type == bp_static_tracepoint && !marker_spec)
12015 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 12016
58438ac1
TT
12017 *found = 1;
12018 }
12019 else
12020 *found = 0;
ef23e705
TJB
12021
12022 return sals;
12023}
12024
348d480f
PA
12025/* The default re_set method, for typical hardware or software
12026 breakpoints. Reevaluate the breakpoint and recreate its
12027 locations. */
12028
12029static void
28010a5d 12030breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
12031{
12032 int found;
f1310107 12033 struct symtabs_and_lines sals, sals_end;
ef23e705 12034 struct symtabs_and_lines expanded = {0};
f1310107 12035 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
12036
12037 sals = addr_string_to_sals (b, b->addr_string, &found);
12038 if (found)
12039 {
12040 make_cleanup (xfree, sals.sals);
12041 expanded = expand_line_sal_maybe (sals.sals[0]);
12042 }
12043
f1310107
TJB
12044 if (b->addr_string_range_end)
12045 {
12046 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
12047 if (found)
12048 {
12049 make_cleanup (xfree, sals_end.sals);
12050 expanded_end = expand_line_sal_maybe (sals_end.sals[0]);
12051 }
12052 }
12053
12054 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
12055}
12056
12057/* Prepare the global context for a re-set of breakpoint B. */
12058
12059static struct cleanup *
12060prepare_re_set_context (struct breakpoint *b)
12061{
12062 struct cleanup *cleanups;
12063
12064 input_radix = b->input_radix;
12065 cleanups = save_current_space_and_thread ();
12066 switch_to_program_space_and_thread (b->pspace);
12067 set_language (b->language);
12068
12069 return cleanups;
ef23e705
TJB
12070}
12071
c906108c
SS
12072/* Reset a breakpoint given it's struct breakpoint * BINT.
12073 The value we return ends up being the return value from catch_errors.
12074 Unused in this case. */
12075
12076static int
4efb68b1 12077breakpoint_re_set_one (void *bint)
c906108c 12078{
4a64f543 12079 /* Get past catch_errs. */
53a5351d 12080 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 12081 struct cleanup *cleanups;
c906108c 12082
348d480f
PA
12083 cleanups = prepare_re_set_context (b);
12084 b->ops->re_set (b);
12085 do_cleanups (cleanups);
c906108c
SS
12086 return 0;
12087}
12088
69de3c6a 12089/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 12090void
69de3c6a 12091breakpoint_re_set (void)
c906108c 12092{
35df4500 12093 struct breakpoint *b, *b_tmp;
c906108c
SS
12094 enum language save_language;
12095 int save_input_radix;
6c95b8df 12096 struct cleanup *old_chain;
c5aa993b 12097
c906108c
SS
12098 save_language = current_language->la_language;
12099 save_input_radix = input_radix;
6c95b8df
PA
12100 old_chain = save_current_program_space ();
12101
35df4500 12102 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12103 {
4a64f543 12104 /* Format possible error msg. */
fe3f5fa8 12105 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
12106 b->number);
12107 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 12108 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 12109 do_cleanups (cleanups);
c5aa993b 12110 }
c906108c
SS
12111 set_language (save_language);
12112 input_radix = save_input_radix;
e62c965a 12113
0756c555 12114 jit_breakpoint_re_set ();
4efc6507 12115
6c95b8df
PA
12116 do_cleanups (old_chain);
12117
af02033e
PP
12118 create_overlay_event_breakpoint ();
12119 create_longjmp_master_breakpoint ();
12120 create_std_terminate_master_breakpoint ();
186c406b 12121 create_exception_master_breakpoint ();
1bfeeb0f
JL
12122
12123 /* While we're at it, reset the skip list too. */
12124 skip_re_set ();
c906108c
SS
12125}
12126\f
c906108c
SS
12127/* Reset the thread number of this breakpoint:
12128
12129 - If the breakpoint is for all threads, leave it as-is.
4a64f543 12130 - Else, reset it to the current thread for inferior_ptid. */
c906108c 12131void
fba45db2 12132breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
12133{
12134 if (b->thread != -1)
12135 {
39f77062
KB
12136 if (in_thread_list (inferior_ptid))
12137 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
12138
12139 /* We're being called after following a fork. The new fork is
12140 selected as current, and unless this was a vfork will have a
12141 different program space from the original thread. Reset that
12142 as well. */
12143 b->loc->pspace = current_program_space;
c906108c
SS
12144 }
12145}
12146
03ac34d5
MS
12147/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12148 If from_tty is nonzero, it prints a message to that effect,
12149 which ends with a period (no newline). */
12150
c906108c 12151void
fba45db2 12152set_ignore_count (int bptnum, int count, int from_tty)
c906108c 12153{
52f0bd74 12154 struct breakpoint *b;
c906108c
SS
12155
12156 if (count < 0)
12157 count = 0;
12158
12159 ALL_BREAKPOINTS (b)
12160 if (b->number == bptnum)
c5aa993b 12161 {
d77f58be
SS
12162 if (is_tracepoint (b))
12163 {
12164 if (from_tty && count != 0)
12165 printf_filtered (_("Ignore count ignored for tracepoint %d."),
12166 bptnum);
12167 return;
12168 }
12169
c5aa993b 12170 b->ignore_count = count;
221ea385
KS
12171 if (from_tty)
12172 {
12173 if (count == 0)
3e43a32a
MS
12174 printf_filtered (_("Will stop next time "
12175 "breakpoint %d is reached."),
221ea385
KS
12176 bptnum);
12177 else if (count == 1)
a3f17187 12178 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
12179 bptnum);
12180 else
3e43a32a
MS
12181 printf_filtered (_("Will ignore next %d "
12182 "crossings of breakpoint %d."),
221ea385
KS
12183 count, bptnum);
12184 }
c5aa993b 12185 breakpoints_changed ();
8d3788bd 12186 observer_notify_breakpoint_modified (b);
c5aa993b
JM
12187 return;
12188 }
c906108c 12189
8a3fe4f8 12190 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
12191}
12192
c906108c
SS
12193/* Command to set ignore-count of breakpoint N to COUNT. */
12194
12195static void
fba45db2 12196ignore_command (char *args, int from_tty)
c906108c
SS
12197{
12198 char *p = args;
52f0bd74 12199 int num;
c906108c
SS
12200
12201 if (p == 0)
e2e0b3e5 12202 error_no_arg (_("a breakpoint number"));
c5aa993b 12203
c906108c 12204 num = get_number (&p);
5c44784c 12205 if (num == 0)
8a3fe4f8 12206 error (_("bad breakpoint number: '%s'"), args);
c906108c 12207 if (*p == 0)
8a3fe4f8 12208 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
12209
12210 set_ignore_count (num,
12211 longest_to_int (value_as_long (parse_and_eval (p))),
12212 from_tty);
221ea385
KS
12213 if (from_tty)
12214 printf_filtered ("\n");
c906108c
SS
12215}
12216\f
12217/* Call FUNCTION on each of the breakpoints
12218 whose numbers are given in ARGS. */
12219
12220static void
95a42b64
TT
12221map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
12222 void *),
12223 void *data)
c906108c 12224{
52f0bd74
AC
12225 int num;
12226 struct breakpoint *b, *tmp;
11cf8741 12227 int match;
197f0a60 12228 struct get_number_or_range_state state;
c906108c 12229
197f0a60 12230 if (args == 0)
e2e0b3e5 12231 error_no_arg (_("one or more breakpoint numbers"));
c906108c 12232
197f0a60
TT
12233 init_number_or_range (&state, args);
12234
12235 while (!state.finished)
c906108c 12236 {
197f0a60
TT
12237 char *p = state.string;
12238
11cf8741 12239 match = 0;
c5aa993b 12240
197f0a60 12241 num = get_number_or_range (&state);
5c44784c 12242 if (num == 0)
c5aa993b 12243 {
8a3fe4f8 12244 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
12245 }
12246 else
12247 {
12248 ALL_BREAKPOINTS_SAFE (b, tmp)
12249 if (b->number == num)
12250 {
11cf8741 12251 match = 1;
cdac0397 12252 function (b, data);
11cf8741 12253 break;
5c44784c 12254 }
11cf8741 12255 if (match == 0)
a3f17187 12256 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 12257 }
c906108c
SS
12258 }
12259}
12260
0d381245
VP
12261static struct bp_location *
12262find_location_by_number (char *number)
12263{
12264 char *dot = strchr (number, '.');
12265 char *p1;
12266 int bp_num;
12267 int loc_num;
12268 struct breakpoint *b;
12269 struct bp_location *loc;
12270
12271 *dot = '\0';
12272
12273 p1 = number;
197f0a60 12274 bp_num = get_number (&p1);
0d381245
VP
12275 if (bp_num == 0)
12276 error (_("Bad breakpoint number '%s'"), number);
12277
12278 ALL_BREAKPOINTS (b)
12279 if (b->number == bp_num)
12280 {
12281 break;
12282 }
12283
12284 if (!b || b->number != bp_num)
12285 error (_("Bad breakpoint number '%s'"), number);
12286
12287 p1 = dot+1;
197f0a60 12288 loc_num = get_number (&p1);
0d381245
VP
12289 if (loc_num == 0)
12290 error (_("Bad breakpoint location number '%s'"), number);
12291
12292 --loc_num;
12293 loc = b->loc;
12294 for (;loc_num && loc; --loc_num, loc = loc->next)
12295 ;
12296 if (!loc)
12297 error (_("Bad breakpoint location number '%s'"), dot+1);
12298
12299 return loc;
12300}
12301
12302
1900040c
MS
12303/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12304 If from_tty is nonzero, it prints a message to that effect,
12305 which ends with a period (no newline). */
12306
c906108c 12307void
fba45db2 12308disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
12309{
12310 /* Never disable a watchpoint scope breakpoint; we want to
12311 hit them when we leave scope so we can delete both the
12312 watchpoint and its scope breakpoint at that time. */
12313 if (bpt->type == bp_watchpoint_scope)
12314 return;
12315
c2c6d25f 12316 /* You can't disable permanent breakpoints. */
b5de0fa7 12317 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
12318 return;
12319
b5de0fa7 12320 bpt->enable_state = bp_disabled;
c906108c 12321
d248b706
KY
12322 if (target_supports_enable_disable_tracepoint ()
12323 && current_trace_status ()->running && is_tracepoint (bpt))
12324 {
12325 struct bp_location *location;
12326
12327 for (location = bpt->loc; location; location = location->next)
12328 target_disable_tracepoint (location);
12329 }
12330
b60e7edf 12331 update_global_location_list (0);
c906108c 12332
8d3788bd 12333 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12334}
12335
51be5b68
PA
12336/* A callback for iterate_over_related_breakpoints. */
12337
12338static void
12339do_disable_breakpoint (struct breakpoint *b, void *ignore)
12340{
12341 disable_breakpoint (b);
12342}
12343
95a42b64
TT
12344/* A callback for map_breakpoint_numbers that calls
12345 disable_breakpoint. */
12346
12347static void
12348do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
12349{
51be5b68 12350 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
12351}
12352
c906108c 12353static void
fba45db2 12354disable_command (char *args, int from_tty)
c906108c 12355{
c906108c 12356 if (args == 0)
46c6471b
PA
12357 {
12358 struct breakpoint *bpt;
12359
12360 ALL_BREAKPOINTS (bpt)
12361 if (user_breakpoint_p (bpt))
12362 disable_breakpoint (bpt);
12363 }
0d381245
VP
12364 else if (strchr (args, '.'))
12365 {
12366 struct bp_location *loc = find_location_by_number (args);
12367 if (loc)
d248b706
KY
12368 {
12369 loc->enabled = 0;
12370 if (target_supports_enable_disable_tracepoint ()
12371 && current_trace_status ()->running && loc->owner
12372 && is_tracepoint (loc->owner))
12373 target_disable_tracepoint (loc);
12374 }
b60e7edf 12375 update_global_location_list (0);
0d381245 12376 }
c906108c 12377 else
95a42b64 12378 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
12379}
12380
12381static void
51be5b68 12382enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 12383{
afe38095 12384 int target_resources_ok;
c906108c
SS
12385
12386 if (bpt->type == bp_hardware_breakpoint)
12387 {
12388 int i;
c5aa993b 12389 i = hw_breakpoint_used_count ();
53a5351d 12390 target_resources_ok =
d92524f1 12391 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 12392 i + 1, 0);
c906108c 12393 if (target_resources_ok == 0)
8a3fe4f8 12394 error (_("No hardware breakpoint support in the target."));
c906108c 12395 else if (target_resources_ok < 0)
8a3fe4f8 12396 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
12397 }
12398
cc60f2e3 12399 if (is_watchpoint (bpt))
c906108c 12400 {
d07205c2
JK
12401 /* Initialize it just to avoid a GCC false warning. */
12402 enum enable_state orig_enable_state = 0;
dde02812
ES
12403 struct gdb_exception e;
12404
12405 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 12406 {
3a5c3e22
PA
12407 struct watchpoint *w = (struct watchpoint *) bpt;
12408
1e718ff1
TJB
12409 orig_enable_state = bpt->enable_state;
12410 bpt->enable_state = bp_enabled;
3a5c3e22 12411 update_watchpoint (w, 1 /* reparse */);
c906108c 12412 }
dde02812 12413 if (e.reason < 0)
c5aa993b 12414 {
1e718ff1 12415 bpt->enable_state = orig_enable_state;
dde02812
ES
12416 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12417 bpt->number);
12418 return;
c5aa993b 12419 }
c906108c 12420 }
0101ce28 12421
b4c291bb
KH
12422 if (bpt->enable_state != bp_permanent)
12423 bpt->enable_state = bp_enabled;
d248b706
KY
12424
12425 if (target_supports_enable_disable_tracepoint ()
12426 && current_trace_status ()->running && is_tracepoint (bpt))
12427 {
12428 struct bp_location *location;
12429
12430 for (location = bpt->loc; location; location = location->next)
12431 target_enable_tracepoint (location);
12432 }
12433
b4c291bb 12434 bpt->disposition = disposition;
b60e7edf 12435 update_global_location_list (1);
b4c291bb
KH
12436 breakpoints_changed ();
12437
8d3788bd 12438 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12439}
12440
fe3f5fa8 12441
c906108c 12442void
fba45db2 12443enable_breakpoint (struct breakpoint *bpt)
c906108c 12444{
51be5b68
PA
12445 enable_breakpoint_disp (bpt, bpt->disposition);
12446}
12447
12448static void
12449do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12450{
12451 enable_breakpoint (bpt);
c906108c
SS
12452}
12453
95a42b64
TT
12454/* A callback for map_breakpoint_numbers that calls
12455 enable_breakpoint. */
12456
12457static void
12458do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12459{
51be5b68 12460 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
12461}
12462
c906108c
SS
12463/* The enable command enables the specified breakpoints (or all defined
12464 breakpoints) so they once again become (or continue to be) effective
1272ad14 12465 in stopping the inferior. */
c906108c 12466
c906108c 12467static void
fba45db2 12468enable_command (char *args, int from_tty)
c906108c 12469{
c906108c 12470 if (args == 0)
46c6471b
PA
12471 {
12472 struct breakpoint *bpt;
12473
12474 ALL_BREAKPOINTS (bpt)
12475 if (user_breakpoint_p (bpt))
12476 enable_breakpoint (bpt);
12477 }
0d381245
VP
12478 else if (strchr (args, '.'))
12479 {
12480 struct bp_location *loc = find_location_by_number (args);
12481 if (loc)
d248b706
KY
12482 {
12483 loc->enabled = 1;
12484 if (target_supports_enable_disable_tracepoint ()
12485 && current_trace_status ()->running && loc->owner
12486 && is_tracepoint (loc->owner))
12487 target_enable_tracepoint (loc);
12488 }
b60e7edf 12489 update_global_location_list (1);
0d381245 12490 }
c906108c 12491 else
95a42b64 12492 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
12493}
12494
12495static void
51be5b68
PA
12496do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12497{
12498 enum bpdisp disp = *(enum bpdisp *) arg;
12499
12500 enable_breakpoint_disp (bpt, disp);
12501}
12502
12503static void
12504do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12505{
51be5b68
PA
12506 enum bpdisp disp = disp_disable;
12507
12508 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12509}
12510
c906108c 12511static void
fba45db2 12512enable_once_command (char *args, int from_tty)
c906108c 12513{
51be5b68 12514 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
12515}
12516
12517static void
51be5b68 12518do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12519{
51be5b68
PA
12520 enum bpdisp disp = disp_del;
12521
12522 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12523}
12524
c906108c 12525static void
fba45db2 12526enable_delete_command (char *args, int from_tty)
c906108c 12527{
51be5b68 12528 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
12529}
12530\f
fa8d40ab
JJ
12531static void
12532set_breakpoint_cmd (char *args, int from_tty)
12533{
12534}
12535
12536static void
12537show_breakpoint_cmd (char *args, int from_tty)
12538{
12539}
12540
1f3b5d1b
PP
12541/* Invalidate last known value of any hardware watchpoint if
12542 the memory which that value represents has been written to by
12543 GDB itself. */
12544
12545static void
12546invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12547 const bfd_byte *data)
12548{
12549 struct breakpoint *bp;
12550
12551 ALL_BREAKPOINTS (bp)
12552 if (bp->enable_state == bp_enabled
3a5c3e22 12553 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 12554 {
3a5c3e22 12555 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 12556
3a5c3e22
PA
12557 if (wp->val_valid && wp->val)
12558 {
12559 struct bp_location *loc;
12560
12561 for (loc = bp->loc; loc != NULL; loc = loc->next)
12562 if (loc->loc_type == bp_loc_hardware_watchpoint
12563 && loc->address + loc->length > addr
12564 && addr + len > loc->address)
12565 {
12566 value_free (wp->val);
12567 wp->val = NULL;
12568 wp->val_valid = 0;
12569 }
12570 }
1f3b5d1b
PP
12571 }
12572}
12573
1bfeeb0f
JL
12574/* Use the last displayed codepoint's values, or nothing
12575 if they aren't valid. */
c906108c
SS
12576
12577struct symtabs_and_lines
fba45db2 12578decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
12579{
12580 struct symtabs_and_lines sals;
cc59ec59 12581
c906108c 12582 if (string == 0)
8a3fe4f8 12583 error (_("Empty line specification."));
1bfeeb0f 12584 if (last_displayed_sal_is_valid ())
c906108c 12585 sals = decode_line_1 (&string, funfirstline,
1bfeeb0f
JL
12586 get_last_displayed_symtab (),
12587 get_last_displayed_line (),
58438ac1 12588 NULL);
c906108c
SS
12589 else
12590 sals = decode_line_1 (&string, funfirstline,
58438ac1 12591 (struct symtab *) NULL, 0, NULL);
c906108c 12592 if (*string)
8a3fe4f8 12593 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
12594 return sals;
12595}
8181d85f
DJ
12596
12597/* Create and insert a raw software breakpoint at PC. Return an
12598 identifier, which should be used to remove the breakpoint later.
12599 In general, places which call this should be using something on the
12600 breakpoint chain instead; this function should be eliminated
12601 someday. */
12602
12603void *
6c95b8df
PA
12604deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12605 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
12606{
12607 struct bp_target_info *bp_tgt;
12608
6c95b8df 12609 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 12610
6c95b8df 12611 bp_tgt->placed_address_space = aspace;
8181d85f 12612 bp_tgt->placed_address = pc;
6c95b8df 12613
a6d9a66e 12614 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
12615 {
12616 /* Could not insert the breakpoint. */
12617 xfree (bp_tgt);
12618 return NULL;
12619 }
12620
12621 return bp_tgt;
12622}
12623
4a64f543
MS
12624/* Remove a breakpoint BP inserted by
12625 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
12626
12627int
a6d9a66e 12628deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
12629{
12630 struct bp_target_info *bp_tgt = bp;
12631 int ret;
12632
a6d9a66e 12633 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
12634 xfree (bp_tgt);
12635
12636 return ret;
12637}
12638
4a64f543
MS
12639/* One (or perhaps two) breakpoints used for software single
12640 stepping. */
8181d85f
DJ
12641
12642static void *single_step_breakpoints[2];
a6d9a66e 12643static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
12644
12645/* Create and insert a breakpoint for software single step. */
12646
12647void
6c95b8df 12648insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
12649 struct address_space *aspace,
12650 CORE_ADDR next_pc)
8181d85f
DJ
12651{
12652 void **bpt_p;
12653
12654 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
12655 {
12656 bpt_p = &single_step_breakpoints[0];
12657 single_step_gdbarch[0] = gdbarch;
12658 }
8181d85f
DJ
12659 else
12660 {
12661 gdb_assert (single_step_breakpoints[1] == NULL);
12662 bpt_p = &single_step_breakpoints[1];
a6d9a66e 12663 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
12664 }
12665
4a64f543
MS
12666 /* NOTE drow/2006-04-11: A future improvement to this function would
12667 be to only create the breakpoints once, and actually put them on
12668 the breakpoint chain. That would let us use set_raw_breakpoint.
12669 We could adjust the addresses each time they were needed. Doing
12670 this requires corresponding changes elsewhere where single step
12671 breakpoints are handled, however. So, for now, we use this. */
8181d85f 12672
6c95b8df 12673 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 12674 if (*bpt_p == NULL)
5af949e3
UW
12675 error (_("Could not insert single-step breakpoint at %s"),
12676 paddress (gdbarch, next_pc));
8181d85f
DJ
12677}
12678
f02253f1
HZ
12679/* Check if the breakpoints used for software single stepping
12680 were inserted or not. */
12681
12682int
12683single_step_breakpoints_inserted (void)
12684{
12685 return (single_step_breakpoints[0] != NULL
12686 || single_step_breakpoints[1] != NULL);
12687}
12688
8181d85f
DJ
12689/* Remove and delete any breakpoints used for software single step. */
12690
12691void
12692remove_single_step_breakpoints (void)
12693{
12694 gdb_assert (single_step_breakpoints[0] != NULL);
12695
12696 /* See insert_single_step_breakpoint for more about this deprecated
12697 call. */
a6d9a66e
UW
12698 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12699 single_step_breakpoints[0]);
12700 single_step_gdbarch[0] = NULL;
8181d85f
DJ
12701 single_step_breakpoints[0] = NULL;
12702
12703 if (single_step_breakpoints[1] != NULL)
12704 {
a6d9a66e
UW
12705 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12706 single_step_breakpoints[1]);
12707 single_step_gdbarch[1] = NULL;
8181d85f
DJ
12708 single_step_breakpoints[1] = NULL;
12709 }
12710}
12711
d03285ec
UW
12712/* Delete software single step breakpoints without removing them from
12713 the inferior. This is intended to be used if the inferior's address
12714 space where they were inserted is already gone, e.g. after exit or
12715 exec. */
12716
12717void
12718cancel_single_step_breakpoints (void)
12719{
12720 int i;
12721
12722 for (i = 0; i < 2; i++)
12723 if (single_step_breakpoints[i])
12724 {
12725 xfree (single_step_breakpoints[i]);
12726 single_step_breakpoints[i] = NULL;
12727 single_step_gdbarch[i] = NULL;
12728 }
12729}
12730
12731/* Detach software single-step breakpoints from INFERIOR_PTID without
12732 removing them. */
12733
12734static void
12735detach_single_step_breakpoints (void)
12736{
12737 int i;
12738
12739 for (i = 0; i < 2; i++)
12740 if (single_step_breakpoints[i])
12741 target_remove_breakpoint (single_step_gdbarch[i],
12742 single_step_breakpoints[i]);
12743}
12744
4a64f543
MS
12745/* Check whether a software single-step breakpoint is inserted at
12746 PC. */
1aafd4da
UW
12747
12748static int
cc59ec59
MS
12749single_step_breakpoint_inserted_here_p (struct address_space *aspace,
12750 CORE_ADDR pc)
1aafd4da
UW
12751{
12752 int i;
12753
12754 for (i = 0; i < 2; i++)
12755 {
12756 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
12757 if (bp_tgt
12758 && breakpoint_address_match (bp_tgt->placed_address_space,
12759 bp_tgt->placed_address,
12760 aspace, pc))
1aafd4da
UW
12761 return 1;
12762 }
12763
12764 return 0;
12765}
12766
a96d9b2e
SDJ
12767/* Returns 0 if 'bp' is NOT a syscall catchpoint,
12768 non-zero otherwise. */
12769static int
12770is_syscall_catchpoint_enabled (struct breakpoint *bp)
12771{
12772 if (syscall_catchpoint_p (bp)
12773 && bp->enable_state != bp_disabled
12774 && bp->enable_state != bp_call_disabled)
12775 return 1;
12776 else
12777 return 0;
12778}
12779
12780int
12781catch_syscall_enabled (void)
12782{
12783 struct inferior *inf = current_inferior ();
12784
12785 return inf->total_syscalls_count != 0;
12786}
12787
12788int
12789catching_syscall_number (int syscall_number)
12790{
12791 struct breakpoint *bp;
12792
12793 ALL_BREAKPOINTS (bp)
12794 if (is_syscall_catchpoint_enabled (bp))
12795 {
be5c67c1
PA
12796 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12797
12798 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
12799 {
12800 int i, iter;
12801 for (i = 0;
be5c67c1 12802 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
12803 i++)
12804 if (syscall_number == iter)
12805 return 1;
12806 }
12807 else
12808 return 1;
12809 }
12810
12811 return 0;
12812}
12813
12814/* Complete syscall names. Used by "catch syscall". */
12815static char **
12816catch_syscall_completer (struct cmd_list_element *cmd,
12817 char *text, char *word)
12818{
12819 const char **list = get_syscall_names ();
c38eea1a
MS
12820 char **retlist
12821 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 12822
c38eea1a
MS
12823 xfree (list);
12824 return retlist;
a96d9b2e
SDJ
12825}
12826
1042e4c0
SS
12827/* Tracepoint-specific operations. */
12828
12829/* Set tracepoint count to NUM. */
12830static void
12831set_tracepoint_count (int num)
12832{
12833 tracepoint_count = num;
4fa62494 12834 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
12835}
12836
12837void
12838trace_command (char *arg, int from_tty)
12839{
8cdf0e15
VP
12840 if (create_breakpoint (get_current_arch (),
12841 arg,
12842 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12843 0 /* tempflag */,
12844 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
12845 0 /* Ignore count */,
12846 pending_break_support,
348d480f 12847 &tracepoint_breakpoint_ops,
8cdf0e15 12848 from_tty,
84f4c1fe
PM
12849 1 /* enabled */,
12850 0 /* internal */))
fd9b8c24 12851 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
12852}
12853
7a697b8d
SS
12854void
12855ftrace_command (char *arg, int from_tty)
12856{
8cdf0e15
VP
12857 if (create_breakpoint (get_current_arch (),
12858 arg,
12859 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12860 0 /* tempflag */,
12861 bp_fast_tracepoint /* type_wanted */,
12862 0 /* Ignore count */,
12863 pending_break_support,
348d480f 12864 &tracepoint_breakpoint_ops,
0fb4aa4b 12865 from_tty,
84f4c1fe
PM
12866 1 /* enabled */,
12867 0 /* internal */))
0fb4aa4b
PA
12868 set_tracepoint_count (breakpoint_count);
12869}
12870
12871/* strace command implementation. Creates a static tracepoint. */
12872
12873void
12874strace_command (char *arg, int from_tty)
12875{
12876 if (create_breakpoint (get_current_arch (),
12877 arg,
12878 NULL, 0, 1 /* parse arg */,
12879 0 /* tempflag */,
12880 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
12881 0 /* Ignore count */,
12882 pending_break_support,
348d480f 12883 &tracepoint_breakpoint_ops,
8cdf0e15 12884 from_tty,
84f4c1fe
PM
12885 1 /* enabled */,
12886 0 /* internal */))
fd9b8c24 12887 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
12888}
12889
409873ef
SS
12890/* Set up a fake reader function that gets command lines from a linked
12891 list that was acquired during tracepoint uploading. */
12892
12893static struct uploaded_tp *this_utp;
3149d8c1 12894static int next_cmd;
409873ef
SS
12895
12896static char *
12897read_uploaded_action (void)
12898{
12899 char *rslt;
12900
3149d8c1 12901 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 12902
3149d8c1 12903 next_cmd++;
409873ef
SS
12904
12905 return rslt;
12906}
12907
00bf0b85
SS
12908/* Given information about a tracepoint as recorded on a target (which
12909 can be either a live system or a trace file), attempt to create an
12910 equivalent GDB tracepoint. This is not a reliable process, since
12911 the target does not necessarily have all the information used when
12912 the tracepoint was originally defined. */
12913
d9b3f62e 12914struct tracepoint *
00bf0b85 12915create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 12916{
409873ef 12917 char *addr_str, small_buf[100];
d9b3f62e 12918 struct tracepoint *tp;
fd9b8c24 12919
409873ef
SS
12920 if (utp->at_string)
12921 addr_str = utp->at_string;
12922 else
12923 {
12924 /* In the absence of a source location, fall back to raw
12925 address. Since there is no way to confirm that the address
12926 means the same thing as when the trace was started, warn the
12927 user. */
3e43a32a
MS
12928 warning (_("Uploaded tracepoint %d has no "
12929 "source location, using raw address"),
409873ef
SS
12930 utp->number);
12931 sprintf (small_buf, "*%s", hex_string (utp->addr));
12932 addr_str = small_buf;
12933 }
12934
12935 /* There's not much we can do with a sequence of bytecodes. */
12936 if (utp->cond && !utp->cond_string)
3e43a32a
MS
12937 warning (_("Uploaded tracepoint %d condition "
12938 "has no source form, ignoring it"),
409873ef 12939 utp->number);
d5551862 12940
8cdf0e15 12941 if (!create_breakpoint (get_current_arch (),
409873ef
SS
12942 addr_str,
12943 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 12944 0 /* tempflag */,
0fb4aa4b 12945 utp->type /* type_wanted */,
8cdf0e15
VP
12946 0 /* Ignore count */,
12947 pending_break_support,
348d480f 12948 &tracepoint_breakpoint_ops,
8cdf0e15 12949 0 /* from_tty */,
84f4c1fe
PM
12950 utp->enabled /* enabled */,
12951 0 /* internal */))
fd9b8c24
PA
12952 return NULL;
12953
00bf0b85
SS
12954 set_tracepoint_count (breakpoint_count);
12955
409873ef 12956 /* Get the tracepoint we just created. */
fd9b8c24
PA
12957 tp = get_tracepoint (tracepoint_count);
12958 gdb_assert (tp != NULL);
d5551862 12959
00bf0b85
SS
12960 if (utp->pass > 0)
12961 {
d9b3f62e 12962 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 12963
409873ef 12964 trace_pass_command (small_buf, 0);
00bf0b85
SS
12965 }
12966
409873ef
SS
12967 /* If we have uploaded versions of the original commands, set up a
12968 special-purpose "reader" function and call the usual command line
12969 reader, then pass the result to the breakpoint command-setting
12970 function. */
3149d8c1 12971 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 12972 {
409873ef 12973 struct command_line *cmd_list;
00bf0b85 12974
409873ef 12975 this_utp = utp;
3149d8c1 12976 next_cmd = 0;
d5551862 12977
409873ef
SS
12978 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12979
d9b3f62e 12980 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 12981 }
3149d8c1
SS
12982 else if (!VEC_empty (char_ptr, utp->actions)
12983 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12984 warning (_("Uploaded tracepoint %d actions "
12985 "have no source form, ignoring them"),
409873ef 12986 utp->number);
00bf0b85 12987
f196051f
SS
12988 /* Copy any status information that might be available. */
12989 tp->base.hit_count = utp->hit_count;
12990 tp->traceframe_usage = utp->traceframe_usage;
12991
00bf0b85 12992 return tp;
d9b3f62e 12993}
00bf0b85 12994
1042e4c0
SS
12995/* Print information on tracepoint number TPNUM_EXP, or all if
12996 omitted. */
12997
12998static void
e5a67952 12999tracepoints_info (char *args, int from_tty)
1042e4c0 13000{
79a45e25 13001 struct ui_out *uiout = current_uiout;
e5a67952 13002 int num_printed;
1042e4c0 13003
e5a67952 13004 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
13005
13006 if (num_printed == 0)
1042e4c0 13007 {
e5a67952 13008 if (args == NULL || *args == '\0')
d77f58be
SS
13009 ui_out_message (uiout, 0, "No tracepoints.\n");
13010 else
e5a67952 13011 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 13012 }
ad443146
SS
13013
13014 default_collect_info ();
1042e4c0
SS
13015}
13016
4a64f543 13017/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
13018 Not supported by all targets. */
13019static void
13020enable_trace_command (char *args, int from_tty)
13021{
13022 enable_command (args, from_tty);
13023}
13024
4a64f543 13025/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
13026 Not supported by all targets. */
13027static void
13028disable_trace_command (char *args, int from_tty)
13029{
13030 disable_command (args, from_tty);
13031}
13032
4a64f543 13033/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
13034static void
13035delete_trace_command (char *arg, int from_tty)
13036{
35df4500 13037 struct breakpoint *b, *b_tmp;
1042e4c0
SS
13038
13039 dont_repeat ();
13040
13041 if (arg == 0)
13042 {
13043 int breaks_to_delete = 0;
13044
13045 /* Delete all breakpoints if no argument.
13046 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
13047 have to be deleted with an explicit breakpoint number
13048 argument. */
1042e4c0 13049 ALL_TRACEPOINTS (b)
46c6471b 13050 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
13051 {
13052 breaks_to_delete = 1;
13053 break;
13054 }
1042e4c0
SS
13055
13056 /* Ask user only if there are some breakpoints to delete. */
13057 if (!from_tty
13058 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13059 {
35df4500 13060 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13061 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 13062 delete_breakpoint (b);
1042e4c0
SS
13063 }
13064 }
13065 else
51be5b68 13066 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
13067}
13068
197f0a60
TT
13069/* Helper function for trace_pass_command. */
13070
13071static void
d9b3f62e 13072trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 13073{
d9b3f62e
PA
13074 tp->pass_count = count;
13075 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
13076 if (from_tty)
13077 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 13078 tp->base.number, count);
197f0a60
TT
13079}
13080
1042e4c0
SS
13081/* Set passcount for tracepoint.
13082
13083 First command argument is passcount, second is tracepoint number.
13084 If tracepoint number omitted, apply to most recently defined.
13085 Also accepts special argument "all". */
13086
13087static void
13088trace_pass_command (char *args, int from_tty)
13089{
d9b3f62e 13090 struct tracepoint *t1;
1042e4c0 13091 unsigned int count;
1042e4c0
SS
13092
13093 if (args == 0 || *args == 0)
3e43a32a
MS
13094 error (_("passcount command requires an "
13095 "argument (count + optional TP num)"));
1042e4c0 13096
4a64f543 13097 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
13098
13099 while (*args && isspace ((int) *args))
13100 args++;
13101
13102 if (*args && strncasecmp (args, "all", 3) == 0)
13103 {
d9b3f62e
PA
13104 struct breakpoint *b;
13105
1042e4c0 13106 args += 3; /* Skip special argument "all". */
1042e4c0
SS
13107 if (*args)
13108 error (_("Junk at end of arguments."));
1042e4c0 13109
d9b3f62e 13110 ALL_TRACEPOINTS (b)
197f0a60 13111 {
d9b3f62e 13112 t1 = (struct tracepoint *) b;
197f0a60
TT
13113 trace_pass_set_count (t1, count, from_tty);
13114 }
13115 }
13116 else if (*args == '\0')
1042e4c0 13117 {
197f0a60 13118 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 13119 if (t1)
197f0a60
TT
13120 trace_pass_set_count (t1, count, from_tty);
13121 }
13122 else
13123 {
13124 struct get_number_or_range_state state;
13125
13126 init_number_or_range (&state, args);
13127 while (!state.finished)
1042e4c0 13128 {
197f0a60
TT
13129 t1 = get_tracepoint_by_number (&args, &state, 1);
13130 if (t1)
13131 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
13132 }
13133 }
1042e4c0
SS
13134}
13135
d9b3f62e 13136struct tracepoint *
1042e4c0
SS
13137get_tracepoint (int num)
13138{
13139 struct breakpoint *t;
13140
13141 ALL_TRACEPOINTS (t)
13142 if (t->number == num)
d9b3f62e 13143 return (struct tracepoint *) t;
1042e4c0
SS
13144
13145 return NULL;
13146}
13147
d5551862
SS
13148/* Find the tracepoint with the given target-side number (which may be
13149 different from the tracepoint number after disconnecting and
13150 reconnecting). */
13151
d9b3f62e 13152struct tracepoint *
d5551862
SS
13153get_tracepoint_by_number_on_target (int num)
13154{
d9b3f62e 13155 struct breakpoint *b;
d5551862 13156
d9b3f62e
PA
13157 ALL_TRACEPOINTS (b)
13158 {
13159 struct tracepoint *t = (struct tracepoint *) b;
13160
13161 if (t->number_on_target == num)
13162 return t;
13163 }
d5551862
SS
13164
13165 return NULL;
13166}
13167
1042e4c0 13168/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
13169 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13170 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 13171 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 13172struct tracepoint *
197f0a60
TT
13173get_tracepoint_by_number (char **arg,
13174 struct get_number_or_range_state *state,
13175 int optional_p)
1042e4c0
SS
13176{
13177 extern int tracepoint_count;
13178 struct breakpoint *t;
13179 int tpnum;
13180 char *instring = arg == NULL ? NULL : *arg;
13181
197f0a60
TT
13182 if (state)
13183 {
13184 gdb_assert (!state->finished);
13185 tpnum = get_number_or_range (state);
13186 }
13187 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
13188 {
13189 if (optional_p)
13190 tpnum = tracepoint_count;
13191 else
13192 error_no_arg (_("tracepoint number"));
13193 }
13194 else
197f0a60 13195 tpnum = get_number (arg);
1042e4c0
SS
13196
13197 if (tpnum <= 0)
13198 {
13199 if (instring && *instring)
13200 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
13201 instring);
13202 else
3e43a32a
MS
13203 printf_filtered (_("Tracepoint argument missing "
13204 "and no previous tracepoint\n"));
1042e4c0
SS
13205 return NULL;
13206 }
13207
13208 ALL_TRACEPOINTS (t)
13209 if (t->number == tpnum)
13210 {
d9b3f62e 13211 return (struct tracepoint *) t;
1042e4c0
SS
13212 }
13213
1042e4c0
SS
13214 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
13215 return NULL;
13216}
13217
d9b3f62e
PA
13218void
13219print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
13220{
13221 if (b->thread != -1)
13222 fprintf_unfiltered (fp, " thread %d", b->thread);
13223
13224 if (b->task != 0)
13225 fprintf_unfiltered (fp, " task %d", b->task);
13226
13227 fprintf_unfiltered (fp, "\n");
13228}
13229
6149aea9
PA
13230/* Save information on user settable breakpoints (watchpoints, etc) to
13231 a new script file named FILENAME. If FILTER is non-NULL, call it
13232 on each breakpoint and only include the ones for which it returns
13233 non-zero. */
13234
1042e4c0 13235static void
6149aea9
PA
13236save_breakpoints (char *filename, int from_tty,
13237 int (*filter) (const struct breakpoint *))
1042e4c0
SS
13238{
13239 struct breakpoint *tp;
6149aea9 13240 int any = 0;
a7bdde9e 13241 char *pathname;
1042e4c0 13242 struct cleanup *cleanup;
a7bdde9e 13243 struct ui_file *fp;
6149aea9 13244 int extra_trace_bits = 0;
1042e4c0 13245
6149aea9
PA
13246 if (filename == 0 || *filename == 0)
13247 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
13248
13249 /* See if we have anything to save. */
6149aea9 13250 ALL_BREAKPOINTS (tp)
1042e4c0 13251 {
6149aea9 13252 /* Skip internal and momentary breakpoints. */
09d682a4 13253 if (!user_breakpoint_p (tp))
6149aea9
PA
13254 continue;
13255
13256 /* If we have a filter, only save the breakpoints it accepts. */
13257 if (filter && !filter (tp))
13258 continue;
13259
13260 any = 1;
13261
13262 if (is_tracepoint (tp))
13263 {
13264 extra_trace_bits = 1;
13265
13266 /* We can stop searching. */
13267 break;
13268 }
1042e4c0 13269 }
6149aea9
PA
13270
13271 if (!any)
1042e4c0 13272 {
6149aea9 13273 warning (_("Nothing to save."));
1042e4c0
SS
13274 return;
13275 }
13276
6149aea9 13277 pathname = tilde_expand (filename);
1042e4c0 13278 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 13279 fp = gdb_fopen (pathname, "w");
059fb39f 13280 if (!fp)
6149aea9
PA
13281 error (_("Unable to open file '%s' for saving (%s)"),
13282 filename, safe_strerror (errno));
a7bdde9e 13283 make_cleanup_ui_file_delete (fp);
8bf6485c 13284
6149aea9
PA
13285 if (extra_trace_bits)
13286 save_trace_state_variables (fp);
8bf6485c 13287
6149aea9 13288 ALL_BREAKPOINTS (tp)
1042e4c0 13289 {
6149aea9 13290 /* Skip internal and momentary breakpoints. */
09d682a4 13291 if (!user_breakpoint_p (tp))
6149aea9 13292 continue;
8bf6485c 13293
6149aea9
PA
13294 /* If we have a filter, only save the breakpoints it accepts. */
13295 if (filter && !filter (tp))
13296 continue;
13297
348d480f 13298 tp->ops->print_recreate (tp, fp);
1042e4c0 13299
6149aea9
PA
13300 /* Note, we can't rely on tp->number for anything, as we can't
13301 assume the recreated breakpoint numbers will match. Use $bpnum
13302 instead. */
13303
13304 if (tp->cond_string)
13305 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
13306
13307 if (tp->ignore_count)
13308 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
13309
a7bdde9e 13310 if (tp->commands)
1042e4c0 13311 {
a7bdde9e
VP
13312 volatile struct gdb_exception ex;
13313
6149aea9 13314 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 13315
79a45e25 13316 ui_out_redirect (current_uiout, fp);
14dba4b4 13317 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 13318 {
79a45e25 13319 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 13320 }
79a45e25 13321 ui_out_redirect (current_uiout, NULL);
1042e4c0 13322
a7bdde9e
VP
13323 if (ex.reason < 0)
13324 throw_exception (ex);
1042e4c0 13325
a7bdde9e 13326 fprintf_unfiltered (fp, " end\n");
1042e4c0 13327 }
6149aea9
PA
13328
13329 if (tp->enable_state == bp_disabled)
13330 fprintf_unfiltered (fp, "disable\n");
13331
13332 /* If this is a multi-location breakpoint, check if the locations
13333 should be individually disabled. Watchpoint locations are
13334 special, and not user visible. */
13335 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13336 {
13337 struct bp_location *loc;
13338 int n = 1;
13339
13340 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
13341 if (!loc->enabled)
13342 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
13343 }
1042e4c0 13344 }
8bf6485c 13345
6149aea9 13346 if (extra_trace_bits && *default_collect)
8bf6485c
SS
13347 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
13348
1042e4c0
SS
13349 do_cleanups (cleanup);
13350 if (from_tty)
6149aea9
PA
13351 printf_filtered (_("Saved to file '%s'.\n"), filename);
13352}
13353
13354/* The `save breakpoints' command. */
13355
13356static void
13357save_breakpoints_command (char *args, int from_tty)
13358{
13359 save_breakpoints (args, from_tty, NULL);
13360}
13361
13362/* The `save tracepoints' command. */
13363
13364static void
13365save_tracepoints_command (char *args, int from_tty)
13366{
13367 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
13368}
13369
13370/* Create a vector of all tracepoints. */
13371
13372VEC(breakpoint_p) *
eeae04df 13373all_tracepoints (void)
1042e4c0
SS
13374{
13375 VEC(breakpoint_p) *tp_vec = 0;
13376 struct breakpoint *tp;
13377
13378 ALL_TRACEPOINTS (tp)
13379 {
13380 VEC_safe_push (breakpoint_p, tp_vec, tp);
13381 }
13382
13383 return tp_vec;
13384}
13385
c906108c 13386\f
4a64f543
MS
13387/* This help string is used for the break, hbreak, tbreak and thbreak
13388 commands. It is defined as a macro to prevent duplication.
13389 COMMAND should be a string constant containing the name of the
13390 command. */
31e2b00f
AS
13391#define BREAK_ARGS_HELP(command) \
13392command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
13393LOCATION may be a line number, function name, or \"*\" and an address.\n\
13394If a line number is specified, break at start of code for that line.\n\
13395If a function is specified, break at start of code for that function.\n\
13396If an address is specified, break at that exact address.\n\
dc10affe
PA
13397With no LOCATION, uses current execution address of the selected\n\
13398stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
13399\n\
13400THREADNUM is the number from \"info threads\".\n\
13401CONDITION is a boolean expression.\n\
13402\n\
d41c0fc8
PA
13403Multiple breakpoints at one place are permitted, and useful if their\n\
13404conditions are different.\n\
31e2b00f
AS
13405\n\
13406Do \"help breakpoints\" for info on other commands dealing with breakpoints."
13407
44feb3ce
TT
13408/* List of subcommands for "catch". */
13409static struct cmd_list_element *catch_cmdlist;
13410
13411/* List of subcommands for "tcatch". */
13412static struct cmd_list_element *tcatch_cmdlist;
13413
9ac4176b 13414void
44feb3ce
TT
13415add_catch_command (char *name, char *docstring,
13416 void (*sfunc) (char *args, int from_tty,
13417 struct cmd_list_element *command),
a96d9b2e
SDJ
13418 char **(*completer) (struct cmd_list_element *cmd,
13419 char *text, char *word),
44feb3ce
TT
13420 void *user_data_catch,
13421 void *user_data_tcatch)
13422{
13423 struct cmd_list_element *command;
13424
13425 command = add_cmd (name, class_breakpoint, NULL, docstring,
13426 &catch_cmdlist);
13427 set_cmd_sfunc (command, sfunc);
13428 set_cmd_context (command, user_data_catch);
a96d9b2e 13429 set_cmd_completer (command, completer);
44feb3ce
TT
13430
13431 command = add_cmd (name, class_breakpoint, NULL, docstring,
13432 &tcatch_cmdlist);
13433 set_cmd_sfunc (command, sfunc);
13434 set_cmd_context (command, user_data_tcatch);
a96d9b2e 13435 set_cmd_completer (command, completer);
44feb3ce
TT
13436}
13437
6c95b8df 13438static void
a79b8f6e 13439clear_syscall_counts (struct inferior *inf)
6c95b8df 13440{
6c95b8df
PA
13441 inf->total_syscalls_count = 0;
13442 inf->any_syscall_count = 0;
13443 VEC_free (int, inf->syscalls_counts);
13444}
13445
6149aea9
PA
13446static void
13447save_command (char *arg, int from_tty)
13448{
3e43a32a
MS
13449 printf_unfiltered (_("\"save\" must be followed by "
13450 "the name of a save subcommand.\n"));
6149aea9
PA
13451 help_list (save_cmdlist, "save ", -1, gdb_stdout);
13452}
13453
84f4c1fe
PM
13454struct breakpoint *
13455iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
13456 void *data)
13457{
35df4500 13458 struct breakpoint *b, *b_tmp;
84f4c1fe 13459
35df4500 13460 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
13461 {
13462 if ((*callback) (b, data))
13463 return b;
13464 }
13465
13466 return NULL;
13467}
13468
0574c78f
GB
13469/* Zero if any of the breakpoint's locations could be a location where
13470 functions have been inlined, nonzero otherwise. */
13471
13472static int
13473is_non_inline_function (struct breakpoint *b)
13474{
13475 /* The shared library event breakpoint is set on the address of a
13476 non-inline function. */
13477 if (b->type == bp_shlib_event)
13478 return 1;
13479
13480 return 0;
13481}
13482
13483/* Nonzero if the specified PC cannot be a location where functions
13484 have been inlined. */
13485
13486int
13487pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc)
13488{
13489 struct breakpoint *b;
13490 struct bp_location *bl;
13491
13492 ALL_BREAKPOINTS (b)
13493 {
13494 if (!is_non_inline_function (b))
13495 continue;
13496
13497 for (bl = b->loc; bl != NULL; bl = bl->next)
13498 {
13499 if (!bl->shlib_disabled
13500 && bpstat_check_location (bl, aspace, pc))
13501 return 1;
13502 }
13503 }
13504
13505 return 0;
13506}
13507
2060206e
PA
13508void
13509initialize_breakpoint_ops (void)
13510{
13511 static int initialized = 0;
13512
13513 struct breakpoint_ops *ops;
13514
13515 if (initialized)
13516 return;
13517 initialized = 1;
13518
13519 /* The breakpoint_ops structure to be inherit by all kinds of
13520 breakpoints (real breakpoints, i.e., user "break" breakpoints,
13521 internal and momentary breakpoints, etc.). */
13522 ops = &bkpt_base_breakpoint_ops;
13523 *ops = base_breakpoint_ops;
13524 ops->re_set = bkpt_re_set;
13525 ops->insert_location = bkpt_insert_location;
13526 ops->remove_location = bkpt_remove_location;
13527 ops->breakpoint_hit = bkpt_breakpoint_hit;
13528
13529 /* The breakpoint_ops structure to be used in regular breakpoints. */
13530 ops = &bkpt_breakpoint_ops;
13531 *ops = bkpt_base_breakpoint_ops;
13532 ops->re_set = bkpt_re_set;
13533 ops->resources_needed = bkpt_resources_needed;
13534 ops->print_it = bkpt_print_it;
13535 ops->print_mention = bkpt_print_mention;
13536 ops->print_recreate = bkpt_print_recreate;
13537
13538 /* Ranged breakpoints. */
13539 ops = &ranged_breakpoint_ops;
13540 *ops = bkpt_breakpoint_ops;
13541 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
13542 ops->resources_needed = resources_needed_ranged_breakpoint;
13543 ops->print_it = print_it_ranged_breakpoint;
13544 ops->print_one = print_one_ranged_breakpoint;
13545 ops->print_one_detail = print_one_detail_ranged_breakpoint;
13546 ops->print_mention = print_mention_ranged_breakpoint;
13547 ops->print_recreate = print_recreate_ranged_breakpoint;
13548
13549 /* Internal breakpoints. */
13550 ops = &internal_breakpoint_ops;
13551 *ops = bkpt_base_breakpoint_ops;
13552 ops->re_set = internal_bkpt_re_set;
13553 ops->check_status = internal_bkpt_check_status;
13554 ops->print_it = internal_bkpt_print_it;
13555 ops->print_mention = internal_bkpt_print_mention;
13556
13557 /* Momentary breakpoints. */
13558 ops = &momentary_breakpoint_ops;
13559 *ops = bkpt_base_breakpoint_ops;
13560 ops->re_set = momentary_bkpt_re_set;
13561 ops->check_status = momentary_bkpt_check_status;
13562 ops->print_it = momentary_bkpt_print_it;
13563 ops->print_mention = momentary_bkpt_print_mention;
13564
13565 /* GNU v3 exception catchpoints. */
13566 ops = &gnu_v3_exception_catchpoint_ops;
13567 *ops = bkpt_breakpoint_ops;
13568 ops->print_it = print_it_exception_catchpoint;
13569 ops->print_one = print_one_exception_catchpoint;
13570 ops->print_mention = print_mention_exception_catchpoint;
13571 ops->print_recreate = print_recreate_exception_catchpoint;
13572
13573 /* Watchpoints. */
13574 ops = &watchpoint_breakpoint_ops;
13575 *ops = base_breakpoint_ops;
3a5c3e22 13576 ops->dtor = dtor_watchpoint;
2060206e
PA
13577 ops->re_set = re_set_watchpoint;
13578 ops->insert_location = insert_watchpoint;
13579 ops->remove_location = remove_watchpoint;
13580 ops->breakpoint_hit = breakpoint_hit_watchpoint;
13581 ops->check_status = check_status_watchpoint;
13582 ops->resources_needed = resources_needed_watchpoint;
13583 ops->works_in_software_mode = works_in_software_mode_watchpoint;
13584 ops->print_it = print_it_watchpoint;
13585 ops->print_mention = print_mention_watchpoint;
13586 ops->print_recreate = print_recreate_watchpoint;
13587
13588 /* Masked watchpoints. */
13589 ops = &masked_watchpoint_breakpoint_ops;
13590 *ops = watchpoint_breakpoint_ops;
13591 ops->insert_location = insert_masked_watchpoint;
13592 ops->remove_location = remove_masked_watchpoint;
13593 ops->resources_needed = resources_needed_masked_watchpoint;
13594 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
13595 ops->print_it = print_it_masked_watchpoint;
13596 ops->print_one_detail = print_one_detail_masked_watchpoint;
13597 ops->print_mention = print_mention_masked_watchpoint;
13598 ops->print_recreate = print_recreate_masked_watchpoint;
13599
13600 /* Tracepoints. */
13601 ops = &tracepoint_breakpoint_ops;
13602 *ops = base_breakpoint_ops;
13603 ops->re_set = tracepoint_re_set;
13604 ops->breakpoint_hit = tracepoint_breakpoint_hit;
13605 ops->print_one_detail = tracepoint_print_one_detail;
13606 ops->print_mention = tracepoint_print_mention;
13607 ops->print_recreate = tracepoint_print_recreate;
13608
13609 /* Fork catchpoints. */
13610 ops = &catch_fork_breakpoint_ops;
13611 *ops = base_breakpoint_ops;
13612 ops->insert_location = insert_catch_fork;
13613 ops->remove_location = remove_catch_fork;
13614 ops->breakpoint_hit = breakpoint_hit_catch_fork;
13615 ops->print_it = print_it_catch_fork;
13616 ops->print_one = print_one_catch_fork;
13617 ops->print_mention = print_mention_catch_fork;
13618 ops->print_recreate = print_recreate_catch_fork;
13619
13620 /* Vfork catchpoints. */
13621 ops = &catch_vfork_breakpoint_ops;
13622 *ops = base_breakpoint_ops;
13623 ops->insert_location = insert_catch_vfork;
13624 ops->remove_location = remove_catch_vfork;
13625 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
13626 ops->print_it = print_it_catch_vfork;
13627 ops->print_one = print_one_catch_vfork;
13628 ops->print_mention = print_mention_catch_vfork;
13629 ops->print_recreate = print_recreate_catch_vfork;
13630
13631 /* Exec catchpoints. */
13632 ops = &catch_exec_breakpoint_ops;
13633 *ops = base_breakpoint_ops;
13634 ops->dtor = dtor_catch_exec;
13635 ops->insert_location = insert_catch_exec;
13636 ops->remove_location = remove_catch_exec;
13637 ops->breakpoint_hit = breakpoint_hit_catch_exec;
13638 ops->print_it = print_it_catch_exec;
13639 ops->print_one = print_one_catch_exec;
13640 ops->print_mention = print_mention_catch_exec;
13641 ops->print_recreate = print_recreate_catch_exec;
13642
13643 /* Syscall catchpoints. */
13644 ops = &catch_syscall_breakpoint_ops;
13645 *ops = base_breakpoint_ops;
13646 ops->dtor = dtor_catch_syscall;
13647 ops->insert_location = insert_catch_syscall;
13648 ops->remove_location = remove_catch_syscall;
13649 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
13650 ops->print_it = print_it_catch_syscall;
13651 ops->print_one = print_one_catch_syscall;
13652 ops->print_mention = print_mention_catch_syscall;
13653 ops->print_recreate = print_recreate_catch_syscall;
13654}
13655
c906108c 13656void
fba45db2 13657_initialize_breakpoint (void)
c906108c
SS
13658{
13659 struct cmd_list_element *c;
13660
2060206e
PA
13661 initialize_breakpoint_ops ();
13662
84acb35a 13663 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 13664 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 13665 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 13666
17450429
PP
13667 breakpoint_objfile_key = register_objfile_data ();
13668
c906108c
SS
13669 breakpoint_chain = 0;
13670 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
13671 before a breakpoint is set. */
13672 breakpoint_count = 0;
13673
1042e4c0
SS
13674 tracepoint_count = 0;
13675
1bedd215
AC
13676 add_com ("ignore", class_breakpoint, ignore_command, _("\
13677Set ignore-count of breakpoint number N to COUNT.\n\
13678Usage is `ignore N COUNT'."));
c906108c 13679 if (xdb_commands)
c5aa993b 13680 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 13681
1bedd215
AC
13682 add_com ("commands", class_breakpoint, commands_command, _("\
13683Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
13684Give breakpoint number as argument after \"commands\".\n\
13685With no argument, the targeted breakpoint is the last one set.\n\
13686The commands themselves follow starting on the next line.\n\
13687Type a line containing \"end\" to indicate the end of them.\n\
13688Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 13689then no output is printed when it is hit, except what the commands print."));
c906108c 13690
1bedd215
AC
13691 add_com ("condition", class_breakpoint, condition_command, _("\
13692Specify breakpoint number N to break only if COND is true.\n\
c906108c 13693Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 13694expression to be evaluated whenever breakpoint N is reached."));
c906108c 13695
1bedd215 13696 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 13697Set a temporary breakpoint.\n\
c906108c
SS
13698Like \"break\" except the breakpoint is only temporary,\n\
13699so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
13700by using \"enable delete\" on the breakpoint number.\n\
13701\n"
13702BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 13703 set_cmd_completer (c, location_completer);
c94fdfd0 13704
1bedd215 13705 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 13706Set a hardware assisted breakpoint.\n\
c906108c 13707Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
13708some target hardware may not have this support.\n\
13709\n"
13710BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 13711 set_cmd_completer (c, location_completer);
c906108c 13712
1bedd215 13713 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 13714Set a temporary hardware assisted breakpoint.\n\
c906108c 13715Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
13716so it will be deleted when hit.\n\
13717\n"
13718BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 13719 set_cmd_completer (c, location_completer);
c906108c 13720
1bedd215
AC
13721 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
13722Enable some breakpoints.\n\
c906108c
SS
13723Give breakpoint numbers (separated by spaces) as arguments.\n\
13724With no subcommand, breakpoints are enabled until you command otherwise.\n\
13725This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13726With a subcommand you can enable temporarily."),
c906108c
SS
13727 &enablelist, "enable ", 1, &cmdlist);
13728 if (xdb_commands)
1bedd215
AC
13729 add_com ("ab", class_breakpoint, enable_command, _("\
13730Enable some breakpoints.\n\
c906108c
SS
13731Give breakpoint numbers (separated by spaces) as arguments.\n\
13732With no subcommand, breakpoints are enabled until you command otherwise.\n\
13733This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13734With a subcommand you can enable temporarily."));
c906108c
SS
13735
13736 add_com_alias ("en", "enable", class_breakpoint, 1);
13737
84951ab5 13738 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 13739Enable some breakpoints.\n\
c906108c
SS
13740Give breakpoint numbers (separated by spaces) as arguments.\n\
13741This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13742May be abbreviated to simply \"enable\".\n"),
c5aa993b 13743 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 13744
1a966eab
AC
13745 add_cmd ("once", no_class, enable_once_command, _("\
13746Enable breakpoints for one hit. Give breakpoint numbers.\n\
13747If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13748 &enablebreaklist);
13749
1a966eab
AC
13750 add_cmd ("delete", no_class, enable_delete_command, _("\
13751Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13752If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13753 &enablebreaklist);
13754
1a966eab
AC
13755 add_cmd ("delete", no_class, enable_delete_command, _("\
13756Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13757If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13758 &enablelist);
13759
1a966eab
AC
13760 add_cmd ("once", no_class, enable_once_command, _("\
13761Enable breakpoints for one hit. Give breakpoint numbers.\n\
13762If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13763 &enablelist);
13764
1bedd215
AC
13765 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13766Disable some breakpoints.\n\
c906108c
SS
13767Arguments are breakpoint numbers with spaces in between.\n\
13768To disable all breakpoints, give no argument.\n\
64b9b334 13769A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
13770 &disablelist, "disable ", 1, &cmdlist);
13771 add_com_alias ("dis", "disable", class_breakpoint, 1);
13772 add_com_alias ("disa", "disable", class_breakpoint, 1);
13773 if (xdb_commands)
1bedd215
AC
13774 add_com ("sb", class_breakpoint, disable_command, _("\
13775Disable some breakpoints.\n\
c906108c
SS
13776Arguments are breakpoint numbers with spaces in between.\n\
13777To disable all breakpoints, give no argument.\n\
64b9b334 13778A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 13779
1a966eab
AC
13780 add_cmd ("breakpoints", class_alias, disable_command, _("\
13781Disable some breakpoints.\n\
c906108c
SS
13782Arguments are breakpoint numbers with spaces in between.\n\
13783To disable all breakpoints, give no argument.\n\
64b9b334 13784A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 13785This command may be abbreviated \"disable\"."),
c906108c
SS
13786 &disablelist);
13787
1bedd215
AC
13788 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13789Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13790Arguments are breakpoint numbers with spaces in between.\n\
13791To delete all breakpoints, give no argument.\n\
13792\n\
13793Also a prefix command for deletion of other GDB objects.\n\
1bedd215 13794The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
13795 &deletelist, "delete ", 1, &cmdlist);
13796 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 13797 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 13798 if (xdb_commands)
1bedd215
AC
13799 add_com ("db", class_breakpoint, delete_command, _("\
13800Delete some breakpoints.\n\
c906108c 13801Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 13802To delete all breakpoints, give no argument.\n"));
c906108c 13803
1a966eab
AC
13804 add_cmd ("breakpoints", class_alias, delete_command, _("\
13805Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13806Arguments are breakpoint numbers with spaces in between.\n\
13807To delete all breakpoints, give no argument.\n\
1a966eab 13808This command may be abbreviated \"delete\"."),
c906108c
SS
13809 &deletelist);
13810
1bedd215
AC
13811 add_com ("clear", class_breakpoint, clear_command, _("\
13812Clear breakpoint at specified line or function.\n\
c906108c
SS
13813Argument may be line number, function name, or \"*\" and an address.\n\
13814If line number is specified, all breakpoints in that line are cleared.\n\
13815If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
13816If an address is specified, breakpoints at that address are cleared.\n\
13817\n\
13818With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
13819is executing in.\n\
13820\n\
1bedd215 13821See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 13822 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 13823
1bedd215 13824 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
13825Set breakpoint at specified line or function.\n"
13826BREAK_ARGS_HELP ("break")));
5ba2abeb 13827 set_cmd_completer (c, location_completer);
c94fdfd0 13828
c906108c
SS
13829 add_com_alias ("b", "break", class_run, 1);
13830 add_com_alias ("br", "break", class_run, 1);
13831 add_com_alias ("bre", "break", class_run, 1);
13832 add_com_alias ("brea", "break", class_run, 1);
13833
7681d515
PM
13834 if (xdb_commands)
13835 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
13836
13837 if (dbx_commands)
13838 {
1bedd215
AC
13839 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13840Break in function/address or break at a line in the current file."),
c5aa993b
JM
13841 &stoplist, "stop ", 1, &cmdlist);
13842 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 13843 _("Break in function or address."), &stoplist);
c5aa993b 13844 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 13845 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
13846 add_com ("status", class_info, breakpoints_info, _("\
13847Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13848The \"Type\" column indicates one of:\n\
13849\tbreakpoint - normal breakpoint\n\
13850\twatchpoint - watchpoint\n\
13851The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13852the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13853breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13854address and file/line number respectively.\n\
13855\n\
13856Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13857are set to the address of the last breakpoint listed unless the command\n\
13858is prefixed with \"server \".\n\n\
c906108c 13859Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13860breakpoint set."));
c906108c
SS
13861 }
13862
1bedd215 13863 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 13864Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
13865The \"Type\" column indicates one of:\n\
13866\tbreakpoint - normal breakpoint\n\
13867\twatchpoint - watchpoint\n\
13868The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13869the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13870breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13871address and file/line number respectively.\n\
13872\n\
13873Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13874are set to the address of the last breakpoint listed unless the command\n\
13875is prefixed with \"server \".\n\n\
c906108c 13876Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13877breakpoint set."));
c906108c 13878
6b04bdb7
MS
13879 add_info_alias ("b", "breakpoints", 1);
13880
c906108c 13881 if (xdb_commands)
1bedd215
AC
13882 add_com ("lb", class_breakpoint, breakpoints_info, _("\
13883Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13884The \"Type\" column indicates one of:\n\
13885\tbreakpoint - normal breakpoint\n\
13886\twatchpoint - watchpoint\n\
13887The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13888the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13889breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13890address and file/line number respectively.\n\
13891\n\
13892Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13893are set to the address of the last breakpoint listed unless the command\n\
13894is prefixed with \"server \".\n\n\
c906108c 13895Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13896breakpoint set."));
c906108c 13897
1a966eab
AC
13898 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13899Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13900The \"Type\" column indicates one of:\n\
13901\tbreakpoint - normal breakpoint\n\
13902\twatchpoint - watchpoint\n\
13903\tlongjmp - internal breakpoint used to step through longjmp()\n\
13904\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13905\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
13906\tfinish - internal breakpoint used by the \"finish\" command\n\
13907The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
13908the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13909breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
13910address and file/line number respectively.\n\
13911\n\
13912Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13913are set to the address of the last breakpoint listed unless the command\n\
13914is prefixed with \"server \".\n\n\
c906108c 13915Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 13916breakpoint set."),
c906108c
SS
13917 &maintenanceinfolist);
13918
44feb3ce
TT
13919 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13920Set catchpoints to catch events."),
13921 &catch_cmdlist, "catch ",
13922 0/*allow-unknown*/, &cmdlist);
13923
13924 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13925Set temporary catchpoints to catch events."),
13926 &tcatch_cmdlist, "tcatch ",
13927 0/*allow-unknown*/, &cmdlist);
13928
13929 /* Add catch and tcatch sub-commands. */
13930 add_catch_command ("catch", _("\
13931Catch an exception, when caught.\n\
13932With an argument, catch only exceptions with the given name."),
13933 catch_catch_command,
a96d9b2e 13934 NULL,
44feb3ce
TT
13935 CATCH_PERMANENT,
13936 CATCH_TEMPORARY);
13937 add_catch_command ("throw", _("\
13938Catch an exception, when thrown.\n\
13939With an argument, catch only exceptions with the given name."),
13940 catch_throw_command,
a96d9b2e 13941 NULL,
44feb3ce
TT
13942 CATCH_PERMANENT,
13943 CATCH_TEMPORARY);
13944 add_catch_command ("fork", _("Catch calls to fork."),
13945 catch_fork_command_1,
a96d9b2e 13946 NULL,
44feb3ce
TT
13947 (void *) (uintptr_t) catch_fork_permanent,
13948 (void *) (uintptr_t) catch_fork_temporary);
13949 add_catch_command ("vfork", _("Catch calls to vfork."),
13950 catch_fork_command_1,
a96d9b2e 13951 NULL,
44feb3ce
TT
13952 (void *) (uintptr_t) catch_vfork_permanent,
13953 (void *) (uintptr_t) catch_vfork_temporary);
13954 add_catch_command ("exec", _("Catch calls to exec."),
13955 catch_exec_command_1,
a96d9b2e
SDJ
13956 NULL,
13957 CATCH_PERMANENT,
13958 CATCH_TEMPORARY);
13959 add_catch_command ("syscall", _("\
13960Catch system calls by their names and/or numbers.\n\
13961Arguments say which system calls to catch. If no arguments\n\
13962are given, every system call will be caught.\n\
13963Arguments, if given, should be one or more system call names\n\
13964(if your system supports that), or system call numbers."),
13965 catch_syscall_command_1,
13966 catch_syscall_completer,
44feb3ce
TT
13967 CATCH_PERMANENT,
13968 CATCH_TEMPORARY);
c5aa993b 13969
1bedd215
AC
13970 c = add_com ("watch", class_breakpoint, watch_command, _("\
13971Set a watchpoint for an expression.\n\
06a64a0b 13972Usage: watch [-l|-location] EXPRESSION\n\
c906108c 13973A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13974an expression changes.\n\
13975If -l or -location is given, this evaluates EXPRESSION and watches\n\
13976the memory to which it refers."));
65d12d83 13977 set_cmd_completer (c, expression_completer);
c906108c 13978
1bedd215
AC
13979 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13980Set a read watchpoint for an expression.\n\
06a64a0b 13981Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 13982A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13983an expression is read.\n\
13984If -l or -location is given, this evaluates EXPRESSION and watches\n\
13985the memory to which it refers."));
65d12d83 13986 set_cmd_completer (c, expression_completer);
c906108c 13987
1bedd215
AC
13988 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13989Set a watchpoint for an expression.\n\
06a64a0b 13990Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 13991A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13992an expression is either read or written.\n\
13993If -l or -location is given, this evaluates EXPRESSION and watches\n\
13994the memory to which it refers."));
65d12d83 13995 set_cmd_completer (c, expression_completer);
c906108c 13996
d77f58be 13997 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 13998Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 13999
920d2a44
AC
14000 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14001 respond to changes - contrary to the description. */
85c07804
AC
14002 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14003 &can_use_hw_watchpoints, _("\
14004Set debugger's willingness to use watchpoint hardware."), _("\
14005Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
14006If zero, gdb will not use hardware for new watchpoints, even if\n\
14007such is available. (However, any hardware watchpoints that were\n\
14008created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
14009hardware.)"),
14010 NULL,
920d2a44 14011 show_can_use_hw_watchpoints,
85c07804 14012 &setlist, &showlist);
c906108c
SS
14013
14014 can_use_hw_watchpoints = 1;
fa8d40ab 14015
1042e4c0
SS
14016 /* Tracepoint manipulation commands. */
14017
14018 c = add_com ("trace", class_breakpoint, trace_command, _("\
14019Set a tracepoint at specified line or function.\n\
14020\n"
14021BREAK_ARGS_HELP ("trace") "\n\
14022Do \"help tracepoints\" for info on other tracepoint commands."));
14023 set_cmd_completer (c, location_completer);
14024
14025 add_com_alias ("tp", "trace", class_alias, 0);
14026 add_com_alias ("tr", "trace", class_alias, 1);
14027 add_com_alias ("tra", "trace", class_alias, 1);
14028 add_com_alias ("trac", "trace", class_alias, 1);
14029
7a697b8d
SS
14030 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14031Set a fast tracepoint at specified line or function.\n\
14032\n"
14033BREAK_ARGS_HELP ("ftrace") "\n\
14034Do \"help tracepoints\" for info on other tracepoint commands."));
14035 set_cmd_completer (c, location_completer);
14036
0fb4aa4b
PA
14037 c = add_com ("strace", class_breakpoint, strace_command, _("\
14038Set a static tracepoint at specified line, function or marker.\n\
14039\n\
14040strace [LOCATION] [if CONDITION]\n\
14041LOCATION may be a line number, function name, \"*\" and an address,\n\
14042or -m MARKER_ID.\n\
14043If a line number is specified, probe the marker at start of code\n\
14044for that line. If a function is specified, probe the marker at start\n\
14045of code for that function. If an address is specified, probe the marker\n\
14046at that exact address. If a marker id is specified, probe the marker\n\
14047with that name. With no LOCATION, uses current execution address of\n\
14048the selected stack frame.\n\
14049Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14050This collects arbitrary user data passed in the probe point call to the\n\
14051tracing library. You can inspect it when analyzing the trace buffer,\n\
14052by printing the $_sdata variable like any other convenience variable.\n\
14053\n\
14054CONDITION is a boolean expression.\n\
14055\n\
d41c0fc8
PA
14056Multiple tracepoints at one place are permitted, and useful if their\n\
14057conditions are different.\n\
0fb4aa4b
PA
14058\n\
14059Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14060Do \"help tracepoints\" for info on other tracepoint commands."));
14061 set_cmd_completer (c, location_completer);
14062
1042e4c0 14063 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 14064Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
14065Convenience variable \"$tpnum\" contains the number of the\n\
14066last tracepoint set."));
14067
14068 add_info_alias ("tp", "tracepoints", 1);
14069
14070 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
14071Delete specified tracepoints.\n\
14072Arguments are tracepoint numbers, separated by spaces.\n\
14073No argument means delete all tracepoints."),
14074 &deletelist);
14075
14076 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14077Disable specified tracepoints.\n\
14078Arguments are tracepoint numbers, separated by spaces.\n\
14079No argument means disable all tracepoints."),
14080 &disablelist);
14081 deprecate_cmd (c, "disable");
14082
14083 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14084Enable specified tracepoints.\n\
14085Arguments are tracepoint numbers, separated by spaces.\n\
14086No argument means enable all tracepoints."),
14087 &enablelist);
14088 deprecate_cmd (c, "enable");
14089
14090 add_com ("passcount", class_trace, trace_pass_command, _("\
14091Set the passcount for a tracepoint.\n\
14092The trace will end when the tracepoint has been passed 'count' times.\n\
14093Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14094if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14095
6149aea9
PA
14096 add_prefix_cmd ("save", class_breakpoint, save_command,
14097 _("Save breakpoint definitions as a script."),
14098 &save_cmdlist, "save ",
14099 0/*allow-unknown*/, &cmdlist);
14100
14101 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14102Save current breakpoint definitions as a script.\n\
cce7e648 14103This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14104catchpoints, tracepoints). Use the 'source' command in another debug\n\
14105session to restore them."),
14106 &save_cmdlist);
14107 set_cmd_completer (c, filename_completer);
14108
14109 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14110Save current tracepoint definitions as a script.\n\
6149aea9
PA
14111Use the 'source' command in another debug session to restore them."),
14112 &save_cmdlist);
1042e4c0
SS
14113 set_cmd_completer (c, filename_completer);
14114
6149aea9
PA
14115 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
14116 deprecate_cmd (c, "save tracepoints");
14117
1bedd215 14118 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
14119Breakpoint specific settings\n\
14120Configure various breakpoint-specific variables such as\n\
1bedd215 14121pending breakpoint behavior"),
fa8d40ab
JJ
14122 &breakpoint_set_cmdlist, "set breakpoint ",
14123 0/*allow-unknown*/, &setlist);
1bedd215 14124 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
14125Breakpoint specific settings\n\
14126Configure various breakpoint-specific variables such as\n\
1bedd215 14127pending breakpoint behavior"),
fa8d40ab
JJ
14128 &breakpoint_show_cmdlist, "show breakpoint ",
14129 0/*allow-unknown*/, &showlist);
14130
7915a72c
AC
14131 add_setshow_auto_boolean_cmd ("pending", no_class,
14132 &pending_break_support, _("\
14133Set debugger's behavior regarding pending breakpoints."), _("\
14134Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14135If on, an unrecognized breakpoint location will cause gdb to create a\n\
14136pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14137an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14138user-query to see if a pending breakpoint should be created."),
2c5b56ce 14139 NULL,
920d2a44 14140 show_pending_break_support,
6e1d7d6c
AC
14141 &breakpoint_set_cmdlist,
14142 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14143
14144 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14145
14146 add_setshow_boolean_cmd ("auto-hw", no_class,
14147 &automatic_hardware_breakpoints, _("\
14148Set automatic usage of hardware breakpoints."), _("\
14149Show automatic usage of hardware breakpoints."), _("\
14150If set, the debugger will automatically use hardware breakpoints for\n\
14151breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14152a warning will be emitted for such breakpoints."),
14153 NULL,
14154 show_automatic_hardware_breakpoints,
14155 &breakpoint_set_cmdlist,
14156 &breakpoint_show_cmdlist);
74960c60 14157
33e5cbd6
PA
14158 add_setshow_enum_cmd ("always-inserted", class_support,
14159 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
14160Set mode for inserting breakpoints."), _("\
14161Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
14162When this mode is off, breakpoints are inserted in inferior when it is\n\
14163resumed, and removed when execution stops. When this mode is on,\n\
14164breakpoints are inserted immediately and removed only when the user\n\
14165deletes the breakpoint. When this mode is auto (which is the default),\n\
14166the behaviour depends on the non-stop setting (see help set non-stop).\n\
14167In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
14168behaves as if always-inserted mode is on; if gdb is controlling the\n\
14169inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
14170 NULL,
14171 &show_always_inserted_mode,
14172 &breakpoint_set_cmdlist,
14173 &breakpoint_show_cmdlist);
f1310107
TJB
14174
14175 add_com ("break-range", class_breakpoint, break_range_command, _("\
14176Set a breakpoint for an address range.\n\
14177break-range START-LOCATION, END-LOCATION\n\
14178where START-LOCATION and END-LOCATION can be one of the following:\n\
14179 LINENUM, for that line in the current file,\n\
14180 FILE:LINENUM, for that line in that file,\n\
14181 +OFFSET, for that number of lines after the current line\n\
14182 or the start of the range\n\
14183 FUNCTION, for the first line in that function,\n\
14184 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14185 *ADDRESS, for the instruction at that address.\n\
14186\n\
14187The breakpoint will stop execution of the inferior whenever it executes\n\
14188an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14189range (including START-LOCATION and END-LOCATION)."));
14190
765dc015 14191 automatic_hardware_breakpoints = 1;
f3b1572e
PA
14192
14193 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 14194}
This page took 2.112564 seconds and 4 git commands to generate.