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