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