2012-02-24 Luis Machado <lgustavo@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
0b302171 3 Copyright (C) 1986-2012 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
35#include "gdbthread.h"
36#include "target.h"
37#include "language.h"
38#include "gdb_string.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
0225421b 50#include "gdb_assert.h"
fe898f56 51#include "block.h"
a77053c2 52#include "solib.h"
84acb35a
JJ
53#include "solist.h"
54#include "observer.h"
60250e8b 55#include "exceptions.h"
765dc015 56#include "memattr.h"
f7f9143b 57#include "ada-lang.h"
d1aa2f50 58#include "top.h"
79a45b7d 59#include "valprint.h"
4efc6507 60#include "jit.h"
a96d9b2e 61#include "xml-syscall.h"
65d79d4b 62#include "parser-defs.h"
e9cafbcc 63#include "cli/cli-utils.h"
be34f849 64#include "continuations.h"
1bfeeb0f
JL
65#include "stack.h"
66#include "skip.h"
049e32d3 67#include "record.h"
edcc5120 68#include "gdb_regex.h"
c906108c 69
1042e4c0
SS
70/* readline include files */
71#include "readline/readline.h"
72#include "readline/history.h"
73
74/* readline defines this. */
75#undef savestring
76
034dad6f 77#include "mi/mi-common.h"
7371cf6d 78#include "python/python.h"
104c1213 79
4a64f543 80/* Prototypes for local functions. */
c906108c 81
a14ed312 82static void enable_delete_command (char *, int);
c906108c 83
a14ed312 84static void enable_once_command (char *, int);
c906108c 85
816338b5
SS
86static void enable_count_command (char *, int);
87
a14ed312 88static void disable_command (char *, int);
c906108c 89
a14ed312 90static void enable_command (char *, int);
c906108c 91
95a42b64
TT
92static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
93 void *),
94 void *);
c906108c 95
a14ed312 96static void ignore_command (char *, int);
c906108c 97
4efb68b1 98static int breakpoint_re_set_one (void *);
c906108c 99
348d480f
PA
100static void breakpoint_re_set_default (struct breakpoint *);
101
983af33b
SDJ
102static void create_sals_from_address_default (char **,
103 struct linespec_result *,
104 enum bptype, char *,
105 char **);
106
107static void create_breakpoints_sal_default (struct gdbarch *,
108 struct linespec_result *,
109 struct linespec_sals *,
110 char *, enum bptype,
111 enum bpdisp, int, int,
112 int,
113 const struct breakpoint_ops *,
114 int, int, int);
115
116static void decode_linespec_default (struct breakpoint *, char **,
117 struct symtabs_and_lines *);
118
a14ed312 119static void clear_command (char *, int);
c906108c 120
a14ed312 121static void catch_command (char *, int);
c906108c 122
a9634178 123static int can_use_hardware_watchpoint (struct value *);
c906108c 124
98deb0da 125static void break_command_1 (char *, int, int);
c906108c 126
a14ed312 127static void mention (struct breakpoint *);
c906108c 128
348d480f
PA
129static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
130 enum bptype,
c0a91b2b 131 const struct breakpoint_ops *);
3742cc8b
YQ
132static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
133 const struct symtab_and_line *);
134
4a64f543
MS
135/* This function is used in gdbtk sources and thus can not be made
136 static. */
63c252f8 137struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 138 struct symtab_and_line,
c0a91b2b
TT
139 enum bptype,
140 const struct breakpoint_ops *);
c906108c 141
06edf0c0
PA
142static struct breakpoint *
143 momentary_breakpoint_from_master (struct breakpoint *orig,
144 enum bptype type,
c0a91b2b 145 const struct breakpoint_ops *ops);
06edf0c0 146
76897487
KB
147static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
148
a6d9a66e
UW
149static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
150 CORE_ADDR bpaddr,
88f7da05 151 enum bptype bptype);
76897487 152
6c95b8df
PA
153static void describe_other_breakpoints (struct gdbarch *,
154 struct program_space *, CORE_ADDR,
5af949e3 155 struct obj_section *, int);
c906108c 156
6c95b8df
PA
157static int breakpoint_address_match (struct address_space *aspace1,
158 CORE_ADDR addr1,
159 struct address_space *aspace2,
160 CORE_ADDR addr2);
161
85d721b8
PA
162static int watchpoint_locations_match (struct bp_location *loc1,
163 struct bp_location *loc2);
164
f1310107
TJB
165static int breakpoint_location_address_match (struct bp_location *bl,
166 struct address_space *aspace,
167 CORE_ADDR addr);
168
a14ed312 169static void breakpoints_info (char *, int);
c906108c 170
d77f58be
SS
171static void watchpoints_info (char *, int);
172
e5a67952
MS
173static int breakpoint_1 (char *, int,
174 int (*) (const struct breakpoint *));
c906108c 175
4efb68b1 176static int breakpoint_cond_eval (void *);
c906108c 177
4efb68b1 178static void cleanup_executing_breakpoints (void *);
c906108c 179
a14ed312 180static void commands_command (char *, int);
c906108c 181
a14ed312 182static void condition_command (char *, int);
c906108c 183
c5aa993b
JM
184typedef enum
185 {
186 mark_inserted,
187 mark_uninserted
188 }
189insertion_state_t;
c906108c 190
0bde7532 191static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 192static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 193
e514a9d6 194static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 195
4efb68b1 196static int watchpoint_check (void *);
c906108c 197
a14ed312 198static void maintenance_info_breakpoints (char *, int);
c906108c 199
a14ed312 200static int hw_breakpoint_used_count (void);
c906108c 201
a1398e0c
PA
202static int hw_watchpoint_use_count (struct breakpoint *);
203
204static int hw_watchpoint_used_count_others (struct breakpoint *except,
205 enum bptype type,
206 int *other_type_used);
c906108c 207
a14ed312 208static void hbreak_command (char *, int);
c906108c 209
a14ed312 210static void thbreak_command (char *, int);
c906108c 211
816338b5
SS
212static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
213 int count);
c906108c 214
a14ed312 215static void stop_command (char *arg, int from_tty);
7a292a7a 216
a14ed312 217static void stopin_command (char *arg, int from_tty);
7a292a7a 218
a14ed312 219static void stopat_command (char *arg, int from_tty);
7a292a7a 220
a14ed312 221static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 222
d85310f7
MS
223static void catch_exception_command_1 (enum exception_event_kind ex_event,
224 char *arg, int tempflag, int from_tty);
7a292a7a 225
a14ed312 226static void tcatch_command (char *arg, int from_tty);
7a292a7a 227
d03285ec
UW
228static void detach_single_step_breakpoints (void);
229
6c95b8df
PA
230static int single_step_breakpoint_inserted_here_p (struct address_space *,
231 CORE_ADDR pc);
1aafd4da 232
fe3f5fa8 233static void free_bp_location (struct bp_location *loc);
f431efe5
PA
234static void incref_bp_location (struct bp_location *loc);
235static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 236
39d61571 237static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 238
b60e7edf 239static void update_global_location_list (int);
a5606eee 240
b60e7edf 241static void update_global_location_list_nothrow (int);
74960c60 242
d77f58be 243static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
244
245static void insert_breakpoint_locations (void);
a5606eee 246
a96d9b2e
SDJ
247static int syscall_catchpoint_p (struct breakpoint *b);
248
1042e4c0
SS
249static void tracepoints_info (char *, int);
250
251static void delete_trace_command (char *, int);
252
253static void enable_trace_command (char *, int);
254
255static void disable_trace_command (char *, int);
256
257static void trace_pass_command (char *, int);
258
9c06b0b4
TJB
259static int is_masked_watchpoint (const struct breakpoint *b);
260
983af33b
SDJ
261/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
262 otherwise. */
263
264static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 265
edcc5120
TT
266static void init_catchpoint (struct breakpoint *b,
267 struct gdbarch *gdbarch, int tempflag,
268 char *cond_string,
269 const struct breakpoint_ops *ops);
270
2060206e
PA
271/* The abstract base class all breakpoint_ops structures inherit
272 from. */
273static struct breakpoint_ops base_breakpoint_ops;
274
275/* The breakpoint_ops structure to be inherited by all breakpoint_ops
276 that are implemented on top of software or hardware breakpoints
277 (user breakpoints, internal and momentary breakpoints, etc.). */
278static struct breakpoint_ops bkpt_base_breakpoint_ops;
279
280/* Internal breakpoints class type. */
06edf0c0 281static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
282
283/* Momentary breakpoints class type. */
06edf0c0
PA
284static struct breakpoint_ops momentary_breakpoint_ops;
285
2060206e
PA
286/* The breakpoint_ops structure to be used in regular user created
287 breakpoints. */
288struct breakpoint_ops bkpt_breakpoint_ops;
289
5cea2a26
PA
290/* A reference-counted struct command_line. This lets multiple
291 breakpoints share a single command list. */
292struct counted_command_line
293{
294 /* The reference count. */
295 int refc;
296
297 /* The command list. */
298 struct command_line *commands;
299};
300
301struct command_line *
302breakpoint_commands (struct breakpoint *b)
303{
304 return b->commands ? b->commands->commands : NULL;
305}
3daf8fe5 306
f3b1572e
PA
307/* Flag indicating that a command has proceeded the inferior past the
308 current breakpoint. */
309
310static int breakpoint_proceeded;
311
956a9fb9 312const char *
2cec12e5
AR
313bpdisp_text (enum bpdisp disp)
314{
4a64f543
MS
315 /* NOTE: the following values are a part of MI protocol and
316 represent values of 'disp' field returned when inferior stops at
317 a breakpoint. */
bc043ef3 318 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 319
2cec12e5
AR
320 return bpdisps[(int) disp];
321}
c906108c 322
4a64f543 323/* Prototypes for exported functions. */
c906108c 324/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 325 if such is available. */
c906108c
SS
326static int can_use_hw_watchpoints;
327
920d2a44
AC
328static void
329show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
330 struct cmd_list_element *c,
331 const char *value)
332{
3e43a32a
MS
333 fprintf_filtered (file,
334 _("Debugger's willingness to use "
335 "watchpoint hardware is %s.\n"),
920d2a44
AC
336 value);
337}
338
fa8d40ab
JJ
339/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
340 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 341 for unrecognized breakpoint locations.
fa8d40ab
JJ
342 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
343static enum auto_boolean pending_break_support;
920d2a44
AC
344static void
345show_pending_break_support (struct ui_file *file, int from_tty,
346 struct cmd_list_element *c,
347 const char *value)
348{
3e43a32a
MS
349 fprintf_filtered (file,
350 _("Debugger's behavior regarding "
351 "pending breakpoints is %s.\n"),
920d2a44
AC
352 value);
353}
fa8d40ab 354
765dc015 355/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 356 set with "break" but falling in read-only memory.
765dc015
VP
357 If 0, gdb will warn about such breakpoints, but won't automatically
358 use hardware breakpoints. */
359static int automatic_hardware_breakpoints;
360static void
361show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
362 struct cmd_list_element *c,
363 const char *value)
364{
3e43a32a
MS
365 fprintf_filtered (file,
366 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
367 value);
368}
369
33e5cbd6
PA
370/* If on, gdb will keep breakpoints inserted even as inferior is
371 stopped, and immediately insert any new breakpoints. If off, gdb
372 will insert breakpoints into inferior only when resuming it, and
373 will remove breakpoints upon stop. If auto, GDB will behave as ON
374 if in non-stop mode, and as OFF if all-stop mode.*/
375
376static const char always_inserted_auto[] = "auto";
377static const char always_inserted_on[] = "on";
378static const char always_inserted_off[] = "off";
40478521 379static const char *const always_inserted_enums[] = {
33e5cbd6
PA
380 always_inserted_auto,
381 always_inserted_off,
382 always_inserted_on,
383 NULL
384};
385static const char *always_inserted_mode = always_inserted_auto;
386static void
74960c60 387show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 388 struct cmd_list_element *c, const char *value)
74960c60 389{
33e5cbd6 390 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
391 fprintf_filtered (file,
392 _("Always inserted breakpoint "
393 "mode is %s (currently %s).\n"),
33e5cbd6
PA
394 value,
395 breakpoints_always_inserted_mode () ? "on" : "off");
396 else
3e43a32a
MS
397 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
398 value);
74960c60
VP
399}
400
33e5cbd6
PA
401int
402breakpoints_always_inserted_mode (void)
403{
049e32d3
PA
404 return ((always_inserted_mode == always_inserted_on
405 || (always_inserted_mode == always_inserted_auto && non_stop))
406 && !RECORD_IS_USED);
33e5cbd6 407}
765dc015 408
a14ed312 409void _initialize_breakpoint (void);
c906108c 410
c906108c
SS
411/* Are we executing breakpoint commands? */
412static int executing_breakpoint_commands;
413
c02f5703
MS
414/* Are overlay event breakpoints enabled? */
415static int overlay_events_enabled;
416
e09342b5
TJB
417/* See description in breakpoint.h. */
418int target_exact_watchpoints = 0;
419
c906108c 420/* Walk the following statement or block through all breakpoints.
e5dd4106 421 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 422 current breakpoint. */
c906108c 423
5c44784c 424#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 425
5c44784c
JM
426#define ALL_BREAKPOINTS_SAFE(B,TMP) \
427 for (B = breakpoint_chain; \
428 B ? (TMP=B->next, 1): 0; \
429 B = TMP)
c906108c 430
4a64f543
MS
431/* Similar iterator for the low-level breakpoints. SAFE variant is
432 not provided so update_global_location_list must not be called
433 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 434
876fa593
JK
435#define ALL_BP_LOCATIONS(B,BP_TMP) \
436 for (BP_TMP = bp_location; \
437 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
438 BP_TMP++)
7cc221ef 439
1042e4c0
SS
440/* Iterator for tracepoints only. */
441
442#define ALL_TRACEPOINTS(B) \
443 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 444 if (is_tracepoint (B))
1042e4c0 445
7cc221ef 446/* Chains of all breakpoints defined. */
c906108c
SS
447
448struct breakpoint *breakpoint_chain;
449
876fa593
JK
450/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
451
452static struct bp_location **bp_location;
453
454/* Number of elements of BP_LOCATION. */
455
456static unsigned bp_location_count;
457
4a64f543
MS
458/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
459 ADDRESS for the current elements of BP_LOCATION which get a valid
460 result from bp_location_has_shadow. You can use it for roughly
461 limiting the subrange of BP_LOCATION to scan for shadow bytes for
462 an address you need to read. */
876fa593
JK
463
464static CORE_ADDR bp_location_placed_address_before_address_max;
465
4a64f543
MS
466/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
467 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
468 BP_LOCATION which get a valid result from bp_location_has_shadow.
469 You can use it for roughly limiting the subrange of BP_LOCATION to
470 scan for shadow bytes for an address you need to read. */
876fa593
JK
471
472static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 473
4a64f543
MS
474/* The locations that no longer correspond to any breakpoint, unlinked
475 from bp_location array, but for which a hit may still be reported
476 by a target. */
20874c92
VP
477VEC(bp_location_p) *moribund_locations = NULL;
478
c906108c
SS
479/* Number of last breakpoint made. */
480
95a42b64
TT
481static int breakpoint_count;
482
86b17b60
PA
483/* The value of `breakpoint_count' before the last command that
484 created breakpoints. If the last (break-like) command created more
485 than one breakpoint, then the difference between BREAKPOINT_COUNT
486 and PREV_BREAKPOINT_COUNT is more than one. */
487static int prev_breakpoint_count;
c906108c 488
1042e4c0
SS
489/* Number of last tracepoint made. */
490
95a42b64 491static int tracepoint_count;
1042e4c0 492
6149aea9
PA
493static struct cmd_list_element *breakpoint_set_cmdlist;
494static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 495struct cmd_list_element *save_cmdlist;
6149aea9 496
468d015d
JJ
497/* Return whether a breakpoint is an active enabled breakpoint. */
498static int
499breakpoint_enabled (struct breakpoint *b)
500{
0d381245 501 return (b->enable_state == bp_enabled);
468d015d
JJ
502}
503
c906108c
SS
504/* Set breakpoint count to NUM. */
505
95a42b64 506static void
fba45db2 507set_breakpoint_count (int num)
c906108c 508{
86b17b60 509 prev_breakpoint_count = breakpoint_count;
c906108c 510 breakpoint_count = num;
4fa62494 511 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
512}
513
86b17b60
PA
514/* Used by `start_rbreak_breakpoints' below, to record the current
515 breakpoint count before "rbreak" creates any breakpoint. */
516static int rbreak_start_breakpoint_count;
517
95a42b64
TT
518/* Called at the start an "rbreak" command to record the first
519 breakpoint made. */
86b17b60 520
95a42b64
TT
521void
522start_rbreak_breakpoints (void)
523{
86b17b60 524 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
525}
526
527/* Called at the end of an "rbreak" command to record the last
528 breakpoint made. */
86b17b60 529
95a42b64
TT
530void
531end_rbreak_breakpoints (void)
532{
86b17b60 533 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
534}
535
4a64f543 536/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
537
538void
fba45db2 539clear_breakpoint_hit_counts (void)
c906108c
SS
540{
541 struct breakpoint *b;
542
543 ALL_BREAKPOINTS (b)
544 b->hit_count = 0;
545}
546
9add0f1b
TT
547/* Allocate a new counted_command_line with reference count of 1.
548 The new structure owns COMMANDS. */
549
550static struct counted_command_line *
551alloc_counted_command_line (struct command_line *commands)
552{
553 struct counted_command_line *result
554 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 555
9add0f1b
TT
556 result->refc = 1;
557 result->commands = commands;
558 return result;
559}
560
561/* Increment reference count. This does nothing if CMD is NULL. */
562
563static void
564incref_counted_command_line (struct counted_command_line *cmd)
565{
566 if (cmd)
567 ++cmd->refc;
568}
569
570/* Decrement reference count. If the reference count reaches 0,
571 destroy the counted_command_line. Sets *CMDP to NULL. This does
572 nothing if *CMDP is NULL. */
573
574static void
575decref_counted_command_line (struct counted_command_line **cmdp)
576{
577 if (*cmdp)
578 {
579 if (--(*cmdp)->refc == 0)
580 {
581 free_command_lines (&(*cmdp)->commands);
582 xfree (*cmdp);
583 }
584 *cmdp = NULL;
585 }
586}
587
588/* A cleanup function that calls decref_counted_command_line. */
589
590static void
591do_cleanup_counted_command_line (void *arg)
592{
593 decref_counted_command_line (arg);
594}
595
596/* Create a cleanup that calls decref_counted_command_line on the
597 argument. */
598
599static struct cleanup *
600make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
601{
602 return make_cleanup (do_cleanup_counted_command_line, cmdp);
603}
604
c906108c 605\f
48cb2d85
VP
606/* Return the breakpoint with the specified number, or NULL
607 if the number does not refer to an existing breakpoint. */
608
609struct breakpoint *
610get_breakpoint (int num)
611{
612 struct breakpoint *b;
613
614 ALL_BREAKPOINTS (b)
615 if (b->number == num)
616 return b;
617
618 return NULL;
619}
5c44784c 620
c906108c 621\f
adc36818
PM
622
623void
624set_breakpoint_condition (struct breakpoint *b, char *exp,
625 int from_tty)
626{
3a5c3e22
PA
627 xfree (b->cond_string);
628 b->cond_string = NULL;
adc36818 629
3a5c3e22 630 if (is_watchpoint (b))
adc36818 631 {
3a5c3e22
PA
632 struct watchpoint *w = (struct watchpoint *) b;
633
634 xfree (w->cond_exp);
635 w->cond_exp = NULL;
636 }
637 else
638 {
639 struct bp_location *loc;
640
641 for (loc = b->loc; loc; loc = loc->next)
642 {
643 xfree (loc->cond);
644 loc->cond = NULL;
645 }
adc36818 646 }
adc36818
PM
647
648 if (*exp == 0)
649 {
650 if (from_tty)
651 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
652 }
653 else
654 {
655 char *arg = exp;
cc59ec59 656
adc36818
PM
657 /* I don't know if it matters whether this is the string the user
658 typed in or the decompiled expression. */
659 b->cond_string = xstrdup (arg);
660 b->condition_not_parsed = 0;
661
662 if (is_watchpoint (b))
663 {
3a5c3e22
PA
664 struct watchpoint *w = (struct watchpoint *) b;
665
adc36818
PM
666 innermost_block = NULL;
667 arg = exp;
3a5c3e22 668 w->cond_exp = parse_exp_1 (&arg, 0, 0);
adc36818
PM
669 if (*arg)
670 error (_("Junk at end of expression"));
3a5c3e22 671 w->cond_exp_valid_block = innermost_block;
adc36818
PM
672 }
673 else
674 {
3a5c3e22
PA
675 struct bp_location *loc;
676
adc36818
PM
677 for (loc = b->loc; loc; loc = loc->next)
678 {
679 arg = exp;
680 loc->cond =
681 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
682 if (*arg)
683 error (_("Junk at end of expression"));
684 }
685 }
686 }
687 breakpoints_changed ();
8d3788bd 688 observer_notify_breakpoint_modified (b);
adc36818
PM
689}
690
c906108c
SS
691/* condition N EXP -- set break condition of breakpoint N to EXP. */
692
693static void
fba45db2 694condition_command (char *arg, int from_tty)
c906108c 695{
52f0bd74 696 struct breakpoint *b;
c906108c 697 char *p;
52f0bd74 698 int bnum;
c906108c
SS
699
700 if (arg == 0)
e2e0b3e5 701 error_no_arg (_("breakpoint number"));
c906108c
SS
702
703 p = arg;
704 bnum = get_number (&p);
5c44784c 705 if (bnum == 0)
8a3fe4f8 706 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
707
708 ALL_BREAKPOINTS (b)
709 if (b->number == bnum)
2f069f6f 710 {
7371cf6d
PM
711 /* Check if this breakpoint has a Python object assigned to
712 it, and if it has a definition of the "stop"
713 method. This method and conditions entered into GDB from
714 the CLI are mutually exclusive. */
715 if (b->py_bp_object
716 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
717 error (_("Cannot set a condition where a Python 'stop' "
718 "method has been defined in the breakpoint."));
2566ad2d 719 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
720 return;
721 }
c906108c 722
8a3fe4f8 723 error (_("No breakpoint number %d."), bnum);
c906108c
SS
724}
725
a7bdde9e
VP
726/* Check that COMMAND do not contain commands that are suitable
727 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
728 Throw if any such commands is found. */
729
a7bdde9e
VP
730static void
731check_no_tracepoint_commands (struct command_line *commands)
732{
733 struct command_line *c;
cc59ec59 734
a7bdde9e
VP
735 for (c = commands; c; c = c->next)
736 {
737 int i;
738
739 if (c->control_type == while_stepping_control)
3e43a32a
MS
740 error (_("The 'while-stepping' command can "
741 "only be used for tracepoints"));
a7bdde9e
VP
742
743 for (i = 0; i < c->body_count; ++i)
744 check_no_tracepoint_commands ((c->body_list)[i]);
745
746 /* Not that command parsing removes leading whitespace and comment
4a64f543 747 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
748 command directly. */
749 if (strstr (c->line, "collect ") == c->line)
750 error (_("The 'collect' command can only be used for tracepoints"));
751
51661e93
VP
752 if (strstr (c->line, "teval ") == c->line)
753 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
754 }
755}
756
d77f58be
SS
757/* Encapsulate tests for different types of tracepoints. */
758
d9b3f62e
PA
759static int
760is_tracepoint_type (enum bptype type)
761{
762 return (type == bp_tracepoint
763 || type == bp_fast_tracepoint
764 || type == bp_static_tracepoint);
765}
766
a7bdde9e 767int
d77f58be 768is_tracepoint (const struct breakpoint *b)
a7bdde9e 769{
d9b3f62e 770 return is_tracepoint_type (b->type);
a7bdde9e 771}
d9b3f62e 772
e5dd4106 773/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
774 breakpoint. This function will throw an exception if a problem is
775 found. */
48cb2d85 776
95a42b64
TT
777static void
778validate_commands_for_breakpoint (struct breakpoint *b,
779 struct command_line *commands)
48cb2d85 780{
d77f58be 781 if (is_tracepoint (b))
a7bdde9e 782 {
4a64f543
MS
783 /* We need to verify that each top-level element of commands is
784 valid for tracepoints, that there's at most one
785 while-stepping element, and that while-stepping's body has
786 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
787 struct command_line *c;
788 struct command_line *while_stepping = 0;
789 for (c = commands; c; c = c->next)
790 {
a7bdde9e
VP
791 if (c->control_type == while_stepping_control)
792 {
793 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
794 error (_("The 'while-stepping' command "
795 "cannot be used for fast tracepoint"));
0fb4aa4b 796 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
797 error (_("The 'while-stepping' command "
798 "cannot be used for static tracepoint"));
a7bdde9e
VP
799
800 if (while_stepping)
3e43a32a
MS
801 error (_("The 'while-stepping' command "
802 "can be used only once"));
a7bdde9e
VP
803 else
804 while_stepping = c;
805 }
806 }
807 if (while_stepping)
808 {
809 struct command_line *c2;
810
811 gdb_assert (while_stepping->body_count == 1);
812 c2 = while_stepping->body_list[0];
813 for (; c2; c2 = c2->next)
814 {
a7bdde9e
VP
815 if (c2->control_type == while_stepping_control)
816 error (_("The 'while-stepping' command cannot be nested"));
817 }
818 }
819 }
820 else
821 {
822 check_no_tracepoint_commands (commands);
823 }
95a42b64
TT
824}
825
0fb4aa4b
PA
826/* Return a vector of all the static tracepoints set at ADDR. The
827 caller is responsible for releasing the vector. */
828
829VEC(breakpoint_p) *
830static_tracepoints_here (CORE_ADDR addr)
831{
832 struct breakpoint *b;
833 VEC(breakpoint_p) *found = 0;
834 struct bp_location *loc;
835
836 ALL_BREAKPOINTS (b)
837 if (b->type == bp_static_tracepoint)
838 {
839 for (loc = b->loc; loc; loc = loc->next)
840 if (loc->address == addr)
841 VEC_safe_push(breakpoint_p, found, b);
842 }
843
844 return found;
845}
846
95a42b64 847/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 848 validate that only allowed commands are included. */
95a42b64
TT
849
850void
4a64f543
MS
851breakpoint_set_commands (struct breakpoint *b,
852 struct command_line *commands)
95a42b64
TT
853{
854 validate_commands_for_breakpoint (b, commands);
a7bdde9e 855
9add0f1b
TT
856 decref_counted_command_line (&b->commands);
857 b->commands = alloc_counted_command_line (commands);
48cb2d85 858 breakpoints_changed ();
8d3788bd 859 observer_notify_breakpoint_modified (b);
48cb2d85
VP
860}
861
45a43567
TT
862/* Set the internal `silent' flag on the breakpoint. Note that this
863 is not the same as the "silent" that may appear in the breakpoint's
864 commands. */
865
866void
867breakpoint_set_silent (struct breakpoint *b, int silent)
868{
869 int old_silent = b->silent;
870
871 b->silent = silent;
872 if (old_silent != silent)
8d3788bd 873 observer_notify_breakpoint_modified (b);
45a43567
TT
874}
875
876/* Set the thread for this breakpoint. If THREAD is -1, make the
877 breakpoint work for any thread. */
878
879void
880breakpoint_set_thread (struct breakpoint *b, int thread)
881{
882 int old_thread = b->thread;
883
884 b->thread = thread;
885 if (old_thread != thread)
8d3788bd 886 observer_notify_breakpoint_modified (b);
45a43567
TT
887}
888
889/* Set the task for this breakpoint. If TASK is 0, make the
890 breakpoint work for any task. */
891
892void
893breakpoint_set_task (struct breakpoint *b, int task)
894{
895 int old_task = b->task;
896
897 b->task = task;
898 if (old_task != task)
8d3788bd 899 observer_notify_breakpoint_modified (b);
45a43567
TT
900}
901
95a42b64
TT
902void
903check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
904{
905 struct breakpoint *b = closure;
cc59ec59 906
a7bdde9e
VP
907 validate_actionline (&line, b);
908}
909
95a42b64
TT
910/* A structure used to pass information through
911 map_breakpoint_numbers. */
912
913struct commands_info
914{
915 /* True if the command was typed at a tty. */
916 int from_tty;
86b17b60
PA
917
918 /* The breakpoint range spec. */
919 char *arg;
920
95a42b64
TT
921 /* Non-NULL if the body of the commands are being read from this
922 already-parsed command. */
923 struct command_line *control;
86b17b60 924
95a42b64
TT
925 /* The command lines read from the user, or NULL if they have not
926 yet been read. */
927 struct counted_command_line *cmd;
928};
929
930/* A callback for map_breakpoint_numbers that sets the commands for
931 commands_command. */
932
c906108c 933static void
95a42b64 934do_map_commands_command (struct breakpoint *b, void *data)
c906108c 935{
95a42b64 936 struct commands_info *info = data;
c906108c 937
95a42b64
TT
938 if (info->cmd == NULL)
939 {
940 struct command_line *l;
5c44784c 941
95a42b64
TT
942 if (info->control != NULL)
943 l = copy_command_lines (info->control->body_list[0]);
944 else
86b17b60
PA
945 {
946 struct cleanup *old_chain;
947 char *str;
c5aa993b 948
3e43a32a
MS
949 str = xstrprintf (_("Type commands for breakpoint(s) "
950 "%s, one per line."),
86b17b60
PA
951 info->arg);
952
953 old_chain = make_cleanup (xfree, str);
954
955 l = read_command_lines (str,
956 info->from_tty, 1,
d77f58be 957 (is_tracepoint (b)
86b17b60
PA
958 ? check_tracepoint_command : 0),
959 b);
960
961 do_cleanups (old_chain);
962 }
a7bdde9e 963
95a42b64
TT
964 info->cmd = alloc_counted_command_line (l);
965 }
966
967 /* If a breakpoint was on the list more than once, we don't need to
968 do anything. */
969 if (b->commands != info->cmd)
970 {
971 validate_commands_for_breakpoint (b, info->cmd->commands);
972 incref_counted_command_line (info->cmd);
973 decref_counted_command_line (&b->commands);
974 b->commands = info->cmd;
975 breakpoints_changed ();
8d3788bd 976 observer_notify_breakpoint_modified (b);
c5aa993b 977 }
95a42b64
TT
978}
979
980static void
4a64f543
MS
981commands_command_1 (char *arg, int from_tty,
982 struct command_line *control)
95a42b64
TT
983{
984 struct cleanup *cleanups;
985 struct commands_info info;
986
987 info.from_tty = from_tty;
988 info.control = control;
989 info.cmd = NULL;
990 /* If we read command lines from the user, then `info' will hold an
991 extra reference to the commands that we must clean up. */
992 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
993
994 if (arg == NULL || !*arg)
995 {
86b17b60 996 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
997 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
998 breakpoint_count);
95a42b64
TT
999 else if (breakpoint_count > 0)
1000 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1001 else
1002 {
1003 /* So that we don't try to free the incoming non-NULL
1004 argument in the cleanup below. Mapping breakpoint
1005 numbers will fail in this case. */
1006 arg = NULL;
1007 }
95a42b64 1008 }
9766ced4
SS
1009 else
1010 /* The command loop has some static state, so we need to preserve
1011 our argument. */
1012 arg = xstrdup (arg);
86b17b60
PA
1013
1014 if (arg != NULL)
1015 make_cleanup (xfree, arg);
1016
1017 info.arg = arg;
95a42b64
TT
1018
1019 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1020
1021 if (info.cmd == NULL)
1022 error (_("No breakpoints specified."));
1023
1024 do_cleanups (cleanups);
1025}
1026
1027static void
1028commands_command (char *arg, int from_tty)
1029{
1030 commands_command_1 (arg, from_tty, NULL);
c906108c 1031}
40c03ae8
EZ
1032
1033/* Like commands_command, but instead of reading the commands from
1034 input stream, takes them from an already parsed command structure.
1035
1036 This is used by cli-script.c to DTRT with breakpoint commands
1037 that are part of if and while bodies. */
1038enum command_control_type
1039commands_from_control_command (char *arg, struct command_line *cmd)
1040{
95a42b64
TT
1041 commands_command_1 (arg, 0, cmd);
1042 return simple_control;
40c03ae8 1043}
876fa593
JK
1044
1045/* Return non-zero if BL->TARGET_INFO contains valid information. */
1046
1047static int
1048bp_location_has_shadow (struct bp_location *bl)
1049{
1050 if (bl->loc_type != bp_loc_software_breakpoint)
1051 return 0;
1052 if (!bl->inserted)
1053 return 0;
1054 if (bl->target_info.shadow_len == 0)
e5dd4106 1055 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1056 return 0;
1057 return 1;
1058}
1059
8defab1a 1060/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1061 by replacing any memory breakpoints with their shadowed contents.
1062
1063 The range of shadowed area by each bp_location is:
35df4500
TJB
1064 bl->address - bp_location_placed_address_before_address_max
1065 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1066 The range we were requested to resolve shadows for is:
1067 memaddr ... memaddr + len
1068 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1069 memaddr + len <= (bl->address
1070 - bp_location_placed_address_before_address_max)
876fa593 1071 and:
35df4500 1072 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1073
8defab1a 1074void
f0ba3972
PA
1075breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1076 const gdb_byte *writebuf_org,
1077 ULONGEST memaddr, LONGEST len)
c906108c 1078{
4a64f543
MS
1079 /* Left boundary, right boundary and median element of our binary
1080 search. */
876fa593
JK
1081 unsigned bc_l, bc_r, bc;
1082
4a64f543
MS
1083 /* Find BC_L which is a leftmost element which may affect BUF
1084 content. It is safe to report lower value but a failure to
1085 report higher one. */
876fa593
JK
1086
1087 bc_l = 0;
1088 bc_r = bp_location_count;
1089 while (bc_l + 1 < bc_r)
1090 {
35df4500 1091 struct bp_location *bl;
876fa593
JK
1092
1093 bc = (bc_l + bc_r) / 2;
35df4500 1094 bl = bp_location[bc];
876fa593 1095
4a64f543
MS
1096 /* Check first BL->ADDRESS will not overflow due to the added
1097 constant. Then advance the left boundary only if we are sure
1098 the BC element can in no way affect the BUF content (MEMADDR
1099 to MEMADDR + LEN range).
876fa593 1100
4a64f543
MS
1101 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1102 offset so that we cannot miss a breakpoint with its shadow
1103 range tail still reaching MEMADDR. */
c5aa993b 1104
35df4500
TJB
1105 if ((bl->address + bp_location_shadow_len_after_address_max
1106 >= bl->address)
1107 && (bl->address + bp_location_shadow_len_after_address_max
1108 <= memaddr))
876fa593
JK
1109 bc_l = bc;
1110 else
1111 bc_r = bc;
1112 }
1113
128070bb
PA
1114 /* Due to the binary search above, we need to make sure we pick the
1115 first location that's at BC_L's address. E.g., if there are
1116 multiple locations at the same address, BC_L may end up pointing
1117 at a duplicate location, and miss the "master"/"inserted"
1118 location. Say, given locations L1, L2 and L3 at addresses A and
1119 B:
1120
1121 L1@A, L2@A, L3@B, ...
1122
1123 BC_L could end up pointing at location L2, while the "master"
1124 location could be L1. Since the `loc->inserted' flag is only set
1125 on "master" locations, we'd forget to restore the shadow of L1
1126 and L2. */
1127 while (bc_l > 0
1128 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1129 bc_l--;
1130
876fa593
JK
1131 /* Now do full processing of the found relevant range of elements. */
1132
1133 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1134 {
35df4500 1135 struct bp_location *bl = bp_location[bc];
876fa593
JK
1136 CORE_ADDR bp_addr = 0;
1137 int bp_size = 0;
1138 int bptoffset = 0;
1139
35df4500
TJB
1140 /* bp_location array has BL->OWNER always non-NULL. */
1141 if (bl->owner->type == bp_none)
8a3fe4f8 1142 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1143 bl->owner->number);
ffce0d52 1144
e5dd4106 1145 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1146 content. */
1147
35df4500
TJB
1148 if (bl->address >= bp_location_placed_address_before_address_max
1149 && memaddr + len <= (bl->address
1150 - bp_location_placed_address_before_address_max))
876fa593
JK
1151 break;
1152
35df4500 1153 if (!bp_location_has_shadow (bl))
c5aa993b 1154 continue;
35df4500 1155 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1156 current_program_space->aspace, 0))
1157 continue;
1158
c5aa993b
JM
1159 /* Addresses and length of the part of the breakpoint that
1160 we need to copy. */
35df4500
TJB
1161 bp_addr = bl->target_info.placed_address;
1162 bp_size = bl->target_info.shadow_len;
8defab1a 1163
c5aa993b
JM
1164 if (bp_addr + bp_size <= memaddr)
1165 /* The breakpoint is entirely before the chunk of memory we
1166 are reading. */
1167 continue;
8defab1a 1168
c5aa993b
JM
1169 if (bp_addr >= memaddr + len)
1170 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1171 reading. */
c5aa993b 1172 continue;
c5aa993b 1173
8defab1a
DJ
1174 /* Offset within shadow_contents. */
1175 if (bp_addr < memaddr)
1176 {
1177 /* Only copy the second part of the breakpoint. */
1178 bp_size -= memaddr - bp_addr;
1179 bptoffset = memaddr - bp_addr;
1180 bp_addr = memaddr;
1181 }
c5aa993b 1182
8defab1a
DJ
1183 if (bp_addr + bp_size > memaddr + len)
1184 {
1185 /* Only copy the first part of the breakpoint. */
1186 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1187 }
c5aa993b 1188
f0ba3972
PA
1189 if (readbuf != NULL)
1190 {
1191 /* Update the read buffer with this inserted breakpoint's
1192 shadow. */
1193 memcpy (readbuf + bp_addr - memaddr,
1194 bl->target_info.shadow_contents + bptoffset, bp_size);
1195 }
1196 else
1197 {
1198 struct gdbarch *gdbarch = bl->gdbarch;
1199 const unsigned char *bp;
1200 CORE_ADDR placed_address = bl->target_info.placed_address;
1201 unsigned placed_size = bl->target_info.placed_size;
1202
1203 /* Update the shadow with what we want to write to memory. */
1204 memcpy (bl->target_info.shadow_contents + bptoffset,
1205 writebuf_org + bp_addr - memaddr, bp_size);
1206
1207 /* Determine appropriate breakpoint contents and size for this
1208 address. */
1209 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1210
1211 /* Update the final write buffer with this inserted
1212 breakpoint's INSN. */
1213 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1214 }
c5aa993b 1215 }
c906108c 1216}
c906108c 1217\f
c5aa993b 1218
60e1c644
PA
1219/* Return true if BPT is of any hardware watchpoint kind. */
1220
a5606eee 1221static int
d77f58be 1222is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1223{
1224 return (bpt->type == bp_hardware_watchpoint
1225 || bpt->type == bp_read_watchpoint
1226 || bpt->type == bp_access_watchpoint);
1227}
7270d8f2 1228
60e1c644
PA
1229/* Return true if BPT is of any watchpoint kind, hardware or
1230 software. */
1231
3a5c3e22 1232int
d77f58be 1233is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1234{
1235 return (is_hardware_watchpoint (bpt)
1236 || bpt->type == bp_watchpoint);
1237}
1238
3a5c3e22
PA
1239/* Returns true if the current thread and its running state are safe
1240 to evaluate or update watchpoint B. Watchpoints on local
1241 expressions need to be evaluated in the context of the thread that
1242 was current when the watchpoint was created, and, that thread needs
1243 to be stopped to be able to select the correct frame context.
1244 Watchpoints on global expressions can be evaluated on any thread,
1245 and in any state. It is presently left to the target allowing
1246 memory accesses when threads are running. */
f6bc2008
PA
1247
1248static int
3a5c3e22 1249watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1250{
d0d8b0c6
JK
1251 return (b->base.pspace == current_program_space
1252 && (ptid_equal (b->watchpoint_thread, null_ptid)
1253 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1254 && !is_executing (inferior_ptid))));
f6bc2008
PA
1255}
1256
d0fb5eae
JK
1257/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1258 associated bp_watchpoint_scope breakpoint. */
1259
1260static void
3a5c3e22 1261watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1262{
3a5c3e22 1263 struct breakpoint *b = &w->base;
d0fb5eae
JK
1264
1265 if (b->related_breakpoint != b)
1266 {
1267 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1268 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1269 b->related_breakpoint->disposition = disp_del_at_next_stop;
1270 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1271 b->related_breakpoint = b;
1272 }
1273 b->disposition = disp_del_at_next_stop;
1274}
1275
567e1b4e
JB
1276/* Assuming that B is a watchpoint:
1277 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1278 - Evaluate expression and store the result in B->val
567e1b4e
JB
1279 - Evaluate the condition if there is one, and store the result
1280 in b->loc->cond.
a5606eee
VP
1281 - Update the list of values that must be watched in B->loc.
1282
4a64f543
MS
1283 If the watchpoint disposition is disp_del_at_next_stop, then do
1284 nothing. If this is local watchpoint that is out of scope, delete
1285 it.
1286
1287 Even with `set breakpoint always-inserted on' the watchpoints are
1288 removed + inserted on each stop here. Normal breakpoints must
1289 never be removed because they might be missed by a running thread
1290 when debugging in non-stop mode. On the other hand, hardware
1291 watchpoints (is_hardware_watchpoint; processed here) are specific
1292 to each LWP since they are stored in each LWP's hardware debug
1293 registers. Therefore, such LWP must be stopped first in order to
1294 be able to modify its hardware watchpoints.
1295
1296 Hardware watchpoints must be reset exactly once after being
1297 presented to the user. It cannot be done sooner, because it would
1298 reset the data used to present the watchpoint hit to the user. And
1299 it must not be done later because it could display the same single
1300 watchpoint hit during multiple GDB stops. Note that the latter is
1301 relevant only to the hardware watchpoint types bp_read_watchpoint
1302 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1303 not user-visible - its hit is suppressed if the memory content has
1304 not changed.
1305
1306 The following constraints influence the location where we can reset
1307 hardware watchpoints:
1308
1309 * target_stopped_by_watchpoint and target_stopped_data_address are
1310 called several times when GDB stops.
1311
1312 [linux]
1313 * Multiple hardware watchpoints can be hit at the same time,
1314 causing GDB to stop. GDB only presents one hardware watchpoint
1315 hit at a time as the reason for stopping, and all the other hits
1316 are presented later, one after the other, each time the user
1317 requests the execution to be resumed. Execution is not resumed
1318 for the threads still having pending hit event stored in
1319 LWP_INFO->STATUS. While the watchpoint is already removed from
1320 the inferior on the first stop the thread hit event is kept being
1321 reported from its cached value by linux_nat_stopped_data_address
1322 until the real thread resume happens after the watchpoint gets
1323 presented and thus its LWP_INFO->STATUS gets reset.
1324
1325 Therefore the hardware watchpoint hit can get safely reset on the
1326 watchpoint removal from inferior. */
a79d3c27 1327
b40ce68a 1328static void
3a5c3e22 1329update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1330{
a5606eee 1331 int within_current_scope;
a5606eee 1332 struct frame_id saved_frame_id;
66076460 1333 int frame_saved;
a5606eee 1334
f6bc2008
PA
1335 /* If this is a local watchpoint, we only want to check if the
1336 watchpoint frame is in scope if the current thread is the thread
1337 that was used to create the watchpoint. */
1338 if (!watchpoint_in_thread_scope (b))
1339 return;
1340
3a5c3e22 1341 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1342 return;
1343
66076460 1344 frame_saved = 0;
a5606eee
VP
1345
1346 /* Determine if the watchpoint is within scope. */
1347 if (b->exp_valid_block == NULL)
1348 within_current_scope = 1;
1349 else
1350 {
b5db5dfc
UW
1351 struct frame_info *fi = get_current_frame ();
1352 struct gdbarch *frame_arch = get_frame_arch (fi);
1353 CORE_ADDR frame_pc = get_frame_pc (fi);
1354
1355 /* If we're in a function epilogue, unwinding may not work
1356 properly, so do not attempt to recreate locations at this
1357 point. See similar comments in watchpoint_check. */
1358 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1359 return;
66076460
DJ
1360
1361 /* Save the current frame's ID so we can restore it after
1362 evaluating the watchpoint expression on its own frame. */
1363 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1364 took a frame parameter, so that we didn't have to change the
1365 selected frame. */
1366 frame_saved = 1;
1367 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1368
a5606eee
VP
1369 fi = frame_find_by_id (b->watchpoint_frame);
1370 within_current_scope = (fi != NULL);
1371 if (within_current_scope)
1372 select_frame (fi);
1373 }
1374
b5db5dfc
UW
1375 /* We don't free locations. They are stored in the bp_location array
1376 and update_global_location_list will eventually delete them and
1377 remove breakpoints if needed. */
3a5c3e22 1378 b->base.loc = NULL;
b5db5dfc 1379
a5606eee
VP
1380 if (within_current_scope && reparse)
1381 {
1382 char *s;
d63d0675 1383
a5606eee
VP
1384 if (b->exp)
1385 {
1386 xfree (b->exp);
1387 b->exp = NULL;
1388 }
d63d0675 1389 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1390 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1391 /* If the meaning of expression itself changed, the old value is
1392 no longer relevant. We don't want to report a watchpoint hit
1393 to the user when the old value and the new value may actually
1394 be completely different objects. */
1395 value_free (b->val);
fa4727a6
DJ
1396 b->val = NULL;
1397 b->val_valid = 0;
60e1c644
PA
1398
1399 /* Note that unlike with breakpoints, the watchpoint's condition
1400 expression is stored in the breakpoint object, not in the
1401 locations (re)created below. */
3a5c3e22 1402 if (b->base.cond_string != NULL)
60e1c644
PA
1403 {
1404 if (b->cond_exp != NULL)
1405 {
1406 xfree (b->cond_exp);
1407 b->cond_exp = NULL;
1408 }
1409
3a5c3e22 1410 s = b->base.cond_string;
60e1c644
PA
1411 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1412 }
a5606eee 1413 }
a5606eee
VP
1414
1415 /* If we failed to parse the expression, for example because
1416 it refers to a global variable in a not-yet-loaded shared library,
1417 don't try to insert watchpoint. We don't automatically delete
1418 such watchpoint, though, since failure to parse expression
1419 is different from out-of-scope watchpoint. */
2d134ed3
PA
1420 if ( !target_has_execution)
1421 {
1422 /* Without execution, memory can't change. No use to try and
1423 set watchpoint locations. The watchpoint will be reset when
1424 the target gains execution, through breakpoint_re_set. */
1425 }
1426 else if (within_current_scope && b->exp)
a5606eee 1427 {
0cf6dd15 1428 int pc = 0;
fa4727a6 1429 struct value *val_chain, *v, *result, *next;
2d134ed3 1430 struct program_space *frame_pspace;
a5606eee 1431
0cf6dd15 1432 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1433
a5606eee
VP
1434 /* Avoid setting b->val if it's already set. The meaning of
1435 b->val is 'the last value' user saw, and we should update
1436 it only if we reported that last value to user. As it
9c06b0b4
TJB
1437 happens, the code that reports it updates b->val directly.
1438 We don't keep track of the memory value for masked
1439 watchpoints. */
3a5c3e22 1440 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1441 {
1442 b->val = v;
1443 b->val_valid = 1;
1444 }
a5606eee 1445
2d134ed3
PA
1446 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1447
a5606eee 1448 /* Look at each value on the value chain. */
9fa40276 1449 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1450 {
1451 /* If it's a memory location, and GDB actually needed
1452 its contents to evaluate the expression, then we
fa4727a6
DJ
1453 must watch it. If the first value returned is
1454 still lazy, that means an error occurred reading it;
1455 watch it anyway in case it becomes readable. */
a5606eee 1456 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1457 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1458 {
1459 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1460
a5606eee
VP
1461 /* We only watch structs and arrays if user asked
1462 for it explicitly, never if they just happen to
1463 appear in the middle of some value chain. */
fa4727a6 1464 if (v == result
a5606eee
VP
1465 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1466 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1467 {
1468 CORE_ADDR addr;
1469 int len, type;
1470 struct bp_location *loc, **tmp;
1471
42ae5230 1472 addr = value_address (v);
a5606eee
VP
1473 len = TYPE_LENGTH (value_type (v));
1474 type = hw_write;
3a5c3e22 1475 if (b->base.type == bp_read_watchpoint)
a5606eee 1476 type = hw_read;
3a5c3e22 1477 else if (b->base.type == bp_access_watchpoint)
a5606eee 1478 type = hw_access;
3a5c3e22
PA
1479
1480 loc = allocate_bp_location (&b->base);
1481 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1482 ;
1483 *tmp = loc;
a6d9a66e 1484 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1485
1486 loc->pspace = frame_pspace;
a5606eee
VP
1487 loc->address = addr;
1488 loc->length = len;
1489 loc->watchpoint_type = type;
1490 }
1491 }
9fa40276
TJB
1492 }
1493
1494 /* Change the type of breakpoint between hardware assisted or
1495 an ordinary watchpoint depending on the hardware support
1496 and free hardware slots. REPARSE is set when the inferior
1497 is started. */
a9634178 1498 if (reparse)
9fa40276 1499 {
e09342b5 1500 int reg_cnt;
9fa40276
TJB
1501 enum bp_loc_type loc_type;
1502 struct bp_location *bl;
a5606eee 1503
a9634178 1504 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1505
1506 if (reg_cnt)
9fa40276
TJB
1507 {
1508 int i, target_resources_ok, other_type_used;
a1398e0c 1509 enum bptype type;
9fa40276 1510
a9634178
TJB
1511 /* Use an exact watchpoint when there's only one memory region to be
1512 watched, and only one debug register is needed to watch it. */
1513 b->exact = target_exact_watchpoints && reg_cnt == 1;
1514
9fa40276 1515 /* We need to determine how many resources are already
e09342b5
TJB
1516 used for all other hardware watchpoints plus this one
1517 to see if we still have enough resources to also fit
a1398e0c
PA
1518 this watchpoint in as well. */
1519
1520 /* If this is a software watchpoint, we try to turn it
1521 to a hardware one -- count resources as if B was of
1522 hardware watchpoint type. */
1523 type = b->base.type;
1524 if (type == bp_watchpoint)
1525 type = bp_hardware_watchpoint;
1526
1527 /* This watchpoint may or may not have been placed on
1528 the list yet at this point (it won't be in the list
1529 if we're trying to create it for the first time,
1530 through watch_command), so always account for it
1531 manually. */
1532
1533 /* Count resources used by all watchpoints except B. */
1534 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1535
1536 /* Add in the resources needed for B. */
1537 i += hw_watchpoint_use_count (&b->base);
1538
1539 target_resources_ok
1540 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1541 if (target_resources_ok <= 0)
a9634178 1542 {
3a5c3e22 1543 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1544
1545 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1546 error (_("Target does not support this type of "
1547 "hardware watchpoint."));
9c06b0b4
TJB
1548 else if (target_resources_ok < 0 && !sw_mode)
1549 error (_("There are not enough available hardware "
1550 "resources for this watchpoint."));
a1398e0c
PA
1551
1552 /* Downgrade to software watchpoint. */
1553 b->base.type = bp_watchpoint;
1554 }
1555 else
1556 {
1557 /* If this was a software watchpoint, we've just
1558 found we have enough resources to turn it to a
1559 hardware watchpoint. Otherwise, this is a
1560 nop. */
1561 b->base.type = type;
a9634178 1562 }
9fa40276 1563 }
3a5c3e22 1564 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1565 error (_("Expression cannot be implemented with "
1566 "read/access watchpoint."));
9fa40276 1567 else
3a5c3e22 1568 b->base.type = bp_watchpoint;
9fa40276 1569
3a5c3e22 1570 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1571 : bp_loc_hardware_watchpoint);
3a5c3e22 1572 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1573 bl->loc_type = loc_type;
1574 }
1575
1576 for (v = val_chain; v; v = next)
1577 {
a5606eee
VP
1578 next = value_next (v);
1579 if (v != b->val)
1580 value_free (v);
1581 }
1582
c7437ca6
PA
1583 /* If a software watchpoint is not watching any memory, then the
1584 above left it without any location set up. But,
1585 bpstat_stop_status requires a location to be able to report
1586 stops, so make sure there's at least a dummy one. */
3a5c3e22 1587 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1588 {
3a5c3e22
PA
1589 struct breakpoint *base = &b->base;
1590 base->loc = allocate_bp_location (base);
1591 base->loc->pspace = frame_pspace;
1592 base->loc->address = -1;
1593 base->loc->length = -1;
1594 base->loc->watchpoint_type = -1;
c7437ca6 1595 }
a5606eee
VP
1596 }
1597 else if (!within_current_scope)
7270d8f2 1598 {
ac74f770
MS
1599 printf_filtered (_("\
1600Watchpoint %d deleted because the program has left the block\n\
1601in which its expression is valid.\n"),
3a5c3e22 1602 b->base.number);
d0fb5eae 1603 watchpoint_del_at_next_stop (b);
7270d8f2 1604 }
a5606eee
VP
1605
1606 /* Restore the selected frame. */
66076460
DJ
1607 if (frame_saved)
1608 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1609}
1610
a5606eee 1611
74960c60 1612/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1613 inserted in the inferior. We don't differentiate the type of BL's owner
1614 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1615 breakpoint_ops is not defined, because in insert_bp_location,
1616 tracepoint's insert_location will not be called. */
74960c60 1617static int
35df4500 1618should_be_inserted (struct bp_location *bl)
74960c60 1619{
35df4500 1620 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1621 return 0;
1622
35df4500 1623 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1624 return 0;
1625
35df4500 1626 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1627 return 0;
1628
f8eba3c6
TT
1629 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1630 return 0;
1631
56710373
PA
1632 /* This is set for example, when we're attached to the parent of a
1633 vfork, and have detached from the child. The child is running
1634 free, and we expect it to do an exec or exit, at which point the
1635 OS makes the parent schedulable again (and the target reports
1636 that the vfork is done). Until the child is done with the shared
1637 memory region, do not insert breakpoints in the parent, otherwise
1638 the child could still trip on the parent's breakpoints. Since
1639 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1640 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1641 return 0;
1642
74960c60
VP
1643 return 1;
1644}
1645
934709f0
PW
1646/* Same as should_be_inserted but does the check assuming
1647 that the location is not duplicated. */
1648
1649static int
1650unduplicated_should_be_inserted (struct bp_location *bl)
1651{
1652 int result;
1653 const int save_duplicate = bl->duplicate;
1654
1655 bl->duplicate = 0;
1656 result = should_be_inserted (bl);
1657 bl->duplicate = save_duplicate;
1658 return result;
1659}
1660
35df4500
TJB
1661/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1662 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 1663 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
1664 Returns 0 for success, 1 if the bp_location type is not supported or
1665 -1 for failure.
879bfdc2 1666
4a64f543
MS
1667 NOTE drow/2003-09-09: This routine could be broken down to an
1668 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1669static int
35df4500 1670insert_bp_location (struct bp_location *bl,
26bb91f3 1671 struct ui_file *tmp_error_stream,
3fbb6ffa 1672 int *disabled_breaks,
26bb91f3 1673 int *hw_breakpoint_error)
879bfdc2
DJ
1674{
1675 int val = 0;
1676
35df4500 1677 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1678 return 0;
1679
8181d85f 1680 /* Initialize the target-specific information. */
35df4500
TJB
1681 memset (&bl->target_info, 0, sizeof (bl->target_info));
1682 bl->target_info.placed_address = bl->address;
1683 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1684 bl->target_info.length = bl->length;
8181d85f 1685
35df4500
TJB
1686 if (bl->loc_type == bp_loc_software_breakpoint
1687 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1688 {
35df4500 1689 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1690 {
1691 /* If the explicitly specified breakpoint type
1692 is not hardware breakpoint, check the memory map to see
1693 if the breakpoint address is in read only memory or not.
4a64f543 1694
765dc015
VP
1695 Two important cases are:
1696 - location type is not hardware breakpoint, memory
1697 is readonly. We change the type of the location to
1698 hardware breakpoint.
4a64f543
MS
1699 - location type is hardware breakpoint, memory is
1700 read-write. This means we've previously made the
1701 location hardware one, but then the memory map changed,
1702 so we undo.
765dc015 1703
4a64f543
MS
1704 When breakpoints are removed, remove_breakpoints will use
1705 location types we've just set here, the only possible
1706 problem is that memory map has changed during running
1707 program, but it's not going to work anyway with current
1708 gdb. */
765dc015 1709 struct mem_region *mr
35df4500 1710 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1711
1712 if (mr)
1713 {
1714 if (automatic_hardware_breakpoints)
1715 {
765dc015
VP
1716 enum bp_loc_type new_type;
1717
1718 if (mr->attrib.mode != MEM_RW)
1719 new_type = bp_loc_hardware_breakpoint;
1720 else
1721 new_type = bp_loc_software_breakpoint;
1722
35df4500 1723 if (new_type != bl->loc_type)
765dc015
VP
1724 {
1725 static int said = 0;
cc59ec59 1726
35df4500 1727 bl->loc_type = new_type;
765dc015
VP
1728 if (!said)
1729 {
3e43a32a
MS
1730 fprintf_filtered (gdb_stdout,
1731 _("Note: automatically using "
1732 "hardware breakpoints for "
1733 "read-only addresses.\n"));
765dc015
VP
1734 said = 1;
1735 }
1736 }
1737 }
35df4500 1738 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1739 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1740 warning (_("cannot set software breakpoint "
1741 "at readonly address %s"),
35df4500 1742 paddress (bl->gdbarch, bl->address));
765dc015
VP
1743 }
1744 }
1745
879bfdc2
DJ
1746 /* First check to see if we have to handle an overlay. */
1747 if (overlay_debugging == ovly_off
35df4500
TJB
1748 || bl->section == NULL
1749 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1750 {
1751 /* No overlay handling: just set the breakpoint. */
1752
348d480f 1753 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1754 }
1755 else
1756 {
4a64f543 1757 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1758 Shall we set a breakpoint at the LMA? */
1759 if (!overlay_events_enabled)
1760 {
1761 /* Yes -- overlay event support is not active,
1762 so we must try to set a breakpoint at the LMA.
1763 This will not work for a hardware breakpoint. */
35df4500 1764 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1765 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1766 bl->owner->number);
879bfdc2
DJ
1767 else
1768 {
35df4500
TJB
1769 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1770 bl->section);
879bfdc2 1771 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1772 bl->overlay_target_info = bl->target_info;
1773 bl->overlay_target_info.placed_address = addr;
1774 val = target_insert_breakpoint (bl->gdbarch,
1775 &bl->overlay_target_info);
879bfdc2 1776 if (val != 0)
99361f52 1777 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1778 "Overlay breakpoint %d "
1779 "failed: in ROM?\n",
35df4500 1780 bl->owner->number);
879bfdc2
DJ
1781 }
1782 }
1783 /* Shall we set a breakpoint at the VMA? */
35df4500 1784 if (section_is_mapped (bl->section))
879bfdc2
DJ
1785 {
1786 /* Yes. This overlay section is mapped into memory. */
348d480f 1787 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1788 }
1789 else
1790 {
1791 /* No. This breakpoint will not be inserted.
1792 No error, but do not mark the bp as 'inserted'. */
1793 return 0;
1794 }
1795 }
1796
1797 if (val)
1798 {
1799 /* Can't set the breakpoint. */
35df4500 1800 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1801 {
4a64f543 1802 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1803 val = 0;
35df4500 1804 bl->shlib_disabled = 1;
8d3788bd 1805 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
1806 if (!*disabled_breaks)
1807 {
1808 fprintf_unfiltered (tmp_error_stream,
1809 "Cannot insert breakpoint %d.\n",
1810 bl->owner->number);
1811 fprintf_unfiltered (tmp_error_stream,
1812 "Temporarily disabling shared "
1813 "library breakpoints:\n");
1814 }
1815 *disabled_breaks = 1;
879bfdc2 1816 fprintf_unfiltered (tmp_error_stream,
35df4500 1817 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1818 }
1819 else
879bfdc2 1820 {
35df4500 1821 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1822 {
1823 *hw_breakpoint_error = 1;
3e43a32a
MS
1824 fprintf_unfiltered (tmp_error_stream,
1825 "Cannot insert hardware "
1826 "breakpoint %d.\n",
35df4500 1827 bl->owner->number);
879bfdc2
DJ
1828 }
1829 else
1830 {
1831 fprintf_unfiltered (tmp_error_stream,
1832 "Cannot insert breakpoint %d.\n",
35df4500 1833 bl->owner->number);
879bfdc2
DJ
1834 fprintf_filtered (tmp_error_stream,
1835 "Error accessing memory address ");
35df4500 1836 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1837 tmp_error_stream);
879bfdc2
DJ
1838 fprintf_filtered (tmp_error_stream, ": %s.\n",
1839 safe_strerror (val));
1840 }
1841
1842 }
1843 }
1844 else
35df4500 1845 bl->inserted = 1;
879bfdc2
DJ
1846
1847 return val;
1848 }
1849
35df4500 1850 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1851 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1852 watchpoints. It's not clear that it's necessary... */
35df4500 1853 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1854 {
77b06cd7
TJB
1855 gdb_assert (bl->owner->ops != NULL
1856 && bl->owner->ops->insert_location != NULL);
1857
1858 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1859
1860 /* If trying to set a read-watchpoint, and it turns out it's not
1861 supported, try emulating one with an access watchpoint. */
35df4500 1862 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1863 {
1864 struct bp_location *loc, **loc_temp;
1865
1866 /* But don't try to insert it, if there's already another
1867 hw_access location that would be considered a duplicate
1868 of this one. */
1869 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1870 if (loc != bl
85d721b8 1871 && loc->watchpoint_type == hw_access
35df4500 1872 && watchpoint_locations_match (bl, loc))
85d721b8 1873 {
35df4500
TJB
1874 bl->duplicate = 1;
1875 bl->inserted = 1;
1876 bl->target_info = loc->target_info;
1877 bl->watchpoint_type = hw_access;
85d721b8
PA
1878 val = 0;
1879 break;
1880 }
1881
1882 if (val == 1)
1883 {
77b06cd7
TJB
1884 bl->watchpoint_type = hw_access;
1885 val = bl->owner->ops->insert_location (bl);
1886
1887 if (val)
1888 /* Back to the original value. */
1889 bl->watchpoint_type = hw_read;
85d721b8
PA
1890 }
1891 }
1892
35df4500 1893 bl->inserted = (val == 0);
879bfdc2
DJ
1894 }
1895
35df4500 1896 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1897 {
77b06cd7
TJB
1898 gdb_assert (bl->owner->ops != NULL
1899 && bl->owner->ops->insert_location != NULL);
1900
1901 val = bl->owner->ops->insert_location (bl);
1902 if (val)
1903 {
1904 bl->owner->enable_state = bp_disabled;
1905
1906 if (val == 1)
1907 warning (_("\
1908Error inserting catchpoint %d: Your system does not support this type\n\
1909of catchpoint."), bl->owner->number);
1910 else
1911 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1912 }
1913
1914 bl->inserted = (val == 0);
1640b821
DJ
1915
1916 /* We've already printed an error message if there was a problem
1917 inserting this catchpoint, and we've disabled the catchpoint,
1918 so just return success. */
1919 return 0;
879bfdc2
DJ
1920 }
1921
1922 return 0;
1923}
1924
6c95b8df
PA
1925/* This function is called when program space PSPACE is about to be
1926 deleted. It takes care of updating breakpoints to not reference
1927 PSPACE anymore. */
1928
1929void
1930breakpoint_program_space_exit (struct program_space *pspace)
1931{
1932 struct breakpoint *b, *b_temp;
876fa593 1933 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1934
1935 /* Remove any breakpoint that was set through this program space. */
1936 ALL_BREAKPOINTS_SAFE (b, b_temp)
1937 {
1938 if (b->pspace == pspace)
1939 delete_breakpoint (b);
1940 }
1941
1942 /* Breakpoints set through other program spaces could have locations
1943 bound to PSPACE as well. Remove those. */
876fa593 1944 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1945 {
1946 struct bp_location *tmp;
1947
1948 if (loc->pspace == pspace)
1949 {
2bdf28a0 1950 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1951 if (loc->owner->loc == loc)
1952 loc->owner->loc = loc->next;
1953 else
1954 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1955 if (tmp->next == loc)
1956 {
1957 tmp->next = loc->next;
1958 break;
1959 }
1960 }
1961 }
1962
1963 /* Now update the global location list to permanently delete the
1964 removed locations above. */
1965 update_global_location_list (0);
1966}
1967
74960c60
VP
1968/* Make sure all breakpoints are inserted in inferior.
1969 Throws exception on any error.
1970 A breakpoint that is already inserted won't be inserted
1971 again, so calling this function twice is safe. */
1972void
1973insert_breakpoints (void)
1974{
1975 struct breakpoint *bpt;
1976
1977 ALL_BREAKPOINTS (bpt)
1978 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
1979 {
1980 struct watchpoint *w = (struct watchpoint *) bpt;
1981
1982 update_watchpoint (w, 0 /* don't reparse. */);
1983 }
74960c60 1984
b60e7edf 1985 update_global_location_list (1);
74960c60 1986
c35b1492
PA
1987 /* update_global_location_list does not insert breakpoints when
1988 always_inserted_mode is not enabled. Explicitly insert them
1989 now. */
1990 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1991 insert_breakpoint_locations ();
1992}
1993
c30eee59 1994/* Used when starting or continuing the program. */
c906108c 1995
74960c60
VP
1996static void
1997insert_breakpoint_locations (void)
c906108c 1998{
a5606eee 1999 struct breakpoint *bpt;
35df4500 2000 struct bp_location *bl, **blp_tmp;
eacd795a 2001 int error_flag = 0;
c906108c 2002 int val = 0;
3fbb6ffa 2003 int disabled_breaks = 0;
81d0cc19 2004 int hw_breakpoint_error = 0;
c906108c 2005
81d0cc19 2006 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2007 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2008
81d0cc19
GS
2009 /* Explicitly mark the warning -- this will only be printed if
2010 there was an error. */
2011 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2012
2013 save_current_space_and_thread ();
2014
35df4500 2015 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2016 {
35df4500 2017 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
2018 continue;
2019
4a64f543
MS
2020 /* There is no point inserting thread-specific breakpoints if
2021 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2022 has BL->OWNER always non-NULL. */
35df4500
TJB
2023 if (bl->owner->thread != -1
2024 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2025 continue;
2026
35df4500 2027 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2028
2029 /* For targets that support global breakpoints, there's no need
2030 to select an inferior to insert breakpoint to. In fact, even
2031 if we aren't attached to any process yet, we should still
2032 insert breakpoints. */
2033 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2034 && ptid_equal (inferior_ptid, null_ptid))
2035 continue;
2036
3fbb6ffa
TJB
2037 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2038 &hw_breakpoint_error);
879bfdc2 2039 if (val)
eacd795a 2040 error_flag = val;
879bfdc2 2041 }
c906108c 2042
4a64f543
MS
2043 /* If we failed to insert all locations of a watchpoint, remove
2044 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2045 ALL_BREAKPOINTS (bpt)
2046 {
2047 int some_failed = 0;
2048 struct bp_location *loc;
2049
2050 if (!is_hardware_watchpoint (bpt))
2051 continue;
2052
d6b74ac4 2053 if (!breakpoint_enabled (bpt))
a5606eee 2054 continue;
74960c60
VP
2055
2056 if (bpt->disposition == disp_del_at_next_stop)
2057 continue;
a5606eee
VP
2058
2059 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2060 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2061 {
2062 some_failed = 1;
2063 break;
2064 }
2065 if (some_failed)
2066 {
2067 for (loc = bpt->loc; loc; loc = loc->next)
2068 if (loc->inserted)
2069 remove_breakpoint (loc, mark_uninserted);
2070
2071 hw_breakpoint_error = 1;
2072 fprintf_unfiltered (tmp_error_stream,
2073 "Could not insert hardware watchpoint %d.\n",
2074 bpt->number);
eacd795a 2075 error_flag = -1;
a5606eee
VP
2076 }
2077 }
2078
eacd795a 2079 if (error_flag)
81d0cc19
GS
2080 {
2081 /* If a hardware breakpoint or watchpoint was inserted, add a
2082 message about possibly exhausted resources. */
879bfdc2 2083 if (hw_breakpoint_error)
81d0cc19 2084 {
c6510018
MS
2085 fprintf_unfiltered (tmp_error_stream,
2086 "Could not insert hardware breakpoints:\n\
2087You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2088 }
81d0cc19
GS
2089 target_terminal_ours_for_output ();
2090 error_stream (tmp_error_stream);
2091 }
f7545552
TT
2092
2093 do_cleanups (cleanups);
c906108c
SS
2094}
2095
c30eee59
TJB
2096/* Used when the program stops.
2097 Returns zero if successful, or non-zero if there was a problem
2098 removing a breakpoint location. */
2099
c906108c 2100int
fba45db2 2101remove_breakpoints (void)
c906108c 2102{
35df4500 2103 struct bp_location *bl, **blp_tmp;
3a1bae8e 2104 int val = 0;
c906108c 2105
35df4500 2106 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2107 {
1e4d1764 2108 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2109 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2110 }
3a1bae8e 2111 return val;
c906108c
SS
2112}
2113
6c95b8df
PA
2114/* Remove breakpoints of process PID. */
2115
2116int
2117remove_breakpoints_pid (int pid)
2118{
35df4500 2119 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2120 int val;
2121 struct inferior *inf = find_inferior_pid (pid);
2122
35df4500 2123 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2124 {
35df4500 2125 if (bl->pspace != inf->pspace)
6c95b8df
PA
2126 continue;
2127
35df4500 2128 if (bl->inserted)
6c95b8df 2129 {
35df4500 2130 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2131 if (val != 0)
2132 return val;
2133 }
2134 }
2135 return 0;
2136}
2137
c906108c 2138int
fba45db2 2139reattach_breakpoints (int pid)
c906108c 2140{
6c95b8df 2141 struct cleanup *old_chain;
35df4500 2142 struct bp_location *bl, **blp_tmp;
c906108c 2143 int val;
86b887df 2144 struct ui_file *tmp_error_stream;
3fbb6ffa 2145 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2146 struct inferior *inf;
2147 struct thread_info *tp;
2148
2149 tp = any_live_thread_of_process (pid);
2150 if (tp == NULL)
2151 return 1;
2152
2153 inf = find_inferior_pid (pid);
2154 old_chain = save_inferior_ptid ();
2155
2156 inferior_ptid = tp->ptid;
a4954f26 2157
86b887df 2158 tmp_error_stream = mem_fileopen ();
a4954f26 2159 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2160
35df4500 2161 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2162 {
35df4500 2163 if (bl->pspace != inf->pspace)
6c95b8df
PA
2164 continue;
2165
35df4500 2166 if (bl->inserted)
c5aa993b 2167 {
35df4500 2168 bl->inserted = 0;
3fbb6ffa 2169 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2170 if (val != 0)
2171 {
ce696e05 2172 do_cleanups (old_chain);
c5aa993b
JM
2173 return val;
2174 }
2175 }
2176 }
ce696e05 2177 do_cleanups (old_chain);
c906108c
SS
2178 return 0;
2179}
2180
e58b0e63
PA
2181static int internal_breakpoint_number = -1;
2182
84f4c1fe
PM
2183/* Set the breakpoint number of B, depending on the value of INTERNAL.
2184 If INTERNAL is non-zero, the breakpoint number will be populated
2185 from internal_breakpoint_number and that variable decremented.
e5dd4106 2186 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2187 breakpoint_count and that value incremented. Internal breakpoints
2188 do not set the internal var bpnum. */
2189static void
2190set_breakpoint_number (int internal, struct breakpoint *b)
2191{
2192 if (internal)
2193 b->number = internal_breakpoint_number--;
2194 else
2195 {
2196 set_breakpoint_count (breakpoint_count + 1);
2197 b->number = breakpoint_count;
2198 }
2199}
2200
e62c965a 2201static struct breakpoint *
a6d9a66e 2202create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2203 CORE_ADDR address, enum bptype type,
c0a91b2b 2204 const struct breakpoint_ops *ops)
e62c965a 2205{
e62c965a
PP
2206 struct symtab_and_line sal;
2207 struct breakpoint *b;
2208
4a64f543 2209 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2210
2211 sal.pc = address;
2212 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2213 sal.pspace = current_program_space;
e62c965a 2214
06edf0c0 2215 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2216 b->number = internal_breakpoint_number--;
2217 b->disposition = disp_donttouch;
2218
2219 return b;
2220}
2221
17450429
PP
2222static const char *const longjmp_names[] =
2223 {
2224 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2225 };
2226#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2227
2228/* Per-objfile data private to breakpoint.c. */
2229struct breakpoint_objfile_data
2230{
2231 /* Minimal symbol for "_ovly_debug_event" (if any). */
2232 struct minimal_symbol *overlay_msym;
2233
2234 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2235 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2236
2237 /* Minimal symbol for "std::terminate()" (if any). */
2238 struct minimal_symbol *terminate_msym;
2239
2240 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2241 struct minimal_symbol *exception_msym;
2242};
2243
2244static const struct objfile_data *breakpoint_objfile_key;
2245
2246/* Minimal symbol not found sentinel. */
2247static struct minimal_symbol msym_not_found;
2248
2249/* Returns TRUE if MSYM point to the "not found" sentinel. */
2250
2251static int
2252msym_not_found_p (const struct minimal_symbol *msym)
2253{
2254 return msym == &msym_not_found;
2255}
2256
2257/* Return per-objfile data needed by breakpoint.c.
2258 Allocate the data if necessary. */
2259
2260static struct breakpoint_objfile_data *
2261get_breakpoint_objfile_data (struct objfile *objfile)
2262{
2263 struct breakpoint_objfile_data *bp_objfile_data;
2264
2265 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2266 if (bp_objfile_data == NULL)
2267 {
2268 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2269 sizeof (*bp_objfile_data));
2270
2271 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2272 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2273 }
2274 return bp_objfile_data;
2275}
2276
e62c965a 2277static void
af02033e 2278create_overlay_event_breakpoint (void)
e62c965a 2279{
69de3c6a 2280 struct objfile *objfile;
af02033e 2281 const char *const func_name = "_ovly_debug_event";
e62c965a 2282
69de3c6a
PP
2283 ALL_OBJFILES (objfile)
2284 {
2285 struct breakpoint *b;
17450429
PP
2286 struct breakpoint_objfile_data *bp_objfile_data;
2287 CORE_ADDR addr;
69de3c6a 2288
17450429
PP
2289 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2290
2291 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2292 continue;
2293
2294 if (bp_objfile_data->overlay_msym == NULL)
2295 {
2296 struct minimal_symbol *m;
2297
2298 m = lookup_minimal_symbol_text (func_name, objfile);
2299 if (m == NULL)
2300 {
2301 /* Avoid future lookups in this objfile. */
2302 bp_objfile_data->overlay_msym = &msym_not_found;
2303 continue;
2304 }
2305 bp_objfile_data->overlay_msym = m;
2306 }
e62c965a 2307
17450429
PP
2308 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2309 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2310 bp_overlay_event,
2311 &internal_breakpoint_ops);
69de3c6a 2312 b->addr_string = xstrdup (func_name);
e62c965a 2313
69de3c6a
PP
2314 if (overlay_debugging == ovly_auto)
2315 {
2316 b->enable_state = bp_enabled;
2317 overlay_events_enabled = 1;
2318 }
2319 else
2320 {
2321 b->enable_state = bp_disabled;
2322 overlay_events_enabled = 0;
2323 }
e62c965a
PP
2324 }
2325 update_global_location_list (1);
2326}
2327
0fd8e87f 2328static void
af02033e 2329create_longjmp_master_breakpoint (void)
0fd8e87f 2330{
6c95b8df 2331 struct program_space *pspace;
6c95b8df
PA
2332 struct cleanup *old_chain;
2333
2334 old_chain = save_current_program_space ();
0fd8e87f 2335
6c95b8df 2336 ALL_PSPACES (pspace)
af02033e
PP
2337 {
2338 struct objfile *objfile;
2339
2340 set_current_program_space (pspace);
2341
2342 ALL_OBJFILES (objfile)
0fd8e87f 2343 {
af02033e
PP
2344 int i;
2345 struct gdbarch *gdbarch;
17450429 2346 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2347
af02033e
PP
2348 gdbarch = get_objfile_arch (objfile);
2349 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2350 continue;
2351
17450429
PP
2352 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2353
2354 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2355 {
2356 struct breakpoint *b;
af02033e 2357 const char *func_name;
17450429 2358 CORE_ADDR addr;
6c95b8df 2359
17450429 2360 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2361 continue;
0fd8e87f 2362
17450429
PP
2363 func_name = longjmp_names[i];
2364 if (bp_objfile_data->longjmp_msym[i] == NULL)
2365 {
2366 struct minimal_symbol *m;
2367
2368 m = lookup_minimal_symbol_text (func_name, objfile);
2369 if (m == NULL)
2370 {
2371 /* Prevent future lookups in this objfile. */
2372 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2373 continue;
2374 }
2375 bp_objfile_data->longjmp_msym[i] = m;
2376 }
2377
2378 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2379 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2380 &internal_breakpoint_ops);
af02033e
PP
2381 b->addr_string = xstrdup (func_name);
2382 b->enable_state = bp_disabled;
2383 }
0fd8e87f 2384 }
af02033e 2385 }
0fd8e87f 2386 update_global_location_list (1);
6c95b8df
PA
2387
2388 do_cleanups (old_chain);
0fd8e87f
UW
2389}
2390
af02033e 2391/* Create a master std::terminate breakpoint. */
aa7d318d 2392static void
af02033e 2393create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2394{
2395 struct program_space *pspace;
aa7d318d 2396 struct cleanup *old_chain;
af02033e 2397 const char *const func_name = "std::terminate()";
aa7d318d
TT
2398
2399 old_chain = save_current_program_space ();
2400
2401 ALL_PSPACES (pspace)
17450429
PP
2402 {
2403 struct objfile *objfile;
2404 CORE_ADDR addr;
2405
2406 set_current_program_space (pspace);
2407
aa7d318d
TT
2408 ALL_OBJFILES (objfile)
2409 {
2410 struct breakpoint *b;
17450429 2411 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2412
17450429 2413 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2414
17450429
PP
2415 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2416 continue;
2417
2418 if (bp_objfile_data->terminate_msym == NULL)
2419 {
2420 struct minimal_symbol *m;
2421
2422 m = lookup_minimal_symbol (func_name, NULL, objfile);
2423 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2424 && MSYMBOL_TYPE (m) != mst_file_text))
2425 {
2426 /* Prevent future lookups in this objfile. */
2427 bp_objfile_data->terminate_msym = &msym_not_found;
2428 continue;
2429 }
2430 bp_objfile_data->terminate_msym = m;
2431 }
aa7d318d 2432
17450429
PP
2433 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2434 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2435 bp_std_terminate_master,
2436 &internal_breakpoint_ops);
aa7d318d
TT
2437 b->addr_string = xstrdup (func_name);
2438 b->enable_state = bp_disabled;
2439 }
17450429
PP
2440 }
2441
aa7d318d
TT
2442 update_global_location_list (1);
2443
2444 do_cleanups (old_chain);
2445}
2446
186c406b
TT
2447/* Install a master breakpoint on the unwinder's debug hook. */
2448
2449void
2450create_exception_master_breakpoint (void)
2451{
2452 struct objfile *objfile;
17450429 2453 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2454
2455 ALL_OBJFILES (objfile)
2456 {
17450429
PP
2457 struct breakpoint *b;
2458 struct gdbarch *gdbarch;
2459 struct breakpoint_objfile_data *bp_objfile_data;
2460 CORE_ADDR addr;
2461
2462 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2463
2464 if (msym_not_found_p (bp_objfile_data->exception_msym))
2465 continue;
2466
2467 gdbarch = get_objfile_arch (objfile);
186c406b 2468
17450429 2469 if (bp_objfile_data->exception_msym == NULL)
186c406b 2470 {
17450429 2471 struct minimal_symbol *debug_hook;
186c406b 2472
17450429
PP
2473 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2474 if (debug_hook == NULL)
2475 {
2476 bp_objfile_data->exception_msym = &msym_not_found;
2477 continue;
2478 }
2479
2480 bp_objfile_data->exception_msym = debug_hook;
186c406b 2481 }
17450429
PP
2482
2483 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2484 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2485 &current_target);
06edf0c0
PA
2486 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2487 &internal_breakpoint_ops);
17450429
PP
2488 b->addr_string = xstrdup (func_name);
2489 b->enable_state = bp_disabled;
186c406b
TT
2490 }
2491
2492 update_global_location_list (1);
2493}
2494
c906108c 2495void
fba45db2 2496update_breakpoints_after_exec (void)
c906108c 2497{
35df4500 2498 struct breakpoint *b, *b_tmp;
876fa593 2499 struct bp_location *bploc, **bplocp_tmp;
c906108c 2500
25b22b0a
PA
2501 /* We're about to delete breakpoints from GDB's lists. If the
2502 INSERTED flag is true, GDB will try to lift the breakpoints by
2503 writing the breakpoints' "shadow contents" back into memory. The
2504 "shadow contents" are NOT valid after an exec, so GDB should not
2505 do that. Instead, the target is responsible from marking
2506 breakpoints out as soon as it detects an exec. We don't do that
2507 here instead, because there may be other attempts to delete
2508 breakpoints after detecting an exec and before reaching here. */
876fa593 2509 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2510 if (bploc->pspace == current_program_space)
2511 gdb_assert (!bploc->inserted);
c906108c 2512
35df4500 2513 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2514 {
6c95b8df
PA
2515 if (b->pspace != current_program_space)
2516 continue;
2517
4a64f543 2518 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2519 if (b->type == bp_shlib_event)
2520 {
2521 delete_breakpoint (b);
2522 continue;
2523 }
c906108c 2524
4a64f543 2525 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2526 if (b->type == bp_jit_event)
2527 {
2528 delete_breakpoint (b);
2529 continue;
2530 }
2531
1900040c 2532 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2533 as must overlay event and longjmp master breakpoints. */
2534 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2535 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2536 || b->type == bp_exception_master)
c4093a6a
JM
2537 {
2538 delete_breakpoint (b);
2539 continue;
2540 }
2541
4a64f543 2542 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 2543 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
2544 {
2545 delete_breakpoint (b);
2546 continue;
2547 }
2548
611c83ae
PA
2549 /* Longjmp and longjmp-resume breakpoints are also meaningless
2550 after an exec. */
186c406b
TT
2551 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2552 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2553 {
2554 delete_breakpoint (b);
2555 continue;
2556 }
2557
ce78b96d
JB
2558 if (b->type == bp_catchpoint)
2559 {
2560 /* For now, none of the bp_catchpoint breakpoints need to
2561 do anything at this point. In the future, if some of
2562 the catchpoints need to something, we will need to add
2563 a new method, and call this method from here. */
2564 continue;
2565 }
2566
c5aa993b
JM
2567 /* bp_finish is a special case. The only way we ought to be able
2568 to see one of these when an exec() has happened, is if the user
2569 caught a vfork, and then said "finish". Ordinarily a finish just
2570 carries them to the call-site of the current callee, by setting
2571 a temporary bp there and resuming. But in this case, the finish
2572 will carry them entirely through the vfork & exec.
2573
2574 We don't want to allow a bp_finish to remain inserted now. But
2575 we can't safely delete it, 'cause finish_command has a handle to
2576 the bp on a bpstat, and will later want to delete it. There's a
2577 chance (and I've seen it happen) that if we delete the bp_finish
2578 here, that its storage will get reused by the time finish_command
2579 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2580 We really must allow finish_command to delete a bp_finish.
2581
e5dd4106 2582 In the absence of a general solution for the "how do we know
53a5351d
JM
2583 it's safe to delete something others may have handles to?"
2584 problem, what we'll do here is just uninsert the bp_finish, and
2585 let finish_command delete it.
2586
2587 (We know the bp_finish is "doomed" in the sense that it's
2588 momentary, and will be deleted as soon as finish_command sees
2589 the inferior stopped. So it doesn't matter that the bp's
2590 address is probably bogus in the new a.out, unlike e.g., the
2591 solib breakpoints.) */
c5aa993b 2592
c5aa993b
JM
2593 if (b->type == bp_finish)
2594 {
2595 continue;
2596 }
2597
2598 /* Without a symbolic address, we have little hope of the
2599 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2600 a.out. */
c5aa993b
JM
2601 if (b->addr_string == NULL)
2602 {
2603 delete_breakpoint (b);
2604 continue;
2605 }
c5aa993b 2606 }
1900040c 2607 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2608 create_overlay_event_breakpoint ();
2609 create_longjmp_master_breakpoint ();
2610 create_std_terminate_master_breakpoint ();
186c406b 2611 create_exception_master_breakpoint ();
c906108c
SS
2612}
2613
2614int
fba45db2 2615detach_breakpoints (int pid)
c906108c 2616{
35df4500 2617 struct bp_location *bl, **blp_tmp;
3a1bae8e 2618 int val = 0;
ce696e05 2619 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2620 struct inferior *inf = current_inferior ();
c5aa993b 2621
39f77062 2622 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2623 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2624
6c95b8df 2625 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2626 inferior_ptid = pid_to_ptid (pid);
35df4500 2627 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2628 {
35df4500 2629 if (bl->pspace != inf->pspace)
6c95b8df
PA
2630 continue;
2631
35df4500
TJB
2632 if (bl->inserted)
2633 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2634 }
d03285ec
UW
2635
2636 /* Detach single-step breakpoints as well. */
2637 detach_single_step_breakpoints ();
2638
ce696e05 2639 do_cleanups (old_chain);
3a1bae8e 2640 return val;
c906108c
SS
2641}
2642
35df4500 2643/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2644 Note that this is used to detach breakpoints from a child fork.
2645 When we get here, the child isn't in the inferior list, and neither
2646 do we have objects to represent its address space --- we should
35df4500 2647 *not* look at bl->pspace->aspace here. */
6c95b8df 2648
c906108c 2649static int
35df4500 2650remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2651{
2652 int val;
c5aa993b 2653
35df4500
TJB
2654 /* BL is never in moribund_locations by our callers. */
2655 gdb_assert (bl->owner != NULL);
2bdf28a0 2656
35df4500 2657 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2658 /* Permanent breakpoints cannot be inserted or removed. */
2659 return 0;
2660
74960c60
VP
2661 /* The type of none suggests that owner is actually deleted.
2662 This should not ever happen. */
35df4500 2663 gdb_assert (bl->owner->type != bp_none);
0bde7532 2664
35df4500
TJB
2665 if (bl->loc_type == bp_loc_software_breakpoint
2666 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2667 {
c02f5703
MS
2668 /* "Normal" instruction breakpoint: either the standard
2669 trap-instruction bp (bp_breakpoint), or a
2670 bp_hardware_breakpoint. */
2671
2672 /* First check to see if we have to handle an overlay. */
2673 if (overlay_debugging == ovly_off
35df4500
TJB
2674 || bl->section == NULL
2675 || !(section_is_overlay (bl->section)))
c02f5703
MS
2676 {
2677 /* No overlay handling: just remove the breakpoint. */
348d480f 2678 val = bl->owner->ops->remove_location (bl);
c02f5703 2679 }
c906108c
SS
2680 else
2681 {
4a64f543 2682 /* This breakpoint is in an overlay section.
c02f5703
MS
2683 Did we set a breakpoint at the LMA? */
2684 if (!overlay_events_enabled)
2685 {
2686 /* Yes -- overlay event support is not active, so we
2687 should have set a breakpoint at the LMA. Remove it.
2688 */
c02f5703
MS
2689 /* Ignore any failures: if the LMA is in ROM, we will
2690 have already warned when we failed to insert it. */
35df4500
TJB
2691 if (bl->loc_type == bp_loc_hardware_breakpoint)
2692 target_remove_hw_breakpoint (bl->gdbarch,
2693 &bl->overlay_target_info);
c02f5703 2694 else
35df4500
TJB
2695 target_remove_breakpoint (bl->gdbarch,
2696 &bl->overlay_target_info);
c02f5703
MS
2697 }
2698 /* Did we set a breakpoint at the VMA?
2699 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2700 if (bl->inserted)
c906108c 2701 {
c02f5703
MS
2702 /* Yes -- remove it. Previously we did not bother to
2703 remove the breakpoint if the section had been
2704 unmapped, but let's not rely on that being safe. We
2705 don't know what the overlay manager might do. */
aa67235e
UW
2706
2707 /* However, we should remove *software* breakpoints only
2708 if the section is still mapped, or else we overwrite
2709 wrong code with the saved shadow contents. */
348d480f
PA
2710 if (bl->loc_type == bp_loc_hardware_breakpoint
2711 || section_is_mapped (bl->section))
2712 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
2713 else
2714 val = 0;
c906108c 2715 }
c02f5703
MS
2716 else
2717 {
2718 /* No -- not inserted, so no need to remove. No error. */
2719 val = 0;
2720 }
c906108c 2721 }
879d1e6b
UW
2722
2723 /* In some cases, we might not be able to remove a breakpoint
2724 in a shared library that has already been removed, but we
2725 have not yet processed the shlib unload event. */
35df4500 2726 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2727 val = 0;
2728
c906108c
SS
2729 if (val)
2730 return val;
35df4500 2731 bl->inserted = (is == mark_inserted);
c906108c 2732 }
35df4500 2733 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2734 {
77b06cd7
TJB
2735 gdb_assert (bl->owner->ops != NULL
2736 && bl->owner->ops->remove_location != NULL);
2737
35df4500 2738 bl->inserted = (is == mark_inserted);
77b06cd7 2739 bl->owner->ops->remove_location (bl);
2e70b7b9 2740
c906108c 2741 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2742 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2743 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2744 bl->owner->number);
c906108c 2745 }
35df4500
TJB
2746 else if (bl->owner->type == bp_catchpoint
2747 && breakpoint_enabled (bl->owner)
2748 && !bl->duplicate)
ce78b96d 2749 {
77b06cd7
TJB
2750 gdb_assert (bl->owner->ops != NULL
2751 && bl->owner->ops->remove_location != NULL);
ce78b96d 2752
77b06cd7 2753 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2754 if (val)
2755 return val;
77b06cd7 2756
35df4500 2757 bl->inserted = (is == mark_inserted);
ce78b96d 2758 }
c906108c
SS
2759
2760 return 0;
2761}
2762
6c95b8df 2763static int
35df4500 2764remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2765{
2766 int ret;
2767 struct cleanup *old_chain;
2768
35df4500
TJB
2769 /* BL is never in moribund_locations by our callers. */
2770 gdb_assert (bl->owner != NULL);
2bdf28a0 2771
35df4500 2772 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2773 /* Permanent breakpoints cannot be inserted or removed. */
2774 return 0;
2775
2776 /* The type of none suggests that owner is actually deleted.
2777 This should not ever happen. */
35df4500 2778 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2779
2780 old_chain = save_current_space_and_thread ();
2781
35df4500 2782 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2783
35df4500 2784 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2785
2786 do_cleanups (old_chain);
2787 return ret;
2788}
2789
c906108c
SS
2790/* Clear the "inserted" flag in all breakpoints. */
2791
25b22b0a 2792void
fba45db2 2793mark_breakpoints_out (void)
c906108c 2794{
35df4500 2795 struct bp_location *bl, **blp_tmp;
c906108c 2796
35df4500
TJB
2797 ALL_BP_LOCATIONS (bl, blp_tmp)
2798 if (bl->pspace == current_program_space)
2799 bl->inserted = 0;
c906108c
SS
2800}
2801
53a5351d
JM
2802/* Clear the "inserted" flag in all breakpoints and delete any
2803 breakpoints which should go away between runs of the program.
c906108c
SS
2804
2805 Plus other such housekeeping that has to be done for breakpoints
2806 between runs.
2807
53a5351d
JM
2808 Note: this function gets called at the end of a run (by
2809 generic_mourn_inferior) and when a run begins (by
4a64f543 2810 init_wait_for_inferior). */
c906108c
SS
2811
2812
2813
2814void
fba45db2 2815breakpoint_init_inferior (enum inf_context context)
c906108c 2816{
35df4500
TJB
2817 struct breakpoint *b, *b_tmp;
2818 struct bp_location *bl, **blp_tmp;
1c5cfe86 2819 int ix;
6c95b8df 2820 struct program_space *pspace = current_program_space;
c906108c 2821
50c71eaf
PA
2822 /* If breakpoint locations are shared across processes, then there's
2823 nothing to do. */
2567c7d9 2824 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2825 return;
2826
35df4500 2827 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2828 {
35df4500
TJB
2829 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2830 if (bl->pspace == pspace
2831 && bl->owner->enable_state != bp_permanent)
2832 bl->inserted = 0;
6c95b8df 2833 }
075f6582 2834
35df4500 2835 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2836 {
6c95b8df
PA
2837 if (b->loc && b->loc->pspace != pspace)
2838 continue;
2839
c5aa993b
JM
2840 switch (b->type)
2841 {
2842 case bp_call_dummy:
c906108c 2843
c5aa993b 2844 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2845 cause problems when the inferior is rerun, so we better get
2846 rid of it. */
2847
2848 case bp_watchpoint_scope:
2849
2850 /* Also get rid of scope breakpoints. */
2851
2852 case bp_shlib_event:
2853
2854 /* Also remove solib event breakpoints. Their addresses may
2855 have changed since the last time we ran the program.
2856 Actually we may now be debugging against different target;
2857 and so the solib backend that installed this breakpoint may
2858 not be used in by the target. E.g.,
2859
2860 (gdb) file prog-linux
2861 (gdb) run # native linux target
2862 ...
2863 (gdb) kill
2864 (gdb) file prog-win.exe
2865 (gdb) tar rem :9999 # remote Windows gdbserver.
2866 */
c906108c 2867
c5aa993b
JM
2868 delete_breakpoint (b);
2869 break;
c906108c 2870
c5aa993b
JM
2871 case bp_watchpoint:
2872 case bp_hardware_watchpoint:
2873 case bp_read_watchpoint:
2874 case bp_access_watchpoint:
3a5c3e22
PA
2875 {
2876 struct watchpoint *w = (struct watchpoint *) b;
c906108c 2877
3a5c3e22
PA
2878 /* Likewise for watchpoints on local expressions. */
2879 if (w->exp_valid_block != NULL)
2880 delete_breakpoint (b);
2881 else if (context == inf_starting)
2882 {
2883 /* Reset val field to force reread of starting value in
2884 insert_breakpoints. */
2885 if (w->val)
2886 value_free (w->val);
2887 w->val = NULL;
2888 w->val_valid = 0;
c860120c 2889 }
3a5c3e22 2890 }
c5aa993b
JM
2891 break;
2892 default:
c5aa993b
JM
2893 break;
2894 }
2895 }
1c5cfe86
PA
2896
2897 /* Get rid of the moribund locations. */
35df4500
TJB
2898 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2899 decref_bp_location (&bl);
1c5cfe86 2900 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2901}
2902
6c95b8df
PA
2903/* These functions concern about actual breakpoints inserted in the
2904 target --- to e.g. check if we need to do decr_pc adjustment or if
2905 we need to hop over the bkpt --- so we check for address space
2906 match, not program space. */
2907
c2c6d25f
JM
2908/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2909 exists at PC. It returns ordinary_breakpoint_here if it's an
2910 ordinary breakpoint, or permanent_breakpoint_here if it's a
2911 permanent breakpoint.
2912 - When continuing from a location with an ordinary breakpoint, we
2913 actually single step once before calling insert_breakpoints.
e5dd4106 2914 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
2915 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2916 the target, to advance the PC past the breakpoint. */
c906108c 2917
c2c6d25f 2918enum breakpoint_here
6c95b8df 2919breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2920{
35df4500 2921 struct bp_location *bl, **blp_tmp;
c2c6d25f 2922 int any_breakpoint_here = 0;
c906108c 2923
35df4500 2924 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2925 {
35df4500
TJB
2926 if (bl->loc_type != bp_loc_software_breakpoint
2927 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2928 continue;
2929
f1310107 2930 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2931 if ((breakpoint_enabled (bl->owner)
2932 || bl->owner->enable_state == bp_permanent)
f1310107 2933 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2934 {
2935 if (overlay_debugging
35df4500
TJB
2936 && section_is_overlay (bl->section)
2937 && !section_is_mapped (bl->section))
075f6582 2938 continue; /* unmapped overlay -- can't be a match */
35df4500 2939 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2940 return permanent_breakpoint_here;
2941 else
2942 any_breakpoint_here = 1;
2943 }
2944 }
c906108c 2945
c2c6d25f 2946 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2947}
2948
1c5cfe86
PA
2949/* Return true if there's a moribund breakpoint at PC. */
2950
2951int
6c95b8df 2952moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2953{
2954 struct bp_location *loc;
2955 int ix;
2956
2957 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2958 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2959 return 1;
2960
2961 return 0;
2962}
c2c6d25f 2963
c36b740a 2964/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2965 inserted using regular breakpoint_chain / bp_location array
2966 mechanism. This does not check for single-step breakpoints, which
2967 are inserted and removed using direct target manipulation. */
c906108c
SS
2968
2969int
4a64f543
MS
2970regular_breakpoint_inserted_here_p (struct address_space *aspace,
2971 CORE_ADDR pc)
c906108c 2972{
35df4500 2973 struct bp_location *bl, **blp_tmp;
c906108c 2974
35df4500 2975 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2976 {
35df4500
TJB
2977 if (bl->loc_type != bp_loc_software_breakpoint
2978 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2979 continue;
2980
35df4500 2981 if (bl->inserted
f1310107 2982 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2983 {
2984 if (overlay_debugging
35df4500
TJB
2985 && section_is_overlay (bl->section)
2986 && !section_is_mapped (bl->section))
075f6582
DJ
2987 continue; /* unmapped overlay -- can't be a match */
2988 else
2989 return 1;
2990 }
c5aa993b 2991 }
c36b740a
VP
2992 return 0;
2993}
2994
2995/* Returns non-zero iff there's either regular breakpoint
2996 or a single step breakpoint inserted at PC. */
2997
2998int
6c95b8df 2999breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 3000{
6c95b8df 3001 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 3002 return 1;
c906108c 3003
6c95b8df 3004 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3005 return 1;
3006
c906108c
SS
3007 return 0;
3008}
3009
4fa8626c
DJ
3010/* This function returns non-zero iff there is a software breakpoint
3011 inserted at PC. */
3012
3013int
3e43a32a
MS
3014software_breakpoint_inserted_here_p (struct address_space *aspace,
3015 CORE_ADDR pc)
4fa8626c 3016{
35df4500 3017 struct bp_location *bl, **blp_tmp;
4fa8626c 3018
35df4500 3019 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 3020 {
35df4500 3021 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
3022 continue;
3023
35df4500
TJB
3024 if (bl->inserted
3025 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 3026 aspace, pc))
4fa8626c
DJ
3027 {
3028 if (overlay_debugging
35df4500
TJB
3029 && section_is_overlay (bl->section)
3030 && !section_is_mapped (bl->section))
4fa8626c
DJ
3031 continue; /* unmapped overlay -- can't be a match */
3032 else
3033 return 1;
3034 }
3035 }
3036
1aafd4da 3037 /* Also check for software single-step breakpoints. */
6c95b8df 3038 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3039 return 1;
3040
4fa8626c
DJ
3041 return 0;
3042}
3043
9093389c
PA
3044int
3045hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3046 CORE_ADDR addr, ULONGEST len)
3047{
3048 struct breakpoint *bpt;
3049
3050 ALL_BREAKPOINTS (bpt)
3051 {
3052 struct bp_location *loc;
3053
3054 if (bpt->type != bp_hardware_watchpoint
3055 && bpt->type != bp_access_watchpoint)
3056 continue;
3057
3058 if (!breakpoint_enabled (bpt))
3059 continue;
3060
3061 for (loc = bpt->loc; loc; loc = loc->next)
3062 if (loc->pspace->aspace == aspace && loc->inserted)
3063 {
3064 CORE_ADDR l, h;
3065
3066 /* Check for intersection. */
3067 l = max (loc->address, addr);
3068 h = min (loc->address + loc->length, addr + len);
3069 if (l < h)
3070 return 1;
3071 }
3072 }
3073 return 0;
3074}
3075
075f6582
DJ
3076/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3077 PC is valid for process/thread PTID. */
c906108c
SS
3078
3079int
6c95b8df
PA
3080breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3081 ptid_t ptid)
c906108c 3082{
35df4500 3083 struct bp_location *bl, **blp_tmp;
4a306c9a 3084 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3085 int thread = -1;
4a306c9a 3086 int task = 0;
a6f1cd96 3087
35df4500 3088 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3089 {
35df4500
TJB
3090 if (bl->loc_type != bp_loc_software_breakpoint
3091 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3092 continue;
3093
35df4500
TJB
3094 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3095 if (!breakpoint_enabled (bl->owner)
3096 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3097 continue;
3098
f1310107 3099 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3100 continue;
3101
35df4500 3102 if (bl->owner->thread != -1)
075f6582 3103 {
a6f1cd96
JB
3104 /* This is a thread-specific breakpoint. Check that ptid
3105 matches that thread. If thread hasn't been computed yet,
3106 it is now time to do so. */
3107 if (thread == -1)
3108 thread = pid_to_thread_id (ptid);
35df4500 3109 if (bl->owner->thread != thread)
a6f1cd96 3110 continue;
075f6582 3111 }
a6f1cd96 3112
35df4500 3113 if (bl->owner->task != 0)
4a306c9a
JB
3114 {
3115 /* This is a task-specific breakpoint. Check that ptid
3116 matches that task. If task hasn't been computed yet,
3117 it is now time to do so. */
3118 if (task == 0)
3119 task = ada_get_task_number (ptid);
35df4500 3120 if (bl->owner->task != task)
4a306c9a
JB
3121 continue;
3122 }
3123
a6f1cd96 3124 if (overlay_debugging
35df4500
TJB
3125 && section_is_overlay (bl->section)
3126 && !section_is_mapped (bl->section))
a6f1cd96
JB
3127 continue; /* unmapped overlay -- can't be a match */
3128
3129 return 1;
c5aa993b 3130 }
c906108c
SS
3131
3132 return 0;
3133}
c906108c 3134\f
c5aa993b 3135
c906108c
SS
3136/* bpstat stuff. External routines' interfaces are documented
3137 in breakpoint.h. */
3138
3139int
fba45db2 3140ep_is_catchpoint (struct breakpoint *ep)
c906108c 3141{
533be4dd 3142 return (ep->type == bp_catchpoint);
c906108c
SS
3143}
3144
f431efe5
PA
3145/* Frees any storage that is part of a bpstat. Does not walk the
3146 'next' chain. */
3147
3148static void
198757a8
VP
3149bpstat_free (bpstat bs)
3150{
3151 if (bs->old_val != NULL)
3152 value_free (bs->old_val);
9add0f1b 3153 decref_counted_command_line (&bs->commands);
f431efe5 3154 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3155 xfree (bs);
3156}
3157
c906108c
SS
3158/* Clear a bpstat so that it says we are not at any breakpoint.
3159 Also free any storage that is part of a bpstat. */
3160
3161void
fba45db2 3162bpstat_clear (bpstat *bsp)
c906108c
SS
3163{
3164 bpstat p;
3165 bpstat q;
3166
3167 if (bsp == 0)
3168 return;
3169 p = *bsp;
3170 while (p != NULL)
3171 {
3172 q = p->next;
198757a8 3173 bpstat_free (p);
c906108c
SS
3174 p = q;
3175 }
3176 *bsp = NULL;
3177}
3178
3179/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3180 is part of the bpstat is copied as well. */
3181
3182bpstat
fba45db2 3183bpstat_copy (bpstat bs)
c906108c
SS
3184{
3185 bpstat p = NULL;
3186 bpstat tmp;
3187 bpstat retval = NULL;
3188
3189 if (bs == NULL)
3190 return bs;
3191
3192 for (; bs != NULL; bs = bs->next)
3193 {
3194 tmp = (bpstat) xmalloc (sizeof (*tmp));
3195 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3196 incref_counted_command_line (tmp->commands);
f431efe5 3197 incref_bp_location (tmp->bp_location_at);
31cc81e9 3198 if (bs->old_val != NULL)
3c3185ac
JK
3199 {
3200 tmp->old_val = value_copy (bs->old_val);
3201 release_value (tmp->old_val);
3202 }
31cc81e9 3203
c906108c
SS
3204 if (p == NULL)
3205 /* This is the first thing in the chain. */
3206 retval = tmp;
3207 else
3208 p->next = tmp;
3209 p = tmp;
3210 }
3211 p->next = NULL;
3212 return retval;
3213}
3214
4a64f543 3215/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3216
3217bpstat
fba45db2 3218bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3219{
c5aa993b
JM
3220 if (bsp == NULL)
3221 return NULL;
c906108c 3222
c5aa993b
JM
3223 for (; bsp != NULL; bsp = bsp->next)
3224 {
f431efe5 3225 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3226 return bsp;
3227 }
c906108c
SS
3228 return NULL;
3229}
3230
4a64f543
MS
3231/* Put in *NUM the breakpoint number of the first breakpoint we are
3232 stopped at. *BSP upon return is a bpstat which points to the
3233 remaining breakpoints stopped at (but which is not guaranteed to be
3234 good for anything but further calls to bpstat_num).
3235
8671a17b
PA
3236 Return 0 if passed a bpstat which does not indicate any breakpoints.
3237 Return -1 if stopped at a breakpoint that has been deleted since
3238 we set it.
3239 Return 1 otherwise. */
c906108c
SS
3240
3241int
8671a17b 3242bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3243{
3244 struct breakpoint *b;
3245
3246 if ((*bsp) == NULL)
3247 return 0; /* No more breakpoint values */
8671a17b 3248
4a64f543
MS
3249 /* We assume we'll never have several bpstats that correspond to a
3250 single breakpoint -- otherwise, this function might return the
3251 same number more than once and this will look ugly. */
f431efe5 3252 b = (*bsp)->breakpoint_at;
8671a17b
PA
3253 *bsp = (*bsp)->next;
3254 if (b == NULL)
3255 return -1; /* breakpoint that's been deleted since */
3256
3257 *num = b->number; /* We have its number */
3258 return 1;
c906108c
SS
3259}
3260
e93ca019 3261/* See breakpoint.h. */
c906108c
SS
3262
3263void
e93ca019 3264bpstat_clear_actions (void)
c906108c 3265{
e93ca019
JK
3266 struct thread_info *tp;
3267 bpstat bs;
3268
3269 if (ptid_equal (inferior_ptid, null_ptid))
3270 return;
3271
3272 tp = find_thread_ptid (inferior_ptid);
3273 if (tp == NULL)
3274 return;
3275
3276 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 3277 {
9add0f1b 3278 decref_counted_command_line (&bs->commands);
abf85f46 3279
c906108c
SS
3280 if (bs->old_val != NULL)
3281 {
3282 value_free (bs->old_val);
3283 bs->old_val = NULL;
3284 }
3285 }
3286}
3287
f3b1572e
PA
3288/* Called when a command is about to proceed the inferior. */
3289
3290static void
3291breakpoint_about_to_proceed (void)
3292{
3293 if (!ptid_equal (inferior_ptid, null_ptid))
3294 {
3295 struct thread_info *tp = inferior_thread ();
3296
3297 /* Allow inferior function calls in breakpoint commands to not
3298 interrupt the command list. When the call finishes
3299 successfully, the inferior will be standing at the same
3300 breakpoint as if nothing happened. */
16c381f0 3301 if (tp->control.in_infcall)
f3b1572e
PA
3302 return;
3303 }
3304
3305 breakpoint_proceeded = 1;
3306}
3307
4a64f543
MS
3308/* Stub for cleaning up our state if we error-out of a breakpoint
3309 command. */
c906108c 3310static void
4efb68b1 3311cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3312{
3313 executing_breakpoint_commands = 0;
3314}
3315
abf85f46
JK
3316/* Return non-zero iff CMD as the first line of a command sequence is `silent'
3317 or its equivalent. */
3318
3319static int
3320command_line_is_silent (struct command_line *cmd)
3321{
3322 return cmd && (strcmp ("silent", cmd->line) == 0
3323 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3324}
3325
4a64f543
MS
3326/* Execute all the commands associated with all the breakpoints at
3327 this location. Any of these commands could cause the process to
3328 proceed beyond this point, etc. We look out for such changes by
3329 checking the global "breakpoint_proceeded" after each command.
c906108c 3330
347bddb7
PA
3331 Returns true if a breakpoint command resumed the inferior. In that
3332 case, it is the caller's responsibility to recall it again with the
3333 bpstat of the current thread. */
3334
3335static int
3336bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3337{
3338 bpstat bs;
3339 struct cleanup *old_chain;
347bddb7 3340 int again = 0;
c906108c
SS
3341
3342 /* Avoid endless recursion if a `source' command is contained
3343 in bs->commands. */
3344 if (executing_breakpoint_commands)
347bddb7 3345 return 0;
c906108c
SS
3346
3347 executing_breakpoint_commands = 1;
3348 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3349
cf6c5ffb
TT
3350 prevent_dont_repeat ();
3351
4a64f543 3352 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3353 bs = *bsp;
3354
3355 breakpoint_proceeded = 0;
3356 for (; bs != NULL; bs = bs->next)
3357 {
9add0f1b 3358 struct counted_command_line *ccmd;
6c50ab1c
JB
3359 struct command_line *cmd;
3360 struct cleanup *this_cmd_tree_chain;
3361
3362 /* Take ownership of the BSP's command tree, if it has one.
3363
3364 The command tree could legitimately contain commands like
3365 'step' and 'next', which call clear_proceed_status, which
3366 frees stop_bpstat's command tree. To make sure this doesn't
3367 free the tree we're executing out from under us, we need to
3368 take ownership of the tree ourselves. Since a given bpstat's
3369 commands are only executed once, we don't need to copy it; we
3370 can clear the pointer in the bpstat, and make sure we free
3371 the tree when we're done. */
9add0f1b
TT
3372 ccmd = bs->commands;
3373 bs->commands = NULL;
abf85f46
JK
3374 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
3375 cmd = ccmd ? ccmd->commands : NULL;
3376 if (command_line_is_silent (cmd))
3377 {
3378 /* The action has been already done by bpstat_stop_status. */
3379 cmd = cmd->next;
3380 }
6c50ab1c 3381
c906108c
SS
3382 while (cmd != NULL)
3383 {
3384 execute_control_command (cmd);
3385
3386 if (breakpoint_proceeded)
3387 break;
3388 else
3389 cmd = cmd->next;
3390 }
6c50ab1c
JB
3391
3392 /* We can free this command tree now. */
3393 do_cleanups (this_cmd_tree_chain);
3394
c906108c 3395 if (breakpoint_proceeded)
32c1e744
VP
3396 {
3397 if (target_can_async_p ())
347bddb7
PA
3398 /* If we are in async mode, then the target might be still
3399 running, not stopped at any breakpoint, so nothing for
3400 us to do here -- just return to the event loop. */
3401 ;
32c1e744
VP
3402 else
3403 /* In sync mode, when execute_control_command returns
3404 we're already standing on the next breakpoint.
347bddb7
PA
3405 Breakpoint commands for that stop were not run, since
3406 execute_command does not run breakpoint commands --
3407 only command_line_handler does, but that one is not
3408 involved in execution of breakpoint commands. So, we
3409 can now execute breakpoint commands. It should be
3410 noted that making execute_command do bpstat actions is
3411 not an option -- in this case we'll have recursive
3412 invocation of bpstat for each breakpoint with a
3413 command, and can easily blow up GDB stack. Instead, we
3414 return true, which will trigger the caller to recall us
3415 with the new stop_bpstat. */
3416 again = 1;
3417 break;
32c1e744 3418 }
c906108c 3419 }
c2b8ed2c 3420 do_cleanups (old_chain);
347bddb7
PA
3421 return again;
3422}
3423
3424void
3425bpstat_do_actions (void)
3426{
353d1d73
JK
3427 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
3428
347bddb7
PA
3429 /* Do any commands attached to breakpoint we are stopped at. */
3430 while (!ptid_equal (inferior_ptid, null_ptid)
3431 && target_has_execution
3432 && !is_exited (inferior_ptid)
3433 && !is_executing (inferior_ptid))
3434 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3435 and only return when it is stopped at the next breakpoint, we
3436 keep doing breakpoint actions until it returns false to
3437 indicate the inferior was not resumed. */
16c381f0 3438 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3439 break;
353d1d73
JK
3440
3441 discard_cleanups (cleanup_if_error);
c906108c
SS
3442}
3443
fa4727a6
DJ
3444/* Print out the (old or new) value associated with a watchpoint. */
3445
3446static void
3447watchpoint_value_print (struct value *val, struct ui_file *stream)
3448{
3449 if (val == NULL)
3450 fprintf_unfiltered (stream, _("<unreadable>"));
3451 else
79a45b7d
TT
3452 {
3453 struct value_print_options opts;
3454 get_user_print_options (&opts);
3455 value_print (val, stream, &opts);
3456 }
fa4727a6
DJ
3457}
3458
e514a9d6 3459/* Generic routine for printing messages indicating why we
4a64f543 3460 stopped. The behavior of this function depends on the value
e514a9d6
JM
3461 'print_it' in the bpstat structure. Under some circumstances we
3462 may decide not to print anything here and delegate the task to
4a64f543 3463 normal_stop(). */
e514a9d6
JM
3464
3465static enum print_stop_action
3466print_bp_stop_message (bpstat bs)
3467{
3468 switch (bs->print_it)
3469 {
3470 case print_it_noop:
4a64f543 3471 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3472 return PRINT_UNKNOWN;
3473 break;
3474
3475 case print_it_done:
3476 /* We still want to print the frame, but we already printed the
4a64f543 3477 relevant messages. */
e514a9d6
JM
3478 return PRINT_SRC_AND_LOC;
3479 break;
3480
3481 case print_it_normal:
4f8d1dc6 3482 {
f431efe5
PA
3483 struct breakpoint *b = bs->breakpoint_at;
3484
1a6a67de
TJB
3485 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3486 which has since been deleted. */
3487 if (b == NULL)
3488 return PRINT_UNKNOWN;
3489
348d480f
PA
3490 /* Normal case. Call the breakpoint's print_it method. */
3491 return b->ops->print_it (bs);
4f8d1dc6 3492 }
348d480f 3493 break;
3086aeae 3494
e514a9d6 3495 default:
8e65ff28 3496 internal_error (__FILE__, __LINE__,
e2e0b3e5 3497 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3498 break;
c906108c 3499 }
c906108c
SS
3500}
3501
edcc5120
TT
3502/* A helper function that prints a shared library stopped event. */
3503
3504static void
3505print_solib_event (int is_catchpoint)
3506{
3507 int any_deleted
3508 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
3509 int any_added
3510 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
3511
3512 if (!is_catchpoint)
3513 {
3514 if (any_added || any_deleted)
3515 ui_out_text (current_uiout,
3516 _("Stopped due to shared library event:\n"));
3517 else
3518 ui_out_text (current_uiout,
3519 _("Stopped due to shared library event (no "
3520 "libraries added or removed)\n"));
3521 }
3522
3523 if (ui_out_is_mi_like_p (current_uiout))
3524 ui_out_field_string (current_uiout, "reason",
3525 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
3526
3527 if (any_deleted)
3528 {
3529 struct cleanup *cleanup;
3530 char *name;
3531 int ix;
3532
3533 ui_out_text (current_uiout, _(" Inferior unloaded "));
3534 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
3535 "removed");
3536 for (ix = 0;
3537 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
3538 ix, name);
3539 ++ix)
3540 {
3541 if (ix > 0)
3542 ui_out_text (current_uiout, " ");
3543 ui_out_field_string (current_uiout, "library", name);
3544 ui_out_text (current_uiout, "\n");
3545 }
3546
3547 do_cleanups (cleanup);
3548 }
3549
3550 if (any_added)
3551 {
3552 struct so_list *iter;
3553 int ix;
3554 struct cleanup *cleanup;
3555
3556 ui_out_text (current_uiout, _(" Inferior loaded "));
3557 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
3558 "added");
3559 for (ix = 0;
3560 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
3561 ix, iter);
3562 ++ix)
3563 {
3564 if (ix > 0)
3565 ui_out_text (current_uiout, " ");
3566 ui_out_field_string (current_uiout, "library", iter->so_name);
3567 ui_out_text (current_uiout, "\n");
3568 }
3569
3570 do_cleanups (cleanup);
3571 }
3572}
3573
e514a9d6
JM
3574/* Print a message indicating what happened. This is called from
3575 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
3576 list - a list of the eventpoints that caused this stop. KIND is
3577 the target_waitkind for the stopping event. This
e514a9d6
JM
3578 routine calls the generic print routine for printing a message
3579 about reasons for stopping. This will print (for example) the
3580 "Breakpoint n," part of the output. The return value of this
3581 routine is one of:
c906108c 3582
4a64f543 3583 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3584 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3585 code to print the location. An example is
c5aa993b
JM
3586 "Breakpoint 1, " which should be followed by
3587 the location.
917317f4 3588 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3589 to also print the location part of the message.
3590 An example is the catch/throw messages, which
4a64f543 3591 don't require a location appended to the end.
917317f4 3592 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3593 further info to be printed. */
c906108c 3594
917317f4 3595enum print_stop_action
36dfb11c 3596bpstat_print (bpstat bs, int kind)
c906108c
SS
3597{
3598 int val;
c5aa993b 3599
c906108c 3600 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3601 (Currently all watchpoints go on the bpstat whether hit or not.
3602 That probably could (should) be changed, provided care is taken
c906108c 3603 with respect to bpstat_explains_signal). */
e514a9d6
JM
3604 for (; bs; bs = bs->next)
3605 {
3606 val = print_bp_stop_message (bs);
3607 if (val == PRINT_SRC_ONLY
3608 || val == PRINT_SRC_AND_LOC
3609 || val == PRINT_NOTHING)
3610 return val;
3611 }
c906108c 3612
36dfb11c
TT
3613 /* If we had hit a shared library event breakpoint,
3614 print_bp_stop_message would print out this message. If we hit an
3615 OS-level shared library event, do the same thing. */
3616 if (kind == TARGET_WAITKIND_LOADED)
3617 {
edcc5120 3618 print_solib_event (0);
36dfb11c
TT
3619 return PRINT_NOTHING;
3620 }
3621
e514a9d6 3622 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3623 with and nothing was printed. */
917317f4 3624 return PRINT_UNKNOWN;
c906108c
SS
3625}
3626
4a64f543
MS
3627/* Evaluate the expression EXP and return 1 if value is zero. This is
3628 used inside a catch_errors to evaluate the breakpoint condition.
3629 The argument is a "struct expression *" that has been cast to a
3630 "char *" to make it pass through catch_errors. */
c906108c
SS
3631
3632static int
4efb68b1 3633breakpoint_cond_eval (void *exp)
c906108c 3634{
278cd55f 3635 struct value *mark = value_mark ();
c5aa993b 3636 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3637
c906108c
SS
3638 value_free_to_mark (mark);
3639 return i;
3640}
3641
5760d0ab 3642/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3643
3644static bpstat
5760d0ab 3645bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3646{
3647 bpstat bs;
3648
3649 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3650 bs->next = NULL;
3651 **bs_link_pointer = bs;
3652 *bs_link_pointer = &bs->next;
f431efe5
PA
3653 bs->breakpoint_at = bl->owner;
3654 bs->bp_location_at = bl;
3655 incref_bp_location (bl);
c906108c
SS
3656 /* If the condition is false, etc., don't do the commands. */
3657 bs->commands = NULL;
3658 bs->old_val = NULL;
3659 bs->print_it = print_it_normal;
3660 return bs;
3661}
3662\f
d983da9c
DJ
3663/* The target has stopped with waitstatus WS. Check if any hardware
3664 watchpoints have triggered, according to the target. */
3665
3666int
3667watchpoints_triggered (struct target_waitstatus *ws)
3668{
d92524f1 3669 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3670 CORE_ADDR addr;
3671 struct breakpoint *b;
3672
3673 if (!stopped_by_watchpoint)
3674 {
3675 /* We were not stopped by a watchpoint. Mark all watchpoints
3676 as not triggered. */
3677 ALL_BREAKPOINTS (b)
cc60f2e3 3678 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3679 {
3680 struct watchpoint *w = (struct watchpoint *) b;
3681
3682 w->watchpoint_triggered = watch_triggered_no;
3683 }
d983da9c
DJ
3684
3685 return 0;
3686 }
3687
3688 if (!target_stopped_data_address (&current_target, &addr))
3689 {
3690 /* We were stopped by a watchpoint, but we don't know where.
3691 Mark all watchpoints as unknown. */
3692 ALL_BREAKPOINTS (b)
cc60f2e3 3693 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3694 {
3695 struct watchpoint *w = (struct watchpoint *) b;
3696
3697 w->watchpoint_triggered = watch_triggered_unknown;
3698 }
d983da9c
DJ
3699
3700 return stopped_by_watchpoint;
3701 }
3702
3703 /* The target could report the data address. Mark watchpoints
3704 affected by this data address as triggered, and all others as not
3705 triggered. */
3706
3707 ALL_BREAKPOINTS (b)
cc60f2e3 3708 if (is_hardware_watchpoint (b))
d983da9c 3709 {
3a5c3e22 3710 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 3711 struct bp_location *loc;
d983da9c 3712
3a5c3e22 3713 w->watchpoint_triggered = watch_triggered_no;
a5606eee 3714 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 3715 {
3a5c3e22 3716 if (is_masked_watchpoint (b))
9c06b0b4 3717 {
3a5c3e22
PA
3718 CORE_ADDR newaddr = addr & w->hw_wp_mask;
3719 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
3720
3721 if (newaddr == start)
3722 {
3a5c3e22 3723 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3724 break;
3725 }
3726 }
3727 /* Exact match not required. Within range is sufficient. */
3728 else if (target_watchpoint_addr_within_range (&current_target,
3729 addr, loc->address,
3730 loc->length))
3731 {
3a5c3e22 3732 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3733 break;
3734 }
3735 }
d983da9c
DJ
3736 }
3737
3738 return 1;
3739}
3740
c906108c
SS
3741/* Possible return values for watchpoint_check (this can't be an enum
3742 because of check_errors). */
3743/* The watchpoint has been deleted. */
3744#define WP_DELETED 1
3745/* The value has changed. */
3746#define WP_VALUE_CHANGED 2
3747/* The value has not changed. */
3748#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3749/* Ignore this watchpoint, no matter if the value changed or not. */
3750#define WP_IGNORE 4
c906108c
SS
3751
3752#define BP_TEMPFLAG 1
3753#define BP_HARDWAREFLAG 2
3754
4a64f543
MS
3755/* Evaluate watchpoint condition expression and check if its value
3756 changed.
553e4c11
JB
3757
3758 P should be a pointer to struct bpstat, but is defined as a void *
3759 in order for this function to be usable with catch_errors. */
c906108c
SS
3760
3761static int
4efb68b1 3762watchpoint_check (void *p)
c906108c
SS
3763{
3764 bpstat bs = (bpstat) p;
3a5c3e22 3765 struct watchpoint *b;
c906108c
SS
3766 struct frame_info *fr;
3767 int within_current_scope;
3768
f431efe5 3769 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3770 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 3771 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 3772
f6bc2008
PA
3773 /* If this is a local watchpoint, we only want to check if the
3774 watchpoint frame is in scope if the current thread is the thread
3775 that was used to create the watchpoint. */
3776 if (!watchpoint_in_thread_scope (b))
60e1c644 3777 return WP_IGNORE;
f6bc2008 3778
c906108c
SS
3779 if (b->exp_valid_block == NULL)
3780 within_current_scope = 1;
3781 else
3782 {
edb3359d
DJ
3783 struct frame_info *frame = get_current_frame ();
3784 struct gdbarch *frame_arch = get_frame_arch (frame);
3785 CORE_ADDR frame_pc = get_frame_pc (frame);
3786
4a64f543
MS
3787 /* in_function_epilogue_p() returns a non-zero value if we're
3788 still in the function but the stack frame has already been
3789 invalidated. Since we can't rely on the values of local
3790 variables after the stack has been destroyed, we are treating
3791 the watchpoint in that state as `not changed' without further
3792 checking. Don't mark watchpoints as changed if the current
3793 frame is in an epilogue - even if they are in some other
3794 frame, our view of the stack is likely to be wrong and
3795 frame_find_by_id could error out. */
a0f49112 3796 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3797 return WP_IGNORE;
a0f49112 3798
101dcfbe 3799 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3800 within_current_scope = (fr != NULL);
69fbadd5
DJ
3801
3802 /* If we've gotten confused in the unwinder, we might have
3803 returned a frame that can't describe this variable. */
edb3359d
DJ
3804 if (within_current_scope)
3805 {
3806 struct symbol *function;
3807
3808 function = get_frame_function (fr);
3809 if (function == NULL
3810 || !contained_in (b->exp_valid_block,
3811 SYMBOL_BLOCK_VALUE (function)))
3812 within_current_scope = 0;
3813 }
69fbadd5 3814
edb3359d 3815 if (within_current_scope)
c906108c
SS
3816 /* If we end up stopping, the current frame will get selected
3817 in normal_stop. So this call to select_frame won't affect
3818 the user. */
0f7d239c 3819 select_frame (fr);
c906108c 3820 }
c5aa993b 3821
c906108c
SS
3822 if (within_current_scope)
3823 {
4a64f543
MS
3824 /* We use value_{,free_to_}mark because it could be a *long*
3825 time before we return to the command level and call
3826 free_all_values. We can't call free_all_values because we
3827 might be in the middle of evaluating a function call. */
c906108c 3828
0cf6dd15 3829 int pc = 0;
9c06b0b4 3830 struct value *mark;
fa4727a6
DJ
3831 struct value *new_val;
3832
3a5c3e22 3833 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
3834 /* Since we don't know the exact trigger address (from
3835 stopped_data_address), just tell the user we've triggered
3836 a mask watchpoint. */
3837 return WP_VALUE_CHANGED;
3838
3839 mark = value_mark ();
0cf6dd15 3840 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3841
4a64f543
MS
3842 /* We use value_equal_contents instead of value_equal because
3843 the latter coerces an array to a pointer, thus comparing just
3844 the address of the array instead of its contents. This is
3845 not what we want. */
fa4727a6 3846 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3847 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3848 {
fa4727a6
DJ
3849 if (new_val != NULL)
3850 {
3851 release_value (new_val);
3852 value_free_to_mark (mark);
3853 }
c906108c
SS
3854 bs->old_val = b->val;
3855 b->val = new_val;
fa4727a6 3856 b->val_valid = 1;
c906108c
SS
3857 return WP_VALUE_CHANGED;
3858 }
3859 else
3860 {
60e1c644 3861 /* Nothing changed. */
c906108c 3862 value_free_to_mark (mark);
c906108c
SS
3863 return WP_VALUE_NOT_CHANGED;
3864 }
3865 }
3866 else
3867 {
79a45e25
PA
3868 struct ui_out *uiout = current_uiout;
3869
c906108c 3870 /* This seems like the only logical thing to do because
c5aa993b
JM
3871 if we temporarily ignored the watchpoint, then when
3872 we reenter the block in which it is valid it contains
3873 garbage (in the case of a function, it may have two
3874 garbage values, one before and one after the prologue).
3875 So we can't even detect the first assignment to it and
3876 watch after that (since the garbage may or may not equal
3877 the first value assigned). */
348d480f
PA
3878 /* We print all the stop information in
3879 breakpoint_ops->print_it, but in this case, by the time we
3880 call breakpoint_ops->print_it this bp will be deleted
3881 already. So we have no choice but print the information
3882 here. */
9dc5e2a9 3883 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3884 ui_out_field_string
3885 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3886 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 3887 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
3888 ui_out_text (uiout,
3889 " deleted because the program has left the block in\n\
8b93c638 3890which its expression is valid.\n");
4ce44c66 3891
cdac0397 3892 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 3893 decref_counted_command_line (&b->base.commands);
d0fb5eae 3894 watchpoint_del_at_next_stop (b);
c906108c
SS
3895
3896 return WP_DELETED;
3897 }
3898}
3899
18a18393 3900/* Return true if it looks like target has stopped due to hitting
348d480f
PA
3901 breakpoint location BL. This function does not check if we should
3902 stop, only if BL explains the stop. */
3903
18a18393 3904static int
6c95b8df 3905bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
3906 struct address_space *aspace, CORE_ADDR bp_addr,
3907 const struct target_waitstatus *ws)
18a18393
VP
3908{
3909 struct breakpoint *b = bl->owner;
3910
348d480f 3911 /* BL is from an existing breakpoint. */
2bdf28a0
JK
3912 gdb_assert (b != NULL);
3913
09ac7c10 3914 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
3915}
3916
3a5c3e22
PA
3917/* Determine if the watched values have actually changed, and we
3918 should stop. If not, set BS->stop to 0. */
3919
18a18393
VP
3920static void
3921bpstat_check_watchpoint (bpstat bs)
3922{
2bdf28a0 3923 const struct bp_location *bl;
3a5c3e22 3924 struct watchpoint *b;
2bdf28a0
JK
3925
3926 /* BS is built for existing struct breakpoint. */
f431efe5 3927 bl = bs->bp_location_at;
2bdf28a0 3928 gdb_assert (bl != NULL);
3a5c3e22 3929 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 3930 gdb_assert (b != NULL);
18a18393 3931
18a18393 3932 {
18a18393
VP
3933 int must_check_value = 0;
3934
3a5c3e22 3935 if (b->base.type == bp_watchpoint)
18a18393
VP
3936 /* For a software watchpoint, we must always check the
3937 watched value. */
3938 must_check_value = 1;
3939 else if (b->watchpoint_triggered == watch_triggered_yes)
3940 /* We have a hardware watchpoint (read, write, or access)
3941 and the target earlier reported an address watched by
3942 this watchpoint. */
3943 must_check_value = 1;
3944 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 3945 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
3946 /* We were stopped by a hardware watchpoint, but the target could
3947 not report the data address. We must check the watchpoint's
3948 value. Access and read watchpoints are out of luck; without
3949 a data address, we can't figure it out. */
3950 must_check_value = 1;
3a5c3e22 3951
18a18393
VP
3952 if (must_check_value)
3953 {
3e43a32a
MS
3954 char *message
3955 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 3956 b->base.number);
18a18393
VP
3957 struct cleanup *cleanups = make_cleanup (xfree, message);
3958 int e = catch_errors (watchpoint_check, bs, message,
3959 RETURN_MASK_ALL);
3960 do_cleanups (cleanups);
3961 switch (e)
3962 {
3963 case WP_DELETED:
3964 /* We've already printed what needs to be printed. */
3965 bs->print_it = print_it_done;
3966 /* Stop. */
3967 break;
60e1c644
PA
3968 case WP_IGNORE:
3969 bs->print_it = print_it_noop;
3970 bs->stop = 0;
3971 break;
18a18393 3972 case WP_VALUE_CHANGED:
3a5c3e22 3973 if (b->base.type == bp_read_watchpoint)
18a18393 3974 {
85d721b8
PA
3975 /* There are two cases to consider here:
3976
4a64f543 3977 1. We're watching the triggered memory for reads.
85d721b8
PA
3978 In that case, trust the target, and always report
3979 the watchpoint hit to the user. Even though
3980 reads don't cause value changes, the value may
3981 have changed since the last time it was read, and
3982 since we're not trapping writes, we will not see
3983 those, and as such we should ignore our notion of
3984 old value.
3985
4a64f543 3986 2. We're watching the triggered memory for both
85d721b8
PA
3987 reads and writes. There are two ways this may
3988 happen:
3989
4a64f543 3990 2.1. This is a target that can't break on data
85d721b8
PA
3991 reads only, but can break on accesses (reads or
3992 writes), such as e.g., x86. We detect this case
3993 at the time we try to insert read watchpoints.
3994
4a64f543 3995 2.2. Otherwise, the target supports read
85d721b8
PA
3996 watchpoints, but, the user set an access or write
3997 watchpoint watching the same memory as this read
3998 watchpoint.
3999
4000 If we're watching memory writes as well as reads,
4001 ignore watchpoint hits when we find that the
4002 value hasn't changed, as reads don't cause
4003 changes. This still gives false positives when
4004 the program writes the same value to memory as
4005 what there was already in memory (we will confuse
4006 it for a read), but it's much better than
4007 nothing. */
4008
4009 int other_write_watchpoint = 0;
4010
4011 if (bl->watchpoint_type == hw_read)
4012 {
4013 struct breakpoint *other_b;
4014
4015 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
4016 if (other_b->type == bp_hardware_watchpoint
4017 || other_b->type == bp_access_watchpoint)
85d721b8 4018 {
3a5c3e22
PA
4019 struct watchpoint *other_w =
4020 (struct watchpoint *) other_b;
4021
4022 if (other_w->watchpoint_triggered
4023 == watch_triggered_yes)
4024 {
4025 other_write_watchpoint = 1;
4026 break;
4027 }
85d721b8
PA
4028 }
4029 }
4030
4031 if (other_write_watchpoint
4032 || bl->watchpoint_type == hw_access)
4033 {
4034 /* We're watching the same memory for writes,
4035 and the value changed since the last time we
4036 updated it, so this trap must be for a write.
4037 Ignore it. */
4038 bs->print_it = print_it_noop;
4039 bs->stop = 0;
4040 }
18a18393
VP
4041 }
4042 break;
4043 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
4044 if (b->base.type == bp_hardware_watchpoint
4045 || b->base.type == bp_watchpoint)
18a18393
VP
4046 {
4047 /* Don't stop: write watchpoints shouldn't fire if
4048 the value hasn't changed. */
4049 bs->print_it = print_it_noop;
4050 bs->stop = 0;
4051 }
4052 /* Stop. */
4053 break;
4054 default:
4055 /* Can't happen. */
4056 case 0:
4057 /* Error from catch_errors. */
3a5c3e22 4058 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 4059 watchpoint_del_at_next_stop (b);
18a18393
VP
4060 /* We've already printed what needs to be printed. */
4061 bs->print_it = print_it_done;
4062 break;
4063 }
4064 }
4065 else /* must_check_value == 0 */
4066 {
4067 /* This is a case where some watchpoint(s) triggered, but
4068 not at the address of this watchpoint, or else no
4069 watchpoint triggered after all. So don't print
4070 anything for this watchpoint. */
4071 bs->print_it = print_it_noop;
4072 bs->stop = 0;
4073 }
4074 }
4075}
4076
4077
4078/* Check conditions (condition proper, frame, thread and ignore count)
4079 of breakpoint referred to by BS. If we should not stop for this
4080 breakpoint, set BS->stop to 0. */
f431efe5 4081
18a18393
VP
4082static void
4083bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4084{
4085 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
4086 const struct bp_location *bl;
4087 struct breakpoint *b;
4088
4089 /* BS is built for existing struct breakpoint. */
f431efe5 4090 bl = bs->bp_location_at;
2bdf28a0 4091 gdb_assert (bl != NULL);
f431efe5 4092 b = bs->breakpoint_at;
2bdf28a0 4093 gdb_assert (b != NULL);
18a18393
VP
4094
4095 if (frame_id_p (b->frame_id)
edb3359d 4096 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4097 bs->stop = 0;
4098 else if (bs->stop)
4099 {
4100 int value_is_zero = 0;
60e1c644
PA
4101 struct expression *cond;
4102
7371cf6d
PM
4103 /* Evaluate Python breakpoints that have a "stop"
4104 method implemented. */
4105 if (b->py_bp_object)
4106 bs->stop = gdbpy_should_stop (b->py_bp_object);
4107
60e1c644 4108 if (is_watchpoint (b))
3a5c3e22
PA
4109 {
4110 struct watchpoint *w = (struct watchpoint *) b;
4111
4112 cond = w->cond_exp;
4113 }
60e1c644
PA
4114 else
4115 cond = bl->cond;
4116
f431efe5 4117 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4118 {
60e1c644 4119 int within_current_scope = 1;
3a5c3e22 4120 struct watchpoint * w;
60e1c644 4121
c5bc3a77
DJ
4122 /* We use value_mark and value_free_to_mark because it could
4123 be a long time before we return to the command level and
4124 call free_all_values. We can't call free_all_values
4125 because we might be in the middle of evaluating a
4126 function call. */
4127 struct value *mark = value_mark ();
4128
3a5c3e22
PA
4129 if (is_watchpoint (b))
4130 w = (struct watchpoint *) b;
4131 else
4132 w = NULL;
4133
edb3359d
DJ
4134 /* Need to select the frame, with all that implies so that
4135 the conditions will have the right context. Because we
4136 use the frame, we will not see an inlined function's
4137 variables when we arrive at a breakpoint at the start
4138 of the inlined function; the current frame will be the
4139 call site. */
3a5c3e22 4140 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
4141 select_frame (get_current_frame ());
4142 else
4143 {
4144 struct frame_info *frame;
4145
4146 /* For local watchpoint expressions, which particular
4147 instance of a local is being watched matters, so we
4148 keep track of the frame to evaluate the expression
4149 in. To evaluate the condition however, it doesn't
4150 really matter which instantiation of the function
4151 where the condition makes sense triggers the
4152 watchpoint. This allows an expression like "watch
4153 global if q > 10" set in `func', catch writes to
4154 global on all threads that call `func', or catch
4155 writes on all recursive calls of `func' by a single
4156 thread. We simply always evaluate the condition in
4157 the innermost frame that's executing where it makes
4158 sense to evaluate the condition. It seems
4159 intuitive. */
3a5c3e22 4160 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
4161 if (frame != NULL)
4162 select_frame (frame);
4163 else
4164 within_current_scope = 0;
4165 }
4166 if (within_current_scope)
4167 value_is_zero
4168 = catch_errors (breakpoint_cond_eval, cond,
4169 "Error in testing breakpoint condition:\n",
4170 RETURN_MASK_ALL);
4171 else
4172 {
4173 warning (_("Watchpoint condition cannot be tested "
4174 "in the current scope"));
4175 /* If we failed to set the right context for this
4176 watchpoint, unconditionally report it. */
4177 value_is_zero = 0;
4178 }
4a64f543 4179 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4180 value_free_to_mark (mark);
18a18393 4181 }
60e1c644
PA
4182
4183 if (cond && value_is_zero)
18a18393
VP
4184 {
4185 bs->stop = 0;
4186 }
4187 else if (b->thread != -1 && b->thread != thread_id)
4188 {
4189 bs->stop = 0;
4190 }
4191 else if (b->ignore_count > 0)
4192 {
4193 b->ignore_count--;
4194 annotate_ignore_count_change ();
4195 bs->stop = 0;
4a64f543 4196 /* Increase the hit count even though we don't stop. */
18a18393 4197 ++(b->hit_count);
8d3788bd 4198 observer_notify_breakpoint_modified (b);
18a18393
VP
4199 }
4200 }
4201}
4202
4203
9709f61c 4204/* Get a bpstat associated with having just stopped at address
d983da9c 4205 BP_ADDR in thread PTID.
c906108c 4206
d983da9c 4207 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4208 don't understand this stop. Result is a chain of bpstat's such
4209 that:
c906108c 4210
c5aa993b 4211 if we don't understand the stop, the result is a null pointer.
c906108c 4212
c5aa993b 4213 if we understand why we stopped, the result is not null.
c906108c 4214
c5aa993b
JM
4215 Each element of the chain refers to a particular breakpoint or
4216 watchpoint at which we have stopped. (We may have stopped for
4217 several reasons concurrently.)
c906108c 4218
c5aa993b
JM
4219 Each element of the chain has valid next, breakpoint_at,
4220 commands, FIXME??? fields. */
c906108c
SS
4221
4222bpstat
6c95b8df 4223bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
4224 CORE_ADDR bp_addr, ptid_t ptid,
4225 const struct target_waitstatus *ws)
c906108c 4226{
0d381245 4227 struct breakpoint *b = NULL;
afe38095 4228 struct bp_location *bl;
20874c92 4229 struct bp_location *loc;
5760d0ab
JK
4230 /* First item of allocated bpstat's. */
4231 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4232 /* Pointer to the last thing in the chain currently. */
5760d0ab 4233 bpstat bs;
20874c92 4234 int ix;
429374b8 4235 int need_remove_insert;
f431efe5 4236 int removed_any;
c906108c 4237
f431efe5
PA
4238 /* First, build the bpstat chain with locations that explain a
4239 target stop, while being careful to not set the target running,
4240 as that may invalidate locations (in particular watchpoint
4241 locations are recreated). Resuming will happen here with
4242 breakpoint conditions or watchpoint expressions that include
4243 inferior function calls. */
c5aa993b 4244
429374b8
JK
4245 ALL_BREAKPOINTS (b)
4246 {
4247 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4248 continue;
a5606eee 4249
429374b8
JK
4250 for (bl = b->loc; bl != NULL; bl = bl->next)
4251 {
4a64f543
MS
4252 /* For hardware watchpoints, we look only at the first
4253 location. The watchpoint_check function will work on the
4254 entire expression, not the individual locations. For
4255 read watchpoints, the watchpoints_triggered function has
4256 checked all locations already. */
429374b8
JK
4257 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4258 break;
18a18393 4259
429374b8
JK
4260 if (bl->shlib_disabled)
4261 continue;
c5aa993b 4262
09ac7c10 4263 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 4264 continue;
c5aa993b 4265
4a64f543
MS
4266 /* Come here if it's a watchpoint, or if the break address
4267 matches. */
c5aa993b 4268
4a64f543
MS
4269 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4270 explain stop. */
c5aa993b 4271
f431efe5
PA
4272 /* Assume we stop. Should we find a watchpoint that is not
4273 actually triggered, or if the condition of the breakpoint
4274 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4275 bs->stop = 1;
4276 bs->print = 1;
d983da9c 4277
f431efe5
PA
4278 /* If this is a scope breakpoint, mark the associated
4279 watchpoint as triggered so that we will handle the
4280 out-of-scope event. We'll get to the watchpoint next
4281 iteration. */
d0fb5eae 4282 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4283 {
4284 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4285
4286 w->watchpoint_triggered = watch_triggered_yes;
4287 }
f431efe5
PA
4288 }
4289 }
4290
4291 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4292 {
f1310107 4293 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4294 {
5760d0ab 4295 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4296 /* For hits of moribund locations, we should just proceed. */
4297 bs->stop = 0;
4298 bs->print = 0;
4299 bs->print_it = print_it_noop;
4300 }
4301 }
4302
edcc5120
TT
4303 /* A bit of special processing for shlib breakpoints. We need to
4304 process solib loading here, so that the lists of loaded and
4305 unloaded libraries are correct before we handle "catch load" and
4306 "catch unload". */
4307 for (bs = bs_head; bs != NULL; bs = bs->next)
4308 {
5d268276 4309 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
4310 {
4311 handle_solib_event ();
4312 break;
4313 }
4314 }
4315
f431efe5
PA
4316 /* Now go through the locations that caused the target to stop, and
4317 check whether we're interested in reporting this stop to higher
4318 layers, or whether we should resume the target transparently. */
4319
4320 removed_any = 0;
4321
5760d0ab 4322 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4323 {
4324 if (!bs->stop)
4325 continue;
4326
f431efe5 4327 b = bs->breakpoint_at;
348d480f
PA
4328 b->ops->check_status (bs);
4329 if (bs->stop)
28010a5d 4330 {
348d480f 4331 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4332
429374b8
JK
4333 if (bs->stop)
4334 {
4335 ++(b->hit_count);
8d3788bd 4336 observer_notify_breakpoint_modified (b);
c906108c 4337
4a64f543 4338 /* We will stop here. */
429374b8
JK
4339 if (b->disposition == disp_disable)
4340 {
816338b5
SS
4341 --(b->enable_count);
4342 if (b->enable_count <= 0
4343 && b->enable_state != bp_permanent)
429374b8 4344 b->enable_state = bp_disabled;
f431efe5 4345 removed_any = 1;
429374b8
JK
4346 }
4347 if (b->silent)
4348 bs->print = 0;
4349 bs->commands = b->commands;
9add0f1b 4350 incref_counted_command_line (bs->commands);
abf85f46
JK
4351 if (command_line_is_silent (bs->commands
4352 ? bs->commands->commands : NULL))
4353 bs->print = 0;
429374b8
JK
4354 }
4355
348d480f 4356 }
a9b3a50f
PA
4357
4358 /* Print nothing for this entry if we don't stop or don't
4359 print. */
4360 if (!bs->stop || !bs->print)
4361 bs->print_it = print_it_noop;
429374b8 4362 }
876fa593 4363
d983da9c
DJ
4364 /* If we aren't stopping, the value of some hardware watchpoint may
4365 not have changed, but the intermediate memory locations we are
4366 watching may have. Don't bother if we're stopping; this will get
4367 done later. */
d832cb68 4368 need_remove_insert = 0;
5760d0ab
JK
4369 if (! bpstat_causes_stop (bs_head))
4370 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4371 if (!bs->stop
f431efe5
PA
4372 && bs->breakpoint_at
4373 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4374 {
3a5c3e22
PA
4375 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4376
4377 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 4378 need_remove_insert = 1;
d983da9c
DJ
4379 }
4380
d832cb68 4381 if (need_remove_insert)
2d134ed3 4382 update_global_location_list (1);
f431efe5
PA
4383 else if (removed_any)
4384 update_global_location_list (0);
d832cb68 4385
5760d0ab 4386 return bs_head;
c906108c 4387}
628fe4e4
JK
4388
4389static void
4390handle_jit_event (void)
4391{
4392 struct frame_info *frame;
4393 struct gdbarch *gdbarch;
4394
4395 /* Switch terminal for any messages produced by
4396 breakpoint_re_set. */
4397 target_terminal_ours_for_output ();
4398
4399 frame = get_current_frame ();
4400 gdbarch = get_frame_arch (frame);
4401
4402 jit_event_handler (gdbarch);
4403
4404 target_terminal_inferior ();
4405}
4406
edcc5120
TT
4407/* Handle an solib event by calling solib_add. */
4408
4409void
4410handle_solib_event (void)
4411{
4412 clear_program_space_solib_cache (current_inferior ()->pspace);
4413
4414 /* Check for any newly added shared libraries if we're supposed to
4415 be adding them automatically. Switch terminal for any messages
4416 produced by breakpoint_re_set. */
4417 target_terminal_ours_for_output ();
4418#ifdef SOLIB_ADD
4419 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4420#else
4421 solib_add (NULL, 0, &current_target, auto_solib_add);
4422#endif
4423 target_terminal_inferior ();
4424}
4425
628fe4e4
JK
4426/* Prepare WHAT final decision for infrun. */
4427
4428/* Decide what infrun needs to do with this bpstat. */
4429
c906108c 4430struct bpstat_what
0e30163f 4431bpstat_what (bpstat bs_head)
c906108c 4432{
c906108c 4433 struct bpstat_what retval;
628fe4e4 4434 int jit_event = 0;
0e30163f 4435 bpstat bs;
c906108c 4436
628fe4e4 4437 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4438 retval.call_dummy = STOP_NONE;
186c406b 4439 retval.is_longjmp = 0;
628fe4e4 4440
0e30163f 4441 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4442 {
628fe4e4
JK
4443 /* Extract this BS's action. After processing each BS, we check
4444 if its action overrides all we've seem so far. */
4445 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4446 enum bptype bptype;
4447
c906108c 4448 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4449 {
4450 /* I suspect this can happen if it was a momentary
4451 breakpoint which has since been deleted. */
4452 bptype = bp_none;
4453 }
20874c92 4454 else
f431efe5 4455 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4456
4457 switch (bptype)
c906108c
SS
4458 {
4459 case bp_none:
628fe4e4 4460 break;
c906108c
SS
4461 case bp_breakpoint:
4462 case bp_hardware_breakpoint:
4463 case bp_until:
4464 case bp_finish:
a9b3a50f 4465 case bp_shlib_event:
c906108c
SS
4466 if (bs->stop)
4467 {
4468 if (bs->print)
628fe4e4 4469 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4470 else
628fe4e4 4471 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4472 }
4473 else
628fe4e4 4474 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4475 break;
4476 case bp_watchpoint:
4477 case bp_hardware_watchpoint:
4478 case bp_read_watchpoint:
4479 case bp_access_watchpoint:
4480 if (bs->stop)
4481 {
4482 if (bs->print)
628fe4e4 4483 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4484 else
628fe4e4 4485 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4486 }
4487 else
628fe4e4
JK
4488 {
4489 /* There was a watchpoint, but we're not stopping.
4490 This requires no further action. */
4491 }
c906108c
SS
4492 break;
4493 case bp_longjmp:
186c406b 4494 case bp_exception:
628fe4e4 4495 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4496 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4497 break;
4498 case bp_longjmp_resume:
186c406b 4499 case bp_exception_resume:
628fe4e4 4500 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4501 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4502 break;
4503 case bp_step_resume:
4504 if (bs->stop)
628fe4e4
JK
4505 this_action = BPSTAT_WHAT_STEP_RESUME;
4506 else
c906108c 4507 {
628fe4e4
JK
4508 /* It is for the wrong frame. */
4509 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4510 }
c906108c 4511 break;
2c03e5be
PA
4512 case bp_hp_step_resume:
4513 if (bs->stop)
4514 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4515 else
4516 {
4517 /* It is for the wrong frame. */
4518 this_action = BPSTAT_WHAT_SINGLE;
4519 }
4520 break;
c906108c 4521 case bp_watchpoint_scope:
c4093a6a 4522 case bp_thread_event:
1900040c 4523 case bp_overlay_event:
0fd8e87f 4524 case bp_longjmp_master:
aa7d318d 4525 case bp_std_terminate_master:
186c406b 4526 case bp_exception_master:
628fe4e4 4527 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4528 break;
ce78b96d 4529 case bp_catchpoint:
c5aa993b
JM
4530 if (bs->stop)
4531 {
4532 if (bs->print)
628fe4e4 4533 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4534 else
628fe4e4 4535 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4536 }
4537 else
628fe4e4
JK
4538 {
4539 /* There was a catchpoint, but we're not stopping.
4540 This requires no further action. */
4541 }
4542 break;
628fe4e4
JK
4543 case bp_jit_event:
4544 jit_event = 1;
4545 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4546 break;
c906108c 4547 case bp_call_dummy:
53a5351d
JM
4548 /* Make sure the action is stop (silent or noisy),
4549 so infrun.c pops the dummy frame. */
aa7d318d 4550 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4551 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4552 break;
4553 case bp_std_terminate:
4554 /* Make sure the action is stop (silent or noisy),
4555 so infrun.c pops the dummy frame. */
aa7d318d 4556 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4557 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4558 break;
1042e4c0 4559 case bp_tracepoint:
7a697b8d 4560 case bp_fast_tracepoint:
0fb4aa4b 4561 case bp_static_tracepoint:
1042e4c0
SS
4562 /* Tracepoint hits should not be reported back to GDB, and
4563 if one got through somehow, it should have been filtered
4564 out already. */
4565 internal_error (__FILE__, __LINE__,
7a697b8d 4566 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4567 break;
4568 case bp_gnu_ifunc_resolver:
4569 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4570 this_action = BPSTAT_WHAT_SINGLE;
4571 break;
4572 case bp_gnu_ifunc_resolver_return:
4573 /* The breakpoint will be removed, execution will restart from the
4574 PC of the former breakpoint. */
4575 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4576 break;
628fe4e4
JK
4577 default:
4578 internal_error (__FILE__, __LINE__,
4579 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4580 }
628fe4e4
JK
4581
4582 retval.main_action = max (retval.main_action, this_action);
c906108c 4583 }
628fe4e4 4584
0e30163f
JK
4585 /* These operations may affect the bs->breakpoint_at state so they are
4586 delayed after MAIN_ACTION is decided above. */
4587
628fe4e4
JK
4588 if (jit_event)
4589 {
4590 if (debug_infrun)
4591 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4592
4593 handle_jit_event ();
4594 }
4595
0e30163f
JK
4596 for (bs = bs_head; bs != NULL; bs = bs->next)
4597 {
4598 struct breakpoint *b = bs->breakpoint_at;
4599
4600 if (b == NULL)
4601 continue;
4602 switch (b->type)
4603 {
4604 case bp_gnu_ifunc_resolver:
4605 gnu_ifunc_resolver_stop (b);
4606 break;
4607 case bp_gnu_ifunc_resolver_return:
4608 gnu_ifunc_resolver_return_stop (b);
4609 break;
4610 }
4611 }
4612
c906108c
SS
4613 return retval;
4614}
4615
4616/* Nonzero if we should step constantly (e.g. watchpoints on machines
4617 without hardware support). This isn't related to a specific bpstat,
4618 just to things like whether watchpoints are set. */
4619
c5aa993b 4620int
fba45db2 4621bpstat_should_step (void)
c906108c
SS
4622{
4623 struct breakpoint *b;
cc59ec59 4624
c906108c 4625 ALL_BREAKPOINTS (b)
717a8278 4626 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4627 return 1;
c906108c
SS
4628 return 0;
4629}
4630
67822962
PA
4631int
4632bpstat_causes_stop (bpstat bs)
4633{
4634 for (; bs != NULL; bs = bs->next)
4635 if (bs->stop)
4636 return 1;
4637
4638 return 0;
4639}
4640
c906108c 4641\f
c5aa993b 4642
170b53b2
UW
4643/* Compute a string of spaces suitable to indent the next line
4644 so it starts at the position corresponding to the table column
4645 named COL_NAME in the currently active table of UIOUT. */
4646
4647static char *
4648wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4649{
4650 static char wrap_indent[80];
4651 int i, total_width, width, align;
4652 char *text;
4653
4654 total_width = 0;
4655 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4656 {
4657 if (strcmp (text, col_name) == 0)
4658 {
4659 gdb_assert (total_width < sizeof wrap_indent);
4660 memset (wrap_indent, ' ', total_width);
4661 wrap_indent[total_width] = 0;
4662
4663 return wrap_indent;
4664 }
4665
4666 total_width += width + 1;
4667 }
4668
4669 return NULL;
4670}
4671
859825b8
JK
4672/* Print the LOC location out of the list of B->LOC locations. */
4673
170b53b2
UW
4674static void
4675print_breakpoint_location (struct breakpoint *b,
4676 struct bp_location *loc)
0d381245 4677{
79a45e25 4678 struct ui_out *uiout = current_uiout;
6c95b8df
PA
4679 struct cleanup *old_chain = save_current_program_space ();
4680
859825b8
JK
4681 if (loc != NULL && loc->shlib_disabled)
4682 loc = NULL;
4683
6c95b8df
PA
4684 if (loc != NULL)
4685 set_current_program_space (loc->pspace);
4686
56435ebe
TT
4687 if (b->display_canonical)
4688 ui_out_field_string (uiout, "what", b->addr_string);
f8eba3c6 4689 else if (loc && loc->source_file)
0d381245
VP
4690 {
4691 struct symbol *sym
4692 = find_pc_sect_function (loc->address, loc->section);
4693 if (sym)
4694 {
4695 ui_out_text (uiout, "in ");
4696 ui_out_field_string (uiout, "func",
4697 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4698 ui_out_text (uiout, " ");
4699 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4700 ui_out_text (uiout, "at ");
0d381245 4701 }
f8eba3c6 4702 ui_out_field_string (uiout, "file", loc->source_file);
0d381245
VP
4703 ui_out_text (uiout, ":");
4704
4705 if (ui_out_is_mi_like_p (uiout))
4706 {
4707 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4708 char *fullname = symtab_to_fullname (sal.symtab);
4709
4710 if (fullname)
4711 ui_out_field_string (uiout, "fullname", fullname);
4712 }
4713
f8eba3c6 4714 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 4715 }
859825b8 4716 else if (loc)
0d381245 4717 {
170b53b2
UW
4718 struct ui_stream *stb = ui_out_stream_new (uiout);
4719 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4720
22e722e1
DJ
4721 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4722 demangle, "");
0d381245 4723 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4724
4725 do_cleanups (stb_chain);
0d381245 4726 }
859825b8
JK
4727 else
4728 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4729
4730 do_cleanups (old_chain);
0d381245
VP
4731}
4732
269b11a2
PA
4733static const char *
4734bptype_string (enum bptype type)
c906108c 4735{
c4093a6a
JM
4736 struct ep_type_description
4737 {
4738 enum bptype type;
4739 char *description;
4740 };
4741 static struct ep_type_description bptypes[] =
c906108c 4742 {
c5aa993b
JM
4743 {bp_none, "?deleted?"},
4744 {bp_breakpoint, "breakpoint"},
c906108c 4745 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4746 {bp_until, "until"},
4747 {bp_finish, "finish"},
4748 {bp_watchpoint, "watchpoint"},
c906108c 4749 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4750 {bp_read_watchpoint, "read watchpoint"},
4751 {bp_access_watchpoint, "acc watchpoint"},
4752 {bp_longjmp, "longjmp"},
4753 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4754 {bp_exception, "exception"},
4755 {bp_exception_resume, "exception resume"},
c5aa993b 4756 {bp_step_resume, "step resume"},
2c03e5be 4757 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
4758 {bp_watchpoint_scope, "watchpoint scope"},
4759 {bp_call_dummy, "call dummy"},
aa7d318d 4760 {bp_std_terminate, "std::terminate"},
c5aa993b 4761 {bp_shlib_event, "shlib events"},
c4093a6a 4762 {bp_thread_event, "thread events"},
1900040c 4763 {bp_overlay_event, "overlay events"},
0fd8e87f 4764 {bp_longjmp_master, "longjmp master"},
aa7d318d 4765 {bp_std_terminate_master, "std::terminate master"},
186c406b 4766 {bp_exception_master, "exception master"},
ce78b96d 4767 {bp_catchpoint, "catchpoint"},
1042e4c0 4768 {bp_tracepoint, "tracepoint"},
7a697b8d 4769 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4770 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4771 {bp_jit_event, "jit events"},
0e30163f
JK
4772 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4773 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4774 };
269b11a2
PA
4775
4776 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4777 || ((int) type != bptypes[(int) type].type))
4778 internal_error (__FILE__, __LINE__,
4779 _("bptypes table does not describe type #%d."),
4780 (int) type);
4781
4782 return bptypes[(int) type].description;
4783}
4784
4785/* Print B to gdb_stdout. */
4786
4787static void
4788print_one_breakpoint_location (struct breakpoint *b,
4789 struct bp_location *loc,
4790 int loc_number,
4791 struct bp_location **last_loc,
269b11a2
PA
4792 int allflag)
4793{
4794 struct command_line *l;
c2c6d25f 4795 static char bpenables[] = "nynny";
c906108c 4796
79a45e25 4797 struct ui_out *uiout = current_uiout;
0d381245
VP
4798 int header_of_multiple = 0;
4799 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4800 struct value_print_options opts;
4801
4802 get_user_print_options (&opts);
0d381245
VP
4803
4804 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4805 /* See comment in print_one_breakpoint concerning treatment of
4806 breakpoints with single disabled location. */
0d381245
VP
4807 if (loc == NULL
4808 && (b->loc != NULL
4809 && (b->loc->next != NULL || !b->loc->enabled)))
4810 header_of_multiple = 1;
4811 if (loc == NULL)
4812 loc = b->loc;
4813
c4093a6a
JM
4814 annotate_record ();
4815
4816 /* 1 */
4817 annotate_field (0);
0d381245
VP
4818 if (part_of_multiple)
4819 {
4820 char *formatted;
0c6773c1 4821 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4822 ui_out_field_string (uiout, "number", formatted);
4823 xfree (formatted);
4824 }
4825 else
4826 {
4827 ui_out_field_int (uiout, "number", b->number);
4828 }
c4093a6a
JM
4829
4830 /* 2 */
4831 annotate_field (1);
0d381245
VP
4832 if (part_of_multiple)
4833 ui_out_field_skip (uiout, "type");
269b11a2
PA
4834 else
4835 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4836
4837 /* 3 */
4838 annotate_field (2);
0d381245
VP
4839 if (part_of_multiple)
4840 ui_out_field_skip (uiout, "disp");
4841 else
2cec12e5 4842 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4843
c4093a6a
JM
4844
4845 /* 4 */
4846 annotate_field (3);
0d381245 4847 if (part_of_multiple)
54e52265 4848 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4849 else
4a64f543
MS
4850 ui_out_field_fmt (uiout, "enabled", "%c",
4851 bpenables[(int) b->enable_state]);
54e52265 4852 ui_out_spaces (uiout, 2);
0d381245 4853
c4093a6a
JM
4854
4855 /* 5 and 6 */
3086aeae 4856 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4857 {
4a64f543
MS
4858 /* Although the print_one can possibly print all locations,
4859 calling it here is not likely to get any nice result. So,
4860 make sure there's just one location. */
0d381245 4861 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4862 b->ops->print_one (b, last_loc);
0d381245 4863 }
3086aeae
DJ
4864 else
4865 switch (b->type)
4866 {
4867 case bp_none:
4868 internal_error (__FILE__, __LINE__,
e2e0b3e5 4869 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4870 break;
c906108c 4871
3086aeae
DJ
4872 case bp_watchpoint:
4873 case bp_hardware_watchpoint:
4874 case bp_read_watchpoint:
4875 case bp_access_watchpoint:
3a5c3e22
PA
4876 {
4877 struct watchpoint *w = (struct watchpoint *) b;
4878
4879 /* Field 4, the address, is omitted (which makes the columns
4880 not line up too nicely with the headers, but the effect
4881 is relatively readable). */
4882 if (opts.addressprint)
4883 ui_out_field_skip (uiout, "addr");
4884 annotate_field (5);
4885 ui_out_field_string (uiout, "what", w->exp_string);
4886 }
3086aeae
DJ
4887 break;
4888
3086aeae
DJ
4889 case bp_breakpoint:
4890 case bp_hardware_breakpoint:
4891 case bp_until:
4892 case bp_finish:
4893 case bp_longjmp:
4894 case bp_longjmp_resume:
186c406b
TT
4895 case bp_exception:
4896 case bp_exception_resume:
3086aeae 4897 case bp_step_resume:
2c03e5be 4898 case bp_hp_step_resume:
3086aeae
DJ
4899 case bp_watchpoint_scope:
4900 case bp_call_dummy:
aa7d318d 4901 case bp_std_terminate:
3086aeae
DJ
4902 case bp_shlib_event:
4903 case bp_thread_event:
4904 case bp_overlay_event:
0fd8e87f 4905 case bp_longjmp_master:
aa7d318d 4906 case bp_std_terminate_master:
186c406b 4907 case bp_exception_master:
1042e4c0 4908 case bp_tracepoint:
7a697b8d 4909 case bp_fast_tracepoint:
0fb4aa4b 4910 case bp_static_tracepoint:
4efc6507 4911 case bp_jit_event:
0e30163f
JK
4912 case bp_gnu_ifunc_resolver:
4913 case bp_gnu_ifunc_resolver_return:
79a45b7d 4914 if (opts.addressprint)
3086aeae
DJ
4915 {
4916 annotate_field (4);
54e52265 4917 if (header_of_multiple)
0d381245 4918 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4919 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4920 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4921 else
5af949e3
UW
4922 ui_out_field_core_addr (uiout, "addr",
4923 loc->gdbarch, loc->address);
3086aeae
DJ
4924 }
4925 annotate_field (5);
0d381245 4926 if (!header_of_multiple)
170b53b2 4927 print_breakpoint_location (b, loc);
0d381245 4928 if (b->loc)
a6d9a66e 4929 *last_loc = b->loc;
3086aeae
DJ
4930 break;
4931 }
c906108c 4932
6c95b8df
PA
4933
4934 /* For backward compatibility, don't display inferiors unless there
4935 are several. */
4936 if (loc != NULL
4937 && !header_of_multiple
4938 && (allflag
4939 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4940 && (number_of_program_spaces () > 1
4941 || number_of_inferiors () > 1)
4a64f543
MS
4942 /* LOC is for existing B, it cannot be in
4943 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4944 && loc->owner->type != bp_catchpoint)))
4945 {
4946 struct inferior *inf;
4947 int first = 1;
4948
4949 for (inf = inferior_list; inf != NULL; inf = inf->next)
4950 {
4951 if (inf->pspace == loc->pspace)
4952 {
4953 if (first)
4954 {
4955 first = 0;
4956 ui_out_text (uiout, " inf ");
4957 }
4958 else
4959 ui_out_text (uiout, ", ");
4960 ui_out_text (uiout, plongest (inf->num));
4961 }
4962 }
4963 }
4964
4a306c9a 4965 if (!part_of_multiple)
c4093a6a 4966 {
4a306c9a
JB
4967 if (b->thread != -1)
4968 {
4969 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4970 "stop only in" line a little further down. */
4a306c9a
JB
4971 ui_out_text (uiout, " thread ");
4972 ui_out_field_int (uiout, "thread", b->thread);
4973 }
4974 else if (b->task != 0)
4975 {
4976 ui_out_text (uiout, " task ");
4977 ui_out_field_int (uiout, "task", b->task);
4978 }
c4093a6a 4979 }
f1310107 4980
8b93c638 4981 ui_out_text (uiout, "\n");
f1310107 4982
348d480f 4983 if (!part_of_multiple)
f1310107
TJB
4984 b->ops->print_one_detail (b, uiout);
4985
0d381245 4986 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4987 {
4988 annotate_field (6);
8b93c638 4989 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 4990 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 4991 the frame ID. */
5af949e3
UW
4992 ui_out_field_core_addr (uiout, "frame",
4993 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4994 ui_out_text (uiout, "\n");
c4093a6a
JM
4995 }
4996
28010a5d 4997 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
4998 {
4999 annotate_field (7);
d77f58be 5000 if (is_tracepoint (b))
1042e4c0
SS
5001 ui_out_text (uiout, "\ttrace only if ");
5002 else
5003 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
5004 ui_out_field_string (uiout, "cond", b->cond_string);
5005 ui_out_text (uiout, "\n");
5006 }
5007
0d381245 5008 if (!part_of_multiple && b->thread != -1)
c4093a6a 5009 {
4a64f543 5010 /* FIXME should make an annotation for this. */
8b93c638
JM
5011 ui_out_text (uiout, "\tstop only in thread ");
5012 ui_out_field_int (uiout, "thread", b->thread);
5013 ui_out_text (uiout, "\n");
c4093a6a
JM
5014 }
5015
63c715c6 5016 if (!part_of_multiple && b->hit_count)
c4093a6a 5017 {
4a64f543 5018 /* FIXME should make an annotation for this. */
8b93c638
JM
5019 if (ep_is_catchpoint (b))
5020 ui_out_text (uiout, "\tcatchpoint");
f196051f
SS
5021 else if (is_tracepoint (b))
5022 ui_out_text (uiout, "\ttracepoint");
8b93c638
JM
5023 else
5024 ui_out_text (uiout, "\tbreakpoint");
5025 ui_out_text (uiout, " already hit ");
5026 ui_out_field_int (uiout, "times", b->hit_count);
5027 if (b->hit_count == 1)
5028 ui_out_text (uiout, " time\n");
5029 else
5030 ui_out_text (uiout, " times\n");
c4093a6a
JM
5031 }
5032
4a64f543
MS
5033 /* Output the count also if it is zero, but only if this is mi.
5034 FIXME: Should have a better test for this. */
9dc5e2a9 5035 if (ui_out_is_mi_like_p (uiout))
63c715c6 5036 if (!part_of_multiple && b->hit_count == 0)
fb40c209 5037 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 5038
0d381245 5039 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
5040 {
5041 annotate_field (8);
8b93c638
JM
5042 ui_out_text (uiout, "\tignore next ");
5043 ui_out_field_int (uiout, "ignore", b->ignore_count);
5044 ui_out_text (uiout, " hits\n");
c4093a6a 5045 }
059fb39f 5046
816338b5
SS
5047 /* Note that an enable count of 1 corresponds to "enable once"
5048 behavior, which is reported by the combination of enablement and
5049 disposition, so we don't need to mention it here. */
5050 if (!part_of_multiple && b->enable_count > 1)
5051 {
5052 annotate_field (8);
5053 ui_out_text (uiout, "\tdisable after ");
5054 /* Tweak the wording to clarify that ignore and enable counts
5055 are distinct, and have additive effect. */
5056 if (b->ignore_count)
5057 ui_out_text (uiout, "additional ");
5058 else
5059 ui_out_text (uiout, "next ");
5060 ui_out_field_int (uiout, "enable", b->enable_count);
5061 ui_out_text (uiout, " hits\n");
5062 }
5063
f196051f
SS
5064 if (!part_of_multiple && is_tracepoint (b))
5065 {
5066 struct tracepoint *tp = (struct tracepoint *) b;
5067
5068 if (tp->traceframe_usage)
5069 {
5070 ui_out_text (uiout, "\ttrace buffer usage ");
5071 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
5072 ui_out_text (uiout, " bytes\n");
5073 }
5074 }
5075
9add0f1b 5076 l = b->commands ? b->commands->commands : NULL;
059fb39f 5077 if (!part_of_multiple && l)
c4093a6a 5078 {
3b31d625
EZ
5079 struct cleanup *script_chain;
5080
c4093a6a 5081 annotate_field (9);
3b31d625 5082 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 5083 print_command_lines (uiout, l, 4);
3b31d625 5084 do_cleanups (script_chain);
c4093a6a 5085 }
d24317b4 5086
d9b3f62e 5087 if (is_tracepoint (b))
1042e4c0 5088 {
d9b3f62e
PA
5089 struct tracepoint *t = (struct tracepoint *) b;
5090
5091 if (!part_of_multiple && t->pass_count)
5092 {
5093 annotate_field (10);
5094 ui_out_text (uiout, "\tpass count ");
5095 ui_out_field_int (uiout, "pass", t->pass_count);
5096 ui_out_text (uiout, " \n");
5097 }
1042e4c0
SS
5098 }
5099
d24317b4
VP
5100 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5101 {
3a5c3e22
PA
5102 if (is_watchpoint (b))
5103 {
5104 struct watchpoint *w = (struct watchpoint *) b;
5105
5106 ui_out_field_string (uiout, "original-location", w->exp_string);
5107 }
5108 else if (b->addr_string)
d24317b4 5109 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 5110 }
c4093a6a 5111}
c5aa993b 5112
0d381245
VP
5113static void
5114print_one_breakpoint (struct breakpoint *b,
4a64f543 5115 struct bp_location **last_loc,
6c95b8df 5116 int allflag)
0d381245 5117{
8d3788bd 5118 struct cleanup *bkpt_chain;
79a45e25 5119 struct ui_out *uiout = current_uiout;
8d3788bd
VP
5120
5121 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5122
12c5a436 5123 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 5124 do_cleanups (bkpt_chain);
0d381245
VP
5125
5126 /* If this breakpoint has custom print function,
5127 it's already printed. Otherwise, print individual
5128 locations, if any. */
5129 if (b->ops == NULL || b->ops->print_one == NULL)
5130 {
4a64f543
MS
5131 /* If breakpoint has a single location that is disabled, we
5132 print it as if it had several locations, since otherwise it's
5133 hard to represent "breakpoint enabled, location disabled"
5134 situation.
5135
5136 Note that while hardware watchpoints have several locations
a3be7890 5137 internally, that's not a property exposed to user. */
0d381245 5138 if (b->loc
a5606eee 5139 && !is_hardware_watchpoint (b)
8d3788bd 5140 && (b->loc->next || !b->loc->enabled))
0d381245
VP
5141 {
5142 struct bp_location *loc;
5143 int n = 1;
8d3788bd 5144
0d381245 5145 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5146 {
5147 struct cleanup *inner2 =
5148 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5149 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5150 do_cleanups (inner2);
5151 }
0d381245
VP
5152 }
5153 }
5154}
5155
a6d9a66e
UW
5156static int
5157breakpoint_address_bits (struct breakpoint *b)
5158{
5159 int print_address_bits = 0;
5160 struct bp_location *loc;
5161
5162 for (loc = b->loc; loc; loc = loc->next)
5163 {
c7437ca6
PA
5164 int addr_bit;
5165
5166 /* Software watchpoints that aren't watching memory don't have
5167 an address to print. */
5168 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5169 continue;
5170
5171 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5172 if (addr_bit > print_address_bits)
5173 print_address_bits = addr_bit;
5174 }
5175
5176 return print_address_bits;
5177}
0d381245 5178
c4093a6a
JM
5179struct captured_breakpoint_query_args
5180 {
5181 int bnum;
5182 };
c5aa993b 5183
c4093a6a 5184static int
2b65245e 5185do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5186{
5187 struct captured_breakpoint_query_args *args = data;
52f0bd74 5188 struct breakpoint *b;
a6d9a66e 5189 struct bp_location *dummy_loc = NULL;
cc59ec59 5190
c4093a6a
JM
5191 ALL_BREAKPOINTS (b)
5192 {
5193 if (args->bnum == b->number)
c5aa993b 5194 {
12c5a436 5195 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5196 return GDB_RC_OK;
c5aa993b 5197 }
c4093a6a
JM
5198 }
5199 return GDB_RC_NONE;
5200}
c5aa993b 5201
c4093a6a 5202enum gdb_rc
4a64f543
MS
5203gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5204 char **error_message)
c4093a6a
JM
5205{
5206 struct captured_breakpoint_query_args args;
cc59ec59 5207
c4093a6a
JM
5208 args.bnum = bnum;
5209 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5210 an error. */
b0b13bb4
DJ
5211 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5212 error_message, RETURN_MASK_ALL) < 0)
5213 return GDB_RC_FAIL;
5214 else
5215 return GDB_RC_OK;
c4093a6a 5216}
c5aa993b 5217
09d682a4
TT
5218/* Return true if this breakpoint was set by the user, false if it is
5219 internal or momentary. */
5220
5221int
5222user_breakpoint_p (struct breakpoint *b)
5223{
46c6471b 5224 return b->number > 0;
09d682a4
TT
5225}
5226
7f3b0473 5227/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5228 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5229 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5230 FILTER is non-NULL, call it on each breakpoint and only include the
5231 ones for which it returns non-zero. Return the total number of
5232 breakpoints listed. */
c906108c 5233
d77f58be 5234static int
e5a67952 5235breakpoint_1 (char *args, int allflag,
4a64f543 5236 int (*filter) (const struct breakpoint *))
c4093a6a 5237{
52f0bd74 5238 struct breakpoint *b;
a6d9a66e 5239 struct bp_location *last_loc = NULL;
7f3b0473 5240 int nr_printable_breakpoints;
3b31d625 5241 struct cleanup *bkpttbl_chain;
79a45b7d 5242 struct value_print_options opts;
a6d9a66e 5243 int print_address_bits = 0;
269b11a2 5244 int print_type_col_width = 14;
79a45e25 5245 struct ui_out *uiout = current_uiout;
269b11a2 5246
79a45b7d
TT
5247 get_user_print_options (&opts);
5248
4a64f543
MS
5249 /* Compute the number of rows in the table, as well as the size
5250 required for address fields. */
7f3b0473
AC
5251 nr_printable_breakpoints = 0;
5252 ALL_BREAKPOINTS (b)
e5a67952
MS
5253 {
5254 /* If we have a filter, only list the breakpoints it accepts. */
5255 if (filter && !filter (b))
5256 continue;
5257
5258 /* If we have an "args" string, it is a list of breakpoints to
5259 accept. Skip the others. */
5260 if (args != NULL && *args != '\0')
5261 {
5262 if (allflag && parse_and_eval_long (args) != b->number)
5263 continue;
5264 if (!allflag && !number_is_in_list (args, b->number))
5265 continue;
5266 }
269b11a2 5267
e5a67952
MS
5268 if (allflag || user_breakpoint_p (b))
5269 {
5270 int addr_bit, type_len;
a6d9a66e 5271
e5a67952
MS
5272 addr_bit = breakpoint_address_bits (b);
5273 if (addr_bit > print_address_bits)
5274 print_address_bits = addr_bit;
269b11a2 5275
e5a67952
MS
5276 type_len = strlen (bptype_string (b->type));
5277 if (type_len > print_type_col_width)
5278 print_type_col_width = type_len;
5279
5280 nr_printable_breakpoints++;
5281 }
5282 }
7f3b0473 5283
79a45b7d 5284 if (opts.addressprint)
3b31d625 5285 bkpttbl_chain
3e43a32a
MS
5286 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5287 nr_printable_breakpoints,
3b31d625 5288 "BreakpointTable");
8b93c638 5289 else
3b31d625 5290 bkpttbl_chain
3e43a32a
MS
5291 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5292 nr_printable_breakpoints,
3b31d625 5293 "BreakpointTable");
8b93c638 5294
7f3b0473 5295 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5296 annotate_breakpoints_headers ();
5297 if (nr_printable_breakpoints > 0)
5298 annotate_field (0);
4a64f543 5299 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5300 if (nr_printable_breakpoints > 0)
5301 annotate_field (1);
269b11a2 5302 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5303 "type", "Type"); /* 2 */
d7faa9e7
AC
5304 if (nr_printable_breakpoints > 0)
5305 annotate_field (2);
4a64f543 5306 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5307 if (nr_printable_breakpoints > 0)
5308 annotate_field (3);
54e52265 5309 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5310 if (opts.addressprint)
e5a67952
MS
5311 {
5312 if (nr_printable_breakpoints > 0)
5313 annotate_field (4);
5314 if (print_address_bits <= 32)
5315 ui_out_table_header (uiout, 10, ui_left,
5316 "addr", "Address"); /* 5 */
5317 else
5318 ui_out_table_header (uiout, 18, ui_left,
5319 "addr", "Address"); /* 5 */
5320 }
d7faa9e7
AC
5321 if (nr_printable_breakpoints > 0)
5322 annotate_field (5);
5323 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5324 ui_out_table_body (uiout);
5325 if (nr_printable_breakpoints > 0)
5326 annotate_breakpoints_table ();
7f3b0473 5327
c4093a6a 5328 ALL_BREAKPOINTS (b)
e5a67952
MS
5329 {
5330 QUIT;
5331 /* If we have a filter, only list the breakpoints it accepts. */
5332 if (filter && !filter (b))
5333 continue;
5334
5335 /* If we have an "args" string, it is a list of breakpoints to
5336 accept. Skip the others. */
5337
5338 if (args != NULL && *args != '\0')
5339 {
5340 if (allflag) /* maintenance info breakpoint */
5341 {
5342 if (parse_and_eval_long (args) != b->number)
5343 continue;
5344 }
5345 else /* all others */
5346 {
5347 if (!number_is_in_list (args, b->number))
5348 continue;
5349 }
5350 }
5351 /* We only print out user settable breakpoints unless the
5352 allflag is set. */
5353 if (allflag || user_breakpoint_p (b))
12c5a436 5354 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5355 }
5356
3b31d625 5357 do_cleanups (bkpttbl_chain);
698384cd 5358
7f3b0473 5359 if (nr_printable_breakpoints == 0)
c906108c 5360 {
4a64f543
MS
5361 /* If there's a filter, let the caller decide how to report
5362 empty list. */
d77f58be
SS
5363 if (!filter)
5364 {
e5a67952 5365 if (args == NULL || *args == '\0')
d77f58be
SS
5366 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5367 else
4a64f543 5368 ui_out_message (uiout, 0,
e5a67952
MS
5369 "No breakpoint or watchpoint matching '%s'.\n",
5370 args);
d77f58be 5371 }
c906108c
SS
5372 }
5373 else
c4093a6a 5374 {
a6d9a66e
UW
5375 if (last_loc && !server_command)
5376 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5377 }
c906108c 5378
4a64f543 5379 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5380 there have been breakpoints? */
c906108c 5381 annotate_breakpoints_table_end ();
d77f58be
SS
5382
5383 return nr_printable_breakpoints;
c906108c
SS
5384}
5385
ad443146
SS
5386/* Display the value of default-collect in a way that is generally
5387 compatible with the breakpoint list. */
5388
5389static void
5390default_collect_info (void)
5391{
79a45e25
PA
5392 struct ui_out *uiout = current_uiout;
5393
ad443146
SS
5394 /* If it has no value (which is frequently the case), say nothing; a
5395 message like "No default-collect." gets in user's face when it's
5396 not wanted. */
5397 if (!*default_collect)
5398 return;
5399
5400 /* The following phrase lines up nicely with per-tracepoint collect
5401 actions. */
5402 ui_out_text (uiout, "default collect ");
5403 ui_out_field_string (uiout, "default-collect", default_collect);
5404 ui_out_text (uiout, " \n");
5405}
5406
c906108c 5407static void
e5a67952 5408breakpoints_info (char *args, int from_tty)
c906108c 5409{
e5a67952 5410 breakpoint_1 (args, 0, NULL);
ad443146
SS
5411
5412 default_collect_info ();
d77f58be
SS
5413}
5414
5415static void
e5a67952 5416watchpoints_info (char *args, int from_tty)
d77f58be 5417{
e5a67952 5418 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 5419 struct ui_out *uiout = current_uiout;
d77f58be
SS
5420
5421 if (num_printed == 0)
5422 {
e5a67952 5423 if (args == NULL || *args == '\0')
d77f58be
SS
5424 ui_out_message (uiout, 0, "No watchpoints.\n");
5425 else
e5a67952 5426 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5427 }
c906108c
SS
5428}
5429
7a292a7a 5430static void
e5a67952 5431maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5432{
e5a67952 5433 breakpoint_1 (args, 1, NULL);
ad443146
SS
5434
5435 default_collect_info ();
c906108c
SS
5436}
5437
0d381245 5438static int
714835d5 5439breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5440 struct program_space *pspace,
714835d5 5441 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5442{
5443 struct bp_location *bl = b->loc;
cc59ec59 5444
0d381245
VP
5445 for (; bl; bl = bl->next)
5446 {
6c95b8df
PA
5447 if (bl->pspace == pspace
5448 && bl->address == pc
0d381245
VP
5449 && (!overlay_debugging || bl->section == section))
5450 return 1;
5451 }
5452 return 0;
5453}
5454
672f9b60 5455/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
5456 concerns with logical breakpoints, so we match program spaces, not
5457 address spaces. */
c906108c
SS
5458
5459static void
6c95b8df
PA
5460describe_other_breakpoints (struct gdbarch *gdbarch,
5461 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5462 struct obj_section *section, int thread)
c906108c 5463{
52f0bd74
AC
5464 int others = 0;
5465 struct breakpoint *b;
c906108c
SS
5466
5467 ALL_BREAKPOINTS (b)
672f9b60
KP
5468 others += (user_breakpoint_p (b)
5469 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
5470 if (others > 0)
5471 {
a3f17187
AC
5472 if (others == 1)
5473 printf_filtered (_("Note: breakpoint "));
5474 else /* if (others == ???) */
5475 printf_filtered (_("Note: breakpoints "));
c906108c 5476 ALL_BREAKPOINTS (b)
672f9b60 5477 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5478 {
5479 others--;
5480 printf_filtered ("%d", b->number);
5481 if (b->thread == -1 && thread != -1)
5482 printf_filtered (" (all threads)");
5483 else if (b->thread != -1)
5484 printf_filtered (" (thread %d)", b->thread);
5485 printf_filtered ("%s%s ",
059fb39f 5486 ((b->enable_state == bp_disabled
f8eba3c6 5487 || b->enable_state == bp_call_disabled)
0d381245
VP
5488 ? " (disabled)"
5489 : b->enable_state == bp_permanent
5490 ? " (permanent)"
5491 : ""),
5492 (others > 1) ? ","
5493 : ((others == 1) ? " and" : ""));
5494 }
a3f17187 5495 printf_filtered (_("also set at pc "));
5af949e3 5496 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5497 printf_filtered (".\n");
5498 }
5499}
5500\f
c906108c 5501
e4f237da
KB
5502/* Return true iff it is meaningful to use the address member of
5503 BPT. For some breakpoint types, the address member is irrelevant
5504 and it makes no sense to attempt to compare it to other addresses
5505 (or use it for any other purpose either).
5506
4a64f543
MS
5507 More specifically, each of the following breakpoint types will
5508 always have a zero valued address and we don't want to mark
5509 breakpoints of any of these types to be a duplicate of an actual
5510 breakpoint at address zero:
e4f237da
KB
5511
5512 bp_watchpoint
2d134ed3
PA
5513 bp_catchpoint
5514
5515*/
e4f237da
KB
5516
5517static int
5518breakpoint_address_is_meaningful (struct breakpoint *bpt)
5519{
5520 enum bptype type = bpt->type;
5521
2d134ed3
PA
5522 return (type != bp_watchpoint && type != bp_catchpoint);
5523}
5524
5525/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5526 true if LOC1 and LOC2 represent the same watchpoint location. */
5527
5528static int
4a64f543
MS
5529watchpoint_locations_match (struct bp_location *loc1,
5530 struct bp_location *loc2)
2d134ed3 5531{
3a5c3e22
PA
5532 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
5533 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
5534
5535 /* Both of them must exist. */
5536 gdb_assert (w1 != NULL);
5537 gdb_assert (w2 != NULL);
2bdf28a0 5538
4a64f543
MS
5539 /* If the target can evaluate the condition expression in hardware,
5540 then we we need to insert both watchpoints even if they are at
5541 the same place. Otherwise the watchpoint will only trigger when
5542 the condition of whichever watchpoint was inserted evaluates to
5543 true, not giving a chance for GDB to check the condition of the
5544 other watchpoint. */
3a5c3e22 5545 if ((w1->cond_exp
4a64f543
MS
5546 && target_can_accel_watchpoint_condition (loc1->address,
5547 loc1->length,
0cf6dd15 5548 loc1->watchpoint_type,
3a5c3e22
PA
5549 w1->cond_exp))
5550 || (w2->cond_exp
4a64f543
MS
5551 && target_can_accel_watchpoint_condition (loc2->address,
5552 loc2->length,
0cf6dd15 5553 loc2->watchpoint_type,
3a5c3e22 5554 w2->cond_exp)))
0cf6dd15
TJB
5555 return 0;
5556
85d721b8
PA
5557 /* Note that this checks the owner's type, not the location's. In
5558 case the target does not support read watchpoints, but does
5559 support access watchpoints, we'll have bp_read_watchpoint
5560 watchpoints with hw_access locations. Those should be considered
5561 duplicates of hw_read locations. The hw_read locations will
5562 become hw_access locations later. */
2d134ed3
PA
5563 return (loc1->owner->type == loc2->owner->type
5564 && loc1->pspace->aspace == loc2->pspace->aspace
5565 && loc1->address == loc2->address
5566 && loc1->length == loc2->length);
e4f237da
KB
5567}
5568
6c95b8df
PA
5569/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5570 same breakpoint location. In most targets, this can only be true
5571 if ASPACE1 matches ASPACE2. On targets that have global
5572 breakpoints, the address space doesn't really matter. */
5573
5574static int
5575breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5576 struct address_space *aspace2, CORE_ADDR addr2)
5577{
5578 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5579 || aspace1 == aspace2)
5580 && addr1 == addr2);
5581}
5582
f1310107
TJB
5583/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5584 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5585 matches ASPACE2. On targets that have global breakpoints, the address
5586 space doesn't really matter. */
5587
5588static int
5589breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5590 int len1, struct address_space *aspace2,
5591 CORE_ADDR addr2)
5592{
5593 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5594 || aspace1 == aspace2)
5595 && addr2 >= addr1 && addr2 < addr1 + len1);
5596}
5597
5598/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5599 a ranged breakpoint. In most targets, a match happens only if ASPACE
5600 matches the breakpoint's address space. On targets that have global
5601 breakpoints, the address space doesn't really matter. */
5602
5603static int
5604breakpoint_location_address_match (struct bp_location *bl,
5605 struct address_space *aspace,
5606 CORE_ADDR addr)
5607{
5608 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5609 aspace, addr)
5610 || (bl->length
5611 && breakpoint_address_match_range (bl->pspace->aspace,
5612 bl->address, bl->length,
5613 aspace, addr)));
5614}
5615
1e4d1764
YQ
5616/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
5617 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
5618 true, otherwise returns false. */
5619
5620static int
5621tracepoint_locations_match (struct bp_location *loc1,
5622 struct bp_location *loc2)
5623{
5624 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
5625 /* Since tracepoint locations are never duplicated with others', tracepoint
5626 locations at the same address of different tracepoints are regarded as
5627 different locations. */
5628 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
5629 else
5630 return 0;
5631}
5632
2d134ed3
PA
5633/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5634 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5635 represent the same location. */
5636
5637static int
4a64f543
MS
5638breakpoint_locations_match (struct bp_location *loc1,
5639 struct bp_location *loc2)
2d134ed3 5640{
2bdf28a0
JK
5641 int hw_point1, hw_point2;
5642
5643 /* Both of them must not be in moribund_locations. */
5644 gdb_assert (loc1->owner != NULL);
5645 gdb_assert (loc2->owner != NULL);
5646
5647 hw_point1 = is_hardware_watchpoint (loc1->owner);
5648 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5649
5650 if (hw_point1 != hw_point2)
5651 return 0;
5652 else if (hw_point1)
5653 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
5654 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
5655 return tracepoint_locations_match (loc1, loc2);
2d134ed3 5656 else
f1310107
TJB
5657 /* We compare bp_location.length in order to cover ranged breakpoints. */
5658 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5659 loc2->pspace->aspace, loc2->address)
5660 && loc1->length == loc2->length);
2d134ed3
PA
5661}
5662
76897487
KB
5663static void
5664breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5665 int bnum, int have_bnum)
5666{
f63fbe86
MS
5667 /* The longest string possibly returned by hex_string_custom
5668 is 50 chars. These must be at least that big for safety. */
5669 char astr1[64];
5670 char astr2[64];
76897487 5671
bb599908
PH
5672 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5673 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5674 if (have_bnum)
8a3fe4f8 5675 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5676 bnum, astr1, astr2);
5677 else
8a3fe4f8 5678 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5679}
5680
4a64f543
MS
5681/* Adjust a breakpoint's address to account for architectural
5682 constraints on breakpoint placement. Return the adjusted address.
5683 Note: Very few targets require this kind of adjustment. For most
5684 targets, this function is simply the identity function. */
76897487
KB
5685
5686static CORE_ADDR
a6d9a66e
UW
5687adjust_breakpoint_address (struct gdbarch *gdbarch,
5688 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5689{
a6d9a66e 5690 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5691 {
5692 /* Very few targets need any kind of breakpoint adjustment. */
5693 return bpaddr;
5694 }
88f7da05
KB
5695 else if (bptype == bp_watchpoint
5696 || bptype == bp_hardware_watchpoint
5697 || bptype == bp_read_watchpoint
5698 || bptype == bp_access_watchpoint
fe798b75 5699 || bptype == bp_catchpoint)
88f7da05
KB
5700 {
5701 /* Watchpoints and the various bp_catch_* eventpoints should not
5702 have their addresses modified. */
5703 return bpaddr;
5704 }
76897487
KB
5705 else
5706 {
5707 CORE_ADDR adjusted_bpaddr;
5708
5709 /* Some targets have architectural constraints on the placement
5710 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5711 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5712
5713 /* An adjusted breakpoint address can significantly alter
5714 a user's expectations. Print a warning if an adjustment
5715 is required. */
5716 if (adjusted_bpaddr != bpaddr)
5717 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5718
5719 return adjusted_bpaddr;
5720 }
5721}
5722
28010a5d
PA
5723void
5724init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5725 struct breakpoint *owner)
7cc221ef 5726{
7cc221ef
DJ
5727 memset (loc, 0, sizeof (*loc));
5728
348d480f
PA
5729 gdb_assert (ops != NULL);
5730
28010a5d
PA
5731 loc->ops = ops;
5732 loc->owner = owner;
511a6cd4 5733 loc->cond = NULL;
0d381245
VP
5734 loc->shlib_disabled = 0;
5735 loc->enabled = 1;
e049a4b5 5736
28010a5d 5737 switch (owner->type)
e049a4b5
DJ
5738 {
5739 case bp_breakpoint:
5740 case bp_until:
5741 case bp_finish:
5742 case bp_longjmp:
5743 case bp_longjmp_resume:
186c406b
TT
5744 case bp_exception:
5745 case bp_exception_resume:
e049a4b5 5746 case bp_step_resume:
2c03e5be 5747 case bp_hp_step_resume:
e049a4b5
DJ
5748 case bp_watchpoint_scope:
5749 case bp_call_dummy:
aa7d318d 5750 case bp_std_terminate:
e049a4b5
DJ
5751 case bp_shlib_event:
5752 case bp_thread_event:
5753 case bp_overlay_event:
4efc6507 5754 case bp_jit_event:
0fd8e87f 5755 case bp_longjmp_master:
aa7d318d 5756 case bp_std_terminate_master:
186c406b 5757 case bp_exception_master:
0e30163f
JK
5758 case bp_gnu_ifunc_resolver:
5759 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5760 loc->loc_type = bp_loc_software_breakpoint;
5761 break;
5762 case bp_hardware_breakpoint:
5763 loc->loc_type = bp_loc_hardware_breakpoint;
5764 break;
5765 case bp_hardware_watchpoint:
5766 case bp_read_watchpoint:
5767 case bp_access_watchpoint:
5768 loc->loc_type = bp_loc_hardware_watchpoint;
5769 break;
5770 case bp_watchpoint:
ce78b96d 5771 case bp_catchpoint:
15c3d785
PA
5772 case bp_tracepoint:
5773 case bp_fast_tracepoint:
0fb4aa4b 5774 case bp_static_tracepoint:
e049a4b5
DJ
5775 loc->loc_type = bp_loc_other;
5776 break;
5777 default:
e2e0b3e5 5778 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5779 }
5780
f431efe5 5781 loc->refc = 1;
28010a5d
PA
5782}
5783
5784/* Allocate a struct bp_location. */
5785
5786static struct bp_location *
5787allocate_bp_location (struct breakpoint *bpt)
5788{
348d480f
PA
5789 return bpt->ops->allocate_location (bpt);
5790}
7cc221ef 5791
f431efe5
PA
5792static void
5793free_bp_location (struct bp_location *loc)
fe3f5fa8 5794{
348d480f 5795 loc->ops->dtor (loc);
fe3f5fa8
VP
5796 xfree (loc);
5797}
5798
f431efe5
PA
5799/* Increment reference count. */
5800
5801static void
5802incref_bp_location (struct bp_location *bl)
5803{
5804 ++bl->refc;
5805}
5806
5807/* Decrement reference count. If the reference count reaches 0,
5808 destroy the bp_location. Sets *BLP to NULL. */
5809
5810static void
5811decref_bp_location (struct bp_location **blp)
5812{
0807b50c
PA
5813 gdb_assert ((*blp)->refc > 0);
5814
f431efe5
PA
5815 if (--(*blp)->refc == 0)
5816 free_bp_location (*blp);
5817 *blp = NULL;
5818}
5819
346774a9 5820/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 5821
346774a9
PA
5822static void
5823add_to_breakpoint_chain (struct breakpoint *b)
c906108c 5824{
346774a9 5825 struct breakpoint *b1;
c906108c 5826
346774a9
PA
5827 /* Add this breakpoint to the end of the chain so that a list of
5828 breakpoints will come out in order of increasing numbers. */
5829
5830 b1 = breakpoint_chain;
5831 if (b1 == 0)
5832 breakpoint_chain = b;
5833 else
5834 {
5835 while (b1->next)
5836 b1 = b1->next;
5837 b1->next = b;
5838 }
5839}
5840
5841/* Initializes breakpoint B with type BPTYPE and no locations yet. */
5842
5843static void
5844init_raw_breakpoint_without_location (struct breakpoint *b,
5845 struct gdbarch *gdbarch,
28010a5d 5846 enum bptype bptype,
c0a91b2b 5847 const struct breakpoint_ops *ops)
346774a9 5848{
c906108c 5849 memset (b, 0, sizeof (*b));
2219d63c 5850
348d480f
PA
5851 gdb_assert (ops != NULL);
5852
28010a5d 5853 b->ops = ops;
4d28f7a8 5854 b->type = bptype;
a6d9a66e 5855 b->gdbarch = gdbarch;
c906108c
SS
5856 b->language = current_language->la_language;
5857 b->input_radix = input_radix;
5858 b->thread = -1;
b5de0fa7 5859 b->enable_state = bp_enabled;
c906108c
SS
5860 b->next = 0;
5861 b->silent = 0;
5862 b->ignore_count = 0;
5863 b->commands = NULL;
818dd999 5864 b->frame_id = null_frame_id;
0d381245 5865 b->condition_not_parsed = 0;
84f4c1fe 5866 b->py_bp_object = NULL;
d0fb5eae 5867 b->related_breakpoint = b;
346774a9
PA
5868}
5869
5870/* Helper to set_raw_breakpoint below. Creates a breakpoint
5871 that has type BPTYPE and has no locations as yet. */
346774a9
PA
5872
5873static struct breakpoint *
5874set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 5875 enum bptype bptype,
c0a91b2b 5876 const struct breakpoint_ops *ops)
346774a9
PA
5877{
5878 struct breakpoint *b = XNEW (struct breakpoint);
5879
348d480f 5880 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 5881 add_to_breakpoint_chain (b);
0d381245
VP
5882 return b;
5883}
5884
0e30163f
JK
5885/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5886 resolutions should be made as the user specified the location explicitly
5887 enough. */
5888
0d381245 5889static void
0e30163f 5890set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5891{
2bdf28a0
JK
5892 gdb_assert (loc->owner != NULL);
5893
0d381245 5894 if (loc->owner->type == bp_breakpoint
1042e4c0 5895 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5896 || is_tracepoint (loc->owner))
0d381245 5897 {
0e30163f 5898 int is_gnu_ifunc;
2c02bd72 5899 const char *function_name;
0e30163f 5900
2c02bd72 5901 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
0e30163f
JK
5902 NULL, NULL, &is_gnu_ifunc);
5903
5904 if (is_gnu_ifunc && !explicit_loc)
5905 {
5906 struct breakpoint *b = loc->owner;
5907
5908 gdb_assert (loc->pspace == current_program_space);
2c02bd72 5909 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
5910 &loc->requested_address))
5911 {
5912 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5913 loc->address = adjust_breakpoint_address (loc->gdbarch,
5914 loc->requested_address,
5915 b->type);
5916 }
5917 else if (b->type == bp_breakpoint && b->loc == loc
5918 && loc->next == NULL && b->related_breakpoint == b)
5919 {
5920 /* Create only the whole new breakpoint of this type but do not
5921 mess more complicated breakpoints with multiple locations. */
5922 b->type = bp_gnu_ifunc_resolver;
5923 }
5924 }
5925
2c02bd72
DE
5926 if (function_name)
5927 loc->function_name = xstrdup (function_name);
0d381245
VP
5928 }
5929}
5930
a6d9a66e 5931/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 5932struct gdbarch *
a6d9a66e
UW
5933get_sal_arch (struct symtab_and_line sal)
5934{
5935 if (sal.section)
5936 return get_objfile_arch (sal.section->objfile);
5937 if (sal.symtab)
5938 return get_objfile_arch (sal.symtab->objfile);
5939
5940 return NULL;
5941}
5942
346774a9
PA
5943/* Low level routine for partially initializing a breakpoint of type
5944 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 5945 file name, and line number are provided by SAL.
0d381245
VP
5946
5947 It is expected that the caller will complete the initialization of
5948 the newly created breakpoint struct as well as output any status
c56053d2 5949 information regarding the creation of a new breakpoint. */
0d381245 5950
346774a9
PA
5951static void
5952init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 5953 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5954 const struct breakpoint_ops *ops)
0d381245 5955{
28010a5d 5956 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 5957
3742cc8b 5958 add_location_to_breakpoint (b, &sal);
0d381245 5959
6c95b8df
PA
5960 if (bptype != bp_catchpoint)
5961 gdb_assert (sal.pspace != NULL);
5962
f8eba3c6
TT
5963 /* Store the program space that was used to set the breakpoint,
5964 except for ordinary breakpoints, which are independent of the
5965 program space. */
5966 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
5967 b->pspace = sal.pspace;
0d381245 5968
c906108c 5969 breakpoints_changed ();
346774a9 5970}
c906108c 5971
346774a9
PA
5972/* set_raw_breakpoint is a low level routine for allocating and
5973 partially initializing a breakpoint of type BPTYPE. The newly
5974 created breakpoint's address, section, source file name, and line
5975 number are provided by SAL. The newly created and partially
5976 initialized breakpoint is added to the breakpoint chain and
5977 is also returned as the value of this function.
5978
5979 It is expected that the caller will complete the initialization of
5980 the newly created breakpoint struct as well as output any status
5981 information regarding the creation of a new breakpoint. In
5982 particular, set_raw_breakpoint does NOT set the breakpoint
5983 number! Care should be taken to not allow an error to occur
5984 prior to completing the initialization of the breakpoint. If this
5985 should happen, a bogus breakpoint will be left on the chain. */
5986
5987struct breakpoint *
5988set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 5989 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5990 const struct breakpoint_ops *ops)
346774a9
PA
5991{
5992 struct breakpoint *b = XNEW (struct breakpoint);
5993
348d480f 5994 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 5995 add_to_breakpoint_chain (b);
c906108c
SS
5996 return b;
5997}
5998
c2c6d25f
JM
5999
6000/* Note that the breakpoint object B describes a permanent breakpoint
6001 instruction, hard-wired into the inferior's code. */
6002void
6003make_breakpoint_permanent (struct breakpoint *b)
6004{
0d381245 6005 struct bp_location *bl;
cc59ec59 6006
b5de0fa7 6007 b->enable_state = bp_permanent;
c2c6d25f 6008
4a64f543
MS
6009 /* By definition, permanent breakpoints are already present in the
6010 code. Mark all locations as inserted. For now,
6011 make_breakpoint_permanent is called in just one place, so it's
6012 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 6013 multiple locations or not, but it's easy to implement. */
0d381245
VP
6014 for (bl = b->loc; bl; bl = bl->next)
6015 bl->inserted = 1;
c2c6d25f
JM
6016}
6017
53a5351d 6018/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
6019 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
6020 initiated the operation. */
c906108c
SS
6021
6022void
186c406b 6023set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 6024{
35df4500 6025 struct breakpoint *b, *b_tmp;
186c406b 6026 int thread = tp->num;
0fd8e87f
UW
6027
6028 /* To avoid having to rescan all objfile symbols at every step,
6029 we maintain a list of continually-inserted but always disabled
6030 longjmp "master" breakpoints. Here, we simply create momentary
6031 clones of those and enable them for the requested thread. */
35df4500 6032 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 6033 if (b->pspace == current_program_space
186c406b
TT
6034 && (b->type == bp_longjmp_master
6035 || b->type == bp_exception_master))
0fd8e87f 6036 {
06edf0c0
PA
6037 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
6038 struct breakpoint *clone;
cc59ec59 6039
06edf0c0
PA
6040 clone = momentary_breakpoint_from_master (b, type,
6041 &momentary_breakpoint_ops);
0fd8e87f
UW
6042 clone->thread = thread;
6043 }
186c406b
TT
6044
6045 tp->initiating_frame = frame;
c906108c
SS
6046}
6047
611c83ae 6048/* Delete all longjmp breakpoints from THREAD. */
c906108c 6049void
611c83ae 6050delete_longjmp_breakpoint (int thread)
c906108c 6051{
35df4500 6052 struct breakpoint *b, *b_tmp;
c906108c 6053
35df4500 6054 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 6055 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
6056 {
6057 if (b->thread == thread)
6058 delete_breakpoint (b);
6059 }
c906108c
SS
6060}
6061
1900040c
MS
6062void
6063enable_overlay_breakpoints (void)
6064{
52f0bd74 6065 struct breakpoint *b;
1900040c
MS
6066
6067 ALL_BREAKPOINTS (b)
6068 if (b->type == bp_overlay_event)
6069 {
6070 b->enable_state = bp_enabled;
b60e7edf 6071 update_global_location_list (1);
c02f5703 6072 overlay_events_enabled = 1;
1900040c
MS
6073 }
6074}
6075
6076void
6077disable_overlay_breakpoints (void)
6078{
52f0bd74 6079 struct breakpoint *b;
1900040c
MS
6080
6081 ALL_BREAKPOINTS (b)
6082 if (b->type == bp_overlay_event)
6083 {
6084 b->enable_state = bp_disabled;
b60e7edf 6085 update_global_location_list (0);
c02f5703 6086 overlay_events_enabled = 0;
1900040c
MS
6087 }
6088}
6089
aa7d318d
TT
6090/* Set an active std::terminate breakpoint for each std::terminate
6091 master breakpoint. */
6092void
6093set_std_terminate_breakpoint (void)
6094{
35df4500 6095 struct breakpoint *b, *b_tmp;
aa7d318d 6096
35df4500 6097 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6098 if (b->pspace == current_program_space
6099 && b->type == bp_std_terminate_master)
6100 {
06edf0c0
PA
6101 momentary_breakpoint_from_master (b, bp_std_terminate,
6102 &momentary_breakpoint_ops);
aa7d318d
TT
6103 }
6104}
6105
6106/* Delete all the std::terminate breakpoints. */
6107void
6108delete_std_terminate_breakpoint (void)
6109{
35df4500 6110 struct breakpoint *b, *b_tmp;
aa7d318d 6111
35df4500 6112 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6113 if (b->type == bp_std_terminate)
6114 delete_breakpoint (b);
6115}
6116
c4093a6a 6117struct breakpoint *
a6d9a66e 6118create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6119{
6120 struct breakpoint *b;
c4093a6a 6121
06edf0c0
PA
6122 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
6123 &internal_breakpoint_ops);
6124
b5de0fa7 6125 b->enable_state = bp_enabled;
c4093a6a 6126 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6127 b->addr_string
6128 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6129
b60e7edf 6130 update_global_location_list_nothrow (1);
74960c60 6131
c4093a6a
JM
6132 return b;
6133}
6134
6135void
6136remove_thread_event_breakpoints (void)
6137{
35df4500 6138 struct breakpoint *b, *b_tmp;
c4093a6a 6139
35df4500 6140 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6141 if (b->type == bp_thread_event
6142 && b->loc->pspace == current_program_space)
c4093a6a
JM
6143 delete_breakpoint (b);
6144}
6145
0101ce28
JJ
6146struct lang_and_radix
6147 {
6148 enum language lang;
6149 int radix;
6150 };
6151
4efc6507
DE
6152/* Create a breakpoint for JIT code registration and unregistration. */
6153
6154struct breakpoint *
6155create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6156{
6157 struct breakpoint *b;
6158
06edf0c0
PA
6159 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
6160 &internal_breakpoint_ops);
4efc6507
DE
6161 update_global_location_list_nothrow (1);
6162 return b;
6163}
0101ce28 6164
03673fc7
PP
6165/* Remove JIT code registration and unregistration breakpoint(s). */
6166
6167void
6168remove_jit_event_breakpoints (void)
6169{
6170 struct breakpoint *b, *b_tmp;
6171
6172 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6173 if (b->type == bp_jit_event
6174 && b->loc->pspace == current_program_space)
6175 delete_breakpoint (b);
6176}
6177
cae688ec
JJ
6178void
6179remove_solib_event_breakpoints (void)
6180{
35df4500 6181 struct breakpoint *b, *b_tmp;
cae688ec 6182
35df4500 6183 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6184 if (b->type == bp_shlib_event
6185 && b->loc->pspace == current_program_space)
cae688ec
JJ
6186 delete_breakpoint (b);
6187}
6188
6189struct breakpoint *
a6d9a66e 6190create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6191{
6192 struct breakpoint *b;
6193
06edf0c0
PA
6194 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6195 &internal_breakpoint_ops);
b60e7edf 6196 update_global_location_list_nothrow (1);
cae688ec
JJ
6197 return b;
6198}
6199
6200/* Disable any breakpoints that are on code in shared libraries. Only
6201 apply to enabled breakpoints, disabled ones can just stay disabled. */
6202
6203void
cb851954 6204disable_breakpoints_in_shlibs (void)
cae688ec 6205{
876fa593 6206 struct bp_location *loc, **locp_tmp;
cae688ec 6207
876fa593 6208 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6209 {
2bdf28a0 6210 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6211 struct breakpoint *b = loc->owner;
2bdf28a0 6212
4a64f543
MS
6213 /* We apply the check to all breakpoints, including disabled for
6214 those with loc->duplicate set. This is so that when breakpoint
6215 becomes enabled, or the duplicate is removed, gdb will try to
6216 insert all breakpoints. If we don't set shlib_disabled here,
6217 we'll try to insert those breakpoints and fail. */
1042e4c0 6218 if (((b->type == bp_breakpoint)
508ccb1f 6219 || (b->type == bp_jit_event)
1042e4c0 6220 || (b->type == bp_hardware_breakpoint)
d77f58be 6221 || (is_tracepoint (b)))
6c95b8df 6222 && loc->pspace == current_program_space
0d381245 6223 && !loc->shlib_disabled
a77053c2 6224#ifdef PC_SOLIB
0d381245 6225 && PC_SOLIB (loc->address)
a77053c2 6226#else
6c95b8df 6227 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6228#endif
6229 )
0d381245
VP
6230 {
6231 loc->shlib_disabled = 1;
6232 }
cae688ec
JJ
6233 }
6234}
6235
1e4d1764
YQ
6236/* Disable any breakpoints and tracepoints that are in an unloaded shared
6237 library. Only apply to enabled breakpoints, disabled ones can just stay
4a64f543 6238 disabled. */
84acb35a 6239
75149521 6240static void
84acb35a
JJ
6241disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6242{
876fa593 6243 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6244 int disabled_shlib_breaks = 0;
6245
c86cf029
VP
6246 /* SunOS a.out shared libraries are always mapped, so do not
6247 disable breakpoints; they will only be reported as unloaded
6248 through clear_solib when GDB discards its shared library
6249 list. See clear_solib for more information. */
6250 if (exec_bfd != NULL
6251 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6252 return;
6253
876fa593 6254 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6255 {
2bdf28a0 6256 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6257 struct breakpoint *b = loc->owner;
cc59ec59 6258
1e4d1764 6259 if (solib->pspace == loc->pspace
e2dd7057 6260 && !loc->shlib_disabled
1e4d1764
YQ
6261 && (((b->type == bp_breakpoint
6262 || b->type == bp_jit_event
6263 || b->type == bp_hardware_breakpoint)
6264 && (loc->loc_type == bp_loc_hardware_breakpoint
6265 || loc->loc_type == bp_loc_software_breakpoint))
6266 || is_tracepoint (b))
e2dd7057 6267 && solib_contains_address_p (solib, loc->address))
84acb35a 6268 {
e2dd7057
PP
6269 loc->shlib_disabled = 1;
6270 /* At this point, we cannot rely on remove_breakpoint
6271 succeeding so we must mark the breakpoint as not inserted
6272 to prevent future errors occurring in remove_breakpoints. */
6273 loc->inserted = 0;
8d3788bd
VP
6274
6275 /* This may cause duplicate notifications for the same breakpoint. */
6276 observer_notify_breakpoint_modified (b);
6277
e2dd7057
PP
6278 if (!disabled_shlib_breaks)
6279 {
6280 target_terminal_ours_for_output ();
3e43a32a
MS
6281 warning (_("Temporarily disabling breakpoints "
6282 "for unloaded shared library \"%s\""),
e2dd7057 6283 solib->so_name);
84acb35a 6284 }
e2dd7057 6285 disabled_shlib_breaks = 1;
84acb35a
JJ
6286 }
6287 }
84acb35a
JJ
6288}
6289
ce78b96d
JB
6290/* FORK & VFORK catchpoints. */
6291
e29a4733
PA
6292/* An instance of this type is used to represent a fork or vfork
6293 catchpoint. It includes a "struct breakpoint" as a kind of base
6294 class; users downcast to "struct breakpoint *" when needed. A
6295 breakpoint is really of this type iff its ops pointer points to
6296 CATCH_FORK_BREAKPOINT_OPS. */
6297
6298struct fork_catchpoint
6299{
6300 /* The base class. */
6301 struct breakpoint base;
6302
6303 /* Process id of a child process whose forking triggered this
6304 catchpoint. This field is only valid immediately after this
6305 catchpoint has triggered. */
6306 ptid_t forked_inferior_pid;
6307};
6308
4a64f543
MS
6309/* Implement the "insert" breakpoint_ops method for fork
6310 catchpoints. */
ce78b96d 6311
77b06cd7
TJB
6312static int
6313insert_catch_fork (struct bp_location *bl)
ce78b96d 6314{
77b06cd7 6315 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6316}
6317
4a64f543
MS
6318/* Implement the "remove" breakpoint_ops method for fork
6319 catchpoints. */
ce78b96d
JB
6320
6321static int
77b06cd7 6322remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6323{
6324 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6325}
6326
6327/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6328 catchpoints. */
6329
6330static int
f1310107 6331breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
6332 struct address_space *aspace, CORE_ADDR bp_addr,
6333 const struct target_waitstatus *ws)
ce78b96d 6334{
e29a4733
PA
6335 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6336
f90263c1
TT
6337 if (ws->kind != TARGET_WAITKIND_FORKED)
6338 return 0;
6339
6340 c->forked_inferior_pid = ws->value.related_pid;
6341 return 1;
ce78b96d
JB
6342}
6343
4a64f543
MS
6344/* Implement the "print_it" breakpoint_ops method for fork
6345 catchpoints. */
ce78b96d
JB
6346
6347static enum print_stop_action
348d480f 6348print_it_catch_fork (bpstat bs)
ce78b96d 6349{
36dfb11c 6350 struct ui_out *uiout = current_uiout;
348d480f
PA
6351 struct breakpoint *b = bs->breakpoint_at;
6352 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 6353
ce78b96d 6354 annotate_catchpoint (b->number);
36dfb11c
TT
6355 if (b->disposition == disp_del)
6356 ui_out_text (uiout, "\nTemporary catchpoint ");
6357 else
6358 ui_out_text (uiout, "\nCatchpoint ");
6359 if (ui_out_is_mi_like_p (uiout))
6360 {
6361 ui_out_field_string (uiout, "reason",
6362 async_reason_lookup (EXEC_ASYNC_FORK));
6363 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6364 }
6365 ui_out_field_int (uiout, "bkptno", b->number);
6366 ui_out_text (uiout, " (forked process ");
6367 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6368 ui_out_text (uiout, "), ");
ce78b96d
JB
6369 return PRINT_SRC_AND_LOC;
6370}
6371
4a64f543
MS
6372/* Implement the "print_one" breakpoint_ops method for fork
6373 catchpoints. */
ce78b96d
JB
6374
6375static void
a6d9a66e 6376print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6377{
e29a4733 6378 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6379 struct value_print_options opts;
79a45e25 6380 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6381
6382 get_user_print_options (&opts);
6383
4a64f543
MS
6384 /* Field 4, the address, is omitted (which makes the columns not
6385 line up too nicely with the headers, but the effect is relatively
6386 readable). */
79a45b7d 6387 if (opts.addressprint)
ce78b96d
JB
6388 ui_out_field_skip (uiout, "addr");
6389 annotate_field (5);
6390 ui_out_text (uiout, "fork");
e29a4733 6391 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6392 {
6393 ui_out_text (uiout, ", process ");
6394 ui_out_field_int (uiout, "what",
e29a4733 6395 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6396 ui_out_spaces (uiout, 1);
6397 }
6398}
6399
6400/* Implement the "print_mention" breakpoint_ops method for fork
6401 catchpoints. */
6402
6403static void
6404print_mention_catch_fork (struct breakpoint *b)
6405{
6406 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6407}
6408
6149aea9
PA
6409/* Implement the "print_recreate" breakpoint_ops method for fork
6410 catchpoints. */
6411
6412static void
6413print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6414{
6415 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 6416 print_recreate_thread (b, fp);
6149aea9
PA
6417}
6418
ce78b96d
JB
6419/* The breakpoint_ops structure to be used in fork catchpoints. */
6420
2060206e 6421static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 6422
4a64f543
MS
6423/* Implement the "insert" breakpoint_ops method for vfork
6424 catchpoints. */
ce78b96d 6425
77b06cd7
TJB
6426static int
6427insert_catch_vfork (struct bp_location *bl)
ce78b96d 6428{
77b06cd7 6429 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6430}
6431
4a64f543
MS
6432/* Implement the "remove" breakpoint_ops method for vfork
6433 catchpoints. */
ce78b96d
JB
6434
6435static int
77b06cd7 6436remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6437{
6438 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6439}
6440
6441/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6442 catchpoints. */
6443
6444static int
f1310107 6445breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
6446 struct address_space *aspace, CORE_ADDR bp_addr,
6447 const struct target_waitstatus *ws)
ce78b96d 6448{
e29a4733
PA
6449 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6450
f90263c1
TT
6451 if (ws->kind != TARGET_WAITKIND_VFORKED)
6452 return 0;
6453
6454 c->forked_inferior_pid = ws->value.related_pid;
6455 return 1;
ce78b96d
JB
6456}
6457
4a64f543
MS
6458/* Implement the "print_it" breakpoint_ops method for vfork
6459 catchpoints. */
ce78b96d
JB
6460
6461static enum print_stop_action
348d480f 6462print_it_catch_vfork (bpstat bs)
ce78b96d 6463{
36dfb11c 6464 struct ui_out *uiout = current_uiout;
348d480f 6465 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
6466 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6467
ce78b96d 6468 annotate_catchpoint (b->number);
36dfb11c
TT
6469 if (b->disposition == disp_del)
6470 ui_out_text (uiout, "\nTemporary catchpoint ");
6471 else
6472 ui_out_text (uiout, "\nCatchpoint ");
6473 if (ui_out_is_mi_like_p (uiout))
6474 {
6475 ui_out_field_string (uiout, "reason",
6476 async_reason_lookup (EXEC_ASYNC_VFORK));
6477 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6478 }
6479 ui_out_field_int (uiout, "bkptno", b->number);
6480 ui_out_text (uiout, " (vforked process ");
6481 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6482 ui_out_text (uiout, "), ");
ce78b96d
JB
6483 return PRINT_SRC_AND_LOC;
6484}
6485
4a64f543
MS
6486/* Implement the "print_one" breakpoint_ops method for vfork
6487 catchpoints. */
ce78b96d
JB
6488
6489static void
a6d9a66e 6490print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6491{
e29a4733 6492 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6493 struct value_print_options opts;
79a45e25 6494 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6495
6496 get_user_print_options (&opts);
4a64f543
MS
6497 /* Field 4, the address, is omitted (which makes the columns not
6498 line up too nicely with the headers, but the effect is relatively
6499 readable). */
79a45b7d 6500 if (opts.addressprint)
ce78b96d
JB
6501 ui_out_field_skip (uiout, "addr");
6502 annotate_field (5);
6503 ui_out_text (uiout, "vfork");
e29a4733 6504 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6505 {
6506 ui_out_text (uiout, ", process ");
6507 ui_out_field_int (uiout, "what",
e29a4733 6508 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6509 ui_out_spaces (uiout, 1);
6510 }
6511}
6512
6513/* Implement the "print_mention" breakpoint_ops method for vfork
6514 catchpoints. */
6515
6516static void
6517print_mention_catch_vfork (struct breakpoint *b)
6518{
6519 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6520}
6521
6149aea9
PA
6522/* Implement the "print_recreate" breakpoint_ops method for vfork
6523 catchpoints. */
6524
6525static void
6526print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6527{
6528 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 6529 print_recreate_thread (b, fp);
6149aea9
PA
6530}
6531
ce78b96d
JB
6532/* The breakpoint_ops structure to be used in vfork catchpoints. */
6533
2060206e 6534static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 6535
edcc5120
TT
6536/* An instance of this type is used to represent an solib catchpoint.
6537 It includes a "struct breakpoint" as a kind of base class; users
6538 downcast to "struct breakpoint *" when needed. A breakpoint is
6539 really of this type iff its ops pointer points to
6540 CATCH_SOLIB_BREAKPOINT_OPS. */
6541
6542struct solib_catchpoint
6543{
6544 /* The base class. */
6545 struct breakpoint base;
6546
6547 /* True for "catch load", false for "catch unload". */
6548 unsigned char is_load;
6549
6550 /* Regular expression to match, if any. COMPILED is only valid when
6551 REGEX is non-NULL. */
6552 char *regex;
6553 regex_t compiled;
6554};
6555
6556static void
6557dtor_catch_solib (struct breakpoint *b)
6558{
6559 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
6560
6561 if (self->regex)
6562 regfree (&self->compiled);
6563 xfree (self->regex);
6564
6565 base_breakpoint_ops.dtor (b);
6566}
6567
6568static int
6569insert_catch_solib (struct bp_location *ignore)
6570{
6571 return 0;
6572}
6573
6574static int
6575remove_catch_solib (struct bp_location *ignore)
6576{
6577 return 0;
6578}
6579
6580static int
6581breakpoint_hit_catch_solib (const struct bp_location *bl,
6582 struct address_space *aspace,
6583 CORE_ADDR bp_addr,
6584 const struct target_waitstatus *ws)
6585{
6586 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
6587 struct breakpoint *other;
6588
6589 if (ws->kind == TARGET_WAITKIND_LOADED)
6590 return 1;
6591
6592 ALL_BREAKPOINTS (other)
6593 {
6594 struct bp_location *other_bl;
6595
6596 if (other == bl->owner)
6597 continue;
6598
6599 if (other->type != bp_shlib_event)
6600 continue;
6601
6602 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
6603 continue;
6604
6605 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
6606 {
6607 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
6608 return 1;
6609 }
6610 }
6611
6612 return 0;
6613}
6614
6615static void
6616check_status_catch_solib (struct bpstats *bs)
6617{
6618 struct solib_catchpoint *self
6619 = (struct solib_catchpoint *) bs->breakpoint_at;
6620 int ix;
6621
6622 if (self->is_load)
6623 {
6624 struct so_list *iter;
6625
6626 for (ix = 0;
6627 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
6628 ix, iter);
6629 ++ix)
6630 {
6631 if (!self->regex
6632 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
6633 return;
6634 }
6635 }
6636 else
6637 {
6638 char *iter;
6639
6640 for (ix = 0;
6641 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
6642 ix, iter);
6643 ++ix)
6644 {
6645 if (!self->regex
6646 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
6647 return;
6648 }
6649 }
6650
6651 bs->stop = 0;
6652 bs->print_it = print_it_noop;
6653}
6654
6655static enum print_stop_action
6656print_it_catch_solib (bpstat bs)
6657{
6658 struct breakpoint *b = bs->breakpoint_at;
6659 struct ui_out *uiout = current_uiout;
6660
6661 annotate_catchpoint (b->number);
6662 if (b->disposition == disp_del)
6663 ui_out_text (uiout, "\nTemporary catchpoint ");
6664 else
6665 ui_out_text (uiout, "\nCatchpoint ");
6666 ui_out_field_int (uiout, "bkptno", b->number);
6667 ui_out_text (uiout, "\n");
6668 if (ui_out_is_mi_like_p (uiout))
6669 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6670 print_solib_event (1);
6671 return PRINT_SRC_AND_LOC;
6672}
6673
6674static void
6675print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
6676{
6677 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
6678 struct value_print_options opts;
6679 struct ui_out *uiout = current_uiout;
6680 char *msg;
6681
6682 get_user_print_options (&opts);
6683 /* Field 4, the address, is omitted (which makes the columns not
6684 line up too nicely with the headers, but the effect is relatively
6685 readable). */
6686 if (opts.addressprint)
6687 {
6688 annotate_field (4);
6689 ui_out_field_skip (uiout, "addr");
6690 }
6691
6692 annotate_field (5);
6693 if (self->is_load)
6694 {
6695 if (self->regex)
6696 msg = xstrprintf (_("load of library matching %s"), self->regex);
6697 else
6698 msg = xstrdup (_("load of library"));
6699 }
6700 else
6701 {
6702 if (self->regex)
6703 msg = xstrprintf (_("unload of library matching %s"), self->regex);
6704 else
6705 msg = xstrdup (_("unload of library"));
6706 }
6707 ui_out_field_string (uiout, "what", msg);
6708 xfree (msg);
6709}
6710
6711static void
6712print_mention_catch_solib (struct breakpoint *b)
6713{
6714 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
6715
6716 printf_filtered (_("Catchpoint %d (%s)"), b->number,
6717 self->is_load ? "load" : "unload");
6718}
6719
6720static void
6721print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
6722{
6723 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
6724
6725 fprintf_unfiltered (fp, "%s %s",
6726 b->disposition == disp_del ? "tcatch" : "catch",
6727 self->is_load ? "load" : "unload");
6728 if (self->regex)
6729 fprintf_unfiltered (fp, " %s", self->regex);
6730 fprintf_unfiltered (fp, "\n");
6731}
6732
6733static struct breakpoint_ops catch_solib_breakpoint_ops;
6734
6735/* A helper function that does all the work for "catch load" and
6736 "catch unload". */
6737
6738static void
6739catch_load_or_unload (char *arg, int from_tty, int is_load,
6740 struct cmd_list_element *command)
6741{
6742 struct solib_catchpoint *c;
6743 struct gdbarch *gdbarch = get_current_arch ();
6744 int tempflag;
6745 regex_t compiled;
6746 struct cleanup *cleanup;
6747
6748 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6749
6750 if (!arg)
6751 arg = "";
6752 arg = skip_spaces (arg);
6753
6754 c = XCNEW (struct solib_catchpoint);
6755 cleanup = make_cleanup (xfree, c);
6756
6757 if (*arg != '\0')
6758 {
6759 int errcode;
6760
6761 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
6762 if (errcode != 0)
6763 {
6764 char *err = get_regcomp_error (errcode, &c->compiled);
6765
6766 make_cleanup (xfree, err);
6767 error (_("Invalid regexp (%s): %s"), err, arg);
6768 }
6769 c->regex = xstrdup (arg);
6770 }
6771
6772 c->is_load = is_load;
6773 init_catchpoint (&c->base, gdbarch, tempflag, NULL,
6774 &catch_solib_breakpoint_ops);
6775
6776 discard_cleanups (cleanup);
6777 install_breakpoint (0, &c->base, 1);
6778}
6779
6780static void
6781catch_load_command_1 (char *arg, int from_tty,
6782 struct cmd_list_element *command)
6783{
6784 catch_load_or_unload (arg, from_tty, 1, command);
6785}
6786
6787static void
6788catch_unload_command_1 (char *arg, int from_tty,
6789 struct cmd_list_element *command)
6790{
6791 catch_load_or_unload (arg, from_tty, 0, command);
6792}
6793
be5c67c1
PA
6794/* An instance of this type is used to represent a syscall catchpoint.
6795 It includes a "struct breakpoint" as a kind of base class; users
6796 downcast to "struct breakpoint *" when needed. A breakpoint is
6797 really of this type iff its ops pointer points to
6798 CATCH_SYSCALL_BREAKPOINT_OPS. */
6799
6800struct syscall_catchpoint
6801{
6802 /* The base class. */
6803 struct breakpoint base;
6804
6805 /* Syscall numbers used for the 'catch syscall' feature. If no
6806 syscall has been specified for filtering, its value is NULL.
6807 Otherwise, it holds a list of all syscalls to be caught. The
6808 list elements are allocated with xmalloc. */
6809 VEC(int) *syscalls_to_be_caught;
6810};
6811
6812/* Implement the "dtor" breakpoint_ops method for syscall
6813 catchpoints. */
6814
6815static void
6816dtor_catch_syscall (struct breakpoint *b)
6817{
6818 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6819
6820 VEC_free (int, c->syscalls_to_be_caught);
348d480f 6821
2060206e 6822 base_breakpoint_ops.dtor (b);
be5c67c1
PA
6823}
6824
a96d9b2e
SDJ
6825/* Implement the "insert" breakpoint_ops method for syscall
6826 catchpoints. */
6827
77b06cd7
TJB
6828static int
6829insert_catch_syscall (struct bp_location *bl)
a96d9b2e 6830{
be5c67c1 6831 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6832 struct inferior *inf = current_inferior ();
6833
6834 ++inf->total_syscalls_count;
be5c67c1 6835 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6836 ++inf->any_syscall_count;
6837 else
6838 {
6839 int i, iter;
cc59ec59 6840
a96d9b2e 6841 for (i = 0;
be5c67c1 6842 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6843 i++)
6844 {
6845 int elem;
cc59ec59 6846
a96d9b2e
SDJ
6847 if (iter >= VEC_length (int, inf->syscalls_counts))
6848 {
6849 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6850 uintptr_t vec_addr_offset
6851 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6852 uintptr_t vec_addr;
6853 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6854 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6855 vec_addr_offset;
6856 memset ((void *) vec_addr, 0,
6857 (iter + 1 - old_size) * sizeof (int));
6858 }
6859 elem = VEC_index (int, inf->syscalls_counts, iter);
6860 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6861 }
6862 }
6863
77b06cd7
TJB
6864 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6865 inf->total_syscalls_count != 0,
6866 inf->any_syscall_count,
6867 VEC_length (int, inf->syscalls_counts),
6868 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6869}
6870
6871/* Implement the "remove" breakpoint_ops method for syscall
6872 catchpoints. */
6873
6874static int
77b06cd7 6875remove_catch_syscall (struct bp_location *bl)
a96d9b2e 6876{
be5c67c1 6877 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6878 struct inferior *inf = current_inferior ();
6879
6880 --inf->total_syscalls_count;
be5c67c1 6881 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6882 --inf->any_syscall_count;
6883 else
6884 {
6885 int i, iter;
cc59ec59 6886
a96d9b2e 6887 for (i = 0;
be5c67c1 6888 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6889 i++)
6890 {
6891 int elem;
6892 if (iter >= VEC_length (int, inf->syscalls_counts))
6893 /* Shouldn't happen. */
6894 continue;
6895 elem = VEC_index (int, inf->syscalls_counts, iter);
6896 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6897 }
6898 }
6899
6900 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6901 inf->total_syscalls_count != 0,
6902 inf->any_syscall_count,
6903 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6904 VEC_address (int,
6905 inf->syscalls_counts));
a96d9b2e
SDJ
6906}
6907
6908/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6909 catchpoints. */
6910
6911static int
f1310107 6912breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
6913 struct address_space *aspace, CORE_ADDR bp_addr,
6914 const struct target_waitstatus *ws)
a96d9b2e 6915{
4a64f543
MS
6916 /* We must check if we are catching specific syscalls in this
6917 breakpoint. If we are, then we must guarantee that the called
6918 syscall is the same syscall we are catching. */
a96d9b2e 6919 int syscall_number = 0;
be5c67c1
PA
6920 const struct syscall_catchpoint *c
6921 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 6922
f90263c1
TT
6923 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
6924 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
6925 return 0;
6926
f90263c1
TT
6927 syscall_number = ws->value.syscall_number;
6928
a96d9b2e 6929 /* Now, checking if the syscall is the same. */
be5c67c1 6930 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6931 {
6932 int i, iter;
cc59ec59 6933
a96d9b2e 6934 for (i = 0;
be5c67c1 6935 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6936 i++)
6937 if (syscall_number == iter)
6938 break;
6939 /* Not the same. */
6940 if (!iter)
6941 return 0;
6942 }
6943
6944 return 1;
6945}
6946
6947/* Implement the "print_it" breakpoint_ops method for syscall
6948 catchpoints. */
6949
6950static enum print_stop_action
348d480f 6951print_it_catch_syscall (bpstat bs)
a96d9b2e 6952{
36dfb11c 6953 struct ui_out *uiout = current_uiout;
348d480f 6954 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
6955 /* These are needed because we want to know in which state a
6956 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6957 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6958 must print "called syscall" or "returned from syscall". */
6959 ptid_t ptid;
6960 struct target_waitstatus last;
6961 struct syscall s;
a96d9b2e
SDJ
6962 char *syscall_id;
6963
6964 get_last_target_status (&ptid, &last);
6965
6966 get_syscall_by_number (last.value.syscall_number, &s);
6967
6968 annotate_catchpoint (b->number);
6969
36dfb11c
TT
6970 if (b->disposition == disp_del)
6971 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 6972 else
36dfb11c
TT
6973 ui_out_text (uiout, "\nCatchpoint ");
6974 if (ui_out_is_mi_like_p (uiout))
6975 {
6976 ui_out_field_string (uiout, "reason",
6977 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
6978 ? EXEC_ASYNC_SYSCALL_ENTRY
6979 : EXEC_ASYNC_SYSCALL_RETURN));
6980 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6981 }
6982 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
6983
6984 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
6985 ui_out_text (uiout, " (call to syscall ");
6986 else
6987 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 6988
36dfb11c
TT
6989 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
6990 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
6991 if (s.name != NULL)
6992 ui_out_field_string (uiout, "syscall-name", s.name);
6993
6994 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
6995
6996 return PRINT_SRC_AND_LOC;
6997}
6998
6999/* Implement the "print_one" breakpoint_ops method for syscall
7000 catchpoints. */
7001
7002static void
7003print_one_catch_syscall (struct breakpoint *b,
f1310107 7004 struct bp_location **last_loc)
a96d9b2e 7005{
be5c67c1 7006 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 7007 struct value_print_options opts;
79a45e25 7008 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
7009
7010 get_user_print_options (&opts);
4a64f543
MS
7011 /* Field 4, the address, is omitted (which makes the columns not
7012 line up too nicely with the headers, but the effect is relatively
7013 readable). */
a96d9b2e
SDJ
7014 if (opts.addressprint)
7015 ui_out_field_skip (uiout, "addr");
7016 annotate_field (5);
7017
be5c67c1
PA
7018 if (c->syscalls_to_be_caught
7019 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
7020 ui_out_text (uiout, "syscalls \"");
7021 else
7022 ui_out_text (uiout, "syscall \"");
7023
be5c67c1 7024 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7025 {
7026 int i, iter;
7027 char *text = xstrprintf ("%s", "");
cc59ec59 7028
a96d9b2e 7029 for (i = 0;
be5c67c1 7030 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7031 i++)
7032 {
7033 char *x = text;
7034 struct syscall s;
7035 get_syscall_by_number (iter, &s);
7036
7037 if (s.name != NULL)
7038 text = xstrprintf ("%s%s, ", text, s.name);
7039 else
7040 text = xstrprintf ("%s%d, ", text, iter);
7041
7042 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 7043 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
7044 on every call. */
7045 xfree (x);
7046 }
7047 /* Remove the last comma. */
7048 text[strlen (text) - 2] = '\0';
7049 ui_out_field_string (uiout, "what", text);
7050 }
7051 else
7052 ui_out_field_string (uiout, "what", "<any syscall>");
7053 ui_out_text (uiout, "\" ");
7054}
7055
7056/* Implement the "print_mention" breakpoint_ops method for syscall
7057 catchpoints. */
7058
7059static void
7060print_mention_catch_syscall (struct breakpoint *b)
7061{
be5c67c1
PA
7062 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7063
7064 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7065 {
7066 int i, iter;
7067
be5c67c1 7068 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
7069 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
7070 else
7071 printf_filtered (_("Catchpoint %d (syscall"), b->number);
7072
7073 for (i = 0;
be5c67c1 7074 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7075 i++)
7076 {
7077 struct syscall s;
7078 get_syscall_by_number (iter, &s);
7079
7080 if (s.name)
7081 printf_filtered (" '%s' [%d]", s.name, s.number);
7082 else
7083 printf_filtered (" %d", s.number);
7084 }
7085 printf_filtered (")");
7086 }
7087 else
7088 printf_filtered (_("Catchpoint %d (any syscall)"),
7089 b->number);
7090}
7091
6149aea9
PA
7092/* Implement the "print_recreate" breakpoint_ops method for syscall
7093 catchpoints. */
7094
7095static void
7096print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
7097{
be5c67c1
PA
7098 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7099
6149aea9
PA
7100 fprintf_unfiltered (fp, "catch syscall");
7101
be5c67c1 7102 if (c->syscalls_to_be_caught)
6149aea9
PA
7103 {
7104 int i, iter;
7105
7106 for (i = 0;
be5c67c1 7107 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
7108 i++)
7109 {
7110 struct syscall s;
7111
7112 get_syscall_by_number (iter, &s);
7113 if (s.name)
7114 fprintf_unfiltered (fp, " %s", s.name);
7115 else
7116 fprintf_unfiltered (fp, " %d", s.number);
7117 }
7118 }
d9b3f62e 7119 print_recreate_thread (b, fp);
6149aea9
PA
7120}
7121
a96d9b2e
SDJ
7122/* The breakpoint_ops structure to be used in syscall catchpoints. */
7123
2060206e 7124static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
7125
7126/* Returns non-zero if 'b' is a syscall catchpoint. */
7127
7128static int
7129syscall_catchpoint_p (struct breakpoint *b)
7130{
7131 return (b->ops == &catch_syscall_breakpoint_ops);
7132}
7133
346774a9
PA
7134/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
7135 is non-zero, then make the breakpoint temporary. If COND_STRING is
7136 not NULL, then store it in the breakpoint. OPS, if not NULL, is
7137 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 7138
346774a9
PA
7139static void
7140init_catchpoint (struct breakpoint *b,
7141 struct gdbarch *gdbarch, int tempflag,
7142 char *cond_string,
c0a91b2b 7143 const struct breakpoint_ops *ops)
c906108c 7144{
c5aa993b 7145 struct symtab_and_line sal;
346774a9 7146
fe39c653 7147 init_sal (&sal);
6c95b8df 7148 sal.pspace = current_program_space;
c5aa993b 7149
28010a5d 7150 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 7151
1b36a34b 7152 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 7153 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
7154}
7155
28010a5d 7156void
3ea46bff 7157install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
7158{
7159 add_to_breakpoint_chain (b);
3a5c3e22
PA
7160 set_breakpoint_number (internal, b);
7161 if (!internal)
7162 mention (b);
c56053d2 7163 observer_notify_breakpoint_created (b);
3ea46bff
YQ
7164
7165 if (update_gll)
7166 update_global_location_list (1);
c56053d2
PA
7167}
7168
9b70b993 7169static void
a6d9a66e
UW
7170create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
7171 int tempflag, char *cond_string,
c0a91b2b 7172 const struct breakpoint_ops *ops)
c906108c 7173{
e29a4733 7174 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 7175
e29a4733
PA
7176 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
7177
7178 c->forked_inferior_pid = null_ptid;
7179
3ea46bff 7180 install_breakpoint (0, &c->base, 1);
c906108c
SS
7181}
7182
fe798b75
JB
7183/* Exec catchpoints. */
7184
b4d90040
PA
7185/* An instance of this type is used to represent an exec catchpoint.
7186 It includes a "struct breakpoint" as a kind of base class; users
7187 downcast to "struct breakpoint *" when needed. A breakpoint is
7188 really of this type iff its ops pointer points to
7189 CATCH_EXEC_BREAKPOINT_OPS. */
7190
7191struct exec_catchpoint
7192{
7193 /* The base class. */
7194 struct breakpoint base;
7195
7196 /* Filename of a program whose exec triggered this catchpoint.
7197 This field is only valid immediately after this catchpoint has
7198 triggered. */
7199 char *exec_pathname;
7200};
7201
7202/* Implement the "dtor" breakpoint_ops method for exec
7203 catchpoints. */
7204
7205static void
7206dtor_catch_exec (struct breakpoint *b)
7207{
7208 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
7209
7210 xfree (c->exec_pathname);
348d480f 7211
2060206e 7212 base_breakpoint_ops.dtor (b);
b4d90040
PA
7213}
7214
77b06cd7
TJB
7215static int
7216insert_catch_exec (struct bp_location *bl)
c906108c 7217{
77b06cd7 7218 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 7219}
c906108c 7220
fe798b75 7221static int
77b06cd7 7222remove_catch_exec (struct bp_location *bl)
fe798b75
JB
7223{
7224 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
7225}
c906108c 7226
fe798b75 7227static int
f1310107 7228breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
7229 struct address_space *aspace, CORE_ADDR bp_addr,
7230 const struct target_waitstatus *ws)
fe798b75 7231{
b4d90040
PA
7232 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
7233
f90263c1
TT
7234 if (ws->kind != TARGET_WAITKIND_EXECD)
7235 return 0;
7236
7237 c->exec_pathname = xstrdup (ws->value.execd_pathname);
7238 return 1;
fe798b75 7239}
c906108c 7240
fe798b75 7241static enum print_stop_action
348d480f 7242print_it_catch_exec (bpstat bs)
fe798b75 7243{
36dfb11c 7244 struct ui_out *uiout = current_uiout;
348d480f 7245 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
7246 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
7247
fe798b75 7248 annotate_catchpoint (b->number);
36dfb11c
TT
7249 if (b->disposition == disp_del)
7250 ui_out_text (uiout, "\nTemporary catchpoint ");
7251 else
7252 ui_out_text (uiout, "\nCatchpoint ");
7253 if (ui_out_is_mi_like_p (uiout))
7254 {
7255 ui_out_field_string (uiout, "reason",
7256 async_reason_lookup (EXEC_ASYNC_EXEC));
7257 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7258 }
7259 ui_out_field_int (uiout, "bkptno", b->number);
7260 ui_out_text (uiout, " (exec'd ");
7261 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
7262 ui_out_text (uiout, "), ");
7263
fe798b75 7264 return PRINT_SRC_AND_LOC;
c906108c
SS
7265}
7266
fe798b75 7267static void
a6d9a66e 7268print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 7269{
b4d90040 7270 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 7271 struct value_print_options opts;
79a45e25 7272 struct ui_out *uiout = current_uiout;
fe798b75
JB
7273
7274 get_user_print_options (&opts);
7275
7276 /* Field 4, the address, is omitted (which makes the columns
7277 not line up too nicely with the headers, but the effect
7278 is relatively readable). */
7279 if (opts.addressprint)
7280 ui_out_field_skip (uiout, "addr");
7281 annotate_field (5);
7282 ui_out_text (uiout, "exec");
b4d90040 7283 if (c->exec_pathname != NULL)
fe798b75
JB
7284 {
7285 ui_out_text (uiout, ", program \"");
b4d90040 7286 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
7287 ui_out_text (uiout, "\" ");
7288 }
7289}
7290
7291static void
7292print_mention_catch_exec (struct breakpoint *b)
7293{
7294 printf_filtered (_("Catchpoint %d (exec)"), b->number);
7295}
7296
6149aea9
PA
7297/* Implement the "print_recreate" breakpoint_ops method for exec
7298 catchpoints. */
7299
7300static void
7301print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
7302{
7303 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 7304 print_recreate_thread (b, fp);
6149aea9
PA
7305}
7306
2060206e 7307static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 7308
a96d9b2e
SDJ
7309static void
7310create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 7311 const struct breakpoint_ops *ops)
a96d9b2e 7312{
be5c67c1 7313 struct syscall_catchpoint *c;
a96d9b2e 7314 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 7315
be5c67c1
PA
7316 c = XNEW (struct syscall_catchpoint);
7317 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
7318 c->syscalls_to_be_caught = filter;
a96d9b2e 7319
3ea46bff 7320 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
7321}
7322
c906108c 7323static int
fba45db2 7324hw_breakpoint_used_count (void)
c906108c 7325{
c906108c 7326 int i = 0;
f1310107
TJB
7327 struct breakpoint *b;
7328 struct bp_location *bl;
c906108c
SS
7329
7330 ALL_BREAKPOINTS (b)
c5aa993b 7331 {
d6b74ac4 7332 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
7333 for (bl = b->loc; bl; bl = bl->next)
7334 {
7335 /* Special types of hardware breakpoints may use more than
7336 one register. */
348d480f 7337 i += b->ops->resources_needed (bl);
f1310107 7338 }
c5aa993b 7339 }
c906108c
SS
7340
7341 return i;
7342}
7343
a1398e0c
PA
7344/* Returns the resources B would use if it were a hardware
7345 watchpoint. */
7346
c906108c 7347static int
a1398e0c 7348hw_watchpoint_use_count (struct breakpoint *b)
c906108c 7349{
c906108c 7350 int i = 0;
e09342b5 7351 struct bp_location *bl;
c906108c 7352
a1398e0c
PA
7353 if (!breakpoint_enabled (b))
7354 return 0;
7355
7356 for (bl = b->loc; bl; bl = bl->next)
7357 {
7358 /* Special types of hardware watchpoints may use more than
7359 one register. */
7360 i += b->ops->resources_needed (bl);
7361 }
7362
7363 return i;
7364}
7365
7366/* Returns the sum the used resources of all hardware watchpoints of
7367 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
7368 the sum of the used resources of all hardware watchpoints of other
7369 types _not_ TYPE. */
7370
7371static int
7372hw_watchpoint_used_count_others (struct breakpoint *except,
7373 enum bptype type, int *other_type_used)
7374{
7375 int i = 0;
7376 struct breakpoint *b;
7377
c906108c
SS
7378 *other_type_used = 0;
7379 ALL_BREAKPOINTS (b)
e09342b5 7380 {
a1398e0c
PA
7381 if (b == except)
7382 continue;
e09342b5
TJB
7383 if (!breakpoint_enabled (b))
7384 continue;
7385
a1398e0c
PA
7386 if (b->type == type)
7387 i += hw_watchpoint_use_count (b);
7388 else if (is_hardware_watchpoint (b))
7389 *other_type_used = 1;
e09342b5
TJB
7390 }
7391
c906108c
SS
7392 return i;
7393}
7394
c906108c 7395void
fba45db2 7396disable_watchpoints_before_interactive_call_start (void)
c906108c 7397{
c5aa993b 7398 struct breakpoint *b;
c906108c
SS
7399
7400 ALL_BREAKPOINTS (b)
c5aa993b 7401 {
cc60f2e3 7402 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 7403 {
b5de0fa7 7404 b->enable_state = bp_call_disabled;
b60e7edf 7405 update_global_location_list (0);
c5aa993b
JM
7406 }
7407 }
c906108c
SS
7408}
7409
7410void
fba45db2 7411enable_watchpoints_after_interactive_call_stop (void)
c906108c 7412{
c5aa993b 7413 struct breakpoint *b;
c906108c
SS
7414
7415 ALL_BREAKPOINTS (b)
c5aa993b 7416 {
cc60f2e3 7417 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 7418 {
b5de0fa7 7419 b->enable_state = bp_enabled;
b60e7edf 7420 update_global_location_list (1);
c5aa993b
JM
7421 }
7422 }
c906108c
SS
7423}
7424
8bea4e01
UW
7425void
7426disable_breakpoints_before_startup (void)
7427{
6c95b8df 7428 current_program_space->executing_startup = 1;
f8eba3c6 7429 update_global_location_list (0);
8bea4e01
UW
7430}
7431
7432void
7433enable_breakpoints_after_startup (void)
7434{
6c95b8df 7435 current_program_space->executing_startup = 0;
f8eba3c6 7436 breakpoint_re_set ();
8bea4e01
UW
7437}
7438
c906108c
SS
7439
7440/* Set a breakpoint that will evaporate an end of command
7441 at address specified by SAL.
7442 Restrict it to frame FRAME if FRAME is nonzero. */
7443
7444struct breakpoint *
a6d9a66e
UW
7445set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7446 struct frame_id frame_id, enum bptype type)
c906108c 7447{
52f0bd74 7448 struct breakpoint *b;
edb3359d
DJ
7449
7450 /* If FRAME_ID is valid, it should be a real frame, not an inlined
7451 one. */
7452 gdb_assert (!frame_id_inlined_p (frame_id));
7453
06edf0c0 7454 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
7455 b->enable_state = bp_enabled;
7456 b->disposition = disp_donttouch;
818dd999 7457 b->frame_id = frame_id;
c906108c 7458
4a64f543
MS
7459 /* If we're debugging a multi-threaded program, then we want
7460 momentary breakpoints to be active in only a single thread of
7461 control. */
39f77062
KB
7462 if (in_thread_list (inferior_ptid))
7463 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 7464
b60e7edf 7465 update_global_location_list_nothrow (1);
74960c60 7466
c906108c
SS
7467 return b;
7468}
611c83ae 7469
06edf0c0
PA
7470/* Make a momentary breakpoint based on the master breakpoint ORIG.
7471 The new breakpoint will have type TYPE, and use OPS as it
7472 breakpoint_ops. */
e58b0e63 7473
06edf0c0
PA
7474static struct breakpoint *
7475momentary_breakpoint_from_master (struct breakpoint *orig,
7476 enum bptype type,
c0a91b2b 7477 const struct breakpoint_ops *ops)
e58b0e63
PA
7478{
7479 struct breakpoint *copy;
7480
06edf0c0 7481 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 7482 copy->loc = allocate_bp_location (copy);
0e30163f 7483 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 7484
a6d9a66e 7485 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7486 copy->loc->requested_address = orig->loc->requested_address;
7487 copy->loc->address = orig->loc->address;
7488 copy->loc->section = orig->loc->section;
6c95b8df 7489 copy->loc->pspace = orig->loc->pspace;
e58b0e63 7490
f8eba3c6
TT
7491 if (orig->loc->source_file != NULL)
7492 copy->loc->source_file = xstrdup (orig->loc->source_file);
e58b0e63 7493
f8eba3c6 7494 copy->loc->line_number = orig->loc->line_number;
e58b0e63
PA
7495 copy->frame_id = orig->frame_id;
7496 copy->thread = orig->thread;
6c95b8df 7497 copy->pspace = orig->pspace;
e58b0e63
PA
7498
7499 copy->enable_state = bp_enabled;
7500 copy->disposition = disp_donttouch;
7501 copy->number = internal_breakpoint_number--;
7502
7503 update_global_location_list_nothrow (0);
7504 return copy;
7505}
7506
06edf0c0
PA
7507/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7508 ORIG is NULL. */
7509
7510struct breakpoint *
7511clone_momentary_breakpoint (struct breakpoint *orig)
7512{
7513 /* If there's nothing to clone, then return nothing. */
7514 if (orig == NULL)
7515 return NULL;
7516
7517 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
7518}
7519
611c83ae 7520struct breakpoint *
a6d9a66e
UW
7521set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7522 enum bptype type)
611c83ae
PA
7523{
7524 struct symtab_and_line sal;
7525
7526 sal = find_pc_line (pc, 0);
7527 sal.pc = pc;
7528 sal.section = find_pc_overlay (pc);
7529 sal.explicit_pc = 1;
7530
a6d9a66e 7531 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7532}
c906108c 7533\f
c5aa993b 7534
c906108c
SS
7535/* Tell the user we have just set a breakpoint B. */
7536
7537static void
fba45db2 7538mention (struct breakpoint *b)
c906108c 7539{
348d480f 7540 b->ops->print_mention (b);
79a45e25 7541 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 7542 return;
c906108c
SS
7543 printf_filtered ("\n");
7544}
c906108c 7545\f
c5aa993b 7546
0d381245 7547static struct bp_location *
39d61571 7548add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7549 const struct symtab_and_line *sal)
7550{
7551 struct bp_location *loc, **tmp;
3742cc8b
YQ
7552 CORE_ADDR adjusted_address;
7553 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
7554
7555 if (loc_gdbarch == NULL)
7556 loc_gdbarch = b->gdbarch;
7557
7558 /* Adjust the breakpoint's address prior to allocating a location.
7559 Once we call allocate_bp_location(), that mostly uninitialized
7560 location will be placed on the location chain. Adjustment of the
7561 breakpoint may cause target_read_memory() to be called and we do
7562 not want its scan of the location chain to find a breakpoint and
7563 location that's only been partially initialized. */
7564 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
7565 sal->pc, b->type);
0d381245 7566
39d61571 7567 loc = allocate_bp_location (b);
0d381245
VP
7568 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7569 ;
7570 *tmp = loc;
3742cc8b 7571
0d381245 7572 loc->requested_address = sal->pc;
3742cc8b 7573 loc->address = adjusted_address;
6c95b8df
PA
7574 loc->pspace = sal->pspace;
7575 gdb_assert (loc->pspace != NULL);
0d381245 7576 loc->section = sal->section;
3742cc8b 7577 loc->gdbarch = loc_gdbarch;
f8eba3c6
TT
7578
7579 if (sal->symtab != NULL)
7580 loc->source_file = xstrdup (sal->symtab->filename);
7581 loc->line_number = sal->line;
7582
0e30163f
JK
7583 set_breakpoint_location_function (loc,
7584 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7585 return loc;
7586}
514f746b
AR
7587\f
7588
7589/* Return 1 if LOC is pointing to a permanent breakpoint,
7590 return 0 otherwise. */
7591
7592static int
7593bp_loc_is_permanent (struct bp_location *loc)
7594{
7595 int len;
7596 CORE_ADDR addr;
1afeeb75 7597 const gdb_byte *bpoint;
514f746b 7598 gdb_byte *target_mem;
939c61fa
JK
7599 struct cleanup *cleanup;
7600 int retval = 0;
514f746b
AR
7601
7602 gdb_assert (loc != NULL);
7603
7604 addr = loc->address;
1afeeb75 7605 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7606
939c61fa 7607 /* Software breakpoints unsupported? */
1afeeb75 7608 if (bpoint == NULL)
939c61fa
JK
7609 return 0;
7610
514f746b
AR
7611 target_mem = alloca (len);
7612
939c61fa
JK
7613 /* Enable the automatic memory restoration from breakpoints while
7614 we read the memory. Otherwise we could say about our temporary
7615 breakpoints they are permanent. */
6c95b8df
PA
7616 cleanup = save_current_space_and_thread ();
7617
7618 switch_to_program_space_and_thread (loc->pspace);
7619 make_show_memory_breakpoints_cleanup (0);
939c61fa 7620
514f746b 7621 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 7622 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 7623 retval = 1;
514f746b 7624
939c61fa
JK
7625 do_cleanups (cleanup);
7626
7627 return retval;
514f746b
AR
7628}
7629
7630
c3f6f71d 7631
018d34a4
VP
7632/* Create a breakpoint with SAL as location. Use ADDR_STRING
7633 as textual description of the location, and COND_STRING
db107f19 7634 as condition expression. */
018d34a4
VP
7635
7636static void
d9b3f62e
PA
7637init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
7638 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 7639 char *filter, char *cond_string,
d9b3f62e
PA
7640 enum bptype type, enum bpdisp disposition,
7641 int thread, int task, int ignore_count,
c0a91b2b 7642 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e 7643 int enabled, int internal, int display_canonical)
018d34a4 7644{
0d381245 7645 int i;
018d34a4
VP
7646
7647 if (type == bp_hardware_breakpoint)
7648 {
fbbd034e
AS
7649 int target_resources_ok;
7650
7651 i = hw_breakpoint_used_count ();
7652 target_resources_ok =
7653 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7654 i + 1, 0);
7655 if (target_resources_ok == 0)
7656 error (_("No hardware breakpoint support in the target."));
7657 else if (target_resources_ok < 0)
7658 error (_("Hardware breakpoints used exceeds limit."));
7659 }
7660
6c95b8df
PA
7661 gdb_assert (sals.nelts > 0);
7662
0d381245
VP
7663 for (i = 0; i < sals.nelts; ++i)
7664 {
7665 struct symtab_and_line sal = sals.sals[i];
7666 struct bp_location *loc;
7667
7668 if (from_tty)
5af949e3
UW
7669 {
7670 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7671 if (!loc_gdbarch)
7672 loc_gdbarch = gdbarch;
7673
7674 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7675 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7676 }
0d381245
VP
7677
7678 if (i == 0)
7679 {
d9b3f62e 7680 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 7681 b->thread = thread;
4a306c9a 7682 b->task = task;
018d34a4 7683
0d381245
VP
7684 b->cond_string = cond_string;
7685 b->ignore_count = ignore_count;
41447f92 7686 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7687 b->disposition = disposition;
6c95b8df 7688
0fb4aa4b
PA
7689 if (type == bp_static_tracepoint)
7690 {
d9b3f62e 7691 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
7692 struct static_tracepoint_marker marker;
7693
983af33b 7694 if (strace_marker_p (b))
0fb4aa4b
PA
7695 {
7696 /* We already know the marker exists, otherwise, we
7697 wouldn't see a sal for it. */
7698 char *p = &addr_string[3];
7699 char *endp;
7700 char *marker_str;
0fb4aa4b 7701
e9cafbcc 7702 p = skip_spaces (p);
0fb4aa4b 7703
e9cafbcc 7704 endp = skip_to_space (p);
0fb4aa4b
PA
7705
7706 marker_str = savestring (p, endp - p);
d9b3f62e 7707 t->static_trace_marker_id = marker_str;
0fb4aa4b 7708
3e43a32a
MS
7709 printf_filtered (_("Probed static tracepoint "
7710 "marker \"%s\"\n"),
d9b3f62e 7711 t->static_trace_marker_id);
0fb4aa4b
PA
7712 }
7713 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7714 {
d9b3f62e 7715 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
7716 release_static_tracepoint_marker (&marker);
7717
3e43a32a
MS
7718 printf_filtered (_("Probed static tracepoint "
7719 "marker \"%s\"\n"),
d9b3f62e 7720 t->static_trace_marker_id);
0fb4aa4b
PA
7721 }
7722 else
3e43a32a
MS
7723 warning (_("Couldn't determine the static "
7724 "tracepoint marker to probe"));
0fb4aa4b
PA
7725 }
7726
0d381245
VP
7727 loc = b->loc;
7728 }
7729 else
018d34a4 7730 {
39d61571 7731 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7732 }
7733
514f746b
AR
7734 if (bp_loc_is_permanent (loc))
7735 make_breakpoint_permanent (b);
7736
0d381245
VP
7737 if (b->cond_string)
7738 {
7739 char *arg = b->cond_string;
d32a6982 7740 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7741 if (*arg)
db107f19 7742 error (_("Garbage %s follows condition"), arg);
018d34a4 7743 }
0d381245 7744 }
018d34a4 7745
56435ebe 7746 b->display_canonical = display_canonical;
018d34a4
VP
7747 if (addr_string)
7748 b->addr_string = addr_string;
7749 else
7750 /* addr_string has to be used or breakpoint_re_set will delete
7751 me. */
5af949e3
UW
7752 b->addr_string
7753 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 7754 b->filter = filter;
d9b3f62e 7755}
018d34a4 7756
d9b3f62e
PA
7757static void
7758create_breakpoint_sal (struct gdbarch *gdbarch,
7759 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 7760 char *filter, char *cond_string,
d9b3f62e
PA
7761 enum bptype type, enum bpdisp disposition,
7762 int thread, int task, int ignore_count,
c0a91b2b 7763 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e
PA
7764 int enabled, int internal, int display_canonical)
7765{
7766 struct breakpoint *b;
7767 struct cleanup *old_chain;
7768
7769 if (is_tracepoint_type (type))
7770 {
7771 struct tracepoint *t;
7772
7773 t = XCNEW (struct tracepoint);
7774 b = &t->base;
7775 }
7776 else
7777 b = XNEW (struct breakpoint);
7778
7779 old_chain = make_cleanup (xfree, b);
7780
7781 init_breakpoint_sal (b, gdbarch,
7782 sals, addr_string,
f8eba3c6 7783 filter, cond_string,
d9b3f62e
PA
7784 type, disposition,
7785 thread, task, ignore_count,
7786 ops, from_tty,
7787 enabled, internal, display_canonical);
7788 discard_cleanups (old_chain);
7789
3ea46bff 7790 install_breakpoint (internal, b, 0);
018d34a4
VP
7791}
7792
7793/* Add SALS.nelts breakpoints to the breakpoint table. For each
7794 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7795 value. COND_STRING, if not NULL, specified the condition to be
7796 used for all breakpoints. Essentially the only case where
7797 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7798 function. In that case, it's still not possible to specify
7799 separate conditions for different overloaded functions, so
7800 we take just a single condition string.
7801
c3f6f71d 7802 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7803 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7804 array contents). If the function fails (error() is called), the
7805 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7806 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7807
7808static void
8cdf0e15 7809create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 7810 struct linespec_result *canonical,
8cdf0e15
VP
7811 char *cond_string,
7812 enum bptype type, enum bpdisp disposition,
7813 int thread, int task, int ignore_count,
c0a91b2b 7814 const struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7815 int enabled, int internal)
c906108c 7816{
018d34a4 7817 int i;
f8eba3c6 7818 struct linespec_sals *lsal;
cc59ec59 7819
f8eba3c6
TT
7820 if (canonical->pre_expanded)
7821 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
7822
7823 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 7824 {
f8eba3c6
TT
7825 /* Note that 'addr_string' can be NULL in the case of a plain
7826 'break', without arguments. */
7827 char *addr_string = (canonical->addr_string
7828 ? xstrdup (canonical->addr_string)
7829 : NULL);
7830 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
7831 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 7832
f8eba3c6
TT
7833 make_cleanup (xfree, filter_string);
7834 create_breakpoint_sal (gdbarch, lsal->sals,
7835 addr_string,
7836 filter_string,
8cdf0e15 7837 cond_string, type, disposition,
84f4c1fe 7838 thread, task, ignore_count, ops,
56435ebe
TT
7839 from_tty, enabled, internal,
7840 canonical->special_display);
f8eba3c6 7841 discard_cleanups (inner);
c3f6f71d 7842 }
c3f6f71d 7843}
c906108c 7844
9998af43 7845/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7846 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7847 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7848 address strings. ADDRESS points to the end of the SAL.
7849
7850 The array and the line spec strings are allocated on the heap, it is
7851 the caller's responsibility to free them. */
c906108c 7852
b9362cc7 7853static void
c3f6f71d 7854parse_breakpoint_sals (char **address,
58438ac1 7855 struct linespec_result *canonical)
c3f6f71d
JM
7856{
7857 char *addr_start = *address;
cc59ec59 7858
c3f6f71d 7859 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7860 breakpoint. */
c3f6f71d
JM
7861 if ((*address) == NULL
7862 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 7863 {
1bfeeb0f
JL
7864 /* The last displayed codepoint, if it's valid, is our default breakpoint
7865 address. */
7866 if (last_displayed_sal_is_valid ())
c906108c 7867 {
f8eba3c6 7868 struct linespec_sals lsal;
c3f6f71d 7869 struct symtab_and_line sal;
cc59ec59 7870
4a64f543 7871 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 7872 lsal.sals.sals = (struct symtab_and_line *)
c906108c 7873 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
7874
7875 /* Set sal's pspace, pc, symtab, and line to the values
7876 corresponding to the last call to print_frame_info. */
7877 get_last_displayed_sal (&sal);
7878 sal.section = find_pc_overlay (sal.pc);
00903456 7879
4a64f543 7880 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
7881 where PC is the last displayed codepoint's address. So
7882 make sure to set sal.explicit_pc to prevent GDB from
7883 trying to expand the list of sals to include all other
7884 instances with the same symtab and line. */
00903456
JK
7885 sal.explicit_pc = 1;
7886
f8eba3c6
TT
7887 lsal.sals.sals[0] = sal;
7888 lsal.sals.nelts = 1;
7889 lsal.canonical = NULL;
7890
7891 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
7892 }
7893 else
8a3fe4f8 7894 error (_("No default breakpoint address now."));
c906108c
SS
7895 }
7896 else
7897 {
c906108c 7898 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7899 current_source_symtab (which is decode_line_1's default).
7900 This should produce the results we want almost all of the
f8eba3c6
TT
7901 time while leaving default_breakpoint_* alone. */
7902 if (last_displayed_sal_is_valid ())
7903 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
7904 get_last_displayed_symtab (),
7905 get_last_displayed_line (),
7906 canonical, NULL, NULL);
c906108c 7907 else
f8eba3c6
TT
7908 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
7909 (struct symtab *) NULL, 0,
7910 canonical, NULL, NULL);
c906108c 7911 }
c3f6f71d 7912}
c906108c 7913
c906108c 7914
c3f6f71d 7915/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7916 inserted as a breakpoint. If it can't throw an error. */
c906108c 7917
b9362cc7 7918static void
23e7acfb 7919breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7920{
7921 int i;
cc59ec59 7922
c3f6f71d 7923 for (i = 0; i < sals->nelts; i++)
ee53e872 7924 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7925}
7926
7a697b8d
SS
7927/* Fast tracepoints may have restrictions on valid locations. For
7928 instance, a fast tracepoint using a jump instead of a trap will
7929 likely have to overwrite more bytes than a trap would, and so can
7930 only be placed where the instruction is longer than the jump, or a
7931 multi-instruction sequence does not have a jump into the middle of
7932 it, etc. */
7933
7934static void
7935check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7936 struct symtabs_and_lines *sals)
7937{
7938 int i, rslt;
7939 struct symtab_and_line *sal;
7940 char *msg;
7941 struct cleanup *old_chain;
7942
7943 for (i = 0; i < sals->nelts; i++)
7944 {
f8eba3c6
TT
7945 struct gdbarch *sarch;
7946
7a697b8d
SS
7947 sal = &sals->sals[i];
7948
f8eba3c6
TT
7949 sarch = get_sal_arch (*sal);
7950 /* We fall back to GDBARCH if there is no architecture
7951 associated with SAL. */
7952 if (sarch == NULL)
7953 sarch = gdbarch;
7954 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
7955 NULL, &msg);
7956 old_chain = make_cleanup (xfree, msg);
7957
7958 if (!rslt)
7959 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 7960 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
7961
7962 do_cleanups (old_chain);
7963 }
7964}
7965
018d34a4
VP
7966/* Given TOK, a string specification of condition and thread, as
7967 accepted by the 'break' command, extract the condition
7968 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7969 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7970 If no condition is found, *COND_STRING is set to NULL.
7971 If no thread is found, *THREAD is set to -1. */
7972static void
7973find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7974 char **cond_string, int *thread, int *task)
018d34a4
VP
7975{
7976 *cond_string = NULL;
7977 *thread = -1;
7978 while (tok && *tok)
7979 {
7980 char *end_tok;
7981 int toklen;
7982 char *cond_start = NULL;
7983 char *cond_end = NULL;
cc59ec59 7984
e9cafbcc 7985 tok = skip_spaces (tok);
018d34a4 7986
e9cafbcc 7987 end_tok = skip_to_space (tok);
018d34a4
VP
7988
7989 toklen = end_tok - tok;
7990
7991 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7992 {
f7545552
TT
7993 struct expression *expr;
7994
018d34a4 7995 tok = cond_start = end_tok + 1;
f7545552
TT
7996 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7997 xfree (expr);
018d34a4
VP
7998 cond_end = tok;
7999 *cond_string = savestring (cond_start,
8000 cond_end - cond_start);
8001 }
8002 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8003 {
8004 char *tmptok;
8005
8006 tok = end_tok + 1;
8007 tmptok = tok;
8008 *thread = strtol (tok, &tok, 0);
8009 if (tok == tmptok)
8010 error (_("Junk after thread keyword."));
8011 if (!valid_thread_id (*thread))
8012 error (_("Unknown thread %d."), *thread);
8013 }
4a306c9a
JB
8014 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8015 {
8016 char *tmptok;
8017
8018 tok = end_tok + 1;
8019 tmptok = tok;
8020 *task = strtol (tok, &tok, 0);
8021 if (tok == tmptok)
8022 error (_("Junk after task keyword."));
8023 if (!valid_task_id (*task))
b6199126 8024 error (_("Unknown task %d."), *task);
4a306c9a 8025 }
018d34a4
VP
8026 else
8027 error (_("Junk at end of arguments."));
8028 }
8029}
8030
0fb4aa4b
PA
8031/* Decode a static tracepoint marker spec. */
8032
8033static struct symtabs_and_lines
8034decode_static_tracepoint_spec (char **arg_p)
8035{
8036 VEC(static_tracepoint_marker_p) *markers = NULL;
8037 struct symtabs_and_lines sals;
8038 struct symtab_and_line sal;
8039 struct symbol *sym;
8040 struct cleanup *old_chain;
8041 char *p = &(*arg_p)[3];
8042 char *endp;
8043 char *marker_str;
8044 int i;
8045
e9cafbcc 8046 p = skip_spaces (p);
0fb4aa4b 8047
e9cafbcc 8048 endp = skip_to_space (p);
0fb4aa4b
PA
8049
8050 marker_str = savestring (p, endp - p);
8051 old_chain = make_cleanup (xfree, marker_str);
8052
8053 markers = target_static_tracepoint_markers_by_strid (marker_str);
8054 if (VEC_empty(static_tracepoint_marker_p, markers))
8055 error (_("No known static tracepoint marker named %s"), marker_str);
8056
8057 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8058 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
8059
8060 for (i = 0; i < sals.nelts; i++)
8061 {
8062 struct static_tracepoint_marker *marker;
8063
8064 marker = VEC_index (static_tracepoint_marker_p, markers, i);
8065
8066 init_sal (&sals.sals[i]);
8067
8068 sals.sals[i] = find_pc_line (marker->address, 0);
8069 sals.sals[i].pc = marker->address;
8070
8071 release_static_tracepoint_marker (marker);
8072 }
8073
8074 do_cleanups (old_chain);
8075
8076 *arg_p = endp;
8077 return sals;
8078}
8079
fd9b8c24
PA
8080/* Set a breakpoint. This function is shared between CLI and MI
8081 functions for setting a breakpoint. This function has two major
8082 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
8083 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
8084 breakpoint location, address and thread. Otherwise, ARG is just
8085 the location of breakpoint, with condition and thread specified by
8086 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
8087 the breakpoint number will be allocated from the internal
8088 breakpoint count. Returns true if any breakpoint was created;
8089 false otherwise. */
0101ce28 8090
8cdf0e15
VP
8091int
8092create_breakpoint (struct gdbarch *gdbarch,
8093 char *arg, char *cond_string, int thread,
8094 int parse_condition_and_thread,
0fb4aa4b 8095 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
8096 int ignore_count,
8097 enum auto_boolean pending_break_support,
c0a91b2b 8098 const struct breakpoint_ops *ops,
84f4c1fe 8099 int from_tty, int enabled, int internal)
c3f6f71d 8100{
b78a6381 8101 volatile struct gdb_exception e;
f8eba3c6 8102 char *copy_arg = NULL;
c3f6f71d 8103 char *addr_start = arg;
7efd8fc2 8104 struct linespec_result canonical;
c3f6f71d 8105 struct cleanup *old_chain;
80c99de1 8106 struct cleanup *bkpt_chain = NULL;
05ff989b 8107 int i;
0101ce28 8108 int pending = 0;
4a306c9a 8109 int task = 0;
86b17b60 8110 int prev_bkpt_count = breakpoint_count;
c3f6f71d 8111
348d480f
PA
8112 gdb_assert (ops != NULL);
8113
7efd8fc2 8114 init_linespec_result (&canonical);
c3f6f71d 8115
b78a6381
TT
8116 TRY_CATCH (e, RETURN_MASK_ALL)
8117 {
983af33b
SDJ
8118 ops->create_sals_from_address (&arg, &canonical, type_wanted,
8119 addr_start, &copy_arg);
b78a6381 8120 }
0101ce28
JJ
8121
8122 /* If caller is interested in rc value from parse, set value. */
05ff989b 8123 switch (e.reason)
0101ce28 8124 {
983af33b
SDJ
8125 case GDB_NO_ERROR:
8126 if (VEC_empty (linespec_sals, canonical.sals))
8127 return 0;
8128 break;
05ff989b
AC
8129 case RETURN_ERROR:
8130 switch (e.error)
0101ce28 8131 {
05ff989b 8132 case NOT_FOUND_ERROR:
0101ce28 8133
05ff989b
AC
8134 /* If pending breakpoint support is turned off, throw
8135 error. */
fa8d40ab
JJ
8136
8137 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
8138 throw_exception (e);
8139
8140 exception_print (gdb_stderr, e);
fa8d40ab 8141
05ff989b
AC
8142 /* If pending breakpoint support is auto query and the user
8143 selects no, then simply return the error code. */
059fb39f 8144 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
8145 && !nquery (_("Make %s pending on future shared library load? "),
8146 bptype_string (type_wanted)))
fd9b8c24 8147 return 0;
fa8d40ab 8148
05ff989b
AC
8149 /* At this point, either the user was queried about setting
8150 a pending breakpoint and selected yes, or pending
8151 breakpoint behavior is on and thus a pending breakpoint
8152 is defaulted on behalf of the user. */
f8eba3c6
TT
8153 {
8154 struct linespec_sals lsal;
8155
8156 copy_arg = xstrdup (addr_start);
8157 lsal.canonical = xstrdup (copy_arg);
8158 lsal.sals.nelts = 1;
8159 lsal.sals.sals = XNEW (struct symtab_and_line);
8160 init_sal (&lsal.sals.sals[0]);
8161 pending = 1;
8162 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
8163 }
05ff989b
AC
8164 break;
8165 default:
98deb0da 8166 throw_exception (e);
0101ce28 8167 }
2abae994 8168 break;
05ff989b 8169 default:
983af33b 8170 throw_exception (e);
0101ce28 8171 }
c3f6f71d 8172
4a64f543 8173 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 8174 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 8175
c3f6f71d
JM
8176 /* ----------------------------- SNIP -----------------------------
8177 Anything added to the cleanup chain beyond this point is assumed
8178 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
8179 then the memory is not reclaimed. */
8180 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 8181
c3f6f71d
JM
8182 /* Resolve all line numbers to PC's and verify that the addresses
8183 are ok for the target. */
0101ce28 8184 if (!pending)
f8eba3c6
TT
8185 {
8186 int ix;
8187 struct linespec_sals *iter;
8188
8189 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
8190 breakpoint_sals_to_pc (&iter->sals);
8191 }
c3f6f71d 8192
7a697b8d 8193 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 8194 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
8195 {
8196 int ix;
8197 struct linespec_sals *iter;
8198
8199 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
8200 check_fast_tracepoint_sals (gdbarch, &iter->sals);
8201 }
7a697b8d 8202
c3f6f71d
JM
8203 /* Verify that condition can be parsed, before setting any
8204 breakpoints. Allocate a separate condition expression for each
4a64f543 8205 breakpoint. */
0101ce28 8206 if (!pending)
c3f6f71d 8207 {
f8eba3c6
TT
8208 struct linespec_sals *lsal;
8209
8210 lsal = VEC_index (linespec_sals, canonical.sals, 0);
8211
2f069f6f 8212 if (parse_condition_and_thread)
72b2ff0e
VP
8213 {
8214 /* Here we only parse 'arg' to separate condition
8215 from thread number, so parsing in context of first
8216 sal is OK. When setting the breakpoint we'll
8217 re-parse it in context of each sal. */
8218 cond_string = NULL;
8219 thread = -1;
f8eba3c6 8220 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
4a306c9a 8221 &thread, &task);
72b2ff0e
VP
8222 if (cond_string)
8223 make_cleanup (xfree, cond_string);
8224 }
2f069f6f 8225 else
72b2ff0e
VP
8226 {
8227 /* Create a private copy of condition string. */
8228 if (cond_string)
8229 {
8230 cond_string = xstrdup (cond_string);
8231 make_cleanup (xfree, cond_string);
8232 }
8233 }
0fb4aa4b 8234
983af33b 8235 ops->create_breakpoints_sal (gdbarch, &canonical, lsal,
d9b3f62e
PA
8236 cond_string, type_wanted,
8237 tempflag ? disp_del : disp_donttouch,
8238 thread, task, ignore_count, ops,
983af33b 8239 from_tty, enabled, internal);
c906108c 8240 }
0101ce28
JJ
8241 else
8242 {
0101ce28
JJ
8243 struct breakpoint *b;
8244
0101ce28
JJ
8245 make_cleanup (xfree, copy_arg);
8246
bfccc43c
YQ
8247 if (is_tracepoint_type (type_wanted))
8248 {
8249 struct tracepoint *t;
8250
8251 t = XCNEW (struct tracepoint);
8252 b = &t->base;
8253 }
8254 else
8255 b = XNEW (struct breakpoint);
8256
8257 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
8258
f8eba3c6 8259 b->addr_string = copy_arg;
72b2ff0e 8260 b->cond_string = NULL;
0101ce28 8261 b->ignore_count = ignore_count;
0101ce28 8262 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 8263 b->condition_not_parsed = 1;
41447f92 8264 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
8265 if ((type_wanted != bp_breakpoint
8266 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 8267 b->pspace = current_program_space;
8bea4e01 8268
bfccc43c 8269 install_breakpoint (internal, b, 0);
0101ce28
JJ
8270 }
8271
f8eba3c6 8272 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 8273 {
3e43a32a
MS
8274 warning (_("Multiple breakpoints were set.\nUse the "
8275 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 8276 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
8277 }
8278
80c99de1
PA
8279 /* That's it. Discard the cleanups for data inserted into the
8280 breakpoint. */
8281 discard_cleanups (bkpt_chain);
8282 /* But cleanup everything else. */
c3f6f71d 8283 do_cleanups (old_chain);
217dc9e2 8284
80c99de1 8285 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 8286 update_global_location_list (1);
fd9b8c24
PA
8287
8288 return 1;
c3f6f71d 8289}
c906108c 8290
348d480f 8291/* Set a breakpoint.
72b2ff0e
VP
8292 ARG is a string describing breakpoint address,
8293 condition, and thread.
8294 FLAG specifies if a breakpoint is hardware on,
8295 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8296 and BP_TEMPFLAG. */
348d480f 8297
98deb0da 8298static void
72b2ff0e 8299break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 8300{
72b2ff0e 8301 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
8302 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8303 ? bp_hardware_breakpoint
8304 : bp_breakpoint);
c3f6f71d 8305
8cdf0e15
VP
8306 create_breakpoint (get_current_arch (),
8307 arg,
8308 NULL, 0, 1 /* parse arg */,
0fb4aa4b 8309 tempflag, type_wanted,
8cdf0e15
VP
8310 0 /* Ignore count */,
8311 pending_break_support,
348d480f 8312 &bkpt_breakpoint_ops,
8cdf0e15 8313 from_tty,
84f4c1fe
PM
8314 1 /* enabled */,
8315 0 /* internal */);
c906108c
SS
8316}
8317
c906108c
SS
8318/* Helper function for break_command_1 and disassemble_command. */
8319
8320void
fba45db2 8321resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
8322{
8323 CORE_ADDR pc;
8324
8325 if (sal->pc == 0 && sal->symtab != NULL)
8326 {
8327 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 8328 error (_("No line %d in file \"%s\"."),
c906108c
SS
8329 sal->line, sal->symtab->filename);
8330 sal->pc = pc;
6a048695 8331
4a64f543
MS
8332 /* If this SAL corresponds to a breakpoint inserted using a line
8333 number, then skip the function prologue if necessary. */
6a048695 8334 if (sal->explicit_line)
059acae7 8335 skip_prologue_sal (sal);
c906108c
SS
8336 }
8337
8338 if (sal->section == 0 && sal->symtab != NULL)
8339 {
8340 struct blockvector *bv;
c5aa993b
JM
8341 struct block *b;
8342 struct symbol *sym;
c906108c 8343
801e3a5b 8344 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
8345 if (bv != NULL)
8346 {
7f0df278 8347 sym = block_linkage_function (b);
c906108c
SS
8348 if (sym != NULL)
8349 {
8350 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 8351 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
8352 }
8353 else
8354 {
4a64f543
MS
8355 /* It really is worthwhile to have the section, so we'll
8356 just have to look harder. This case can be executed
8357 if we have line numbers but no functions (as can
8358 happen in assembly source). */
c906108c 8359
c5aa993b 8360 struct minimal_symbol *msym;
6c95b8df
PA
8361 struct cleanup *old_chain = save_current_space_and_thread ();
8362
8363 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8364
8365 msym = lookup_minimal_symbol_by_pc (sal->pc);
8366 if (msym)
714835d5 8367 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8368
8369 do_cleanups (old_chain);
c906108c
SS
8370 }
8371 }
8372 }
8373}
8374
8375void
fba45db2 8376break_command (char *arg, int from_tty)
c906108c 8377{
db107f19 8378 break_command_1 (arg, 0, from_tty);
c906108c
SS
8379}
8380
c906108c 8381void
fba45db2 8382tbreak_command (char *arg, int from_tty)
c906108c 8383{
db107f19 8384 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8385}
8386
c906108c 8387static void
fba45db2 8388hbreak_command (char *arg, int from_tty)
c906108c 8389{
db107f19 8390 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8391}
8392
8393static void
fba45db2 8394thbreak_command (char *arg, int from_tty)
c906108c 8395{
db107f19 8396 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8397}
8398
8399static void
fba45db2 8400stop_command (char *arg, int from_tty)
c906108c 8401{
a3f17187 8402 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8403Usage: stop in <function | address>\n\
a3f17187 8404 stop at <line>\n"));
c906108c
SS
8405}
8406
8407static void
fba45db2 8408stopin_command (char *arg, int from_tty)
c906108c
SS
8409{
8410 int badInput = 0;
8411
c5aa993b 8412 if (arg == (char *) NULL)
c906108c
SS
8413 badInput = 1;
8414 else if (*arg != '*')
8415 {
8416 char *argptr = arg;
8417 int hasColon = 0;
8418
4a64f543 8419 /* Look for a ':'. If this is a line number specification, then
53a5351d 8420 say it is bad, otherwise, it should be an address or
4a64f543 8421 function/method name. */
c906108c 8422 while (*argptr && !hasColon)
c5aa993b
JM
8423 {
8424 hasColon = (*argptr == ':');
8425 argptr++;
8426 }
c906108c
SS
8427
8428 if (hasColon)
c5aa993b 8429 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8430 else
c5aa993b 8431 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8432 }
8433
8434 if (badInput)
a3f17187 8435 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8436 else
db107f19 8437 break_command_1 (arg, 0, from_tty);
c906108c
SS
8438}
8439
8440static void
fba45db2 8441stopat_command (char *arg, int from_tty)
c906108c
SS
8442{
8443 int badInput = 0;
8444
c5aa993b 8445 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8446 badInput = 1;
8447 else
8448 {
8449 char *argptr = arg;
8450 int hasColon = 0;
8451
4a64f543
MS
8452 /* Look for a ':'. If there is a '::' then get out, otherwise
8453 it is probably a line number. */
c906108c 8454 while (*argptr && !hasColon)
c5aa993b
JM
8455 {
8456 hasColon = (*argptr == ':');
8457 argptr++;
8458 }
c906108c
SS
8459
8460 if (hasColon)
c5aa993b 8461 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8462 else
c5aa993b 8463 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8464 }
8465
8466 if (badInput)
a3f17187 8467 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8468 else
db107f19 8469 break_command_1 (arg, 0, from_tty);
c906108c
SS
8470}
8471
f1310107
TJB
8472/* Implement the "breakpoint_hit" breakpoint_ops method for
8473 ranged breakpoints. */
8474
8475static int
8476breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8477 struct address_space *aspace,
09ac7c10
TT
8478 CORE_ADDR bp_addr,
8479 const struct target_waitstatus *ws)
f1310107 8480{
09ac7c10
TT
8481 if (ws->kind != TARGET_WAITKIND_STOPPED
8482 || ws->value.sig != TARGET_SIGNAL_TRAP)
8483 return 0;
8484
f1310107
TJB
8485 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8486 bl->length, aspace, bp_addr);
8487}
8488
8489/* Implement the "resources_needed" breakpoint_ops method for
8490 ranged breakpoints. */
8491
8492static int
8493resources_needed_ranged_breakpoint (const struct bp_location *bl)
8494{
8495 return target_ranged_break_num_registers ();
8496}
8497
8498/* Implement the "print_it" breakpoint_ops method for
8499 ranged breakpoints. */
8500
8501static enum print_stop_action
348d480f 8502print_it_ranged_breakpoint (bpstat bs)
f1310107 8503{
348d480f 8504 struct breakpoint *b = bs->breakpoint_at;
f1310107 8505 struct bp_location *bl = b->loc;
79a45e25 8506 struct ui_out *uiout = current_uiout;
f1310107
TJB
8507
8508 gdb_assert (b->type == bp_hardware_breakpoint);
8509
8510 /* Ranged breakpoints have only one location. */
8511 gdb_assert (bl && bl->next == NULL);
8512
8513 annotate_breakpoint (b->number);
8514 if (b->disposition == disp_del)
8515 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8516 else
8517 ui_out_text (uiout, "\nRanged breakpoint ");
8518 if (ui_out_is_mi_like_p (uiout))
8519 {
8520 ui_out_field_string (uiout, "reason",
8521 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8522 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8523 }
8524 ui_out_field_int (uiout, "bkptno", b->number);
8525 ui_out_text (uiout, ", ");
8526
8527 return PRINT_SRC_AND_LOC;
8528}
8529
8530/* Implement the "print_one" breakpoint_ops method for
8531 ranged breakpoints. */
8532
8533static void
8534print_one_ranged_breakpoint (struct breakpoint *b,
8535 struct bp_location **last_loc)
8536{
8537 struct bp_location *bl = b->loc;
8538 struct value_print_options opts;
79a45e25 8539 struct ui_out *uiout = current_uiout;
f1310107
TJB
8540
8541 /* Ranged breakpoints have only one location. */
8542 gdb_assert (bl && bl->next == NULL);
8543
8544 get_user_print_options (&opts);
8545
8546 if (opts.addressprint)
8547 /* We don't print the address range here, it will be printed later
8548 by print_one_detail_ranged_breakpoint. */
8549 ui_out_field_skip (uiout, "addr");
8550 annotate_field (5);
8551 print_breakpoint_location (b, bl);
8552 *last_loc = bl;
8553}
8554
8555/* Implement the "print_one_detail" breakpoint_ops method for
8556 ranged breakpoints. */
8557
8558static void
8559print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8560 struct ui_out *uiout)
8561{
8562 CORE_ADDR address_start, address_end;
8563 struct bp_location *bl = b->loc;
8564 struct ui_stream *stb = ui_out_stream_new (uiout);
8565 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8566
8567 gdb_assert (bl);
8568
8569 address_start = bl->address;
8570 address_end = address_start + bl->length - 1;
8571
8572 ui_out_text (uiout, "\taddress range: ");
8573 fprintf_unfiltered (stb->stream, "[%s, %s]",
8574 print_core_address (bl->gdbarch, address_start),
8575 print_core_address (bl->gdbarch, address_end));
8576 ui_out_field_stream (uiout, "addr", stb);
8577 ui_out_text (uiout, "\n");
8578
8579 do_cleanups (cleanup);
8580}
8581
8582/* Implement the "print_mention" breakpoint_ops method for
8583 ranged breakpoints. */
8584
8585static void
8586print_mention_ranged_breakpoint (struct breakpoint *b)
8587{
8588 struct bp_location *bl = b->loc;
79a45e25 8589 struct ui_out *uiout = current_uiout;
f1310107
TJB
8590
8591 gdb_assert (bl);
8592 gdb_assert (b->type == bp_hardware_breakpoint);
8593
8594 if (ui_out_is_mi_like_p (uiout))
8595 return;
8596
8597 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8598 b->number, paddress (bl->gdbarch, bl->address),
8599 paddress (bl->gdbarch, bl->address + bl->length - 1));
8600}
8601
8602/* Implement the "print_recreate" breakpoint_ops method for
8603 ranged breakpoints. */
8604
8605static void
8606print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8607{
8608 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8609 b->addr_string_range_end);
d9b3f62e 8610 print_recreate_thread (b, fp);
f1310107
TJB
8611}
8612
8613/* The breakpoint_ops structure to be used in ranged breakpoints. */
8614
2060206e 8615static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
8616
8617/* Find the address where the end of the breakpoint range should be
8618 placed, given the SAL of the end of the range. This is so that if
8619 the user provides a line number, the end of the range is set to the
8620 last instruction of the given line. */
8621
8622static CORE_ADDR
8623find_breakpoint_range_end (struct symtab_and_line sal)
8624{
8625 CORE_ADDR end;
8626
8627 /* If the user provided a PC value, use it. Otherwise,
8628 find the address of the end of the given location. */
8629 if (sal.explicit_pc)
8630 end = sal.pc;
8631 else
8632 {
8633 int ret;
8634 CORE_ADDR start;
8635
8636 ret = find_line_pc_range (sal, &start, &end);
8637 if (!ret)
8638 error (_("Could not find location of the end of the range."));
8639
8640 /* find_line_pc_range returns the start of the next line. */
8641 end--;
8642 }
8643
8644 return end;
8645}
8646
8647/* Implement the "break-range" CLI command. */
8648
8649static void
8650break_range_command (char *arg, int from_tty)
8651{
8652 char *arg_start, *addr_string_start, *addr_string_end;
8653 struct linespec_result canonical_start, canonical_end;
8654 int bp_count, can_use_bp, length;
8655 CORE_ADDR end;
8656 struct breakpoint *b;
8657 struct symtab_and_line sal_start, sal_end;
f1310107 8658 struct cleanup *cleanup_bkpt;
f8eba3c6 8659 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
8660
8661 /* We don't support software ranged breakpoints. */
8662 if (target_ranged_break_num_registers () < 0)
8663 error (_("This target does not support hardware ranged breakpoints."));
8664
8665 bp_count = hw_breakpoint_used_count ();
8666 bp_count += target_ranged_break_num_registers ();
8667 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8668 bp_count, 0);
8669 if (can_use_bp < 0)
8670 error (_("Hardware breakpoints used exceeds limit."));
8671
f8eba3c6 8672 arg = skip_spaces (arg);
f1310107
TJB
8673 if (arg == NULL || arg[0] == '\0')
8674 error(_("No address range specified."));
8675
f1310107
TJB
8676 init_linespec_result (&canonical_start);
8677
f8eba3c6
TT
8678 arg_start = arg;
8679 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 8680
f8eba3c6 8681 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
8682
8683 if (arg[0] != ',')
8684 error (_("Too few arguments."));
f8eba3c6 8685 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 8686 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
8687
8688 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
8689
8690 if (VEC_length (linespec_sals, canonical_start.sals) > 1
8691 || lsal_start->sals.nelts != 1)
f1310107
TJB
8692 error (_("Cannot create a ranged breakpoint with multiple locations."));
8693
f8eba3c6
TT
8694 sal_start = lsal_start->sals.sals[0];
8695 addr_string_start = savestring (arg_start, arg - arg_start);
8696 make_cleanup (xfree, addr_string_start);
f1310107
TJB
8697
8698 arg++; /* Skip the comma. */
f8eba3c6 8699 arg = skip_spaces (arg);
f1310107
TJB
8700
8701 /* Parse the end location. */
8702
f1310107
TJB
8703 init_linespec_result (&canonical_end);
8704 arg_start = arg;
8705
f8eba3c6 8706 /* We call decode_line_full directly here instead of using
f1310107
TJB
8707 parse_breakpoint_sals because we need to specify the start location's
8708 symtab and line as the default symtab and line for the end of the
8709 range. This makes it possible to have ranges like "foo.c:27, +14",
8710 where +14 means 14 lines from the start location. */
f8eba3c6
TT
8711 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
8712 sal_start.symtab, sal_start.line,
8713 &canonical_end, NULL, NULL);
8714
8715 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 8716
f8eba3c6 8717 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 8718 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
8719
8720 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
8721 if (VEC_length (linespec_sals, canonical_end.sals) > 1
8722 || lsal_end->sals.nelts != 1)
f1310107
TJB
8723 error (_("Cannot create a ranged breakpoint with multiple locations."));
8724
f8eba3c6
TT
8725 sal_end = lsal_end->sals.sals[0];
8726 addr_string_end = savestring (arg_start, arg - arg_start);
8727 make_cleanup (xfree, addr_string_end);
f1310107
TJB
8728
8729 end = find_breakpoint_range_end (sal_end);
8730 if (sal_start.pc > end)
177b42fe 8731 error (_("Invalid address range, end precedes start."));
f1310107
TJB
8732
8733 length = end - sal_start.pc + 1;
8734 if (length < 0)
8735 /* Length overflowed. */
8736 error (_("Address range too large."));
8737 else if (length == 1)
8738 {
8739 /* This range is simple enough to be handled by
8740 the `hbreak' command. */
8741 hbreak_command (addr_string_start, 1);
8742
8743 do_cleanups (cleanup_bkpt);
8744
8745 return;
8746 }
8747
8748 /* Now set up the breakpoint. */
8749 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 8750 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
8751 set_breakpoint_count (breakpoint_count + 1);
8752 b->number = breakpoint_count;
8753 b->disposition = disp_donttouch;
f8eba3c6
TT
8754 b->addr_string = xstrdup (addr_string_start);
8755 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
8756 b->loc->length = length;
8757
f8eba3c6 8758 do_cleanups (cleanup_bkpt);
f1310107
TJB
8759
8760 mention (b);
8d3788bd 8761 observer_notify_breakpoint_created (b);
f1310107
TJB
8762 update_global_location_list (1);
8763}
8764
4a64f543
MS
8765/* Return non-zero if EXP is verified as constant. Returned zero
8766 means EXP is variable. Also the constant detection may fail for
8767 some constant expressions and in such case still falsely return
8768 zero. */
2e6e3d9c 8769
65d79d4b
SDJ
8770static int
8771watchpoint_exp_is_const (const struct expression *exp)
8772{
8773 int i = exp->nelts;
8774
8775 while (i > 0)
8776 {
8777 int oplenp, argsp;
8778
8779 /* We are only interested in the descriptor of each element. */
8780 operator_length (exp, i, &oplenp, &argsp);
8781 i -= oplenp;
8782
8783 switch (exp->elts[i].opcode)
8784 {
8785 case BINOP_ADD:
8786 case BINOP_SUB:
8787 case BINOP_MUL:
8788 case BINOP_DIV:
8789 case BINOP_REM:
8790 case BINOP_MOD:
8791 case BINOP_LSH:
8792 case BINOP_RSH:
8793 case BINOP_LOGICAL_AND:
8794 case BINOP_LOGICAL_OR:
8795 case BINOP_BITWISE_AND:
8796 case BINOP_BITWISE_IOR:
8797 case BINOP_BITWISE_XOR:
8798 case BINOP_EQUAL:
8799 case BINOP_NOTEQUAL:
8800 case BINOP_LESS:
8801 case BINOP_GTR:
8802 case BINOP_LEQ:
8803 case BINOP_GEQ:
8804 case BINOP_REPEAT:
8805 case BINOP_COMMA:
8806 case BINOP_EXP:
8807 case BINOP_MIN:
8808 case BINOP_MAX:
8809 case BINOP_INTDIV:
8810 case BINOP_CONCAT:
8811 case BINOP_IN:
8812 case BINOP_RANGE:
8813 case TERNOP_COND:
8814 case TERNOP_SLICE:
8815 case TERNOP_SLICE_COUNT:
8816
8817 case OP_LONG:
8818 case OP_DOUBLE:
8819 case OP_DECFLOAT:
8820 case OP_LAST:
8821 case OP_COMPLEX:
8822 case OP_STRING:
8823 case OP_BITSTRING:
8824 case OP_ARRAY:
8825 case OP_TYPE:
8826 case OP_NAME:
8827 case OP_OBJC_NSSTRING:
8828
8829 case UNOP_NEG:
8830 case UNOP_LOGICAL_NOT:
8831 case UNOP_COMPLEMENT:
8832 case UNOP_ADDR:
8833 case UNOP_HIGH:
aeaa2474 8834 case UNOP_CAST:
4a64f543
MS
8835 /* Unary, binary and ternary operators: We have to check
8836 their operands. If they are constant, then so is the
8837 result of that operation. For instance, if A and B are
8838 determined to be constants, then so is "A + B".
8839
8840 UNOP_IND is one exception to the rule above, because the
8841 value of *ADDR is not necessarily a constant, even when
8842 ADDR is. */
65d79d4b
SDJ
8843 break;
8844
8845 case OP_VAR_VALUE:
8846 /* Check whether the associated symbol is a constant.
4a64f543 8847
65d79d4b 8848 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8849 possible that a buggy compiler could mark a variable as
8850 constant even when it is not, and TYPE_CONST would return
8851 true in this case, while SYMBOL_CLASS wouldn't.
8852
8853 We also have to check for function symbols because they
8854 are always constant. */
65d79d4b
SDJ
8855 {
8856 struct symbol *s = exp->elts[i + 2].symbol;
8857
8858 if (SYMBOL_CLASS (s) != LOC_BLOCK
8859 && SYMBOL_CLASS (s) != LOC_CONST
8860 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8861 return 0;
8862 break;
8863 }
8864
8865 /* The default action is to return 0 because we are using
8866 the optimistic approach here: If we don't know something,
8867 then it is not a constant. */
8868 default:
8869 return 0;
8870 }
8871 }
8872
8873 return 1;
8874}
8875
3a5c3e22
PA
8876/* Implement the "dtor" breakpoint_ops method for watchpoints. */
8877
8878static void
8879dtor_watchpoint (struct breakpoint *self)
8880{
8881 struct watchpoint *w = (struct watchpoint *) self;
8882
8883 xfree (w->cond_exp);
8884 xfree (w->exp);
8885 xfree (w->exp_string);
8886 xfree (w->exp_string_reparse);
8887 value_free (w->val);
8888
8889 base_breakpoint_ops.dtor (self);
8890}
8891
348d480f
PA
8892/* Implement the "re_set" breakpoint_ops method for watchpoints. */
8893
8894static void
8895re_set_watchpoint (struct breakpoint *b)
8896{
3a5c3e22
PA
8897 struct watchpoint *w = (struct watchpoint *) b;
8898
348d480f
PA
8899 /* Watchpoint can be either on expression using entirely global
8900 variables, or it can be on local variables.
8901
8902 Watchpoints of the first kind are never auto-deleted, and even
8903 persist across program restarts. Since they can use variables
8904 from shared libraries, we need to reparse expression as libraries
8905 are loaded and unloaded.
8906
8907 Watchpoints on local variables can also change meaning as result
8908 of solib event. For example, if a watchpoint uses both a local
8909 and a global variables in expression, it's a local watchpoint,
8910 but unloading of a shared library will make the expression
8911 invalid. This is not a very common use case, but we still
8912 re-evaluate expression, to avoid surprises to the user.
8913
8914 Note that for local watchpoints, we re-evaluate it only if
8915 watchpoints frame id is still valid. If it's not, it means the
8916 watchpoint is out of scope and will be deleted soon. In fact,
8917 I'm not sure we'll ever be called in this case.
8918
8919 If a local watchpoint's frame id is still valid, then
3a5c3e22 8920 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 8921
3a5c3e22
PA
8922 Don't do anything about disabled watchpoints, since they will be
8923 reevaluated again when enabled. */
8924 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
8925}
8926
77b06cd7
TJB
8927/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8928
8929static int
8930insert_watchpoint (struct bp_location *bl)
8931{
3a5c3e22
PA
8932 struct watchpoint *w = (struct watchpoint *) bl->owner;
8933 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8934
8935 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8936 w->cond_exp);
77b06cd7
TJB
8937}
8938
8939/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8940
8941static int
8942remove_watchpoint (struct bp_location *bl)
8943{
3a5c3e22
PA
8944 struct watchpoint *w = (struct watchpoint *) bl->owner;
8945 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8946
8947 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8948 w->cond_exp);
e09342b5
TJB
8949}
8950
e09342b5 8951static int
348d480f 8952breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
8953 struct address_space *aspace, CORE_ADDR bp_addr,
8954 const struct target_waitstatus *ws)
e09342b5 8955{
348d480f 8956 struct breakpoint *b = bl->owner;
3a5c3e22 8957 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 8958
348d480f
PA
8959 /* Continuable hardware watchpoints are treated as non-existent if the
8960 reason we stopped wasn't a hardware watchpoint (we didn't stop on
8961 some data address). Otherwise gdb won't stop on a break instruction
8962 in the code (not from a breakpoint) when a hardware watchpoint has
8963 been defined. Also skip watchpoints which we know did not trigger
8964 (did not match the data address). */
8965 if (is_hardware_watchpoint (b)
3a5c3e22 8966 && w->watchpoint_triggered == watch_triggered_no)
348d480f 8967 return 0;
9c06b0b4 8968
348d480f 8969 return 1;
9c06b0b4
TJB
8970}
8971
348d480f
PA
8972static void
8973check_status_watchpoint (bpstat bs)
9c06b0b4 8974{
348d480f 8975 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 8976
348d480f 8977 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
8978}
8979
8980/* Implement the "resources_needed" breakpoint_ops method for
348d480f 8981 hardware watchpoints. */
9c06b0b4
TJB
8982
8983static int
348d480f 8984resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 8985{
3a5c3e22
PA
8986 struct watchpoint *w = (struct watchpoint *) bl->owner;
8987 int length = w->exact? 1 : bl->length;
348d480f
PA
8988
8989 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
8990}
8991
8992/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 8993 hardware watchpoints. */
9c06b0b4
TJB
8994
8995static int
348d480f 8996works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 8997{
efa80663
PA
8998 /* Read and access watchpoints only work with hardware support. */
8999 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
9000}
9001
9c06b0b4 9002static enum print_stop_action
348d480f 9003print_it_watchpoint (bpstat bs)
9c06b0b4 9004{
348d480f
PA
9005 struct cleanup *old_chain;
9006 struct breakpoint *b;
9007 const struct bp_location *bl;
9008 struct ui_stream *stb;
9009 enum print_stop_action result;
3a5c3e22 9010 struct watchpoint *w;
79a45e25 9011 struct ui_out *uiout = current_uiout;
348d480f
PA
9012
9013 gdb_assert (bs->bp_location_at != NULL);
9014
9015 bl = bs->bp_location_at;
9016 b = bs->breakpoint_at;
3a5c3e22 9017 w = (struct watchpoint *) b;
348d480f
PA
9018
9019 stb = ui_out_stream_new (uiout);
9020 old_chain = make_cleanup_ui_out_stream_delete (stb);
9c06b0b4
TJB
9021
9022 switch (b->type)
9023 {
348d480f 9024 case bp_watchpoint:
9c06b0b4
TJB
9025 case bp_hardware_watchpoint:
9026 annotate_watchpoint (b->number);
9027 if (ui_out_is_mi_like_p (uiout))
9028 ui_out_field_string
9029 (uiout, "reason",
9030 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
9031 mention (b);
9032 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
9033 ui_out_text (uiout, "\nOld value = ");
9034 watchpoint_value_print (bs->old_val, stb->stream);
9035 ui_out_field_stream (uiout, "old", stb);
9036 ui_out_text (uiout, "\nNew value = ");
3a5c3e22 9037 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
9038 ui_out_field_stream (uiout, "new", stb);
9039 ui_out_text (uiout, "\n");
9040 /* More than one watchpoint may have been triggered. */
9041 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9042 break;
9043
9044 case bp_read_watchpoint:
9045 if (ui_out_is_mi_like_p (uiout))
9046 ui_out_field_string
9047 (uiout, "reason",
9048 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
9049 mention (b);
9050 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
9051 ui_out_text (uiout, "\nValue = ");
3a5c3e22 9052 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
9053 ui_out_field_stream (uiout, "value", stb);
9054 ui_out_text (uiout, "\n");
9055 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9056 break;
9057
9058 case bp_access_watchpoint:
348d480f
PA
9059 if (bs->old_val != NULL)
9060 {
9061 annotate_watchpoint (b->number);
9062 if (ui_out_is_mi_like_p (uiout))
9063 ui_out_field_string
9064 (uiout, "reason",
9065 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9066 mention (b);
9067 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
9068 ui_out_text (uiout, "\nOld value = ");
9069 watchpoint_value_print (bs->old_val, stb->stream);
9070 ui_out_field_stream (uiout, "old", stb);
9071 ui_out_text (uiout, "\nNew value = ");
9072 }
9073 else
9074 {
9075 mention (b);
9076 if (ui_out_is_mi_like_p (uiout))
9077 ui_out_field_string
9078 (uiout, "reason",
9079 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9080 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
9081 ui_out_text (uiout, "\nValue = ");
9082 }
3a5c3e22 9083 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
9084 ui_out_field_stream (uiout, "new", stb);
9085 ui_out_text (uiout, "\n");
9086 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9087 break;
9088 default:
348d480f 9089 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9090 }
9091
348d480f
PA
9092 do_cleanups (old_chain);
9093 return result;
9094}
9095
9096/* Implement the "print_mention" breakpoint_ops method for hardware
9097 watchpoints. */
9098
9099static void
9100print_mention_watchpoint (struct breakpoint *b)
9101{
9102 struct cleanup *ui_out_chain;
3a5c3e22 9103 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 9104 struct ui_out *uiout = current_uiout;
348d480f
PA
9105
9106 switch (b->type)
9107 {
9108 case bp_watchpoint:
9109 ui_out_text (uiout, "Watchpoint ");
9110 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
9111 break;
9112 case bp_hardware_watchpoint:
9113 ui_out_text (uiout, "Hardware watchpoint ");
9114 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
9115 break;
9116 case bp_read_watchpoint:
9117 ui_out_text (uiout, "Hardware read watchpoint ");
9118 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
9119 break;
9120 case bp_access_watchpoint:
9121 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
9122 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
9123 break;
9124 default:
9125 internal_error (__FILE__, __LINE__,
9126 _("Invalid hardware watchpoint type."));
9127 }
9128
9129 ui_out_field_int (uiout, "number", b->number);
9130 ui_out_text (uiout, ": ");
3a5c3e22 9131 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
9132 do_cleanups (ui_out_chain);
9133}
9134
9135/* Implement the "print_recreate" breakpoint_ops method for
9136 watchpoints. */
9137
9138static void
9139print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
9140{
3a5c3e22
PA
9141 struct watchpoint *w = (struct watchpoint *) b;
9142
348d480f
PA
9143 switch (b->type)
9144 {
9145 case bp_watchpoint:
9146 case bp_hardware_watchpoint:
9147 fprintf_unfiltered (fp, "watch");
9148 break;
9149 case bp_read_watchpoint:
9150 fprintf_unfiltered (fp, "rwatch");
9151 break;
9152 case bp_access_watchpoint:
9153 fprintf_unfiltered (fp, "awatch");
9154 break;
9155 default:
9156 internal_error (__FILE__, __LINE__,
9157 _("Invalid watchpoint type."));
9158 }
9159
3a5c3e22 9160 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 9161 print_recreate_thread (b, fp);
348d480f
PA
9162}
9163
9164/* The breakpoint_ops structure to be used in hardware watchpoints. */
9165
2060206e 9166static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
9167
9168/* Implement the "insert" breakpoint_ops method for
9169 masked hardware watchpoints. */
9170
9171static int
9172insert_masked_watchpoint (struct bp_location *bl)
9173{
3a5c3e22
PA
9174 struct watchpoint *w = (struct watchpoint *) bl->owner;
9175
9176 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
9177 bl->watchpoint_type);
9178}
9179
9180/* Implement the "remove" breakpoint_ops method for
9181 masked hardware watchpoints. */
9182
9183static int
9184remove_masked_watchpoint (struct bp_location *bl)
9185{
3a5c3e22
PA
9186 struct watchpoint *w = (struct watchpoint *) bl->owner;
9187
9188 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
9189 bl->watchpoint_type);
9190}
9191
9192/* Implement the "resources_needed" breakpoint_ops method for
9193 masked hardware watchpoints. */
9194
9195static int
9196resources_needed_masked_watchpoint (const struct bp_location *bl)
9197{
3a5c3e22
PA
9198 struct watchpoint *w = (struct watchpoint *) bl->owner;
9199
9200 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
9201}
9202
9203/* Implement the "works_in_software_mode" breakpoint_ops method for
9204 masked hardware watchpoints. */
9205
9206static int
9207works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
9208{
9209 return 0;
9210}
9211
9212/* Implement the "print_it" breakpoint_ops method for
9213 masked hardware watchpoints. */
9214
9215static enum print_stop_action
9216print_it_masked_watchpoint (bpstat bs)
9217{
9218 struct breakpoint *b = bs->breakpoint_at;
79a45e25 9219 struct ui_out *uiout = current_uiout;
348d480f
PA
9220
9221 /* Masked watchpoints have only one location. */
9222 gdb_assert (b->loc && b->loc->next == NULL);
9223
9224 switch (b->type)
9225 {
9226 case bp_hardware_watchpoint:
9227 annotate_watchpoint (b->number);
9228 if (ui_out_is_mi_like_p (uiout))
9229 ui_out_field_string
9230 (uiout, "reason",
9231 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9232 break;
9233
9234 case bp_read_watchpoint:
9235 if (ui_out_is_mi_like_p (uiout))
9236 ui_out_field_string
9237 (uiout, "reason",
9238 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9239 break;
9240
9241 case bp_access_watchpoint:
9242 if (ui_out_is_mi_like_p (uiout))
9243 ui_out_field_string
9244 (uiout, "reason",
9245 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9246 break;
9247 default:
9248 internal_error (__FILE__, __LINE__,
9249 _("Invalid hardware watchpoint type."));
9250 }
9251
9252 mention (b);
9c06b0b4
TJB
9253 ui_out_text (uiout, _("\n\
9254Check the underlying instruction at PC for the memory\n\
9255address and value which triggered this watchpoint.\n"));
9256 ui_out_text (uiout, "\n");
9257
9258 /* More than one watchpoint may have been triggered. */
9259 return PRINT_UNKNOWN;
9260}
9261
9262/* Implement the "print_one_detail" breakpoint_ops method for
9263 masked hardware watchpoints. */
9264
9265static void
9266print_one_detail_masked_watchpoint (const struct breakpoint *b,
9267 struct ui_out *uiout)
9268{
3a5c3e22
PA
9269 struct watchpoint *w = (struct watchpoint *) b;
9270
9c06b0b4
TJB
9271 /* Masked watchpoints have only one location. */
9272 gdb_assert (b->loc && b->loc->next == NULL);
9273
9274 ui_out_text (uiout, "\tmask ");
3a5c3e22 9275 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
9276 ui_out_text (uiout, "\n");
9277}
9278
9279/* Implement the "print_mention" breakpoint_ops method for
9280 masked hardware watchpoints. */
9281
9282static void
9283print_mention_masked_watchpoint (struct breakpoint *b)
9284{
3a5c3e22 9285 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 9286 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
9287 struct cleanup *ui_out_chain;
9288
9289 switch (b->type)
9290 {
9291 case bp_hardware_watchpoint:
9292 ui_out_text (uiout, "Masked hardware watchpoint ");
9293 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
9294 break;
9295 case bp_read_watchpoint:
9296 ui_out_text (uiout, "Masked hardware read watchpoint ");
9297 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
9298 break;
9299 case bp_access_watchpoint:
9300 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
9301 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
9302 break;
9303 default:
9304 internal_error (__FILE__, __LINE__,
9305 _("Invalid hardware watchpoint type."));
9306 }
9307
9308 ui_out_field_int (uiout, "number", b->number);
9309 ui_out_text (uiout, ": ");
3a5c3e22 9310 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
9311 do_cleanups (ui_out_chain);
9312}
9313
9314/* Implement the "print_recreate" breakpoint_ops method for
9315 masked hardware watchpoints. */
9316
9317static void
9318print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
9319{
3a5c3e22 9320 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
9321 char tmp[40];
9322
9323 switch (b->type)
9324 {
9325 case bp_hardware_watchpoint:
9326 fprintf_unfiltered (fp, "watch");
9327 break;
9328 case bp_read_watchpoint:
9329 fprintf_unfiltered (fp, "rwatch");
9330 break;
9331 case bp_access_watchpoint:
9332 fprintf_unfiltered (fp, "awatch");
9333 break;
9334 default:
9335 internal_error (__FILE__, __LINE__,
9336 _("Invalid hardware watchpoint type."));
9337 }
9338
3a5c3e22
PA
9339 sprintf_vma (tmp, w->hw_wp_mask);
9340 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 9341 print_recreate_thread (b, fp);
9c06b0b4
TJB
9342}
9343
9344/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
9345
2060206e 9346static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
9347
9348/* Tell whether the given watchpoint is a masked hardware watchpoint. */
9349
9350static int
9351is_masked_watchpoint (const struct breakpoint *b)
9352{
9353 return b->ops == &masked_watchpoint_breakpoint_ops;
9354}
9355
53a5351d
JM
9356/* accessflag: hw_write: watch write,
9357 hw_read: watch read,
9358 hw_access: watch access (read or write) */
c906108c 9359static void
84f4c1fe
PM
9360watch_command_1 (char *arg, int accessflag, int from_tty,
9361 int just_location, int internal)
c906108c 9362{
a9634178 9363 volatile struct gdb_exception e;
d983da9c 9364 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 9365 struct expression *exp;
60e1c644 9366 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 9367 struct value *val, *mark, *result;
c906108c 9368 struct frame_info *frame;
c906108c
SS
9369 char *exp_start = NULL;
9370 char *exp_end = NULL;
9c06b0b4
TJB
9371 char *tok, *end_tok;
9372 int toklen = -1;
c906108c
SS
9373 char *cond_start = NULL;
9374 char *cond_end = NULL;
c906108c 9375 enum bptype bp_type;
37e4754d 9376 int thread = -1;
0cf6dd15 9377 int pc = 0;
9c06b0b4
TJB
9378 /* Flag to indicate whether we are going to use masks for
9379 the hardware watchpoint. */
9380 int use_mask = 0;
9381 CORE_ADDR mask = 0;
3a5c3e22 9382 struct watchpoint *w;
c906108c 9383
37e4754d
LM
9384 /* Make sure that we actually have parameters to parse. */
9385 if (arg != NULL && arg[0] != '\0')
9386 {
9c06b0b4 9387 char *value_start;
37e4754d 9388
9c06b0b4
TJB
9389 /* Look for "parameter value" pairs at the end
9390 of the arguments string. */
9391 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
9392 {
9393 /* Skip whitespace at the end of the argument list. */
9394 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9395 tok--;
9396
9397 /* Find the beginning of the last token.
9398 This is the value of the parameter. */
9399 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9400 tok--;
9401 value_start = tok + 1;
9402
9403 /* Skip whitespace. */
9404 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9405 tok--;
9406
9407 end_tok = tok;
9408
9409 /* Find the beginning of the second to last token.
9410 This is the parameter itself. */
9411 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9412 tok--;
9413 tok++;
9414 toklen = end_tok - tok + 1;
9415
9416 if (toklen == 6 && !strncmp (tok, "thread", 6))
9417 {
9418 /* At this point we've found a "thread" token, which means
9419 the user is trying to set a watchpoint that triggers
9420 only in a specific thread. */
9421 char *endp;
37e4754d 9422
9c06b0b4
TJB
9423 if (thread != -1)
9424 error(_("You can specify only one thread."));
37e4754d 9425
9c06b0b4
TJB
9426 /* Extract the thread ID from the next token. */
9427 thread = strtol (value_start, &endp, 0);
37e4754d 9428
9c06b0b4
TJB
9429 /* Check if the user provided a valid numeric value for the
9430 thread ID. */
9431 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9432 error (_("Invalid thread ID specification %s."), value_start);
9433
9434 /* Check if the thread actually exists. */
9435 if (!valid_thread_id (thread))
9436 error (_("Unknown thread %d."), thread);
9437 }
9438 else if (toklen == 4 && !strncmp (tok, "mask", 4))
9439 {
9440 /* We've found a "mask" token, which means the user wants to
9441 create a hardware watchpoint that is going to have the mask
9442 facility. */
9443 struct value *mask_value, *mark;
37e4754d 9444
9c06b0b4
TJB
9445 if (use_mask)
9446 error(_("You can specify only one mask."));
37e4754d 9447
9c06b0b4 9448 use_mask = just_location = 1;
37e4754d 9449
9c06b0b4
TJB
9450 mark = value_mark ();
9451 mask_value = parse_to_comma_and_eval (&value_start);
9452 mask = value_as_address (mask_value);
9453 value_free_to_mark (mark);
9454 }
9455 else
9456 /* We didn't recognize what we found. We should stop here. */
9457 break;
37e4754d 9458
9c06b0b4
TJB
9459 /* Truncate the string and get rid of the "parameter value" pair before
9460 the arguments string is parsed by the parse_exp_1 function. */
9461 *tok = '\0';
9462 }
37e4754d
LM
9463 }
9464
9465 /* Parse the rest of the arguments. */
c906108c
SS
9466 innermost_block = NULL;
9467 exp_start = arg;
9468 exp = parse_exp_1 (&arg, 0, 0);
9469 exp_end = arg;
fa8a61dc
TT
9470 /* Remove trailing whitespace from the expression before saving it.
9471 This makes the eventual display of the expression string a bit
9472 prettier. */
9473 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9474 --exp_end;
9475
65d79d4b
SDJ
9476 /* Checking if the expression is not constant. */
9477 if (watchpoint_exp_is_const (exp))
9478 {
9479 int len;
9480
9481 len = exp_end - exp_start;
9482 while (len > 0 && isspace (exp_start[len - 1]))
9483 len--;
9484 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9485 }
9486
c906108c
SS
9487 exp_valid_block = innermost_block;
9488 mark = value_mark ();
a1442452 9489 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
9490
9491 if (just_location)
9492 {
9c06b0b4
TJB
9493 int ret;
9494
06a64a0b 9495 exp_valid_block = NULL;
a1442452 9496 val = value_addr (result);
06a64a0b
TT
9497 release_value (val);
9498 value_free_to_mark (mark);
9c06b0b4
TJB
9499
9500 if (use_mask)
9501 {
9502 ret = target_masked_watch_num_registers (value_as_address (val),
9503 mask);
9504 if (ret == -1)
9505 error (_("This target does not support masked watchpoints."));
9506 else if (ret == -2)
9507 error (_("Invalid mask or memory region."));
9508 }
06a64a0b
TT
9509 }
9510 else if (val != NULL)
fa4727a6 9511 release_value (val);
c906108c 9512
e9cafbcc
TT
9513 tok = skip_spaces (arg);
9514 end_tok = skip_to_space (tok);
c906108c
SS
9515
9516 toklen = end_tok - tok;
9517 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9518 {
2d134ed3
PA
9519 struct expression *cond;
9520
60e1c644 9521 innermost_block = NULL;
c906108c
SS
9522 tok = cond_start = end_tok + 1;
9523 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
9524
9525 /* The watchpoint expression may not be local, but the condition
9526 may still be. E.g.: `watch global if local > 0'. */
9527 cond_exp_valid_block = innermost_block;
9528
2d134ed3 9529 xfree (cond);
c906108c
SS
9530 cond_end = tok;
9531 }
9532 if (*tok)
8a3fe4f8 9533 error (_("Junk at end of command."));
c906108c 9534
53a5351d 9535 if (accessflag == hw_read)
c5aa993b 9536 bp_type = bp_read_watchpoint;
53a5351d 9537 else if (accessflag == hw_access)
c5aa993b
JM
9538 bp_type = bp_access_watchpoint;
9539 else
9540 bp_type = bp_hardware_watchpoint;
c906108c 9541
d983da9c 9542 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
9543
9544 /* If the expression is "local", then set up a "watchpoint scope"
9545 breakpoint at the point where we've left the scope of the watchpoint
9546 expression. Create the scope breakpoint before the watchpoint, so
9547 that we will encounter it first in bpstat_stop_status. */
60e1c644 9548 if (exp_valid_block && frame)
d983da9c 9549 {
edb3359d
DJ
9550 if (frame_id_p (frame_unwind_caller_id (frame)))
9551 {
9552 scope_breakpoint
a6d9a66e
UW
9553 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
9554 frame_unwind_caller_pc (frame),
06edf0c0
PA
9555 bp_watchpoint_scope,
9556 &momentary_breakpoint_ops);
d983da9c 9557
edb3359d 9558 scope_breakpoint->enable_state = bp_enabled;
d983da9c 9559
edb3359d
DJ
9560 /* Automatically delete the breakpoint when it hits. */
9561 scope_breakpoint->disposition = disp_del;
d983da9c 9562
edb3359d
DJ
9563 /* Only break in the proper frame (help with recursion). */
9564 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 9565
edb3359d 9566 /* Set the address at which we will stop. */
a6d9a66e
UW
9567 scope_breakpoint->loc->gdbarch
9568 = frame_unwind_caller_arch (frame);
edb3359d
DJ
9569 scope_breakpoint->loc->requested_address
9570 = frame_unwind_caller_pc (frame);
9571 scope_breakpoint->loc->address
a6d9a66e
UW
9572 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9573 scope_breakpoint->loc->requested_address,
edb3359d
DJ
9574 scope_breakpoint->type);
9575 }
d983da9c
DJ
9576 }
9577
c906108c 9578 /* Now set up the breakpoint. */
3a5c3e22
PA
9579
9580 w = XCNEW (struct watchpoint);
9581 b = &w->base;
348d480f 9582 if (use_mask)
3a5c3e22
PA
9583 init_raw_breakpoint_without_location (b, NULL, bp_type,
9584 &masked_watchpoint_breakpoint_ops);
348d480f 9585 else
3a5c3e22
PA
9586 init_raw_breakpoint_without_location (b, NULL, bp_type,
9587 &watchpoint_breakpoint_ops);
37e4754d 9588 b->thread = thread;
b5de0fa7 9589 b->disposition = disp_donttouch;
348d480f 9590 b->pspace = current_program_space;
3a5c3e22
PA
9591 w->exp = exp;
9592 w->exp_valid_block = exp_valid_block;
9593 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
9594 if (just_location)
9595 {
9596 struct type *t = value_type (val);
9597 CORE_ADDR addr = value_as_address (val);
9598 char *name;
9599
9600 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9601 name = type_to_string (t);
9602
3a5c3e22 9603 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 9604 core_addr_to_string (addr));
06a64a0b
TT
9605 xfree (name);
9606
3a5c3e22 9607 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
9608 (int) (exp_end - exp_start), exp_start);
9609
06a64a0b
TT
9610 /* The above expression is in C. */
9611 b->language = language_c;
9612 }
9613 else
3a5c3e22 9614 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
9615
9616 if (use_mask)
9617 {
3a5c3e22 9618 w->hw_wp_mask = mask;
9c06b0b4
TJB
9619 }
9620 else
9621 {
3a5c3e22
PA
9622 w->val = val;
9623 w->val_valid = 1;
9c06b0b4 9624 }
77b06cd7 9625
c906108c
SS
9626 if (cond_start)
9627 b->cond_string = savestring (cond_start, cond_end - cond_start);
9628 else
9629 b->cond_string = 0;
c5aa993b 9630
c906108c 9631 if (frame)
f6bc2008 9632 {
3a5c3e22
PA
9633 w->watchpoint_frame = get_frame_id (frame);
9634 w->watchpoint_thread = inferior_ptid;
f6bc2008 9635 }
c906108c 9636 else
f6bc2008 9637 {
3a5c3e22
PA
9638 w->watchpoint_frame = null_frame_id;
9639 w->watchpoint_thread = null_ptid;
f6bc2008 9640 }
c906108c 9641
d983da9c 9642 if (scope_breakpoint != NULL)
c906108c 9643 {
d983da9c
DJ
9644 /* The scope breakpoint is related to the watchpoint. We will
9645 need to act on them together. */
9646 b->related_breakpoint = scope_breakpoint;
9647 scope_breakpoint->related_breakpoint = b;
c906108c 9648 }
d983da9c 9649
06a64a0b
TT
9650 if (!just_location)
9651 value_free_to_mark (mark);
2d134ed3 9652
a9634178
TJB
9653 TRY_CATCH (e, RETURN_MASK_ALL)
9654 {
9655 /* Finally update the new watchpoint. This creates the locations
9656 that should be inserted. */
3a5c3e22 9657 update_watchpoint (w, 1);
a9634178
TJB
9658 }
9659 if (e.reason < 0)
9660 {
9661 delete_breakpoint (b);
9662 throw_exception (e);
9663 }
9664
3ea46bff 9665 install_breakpoint (internal, b, 1);
c906108c
SS
9666}
9667
e09342b5 9668/* Return count of debug registers needed to watch the given expression.
e09342b5 9669 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9670
c906108c 9671static int
a9634178 9672can_use_hardware_watchpoint (struct value *v)
c906108c
SS
9673{
9674 int found_memory_cnt = 0;
2e70b7b9 9675 struct value *head = v;
c906108c
SS
9676
9677 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9678 if (!can_use_hw_watchpoints)
c906108c 9679 return 0;
c5aa993b 9680
5c44784c
JM
9681 /* Make sure that the value of the expression depends only upon
9682 memory contents, and values computed from them within GDB. If we
9683 find any register references or function calls, we can't use a
9684 hardware watchpoint.
9685
9686 The idea here is that evaluating an expression generates a series
9687 of values, one holding the value of every subexpression. (The
9688 expression a*b+c has five subexpressions: a, b, a*b, c, and
9689 a*b+c.) GDB's values hold almost enough information to establish
9690 the criteria given above --- they identify memory lvalues,
9691 register lvalues, computed values, etcetera. So we can evaluate
9692 the expression, and then scan the chain of values that leaves
9693 behind to decide whether we can detect any possible change to the
9694 expression's final value using only hardware watchpoints.
9695
9696 However, I don't think that the values returned by inferior
9697 function calls are special in any way. So this function may not
9698 notice that an expression involving an inferior function call
9699 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9700 for (; v; v = value_next (v))
c906108c 9701 {
5c44784c 9702 if (VALUE_LVAL (v) == lval_memory)
c906108c 9703 {
8464be76
DJ
9704 if (v != head && value_lazy (v))
9705 /* A lazy memory lvalue in the chain is one that GDB never
9706 needed to fetch; we either just used its address (e.g.,
9707 `a' in `a.b') or we never needed it at all (e.g., `a'
9708 in `a,b'). This doesn't apply to HEAD; if that is
9709 lazy then it was not readable, but watch it anyway. */
5c44784c 9710 ;
53a5351d 9711 else
5c44784c
JM
9712 {
9713 /* Ahh, memory we actually used! Check if we can cover
9714 it with hardware watchpoints. */
df407dfe 9715 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9716
9717 /* We only watch structs and arrays if user asked for it
9718 explicitly, never if they just happen to appear in a
9719 middle of some value chain. */
9720 if (v == head
9721 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9722 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9723 {
42ae5230 9724 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9725 int len;
9726 int num_regs;
9727
a9634178 9728 len = (target_exact_watchpoints
e09342b5
TJB
9729 && is_scalar_type_recursive (vtype))?
9730 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9731
e09342b5
TJB
9732 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9733 if (!num_regs)
2e70b7b9
MS
9734 return 0;
9735 else
e09342b5 9736 found_memory_cnt += num_regs;
2e70b7b9 9737 }
5c44784c 9738 }
c5aa993b 9739 }
5086187c
AC
9740 else if (VALUE_LVAL (v) != not_lval
9741 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9742 return 0; /* These are values from the history (e.g., $1). */
5086187c 9743 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9744 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9745 }
9746
9747 /* The expression itself looks suitable for using a hardware
9748 watchpoint, but give the target machine a chance to reject it. */
9749 return found_memory_cnt;
9750}
9751
8b93c638 9752void
84f4c1fe 9753watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9754{
84f4c1fe 9755 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9756}
9757
9758/* A helper function that looks for an argument at the start of a
9759 string. The argument must also either be at the end of the string,
9760 or be followed by whitespace. Returns 1 if it finds the argument,
9761 0 otherwise. If the argument is found, it updates *STR. */
9762
9763static int
9764check_for_argument (char **str, char *arg, int arg_len)
9765{
9766 if (strncmp (*str, arg, arg_len) == 0
9767 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9768 {
9769 *str += arg_len;
9770 return 1;
9771 }
9772 return 0;
9773}
9774
9775/* A helper function that looks for the "-location" argument and then
9776 calls watch_command_1. */
9777
9778static void
9779watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9780{
9781 int just_location = 0;
9782
9783 if (arg
9784 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9785 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9786 {
e9cafbcc 9787 arg = skip_spaces (arg);
06a64a0b
TT
9788 just_location = 1;
9789 }
9790
84f4c1fe 9791 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9792}
8926118c 9793
c5aa993b 9794static void
fba45db2 9795watch_command (char *arg, int from_tty)
c906108c 9796{
06a64a0b 9797 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9798}
9799
8b93c638 9800void
84f4c1fe 9801rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9802{
84f4c1fe 9803 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9804}
8926118c 9805
c5aa993b 9806static void
fba45db2 9807rwatch_command (char *arg, int from_tty)
c906108c 9808{
06a64a0b 9809 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9810}
9811
8b93c638 9812void
84f4c1fe 9813awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9814{
84f4c1fe 9815 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9816}
8926118c 9817
c5aa993b 9818static void
fba45db2 9819awatch_command (char *arg, int from_tty)
c906108c 9820{
06a64a0b 9821 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9822}
c906108c 9823\f
c5aa993b 9824
43ff13b4 9825/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9826 because it uses the mechanisms of breakpoints. */
9827
bfec99b2
PA
9828struct until_break_command_continuation_args
9829{
9830 struct breakpoint *breakpoint;
9831 struct breakpoint *breakpoint2;
186c406b 9832 int thread_num;
bfec99b2
PA
9833};
9834
43ff13b4 9835/* This function is called by fetch_inferior_event via the
4a64f543 9836 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9837 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9838 command. */
c2c6d25f 9839static void
fa4cd53f 9840until_break_command_continuation (void *arg, int err)
43ff13b4 9841{
bfec99b2
PA
9842 struct until_break_command_continuation_args *a = arg;
9843
9844 delete_breakpoint (a->breakpoint);
9845 if (a->breakpoint2)
9846 delete_breakpoint (a->breakpoint2);
186c406b 9847 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9848}
9849
c906108c 9850void
ae66c1fc 9851until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9852{
9853 struct symtabs_and_lines sals;
9854 struct symtab_and_line sal;
206415a3 9855 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9856 struct breakpoint *breakpoint;
f107f563 9857 struct breakpoint *breakpoint2 = NULL;
c906108c 9858 struct cleanup *old_chain;
186c406b
TT
9859 int thread;
9860 struct thread_info *tp;
c906108c
SS
9861
9862 clear_proceed_status ();
9863
9864 /* Set a breakpoint where the user wants it and at return from
4a64f543 9865 this function. */
c5aa993b 9866
1bfeeb0f 9867 if (last_displayed_sal_is_valid ())
f8eba3c6 9868 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 9869 get_last_displayed_symtab (),
f8eba3c6 9870 get_last_displayed_line ());
c906108c 9871 else
f8eba3c6
TT
9872 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
9873 (struct symtab *) NULL, 0);
c5aa993b 9874
c906108c 9875 if (sals.nelts != 1)
8a3fe4f8 9876 error (_("Couldn't get information on specified line."));
c5aa993b 9877
c906108c 9878 sal = sals.sals[0];
4a64f543 9879 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9880
c906108c 9881 if (*arg)
8a3fe4f8 9882 error (_("Junk at end of arguments."));
c5aa993b 9883
c906108c 9884 resolve_sal_pc (&sal);
c5aa993b 9885
ae66c1fc
EZ
9886 if (anywhere)
9887 /* If the user told us to continue until a specified location,
9888 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9889 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9890 null_frame_id, bp_until);
ae66c1fc 9891 else
4a64f543
MS
9892 /* Otherwise, specify the selected frame, because we want to stop
9893 only at the very same frame. */
a6d9a66e
UW
9894 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9895 get_stack_frame_id (frame),
ae66c1fc 9896 bp_until);
c5aa993b 9897
f107f563 9898 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9899
186c406b
TT
9900 tp = inferior_thread ();
9901 thread = tp->num;
9902
ae66c1fc
EZ
9903 /* Keep within the current frame, or in frames called by the current
9904 one. */
edb3359d
DJ
9905
9906 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9907 {
edb3359d
DJ
9908 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9909 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9910 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9911 sal,
edb3359d 9912 frame_unwind_caller_id (frame),
f107f563
VP
9913 bp_until);
9914 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9915
9916 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9917 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9918 }
c5aa993b 9919
c906108c 9920 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9921
4a64f543
MS
9922 /* If we are running asynchronously, and proceed call above has
9923 actually managed to start the target, arrange for breakpoints to
9924 be deleted when the target stops. Otherwise, we're already
9925 stopped and delete breakpoints via cleanup chain. */
f107f563 9926
8ea051c5 9927 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9928 {
bfec99b2
PA
9929 struct until_break_command_continuation_args *args;
9930 args = xmalloc (sizeof (*args));
f107f563 9931
bfec99b2
PA
9932 args->breakpoint = breakpoint;
9933 args->breakpoint2 = breakpoint2;
186c406b 9934 args->thread_num = thread;
f107f563
VP
9935
9936 discard_cleanups (old_chain);
95e54da7
PA
9937 add_continuation (inferior_thread (),
9938 until_break_command_continuation, args,
604ead4a 9939 xfree);
f107f563
VP
9940 }
9941 else
c5aa993b 9942 do_cleanups (old_chain);
c906108c 9943}
ae66c1fc 9944
c906108c
SS
9945/* This function attempts to parse an optional "if <cond>" clause
9946 from the arg string. If one is not found, it returns NULL.
c5aa993b 9947
c906108c
SS
9948 Else, it returns a pointer to the condition string. (It does not
9949 attempt to evaluate the string against a particular block.) And,
9950 it updates arg to point to the first character following the parsed
4a64f543 9951 if clause in the arg string. */
53a5351d 9952
c906108c 9953static char *
fba45db2 9954ep_parse_optional_if_clause (char **arg)
c906108c 9955{
c5aa993b
JM
9956 char *cond_string;
9957
9958 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9959 return NULL;
c5aa993b 9960
4a64f543 9961 /* Skip the "if" keyword. */
c906108c 9962 (*arg) += 2;
c5aa993b 9963
c906108c 9964 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9965 condition string. */
e9cafbcc 9966 *arg = skip_spaces (*arg);
c906108c 9967 cond_string = *arg;
c5aa993b 9968
4a64f543
MS
9969 /* Assume that the condition occupies the remainder of the arg
9970 string. */
c906108c 9971 (*arg) += strlen (cond_string);
c5aa993b 9972
c906108c
SS
9973 return cond_string;
9974}
c5aa993b 9975
c906108c
SS
9976/* Commands to deal with catching events, such as signals, exceptions,
9977 process start/exit, etc. */
c5aa993b
JM
9978
9979typedef enum
9980{
44feb3ce
TT
9981 catch_fork_temporary, catch_vfork_temporary,
9982 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9983}
9984catch_fork_kind;
9985
c906108c 9986static void
cc59ec59
MS
9987catch_fork_command_1 (char *arg, int from_tty,
9988 struct cmd_list_element *command)
c906108c 9989{
a6d9a66e 9990 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9991 char *cond_string = NULL;
44feb3ce
TT
9992 catch_fork_kind fork_kind;
9993 int tempflag;
9994
9995 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9996 tempflag = (fork_kind == catch_fork_temporary
9997 || fork_kind == catch_vfork_temporary);
c5aa993b 9998
44feb3ce
TT
9999 if (!arg)
10000 arg = "";
e9cafbcc 10001 arg = skip_spaces (arg);
c5aa993b 10002
c906108c 10003 /* The allowed syntax is:
c5aa993b
JM
10004 catch [v]fork
10005 catch [v]fork if <cond>
10006
4a64f543 10007 First, check if there's an if clause. */
c906108c 10008 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 10009
c906108c 10010 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 10011 error (_("Junk at end of arguments."));
c5aa993b 10012
c906108c 10013 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 10014 and enable reporting of such events. */
c5aa993b
JM
10015 switch (fork_kind)
10016 {
44feb3ce
TT
10017 case catch_fork_temporary:
10018 case catch_fork_permanent:
a6d9a66e 10019 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 10020 &catch_fork_breakpoint_ops);
c906108c 10021 break;
44feb3ce
TT
10022 case catch_vfork_temporary:
10023 case catch_vfork_permanent:
a6d9a66e 10024 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 10025 &catch_vfork_breakpoint_ops);
c906108c 10026 break;
c5aa993b 10027 default:
8a3fe4f8 10028 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 10029 break;
c5aa993b 10030 }
c906108c
SS
10031}
10032
10033static void
cc59ec59
MS
10034catch_exec_command_1 (char *arg, int from_tty,
10035 struct cmd_list_element *command)
c906108c 10036{
b4d90040 10037 struct exec_catchpoint *c;
a6d9a66e 10038 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 10039 int tempflag;
c5aa993b 10040 char *cond_string = NULL;
c906108c 10041
44feb3ce
TT
10042 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
10043
10044 if (!arg)
10045 arg = "";
e9cafbcc 10046 arg = skip_spaces (arg);
c906108c
SS
10047
10048 /* The allowed syntax is:
c5aa993b
JM
10049 catch exec
10050 catch exec if <cond>
c906108c 10051
4a64f543 10052 First, check if there's an if clause. */
c906108c
SS
10053 cond_string = ep_parse_optional_if_clause (&arg);
10054
10055 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 10056 error (_("Junk at end of arguments."));
c906108c 10057
b4d90040
PA
10058 c = XNEW (struct exec_catchpoint);
10059 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
10060 &catch_exec_breakpoint_ops);
10061 c->exec_pathname = NULL;
10062
3ea46bff 10063 install_breakpoint (0, &c->base, 1);
c906108c 10064}
c5aa993b 10065
3086aeae 10066static enum print_stop_action
348d480f 10067print_it_exception_catchpoint (bpstat bs)
3086aeae 10068{
79a45e25 10069 struct ui_out *uiout = current_uiout;
348d480f 10070 struct breakpoint *b = bs->breakpoint_at;
ade92717 10071 int bp_temp, bp_throw;
3086aeae 10072
ade92717 10073 annotate_catchpoint (b->number);
3086aeae 10074
ade92717
AR
10075 bp_throw = strstr (b->addr_string, "throw") != NULL;
10076 if (b->loc->address != b->loc->requested_address)
10077 breakpoint_adjustment_warning (b->loc->requested_address,
10078 b->loc->address,
10079 b->number, 1);
df2b6d2d 10080 bp_temp = b->disposition == disp_del;
ade92717
AR
10081 ui_out_text (uiout,
10082 bp_temp ? "Temporary catchpoint "
10083 : "Catchpoint ");
10084 if (!ui_out_is_mi_like_p (uiout))
10085 ui_out_field_int (uiout, "bkptno", b->number);
10086 ui_out_text (uiout,
c0b37c48
AR
10087 bp_throw ? " (exception thrown), "
10088 : " (exception caught), ");
ade92717
AR
10089 if (ui_out_is_mi_like_p (uiout))
10090 {
10091 ui_out_field_string (uiout, "reason",
10092 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10093 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10094 ui_out_field_int (uiout, "bkptno", b->number);
10095 }
3086aeae
DJ
10096 return PRINT_SRC_AND_LOC;
10097}
10098
10099static void
cc59ec59
MS
10100print_one_exception_catchpoint (struct breakpoint *b,
10101 struct bp_location **last_loc)
3086aeae 10102{
79a45b7d 10103 struct value_print_options opts;
79a45e25 10104 struct ui_out *uiout = current_uiout;
cc59ec59 10105
79a45b7d
TT
10106 get_user_print_options (&opts);
10107 if (opts.addressprint)
3086aeae
DJ
10108 {
10109 annotate_field (4);
604133b5
AR
10110 if (b->loc == NULL || b->loc->shlib_disabled)
10111 ui_out_field_string (uiout, "addr", "<PENDING>");
10112 else
5af949e3
UW
10113 ui_out_field_core_addr (uiout, "addr",
10114 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
10115 }
10116 annotate_field (5);
604133b5 10117 if (b->loc)
a6d9a66e 10118 *last_loc = b->loc;
3086aeae
DJ
10119 if (strstr (b->addr_string, "throw") != NULL)
10120 ui_out_field_string (uiout, "what", "exception throw");
10121 else
10122 ui_out_field_string (uiout, "what", "exception catch");
10123}
10124
10125static void
10126print_mention_exception_catchpoint (struct breakpoint *b)
10127{
79a45e25 10128 struct ui_out *uiout = current_uiout;
ade92717
AR
10129 int bp_temp;
10130 int bp_throw;
10131
df2b6d2d 10132 bp_temp = b->disposition == disp_del;
ade92717
AR
10133 bp_throw = strstr (b->addr_string, "throw") != NULL;
10134 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
10135 : _("Catchpoint "));
10136 ui_out_field_int (uiout, "bkptno", b->number);
10137 ui_out_text (uiout, bp_throw ? _(" (throw)")
10138 : _(" (catch)"));
3086aeae
DJ
10139}
10140
6149aea9
PA
10141/* Implement the "print_recreate" breakpoint_ops method for throw and
10142 catch catchpoints. */
10143
10144static void
4a64f543
MS
10145print_recreate_exception_catchpoint (struct breakpoint *b,
10146 struct ui_file *fp)
6149aea9
PA
10147{
10148 int bp_temp;
10149 int bp_throw;
10150
10151 bp_temp = b->disposition == disp_del;
10152 bp_throw = strstr (b->addr_string, "throw") != NULL;
10153 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
10154 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 10155 print_recreate_thread (b, fp);
6149aea9
PA
10156}
10157
2060206e 10158static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
10159
10160static int
10161handle_gnu_v3_exceptions (int tempflag, char *cond_string,
10162 enum exception_event_kind ex_event, int from_tty)
10163{
604133b5
AR
10164 char *trigger_func_name;
10165
3086aeae 10166 if (ex_event == EX_EVENT_CATCH)
604133b5 10167 trigger_func_name = "__cxa_begin_catch";
3086aeae 10168 else
604133b5 10169 trigger_func_name = "__cxa_throw";
3086aeae 10170
8cdf0e15
VP
10171 create_breakpoint (get_current_arch (),
10172 trigger_func_name, cond_string, -1,
10173 0 /* condition and thread are valid. */,
0fb4aa4b 10174 tempflag, bp_breakpoint,
8cdf0e15
VP
10175 0,
10176 AUTO_BOOLEAN_TRUE /* pending */,
10177 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
10178 1 /* enabled */,
10179 0 /* internal */);
3086aeae 10180
3086aeae
DJ
10181 return 1;
10182}
10183
4a64f543 10184/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
10185
10186static void
fba45db2
KB
10187catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
10188 int tempflag, int from_tty)
c906108c 10189{
c5aa993b 10190 char *cond_string = NULL;
c5aa993b 10191
44feb3ce
TT
10192 if (!arg)
10193 arg = "";
e9cafbcc 10194 arg = skip_spaces (arg);
c5aa993b 10195
c906108c
SS
10196 cond_string = ep_parse_optional_if_clause (&arg);
10197
10198 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 10199 error (_("Junk at end of arguments."));
c906108c 10200
059fb39f
PM
10201 if (ex_event != EX_EVENT_THROW
10202 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 10203 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 10204
3086aeae
DJ
10205 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
10206 return;
10207
8a3fe4f8 10208 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
10209}
10210
44feb3ce
TT
10211/* Implementation of "catch catch" command. */
10212
10213static void
10214catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
10215{
10216 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 10217
44feb3ce
TT
10218 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
10219}
10220
10221/* Implementation of "catch throw" command. */
10222
10223static void
10224catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
10225{
10226 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 10227
44feb3ce
TT
10228 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
10229}
10230
9ac4176b 10231void
28010a5d
PA
10232init_ada_exception_breakpoint (struct breakpoint *b,
10233 struct gdbarch *gdbarch,
10234 struct symtab_and_line sal,
10235 char *addr_string,
c0a91b2b 10236 const struct breakpoint_ops *ops,
28010a5d
PA
10237 int tempflag,
10238 int from_tty)
f7f9143b 10239{
f7f9143b
JB
10240 if (from_tty)
10241 {
5af949e3
UW
10242 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
10243 if (!loc_gdbarch)
10244 loc_gdbarch = gdbarch;
10245
6c95b8df
PA
10246 describe_other_breakpoints (loc_gdbarch,
10247 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
10248 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
10249 version for exception catchpoints, because two catchpoints
10250 used for different exception names will use the same address.
10251 In this case, a "breakpoint ... also set at..." warning is
4a64f543 10252 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 10253 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
10254 the user what type of catchpoint it is. The above is good
10255 enough for now, though. */
10256 }
10257
28010a5d 10258 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
10259
10260 b->enable_state = bp_enabled;
10261 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
10262 b->addr_string = addr_string;
10263 b->language = language_ada;
f7f9143b
JB
10264}
10265
a96d9b2e
SDJ
10266/* Splits the argument using space as delimiter. Returns an xmalloc'd
10267 filter list, or NULL if no filtering is required. */
10268static VEC(int) *
10269catch_syscall_split_args (char *arg)
10270{
10271 VEC(int) *result = NULL;
29d0bb3d 10272 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
10273
10274 while (*arg != '\0')
10275 {
10276 int i, syscall_number;
10277 char *endptr;
10278 char cur_name[128];
10279 struct syscall s;
10280
10281 /* Skip whitespace. */
10282 while (isspace (*arg))
10283 arg++;
10284
10285 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
10286 cur_name[i] = arg[i];
10287 cur_name[i] = '\0';
10288 arg += i;
10289
10290 /* Check if the user provided a syscall name or a number. */
10291 syscall_number = (int) strtol (cur_name, &endptr, 0);
10292 if (*endptr == '\0')
bccd0dd2 10293 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
10294 else
10295 {
10296 /* We have a name. Let's check if it's valid and convert it
10297 to a number. */
10298 get_syscall_by_name (cur_name, &s);
10299
10300 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
10301 /* Here we have to issue an error instead of a warning,
10302 because GDB cannot do anything useful if there's no
10303 syscall number to be caught. */
a96d9b2e
SDJ
10304 error (_("Unknown syscall name '%s'."), cur_name);
10305 }
10306
10307 /* Ok, it's valid. */
10308 VEC_safe_push (int, result, s.number);
10309 }
10310
10311 discard_cleanups (cleanup);
10312 return result;
10313}
10314
10315/* Implement the "catch syscall" command. */
10316
10317static void
cc59ec59
MS
10318catch_syscall_command_1 (char *arg, int from_tty,
10319 struct cmd_list_element *command)
a96d9b2e
SDJ
10320{
10321 int tempflag;
10322 VEC(int) *filter;
10323 struct syscall s;
10324 struct gdbarch *gdbarch = get_current_arch ();
10325
10326 /* Checking if the feature if supported. */
10327 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
10328 error (_("The feature 'catch syscall' is not supported on \
ea666128 10329this architecture yet."));
a96d9b2e
SDJ
10330
10331 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
10332
e9cafbcc 10333 arg = skip_spaces (arg);
a96d9b2e
SDJ
10334
10335 /* We need to do this first "dummy" translation in order
10336 to get the syscall XML file loaded or, most important,
10337 to display a warning to the user if there's no XML file
10338 for his/her architecture. */
10339 get_syscall_by_number (0, &s);
10340
10341 /* The allowed syntax is:
10342 catch syscall
10343 catch syscall <name | number> [<name | number> ... <name | number>]
10344
10345 Let's check if there's a syscall name. */
10346
10347 if (arg != NULL)
10348 filter = catch_syscall_split_args (arg);
10349 else
10350 filter = NULL;
10351
10352 create_syscall_event_catchpoint (tempflag, filter,
10353 &catch_syscall_breakpoint_ops);
10354}
10355
c906108c 10356static void
fba45db2 10357catch_command (char *arg, int from_tty)
c906108c 10358{
44feb3ce 10359 error (_("Catch requires an event name."));
c906108c
SS
10360}
10361\f
10362
10363static void
fba45db2 10364tcatch_command (char *arg, int from_tty)
c906108c 10365{
44feb3ce 10366 error (_("Catch requires an event name."));
c906108c
SS
10367}
10368
8a2c437b
TT
10369/* A qsort comparison function that sorts breakpoints in order. */
10370
10371static int
10372compare_breakpoints (const void *a, const void *b)
10373{
10374 const breakpoint_p *ba = a;
10375 uintptr_t ua = (uintptr_t) *ba;
10376 const breakpoint_p *bb = b;
10377 uintptr_t ub = (uintptr_t) *bb;
10378
10379 if ((*ba)->number < (*bb)->number)
10380 return -1;
10381 else if ((*ba)->number > (*bb)->number)
10382 return 1;
10383
10384 /* Now sort by address, in case we see, e..g, two breakpoints with
10385 the number 0. */
10386 if (ua < ub)
10387 return -1;
10388 return ub > ub ? 1 : 0;
10389}
10390
80f8a6eb 10391/* Delete breakpoints by address or line. */
c906108c
SS
10392
10393static void
fba45db2 10394clear_command (char *arg, int from_tty)
c906108c 10395{
8a2c437b 10396 struct breakpoint *b, *prev;
d6e956e5
VP
10397 VEC(breakpoint_p) *found = 0;
10398 int ix;
c906108c
SS
10399 int default_match;
10400 struct symtabs_and_lines sals;
10401 struct symtab_and_line sal;
c906108c 10402 int i;
8a2c437b 10403 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
10404
10405 if (arg)
10406 {
f8eba3c6
TT
10407 sals = decode_line_spec (arg, (DECODE_LINE_FUNFIRSTLINE
10408 | DECODE_LINE_LIST_MODE));
c906108c
SS
10409 default_match = 0;
10410 }
10411 else
10412 {
c5aa993b 10413 sals.sals = (struct symtab_and_line *)
c906108c 10414 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 10415 make_cleanup (xfree, sals.sals);
4a64f543 10416 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
10417
10418 /* Set sal's line, symtab, pc, and pspace to the values
10419 corresponding to the last call to print_frame_info. If the
10420 codepoint is not valid, this will set all the fields to 0. */
10421 get_last_displayed_sal (&sal);
c906108c 10422 if (sal.symtab == 0)
8a3fe4f8 10423 error (_("No source file specified."));
c906108c
SS
10424
10425 sals.sals[0] = sal;
10426 sals.nelts = 1;
10427
10428 default_match = 1;
10429 }
10430
4a64f543
MS
10431 /* We don't call resolve_sal_pc here. That's not as bad as it
10432 seems, because all existing breakpoints typically have both
10433 file/line and pc set. So, if clear is given file/line, we can
10434 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10435
10436 We only support clearing given the address explicitly
10437 present in breakpoint table. Say, we've set breakpoint
4a64f543 10438 at file:line. There were several PC values for that file:line,
ed0616c6 10439 due to optimization, all in one block.
4a64f543
MS
10440
10441 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10442 PC corresponding to the same file:line, the breakpoint won't
10443 be cleared. We probably can still clear the breakpoint, but
10444 since the other PC value is never presented to user, user
10445 can only find it by guessing, and it does not seem important
10446 to support that. */
10447
4a64f543
MS
10448 /* For each line spec given, delete bps which correspond to it. Do
10449 it in two passes, solely to preserve the current behavior that
10450 from_tty is forced true if we delete more than one
10451 breakpoint. */
c906108c 10452
80f8a6eb 10453 found = NULL;
8a2c437b 10454 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
10455 for (i = 0; i < sals.nelts; i++)
10456 {
4aac40c8
TT
10457 int is_abs, sal_name_len;
10458
c906108c 10459 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
10460 If line given (pc == 0), clear all bpts on specified line.
10461 If defaulting, clear all bpts on default line
c906108c 10462 or at default pc.
c5aa993b
JM
10463
10464 defaulting sal.pc != 0 tests to do
10465
10466 0 1 pc
10467 1 1 pc _and_ line
10468 0 0 line
10469 1 0 <can't happen> */
c906108c
SS
10470
10471 sal = sals.sals[i];
4aac40c8
TT
10472 is_abs = sal.symtab == NULL ? 1 : IS_ABSOLUTE_PATH (sal.symtab->filename);
10473 sal_name_len = is_abs ? 0 : strlen (sal.symtab->filename);
c906108c 10474
4a64f543 10475 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 10476 ALL_BREAKPOINTS (b)
c5aa993b 10477 {
0d381245 10478 int match = 0;
4a64f543 10479 /* Are we going to delete b? */
cc60f2e3 10480 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
10481 {
10482 struct bp_location *loc = b->loc;
10483 for (; loc; loc = loc->next)
10484 {
f8eba3c6
TT
10485 /* If the user specified file:line, don't allow a PC
10486 match. This matches historical gdb behavior. */
10487 int pc_match = (!sal.explicit_line
10488 && sal.pc
10489 && (loc->pspace == sal.pspace)
10490 && (loc->address == sal.pc)
10491 && (!section_is_overlay (loc->section)
10492 || loc->section == sal.section));
4aac40c8
TT
10493 int line_match = 0;
10494
10495 if ((default_match || sal.explicit_line)
10496 && loc->source_file != NULL
10497 && sal.symtab != NULL
10498 && sal.pspace == loc->pspace
10499 && loc->line_number == sal.line)
10500 {
10501 if (filename_cmp (loc->source_file,
10502 sal.symtab->filename) == 0)
10503 line_match = 1;
10504 else if (!IS_ABSOLUTE_PATH (sal.symtab->filename)
10505 && compare_filenames_for_search (loc->source_file,
10506 sal.symtab->filename,
10507 sal_name_len))
10508 line_match = 1;
10509 }
10510
0d381245
VP
10511 if (pc_match || line_match)
10512 {
10513 match = 1;
10514 break;
10515 }
10516 }
10517 }
10518
10519 if (match)
d6e956e5 10520 VEC_safe_push(breakpoint_p, found, b);
c906108c 10521 }
80f8a6eb 10522 }
8a2c437b 10523
80f8a6eb 10524 /* Now go thru the 'found' chain and delete them. */
d6e956e5 10525 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
10526 {
10527 if (arg)
8a3fe4f8 10528 error (_("No breakpoint at %s."), arg);
80f8a6eb 10529 else
8a3fe4f8 10530 error (_("No breakpoint at this line."));
80f8a6eb 10531 }
c906108c 10532
8a2c437b
TT
10533 /* Remove duplicates from the vec. */
10534 qsort (VEC_address (breakpoint_p, found),
10535 VEC_length (breakpoint_p, found),
10536 sizeof (breakpoint_p),
10537 compare_breakpoints);
10538 prev = VEC_index (breakpoint_p, found, 0);
10539 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
10540 {
10541 if (b == prev)
10542 {
10543 VEC_ordered_remove (breakpoint_p, found, ix);
10544 --ix;
10545 }
10546 }
10547
d6e956e5 10548 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 10549 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10550 if (from_tty)
a3f17187 10551 {
d6e956e5 10552 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
10553 printf_unfiltered (_("Deleted breakpoint "));
10554 else
10555 printf_unfiltered (_("Deleted breakpoints "));
10556 }
80f8a6eb 10557 breakpoints_changed ();
d6e956e5
VP
10558
10559 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 10560 {
c5aa993b 10561 if (from_tty)
d6e956e5
VP
10562 printf_unfiltered ("%d ", b->number);
10563 delete_breakpoint (b);
c906108c 10564 }
80f8a6eb
MS
10565 if (from_tty)
10566 putchar_unfiltered ('\n');
8a2c437b
TT
10567
10568 do_cleanups (cleanups);
c906108c
SS
10569}
10570\f
10571/* Delete breakpoint in BS if they are `delete' breakpoints and
10572 all breakpoints that are marked for deletion, whether hit or not.
10573 This is called after any breakpoint is hit, or after errors. */
10574
10575void
fba45db2 10576breakpoint_auto_delete (bpstat bs)
c906108c 10577{
35df4500 10578 struct breakpoint *b, *b_tmp;
c906108c
SS
10579
10580 for (; bs; bs = bs->next)
f431efe5
PA
10581 if (bs->breakpoint_at
10582 && bs->breakpoint_at->disposition == disp_del
c906108c 10583 && bs->stop)
f431efe5 10584 delete_breakpoint (bs->breakpoint_at);
c906108c 10585
35df4500 10586 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10587 {
b5de0fa7 10588 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10589 delete_breakpoint (b);
10590 }
c906108c
SS
10591}
10592
4a64f543
MS
10593/* A comparison function for bp_location AP and BP being interfaced to
10594 qsort. Sort elements primarily by their ADDRESS (no matter what
10595 does breakpoint_address_is_meaningful say for its OWNER),
10596 secondarily by ordering first bp_permanent OWNERed elements and
10597 terciarily just ensuring the array is sorted stable way despite
e5dd4106 10598 qsort being an unstable algorithm. */
876fa593
JK
10599
10600static int
494cfb0f 10601bp_location_compare (const void *ap, const void *bp)
876fa593 10602{
494cfb0f
JK
10603 struct bp_location *a = *(void **) ap;
10604 struct bp_location *b = *(void **) bp;
2bdf28a0 10605 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10606 int a_perm = a->owner->enable_state == bp_permanent;
10607 int b_perm = b->owner->enable_state == bp_permanent;
10608
10609 if (a->address != b->address)
10610 return (a->address > b->address) - (a->address < b->address);
10611
dea2aa5f
LM
10612 /* Sort locations at the same address by their pspace number, keeping
10613 locations of the same inferior (in a multi-inferior environment)
10614 grouped. */
10615
10616 if (a->pspace->num != b->pspace->num)
10617 return ((a->pspace->num > b->pspace->num)
10618 - (a->pspace->num < b->pspace->num));
10619
876fa593
JK
10620 /* Sort permanent breakpoints first. */
10621 if (a_perm != b_perm)
10622 return (a_perm < b_perm) - (a_perm > b_perm);
10623
c56a97f9
JK
10624 /* Make the internal GDB representation stable across GDB runs
10625 where A and B memory inside GDB can differ. Breakpoint locations of
10626 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
10627
10628 if (a->owner->number != b->owner->number)
c56a97f9
JK
10629 return ((a->owner->number > b->owner->number)
10630 - (a->owner->number < b->owner->number));
876fa593
JK
10631
10632 return (a > b) - (a < b);
10633}
10634
876fa593 10635/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10636 bp_location_shadow_len_after_address_max according to the current
10637 content of the bp_location array. */
f7545552
TT
10638
10639static void
876fa593 10640bp_location_target_extensions_update (void)
f7545552 10641{
876fa593
JK
10642 struct bp_location *bl, **blp_tmp;
10643
10644 bp_location_placed_address_before_address_max = 0;
10645 bp_location_shadow_len_after_address_max = 0;
10646
10647 ALL_BP_LOCATIONS (bl, blp_tmp)
10648 {
10649 CORE_ADDR start, end, addr;
10650
10651 if (!bp_location_has_shadow (bl))
10652 continue;
10653
10654 start = bl->target_info.placed_address;
10655 end = start + bl->target_info.shadow_len;
10656
10657 gdb_assert (bl->address >= start);
10658 addr = bl->address - start;
10659 if (addr > bp_location_placed_address_before_address_max)
10660 bp_location_placed_address_before_address_max = addr;
10661
10662 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10663
10664 gdb_assert (bl->address < end);
10665 addr = end - bl->address;
10666 if (addr > bp_location_shadow_len_after_address_max)
10667 bp_location_shadow_len_after_address_max = addr;
10668 }
f7545552
TT
10669}
10670
1e4d1764
YQ
10671/* Download tracepoint locations if they haven't been. */
10672
10673static void
10674download_tracepoint_locations (void)
10675{
10676 struct bp_location *bl, **blp_tmp;
10677 struct cleanup *old_chain;
10678
10679 if (!target_can_download_tracepoint ())
10680 return;
10681
10682 old_chain = save_current_space_and_thread ();
10683
10684 ALL_BP_LOCATIONS (bl, blp_tmp)
10685 {
10686 struct tracepoint *t;
10687
10688 if (!is_tracepoint (bl->owner))
10689 continue;
10690
10691 if ((bl->owner->type == bp_fast_tracepoint
10692 ? !may_insert_fast_tracepoints
10693 : !may_insert_tracepoints))
10694 continue;
10695
10696 /* In tracepoint, locations are _never_ duplicated, so
10697 should_be_inserted is equivalent to
10698 unduplicated_should_be_inserted. */
10699 if (!should_be_inserted (bl) || bl->inserted)
10700 continue;
10701
10702 switch_to_program_space_and_thread (bl->pspace);
10703
10704 target_download_tracepoint (bl);
10705
10706 bl->inserted = 1;
10707 t = (struct tracepoint *) bl->owner;
10708 t->number_on_target = bl->owner->number;
10709 }
10710
10711 do_cleanups (old_chain);
10712}
10713
934709f0
PW
10714/* Swap the insertion/duplication state between two locations. */
10715
10716static void
10717swap_insertion (struct bp_location *left, struct bp_location *right)
10718{
10719 const int left_inserted = left->inserted;
10720 const int left_duplicate = left->duplicate;
10721 const struct bp_target_info left_target_info = left->target_info;
10722
1e4d1764
YQ
10723 /* Locations of tracepoints can never be duplicated. */
10724 if (is_tracepoint (left->owner))
10725 gdb_assert (!left->duplicate);
10726 if (is_tracepoint (right->owner))
10727 gdb_assert (!right->duplicate);
10728
934709f0
PW
10729 left->inserted = right->inserted;
10730 left->duplicate = right->duplicate;
10731 left->target_info = right->target_info;
10732 right->inserted = left_inserted;
10733 right->duplicate = left_duplicate;
10734 right->target_info = left_target_info;
10735}
10736
4cd9bd08 10737/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10738 into the inferior, only remove already-inserted locations that no
10739 longer should be inserted. Functions that delete a breakpoint or
10740 breakpoints should pass false, so that deleting a breakpoint
10741 doesn't have the side effect of inserting the locations of other
10742 breakpoints that are marked not-inserted, but should_be_inserted
10743 returns true on them.
10744
10745 This behaviour is useful is situations close to tear-down -- e.g.,
10746 after an exec, while the target still has execution, but breakpoint
10747 shadows of the previous executable image should *NOT* be restored
10748 to the new image; or before detaching, where the target still has
10749 execution and wants to delete breakpoints from GDB's lists, and all
10750 breakpoints had already been removed from the inferior. */
10751
0d381245 10752static void
b60e7edf 10753update_global_location_list (int should_insert)
0d381245 10754{
74960c60 10755 struct breakpoint *b;
876fa593 10756 struct bp_location **locp, *loc;
f7545552
TT
10757 struct cleanup *cleanups;
10758
2d134ed3
PA
10759 /* Used in the duplicates detection below. When iterating over all
10760 bp_locations, points to the first bp_location of a given address.
10761 Breakpoints and watchpoints of different types are never
10762 duplicates of each other. Keep one pointer for each type of
10763 breakpoint/watchpoint, so we only need to loop over all locations
10764 once. */
10765 struct bp_location *bp_loc_first; /* breakpoint */
10766 struct bp_location *wp_loc_first; /* hardware watchpoint */
10767 struct bp_location *awp_loc_first; /* access watchpoint */
10768 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10769
4a64f543
MS
10770 /* Saved former bp_location array which we compare against the newly
10771 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10772 struct bp_location **old_location, **old_locp;
10773 unsigned old_location_count;
10774
10775 old_location = bp_location;
10776 old_location_count = bp_location_count;
10777 bp_location = NULL;
10778 bp_location_count = 0;
10779 cleanups = make_cleanup (xfree, old_location);
0d381245 10780
74960c60 10781 ALL_BREAKPOINTS (b)
876fa593
JK
10782 for (loc = b->loc; loc; loc = loc->next)
10783 bp_location_count++;
10784
10785 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10786 locp = bp_location;
10787 ALL_BREAKPOINTS (b)
10788 for (loc = b->loc; loc; loc = loc->next)
10789 *locp++ = loc;
10790 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10791 bp_location_compare);
876fa593
JK
10792
10793 bp_location_target_extensions_update ();
74960c60 10794
4a64f543
MS
10795 /* Identify bp_location instances that are no longer present in the
10796 new list, and therefore should be freed. Note that it's not
10797 necessary that those locations should be removed from inferior --
10798 if there's another location at the same address (previously
10799 marked as duplicate), we don't need to remove/insert the
10800 location.
876fa593 10801
4a64f543
MS
10802 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10803 and former bp_location array state respectively. */
876fa593
JK
10804
10805 locp = bp_location;
10806 for (old_locp = old_location; old_locp < old_location + old_location_count;
10807 old_locp++)
74960c60 10808 {
876fa593 10809 struct bp_location *old_loc = *old_locp;
c7d46a38 10810 struct bp_location **loc2p;
876fa593 10811
e5dd4106 10812 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 10813 not, we have to free it. */
c7d46a38 10814 int found_object = 0;
20874c92
VP
10815 /* Tells if the location should remain inserted in the target. */
10816 int keep_in_target = 0;
10817 int removed = 0;
876fa593 10818
4a64f543
MS
10819 /* Skip LOCP entries which will definitely never be needed.
10820 Stop either at or being the one matching OLD_LOC. */
876fa593 10821 while (locp < bp_location + bp_location_count
c7d46a38 10822 && (*locp)->address < old_loc->address)
876fa593 10823 locp++;
c7d46a38
PA
10824
10825 for (loc2p = locp;
10826 (loc2p < bp_location + bp_location_count
10827 && (*loc2p)->address == old_loc->address);
10828 loc2p++)
10829 {
10830 if (*loc2p == old_loc)
10831 {
10832 found_object = 1;
10833 break;
10834 }
10835 }
74960c60 10836
4a64f543
MS
10837 /* If this location is no longer present, and inserted, look if
10838 there's maybe a new location at the same address. If so,
10839 mark that one inserted, and don't remove this one. This is
10840 needed so that we don't have a time window where a breakpoint
10841 at certain location is not inserted. */
74960c60 10842
876fa593 10843 if (old_loc->inserted)
0d381245 10844 {
4a64f543
MS
10845 /* If the location is inserted now, we might have to remove
10846 it. */
74960c60 10847
876fa593 10848 if (found_object && should_be_inserted (old_loc))
74960c60 10849 {
4a64f543
MS
10850 /* The location is still present in the location list,
10851 and still should be inserted. Don't do anything. */
20874c92 10852 keep_in_target = 1;
74960c60
VP
10853 }
10854 else
10855 {
4a64f543
MS
10856 /* The location is either no longer present, or got
10857 disabled. See if there's another location at the
10858 same address, in which case we don't need to remove
10859 this one from the target. */
876fa593 10860
2bdf28a0 10861 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10862 if (breakpoint_address_is_meaningful (old_loc->owner))
10863 {
876fa593 10864 for (loc2p = locp;
c7d46a38
PA
10865 (loc2p < bp_location + bp_location_count
10866 && (*loc2p)->address == old_loc->address);
876fa593
JK
10867 loc2p++)
10868 {
10869 struct bp_location *loc2 = *loc2p;
10870
2d134ed3 10871 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 10872 {
85d721b8
PA
10873 /* Read watchpoint locations are switched to
10874 access watchpoints, if the former are not
10875 supported, but the latter are. */
10876 if (is_hardware_watchpoint (old_loc->owner))
10877 {
10878 gdb_assert (is_hardware_watchpoint (loc2->owner));
10879 loc2->watchpoint_type = old_loc->watchpoint_type;
10880 }
10881
934709f0
PW
10882 /* loc2 is a duplicated location. We need to check
10883 if it should be inserted in case it will be
10884 unduplicated. */
10885 if (loc2 != old_loc
10886 && unduplicated_should_be_inserted (loc2))
c7d46a38 10887 {
934709f0 10888 swap_insertion (old_loc, loc2);
c7d46a38
PA
10889 keep_in_target = 1;
10890 break;
10891 }
876fa593
JK
10892 }
10893 }
10894 }
74960c60
VP
10895 }
10896
20874c92
VP
10897 if (!keep_in_target)
10898 {
876fa593 10899 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10900 {
4a64f543
MS
10901 /* This is just about all we can do. We could keep
10902 this location on the global list, and try to
10903 remove it next time, but there's no particular
10904 reason why we will succeed next time.
20874c92 10905
4a64f543
MS
10906 Note that at this point, old_loc->owner is still
10907 valid, as delete_breakpoint frees the breakpoint
10908 only after calling us. */
3e43a32a
MS
10909 printf_filtered (_("warning: Error removing "
10910 "breakpoint %d\n"),
876fa593 10911 old_loc->owner->number);
20874c92
VP
10912 }
10913 removed = 1;
10914 }
0d381245 10915 }
74960c60
VP
10916
10917 if (!found_object)
1c5cfe86 10918 {
db82e815
PA
10919 if (removed && non_stop
10920 && breakpoint_address_is_meaningful (old_loc->owner)
10921 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10922 {
db82e815
PA
10923 /* This location was removed from the target. In
10924 non-stop mode, a race condition is possible where
10925 we've removed a breakpoint, but stop events for that
10926 breakpoint are already queued and will arrive later.
10927 We apply an heuristic to be able to distinguish such
10928 SIGTRAPs from other random SIGTRAPs: we keep this
10929 breakpoint location for a bit, and will retire it
10930 after we see some number of events. The theory here
10931 is that reporting of events should, "on the average",
10932 be fair, so after a while we'll see events from all
10933 threads that have anything of interest, and no longer
10934 need to keep this breakpoint location around. We
10935 don't hold locations forever so to reduce chances of
10936 mistaking a non-breakpoint SIGTRAP for a breakpoint
10937 SIGTRAP.
10938
10939 The heuristic failing can be disastrous on
10940 decr_pc_after_break targets.
10941
10942 On decr_pc_after_break targets, like e.g., x86-linux,
10943 if we fail to recognize a late breakpoint SIGTRAP,
10944 because events_till_retirement has reached 0 too
10945 soon, we'll fail to do the PC adjustment, and report
10946 a random SIGTRAP to the user. When the user resumes
10947 the inferior, it will most likely immediately crash
2dec564e 10948 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10949 corrupted, because of being resumed e.g., in the
10950 middle of a multi-byte instruction, or skipped a
10951 one-byte instruction. This was actually seen happen
10952 on native x86-linux, and should be less rare on
10953 targets that do not support new thread events, like
10954 remote, due to the heuristic depending on
10955 thread_count.
10956
10957 Mistaking a random SIGTRAP for a breakpoint trap
10958 causes similar symptoms (PC adjustment applied when
10959 it shouldn't), but then again, playing with SIGTRAPs
10960 behind the debugger's back is asking for trouble.
10961
10962 Since hardware watchpoint traps are always
10963 distinguishable from other traps, so we don't need to
10964 apply keep hardware watchpoint moribund locations
10965 around. We simply always ignore hardware watchpoint
10966 traps we can no longer explain. */
10967
876fa593
JK
10968 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10969 old_loc->owner = NULL;
20874c92 10970
876fa593 10971 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10972 }
10973 else
f431efe5
PA
10974 {
10975 old_loc->owner = NULL;
10976 decref_bp_location (&old_loc);
10977 }
20874c92 10978 }
74960c60 10979 }
1c5cfe86 10980
348d480f
PA
10981 /* Rescan breakpoints at the same address and section, marking the
10982 first one as "first" and any others as "duplicates". This is so
10983 that the bpt instruction is only inserted once. If we have a
10984 permanent breakpoint at the same place as BPT, make that one the
10985 official one, and the rest as duplicates. Permanent breakpoints
10986 are sorted first for the same address.
10987
10988 Do the same for hardware watchpoints, but also considering the
10989 watchpoint's type (regular/access/read) and length. */
10990
10991 bp_loc_first = NULL;
10992 wp_loc_first = NULL;
10993 awp_loc_first = NULL;
10994 rwp_loc_first = NULL;
10995 ALL_BP_LOCATIONS (loc, locp)
10996 {
10997 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10998 non-NULL. */
348d480f 10999 struct bp_location **loc_first_p;
d3fbdd86 11000 b = loc->owner;
348d480f 11001
f8eba3c6 11002 if (!should_be_inserted (loc)
348d480f 11003 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
11004 /* Don't detect duplicate for tracepoint locations because they are
11005 never duplicated. See the comments in field `duplicate' of
11006 `struct bp_location'. */
348d480f
PA
11007 || is_tracepoint (b))
11008 continue;
11009
11010 /* Permanent breakpoint should always be inserted. */
11011 if (b->enable_state == bp_permanent && ! loc->inserted)
11012 internal_error (__FILE__, __LINE__,
11013 _("allegedly permanent breakpoint is not "
11014 "actually inserted"));
11015
11016 if (b->type == bp_hardware_watchpoint)
11017 loc_first_p = &wp_loc_first;
11018 else if (b->type == bp_read_watchpoint)
11019 loc_first_p = &rwp_loc_first;
11020 else if (b->type == bp_access_watchpoint)
11021 loc_first_p = &awp_loc_first;
11022 else
11023 loc_first_p = &bp_loc_first;
11024
11025 if (*loc_first_p == NULL
11026 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11027 || !breakpoint_locations_match (loc, *loc_first_p))
11028 {
11029 *loc_first_p = loc;
11030 loc->duplicate = 0;
11031 continue;
11032 }
11033
934709f0
PW
11034
11035 /* This and the above ensure the invariant that the first location
11036 is not duplicated, and is the inserted one.
11037 All following are marked as duplicated, and are not inserted. */
11038 if (loc->inserted)
11039 swap_insertion (loc, *loc_first_p);
348d480f
PA
11040 loc->duplicate = 1;
11041
11042 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
11043 && b->enable_state != bp_permanent)
11044 internal_error (__FILE__, __LINE__,
11045 _("another breakpoint was inserted on top of "
11046 "a permanent breakpoint"));
11047 }
11048
11049 if (breakpoints_always_inserted_mode () && should_insert
11050 && (have_live_inferiors ()
11051 || (gdbarch_has_global_breakpoints (target_gdbarch))))
11052 insert_breakpoint_locations ();
11053
1e4d1764
YQ
11054 if (should_insert)
11055 download_tracepoint_locations ();
11056
348d480f
PA
11057 do_cleanups (cleanups);
11058}
11059
11060void
11061breakpoint_retire_moribund (void)
11062{
11063 struct bp_location *loc;
11064 int ix;
11065
11066 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
11067 if (--(loc->events_till_retirement) == 0)
11068 {
11069 decref_bp_location (&loc);
11070 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
11071 --ix;
11072 }
11073}
11074
11075static void
11076update_global_location_list_nothrow (int inserting)
11077{
bfd189b1 11078 volatile struct gdb_exception e;
348d480f
PA
11079
11080 TRY_CATCH (e, RETURN_MASK_ERROR)
11081 update_global_location_list (inserting);
11082}
11083
11084/* Clear BKP from a BPS. */
11085
11086static void
11087bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
11088{
11089 bpstat bs;
11090
11091 for (bs = bps; bs; bs = bs->next)
11092 if (bs->breakpoint_at == bpt)
11093 {
11094 bs->breakpoint_at = NULL;
11095 bs->old_val = NULL;
11096 /* bs->commands will be freed later. */
11097 }
11098}
11099
11100/* Callback for iterate_over_threads. */
11101static int
11102bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11103{
11104 struct breakpoint *bpt = data;
11105
11106 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11107 return 0;
11108}
11109
11110/* Helper for breakpoint and tracepoint breakpoint_ops->mention
11111 callbacks. */
11112
11113static void
11114say_where (struct breakpoint *b)
11115{
79a45e25 11116 struct ui_out *uiout = current_uiout;
348d480f
PA
11117 struct value_print_options opts;
11118
11119 get_user_print_options (&opts);
11120
11121 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11122 single string. */
11123 if (b->loc == NULL)
11124 {
11125 printf_filtered (_(" (%s) pending."), b->addr_string);
11126 }
11127 else
11128 {
f8eba3c6 11129 if (opts.addressprint || b->loc->source_file == NULL)
348d480f
PA
11130 {
11131 printf_filtered (" at ");
11132 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
11133 gdb_stdout);
11134 }
f8eba3c6
TT
11135 if (b->loc->source_file)
11136 {
11137 /* If there is a single location, we can print the location
11138 more nicely. */
11139 if (b->loc->next == NULL)
11140 printf_filtered (": file %s, line %d.",
11141 b->loc->source_file, b->loc->line_number);
11142 else
11143 /* This is not ideal, but each location may have a
11144 different file name, and this at least reflects the
11145 real situation somewhat. */
11146 printf_filtered (": %s.", b->addr_string);
11147 }
348d480f
PA
11148
11149 if (b->loc->next)
11150 {
11151 struct bp_location *loc = b->loc;
11152 int n = 0;
11153 for (; loc; loc = loc->next)
11154 ++n;
11155 printf_filtered (" (%d locations)", n);
11156 }
11157 }
11158}
11159
348d480f
PA
11160/* Default bp_location_ops methods. */
11161
11162static void
11163bp_location_dtor (struct bp_location *self)
11164{
11165 xfree (self->cond);
11166 xfree (self->function_name);
f8eba3c6 11167 xfree (self->source_file);
348d480f
PA
11168}
11169
11170static const struct bp_location_ops bp_location_ops =
11171{
11172 bp_location_dtor
11173};
11174
2060206e
PA
11175/* Default breakpoint_ops methods all breakpoint_ops ultimately
11176 inherit from. */
348d480f 11177
2060206e
PA
11178static void
11179base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
11180{
11181 decref_counted_command_line (&self->commands);
11182 xfree (self->cond_string);
348d480f 11183 xfree (self->addr_string);
f8eba3c6 11184 xfree (self->filter);
348d480f 11185 xfree (self->addr_string_range_end);
348d480f
PA
11186}
11187
2060206e
PA
11188static struct bp_location *
11189base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
11190{
11191 struct bp_location *loc;
11192
11193 loc = XNEW (struct bp_location);
11194 init_bp_location (loc, &bp_location_ops, self);
11195 return loc;
11196}
11197
2060206e
PA
11198static void
11199base_breakpoint_re_set (struct breakpoint *b)
11200{
11201 /* Nothing to re-set. */
11202}
11203
11204#define internal_error_pure_virtual_called() \
11205 gdb_assert_not_reached ("pure virtual function called")
11206
11207static int
11208base_breakpoint_insert_location (struct bp_location *bl)
11209{
11210 internal_error_pure_virtual_called ();
11211}
11212
11213static int
11214base_breakpoint_remove_location (struct bp_location *bl)
11215{
11216 internal_error_pure_virtual_called ();
11217}
11218
11219static int
11220base_breakpoint_breakpoint_hit (const struct bp_location *bl,
11221 struct address_space *aspace,
09ac7c10
TT
11222 CORE_ADDR bp_addr,
11223 const struct target_waitstatus *ws)
2060206e
PA
11224{
11225 internal_error_pure_virtual_called ();
11226}
11227
11228static void
11229base_breakpoint_check_status (bpstat bs)
11230{
11231 /* Always stop. */
11232}
11233
11234/* A "works_in_software_mode" breakpoint_ops method that just internal
11235 errors. */
11236
11237static int
11238base_breakpoint_works_in_software_mode (const struct breakpoint *b)
11239{
11240 internal_error_pure_virtual_called ();
11241}
11242
11243/* A "resources_needed" breakpoint_ops method that just internal
11244 errors. */
11245
11246static int
11247base_breakpoint_resources_needed (const struct bp_location *bl)
11248{
11249 internal_error_pure_virtual_called ();
11250}
11251
11252static enum print_stop_action
11253base_breakpoint_print_it (bpstat bs)
11254{
11255 internal_error_pure_virtual_called ();
11256}
11257
11258static void
11259base_breakpoint_print_one_detail (const struct breakpoint *self,
11260 struct ui_out *uiout)
11261{
11262 /* nothing */
11263}
11264
11265static void
11266base_breakpoint_print_mention (struct breakpoint *b)
11267{
11268 internal_error_pure_virtual_called ();
11269}
11270
11271static void
11272base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
11273{
11274 internal_error_pure_virtual_called ();
11275}
11276
983af33b
SDJ
11277static void
11278base_breakpoint_create_sals_from_address (char **arg,
11279 struct linespec_result *canonical,
11280 enum bptype type_wanted,
11281 char *addr_start,
11282 char **copy_arg)
11283{
11284 internal_error_pure_virtual_called ();
11285}
11286
11287static void
11288base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
11289 struct linespec_result *c,
11290 struct linespec_sals *lsal,
11291 char *cond_string,
11292 enum bptype type_wanted,
11293 enum bpdisp disposition,
11294 int thread,
11295 int task, int ignore_count,
11296 const struct breakpoint_ops *o,
11297 int from_tty, int enabled,
11298 int internal)
11299{
11300 internal_error_pure_virtual_called ();
11301}
11302
11303static void
11304base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
11305 struct symtabs_and_lines *sals)
11306{
11307 internal_error_pure_virtual_called ();
11308}
11309
2060206e
PA
11310static struct breakpoint_ops base_breakpoint_ops =
11311{
11312 base_breakpoint_dtor,
11313 base_breakpoint_allocate_location,
11314 base_breakpoint_re_set,
11315 base_breakpoint_insert_location,
11316 base_breakpoint_remove_location,
11317 base_breakpoint_breakpoint_hit,
11318 base_breakpoint_check_status,
11319 base_breakpoint_resources_needed,
11320 base_breakpoint_works_in_software_mode,
11321 base_breakpoint_print_it,
11322 NULL,
11323 base_breakpoint_print_one_detail,
11324 base_breakpoint_print_mention,
983af33b
SDJ
11325 base_breakpoint_print_recreate,
11326 base_breakpoint_create_sals_from_address,
11327 base_breakpoint_create_breakpoints_sal,
11328 base_breakpoint_decode_linespec,
2060206e
PA
11329};
11330
11331/* Default breakpoint_ops methods. */
11332
11333static void
348d480f
PA
11334bkpt_re_set (struct breakpoint *b)
11335{
06edf0c0
PA
11336 /* FIXME: is this still reachable? */
11337 if (b->addr_string == NULL)
11338 {
11339 /* Anything without a string can't be re-set. */
348d480f 11340 delete_breakpoint (b);
06edf0c0 11341 return;
348d480f 11342 }
06edf0c0
PA
11343
11344 breakpoint_re_set_default (b);
348d480f
PA
11345}
11346
2060206e 11347static int
348d480f
PA
11348bkpt_insert_location (struct bp_location *bl)
11349{
11350 if (bl->loc_type == bp_loc_hardware_breakpoint)
11351 return target_insert_hw_breakpoint (bl->gdbarch,
11352 &bl->target_info);
11353 else
11354 return target_insert_breakpoint (bl->gdbarch,
11355 &bl->target_info);
11356}
11357
2060206e 11358static int
348d480f
PA
11359bkpt_remove_location (struct bp_location *bl)
11360{
11361 if (bl->loc_type == bp_loc_hardware_breakpoint)
11362 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11363 else
11364 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
11365}
11366
2060206e 11367static int
348d480f 11368bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
11369 struct address_space *aspace, CORE_ADDR bp_addr,
11370 const struct target_waitstatus *ws)
348d480f
PA
11371{
11372 struct breakpoint *b = bl->owner;
11373
09ac7c10
TT
11374 if (ws->kind != TARGET_WAITKIND_STOPPED
11375 || ws->value.sig != TARGET_SIGNAL_TRAP)
11376 return 0;
11377
348d480f
PA
11378 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11379 aspace, bp_addr))
11380 return 0;
11381
11382 if (overlay_debugging /* unmapped overlay section */
11383 && section_is_overlay (bl->section)
11384 && !section_is_mapped (bl->section))
11385 return 0;
11386
11387 return 1;
11388}
11389
2060206e 11390static int
348d480f
PA
11391bkpt_resources_needed (const struct bp_location *bl)
11392{
11393 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
11394
11395 return 1;
11396}
11397
2060206e 11398static enum print_stop_action
348d480f
PA
11399bkpt_print_it (bpstat bs)
11400{
348d480f
PA
11401 struct breakpoint *b;
11402 const struct bp_location *bl;
001c8c33 11403 int bp_temp;
79a45e25 11404 struct ui_out *uiout = current_uiout;
348d480f
PA
11405
11406 gdb_assert (bs->bp_location_at != NULL);
11407
11408 bl = bs->bp_location_at;
11409 b = bs->breakpoint_at;
11410
001c8c33
PA
11411 bp_temp = b->disposition == disp_del;
11412 if (bl->address != bl->requested_address)
11413 breakpoint_adjustment_warning (bl->requested_address,
11414 bl->address,
11415 b->number, 1);
11416 annotate_breakpoint (b->number);
11417 if (bp_temp)
11418 ui_out_text (uiout, "\nTemporary breakpoint ");
11419 else
11420 ui_out_text (uiout, "\nBreakpoint ");
11421 if (ui_out_is_mi_like_p (uiout))
348d480f 11422 {
001c8c33
PA
11423 ui_out_field_string (uiout, "reason",
11424 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11425 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 11426 }
001c8c33
PA
11427 ui_out_field_int (uiout, "bkptno", b->number);
11428 ui_out_text (uiout, ", ");
06edf0c0 11429
001c8c33 11430 return PRINT_SRC_AND_LOC;
06edf0c0
PA
11431}
11432
2060206e 11433static void
06edf0c0
PA
11434bkpt_print_mention (struct breakpoint *b)
11435{
79a45e25 11436 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
11437 return;
11438
11439 switch (b->type)
11440 {
11441 case bp_breakpoint:
11442 case bp_gnu_ifunc_resolver:
11443 if (b->disposition == disp_del)
11444 printf_filtered (_("Temporary breakpoint"));
11445 else
11446 printf_filtered (_("Breakpoint"));
11447 printf_filtered (_(" %d"), b->number);
11448 if (b->type == bp_gnu_ifunc_resolver)
11449 printf_filtered (_(" at gnu-indirect-function resolver"));
11450 break;
11451 case bp_hardware_breakpoint:
11452 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
11453 break;
11454 }
11455
11456 say_where (b);
11457}
11458
2060206e 11459static void
06edf0c0
PA
11460bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
11461{
11462 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11463 fprintf_unfiltered (fp, "tbreak");
11464 else if (tp->type == bp_breakpoint)
11465 fprintf_unfiltered (fp, "break");
11466 else if (tp->type == bp_hardware_breakpoint
11467 && tp->disposition == disp_del)
11468 fprintf_unfiltered (fp, "thbreak");
11469 else if (tp->type == bp_hardware_breakpoint)
11470 fprintf_unfiltered (fp, "hbreak");
11471 else
11472 internal_error (__FILE__, __LINE__,
11473 _("unhandled breakpoint type %d"), (int) tp->type);
11474
2060206e 11475 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 11476 print_recreate_thread (tp, fp);
06edf0c0
PA
11477}
11478
983af33b
SDJ
11479static void
11480bkpt_create_sals_from_address (char **arg,
11481 struct linespec_result *canonical,
11482 enum bptype type_wanted,
11483 char *addr_start, char **copy_arg)
11484{
11485 create_sals_from_address_default (arg, canonical, type_wanted,
11486 addr_start, copy_arg);
11487}
11488
11489static void
11490bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
11491 struct linespec_result *canonical,
11492 struct linespec_sals *lsal,
11493 char *cond_string,
11494 enum bptype type_wanted,
11495 enum bpdisp disposition,
11496 int thread,
11497 int task, int ignore_count,
11498 const struct breakpoint_ops *ops,
11499 int from_tty, int enabled,
11500 int internal)
11501{
11502 create_breakpoints_sal_default (gdbarch, canonical, lsal,
11503 cond_string, type_wanted,
11504 disposition, thread, task,
11505 ignore_count, ops, from_tty,
11506 enabled, internal);
11507}
11508
11509static void
11510bkpt_decode_linespec (struct breakpoint *b, char **s,
11511 struct symtabs_and_lines *sals)
11512{
11513 decode_linespec_default (b, s, sals);
11514}
11515
06edf0c0
PA
11516/* Virtual table for internal breakpoints. */
11517
11518static void
11519internal_bkpt_re_set (struct breakpoint *b)
11520{
11521 switch (b->type)
11522 {
11523 /* Delete overlay event and longjmp master breakpoints; they
11524 will be reset later by breakpoint_re_set. */
11525 case bp_overlay_event:
11526 case bp_longjmp_master:
11527 case bp_std_terminate_master:
11528 case bp_exception_master:
11529 delete_breakpoint (b);
11530 break;
11531
11532 /* This breakpoint is special, it's set up when the inferior
11533 starts and we really don't want to touch it. */
11534 case bp_shlib_event:
11535
11536 /* Like bp_shlib_event, this breakpoint type is special. Once
11537 it is set up, we do not want to touch it. */
11538 case bp_thread_event:
11539 break;
11540 }
11541}
11542
11543static void
11544internal_bkpt_check_status (bpstat bs)
11545{
a9b3a50f
PA
11546 if (bs->breakpoint_at->type == bp_shlib_event)
11547 {
11548 /* If requested, stop when the dynamic linker notifies GDB of
11549 events. This allows the user to get control and place
11550 breakpoints in initializer routines for dynamically loaded
11551 objects (among other things). */
11552 bs->stop = stop_on_solib_events;
11553 bs->print = stop_on_solib_events;
11554 }
11555 else
11556 bs->stop = 0;
06edf0c0
PA
11557}
11558
11559static enum print_stop_action
11560internal_bkpt_print_it (bpstat bs)
11561{
36dfb11c 11562 struct ui_out *uiout = current_uiout;
06edf0c0 11563 struct breakpoint *b;
06edf0c0 11564
06edf0c0
PA
11565 b = bs->breakpoint_at;
11566
06edf0c0
PA
11567 switch (b->type)
11568 {
348d480f
PA
11569 case bp_shlib_event:
11570 /* Did we stop because the user set the stop_on_solib_events
11571 variable? (If so, we report this as a generic, "Stopped due
11572 to shlib event" message.) */
edcc5120 11573 print_solib_event (0);
348d480f
PA
11574 break;
11575
11576 case bp_thread_event:
11577 /* Not sure how we will get here.
11578 GDB should not stop for these breakpoints. */
11579 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11580 break;
11581
11582 case bp_overlay_event:
11583 /* By analogy with the thread event, GDB should not stop for these. */
11584 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11585 break;
11586
11587 case bp_longjmp_master:
11588 /* These should never be enabled. */
11589 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11590 break;
11591
11592 case bp_std_terminate_master:
11593 /* These should never be enabled. */
11594 printf_filtered (_("std::terminate Master Breakpoint: "
11595 "gdb should not stop!\n"));
348d480f
PA
11596 break;
11597
11598 case bp_exception_master:
11599 /* These should never be enabled. */
11600 printf_filtered (_("Exception Master Breakpoint: "
11601 "gdb should not stop!\n"));
06edf0c0
PA
11602 break;
11603 }
11604
001c8c33 11605 return PRINT_NOTHING;
06edf0c0
PA
11606}
11607
11608static void
11609internal_bkpt_print_mention (struct breakpoint *b)
11610{
11611 /* Nothing to mention. These breakpoints are internal. */
11612}
11613
06edf0c0
PA
11614/* Virtual table for momentary breakpoints */
11615
11616static void
11617momentary_bkpt_re_set (struct breakpoint *b)
11618{
11619 /* Keep temporary breakpoints, which can be encountered when we step
11620 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
11621 Otherwise these should have been blown away via the cleanup chain
11622 or by breakpoint_init_inferior when we rerun the executable. */
11623}
11624
11625static void
11626momentary_bkpt_check_status (bpstat bs)
11627{
11628 /* Nothing. The point of these breakpoints is causing a stop. */
11629}
11630
11631static enum print_stop_action
11632momentary_bkpt_print_it (bpstat bs)
11633{
79a45e25
PA
11634 struct ui_out *uiout = current_uiout;
11635
001c8c33 11636 if (ui_out_is_mi_like_p (uiout))
06edf0c0 11637 {
001c8c33 11638 struct breakpoint *b = bs->breakpoint_at;
348d480f 11639
001c8c33
PA
11640 switch (b->type)
11641 {
11642 case bp_finish:
11643 ui_out_field_string
11644 (uiout, "reason",
11645 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
11646 break;
348d480f 11647
001c8c33
PA
11648 case bp_until:
11649 ui_out_field_string
11650 (uiout, "reason",
11651 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
11652 break;
11653 }
348d480f
PA
11654 }
11655
001c8c33 11656 return PRINT_UNKNOWN;
348d480f
PA
11657}
11658
06edf0c0
PA
11659static void
11660momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 11661{
06edf0c0 11662 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11663}
11664
348d480f 11665/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 11666
348d480f
PA
11667static void
11668tracepoint_re_set (struct breakpoint *b)
11669{
11670 breakpoint_re_set_default (b);
11671}
876fa593 11672
348d480f
PA
11673static int
11674tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
11675 struct address_space *aspace, CORE_ADDR bp_addr,
11676 const struct target_waitstatus *ws)
348d480f
PA
11677{
11678 /* By definition, the inferior does not report stops at
11679 tracepoints. */
11680 return 0;
74960c60
VP
11681}
11682
11683static void
348d480f
PA
11684tracepoint_print_one_detail (const struct breakpoint *self,
11685 struct ui_out *uiout)
74960c60 11686{
d9b3f62e
PA
11687 struct tracepoint *tp = (struct tracepoint *) self;
11688 if (tp->static_trace_marker_id)
348d480f
PA
11689 {
11690 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 11691
348d480f
PA
11692 ui_out_text (uiout, "\tmarker id is ");
11693 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 11694 tp->static_trace_marker_id);
348d480f
PA
11695 ui_out_text (uiout, "\n");
11696 }
0d381245
VP
11697}
11698
a474d7c2 11699static void
348d480f 11700tracepoint_print_mention (struct breakpoint *b)
a474d7c2 11701{
79a45e25 11702 if (ui_out_is_mi_like_p (current_uiout))
348d480f 11703 return;
cc59ec59 11704
348d480f
PA
11705 switch (b->type)
11706 {
11707 case bp_tracepoint:
11708 printf_filtered (_("Tracepoint"));
11709 printf_filtered (_(" %d"), b->number);
11710 break;
11711 case bp_fast_tracepoint:
11712 printf_filtered (_("Fast tracepoint"));
11713 printf_filtered (_(" %d"), b->number);
11714 break;
11715 case bp_static_tracepoint:
11716 printf_filtered (_("Static tracepoint"));
11717 printf_filtered (_(" %d"), b->number);
11718 break;
11719 default:
11720 internal_error (__FILE__, __LINE__,
11721 _("unhandled tracepoint type %d"), (int) b->type);
11722 }
11723
11724 say_where (b);
a474d7c2
PA
11725}
11726
348d480f 11727static void
d9b3f62e 11728tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 11729{
d9b3f62e
PA
11730 struct tracepoint *tp = (struct tracepoint *) self;
11731
11732 if (self->type == bp_fast_tracepoint)
348d480f 11733 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 11734 if (self->type == bp_static_tracepoint)
348d480f 11735 fprintf_unfiltered (fp, "strace");
d9b3f62e 11736 else if (self->type == bp_tracepoint)
348d480f
PA
11737 fprintf_unfiltered (fp, "trace");
11738 else
11739 internal_error (__FILE__, __LINE__,
d9b3f62e 11740 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 11741
d9b3f62e
PA
11742 fprintf_unfiltered (fp, " %s", self->addr_string);
11743 print_recreate_thread (self, fp);
11744
11745 if (tp->pass_count)
11746 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
11747}
11748
983af33b
SDJ
11749static void
11750tracepoint_create_sals_from_address (char **arg,
11751 struct linespec_result *canonical,
11752 enum bptype type_wanted,
11753 char *addr_start, char **copy_arg)
11754{
11755 create_sals_from_address_default (arg, canonical, type_wanted,
11756 addr_start, copy_arg);
11757}
11758
11759static void
11760tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
11761 struct linespec_result *canonical,
11762 struct linespec_sals *lsal,
11763 char *cond_string,
11764 enum bptype type_wanted,
11765 enum bpdisp disposition,
11766 int thread,
11767 int task, int ignore_count,
11768 const struct breakpoint_ops *ops,
11769 int from_tty, int enabled,
11770 int internal)
11771{
11772 create_breakpoints_sal_default (gdbarch, canonical, lsal,
11773 cond_string, type_wanted,
11774 disposition, thread, task,
11775 ignore_count, ops, from_tty,
11776 enabled, internal);
11777}
11778
11779static void
11780tracepoint_decode_linespec (struct breakpoint *b, char **s,
11781 struct symtabs_and_lines *sals)
11782{
11783 decode_linespec_default (b, s, sals);
11784}
11785
2060206e 11786struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 11787
983af33b
SDJ
11788/* The breakpoint_ops structure to be used on static tracepoints with
11789 markers (`-m'). */
11790
11791static void
11792strace_marker_create_sals_from_address (char **arg,
11793 struct linespec_result *canonical,
11794 enum bptype type_wanted,
11795 char *addr_start, char **copy_arg)
11796{
11797 struct linespec_sals lsal;
11798
11799 lsal.sals = decode_static_tracepoint_spec (arg);
11800
11801 *copy_arg = savestring (addr_start, *arg - addr_start);
11802
11803 canonical->addr_string = xstrdup (*copy_arg);
11804 lsal.canonical = xstrdup (*copy_arg);
11805 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
11806}
11807
11808static void
11809strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
11810 struct linespec_result *canonical,
11811 struct linespec_sals *lsal,
11812 char *cond_string,
11813 enum bptype type_wanted,
11814 enum bpdisp disposition,
11815 int thread,
11816 int task, int ignore_count,
11817 const struct breakpoint_ops *ops,
11818 int from_tty, int enabled,
11819 int internal)
11820{
11821 int i;
11822
11823 /* If the user is creating a static tracepoint by marker id
11824 (strace -m MARKER_ID), then store the sals index, so that
11825 breakpoint_re_set can try to match up which of the newly
11826 found markers corresponds to this one, and, don't try to
11827 expand multiple locations for each sal, given than SALS
11828 already should contain all sals for MARKER_ID. */
11829
11830 for (i = 0; i < lsal->sals.nelts; ++i)
11831 {
11832 struct symtabs_and_lines expanded;
11833 struct tracepoint *tp;
11834 struct cleanup *old_chain;
11835 char *addr_string;
11836
11837 expanded.nelts = 1;
11838 expanded.sals = &lsal->sals.sals[i];
11839
11840 addr_string = xstrdup (canonical->addr_string);
11841 old_chain = make_cleanup (xfree, addr_string);
11842
11843 tp = XCNEW (struct tracepoint);
11844 init_breakpoint_sal (&tp->base, gdbarch, expanded,
11845 addr_string, NULL,
11846 cond_string, type_wanted, disposition,
11847 thread, task, ignore_count, ops,
11848 from_tty, enabled, internal,
11849 canonical->special_display);
11850 /* Given that its possible to have multiple markers with
11851 the same string id, if the user is creating a static
11852 tracepoint by marker id ("strace -m MARKER_ID"), then
11853 store the sals index, so that breakpoint_re_set can
11854 try to match up which of the newly found markers
11855 corresponds to this one */
11856 tp->static_trace_marker_id_idx = i;
11857
11858 install_breakpoint (internal, &tp->base, 0);
11859
11860 discard_cleanups (old_chain);
11861 }
11862}
11863
11864static void
11865strace_marker_decode_linespec (struct breakpoint *b, char **s,
11866 struct symtabs_and_lines *sals)
11867{
11868 struct tracepoint *tp = (struct tracepoint *) b;
11869
11870 *sals = decode_static_tracepoint_spec (s);
11871 if (sals->nelts > tp->static_trace_marker_id_idx)
11872 {
11873 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
11874 sals->nelts = 1;
11875 }
11876 else
11877 error (_("marker %s not found"), tp->static_trace_marker_id);
11878}
11879
11880static struct breakpoint_ops strace_marker_breakpoint_ops;
11881
11882static int
11883strace_marker_p (struct breakpoint *b)
11884{
11885 return b->ops == &strace_marker_breakpoint_ops;
11886}
11887
53a5351d 11888/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 11889 structures. */
c906108c
SS
11890
11891void
fba45db2 11892delete_breakpoint (struct breakpoint *bpt)
c906108c 11893{
52f0bd74 11894 struct breakpoint *b;
c906108c 11895
8a3fe4f8 11896 gdb_assert (bpt != NULL);
c906108c 11897
4a64f543
MS
11898 /* Has this bp already been deleted? This can happen because
11899 multiple lists can hold pointers to bp's. bpstat lists are
11900 especial culprits.
11901
11902 One example of this happening is a watchpoint's scope bp. When
11903 the scope bp triggers, we notice that the watchpoint is out of
11904 scope, and delete it. We also delete its scope bp. But the
11905 scope bp is marked "auto-deleting", and is already on a bpstat.
11906 That bpstat is then checked for auto-deleting bp's, which are
11907 deleted.
11908
11909 A real solution to this problem might involve reference counts in
11910 bp's, and/or giving them pointers back to their referencing
11911 bpstat's, and teaching delete_breakpoint to only free a bp's
11912 storage when no more references were extent. A cheaper bandaid
11913 was chosen. */
c906108c
SS
11914 if (bpt->type == bp_none)
11915 return;
11916
4a64f543
MS
11917 /* At least avoid this stale reference until the reference counting
11918 of breakpoints gets resolved. */
d0fb5eae 11919 if (bpt->related_breakpoint != bpt)
e5a0a904 11920 {
d0fb5eae 11921 struct breakpoint *related;
3a5c3e22 11922 struct watchpoint *w;
d0fb5eae
JK
11923
11924 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 11925 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 11926 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
11927 w = (struct watchpoint *) bpt;
11928 else
11929 w = NULL;
11930 if (w != NULL)
11931 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
11932
11933 /* Unlink bpt from the bpt->related_breakpoint ring. */
11934 for (related = bpt; related->related_breakpoint != bpt;
11935 related = related->related_breakpoint);
11936 related->related_breakpoint = bpt->related_breakpoint;
11937 bpt->related_breakpoint = bpt;
e5a0a904
JK
11938 }
11939
a9634178
TJB
11940 /* watch_command_1 creates a watchpoint but only sets its number if
11941 update_watchpoint succeeds in creating its bp_locations. If there's
11942 a problem in that process, we'll be asked to delete the half-created
11943 watchpoint. In that case, don't announce the deletion. */
11944 if (bpt->number)
11945 observer_notify_breakpoint_deleted (bpt);
c906108c 11946
c906108c
SS
11947 if (breakpoint_chain == bpt)
11948 breakpoint_chain = bpt->next;
11949
c906108c
SS
11950 ALL_BREAKPOINTS (b)
11951 if (b->next == bpt)
c5aa993b
JM
11952 {
11953 b->next = bpt->next;
11954 break;
11955 }
c906108c 11956
f431efe5
PA
11957 /* Be sure no bpstat's are pointing at the breakpoint after it's
11958 been freed. */
11959 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 11960 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
11961 pointing at bpt from the stop_bpstat list entirely, as breakpoint
11962 commands are associated with the bpstat; if we remove it here,
11963 then the later call to bpstat_do_actions (&stop_bpstat); in
11964 event-top.c won't do anything, and temporary breakpoints with
11965 commands won't work. */
11966
11967 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11968
4a64f543
MS
11969 /* Now that breakpoint is removed from breakpoint list, update the
11970 global location list. This will remove locations that used to
11971 belong to this breakpoint. Do this before freeing the breakpoint
11972 itself, since remove_breakpoint looks at location's owner. It
11973 might be better design to have location completely
11974 self-contained, but it's not the case now. */
b60e7edf 11975 update_global_location_list (0);
74960c60 11976
348d480f 11977 bpt->ops->dtor (bpt);
4a64f543
MS
11978 /* On the chance that someone will soon try again to delete this
11979 same bp, we mark it as deleted before freeing its storage. */
c906108c 11980 bpt->type = bp_none;
b8c9b27d 11981 xfree (bpt);
c906108c
SS
11982}
11983
4d6140d9
AC
11984static void
11985do_delete_breakpoint_cleanup (void *b)
11986{
11987 delete_breakpoint (b);
11988}
11989
11990struct cleanup *
11991make_cleanup_delete_breakpoint (struct breakpoint *b)
11992{
11993 return make_cleanup (do_delete_breakpoint_cleanup, b);
11994}
11995
51be5b68
PA
11996/* Iterator function to call a user-provided callback function once
11997 for each of B and its related breakpoints. */
11998
11999static void
12000iterate_over_related_breakpoints (struct breakpoint *b,
12001 void (*function) (struct breakpoint *,
12002 void *),
12003 void *data)
12004{
12005 struct breakpoint *related;
12006
12007 related = b;
12008 do
12009 {
12010 struct breakpoint *next;
12011
12012 /* FUNCTION may delete RELATED. */
12013 next = related->related_breakpoint;
12014
12015 if (next == related)
12016 {
12017 /* RELATED is the last ring entry. */
12018 function (related, data);
12019
12020 /* FUNCTION may have deleted it, so we'd never reach back to
12021 B. There's nothing left to do anyway, so just break
12022 out. */
12023 break;
12024 }
12025 else
12026 function (related, data);
12027
12028 related = next;
12029 }
12030 while (related != b);
12031}
95a42b64
TT
12032
12033static void
12034do_delete_breakpoint (struct breakpoint *b, void *ignore)
12035{
12036 delete_breakpoint (b);
12037}
12038
51be5b68
PA
12039/* A callback for map_breakpoint_numbers that calls
12040 delete_breakpoint. */
12041
12042static void
12043do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
12044{
12045 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
12046}
12047
c906108c 12048void
fba45db2 12049delete_command (char *arg, int from_tty)
c906108c 12050{
35df4500 12051 struct breakpoint *b, *b_tmp;
c906108c 12052
ea9365bb
TT
12053 dont_repeat ();
12054
c906108c
SS
12055 if (arg == 0)
12056 {
12057 int breaks_to_delete = 0;
12058
46c6471b
PA
12059 /* Delete all breakpoints if no argument. Do not delete
12060 internal breakpoints, these have to be deleted with an
12061 explicit breakpoint number argument. */
c5aa993b 12062 ALL_BREAKPOINTS (b)
46c6471b 12063 if (user_breakpoint_p (b))
973d738b
DJ
12064 {
12065 breaks_to_delete = 1;
12066 break;
12067 }
c906108c
SS
12068
12069 /* Ask user only if there are some breakpoints to delete. */
12070 if (!from_tty
e2e0b3e5 12071 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 12072 {
35df4500 12073 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 12074 if (user_breakpoint_p (b))
c5aa993b 12075 delete_breakpoint (b);
c906108c
SS
12076 }
12077 }
12078 else
51be5b68 12079 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
12080}
12081
0d381245
VP
12082static int
12083all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 12084{
0d381245 12085 for (; loc; loc = loc->next)
8645ff69
UW
12086 if (!loc->shlib_disabled
12087 && !loc->pspace->executing_startup)
0d381245
VP
12088 return 0;
12089 return 1;
fe3f5fa8
VP
12090}
12091
776592bf
DE
12092/* Subroutine of update_breakpoint_locations to simplify it.
12093 Return non-zero if multiple fns in list LOC have the same name.
12094 Null names are ignored. */
12095
12096static int
12097ambiguous_names_p (struct bp_location *loc)
12098{
12099 struct bp_location *l;
12100 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
12101 (int (*) (const void *,
12102 const void *)) streq,
776592bf
DE
12103 NULL, xcalloc, xfree);
12104
12105 for (l = loc; l != NULL; l = l->next)
12106 {
12107 const char **slot;
12108 const char *name = l->function_name;
12109
12110 /* Allow for some names to be NULL, ignore them. */
12111 if (name == NULL)
12112 continue;
12113
12114 slot = (const char **) htab_find_slot (htab, (const void *) name,
12115 INSERT);
4a64f543
MS
12116 /* NOTE: We can assume slot != NULL here because xcalloc never
12117 returns NULL. */
776592bf
DE
12118 if (*slot != NULL)
12119 {
12120 htab_delete (htab);
12121 return 1;
12122 }
12123 *slot = name;
12124 }
12125
12126 htab_delete (htab);
12127 return 0;
12128}
12129
0fb4aa4b
PA
12130/* When symbols change, it probably means the sources changed as well,
12131 and it might mean the static tracepoint markers are no longer at
12132 the same address or line numbers they used to be at last we
12133 checked. Losing your static tracepoints whenever you rebuild is
12134 undesirable. This function tries to resync/rematch gdb static
12135 tracepoints with the markers on the target, for static tracepoints
12136 that have not been set by marker id. Static tracepoint that have
12137 been set by marker id are reset by marker id in breakpoint_re_set.
12138 The heuristic is:
12139
12140 1) For a tracepoint set at a specific address, look for a marker at
12141 the old PC. If one is found there, assume to be the same marker.
12142 If the name / string id of the marker found is different from the
12143 previous known name, assume that means the user renamed the marker
12144 in the sources, and output a warning.
12145
12146 2) For a tracepoint set at a given line number, look for a marker
12147 at the new address of the old line number. If one is found there,
12148 assume to be the same marker. If the name / string id of the
12149 marker found is different from the previous known name, assume that
12150 means the user renamed the marker in the sources, and output a
12151 warning.
12152
12153 3) If a marker is no longer found at the same address or line, it
12154 may mean the marker no longer exists. But it may also just mean
12155 the code changed a bit. Maybe the user added a few lines of code
12156 that made the marker move up or down (in line number terms). Ask
12157 the target for info about the marker with the string id as we knew
12158 it. If found, update line number and address in the matching
12159 static tracepoint. This will get confused if there's more than one
12160 marker with the same ID (possible in UST, although unadvised
12161 precisely because it confuses tools). */
12162
12163static struct symtab_and_line
12164update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12165{
d9b3f62e 12166 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
12167 struct static_tracepoint_marker marker;
12168 CORE_ADDR pc;
12169 int i;
12170
12171 pc = sal.pc;
12172 if (sal.line)
12173 find_line_pc (sal.symtab, sal.line, &pc);
12174
12175 if (target_static_tracepoint_marker_at (pc, &marker))
12176 {
d9b3f62e 12177 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
12178 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12179 b->number,
d9b3f62e 12180 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 12181
d9b3f62e
PA
12182 xfree (tp->static_trace_marker_id);
12183 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
12184 release_static_tracepoint_marker (&marker);
12185
12186 return sal;
12187 }
12188
12189 /* Old marker wasn't found on target at lineno. Try looking it up
12190 by string ID. */
12191 if (!sal.explicit_pc
12192 && sal.line != 0
12193 && sal.symtab != NULL
d9b3f62e 12194 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
12195 {
12196 VEC(static_tracepoint_marker_p) *markers;
12197
12198 markers
d9b3f62e 12199 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
12200
12201 if (!VEC_empty(static_tracepoint_marker_p, markers))
12202 {
80e1d417 12203 struct symtab_and_line sal2;
0fb4aa4b 12204 struct symbol *sym;
80e1d417 12205 struct static_tracepoint_marker *tpmarker;
79a45e25 12206 struct ui_out *uiout = current_uiout;
0fb4aa4b 12207
80e1d417 12208 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 12209
d9b3f62e 12210 xfree (tp->static_trace_marker_id);
80e1d417 12211 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
12212
12213 warning (_("marker for static tracepoint %d (%s) not "
12214 "found at previous line number"),
d9b3f62e 12215 b->number, tp->static_trace_marker_id);
0fb4aa4b 12216
80e1d417 12217 init_sal (&sal2);
0fb4aa4b 12218
80e1d417 12219 sal2.pc = tpmarker->address;
0fb4aa4b 12220
80e1d417
AS
12221 sal2 = find_pc_line (tpmarker->address, 0);
12222 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
12223 ui_out_text (uiout, "Now in ");
12224 if (sym)
12225 {
12226 ui_out_field_string (uiout, "func",
12227 SYMBOL_PRINT_NAME (sym));
12228 ui_out_text (uiout, " at ");
12229 }
80e1d417 12230 ui_out_field_string (uiout, "file", sal2.symtab->filename);
0fb4aa4b
PA
12231 ui_out_text (uiout, ":");
12232
12233 if (ui_out_is_mi_like_p (uiout))
12234 {
80e1d417 12235 char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b
PA
12236
12237 if (fullname)
12238 ui_out_field_string (uiout, "fullname", fullname);
12239 }
12240
80e1d417 12241 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
12242 ui_out_text (uiout, "\n");
12243
80e1d417 12244 b->loc->line_number = sal2.line;
0fb4aa4b 12245
f8eba3c6 12246 xfree (b->loc->source_file);
0fb4aa4b 12247 if (sym)
80e1d417 12248 b->loc->source_file = xstrdup (sal2.symtab->filename);
0fb4aa4b 12249 else
f8eba3c6 12250 b->loc->source_file = NULL;
0fb4aa4b
PA
12251
12252 xfree (b->addr_string);
12253 b->addr_string = xstrprintf ("%s:%d",
80e1d417 12254 sal2.symtab->filename,
f8eba3c6 12255 b->loc->line_number);
0fb4aa4b
PA
12256
12257 /* Might be nice to check if function changed, and warn if
12258 so. */
12259
80e1d417 12260 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
12261 }
12262 }
12263 return sal;
12264}
12265
8d3788bd
VP
12266/* Returns 1 iff locations A and B are sufficiently same that
12267 we don't need to report breakpoint as changed. */
12268
12269static int
12270locations_are_equal (struct bp_location *a, struct bp_location *b)
12271{
12272 while (a && b)
12273 {
12274 if (a->address != b->address)
12275 return 0;
12276
12277 if (a->shlib_disabled != b->shlib_disabled)
12278 return 0;
12279
12280 if (a->enabled != b->enabled)
12281 return 0;
12282
12283 a = a->next;
12284 b = b->next;
12285 }
12286
12287 if ((a == NULL) != (b == NULL))
12288 return 0;
12289
12290 return 1;
12291}
12292
f1310107
TJB
12293/* Create new breakpoint locations for B (a hardware or software breakpoint)
12294 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
12295 a ranged breakpoint. */
12296
0e30163f 12297void
0d381245 12298update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
12299 struct symtabs_and_lines sals,
12300 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
12301{
12302 int i;
0d381245
VP
12303 struct bp_location *existing_locations = b->loc;
12304
f8eba3c6
TT
12305 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
12306 {
12307 /* Ranged breakpoints have only one start location and one end
12308 location. */
12309 b->enable_state = bp_disabled;
12310 update_global_location_list (1);
12311 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
12312 "multiple locations found\n"),
12313 b->number);
12314 return;
12315 }
f1310107 12316
4a64f543
MS
12317 /* If there's no new locations, and all existing locations are
12318 pending, don't do anything. This optimizes the common case where
12319 all locations are in the same shared library, that was unloaded.
12320 We'd like to retain the location, so that when the library is
12321 loaded again, we don't loose the enabled/disabled status of the
12322 individual locations. */
0d381245 12323 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
12324 return;
12325
fe3f5fa8
VP
12326 b->loc = NULL;
12327
0d381245 12328 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 12329 {
f8eba3c6
TT
12330 struct bp_location *new_loc;
12331
12332 switch_to_program_space_and_thread (sals.sals[i].pspace);
12333
12334 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 12335
0d381245
VP
12336 /* Reparse conditions, they might contain references to the
12337 old symtab. */
12338 if (b->cond_string != NULL)
12339 {
f1310107 12340 char *s;
bfd189b1 12341 volatile struct gdb_exception e;
fe3f5fa8 12342
0d381245
VP
12343 s = b->cond_string;
12344 TRY_CATCH (e, RETURN_MASK_ERROR)
12345 {
12346 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
12347 0);
12348 }
12349 if (e.reason < 0)
12350 {
3e43a32a
MS
12351 warning (_("failed to reevaluate condition "
12352 "for breakpoint %d: %s"),
0d381245
VP
12353 b->number, e.message);
12354 new_loc->enabled = 0;
12355 }
12356 }
fe3f5fa8 12357
f1310107
TJB
12358 if (sals_end.nelts)
12359 {
12360 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
12361
12362 new_loc->length = end - sals.sals[0].pc + 1;
12363 }
0d381245 12364 }
fe3f5fa8 12365
514f746b
AR
12366 /* Update locations of permanent breakpoints. */
12367 if (b->enable_state == bp_permanent)
12368 make_breakpoint_permanent (b);
12369
4a64f543
MS
12370 /* If possible, carry over 'disable' status from existing
12371 breakpoints. */
0d381245
VP
12372 {
12373 struct bp_location *e = existing_locations;
776592bf
DE
12374 /* If there are multiple breakpoints with the same function name,
12375 e.g. for inline functions, comparing function names won't work.
12376 Instead compare pc addresses; this is just a heuristic as things
12377 may have moved, but in practice it gives the correct answer
12378 often enough until a better solution is found. */
12379 int have_ambiguous_names = ambiguous_names_p (b->loc);
12380
0d381245
VP
12381 for (; e; e = e->next)
12382 {
12383 if (!e->enabled && e->function_name)
12384 {
12385 struct bp_location *l = b->loc;
776592bf
DE
12386 if (have_ambiguous_names)
12387 {
12388 for (; l; l = l->next)
f1310107 12389 if (breakpoint_locations_match (e, l))
776592bf
DE
12390 {
12391 l->enabled = 0;
12392 break;
12393 }
12394 }
12395 else
12396 {
12397 for (; l; l = l->next)
12398 if (l->function_name
12399 && strcmp (e->function_name, l->function_name) == 0)
12400 {
12401 l->enabled = 0;
12402 break;
12403 }
12404 }
0d381245
VP
12405 }
12406 }
12407 }
fe3f5fa8 12408
8d3788bd
VP
12409 if (!locations_are_equal (existing_locations, b->loc))
12410 observer_notify_breakpoint_modified (b);
12411
b60e7edf 12412 update_global_location_list (1);
fe3f5fa8
VP
12413}
12414
ef23e705
TJB
12415/* Find the SaL locations corresponding to the given ADDR_STRING.
12416 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12417
12418static struct symtabs_and_lines
12419addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
12420{
12421 char *s;
02d20e4a 12422 struct symtabs_and_lines sals = {0};
f8eba3c6 12423 volatile struct gdb_exception e;
ef23e705 12424
983af33b 12425 gdb_assert (b->ops != NULL);
ef23e705 12426 s = addr_string;
ef23e705
TJB
12427
12428 TRY_CATCH (e, RETURN_MASK_ERROR)
12429 {
983af33b 12430 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
12431 }
12432 if (e.reason < 0)
12433 {
12434 int not_found_and_ok = 0;
12435 /* For pending breakpoints, it's expected that parsing will
12436 fail until the right shared library is loaded. User has
12437 already told to create pending breakpoints and don't need
12438 extra messages. If breakpoint is in bp_shlib_disabled
12439 state, then user already saw the message about that
12440 breakpoint being disabled, and don't want to see more
12441 errors. */
58438ac1 12442 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
12443 && (b->condition_not_parsed
12444 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 12445 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
12446 || b->enable_state == bp_disabled))
12447 not_found_and_ok = 1;
12448
12449 if (!not_found_and_ok)
12450 {
12451 /* We surely don't want to warn about the same breakpoint
12452 10 times. One solution, implemented here, is disable
12453 the breakpoint on error. Another solution would be to
12454 have separate 'warning emitted' flag. Since this
12455 happens only when a binary has changed, I don't know
12456 which approach is better. */
12457 b->enable_state = bp_disabled;
12458 throw_exception (e);
12459 }
12460 }
12461
58438ac1 12462 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 12463 {
f8eba3c6 12464 int i;
ef23e705 12465
f8eba3c6
TT
12466 for (i = 0; i < sals.nelts; ++i)
12467 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
12468 if (b->condition_not_parsed && s && s[0])
12469 {
12470 char *cond_string = 0;
12471 int thread = -1;
12472 int task = 0;
12473
12474 find_condition_and_thread (s, sals.sals[0].pc,
12475 &cond_string, &thread, &task);
12476 if (cond_string)
12477 b->cond_string = cond_string;
12478 b->thread = thread;
12479 b->task = task;
12480 b->condition_not_parsed = 0;
12481 }
12482
983af33b 12483 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 12484 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 12485
58438ac1
TT
12486 *found = 1;
12487 }
12488 else
12489 *found = 0;
ef23e705
TJB
12490
12491 return sals;
12492}
12493
348d480f
PA
12494/* The default re_set method, for typical hardware or software
12495 breakpoints. Reevaluate the breakpoint and recreate its
12496 locations. */
12497
12498static void
28010a5d 12499breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
12500{
12501 int found;
f1310107 12502 struct symtabs_and_lines sals, sals_end;
ef23e705 12503 struct symtabs_and_lines expanded = {0};
f1310107 12504 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
12505
12506 sals = addr_string_to_sals (b, b->addr_string, &found);
12507 if (found)
12508 {
12509 make_cleanup (xfree, sals.sals);
f8eba3c6 12510 expanded = sals;
ef23e705
TJB
12511 }
12512
f1310107
TJB
12513 if (b->addr_string_range_end)
12514 {
12515 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
12516 if (found)
12517 {
12518 make_cleanup (xfree, sals_end.sals);
f8eba3c6 12519 expanded_end = sals_end;
f1310107
TJB
12520 }
12521 }
12522
12523 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
12524}
12525
983af33b
SDJ
12526/* Default method for creating SALs from an address string. It basically
12527 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12528
12529static void
12530create_sals_from_address_default (char **arg,
12531 struct linespec_result *canonical,
12532 enum bptype type_wanted,
12533 char *addr_start, char **copy_arg)
12534{
12535 parse_breakpoint_sals (arg, canonical);
12536}
12537
12538/* Call create_breakpoints_sal for the given arguments. This is the default
12539 function for the `create_breakpoints_sal' method of
12540 breakpoint_ops. */
12541
12542static void
12543create_breakpoints_sal_default (struct gdbarch *gdbarch,
12544 struct linespec_result *canonical,
12545 struct linespec_sals *lsal,
12546 char *cond_string,
12547 enum bptype type_wanted,
12548 enum bpdisp disposition,
12549 int thread,
12550 int task, int ignore_count,
12551 const struct breakpoint_ops *ops,
12552 int from_tty, int enabled,
12553 int internal)
12554{
12555 create_breakpoints_sal (gdbarch, canonical, cond_string,
12556 type_wanted, disposition,
12557 thread, task, ignore_count, ops, from_tty,
12558 enabled, internal);
12559}
12560
12561/* Decode the line represented by S by calling decode_line_full. This is the
12562 default function for the `decode_linespec' method of breakpoint_ops. */
12563
12564static void
12565decode_linespec_default (struct breakpoint *b, char **s,
12566 struct symtabs_and_lines *sals)
12567{
12568 struct linespec_result canonical;
12569
12570 init_linespec_result (&canonical);
12571 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
12572 (struct symtab *) NULL, 0,
12573 &canonical, multiple_symbols_all,
12574 b->filter);
12575
12576 /* We should get 0 or 1 resulting SALs. */
12577 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
12578
12579 if (VEC_length (linespec_sals, canonical.sals) > 0)
12580 {
12581 struct linespec_sals *lsal;
12582
12583 lsal = VEC_index (linespec_sals, canonical.sals, 0);
12584 *sals = lsal->sals;
12585 /* Arrange it so the destructor does not free the
12586 contents. */
12587 lsal->sals.sals = NULL;
12588 }
12589
12590 destroy_linespec_result (&canonical);
12591}
12592
28010a5d
PA
12593/* Prepare the global context for a re-set of breakpoint B. */
12594
12595static struct cleanup *
12596prepare_re_set_context (struct breakpoint *b)
12597{
12598 struct cleanup *cleanups;
12599
12600 input_radix = b->input_radix;
12601 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
12602 if (b->pspace != NULL)
12603 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
12604 set_language (b->language);
12605
12606 return cleanups;
ef23e705
TJB
12607}
12608
c906108c
SS
12609/* Reset a breakpoint given it's struct breakpoint * BINT.
12610 The value we return ends up being the return value from catch_errors.
12611 Unused in this case. */
12612
12613static int
4efb68b1 12614breakpoint_re_set_one (void *bint)
c906108c 12615{
4a64f543 12616 /* Get past catch_errs. */
53a5351d 12617 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 12618 struct cleanup *cleanups;
c906108c 12619
348d480f
PA
12620 cleanups = prepare_re_set_context (b);
12621 b->ops->re_set (b);
12622 do_cleanups (cleanups);
c906108c
SS
12623 return 0;
12624}
12625
69de3c6a 12626/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 12627void
69de3c6a 12628breakpoint_re_set (void)
c906108c 12629{
35df4500 12630 struct breakpoint *b, *b_tmp;
c906108c
SS
12631 enum language save_language;
12632 int save_input_radix;
6c95b8df 12633 struct cleanup *old_chain;
c5aa993b 12634
c906108c
SS
12635 save_language = current_language->la_language;
12636 save_input_radix = input_radix;
6c95b8df
PA
12637 old_chain = save_current_program_space ();
12638
35df4500 12639 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12640 {
4a64f543 12641 /* Format possible error msg. */
fe3f5fa8 12642 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
12643 b->number);
12644 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 12645 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 12646 do_cleanups (cleanups);
c5aa993b 12647 }
c906108c
SS
12648 set_language (save_language);
12649 input_radix = save_input_radix;
e62c965a 12650
0756c555 12651 jit_breakpoint_re_set ();
4efc6507 12652
6c95b8df
PA
12653 do_cleanups (old_chain);
12654
af02033e
PP
12655 create_overlay_event_breakpoint ();
12656 create_longjmp_master_breakpoint ();
12657 create_std_terminate_master_breakpoint ();
186c406b 12658 create_exception_master_breakpoint ();
1bfeeb0f
JL
12659
12660 /* While we're at it, reset the skip list too. */
12661 skip_re_set ();
c906108c
SS
12662}
12663\f
c906108c
SS
12664/* Reset the thread number of this breakpoint:
12665
12666 - If the breakpoint is for all threads, leave it as-is.
4a64f543 12667 - Else, reset it to the current thread for inferior_ptid. */
c906108c 12668void
fba45db2 12669breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
12670{
12671 if (b->thread != -1)
12672 {
39f77062
KB
12673 if (in_thread_list (inferior_ptid))
12674 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
12675
12676 /* We're being called after following a fork. The new fork is
12677 selected as current, and unless this was a vfork will have a
12678 different program space from the original thread. Reset that
12679 as well. */
12680 b->loc->pspace = current_program_space;
c906108c
SS
12681 }
12682}
12683
03ac34d5
MS
12684/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12685 If from_tty is nonzero, it prints a message to that effect,
12686 which ends with a period (no newline). */
12687
c906108c 12688void
fba45db2 12689set_ignore_count (int bptnum, int count, int from_tty)
c906108c 12690{
52f0bd74 12691 struct breakpoint *b;
c906108c
SS
12692
12693 if (count < 0)
12694 count = 0;
12695
12696 ALL_BREAKPOINTS (b)
12697 if (b->number == bptnum)
c5aa993b 12698 {
d77f58be
SS
12699 if (is_tracepoint (b))
12700 {
12701 if (from_tty && count != 0)
12702 printf_filtered (_("Ignore count ignored for tracepoint %d."),
12703 bptnum);
12704 return;
12705 }
12706
c5aa993b 12707 b->ignore_count = count;
221ea385
KS
12708 if (from_tty)
12709 {
12710 if (count == 0)
3e43a32a
MS
12711 printf_filtered (_("Will stop next time "
12712 "breakpoint %d is reached."),
221ea385
KS
12713 bptnum);
12714 else if (count == 1)
a3f17187 12715 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
12716 bptnum);
12717 else
3e43a32a
MS
12718 printf_filtered (_("Will ignore next %d "
12719 "crossings of breakpoint %d."),
221ea385
KS
12720 count, bptnum);
12721 }
c5aa993b 12722 breakpoints_changed ();
8d3788bd 12723 observer_notify_breakpoint_modified (b);
c5aa993b
JM
12724 return;
12725 }
c906108c 12726
8a3fe4f8 12727 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
12728}
12729
c906108c
SS
12730/* Command to set ignore-count of breakpoint N to COUNT. */
12731
12732static void
fba45db2 12733ignore_command (char *args, int from_tty)
c906108c
SS
12734{
12735 char *p = args;
52f0bd74 12736 int num;
c906108c
SS
12737
12738 if (p == 0)
e2e0b3e5 12739 error_no_arg (_("a breakpoint number"));
c5aa993b 12740
c906108c 12741 num = get_number (&p);
5c44784c 12742 if (num == 0)
8a3fe4f8 12743 error (_("bad breakpoint number: '%s'"), args);
c906108c 12744 if (*p == 0)
8a3fe4f8 12745 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
12746
12747 set_ignore_count (num,
12748 longest_to_int (value_as_long (parse_and_eval (p))),
12749 from_tty);
221ea385
KS
12750 if (from_tty)
12751 printf_filtered ("\n");
c906108c
SS
12752}
12753\f
12754/* Call FUNCTION on each of the breakpoints
12755 whose numbers are given in ARGS. */
12756
12757static void
95a42b64
TT
12758map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
12759 void *),
12760 void *data)
c906108c 12761{
52f0bd74
AC
12762 int num;
12763 struct breakpoint *b, *tmp;
11cf8741 12764 int match;
197f0a60 12765 struct get_number_or_range_state state;
c906108c 12766
197f0a60 12767 if (args == 0)
e2e0b3e5 12768 error_no_arg (_("one or more breakpoint numbers"));
c906108c 12769
197f0a60
TT
12770 init_number_or_range (&state, args);
12771
12772 while (!state.finished)
c906108c 12773 {
197f0a60
TT
12774 char *p = state.string;
12775
11cf8741 12776 match = 0;
c5aa993b 12777
197f0a60 12778 num = get_number_or_range (&state);
5c44784c 12779 if (num == 0)
c5aa993b 12780 {
8a3fe4f8 12781 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
12782 }
12783 else
12784 {
12785 ALL_BREAKPOINTS_SAFE (b, tmp)
12786 if (b->number == num)
12787 {
11cf8741 12788 match = 1;
cdac0397 12789 function (b, data);
11cf8741 12790 break;
5c44784c 12791 }
11cf8741 12792 if (match == 0)
a3f17187 12793 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 12794 }
c906108c
SS
12795 }
12796}
12797
0d381245
VP
12798static struct bp_location *
12799find_location_by_number (char *number)
12800{
12801 char *dot = strchr (number, '.');
12802 char *p1;
12803 int bp_num;
12804 int loc_num;
12805 struct breakpoint *b;
12806 struct bp_location *loc;
12807
12808 *dot = '\0';
12809
12810 p1 = number;
197f0a60 12811 bp_num = get_number (&p1);
0d381245
VP
12812 if (bp_num == 0)
12813 error (_("Bad breakpoint number '%s'"), number);
12814
12815 ALL_BREAKPOINTS (b)
12816 if (b->number == bp_num)
12817 {
12818 break;
12819 }
12820
12821 if (!b || b->number != bp_num)
12822 error (_("Bad breakpoint number '%s'"), number);
12823
12824 p1 = dot+1;
197f0a60 12825 loc_num = get_number (&p1);
0d381245
VP
12826 if (loc_num == 0)
12827 error (_("Bad breakpoint location number '%s'"), number);
12828
12829 --loc_num;
12830 loc = b->loc;
12831 for (;loc_num && loc; --loc_num, loc = loc->next)
12832 ;
12833 if (!loc)
12834 error (_("Bad breakpoint location number '%s'"), dot+1);
12835
12836 return loc;
12837}
12838
12839
1900040c
MS
12840/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12841 If from_tty is nonzero, it prints a message to that effect,
12842 which ends with a period (no newline). */
12843
c906108c 12844void
fba45db2 12845disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
12846{
12847 /* Never disable a watchpoint scope breakpoint; we want to
12848 hit them when we leave scope so we can delete both the
12849 watchpoint and its scope breakpoint at that time. */
12850 if (bpt->type == bp_watchpoint_scope)
12851 return;
12852
c2c6d25f 12853 /* You can't disable permanent breakpoints. */
b5de0fa7 12854 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
12855 return;
12856
b5de0fa7 12857 bpt->enable_state = bp_disabled;
c906108c 12858
d248b706
KY
12859 if (target_supports_enable_disable_tracepoint ()
12860 && current_trace_status ()->running && is_tracepoint (bpt))
12861 {
12862 struct bp_location *location;
12863
12864 for (location = bpt->loc; location; location = location->next)
12865 target_disable_tracepoint (location);
12866 }
12867
b60e7edf 12868 update_global_location_list (0);
c906108c 12869
8d3788bd 12870 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12871}
12872
51be5b68
PA
12873/* A callback for iterate_over_related_breakpoints. */
12874
12875static void
12876do_disable_breakpoint (struct breakpoint *b, void *ignore)
12877{
12878 disable_breakpoint (b);
12879}
12880
95a42b64
TT
12881/* A callback for map_breakpoint_numbers that calls
12882 disable_breakpoint. */
12883
12884static void
12885do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
12886{
51be5b68 12887 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
12888}
12889
c906108c 12890static void
fba45db2 12891disable_command (char *args, int from_tty)
c906108c 12892{
c906108c 12893 if (args == 0)
46c6471b
PA
12894 {
12895 struct breakpoint *bpt;
12896
12897 ALL_BREAKPOINTS (bpt)
12898 if (user_breakpoint_p (bpt))
12899 disable_breakpoint (bpt);
12900 }
0d381245
VP
12901 else if (strchr (args, '.'))
12902 {
12903 struct bp_location *loc = find_location_by_number (args);
12904 if (loc)
d248b706
KY
12905 {
12906 loc->enabled = 0;
12907 if (target_supports_enable_disable_tracepoint ()
12908 && current_trace_status ()->running && loc->owner
12909 && is_tracepoint (loc->owner))
12910 target_disable_tracepoint (loc);
12911 }
b60e7edf 12912 update_global_location_list (0);
0d381245 12913 }
c906108c 12914 else
95a42b64 12915 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
12916}
12917
12918static void
816338b5
SS
12919enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
12920 int count)
c906108c 12921{
afe38095 12922 int target_resources_ok;
c906108c
SS
12923
12924 if (bpt->type == bp_hardware_breakpoint)
12925 {
12926 int i;
c5aa993b 12927 i = hw_breakpoint_used_count ();
53a5351d 12928 target_resources_ok =
d92524f1 12929 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 12930 i + 1, 0);
c906108c 12931 if (target_resources_ok == 0)
8a3fe4f8 12932 error (_("No hardware breakpoint support in the target."));
c906108c 12933 else if (target_resources_ok < 0)
8a3fe4f8 12934 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
12935 }
12936
cc60f2e3 12937 if (is_watchpoint (bpt))
c906108c 12938 {
d07205c2
JK
12939 /* Initialize it just to avoid a GCC false warning. */
12940 enum enable_state orig_enable_state = 0;
bfd189b1 12941 volatile struct gdb_exception e;
dde02812
ES
12942
12943 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 12944 {
3a5c3e22
PA
12945 struct watchpoint *w = (struct watchpoint *) bpt;
12946
1e718ff1
TJB
12947 orig_enable_state = bpt->enable_state;
12948 bpt->enable_state = bp_enabled;
3a5c3e22 12949 update_watchpoint (w, 1 /* reparse */);
c906108c 12950 }
dde02812 12951 if (e.reason < 0)
c5aa993b 12952 {
1e718ff1 12953 bpt->enable_state = orig_enable_state;
dde02812
ES
12954 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12955 bpt->number);
12956 return;
c5aa993b 12957 }
c906108c 12958 }
0101ce28 12959
b4c291bb
KH
12960 if (bpt->enable_state != bp_permanent)
12961 bpt->enable_state = bp_enabled;
d248b706
KY
12962
12963 if (target_supports_enable_disable_tracepoint ()
12964 && current_trace_status ()->running && is_tracepoint (bpt))
12965 {
12966 struct bp_location *location;
12967
12968 for (location = bpt->loc; location; location = location->next)
12969 target_enable_tracepoint (location);
12970 }
12971
b4c291bb 12972 bpt->disposition = disposition;
816338b5 12973 bpt->enable_count = count;
b60e7edf 12974 update_global_location_list (1);
b4c291bb
KH
12975 breakpoints_changed ();
12976
8d3788bd 12977 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12978}
12979
fe3f5fa8 12980
c906108c 12981void
fba45db2 12982enable_breakpoint (struct breakpoint *bpt)
c906108c 12983{
816338b5 12984 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
12985}
12986
12987static void
12988do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12989{
12990 enable_breakpoint (bpt);
c906108c
SS
12991}
12992
95a42b64
TT
12993/* A callback for map_breakpoint_numbers that calls
12994 enable_breakpoint. */
12995
12996static void
12997do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12998{
51be5b68 12999 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
13000}
13001
c906108c
SS
13002/* The enable command enables the specified breakpoints (or all defined
13003 breakpoints) so they once again become (or continue to be) effective
1272ad14 13004 in stopping the inferior. */
c906108c 13005
c906108c 13006static void
fba45db2 13007enable_command (char *args, int from_tty)
c906108c 13008{
c906108c 13009 if (args == 0)
46c6471b
PA
13010 {
13011 struct breakpoint *bpt;
13012
13013 ALL_BREAKPOINTS (bpt)
13014 if (user_breakpoint_p (bpt))
13015 enable_breakpoint (bpt);
13016 }
0d381245
VP
13017 else if (strchr (args, '.'))
13018 {
13019 struct bp_location *loc = find_location_by_number (args);
13020 if (loc)
d248b706
KY
13021 {
13022 loc->enabled = 1;
13023 if (target_supports_enable_disable_tracepoint ()
13024 && current_trace_status ()->running && loc->owner
13025 && is_tracepoint (loc->owner))
13026 target_enable_tracepoint (loc);
13027 }
b60e7edf 13028 update_global_location_list (1);
0d381245 13029 }
c906108c 13030 else
95a42b64 13031 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
13032}
13033
816338b5
SS
13034/* This struct packages up disposition data for application to multiple
13035 breakpoints. */
13036
13037struct disp_data
13038{
13039 enum bpdisp disp;
13040 int count;
13041};
13042
c906108c 13043static void
51be5b68
PA
13044do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
13045{
816338b5 13046 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 13047
816338b5 13048 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
13049}
13050
13051static void
13052do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 13053{
816338b5 13054 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
13055
13056 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
13057}
13058
c906108c 13059static void
fba45db2 13060enable_once_command (char *args, int from_tty)
c906108c 13061{
51be5b68 13062 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
13063}
13064
816338b5
SS
13065static void
13066do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
13067{
13068 struct disp_data disp = { disp_disable, *(int *) countptr };
13069
13070 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
13071}
13072
13073static void
13074enable_count_command (char *args, int from_tty)
13075{
13076 int count = get_number (&args);
13077
13078 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
13079}
13080
c906108c 13081static void
51be5b68 13082do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 13083{
816338b5 13084 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
13085
13086 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
13087}
13088
c906108c 13089static void
fba45db2 13090enable_delete_command (char *args, int from_tty)
c906108c 13091{
51be5b68 13092 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
13093}
13094\f
fa8d40ab
JJ
13095static void
13096set_breakpoint_cmd (char *args, int from_tty)
13097{
13098}
13099
13100static void
13101show_breakpoint_cmd (char *args, int from_tty)
13102{
13103}
13104
1f3b5d1b
PP
13105/* Invalidate last known value of any hardware watchpoint if
13106 the memory which that value represents has been written to by
13107 GDB itself. */
13108
13109static void
13110invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
13111 const bfd_byte *data)
13112{
13113 struct breakpoint *bp;
13114
13115 ALL_BREAKPOINTS (bp)
13116 if (bp->enable_state == bp_enabled
3a5c3e22 13117 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 13118 {
3a5c3e22 13119 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 13120
3a5c3e22
PA
13121 if (wp->val_valid && wp->val)
13122 {
13123 struct bp_location *loc;
13124
13125 for (loc = bp->loc; loc != NULL; loc = loc->next)
13126 if (loc->loc_type == bp_loc_hardware_watchpoint
13127 && loc->address + loc->length > addr
13128 && addr + len > loc->address)
13129 {
13130 value_free (wp->val);
13131 wp->val = NULL;
13132 wp->val_valid = 0;
13133 }
13134 }
1f3b5d1b
PP
13135 }
13136}
13137
1bfeeb0f
JL
13138/* Use the last displayed codepoint's values, or nothing
13139 if they aren't valid. */
c906108c
SS
13140
13141struct symtabs_and_lines
f8eba3c6 13142decode_line_spec_1 (char *string, int flags)
c906108c
SS
13143{
13144 struct symtabs_and_lines sals;
cc59ec59 13145
c906108c 13146 if (string == 0)
8a3fe4f8 13147 error (_("Empty line specification."));
1bfeeb0f 13148 if (last_displayed_sal_is_valid ())
f8eba3c6 13149 sals = decode_line_1 (&string, flags,
1bfeeb0f 13150 get_last_displayed_symtab (),
f8eba3c6 13151 get_last_displayed_line ());
c906108c 13152 else
f8eba3c6 13153 sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
c906108c 13154 if (*string)
8a3fe4f8 13155 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
13156 return sals;
13157}
8181d85f
DJ
13158
13159/* Create and insert a raw software breakpoint at PC. Return an
13160 identifier, which should be used to remove the breakpoint later.
13161 In general, places which call this should be using something on the
13162 breakpoint chain instead; this function should be eliminated
13163 someday. */
13164
13165void *
6c95b8df
PA
13166deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
13167 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
13168{
13169 struct bp_target_info *bp_tgt;
13170
6c95b8df 13171 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 13172
6c95b8df 13173 bp_tgt->placed_address_space = aspace;
8181d85f 13174 bp_tgt->placed_address = pc;
6c95b8df 13175
a6d9a66e 13176 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
13177 {
13178 /* Could not insert the breakpoint. */
13179 xfree (bp_tgt);
13180 return NULL;
13181 }
13182
13183 return bp_tgt;
13184}
13185
4a64f543
MS
13186/* Remove a breakpoint BP inserted by
13187 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
13188
13189int
a6d9a66e 13190deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
13191{
13192 struct bp_target_info *bp_tgt = bp;
13193 int ret;
13194
a6d9a66e 13195 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
13196 xfree (bp_tgt);
13197
13198 return ret;
13199}
13200
4a64f543
MS
13201/* One (or perhaps two) breakpoints used for software single
13202 stepping. */
8181d85f
DJ
13203
13204static void *single_step_breakpoints[2];
a6d9a66e 13205static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
13206
13207/* Create and insert a breakpoint for software single step. */
13208
13209void
6c95b8df 13210insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
13211 struct address_space *aspace,
13212 CORE_ADDR next_pc)
8181d85f
DJ
13213{
13214 void **bpt_p;
13215
13216 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
13217 {
13218 bpt_p = &single_step_breakpoints[0];
13219 single_step_gdbarch[0] = gdbarch;
13220 }
8181d85f
DJ
13221 else
13222 {
13223 gdb_assert (single_step_breakpoints[1] == NULL);
13224 bpt_p = &single_step_breakpoints[1];
a6d9a66e 13225 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
13226 }
13227
4a64f543
MS
13228 /* NOTE drow/2006-04-11: A future improvement to this function would
13229 be to only create the breakpoints once, and actually put them on
13230 the breakpoint chain. That would let us use set_raw_breakpoint.
13231 We could adjust the addresses each time they were needed. Doing
13232 this requires corresponding changes elsewhere where single step
13233 breakpoints are handled, however. So, for now, we use this. */
8181d85f 13234
6c95b8df 13235 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 13236 if (*bpt_p == NULL)
5af949e3
UW
13237 error (_("Could not insert single-step breakpoint at %s"),
13238 paddress (gdbarch, next_pc));
8181d85f
DJ
13239}
13240
f02253f1
HZ
13241/* Check if the breakpoints used for software single stepping
13242 were inserted or not. */
13243
13244int
13245single_step_breakpoints_inserted (void)
13246{
13247 return (single_step_breakpoints[0] != NULL
13248 || single_step_breakpoints[1] != NULL);
13249}
13250
8181d85f
DJ
13251/* Remove and delete any breakpoints used for software single step. */
13252
13253void
13254remove_single_step_breakpoints (void)
13255{
13256 gdb_assert (single_step_breakpoints[0] != NULL);
13257
13258 /* See insert_single_step_breakpoint for more about this deprecated
13259 call. */
a6d9a66e
UW
13260 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
13261 single_step_breakpoints[0]);
13262 single_step_gdbarch[0] = NULL;
8181d85f
DJ
13263 single_step_breakpoints[0] = NULL;
13264
13265 if (single_step_breakpoints[1] != NULL)
13266 {
a6d9a66e
UW
13267 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
13268 single_step_breakpoints[1]);
13269 single_step_gdbarch[1] = NULL;
8181d85f
DJ
13270 single_step_breakpoints[1] = NULL;
13271 }
13272}
13273
d03285ec
UW
13274/* Delete software single step breakpoints without removing them from
13275 the inferior. This is intended to be used if the inferior's address
13276 space where they were inserted is already gone, e.g. after exit or
13277 exec. */
13278
13279void
13280cancel_single_step_breakpoints (void)
13281{
13282 int i;
13283
13284 for (i = 0; i < 2; i++)
13285 if (single_step_breakpoints[i])
13286 {
13287 xfree (single_step_breakpoints[i]);
13288 single_step_breakpoints[i] = NULL;
13289 single_step_gdbarch[i] = NULL;
13290 }
13291}
13292
13293/* Detach software single-step breakpoints from INFERIOR_PTID without
13294 removing them. */
13295
13296static void
13297detach_single_step_breakpoints (void)
13298{
13299 int i;
13300
13301 for (i = 0; i < 2; i++)
13302 if (single_step_breakpoints[i])
13303 target_remove_breakpoint (single_step_gdbarch[i],
13304 single_step_breakpoints[i]);
13305}
13306
4a64f543
MS
13307/* Check whether a software single-step breakpoint is inserted at
13308 PC. */
1aafd4da
UW
13309
13310static int
cc59ec59
MS
13311single_step_breakpoint_inserted_here_p (struct address_space *aspace,
13312 CORE_ADDR pc)
1aafd4da
UW
13313{
13314 int i;
13315
13316 for (i = 0; i < 2; i++)
13317 {
13318 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
13319 if (bp_tgt
13320 && breakpoint_address_match (bp_tgt->placed_address_space,
13321 bp_tgt->placed_address,
13322 aspace, pc))
1aafd4da
UW
13323 return 1;
13324 }
13325
13326 return 0;
13327}
13328
a96d9b2e
SDJ
13329/* Returns 0 if 'bp' is NOT a syscall catchpoint,
13330 non-zero otherwise. */
13331static int
13332is_syscall_catchpoint_enabled (struct breakpoint *bp)
13333{
13334 if (syscall_catchpoint_p (bp)
13335 && bp->enable_state != bp_disabled
13336 && bp->enable_state != bp_call_disabled)
13337 return 1;
13338 else
13339 return 0;
13340}
13341
13342int
13343catch_syscall_enabled (void)
13344{
13345 struct inferior *inf = current_inferior ();
13346
13347 return inf->total_syscalls_count != 0;
13348}
13349
13350int
13351catching_syscall_number (int syscall_number)
13352{
13353 struct breakpoint *bp;
13354
13355 ALL_BREAKPOINTS (bp)
13356 if (is_syscall_catchpoint_enabled (bp))
13357 {
be5c67c1
PA
13358 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
13359
13360 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
13361 {
13362 int i, iter;
13363 for (i = 0;
be5c67c1 13364 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
13365 i++)
13366 if (syscall_number == iter)
13367 return 1;
13368 }
13369 else
13370 return 1;
13371 }
13372
13373 return 0;
13374}
13375
13376/* Complete syscall names. Used by "catch syscall". */
13377static char **
13378catch_syscall_completer (struct cmd_list_element *cmd,
13379 char *text, char *word)
13380{
13381 const char **list = get_syscall_names ();
c38eea1a
MS
13382 char **retlist
13383 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 13384
c38eea1a
MS
13385 xfree (list);
13386 return retlist;
a96d9b2e
SDJ
13387}
13388
1042e4c0
SS
13389/* Tracepoint-specific operations. */
13390
13391/* Set tracepoint count to NUM. */
13392static void
13393set_tracepoint_count (int num)
13394{
13395 tracepoint_count = num;
4fa62494 13396 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
13397}
13398
13399void
13400trace_command (char *arg, int from_tty)
13401{
8cdf0e15
VP
13402 if (create_breakpoint (get_current_arch (),
13403 arg,
13404 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
13405 0 /* tempflag */,
13406 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
13407 0 /* Ignore count */,
13408 pending_break_support,
348d480f 13409 &tracepoint_breakpoint_ops,
8cdf0e15 13410 from_tty,
84f4c1fe
PM
13411 1 /* enabled */,
13412 0 /* internal */))
fd9b8c24 13413 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
13414}
13415
7a697b8d
SS
13416void
13417ftrace_command (char *arg, int from_tty)
13418{
8cdf0e15
VP
13419 if (create_breakpoint (get_current_arch (),
13420 arg,
13421 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
13422 0 /* tempflag */,
13423 bp_fast_tracepoint /* type_wanted */,
13424 0 /* Ignore count */,
13425 pending_break_support,
348d480f 13426 &tracepoint_breakpoint_ops,
0fb4aa4b 13427 from_tty,
84f4c1fe
PM
13428 1 /* enabled */,
13429 0 /* internal */))
0fb4aa4b
PA
13430 set_tracepoint_count (breakpoint_count);
13431}
13432
13433/* strace command implementation. Creates a static tracepoint. */
13434
13435void
13436strace_command (char *arg, int from_tty)
13437{
983af33b
SDJ
13438 struct breakpoint_ops *ops;
13439
13440 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13441 or with a normal static tracepoint. */
13442 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
13443 ops = &strace_marker_breakpoint_ops;
13444 else
13445 ops = &tracepoint_breakpoint_ops;
13446
0fb4aa4b
PA
13447 if (create_breakpoint (get_current_arch (),
13448 arg,
13449 NULL, 0, 1 /* parse arg */,
13450 0 /* tempflag */,
13451 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
13452 0 /* Ignore count */,
13453 pending_break_support,
983af33b 13454 ops,
8cdf0e15 13455 from_tty,
84f4c1fe
PM
13456 1 /* enabled */,
13457 0 /* internal */))
fd9b8c24 13458 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
13459}
13460
409873ef
SS
13461/* Set up a fake reader function that gets command lines from a linked
13462 list that was acquired during tracepoint uploading. */
13463
13464static struct uploaded_tp *this_utp;
3149d8c1 13465static int next_cmd;
409873ef
SS
13466
13467static char *
13468read_uploaded_action (void)
13469{
13470 char *rslt;
13471
3149d8c1 13472 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 13473
3149d8c1 13474 next_cmd++;
409873ef
SS
13475
13476 return rslt;
13477}
13478
00bf0b85
SS
13479/* Given information about a tracepoint as recorded on a target (which
13480 can be either a live system or a trace file), attempt to create an
13481 equivalent GDB tracepoint. This is not a reliable process, since
13482 the target does not necessarily have all the information used when
13483 the tracepoint was originally defined. */
13484
d9b3f62e 13485struct tracepoint *
00bf0b85 13486create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 13487{
409873ef 13488 char *addr_str, small_buf[100];
d9b3f62e 13489 struct tracepoint *tp;
fd9b8c24 13490
409873ef
SS
13491 if (utp->at_string)
13492 addr_str = utp->at_string;
13493 else
13494 {
13495 /* In the absence of a source location, fall back to raw
13496 address. Since there is no way to confirm that the address
13497 means the same thing as when the trace was started, warn the
13498 user. */
3e43a32a
MS
13499 warning (_("Uploaded tracepoint %d has no "
13500 "source location, using raw address"),
409873ef
SS
13501 utp->number);
13502 sprintf (small_buf, "*%s", hex_string (utp->addr));
13503 addr_str = small_buf;
13504 }
13505
13506 /* There's not much we can do with a sequence of bytecodes. */
13507 if (utp->cond && !utp->cond_string)
3e43a32a
MS
13508 warning (_("Uploaded tracepoint %d condition "
13509 "has no source form, ignoring it"),
409873ef 13510 utp->number);
d5551862 13511
8cdf0e15 13512 if (!create_breakpoint (get_current_arch (),
409873ef
SS
13513 addr_str,
13514 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 13515 0 /* tempflag */,
0fb4aa4b 13516 utp->type /* type_wanted */,
8cdf0e15
VP
13517 0 /* Ignore count */,
13518 pending_break_support,
348d480f 13519 &tracepoint_breakpoint_ops,
8cdf0e15 13520 0 /* from_tty */,
84f4c1fe
PM
13521 utp->enabled /* enabled */,
13522 0 /* internal */))
fd9b8c24
PA
13523 return NULL;
13524
00bf0b85
SS
13525 set_tracepoint_count (breakpoint_count);
13526
409873ef 13527 /* Get the tracepoint we just created. */
fd9b8c24
PA
13528 tp = get_tracepoint (tracepoint_count);
13529 gdb_assert (tp != NULL);
d5551862 13530
00bf0b85
SS
13531 if (utp->pass > 0)
13532 {
d9b3f62e 13533 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 13534
409873ef 13535 trace_pass_command (small_buf, 0);
00bf0b85
SS
13536 }
13537
409873ef
SS
13538 /* If we have uploaded versions of the original commands, set up a
13539 special-purpose "reader" function and call the usual command line
13540 reader, then pass the result to the breakpoint command-setting
13541 function. */
3149d8c1 13542 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 13543 {
409873ef 13544 struct command_line *cmd_list;
00bf0b85 13545
409873ef 13546 this_utp = utp;
3149d8c1 13547 next_cmd = 0;
d5551862 13548
409873ef
SS
13549 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
13550
d9b3f62e 13551 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 13552 }
3149d8c1
SS
13553 else if (!VEC_empty (char_ptr, utp->actions)
13554 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
13555 warning (_("Uploaded tracepoint %d actions "
13556 "have no source form, ignoring them"),
409873ef 13557 utp->number);
00bf0b85 13558
f196051f
SS
13559 /* Copy any status information that might be available. */
13560 tp->base.hit_count = utp->hit_count;
13561 tp->traceframe_usage = utp->traceframe_usage;
13562
00bf0b85 13563 return tp;
d9b3f62e 13564}
00bf0b85 13565
1042e4c0
SS
13566/* Print information on tracepoint number TPNUM_EXP, or all if
13567 omitted. */
13568
13569static void
e5a67952 13570tracepoints_info (char *args, int from_tty)
1042e4c0 13571{
79a45e25 13572 struct ui_out *uiout = current_uiout;
e5a67952 13573 int num_printed;
1042e4c0 13574
e5a67952 13575 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
13576
13577 if (num_printed == 0)
1042e4c0 13578 {
e5a67952 13579 if (args == NULL || *args == '\0')
d77f58be
SS
13580 ui_out_message (uiout, 0, "No tracepoints.\n");
13581 else
e5a67952 13582 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 13583 }
ad443146
SS
13584
13585 default_collect_info ();
1042e4c0
SS
13586}
13587
4a64f543 13588/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
13589 Not supported by all targets. */
13590static void
13591enable_trace_command (char *args, int from_tty)
13592{
13593 enable_command (args, from_tty);
13594}
13595
4a64f543 13596/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
13597 Not supported by all targets. */
13598static void
13599disable_trace_command (char *args, int from_tty)
13600{
13601 disable_command (args, from_tty);
13602}
13603
4a64f543 13604/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
13605static void
13606delete_trace_command (char *arg, int from_tty)
13607{
35df4500 13608 struct breakpoint *b, *b_tmp;
1042e4c0
SS
13609
13610 dont_repeat ();
13611
13612 if (arg == 0)
13613 {
13614 int breaks_to_delete = 0;
13615
13616 /* Delete all breakpoints if no argument.
13617 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
13618 have to be deleted with an explicit breakpoint number
13619 argument. */
1042e4c0 13620 ALL_TRACEPOINTS (b)
46c6471b 13621 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
13622 {
13623 breaks_to_delete = 1;
13624 break;
13625 }
1042e4c0
SS
13626
13627 /* Ask user only if there are some breakpoints to delete. */
13628 if (!from_tty
13629 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13630 {
35df4500 13631 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13632 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 13633 delete_breakpoint (b);
1042e4c0
SS
13634 }
13635 }
13636 else
51be5b68 13637 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
13638}
13639
197f0a60
TT
13640/* Helper function for trace_pass_command. */
13641
13642static void
d9b3f62e 13643trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 13644{
d9b3f62e
PA
13645 tp->pass_count = count;
13646 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
13647 if (from_tty)
13648 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 13649 tp->base.number, count);
197f0a60
TT
13650}
13651
1042e4c0
SS
13652/* Set passcount for tracepoint.
13653
13654 First command argument is passcount, second is tracepoint number.
13655 If tracepoint number omitted, apply to most recently defined.
13656 Also accepts special argument "all". */
13657
13658static void
13659trace_pass_command (char *args, int from_tty)
13660{
d9b3f62e 13661 struct tracepoint *t1;
1042e4c0 13662 unsigned int count;
1042e4c0
SS
13663
13664 if (args == 0 || *args == 0)
3e43a32a
MS
13665 error (_("passcount command requires an "
13666 "argument (count + optional TP num)"));
1042e4c0 13667
4a64f543 13668 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
13669
13670 while (*args && isspace ((int) *args))
13671 args++;
13672
13673 if (*args && strncasecmp (args, "all", 3) == 0)
13674 {
d9b3f62e
PA
13675 struct breakpoint *b;
13676
1042e4c0 13677 args += 3; /* Skip special argument "all". */
1042e4c0
SS
13678 if (*args)
13679 error (_("Junk at end of arguments."));
1042e4c0 13680
d9b3f62e 13681 ALL_TRACEPOINTS (b)
197f0a60 13682 {
d9b3f62e 13683 t1 = (struct tracepoint *) b;
197f0a60
TT
13684 trace_pass_set_count (t1, count, from_tty);
13685 }
13686 }
13687 else if (*args == '\0')
1042e4c0 13688 {
197f0a60 13689 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 13690 if (t1)
197f0a60
TT
13691 trace_pass_set_count (t1, count, from_tty);
13692 }
13693 else
13694 {
13695 struct get_number_or_range_state state;
13696
13697 init_number_or_range (&state, args);
13698 while (!state.finished)
1042e4c0 13699 {
197f0a60
TT
13700 t1 = get_tracepoint_by_number (&args, &state, 1);
13701 if (t1)
13702 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
13703 }
13704 }
1042e4c0
SS
13705}
13706
d9b3f62e 13707struct tracepoint *
1042e4c0
SS
13708get_tracepoint (int num)
13709{
13710 struct breakpoint *t;
13711
13712 ALL_TRACEPOINTS (t)
13713 if (t->number == num)
d9b3f62e 13714 return (struct tracepoint *) t;
1042e4c0
SS
13715
13716 return NULL;
13717}
13718
d5551862
SS
13719/* Find the tracepoint with the given target-side number (which may be
13720 different from the tracepoint number after disconnecting and
13721 reconnecting). */
13722
d9b3f62e 13723struct tracepoint *
d5551862
SS
13724get_tracepoint_by_number_on_target (int num)
13725{
d9b3f62e 13726 struct breakpoint *b;
d5551862 13727
d9b3f62e
PA
13728 ALL_TRACEPOINTS (b)
13729 {
13730 struct tracepoint *t = (struct tracepoint *) b;
13731
13732 if (t->number_on_target == num)
13733 return t;
13734 }
d5551862
SS
13735
13736 return NULL;
13737}
13738
1042e4c0 13739/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
13740 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13741 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 13742 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 13743struct tracepoint *
197f0a60
TT
13744get_tracepoint_by_number (char **arg,
13745 struct get_number_or_range_state *state,
13746 int optional_p)
1042e4c0
SS
13747{
13748 extern int tracepoint_count;
13749 struct breakpoint *t;
13750 int tpnum;
13751 char *instring = arg == NULL ? NULL : *arg;
13752
197f0a60
TT
13753 if (state)
13754 {
13755 gdb_assert (!state->finished);
13756 tpnum = get_number_or_range (state);
13757 }
13758 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
13759 {
13760 if (optional_p)
13761 tpnum = tracepoint_count;
13762 else
13763 error_no_arg (_("tracepoint number"));
13764 }
13765 else
197f0a60 13766 tpnum = get_number (arg);
1042e4c0
SS
13767
13768 if (tpnum <= 0)
13769 {
13770 if (instring && *instring)
13771 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
13772 instring);
13773 else
3e43a32a
MS
13774 printf_filtered (_("Tracepoint argument missing "
13775 "and no previous tracepoint\n"));
1042e4c0
SS
13776 return NULL;
13777 }
13778
13779 ALL_TRACEPOINTS (t)
13780 if (t->number == tpnum)
13781 {
d9b3f62e 13782 return (struct tracepoint *) t;
1042e4c0
SS
13783 }
13784
1042e4c0
SS
13785 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
13786 return NULL;
13787}
13788
d9b3f62e
PA
13789void
13790print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
13791{
13792 if (b->thread != -1)
13793 fprintf_unfiltered (fp, " thread %d", b->thread);
13794
13795 if (b->task != 0)
13796 fprintf_unfiltered (fp, " task %d", b->task);
13797
13798 fprintf_unfiltered (fp, "\n");
13799}
13800
6149aea9
PA
13801/* Save information on user settable breakpoints (watchpoints, etc) to
13802 a new script file named FILENAME. If FILTER is non-NULL, call it
13803 on each breakpoint and only include the ones for which it returns
13804 non-zero. */
13805
1042e4c0 13806static void
6149aea9
PA
13807save_breakpoints (char *filename, int from_tty,
13808 int (*filter) (const struct breakpoint *))
1042e4c0
SS
13809{
13810 struct breakpoint *tp;
6149aea9 13811 int any = 0;
a7bdde9e 13812 char *pathname;
1042e4c0 13813 struct cleanup *cleanup;
a7bdde9e 13814 struct ui_file *fp;
6149aea9 13815 int extra_trace_bits = 0;
1042e4c0 13816
6149aea9
PA
13817 if (filename == 0 || *filename == 0)
13818 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
13819
13820 /* See if we have anything to save. */
6149aea9 13821 ALL_BREAKPOINTS (tp)
1042e4c0 13822 {
6149aea9 13823 /* Skip internal and momentary breakpoints. */
09d682a4 13824 if (!user_breakpoint_p (tp))
6149aea9
PA
13825 continue;
13826
13827 /* If we have a filter, only save the breakpoints it accepts. */
13828 if (filter && !filter (tp))
13829 continue;
13830
13831 any = 1;
13832
13833 if (is_tracepoint (tp))
13834 {
13835 extra_trace_bits = 1;
13836
13837 /* We can stop searching. */
13838 break;
13839 }
1042e4c0 13840 }
6149aea9
PA
13841
13842 if (!any)
1042e4c0 13843 {
6149aea9 13844 warning (_("Nothing to save."));
1042e4c0
SS
13845 return;
13846 }
13847
6149aea9 13848 pathname = tilde_expand (filename);
1042e4c0 13849 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 13850 fp = gdb_fopen (pathname, "w");
059fb39f 13851 if (!fp)
6149aea9
PA
13852 error (_("Unable to open file '%s' for saving (%s)"),
13853 filename, safe_strerror (errno));
a7bdde9e 13854 make_cleanup_ui_file_delete (fp);
8bf6485c 13855
6149aea9
PA
13856 if (extra_trace_bits)
13857 save_trace_state_variables (fp);
8bf6485c 13858
6149aea9 13859 ALL_BREAKPOINTS (tp)
1042e4c0 13860 {
6149aea9 13861 /* Skip internal and momentary breakpoints. */
09d682a4 13862 if (!user_breakpoint_p (tp))
6149aea9 13863 continue;
8bf6485c 13864
6149aea9
PA
13865 /* If we have a filter, only save the breakpoints it accepts. */
13866 if (filter && !filter (tp))
13867 continue;
13868
348d480f 13869 tp->ops->print_recreate (tp, fp);
1042e4c0 13870
6149aea9
PA
13871 /* Note, we can't rely on tp->number for anything, as we can't
13872 assume the recreated breakpoint numbers will match. Use $bpnum
13873 instead. */
13874
13875 if (tp->cond_string)
13876 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
13877
13878 if (tp->ignore_count)
13879 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
13880
a7bdde9e 13881 if (tp->commands)
1042e4c0 13882 {
a7bdde9e
VP
13883 volatile struct gdb_exception ex;
13884
6149aea9 13885 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 13886
79a45e25 13887 ui_out_redirect (current_uiout, fp);
14dba4b4 13888 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 13889 {
79a45e25 13890 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 13891 }
79a45e25 13892 ui_out_redirect (current_uiout, NULL);
1042e4c0 13893
a7bdde9e
VP
13894 if (ex.reason < 0)
13895 throw_exception (ex);
1042e4c0 13896
a7bdde9e 13897 fprintf_unfiltered (fp, " end\n");
1042e4c0 13898 }
6149aea9
PA
13899
13900 if (tp->enable_state == bp_disabled)
13901 fprintf_unfiltered (fp, "disable\n");
13902
13903 /* If this is a multi-location breakpoint, check if the locations
13904 should be individually disabled. Watchpoint locations are
13905 special, and not user visible. */
13906 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13907 {
13908 struct bp_location *loc;
13909 int n = 1;
13910
13911 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
13912 if (!loc->enabled)
13913 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
13914 }
1042e4c0 13915 }
8bf6485c 13916
6149aea9 13917 if (extra_trace_bits && *default_collect)
8bf6485c
SS
13918 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
13919
1042e4c0
SS
13920 do_cleanups (cleanup);
13921 if (from_tty)
6149aea9
PA
13922 printf_filtered (_("Saved to file '%s'.\n"), filename);
13923}
13924
13925/* The `save breakpoints' command. */
13926
13927static void
13928save_breakpoints_command (char *args, int from_tty)
13929{
13930 save_breakpoints (args, from_tty, NULL);
13931}
13932
13933/* The `save tracepoints' command. */
13934
13935static void
13936save_tracepoints_command (char *args, int from_tty)
13937{
13938 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
13939}
13940
13941/* Create a vector of all tracepoints. */
13942
13943VEC(breakpoint_p) *
eeae04df 13944all_tracepoints (void)
1042e4c0
SS
13945{
13946 VEC(breakpoint_p) *tp_vec = 0;
13947 struct breakpoint *tp;
13948
13949 ALL_TRACEPOINTS (tp)
13950 {
13951 VEC_safe_push (breakpoint_p, tp_vec, tp);
13952 }
13953
13954 return tp_vec;
13955}
13956
c906108c 13957\f
4a64f543
MS
13958/* This help string is used for the break, hbreak, tbreak and thbreak
13959 commands. It is defined as a macro to prevent duplication.
13960 COMMAND should be a string constant containing the name of the
13961 command. */
31e2b00f
AS
13962#define BREAK_ARGS_HELP(command) \
13963command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
13964LOCATION may be a line number, function name, or \"*\" and an address.\n\
13965If a line number is specified, break at start of code for that line.\n\
13966If a function is specified, break at start of code for that function.\n\
13967If an address is specified, break at that exact address.\n\
dc10affe
PA
13968With no LOCATION, uses current execution address of the selected\n\
13969stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
13970\n\
13971THREADNUM is the number from \"info threads\".\n\
13972CONDITION is a boolean expression.\n\
13973\n\
d41c0fc8
PA
13974Multiple breakpoints at one place are permitted, and useful if their\n\
13975conditions are different.\n\
31e2b00f
AS
13976\n\
13977Do \"help breakpoints\" for info on other commands dealing with breakpoints."
13978
44feb3ce
TT
13979/* List of subcommands for "catch". */
13980static struct cmd_list_element *catch_cmdlist;
13981
13982/* List of subcommands for "tcatch". */
13983static struct cmd_list_element *tcatch_cmdlist;
13984
9ac4176b 13985void
44feb3ce
TT
13986add_catch_command (char *name, char *docstring,
13987 void (*sfunc) (char *args, int from_tty,
13988 struct cmd_list_element *command),
a96d9b2e
SDJ
13989 char **(*completer) (struct cmd_list_element *cmd,
13990 char *text, char *word),
44feb3ce
TT
13991 void *user_data_catch,
13992 void *user_data_tcatch)
13993{
13994 struct cmd_list_element *command;
13995
13996 command = add_cmd (name, class_breakpoint, NULL, docstring,
13997 &catch_cmdlist);
13998 set_cmd_sfunc (command, sfunc);
13999 set_cmd_context (command, user_data_catch);
a96d9b2e 14000 set_cmd_completer (command, completer);
44feb3ce
TT
14001
14002 command = add_cmd (name, class_breakpoint, NULL, docstring,
14003 &tcatch_cmdlist);
14004 set_cmd_sfunc (command, sfunc);
14005 set_cmd_context (command, user_data_tcatch);
a96d9b2e 14006 set_cmd_completer (command, completer);
44feb3ce
TT
14007}
14008
6c95b8df 14009static void
a79b8f6e 14010clear_syscall_counts (struct inferior *inf)
6c95b8df 14011{
6c95b8df
PA
14012 inf->total_syscalls_count = 0;
14013 inf->any_syscall_count = 0;
14014 VEC_free (int, inf->syscalls_counts);
14015}
14016
6149aea9
PA
14017static void
14018save_command (char *arg, int from_tty)
14019{
3e43a32a
MS
14020 printf_unfiltered (_("\"save\" must be followed by "
14021 "the name of a save subcommand.\n"));
6149aea9
PA
14022 help_list (save_cmdlist, "save ", -1, gdb_stdout);
14023}
14024
84f4c1fe
PM
14025struct breakpoint *
14026iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
14027 void *data)
14028{
35df4500 14029 struct breakpoint *b, *b_tmp;
84f4c1fe 14030
35df4500 14031 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
14032 {
14033 if ((*callback) (b, data))
14034 return b;
14035 }
14036
14037 return NULL;
14038}
14039
0574c78f
GB
14040/* Zero if any of the breakpoint's locations could be a location where
14041 functions have been inlined, nonzero otherwise. */
14042
14043static int
14044is_non_inline_function (struct breakpoint *b)
14045{
14046 /* The shared library event breakpoint is set on the address of a
14047 non-inline function. */
14048 if (b->type == bp_shlib_event)
14049 return 1;
14050
14051 return 0;
14052}
14053
14054/* Nonzero if the specified PC cannot be a location where functions
14055 have been inlined. */
14056
14057int
09ac7c10
TT
14058pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
14059 const struct target_waitstatus *ws)
0574c78f
GB
14060{
14061 struct breakpoint *b;
14062 struct bp_location *bl;
14063
14064 ALL_BREAKPOINTS (b)
14065 {
14066 if (!is_non_inline_function (b))
14067 continue;
14068
14069 for (bl = b->loc; bl != NULL; bl = bl->next)
14070 {
14071 if (!bl->shlib_disabled
09ac7c10 14072 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
14073 return 1;
14074 }
14075 }
14076
14077 return 0;
14078}
14079
2060206e
PA
14080void
14081initialize_breakpoint_ops (void)
14082{
14083 static int initialized = 0;
14084
14085 struct breakpoint_ops *ops;
14086
14087 if (initialized)
14088 return;
14089 initialized = 1;
14090
14091 /* The breakpoint_ops structure to be inherit by all kinds of
14092 breakpoints (real breakpoints, i.e., user "break" breakpoints,
14093 internal and momentary breakpoints, etc.). */
14094 ops = &bkpt_base_breakpoint_ops;
14095 *ops = base_breakpoint_ops;
14096 ops->re_set = bkpt_re_set;
14097 ops->insert_location = bkpt_insert_location;
14098 ops->remove_location = bkpt_remove_location;
14099 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
14100 ops->create_sals_from_address = bkpt_create_sals_from_address;
14101 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
14102 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
14103
14104 /* The breakpoint_ops structure to be used in regular breakpoints. */
14105 ops = &bkpt_breakpoint_ops;
14106 *ops = bkpt_base_breakpoint_ops;
14107 ops->re_set = bkpt_re_set;
14108 ops->resources_needed = bkpt_resources_needed;
14109 ops->print_it = bkpt_print_it;
14110 ops->print_mention = bkpt_print_mention;
14111 ops->print_recreate = bkpt_print_recreate;
14112
14113 /* Ranged breakpoints. */
14114 ops = &ranged_breakpoint_ops;
14115 *ops = bkpt_breakpoint_ops;
14116 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
14117 ops->resources_needed = resources_needed_ranged_breakpoint;
14118 ops->print_it = print_it_ranged_breakpoint;
14119 ops->print_one = print_one_ranged_breakpoint;
14120 ops->print_one_detail = print_one_detail_ranged_breakpoint;
14121 ops->print_mention = print_mention_ranged_breakpoint;
14122 ops->print_recreate = print_recreate_ranged_breakpoint;
14123
14124 /* Internal breakpoints. */
14125 ops = &internal_breakpoint_ops;
14126 *ops = bkpt_base_breakpoint_ops;
14127 ops->re_set = internal_bkpt_re_set;
14128 ops->check_status = internal_bkpt_check_status;
14129 ops->print_it = internal_bkpt_print_it;
14130 ops->print_mention = internal_bkpt_print_mention;
14131
14132 /* Momentary breakpoints. */
14133 ops = &momentary_breakpoint_ops;
14134 *ops = bkpt_base_breakpoint_ops;
14135 ops->re_set = momentary_bkpt_re_set;
14136 ops->check_status = momentary_bkpt_check_status;
14137 ops->print_it = momentary_bkpt_print_it;
14138 ops->print_mention = momentary_bkpt_print_mention;
14139
14140 /* GNU v3 exception catchpoints. */
14141 ops = &gnu_v3_exception_catchpoint_ops;
14142 *ops = bkpt_breakpoint_ops;
14143 ops->print_it = print_it_exception_catchpoint;
14144 ops->print_one = print_one_exception_catchpoint;
14145 ops->print_mention = print_mention_exception_catchpoint;
14146 ops->print_recreate = print_recreate_exception_catchpoint;
14147
14148 /* Watchpoints. */
14149 ops = &watchpoint_breakpoint_ops;
14150 *ops = base_breakpoint_ops;
3a5c3e22 14151 ops->dtor = dtor_watchpoint;
2060206e
PA
14152 ops->re_set = re_set_watchpoint;
14153 ops->insert_location = insert_watchpoint;
14154 ops->remove_location = remove_watchpoint;
14155 ops->breakpoint_hit = breakpoint_hit_watchpoint;
14156 ops->check_status = check_status_watchpoint;
14157 ops->resources_needed = resources_needed_watchpoint;
14158 ops->works_in_software_mode = works_in_software_mode_watchpoint;
14159 ops->print_it = print_it_watchpoint;
14160 ops->print_mention = print_mention_watchpoint;
14161 ops->print_recreate = print_recreate_watchpoint;
14162
14163 /* Masked watchpoints. */
14164 ops = &masked_watchpoint_breakpoint_ops;
14165 *ops = watchpoint_breakpoint_ops;
14166 ops->insert_location = insert_masked_watchpoint;
14167 ops->remove_location = remove_masked_watchpoint;
14168 ops->resources_needed = resources_needed_masked_watchpoint;
14169 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
14170 ops->print_it = print_it_masked_watchpoint;
14171 ops->print_one_detail = print_one_detail_masked_watchpoint;
14172 ops->print_mention = print_mention_masked_watchpoint;
14173 ops->print_recreate = print_recreate_masked_watchpoint;
14174
14175 /* Tracepoints. */
14176 ops = &tracepoint_breakpoint_ops;
14177 *ops = base_breakpoint_ops;
14178 ops->re_set = tracepoint_re_set;
14179 ops->breakpoint_hit = tracepoint_breakpoint_hit;
14180 ops->print_one_detail = tracepoint_print_one_detail;
14181 ops->print_mention = tracepoint_print_mention;
14182 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
14183 ops->create_sals_from_address = tracepoint_create_sals_from_address;
14184 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
14185 ops->decode_linespec = tracepoint_decode_linespec;
14186
14187 /* Static tracepoints with marker (`-m'). */
14188 ops = &strace_marker_breakpoint_ops;
14189 *ops = tracepoint_breakpoint_ops;
14190 ops->create_sals_from_address = strace_marker_create_sals_from_address;
14191 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
14192 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
14193
14194 /* Fork catchpoints. */
14195 ops = &catch_fork_breakpoint_ops;
14196 *ops = base_breakpoint_ops;
14197 ops->insert_location = insert_catch_fork;
14198 ops->remove_location = remove_catch_fork;
14199 ops->breakpoint_hit = breakpoint_hit_catch_fork;
14200 ops->print_it = print_it_catch_fork;
14201 ops->print_one = print_one_catch_fork;
14202 ops->print_mention = print_mention_catch_fork;
14203 ops->print_recreate = print_recreate_catch_fork;
14204
14205 /* Vfork catchpoints. */
14206 ops = &catch_vfork_breakpoint_ops;
14207 *ops = base_breakpoint_ops;
14208 ops->insert_location = insert_catch_vfork;
14209 ops->remove_location = remove_catch_vfork;
14210 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
14211 ops->print_it = print_it_catch_vfork;
14212 ops->print_one = print_one_catch_vfork;
14213 ops->print_mention = print_mention_catch_vfork;
14214 ops->print_recreate = print_recreate_catch_vfork;
14215
14216 /* Exec catchpoints. */
14217 ops = &catch_exec_breakpoint_ops;
14218 *ops = base_breakpoint_ops;
14219 ops->dtor = dtor_catch_exec;
14220 ops->insert_location = insert_catch_exec;
14221 ops->remove_location = remove_catch_exec;
14222 ops->breakpoint_hit = breakpoint_hit_catch_exec;
14223 ops->print_it = print_it_catch_exec;
14224 ops->print_one = print_one_catch_exec;
14225 ops->print_mention = print_mention_catch_exec;
14226 ops->print_recreate = print_recreate_catch_exec;
14227
14228 /* Syscall catchpoints. */
14229 ops = &catch_syscall_breakpoint_ops;
14230 *ops = base_breakpoint_ops;
14231 ops->dtor = dtor_catch_syscall;
14232 ops->insert_location = insert_catch_syscall;
14233 ops->remove_location = remove_catch_syscall;
14234 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
14235 ops->print_it = print_it_catch_syscall;
14236 ops->print_one = print_one_catch_syscall;
14237 ops->print_mention = print_mention_catch_syscall;
14238 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
14239
14240 /* Solib-related catchpoints. */
14241 ops = &catch_solib_breakpoint_ops;
14242 *ops = base_breakpoint_ops;
14243 ops->dtor = dtor_catch_solib;
14244 ops->insert_location = insert_catch_solib;
14245 ops->remove_location = remove_catch_solib;
14246 ops->breakpoint_hit = breakpoint_hit_catch_solib;
14247 ops->check_status = check_status_catch_solib;
14248 ops->print_it = print_it_catch_solib;
14249 ops->print_one = print_one_catch_solib;
14250 ops->print_mention = print_mention_catch_solib;
14251 ops->print_recreate = print_recreate_catch_solib;
2060206e
PA
14252}
14253
c906108c 14254void
fba45db2 14255_initialize_breakpoint (void)
c906108c
SS
14256{
14257 struct cmd_list_element *c;
14258
2060206e
PA
14259 initialize_breakpoint_ops ();
14260
84acb35a 14261 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 14262 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 14263 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 14264
17450429
PP
14265 breakpoint_objfile_key = register_objfile_data ();
14266
c906108c
SS
14267 breakpoint_chain = 0;
14268 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14269 before a breakpoint is set. */
14270 breakpoint_count = 0;
14271
1042e4c0
SS
14272 tracepoint_count = 0;
14273
1bedd215
AC
14274 add_com ("ignore", class_breakpoint, ignore_command, _("\
14275Set ignore-count of breakpoint number N to COUNT.\n\
14276Usage is `ignore N COUNT'."));
c906108c 14277 if (xdb_commands)
c5aa993b 14278 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 14279
1bedd215
AC
14280 add_com ("commands", class_breakpoint, commands_command, _("\
14281Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
14282Give breakpoint number as argument after \"commands\".\n\
14283With no argument, the targeted breakpoint is the last one set.\n\
14284The commands themselves follow starting on the next line.\n\
14285Type a line containing \"end\" to indicate the end of them.\n\
14286Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 14287then no output is printed when it is hit, except what the commands print."));
c906108c 14288
1bedd215
AC
14289 add_com ("condition", class_breakpoint, condition_command, _("\
14290Specify breakpoint number N to break only if COND is true.\n\
c906108c 14291Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 14292expression to be evaluated whenever breakpoint N is reached."));
c906108c 14293
1bedd215 14294 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 14295Set a temporary breakpoint.\n\
c906108c
SS
14296Like \"break\" except the breakpoint is only temporary,\n\
14297so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
14298by using \"enable delete\" on the breakpoint number.\n\
14299\n"
14300BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 14301 set_cmd_completer (c, location_completer);
c94fdfd0 14302
1bedd215 14303 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 14304Set a hardware assisted breakpoint.\n\
c906108c 14305Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
14306some target hardware may not have this support.\n\
14307\n"
14308BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 14309 set_cmd_completer (c, location_completer);
c906108c 14310
1bedd215 14311 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 14312Set a temporary hardware assisted breakpoint.\n\
c906108c 14313Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
14314so it will be deleted when hit.\n\
14315\n"
14316BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 14317 set_cmd_completer (c, location_completer);
c906108c 14318
1bedd215
AC
14319 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14320Enable some breakpoints.\n\
c906108c
SS
14321Give breakpoint numbers (separated by spaces) as arguments.\n\
14322With no subcommand, breakpoints are enabled until you command otherwise.\n\
14323This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 14324With a subcommand you can enable temporarily."),
c906108c
SS
14325 &enablelist, "enable ", 1, &cmdlist);
14326 if (xdb_commands)
1bedd215
AC
14327 add_com ("ab", class_breakpoint, enable_command, _("\
14328Enable some breakpoints.\n\
c906108c
SS
14329Give breakpoint numbers (separated by spaces) as arguments.\n\
14330With no subcommand, breakpoints are enabled until you command otherwise.\n\
14331This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 14332With a subcommand you can enable temporarily."));
c906108c
SS
14333
14334 add_com_alias ("en", "enable", class_breakpoint, 1);
14335
84951ab5 14336 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 14337Enable some breakpoints.\n\
c906108c
SS
14338Give breakpoint numbers (separated by spaces) as arguments.\n\
14339This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 14340May be abbreviated to simply \"enable\".\n"),
c5aa993b 14341 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 14342
1a966eab
AC
14343 add_cmd ("once", no_class, enable_once_command, _("\
14344Enable breakpoints for one hit. Give breakpoint numbers.\n\
14345If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
14346 &enablebreaklist);
14347
1a966eab
AC
14348 add_cmd ("delete", no_class, enable_delete_command, _("\
14349Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
14350If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14351 &enablebreaklist);
14352
816338b5
SS
14353 add_cmd ("count", no_class, enable_count_command, _("\
14354Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
14355If a breakpoint is hit while enabled in this fashion,\n\
14356the count is decremented; when it reaches zero, the breakpoint is disabled."),
14357 &enablebreaklist);
14358
1a966eab
AC
14359 add_cmd ("delete", no_class, enable_delete_command, _("\
14360Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
14361If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14362 &enablelist);
14363
1a966eab
AC
14364 add_cmd ("once", no_class, enable_once_command, _("\
14365Enable breakpoints for one hit. Give breakpoint numbers.\n\
14366If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
14367 &enablelist);
14368
14369 add_cmd ("count", no_class, enable_count_command, _("\
14370Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
14371If a breakpoint is hit while enabled in this fashion,\n\
14372the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
14373 &enablelist);
14374
1bedd215
AC
14375 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14376Disable some breakpoints.\n\
c906108c
SS
14377Arguments are breakpoint numbers with spaces in between.\n\
14378To disable all breakpoints, give no argument.\n\
64b9b334 14379A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
14380 &disablelist, "disable ", 1, &cmdlist);
14381 add_com_alias ("dis", "disable", class_breakpoint, 1);
14382 add_com_alias ("disa", "disable", class_breakpoint, 1);
14383 if (xdb_commands)
1bedd215
AC
14384 add_com ("sb", class_breakpoint, disable_command, _("\
14385Disable some breakpoints.\n\
c906108c
SS
14386Arguments are breakpoint numbers with spaces in between.\n\
14387To disable all breakpoints, give no argument.\n\
64b9b334 14388A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 14389
1a966eab
AC
14390 add_cmd ("breakpoints", class_alias, disable_command, _("\
14391Disable some breakpoints.\n\
c906108c
SS
14392Arguments are breakpoint numbers with spaces in between.\n\
14393To disable all breakpoints, give no argument.\n\
64b9b334 14394A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 14395This command may be abbreviated \"disable\"."),
c906108c
SS
14396 &disablelist);
14397
1bedd215
AC
14398 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14399Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
14400Arguments are breakpoint numbers with spaces in between.\n\
14401To delete all breakpoints, give no argument.\n\
14402\n\
14403Also a prefix command for deletion of other GDB objects.\n\
1bedd215 14404The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
14405 &deletelist, "delete ", 1, &cmdlist);
14406 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 14407 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 14408 if (xdb_commands)
1bedd215
AC
14409 add_com ("db", class_breakpoint, delete_command, _("\
14410Delete some breakpoints.\n\
c906108c 14411Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 14412To delete all breakpoints, give no argument.\n"));
c906108c 14413
1a966eab
AC
14414 add_cmd ("breakpoints", class_alias, delete_command, _("\
14415Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
14416Arguments are breakpoint numbers with spaces in between.\n\
14417To delete all breakpoints, give no argument.\n\
1a966eab 14418This command may be abbreviated \"delete\"."),
c906108c
SS
14419 &deletelist);
14420
1bedd215
AC
14421 add_com ("clear", class_breakpoint, clear_command, _("\
14422Clear breakpoint at specified line or function.\n\
c906108c
SS
14423Argument may be line number, function name, or \"*\" and an address.\n\
14424If line number is specified, all breakpoints in that line are cleared.\n\
14425If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
14426If an address is specified, breakpoints at that address are cleared.\n\
14427\n\
14428With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
14429is executing in.\n\
14430\n\
1bedd215 14431See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 14432 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 14433
1bedd215 14434 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
14435Set breakpoint at specified line or function.\n"
14436BREAK_ARGS_HELP ("break")));
5ba2abeb 14437 set_cmd_completer (c, location_completer);
c94fdfd0 14438
c906108c
SS
14439 add_com_alias ("b", "break", class_run, 1);
14440 add_com_alias ("br", "break", class_run, 1);
14441 add_com_alias ("bre", "break", class_run, 1);
14442 add_com_alias ("brea", "break", class_run, 1);
14443
7681d515
PM
14444 if (xdb_commands)
14445 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
14446
14447 if (dbx_commands)
14448 {
1bedd215
AC
14449 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
14450Break in function/address or break at a line in the current file."),
c5aa993b
JM
14451 &stoplist, "stop ", 1, &cmdlist);
14452 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 14453 _("Break in function or address."), &stoplist);
c5aa993b 14454 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 14455 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
14456 add_com ("status", class_info, breakpoints_info, _("\
14457Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
14458The \"Type\" column indicates one of:\n\
14459\tbreakpoint - normal breakpoint\n\
14460\twatchpoint - watchpoint\n\
14461The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14462the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14463breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
14464address and file/line number respectively.\n\
14465\n\
14466Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14467are set to the address of the last breakpoint listed unless the command\n\
14468is prefixed with \"server \".\n\n\
c906108c 14469Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 14470breakpoint set."));
c906108c
SS
14471 }
14472
1bedd215 14473 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 14474Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
14475The \"Type\" column indicates one of:\n\
14476\tbreakpoint - normal breakpoint\n\
14477\twatchpoint - watchpoint\n\
14478The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14479the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14480breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
14481address and file/line number respectively.\n\
14482\n\
14483Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14484are set to the address of the last breakpoint listed unless the command\n\
14485is prefixed with \"server \".\n\n\
c906108c 14486Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 14487breakpoint set."));
c906108c 14488
6b04bdb7
MS
14489 add_info_alias ("b", "breakpoints", 1);
14490
c906108c 14491 if (xdb_commands)
1bedd215
AC
14492 add_com ("lb", class_breakpoint, breakpoints_info, _("\
14493Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
14494The \"Type\" column indicates one of:\n\
14495\tbreakpoint - normal breakpoint\n\
14496\twatchpoint - watchpoint\n\
14497The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14498the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14499breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
14500address and file/line number respectively.\n\
14501\n\
14502Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14503are set to the address of the last breakpoint listed unless the command\n\
14504is prefixed with \"server \".\n\n\
c906108c 14505Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 14506breakpoint set."));
c906108c 14507
1a966eab
AC
14508 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14509Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
14510The \"Type\" column indicates one of:\n\
14511\tbreakpoint - normal breakpoint\n\
14512\twatchpoint - watchpoint\n\
14513\tlongjmp - internal breakpoint used to step through longjmp()\n\
14514\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14515\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
14516\tfinish - internal breakpoint used by the \"finish\" command\n\
14517The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
14518the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14519breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
14520address and file/line number respectively.\n\
14521\n\
14522Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14523are set to the address of the last breakpoint listed unless the command\n\
14524is prefixed with \"server \".\n\n\
c906108c 14525Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 14526breakpoint set."),
c906108c
SS
14527 &maintenanceinfolist);
14528
44feb3ce
TT
14529 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
14530Set catchpoints to catch events."),
14531 &catch_cmdlist, "catch ",
14532 0/*allow-unknown*/, &cmdlist);
14533
14534 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
14535Set temporary catchpoints to catch events."),
14536 &tcatch_cmdlist, "tcatch ",
14537 0/*allow-unknown*/, &cmdlist);
14538
14539 /* Add catch and tcatch sub-commands. */
14540 add_catch_command ("catch", _("\
88e7d25d 14541Catch an exception, when caught."),
44feb3ce 14542 catch_catch_command,
a96d9b2e 14543 NULL,
44feb3ce
TT
14544 CATCH_PERMANENT,
14545 CATCH_TEMPORARY);
14546 add_catch_command ("throw", _("\
88e7d25d 14547Catch an exception, when thrown."),
44feb3ce 14548 catch_throw_command,
a96d9b2e 14549 NULL,
44feb3ce
TT
14550 CATCH_PERMANENT,
14551 CATCH_TEMPORARY);
14552 add_catch_command ("fork", _("Catch calls to fork."),
14553 catch_fork_command_1,
a96d9b2e 14554 NULL,
44feb3ce
TT
14555 (void *) (uintptr_t) catch_fork_permanent,
14556 (void *) (uintptr_t) catch_fork_temporary);
14557 add_catch_command ("vfork", _("Catch calls to vfork."),
14558 catch_fork_command_1,
a96d9b2e 14559 NULL,
44feb3ce
TT
14560 (void *) (uintptr_t) catch_vfork_permanent,
14561 (void *) (uintptr_t) catch_vfork_temporary);
14562 add_catch_command ("exec", _("Catch calls to exec."),
14563 catch_exec_command_1,
a96d9b2e
SDJ
14564 NULL,
14565 CATCH_PERMANENT,
14566 CATCH_TEMPORARY);
edcc5120
TT
14567 add_catch_command ("load", _("Catch loads of shared libraries.\n\
14568Usage: catch load [REGEX]\n\
14569If REGEX is given, only stop for libraries matching the regular expression."),
14570 catch_load_command_1,
14571 NULL,
14572 CATCH_PERMANENT,
14573 CATCH_TEMPORARY);
14574 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
14575Usage: catch unload [REGEX]\n\
14576If REGEX is given, only stop for libraries matching the regular expression."),
14577 catch_unload_command_1,
14578 NULL,
14579 CATCH_PERMANENT,
14580 CATCH_TEMPORARY);
a96d9b2e
SDJ
14581 add_catch_command ("syscall", _("\
14582Catch system calls by their names and/or numbers.\n\
14583Arguments say which system calls to catch. If no arguments\n\
14584are given, every system call will be caught.\n\
14585Arguments, if given, should be one or more system call names\n\
14586(if your system supports that), or system call numbers."),
14587 catch_syscall_command_1,
14588 catch_syscall_completer,
44feb3ce
TT
14589 CATCH_PERMANENT,
14590 CATCH_TEMPORARY);
c5aa993b 14591
1bedd215
AC
14592 c = add_com ("watch", class_breakpoint, watch_command, _("\
14593Set a watchpoint for an expression.\n\
06a64a0b 14594Usage: watch [-l|-location] EXPRESSION\n\
c906108c 14595A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
14596an expression changes.\n\
14597If -l or -location is given, this evaluates EXPRESSION and watches\n\
14598the memory to which it refers."));
65d12d83 14599 set_cmd_completer (c, expression_completer);
c906108c 14600
1bedd215
AC
14601 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
14602Set a read watchpoint for an expression.\n\
06a64a0b 14603Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 14604A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
14605an expression is read.\n\
14606If -l or -location is given, this evaluates EXPRESSION and watches\n\
14607the memory to which it refers."));
65d12d83 14608 set_cmd_completer (c, expression_completer);
c906108c 14609
1bedd215
AC
14610 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
14611Set a watchpoint for an expression.\n\
06a64a0b 14612Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 14613A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
14614an expression is either read or written.\n\
14615If -l or -location is given, this evaluates EXPRESSION and watches\n\
14616the memory to which it refers."));
65d12d83 14617 set_cmd_completer (c, expression_completer);
c906108c 14618
d77f58be 14619 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 14620Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 14621
920d2a44
AC
14622 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14623 respond to changes - contrary to the description. */
85c07804
AC
14624 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14625 &can_use_hw_watchpoints, _("\
14626Set debugger's willingness to use watchpoint hardware."), _("\
14627Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
14628If zero, gdb will not use hardware for new watchpoints, even if\n\
14629such is available. (However, any hardware watchpoints that were\n\
14630created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
14631hardware.)"),
14632 NULL,
920d2a44 14633 show_can_use_hw_watchpoints,
85c07804 14634 &setlist, &showlist);
c906108c
SS
14635
14636 can_use_hw_watchpoints = 1;
fa8d40ab 14637
1042e4c0
SS
14638 /* Tracepoint manipulation commands. */
14639
14640 c = add_com ("trace", class_breakpoint, trace_command, _("\
14641Set a tracepoint at specified line or function.\n\
14642\n"
14643BREAK_ARGS_HELP ("trace") "\n\
14644Do \"help tracepoints\" for info on other tracepoint commands."));
14645 set_cmd_completer (c, location_completer);
14646
14647 add_com_alias ("tp", "trace", class_alias, 0);
14648 add_com_alias ("tr", "trace", class_alias, 1);
14649 add_com_alias ("tra", "trace", class_alias, 1);
14650 add_com_alias ("trac", "trace", class_alias, 1);
14651
7a697b8d
SS
14652 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14653Set a fast tracepoint at specified line or function.\n\
14654\n"
14655BREAK_ARGS_HELP ("ftrace") "\n\
14656Do \"help tracepoints\" for info on other tracepoint commands."));
14657 set_cmd_completer (c, location_completer);
14658
0fb4aa4b
PA
14659 c = add_com ("strace", class_breakpoint, strace_command, _("\
14660Set a static tracepoint at specified line, function or marker.\n\
14661\n\
14662strace [LOCATION] [if CONDITION]\n\
14663LOCATION may be a line number, function name, \"*\" and an address,\n\
14664or -m MARKER_ID.\n\
14665If a line number is specified, probe the marker at start of code\n\
14666for that line. If a function is specified, probe the marker at start\n\
14667of code for that function. If an address is specified, probe the marker\n\
14668at that exact address. If a marker id is specified, probe the marker\n\
14669with that name. With no LOCATION, uses current execution address of\n\
14670the selected stack frame.\n\
14671Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14672This collects arbitrary user data passed in the probe point call to the\n\
14673tracing library. You can inspect it when analyzing the trace buffer,\n\
14674by printing the $_sdata variable like any other convenience variable.\n\
14675\n\
14676CONDITION is a boolean expression.\n\
14677\n\
d41c0fc8
PA
14678Multiple tracepoints at one place are permitted, and useful if their\n\
14679conditions are different.\n\
0fb4aa4b
PA
14680\n\
14681Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14682Do \"help tracepoints\" for info on other tracepoint commands."));
14683 set_cmd_completer (c, location_completer);
14684
1042e4c0 14685 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 14686Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
14687Convenience variable \"$tpnum\" contains the number of the\n\
14688last tracepoint set."));
14689
14690 add_info_alias ("tp", "tracepoints", 1);
14691
14692 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
14693Delete specified tracepoints.\n\
14694Arguments are tracepoint numbers, separated by spaces.\n\
14695No argument means delete all tracepoints."),
14696 &deletelist);
14697
14698 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14699Disable specified tracepoints.\n\
14700Arguments are tracepoint numbers, separated by spaces.\n\
14701No argument means disable all tracepoints."),
14702 &disablelist);
14703 deprecate_cmd (c, "disable");
14704
14705 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14706Enable specified tracepoints.\n\
14707Arguments are tracepoint numbers, separated by spaces.\n\
14708No argument means enable all tracepoints."),
14709 &enablelist);
14710 deprecate_cmd (c, "enable");
14711
14712 add_com ("passcount", class_trace, trace_pass_command, _("\
14713Set the passcount for a tracepoint.\n\
14714The trace will end when the tracepoint has been passed 'count' times.\n\
14715Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14716if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14717
6149aea9
PA
14718 add_prefix_cmd ("save", class_breakpoint, save_command,
14719 _("Save breakpoint definitions as a script."),
14720 &save_cmdlist, "save ",
14721 0/*allow-unknown*/, &cmdlist);
14722
14723 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14724Save current breakpoint definitions as a script.\n\
cce7e648 14725This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14726catchpoints, tracepoints). Use the 'source' command in another debug\n\
14727session to restore them."),
14728 &save_cmdlist);
14729 set_cmd_completer (c, filename_completer);
14730
14731 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14732Save current tracepoint definitions as a script.\n\
6149aea9
PA
14733Use the 'source' command in another debug session to restore them."),
14734 &save_cmdlist);
1042e4c0
SS
14735 set_cmd_completer (c, filename_completer);
14736
6149aea9
PA
14737 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
14738 deprecate_cmd (c, "save tracepoints");
14739
1bedd215 14740 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
14741Breakpoint specific settings\n\
14742Configure various breakpoint-specific variables such as\n\
1bedd215 14743pending breakpoint behavior"),
fa8d40ab
JJ
14744 &breakpoint_set_cmdlist, "set breakpoint ",
14745 0/*allow-unknown*/, &setlist);
1bedd215 14746 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
14747Breakpoint specific settings\n\
14748Configure various breakpoint-specific variables such as\n\
1bedd215 14749pending breakpoint behavior"),
fa8d40ab
JJ
14750 &breakpoint_show_cmdlist, "show breakpoint ",
14751 0/*allow-unknown*/, &showlist);
14752
7915a72c
AC
14753 add_setshow_auto_boolean_cmd ("pending", no_class,
14754 &pending_break_support, _("\
14755Set debugger's behavior regarding pending breakpoints."), _("\
14756Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14757If on, an unrecognized breakpoint location will cause gdb to create a\n\
14758pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14759an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14760user-query to see if a pending breakpoint should be created."),
2c5b56ce 14761 NULL,
920d2a44 14762 show_pending_break_support,
6e1d7d6c
AC
14763 &breakpoint_set_cmdlist,
14764 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14765
14766 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14767
14768 add_setshow_boolean_cmd ("auto-hw", no_class,
14769 &automatic_hardware_breakpoints, _("\
14770Set automatic usage of hardware breakpoints."), _("\
14771Show automatic usage of hardware breakpoints."), _("\
14772If set, the debugger will automatically use hardware breakpoints for\n\
14773breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14774a warning will be emitted for such breakpoints."),
14775 NULL,
14776 show_automatic_hardware_breakpoints,
14777 &breakpoint_set_cmdlist,
14778 &breakpoint_show_cmdlist);
74960c60 14779
33e5cbd6
PA
14780 add_setshow_enum_cmd ("always-inserted", class_support,
14781 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
14782Set mode for inserting breakpoints."), _("\
14783Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
14784When this mode is off, breakpoints are inserted in inferior when it is\n\
14785resumed, and removed when execution stops. When this mode is on,\n\
14786breakpoints are inserted immediately and removed only when the user\n\
14787deletes the breakpoint. When this mode is auto (which is the default),\n\
14788the behaviour depends on the non-stop setting (see help set non-stop).\n\
14789In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
14790behaves as if always-inserted mode is on; if gdb is controlling the\n\
14791inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
14792 NULL,
14793 &show_always_inserted_mode,
14794 &breakpoint_set_cmdlist,
14795 &breakpoint_show_cmdlist);
f1310107
TJB
14796
14797 add_com ("break-range", class_breakpoint, break_range_command, _("\
14798Set a breakpoint for an address range.\n\
14799break-range START-LOCATION, END-LOCATION\n\
14800where START-LOCATION and END-LOCATION can be one of the following:\n\
14801 LINENUM, for that line in the current file,\n\
14802 FILE:LINENUM, for that line in that file,\n\
14803 +OFFSET, for that number of lines after the current line\n\
14804 or the start of the range\n\
14805 FUNCTION, for the first line in that function,\n\
14806 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14807 *ADDRESS, for the instruction at that address.\n\
14808\n\
14809The breakpoint will stop execution of the inferior whenever it executes\n\
14810an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14811range (including START-LOCATION and END-LOCATION)."));
14812
765dc015 14813 automatic_hardware_breakpoints = 1;
f3b1572e
PA
14814
14815 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 14816}
This page took 2.263317 seconds and 4 git commands to generate.