Fix typo in ada-lang.c comment.
[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,
4c38e0a4 5 2008, 2009, 2010 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"
42#include "annotate.h"
43#include "symfile.h"
44#include "objfiles.h"
0378c332 45#include "source.h"
c5f0f3d0 46#include "linespec.h"
c94fdfd0 47#include "completer.h"
5b7f31a4 48#include "gdb.h"
8b93c638 49#include "ui-out.h"
e1507482 50#include "cli/cli-script.h"
0225421b 51#include "gdb_assert.h"
fe898f56 52#include "block.h"
a77053c2 53#include "solib.h"
84acb35a
JJ
54#include "solist.h"
55#include "observer.h"
60250e8b 56#include "exceptions.h"
765dc015 57#include "memattr.h"
f7f9143b 58#include "ada-lang.h"
d1aa2f50 59#include "top.h"
fa4727a6 60#include "wrapper.h"
79a45b7d 61#include "valprint.h"
4efc6507 62#include "jit.h"
a96d9b2e 63#include "xml-syscall.h"
65d79d4b 64#include "parser-defs.h"
c906108c 65
1042e4c0
SS
66/* readline include files */
67#include "readline/readline.h"
68#include "readline/history.h"
69
70/* readline defines this. */
71#undef savestring
72
034dad6f 73#include "mi/mi-common.h"
104c1213 74
44feb3ce
TT
75/* Arguments to pass as context to some catch command handlers. */
76#define CATCH_PERMANENT ((void *) (uintptr_t) 0)
77#define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
c906108c 78
44feb3ce 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
a14ed312 97static void clear_command (char *, int);
c906108c 98
a14ed312 99static void catch_command (char *, int);
c906108c 100
a14ed312 101static int can_use_hardware_watchpoint (struct value *);
c906108c 102
98deb0da 103static void break_command_1 (char *, int, int);
c906108c 104
a14ed312 105static void mention (struct breakpoint *);
c906108c 106
63c252f8
PM
107/* This function is used in gdbtk sources and thus can not be made static. */
108struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
a6d9a66e
UW
109 struct symtab_and_line,
110 enum bptype);
c906108c 111
76897487
KB
112static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
113
a6d9a66e
UW
114static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
115 CORE_ADDR bpaddr,
88f7da05 116 enum bptype bptype);
76897487 117
6c95b8df
PA
118static void describe_other_breakpoints (struct gdbarch *,
119 struct program_space *, CORE_ADDR,
5af949e3 120 struct obj_section *, int);
c906108c 121
6c95b8df
PA
122static int breakpoint_address_match (struct address_space *aspace1,
123 CORE_ADDR addr1,
124 struct address_space *aspace2,
125 CORE_ADDR addr2);
126
85d721b8
PA
127static int watchpoint_locations_match (struct bp_location *loc1,
128 struct bp_location *loc2);
129
a14ed312 130static void breakpoints_info (char *, int);
c906108c 131
d77f58be
SS
132static void watchpoints_info (char *, int);
133
134static int breakpoint_1 (int, int, int (*) (const struct breakpoint *));
c906108c 135
4efb68b1 136static int breakpoint_cond_eval (void *);
c906108c 137
4efb68b1 138static void cleanup_executing_breakpoints (void *);
c906108c 139
a14ed312 140static void commands_command (char *, int);
c906108c 141
a14ed312 142static void condition_command (char *, int);
c906108c 143
a14ed312 144static int get_number_trailer (char **, int);
c906108c 145
c5aa993b
JM
146typedef enum
147 {
148 mark_inserted,
149 mark_uninserted
150 }
151insertion_state_t;
c906108c 152
0bde7532 153static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 154static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 155
a14ed312 156static enum print_stop_action print_it_typical (bpstat);
e514a9d6
JM
157
158static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 159
4efb68b1 160static int watchpoint_check (void *);
c906108c 161
a14ed312 162static void maintenance_info_breakpoints (char *, int);
c906108c 163
a14ed312 164static int hw_breakpoint_used_count (void);
c906108c 165
a14ed312 166static int hw_watchpoint_used_count (enum bptype, int *);
c906108c 167
a14ed312 168static void hbreak_command (char *, int);
c906108c 169
a14ed312 170static void thbreak_command (char *, int);
c906108c 171
a14ed312 172static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
c906108c 173
a14ed312 174static void stop_command (char *arg, int from_tty);
7a292a7a 175
a14ed312 176static void stopin_command (char *arg, int from_tty);
7a292a7a 177
a14ed312 178static void stopat_command (char *arg, int from_tty);
7a292a7a 179
a14ed312 180static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 181
d85310f7
MS
182static void catch_exception_command_1 (enum exception_event_kind ex_event,
183 char *arg, int tempflag, int from_tty);
7a292a7a 184
a14ed312 185static void tcatch_command (char *arg, int from_tty);
7a292a7a 186
a14ed312 187static void ep_skip_leading_whitespace (char **s);
7a292a7a 188
d03285ec
UW
189static void detach_single_step_breakpoints (void);
190
6c95b8df
PA
191static int single_step_breakpoint_inserted_here_p (struct address_space *,
192 CORE_ADDR pc);
1aafd4da 193
fe3f5fa8 194static void free_bp_location (struct bp_location *loc);
f431efe5
PA
195static void incref_bp_location (struct bp_location *loc);
196static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 197
39d61571 198static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 199
b60e7edf 200static void update_global_location_list (int);
a5606eee 201
b60e7edf 202static void update_global_location_list_nothrow (int);
74960c60 203
d77f58be 204static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60 205
d77f58be 206static int is_watchpoint (const struct breakpoint *bpt);
60e1c644 207
74960c60 208static void insert_breakpoint_locations (void);
a5606eee 209
a96d9b2e
SDJ
210static int syscall_catchpoint_p (struct breakpoint *b);
211
1042e4c0
SS
212static void tracepoints_info (char *, int);
213
214static void delete_trace_command (char *, int);
215
216static void enable_trace_command (char *, int);
217
218static void disable_trace_command (char *, int);
219
220static void trace_pass_command (char *, int);
221
0fb4aa4b
PA
222/* Assuming we're creating a static tracepoint, does S look like a
223 static tracepoint marker spec ("-m MARKER_ID")? */
224#define is_marker_spec(s) \
f5a8e22b 225 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 226
5cea2a26
PA
227/* A reference-counted struct command_line. This lets multiple
228 breakpoints share a single command list. */
229struct counted_command_line
230{
231 /* The reference count. */
232 int refc;
233
234 /* The command list. */
235 struct command_line *commands;
236};
237
238struct command_line *
239breakpoint_commands (struct breakpoint *b)
240{
241 return b->commands ? b->commands->commands : NULL;
242}
3daf8fe5 243
f3b1572e
PA
244/* Flag indicating that a command has proceeded the inferior past the
245 current breakpoint. */
246
247static int breakpoint_proceeded;
248
2cec12e5
AR
249static const char *
250bpdisp_text (enum bpdisp disp)
251{
252 /* NOTE: the following values are a part of MI protocol and represent
253 values of 'disp' field returned when inferior stops at a breakpoint. */
bc043ef3 254 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 255
2cec12e5
AR
256 return bpdisps[(int) disp];
257}
c906108c 258
2cec12e5 259/* Prototypes for exported functions. */
c906108c
SS
260/* If FALSE, gdb will not use hardware support for watchpoints, even
261 if such is available. */
262static int can_use_hw_watchpoints;
263
920d2a44
AC
264static void
265show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
266 struct cmd_list_element *c,
267 const char *value)
268{
269 fprintf_filtered (file, _("\
270Debugger's willingness to use watchpoint hardware is %s.\n"),
271 value);
272}
273
fa8d40ab
JJ
274/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
275 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
276 for unrecognized breakpoint locations.
277 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
278static enum auto_boolean pending_break_support;
920d2a44
AC
279static void
280show_pending_break_support (struct ui_file *file, int from_tty,
281 struct cmd_list_element *c,
282 const char *value)
283{
284 fprintf_filtered (file, _("\
285Debugger's behavior regarding pending breakpoints is %s.\n"),
286 value);
287}
fa8d40ab 288
765dc015
VP
289/* If 1, gdb will automatically use hardware breakpoints for breakpoints
290 set with "break" but falling in read-only memory.
291 If 0, gdb will warn about such breakpoints, but won't automatically
292 use hardware breakpoints. */
293static int automatic_hardware_breakpoints;
294static void
295show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
296 struct cmd_list_element *c,
297 const char *value)
298{
299 fprintf_filtered (file, _("\
300Automatic usage of hardware breakpoints is %s.\n"),
301 value);
302}
303
33e5cbd6
PA
304/* If on, gdb will keep breakpoints inserted even as inferior is
305 stopped, and immediately insert any new breakpoints. If off, gdb
306 will insert breakpoints into inferior only when resuming it, and
307 will remove breakpoints upon stop. If auto, GDB will behave as ON
308 if in non-stop mode, and as OFF if all-stop mode.*/
309
310static const char always_inserted_auto[] = "auto";
311static const char always_inserted_on[] = "on";
312static const char always_inserted_off[] = "off";
313static const char *always_inserted_enums[] = {
314 always_inserted_auto,
315 always_inserted_off,
316 always_inserted_on,
317 NULL
318};
319static const char *always_inserted_mode = always_inserted_auto;
320static void
74960c60 321show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 322 struct cmd_list_element *c, const char *value)
74960c60 323{
33e5cbd6
PA
324 if (always_inserted_mode == always_inserted_auto)
325 fprintf_filtered (file, _("\
326Always inserted breakpoint mode is %s (currently %s).\n"),
327 value,
328 breakpoints_always_inserted_mode () ? "on" : "off");
329 else
330 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"), value);
74960c60
VP
331}
332
33e5cbd6
PA
333int
334breakpoints_always_inserted_mode (void)
335{
336 return (always_inserted_mode == always_inserted_on
337 || (always_inserted_mode == always_inserted_auto && non_stop));
338}
765dc015 339
a14ed312 340void _initialize_breakpoint (void);
c906108c 341
c906108c
SS
342/* Are we executing breakpoint commands? */
343static int executing_breakpoint_commands;
344
c02f5703
MS
345/* Are overlay event breakpoints enabled? */
346static int overlay_events_enabled;
347
c906108c
SS
348/* Walk the following statement or block through all breakpoints.
349 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the current
350 breakpoint. */
351
5c44784c 352#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 353
5c44784c
JM
354#define ALL_BREAKPOINTS_SAFE(B,TMP) \
355 for (B = breakpoint_chain; \
356 B ? (TMP=B->next, 1): 0; \
357 B = TMP)
c906108c 358
876fa593
JK
359/* Similar iterator for the low-level breakpoints. SAFE variant is not
360 provided so update_global_location_list must not be called while executing
361 the block of ALL_BP_LOCATIONS. */
7cc221ef 362
876fa593
JK
363#define ALL_BP_LOCATIONS(B,BP_TMP) \
364 for (BP_TMP = bp_location; \
365 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
366 BP_TMP++)
7cc221ef 367
1042e4c0
SS
368/* Iterator for tracepoints only. */
369
370#define ALL_TRACEPOINTS(B) \
371 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 372 if (is_tracepoint (B))
1042e4c0 373
7cc221ef 374/* Chains of all breakpoints defined. */
c906108c
SS
375
376struct breakpoint *breakpoint_chain;
377
876fa593
JK
378/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
379
380static struct bp_location **bp_location;
381
382/* Number of elements of BP_LOCATION. */
383
384static unsigned bp_location_count;
385
386/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and ADDRESS
387 for the current elements of BP_LOCATION which get a valid result from
388 bp_location_has_shadow. You can use it for roughly limiting the subrange of
389 BP_LOCATION to scan for shadow bytes for an address you need to read. */
390
391static CORE_ADDR bp_location_placed_address_before_address_max;
392
393/* Maximum offset plus alignment between
394 bp_target_info.PLACED_ADDRESS + bp_target_info.SHADOW_LEN and ADDRESS for
395 the current elements of BP_LOCATION which get a valid result from
396 bp_location_has_shadow. You can use it for roughly limiting the subrange of
397 BP_LOCATION to scan for shadow bytes for an address you need to read. */
398
399static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 400
20874c92 401/* The locations that no longer correspond to any breakpoint,
876fa593 402 unlinked from bp_location array, but for which a hit
20874c92
VP
403 may still be reported by a target. */
404VEC(bp_location_p) *moribund_locations = NULL;
405
c906108c
SS
406/* Number of last breakpoint made. */
407
95a42b64
TT
408static int breakpoint_count;
409
86b17b60
PA
410/* The value of `breakpoint_count' before the last command that
411 created breakpoints. If the last (break-like) command created more
412 than one breakpoint, then the difference between BREAKPOINT_COUNT
413 and PREV_BREAKPOINT_COUNT is more than one. */
414static int prev_breakpoint_count;
c906108c 415
1042e4c0
SS
416/* Number of last tracepoint made. */
417
95a42b64 418static int tracepoint_count;
1042e4c0 419
6149aea9
PA
420static struct cmd_list_element *breakpoint_set_cmdlist;
421static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 422struct cmd_list_element *save_cmdlist;
6149aea9 423
468d015d
JJ
424/* Return whether a breakpoint is an active enabled breakpoint. */
425static int
426breakpoint_enabled (struct breakpoint *b)
427{
0d381245 428 return (b->enable_state == bp_enabled);
468d015d
JJ
429}
430
c906108c
SS
431/* Set breakpoint count to NUM. */
432
95a42b64 433static void
fba45db2 434set_breakpoint_count (int num)
c906108c 435{
86b17b60 436 prev_breakpoint_count = breakpoint_count;
c906108c 437 breakpoint_count = num;
4fa62494 438 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
439}
440
86b17b60
PA
441/* Used by `start_rbreak_breakpoints' below, to record the current
442 breakpoint count before "rbreak" creates any breakpoint. */
443static int rbreak_start_breakpoint_count;
444
95a42b64
TT
445/* Called at the start an "rbreak" command to record the first
446 breakpoint made. */
86b17b60 447
95a42b64
TT
448void
449start_rbreak_breakpoints (void)
450{
86b17b60 451 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
452}
453
454/* Called at the end of an "rbreak" command to record the last
455 breakpoint made. */
86b17b60 456
95a42b64
TT
457void
458end_rbreak_breakpoints (void)
459{
86b17b60 460 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
461}
462
c906108c
SS
463/* Used in run_command to zero the hit count when a new run starts. */
464
465void
fba45db2 466clear_breakpoint_hit_counts (void)
c906108c
SS
467{
468 struct breakpoint *b;
469
470 ALL_BREAKPOINTS (b)
471 b->hit_count = 0;
472}
473
9add0f1b
TT
474/* Allocate a new counted_command_line with reference count of 1.
475 The new structure owns COMMANDS. */
476
477static struct counted_command_line *
478alloc_counted_command_line (struct command_line *commands)
479{
480 struct counted_command_line *result
481 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 482
9add0f1b
TT
483 result->refc = 1;
484 result->commands = commands;
485 return result;
486}
487
488/* Increment reference count. This does nothing if CMD is NULL. */
489
490static void
491incref_counted_command_line (struct counted_command_line *cmd)
492{
493 if (cmd)
494 ++cmd->refc;
495}
496
497/* Decrement reference count. If the reference count reaches 0,
498 destroy the counted_command_line. Sets *CMDP to NULL. This does
499 nothing if *CMDP is NULL. */
500
501static void
502decref_counted_command_line (struct counted_command_line **cmdp)
503{
504 if (*cmdp)
505 {
506 if (--(*cmdp)->refc == 0)
507 {
508 free_command_lines (&(*cmdp)->commands);
509 xfree (*cmdp);
510 }
511 *cmdp = NULL;
512 }
513}
514
515/* A cleanup function that calls decref_counted_command_line. */
516
517static void
518do_cleanup_counted_command_line (void *arg)
519{
520 decref_counted_command_line (arg);
521}
522
523/* Create a cleanup that calls decref_counted_command_line on the
524 argument. */
525
526static struct cleanup *
527make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
528{
529 return make_cleanup (do_cleanup_counted_command_line, cmdp);
530}
531
c906108c
SS
532/* Default address, symtab and line to put a breakpoint at
533 for "break" command with no arg.
534 if default_breakpoint_valid is zero, the other three are
535 not valid, and "break" with no arg is an error.
536
537 This set by print_stack_frame, which calls set_default_breakpoint. */
538
539int default_breakpoint_valid;
540CORE_ADDR default_breakpoint_address;
541struct symtab *default_breakpoint_symtab;
542int default_breakpoint_line;
6c95b8df
PA
543struct program_space *default_breakpoint_pspace;
544
c906108c
SS
545\f
546/* *PP is a string denoting a breakpoint. Get the number of the breakpoint.
547 Advance *PP after the string and any trailing whitespace.
548
549 Currently the string can either be a number or "$" followed by the name
550 of a convenience variable. Making it an expression wouldn't work well
5c44784c 551 for map_breakpoint_numbers (e.g. "4 + 5 + 6").
40c03ae8
EZ
552
553 If the string is a NULL pointer, that denotes the last breakpoint.
5c44784c
JM
554
555 TRAILER is a character which can be found after the number; most
556 commonly this is `-'. If you don't want a trailer, use \0. */
c906108c 557static int
fba45db2 558get_number_trailer (char **pp, int trailer)
c906108c 559{
5c44784c 560 int retval = 0; /* default */
c906108c
SS
561 char *p = *pp;
562
563 if (p == NULL)
564 /* Empty line means refer to the last breakpoint. */
565 return breakpoint_count;
566 else if (*p == '$')
567 {
568 /* Make a copy of the name, so we can null-terminate it
c5aa993b 569 to pass to lookup_internalvar(). */
c906108c
SS
570 char *varname;
571 char *start = ++p;
4fa62494 572 LONGEST val;
c906108c
SS
573
574 while (isalnum (*p) || *p == '_')
575 p++;
576 varname = (char *) alloca (p - start + 1);
577 strncpy (varname, start, p - start);
578 varname[p - start] = '\0';
4fa62494
UW
579 if (get_internalvar_integer (lookup_internalvar (varname), &val))
580 retval = (int) val;
5c44784c
JM
581 else
582 {
a3f17187 583 printf_filtered (_("Convenience variable must have integer value.\n"));
5c44784c
JM
584 retval = 0;
585 }
c906108c
SS
586 }
587 else
588 {
589 if (*p == '-')
590 ++p;
591 while (*p >= '0' && *p <= '9')
592 ++p;
593 if (p == *pp)
594 /* There is no number here. (e.g. "cond a == b"). */
5c44784c
JM
595 {
596 /* Skip non-numeric token */
597 while (*p && !isspace((int) *p))
598 ++p;
599 /* Return zero, which caller must interpret as error. */
600 retval = 0;
601 }
602 else
603 retval = atoi (*pp);
604 }
605 if (!(isspace (*p) || *p == '\0' || *p == trailer))
606 {
607 /* Trailing junk: return 0 and let caller print error msg. */
608 while (!(isspace (*p) || *p == '\0' || *p == trailer))
609 ++p;
610 retval = 0;
c906108c 611 }
c906108c
SS
612 while (isspace (*p))
613 p++;
614 *pp = p;
615 return retval;
616}
5c44784c 617
11cf8741 618
5c44784c
JM
619/* Like get_number_trailer, but don't allow a trailer. */
620int
fba45db2 621get_number (char **pp)
5c44784c
JM
622{
623 return get_number_trailer (pp, '\0');
624}
625
626/* Parse a number or a range.
627 * A number will be of the form handled by get_number.
628 * A range will be of the form <number1> - <number2>, and
629 * will represent all the integers between number1 and number2,
630 * inclusive.
631 *
632 * While processing a range, this fuction is called iteratively;
633 * At each call it will return the next value in the range.
634 *
635 * At the beginning of parsing a range, the char pointer PP will
636 * be advanced past <number1> and left pointing at the '-' token.
637 * Subsequent calls will not advance the pointer until the range
638 * is completed. The call that completes the range will advance
639 * pointer PP past <number2>.
640 */
641
642int
fba45db2 643get_number_or_range (char **pp)
5c44784c
JM
644{
645 static int last_retval, end_value;
646 static char *end_ptr;
647 static int in_range = 0;
648
649 if (**pp != '-')
650 {
651 /* Default case: pp is pointing either to a solo number,
652 or to the first number of a range. */
653 last_retval = get_number_trailer (pp, '-');
654 if (**pp == '-')
655 {
656 char **temp;
657
658 /* This is the start of a range (<number1> - <number2>).
659 Skip the '-', parse and remember the second number,
660 and also remember the end of the final token. */
661
662 temp = &end_ptr;
663 end_ptr = *pp + 1;
664 while (isspace ((int) *end_ptr))
665 end_ptr++; /* skip white space */
666 end_value = get_number (temp);
667 if (end_value < last_retval)
668 {
8a3fe4f8 669 error (_("inverted range"));
5c44784c
JM
670 }
671 else if (end_value == last_retval)
672 {
673 /* degenerate range (number1 == number2). Advance the
674 token pointer so that the range will be treated as a
675 single number. */
676 *pp = end_ptr;
677 }
678 else
679 in_range = 1;
680 }
681 }
682 else if (! in_range)
8a3fe4f8 683 error (_("negative value"));
5c44784c
JM
684 else
685 {
686 /* pp points to the '-' that betokens a range. All
687 number-parsing has already been done. Return the next
688 integer value (one greater than the saved previous value).
689 Do not advance the token pointer 'pp' until the end of range
690 is reached. */
691
692 if (++last_retval == end_value)
693 {
694 /* End of range reached; advance token pointer. */
695 *pp = end_ptr;
696 in_range = 0;
697 }
698 }
699 return last_retval;
700}
701
48cb2d85
VP
702/* Return the breakpoint with the specified number, or NULL
703 if the number does not refer to an existing breakpoint. */
704
705struct breakpoint *
706get_breakpoint (int num)
707{
708 struct breakpoint *b;
709
710 ALL_BREAKPOINTS (b)
711 if (b->number == num)
712 return b;
713
714 return NULL;
715}
5c44784c 716
c906108c 717\f
adc36818
PM
718
719void
720set_breakpoint_condition (struct breakpoint *b, char *exp,
721 int from_tty)
722{
723 struct bp_location *loc = b->loc;
724
725 for (; loc; loc = loc->next)
726 {
727 xfree (loc->cond);
728 loc->cond = NULL;
729 }
730 xfree (b->cond_string);
731 b->cond_string = NULL;
732 xfree (b->cond_exp);
733 b->cond_exp = NULL;
734
735 if (*exp == 0)
736 {
737 if (from_tty)
738 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
739 }
740 else
741 {
742 char *arg = exp;
cc59ec59 743
adc36818
PM
744 /* I don't know if it matters whether this is the string the user
745 typed in or the decompiled expression. */
746 b->cond_string = xstrdup (arg);
747 b->condition_not_parsed = 0;
748
749 if (is_watchpoint (b))
750 {
751 innermost_block = NULL;
752 arg = exp;
753 b->cond_exp = parse_exp_1 (&arg, 0, 0);
754 if (*arg)
755 error (_("Junk at end of expression"));
756 b->cond_exp_valid_block = innermost_block;
757 }
758 else
759 {
760 for (loc = b->loc; loc; loc = loc->next)
761 {
762 arg = exp;
763 loc->cond =
764 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
765 if (*arg)
766 error (_("Junk at end of expression"));
767 }
768 }
769 }
770 breakpoints_changed ();
771 observer_notify_breakpoint_modified (b->number);
772}
773
c906108c
SS
774/* condition N EXP -- set break condition of breakpoint N to EXP. */
775
776static void
fba45db2 777condition_command (char *arg, int from_tty)
c906108c 778{
52f0bd74 779 struct breakpoint *b;
c906108c 780 char *p;
52f0bd74 781 int bnum;
c906108c
SS
782
783 if (arg == 0)
e2e0b3e5 784 error_no_arg (_("breakpoint number"));
c906108c
SS
785
786 p = arg;
787 bnum = get_number (&p);
5c44784c 788 if (bnum == 0)
8a3fe4f8 789 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
790
791 ALL_BREAKPOINTS (b)
792 if (b->number == bnum)
2f069f6f 793 {
2566ad2d 794 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
795 return;
796 }
c906108c 797
8a3fe4f8 798 error (_("No breakpoint number %d."), bnum);
c906108c
SS
799}
800
a7bdde9e
VP
801/* Check that COMMAND do not contain commands that are suitable
802 only for tracepoints and not suitable for ordinary breakpoints.
803 Throw if any such commands is found.
804*/
805static void
806check_no_tracepoint_commands (struct command_line *commands)
807{
808 struct command_line *c;
cc59ec59 809
a7bdde9e
VP
810 for (c = commands; c; c = c->next)
811 {
812 int i;
813
814 if (c->control_type == while_stepping_control)
815 error (_("The 'while-stepping' command can only be used for tracepoints"));
816
817 for (i = 0; i < c->body_count; ++i)
818 check_no_tracepoint_commands ((c->body_list)[i]);
819
820 /* Not that command parsing removes leading whitespace and comment
821 lines and also empty lines. So, we only need to check for
822 command directly. */
823 if (strstr (c->line, "collect ") == c->line)
824 error (_("The 'collect' command can only be used for tracepoints"));
825
51661e93
VP
826 if (strstr (c->line, "teval ") == c->line)
827 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
828 }
829}
830
d77f58be
SS
831/* Encapsulate tests for different types of tracepoints. */
832
a7bdde9e 833int
d77f58be 834is_tracepoint (const struct breakpoint *b)
a7bdde9e 835{
0fb4aa4b
PA
836 return (b->type == bp_tracepoint
837 || b->type == bp_fast_tracepoint
838 || b->type == bp_static_tracepoint);
a7bdde9e 839}
d77f58be 840
95a42b64
TT
841/* A helper function that validsates that COMMANDS are valid for a
842 breakpoint. This function will throw an exception if a problem is
843 found. */
48cb2d85 844
95a42b64
TT
845static void
846validate_commands_for_breakpoint (struct breakpoint *b,
847 struct command_line *commands)
48cb2d85 848{
d77f58be 849 if (is_tracepoint (b))
a7bdde9e
VP
850 {
851 /* We need to verify that each top-level element of commands
852 is valid for tracepoints, that there's at most one while-stepping
853 element, and that while-stepping's body has valid tracing commands
854 excluding nested while-stepping. */
855 struct command_line *c;
856 struct command_line *while_stepping = 0;
857 for (c = commands; c; c = c->next)
858 {
a7bdde9e
VP
859 if (c->control_type == while_stepping_control)
860 {
861 if (b->type == bp_fast_tracepoint)
0fb4aa4b
PA
862 error (_("\
863The 'while-stepping' command cannot be used for fast tracepoint"));
864 else if (b->type == bp_static_tracepoint)
865 error (_("\
866The 'while-stepping' command cannot be used for static tracepoint"));
a7bdde9e
VP
867
868 if (while_stepping)
869 error (_("The 'while-stepping' command can be used only once"));
870 else
871 while_stepping = c;
872 }
873 }
874 if (while_stepping)
875 {
876 struct command_line *c2;
877
878 gdb_assert (while_stepping->body_count == 1);
879 c2 = while_stepping->body_list[0];
880 for (; c2; c2 = c2->next)
881 {
a7bdde9e
VP
882 if (c2->control_type == while_stepping_control)
883 error (_("The 'while-stepping' command cannot be nested"));
884 }
885 }
886 }
887 else
888 {
889 check_no_tracepoint_commands (commands);
890 }
95a42b64
TT
891}
892
0fb4aa4b
PA
893/* Return a vector of all the static tracepoints set at ADDR. The
894 caller is responsible for releasing the vector. */
895
896VEC(breakpoint_p) *
897static_tracepoints_here (CORE_ADDR addr)
898{
899 struct breakpoint *b;
900 VEC(breakpoint_p) *found = 0;
901 struct bp_location *loc;
902
903 ALL_BREAKPOINTS (b)
904 if (b->type == bp_static_tracepoint)
905 {
906 for (loc = b->loc; loc; loc = loc->next)
907 if (loc->address == addr)
908 VEC_safe_push(breakpoint_p, found, b);
909 }
910
911 return found;
912}
913
95a42b64
TT
914/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
915 validate that only allowed commands are included.
916*/
917
918void
919breakpoint_set_commands (struct breakpoint *b, struct command_line *commands)
920{
921 validate_commands_for_breakpoint (b, commands);
a7bdde9e 922
9add0f1b
TT
923 decref_counted_command_line (&b->commands);
924 b->commands = alloc_counted_command_line (commands);
48cb2d85
VP
925 breakpoints_changed ();
926 observer_notify_breakpoint_modified (b->number);
927}
928
95a42b64
TT
929void
930check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
931{
932 struct breakpoint *b = closure;
cc59ec59 933
a7bdde9e
VP
934 validate_actionline (&line, b);
935}
936
95a42b64
TT
937/* A structure used to pass information through
938 map_breakpoint_numbers. */
939
940struct commands_info
941{
942 /* True if the command was typed at a tty. */
943 int from_tty;
86b17b60
PA
944
945 /* The breakpoint range spec. */
946 char *arg;
947
95a42b64
TT
948 /* Non-NULL if the body of the commands are being read from this
949 already-parsed command. */
950 struct command_line *control;
86b17b60 951
95a42b64
TT
952 /* The command lines read from the user, or NULL if they have not
953 yet been read. */
954 struct counted_command_line *cmd;
955};
956
957/* A callback for map_breakpoint_numbers that sets the commands for
958 commands_command. */
959
c906108c 960static void
95a42b64 961do_map_commands_command (struct breakpoint *b, void *data)
c906108c 962{
95a42b64 963 struct commands_info *info = data;
c906108c 964
95a42b64
TT
965 if (info->cmd == NULL)
966 {
967 struct command_line *l;
5c44784c 968
95a42b64
TT
969 if (info->control != NULL)
970 l = copy_command_lines (info->control->body_list[0]);
971 else
86b17b60
PA
972 {
973 struct cleanup *old_chain;
974 char *str;
c5aa993b 975
86b17b60
PA
976 str = xstrprintf (_("Type commands for breakpoint(s) %s, one per line."),
977 info->arg);
978
979 old_chain = make_cleanup (xfree, str);
980
981 l = read_command_lines (str,
982 info->from_tty, 1,
d77f58be 983 (is_tracepoint (b)
86b17b60
PA
984 ? check_tracepoint_command : 0),
985 b);
986
987 do_cleanups (old_chain);
988 }
a7bdde9e 989
95a42b64
TT
990 info->cmd = alloc_counted_command_line (l);
991 }
992
993 /* If a breakpoint was on the list more than once, we don't need to
994 do anything. */
995 if (b->commands != info->cmd)
996 {
997 validate_commands_for_breakpoint (b, info->cmd->commands);
998 incref_counted_command_line (info->cmd);
999 decref_counted_command_line (&b->commands);
1000 b->commands = info->cmd;
1001 breakpoints_changed ();
1002 observer_notify_breakpoint_modified (b->number);
c5aa993b 1003 }
95a42b64
TT
1004}
1005
1006static void
1007commands_command_1 (char *arg, int from_tty, struct command_line *control)
1008{
1009 struct cleanup *cleanups;
1010 struct commands_info info;
1011
1012 info.from_tty = from_tty;
1013 info.control = control;
1014 info.cmd = NULL;
1015 /* If we read command lines from the user, then `info' will hold an
1016 extra reference to the commands that we must clean up. */
1017 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1018
1019 if (arg == NULL || !*arg)
1020 {
86b17b60
PA
1021 if (breakpoint_count - prev_breakpoint_count > 1)
1022 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1, breakpoint_count);
95a42b64
TT
1023 else if (breakpoint_count > 0)
1024 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1025 else
1026 {
1027 /* So that we don't try to free the incoming non-NULL
1028 argument in the cleanup below. Mapping breakpoint
1029 numbers will fail in this case. */
1030 arg = NULL;
1031 }
95a42b64 1032 }
9766ced4
SS
1033 else
1034 /* The command loop has some static state, so we need to preserve
1035 our argument. */
1036 arg = xstrdup (arg);
86b17b60
PA
1037
1038 if (arg != NULL)
1039 make_cleanup (xfree, arg);
1040
1041 info.arg = arg;
95a42b64
TT
1042
1043 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1044
1045 if (info.cmd == NULL)
1046 error (_("No breakpoints specified."));
1047
1048 do_cleanups (cleanups);
1049}
1050
1051static void
1052commands_command (char *arg, int from_tty)
1053{
1054 commands_command_1 (arg, from_tty, NULL);
c906108c 1055}
40c03ae8
EZ
1056
1057/* Like commands_command, but instead of reading the commands from
1058 input stream, takes them from an already parsed command structure.
1059
1060 This is used by cli-script.c to DTRT with breakpoint commands
1061 that are part of if and while bodies. */
1062enum command_control_type
1063commands_from_control_command (char *arg, struct command_line *cmd)
1064{
95a42b64
TT
1065 commands_command_1 (arg, 0, cmd);
1066 return simple_control;
40c03ae8 1067}
876fa593
JK
1068
1069/* Return non-zero if BL->TARGET_INFO contains valid information. */
1070
1071static int
1072bp_location_has_shadow (struct bp_location *bl)
1073{
1074 if (bl->loc_type != bp_loc_software_breakpoint)
1075 return 0;
1076 if (!bl->inserted)
1077 return 0;
1078 if (bl->target_info.shadow_len == 0)
1079 /* bp isn't valid, or doesn't shadow memory. */
1080 return 0;
1081 return 1;
1082}
1083
8defab1a 1084/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1085 by replacing any memory breakpoints with their shadowed contents.
1086
1087 The range of shadowed area by each bp_location is:
35df4500
TJB
1088 bl->address - bp_location_placed_address_before_address_max
1089 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1090 The range we were requested to resolve shadows for is:
1091 memaddr ... memaddr + len
1092 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1093 memaddr + len <= (bl->address
1094 - bp_location_placed_address_before_address_max)
876fa593 1095 and:
35df4500 1096 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1097
8defab1a
DJ
1098void
1099breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1100{
876fa593
JK
1101 /* Left boundary, right boundary and median element of our binary search. */
1102 unsigned bc_l, bc_r, bc;
1103
1104 /* Find BC_L which is a leftmost element which may affect BUF content. It is
1105 safe to report lower value but a failure to report higher one. */
1106
1107 bc_l = 0;
1108 bc_r = bp_location_count;
1109 while (bc_l + 1 < bc_r)
1110 {
35df4500 1111 struct bp_location *bl;
876fa593
JK
1112
1113 bc = (bc_l + bc_r) / 2;
35df4500 1114 bl = bp_location[bc];
876fa593 1115
35df4500 1116 /* Check first BL->ADDRESS will not overflow due to the added constant.
876fa593
JK
1117 Then advance the left boundary only if we are sure the BC element can
1118 in no way affect the BUF content (MEMADDR to MEMADDR + LEN range).
1119
1120 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety offset so that
1121 we cannot miss a breakpoint with its shadow range tail still reaching
1122 MEMADDR. */
c5aa993b 1123
35df4500
TJB
1124 if ((bl->address + bp_location_shadow_len_after_address_max
1125 >= bl->address)
1126 && (bl->address + bp_location_shadow_len_after_address_max
1127 <= memaddr))
876fa593
JK
1128 bc_l = bc;
1129 else
1130 bc_r = bc;
1131 }
1132
128070bb
PA
1133 /* Due to the binary search above, we need to make sure we pick the
1134 first location that's at BC_L's address. E.g., if there are
1135 multiple locations at the same address, BC_L may end up pointing
1136 at a duplicate location, and miss the "master"/"inserted"
1137 location. Say, given locations L1, L2 and L3 at addresses A and
1138 B:
1139
1140 L1@A, L2@A, L3@B, ...
1141
1142 BC_L could end up pointing at location L2, while the "master"
1143 location could be L1. Since the `loc->inserted' flag is only set
1144 on "master" locations, we'd forget to restore the shadow of L1
1145 and L2. */
1146 while (bc_l > 0
1147 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1148 bc_l--;
1149
876fa593
JK
1150 /* Now do full processing of the found relevant range of elements. */
1151
1152 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1153 {
35df4500 1154 struct bp_location *bl = bp_location[bc];
876fa593
JK
1155 CORE_ADDR bp_addr = 0;
1156 int bp_size = 0;
1157 int bptoffset = 0;
1158
35df4500
TJB
1159 /* bp_location array has BL->OWNER always non-NULL. */
1160 if (bl->owner->type == bp_none)
8a3fe4f8 1161 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1162 bl->owner->number);
ffce0d52 1163
876fa593
JK
1164 /* Performance optimization: any futher element can no longer affect BUF
1165 content. */
1166
35df4500
TJB
1167 if (bl->address >= bp_location_placed_address_before_address_max
1168 && memaddr + len <= (bl->address
1169 - bp_location_placed_address_before_address_max))
876fa593
JK
1170 break;
1171
35df4500 1172 if (!bp_location_has_shadow (bl))
c5aa993b 1173 continue;
35df4500 1174 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1175 current_program_space->aspace, 0))
1176 continue;
1177
c5aa993b
JM
1178 /* Addresses and length of the part of the breakpoint that
1179 we need to copy. */
35df4500
TJB
1180 bp_addr = bl->target_info.placed_address;
1181 bp_size = bl->target_info.shadow_len;
8defab1a 1182
c5aa993b
JM
1183 if (bp_addr + bp_size <= memaddr)
1184 /* The breakpoint is entirely before the chunk of memory we
1185 are reading. */
1186 continue;
8defab1a 1187
c5aa993b
JM
1188 if (bp_addr >= memaddr + len)
1189 /* The breakpoint is entirely after the chunk of memory we are
1190 reading. */
1191 continue;
c5aa993b 1192
8defab1a
DJ
1193 /* Offset within shadow_contents. */
1194 if (bp_addr < memaddr)
1195 {
1196 /* Only copy the second part of the breakpoint. */
1197 bp_size -= memaddr - bp_addr;
1198 bptoffset = memaddr - bp_addr;
1199 bp_addr = memaddr;
1200 }
c5aa993b 1201
8defab1a
DJ
1202 if (bp_addr + bp_size > memaddr + len)
1203 {
1204 /* Only copy the first part of the breakpoint. */
1205 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1206 }
c5aa993b 1207
8defab1a 1208 memcpy (buf + bp_addr - memaddr,
35df4500 1209 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1210 }
c906108c 1211}
c906108c 1212\f
c5aa993b 1213
687595f9 1214/* A wrapper function for inserting catchpoints. */
9cbc821d 1215static void
687595f9
DJ
1216insert_catchpoint (struct ui_out *uo, void *args)
1217{
1218 struct breakpoint *b = (struct breakpoint *) args;
687595f9 1219
fe798b75
JB
1220 gdb_assert (b->type == bp_catchpoint);
1221 gdb_assert (b->ops != NULL && b->ops->insert != NULL);
1222
1223 b->ops->insert (b);
687595f9
DJ
1224}
1225
60e1c644
PA
1226/* Return true if BPT is of any hardware watchpoint kind. */
1227
a5606eee 1228static int
d77f58be 1229is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1230{
1231 return (bpt->type == bp_hardware_watchpoint
1232 || bpt->type == bp_read_watchpoint
1233 || bpt->type == bp_access_watchpoint);
1234}
7270d8f2 1235
60e1c644
PA
1236/* Return true if BPT is of any watchpoint kind, hardware or
1237 software. */
1238
1239static int
d77f58be 1240is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1241{
1242 return (is_hardware_watchpoint (bpt)
1243 || bpt->type == bp_watchpoint);
1244}
1245
f6bc2008
PA
1246/* Assuming that B is a watchpoint: returns true if the current thread
1247 and its running state are safe to evaluate or update watchpoint B.
1248 Watchpoints on local expressions need to be evaluated in the
1249 context of the thread that was current when the watchpoint was
1250 created, and, that thread needs to be stopped to be able to select
1251 the correct frame context. Watchpoints on global expressions can
1252 be evaluated on any thread, and in any state. It is presently left
1253 to the target allowing memory accesses when threads are
1254 running. */
1255
1256static int
1257watchpoint_in_thread_scope (struct breakpoint *b)
1258{
1259 return (ptid_equal (b->watchpoint_thread, null_ptid)
1260 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1261 && !is_executing (inferior_ptid)));
1262}
1263
567e1b4e
JB
1264/* Assuming that B is a watchpoint:
1265 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1266 - Evaluate expression and store the result in B->val
567e1b4e
JB
1267 - Evaluate the condition if there is one, and store the result
1268 in b->loc->cond.
a5606eee
VP
1269 - Update the list of values that must be watched in B->loc.
1270
bfa149ac 1271 If the watchpoint disposition is disp_del_at_next_stop, then do nothing.
a79d3c27
JK
1272 If this is local watchpoint that is out of scope, delete it.
1273
1274 Even with `set breakpoint always-inserted on' the watchpoints are removed
1275 + inserted on each stop here. Normal breakpoints must never be removed
1276 because they might be missed by a running thread when debugging in non-stop
1277 mode. On the other hand, hardware watchpoints (is_hardware_watchpoint;
1278 processed here) are specific to each LWP since they are stored in each LWP's
1279 hardware debug registers. Therefore, such LWP must be stopped first in
1280 order to be able to modify its hardware watchpoints.
1281
1282 Hardware watchpoints must be reset exactly once after being presented to the
1283 user. It cannot be done sooner, because it would reset the data used to
1284 present the watchpoint hit to the user. And it must not be done later
1285 because it could display the same single watchpoint hit during multiple GDB
1286 stops. Note that the latter is relevant only to the hardware watchpoint
1287 types bp_read_watchpoint and bp_access_watchpoint. False hit by
1288 bp_hardware_watchpoint is not user-visible - its hit is suppressed if the
1289 memory content has not changed.
1290
1291 The following constraints influence the location where we can reset hardware
1292 watchpoints:
1293
1294 * target_stopped_by_watchpoint and target_stopped_data_address are called
1295 several times when GDB stops.
1296
1297 [linux]
1298 * Multiple hardware watchpoints can be hit at the same time, causing GDB to
1299 stop. GDB only presents one hardware watchpoint hit at a time as the
1300 reason for stopping, and all the other hits are presented later, one after
1301 the other, each time the user requests the execution to be resumed.
1302 Execution is not resumed for the threads still having pending hit event
1303 stored in LWP_INFO->STATUS. While the watchpoint is already removed from
1304 the inferior on the first stop the thread hit event is kept being reported
1305 from its cached value by linux_nat_stopped_data_address until the real
1306 thread resume happens after the watchpoint gets presented and thus its
1307 LWP_INFO->STATUS gets reset.
1308
1309 Therefore the hardware watchpoint hit can get safely reset on the watchpoint
1310 removal from inferior. */
1311
b40ce68a 1312static void
a5606eee 1313update_watchpoint (struct breakpoint *b, int reparse)
7270d8f2 1314{
a5606eee 1315 int within_current_scope;
a5606eee 1316 struct frame_id saved_frame_id;
66076460 1317 int frame_saved;
a5606eee 1318
f6bc2008
PA
1319 /* If this is a local watchpoint, we only want to check if the
1320 watchpoint frame is in scope if the current thread is the thread
1321 that was used to create the watchpoint. */
1322 if (!watchpoint_in_thread_scope (b))
1323 return;
1324
876fa593 1325 /* We don't free locations. They are stored in bp_location array and
567e1b4e
JB
1326 update_global_locations will eventually delete them and remove
1327 breakpoints if needed. */
a5606eee
VP
1328 b->loc = NULL;
1329
1330 if (b->disposition == disp_del_at_next_stop)
1331 return;
1332
66076460 1333 frame_saved = 0;
a5606eee
VP
1334
1335 /* Determine if the watchpoint is within scope. */
1336 if (b->exp_valid_block == NULL)
1337 within_current_scope = 1;
1338 else
1339 {
1340 struct frame_info *fi;
66076460
DJ
1341
1342 /* Save the current frame's ID so we can restore it after
1343 evaluating the watchpoint expression on its own frame. */
1344 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1345 took a frame parameter, so that we didn't have to change the
1346 selected frame. */
1347 frame_saved = 1;
1348 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1349
a5606eee
VP
1350 fi = frame_find_by_id (b->watchpoint_frame);
1351 within_current_scope = (fi != NULL);
1352 if (within_current_scope)
1353 select_frame (fi);
1354 }
1355
1356 if (within_current_scope && reparse)
1357 {
1358 char *s;
d63d0675 1359
a5606eee
VP
1360 if (b->exp)
1361 {
1362 xfree (b->exp);
1363 b->exp = NULL;
1364 }
d63d0675 1365 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1366 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1367 /* If the meaning of expression itself changed, the old value is
1368 no longer relevant. We don't want to report a watchpoint hit
1369 to the user when the old value and the new value may actually
1370 be completely different objects. */
1371 value_free (b->val);
fa4727a6
DJ
1372 b->val = NULL;
1373 b->val_valid = 0;
60e1c644
PA
1374
1375 /* Note that unlike with breakpoints, the watchpoint's condition
1376 expression is stored in the breakpoint object, not in the
1377 locations (re)created below. */
1378 if (b->cond_string != NULL)
1379 {
1380 if (b->cond_exp != NULL)
1381 {
1382 xfree (b->cond_exp);
1383 b->cond_exp = NULL;
1384 }
1385
1386 s = b->cond_string;
1387 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1388 }
a5606eee 1389 }
a5606eee
VP
1390
1391 /* If we failed to parse the expression, for example because
1392 it refers to a global variable in a not-yet-loaded shared library,
1393 don't try to insert watchpoint. We don't automatically delete
1394 such watchpoint, though, since failure to parse expression
1395 is different from out-of-scope watchpoint. */
2d134ed3
PA
1396 if ( !target_has_execution)
1397 {
1398 /* Without execution, memory can't change. No use to try and
1399 set watchpoint locations. The watchpoint will be reset when
1400 the target gains execution, through breakpoint_re_set. */
1401 }
1402 else if (within_current_scope && b->exp)
a5606eee 1403 {
0cf6dd15 1404 int pc = 0;
fa4727a6 1405 struct value *val_chain, *v, *result, *next;
2d134ed3 1406 struct program_space *frame_pspace;
a5606eee 1407
0cf6dd15 1408 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1409
a5606eee
VP
1410 /* Avoid setting b->val if it's already set. The meaning of
1411 b->val is 'the last value' user saw, and we should update
1412 it only if we reported that last value to user. As it
1413 happens, the code that reports it updates b->val directly. */
fa4727a6
DJ
1414 if (!b->val_valid)
1415 {
1416 b->val = v;
1417 b->val_valid = 1;
1418 }
a5606eee 1419
db2ad4c3
JK
1420 /* Change the type of breakpoint between hardware assisted or an
1421 ordinary watchpoint depending on the hardware support and free
1422 hardware slots. REPARSE is set when the inferior is started. */
1423 if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
1424 && reparse)
1425 {
ca2d49e8 1426 int i, mem_cnt, other_type_used;
db2ad4c3 1427
7b838ca2
TJB
1428 /* We need to determine how many resources are already used
1429 for all other hardware watchpoints to see if we still have
1430 enough resources to also fit this watchpoint in as well.
1431 To avoid the hw_watchpoint_used_count call below from counting
1432 this watchpoint, make sure that it is marked as a software
1433 watchpoint. */
1434 b->type = bp_watchpoint;
db2ad4c3
JK
1435 i = hw_watchpoint_used_count (bp_hardware_watchpoint,
1436 &other_type_used);
1437 mem_cnt = can_use_hardware_watchpoint (val_chain);
1438
ca2d49e8 1439 if (!mem_cnt)
db2ad4c3
JK
1440 b->type = bp_watchpoint;
1441 else
ca2d49e8 1442 {
d92524f1 1443 int target_resources_ok = target_can_use_hardware_watchpoint
ca2d49e8
SL
1444 (bp_hardware_watchpoint, i + mem_cnt, other_type_used);
1445 if (target_resources_ok <= 0)
1446 b->type = bp_watchpoint;
1447 else
1448 b->type = bp_hardware_watchpoint;
1449 }
db2ad4c3
JK
1450 }
1451
2d134ed3
PA
1452 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1453
a5606eee 1454 /* Look at each value on the value chain. */
fa4727a6 1455 for (v = val_chain; v; v = next)
a5606eee
VP
1456 {
1457 /* If it's a memory location, and GDB actually needed
1458 its contents to evaluate the expression, then we
fa4727a6
DJ
1459 must watch it. If the first value returned is
1460 still lazy, that means an error occurred reading it;
1461 watch it anyway in case it becomes readable. */
a5606eee 1462 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1463 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1464 {
1465 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1466
a5606eee
VP
1467 /* We only watch structs and arrays if user asked
1468 for it explicitly, never if they just happen to
1469 appear in the middle of some value chain. */
fa4727a6 1470 if (v == result
a5606eee
VP
1471 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1472 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1473 {
1474 CORE_ADDR addr;
1475 int len, type;
1476 struct bp_location *loc, **tmp;
1477
42ae5230 1478 addr = value_address (v);
a5606eee
VP
1479 len = TYPE_LENGTH (value_type (v));
1480 type = hw_write;
1481 if (b->type == bp_read_watchpoint)
1482 type = hw_read;
1483 else if (b->type == bp_access_watchpoint)
1484 type = hw_access;
1485
39d61571 1486 loc = allocate_bp_location (b);
a5606eee
VP
1487 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1488 ;
1489 *tmp = loc;
a6d9a66e 1490 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1491
1492 loc->pspace = frame_pspace;
a5606eee
VP
1493 loc->address = addr;
1494 loc->length = len;
1495 loc->watchpoint_type = type;
1496 }
1497 }
1498
1499 next = value_next (v);
1500 if (v != b->val)
1501 value_free (v);
1502 }
1503
c7437ca6
PA
1504 /* If a software watchpoint is not watching any memory, then the
1505 above left it without any location set up. But,
1506 bpstat_stop_status requires a location to be able to report
1507 stops, so make sure there's at least a dummy one. */
1508 if (b->type == bp_watchpoint && b->loc == NULL)
1509 {
1510 b->loc = allocate_bp_location (b);
1511 b->loc->pspace = frame_pspace;
1512 b->loc->address = -1;
1513 b->loc->length = -1;
1514 b->loc->watchpoint_type = -1;
1515 }
a5606eee
VP
1516 }
1517 else if (!within_current_scope)
7270d8f2 1518 {
a5606eee 1519 printf_filtered (_("\
cce7e648 1520Watchpoint %d deleted because the program has left the block\n\
a5606eee
VP
1521in which its expression is valid.\n"),
1522 b->number);
1523 if (b->related_breakpoint)
60e1c644
PA
1524 {
1525 b->related_breakpoint->disposition = disp_del_at_next_stop;
1526 b->related_breakpoint->related_breakpoint = NULL;
1527 b->related_breakpoint= NULL;
1528 }
a5606eee 1529 b->disposition = disp_del_at_next_stop;
7270d8f2 1530 }
a5606eee
VP
1531
1532 /* Restore the selected frame. */
66076460
DJ
1533 if (frame_saved)
1534 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1535}
1536
a5606eee 1537
74960c60
VP
1538/* Returns 1 iff breakpoint location should be
1539 inserted in the inferior. */
1540static int
35df4500 1541should_be_inserted (struct bp_location *bl)
74960c60 1542{
35df4500 1543 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1544 return 0;
1545
35df4500 1546 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1547 return 0;
1548
35df4500 1549 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1550 return 0;
1551
56710373
PA
1552 /* This is set for example, when we're attached to the parent of a
1553 vfork, and have detached from the child. The child is running
1554 free, and we expect it to do an exec or exit, at which point the
1555 OS makes the parent schedulable again (and the target reports
1556 that the vfork is done). Until the child is done with the shared
1557 memory region, do not insert breakpoints in the parent, otherwise
1558 the child could still trip on the parent's breakpoints. Since
1559 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1560 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1561 return 0;
1562
1042e4c0
SS
1563 /* Tracepoints are inserted by the target at a time of its choosing,
1564 not by us. */
35df4500 1565 if (is_tracepoint (bl->owner))
1042e4c0
SS
1566 return 0;
1567
74960c60
VP
1568 return 1;
1569}
1570
35df4500
TJB
1571/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1572 location. Any error messages are printed to TMP_ERROR_STREAM; and
1573 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
879bfdc2
DJ
1574
1575 NOTE drow/2003-09-09: This routine could be broken down to an object-style
1576 method for each breakpoint or catchpoint type. */
26bb91f3 1577static int
35df4500 1578insert_bp_location (struct bp_location *bl,
26bb91f3 1579 struct ui_file *tmp_error_stream,
fa3a767f 1580 int *disabled_breaks,
26bb91f3 1581 int *hw_breakpoint_error)
879bfdc2
DJ
1582{
1583 int val = 0;
1584
35df4500 1585 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1586 return 0;
1587
8181d85f 1588 /* Initialize the target-specific information. */
35df4500
TJB
1589 memset (&bl->target_info, 0, sizeof (bl->target_info));
1590 bl->target_info.placed_address = bl->address;
1591 bl->target_info.placed_address_space = bl->pspace->aspace;
8181d85f 1592
35df4500
TJB
1593 if (bl->loc_type == bp_loc_software_breakpoint
1594 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1595 {
35df4500 1596 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1597 {
1598 /* If the explicitly specified breakpoint type
1599 is not hardware breakpoint, check the memory map to see
1600 if the breakpoint address is in read only memory or not.
1601 Two important cases are:
1602 - location type is not hardware breakpoint, memory
1603 is readonly. We change the type of the location to
1604 hardware breakpoint.
1605 - location type is hardware breakpoint, memory is read-write.
1606 This means we've previously made the location hardware one, but
1607 then the memory map changed, so we undo.
1608
1609 When breakpoints are removed, remove_breakpoints will
1610 use location types we've just set here, the only possible
1611 problem is that memory map has changed during running program,
1612 but it's not going to work anyway with current gdb. */
1613 struct mem_region *mr
35df4500 1614 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1615
1616 if (mr)
1617 {
1618 if (automatic_hardware_breakpoints)
1619 {
765dc015
VP
1620 enum bp_loc_type new_type;
1621
1622 if (mr->attrib.mode != MEM_RW)
1623 new_type = bp_loc_hardware_breakpoint;
1624 else
1625 new_type = bp_loc_software_breakpoint;
1626
35df4500 1627 if (new_type != bl->loc_type)
765dc015
VP
1628 {
1629 static int said = 0;
cc59ec59 1630
35df4500 1631 bl->loc_type = new_type;
765dc015
VP
1632 if (!said)
1633 {
1634 fprintf_filtered (gdb_stdout, _("\
0767c96d 1635Note: automatically using hardware breakpoints for read-only addresses.\n"));
765dc015
VP
1636 said = 1;
1637 }
1638 }
1639 }
35df4500 1640 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015
VP
1641 && mr->attrib.mode != MEM_RW)
1642 warning (_("cannot set software breakpoint at readonly address %s"),
35df4500 1643 paddress (bl->gdbarch, bl->address));
765dc015
VP
1644 }
1645 }
1646
879bfdc2
DJ
1647 /* First check to see if we have to handle an overlay. */
1648 if (overlay_debugging == ovly_off
35df4500
TJB
1649 || bl->section == NULL
1650 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1651 {
1652 /* No overlay handling: just set the breakpoint. */
1653
35df4500
TJB
1654 if (bl->loc_type == bp_loc_hardware_breakpoint)
1655 val = target_insert_hw_breakpoint (bl->gdbarch,
1656 &bl->target_info);
879bfdc2 1657 else
35df4500
TJB
1658 val = target_insert_breakpoint (bl->gdbarch,
1659 &bl->target_info);
879bfdc2
DJ
1660 }
1661 else
1662 {
1663 /* This breakpoint is in an overlay section.
1664 Shall we set a breakpoint at the LMA? */
1665 if (!overlay_events_enabled)
1666 {
1667 /* Yes -- overlay event support is not active,
1668 so we must try to set a breakpoint at the LMA.
1669 This will not work for a hardware breakpoint. */
35df4500 1670 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1671 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1672 bl->owner->number);
879bfdc2
DJ
1673 else
1674 {
35df4500
TJB
1675 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1676 bl->section);
879bfdc2 1677 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1678 bl->overlay_target_info = bl->target_info;
1679 bl->overlay_target_info.placed_address = addr;
1680 val = target_insert_breakpoint (bl->gdbarch,
1681 &bl->overlay_target_info);
879bfdc2 1682 if (val != 0)
99361f52
DE
1683 fprintf_unfiltered (tmp_error_stream,
1684 "Overlay breakpoint %d failed: in ROM?\n",
35df4500 1685 bl->owner->number);
879bfdc2
DJ
1686 }
1687 }
1688 /* Shall we set a breakpoint at the VMA? */
35df4500 1689 if (section_is_mapped (bl->section))
879bfdc2
DJ
1690 {
1691 /* Yes. This overlay section is mapped into memory. */
35df4500
TJB
1692 if (bl->loc_type == bp_loc_hardware_breakpoint)
1693 val = target_insert_hw_breakpoint (bl->gdbarch,
1694 &bl->target_info);
879bfdc2 1695 else
35df4500
TJB
1696 val = target_insert_breakpoint (bl->gdbarch,
1697 &bl->target_info);
879bfdc2
DJ
1698 }
1699 else
1700 {
1701 /* No. This breakpoint will not be inserted.
1702 No error, but do not mark the bp as 'inserted'. */
1703 return 0;
1704 }
1705 }
1706
1707 if (val)
1708 {
1709 /* Can't set the breakpoint. */
35df4500 1710 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2
DJ
1711 {
1712 /* See also: disable_breakpoints_in_shlibs. */
1713 val = 0;
35df4500 1714 bl->shlib_disabled = 1;
879bfdc2
DJ
1715 if (!*disabled_breaks)
1716 {
1717 fprintf_unfiltered (tmp_error_stream,
1718 "Cannot insert breakpoint %d.\n",
35df4500 1719 bl->owner->number);
879bfdc2
DJ
1720 fprintf_unfiltered (tmp_error_stream,
1721 "Temporarily disabling shared library breakpoints:\n");
1722 }
1723 *disabled_breaks = 1;
1724 fprintf_unfiltered (tmp_error_stream,
35df4500 1725 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1726 }
1727 else
879bfdc2 1728 {
35df4500 1729 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1730 {
1731 *hw_breakpoint_error = 1;
1732 fprintf_unfiltered (tmp_error_stream,
1733 "Cannot insert hardware breakpoint %d.\n",
35df4500 1734 bl->owner->number);
879bfdc2
DJ
1735 }
1736 else
1737 {
1738 fprintf_unfiltered (tmp_error_stream,
1739 "Cannot insert breakpoint %d.\n",
35df4500 1740 bl->owner->number);
879bfdc2
DJ
1741 fprintf_filtered (tmp_error_stream,
1742 "Error accessing memory address ");
35df4500 1743 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1744 tmp_error_stream);
879bfdc2
DJ
1745 fprintf_filtered (tmp_error_stream, ": %s.\n",
1746 safe_strerror (val));
1747 }
1748
1749 }
1750 }
1751 else
35df4500 1752 bl->inserted = 1;
879bfdc2
DJ
1753
1754 return val;
1755 }
1756
35df4500 1757 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2
DJ
1758 /* NOTE drow/2003-09-08: This state only exists for removing
1759 watchpoints. It's not clear that it's necessary... */
35df4500 1760 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1761 {
35df4500
TJB
1762 val = target_insert_watchpoint (bl->address,
1763 bl->length,
1764 bl->watchpoint_type,
1765 bl->owner->cond_exp);
85d721b8
PA
1766
1767 /* If trying to set a read-watchpoint, and it turns out it's not
1768 supported, try emulating one with an access watchpoint. */
35df4500 1769 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1770 {
1771 struct bp_location *loc, **loc_temp;
1772
1773 /* But don't try to insert it, if there's already another
1774 hw_access location that would be considered a duplicate
1775 of this one. */
1776 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1777 if (loc != bl
85d721b8 1778 && loc->watchpoint_type == hw_access
35df4500 1779 && watchpoint_locations_match (bl, loc))
85d721b8 1780 {
35df4500
TJB
1781 bl->duplicate = 1;
1782 bl->inserted = 1;
1783 bl->target_info = loc->target_info;
1784 bl->watchpoint_type = hw_access;
85d721b8
PA
1785 val = 0;
1786 break;
1787 }
1788
1789 if (val == 1)
1790 {
35df4500
TJB
1791 val = target_insert_watchpoint (bl->address,
1792 bl->length,
0cf6dd15 1793 hw_access,
35df4500 1794 bl->owner->cond_exp);
85d721b8 1795 if (val == 0)
35df4500 1796 bl->watchpoint_type = hw_access;
85d721b8
PA
1797 }
1798 }
1799
35df4500 1800 bl->inserted = (val == 0);
879bfdc2
DJ
1801 }
1802
35df4500 1803 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1804 {
71fff37b 1805 struct gdb_exception e = catch_exception (uiout, insert_catchpoint,
35df4500 1806 bl->owner, RETURN_MASK_ERROR);
9cbc821d 1807 exception_fprintf (gdb_stderr, e, "warning: inserting catchpoint %d: ",
35df4500 1808 bl->owner->number);
9cbc821d 1809 if (e.reason < 0)
35df4500 1810 bl->owner->enable_state = bp_disabled;
879bfdc2 1811 else
35df4500 1812 bl->inserted = 1;
1640b821
DJ
1813
1814 /* We've already printed an error message if there was a problem
1815 inserting this catchpoint, and we've disabled the catchpoint,
1816 so just return success. */
1817 return 0;
879bfdc2
DJ
1818 }
1819
1820 return 0;
1821}
1822
6c95b8df
PA
1823/* This function is called when program space PSPACE is about to be
1824 deleted. It takes care of updating breakpoints to not reference
1825 PSPACE anymore. */
1826
1827void
1828breakpoint_program_space_exit (struct program_space *pspace)
1829{
1830 struct breakpoint *b, *b_temp;
876fa593 1831 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1832
1833 /* Remove any breakpoint that was set through this program space. */
1834 ALL_BREAKPOINTS_SAFE (b, b_temp)
1835 {
1836 if (b->pspace == pspace)
1837 delete_breakpoint (b);
1838 }
1839
1840 /* Breakpoints set through other program spaces could have locations
1841 bound to PSPACE as well. Remove those. */
876fa593 1842 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1843 {
1844 struct bp_location *tmp;
1845
1846 if (loc->pspace == pspace)
1847 {
2bdf28a0 1848 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1849 if (loc->owner->loc == loc)
1850 loc->owner->loc = loc->next;
1851 else
1852 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1853 if (tmp->next == loc)
1854 {
1855 tmp->next = loc->next;
1856 break;
1857 }
1858 }
1859 }
1860
1861 /* Now update the global location list to permanently delete the
1862 removed locations above. */
1863 update_global_location_list (0);
1864}
1865
74960c60
VP
1866/* Make sure all breakpoints are inserted in inferior.
1867 Throws exception on any error.
1868 A breakpoint that is already inserted won't be inserted
1869 again, so calling this function twice is safe. */
1870void
1871insert_breakpoints (void)
1872{
1873 struct breakpoint *bpt;
1874
1875 ALL_BREAKPOINTS (bpt)
1876 if (is_hardware_watchpoint (bpt))
1877 update_watchpoint (bpt, 0 /* don't reparse. */);
1878
b60e7edf 1879 update_global_location_list (1);
74960c60 1880
c35b1492
PA
1881 /* update_global_location_list does not insert breakpoints when
1882 always_inserted_mode is not enabled. Explicitly insert them
1883 now. */
1884 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1885 insert_breakpoint_locations ();
1886}
1887
c906108c
SS
1888/* insert_breakpoints is used when starting or continuing the program.
1889 remove_breakpoints is used when the program stops.
1890 Both return zero if successful,
1891 or an `errno' value if could not write the inferior. */
1892
74960c60
VP
1893static void
1894insert_breakpoint_locations (void)
c906108c 1895{
a5606eee 1896 struct breakpoint *bpt;
35df4500 1897 struct bp_location *bl, **blp_tmp;
e236ba44 1898 int error = 0;
c906108c
SS
1899 int val = 0;
1900 int disabled_breaks = 0;
81d0cc19 1901 int hw_breakpoint_error = 0;
c906108c 1902
81d0cc19 1903 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1904 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1905
81d0cc19
GS
1906 /* Explicitly mark the warning -- this will only be printed if
1907 there was an error. */
1908 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1909
1910 save_current_space_and_thread ();
1911
35df4500 1912 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1913 {
35df4500 1914 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1915 continue;
1916
f365de73 1917 /* There is no point inserting thread-specific breakpoints if the
35df4500 1918 thread no longer exists. ALL_BP_LOCATIONS bp_location has BL->OWNER
2bdf28a0 1919 always non-NULL. */
35df4500
TJB
1920 if (bl->owner->thread != -1
1921 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1922 continue;
1923
35df4500 1924 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1925
1926 /* For targets that support global breakpoints, there's no need
1927 to select an inferior to insert breakpoint to. In fact, even
1928 if we aren't attached to any process yet, we should still
1929 insert breakpoints. */
1930 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1931 && ptid_equal (inferior_ptid, null_ptid))
1932 continue;
1933
35df4500 1934 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
879bfdc2
DJ
1935 &hw_breakpoint_error);
1936 if (val)
e236ba44 1937 error = val;
879bfdc2 1938 }
c906108c 1939
a5606eee
VP
1940 /* If we failed to insert all locations of a watchpoint,
1941 remove them, as half-inserted watchpoint is of limited use. */
1942 ALL_BREAKPOINTS (bpt)
1943 {
1944 int some_failed = 0;
1945 struct bp_location *loc;
1946
1947 if (!is_hardware_watchpoint (bpt))
1948 continue;
1949
d6b74ac4 1950 if (!breakpoint_enabled (bpt))
a5606eee 1951 continue;
74960c60
VP
1952
1953 if (bpt->disposition == disp_del_at_next_stop)
1954 continue;
a5606eee
VP
1955
1956 for (loc = bpt->loc; loc; loc = loc->next)
56710373 1957 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
1958 {
1959 some_failed = 1;
1960 break;
1961 }
1962 if (some_failed)
1963 {
1964 for (loc = bpt->loc; loc; loc = loc->next)
1965 if (loc->inserted)
1966 remove_breakpoint (loc, mark_uninserted);
1967
1968 hw_breakpoint_error = 1;
1969 fprintf_unfiltered (tmp_error_stream,
1970 "Could not insert hardware watchpoint %d.\n",
1971 bpt->number);
1972 error = -1;
1973 }
1974 }
1975
e236ba44 1976 if (error)
81d0cc19
GS
1977 {
1978 /* If a hardware breakpoint or watchpoint was inserted, add a
1979 message about possibly exhausted resources. */
879bfdc2 1980 if (hw_breakpoint_error)
81d0cc19 1981 {
c6510018
MS
1982 fprintf_unfiltered (tmp_error_stream,
1983 "Could not insert hardware breakpoints:\n\
1984You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 1985 }
81d0cc19
GS
1986 target_terminal_ours_for_output ();
1987 error_stream (tmp_error_stream);
1988 }
f7545552
TT
1989
1990 do_cleanups (cleanups);
c906108c
SS
1991}
1992
c906108c 1993int
fba45db2 1994remove_breakpoints (void)
c906108c 1995{
35df4500 1996 struct bp_location *bl, **blp_tmp;
3a1bae8e 1997 int val = 0;
c906108c 1998
35df4500 1999 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2000 {
35df4500
TJB
2001 if (bl->inserted)
2002 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2003 }
3a1bae8e 2004 return val;
c906108c
SS
2005}
2006
6c95b8df
PA
2007/* Remove breakpoints of process PID. */
2008
2009int
2010remove_breakpoints_pid (int pid)
2011{
35df4500 2012 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2013 int val;
2014 struct inferior *inf = find_inferior_pid (pid);
2015
35df4500 2016 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2017 {
35df4500 2018 if (bl->pspace != inf->pspace)
6c95b8df
PA
2019 continue;
2020
35df4500 2021 if (bl->inserted)
6c95b8df 2022 {
35df4500 2023 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2024 if (val != 0)
2025 return val;
2026 }
2027 }
2028 return 0;
2029}
2030
692590c1 2031int
80ce1ecb 2032remove_hw_watchpoints (void)
692590c1 2033{
35df4500 2034 struct bp_location *bl, **blp_tmp;
3a1bae8e 2035 int val = 0;
692590c1 2036
35df4500 2037 ALL_BP_LOCATIONS (bl, blp_tmp)
692590c1 2038 {
35df4500
TJB
2039 if (bl->inserted && bl->loc_type == bp_loc_hardware_watchpoint)
2040 val |= remove_breakpoint (bl, mark_uninserted);
692590c1 2041 }
3a1bae8e 2042 return val;
692590c1
MS
2043}
2044
c906108c 2045int
fba45db2 2046reattach_breakpoints (int pid)
c906108c 2047{
6c95b8df 2048 struct cleanup *old_chain;
35df4500 2049 struct bp_location *bl, **blp_tmp;
c906108c 2050 int val;
a4954f26 2051 struct ui_file *tmp_error_stream = mem_fileopen ();
fa3a767f 2052 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2053 struct inferior *inf;
2054 struct thread_info *tp;
2055
2056 tp = any_live_thread_of_process (pid);
2057 if (tp == NULL)
2058 return 1;
2059
2060 inf = find_inferior_pid (pid);
2061 old_chain = save_inferior_ptid ();
2062
2063 inferior_ptid = tp->ptid;
a4954f26
DJ
2064
2065 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2066
35df4500 2067 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2068 {
35df4500 2069 if (bl->pspace != inf->pspace)
6c95b8df
PA
2070 continue;
2071
35df4500 2072 if (bl->inserted)
c5aa993b 2073 {
35df4500
TJB
2074 bl->inserted = 0;
2075 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2076 if (val != 0)
2077 {
ce696e05 2078 do_cleanups (old_chain);
c5aa993b
JM
2079 return val;
2080 }
2081 }
2082 }
ce696e05 2083 do_cleanups (old_chain);
c906108c
SS
2084 return 0;
2085}
2086
e58b0e63
PA
2087static int internal_breakpoint_number = -1;
2088
84f4c1fe
PM
2089/* Set the breakpoint number of B, depending on the value of INTERNAL.
2090 If INTERNAL is non-zero, the breakpoint number will be populated
2091 from internal_breakpoint_number and that variable decremented.
2092 Otherwis the breakpoint number will be populated from
2093 breakpoint_count and that value incremented. Internal breakpoints
2094 do not set the internal var bpnum. */
2095static void
2096set_breakpoint_number (int internal, struct breakpoint *b)
2097{
2098 if (internal)
2099 b->number = internal_breakpoint_number--;
2100 else
2101 {
2102 set_breakpoint_count (breakpoint_count + 1);
2103 b->number = breakpoint_count;
2104 }
2105}
2106
e62c965a 2107static struct breakpoint *
a6d9a66e
UW
2108create_internal_breakpoint (struct gdbarch *gdbarch,
2109 CORE_ADDR address, enum bptype type)
e62c965a 2110{
e62c965a
PP
2111 struct symtab_and_line sal;
2112 struct breakpoint *b;
2113
2114 init_sal (&sal); /* initialize to zeroes */
2115
2116 sal.pc = address;
2117 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2118 sal.pspace = current_program_space;
e62c965a 2119
a6d9a66e 2120 b = set_raw_breakpoint (gdbarch, sal, type);
e62c965a
PP
2121 b->number = internal_breakpoint_number--;
2122 b->disposition = disp_donttouch;
2123
2124 return b;
2125}
2126
2127static void
69de3c6a 2128create_overlay_event_breakpoint (char *func_name)
e62c965a 2129{
69de3c6a 2130 struct objfile *objfile;
e62c965a 2131
69de3c6a
PP
2132 ALL_OBJFILES (objfile)
2133 {
2134 struct breakpoint *b;
2135 struct minimal_symbol *m;
2136
2137 m = lookup_minimal_symbol_text (func_name, objfile);
2138 if (m == NULL)
2139 continue;
e62c965a 2140
a6d9a66e
UW
2141 b = create_internal_breakpoint (get_objfile_arch (objfile),
2142 SYMBOL_VALUE_ADDRESS (m),
69de3c6a
PP
2143 bp_overlay_event);
2144 b->addr_string = xstrdup (func_name);
e62c965a 2145
69de3c6a
PP
2146 if (overlay_debugging == ovly_auto)
2147 {
2148 b->enable_state = bp_enabled;
2149 overlay_events_enabled = 1;
2150 }
2151 else
2152 {
2153 b->enable_state = bp_disabled;
2154 overlay_events_enabled = 0;
2155 }
e62c965a
PP
2156 }
2157 update_global_location_list (1);
2158}
2159
0fd8e87f
UW
2160static void
2161create_longjmp_master_breakpoint (char *func_name)
2162{
6c95b8df 2163 struct program_space *pspace;
0fd8e87f 2164 struct objfile *objfile;
6c95b8df
PA
2165 struct cleanup *old_chain;
2166
2167 old_chain = save_current_program_space ();
0fd8e87f 2168
6c95b8df 2169 ALL_PSPACES (pspace)
0fd8e87f
UW
2170 ALL_OBJFILES (objfile)
2171 {
2172 struct breakpoint *b;
2173 struct minimal_symbol *m;
2174
2175 if (!gdbarch_get_longjmp_target_p (get_objfile_arch (objfile)))
2176 continue;
2177
6c95b8df
PA
2178 set_current_program_space (pspace);
2179
0fd8e87f
UW
2180 m = lookup_minimal_symbol_text (func_name, objfile);
2181 if (m == NULL)
2182 continue;
2183
a6d9a66e
UW
2184 b = create_internal_breakpoint (get_objfile_arch (objfile),
2185 SYMBOL_VALUE_ADDRESS (m),
0fd8e87f
UW
2186 bp_longjmp_master);
2187 b->addr_string = xstrdup (func_name);
2188 b->enable_state = bp_disabled;
2189 }
2190 update_global_location_list (1);
6c95b8df
PA
2191
2192 do_cleanups (old_chain);
0fd8e87f
UW
2193}
2194
aa7d318d
TT
2195/* Create a master std::terminate breakpoint. The actual function
2196 looked for is named FUNC_NAME. */
2197static void
2198create_std_terminate_master_breakpoint (const char *func_name)
2199{
2200 struct program_space *pspace;
2201 struct objfile *objfile;
2202 struct cleanup *old_chain;
2203
2204 old_chain = save_current_program_space ();
2205
2206 ALL_PSPACES (pspace)
2207 ALL_OBJFILES (objfile)
2208 {
2209 struct breakpoint *b;
2210 struct minimal_symbol *m;
2211
2212 set_current_program_space (pspace);
2213
2214 m = lookup_minimal_symbol (func_name, NULL, objfile);
2215 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2216 && MSYMBOL_TYPE (m) != mst_file_text))
2217 continue;
2218
2219 b = create_internal_breakpoint (get_objfile_arch (objfile),
2220 SYMBOL_VALUE_ADDRESS (m),
2221 bp_std_terminate_master);
2222 b->addr_string = xstrdup (func_name);
2223 b->enable_state = bp_disabled;
2224 }
2225 update_global_location_list (1);
2226
2227 do_cleanups (old_chain);
2228}
2229
186c406b
TT
2230/* Install a master breakpoint on the unwinder's debug hook. */
2231
2232void
2233create_exception_master_breakpoint (void)
2234{
2235 struct objfile *objfile;
2236
2237 ALL_OBJFILES (objfile)
2238 {
2239 struct minimal_symbol *debug_hook;
2240
2241 debug_hook = lookup_minimal_symbol ("_Unwind_DebugHook", NULL, objfile);
2242 if (debug_hook != NULL)
2243 {
2244 struct breakpoint *b;
2245 CORE_ADDR addr = SYMBOL_VALUE_ADDRESS (debug_hook);
2246 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2247
2248 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2249 &current_target);
2250 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
2251 b->addr_string = xstrdup ("_Unwind_DebugHook");
2252 b->enable_state = bp_disabled;
2253 }
2254 }
2255
2256 update_global_location_list (1);
2257}
2258
c906108c 2259void
fba45db2 2260update_breakpoints_after_exec (void)
c906108c 2261{
35df4500 2262 struct breakpoint *b, *b_tmp;
876fa593 2263 struct bp_location *bploc, **bplocp_tmp;
c906108c 2264
25b22b0a
PA
2265 /* We're about to delete breakpoints from GDB's lists. If the
2266 INSERTED flag is true, GDB will try to lift the breakpoints by
2267 writing the breakpoints' "shadow contents" back into memory. The
2268 "shadow contents" are NOT valid after an exec, so GDB should not
2269 do that. Instead, the target is responsible from marking
2270 breakpoints out as soon as it detects an exec. We don't do that
2271 here instead, because there may be other attempts to delete
2272 breakpoints after detecting an exec and before reaching here. */
876fa593 2273 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2274 if (bploc->pspace == current_program_space)
2275 gdb_assert (!bploc->inserted);
c906108c 2276
35df4500 2277 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2278 {
6c95b8df
PA
2279 if (b->pspace != current_program_space)
2280 continue;
2281
c5aa993b
JM
2282 /* Solib breakpoints must be explicitly reset after an exec(). */
2283 if (b->type == bp_shlib_event)
2284 {
2285 delete_breakpoint (b);
2286 continue;
2287 }
c906108c 2288
4efc6507
DE
2289 /* JIT breakpoints must be explicitly reset after an exec(). */
2290 if (b->type == bp_jit_event)
2291 {
2292 delete_breakpoint (b);
2293 continue;
2294 }
2295
1900040c 2296 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2297 as must overlay event and longjmp master breakpoints. */
2298 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2299 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2300 || b->type == bp_exception_master)
c4093a6a
JM
2301 {
2302 delete_breakpoint (b);
2303 continue;
2304 }
2305
c5aa993b
JM
2306 /* Step-resume breakpoints are meaningless after an exec(). */
2307 if (b->type == bp_step_resume)
2308 {
2309 delete_breakpoint (b);
2310 continue;
2311 }
2312
611c83ae
PA
2313 /* Longjmp and longjmp-resume breakpoints are also meaningless
2314 after an exec. */
186c406b
TT
2315 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2316 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2317 {
2318 delete_breakpoint (b);
2319 continue;
2320 }
2321
ce78b96d
JB
2322 if (b->type == bp_catchpoint)
2323 {
2324 /* For now, none of the bp_catchpoint breakpoints need to
2325 do anything at this point. In the future, if some of
2326 the catchpoints need to something, we will need to add
2327 a new method, and call this method from here. */
2328 continue;
2329 }
2330
c5aa993b
JM
2331 /* bp_finish is a special case. The only way we ought to be able
2332 to see one of these when an exec() has happened, is if the user
2333 caught a vfork, and then said "finish". Ordinarily a finish just
2334 carries them to the call-site of the current callee, by setting
2335 a temporary bp there and resuming. But in this case, the finish
2336 will carry them entirely through the vfork & exec.
2337
2338 We don't want to allow a bp_finish to remain inserted now. But
2339 we can't safely delete it, 'cause finish_command has a handle to
2340 the bp on a bpstat, and will later want to delete it. There's a
2341 chance (and I've seen it happen) that if we delete the bp_finish
2342 here, that its storage will get reused by the time finish_command
2343 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2344 We really must allow finish_command to delete a bp_finish.
2345
53a5351d
JM
2346 In the absense of a general solution for the "how do we know
2347 it's safe to delete something others may have handles to?"
2348 problem, what we'll do here is just uninsert the bp_finish, and
2349 let finish_command delete it.
2350
2351 (We know the bp_finish is "doomed" in the sense that it's
2352 momentary, and will be deleted as soon as finish_command sees
2353 the inferior stopped. So it doesn't matter that the bp's
2354 address is probably bogus in the new a.out, unlike e.g., the
2355 solib breakpoints.) */
c5aa993b 2356
c5aa993b
JM
2357 if (b->type == bp_finish)
2358 {
2359 continue;
2360 }
2361
2362 /* Without a symbolic address, we have little hope of the
2363 pre-exec() address meaning the same thing in the post-exec()
2364 a.out. */
2365 if (b->addr_string == NULL)
2366 {
2367 delete_breakpoint (b);
2368 continue;
2369 }
c5aa993b 2370 }
1900040c 2371 /* FIXME what about longjmp breakpoints? Re-create them here? */
69de3c6a 2372 create_overlay_event_breakpoint ("_ovly_debug_event");
0fd8e87f
UW
2373 create_longjmp_master_breakpoint ("longjmp");
2374 create_longjmp_master_breakpoint ("_longjmp");
2375 create_longjmp_master_breakpoint ("siglongjmp");
2376 create_longjmp_master_breakpoint ("_siglongjmp");
aa7d318d 2377 create_std_terminate_master_breakpoint ("std::terminate()");
186c406b 2378 create_exception_master_breakpoint ();
c906108c
SS
2379}
2380
2381int
fba45db2 2382detach_breakpoints (int pid)
c906108c 2383{
35df4500 2384 struct bp_location *bl, **blp_tmp;
3a1bae8e 2385 int val = 0;
ce696e05 2386 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2387 struct inferior *inf = current_inferior ();
c5aa993b 2388
39f77062 2389 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2390 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2391
6c95b8df 2392 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2393 inferior_ptid = pid_to_ptid (pid);
35df4500 2394 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2395 {
35df4500 2396 if (bl->pspace != inf->pspace)
6c95b8df
PA
2397 continue;
2398
35df4500
TJB
2399 if (bl->inserted)
2400 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2401 }
d03285ec
UW
2402
2403 /* Detach single-step breakpoints as well. */
2404 detach_single_step_breakpoints ();
2405
ce696e05 2406 do_cleanups (old_chain);
3a1bae8e 2407 return val;
c906108c
SS
2408}
2409
35df4500 2410/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2411 Note that this is used to detach breakpoints from a child fork.
2412 When we get here, the child isn't in the inferior list, and neither
2413 do we have objects to represent its address space --- we should
35df4500 2414 *not* look at bl->pspace->aspace here. */
6c95b8df 2415
c906108c 2416static int
35df4500 2417remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2418{
2419 int val;
c5aa993b 2420
35df4500
TJB
2421 /* BL is never in moribund_locations by our callers. */
2422 gdb_assert (bl->owner != NULL);
2bdf28a0 2423
35df4500 2424 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2425 /* Permanent breakpoints cannot be inserted or removed. */
2426 return 0;
2427
74960c60
VP
2428 /* The type of none suggests that owner is actually deleted.
2429 This should not ever happen. */
35df4500 2430 gdb_assert (bl->owner->type != bp_none);
0bde7532 2431
35df4500
TJB
2432 if (bl->loc_type == bp_loc_software_breakpoint
2433 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2434 {
c02f5703
MS
2435 /* "Normal" instruction breakpoint: either the standard
2436 trap-instruction bp (bp_breakpoint), or a
2437 bp_hardware_breakpoint. */
2438
2439 /* First check to see if we have to handle an overlay. */
2440 if (overlay_debugging == ovly_off
35df4500
TJB
2441 || bl->section == NULL
2442 || !(section_is_overlay (bl->section)))
c02f5703
MS
2443 {
2444 /* No overlay handling: just remove the breakpoint. */
2445
35df4500
TJB
2446 if (bl->loc_type == bp_loc_hardware_breakpoint)
2447 val = target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2448 else
35df4500 2449 val = target_remove_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2450 }
c906108c
SS
2451 else
2452 {
c02f5703
MS
2453 /* This breakpoint is in an overlay section.
2454 Did we set a breakpoint at the LMA? */
2455 if (!overlay_events_enabled)
2456 {
2457 /* Yes -- overlay event support is not active, so we
2458 should have set a breakpoint at the LMA. Remove it.
2459 */
c02f5703
MS
2460 /* Ignore any failures: if the LMA is in ROM, we will
2461 have already warned when we failed to insert it. */
35df4500
TJB
2462 if (bl->loc_type == bp_loc_hardware_breakpoint)
2463 target_remove_hw_breakpoint (bl->gdbarch,
2464 &bl->overlay_target_info);
c02f5703 2465 else
35df4500
TJB
2466 target_remove_breakpoint (bl->gdbarch,
2467 &bl->overlay_target_info);
c02f5703
MS
2468 }
2469 /* Did we set a breakpoint at the VMA?
2470 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2471 if (bl->inserted)
c906108c 2472 {
c02f5703
MS
2473 /* Yes -- remove it. Previously we did not bother to
2474 remove the breakpoint if the section had been
2475 unmapped, but let's not rely on that being safe. We
2476 don't know what the overlay manager might do. */
35df4500
TJB
2477 if (bl->loc_type == bp_loc_hardware_breakpoint)
2478 val = target_remove_hw_breakpoint (bl->gdbarch,
2479 &bl->target_info);
aa67235e
UW
2480
2481 /* However, we should remove *software* breakpoints only
2482 if the section is still mapped, or else we overwrite
2483 wrong code with the saved shadow contents. */
35df4500
TJB
2484 else if (section_is_mapped (bl->section))
2485 val = target_remove_breakpoint (bl->gdbarch,
2486 &bl->target_info);
aa67235e
UW
2487 else
2488 val = 0;
c906108c 2489 }
c02f5703
MS
2490 else
2491 {
2492 /* No -- not inserted, so no need to remove. No error. */
2493 val = 0;
2494 }
c906108c 2495 }
879d1e6b
UW
2496
2497 /* In some cases, we might not be able to remove a breakpoint
2498 in a shared library that has already been removed, but we
2499 have not yet processed the shlib unload event. */
35df4500 2500 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2501 val = 0;
2502
c906108c
SS
2503 if (val)
2504 return val;
35df4500 2505 bl->inserted = (is == mark_inserted);
c906108c 2506 }
35df4500 2507 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2508 {
35df4500
TJB
2509 bl->inserted = (is == mark_inserted);
2510 val = target_remove_watchpoint (bl->address, bl->length,
2511 bl->watchpoint_type, bl->owner->cond_exp);
2e70b7b9 2512
c906108c 2513 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2514 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2515 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2516 bl->owner->number);
c906108c 2517 }
35df4500
TJB
2518 else if (bl->owner->type == bp_catchpoint
2519 && breakpoint_enabled (bl->owner)
2520 && !bl->duplicate)
ce78b96d 2521 {
35df4500 2522 gdb_assert (bl->owner->ops != NULL && bl->owner->ops->remove != NULL);
ce78b96d 2523
35df4500 2524 val = bl->owner->ops->remove (bl->owner);
ce78b96d
JB
2525 if (val)
2526 return val;
35df4500 2527 bl->inserted = (is == mark_inserted);
ce78b96d 2528 }
c906108c
SS
2529
2530 return 0;
2531}
2532
6c95b8df 2533static int
35df4500 2534remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2535{
2536 int ret;
2537 struct cleanup *old_chain;
2538
35df4500
TJB
2539 /* BL is never in moribund_locations by our callers. */
2540 gdb_assert (bl->owner != NULL);
2bdf28a0 2541
35df4500 2542 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2543 /* Permanent breakpoints cannot be inserted or removed. */
2544 return 0;
2545
2546 /* The type of none suggests that owner is actually deleted.
2547 This should not ever happen. */
35df4500 2548 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2549
2550 old_chain = save_current_space_and_thread ();
2551
35df4500 2552 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2553
35df4500 2554 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2555
2556 do_cleanups (old_chain);
2557 return ret;
2558}
2559
c906108c
SS
2560/* Clear the "inserted" flag in all breakpoints. */
2561
25b22b0a 2562void
fba45db2 2563mark_breakpoints_out (void)
c906108c 2564{
35df4500 2565 struct bp_location *bl, **blp_tmp;
c906108c 2566
35df4500
TJB
2567 ALL_BP_LOCATIONS (bl, blp_tmp)
2568 if (bl->pspace == current_program_space)
2569 bl->inserted = 0;
c906108c
SS
2570}
2571
53a5351d
JM
2572/* Clear the "inserted" flag in all breakpoints and delete any
2573 breakpoints which should go away between runs of the program.
c906108c
SS
2574
2575 Plus other such housekeeping that has to be done for breakpoints
2576 between runs.
2577
53a5351d
JM
2578 Note: this function gets called at the end of a run (by
2579 generic_mourn_inferior) and when a run begins (by
2580 init_wait_for_inferior). */
c906108c
SS
2581
2582
2583
2584void
fba45db2 2585breakpoint_init_inferior (enum inf_context context)
c906108c 2586{
35df4500
TJB
2587 struct breakpoint *b, *b_tmp;
2588 struct bp_location *bl, **blp_tmp;
1c5cfe86 2589 int ix;
6c95b8df 2590 struct program_space *pspace = current_program_space;
c906108c 2591
50c71eaf
PA
2592 /* If breakpoint locations are shared across processes, then there's
2593 nothing to do. */
2567c7d9 2594 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2595 return;
2596
35df4500 2597 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2598 {
35df4500
TJB
2599 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2600 if (bl->pspace == pspace
2601 && bl->owner->enable_state != bp_permanent)
2602 bl->inserted = 0;
6c95b8df 2603 }
075f6582 2604
35df4500 2605 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2606 {
6c95b8df
PA
2607 if (b->loc && b->loc->pspace != pspace)
2608 continue;
2609
c5aa993b
JM
2610 switch (b->type)
2611 {
2612 case bp_call_dummy:
c906108c 2613
c5aa993b 2614 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2615 cause problems when the inferior is rerun, so we better get
2616 rid of it. */
2617
2618 case bp_watchpoint_scope:
2619
2620 /* Also get rid of scope breakpoints. */
2621
2622 case bp_shlib_event:
2623
2624 /* Also remove solib event breakpoints. Their addresses may
2625 have changed since the last time we ran the program.
2626 Actually we may now be debugging against different target;
2627 and so the solib backend that installed this breakpoint may
2628 not be used in by the target. E.g.,
2629
2630 (gdb) file prog-linux
2631 (gdb) run # native linux target
2632 ...
2633 (gdb) kill
2634 (gdb) file prog-win.exe
2635 (gdb) tar rem :9999 # remote Windows gdbserver.
2636 */
c906108c 2637
c5aa993b
JM
2638 delete_breakpoint (b);
2639 break;
c906108c 2640
c5aa993b
JM
2641 case bp_watchpoint:
2642 case bp_hardware_watchpoint:
2643 case bp_read_watchpoint:
2644 case bp_access_watchpoint:
c906108c 2645
c5aa993b
JM
2646 /* Likewise for watchpoints on local expressions. */
2647 if (b->exp_valid_block != NULL)
2648 delete_breakpoint (b);
967af18d 2649 else if (context == inf_starting)
c860120c
PM
2650 {
2651 /* Reset val field to force reread of starting value
2652 in insert_breakpoints. */
2653 if (b->val)
2654 value_free (b->val);
2655 b->val = NULL;
fa4727a6 2656 b->val_valid = 0;
c860120c 2657 }
c5aa993b
JM
2658 break;
2659 default:
c5aa993b
JM
2660 break;
2661 }
2662 }
1c5cfe86
PA
2663
2664 /* Get rid of the moribund locations. */
35df4500
TJB
2665 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2666 decref_bp_location (&bl);
1c5cfe86 2667 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2668}
2669
6c95b8df
PA
2670/* These functions concern about actual breakpoints inserted in the
2671 target --- to e.g. check if we need to do decr_pc adjustment or if
2672 we need to hop over the bkpt --- so we check for address space
2673 match, not program space. */
2674
c2c6d25f
JM
2675/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2676 exists at PC. It returns ordinary_breakpoint_here if it's an
2677 ordinary breakpoint, or permanent_breakpoint_here if it's a
2678 permanent breakpoint.
2679 - When continuing from a location with an ordinary breakpoint, we
2680 actually single step once before calling insert_breakpoints.
2681 - When continuing from a localion with a permanent breakpoint, we
2682 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2683 the target, to advance the PC past the breakpoint. */
c906108c 2684
c2c6d25f 2685enum breakpoint_here
6c95b8df 2686breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2687{
35df4500 2688 struct bp_location *bl, **blp_tmp;
c2c6d25f 2689 int any_breakpoint_here = 0;
c906108c 2690
35df4500 2691 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2692 {
35df4500
TJB
2693 if (bl->loc_type != bp_loc_software_breakpoint
2694 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2695 continue;
2696
35df4500
TJB
2697 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2698 if ((breakpoint_enabled (bl->owner)
2699 || bl->owner->enable_state == bp_permanent)
2700 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2701 aspace, pc))
075f6582
DJ
2702 {
2703 if (overlay_debugging
35df4500
TJB
2704 && section_is_overlay (bl->section)
2705 && !section_is_mapped (bl->section))
075f6582 2706 continue; /* unmapped overlay -- can't be a match */
35df4500 2707 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2708 return permanent_breakpoint_here;
2709 else
2710 any_breakpoint_here = 1;
2711 }
2712 }
c906108c 2713
c2c6d25f 2714 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2715}
2716
1c5cfe86
PA
2717/* Return true if there's a moribund breakpoint at PC. */
2718
2719int
6c95b8df 2720moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2721{
2722 struct bp_location *loc;
2723 int ix;
2724
2725 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
6c95b8df
PA
2726 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
2727 aspace, pc))
1c5cfe86
PA
2728 return 1;
2729
2730 return 0;
2731}
c2c6d25f 2732
c36b740a 2733/* Returns non-zero if there's a breakpoint inserted at PC, which is
876fa593 2734 inserted using regular breakpoint_chain / bp_location array mechanism.
c36b740a
VP
2735 This does not check for single-step breakpoints, which are
2736 inserted and removed using direct target manipulation. */
c906108c
SS
2737
2738int
6c95b8df 2739regular_breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2740{
35df4500 2741 struct bp_location *bl, **blp_tmp;
c906108c 2742
35df4500 2743 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2744 {
35df4500
TJB
2745 if (bl->loc_type != bp_loc_software_breakpoint
2746 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2747 continue;
2748
35df4500
TJB
2749 if (bl->inserted
2750 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2751 aspace, pc))
075f6582
DJ
2752 {
2753 if (overlay_debugging
35df4500
TJB
2754 && section_is_overlay (bl->section)
2755 && !section_is_mapped (bl->section))
075f6582
DJ
2756 continue; /* unmapped overlay -- can't be a match */
2757 else
2758 return 1;
2759 }
c5aa993b 2760 }
c36b740a
VP
2761 return 0;
2762}
2763
2764/* Returns non-zero iff there's either regular breakpoint
2765 or a single step breakpoint inserted at PC. */
2766
2767int
6c95b8df 2768breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2769{
6c95b8df 2770 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2771 return 1;
c906108c 2772
6c95b8df 2773 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2774 return 1;
2775
c906108c
SS
2776 return 0;
2777}
2778
4fa8626c
DJ
2779/* This function returns non-zero iff there is a software breakpoint
2780 inserted at PC. */
2781
2782int
6c95b8df 2783software_breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4fa8626c 2784{
35df4500 2785 struct bp_location *bl, **blp_tmp;
4fa8626c 2786
35df4500 2787 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2788 {
35df4500 2789 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2790 continue;
2791
35df4500
TJB
2792 if (bl->inserted
2793 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2794 aspace, pc))
4fa8626c
DJ
2795 {
2796 if (overlay_debugging
35df4500
TJB
2797 && section_is_overlay (bl->section)
2798 && !section_is_mapped (bl->section))
4fa8626c
DJ
2799 continue; /* unmapped overlay -- can't be a match */
2800 else
2801 return 1;
2802 }
2803 }
2804
1aafd4da 2805 /* Also check for software single-step breakpoints. */
6c95b8df 2806 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2807 return 1;
2808
4fa8626c
DJ
2809 return 0;
2810}
2811
9093389c
PA
2812int
2813hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2814 CORE_ADDR addr, ULONGEST len)
2815{
2816 struct breakpoint *bpt;
2817
2818 ALL_BREAKPOINTS (bpt)
2819 {
2820 struct bp_location *loc;
2821
2822 if (bpt->type != bp_hardware_watchpoint
2823 && bpt->type != bp_access_watchpoint)
2824 continue;
2825
2826 if (!breakpoint_enabled (bpt))
2827 continue;
2828
2829 for (loc = bpt->loc; loc; loc = loc->next)
2830 if (loc->pspace->aspace == aspace && loc->inserted)
2831 {
2832 CORE_ADDR l, h;
2833
2834 /* Check for intersection. */
2835 l = max (loc->address, addr);
2836 h = min (loc->address + loc->length, addr + len);
2837 if (l < h)
2838 return 1;
2839 }
2840 }
2841 return 0;
2842}
2843
075f6582
DJ
2844/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2845 PC is valid for process/thread PTID. */
c906108c
SS
2846
2847int
6c95b8df
PA
2848breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2849 ptid_t ptid)
c906108c 2850{
35df4500 2851 struct bp_location *bl, **blp_tmp;
4a306c9a 2852 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 2853 int thread = -1;
4a306c9a 2854 int task = 0;
a6f1cd96 2855
35df4500 2856 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2857 {
35df4500
TJB
2858 if (bl->loc_type != bp_loc_software_breakpoint
2859 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2860 continue;
2861
35df4500
TJB
2862 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2863 if (!breakpoint_enabled (bl->owner)
2864 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
2865 continue;
2866
35df4500 2867 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2868 aspace, pc))
a6f1cd96
JB
2869 continue;
2870
35df4500 2871 if (bl->owner->thread != -1)
075f6582 2872 {
a6f1cd96
JB
2873 /* This is a thread-specific breakpoint. Check that ptid
2874 matches that thread. If thread hasn't been computed yet,
2875 it is now time to do so. */
2876 if (thread == -1)
2877 thread = pid_to_thread_id (ptid);
35df4500 2878 if (bl->owner->thread != thread)
a6f1cd96 2879 continue;
075f6582 2880 }
a6f1cd96 2881
35df4500 2882 if (bl->owner->task != 0)
4a306c9a
JB
2883 {
2884 /* This is a task-specific breakpoint. Check that ptid
2885 matches that task. If task hasn't been computed yet,
2886 it is now time to do so. */
2887 if (task == 0)
2888 task = ada_get_task_number (ptid);
35df4500 2889 if (bl->owner->task != task)
4a306c9a
JB
2890 continue;
2891 }
2892
a6f1cd96 2893 if (overlay_debugging
35df4500
TJB
2894 && section_is_overlay (bl->section)
2895 && !section_is_mapped (bl->section))
a6f1cd96
JB
2896 continue; /* unmapped overlay -- can't be a match */
2897
2898 return 1;
c5aa993b 2899 }
c906108c
SS
2900
2901 return 0;
2902}
c906108c 2903\f
c5aa993b 2904
c906108c
SS
2905/* bpstat stuff. External routines' interfaces are documented
2906 in breakpoint.h. */
2907
2908int
fba45db2 2909ep_is_catchpoint (struct breakpoint *ep)
c906108c 2910{
533be4dd 2911 return (ep->type == bp_catchpoint);
c906108c
SS
2912}
2913
f431efe5
PA
2914/* Frees any storage that is part of a bpstat. Does not walk the
2915 'next' chain. */
2916
2917static void
198757a8
VP
2918bpstat_free (bpstat bs)
2919{
2920 if (bs->old_val != NULL)
2921 value_free (bs->old_val);
9add0f1b 2922 decref_counted_command_line (&bs->commands);
f431efe5 2923 decref_bp_location (&bs->bp_location_at);
198757a8
VP
2924 xfree (bs);
2925}
2926
c906108c
SS
2927/* Clear a bpstat so that it says we are not at any breakpoint.
2928 Also free any storage that is part of a bpstat. */
2929
2930void
fba45db2 2931bpstat_clear (bpstat *bsp)
c906108c
SS
2932{
2933 bpstat p;
2934 bpstat q;
2935
2936 if (bsp == 0)
2937 return;
2938 p = *bsp;
2939 while (p != NULL)
2940 {
2941 q = p->next;
198757a8 2942 bpstat_free (p);
c906108c
SS
2943 p = q;
2944 }
2945 *bsp = NULL;
2946}
2947
2948/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
2949 is part of the bpstat is copied as well. */
2950
2951bpstat
fba45db2 2952bpstat_copy (bpstat bs)
c906108c
SS
2953{
2954 bpstat p = NULL;
2955 bpstat tmp;
2956 bpstat retval = NULL;
2957
2958 if (bs == NULL)
2959 return bs;
2960
2961 for (; bs != NULL; bs = bs->next)
2962 {
2963 tmp = (bpstat) xmalloc (sizeof (*tmp));
2964 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 2965 incref_counted_command_line (tmp->commands);
f431efe5 2966 incref_bp_location (tmp->bp_location_at);
31cc81e9 2967 if (bs->old_val != NULL)
3c3185ac
JK
2968 {
2969 tmp->old_val = value_copy (bs->old_val);
2970 release_value (tmp->old_val);
2971 }
31cc81e9 2972
c906108c
SS
2973 if (p == NULL)
2974 /* This is the first thing in the chain. */
2975 retval = tmp;
2976 else
2977 p->next = tmp;
2978 p = tmp;
2979 }
2980 p->next = NULL;
2981 return retval;
2982}
2983
2984/* Find the bpstat associated with this breakpoint */
2985
2986bpstat
fba45db2 2987bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 2988{
c5aa993b
JM
2989 if (bsp == NULL)
2990 return NULL;
c906108c 2991
c5aa993b
JM
2992 for (; bsp != NULL; bsp = bsp->next)
2993 {
f431efe5 2994 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
2995 return bsp;
2996 }
c906108c
SS
2997 return NULL;
2998}
2999
8671a17b 3000/* Put in *NUM the breakpoint number of the first breakpoint we are stopped
c906108c
SS
3001 at. *BSP upon return is a bpstat which points to the remaining
3002 breakpoints stopped at (but which is not guaranteed to be good for
3003 anything but further calls to bpstat_num).
8671a17b
PA
3004 Return 0 if passed a bpstat which does not indicate any breakpoints.
3005 Return -1 if stopped at a breakpoint that has been deleted since
3006 we set it.
3007 Return 1 otherwise. */
c906108c
SS
3008
3009int
8671a17b 3010bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3011{
3012 struct breakpoint *b;
3013
3014 if ((*bsp) == NULL)
3015 return 0; /* No more breakpoint values */
8671a17b 3016
4f8d1dc6
VP
3017 /* We assume we'll never have several bpstats that
3018 correspond to a single breakpoint -- otherwise,
3019 this function might return the same number more
3020 than once and this will look ugly. */
f431efe5 3021 b = (*bsp)->breakpoint_at;
8671a17b
PA
3022 *bsp = (*bsp)->next;
3023 if (b == NULL)
3024 return -1; /* breakpoint that's been deleted since */
3025
3026 *num = b->number; /* We have its number */
3027 return 1;
c906108c
SS
3028}
3029
3030/* Modify BS so that the actions will not be performed. */
3031
3032void
fba45db2 3033bpstat_clear_actions (bpstat bs)
c906108c
SS
3034{
3035 for (; bs != NULL; bs = bs->next)
3036 {
9add0f1b 3037 decref_counted_command_line (&bs->commands);
dde2d684 3038 bs->commands_left = NULL;
c906108c
SS
3039 if (bs->old_val != NULL)
3040 {
3041 value_free (bs->old_val);
3042 bs->old_val = NULL;
3043 }
3044 }
3045}
3046
f3b1572e
PA
3047/* Called when a command is about to proceed the inferior. */
3048
3049static void
3050breakpoint_about_to_proceed (void)
3051{
3052 if (!ptid_equal (inferior_ptid, null_ptid))
3053 {
3054 struct thread_info *tp = inferior_thread ();
3055
3056 /* Allow inferior function calls in breakpoint commands to not
3057 interrupt the command list. When the call finishes
3058 successfully, the inferior will be standing at the same
3059 breakpoint as if nothing happened. */
16c381f0 3060 if (tp->control.in_infcall)
f3b1572e
PA
3061 return;
3062 }
3063
3064 breakpoint_proceeded = 1;
3065}
3066
c906108c 3067/* Stub for cleaning up our state if we error-out of a breakpoint command */
c906108c 3068static void
4efb68b1 3069cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3070{
3071 executing_breakpoint_commands = 0;
3072}
3073
3074/* Execute all the commands associated with all the breakpoints at this
3075 location. Any of these commands could cause the process to proceed
3076 beyond this point, etc. We look out for such changes by checking
347bddb7 3077 the global "breakpoint_proceeded" after each command.
c906108c 3078
347bddb7
PA
3079 Returns true if a breakpoint command resumed the inferior. In that
3080 case, it is the caller's responsibility to recall it again with the
3081 bpstat of the current thread. */
3082
3083static int
3084bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3085{
3086 bpstat bs;
3087 struct cleanup *old_chain;
347bddb7 3088 int again = 0;
c906108c
SS
3089
3090 /* Avoid endless recursion if a `source' command is contained
3091 in bs->commands. */
3092 if (executing_breakpoint_commands)
347bddb7 3093 return 0;
c906108c
SS
3094
3095 executing_breakpoint_commands = 1;
3096 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3097
c906108c
SS
3098 /* This pointer will iterate over the list of bpstat's. */
3099 bs = *bsp;
3100
3101 breakpoint_proceeded = 0;
3102 for (; bs != NULL; bs = bs->next)
3103 {
9add0f1b 3104 struct counted_command_line *ccmd;
6c50ab1c
JB
3105 struct command_line *cmd;
3106 struct cleanup *this_cmd_tree_chain;
3107
3108 /* Take ownership of the BSP's command tree, if it has one.
3109
3110 The command tree could legitimately contain commands like
3111 'step' and 'next', which call clear_proceed_status, which
3112 frees stop_bpstat's command tree. To make sure this doesn't
3113 free the tree we're executing out from under us, we need to
3114 take ownership of the tree ourselves. Since a given bpstat's
3115 commands are only executed once, we don't need to copy it; we
3116 can clear the pointer in the bpstat, and make sure we free
3117 the tree when we're done. */
9add0f1b
TT
3118 ccmd = bs->commands;
3119 bs->commands = NULL;
3120 this_cmd_tree_chain
3121 = make_cleanup_decref_counted_command_line (&ccmd);
3122 cmd = bs->commands_left;
3123 bs->commands_left = NULL;
6c50ab1c 3124
c906108c
SS
3125 while (cmd != NULL)
3126 {
3127 execute_control_command (cmd);
3128
3129 if (breakpoint_proceeded)
3130 break;
3131 else
3132 cmd = cmd->next;
3133 }
6c50ab1c
JB
3134
3135 /* We can free this command tree now. */
3136 do_cleanups (this_cmd_tree_chain);
3137
c906108c 3138 if (breakpoint_proceeded)
32c1e744
VP
3139 {
3140 if (target_can_async_p ())
347bddb7
PA
3141 /* If we are in async mode, then the target might be still
3142 running, not stopped at any breakpoint, so nothing for
3143 us to do here -- just return to the event loop. */
3144 ;
32c1e744
VP
3145 else
3146 /* In sync mode, when execute_control_command returns
3147 we're already standing on the next breakpoint.
347bddb7
PA
3148 Breakpoint commands for that stop were not run, since
3149 execute_command does not run breakpoint commands --
3150 only command_line_handler does, but that one is not
3151 involved in execution of breakpoint commands. So, we
3152 can now execute breakpoint commands. It should be
3153 noted that making execute_command do bpstat actions is
3154 not an option -- in this case we'll have recursive
3155 invocation of bpstat for each breakpoint with a
3156 command, and can easily blow up GDB stack. Instead, we
3157 return true, which will trigger the caller to recall us
3158 with the new stop_bpstat. */
3159 again = 1;
3160 break;
32c1e744 3161 }
c906108c 3162 }
c2b8ed2c 3163 do_cleanups (old_chain);
347bddb7
PA
3164 return again;
3165}
3166
3167void
3168bpstat_do_actions (void)
3169{
3170 /* Do any commands attached to breakpoint we are stopped at. */
3171 while (!ptid_equal (inferior_ptid, null_ptid)
3172 && target_has_execution
3173 && !is_exited (inferior_ptid)
3174 && !is_executing (inferior_ptid))
3175 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3176 and only return when it is stopped at the next breakpoint, we
3177 keep doing breakpoint actions until it returns false to
3178 indicate the inferior was not resumed. */
16c381f0 3179 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3180 break;
c906108c
SS
3181}
3182
fa4727a6
DJ
3183/* Print out the (old or new) value associated with a watchpoint. */
3184
3185static void
3186watchpoint_value_print (struct value *val, struct ui_file *stream)
3187{
3188 if (val == NULL)
3189 fprintf_unfiltered (stream, _("<unreadable>"));
3190 else
79a45b7d
TT
3191 {
3192 struct value_print_options opts;
3193 get_user_print_options (&opts);
3194 value_print (val, stream, &opts);
3195 }
fa4727a6
DJ
3196}
3197
e514a9d6 3198/* This is the normal print function for a bpstat. In the future,
c906108c 3199 much of this logic could (should?) be moved to bpstat_stop_status,
e514a9d6
JM
3200 by having it set different print_it values.
3201
3202 Current scheme: When we stop, bpstat_print() is called. It loops
3203 through the bpstat list of things causing this stop, calling the
3204 print_bp_stop_message function on each one. The behavior of the
3205 print_bp_stop_message function depends on the print_it field of
3206 bpstat. If such field so indicates, call this function here.
3207
3208 Return values from this routine (ultimately used by bpstat_print()
3209 and normal_stop() to decide what to do):
3210 PRINT_NOTHING: Means we already printed all we needed to print,
3211 don't print anything else.
3212 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
3213 that something to be followed by a location.
3214 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
3215 that something to be followed by a location.
3216 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
3217 analysis. */
c906108c 3218
917317f4 3219static enum print_stop_action
fba45db2 3220print_it_typical (bpstat bs)
c906108c 3221{
f7545552 3222 struct cleanup *old_chain;
4f8d1dc6 3223 struct breakpoint *b;
89f9893c 3224 const struct bp_location *bl;
8b93c638 3225 struct ui_stream *stb;
f7545552
TT
3226 int bp_temp = 0;
3227 enum print_stop_action result;
3228
c906108c
SS
3229 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3230 which has since been deleted. */
e514a9d6 3231 if (bs->breakpoint_at == NULL)
917317f4 3232 return PRINT_UNKNOWN;
2bdf28a0 3233
f431efe5
PA
3234 gdb_assert (bs->bp_location_at != NULL);
3235
3236 bl = bs->bp_location_at;
3237 b = bs->breakpoint_at;
c906108c 3238
f7545552
TT
3239 stb = ui_out_stream_new (uiout);
3240 old_chain = make_cleanup_ui_out_stream_delete (stb);
3241
4f8d1dc6 3242 switch (b->type)
c906108c 3243 {
e514a9d6
JM
3244 case bp_breakpoint:
3245 case bp_hardware_breakpoint:
f431efe5 3246 bp_temp = b->disposition == disp_del;
0d381245
VP
3247 if (bl->address != bl->requested_address)
3248 breakpoint_adjustment_warning (bl->requested_address,
3249 bl->address,
4f8d1dc6
VP
3250 b->number, 1);
3251 annotate_breakpoint (b->number);
2cec12e5
AR
3252 if (bp_temp)
3253 ui_out_text (uiout, "\nTemporary breakpoint ");
3254 else
3255 ui_out_text (uiout, "\nBreakpoint ");
9dc5e2a9 3256 if (ui_out_is_mi_like_p (uiout))
2cec12e5
AR
3257 {
3258 ui_out_field_string (uiout, "reason",
3259 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
3260 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3261 }
4f8d1dc6 3262 ui_out_field_int (uiout, "bkptno", b->number);
8b93c638 3263 ui_out_text (uiout, ", ");
f7545552 3264 result = PRINT_SRC_AND_LOC;
e514a9d6
JM
3265 break;
3266
3267 case bp_shlib_event:
917317f4
JM
3268 /* Did we stop because the user set the stop_on_solib_events
3269 variable? (If so, we report this as a generic, "Stopped due
3270 to shlib event" message.) */
a3f17187 3271 printf_filtered (_("Stopped due to shared library event\n"));
f7545552 3272 result = PRINT_NOTHING;
e514a9d6
JM
3273 break;
3274
c4093a6a
JM
3275 case bp_thread_event:
3276 /* Not sure how we will get here.
3277 GDB should not stop for these breakpoints. */
a3f17187 3278 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
f7545552 3279 result = PRINT_NOTHING;
c4093a6a
JM
3280 break;
3281
1900040c
MS
3282 case bp_overlay_event:
3283 /* By analogy with the thread event, GDB should not stop for these. */
a3f17187 3284 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
f7545552 3285 result = PRINT_NOTHING;
1900040c
MS
3286 break;
3287
0fd8e87f
UW
3288 case bp_longjmp_master:
3289 /* These should never be enabled. */
3290 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
3291 result = PRINT_NOTHING;
3292 break;
3293
aa7d318d
TT
3294 case bp_std_terminate_master:
3295 /* These should never be enabled. */
3296 printf_filtered (_("std::terminate Master Breakpoint: gdb should not stop!\n"));
3297 result = PRINT_NOTHING;
3298 break;
3299
186c406b
TT
3300 case bp_exception_master:
3301 /* These should never be enabled. */
3302 printf_filtered (_("Exception Master Breakpoint: gdb should not stop!\n"));
3303 result = PRINT_NOTHING;
3304 break;
3305
e514a9d6
JM
3306 case bp_watchpoint:
3307 case bp_hardware_watchpoint:
fa4727a6
DJ
3308 annotate_watchpoint (b->number);
3309 if (ui_out_is_mi_like_p (uiout))
3310 ui_out_field_string
3311 (uiout, "reason",
3312 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
3313 mention (b);
f7545552 3314 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
fa4727a6
DJ
3315 ui_out_text (uiout, "\nOld value = ");
3316 watchpoint_value_print (bs->old_val, stb->stream);
3317 ui_out_field_stream (uiout, "old", stb);
3318 ui_out_text (uiout, "\nNew value = ");
3319 watchpoint_value_print (b->val, stb->stream);
3320 ui_out_field_stream (uiout, "new", stb);
fa4727a6 3321 ui_out_text (uiout, "\n");
e514a9d6 3322 /* More than one watchpoint may have been triggered. */
f7545552 3323 result = PRINT_UNKNOWN;
e514a9d6
JM
3324 break;
3325
3326 case bp_read_watchpoint:
9dc5e2a9 3327 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3328 ui_out_field_string
3329 (uiout, "reason",
3330 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
4f8d1dc6 3331 mention (b);
f7545552 3332 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3333 ui_out_text (uiout, "\nValue = ");
fa4727a6 3334 watchpoint_value_print (b->val, stb->stream);
8b93c638 3335 ui_out_field_stream (uiout, "value", stb);
8b93c638 3336 ui_out_text (uiout, "\n");
f7545552 3337 result = PRINT_UNKNOWN;
e514a9d6
JM
3338 break;
3339
3340 case bp_access_watchpoint:
fa4727a6 3341 if (bs->old_val != NULL)
8b93c638 3342 {
4f8d1dc6 3343 annotate_watchpoint (b->number);
9dc5e2a9 3344 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3345 ui_out_field_string
3346 (uiout, "reason",
3347 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
4f8d1dc6 3348 mention (b);
f7545552 3349 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3350 ui_out_text (uiout, "\nOld value = ");
fa4727a6 3351 watchpoint_value_print (bs->old_val, stb->stream);
8b93c638 3352 ui_out_field_stream (uiout, "old", stb);
8b93c638
JM
3353 ui_out_text (uiout, "\nNew value = ");
3354 }
3355 else
3356 {
4f8d1dc6 3357 mention (b);
9dc5e2a9 3358 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3359 ui_out_field_string
3360 (uiout, "reason",
3361 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
f7545552 3362 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638
JM
3363 ui_out_text (uiout, "\nValue = ");
3364 }
fa4727a6 3365 watchpoint_value_print (b->val, stb->stream);
8b93c638 3366 ui_out_field_stream (uiout, "new", stb);
8b93c638 3367 ui_out_text (uiout, "\n");
f7545552 3368 result = PRINT_UNKNOWN;
e514a9d6 3369 break;
4ce44c66 3370
e514a9d6
JM
3371 /* Fall through, we don't deal with these types of breakpoints
3372 here. */
3373
11cf8741 3374 case bp_finish:
9dc5e2a9 3375 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3376 ui_out_field_string
3377 (uiout, "reason",
3378 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
f7545552 3379 result = PRINT_UNKNOWN;
8b93c638
JM
3380 break;
3381
e514a9d6 3382 case bp_until:
9dc5e2a9 3383 if (ui_out_is_mi_like_p (uiout))
1fbc2a49
NR
3384 ui_out_field_string
3385 (uiout, "reason",
3386 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
f7545552 3387 result = PRINT_UNKNOWN;
8b93c638
JM
3388 break;
3389
c2d11a7d 3390 case bp_none:
e514a9d6
JM
3391 case bp_longjmp:
3392 case bp_longjmp_resume:
186c406b
TT
3393 case bp_exception:
3394 case bp_exception_resume:
e514a9d6 3395 case bp_step_resume:
e514a9d6
JM
3396 case bp_watchpoint_scope:
3397 case bp_call_dummy:
aa7d318d 3398 case bp_std_terminate:
1042e4c0 3399 case bp_tracepoint:
7a697b8d 3400 case bp_fast_tracepoint:
4efc6507 3401 case bp_jit_event:
e514a9d6 3402 default:
f7545552
TT
3403 result = PRINT_UNKNOWN;
3404 break;
e514a9d6 3405 }
f7545552
TT
3406
3407 do_cleanups (old_chain);
3408 return result;
e514a9d6
JM
3409}
3410
3411/* Generic routine for printing messages indicating why we
3412 stopped. The behavior of this function depends on the value
3413 'print_it' in the bpstat structure. Under some circumstances we
3414 may decide not to print anything here and delegate the task to
3415 normal_stop(). */
3416
3417static enum print_stop_action
3418print_bp_stop_message (bpstat bs)
3419{
3420 switch (bs->print_it)
3421 {
3422 case print_it_noop:
3423 /* Nothing should be printed for this bpstat entry. */
3424 return PRINT_UNKNOWN;
3425 break;
3426
3427 case print_it_done:
3428 /* We still want to print the frame, but we already printed the
3429 relevant messages. */
3430 return PRINT_SRC_AND_LOC;
3431 break;
3432
3433 case print_it_normal:
4f8d1dc6 3434 {
f431efe5
PA
3435 struct breakpoint *b = bs->breakpoint_at;
3436
4f8d1dc6
VP
3437 /* Normal case. Call the breakpoint's print_it method, or
3438 print_it_typical. */
3439 /* FIXME: how breakpoint can ever be NULL here? */
3440 if (b != NULL && b->ops != NULL && b->ops->print_it != NULL)
3441 return b->ops->print_it (b);
3442 else
3443 return print_it_typical (bs);
3444 }
3445 break;
3086aeae 3446
e514a9d6 3447 default:
8e65ff28 3448 internal_error (__FILE__, __LINE__,
e2e0b3e5 3449 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3450 break;
c906108c 3451 }
c906108c
SS
3452}
3453
e514a9d6
JM
3454/* Print a message indicating what happened. This is called from
3455 normal_stop(). The input to this routine is the head of the bpstat
3456 list - a list of the eventpoints that caused this stop. This
3457 routine calls the generic print routine for printing a message
3458 about reasons for stopping. This will print (for example) the
3459 "Breakpoint n," part of the output. The return value of this
3460 routine is one of:
c906108c 3461
917317f4
JM
3462 PRINT_UNKNOWN: Means we printed nothing
3463 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
c5aa993b
JM
3464 code to print the location. An example is
3465 "Breakpoint 1, " which should be followed by
3466 the location.
917317f4 3467 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3468 to also print the location part of the message.
3469 An example is the catch/throw messages, which
917317f4
JM
3470 don't require a location appended to the end.
3471 PRINT_NOTHING: We have done some printing and we don't need any
3472 further info to be printed.*/
c906108c 3473
917317f4 3474enum print_stop_action
fba45db2 3475bpstat_print (bpstat bs)
c906108c
SS
3476{
3477 int val;
c5aa993b 3478
c906108c 3479 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3480 (Currently all watchpoints go on the bpstat whether hit or not.
3481 That probably could (should) be changed, provided care is taken
c906108c 3482 with respect to bpstat_explains_signal). */
e514a9d6
JM
3483 for (; bs; bs = bs->next)
3484 {
3485 val = print_bp_stop_message (bs);
3486 if (val == PRINT_SRC_ONLY
3487 || val == PRINT_SRC_AND_LOC
3488 || val == PRINT_NOTHING)
3489 return val;
3490 }
c906108c 3491
e514a9d6
JM
3492 /* We reached the end of the chain, or we got a null BS to start
3493 with and nothing was printed. */
917317f4 3494 return PRINT_UNKNOWN;
c906108c
SS
3495}
3496
3497/* Evaluate the expression EXP and return 1 if value is zero.
3498 This is used inside a catch_errors to evaluate the breakpoint condition.
3499 The argument is a "struct expression *" that has been cast to char * to
3500 make it pass through catch_errors. */
3501
3502static int
4efb68b1 3503breakpoint_cond_eval (void *exp)
c906108c 3504{
278cd55f 3505 struct value *mark = value_mark ();
c5aa993b 3506 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3507
c906108c
SS
3508 value_free_to_mark (mark);
3509 return i;
3510}
3511
5760d0ab 3512/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3513
3514static bpstat
5760d0ab 3515bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3516{
3517 bpstat bs;
3518
3519 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3520 bs->next = NULL;
3521 **bs_link_pointer = bs;
3522 *bs_link_pointer = &bs->next;
f431efe5
PA
3523 bs->breakpoint_at = bl->owner;
3524 bs->bp_location_at = bl;
3525 incref_bp_location (bl);
c906108c
SS
3526 /* If the condition is false, etc., don't do the commands. */
3527 bs->commands = NULL;
9add0f1b 3528 bs->commands_left = NULL;
c906108c
SS
3529 bs->old_val = NULL;
3530 bs->print_it = print_it_normal;
3531 return bs;
3532}
3533\f
d983da9c
DJ
3534/* The target has stopped with waitstatus WS. Check if any hardware
3535 watchpoints have triggered, according to the target. */
3536
3537int
3538watchpoints_triggered (struct target_waitstatus *ws)
3539{
d92524f1 3540 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3541 CORE_ADDR addr;
3542 struct breakpoint *b;
3543
3544 if (!stopped_by_watchpoint)
3545 {
3546 /* We were not stopped by a watchpoint. Mark all watchpoints
3547 as not triggered. */
3548 ALL_BREAKPOINTS (b)
cc60f2e3 3549 if (is_hardware_watchpoint (b))
d983da9c
DJ
3550 b->watchpoint_triggered = watch_triggered_no;
3551
3552 return 0;
3553 }
3554
3555 if (!target_stopped_data_address (&current_target, &addr))
3556 {
3557 /* We were stopped by a watchpoint, but we don't know where.
3558 Mark all watchpoints as unknown. */
3559 ALL_BREAKPOINTS (b)
cc60f2e3 3560 if (is_hardware_watchpoint (b))
d983da9c
DJ
3561 b->watchpoint_triggered = watch_triggered_unknown;
3562
3563 return stopped_by_watchpoint;
3564 }
3565
3566 /* The target could report the data address. Mark watchpoints
3567 affected by this data address as triggered, and all others as not
3568 triggered. */
3569
3570 ALL_BREAKPOINTS (b)
cc60f2e3 3571 if (is_hardware_watchpoint (b))
d983da9c 3572 {
a5606eee 3573 struct bp_location *loc;
d983da9c
DJ
3574
3575 b->watchpoint_triggered = watch_triggered_no;
a5606eee
VP
3576 for (loc = b->loc; loc; loc = loc->next)
3577 /* Exact match not required. Within range is
3578 sufficient. */
5009afc5
AS
3579 if (target_watchpoint_addr_within_range (&current_target,
3580 addr, loc->address,
3581 loc->length))
a5606eee
VP
3582 {
3583 b->watchpoint_triggered = watch_triggered_yes;
3584 break;
3585 }
d983da9c
DJ
3586 }
3587
3588 return 1;
3589}
3590
c906108c
SS
3591/* Possible return values for watchpoint_check (this can't be an enum
3592 because of check_errors). */
3593/* The watchpoint has been deleted. */
3594#define WP_DELETED 1
3595/* The value has changed. */
3596#define WP_VALUE_CHANGED 2
3597/* The value has not changed. */
3598#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3599/* Ignore this watchpoint, no matter if the value changed or not. */
3600#define WP_IGNORE 4
c906108c
SS
3601
3602#define BP_TEMPFLAG 1
3603#define BP_HARDWAREFLAG 2
3604
553e4c11
JB
3605/* Evaluate watchpoint condition expression and check if its value changed.
3606
3607 P should be a pointer to struct bpstat, but is defined as a void *
3608 in order for this function to be usable with catch_errors. */
c906108c
SS
3609
3610static int
4efb68b1 3611watchpoint_check (void *p)
c906108c
SS
3612{
3613 bpstat bs = (bpstat) p;
3614 struct breakpoint *b;
3615 struct frame_info *fr;
3616 int within_current_scope;
3617
f431efe5 3618 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3619 gdb_assert (bs->breakpoint_at != NULL);
f431efe5 3620 b = bs->breakpoint_at;
c906108c 3621
f6bc2008
PA
3622 /* If this is a local watchpoint, we only want to check if the
3623 watchpoint frame is in scope if the current thread is the thread
3624 that was used to create the watchpoint. */
3625 if (!watchpoint_in_thread_scope (b))
60e1c644 3626 return WP_IGNORE;
f6bc2008 3627
c906108c
SS
3628 if (b->exp_valid_block == NULL)
3629 within_current_scope = 1;
3630 else
3631 {
edb3359d
DJ
3632 struct frame_info *frame = get_current_frame ();
3633 struct gdbarch *frame_arch = get_frame_arch (frame);
3634 CORE_ADDR frame_pc = get_frame_pc (frame);
3635
a0f49112
JK
3636 /* in_function_epilogue_p() returns a non-zero value if we're still
3637 in the function but the stack frame has already been invalidated.
3638 Since we can't rely on the values of local variables after the
3639 stack has been destroyed, we are treating the watchpoint in that
3640 state as `not changed' without further checking. Don't mark
3641 watchpoints as changed if the current frame is in an epilogue -
3642 even if they are in some other frame, our view of the stack
3643 is likely to be wrong and frame_find_by_id could error out. */
3644 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3645 return WP_IGNORE;
a0f49112 3646
101dcfbe 3647 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3648 within_current_scope = (fr != NULL);
69fbadd5
DJ
3649
3650 /* If we've gotten confused in the unwinder, we might have
3651 returned a frame that can't describe this variable. */
edb3359d
DJ
3652 if (within_current_scope)
3653 {
3654 struct symbol *function;
3655
3656 function = get_frame_function (fr);
3657 if (function == NULL
3658 || !contained_in (b->exp_valid_block,
3659 SYMBOL_BLOCK_VALUE (function)))
3660 within_current_scope = 0;
3661 }
69fbadd5 3662
edb3359d 3663 if (within_current_scope)
c906108c
SS
3664 /* If we end up stopping, the current frame will get selected
3665 in normal_stop. So this call to select_frame won't affect
3666 the user. */
0f7d239c 3667 select_frame (fr);
c906108c 3668 }
c5aa993b 3669
c906108c
SS
3670 if (within_current_scope)
3671 {
3672 /* We use value_{,free_to_}mark because it could be a
3673 *long* time before we return to the command level and
c5aa993b
JM
3674 call free_all_values. We can't call free_all_values because
3675 we might be in the middle of evaluating a function call. */
c906108c 3676
0cf6dd15 3677 int pc = 0;
278cd55f 3678 struct value *mark = value_mark ();
fa4727a6
DJ
3679 struct value *new_val;
3680
0cf6dd15 3681 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6
TJB
3682
3683 /* We use value_equal_contents instead of value_equal because the latter
3684 coerces an array to a pointer, thus comparing just the address of the
3685 array instead of its contents. This is not what we want. */
fa4727a6 3686 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3687 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3688 {
fa4727a6
DJ
3689 if (new_val != NULL)
3690 {
3691 release_value (new_val);
3692 value_free_to_mark (mark);
3693 }
c906108c
SS
3694 bs->old_val = b->val;
3695 b->val = new_val;
fa4727a6 3696 b->val_valid = 1;
c906108c
SS
3697 return WP_VALUE_CHANGED;
3698 }
3699 else
3700 {
60e1c644 3701 /* Nothing changed. */
c906108c 3702 value_free_to_mark (mark);
c906108c
SS
3703 return WP_VALUE_NOT_CHANGED;
3704 }
3705 }
3706 else
3707 {
3708 /* This seems like the only logical thing to do because
c5aa993b
JM
3709 if we temporarily ignored the watchpoint, then when
3710 we reenter the block in which it is valid it contains
3711 garbage (in the case of a function, it may have two
3712 garbage values, one before and one after the prologue).
3713 So we can't even detect the first assignment to it and
3714 watch after that (since the garbage may or may not equal
3715 the first value assigned). */
4ce44c66
JM
3716 /* We print all the stop information in print_it_typical(), but
3717 in this case, by the time we call print_it_typical() this bp
3718 will be deleted already. So we have no choice but print the
3719 information here. */
9dc5e2a9 3720 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3721 ui_out_field_string
3722 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3723 ui_out_text (uiout, "\nWatchpoint ");
4f8d1dc6 3724 ui_out_field_int (uiout, "wpnum", b->number);
8b93c638
JM
3725 ui_out_text (uiout, " deleted because the program has left the block in\n\
3726which its expression is valid.\n");
4ce44c66 3727
c906108c 3728 if (b->related_breakpoint)
60e1c644
PA
3729 {
3730 b->related_breakpoint->disposition = disp_del_at_next_stop;
3731 b->related_breakpoint->related_breakpoint = NULL;
3732 b->related_breakpoint = NULL;
3733 }
b5de0fa7 3734 b->disposition = disp_del_at_next_stop;
c906108c
SS
3735
3736 return WP_DELETED;
3737 }
3738}
3739
18a18393
VP
3740/* Return true if it looks like target has stopped due to hitting
3741 breakpoint location BL. This function does not check if we
3742 should stop, only if BL explains the stop. */
3743static int
6c95b8df
PA
3744bpstat_check_location (const struct bp_location *bl,
3745 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3746{
3747 struct breakpoint *b = bl->owner;
3748
2bdf28a0
JK
3749 /* BL is from existing struct breakpoint. */
3750 gdb_assert (b != NULL);
3751
e8595ef6
SS
3752 /* By definition, the inferior does not report stops at
3753 tracepoints. */
d77f58be 3754 if (is_tracepoint (b))
e8595ef6
SS
3755 return 0;
3756
cc60f2e3 3757 if (!is_watchpoint (b)
18a18393 3758 && b->type != bp_hardware_breakpoint
fe798b75 3759 && b->type != bp_catchpoint) /* a non-watchpoint bp */
18a18393 3760 {
6c95b8df
PA
3761 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
3762 aspace, bp_addr))
18a18393
VP
3763 return 0;
3764 if (overlay_debugging /* unmapped overlay section */
35df4500 3765 && section_is_overlay (bl->section)
18a18393
VP
3766 && !section_is_mapped (bl->section))
3767 return 0;
3768 }
cc60f2e3 3769
18a18393
VP
3770 /* Continuable hardware watchpoints are treated as non-existent if the
3771 reason we stopped wasn't a hardware watchpoint (we didn't stop on
3772 some data address). Otherwise gdb won't stop on a break instruction
3773 in the code (not from a breakpoint) when a hardware watchpoint has
3774 been defined. Also skip watchpoints which we know did not trigger
3775 (did not match the data address). */
cc60f2e3
PA
3776
3777 if (is_hardware_watchpoint (b)
18a18393
VP
3778 && b->watchpoint_triggered == watch_triggered_no)
3779 return 0;
3780
3781 if (b->type == bp_hardware_breakpoint)
3782 {
3783 if (bl->address != bp_addr)
3784 return 0;
3785 if (overlay_debugging /* unmapped overlay section */
35df4500 3786 && section_is_overlay (bl->section)
18a18393
VP
3787 && !section_is_mapped (bl->section))
3788 return 0;
3789 }
ce78b96d 3790
ce78b96d
JB
3791 if (b->type == bp_catchpoint)
3792 {
3793 gdb_assert (b->ops != NULL && b->ops->breakpoint_hit != NULL);
3794 if (!b->ops->breakpoint_hit (b))
3795 return 0;
3796 }
3797
18a18393
VP
3798 return 1;
3799}
3800
3801/* If BS refers to a watchpoint, determine if the watched values
3802 has actually changed, and we should stop. If not, set BS->stop
3803 to 0. */
3804static void
3805bpstat_check_watchpoint (bpstat bs)
3806{
2bdf28a0
JK
3807 const struct bp_location *bl;
3808 struct breakpoint *b;
3809
3810 /* BS is built for existing struct breakpoint. */
f431efe5 3811 bl = bs->bp_location_at;
2bdf28a0 3812 gdb_assert (bl != NULL);
f431efe5 3813 b = bs->breakpoint_at;
2bdf28a0 3814 gdb_assert (b != NULL);
18a18393 3815
cc60f2e3 3816 if (is_watchpoint (b))
18a18393 3817 {
18a18393
VP
3818 int must_check_value = 0;
3819
3820 if (b->type == bp_watchpoint)
3821 /* For a software watchpoint, we must always check the
3822 watched value. */
3823 must_check_value = 1;
3824 else if (b->watchpoint_triggered == watch_triggered_yes)
3825 /* We have a hardware watchpoint (read, write, or access)
3826 and the target earlier reported an address watched by
3827 this watchpoint. */
3828 must_check_value = 1;
3829 else if (b->watchpoint_triggered == watch_triggered_unknown
3830 && b->type == bp_hardware_watchpoint)
3831 /* We were stopped by a hardware watchpoint, but the target could
3832 not report the data address. We must check the watchpoint's
3833 value. Access and read watchpoints are out of luck; without
3834 a data address, we can't figure it out. */
3835 must_check_value = 1;
3836
3837 if (must_check_value)
3838 {
3839 char *message = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3840 b->number);
3841 struct cleanup *cleanups = make_cleanup (xfree, message);
3842 int e = catch_errors (watchpoint_check, bs, message,
3843 RETURN_MASK_ALL);
3844 do_cleanups (cleanups);
3845 switch (e)
3846 {
3847 case WP_DELETED:
3848 /* We've already printed what needs to be printed. */
3849 bs->print_it = print_it_done;
3850 /* Stop. */
3851 break;
60e1c644
PA
3852 case WP_IGNORE:
3853 bs->print_it = print_it_noop;
3854 bs->stop = 0;
3855 break;
18a18393
VP
3856 case WP_VALUE_CHANGED:
3857 if (b->type == bp_read_watchpoint)
3858 {
85d721b8
PA
3859 /* There are two cases to consider here:
3860
3861 1. we're watching the triggered memory for reads.
3862 In that case, trust the target, and always report
3863 the watchpoint hit to the user. Even though
3864 reads don't cause value changes, the value may
3865 have changed since the last time it was read, and
3866 since we're not trapping writes, we will not see
3867 those, and as such we should ignore our notion of
3868 old value.
3869
3870 2. we're watching the triggered memory for both
3871 reads and writes. There are two ways this may
3872 happen:
3873
3874 2.1. this is a target that can't break on data
3875 reads only, but can break on accesses (reads or
3876 writes), such as e.g., x86. We detect this case
3877 at the time we try to insert read watchpoints.
3878
3879 2.2. otherwise, the target supports read
3880 watchpoints, but, the user set an access or write
3881 watchpoint watching the same memory as this read
3882 watchpoint.
3883
3884 If we're watching memory writes as well as reads,
3885 ignore watchpoint hits when we find that the
3886 value hasn't changed, as reads don't cause
3887 changes. This still gives false positives when
3888 the program writes the same value to memory as
3889 what there was already in memory (we will confuse
3890 it for a read), but it's much better than
3891 nothing. */
3892
3893 int other_write_watchpoint = 0;
3894
3895 if (bl->watchpoint_type == hw_read)
3896 {
3897 struct breakpoint *other_b;
3898
3899 ALL_BREAKPOINTS (other_b)
3900 if ((other_b->type == bp_hardware_watchpoint
3901 || other_b->type == bp_access_watchpoint)
3902 && (other_b->watchpoint_triggered
3903 == watch_triggered_yes))
3904 {
3905 other_write_watchpoint = 1;
3906 break;
3907 }
3908 }
3909
3910 if (other_write_watchpoint
3911 || bl->watchpoint_type == hw_access)
3912 {
3913 /* We're watching the same memory for writes,
3914 and the value changed since the last time we
3915 updated it, so this trap must be for a write.
3916 Ignore it. */
3917 bs->print_it = print_it_noop;
3918 bs->stop = 0;
3919 }
18a18393
VP
3920 }
3921 break;
3922 case WP_VALUE_NOT_CHANGED:
3923 if (b->type == bp_hardware_watchpoint
3924 || b->type == bp_watchpoint)
3925 {
3926 /* Don't stop: write watchpoints shouldn't fire if
3927 the value hasn't changed. */
3928 bs->print_it = print_it_noop;
3929 bs->stop = 0;
3930 }
3931 /* Stop. */
3932 break;
3933 default:
3934 /* Can't happen. */
3935 case 0:
3936 /* Error from catch_errors. */
3937 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
3938 if (b->related_breakpoint)
3939 b->related_breakpoint->disposition = disp_del_at_next_stop;
3940 b->disposition = disp_del_at_next_stop;
3941 /* We've already printed what needs to be printed. */
3942 bs->print_it = print_it_done;
3943 break;
3944 }
3945 }
3946 else /* must_check_value == 0 */
3947 {
3948 /* This is a case where some watchpoint(s) triggered, but
3949 not at the address of this watchpoint, or else no
3950 watchpoint triggered after all. So don't print
3951 anything for this watchpoint. */
3952 bs->print_it = print_it_noop;
3953 bs->stop = 0;
3954 }
3955 }
3956}
3957
3958
3959/* Check conditions (condition proper, frame, thread and ignore count)
3960 of breakpoint referred to by BS. If we should not stop for this
3961 breakpoint, set BS->stop to 0. */
f431efe5 3962
18a18393
VP
3963static void
3964bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3965{
3966 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
3967 const struct bp_location *bl;
3968 struct breakpoint *b;
3969
3970 /* BS is built for existing struct breakpoint. */
f431efe5 3971 bl = bs->bp_location_at;
2bdf28a0 3972 gdb_assert (bl != NULL);
f431efe5 3973 b = bs->breakpoint_at;
2bdf28a0 3974 gdb_assert (b != NULL);
18a18393
VP
3975
3976 if (frame_id_p (b->frame_id)
edb3359d 3977 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
3978 bs->stop = 0;
3979 else if (bs->stop)
3980 {
3981 int value_is_zero = 0;
60e1c644
PA
3982 struct expression *cond;
3983
60e1c644
PA
3984 if (is_watchpoint (b))
3985 cond = b->cond_exp;
3986 else
3987 cond = bl->cond;
3988
f431efe5 3989 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 3990 {
60e1c644
PA
3991 int within_current_scope = 1;
3992
c5bc3a77
DJ
3993 /* We use value_mark and value_free_to_mark because it could
3994 be a long time before we return to the command level and
3995 call free_all_values. We can't call free_all_values
3996 because we might be in the middle of evaluating a
3997 function call. */
3998 struct value *mark = value_mark ();
3999
edb3359d
DJ
4000 /* Need to select the frame, with all that implies so that
4001 the conditions will have the right context. Because we
4002 use the frame, we will not see an inlined function's
4003 variables when we arrive at a breakpoint at the start
4004 of the inlined function; the current frame will be the
4005 call site. */
60e1c644
PA
4006 if (!is_watchpoint (b) || b->cond_exp_valid_block == NULL)
4007 select_frame (get_current_frame ());
4008 else
4009 {
4010 struct frame_info *frame;
4011
4012 /* For local watchpoint expressions, which particular
4013 instance of a local is being watched matters, so we
4014 keep track of the frame to evaluate the expression
4015 in. To evaluate the condition however, it doesn't
4016 really matter which instantiation of the function
4017 where the condition makes sense triggers the
4018 watchpoint. This allows an expression like "watch
4019 global if q > 10" set in `func', catch writes to
4020 global on all threads that call `func', or catch
4021 writes on all recursive calls of `func' by a single
4022 thread. We simply always evaluate the condition in
4023 the innermost frame that's executing where it makes
4024 sense to evaluate the condition. It seems
4025 intuitive. */
4026 frame = block_innermost_frame (b->cond_exp_valid_block);
4027 if (frame != NULL)
4028 select_frame (frame);
4029 else
4030 within_current_scope = 0;
4031 }
4032 if (within_current_scope)
4033 value_is_zero
4034 = catch_errors (breakpoint_cond_eval, cond,
4035 "Error in testing breakpoint condition:\n",
4036 RETURN_MASK_ALL);
4037 else
4038 {
4039 warning (_("Watchpoint condition cannot be tested "
4040 "in the current scope"));
4041 /* If we failed to set the right context for this
4042 watchpoint, unconditionally report it. */
4043 value_is_zero = 0;
4044 }
18a18393 4045 /* FIXME-someday, should give breakpoint # */
c5bc3a77 4046 value_free_to_mark (mark);
18a18393 4047 }
60e1c644
PA
4048
4049 if (cond && value_is_zero)
18a18393
VP
4050 {
4051 bs->stop = 0;
4052 }
4053 else if (b->thread != -1 && b->thread != thread_id)
4054 {
4055 bs->stop = 0;
4056 }
4057 else if (b->ignore_count > 0)
4058 {
4059 b->ignore_count--;
4060 annotate_ignore_count_change ();
4061 bs->stop = 0;
4062 /* Increase the hit count even though we don't
4063 stop. */
4064 ++(b->hit_count);
4065 }
4066 }
4067}
4068
4069
9709f61c 4070/* Get a bpstat associated with having just stopped at address
d983da9c 4071 BP_ADDR in thread PTID.
c906108c 4072
d983da9c 4073 Determine whether we stopped at a breakpoint, etc, or whether we
c906108c
SS
4074 don't understand this stop. Result is a chain of bpstat's such that:
4075
c5aa993b 4076 if we don't understand the stop, the result is a null pointer.
c906108c 4077
c5aa993b 4078 if we understand why we stopped, the result is not null.
c906108c 4079
c5aa993b
JM
4080 Each element of the chain refers to a particular breakpoint or
4081 watchpoint at which we have stopped. (We may have stopped for
4082 several reasons concurrently.)
c906108c 4083
c5aa993b
JM
4084 Each element of the chain has valid next, breakpoint_at,
4085 commands, FIXME??? fields. */
c906108c
SS
4086
4087bpstat
6c95b8df
PA
4088bpstat_stop_status (struct address_space *aspace,
4089 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4090{
0d381245 4091 struct breakpoint *b = NULL;
afe38095 4092 struct bp_location *bl;
20874c92 4093 struct bp_location *loc;
5760d0ab
JK
4094 /* First item of allocated bpstat's. */
4095 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4096 /* Pointer to the last thing in the chain currently. */
5760d0ab 4097 bpstat bs;
20874c92 4098 int ix;
429374b8 4099 int need_remove_insert;
f431efe5 4100 int removed_any;
c906108c 4101
f431efe5
PA
4102 /* First, build the bpstat chain with locations that explain a
4103 target stop, while being careful to not set the target running,
4104 as that may invalidate locations (in particular watchpoint
4105 locations are recreated). Resuming will happen here with
4106 breakpoint conditions or watchpoint expressions that include
4107 inferior function calls. */
c5aa993b 4108
429374b8
JK
4109 ALL_BREAKPOINTS (b)
4110 {
4111 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4112 continue;
a5606eee 4113
429374b8
JK
4114 for (bl = b->loc; bl != NULL; bl = bl->next)
4115 {
4116 /* For hardware watchpoints, we look only at the first location.
cc60f2e3
PA
4117 The watchpoint_check function will work on the entire expression,
4118 not the individual locations. For read watchpoints, the
4119 watchpoints_triggered function has checked all locations
429374b8
JK
4120 already. */
4121 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4122 break;
18a18393 4123
429374b8
JK
4124 if (bl->shlib_disabled)
4125 continue;
c5aa993b 4126
429374b8
JK
4127 if (!bpstat_check_location (bl, aspace, bp_addr))
4128 continue;
c5aa993b 4129
429374b8 4130 /* Come here if it's a watchpoint, or if the break address matches */
c5aa993b 4131
5760d0ab 4132 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to explain stop */
c5aa993b 4133
f431efe5
PA
4134 /* Assume we stop. Should we find a watchpoint that is not
4135 actually triggered, or if the condition of the breakpoint
4136 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4137 bs->stop = 1;
4138 bs->print = 1;
d983da9c 4139
f431efe5
PA
4140 /* If this is a scope breakpoint, mark the associated
4141 watchpoint as triggered so that we will handle the
4142 out-of-scope event. We'll get to the watchpoint next
4143 iteration. */
4144 if (b->type == bp_watchpoint_scope)
4145 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
4146 }
4147 }
4148
4149 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4150 {
4151 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
4152 aspace, bp_addr))
4153 {
5760d0ab 4154 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4155 /* For hits of moribund locations, we should just proceed. */
4156 bs->stop = 0;
4157 bs->print = 0;
4158 bs->print_it = print_it_noop;
4159 }
4160 }
4161
f431efe5
PA
4162 /* Now go through the locations that caused the target to stop, and
4163 check whether we're interested in reporting this stop to higher
4164 layers, or whether we should resume the target transparently. */
4165
4166 removed_any = 0;
4167
5760d0ab 4168 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4169 {
4170 if (!bs->stop)
4171 continue;
4172
4173 bpstat_check_watchpoint (bs);
4174 if (!bs->stop)
4175 continue;
4176
4177 b = bs->breakpoint_at;
18a18393 4178
429374b8 4179 if (b->type == bp_thread_event || b->type == bp_overlay_event
aa7d318d 4180 || b->type == bp_longjmp_master
186c406b
TT
4181 || b->type == bp_std_terminate_master
4182 || b->type == bp_exception_master)
429374b8
JK
4183 /* We do not stop for these. */
4184 bs->stop = 0;
4185 else
4186 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4187
429374b8
JK
4188 if (bs->stop)
4189 {
4190 ++(b->hit_count);
c906108c 4191
429374b8
JK
4192 /* We will stop here */
4193 if (b->disposition == disp_disable)
4194 {
4195 if (b->enable_state != bp_permanent)
4196 b->enable_state = bp_disabled;
f431efe5 4197 removed_any = 1;
429374b8
JK
4198 }
4199 if (b->silent)
4200 bs->print = 0;
4201 bs->commands = b->commands;
9add0f1b
TT
4202 incref_counted_command_line (bs->commands);
4203 bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4204 if (bs->commands_left
4205 && (strcmp ("silent", bs->commands_left->line) == 0
4206 || (xdb_commands
4207 && strcmp ("Q",
4208 bs->commands_left->line) == 0)))
429374b8 4209 {
9add0f1b 4210 bs->commands_left = bs->commands_left->next;
429374b8
JK
4211 bs->print = 0;
4212 }
429374b8
JK
4213 }
4214
4215 /* Print nothing for this entry if we dont stop or dont print. */
4216 if (bs->stop == 0 || bs->print == 0)
4217 bs->print_it = print_it_noop;
429374b8 4218 }
876fa593 4219
d983da9c
DJ
4220 /* If we aren't stopping, the value of some hardware watchpoint may
4221 not have changed, but the intermediate memory locations we are
4222 watching may have. Don't bother if we're stopping; this will get
4223 done later. */
d832cb68 4224 need_remove_insert = 0;
5760d0ab
JK
4225 if (! bpstat_causes_stop (bs_head))
4226 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4227 if (!bs->stop
f431efe5
PA
4228 && bs->breakpoint_at
4229 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4230 {
f431efe5 4231 update_watchpoint (bs->breakpoint_at, 0 /* don't reparse. */);
d832cb68 4232 need_remove_insert = 1;
d983da9c
DJ
4233 }
4234
d832cb68 4235 if (need_remove_insert)
2d134ed3 4236 update_global_location_list (1);
f431efe5
PA
4237 else if (removed_any)
4238 update_global_location_list (0);
d832cb68 4239
5760d0ab 4240 return bs_head;
c906108c 4241}
628fe4e4
JK
4242
4243static void
4244handle_jit_event (void)
4245{
4246 struct frame_info *frame;
4247 struct gdbarch *gdbarch;
4248
4249 /* Switch terminal for any messages produced by
4250 breakpoint_re_set. */
4251 target_terminal_ours_for_output ();
4252
4253 frame = get_current_frame ();
4254 gdbarch = get_frame_arch (frame);
4255
4256 jit_event_handler (gdbarch);
4257
4258 target_terminal_inferior ();
4259}
4260
4261/* Prepare WHAT final decision for infrun. */
4262
4263/* Decide what infrun needs to do with this bpstat. */
4264
c906108c 4265struct bpstat_what
fba45db2 4266bpstat_what (bpstat bs)
c906108c 4267{
c906108c 4268 struct bpstat_what retval;
628fe4e4
JK
4269 /* We need to defer calling `solib_add', as adding new symbols
4270 resets breakpoints, which in turn deletes breakpoint locations,
4271 and hence may clear unprocessed entries in the BS chain. */
4272 int shlib_event = 0;
4273 int jit_event = 0;
c906108c 4274
628fe4e4 4275 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4276 retval.call_dummy = STOP_NONE;
186c406b 4277 retval.is_longjmp = 0;
628fe4e4 4278
c906108c
SS
4279 for (; bs != NULL; bs = bs->next)
4280 {
628fe4e4
JK
4281 /* Extract this BS's action. After processing each BS, we check
4282 if its action overrides all we've seem so far. */
4283 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4284 enum bptype bptype;
4285
c906108c 4286 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4287 {
4288 /* I suspect this can happen if it was a momentary
4289 breakpoint which has since been deleted. */
4290 bptype = bp_none;
4291 }
f431efe5 4292 else if (bs->breakpoint_at == NULL)
628fe4e4 4293 bptype = bp_none;
20874c92 4294 else
f431efe5 4295 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4296
4297 switch (bptype)
c906108c
SS
4298 {
4299 case bp_none:
628fe4e4 4300 break;
c906108c
SS
4301 case bp_breakpoint:
4302 case bp_hardware_breakpoint:
4303 case bp_until:
4304 case bp_finish:
4305 if (bs->stop)
4306 {
4307 if (bs->print)
628fe4e4 4308 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4309 else
628fe4e4 4310 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4311 }
4312 else
628fe4e4 4313 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4314 break;
4315 case bp_watchpoint:
4316 case bp_hardware_watchpoint:
4317 case bp_read_watchpoint:
4318 case bp_access_watchpoint:
4319 if (bs->stop)
4320 {
4321 if (bs->print)
628fe4e4 4322 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4323 else
628fe4e4 4324 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4325 }
4326 else
628fe4e4
JK
4327 {
4328 /* There was a watchpoint, but we're not stopping.
4329 This requires no further action. */
4330 }
c906108c
SS
4331 break;
4332 case bp_longjmp:
186c406b 4333 case bp_exception:
628fe4e4 4334 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4335 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4336 break;
4337 case bp_longjmp_resume:
186c406b 4338 case bp_exception_resume:
628fe4e4 4339 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4340 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4341 break;
4342 case bp_step_resume:
4343 if (bs->stop)
628fe4e4
JK
4344 this_action = BPSTAT_WHAT_STEP_RESUME;
4345 else
c906108c 4346 {
628fe4e4
JK
4347 /* It is for the wrong frame. */
4348 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4349 }
c906108c 4350 break;
c906108c 4351 case bp_watchpoint_scope:
c4093a6a 4352 case bp_thread_event:
1900040c 4353 case bp_overlay_event:
0fd8e87f 4354 case bp_longjmp_master:
aa7d318d 4355 case bp_std_terminate_master:
186c406b 4356 case bp_exception_master:
628fe4e4 4357 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4358 break;
ce78b96d 4359 case bp_catchpoint:
c5aa993b
JM
4360 if (bs->stop)
4361 {
4362 if (bs->print)
628fe4e4 4363 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4364 else
628fe4e4 4365 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4366 }
4367 else
628fe4e4
JK
4368 {
4369 /* There was a catchpoint, but we're not stopping.
4370 This requires no further action. */
4371 }
4372 break;
4373 case bp_shlib_event:
4374 shlib_event = 1;
4375
4376 /* If requested, stop when the dynamic linker notifies GDB
4377 of events. This allows the user to get control and place
4378 breakpoints in initializer routines for dynamically
4379 loaded objects (among other things). */
4380 if (stop_on_solib_events)
4381 this_action = BPSTAT_WHAT_STOP_NOISY;
4382 else
4383 this_action = BPSTAT_WHAT_SINGLE;
4384 break;
4385 case bp_jit_event:
4386 jit_event = 1;
4387 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4388 break;
c906108c 4389 case bp_call_dummy:
53a5351d
JM
4390 /* Make sure the action is stop (silent or noisy),
4391 so infrun.c pops the dummy frame. */
aa7d318d 4392 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4393 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4394 break;
4395 case bp_std_terminate:
4396 /* Make sure the action is stop (silent or noisy),
4397 so infrun.c pops the dummy frame. */
aa7d318d 4398 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4399 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4400 break;
1042e4c0 4401 case bp_tracepoint:
7a697b8d 4402 case bp_fast_tracepoint:
0fb4aa4b 4403 case bp_static_tracepoint:
1042e4c0
SS
4404 /* Tracepoint hits should not be reported back to GDB, and
4405 if one got through somehow, it should have been filtered
4406 out already. */
4407 internal_error (__FILE__, __LINE__,
7a697b8d 4408 _("bpstat_what: tracepoint encountered"));
628fe4e4
JK
4409 default:
4410 internal_error (__FILE__, __LINE__,
4411 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4412 }
628fe4e4
JK
4413
4414 retval.main_action = max (retval.main_action, this_action);
c906108c 4415 }
628fe4e4
JK
4416
4417 if (shlib_event)
4418 {
4419 if (debug_infrun)
4420 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4421
4422 /* Check for any newly added shared libraries if we're supposed
4423 to be adding them automatically. */
4424
4425 /* Switch terminal for any messages produced by
4426 breakpoint_re_set. */
4427 target_terminal_ours_for_output ();
4428
4429#ifdef SOLIB_ADD
4430 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4431#else
4432 solib_add (NULL, 0, &current_target, auto_solib_add);
4433#endif
4434
4435 target_terminal_inferior ();
4436 }
4437
4438 if (jit_event)
4439 {
4440 if (debug_infrun)
4441 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4442
4443 handle_jit_event ();
4444 }
4445
c906108c
SS
4446 return retval;
4447}
4448
4449/* Nonzero if we should step constantly (e.g. watchpoints on machines
4450 without hardware support). This isn't related to a specific bpstat,
4451 just to things like whether watchpoints are set. */
4452
c5aa993b 4453int
fba45db2 4454bpstat_should_step (void)
c906108c
SS
4455{
4456 struct breakpoint *b;
cc59ec59 4457
c906108c 4458 ALL_BREAKPOINTS (b)
717a8278 4459 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4460 return 1;
c906108c
SS
4461 return 0;
4462}
4463
67822962
PA
4464int
4465bpstat_causes_stop (bpstat bs)
4466{
4467 for (; bs != NULL; bs = bs->next)
4468 if (bs->stop)
4469 return 1;
4470
4471 return 0;
4472}
4473
c906108c 4474\f
c5aa993b 4475
859825b8
JK
4476/* Print the LOC location out of the list of B->LOC locations. */
4477
0d381245
VP
4478static void print_breakpoint_location (struct breakpoint *b,
4479 struct bp_location *loc,
4480 char *wrap_indent,
4481 struct ui_stream *stb)
4482{
6c95b8df
PA
4483 struct cleanup *old_chain = save_current_program_space ();
4484
859825b8
JK
4485 if (loc != NULL && loc->shlib_disabled)
4486 loc = NULL;
4487
6c95b8df
PA
4488 if (loc != NULL)
4489 set_current_program_space (loc->pspace);
4490
859825b8 4491 if (b->source_file && loc)
0d381245
VP
4492 {
4493 struct symbol *sym
4494 = find_pc_sect_function (loc->address, loc->section);
4495 if (sym)
4496 {
4497 ui_out_text (uiout, "in ");
4498 ui_out_field_string (uiout, "func",
4499 SYMBOL_PRINT_NAME (sym));
4500 ui_out_wrap_hint (uiout, wrap_indent);
4501 ui_out_text (uiout, " at ");
4502 }
4503 ui_out_field_string (uiout, "file", b->source_file);
4504 ui_out_text (uiout, ":");
4505
4506 if (ui_out_is_mi_like_p (uiout))
4507 {
4508 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4509 char *fullname = symtab_to_fullname (sal.symtab);
4510
4511 if (fullname)
4512 ui_out_field_string (uiout, "fullname", fullname);
4513 }
4514
4515 ui_out_field_int (uiout, "line", b->line_number);
4516 }
859825b8 4517 else if (loc)
0d381245 4518 {
22e722e1
DJ
4519 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4520 demangle, "");
0d381245
VP
4521 ui_out_field_stream (uiout, "at", stb);
4522 }
859825b8
JK
4523 else
4524 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4525
4526 do_cleanups (old_chain);
0d381245
VP
4527}
4528
269b11a2
PA
4529static const char *
4530bptype_string (enum bptype type)
c906108c 4531{
c4093a6a
JM
4532 struct ep_type_description
4533 {
4534 enum bptype type;
4535 char *description;
4536 };
4537 static struct ep_type_description bptypes[] =
c906108c 4538 {
c5aa993b
JM
4539 {bp_none, "?deleted?"},
4540 {bp_breakpoint, "breakpoint"},
c906108c 4541 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4542 {bp_until, "until"},
4543 {bp_finish, "finish"},
4544 {bp_watchpoint, "watchpoint"},
c906108c 4545 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4546 {bp_read_watchpoint, "read watchpoint"},
4547 {bp_access_watchpoint, "acc watchpoint"},
4548 {bp_longjmp, "longjmp"},
4549 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4550 {bp_exception, "exception"},
4551 {bp_exception_resume, "exception resume"},
c5aa993b 4552 {bp_step_resume, "step resume"},
c5aa993b
JM
4553 {bp_watchpoint_scope, "watchpoint scope"},
4554 {bp_call_dummy, "call dummy"},
aa7d318d 4555 {bp_std_terminate, "std::terminate"},
c5aa993b 4556 {bp_shlib_event, "shlib events"},
c4093a6a 4557 {bp_thread_event, "thread events"},
1900040c 4558 {bp_overlay_event, "overlay events"},
0fd8e87f 4559 {bp_longjmp_master, "longjmp master"},
aa7d318d 4560 {bp_std_terminate_master, "std::terminate master"},
186c406b 4561 {bp_exception_master, "exception master"},
ce78b96d 4562 {bp_catchpoint, "catchpoint"},
1042e4c0 4563 {bp_tracepoint, "tracepoint"},
7a697b8d 4564 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4565 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4566 {bp_jit_event, "jit events"},
c5aa993b 4567 };
269b11a2
PA
4568
4569 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4570 || ((int) type != bptypes[(int) type].type))
4571 internal_error (__FILE__, __LINE__,
4572 _("bptypes table does not describe type #%d."),
4573 (int) type);
4574
4575 return bptypes[(int) type].description;
4576}
4577
4578/* Print B to gdb_stdout. */
4579
4580static void
4581print_one_breakpoint_location (struct breakpoint *b,
4582 struct bp_location *loc,
4583 int loc_number,
4584 struct bp_location **last_loc,
4585 int print_address_bits,
4586 int allflag)
4587{
4588 struct command_line *l;
c2c6d25f 4589 static char bpenables[] = "nynny";
c906108c 4590 char wrap_indent[80];
8b93c638
JM
4591 struct ui_stream *stb = ui_out_stream_new (uiout);
4592 struct cleanup *old_chain = make_cleanup_ui_out_stream_delete (stb);
3b31d625 4593 struct cleanup *bkpt_chain;
c906108c 4594
0d381245
VP
4595 int header_of_multiple = 0;
4596 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4597 struct value_print_options opts;
4598
4599 get_user_print_options (&opts);
0d381245
VP
4600
4601 gdb_assert (!loc || loc_number != 0);
4602 /* See comment in print_one_breakpoint concerning
4603 treatment of breakpoints with single disabled
4604 location. */
4605 if (loc == NULL
4606 && (b->loc != NULL
4607 && (b->loc->next != NULL || !b->loc->enabled)))
4608 header_of_multiple = 1;
4609 if (loc == NULL)
4610 loc = b->loc;
4611
c4093a6a 4612 annotate_record ();
3b31d625 4613 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
c4093a6a
JM
4614
4615 /* 1 */
4616 annotate_field (0);
0d381245
VP
4617 if (part_of_multiple)
4618 {
4619 char *formatted;
0c6773c1 4620 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4621 ui_out_field_string (uiout, "number", formatted);
4622 xfree (formatted);
4623 }
4624 else
4625 {
4626 ui_out_field_int (uiout, "number", b->number);
4627 }
c4093a6a
JM
4628
4629 /* 2 */
4630 annotate_field (1);
0d381245
VP
4631 if (part_of_multiple)
4632 ui_out_field_skip (uiout, "type");
269b11a2
PA
4633 else
4634 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4635
4636 /* 3 */
4637 annotate_field (2);
0d381245
VP
4638 if (part_of_multiple)
4639 ui_out_field_skip (uiout, "disp");
4640 else
2cec12e5 4641 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4642
c4093a6a
JM
4643
4644 /* 4 */
4645 annotate_field (3);
0d381245 4646 if (part_of_multiple)
54e52265 4647 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4648 else
54e52265
VP
4649 ui_out_field_fmt (uiout, "enabled", "%c",
4650 bpenables[(int) b->enable_state]);
4651 ui_out_spaces (uiout, 2);
0d381245 4652
c4093a6a
JM
4653
4654 /* 5 and 6 */
4655 strcpy (wrap_indent, " ");
79a45b7d 4656 if (opts.addressprint)
75ac9d7b 4657 {
a6d9a66e 4658 if (print_address_bits <= 32)
75ac9d7b
MS
4659 strcat (wrap_indent, " ");
4660 else
4661 strcat (wrap_indent, " ");
4662 }
c906108c 4663
3086aeae 4664 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245
VP
4665 {
4666 /* Although the print_one can possibly print
4667 all locations, calling it here is not likely
4668 to get any nice result. So, make sure there's
4669 just one location. */
4670 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4671 b->ops->print_one (b, last_loc);
0d381245 4672 }
3086aeae
DJ
4673 else
4674 switch (b->type)
4675 {
4676 case bp_none:
4677 internal_error (__FILE__, __LINE__,
e2e0b3e5 4678 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4679 break;
c906108c 4680
3086aeae
DJ
4681 case bp_watchpoint:
4682 case bp_hardware_watchpoint:
4683 case bp_read_watchpoint:
4684 case bp_access_watchpoint:
4685 /* Field 4, the address, is omitted (which makes the columns
4686 not line up too nicely with the headers, but the effect
4687 is relatively readable). */
79a45b7d 4688 if (opts.addressprint)
3086aeae
DJ
4689 ui_out_field_skip (uiout, "addr");
4690 annotate_field (5);
fa8a61dc 4691 ui_out_field_string (uiout, "what", b->exp_string);
3086aeae
DJ
4692 break;
4693
3086aeae
DJ
4694 case bp_breakpoint:
4695 case bp_hardware_breakpoint:
4696 case bp_until:
4697 case bp_finish:
4698 case bp_longjmp:
4699 case bp_longjmp_resume:
186c406b
TT
4700 case bp_exception:
4701 case bp_exception_resume:
3086aeae 4702 case bp_step_resume:
3086aeae
DJ
4703 case bp_watchpoint_scope:
4704 case bp_call_dummy:
aa7d318d 4705 case bp_std_terminate:
3086aeae
DJ
4706 case bp_shlib_event:
4707 case bp_thread_event:
4708 case bp_overlay_event:
0fd8e87f 4709 case bp_longjmp_master:
aa7d318d 4710 case bp_std_terminate_master:
186c406b 4711 case bp_exception_master:
1042e4c0 4712 case bp_tracepoint:
7a697b8d 4713 case bp_fast_tracepoint:
0fb4aa4b 4714 case bp_static_tracepoint:
4efc6507 4715 case bp_jit_event:
79a45b7d 4716 if (opts.addressprint)
3086aeae
DJ
4717 {
4718 annotate_field (4);
54e52265 4719 if (header_of_multiple)
0d381245 4720 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4721 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4722 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4723 else
5af949e3
UW
4724 ui_out_field_core_addr (uiout, "addr",
4725 loc->gdbarch, loc->address);
3086aeae
DJ
4726 }
4727 annotate_field (5);
0d381245
VP
4728 if (!header_of_multiple)
4729 print_breakpoint_location (b, loc, wrap_indent, stb);
4730 if (b->loc)
a6d9a66e 4731 *last_loc = b->loc;
3086aeae
DJ
4732 break;
4733 }
c906108c 4734
6c95b8df
PA
4735
4736 /* For backward compatibility, don't display inferiors unless there
4737 are several. */
4738 if (loc != NULL
4739 && !header_of_multiple
4740 && (allflag
4741 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4742 && (number_of_program_spaces () > 1
4743 || number_of_inferiors () > 1)
2bdf28a0
JK
4744 /* LOC is for existing B, it cannot be in moribund_locations and
4745 thus having NULL OWNER. */
6c95b8df
PA
4746 && loc->owner->type != bp_catchpoint)))
4747 {
4748 struct inferior *inf;
4749 int first = 1;
4750
4751 for (inf = inferior_list; inf != NULL; inf = inf->next)
4752 {
4753 if (inf->pspace == loc->pspace)
4754 {
4755 if (first)
4756 {
4757 first = 0;
4758 ui_out_text (uiout, " inf ");
4759 }
4760 else
4761 ui_out_text (uiout, ", ");
4762 ui_out_text (uiout, plongest (inf->num));
4763 }
4764 }
4765 }
4766
4a306c9a 4767 if (!part_of_multiple)
c4093a6a 4768 {
4a306c9a
JB
4769 if (b->thread != -1)
4770 {
4771 /* FIXME: This seems to be redundant and lost here; see the
4772 "stop only in" line a little further down. */
4773 ui_out_text (uiout, " thread ");
4774 ui_out_field_int (uiout, "thread", b->thread);
4775 }
4776 else if (b->task != 0)
4777 {
4778 ui_out_text (uiout, " task ");
4779 ui_out_field_int (uiout, "task", b->task);
4780 }
c4093a6a
JM
4781 }
4782
8b93c638 4783 ui_out_text (uiout, "\n");
c4093a6a 4784
0fb4aa4b
PA
4785 if (!part_of_multiple && b->static_trace_marker_id)
4786 {
4787 gdb_assert (b->type == bp_static_tracepoint);
4788
4789 ui_out_text (uiout, "\tmarker id is ");
4790 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
4791 b->static_trace_marker_id);
4792 ui_out_text (uiout, "\n");
4793 }
4794
0d381245 4795 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4796 {
4797 annotate_field (6);
8b93c638 4798 ui_out_text (uiout, "\tstop only in stack frame at ");
818dd999
AC
4799 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
4800 the frame ID. */
5af949e3
UW
4801 ui_out_field_core_addr (uiout, "frame",
4802 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4803 ui_out_text (uiout, "\n");
c4093a6a
JM
4804 }
4805
0d381245 4806 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
c4093a6a 4807 {
f7f9143b
JB
4808 /* We do not print the condition for Ada exception catchpoints
4809 because the condition is an internal implementation detail
4810 that we do not want to expose to the user. */
c4093a6a 4811 annotate_field (7);
d77f58be 4812 if (is_tracepoint (b))
1042e4c0
SS
4813 ui_out_text (uiout, "\ttrace only if ");
4814 else
4815 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4816 ui_out_field_string (uiout, "cond", b->cond_string);
4817 ui_out_text (uiout, "\n");
4818 }
4819
0d381245 4820 if (!part_of_multiple && b->thread != -1)
c4093a6a
JM
4821 {
4822 /* FIXME should make an annotation for this */
8b93c638
JM
4823 ui_out_text (uiout, "\tstop only in thread ");
4824 ui_out_field_int (uiout, "thread", b->thread);
4825 ui_out_text (uiout, "\n");
c4093a6a
JM
4826 }
4827
63c715c6 4828 if (!part_of_multiple && b->hit_count)
c4093a6a
JM
4829 {
4830 /* FIXME should make an annotation for this */
8b93c638
JM
4831 if (ep_is_catchpoint (b))
4832 ui_out_text (uiout, "\tcatchpoint");
4833 else
4834 ui_out_text (uiout, "\tbreakpoint");
4835 ui_out_text (uiout, " already hit ");
4836 ui_out_field_int (uiout, "times", b->hit_count);
4837 if (b->hit_count == 1)
4838 ui_out_text (uiout, " time\n");
4839 else
4840 ui_out_text (uiout, " times\n");
c4093a6a
JM
4841 }
4842
fb40c209
AC
4843 /* Output the count also if it is zero, but only if this is
4844 mi. FIXME: Should have a better test for this. */
9dc5e2a9 4845 if (ui_out_is_mi_like_p (uiout))
63c715c6 4846 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4847 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4848
0d381245 4849 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4850 {
4851 annotate_field (8);
8b93c638
JM
4852 ui_out_text (uiout, "\tignore next ");
4853 ui_out_field_int (uiout, "ignore", b->ignore_count);
4854 ui_out_text (uiout, " hits\n");
c4093a6a 4855 }
059fb39f 4856
9add0f1b 4857 l = b->commands ? b->commands->commands : NULL;
059fb39f 4858 if (!part_of_multiple && l)
c4093a6a 4859 {
3b31d625
EZ
4860 struct cleanup *script_chain;
4861
c4093a6a 4862 annotate_field (9);
3b31d625 4863 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4864 print_command_lines (uiout, l, 4);
3b31d625 4865 do_cleanups (script_chain);
c4093a6a 4866 }
d24317b4 4867
1042e4c0
SS
4868 if (!part_of_multiple && b->pass_count)
4869 {
4870 annotate_field (10);
4871 ui_out_text (uiout, "\tpass count ");
4872 ui_out_field_int (uiout, "pass", b->pass_count);
4873 ui_out_text (uiout, " \n");
4874 }
4875
d24317b4
VP
4876 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4877 {
4878 if (b->addr_string)
4879 ui_out_field_string (uiout, "original-location", b->addr_string);
4880 else if (b->exp_string)
4881 ui_out_field_string (uiout, "original-location", b->exp_string);
4882 }
4883
3b31d625 4884 do_cleanups (bkpt_chain);
8b93c638 4885 do_cleanups (old_chain);
c4093a6a 4886}
c5aa993b 4887
0d381245
VP
4888static void
4889print_one_breakpoint (struct breakpoint *b,
6c95b8df
PA
4890 struct bp_location **last_loc, int print_address_bits,
4891 int allflag)
0d381245 4892{
6c95b8df
PA
4893 print_one_breakpoint_location (b, NULL, 0, last_loc,
4894 print_address_bits, allflag);
0d381245
VP
4895
4896 /* If this breakpoint has custom print function,
4897 it's already printed. Otherwise, print individual
4898 locations, if any. */
4899 if (b->ops == NULL || b->ops->print_one == NULL)
4900 {
4901 /* If breakpoint has a single location that is
4902 disabled, we print it as if it had
4903 several locations, since otherwise it's hard to
4904 represent "breakpoint enabled, location disabled"
a5606eee
VP
4905 situation.
4906 Note that while hardware watchpoints have
4907 several locations internally, that's no a property
4908 exposed to user. */
0d381245 4909 if (b->loc
a5606eee 4910 && !is_hardware_watchpoint (b)
0d381245 4911 && (b->loc->next || !b->loc->enabled)
a5606eee 4912 && !ui_out_is_mi_like_p (uiout))
0d381245
VP
4913 {
4914 struct bp_location *loc;
4915 int n = 1;
4916 for (loc = b->loc; loc; loc = loc->next, ++n)
a6d9a66e 4917 print_one_breakpoint_location (b, loc, n, last_loc,
6c95b8df 4918 print_address_bits, allflag);
0d381245
VP
4919 }
4920 }
4921}
4922
a6d9a66e
UW
4923static int
4924breakpoint_address_bits (struct breakpoint *b)
4925{
4926 int print_address_bits = 0;
4927 struct bp_location *loc;
4928
4929 for (loc = b->loc; loc; loc = loc->next)
4930 {
c7437ca6
PA
4931 int addr_bit;
4932
4933 /* Software watchpoints that aren't watching memory don't have
4934 an address to print. */
4935 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
4936 continue;
4937
4938 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
4939 if (addr_bit > print_address_bits)
4940 print_address_bits = addr_bit;
4941 }
4942
4943 return print_address_bits;
4944}
0d381245 4945
c4093a6a
JM
4946struct captured_breakpoint_query_args
4947 {
4948 int bnum;
4949 };
c5aa993b 4950
c4093a6a 4951static int
2b65245e 4952do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
4953{
4954 struct captured_breakpoint_query_args *args = data;
52f0bd74 4955 struct breakpoint *b;
a6d9a66e 4956 struct bp_location *dummy_loc = NULL;
cc59ec59 4957
c4093a6a
JM
4958 ALL_BREAKPOINTS (b)
4959 {
4960 if (args->bnum == b->number)
c5aa993b 4961 {
a6d9a66e 4962 int print_address_bits = breakpoint_address_bits (b);
cc59ec59 4963
6c95b8df 4964 print_one_breakpoint (b, &dummy_loc, print_address_bits, 0);
c4093a6a 4965 return GDB_RC_OK;
c5aa993b 4966 }
c4093a6a
JM
4967 }
4968 return GDB_RC_NONE;
4969}
c5aa993b 4970
c4093a6a 4971enum gdb_rc
ce43223b 4972gdb_breakpoint_query (struct ui_out *uiout, int bnum, char **error_message)
c4093a6a
JM
4973{
4974 struct captured_breakpoint_query_args args;
cc59ec59 4975
c4093a6a
JM
4976 args.bnum = bnum;
4977 /* For the moment we don't trust print_one_breakpoint() to not throw
4978 an error. */
b0b13bb4
DJ
4979 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
4980 error_message, RETURN_MASK_ALL) < 0)
4981 return GDB_RC_FAIL;
4982 else
4983 return GDB_RC_OK;
c4093a6a 4984}
c5aa993b 4985
7f3b0473
AC
4986/* Return non-zero if B is user settable (breakpoints, watchpoints,
4987 catchpoints, et.al.). */
4988
4989static int
4990user_settable_breakpoint (const struct breakpoint *b)
4991{
4992 return (b->type == bp_breakpoint
ce78b96d 4993 || b->type == bp_catchpoint
7f3b0473 4994 || b->type == bp_hardware_breakpoint
d77f58be 4995 || is_tracepoint (b)
cc60f2e3 4996 || is_watchpoint (b));
7f3b0473
AC
4997}
4998
4999/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5000 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5001 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5002 FILTER is non-NULL, call it on each breakpoint and only include the
5003 ones for which it returns non-zero. Return the total number of
5004 breakpoints listed. */
c906108c 5005
d77f58be
SS
5006static int
5007breakpoint_1 (int bnum, int allflag, int (*filter) (const struct breakpoint *))
c4093a6a 5008{
52f0bd74 5009 struct breakpoint *b;
a6d9a66e 5010 struct bp_location *last_loc = NULL;
7f3b0473 5011 int nr_printable_breakpoints;
3b31d625 5012 struct cleanup *bkpttbl_chain;
79a45b7d 5013 struct value_print_options opts;
a6d9a66e 5014 int print_address_bits = 0;
269b11a2
PA
5015 int print_type_col_width = 14;
5016
79a45b7d
TT
5017 get_user_print_options (&opts);
5018
a6d9a66e
UW
5019 /* Compute the number of rows in the table, as well as the
5020 size required for address fields. */
7f3b0473
AC
5021 nr_printable_breakpoints = 0;
5022 ALL_BREAKPOINTS (b)
5023 if (bnum == -1
5024 || bnum == b->number)
5025 {
d77f58be
SS
5026 /* If we have a filter, only list the breakpoints it accepts. */
5027 if (filter && !filter (b))
5028 continue;
5029
84f4c1fe
PM
5030 if (allflag || (user_settable_breakpoint (b)
5031 && b->number > 0))
a6d9a66e 5032 {
269b11a2
PA
5033 int addr_bit, type_len;
5034
5035 addr_bit = breakpoint_address_bits (b);
a6d9a66e
UW
5036 if (addr_bit > print_address_bits)
5037 print_address_bits = addr_bit;
5038
269b11a2
PA
5039 type_len = strlen (bptype_string (b->type));
5040 if (type_len > print_type_col_width)
5041 print_type_col_width = type_len;
5042
a6d9a66e
UW
5043 nr_printable_breakpoints++;
5044 }
7f3b0473
AC
5045 }
5046
79a45b7d 5047 if (opts.addressprint)
3b31d625
EZ
5048 bkpttbl_chain
5049 = make_cleanup_ui_out_table_begin_end (uiout, 6, nr_printable_breakpoints,
5050 "BreakpointTable");
8b93c638 5051 else
3b31d625
EZ
5052 bkpttbl_chain
5053 = make_cleanup_ui_out_table_begin_end (uiout, 5, nr_printable_breakpoints,
5054 "BreakpointTable");
8b93c638 5055
7f3b0473 5056 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5057 annotate_breakpoints_headers ();
5058 if (nr_printable_breakpoints > 0)
5059 annotate_field (0);
0d381245 5060 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5061 if (nr_printable_breakpoints > 0)
5062 annotate_field (1);
269b11a2
PA
5063 ui_out_table_header (uiout, print_type_col_width, ui_left,
5064 "type", "Type"); /* 2 */
d7faa9e7
AC
5065 if (nr_printable_breakpoints > 0)
5066 annotate_field (2);
5067 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
5068 if (nr_printable_breakpoints > 0)
5069 annotate_field (3);
54e52265 5070 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5071 if (opts.addressprint)
7f3b0473 5072 {
d7faa9e7
AC
5073 if (nr_printable_breakpoints > 0)
5074 annotate_field (4);
a6d9a66e 5075 if (print_address_bits <= 32)
b25959ec 5076 ui_out_table_header (uiout, 10, ui_left, "addr", "Address");/* 5 */
7f3b0473 5077 else
b25959ec 5078 ui_out_table_header (uiout, 18, ui_left, "addr", "Address");/* 5 */
7f3b0473 5079 }
d7faa9e7
AC
5080 if (nr_printable_breakpoints > 0)
5081 annotate_field (5);
5082 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5083 ui_out_table_body (uiout);
5084 if (nr_printable_breakpoints > 0)
5085 annotate_breakpoints_table ();
7f3b0473 5086
c4093a6a 5087 ALL_BREAKPOINTS (b)
bad56014
TT
5088 {
5089 QUIT;
c4093a6a
JM
5090 if (bnum == -1
5091 || bnum == b->number)
5092 {
d77f58be
SS
5093 /* If we have a filter, only list the breakpoints it accepts. */
5094 if (filter && !filter (b))
5095 continue;
5096
c4093a6a
JM
5097 /* We only print out user settable breakpoints unless the
5098 allflag is set. */
84f4c1fe
PM
5099 if (allflag || (user_settable_breakpoint (b)
5100 && b->number > 0))
6c95b8df 5101 print_one_breakpoint (b, &last_loc, print_address_bits, allflag);
c4093a6a 5102 }
bad56014 5103 }
c4093a6a 5104
3b31d625 5105 do_cleanups (bkpttbl_chain);
698384cd 5106
7f3b0473 5107 if (nr_printable_breakpoints == 0)
c906108c 5108 {
d77f58be
SS
5109 /* If there's a filter, let the caller decide how to report empty list. */
5110 if (!filter)
5111 {
5112 if (bnum == -1)
5113 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5114 else
5115 ui_out_message (uiout, 0, "No breakpoint or watchpoint number %d.\n",
5116 bnum);
5117 }
c906108c
SS
5118 }
5119 else
c4093a6a 5120 {
a6d9a66e
UW
5121 if (last_loc && !server_command)
5122 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5123 }
c906108c 5124
c4093a6a
JM
5125 /* FIXME? Should this be moved up so that it is only called when
5126 there have been breakpoints? */
c906108c 5127 annotate_breakpoints_table_end ();
d77f58be
SS
5128
5129 return nr_printable_breakpoints;
c906108c
SS
5130}
5131
ad443146
SS
5132/* Display the value of default-collect in a way that is generally
5133 compatible with the breakpoint list. */
5134
5135static void
5136default_collect_info (void)
5137{
5138 /* If it has no value (which is frequently the case), say nothing; a
5139 message like "No default-collect." gets in user's face when it's
5140 not wanted. */
5141 if (!*default_collect)
5142 return;
5143
5144 /* The following phrase lines up nicely with per-tracepoint collect
5145 actions. */
5146 ui_out_text (uiout, "default collect ");
5147 ui_out_field_string (uiout, "default-collect", default_collect);
5148 ui_out_text (uiout, " \n");
5149}
5150
c906108c 5151static void
fba45db2 5152breakpoints_info (char *bnum_exp, int from_tty)
c906108c
SS
5153{
5154 int bnum = -1;
5155
5156 if (bnum_exp)
bb518678 5157 bnum = parse_and_eval_long (bnum_exp);
c906108c 5158
d77f58be 5159 breakpoint_1 (bnum, 0, NULL);
ad443146
SS
5160
5161 default_collect_info ();
d77f58be
SS
5162}
5163
5164static void
5165watchpoints_info (char *wpnum_exp, int from_tty)
5166{
5167 int wpnum = -1, num_printed;
5168
5169 if (wpnum_exp)
5170 wpnum = parse_and_eval_long (wpnum_exp);
5171
5172 num_printed = breakpoint_1 (wpnum, 0, is_watchpoint);
5173
5174 if (num_printed == 0)
5175 {
5176 if (wpnum == -1)
5177 ui_out_message (uiout, 0, "No watchpoints.\n");
5178 else
5179 ui_out_message (uiout, 0, "No watchpoint number %d.\n", wpnum);
5180 }
c906108c
SS
5181}
5182
7a292a7a 5183static void
fba45db2 5184maintenance_info_breakpoints (char *bnum_exp, int from_tty)
c906108c
SS
5185{
5186 int bnum = -1;
5187
5188 if (bnum_exp)
bb518678 5189 bnum = parse_and_eval_long (bnum_exp);
c906108c 5190
d77f58be 5191 breakpoint_1 (bnum, 1, NULL);
ad443146
SS
5192
5193 default_collect_info ();
c906108c
SS
5194}
5195
0d381245 5196static int
714835d5 5197breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5198 struct program_space *pspace,
714835d5 5199 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5200{
5201 struct bp_location *bl = b->loc;
cc59ec59 5202
0d381245
VP
5203 for (; bl; bl = bl->next)
5204 {
6c95b8df
PA
5205 if (bl->pspace == pspace
5206 && bl->address == pc
0d381245
VP
5207 && (!overlay_debugging || bl->section == section))
5208 return 1;
5209 }
5210 return 0;
5211}
5212
6c95b8df
PA
5213/* Print a message describing any breakpoints set at PC. This
5214 concerns with logical breakpoints, so we match program spaces, not
5215 address spaces. */
c906108c
SS
5216
5217static void
6c95b8df
PA
5218describe_other_breakpoints (struct gdbarch *gdbarch,
5219 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5220 struct obj_section *section, int thread)
c906108c 5221{
52f0bd74
AC
5222 int others = 0;
5223 struct breakpoint *b;
c906108c
SS
5224
5225 ALL_BREAKPOINTS (b)
6c95b8df 5226 others += breakpoint_has_pc (b, pspace, pc, section);
c906108c
SS
5227 if (others > 0)
5228 {
a3f17187
AC
5229 if (others == 1)
5230 printf_filtered (_("Note: breakpoint "));
5231 else /* if (others == ???) */
5232 printf_filtered (_("Note: breakpoints "));
c906108c 5233 ALL_BREAKPOINTS (b)
6c95b8df 5234 if (breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5235 {
5236 others--;
5237 printf_filtered ("%d", b->number);
5238 if (b->thread == -1 && thread != -1)
5239 printf_filtered (" (all threads)");
5240 else if (b->thread != -1)
5241 printf_filtered (" (thread %d)", b->thread);
5242 printf_filtered ("%s%s ",
059fb39f 5243 ((b->enable_state == bp_disabled
8bea4e01
UW
5244 || b->enable_state == bp_call_disabled
5245 || b->enable_state == bp_startup_disabled)
0d381245
VP
5246 ? " (disabled)"
5247 : b->enable_state == bp_permanent
5248 ? " (permanent)"
5249 : ""),
5250 (others > 1) ? ","
5251 : ((others == 1) ? " and" : ""));
5252 }
a3f17187 5253 printf_filtered (_("also set at pc "));
5af949e3 5254 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5255 printf_filtered (".\n");
5256 }
5257}
5258\f
5259/* Set the default place to put a breakpoint
5260 for the `break' command with no arguments. */
5261
5262void
6c95b8df
PA
5263set_default_breakpoint (int valid, struct program_space *pspace,
5264 CORE_ADDR addr, struct symtab *symtab,
fba45db2 5265 int line)
c906108c
SS
5266{
5267 default_breakpoint_valid = valid;
6c95b8df 5268 default_breakpoint_pspace = pspace;
c906108c
SS
5269 default_breakpoint_address = addr;
5270 default_breakpoint_symtab = symtab;
5271 default_breakpoint_line = line;
5272}
5273
e4f237da
KB
5274/* Return true iff it is meaningful to use the address member of
5275 BPT. For some breakpoint types, the address member is irrelevant
5276 and it makes no sense to attempt to compare it to other addresses
5277 (or use it for any other purpose either).
5278
5279 More specifically, each of the following breakpoint types will always
876fa593
JK
5280 have a zero valued address and we don't want to mark breakpoints of any of
5281 these types to be a duplicate of an actual breakpoint at address zero:
e4f237da
KB
5282
5283 bp_watchpoint
2d134ed3
PA
5284 bp_catchpoint
5285
5286*/
e4f237da
KB
5287
5288static int
5289breakpoint_address_is_meaningful (struct breakpoint *bpt)
5290{
5291 enum bptype type = bpt->type;
5292
2d134ed3
PA
5293 return (type != bp_watchpoint && type != bp_catchpoint);
5294}
5295
5296/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5297 true if LOC1 and LOC2 represent the same watchpoint location. */
5298
5299static int
5300watchpoint_locations_match (struct bp_location *loc1, struct bp_location *loc2)
5301{
2bdf28a0
JK
5302 /* Both of them must not be in moribund_locations. */
5303 gdb_assert (loc1->owner != NULL);
5304 gdb_assert (loc2->owner != NULL);
5305
0cf6dd15
TJB
5306 /* If the target can evaluate the condition expression in hardware, then we
5307 we need to insert both watchpoints even if they are at the same place.
5308 Otherwise the watchpoint will only trigger when the condition of whichever
5309 watchpoint was inserted evaluates to true, not giving a chance for GDB to
5310 check the condition of the other watchpoint. */
5311 if ((loc1->owner->cond_exp
5312 && target_can_accel_watchpoint_condition (loc1->address, loc1->length,
5313 loc1->watchpoint_type,
5314 loc1->owner->cond_exp))
5315 || (loc2->owner->cond_exp
5316 && target_can_accel_watchpoint_condition (loc2->address, loc2->length,
5317 loc2->watchpoint_type,
5318 loc2->owner->cond_exp)))
5319 return 0;
5320
85d721b8
PA
5321 /* Note that this checks the owner's type, not the location's. In
5322 case the target does not support read watchpoints, but does
5323 support access watchpoints, we'll have bp_read_watchpoint
5324 watchpoints with hw_access locations. Those should be considered
5325 duplicates of hw_read locations. The hw_read locations will
5326 become hw_access locations later. */
2d134ed3
PA
5327 return (loc1->owner->type == loc2->owner->type
5328 && loc1->pspace->aspace == loc2->pspace->aspace
5329 && loc1->address == loc2->address
5330 && loc1->length == loc2->length);
e4f237da
KB
5331}
5332
6c95b8df
PA
5333/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5334 same breakpoint location. In most targets, this can only be true
5335 if ASPACE1 matches ASPACE2. On targets that have global
5336 breakpoints, the address space doesn't really matter. */
5337
5338static int
5339breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5340 struct address_space *aspace2, CORE_ADDR addr2)
5341{
5342 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5343 || aspace1 == aspace2)
5344 && addr1 == addr2);
5345}
5346
2d134ed3
PA
5347/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5348 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5349 represent the same location. */
5350
5351static int
5352breakpoint_locations_match (struct bp_location *loc1, struct bp_location *loc2)
5353{
2bdf28a0
JK
5354 int hw_point1, hw_point2;
5355
5356 /* Both of them must not be in moribund_locations. */
5357 gdb_assert (loc1->owner != NULL);
5358 gdb_assert (loc2->owner != NULL);
5359
5360 hw_point1 = is_hardware_watchpoint (loc1->owner);
5361 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5362
5363 if (hw_point1 != hw_point2)
5364 return 0;
5365 else if (hw_point1)
5366 return watchpoint_locations_match (loc1, loc2);
5367 else
5368 return breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5369 loc2->pspace->aspace, loc2->address);
5370}
5371
76897487
KB
5372static void
5373breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5374 int bnum, int have_bnum)
5375{
5376 char astr1[40];
5377 char astr2[40];
5378
bb599908
PH
5379 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5380 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5381 if (have_bnum)
8a3fe4f8 5382 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5383 bnum, astr1, astr2);
5384 else
8a3fe4f8 5385 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5386}
5387
5388/* Adjust a breakpoint's address to account for architectural constraints
5389 on breakpoint placement. Return the adjusted address. Note: Very
5390 few targets require this kind of adjustment. For most targets,
5391 this function is simply the identity function. */
5392
5393static CORE_ADDR
a6d9a66e
UW
5394adjust_breakpoint_address (struct gdbarch *gdbarch,
5395 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5396{
a6d9a66e 5397 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5398 {
5399 /* Very few targets need any kind of breakpoint adjustment. */
5400 return bpaddr;
5401 }
88f7da05
KB
5402 else if (bptype == bp_watchpoint
5403 || bptype == bp_hardware_watchpoint
5404 || bptype == bp_read_watchpoint
5405 || bptype == bp_access_watchpoint
fe798b75 5406 || bptype == bp_catchpoint)
88f7da05
KB
5407 {
5408 /* Watchpoints and the various bp_catch_* eventpoints should not
5409 have their addresses modified. */
5410 return bpaddr;
5411 }
76897487
KB
5412 else
5413 {
5414 CORE_ADDR adjusted_bpaddr;
5415
5416 /* Some targets have architectural constraints on the placement
5417 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5418 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5419
5420 /* An adjusted breakpoint address can significantly alter
5421 a user's expectations. Print a warning if an adjustment
5422 is required. */
5423 if (adjusted_bpaddr != bpaddr)
5424 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5425
5426 return adjusted_bpaddr;
5427 }
5428}
5429
7cc221ef
DJ
5430/* Allocate a struct bp_location. */
5431
26bb91f3 5432static struct bp_location *
39d61571 5433allocate_bp_location (struct breakpoint *bpt)
7cc221ef 5434{
afe38095 5435 struct bp_location *loc;
7cc221ef
DJ
5436
5437 loc = xmalloc (sizeof (struct bp_location));
5438 memset (loc, 0, sizeof (*loc));
5439
e049a4b5 5440 loc->owner = bpt;
511a6cd4 5441 loc->cond = NULL;
0d381245
VP
5442 loc->shlib_disabled = 0;
5443 loc->enabled = 1;
e049a4b5 5444
39d61571 5445 switch (bpt->type)
e049a4b5
DJ
5446 {
5447 case bp_breakpoint:
5448 case bp_until:
5449 case bp_finish:
5450 case bp_longjmp:
5451 case bp_longjmp_resume:
186c406b
TT
5452 case bp_exception:
5453 case bp_exception_resume:
e049a4b5 5454 case bp_step_resume:
e049a4b5
DJ
5455 case bp_watchpoint_scope:
5456 case bp_call_dummy:
aa7d318d 5457 case bp_std_terminate:
e049a4b5
DJ
5458 case bp_shlib_event:
5459 case bp_thread_event:
5460 case bp_overlay_event:
4efc6507 5461 case bp_jit_event:
0fd8e87f 5462 case bp_longjmp_master:
aa7d318d 5463 case bp_std_terminate_master:
186c406b 5464 case bp_exception_master:
e049a4b5
DJ
5465 loc->loc_type = bp_loc_software_breakpoint;
5466 break;
5467 case bp_hardware_breakpoint:
5468 loc->loc_type = bp_loc_hardware_breakpoint;
5469 break;
5470 case bp_hardware_watchpoint:
5471 case bp_read_watchpoint:
5472 case bp_access_watchpoint:
5473 loc->loc_type = bp_loc_hardware_watchpoint;
5474 break;
5475 case bp_watchpoint:
ce78b96d 5476 case bp_catchpoint:
15c3d785
PA
5477 case bp_tracepoint:
5478 case bp_fast_tracepoint:
0fb4aa4b 5479 case bp_static_tracepoint:
e049a4b5
DJ
5480 loc->loc_type = bp_loc_other;
5481 break;
5482 default:
e2e0b3e5 5483 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5484 }
5485
f431efe5 5486 loc->refc = 1;
7cc221ef
DJ
5487 return loc;
5488}
5489
f431efe5
PA
5490static void
5491free_bp_location (struct bp_location *loc)
fe3f5fa8
VP
5492{
5493 if (loc->cond)
5494 xfree (loc->cond);
74960c60
VP
5495
5496 if (loc->function_name)
5497 xfree (loc->function_name);
f431efe5 5498
fe3f5fa8
VP
5499 xfree (loc);
5500}
5501
f431efe5
PA
5502/* Increment reference count. */
5503
5504static void
5505incref_bp_location (struct bp_location *bl)
5506{
5507 ++bl->refc;
5508}
5509
5510/* Decrement reference count. If the reference count reaches 0,
5511 destroy the bp_location. Sets *BLP to NULL. */
5512
5513static void
5514decref_bp_location (struct bp_location **blp)
5515{
0807b50c
PA
5516 gdb_assert ((*blp)->refc > 0);
5517
f431efe5
PA
5518 if (--(*blp)->refc == 0)
5519 free_bp_location (*blp);
5520 *blp = NULL;
5521}
5522
0d381245
VP
5523/* Helper to set_raw_breakpoint below. Creates a breakpoint
5524 that has type BPTYPE and has no locations as yet. */
63c252f8 5525/* This function is used in gdbtk sources and thus can not be made static. */
c906108c 5526
c40e75cd 5527static struct breakpoint *
a6d9a66e
UW
5528set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
5529 enum bptype bptype)
c906108c 5530{
52f0bd74 5531 struct breakpoint *b, *b1;
c906108c
SS
5532
5533 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
5534 memset (b, 0, sizeof (*b));
2219d63c 5535
4d28f7a8 5536 b->type = bptype;
a6d9a66e 5537 b->gdbarch = gdbarch;
c906108c
SS
5538 b->language = current_language->la_language;
5539 b->input_radix = input_radix;
5540 b->thread = -1;
b5de0fa7 5541 b->enable_state = bp_enabled;
c906108c
SS
5542 b->next = 0;
5543 b->silent = 0;
5544 b->ignore_count = 0;
5545 b->commands = NULL;
818dd999 5546 b->frame_id = null_frame_id;
3a3e9ee3 5547 b->forked_inferior_pid = null_ptid;
c906108c 5548 b->exec_pathname = NULL;
a96d9b2e 5549 b->syscalls_to_be_caught = NULL;
3086aeae 5550 b->ops = NULL;
0d381245 5551 b->condition_not_parsed = 0;
84f4c1fe 5552 b->py_bp_object = NULL;
c906108c
SS
5553
5554 /* Add this breakpoint to the end of the chain
5555 so that a list of breakpoints will come out in order
5556 of increasing numbers. */
5557
5558 b1 = breakpoint_chain;
5559 if (b1 == 0)
5560 breakpoint_chain = b;
5561 else
5562 {
5563 while (b1->next)
5564 b1 = b1->next;
5565 b1->next = b;
5566 }
0d381245
VP
5567 return b;
5568}
5569
5570/* Initialize loc->function_name. */
5571static void
5572set_breakpoint_location_function (struct bp_location *loc)
5573{
2bdf28a0
JK
5574 gdb_assert (loc->owner != NULL);
5575
0d381245 5576 if (loc->owner->type == bp_breakpoint
1042e4c0 5577 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5578 || is_tracepoint (loc->owner))
0d381245
VP
5579 {
5580 find_pc_partial_function (loc->address, &(loc->function_name),
5581 NULL, NULL);
5582 if (loc->function_name)
5583 loc->function_name = xstrdup (loc->function_name);
5584 }
5585}
5586
a6d9a66e
UW
5587/* Attempt to determine architecture of location identified by SAL. */
5588static struct gdbarch *
5589get_sal_arch (struct symtab_and_line sal)
5590{
5591 if (sal.section)
5592 return get_objfile_arch (sal.section->objfile);
5593 if (sal.symtab)
5594 return get_objfile_arch (sal.symtab->objfile);
5595
5596 return NULL;
5597}
5598
0d381245
VP
5599/* set_raw_breakpoint is a low level routine for allocating and
5600 partially initializing a breakpoint of type BPTYPE. The newly
5601 created breakpoint's address, section, source file name, and line
5602 number are provided by SAL. The newly created and partially
5603 initialized breakpoint is added to the breakpoint chain and
5604 is also returned as the value of this function.
5605
5606 It is expected that the caller will complete the initialization of
5607 the newly created breakpoint struct as well as output any status
5608 information regarding the creation of a new breakpoint. In
5609 particular, set_raw_breakpoint does NOT set the breakpoint
5610 number! Care should be taken to not allow an error to occur
5611 prior to completing the initialization of the breakpoint. If this
5612 should happen, a bogus breakpoint will be left on the chain. */
5613
63c252f8 5614struct breakpoint *
a6d9a66e
UW
5615set_raw_breakpoint (struct gdbarch *gdbarch,
5616 struct symtab_and_line sal, enum bptype bptype)
0d381245 5617{
a6d9a66e 5618 struct breakpoint *b = set_raw_breakpoint_without_location (gdbarch, bptype);
0d381245 5619 CORE_ADDR adjusted_address;
a6d9a66e
UW
5620 struct gdbarch *loc_gdbarch;
5621
5622 loc_gdbarch = get_sal_arch (sal);
5623 if (!loc_gdbarch)
5624 loc_gdbarch = b->gdbarch;
0d381245 5625
6c95b8df
PA
5626 if (bptype != bp_catchpoint)
5627 gdb_assert (sal.pspace != NULL);
5628
0d381245
VP
5629 /* Adjust the breakpoint's address prior to allocating a location.
5630 Once we call allocate_bp_location(), that mostly uninitialized
5631 location will be placed on the location chain. Adjustment of the
8defab1a 5632 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
5633 not want its scan of the location chain to find a breakpoint and
5634 location that's only been partially initialized. */
a6d9a66e 5635 adjusted_address = adjust_breakpoint_address (loc_gdbarch, sal.pc, b->type);
0d381245 5636
39d61571 5637 b->loc = allocate_bp_location (b);
a6d9a66e 5638 b->loc->gdbarch = loc_gdbarch;
0d381245
VP
5639 b->loc->requested_address = sal.pc;
5640 b->loc->address = adjusted_address;
6c95b8df
PA
5641 b->loc->pspace = sal.pspace;
5642
5643 /* Store the program space that was used to set the breakpoint, for
5644 breakpoint resetting. */
5645 b->pspace = sal.pspace;
0d381245
VP
5646
5647 if (sal.symtab == NULL)
5648 b->source_file = NULL;
5649 else
1b36a34b 5650 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5651 b->loc->section = sal.section;
5652 b->line_number = sal.line;
5653
5654 set_breakpoint_location_function (b->loc);
c906108c 5655
c906108c
SS
5656 breakpoints_changed ();
5657
5658 return b;
5659}
5660
c2c6d25f
JM
5661
5662/* Note that the breakpoint object B describes a permanent breakpoint
5663 instruction, hard-wired into the inferior's code. */
5664void
5665make_breakpoint_permanent (struct breakpoint *b)
5666{
0d381245 5667 struct bp_location *bl;
cc59ec59 5668
b5de0fa7 5669 b->enable_state = bp_permanent;
c2c6d25f 5670
0d381245
VP
5671 /* By definition, permanent breakpoints are already present in the code.
5672 Mark all locations as inserted. For now, make_breakpoint_permanent
5673 is called in just one place, so it's hard to say if it's reasonable
5674 to have permanent breakpoint with multiple locations or not,
5675 but it's easy to implmement. */
5676 for (bl = b->loc; bl; bl = bl->next)
5677 bl->inserted = 1;
c2c6d25f
JM
5678}
5679
53a5351d 5680/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5681 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5682 initiated the operation. */
c906108c
SS
5683
5684void
186c406b 5685set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5686{
35df4500 5687 struct breakpoint *b, *b_tmp;
186c406b 5688 int thread = tp->num;
0fd8e87f
UW
5689
5690 /* To avoid having to rescan all objfile symbols at every step,
5691 we maintain a list of continually-inserted but always disabled
5692 longjmp "master" breakpoints. Here, we simply create momentary
5693 clones of those and enable them for the requested thread. */
35df4500 5694 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5695 if (b->pspace == current_program_space
186c406b
TT
5696 && (b->type == bp_longjmp_master
5697 || b->type == bp_exception_master))
0fd8e87f
UW
5698 {
5699 struct breakpoint *clone = clone_momentary_breakpoint (b);
cc59ec59 5700
186c406b 5701 clone->type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
0fd8e87f
UW
5702 clone->thread = thread;
5703 }
186c406b
TT
5704
5705 tp->initiating_frame = frame;
c906108c
SS
5706}
5707
611c83ae 5708/* Delete all longjmp breakpoints from THREAD. */
c906108c 5709void
611c83ae 5710delete_longjmp_breakpoint (int thread)
c906108c 5711{
35df4500 5712 struct breakpoint *b, *b_tmp;
c906108c 5713
35df4500 5714 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5715 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5716 {
5717 if (b->thread == thread)
5718 delete_breakpoint (b);
5719 }
c906108c
SS
5720}
5721
1900040c
MS
5722void
5723enable_overlay_breakpoints (void)
5724{
52f0bd74 5725 struct breakpoint *b;
1900040c
MS
5726
5727 ALL_BREAKPOINTS (b)
5728 if (b->type == bp_overlay_event)
5729 {
5730 b->enable_state = bp_enabled;
b60e7edf 5731 update_global_location_list (1);
c02f5703 5732 overlay_events_enabled = 1;
1900040c
MS
5733 }
5734}
5735
5736void
5737disable_overlay_breakpoints (void)
5738{
52f0bd74 5739 struct breakpoint *b;
1900040c
MS
5740
5741 ALL_BREAKPOINTS (b)
5742 if (b->type == bp_overlay_event)
5743 {
5744 b->enable_state = bp_disabled;
b60e7edf 5745 update_global_location_list (0);
c02f5703 5746 overlay_events_enabled = 0;
1900040c
MS
5747 }
5748}
5749
aa7d318d
TT
5750/* Set an active std::terminate breakpoint for each std::terminate
5751 master breakpoint. */
5752void
5753set_std_terminate_breakpoint (void)
5754{
35df4500 5755 struct breakpoint *b, *b_tmp;
aa7d318d 5756
35df4500 5757 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5758 if (b->pspace == current_program_space
5759 && b->type == bp_std_terminate_master)
5760 {
5761 struct breakpoint *clone = clone_momentary_breakpoint (b);
5762 clone->type = bp_std_terminate;
5763 }
5764}
5765
5766/* Delete all the std::terminate breakpoints. */
5767void
5768delete_std_terminate_breakpoint (void)
5769{
35df4500 5770 struct breakpoint *b, *b_tmp;
aa7d318d 5771
35df4500 5772 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5773 if (b->type == bp_std_terminate)
5774 delete_breakpoint (b);
5775}
5776
c4093a6a 5777struct breakpoint *
a6d9a66e 5778create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
5779{
5780 struct breakpoint *b;
c4093a6a 5781
a6d9a66e 5782 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
c4093a6a 5783
b5de0fa7 5784 b->enable_state = bp_enabled;
c4093a6a 5785 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
5786 b->addr_string
5787 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 5788
b60e7edf 5789 update_global_location_list_nothrow (1);
74960c60 5790
c4093a6a
JM
5791 return b;
5792}
5793
5794void
5795remove_thread_event_breakpoints (void)
5796{
35df4500 5797 struct breakpoint *b, *b_tmp;
c4093a6a 5798
35df4500 5799 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
5800 if (b->type == bp_thread_event
5801 && b->loc->pspace == current_program_space)
c4093a6a
JM
5802 delete_breakpoint (b);
5803}
5804
0101ce28
JJ
5805struct captured_parse_breakpoint_args
5806 {
5807 char **arg_p;
5808 struct symtabs_and_lines *sals_p;
5809 char ***addr_string_p;
5810 int *not_found_ptr;
5811 };
5812
5813struct lang_and_radix
5814 {
5815 enum language lang;
5816 int radix;
5817 };
5818
4efc6507
DE
5819/* Create a breakpoint for JIT code registration and unregistration. */
5820
5821struct breakpoint *
5822create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5823{
5824 struct breakpoint *b;
5825
5826 b = create_internal_breakpoint (gdbarch, address, bp_jit_event);
5827 update_global_location_list_nothrow (1);
5828 return b;
5829}
0101ce28 5830
cae688ec
JJ
5831void
5832remove_solib_event_breakpoints (void)
5833{
35df4500 5834 struct breakpoint *b, *b_tmp;
cae688ec 5835
35df4500 5836 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
5837 if (b->type == bp_shlib_event
5838 && b->loc->pspace == current_program_space)
cae688ec
JJ
5839 delete_breakpoint (b);
5840}
5841
5842struct breakpoint *
a6d9a66e 5843create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
5844{
5845 struct breakpoint *b;
5846
a6d9a66e 5847 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
b60e7edf 5848 update_global_location_list_nothrow (1);
cae688ec
JJ
5849 return b;
5850}
5851
5852/* Disable any breakpoints that are on code in shared libraries. Only
5853 apply to enabled breakpoints, disabled ones can just stay disabled. */
5854
5855void
cb851954 5856disable_breakpoints_in_shlibs (void)
cae688ec 5857{
876fa593 5858 struct bp_location *loc, **locp_tmp;
cae688ec 5859
876fa593 5860 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 5861 {
2bdf28a0 5862 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 5863 struct breakpoint *b = loc->owner;
2bdf28a0 5864
0d381245
VP
5865 /* We apply the check to all breakpoints, including disabled
5866 for those with loc->duplicate set. This is so that when breakpoint
5867 becomes enabled, or the duplicate is removed, gdb will try to insert
5868 all breakpoints. If we don't set shlib_disabled here, we'll try
5869 to insert those breakpoints and fail. */
1042e4c0 5870 if (((b->type == bp_breakpoint)
508ccb1f 5871 || (b->type == bp_jit_event)
1042e4c0 5872 || (b->type == bp_hardware_breakpoint)
d77f58be 5873 || (is_tracepoint (b)))
6c95b8df 5874 && loc->pspace == current_program_space
0d381245 5875 && !loc->shlib_disabled
a77053c2 5876#ifdef PC_SOLIB
0d381245 5877 && PC_SOLIB (loc->address)
a77053c2 5878#else
6c95b8df 5879 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
5880#endif
5881 )
0d381245
VP
5882 {
5883 loc->shlib_disabled = 1;
5884 }
cae688ec
JJ
5885 }
5886}
5887
84acb35a
JJ
5888/* Disable any breakpoints that are in in an unloaded shared library. Only
5889 apply to enabled breakpoints, disabled ones can just stay disabled. */
5890
75149521 5891static void
84acb35a
JJ
5892disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
5893{
876fa593 5894 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
5895 int disabled_shlib_breaks = 0;
5896
c86cf029
VP
5897 /* SunOS a.out shared libraries are always mapped, so do not
5898 disable breakpoints; they will only be reported as unloaded
5899 through clear_solib when GDB discards its shared library
5900 list. See clear_solib for more information. */
5901 if (exec_bfd != NULL
5902 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
5903 return;
5904
876fa593 5905 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 5906 {
2bdf28a0 5907 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 5908 struct breakpoint *b = loc->owner;
cc59ec59 5909
0d381245
VP
5910 if ((loc->loc_type == bp_loc_hardware_breakpoint
5911 || loc->loc_type == bp_loc_software_breakpoint)
6c95b8df 5912 && solib->pspace == loc->pspace
e2dd7057 5913 && !loc->shlib_disabled
508ccb1f
TT
5914 && (b->type == bp_breakpoint
5915 || b->type == bp_jit_event
5916 || b->type == bp_hardware_breakpoint)
e2dd7057 5917 && solib_contains_address_p (solib, loc->address))
84acb35a 5918 {
e2dd7057
PP
5919 loc->shlib_disabled = 1;
5920 /* At this point, we cannot rely on remove_breakpoint
5921 succeeding so we must mark the breakpoint as not inserted
5922 to prevent future errors occurring in remove_breakpoints. */
5923 loc->inserted = 0;
5924 if (!disabled_shlib_breaks)
5925 {
5926 target_terminal_ours_for_output ();
5927 warning (_("Temporarily disabling breakpoints for unloaded shared library \"%s\""),
5928 solib->so_name);
84acb35a 5929 }
e2dd7057 5930 disabled_shlib_breaks = 1;
84acb35a
JJ
5931 }
5932 }
84acb35a
JJ
5933}
5934
ce78b96d
JB
5935/* FORK & VFORK catchpoints. */
5936
5937/* Implement the "insert" breakpoint_ops method for fork catchpoints. */
5938
c906108c 5939static void
ce78b96d
JB
5940insert_catch_fork (struct breakpoint *b)
5941{
5942 target_insert_fork_catchpoint (PIDGET (inferior_ptid));
5943}
5944
5945/* Implement the "remove" breakpoint_ops method for fork catchpoints. */
5946
5947static int
5948remove_catch_fork (struct breakpoint *b)
5949{
5950 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
5951}
5952
5953/* Implement the "breakpoint_hit" breakpoint_ops method for fork
5954 catchpoints. */
5955
5956static int
5957breakpoint_hit_catch_fork (struct breakpoint *b)
5958{
5959 return inferior_has_forked (inferior_ptid, &b->forked_inferior_pid);
5960}
5961
5962/* Implement the "print_it" breakpoint_ops method for fork catchpoints. */
5963
5964static enum print_stop_action
5965print_it_catch_fork (struct breakpoint *b)
5966{
5967 annotate_catchpoint (b->number);
5968 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
5969 b->number, ptid_get_pid (b->forked_inferior_pid));
5970 return PRINT_SRC_AND_LOC;
5971}
5972
5973/* Implement the "print_one" breakpoint_ops method for fork catchpoints. */
5974
5975static void
a6d9a66e 5976print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 5977{
79a45b7d
TT
5978 struct value_print_options opts;
5979
5980 get_user_print_options (&opts);
5981
ce78b96d
JB
5982 /* Field 4, the address, is omitted (which makes the columns
5983 not line up too nicely with the headers, but the effect
5984 is relatively readable). */
79a45b7d 5985 if (opts.addressprint)
ce78b96d
JB
5986 ui_out_field_skip (uiout, "addr");
5987 annotate_field (5);
5988 ui_out_text (uiout, "fork");
5989 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
5990 {
5991 ui_out_text (uiout, ", process ");
5992 ui_out_field_int (uiout, "what",
5993 ptid_get_pid (b->forked_inferior_pid));
5994 ui_out_spaces (uiout, 1);
5995 }
5996}
5997
5998/* Implement the "print_mention" breakpoint_ops method for fork
5999 catchpoints. */
6000
6001static void
6002print_mention_catch_fork (struct breakpoint *b)
6003{
6004 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6005}
6006
6149aea9
PA
6007/* Implement the "print_recreate" breakpoint_ops method for fork
6008 catchpoints. */
6009
6010static void
6011print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6012{
6013 fprintf_unfiltered (fp, "catch fork");
6014}
6015
ce78b96d
JB
6016/* The breakpoint_ops structure to be used in fork catchpoints. */
6017
6018static struct breakpoint_ops catch_fork_breakpoint_ops =
6019{
6020 insert_catch_fork,
6021 remove_catch_fork,
6022 breakpoint_hit_catch_fork,
6023 print_it_catch_fork,
6024 print_one_catch_fork,
6149aea9
PA
6025 print_mention_catch_fork,
6026 print_recreate_catch_fork
ce78b96d
JB
6027};
6028
6029/* Implement the "insert" breakpoint_ops method for vfork catchpoints. */
6030
6031static void
6032insert_catch_vfork (struct breakpoint *b)
6033{
6034 target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
6035}
6036
6037/* Implement the "remove" breakpoint_ops method for vfork catchpoints. */
6038
6039static int
6040remove_catch_vfork (struct breakpoint *b)
6041{
6042 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6043}
6044
6045/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6046 catchpoints. */
6047
6048static int
6049breakpoint_hit_catch_vfork (struct breakpoint *b)
6050{
6051 return inferior_has_vforked (inferior_ptid, &b->forked_inferior_pid);
6052}
6053
6054/* Implement the "print_it" breakpoint_ops method for vfork catchpoints. */
6055
6056static enum print_stop_action
6057print_it_catch_vfork (struct breakpoint *b)
6058{
6059 annotate_catchpoint (b->number);
6060 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
6061 b->number, ptid_get_pid (b->forked_inferior_pid));
6062 return PRINT_SRC_AND_LOC;
6063}
6064
6065/* Implement the "print_one" breakpoint_ops method for vfork catchpoints. */
6066
6067static void
a6d9a66e 6068print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6069{
79a45b7d
TT
6070 struct value_print_options opts;
6071
6072 get_user_print_options (&opts);
ce78b96d
JB
6073 /* Field 4, the address, is omitted (which makes the columns
6074 not line up too nicely with the headers, but the effect
6075 is relatively readable). */
79a45b7d 6076 if (opts.addressprint)
ce78b96d
JB
6077 ui_out_field_skip (uiout, "addr");
6078 annotate_field (5);
6079 ui_out_text (uiout, "vfork");
6080 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6081 {
6082 ui_out_text (uiout, ", process ");
6083 ui_out_field_int (uiout, "what",
6084 ptid_get_pid (b->forked_inferior_pid));
6085 ui_out_spaces (uiout, 1);
6086 }
6087}
6088
6089/* Implement the "print_mention" breakpoint_ops method for vfork
6090 catchpoints. */
6091
6092static void
6093print_mention_catch_vfork (struct breakpoint *b)
6094{
6095 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6096}
6097
6149aea9
PA
6098/* Implement the "print_recreate" breakpoint_ops method for vfork
6099 catchpoints. */
6100
6101static void
6102print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6103{
6104 fprintf_unfiltered (fp, "catch vfork");
6105}
6106
ce78b96d
JB
6107/* The breakpoint_ops structure to be used in vfork catchpoints. */
6108
6109static struct breakpoint_ops catch_vfork_breakpoint_ops =
6110{
6111 insert_catch_vfork,
6112 remove_catch_vfork,
6113 breakpoint_hit_catch_vfork,
6114 print_it_catch_vfork,
6115 print_one_catch_vfork,
6149aea9
PA
6116 print_mention_catch_vfork,
6117 print_recreate_catch_vfork
ce78b96d
JB
6118};
6119
a96d9b2e
SDJ
6120/* Implement the "insert" breakpoint_ops method for syscall
6121 catchpoints. */
6122
6123static void
6124insert_catch_syscall (struct breakpoint *b)
6125{
6126 struct inferior *inf = current_inferior ();
6127
6128 ++inf->total_syscalls_count;
6129 if (!b->syscalls_to_be_caught)
6130 ++inf->any_syscall_count;
6131 else
6132 {
6133 int i, iter;
cc59ec59 6134
a96d9b2e
SDJ
6135 for (i = 0;
6136 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6137 i++)
6138 {
6139 int elem;
cc59ec59 6140
a96d9b2e
SDJ
6141 if (iter >= VEC_length (int, inf->syscalls_counts))
6142 {
6143 int old_size = VEC_length (int, inf->syscalls_counts);
6144 uintptr_t vec_addr_offset = old_size * ((uintptr_t) sizeof (int));
6145 uintptr_t vec_addr;
6146 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6147 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6148 vec_addr_offset;
6149 memset ((void *) vec_addr, 0,
6150 (iter + 1 - old_size) * sizeof (int));
6151 }
6152 elem = VEC_index (int, inf->syscalls_counts, iter);
6153 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6154 }
6155 }
6156
6157 target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6158 inf->total_syscalls_count != 0,
6159 inf->any_syscall_count,
6160 VEC_length (int, inf->syscalls_counts),
6161 VEC_address (int, inf->syscalls_counts));
6162}
6163
6164/* Implement the "remove" breakpoint_ops method for syscall
6165 catchpoints. */
6166
6167static int
6168remove_catch_syscall (struct breakpoint *b)
6169{
6170 struct inferior *inf = current_inferior ();
6171
6172 --inf->total_syscalls_count;
6173 if (!b->syscalls_to_be_caught)
6174 --inf->any_syscall_count;
6175 else
6176 {
6177 int i, iter;
cc59ec59 6178
a96d9b2e
SDJ
6179 for (i = 0;
6180 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6181 i++)
6182 {
6183 int elem;
6184 if (iter >= VEC_length (int, inf->syscalls_counts))
6185 /* Shouldn't happen. */
6186 continue;
6187 elem = VEC_index (int, inf->syscalls_counts, iter);
6188 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6189 }
6190 }
6191
6192 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6193 inf->total_syscalls_count != 0,
6194 inf->any_syscall_count,
6195 VEC_length (int, inf->syscalls_counts),
6196 VEC_address (int, inf->syscalls_counts));
6197}
6198
6199/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6200 catchpoints. */
6201
6202static int
6203breakpoint_hit_catch_syscall (struct breakpoint *b)
6204{
6205 /* We must check if we are catching specific syscalls in this breakpoint.
6206 If we are, then we must guarantee that the called syscall is the same
6207 syscall we are catching. */
6208 int syscall_number = 0;
6209
6210 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6211 return 0;
6212
6213 /* Now, checking if the syscall is the same. */
6214 if (b->syscalls_to_be_caught)
6215 {
6216 int i, iter;
cc59ec59 6217
a96d9b2e
SDJ
6218 for (i = 0;
6219 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6220 i++)
6221 if (syscall_number == iter)
6222 break;
6223 /* Not the same. */
6224 if (!iter)
6225 return 0;
6226 }
6227
6228 return 1;
6229}
6230
6231/* Implement the "print_it" breakpoint_ops method for syscall
6232 catchpoints. */
6233
6234static enum print_stop_action
6235print_it_catch_syscall (struct breakpoint *b)
6236{
6237 /* These are needed because we want to know in which state a
6238 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6239 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6240 must print "called syscall" or "returned from syscall". */
6241 ptid_t ptid;
6242 struct target_waitstatus last;
6243 struct syscall s;
6244 struct cleanup *old_chain;
6245 char *syscall_id;
6246
6247 get_last_target_status (&ptid, &last);
6248
6249 get_syscall_by_number (last.value.syscall_number, &s);
6250
6251 annotate_catchpoint (b->number);
6252
6253 if (s.name == NULL)
6254 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6255 else
6256 syscall_id = xstrprintf ("'%s'", s.name);
6257
6258 old_chain = make_cleanup (xfree, syscall_id);
6259
6260 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6261 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6262 b->number, syscall_id);
6263 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6264 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6265 b->number, syscall_id);
6266
6267 do_cleanups (old_chain);
6268
6269 return PRINT_SRC_AND_LOC;
6270}
6271
6272/* Implement the "print_one" breakpoint_ops method for syscall
6273 catchpoints. */
6274
6275static void
6276print_one_catch_syscall (struct breakpoint *b,
6277 struct bp_location **last_loc)
6278{
6279 struct value_print_options opts;
6280
6281 get_user_print_options (&opts);
6282 /* Field 4, the address, is omitted (which makes the columns
6283 not line up too nicely with the headers, but the effect
6284 is relatively readable). */
6285 if (opts.addressprint)
6286 ui_out_field_skip (uiout, "addr");
6287 annotate_field (5);
6288
6289 if (b->syscalls_to_be_caught
6290 && VEC_length (int, b->syscalls_to_be_caught) > 1)
6291 ui_out_text (uiout, "syscalls \"");
6292 else
6293 ui_out_text (uiout, "syscall \"");
6294
6295 if (b->syscalls_to_be_caught)
6296 {
6297 int i, iter;
6298 char *text = xstrprintf ("%s", "");
cc59ec59 6299
a96d9b2e
SDJ
6300 for (i = 0;
6301 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6302 i++)
6303 {
6304 char *x = text;
6305 struct syscall s;
6306 get_syscall_by_number (iter, &s);
6307
6308 if (s.name != NULL)
6309 text = xstrprintf ("%s%s, ", text, s.name);
6310 else
6311 text = xstrprintf ("%s%d, ", text, iter);
6312
6313 /* We have to xfree the last 'text' (now stored at 'x')
6314 because xstrprintf dinamically allocates new space for it
6315 on every call. */
6316 xfree (x);
6317 }
6318 /* Remove the last comma. */
6319 text[strlen (text) - 2] = '\0';
6320 ui_out_field_string (uiout, "what", text);
6321 }
6322 else
6323 ui_out_field_string (uiout, "what", "<any syscall>");
6324 ui_out_text (uiout, "\" ");
6325}
6326
6327/* Implement the "print_mention" breakpoint_ops method for syscall
6328 catchpoints. */
6329
6330static void
6331print_mention_catch_syscall (struct breakpoint *b)
6332{
6333 if (b->syscalls_to_be_caught)
6334 {
6335 int i, iter;
6336
6337 if (VEC_length (int, b->syscalls_to_be_caught) > 1)
6338 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6339 else
6340 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6341
6342 for (i = 0;
6343 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6344 i++)
6345 {
6346 struct syscall s;
6347 get_syscall_by_number (iter, &s);
6348
6349 if (s.name)
6350 printf_filtered (" '%s' [%d]", s.name, s.number);
6351 else
6352 printf_filtered (" %d", s.number);
6353 }
6354 printf_filtered (")");
6355 }
6356 else
6357 printf_filtered (_("Catchpoint %d (any syscall)"),
6358 b->number);
6359}
6360
6149aea9
PA
6361/* Implement the "print_recreate" breakpoint_ops method for syscall
6362 catchpoints. */
6363
6364static void
6365print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6366{
6367 fprintf_unfiltered (fp, "catch syscall");
6368
6369 if (b->syscalls_to_be_caught)
6370 {
6371 int i, iter;
6372
6373 for (i = 0;
6374 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6375 i++)
6376 {
6377 struct syscall s;
6378
6379 get_syscall_by_number (iter, &s);
6380 if (s.name)
6381 fprintf_unfiltered (fp, " %s", s.name);
6382 else
6383 fprintf_unfiltered (fp, " %d", s.number);
6384 }
6385 }
6386}
6387
a96d9b2e
SDJ
6388/* The breakpoint_ops structure to be used in syscall catchpoints. */
6389
6390static struct breakpoint_ops catch_syscall_breakpoint_ops =
6391{
6392 insert_catch_syscall,
6393 remove_catch_syscall,
6394 breakpoint_hit_catch_syscall,
6395 print_it_catch_syscall,
6396 print_one_catch_syscall,
6149aea9
PA
6397 print_mention_catch_syscall,
6398 print_recreate_catch_syscall
a96d9b2e
SDJ
6399};
6400
6401/* Returns non-zero if 'b' is a syscall catchpoint. */
6402
6403static int
6404syscall_catchpoint_p (struct breakpoint *b)
6405{
6406 return (b->ops == &catch_syscall_breakpoint_ops);
6407}
6408
6409/* Create a new breakpoint of the bp_catchpoint kind and return it,
6410 but does NOT mention it nor update the global location list.
6411 This is useful if you need to fill more fields in the
6412 struct breakpoint before calling mention.
ce78b96d
JB
6413
6414 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6415 If COND_STRING is not NULL, then store it in the breakpoint.
6416 OPS, if not NULL, is the breakpoint_ops structure associated
6417 to the catchpoint. */
6418
6419static struct breakpoint *
a96d9b2e
SDJ
6420create_catchpoint_without_mention (struct gdbarch *gdbarch, int tempflag,
6421 char *cond_string,
6422 struct breakpoint_ops *ops)
c906108c 6423{
c5aa993b
JM
6424 struct symtab_and_line sal;
6425 struct breakpoint *b;
c5aa993b 6426
fe39c653 6427 init_sal (&sal);
6c95b8df 6428 sal.pspace = current_program_space;
c5aa993b 6429
a6d9a66e 6430 b = set_raw_breakpoint (gdbarch, sal, bp_catchpoint);
c906108c
SS
6431 set_breakpoint_count (breakpoint_count + 1);
6432 b->number = breakpoint_count;
ce78b96d 6433
1b36a34b 6434 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
ce78b96d 6435 b->thread = -1;
c906108c 6436 b->addr_string = NULL;
b5de0fa7
EZ
6437 b->enable_state = bp_enabled;
6438 b->disposition = tempflag ? disp_del : disp_donttouch;
ce78b96d 6439 b->ops = ops;
c5aa993b 6440
a96d9b2e
SDJ
6441 return b;
6442}
6443
6444/* Create a new breakpoint of the bp_catchpoint kind and return it.
6445
6446 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6447 If COND_STRING is not NULL, then store it in the breakpoint.
6448 OPS, if not NULL, is the breakpoint_ops structure associated
6449 to the catchpoint. */
6450
6451static struct breakpoint *
6452create_catchpoint (struct gdbarch *gdbarch, int tempflag,
6453 char *cond_string, struct breakpoint_ops *ops)
6454{
6455 struct breakpoint *b =
6456 create_catchpoint_without_mention (gdbarch, tempflag, cond_string, ops);
6457
c906108c 6458 mention (b);
ce78b96d 6459 update_global_location_list (1);
c906108c 6460
ce78b96d 6461 return b;
c906108c 6462}
c5aa993b 6463
9b70b993 6464static void
a6d9a66e
UW
6465create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6466 int tempflag, char *cond_string,
ce78b96d 6467 struct breakpoint_ops *ops)
c906108c 6468{
a6d9a66e
UW
6469 struct breakpoint *b
6470 = create_catchpoint (gdbarch, tempflag, cond_string, ops);
ce78b96d
JB
6471
6472 /* FIXME: We should put this information in a breakpoint private data
6473 area. */
6474 b->forked_inferior_pid = null_ptid;
c906108c
SS
6475}
6476
fe798b75
JB
6477/* Exec catchpoints. */
6478
9b70b993 6479static void
fe798b75 6480insert_catch_exec (struct breakpoint *b)
c906108c 6481{
fe798b75
JB
6482 target_insert_exec_catchpoint (PIDGET (inferior_ptid));
6483}
c906108c 6484
fe798b75
JB
6485static int
6486remove_catch_exec (struct breakpoint *b)
6487{
6488 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6489}
c906108c 6490
fe798b75
JB
6491static int
6492breakpoint_hit_catch_exec (struct breakpoint *b)
6493{
6494 return inferior_has_execd (inferior_ptid, &b->exec_pathname);
6495}
c906108c 6496
fe798b75
JB
6497static enum print_stop_action
6498print_it_catch_exec (struct breakpoint *b)
6499{
6500 annotate_catchpoint (b->number);
6501 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
6502 b->exec_pathname);
6503 return PRINT_SRC_AND_LOC;
c906108c
SS
6504}
6505
fe798b75 6506static void
a6d9a66e 6507print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75
JB
6508{
6509 struct value_print_options opts;
6510
6511 get_user_print_options (&opts);
6512
6513 /* Field 4, the address, is omitted (which makes the columns
6514 not line up too nicely with the headers, but the effect
6515 is relatively readable). */
6516 if (opts.addressprint)
6517 ui_out_field_skip (uiout, "addr");
6518 annotate_field (5);
6519 ui_out_text (uiout, "exec");
6520 if (b->exec_pathname != NULL)
6521 {
6522 ui_out_text (uiout, ", program \"");
6523 ui_out_field_string (uiout, "what", b->exec_pathname);
6524 ui_out_text (uiout, "\" ");
6525 }
6526}
6527
6528static void
6529print_mention_catch_exec (struct breakpoint *b)
6530{
6531 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6532}
6533
6149aea9
PA
6534/* Implement the "print_recreate" breakpoint_ops method for exec
6535 catchpoints. */
6536
6537static void
6538print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6539{
6540 fprintf_unfiltered (fp, "catch exec");
6541}
6542
fe798b75
JB
6543static struct breakpoint_ops catch_exec_breakpoint_ops =
6544{
6545 insert_catch_exec,
6546 remove_catch_exec,
6547 breakpoint_hit_catch_exec,
6548 print_it_catch_exec,
6549 print_one_catch_exec,
6149aea9
PA
6550 print_mention_catch_exec,
6551 print_recreate_catch_exec
fe798b75
JB
6552};
6553
a96d9b2e
SDJ
6554static void
6555create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6556 struct breakpoint_ops *ops)
6557{
6558 struct gdbarch *gdbarch = get_current_arch ();
6559 struct breakpoint *b =
6560 create_catchpoint_without_mention (gdbarch, tempflag, NULL, ops);
6561
6562 b->syscalls_to_be_caught = filter;
6563
6564 /* Now, we have to mention the breakpoint and update the global
6565 location list. */
6566 mention (b);
6567 update_global_location_list (1);
6568}
6569
c906108c 6570static int
fba45db2 6571hw_breakpoint_used_count (void)
c906108c 6572{
52f0bd74 6573 struct breakpoint *b;
c906108c
SS
6574 int i = 0;
6575
6576 ALL_BREAKPOINTS (b)
c5aa993b 6577 {
d6b74ac4 6578 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
c5aa993b
JM
6579 i++;
6580 }
c906108c
SS
6581
6582 return i;
6583}
6584
6585static int
fba45db2 6586hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 6587{
52f0bd74 6588 struct breakpoint *b;
c906108c
SS
6589 int i = 0;
6590
6591 *other_type_used = 0;
6592 ALL_BREAKPOINTS (b)
c5aa993b 6593 {
468d015d 6594 if (breakpoint_enabled (b))
c5aa993b
JM
6595 {
6596 if (b->type == type)
6597 i++;
cc60f2e3 6598 else if (is_hardware_watchpoint (b))
c5aa993b
JM
6599 *other_type_used = 1;
6600 }
6601 }
c906108c
SS
6602 return i;
6603}
6604
c906108c 6605void
fba45db2 6606disable_watchpoints_before_interactive_call_start (void)
c906108c 6607{
c5aa993b 6608 struct breakpoint *b;
c906108c
SS
6609
6610 ALL_BREAKPOINTS (b)
c5aa993b 6611 {
cc60f2e3 6612 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6613 {
b5de0fa7 6614 b->enable_state = bp_call_disabled;
b60e7edf 6615 update_global_location_list (0);
c5aa993b
JM
6616 }
6617 }
c906108c
SS
6618}
6619
6620void
fba45db2 6621enable_watchpoints_after_interactive_call_stop (void)
c906108c 6622{
c5aa993b 6623 struct breakpoint *b;
c906108c
SS
6624
6625 ALL_BREAKPOINTS (b)
c5aa993b 6626 {
cc60f2e3 6627 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 6628 {
b5de0fa7 6629 b->enable_state = bp_enabled;
b60e7edf 6630 update_global_location_list (1);
c5aa993b
JM
6631 }
6632 }
c906108c
SS
6633}
6634
8bea4e01
UW
6635void
6636disable_breakpoints_before_startup (void)
6637{
6638 struct breakpoint *b;
6639 int found = 0;
6640
6641 ALL_BREAKPOINTS (b)
6642 {
6c95b8df
PA
6643 if (b->pspace != current_program_space)
6644 continue;
6645
8bea4e01
UW
6646 if ((b->type == bp_breakpoint
6647 || b->type == bp_hardware_breakpoint)
6648 && breakpoint_enabled (b))
6649 {
6650 b->enable_state = bp_startup_disabled;
6651 found = 1;
6652 }
6653 }
6654
6655 if (found)
6656 update_global_location_list (0);
6657
6c95b8df 6658 current_program_space->executing_startup = 1;
8bea4e01
UW
6659}
6660
6661void
6662enable_breakpoints_after_startup (void)
6663{
6664 struct breakpoint *b;
6665 int found = 0;
6666
6c95b8df 6667 current_program_space->executing_startup = 0;
8bea4e01
UW
6668
6669 ALL_BREAKPOINTS (b)
6670 {
6c95b8df
PA
6671 if (b->pspace != current_program_space)
6672 continue;
6673
8bea4e01
UW
6674 if ((b->type == bp_breakpoint
6675 || b->type == bp_hardware_breakpoint)
6676 && b->enable_state == bp_startup_disabled)
6677 {
6678 b->enable_state = bp_enabled;
6679 found = 1;
6680 }
6681 }
6682
6683 if (found)
6684 breakpoint_re_set ();
6685}
6686
c906108c
SS
6687
6688/* Set a breakpoint that will evaporate an end of command
6689 at address specified by SAL.
6690 Restrict it to frame FRAME if FRAME is nonzero. */
6691
6692struct breakpoint *
a6d9a66e
UW
6693set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
6694 struct frame_id frame_id, enum bptype type)
c906108c 6695{
52f0bd74 6696 struct breakpoint *b;
edb3359d
DJ
6697
6698 /* If FRAME_ID is valid, it should be a real frame, not an inlined
6699 one. */
6700 gdb_assert (!frame_id_inlined_p (frame_id));
6701
a6d9a66e 6702 b = set_raw_breakpoint (gdbarch, sal, type);
b5de0fa7
EZ
6703 b->enable_state = bp_enabled;
6704 b->disposition = disp_donttouch;
818dd999 6705 b->frame_id = frame_id;
c906108c
SS
6706
6707 /* If we're debugging a multi-threaded program, then we
6708 want momentary breakpoints to be active in only a
6709 single thread of control. */
39f77062
KB
6710 if (in_thread_list (inferior_ptid))
6711 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 6712
b60e7edf 6713 update_global_location_list_nothrow (1);
74960c60 6714
c906108c
SS
6715 return b;
6716}
611c83ae 6717
e58b0e63
PA
6718/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
6719 ORIG is NULL. */
6720
6721struct breakpoint *
6722clone_momentary_breakpoint (struct breakpoint *orig)
6723{
6724 struct breakpoint *copy;
6725
6726 /* If there's nothing to clone, then return nothing. */
6727 if (orig == NULL)
6728 return NULL;
6729
a6d9a66e 6730 copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
e58b0e63
PA
6731 copy->loc = allocate_bp_location (copy);
6732 set_breakpoint_location_function (copy->loc);
6733
a6d9a66e 6734 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
6735 copy->loc->requested_address = orig->loc->requested_address;
6736 copy->loc->address = orig->loc->address;
6737 copy->loc->section = orig->loc->section;
6c95b8df 6738 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
6739
6740 if (orig->source_file == NULL)
6741 copy->source_file = NULL;
6742 else
6743 copy->source_file = xstrdup (orig->source_file);
6744
6745 copy->line_number = orig->line_number;
6746 copy->frame_id = orig->frame_id;
6747 copy->thread = orig->thread;
6c95b8df 6748 copy->pspace = orig->pspace;
e58b0e63
PA
6749
6750 copy->enable_state = bp_enabled;
6751 copy->disposition = disp_donttouch;
6752 copy->number = internal_breakpoint_number--;
6753
6754 update_global_location_list_nothrow (0);
6755 return copy;
6756}
6757
611c83ae 6758struct breakpoint *
a6d9a66e
UW
6759set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
6760 enum bptype type)
611c83ae
PA
6761{
6762 struct symtab_and_line sal;
6763
6764 sal = find_pc_line (pc, 0);
6765 sal.pc = pc;
6766 sal.section = find_pc_overlay (pc);
6767 sal.explicit_pc = 1;
6768
a6d9a66e 6769 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 6770}
c906108c 6771\f
c5aa993b 6772
c906108c
SS
6773/* Tell the user we have just set a breakpoint B. */
6774
6775static void
fba45db2 6776mention (struct breakpoint *b)
c906108c
SS
6777{
6778 int say_where = 0;
fa8a61dc 6779 struct cleanup *ui_out_chain;
79a45b7d
TT
6780 struct value_print_options opts;
6781
6782 get_user_print_options (&opts);
8b93c638 6783
9a4105ab
AC
6784 /* FIXME: This is misplaced; mention() is called by things (like
6785 hitting a watchpoint) other than breakpoint creation. It should
6786 be possible to clean this up and at the same time replace the
7f4b89d1 6787 random calls to breakpoint_changed with this hook. */
383f836e 6788 observer_notify_breakpoint_created (b->number);
c906108c 6789
3086aeae
DJ
6790 if (b->ops != NULL && b->ops->print_mention != NULL)
6791 b->ops->print_mention (b);
6792 else
6793 switch (b->type)
6794 {
6795 case bp_none:
a3f17187 6796 printf_filtered (_("(apparently deleted?) Eventpoint %d: "), b->number);
3086aeae
DJ
6797 break;
6798 case bp_watchpoint:
6799 ui_out_text (uiout, "Watchpoint ");
6800 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
6801 ui_out_field_int (uiout, "number", b->number);
6802 ui_out_text (uiout, ": ");
fa8a61dc 6803 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
6804 do_cleanups (ui_out_chain);
6805 break;
6806 case bp_hardware_watchpoint:
6807 ui_out_text (uiout, "Hardware watchpoint ");
6808 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
6809 ui_out_field_int (uiout, "number", b->number);
6810 ui_out_text (uiout, ": ");
fa8a61dc 6811 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
6812 do_cleanups (ui_out_chain);
6813 break;
6814 case bp_read_watchpoint:
6815 ui_out_text (uiout, "Hardware read watchpoint ");
6816 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
6817 ui_out_field_int (uiout, "number", b->number);
6818 ui_out_text (uiout, ": ");
fa8a61dc 6819 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
6820 do_cleanups (ui_out_chain);
6821 break;
6822 case bp_access_watchpoint:
6823 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
6824 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
6825 ui_out_field_int (uiout, "number", b->number);
6826 ui_out_text (uiout, ": ");
fa8a61dc 6827 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
6828 do_cleanups (ui_out_chain);
6829 break;
6830 case bp_breakpoint:
6831 if (ui_out_is_mi_like_p (uiout))
6832 {
6833 say_where = 0;
6834 break;
6835 }
2cec12e5
AR
6836 if (b->disposition == disp_del)
6837 printf_filtered (_("Temporary breakpoint"));
6838 else
6839 printf_filtered (_("Breakpoint"));
6840 printf_filtered (_(" %d"), b->number);
3086aeae
DJ
6841 say_where = 1;
6842 break;
6843 case bp_hardware_breakpoint:
6844 if (ui_out_is_mi_like_p (uiout))
6845 {
6846 say_where = 0;
6847 break;
6848 }
a3f17187 6849 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
3086aeae
DJ
6850 say_where = 1;
6851 break;
1042e4c0
SS
6852 case bp_tracepoint:
6853 if (ui_out_is_mi_like_p (uiout))
6854 {
6855 say_where = 0;
6856 break;
6857 }
6858 printf_filtered (_("Tracepoint"));
6859 printf_filtered (_(" %d"), b->number);
6860 say_where = 1;
6861 break;
7a697b8d
SS
6862 case bp_fast_tracepoint:
6863 if (ui_out_is_mi_like_p (uiout))
6864 {
6865 say_where = 0;
6866 break;
6867 }
6868 printf_filtered (_("Fast tracepoint"));
6869 printf_filtered (_(" %d"), b->number);
6870 say_where = 1;
6871 break;
0fb4aa4b
PA
6872 case bp_static_tracepoint:
6873 if (ui_out_is_mi_like_p (uiout))
6874 {
6875 say_where = 0;
6876 break;
6877 }
6878 printf_filtered (_("Static tracepoint"));
6879 printf_filtered (_(" %d"), b->number);
6880 say_where = 1;
6881 break;
3086aeae
DJ
6882
6883 case bp_until:
6884 case bp_finish:
6885 case bp_longjmp:
6886 case bp_longjmp_resume:
186c406b
TT
6887 case bp_exception:
6888 case bp_exception_resume:
3086aeae 6889 case bp_step_resume:
3086aeae 6890 case bp_call_dummy:
aa7d318d 6891 case bp_std_terminate:
3086aeae
DJ
6892 case bp_watchpoint_scope:
6893 case bp_shlib_event:
6894 case bp_thread_event:
6895 case bp_overlay_event:
4efc6507 6896 case bp_jit_event:
0fd8e87f 6897 case bp_longjmp_master:
aa7d318d 6898 case bp_std_terminate_master:
186c406b 6899 case bp_exception_master:
3086aeae
DJ
6900 break;
6901 }
c906108c 6902
c906108c
SS
6903 if (say_where)
6904 {
a3f17187
AC
6905 /* i18n: cagney/2005-02-11: Below needs to be merged into a
6906 single string. */
0d381245 6907 if (b->loc == NULL)
c906108c 6908 {
a3f17187 6909 printf_filtered (_(" (%s) pending."), b->addr_string);
0101ce28
JJ
6910 }
6911 else
6912 {
79a45b7d 6913 if (opts.addressprint || b->source_file == NULL)
0101ce28
JJ
6914 {
6915 printf_filtered (" at ");
5af949e3
UW
6916 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
6917 gdb_stdout);
0101ce28
JJ
6918 }
6919 if (b->source_file)
6920 printf_filtered (": file %s, line %d.",
6921 b->source_file, b->line_number);
0d381245
VP
6922
6923 if (b->loc->next)
6924 {
6925 struct bp_location *loc = b->loc;
6926 int n = 0;
6927 for (; loc; loc = loc->next)
6928 ++n;
6929 printf_filtered (" (%d locations)", n);
6930 }
6931
c906108c 6932 }
c906108c 6933 }
9dc5e2a9 6934 if (ui_out_is_mi_like_p (uiout))
fb40c209 6935 return;
c906108c
SS
6936 printf_filtered ("\n");
6937}
c906108c 6938\f
c5aa993b 6939
0d381245 6940static struct bp_location *
39d61571 6941add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
6942 const struct symtab_and_line *sal)
6943{
6944 struct bp_location *loc, **tmp;
6945
39d61571 6946 loc = allocate_bp_location (b);
0d381245
VP
6947 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
6948 ;
6949 *tmp = loc;
a6d9a66e
UW
6950 loc->gdbarch = get_sal_arch (*sal);
6951 if (!loc->gdbarch)
6952 loc->gdbarch = b->gdbarch;
0d381245 6953 loc->requested_address = sal->pc;
a6d9a66e
UW
6954 loc->address = adjust_breakpoint_address (loc->gdbarch,
6955 loc->requested_address, b->type);
6c95b8df
PA
6956 loc->pspace = sal->pspace;
6957 gdb_assert (loc->pspace != NULL);
0d381245
VP
6958 loc->section = sal->section;
6959
6960 set_breakpoint_location_function (loc);
6961 return loc;
6962}
514f746b
AR
6963\f
6964
6965/* Return 1 if LOC is pointing to a permanent breakpoint,
6966 return 0 otherwise. */
6967
6968static int
6969bp_loc_is_permanent (struct bp_location *loc)
6970{
6971 int len;
6972 CORE_ADDR addr;
6973 const gdb_byte *brk;
6974 gdb_byte *target_mem;
939c61fa
JK
6975 struct cleanup *cleanup;
6976 int retval = 0;
514f746b
AR
6977
6978 gdb_assert (loc != NULL);
6979
6980 addr = loc->address;
a6d9a66e 6981 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 6982
939c61fa
JK
6983 /* Software breakpoints unsupported? */
6984 if (brk == NULL)
6985 return 0;
6986
514f746b
AR
6987 target_mem = alloca (len);
6988
939c61fa
JK
6989 /* Enable the automatic memory restoration from breakpoints while
6990 we read the memory. Otherwise we could say about our temporary
6991 breakpoints they are permanent. */
6c95b8df
PA
6992 cleanup = save_current_space_and_thread ();
6993
6994 switch_to_program_space_and_thread (loc->pspace);
6995 make_show_memory_breakpoints_cleanup (0);
939c61fa 6996
514f746b
AR
6997 if (target_read_memory (loc->address, target_mem, len) == 0
6998 && memcmp (target_mem, brk, len) == 0)
939c61fa 6999 retval = 1;
514f746b 7000
939c61fa
JK
7001 do_cleanups (cleanup);
7002
7003 return retval;
514f746b
AR
7004}
7005
7006
c3f6f71d 7007
018d34a4
VP
7008/* Create a breakpoint with SAL as location. Use ADDR_STRING
7009 as textual description of the location, and COND_STRING
db107f19 7010 as condition expression. */
018d34a4
VP
7011
7012static void
8cdf0e15
VP
7013create_breakpoint_sal (struct gdbarch *gdbarch,
7014 struct symtabs_and_lines sals, char *addr_string,
7015 char *cond_string,
7016 enum bptype type, enum bpdisp disposition,
7017 int thread, int task, int ignore_count,
84f4c1fe
PM
7018 struct breakpoint_ops *ops, int from_tty,
7019 int enabled, int internal)
018d34a4 7020{
0d381245
VP
7021 struct breakpoint *b = NULL;
7022 int i;
018d34a4
VP
7023
7024 if (type == bp_hardware_breakpoint)
7025 {
7026 int i = hw_breakpoint_used_count ();
7027 int target_resources_ok =
d92524f1 7028 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7029 i + 1, 0);
7030 if (target_resources_ok == 0)
7031 error (_("No hardware breakpoint support in the target."));
7032 else if (target_resources_ok < 0)
7033 error (_("Hardware breakpoints used exceeds limit."));
7034 }
7035
6c95b8df
PA
7036 gdb_assert (sals.nelts > 0);
7037
0d381245
VP
7038 for (i = 0; i < sals.nelts; ++i)
7039 {
7040 struct symtab_and_line sal = sals.sals[i];
7041 struct bp_location *loc;
7042
7043 if (from_tty)
5af949e3
UW
7044 {
7045 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7046 if (!loc_gdbarch)
7047 loc_gdbarch = gdbarch;
7048
7049 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7050 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7051 }
0d381245
VP
7052
7053 if (i == 0)
7054 {
a6d9a66e 7055 b = set_raw_breakpoint (gdbarch, sal, type);
84f4c1fe 7056 set_breakpoint_number (internal, b);
0d381245 7057 b->thread = thread;
4a306c9a 7058 b->task = task;
018d34a4 7059
0d381245
VP
7060 b->cond_string = cond_string;
7061 b->ignore_count = ignore_count;
41447f92 7062 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7063 b->disposition = disposition;
6c95b8df
PA
7064 b->pspace = sals.sals[0].pspace;
7065
0fb4aa4b
PA
7066 if (type == bp_static_tracepoint)
7067 {
7068 struct static_tracepoint_marker marker;
7069
7070 if (is_marker_spec (addr_string))
7071 {
7072 /* We already know the marker exists, otherwise, we
7073 wouldn't see a sal for it. */
7074 char *p = &addr_string[3];
7075 char *endp;
7076 char *marker_str;
7077 int i;
7078
7079 while (*p == ' ' || *p == '\t')
7080 p++;
7081
7082 endp = p;
7083 while (*endp != ' ' && *endp != '\t' && *endp != '\0')
7084 endp++;
7085
7086 marker_str = savestring (p, endp - p);
7087 b->static_trace_marker_id = marker_str;
7088
7089 printf_filtered (_("Probed static tracepoint marker \"%s\"\n"),
7090 b->static_trace_marker_id);
7091 }
7092 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7093 {
7094 b->static_trace_marker_id = xstrdup (marker.str_id);
7095 release_static_tracepoint_marker (&marker);
7096
7097 printf_filtered (_("Probed static tracepoint marker \"%s\"\n"),
7098 b->static_trace_marker_id);
7099 }
7100 else
7101 warning (_("\
7102Couldn't determine the static tracepoint marker to probe"));
7103 }
7104
6c95b8df 7105 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7106 && (b->type == bp_breakpoint
7107 || b->type == bp_hardware_breakpoint))
7108 b->enable_state = bp_startup_disabled;
7109
0d381245
VP
7110 loc = b->loc;
7111 }
7112 else
018d34a4 7113 {
39d61571 7114 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7115 }
7116
514f746b
AR
7117 if (bp_loc_is_permanent (loc))
7118 make_breakpoint_permanent (b);
7119
0d381245
VP
7120 if (b->cond_string)
7121 {
7122 char *arg = b->cond_string;
d32a6982 7123 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7124 if (*arg)
db107f19 7125 error (_("Garbage %s follows condition"), arg);
018d34a4 7126 }
0d381245 7127 }
018d34a4
VP
7128
7129 if (addr_string)
7130 b->addr_string = addr_string;
7131 else
7132 /* addr_string has to be used or breakpoint_re_set will delete
7133 me. */
5af949e3
UW
7134 b->addr_string
7135 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
018d34a4 7136
604133b5 7137 b->ops = ops;
84f4c1fe
PM
7138 if (internal)
7139 /* Do not mention breakpoints with a negative number, but do
7140 notify observers. */
7141 observer_notify_breakpoint_created (b->number);
7142 else
7143 mention (b);
018d34a4
VP
7144}
7145
ed0616c6
VP
7146/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7147 elements to fill the void space. */
2c0b251b
PA
7148static void
7149remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7150{
7151 int i = index_to_remove+1;
7152 int last_index = sal->nelts-1;
7153
7154 for (;i <= last_index; ++i)
7155 sal->sals[i-1] = sal->sals[i];
7156
7157 --(sal->nelts);
7158}
7159
6c95b8df
PA
7160/* If appropriate, obtains all sals that correspond to the same file
7161 and line as SAL, in all program spaces. Users debugging with IDEs,
7162 will want to set a breakpoint at foo.c:line, and not really care
7163 about program spaces. This is done only if SAL does not have
7164 explicit PC and has line and file information. If we got just a
7165 single expanded sal, return the original.
ed0616c6 7166
6c95b8df
PA
7167 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7168 which the name of enclosing function is different from SAL. This
7169 makes sure that if we have breakpoint originally set in template
7170 instantiation, say foo<int>(), we won't expand SAL to locations at
7171 the same line in all existing instantiations of 'foo'. */
ed0616c6 7172
2c0b251b 7173static struct symtabs_and_lines
ed0616c6
VP
7174expand_line_sal_maybe (struct symtab_and_line sal)
7175{
7176 struct symtabs_and_lines expanded;
7177 CORE_ADDR original_pc = sal.pc;
7178 char *original_function = NULL;
7179 int found;
7180 int i;
6c95b8df 7181 struct cleanup *old_chain;
ed0616c6
VP
7182
7183 /* If we have explicit pc, don't expand.
7184 If we have no line number, we can't expand. */
7185 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7186 {
7187 expanded.nelts = 1;
7188 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7189 expanded.sals[0] = sal;
7190 return expanded;
7191 }
7192
7193 sal.pc = 0;
6c95b8df
PA
7194
7195 old_chain = save_current_space_and_thread ();
7196
7197 switch_to_program_space_and_thread (sal.pspace);
7198
ed0616c6 7199 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7200
7201 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7202 expanded = expand_line_sal (sal);
6c95b8df 7203
ed0616c6
VP
7204 if (expanded.nelts == 1)
7205 {
3dba1c98
JB
7206 /* We had one sal, we got one sal. Return that sal, adjusting it
7207 past the function prologue if necessary. */
ed0616c6
VP
7208 xfree (expanded.sals);
7209 expanded.nelts = 1;
7210 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7211 sal.pc = original_pc;
7212 expanded.sals[0] = sal;
3dba1c98 7213 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7214 do_cleanups (old_chain);
ed0616c6
VP
7215 return expanded;
7216 }
7217
7218 if (!sal.explicit_line)
7219 {
7220 CORE_ADDR func_addr, func_end;
7221 for (i = 0; i < expanded.nelts; ++i)
7222 {
7223 CORE_ADDR pc = expanded.sals[i].pc;
7224 char *this_function;
6c95b8df
PA
7225
7226 /* We need to switch threads as well since we're about to
7227 read memory. */
7228 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7229
ed0616c6
VP
7230 if (find_pc_partial_function (pc, &this_function,
7231 &func_addr, &func_end))
7232 {
059fb39f
PM
7233 if (this_function
7234 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7235 {
7236 remove_sal (&expanded, i);
7237 --i;
7238 }
ed0616c6
VP
7239 }
7240 }
7241 }
059acae7
UW
7242
7243 /* Skip the function prologue if necessary. */
7244 for (i = 0; i < expanded.nelts; ++i)
7245 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7246
6c95b8df
PA
7247 do_cleanups (old_chain);
7248
ed0616c6
VP
7249 if (expanded.nelts <= 1)
7250 {
7251 /* This is un ugly workaround. If we get zero
7252 expanded sals then something is really wrong.
7253 Fix that by returnign the original sal. */
7254 xfree (expanded.sals);
7255 expanded.nelts = 1;
7256 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7257 sal.pc = original_pc;
7258 expanded.sals[0] = sal;
7259 return expanded;
7260 }
7261
7262 if (original_pc)
7263 {
7264 found = 0;
7265 for (i = 0; i < expanded.nelts; ++i)
7266 if (expanded.sals[i].pc == original_pc)
7267 {
7268 found = 1;
7269 break;
7270 }
7271 gdb_assert (found);
7272 }
7273
7274 return expanded;
7275}
7276
018d34a4
VP
7277/* Add SALS.nelts breakpoints to the breakpoint table. For each
7278 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7279 value. COND_STRING, if not NULL, specified the condition to be
7280 used for all breakpoints. Essentially the only case where
7281 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7282 function. In that case, it's still not possible to specify
7283 separate conditions for different overloaded functions, so
7284 we take just a single condition string.
7285
c3f6f71d 7286 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7287 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7288 array contents). If the function fails (error() is called), the
7289 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
7290 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7291
7292static void
8cdf0e15
VP
7293create_breakpoints_sal (struct gdbarch *gdbarch,
7294 struct symtabs_and_lines sals, char **addr_string,
7295 char *cond_string,
7296 enum bptype type, enum bpdisp disposition,
7297 int thread, int task, int ignore_count,
7298 struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7299 int enabled, int internal)
c906108c 7300{
018d34a4 7301 int i;
cc59ec59 7302
018d34a4 7303 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7304 {
ed0616c6
VP
7305 struct symtabs_and_lines expanded =
7306 expand_line_sal_maybe (sals.sals[i]);
0d381245 7307
8cdf0e15
VP
7308 create_breakpoint_sal (gdbarch, expanded, addr_string[i],
7309 cond_string, type, disposition,
84f4c1fe
PM
7310 thread, task, ignore_count, ops,
7311 from_tty, enabled, internal);
c3f6f71d 7312 }
c3f6f71d 7313}
c906108c 7314
c3f6f71d
JM
7315/* Parse ARG which is assumed to be a SAL specification possibly
7316 followed by conditionals. On return, SALS contains an array of SAL
7317 addresses found. ADDR_STRING contains a vector of (canonical)
7318 address strings. ARG points to the end of the SAL. */
c906108c 7319
b9362cc7 7320static void
c3f6f71d
JM
7321parse_breakpoint_sals (char **address,
7322 struct symtabs_and_lines *sals,
0101ce28
JJ
7323 char ***addr_string,
7324 int *not_found_ptr)
c3f6f71d
JM
7325{
7326 char *addr_start = *address;
cc59ec59 7327
c3f6f71d
JM
7328 *addr_string = NULL;
7329 /* If no arg given, or if first arg is 'if ', use the default
7330 breakpoint. */
7331 if ((*address) == NULL
7332 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c
SS
7333 {
7334 if (default_breakpoint_valid)
7335 {
c3f6f71d 7336 struct symtab_and_line sal;
cc59ec59 7337
fe39c653 7338 init_sal (&sal); /* initialize to zeroes */
c3f6f71d 7339 sals->sals = (struct symtab_and_line *)
c906108c
SS
7340 xmalloc (sizeof (struct symtab_and_line));
7341 sal.pc = default_breakpoint_address;
7342 sal.line = default_breakpoint_line;
7343 sal.symtab = default_breakpoint_symtab;
6c95b8df 7344 sal.pspace = default_breakpoint_pspace;
c5aa993b 7345 sal.section = find_pc_overlay (sal.pc);
00903456
JK
7346
7347 /* "break" without arguments is equivalent to "break *PC" where PC is
7348 the default_breakpoint_address. So make sure to set
7349 sal.explicit_pc to prevent GDB from trying to expand the list of
7350 sals to include all other instances with the same symtab and line.
7351 */
7352 sal.explicit_pc = 1;
7353
c3f6f71d
JM
7354 sals->sals[0] = sal;
7355 sals->nelts = 1;
c906108c
SS
7356 }
7357 else
8a3fe4f8 7358 error (_("No default breakpoint address now."));
c906108c
SS
7359 }
7360 else
7361 {
c906108c 7362 /* Force almost all breakpoints to be in terms of the
c5aa993b
JM
7363 current_source_symtab (which is decode_line_1's default). This
7364 should produce the results we want almost all of the time while
1aeae86e
AF
7365 leaving default_breakpoint_* alone.
7366 ObjC: However, don't match an Objective-C method name which
7367 may have a '+' or '-' succeeded by a '[' */
0378c332 7368
c214a6fd 7369 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 7370
c906108c 7371 if (default_breakpoint_valid
0378c332 7372 && (!cursal.symtab
1aeae86e
AF
7373 || ((strchr ("+-", (*address)[0]) != NULL)
7374 && ((*address)[1] != '['))))
c3f6f71d 7375 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
0101ce28
JJ
7376 default_breakpoint_line, addr_string,
7377 not_found_ptr);
c906108c 7378 else
0101ce28
JJ
7379 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
7380 addr_string, not_found_ptr);
c906108c 7381 }
c3f6f71d
JM
7382 /* For any SAL that didn't have a canonical string, fill one in. */
7383 if (sals->nelts > 0 && *addr_string == NULL)
7384 *addr_string = xcalloc (sals->nelts, sizeof (char **));
7385 if (addr_start != (*address))
c906108c 7386 {
c3f6f71d 7387 int i;
cc59ec59 7388
c3f6f71d 7389 for (i = 0; i < sals->nelts; i++)
c906108c 7390 {
c3f6f71d
JM
7391 /* Add the string if not present. */
7392 if ((*addr_string)[i] == NULL)
cc59ec59
MS
7393 (*addr_string)[i] = savestring (addr_start,
7394 (*address) - addr_start);
c906108c
SS
7395 }
7396 }
c3f6f71d 7397}
c906108c 7398
c906108c 7399
c3f6f71d
JM
7400/* Convert each SAL into a real PC. Verify that the PC can be
7401 inserted as a breakpoint. If it can't throw an error. */
c906108c 7402
b9362cc7 7403static void
23e7acfb 7404breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7405{
7406 int i;
cc59ec59 7407
c3f6f71d 7408 for (i = 0; i < sals->nelts; i++)
ee53e872 7409 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7410}
7411
7a697b8d
SS
7412/* Fast tracepoints may have restrictions on valid locations. For
7413 instance, a fast tracepoint using a jump instead of a trap will
7414 likely have to overwrite more bytes than a trap would, and so can
7415 only be placed where the instruction is longer than the jump, or a
7416 multi-instruction sequence does not have a jump into the middle of
7417 it, etc. */
7418
7419static void
7420check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7421 struct symtabs_and_lines *sals)
7422{
7423 int i, rslt;
7424 struct symtab_and_line *sal;
7425 char *msg;
7426 struct cleanup *old_chain;
7427
7428 for (i = 0; i < sals->nelts; i++)
7429 {
7430 sal = &sals->sals[i];
7431
7432 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7433 NULL, &msg);
7434 old_chain = make_cleanup (xfree, msg);
7435
7436 if (!rslt)
7437 error (_("May not have a fast tracepoint at 0x%s%s"),
7438 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7439
7440 do_cleanups (old_chain);
7441 }
7442}
7443
05ff989b 7444static void
0101ce28
JJ
7445do_captured_parse_breakpoint (struct ui_out *ui, void *data)
7446{
7447 struct captured_parse_breakpoint_args *args = data;
7448
7449 parse_breakpoint_sals (args->arg_p, args->sals_p, args->addr_string_p,
7450 args->not_found_ptr);
0101ce28
JJ
7451}
7452
018d34a4
VP
7453/* Given TOK, a string specification of condition and thread, as
7454 accepted by the 'break' command, extract the condition
7455 string and thread number and set *COND_STRING and *THREAD.
7456 PC identifies the context at which the condition should be parsed.
7457 If no condition is found, *COND_STRING is set to NULL.
7458 If no thread is found, *THREAD is set to -1. */
7459static void
7460find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7461 char **cond_string, int *thread, int *task)
018d34a4
VP
7462{
7463 *cond_string = NULL;
7464 *thread = -1;
7465 while (tok && *tok)
7466 {
7467 char *end_tok;
7468 int toklen;
7469 char *cond_start = NULL;
7470 char *cond_end = NULL;
cc59ec59 7471
018d34a4
VP
7472 while (*tok == ' ' || *tok == '\t')
7473 tok++;
7474
7475 end_tok = tok;
7476
7477 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
7478 end_tok++;
7479
7480 toklen = end_tok - tok;
7481
7482 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7483 {
f7545552
TT
7484 struct expression *expr;
7485
018d34a4 7486 tok = cond_start = end_tok + 1;
f7545552
TT
7487 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7488 xfree (expr);
018d34a4
VP
7489 cond_end = tok;
7490 *cond_string = savestring (cond_start,
7491 cond_end - cond_start);
7492 }
7493 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7494 {
7495 char *tmptok;
7496
7497 tok = end_tok + 1;
7498 tmptok = tok;
7499 *thread = strtol (tok, &tok, 0);
7500 if (tok == tmptok)
7501 error (_("Junk after thread keyword."));
7502 if (!valid_thread_id (*thread))
7503 error (_("Unknown thread %d."), *thread);
7504 }
4a306c9a
JB
7505 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7506 {
7507 char *tmptok;
7508
7509 tok = end_tok + 1;
7510 tmptok = tok;
7511 *task = strtol (tok, &tok, 0);
7512 if (tok == tmptok)
7513 error (_("Junk after task keyword."));
7514 if (!valid_task_id (*task))
b6199126 7515 error (_("Unknown task %d."), *task);
4a306c9a 7516 }
018d34a4
VP
7517 else
7518 error (_("Junk at end of arguments."));
7519 }
7520}
7521
0fb4aa4b
PA
7522/* Decode a static tracepoint marker spec. */
7523
7524static struct symtabs_and_lines
7525decode_static_tracepoint_spec (char **arg_p)
7526{
7527 VEC(static_tracepoint_marker_p) *markers = NULL;
7528 struct symtabs_and_lines sals;
7529 struct symtab_and_line sal;
7530 struct symbol *sym;
7531 struct cleanup *old_chain;
7532 char *p = &(*arg_p)[3];
7533 char *endp;
7534 char *marker_str;
7535 int i;
7536
7537 while (*p == ' ' || *p == '\t')
7538 p++;
7539
7540 endp = p;
7541 while (*endp != ' ' && *endp != '\t' && *endp != '\0')
7542 endp++;
7543
7544 marker_str = savestring (p, endp - p);
7545 old_chain = make_cleanup (xfree, marker_str);
7546
7547 markers = target_static_tracepoint_markers_by_strid (marker_str);
7548 if (VEC_empty(static_tracepoint_marker_p, markers))
7549 error (_("No known static tracepoint marker named %s"), marker_str);
7550
7551 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7552 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7553
7554 for (i = 0; i < sals.nelts; i++)
7555 {
7556 struct static_tracepoint_marker *marker;
7557
7558 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7559
7560 init_sal (&sals.sals[i]);
7561
7562 sals.sals[i] = find_pc_line (marker->address, 0);
7563 sals.sals[i].pc = marker->address;
7564
7565 release_static_tracepoint_marker (marker);
7566 }
7567
7568 do_cleanups (old_chain);
7569
7570 *arg_p = endp;
7571 return sals;
7572}
7573
fd9b8c24
PA
7574/* Set a breakpoint. This function is shared between CLI and MI
7575 functions for setting a breakpoint. This function has two major
7576 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7577 parameter. If non-zero, the function will parse arg, extracting
7578 breakpoint location, address and thread. Otherwise, ARG is just the
7579 location of breakpoint, with condition and thread specified by the
84f4c1fe
PM
7580 COND_STRING and THREAD parameters. If INTERNAL is non-zero, the
7581 breakpoint number will be allocated from the internal breakpoint
7582 count. Returns true if any breakpoint was created; false
7583 otherwise. */
0101ce28 7584
8cdf0e15
VP
7585int
7586create_breakpoint (struct gdbarch *gdbarch,
7587 char *arg, char *cond_string, int thread,
7588 int parse_condition_and_thread,
0fb4aa4b 7589 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7590 int ignore_count,
7591 enum auto_boolean pending_break_support,
7592 struct breakpoint_ops *ops,
84f4c1fe 7593 int from_tty, int enabled, int internal)
c3f6f71d 7594{
71fff37b 7595 struct gdb_exception e;
c3f6f71d 7596 struct symtabs_and_lines sals;
0101ce28 7597 struct symtab_and_line pending_sal;
0101ce28 7598 char *copy_arg;
c3f6f71d
JM
7599 char *addr_start = arg;
7600 char **addr_string;
7601 struct cleanup *old_chain;
80c99de1 7602 struct cleanup *bkpt_chain = NULL;
0101ce28 7603 struct captured_parse_breakpoint_args parse_args;
05ff989b 7604 int i;
0101ce28 7605 int pending = 0;
0101ce28 7606 int not_found = 0;
4a306c9a 7607 int task = 0;
86b17b60 7608 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7609
c3f6f71d
JM
7610 sals.sals = NULL;
7611 sals.nelts = 0;
7612 addr_string = NULL;
c3f6f71d 7613
0101ce28
JJ
7614 parse_args.arg_p = &arg;
7615 parse_args.sals_p = &sals;
7616 parse_args.addr_string_p = &addr_string;
7617 parse_args.not_found_ptr = &not_found;
7618
0fb4aa4b
PA
7619 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7620 {
7621 int i;
7622
7623 sals = decode_static_tracepoint_spec (&arg);
7624
7625 copy_arg = savestring (addr_start, arg - addr_start);
7626 addr_string = xcalloc (sals.nelts, sizeof (char **));
7627 for (i = 0; i < sals.nelts; i++)
7628 addr_string[i] = xstrdup (copy_arg);
7629 goto done;
7630 }
7631
05ff989b
AC
7632 e = catch_exception (uiout, do_captured_parse_breakpoint,
7633 &parse_args, RETURN_MASK_ALL);
0101ce28
JJ
7634
7635 /* If caller is interested in rc value from parse, set value. */
05ff989b 7636 switch (e.reason)
0101ce28 7637 {
05ff989b 7638 case RETURN_QUIT:
98deb0da 7639 throw_exception (e);
05ff989b
AC
7640 case RETURN_ERROR:
7641 switch (e.error)
0101ce28 7642 {
05ff989b 7643 case NOT_FOUND_ERROR:
0101ce28 7644
05ff989b
AC
7645 /* If pending breakpoint support is turned off, throw
7646 error. */
fa8d40ab
JJ
7647
7648 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7649 throw_exception (e);
7650
7651 exception_print (gdb_stderr, e);
fa8d40ab 7652
05ff989b
AC
7653 /* If pending breakpoint support is auto query and the user
7654 selects no, then simply return the error code. */
059fb39f 7655 if (pending_break_support == AUTO_BOOLEAN_AUTO
9bd89d67 7656 && !nquery (_("Make breakpoint pending on future shared library load? ")))
fd9b8c24 7657 return 0;
fa8d40ab 7658
05ff989b
AC
7659 /* At this point, either the user was queried about setting
7660 a pending breakpoint and selected yes, or pending
7661 breakpoint behavior is on and thus a pending breakpoint
7662 is defaulted on behalf of the user. */
0101ce28
JJ
7663 copy_arg = xstrdup (addr_start);
7664 addr_string = &copy_arg;
7665 sals.nelts = 1;
7666 sals.sals = &pending_sal;
7667 pending_sal.pc = 0;
7668 pending = 1;
05ff989b
AC
7669 break;
7670 default:
98deb0da 7671 throw_exception (e);
0101ce28 7672 }
05ff989b
AC
7673 default:
7674 if (!sals.nelts)
fd9b8c24 7675 return 0;
0101ce28 7676 }
c3f6f71d 7677
0fb4aa4b
PA
7678 done:
7679
c3f6f71d
JM
7680 /* Create a chain of things that always need to be cleaned up. */
7681 old_chain = make_cleanup (null_cleanup, 0);
7682
0101ce28
JJ
7683 if (!pending)
7684 {
7685 /* Make sure that all storage allocated to SALS gets freed. */
7686 make_cleanup (xfree, sals.sals);
7687
7688 /* Cleanup the addr_string array but not its contents. */
7689 make_cleanup (xfree, addr_string);
7690 }
c3f6f71d 7691
c3f6f71d
JM
7692 /* ----------------------------- SNIP -----------------------------
7693 Anything added to the cleanup chain beyond this point is assumed
7694 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7695 then the memory is not reclaimed. */
7696 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d
JM
7697
7698 /* Mark the contents of the addr_string for cleanup. These go on
80c99de1 7699 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
7700 for (i = 0; i < sals.nelts; i++)
7701 {
7702 if (addr_string[i] != NULL)
b8c9b27d 7703 make_cleanup (xfree, addr_string[i]);
c3f6f71d
JM
7704 }
7705
7706 /* Resolve all line numbers to PC's and verify that the addresses
7707 are ok for the target. */
0101ce28 7708 if (!pending)
23e7acfb 7709 breakpoint_sals_to_pc (&sals);
c3f6f71d 7710
7a697b8d
SS
7711 /* Fast tracepoints may have additional restrictions on location. */
7712 if (type_wanted == bp_fast_tracepoint)
7713 check_fast_tracepoint_sals (gdbarch, &sals);
7714
c3f6f71d
JM
7715 /* Verify that condition can be parsed, before setting any
7716 breakpoints. Allocate a separate condition expression for each
7717 breakpoint. */
0101ce28 7718 if (!pending)
c3f6f71d 7719 {
2f069f6f 7720 if (parse_condition_and_thread)
72b2ff0e
VP
7721 {
7722 /* Here we only parse 'arg' to separate condition
7723 from thread number, so parsing in context of first
7724 sal is OK. When setting the breakpoint we'll
7725 re-parse it in context of each sal. */
7726 cond_string = NULL;
7727 thread = -1;
4a306c9a
JB
7728 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
7729 &thread, &task);
72b2ff0e
VP
7730 if (cond_string)
7731 make_cleanup (xfree, cond_string);
7732 }
2f069f6f 7733 else
72b2ff0e
VP
7734 {
7735 /* Create a private copy of condition string. */
7736 if (cond_string)
7737 {
7738 cond_string = xstrdup (cond_string);
7739 make_cleanup (xfree, cond_string);
7740 }
7741 }
0fb4aa4b
PA
7742
7743 /* If the user is creating a static tracepoint by marker id
7744 (strace -m MARKER_ID), then store the sals index, so that
7745 breakpoint_re_set can try to match up which of the newly
7746 found markers corresponds to this one, and, don't try to
7747 expand multiple locations for each sal, given than SALS
7748 already should contain all sals for MARKER_ID. */
7749 if (type_wanted == bp_static_tracepoint
7750 && is_marker_spec (addr_string[0]))
7751 {
7752 int i;
7753
7754 for (i = 0; i < sals.nelts; ++i)
7755 {
7756 struct symtabs_and_lines expanded;
7757 struct breakpoint *tp;
7758 struct cleanup *old_chain;
7759
7760 expanded.nelts = 1;
7761 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7762 expanded.sals[0] = sals.sals[i];
7763 old_chain = make_cleanup (xfree, expanded.sals);
7764
7765 create_breakpoint_sal (gdbarch, expanded, addr_string[i],
7766 cond_string, type_wanted,
7767 tempflag ? disp_del : disp_donttouch,
7768 thread, task, ignore_count, ops,
84f4c1fe 7769 from_tty, enabled, internal);
0fb4aa4b
PA
7770
7771 do_cleanups (old_chain);
7772
7773 /* Get the tracepoint we just created. */
84f4c1fe
PM
7774 if (internal)
7775 tp = get_breakpoint (internal_breakpoint_number);
7776 else
7777 tp = get_breakpoint (breakpoint_count);
0fb4aa4b
PA
7778 gdb_assert (tp != NULL);
7779
7780 /* Given that its possible to have multiple markers with
7781 the same string id, if the user is creating a static
7782 tracepoint by marker id ("strace -m MARKER_ID"), then
7783 store the sals index, so that breakpoint_re_set can
7784 try to match up which of the newly found markers
7785 corresponds to this one */
7786 tp->static_trace_marker_id_idx = i;
7787 }
7788 }
7789 else
7790 create_breakpoints_sal (gdbarch, sals, addr_string, cond_string,
7791 type_wanted, tempflag ? disp_del : disp_donttouch,
7792 thread, task, ignore_count, ops, from_tty,
84f4c1fe 7793 enabled, internal);
c906108c 7794 }
0101ce28
JJ
7795 else
7796 {
0101ce28
JJ
7797 struct breakpoint *b;
7798
0101ce28
JJ
7799 make_cleanup (xfree, copy_arg);
7800
a6d9a66e 7801 b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
84f4c1fe 7802 set_breakpoint_number (internal, b);
72b2ff0e 7803 b->thread = -1;
018d34a4 7804 b->addr_string = addr_string[0];
72b2ff0e 7805 b->cond_string = NULL;
0101ce28 7806 b->ignore_count = ignore_count;
0101ce28 7807 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 7808 b->condition_not_parsed = 1;
604133b5 7809 b->ops = ops;
41447f92 7810 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 7811 b->pspace = current_program_space;
84f4c1fe 7812 b->py_bp_object = NULL;
74960c60 7813
6c95b8df 7814 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7815 && (b->type == bp_breakpoint
7816 || b->type == bp_hardware_breakpoint))
7817 b->enable_state = bp_startup_disabled;
7818
84f4c1fe
PM
7819 if (internal)
7820 /* Do not mention breakpoints with a negative number,
7821 but do notify observers. */
7822 observer_notify_breakpoint_created (b->number);
7823 else
7824 mention (b);
0101ce28
JJ
7825 }
7826
c3f6f71d 7827 if (sals.nelts > 1)
95a42b64
TT
7828 {
7829 warning (_("Multiple breakpoints were set.\n"
7830 "Use the \"delete\" command to delete unwanted breakpoints."));
86b17b60 7831 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
7832 }
7833
80c99de1
PA
7834 /* That's it. Discard the cleanups for data inserted into the
7835 breakpoint. */
7836 discard_cleanups (bkpt_chain);
7837 /* But cleanup everything else. */
c3f6f71d 7838 do_cleanups (old_chain);
217dc9e2 7839
80c99de1 7840 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 7841 update_global_location_list (1);
fd9b8c24
PA
7842
7843 return 1;
c3f6f71d 7844}
c906108c 7845
72b2ff0e
VP
7846/* Set a breakpoint.
7847 ARG is a string describing breakpoint address,
7848 condition, and thread.
7849 FLAG specifies if a breakpoint is hardware on,
7850 and if breakpoint is temporary, using BP_HARDWARE_FLAG
7851 and BP_TEMPFLAG. */
7852
98deb0da 7853static void
72b2ff0e 7854break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 7855{
72b2ff0e 7856 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
7857 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
7858 ? bp_hardware_breakpoint
7859 : bp_breakpoint);
c3f6f71d 7860
8cdf0e15
VP
7861 create_breakpoint (get_current_arch (),
7862 arg,
7863 NULL, 0, 1 /* parse arg */,
0fb4aa4b 7864 tempflag, type_wanted,
8cdf0e15
VP
7865 0 /* Ignore count */,
7866 pending_break_support,
7867 NULL /* breakpoint_ops */,
7868 from_tty,
84f4c1fe
PM
7869 1 /* enabled */,
7870 0 /* internal */);
c906108c
SS
7871}
7872
72b2ff0e 7873
c906108c
SS
7874/* Helper function for break_command_1 and disassemble_command. */
7875
7876void
fba45db2 7877resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
7878{
7879 CORE_ADDR pc;
7880
7881 if (sal->pc == 0 && sal->symtab != NULL)
7882 {
7883 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 7884 error (_("No line %d in file \"%s\"."),
c906108c
SS
7885 sal->line, sal->symtab->filename);
7886 sal->pc = pc;
6a048695
JB
7887
7888 /* If this SAL corresponds to a breakpoint inserted using
7889 a line number, then skip the function prologue if necessary. */
7890 if (sal->explicit_line)
059acae7 7891 skip_prologue_sal (sal);
c906108c
SS
7892 }
7893
7894 if (sal->section == 0 && sal->symtab != NULL)
7895 {
7896 struct blockvector *bv;
c5aa993b
JM
7897 struct block *b;
7898 struct symbol *sym;
c906108c 7899
801e3a5b 7900 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
7901 if (bv != NULL)
7902 {
7f0df278 7903 sym = block_linkage_function (b);
c906108c
SS
7904 if (sym != NULL)
7905 {
7906 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 7907 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
7908 }
7909 else
7910 {
7911 /* It really is worthwhile to have the section, so we'll just
c5aa993b
JM
7912 have to look harder. This case can be executed if we have
7913 line numbers but no functions (as can happen in assembly
7914 source). */
c906108c 7915
c5aa993b 7916 struct minimal_symbol *msym;
6c95b8df
PA
7917 struct cleanup *old_chain = save_current_space_and_thread ();
7918
7919 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
7920
7921 msym = lookup_minimal_symbol_by_pc (sal->pc);
7922 if (msym)
714835d5 7923 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
7924
7925 do_cleanups (old_chain);
c906108c
SS
7926 }
7927 }
7928 }
7929}
7930
7931void
fba45db2 7932break_command (char *arg, int from_tty)
c906108c 7933{
db107f19 7934 break_command_1 (arg, 0, from_tty);
c906108c
SS
7935}
7936
c906108c 7937void
fba45db2 7938tbreak_command (char *arg, int from_tty)
c906108c 7939{
db107f19 7940 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
7941}
7942
c906108c 7943static void
fba45db2 7944hbreak_command (char *arg, int from_tty)
c906108c 7945{
db107f19 7946 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
7947}
7948
7949static void
fba45db2 7950thbreak_command (char *arg, int from_tty)
c906108c 7951{
db107f19 7952 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
7953}
7954
7955static void
fba45db2 7956stop_command (char *arg, int from_tty)
c906108c 7957{
a3f17187 7958 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 7959Usage: stop in <function | address>\n\
a3f17187 7960 stop at <line>\n"));
c906108c
SS
7961}
7962
7963static void
fba45db2 7964stopin_command (char *arg, int from_tty)
c906108c
SS
7965{
7966 int badInput = 0;
7967
c5aa993b 7968 if (arg == (char *) NULL)
c906108c
SS
7969 badInput = 1;
7970 else if (*arg != '*')
7971 {
7972 char *argptr = arg;
7973 int hasColon = 0;
7974
53a5351d
JM
7975 /* look for a ':'. If this is a line number specification, then
7976 say it is bad, otherwise, it should be an address or
7977 function/method name */
c906108c 7978 while (*argptr && !hasColon)
c5aa993b
JM
7979 {
7980 hasColon = (*argptr == ':');
7981 argptr++;
7982 }
c906108c
SS
7983
7984 if (hasColon)
c5aa993b 7985 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 7986 else
c5aa993b 7987 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
7988 }
7989
7990 if (badInput)
a3f17187 7991 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 7992 else
db107f19 7993 break_command_1 (arg, 0, from_tty);
c906108c
SS
7994}
7995
7996static void
fba45db2 7997stopat_command (char *arg, int from_tty)
c906108c
SS
7998{
7999 int badInput = 0;
8000
c5aa993b 8001 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8002 badInput = 1;
8003 else
8004 {
8005 char *argptr = arg;
8006 int hasColon = 0;
8007
8008 /* look for a ':'. If there is a '::' then get out, otherwise
c5aa993b 8009 it is probably a line number. */
c906108c 8010 while (*argptr && !hasColon)
c5aa993b
JM
8011 {
8012 hasColon = (*argptr == ':');
8013 argptr++;
8014 }
c906108c
SS
8015
8016 if (hasColon)
c5aa993b 8017 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8018 else
c5aa993b 8019 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8020 }
8021
8022 if (badInput)
a3f17187 8023 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8024 else
db107f19 8025 break_command_1 (arg, 0, from_tty);
c906108c
SS
8026}
8027
65d79d4b
SDJ
8028/* Return non-zero if EXP is verified as constant. Returned zero means EXP is
8029 variable. Also the constant detection may fail for some constant
8030 expressions and in such case still falsely return zero. */
8031static int
8032watchpoint_exp_is_const (const struct expression *exp)
8033{
8034 int i = exp->nelts;
8035
8036 while (i > 0)
8037 {
8038 int oplenp, argsp;
8039
8040 /* We are only interested in the descriptor of each element. */
8041 operator_length (exp, i, &oplenp, &argsp);
8042 i -= oplenp;
8043
8044 switch (exp->elts[i].opcode)
8045 {
8046 case BINOP_ADD:
8047 case BINOP_SUB:
8048 case BINOP_MUL:
8049 case BINOP_DIV:
8050 case BINOP_REM:
8051 case BINOP_MOD:
8052 case BINOP_LSH:
8053 case BINOP_RSH:
8054 case BINOP_LOGICAL_AND:
8055 case BINOP_LOGICAL_OR:
8056 case BINOP_BITWISE_AND:
8057 case BINOP_BITWISE_IOR:
8058 case BINOP_BITWISE_XOR:
8059 case BINOP_EQUAL:
8060 case BINOP_NOTEQUAL:
8061 case BINOP_LESS:
8062 case BINOP_GTR:
8063 case BINOP_LEQ:
8064 case BINOP_GEQ:
8065 case BINOP_REPEAT:
8066 case BINOP_COMMA:
8067 case BINOP_EXP:
8068 case BINOP_MIN:
8069 case BINOP_MAX:
8070 case BINOP_INTDIV:
8071 case BINOP_CONCAT:
8072 case BINOP_IN:
8073 case BINOP_RANGE:
8074 case TERNOP_COND:
8075 case TERNOP_SLICE:
8076 case TERNOP_SLICE_COUNT:
8077
8078 case OP_LONG:
8079 case OP_DOUBLE:
8080 case OP_DECFLOAT:
8081 case OP_LAST:
8082 case OP_COMPLEX:
8083 case OP_STRING:
8084 case OP_BITSTRING:
8085 case OP_ARRAY:
8086 case OP_TYPE:
8087 case OP_NAME:
8088 case OP_OBJC_NSSTRING:
8089
8090 case UNOP_NEG:
8091 case UNOP_LOGICAL_NOT:
8092 case UNOP_COMPLEMENT:
8093 case UNOP_ADDR:
8094 case UNOP_HIGH:
8095 /* Unary, binary and ternary operators: We have to check their
8096 operands. If they are constant, then so is the result of
8097 that operation. For instance, if A and B are determined to be
8098 constants, then so is "A + B".
8099
8100 UNOP_IND is one exception to the rule above, because the value
8101 of *ADDR is not necessarily a constant, even when ADDR is. */
8102 break;
8103
8104 case OP_VAR_VALUE:
8105 /* Check whether the associated symbol is a constant.
8106 We use SYMBOL_CLASS rather than TYPE_CONST because it's
8107 possible that a buggy compiler could mark a variable as constant
8108 even when it is not, and TYPE_CONST would return true in this
8109 case, while SYMBOL_CLASS wouldn't.
8110 We also have to check for function symbols because they are
8111 always constant. */
8112 {
8113 struct symbol *s = exp->elts[i + 2].symbol;
8114
8115 if (SYMBOL_CLASS (s) != LOC_BLOCK
8116 && SYMBOL_CLASS (s) != LOC_CONST
8117 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8118 return 0;
8119 break;
8120 }
8121
8122 /* The default action is to return 0 because we are using
8123 the optimistic approach here: If we don't know something,
8124 then it is not a constant. */
8125 default:
8126 return 0;
8127 }
8128 }
8129
8130 return 1;
8131}
8132
53a5351d
JM
8133/* accessflag: hw_write: watch write,
8134 hw_read: watch read,
8135 hw_access: watch access (read or write) */
c906108c 8136static void
84f4c1fe
PM
8137watch_command_1 (char *arg, int accessflag, int from_tty,
8138 int just_location, int internal)
c906108c 8139{
d983da9c 8140 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 8141 struct expression *exp;
60e1c644 8142 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 8143 struct value *val, *mark, *result;
c906108c 8144 struct frame_info *frame;
c906108c
SS
8145 char *exp_start = NULL;
8146 char *exp_end = NULL;
37e4754d 8147 char *tok, *id_tok_start, *end_tok;
c906108c
SS
8148 int toklen;
8149 char *cond_start = NULL;
8150 char *cond_end = NULL;
c906108c
SS
8151 int i, other_type_used, target_resources_ok = 0;
8152 enum bptype bp_type;
8153 int mem_cnt = 0;
37e4754d 8154 int thread = -1;
0cf6dd15 8155 int pc = 0;
c906108c 8156
37e4754d
LM
8157 /* Make sure that we actually have parameters to parse. */
8158 if (arg != NULL && arg[0] != '\0')
8159 {
8160 toklen = strlen (arg); /* Size of argument list. */
8161
8162 /* Points tok to the end of the argument list. */
8163 tok = arg + toklen - 1;
8164
8165 /* Go backwards in the parameters list. Skip the last parameter.
8166 If we're expecting a 'thread <thread_num>' parameter, this should
8167 be the thread identifier. */
8168 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8169 tok--;
8170 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8171 tok--;
8172
8173 /* Points end_tok to the beginning of the last token. */
8174 id_tok_start = tok + 1;
8175
8176 /* Go backwards in the parameters list. Skip one more parameter.
8177 If we're expecting a 'thread <thread_num>' parameter, we should
8178 reach a "thread" token. */
8179 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8180 tok--;
8181
8182 end_tok = tok;
8183
8184 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8185 tok--;
8186
8187 /* Move the pointer forward to skip the whitespace and
8188 calculate the length of the token. */
8189 tok++;
8190 toklen = end_tok - tok;
8191
8192 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8193 {
8194 /* At this point we've found a "thread" token, which means
8195 the user is trying to set a watchpoint that triggers
8196 only in a specific thread. */
8197 char *endp;
8198
8199 /* Extract the thread ID from the next token. */
8200 thread = strtol (id_tok_start, &endp, 0);
8201
8202 /* Check if the user provided a valid numeric value for the
8203 thread ID. */
8204 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
8205 error (_("Invalid thread ID specification %s."), id_tok_start);
8206
8207 /* Check if the thread actually exists. */
8208 if (!valid_thread_id (thread))
8209 error (_("Unknown thread %d."), thread);
8210
8211 /* Truncate the string and get rid of the thread <thread_num>
8212 parameter before the parameter list is parsed by the
8213 evaluate_expression() function. */
8214 *tok = '\0';
8215 }
8216 }
8217
8218 /* Parse the rest of the arguments. */
c906108c
SS
8219 innermost_block = NULL;
8220 exp_start = arg;
8221 exp = parse_exp_1 (&arg, 0, 0);
8222 exp_end = arg;
fa8a61dc
TT
8223 /* Remove trailing whitespace from the expression before saving it.
8224 This makes the eventual display of the expression string a bit
8225 prettier. */
8226 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
8227 --exp_end;
8228
65d79d4b
SDJ
8229 /* Checking if the expression is not constant. */
8230 if (watchpoint_exp_is_const (exp))
8231 {
8232 int len;
8233
8234 len = exp_end - exp_start;
8235 while (len > 0 && isspace (exp_start[len - 1]))
8236 len--;
8237 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
8238 }
8239
c906108c
SS
8240 exp_valid_block = innermost_block;
8241 mark = value_mark ();
a1442452 8242 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
8243
8244 if (just_location)
8245 {
8246 exp_valid_block = NULL;
a1442452 8247 val = value_addr (result);
06a64a0b
TT
8248 release_value (val);
8249 value_free_to_mark (mark);
8250 }
8251 else if (val != NULL)
fa4727a6 8252 release_value (val);
c906108c
SS
8253
8254 tok = arg;
8255 while (*tok == ' ' || *tok == '\t')
8256 tok++;
8257 end_tok = tok;
8258
8259 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
8260 end_tok++;
8261
8262 toklen = end_tok - tok;
8263 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8264 {
2d134ed3
PA
8265 struct expression *cond;
8266
60e1c644 8267 innermost_block = NULL;
c906108c
SS
8268 tok = cond_start = end_tok + 1;
8269 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
8270
8271 /* The watchpoint expression may not be local, but the condition
8272 may still be. E.g.: `watch global if local > 0'. */
8273 cond_exp_valid_block = innermost_block;
8274
2d134ed3 8275 xfree (cond);
c906108c
SS
8276 cond_end = tok;
8277 }
8278 if (*tok)
8a3fe4f8 8279 error (_("Junk at end of command."));
c906108c 8280
53a5351d 8281 if (accessflag == hw_read)
c5aa993b 8282 bp_type = bp_read_watchpoint;
53a5351d 8283 else if (accessflag == hw_access)
c5aa993b
JM
8284 bp_type = bp_access_watchpoint;
8285 else
8286 bp_type = bp_hardware_watchpoint;
c906108c
SS
8287
8288 mem_cnt = can_use_hardware_watchpoint (val);
8289 if (mem_cnt == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 8290 error (_("Expression cannot be implemented with read/access watchpoint."));
c5aa993b
JM
8291 if (mem_cnt != 0)
8292 {
8293 i = hw_watchpoint_used_count (bp_type, &other_type_used);
53a5351d 8294 target_resources_ok =
d92524f1 8295 target_can_use_hardware_watchpoint (bp_type, i + mem_cnt,
53a5351d 8296 other_type_used);
c5aa993b 8297 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 8298 error (_("Target does not support this type of hardware watchpoint."));
53a5351d 8299
c5aa993b 8300 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 8301 error (_("Target can only support one kind of HW watchpoint at a time."));
c5aa993b 8302 }
c906108c 8303
4d28f7a8
KB
8304 /* Change the type of breakpoint to an ordinary watchpoint if a hardware
8305 watchpoint could not be set. */
8306 if (!mem_cnt || target_resources_ok <= 0)
8307 bp_type = bp_watchpoint;
8308
d983da9c 8309 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
8310
8311 /* If the expression is "local", then set up a "watchpoint scope"
8312 breakpoint at the point where we've left the scope of the watchpoint
8313 expression. Create the scope breakpoint before the watchpoint, so
8314 that we will encounter it first in bpstat_stop_status. */
60e1c644 8315 if (exp_valid_block && frame)
d983da9c 8316 {
edb3359d
DJ
8317 if (frame_id_p (frame_unwind_caller_id (frame)))
8318 {
8319 scope_breakpoint
a6d9a66e
UW
8320 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
8321 frame_unwind_caller_pc (frame),
edb3359d 8322 bp_watchpoint_scope);
d983da9c 8323
edb3359d 8324 scope_breakpoint->enable_state = bp_enabled;
d983da9c 8325
edb3359d
DJ
8326 /* Automatically delete the breakpoint when it hits. */
8327 scope_breakpoint->disposition = disp_del;
d983da9c 8328
edb3359d
DJ
8329 /* Only break in the proper frame (help with recursion). */
8330 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 8331
edb3359d 8332 /* Set the address at which we will stop. */
a6d9a66e
UW
8333 scope_breakpoint->loc->gdbarch
8334 = frame_unwind_caller_arch (frame);
edb3359d
DJ
8335 scope_breakpoint->loc->requested_address
8336 = frame_unwind_caller_pc (frame);
8337 scope_breakpoint->loc->address
a6d9a66e
UW
8338 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
8339 scope_breakpoint->loc->requested_address,
edb3359d
DJ
8340 scope_breakpoint->type);
8341 }
d983da9c
DJ
8342 }
8343
c906108c 8344 /* Now set up the breakpoint. */
2d134ed3 8345 b = set_raw_breakpoint_without_location (NULL, bp_type);
84f4c1fe 8346 set_breakpoint_number (internal, b);
37e4754d 8347 b->thread = thread;
b5de0fa7 8348 b->disposition = disp_donttouch;
c906108c
SS
8349 b->exp = exp;
8350 b->exp_valid_block = exp_valid_block;
60e1c644 8351 b->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
8352 if (just_location)
8353 {
8354 struct type *t = value_type (val);
8355 CORE_ADDR addr = value_as_address (val);
8356 char *name;
8357
8358 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
8359 name = type_to_string (t);
8360
d63d0675
JK
8361 b->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
8362 core_addr_to_string (addr));
06a64a0b
TT
8363 xfree (name);
8364
d63d0675
JK
8365 b->exp_string = xstrprintf ("-location: %.*s",
8366 (int) (exp_end - exp_start), exp_start);
8367
06a64a0b
TT
8368 /* The above expression is in C. */
8369 b->language = language_c;
8370 }
8371 else
8372 b->exp_string = savestring (exp_start, exp_end - exp_start);
c906108c 8373 b->val = val;
fa4727a6 8374 b->val_valid = 1;
c906108c
SS
8375 if (cond_start)
8376 b->cond_string = savestring (cond_start, cond_end - cond_start);
8377 else
8378 b->cond_string = 0;
c5aa993b 8379
c906108c 8380 if (frame)
f6bc2008
PA
8381 {
8382 b->watchpoint_frame = get_frame_id (frame);
8383 b->watchpoint_thread = inferior_ptid;
8384 }
c906108c 8385 else
f6bc2008
PA
8386 {
8387 b->watchpoint_frame = null_frame_id;
8388 b->watchpoint_thread = null_ptid;
8389 }
c906108c 8390
d983da9c 8391 if (scope_breakpoint != NULL)
c906108c 8392 {
d983da9c
DJ
8393 /* The scope breakpoint is related to the watchpoint. We will
8394 need to act on them together. */
8395 b->related_breakpoint = scope_breakpoint;
8396 scope_breakpoint->related_breakpoint = b;
c906108c 8397 }
d983da9c 8398
06a64a0b
TT
8399 if (!just_location)
8400 value_free_to_mark (mark);
2d134ed3
PA
8401
8402 /* Finally update the new watchpoint. This creates the locations
8403 that should be inserted. */
8404 update_watchpoint (b, 1);
84f4c1fe
PM
8405 if (internal)
8406 /* Do not mention breakpoints with a negative number, but do
8407 notify observers. */
8408 observer_notify_breakpoint_created (b->number);
8409 else
8410 mention (b);
b60e7edf 8411 update_global_location_list (1);
c906108c
SS
8412}
8413
8414/* Return count of locations need to be watched and can be handled
8415 in hardware. If the watchpoint can not be handled
8416 in hardware return zero. */
8417
c906108c 8418static int
fba45db2 8419can_use_hardware_watchpoint (struct value *v)
c906108c
SS
8420{
8421 int found_memory_cnt = 0;
2e70b7b9 8422 struct value *head = v;
c906108c
SS
8423
8424 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 8425 if (!can_use_hw_watchpoints)
c906108c 8426 return 0;
c5aa993b 8427
5c44784c
JM
8428 /* Make sure that the value of the expression depends only upon
8429 memory contents, and values computed from them within GDB. If we
8430 find any register references or function calls, we can't use a
8431 hardware watchpoint.
8432
8433 The idea here is that evaluating an expression generates a series
8434 of values, one holding the value of every subexpression. (The
8435 expression a*b+c has five subexpressions: a, b, a*b, c, and
8436 a*b+c.) GDB's values hold almost enough information to establish
8437 the criteria given above --- they identify memory lvalues,
8438 register lvalues, computed values, etcetera. So we can evaluate
8439 the expression, and then scan the chain of values that leaves
8440 behind to decide whether we can detect any possible change to the
8441 expression's final value using only hardware watchpoints.
8442
8443 However, I don't think that the values returned by inferior
8444 function calls are special in any way. So this function may not
8445 notice that an expression involving an inferior function call
8446 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 8447 for (; v; v = value_next (v))
c906108c 8448 {
5c44784c 8449 if (VALUE_LVAL (v) == lval_memory)
c906108c 8450 {
8464be76
DJ
8451 if (v != head && value_lazy (v))
8452 /* A lazy memory lvalue in the chain is one that GDB never
8453 needed to fetch; we either just used its address (e.g.,
8454 `a' in `a.b') or we never needed it at all (e.g., `a'
8455 in `a,b'). This doesn't apply to HEAD; if that is
8456 lazy then it was not readable, but watch it anyway. */
5c44784c 8457 ;
53a5351d 8458 else
5c44784c
JM
8459 {
8460 /* Ahh, memory we actually used! Check if we can cover
8461 it with hardware watchpoints. */
df407dfe 8462 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
8463
8464 /* We only watch structs and arrays if user asked for it
8465 explicitly, never if they just happen to appear in a
8466 middle of some value chain. */
8467 if (v == head
8468 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
8469 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
8470 {
42ae5230 8471 CORE_ADDR vaddr = value_address (v);
df407dfe 8472 int len = TYPE_LENGTH (value_type (v));
2e70b7b9 8473
d92524f1 8474 if (!target_region_ok_for_hw_watchpoint (vaddr, len))
2e70b7b9
MS
8475 return 0;
8476 else
8477 found_memory_cnt++;
8478 }
5c44784c 8479 }
c5aa993b 8480 }
5086187c
AC
8481 else if (VALUE_LVAL (v) != not_lval
8482 && deprecated_value_modifiable (v) == 0)
38b6c3b3 8483 return 0; /* These are values from the history (e.g., $1). */
5086187c 8484 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 8485 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
8486 }
8487
8488 /* The expression itself looks suitable for using a hardware
8489 watchpoint, but give the target machine a chance to reject it. */
8490 return found_memory_cnt;
8491}
8492
8b93c638 8493void
84f4c1fe 8494watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 8495{
84f4c1fe 8496 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
8497}
8498
8499/* A helper function that looks for an argument at the start of a
8500 string. The argument must also either be at the end of the string,
8501 or be followed by whitespace. Returns 1 if it finds the argument,
8502 0 otherwise. If the argument is found, it updates *STR. */
8503
8504static int
8505check_for_argument (char **str, char *arg, int arg_len)
8506{
8507 if (strncmp (*str, arg, arg_len) == 0
8508 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
8509 {
8510 *str += arg_len;
8511 return 1;
8512 }
8513 return 0;
8514}
8515
8516/* A helper function that looks for the "-location" argument and then
8517 calls watch_command_1. */
8518
8519static void
8520watch_maybe_just_location (char *arg, int accessflag, int from_tty)
8521{
8522 int just_location = 0;
8523
8524 if (arg
8525 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
8526 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
8527 {
8528 ep_skip_leading_whitespace (&arg);
8529 just_location = 1;
8530 }
8531
84f4c1fe 8532 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 8533}
8926118c 8534
c5aa993b 8535static void
fba45db2 8536watch_command (char *arg, int from_tty)
c906108c 8537{
06a64a0b 8538 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
8539}
8540
8b93c638 8541void
84f4c1fe 8542rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 8543{
84f4c1fe 8544 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 8545}
8926118c 8546
c5aa993b 8547static void
fba45db2 8548rwatch_command (char *arg, int from_tty)
c906108c 8549{
06a64a0b 8550 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
8551}
8552
8b93c638 8553void
84f4c1fe 8554awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 8555{
84f4c1fe 8556 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 8557}
8926118c 8558
c5aa993b 8559static void
fba45db2 8560awatch_command (char *arg, int from_tty)
c906108c 8561{
06a64a0b 8562 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 8563}
c906108c 8564\f
c5aa993b 8565
43ff13b4 8566/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
8567 because it uses the mechanisms of breakpoints. */
8568
bfec99b2
PA
8569struct until_break_command_continuation_args
8570{
8571 struct breakpoint *breakpoint;
8572 struct breakpoint *breakpoint2;
186c406b 8573 int thread_num;
bfec99b2
PA
8574};
8575
43ff13b4
JM
8576/* This function is called by fetch_inferior_event via the
8577 cmd_continuation pointer, to complete the until command. It takes
8578 care of cleaning up the temporary breakpoints set up by the until
8579 command. */
c2c6d25f 8580static void
604ead4a 8581until_break_command_continuation (void *arg)
43ff13b4 8582{
bfec99b2
PA
8583 struct until_break_command_continuation_args *a = arg;
8584
8585 delete_breakpoint (a->breakpoint);
8586 if (a->breakpoint2)
8587 delete_breakpoint (a->breakpoint2);
186c406b 8588 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
8589}
8590
c906108c 8591void
ae66c1fc 8592until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
8593{
8594 struct symtabs_and_lines sals;
8595 struct symtab_and_line sal;
206415a3 8596 struct frame_info *frame = get_selected_frame (NULL);
c906108c 8597 struct breakpoint *breakpoint;
f107f563 8598 struct breakpoint *breakpoint2 = NULL;
c906108c 8599 struct cleanup *old_chain;
186c406b
TT
8600 int thread;
8601 struct thread_info *tp;
c906108c
SS
8602
8603 clear_proceed_status ();
8604
8605 /* Set a breakpoint where the user wants it and at return from
8606 this function */
c5aa993b 8607
c906108c
SS
8608 if (default_breakpoint_valid)
8609 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
68219205 8610 default_breakpoint_line, (char ***) NULL, NULL);
c906108c 8611 else
53a5351d 8612 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL,
68219205 8613 0, (char ***) NULL, NULL);
c5aa993b 8614
c906108c 8615 if (sals.nelts != 1)
8a3fe4f8 8616 error (_("Couldn't get information on specified line."));
c5aa993b 8617
c906108c 8618 sal = sals.sals[0];
b8c9b27d 8619 xfree (sals.sals); /* malloc'd, so freed */
c5aa993b 8620
c906108c 8621 if (*arg)
8a3fe4f8 8622 error (_("Junk at end of arguments."));
c5aa993b 8623
c906108c 8624 resolve_sal_pc (&sal);
c5aa993b 8625
ae66c1fc
EZ
8626 if (anywhere)
8627 /* If the user told us to continue until a specified location,
8628 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
8629 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
8630 null_frame_id, bp_until);
ae66c1fc 8631 else
edb3359d 8632 /* Otherwise, specify the selected frame, because we want to stop only
ae66c1fc 8633 at the very same frame. */
a6d9a66e
UW
8634 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
8635 get_stack_frame_id (frame),
ae66c1fc 8636 bp_until);
c5aa993b 8637
f107f563 8638 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 8639
186c406b
TT
8640 tp = inferior_thread ();
8641 thread = tp->num;
8642
ae66c1fc
EZ
8643 /* Keep within the current frame, or in frames called by the current
8644 one. */
edb3359d
DJ
8645
8646 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 8647 {
edb3359d
DJ
8648 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
8649 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
8650 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
8651 sal,
edb3359d 8652 frame_unwind_caller_id (frame),
f107f563
VP
8653 bp_until);
8654 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
8655
8656 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
8657 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 8658 }
c5aa993b 8659
c906108c 8660 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563
VP
8661
8662 /* If we are running asynchronously, and proceed call above has actually
8663 managed to start the target, arrange for breakpoints to be
8664 deleted when the target stops. Otherwise, we're already stopped and
8665 delete breakpoints via cleanup chain. */
8666
8ea051c5 8667 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 8668 {
bfec99b2
PA
8669 struct until_break_command_continuation_args *args;
8670 args = xmalloc (sizeof (*args));
f107f563 8671
bfec99b2
PA
8672 args->breakpoint = breakpoint;
8673 args->breakpoint2 = breakpoint2;
186c406b 8674 args->thread_num = thread;
f107f563
VP
8675
8676 discard_cleanups (old_chain);
95e54da7
PA
8677 add_continuation (inferior_thread (),
8678 until_break_command_continuation, args,
604ead4a 8679 xfree);
f107f563
VP
8680 }
8681 else
c5aa993b 8682 do_cleanups (old_chain);
c906108c 8683}
ae66c1fc 8684
c906108c 8685static void
fba45db2 8686ep_skip_leading_whitespace (char **s)
c906108c 8687{
c5aa993b
JM
8688 if ((s == NULL) || (*s == NULL))
8689 return;
8690 while (isspace (**s))
8691 *s += 1;
c906108c 8692}
c5aa993b 8693
c906108c
SS
8694/* This function attempts to parse an optional "if <cond>" clause
8695 from the arg string. If one is not found, it returns NULL.
c5aa993b 8696
c906108c
SS
8697 Else, it returns a pointer to the condition string. (It does not
8698 attempt to evaluate the string against a particular block.) And,
8699 it updates arg to point to the first character following the parsed
8700 if clause in the arg string. */
53a5351d 8701
c906108c 8702static char *
fba45db2 8703ep_parse_optional_if_clause (char **arg)
c906108c 8704{
c5aa993b
JM
8705 char *cond_string;
8706
8707 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 8708 return NULL;
c5aa993b 8709
c906108c
SS
8710 /* Skip the "if" keyword. */
8711 (*arg) += 2;
c5aa993b 8712
c906108c
SS
8713 /* Skip any extra leading whitespace, and record the start of the
8714 condition string. */
8715 ep_skip_leading_whitespace (arg);
8716 cond_string = *arg;
c5aa993b 8717
c906108c
SS
8718 /* Assume that the condition occupies the remainder of the arg string. */
8719 (*arg) += strlen (cond_string);
c5aa993b 8720
c906108c
SS
8721 return cond_string;
8722}
c5aa993b 8723
c906108c
SS
8724/* Commands to deal with catching events, such as signals, exceptions,
8725 process start/exit, etc. */
c5aa993b
JM
8726
8727typedef enum
8728{
44feb3ce
TT
8729 catch_fork_temporary, catch_vfork_temporary,
8730 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
8731}
8732catch_fork_kind;
8733
c906108c 8734static void
cc59ec59
MS
8735catch_fork_command_1 (char *arg, int from_tty,
8736 struct cmd_list_element *command)
c906108c 8737{
a6d9a66e 8738 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 8739 char *cond_string = NULL;
44feb3ce
TT
8740 catch_fork_kind fork_kind;
8741 int tempflag;
8742
8743 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
8744 tempflag = (fork_kind == catch_fork_temporary
8745 || fork_kind == catch_vfork_temporary);
c5aa993b 8746
44feb3ce
TT
8747 if (!arg)
8748 arg = "";
c906108c 8749 ep_skip_leading_whitespace (&arg);
c5aa993b 8750
c906108c 8751 /* The allowed syntax is:
c5aa993b
JM
8752 catch [v]fork
8753 catch [v]fork if <cond>
8754
c906108c
SS
8755 First, check if there's an if clause. */
8756 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 8757
c906108c 8758 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 8759 error (_("Junk at end of arguments."));
c5aa993b 8760
c906108c
SS
8761 /* If this target supports it, create a fork or vfork catchpoint
8762 and enable reporting of such events. */
c5aa993b
JM
8763 switch (fork_kind)
8764 {
44feb3ce
TT
8765 case catch_fork_temporary:
8766 case catch_fork_permanent:
a6d9a66e 8767 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 8768 &catch_fork_breakpoint_ops);
c906108c 8769 break;
44feb3ce
TT
8770 case catch_vfork_temporary:
8771 case catch_vfork_permanent:
a6d9a66e 8772 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 8773 &catch_vfork_breakpoint_ops);
c906108c 8774 break;
c5aa993b 8775 default:
8a3fe4f8 8776 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 8777 break;
c5aa993b 8778 }
c906108c
SS
8779}
8780
8781static void
cc59ec59
MS
8782catch_exec_command_1 (char *arg, int from_tty,
8783 struct cmd_list_element *command)
c906108c 8784{
a6d9a66e 8785 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 8786 int tempflag;
c5aa993b 8787 char *cond_string = NULL;
c906108c 8788
44feb3ce
TT
8789 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8790
8791 if (!arg)
8792 arg = "";
c906108c
SS
8793 ep_skip_leading_whitespace (&arg);
8794
8795 /* The allowed syntax is:
c5aa993b
JM
8796 catch exec
8797 catch exec if <cond>
c906108c
SS
8798
8799 First, check if there's an if clause. */
8800 cond_string = ep_parse_optional_if_clause (&arg);
8801
8802 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 8803 error (_("Junk at end of arguments."));
c906108c
SS
8804
8805 /* If this target supports it, create an exec catchpoint
8806 and enable reporting of such events. */
a6d9a66e
UW
8807 create_catchpoint (gdbarch, tempflag, cond_string,
8808 &catch_exec_breakpoint_ops);
c906108c 8809}
c5aa993b 8810
3086aeae
DJ
8811static enum print_stop_action
8812print_exception_catchpoint (struct breakpoint *b)
8813{
ade92717 8814 int bp_temp, bp_throw;
3086aeae 8815
ade92717 8816 annotate_catchpoint (b->number);
3086aeae 8817
ade92717
AR
8818 bp_throw = strstr (b->addr_string, "throw") != NULL;
8819 if (b->loc->address != b->loc->requested_address)
8820 breakpoint_adjustment_warning (b->loc->requested_address,
8821 b->loc->address,
8822 b->number, 1);
df2b6d2d 8823 bp_temp = b->disposition == disp_del;
ade92717
AR
8824 ui_out_text (uiout,
8825 bp_temp ? "Temporary catchpoint "
8826 : "Catchpoint ");
8827 if (!ui_out_is_mi_like_p (uiout))
8828 ui_out_field_int (uiout, "bkptno", b->number);
8829 ui_out_text (uiout,
c0b37c48
AR
8830 bp_throw ? " (exception thrown), "
8831 : " (exception caught), ");
ade92717
AR
8832 if (ui_out_is_mi_like_p (uiout))
8833 {
8834 ui_out_field_string (uiout, "reason",
8835 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8836 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8837 ui_out_field_int (uiout, "bkptno", b->number);
8838 }
3086aeae
DJ
8839 return PRINT_SRC_AND_LOC;
8840}
8841
8842static void
cc59ec59
MS
8843print_one_exception_catchpoint (struct breakpoint *b,
8844 struct bp_location **last_loc)
3086aeae 8845{
79a45b7d 8846 struct value_print_options opts;
cc59ec59 8847
79a45b7d
TT
8848 get_user_print_options (&opts);
8849 if (opts.addressprint)
3086aeae
DJ
8850 {
8851 annotate_field (4);
604133b5
AR
8852 if (b->loc == NULL || b->loc->shlib_disabled)
8853 ui_out_field_string (uiout, "addr", "<PENDING>");
8854 else
5af949e3
UW
8855 ui_out_field_core_addr (uiout, "addr",
8856 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
8857 }
8858 annotate_field (5);
604133b5 8859 if (b->loc)
a6d9a66e 8860 *last_loc = b->loc;
3086aeae
DJ
8861 if (strstr (b->addr_string, "throw") != NULL)
8862 ui_out_field_string (uiout, "what", "exception throw");
8863 else
8864 ui_out_field_string (uiout, "what", "exception catch");
8865}
8866
8867static void
8868print_mention_exception_catchpoint (struct breakpoint *b)
8869{
ade92717
AR
8870 int bp_temp;
8871 int bp_throw;
8872
df2b6d2d 8873 bp_temp = b->disposition == disp_del;
ade92717
AR
8874 bp_throw = strstr (b->addr_string, "throw") != NULL;
8875 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
8876 : _("Catchpoint "));
8877 ui_out_field_int (uiout, "bkptno", b->number);
8878 ui_out_text (uiout, bp_throw ? _(" (throw)")
8879 : _(" (catch)"));
3086aeae
DJ
8880}
8881
6149aea9
PA
8882/* Implement the "print_recreate" breakpoint_ops method for throw and
8883 catch catchpoints. */
8884
8885static void
8886print_recreate_exception_catchpoint (struct breakpoint *b, struct ui_file *fp)
8887{
8888 int bp_temp;
8889 int bp_throw;
8890
8891 bp_temp = b->disposition == disp_del;
8892 bp_throw = strstr (b->addr_string, "throw") != NULL;
8893 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
8894 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
8895}
8896
3086aeae 8897static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
ce78b96d
JB
8898 NULL, /* insert */
8899 NULL, /* remove */
8900 NULL, /* breakpoint_hit */
3086aeae
DJ
8901 print_exception_catchpoint,
8902 print_one_exception_catchpoint,
6149aea9
PA
8903 print_mention_exception_catchpoint,
8904 print_recreate_exception_catchpoint
3086aeae
DJ
8905};
8906
8907static int
8908handle_gnu_v3_exceptions (int tempflag, char *cond_string,
8909 enum exception_event_kind ex_event, int from_tty)
8910{
604133b5
AR
8911 char *trigger_func_name;
8912
3086aeae 8913 if (ex_event == EX_EVENT_CATCH)
604133b5 8914 trigger_func_name = "__cxa_begin_catch";
3086aeae 8915 else
604133b5 8916 trigger_func_name = "__cxa_throw";
3086aeae 8917
8cdf0e15
VP
8918 create_breakpoint (get_current_arch (),
8919 trigger_func_name, cond_string, -1,
8920 0 /* condition and thread are valid. */,
0fb4aa4b 8921 tempflag, bp_breakpoint,
8cdf0e15
VP
8922 0,
8923 AUTO_BOOLEAN_TRUE /* pending */,
8924 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
8925 1 /* enabled */,
8926 0 /* internal */);
3086aeae 8927
3086aeae
DJ
8928 return 1;
8929}
8930
c5aa993b 8931/* Deal with "catch catch" and "catch throw" commands */
c906108c
SS
8932
8933static void
fba45db2
KB
8934catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
8935 int tempflag, int from_tty)
c906108c 8936{
c5aa993b 8937 char *cond_string = NULL;
c5aa993b 8938
44feb3ce
TT
8939 if (!arg)
8940 arg = "";
c906108c 8941 ep_skip_leading_whitespace (&arg);
c5aa993b 8942
c906108c
SS
8943 cond_string = ep_parse_optional_if_clause (&arg);
8944
8945 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 8946 error (_("Junk at end of arguments."));
c906108c 8947
059fb39f
PM
8948 if (ex_event != EX_EVENT_THROW
8949 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 8950 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 8951
3086aeae
DJ
8952 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
8953 return;
8954
8a3fe4f8 8955 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
8956}
8957
44feb3ce
TT
8958/* Implementation of "catch catch" command. */
8959
8960static void
8961catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
8962{
8963 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 8964
44feb3ce
TT
8965 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
8966}
8967
8968/* Implementation of "catch throw" command. */
8969
8970static void
8971catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
8972{
8973 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 8974
44feb3ce
TT
8975 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
8976}
8977
f7f9143b
JB
8978/* Create a breakpoint struct for Ada exception catchpoints. */
8979
8980static void
a6d9a66e
UW
8981create_ada_exception_breakpoint (struct gdbarch *gdbarch,
8982 struct symtab_and_line sal,
f7f9143b
JB
8983 char *addr_string,
8984 char *exp_string,
8985 char *cond_string,
8986 struct expression *cond,
8987 struct breakpoint_ops *ops,
8988 int tempflag,
8989 int from_tty)
8990{
8991 struct breakpoint *b;
8992
8993 if (from_tty)
8994 {
5af949e3
UW
8995 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8996 if (!loc_gdbarch)
8997 loc_gdbarch = gdbarch;
8998
6c95b8df
PA
8999 describe_other_breakpoints (loc_gdbarch,
9000 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9001 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9002 version for exception catchpoints, because two catchpoints
9003 used for different exception names will use the same address.
9004 In this case, a "breakpoint ... also set at..." warning is
9005 unproductive. Besides. the warning phrasing is also a bit
9006 inapropriate, we should use the word catchpoint, and tell
9007 the user what type of catchpoint it is. The above is good
9008 enough for now, though. */
9009 }
9010
a6d9a66e 9011 b = set_raw_breakpoint (gdbarch, sal, bp_breakpoint);
f7f9143b
JB
9012 set_breakpoint_count (breakpoint_count + 1);
9013
9014 b->enable_state = bp_enabled;
9015 b->disposition = tempflag ? disp_del : disp_donttouch;
9016 b->number = breakpoint_count;
9017 b->ignore_count = 0;
511a6cd4 9018 b->loc->cond = cond;
f7f9143b
JB
9019 b->addr_string = addr_string;
9020 b->language = language_ada;
9021 b->cond_string = cond_string;
9022 b->exp_string = exp_string;
9023 b->thread = -1;
9024 b->ops = ops;
f7f9143b
JB
9025
9026 mention (b);
b60e7edf 9027 update_global_location_list (1);
f7f9143b
JB
9028}
9029
9030/* Implement the "catch exception" command. */
9031
9032static void
44feb3ce
TT
9033catch_ada_exception_command (char *arg, int from_tty,
9034 struct cmd_list_element *command)
f7f9143b 9035{
a6d9a66e 9036 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9037 int tempflag;
f7f9143b 9038 struct symtab_and_line sal;
f7f9143b
JB
9039 char *addr_string = NULL;
9040 char *exp_string = NULL;
9041 char *cond_string = NULL;
9042 struct expression *cond = NULL;
9043 struct breakpoint_ops *ops = NULL;
9044
44feb3ce
TT
9045 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9046
9047 if (!arg)
9048 arg = "";
f7f9143b
JB
9049 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
9050 &cond_string, &cond, &ops);
a6d9a66e 9051 create_ada_exception_breakpoint (gdbarch, sal, addr_string, exp_string,
f7f9143b
JB
9052 cond_string, cond, ops, tempflag,
9053 from_tty);
9054}
9055
a96d9b2e
SDJ
9056/* Cleanup function for a syscall filter list. */
9057static void
9058clean_up_filters (void *arg)
9059{
9060 VEC(int) *iter = *(VEC(int) **) arg;
9061 VEC_free (int, iter);
9062}
9063
9064/* Splits the argument using space as delimiter. Returns an xmalloc'd
9065 filter list, or NULL if no filtering is required. */
9066static VEC(int) *
9067catch_syscall_split_args (char *arg)
9068{
9069 VEC(int) *result = NULL;
9070 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9071
9072 while (*arg != '\0')
9073 {
9074 int i, syscall_number;
9075 char *endptr;
9076 char cur_name[128];
9077 struct syscall s;
9078
9079 /* Skip whitespace. */
9080 while (isspace (*arg))
9081 arg++;
9082
9083 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9084 cur_name[i] = arg[i];
9085 cur_name[i] = '\0';
9086 arg += i;
9087
9088 /* Check if the user provided a syscall name or a number. */
9089 syscall_number = (int) strtol (cur_name, &endptr, 0);
9090 if (*endptr == '\0')
bccd0dd2 9091 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9092 else
9093 {
9094 /* We have a name. Let's check if it's valid and convert it
9095 to a number. */
9096 get_syscall_by_name (cur_name, &s);
9097
9098 if (s.number == UNKNOWN_SYSCALL)
9099 /* Here we have to issue an error instead of a warning, because
9100 GDB cannot do anything useful if there's no syscall number to
9101 be caught. */
9102 error (_("Unknown syscall name '%s'."), cur_name);
9103 }
9104
9105 /* Ok, it's valid. */
9106 VEC_safe_push (int, result, s.number);
9107 }
9108
9109 discard_cleanups (cleanup);
9110 return result;
9111}
9112
9113/* Implement the "catch syscall" command. */
9114
9115static void
cc59ec59
MS
9116catch_syscall_command_1 (char *arg, int from_tty,
9117 struct cmd_list_element *command)
a96d9b2e
SDJ
9118{
9119 int tempflag;
9120 VEC(int) *filter;
9121 struct syscall s;
9122 struct gdbarch *gdbarch = get_current_arch ();
9123
9124 /* Checking if the feature if supported. */
9125 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9126 error (_("The feature 'catch syscall' is not supported on \
9127this architeture yet."));
9128
9129 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9130
9131 ep_skip_leading_whitespace (&arg);
9132
9133 /* We need to do this first "dummy" translation in order
9134 to get the syscall XML file loaded or, most important,
9135 to display a warning to the user if there's no XML file
9136 for his/her architecture. */
9137 get_syscall_by_number (0, &s);
9138
9139 /* The allowed syntax is:
9140 catch syscall
9141 catch syscall <name | number> [<name | number> ... <name | number>]
9142
9143 Let's check if there's a syscall name. */
9144
9145 if (arg != NULL)
9146 filter = catch_syscall_split_args (arg);
9147 else
9148 filter = NULL;
9149
9150 create_syscall_event_catchpoint (tempflag, filter,
9151 &catch_syscall_breakpoint_ops);
9152}
9153
f7f9143b
JB
9154/* Implement the "catch assert" command. */
9155
9156static void
44feb3ce 9157catch_assert_command (char *arg, int from_tty, struct cmd_list_element *command)
f7f9143b 9158{
a6d9a66e 9159 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9160 int tempflag;
f7f9143b
JB
9161 struct symtab_and_line sal;
9162 char *addr_string = NULL;
9163 struct breakpoint_ops *ops = NULL;
9164
44feb3ce
TT
9165 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9166
9167 if (!arg)
9168 arg = "";
f7f9143b 9169 sal = ada_decode_assert_location (arg, &addr_string, &ops);
a6d9a66e
UW
9170 create_ada_exception_breakpoint (gdbarch, sal, addr_string, NULL, NULL, NULL,
9171 ops, tempflag, from_tty);
f7f9143b
JB
9172}
9173
c906108c 9174static void
fba45db2 9175catch_command (char *arg, int from_tty)
c906108c 9176{
44feb3ce 9177 error (_("Catch requires an event name."));
c906108c
SS
9178}
9179\f
9180
9181static void
fba45db2 9182tcatch_command (char *arg, int from_tty)
c906108c 9183{
44feb3ce 9184 error (_("Catch requires an event name."));
c906108c
SS
9185}
9186
80f8a6eb 9187/* Delete breakpoints by address or line. */
c906108c
SS
9188
9189static void
fba45db2 9190clear_command (char *arg, int from_tty)
c906108c 9191{
d6e956e5
VP
9192 struct breakpoint *b;
9193 VEC(breakpoint_p) *found = 0;
9194 int ix;
c906108c
SS
9195 int default_match;
9196 struct symtabs_and_lines sals;
9197 struct symtab_and_line sal;
c906108c
SS
9198 int i;
9199
9200 if (arg)
9201 {
9202 sals = decode_line_spec (arg, 1);
9203 default_match = 0;
9204 }
9205 else
9206 {
c5aa993b 9207 sals.sals = (struct symtab_and_line *)
c906108c 9208 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 9209 make_cleanup (xfree, sals.sals);
fe39c653 9210 init_sal (&sal); /* initialize to zeroes */
c906108c
SS
9211 sal.line = default_breakpoint_line;
9212 sal.symtab = default_breakpoint_symtab;
9213 sal.pc = default_breakpoint_address;
6c95b8df 9214 sal.pspace = default_breakpoint_pspace;
c906108c 9215 if (sal.symtab == 0)
8a3fe4f8 9216 error (_("No source file specified."));
c906108c
SS
9217
9218 sals.sals[0] = sal;
9219 sals.nelts = 1;
9220
9221 default_match = 1;
9222 }
9223
ed0616c6
VP
9224 /* We don't call resolve_sal_pc here. That's not
9225 as bad as it seems, because all existing breakpoints
9226 typically have both file/line and pc set. So, if
9227 clear is given file/line, we can match this to existing
9228 breakpoint without obtaining pc at all.
9229
9230 We only support clearing given the address explicitly
9231 present in breakpoint table. Say, we've set breakpoint
9232 at file:line. There were several PC values for that file:line,
9233 due to optimization, all in one block.
9234 We've picked one PC value. If "clear" is issued with another
9235 PC corresponding to the same file:line, the breakpoint won't
9236 be cleared. We probably can still clear the breakpoint, but
9237 since the other PC value is never presented to user, user
9238 can only find it by guessing, and it does not seem important
9239 to support that. */
9240
c906108c 9241 /* For each line spec given, delete bps which correspond
80f8a6eb
MS
9242 to it. Do it in two passes, solely to preserve the current
9243 behavior that from_tty is forced true if we delete more than
9244 one breakpoint. */
c906108c 9245
80f8a6eb 9246 found = NULL;
c906108c
SS
9247 for (i = 0; i < sals.nelts; i++)
9248 {
9249 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
9250 If line given (pc == 0), clear all bpts on specified line.
9251 If defaulting, clear all bpts on default line
c906108c 9252 or at default pc.
c5aa993b
JM
9253
9254 defaulting sal.pc != 0 tests to do
9255
9256 0 1 pc
9257 1 1 pc _and_ line
9258 0 0 line
9259 1 0 <can't happen> */
c906108c
SS
9260
9261 sal = sals.sals[i];
c906108c 9262
d6e956e5
VP
9263 /* Find all matching breakpoints and add them to
9264 'found'. */
9265 ALL_BREAKPOINTS (b)
c5aa993b 9266 {
0d381245 9267 int match = 0;
80f8a6eb 9268 /* Are we going to delete b? */
cc60f2e3 9269 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
9270 {
9271 struct bp_location *loc = b->loc;
9272 for (; loc; loc = loc->next)
9273 {
6c95b8df
PA
9274 int pc_match = sal.pc
9275 && (loc->pspace == sal.pspace)
0d381245
VP
9276 && (loc->address == sal.pc)
9277 && (!section_is_overlay (loc->section)
9278 || loc->section == sal.section);
9279 int line_match = ((default_match || (0 == sal.pc))
9280 && b->source_file != NULL
9281 && sal.symtab != NULL
6c95b8df 9282 && sal.pspace == loc->pspace
0d381245
VP
9283 && strcmp (b->source_file, sal.symtab->filename) == 0
9284 && b->line_number == sal.line);
9285 if (pc_match || line_match)
9286 {
9287 match = 1;
9288 break;
9289 }
9290 }
9291 }
9292
9293 if (match)
d6e956e5 9294 VEC_safe_push(breakpoint_p, found, b);
c906108c 9295 }
80f8a6eb
MS
9296 }
9297 /* Now go thru the 'found' chain and delete them. */
d6e956e5 9298 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
9299 {
9300 if (arg)
8a3fe4f8 9301 error (_("No breakpoint at %s."), arg);
80f8a6eb 9302 else
8a3fe4f8 9303 error (_("No breakpoint at this line."));
80f8a6eb 9304 }
c906108c 9305
d6e956e5 9306 if (VEC_length(breakpoint_p, found) > 1)
80f8a6eb
MS
9307 from_tty = 1; /* Always report if deleted more than one */
9308 if (from_tty)
a3f17187 9309 {
d6e956e5 9310 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
9311 printf_unfiltered (_("Deleted breakpoint "));
9312 else
9313 printf_unfiltered (_("Deleted breakpoints "));
9314 }
80f8a6eb 9315 breakpoints_changed ();
d6e956e5
VP
9316
9317 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 9318 {
c5aa993b 9319 if (from_tty)
d6e956e5
VP
9320 printf_unfiltered ("%d ", b->number);
9321 delete_breakpoint (b);
c906108c 9322 }
80f8a6eb
MS
9323 if (from_tty)
9324 putchar_unfiltered ('\n');
c906108c
SS
9325}
9326\f
9327/* Delete breakpoint in BS if they are `delete' breakpoints and
9328 all breakpoints that are marked for deletion, whether hit or not.
9329 This is called after any breakpoint is hit, or after errors. */
9330
9331void
fba45db2 9332breakpoint_auto_delete (bpstat bs)
c906108c 9333{
35df4500 9334 struct breakpoint *b, *b_tmp;
c906108c
SS
9335
9336 for (; bs; bs = bs->next)
f431efe5
PA
9337 if (bs->breakpoint_at
9338 && bs->breakpoint_at->disposition == disp_del
c906108c 9339 && bs->stop)
f431efe5 9340 delete_breakpoint (bs->breakpoint_at);
c906108c 9341
35df4500 9342 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 9343 {
b5de0fa7 9344 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
9345 delete_breakpoint (b);
9346 }
c906108c
SS
9347}
9348
494cfb0f 9349/* A comparison function for bp_location AP and BP being interfaced to qsort.
876fa593
JK
9350 Sort elements primarily by their ADDRESS (no matter what does
9351 breakpoint_address_is_meaningful say for its OWNER), secondarily by ordering
9352 first bp_permanent OWNERed elements and terciarily just ensuring the array
9353 is sorted stable way despite qsort being an instable algorithm. */
9354
9355static int
494cfb0f 9356bp_location_compare (const void *ap, const void *bp)
876fa593 9357{
494cfb0f
JK
9358 struct bp_location *a = *(void **) ap;
9359 struct bp_location *b = *(void **) bp;
2bdf28a0 9360 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
9361 int a_perm = a->owner->enable_state == bp_permanent;
9362 int b_perm = b->owner->enable_state == bp_permanent;
9363
9364 if (a->address != b->address)
9365 return (a->address > b->address) - (a->address < b->address);
9366
9367 /* Sort permanent breakpoints first. */
9368 if (a_perm != b_perm)
9369 return (a_perm < b_perm) - (a_perm > b_perm);
9370
9371 /* Make the user-visible order stable across GDB runs. Locations of the same
9372 breakpoint can be sorted in arbitrary order. */
9373
9374 if (a->owner->number != b->owner->number)
9375 return (a->owner->number > b->owner->number)
9376 - (a->owner->number < b->owner->number);
9377
9378 return (a > b) - (a < b);
9379}
9380
876fa593
JK
9381/* Set bp_location_placed_address_before_address_max and
9382 bp_location_shadow_len_after_address_max according to the current content of
9383 the bp_location array. */
f7545552
TT
9384
9385static void
876fa593 9386bp_location_target_extensions_update (void)
f7545552 9387{
876fa593
JK
9388 struct bp_location *bl, **blp_tmp;
9389
9390 bp_location_placed_address_before_address_max = 0;
9391 bp_location_shadow_len_after_address_max = 0;
9392
9393 ALL_BP_LOCATIONS (bl, blp_tmp)
9394 {
9395 CORE_ADDR start, end, addr;
9396
9397 if (!bp_location_has_shadow (bl))
9398 continue;
9399
9400 start = bl->target_info.placed_address;
9401 end = start + bl->target_info.shadow_len;
9402
9403 gdb_assert (bl->address >= start);
9404 addr = bl->address - start;
9405 if (addr > bp_location_placed_address_before_address_max)
9406 bp_location_placed_address_before_address_max = addr;
9407
9408 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
9409
9410 gdb_assert (bl->address < end);
9411 addr = end - bl->address;
9412 if (addr > bp_location_shadow_len_after_address_max)
9413 bp_location_shadow_len_after_address_max = addr;
9414 }
f7545552
TT
9415}
9416
4cd9bd08 9417/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
9418 into the inferior, only remove already-inserted locations that no
9419 longer should be inserted. Functions that delete a breakpoint or
9420 breakpoints should pass false, so that deleting a breakpoint
9421 doesn't have the side effect of inserting the locations of other
9422 breakpoints that are marked not-inserted, but should_be_inserted
9423 returns true on them.
9424
9425 This behaviour is useful is situations close to tear-down -- e.g.,
9426 after an exec, while the target still has execution, but breakpoint
9427 shadows of the previous executable image should *NOT* be restored
9428 to the new image; or before detaching, where the target still has
9429 execution and wants to delete breakpoints from GDB's lists, and all
9430 breakpoints had already been removed from the inferior. */
9431
0d381245 9432static void
b60e7edf 9433update_global_location_list (int should_insert)
0d381245 9434{
74960c60 9435 struct breakpoint *b;
876fa593 9436 struct bp_location **locp, *loc;
f7545552
TT
9437 struct cleanup *cleanups;
9438
2d134ed3
PA
9439 /* Used in the duplicates detection below. When iterating over all
9440 bp_locations, points to the first bp_location of a given address.
9441 Breakpoints and watchpoints of different types are never
9442 duplicates of each other. Keep one pointer for each type of
9443 breakpoint/watchpoint, so we only need to loop over all locations
9444 once. */
9445 struct bp_location *bp_loc_first; /* breakpoint */
9446 struct bp_location *wp_loc_first; /* hardware watchpoint */
9447 struct bp_location *awp_loc_first; /* access watchpoint */
9448 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593
JK
9449
9450 /* Saved former bp_location array which we compare against the newly built
9451 bp_location from the current state of ALL_BREAKPOINTS. */
9452 struct bp_location **old_location, **old_locp;
9453 unsigned old_location_count;
9454
9455 old_location = bp_location;
9456 old_location_count = bp_location_count;
9457 bp_location = NULL;
9458 bp_location_count = 0;
9459 cleanups = make_cleanup (xfree, old_location);
0d381245 9460
74960c60 9461 ALL_BREAKPOINTS (b)
876fa593
JK
9462 for (loc = b->loc; loc; loc = loc->next)
9463 bp_location_count++;
9464
9465 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
9466 locp = bp_location;
9467 ALL_BREAKPOINTS (b)
9468 for (loc = b->loc; loc; loc = loc->next)
9469 *locp++ = loc;
9470 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 9471 bp_location_compare);
876fa593
JK
9472
9473 bp_location_target_extensions_update ();
74960c60
VP
9474
9475 /* Identify bp_location instances that are no longer present in the new
9476 list, and therefore should be freed. Note that it's not necessary that
9477 those locations should be removed from inferior -- if there's another
9478 location at the same address (previously marked as duplicate),
876fa593
JK
9479 we don't need to remove/insert the location.
9480
9481 LOCP is kept in sync with OLD_LOCP, each pointing to the current and
9482 former bp_location array state respectively. */
9483
9484 locp = bp_location;
9485 for (old_locp = old_location; old_locp < old_location + old_location_count;
9486 old_locp++)
74960c60 9487 {
876fa593 9488 struct bp_location *old_loc = *old_locp;
c7d46a38 9489 struct bp_location **loc2p;
876fa593
JK
9490
9491 /* Tells if 'old_loc' is found amoung the new locations. If not, we
74960c60 9492 have to free it. */
c7d46a38 9493 int found_object = 0;
20874c92
VP
9494 /* Tells if the location should remain inserted in the target. */
9495 int keep_in_target = 0;
9496 int removed = 0;
876fa593
JK
9497
9498 /* Skip LOCP entries which will definitely never be needed. Stop either
9499 at or being the one matching OLD_LOC. */
9500 while (locp < bp_location + bp_location_count
c7d46a38 9501 && (*locp)->address < old_loc->address)
876fa593 9502 locp++;
c7d46a38
PA
9503
9504 for (loc2p = locp;
9505 (loc2p < bp_location + bp_location_count
9506 && (*loc2p)->address == old_loc->address);
9507 loc2p++)
9508 {
9509 if (*loc2p == old_loc)
9510 {
9511 found_object = 1;
9512 break;
9513 }
9514 }
74960c60
VP
9515
9516 /* If this location is no longer present, and inserted, look if there's
9517 maybe a new location at the same address. If so, mark that one
9518 inserted, and don't remove this one. This is needed so that we
9519 don't have a time window where a breakpoint at certain location is not
9520 inserted. */
9521
876fa593 9522 if (old_loc->inserted)
0d381245 9523 {
74960c60 9524 /* If the location is inserted now, we might have to remove it. */
74960c60 9525
876fa593 9526 if (found_object && should_be_inserted (old_loc))
74960c60
VP
9527 {
9528 /* The location is still present in the location list, and still
9529 should be inserted. Don't do anything. */
20874c92 9530 keep_in_target = 1;
74960c60
VP
9531 }
9532 else
9533 {
9534 /* The location is either no longer present, or got disabled.
9535 See if there's another location at the same address, in which
9536 case we don't need to remove this one from the target. */
876fa593 9537
2bdf28a0 9538 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
9539 if (breakpoint_address_is_meaningful (old_loc->owner))
9540 {
876fa593 9541 for (loc2p = locp;
c7d46a38
PA
9542 (loc2p < bp_location + bp_location_count
9543 && (*loc2p)->address == old_loc->address);
876fa593
JK
9544 loc2p++)
9545 {
9546 struct bp_location *loc2 = *loc2p;
9547
2d134ed3 9548 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38
PA
9549 {
9550 /* For the sake of should_be_inserted.
9551 Duplicates check below will fix up this later. */
9552 loc2->duplicate = 0;
85d721b8
PA
9553
9554 /* Read watchpoint locations are switched to
9555 access watchpoints, if the former are not
9556 supported, but the latter are. */
9557 if (is_hardware_watchpoint (old_loc->owner))
9558 {
9559 gdb_assert (is_hardware_watchpoint (loc2->owner));
9560 loc2->watchpoint_type = old_loc->watchpoint_type;
9561 }
9562
c7d46a38
PA
9563 if (loc2 != old_loc && should_be_inserted (loc2))
9564 {
9565 loc2->inserted = 1;
9566 loc2->target_info = old_loc->target_info;
9567 keep_in_target = 1;
9568 break;
9569 }
876fa593
JK
9570 }
9571 }
9572 }
74960c60
VP
9573 }
9574
20874c92
VP
9575 if (!keep_in_target)
9576 {
876fa593 9577 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92
VP
9578 {
9579 /* This is just about all we can do. We could keep this
9580 location on the global list, and try to remove it next
9581 time, but there's no particular reason why we will
9582 succeed next time.
9583
876fa593 9584 Note that at this point, old_loc->owner is still valid,
20874c92
VP
9585 as delete_breakpoint frees the breakpoint only
9586 after calling us. */
9587 printf_filtered (_("warning: Error removing breakpoint %d\n"),
876fa593 9588 old_loc->owner->number);
20874c92
VP
9589 }
9590 removed = 1;
9591 }
0d381245 9592 }
74960c60
VP
9593
9594 if (!found_object)
1c5cfe86 9595 {
db82e815
PA
9596 if (removed && non_stop
9597 && breakpoint_address_is_meaningful (old_loc->owner)
9598 && !is_hardware_watchpoint (old_loc->owner))
20874c92 9599 {
db82e815
PA
9600 /* This location was removed from the target. In
9601 non-stop mode, a race condition is possible where
9602 we've removed a breakpoint, but stop events for that
9603 breakpoint are already queued and will arrive later.
9604 We apply an heuristic to be able to distinguish such
9605 SIGTRAPs from other random SIGTRAPs: we keep this
9606 breakpoint location for a bit, and will retire it
9607 after we see some number of events. The theory here
9608 is that reporting of events should, "on the average",
9609 be fair, so after a while we'll see events from all
9610 threads that have anything of interest, and no longer
9611 need to keep this breakpoint location around. We
9612 don't hold locations forever so to reduce chances of
9613 mistaking a non-breakpoint SIGTRAP for a breakpoint
9614 SIGTRAP.
9615
9616 The heuristic failing can be disastrous on
9617 decr_pc_after_break targets.
9618
9619 On decr_pc_after_break targets, like e.g., x86-linux,
9620 if we fail to recognize a late breakpoint SIGTRAP,
9621 because events_till_retirement has reached 0 too
9622 soon, we'll fail to do the PC adjustment, and report
9623 a random SIGTRAP to the user. When the user resumes
9624 the inferior, it will most likely immediately crash
2dec564e 9625 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
9626 corrupted, because of being resumed e.g., in the
9627 middle of a multi-byte instruction, or skipped a
9628 one-byte instruction. This was actually seen happen
9629 on native x86-linux, and should be less rare on
9630 targets that do not support new thread events, like
9631 remote, due to the heuristic depending on
9632 thread_count.
9633
9634 Mistaking a random SIGTRAP for a breakpoint trap
9635 causes similar symptoms (PC adjustment applied when
9636 it shouldn't), but then again, playing with SIGTRAPs
9637 behind the debugger's back is asking for trouble.
9638
9639 Since hardware watchpoint traps are always
9640 distinguishable from other traps, so we don't need to
9641 apply keep hardware watchpoint moribund locations
9642 around. We simply always ignore hardware watchpoint
9643 traps we can no longer explain. */
9644
876fa593
JK
9645 old_loc->events_till_retirement = 3 * (thread_count () + 1);
9646 old_loc->owner = NULL;
20874c92 9647
876fa593 9648 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
9649 }
9650 else
f431efe5
PA
9651 {
9652 old_loc->owner = NULL;
9653 decref_bp_location (&old_loc);
9654 }
20874c92 9655 }
74960c60 9656 }
1c5cfe86 9657
2d134ed3
PA
9658 /* Rescan breakpoints at the same address and section, marking the
9659 first one as "first" and any others as "duplicates". This is so
9660 that the bpt instruction is only inserted once. If we have a
9661 permanent breakpoint at the same place as BPT, make that one the
9662 official one, and the rest as duplicates. Permanent breakpoints
9663 are sorted first for the same address.
9664
9665 Do the same for hardware watchpoints, but also considering the
9666 watchpoint's type (regular/access/read) and length. */
876fa593 9667
2d134ed3
PA
9668 bp_loc_first = NULL;
9669 wp_loc_first = NULL;
9670 awp_loc_first = NULL;
9671 rwp_loc_first = NULL;
876fa593 9672 ALL_BP_LOCATIONS (loc, locp)
74960c60 9673 {
2bdf28a0 9674 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
876fa593 9675 struct breakpoint *b = loc->owner;
2d134ed3 9676 struct bp_location **loc_first_p;
876fa593
JK
9677
9678 if (b->enable_state == bp_disabled
9679 || b->enable_state == bp_call_disabled
9680 || b->enable_state == bp_startup_disabled
9681 || !loc->enabled
9682 || loc->shlib_disabled
15c3d785 9683 || !breakpoint_address_is_meaningful (b)
d77f58be 9684 || is_tracepoint (b))
876fa593
JK
9685 continue;
9686
9687 /* Permanent breakpoint should always be inserted. */
9688 if (b->enable_state == bp_permanent && ! loc->inserted)
9689 internal_error (__FILE__, __LINE__,
9690 _("allegedly permanent breakpoint is not "
9691 "actually inserted"));
9692
2d134ed3
PA
9693 if (b->type == bp_hardware_watchpoint)
9694 loc_first_p = &wp_loc_first;
9695 else if (b->type == bp_read_watchpoint)
9696 loc_first_p = &rwp_loc_first;
9697 else if (b->type == bp_access_watchpoint)
9698 loc_first_p = &awp_loc_first;
9699 else
9700 loc_first_p = &bp_loc_first;
9701
9702 if (*loc_first_p == NULL
9703 || (overlay_debugging && loc->section != (*loc_first_p)->section)
9704 || !breakpoint_locations_match (loc, *loc_first_p))
876fa593 9705 {
2d134ed3 9706 *loc_first_p = loc;
876fa593
JK
9707 loc->duplicate = 0;
9708 continue;
9709 }
9710
9711 loc->duplicate = 1;
9712
2d134ed3
PA
9713 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
9714 && b->enable_state != bp_permanent)
876fa593
JK
9715 internal_error (__FILE__, __LINE__,
9716 _("another breakpoint was inserted on top of "
9717 "a permanent breakpoint"));
0d381245 9718 }
74960c60 9719
50c71eaf 9720 if (breakpoints_always_inserted_mode () && should_insert
c35b1492 9721 && (have_live_inferiors ()
2567c7d9 9722 || (gdbarch_has_global_breakpoints (target_gdbarch))))
74960c60 9723 insert_breakpoint_locations ();
f7545552
TT
9724
9725 do_cleanups (cleanups);
74960c60
VP
9726}
9727
20874c92
VP
9728void
9729breakpoint_retire_moribund (void)
9730{
9731 struct bp_location *loc;
9732 int ix;
9733
9734 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
9735 if (--(loc->events_till_retirement) == 0)
9736 {
f431efe5 9737 decref_bp_location (&loc);
20874c92
VP
9738 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
9739 --ix;
9740 }
9741}
9742
74960c60 9743static void
b60e7edf 9744update_global_location_list_nothrow (int inserting)
74960c60
VP
9745{
9746 struct gdb_exception e;
cc59ec59 9747
74960c60 9748 TRY_CATCH (e, RETURN_MASK_ERROR)
b60e7edf 9749 update_global_location_list (inserting);
0d381245
VP
9750}
9751
f431efe5
PA
9752/* Clear BKP from a BPS. */
9753
a474d7c2 9754static void
f431efe5 9755bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
a474d7c2
PA
9756{
9757 bpstat bs;
cc59ec59 9758
a474d7c2 9759 for (bs = bps; bs; bs = bs->next)
f431efe5 9760 if (bs->breakpoint_at == bpt)
a474d7c2
PA
9761 {
9762 bs->breakpoint_at = NULL;
9763 bs->old_val = NULL;
9764 /* bs->commands will be freed later. */
9765 }
9766}
9767
9768/* Callback for iterate_over_threads. */
9769static int
f431efe5 9770bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
a474d7c2 9771{
f431efe5 9772 struct breakpoint *bpt = data;
cc59ec59 9773
16c381f0 9774 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
a474d7c2
PA
9775 return 0;
9776}
9777
53a5351d 9778/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 9779 structures. */
c906108c
SS
9780
9781void
fba45db2 9782delete_breakpoint (struct breakpoint *bpt)
c906108c 9783{
52f0bd74 9784 struct breakpoint *b;
c906108c 9785
8a3fe4f8 9786 gdb_assert (bpt != NULL);
c906108c
SS
9787
9788 /* Has this bp already been deleted? This can happen because multiple
9789 lists can hold pointers to bp's. bpstat lists are especial culprits.
9790
9791 One example of this happening is a watchpoint's scope bp. When the
9792 scope bp triggers, we notice that the watchpoint is out of scope, and
9793 delete it. We also delete its scope bp. But the scope bp is marked
9794 "auto-deleting", and is already on a bpstat. That bpstat is then
9795 checked for auto-deleting bp's, which are deleted.
9796
9797 A real solution to this problem might involve reference counts in bp's,
9798 and/or giving them pointers back to their referencing bpstat's, and
9799 teaching delete_breakpoint to only free a bp's storage when no more
1272ad14 9800 references were extent. A cheaper bandaid was chosen. */
c906108c
SS
9801 if (bpt->type == bp_none)
9802 return;
9803
e5a0a904
JK
9804 /* At least avoid this stale reference until the reference counting of
9805 breakpoints gets resolved. */
9806 if (bpt->related_breakpoint != NULL)
9807 {
9808 gdb_assert (bpt->related_breakpoint->related_breakpoint == bpt);
9809 bpt->related_breakpoint->disposition = disp_del_at_next_stop;
9810 bpt->related_breakpoint->related_breakpoint = NULL;
9811 bpt->related_breakpoint = NULL;
9812 }
9813
383f836e 9814 observer_notify_breakpoint_deleted (bpt->number);
c906108c 9815
c906108c
SS
9816 if (breakpoint_chain == bpt)
9817 breakpoint_chain = bpt->next;
9818
c906108c
SS
9819 ALL_BREAKPOINTS (b)
9820 if (b->next == bpt)
c5aa993b
JM
9821 {
9822 b->next = bpt->next;
9823 break;
9824 }
c906108c 9825
9add0f1b 9826 decref_counted_command_line (&bpt->commands);
60e1c644
PA
9827 xfree (bpt->cond_string);
9828 xfree (bpt->cond_exp);
9829 xfree (bpt->addr_string);
9830 xfree (bpt->exp);
9831 xfree (bpt->exp_string);
d63d0675 9832 xfree (bpt->exp_string_reparse);
60e1c644
PA
9833 value_free (bpt->val);
9834 xfree (bpt->source_file);
9835 xfree (bpt->exec_pathname);
a96d9b2e 9836 clean_up_filters (&bpt->syscalls_to_be_caught);
c906108c 9837
f431efe5
PA
9838
9839 /* Be sure no bpstat's are pointing at the breakpoint after it's
9840 been freed. */
9841 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
9842 in all threeds for now. Note that we cannot just remove bpstats
9843 pointing at bpt from the stop_bpstat list entirely, as breakpoint
9844 commands are associated with the bpstat; if we remove it here,
9845 then the later call to bpstat_do_actions (&stop_bpstat); in
9846 event-top.c won't do anything, and temporary breakpoints with
9847 commands won't work. */
9848
9849 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
9850
74960c60
VP
9851 /* Now that breakpoint is removed from breakpoint
9852 list, update the global location list. This
9853 will remove locations that used to belong to
9854 this breakpoint. Do this before freeing
9855 the breakpoint itself, since remove_breakpoint
9856 looks at location's owner. It might be better
9857 design to have location completely self-contained,
9858 but it's not the case now. */
b60e7edf 9859 update_global_location_list (0);
74960c60
VP
9860
9861
c906108c
SS
9862 /* On the chance that someone will soon try again to delete this same
9863 bp, we mark it as deleted before freeing its storage. */
9864 bpt->type = bp_none;
9865
b8c9b27d 9866 xfree (bpt);
c906108c
SS
9867}
9868
4d6140d9
AC
9869static void
9870do_delete_breakpoint_cleanup (void *b)
9871{
9872 delete_breakpoint (b);
9873}
9874
9875struct cleanup *
9876make_cleanup_delete_breakpoint (struct breakpoint *b)
9877{
9878 return make_cleanup (do_delete_breakpoint_cleanup, b);
9879}
9880
95a42b64
TT
9881/* A callback for map_breakpoint_numbers that calls
9882 delete_breakpoint. */
9883
9884static void
9885do_delete_breakpoint (struct breakpoint *b, void *ignore)
9886{
9887 delete_breakpoint (b);
9888}
9889
c906108c 9890void
fba45db2 9891delete_command (char *arg, int from_tty)
c906108c 9892{
35df4500 9893 struct breakpoint *b, *b_tmp;
c906108c 9894
ea9365bb
TT
9895 dont_repeat ();
9896
c906108c
SS
9897 if (arg == 0)
9898 {
9899 int breaks_to_delete = 0;
9900
9901 /* Delete all breakpoints if no argument.
c5aa993b
JM
9902 Do not delete internal or call-dummy breakpoints, these
9903 have to be deleted with an explicit breakpoint number argument. */
9904 ALL_BREAKPOINTS (b)
9905 {
059fb39f 9906 if (b->type != bp_call_dummy
aa7d318d 9907 && b->type != bp_std_terminate
059fb39f 9908 && b->type != bp_shlib_event
4efc6507 9909 && b->type != bp_jit_event
059fb39f
PM
9910 && b->type != bp_thread_event
9911 && b->type != bp_overlay_event
0fd8e87f 9912 && b->type != bp_longjmp_master
aa7d318d 9913 && b->type != bp_std_terminate_master
186c406b 9914 && b->type != bp_exception_master
059fb39f 9915 && b->number >= 0)
973d738b
DJ
9916 {
9917 breaks_to_delete = 1;
9918 break;
9919 }
c5aa993b 9920 }
c906108c
SS
9921
9922 /* Ask user only if there are some breakpoints to delete. */
9923 if (!from_tty
e2e0b3e5 9924 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 9925 {
35df4500 9926 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 9927 {
059fb39f 9928 if (b->type != bp_call_dummy
aa7d318d 9929 && b->type != bp_std_terminate
059fb39f
PM
9930 && b->type != bp_shlib_event
9931 && b->type != bp_thread_event
4efc6507 9932 && b->type != bp_jit_event
059fb39f 9933 && b->type != bp_overlay_event
0fd8e87f 9934 && b->type != bp_longjmp_master
aa7d318d 9935 && b->type != bp_std_terminate_master
186c406b 9936 && b->type != bp_exception_master
059fb39f 9937 && b->number >= 0)
c5aa993b
JM
9938 delete_breakpoint (b);
9939 }
c906108c
SS
9940 }
9941 }
9942 else
95a42b64 9943 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
c906108c
SS
9944}
9945
0d381245
VP
9946static int
9947all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 9948{
0d381245
VP
9949 for (; loc; loc = loc->next)
9950 if (!loc->shlib_disabled)
9951 return 0;
9952 return 1;
fe3f5fa8
VP
9953}
9954
776592bf
DE
9955/* Subroutine of update_breakpoint_locations to simplify it.
9956 Return non-zero if multiple fns in list LOC have the same name.
9957 Null names are ignored. */
9958
9959static int
9960ambiguous_names_p (struct bp_location *loc)
9961{
9962 struct bp_location *l;
9963 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
9964 (int (*) (const void *,
9965 const void *)) streq,
776592bf
DE
9966 NULL, xcalloc, xfree);
9967
9968 for (l = loc; l != NULL; l = l->next)
9969 {
9970 const char **slot;
9971 const char *name = l->function_name;
9972
9973 /* Allow for some names to be NULL, ignore them. */
9974 if (name == NULL)
9975 continue;
9976
9977 slot = (const char **) htab_find_slot (htab, (const void *) name,
9978 INSERT);
9979 /* NOTE: We can assume slot != NULL here because xcalloc never returns
9980 NULL. */
9981 if (*slot != NULL)
9982 {
9983 htab_delete (htab);
9984 return 1;
9985 }
9986 *slot = name;
9987 }
9988
9989 htab_delete (htab);
9990 return 0;
9991}
9992
0fb4aa4b
PA
9993/* When symbols change, it probably means the sources changed as well,
9994 and it might mean the static tracepoint markers are no longer at
9995 the same address or line numbers they used to be at last we
9996 checked. Losing your static tracepoints whenever you rebuild is
9997 undesirable. This function tries to resync/rematch gdb static
9998 tracepoints with the markers on the target, for static tracepoints
9999 that have not been set by marker id. Static tracepoint that have
10000 been set by marker id are reset by marker id in breakpoint_re_set.
10001 The heuristic is:
10002
10003 1) For a tracepoint set at a specific address, look for a marker at
10004 the old PC. If one is found there, assume to be the same marker.
10005 If the name / string id of the marker found is different from the
10006 previous known name, assume that means the user renamed the marker
10007 in the sources, and output a warning.
10008
10009 2) For a tracepoint set at a given line number, look for a marker
10010 at the new address of the old line number. If one is found there,
10011 assume to be the same marker. If the name / string id of the
10012 marker found is different from the previous known name, assume that
10013 means the user renamed the marker in the sources, and output a
10014 warning.
10015
10016 3) If a marker is no longer found at the same address or line, it
10017 may mean the marker no longer exists. But it may also just mean
10018 the code changed a bit. Maybe the user added a few lines of code
10019 that made the marker move up or down (in line number terms). Ask
10020 the target for info about the marker with the string id as we knew
10021 it. If found, update line number and address in the matching
10022 static tracepoint. This will get confused if there's more than one
10023 marker with the same ID (possible in UST, although unadvised
10024 precisely because it confuses tools). */
10025
10026static struct symtab_and_line
10027update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
10028{
10029 struct static_tracepoint_marker marker;
10030 CORE_ADDR pc;
10031 int i;
10032
10033 pc = sal.pc;
10034 if (sal.line)
10035 find_line_pc (sal.symtab, sal.line, &pc);
10036
10037 if (target_static_tracepoint_marker_at (pc, &marker))
10038 {
10039 if (strcmp (b->static_trace_marker_id, marker.str_id) != 0)
10040 warning (_("static tracepoint %d changed probed marker from %s to %s"),
10041 b->number,
10042 b->static_trace_marker_id, marker.str_id);
10043
10044 xfree (b->static_trace_marker_id);
10045 b->static_trace_marker_id = xstrdup (marker.str_id);
10046 release_static_tracepoint_marker (&marker);
10047
10048 return sal;
10049 }
10050
10051 /* Old marker wasn't found on target at lineno. Try looking it up
10052 by string ID. */
10053 if (!sal.explicit_pc
10054 && sal.line != 0
10055 && sal.symtab != NULL
10056 && b->static_trace_marker_id != NULL)
10057 {
10058 VEC(static_tracepoint_marker_p) *markers;
10059
10060 markers
10061 = target_static_tracepoint_markers_by_strid (b->static_trace_marker_id);
10062
10063 if (!VEC_empty(static_tracepoint_marker_p, markers))
10064 {
10065 struct symtab_and_line sal;
10066 struct symbol *sym;
10067 struct static_tracepoint_marker *marker;
10068
10069 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
10070
10071 xfree (b->static_trace_marker_id);
10072 b->static_trace_marker_id = xstrdup (marker->str_id);
10073
10074 warning (_("marker for static tracepoint %d (%s) not "
10075 "found at previous line number"),
10076 b->number, b->static_trace_marker_id);
10077
10078 init_sal (&sal);
10079
10080 sal.pc = marker->address;
10081
10082 sal = find_pc_line (marker->address, 0);
10083 sym = find_pc_sect_function (marker->address, NULL);
10084 ui_out_text (uiout, "Now in ");
10085 if (sym)
10086 {
10087 ui_out_field_string (uiout, "func",
10088 SYMBOL_PRINT_NAME (sym));
10089 ui_out_text (uiout, " at ");
10090 }
10091 ui_out_field_string (uiout, "file", sal.symtab->filename);
10092 ui_out_text (uiout, ":");
10093
10094 if (ui_out_is_mi_like_p (uiout))
10095 {
10096 char *fullname = symtab_to_fullname (sal.symtab);
10097
10098 if (fullname)
10099 ui_out_field_string (uiout, "fullname", fullname);
10100 }
10101
10102 ui_out_field_int (uiout, "line", sal.line);
10103 ui_out_text (uiout, "\n");
10104
10105 b->line_number = sal.line;
10106
10107 xfree (b->source_file);
10108 if (sym)
10109 b->source_file = xstrdup (sal.symtab->filename);
10110 else
10111 b->source_file = NULL;
10112
10113 xfree (b->addr_string);
10114 b->addr_string = xstrprintf ("%s:%d",
10115 sal.symtab->filename, b->line_number);
10116
10117 /* Might be nice to check if function changed, and warn if
10118 so. */
10119
10120 release_static_tracepoint_marker (marker);
10121 }
10122 }
10123 return sal;
10124}
10125
fe3f5fa8 10126static void
0d381245
VP
10127update_breakpoint_locations (struct breakpoint *b,
10128 struct symtabs_and_lines sals)
fe3f5fa8
VP
10129{
10130 int i;
10131 char *s;
0d381245
VP
10132 struct bp_location *existing_locations = b->loc;
10133
10134 /* If there's no new locations, and all existing locations
10135 are pending, don't do anything. This optimizes
10136 the common case where all locations are in the same
10137 shared library, that was unloaded. We'd like to
10138 retain the location, so that when the library
10139 is loaded again, we don't loose the enabled/disabled
10140 status of the individual locations. */
10141 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
10142 return;
10143
fe3f5fa8
VP
10144 b->loc = NULL;
10145
0d381245 10146 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 10147 {
0d381245 10148 struct bp_location *new_loc =
39d61571 10149 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 10150
0d381245
VP
10151 /* Reparse conditions, they might contain references to the
10152 old symtab. */
10153 if (b->cond_string != NULL)
10154 {
10155 struct gdb_exception e;
fe3f5fa8 10156
0d381245
VP
10157 s = b->cond_string;
10158 TRY_CATCH (e, RETURN_MASK_ERROR)
10159 {
10160 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
10161 0);
10162 }
10163 if (e.reason < 0)
10164 {
10165 warning (_("failed to reevaluate condition for breakpoint %d: %s"),
10166 b->number, e.message);
10167 new_loc->enabled = 0;
10168 }
10169 }
fe3f5fa8 10170
0d381245
VP
10171 if (b->source_file != NULL)
10172 xfree (b->source_file);
10173 if (sals.sals[i].symtab == NULL)
10174 b->source_file = NULL;
10175 else
1b36a34b 10176 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 10177
0d381245
VP
10178 if (b->line_number == 0)
10179 b->line_number = sals.sals[i].line;
10180 }
fe3f5fa8 10181
514f746b
AR
10182 /* Update locations of permanent breakpoints. */
10183 if (b->enable_state == bp_permanent)
10184 make_breakpoint_permanent (b);
10185
0d381245
VP
10186 /* If possible, carry over 'disable' status from existing breakpoints. */
10187 {
10188 struct bp_location *e = existing_locations;
776592bf
DE
10189 /* If there are multiple breakpoints with the same function name,
10190 e.g. for inline functions, comparing function names won't work.
10191 Instead compare pc addresses; this is just a heuristic as things
10192 may have moved, but in practice it gives the correct answer
10193 often enough until a better solution is found. */
10194 int have_ambiguous_names = ambiguous_names_p (b->loc);
10195
0d381245
VP
10196 for (; e; e = e->next)
10197 {
10198 if (!e->enabled && e->function_name)
10199 {
10200 struct bp_location *l = b->loc;
776592bf
DE
10201 if (have_ambiguous_names)
10202 {
10203 for (; l; l = l->next)
6c95b8df
PA
10204 if (breakpoint_address_match (e->pspace->aspace, e->address,
10205 l->pspace->aspace, l->address))
776592bf
DE
10206 {
10207 l->enabled = 0;
10208 break;
10209 }
10210 }
10211 else
10212 {
10213 for (; l; l = l->next)
10214 if (l->function_name
10215 && strcmp (e->function_name, l->function_name) == 0)
10216 {
10217 l->enabled = 0;
10218 break;
10219 }
10220 }
0d381245
VP
10221 }
10222 }
10223 }
fe3f5fa8 10224
b60e7edf 10225 update_global_location_list (1);
fe3f5fa8
VP
10226}
10227
c906108c
SS
10228/* Reset a breakpoint given it's struct breakpoint * BINT.
10229 The value we return ends up being the return value from catch_errors.
10230 Unused in this case. */
10231
10232static int
4efb68b1 10233breakpoint_re_set_one (void *bint)
c906108c 10234{
53a5351d
JM
10235 /* get past catch_errs */
10236 struct breakpoint *b = (struct breakpoint *) bint;
fe3f5fa8
VP
10237 int not_found = 0;
10238 int *not_found_ptr = &not_found;
6c95b8df
PA
10239 struct symtabs_and_lines sals = {0};
10240 struct symtabs_and_lines expanded = {0};
c906108c 10241 char *s;
fe3f5fa8 10242 struct gdb_exception e;
6c95b8df 10243 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
0fb4aa4b 10244 int marker_spec = 0;
c906108c
SS
10245
10246 switch (b->type)
10247 {
10248 case bp_none:
8a3fe4f8 10249 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
53a5351d 10250 b->number);
c906108c
SS
10251 return 0;
10252 case bp_breakpoint:
10253 case bp_hardware_breakpoint:
1042e4c0 10254 case bp_tracepoint:
7a697b8d 10255 case bp_fast_tracepoint:
0fb4aa4b 10256 case bp_static_tracepoint:
8bea4e01
UW
10257 /* Do not attempt to re-set breakpoints disabled during startup. */
10258 if (b->enable_state == bp_startup_disabled)
10259 return 0;
10260
c906108c
SS
10261 if (b->addr_string == NULL)
10262 {
10263 /* Anything without a string can't be re-set. */
10264 delete_breakpoint (b);
10265 return 0;
10266 }
c906108c 10267
c906108c
SS
10268 input_radix = b->input_radix;
10269 s = b->addr_string;
6c95b8df
PA
10270
10271 save_current_space_and_thread ();
10272 switch_to_program_space_and_thread (b->pspace);
10273
0fb4aa4b
PA
10274 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
10275
385d04dc 10276 set_language (b->language);
fe3f5fa8 10277 TRY_CATCH (e, RETURN_MASK_ERROR)
c906108c 10278 {
0fb4aa4b
PA
10279 if (marker_spec)
10280 {
10281 sals = decode_static_tracepoint_spec (&s);
10282 if (sals.nelts > b->static_trace_marker_id_idx)
10283 {
10284 sals.sals[0] = sals.sals[b->static_trace_marker_id_idx];
10285 sals.nelts = 1;
10286 }
10287 else
10288 error (_("marker %s not found"), b->static_trace_marker_id);
10289 }
10290 else
10291 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, (char ***) NULL,
10292 not_found_ptr);
fe3f5fa8
VP
10293 }
10294 if (e.reason < 0)
10295 {
10296 int not_found_and_ok = 0;
10297 /* For pending breakpoints, it's expected that parsing
10298 will fail until the right shared library is loaded.
10299 User has already told to create pending breakpoints and
10300 don't need extra messages. If breakpoint is in bp_shlib_disabled
10301 state, then user already saw the message about that breakpoint
10302 being disabled, and don't want to see more errors. */
0d381245
VP
10303 if (not_found
10304 && (b->condition_not_parsed
10305 || (b->loc && b->loc->shlib_disabled)
10306 || b->enable_state == bp_disabled))
fe3f5fa8
VP
10307 not_found_and_ok = 1;
10308
10309 if (!not_found_and_ok)
c906108c 10310 {
fe3f5fa8
VP
10311 /* We surely don't want to warn about the same breakpoint
10312 10 times. One solution, implemented here, is disable
10313 the breakpoint on error. Another solution would be to
10314 have separate 'warning emitted' flag. Since this
10315 happens only when a binary has changed, I don't know
10316 which approach is better. */
10317 b->enable_state = bp_disabled;
10318 throw_exception (e);
c906108c 10319 }
fe3f5fa8 10320 }
c906108c 10321
6c95b8df 10322 if (!not_found)
fe3f5fa8 10323 {
6c95b8df
PA
10324 gdb_assert (sals.nelts == 1);
10325
10326 resolve_sal_pc (&sals.sals[0]);
10327 if (b->condition_not_parsed && s && s[0])
10328 {
10329 char *cond_string = 0;
10330 int thread = -1;
10331 int task = 0;
10332
10333 find_condition_and_thread (s, sals.sals[0].pc,
10334 &cond_string, &thread, &task);
10335 if (cond_string)
10336 b->cond_string = cond_string;
10337 b->thread = thread;
10338 b->task = task;
10339 b->condition_not_parsed = 0;
10340 }
10341
0fb4aa4b
PA
10342 if (b->type == bp_static_tracepoint && !marker_spec)
10343 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
10344
6c95b8df 10345 expanded = expand_line_sal_maybe (sals.sals[0]);
fe3f5fa8 10346 }
6c95b8df
PA
10347
10348 make_cleanup (xfree, sals.sals);
ed0616c6 10349 update_breakpoint_locations (b, expanded);
c906108c
SS
10350 break;
10351
10352 case bp_watchpoint:
10353 case bp_hardware_watchpoint:
10354 case bp_read_watchpoint:
10355 case bp_access_watchpoint:
0b3de036
VP
10356 /* Watchpoint can be either on expression using entirely global variables,
10357 or it can be on local variables.
10358
10359 Watchpoints of the first kind are never auto-deleted, and even persist
10360 across program restarts. Since they can use variables from shared
10361 libraries, we need to reparse expression as libraries are loaded
10362 and unloaded.
10363
10364 Watchpoints on local variables can also change meaning as result
10365 of solib event. For example, if a watchpoint uses both a local and
10366 a global variables in expression, it's a local watchpoint, but
10367 unloading of a shared library will make the expression invalid.
10368 This is not a very common use case, but we still re-evaluate
10369 expression, to avoid surprises to the user.
10370
10371 Note that for local watchpoints, we re-evaluate it only if
10372 watchpoints frame id is still valid. If it's not, it means
10373 the watchpoint is out of scope and will be deleted soon. In fact,
10374 I'm not sure we'll ever be called in this case.
10375
10376 If a local watchpoint's frame id is still valid, then
10377 b->exp_valid_block is likewise valid, and we can safely use it.
10378
10379 Don't do anything about disabled watchpoints, since they will
10380 be reevaluated again when enabled. */
a5606eee 10381 update_watchpoint (b, 1 /* reparse */);
c906108c 10382 break;
c5aa993b
JM
10383 /* We needn't really do anything to reset these, since the mask
10384 that requests them is unaffected by e.g., new libraries being
10385 loaded. */
ce78b96d 10386 case bp_catchpoint:
c906108c 10387 break;
c5aa993b 10388
c906108c 10389 default:
a3f17187 10390 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
c906108c 10391 /* fall through */
0fd8e87f
UW
10392 /* Delete overlay event and longjmp master breakpoints; they will be
10393 reset later by breakpoint_re_set. */
1900040c 10394 case bp_overlay_event:
0fd8e87f 10395 case bp_longjmp_master:
aa7d318d 10396 case bp_std_terminate_master:
186c406b 10397 case bp_exception_master:
c906108c
SS
10398 delete_breakpoint (b);
10399 break;
10400
c5aa993b
JM
10401 /* This breakpoint is special, it's set up when the inferior
10402 starts and we really don't want to touch it. */
c906108c
SS
10403 case bp_shlib_event:
10404
c4093a6a
JM
10405 /* Like bp_shlib_event, this breakpoint type is special.
10406 Once it is set up, we do not want to touch it. */
10407 case bp_thread_event:
10408
c5aa993b
JM
10409 /* Keep temporary breakpoints, which can be encountered when we step
10410 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
10411 Otherwise these should have been blown away via the cleanup chain
10412 or by breakpoint_init_inferior when we rerun the executable. */
c906108c
SS
10413 case bp_until:
10414 case bp_finish:
10415 case bp_watchpoint_scope:
10416 case bp_call_dummy:
aa7d318d 10417 case bp_std_terminate:
c906108c 10418 case bp_step_resume:
611c83ae
PA
10419 case bp_longjmp:
10420 case bp_longjmp_resume:
186c406b
TT
10421 case bp_exception:
10422 case bp_exception_resume:
4efc6507 10423 case bp_jit_event:
c906108c
SS
10424 break;
10425 }
10426
6c95b8df 10427 do_cleanups (cleanups);
c906108c
SS
10428 return 0;
10429}
10430
69de3c6a 10431/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 10432void
69de3c6a 10433breakpoint_re_set (void)
c906108c 10434{
35df4500 10435 struct breakpoint *b, *b_tmp;
c906108c
SS
10436 enum language save_language;
10437 int save_input_radix;
6c95b8df 10438 struct cleanup *old_chain;
c5aa993b 10439
c906108c
SS
10440 save_language = current_language->la_language;
10441 save_input_radix = input_radix;
6c95b8df
PA
10442 old_chain = save_current_program_space ();
10443
35df4500 10444 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10445 {
53a5351d 10446 /* Format possible error msg */
fe3f5fa8 10447 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
10448 b->number);
10449 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 10450 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 10451 do_cleanups (cleanups);
c5aa993b 10452 }
c906108c
SS
10453 set_language (save_language);
10454 input_radix = save_input_radix;
e62c965a 10455
0756c555 10456 jit_breakpoint_re_set ();
4efc6507 10457
6c95b8df
PA
10458 do_cleanups (old_chain);
10459
69de3c6a 10460 create_overlay_event_breakpoint ("_ovly_debug_event");
0fd8e87f
UW
10461 create_longjmp_master_breakpoint ("longjmp");
10462 create_longjmp_master_breakpoint ("_longjmp");
10463 create_longjmp_master_breakpoint ("siglongjmp");
10464 create_longjmp_master_breakpoint ("_siglongjmp");
aa7d318d 10465 create_std_terminate_master_breakpoint ("std::terminate()");
186c406b 10466 create_exception_master_breakpoint ();
c906108c
SS
10467}
10468\f
c906108c
SS
10469/* Reset the thread number of this breakpoint:
10470
10471 - If the breakpoint is for all threads, leave it as-is.
39f77062 10472 - Else, reset it to the current thread for inferior_ptid. */
c906108c 10473void
fba45db2 10474breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
10475{
10476 if (b->thread != -1)
10477 {
39f77062
KB
10478 if (in_thread_list (inferior_ptid))
10479 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
10480
10481 /* We're being called after following a fork. The new fork is
10482 selected as current, and unless this was a vfork will have a
10483 different program space from the original thread. Reset that
10484 as well. */
10485 b->loc->pspace = current_program_space;
c906108c
SS
10486 }
10487}
10488
03ac34d5
MS
10489/* Set ignore-count of breakpoint number BPTNUM to COUNT.
10490 If from_tty is nonzero, it prints a message to that effect,
10491 which ends with a period (no newline). */
10492
c906108c 10493void
fba45db2 10494set_ignore_count (int bptnum, int count, int from_tty)
c906108c 10495{
52f0bd74 10496 struct breakpoint *b;
c906108c
SS
10497
10498 if (count < 0)
10499 count = 0;
10500
10501 ALL_BREAKPOINTS (b)
10502 if (b->number == bptnum)
c5aa993b 10503 {
d77f58be
SS
10504 if (is_tracepoint (b))
10505 {
10506 if (from_tty && count != 0)
10507 printf_filtered (_("Ignore count ignored for tracepoint %d."),
10508 bptnum);
10509 return;
10510 }
10511
c5aa993b 10512 b->ignore_count = count;
221ea385
KS
10513 if (from_tty)
10514 {
10515 if (count == 0)
a3f17187 10516 printf_filtered (_("Will stop next time breakpoint %d is reached."),
221ea385
KS
10517 bptnum);
10518 else if (count == 1)
a3f17187 10519 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
10520 bptnum);
10521 else
a3f17187 10522 printf_filtered (_("Will ignore next %d crossings of breakpoint %d."),
221ea385
KS
10523 count, bptnum);
10524 }
c5aa993b 10525 breakpoints_changed ();
383f836e 10526 observer_notify_breakpoint_modified (b->number);
c5aa993b
JM
10527 return;
10528 }
c906108c 10529
8a3fe4f8 10530 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
10531}
10532
b2175913
MS
10533void
10534make_breakpoint_silent (struct breakpoint *b)
10535{
10536 /* Silence the breakpoint. */
10537 b->silent = 1;
10538}
10539
c906108c
SS
10540/* Command to set ignore-count of breakpoint N to COUNT. */
10541
10542static void
fba45db2 10543ignore_command (char *args, int from_tty)
c906108c
SS
10544{
10545 char *p = args;
52f0bd74 10546 int num;
c906108c
SS
10547
10548 if (p == 0)
e2e0b3e5 10549 error_no_arg (_("a breakpoint number"));
c5aa993b 10550
c906108c 10551 num = get_number (&p);
5c44784c 10552 if (num == 0)
8a3fe4f8 10553 error (_("bad breakpoint number: '%s'"), args);
c906108c 10554 if (*p == 0)
8a3fe4f8 10555 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
10556
10557 set_ignore_count (num,
10558 longest_to_int (value_as_long (parse_and_eval (p))),
10559 from_tty);
221ea385
KS
10560 if (from_tty)
10561 printf_filtered ("\n");
c906108c
SS
10562}
10563\f
10564/* Call FUNCTION on each of the breakpoints
10565 whose numbers are given in ARGS. */
10566
10567static void
95a42b64
TT
10568map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
10569 void *),
10570 void *data)
c906108c 10571{
52f0bd74 10572 char *p = args;
c906108c 10573 char *p1;
52f0bd74
AC
10574 int num;
10575 struct breakpoint *b, *tmp;
11cf8741 10576 int match;
c906108c
SS
10577
10578 if (p == 0)
e2e0b3e5 10579 error_no_arg (_("one or more breakpoint numbers"));
c906108c
SS
10580
10581 while (*p)
10582 {
11cf8741 10583 match = 0;
c906108c 10584 p1 = p;
c5aa993b 10585
5c44784c
JM
10586 num = get_number_or_range (&p1);
10587 if (num == 0)
c5aa993b 10588 {
8a3fe4f8 10589 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
10590 }
10591 else
10592 {
10593 ALL_BREAKPOINTS_SAFE (b, tmp)
10594 if (b->number == num)
10595 {
10596 struct breakpoint *related_breakpoint = b->related_breakpoint;
11cf8741 10597 match = 1;
95a42b64 10598 function (b, data);
5c44784c 10599 if (related_breakpoint)
95a42b64 10600 function (related_breakpoint, data);
11cf8741 10601 break;
5c44784c 10602 }
11cf8741 10603 if (match == 0)
a3f17187 10604 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 10605 }
c906108c
SS
10606 p = p1;
10607 }
10608}
10609
0d381245
VP
10610static struct bp_location *
10611find_location_by_number (char *number)
10612{
10613 char *dot = strchr (number, '.');
10614 char *p1;
10615 int bp_num;
10616 int loc_num;
10617 struct breakpoint *b;
10618 struct bp_location *loc;
10619
10620 *dot = '\0';
10621
10622 p1 = number;
10623 bp_num = get_number_or_range (&p1);
10624 if (bp_num == 0)
10625 error (_("Bad breakpoint number '%s'"), number);
10626
10627 ALL_BREAKPOINTS (b)
10628 if (b->number == bp_num)
10629 {
10630 break;
10631 }
10632
10633 if (!b || b->number != bp_num)
10634 error (_("Bad breakpoint number '%s'"), number);
10635
10636 p1 = dot+1;
10637 loc_num = get_number_or_range (&p1);
10638 if (loc_num == 0)
10639 error (_("Bad breakpoint location number '%s'"), number);
10640
10641 --loc_num;
10642 loc = b->loc;
10643 for (;loc_num && loc; --loc_num, loc = loc->next)
10644 ;
10645 if (!loc)
10646 error (_("Bad breakpoint location number '%s'"), dot+1);
10647
10648 return loc;
10649}
10650
10651
1900040c
MS
10652/* Set ignore-count of breakpoint number BPTNUM to COUNT.
10653 If from_tty is nonzero, it prints a message to that effect,
10654 which ends with a period (no newline). */
10655
c906108c 10656void
fba45db2 10657disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
10658{
10659 /* Never disable a watchpoint scope breakpoint; we want to
10660 hit them when we leave scope so we can delete both the
10661 watchpoint and its scope breakpoint at that time. */
10662 if (bpt->type == bp_watchpoint_scope)
10663 return;
10664
c2c6d25f 10665 /* You can't disable permanent breakpoints. */
b5de0fa7 10666 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
10667 return;
10668
b5de0fa7 10669 bpt->enable_state = bp_disabled;
c906108c 10670
b60e7edf 10671 update_global_location_list (0);
c906108c 10672
383f836e 10673 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
10674}
10675
95a42b64
TT
10676/* A callback for map_breakpoint_numbers that calls
10677 disable_breakpoint. */
10678
10679static void
10680do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
10681{
10682 disable_breakpoint (b);
10683}
10684
c906108c 10685static void
fba45db2 10686disable_command (char *args, int from_tty)
c906108c 10687{
52f0bd74 10688 struct breakpoint *bpt;
cc59ec59 10689
c906108c
SS
10690 if (args == 0)
10691 ALL_BREAKPOINTS (bpt)
10692 switch (bpt->type)
c5aa993b
JM
10693 {
10694 case bp_none:
8a3fe4f8 10695 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
53a5351d 10696 bpt->number);
c5aa993b
JM
10697 continue;
10698 case bp_breakpoint:
1042e4c0 10699 case bp_tracepoint:
7a697b8d 10700 case bp_fast_tracepoint:
0fb4aa4b 10701 case bp_static_tracepoint:
ce78b96d 10702 case bp_catchpoint:
c5aa993b
JM
10703 case bp_hardware_breakpoint:
10704 case bp_watchpoint:
10705 case bp_hardware_watchpoint:
10706 case bp_read_watchpoint:
10707 case bp_access_watchpoint:
10708 disable_breakpoint (bpt);
10709 default:
10710 continue;
10711 }
0d381245
VP
10712 else if (strchr (args, '.'))
10713 {
10714 struct bp_location *loc = find_location_by_number (args);
10715 if (loc)
10716 loc->enabled = 0;
b60e7edf 10717 update_global_location_list (0);
0d381245 10718 }
c906108c 10719 else
95a42b64 10720 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
10721}
10722
10723static void
fba45db2 10724do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 10725{
afe38095 10726 int target_resources_ok;
c906108c
SS
10727
10728 if (bpt->type == bp_hardware_breakpoint)
10729 {
10730 int i;
c5aa993b 10731 i = hw_breakpoint_used_count ();
53a5351d 10732 target_resources_ok =
d92524f1 10733 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 10734 i + 1, 0);
c906108c 10735 if (target_resources_ok == 0)
8a3fe4f8 10736 error (_("No hardware breakpoint support in the target."));
c906108c 10737 else if (target_resources_ok < 0)
8a3fe4f8 10738 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
10739 }
10740
cc60f2e3 10741 if (is_watchpoint (bpt))
c906108c 10742 {
dde02812
ES
10743 struct gdb_exception e;
10744
10745 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 10746 {
dde02812 10747 update_watchpoint (bpt, 1 /* reparse */);
c906108c 10748 }
dde02812 10749 if (e.reason < 0)
c5aa993b 10750 {
dde02812
ES
10751 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
10752 bpt->number);
10753 return;
c5aa993b 10754 }
c906108c 10755 }
0101ce28 10756
b4c291bb
KH
10757 if (bpt->enable_state != bp_permanent)
10758 bpt->enable_state = bp_enabled;
10759 bpt->disposition = disposition;
b60e7edf 10760 update_global_location_list (1);
b4c291bb
KH
10761 breakpoints_changed ();
10762
383f836e 10763 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
10764}
10765
fe3f5fa8 10766
c906108c 10767void
fba45db2 10768enable_breakpoint (struct breakpoint *bpt)
c906108c
SS
10769{
10770 do_enable_breakpoint (bpt, bpt->disposition);
10771}
10772
95a42b64
TT
10773/* A callback for map_breakpoint_numbers that calls
10774 enable_breakpoint. */
10775
10776static void
10777do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
10778{
10779 enable_breakpoint (b);
10780}
10781
c906108c
SS
10782/* The enable command enables the specified breakpoints (or all defined
10783 breakpoints) so they once again become (or continue to be) effective
1272ad14 10784 in stopping the inferior. */
c906108c 10785
c906108c 10786static void
fba45db2 10787enable_command (char *args, int from_tty)
c906108c 10788{
52f0bd74 10789 struct breakpoint *bpt;
cc59ec59 10790
c906108c
SS
10791 if (args == 0)
10792 ALL_BREAKPOINTS (bpt)
10793 switch (bpt->type)
c5aa993b
JM
10794 {
10795 case bp_none:
8a3fe4f8 10796 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
53a5351d 10797 bpt->number);
c5aa993b
JM
10798 continue;
10799 case bp_breakpoint:
1042e4c0 10800 case bp_tracepoint:
7a697b8d 10801 case bp_fast_tracepoint:
0fb4aa4b 10802 case bp_static_tracepoint:
ce78b96d 10803 case bp_catchpoint:
c5aa993b
JM
10804 case bp_hardware_breakpoint:
10805 case bp_watchpoint:
10806 case bp_hardware_watchpoint:
10807 case bp_read_watchpoint:
10808 case bp_access_watchpoint:
10809 enable_breakpoint (bpt);
10810 default:
10811 continue;
10812 }
0d381245
VP
10813 else if (strchr (args, '.'))
10814 {
10815 struct bp_location *loc = find_location_by_number (args);
10816 if (loc)
10817 loc->enabled = 1;
b60e7edf 10818 update_global_location_list (1);
0d381245 10819 }
c906108c 10820 else
95a42b64 10821 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
10822}
10823
10824static void
95a42b64 10825enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 10826{
b5de0fa7 10827 do_enable_breakpoint (bpt, disp_disable);
c906108c
SS
10828}
10829
c906108c 10830static void
fba45db2 10831enable_once_command (char *args, int from_tty)
c906108c 10832{
95a42b64 10833 map_breakpoint_numbers (args, enable_once_breakpoint, NULL);
c906108c
SS
10834}
10835
10836static void
95a42b64 10837enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 10838{
b5de0fa7 10839 do_enable_breakpoint (bpt, disp_del);
c906108c
SS
10840}
10841
c906108c 10842static void
fba45db2 10843enable_delete_command (char *args, int from_tty)
c906108c 10844{
95a42b64 10845 map_breakpoint_numbers (args, enable_delete_breakpoint, NULL);
c906108c
SS
10846}
10847\f
fa8d40ab
JJ
10848static void
10849set_breakpoint_cmd (char *args, int from_tty)
10850{
10851}
10852
10853static void
10854show_breakpoint_cmd (char *args, int from_tty)
10855{
10856}
10857
1f3b5d1b
PP
10858/* Invalidate last known value of any hardware watchpoint if
10859 the memory which that value represents has been written to by
10860 GDB itself. */
10861
10862static void
10863invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
10864 const bfd_byte *data)
10865{
10866 struct breakpoint *bp;
10867
10868 ALL_BREAKPOINTS (bp)
10869 if (bp->enable_state == bp_enabled
10870 && bp->type == bp_hardware_watchpoint
10871 && bp->val_valid && bp->val)
10872 {
10873 struct bp_location *loc;
10874
10875 for (loc = bp->loc; loc != NULL; loc = loc->next)
10876 if (loc->loc_type == bp_loc_hardware_watchpoint
10877 && loc->address + loc->length > addr
10878 && addr + len > loc->address)
10879 {
10880 value_free (bp->val);
10881 bp->val = NULL;
10882 bp->val_valid = 0;
10883 }
10884 }
10885}
10886
c906108c
SS
10887/* Use default_breakpoint_'s, or nothing if they aren't valid. */
10888
10889struct symtabs_and_lines
fba45db2 10890decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
10891{
10892 struct symtabs_and_lines sals;
cc59ec59 10893
c906108c 10894 if (string == 0)
8a3fe4f8 10895 error (_("Empty line specification."));
c906108c
SS
10896 if (default_breakpoint_valid)
10897 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
10898 default_breakpoint_symtab,
10899 default_breakpoint_line,
68219205 10900 (char ***) NULL, NULL);
c906108c
SS
10901 else
10902 sals = decode_line_1 (&string, funfirstline,
68219205 10903 (struct symtab *) NULL, 0, (char ***) NULL, NULL);
c906108c 10904 if (*string)
8a3fe4f8 10905 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
10906 return sals;
10907}
8181d85f
DJ
10908
10909/* Create and insert a raw software breakpoint at PC. Return an
10910 identifier, which should be used to remove the breakpoint later.
10911 In general, places which call this should be using something on the
10912 breakpoint chain instead; this function should be eliminated
10913 someday. */
10914
10915void *
6c95b8df
PA
10916deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
10917 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
10918{
10919 struct bp_target_info *bp_tgt;
10920
6c95b8df 10921 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 10922
6c95b8df 10923 bp_tgt->placed_address_space = aspace;
8181d85f 10924 bp_tgt->placed_address = pc;
6c95b8df 10925
a6d9a66e 10926 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
10927 {
10928 /* Could not insert the breakpoint. */
10929 xfree (bp_tgt);
10930 return NULL;
10931 }
10932
10933 return bp_tgt;
10934}
10935
10936/* Remove a breakpoint BP inserted by deprecated_insert_raw_breakpoint. */
10937
10938int
a6d9a66e 10939deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
10940{
10941 struct bp_target_info *bp_tgt = bp;
10942 int ret;
10943
a6d9a66e 10944 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
10945 xfree (bp_tgt);
10946
10947 return ret;
10948}
10949
10950/* One (or perhaps two) breakpoints used for software single stepping. */
10951
10952static void *single_step_breakpoints[2];
a6d9a66e 10953static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
10954
10955/* Create and insert a breakpoint for software single step. */
10956
10957void
6c95b8df
PA
10958insert_single_step_breakpoint (struct gdbarch *gdbarch,
10959 struct address_space *aspace, CORE_ADDR next_pc)
8181d85f
DJ
10960{
10961 void **bpt_p;
10962
10963 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
10964 {
10965 bpt_p = &single_step_breakpoints[0];
10966 single_step_gdbarch[0] = gdbarch;
10967 }
8181d85f
DJ
10968 else
10969 {
10970 gdb_assert (single_step_breakpoints[1] == NULL);
10971 bpt_p = &single_step_breakpoints[1];
a6d9a66e 10972 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
10973 }
10974
10975 /* NOTE drow/2006-04-11: A future improvement to this function would be
10976 to only create the breakpoints once, and actually put them on the
10977 breakpoint chain. That would let us use set_raw_breakpoint. We could
10978 adjust the addresses each time they were needed. Doing this requires
10979 corresponding changes elsewhere where single step breakpoints are
10980 handled, however. So, for now, we use this. */
10981
6c95b8df 10982 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 10983 if (*bpt_p == NULL)
5af949e3
UW
10984 error (_("Could not insert single-step breakpoint at %s"),
10985 paddress (gdbarch, next_pc));
8181d85f
DJ
10986}
10987
f02253f1
HZ
10988/* Check if the breakpoints used for software single stepping
10989 were inserted or not. */
10990
10991int
10992single_step_breakpoints_inserted (void)
10993{
10994 return (single_step_breakpoints[0] != NULL
10995 || single_step_breakpoints[1] != NULL);
10996}
10997
8181d85f
DJ
10998/* Remove and delete any breakpoints used for software single step. */
10999
11000void
11001remove_single_step_breakpoints (void)
11002{
11003 gdb_assert (single_step_breakpoints[0] != NULL);
11004
11005 /* See insert_single_step_breakpoint for more about this deprecated
11006 call. */
a6d9a66e
UW
11007 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
11008 single_step_breakpoints[0]);
11009 single_step_gdbarch[0] = NULL;
8181d85f
DJ
11010 single_step_breakpoints[0] = NULL;
11011
11012 if (single_step_breakpoints[1] != NULL)
11013 {
a6d9a66e
UW
11014 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
11015 single_step_breakpoints[1]);
11016 single_step_gdbarch[1] = NULL;
8181d85f
DJ
11017 single_step_breakpoints[1] = NULL;
11018 }
11019}
11020
d03285ec
UW
11021/* Delete software single step breakpoints without removing them from
11022 the inferior. This is intended to be used if the inferior's address
11023 space where they were inserted is already gone, e.g. after exit or
11024 exec. */
11025
11026void
11027cancel_single_step_breakpoints (void)
11028{
11029 int i;
11030
11031 for (i = 0; i < 2; i++)
11032 if (single_step_breakpoints[i])
11033 {
11034 xfree (single_step_breakpoints[i]);
11035 single_step_breakpoints[i] = NULL;
11036 single_step_gdbarch[i] = NULL;
11037 }
11038}
11039
11040/* Detach software single-step breakpoints from INFERIOR_PTID without
11041 removing them. */
11042
11043static void
11044detach_single_step_breakpoints (void)
11045{
11046 int i;
11047
11048 for (i = 0; i < 2; i++)
11049 if (single_step_breakpoints[i])
11050 target_remove_breakpoint (single_step_gdbarch[i],
11051 single_step_breakpoints[i]);
11052}
11053
1aafd4da
UW
11054/* Check whether a software single-step breakpoint is inserted at PC. */
11055
11056static int
cc59ec59
MS
11057single_step_breakpoint_inserted_here_p (struct address_space *aspace,
11058 CORE_ADDR pc)
1aafd4da
UW
11059{
11060 int i;
11061
11062 for (i = 0; i < 2; i++)
11063 {
11064 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
11065 if (bp_tgt
11066 && breakpoint_address_match (bp_tgt->placed_address_space,
11067 bp_tgt->placed_address,
11068 aspace, pc))
1aafd4da
UW
11069 return 1;
11070 }
11071
11072 return 0;
11073}
11074
a96d9b2e
SDJ
11075/* Returns 0 if 'bp' is NOT a syscall catchpoint,
11076 non-zero otherwise. */
11077static int
11078is_syscall_catchpoint_enabled (struct breakpoint *bp)
11079{
11080 if (syscall_catchpoint_p (bp)
11081 && bp->enable_state != bp_disabled
11082 && bp->enable_state != bp_call_disabled)
11083 return 1;
11084 else
11085 return 0;
11086}
11087
11088int
11089catch_syscall_enabled (void)
11090{
11091 struct inferior *inf = current_inferior ();
11092
11093 return inf->total_syscalls_count != 0;
11094}
11095
11096int
11097catching_syscall_number (int syscall_number)
11098{
11099 struct breakpoint *bp;
11100
11101 ALL_BREAKPOINTS (bp)
11102 if (is_syscall_catchpoint_enabled (bp))
11103 {
11104 if (bp->syscalls_to_be_caught)
11105 {
11106 int i, iter;
11107 for (i = 0;
11108 VEC_iterate (int, bp->syscalls_to_be_caught, i, iter);
11109 i++)
11110 if (syscall_number == iter)
11111 return 1;
11112 }
11113 else
11114 return 1;
11115 }
11116
11117 return 0;
11118}
11119
11120/* Complete syscall names. Used by "catch syscall". */
11121static char **
11122catch_syscall_completer (struct cmd_list_element *cmd,
11123 char *text, char *word)
11124{
11125 const char **list = get_syscall_names ();
cc59ec59 11126
a96d9b2e
SDJ
11127 return (list == NULL) ? NULL : complete_on_enum (list, text, word);
11128}
11129
1042e4c0
SS
11130/* Tracepoint-specific operations. */
11131
11132/* Set tracepoint count to NUM. */
11133static void
11134set_tracepoint_count (int num)
11135{
11136 tracepoint_count = num;
4fa62494 11137 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
11138}
11139
11140void
11141trace_command (char *arg, int from_tty)
11142{
8cdf0e15
VP
11143 if (create_breakpoint (get_current_arch (),
11144 arg,
11145 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
11146 0 /* tempflag */,
11147 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
11148 0 /* Ignore count */,
11149 pending_break_support,
11150 NULL,
11151 from_tty,
84f4c1fe
PM
11152 1 /* enabled */,
11153 0 /* internal */))
fd9b8c24 11154 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
11155}
11156
7a697b8d
SS
11157void
11158ftrace_command (char *arg, int from_tty)
11159{
8cdf0e15
VP
11160 if (create_breakpoint (get_current_arch (),
11161 arg,
11162 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
11163 0 /* tempflag */,
11164 bp_fast_tracepoint /* type_wanted */,
11165 0 /* Ignore count */,
11166 pending_break_support,
11167 NULL,
11168 from_tty,
84f4c1fe
PM
11169 1 /* enabled */,
11170 0 /* internal */))
0fb4aa4b
PA
11171 set_tracepoint_count (breakpoint_count);
11172}
11173
11174/* strace command implementation. Creates a static tracepoint. */
11175
11176void
11177strace_command (char *arg, int from_tty)
11178{
11179 if (create_breakpoint (get_current_arch (),
11180 arg,
11181 NULL, 0, 1 /* parse arg */,
11182 0 /* tempflag */,
11183 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
11184 0 /* Ignore count */,
11185 pending_break_support,
11186 NULL,
11187 from_tty,
84f4c1fe
PM
11188 1 /* enabled */,
11189 0 /* internal */))
fd9b8c24 11190 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
11191}
11192
409873ef
SS
11193/* Set up a fake reader function that gets command lines from a linked
11194 list that was acquired during tracepoint uploading. */
11195
11196static struct uploaded_tp *this_utp;
3149d8c1 11197static int next_cmd;
409873ef
SS
11198
11199static char *
11200read_uploaded_action (void)
11201{
11202 char *rslt;
11203
3149d8c1 11204 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 11205
3149d8c1 11206 next_cmd++;
409873ef
SS
11207
11208 return rslt;
11209}
11210
00bf0b85
SS
11211/* Given information about a tracepoint as recorded on a target (which
11212 can be either a live system or a trace file), attempt to create an
11213 equivalent GDB tracepoint. This is not a reliable process, since
11214 the target does not necessarily have all the information used when
11215 the tracepoint was originally defined. */
11216
11217struct breakpoint *
11218create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 11219{
409873ef 11220 char *addr_str, small_buf[100];
d5551862 11221 struct breakpoint *tp;
fd9b8c24 11222
409873ef
SS
11223 if (utp->at_string)
11224 addr_str = utp->at_string;
11225 else
11226 {
11227 /* In the absence of a source location, fall back to raw
11228 address. Since there is no way to confirm that the address
11229 means the same thing as when the trace was started, warn the
11230 user. */
11231 warning (_("Uploaded tracepoint %d has no source location, using raw address"),
11232 utp->number);
11233 sprintf (small_buf, "*%s", hex_string (utp->addr));
11234 addr_str = small_buf;
11235 }
11236
11237 /* There's not much we can do with a sequence of bytecodes. */
11238 if (utp->cond && !utp->cond_string)
11239 warning (_("Uploaded tracepoint %d condition has no source form, ignoring it"),
11240 utp->number);
d5551862 11241
8cdf0e15 11242 if (!create_breakpoint (get_current_arch (),
409873ef
SS
11243 addr_str,
11244 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 11245 0 /* tempflag */,
0fb4aa4b 11246 utp->type /* type_wanted */,
8cdf0e15
VP
11247 0 /* Ignore count */,
11248 pending_break_support,
11249 NULL,
11250 0 /* from_tty */,
84f4c1fe
PM
11251 utp->enabled /* enabled */,
11252 0 /* internal */))
fd9b8c24
PA
11253 return NULL;
11254
00bf0b85
SS
11255 set_tracepoint_count (breakpoint_count);
11256
409873ef 11257 /* Get the tracepoint we just created. */
fd9b8c24
PA
11258 tp = get_tracepoint (tracepoint_count);
11259 gdb_assert (tp != NULL);
d5551862 11260
00bf0b85
SS
11261 if (utp->pass > 0)
11262 {
409873ef 11263 sprintf (small_buf, "%d %d", utp->pass, tp->number);
00bf0b85 11264
409873ef 11265 trace_pass_command (small_buf, 0);
00bf0b85
SS
11266 }
11267
409873ef
SS
11268 /* If we have uploaded versions of the original commands, set up a
11269 special-purpose "reader" function and call the usual command line
11270 reader, then pass the result to the breakpoint command-setting
11271 function. */
3149d8c1 11272 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 11273 {
409873ef 11274 struct command_line *cmd_list;
00bf0b85 11275
409873ef 11276 this_utp = utp;
3149d8c1 11277 next_cmd = 0;
d5551862 11278
409873ef
SS
11279 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
11280
11281 breakpoint_set_commands (tp, cmd_list);
00bf0b85 11282 }
3149d8c1
SS
11283 else if (!VEC_empty (char_ptr, utp->actions)
11284 || !VEC_empty (char_ptr, utp->step_actions))
409873ef
SS
11285 warning (_("Uploaded tracepoint %d actions have no source form, ignoring them"),
11286 utp->number);
00bf0b85
SS
11287
11288 return tp;
11289 }
11290
1042e4c0
SS
11291/* Print information on tracepoint number TPNUM_EXP, or all if
11292 omitted. */
11293
11294static void
11295tracepoints_info (char *tpnum_exp, int from_tty)
11296{
d77f58be
SS
11297 int tpnum = -1, num_printed;
11298
11299 if (tpnum_exp)
11300 tpnum = parse_and_eval_long (tpnum_exp);
1042e4c0 11301
d77f58be
SS
11302 num_printed = breakpoint_1 (tpnum, 0, is_tracepoint);
11303
11304 if (num_printed == 0)
1042e4c0 11305 {
d77f58be
SS
11306 if (tpnum == -1)
11307 ui_out_message (uiout, 0, "No tracepoints.\n");
11308 else
11309 ui_out_message (uiout, 0, "No tracepoint number %d.\n", tpnum);
1042e4c0 11310 }
ad443146
SS
11311
11312 default_collect_info ();
1042e4c0
SS
11313}
11314
11315/* The 'enable trace' command enables tracepoints.
11316 Not supported by all targets. */
11317static void
11318enable_trace_command (char *args, int from_tty)
11319{
11320 enable_command (args, from_tty);
11321}
11322
11323/* The 'disable trace' command disables tracepoints.
11324 Not supported by all targets. */
11325static void
11326disable_trace_command (char *args, int from_tty)
11327{
11328 disable_command (args, from_tty);
11329}
11330
11331/* Remove a tracepoint (or all if no argument) */
11332static void
11333delete_trace_command (char *arg, int from_tty)
11334{
35df4500 11335 struct breakpoint *b, *b_tmp;
1042e4c0
SS
11336
11337 dont_repeat ();
11338
11339 if (arg == 0)
11340 {
11341 int breaks_to_delete = 0;
11342
11343 /* Delete all breakpoints if no argument.
11344 Do not delete internal or call-dummy breakpoints, these
11345 have to be deleted with an explicit breakpoint number argument. */
11346 ALL_TRACEPOINTS (b)
11347 {
11348 if (b->number >= 0)
11349 {
11350 breaks_to_delete = 1;
11351 break;
11352 }
11353 }
11354
11355 /* Ask user only if there are some breakpoints to delete. */
11356 if (!from_tty
11357 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
11358 {
35df4500 11359 ALL_BREAKPOINTS_SAFE (b, b_tmp)
1042e4c0 11360 {
d77f58be 11361 if (is_tracepoint (b)
059fb39f 11362 && b->number >= 0)
1042e4c0
SS
11363 delete_breakpoint (b);
11364 }
11365 }
11366 }
11367 else
95a42b64 11368 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
1042e4c0
SS
11369}
11370
11371/* Set passcount for tracepoint.
11372
11373 First command argument is passcount, second is tracepoint number.
11374 If tracepoint number omitted, apply to most recently defined.
11375 Also accepts special argument "all". */
11376
11377static void
11378trace_pass_command (char *args, int from_tty)
11379{
11380 struct breakpoint *t1 = (struct breakpoint *) -1, *t2;
11381 unsigned int count;
11382 int all = 0;
11383
11384 if (args == 0 || *args == 0)
11385 error (_("passcount command requires an argument (count + optional TP num)"));
11386
11387 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
11388
11389 while (*args && isspace ((int) *args))
11390 args++;
11391
11392 if (*args && strncasecmp (args, "all", 3) == 0)
11393 {
11394 args += 3; /* Skip special argument "all". */
11395 all = 1;
11396 if (*args)
11397 error (_("Junk at end of arguments."));
11398 }
11399 else
11400 t1 = get_tracepoint_by_number (&args, 1, 1);
11401
11402 do
11403 {
11404 if (t1)
11405 {
11406 ALL_TRACEPOINTS (t2)
11407 if (t1 == (struct breakpoint *) -1 || t1 == t2)
11408 {
11409 t2->pass_count = count;
11410 observer_notify_tracepoint_modified (t2->number);
11411 if (from_tty)
11412 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
11413 t2->number, count);
11414 }
11415 if (! all && *args)
11416 t1 = get_tracepoint_by_number (&args, 1, 0);
11417 }
11418 }
11419 while (*args);
11420}
11421
11422struct breakpoint *
11423get_tracepoint (int num)
11424{
11425 struct breakpoint *t;
11426
11427 ALL_TRACEPOINTS (t)
11428 if (t->number == num)
11429 return t;
11430
11431 return NULL;
11432}
11433
d5551862
SS
11434/* Find the tracepoint with the given target-side number (which may be
11435 different from the tracepoint number after disconnecting and
11436 reconnecting). */
11437
11438struct breakpoint *
11439get_tracepoint_by_number_on_target (int num)
11440{
11441 struct breakpoint *t;
11442
11443 ALL_TRACEPOINTS (t)
11444 if (t->number_on_target == num)
11445 return t;
11446
11447 return NULL;
11448}
11449
1042e4c0
SS
11450/* Utility: parse a tracepoint number and look it up in the list.
11451 If MULTI_P is true, there might be a range of tracepoints in ARG.
11452 if OPTIONAL_P is true, then if the argument is missing, the most
11453 recent tracepoint (tracepoint_count) is returned. */
11454struct breakpoint *
11455get_tracepoint_by_number (char **arg, int multi_p, int optional_p)
11456{
11457 extern int tracepoint_count;
11458 struct breakpoint *t;
11459 int tpnum;
11460 char *instring = arg == NULL ? NULL : *arg;
11461
11462 if (arg == NULL || *arg == NULL || ! **arg)
11463 {
11464 if (optional_p)
11465 tpnum = tracepoint_count;
11466 else
11467 error_no_arg (_("tracepoint number"));
11468 }
11469 else
11470 tpnum = multi_p ? get_number_or_range (arg) : get_number (arg);
11471
11472 if (tpnum <= 0)
11473 {
11474 if (instring && *instring)
11475 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
11476 instring);
11477 else
11478 printf_filtered (_("Tracepoint argument missing and no previous tracepoint\n"));
11479 return NULL;
11480 }
11481
11482 ALL_TRACEPOINTS (t)
11483 if (t->number == tpnum)
11484 {
11485 return t;
11486 }
11487
11488 /* FIXME: if we are in the middle of a range we don't want to give
11489 a message. The current interface to get_number_or_range doesn't
11490 allow us to discover this. */
11491 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
11492 return NULL;
11493}
11494
6149aea9
PA
11495/* Save information on user settable breakpoints (watchpoints, etc) to
11496 a new script file named FILENAME. If FILTER is non-NULL, call it
11497 on each breakpoint and only include the ones for which it returns
11498 non-zero. */
11499
1042e4c0 11500static void
6149aea9
PA
11501save_breakpoints (char *filename, int from_tty,
11502 int (*filter) (const struct breakpoint *))
1042e4c0
SS
11503{
11504 struct breakpoint *tp;
6149aea9 11505 int any = 0;
a7bdde9e 11506 char *pathname;
1042e4c0 11507 struct cleanup *cleanup;
a7bdde9e 11508 struct ui_file *fp;
6149aea9 11509 int extra_trace_bits = 0;
1042e4c0 11510
6149aea9
PA
11511 if (filename == 0 || *filename == 0)
11512 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
11513
11514 /* See if we have anything to save. */
6149aea9 11515 ALL_BREAKPOINTS (tp)
1042e4c0 11516 {
6149aea9 11517 /* Skip internal and momentary breakpoints. */
84f4c1fe 11518 if (!user_settable_breakpoint (tp) || tp->number < 0)
6149aea9
PA
11519 continue;
11520
11521 /* If we have a filter, only save the breakpoints it accepts. */
11522 if (filter && !filter (tp))
11523 continue;
11524
11525 any = 1;
11526
11527 if (is_tracepoint (tp))
11528 {
11529 extra_trace_bits = 1;
11530
11531 /* We can stop searching. */
11532 break;
11533 }
1042e4c0 11534 }
6149aea9
PA
11535
11536 if (!any)
1042e4c0 11537 {
6149aea9 11538 warning (_("Nothing to save."));
1042e4c0
SS
11539 return;
11540 }
11541
6149aea9 11542 pathname = tilde_expand (filename);
1042e4c0 11543 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 11544 fp = gdb_fopen (pathname, "w");
059fb39f 11545 if (!fp)
6149aea9
PA
11546 error (_("Unable to open file '%s' for saving (%s)"),
11547 filename, safe_strerror (errno));
a7bdde9e 11548 make_cleanup_ui_file_delete (fp);
8bf6485c 11549
6149aea9
PA
11550 if (extra_trace_bits)
11551 save_trace_state_variables (fp);
8bf6485c 11552
6149aea9 11553 ALL_BREAKPOINTS (tp)
1042e4c0 11554 {
6149aea9 11555 /* Skip internal and momentary breakpoints. */
84f4c1fe 11556 if (!user_settable_breakpoint (tp) || tp->number < 0)
6149aea9 11557 continue;
8bf6485c 11558
6149aea9
PA
11559 /* If we have a filter, only save the breakpoints it accepts. */
11560 if (filter && !filter (tp))
11561 continue;
11562
11563 if (tp->ops != NULL)
11564 (tp->ops->print_recreate) (tp, fp);
1042e4c0
SS
11565 else
11566 {
6149aea9
PA
11567 if (tp->type == bp_fast_tracepoint)
11568 fprintf_unfiltered (fp, "ftrace");
0fb4aa4b
PA
11569 if (tp->type == bp_static_tracepoint)
11570 fprintf_unfiltered (fp, "strace");
6149aea9
PA
11571 else if (tp->type == bp_tracepoint)
11572 fprintf_unfiltered (fp, "trace");
11573 else if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11574 fprintf_unfiltered (fp, "tbreak");
11575 else if (tp->type == bp_breakpoint)
11576 fprintf_unfiltered (fp, "break");
11577 else if (tp->type == bp_hardware_breakpoint
11578 && tp->disposition == disp_del)
11579 fprintf_unfiltered (fp, "thbreak");
11580 else if (tp->type == bp_hardware_breakpoint)
11581 fprintf_unfiltered (fp, "hbreak");
11582 else if (tp->type == bp_watchpoint)
11583 fprintf_unfiltered (fp, "watch");
11584 else if (tp->type == bp_hardware_watchpoint)
11585 fprintf_unfiltered (fp, "watch");
11586 else if (tp->type == bp_read_watchpoint)
11587 fprintf_unfiltered (fp, "rwatch");
11588 else if (tp->type == bp_access_watchpoint)
11589 fprintf_unfiltered (fp, "awatch");
11590 else
11591 internal_error (__FILE__, __LINE__,
11592 _("unhandled breakpoint type %d"), (int) tp->type);
11593
11594 if (tp->exp_string)
11595 fprintf_unfiltered (fp, " %s", tp->exp_string);
11596 else if (tp->addr_string)
11597 fprintf_unfiltered (fp, " %s", tp->addr_string);
11598 else
11599 {
11600 char tmp[40];
11601
11602 sprintf_vma (tmp, tp->loc->address);
11603 fprintf_unfiltered (fp, " *0x%s", tmp);
11604 }
1042e4c0
SS
11605 }
11606
6149aea9
PA
11607 if (tp->thread != -1)
11608 fprintf_unfiltered (fp, " thread %d", tp->thread);
11609
11610 if (tp->task != 0)
11611 fprintf_unfiltered (fp, " task %d", tp->task);
8bf6485c
SS
11612
11613 fprintf_unfiltered (fp, "\n");
11614
6149aea9
PA
11615 /* Note, we can't rely on tp->number for anything, as we can't
11616 assume the recreated breakpoint numbers will match. Use $bpnum
11617 instead. */
11618
11619 if (tp->cond_string)
11620 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
11621
11622 if (tp->ignore_count)
11623 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
11624
1042e4c0 11625 if (tp->pass_count)
a7bdde9e 11626 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
1042e4c0 11627
a7bdde9e 11628 if (tp->commands)
1042e4c0 11629 {
a7bdde9e
VP
11630 volatile struct gdb_exception ex;
11631
6149aea9 11632 fprintf_unfiltered (fp, " commands\n");
a7bdde9e
VP
11633
11634 ui_out_redirect (uiout, fp);
14dba4b4 11635 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 11636 {
9add0f1b 11637 print_command_lines (uiout, tp->commands->commands, 2);
a7bdde9e
VP
11638 }
11639 ui_out_redirect (uiout, NULL);
1042e4c0 11640
a7bdde9e
VP
11641 if (ex.reason < 0)
11642 throw_exception (ex);
1042e4c0 11643
a7bdde9e 11644 fprintf_unfiltered (fp, " end\n");
1042e4c0 11645 }
6149aea9
PA
11646
11647 if (tp->enable_state == bp_disabled)
11648 fprintf_unfiltered (fp, "disable\n");
11649
11650 /* If this is a multi-location breakpoint, check if the locations
11651 should be individually disabled. Watchpoint locations are
11652 special, and not user visible. */
11653 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
11654 {
11655 struct bp_location *loc;
11656 int n = 1;
11657
11658 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
11659 if (!loc->enabled)
11660 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
11661 }
1042e4c0 11662 }
8bf6485c 11663
6149aea9 11664 if (extra_trace_bits && *default_collect)
8bf6485c
SS
11665 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
11666
1042e4c0
SS
11667 do_cleanups (cleanup);
11668 if (from_tty)
6149aea9
PA
11669 printf_filtered (_("Saved to file '%s'.\n"), filename);
11670}
11671
11672/* The `save breakpoints' command. */
11673
11674static void
11675save_breakpoints_command (char *args, int from_tty)
11676{
11677 save_breakpoints (args, from_tty, NULL);
11678}
11679
11680/* The `save tracepoints' command. */
11681
11682static void
11683save_tracepoints_command (char *args, int from_tty)
11684{
11685 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
11686}
11687
11688/* Create a vector of all tracepoints. */
11689
11690VEC(breakpoint_p) *
11691all_tracepoints ()
11692{
11693 VEC(breakpoint_p) *tp_vec = 0;
11694 struct breakpoint *tp;
11695
11696 ALL_TRACEPOINTS (tp)
11697 {
11698 VEC_safe_push (breakpoint_p, tp_vec, tp);
11699 }
11700
11701 return tp_vec;
11702}
11703
c906108c 11704\f
31e2b00f
AS
11705/* This help string is used for the break, hbreak, tbreak and thbreak commands.
11706 It is defined as a macro to prevent duplication.
11707 COMMAND should be a string constant containing the name of the command. */
11708#define BREAK_ARGS_HELP(command) \
11709command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
11710LOCATION may be a line number, function name, or \"*\" and an address.\n\
11711If a line number is specified, break at start of code for that line.\n\
11712If a function is specified, break at start of code for that function.\n\
11713If an address is specified, break at that exact address.\n\
dc10affe
PA
11714With no LOCATION, uses current execution address of the selected\n\
11715stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
11716\n\
11717THREADNUM is the number from \"info threads\".\n\
11718CONDITION is a boolean expression.\n\
11719\n\
d41c0fc8
PA
11720Multiple breakpoints at one place are permitted, and useful if their\n\
11721conditions are different.\n\
31e2b00f
AS
11722\n\
11723Do \"help breakpoints\" for info on other commands dealing with breakpoints."
11724
44feb3ce
TT
11725/* List of subcommands for "catch". */
11726static struct cmd_list_element *catch_cmdlist;
11727
11728/* List of subcommands for "tcatch". */
11729static struct cmd_list_element *tcatch_cmdlist;
11730
11731/* Like add_cmd, but add the command to both the "catch" and "tcatch"
11732 lists, and pass some additional user data to the command function. */
11733static void
11734add_catch_command (char *name, char *docstring,
11735 void (*sfunc) (char *args, int from_tty,
11736 struct cmd_list_element *command),
a96d9b2e
SDJ
11737 char **(*completer) (struct cmd_list_element *cmd,
11738 char *text, char *word),
44feb3ce
TT
11739 void *user_data_catch,
11740 void *user_data_tcatch)
11741{
11742 struct cmd_list_element *command;
11743
11744 command = add_cmd (name, class_breakpoint, NULL, docstring,
11745 &catch_cmdlist);
11746 set_cmd_sfunc (command, sfunc);
11747 set_cmd_context (command, user_data_catch);
a96d9b2e 11748 set_cmd_completer (command, completer);
44feb3ce
TT
11749
11750 command = add_cmd (name, class_breakpoint, NULL, docstring,
11751 &tcatch_cmdlist);
11752 set_cmd_sfunc (command, sfunc);
11753 set_cmd_context (command, user_data_tcatch);
a96d9b2e 11754 set_cmd_completer (command, completer);
44feb3ce
TT
11755}
11756
6c95b8df 11757static void
a79b8f6e 11758clear_syscall_counts (struct inferior *inf)
6c95b8df 11759{
6c95b8df
PA
11760 inf->total_syscalls_count = 0;
11761 inf->any_syscall_count = 0;
11762 VEC_free (int, inf->syscalls_counts);
11763}
11764
6149aea9
PA
11765static void
11766save_command (char *arg, int from_tty)
11767{
11768 printf_unfiltered (_("\
11769\"save\" must be followed by the name of a save subcommand.\n"));
11770 help_list (save_cmdlist, "save ", -1, gdb_stdout);
11771}
11772
84f4c1fe
PM
11773struct breakpoint *
11774iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
11775 void *data)
11776{
35df4500 11777 struct breakpoint *b, *b_tmp;
84f4c1fe 11778
35df4500 11779 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
11780 {
11781 if ((*callback) (b, data))
11782 return b;
11783 }
11784
11785 return NULL;
11786}
11787
c906108c 11788void
fba45db2 11789_initialize_breakpoint (void)
c906108c
SS
11790{
11791 struct cmd_list_element *c;
11792
84acb35a 11793 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 11794 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 11795 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 11796
c906108c
SS
11797 breakpoint_chain = 0;
11798 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
11799 before a breakpoint is set. */
11800 breakpoint_count = 0;
11801
1042e4c0
SS
11802 tracepoint_count = 0;
11803
1bedd215
AC
11804 add_com ("ignore", class_breakpoint, ignore_command, _("\
11805Set ignore-count of breakpoint number N to COUNT.\n\
11806Usage is `ignore N COUNT'."));
c906108c 11807 if (xdb_commands)
c5aa993b 11808 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 11809
1bedd215
AC
11810 add_com ("commands", class_breakpoint, commands_command, _("\
11811Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
11812Give breakpoint number as argument after \"commands\".\n\
11813With no argument, the targeted breakpoint is the last one set.\n\
11814The commands themselves follow starting on the next line.\n\
11815Type a line containing \"end\" to indicate the end of them.\n\
11816Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 11817then no output is printed when it is hit, except what the commands print."));
c906108c 11818
1bedd215
AC
11819 add_com ("condition", class_breakpoint, condition_command, _("\
11820Specify breakpoint number N to break only if COND is true.\n\
c906108c 11821Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 11822expression to be evaluated whenever breakpoint N is reached."));
c906108c 11823
1bedd215 11824 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 11825Set a temporary breakpoint.\n\
c906108c
SS
11826Like \"break\" except the breakpoint is only temporary,\n\
11827so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
11828by using \"enable delete\" on the breakpoint number.\n\
11829\n"
11830BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 11831 set_cmd_completer (c, location_completer);
c94fdfd0 11832
1bedd215 11833 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
31e2b00f 11834Set a hardware assisted breakpoint.\n\
c906108c 11835Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
11836some target hardware may not have this support.\n\
11837\n"
11838BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 11839 set_cmd_completer (c, location_completer);
c906108c 11840
1bedd215 11841 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 11842Set a temporary hardware assisted breakpoint.\n\
c906108c 11843Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
11844so it will be deleted when hit.\n\
11845\n"
11846BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 11847 set_cmd_completer (c, location_completer);
c906108c 11848
1bedd215
AC
11849 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
11850Enable some breakpoints.\n\
c906108c
SS
11851Give breakpoint numbers (separated by spaces) as arguments.\n\
11852With no subcommand, breakpoints are enabled until you command otherwise.\n\
11853This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 11854With a subcommand you can enable temporarily."),
c906108c
SS
11855 &enablelist, "enable ", 1, &cmdlist);
11856 if (xdb_commands)
1bedd215
AC
11857 add_com ("ab", class_breakpoint, enable_command, _("\
11858Enable some breakpoints.\n\
c906108c
SS
11859Give breakpoint numbers (separated by spaces) as arguments.\n\
11860With no subcommand, breakpoints are enabled until you command otherwise.\n\
11861This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 11862With a subcommand you can enable temporarily."));
c906108c
SS
11863
11864 add_com_alias ("en", "enable", class_breakpoint, 1);
11865
84951ab5 11866 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 11867Enable some breakpoints.\n\
c906108c
SS
11868Give breakpoint numbers (separated by spaces) as arguments.\n\
11869This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 11870May be abbreviated to simply \"enable\".\n"),
c5aa993b 11871 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 11872
1a966eab
AC
11873 add_cmd ("once", no_class, enable_once_command, _("\
11874Enable breakpoints for one hit. Give breakpoint numbers.\n\
11875If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
11876 &enablebreaklist);
11877
1a966eab
AC
11878 add_cmd ("delete", no_class, enable_delete_command, _("\
11879Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
11880If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
11881 &enablebreaklist);
11882
1a966eab
AC
11883 add_cmd ("delete", no_class, enable_delete_command, _("\
11884Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
11885If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
11886 &enablelist);
11887
1a966eab
AC
11888 add_cmd ("once", no_class, enable_once_command, _("\
11889Enable breakpoints for one hit. Give breakpoint numbers.\n\
11890If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
11891 &enablelist);
11892
1bedd215
AC
11893 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
11894Disable some breakpoints.\n\
c906108c
SS
11895Arguments are breakpoint numbers with spaces in between.\n\
11896To disable all breakpoints, give no argument.\n\
1bedd215 11897A disabled breakpoint is not forgotten, but has no effect until reenabled."),
c906108c
SS
11898 &disablelist, "disable ", 1, &cmdlist);
11899 add_com_alias ("dis", "disable", class_breakpoint, 1);
11900 add_com_alias ("disa", "disable", class_breakpoint, 1);
11901 if (xdb_commands)
1bedd215
AC
11902 add_com ("sb", class_breakpoint, disable_command, _("\
11903Disable some breakpoints.\n\
c906108c
SS
11904Arguments are breakpoint numbers with spaces in between.\n\
11905To disable all breakpoints, give no argument.\n\
1bedd215 11906A disabled breakpoint is not forgotten, but has no effect until reenabled."));
c906108c 11907
1a966eab
AC
11908 add_cmd ("breakpoints", class_alias, disable_command, _("\
11909Disable some breakpoints.\n\
c906108c
SS
11910Arguments are breakpoint numbers with spaces in between.\n\
11911To disable all breakpoints, give no argument.\n\
11912A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1a966eab 11913This command may be abbreviated \"disable\"."),
c906108c
SS
11914 &disablelist);
11915
1bedd215
AC
11916 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
11917Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
11918Arguments are breakpoint numbers with spaces in between.\n\
11919To delete all breakpoints, give no argument.\n\
11920\n\
11921Also a prefix command for deletion of other GDB objects.\n\
1bedd215 11922The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
11923 &deletelist, "delete ", 1, &cmdlist);
11924 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 11925 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 11926 if (xdb_commands)
1bedd215
AC
11927 add_com ("db", class_breakpoint, delete_command, _("\
11928Delete some breakpoints.\n\
c906108c 11929Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 11930To delete all breakpoints, give no argument.\n"));
c906108c 11931
1a966eab
AC
11932 add_cmd ("breakpoints", class_alias, delete_command, _("\
11933Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
11934Arguments are breakpoint numbers with spaces in between.\n\
11935To delete all breakpoints, give no argument.\n\
1a966eab 11936This command may be abbreviated \"delete\"."),
c906108c
SS
11937 &deletelist);
11938
1bedd215
AC
11939 add_com ("clear", class_breakpoint, clear_command, _("\
11940Clear breakpoint at specified line or function.\n\
c906108c
SS
11941Argument may be line number, function name, or \"*\" and an address.\n\
11942If line number is specified, all breakpoints in that line are cleared.\n\
11943If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
11944If an address is specified, breakpoints at that address are cleared.\n\
11945\n\
11946With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
11947is executing in.\n\
11948\n\
1bedd215 11949See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 11950 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 11951
1bedd215 11952 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
11953Set breakpoint at specified line or function.\n"
11954BREAK_ARGS_HELP ("break")));
5ba2abeb 11955 set_cmd_completer (c, location_completer);
c94fdfd0 11956
c906108c
SS
11957 add_com_alias ("b", "break", class_run, 1);
11958 add_com_alias ("br", "break", class_run, 1);
11959 add_com_alias ("bre", "break", class_run, 1);
11960 add_com_alias ("brea", "break", class_run, 1);
11961
7681d515
PM
11962 if (xdb_commands)
11963 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
11964
11965 if (dbx_commands)
11966 {
1bedd215
AC
11967 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
11968Break in function/address or break at a line in the current file."),
c5aa993b
JM
11969 &stoplist, "stop ", 1, &cmdlist);
11970 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 11971 _("Break in function or address."), &stoplist);
c5aa993b 11972 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 11973 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
11974 add_com ("status", class_info, breakpoints_info, _("\
11975Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
11976The \"Type\" column indicates one of:\n\
11977\tbreakpoint - normal breakpoint\n\
11978\twatchpoint - watchpoint\n\
11979The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
11980the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
11981breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
11982address and file/line number respectively.\n\
11983\n\
11984Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
11985are set to the address of the last breakpoint listed unless the command\n\
11986is prefixed with \"server \".\n\n\
c906108c 11987Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 11988breakpoint set."));
c906108c
SS
11989 }
11990
1bedd215
AC
11991 add_info ("breakpoints", breakpoints_info, _("\
11992Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
11993The \"Type\" column indicates one of:\n\
11994\tbreakpoint - normal breakpoint\n\
11995\twatchpoint - watchpoint\n\
11996The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
11997the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
11998breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
11999address and file/line number respectively.\n\
12000\n\
12001Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12002are set to the address of the last breakpoint listed unless the command\n\
12003is prefixed with \"server \".\n\n\
c906108c 12004Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12005breakpoint set."));
c906108c 12006
6b04bdb7
MS
12007 add_info_alias ("b", "breakpoints", 1);
12008
c906108c 12009 if (xdb_commands)
1bedd215
AC
12010 add_com ("lb", class_breakpoint, breakpoints_info, _("\
12011Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12012The \"Type\" column indicates one of:\n\
12013\tbreakpoint - normal breakpoint\n\
12014\twatchpoint - watchpoint\n\
12015The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12016the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12017breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12018address and file/line number respectively.\n\
12019\n\
12020Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12021are set to the address of the last breakpoint listed unless the command\n\
12022is prefixed with \"server \".\n\n\
c906108c 12023Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12024breakpoint set."));
c906108c 12025
1a966eab
AC
12026 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
12027Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12028The \"Type\" column indicates one of:\n\
12029\tbreakpoint - normal breakpoint\n\
12030\twatchpoint - watchpoint\n\
12031\tlongjmp - internal breakpoint used to step through longjmp()\n\
12032\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
12033\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
12034\tfinish - internal breakpoint used by the \"finish\" command\n\
12035The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
12036the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12037breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
12038address and file/line number respectively.\n\
12039\n\
12040Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12041are set to the address of the last breakpoint listed unless the command\n\
12042is prefixed with \"server \".\n\n\
c906108c 12043Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 12044breakpoint set."),
c906108c
SS
12045 &maintenanceinfolist);
12046
44feb3ce
TT
12047 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
12048Set catchpoints to catch events."),
12049 &catch_cmdlist, "catch ",
12050 0/*allow-unknown*/, &cmdlist);
12051
12052 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
12053Set temporary catchpoints to catch events."),
12054 &tcatch_cmdlist, "tcatch ",
12055 0/*allow-unknown*/, &cmdlist);
12056
12057 /* Add catch and tcatch sub-commands. */
12058 add_catch_command ("catch", _("\
12059Catch an exception, when caught.\n\
12060With an argument, catch only exceptions with the given name."),
12061 catch_catch_command,
a96d9b2e 12062 NULL,
44feb3ce
TT
12063 CATCH_PERMANENT,
12064 CATCH_TEMPORARY);
12065 add_catch_command ("throw", _("\
12066Catch an exception, when thrown.\n\
12067With an argument, catch only exceptions with the given name."),
12068 catch_throw_command,
a96d9b2e 12069 NULL,
44feb3ce
TT
12070 CATCH_PERMANENT,
12071 CATCH_TEMPORARY);
12072 add_catch_command ("fork", _("Catch calls to fork."),
12073 catch_fork_command_1,
a96d9b2e 12074 NULL,
44feb3ce
TT
12075 (void *) (uintptr_t) catch_fork_permanent,
12076 (void *) (uintptr_t) catch_fork_temporary);
12077 add_catch_command ("vfork", _("Catch calls to vfork."),
12078 catch_fork_command_1,
a96d9b2e 12079 NULL,
44feb3ce
TT
12080 (void *) (uintptr_t) catch_vfork_permanent,
12081 (void *) (uintptr_t) catch_vfork_temporary);
12082 add_catch_command ("exec", _("Catch calls to exec."),
12083 catch_exec_command_1,
a96d9b2e
SDJ
12084 NULL,
12085 CATCH_PERMANENT,
12086 CATCH_TEMPORARY);
12087 add_catch_command ("syscall", _("\
12088Catch system calls by their names and/or numbers.\n\
12089Arguments say which system calls to catch. If no arguments\n\
12090are given, every system call will be caught.\n\
12091Arguments, if given, should be one or more system call names\n\
12092(if your system supports that), or system call numbers."),
12093 catch_syscall_command_1,
12094 catch_syscall_completer,
44feb3ce
TT
12095 CATCH_PERMANENT,
12096 CATCH_TEMPORARY);
44feb3ce
TT
12097 add_catch_command ("exception", _("\
12098Catch Ada exceptions, when raised.\n\
12099With an argument, catch only exceptions with the given name."),
12100 catch_ada_exception_command,
a96d9b2e 12101 NULL,
44feb3ce
TT
12102 CATCH_PERMANENT,
12103 CATCH_TEMPORARY);
12104 add_catch_command ("assert", _("\
12105Catch failed Ada assertions, when raised.\n\
12106With an argument, catch only exceptions with the given name."),
12107 catch_assert_command,
a96d9b2e 12108 NULL,
44feb3ce
TT
12109 CATCH_PERMANENT,
12110 CATCH_TEMPORARY);
c5aa993b 12111
1bedd215
AC
12112 c = add_com ("watch", class_breakpoint, watch_command, _("\
12113Set a watchpoint for an expression.\n\
06a64a0b 12114Usage: watch [-l|-location] EXPRESSION\n\
c906108c 12115A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12116an expression changes.\n\
12117If -l or -location is given, this evaluates EXPRESSION and watches\n\
12118the memory to which it refers."));
65d12d83 12119 set_cmd_completer (c, expression_completer);
c906108c 12120
1bedd215
AC
12121 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
12122Set a read watchpoint for an expression.\n\
06a64a0b 12123Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 12124A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12125an expression is read.\n\
12126If -l or -location is given, this evaluates EXPRESSION and watches\n\
12127the memory to which it refers."));
65d12d83 12128 set_cmd_completer (c, expression_completer);
c906108c 12129
1bedd215
AC
12130 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
12131Set a watchpoint for an expression.\n\
06a64a0b 12132Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 12133A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12134an expression is either read or written.\n\
12135If -l or -location is given, this evaluates EXPRESSION and watches\n\
12136the memory to which it refers."));
65d12d83 12137 set_cmd_completer (c, expression_completer);
c906108c 12138
d77f58be
SS
12139 add_info ("watchpoints", watchpoints_info, _("\
12140Status of watchpoints, or watchpoint number NUMBER."));
12141
c906108c
SS
12142
12143
920d2a44
AC
12144 /* XXX: cagney/2005-02-23: This should be a boolean, and should
12145 respond to changes - contrary to the description. */
85c07804
AC
12146 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
12147 &can_use_hw_watchpoints, _("\
12148Set debugger's willingness to use watchpoint hardware."), _("\
12149Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
12150If zero, gdb will not use hardware for new watchpoints, even if\n\
12151such is available. (However, any hardware watchpoints that were\n\
12152created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
12153hardware.)"),
12154 NULL,
920d2a44 12155 show_can_use_hw_watchpoints,
85c07804 12156 &setlist, &showlist);
c906108c
SS
12157
12158 can_use_hw_watchpoints = 1;
fa8d40ab 12159
1042e4c0
SS
12160 /* Tracepoint manipulation commands. */
12161
12162 c = add_com ("trace", class_breakpoint, trace_command, _("\
12163Set a tracepoint at specified line or function.\n\
12164\n"
12165BREAK_ARGS_HELP ("trace") "\n\
12166Do \"help tracepoints\" for info on other tracepoint commands."));
12167 set_cmd_completer (c, location_completer);
12168
12169 add_com_alias ("tp", "trace", class_alias, 0);
12170 add_com_alias ("tr", "trace", class_alias, 1);
12171 add_com_alias ("tra", "trace", class_alias, 1);
12172 add_com_alias ("trac", "trace", class_alias, 1);
12173
7a697b8d
SS
12174 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
12175Set a fast tracepoint at specified line or function.\n\
12176\n"
12177BREAK_ARGS_HELP ("ftrace") "\n\
12178Do \"help tracepoints\" for info on other tracepoint commands."));
12179 set_cmd_completer (c, location_completer);
12180
0fb4aa4b
PA
12181 c = add_com ("strace", class_breakpoint, strace_command, _("\
12182Set a static tracepoint at specified line, function or marker.\n\
12183\n\
12184strace [LOCATION] [if CONDITION]\n\
12185LOCATION may be a line number, function name, \"*\" and an address,\n\
12186or -m MARKER_ID.\n\
12187If a line number is specified, probe the marker at start of code\n\
12188for that line. If a function is specified, probe the marker at start\n\
12189of code for that function. If an address is specified, probe the marker\n\
12190at that exact address. If a marker id is specified, probe the marker\n\
12191with that name. With no LOCATION, uses current execution address of\n\
12192the selected stack frame.\n\
12193Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
12194This collects arbitrary user data passed in the probe point call to the\n\
12195tracing library. You can inspect it when analyzing the trace buffer,\n\
12196by printing the $_sdata variable like any other convenience variable.\n\
12197\n\
12198CONDITION is a boolean expression.\n\
12199\n\
d41c0fc8
PA
12200Multiple tracepoints at one place are permitted, and useful if their\n\
12201conditions are different.\n\
0fb4aa4b
PA
12202\n\
12203Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
12204Do \"help tracepoints\" for info on other tracepoint commands."));
12205 set_cmd_completer (c, location_completer);
12206
1042e4c0
SS
12207 add_info ("tracepoints", tracepoints_info, _("\
12208Status of tracepoints, or tracepoint number NUMBER.\n\
12209Convenience variable \"$tpnum\" contains the number of the\n\
12210last tracepoint set."));
12211
12212 add_info_alias ("tp", "tracepoints", 1);
12213
12214 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
12215Delete specified tracepoints.\n\
12216Arguments are tracepoint numbers, separated by spaces.\n\
12217No argument means delete all tracepoints."),
12218 &deletelist);
12219
12220 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
12221Disable specified tracepoints.\n\
12222Arguments are tracepoint numbers, separated by spaces.\n\
12223No argument means disable all tracepoints."),
12224 &disablelist);
12225 deprecate_cmd (c, "disable");
12226
12227 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
12228Enable specified tracepoints.\n\
12229Arguments are tracepoint numbers, separated by spaces.\n\
12230No argument means enable all tracepoints."),
12231 &enablelist);
12232 deprecate_cmd (c, "enable");
12233
12234 add_com ("passcount", class_trace, trace_pass_command, _("\
12235Set the passcount for a tracepoint.\n\
12236The trace will end when the tracepoint has been passed 'count' times.\n\
12237Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
12238if TPNUM is omitted, passcount refers to the last tracepoint defined."));
12239
6149aea9
PA
12240 add_prefix_cmd ("save", class_breakpoint, save_command,
12241 _("Save breakpoint definitions as a script."),
12242 &save_cmdlist, "save ",
12243 0/*allow-unknown*/, &cmdlist);
12244
12245 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
12246Save current breakpoint definitions as a script.\n\
cce7e648 12247This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
12248catchpoints, tracepoints). Use the 'source' command in another debug\n\
12249session to restore them."),
12250 &save_cmdlist);
12251 set_cmd_completer (c, filename_completer);
12252
12253 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 12254Save current tracepoint definitions as a script.\n\
6149aea9
PA
12255Use the 'source' command in another debug session to restore them."),
12256 &save_cmdlist);
1042e4c0
SS
12257 set_cmd_completer (c, filename_completer);
12258
6149aea9
PA
12259 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
12260 deprecate_cmd (c, "save tracepoints");
12261
1bedd215 12262 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
12263Breakpoint specific settings\n\
12264Configure various breakpoint-specific variables such as\n\
1bedd215 12265pending breakpoint behavior"),
fa8d40ab
JJ
12266 &breakpoint_set_cmdlist, "set breakpoint ",
12267 0/*allow-unknown*/, &setlist);
1bedd215 12268 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
12269Breakpoint specific settings\n\
12270Configure various breakpoint-specific variables such as\n\
1bedd215 12271pending breakpoint behavior"),
fa8d40ab
JJ
12272 &breakpoint_show_cmdlist, "show breakpoint ",
12273 0/*allow-unknown*/, &showlist);
12274
7915a72c
AC
12275 add_setshow_auto_boolean_cmd ("pending", no_class,
12276 &pending_break_support, _("\
12277Set debugger's behavior regarding pending breakpoints."), _("\
12278Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
12279If on, an unrecognized breakpoint location will cause gdb to create a\n\
12280pending breakpoint. If off, an unrecognized breakpoint location results in\n\
12281an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 12282user-query to see if a pending breakpoint should be created."),
2c5b56ce 12283 NULL,
920d2a44 12284 show_pending_break_support,
6e1d7d6c
AC
12285 &breakpoint_set_cmdlist,
12286 &breakpoint_show_cmdlist);
fa8d40ab
JJ
12287
12288 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
12289
12290 add_setshow_boolean_cmd ("auto-hw", no_class,
12291 &automatic_hardware_breakpoints, _("\
12292Set automatic usage of hardware breakpoints."), _("\
12293Show automatic usage of hardware breakpoints."), _("\
12294If set, the debugger will automatically use hardware breakpoints for\n\
12295breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
12296a warning will be emitted for such breakpoints."),
12297 NULL,
12298 show_automatic_hardware_breakpoints,
12299 &breakpoint_set_cmdlist,
12300 &breakpoint_show_cmdlist);
74960c60 12301
33e5cbd6
PA
12302 add_setshow_enum_cmd ("always-inserted", class_support,
12303 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
12304Set mode for inserting breakpoints."), _("\
12305Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
12306When this mode is off, breakpoints are inserted in inferior when it is\n\
12307resumed, and removed when execution stops. When this mode is on,\n\
12308breakpoints are inserted immediately and removed only when the user\n\
12309deletes the breakpoint. When this mode is auto (which is the default),\n\
12310the behaviour depends on the non-stop setting (see help set non-stop).\n\
12311In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
12312behaves as if always-inserted mode is on; if gdb is controlling the\n\
12313inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
12314 NULL,
12315 &show_always_inserted_mode,
12316 &breakpoint_set_cmdlist,
12317 &breakpoint_show_cmdlist);
765dc015
VP
12318
12319 automatic_hardware_breakpoints = 1;
f3b1572e
PA
12320
12321 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 12322}
This page took 1.895125 seconds and 4 git commands to generate.