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