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