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