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