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