2011-07-25 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1 4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7b6bb8da 5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
a6d9a66e 23#include "arch-utils.h"
c906108c 24#include <ctype.h>
776592bf 25#include "hashtab.h"
c906108c
SS
26#include "symtab.h"
27#include "frame.h"
28#include "breakpoint.h"
1042e4c0 29#include "tracepoint.h"
c906108c
SS
30#include "gdbtypes.h"
31#include "expression.h"
32#include "gdbcore.h"
33#include "gdbcmd.h"
34#include "value.h"
35#include "command.h"
36#include "inferior.h"
37#include "gdbthread.h"
38#include "target.h"
39#include "language.h"
40#include "gdb_string.h"
41#include "demangle.h"
0ba1096a 42#include "filenames.h"
c906108c
SS
43#include "annotate.h"
44#include "symfile.h"
45#include "objfiles.h"
0378c332 46#include "source.h"
c5f0f3d0 47#include "linespec.h"
c94fdfd0 48#include "completer.h"
5b7f31a4 49#include "gdb.h"
8b93c638 50#include "ui-out.h"
e1507482 51#include "cli/cli-script.h"
0225421b 52#include "gdb_assert.h"
fe898f56 53#include "block.h"
a77053c2 54#include "solib.h"
84acb35a
JJ
55#include "solist.h"
56#include "observer.h"
60250e8b 57#include "exceptions.h"
765dc015 58#include "memattr.h"
f7f9143b 59#include "ada-lang.h"
d1aa2f50 60#include "top.h"
fa4727a6 61#include "wrapper.h"
79a45b7d 62#include "valprint.h"
4efc6507 63#include "jit.h"
a96d9b2e 64#include "xml-syscall.h"
65d79d4b 65#include "parser-defs.h"
e9cafbcc 66#include "cli/cli-utils.h"
be34f849 67#include "continuations.h"
c906108c 68
1042e4c0
SS
69/* readline include files */
70#include "readline/readline.h"
71#include "readline/history.h"
72
73/* readline defines this. */
74#undef savestring
75
034dad6f 76#include "mi/mi-common.h"
7371cf6d 77#include "python/python.h"
104c1213 78
4a64f543 79/* Prototypes for local functions. */
c906108c 80
a14ed312 81static void enable_delete_command (char *, int);
c906108c 82
a14ed312 83static void enable_once_command (char *, int);
c906108c 84
a14ed312 85static void disable_command (char *, int);
c906108c 86
a14ed312 87static void enable_command (char *, int);
c906108c 88
95a42b64
TT
89static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
90 void *),
91 void *);
c906108c 92
a14ed312 93static void ignore_command (char *, int);
c906108c 94
4efb68b1 95static int breakpoint_re_set_one (void *);
c906108c 96
348d480f
PA
97static void breakpoint_re_set_default (struct breakpoint *);
98
a14ed312 99static void clear_command (char *, int);
c906108c 100
a14ed312 101static void catch_command (char *, int);
c906108c 102
a9634178 103static int can_use_hardware_watchpoint (struct value *);
c906108c 104
98deb0da 105static void break_command_1 (char *, int, int);
c906108c 106
a14ed312 107static void mention (struct breakpoint *);
c906108c 108
348d480f
PA
109static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
110 enum bptype,
111 struct breakpoint_ops *);
4a64f543
MS
112/* This function is used in gdbtk sources and thus can not be made
113 static. */
63c252f8 114struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f
PA
115 struct symtab_and_line,
116 enum bptype, struct breakpoint_ops *);
c906108c 117
06edf0c0
PA
118static struct breakpoint *
119 momentary_breakpoint_from_master (struct breakpoint *orig,
120 enum bptype type,
121 struct breakpoint_ops *ops);
122
76897487
KB
123static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
124
a6d9a66e
UW
125static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
126 CORE_ADDR bpaddr,
88f7da05 127 enum bptype bptype);
76897487 128
6c95b8df
PA
129static void describe_other_breakpoints (struct gdbarch *,
130 struct program_space *, CORE_ADDR,
5af949e3 131 struct obj_section *, int);
c906108c 132
6c95b8df
PA
133static int breakpoint_address_match (struct address_space *aspace1,
134 CORE_ADDR addr1,
135 struct address_space *aspace2,
136 CORE_ADDR addr2);
137
85d721b8
PA
138static int watchpoint_locations_match (struct bp_location *loc1,
139 struct bp_location *loc2);
140
f1310107
TJB
141static int breakpoint_location_address_match (struct bp_location *bl,
142 struct address_space *aspace,
143 CORE_ADDR addr);
144
a14ed312 145static void breakpoints_info (char *, int);
c906108c 146
d77f58be
SS
147static void watchpoints_info (char *, int);
148
e5a67952
MS
149static int breakpoint_1 (char *, int,
150 int (*) (const struct breakpoint *));
c906108c 151
4efb68b1 152static int breakpoint_cond_eval (void *);
c906108c 153
4efb68b1 154static void cleanup_executing_breakpoints (void *);
c906108c 155
a14ed312 156static void commands_command (char *, int);
c906108c 157
a14ed312 158static void condition_command (char *, int);
c906108c 159
c5aa993b
JM
160typedef enum
161 {
162 mark_inserted,
163 mark_uninserted
164 }
165insertion_state_t;
c906108c 166
0bde7532 167static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 168static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 169
e514a9d6 170static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 171
4efb68b1 172static int watchpoint_check (void *);
c906108c 173
a14ed312 174static void maintenance_info_breakpoints (char *, int);
c906108c 175
a14ed312 176static int hw_breakpoint_used_count (void);
c906108c 177
a14ed312 178static int hw_watchpoint_used_count (enum bptype, int *);
c906108c 179
a14ed312 180static void hbreak_command (char *, int);
c906108c 181
a14ed312 182static void thbreak_command (char *, int);
c906108c 183
51be5b68 184static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp);
c906108c 185
a14ed312 186static void stop_command (char *arg, int from_tty);
7a292a7a 187
a14ed312 188static void stopin_command (char *arg, int from_tty);
7a292a7a 189
a14ed312 190static void stopat_command (char *arg, int from_tty);
7a292a7a 191
a14ed312 192static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 193
d85310f7
MS
194static void catch_exception_command_1 (enum exception_event_kind ex_event,
195 char *arg, int tempflag, int from_tty);
7a292a7a 196
a14ed312 197static void tcatch_command (char *arg, int from_tty);
7a292a7a 198
d03285ec
UW
199static void detach_single_step_breakpoints (void);
200
6c95b8df
PA
201static int single_step_breakpoint_inserted_here_p (struct address_space *,
202 CORE_ADDR pc);
1aafd4da 203
fe3f5fa8 204static void free_bp_location (struct bp_location *loc);
f431efe5
PA
205static void incref_bp_location (struct bp_location *loc);
206static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 207
39d61571 208static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 209
b60e7edf 210static void update_global_location_list (int);
a5606eee 211
b60e7edf 212static void update_global_location_list_nothrow (int);
74960c60 213
d77f58be 214static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60 215
d77f58be 216static int is_watchpoint (const struct breakpoint *bpt);
60e1c644 217
74960c60 218static void insert_breakpoint_locations (void);
a5606eee 219
a96d9b2e
SDJ
220static int syscall_catchpoint_p (struct breakpoint *b);
221
1042e4c0
SS
222static void tracepoints_info (char *, int);
223
224static void delete_trace_command (char *, int);
225
226static void enable_trace_command (char *, int);
227
228static void disable_trace_command (char *, int);
229
230static void trace_pass_command (char *, int);
231
9c06b0b4
TJB
232static int is_masked_watchpoint (const struct breakpoint *b);
233
0fb4aa4b
PA
234/* Assuming we're creating a static tracepoint, does S look like a
235 static tracepoint marker spec ("-m MARKER_ID")? */
236#define is_marker_spec(s) \
f5a8e22b 237 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 238
2060206e
PA
239/* The abstract base class all breakpoint_ops structures inherit
240 from. */
241static struct breakpoint_ops base_breakpoint_ops;
242
243/* The breakpoint_ops structure to be inherited by all breakpoint_ops
244 that are implemented on top of software or hardware breakpoints
245 (user breakpoints, internal and momentary breakpoints, etc.). */
246static struct breakpoint_ops bkpt_base_breakpoint_ops;
247
248/* Internal breakpoints class type. */
06edf0c0 249static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
250
251/* Momentary breakpoints class type. */
06edf0c0
PA
252static struct breakpoint_ops momentary_breakpoint_ops;
253
2060206e
PA
254/* The breakpoint_ops structure to be used in regular user created
255 breakpoints. */
256struct breakpoint_ops bkpt_breakpoint_ops;
257
5cea2a26
PA
258/* A reference-counted struct command_line. This lets multiple
259 breakpoints share a single command list. */
260struct counted_command_line
261{
262 /* The reference count. */
263 int refc;
264
265 /* The command list. */
266 struct command_line *commands;
267};
268
269struct command_line *
270breakpoint_commands (struct breakpoint *b)
271{
272 return b->commands ? b->commands->commands : NULL;
273}
3daf8fe5 274
f3b1572e
PA
275/* Flag indicating that a command has proceeded the inferior past the
276 current breakpoint. */
277
278static int breakpoint_proceeded;
279
956a9fb9 280const char *
2cec12e5
AR
281bpdisp_text (enum bpdisp disp)
282{
4a64f543
MS
283 /* NOTE: the following values are a part of MI protocol and
284 represent values of 'disp' field returned when inferior stops at
285 a breakpoint. */
bc043ef3 286 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 287
2cec12e5
AR
288 return bpdisps[(int) disp];
289}
c906108c 290
4a64f543 291/* Prototypes for exported functions. */
c906108c 292/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 293 if such is available. */
c906108c
SS
294static int can_use_hw_watchpoints;
295
920d2a44
AC
296static void
297show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
298 struct cmd_list_element *c,
299 const char *value)
300{
3e43a32a
MS
301 fprintf_filtered (file,
302 _("Debugger's willingness to use "
303 "watchpoint hardware is %s.\n"),
920d2a44
AC
304 value);
305}
306
fa8d40ab
JJ
307/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
308 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 309 for unrecognized breakpoint locations.
fa8d40ab
JJ
310 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
311static enum auto_boolean pending_break_support;
920d2a44
AC
312static void
313show_pending_break_support (struct ui_file *file, int from_tty,
314 struct cmd_list_element *c,
315 const char *value)
316{
3e43a32a
MS
317 fprintf_filtered (file,
318 _("Debugger's behavior regarding "
319 "pending breakpoints is %s.\n"),
920d2a44
AC
320 value);
321}
fa8d40ab 322
765dc015 323/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 324 set with "break" but falling in read-only memory.
765dc015
VP
325 If 0, gdb will warn about such breakpoints, but won't automatically
326 use hardware breakpoints. */
327static int automatic_hardware_breakpoints;
328static void
329show_automatic_hardware_breakpoints (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 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
335 value);
336}
337
33e5cbd6
PA
338/* If on, gdb will keep breakpoints inserted even as inferior is
339 stopped, and immediately insert any new breakpoints. If off, gdb
340 will insert breakpoints into inferior only when resuming it, and
341 will remove breakpoints upon stop. If auto, GDB will behave as ON
342 if in non-stop mode, and as OFF if all-stop mode.*/
343
344static const char always_inserted_auto[] = "auto";
345static const char always_inserted_on[] = "on";
346static const char always_inserted_off[] = "off";
347static const char *always_inserted_enums[] = {
348 always_inserted_auto,
349 always_inserted_off,
350 always_inserted_on,
351 NULL
352};
353static const char *always_inserted_mode = always_inserted_auto;
354static void
74960c60 355show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 356 struct cmd_list_element *c, const char *value)
74960c60 357{
33e5cbd6 358 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
359 fprintf_filtered (file,
360 _("Always inserted breakpoint "
361 "mode is %s (currently %s).\n"),
33e5cbd6
PA
362 value,
363 breakpoints_always_inserted_mode () ? "on" : "off");
364 else
3e43a32a
MS
365 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
366 value);
74960c60
VP
367}
368
33e5cbd6
PA
369int
370breakpoints_always_inserted_mode (void)
371{
372 return (always_inserted_mode == always_inserted_on
373 || (always_inserted_mode == always_inserted_auto && non_stop));
374}
765dc015 375
a14ed312 376void _initialize_breakpoint (void);
c906108c 377
c906108c
SS
378/* Are we executing breakpoint commands? */
379static int executing_breakpoint_commands;
380
c02f5703
MS
381/* Are overlay event breakpoints enabled? */
382static int overlay_events_enabled;
383
e09342b5
TJB
384/* See description in breakpoint.h. */
385int target_exact_watchpoints = 0;
386
c906108c 387/* Walk the following statement or block through all breakpoints.
e5dd4106 388 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 389 current breakpoint. */
c906108c 390
5c44784c 391#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 392
5c44784c
JM
393#define ALL_BREAKPOINTS_SAFE(B,TMP) \
394 for (B = breakpoint_chain; \
395 B ? (TMP=B->next, 1): 0; \
396 B = TMP)
c906108c 397
4a64f543
MS
398/* Similar iterator for the low-level breakpoints. SAFE variant is
399 not provided so update_global_location_list must not be called
400 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 401
876fa593
JK
402#define ALL_BP_LOCATIONS(B,BP_TMP) \
403 for (BP_TMP = bp_location; \
404 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
405 BP_TMP++)
7cc221ef 406
1042e4c0
SS
407/* Iterator for tracepoints only. */
408
409#define ALL_TRACEPOINTS(B) \
410 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 411 if (is_tracepoint (B))
1042e4c0 412
7cc221ef 413/* Chains of all breakpoints defined. */
c906108c
SS
414
415struct breakpoint *breakpoint_chain;
416
876fa593
JK
417/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
418
419static struct bp_location **bp_location;
420
421/* Number of elements of BP_LOCATION. */
422
423static unsigned bp_location_count;
424
4a64f543
MS
425/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
426 ADDRESS for the current elements of BP_LOCATION which get a valid
427 result from bp_location_has_shadow. You can use it for roughly
428 limiting the subrange of BP_LOCATION to scan for shadow bytes for
429 an address you need to read. */
876fa593
JK
430
431static CORE_ADDR bp_location_placed_address_before_address_max;
432
4a64f543
MS
433/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
434 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
435 BP_LOCATION which get a valid result from bp_location_has_shadow.
436 You can use it for roughly limiting the subrange of BP_LOCATION to
437 scan for shadow bytes for an address you need to read. */
876fa593
JK
438
439static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 440
4a64f543
MS
441/* The locations that no longer correspond to any breakpoint, unlinked
442 from bp_location array, but for which a hit may still be reported
443 by a target. */
20874c92
VP
444VEC(bp_location_p) *moribund_locations = NULL;
445
c906108c
SS
446/* Number of last breakpoint made. */
447
95a42b64
TT
448static int breakpoint_count;
449
86b17b60
PA
450/* The value of `breakpoint_count' before the last command that
451 created breakpoints. If the last (break-like) command created more
452 than one breakpoint, then the difference between BREAKPOINT_COUNT
453 and PREV_BREAKPOINT_COUNT is more than one. */
454static int prev_breakpoint_count;
c906108c 455
1042e4c0
SS
456/* Number of last tracepoint made. */
457
95a42b64 458static int tracepoint_count;
1042e4c0 459
6149aea9
PA
460static struct cmd_list_element *breakpoint_set_cmdlist;
461static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 462struct cmd_list_element *save_cmdlist;
6149aea9 463
468d015d
JJ
464/* Return whether a breakpoint is an active enabled breakpoint. */
465static int
466breakpoint_enabled (struct breakpoint *b)
467{
0d381245 468 return (b->enable_state == bp_enabled);
468d015d
JJ
469}
470
c906108c
SS
471/* Set breakpoint count to NUM. */
472
95a42b64 473static void
fba45db2 474set_breakpoint_count (int num)
c906108c 475{
86b17b60 476 prev_breakpoint_count = breakpoint_count;
c906108c 477 breakpoint_count = num;
4fa62494 478 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
479}
480
86b17b60
PA
481/* Used by `start_rbreak_breakpoints' below, to record the current
482 breakpoint count before "rbreak" creates any breakpoint. */
483static int rbreak_start_breakpoint_count;
484
95a42b64
TT
485/* Called at the start an "rbreak" command to record the first
486 breakpoint made. */
86b17b60 487
95a42b64
TT
488void
489start_rbreak_breakpoints (void)
490{
86b17b60 491 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
492}
493
494/* Called at the end of an "rbreak" command to record the last
495 breakpoint made. */
86b17b60 496
95a42b64
TT
497void
498end_rbreak_breakpoints (void)
499{
86b17b60 500 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
501}
502
4a64f543 503/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
504
505void
fba45db2 506clear_breakpoint_hit_counts (void)
c906108c
SS
507{
508 struct breakpoint *b;
509
510 ALL_BREAKPOINTS (b)
511 b->hit_count = 0;
512}
513
9add0f1b
TT
514/* Allocate a new counted_command_line with reference count of 1.
515 The new structure owns COMMANDS. */
516
517static struct counted_command_line *
518alloc_counted_command_line (struct command_line *commands)
519{
520 struct counted_command_line *result
521 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 522
9add0f1b
TT
523 result->refc = 1;
524 result->commands = commands;
525 return result;
526}
527
528/* Increment reference count. This does nothing if CMD is NULL. */
529
530static void
531incref_counted_command_line (struct counted_command_line *cmd)
532{
533 if (cmd)
534 ++cmd->refc;
535}
536
537/* Decrement reference count. If the reference count reaches 0,
538 destroy the counted_command_line. Sets *CMDP to NULL. This does
539 nothing if *CMDP is NULL. */
540
541static void
542decref_counted_command_line (struct counted_command_line **cmdp)
543{
544 if (*cmdp)
545 {
546 if (--(*cmdp)->refc == 0)
547 {
548 free_command_lines (&(*cmdp)->commands);
549 xfree (*cmdp);
550 }
551 *cmdp = NULL;
552 }
553}
554
555/* A cleanup function that calls decref_counted_command_line. */
556
557static void
558do_cleanup_counted_command_line (void *arg)
559{
560 decref_counted_command_line (arg);
561}
562
563/* Create a cleanup that calls decref_counted_command_line on the
564 argument. */
565
566static struct cleanup *
567make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
568{
569 return make_cleanup (do_cleanup_counted_command_line, cmdp);
570}
571
c906108c
SS
572/* Default address, symtab and line to put a breakpoint at
573 for "break" command with no arg.
4a64f543 574 If default_breakpoint_valid is zero, the other three are
c906108c
SS
575 not valid, and "break" with no arg is an error.
576
577 This set by print_stack_frame, which calls set_default_breakpoint. */
578
579int default_breakpoint_valid;
580CORE_ADDR default_breakpoint_address;
581struct symtab *default_breakpoint_symtab;
582int default_breakpoint_line;
6c95b8df
PA
583struct program_space *default_breakpoint_pspace;
584
c906108c 585\f
48cb2d85
VP
586/* Return the breakpoint with the specified number, or NULL
587 if the number does not refer to an existing breakpoint. */
588
589struct breakpoint *
590get_breakpoint (int num)
591{
592 struct breakpoint *b;
593
594 ALL_BREAKPOINTS (b)
595 if (b->number == num)
596 return b;
597
598 return NULL;
599}
5c44784c 600
c906108c 601\f
adc36818
PM
602
603void
604set_breakpoint_condition (struct breakpoint *b, char *exp,
605 int from_tty)
606{
607 struct bp_location *loc = b->loc;
608
609 for (; loc; loc = loc->next)
610 {
611 xfree (loc->cond);
612 loc->cond = NULL;
613 }
614 xfree (b->cond_string);
615 b->cond_string = NULL;
616 xfree (b->cond_exp);
617 b->cond_exp = NULL;
618
619 if (*exp == 0)
620 {
621 if (from_tty)
622 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
623 }
624 else
625 {
626 char *arg = exp;
cc59ec59 627
adc36818
PM
628 /* I don't know if it matters whether this is the string the user
629 typed in or the decompiled expression. */
630 b->cond_string = xstrdup (arg);
631 b->condition_not_parsed = 0;
632
633 if (is_watchpoint (b))
634 {
635 innermost_block = NULL;
636 arg = exp;
637 b->cond_exp = parse_exp_1 (&arg, 0, 0);
638 if (*arg)
639 error (_("Junk at end of expression"));
640 b->cond_exp_valid_block = innermost_block;
641 }
642 else
643 {
644 for (loc = b->loc; loc; loc = loc->next)
645 {
646 arg = exp;
647 loc->cond =
648 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
649 if (*arg)
650 error (_("Junk at end of expression"));
651 }
652 }
653 }
654 breakpoints_changed ();
8d3788bd 655 observer_notify_breakpoint_modified (b);
adc36818
PM
656}
657
c906108c
SS
658/* condition N EXP -- set break condition of breakpoint N to EXP. */
659
660static void
fba45db2 661condition_command (char *arg, int from_tty)
c906108c 662{
52f0bd74 663 struct breakpoint *b;
c906108c 664 char *p;
52f0bd74 665 int bnum;
c906108c
SS
666
667 if (arg == 0)
e2e0b3e5 668 error_no_arg (_("breakpoint number"));
c906108c
SS
669
670 p = arg;
671 bnum = get_number (&p);
5c44784c 672 if (bnum == 0)
8a3fe4f8 673 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
674
675 ALL_BREAKPOINTS (b)
676 if (b->number == bnum)
2f069f6f 677 {
7371cf6d
PM
678 /* Check if this breakpoint has a Python object assigned to
679 it, and if it has a definition of the "stop"
680 method. This method and conditions entered into GDB from
681 the CLI are mutually exclusive. */
682 if (b->py_bp_object
683 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
684 error (_("Cannot set a condition where a Python 'stop' "
685 "method has been defined in the breakpoint."));
2566ad2d 686 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
687 return;
688 }
c906108c 689
8a3fe4f8 690 error (_("No breakpoint number %d."), bnum);
c906108c
SS
691}
692
a7bdde9e
VP
693/* Check that COMMAND do not contain commands that are suitable
694 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
695 Throw if any such commands is found. */
696
a7bdde9e
VP
697static void
698check_no_tracepoint_commands (struct command_line *commands)
699{
700 struct command_line *c;
cc59ec59 701
a7bdde9e
VP
702 for (c = commands; c; c = c->next)
703 {
704 int i;
705
706 if (c->control_type == while_stepping_control)
3e43a32a
MS
707 error (_("The 'while-stepping' command can "
708 "only be used for tracepoints"));
a7bdde9e
VP
709
710 for (i = 0; i < c->body_count; ++i)
711 check_no_tracepoint_commands ((c->body_list)[i]);
712
713 /* Not that command parsing removes leading whitespace and comment
4a64f543 714 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
715 command directly. */
716 if (strstr (c->line, "collect ") == c->line)
717 error (_("The 'collect' command can only be used for tracepoints"));
718
51661e93
VP
719 if (strstr (c->line, "teval ") == c->line)
720 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
721 }
722}
723
d77f58be
SS
724/* Encapsulate tests for different types of tracepoints. */
725
a7bdde9e 726int
d77f58be 727is_tracepoint (const struct breakpoint *b)
a7bdde9e 728{
0fb4aa4b
PA
729 return (b->type == bp_tracepoint
730 || b->type == bp_fast_tracepoint
731 || b->type == bp_static_tracepoint);
a7bdde9e 732}
d77f58be 733
e5dd4106 734/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
735 breakpoint. This function will throw an exception if a problem is
736 found. */
48cb2d85 737
95a42b64
TT
738static void
739validate_commands_for_breakpoint (struct breakpoint *b,
740 struct command_line *commands)
48cb2d85 741{
d77f58be 742 if (is_tracepoint (b))
a7bdde9e 743 {
4a64f543
MS
744 /* We need to verify that each top-level element of commands is
745 valid for tracepoints, that there's at most one
746 while-stepping element, and that while-stepping's body has
747 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
748 struct command_line *c;
749 struct command_line *while_stepping = 0;
750 for (c = commands; c; c = c->next)
751 {
a7bdde9e
VP
752 if (c->control_type == while_stepping_control)
753 {
754 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
755 error (_("The 'while-stepping' command "
756 "cannot be used for fast tracepoint"));
0fb4aa4b 757 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
758 error (_("The 'while-stepping' command "
759 "cannot be used for static tracepoint"));
a7bdde9e
VP
760
761 if (while_stepping)
3e43a32a
MS
762 error (_("The 'while-stepping' command "
763 "can be used only once"));
a7bdde9e
VP
764 else
765 while_stepping = c;
766 }
767 }
768 if (while_stepping)
769 {
770 struct command_line *c2;
771
772 gdb_assert (while_stepping->body_count == 1);
773 c2 = while_stepping->body_list[0];
774 for (; c2; c2 = c2->next)
775 {
a7bdde9e
VP
776 if (c2->control_type == while_stepping_control)
777 error (_("The 'while-stepping' command cannot be nested"));
778 }
779 }
780 }
781 else
782 {
783 check_no_tracepoint_commands (commands);
784 }
95a42b64
TT
785}
786
0fb4aa4b
PA
787/* Return a vector of all the static tracepoints set at ADDR. The
788 caller is responsible for releasing the vector. */
789
790VEC(breakpoint_p) *
791static_tracepoints_here (CORE_ADDR addr)
792{
793 struct breakpoint *b;
794 VEC(breakpoint_p) *found = 0;
795 struct bp_location *loc;
796
797 ALL_BREAKPOINTS (b)
798 if (b->type == bp_static_tracepoint)
799 {
800 for (loc = b->loc; loc; loc = loc->next)
801 if (loc->address == addr)
802 VEC_safe_push(breakpoint_p, found, b);
803 }
804
805 return found;
806}
807
95a42b64 808/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 809 validate that only allowed commands are included. */
95a42b64
TT
810
811void
4a64f543
MS
812breakpoint_set_commands (struct breakpoint *b,
813 struct command_line *commands)
95a42b64
TT
814{
815 validate_commands_for_breakpoint (b, commands);
a7bdde9e 816
9add0f1b
TT
817 decref_counted_command_line (&b->commands);
818 b->commands = alloc_counted_command_line (commands);
48cb2d85 819 breakpoints_changed ();
8d3788bd 820 observer_notify_breakpoint_modified (b);
48cb2d85
VP
821}
822
45a43567
TT
823/* Set the internal `silent' flag on the breakpoint. Note that this
824 is not the same as the "silent" that may appear in the breakpoint's
825 commands. */
826
827void
828breakpoint_set_silent (struct breakpoint *b, int silent)
829{
830 int old_silent = b->silent;
831
832 b->silent = silent;
833 if (old_silent != silent)
8d3788bd 834 observer_notify_breakpoint_modified (b);
45a43567
TT
835}
836
837/* Set the thread for this breakpoint. If THREAD is -1, make the
838 breakpoint work for any thread. */
839
840void
841breakpoint_set_thread (struct breakpoint *b, int thread)
842{
843 int old_thread = b->thread;
844
845 b->thread = thread;
846 if (old_thread != thread)
8d3788bd 847 observer_notify_breakpoint_modified (b);
45a43567
TT
848}
849
850/* Set the task for this breakpoint. If TASK is 0, make the
851 breakpoint work for any task. */
852
853void
854breakpoint_set_task (struct breakpoint *b, int task)
855{
856 int old_task = b->task;
857
858 b->task = task;
859 if (old_task != task)
8d3788bd 860 observer_notify_breakpoint_modified (b);
45a43567
TT
861}
862
95a42b64
TT
863void
864check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
865{
866 struct breakpoint *b = closure;
cc59ec59 867
a7bdde9e
VP
868 validate_actionline (&line, b);
869}
870
95a42b64
TT
871/* A structure used to pass information through
872 map_breakpoint_numbers. */
873
874struct commands_info
875{
876 /* True if the command was typed at a tty. */
877 int from_tty;
86b17b60
PA
878
879 /* The breakpoint range spec. */
880 char *arg;
881
95a42b64
TT
882 /* Non-NULL if the body of the commands are being read from this
883 already-parsed command. */
884 struct command_line *control;
86b17b60 885
95a42b64
TT
886 /* The command lines read from the user, or NULL if they have not
887 yet been read. */
888 struct counted_command_line *cmd;
889};
890
891/* A callback for map_breakpoint_numbers that sets the commands for
892 commands_command. */
893
c906108c 894static void
95a42b64 895do_map_commands_command (struct breakpoint *b, void *data)
c906108c 896{
95a42b64 897 struct commands_info *info = data;
c906108c 898
95a42b64
TT
899 if (info->cmd == NULL)
900 {
901 struct command_line *l;
5c44784c 902
95a42b64
TT
903 if (info->control != NULL)
904 l = copy_command_lines (info->control->body_list[0]);
905 else
86b17b60
PA
906 {
907 struct cleanup *old_chain;
908 char *str;
c5aa993b 909
3e43a32a
MS
910 str = xstrprintf (_("Type commands for breakpoint(s) "
911 "%s, one per line."),
86b17b60
PA
912 info->arg);
913
914 old_chain = make_cleanup (xfree, str);
915
916 l = read_command_lines (str,
917 info->from_tty, 1,
d77f58be 918 (is_tracepoint (b)
86b17b60
PA
919 ? check_tracepoint_command : 0),
920 b);
921
922 do_cleanups (old_chain);
923 }
a7bdde9e 924
95a42b64
TT
925 info->cmd = alloc_counted_command_line (l);
926 }
927
928 /* If a breakpoint was on the list more than once, we don't need to
929 do anything. */
930 if (b->commands != info->cmd)
931 {
932 validate_commands_for_breakpoint (b, info->cmd->commands);
933 incref_counted_command_line (info->cmd);
934 decref_counted_command_line (&b->commands);
935 b->commands = info->cmd;
936 breakpoints_changed ();
8d3788bd 937 observer_notify_breakpoint_modified (b);
c5aa993b 938 }
95a42b64
TT
939}
940
941static void
4a64f543
MS
942commands_command_1 (char *arg, int from_tty,
943 struct command_line *control)
95a42b64
TT
944{
945 struct cleanup *cleanups;
946 struct commands_info info;
947
948 info.from_tty = from_tty;
949 info.control = control;
950 info.cmd = NULL;
951 /* If we read command lines from the user, then `info' will hold an
952 extra reference to the commands that we must clean up. */
953 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
954
955 if (arg == NULL || !*arg)
956 {
86b17b60 957 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
958 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
959 breakpoint_count);
95a42b64
TT
960 else if (breakpoint_count > 0)
961 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
962 else
963 {
964 /* So that we don't try to free the incoming non-NULL
965 argument in the cleanup below. Mapping breakpoint
966 numbers will fail in this case. */
967 arg = NULL;
968 }
95a42b64 969 }
9766ced4
SS
970 else
971 /* The command loop has some static state, so we need to preserve
972 our argument. */
973 arg = xstrdup (arg);
86b17b60
PA
974
975 if (arg != NULL)
976 make_cleanup (xfree, arg);
977
978 info.arg = arg;
95a42b64
TT
979
980 map_breakpoint_numbers (arg, do_map_commands_command, &info);
981
982 if (info.cmd == NULL)
983 error (_("No breakpoints specified."));
984
985 do_cleanups (cleanups);
986}
987
988static void
989commands_command (char *arg, int from_tty)
990{
991 commands_command_1 (arg, from_tty, NULL);
c906108c 992}
40c03ae8
EZ
993
994/* Like commands_command, but instead of reading the commands from
995 input stream, takes them from an already parsed command structure.
996
997 This is used by cli-script.c to DTRT with breakpoint commands
998 that are part of if and while bodies. */
999enum command_control_type
1000commands_from_control_command (char *arg, struct command_line *cmd)
1001{
95a42b64
TT
1002 commands_command_1 (arg, 0, cmd);
1003 return simple_control;
40c03ae8 1004}
876fa593
JK
1005
1006/* Return non-zero if BL->TARGET_INFO contains valid information. */
1007
1008static int
1009bp_location_has_shadow (struct bp_location *bl)
1010{
1011 if (bl->loc_type != bp_loc_software_breakpoint)
1012 return 0;
1013 if (!bl->inserted)
1014 return 0;
1015 if (bl->target_info.shadow_len == 0)
e5dd4106 1016 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1017 return 0;
1018 return 1;
1019}
1020
8defab1a 1021/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1022 by replacing any memory breakpoints with their shadowed contents.
1023
1024 The range of shadowed area by each bp_location is:
35df4500
TJB
1025 bl->address - bp_location_placed_address_before_address_max
1026 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1027 The range we were requested to resolve shadows for is:
1028 memaddr ... memaddr + len
1029 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1030 memaddr + len <= (bl->address
1031 - bp_location_placed_address_before_address_max)
876fa593 1032 and:
35df4500 1033 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1034
8defab1a
DJ
1035void
1036breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1037{
4a64f543
MS
1038 /* Left boundary, right boundary and median element of our binary
1039 search. */
876fa593
JK
1040 unsigned bc_l, bc_r, bc;
1041
4a64f543
MS
1042 /* Find BC_L which is a leftmost element which may affect BUF
1043 content. It is safe to report lower value but a failure to
1044 report higher one. */
876fa593
JK
1045
1046 bc_l = 0;
1047 bc_r = bp_location_count;
1048 while (bc_l + 1 < bc_r)
1049 {
35df4500 1050 struct bp_location *bl;
876fa593
JK
1051
1052 bc = (bc_l + bc_r) / 2;
35df4500 1053 bl = bp_location[bc];
876fa593 1054
4a64f543
MS
1055 /* Check first BL->ADDRESS will not overflow due to the added
1056 constant. Then advance the left boundary only if we are sure
1057 the BC element can in no way affect the BUF content (MEMADDR
1058 to MEMADDR + LEN range).
876fa593 1059
4a64f543
MS
1060 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1061 offset so that we cannot miss a breakpoint with its shadow
1062 range tail still reaching MEMADDR. */
c5aa993b 1063
35df4500
TJB
1064 if ((bl->address + bp_location_shadow_len_after_address_max
1065 >= bl->address)
1066 && (bl->address + bp_location_shadow_len_after_address_max
1067 <= memaddr))
876fa593
JK
1068 bc_l = bc;
1069 else
1070 bc_r = bc;
1071 }
1072
128070bb
PA
1073 /* Due to the binary search above, we need to make sure we pick the
1074 first location that's at BC_L's address. E.g., if there are
1075 multiple locations at the same address, BC_L may end up pointing
1076 at a duplicate location, and miss the "master"/"inserted"
1077 location. Say, given locations L1, L2 and L3 at addresses A and
1078 B:
1079
1080 L1@A, L2@A, L3@B, ...
1081
1082 BC_L could end up pointing at location L2, while the "master"
1083 location could be L1. Since the `loc->inserted' flag is only set
1084 on "master" locations, we'd forget to restore the shadow of L1
1085 and L2. */
1086 while (bc_l > 0
1087 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1088 bc_l--;
1089
876fa593
JK
1090 /* Now do full processing of the found relevant range of elements. */
1091
1092 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1093 {
35df4500 1094 struct bp_location *bl = bp_location[bc];
876fa593
JK
1095 CORE_ADDR bp_addr = 0;
1096 int bp_size = 0;
1097 int bptoffset = 0;
1098
35df4500
TJB
1099 /* bp_location array has BL->OWNER always non-NULL. */
1100 if (bl->owner->type == bp_none)
8a3fe4f8 1101 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1102 bl->owner->number);
ffce0d52 1103
e5dd4106 1104 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1105 content. */
1106
35df4500
TJB
1107 if (bl->address >= bp_location_placed_address_before_address_max
1108 && memaddr + len <= (bl->address
1109 - bp_location_placed_address_before_address_max))
876fa593
JK
1110 break;
1111
35df4500 1112 if (!bp_location_has_shadow (bl))
c5aa993b 1113 continue;
35df4500 1114 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1115 current_program_space->aspace, 0))
1116 continue;
1117
c5aa993b
JM
1118 /* Addresses and length of the part of the breakpoint that
1119 we need to copy. */
35df4500
TJB
1120 bp_addr = bl->target_info.placed_address;
1121 bp_size = bl->target_info.shadow_len;
8defab1a 1122
c5aa993b
JM
1123 if (bp_addr + bp_size <= memaddr)
1124 /* The breakpoint is entirely before the chunk of memory we
1125 are reading. */
1126 continue;
8defab1a 1127
c5aa993b
JM
1128 if (bp_addr >= memaddr + len)
1129 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1130 reading. */
c5aa993b 1131 continue;
c5aa993b 1132
8defab1a
DJ
1133 /* Offset within shadow_contents. */
1134 if (bp_addr < memaddr)
1135 {
1136 /* Only copy the second part of the breakpoint. */
1137 bp_size -= memaddr - bp_addr;
1138 bptoffset = memaddr - bp_addr;
1139 bp_addr = memaddr;
1140 }
c5aa993b 1141
8defab1a
DJ
1142 if (bp_addr + bp_size > memaddr + len)
1143 {
1144 /* Only copy the first part of the breakpoint. */
1145 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1146 }
c5aa993b 1147
8defab1a 1148 memcpy (buf + bp_addr - memaddr,
35df4500 1149 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1150 }
c906108c 1151}
c906108c 1152\f
c5aa993b 1153
60e1c644
PA
1154/* Return true if BPT is of any hardware watchpoint kind. */
1155
a5606eee 1156static int
d77f58be 1157is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1158{
1159 return (bpt->type == bp_hardware_watchpoint
1160 || bpt->type == bp_read_watchpoint
1161 || bpt->type == bp_access_watchpoint);
1162}
7270d8f2 1163
60e1c644
PA
1164/* Return true if BPT is of any watchpoint kind, hardware or
1165 software. */
1166
1167static int
d77f58be 1168is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1169{
1170 return (is_hardware_watchpoint (bpt)
1171 || bpt->type == bp_watchpoint);
1172}
1173
f6bc2008
PA
1174/* Assuming that B is a watchpoint: returns true if the current thread
1175 and its running state are safe to evaluate or update watchpoint B.
1176 Watchpoints on local expressions need to be evaluated in the
1177 context of the thread that was current when the watchpoint was
1178 created, and, that thread needs to be stopped to be able to select
1179 the correct frame context. Watchpoints on global expressions can
1180 be evaluated on any thread, and in any state. It is presently left
1181 to the target allowing memory accesses when threads are
1182 running. */
1183
1184static int
1185watchpoint_in_thread_scope (struct breakpoint *b)
1186{
1187 return (ptid_equal (b->watchpoint_thread, null_ptid)
1188 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1189 && !is_executing (inferior_ptid)));
1190}
1191
d0fb5eae
JK
1192/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1193 associated bp_watchpoint_scope breakpoint. */
1194
1195static void
1196watchpoint_del_at_next_stop (struct breakpoint *b)
1197{
1198 gdb_assert (is_watchpoint (b));
1199
1200 if (b->related_breakpoint != b)
1201 {
1202 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1203 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1204 b->related_breakpoint->disposition = disp_del_at_next_stop;
1205 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1206 b->related_breakpoint = b;
1207 }
1208 b->disposition = disp_del_at_next_stop;
1209}
1210
567e1b4e
JB
1211/* Assuming that B is a watchpoint:
1212 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1213 - Evaluate expression and store the result in B->val
567e1b4e
JB
1214 - Evaluate the condition if there is one, and store the result
1215 in b->loc->cond.
a5606eee
VP
1216 - Update the list of values that must be watched in B->loc.
1217
4a64f543
MS
1218 If the watchpoint disposition is disp_del_at_next_stop, then do
1219 nothing. If this is local watchpoint that is out of scope, delete
1220 it.
1221
1222 Even with `set breakpoint always-inserted on' the watchpoints are
1223 removed + inserted on each stop here. Normal breakpoints must
1224 never be removed because they might be missed by a running thread
1225 when debugging in non-stop mode. On the other hand, hardware
1226 watchpoints (is_hardware_watchpoint; processed here) are specific
1227 to each LWP since they are stored in each LWP's hardware debug
1228 registers. Therefore, such LWP must be stopped first in order to
1229 be able to modify its hardware watchpoints.
1230
1231 Hardware watchpoints must be reset exactly once after being
1232 presented to the user. It cannot be done sooner, because it would
1233 reset the data used to present the watchpoint hit to the user. And
1234 it must not be done later because it could display the same single
1235 watchpoint hit during multiple GDB stops. Note that the latter is
1236 relevant only to the hardware watchpoint types bp_read_watchpoint
1237 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1238 not user-visible - its hit is suppressed if the memory content has
1239 not changed.
1240
1241 The following constraints influence the location where we can reset
1242 hardware watchpoints:
1243
1244 * target_stopped_by_watchpoint and target_stopped_data_address are
1245 called several times when GDB stops.
1246
1247 [linux]
1248 * Multiple hardware watchpoints can be hit at the same time,
1249 causing GDB to stop. GDB only presents one hardware watchpoint
1250 hit at a time as the reason for stopping, and all the other hits
1251 are presented later, one after the other, each time the user
1252 requests the execution to be resumed. Execution is not resumed
1253 for the threads still having pending hit event stored in
1254 LWP_INFO->STATUS. While the watchpoint is already removed from
1255 the inferior on the first stop the thread hit event is kept being
1256 reported from its cached value by linux_nat_stopped_data_address
1257 until the real thread resume happens after the watchpoint gets
1258 presented and thus its LWP_INFO->STATUS gets reset.
1259
1260 Therefore the hardware watchpoint hit can get safely reset on the
1261 watchpoint removal from inferior. */
a79d3c27 1262
b40ce68a 1263static void
a5606eee 1264update_watchpoint (struct breakpoint *b, int reparse)
7270d8f2 1265{
a5606eee 1266 int within_current_scope;
a5606eee 1267 struct frame_id saved_frame_id;
66076460 1268 int frame_saved;
a5606eee 1269
d0fb5eae
JK
1270 gdb_assert (is_watchpoint (b));
1271
f6bc2008
PA
1272 /* If this is a local watchpoint, we only want to check if the
1273 watchpoint frame is in scope if the current thread is the thread
1274 that was used to create the watchpoint. */
1275 if (!watchpoint_in_thread_scope (b))
1276 return;
1277
a5606eee
VP
1278 if (b->disposition == disp_del_at_next_stop)
1279 return;
1280
66076460 1281 frame_saved = 0;
a5606eee
VP
1282
1283 /* Determine if the watchpoint is within scope. */
1284 if (b->exp_valid_block == NULL)
1285 within_current_scope = 1;
1286 else
1287 {
b5db5dfc
UW
1288 struct frame_info *fi = get_current_frame ();
1289 struct gdbarch *frame_arch = get_frame_arch (fi);
1290 CORE_ADDR frame_pc = get_frame_pc (fi);
1291
1292 /* If we're in a function epilogue, unwinding may not work
1293 properly, so do not attempt to recreate locations at this
1294 point. See similar comments in watchpoint_check. */
1295 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1296 return;
66076460
DJ
1297
1298 /* Save the current frame's ID so we can restore it after
1299 evaluating the watchpoint expression on its own frame. */
1300 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1301 took a frame parameter, so that we didn't have to change the
1302 selected frame. */
1303 frame_saved = 1;
1304 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1305
a5606eee
VP
1306 fi = frame_find_by_id (b->watchpoint_frame);
1307 within_current_scope = (fi != NULL);
1308 if (within_current_scope)
1309 select_frame (fi);
1310 }
1311
b5db5dfc
UW
1312 /* We don't free locations. They are stored in the bp_location array
1313 and update_global_location_list will eventually delete them and
1314 remove breakpoints if needed. */
1315 b->loc = NULL;
1316
a5606eee
VP
1317 if (within_current_scope && reparse)
1318 {
1319 char *s;
d63d0675 1320
a5606eee
VP
1321 if (b->exp)
1322 {
1323 xfree (b->exp);
1324 b->exp = NULL;
1325 }
d63d0675 1326 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1327 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1328 /* If the meaning of expression itself changed, the old value is
1329 no longer relevant. We don't want to report a watchpoint hit
1330 to the user when the old value and the new value may actually
1331 be completely different objects. */
1332 value_free (b->val);
fa4727a6
DJ
1333 b->val = NULL;
1334 b->val_valid = 0;
60e1c644
PA
1335
1336 /* Note that unlike with breakpoints, the watchpoint's condition
1337 expression is stored in the breakpoint object, not in the
1338 locations (re)created below. */
1339 if (b->cond_string != NULL)
1340 {
1341 if (b->cond_exp != NULL)
1342 {
1343 xfree (b->cond_exp);
1344 b->cond_exp = NULL;
1345 }
1346
1347 s = b->cond_string;
1348 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1349 }
a5606eee 1350 }
a5606eee
VP
1351
1352 /* If we failed to parse the expression, for example because
1353 it refers to a global variable in a not-yet-loaded shared library,
1354 don't try to insert watchpoint. We don't automatically delete
1355 such watchpoint, though, since failure to parse expression
1356 is different from out-of-scope watchpoint. */
2d134ed3
PA
1357 if ( !target_has_execution)
1358 {
1359 /* Without execution, memory can't change. No use to try and
1360 set watchpoint locations. The watchpoint will be reset when
1361 the target gains execution, through breakpoint_re_set. */
1362 }
1363 else if (within_current_scope && b->exp)
a5606eee 1364 {
0cf6dd15 1365 int pc = 0;
fa4727a6 1366 struct value *val_chain, *v, *result, *next;
2d134ed3 1367 struct program_space *frame_pspace;
a5606eee 1368
0cf6dd15 1369 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1370
a5606eee
VP
1371 /* Avoid setting b->val if it's already set. The meaning of
1372 b->val is 'the last value' user saw, and we should update
1373 it only if we reported that last value to user. As it
9c06b0b4
TJB
1374 happens, the code that reports it updates b->val directly.
1375 We don't keep track of the memory value for masked
1376 watchpoints. */
1377 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6
DJ
1378 {
1379 b->val = v;
1380 b->val_valid = 1;
1381 }
a5606eee 1382
2d134ed3
PA
1383 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1384
a5606eee 1385 /* Look at each value on the value chain. */
9fa40276 1386 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1387 {
1388 /* If it's a memory location, and GDB actually needed
1389 its contents to evaluate the expression, then we
fa4727a6
DJ
1390 must watch it. If the first value returned is
1391 still lazy, that means an error occurred reading it;
1392 watch it anyway in case it becomes readable. */
a5606eee 1393 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1394 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1395 {
1396 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1397
a5606eee
VP
1398 /* We only watch structs and arrays if user asked
1399 for it explicitly, never if they just happen to
1400 appear in the middle of some value chain. */
fa4727a6 1401 if (v == result
a5606eee
VP
1402 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1403 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1404 {
1405 CORE_ADDR addr;
1406 int len, type;
1407 struct bp_location *loc, **tmp;
1408
42ae5230 1409 addr = value_address (v);
a5606eee
VP
1410 len = TYPE_LENGTH (value_type (v));
1411 type = hw_write;
1412 if (b->type == bp_read_watchpoint)
1413 type = hw_read;
1414 else if (b->type == bp_access_watchpoint)
1415 type = hw_access;
1416
39d61571 1417 loc = allocate_bp_location (b);
a5606eee
VP
1418 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1419 ;
1420 *tmp = loc;
a6d9a66e 1421 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1422
1423 loc->pspace = frame_pspace;
a5606eee
VP
1424 loc->address = addr;
1425 loc->length = len;
1426 loc->watchpoint_type = type;
1427 }
1428 }
9fa40276
TJB
1429 }
1430
1431 /* Change the type of breakpoint between hardware assisted or
1432 an ordinary watchpoint depending on the hardware support
1433 and free hardware slots. REPARSE is set when the inferior
1434 is started. */
a9634178 1435 if (reparse)
9fa40276 1436 {
e09342b5 1437 int reg_cnt;
9fa40276
TJB
1438 enum bp_loc_type loc_type;
1439 struct bp_location *bl;
a5606eee 1440
a9634178 1441 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1442
1443 if (reg_cnt)
9fa40276
TJB
1444 {
1445 int i, target_resources_ok, other_type_used;
1446
a9634178
TJB
1447 /* Use an exact watchpoint when there's only one memory region to be
1448 watched, and only one debug register is needed to watch it. */
1449 b->exact = target_exact_watchpoints && reg_cnt == 1;
1450
9fa40276 1451 /* We need to determine how many resources are already
e09342b5
TJB
1452 used for all other hardware watchpoints plus this one
1453 to see if we still have enough resources to also fit
1454 this watchpoint in as well. To guarantee the
1455 hw_watchpoint_used_count call below counts this
1456 watchpoint, make sure that it is marked as a hardware
1457 watchpoint. */
a9634178
TJB
1458 if (b->type == bp_watchpoint)
1459 b->type = bp_hardware_watchpoint;
9fa40276 1460
a9634178 1461 i = hw_watchpoint_used_count (b->type, &other_type_used);
e09342b5 1462 target_resources_ok = target_can_use_hardware_watchpoint
a9634178 1463 (b->type, i, other_type_used);
e09342b5 1464 if (target_resources_ok <= 0)
a9634178 1465 {
348d480f 1466 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1467
1468 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1469 error (_("Target does not support this type of "
1470 "hardware watchpoint."));
9c06b0b4
TJB
1471 else if (target_resources_ok < 0 && !sw_mode)
1472 error (_("There are not enough available hardware "
1473 "resources for this watchpoint."));
a9634178
TJB
1474 else
1475 b->type = bp_watchpoint;
1476 }
9fa40276 1477 }
348d480f 1478 else if (!b->ops->works_in_software_mode (b))
a9634178
TJB
1479 error (_("Expression cannot be implemented with "
1480 "read/access watchpoint."));
9fa40276
TJB
1481 else
1482 b->type = bp_watchpoint;
1483
1484 loc_type = (b->type == bp_watchpoint? bp_loc_other
1485 : bp_loc_hardware_watchpoint);
1486 for (bl = b->loc; bl; bl = bl->next)
1487 bl->loc_type = loc_type;
1488 }
1489
1490 for (v = val_chain; v; v = next)
1491 {
a5606eee
VP
1492 next = value_next (v);
1493 if (v != b->val)
1494 value_free (v);
1495 }
1496
c7437ca6
PA
1497 /* If a software watchpoint is not watching any memory, then the
1498 above left it without any location set up. But,
1499 bpstat_stop_status requires a location to be able to report
1500 stops, so make sure there's at least a dummy one. */
1501 if (b->type == bp_watchpoint && b->loc == NULL)
1502 {
1503 b->loc = allocate_bp_location (b);
1504 b->loc->pspace = frame_pspace;
1505 b->loc->address = -1;
1506 b->loc->length = -1;
1507 b->loc->watchpoint_type = -1;
1508 }
a5606eee
VP
1509 }
1510 else if (!within_current_scope)
7270d8f2 1511 {
ac74f770
MS
1512 printf_filtered (_("\
1513Watchpoint %d deleted because the program has left the block\n\
1514in which its expression is valid.\n"),
a5606eee 1515 b->number);
d0fb5eae 1516 watchpoint_del_at_next_stop (b);
7270d8f2 1517 }
a5606eee
VP
1518
1519 /* Restore the selected frame. */
66076460
DJ
1520 if (frame_saved)
1521 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1522}
1523
a5606eee 1524
74960c60
VP
1525/* Returns 1 iff breakpoint location should be
1526 inserted in the inferior. */
1527static int
35df4500 1528should_be_inserted (struct bp_location *bl)
74960c60 1529{
35df4500 1530 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1531 return 0;
1532
35df4500 1533 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1534 return 0;
1535
35df4500 1536 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1537 return 0;
1538
56710373
PA
1539 /* This is set for example, when we're attached to the parent of a
1540 vfork, and have detached from the child. The child is running
1541 free, and we expect it to do an exec or exit, at which point the
1542 OS makes the parent schedulable again (and the target reports
1543 that the vfork is done). Until the child is done with the shared
1544 memory region, do not insert breakpoints in the parent, otherwise
1545 the child could still trip on the parent's breakpoints. Since
1546 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1547 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1548 return 0;
1549
1042e4c0
SS
1550 /* Tracepoints are inserted by the target at a time of its choosing,
1551 not by us. */
35df4500 1552 if (is_tracepoint (bl->owner))
1042e4c0
SS
1553 return 0;
1554
74960c60
VP
1555 return 1;
1556}
1557
35df4500
TJB
1558/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1559 location. Any error messages are printed to TMP_ERROR_STREAM; and
1560 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
879bfdc2 1561
4a64f543
MS
1562 NOTE drow/2003-09-09: This routine could be broken down to an
1563 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1564static int
35df4500 1565insert_bp_location (struct bp_location *bl,
26bb91f3 1566 struct ui_file *tmp_error_stream,
fa3a767f 1567 int *disabled_breaks,
26bb91f3 1568 int *hw_breakpoint_error)
879bfdc2
DJ
1569{
1570 int val = 0;
1571
35df4500 1572 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1573 return 0;
1574
8181d85f 1575 /* Initialize the target-specific information. */
35df4500
TJB
1576 memset (&bl->target_info, 0, sizeof (bl->target_info));
1577 bl->target_info.placed_address = bl->address;
1578 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1579 bl->target_info.length = bl->length;
8181d85f 1580
35df4500
TJB
1581 if (bl->loc_type == bp_loc_software_breakpoint
1582 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1583 {
35df4500 1584 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1585 {
1586 /* If the explicitly specified breakpoint type
1587 is not hardware breakpoint, check the memory map to see
1588 if the breakpoint address is in read only memory or not.
4a64f543 1589
765dc015
VP
1590 Two important cases are:
1591 - location type is not hardware breakpoint, memory
1592 is readonly. We change the type of the location to
1593 hardware breakpoint.
4a64f543
MS
1594 - location type is hardware breakpoint, memory is
1595 read-write. This means we've previously made the
1596 location hardware one, but then the memory map changed,
1597 so we undo.
765dc015 1598
4a64f543
MS
1599 When breakpoints are removed, remove_breakpoints will use
1600 location types we've just set here, the only possible
1601 problem is that memory map has changed during running
1602 program, but it's not going to work anyway with current
1603 gdb. */
765dc015 1604 struct mem_region *mr
35df4500 1605 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1606
1607 if (mr)
1608 {
1609 if (automatic_hardware_breakpoints)
1610 {
765dc015
VP
1611 enum bp_loc_type new_type;
1612
1613 if (mr->attrib.mode != MEM_RW)
1614 new_type = bp_loc_hardware_breakpoint;
1615 else
1616 new_type = bp_loc_software_breakpoint;
1617
35df4500 1618 if (new_type != bl->loc_type)
765dc015
VP
1619 {
1620 static int said = 0;
cc59ec59 1621
35df4500 1622 bl->loc_type = new_type;
765dc015
VP
1623 if (!said)
1624 {
3e43a32a
MS
1625 fprintf_filtered (gdb_stdout,
1626 _("Note: automatically using "
1627 "hardware breakpoints for "
1628 "read-only addresses.\n"));
765dc015
VP
1629 said = 1;
1630 }
1631 }
1632 }
35df4500 1633 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1634 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1635 warning (_("cannot set software breakpoint "
1636 "at readonly address %s"),
35df4500 1637 paddress (bl->gdbarch, bl->address));
765dc015
VP
1638 }
1639 }
1640
879bfdc2
DJ
1641 /* First check to see if we have to handle an overlay. */
1642 if (overlay_debugging == ovly_off
35df4500
TJB
1643 || bl->section == NULL
1644 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1645 {
1646 /* No overlay handling: just set the breakpoint. */
1647
348d480f 1648 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1649 }
1650 else
1651 {
4a64f543 1652 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1653 Shall we set a breakpoint at the LMA? */
1654 if (!overlay_events_enabled)
1655 {
1656 /* Yes -- overlay event support is not active,
1657 so we must try to set a breakpoint at the LMA.
1658 This will not work for a hardware breakpoint. */
35df4500 1659 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1660 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1661 bl->owner->number);
879bfdc2
DJ
1662 else
1663 {
35df4500
TJB
1664 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1665 bl->section);
879bfdc2 1666 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1667 bl->overlay_target_info = bl->target_info;
1668 bl->overlay_target_info.placed_address = addr;
1669 val = target_insert_breakpoint (bl->gdbarch,
1670 &bl->overlay_target_info);
879bfdc2 1671 if (val != 0)
99361f52 1672 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1673 "Overlay breakpoint %d "
1674 "failed: in ROM?\n",
35df4500 1675 bl->owner->number);
879bfdc2
DJ
1676 }
1677 }
1678 /* Shall we set a breakpoint at the VMA? */
35df4500 1679 if (section_is_mapped (bl->section))
879bfdc2
DJ
1680 {
1681 /* Yes. This overlay section is mapped into memory. */
348d480f 1682 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1683 }
1684 else
1685 {
1686 /* No. This breakpoint will not be inserted.
1687 No error, but do not mark the bp as 'inserted'. */
1688 return 0;
1689 }
1690 }
1691
1692 if (val)
1693 {
1694 /* Can't set the breakpoint. */
35df4500 1695 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1696 {
4a64f543 1697 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1698 val = 0;
35df4500 1699 bl->shlib_disabled = 1;
8d3788bd 1700 observer_notify_breakpoint_modified (bl->owner);
879bfdc2
DJ
1701 if (!*disabled_breaks)
1702 {
1703 fprintf_unfiltered (tmp_error_stream,
1704 "Cannot insert breakpoint %d.\n",
35df4500 1705 bl->owner->number);
879bfdc2 1706 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1707 "Temporarily disabling shared "
1708 "library breakpoints:\n");
879bfdc2
DJ
1709 }
1710 *disabled_breaks = 1;
1711 fprintf_unfiltered (tmp_error_stream,
35df4500 1712 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1713 }
1714 else
879bfdc2 1715 {
35df4500 1716 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1717 {
1718 *hw_breakpoint_error = 1;
3e43a32a
MS
1719 fprintf_unfiltered (tmp_error_stream,
1720 "Cannot insert hardware "
1721 "breakpoint %d.\n",
35df4500 1722 bl->owner->number);
879bfdc2
DJ
1723 }
1724 else
1725 {
1726 fprintf_unfiltered (tmp_error_stream,
1727 "Cannot insert breakpoint %d.\n",
35df4500 1728 bl->owner->number);
879bfdc2
DJ
1729 fprintf_filtered (tmp_error_stream,
1730 "Error accessing memory address ");
35df4500 1731 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1732 tmp_error_stream);
879bfdc2
DJ
1733 fprintf_filtered (tmp_error_stream, ": %s.\n",
1734 safe_strerror (val));
1735 }
1736
1737 }
1738 }
1739 else
35df4500 1740 bl->inserted = 1;
879bfdc2
DJ
1741
1742 return val;
1743 }
1744
35df4500 1745 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1746 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1747 watchpoints. It's not clear that it's necessary... */
35df4500 1748 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1749 {
77b06cd7
TJB
1750 gdb_assert (bl->owner->ops != NULL
1751 && bl->owner->ops->insert_location != NULL);
1752
1753 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1754
1755 /* If trying to set a read-watchpoint, and it turns out it's not
1756 supported, try emulating one with an access watchpoint. */
35df4500 1757 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1758 {
1759 struct bp_location *loc, **loc_temp;
1760
1761 /* But don't try to insert it, if there's already another
1762 hw_access location that would be considered a duplicate
1763 of this one. */
1764 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1765 if (loc != bl
85d721b8 1766 && loc->watchpoint_type == hw_access
35df4500 1767 && watchpoint_locations_match (bl, loc))
85d721b8 1768 {
35df4500
TJB
1769 bl->duplicate = 1;
1770 bl->inserted = 1;
1771 bl->target_info = loc->target_info;
1772 bl->watchpoint_type = hw_access;
85d721b8
PA
1773 val = 0;
1774 break;
1775 }
1776
1777 if (val == 1)
1778 {
77b06cd7
TJB
1779 bl->watchpoint_type = hw_access;
1780 val = bl->owner->ops->insert_location (bl);
1781
1782 if (val)
1783 /* Back to the original value. */
1784 bl->watchpoint_type = hw_read;
85d721b8
PA
1785 }
1786 }
1787
35df4500 1788 bl->inserted = (val == 0);
879bfdc2
DJ
1789 }
1790
35df4500 1791 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1792 {
77b06cd7
TJB
1793 gdb_assert (bl->owner->ops != NULL
1794 && bl->owner->ops->insert_location != NULL);
1795
1796 val = bl->owner->ops->insert_location (bl);
1797 if (val)
1798 {
1799 bl->owner->enable_state = bp_disabled;
1800
1801 if (val == 1)
1802 warning (_("\
1803Error inserting catchpoint %d: Your system does not support this type\n\
1804of catchpoint."), bl->owner->number);
1805 else
1806 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1807 }
1808
1809 bl->inserted = (val == 0);
1640b821
DJ
1810
1811 /* We've already printed an error message if there was a problem
1812 inserting this catchpoint, and we've disabled the catchpoint,
1813 so just return success. */
1814 return 0;
879bfdc2
DJ
1815 }
1816
1817 return 0;
1818}
1819
6c95b8df
PA
1820/* This function is called when program space PSPACE is about to be
1821 deleted. It takes care of updating breakpoints to not reference
1822 PSPACE anymore. */
1823
1824void
1825breakpoint_program_space_exit (struct program_space *pspace)
1826{
1827 struct breakpoint *b, *b_temp;
876fa593 1828 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1829
1830 /* Remove any breakpoint that was set through this program space. */
1831 ALL_BREAKPOINTS_SAFE (b, b_temp)
1832 {
1833 if (b->pspace == pspace)
1834 delete_breakpoint (b);
1835 }
1836
1837 /* Breakpoints set through other program spaces could have locations
1838 bound to PSPACE as well. Remove those. */
876fa593 1839 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1840 {
1841 struct bp_location *tmp;
1842
1843 if (loc->pspace == pspace)
1844 {
2bdf28a0 1845 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1846 if (loc->owner->loc == loc)
1847 loc->owner->loc = loc->next;
1848 else
1849 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1850 if (tmp->next == loc)
1851 {
1852 tmp->next = loc->next;
1853 break;
1854 }
1855 }
1856 }
1857
1858 /* Now update the global location list to permanently delete the
1859 removed locations above. */
1860 update_global_location_list (0);
1861}
1862
74960c60
VP
1863/* Make sure all breakpoints are inserted in inferior.
1864 Throws exception on any error.
1865 A breakpoint that is already inserted won't be inserted
1866 again, so calling this function twice is safe. */
1867void
1868insert_breakpoints (void)
1869{
1870 struct breakpoint *bpt;
1871
1872 ALL_BREAKPOINTS (bpt)
1873 if (is_hardware_watchpoint (bpt))
4a64f543 1874 update_watchpoint (bpt, 0 /* don't reparse. */);
74960c60 1875
b60e7edf 1876 update_global_location_list (1);
74960c60 1877
c35b1492
PA
1878 /* update_global_location_list does not insert breakpoints when
1879 always_inserted_mode is not enabled. Explicitly insert them
1880 now. */
1881 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1882 insert_breakpoint_locations ();
1883}
1884
c906108c
SS
1885/* insert_breakpoints is used when starting or continuing the program.
1886 remove_breakpoints is used when the program stops.
1887 Both return zero if successful,
1888 or an `errno' value if could not write the inferior. */
1889
74960c60
VP
1890static void
1891insert_breakpoint_locations (void)
c906108c 1892{
a5606eee 1893 struct breakpoint *bpt;
35df4500 1894 struct bp_location *bl, **blp_tmp;
e236ba44 1895 int error = 0;
c906108c
SS
1896 int val = 0;
1897 int disabled_breaks = 0;
81d0cc19 1898 int hw_breakpoint_error = 0;
c906108c 1899
81d0cc19 1900 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1901 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1902
81d0cc19
GS
1903 /* Explicitly mark the warning -- this will only be printed if
1904 there was an error. */
1905 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1906
1907 save_current_space_and_thread ();
1908
35df4500 1909 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1910 {
35df4500 1911 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1912 continue;
1913
4a64f543
MS
1914 /* There is no point inserting thread-specific breakpoints if
1915 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1916 has BL->OWNER always non-NULL. */
35df4500
TJB
1917 if (bl->owner->thread != -1
1918 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1919 continue;
1920
35df4500 1921 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1922
1923 /* For targets that support global breakpoints, there's no need
1924 to select an inferior to insert breakpoint to. In fact, even
1925 if we aren't attached to any process yet, we should still
1926 insert breakpoints. */
1927 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1928 && ptid_equal (inferior_ptid, null_ptid))
1929 continue;
1930
35df4500 1931 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
879bfdc2
DJ
1932 &hw_breakpoint_error);
1933 if (val)
e236ba44 1934 error = val;
879bfdc2 1935 }
c906108c 1936
4a64f543
MS
1937 /* If we failed to insert all locations of a watchpoint, remove
1938 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
1939 ALL_BREAKPOINTS (bpt)
1940 {
1941 int some_failed = 0;
1942 struct bp_location *loc;
1943
1944 if (!is_hardware_watchpoint (bpt))
1945 continue;
1946
d6b74ac4 1947 if (!breakpoint_enabled (bpt))
a5606eee 1948 continue;
74960c60
VP
1949
1950 if (bpt->disposition == disp_del_at_next_stop)
1951 continue;
a5606eee
VP
1952
1953 for (loc = bpt->loc; loc; loc = loc->next)
56710373 1954 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
1955 {
1956 some_failed = 1;
1957 break;
1958 }
1959 if (some_failed)
1960 {
1961 for (loc = bpt->loc; loc; loc = loc->next)
1962 if (loc->inserted)
1963 remove_breakpoint (loc, mark_uninserted);
1964
1965 hw_breakpoint_error = 1;
1966 fprintf_unfiltered (tmp_error_stream,
1967 "Could not insert hardware watchpoint %d.\n",
1968 bpt->number);
1969 error = -1;
1970 }
1971 }
1972
e236ba44 1973 if (error)
81d0cc19
GS
1974 {
1975 /* If a hardware breakpoint or watchpoint was inserted, add a
1976 message about possibly exhausted resources. */
879bfdc2 1977 if (hw_breakpoint_error)
81d0cc19 1978 {
c6510018
MS
1979 fprintf_unfiltered (tmp_error_stream,
1980 "Could not insert hardware breakpoints:\n\
1981You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 1982 }
81d0cc19
GS
1983 target_terminal_ours_for_output ();
1984 error_stream (tmp_error_stream);
1985 }
f7545552
TT
1986
1987 do_cleanups (cleanups);
c906108c
SS
1988}
1989
c906108c 1990int
fba45db2 1991remove_breakpoints (void)
c906108c 1992{
35df4500 1993 struct bp_location *bl, **blp_tmp;
3a1bae8e 1994 int val = 0;
c906108c 1995
35df4500 1996 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 1997 {
35df4500
TJB
1998 if (bl->inserted)
1999 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2000 }
3a1bae8e 2001 return val;
c906108c
SS
2002}
2003
6c95b8df
PA
2004/* Remove breakpoints of process PID. */
2005
2006int
2007remove_breakpoints_pid (int pid)
2008{
35df4500 2009 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2010 int val;
2011 struct inferior *inf = find_inferior_pid (pid);
2012
35df4500 2013 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2014 {
35df4500 2015 if (bl->pspace != inf->pspace)
6c95b8df
PA
2016 continue;
2017
35df4500 2018 if (bl->inserted)
6c95b8df 2019 {
35df4500 2020 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2021 if (val != 0)
2022 return val;
2023 }
2024 }
2025 return 0;
2026}
2027
c906108c 2028int
fba45db2 2029reattach_breakpoints (int pid)
c906108c 2030{
6c95b8df 2031 struct cleanup *old_chain;
35df4500 2032 struct bp_location *bl, **blp_tmp;
c906108c 2033 int val;
86b887df 2034 struct ui_file *tmp_error_stream;
fa3a767f 2035 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2036 struct inferior *inf;
2037 struct thread_info *tp;
2038
2039 tp = any_live_thread_of_process (pid);
2040 if (tp == NULL)
2041 return 1;
2042
2043 inf = find_inferior_pid (pid);
2044 old_chain = save_inferior_ptid ();
2045
2046 inferior_ptid = tp->ptid;
a4954f26 2047
86b887df 2048 tmp_error_stream = mem_fileopen ();
a4954f26 2049 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2050
35df4500 2051 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2052 {
35df4500 2053 if (bl->pspace != inf->pspace)
6c95b8df
PA
2054 continue;
2055
35df4500 2056 if (bl->inserted)
c5aa993b 2057 {
35df4500
TJB
2058 bl->inserted = 0;
2059 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2060 if (val != 0)
2061 {
ce696e05 2062 do_cleanups (old_chain);
c5aa993b
JM
2063 return val;
2064 }
2065 }
2066 }
ce696e05 2067 do_cleanups (old_chain);
c906108c
SS
2068 return 0;
2069}
2070
e58b0e63
PA
2071static int internal_breakpoint_number = -1;
2072
84f4c1fe
PM
2073/* Set the breakpoint number of B, depending on the value of INTERNAL.
2074 If INTERNAL is non-zero, the breakpoint number will be populated
2075 from internal_breakpoint_number and that variable decremented.
e5dd4106 2076 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2077 breakpoint_count and that value incremented. Internal breakpoints
2078 do not set the internal var bpnum. */
2079static void
2080set_breakpoint_number (int internal, struct breakpoint *b)
2081{
2082 if (internal)
2083 b->number = internal_breakpoint_number--;
2084 else
2085 {
2086 set_breakpoint_count (breakpoint_count + 1);
2087 b->number = breakpoint_count;
2088 }
2089}
2090
e62c965a 2091static struct breakpoint *
a6d9a66e 2092create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0
PA
2093 CORE_ADDR address, enum bptype type,
2094 struct breakpoint_ops *ops)
e62c965a 2095{
e62c965a
PP
2096 struct symtab_and_line sal;
2097 struct breakpoint *b;
2098
4a64f543 2099 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2100
2101 sal.pc = address;
2102 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2103 sal.pspace = current_program_space;
e62c965a 2104
06edf0c0 2105 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2106 b->number = internal_breakpoint_number--;
2107 b->disposition = disp_donttouch;
2108
2109 return b;
2110}
2111
17450429
PP
2112static const char *const longjmp_names[] =
2113 {
2114 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2115 };
2116#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2117
2118/* Per-objfile data private to breakpoint.c. */
2119struct breakpoint_objfile_data
2120{
2121 /* Minimal symbol for "_ovly_debug_event" (if any). */
2122 struct minimal_symbol *overlay_msym;
2123
2124 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2125 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2126
2127 /* Minimal symbol for "std::terminate()" (if any). */
2128 struct minimal_symbol *terminate_msym;
2129
2130 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2131 struct minimal_symbol *exception_msym;
2132};
2133
2134static const struct objfile_data *breakpoint_objfile_key;
2135
2136/* Minimal symbol not found sentinel. */
2137static struct minimal_symbol msym_not_found;
2138
2139/* Returns TRUE if MSYM point to the "not found" sentinel. */
2140
2141static int
2142msym_not_found_p (const struct minimal_symbol *msym)
2143{
2144 return msym == &msym_not_found;
2145}
2146
2147/* Return per-objfile data needed by breakpoint.c.
2148 Allocate the data if necessary. */
2149
2150static struct breakpoint_objfile_data *
2151get_breakpoint_objfile_data (struct objfile *objfile)
2152{
2153 struct breakpoint_objfile_data *bp_objfile_data;
2154
2155 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2156 if (bp_objfile_data == NULL)
2157 {
2158 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2159 sizeof (*bp_objfile_data));
2160
2161 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2162 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2163 }
2164 return bp_objfile_data;
2165}
2166
e62c965a 2167static void
af02033e 2168create_overlay_event_breakpoint (void)
e62c965a 2169{
69de3c6a 2170 struct objfile *objfile;
af02033e 2171 const char *const func_name = "_ovly_debug_event";
e62c965a 2172
69de3c6a
PP
2173 ALL_OBJFILES (objfile)
2174 {
2175 struct breakpoint *b;
17450429
PP
2176 struct breakpoint_objfile_data *bp_objfile_data;
2177 CORE_ADDR addr;
69de3c6a 2178
17450429
PP
2179 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2180
2181 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2182 continue;
2183
2184 if (bp_objfile_data->overlay_msym == NULL)
2185 {
2186 struct minimal_symbol *m;
2187
2188 m = lookup_minimal_symbol_text (func_name, objfile);
2189 if (m == NULL)
2190 {
2191 /* Avoid future lookups in this objfile. */
2192 bp_objfile_data->overlay_msym = &msym_not_found;
2193 continue;
2194 }
2195 bp_objfile_data->overlay_msym = m;
2196 }
e62c965a 2197
17450429
PP
2198 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2199 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2200 bp_overlay_event,
2201 &internal_breakpoint_ops);
69de3c6a 2202 b->addr_string = xstrdup (func_name);
e62c965a 2203
69de3c6a
PP
2204 if (overlay_debugging == ovly_auto)
2205 {
2206 b->enable_state = bp_enabled;
2207 overlay_events_enabled = 1;
2208 }
2209 else
2210 {
2211 b->enable_state = bp_disabled;
2212 overlay_events_enabled = 0;
2213 }
e62c965a
PP
2214 }
2215 update_global_location_list (1);
2216}
2217
0fd8e87f 2218static void
af02033e 2219create_longjmp_master_breakpoint (void)
0fd8e87f 2220{
6c95b8df 2221 struct program_space *pspace;
6c95b8df
PA
2222 struct cleanup *old_chain;
2223
2224 old_chain = save_current_program_space ();
0fd8e87f 2225
6c95b8df 2226 ALL_PSPACES (pspace)
af02033e
PP
2227 {
2228 struct objfile *objfile;
2229
2230 set_current_program_space (pspace);
2231
2232 ALL_OBJFILES (objfile)
0fd8e87f 2233 {
af02033e
PP
2234 int i;
2235 struct gdbarch *gdbarch;
17450429 2236 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2237
af02033e
PP
2238 gdbarch = get_objfile_arch (objfile);
2239 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2240 continue;
2241
17450429
PP
2242 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2243
2244 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2245 {
2246 struct breakpoint *b;
af02033e 2247 const char *func_name;
17450429 2248 CORE_ADDR addr;
6c95b8df 2249
17450429 2250 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2251 continue;
0fd8e87f 2252
17450429
PP
2253 func_name = longjmp_names[i];
2254 if (bp_objfile_data->longjmp_msym[i] == NULL)
2255 {
2256 struct minimal_symbol *m;
2257
2258 m = lookup_minimal_symbol_text (func_name, objfile);
2259 if (m == NULL)
2260 {
2261 /* Prevent future lookups in this objfile. */
2262 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2263 continue;
2264 }
2265 bp_objfile_data->longjmp_msym[i] = m;
2266 }
2267
2268 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2269 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2270 &internal_breakpoint_ops);
af02033e
PP
2271 b->addr_string = xstrdup (func_name);
2272 b->enable_state = bp_disabled;
2273 }
0fd8e87f 2274 }
af02033e 2275 }
0fd8e87f 2276 update_global_location_list (1);
6c95b8df
PA
2277
2278 do_cleanups (old_chain);
0fd8e87f
UW
2279}
2280
af02033e 2281/* Create a master std::terminate breakpoint. */
aa7d318d 2282static void
af02033e 2283create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2284{
2285 struct program_space *pspace;
aa7d318d 2286 struct cleanup *old_chain;
af02033e 2287 const char *const func_name = "std::terminate()";
aa7d318d
TT
2288
2289 old_chain = save_current_program_space ();
2290
2291 ALL_PSPACES (pspace)
17450429
PP
2292 {
2293 struct objfile *objfile;
2294 CORE_ADDR addr;
2295
2296 set_current_program_space (pspace);
2297
aa7d318d
TT
2298 ALL_OBJFILES (objfile)
2299 {
2300 struct breakpoint *b;
17450429 2301 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2302
17450429 2303 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2304
17450429
PP
2305 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2306 continue;
2307
2308 if (bp_objfile_data->terminate_msym == NULL)
2309 {
2310 struct minimal_symbol *m;
2311
2312 m = lookup_minimal_symbol (func_name, NULL, objfile);
2313 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2314 && MSYMBOL_TYPE (m) != mst_file_text))
2315 {
2316 /* Prevent future lookups in this objfile. */
2317 bp_objfile_data->terminate_msym = &msym_not_found;
2318 continue;
2319 }
2320 bp_objfile_data->terminate_msym = m;
2321 }
aa7d318d 2322
17450429
PP
2323 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2324 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2325 bp_std_terminate_master,
2326 &internal_breakpoint_ops);
aa7d318d
TT
2327 b->addr_string = xstrdup (func_name);
2328 b->enable_state = bp_disabled;
2329 }
17450429
PP
2330 }
2331
aa7d318d
TT
2332 update_global_location_list (1);
2333
2334 do_cleanups (old_chain);
2335}
2336
186c406b
TT
2337/* Install a master breakpoint on the unwinder's debug hook. */
2338
2339void
2340create_exception_master_breakpoint (void)
2341{
2342 struct objfile *objfile;
17450429 2343 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2344
2345 ALL_OBJFILES (objfile)
2346 {
17450429
PP
2347 struct breakpoint *b;
2348 struct gdbarch *gdbarch;
2349 struct breakpoint_objfile_data *bp_objfile_data;
2350 CORE_ADDR addr;
2351
2352 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2353
2354 if (msym_not_found_p (bp_objfile_data->exception_msym))
2355 continue;
2356
2357 gdbarch = get_objfile_arch (objfile);
186c406b 2358
17450429 2359 if (bp_objfile_data->exception_msym == NULL)
186c406b 2360 {
17450429 2361 struct minimal_symbol *debug_hook;
186c406b 2362
17450429
PP
2363 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2364 if (debug_hook == NULL)
2365 {
2366 bp_objfile_data->exception_msym = &msym_not_found;
2367 continue;
2368 }
2369
2370 bp_objfile_data->exception_msym = debug_hook;
186c406b 2371 }
17450429
PP
2372
2373 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2374 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2375 &current_target);
06edf0c0
PA
2376 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2377 &internal_breakpoint_ops);
17450429
PP
2378 b->addr_string = xstrdup (func_name);
2379 b->enable_state = bp_disabled;
186c406b
TT
2380 }
2381
2382 update_global_location_list (1);
2383}
2384
c906108c 2385void
fba45db2 2386update_breakpoints_after_exec (void)
c906108c 2387{
35df4500 2388 struct breakpoint *b, *b_tmp;
876fa593 2389 struct bp_location *bploc, **bplocp_tmp;
c906108c 2390
25b22b0a
PA
2391 /* We're about to delete breakpoints from GDB's lists. If the
2392 INSERTED flag is true, GDB will try to lift the breakpoints by
2393 writing the breakpoints' "shadow contents" back into memory. The
2394 "shadow contents" are NOT valid after an exec, so GDB should not
2395 do that. Instead, the target is responsible from marking
2396 breakpoints out as soon as it detects an exec. We don't do that
2397 here instead, because there may be other attempts to delete
2398 breakpoints after detecting an exec and before reaching here. */
876fa593 2399 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2400 if (bploc->pspace == current_program_space)
2401 gdb_assert (!bploc->inserted);
c906108c 2402
35df4500 2403 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2404 {
6c95b8df
PA
2405 if (b->pspace != current_program_space)
2406 continue;
2407
4a64f543 2408 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2409 if (b->type == bp_shlib_event)
2410 {
2411 delete_breakpoint (b);
2412 continue;
2413 }
c906108c 2414
4a64f543 2415 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2416 if (b->type == bp_jit_event)
2417 {
2418 delete_breakpoint (b);
2419 continue;
2420 }
2421
1900040c 2422 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2423 as must overlay event and longjmp master breakpoints. */
2424 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2425 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2426 || b->type == bp_exception_master)
c4093a6a
JM
2427 {
2428 delete_breakpoint (b);
2429 continue;
2430 }
2431
4a64f543 2432 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 2433 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
2434 {
2435 delete_breakpoint (b);
2436 continue;
2437 }
2438
611c83ae
PA
2439 /* Longjmp and longjmp-resume breakpoints are also meaningless
2440 after an exec. */
186c406b
TT
2441 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2442 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2443 {
2444 delete_breakpoint (b);
2445 continue;
2446 }
2447
ce78b96d
JB
2448 if (b->type == bp_catchpoint)
2449 {
2450 /* For now, none of the bp_catchpoint breakpoints need to
2451 do anything at this point. In the future, if some of
2452 the catchpoints need to something, we will need to add
2453 a new method, and call this method from here. */
2454 continue;
2455 }
2456
c5aa993b
JM
2457 /* bp_finish is a special case. The only way we ought to be able
2458 to see one of these when an exec() has happened, is if the user
2459 caught a vfork, and then said "finish". Ordinarily a finish just
2460 carries them to the call-site of the current callee, by setting
2461 a temporary bp there and resuming. But in this case, the finish
2462 will carry them entirely through the vfork & exec.
2463
2464 We don't want to allow a bp_finish to remain inserted now. But
2465 we can't safely delete it, 'cause finish_command has a handle to
2466 the bp on a bpstat, and will later want to delete it. There's a
2467 chance (and I've seen it happen) that if we delete the bp_finish
2468 here, that its storage will get reused by the time finish_command
2469 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2470 We really must allow finish_command to delete a bp_finish.
2471
e5dd4106 2472 In the absence of a general solution for the "how do we know
53a5351d
JM
2473 it's safe to delete something others may have handles to?"
2474 problem, what we'll do here is just uninsert the bp_finish, and
2475 let finish_command delete it.
2476
2477 (We know the bp_finish is "doomed" in the sense that it's
2478 momentary, and will be deleted as soon as finish_command sees
2479 the inferior stopped. So it doesn't matter that the bp's
2480 address is probably bogus in the new a.out, unlike e.g., the
2481 solib breakpoints.) */
c5aa993b 2482
c5aa993b
JM
2483 if (b->type == bp_finish)
2484 {
2485 continue;
2486 }
2487
2488 /* Without a symbolic address, we have little hope of the
2489 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2490 a.out. */
c5aa993b
JM
2491 if (b->addr_string == NULL)
2492 {
2493 delete_breakpoint (b);
2494 continue;
2495 }
c5aa993b 2496 }
1900040c 2497 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2498 create_overlay_event_breakpoint ();
2499 create_longjmp_master_breakpoint ();
2500 create_std_terminate_master_breakpoint ();
186c406b 2501 create_exception_master_breakpoint ();
c906108c
SS
2502}
2503
2504int
fba45db2 2505detach_breakpoints (int pid)
c906108c 2506{
35df4500 2507 struct bp_location *bl, **blp_tmp;
3a1bae8e 2508 int val = 0;
ce696e05 2509 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2510 struct inferior *inf = current_inferior ();
c5aa993b 2511
39f77062 2512 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2513 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2514
6c95b8df 2515 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2516 inferior_ptid = pid_to_ptid (pid);
35df4500 2517 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2518 {
35df4500 2519 if (bl->pspace != inf->pspace)
6c95b8df
PA
2520 continue;
2521
35df4500
TJB
2522 if (bl->inserted)
2523 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2524 }
d03285ec
UW
2525
2526 /* Detach single-step breakpoints as well. */
2527 detach_single_step_breakpoints ();
2528
ce696e05 2529 do_cleanups (old_chain);
3a1bae8e 2530 return val;
c906108c
SS
2531}
2532
35df4500 2533/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2534 Note that this is used to detach breakpoints from a child fork.
2535 When we get here, the child isn't in the inferior list, and neither
2536 do we have objects to represent its address space --- we should
35df4500 2537 *not* look at bl->pspace->aspace here. */
6c95b8df 2538
c906108c 2539static int
35df4500 2540remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2541{
2542 int val;
c5aa993b 2543
35df4500
TJB
2544 /* BL is never in moribund_locations by our callers. */
2545 gdb_assert (bl->owner != NULL);
2bdf28a0 2546
35df4500 2547 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2548 /* Permanent breakpoints cannot be inserted or removed. */
2549 return 0;
2550
74960c60
VP
2551 /* The type of none suggests that owner is actually deleted.
2552 This should not ever happen. */
35df4500 2553 gdb_assert (bl->owner->type != bp_none);
0bde7532 2554
35df4500
TJB
2555 if (bl->loc_type == bp_loc_software_breakpoint
2556 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2557 {
c02f5703
MS
2558 /* "Normal" instruction breakpoint: either the standard
2559 trap-instruction bp (bp_breakpoint), or a
2560 bp_hardware_breakpoint. */
2561
2562 /* First check to see if we have to handle an overlay. */
2563 if (overlay_debugging == ovly_off
35df4500
TJB
2564 || bl->section == NULL
2565 || !(section_is_overlay (bl->section)))
c02f5703
MS
2566 {
2567 /* No overlay handling: just remove the breakpoint. */
348d480f 2568 val = bl->owner->ops->remove_location (bl);
c02f5703 2569 }
c906108c
SS
2570 else
2571 {
4a64f543 2572 /* This breakpoint is in an overlay section.
c02f5703
MS
2573 Did we set a breakpoint at the LMA? */
2574 if (!overlay_events_enabled)
2575 {
2576 /* Yes -- overlay event support is not active, so we
2577 should have set a breakpoint at the LMA. Remove it.
2578 */
c02f5703
MS
2579 /* Ignore any failures: if the LMA is in ROM, we will
2580 have already warned when we failed to insert it. */
35df4500
TJB
2581 if (bl->loc_type == bp_loc_hardware_breakpoint)
2582 target_remove_hw_breakpoint (bl->gdbarch,
2583 &bl->overlay_target_info);
c02f5703 2584 else
35df4500
TJB
2585 target_remove_breakpoint (bl->gdbarch,
2586 &bl->overlay_target_info);
c02f5703
MS
2587 }
2588 /* Did we set a breakpoint at the VMA?
2589 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2590 if (bl->inserted)
c906108c 2591 {
c02f5703
MS
2592 /* Yes -- remove it. Previously we did not bother to
2593 remove the breakpoint if the section had been
2594 unmapped, but let's not rely on that being safe. We
2595 don't know what the overlay manager might do. */
aa67235e
UW
2596
2597 /* However, we should remove *software* breakpoints only
2598 if the section is still mapped, or else we overwrite
2599 wrong code with the saved shadow contents. */
348d480f
PA
2600 if (bl->loc_type == bp_loc_hardware_breakpoint
2601 || section_is_mapped (bl->section))
2602 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
2603 else
2604 val = 0;
c906108c 2605 }
c02f5703
MS
2606 else
2607 {
2608 /* No -- not inserted, so no need to remove. No error. */
2609 val = 0;
2610 }
c906108c 2611 }
879d1e6b
UW
2612
2613 /* In some cases, we might not be able to remove a breakpoint
2614 in a shared library that has already been removed, but we
2615 have not yet processed the shlib unload event. */
35df4500 2616 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2617 val = 0;
2618
c906108c
SS
2619 if (val)
2620 return val;
35df4500 2621 bl->inserted = (is == mark_inserted);
c906108c 2622 }
35df4500 2623 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2624 {
77b06cd7
TJB
2625 gdb_assert (bl->owner->ops != NULL
2626 && bl->owner->ops->remove_location != NULL);
2627
35df4500 2628 bl->inserted = (is == mark_inserted);
77b06cd7 2629 bl->owner->ops->remove_location (bl);
2e70b7b9 2630
c906108c 2631 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2632 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2633 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2634 bl->owner->number);
c906108c 2635 }
35df4500
TJB
2636 else if (bl->owner->type == bp_catchpoint
2637 && breakpoint_enabled (bl->owner)
2638 && !bl->duplicate)
ce78b96d 2639 {
77b06cd7
TJB
2640 gdb_assert (bl->owner->ops != NULL
2641 && bl->owner->ops->remove_location != NULL);
ce78b96d 2642
77b06cd7 2643 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2644 if (val)
2645 return val;
77b06cd7 2646
35df4500 2647 bl->inserted = (is == mark_inserted);
ce78b96d 2648 }
c906108c
SS
2649
2650 return 0;
2651}
2652
6c95b8df 2653static int
35df4500 2654remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2655{
2656 int ret;
2657 struct cleanup *old_chain;
2658
35df4500
TJB
2659 /* BL is never in moribund_locations by our callers. */
2660 gdb_assert (bl->owner != NULL);
2bdf28a0 2661
35df4500 2662 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2663 /* Permanent breakpoints cannot be inserted or removed. */
2664 return 0;
2665
2666 /* The type of none suggests that owner is actually deleted.
2667 This should not ever happen. */
35df4500 2668 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2669
2670 old_chain = save_current_space_and_thread ();
2671
35df4500 2672 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2673
35df4500 2674 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2675
2676 do_cleanups (old_chain);
2677 return ret;
2678}
2679
c906108c
SS
2680/* Clear the "inserted" flag in all breakpoints. */
2681
25b22b0a 2682void
fba45db2 2683mark_breakpoints_out (void)
c906108c 2684{
35df4500 2685 struct bp_location *bl, **blp_tmp;
c906108c 2686
35df4500
TJB
2687 ALL_BP_LOCATIONS (bl, blp_tmp)
2688 if (bl->pspace == current_program_space)
2689 bl->inserted = 0;
c906108c
SS
2690}
2691
53a5351d
JM
2692/* Clear the "inserted" flag in all breakpoints and delete any
2693 breakpoints which should go away between runs of the program.
c906108c
SS
2694
2695 Plus other such housekeeping that has to be done for breakpoints
2696 between runs.
2697
53a5351d
JM
2698 Note: this function gets called at the end of a run (by
2699 generic_mourn_inferior) and when a run begins (by
4a64f543 2700 init_wait_for_inferior). */
c906108c
SS
2701
2702
2703
2704void
fba45db2 2705breakpoint_init_inferior (enum inf_context context)
c906108c 2706{
35df4500
TJB
2707 struct breakpoint *b, *b_tmp;
2708 struct bp_location *bl, **blp_tmp;
1c5cfe86 2709 int ix;
6c95b8df 2710 struct program_space *pspace = current_program_space;
c906108c 2711
50c71eaf
PA
2712 /* If breakpoint locations are shared across processes, then there's
2713 nothing to do. */
2567c7d9 2714 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2715 return;
2716
35df4500 2717 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2718 {
35df4500
TJB
2719 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2720 if (bl->pspace == pspace
2721 && bl->owner->enable_state != bp_permanent)
2722 bl->inserted = 0;
6c95b8df 2723 }
075f6582 2724
35df4500 2725 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2726 {
6c95b8df
PA
2727 if (b->loc && b->loc->pspace != pspace)
2728 continue;
2729
c5aa993b
JM
2730 switch (b->type)
2731 {
2732 case bp_call_dummy:
c906108c 2733
c5aa993b 2734 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2735 cause problems when the inferior is rerun, so we better get
2736 rid of it. */
2737
2738 case bp_watchpoint_scope:
2739
2740 /* Also get rid of scope breakpoints. */
2741
2742 case bp_shlib_event:
2743
2744 /* Also remove solib event breakpoints. Their addresses may
2745 have changed since the last time we ran the program.
2746 Actually we may now be debugging against different target;
2747 and so the solib backend that installed this breakpoint may
2748 not be used in by the target. E.g.,
2749
2750 (gdb) file prog-linux
2751 (gdb) run # native linux target
2752 ...
2753 (gdb) kill
2754 (gdb) file prog-win.exe
2755 (gdb) tar rem :9999 # remote Windows gdbserver.
2756 */
c906108c 2757
c5aa993b
JM
2758 delete_breakpoint (b);
2759 break;
c906108c 2760
c5aa993b
JM
2761 case bp_watchpoint:
2762 case bp_hardware_watchpoint:
2763 case bp_read_watchpoint:
2764 case bp_access_watchpoint:
c906108c 2765
c5aa993b
JM
2766 /* Likewise for watchpoints on local expressions. */
2767 if (b->exp_valid_block != NULL)
2768 delete_breakpoint (b);
967af18d 2769 else if (context == inf_starting)
c860120c 2770 {
4a64f543
MS
2771 /* Reset val field to force reread of starting value in
2772 insert_breakpoints. */
c860120c
PM
2773 if (b->val)
2774 value_free (b->val);
2775 b->val = NULL;
fa4727a6 2776 b->val_valid = 0;
c860120c 2777 }
c5aa993b
JM
2778 break;
2779 default:
c5aa993b
JM
2780 break;
2781 }
2782 }
1c5cfe86
PA
2783
2784 /* Get rid of the moribund locations. */
35df4500
TJB
2785 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2786 decref_bp_location (&bl);
1c5cfe86 2787 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2788}
2789
6c95b8df
PA
2790/* These functions concern about actual breakpoints inserted in the
2791 target --- to e.g. check if we need to do decr_pc adjustment or if
2792 we need to hop over the bkpt --- so we check for address space
2793 match, not program space. */
2794
c2c6d25f
JM
2795/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2796 exists at PC. It returns ordinary_breakpoint_here if it's an
2797 ordinary breakpoint, or permanent_breakpoint_here if it's a
2798 permanent breakpoint.
2799 - When continuing from a location with an ordinary breakpoint, we
2800 actually single step once before calling insert_breakpoints.
e5dd4106 2801 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
2802 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2803 the target, to advance the PC past the breakpoint. */
c906108c 2804
c2c6d25f 2805enum breakpoint_here
6c95b8df 2806breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2807{
35df4500 2808 struct bp_location *bl, **blp_tmp;
c2c6d25f 2809 int any_breakpoint_here = 0;
c906108c 2810
35df4500 2811 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2812 {
35df4500
TJB
2813 if (bl->loc_type != bp_loc_software_breakpoint
2814 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2815 continue;
2816
f1310107 2817 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2818 if ((breakpoint_enabled (bl->owner)
2819 || bl->owner->enable_state == bp_permanent)
f1310107 2820 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2821 {
2822 if (overlay_debugging
35df4500
TJB
2823 && section_is_overlay (bl->section)
2824 && !section_is_mapped (bl->section))
075f6582 2825 continue; /* unmapped overlay -- can't be a match */
35df4500 2826 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2827 return permanent_breakpoint_here;
2828 else
2829 any_breakpoint_here = 1;
2830 }
2831 }
c906108c 2832
c2c6d25f 2833 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2834}
2835
1c5cfe86
PA
2836/* Return true if there's a moribund breakpoint at PC. */
2837
2838int
6c95b8df 2839moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2840{
2841 struct bp_location *loc;
2842 int ix;
2843
2844 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2845 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2846 return 1;
2847
2848 return 0;
2849}
c2c6d25f 2850
c36b740a 2851/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2852 inserted using regular breakpoint_chain / bp_location array
2853 mechanism. This does not check for single-step breakpoints, which
2854 are inserted and removed using direct target manipulation. */
c906108c
SS
2855
2856int
4a64f543
MS
2857regular_breakpoint_inserted_here_p (struct address_space *aspace,
2858 CORE_ADDR pc)
c906108c 2859{
35df4500 2860 struct bp_location *bl, **blp_tmp;
c906108c 2861
35df4500 2862 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2863 {
35df4500
TJB
2864 if (bl->loc_type != bp_loc_software_breakpoint
2865 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2866 continue;
2867
35df4500 2868 if (bl->inserted
f1310107 2869 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2870 {
2871 if (overlay_debugging
35df4500
TJB
2872 && section_is_overlay (bl->section)
2873 && !section_is_mapped (bl->section))
075f6582
DJ
2874 continue; /* unmapped overlay -- can't be a match */
2875 else
2876 return 1;
2877 }
c5aa993b 2878 }
c36b740a
VP
2879 return 0;
2880}
2881
2882/* Returns non-zero iff there's either regular breakpoint
2883 or a single step breakpoint inserted at PC. */
2884
2885int
6c95b8df 2886breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2887{
6c95b8df 2888 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2889 return 1;
c906108c 2890
6c95b8df 2891 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2892 return 1;
2893
c906108c
SS
2894 return 0;
2895}
2896
4fa8626c
DJ
2897/* This function returns non-zero iff there is a software breakpoint
2898 inserted at PC. */
2899
2900int
3e43a32a
MS
2901software_breakpoint_inserted_here_p (struct address_space *aspace,
2902 CORE_ADDR pc)
4fa8626c 2903{
35df4500 2904 struct bp_location *bl, **blp_tmp;
4fa8626c 2905
35df4500 2906 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2907 {
35df4500 2908 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2909 continue;
2910
35df4500
TJB
2911 if (bl->inserted
2912 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2913 aspace, pc))
4fa8626c
DJ
2914 {
2915 if (overlay_debugging
35df4500
TJB
2916 && section_is_overlay (bl->section)
2917 && !section_is_mapped (bl->section))
4fa8626c
DJ
2918 continue; /* unmapped overlay -- can't be a match */
2919 else
2920 return 1;
2921 }
2922 }
2923
1aafd4da 2924 /* Also check for software single-step breakpoints. */
6c95b8df 2925 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2926 return 1;
2927
4fa8626c
DJ
2928 return 0;
2929}
2930
9093389c
PA
2931int
2932hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2933 CORE_ADDR addr, ULONGEST len)
2934{
2935 struct breakpoint *bpt;
2936
2937 ALL_BREAKPOINTS (bpt)
2938 {
2939 struct bp_location *loc;
2940
2941 if (bpt->type != bp_hardware_watchpoint
2942 && bpt->type != bp_access_watchpoint)
2943 continue;
2944
2945 if (!breakpoint_enabled (bpt))
2946 continue;
2947
2948 for (loc = bpt->loc; loc; loc = loc->next)
2949 if (loc->pspace->aspace == aspace && loc->inserted)
2950 {
2951 CORE_ADDR l, h;
2952
2953 /* Check for intersection. */
2954 l = max (loc->address, addr);
2955 h = min (loc->address + loc->length, addr + len);
2956 if (l < h)
2957 return 1;
2958 }
2959 }
2960 return 0;
2961}
2962
075f6582
DJ
2963/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2964 PC is valid for process/thread PTID. */
c906108c
SS
2965
2966int
6c95b8df
PA
2967breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2968 ptid_t ptid)
c906108c 2969{
35df4500 2970 struct bp_location *bl, **blp_tmp;
4a306c9a 2971 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 2972 int thread = -1;
4a306c9a 2973 int task = 0;
a6f1cd96 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
TJB
2981 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2982 if (!breakpoint_enabled (bl->owner)
2983 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
2984 continue;
2985
f1310107 2986 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
2987 continue;
2988
35df4500 2989 if (bl->owner->thread != -1)
075f6582 2990 {
a6f1cd96
JB
2991 /* This is a thread-specific breakpoint. Check that ptid
2992 matches that thread. If thread hasn't been computed yet,
2993 it is now time to do so. */
2994 if (thread == -1)
2995 thread = pid_to_thread_id (ptid);
35df4500 2996 if (bl->owner->thread != thread)
a6f1cd96 2997 continue;
075f6582 2998 }
a6f1cd96 2999
35df4500 3000 if (bl->owner->task != 0)
4a306c9a
JB
3001 {
3002 /* This is a task-specific breakpoint. Check that ptid
3003 matches that task. If task hasn't been computed yet,
3004 it is now time to do so. */
3005 if (task == 0)
3006 task = ada_get_task_number (ptid);
35df4500 3007 if (bl->owner->task != task)
4a306c9a
JB
3008 continue;
3009 }
3010
a6f1cd96 3011 if (overlay_debugging
35df4500
TJB
3012 && section_is_overlay (bl->section)
3013 && !section_is_mapped (bl->section))
a6f1cd96
JB
3014 continue; /* unmapped overlay -- can't be a match */
3015
3016 return 1;
c5aa993b 3017 }
c906108c
SS
3018
3019 return 0;
3020}
c906108c 3021\f
c5aa993b 3022
c906108c
SS
3023/* bpstat stuff. External routines' interfaces are documented
3024 in breakpoint.h. */
3025
3026int
fba45db2 3027ep_is_catchpoint (struct breakpoint *ep)
c906108c 3028{
533be4dd 3029 return (ep->type == bp_catchpoint);
c906108c
SS
3030}
3031
f431efe5
PA
3032/* Frees any storage that is part of a bpstat. Does not walk the
3033 'next' chain. */
3034
3035static void
198757a8
VP
3036bpstat_free (bpstat bs)
3037{
3038 if (bs->old_val != NULL)
3039 value_free (bs->old_val);
9add0f1b 3040 decref_counted_command_line (&bs->commands);
f431efe5 3041 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3042 xfree (bs);
3043}
3044
c906108c
SS
3045/* Clear a bpstat so that it says we are not at any breakpoint.
3046 Also free any storage that is part of a bpstat. */
3047
3048void
fba45db2 3049bpstat_clear (bpstat *bsp)
c906108c
SS
3050{
3051 bpstat p;
3052 bpstat q;
3053
3054 if (bsp == 0)
3055 return;
3056 p = *bsp;
3057 while (p != NULL)
3058 {
3059 q = p->next;
198757a8 3060 bpstat_free (p);
c906108c
SS
3061 p = q;
3062 }
3063 *bsp = NULL;
3064}
3065
3066/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3067 is part of the bpstat is copied as well. */
3068
3069bpstat
fba45db2 3070bpstat_copy (bpstat bs)
c906108c
SS
3071{
3072 bpstat p = NULL;
3073 bpstat tmp;
3074 bpstat retval = NULL;
3075
3076 if (bs == NULL)
3077 return bs;
3078
3079 for (; bs != NULL; bs = bs->next)
3080 {
3081 tmp = (bpstat) xmalloc (sizeof (*tmp));
3082 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3083 incref_counted_command_line (tmp->commands);
f431efe5 3084 incref_bp_location (tmp->bp_location_at);
31cc81e9 3085 if (bs->old_val != NULL)
3c3185ac
JK
3086 {
3087 tmp->old_val = value_copy (bs->old_val);
3088 release_value (tmp->old_val);
3089 }
31cc81e9 3090
c906108c
SS
3091 if (p == NULL)
3092 /* This is the first thing in the chain. */
3093 retval = tmp;
3094 else
3095 p->next = tmp;
3096 p = tmp;
3097 }
3098 p->next = NULL;
3099 return retval;
3100}
3101
4a64f543 3102/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3103
3104bpstat
fba45db2 3105bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3106{
c5aa993b
JM
3107 if (bsp == NULL)
3108 return NULL;
c906108c 3109
c5aa993b
JM
3110 for (; bsp != NULL; bsp = bsp->next)
3111 {
f431efe5 3112 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3113 return bsp;
3114 }
c906108c
SS
3115 return NULL;
3116}
3117
4a64f543
MS
3118/* Put in *NUM the breakpoint number of the first breakpoint we are
3119 stopped at. *BSP upon return is a bpstat which points to the
3120 remaining breakpoints stopped at (but which is not guaranteed to be
3121 good for anything but further calls to bpstat_num).
3122
8671a17b
PA
3123 Return 0 if passed a bpstat which does not indicate any breakpoints.
3124 Return -1 if stopped at a breakpoint that has been deleted since
3125 we set it.
3126 Return 1 otherwise. */
c906108c
SS
3127
3128int
8671a17b 3129bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3130{
3131 struct breakpoint *b;
3132
3133 if ((*bsp) == NULL)
3134 return 0; /* No more breakpoint values */
8671a17b 3135
4a64f543
MS
3136 /* We assume we'll never have several bpstats that correspond to a
3137 single breakpoint -- otherwise, this function might return the
3138 same number more than once and this will look ugly. */
f431efe5 3139 b = (*bsp)->breakpoint_at;
8671a17b
PA
3140 *bsp = (*bsp)->next;
3141 if (b == NULL)
3142 return -1; /* breakpoint that's been deleted since */
3143
3144 *num = b->number; /* We have its number */
3145 return 1;
c906108c
SS
3146}
3147
3148/* Modify BS so that the actions will not be performed. */
3149
3150void
fba45db2 3151bpstat_clear_actions (bpstat bs)
c906108c
SS
3152{
3153 for (; bs != NULL; bs = bs->next)
3154 {
9add0f1b 3155 decref_counted_command_line (&bs->commands);
dde2d684 3156 bs->commands_left = NULL;
c906108c
SS
3157 if (bs->old_val != NULL)
3158 {
3159 value_free (bs->old_val);
3160 bs->old_val = NULL;
3161 }
3162 }
3163}
3164
f3b1572e
PA
3165/* Called when a command is about to proceed the inferior. */
3166
3167static void
3168breakpoint_about_to_proceed (void)
3169{
3170 if (!ptid_equal (inferior_ptid, null_ptid))
3171 {
3172 struct thread_info *tp = inferior_thread ();
3173
3174 /* Allow inferior function calls in breakpoint commands to not
3175 interrupt the command list. When the call finishes
3176 successfully, the inferior will be standing at the same
3177 breakpoint as if nothing happened. */
16c381f0 3178 if (tp->control.in_infcall)
f3b1572e
PA
3179 return;
3180 }
3181
3182 breakpoint_proceeded = 1;
3183}
3184
4a64f543
MS
3185/* Stub for cleaning up our state if we error-out of a breakpoint
3186 command. */
c906108c 3187static void
4efb68b1 3188cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3189{
3190 executing_breakpoint_commands = 0;
3191}
3192
4a64f543
MS
3193/* Execute all the commands associated with all the breakpoints at
3194 this location. Any of these commands could cause the process to
3195 proceed beyond this point, etc. We look out for such changes by
3196 checking the global "breakpoint_proceeded" after each command.
c906108c 3197
347bddb7
PA
3198 Returns true if a breakpoint command resumed the inferior. In that
3199 case, it is the caller's responsibility to recall it again with the
3200 bpstat of the current thread. */
3201
3202static int
3203bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3204{
3205 bpstat bs;
3206 struct cleanup *old_chain;
347bddb7 3207 int again = 0;
c906108c
SS
3208
3209 /* Avoid endless recursion if a `source' command is contained
3210 in bs->commands. */
3211 if (executing_breakpoint_commands)
347bddb7 3212 return 0;
c906108c
SS
3213
3214 executing_breakpoint_commands = 1;
3215 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3216
cf6c5ffb
TT
3217 prevent_dont_repeat ();
3218
4a64f543 3219 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3220 bs = *bsp;
3221
3222 breakpoint_proceeded = 0;
3223 for (; bs != NULL; bs = bs->next)
3224 {
9add0f1b 3225 struct counted_command_line *ccmd;
6c50ab1c
JB
3226 struct command_line *cmd;
3227 struct cleanup *this_cmd_tree_chain;
3228
3229 /* Take ownership of the BSP's command tree, if it has one.
3230
3231 The command tree could legitimately contain commands like
3232 'step' and 'next', which call clear_proceed_status, which
3233 frees stop_bpstat's command tree. To make sure this doesn't
3234 free the tree we're executing out from under us, we need to
3235 take ownership of the tree ourselves. Since a given bpstat's
3236 commands are only executed once, we don't need to copy it; we
3237 can clear the pointer in the bpstat, and make sure we free
3238 the tree when we're done. */
9add0f1b
TT
3239 ccmd = bs->commands;
3240 bs->commands = NULL;
3241 this_cmd_tree_chain
3242 = make_cleanup_decref_counted_command_line (&ccmd);
3243 cmd = bs->commands_left;
3244 bs->commands_left = NULL;
6c50ab1c 3245
c906108c
SS
3246 while (cmd != NULL)
3247 {
3248 execute_control_command (cmd);
3249
3250 if (breakpoint_proceeded)
3251 break;
3252 else
3253 cmd = cmd->next;
3254 }
6c50ab1c
JB
3255
3256 /* We can free this command tree now. */
3257 do_cleanups (this_cmd_tree_chain);
3258
c906108c 3259 if (breakpoint_proceeded)
32c1e744
VP
3260 {
3261 if (target_can_async_p ())
347bddb7
PA
3262 /* If we are in async mode, then the target might be still
3263 running, not stopped at any breakpoint, so nothing for
3264 us to do here -- just return to the event loop. */
3265 ;
32c1e744
VP
3266 else
3267 /* In sync mode, when execute_control_command returns
3268 we're already standing on the next breakpoint.
347bddb7
PA
3269 Breakpoint commands for that stop were not run, since
3270 execute_command does not run breakpoint commands --
3271 only command_line_handler does, but that one is not
3272 involved in execution of breakpoint commands. So, we
3273 can now execute breakpoint commands. It should be
3274 noted that making execute_command do bpstat actions is
3275 not an option -- in this case we'll have recursive
3276 invocation of bpstat for each breakpoint with a
3277 command, and can easily blow up GDB stack. Instead, we
3278 return true, which will trigger the caller to recall us
3279 with the new stop_bpstat. */
3280 again = 1;
3281 break;
32c1e744 3282 }
c906108c 3283 }
c2b8ed2c 3284 do_cleanups (old_chain);
347bddb7
PA
3285 return again;
3286}
3287
3288void
3289bpstat_do_actions (void)
3290{
3291 /* Do any commands attached to breakpoint we are stopped at. */
3292 while (!ptid_equal (inferior_ptid, null_ptid)
3293 && target_has_execution
3294 && !is_exited (inferior_ptid)
3295 && !is_executing (inferior_ptid))
3296 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3297 and only return when it is stopped at the next breakpoint, we
3298 keep doing breakpoint actions until it returns false to
3299 indicate the inferior was not resumed. */
16c381f0 3300 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3301 break;
c906108c
SS
3302}
3303
fa4727a6
DJ
3304/* Print out the (old or new) value associated with a watchpoint. */
3305
3306static void
3307watchpoint_value_print (struct value *val, struct ui_file *stream)
3308{
3309 if (val == NULL)
3310 fprintf_unfiltered (stream, _("<unreadable>"));
3311 else
79a45b7d
TT
3312 {
3313 struct value_print_options opts;
3314 get_user_print_options (&opts);
3315 value_print (val, stream, &opts);
3316 }
fa4727a6
DJ
3317}
3318
e514a9d6 3319/* Generic routine for printing messages indicating why we
4a64f543 3320 stopped. The behavior of this function depends on the value
e514a9d6
JM
3321 'print_it' in the bpstat structure. Under some circumstances we
3322 may decide not to print anything here and delegate the task to
4a64f543 3323 normal_stop(). */
e514a9d6
JM
3324
3325static enum print_stop_action
3326print_bp_stop_message (bpstat bs)
3327{
3328 switch (bs->print_it)
3329 {
3330 case print_it_noop:
4a64f543 3331 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3332 return PRINT_UNKNOWN;
3333 break;
3334
3335 case print_it_done:
3336 /* We still want to print the frame, but we already printed the
4a64f543 3337 relevant messages. */
e514a9d6
JM
3338 return PRINT_SRC_AND_LOC;
3339 break;
3340
3341 case print_it_normal:
4f8d1dc6 3342 {
f431efe5
PA
3343 struct breakpoint *b = bs->breakpoint_at;
3344
1a6a67de
TJB
3345 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3346 which has since been deleted. */
3347 if (b == NULL)
3348 return PRINT_UNKNOWN;
3349
348d480f
PA
3350 /* Normal case. Call the breakpoint's print_it method. */
3351 return b->ops->print_it (bs);
4f8d1dc6 3352 }
348d480f 3353 break;
3086aeae 3354
e514a9d6 3355 default:
8e65ff28 3356 internal_error (__FILE__, __LINE__,
e2e0b3e5 3357 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3358 break;
c906108c 3359 }
c906108c
SS
3360}
3361
e514a9d6
JM
3362/* Print a message indicating what happened. This is called from
3363 normal_stop(). The input to this routine is the head of the bpstat
3364 list - a list of the eventpoints that caused this stop. This
3365 routine calls the generic print routine for printing a message
3366 about reasons for stopping. This will print (for example) the
3367 "Breakpoint n," part of the output. The return value of this
3368 routine is one of:
c906108c 3369
4a64f543 3370 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3371 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3372 code to print the location. An example is
c5aa993b
JM
3373 "Breakpoint 1, " which should be followed by
3374 the location.
917317f4 3375 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3376 to also print the location part of the message.
3377 An example is the catch/throw messages, which
4a64f543 3378 don't require a location appended to the end.
917317f4 3379 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3380 further info to be printed. */
c906108c 3381
917317f4 3382enum print_stop_action
fba45db2 3383bpstat_print (bpstat bs)
c906108c
SS
3384{
3385 int val;
c5aa993b 3386
c906108c 3387 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3388 (Currently all watchpoints go on the bpstat whether hit or not.
3389 That probably could (should) be changed, provided care is taken
c906108c 3390 with respect to bpstat_explains_signal). */
e514a9d6
JM
3391 for (; bs; bs = bs->next)
3392 {
3393 val = print_bp_stop_message (bs);
3394 if (val == PRINT_SRC_ONLY
3395 || val == PRINT_SRC_AND_LOC
3396 || val == PRINT_NOTHING)
3397 return val;
3398 }
c906108c 3399
e514a9d6 3400 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3401 with and nothing was printed. */
917317f4 3402 return PRINT_UNKNOWN;
c906108c
SS
3403}
3404
4a64f543
MS
3405/* Evaluate the expression EXP and return 1 if value is zero. This is
3406 used inside a catch_errors to evaluate the breakpoint condition.
3407 The argument is a "struct expression *" that has been cast to a
3408 "char *" to make it pass through catch_errors. */
c906108c
SS
3409
3410static int
4efb68b1 3411breakpoint_cond_eval (void *exp)
c906108c 3412{
278cd55f 3413 struct value *mark = value_mark ();
c5aa993b 3414 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3415
c906108c
SS
3416 value_free_to_mark (mark);
3417 return i;
3418}
3419
5760d0ab 3420/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3421
3422static bpstat
5760d0ab 3423bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3424{
3425 bpstat bs;
3426
3427 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3428 bs->next = NULL;
3429 **bs_link_pointer = bs;
3430 *bs_link_pointer = &bs->next;
f431efe5
PA
3431 bs->breakpoint_at = bl->owner;
3432 bs->bp_location_at = bl;
3433 incref_bp_location (bl);
c906108c
SS
3434 /* If the condition is false, etc., don't do the commands. */
3435 bs->commands = NULL;
9add0f1b 3436 bs->commands_left = NULL;
c906108c
SS
3437 bs->old_val = NULL;
3438 bs->print_it = print_it_normal;
3439 return bs;
3440}
3441\f
d983da9c
DJ
3442/* The target has stopped with waitstatus WS. Check if any hardware
3443 watchpoints have triggered, according to the target. */
3444
3445int
3446watchpoints_triggered (struct target_waitstatus *ws)
3447{
d92524f1 3448 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3449 CORE_ADDR addr;
3450 struct breakpoint *b;
3451
3452 if (!stopped_by_watchpoint)
3453 {
3454 /* We were not stopped by a watchpoint. Mark all watchpoints
3455 as not triggered. */
3456 ALL_BREAKPOINTS (b)
cc60f2e3 3457 if (is_hardware_watchpoint (b))
d983da9c
DJ
3458 b->watchpoint_triggered = watch_triggered_no;
3459
3460 return 0;
3461 }
3462
3463 if (!target_stopped_data_address (&current_target, &addr))
3464 {
3465 /* We were stopped by a watchpoint, but we don't know where.
3466 Mark all watchpoints as unknown. */
3467 ALL_BREAKPOINTS (b)
cc60f2e3 3468 if (is_hardware_watchpoint (b))
d983da9c
DJ
3469 b->watchpoint_triggered = watch_triggered_unknown;
3470
3471 return stopped_by_watchpoint;
3472 }
3473
3474 /* The target could report the data address. Mark watchpoints
3475 affected by this data address as triggered, and all others as not
3476 triggered. */
3477
3478 ALL_BREAKPOINTS (b)
cc60f2e3 3479 if (is_hardware_watchpoint (b))
d983da9c 3480 {
a5606eee 3481 struct bp_location *loc;
d983da9c
DJ
3482
3483 b->watchpoint_triggered = watch_triggered_no;
a5606eee 3484 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4
TJB
3485 {
3486 if (is_masked_watchpoint (loc->owner))
3487 {
3488 CORE_ADDR newaddr = addr & loc->owner->hw_wp_mask;
3489 CORE_ADDR start = loc->address & loc->owner->hw_wp_mask;
3490
3491 if (newaddr == start)
3492 {
3493 b->watchpoint_triggered = watch_triggered_yes;
3494 break;
3495 }
3496 }
3497 /* Exact match not required. Within range is sufficient. */
3498 else if (target_watchpoint_addr_within_range (&current_target,
3499 addr, loc->address,
3500 loc->length))
3501 {
3502 b->watchpoint_triggered = watch_triggered_yes;
3503 break;
3504 }
3505 }
d983da9c
DJ
3506 }
3507
3508 return 1;
3509}
3510
c906108c
SS
3511/* Possible return values for watchpoint_check (this can't be an enum
3512 because of check_errors). */
3513/* The watchpoint has been deleted. */
3514#define WP_DELETED 1
3515/* The value has changed. */
3516#define WP_VALUE_CHANGED 2
3517/* The value has not changed. */
3518#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3519/* Ignore this watchpoint, no matter if the value changed or not. */
3520#define WP_IGNORE 4
c906108c
SS
3521
3522#define BP_TEMPFLAG 1
3523#define BP_HARDWAREFLAG 2
3524
4a64f543
MS
3525/* Evaluate watchpoint condition expression and check if its value
3526 changed.
553e4c11
JB
3527
3528 P should be a pointer to struct bpstat, but is defined as a void *
3529 in order for this function to be usable with catch_errors. */
c906108c
SS
3530
3531static int
4efb68b1 3532watchpoint_check (void *p)
c906108c
SS
3533{
3534 bpstat bs = (bpstat) p;
3535 struct breakpoint *b;
3536 struct frame_info *fr;
3537 int within_current_scope;
3538
f431efe5 3539 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3540 gdb_assert (bs->breakpoint_at != NULL);
f431efe5 3541 b = bs->breakpoint_at;
c906108c 3542
d0fb5eae
JK
3543 gdb_assert (is_watchpoint (b));
3544
f6bc2008
PA
3545 /* If this is a local watchpoint, we only want to check if the
3546 watchpoint frame is in scope if the current thread is the thread
3547 that was used to create the watchpoint. */
3548 if (!watchpoint_in_thread_scope (b))
60e1c644 3549 return WP_IGNORE;
f6bc2008 3550
c906108c
SS
3551 if (b->exp_valid_block == NULL)
3552 within_current_scope = 1;
3553 else
3554 {
edb3359d
DJ
3555 struct frame_info *frame = get_current_frame ();
3556 struct gdbarch *frame_arch = get_frame_arch (frame);
3557 CORE_ADDR frame_pc = get_frame_pc (frame);
3558
4a64f543
MS
3559 /* in_function_epilogue_p() returns a non-zero value if we're
3560 still in the function but the stack frame has already been
3561 invalidated. Since we can't rely on the values of local
3562 variables after the stack has been destroyed, we are treating
3563 the watchpoint in that state as `not changed' without further
3564 checking. Don't mark watchpoints as changed if the current
3565 frame is in an epilogue - even if they are in some other
3566 frame, our view of the stack is likely to be wrong and
3567 frame_find_by_id could error out. */
a0f49112 3568 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3569 return WP_IGNORE;
a0f49112 3570
101dcfbe 3571 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3572 within_current_scope = (fr != NULL);
69fbadd5
DJ
3573
3574 /* If we've gotten confused in the unwinder, we might have
3575 returned a frame that can't describe this variable. */
edb3359d
DJ
3576 if (within_current_scope)
3577 {
3578 struct symbol *function;
3579
3580 function = get_frame_function (fr);
3581 if (function == NULL
3582 || !contained_in (b->exp_valid_block,
3583 SYMBOL_BLOCK_VALUE (function)))
3584 within_current_scope = 0;
3585 }
69fbadd5 3586
edb3359d 3587 if (within_current_scope)
c906108c
SS
3588 /* If we end up stopping, the current frame will get selected
3589 in normal_stop. So this call to select_frame won't affect
3590 the user. */
0f7d239c 3591 select_frame (fr);
c906108c 3592 }
c5aa993b 3593
c906108c
SS
3594 if (within_current_scope)
3595 {
4a64f543
MS
3596 /* We use value_{,free_to_}mark because it could be a *long*
3597 time before we return to the command level and call
3598 free_all_values. We can't call free_all_values because we
3599 might be in the middle of evaluating a function call. */
c906108c 3600
0cf6dd15 3601 int pc = 0;
9c06b0b4 3602 struct value *mark;
fa4727a6
DJ
3603 struct value *new_val;
3604
9c06b0b4
TJB
3605 if (is_masked_watchpoint (b))
3606 /* Since we don't know the exact trigger address (from
3607 stopped_data_address), just tell the user we've triggered
3608 a mask watchpoint. */
3609 return WP_VALUE_CHANGED;
3610
3611 mark = value_mark ();
0cf6dd15 3612 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3613
4a64f543
MS
3614 /* We use value_equal_contents instead of value_equal because
3615 the latter coerces an array to a pointer, thus comparing just
3616 the address of the array instead of its contents. This is
3617 not what we want. */
fa4727a6 3618 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3619 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3620 {
fa4727a6
DJ
3621 if (new_val != NULL)
3622 {
3623 release_value (new_val);
3624 value_free_to_mark (mark);
3625 }
c906108c
SS
3626 bs->old_val = b->val;
3627 b->val = new_val;
fa4727a6 3628 b->val_valid = 1;
c906108c
SS
3629 return WP_VALUE_CHANGED;
3630 }
3631 else
3632 {
60e1c644 3633 /* Nothing changed. */
c906108c 3634 value_free_to_mark (mark);
c906108c
SS
3635 return WP_VALUE_NOT_CHANGED;
3636 }
3637 }
3638 else
3639 {
3640 /* This seems like the only logical thing to do because
c5aa993b
JM
3641 if we temporarily ignored the watchpoint, then when
3642 we reenter the block in which it is valid it contains
3643 garbage (in the case of a function, it may have two
3644 garbage values, one before and one after the prologue).
3645 So we can't even detect the first assignment to it and
3646 watch after that (since the garbage may or may not equal
3647 the first value assigned). */
348d480f
PA
3648 /* We print all the stop information in
3649 breakpoint_ops->print_it, but in this case, by the time we
3650 call breakpoint_ops->print_it this bp will be deleted
3651 already. So we have no choice but print the information
3652 here. */
9dc5e2a9 3653 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3654 ui_out_field_string
3655 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3656 ui_out_text (uiout, "\nWatchpoint ");
4f8d1dc6 3657 ui_out_field_int (uiout, "wpnum", b->number);
3e43a32a
MS
3658 ui_out_text (uiout,
3659 " deleted because the program has left the block in\n\
8b93c638 3660which its expression is valid.\n");
4ce44c66 3661
cdac0397
PA
3662 /* Make sure the watchpoint's commands aren't executed. */
3663 decref_counted_command_line (&b->commands);
d0fb5eae 3664 watchpoint_del_at_next_stop (b);
c906108c
SS
3665
3666 return WP_DELETED;
3667 }
3668}
3669
18a18393 3670/* Return true if it looks like target has stopped due to hitting
348d480f
PA
3671 breakpoint location BL. This function does not check if we should
3672 stop, only if BL explains the stop. */
3673
18a18393 3674static int
6c95b8df
PA
3675bpstat_check_location (const struct bp_location *bl,
3676 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3677{
3678 struct breakpoint *b = bl->owner;
3679
348d480f 3680 /* BL is from an existing breakpoint. */
2bdf28a0
JK
3681 gdb_assert (b != NULL);
3682
348d480f 3683 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
18a18393
VP
3684}
3685
3686/* If BS refers to a watchpoint, determine if the watched values
3687 has actually changed, and we should stop. If not, set BS->stop
3688 to 0. */
3689static void
3690bpstat_check_watchpoint (bpstat bs)
3691{
2bdf28a0
JK
3692 const struct bp_location *bl;
3693 struct breakpoint *b;
3694
3695 /* BS is built for existing struct breakpoint. */
f431efe5 3696 bl = bs->bp_location_at;
2bdf28a0 3697 gdb_assert (bl != NULL);
f431efe5 3698 b = bs->breakpoint_at;
2bdf28a0 3699 gdb_assert (b != NULL);
18a18393 3700
cc60f2e3 3701 if (is_watchpoint (b))
18a18393 3702 {
18a18393
VP
3703 int must_check_value = 0;
3704
3705 if (b->type == bp_watchpoint)
3706 /* For a software watchpoint, we must always check the
3707 watched value. */
3708 must_check_value = 1;
3709 else if (b->watchpoint_triggered == watch_triggered_yes)
3710 /* We have a hardware watchpoint (read, write, or access)
3711 and the target earlier reported an address watched by
3712 this watchpoint. */
3713 must_check_value = 1;
3714 else if (b->watchpoint_triggered == watch_triggered_unknown
3715 && b->type == bp_hardware_watchpoint)
3716 /* We were stopped by a hardware watchpoint, but the target could
3717 not report the data address. We must check the watchpoint's
3718 value. Access and read watchpoints are out of luck; without
3719 a data address, we can't figure it out. */
3720 must_check_value = 1;
3721
3722 if (must_check_value)
3723 {
3e43a32a
MS
3724 char *message
3725 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3726 b->number);
18a18393
VP
3727 struct cleanup *cleanups = make_cleanup (xfree, message);
3728 int e = catch_errors (watchpoint_check, bs, message,
3729 RETURN_MASK_ALL);
3730 do_cleanups (cleanups);
3731 switch (e)
3732 {
3733 case WP_DELETED:
3734 /* We've already printed what needs to be printed. */
3735 bs->print_it = print_it_done;
3736 /* Stop. */
3737 break;
60e1c644
PA
3738 case WP_IGNORE:
3739 bs->print_it = print_it_noop;
3740 bs->stop = 0;
3741 break;
18a18393
VP
3742 case WP_VALUE_CHANGED:
3743 if (b->type == bp_read_watchpoint)
3744 {
85d721b8
PA
3745 /* There are two cases to consider here:
3746
4a64f543 3747 1. We're watching the triggered memory for reads.
85d721b8
PA
3748 In that case, trust the target, and always report
3749 the watchpoint hit to the user. Even though
3750 reads don't cause value changes, the value may
3751 have changed since the last time it was read, and
3752 since we're not trapping writes, we will not see
3753 those, and as such we should ignore our notion of
3754 old value.
3755
4a64f543 3756 2. We're watching the triggered memory for both
85d721b8
PA
3757 reads and writes. There are two ways this may
3758 happen:
3759
4a64f543 3760 2.1. This is a target that can't break on data
85d721b8
PA
3761 reads only, but can break on accesses (reads or
3762 writes), such as e.g., x86. We detect this case
3763 at the time we try to insert read watchpoints.
3764
4a64f543 3765 2.2. Otherwise, the target supports read
85d721b8
PA
3766 watchpoints, but, the user set an access or write
3767 watchpoint watching the same memory as this read
3768 watchpoint.
3769
3770 If we're watching memory writes as well as reads,
3771 ignore watchpoint hits when we find that the
3772 value hasn't changed, as reads don't cause
3773 changes. This still gives false positives when
3774 the program writes the same value to memory as
3775 what there was already in memory (we will confuse
3776 it for a read), but it's much better than
3777 nothing. */
3778
3779 int other_write_watchpoint = 0;
3780
3781 if (bl->watchpoint_type == hw_read)
3782 {
3783 struct breakpoint *other_b;
3784
3785 ALL_BREAKPOINTS (other_b)
3786 if ((other_b->type == bp_hardware_watchpoint
3787 || other_b->type == bp_access_watchpoint)
3788 && (other_b->watchpoint_triggered
3789 == watch_triggered_yes))
3790 {
3791 other_write_watchpoint = 1;
3792 break;
3793 }
3794 }
3795
3796 if (other_write_watchpoint
3797 || bl->watchpoint_type == hw_access)
3798 {
3799 /* We're watching the same memory for writes,
3800 and the value changed since the last time we
3801 updated it, so this trap must be for a write.
3802 Ignore it. */
3803 bs->print_it = print_it_noop;
3804 bs->stop = 0;
3805 }
18a18393
VP
3806 }
3807 break;
3808 case WP_VALUE_NOT_CHANGED:
3809 if (b->type == bp_hardware_watchpoint
3810 || b->type == bp_watchpoint)
3811 {
3812 /* Don't stop: write watchpoints shouldn't fire if
3813 the value hasn't changed. */
3814 bs->print_it = print_it_noop;
3815 bs->stop = 0;
3816 }
3817 /* Stop. */
3818 break;
3819 default:
3820 /* Can't happen. */
3821 case 0:
3822 /* Error from catch_errors. */
3823 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
d0fb5eae 3824 watchpoint_del_at_next_stop (b);
18a18393
VP
3825 /* We've already printed what needs to be printed. */
3826 bs->print_it = print_it_done;
3827 break;
3828 }
3829 }
3830 else /* must_check_value == 0 */
3831 {
3832 /* This is a case where some watchpoint(s) triggered, but
3833 not at the address of this watchpoint, or else no
3834 watchpoint triggered after all. So don't print
3835 anything for this watchpoint. */
3836 bs->print_it = print_it_noop;
3837 bs->stop = 0;
3838 }
3839 }
3840}
3841
3842
3843/* Check conditions (condition proper, frame, thread and ignore count)
3844 of breakpoint referred to by BS. If we should not stop for this
3845 breakpoint, set BS->stop to 0. */
f431efe5 3846
18a18393
VP
3847static void
3848bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3849{
3850 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
3851 const struct bp_location *bl;
3852 struct breakpoint *b;
3853
3854 /* BS is built for existing struct breakpoint. */
f431efe5 3855 bl = bs->bp_location_at;
2bdf28a0 3856 gdb_assert (bl != NULL);
f431efe5 3857 b = bs->breakpoint_at;
2bdf28a0 3858 gdb_assert (b != NULL);
18a18393
VP
3859
3860 if (frame_id_p (b->frame_id)
edb3359d 3861 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
3862 bs->stop = 0;
3863 else if (bs->stop)
3864 {
3865 int value_is_zero = 0;
60e1c644
PA
3866 struct expression *cond;
3867
7371cf6d
PM
3868 /* Evaluate Python breakpoints that have a "stop"
3869 method implemented. */
3870 if (b->py_bp_object)
3871 bs->stop = gdbpy_should_stop (b->py_bp_object);
3872
60e1c644
PA
3873 if (is_watchpoint (b))
3874 cond = b->cond_exp;
3875 else
3876 cond = bl->cond;
3877
f431efe5 3878 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 3879 {
60e1c644
PA
3880 int within_current_scope = 1;
3881
c5bc3a77
DJ
3882 /* We use value_mark and value_free_to_mark because it could
3883 be a long time before we return to the command level and
3884 call free_all_values. We can't call free_all_values
3885 because we might be in the middle of evaluating a
3886 function call. */
3887 struct value *mark = value_mark ();
3888
edb3359d
DJ
3889 /* Need to select the frame, with all that implies so that
3890 the conditions will have the right context. Because we
3891 use the frame, we will not see an inlined function's
3892 variables when we arrive at a breakpoint at the start
3893 of the inlined function; the current frame will be the
3894 call site. */
60e1c644
PA
3895 if (!is_watchpoint (b) || b->cond_exp_valid_block == NULL)
3896 select_frame (get_current_frame ());
3897 else
3898 {
3899 struct frame_info *frame;
3900
3901 /* For local watchpoint expressions, which particular
3902 instance of a local is being watched matters, so we
3903 keep track of the frame to evaluate the expression
3904 in. To evaluate the condition however, it doesn't
3905 really matter which instantiation of the function
3906 where the condition makes sense triggers the
3907 watchpoint. This allows an expression like "watch
3908 global if q > 10" set in `func', catch writes to
3909 global on all threads that call `func', or catch
3910 writes on all recursive calls of `func' by a single
3911 thread. We simply always evaluate the condition in
3912 the innermost frame that's executing where it makes
3913 sense to evaluate the condition. It seems
3914 intuitive. */
3915 frame = block_innermost_frame (b->cond_exp_valid_block);
3916 if (frame != NULL)
3917 select_frame (frame);
3918 else
3919 within_current_scope = 0;
3920 }
3921 if (within_current_scope)
3922 value_is_zero
3923 = catch_errors (breakpoint_cond_eval, cond,
3924 "Error in testing breakpoint condition:\n",
3925 RETURN_MASK_ALL);
3926 else
3927 {
3928 warning (_("Watchpoint condition cannot be tested "
3929 "in the current scope"));
3930 /* If we failed to set the right context for this
3931 watchpoint, unconditionally report it. */
3932 value_is_zero = 0;
3933 }
4a64f543 3934 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 3935 value_free_to_mark (mark);
18a18393 3936 }
60e1c644
PA
3937
3938 if (cond && value_is_zero)
18a18393
VP
3939 {
3940 bs->stop = 0;
3941 }
3942 else if (b->thread != -1 && b->thread != thread_id)
3943 {
3944 bs->stop = 0;
3945 }
3946 else if (b->ignore_count > 0)
3947 {
3948 b->ignore_count--;
3949 annotate_ignore_count_change ();
3950 bs->stop = 0;
4a64f543 3951 /* Increase the hit count even though we don't stop. */
18a18393 3952 ++(b->hit_count);
8d3788bd 3953 observer_notify_breakpoint_modified (b);
18a18393
VP
3954 }
3955 }
3956}
3957
3958
9709f61c 3959/* Get a bpstat associated with having just stopped at address
d983da9c 3960 BP_ADDR in thread PTID.
c906108c 3961
d983da9c 3962 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
3963 don't understand this stop. Result is a chain of bpstat's such
3964 that:
c906108c 3965
c5aa993b 3966 if we don't understand the stop, the result is a null pointer.
c906108c 3967
c5aa993b 3968 if we understand why we stopped, the result is not null.
c906108c 3969
c5aa993b
JM
3970 Each element of the chain refers to a particular breakpoint or
3971 watchpoint at which we have stopped. (We may have stopped for
3972 several reasons concurrently.)
c906108c 3973
c5aa993b
JM
3974 Each element of the chain has valid next, breakpoint_at,
3975 commands, FIXME??? fields. */
c906108c
SS
3976
3977bpstat
6c95b8df
PA
3978bpstat_stop_status (struct address_space *aspace,
3979 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 3980{
0d381245 3981 struct breakpoint *b = NULL;
afe38095 3982 struct bp_location *bl;
20874c92 3983 struct bp_location *loc;
5760d0ab
JK
3984 /* First item of allocated bpstat's. */
3985 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 3986 /* Pointer to the last thing in the chain currently. */
5760d0ab 3987 bpstat bs;
20874c92 3988 int ix;
429374b8 3989 int need_remove_insert;
f431efe5 3990 int removed_any;
c906108c 3991
f431efe5
PA
3992 /* First, build the bpstat chain with locations that explain a
3993 target stop, while being careful to not set the target running,
3994 as that may invalidate locations (in particular watchpoint
3995 locations are recreated). Resuming will happen here with
3996 breakpoint conditions or watchpoint expressions that include
3997 inferior function calls. */
c5aa993b 3998
429374b8
JK
3999 ALL_BREAKPOINTS (b)
4000 {
4001 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4002 continue;
a5606eee 4003
429374b8
JK
4004 for (bl = b->loc; bl != NULL; bl = bl->next)
4005 {
4a64f543
MS
4006 /* For hardware watchpoints, we look only at the first
4007 location. The watchpoint_check function will work on the
4008 entire expression, not the individual locations. For
4009 read watchpoints, the watchpoints_triggered function has
4010 checked all locations already. */
429374b8
JK
4011 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4012 break;
18a18393 4013
429374b8
JK
4014 if (bl->shlib_disabled)
4015 continue;
c5aa993b 4016
429374b8
JK
4017 if (!bpstat_check_location (bl, aspace, bp_addr))
4018 continue;
c5aa993b 4019
4a64f543
MS
4020 /* Come here if it's a watchpoint, or if the break address
4021 matches. */
c5aa993b 4022
4a64f543
MS
4023 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4024 explain stop. */
c5aa993b 4025
f431efe5
PA
4026 /* Assume we stop. Should we find a watchpoint that is not
4027 actually triggered, or if the condition of the breakpoint
4028 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4029 bs->stop = 1;
4030 bs->print = 1;
d983da9c 4031
f431efe5
PA
4032 /* If this is a scope breakpoint, mark the associated
4033 watchpoint as triggered so that we will handle the
4034 out-of-scope event. We'll get to the watchpoint next
4035 iteration. */
d0fb5eae 4036 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
f431efe5
PA
4037 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
4038 }
4039 }
4040
4041 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4042 {
f1310107 4043 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4044 {
5760d0ab 4045 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4046 /* For hits of moribund locations, we should just proceed. */
4047 bs->stop = 0;
4048 bs->print = 0;
4049 bs->print_it = print_it_noop;
4050 }
4051 }
4052
f431efe5
PA
4053 /* Now go through the locations that caused the target to stop, and
4054 check whether we're interested in reporting this stop to higher
4055 layers, or whether we should resume the target transparently. */
4056
4057 removed_any = 0;
4058
5760d0ab 4059 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4060 {
4061 if (!bs->stop)
4062 continue;
4063
f431efe5 4064 b = bs->breakpoint_at;
348d480f
PA
4065 b->ops->check_status (bs);
4066 if (bs->stop)
28010a5d 4067 {
348d480f 4068 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4069
429374b8
JK
4070 if (bs->stop)
4071 {
4072 ++(b->hit_count);
8d3788bd 4073 observer_notify_breakpoint_modified (b);
c906108c 4074
4a64f543 4075 /* We will stop here. */
429374b8
JK
4076 if (b->disposition == disp_disable)
4077 {
4078 if (b->enable_state != bp_permanent)
4079 b->enable_state = bp_disabled;
f431efe5 4080 removed_any = 1;
429374b8
JK
4081 }
4082 if (b->silent)
4083 bs->print = 0;
4084 bs->commands = b->commands;
9add0f1b
TT
4085 incref_counted_command_line (bs->commands);
4086 bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4087 if (bs->commands_left
4088 && (strcmp ("silent", bs->commands_left->line) == 0
4089 || (xdb_commands
4090 && strcmp ("Q",
4091 bs->commands_left->line) == 0)))
429374b8 4092 {
9add0f1b 4093 bs->commands_left = bs->commands_left->next;
429374b8
JK
4094 bs->print = 0;
4095 }
429374b8
JK
4096 }
4097
e5dd4106 4098 /* Print nothing for this entry if we don't stop or don't print. */
429374b8
JK
4099 if (bs->stop == 0 || bs->print == 0)
4100 bs->print_it = print_it_noop;
348d480f 4101 }
429374b8 4102 }
876fa593 4103
d983da9c
DJ
4104 /* If we aren't stopping, the value of some hardware watchpoint may
4105 not have changed, but the intermediate memory locations we are
4106 watching may have. Don't bother if we're stopping; this will get
4107 done later. */
d832cb68 4108 need_remove_insert = 0;
5760d0ab
JK
4109 if (! bpstat_causes_stop (bs_head))
4110 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4111 if (!bs->stop
f431efe5
PA
4112 && bs->breakpoint_at
4113 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4114 {
4a64f543 4115 update_watchpoint (bs->breakpoint_at, 0 /* don't reparse. */);
d832cb68 4116 need_remove_insert = 1;
d983da9c
DJ
4117 }
4118
d832cb68 4119 if (need_remove_insert)
2d134ed3 4120 update_global_location_list (1);
f431efe5
PA
4121 else if (removed_any)
4122 update_global_location_list (0);
d832cb68 4123
5760d0ab 4124 return bs_head;
c906108c 4125}
628fe4e4
JK
4126
4127static void
4128handle_jit_event (void)
4129{
4130 struct frame_info *frame;
4131 struct gdbarch *gdbarch;
4132
4133 /* Switch terminal for any messages produced by
4134 breakpoint_re_set. */
4135 target_terminal_ours_for_output ();
4136
4137 frame = get_current_frame ();
4138 gdbarch = get_frame_arch (frame);
4139
4140 jit_event_handler (gdbarch);
4141
4142 target_terminal_inferior ();
4143}
4144
4145/* Prepare WHAT final decision for infrun. */
4146
4147/* Decide what infrun needs to do with this bpstat. */
4148
c906108c 4149struct bpstat_what
0e30163f 4150bpstat_what (bpstat bs_head)
c906108c 4151{
c906108c 4152 struct bpstat_what retval;
628fe4e4
JK
4153 /* We need to defer calling `solib_add', as adding new symbols
4154 resets breakpoints, which in turn deletes breakpoint locations,
4155 and hence may clear unprocessed entries in the BS chain. */
4156 int shlib_event = 0;
4157 int jit_event = 0;
0e30163f 4158 bpstat bs;
c906108c 4159
628fe4e4 4160 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4161 retval.call_dummy = STOP_NONE;
186c406b 4162 retval.is_longjmp = 0;
628fe4e4 4163
0e30163f 4164 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4165 {
628fe4e4
JK
4166 /* Extract this BS's action. After processing each BS, we check
4167 if its action overrides all we've seem so far. */
4168 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4169 enum bptype bptype;
4170
c906108c 4171 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4172 {
4173 /* I suspect this can happen if it was a momentary
4174 breakpoint which has since been deleted. */
4175 bptype = bp_none;
4176 }
20874c92 4177 else
f431efe5 4178 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4179
4180 switch (bptype)
c906108c
SS
4181 {
4182 case bp_none:
628fe4e4 4183 break;
c906108c
SS
4184 case bp_breakpoint:
4185 case bp_hardware_breakpoint:
4186 case bp_until:
4187 case bp_finish:
4188 if (bs->stop)
4189 {
4190 if (bs->print)
628fe4e4 4191 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4192 else
628fe4e4 4193 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4194 }
4195 else
628fe4e4 4196 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4197 break;
4198 case bp_watchpoint:
4199 case bp_hardware_watchpoint:
4200 case bp_read_watchpoint:
4201 case bp_access_watchpoint:
4202 if (bs->stop)
4203 {
4204 if (bs->print)
628fe4e4 4205 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4206 else
628fe4e4 4207 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4208 }
4209 else
628fe4e4
JK
4210 {
4211 /* There was a watchpoint, but we're not stopping.
4212 This requires no further action. */
4213 }
c906108c
SS
4214 break;
4215 case bp_longjmp:
186c406b 4216 case bp_exception:
628fe4e4 4217 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4218 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4219 break;
4220 case bp_longjmp_resume:
186c406b 4221 case bp_exception_resume:
628fe4e4 4222 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4223 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4224 break;
4225 case bp_step_resume:
4226 if (bs->stop)
628fe4e4
JK
4227 this_action = BPSTAT_WHAT_STEP_RESUME;
4228 else
c906108c 4229 {
628fe4e4
JK
4230 /* It is for the wrong frame. */
4231 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4232 }
c906108c 4233 break;
2c03e5be
PA
4234 case bp_hp_step_resume:
4235 if (bs->stop)
4236 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4237 else
4238 {
4239 /* It is for the wrong frame. */
4240 this_action = BPSTAT_WHAT_SINGLE;
4241 }
4242 break;
c906108c 4243 case bp_watchpoint_scope:
c4093a6a 4244 case bp_thread_event:
1900040c 4245 case bp_overlay_event:
0fd8e87f 4246 case bp_longjmp_master:
aa7d318d 4247 case bp_std_terminate_master:
186c406b 4248 case bp_exception_master:
628fe4e4 4249 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4250 break;
ce78b96d 4251 case bp_catchpoint:
c5aa993b
JM
4252 if (bs->stop)
4253 {
4254 if (bs->print)
628fe4e4 4255 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4256 else
628fe4e4 4257 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4258 }
4259 else
628fe4e4
JK
4260 {
4261 /* There was a catchpoint, but we're not stopping.
4262 This requires no further action. */
4263 }
4264 break;
4265 case bp_shlib_event:
4266 shlib_event = 1;
4267
4268 /* If requested, stop when the dynamic linker notifies GDB
4269 of events. This allows the user to get control and place
4270 breakpoints in initializer routines for dynamically
4271 loaded objects (among other things). */
4272 if (stop_on_solib_events)
4273 this_action = BPSTAT_WHAT_STOP_NOISY;
4274 else
4275 this_action = BPSTAT_WHAT_SINGLE;
4276 break;
4277 case bp_jit_event:
4278 jit_event = 1;
4279 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4280 break;
c906108c 4281 case bp_call_dummy:
53a5351d
JM
4282 /* Make sure the action is stop (silent or noisy),
4283 so infrun.c pops the dummy frame. */
aa7d318d 4284 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4285 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4286 break;
4287 case bp_std_terminate:
4288 /* Make sure the action is stop (silent or noisy),
4289 so infrun.c pops the dummy frame. */
aa7d318d 4290 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4291 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4292 break;
1042e4c0 4293 case bp_tracepoint:
7a697b8d 4294 case bp_fast_tracepoint:
0fb4aa4b 4295 case bp_static_tracepoint:
1042e4c0
SS
4296 /* Tracepoint hits should not be reported back to GDB, and
4297 if one got through somehow, it should have been filtered
4298 out already. */
4299 internal_error (__FILE__, __LINE__,
7a697b8d 4300 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4301 break;
4302 case bp_gnu_ifunc_resolver:
4303 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4304 this_action = BPSTAT_WHAT_SINGLE;
4305 break;
4306 case bp_gnu_ifunc_resolver_return:
4307 /* The breakpoint will be removed, execution will restart from the
4308 PC of the former breakpoint. */
4309 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4310 break;
628fe4e4
JK
4311 default:
4312 internal_error (__FILE__, __LINE__,
4313 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4314 }
628fe4e4
JK
4315
4316 retval.main_action = max (retval.main_action, this_action);
c906108c 4317 }
628fe4e4 4318
0e30163f
JK
4319 /* These operations may affect the bs->breakpoint_at state so they are
4320 delayed after MAIN_ACTION is decided above. */
4321
628fe4e4
JK
4322 if (shlib_event)
4323 {
4324 if (debug_infrun)
4325 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4326
4327 /* Check for any newly added shared libraries if we're supposed
4328 to be adding them automatically. */
4329
4330 /* Switch terminal for any messages produced by
4331 breakpoint_re_set. */
4332 target_terminal_ours_for_output ();
4333
4334#ifdef SOLIB_ADD
4335 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4336#else
4337 solib_add (NULL, 0, &current_target, auto_solib_add);
4338#endif
4339
4340 target_terminal_inferior ();
4341 }
4342
4343 if (jit_event)
4344 {
4345 if (debug_infrun)
4346 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4347
4348 handle_jit_event ();
4349 }
4350
0e30163f
JK
4351 for (bs = bs_head; bs != NULL; bs = bs->next)
4352 {
4353 struct breakpoint *b = bs->breakpoint_at;
4354
4355 if (b == NULL)
4356 continue;
4357 switch (b->type)
4358 {
4359 case bp_gnu_ifunc_resolver:
4360 gnu_ifunc_resolver_stop (b);
4361 break;
4362 case bp_gnu_ifunc_resolver_return:
4363 gnu_ifunc_resolver_return_stop (b);
4364 break;
4365 }
4366 }
4367
c906108c
SS
4368 return retval;
4369}
4370
4371/* Nonzero if we should step constantly (e.g. watchpoints on machines
4372 without hardware support). This isn't related to a specific bpstat,
4373 just to things like whether watchpoints are set. */
4374
c5aa993b 4375int
fba45db2 4376bpstat_should_step (void)
c906108c
SS
4377{
4378 struct breakpoint *b;
cc59ec59 4379
c906108c 4380 ALL_BREAKPOINTS (b)
717a8278 4381 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4382 return 1;
c906108c
SS
4383 return 0;
4384}
4385
67822962
PA
4386int
4387bpstat_causes_stop (bpstat bs)
4388{
4389 for (; bs != NULL; bs = bs->next)
4390 if (bs->stop)
4391 return 1;
4392
4393 return 0;
4394}
4395
c906108c 4396\f
c5aa993b 4397
170b53b2
UW
4398/* Compute a string of spaces suitable to indent the next line
4399 so it starts at the position corresponding to the table column
4400 named COL_NAME in the currently active table of UIOUT. */
4401
4402static char *
4403wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4404{
4405 static char wrap_indent[80];
4406 int i, total_width, width, align;
4407 char *text;
4408
4409 total_width = 0;
4410 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4411 {
4412 if (strcmp (text, col_name) == 0)
4413 {
4414 gdb_assert (total_width < sizeof wrap_indent);
4415 memset (wrap_indent, ' ', total_width);
4416 wrap_indent[total_width] = 0;
4417
4418 return wrap_indent;
4419 }
4420
4421 total_width += width + 1;
4422 }
4423
4424 return NULL;
4425}
4426
859825b8
JK
4427/* Print the LOC location out of the list of B->LOC locations. */
4428
170b53b2
UW
4429static void
4430print_breakpoint_location (struct breakpoint *b,
4431 struct bp_location *loc)
0d381245 4432{
6c95b8df
PA
4433 struct cleanup *old_chain = save_current_program_space ();
4434
859825b8
JK
4435 if (loc != NULL && loc->shlib_disabled)
4436 loc = NULL;
4437
6c95b8df
PA
4438 if (loc != NULL)
4439 set_current_program_space (loc->pspace);
4440
56435ebe
TT
4441 if (b->display_canonical)
4442 ui_out_field_string (uiout, "what", b->addr_string);
4443 else if (b->source_file && loc)
0d381245
VP
4444 {
4445 struct symbol *sym
4446 = find_pc_sect_function (loc->address, loc->section);
4447 if (sym)
4448 {
4449 ui_out_text (uiout, "in ");
4450 ui_out_field_string (uiout, "func",
4451 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4452 ui_out_text (uiout, " ");
4453 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4454 ui_out_text (uiout, "at ");
0d381245
VP
4455 }
4456 ui_out_field_string (uiout, "file", b->source_file);
4457 ui_out_text (uiout, ":");
4458
4459 if (ui_out_is_mi_like_p (uiout))
4460 {
4461 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4462 char *fullname = symtab_to_fullname (sal.symtab);
4463
4464 if (fullname)
4465 ui_out_field_string (uiout, "fullname", fullname);
4466 }
4467
4468 ui_out_field_int (uiout, "line", b->line_number);
4469 }
859825b8 4470 else if (loc)
0d381245 4471 {
170b53b2
UW
4472 struct ui_stream *stb = ui_out_stream_new (uiout);
4473 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4474
22e722e1
DJ
4475 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4476 demangle, "");
0d381245 4477 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4478
4479 do_cleanups (stb_chain);
0d381245 4480 }
859825b8
JK
4481 else
4482 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4483
4484 do_cleanups (old_chain);
0d381245
VP
4485}
4486
269b11a2
PA
4487static const char *
4488bptype_string (enum bptype type)
c906108c 4489{
c4093a6a
JM
4490 struct ep_type_description
4491 {
4492 enum bptype type;
4493 char *description;
4494 };
4495 static struct ep_type_description bptypes[] =
c906108c 4496 {
c5aa993b
JM
4497 {bp_none, "?deleted?"},
4498 {bp_breakpoint, "breakpoint"},
c906108c 4499 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4500 {bp_until, "until"},
4501 {bp_finish, "finish"},
4502 {bp_watchpoint, "watchpoint"},
c906108c 4503 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4504 {bp_read_watchpoint, "read watchpoint"},
4505 {bp_access_watchpoint, "acc watchpoint"},
4506 {bp_longjmp, "longjmp"},
4507 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4508 {bp_exception, "exception"},
4509 {bp_exception_resume, "exception resume"},
c5aa993b 4510 {bp_step_resume, "step resume"},
2c03e5be 4511 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
4512 {bp_watchpoint_scope, "watchpoint scope"},
4513 {bp_call_dummy, "call dummy"},
aa7d318d 4514 {bp_std_terminate, "std::terminate"},
c5aa993b 4515 {bp_shlib_event, "shlib events"},
c4093a6a 4516 {bp_thread_event, "thread events"},
1900040c 4517 {bp_overlay_event, "overlay events"},
0fd8e87f 4518 {bp_longjmp_master, "longjmp master"},
aa7d318d 4519 {bp_std_terminate_master, "std::terminate master"},
186c406b 4520 {bp_exception_master, "exception master"},
ce78b96d 4521 {bp_catchpoint, "catchpoint"},
1042e4c0 4522 {bp_tracepoint, "tracepoint"},
7a697b8d 4523 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4524 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4525 {bp_jit_event, "jit events"},
0e30163f
JK
4526 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4527 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4528 };
269b11a2
PA
4529
4530 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4531 || ((int) type != bptypes[(int) type].type))
4532 internal_error (__FILE__, __LINE__,
4533 _("bptypes table does not describe type #%d."),
4534 (int) type);
4535
4536 return bptypes[(int) type].description;
4537}
4538
4539/* Print B to gdb_stdout. */
4540
4541static void
4542print_one_breakpoint_location (struct breakpoint *b,
4543 struct bp_location *loc,
4544 int loc_number,
4545 struct bp_location **last_loc,
269b11a2
PA
4546 int allflag)
4547{
4548 struct command_line *l;
c2c6d25f 4549 static char bpenables[] = "nynny";
c906108c 4550
0d381245
VP
4551 int header_of_multiple = 0;
4552 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4553 struct value_print_options opts;
4554
4555 get_user_print_options (&opts);
0d381245
VP
4556
4557 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4558 /* See comment in print_one_breakpoint concerning treatment of
4559 breakpoints with single disabled location. */
0d381245
VP
4560 if (loc == NULL
4561 && (b->loc != NULL
4562 && (b->loc->next != NULL || !b->loc->enabled)))
4563 header_of_multiple = 1;
4564 if (loc == NULL)
4565 loc = b->loc;
4566
c4093a6a
JM
4567 annotate_record ();
4568
4569 /* 1 */
4570 annotate_field (0);
0d381245
VP
4571 if (part_of_multiple)
4572 {
4573 char *formatted;
0c6773c1 4574 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4575 ui_out_field_string (uiout, "number", formatted);
4576 xfree (formatted);
4577 }
4578 else
4579 {
4580 ui_out_field_int (uiout, "number", b->number);
4581 }
c4093a6a
JM
4582
4583 /* 2 */
4584 annotate_field (1);
0d381245
VP
4585 if (part_of_multiple)
4586 ui_out_field_skip (uiout, "type");
269b11a2
PA
4587 else
4588 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4589
4590 /* 3 */
4591 annotate_field (2);
0d381245
VP
4592 if (part_of_multiple)
4593 ui_out_field_skip (uiout, "disp");
4594 else
2cec12e5 4595 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4596
c4093a6a
JM
4597
4598 /* 4 */
4599 annotate_field (3);
0d381245 4600 if (part_of_multiple)
54e52265 4601 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4602 else
4a64f543
MS
4603 ui_out_field_fmt (uiout, "enabled", "%c",
4604 bpenables[(int) b->enable_state]);
54e52265 4605 ui_out_spaces (uiout, 2);
0d381245 4606
c4093a6a
JM
4607
4608 /* 5 and 6 */
3086aeae 4609 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4610 {
4a64f543
MS
4611 /* Although the print_one can possibly print all locations,
4612 calling it here is not likely to get any nice result. So,
4613 make sure there's just one location. */
0d381245 4614 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4615 b->ops->print_one (b, last_loc);
0d381245 4616 }
3086aeae
DJ
4617 else
4618 switch (b->type)
4619 {
4620 case bp_none:
4621 internal_error (__FILE__, __LINE__,
e2e0b3e5 4622 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4623 break;
c906108c 4624
3086aeae
DJ
4625 case bp_watchpoint:
4626 case bp_hardware_watchpoint:
4627 case bp_read_watchpoint:
4628 case bp_access_watchpoint:
4629 /* Field 4, the address, is omitted (which makes the columns
4630 not line up too nicely with the headers, but the effect
4631 is relatively readable). */
79a45b7d 4632 if (opts.addressprint)
3086aeae
DJ
4633 ui_out_field_skip (uiout, "addr");
4634 annotate_field (5);
fa8a61dc 4635 ui_out_field_string (uiout, "what", b->exp_string);
3086aeae
DJ
4636 break;
4637
3086aeae
DJ
4638 case bp_breakpoint:
4639 case bp_hardware_breakpoint:
4640 case bp_until:
4641 case bp_finish:
4642 case bp_longjmp:
4643 case bp_longjmp_resume:
186c406b
TT
4644 case bp_exception:
4645 case bp_exception_resume:
3086aeae 4646 case bp_step_resume:
2c03e5be 4647 case bp_hp_step_resume:
3086aeae
DJ
4648 case bp_watchpoint_scope:
4649 case bp_call_dummy:
aa7d318d 4650 case bp_std_terminate:
3086aeae
DJ
4651 case bp_shlib_event:
4652 case bp_thread_event:
4653 case bp_overlay_event:
0fd8e87f 4654 case bp_longjmp_master:
aa7d318d 4655 case bp_std_terminate_master:
186c406b 4656 case bp_exception_master:
1042e4c0 4657 case bp_tracepoint:
7a697b8d 4658 case bp_fast_tracepoint:
0fb4aa4b 4659 case bp_static_tracepoint:
4efc6507 4660 case bp_jit_event:
0e30163f
JK
4661 case bp_gnu_ifunc_resolver:
4662 case bp_gnu_ifunc_resolver_return:
79a45b7d 4663 if (opts.addressprint)
3086aeae
DJ
4664 {
4665 annotate_field (4);
54e52265 4666 if (header_of_multiple)
0d381245 4667 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4668 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4669 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4670 else
5af949e3
UW
4671 ui_out_field_core_addr (uiout, "addr",
4672 loc->gdbarch, loc->address);
3086aeae
DJ
4673 }
4674 annotate_field (5);
0d381245 4675 if (!header_of_multiple)
170b53b2 4676 print_breakpoint_location (b, loc);
0d381245 4677 if (b->loc)
a6d9a66e 4678 *last_loc = b->loc;
3086aeae
DJ
4679 break;
4680 }
c906108c 4681
6c95b8df
PA
4682
4683 /* For backward compatibility, don't display inferiors unless there
4684 are several. */
4685 if (loc != NULL
4686 && !header_of_multiple
4687 && (allflag
4688 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4689 && (number_of_program_spaces () > 1
4690 || number_of_inferiors () > 1)
4a64f543
MS
4691 /* LOC is for existing B, it cannot be in
4692 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4693 && loc->owner->type != bp_catchpoint)))
4694 {
4695 struct inferior *inf;
4696 int first = 1;
4697
4698 for (inf = inferior_list; inf != NULL; inf = inf->next)
4699 {
4700 if (inf->pspace == loc->pspace)
4701 {
4702 if (first)
4703 {
4704 first = 0;
4705 ui_out_text (uiout, " inf ");
4706 }
4707 else
4708 ui_out_text (uiout, ", ");
4709 ui_out_text (uiout, plongest (inf->num));
4710 }
4711 }
4712 }
4713
4a306c9a 4714 if (!part_of_multiple)
c4093a6a 4715 {
4a306c9a
JB
4716 if (b->thread != -1)
4717 {
4718 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4719 "stop only in" line a little further down. */
4a306c9a
JB
4720 ui_out_text (uiout, " thread ");
4721 ui_out_field_int (uiout, "thread", b->thread);
4722 }
4723 else if (b->task != 0)
4724 {
4725 ui_out_text (uiout, " task ");
4726 ui_out_field_int (uiout, "task", b->task);
4727 }
c4093a6a 4728 }
f1310107 4729
8b93c638 4730 ui_out_text (uiout, "\n");
f1310107 4731
348d480f 4732 if (!part_of_multiple)
f1310107
TJB
4733 b->ops->print_one_detail (b, uiout);
4734
0d381245 4735 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4736 {
4737 annotate_field (6);
8b93c638 4738 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 4739 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 4740 the frame ID. */
5af949e3
UW
4741 ui_out_field_core_addr (uiout, "frame",
4742 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4743 ui_out_text (uiout, "\n");
c4093a6a
JM
4744 }
4745
28010a5d 4746 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
4747 {
4748 annotate_field (7);
d77f58be 4749 if (is_tracepoint (b))
1042e4c0
SS
4750 ui_out_text (uiout, "\ttrace only if ");
4751 else
4752 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4753 ui_out_field_string (uiout, "cond", b->cond_string);
4754 ui_out_text (uiout, "\n");
4755 }
4756
0d381245 4757 if (!part_of_multiple && b->thread != -1)
c4093a6a 4758 {
4a64f543 4759 /* FIXME should make an annotation for this. */
8b93c638
JM
4760 ui_out_text (uiout, "\tstop only in thread ");
4761 ui_out_field_int (uiout, "thread", b->thread);
4762 ui_out_text (uiout, "\n");
c4093a6a
JM
4763 }
4764
63c715c6 4765 if (!part_of_multiple && b->hit_count)
c4093a6a 4766 {
4a64f543 4767 /* FIXME should make an annotation for this. */
8b93c638
JM
4768 if (ep_is_catchpoint (b))
4769 ui_out_text (uiout, "\tcatchpoint");
4770 else
4771 ui_out_text (uiout, "\tbreakpoint");
4772 ui_out_text (uiout, " already hit ");
4773 ui_out_field_int (uiout, "times", b->hit_count);
4774 if (b->hit_count == 1)
4775 ui_out_text (uiout, " time\n");
4776 else
4777 ui_out_text (uiout, " times\n");
c4093a6a
JM
4778 }
4779
4a64f543
MS
4780 /* Output the count also if it is zero, but only if this is mi.
4781 FIXME: Should have a better test for this. */
9dc5e2a9 4782 if (ui_out_is_mi_like_p (uiout))
63c715c6 4783 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4784 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4785
0d381245 4786 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4787 {
4788 annotate_field (8);
8b93c638
JM
4789 ui_out_text (uiout, "\tignore next ");
4790 ui_out_field_int (uiout, "ignore", b->ignore_count);
4791 ui_out_text (uiout, " hits\n");
c4093a6a 4792 }
059fb39f 4793
9add0f1b 4794 l = b->commands ? b->commands->commands : NULL;
059fb39f 4795 if (!part_of_multiple && l)
c4093a6a 4796 {
3b31d625
EZ
4797 struct cleanup *script_chain;
4798
c4093a6a 4799 annotate_field (9);
3b31d625 4800 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4801 print_command_lines (uiout, l, 4);
3b31d625 4802 do_cleanups (script_chain);
c4093a6a 4803 }
d24317b4 4804
1042e4c0
SS
4805 if (!part_of_multiple && b->pass_count)
4806 {
4807 annotate_field (10);
4808 ui_out_text (uiout, "\tpass count ");
4809 ui_out_field_int (uiout, "pass", b->pass_count);
4810 ui_out_text (uiout, " \n");
4811 }
4812
d24317b4
VP
4813 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4814 {
4815 if (b->addr_string)
4816 ui_out_field_string (uiout, "original-location", b->addr_string);
4817 else if (b->exp_string)
4818 ui_out_field_string (uiout, "original-location", b->exp_string);
4819 }
c4093a6a 4820}
c5aa993b 4821
0d381245
VP
4822static void
4823print_one_breakpoint (struct breakpoint *b,
4a64f543 4824 struct bp_location **last_loc,
6c95b8df 4825 int allflag)
0d381245 4826{
8d3788bd
VP
4827 struct cleanup *bkpt_chain;
4828
4829 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
4830
12c5a436 4831 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 4832 do_cleanups (bkpt_chain);
0d381245
VP
4833
4834 /* If this breakpoint has custom print function,
4835 it's already printed. Otherwise, print individual
4836 locations, if any. */
4837 if (b->ops == NULL || b->ops->print_one == NULL)
4838 {
4a64f543
MS
4839 /* If breakpoint has a single location that is disabled, we
4840 print it as if it had several locations, since otherwise it's
4841 hard to represent "breakpoint enabled, location disabled"
4842 situation.
4843
4844 Note that while hardware watchpoints have several locations
a3be7890 4845 internally, that's not a property exposed to user. */
0d381245 4846 if (b->loc
a5606eee 4847 && !is_hardware_watchpoint (b)
8d3788bd 4848 && (b->loc->next || !b->loc->enabled))
0d381245
VP
4849 {
4850 struct bp_location *loc;
4851 int n = 1;
8d3788bd 4852
0d381245 4853 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
4854 {
4855 struct cleanup *inner2 =
4856 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
4857 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
4858 do_cleanups (inner2);
4859 }
0d381245
VP
4860 }
4861 }
4862}
4863
a6d9a66e
UW
4864static int
4865breakpoint_address_bits (struct breakpoint *b)
4866{
4867 int print_address_bits = 0;
4868 struct bp_location *loc;
4869
4870 for (loc = b->loc; loc; loc = loc->next)
4871 {
c7437ca6
PA
4872 int addr_bit;
4873
4874 /* Software watchpoints that aren't watching memory don't have
4875 an address to print. */
4876 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
4877 continue;
4878
4879 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
4880 if (addr_bit > print_address_bits)
4881 print_address_bits = addr_bit;
4882 }
4883
4884 return print_address_bits;
4885}
0d381245 4886
c4093a6a
JM
4887struct captured_breakpoint_query_args
4888 {
4889 int bnum;
4890 };
c5aa993b 4891
c4093a6a 4892static int
2b65245e 4893do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
4894{
4895 struct captured_breakpoint_query_args *args = data;
52f0bd74 4896 struct breakpoint *b;
a6d9a66e 4897 struct bp_location *dummy_loc = NULL;
cc59ec59 4898
c4093a6a
JM
4899 ALL_BREAKPOINTS (b)
4900 {
4901 if (args->bnum == b->number)
c5aa993b 4902 {
12c5a436 4903 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 4904 return GDB_RC_OK;
c5aa993b 4905 }
c4093a6a
JM
4906 }
4907 return GDB_RC_NONE;
4908}
c5aa993b 4909
c4093a6a 4910enum gdb_rc
4a64f543
MS
4911gdb_breakpoint_query (struct ui_out *uiout, int bnum,
4912 char **error_message)
c4093a6a
JM
4913{
4914 struct captured_breakpoint_query_args args;
cc59ec59 4915
c4093a6a
JM
4916 args.bnum = bnum;
4917 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 4918 an error. */
b0b13bb4
DJ
4919 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
4920 error_message, RETURN_MASK_ALL) < 0)
4921 return GDB_RC_FAIL;
4922 else
4923 return GDB_RC_OK;
c4093a6a 4924}
c5aa993b 4925
09d682a4
TT
4926/* Return true if this breakpoint was set by the user, false if it is
4927 internal or momentary. */
4928
4929int
4930user_breakpoint_p (struct breakpoint *b)
4931{
46c6471b 4932 return b->number > 0;
09d682a4
TT
4933}
4934
7f3b0473 4935/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
4936 number BNUM. If BNUM is -1 print all user-settable breakpoints.
4937 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
4938 FILTER is non-NULL, call it on each breakpoint and only include the
4939 ones for which it returns non-zero. Return the total number of
4940 breakpoints listed. */
c906108c 4941
d77f58be 4942static int
e5a67952 4943breakpoint_1 (char *args, int allflag,
4a64f543 4944 int (*filter) (const struct breakpoint *))
c4093a6a 4945{
52f0bd74 4946 struct breakpoint *b;
a6d9a66e 4947 struct bp_location *last_loc = NULL;
7f3b0473 4948 int nr_printable_breakpoints;
3b31d625 4949 struct cleanup *bkpttbl_chain;
79a45b7d 4950 struct value_print_options opts;
a6d9a66e 4951 int print_address_bits = 0;
269b11a2
PA
4952 int print_type_col_width = 14;
4953
79a45b7d
TT
4954 get_user_print_options (&opts);
4955
4a64f543
MS
4956 /* Compute the number of rows in the table, as well as the size
4957 required for address fields. */
7f3b0473
AC
4958 nr_printable_breakpoints = 0;
4959 ALL_BREAKPOINTS (b)
e5a67952
MS
4960 {
4961 /* If we have a filter, only list the breakpoints it accepts. */
4962 if (filter && !filter (b))
4963 continue;
4964
4965 /* If we have an "args" string, it is a list of breakpoints to
4966 accept. Skip the others. */
4967 if (args != NULL && *args != '\0')
4968 {
4969 if (allflag && parse_and_eval_long (args) != b->number)
4970 continue;
4971 if (!allflag && !number_is_in_list (args, b->number))
4972 continue;
4973 }
269b11a2 4974
e5a67952
MS
4975 if (allflag || user_breakpoint_p (b))
4976 {
4977 int addr_bit, type_len;
a6d9a66e 4978
e5a67952
MS
4979 addr_bit = breakpoint_address_bits (b);
4980 if (addr_bit > print_address_bits)
4981 print_address_bits = addr_bit;
269b11a2 4982
e5a67952
MS
4983 type_len = strlen (bptype_string (b->type));
4984 if (type_len > print_type_col_width)
4985 print_type_col_width = type_len;
4986
4987 nr_printable_breakpoints++;
4988 }
4989 }
7f3b0473 4990
79a45b7d 4991 if (opts.addressprint)
3b31d625 4992 bkpttbl_chain
3e43a32a
MS
4993 = make_cleanup_ui_out_table_begin_end (uiout, 6,
4994 nr_printable_breakpoints,
3b31d625 4995 "BreakpointTable");
8b93c638 4996 else
3b31d625 4997 bkpttbl_chain
3e43a32a
MS
4998 = make_cleanup_ui_out_table_begin_end (uiout, 5,
4999 nr_printable_breakpoints,
3b31d625 5000 "BreakpointTable");
8b93c638 5001
7f3b0473 5002 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5003 annotate_breakpoints_headers ();
5004 if (nr_printable_breakpoints > 0)
5005 annotate_field (0);
4a64f543 5006 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5007 if (nr_printable_breakpoints > 0)
5008 annotate_field (1);
269b11a2 5009 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5010 "type", "Type"); /* 2 */
d7faa9e7
AC
5011 if (nr_printable_breakpoints > 0)
5012 annotate_field (2);
4a64f543 5013 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5014 if (nr_printable_breakpoints > 0)
5015 annotate_field (3);
54e52265 5016 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5017 if (opts.addressprint)
e5a67952
MS
5018 {
5019 if (nr_printable_breakpoints > 0)
5020 annotate_field (4);
5021 if (print_address_bits <= 32)
5022 ui_out_table_header (uiout, 10, ui_left,
5023 "addr", "Address"); /* 5 */
5024 else
5025 ui_out_table_header (uiout, 18, ui_left,
5026 "addr", "Address"); /* 5 */
5027 }
d7faa9e7
AC
5028 if (nr_printable_breakpoints > 0)
5029 annotate_field (5);
5030 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5031 ui_out_table_body (uiout);
5032 if (nr_printable_breakpoints > 0)
5033 annotate_breakpoints_table ();
7f3b0473 5034
c4093a6a 5035 ALL_BREAKPOINTS (b)
e5a67952
MS
5036 {
5037 QUIT;
5038 /* If we have a filter, only list the breakpoints it accepts. */
5039 if (filter && !filter (b))
5040 continue;
5041
5042 /* If we have an "args" string, it is a list of breakpoints to
5043 accept. Skip the others. */
5044
5045 if (args != NULL && *args != '\0')
5046 {
5047 if (allflag) /* maintenance info breakpoint */
5048 {
5049 if (parse_and_eval_long (args) != b->number)
5050 continue;
5051 }
5052 else /* all others */
5053 {
5054 if (!number_is_in_list (args, b->number))
5055 continue;
5056 }
5057 }
5058 /* We only print out user settable breakpoints unless the
5059 allflag is set. */
5060 if (allflag || user_breakpoint_p (b))
12c5a436 5061 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5062 }
5063
3b31d625 5064 do_cleanups (bkpttbl_chain);
698384cd 5065
7f3b0473 5066 if (nr_printable_breakpoints == 0)
c906108c 5067 {
4a64f543
MS
5068 /* If there's a filter, let the caller decide how to report
5069 empty list. */
d77f58be
SS
5070 if (!filter)
5071 {
e5a67952 5072 if (args == NULL || *args == '\0')
d77f58be
SS
5073 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5074 else
4a64f543 5075 ui_out_message (uiout, 0,
e5a67952
MS
5076 "No breakpoint or watchpoint matching '%s'.\n",
5077 args);
d77f58be 5078 }
c906108c
SS
5079 }
5080 else
c4093a6a 5081 {
a6d9a66e
UW
5082 if (last_loc && !server_command)
5083 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5084 }
c906108c 5085
4a64f543 5086 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5087 there have been breakpoints? */
c906108c 5088 annotate_breakpoints_table_end ();
d77f58be
SS
5089
5090 return nr_printable_breakpoints;
c906108c
SS
5091}
5092
ad443146
SS
5093/* Display the value of default-collect in a way that is generally
5094 compatible with the breakpoint list. */
5095
5096static void
5097default_collect_info (void)
5098{
5099 /* If it has no value (which is frequently the case), say nothing; a
5100 message like "No default-collect." gets in user's face when it's
5101 not wanted. */
5102 if (!*default_collect)
5103 return;
5104
5105 /* The following phrase lines up nicely with per-tracepoint collect
5106 actions. */
5107 ui_out_text (uiout, "default collect ");
5108 ui_out_field_string (uiout, "default-collect", default_collect);
5109 ui_out_text (uiout, " \n");
5110}
5111
c906108c 5112static void
e5a67952 5113breakpoints_info (char *args, int from_tty)
c906108c 5114{
e5a67952 5115 breakpoint_1 (args, 0, NULL);
ad443146
SS
5116
5117 default_collect_info ();
d77f58be
SS
5118}
5119
5120static void
e5a67952 5121watchpoints_info (char *args, int from_tty)
d77f58be 5122{
e5a67952 5123 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
d77f58be
SS
5124
5125 if (num_printed == 0)
5126 {
e5a67952 5127 if (args == NULL || *args == '\0')
d77f58be
SS
5128 ui_out_message (uiout, 0, "No watchpoints.\n");
5129 else
e5a67952 5130 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5131 }
c906108c
SS
5132}
5133
7a292a7a 5134static void
e5a67952 5135maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5136{
e5a67952 5137 breakpoint_1 (args, 1, NULL);
ad443146
SS
5138
5139 default_collect_info ();
c906108c
SS
5140}
5141
0d381245 5142static int
714835d5 5143breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5144 struct program_space *pspace,
714835d5 5145 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5146{
5147 struct bp_location *bl = b->loc;
cc59ec59 5148
0d381245
VP
5149 for (; bl; bl = bl->next)
5150 {
6c95b8df
PA
5151 if (bl->pspace == pspace
5152 && bl->address == pc
0d381245
VP
5153 && (!overlay_debugging || bl->section == section))
5154 return 1;
5155 }
5156 return 0;
5157}
5158
6c95b8df
PA
5159/* Print a message describing any breakpoints set at PC. This
5160 concerns with logical breakpoints, so we match program spaces, not
5161 address spaces. */
c906108c
SS
5162
5163static void
6c95b8df
PA
5164describe_other_breakpoints (struct gdbarch *gdbarch,
5165 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5166 struct obj_section *section, int thread)
c906108c 5167{
52f0bd74
AC
5168 int others = 0;
5169 struct breakpoint *b;
c906108c
SS
5170
5171 ALL_BREAKPOINTS (b)
6c95b8df 5172 others += breakpoint_has_pc (b, pspace, pc, section);
c906108c
SS
5173 if (others > 0)
5174 {
a3f17187
AC
5175 if (others == 1)
5176 printf_filtered (_("Note: breakpoint "));
5177 else /* if (others == ???) */
5178 printf_filtered (_("Note: breakpoints "));
c906108c 5179 ALL_BREAKPOINTS (b)
6c95b8df 5180 if (breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5181 {
5182 others--;
5183 printf_filtered ("%d", b->number);
5184 if (b->thread == -1 && thread != -1)
5185 printf_filtered (" (all threads)");
5186 else if (b->thread != -1)
5187 printf_filtered (" (thread %d)", b->thread);
5188 printf_filtered ("%s%s ",
059fb39f 5189 ((b->enable_state == bp_disabled
8bea4e01
UW
5190 || b->enable_state == bp_call_disabled
5191 || b->enable_state == bp_startup_disabled)
0d381245
VP
5192 ? " (disabled)"
5193 : b->enable_state == bp_permanent
5194 ? " (permanent)"
5195 : ""),
5196 (others > 1) ? ","
5197 : ((others == 1) ? " and" : ""));
5198 }
a3f17187 5199 printf_filtered (_("also set at pc "));
5af949e3 5200 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5201 printf_filtered (".\n");
5202 }
5203}
5204\f
5205/* Set the default place to put a breakpoint
5206 for the `break' command with no arguments. */
5207
5208void
6c95b8df
PA
5209set_default_breakpoint (int valid, struct program_space *pspace,
5210 CORE_ADDR addr, struct symtab *symtab,
fba45db2 5211 int line)
c906108c
SS
5212{
5213 default_breakpoint_valid = valid;
6c95b8df 5214 default_breakpoint_pspace = pspace;
c906108c
SS
5215 default_breakpoint_address = addr;
5216 default_breakpoint_symtab = symtab;
5217 default_breakpoint_line = line;
5218}
5219
e4f237da
KB
5220/* Return true iff it is meaningful to use the address member of
5221 BPT. For some breakpoint types, the address member is irrelevant
5222 and it makes no sense to attempt to compare it to other addresses
5223 (or use it for any other purpose either).
5224
4a64f543
MS
5225 More specifically, each of the following breakpoint types will
5226 always have a zero valued address and we don't want to mark
5227 breakpoints of any of these types to be a duplicate of an actual
5228 breakpoint at address zero:
e4f237da
KB
5229
5230 bp_watchpoint
2d134ed3
PA
5231 bp_catchpoint
5232
5233*/
e4f237da
KB
5234
5235static int
5236breakpoint_address_is_meaningful (struct breakpoint *bpt)
5237{
5238 enum bptype type = bpt->type;
5239
2d134ed3
PA
5240 return (type != bp_watchpoint && type != bp_catchpoint);
5241}
5242
5243/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5244 true if LOC1 and LOC2 represent the same watchpoint location. */
5245
5246static int
4a64f543
MS
5247watchpoint_locations_match (struct bp_location *loc1,
5248 struct bp_location *loc2)
2d134ed3 5249{
2bdf28a0
JK
5250 /* Both of them must not be in moribund_locations. */
5251 gdb_assert (loc1->owner != NULL);
5252 gdb_assert (loc2->owner != NULL);
5253
4a64f543
MS
5254 /* If the target can evaluate the condition expression in hardware,
5255 then we we need to insert both watchpoints even if they are at
5256 the same place. Otherwise the watchpoint will only trigger when
5257 the condition of whichever watchpoint was inserted evaluates to
5258 true, not giving a chance for GDB to check the condition of the
5259 other watchpoint. */
0cf6dd15 5260 if ((loc1->owner->cond_exp
4a64f543
MS
5261 && target_can_accel_watchpoint_condition (loc1->address,
5262 loc1->length,
0cf6dd15
TJB
5263 loc1->watchpoint_type,
5264 loc1->owner->cond_exp))
5265 || (loc2->owner->cond_exp
4a64f543
MS
5266 && target_can_accel_watchpoint_condition (loc2->address,
5267 loc2->length,
0cf6dd15
TJB
5268 loc2->watchpoint_type,
5269 loc2->owner->cond_exp)))
5270 return 0;
5271
85d721b8
PA
5272 /* Note that this checks the owner's type, not the location's. In
5273 case the target does not support read watchpoints, but does
5274 support access watchpoints, we'll have bp_read_watchpoint
5275 watchpoints with hw_access locations. Those should be considered
5276 duplicates of hw_read locations. The hw_read locations will
5277 become hw_access locations later. */
2d134ed3
PA
5278 return (loc1->owner->type == loc2->owner->type
5279 && loc1->pspace->aspace == loc2->pspace->aspace
5280 && loc1->address == loc2->address
5281 && loc1->length == loc2->length);
e4f237da
KB
5282}
5283
6c95b8df
PA
5284/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5285 same breakpoint location. In most targets, this can only be true
5286 if ASPACE1 matches ASPACE2. On targets that have global
5287 breakpoints, the address space doesn't really matter. */
5288
5289static int
5290breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5291 struct address_space *aspace2, CORE_ADDR addr2)
5292{
5293 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5294 || aspace1 == aspace2)
5295 && addr1 == addr2);
5296}
5297
f1310107
TJB
5298/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5299 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5300 matches ASPACE2. On targets that have global breakpoints, the address
5301 space doesn't really matter. */
5302
5303static int
5304breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5305 int len1, struct address_space *aspace2,
5306 CORE_ADDR addr2)
5307{
5308 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5309 || aspace1 == aspace2)
5310 && addr2 >= addr1 && addr2 < addr1 + len1);
5311}
5312
5313/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5314 a ranged breakpoint. In most targets, a match happens only if ASPACE
5315 matches the breakpoint's address space. On targets that have global
5316 breakpoints, the address space doesn't really matter. */
5317
5318static int
5319breakpoint_location_address_match (struct bp_location *bl,
5320 struct address_space *aspace,
5321 CORE_ADDR addr)
5322{
5323 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5324 aspace, addr)
5325 || (bl->length
5326 && breakpoint_address_match_range (bl->pspace->aspace,
5327 bl->address, bl->length,
5328 aspace, addr)));
5329}
5330
2d134ed3
PA
5331/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5332 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5333 represent the same location. */
5334
5335static int
4a64f543
MS
5336breakpoint_locations_match (struct bp_location *loc1,
5337 struct bp_location *loc2)
2d134ed3 5338{
2bdf28a0
JK
5339 int hw_point1, hw_point2;
5340
5341 /* Both of them must not be in moribund_locations. */
5342 gdb_assert (loc1->owner != NULL);
5343 gdb_assert (loc2->owner != NULL);
5344
5345 hw_point1 = is_hardware_watchpoint (loc1->owner);
5346 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5347
5348 if (hw_point1 != hw_point2)
5349 return 0;
5350 else if (hw_point1)
5351 return watchpoint_locations_match (loc1, loc2);
5352 else
f1310107
TJB
5353 /* We compare bp_location.length in order to cover ranged breakpoints. */
5354 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5355 loc2->pspace->aspace, loc2->address)
5356 && loc1->length == loc2->length);
2d134ed3
PA
5357}
5358
76897487
KB
5359static void
5360breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5361 int bnum, int have_bnum)
5362{
f63fbe86
MS
5363 /* The longest string possibly returned by hex_string_custom
5364 is 50 chars. These must be at least that big for safety. */
5365 char astr1[64];
5366 char astr2[64];
76897487 5367
bb599908
PH
5368 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5369 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5370 if (have_bnum)
8a3fe4f8 5371 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5372 bnum, astr1, astr2);
5373 else
8a3fe4f8 5374 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5375}
5376
4a64f543
MS
5377/* Adjust a breakpoint's address to account for architectural
5378 constraints on breakpoint placement. Return the adjusted address.
5379 Note: Very few targets require this kind of adjustment. For most
5380 targets, this function is simply the identity function. */
76897487
KB
5381
5382static CORE_ADDR
a6d9a66e
UW
5383adjust_breakpoint_address (struct gdbarch *gdbarch,
5384 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5385{
a6d9a66e 5386 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5387 {
5388 /* Very few targets need any kind of breakpoint adjustment. */
5389 return bpaddr;
5390 }
88f7da05
KB
5391 else if (bptype == bp_watchpoint
5392 || bptype == bp_hardware_watchpoint
5393 || bptype == bp_read_watchpoint
5394 || bptype == bp_access_watchpoint
fe798b75 5395 || bptype == bp_catchpoint)
88f7da05
KB
5396 {
5397 /* Watchpoints and the various bp_catch_* eventpoints should not
5398 have their addresses modified. */
5399 return bpaddr;
5400 }
76897487
KB
5401 else
5402 {
5403 CORE_ADDR adjusted_bpaddr;
5404
5405 /* Some targets have architectural constraints on the placement
5406 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5407 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5408
5409 /* An adjusted breakpoint address can significantly alter
5410 a user's expectations. Print a warning if an adjustment
5411 is required. */
5412 if (adjusted_bpaddr != bpaddr)
5413 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5414
5415 return adjusted_bpaddr;
5416 }
5417}
5418
28010a5d
PA
5419void
5420init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5421 struct breakpoint *owner)
7cc221ef 5422{
7cc221ef
DJ
5423 memset (loc, 0, sizeof (*loc));
5424
348d480f
PA
5425 gdb_assert (ops != NULL);
5426
28010a5d
PA
5427 loc->ops = ops;
5428 loc->owner = owner;
511a6cd4 5429 loc->cond = NULL;
0d381245
VP
5430 loc->shlib_disabled = 0;
5431 loc->enabled = 1;
e049a4b5 5432
28010a5d 5433 switch (owner->type)
e049a4b5
DJ
5434 {
5435 case bp_breakpoint:
5436 case bp_until:
5437 case bp_finish:
5438 case bp_longjmp:
5439 case bp_longjmp_resume:
186c406b
TT
5440 case bp_exception:
5441 case bp_exception_resume:
e049a4b5 5442 case bp_step_resume:
2c03e5be 5443 case bp_hp_step_resume:
e049a4b5
DJ
5444 case bp_watchpoint_scope:
5445 case bp_call_dummy:
aa7d318d 5446 case bp_std_terminate:
e049a4b5
DJ
5447 case bp_shlib_event:
5448 case bp_thread_event:
5449 case bp_overlay_event:
4efc6507 5450 case bp_jit_event:
0fd8e87f 5451 case bp_longjmp_master:
aa7d318d 5452 case bp_std_terminate_master:
186c406b 5453 case bp_exception_master:
0e30163f
JK
5454 case bp_gnu_ifunc_resolver:
5455 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5456 loc->loc_type = bp_loc_software_breakpoint;
5457 break;
5458 case bp_hardware_breakpoint:
5459 loc->loc_type = bp_loc_hardware_breakpoint;
5460 break;
5461 case bp_hardware_watchpoint:
5462 case bp_read_watchpoint:
5463 case bp_access_watchpoint:
5464 loc->loc_type = bp_loc_hardware_watchpoint;
5465 break;
5466 case bp_watchpoint:
ce78b96d 5467 case bp_catchpoint:
15c3d785
PA
5468 case bp_tracepoint:
5469 case bp_fast_tracepoint:
0fb4aa4b 5470 case bp_static_tracepoint:
e049a4b5
DJ
5471 loc->loc_type = bp_loc_other;
5472 break;
5473 default:
e2e0b3e5 5474 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5475 }
5476
f431efe5 5477 loc->refc = 1;
28010a5d
PA
5478}
5479
5480/* Allocate a struct bp_location. */
5481
5482static struct bp_location *
5483allocate_bp_location (struct breakpoint *bpt)
5484{
348d480f
PA
5485 return bpt->ops->allocate_location (bpt);
5486}
7cc221ef 5487
f431efe5
PA
5488static void
5489free_bp_location (struct bp_location *loc)
fe3f5fa8 5490{
348d480f 5491 loc->ops->dtor (loc);
fe3f5fa8
VP
5492 xfree (loc);
5493}
5494
f431efe5
PA
5495/* Increment reference count. */
5496
5497static void
5498incref_bp_location (struct bp_location *bl)
5499{
5500 ++bl->refc;
5501}
5502
5503/* Decrement reference count. If the reference count reaches 0,
5504 destroy the bp_location. Sets *BLP to NULL. */
5505
5506static void
5507decref_bp_location (struct bp_location **blp)
5508{
0807b50c
PA
5509 gdb_assert ((*blp)->refc > 0);
5510
f431efe5
PA
5511 if (--(*blp)->refc == 0)
5512 free_bp_location (*blp);
5513 *blp = NULL;
5514}
5515
346774a9 5516/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 5517
346774a9
PA
5518static void
5519add_to_breakpoint_chain (struct breakpoint *b)
c906108c 5520{
346774a9 5521 struct breakpoint *b1;
c906108c 5522
346774a9
PA
5523 /* Add this breakpoint to the end of the chain so that a list of
5524 breakpoints will come out in order of increasing numbers. */
5525
5526 b1 = breakpoint_chain;
5527 if (b1 == 0)
5528 breakpoint_chain = b;
5529 else
5530 {
5531 while (b1->next)
5532 b1 = b1->next;
5533 b1->next = b;
5534 }
5535}
5536
5537/* Initializes breakpoint B with type BPTYPE and no locations yet. */
5538
5539static void
5540init_raw_breakpoint_without_location (struct breakpoint *b,
5541 struct gdbarch *gdbarch,
28010a5d
PA
5542 enum bptype bptype,
5543 struct breakpoint_ops *ops)
346774a9 5544{
c906108c 5545 memset (b, 0, sizeof (*b));
2219d63c 5546
348d480f
PA
5547 gdb_assert (ops != NULL);
5548
28010a5d 5549 b->ops = ops;
4d28f7a8 5550 b->type = bptype;
a6d9a66e 5551 b->gdbarch = gdbarch;
c906108c
SS
5552 b->language = current_language->la_language;
5553 b->input_radix = input_radix;
5554 b->thread = -1;
b5de0fa7 5555 b->enable_state = bp_enabled;
c906108c
SS
5556 b->next = 0;
5557 b->silent = 0;
5558 b->ignore_count = 0;
5559 b->commands = NULL;
818dd999 5560 b->frame_id = null_frame_id;
0d381245 5561 b->condition_not_parsed = 0;
84f4c1fe 5562 b->py_bp_object = NULL;
d0fb5eae 5563 b->related_breakpoint = b;
346774a9
PA
5564}
5565
5566/* Helper to set_raw_breakpoint below. Creates a breakpoint
5567 that has type BPTYPE and has no locations as yet. */
346774a9
PA
5568
5569static struct breakpoint *
5570set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f
PA
5571 enum bptype bptype,
5572 struct breakpoint_ops *ops)
346774a9
PA
5573{
5574 struct breakpoint *b = XNEW (struct breakpoint);
5575
348d480f 5576 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 5577 add_to_breakpoint_chain (b);
0d381245
VP
5578 return b;
5579}
5580
0e30163f
JK
5581/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5582 resolutions should be made as the user specified the location explicitly
5583 enough. */
5584
0d381245 5585static void
0e30163f 5586set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5587{
2bdf28a0
JK
5588 gdb_assert (loc->owner != NULL);
5589
0d381245 5590 if (loc->owner->type == bp_breakpoint
1042e4c0 5591 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5592 || is_tracepoint (loc->owner))
0d381245 5593 {
0e30163f
JK
5594 int is_gnu_ifunc;
5595
5596 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5597 NULL, NULL, &is_gnu_ifunc);
5598
5599 if (is_gnu_ifunc && !explicit_loc)
5600 {
5601 struct breakpoint *b = loc->owner;
5602
5603 gdb_assert (loc->pspace == current_program_space);
5604 if (gnu_ifunc_resolve_name (loc->function_name,
5605 &loc->requested_address))
5606 {
5607 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5608 loc->address = adjust_breakpoint_address (loc->gdbarch,
5609 loc->requested_address,
5610 b->type);
5611 }
5612 else if (b->type == bp_breakpoint && b->loc == loc
5613 && loc->next == NULL && b->related_breakpoint == b)
5614 {
5615 /* Create only the whole new breakpoint of this type but do not
5616 mess more complicated breakpoints with multiple locations. */
5617 b->type = bp_gnu_ifunc_resolver;
5618 }
5619 }
5620
0d381245
VP
5621 if (loc->function_name)
5622 loc->function_name = xstrdup (loc->function_name);
5623 }
5624}
5625
a6d9a66e
UW
5626/* Attempt to determine architecture of location identified by SAL. */
5627static struct gdbarch *
5628get_sal_arch (struct symtab_and_line sal)
5629{
5630 if (sal.section)
5631 return get_objfile_arch (sal.section->objfile);
5632 if (sal.symtab)
5633 return get_objfile_arch (sal.symtab->objfile);
5634
5635 return NULL;
5636}
5637
346774a9
PA
5638/* Low level routine for partially initializing a breakpoint of type
5639 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 5640 file name, and line number are provided by SAL.
0d381245
VP
5641
5642 It is expected that the caller will complete the initialization of
5643 the newly created breakpoint struct as well as output any status
c56053d2 5644 information regarding the creation of a new breakpoint. */
0d381245 5645
346774a9
PA
5646static void
5647init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d
PA
5648 struct symtab_and_line sal, enum bptype bptype,
5649 struct breakpoint_ops *ops)
0d381245 5650{
0d381245 5651 CORE_ADDR adjusted_address;
a6d9a66e
UW
5652 struct gdbarch *loc_gdbarch;
5653
28010a5d 5654 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 5655
a6d9a66e
UW
5656 loc_gdbarch = get_sal_arch (sal);
5657 if (!loc_gdbarch)
5658 loc_gdbarch = b->gdbarch;
0d381245 5659
6c95b8df
PA
5660 if (bptype != bp_catchpoint)
5661 gdb_assert (sal.pspace != NULL);
5662
0d381245
VP
5663 /* Adjust the breakpoint's address prior to allocating a location.
5664 Once we call allocate_bp_location(), that mostly uninitialized
5665 location will be placed on the location chain. Adjustment of the
8defab1a 5666 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
5667 not want its scan of the location chain to find a breakpoint and
5668 location that's only been partially initialized. */
4a64f543
MS
5669 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5670 sal.pc, b->type);
0d381245 5671
39d61571 5672 b->loc = allocate_bp_location (b);
a6d9a66e 5673 b->loc->gdbarch = loc_gdbarch;
0d381245
VP
5674 b->loc->requested_address = sal.pc;
5675 b->loc->address = adjusted_address;
6c95b8df
PA
5676 b->loc->pspace = sal.pspace;
5677
5678 /* Store the program space that was used to set the breakpoint, for
5679 breakpoint resetting. */
5680 b->pspace = sal.pspace;
0d381245
VP
5681
5682 if (sal.symtab == NULL)
5683 b->source_file = NULL;
5684 else
1b36a34b 5685 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5686 b->loc->section = sal.section;
5687 b->line_number = sal.line;
5688
0e30163f
JK
5689 set_breakpoint_location_function (b->loc,
5690 sal.explicit_pc || sal.explicit_line);
c906108c 5691
c906108c 5692 breakpoints_changed ();
346774a9 5693}
c906108c 5694
346774a9
PA
5695/* set_raw_breakpoint is a low level routine for allocating and
5696 partially initializing a breakpoint of type BPTYPE. The newly
5697 created breakpoint's address, section, source file name, and line
5698 number are provided by SAL. The newly created and partially
5699 initialized breakpoint is added to the breakpoint chain and
5700 is also returned as the value of this function.
5701
5702 It is expected that the caller will complete the initialization of
5703 the newly created breakpoint struct as well as output any status
5704 information regarding the creation of a new breakpoint. In
5705 particular, set_raw_breakpoint does NOT set the breakpoint
5706 number! Care should be taken to not allow an error to occur
5707 prior to completing the initialization of the breakpoint. If this
5708 should happen, a bogus breakpoint will be left on the chain. */
5709
5710struct breakpoint *
5711set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f
PA
5712 struct symtab_and_line sal, enum bptype bptype,
5713 struct breakpoint_ops *ops)
346774a9
PA
5714{
5715 struct breakpoint *b = XNEW (struct breakpoint);
5716
348d480f 5717 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 5718 add_to_breakpoint_chain (b);
c906108c
SS
5719 return b;
5720}
5721
c2c6d25f
JM
5722
5723/* Note that the breakpoint object B describes a permanent breakpoint
5724 instruction, hard-wired into the inferior's code. */
5725void
5726make_breakpoint_permanent (struct breakpoint *b)
5727{
0d381245 5728 struct bp_location *bl;
cc59ec59 5729
b5de0fa7 5730 b->enable_state = bp_permanent;
c2c6d25f 5731
4a64f543
MS
5732 /* By definition, permanent breakpoints are already present in the
5733 code. Mark all locations as inserted. For now,
5734 make_breakpoint_permanent is called in just one place, so it's
5735 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 5736 multiple locations or not, but it's easy to implement. */
0d381245
VP
5737 for (bl = b->loc; bl; bl = bl->next)
5738 bl->inserted = 1;
c2c6d25f
JM
5739}
5740
53a5351d 5741/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5742 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5743 initiated the operation. */
c906108c
SS
5744
5745void
186c406b 5746set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5747{
35df4500 5748 struct breakpoint *b, *b_tmp;
186c406b 5749 int thread = tp->num;
0fd8e87f
UW
5750
5751 /* To avoid having to rescan all objfile symbols at every step,
5752 we maintain a list of continually-inserted but always disabled
5753 longjmp "master" breakpoints. Here, we simply create momentary
5754 clones of those and enable them for the requested thread. */
35df4500 5755 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5756 if (b->pspace == current_program_space
186c406b
TT
5757 && (b->type == bp_longjmp_master
5758 || b->type == bp_exception_master))
0fd8e87f 5759 {
06edf0c0
PA
5760 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5761 struct breakpoint *clone;
cc59ec59 5762
06edf0c0
PA
5763 clone = momentary_breakpoint_from_master (b, type,
5764 &momentary_breakpoint_ops);
0fd8e87f
UW
5765 clone->thread = thread;
5766 }
186c406b
TT
5767
5768 tp->initiating_frame = frame;
c906108c
SS
5769}
5770
611c83ae 5771/* Delete all longjmp breakpoints from THREAD. */
c906108c 5772void
611c83ae 5773delete_longjmp_breakpoint (int thread)
c906108c 5774{
35df4500 5775 struct breakpoint *b, *b_tmp;
c906108c 5776
35df4500 5777 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5778 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5779 {
5780 if (b->thread == thread)
5781 delete_breakpoint (b);
5782 }
c906108c
SS
5783}
5784
1900040c
MS
5785void
5786enable_overlay_breakpoints (void)
5787{
52f0bd74 5788 struct breakpoint *b;
1900040c
MS
5789
5790 ALL_BREAKPOINTS (b)
5791 if (b->type == bp_overlay_event)
5792 {
5793 b->enable_state = bp_enabled;
b60e7edf 5794 update_global_location_list (1);
c02f5703 5795 overlay_events_enabled = 1;
1900040c
MS
5796 }
5797}
5798
5799void
5800disable_overlay_breakpoints (void)
5801{
52f0bd74 5802 struct breakpoint *b;
1900040c
MS
5803
5804 ALL_BREAKPOINTS (b)
5805 if (b->type == bp_overlay_event)
5806 {
5807 b->enable_state = bp_disabled;
b60e7edf 5808 update_global_location_list (0);
c02f5703 5809 overlay_events_enabled = 0;
1900040c
MS
5810 }
5811}
5812
aa7d318d
TT
5813/* Set an active std::terminate breakpoint for each std::terminate
5814 master breakpoint. */
5815void
5816set_std_terminate_breakpoint (void)
5817{
35df4500 5818 struct breakpoint *b, *b_tmp;
aa7d318d 5819
35df4500 5820 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5821 if (b->pspace == current_program_space
5822 && b->type == bp_std_terminate_master)
5823 {
06edf0c0
PA
5824 momentary_breakpoint_from_master (b, bp_std_terminate,
5825 &momentary_breakpoint_ops);
aa7d318d
TT
5826 }
5827}
5828
5829/* Delete all the std::terminate breakpoints. */
5830void
5831delete_std_terminate_breakpoint (void)
5832{
35df4500 5833 struct breakpoint *b, *b_tmp;
aa7d318d 5834
35df4500 5835 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5836 if (b->type == bp_std_terminate)
5837 delete_breakpoint (b);
5838}
5839
c4093a6a 5840struct breakpoint *
a6d9a66e 5841create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
5842{
5843 struct breakpoint *b;
c4093a6a 5844
06edf0c0
PA
5845 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
5846 &internal_breakpoint_ops);
5847
b5de0fa7 5848 b->enable_state = bp_enabled;
c4093a6a 5849 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
5850 b->addr_string
5851 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 5852
b60e7edf 5853 update_global_location_list_nothrow (1);
74960c60 5854
c4093a6a
JM
5855 return b;
5856}
5857
5858void
5859remove_thread_event_breakpoints (void)
5860{
35df4500 5861 struct breakpoint *b, *b_tmp;
c4093a6a 5862
35df4500 5863 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
5864 if (b->type == bp_thread_event
5865 && b->loc->pspace == current_program_space)
c4093a6a
JM
5866 delete_breakpoint (b);
5867}
5868
0101ce28
JJ
5869struct lang_and_radix
5870 {
5871 enum language lang;
5872 int radix;
5873 };
5874
4efc6507
DE
5875/* Create a breakpoint for JIT code registration and unregistration. */
5876
5877struct breakpoint *
5878create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5879{
5880 struct breakpoint *b;
5881
06edf0c0
PA
5882 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
5883 &internal_breakpoint_ops);
4efc6507
DE
5884 update_global_location_list_nothrow (1);
5885 return b;
5886}
0101ce28 5887
03673fc7
PP
5888/* Remove JIT code registration and unregistration breakpoint(s). */
5889
5890void
5891remove_jit_event_breakpoints (void)
5892{
5893 struct breakpoint *b, *b_tmp;
5894
5895 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5896 if (b->type == bp_jit_event
5897 && b->loc->pspace == current_program_space)
5898 delete_breakpoint (b);
5899}
5900
cae688ec
JJ
5901void
5902remove_solib_event_breakpoints (void)
5903{
35df4500 5904 struct breakpoint *b, *b_tmp;
cae688ec 5905
35df4500 5906 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
5907 if (b->type == bp_shlib_event
5908 && b->loc->pspace == current_program_space)
cae688ec
JJ
5909 delete_breakpoint (b);
5910}
5911
5912struct breakpoint *
a6d9a66e 5913create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
5914{
5915 struct breakpoint *b;
5916
06edf0c0
PA
5917 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
5918 &internal_breakpoint_ops);
b60e7edf 5919 update_global_location_list_nothrow (1);
cae688ec
JJ
5920 return b;
5921}
5922
5923/* Disable any breakpoints that are on code in shared libraries. Only
5924 apply to enabled breakpoints, disabled ones can just stay disabled. */
5925
5926void
cb851954 5927disable_breakpoints_in_shlibs (void)
cae688ec 5928{
876fa593 5929 struct bp_location *loc, **locp_tmp;
cae688ec 5930
876fa593 5931 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 5932 {
2bdf28a0 5933 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 5934 struct breakpoint *b = loc->owner;
2bdf28a0 5935
4a64f543
MS
5936 /* We apply the check to all breakpoints, including disabled for
5937 those with loc->duplicate set. This is so that when breakpoint
5938 becomes enabled, or the duplicate is removed, gdb will try to
5939 insert all breakpoints. If we don't set shlib_disabled here,
5940 we'll try to insert those breakpoints and fail. */
1042e4c0 5941 if (((b->type == bp_breakpoint)
508ccb1f 5942 || (b->type == bp_jit_event)
1042e4c0 5943 || (b->type == bp_hardware_breakpoint)
d77f58be 5944 || (is_tracepoint (b)))
6c95b8df 5945 && loc->pspace == current_program_space
0d381245 5946 && !loc->shlib_disabled
a77053c2 5947#ifdef PC_SOLIB
0d381245 5948 && PC_SOLIB (loc->address)
a77053c2 5949#else
6c95b8df 5950 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
5951#endif
5952 )
0d381245
VP
5953 {
5954 loc->shlib_disabled = 1;
5955 }
cae688ec
JJ
5956 }
5957}
5958
7a9dd1b2 5959/* Disable any breakpoints that are in an unloaded shared library.
4a64f543
MS
5960 Only apply to enabled breakpoints, disabled ones can just stay
5961 disabled. */
84acb35a 5962
75149521 5963static void
84acb35a
JJ
5964disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
5965{
876fa593 5966 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
5967 int disabled_shlib_breaks = 0;
5968
c86cf029
VP
5969 /* SunOS a.out shared libraries are always mapped, so do not
5970 disable breakpoints; they will only be reported as unloaded
5971 through clear_solib when GDB discards its shared library
5972 list. See clear_solib for more information. */
5973 if (exec_bfd != NULL
5974 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
5975 return;
5976
876fa593 5977 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 5978 {
2bdf28a0 5979 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 5980 struct breakpoint *b = loc->owner;
cc59ec59 5981
0d381245
VP
5982 if ((loc->loc_type == bp_loc_hardware_breakpoint
5983 || loc->loc_type == bp_loc_software_breakpoint)
6c95b8df 5984 && solib->pspace == loc->pspace
e2dd7057 5985 && !loc->shlib_disabled
508ccb1f
TT
5986 && (b->type == bp_breakpoint
5987 || b->type == bp_jit_event
5988 || b->type == bp_hardware_breakpoint)
e2dd7057 5989 && solib_contains_address_p (solib, loc->address))
84acb35a 5990 {
e2dd7057
PP
5991 loc->shlib_disabled = 1;
5992 /* At this point, we cannot rely on remove_breakpoint
5993 succeeding so we must mark the breakpoint as not inserted
5994 to prevent future errors occurring in remove_breakpoints. */
5995 loc->inserted = 0;
8d3788bd
VP
5996
5997 /* This may cause duplicate notifications for the same breakpoint. */
5998 observer_notify_breakpoint_modified (b);
5999
e2dd7057
PP
6000 if (!disabled_shlib_breaks)
6001 {
6002 target_terminal_ours_for_output ();
3e43a32a
MS
6003 warning (_("Temporarily disabling breakpoints "
6004 "for unloaded shared library \"%s\""),
e2dd7057 6005 solib->so_name);
84acb35a 6006 }
e2dd7057 6007 disabled_shlib_breaks = 1;
84acb35a
JJ
6008 }
6009 }
84acb35a
JJ
6010}
6011
ce78b96d
JB
6012/* FORK & VFORK catchpoints. */
6013
e29a4733
PA
6014/* An instance of this type is used to represent a fork or vfork
6015 catchpoint. It includes a "struct breakpoint" as a kind of base
6016 class; users downcast to "struct breakpoint *" when needed. A
6017 breakpoint is really of this type iff its ops pointer points to
6018 CATCH_FORK_BREAKPOINT_OPS. */
6019
6020struct fork_catchpoint
6021{
6022 /* The base class. */
6023 struct breakpoint base;
6024
6025 /* Process id of a child process whose forking triggered this
6026 catchpoint. This field is only valid immediately after this
6027 catchpoint has triggered. */
6028 ptid_t forked_inferior_pid;
6029};
6030
4a64f543
MS
6031/* Implement the "insert" breakpoint_ops method for fork
6032 catchpoints. */
ce78b96d 6033
77b06cd7
TJB
6034static int
6035insert_catch_fork (struct bp_location *bl)
ce78b96d 6036{
77b06cd7 6037 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6038}
6039
4a64f543
MS
6040/* Implement the "remove" breakpoint_ops method for fork
6041 catchpoints. */
ce78b96d
JB
6042
6043static int
77b06cd7 6044remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6045{
6046 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6047}
6048
6049/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6050 catchpoints. */
6051
6052static int
f1310107
TJB
6053breakpoint_hit_catch_fork (const struct bp_location *bl,
6054 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6055{
e29a4733
PA
6056 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6057
6058 return inferior_has_forked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6059}
6060
4a64f543
MS
6061/* Implement the "print_it" breakpoint_ops method for fork
6062 catchpoints. */
ce78b96d
JB
6063
6064static enum print_stop_action
348d480f 6065print_it_catch_fork (bpstat bs)
ce78b96d 6066{
348d480f
PA
6067 struct breakpoint *b = bs->breakpoint_at;
6068 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 6069
ce78b96d
JB
6070 annotate_catchpoint (b->number);
6071 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
e29a4733 6072 b->number, ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6073 return PRINT_SRC_AND_LOC;
6074}
6075
4a64f543
MS
6076/* Implement the "print_one" breakpoint_ops method for fork
6077 catchpoints. */
ce78b96d
JB
6078
6079static void
a6d9a66e 6080print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6081{
e29a4733 6082 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d
TT
6083 struct value_print_options opts;
6084
6085 get_user_print_options (&opts);
6086
4a64f543
MS
6087 /* Field 4, the address, is omitted (which makes the columns not
6088 line up too nicely with the headers, but the effect is relatively
6089 readable). */
79a45b7d 6090 if (opts.addressprint)
ce78b96d
JB
6091 ui_out_field_skip (uiout, "addr");
6092 annotate_field (5);
6093 ui_out_text (uiout, "fork");
e29a4733 6094 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6095 {
6096 ui_out_text (uiout, ", process ");
6097 ui_out_field_int (uiout, "what",
e29a4733 6098 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6099 ui_out_spaces (uiout, 1);
6100 }
6101}
6102
6103/* Implement the "print_mention" breakpoint_ops method for fork
6104 catchpoints. */
6105
6106static void
6107print_mention_catch_fork (struct breakpoint *b)
6108{
6109 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6110}
6111
6149aea9
PA
6112/* Implement the "print_recreate" breakpoint_ops method for fork
6113 catchpoints. */
6114
6115static void
6116print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6117{
6118 fprintf_unfiltered (fp, "catch fork");
6119}
6120
ce78b96d
JB
6121/* The breakpoint_ops structure to be used in fork catchpoints. */
6122
2060206e 6123static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 6124
4a64f543
MS
6125/* Implement the "insert" breakpoint_ops method for vfork
6126 catchpoints. */
ce78b96d 6127
77b06cd7
TJB
6128static int
6129insert_catch_vfork (struct bp_location *bl)
ce78b96d 6130{
77b06cd7 6131 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6132}
6133
4a64f543
MS
6134/* Implement the "remove" breakpoint_ops method for vfork
6135 catchpoints. */
ce78b96d
JB
6136
6137static int
77b06cd7 6138remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6139{
6140 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6141}
6142
6143/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6144 catchpoints. */
6145
6146static int
f1310107
TJB
6147breakpoint_hit_catch_vfork (const struct bp_location *bl,
6148 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6149{
e29a4733
PA
6150 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6151
6152 return inferior_has_vforked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6153}
6154
4a64f543
MS
6155/* Implement the "print_it" breakpoint_ops method for vfork
6156 catchpoints. */
ce78b96d
JB
6157
6158static enum print_stop_action
348d480f 6159print_it_catch_vfork (bpstat bs)
ce78b96d 6160{
348d480f 6161 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
6162 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6163
ce78b96d
JB
6164 annotate_catchpoint (b->number);
6165 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
e29a4733 6166 b->number, ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6167 return PRINT_SRC_AND_LOC;
6168}
6169
4a64f543
MS
6170/* Implement the "print_one" breakpoint_ops method for vfork
6171 catchpoints. */
ce78b96d
JB
6172
6173static void
a6d9a66e 6174print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6175{
e29a4733 6176 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d
TT
6177 struct value_print_options opts;
6178
6179 get_user_print_options (&opts);
4a64f543
MS
6180 /* Field 4, the address, is omitted (which makes the columns not
6181 line up too nicely with the headers, but the effect is relatively
6182 readable). */
79a45b7d 6183 if (opts.addressprint)
ce78b96d
JB
6184 ui_out_field_skip (uiout, "addr");
6185 annotate_field (5);
6186 ui_out_text (uiout, "vfork");
e29a4733 6187 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6188 {
6189 ui_out_text (uiout, ", process ");
6190 ui_out_field_int (uiout, "what",
e29a4733 6191 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6192 ui_out_spaces (uiout, 1);
6193 }
6194}
6195
6196/* Implement the "print_mention" breakpoint_ops method for vfork
6197 catchpoints. */
6198
6199static void
6200print_mention_catch_vfork (struct breakpoint *b)
6201{
6202 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6203}
6204
6149aea9
PA
6205/* Implement the "print_recreate" breakpoint_ops method for vfork
6206 catchpoints. */
6207
6208static void
6209print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6210{
6211 fprintf_unfiltered (fp, "catch vfork");
6212}
6213
ce78b96d
JB
6214/* The breakpoint_ops structure to be used in vfork catchpoints. */
6215
2060206e 6216static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 6217
be5c67c1
PA
6218/* An instance of this type is used to represent a syscall catchpoint.
6219 It includes a "struct breakpoint" as a kind of base class; users
6220 downcast to "struct breakpoint *" when needed. A breakpoint is
6221 really of this type iff its ops pointer points to
6222 CATCH_SYSCALL_BREAKPOINT_OPS. */
6223
6224struct syscall_catchpoint
6225{
6226 /* The base class. */
6227 struct breakpoint base;
6228
6229 /* Syscall numbers used for the 'catch syscall' feature. If no
6230 syscall has been specified for filtering, its value is NULL.
6231 Otherwise, it holds a list of all syscalls to be caught. The
6232 list elements are allocated with xmalloc. */
6233 VEC(int) *syscalls_to_be_caught;
6234};
6235
6236/* Implement the "dtor" breakpoint_ops method for syscall
6237 catchpoints. */
6238
6239static void
6240dtor_catch_syscall (struct breakpoint *b)
6241{
6242 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6243
6244 VEC_free (int, c->syscalls_to_be_caught);
348d480f 6245
2060206e 6246 base_breakpoint_ops.dtor (b);
be5c67c1
PA
6247}
6248
a96d9b2e
SDJ
6249/* Implement the "insert" breakpoint_ops method for syscall
6250 catchpoints. */
6251
77b06cd7
TJB
6252static int
6253insert_catch_syscall (struct bp_location *bl)
a96d9b2e 6254{
be5c67c1 6255 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6256 struct inferior *inf = current_inferior ();
6257
6258 ++inf->total_syscalls_count;
be5c67c1 6259 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6260 ++inf->any_syscall_count;
6261 else
6262 {
6263 int i, iter;
cc59ec59 6264
a96d9b2e 6265 for (i = 0;
be5c67c1 6266 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6267 i++)
6268 {
6269 int elem;
cc59ec59 6270
a96d9b2e
SDJ
6271 if (iter >= VEC_length (int, inf->syscalls_counts))
6272 {
6273 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6274 uintptr_t vec_addr_offset
6275 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6276 uintptr_t vec_addr;
6277 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6278 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6279 vec_addr_offset;
6280 memset ((void *) vec_addr, 0,
6281 (iter + 1 - old_size) * sizeof (int));
6282 }
6283 elem = VEC_index (int, inf->syscalls_counts, iter);
6284 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6285 }
6286 }
6287
77b06cd7
TJB
6288 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6289 inf->total_syscalls_count != 0,
6290 inf->any_syscall_count,
6291 VEC_length (int, inf->syscalls_counts),
6292 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6293}
6294
6295/* Implement the "remove" breakpoint_ops method for syscall
6296 catchpoints. */
6297
6298static int
77b06cd7 6299remove_catch_syscall (struct bp_location *bl)
a96d9b2e 6300{
be5c67c1 6301 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6302 struct inferior *inf = current_inferior ();
6303
6304 --inf->total_syscalls_count;
be5c67c1 6305 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6306 --inf->any_syscall_count;
6307 else
6308 {
6309 int i, iter;
cc59ec59 6310
a96d9b2e 6311 for (i = 0;
be5c67c1 6312 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6313 i++)
6314 {
6315 int elem;
6316 if (iter >= VEC_length (int, inf->syscalls_counts))
6317 /* Shouldn't happen. */
6318 continue;
6319 elem = VEC_index (int, inf->syscalls_counts, iter);
6320 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6321 }
6322 }
6323
6324 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6325 inf->total_syscalls_count != 0,
6326 inf->any_syscall_count,
6327 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6328 VEC_address (int,
6329 inf->syscalls_counts));
a96d9b2e
SDJ
6330}
6331
6332/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6333 catchpoints. */
6334
6335static int
f1310107
TJB
6336breakpoint_hit_catch_syscall (const struct bp_location *bl,
6337 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6338{
4a64f543
MS
6339 /* We must check if we are catching specific syscalls in this
6340 breakpoint. If we are, then we must guarantee that the called
6341 syscall is the same syscall we are catching. */
a96d9b2e 6342 int syscall_number = 0;
be5c67c1
PA
6343 const struct syscall_catchpoint *c
6344 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6345
6346 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6347 return 0;
6348
6349 /* Now, checking if the syscall is the same. */
be5c67c1 6350 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6351 {
6352 int i, iter;
cc59ec59 6353
a96d9b2e 6354 for (i = 0;
be5c67c1 6355 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6356 i++)
6357 if (syscall_number == iter)
6358 break;
6359 /* Not the same. */
6360 if (!iter)
6361 return 0;
6362 }
6363
6364 return 1;
6365}
6366
6367/* Implement the "print_it" breakpoint_ops method for syscall
6368 catchpoints. */
6369
6370static enum print_stop_action
348d480f 6371print_it_catch_syscall (bpstat bs)
a96d9b2e 6372{
348d480f 6373 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
6374 /* These are needed because we want to know in which state a
6375 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6376 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6377 must print "called syscall" or "returned from syscall". */
6378 ptid_t ptid;
6379 struct target_waitstatus last;
6380 struct syscall s;
6381 struct cleanup *old_chain;
6382 char *syscall_id;
6383
6384 get_last_target_status (&ptid, &last);
6385
6386 get_syscall_by_number (last.value.syscall_number, &s);
6387
6388 annotate_catchpoint (b->number);
6389
6390 if (s.name == NULL)
6391 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6392 else
6393 syscall_id = xstrprintf ("'%s'", s.name);
6394
6395 old_chain = make_cleanup (xfree, syscall_id);
6396
6397 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6398 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6399 b->number, syscall_id);
6400 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6401 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6402 b->number, syscall_id);
6403
6404 do_cleanups (old_chain);
6405
6406 return PRINT_SRC_AND_LOC;
6407}
6408
6409/* Implement the "print_one" breakpoint_ops method for syscall
6410 catchpoints. */
6411
6412static void
6413print_one_catch_syscall (struct breakpoint *b,
f1310107 6414 struct bp_location **last_loc)
a96d9b2e 6415{
be5c67c1 6416 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e
SDJ
6417 struct value_print_options opts;
6418
6419 get_user_print_options (&opts);
4a64f543
MS
6420 /* Field 4, the address, is omitted (which makes the columns not
6421 line up too nicely with the headers, but the effect is relatively
6422 readable). */
a96d9b2e
SDJ
6423 if (opts.addressprint)
6424 ui_out_field_skip (uiout, "addr");
6425 annotate_field (5);
6426
be5c67c1
PA
6427 if (c->syscalls_to_be_caught
6428 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6429 ui_out_text (uiout, "syscalls \"");
6430 else
6431 ui_out_text (uiout, "syscall \"");
6432
be5c67c1 6433 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6434 {
6435 int i, iter;
6436 char *text = xstrprintf ("%s", "");
cc59ec59 6437
a96d9b2e 6438 for (i = 0;
be5c67c1 6439 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6440 i++)
6441 {
6442 char *x = text;
6443 struct syscall s;
6444 get_syscall_by_number (iter, &s);
6445
6446 if (s.name != NULL)
6447 text = xstrprintf ("%s%s, ", text, s.name);
6448 else
6449 text = xstrprintf ("%s%d, ", text, iter);
6450
6451 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 6452 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
6453 on every call. */
6454 xfree (x);
6455 }
6456 /* Remove the last comma. */
6457 text[strlen (text) - 2] = '\0';
6458 ui_out_field_string (uiout, "what", text);
6459 }
6460 else
6461 ui_out_field_string (uiout, "what", "<any syscall>");
6462 ui_out_text (uiout, "\" ");
6463}
6464
6465/* Implement the "print_mention" breakpoint_ops method for syscall
6466 catchpoints. */
6467
6468static void
6469print_mention_catch_syscall (struct breakpoint *b)
6470{
be5c67c1
PA
6471 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6472
6473 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6474 {
6475 int i, iter;
6476
be5c67c1 6477 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6478 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6479 else
6480 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6481
6482 for (i = 0;
be5c67c1 6483 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6484 i++)
6485 {
6486 struct syscall s;
6487 get_syscall_by_number (iter, &s);
6488
6489 if (s.name)
6490 printf_filtered (" '%s' [%d]", s.name, s.number);
6491 else
6492 printf_filtered (" %d", s.number);
6493 }
6494 printf_filtered (")");
6495 }
6496 else
6497 printf_filtered (_("Catchpoint %d (any syscall)"),
6498 b->number);
6499}
6500
6149aea9
PA
6501/* Implement the "print_recreate" breakpoint_ops method for syscall
6502 catchpoints. */
6503
6504static void
6505print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6506{
be5c67c1
PA
6507 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6508
6149aea9
PA
6509 fprintf_unfiltered (fp, "catch syscall");
6510
be5c67c1 6511 if (c->syscalls_to_be_caught)
6149aea9
PA
6512 {
6513 int i, iter;
6514
6515 for (i = 0;
be5c67c1 6516 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
6517 i++)
6518 {
6519 struct syscall s;
6520
6521 get_syscall_by_number (iter, &s);
6522 if (s.name)
6523 fprintf_unfiltered (fp, " %s", s.name);
6524 else
6525 fprintf_unfiltered (fp, " %d", s.number);
6526 }
6527 }
6528}
6529
a96d9b2e
SDJ
6530/* The breakpoint_ops structure to be used in syscall catchpoints. */
6531
2060206e 6532static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
6533
6534/* Returns non-zero if 'b' is a syscall catchpoint. */
6535
6536static int
6537syscall_catchpoint_p (struct breakpoint *b)
6538{
6539 return (b->ops == &catch_syscall_breakpoint_ops);
6540}
6541
346774a9
PA
6542/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
6543 is non-zero, then make the breakpoint temporary. If COND_STRING is
6544 not NULL, then store it in the breakpoint. OPS, if not NULL, is
6545 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 6546
346774a9
PA
6547static void
6548init_catchpoint (struct breakpoint *b,
6549 struct gdbarch *gdbarch, int tempflag,
6550 char *cond_string,
6551 struct breakpoint_ops *ops)
c906108c 6552{
c5aa993b 6553 struct symtab_and_line sal;
346774a9 6554
fe39c653 6555 init_sal (&sal);
6c95b8df 6556 sal.pspace = current_program_space;
c5aa993b 6557
28010a5d 6558 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 6559
1b36a34b 6560 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 6561 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
6562}
6563
28010a5d
PA
6564void
6565install_breakpoint (struct breakpoint *b)
c56053d2
PA
6566{
6567 add_to_breakpoint_chain (b);
6568 set_breakpoint_count (breakpoint_count + 1);
6569 b->number = breakpoint_count;
6570 mention (b);
6571 observer_notify_breakpoint_created (b);
6572 update_global_location_list (1);
6573}
6574
9b70b993 6575static void
a6d9a66e
UW
6576create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6577 int tempflag, char *cond_string,
ce78b96d 6578 struct breakpoint_ops *ops)
c906108c 6579{
e29a4733 6580 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 6581
e29a4733
PA
6582 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
6583
6584 c->forked_inferior_pid = null_ptid;
6585
28010a5d 6586 install_breakpoint (&c->base);
c906108c
SS
6587}
6588
fe798b75
JB
6589/* Exec catchpoints. */
6590
b4d90040
PA
6591/* An instance of this type is used to represent an exec catchpoint.
6592 It includes a "struct breakpoint" as a kind of base class; users
6593 downcast to "struct breakpoint *" when needed. A breakpoint is
6594 really of this type iff its ops pointer points to
6595 CATCH_EXEC_BREAKPOINT_OPS. */
6596
6597struct exec_catchpoint
6598{
6599 /* The base class. */
6600 struct breakpoint base;
6601
6602 /* Filename of a program whose exec triggered this catchpoint.
6603 This field is only valid immediately after this catchpoint has
6604 triggered. */
6605 char *exec_pathname;
6606};
6607
6608/* Implement the "dtor" breakpoint_ops method for exec
6609 catchpoints. */
6610
6611static void
6612dtor_catch_exec (struct breakpoint *b)
6613{
6614 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6615
6616 xfree (c->exec_pathname);
348d480f 6617
2060206e 6618 base_breakpoint_ops.dtor (b);
b4d90040
PA
6619}
6620
77b06cd7
TJB
6621static int
6622insert_catch_exec (struct bp_location *bl)
c906108c 6623{
77b06cd7 6624 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6625}
c906108c 6626
fe798b75 6627static int
77b06cd7 6628remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6629{
6630 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6631}
c906108c 6632
fe798b75 6633static int
f1310107
TJB
6634breakpoint_hit_catch_exec (const struct bp_location *bl,
6635 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6636{
b4d90040
PA
6637 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
6638
6639 return inferior_has_execd (inferior_ptid, &c->exec_pathname);
fe798b75 6640}
c906108c 6641
fe798b75 6642static enum print_stop_action
348d480f 6643print_it_catch_exec (bpstat bs)
fe798b75 6644{
348d480f 6645 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
6646 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6647
fe798b75
JB
6648 annotate_catchpoint (b->number);
6649 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
b4d90040 6650 c->exec_pathname);
fe798b75 6651 return PRINT_SRC_AND_LOC;
c906108c
SS
6652}
6653
fe798b75 6654static void
a6d9a66e 6655print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 6656{
b4d90040 6657 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75
JB
6658 struct value_print_options opts;
6659
6660 get_user_print_options (&opts);
6661
6662 /* Field 4, the address, is omitted (which makes the columns
6663 not line up too nicely with the headers, but the effect
6664 is relatively readable). */
6665 if (opts.addressprint)
6666 ui_out_field_skip (uiout, "addr");
6667 annotate_field (5);
6668 ui_out_text (uiout, "exec");
b4d90040 6669 if (c->exec_pathname != NULL)
fe798b75
JB
6670 {
6671 ui_out_text (uiout, ", program \"");
b4d90040 6672 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
6673 ui_out_text (uiout, "\" ");
6674 }
6675}
6676
6677static void
6678print_mention_catch_exec (struct breakpoint *b)
6679{
6680 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6681}
6682
6149aea9
PA
6683/* Implement the "print_recreate" breakpoint_ops method for exec
6684 catchpoints. */
6685
6686static void
6687print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6688{
6689 fprintf_unfiltered (fp, "catch exec");
6690}
6691
2060206e 6692static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 6693
a96d9b2e
SDJ
6694static void
6695create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6696 struct breakpoint_ops *ops)
6697{
be5c67c1 6698 struct syscall_catchpoint *c;
a96d9b2e 6699 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 6700
be5c67c1
PA
6701 c = XNEW (struct syscall_catchpoint);
6702 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
6703 c->syscalls_to_be_caught = filter;
a96d9b2e 6704
28010a5d 6705 install_breakpoint (&c->base);
a96d9b2e
SDJ
6706}
6707
c906108c 6708static int
fba45db2 6709hw_breakpoint_used_count (void)
c906108c 6710{
c906108c 6711 int i = 0;
f1310107
TJB
6712 struct breakpoint *b;
6713 struct bp_location *bl;
c906108c
SS
6714
6715 ALL_BREAKPOINTS (b)
c5aa993b 6716 {
d6b74ac4 6717 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
6718 for (bl = b->loc; bl; bl = bl->next)
6719 {
6720 /* Special types of hardware breakpoints may use more than
6721 one register. */
348d480f 6722 i += b->ops->resources_needed (bl);
f1310107 6723 }
c5aa993b 6724 }
c906108c
SS
6725
6726 return i;
6727}
6728
6729static int
fba45db2 6730hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 6731{
c906108c 6732 int i = 0;
e09342b5
TJB
6733 struct breakpoint *b;
6734 struct bp_location *bl;
c906108c
SS
6735
6736 *other_type_used = 0;
6737 ALL_BREAKPOINTS (b)
e09342b5
TJB
6738 {
6739 if (!breakpoint_enabled (b))
6740 continue;
6741
c5aa993b 6742 if (b->type == type)
e09342b5
TJB
6743 for (bl = b->loc; bl; bl = bl->next)
6744 {
6745 /* Special types of hardware watchpoints may use more than
6746 one register. */
348d480f 6747 i += b->ops->resources_needed (bl);
e09342b5 6748 }
cc60f2e3 6749 else if (is_hardware_watchpoint (b))
c5aa993b 6750 *other_type_used = 1;
e09342b5
TJB
6751 }
6752
c906108c
SS
6753 return i;
6754}
6755
c906108c 6756void
fba45db2 6757disable_watchpoints_before_interactive_call_start (void)
c906108c 6758{
c5aa993b 6759 struct breakpoint *b;
c906108c
SS
6760
6761 ALL_BREAKPOINTS (b)
c5aa993b 6762 {
cc60f2e3 6763 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6764 {
b5de0fa7 6765 b->enable_state = bp_call_disabled;
b60e7edf 6766 update_global_location_list (0);
c5aa993b
JM
6767 }
6768 }
c906108c
SS
6769}
6770
6771void
fba45db2 6772enable_watchpoints_after_interactive_call_stop (void)
c906108c 6773{
c5aa993b 6774 struct breakpoint *b;
c906108c
SS
6775
6776 ALL_BREAKPOINTS (b)
c5aa993b 6777 {
cc60f2e3 6778 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 6779 {
b5de0fa7 6780 b->enable_state = bp_enabled;
b60e7edf 6781 update_global_location_list (1);
c5aa993b
JM
6782 }
6783 }
c906108c
SS
6784}
6785
8bea4e01
UW
6786void
6787disable_breakpoints_before_startup (void)
6788{
6789 struct breakpoint *b;
6790 int found = 0;
6791
6792 ALL_BREAKPOINTS (b)
6793 {
6c95b8df
PA
6794 if (b->pspace != current_program_space)
6795 continue;
6796
8bea4e01
UW
6797 if ((b->type == bp_breakpoint
6798 || b->type == bp_hardware_breakpoint)
6799 && breakpoint_enabled (b))
6800 {
6801 b->enable_state = bp_startup_disabled;
6802 found = 1;
6803 }
6804 }
6805
6806 if (found)
6807 update_global_location_list (0);
6808
6c95b8df 6809 current_program_space->executing_startup = 1;
8bea4e01
UW
6810}
6811
6812void
6813enable_breakpoints_after_startup (void)
6814{
6815 struct breakpoint *b;
6816 int found = 0;
6817
6c95b8df 6818 current_program_space->executing_startup = 0;
8bea4e01
UW
6819
6820 ALL_BREAKPOINTS (b)
6821 {
6c95b8df
PA
6822 if (b->pspace != current_program_space)
6823 continue;
6824
8bea4e01
UW
6825 if ((b->type == bp_breakpoint
6826 || b->type == bp_hardware_breakpoint)
6827 && b->enable_state == bp_startup_disabled)
6828 {
6829 b->enable_state = bp_enabled;
6830 found = 1;
6831 }
6832 }
6833
6834 if (found)
6835 breakpoint_re_set ();
6836}
6837
c906108c
SS
6838
6839/* Set a breakpoint that will evaporate an end of command
6840 at address specified by SAL.
6841 Restrict it to frame FRAME if FRAME is nonzero. */
6842
6843struct breakpoint *
a6d9a66e
UW
6844set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
6845 struct frame_id frame_id, enum bptype type)
c906108c 6846{
52f0bd74 6847 struct breakpoint *b;
edb3359d
DJ
6848
6849 /* If FRAME_ID is valid, it should be a real frame, not an inlined
6850 one. */
6851 gdb_assert (!frame_id_inlined_p (frame_id));
6852
06edf0c0 6853 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
6854 b->enable_state = bp_enabled;
6855 b->disposition = disp_donttouch;
818dd999 6856 b->frame_id = frame_id;
c906108c 6857
4a64f543
MS
6858 /* If we're debugging a multi-threaded program, then we want
6859 momentary breakpoints to be active in only a single thread of
6860 control. */
39f77062
KB
6861 if (in_thread_list (inferior_ptid))
6862 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 6863
b60e7edf 6864 update_global_location_list_nothrow (1);
74960c60 6865
c906108c
SS
6866 return b;
6867}
611c83ae 6868
06edf0c0
PA
6869/* Make a momentary breakpoint based on the master breakpoint ORIG.
6870 The new breakpoint will have type TYPE, and use OPS as it
6871 breakpoint_ops. */
e58b0e63 6872
06edf0c0
PA
6873static struct breakpoint *
6874momentary_breakpoint_from_master (struct breakpoint *orig,
6875 enum bptype type,
6876 struct breakpoint_ops *ops)
e58b0e63
PA
6877{
6878 struct breakpoint *copy;
6879
06edf0c0 6880 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 6881 copy->loc = allocate_bp_location (copy);
0e30163f 6882 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 6883
a6d9a66e 6884 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
6885 copy->loc->requested_address = orig->loc->requested_address;
6886 copy->loc->address = orig->loc->address;
6887 copy->loc->section = orig->loc->section;
6c95b8df 6888 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
6889
6890 if (orig->source_file == NULL)
6891 copy->source_file = NULL;
6892 else
6893 copy->source_file = xstrdup (orig->source_file);
6894
6895 copy->line_number = orig->line_number;
6896 copy->frame_id = orig->frame_id;
6897 copy->thread = orig->thread;
6c95b8df 6898 copy->pspace = orig->pspace;
e58b0e63
PA
6899
6900 copy->enable_state = bp_enabled;
6901 copy->disposition = disp_donttouch;
6902 copy->number = internal_breakpoint_number--;
6903
6904 update_global_location_list_nothrow (0);
6905 return copy;
6906}
6907
06edf0c0
PA
6908/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
6909 ORIG is NULL. */
6910
6911struct breakpoint *
6912clone_momentary_breakpoint (struct breakpoint *orig)
6913{
6914 /* If there's nothing to clone, then return nothing. */
6915 if (orig == NULL)
6916 return NULL;
6917
6918 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
6919}
6920
611c83ae 6921struct breakpoint *
a6d9a66e
UW
6922set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
6923 enum bptype type)
611c83ae
PA
6924{
6925 struct symtab_and_line sal;
6926
6927 sal = find_pc_line (pc, 0);
6928 sal.pc = pc;
6929 sal.section = find_pc_overlay (pc);
6930 sal.explicit_pc = 1;
6931
a6d9a66e 6932 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 6933}
c906108c 6934\f
c5aa993b 6935
c906108c
SS
6936/* Tell the user we have just set a breakpoint B. */
6937
6938static void
fba45db2 6939mention (struct breakpoint *b)
c906108c 6940{
348d480f 6941 b->ops->print_mention (b);
9dc5e2a9 6942 if (ui_out_is_mi_like_p (uiout))
fb40c209 6943 return;
c906108c
SS
6944 printf_filtered ("\n");
6945}
c906108c 6946\f
c5aa993b 6947
0d381245 6948static struct bp_location *
39d61571 6949add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
6950 const struct symtab_and_line *sal)
6951{
6952 struct bp_location *loc, **tmp;
6953
39d61571 6954 loc = allocate_bp_location (b);
0d381245
VP
6955 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
6956 ;
6957 *tmp = loc;
a6d9a66e
UW
6958 loc->gdbarch = get_sal_arch (*sal);
6959 if (!loc->gdbarch)
6960 loc->gdbarch = b->gdbarch;
0d381245 6961 loc->requested_address = sal->pc;
a6d9a66e
UW
6962 loc->address = adjust_breakpoint_address (loc->gdbarch,
6963 loc->requested_address, b->type);
6c95b8df
PA
6964 loc->pspace = sal->pspace;
6965 gdb_assert (loc->pspace != NULL);
0d381245
VP
6966 loc->section = sal->section;
6967
0e30163f
JK
6968 set_breakpoint_location_function (loc,
6969 sal->explicit_pc || sal->explicit_line);
0d381245
VP
6970 return loc;
6971}
514f746b
AR
6972\f
6973
6974/* Return 1 if LOC is pointing to a permanent breakpoint,
6975 return 0 otherwise. */
6976
6977static int
6978bp_loc_is_permanent (struct bp_location *loc)
6979{
6980 int len;
6981 CORE_ADDR addr;
6982 const gdb_byte *brk;
6983 gdb_byte *target_mem;
939c61fa
JK
6984 struct cleanup *cleanup;
6985 int retval = 0;
514f746b
AR
6986
6987 gdb_assert (loc != NULL);
6988
6989 addr = loc->address;
a6d9a66e 6990 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 6991
939c61fa
JK
6992 /* Software breakpoints unsupported? */
6993 if (brk == NULL)
6994 return 0;
6995
514f746b
AR
6996 target_mem = alloca (len);
6997
939c61fa
JK
6998 /* Enable the automatic memory restoration from breakpoints while
6999 we read the memory. Otherwise we could say about our temporary
7000 breakpoints they are permanent. */
6c95b8df
PA
7001 cleanup = save_current_space_and_thread ();
7002
7003 switch_to_program_space_and_thread (loc->pspace);
7004 make_show_memory_breakpoints_cleanup (0);
939c61fa 7005
514f746b
AR
7006 if (target_read_memory (loc->address, target_mem, len) == 0
7007 && memcmp (target_mem, brk, len) == 0)
939c61fa 7008 retval = 1;
514f746b 7009
939c61fa
JK
7010 do_cleanups (cleanup);
7011
7012 return retval;
514f746b
AR
7013}
7014
7015
c3f6f71d 7016
018d34a4
VP
7017/* Create a breakpoint with SAL as location. Use ADDR_STRING
7018 as textual description of the location, and COND_STRING
db107f19 7019 as condition expression. */
018d34a4
VP
7020
7021static void
8cdf0e15
VP
7022create_breakpoint_sal (struct gdbarch *gdbarch,
7023 struct symtabs_and_lines sals, char *addr_string,
7024 char *cond_string,
7025 enum bptype type, enum bpdisp disposition,
7026 int thread, int task, int ignore_count,
84f4c1fe 7027 struct breakpoint_ops *ops, int from_tty,
56435ebe 7028 int enabled, int internal, int display_canonical)
018d34a4 7029{
0d381245
VP
7030 struct breakpoint *b = NULL;
7031 int i;
018d34a4
VP
7032
7033 if (type == bp_hardware_breakpoint)
7034 {
7035 int i = hw_breakpoint_used_count ();
7036 int target_resources_ok =
d92524f1 7037 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7038 i + 1, 0);
7039 if (target_resources_ok == 0)
7040 error (_("No hardware breakpoint support in the target."));
7041 else if (target_resources_ok < 0)
7042 error (_("Hardware breakpoints used exceeds limit."));
7043 }
7044
6c95b8df
PA
7045 gdb_assert (sals.nelts > 0);
7046
0d381245
VP
7047 for (i = 0; i < sals.nelts; ++i)
7048 {
7049 struct symtab_and_line sal = sals.sals[i];
7050 struct bp_location *loc;
7051
7052 if (from_tty)
5af949e3
UW
7053 {
7054 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7055 if (!loc_gdbarch)
7056 loc_gdbarch = gdbarch;
7057
7058 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7059 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7060 }
0d381245
VP
7061
7062 if (i == 0)
7063 {
348d480f 7064 b = set_raw_breakpoint (gdbarch, sal, type, ops);
84f4c1fe 7065 set_breakpoint_number (internal, b);
0d381245 7066 b->thread = thread;
4a306c9a 7067 b->task = task;
018d34a4 7068
0d381245
VP
7069 b->cond_string = cond_string;
7070 b->ignore_count = ignore_count;
41447f92 7071 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7072 b->disposition = disposition;
6c95b8df
PA
7073 b->pspace = sals.sals[0].pspace;
7074
0fb4aa4b
PA
7075 if (type == bp_static_tracepoint)
7076 {
7077 struct static_tracepoint_marker marker;
7078
7079 if (is_marker_spec (addr_string))
7080 {
7081 /* We already know the marker exists, otherwise, we
7082 wouldn't see a sal for it. */
7083 char *p = &addr_string[3];
7084 char *endp;
7085 char *marker_str;
7086 int i;
7087
e9cafbcc 7088 p = skip_spaces (p);
0fb4aa4b 7089
e9cafbcc 7090 endp = skip_to_space (p);
0fb4aa4b
PA
7091
7092 marker_str = savestring (p, endp - p);
7093 b->static_trace_marker_id = marker_str;
7094
3e43a32a
MS
7095 printf_filtered (_("Probed static tracepoint "
7096 "marker \"%s\"\n"),
0fb4aa4b
PA
7097 b->static_trace_marker_id);
7098 }
7099 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7100 {
7101 b->static_trace_marker_id = xstrdup (marker.str_id);
7102 release_static_tracepoint_marker (&marker);
7103
3e43a32a
MS
7104 printf_filtered (_("Probed static tracepoint "
7105 "marker \"%s\"\n"),
0fb4aa4b
PA
7106 b->static_trace_marker_id);
7107 }
7108 else
3e43a32a
MS
7109 warning (_("Couldn't determine the static "
7110 "tracepoint marker to probe"));
0fb4aa4b
PA
7111 }
7112
6c95b8df 7113 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7114 && (b->type == bp_breakpoint
7115 || b->type == bp_hardware_breakpoint))
7116 b->enable_state = bp_startup_disabled;
7117
0d381245
VP
7118 loc = b->loc;
7119 }
7120 else
018d34a4 7121 {
39d61571 7122 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7123 }
7124
514f746b
AR
7125 if (bp_loc_is_permanent (loc))
7126 make_breakpoint_permanent (b);
7127
0d381245
VP
7128 if (b->cond_string)
7129 {
7130 char *arg = b->cond_string;
d32a6982 7131 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7132 if (*arg)
db107f19 7133 error (_("Garbage %s follows condition"), arg);
018d34a4 7134 }
0d381245 7135 }
018d34a4 7136
56435ebe 7137 b->display_canonical = display_canonical;
018d34a4
VP
7138 if (addr_string)
7139 b->addr_string = addr_string;
7140 else
7141 /* addr_string has to be used or breakpoint_re_set will delete
7142 me. */
5af949e3
UW
7143 b->addr_string
7144 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
018d34a4 7145
8d3788bd
VP
7146 /* Do not mention breakpoints with a negative number, but do
7147 notify observers. */
7148 if (!internal)
84f4c1fe 7149 mention (b);
8d3788bd 7150 observer_notify_breakpoint_created (b);
018d34a4
VP
7151}
7152
ed0616c6
VP
7153/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7154 elements to fill the void space. */
2c0b251b
PA
7155static void
7156remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7157{
7158 int i = index_to_remove+1;
7159 int last_index = sal->nelts-1;
7160
7161 for (;i <= last_index; ++i)
7162 sal->sals[i-1] = sal->sals[i];
7163
7164 --(sal->nelts);
7165}
7166
6c95b8df
PA
7167/* If appropriate, obtains all sals that correspond to the same file
7168 and line as SAL, in all program spaces. Users debugging with IDEs,
7169 will want to set a breakpoint at foo.c:line, and not really care
7170 about program spaces. This is done only if SAL does not have
7171 explicit PC and has line and file information. If we got just a
7172 single expanded sal, return the original.
ed0616c6 7173
6c95b8df
PA
7174 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7175 which the name of enclosing function is different from SAL. This
7176 makes sure that if we have breakpoint originally set in template
7177 instantiation, say foo<int>(), we won't expand SAL to locations at
7178 the same line in all existing instantiations of 'foo'. */
ed0616c6 7179
2c0b251b 7180static struct symtabs_and_lines
ed0616c6
VP
7181expand_line_sal_maybe (struct symtab_and_line sal)
7182{
7183 struct symtabs_and_lines expanded;
7184 CORE_ADDR original_pc = sal.pc;
7185 char *original_function = NULL;
7186 int found;
7187 int i;
6c95b8df 7188 struct cleanup *old_chain;
ed0616c6
VP
7189
7190 /* If we have explicit pc, don't expand.
7191 If we have no line number, we can't expand. */
7192 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7193 {
7194 expanded.nelts = 1;
7195 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7196 expanded.sals[0] = sal;
7197 return expanded;
7198 }
7199
7200 sal.pc = 0;
6c95b8df
PA
7201
7202 old_chain = save_current_space_and_thread ();
7203
7204 switch_to_program_space_and_thread (sal.pspace);
7205
ed0616c6 7206 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7207
7208 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7209 expanded = expand_line_sal (sal);
6c95b8df 7210
ed0616c6
VP
7211 if (expanded.nelts == 1)
7212 {
3dba1c98
JB
7213 /* We had one sal, we got one sal. Return that sal, adjusting it
7214 past the function prologue if necessary. */
ed0616c6
VP
7215 xfree (expanded.sals);
7216 expanded.nelts = 1;
7217 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7218 sal.pc = original_pc;
7219 expanded.sals[0] = sal;
3dba1c98 7220 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7221 do_cleanups (old_chain);
ed0616c6
VP
7222 return expanded;
7223 }
7224
7225 if (!sal.explicit_line)
7226 {
7227 CORE_ADDR func_addr, func_end;
7228 for (i = 0; i < expanded.nelts; ++i)
7229 {
7230 CORE_ADDR pc = expanded.sals[i].pc;
7231 char *this_function;
6c95b8df
PA
7232
7233 /* We need to switch threads as well since we're about to
7234 read memory. */
7235 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7236
ed0616c6
VP
7237 if (find_pc_partial_function (pc, &this_function,
7238 &func_addr, &func_end))
7239 {
059fb39f
PM
7240 if (this_function
7241 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7242 {
7243 remove_sal (&expanded, i);
7244 --i;
7245 }
ed0616c6
VP
7246 }
7247 }
7248 }
059acae7
UW
7249
7250 /* Skip the function prologue if necessary. */
7251 for (i = 0; i < expanded.nelts; ++i)
7252 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7253
6c95b8df
PA
7254 do_cleanups (old_chain);
7255
ed0616c6
VP
7256 if (expanded.nelts <= 1)
7257 {
e5dd4106 7258 /* This is an ugly workaround. If we get zero expanded sals
4a64f543
MS
7259 then something is really wrong. Fix that by returning the
7260 original sal. */
7261
ed0616c6
VP
7262 xfree (expanded.sals);
7263 expanded.nelts = 1;
7264 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7265 sal.pc = original_pc;
7266 expanded.sals[0] = sal;
7267 return expanded;
7268 }
7269
7270 if (original_pc)
7271 {
7272 found = 0;
7273 for (i = 0; i < expanded.nelts; ++i)
7274 if (expanded.sals[i].pc == original_pc)
7275 {
7276 found = 1;
7277 break;
7278 }
7279 gdb_assert (found);
7280 }
7281
7282 return expanded;
7283}
7284
018d34a4
VP
7285/* Add SALS.nelts breakpoints to the breakpoint table. For each
7286 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7287 value. COND_STRING, if not NULL, specified the condition to be
7288 used for all breakpoints. Essentially the only case where
7289 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7290 function. In that case, it's still not possible to specify
7291 separate conditions for different overloaded functions, so
7292 we take just a single condition string.
7293
c3f6f71d 7294 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7295 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7296 array contents). If the function fails (error() is called), the
7297 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7298 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7299
7300static void
8cdf0e15 7301create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2
TT
7302 struct symtabs_and_lines sals,
7303 struct linespec_result *canonical,
8cdf0e15
VP
7304 char *cond_string,
7305 enum bptype type, enum bpdisp disposition,
7306 int thread, int task, int ignore_count,
7307 struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7308 int enabled, int internal)
c906108c 7309{
018d34a4 7310 int i;
cc59ec59 7311
018d34a4 7312 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7313 {
ed0616c6
VP
7314 struct symtabs_and_lines expanded =
7315 expand_line_sal_maybe (sals.sals[i]);
0d381245 7316
7efd8fc2 7317 create_breakpoint_sal (gdbarch, expanded, canonical->canonical[i],
8cdf0e15 7318 cond_string, type, disposition,
84f4c1fe 7319 thread, task, ignore_count, ops,
56435ebe
TT
7320 from_tty, enabled, internal,
7321 canonical->special_display);
c3f6f71d 7322 }
c3f6f71d 7323}
c906108c 7324
9998af43 7325/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7326 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7327 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7328 address strings. ADDRESS points to the end of the SAL.
7329
7330 The array and the line spec strings are allocated on the heap, it is
7331 the caller's responsibility to free them. */
c906108c 7332
b9362cc7 7333static void
c3f6f71d
JM
7334parse_breakpoint_sals (char **address,
7335 struct symtabs_and_lines *sals,
58438ac1 7336 struct linespec_result *canonical)
c3f6f71d
JM
7337{
7338 char *addr_start = *address;
cc59ec59 7339
c3f6f71d 7340 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7341 breakpoint. */
c3f6f71d
JM
7342 if ((*address) == NULL
7343 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c
SS
7344 {
7345 if (default_breakpoint_valid)
7346 {
c3f6f71d 7347 struct symtab_and_line sal;
cc59ec59 7348
4a64f543 7349 init_sal (&sal); /* Initialize to zeroes. */
c3f6f71d 7350 sals->sals = (struct symtab_and_line *)
c906108c
SS
7351 xmalloc (sizeof (struct symtab_and_line));
7352 sal.pc = default_breakpoint_address;
7353 sal.line = default_breakpoint_line;
7354 sal.symtab = default_breakpoint_symtab;
6c95b8df 7355 sal.pspace = default_breakpoint_pspace;
c5aa993b 7356 sal.section = find_pc_overlay (sal.pc);
00903456 7357
4a64f543
MS
7358 /* "break" without arguments is equivalent to "break *PC"
7359 where PC is the default_breakpoint_address. So make sure
7360 to set sal.explicit_pc to prevent GDB from trying to
7361 expand the list of sals to include all other instances
7362 with the same symtab and line. */
00903456
JK
7363 sal.explicit_pc = 1;
7364
c3f6f71d
JM
7365 sals->sals[0] = sal;
7366 sals->nelts = 1;
c906108c
SS
7367 }
7368 else
8a3fe4f8 7369 error (_("No default breakpoint address now."));
c906108c
SS
7370 }
7371 else
7372 {
c906108c 7373 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7374 current_source_symtab (which is decode_line_1's default).
7375 This should produce the results we want almost all of the
7376 time while leaving default_breakpoint_* alone.
7377
1aeae86e
AF
7378 ObjC: However, don't match an Objective-C method name which
7379 may have a '+' or '-' succeeded by a '[' */
0378c332 7380
c214a6fd 7381 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 7382
c906108c 7383 if (default_breakpoint_valid
0378c332 7384 && (!cursal.symtab
1aeae86e
AF
7385 || ((strchr ("+-", (*address)[0]) != NULL)
7386 && ((*address)[1] != '['))))
c3f6f71d 7387 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
58438ac1 7388 default_breakpoint_line, canonical);
c906108c 7389 else
0101ce28 7390 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
58438ac1 7391 canonical);
c906108c 7392 }
4a64f543 7393 /* For any SAL that didn't have a canonical string, fill one in. */
7efd8fc2 7394 if (sals->nelts > 0 && canonical->canonical == NULL)
38a714bb 7395 canonical->canonical = xcalloc (sals->nelts, sizeof (char *));
c3f6f71d 7396 if (addr_start != (*address))
c906108c 7397 {
c3f6f71d 7398 int i;
cc59ec59 7399
c3f6f71d 7400 for (i = 0; i < sals->nelts; i++)
c906108c 7401 {
4a64f543 7402 /* Add the string if not present. */
7efd8fc2
TT
7403 if (canonical->canonical[i] == NULL)
7404 canonical->canonical[i] = savestring (addr_start,
7405 (*address) - addr_start);
c906108c
SS
7406 }
7407 }
c3f6f71d 7408}
c906108c 7409
c906108c 7410
c3f6f71d 7411/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7412 inserted as a breakpoint. If it can't throw an error. */
c906108c 7413
b9362cc7 7414static void
23e7acfb 7415breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7416{
7417 int i;
cc59ec59 7418
c3f6f71d 7419 for (i = 0; i < sals->nelts; i++)
ee53e872 7420 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7421}
7422
7a697b8d
SS
7423/* Fast tracepoints may have restrictions on valid locations. For
7424 instance, a fast tracepoint using a jump instead of a trap will
7425 likely have to overwrite more bytes than a trap would, and so can
7426 only be placed where the instruction is longer than the jump, or a
7427 multi-instruction sequence does not have a jump into the middle of
7428 it, etc. */
7429
7430static void
7431check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7432 struct symtabs_and_lines *sals)
7433{
7434 int i, rslt;
7435 struct symtab_and_line *sal;
7436 char *msg;
7437 struct cleanup *old_chain;
7438
7439 for (i = 0; i < sals->nelts; i++)
7440 {
7441 sal = &sals->sals[i];
7442
7443 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7444 NULL, &msg);
7445 old_chain = make_cleanup (xfree, msg);
7446
7447 if (!rslt)
7448 error (_("May not have a fast tracepoint at 0x%s%s"),
7449 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7450
7451 do_cleanups (old_chain);
7452 }
7453}
7454
018d34a4
VP
7455/* Given TOK, a string specification of condition and thread, as
7456 accepted by the 'break' command, extract the condition
7457 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7458 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7459 If no condition is found, *COND_STRING is set to NULL.
7460 If no thread is found, *THREAD is set to -1. */
7461static void
7462find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7463 char **cond_string, int *thread, int *task)
018d34a4
VP
7464{
7465 *cond_string = NULL;
7466 *thread = -1;
7467 while (tok && *tok)
7468 {
7469 char *end_tok;
7470 int toklen;
7471 char *cond_start = NULL;
7472 char *cond_end = NULL;
cc59ec59 7473
e9cafbcc 7474 tok = skip_spaces (tok);
018d34a4 7475
e9cafbcc 7476 end_tok = skip_to_space (tok);
018d34a4
VP
7477
7478 toklen = end_tok - tok;
7479
7480 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7481 {
f7545552
TT
7482 struct expression *expr;
7483
018d34a4 7484 tok = cond_start = end_tok + 1;
f7545552
TT
7485 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7486 xfree (expr);
018d34a4
VP
7487 cond_end = tok;
7488 *cond_string = savestring (cond_start,
7489 cond_end - cond_start);
7490 }
7491 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7492 {
7493 char *tmptok;
7494
7495 tok = end_tok + 1;
7496 tmptok = tok;
7497 *thread = strtol (tok, &tok, 0);
7498 if (tok == tmptok)
7499 error (_("Junk after thread keyword."));
7500 if (!valid_thread_id (*thread))
7501 error (_("Unknown thread %d."), *thread);
7502 }
4a306c9a
JB
7503 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7504 {
7505 char *tmptok;
7506
7507 tok = end_tok + 1;
7508 tmptok = tok;
7509 *task = strtol (tok, &tok, 0);
7510 if (tok == tmptok)
7511 error (_("Junk after task keyword."));
7512 if (!valid_task_id (*task))
b6199126 7513 error (_("Unknown task %d."), *task);
4a306c9a 7514 }
018d34a4
VP
7515 else
7516 error (_("Junk at end of arguments."));
7517 }
7518}
7519
0fb4aa4b
PA
7520/* Decode a static tracepoint marker spec. */
7521
7522static struct symtabs_and_lines
7523decode_static_tracepoint_spec (char **arg_p)
7524{
7525 VEC(static_tracepoint_marker_p) *markers = NULL;
7526 struct symtabs_and_lines sals;
7527 struct symtab_and_line sal;
7528 struct symbol *sym;
7529 struct cleanup *old_chain;
7530 char *p = &(*arg_p)[3];
7531 char *endp;
7532 char *marker_str;
7533 int i;
7534
e9cafbcc 7535 p = skip_spaces (p);
0fb4aa4b 7536
e9cafbcc 7537 endp = skip_to_space (p);
0fb4aa4b
PA
7538
7539 marker_str = savestring (p, endp - p);
7540 old_chain = make_cleanup (xfree, marker_str);
7541
7542 markers = target_static_tracepoint_markers_by_strid (marker_str);
7543 if (VEC_empty(static_tracepoint_marker_p, markers))
7544 error (_("No known static tracepoint marker named %s"), marker_str);
7545
7546 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7547 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7548
7549 for (i = 0; i < sals.nelts; i++)
7550 {
7551 struct static_tracepoint_marker *marker;
7552
7553 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7554
7555 init_sal (&sals.sals[i]);
7556
7557 sals.sals[i] = find_pc_line (marker->address, 0);
7558 sals.sals[i].pc = marker->address;
7559
7560 release_static_tracepoint_marker (marker);
7561 }
7562
7563 do_cleanups (old_chain);
7564
7565 *arg_p = endp;
7566 return sals;
7567}
7568
fd9b8c24
PA
7569/* Set a breakpoint. This function is shared between CLI and MI
7570 functions for setting a breakpoint. This function has two major
7571 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7572 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7573 breakpoint location, address and thread. Otherwise, ARG is just
7574 the location of breakpoint, with condition and thread specified by
7575 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7576 the breakpoint number will be allocated from the internal
7577 breakpoint count. Returns true if any breakpoint was created;
7578 false otherwise. */
0101ce28 7579
8cdf0e15
VP
7580int
7581create_breakpoint (struct gdbarch *gdbarch,
7582 char *arg, char *cond_string, int thread,
7583 int parse_condition_and_thread,
0fb4aa4b 7584 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7585 int ignore_count,
7586 enum auto_boolean pending_break_support,
7587 struct breakpoint_ops *ops,
84f4c1fe 7588 int from_tty, int enabled, int internal)
c3f6f71d 7589{
b78a6381 7590 volatile struct gdb_exception e;
c3f6f71d 7591 struct symtabs_and_lines sals;
0101ce28 7592 struct symtab_and_line pending_sal;
0101ce28 7593 char *copy_arg;
c3f6f71d 7594 char *addr_start = arg;
7efd8fc2 7595 struct linespec_result canonical;
c3f6f71d 7596 struct cleanup *old_chain;
80c99de1 7597 struct cleanup *bkpt_chain = NULL;
05ff989b 7598 int i;
0101ce28 7599 int pending = 0;
4a306c9a 7600 int task = 0;
86b17b60 7601 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7602
348d480f
PA
7603 gdb_assert (ops != NULL);
7604
c3f6f71d
JM
7605 sals.sals = NULL;
7606 sals.nelts = 0;
7efd8fc2 7607 init_linespec_result (&canonical);
c3f6f71d 7608
0fb4aa4b
PA
7609 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7610 {
7611 int i;
7612
7613 sals = decode_static_tracepoint_spec (&arg);
7614
7615 copy_arg = savestring (addr_start, arg - addr_start);
38a714bb 7616 canonical.canonical = xcalloc (sals.nelts, sizeof (char *));
0fb4aa4b 7617 for (i = 0; i < sals.nelts; i++)
7efd8fc2 7618 canonical.canonical[i] = xstrdup (copy_arg);
0fb4aa4b
PA
7619 goto done;
7620 }
7621
b78a6381
TT
7622 TRY_CATCH (e, RETURN_MASK_ALL)
7623 {
58438ac1 7624 parse_breakpoint_sals (&arg, &sals, &canonical);
b78a6381 7625 }
0101ce28
JJ
7626
7627 /* If caller is interested in rc value from parse, set value. */
05ff989b 7628 switch (e.reason)
0101ce28 7629 {
05ff989b 7630 case RETURN_QUIT:
98deb0da 7631 throw_exception (e);
05ff989b
AC
7632 case RETURN_ERROR:
7633 switch (e.error)
0101ce28 7634 {
05ff989b 7635 case NOT_FOUND_ERROR:
0101ce28 7636
05ff989b
AC
7637 /* If pending breakpoint support is turned off, throw
7638 error. */
fa8d40ab
JJ
7639
7640 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7641 throw_exception (e);
7642
7643 exception_print (gdb_stderr, e);
fa8d40ab 7644
05ff989b
AC
7645 /* If pending breakpoint support is auto query and the user
7646 selects no, then simply return the error code. */
059fb39f 7647 if (pending_break_support == AUTO_BOOLEAN_AUTO
3e43a32a
MS
7648 && !nquery (_("Make breakpoint pending on "
7649 "future shared library load? ")))
fd9b8c24 7650 return 0;
fa8d40ab 7651
05ff989b
AC
7652 /* At this point, either the user was queried about setting
7653 a pending breakpoint and selected yes, or pending
7654 breakpoint behavior is on and thus a pending breakpoint
7655 is defaulted on behalf of the user. */
0101ce28 7656 copy_arg = xstrdup (addr_start);
7efd8fc2 7657 canonical.canonical = &copy_arg;
0101ce28
JJ
7658 sals.nelts = 1;
7659 sals.sals = &pending_sal;
7660 pending_sal.pc = 0;
7661 pending = 1;
05ff989b
AC
7662 break;
7663 default:
98deb0da 7664 throw_exception (e);
0101ce28 7665 }
2abae994 7666 break;
05ff989b
AC
7667 default:
7668 if (!sals.nelts)
fd9b8c24 7669 return 0;
0101ce28 7670 }
c3f6f71d 7671
0fb4aa4b
PA
7672 done:
7673
4a64f543 7674 /* Create a chain of things that always need to be cleaned up. */
c3f6f71d
JM
7675 old_chain = make_cleanup (null_cleanup, 0);
7676
0101ce28
JJ
7677 if (!pending)
7678 {
7679 /* Make sure that all storage allocated to SALS gets freed. */
7680 make_cleanup (xfree, sals.sals);
7681
7efd8fc2
TT
7682 /* Cleanup the canonical array but not its contents. */
7683 make_cleanup (xfree, canonical.canonical);
0101ce28 7684 }
c3f6f71d 7685
c3f6f71d
JM
7686 /* ----------------------------- SNIP -----------------------------
7687 Anything added to the cleanup chain beyond this point is assumed
7688 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7689 then the memory is not reclaimed. */
7690 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 7691
7efd8fc2 7692 /* Mark the contents of the canonical for cleanup. These go on
80c99de1 7693 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
7694 for (i = 0; i < sals.nelts; i++)
7695 {
7efd8fc2
TT
7696 if (canonical.canonical[i] != NULL)
7697 make_cleanup (xfree, canonical.canonical[i]);
c3f6f71d
JM
7698 }
7699
7700 /* Resolve all line numbers to PC's and verify that the addresses
7701 are ok for the target. */
0101ce28 7702 if (!pending)
23e7acfb 7703 breakpoint_sals_to_pc (&sals);
c3f6f71d 7704
7a697b8d
SS
7705 /* Fast tracepoints may have additional restrictions on location. */
7706 if (type_wanted == bp_fast_tracepoint)
7707 check_fast_tracepoint_sals (gdbarch, &sals);
7708
c3f6f71d
JM
7709 /* Verify that condition can be parsed, before setting any
7710 breakpoints. Allocate a separate condition expression for each
4a64f543 7711 breakpoint. */
0101ce28 7712 if (!pending)
c3f6f71d 7713 {
2f069f6f 7714 if (parse_condition_and_thread)
72b2ff0e
VP
7715 {
7716 /* Here we only parse 'arg' to separate condition
7717 from thread number, so parsing in context of first
7718 sal is OK. When setting the breakpoint we'll
7719 re-parse it in context of each sal. */
7720 cond_string = NULL;
7721 thread = -1;
4a306c9a
JB
7722 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
7723 &thread, &task);
72b2ff0e
VP
7724 if (cond_string)
7725 make_cleanup (xfree, cond_string);
7726 }
2f069f6f 7727 else
72b2ff0e
VP
7728 {
7729 /* Create a private copy of condition string. */
7730 if (cond_string)
7731 {
7732 cond_string = xstrdup (cond_string);
7733 make_cleanup (xfree, cond_string);
7734 }
7735 }
0fb4aa4b
PA
7736
7737 /* If the user is creating a static tracepoint by marker id
7738 (strace -m MARKER_ID), then store the sals index, so that
7739 breakpoint_re_set can try to match up which of the newly
7740 found markers corresponds to this one, and, don't try to
7741 expand multiple locations for each sal, given than SALS
7742 already should contain all sals for MARKER_ID. */
7743 if (type_wanted == bp_static_tracepoint
7efd8fc2 7744 && is_marker_spec (canonical.canonical[0]))
0fb4aa4b
PA
7745 {
7746 int i;
7747
7748 for (i = 0; i < sals.nelts; ++i)
7749 {
7750 struct symtabs_and_lines expanded;
7751 struct breakpoint *tp;
7752 struct cleanup *old_chain;
7753
7754 expanded.nelts = 1;
7755 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7756 expanded.sals[0] = sals.sals[i];
7757 old_chain = make_cleanup (xfree, expanded.sals);
7758
7efd8fc2 7759 create_breakpoint_sal (gdbarch, expanded, canonical.canonical[i],
0fb4aa4b
PA
7760 cond_string, type_wanted,
7761 tempflag ? disp_del : disp_donttouch,
7762 thread, task, ignore_count, ops,
56435ebe
TT
7763 from_tty, enabled, internal,
7764 canonical.special_display);
0fb4aa4b
PA
7765
7766 do_cleanups (old_chain);
7767
7768 /* Get the tracepoint we just created. */
84f4c1fe
PM
7769 if (internal)
7770 tp = get_breakpoint (internal_breakpoint_number);
7771 else
7772 tp = get_breakpoint (breakpoint_count);
0fb4aa4b
PA
7773 gdb_assert (tp != NULL);
7774
7775 /* Given that its possible to have multiple markers with
7776 the same string id, if the user is creating a static
7777 tracepoint by marker id ("strace -m MARKER_ID"), then
7778 store the sals index, so that breakpoint_re_set can
7779 try to match up which of the newly found markers
7780 corresponds to this one */
7781 tp->static_trace_marker_id_idx = i;
7782 }
7783 }
7784 else
7efd8fc2 7785 create_breakpoints_sal (gdbarch, sals, &canonical, cond_string,
3e43a32a
MS
7786 type_wanted,
7787 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 7788 thread, task, ignore_count, ops, from_tty,
84f4c1fe 7789 enabled, internal);
c906108c 7790 }
0101ce28
JJ
7791 else
7792 {
0101ce28
JJ
7793 struct breakpoint *b;
7794
0101ce28
JJ
7795 make_cleanup (xfree, copy_arg);
7796
348d480f 7797 b = set_raw_breakpoint_without_location (gdbarch, type_wanted, ops);
84f4c1fe 7798 set_breakpoint_number (internal, b);
72b2ff0e 7799 b->thread = -1;
7efd8fc2 7800 b->addr_string = canonical.canonical[0];
72b2ff0e 7801 b->cond_string = NULL;
0101ce28 7802 b->ignore_count = ignore_count;
0101ce28 7803 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 7804 b->condition_not_parsed = 1;
41447f92 7805 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 7806 b->pspace = current_program_space;
84f4c1fe 7807 b->py_bp_object = NULL;
74960c60 7808
6c95b8df 7809 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7810 && (b->type == bp_breakpoint
7811 || b->type == bp_hardware_breakpoint))
7812 b->enable_state = bp_startup_disabled;
7813
8d3788bd 7814 if (!internal)
84f4c1fe
PM
7815 /* Do not mention breakpoints with a negative number,
7816 but do notify observers. */
8d3788bd
VP
7817 mention (b);
7818 observer_notify_breakpoint_created (b);
0101ce28
JJ
7819 }
7820
c3f6f71d 7821 if (sals.nelts > 1)
95a42b64 7822 {
3e43a32a
MS
7823 warning (_("Multiple breakpoints were set.\nUse the "
7824 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 7825 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
7826 }
7827
80c99de1
PA
7828 /* That's it. Discard the cleanups for data inserted into the
7829 breakpoint. */
7830 discard_cleanups (bkpt_chain);
7831 /* But cleanup everything else. */
c3f6f71d 7832 do_cleanups (old_chain);
217dc9e2 7833
80c99de1 7834 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 7835 update_global_location_list (1);
fd9b8c24
PA
7836
7837 return 1;
c3f6f71d 7838}
c906108c 7839
348d480f 7840/* Set a breakpoint.
72b2ff0e
VP
7841 ARG is a string describing breakpoint address,
7842 condition, and thread.
7843 FLAG specifies if a breakpoint is hardware on,
7844 and if breakpoint is temporary, using BP_HARDWARE_FLAG
7845 and BP_TEMPFLAG. */
348d480f 7846
98deb0da 7847static void
72b2ff0e 7848break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 7849{
72b2ff0e 7850 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
7851 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
7852 ? bp_hardware_breakpoint
7853 : bp_breakpoint);
c3f6f71d 7854
8cdf0e15
VP
7855 create_breakpoint (get_current_arch (),
7856 arg,
7857 NULL, 0, 1 /* parse arg */,
0fb4aa4b 7858 tempflag, type_wanted,
8cdf0e15
VP
7859 0 /* Ignore count */,
7860 pending_break_support,
348d480f 7861 &bkpt_breakpoint_ops,
8cdf0e15 7862 from_tty,
84f4c1fe
PM
7863 1 /* enabled */,
7864 0 /* internal */);
c906108c
SS
7865}
7866
c906108c
SS
7867/* Helper function for break_command_1 and disassemble_command. */
7868
7869void
fba45db2 7870resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
7871{
7872 CORE_ADDR pc;
7873
7874 if (sal->pc == 0 && sal->symtab != NULL)
7875 {
7876 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 7877 error (_("No line %d in file \"%s\"."),
c906108c
SS
7878 sal->line, sal->symtab->filename);
7879 sal->pc = pc;
6a048695 7880
4a64f543
MS
7881 /* If this SAL corresponds to a breakpoint inserted using a line
7882 number, then skip the function prologue if necessary. */
6a048695 7883 if (sal->explicit_line)
059acae7 7884 skip_prologue_sal (sal);
c906108c
SS
7885 }
7886
7887 if (sal->section == 0 && sal->symtab != NULL)
7888 {
7889 struct blockvector *bv;
c5aa993b
JM
7890 struct block *b;
7891 struct symbol *sym;
c906108c 7892
801e3a5b 7893 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
7894 if (bv != NULL)
7895 {
7f0df278 7896 sym = block_linkage_function (b);
c906108c
SS
7897 if (sym != NULL)
7898 {
7899 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 7900 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
7901 }
7902 else
7903 {
4a64f543
MS
7904 /* It really is worthwhile to have the section, so we'll
7905 just have to look harder. This case can be executed
7906 if we have line numbers but no functions (as can
7907 happen in assembly source). */
c906108c 7908
c5aa993b 7909 struct minimal_symbol *msym;
6c95b8df
PA
7910 struct cleanup *old_chain = save_current_space_and_thread ();
7911
7912 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
7913
7914 msym = lookup_minimal_symbol_by_pc (sal->pc);
7915 if (msym)
714835d5 7916 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
7917
7918 do_cleanups (old_chain);
c906108c
SS
7919 }
7920 }
7921 }
7922}
7923
7924void
fba45db2 7925break_command (char *arg, int from_tty)
c906108c 7926{
db107f19 7927 break_command_1 (arg, 0, from_tty);
c906108c
SS
7928}
7929
c906108c 7930void
fba45db2 7931tbreak_command (char *arg, int from_tty)
c906108c 7932{
db107f19 7933 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
7934}
7935
c906108c 7936static void
fba45db2 7937hbreak_command (char *arg, int from_tty)
c906108c 7938{
db107f19 7939 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
7940}
7941
7942static void
fba45db2 7943thbreak_command (char *arg, int from_tty)
c906108c 7944{
db107f19 7945 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
7946}
7947
7948static void
fba45db2 7949stop_command (char *arg, int from_tty)
c906108c 7950{
a3f17187 7951 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 7952Usage: stop in <function | address>\n\
a3f17187 7953 stop at <line>\n"));
c906108c
SS
7954}
7955
7956static void
fba45db2 7957stopin_command (char *arg, int from_tty)
c906108c
SS
7958{
7959 int badInput = 0;
7960
c5aa993b 7961 if (arg == (char *) NULL)
c906108c
SS
7962 badInput = 1;
7963 else if (*arg != '*')
7964 {
7965 char *argptr = arg;
7966 int hasColon = 0;
7967
4a64f543 7968 /* Look for a ':'. If this is a line number specification, then
53a5351d 7969 say it is bad, otherwise, it should be an address or
4a64f543 7970 function/method name. */
c906108c 7971 while (*argptr && !hasColon)
c5aa993b
JM
7972 {
7973 hasColon = (*argptr == ':');
7974 argptr++;
7975 }
c906108c
SS
7976
7977 if (hasColon)
c5aa993b 7978 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 7979 else
c5aa993b 7980 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
7981 }
7982
7983 if (badInput)
a3f17187 7984 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 7985 else
db107f19 7986 break_command_1 (arg, 0, from_tty);
c906108c
SS
7987}
7988
7989static void
fba45db2 7990stopat_command (char *arg, int from_tty)
c906108c
SS
7991{
7992 int badInput = 0;
7993
c5aa993b 7994 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
7995 badInput = 1;
7996 else
7997 {
7998 char *argptr = arg;
7999 int hasColon = 0;
8000
4a64f543
MS
8001 /* Look for a ':'. If there is a '::' then get out, otherwise
8002 it is probably a line number. */
c906108c 8003 while (*argptr && !hasColon)
c5aa993b
JM
8004 {
8005 hasColon = (*argptr == ':');
8006 argptr++;
8007 }
c906108c
SS
8008
8009 if (hasColon)
c5aa993b 8010 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8011 else
c5aa993b 8012 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8013 }
8014
8015 if (badInput)
a3f17187 8016 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8017 else
db107f19 8018 break_command_1 (arg, 0, from_tty);
c906108c
SS
8019}
8020
f1310107
TJB
8021/* Implement the "breakpoint_hit" breakpoint_ops method for
8022 ranged breakpoints. */
8023
8024static int
8025breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8026 struct address_space *aspace,
8027 CORE_ADDR bp_addr)
8028{
8029 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8030 bl->length, aspace, bp_addr);
8031}
8032
8033/* Implement the "resources_needed" breakpoint_ops method for
8034 ranged breakpoints. */
8035
8036static int
8037resources_needed_ranged_breakpoint (const struct bp_location *bl)
8038{
8039 return target_ranged_break_num_registers ();
8040}
8041
8042/* Implement the "print_it" breakpoint_ops method for
8043 ranged breakpoints. */
8044
8045static enum print_stop_action
348d480f 8046print_it_ranged_breakpoint (bpstat bs)
f1310107 8047{
348d480f 8048 struct breakpoint *b = bs->breakpoint_at;
f1310107
TJB
8049 struct bp_location *bl = b->loc;
8050
8051 gdb_assert (b->type == bp_hardware_breakpoint);
8052
8053 /* Ranged breakpoints have only one location. */
8054 gdb_assert (bl && bl->next == NULL);
8055
8056 annotate_breakpoint (b->number);
8057 if (b->disposition == disp_del)
8058 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8059 else
8060 ui_out_text (uiout, "\nRanged breakpoint ");
8061 if (ui_out_is_mi_like_p (uiout))
8062 {
8063 ui_out_field_string (uiout, "reason",
8064 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8065 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8066 }
8067 ui_out_field_int (uiout, "bkptno", b->number);
8068 ui_out_text (uiout, ", ");
8069
8070 return PRINT_SRC_AND_LOC;
8071}
8072
8073/* Implement the "print_one" breakpoint_ops method for
8074 ranged breakpoints. */
8075
8076static void
8077print_one_ranged_breakpoint (struct breakpoint *b,
8078 struct bp_location **last_loc)
8079{
8080 struct bp_location *bl = b->loc;
8081 struct value_print_options opts;
8082
8083 /* Ranged breakpoints have only one location. */
8084 gdb_assert (bl && bl->next == NULL);
8085
8086 get_user_print_options (&opts);
8087
8088 if (opts.addressprint)
8089 /* We don't print the address range here, it will be printed later
8090 by print_one_detail_ranged_breakpoint. */
8091 ui_out_field_skip (uiout, "addr");
8092 annotate_field (5);
8093 print_breakpoint_location (b, bl);
8094 *last_loc = bl;
8095}
8096
8097/* Implement the "print_one_detail" breakpoint_ops method for
8098 ranged breakpoints. */
8099
8100static void
8101print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8102 struct ui_out *uiout)
8103{
8104 CORE_ADDR address_start, address_end;
8105 struct bp_location *bl = b->loc;
8106 struct ui_stream *stb = ui_out_stream_new (uiout);
8107 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8108
8109 gdb_assert (bl);
8110
8111 address_start = bl->address;
8112 address_end = address_start + bl->length - 1;
8113
8114 ui_out_text (uiout, "\taddress range: ");
8115 fprintf_unfiltered (stb->stream, "[%s, %s]",
8116 print_core_address (bl->gdbarch, address_start),
8117 print_core_address (bl->gdbarch, address_end));
8118 ui_out_field_stream (uiout, "addr", stb);
8119 ui_out_text (uiout, "\n");
8120
8121 do_cleanups (cleanup);
8122}
8123
8124/* Implement the "print_mention" breakpoint_ops method for
8125 ranged breakpoints. */
8126
8127static void
8128print_mention_ranged_breakpoint (struct breakpoint *b)
8129{
8130 struct bp_location *bl = b->loc;
8131
8132 gdb_assert (bl);
8133 gdb_assert (b->type == bp_hardware_breakpoint);
8134
8135 if (ui_out_is_mi_like_p (uiout))
8136 return;
8137
8138 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8139 b->number, paddress (bl->gdbarch, bl->address),
8140 paddress (bl->gdbarch, bl->address + bl->length - 1));
8141}
8142
8143/* Implement the "print_recreate" breakpoint_ops method for
8144 ranged breakpoints. */
8145
8146static void
8147print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8148{
8149 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8150 b->addr_string_range_end);
8151}
8152
8153/* The breakpoint_ops structure to be used in ranged breakpoints. */
8154
2060206e 8155static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
8156
8157/* Find the address where the end of the breakpoint range should be
8158 placed, given the SAL of the end of the range. This is so that if
8159 the user provides a line number, the end of the range is set to the
8160 last instruction of the given line. */
8161
8162static CORE_ADDR
8163find_breakpoint_range_end (struct symtab_and_line sal)
8164{
8165 CORE_ADDR end;
8166
8167 /* If the user provided a PC value, use it. Otherwise,
8168 find the address of the end of the given location. */
8169 if (sal.explicit_pc)
8170 end = sal.pc;
8171 else
8172 {
8173 int ret;
8174 CORE_ADDR start;
8175
8176 ret = find_line_pc_range (sal, &start, &end);
8177 if (!ret)
8178 error (_("Could not find location of the end of the range."));
8179
8180 /* find_line_pc_range returns the start of the next line. */
8181 end--;
8182 }
8183
8184 return end;
8185}
8186
8187/* Implement the "break-range" CLI command. */
8188
8189static void
8190break_range_command (char *arg, int from_tty)
8191{
8192 char *arg_start, *addr_string_start, *addr_string_end;
8193 struct linespec_result canonical_start, canonical_end;
8194 int bp_count, can_use_bp, length;
8195 CORE_ADDR end;
8196 struct breakpoint *b;
8197 struct symtab_and_line sal_start, sal_end;
8198 struct symtabs_and_lines sals_start, sals_end;
8199 struct cleanup *cleanup_bkpt;
8200
8201 /* We don't support software ranged breakpoints. */
8202 if (target_ranged_break_num_registers () < 0)
8203 error (_("This target does not support hardware ranged breakpoints."));
8204
8205 bp_count = hw_breakpoint_used_count ();
8206 bp_count += target_ranged_break_num_registers ();
8207 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8208 bp_count, 0);
8209 if (can_use_bp < 0)
8210 error (_("Hardware breakpoints used exceeds limit."));
8211
8212 if (arg == NULL || arg[0] == '\0')
8213 error(_("No address range specified."));
8214
8215 sals_start.sals = NULL;
8216 sals_start.nelts = 0;
8217 init_linespec_result (&canonical_start);
8218
8219 while (*arg == ' ' || *arg == '\t')
8220 arg++;
8221
58438ac1 8222 parse_breakpoint_sals (&arg, &sals_start, &canonical_start);
f1310107
TJB
8223
8224 sal_start = sals_start.sals[0];
8225 addr_string_start = canonical_start.canonical[0];
8226 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
8227 xfree (sals_start.sals);
8228 xfree (canonical_start.canonical);
8229
8230 if (arg[0] != ',')
8231 error (_("Too few arguments."));
8232 else if (sals_start.nelts == 0)
8233 error (_("Could not find location of the beginning of the range."));
8234 else if (sals_start.nelts != 1)
8235 error (_("Cannot create a ranged breakpoint with multiple locations."));
8236
8237 resolve_sal_pc (&sal_start);
8238
8239 arg++; /* Skip the comma. */
8240 while (*arg == ' ' || *arg == '\t')
8241 arg++;
8242
8243 /* Parse the end location. */
8244
8245 sals_end.sals = NULL;
8246 sals_end.nelts = 0;
8247 init_linespec_result (&canonical_end);
8248 arg_start = arg;
8249
423f41a5 8250 /* We call decode_line_1 directly here instead of using
f1310107
TJB
8251 parse_breakpoint_sals because we need to specify the start location's
8252 symtab and line as the default symtab and line for the end of the
8253 range. This makes it possible to have ranges like "foo.c:27, +14",
8254 where +14 means 14 lines from the start location. */
8255 sals_end = decode_line_1 (&arg, 1, sal_start.symtab, sal_start.line,
58438ac1 8256 &canonical_end);
f1310107
TJB
8257
8258 /* canonical_end can be NULL if it was of the form "*0xdeadbeef". */
8259 if (canonical_end.canonical == NULL)
38a714bb 8260 canonical_end.canonical = xcalloc (1, sizeof (char *));
f1310107
TJB
8261 /* Add the string if not present. */
8262 if (arg_start != arg && canonical_end.canonical[0] == NULL)
8263 canonical_end.canonical[0] = savestring (arg_start, arg - arg_start);
8264
8265 sal_end = sals_end.sals[0];
8266 addr_string_end = canonical_end.canonical[0];
8267 make_cleanup (xfree, addr_string_end);
8268 xfree (sals_end.sals);
8269 xfree (canonical_end.canonical);
8270
8271 if (sals_end.nelts == 0)
8272 error (_("Could not find location of the end of the range."));
8273 else if (sals_end.nelts != 1)
8274 error (_("Cannot create a ranged breakpoint with multiple locations."));
8275
8276 resolve_sal_pc (&sal_end);
8277
8278 end = find_breakpoint_range_end (sal_end);
8279 if (sal_start.pc > end)
177b42fe 8280 error (_("Invalid address range, end precedes start."));
f1310107
TJB
8281
8282 length = end - sal_start.pc + 1;
8283 if (length < 0)
8284 /* Length overflowed. */
8285 error (_("Address range too large."));
8286 else if (length == 1)
8287 {
8288 /* This range is simple enough to be handled by
8289 the `hbreak' command. */
8290 hbreak_command (addr_string_start, 1);
8291
8292 do_cleanups (cleanup_bkpt);
8293
8294 return;
8295 }
8296
8297 /* Now set up the breakpoint. */
8298 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 8299 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
8300 set_breakpoint_count (breakpoint_count + 1);
8301 b->number = breakpoint_count;
8302 b->disposition = disp_donttouch;
8303 b->addr_string = addr_string_start;
8304 b->addr_string_range_end = addr_string_end;
f1310107
TJB
8305 b->loc->length = length;
8306
8307 discard_cleanups (cleanup_bkpt);
8308
8309 mention (b);
8d3788bd 8310 observer_notify_breakpoint_created (b);
f1310107
TJB
8311 update_global_location_list (1);
8312}
8313
4a64f543
MS
8314/* Return non-zero if EXP is verified as constant. Returned zero
8315 means EXP is variable. Also the constant detection may fail for
8316 some constant expressions and in such case still falsely return
8317 zero. */
65d79d4b
SDJ
8318static int
8319watchpoint_exp_is_const (const struct expression *exp)
8320{
8321 int i = exp->nelts;
8322
8323 while (i > 0)
8324 {
8325 int oplenp, argsp;
8326
8327 /* We are only interested in the descriptor of each element. */
8328 operator_length (exp, i, &oplenp, &argsp);
8329 i -= oplenp;
8330
8331 switch (exp->elts[i].opcode)
8332 {
8333 case BINOP_ADD:
8334 case BINOP_SUB:
8335 case BINOP_MUL:
8336 case BINOP_DIV:
8337 case BINOP_REM:
8338 case BINOP_MOD:
8339 case BINOP_LSH:
8340 case BINOP_RSH:
8341 case BINOP_LOGICAL_AND:
8342 case BINOP_LOGICAL_OR:
8343 case BINOP_BITWISE_AND:
8344 case BINOP_BITWISE_IOR:
8345 case BINOP_BITWISE_XOR:
8346 case BINOP_EQUAL:
8347 case BINOP_NOTEQUAL:
8348 case BINOP_LESS:
8349 case BINOP_GTR:
8350 case BINOP_LEQ:
8351 case BINOP_GEQ:
8352 case BINOP_REPEAT:
8353 case BINOP_COMMA:
8354 case BINOP_EXP:
8355 case BINOP_MIN:
8356 case BINOP_MAX:
8357 case BINOP_INTDIV:
8358 case BINOP_CONCAT:
8359 case BINOP_IN:
8360 case BINOP_RANGE:
8361 case TERNOP_COND:
8362 case TERNOP_SLICE:
8363 case TERNOP_SLICE_COUNT:
8364
8365 case OP_LONG:
8366 case OP_DOUBLE:
8367 case OP_DECFLOAT:
8368 case OP_LAST:
8369 case OP_COMPLEX:
8370 case OP_STRING:
8371 case OP_BITSTRING:
8372 case OP_ARRAY:
8373 case OP_TYPE:
8374 case OP_NAME:
8375 case OP_OBJC_NSSTRING:
8376
8377 case UNOP_NEG:
8378 case UNOP_LOGICAL_NOT:
8379 case UNOP_COMPLEMENT:
8380 case UNOP_ADDR:
8381 case UNOP_HIGH:
4a64f543
MS
8382 /* Unary, binary and ternary operators: We have to check
8383 their operands. If they are constant, then so is the
8384 result of that operation. For instance, if A and B are
8385 determined to be constants, then so is "A + B".
8386
8387 UNOP_IND is one exception to the rule above, because the
8388 value of *ADDR is not necessarily a constant, even when
8389 ADDR is. */
65d79d4b
SDJ
8390 break;
8391
8392 case OP_VAR_VALUE:
8393 /* Check whether the associated symbol is a constant.
4a64f543 8394
65d79d4b 8395 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8396 possible that a buggy compiler could mark a variable as
8397 constant even when it is not, and TYPE_CONST would return
8398 true in this case, while SYMBOL_CLASS wouldn't.
8399
8400 We also have to check for function symbols because they
8401 are always constant. */
65d79d4b
SDJ
8402 {
8403 struct symbol *s = exp->elts[i + 2].symbol;
8404
8405 if (SYMBOL_CLASS (s) != LOC_BLOCK
8406 && SYMBOL_CLASS (s) != LOC_CONST
8407 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8408 return 0;
8409 break;
8410 }
8411
8412 /* The default action is to return 0 because we are using
8413 the optimistic approach here: If we don't know something,
8414 then it is not a constant. */
8415 default:
8416 return 0;
8417 }
8418 }
8419
8420 return 1;
8421}
8422
348d480f
PA
8423/* Implement the "re_set" breakpoint_ops method for watchpoints. */
8424
8425static void
8426re_set_watchpoint (struct breakpoint *b)
8427{
8428 /* Watchpoint can be either on expression using entirely global
8429 variables, or it can be on local variables.
8430
8431 Watchpoints of the first kind are never auto-deleted, and even
8432 persist across program restarts. Since they can use variables
8433 from shared libraries, we need to reparse expression as libraries
8434 are loaded and unloaded.
8435
8436 Watchpoints on local variables can also change meaning as result
8437 of solib event. For example, if a watchpoint uses both a local
8438 and a global variables in expression, it's a local watchpoint,
8439 but unloading of a shared library will make the expression
8440 invalid. This is not a very common use case, but we still
8441 re-evaluate expression, to avoid surprises to the user.
8442
8443 Note that for local watchpoints, we re-evaluate it only if
8444 watchpoints frame id is still valid. If it's not, it means the
8445 watchpoint is out of scope and will be deleted soon. In fact,
8446 I'm not sure we'll ever be called in this case.
8447
8448 If a local watchpoint's frame id is still valid, then
8449 b->exp_valid_block is likewise valid, and we can safely use it.
8450
8451 Don't do anything about disabled watchpoints, since they will
8452 be reevaluated again when enabled. */
8453 update_watchpoint (b, 1 /* reparse */);
8454}
8455
77b06cd7
TJB
8456/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8457
8458static int
8459insert_watchpoint (struct bp_location *bl)
8460{
e09342b5
TJB
8461 int length = bl->owner->exact? 1 : bl->length;
8462
8463 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
8464 bl->owner->cond_exp);
77b06cd7
TJB
8465}
8466
8467/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8468
8469static int
8470remove_watchpoint (struct bp_location *bl)
8471{
e09342b5
TJB
8472 int length = bl->owner->exact? 1 : bl->length;
8473
8474 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
8475 bl->owner->cond_exp);
8476}
8477
e09342b5 8478static int
348d480f
PA
8479breakpoint_hit_watchpoint (const struct bp_location *bl,
8480 struct address_space *aspace, CORE_ADDR bp_addr)
e09342b5 8481{
348d480f 8482 struct breakpoint *b = bl->owner;
77b06cd7 8483
348d480f
PA
8484 /* Continuable hardware watchpoints are treated as non-existent if the
8485 reason we stopped wasn't a hardware watchpoint (we didn't stop on
8486 some data address). Otherwise gdb won't stop on a break instruction
8487 in the code (not from a breakpoint) when a hardware watchpoint has
8488 been defined. Also skip watchpoints which we know did not trigger
8489 (did not match the data address). */
8490 if (is_hardware_watchpoint (b)
8491 && b->watchpoint_triggered == watch_triggered_no)
8492 return 0;
9c06b0b4 8493
348d480f 8494 return 1;
9c06b0b4
TJB
8495}
8496
348d480f
PA
8497static void
8498check_status_watchpoint (bpstat bs)
9c06b0b4 8499{
348d480f 8500 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 8501
348d480f 8502 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
8503}
8504
8505/* Implement the "resources_needed" breakpoint_ops method for
348d480f 8506 hardware watchpoints. */
9c06b0b4
TJB
8507
8508static int
348d480f 8509resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 8510{
348d480f
PA
8511 int length = bl->owner->exact? 1 : bl->length;
8512
8513 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
8514}
8515
8516/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 8517 hardware watchpoints. */
9c06b0b4
TJB
8518
8519static int
348d480f 8520works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 8521{
348d480f 8522 return b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
8523}
8524
9c06b0b4 8525static enum print_stop_action
348d480f 8526print_it_watchpoint (bpstat bs)
9c06b0b4 8527{
348d480f
PA
8528 struct cleanup *old_chain;
8529 struct breakpoint *b;
8530 const struct bp_location *bl;
8531 struct ui_stream *stb;
8532 enum print_stop_action result;
8533
8534 gdb_assert (bs->bp_location_at != NULL);
8535
8536 bl = bs->bp_location_at;
8537 b = bs->breakpoint_at;
8538
8539 stb = ui_out_stream_new (uiout);
8540 old_chain = make_cleanup_ui_out_stream_delete (stb);
9c06b0b4
TJB
8541
8542 switch (b->type)
8543 {
348d480f 8544 case bp_watchpoint:
9c06b0b4
TJB
8545 case bp_hardware_watchpoint:
8546 annotate_watchpoint (b->number);
8547 if (ui_out_is_mi_like_p (uiout))
8548 ui_out_field_string
8549 (uiout, "reason",
8550 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
8551 mention (b);
8552 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8553 ui_out_text (uiout, "\nOld value = ");
8554 watchpoint_value_print (bs->old_val, stb->stream);
8555 ui_out_field_stream (uiout, "old", stb);
8556 ui_out_text (uiout, "\nNew value = ");
8557 watchpoint_value_print (b->val, stb->stream);
8558 ui_out_field_stream (uiout, "new", stb);
8559 ui_out_text (uiout, "\n");
8560 /* More than one watchpoint may have been triggered. */
8561 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8562 break;
8563
8564 case bp_read_watchpoint:
8565 if (ui_out_is_mi_like_p (uiout))
8566 ui_out_field_string
8567 (uiout, "reason",
8568 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
8569 mention (b);
8570 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8571 ui_out_text (uiout, "\nValue = ");
8572 watchpoint_value_print (b->val, stb->stream);
8573 ui_out_field_stream (uiout, "value", stb);
8574 ui_out_text (uiout, "\n");
8575 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8576 break;
8577
8578 case bp_access_watchpoint:
348d480f
PA
8579 if (bs->old_val != NULL)
8580 {
8581 annotate_watchpoint (b->number);
8582 if (ui_out_is_mi_like_p (uiout))
8583 ui_out_field_string
8584 (uiout, "reason",
8585 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8586 mention (b);
8587 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8588 ui_out_text (uiout, "\nOld value = ");
8589 watchpoint_value_print (bs->old_val, stb->stream);
8590 ui_out_field_stream (uiout, "old", stb);
8591 ui_out_text (uiout, "\nNew value = ");
8592 }
8593 else
8594 {
8595 mention (b);
8596 if (ui_out_is_mi_like_p (uiout))
8597 ui_out_field_string
8598 (uiout, "reason",
8599 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8600 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8601 ui_out_text (uiout, "\nValue = ");
8602 }
8603 watchpoint_value_print (b->val, stb->stream);
8604 ui_out_field_stream (uiout, "new", stb);
8605 ui_out_text (uiout, "\n");
8606 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8607 break;
8608 default:
348d480f 8609 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8610 }
8611
348d480f
PA
8612 do_cleanups (old_chain);
8613 return result;
8614}
8615
8616/* Implement the "print_mention" breakpoint_ops method for hardware
8617 watchpoints. */
8618
8619static void
8620print_mention_watchpoint (struct breakpoint *b)
8621{
8622 struct cleanup *ui_out_chain;
8623
8624 switch (b->type)
8625 {
8626 case bp_watchpoint:
8627 ui_out_text (uiout, "Watchpoint ");
8628 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8629 break;
8630 case bp_hardware_watchpoint:
8631 ui_out_text (uiout, "Hardware watchpoint ");
8632 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8633 break;
8634 case bp_read_watchpoint:
8635 ui_out_text (uiout, "Hardware read watchpoint ");
8636 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8637 break;
8638 case bp_access_watchpoint:
8639 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
8640 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8641 break;
8642 default:
8643 internal_error (__FILE__, __LINE__,
8644 _("Invalid hardware watchpoint type."));
8645 }
8646
8647 ui_out_field_int (uiout, "number", b->number);
8648 ui_out_text (uiout, ": ");
8649 ui_out_field_string (uiout, "exp", b->exp_string);
8650 do_cleanups (ui_out_chain);
8651}
8652
8653/* Implement the "print_recreate" breakpoint_ops method for
8654 watchpoints. */
8655
8656static void
8657print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
8658{
8659 switch (b->type)
8660 {
8661 case bp_watchpoint:
8662 case bp_hardware_watchpoint:
8663 fprintf_unfiltered (fp, "watch");
8664 break;
8665 case bp_read_watchpoint:
8666 fprintf_unfiltered (fp, "rwatch");
8667 break;
8668 case bp_access_watchpoint:
8669 fprintf_unfiltered (fp, "awatch");
8670 break;
8671 default:
8672 internal_error (__FILE__, __LINE__,
8673 _("Invalid watchpoint type."));
8674 }
8675
8676 fprintf_unfiltered (fp, " %s", b->exp_string);
8677}
8678
8679/* The breakpoint_ops structure to be used in hardware watchpoints. */
8680
2060206e 8681static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
8682
8683/* Implement the "insert" breakpoint_ops method for
8684 masked hardware watchpoints. */
8685
8686static int
8687insert_masked_watchpoint (struct bp_location *bl)
8688{
8689 return target_insert_mask_watchpoint (bl->address, bl->owner->hw_wp_mask,
8690 bl->watchpoint_type);
8691}
8692
8693/* Implement the "remove" breakpoint_ops method for
8694 masked hardware watchpoints. */
8695
8696static int
8697remove_masked_watchpoint (struct bp_location *bl)
8698{
8699 return target_remove_mask_watchpoint (bl->address, bl->owner->hw_wp_mask,
8700 bl->watchpoint_type);
8701}
8702
8703/* Implement the "resources_needed" breakpoint_ops method for
8704 masked hardware watchpoints. */
8705
8706static int
8707resources_needed_masked_watchpoint (const struct bp_location *bl)
8708{
8709 return target_masked_watch_num_registers (bl->address,
8710 bl->owner->hw_wp_mask);
8711}
8712
8713/* Implement the "works_in_software_mode" breakpoint_ops method for
8714 masked hardware watchpoints. */
8715
8716static int
8717works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
8718{
8719 return 0;
8720}
8721
8722/* Implement the "print_it" breakpoint_ops method for
8723 masked hardware watchpoints. */
8724
8725static enum print_stop_action
8726print_it_masked_watchpoint (bpstat bs)
8727{
8728 struct breakpoint *b = bs->breakpoint_at;
8729
8730 /* Masked watchpoints have only one location. */
8731 gdb_assert (b->loc && b->loc->next == NULL);
8732
8733 switch (b->type)
8734 {
8735 case bp_hardware_watchpoint:
8736 annotate_watchpoint (b->number);
8737 if (ui_out_is_mi_like_p (uiout))
8738 ui_out_field_string
8739 (uiout, "reason",
8740 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
8741 break;
8742
8743 case bp_read_watchpoint:
8744 if (ui_out_is_mi_like_p (uiout))
8745 ui_out_field_string
8746 (uiout, "reason",
8747 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
8748 break;
8749
8750 case bp_access_watchpoint:
8751 if (ui_out_is_mi_like_p (uiout))
8752 ui_out_field_string
8753 (uiout, "reason",
8754 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8755 break;
8756 default:
8757 internal_error (__FILE__, __LINE__,
8758 _("Invalid hardware watchpoint type."));
8759 }
8760
8761 mention (b);
9c06b0b4
TJB
8762 ui_out_text (uiout, _("\n\
8763Check the underlying instruction at PC for the memory\n\
8764address and value which triggered this watchpoint.\n"));
8765 ui_out_text (uiout, "\n");
8766
8767 /* More than one watchpoint may have been triggered. */
8768 return PRINT_UNKNOWN;
8769}
8770
8771/* Implement the "print_one_detail" breakpoint_ops method for
8772 masked hardware watchpoints. */
8773
8774static void
8775print_one_detail_masked_watchpoint (const struct breakpoint *b,
8776 struct ui_out *uiout)
8777{
8778 /* Masked watchpoints have only one location. */
8779 gdb_assert (b->loc && b->loc->next == NULL);
8780
8781 ui_out_text (uiout, "\tmask ");
8782 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, b->hw_wp_mask);
8783 ui_out_text (uiout, "\n");
8784}
8785
8786/* Implement the "print_mention" breakpoint_ops method for
8787 masked hardware watchpoints. */
8788
8789static void
8790print_mention_masked_watchpoint (struct breakpoint *b)
8791{
8792 struct cleanup *ui_out_chain;
8793
8794 switch (b->type)
8795 {
8796 case bp_hardware_watchpoint:
8797 ui_out_text (uiout, "Masked hardware watchpoint ");
8798 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8799 break;
8800 case bp_read_watchpoint:
8801 ui_out_text (uiout, "Masked hardware read watchpoint ");
8802 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8803 break;
8804 case bp_access_watchpoint:
8805 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
8806 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8807 break;
8808 default:
8809 internal_error (__FILE__, __LINE__,
8810 _("Invalid hardware watchpoint type."));
8811 }
8812
8813 ui_out_field_int (uiout, "number", b->number);
8814 ui_out_text (uiout, ": ");
8815 ui_out_field_string (uiout, "exp", b->exp_string);
8816 do_cleanups (ui_out_chain);
8817}
8818
8819/* Implement the "print_recreate" breakpoint_ops method for
8820 masked hardware watchpoints. */
8821
8822static void
8823print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
8824{
8825 char tmp[40];
8826
8827 switch (b->type)
8828 {
8829 case bp_hardware_watchpoint:
8830 fprintf_unfiltered (fp, "watch");
8831 break;
8832 case bp_read_watchpoint:
8833 fprintf_unfiltered (fp, "rwatch");
8834 break;
8835 case bp_access_watchpoint:
8836 fprintf_unfiltered (fp, "awatch");
8837 break;
8838 default:
8839 internal_error (__FILE__, __LINE__,
8840 _("Invalid hardware watchpoint type."));
8841 }
8842
8843 sprintf_vma (tmp, b->hw_wp_mask);
8844 fprintf_unfiltered (fp, " %s mask 0x%s", b->exp_string, tmp);
8845}
8846
8847/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
8848
2060206e 8849static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
8850
8851/* Tell whether the given watchpoint is a masked hardware watchpoint. */
8852
8853static int
8854is_masked_watchpoint (const struct breakpoint *b)
8855{
8856 return b->ops == &masked_watchpoint_breakpoint_ops;
8857}
8858
53a5351d
JM
8859/* accessflag: hw_write: watch write,
8860 hw_read: watch read,
8861 hw_access: watch access (read or write) */
c906108c 8862static void
84f4c1fe
PM
8863watch_command_1 (char *arg, int accessflag, int from_tty,
8864 int just_location, int internal)
c906108c 8865{
a9634178 8866 volatile struct gdb_exception e;
d983da9c 8867 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 8868 struct expression *exp;
60e1c644 8869 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 8870 struct value *val, *mark, *result;
c906108c 8871 struct frame_info *frame;
c906108c
SS
8872 char *exp_start = NULL;
8873 char *exp_end = NULL;
9c06b0b4
TJB
8874 char *tok, *end_tok;
8875 int toklen = -1;
c906108c
SS
8876 char *cond_start = NULL;
8877 char *cond_end = NULL;
c906108c 8878 enum bptype bp_type;
37e4754d 8879 int thread = -1;
0cf6dd15 8880 int pc = 0;
9c06b0b4
TJB
8881 /* Flag to indicate whether we are going to use masks for
8882 the hardware watchpoint. */
8883 int use_mask = 0;
8884 CORE_ADDR mask = 0;
c906108c 8885
37e4754d
LM
8886 /* Make sure that we actually have parameters to parse. */
8887 if (arg != NULL && arg[0] != '\0')
8888 {
9c06b0b4 8889 char *value_start;
37e4754d 8890
9c06b0b4
TJB
8891 /* Look for "parameter value" pairs at the end
8892 of the arguments string. */
8893 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
8894 {
8895 /* Skip whitespace at the end of the argument list. */
8896 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8897 tok--;
8898
8899 /* Find the beginning of the last token.
8900 This is the value of the parameter. */
8901 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8902 tok--;
8903 value_start = tok + 1;
8904
8905 /* Skip whitespace. */
8906 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8907 tok--;
8908
8909 end_tok = tok;
8910
8911 /* Find the beginning of the second to last token.
8912 This is the parameter itself. */
8913 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8914 tok--;
8915 tok++;
8916 toklen = end_tok - tok + 1;
8917
8918 if (toklen == 6 && !strncmp (tok, "thread", 6))
8919 {
8920 /* At this point we've found a "thread" token, which means
8921 the user is trying to set a watchpoint that triggers
8922 only in a specific thread. */
8923 char *endp;
37e4754d 8924
9c06b0b4
TJB
8925 if (thread != -1)
8926 error(_("You can specify only one thread."));
37e4754d 8927
9c06b0b4
TJB
8928 /* Extract the thread ID from the next token. */
8929 thread = strtol (value_start, &endp, 0);
37e4754d 8930
9c06b0b4
TJB
8931 /* Check if the user provided a valid numeric value for the
8932 thread ID. */
8933 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
8934 error (_("Invalid thread ID specification %s."), value_start);
8935
8936 /* Check if the thread actually exists. */
8937 if (!valid_thread_id (thread))
8938 error (_("Unknown thread %d."), thread);
8939 }
8940 else if (toklen == 4 && !strncmp (tok, "mask", 4))
8941 {
8942 /* We've found a "mask" token, which means the user wants to
8943 create a hardware watchpoint that is going to have the mask
8944 facility. */
8945 struct value *mask_value, *mark;
37e4754d 8946
9c06b0b4
TJB
8947 if (use_mask)
8948 error(_("You can specify only one mask."));
37e4754d 8949
9c06b0b4 8950 use_mask = just_location = 1;
37e4754d 8951
9c06b0b4
TJB
8952 mark = value_mark ();
8953 mask_value = parse_to_comma_and_eval (&value_start);
8954 mask = value_as_address (mask_value);
8955 value_free_to_mark (mark);
8956 }
8957 else
8958 /* We didn't recognize what we found. We should stop here. */
8959 break;
37e4754d 8960
9c06b0b4
TJB
8961 /* Truncate the string and get rid of the "parameter value" pair before
8962 the arguments string is parsed by the parse_exp_1 function. */
8963 *tok = '\0';
8964 }
37e4754d
LM
8965 }
8966
8967 /* Parse the rest of the arguments. */
c906108c
SS
8968 innermost_block = NULL;
8969 exp_start = arg;
8970 exp = parse_exp_1 (&arg, 0, 0);
8971 exp_end = arg;
fa8a61dc
TT
8972 /* Remove trailing whitespace from the expression before saving it.
8973 This makes the eventual display of the expression string a bit
8974 prettier. */
8975 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
8976 --exp_end;
8977
65d79d4b
SDJ
8978 /* Checking if the expression is not constant. */
8979 if (watchpoint_exp_is_const (exp))
8980 {
8981 int len;
8982
8983 len = exp_end - exp_start;
8984 while (len > 0 && isspace (exp_start[len - 1]))
8985 len--;
8986 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
8987 }
8988
c906108c
SS
8989 exp_valid_block = innermost_block;
8990 mark = value_mark ();
a1442452 8991 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
8992
8993 if (just_location)
8994 {
9c06b0b4
TJB
8995 int ret;
8996
06a64a0b 8997 exp_valid_block = NULL;
a1442452 8998 val = value_addr (result);
06a64a0b
TT
8999 release_value (val);
9000 value_free_to_mark (mark);
9c06b0b4
TJB
9001
9002 if (use_mask)
9003 {
9004 ret = target_masked_watch_num_registers (value_as_address (val),
9005 mask);
9006 if (ret == -1)
9007 error (_("This target does not support masked watchpoints."));
9008 else if (ret == -2)
9009 error (_("Invalid mask or memory region."));
9010 }
06a64a0b
TT
9011 }
9012 else if (val != NULL)
fa4727a6 9013 release_value (val);
c906108c 9014
e9cafbcc
TT
9015 tok = skip_spaces (arg);
9016 end_tok = skip_to_space (tok);
c906108c
SS
9017
9018 toklen = end_tok - tok;
9019 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9020 {
2d134ed3
PA
9021 struct expression *cond;
9022
60e1c644 9023 innermost_block = NULL;
c906108c
SS
9024 tok = cond_start = end_tok + 1;
9025 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
9026
9027 /* The watchpoint expression may not be local, but the condition
9028 may still be. E.g.: `watch global if local > 0'. */
9029 cond_exp_valid_block = innermost_block;
9030
2d134ed3 9031 xfree (cond);
c906108c
SS
9032 cond_end = tok;
9033 }
9034 if (*tok)
8a3fe4f8 9035 error (_("Junk at end of command."));
c906108c 9036
53a5351d 9037 if (accessflag == hw_read)
c5aa993b 9038 bp_type = bp_read_watchpoint;
53a5351d 9039 else if (accessflag == hw_access)
c5aa993b
JM
9040 bp_type = bp_access_watchpoint;
9041 else
9042 bp_type = bp_hardware_watchpoint;
c906108c 9043
d983da9c 9044 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
9045
9046 /* If the expression is "local", then set up a "watchpoint scope"
9047 breakpoint at the point where we've left the scope of the watchpoint
9048 expression. Create the scope breakpoint before the watchpoint, so
9049 that we will encounter it first in bpstat_stop_status. */
60e1c644 9050 if (exp_valid_block && frame)
d983da9c 9051 {
edb3359d
DJ
9052 if (frame_id_p (frame_unwind_caller_id (frame)))
9053 {
9054 scope_breakpoint
a6d9a66e
UW
9055 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
9056 frame_unwind_caller_pc (frame),
06edf0c0
PA
9057 bp_watchpoint_scope,
9058 &momentary_breakpoint_ops);
d983da9c 9059
edb3359d 9060 scope_breakpoint->enable_state = bp_enabled;
d983da9c 9061
edb3359d
DJ
9062 /* Automatically delete the breakpoint when it hits. */
9063 scope_breakpoint->disposition = disp_del;
d983da9c 9064
edb3359d
DJ
9065 /* Only break in the proper frame (help with recursion). */
9066 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 9067
edb3359d 9068 /* Set the address at which we will stop. */
a6d9a66e
UW
9069 scope_breakpoint->loc->gdbarch
9070 = frame_unwind_caller_arch (frame);
edb3359d
DJ
9071 scope_breakpoint->loc->requested_address
9072 = frame_unwind_caller_pc (frame);
9073 scope_breakpoint->loc->address
a6d9a66e
UW
9074 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9075 scope_breakpoint->loc->requested_address,
edb3359d
DJ
9076 scope_breakpoint->type);
9077 }
d983da9c
DJ
9078 }
9079
c906108c 9080 /* Now set up the breakpoint. */
348d480f
PA
9081 if (use_mask)
9082 b = set_raw_breakpoint_without_location (NULL, bp_type,
9083 &masked_watchpoint_breakpoint_ops);
9084 else
9085 b = set_raw_breakpoint_without_location (NULL, bp_type,
9086 &watchpoint_breakpoint_ops);
37e4754d 9087 b->thread = thread;
b5de0fa7 9088 b->disposition = disp_donttouch;
c906108c
SS
9089 b->exp = exp;
9090 b->exp_valid_block = exp_valid_block;
60e1c644 9091 b->cond_exp_valid_block = cond_exp_valid_block;
348d480f 9092 b->pspace = current_program_space;
06a64a0b
TT
9093 if (just_location)
9094 {
9095 struct type *t = value_type (val);
9096 CORE_ADDR addr = value_as_address (val);
9097 char *name;
9098
9099 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9100 name = type_to_string (t);
9101
d63d0675
JK
9102 b->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
9103 core_addr_to_string (addr));
06a64a0b
TT
9104 xfree (name);
9105
4a4106ca 9106 b->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
9107 (int) (exp_end - exp_start), exp_start);
9108
06a64a0b
TT
9109 /* The above expression is in C. */
9110 b->language = language_c;
9111 }
9112 else
9113 b->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
9114
9115 if (use_mask)
9116 {
9117 b->hw_wp_mask = mask;
9c06b0b4
TJB
9118 }
9119 else
9120 {
9121 b->val = val;
9122 b->val_valid = 1;
9c06b0b4 9123 }
77b06cd7 9124
c906108c
SS
9125 if (cond_start)
9126 b->cond_string = savestring (cond_start, cond_end - cond_start);
9127 else
9128 b->cond_string = 0;
c5aa993b 9129
c906108c 9130 if (frame)
f6bc2008
PA
9131 {
9132 b->watchpoint_frame = get_frame_id (frame);
9133 b->watchpoint_thread = inferior_ptid;
9134 }
c906108c 9135 else
f6bc2008
PA
9136 {
9137 b->watchpoint_frame = null_frame_id;
9138 b->watchpoint_thread = null_ptid;
9139 }
c906108c 9140
d983da9c 9141 if (scope_breakpoint != NULL)
c906108c 9142 {
d983da9c
DJ
9143 /* The scope breakpoint is related to the watchpoint. We will
9144 need to act on them together. */
9145 b->related_breakpoint = scope_breakpoint;
9146 scope_breakpoint->related_breakpoint = b;
c906108c 9147 }
d983da9c 9148
06a64a0b
TT
9149 if (!just_location)
9150 value_free_to_mark (mark);
2d134ed3 9151
a9634178
TJB
9152 TRY_CATCH (e, RETURN_MASK_ALL)
9153 {
9154 /* Finally update the new watchpoint. This creates the locations
9155 that should be inserted. */
9156 update_watchpoint (b, 1);
9157 }
9158 if (e.reason < 0)
9159 {
9160 delete_breakpoint (b);
9161 throw_exception (e);
9162 }
9163
9164 set_breakpoint_number (internal, b);
8d3788bd
VP
9165
9166 /* Do not mention breakpoints with a negative number, but do
a9634178 9167 notify observers. */
8d3788bd 9168 if (!internal)
84f4c1fe 9169 mention (b);
8d3788bd
VP
9170 observer_notify_breakpoint_created (b);
9171
b60e7edf 9172 update_global_location_list (1);
c906108c
SS
9173}
9174
e09342b5 9175/* Return count of debug registers needed to watch the given expression.
e09342b5 9176 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9177
c906108c 9178static int
a9634178 9179can_use_hardware_watchpoint (struct value *v)
c906108c
SS
9180{
9181 int found_memory_cnt = 0;
2e70b7b9 9182 struct value *head = v;
c906108c
SS
9183
9184 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9185 if (!can_use_hw_watchpoints)
c906108c 9186 return 0;
c5aa993b 9187
5c44784c
JM
9188 /* Make sure that the value of the expression depends only upon
9189 memory contents, and values computed from them within GDB. If we
9190 find any register references or function calls, we can't use a
9191 hardware watchpoint.
9192
9193 The idea here is that evaluating an expression generates a series
9194 of values, one holding the value of every subexpression. (The
9195 expression a*b+c has five subexpressions: a, b, a*b, c, and
9196 a*b+c.) GDB's values hold almost enough information to establish
9197 the criteria given above --- they identify memory lvalues,
9198 register lvalues, computed values, etcetera. So we can evaluate
9199 the expression, and then scan the chain of values that leaves
9200 behind to decide whether we can detect any possible change to the
9201 expression's final value using only hardware watchpoints.
9202
9203 However, I don't think that the values returned by inferior
9204 function calls are special in any way. So this function may not
9205 notice that an expression involving an inferior function call
9206 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9207 for (; v; v = value_next (v))
c906108c 9208 {
5c44784c 9209 if (VALUE_LVAL (v) == lval_memory)
c906108c 9210 {
8464be76
DJ
9211 if (v != head && value_lazy (v))
9212 /* A lazy memory lvalue in the chain is one that GDB never
9213 needed to fetch; we either just used its address (e.g.,
9214 `a' in `a.b') or we never needed it at all (e.g., `a'
9215 in `a,b'). This doesn't apply to HEAD; if that is
9216 lazy then it was not readable, but watch it anyway. */
5c44784c 9217 ;
53a5351d 9218 else
5c44784c
JM
9219 {
9220 /* Ahh, memory we actually used! Check if we can cover
9221 it with hardware watchpoints. */
df407dfe 9222 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9223
9224 /* We only watch structs and arrays if user asked for it
9225 explicitly, never if they just happen to appear in a
9226 middle of some value chain. */
9227 if (v == head
9228 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9229 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9230 {
42ae5230 9231 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9232 int len;
9233 int num_regs;
9234
a9634178 9235 len = (target_exact_watchpoints
e09342b5
TJB
9236 && is_scalar_type_recursive (vtype))?
9237 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9238
e09342b5
TJB
9239 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9240 if (!num_regs)
2e70b7b9
MS
9241 return 0;
9242 else
e09342b5 9243 found_memory_cnt += num_regs;
2e70b7b9 9244 }
5c44784c 9245 }
c5aa993b 9246 }
5086187c
AC
9247 else if (VALUE_LVAL (v) != not_lval
9248 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9249 return 0; /* These are values from the history (e.g., $1). */
5086187c 9250 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9251 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9252 }
9253
9254 /* The expression itself looks suitable for using a hardware
9255 watchpoint, but give the target machine a chance to reject it. */
9256 return found_memory_cnt;
9257}
9258
8b93c638 9259void
84f4c1fe 9260watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9261{
84f4c1fe 9262 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9263}
9264
9265/* A helper function that looks for an argument at the start of a
9266 string. The argument must also either be at the end of the string,
9267 or be followed by whitespace. Returns 1 if it finds the argument,
9268 0 otherwise. If the argument is found, it updates *STR. */
9269
9270static int
9271check_for_argument (char **str, char *arg, int arg_len)
9272{
9273 if (strncmp (*str, arg, arg_len) == 0
9274 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9275 {
9276 *str += arg_len;
9277 return 1;
9278 }
9279 return 0;
9280}
9281
9282/* A helper function that looks for the "-location" argument and then
9283 calls watch_command_1. */
9284
9285static void
9286watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9287{
9288 int just_location = 0;
9289
9290 if (arg
9291 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9292 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9293 {
e9cafbcc 9294 arg = skip_spaces (arg);
06a64a0b
TT
9295 just_location = 1;
9296 }
9297
84f4c1fe 9298 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9299}
8926118c 9300
c5aa993b 9301static void
fba45db2 9302watch_command (char *arg, int from_tty)
c906108c 9303{
06a64a0b 9304 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9305}
9306
8b93c638 9307void
84f4c1fe 9308rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9309{
84f4c1fe 9310 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9311}
8926118c 9312
c5aa993b 9313static void
fba45db2 9314rwatch_command (char *arg, int from_tty)
c906108c 9315{
06a64a0b 9316 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9317}
9318
8b93c638 9319void
84f4c1fe 9320awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9321{
84f4c1fe 9322 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9323}
8926118c 9324
c5aa993b 9325static void
fba45db2 9326awatch_command (char *arg, int from_tty)
c906108c 9327{
06a64a0b 9328 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9329}
c906108c 9330\f
c5aa993b 9331
43ff13b4 9332/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9333 because it uses the mechanisms of breakpoints. */
9334
bfec99b2
PA
9335struct until_break_command_continuation_args
9336{
9337 struct breakpoint *breakpoint;
9338 struct breakpoint *breakpoint2;
186c406b 9339 int thread_num;
bfec99b2
PA
9340};
9341
43ff13b4 9342/* This function is called by fetch_inferior_event via the
4a64f543 9343 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9344 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9345 command. */
c2c6d25f 9346static void
fa4cd53f 9347until_break_command_continuation (void *arg, int err)
43ff13b4 9348{
bfec99b2
PA
9349 struct until_break_command_continuation_args *a = arg;
9350
9351 delete_breakpoint (a->breakpoint);
9352 if (a->breakpoint2)
9353 delete_breakpoint (a->breakpoint2);
186c406b 9354 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9355}
9356
c906108c 9357void
ae66c1fc 9358until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9359{
9360 struct symtabs_and_lines sals;
9361 struct symtab_and_line sal;
206415a3 9362 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9363 struct breakpoint *breakpoint;
f107f563 9364 struct breakpoint *breakpoint2 = NULL;
c906108c 9365 struct cleanup *old_chain;
186c406b
TT
9366 int thread;
9367 struct thread_info *tp;
c906108c
SS
9368
9369 clear_proceed_status ();
9370
9371 /* Set a breakpoint where the user wants it and at return from
4a64f543 9372 this function. */
c5aa993b 9373
c906108c
SS
9374 if (default_breakpoint_valid)
9375 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
58438ac1 9376 default_breakpoint_line, NULL);
c906108c 9377 else
58438ac1 9378 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, NULL);
c5aa993b 9379
c906108c 9380 if (sals.nelts != 1)
8a3fe4f8 9381 error (_("Couldn't get information on specified line."));
c5aa993b 9382
c906108c 9383 sal = sals.sals[0];
4a64f543 9384 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9385
c906108c 9386 if (*arg)
8a3fe4f8 9387 error (_("Junk at end of arguments."));
c5aa993b 9388
c906108c 9389 resolve_sal_pc (&sal);
c5aa993b 9390
ae66c1fc
EZ
9391 if (anywhere)
9392 /* If the user told us to continue until a specified location,
9393 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9394 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9395 null_frame_id, bp_until);
ae66c1fc 9396 else
4a64f543
MS
9397 /* Otherwise, specify the selected frame, because we want to stop
9398 only at the very same frame. */
a6d9a66e
UW
9399 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9400 get_stack_frame_id (frame),
ae66c1fc 9401 bp_until);
c5aa993b 9402
f107f563 9403 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9404
186c406b
TT
9405 tp = inferior_thread ();
9406 thread = tp->num;
9407
ae66c1fc
EZ
9408 /* Keep within the current frame, or in frames called by the current
9409 one. */
edb3359d
DJ
9410
9411 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9412 {
edb3359d
DJ
9413 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9414 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9415 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9416 sal,
edb3359d 9417 frame_unwind_caller_id (frame),
f107f563
VP
9418 bp_until);
9419 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9420
9421 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9422 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9423 }
c5aa993b 9424
c906108c 9425 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9426
4a64f543
MS
9427 /* If we are running asynchronously, and proceed call above has
9428 actually managed to start the target, arrange for breakpoints to
9429 be deleted when the target stops. Otherwise, we're already
9430 stopped and delete breakpoints via cleanup chain. */
f107f563 9431
8ea051c5 9432 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9433 {
bfec99b2
PA
9434 struct until_break_command_continuation_args *args;
9435 args = xmalloc (sizeof (*args));
f107f563 9436
bfec99b2
PA
9437 args->breakpoint = breakpoint;
9438 args->breakpoint2 = breakpoint2;
186c406b 9439 args->thread_num = thread;
f107f563
VP
9440
9441 discard_cleanups (old_chain);
95e54da7
PA
9442 add_continuation (inferior_thread (),
9443 until_break_command_continuation, args,
604ead4a 9444 xfree);
f107f563
VP
9445 }
9446 else
c5aa993b 9447 do_cleanups (old_chain);
c906108c 9448}
ae66c1fc 9449
c906108c
SS
9450/* This function attempts to parse an optional "if <cond>" clause
9451 from the arg string. If one is not found, it returns NULL.
c5aa993b 9452
c906108c
SS
9453 Else, it returns a pointer to the condition string. (It does not
9454 attempt to evaluate the string against a particular block.) And,
9455 it updates arg to point to the first character following the parsed
4a64f543 9456 if clause in the arg string. */
53a5351d 9457
c906108c 9458static char *
fba45db2 9459ep_parse_optional_if_clause (char **arg)
c906108c 9460{
c5aa993b
JM
9461 char *cond_string;
9462
9463 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9464 return NULL;
c5aa993b 9465
4a64f543 9466 /* Skip the "if" keyword. */
c906108c 9467 (*arg) += 2;
c5aa993b 9468
c906108c 9469 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9470 condition string. */
e9cafbcc 9471 *arg = skip_spaces (*arg);
c906108c 9472 cond_string = *arg;
c5aa993b 9473
4a64f543
MS
9474 /* Assume that the condition occupies the remainder of the arg
9475 string. */
c906108c 9476 (*arg) += strlen (cond_string);
c5aa993b 9477
c906108c
SS
9478 return cond_string;
9479}
c5aa993b 9480
c906108c
SS
9481/* Commands to deal with catching events, such as signals, exceptions,
9482 process start/exit, etc. */
c5aa993b
JM
9483
9484typedef enum
9485{
44feb3ce
TT
9486 catch_fork_temporary, catch_vfork_temporary,
9487 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9488}
9489catch_fork_kind;
9490
c906108c 9491static void
cc59ec59
MS
9492catch_fork_command_1 (char *arg, int from_tty,
9493 struct cmd_list_element *command)
c906108c 9494{
a6d9a66e 9495 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9496 char *cond_string = NULL;
44feb3ce
TT
9497 catch_fork_kind fork_kind;
9498 int tempflag;
9499
9500 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9501 tempflag = (fork_kind == catch_fork_temporary
9502 || fork_kind == catch_vfork_temporary);
c5aa993b 9503
44feb3ce
TT
9504 if (!arg)
9505 arg = "";
e9cafbcc 9506 arg = skip_spaces (arg);
c5aa993b 9507
c906108c 9508 /* The allowed syntax is:
c5aa993b
JM
9509 catch [v]fork
9510 catch [v]fork if <cond>
9511
4a64f543 9512 First, check if there's an if clause. */
c906108c 9513 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9514
c906108c 9515 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9516 error (_("Junk at end of arguments."));
c5aa993b 9517
c906108c 9518 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9519 and enable reporting of such events. */
c5aa993b
JM
9520 switch (fork_kind)
9521 {
44feb3ce
TT
9522 case catch_fork_temporary:
9523 case catch_fork_permanent:
a6d9a66e 9524 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9525 &catch_fork_breakpoint_ops);
c906108c 9526 break;
44feb3ce
TT
9527 case catch_vfork_temporary:
9528 case catch_vfork_permanent:
a6d9a66e 9529 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9530 &catch_vfork_breakpoint_ops);
c906108c 9531 break;
c5aa993b 9532 default:
8a3fe4f8 9533 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9534 break;
c5aa993b 9535 }
c906108c
SS
9536}
9537
9538static void
cc59ec59
MS
9539catch_exec_command_1 (char *arg, int from_tty,
9540 struct cmd_list_element *command)
c906108c 9541{
b4d90040 9542 struct exec_catchpoint *c;
a6d9a66e 9543 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9544 int tempflag;
c5aa993b 9545 char *cond_string = NULL;
c906108c 9546
44feb3ce
TT
9547 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9548
9549 if (!arg)
9550 arg = "";
e9cafbcc 9551 arg = skip_spaces (arg);
c906108c
SS
9552
9553 /* The allowed syntax is:
c5aa993b
JM
9554 catch exec
9555 catch exec if <cond>
c906108c 9556
4a64f543 9557 First, check if there's an if clause. */
c906108c
SS
9558 cond_string = ep_parse_optional_if_clause (&arg);
9559
9560 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9561 error (_("Junk at end of arguments."));
c906108c 9562
b4d90040
PA
9563 c = XNEW (struct exec_catchpoint);
9564 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
9565 &catch_exec_breakpoint_ops);
9566 c->exec_pathname = NULL;
9567
28010a5d 9568 install_breakpoint (&c->base);
c906108c 9569}
c5aa993b 9570
3086aeae 9571static enum print_stop_action
348d480f 9572print_it_exception_catchpoint (bpstat bs)
3086aeae 9573{
348d480f 9574 struct breakpoint *b = bs->breakpoint_at;
ade92717 9575 int bp_temp, bp_throw;
3086aeae 9576
ade92717 9577 annotate_catchpoint (b->number);
3086aeae 9578
ade92717
AR
9579 bp_throw = strstr (b->addr_string, "throw") != NULL;
9580 if (b->loc->address != b->loc->requested_address)
9581 breakpoint_adjustment_warning (b->loc->requested_address,
9582 b->loc->address,
9583 b->number, 1);
df2b6d2d 9584 bp_temp = b->disposition == disp_del;
ade92717
AR
9585 ui_out_text (uiout,
9586 bp_temp ? "Temporary catchpoint "
9587 : "Catchpoint ");
9588 if (!ui_out_is_mi_like_p (uiout))
9589 ui_out_field_int (uiout, "bkptno", b->number);
9590 ui_out_text (uiout,
c0b37c48
AR
9591 bp_throw ? " (exception thrown), "
9592 : " (exception caught), ");
ade92717
AR
9593 if (ui_out_is_mi_like_p (uiout))
9594 {
9595 ui_out_field_string (uiout, "reason",
9596 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9597 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9598 ui_out_field_int (uiout, "bkptno", b->number);
9599 }
3086aeae
DJ
9600 return PRINT_SRC_AND_LOC;
9601}
9602
9603static void
cc59ec59
MS
9604print_one_exception_catchpoint (struct breakpoint *b,
9605 struct bp_location **last_loc)
3086aeae 9606{
79a45b7d 9607 struct value_print_options opts;
cc59ec59 9608
79a45b7d
TT
9609 get_user_print_options (&opts);
9610 if (opts.addressprint)
3086aeae
DJ
9611 {
9612 annotate_field (4);
604133b5
AR
9613 if (b->loc == NULL || b->loc->shlib_disabled)
9614 ui_out_field_string (uiout, "addr", "<PENDING>");
9615 else
5af949e3
UW
9616 ui_out_field_core_addr (uiout, "addr",
9617 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9618 }
9619 annotate_field (5);
604133b5 9620 if (b->loc)
a6d9a66e 9621 *last_loc = b->loc;
3086aeae
DJ
9622 if (strstr (b->addr_string, "throw") != NULL)
9623 ui_out_field_string (uiout, "what", "exception throw");
9624 else
9625 ui_out_field_string (uiout, "what", "exception catch");
9626}
9627
9628static void
9629print_mention_exception_catchpoint (struct breakpoint *b)
9630{
ade92717
AR
9631 int bp_temp;
9632 int bp_throw;
9633
df2b6d2d 9634 bp_temp = b->disposition == disp_del;
ade92717
AR
9635 bp_throw = strstr (b->addr_string, "throw") != NULL;
9636 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9637 : _("Catchpoint "));
9638 ui_out_field_int (uiout, "bkptno", b->number);
9639 ui_out_text (uiout, bp_throw ? _(" (throw)")
9640 : _(" (catch)"));
3086aeae
DJ
9641}
9642
6149aea9
PA
9643/* Implement the "print_recreate" breakpoint_ops method for throw and
9644 catch catchpoints. */
9645
9646static void
4a64f543
MS
9647print_recreate_exception_catchpoint (struct breakpoint *b,
9648 struct ui_file *fp)
6149aea9
PA
9649{
9650 int bp_temp;
9651 int bp_throw;
9652
9653 bp_temp = b->disposition == disp_del;
9654 bp_throw = strstr (b->addr_string, "throw") != NULL;
9655 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9656 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
9657}
9658
2060206e 9659static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
9660
9661static int
9662handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9663 enum exception_event_kind ex_event, int from_tty)
9664{
604133b5
AR
9665 char *trigger_func_name;
9666
3086aeae 9667 if (ex_event == EX_EVENT_CATCH)
604133b5 9668 trigger_func_name = "__cxa_begin_catch";
3086aeae 9669 else
604133b5 9670 trigger_func_name = "__cxa_throw";
3086aeae 9671
8cdf0e15
VP
9672 create_breakpoint (get_current_arch (),
9673 trigger_func_name, cond_string, -1,
9674 0 /* condition and thread are valid. */,
0fb4aa4b 9675 tempflag, bp_breakpoint,
8cdf0e15
VP
9676 0,
9677 AUTO_BOOLEAN_TRUE /* pending */,
9678 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9679 1 /* enabled */,
9680 0 /* internal */);
3086aeae 9681
3086aeae
DJ
9682 return 1;
9683}
9684
4a64f543 9685/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9686
9687static void
fba45db2
KB
9688catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9689 int tempflag, int from_tty)
c906108c 9690{
c5aa993b 9691 char *cond_string = NULL;
c5aa993b 9692
44feb3ce
TT
9693 if (!arg)
9694 arg = "";
e9cafbcc 9695 arg = skip_spaces (arg);
c5aa993b 9696
c906108c
SS
9697 cond_string = ep_parse_optional_if_clause (&arg);
9698
9699 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9700 error (_("Junk at end of arguments."));
c906108c 9701
059fb39f
PM
9702 if (ex_event != EX_EVENT_THROW
9703 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9704 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9705
3086aeae
DJ
9706 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9707 return;
9708
8a3fe4f8 9709 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9710}
9711
44feb3ce
TT
9712/* Implementation of "catch catch" command. */
9713
9714static void
9715catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9716{
9717 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9718
44feb3ce
TT
9719 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9720}
9721
9722/* Implementation of "catch throw" command. */
9723
9724static void
9725catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9726{
9727 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9728
44feb3ce
TT
9729 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9730}
9731
9ac4176b 9732void
28010a5d
PA
9733init_ada_exception_breakpoint (struct breakpoint *b,
9734 struct gdbarch *gdbarch,
9735 struct symtab_and_line sal,
9736 char *addr_string,
9737 struct breakpoint_ops *ops,
9738 int tempflag,
9739 int from_tty)
f7f9143b 9740{
f7f9143b
JB
9741 if (from_tty)
9742 {
5af949e3
UW
9743 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9744 if (!loc_gdbarch)
9745 loc_gdbarch = gdbarch;
9746
6c95b8df
PA
9747 describe_other_breakpoints (loc_gdbarch,
9748 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9749 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9750 version for exception catchpoints, because two catchpoints
9751 used for different exception names will use the same address.
9752 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9753 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 9754 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
9755 the user what type of catchpoint it is. The above is good
9756 enough for now, though. */
9757 }
9758
28010a5d 9759 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
9760
9761 b->enable_state = bp_enabled;
9762 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
9763 b->addr_string = addr_string;
9764 b->language = language_ada;
f7f9143b
JB
9765}
9766
a96d9b2e
SDJ
9767/* Cleanup function for a syscall filter list. */
9768static void
9769clean_up_filters (void *arg)
9770{
9771 VEC(int) *iter = *(VEC(int) **) arg;
9772 VEC_free (int, iter);
9773}
9774
9775/* Splits the argument using space as delimiter. Returns an xmalloc'd
9776 filter list, or NULL if no filtering is required. */
9777static VEC(int) *
9778catch_syscall_split_args (char *arg)
9779{
9780 VEC(int) *result = NULL;
9781 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9782
9783 while (*arg != '\0')
9784 {
9785 int i, syscall_number;
9786 char *endptr;
9787 char cur_name[128];
9788 struct syscall s;
9789
9790 /* Skip whitespace. */
9791 while (isspace (*arg))
9792 arg++;
9793
9794 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9795 cur_name[i] = arg[i];
9796 cur_name[i] = '\0';
9797 arg += i;
9798
9799 /* Check if the user provided a syscall name or a number. */
9800 syscall_number = (int) strtol (cur_name, &endptr, 0);
9801 if (*endptr == '\0')
bccd0dd2 9802 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9803 else
9804 {
9805 /* We have a name. Let's check if it's valid and convert it
9806 to a number. */
9807 get_syscall_by_name (cur_name, &s);
9808
9809 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
9810 /* Here we have to issue an error instead of a warning,
9811 because GDB cannot do anything useful if there's no
9812 syscall number to be caught. */
a96d9b2e
SDJ
9813 error (_("Unknown syscall name '%s'."), cur_name);
9814 }
9815
9816 /* Ok, it's valid. */
9817 VEC_safe_push (int, result, s.number);
9818 }
9819
9820 discard_cleanups (cleanup);
9821 return result;
9822}
9823
9824/* Implement the "catch syscall" command. */
9825
9826static void
cc59ec59
MS
9827catch_syscall_command_1 (char *arg, int from_tty,
9828 struct cmd_list_element *command)
a96d9b2e
SDJ
9829{
9830 int tempflag;
9831 VEC(int) *filter;
9832 struct syscall s;
9833 struct gdbarch *gdbarch = get_current_arch ();
9834
9835 /* Checking if the feature if supported. */
9836 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9837 error (_("The feature 'catch syscall' is not supported on \
ea666128 9838this architecture yet."));
a96d9b2e
SDJ
9839
9840 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9841
e9cafbcc 9842 arg = skip_spaces (arg);
a96d9b2e
SDJ
9843
9844 /* We need to do this first "dummy" translation in order
9845 to get the syscall XML file loaded or, most important,
9846 to display a warning to the user if there's no XML file
9847 for his/her architecture. */
9848 get_syscall_by_number (0, &s);
9849
9850 /* The allowed syntax is:
9851 catch syscall
9852 catch syscall <name | number> [<name | number> ... <name | number>]
9853
9854 Let's check if there's a syscall name. */
9855
9856 if (arg != NULL)
9857 filter = catch_syscall_split_args (arg);
9858 else
9859 filter = NULL;
9860
9861 create_syscall_event_catchpoint (tempflag, filter,
9862 &catch_syscall_breakpoint_ops);
9863}
9864
c906108c 9865static void
fba45db2 9866catch_command (char *arg, int from_tty)
c906108c 9867{
44feb3ce 9868 error (_("Catch requires an event name."));
c906108c
SS
9869}
9870\f
9871
9872static void
fba45db2 9873tcatch_command (char *arg, int from_tty)
c906108c 9874{
44feb3ce 9875 error (_("Catch requires an event name."));
c906108c
SS
9876}
9877
80f8a6eb 9878/* Delete breakpoints by address or line. */
c906108c
SS
9879
9880static void
fba45db2 9881clear_command (char *arg, int from_tty)
c906108c 9882{
d6e956e5
VP
9883 struct breakpoint *b;
9884 VEC(breakpoint_p) *found = 0;
9885 int ix;
c906108c
SS
9886 int default_match;
9887 struct symtabs_and_lines sals;
9888 struct symtab_and_line sal;
c906108c
SS
9889 int i;
9890
9891 if (arg)
9892 {
9893 sals = decode_line_spec (arg, 1);
9894 default_match = 0;
9895 }
9896 else
9897 {
c5aa993b 9898 sals.sals = (struct symtab_and_line *)
c906108c 9899 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 9900 make_cleanup (xfree, sals.sals);
4a64f543 9901 init_sal (&sal); /* Initialize to zeroes. */
c906108c
SS
9902 sal.line = default_breakpoint_line;
9903 sal.symtab = default_breakpoint_symtab;
9904 sal.pc = default_breakpoint_address;
6c95b8df 9905 sal.pspace = default_breakpoint_pspace;
c906108c 9906 if (sal.symtab == 0)
8a3fe4f8 9907 error (_("No source file specified."));
c906108c
SS
9908
9909 sals.sals[0] = sal;
9910 sals.nelts = 1;
9911
9912 default_match = 1;
9913 }
9914
4a64f543
MS
9915 /* We don't call resolve_sal_pc here. That's not as bad as it
9916 seems, because all existing breakpoints typically have both
9917 file/line and pc set. So, if clear is given file/line, we can
9918 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
9919
9920 We only support clearing given the address explicitly
9921 present in breakpoint table. Say, we've set breakpoint
4a64f543 9922 at file:line. There were several PC values for that file:line,
ed0616c6 9923 due to optimization, all in one block.
4a64f543
MS
9924
9925 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
9926 PC corresponding to the same file:line, the breakpoint won't
9927 be cleared. We probably can still clear the breakpoint, but
9928 since the other PC value is never presented to user, user
9929 can only find it by guessing, and it does not seem important
9930 to support that. */
9931
4a64f543
MS
9932 /* For each line spec given, delete bps which correspond to it. Do
9933 it in two passes, solely to preserve the current behavior that
9934 from_tty is forced true if we delete more than one
9935 breakpoint. */
c906108c 9936
80f8a6eb 9937 found = NULL;
c906108c
SS
9938 for (i = 0; i < sals.nelts; i++)
9939 {
9940 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
9941 If line given (pc == 0), clear all bpts on specified line.
9942 If defaulting, clear all bpts on default line
c906108c 9943 or at default pc.
c5aa993b
JM
9944
9945 defaulting sal.pc != 0 tests to do
9946
9947 0 1 pc
9948 1 1 pc _and_ line
9949 0 0 line
9950 1 0 <can't happen> */
c906108c
SS
9951
9952 sal = sals.sals[i];
c906108c 9953
4a64f543 9954 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 9955 ALL_BREAKPOINTS (b)
c5aa993b 9956 {
0d381245 9957 int match = 0;
4a64f543 9958 /* Are we going to delete b? */
cc60f2e3 9959 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
9960 {
9961 struct bp_location *loc = b->loc;
9962 for (; loc; loc = loc->next)
9963 {
6c95b8df
PA
9964 int pc_match = sal.pc
9965 && (loc->pspace == sal.pspace)
0d381245
VP
9966 && (loc->address == sal.pc)
9967 && (!section_is_overlay (loc->section)
9968 || loc->section == sal.section);
9969 int line_match = ((default_match || (0 == sal.pc))
9970 && b->source_file != NULL
9971 && sal.symtab != NULL
6c95b8df 9972 && sal.pspace == loc->pspace
0ba1096a
KT
9973 && filename_cmp (b->source_file,
9974 sal.symtab->filename) == 0
0d381245
VP
9975 && b->line_number == sal.line);
9976 if (pc_match || line_match)
9977 {
9978 match = 1;
9979 break;
9980 }
9981 }
9982 }
9983
9984 if (match)
d6e956e5 9985 VEC_safe_push(breakpoint_p, found, b);
c906108c 9986 }
80f8a6eb
MS
9987 }
9988 /* Now go thru the 'found' chain and delete them. */
d6e956e5 9989 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
9990 {
9991 if (arg)
8a3fe4f8 9992 error (_("No breakpoint at %s."), arg);
80f8a6eb 9993 else
8a3fe4f8 9994 error (_("No breakpoint at this line."));
80f8a6eb 9995 }
c906108c 9996
d6e956e5 9997 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 9998 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 9999 if (from_tty)
a3f17187 10000 {
d6e956e5 10001 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
10002 printf_unfiltered (_("Deleted breakpoint "));
10003 else
10004 printf_unfiltered (_("Deleted breakpoints "));
10005 }
80f8a6eb 10006 breakpoints_changed ();
d6e956e5
VP
10007
10008 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 10009 {
c5aa993b 10010 if (from_tty)
d6e956e5
VP
10011 printf_unfiltered ("%d ", b->number);
10012 delete_breakpoint (b);
c906108c 10013 }
80f8a6eb
MS
10014 if (from_tty)
10015 putchar_unfiltered ('\n');
c906108c
SS
10016}
10017\f
10018/* Delete breakpoint in BS if they are `delete' breakpoints and
10019 all breakpoints that are marked for deletion, whether hit or not.
10020 This is called after any breakpoint is hit, or after errors. */
10021
10022void
fba45db2 10023breakpoint_auto_delete (bpstat bs)
c906108c 10024{
35df4500 10025 struct breakpoint *b, *b_tmp;
c906108c
SS
10026
10027 for (; bs; bs = bs->next)
f431efe5
PA
10028 if (bs->breakpoint_at
10029 && bs->breakpoint_at->disposition == disp_del
c906108c 10030 && bs->stop)
f431efe5 10031 delete_breakpoint (bs->breakpoint_at);
c906108c 10032
35df4500 10033 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10034 {
b5de0fa7 10035 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10036 delete_breakpoint (b);
10037 }
c906108c
SS
10038}
10039
4a64f543
MS
10040/* A comparison function for bp_location AP and BP being interfaced to
10041 qsort. Sort elements primarily by their ADDRESS (no matter what
10042 does breakpoint_address_is_meaningful say for its OWNER),
10043 secondarily by ordering first bp_permanent OWNERed elements and
10044 terciarily just ensuring the array is sorted stable way despite
e5dd4106 10045 qsort being an unstable algorithm. */
876fa593
JK
10046
10047static int
494cfb0f 10048bp_location_compare (const void *ap, const void *bp)
876fa593 10049{
494cfb0f
JK
10050 struct bp_location *a = *(void **) ap;
10051 struct bp_location *b = *(void **) bp;
2bdf28a0 10052 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10053 int a_perm = a->owner->enable_state == bp_permanent;
10054 int b_perm = b->owner->enable_state == bp_permanent;
10055
10056 if (a->address != b->address)
10057 return (a->address > b->address) - (a->address < b->address);
10058
10059 /* Sort permanent breakpoints first. */
10060 if (a_perm != b_perm)
10061 return (a_perm < b_perm) - (a_perm > b_perm);
10062
4a64f543
MS
10063 /* Make the user-visible order stable across GDB runs. Locations of
10064 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10065
10066 if (a->owner->number != b->owner->number)
10067 return (a->owner->number > b->owner->number)
10068 - (a->owner->number < b->owner->number);
10069
10070 return (a > b) - (a < b);
10071}
10072
876fa593 10073/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10074 bp_location_shadow_len_after_address_max according to the current
10075 content of the bp_location array. */
f7545552
TT
10076
10077static void
876fa593 10078bp_location_target_extensions_update (void)
f7545552 10079{
876fa593
JK
10080 struct bp_location *bl, **blp_tmp;
10081
10082 bp_location_placed_address_before_address_max = 0;
10083 bp_location_shadow_len_after_address_max = 0;
10084
10085 ALL_BP_LOCATIONS (bl, blp_tmp)
10086 {
10087 CORE_ADDR start, end, addr;
10088
10089 if (!bp_location_has_shadow (bl))
10090 continue;
10091
10092 start = bl->target_info.placed_address;
10093 end = start + bl->target_info.shadow_len;
10094
10095 gdb_assert (bl->address >= start);
10096 addr = bl->address - start;
10097 if (addr > bp_location_placed_address_before_address_max)
10098 bp_location_placed_address_before_address_max = addr;
10099
10100 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10101
10102 gdb_assert (bl->address < end);
10103 addr = end - bl->address;
10104 if (addr > bp_location_shadow_len_after_address_max)
10105 bp_location_shadow_len_after_address_max = addr;
10106 }
f7545552
TT
10107}
10108
4cd9bd08 10109/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10110 into the inferior, only remove already-inserted locations that no
10111 longer should be inserted. Functions that delete a breakpoint or
10112 breakpoints should pass false, so that deleting a breakpoint
10113 doesn't have the side effect of inserting the locations of other
10114 breakpoints that are marked not-inserted, but should_be_inserted
10115 returns true on them.
10116
10117 This behaviour is useful is situations close to tear-down -- e.g.,
10118 after an exec, while the target still has execution, but breakpoint
10119 shadows of the previous executable image should *NOT* be restored
10120 to the new image; or before detaching, where the target still has
10121 execution and wants to delete breakpoints from GDB's lists, and all
10122 breakpoints had already been removed from the inferior. */
10123
0d381245 10124static void
b60e7edf 10125update_global_location_list (int should_insert)
0d381245 10126{
74960c60 10127 struct breakpoint *b;
876fa593 10128 struct bp_location **locp, *loc;
f7545552
TT
10129 struct cleanup *cleanups;
10130
2d134ed3
PA
10131 /* Used in the duplicates detection below. When iterating over all
10132 bp_locations, points to the first bp_location of a given address.
10133 Breakpoints and watchpoints of different types are never
10134 duplicates of each other. Keep one pointer for each type of
10135 breakpoint/watchpoint, so we only need to loop over all locations
10136 once. */
10137 struct bp_location *bp_loc_first; /* breakpoint */
10138 struct bp_location *wp_loc_first; /* hardware watchpoint */
10139 struct bp_location *awp_loc_first; /* access watchpoint */
10140 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10141
4a64f543
MS
10142 /* Saved former bp_location array which we compare against the newly
10143 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10144 struct bp_location **old_location, **old_locp;
10145 unsigned old_location_count;
10146
10147 old_location = bp_location;
10148 old_location_count = bp_location_count;
10149 bp_location = NULL;
10150 bp_location_count = 0;
10151 cleanups = make_cleanup (xfree, old_location);
0d381245 10152
74960c60 10153 ALL_BREAKPOINTS (b)
876fa593
JK
10154 for (loc = b->loc; loc; loc = loc->next)
10155 bp_location_count++;
10156
10157 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10158 locp = bp_location;
10159 ALL_BREAKPOINTS (b)
10160 for (loc = b->loc; loc; loc = loc->next)
10161 *locp++ = loc;
10162 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10163 bp_location_compare);
876fa593
JK
10164
10165 bp_location_target_extensions_update ();
74960c60 10166
4a64f543
MS
10167 /* Identify bp_location instances that are no longer present in the
10168 new list, and therefore should be freed. Note that it's not
10169 necessary that those locations should be removed from inferior --
10170 if there's another location at the same address (previously
10171 marked as duplicate), we don't need to remove/insert the
10172 location.
876fa593 10173
4a64f543
MS
10174 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10175 and former bp_location array state respectively. */
876fa593
JK
10176
10177 locp = bp_location;
10178 for (old_locp = old_location; old_locp < old_location + old_location_count;
10179 old_locp++)
74960c60 10180 {
876fa593 10181 struct bp_location *old_loc = *old_locp;
c7d46a38 10182 struct bp_location **loc2p;
876fa593 10183
e5dd4106 10184 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 10185 not, we have to free it. */
c7d46a38 10186 int found_object = 0;
20874c92
VP
10187 /* Tells if the location should remain inserted in the target. */
10188 int keep_in_target = 0;
10189 int removed = 0;
876fa593 10190
4a64f543
MS
10191 /* Skip LOCP entries which will definitely never be needed.
10192 Stop either at or being the one matching OLD_LOC. */
876fa593 10193 while (locp < bp_location + bp_location_count
c7d46a38 10194 && (*locp)->address < old_loc->address)
876fa593 10195 locp++;
c7d46a38
PA
10196
10197 for (loc2p = locp;
10198 (loc2p < bp_location + bp_location_count
10199 && (*loc2p)->address == old_loc->address);
10200 loc2p++)
10201 {
10202 if (*loc2p == old_loc)
10203 {
10204 found_object = 1;
10205 break;
10206 }
10207 }
74960c60 10208
4a64f543
MS
10209 /* If this location is no longer present, and inserted, look if
10210 there's maybe a new location at the same address. If so,
10211 mark that one inserted, and don't remove this one. This is
10212 needed so that we don't have a time window where a breakpoint
10213 at certain location is not inserted. */
74960c60 10214
876fa593 10215 if (old_loc->inserted)
0d381245 10216 {
4a64f543
MS
10217 /* If the location is inserted now, we might have to remove
10218 it. */
74960c60 10219
876fa593 10220 if (found_object && should_be_inserted (old_loc))
74960c60 10221 {
4a64f543
MS
10222 /* The location is still present in the location list,
10223 and still should be inserted. Don't do anything. */
20874c92 10224 keep_in_target = 1;
74960c60
VP
10225 }
10226 else
10227 {
4a64f543
MS
10228 /* The location is either no longer present, or got
10229 disabled. See if there's another location at the
10230 same address, in which case we don't need to remove
10231 this one from the target. */
876fa593 10232
2bdf28a0 10233 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10234 if (breakpoint_address_is_meaningful (old_loc->owner))
10235 {
876fa593 10236 for (loc2p = locp;
c7d46a38
PA
10237 (loc2p < bp_location + bp_location_count
10238 && (*loc2p)->address == old_loc->address);
876fa593
JK
10239 loc2p++)
10240 {
10241 struct bp_location *loc2 = *loc2p;
10242
2d134ed3 10243 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38
PA
10244 {
10245 /* For the sake of should_be_inserted.
4a64f543
MS
10246 Duplicates check below will fix up this
10247 later. */
c7d46a38 10248 loc2->duplicate = 0;
85d721b8
PA
10249
10250 /* Read watchpoint locations are switched to
10251 access watchpoints, if the former are not
10252 supported, but the latter are. */
10253 if (is_hardware_watchpoint (old_loc->owner))
10254 {
10255 gdb_assert (is_hardware_watchpoint (loc2->owner));
10256 loc2->watchpoint_type = old_loc->watchpoint_type;
10257 }
10258
c7d46a38
PA
10259 if (loc2 != old_loc && should_be_inserted (loc2))
10260 {
10261 loc2->inserted = 1;
10262 loc2->target_info = old_loc->target_info;
10263 keep_in_target = 1;
10264 break;
10265 }
876fa593
JK
10266 }
10267 }
10268 }
74960c60
VP
10269 }
10270
20874c92
VP
10271 if (!keep_in_target)
10272 {
876fa593 10273 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10274 {
4a64f543
MS
10275 /* This is just about all we can do. We could keep
10276 this location on the global list, and try to
10277 remove it next time, but there's no particular
10278 reason why we will succeed next time.
20874c92 10279
4a64f543
MS
10280 Note that at this point, old_loc->owner is still
10281 valid, as delete_breakpoint frees the breakpoint
10282 only after calling us. */
3e43a32a
MS
10283 printf_filtered (_("warning: Error removing "
10284 "breakpoint %d\n"),
876fa593 10285 old_loc->owner->number);
20874c92
VP
10286 }
10287 removed = 1;
10288 }
0d381245 10289 }
74960c60
VP
10290
10291 if (!found_object)
1c5cfe86 10292 {
db82e815
PA
10293 if (removed && non_stop
10294 && breakpoint_address_is_meaningful (old_loc->owner)
10295 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10296 {
db82e815
PA
10297 /* This location was removed from the target. In
10298 non-stop mode, a race condition is possible where
10299 we've removed a breakpoint, but stop events for that
10300 breakpoint are already queued and will arrive later.
10301 We apply an heuristic to be able to distinguish such
10302 SIGTRAPs from other random SIGTRAPs: we keep this
10303 breakpoint location for a bit, and will retire it
10304 after we see some number of events. The theory here
10305 is that reporting of events should, "on the average",
10306 be fair, so after a while we'll see events from all
10307 threads that have anything of interest, and no longer
10308 need to keep this breakpoint location around. We
10309 don't hold locations forever so to reduce chances of
10310 mistaking a non-breakpoint SIGTRAP for a breakpoint
10311 SIGTRAP.
10312
10313 The heuristic failing can be disastrous on
10314 decr_pc_after_break targets.
10315
10316 On decr_pc_after_break targets, like e.g., x86-linux,
10317 if we fail to recognize a late breakpoint SIGTRAP,
10318 because events_till_retirement has reached 0 too
10319 soon, we'll fail to do the PC adjustment, and report
10320 a random SIGTRAP to the user. When the user resumes
10321 the inferior, it will most likely immediately crash
2dec564e 10322 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10323 corrupted, because of being resumed e.g., in the
10324 middle of a multi-byte instruction, or skipped a
10325 one-byte instruction. This was actually seen happen
10326 on native x86-linux, and should be less rare on
10327 targets that do not support new thread events, like
10328 remote, due to the heuristic depending on
10329 thread_count.
10330
10331 Mistaking a random SIGTRAP for a breakpoint trap
10332 causes similar symptoms (PC adjustment applied when
10333 it shouldn't), but then again, playing with SIGTRAPs
10334 behind the debugger's back is asking for trouble.
10335
10336 Since hardware watchpoint traps are always
10337 distinguishable from other traps, so we don't need to
10338 apply keep hardware watchpoint moribund locations
10339 around. We simply always ignore hardware watchpoint
10340 traps we can no longer explain. */
10341
876fa593
JK
10342 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10343 old_loc->owner = NULL;
20874c92 10344
876fa593 10345 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10346 }
10347 else
f431efe5
PA
10348 {
10349 old_loc->owner = NULL;
10350 decref_bp_location (&old_loc);
10351 }
20874c92 10352 }
74960c60 10353 }
1c5cfe86 10354
348d480f
PA
10355 /* Rescan breakpoints at the same address and section, marking the
10356 first one as "first" and any others as "duplicates". This is so
10357 that the bpt instruction is only inserted once. If we have a
10358 permanent breakpoint at the same place as BPT, make that one the
10359 official one, and the rest as duplicates. Permanent breakpoints
10360 are sorted first for the same address.
10361
10362 Do the same for hardware watchpoints, but also considering the
10363 watchpoint's type (regular/access/read) and length. */
10364
10365 bp_loc_first = NULL;
10366 wp_loc_first = NULL;
10367 awp_loc_first = NULL;
10368 rwp_loc_first = NULL;
10369 ALL_BP_LOCATIONS (loc, locp)
10370 {
10371 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10372 non-NULL. */
10373 struct breakpoint *b = loc->owner;
10374 struct bp_location **loc_first_p;
10375
10376 if (b->enable_state == bp_disabled
10377 || b->enable_state == bp_call_disabled
10378 || b->enable_state == bp_startup_disabled
10379 || !loc->enabled
10380 || loc->shlib_disabled
10381 || !breakpoint_address_is_meaningful (b)
10382 || is_tracepoint (b))
10383 continue;
10384
10385 /* Permanent breakpoint should always be inserted. */
10386 if (b->enable_state == bp_permanent && ! loc->inserted)
10387 internal_error (__FILE__, __LINE__,
10388 _("allegedly permanent breakpoint is not "
10389 "actually inserted"));
10390
10391 if (b->type == bp_hardware_watchpoint)
10392 loc_first_p = &wp_loc_first;
10393 else if (b->type == bp_read_watchpoint)
10394 loc_first_p = &rwp_loc_first;
10395 else if (b->type == bp_access_watchpoint)
10396 loc_first_p = &awp_loc_first;
10397 else
10398 loc_first_p = &bp_loc_first;
10399
10400 if (*loc_first_p == NULL
10401 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10402 || !breakpoint_locations_match (loc, *loc_first_p))
10403 {
10404 *loc_first_p = loc;
10405 loc->duplicate = 0;
10406 continue;
10407 }
10408
10409 loc->duplicate = 1;
10410
10411 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10412 && b->enable_state != bp_permanent)
10413 internal_error (__FILE__, __LINE__,
10414 _("another breakpoint was inserted on top of "
10415 "a permanent breakpoint"));
10416 }
10417
10418 if (breakpoints_always_inserted_mode () && should_insert
10419 && (have_live_inferiors ()
10420 || (gdbarch_has_global_breakpoints (target_gdbarch))))
10421 insert_breakpoint_locations ();
10422
10423 do_cleanups (cleanups);
10424}
10425
10426void
10427breakpoint_retire_moribund (void)
10428{
10429 struct bp_location *loc;
10430 int ix;
10431
10432 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10433 if (--(loc->events_till_retirement) == 0)
10434 {
10435 decref_bp_location (&loc);
10436 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10437 --ix;
10438 }
10439}
10440
10441static void
10442update_global_location_list_nothrow (int inserting)
10443{
10444 struct gdb_exception e;
10445
10446 TRY_CATCH (e, RETURN_MASK_ERROR)
10447 update_global_location_list (inserting);
10448}
10449
10450/* Clear BKP from a BPS. */
10451
10452static void
10453bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10454{
10455 bpstat bs;
10456
10457 for (bs = bps; bs; bs = bs->next)
10458 if (bs->breakpoint_at == bpt)
10459 {
10460 bs->breakpoint_at = NULL;
10461 bs->old_val = NULL;
10462 /* bs->commands will be freed later. */
10463 }
10464}
10465
10466/* Callback for iterate_over_threads. */
10467static int
10468bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10469{
10470 struct breakpoint *bpt = data;
10471
10472 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10473 return 0;
10474}
10475
10476/* Helper for breakpoint and tracepoint breakpoint_ops->mention
10477 callbacks. */
10478
10479static void
10480say_where (struct breakpoint *b)
10481{
10482 struct value_print_options opts;
10483
10484 get_user_print_options (&opts);
10485
10486 /* i18n: cagney/2005-02-11: Below needs to be merged into a
10487 single string. */
10488 if (b->loc == NULL)
10489 {
10490 printf_filtered (_(" (%s) pending."), b->addr_string);
10491 }
10492 else
10493 {
10494 if (opts.addressprint || b->source_file == NULL)
10495 {
10496 printf_filtered (" at ");
10497 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
10498 gdb_stdout);
10499 }
10500 if (b->source_file)
10501 printf_filtered (": file %s, line %d.",
10502 b->source_file, b->line_number);
10503
10504 if (b->loc->next)
10505 {
10506 struct bp_location *loc = b->loc;
10507 int n = 0;
10508 for (; loc; loc = loc->next)
10509 ++n;
10510 printf_filtered (" (%d locations)", n);
10511 }
10512 }
10513}
10514
348d480f
PA
10515/* Default bp_location_ops methods. */
10516
10517static void
10518bp_location_dtor (struct bp_location *self)
10519{
10520 xfree (self->cond);
10521 xfree (self->function_name);
10522}
10523
10524static const struct bp_location_ops bp_location_ops =
10525{
10526 bp_location_dtor
10527};
10528
2060206e
PA
10529/* Default breakpoint_ops methods all breakpoint_ops ultimately
10530 inherit from. */
348d480f 10531
2060206e
PA
10532static void
10533base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
10534{
10535 decref_counted_command_line (&self->commands);
10536 xfree (self->cond_string);
10537 xfree (self->cond_exp);
10538 xfree (self->addr_string);
10539 xfree (self->addr_string_range_end);
10540 xfree (self->exp);
10541 xfree (self->exp_string);
10542 xfree (self->exp_string_reparse);
10543 value_free (self->val);
10544 xfree (self->source_file);
10545}
10546
2060206e
PA
10547static struct bp_location *
10548base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
10549{
10550 struct bp_location *loc;
10551
10552 loc = XNEW (struct bp_location);
10553 init_bp_location (loc, &bp_location_ops, self);
10554 return loc;
10555}
10556
2060206e
PA
10557static void
10558base_breakpoint_re_set (struct breakpoint *b)
10559{
10560 /* Nothing to re-set. */
10561}
10562
10563#define internal_error_pure_virtual_called() \
10564 gdb_assert_not_reached ("pure virtual function called")
10565
10566static int
10567base_breakpoint_insert_location (struct bp_location *bl)
10568{
10569 internal_error_pure_virtual_called ();
10570}
10571
10572static int
10573base_breakpoint_remove_location (struct bp_location *bl)
10574{
10575 internal_error_pure_virtual_called ();
10576}
10577
10578static int
10579base_breakpoint_breakpoint_hit (const struct bp_location *bl,
10580 struct address_space *aspace,
10581 CORE_ADDR bp_addr)
10582{
10583 internal_error_pure_virtual_called ();
10584}
10585
10586static void
10587base_breakpoint_check_status (bpstat bs)
10588{
10589 /* Always stop. */
10590}
10591
10592/* A "works_in_software_mode" breakpoint_ops method that just internal
10593 errors. */
10594
10595static int
10596base_breakpoint_works_in_software_mode (const struct breakpoint *b)
10597{
10598 internal_error_pure_virtual_called ();
10599}
10600
10601/* A "resources_needed" breakpoint_ops method that just internal
10602 errors. */
10603
10604static int
10605base_breakpoint_resources_needed (const struct bp_location *bl)
10606{
10607 internal_error_pure_virtual_called ();
10608}
10609
10610static enum print_stop_action
10611base_breakpoint_print_it (bpstat bs)
10612{
10613 internal_error_pure_virtual_called ();
10614}
10615
10616static void
10617base_breakpoint_print_one_detail (const struct breakpoint *self,
10618 struct ui_out *uiout)
10619{
10620 /* nothing */
10621}
10622
10623static void
10624base_breakpoint_print_mention (struct breakpoint *b)
10625{
10626 internal_error_pure_virtual_called ();
10627}
10628
10629static void
10630base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
10631{
10632 internal_error_pure_virtual_called ();
10633}
10634
10635static struct breakpoint_ops base_breakpoint_ops =
10636{
10637 base_breakpoint_dtor,
10638 base_breakpoint_allocate_location,
10639 base_breakpoint_re_set,
10640 base_breakpoint_insert_location,
10641 base_breakpoint_remove_location,
10642 base_breakpoint_breakpoint_hit,
10643 base_breakpoint_check_status,
10644 base_breakpoint_resources_needed,
10645 base_breakpoint_works_in_software_mode,
10646 base_breakpoint_print_it,
10647 NULL,
10648 base_breakpoint_print_one_detail,
10649 base_breakpoint_print_mention,
10650 base_breakpoint_print_recreate
10651};
10652
10653/* Default breakpoint_ops methods. */
10654
10655static void
348d480f
PA
10656bkpt_re_set (struct breakpoint *b)
10657{
06edf0c0
PA
10658 /* Do not attempt to re-set breakpoints disabled during startup. */
10659 if (b->enable_state == bp_startup_disabled)
10660 return;
348d480f 10661
06edf0c0
PA
10662 /* FIXME: is this still reachable? */
10663 if (b->addr_string == NULL)
10664 {
10665 /* Anything without a string can't be re-set. */
348d480f 10666 delete_breakpoint (b);
06edf0c0 10667 return;
348d480f 10668 }
06edf0c0
PA
10669
10670 breakpoint_re_set_default (b);
348d480f
PA
10671}
10672
2060206e 10673static int
348d480f
PA
10674bkpt_insert_location (struct bp_location *bl)
10675{
10676 if (bl->loc_type == bp_loc_hardware_breakpoint)
10677 return target_insert_hw_breakpoint (bl->gdbarch,
10678 &bl->target_info);
10679 else
10680 return target_insert_breakpoint (bl->gdbarch,
10681 &bl->target_info);
10682}
10683
2060206e 10684static int
348d480f
PA
10685bkpt_remove_location (struct bp_location *bl)
10686{
10687 if (bl->loc_type == bp_loc_hardware_breakpoint)
10688 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
10689 else
10690 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
10691}
10692
2060206e 10693static int
348d480f
PA
10694bkpt_breakpoint_hit (const struct bp_location *bl,
10695 struct address_space *aspace, CORE_ADDR bp_addr)
10696{
10697 struct breakpoint *b = bl->owner;
10698
10699 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
10700 aspace, bp_addr))
10701 return 0;
10702
10703 if (overlay_debugging /* unmapped overlay section */
10704 && section_is_overlay (bl->section)
10705 && !section_is_mapped (bl->section))
10706 return 0;
10707
10708 return 1;
10709}
10710
2060206e 10711static int
348d480f
PA
10712bkpt_resources_needed (const struct bp_location *bl)
10713{
10714 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
10715
10716 return 1;
10717}
10718
2060206e 10719static enum print_stop_action
348d480f
PA
10720bkpt_print_it (bpstat bs)
10721{
348d480f
PA
10722 struct breakpoint *b;
10723 const struct bp_location *bl;
001c8c33 10724 int bp_temp;
348d480f
PA
10725
10726 gdb_assert (bs->bp_location_at != NULL);
10727
10728 bl = bs->bp_location_at;
10729 b = bs->breakpoint_at;
10730
001c8c33
PA
10731 bp_temp = b->disposition == disp_del;
10732 if (bl->address != bl->requested_address)
10733 breakpoint_adjustment_warning (bl->requested_address,
10734 bl->address,
10735 b->number, 1);
10736 annotate_breakpoint (b->number);
10737 if (bp_temp)
10738 ui_out_text (uiout, "\nTemporary breakpoint ");
10739 else
10740 ui_out_text (uiout, "\nBreakpoint ");
10741 if (ui_out_is_mi_like_p (uiout))
348d480f 10742 {
001c8c33
PA
10743 ui_out_field_string (uiout, "reason",
10744 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10745 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 10746 }
001c8c33
PA
10747 ui_out_field_int (uiout, "bkptno", b->number);
10748 ui_out_text (uiout, ", ");
06edf0c0 10749
001c8c33 10750 return PRINT_SRC_AND_LOC;
06edf0c0
PA
10751}
10752
2060206e 10753static void
06edf0c0
PA
10754bkpt_print_mention (struct breakpoint *b)
10755{
10756 if (ui_out_is_mi_like_p (uiout))
10757 return;
10758
10759 switch (b->type)
10760 {
10761 case bp_breakpoint:
10762 case bp_gnu_ifunc_resolver:
10763 if (b->disposition == disp_del)
10764 printf_filtered (_("Temporary breakpoint"));
10765 else
10766 printf_filtered (_("Breakpoint"));
10767 printf_filtered (_(" %d"), b->number);
10768 if (b->type == bp_gnu_ifunc_resolver)
10769 printf_filtered (_(" at gnu-indirect-function resolver"));
10770 break;
10771 case bp_hardware_breakpoint:
10772 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
10773 break;
10774 }
10775
10776 say_where (b);
10777}
10778
2060206e 10779static void
06edf0c0
PA
10780bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
10781{
10782 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
10783 fprintf_unfiltered (fp, "tbreak");
10784 else if (tp->type == bp_breakpoint)
10785 fprintf_unfiltered (fp, "break");
10786 else if (tp->type == bp_hardware_breakpoint
10787 && tp->disposition == disp_del)
10788 fprintf_unfiltered (fp, "thbreak");
10789 else if (tp->type == bp_hardware_breakpoint)
10790 fprintf_unfiltered (fp, "hbreak");
10791 else
10792 internal_error (__FILE__, __LINE__,
10793 _("unhandled breakpoint type %d"), (int) tp->type);
10794
2060206e 10795 fprintf_unfiltered (fp, " %s", tp->addr_string);
06edf0c0
PA
10796}
10797
06edf0c0
PA
10798/* Virtual table for internal breakpoints. */
10799
10800static void
10801internal_bkpt_re_set (struct breakpoint *b)
10802{
10803 switch (b->type)
10804 {
10805 /* Delete overlay event and longjmp master breakpoints; they
10806 will be reset later by breakpoint_re_set. */
10807 case bp_overlay_event:
10808 case bp_longjmp_master:
10809 case bp_std_terminate_master:
10810 case bp_exception_master:
10811 delete_breakpoint (b);
10812 break;
10813
10814 /* This breakpoint is special, it's set up when the inferior
10815 starts and we really don't want to touch it. */
10816 case bp_shlib_event:
10817
10818 /* Like bp_shlib_event, this breakpoint type is special. Once
10819 it is set up, we do not want to touch it. */
10820 case bp_thread_event:
10821 break;
10822 }
10823}
10824
10825static void
10826internal_bkpt_check_status (bpstat bs)
10827{
10828 /* We do not stop for these. */
10829 bs->stop = 0;
10830}
10831
10832static enum print_stop_action
10833internal_bkpt_print_it (bpstat bs)
10834{
06edf0c0 10835 struct breakpoint *b;
06edf0c0 10836
06edf0c0
PA
10837 b = bs->breakpoint_at;
10838
06edf0c0
PA
10839 switch (b->type)
10840 {
348d480f
PA
10841 case bp_shlib_event:
10842 /* Did we stop because the user set the stop_on_solib_events
10843 variable? (If so, we report this as a generic, "Stopped due
10844 to shlib event" message.) */
10845 printf_filtered (_("Stopped due to shared library event\n"));
348d480f
PA
10846 break;
10847
10848 case bp_thread_event:
10849 /* Not sure how we will get here.
10850 GDB should not stop for these breakpoints. */
10851 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
10852 break;
10853
10854 case bp_overlay_event:
10855 /* By analogy with the thread event, GDB should not stop for these. */
10856 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
10857 break;
10858
10859 case bp_longjmp_master:
10860 /* These should never be enabled. */
10861 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
10862 break;
10863
10864 case bp_std_terminate_master:
10865 /* These should never be enabled. */
10866 printf_filtered (_("std::terminate Master Breakpoint: "
10867 "gdb should not stop!\n"));
348d480f
PA
10868 break;
10869
10870 case bp_exception_master:
10871 /* These should never be enabled. */
10872 printf_filtered (_("Exception Master Breakpoint: "
10873 "gdb should not stop!\n"));
06edf0c0
PA
10874 break;
10875 }
10876
001c8c33 10877 return PRINT_NOTHING;
06edf0c0
PA
10878}
10879
10880static void
10881internal_bkpt_print_mention (struct breakpoint *b)
10882{
10883 /* Nothing to mention. These breakpoints are internal. */
10884}
10885
06edf0c0
PA
10886/* Virtual table for momentary breakpoints */
10887
10888static void
10889momentary_bkpt_re_set (struct breakpoint *b)
10890{
10891 /* Keep temporary breakpoints, which can be encountered when we step
10892 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
10893 Otherwise these should have been blown away via the cleanup chain
10894 or by breakpoint_init_inferior when we rerun the executable. */
10895}
10896
10897static void
10898momentary_bkpt_check_status (bpstat bs)
10899{
10900 /* Nothing. The point of these breakpoints is causing a stop. */
10901}
10902
10903static enum print_stop_action
10904momentary_bkpt_print_it (bpstat bs)
10905{
001c8c33 10906 if (ui_out_is_mi_like_p (uiout))
06edf0c0 10907 {
001c8c33 10908 struct breakpoint *b = bs->breakpoint_at;
348d480f 10909
001c8c33
PA
10910 switch (b->type)
10911 {
10912 case bp_finish:
10913 ui_out_field_string
10914 (uiout, "reason",
10915 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
10916 break;
348d480f 10917
001c8c33
PA
10918 case bp_until:
10919 ui_out_field_string
10920 (uiout, "reason",
10921 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
10922 break;
10923 }
348d480f
PA
10924 }
10925
001c8c33 10926 return PRINT_UNKNOWN;
348d480f
PA
10927}
10928
06edf0c0
PA
10929static void
10930momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 10931{
06edf0c0 10932 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
10933}
10934
348d480f 10935/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 10936
348d480f
PA
10937static void
10938tracepoint_re_set (struct breakpoint *b)
10939{
10940 breakpoint_re_set_default (b);
10941}
876fa593 10942
348d480f
PA
10943static int
10944tracepoint_breakpoint_hit (const struct bp_location *bl,
10945 struct address_space *aspace, CORE_ADDR bp_addr)
10946{
10947 /* By definition, the inferior does not report stops at
10948 tracepoints. */
10949 return 0;
74960c60
VP
10950}
10951
10952static void
348d480f
PA
10953tracepoint_print_one_detail (const struct breakpoint *self,
10954 struct ui_out *uiout)
74960c60 10955{
348d480f
PA
10956 if (self->static_trace_marker_id)
10957 {
10958 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 10959
348d480f
PA
10960 ui_out_text (uiout, "\tmarker id is ");
10961 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
10962 self->static_trace_marker_id);
10963 ui_out_text (uiout, "\n");
10964 }
0d381245
VP
10965}
10966
a474d7c2 10967static void
348d480f 10968tracepoint_print_mention (struct breakpoint *b)
a474d7c2 10969{
348d480f
PA
10970 if (ui_out_is_mi_like_p (uiout))
10971 return;
cc59ec59 10972
348d480f
PA
10973 switch (b->type)
10974 {
10975 case bp_tracepoint:
10976 printf_filtered (_("Tracepoint"));
10977 printf_filtered (_(" %d"), b->number);
10978 break;
10979 case bp_fast_tracepoint:
10980 printf_filtered (_("Fast tracepoint"));
10981 printf_filtered (_(" %d"), b->number);
10982 break;
10983 case bp_static_tracepoint:
10984 printf_filtered (_("Static tracepoint"));
10985 printf_filtered (_(" %d"), b->number);
10986 break;
10987 default:
10988 internal_error (__FILE__, __LINE__,
10989 _("unhandled tracepoint type %d"), (int) b->type);
10990 }
10991
10992 say_where (b);
a474d7c2
PA
10993}
10994
348d480f
PA
10995static void
10996tracepoint_print_recreate (struct breakpoint *tp, struct ui_file *fp)
a474d7c2 10997{
348d480f
PA
10998 if (tp->type == bp_fast_tracepoint)
10999 fprintf_unfiltered (fp, "ftrace");
11000 if (tp->type == bp_static_tracepoint)
11001 fprintf_unfiltered (fp, "strace");
11002 else if (tp->type == bp_tracepoint)
11003 fprintf_unfiltered (fp, "trace");
11004 else
11005 internal_error (__FILE__, __LINE__,
11006 _("unhandled tracepoint type %d"), (int) tp->type);
cc59ec59 11007
348d480f 11008 fprintf_unfiltered (fp, " %s", tp->addr_string);
a474d7c2
PA
11009}
11010
2060206e 11011struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 11012
53a5351d 11013/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 11014 structures. */
c906108c
SS
11015
11016void
fba45db2 11017delete_breakpoint (struct breakpoint *bpt)
c906108c 11018{
52f0bd74 11019 struct breakpoint *b;
c906108c 11020
8a3fe4f8 11021 gdb_assert (bpt != NULL);
c906108c 11022
4a64f543
MS
11023 /* Has this bp already been deleted? This can happen because
11024 multiple lists can hold pointers to bp's. bpstat lists are
11025 especial culprits.
11026
11027 One example of this happening is a watchpoint's scope bp. When
11028 the scope bp triggers, we notice that the watchpoint is out of
11029 scope, and delete it. We also delete its scope bp. But the
11030 scope bp is marked "auto-deleting", and is already on a bpstat.
11031 That bpstat is then checked for auto-deleting bp's, which are
11032 deleted.
11033
11034 A real solution to this problem might involve reference counts in
11035 bp's, and/or giving them pointers back to their referencing
11036 bpstat's, and teaching delete_breakpoint to only free a bp's
11037 storage when no more references were extent. A cheaper bandaid
11038 was chosen. */
c906108c
SS
11039 if (bpt->type == bp_none)
11040 return;
11041
4a64f543
MS
11042 /* At least avoid this stale reference until the reference counting
11043 of breakpoints gets resolved. */
d0fb5eae 11044 if (bpt->related_breakpoint != bpt)
e5a0a904 11045 {
d0fb5eae
JK
11046 struct breakpoint *related;
11047
11048 if (bpt->type == bp_watchpoint_scope)
11049 watchpoint_del_at_next_stop (bpt->related_breakpoint);
11050 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
11051 watchpoint_del_at_next_stop (bpt);
11052
11053 /* Unlink bpt from the bpt->related_breakpoint ring. */
11054 for (related = bpt; related->related_breakpoint != bpt;
11055 related = related->related_breakpoint);
11056 related->related_breakpoint = bpt->related_breakpoint;
11057 bpt->related_breakpoint = bpt;
e5a0a904
JK
11058 }
11059
a9634178
TJB
11060 /* watch_command_1 creates a watchpoint but only sets its number if
11061 update_watchpoint succeeds in creating its bp_locations. If there's
11062 a problem in that process, we'll be asked to delete the half-created
11063 watchpoint. In that case, don't announce the deletion. */
11064 if (bpt->number)
11065 observer_notify_breakpoint_deleted (bpt);
c906108c 11066
c906108c
SS
11067 if (breakpoint_chain == bpt)
11068 breakpoint_chain = bpt->next;
11069
c906108c
SS
11070 ALL_BREAKPOINTS (b)
11071 if (b->next == bpt)
c5aa993b
JM
11072 {
11073 b->next = bpt->next;
11074 break;
11075 }
c906108c 11076
f431efe5
PA
11077 /* Be sure no bpstat's are pointing at the breakpoint after it's
11078 been freed. */
11079 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 11080 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
11081 pointing at bpt from the stop_bpstat list entirely, as breakpoint
11082 commands are associated with the bpstat; if we remove it here,
11083 then the later call to bpstat_do_actions (&stop_bpstat); in
11084 event-top.c won't do anything, and temporary breakpoints with
11085 commands won't work. */
11086
11087 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11088
4a64f543
MS
11089 /* Now that breakpoint is removed from breakpoint list, update the
11090 global location list. This will remove locations that used to
11091 belong to this breakpoint. Do this before freeing the breakpoint
11092 itself, since remove_breakpoint looks at location's owner. It
11093 might be better design to have location completely
11094 self-contained, but it's not the case now. */
b60e7edf 11095 update_global_location_list (0);
74960c60 11096
348d480f 11097 bpt->ops->dtor (bpt);
4a64f543
MS
11098 /* On the chance that someone will soon try again to delete this
11099 same bp, we mark it as deleted before freeing its storage. */
c906108c 11100 bpt->type = bp_none;
b8c9b27d 11101 xfree (bpt);
c906108c
SS
11102}
11103
4d6140d9
AC
11104static void
11105do_delete_breakpoint_cleanup (void *b)
11106{
11107 delete_breakpoint (b);
11108}
11109
11110struct cleanup *
11111make_cleanup_delete_breakpoint (struct breakpoint *b)
11112{
11113 return make_cleanup (do_delete_breakpoint_cleanup, b);
11114}
11115
51be5b68
PA
11116/* Iterator function to call a user-provided callback function once
11117 for each of B and its related breakpoints. */
11118
11119static void
11120iterate_over_related_breakpoints (struct breakpoint *b,
11121 void (*function) (struct breakpoint *,
11122 void *),
11123 void *data)
11124{
11125 struct breakpoint *related;
11126
11127 related = b;
11128 do
11129 {
11130 struct breakpoint *next;
11131
11132 /* FUNCTION may delete RELATED. */
11133 next = related->related_breakpoint;
11134
11135 if (next == related)
11136 {
11137 /* RELATED is the last ring entry. */
11138 function (related, data);
11139
11140 /* FUNCTION may have deleted it, so we'd never reach back to
11141 B. There's nothing left to do anyway, so just break
11142 out. */
11143 break;
11144 }
11145 else
11146 function (related, data);
11147
11148 related = next;
11149 }
11150 while (related != b);
11151}
95a42b64
TT
11152
11153static void
11154do_delete_breakpoint (struct breakpoint *b, void *ignore)
11155{
11156 delete_breakpoint (b);
11157}
11158
51be5b68
PA
11159/* A callback for map_breakpoint_numbers that calls
11160 delete_breakpoint. */
11161
11162static void
11163do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
11164{
11165 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
11166}
11167
c906108c 11168void
fba45db2 11169delete_command (char *arg, int from_tty)
c906108c 11170{
35df4500 11171 struct breakpoint *b, *b_tmp;
c906108c 11172
ea9365bb
TT
11173 dont_repeat ();
11174
c906108c
SS
11175 if (arg == 0)
11176 {
11177 int breaks_to_delete = 0;
11178
46c6471b
PA
11179 /* Delete all breakpoints if no argument. Do not delete
11180 internal breakpoints, these have to be deleted with an
11181 explicit breakpoint number argument. */
c5aa993b 11182 ALL_BREAKPOINTS (b)
46c6471b 11183 if (user_breakpoint_p (b))
973d738b
DJ
11184 {
11185 breaks_to_delete = 1;
11186 break;
11187 }
c906108c
SS
11188
11189 /* Ask user only if there are some breakpoints to delete. */
11190 if (!from_tty
e2e0b3e5 11191 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 11192 {
35df4500 11193 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 11194 if (user_breakpoint_p (b))
c5aa993b 11195 delete_breakpoint (b);
c906108c
SS
11196 }
11197 }
11198 else
51be5b68 11199 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
11200}
11201
0d381245
VP
11202static int
11203all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 11204{
0d381245
VP
11205 for (; loc; loc = loc->next)
11206 if (!loc->shlib_disabled)
11207 return 0;
11208 return 1;
fe3f5fa8
VP
11209}
11210
776592bf
DE
11211/* Subroutine of update_breakpoint_locations to simplify it.
11212 Return non-zero if multiple fns in list LOC have the same name.
11213 Null names are ignored. */
11214
11215static int
11216ambiguous_names_p (struct bp_location *loc)
11217{
11218 struct bp_location *l;
11219 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
11220 (int (*) (const void *,
11221 const void *)) streq,
776592bf
DE
11222 NULL, xcalloc, xfree);
11223
11224 for (l = loc; l != NULL; l = l->next)
11225 {
11226 const char **slot;
11227 const char *name = l->function_name;
11228
11229 /* Allow for some names to be NULL, ignore them. */
11230 if (name == NULL)
11231 continue;
11232
11233 slot = (const char **) htab_find_slot (htab, (const void *) name,
11234 INSERT);
4a64f543
MS
11235 /* NOTE: We can assume slot != NULL here because xcalloc never
11236 returns NULL. */
776592bf
DE
11237 if (*slot != NULL)
11238 {
11239 htab_delete (htab);
11240 return 1;
11241 }
11242 *slot = name;
11243 }
11244
11245 htab_delete (htab);
11246 return 0;
11247}
11248
0fb4aa4b
PA
11249/* When symbols change, it probably means the sources changed as well,
11250 and it might mean the static tracepoint markers are no longer at
11251 the same address or line numbers they used to be at last we
11252 checked. Losing your static tracepoints whenever you rebuild is
11253 undesirable. This function tries to resync/rematch gdb static
11254 tracepoints with the markers on the target, for static tracepoints
11255 that have not been set by marker id. Static tracepoint that have
11256 been set by marker id are reset by marker id in breakpoint_re_set.
11257 The heuristic is:
11258
11259 1) For a tracepoint set at a specific address, look for a marker at
11260 the old PC. If one is found there, assume to be the same marker.
11261 If the name / string id of the marker found is different from the
11262 previous known name, assume that means the user renamed the marker
11263 in the sources, and output a warning.
11264
11265 2) For a tracepoint set at a given line number, look for a marker
11266 at the new address of the old line number. If one is found there,
11267 assume to be the same marker. If the name / string id of the
11268 marker found is different from the previous known name, assume that
11269 means the user renamed the marker in the sources, and output a
11270 warning.
11271
11272 3) If a marker is no longer found at the same address or line, it
11273 may mean the marker no longer exists. But it may also just mean
11274 the code changed a bit. Maybe the user added a few lines of code
11275 that made the marker move up or down (in line number terms). Ask
11276 the target for info about the marker with the string id as we knew
11277 it. If found, update line number and address in the matching
11278 static tracepoint. This will get confused if there's more than one
11279 marker with the same ID (possible in UST, although unadvised
11280 precisely because it confuses tools). */
11281
11282static struct symtab_and_line
11283update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11284{
11285 struct static_tracepoint_marker marker;
11286 CORE_ADDR pc;
11287 int i;
11288
11289 pc = sal.pc;
11290 if (sal.line)
11291 find_line_pc (sal.symtab, sal.line, &pc);
11292
11293 if (target_static_tracepoint_marker_at (pc, &marker))
11294 {
11295 if (strcmp (b->static_trace_marker_id, marker.str_id) != 0)
11296 warning (_("static tracepoint %d changed probed marker from %s to %s"),
11297 b->number,
11298 b->static_trace_marker_id, marker.str_id);
11299
11300 xfree (b->static_trace_marker_id);
11301 b->static_trace_marker_id = xstrdup (marker.str_id);
11302 release_static_tracepoint_marker (&marker);
11303
11304 return sal;
11305 }
11306
11307 /* Old marker wasn't found on target at lineno. Try looking it up
11308 by string ID. */
11309 if (!sal.explicit_pc
11310 && sal.line != 0
11311 && sal.symtab != NULL
11312 && b->static_trace_marker_id != NULL)
11313 {
11314 VEC(static_tracepoint_marker_p) *markers;
11315
11316 markers
11317 = target_static_tracepoint_markers_by_strid (b->static_trace_marker_id);
11318
11319 if (!VEC_empty(static_tracepoint_marker_p, markers))
11320 {
11321 struct symtab_and_line sal;
11322 struct symbol *sym;
11323 struct static_tracepoint_marker *marker;
11324
11325 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
11326
11327 xfree (b->static_trace_marker_id);
11328 b->static_trace_marker_id = xstrdup (marker->str_id);
11329
11330 warning (_("marker for static tracepoint %d (%s) not "
11331 "found at previous line number"),
11332 b->number, b->static_trace_marker_id);
11333
11334 init_sal (&sal);
11335
11336 sal.pc = marker->address;
11337
11338 sal = find_pc_line (marker->address, 0);
11339 sym = find_pc_sect_function (marker->address, NULL);
11340 ui_out_text (uiout, "Now in ");
11341 if (sym)
11342 {
11343 ui_out_field_string (uiout, "func",
11344 SYMBOL_PRINT_NAME (sym));
11345 ui_out_text (uiout, " at ");
11346 }
11347 ui_out_field_string (uiout, "file", sal.symtab->filename);
11348 ui_out_text (uiout, ":");
11349
11350 if (ui_out_is_mi_like_p (uiout))
11351 {
11352 char *fullname = symtab_to_fullname (sal.symtab);
11353
11354 if (fullname)
11355 ui_out_field_string (uiout, "fullname", fullname);
11356 }
11357
11358 ui_out_field_int (uiout, "line", sal.line);
11359 ui_out_text (uiout, "\n");
11360
11361 b->line_number = sal.line;
11362
11363 xfree (b->source_file);
11364 if (sym)
11365 b->source_file = xstrdup (sal.symtab->filename);
11366 else
11367 b->source_file = NULL;
11368
11369 xfree (b->addr_string);
11370 b->addr_string = xstrprintf ("%s:%d",
11371 sal.symtab->filename, b->line_number);
11372
11373 /* Might be nice to check if function changed, and warn if
11374 so. */
11375
11376 release_static_tracepoint_marker (marker);
11377 }
11378 }
11379 return sal;
11380}
11381
8d3788bd
VP
11382/* Returns 1 iff locations A and B are sufficiently same that
11383 we don't need to report breakpoint as changed. */
11384
11385static int
11386locations_are_equal (struct bp_location *a, struct bp_location *b)
11387{
11388 while (a && b)
11389 {
11390 if (a->address != b->address)
11391 return 0;
11392
11393 if (a->shlib_disabled != b->shlib_disabled)
11394 return 0;
11395
11396 if (a->enabled != b->enabled)
11397 return 0;
11398
11399 a = a->next;
11400 b = b->next;
11401 }
11402
11403 if ((a == NULL) != (b == NULL))
11404 return 0;
11405
11406 return 1;
11407}
11408
f1310107
TJB
11409/* Create new breakpoint locations for B (a hardware or software breakpoint)
11410 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
11411 a ranged breakpoint. */
11412
0e30163f 11413void
0d381245 11414update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
11415 struct symtabs_and_lines sals,
11416 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
11417{
11418 int i;
0d381245
VP
11419 struct bp_location *existing_locations = b->loc;
11420
f1310107
TJB
11421 /* Ranged breakpoints have only one start location and one end location. */
11422 gdb_assert (sals_end.nelts == 0 || (sals.nelts == 1 && sals_end.nelts == 1));
11423
4a64f543
MS
11424 /* If there's no new locations, and all existing locations are
11425 pending, don't do anything. This optimizes the common case where
11426 all locations are in the same shared library, that was unloaded.
11427 We'd like to retain the location, so that when the library is
11428 loaded again, we don't loose the enabled/disabled status of the
11429 individual locations. */
0d381245 11430 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
11431 return;
11432
fe3f5fa8
VP
11433 b->loc = NULL;
11434
0d381245 11435 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 11436 {
0d381245 11437 struct bp_location *new_loc =
39d61571 11438 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 11439
0d381245
VP
11440 /* Reparse conditions, they might contain references to the
11441 old symtab. */
11442 if (b->cond_string != NULL)
11443 {
f1310107 11444 char *s;
0d381245 11445 struct gdb_exception e;
fe3f5fa8 11446
0d381245
VP
11447 s = b->cond_string;
11448 TRY_CATCH (e, RETURN_MASK_ERROR)
11449 {
11450 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
11451 0);
11452 }
11453 if (e.reason < 0)
11454 {
3e43a32a
MS
11455 warning (_("failed to reevaluate condition "
11456 "for breakpoint %d: %s"),
0d381245
VP
11457 b->number, e.message);
11458 new_loc->enabled = 0;
11459 }
11460 }
fe3f5fa8 11461
0d381245
VP
11462 if (b->source_file != NULL)
11463 xfree (b->source_file);
11464 if (sals.sals[i].symtab == NULL)
11465 b->source_file = NULL;
11466 else
1b36a34b 11467 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 11468
0d381245
VP
11469 if (b->line_number == 0)
11470 b->line_number = sals.sals[i].line;
f1310107
TJB
11471
11472 if (sals_end.nelts)
11473 {
11474 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11475
11476 new_loc->length = end - sals.sals[0].pc + 1;
11477 }
0d381245 11478 }
fe3f5fa8 11479
514f746b
AR
11480 /* Update locations of permanent breakpoints. */
11481 if (b->enable_state == bp_permanent)
11482 make_breakpoint_permanent (b);
11483
4a64f543
MS
11484 /* If possible, carry over 'disable' status from existing
11485 breakpoints. */
0d381245
VP
11486 {
11487 struct bp_location *e = existing_locations;
776592bf
DE
11488 /* If there are multiple breakpoints with the same function name,
11489 e.g. for inline functions, comparing function names won't work.
11490 Instead compare pc addresses; this is just a heuristic as things
11491 may have moved, but in practice it gives the correct answer
11492 often enough until a better solution is found. */
11493 int have_ambiguous_names = ambiguous_names_p (b->loc);
11494
0d381245
VP
11495 for (; e; e = e->next)
11496 {
11497 if (!e->enabled && e->function_name)
11498 {
11499 struct bp_location *l = b->loc;
776592bf
DE
11500 if (have_ambiguous_names)
11501 {
11502 for (; l; l = l->next)
f1310107 11503 if (breakpoint_locations_match (e, l))
776592bf
DE
11504 {
11505 l->enabled = 0;
11506 break;
11507 }
11508 }
11509 else
11510 {
11511 for (; l; l = l->next)
11512 if (l->function_name
11513 && strcmp (e->function_name, l->function_name) == 0)
11514 {
11515 l->enabled = 0;
11516 break;
11517 }
11518 }
0d381245
VP
11519 }
11520 }
11521 }
fe3f5fa8 11522
8d3788bd
VP
11523 if (!locations_are_equal (existing_locations, b->loc))
11524 observer_notify_breakpoint_modified (b);
11525
b60e7edf 11526 update_global_location_list (1);
fe3f5fa8
VP
11527}
11528
ef23e705
TJB
11529/* Find the SaL locations corresponding to the given ADDR_STRING.
11530 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
11531
11532static struct symtabs_and_lines
11533addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11534{
11535 char *s;
58438ac1 11536 int marker_spec;
02d20e4a 11537 struct symtabs_and_lines sals = {0};
ef23e705
TJB
11538 struct gdb_exception e;
11539
11540 s = addr_string;
11541 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11542
11543 TRY_CATCH (e, RETURN_MASK_ERROR)
11544 {
11545 if (marker_spec)
11546 {
11547 sals = decode_static_tracepoint_spec (&s);
11548 if (sals.nelts > b->static_trace_marker_id_idx)
11549 {
11550 sals.sals[0] = sals.sals[b->static_trace_marker_id_idx];
11551 sals.nelts = 1;
11552 }
11553 else
11554 error (_("marker %s not found"), b->static_trace_marker_id);
11555 }
11556 else
58438ac1 11557 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, NULL);
ef23e705
TJB
11558 }
11559 if (e.reason < 0)
11560 {
11561 int not_found_and_ok = 0;
11562 /* For pending breakpoints, it's expected that parsing will
11563 fail until the right shared library is loaded. User has
11564 already told to create pending breakpoints and don't need
11565 extra messages. If breakpoint is in bp_shlib_disabled
11566 state, then user already saw the message about that
11567 breakpoint being disabled, and don't want to see more
11568 errors. */
58438ac1 11569 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
11570 && (b->condition_not_parsed
11571 || (b->loc && b->loc->shlib_disabled)
11572 || b->enable_state == bp_disabled))
11573 not_found_and_ok = 1;
11574
11575 if (!not_found_and_ok)
11576 {
11577 /* We surely don't want to warn about the same breakpoint
11578 10 times. One solution, implemented here, is disable
11579 the breakpoint on error. Another solution would be to
11580 have separate 'warning emitted' flag. Since this
11581 happens only when a binary has changed, I don't know
11582 which approach is better. */
11583 b->enable_state = bp_disabled;
11584 throw_exception (e);
11585 }
11586 }
11587
58438ac1 11588 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705
TJB
11589 {
11590 gdb_assert (sals.nelts == 1);
11591
11592 resolve_sal_pc (&sals.sals[0]);
11593 if (b->condition_not_parsed && s && s[0])
11594 {
11595 char *cond_string = 0;
11596 int thread = -1;
11597 int task = 0;
11598
11599 find_condition_and_thread (s, sals.sals[0].pc,
11600 &cond_string, &thread, &task);
11601 if (cond_string)
11602 b->cond_string = cond_string;
11603 b->thread = thread;
11604 b->task = task;
11605 b->condition_not_parsed = 0;
11606 }
11607
11608 if (b->type == bp_static_tracepoint && !marker_spec)
11609 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 11610
58438ac1
TT
11611 *found = 1;
11612 }
11613 else
11614 *found = 0;
ef23e705
TJB
11615
11616 return sals;
11617}
11618
348d480f
PA
11619/* The default re_set method, for typical hardware or software
11620 breakpoints. Reevaluate the breakpoint and recreate its
11621 locations. */
11622
11623static void
28010a5d 11624breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
11625{
11626 int found;
f1310107 11627 struct symtabs_and_lines sals, sals_end;
ef23e705 11628 struct symtabs_and_lines expanded = {0};
f1310107 11629 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
11630
11631 sals = addr_string_to_sals (b, b->addr_string, &found);
11632 if (found)
11633 {
11634 make_cleanup (xfree, sals.sals);
11635 expanded = expand_line_sal_maybe (sals.sals[0]);
11636 }
11637
f1310107
TJB
11638 if (b->addr_string_range_end)
11639 {
11640 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11641 if (found)
11642 {
11643 make_cleanup (xfree, sals_end.sals);
11644 expanded_end = expand_line_sal_maybe (sals_end.sals[0]);
11645 }
11646 }
11647
11648 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
11649}
11650
11651/* Prepare the global context for a re-set of breakpoint B. */
11652
11653static struct cleanup *
11654prepare_re_set_context (struct breakpoint *b)
11655{
11656 struct cleanup *cleanups;
11657
11658 input_radix = b->input_radix;
11659 cleanups = save_current_space_and_thread ();
11660 switch_to_program_space_and_thread (b->pspace);
11661 set_language (b->language);
11662
11663 return cleanups;
ef23e705
TJB
11664}
11665
c906108c
SS
11666/* Reset a breakpoint given it's struct breakpoint * BINT.
11667 The value we return ends up being the return value from catch_errors.
11668 Unused in this case. */
11669
11670static int
4efb68b1 11671breakpoint_re_set_one (void *bint)
c906108c 11672{
4a64f543 11673 /* Get past catch_errs. */
53a5351d 11674 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 11675 struct cleanup *cleanups;
c906108c 11676
348d480f
PA
11677 cleanups = prepare_re_set_context (b);
11678 b->ops->re_set (b);
11679 do_cleanups (cleanups);
c906108c
SS
11680 return 0;
11681}
11682
69de3c6a 11683/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 11684void
69de3c6a 11685breakpoint_re_set (void)
c906108c 11686{
35df4500 11687 struct breakpoint *b, *b_tmp;
c906108c
SS
11688 enum language save_language;
11689 int save_input_radix;
6c95b8df 11690 struct cleanup *old_chain;
c5aa993b 11691
c906108c
SS
11692 save_language = current_language->la_language;
11693 save_input_radix = input_radix;
6c95b8df
PA
11694 old_chain = save_current_program_space ();
11695
35df4500 11696 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11697 {
4a64f543 11698 /* Format possible error msg. */
fe3f5fa8 11699 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
11700 b->number);
11701 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 11702 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 11703 do_cleanups (cleanups);
c5aa993b 11704 }
c906108c
SS
11705 set_language (save_language);
11706 input_radix = save_input_radix;
e62c965a 11707
0756c555 11708 jit_breakpoint_re_set ();
4efc6507 11709
6c95b8df
PA
11710 do_cleanups (old_chain);
11711
af02033e
PP
11712 create_overlay_event_breakpoint ();
11713 create_longjmp_master_breakpoint ();
11714 create_std_terminate_master_breakpoint ();
186c406b 11715 create_exception_master_breakpoint ();
c906108c
SS
11716}
11717\f
c906108c
SS
11718/* Reset the thread number of this breakpoint:
11719
11720 - If the breakpoint is for all threads, leave it as-is.
4a64f543 11721 - Else, reset it to the current thread for inferior_ptid. */
c906108c 11722void
fba45db2 11723breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
11724{
11725 if (b->thread != -1)
11726 {
39f77062
KB
11727 if (in_thread_list (inferior_ptid))
11728 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
11729
11730 /* We're being called after following a fork. The new fork is
11731 selected as current, and unless this was a vfork will have a
11732 different program space from the original thread. Reset that
11733 as well. */
11734 b->loc->pspace = current_program_space;
c906108c
SS
11735 }
11736}
11737
03ac34d5
MS
11738/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11739 If from_tty is nonzero, it prints a message to that effect,
11740 which ends with a period (no newline). */
11741
c906108c 11742void
fba45db2 11743set_ignore_count (int bptnum, int count, int from_tty)
c906108c 11744{
52f0bd74 11745 struct breakpoint *b;
c906108c
SS
11746
11747 if (count < 0)
11748 count = 0;
11749
11750 ALL_BREAKPOINTS (b)
11751 if (b->number == bptnum)
c5aa993b 11752 {
d77f58be
SS
11753 if (is_tracepoint (b))
11754 {
11755 if (from_tty && count != 0)
11756 printf_filtered (_("Ignore count ignored for tracepoint %d."),
11757 bptnum);
11758 return;
11759 }
11760
c5aa993b 11761 b->ignore_count = count;
221ea385
KS
11762 if (from_tty)
11763 {
11764 if (count == 0)
3e43a32a
MS
11765 printf_filtered (_("Will stop next time "
11766 "breakpoint %d is reached."),
221ea385
KS
11767 bptnum);
11768 else if (count == 1)
a3f17187 11769 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
11770 bptnum);
11771 else
3e43a32a
MS
11772 printf_filtered (_("Will ignore next %d "
11773 "crossings of breakpoint %d."),
221ea385
KS
11774 count, bptnum);
11775 }
c5aa993b 11776 breakpoints_changed ();
8d3788bd 11777 observer_notify_breakpoint_modified (b);
c5aa993b
JM
11778 return;
11779 }
c906108c 11780
8a3fe4f8 11781 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
11782}
11783
c906108c
SS
11784/* Command to set ignore-count of breakpoint N to COUNT. */
11785
11786static void
fba45db2 11787ignore_command (char *args, int from_tty)
c906108c
SS
11788{
11789 char *p = args;
52f0bd74 11790 int num;
c906108c
SS
11791
11792 if (p == 0)
e2e0b3e5 11793 error_no_arg (_("a breakpoint number"));
c5aa993b 11794
c906108c 11795 num = get_number (&p);
5c44784c 11796 if (num == 0)
8a3fe4f8 11797 error (_("bad breakpoint number: '%s'"), args);
c906108c 11798 if (*p == 0)
8a3fe4f8 11799 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
11800
11801 set_ignore_count (num,
11802 longest_to_int (value_as_long (parse_and_eval (p))),
11803 from_tty);
221ea385
KS
11804 if (from_tty)
11805 printf_filtered ("\n");
c906108c
SS
11806}
11807\f
11808/* Call FUNCTION on each of the breakpoints
11809 whose numbers are given in ARGS. */
11810
11811static void
95a42b64
TT
11812map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
11813 void *),
11814 void *data)
c906108c 11815{
52f0bd74
AC
11816 int num;
11817 struct breakpoint *b, *tmp;
11cf8741 11818 int match;
197f0a60 11819 struct get_number_or_range_state state;
c906108c 11820
197f0a60 11821 if (args == 0)
e2e0b3e5 11822 error_no_arg (_("one or more breakpoint numbers"));
c906108c 11823
197f0a60
TT
11824 init_number_or_range (&state, args);
11825
11826 while (!state.finished)
c906108c 11827 {
197f0a60
TT
11828 char *p = state.string;
11829
11cf8741 11830 match = 0;
c5aa993b 11831
197f0a60 11832 num = get_number_or_range (&state);
5c44784c 11833 if (num == 0)
c5aa993b 11834 {
8a3fe4f8 11835 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
11836 }
11837 else
11838 {
11839 ALL_BREAKPOINTS_SAFE (b, tmp)
11840 if (b->number == num)
11841 {
11cf8741 11842 match = 1;
cdac0397 11843 function (b, data);
11cf8741 11844 break;
5c44784c 11845 }
11cf8741 11846 if (match == 0)
a3f17187 11847 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 11848 }
c906108c
SS
11849 }
11850}
11851
0d381245
VP
11852static struct bp_location *
11853find_location_by_number (char *number)
11854{
11855 char *dot = strchr (number, '.');
11856 char *p1;
11857 int bp_num;
11858 int loc_num;
11859 struct breakpoint *b;
11860 struct bp_location *loc;
11861
11862 *dot = '\0';
11863
11864 p1 = number;
197f0a60 11865 bp_num = get_number (&p1);
0d381245
VP
11866 if (bp_num == 0)
11867 error (_("Bad breakpoint number '%s'"), number);
11868
11869 ALL_BREAKPOINTS (b)
11870 if (b->number == bp_num)
11871 {
11872 break;
11873 }
11874
11875 if (!b || b->number != bp_num)
11876 error (_("Bad breakpoint number '%s'"), number);
11877
11878 p1 = dot+1;
197f0a60 11879 loc_num = get_number (&p1);
0d381245
VP
11880 if (loc_num == 0)
11881 error (_("Bad breakpoint location number '%s'"), number);
11882
11883 --loc_num;
11884 loc = b->loc;
11885 for (;loc_num && loc; --loc_num, loc = loc->next)
11886 ;
11887 if (!loc)
11888 error (_("Bad breakpoint location number '%s'"), dot+1);
11889
11890 return loc;
11891}
11892
11893
1900040c
MS
11894/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11895 If from_tty is nonzero, it prints a message to that effect,
11896 which ends with a period (no newline). */
11897
c906108c 11898void
fba45db2 11899disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
11900{
11901 /* Never disable a watchpoint scope breakpoint; we want to
11902 hit them when we leave scope so we can delete both the
11903 watchpoint and its scope breakpoint at that time. */
11904 if (bpt->type == bp_watchpoint_scope)
11905 return;
11906
c2c6d25f 11907 /* You can't disable permanent breakpoints. */
b5de0fa7 11908 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
11909 return;
11910
b5de0fa7 11911 bpt->enable_state = bp_disabled;
c906108c 11912
d248b706
KY
11913 if (target_supports_enable_disable_tracepoint ()
11914 && current_trace_status ()->running && is_tracepoint (bpt))
11915 {
11916 struct bp_location *location;
11917
11918 for (location = bpt->loc; location; location = location->next)
11919 target_disable_tracepoint (location);
11920 }
11921
b60e7edf 11922 update_global_location_list (0);
c906108c 11923
8d3788bd 11924 observer_notify_breakpoint_modified (bpt);
c906108c
SS
11925}
11926
51be5b68
PA
11927/* A callback for iterate_over_related_breakpoints. */
11928
11929static void
11930do_disable_breakpoint (struct breakpoint *b, void *ignore)
11931{
11932 disable_breakpoint (b);
11933}
11934
95a42b64
TT
11935/* A callback for map_breakpoint_numbers that calls
11936 disable_breakpoint. */
11937
11938static void
11939do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
11940{
51be5b68 11941 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
11942}
11943
c906108c 11944static void
fba45db2 11945disable_command (char *args, int from_tty)
c906108c 11946{
c906108c 11947 if (args == 0)
46c6471b
PA
11948 {
11949 struct breakpoint *bpt;
11950
11951 ALL_BREAKPOINTS (bpt)
11952 if (user_breakpoint_p (bpt))
11953 disable_breakpoint (bpt);
11954 }
0d381245
VP
11955 else if (strchr (args, '.'))
11956 {
11957 struct bp_location *loc = find_location_by_number (args);
11958 if (loc)
d248b706
KY
11959 {
11960 loc->enabled = 0;
11961 if (target_supports_enable_disable_tracepoint ()
11962 && current_trace_status ()->running && loc->owner
11963 && is_tracepoint (loc->owner))
11964 target_disable_tracepoint (loc);
11965 }
b60e7edf 11966 update_global_location_list (0);
0d381245 11967 }
c906108c 11968 else
95a42b64 11969 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
11970}
11971
11972static void
51be5b68 11973enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 11974{
afe38095 11975 int target_resources_ok;
c906108c
SS
11976
11977 if (bpt->type == bp_hardware_breakpoint)
11978 {
11979 int i;
c5aa993b 11980 i = hw_breakpoint_used_count ();
53a5351d 11981 target_resources_ok =
d92524f1 11982 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 11983 i + 1, 0);
c906108c 11984 if (target_resources_ok == 0)
8a3fe4f8 11985 error (_("No hardware breakpoint support in the target."));
c906108c 11986 else if (target_resources_ok < 0)
8a3fe4f8 11987 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
11988 }
11989
cc60f2e3 11990 if (is_watchpoint (bpt))
c906108c 11991 {
d07205c2
JK
11992 /* Initialize it just to avoid a GCC false warning. */
11993 enum enable_state orig_enable_state = 0;
dde02812
ES
11994 struct gdb_exception e;
11995
11996 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 11997 {
1e718ff1
TJB
11998 orig_enable_state = bpt->enable_state;
11999 bpt->enable_state = bp_enabled;
dde02812 12000 update_watchpoint (bpt, 1 /* reparse */);
c906108c 12001 }
dde02812 12002 if (e.reason < 0)
c5aa993b 12003 {
1e718ff1 12004 bpt->enable_state = orig_enable_state;
dde02812
ES
12005 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12006 bpt->number);
12007 return;
c5aa993b 12008 }
c906108c 12009 }
0101ce28 12010
b4c291bb
KH
12011 if (bpt->enable_state != bp_permanent)
12012 bpt->enable_state = bp_enabled;
d248b706
KY
12013
12014 if (target_supports_enable_disable_tracepoint ()
12015 && current_trace_status ()->running && is_tracepoint (bpt))
12016 {
12017 struct bp_location *location;
12018
12019 for (location = bpt->loc; location; location = location->next)
12020 target_enable_tracepoint (location);
12021 }
12022
b4c291bb 12023 bpt->disposition = disposition;
b60e7edf 12024 update_global_location_list (1);
b4c291bb
KH
12025 breakpoints_changed ();
12026
8d3788bd 12027 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12028}
12029
fe3f5fa8 12030
c906108c 12031void
fba45db2 12032enable_breakpoint (struct breakpoint *bpt)
c906108c 12033{
51be5b68
PA
12034 enable_breakpoint_disp (bpt, bpt->disposition);
12035}
12036
12037static void
12038do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12039{
12040 enable_breakpoint (bpt);
c906108c
SS
12041}
12042
95a42b64
TT
12043/* A callback for map_breakpoint_numbers that calls
12044 enable_breakpoint. */
12045
12046static void
12047do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12048{
51be5b68 12049 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
12050}
12051
c906108c
SS
12052/* The enable command enables the specified breakpoints (or all defined
12053 breakpoints) so they once again become (or continue to be) effective
1272ad14 12054 in stopping the inferior. */
c906108c 12055
c906108c 12056static void
fba45db2 12057enable_command (char *args, int from_tty)
c906108c 12058{
c906108c 12059 if (args == 0)
46c6471b
PA
12060 {
12061 struct breakpoint *bpt;
12062
12063 ALL_BREAKPOINTS (bpt)
12064 if (user_breakpoint_p (bpt))
12065 enable_breakpoint (bpt);
12066 }
0d381245
VP
12067 else if (strchr (args, '.'))
12068 {
12069 struct bp_location *loc = find_location_by_number (args);
12070 if (loc)
d248b706
KY
12071 {
12072 loc->enabled = 1;
12073 if (target_supports_enable_disable_tracepoint ()
12074 && current_trace_status ()->running && loc->owner
12075 && is_tracepoint (loc->owner))
12076 target_enable_tracepoint (loc);
12077 }
b60e7edf 12078 update_global_location_list (1);
0d381245 12079 }
c906108c 12080 else
95a42b64 12081 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
12082}
12083
12084static void
51be5b68
PA
12085do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12086{
12087 enum bpdisp disp = *(enum bpdisp *) arg;
12088
12089 enable_breakpoint_disp (bpt, disp);
12090}
12091
12092static void
12093do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12094{
51be5b68
PA
12095 enum bpdisp disp = disp_disable;
12096
12097 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12098}
12099
c906108c 12100static void
fba45db2 12101enable_once_command (char *args, int from_tty)
c906108c 12102{
51be5b68 12103 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
12104}
12105
12106static void
51be5b68 12107do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12108{
51be5b68
PA
12109 enum bpdisp disp = disp_del;
12110
12111 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12112}
12113
c906108c 12114static void
fba45db2 12115enable_delete_command (char *args, int from_tty)
c906108c 12116{
51be5b68 12117 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
12118}
12119\f
fa8d40ab
JJ
12120static void
12121set_breakpoint_cmd (char *args, int from_tty)
12122{
12123}
12124
12125static void
12126show_breakpoint_cmd (char *args, int from_tty)
12127{
12128}
12129
1f3b5d1b
PP
12130/* Invalidate last known value of any hardware watchpoint if
12131 the memory which that value represents has been written to by
12132 GDB itself. */
12133
12134static void
12135invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12136 const bfd_byte *data)
12137{
12138 struct breakpoint *bp;
12139
12140 ALL_BREAKPOINTS (bp)
12141 if (bp->enable_state == bp_enabled
12142 && bp->type == bp_hardware_watchpoint
12143 && bp->val_valid && bp->val)
12144 {
12145 struct bp_location *loc;
12146
12147 for (loc = bp->loc; loc != NULL; loc = loc->next)
12148 if (loc->loc_type == bp_loc_hardware_watchpoint
12149 && loc->address + loc->length > addr
12150 && addr + len > loc->address)
12151 {
12152 value_free (bp->val);
12153 bp->val = NULL;
12154 bp->val_valid = 0;
12155 }
12156 }
12157}
12158
c906108c
SS
12159/* Use default_breakpoint_'s, or nothing if they aren't valid. */
12160
12161struct symtabs_and_lines
fba45db2 12162decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
12163{
12164 struct symtabs_and_lines sals;
cc59ec59 12165
c906108c 12166 if (string == 0)
8a3fe4f8 12167 error (_("Empty line specification."));
c906108c
SS
12168 if (default_breakpoint_valid)
12169 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
12170 default_breakpoint_symtab,
12171 default_breakpoint_line,
58438ac1 12172 NULL);
c906108c
SS
12173 else
12174 sals = decode_line_1 (&string, funfirstline,
58438ac1 12175 (struct symtab *) NULL, 0, NULL);
c906108c 12176 if (*string)
8a3fe4f8 12177 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
12178 return sals;
12179}
8181d85f
DJ
12180
12181/* Create and insert a raw software breakpoint at PC. Return an
12182 identifier, which should be used to remove the breakpoint later.
12183 In general, places which call this should be using something on the
12184 breakpoint chain instead; this function should be eliminated
12185 someday. */
12186
12187void *
6c95b8df
PA
12188deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12189 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
12190{
12191 struct bp_target_info *bp_tgt;
12192
6c95b8df 12193 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 12194
6c95b8df 12195 bp_tgt->placed_address_space = aspace;
8181d85f 12196 bp_tgt->placed_address = pc;
6c95b8df 12197
a6d9a66e 12198 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
12199 {
12200 /* Could not insert the breakpoint. */
12201 xfree (bp_tgt);
12202 return NULL;
12203 }
12204
12205 return bp_tgt;
12206}
12207
4a64f543
MS
12208/* Remove a breakpoint BP inserted by
12209 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
12210
12211int
a6d9a66e 12212deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
12213{
12214 struct bp_target_info *bp_tgt = bp;
12215 int ret;
12216
a6d9a66e 12217 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
12218 xfree (bp_tgt);
12219
12220 return ret;
12221}
12222
4a64f543
MS
12223/* One (or perhaps two) breakpoints used for software single
12224 stepping. */
8181d85f
DJ
12225
12226static void *single_step_breakpoints[2];
a6d9a66e 12227static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
12228
12229/* Create and insert a breakpoint for software single step. */
12230
12231void
6c95b8df 12232insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
12233 struct address_space *aspace,
12234 CORE_ADDR next_pc)
8181d85f
DJ
12235{
12236 void **bpt_p;
12237
12238 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
12239 {
12240 bpt_p = &single_step_breakpoints[0];
12241 single_step_gdbarch[0] = gdbarch;
12242 }
8181d85f
DJ
12243 else
12244 {
12245 gdb_assert (single_step_breakpoints[1] == NULL);
12246 bpt_p = &single_step_breakpoints[1];
a6d9a66e 12247 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
12248 }
12249
4a64f543
MS
12250 /* NOTE drow/2006-04-11: A future improvement to this function would
12251 be to only create the breakpoints once, and actually put them on
12252 the breakpoint chain. That would let us use set_raw_breakpoint.
12253 We could adjust the addresses each time they were needed. Doing
12254 this requires corresponding changes elsewhere where single step
12255 breakpoints are handled, however. So, for now, we use this. */
8181d85f 12256
6c95b8df 12257 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 12258 if (*bpt_p == NULL)
5af949e3
UW
12259 error (_("Could not insert single-step breakpoint at %s"),
12260 paddress (gdbarch, next_pc));
8181d85f
DJ
12261}
12262
f02253f1
HZ
12263/* Check if the breakpoints used for software single stepping
12264 were inserted or not. */
12265
12266int
12267single_step_breakpoints_inserted (void)
12268{
12269 return (single_step_breakpoints[0] != NULL
12270 || single_step_breakpoints[1] != NULL);
12271}
12272
8181d85f
DJ
12273/* Remove and delete any breakpoints used for software single step. */
12274
12275void
12276remove_single_step_breakpoints (void)
12277{
12278 gdb_assert (single_step_breakpoints[0] != NULL);
12279
12280 /* See insert_single_step_breakpoint for more about this deprecated
12281 call. */
a6d9a66e
UW
12282 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12283 single_step_breakpoints[0]);
12284 single_step_gdbarch[0] = NULL;
8181d85f
DJ
12285 single_step_breakpoints[0] = NULL;
12286
12287 if (single_step_breakpoints[1] != NULL)
12288 {
a6d9a66e
UW
12289 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12290 single_step_breakpoints[1]);
12291 single_step_gdbarch[1] = NULL;
8181d85f
DJ
12292 single_step_breakpoints[1] = NULL;
12293 }
12294}
12295
d03285ec
UW
12296/* Delete software single step breakpoints without removing them from
12297 the inferior. This is intended to be used if the inferior's address
12298 space where they were inserted is already gone, e.g. after exit or
12299 exec. */
12300
12301void
12302cancel_single_step_breakpoints (void)
12303{
12304 int i;
12305
12306 for (i = 0; i < 2; i++)
12307 if (single_step_breakpoints[i])
12308 {
12309 xfree (single_step_breakpoints[i]);
12310 single_step_breakpoints[i] = NULL;
12311 single_step_gdbarch[i] = NULL;
12312 }
12313}
12314
12315/* Detach software single-step breakpoints from INFERIOR_PTID without
12316 removing them. */
12317
12318static void
12319detach_single_step_breakpoints (void)
12320{
12321 int i;
12322
12323 for (i = 0; i < 2; i++)
12324 if (single_step_breakpoints[i])
12325 target_remove_breakpoint (single_step_gdbarch[i],
12326 single_step_breakpoints[i]);
12327}
12328
4a64f543
MS
12329/* Check whether a software single-step breakpoint is inserted at
12330 PC. */
1aafd4da
UW
12331
12332static int
cc59ec59
MS
12333single_step_breakpoint_inserted_here_p (struct address_space *aspace,
12334 CORE_ADDR pc)
1aafd4da
UW
12335{
12336 int i;
12337
12338 for (i = 0; i < 2; i++)
12339 {
12340 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
12341 if (bp_tgt
12342 && breakpoint_address_match (bp_tgt->placed_address_space,
12343 bp_tgt->placed_address,
12344 aspace, pc))
1aafd4da
UW
12345 return 1;
12346 }
12347
12348 return 0;
12349}
12350
a96d9b2e
SDJ
12351/* Returns 0 if 'bp' is NOT a syscall catchpoint,
12352 non-zero otherwise. */
12353static int
12354is_syscall_catchpoint_enabled (struct breakpoint *bp)
12355{
12356 if (syscall_catchpoint_p (bp)
12357 && bp->enable_state != bp_disabled
12358 && bp->enable_state != bp_call_disabled)
12359 return 1;
12360 else
12361 return 0;
12362}
12363
12364int
12365catch_syscall_enabled (void)
12366{
12367 struct inferior *inf = current_inferior ();
12368
12369 return inf->total_syscalls_count != 0;
12370}
12371
12372int
12373catching_syscall_number (int syscall_number)
12374{
12375 struct breakpoint *bp;
12376
12377 ALL_BREAKPOINTS (bp)
12378 if (is_syscall_catchpoint_enabled (bp))
12379 {
be5c67c1
PA
12380 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12381
12382 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
12383 {
12384 int i, iter;
12385 for (i = 0;
be5c67c1 12386 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
12387 i++)
12388 if (syscall_number == iter)
12389 return 1;
12390 }
12391 else
12392 return 1;
12393 }
12394
12395 return 0;
12396}
12397
12398/* Complete syscall names. Used by "catch syscall". */
12399static char **
12400catch_syscall_completer (struct cmd_list_element *cmd,
12401 char *text, char *word)
12402{
12403 const char **list = get_syscall_names ();
c38eea1a
MS
12404 char **retlist
12405 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 12406
c38eea1a
MS
12407 xfree (list);
12408 return retlist;
a96d9b2e
SDJ
12409}
12410
1042e4c0
SS
12411/* Tracepoint-specific operations. */
12412
12413/* Set tracepoint count to NUM. */
12414static void
12415set_tracepoint_count (int num)
12416{
12417 tracepoint_count = num;
4fa62494 12418 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
12419}
12420
12421void
12422trace_command (char *arg, int from_tty)
12423{
8cdf0e15
VP
12424 if (create_breakpoint (get_current_arch (),
12425 arg,
12426 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12427 0 /* tempflag */,
12428 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
12429 0 /* Ignore count */,
12430 pending_break_support,
348d480f 12431 &tracepoint_breakpoint_ops,
8cdf0e15 12432 from_tty,
84f4c1fe
PM
12433 1 /* enabled */,
12434 0 /* internal */))
fd9b8c24 12435 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
12436}
12437
7a697b8d
SS
12438void
12439ftrace_command (char *arg, int from_tty)
12440{
8cdf0e15
VP
12441 if (create_breakpoint (get_current_arch (),
12442 arg,
12443 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12444 0 /* tempflag */,
12445 bp_fast_tracepoint /* type_wanted */,
12446 0 /* Ignore count */,
12447 pending_break_support,
348d480f 12448 &tracepoint_breakpoint_ops,
0fb4aa4b 12449 from_tty,
84f4c1fe
PM
12450 1 /* enabled */,
12451 0 /* internal */))
0fb4aa4b
PA
12452 set_tracepoint_count (breakpoint_count);
12453}
12454
12455/* strace command implementation. Creates a static tracepoint. */
12456
12457void
12458strace_command (char *arg, int from_tty)
12459{
12460 if (create_breakpoint (get_current_arch (),
12461 arg,
12462 NULL, 0, 1 /* parse arg */,
12463 0 /* tempflag */,
12464 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
12465 0 /* Ignore count */,
12466 pending_break_support,
348d480f 12467 &tracepoint_breakpoint_ops,
8cdf0e15 12468 from_tty,
84f4c1fe
PM
12469 1 /* enabled */,
12470 0 /* internal */))
fd9b8c24 12471 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
12472}
12473
409873ef
SS
12474/* Set up a fake reader function that gets command lines from a linked
12475 list that was acquired during tracepoint uploading. */
12476
12477static struct uploaded_tp *this_utp;
3149d8c1 12478static int next_cmd;
409873ef
SS
12479
12480static char *
12481read_uploaded_action (void)
12482{
12483 char *rslt;
12484
3149d8c1 12485 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 12486
3149d8c1 12487 next_cmd++;
409873ef
SS
12488
12489 return rslt;
12490}
12491
00bf0b85
SS
12492/* Given information about a tracepoint as recorded on a target (which
12493 can be either a live system or a trace file), attempt to create an
12494 equivalent GDB tracepoint. This is not a reliable process, since
12495 the target does not necessarily have all the information used when
12496 the tracepoint was originally defined. */
12497
12498struct breakpoint *
12499create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 12500{
409873ef 12501 char *addr_str, small_buf[100];
d5551862 12502 struct breakpoint *tp;
fd9b8c24 12503
409873ef
SS
12504 if (utp->at_string)
12505 addr_str = utp->at_string;
12506 else
12507 {
12508 /* In the absence of a source location, fall back to raw
12509 address. Since there is no way to confirm that the address
12510 means the same thing as when the trace was started, warn the
12511 user. */
3e43a32a
MS
12512 warning (_("Uploaded tracepoint %d has no "
12513 "source location, using raw address"),
409873ef
SS
12514 utp->number);
12515 sprintf (small_buf, "*%s", hex_string (utp->addr));
12516 addr_str = small_buf;
12517 }
12518
12519 /* There's not much we can do with a sequence of bytecodes. */
12520 if (utp->cond && !utp->cond_string)
3e43a32a
MS
12521 warning (_("Uploaded tracepoint %d condition "
12522 "has no source form, ignoring it"),
409873ef 12523 utp->number);
d5551862 12524
8cdf0e15 12525 if (!create_breakpoint (get_current_arch (),
409873ef
SS
12526 addr_str,
12527 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 12528 0 /* tempflag */,
0fb4aa4b 12529 utp->type /* type_wanted */,
8cdf0e15
VP
12530 0 /* Ignore count */,
12531 pending_break_support,
348d480f 12532 &tracepoint_breakpoint_ops,
8cdf0e15 12533 0 /* from_tty */,
84f4c1fe
PM
12534 utp->enabled /* enabled */,
12535 0 /* internal */))
fd9b8c24
PA
12536 return NULL;
12537
00bf0b85
SS
12538 set_tracepoint_count (breakpoint_count);
12539
409873ef 12540 /* Get the tracepoint we just created. */
fd9b8c24
PA
12541 tp = get_tracepoint (tracepoint_count);
12542 gdb_assert (tp != NULL);
d5551862 12543
00bf0b85
SS
12544 if (utp->pass > 0)
12545 {
409873ef 12546 sprintf (small_buf, "%d %d", utp->pass, tp->number);
00bf0b85 12547
409873ef 12548 trace_pass_command (small_buf, 0);
00bf0b85
SS
12549 }
12550
409873ef
SS
12551 /* If we have uploaded versions of the original commands, set up a
12552 special-purpose "reader" function and call the usual command line
12553 reader, then pass the result to the breakpoint command-setting
12554 function. */
3149d8c1 12555 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 12556 {
409873ef 12557 struct command_line *cmd_list;
00bf0b85 12558
409873ef 12559 this_utp = utp;
3149d8c1 12560 next_cmd = 0;
d5551862 12561
409873ef
SS
12562 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12563
12564 breakpoint_set_commands (tp, cmd_list);
00bf0b85 12565 }
3149d8c1
SS
12566 else if (!VEC_empty (char_ptr, utp->actions)
12567 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12568 warning (_("Uploaded tracepoint %d actions "
12569 "have no source form, ignoring them"),
409873ef 12570 utp->number);
00bf0b85
SS
12571
12572 return tp;
12573 }
12574
1042e4c0
SS
12575/* Print information on tracepoint number TPNUM_EXP, or all if
12576 omitted. */
12577
12578static void
e5a67952 12579tracepoints_info (char *args, int from_tty)
1042e4c0 12580{
e5a67952 12581 int num_printed;
1042e4c0 12582
e5a67952 12583 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
12584
12585 if (num_printed == 0)
1042e4c0 12586 {
e5a67952 12587 if (args == NULL || *args == '\0')
d77f58be
SS
12588 ui_out_message (uiout, 0, "No tracepoints.\n");
12589 else
e5a67952 12590 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 12591 }
ad443146
SS
12592
12593 default_collect_info ();
1042e4c0
SS
12594}
12595
4a64f543 12596/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
12597 Not supported by all targets. */
12598static void
12599enable_trace_command (char *args, int from_tty)
12600{
12601 enable_command (args, from_tty);
12602}
12603
4a64f543 12604/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
12605 Not supported by all targets. */
12606static void
12607disable_trace_command (char *args, int from_tty)
12608{
12609 disable_command (args, from_tty);
12610}
12611
4a64f543 12612/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
12613static void
12614delete_trace_command (char *arg, int from_tty)
12615{
35df4500 12616 struct breakpoint *b, *b_tmp;
1042e4c0
SS
12617
12618 dont_repeat ();
12619
12620 if (arg == 0)
12621 {
12622 int breaks_to_delete = 0;
12623
12624 /* Delete all breakpoints if no argument.
12625 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
12626 have to be deleted with an explicit breakpoint number
12627 argument. */
1042e4c0 12628 ALL_TRACEPOINTS (b)
46c6471b 12629 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
12630 {
12631 breaks_to_delete = 1;
12632 break;
12633 }
1042e4c0
SS
12634
12635 /* Ask user only if there are some breakpoints to delete. */
12636 if (!from_tty
12637 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12638 {
35df4500 12639 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 12640 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 12641 delete_breakpoint (b);
1042e4c0
SS
12642 }
12643 }
12644 else
51be5b68 12645 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
12646}
12647
197f0a60
TT
12648/* Helper function for trace_pass_command. */
12649
12650static void
12651trace_pass_set_count (struct breakpoint *bp, int count, int from_tty)
12652{
12653 bp->pass_count = count;
12654 observer_notify_tracepoint_modified (bp->number);
12655 if (from_tty)
12656 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
12657 bp->number, count);
12658}
12659
1042e4c0
SS
12660/* Set passcount for tracepoint.
12661
12662 First command argument is passcount, second is tracepoint number.
12663 If tracepoint number omitted, apply to most recently defined.
12664 Also accepts special argument "all". */
12665
12666static void
12667trace_pass_command (char *args, int from_tty)
12668{
197f0a60 12669 struct breakpoint *t1;
1042e4c0 12670 unsigned int count;
1042e4c0
SS
12671
12672 if (args == 0 || *args == 0)
3e43a32a
MS
12673 error (_("passcount command requires an "
12674 "argument (count + optional TP num)"));
1042e4c0 12675
4a64f543 12676 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
12677
12678 while (*args && isspace ((int) *args))
12679 args++;
12680
12681 if (*args && strncasecmp (args, "all", 3) == 0)
12682 {
12683 args += 3; /* Skip special argument "all". */
1042e4c0
SS
12684 if (*args)
12685 error (_("Junk at end of arguments."));
1042e4c0 12686
197f0a60
TT
12687 ALL_TRACEPOINTS (t1)
12688 {
12689 trace_pass_set_count (t1, count, from_tty);
12690 }
12691 }
12692 else if (*args == '\0')
1042e4c0 12693 {
197f0a60 12694 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 12695 if (t1)
197f0a60
TT
12696 trace_pass_set_count (t1, count, from_tty);
12697 }
12698 else
12699 {
12700 struct get_number_or_range_state state;
12701
12702 init_number_or_range (&state, args);
12703 while (!state.finished)
1042e4c0 12704 {
197f0a60
TT
12705 t1 = get_tracepoint_by_number (&args, &state, 1);
12706 if (t1)
12707 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
12708 }
12709 }
1042e4c0
SS
12710}
12711
12712struct breakpoint *
12713get_tracepoint (int num)
12714{
12715 struct breakpoint *t;
12716
12717 ALL_TRACEPOINTS (t)
12718 if (t->number == num)
12719 return t;
12720
12721 return NULL;
12722}
12723
d5551862
SS
12724/* Find the tracepoint with the given target-side number (which may be
12725 different from the tracepoint number after disconnecting and
12726 reconnecting). */
12727
12728struct breakpoint *
12729get_tracepoint_by_number_on_target (int num)
12730{
12731 struct breakpoint *t;
12732
12733 ALL_TRACEPOINTS (t)
12734 if (t->number_on_target == num)
12735 return t;
12736
12737 return NULL;
12738}
12739
1042e4c0 12740/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
12741 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
12742 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0
SS
12743 recent tracepoint (tracepoint_count) is returned. */
12744struct breakpoint *
197f0a60
TT
12745get_tracepoint_by_number (char **arg,
12746 struct get_number_or_range_state *state,
12747 int optional_p)
1042e4c0
SS
12748{
12749 extern int tracepoint_count;
12750 struct breakpoint *t;
12751 int tpnum;
12752 char *instring = arg == NULL ? NULL : *arg;
12753
197f0a60
TT
12754 if (state)
12755 {
12756 gdb_assert (!state->finished);
12757 tpnum = get_number_or_range (state);
12758 }
12759 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
12760 {
12761 if (optional_p)
12762 tpnum = tracepoint_count;
12763 else
12764 error_no_arg (_("tracepoint number"));
12765 }
12766 else
197f0a60 12767 tpnum = get_number (arg);
1042e4c0
SS
12768
12769 if (tpnum <= 0)
12770 {
12771 if (instring && *instring)
12772 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
12773 instring);
12774 else
3e43a32a
MS
12775 printf_filtered (_("Tracepoint argument missing "
12776 "and no previous tracepoint\n"));
1042e4c0
SS
12777 return NULL;
12778 }
12779
12780 ALL_TRACEPOINTS (t)
12781 if (t->number == tpnum)
12782 {
12783 return t;
12784 }
12785
1042e4c0
SS
12786 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
12787 return NULL;
12788}
12789
6149aea9
PA
12790/* Save information on user settable breakpoints (watchpoints, etc) to
12791 a new script file named FILENAME. If FILTER is non-NULL, call it
12792 on each breakpoint and only include the ones for which it returns
12793 non-zero. */
12794
1042e4c0 12795static void
6149aea9
PA
12796save_breakpoints (char *filename, int from_tty,
12797 int (*filter) (const struct breakpoint *))
1042e4c0
SS
12798{
12799 struct breakpoint *tp;
6149aea9 12800 int any = 0;
a7bdde9e 12801 char *pathname;
1042e4c0 12802 struct cleanup *cleanup;
a7bdde9e 12803 struct ui_file *fp;
6149aea9 12804 int extra_trace_bits = 0;
1042e4c0 12805
6149aea9
PA
12806 if (filename == 0 || *filename == 0)
12807 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
12808
12809 /* See if we have anything to save. */
6149aea9 12810 ALL_BREAKPOINTS (tp)
1042e4c0 12811 {
6149aea9 12812 /* Skip internal and momentary breakpoints. */
09d682a4 12813 if (!user_breakpoint_p (tp))
6149aea9
PA
12814 continue;
12815
12816 /* If we have a filter, only save the breakpoints it accepts. */
12817 if (filter && !filter (tp))
12818 continue;
12819
12820 any = 1;
12821
12822 if (is_tracepoint (tp))
12823 {
12824 extra_trace_bits = 1;
12825
12826 /* We can stop searching. */
12827 break;
12828 }
1042e4c0 12829 }
6149aea9
PA
12830
12831 if (!any)
1042e4c0 12832 {
6149aea9 12833 warning (_("Nothing to save."));
1042e4c0
SS
12834 return;
12835 }
12836
6149aea9 12837 pathname = tilde_expand (filename);
1042e4c0 12838 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 12839 fp = gdb_fopen (pathname, "w");
059fb39f 12840 if (!fp)
6149aea9
PA
12841 error (_("Unable to open file '%s' for saving (%s)"),
12842 filename, safe_strerror (errno));
a7bdde9e 12843 make_cleanup_ui_file_delete (fp);
8bf6485c 12844
6149aea9
PA
12845 if (extra_trace_bits)
12846 save_trace_state_variables (fp);
8bf6485c 12847
6149aea9 12848 ALL_BREAKPOINTS (tp)
1042e4c0 12849 {
6149aea9 12850 /* Skip internal and momentary breakpoints. */
09d682a4 12851 if (!user_breakpoint_p (tp))
6149aea9 12852 continue;
8bf6485c 12853
6149aea9
PA
12854 /* If we have a filter, only save the breakpoints it accepts. */
12855 if (filter && !filter (tp))
12856 continue;
12857
348d480f 12858 tp->ops->print_recreate (tp, fp);
1042e4c0 12859
6149aea9
PA
12860 if (tp->thread != -1)
12861 fprintf_unfiltered (fp, " thread %d", tp->thread);
12862
12863 if (tp->task != 0)
12864 fprintf_unfiltered (fp, " task %d", tp->task);
8bf6485c
SS
12865
12866 fprintf_unfiltered (fp, "\n");
12867
6149aea9
PA
12868 /* Note, we can't rely on tp->number for anything, as we can't
12869 assume the recreated breakpoint numbers will match. Use $bpnum
12870 instead. */
12871
12872 if (tp->cond_string)
12873 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
12874
12875 if (tp->ignore_count)
12876 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
12877
1042e4c0 12878 if (tp->pass_count)
a7bdde9e 12879 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
1042e4c0 12880
a7bdde9e 12881 if (tp->commands)
1042e4c0 12882 {
a7bdde9e
VP
12883 volatile struct gdb_exception ex;
12884
6149aea9 12885 fprintf_unfiltered (fp, " commands\n");
a7bdde9e
VP
12886
12887 ui_out_redirect (uiout, fp);
14dba4b4 12888 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 12889 {
9add0f1b 12890 print_command_lines (uiout, tp->commands->commands, 2);
a7bdde9e
VP
12891 }
12892 ui_out_redirect (uiout, NULL);
1042e4c0 12893
a7bdde9e
VP
12894 if (ex.reason < 0)
12895 throw_exception (ex);
1042e4c0 12896
a7bdde9e 12897 fprintf_unfiltered (fp, " end\n");
1042e4c0 12898 }
6149aea9
PA
12899
12900 if (tp->enable_state == bp_disabled)
12901 fprintf_unfiltered (fp, "disable\n");
12902
12903 /* If this is a multi-location breakpoint, check if the locations
12904 should be individually disabled. Watchpoint locations are
12905 special, and not user visible. */
12906 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
12907 {
12908 struct bp_location *loc;
12909 int n = 1;
12910
12911 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
12912 if (!loc->enabled)
12913 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
12914 }
1042e4c0 12915 }
8bf6485c 12916
6149aea9 12917 if (extra_trace_bits && *default_collect)
8bf6485c
SS
12918 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
12919
1042e4c0
SS
12920 do_cleanups (cleanup);
12921 if (from_tty)
6149aea9
PA
12922 printf_filtered (_("Saved to file '%s'.\n"), filename);
12923}
12924
12925/* The `save breakpoints' command. */
12926
12927static void
12928save_breakpoints_command (char *args, int from_tty)
12929{
12930 save_breakpoints (args, from_tty, NULL);
12931}
12932
12933/* The `save tracepoints' command. */
12934
12935static void
12936save_tracepoints_command (char *args, int from_tty)
12937{
12938 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
12939}
12940
12941/* Create a vector of all tracepoints. */
12942
12943VEC(breakpoint_p) *
eeae04df 12944all_tracepoints (void)
1042e4c0
SS
12945{
12946 VEC(breakpoint_p) *tp_vec = 0;
12947 struct breakpoint *tp;
12948
12949 ALL_TRACEPOINTS (tp)
12950 {
12951 VEC_safe_push (breakpoint_p, tp_vec, tp);
12952 }
12953
12954 return tp_vec;
12955}
12956
c906108c 12957\f
4a64f543
MS
12958/* This help string is used for the break, hbreak, tbreak and thbreak
12959 commands. It is defined as a macro to prevent duplication.
12960 COMMAND should be a string constant containing the name of the
12961 command. */
31e2b00f
AS
12962#define BREAK_ARGS_HELP(command) \
12963command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
12964LOCATION may be a line number, function name, or \"*\" and an address.\n\
12965If a line number is specified, break at start of code for that line.\n\
12966If a function is specified, break at start of code for that function.\n\
12967If an address is specified, break at that exact address.\n\
dc10affe
PA
12968With no LOCATION, uses current execution address of the selected\n\
12969stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
12970\n\
12971THREADNUM is the number from \"info threads\".\n\
12972CONDITION is a boolean expression.\n\
12973\n\
d41c0fc8
PA
12974Multiple breakpoints at one place are permitted, and useful if their\n\
12975conditions are different.\n\
31e2b00f
AS
12976\n\
12977Do \"help breakpoints\" for info on other commands dealing with breakpoints."
12978
44feb3ce
TT
12979/* List of subcommands for "catch". */
12980static struct cmd_list_element *catch_cmdlist;
12981
12982/* List of subcommands for "tcatch". */
12983static struct cmd_list_element *tcatch_cmdlist;
12984
9ac4176b 12985void
44feb3ce
TT
12986add_catch_command (char *name, char *docstring,
12987 void (*sfunc) (char *args, int from_tty,
12988 struct cmd_list_element *command),
a96d9b2e
SDJ
12989 char **(*completer) (struct cmd_list_element *cmd,
12990 char *text, char *word),
44feb3ce
TT
12991 void *user_data_catch,
12992 void *user_data_tcatch)
12993{
12994 struct cmd_list_element *command;
12995
12996 command = add_cmd (name, class_breakpoint, NULL, docstring,
12997 &catch_cmdlist);
12998 set_cmd_sfunc (command, sfunc);
12999 set_cmd_context (command, user_data_catch);
a96d9b2e 13000 set_cmd_completer (command, completer);
44feb3ce
TT
13001
13002 command = add_cmd (name, class_breakpoint, NULL, docstring,
13003 &tcatch_cmdlist);
13004 set_cmd_sfunc (command, sfunc);
13005 set_cmd_context (command, user_data_tcatch);
a96d9b2e 13006 set_cmd_completer (command, completer);
44feb3ce
TT
13007}
13008
6c95b8df 13009static void
a79b8f6e 13010clear_syscall_counts (struct inferior *inf)
6c95b8df 13011{
6c95b8df
PA
13012 inf->total_syscalls_count = 0;
13013 inf->any_syscall_count = 0;
13014 VEC_free (int, inf->syscalls_counts);
13015}
13016
6149aea9
PA
13017static void
13018save_command (char *arg, int from_tty)
13019{
3e43a32a
MS
13020 printf_unfiltered (_("\"save\" must be followed by "
13021 "the name of a save subcommand.\n"));
6149aea9
PA
13022 help_list (save_cmdlist, "save ", -1, gdb_stdout);
13023}
13024
84f4c1fe
PM
13025struct breakpoint *
13026iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
13027 void *data)
13028{
35df4500 13029 struct breakpoint *b, *b_tmp;
84f4c1fe 13030
35df4500 13031 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
13032 {
13033 if ((*callback) (b, data))
13034 return b;
13035 }
13036
13037 return NULL;
13038}
13039
2060206e
PA
13040void
13041initialize_breakpoint_ops (void)
13042{
13043 static int initialized = 0;
13044
13045 struct breakpoint_ops *ops;
13046
13047 if (initialized)
13048 return;
13049 initialized = 1;
13050
13051 /* The breakpoint_ops structure to be inherit by all kinds of
13052 breakpoints (real breakpoints, i.e., user "break" breakpoints,
13053 internal and momentary breakpoints, etc.). */
13054 ops = &bkpt_base_breakpoint_ops;
13055 *ops = base_breakpoint_ops;
13056 ops->re_set = bkpt_re_set;
13057 ops->insert_location = bkpt_insert_location;
13058 ops->remove_location = bkpt_remove_location;
13059 ops->breakpoint_hit = bkpt_breakpoint_hit;
13060
13061 /* The breakpoint_ops structure to be used in regular breakpoints. */
13062 ops = &bkpt_breakpoint_ops;
13063 *ops = bkpt_base_breakpoint_ops;
13064 ops->re_set = bkpt_re_set;
13065 ops->resources_needed = bkpt_resources_needed;
13066 ops->print_it = bkpt_print_it;
13067 ops->print_mention = bkpt_print_mention;
13068 ops->print_recreate = bkpt_print_recreate;
13069
13070 /* Ranged breakpoints. */
13071 ops = &ranged_breakpoint_ops;
13072 *ops = bkpt_breakpoint_ops;
13073 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
13074 ops->resources_needed = resources_needed_ranged_breakpoint;
13075 ops->print_it = print_it_ranged_breakpoint;
13076 ops->print_one = print_one_ranged_breakpoint;
13077 ops->print_one_detail = print_one_detail_ranged_breakpoint;
13078 ops->print_mention = print_mention_ranged_breakpoint;
13079 ops->print_recreate = print_recreate_ranged_breakpoint;
13080
13081 /* Internal breakpoints. */
13082 ops = &internal_breakpoint_ops;
13083 *ops = bkpt_base_breakpoint_ops;
13084 ops->re_set = internal_bkpt_re_set;
13085 ops->check_status = internal_bkpt_check_status;
13086 ops->print_it = internal_bkpt_print_it;
13087 ops->print_mention = internal_bkpt_print_mention;
13088
13089 /* Momentary breakpoints. */
13090 ops = &momentary_breakpoint_ops;
13091 *ops = bkpt_base_breakpoint_ops;
13092 ops->re_set = momentary_bkpt_re_set;
13093 ops->check_status = momentary_bkpt_check_status;
13094 ops->print_it = momentary_bkpt_print_it;
13095 ops->print_mention = momentary_bkpt_print_mention;
13096
13097 /* GNU v3 exception catchpoints. */
13098 ops = &gnu_v3_exception_catchpoint_ops;
13099 *ops = bkpt_breakpoint_ops;
13100 ops->print_it = print_it_exception_catchpoint;
13101 ops->print_one = print_one_exception_catchpoint;
13102 ops->print_mention = print_mention_exception_catchpoint;
13103 ops->print_recreate = print_recreate_exception_catchpoint;
13104
13105 /* Watchpoints. */
13106 ops = &watchpoint_breakpoint_ops;
13107 *ops = base_breakpoint_ops;
13108 ops->re_set = re_set_watchpoint;
13109 ops->insert_location = insert_watchpoint;
13110 ops->remove_location = remove_watchpoint;
13111 ops->breakpoint_hit = breakpoint_hit_watchpoint;
13112 ops->check_status = check_status_watchpoint;
13113 ops->resources_needed = resources_needed_watchpoint;
13114 ops->works_in_software_mode = works_in_software_mode_watchpoint;
13115 ops->print_it = print_it_watchpoint;
13116 ops->print_mention = print_mention_watchpoint;
13117 ops->print_recreate = print_recreate_watchpoint;
13118
13119 /* Masked watchpoints. */
13120 ops = &masked_watchpoint_breakpoint_ops;
13121 *ops = watchpoint_breakpoint_ops;
13122 ops->insert_location = insert_masked_watchpoint;
13123 ops->remove_location = remove_masked_watchpoint;
13124 ops->resources_needed = resources_needed_masked_watchpoint;
13125 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
13126 ops->print_it = print_it_masked_watchpoint;
13127 ops->print_one_detail = print_one_detail_masked_watchpoint;
13128 ops->print_mention = print_mention_masked_watchpoint;
13129 ops->print_recreate = print_recreate_masked_watchpoint;
13130
13131 /* Tracepoints. */
13132 ops = &tracepoint_breakpoint_ops;
13133 *ops = base_breakpoint_ops;
13134 ops->re_set = tracepoint_re_set;
13135 ops->breakpoint_hit = tracepoint_breakpoint_hit;
13136 ops->print_one_detail = tracepoint_print_one_detail;
13137 ops->print_mention = tracepoint_print_mention;
13138 ops->print_recreate = tracepoint_print_recreate;
13139
13140 /* Fork catchpoints. */
13141 ops = &catch_fork_breakpoint_ops;
13142 *ops = base_breakpoint_ops;
13143 ops->insert_location = insert_catch_fork;
13144 ops->remove_location = remove_catch_fork;
13145 ops->breakpoint_hit = breakpoint_hit_catch_fork;
13146 ops->print_it = print_it_catch_fork;
13147 ops->print_one = print_one_catch_fork;
13148 ops->print_mention = print_mention_catch_fork;
13149 ops->print_recreate = print_recreate_catch_fork;
13150
13151 /* Vfork catchpoints. */
13152 ops = &catch_vfork_breakpoint_ops;
13153 *ops = base_breakpoint_ops;
13154 ops->insert_location = insert_catch_vfork;
13155 ops->remove_location = remove_catch_vfork;
13156 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
13157 ops->print_it = print_it_catch_vfork;
13158 ops->print_one = print_one_catch_vfork;
13159 ops->print_mention = print_mention_catch_vfork;
13160 ops->print_recreate = print_recreate_catch_vfork;
13161
13162 /* Exec catchpoints. */
13163 ops = &catch_exec_breakpoint_ops;
13164 *ops = base_breakpoint_ops;
13165 ops->dtor = dtor_catch_exec;
13166 ops->insert_location = insert_catch_exec;
13167 ops->remove_location = remove_catch_exec;
13168 ops->breakpoint_hit = breakpoint_hit_catch_exec;
13169 ops->print_it = print_it_catch_exec;
13170 ops->print_one = print_one_catch_exec;
13171 ops->print_mention = print_mention_catch_exec;
13172 ops->print_recreate = print_recreate_catch_exec;
13173
13174 /* Syscall catchpoints. */
13175 ops = &catch_syscall_breakpoint_ops;
13176 *ops = base_breakpoint_ops;
13177 ops->dtor = dtor_catch_syscall;
13178 ops->insert_location = insert_catch_syscall;
13179 ops->remove_location = remove_catch_syscall;
13180 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
13181 ops->print_it = print_it_catch_syscall;
13182 ops->print_one = print_one_catch_syscall;
13183 ops->print_mention = print_mention_catch_syscall;
13184 ops->print_recreate = print_recreate_catch_syscall;
13185}
13186
c906108c 13187void
fba45db2 13188_initialize_breakpoint (void)
c906108c
SS
13189{
13190 struct cmd_list_element *c;
13191
2060206e
PA
13192 initialize_breakpoint_ops ();
13193
84acb35a 13194 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 13195 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 13196 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 13197
17450429
PP
13198 breakpoint_objfile_key = register_objfile_data ();
13199
c906108c
SS
13200 breakpoint_chain = 0;
13201 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
13202 before a breakpoint is set. */
13203 breakpoint_count = 0;
13204
1042e4c0
SS
13205 tracepoint_count = 0;
13206
1bedd215
AC
13207 add_com ("ignore", class_breakpoint, ignore_command, _("\
13208Set ignore-count of breakpoint number N to COUNT.\n\
13209Usage is `ignore N COUNT'."));
c906108c 13210 if (xdb_commands)
c5aa993b 13211 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 13212
1bedd215
AC
13213 add_com ("commands", class_breakpoint, commands_command, _("\
13214Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
13215Give breakpoint number as argument after \"commands\".\n\
13216With no argument, the targeted breakpoint is the last one set.\n\
13217The commands themselves follow starting on the next line.\n\
13218Type a line containing \"end\" to indicate the end of them.\n\
13219Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 13220then no output is printed when it is hit, except what the commands print."));
c906108c 13221
1bedd215
AC
13222 add_com ("condition", class_breakpoint, condition_command, _("\
13223Specify breakpoint number N to break only if COND is true.\n\
c906108c 13224Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 13225expression to be evaluated whenever breakpoint N is reached."));
c906108c 13226
1bedd215 13227 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 13228Set a temporary breakpoint.\n\
c906108c
SS
13229Like \"break\" except the breakpoint is only temporary,\n\
13230so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
13231by using \"enable delete\" on the breakpoint number.\n\
13232\n"
13233BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 13234 set_cmd_completer (c, location_completer);
c94fdfd0 13235
1bedd215 13236 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 13237Set a hardware assisted breakpoint.\n\
c906108c 13238Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
13239some target hardware may not have this support.\n\
13240\n"
13241BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 13242 set_cmd_completer (c, location_completer);
c906108c 13243
1bedd215 13244 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 13245Set a temporary hardware assisted breakpoint.\n\
c906108c 13246Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
13247so it will be deleted when hit.\n\
13248\n"
13249BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 13250 set_cmd_completer (c, location_completer);
c906108c 13251
1bedd215
AC
13252 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
13253Enable some breakpoints.\n\
c906108c
SS
13254Give breakpoint numbers (separated by spaces) as arguments.\n\
13255With no subcommand, breakpoints are enabled until you command otherwise.\n\
13256This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13257With a subcommand you can enable temporarily."),
c906108c
SS
13258 &enablelist, "enable ", 1, &cmdlist);
13259 if (xdb_commands)
1bedd215
AC
13260 add_com ("ab", class_breakpoint, enable_command, _("\
13261Enable some breakpoints.\n\
c906108c
SS
13262Give breakpoint numbers (separated by spaces) as arguments.\n\
13263With no subcommand, breakpoints are enabled until you command otherwise.\n\
13264This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13265With a subcommand you can enable temporarily."));
c906108c
SS
13266
13267 add_com_alias ("en", "enable", class_breakpoint, 1);
13268
84951ab5 13269 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 13270Enable some breakpoints.\n\
c906108c
SS
13271Give breakpoint numbers (separated by spaces) as arguments.\n\
13272This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13273May be abbreviated to simply \"enable\".\n"),
c5aa993b 13274 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 13275
1a966eab
AC
13276 add_cmd ("once", no_class, enable_once_command, _("\
13277Enable breakpoints for one hit. Give breakpoint numbers.\n\
13278If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13279 &enablebreaklist);
13280
1a966eab
AC
13281 add_cmd ("delete", no_class, enable_delete_command, _("\
13282Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13283If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13284 &enablebreaklist);
13285
1a966eab
AC
13286 add_cmd ("delete", no_class, enable_delete_command, _("\
13287Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13288If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13289 &enablelist);
13290
1a966eab
AC
13291 add_cmd ("once", no_class, enable_once_command, _("\
13292Enable breakpoints for one hit. Give breakpoint numbers.\n\
13293If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13294 &enablelist);
13295
1bedd215
AC
13296 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13297Disable some breakpoints.\n\
c906108c
SS
13298Arguments are breakpoint numbers with spaces in between.\n\
13299To disable all breakpoints, give no argument.\n\
64b9b334 13300A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
13301 &disablelist, "disable ", 1, &cmdlist);
13302 add_com_alias ("dis", "disable", class_breakpoint, 1);
13303 add_com_alias ("disa", "disable", class_breakpoint, 1);
13304 if (xdb_commands)
1bedd215
AC
13305 add_com ("sb", class_breakpoint, disable_command, _("\
13306Disable some breakpoints.\n\
c906108c
SS
13307Arguments are breakpoint numbers with spaces in between.\n\
13308To disable all breakpoints, give no argument.\n\
64b9b334 13309A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 13310
1a966eab
AC
13311 add_cmd ("breakpoints", class_alias, disable_command, _("\
13312Disable some breakpoints.\n\
c906108c
SS
13313Arguments are breakpoint numbers with spaces in between.\n\
13314To disable all breakpoints, give no argument.\n\
64b9b334 13315A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 13316This command may be abbreviated \"disable\"."),
c906108c
SS
13317 &disablelist);
13318
1bedd215
AC
13319 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13320Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13321Arguments are breakpoint numbers with spaces in between.\n\
13322To delete all breakpoints, give no argument.\n\
13323\n\
13324Also a prefix command for deletion of other GDB objects.\n\
1bedd215 13325The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
13326 &deletelist, "delete ", 1, &cmdlist);
13327 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 13328 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 13329 if (xdb_commands)
1bedd215
AC
13330 add_com ("db", class_breakpoint, delete_command, _("\
13331Delete some breakpoints.\n\
c906108c 13332Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 13333To delete all breakpoints, give no argument.\n"));
c906108c 13334
1a966eab
AC
13335 add_cmd ("breakpoints", class_alias, delete_command, _("\
13336Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13337Arguments are breakpoint numbers with spaces in between.\n\
13338To delete all breakpoints, give no argument.\n\
1a966eab 13339This command may be abbreviated \"delete\"."),
c906108c
SS
13340 &deletelist);
13341
1bedd215
AC
13342 add_com ("clear", class_breakpoint, clear_command, _("\
13343Clear breakpoint at specified line or function.\n\
c906108c
SS
13344Argument may be line number, function name, or \"*\" and an address.\n\
13345If line number is specified, all breakpoints in that line are cleared.\n\
13346If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
13347If an address is specified, breakpoints at that address are cleared.\n\
13348\n\
13349With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
13350is executing in.\n\
13351\n\
1bedd215 13352See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 13353 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 13354
1bedd215 13355 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
13356Set breakpoint at specified line or function.\n"
13357BREAK_ARGS_HELP ("break")));
5ba2abeb 13358 set_cmd_completer (c, location_completer);
c94fdfd0 13359
c906108c
SS
13360 add_com_alias ("b", "break", class_run, 1);
13361 add_com_alias ("br", "break", class_run, 1);
13362 add_com_alias ("bre", "break", class_run, 1);
13363 add_com_alias ("brea", "break", class_run, 1);
13364
7681d515
PM
13365 if (xdb_commands)
13366 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
13367
13368 if (dbx_commands)
13369 {
1bedd215
AC
13370 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13371Break in function/address or break at a line in the current file."),
c5aa993b
JM
13372 &stoplist, "stop ", 1, &cmdlist);
13373 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 13374 _("Break in function or address."), &stoplist);
c5aa993b 13375 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 13376 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
13377 add_com ("status", class_info, breakpoints_info, _("\
13378Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13379The \"Type\" column indicates one of:\n\
13380\tbreakpoint - normal breakpoint\n\
13381\twatchpoint - watchpoint\n\
13382The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13383the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13384breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13385address and file/line number respectively.\n\
13386\n\
13387Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13388are set to the address of the last breakpoint listed unless the command\n\
13389is prefixed with \"server \".\n\n\
c906108c 13390Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13391breakpoint set."));
c906108c
SS
13392 }
13393
1bedd215 13394 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 13395Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
13396The \"Type\" column indicates one of:\n\
13397\tbreakpoint - normal breakpoint\n\
13398\twatchpoint - watchpoint\n\
13399The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13400the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13401breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13402address and file/line number respectively.\n\
13403\n\
13404Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13405are set to the address of the last breakpoint listed unless the command\n\
13406is prefixed with \"server \".\n\n\
c906108c 13407Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13408breakpoint set."));
c906108c 13409
6b04bdb7
MS
13410 add_info_alias ("b", "breakpoints", 1);
13411
c906108c 13412 if (xdb_commands)
1bedd215
AC
13413 add_com ("lb", class_breakpoint, breakpoints_info, _("\
13414Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13415The \"Type\" column indicates one of:\n\
13416\tbreakpoint - normal breakpoint\n\
13417\twatchpoint - watchpoint\n\
13418The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13419the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13420breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13421address and file/line number respectively.\n\
13422\n\
13423Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13424are set to the address of the last breakpoint listed unless the command\n\
13425is prefixed with \"server \".\n\n\
c906108c 13426Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13427breakpoint set."));
c906108c 13428
1a966eab
AC
13429 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13430Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13431The \"Type\" column indicates one of:\n\
13432\tbreakpoint - normal breakpoint\n\
13433\twatchpoint - watchpoint\n\
13434\tlongjmp - internal breakpoint used to step through longjmp()\n\
13435\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13436\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
13437\tfinish - internal breakpoint used by the \"finish\" command\n\
13438The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
13439the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13440breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
13441address and file/line number respectively.\n\
13442\n\
13443Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13444are set to the address of the last breakpoint listed unless the command\n\
13445is prefixed with \"server \".\n\n\
c906108c 13446Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 13447breakpoint set."),
c906108c
SS
13448 &maintenanceinfolist);
13449
44feb3ce
TT
13450 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13451Set catchpoints to catch events."),
13452 &catch_cmdlist, "catch ",
13453 0/*allow-unknown*/, &cmdlist);
13454
13455 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13456Set temporary catchpoints to catch events."),
13457 &tcatch_cmdlist, "tcatch ",
13458 0/*allow-unknown*/, &cmdlist);
13459
13460 /* Add catch and tcatch sub-commands. */
13461 add_catch_command ("catch", _("\
13462Catch an exception, when caught.\n\
13463With an argument, catch only exceptions with the given name."),
13464 catch_catch_command,
a96d9b2e 13465 NULL,
44feb3ce
TT
13466 CATCH_PERMANENT,
13467 CATCH_TEMPORARY);
13468 add_catch_command ("throw", _("\
13469Catch an exception, when thrown.\n\
13470With an argument, catch only exceptions with the given name."),
13471 catch_throw_command,
a96d9b2e 13472 NULL,
44feb3ce
TT
13473 CATCH_PERMANENT,
13474 CATCH_TEMPORARY);
13475 add_catch_command ("fork", _("Catch calls to fork."),
13476 catch_fork_command_1,
a96d9b2e 13477 NULL,
44feb3ce
TT
13478 (void *) (uintptr_t) catch_fork_permanent,
13479 (void *) (uintptr_t) catch_fork_temporary);
13480 add_catch_command ("vfork", _("Catch calls to vfork."),
13481 catch_fork_command_1,
a96d9b2e 13482 NULL,
44feb3ce
TT
13483 (void *) (uintptr_t) catch_vfork_permanent,
13484 (void *) (uintptr_t) catch_vfork_temporary);
13485 add_catch_command ("exec", _("Catch calls to exec."),
13486 catch_exec_command_1,
a96d9b2e
SDJ
13487 NULL,
13488 CATCH_PERMANENT,
13489 CATCH_TEMPORARY);
13490 add_catch_command ("syscall", _("\
13491Catch system calls by their names and/or numbers.\n\
13492Arguments say which system calls to catch. If no arguments\n\
13493are given, every system call will be caught.\n\
13494Arguments, if given, should be one or more system call names\n\
13495(if your system supports that), or system call numbers."),
13496 catch_syscall_command_1,
13497 catch_syscall_completer,
44feb3ce
TT
13498 CATCH_PERMANENT,
13499 CATCH_TEMPORARY);
c5aa993b 13500
1bedd215
AC
13501 c = add_com ("watch", class_breakpoint, watch_command, _("\
13502Set a watchpoint for an expression.\n\
06a64a0b 13503Usage: watch [-l|-location] EXPRESSION\n\
c906108c 13504A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13505an expression changes.\n\
13506If -l or -location is given, this evaluates EXPRESSION and watches\n\
13507the memory to which it refers."));
65d12d83 13508 set_cmd_completer (c, expression_completer);
c906108c 13509
1bedd215
AC
13510 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13511Set a read watchpoint for an expression.\n\
06a64a0b 13512Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 13513A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13514an expression is read.\n\
13515If -l or -location is given, this evaluates EXPRESSION and watches\n\
13516the memory to which it refers."));
65d12d83 13517 set_cmd_completer (c, expression_completer);
c906108c 13518
1bedd215
AC
13519 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13520Set a watchpoint for an expression.\n\
06a64a0b 13521Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 13522A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13523an expression is either read or written.\n\
13524If -l or -location is given, this evaluates EXPRESSION and watches\n\
13525the memory to which it refers."));
65d12d83 13526 set_cmd_completer (c, expression_completer);
c906108c 13527
d77f58be 13528 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 13529Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 13530
920d2a44
AC
13531 /* XXX: cagney/2005-02-23: This should be a boolean, and should
13532 respond to changes - contrary to the description. */
85c07804
AC
13533 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
13534 &can_use_hw_watchpoints, _("\
13535Set debugger's willingness to use watchpoint hardware."), _("\
13536Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
13537If zero, gdb will not use hardware for new watchpoints, even if\n\
13538such is available. (However, any hardware watchpoints that were\n\
13539created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
13540hardware.)"),
13541 NULL,
920d2a44 13542 show_can_use_hw_watchpoints,
85c07804 13543 &setlist, &showlist);
c906108c
SS
13544
13545 can_use_hw_watchpoints = 1;
fa8d40ab 13546
1042e4c0
SS
13547 /* Tracepoint manipulation commands. */
13548
13549 c = add_com ("trace", class_breakpoint, trace_command, _("\
13550Set a tracepoint at specified line or function.\n\
13551\n"
13552BREAK_ARGS_HELP ("trace") "\n\
13553Do \"help tracepoints\" for info on other tracepoint commands."));
13554 set_cmd_completer (c, location_completer);
13555
13556 add_com_alias ("tp", "trace", class_alias, 0);
13557 add_com_alias ("tr", "trace", class_alias, 1);
13558 add_com_alias ("tra", "trace", class_alias, 1);
13559 add_com_alias ("trac", "trace", class_alias, 1);
13560
7a697b8d
SS
13561 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
13562Set a fast tracepoint at specified line or function.\n\
13563\n"
13564BREAK_ARGS_HELP ("ftrace") "\n\
13565Do \"help tracepoints\" for info on other tracepoint commands."));
13566 set_cmd_completer (c, location_completer);
13567
0fb4aa4b
PA
13568 c = add_com ("strace", class_breakpoint, strace_command, _("\
13569Set a static tracepoint at specified line, function or marker.\n\
13570\n\
13571strace [LOCATION] [if CONDITION]\n\
13572LOCATION may be a line number, function name, \"*\" and an address,\n\
13573or -m MARKER_ID.\n\
13574If a line number is specified, probe the marker at start of code\n\
13575for that line. If a function is specified, probe the marker at start\n\
13576of code for that function. If an address is specified, probe the marker\n\
13577at that exact address. If a marker id is specified, probe the marker\n\
13578with that name. With no LOCATION, uses current execution address of\n\
13579the selected stack frame.\n\
13580Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
13581This collects arbitrary user data passed in the probe point call to the\n\
13582tracing library. You can inspect it when analyzing the trace buffer,\n\
13583by printing the $_sdata variable like any other convenience variable.\n\
13584\n\
13585CONDITION is a boolean expression.\n\
13586\n\
d41c0fc8
PA
13587Multiple tracepoints at one place are permitted, and useful if their\n\
13588conditions are different.\n\
0fb4aa4b
PA
13589\n\
13590Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
13591Do \"help tracepoints\" for info on other tracepoint commands."));
13592 set_cmd_completer (c, location_completer);
13593
1042e4c0 13594 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 13595Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
13596Convenience variable \"$tpnum\" contains the number of the\n\
13597last tracepoint set."));
13598
13599 add_info_alias ("tp", "tracepoints", 1);
13600
13601 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
13602Delete specified tracepoints.\n\
13603Arguments are tracepoint numbers, separated by spaces.\n\
13604No argument means delete all tracepoints."),
13605 &deletelist);
13606
13607 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13608Disable specified tracepoints.\n\
13609Arguments are tracepoint numbers, separated by spaces.\n\
13610No argument means disable all tracepoints."),
13611 &disablelist);
13612 deprecate_cmd (c, "disable");
13613
13614 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
13615Enable specified tracepoints.\n\
13616Arguments are tracepoint numbers, separated by spaces.\n\
13617No argument means enable all tracepoints."),
13618 &enablelist);
13619 deprecate_cmd (c, "enable");
13620
13621 add_com ("passcount", class_trace, trace_pass_command, _("\
13622Set the passcount for a tracepoint.\n\
13623The trace will end when the tracepoint has been passed 'count' times.\n\
13624Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
13625if TPNUM is omitted, passcount refers to the last tracepoint defined."));
13626
6149aea9
PA
13627 add_prefix_cmd ("save", class_breakpoint, save_command,
13628 _("Save breakpoint definitions as a script."),
13629 &save_cmdlist, "save ",
13630 0/*allow-unknown*/, &cmdlist);
13631
13632 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
13633Save current breakpoint definitions as a script.\n\
cce7e648 13634This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
13635catchpoints, tracepoints). Use the 'source' command in another debug\n\
13636session to restore them."),
13637 &save_cmdlist);
13638 set_cmd_completer (c, filename_completer);
13639
13640 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 13641Save current tracepoint definitions as a script.\n\
6149aea9
PA
13642Use the 'source' command in another debug session to restore them."),
13643 &save_cmdlist);
1042e4c0
SS
13644 set_cmd_completer (c, filename_completer);
13645
6149aea9
PA
13646 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
13647 deprecate_cmd (c, "save tracepoints");
13648
1bedd215 13649 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
13650Breakpoint specific settings\n\
13651Configure various breakpoint-specific variables such as\n\
1bedd215 13652pending breakpoint behavior"),
fa8d40ab
JJ
13653 &breakpoint_set_cmdlist, "set breakpoint ",
13654 0/*allow-unknown*/, &setlist);
1bedd215 13655 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
13656Breakpoint specific settings\n\
13657Configure various breakpoint-specific variables such as\n\
1bedd215 13658pending breakpoint behavior"),
fa8d40ab
JJ
13659 &breakpoint_show_cmdlist, "show breakpoint ",
13660 0/*allow-unknown*/, &showlist);
13661
7915a72c
AC
13662 add_setshow_auto_boolean_cmd ("pending", no_class,
13663 &pending_break_support, _("\
13664Set debugger's behavior regarding pending breakpoints."), _("\
13665Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
13666If on, an unrecognized breakpoint location will cause gdb to create a\n\
13667pending breakpoint. If off, an unrecognized breakpoint location results in\n\
13668an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 13669user-query to see if a pending breakpoint should be created."),
2c5b56ce 13670 NULL,
920d2a44 13671 show_pending_break_support,
6e1d7d6c
AC
13672 &breakpoint_set_cmdlist,
13673 &breakpoint_show_cmdlist);
fa8d40ab
JJ
13674
13675 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
13676
13677 add_setshow_boolean_cmd ("auto-hw", no_class,
13678 &automatic_hardware_breakpoints, _("\
13679Set automatic usage of hardware breakpoints."), _("\
13680Show automatic usage of hardware breakpoints."), _("\
13681If set, the debugger will automatically use hardware breakpoints for\n\
13682breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
13683a warning will be emitted for such breakpoints."),
13684 NULL,
13685 show_automatic_hardware_breakpoints,
13686 &breakpoint_set_cmdlist,
13687 &breakpoint_show_cmdlist);
74960c60 13688
33e5cbd6
PA
13689 add_setshow_enum_cmd ("always-inserted", class_support,
13690 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
13691Set mode for inserting breakpoints."), _("\
13692Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
13693When this mode is off, breakpoints are inserted in inferior when it is\n\
13694resumed, and removed when execution stops. When this mode is on,\n\
13695breakpoints are inserted immediately and removed only when the user\n\
13696deletes the breakpoint. When this mode is auto (which is the default),\n\
13697the behaviour depends on the non-stop setting (see help set non-stop).\n\
13698In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
13699behaves as if always-inserted mode is on; if gdb is controlling the\n\
13700inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
13701 NULL,
13702 &show_always_inserted_mode,
13703 &breakpoint_set_cmdlist,
13704 &breakpoint_show_cmdlist);
f1310107
TJB
13705
13706 add_com ("break-range", class_breakpoint, break_range_command, _("\
13707Set a breakpoint for an address range.\n\
13708break-range START-LOCATION, END-LOCATION\n\
13709where START-LOCATION and END-LOCATION can be one of the following:\n\
13710 LINENUM, for that line in the current file,\n\
13711 FILE:LINENUM, for that line in that file,\n\
13712 +OFFSET, for that number of lines after the current line\n\
13713 or the start of the range\n\
13714 FUNCTION, for the first line in that function,\n\
13715 FILE:FUNCTION, to distinguish among like-named static functions.\n\
13716 *ADDRESS, for the instruction at that address.\n\
13717\n\
13718The breakpoint will stop execution of the inferior whenever it executes\n\
13719an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
13720range (including START-LOCATION and END-LOCATION)."));
13721
765dc015 13722 automatic_hardware_breakpoints = 1;
f3b1572e
PA
13723
13724 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 13725}
This page took 2.047874 seconds and 4 git commands to generate.