* remote.c (remote_close): Reset INFERIOR_PTID to NULL_PTID
[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,
7b6bb8da 5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
a6d9a66e 23#include "arch-utils.h"
c906108c 24#include <ctype.h>
776592bf 25#include "hashtab.h"
c906108c
SS
26#include "symtab.h"
27#include "frame.h"
28#include "breakpoint.h"
1042e4c0 29#include "tracepoint.h"
c906108c
SS
30#include "gdbtypes.h"
31#include "expression.h"
32#include "gdbcore.h"
33#include "gdbcmd.h"
34#include "value.h"
35#include "command.h"
36#include "inferior.h"
37#include "gdbthread.h"
38#include "target.h"
39#include "language.h"
40#include "gdb_string.h"
41#include "demangle.h"
42#include "annotate.h"
43#include "symfile.h"
44#include "objfiles.h"
0378c332 45#include "source.h"
c5f0f3d0 46#include "linespec.h"
c94fdfd0 47#include "completer.h"
5b7f31a4 48#include "gdb.h"
8b93c638 49#include "ui-out.h"
e1507482 50#include "cli/cli-script.h"
0225421b 51#include "gdb_assert.h"
fe898f56 52#include "block.h"
a77053c2 53#include "solib.h"
84acb35a
JJ
54#include "solist.h"
55#include "observer.h"
60250e8b 56#include "exceptions.h"
765dc015 57#include "memattr.h"
f7f9143b 58#include "ada-lang.h"
d1aa2f50 59#include "top.h"
fa4727a6 60#include "wrapper.h"
79a45b7d 61#include "valprint.h"
4efc6507 62#include "jit.h"
a96d9b2e 63#include "xml-syscall.h"
65d79d4b 64#include "parser-defs.h"
c906108c 65
1042e4c0
SS
66/* readline include files */
67#include "readline/readline.h"
68#include "readline/history.h"
69
70/* readline defines this. */
71#undef savestring
72
034dad6f 73#include "mi/mi-common.h"
104c1213 74
44feb3ce
TT
75/* Arguments to pass as context to some catch command handlers. */
76#define CATCH_PERMANENT ((void *) (uintptr_t) 0)
77#define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
c906108c 78
4a64f543 79/* Prototypes for local functions. */
c906108c 80
a14ed312 81static void enable_delete_command (char *, int);
c906108c 82
a14ed312 83static void enable_once_command (char *, int);
c906108c 84
a14ed312 85static void disable_command (char *, int);
c906108c 86
a14ed312 87static void enable_command (char *, int);
c906108c 88
95a42b64
TT
89static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
90 void *),
91 void *);
c906108c 92
a14ed312 93static void ignore_command (char *, int);
c906108c 94
4efb68b1 95static int breakpoint_re_set_one (void *);
c906108c 96
a14ed312 97static void clear_command (char *, int);
c906108c 98
a14ed312 99static void catch_command (char *, int);
c906108c 100
e09342b5 101static int can_use_hardware_watchpoint (struct value *, int);
c906108c 102
98deb0da 103static void break_command_1 (char *, int, int);
c906108c 104
a14ed312 105static void mention (struct breakpoint *);
c906108c 106
4a64f543
MS
107/* This function is used in gdbtk sources and thus can not be made
108 static. */
63c252f8 109struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
a6d9a66e
UW
110 struct symtab_and_line,
111 enum bptype);
c906108c 112
76897487
KB
113static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
114
a6d9a66e
UW
115static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
116 CORE_ADDR bpaddr,
88f7da05 117 enum bptype bptype);
76897487 118
6c95b8df
PA
119static void describe_other_breakpoints (struct gdbarch *,
120 struct program_space *, CORE_ADDR,
5af949e3 121 struct obj_section *, int);
c906108c 122
6c95b8df
PA
123static int breakpoint_address_match (struct address_space *aspace1,
124 CORE_ADDR addr1,
125 struct address_space *aspace2,
126 CORE_ADDR addr2);
127
85d721b8
PA
128static int watchpoint_locations_match (struct bp_location *loc1,
129 struct bp_location *loc2);
130
a14ed312 131static void breakpoints_info (char *, int);
c906108c 132
d77f58be
SS
133static void watchpoints_info (char *, int);
134
135static int breakpoint_1 (int, int, int (*) (const struct breakpoint *));
c906108c 136
4efb68b1 137static int breakpoint_cond_eval (void *);
c906108c 138
4efb68b1 139static void cleanup_executing_breakpoints (void *);
c906108c 140
a14ed312 141static void commands_command (char *, int);
c906108c 142
a14ed312 143static void condition_command (char *, int);
c906108c 144
a14ed312 145static int get_number_trailer (char **, int);
c906108c 146
c5aa993b
JM
147typedef enum
148 {
149 mark_inserted,
150 mark_uninserted
151 }
152insertion_state_t;
c906108c 153
0bde7532 154static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 155static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 156
a14ed312 157static enum print_stop_action print_it_typical (bpstat);
e514a9d6
JM
158
159static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 160
4efb68b1 161static int watchpoint_check (void *);
c906108c 162
a14ed312 163static void maintenance_info_breakpoints (char *, int);
c906108c 164
a14ed312 165static int hw_breakpoint_used_count (void);
c906108c 166
a14ed312 167static int hw_watchpoint_used_count (enum bptype, int *);
c906108c 168
a14ed312 169static void hbreak_command (char *, int);
c906108c 170
a14ed312 171static void thbreak_command (char *, int);
c906108c 172
a14ed312 173static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
c906108c 174
a14ed312 175static void stop_command (char *arg, int from_tty);
7a292a7a 176
a14ed312 177static void stopin_command (char *arg, int from_tty);
7a292a7a 178
a14ed312 179static void stopat_command (char *arg, int from_tty);
7a292a7a 180
a14ed312 181static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 182
d85310f7
MS
183static void catch_exception_command_1 (enum exception_event_kind ex_event,
184 char *arg, int tempflag, int from_tty);
7a292a7a 185
a14ed312 186static void tcatch_command (char *arg, int from_tty);
7a292a7a 187
a14ed312 188static void ep_skip_leading_whitespace (char **s);
7a292a7a 189
d03285ec
UW
190static void detach_single_step_breakpoints (void);
191
6c95b8df
PA
192static int single_step_breakpoint_inserted_here_p (struct address_space *,
193 CORE_ADDR pc);
1aafd4da 194
fe3f5fa8 195static void free_bp_location (struct bp_location *loc);
f431efe5
PA
196static void incref_bp_location (struct bp_location *loc);
197static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 198
39d61571 199static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 200
b60e7edf 201static void update_global_location_list (int);
a5606eee 202
b60e7edf 203static void update_global_location_list_nothrow (int);
74960c60 204
d77f58be 205static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60 206
d77f58be 207static int is_watchpoint (const struct breakpoint *bpt);
60e1c644 208
74960c60 209static void insert_breakpoint_locations (void);
a5606eee 210
a96d9b2e
SDJ
211static int syscall_catchpoint_p (struct breakpoint *b);
212
1042e4c0
SS
213static void tracepoints_info (char *, int);
214
215static void delete_trace_command (char *, int);
216
217static void enable_trace_command (char *, int);
218
219static void disable_trace_command (char *, int);
220
221static void trace_pass_command (char *, int);
222
0fb4aa4b
PA
223/* Assuming we're creating a static tracepoint, does S look like a
224 static tracepoint marker spec ("-m MARKER_ID")? */
225#define is_marker_spec(s) \
f5a8e22b 226 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 227
5cea2a26
PA
228/* A reference-counted struct command_line. This lets multiple
229 breakpoints share a single command list. */
230struct counted_command_line
231{
232 /* The reference count. */
233 int refc;
234
235 /* The command list. */
236 struct command_line *commands;
237};
238
239struct command_line *
240breakpoint_commands (struct breakpoint *b)
241{
242 return b->commands ? b->commands->commands : NULL;
243}
3daf8fe5 244
f3b1572e
PA
245/* Flag indicating that a command has proceeded the inferior past the
246 current breakpoint. */
247
248static int breakpoint_proceeded;
249
2cec12e5
AR
250static const char *
251bpdisp_text (enum bpdisp disp)
252{
4a64f543
MS
253 /* NOTE: the following values are a part of MI protocol and
254 represent values of 'disp' field returned when inferior stops at
255 a breakpoint. */
bc043ef3 256 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 257
2cec12e5
AR
258 return bpdisps[(int) disp];
259}
c906108c 260
4a64f543 261/* Prototypes for exported functions. */
c906108c 262/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 263 if such is available. */
c906108c
SS
264static int can_use_hw_watchpoints;
265
920d2a44
AC
266static void
267show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
268 struct cmd_list_element *c,
269 const char *value)
270{
3e43a32a
MS
271 fprintf_filtered (file,
272 _("Debugger's willingness to use "
273 "watchpoint hardware is %s.\n"),
920d2a44
AC
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
4a64f543 279 for unrecognized breakpoint locations.
fa8d40ab
JJ
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{
3e43a32a
MS
287 fprintf_filtered (file,
288 _("Debugger's behavior regarding "
289 "pending breakpoints is %s.\n"),
920d2a44
AC
290 value);
291}
fa8d40ab 292
765dc015 293/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 294 set with "break" but falling in read-only memory.
765dc015
VP
295 If 0, gdb will warn about such breakpoints, but won't automatically
296 use hardware breakpoints. */
297static int automatic_hardware_breakpoints;
298static void
299show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
300 struct cmd_list_element *c,
301 const char *value)
302{
3e43a32a
MS
303 fprintf_filtered (file,
304 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
305 value);
306}
307
33e5cbd6
PA
308/* If on, gdb will keep breakpoints inserted even as inferior is
309 stopped, and immediately insert any new breakpoints. If off, gdb
310 will insert breakpoints into inferior only when resuming it, and
311 will remove breakpoints upon stop. If auto, GDB will behave as ON
312 if in non-stop mode, and as OFF if all-stop mode.*/
313
314static const char always_inserted_auto[] = "auto";
315static const char always_inserted_on[] = "on";
316static const char always_inserted_off[] = "off";
317static const char *always_inserted_enums[] = {
318 always_inserted_auto,
319 always_inserted_off,
320 always_inserted_on,
321 NULL
322};
323static const char *always_inserted_mode = always_inserted_auto;
324static void
74960c60 325show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 326 struct cmd_list_element *c, const char *value)
74960c60 327{
33e5cbd6 328 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
329 fprintf_filtered (file,
330 _("Always inserted breakpoint "
331 "mode is %s (currently %s).\n"),
33e5cbd6
PA
332 value,
333 breakpoints_always_inserted_mode () ? "on" : "off");
334 else
3e43a32a
MS
335 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
336 value);
74960c60
VP
337}
338
33e5cbd6
PA
339int
340breakpoints_always_inserted_mode (void)
341{
342 return (always_inserted_mode == always_inserted_on
343 || (always_inserted_mode == always_inserted_auto && non_stop));
344}
765dc015 345
a14ed312 346void _initialize_breakpoint (void);
c906108c 347
c906108c
SS
348/* Are we executing breakpoint commands? */
349static int executing_breakpoint_commands;
350
c02f5703
MS
351/* Are overlay event breakpoints enabled? */
352static int overlay_events_enabled;
353
e09342b5
TJB
354/* See description in breakpoint.h. */
355int target_exact_watchpoints = 0;
356
c906108c 357/* Walk the following statement or block through all breakpoints.
4a64f543
MS
358 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the
359 current breakpoint. */
c906108c 360
5c44784c 361#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 362
5c44784c
JM
363#define ALL_BREAKPOINTS_SAFE(B,TMP) \
364 for (B = breakpoint_chain; \
365 B ? (TMP=B->next, 1): 0; \
366 B = TMP)
c906108c 367
4a64f543
MS
368/* Similar iterator for the low-level breakpoints. SAFE variant is
369 not provided so update_global_location_list must not be called
370 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 371
876fa593
JK
372#define ALL_BP_LOCATIONS(B,BP_TMP) \
373 for (BP_TMP = bp_location; \
374 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
375 BP_TMP++)
7cc221ef 376
1042e4c0
SS
377/* Iterator for tracepoints only. */
378
379#define ALL_TRACEPOINTS(B) \
380 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 381 if (is_tracepoint (B))
1042e4c0 382
7cc221ef 383/* Chains of all breakpoints defined. */
c906108c
SS
384
385struct breakpoint *breakpoint_chain;
386
876fa593
JK
387/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
388
389static struct bp_location **bp_location;
390
391/* Number of elements of BP_LOCATION. */
392
393static unsigned bp_location_count;
394
4a64f543
MS
395/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
396 ADDRESS for the current elements of BP_LOCATION which get a valid
397 result from bp_location_has_shadow. You can use it for roughly
398 limiting the subrange of BP_LOCATION to scan for shadow bytes for
399 an address you need to read. */
876fa593
JK
400
401static CORE_ADDR bp_location_placed_address_before_address_max;
402
4a64f543
MS
403/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
404 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
405 BP_LOCATION which get a valid result from bp_location_has_shadow.
406 You can use it for roughly limiting the subrange of BP_LOCATION to
407 scan for shadow bytes for an address you need to read. */
876fa593
JK
408
409static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 410
4a64f543
MS
411/* The locations that no longer correspond to any breakpoint, unlinked
412 from bp_location array, but for which a hit may still be reported
413 by a target. */
20874c92
VP
414VEC(bp_location_p) *moribund_locations = NULL;
415
c906108c
SS
416/* Number of last breakpoint made. */
417
95a42b64
TT
418static int breakpoint_count;
419
86b17b60
PA
420/* The value of `breakpoint_count' before the last command that
421 created breakpoints. If the last (break-like) command created more
422 than one breakpoint, then the difference between BREAKPOINT_COUNT
423 and PREV_BREAKPOINT_COUNT is more than one. */
424static int prev_breakpoint_count;
c906108c 425
1042e4c0
SS
426/* Number of last tracepoint made. */
427
95a42b64 428static int tracepoint_count;
1042e4c0 429
6149aea9
PA
430static struct cmd_list_element *breakpoint_set_cmdlist;
431static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 432struct cmd_list_element *save_cmdlist;
6149aea9 433
468d015d
JJ
434/* Return whether a breakpoint is an active enabled breakpoint. */
435static int
436breakpoint_enabled (struct breakpoint *b)
437{
0d381245 438 return (b->enable_state == bp_enabled);
468d015d
JJ
439}
440
c906108c
SS
441/* Set breakpoint count to NUM. */
442
95a42b64 443static void
fba45db2 444set_breakpoint_count (int num)
c906108c 445{
86b17b60 446 prev_breakpoint_count = breakpoint_count;
c906108c 447 breakpoint_count = num;
4fa62494 448 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
449}
450
86b17b60
PA
451/* Used by `start_rbreak_breakpoints' below, to record the current
452 breakpoint count before "rbreak" creates any breakpoint. */
453static int rbreak_start_breakpoint_count;
454
95a42b64
TT
455/* Called at the start an "rbreak" command to record the first
456 breakpoint made. */
86b17b60 457
95a42b64
TT
458void
459start_rbreak_breakpoints (void)
460{
86b17b60 461 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
462}
463
464/* Called at the end of an "rbreak" command to record the last
465 breakpoint made. */
86b17b60 466
95a42b64
TT
467void
468end_rbreak_breakpoints (void)
469{
86b17b60 470 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
471}
472
4a64f543 473/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
474
475void
fba45db2 476clear_breakpoint_hit_counts (void)
c906108c
SS
477{
478 struct breakpoint *b;
479
480 ALL_BREAKPOINTS (b)
481 b->hit_count = 0;
482}
483
9add0f1b
TT
484/* Allocate a new counted_command_line with reference count of 1.
485 The new structure owns COMMANDS. */
486
487static struct counted_command_line *
488alloc_counted_command_line (struct command_line *commands)
489{
490 struct counted_command_line *result
491 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 492
9add0f1b
TT
493 result->refc = 1;
494 result->commands = commands;
495 return result;
496}
497
498/* Increment reference count. This does nothing if CMD is NULL. */
499
500static void
501incref_counted_command_line (struct counted_command_line *cmd)
502{
503 if (cmd)
504 ++cmd->refc;
505}
506
507/* Decrement reference count. If the reference count reaches 0,
508 destroy the counted_command_line. Sets *CMDP to NULL. This does
509 nothing if *CMDP is NULL. */
510
511static void
512decref_counted_command_line (struct counted_command_line **cmdp)
513{
514 if (*cmdp)
515 {
516 if (--(*cmdp)->refc == 0)
517 {
518 free_command_lines (&(*cmdp)->commands);
519 xfree (*cmdp);
520 }
521 *cmdp = NULL;
522 }
523}
524
525/* A cleanup function that calls decref_counted_command_line. */
526
527static void
528do_cleanup_counted_command_line (void *arg)
529{
530 decref_counted_command_line (arg);
531}
532
533/* Create a cleanup that calls decref_counted_command_line on the
534 argument. */
535
536static struct cleanup *
537make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
538{
539 return make_cleanup (do_cleanup_counted_command_line, cmdp);
540}
541
c906108c
SS
542/* Default address, symtab and line to put a breakpoint at
543 for "break" command with no arg.
4a64f543 544 If default_breakpoint_valid is zero, the other three are
c906108c
SS
545 not valid, and "break" with no arg is an error.
546
547 This set by print_stack_frame, which calls set_default_breakpoint. */
548
549int default_breakpoint_valid;
550CORE_ADDR default_breakpoint_address;
551struct symtab *default_breakpoint_symtab;
552int default_breakpoint_line;
6c95b8df
PA
553struct program_space *default_breakpoint_pspace;
554
c906108c 555\f
4a64f543
MS
556/* *PP is a string denoting a breakpoint. Get the number of the
557 breakpoint. Advance *PP after the string and any trailing
558 whitespace.
c906108c 559
4a64f543
MS
560 Currently the string can either be a number or "$" followed by the
561 name of a convenience variable. Making it an expression wouldn't
562 work well for map_breakpoint_numbers (e.g. "4 + 5 + 6").
40c03ae8 563
5c44784c 564 TRAILER is a character which can be found after the number; most
4a64f543
MS
565 commonly this is `-'. If you don't want a trailer, use \0. */
566
c906108c 567static int
fba45db2 568get_number_trailer (char **pp, int trailer)
c906108c 569{
5c44784c 570 int retval = 0; /* default */
c906108c
SS
571 char *p = *pp;
572
3c3fe74c 573 if (*p == '$')
c906108c
SS
574 {
575 /* Make a copy of the name, so we can null-terminate it
c5aa993b 576 to pass to lookup_internalvar(). */
c906108c
SS
577 char *varname;
578 char *start = ++p;
4fa62494 579 LONGEST val;
c906108c
SS
580
581 while (isalnum (*p) || *p == '_')
582 p++;
583 varname = (char *) alloca (p - start + 1);
584 strncpy (varname, start, p - start);
585 varname[p - start] = '\0';
4fa62494
UW
586 if (get_internalvar_integer (lookup_internalvar (varname), &val))
587 retval = (int) val;
5c44784c
JM
588 else
589 {
3e43a32a
MS
590 printf_filtered (_("Convenience variable must "
591 "have integer value.\n"));
5c44784c
JM
592 retval = 0;
593 }
c906108c
SS
594 }
595 else
596 {
597 if (*p == '-')
598 ++p;
599 while (*p >= '0' && *p <= '9')
600 ++p;
601 if (p == *pp)
602 /* There is no number here. (e.g. "cond a == b"). */
5c44784c 603 {
4a64f543 604 /* Skip non-numeric token. */
5c44784c
JM
605 while (*p && !isspace((int) *p))
606 ++p;
4a64f543 607 /* Return zero, which caller must interpret as error. */
5c44784c
JM
608 retval = 0;
609 }
610 else
611 retval = atoi (*pp);
612 }
613 if (!(isspace (*p) || *p == '\0' || *p == trailer))
614 {
4a64f543 615 /* Trailing junk: return 0 and let caller print error msg. */
5c44784c
JM
616 while (!(isspace (*p) || *p == '\0' || *p == trailer))
617 ++p;
618 retval = 0;
c906108c 619 }
c906108c
SS
620 while (isspace (*p))
621 p++;
622 *pp = p;
623 return retval;
624}
5c44784c 625
11cf8741 626
5c44784c
JM
627/* Like get_number_trailer, but don't allow a trailer. */
628int
fba45db2 629get_number (char **pp)
5c44784c
JM
630{
631 return get_number_trailer (pp, '\0');
632}
633
634/* Parse a number or a range.
4a64f543
MS
635 A number will be of the form handled by get_number.
636 A range will be of the form <number1> - <number2>, and
637 will represent all the integers between number1 and number2,
638 inclusive.
639
640 While processing a range, this fuction is called iteratively;
641 At each call it will return the next value in the range.
642
643 At the beginning of parsing a range, the char pointer PP will
644 be advanced past <number1> and left pointing at the '-' token.
645 Subsequent calls will not advance the pointer until the range
646 is completed. The call that completes the range will advance
647 pointer PP past <number2>. */
5c44784c 648
3c3fe74c 649int
fba45db2 650get_number_or_range (char **pp)
5c44784c
JM
651{
652 static int last_retval, end_value;
653 static char *end_ptr;
654 static int in_range = 0;
655
656 if (**pp != '-')
657 {
658 /* Default case: pp is pointing either to a solo number,
659 or to the first number of a range. */
660 last_retval = get_number_trailer (pp, '-');
661 if (**pp == '-')
662 {
663 char **temp;
664
665 /* This is the start of a range (<number1> - <number2>).
666 Skip the '-', parse and remember the second number,
667 and also remember the end of the final token. */
668
669 temp = &end_ptr;
670 end_ptr = *pp + 1;
671 while (isspace ((int) *end_ptr))
672 end_ptr++; /* skip white space */
673 end_value = get_number (temp);
674 if (end_value < last_retval)
675 {
8a3fe4f8 676 error (_("inverted range"));
5c44784c
JM
677 }
678 else if (end_value == last_retval)
679 {
4a64f543 680 /* Degenerate range (number1 == number2). Advance the
5c44784c
JM
681 token pointer so that the range will be treated as a
682 single number. */
683 *pp = end_ptr;
684 }
685 else
686 in_range = 1;
687 }
688 }
689 else if (! in_range)
8a3fe4f8 690 error (_("negative value"));
5c44784c
JM
691 else
692 {
693 /* pp points to the '-' that betokens a range. All
694 number-parsing has already been done. Return the next
695 integer value (one greater than the saved previous value).
696 Do not advance the token pointer 'pp' until the end of range
697 is reached. */
698
699 if (++last_retval == end_value)
700 {
701 /* End of range reached; advance token pointer. */
702 *pp = end_ptr;
703 in_range = 0;
704 }
705 }
706 return last_retval;
707}
708
48cb2d85
VP
709/* Return the breakpoint with the specified number, or NULL
710 if the number does not refer to an existing breakpoint. */
711
712struct breakpoint *
713get_breakpoint (int num)
714{
715 struct breakpoint *b;
716
717 ALL_BREAKPOINTS (b)
718 if (b->number == num)
719 return b;
720
721 return NULL;
722}
5c44784c 723
c906108c 724\f
adc36818
PM
725
726void
727set_breakpoint_condition (struct breakpoint *b, char *exp,
728 int from_tty)
729{
730 struct bp_location *loc = b->loc;
731
732 for (; loc; loc = loc->next)
733 {
734 xfree (loc->cond);
735 loc->cond = NULL;
736 }
737 xfree (b->cond_string);
738 b->cond_string = NULL;
739 xfree (b->cond_exp);
740 b->cond_exp = NULL;
741
742 if (*exp == 0)
743 {
744 if (from_tty)
745 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
746 }
747 else
748 {
749 char *arg = exp;
cc59ec59 750
adc36818
PM
751 /* I don't know if it matters whether this is the string the user
752 typed in or the decompiled expression. */
753 b->cond_string = xstrdup (arg);
754 b->condition_not_parsed = 0;
755
756 if (is_watchpoint (b))
757 {
758 innermost_block = NULL;
759 arg = exp;
760 b->cond_exp = parse_exp_1 (&arg, 0, 0);
761 if (*arg)
762 error (_("Junk at end of expression"));
763 b->cond_exp_valid_block = innermost_block;
764 }
765 else
766 {
767 for (loc = b->loc; loc; loc = loc->next)
768 {
769 arg = exp;
770 loc->cond =
771 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
772 if (*arg)
773 error (_("Junk at end of expression"));
774 }
775 }
776 }
777 breakpoints_changed ();
778 observer_notify_breakpoint_modified (b->number);
779}
780
c906108c
SS
781/* condition N EXP -- set break condition of breakpoint N to EXP. */
782
783static void
fba45db2 784condition_command (char *arg, int from_tty)
c906108c 785{
52f0bd74 786 struct breakpoint *b;
c906108c 787 char *p;
52f0bd74 788 int bnum;
c906108c
SS
789
790 if (arg == 0)
e2e0b3e5 791 error_no_arg (_("breakpoint number"));
c906108c
SS
792
793 p = arg;
794 bnum = get_number (&p);
5c44784c 795 if (bnum == 0)
8a3fe4f8 796 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
797
798 ALL_BREAKPOINTS (b)
799 if (b->number == bnum)
2f069f6f 800 {
2566ad2d 801 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
802 return;
803 }
c906108c 804
8a3fe4f8 805 error (_("No breakpoint number %d."), bnum);
c906108c
SS
806}
807
a7bdde9e
VP
808/* Check that COMMAND do not contain commands that are suitable
809 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
810 Throw if any such commands is found. */
811
a7bdde9e
VP
812static void
813check_no_tracepoint_commands (struct command_line *commands)
814{
815 struct command_line *c;
cc59ec59 816
a7bdde9e
VP
817 for (c = commands; c; c = c->next)
818 {
819 int i;
820
821 if (c->control_type == while_stepping_control)
3e43a32a
MS
822 error (_("The 'while-stepping' command can "
823 "only be used for tracepoints"));
a7bdde9e
VP
824
825 for (i = 0; i < c->body_count; ++i)
826 check_no_tracepoint_commands ((c->body_list)[i]);
827
828 /* Not that command parsing removes leading whitespace and comment
4a64f543 829 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
830 command directly. */
831 if (strstr (c->line, "collect ") == c->line)
832 error (_("The 'collect' command can only be used for tracepoints"));
833
51661e93
VP
834 if (strstr (c->line, "teval ") == c->line)
835 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
836 }
837}
838
d77f58be
SS
839/* Encapsulate tests for different types of tracepoints. */
840
a7bdde9e 841int
d77f58be 842is_tracepoint (const struct breakpoint *b)
a7bdde9e 843{
0fb4aa4b
PA
844 return (b->type == bp_tracepoint
845 || b->type == bp_fast_tracepoint
846 || b->type == bp_static_tracepoint);
a7bdde9e 847}
d77f58be 848
95a42b64
TT
849/* A helper function that validsates that COMMANDS are valid for a
850 breakpoint. This function will throw an exception if a problem is
851 found. */
48cb2d85 852
95a42b64
TT
853static void
854validate_commands_for_breakpoint (struct breakpoint *b,
855 struct command_line *commands)
48cb2d85 856{
d77f58be 857 if (is_tracepoint (b))
a7bdde9e 858 {
4a64f543
MS
859 /* We need to verify that each top-level element of commands is
860 valid for tracepoints, that there's at most one
861 while-stepping element, and that while-stepping's body has
862 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
863 struct command_line *c;
864 struct command_line *while_stepping = 0;
865 for (c = commands; c; c = c->next)
866 {
a7bdde9e
VP
867 if (c->control_type == while_stepping_control)
868 {
869 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
870 error (_("The 'while-stepping' command "
871 "cannot be used for fast tracepoint"));
0fb4aa4b 872 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
873 error (_("The 'while-stepping' command "
874 "cannot be used for static tracepoint"));
a7bdde9e
VP
875
876 if (while_stepping)
3e43a32a
MS
877 error (_("The 'while-stepping' command "
878 "can be used only once"));
a7bdde9e
VP
879 else
880 while_stepping = c;
881 }
882 }
883 if (while_stepping)
884 {
885 struct command_line *c2;
886
887 gdb_assert (while_stepping->body_count == 1);
888 c2 = while_stepping->body_list[0];
889 for (; c2; c2 = c2->next)
890 {
a7bdde9e
VP
891 if (c2->control_type == while_stepping_control)
892 error (_("The 'while-stepping' command cannot be nested"));
893 }
894 }
895 }
896 else
897 {
898 check_no_tracepoint_commands (commands);
899 }
95a42b64
TT
900}
901
0fb4aa4b
PA
902/* Return a vector of all the static tracepoints set at ADDR. The
903 caller is responsible for releasing the vector. */
904
905VEC(breakpoint_p) *
906static_tracepoints_here (CORE_ADDR addr)
907{
908 struct breakpoint *b;
909 VEC(breakpoint_p) *found = 0;
910 struct bp_location *loc;
911
912 ALL_BREAKPOINTS (b)
913 if (b->type == bp_static_tracepoint)
914 {
915 for (loc = b->loc; loc; loc = loc->next)
916 if (loc->address == addr)
917 VEC_safe_push(breakpoint_p, found, b);
918 }
919
920 return found;
921}
922
95a42b64 923/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 924 validate that only allowed commands are included. */
95a42b64
TT
925
926void
4a64f543
MS
927breakpoint_set_commands (struct breakpoint *b,
928 struct command_line *commands)
95a42b64
TT
929{
930 validate_commands_for_breakpoint (b, commands);
a7bdde9e 931
9add0f1b
TT
932 decref_counted_command_line (&b->commands);
933 b->commands = alloc_counted_command_line (commands);
48cb2d85
VP
934 breakpoints_changed ();
935 observer_notify_breakpoint_modified (b->number);
936}
937
45a43567
TT
938/* Set the internal `silent' flag on the breakpoint. Note that this
939 is not the same as the "silent" that may appear in the breakpoint's
940 commands. */
941
942void
943breakpoint_set_silent (struct breakpoint *b, int silent)
944{
945 int old_silent = b->silent;
946
947 b->silent = silent;
948 if (old_silent != silent)
949 observer_notify_breakpoint_modified (b->number);
950}
951
952/* Set the thread for this breakpoint. If THREAD is -1, make the
953 breakpoint work for any thread. */
954
955void
956breakpoint_set_thread (struct breakpoint *b, int thread)
957{
958 int old_thread = b->thread;
959
960 b->thread = thread;
961 if (old_thread != thread)
962 observer_notify_breakpoint_modified (b->number);
963}
964
965/* Set the task for this breakpoint. If TASK is 0, make the
966 breakpoint work for any task. */
967
968void
969breakpoint_set_task (struct breakpoint *b, int task)
970{
971 int old_task = b->task;
972
973 b->task = task;
974 if (old_task != task)
975 observer_notify_breakpoint_modified (b->number);
976}
977
95a42b64
TT
978void
979check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
980{
981 struct breakpoint *b = closure;
cc59ec59 982
a7bdde9e
VP
983 validate_actionline (&line, b);
984}
985
95a42b64
TT
986/* A structure used to pass information through
987 map_breakpoint_numbers. */
988
989struct commands_info
990{
991 /* True if the command was typed at a tty. */
992 int from_tty;
86b17b60
PA
993
994 /* The breakpoint range spec. */
995 char *arg;
996
95a42b64
TT
997 /* Non-NULL if the body of the commands are being read from this
998 already-parsed command. */
999 struct command_line *control;
86b17b60 1000
95a42b64
TT
1001 /* The command lines read from the user, or NULL if they have not
1002 yet been read. */
1003 struct counted_command_line *cmd;
1004};
1005
1006/* A callback for map_breakpoint_numbers that sets the commands for
1007 commands_command. */
1008
c906108c 1009static void
95a42b64 1010do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1011{
95a42b64 1012 struct commands_info *info = data;
c906108c 1013
95a42b64
TT
1014 if (info->cmd == NULL)
1015 {
1016 struct command_line *l;
5c44784c 1017
95a42b64
TT
1018 if (info->control != NULL)
1019 l = copy_command_lines (info->control->body_list[0]);
1020 else
86b17b60
PA
1021 {
1022 struct cleanup *old_chain;
1023 char *str;
c5aa993b 1024
3e43a32a
MS
1025 str = xstrprintf (_("Type commands for breakpoint(s) "
1026 "%s, one per line."),
86b17b60
PA
1027 info->arg);
1028
1029 old_chain = make_cleanup (xfree, str);
1030
1031 l = read_command_lines (str,
1032 info->from_tty, 1,
d77f58be 1033 (is_tracepoint (b)
86b17b60
PA
1034 ? check_tracepoint_command : 0),
1035 b);
1036
1037 do_cleanups (old_chain);
1038 }
a7bdde9e 1039
95a42b64
TT
1040 info->cmd = alloc_counted_command_line (l);
1041 }
1042
1043 /* If a breakpoint was on the list more than once, we don't need to
1044 do anything. */
1045 if (b->commands != info->cmd)
1046 {
1047 validate_commands_for_breakpoint (b, info->cmd->commands);
1048 incref_counted_command_line (info->cmd);
1049 decref_counted_command_line (&b->commands);
1050 b->commands = info->cmd;
1051 breakpoints_changed ();
1052 observer_notify_breakpoint_modified (b->number);
c5aa993b 1053 }
95a42b64
TT
1054}
1055
1056static void
4a64f543
MS
1057commands_command_1 (char *arg, int from_tty,
1058 struct command_line *control)
95a42b64
TT
1059{
1060 struct cleanup *cleanups;
1061 struct commands_info info;
1062
1063 info.from_tty = from_tty;
1064 info.control = control;
1065 info.cmd = NULL;
1066 /* If we read command lines from the user, then `info' will hold an
1067 extra reference to the commands that we must clean up. */
1068 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1069
1070 if (arg == NULL || !*arg)
1071 {
86b17b60 1072 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1073 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1074 breakpoint_count);
95a42b64
TT
1075 else if (breakpoint_count > 0)
1076 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1077 else
1078 {
1079 /* So that we don't try to free the incoming non-NULL
1080 argument in the cleanup below. Mapping breakpoint
1081 numbers will fail in this case. */
1082 arg = NULL;
1083 }
95a42b64 1084 }
9766ced4
SS
1085 else
1086 /* The command loop has some static state, so we need to preserve
1087 our argument. */
1088 arg = xstrdup (arg);
86b17b60
PA
1089
1090 if (arg != NULL)
1091 make_cleanup (xfree, arg);
1092
1093 info.arg = arg;
95a42b64
TT
1094
1095 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1096
1097 if (info.cmd == NULL)
1098 error (_("No breakpoints specified."));
1099
1100 do_cleanups (cleanups);
1101}
1102
1103static void
1104commands_command (char *arg, int from_tty)
1105{
1106 commands_command_1 (arg, from_tty, NULL);
c906108c 1107}
40c03ae8
EZ
1108
1109/* Like commands_command, but instead of reading the commands from
1110 input stream, takes them from an already parsed command structure.
1111
1112 This is used by cli-script.c to DTRT with breakpoint commands
1113 that are part of if and while bodies. */
1114enum command_control_type
1115commands_from_control_command (char *arg, struct command_line *cmd)
1116{
95a42b64
TT
1117 commands_command_1 (arg, 0, cmd);
1118 return simple_control;
40c03ae8 1119}
876fa593
JK
1120
1121/* Return non-zero if BL->TARGET_INFO contains valid information. */
1122
1123static int
1124bp_location_has_shadow (struct bp_location *bl)
1125{
1126 if (bl->loc_type != bp_loc_software_breakpoint)
1127 return 0;
1128 if (!bl->inserted)
1129 return 0;
1130 if (bl->target_info.shadow_len == 0)
1131 /* bp isn't valid, or doesn't shadow memory. */
1132 return 0;
1133 return 1;
1134}
1135
8defab1a 1136/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1137 by replacing any memory breakpoints with their shadowed contents.
1138
1139 The range of shadowed area by each bp_location is:
35df4500
TJB
1140 bl->address - bp_location_placed_address_before_address_max
1141 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1142 The range we were requested to resolve shadows for is:
1143 memaddr ... memaddr + len
1144 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1145 memaddr + len <= (bl->address
1146 - bp_location_placed_address_before_address_max)
876fa593 1147 and:
35df4500 1148 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1149
8defab1a
DJ
1150void
1151breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1152{
4a64f543
MS
1153 /* Left boundary, right boundary and median element of our binary
1154 search. */
876fa593
JK
1155 unsigned bc_l, bc_r, bc;
1156
4a64f543
MS
1157 /* Find BC_L which is a leftmost element which may affect BUF
1158 content. It is safe to report lower value but a failure to
1159 report higher one. */
876fa593
JK
1160
1161 bc_l = 0;
1162 bc_r = bp_location_count;
1163 while (bc_l + 1 < bc_r)
1164 {
35df4500 1165 struct bp_location *bl;
876fa593
JK
1166
1167 bc = (bc_l + bc_r) / 2;
35df4500 1168 bl = bp_location[bc];
876fa593 1169
4a64f543
MS
1170 /* Check first BL->ADDRESS will not overflow due to the added
1171 constant. Then advance the left boundary only if we are sure
1172 the BC element can in no way affect the BUF content (MEMADDR
1173 to MEMADDR + LEN range).
876fa593 1174
4a64f543
MS
1175 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1176 offset so that we cannot miss a breakpoint with its shadow
1177 range tail still reaching MEMADDR. */
c5aa993b 1178
35df4500
TJB
1179 if ((bl->address + bp_location_shadow_len_after_address_max
1180 >= bl->address)
1181 && (bl->address + bp_location_shadow_len_after_address_max
1182 <= memaddr))
876fa593
JK
1183 bc_l = bc;
1184 else
1185 bc_r = bc;
1186 }
1187
128070bb
PA
1188 /* Due to the binary search above, we need to make sure we pick the
1189 first location that's at BC_L's address. E.g., if there are
1190 multiple locations at the same address, BC_L may end up pointing
1191 at a duplicate location, and miss the "master"/"inserted"
1192 location. Say, given locations L1, L2 and L3 at addresses A and
1193 B:
1194
1195 L1@A, L2@A, L3@B, ...
1196
1197 BC_L could end up pointing at location L2, while the "master"
1198 location could be L1. Since the `loc->inserted' flag is only set
1199 on "master" locations, we'd forget to restore the shadow of L1
1200 and L2. */
1201 while (bc_l > 0
1202 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1203 bc_l--;
1204
876fa593
JK
1205 /* Now do full processing of the found relevant range of elements. */
1206
1207 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1208 {
35df4500 1209 struct bp_location *bl = bp_location[bc];
876fa593
JK
1210 CORE_ADDR bp_addr = 0;
1211 int bp_size = 0;
1212 int bptoffset = 0;
1213
35df4500
TJB
1214 /* bp_location array has BL->OWNER always non-NULL. */
1215 if (bl->owner->type == bp_none)
8a3fe4f8 1216 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1217 bl->owner->number);
ffce0d52 1218
876fa593
JK
1219 /* Performance optimization: any futher element can no longer affect BUF
1220 content. */
1221
35df4500
TJB
1222 if (bl->address >= bp_location_placed_address_before_address_max
1223 && memaddr + len <= (bl->address
1224 - bp_location_placed_address_before_address_max))
876fa593
JK
1225 break;
1226
35df4500 1227 if (!bp_location_has_shadow (bl))
c5aa993b 1228 continue;
35df4500 1229 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1230 current_program_space->aspace, 0))
1231 continue;
1232
c5aa993b
JM
1233 /* Addresses and length of the part of the breakpoint that
1234 we need to copy. */
35df4500
TJB
1235 bp_addr = bl->target_info.placed_address;
1236 bp_size = bl->target_info.shadow_len;
8defab1a 1237
c5aa993b
JM
1238 if (bp_addr + bp_size <= memaddr)
1239 /* The breakpoint is entirely before the chunk of memory we
1240 are reading. */
1241 continue;
8defab1a 1242
c5aa993b
JM
1243 if (bp_addr >= memaddr + len)
1244 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1245 reading. */
c5aa993b 1246 continue;
c5aa993b 1247
8defab1a
DJ
1248 /* Offset within shadow_contents. */
1249 if (bp_addr < memaddr)
1250 {
1251 /* Only copy the second part of the breakpoint. */
1252 bp_size -= memaddr - bp_addr;
1253 bptoffset = memaddr - bp_addr;
1254 bp_addr = memaddr;
1255 }
c5aa993b 1256
8defab1a
DJ
1257 if (bp_addr + bp_size > memaddr + len)
1258 {
1259 /* Only copy the first part of the breakpoint. */
1260 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1261 }
c5aa993b 1262
8defab1a 1263 memcpy (buf + bp_addr - memaddr,
35df4500 1264 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1265 }
c906108c 1266}
c906108c 1267\f
c5aa993b 1268
60e1c644
PA
1269/* Return true if BPT is of any hardware watchpoint kind. */
1270
a5606eee 1271static int
d77f58be 1272is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1273{
1274 return (bpt->type == bp_hardware_watchpoint
1275 || bpt->type == bp_read_watchpoint
1276 || bpt->type == bp_access_watchpoint);
1277}
7270d8f2 1278
60e1c644
PA
1279/* Return true if BPT is of any watchpoint kind, hardware or
1280 software. */
1281
1282static int
d77f58be 1283is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1284{
1285 return (is_hardware_watchpoint (bpt)
1286 || bpt->type == bp_watchpoint);
1287}
1288
f6bc2008
PA
1289/* Assuming that B is a watchpoint: returns true if the current thread
1290 and its running state are safe to evaluate or update watchpoint B.
1291 Watchpoints on local expressions need to be evaluated in the
1292 context of the thread that was current when the watchpoint was
1293 created, and, that thread needs to be stopped to be able to select
1294 the correct frame context. Watchpoints on global expressions can
1295 be evaluated on any thread, and in any state. It is presently left
1296 to the target allowing memory accesses when threads are
1297 running. */
1298
1299static int
1300watchpoint_in_thread_scope (struct breakpoint *b)
1301{
1302 return (ptid_equal (b->watchpoint_thread, null_ptid)
1303 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1304 && !is_executing (inferior_ptid)));
1305}
1306
567e1b4e
JB
1307/* Assuming that B is a watchpoint:
1308 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1309 - Evaluate expression and store the result in B->val
567e1b4e
JB
1310 - Evaluate the condition if there is one, and store the result
1311 in b->loc->cond.
a5606eee
VP
1312 - Update the list of values that must be watched in B->loc.
1313
4a64f543
MS
1314 If the watchpoint disposition is disp_del_at_next_stop, then do
1315 nothing. If this is local watchpoint that is out of scope, delete
1316 it.
1317
1318 Even with `set breakpoint always-inserted on' the watchpoints are
1319 removed + inserted on each stop here. Normal breakpoints must
1320 never be removed because they might be missed by a running thread
1321 when debugging in non-stop mode. On the other hand, hardware
1322 watchpoints (is_hardware_watchpoint; processed here) are specific
1323 to each LWP since they are stored in each LWP's hardware debug
1324 registers. Therefore, such LWP must be stopped first in order to
1325 be able to modify its hardware watchpoints.
1326
1327 Hardware watchpoints must be reset exactly once after being
1328 presented to the user. It cannot be done sooner, because it would
1329 reset the data used to present the watchpoint hit to the user. And
1330 it must not be done later because it could display the same single
1331 watchpoint hit during multiple GDB stops. Note that the latter is
1332 relevant only to the hardware watchpoint types bp_read_watchpoint
1333 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1334 not user-visible - its hit is suppressed if the memory content has
1335 not changed.
1336
1337 The following constraints influence the location where we can reset
1338 hardware watchpoints:
1339
1340 * target_stopped_by_watchpoint and target_stopped_data_address are
1341 called several times when GDB stops.
1342
1343 [linux]
1344 * Multiple hardware watchpoints can be hit at the same time,
1345 causing GDB to stop. GDB only presents one hardware watchpoint
1346 hit at a time as the reason for stopping, and all the other hits
1347 are presented later, one after the other, each time the user
1348 requests the execution to be resumed. Execution is not resumed
1349 for the threads still having pending hit event stored in
1350 LWP_INFO->STATUS. While the watchpoint is already removed from
1351 the inferior on the first stop the thread hit event is kept being
1352 reported from its cached value by linux_nat_stopped_data_address
1353 until the real thread resume happens after the watchpoint gets
1354 presented and thus its LWP_INFO->STATUS gets reset.
1355
1356 Therefore the hardware watchpoint hit can get safely reset on the
1357 watchpoint removal from inferior. */
a79d3c27 1358
b40ce68a 1359static void
a5606eee 1360update_watchpoint (struct breakpoint *b, int reparse)
7270d8f2 1361{
a5606eee 1362 int within_current_scope;
a5606eee 1363 struct frame_id saved_frame_id;
66076460 1364 int frame_saved;
a5606eee 1365
f6bc2008
PA
1366 /* If this is a local watchpoint, we only want to check if the
1367 watchpoint frame is in scope if the current thread is the thread
1368 that was used to create the watchpoint. */
1369 if (!watchpoint_in_thread_scope (b))
1370 return;
1371
a5606eee
VP
1372 if (b->disposition == disp_del_at_next_stop)
1373 return;
1374
66076460 1375 frame_saved = 0;
a5606eee
VP
1376
1377 /* Determine if the watchpoint is within scope. */
1378 if (b->exp_valid_block == NULL)
1379 within_current_scope = 1;
1380 else
1381 {
b5db5dfc
UW
1382 struct frame_info *fi = get_current_frame ();
1383 struct gdbarch *frame_arch = get_frame_arch (fi);
1384 CORE_ADDR frame_pc = get_frame_pc (fi);
1385
1386 /* If we're in a function epilogue, unwinding may not work
1387 properly, so do not attempt to recreate locations at this
1388 point. See similar comments in watchpoint_check. */
1389 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1390 return;
66076460
DJ
1391
1392 /* Save the current frame's ID so we can restore it after
1393 evaluating the watchpoint expression on its own frame. */
1394 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1395 took a frame parameter, so that we didn't have to change the
1396 selected frame. */
1397 frame_saved = 1;
1398 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1399
a5606eee
VP
1400 fi = frame_find_by_id (b->watchpoint_frame);
1401 within_current_scope = (fi != NULL);
1402 if (within_current_scope)
1403 select_frame (fi);
1404 }
1405
b5db5dfc
UW
1406 /* We don't free locations. They are stored in the bp_location array
1407 and update_global_location_list will eventually delete them and
1408 remove breakpoints if needed. */
1409 b->loc = NULL;
1410
a5606eee
VP
1411 if (within_current_scope && reparse)
1412 {
1413 char *s;
d63d0675 1414
a5606eee
VP
1415 if (b->exp)
1416 {
1417 xfree (b->exp);
1418 b->exp = NULL;
1419 }
d63d0675 1420 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1421 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1422 /* If the meaning of expression itself changed, the old value is
1423 no longer relevant. We don't want to report a watchpoint hit
1424 to the user when the old value and the new value may actually
1425 be completely different objects. */
1426 value_free (b->val);
fa4727a6
DJ
1427 b->val = NULL;
1428 b->val_valid = 0;
60e1c644
PA
1429
1430 /* Note that unlike with breakpoints, the watchpoint's condition
1431 expression is stored in the breakpoint object, not in the
1432 locations (re)created below. */
1433 if (b->cond_string != NULL)
1434 {
1435 if (b->cond_exp != NULL)
1436 {
1437 xfree (b->cond_exp);
1438 b->cond_exp = NULL;
1439 }
1440
1441 s = b->cond_string;
1442 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1443 }
a5606eee 1444 }
a5606eee
VP
1445
1446 /* If we failed to parse the expression, for example because
1447 it refers to a global variable in a not-yet-loaded shared library,
1448 don't try to insert watchpoint. We don't automatically delete
1449 such watchpoint, though, since failure to parse expression
1450 is different from out-of-scope watchpoint. */
2d134ed3
PA
1451 if ( !target_has_execution)
1452 {
1453 /* Without execution, memory can't change. No use to try and
1454 set watchpoint locations. The watchpoint will be reset when
1455 the target gains execution, through breakpoint_re_set. */
1456 }
1457 else if (within_current_scope && b->exp)
a5606eee 1458 {
0cf6dd15 1459 int pc = 0;
fa4727a6 1460 struct value *val_chain, *v, *result, *next;
2d134ed3 1461 struct program_space *frame_pspace;
a5606eee 1462
0cf6dd15 1463 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1464
a5606eee
VP
1465 /* Avoid setting b->val if it's already set. The meaning of
1466 b->val is 'the last value' user saw, and we should update
1467 it only if we reported that last value to user. As it
1468 happens, the code that reports it updates b->val directly. */
fa4727a6
DJ
1469 if (!b->val_valid)
1470 {
1471 b->val = v;
1472 b->val_valid = 1;
1473 }
a5606eee 1474
2d134ed3
PA
1475 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1476
a5606eee 1477 /* Look at each value on the value chain. */
9fa40276 1478 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1479 {
1480 /* If it's a memory location, and GDB actually needed
1481 its contents to evaluate the expression, then we
fa4727a6
DJ
1482 must watch it. If the first value returned is
1483 still lazy, that means an error occurred reading it;
1484 watch it anyway in case it becomes readable. */
a5606eee 1485 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1486 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1487 {
1488 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1489
a5606eee
VP
1490 /* We only watch structs and arrays if user asked
1491 for it explicitly, never if they just happen to
1492 appear in the middle of some value chain. */
fa4727a6 1493 if (v == result
a5606eee
VP
1494 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1495 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1496 {
1497 CORE_ADDR addr;
1498 int len, type;
1499 struct bp_location *loc, **tmp;
1500
42ae5230 1501 addr = value_address (v);
a5606eee
VP
1502 len = TYPE_LENGTH (value_type (v));
1503 type = hw_write;
1504 if (b->type == bp_read_watchpoint)
1505 type = hw_read;
1506 else if (b->type == bp_access_watchpoint)
1507 type = hw_access;
1508
39d61571 1509 loc = allocate_bp_location (b);
a5606eee
VP
1510 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1511 ;
1512 *tmp = loc;
a6d9a66e 1513 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1514
1515 loc->pspace = frame_pspace;
a5606eee
VP
1516 loc->address = addr;
1517 loc->length = len;
1518 loc->watchpoint_type = type;
1519 }
1520 }
9fa40276
TJB
1521 }
1522
1523 /* Change the type of breakpoint between hardware assisted or
1524 an ordinary watchpoint depending on the hardware support
1525 and free hardware slots. REPARSE is set when the inferior
1526 is started. */
1527 if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
1528 && reparse)
1529 {
e09342b5 1530 int reg_cnt;
9fa40276
TJB
1531 enum bp_loc_type loc_type;
1532 struct bp_location *bl;
a5606eee 1533
e09342b5
TJB
1534 reg_cnt = can_use_hardware_watchpoint (val_chain, b->exact);
1535
1536 if (reg_cnt)
9fa40276
TJB
1537 {
1538 int i, target_resources_ok, other_type_used;
e09342b5 1539 enum enable_state orig_enable_state;
9fa40276
TJB
1540
1541 /* We need to determine how many resources are already
e09342b5
TJB
1542 used for all other hardware watchpoints plus this one
1543 to see if we still have enough resources to also fit
1544 this watchpoint in as well. To guarantee the
1545 hw_watchpoint_used_count call below counts this
1546 watchpoint, make sure that it is marked as a hardware
1547 watchpoint. */
1548 b->type = bp_hardware_watchpoint;
1549
1550 /* hw_watchpoint_used_count ignores disabled watchpoints,
1551 and b might be disabled if we're being called from
1552 do_enable_breakpoint. */
1553 orig_enable_state = b->enable_state;
1554 b->enable_state = bp_enabled;
1555
9fa40276
TJB
1556 i = hw_watchpoint_used_count (bp_hardware_watchpoint,
1557 &other_type_used);
9fa40276 1558
e09342b5
TJB
1559 b->enable_state = orig_enable_state;
1560
1561 target_resources_ok = target_can_use_hardware_watchpoint
1562 (bp_hardware_watchpoint, i, other_type_used);
1563 if (target_resources_ok <= 0)
1564 b->type = bp_watchpoint;
9fa40276
TJB
1565 }
1566 else
1567 b->type = bp_watchpoint;
1568
1569 loc_type = (b->type == bp_watchpoint? bp_loc_other
1570 : bp_loc_hardware_watchpoint);
1571 for (bl = b->loc; bl; bl = bl->next)
1572 bl->loc_type = loc_type;
1573 }
1574
1575 for (v = val_chain; v; v = next)
1576 {
a5606eee
VP
1577 next = value_next (v);
1578 if (v != b->val)
1579 value_free (v);
1580 }
1581
c7437ca6
PA
1582 /* If a software watchpoint is not watching any memory, then the
1583 above left it without any location set up. But,
1584 bpstat_stop_status requires a location to be able to report
1585 stops, so make sure there's at least a dummy one. */
1586 if (b->type == bp_watchpoint && b->loc == NULL)
1587 {
1588 b->loc = allocate_bp_location (b);
1589 b->loc->pspace = frame_pspace;
1590 b->loc->address = -1;
1591 b->loc->length = -1;
1592 b->loc->watchpoint_type = -1;
1593 }
a5606eee
VP
1594 }
1595 else if (!within_current_scope)
7270d8f2 1596 {
ac74f770
MS
1597 printf_filtered (_("\
1598Watchpoint %d deleted because the program has left the block\n\
1599in which its expression is valid.\n"),
a5606eee
VP
1600 b->number);
1601 if (b->related_breakpoint)
60e1c644
PA
1602 {
1603 b->related_breakpoint->disposition = disp_del_at_next_stop;
1604 b->related_breakpoint->related_breakpoint = NULL;
1605 b->related_breakpoint= NULL;
1606 }
a5606eee 1607 b->disposition = disp_del_at_next_stop;
7270d8f2 1608 }
a5606eee
VP
1609
1610 /* Restore the selected frame. */
66076460
DJ
1611 if (frame_saved)
1612 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1613}
1614
a5606eee 1615
74960c60
VP
1616/* Returns 1 iff breakpoint location should be
1617 inserted in the inferior. */
1618static int
35df4500 1619should_be_inserted (struct bp_location *bl)
74960c60 1620{
35df4500 1621 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1622 return 0;
1623
35df4500 1624 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1625 return 0;
1626
35df4500 1627 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1628 return 0;
1629
56710373
PA
1630 /* This is set for example, when we're attached to the parent of a
1631 vfork, and have detached from the child. The child is running
1632 free, and we expect it to do an exec or exit, at which point the
1633 OS makes the parent schedulable again (and the target reports
1634 that the vfork is done). Until the child is done with the shared
1635 memory region, do not insert breakpoints in the parent, otherwise
1636 the child could still trip on the parent's breakpoints. Since
1637 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1638 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1639 return 0;
1640
1042e4c0
SS
1641 /* Tracepoints are inserted by the target at a time of its choosing,
1642 not by us. */
35df4500 1643 if (is_tracepoint (bl->owner))
1042e4c0
SS
1644 return 0;
1645
74960c60
VP
1646 return 1;
1647}
1648
35df4500
TJB
1649/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1650 location. Any error messages are printed to TMP_ERROR_STREAM; and
1651 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
879bfdc2 1652
4a64f543
MS
1653 NOTE drow/2003-09-09: This routine could be broken down to an
1654 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1655static int
35df4500 1656insert_bp_location (struct bp_location *bl,
26bb91f3 1657 struct ui_file *tmp_error_stream,
fa3a767f 1658 int *disabled_breaks,
26bb91f3 1659 int *hw_breakpoint_error)
879bfdc2
DJ
1660{
1661 int val = 0;
1662
35df4500 1663 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1664 return 0;
1665
8181d85f 1666 /* Initialize the target-specific information. */
35df4500
TJB
1667 memset (&bl->target_info, 0, sizeof (bl->target_info));
1668 bl->target_info.placed_address = bl->address;
1669 bl->target_info.placed_address_space = bl->pspace->aspace;
8181d85f 1670
35df4500
TJB
1671 if (bl->loc_type == bp_loc_software_breakpoint
1672 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1673 {
35df4500 1674 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1675 {
1676 /* If the explicitly specified breakpoint type
1677 is not hardware breakpoint, check the memory map to see
1678 if the breakpoint address is in read only memory or not.
4a64f543 1679
765dc015
VP
1680 Two important cases are:
1681 - location type is not hardware breakpoint, memory
1682 is readonly. We change the type of the location to
1683 hardware breakpoint.
4a64f543
MS
1684 - location type is hardware breakpoint, memory is
1685 read-write. This means we've previously made the
1686 location hardware one, but then the memory map changed,
1687 so we undo.
765dc015 1688
4a64f543
MS
1689 When breakpoints are removed, remove_breakpoints will use
1690 location types we've just set here, the only possible
1691 problem is that memory map has changed during running
1692 program, but it's not going to work anyway with current
1693 gdb. */
765dc015 1694 struct mem_region *mr
35df4500 1695 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1696
1697 if (mr)
1698 {
1699 if (automatic_hardware_breakpoints)
1700 {
765dc015
VP
1701 enum bp_loc_type new_type;
1702
1703 if (mr->attrib.mode != MEM_RW)
1704 new_type = bp_loc_hardware_breakpoint;
1705 else
1706 new_type = bp_loc_software_breakpoint;
1707
35df4500 1708 if (new_type != bl->loc_type)
765dc015
VP
1709 {
1710 static int said = 0;
cc59ec59 1711
35df4500 1712 bl->loc_type = new_type;
765dc015
VP
1713 if (!said)
1714 {
3e43a32a
MS
1715 fprintf_filtered (gdb_stdout,
1716 _("Note: automatically using "
1717 "hardware breakpoints for "
1718 "read-only addresses.\n"));
765dc015
VP
1719 said = 1;
1720 }
1721 }
1722 }
35df4500 1723 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1724 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1725 warning (_("cannot set software breakpoint "
1726 "at readonly address %s"),
35df4500 1727 paddress (bl->gdbarch, bl->address));
765dc015
VP
1728 }
1729 }
1730
879bfdc2
DJ
1731 /* First check to see if we have to handle an overlay. */
1732 if (overlay_debugging == ovly_off
35df4500
TJB
1733 || bl->section == NULL
1734 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1735 {
1736 /* No overlay handling: just set the breakpoint. */
1737
35df4500
TJB
1738 if (bl->loc_type == bp_loc_hardware_breakpoint)
1739 val = target_insert_hw_breakpoint (bl->gdbarch,
1740 &bl->target_info);
879bfdc2 1741 else
35df4500
TJB
1742 val = target_insert_breakpoint (bl->gdbarch,
1743 &bl->target_info);
879bfdc2
DJ
1744 }
1745 else
1746 {
4a64f543 1747 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1748 Shall we set a breakpoint at the LMA? */
1749 if (!overlay_events_enabled)
1750 {
1751 /* Yes -- overlay event support is not active,
1752 so we must try to set a breakpoint at the LMA.
1753 This will not work for a hardware breakpoint. */
35df4500 1754 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1755 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1756 bl->owner->number);
879bfdc2
DJ
1757 else
1758 {
35df4500
TJB
1759 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1760 bl->section);
879bfdc2 1761 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1762 bl->overlay_target_info = bl->target_info;
1763 bl->overlay_target_info.placed_address = addr;
1764 val = target_insert_breakpoint (bl->gdbarch,
1765 &bl->overlay_target_info);
879bfdc2 1766 if (val != 0)
99361f52 1767 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1768 "Overlay breakpoint %d "
1769 "failed: in ROM?\n",
35df4500 1770 bl->owner->number);
879bfdc2
DJ
1771 }
1772 }
1773 /* Shall we set a breakpoint at the VMA? */
35df4500 1774 if (section_is_mapped (bl->section))
879bfdc2
DJ
1775 {
1776 /* Yes. This overlay section is mapped into memory. */
35df4500
TJB
1777 if (bl->loc_type == bp_loc_hardware_breakpoint)
1778 val = target_insert_hw_breakpoint (bl->gdbarch,
1779 &bl->target_info);
879bfdc2 1780 else
35df4500
TJB
1781 val = target_insert_breakpoint (bl->gdbarch,
1782 &bl->target_info);
879bfdc2
DJ
1783 }
1784 else
1785 {
1786 /* No. This breakpoint will not be inserted.
1787 No error, but do not mark the bp as 'inserted'. */
1788 return 0;
1789 }
1790 }
1791
1792 if (val)
1793 {
1794 /* Can't set the breakpoint. */
35df4500 1795 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1796 {
4a64f543 1797 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1798 val = 0;
35df4500 1799 bl->shlib_disabled = 1;
879bfdc2
DJ
1800 if (!*disabled_breaks)
1801 {
1802 fprintf_unfiltered (tmp_error_stream,
1803 "Cannot insert breakpoint %d.\n",
35df4500 1804 bl->owner->number);
879bfdc2 1805 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1806 "Temporarily disabling shared "
1807 "library breakpoints:\n");
879bfdc2
DJ
1808 }
1809 *disabled_breaks = 1;
1810 fprintf_unfiltered (tmp_error_stream,
35df4500 1811 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1812 }
1813 else
879bfdc2 1814 {
35df4500 1815 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1816 {
1817 *hw_breakpoint_error = 1;
3e43a32a
MS
1818 fprintf_unfiltered (tmp_error_stream,
1819 "Cannot insert hardware "
1820 "breakpoint %d.\n",
35df4500 1821 bl->owner->number);
879bfdc2
DJ
1822 }
1823 else
1824 {
1825 fprintf_unfiltered (tmp_error_stream,
1826 "Cannot insert breakpoint %d.\n",
35df4500 1827 bl->owner->number);
879bfdc2
DJ
1828 fprintf_filtered (tmp_error_stream,
1829 "Error accessing memory address ");
35df4500 1830 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1831 tmp_error_stream);
879bfdc2
DJ
1832 fprintf_filtered (tmp_error_stream, ": %s.\n",
1833 safe_strerror (val));
1834 }
1835
1836 }
1837 }
1838 else
35df4500 1839 bl->inserted = 1;
879bfdc2
DJ
1840
1841 return val;
1842 }
1843
35df4500 1844 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1845 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1846 watchpoints. It's not clear that it's necessary... */
35df4500 1847 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1848 {
77b06cd7
TJB
1849 gdb_assert (bl->owner->ops != NULL
1850 && bl->owner->ops->insert_location != NULL);
1851
1852 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1853
1854 /* If trying to set a read-watchpoint, and it turns out it's not
1855 supported, try emulating one with an access watchpoint. */
35df4500 1856 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1857 {
1858 struct bp_location *loc, **loc_temp;
1859
1860 /* But don't try to insert it, if there's already another
1861 hw_access location that would be considered a duplicate
1862 of this one. */
1863 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1864 if (loc != bl
85d721b8 1865 && loc->watchpoint_type == hw_access
35df4500 1866 && watchpoint_locations_match (bl, loc))
85d721b8 1867 {
35df4500
TJB
1868 bl->duplicate = 1;
1869 bl->inserted = 1;
1870 bl->target_info = loc->target_info;
1871 bl->watchpoint_type = hw_access;
85d721b8
PA
1872 val = 0;
1873 break;
1874 }
1875
1876 if (val == 1)
1877 {
77b06cd7
TJB
1878 bl->watchpoint_type = hw_access;
1879 val = bl->owner->ops->insert_location (bl);
1880
1881 if (val)
1882 /* Back to the original value. */
1883 bl->watchpoint_type = hw_read;
85d721b8
PA
1884 }
1885 }
1886
35df4500 1887 bl->inserted = (val == 0);
879bfdc2
DJ
1888 }
1889
35df4500 1890 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1891 {
77b06cd7
TJB
1892 gdb_assert (bl->owner->ops != NULL
1893 && bl->owner->ops->insert_location != NULL);
1894
1895 val = bl->owner->ops->insert_location (bl);
1896 if (val)
1897 {
1898 bl->owner->enable_state = bp_disabled;
1899
1900 if (val == 1)
1901 warning (_("\
1902Error inserting catchpoint %d: Your system does not support this type\n\
1903of catchpoint."), bl->owner->number);
1904 else
1905 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1906 }
1907
1908 bl->inserted = (val == 0);
1640b821
DJ
1909
1910 /* We've already printed an error message if there was a problem
1911 inserting this catchpoint, and we've disabled the catchpoint,
1912 so just return success. */
1913 return 0;
879bfdc2
DJ
1914 }
1915
1916 return 0;
1917}
1918
6c95b8df
PA
1919/* This function is called when program space PSPACE is about to be
1920 deleted. It takes care of updating breakpoints to not reference
1921 PSPACE anymore. */
1922
1923void
1924breakpoint_program_space_exit (struct program_space *pspace)
1925{
1926 struct breakpoint *b, *b_temp;
876fa593 1927 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1928
1929 /* Remove any breakpoint that was set through this program space. */
1930 ALL_BREAKPOINTS_SAFE (b, b_temp)
1931 {
1932 if (b->pspace == pspace)
1933 delete_breakpoint (b);
1934 }
1935
1936 /* Breakpoints set through other program spaces could have locations
1937 bound to PSPACE as well. Remove those. */
876fa593 1938 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1939 {
1940 struct bp_location *tmp;
1941
1942 if (loc->pspace == pspace)
1943 {
2bdf28a0 1944 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1945 if (loc->owner->loc == loc)
1946 loc->owner->loc = loc->next;
1947 else
1948 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1949 if (tmp->next == loc)
1950 {
1951 tmp->next = loc->next;
1952 break;
1953 }
1954 }
1955 }
1956
1957 /* Now update the global location list to permanently delete the
1958 removed locations above. */
1959 update_global_location_list (0);
1960}
1961
74960c60
VP
1962/* Make sure all breakpoints are inserted in inferior.
1963 Throws exception on any error.
1964 A breakpoint that is already inserted won't be inserted
1965 again, so calling this function twice is safe. */
1966void
1967insert_breakpoints (void)
1968{
1969 struct breakpoint *bpt;
1970
1971 ALL_BREAKPOINTS (bpt)
1972 if (is_hardware_watchpoint (bpt))
4a64f543 1973 update_watchpoint (bpt, 0 /* don't reparse. */);
74960c60 1974
b60e7edf 1975 update_global_location_list (1);
74960c60 1976
c35b1492
PA
1977 /* update_global_location_list does not insert breakpoints when
1978 always_inserted_mode is not enabled. Explicitly insert them
1979 now. */
1980 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1981 insert_breakpoint_locations ();
1982}
1983
c906108c
SS
1984/* insert_breakpoints is used when starting or continuing the program.
1985 remove_breakpoints is used when the program stops.
1986 Both return zero if successful,
1987 or an `errno' value if could not write the inferior. */
1988
74960c60
VP
1989static void
1990insert_breakpoint_locations (void)
c906108c 1991{
a5606eee 1992 struct breakpoint *bpt;
35df4500 1993 struct bp_location *bl, **blp_tmp;
e236ba44 1994 int error = 0;
c906108c
SS
1995 int val = 0;
1996 int disabled_breaks = 0;
81d0cc19 1997 int hw_breakpoint_error = 0;
c906108c 1998
81d0cc19 1999 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2000 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2001
81d0cc19
GS
2002 /* Explicitly mark the warning -- this will only be printed if
2003 there was an error. */
2004 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2005
2006 save_current_space_and_thread ();
2007
35df4500 2008 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2009 {
35df4500 2010 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
2011 continue;
2012
4a64f543
MS
2013 /* There is no point inserting thread-specific breakpoints if
2014 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2015 has BL->OWNER always non-NULL. */
35df4500
TJB
2016 if (bl->owner->thread != -1
2017 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2018 continue;
2019
35df4500 2020 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2021
2022 /* For targets that support global breakpoints, there's no need
2023 to select an inferior to insert breakpoint to. In fact, even
2024 if we aren't attached to any process yet, we should still
2025 insert breakpoints. */
2026 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2027 && ptid_equal (inferior_ptid, null_ptid))
2028 continue;
2029
35df4500 2030 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
879bfdc2
DJ
2031 &hw_breakpoint_error);
2032 if (val)
e236ba44 2033 error = val;
879bfdc2 2034 }
c906108c 2035
4a64f543
MS
2036 /* If we failed to insert all locations of a watchpoint, remove
2037 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2038 ALL_BREAKPOINTS (bpt)
2039 {
2040 int some_failed = 0;
2041 struct bp_location *loc;
2042
2043 if (!is_hardware_watchpoint (bpt))
2044 continue;
2045
d6b74ac4 2046 if (!breakpoint_enabled (bpt))
a5606eee 2047 continue;
74960c60
VP
2048
2049 if (bpt->disposition == disp_del_at_next_stop)
2050 continue;
a5606eee
VP
2051
2052 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2053 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2054 {
2055 some_failed = 1;
2056 break;
2057 }
2058 if (some_failed)
2059 {
2060 for (loc = bpt->loc; loc; loc = loc->next)
2061 if (loc->inserted)
2062 remove_breakpoint (loc, mark_uninserted);
2063
2064 hw_breakpoint_error = 1;
2065 fprintf_unfiltered (tmp_error_stream,
2066 "Could not insert hardware watchpoint %d.\n",
2067 bpt->number);
2068 error = -1;
2069 }
2070 }
2071
e236ba44 2072 if (error)
81d0cc19
GS
2073 {
2074 /* If a hardware breakpoint or watchpoint was inserted, add a
2075 message about possibly exhausted resources. */
879bfdc2 2076 if (hw_breakpoint_error)
81d0cc19 2077 {
c6510018
MS
2078 fprintf_unfiltered (tmp_error_stream,
2079 "Could not insert hardware breakpoints:\n\
2080You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2081 }
81d0cc19
GS
2082 target_terminal_ours_for_output ();
2083 error_stream (tmp_error_stream);
2084 }
f7545552
TT
2085
2086 do_cleanups (cleanups);
c906108c
SS
2087}
2088
c906108c 2089int
fba45db2 2090remove_breakpoints (void)
c906108c 2091{
35df4500 2092 struct bp_location *bl, **blp_tmp;
3a1bae8e 2093 int val = 0;
c906108c 2094
35df4500 2095 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2096 {
35df4500
TJB
2097 if (bl->inserted)
2098 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2099 }
3a1bae8e 2100 return val;
c906108c
SS
2101}
2102
6c95b8df
PA
2103/* Remove breakpoints of process PID. */
2104
2105int
2106remove_breakpoints_pid (int pid)
2107{
35df4500 2108 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2109 int val;
2110 struct inferior *inf = find_inferior_pid (pid);
2111
35df4500 2112 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2113 {
35df4500 2114 if (bl->pspace != inf->pspace)
6c95b8df
PA
2115 continue;
2116
35df4500 2117 if (bl->inserted)
6c95b8df 2118 {
35df4500 2119 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2120 if (val != 0)
2121 return val;
2122 }
2123 }
2124 return 0;
2125}
2126
692590c1 2127int
80ce1ecb 2128remove_hw_watchpoints (void)
692590c1 2129{
35df4500 2130 struct bp_location *bl, **blp_tmp;
3a1bae8e 2131 int val = 0;
692590c1 2132
35df4500 2133 ALL_BP_LOCATIONS (bl, blp_tmp)
692590c1 2134 {
35df4500
TJB
2135 if (bl->inserted && bl->loc_type == bp_loc_hardware_watchpoint)
2136 val |= remove_breakpoint (bl, mark_uninserted);
692590c1 2137 }
3a1bae8e 2138 return val;
692590c1
MS
2139}
2140
c906108c 2141int
fba45db2 2142reattach_breakpoints (int pid)
c906108c 2143{
6c95b8df 2144 struct cleanup *old_chain;
35df4500 2145 struct bp_location *bl, **blp_tmp;
c906108c 2146 int val;
a4954f26 2147 struct ui_file *tmp_error_stream = mem_fileopen ();
fa3a767f 2148 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2149 struct inferior *inf;
2150 struct thread_info *tp;
2151
2152 tp = any_live_thread_of_process (pid);
2153 if (tp == NULL)
2154 return 1;
2155
2156 inf = find_inferior_pid (pid);
2157 old_chain = save_inferior_ptid ();
2158
2159 inferior_ptid = tp->ptid;
a4954f26
DJ
2160
2161 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2162
35df4500 2163 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2164 {
35df4500 2165 if (bl->pspace != inf->pspace)
6c95b8df
PA
2166 continue;
2167
35df4500 2168 if (bl->inserted)
c5aa993b 2169 {
35df4500
TJB
2170 bl->inserted = 0;
2171 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2172 if (val != 0)
2173 {
ce696e05 2174 do_cleanups (old_chain);
c5aa993b
JM
2175 return val;
2176 }
2177 }
2178 }
ce696e05 2179 do_cleanups (old_chain);
c906108c
SS
2180 return 0;
2181}
2182
e58b0e63
PA
2183static int internal_breakpoint_number = -1;
2184
84f4c1fe
PM
2185/* Set the breakpoint number of B, depending on the value of INTERNAL.
2186 If INTERNAL is non-zero, the breakpoint number will be populated
2187 from internal_breakpoint_number and that variable decremented.
2188 Otherwis the breakpoint number will be populated from
2189 breakpoint_count and that value incremented. Internal breakpoints
2190 do not set the internal var bpnum. */
2191static void
2192set_breakpoint_number (int internal, struct breakpoint *b)
2193{
2194 if (internal)
2195 b->number = internal_breakpoint_number--;
2196 else
2197 {
2198 set_breakpoint_count (breakpoint_count + 1);
2199 b->number = breakpoint_count;
2200 }
2201}
2202
e62c965a 2203static struct breakpoint *
a6d9a66e
UW
2204create_internal_breakpoint (struct gdbarch *gdbarch,
2205 CORE_ADDR address, enum bptype type)
e62c965a 2206{
e62c965a
PP
2207 struct symtab_and_line sal;
2208 struct breakpoint *b;
2209
4a64f543 2210 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2211
2212 sal.pc = address;
2213 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2214 sal.pspace = current_program_space;
e62c965a 2215
a6d9a66e 2216 b = set_raw_breakpoint (gdbarch, sal, type);
e62c965a
PP
2217 b->number = internal_breakpoint_number--;
2218 b->disposition = disp_donttouch;
2219
2220 return b;
2221}
2222
17450429
PP
2223static const char *const longjmp_names[] =
2224 {
2225 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2226 };
2227#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2228
2229/* Per-objfile data private to breakpoint.c. */
2230struct breakpoint_objfile_data
2231{
2232 /* Minimal symbol for "_ovly_debug_event" (if any). */
2233 struct minimal_symbol *overlay_msym;
2234
2235 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2236 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2237
2238 /* Minimal symbol for "std::terminate()" (if any). */
2239 struct minimal_symbol *terminate_msym;
2240
2241 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2242 struct minimal_symbol *exception_msym;
2243};
2244
2245static const struct objfile_data *breakpoint_objfile_key;
2246
2247/* Minimal symbol not found sentinel. */
2248static struct minimal_symbol msym_not_found;
2249
2250/* Returns TRUE if MSYM point to the "not found" sentinel. */
2251
2252static int
2253msym_not_found_p (const struct minimal_symbol *msym)
2254{
2255 return msym == &msym_not_found;
2256}
2257
2258/* Return per-objfile data needed by breakpoint.c.
2259 Allocate the data if necessary. */
2260
2261static struct breakpoint_objfile_data *
2262get_breakpoint_objfile_data (struct objfile *objfile)
2263{
2264 struct breakpoint_objfile_data *bp_objfile_data;
2265
2266 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2267 if (bp_objfile_data == NULL)
2268 {
2269 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2270 sizeof (*bp_objfile_data));
2271
2272 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2273 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2274 }
2275 return bp_objfile_data;
2276}
2277
e62c965a 2278static void
af02033e 2279create_overlay_event_breakpoint (void)
e62c965a 2280{
69de3c6a 2281 struct objfile *objfile;
af02033e 2282 const char *const func_name = "_ovly_debug_event";
e62c965a 2283
69de3c6a
PP
2284 ALL_OBJFILES (objfile)
2285 {
2286 struct breakpoint *b;
17450429
PP
2287 struct breakpoint_objfile_data *bp_objfile_data;
2288 CORE_ADDR addr;
69de3c6a 2289
17450429
PP
2290 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2291
2292 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2293 continue;
2294
2295 if (bp_objfile_data->overlay_msym == NULL)
2296 {
2297 struct minimal_symbol *m;
2298
2299 m = lookup_minimal_symbol_text (func_name, objfile);
2300 if (m == NULL)
2301 {
2302 /* Avoid future lookups in this objfile. */
2303 bp_objfile_data->overlay_msym = &msym_not_found;
2304 continue;
2305 }
2306 bp_objfile_data->overlay_msym = m;
2307 }
e62c965a 2308
17450429
PP
2309 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2310 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
69de3c6a
PP
2311 bp_overlay_event);
2312 b->addr_string = xstrdup (func_name);
e62c965a 2313
69de3c6a
PP
2314 if (overlay_debugging == ovly_auto)
2315 {
2316 b->enable_state = bp_enabled;
2317 overlay_events_enabled = 1;
2318 }
2319 else
2320 {
2321 b->enable_state = bp_disabled;
2322 overlay_events_enabled = 0;
2323 }
e62c965a
PP
2324 }
2325 update_global_location_list (1);
2326}
2327
0fd8e87f 2328static void
af02033e 2329create_longjmp_master_breakpoint (void)
0fd8e87f 2330{
6c95b8df 2331 struct program_space *pspace;
6c95b8df
PA
2332 struct cleanup *old_chain;
2333
2334 old_chain = save_current_program_space ();
0fd8e87f 2335
6c95b8df 2336 ALL_PSPACES (pspace)
af02033e
PP
2337 {
2338 struct objfile *objfile;
2339
2340 set_current_program_space (pspace);
2341
2342 ALL_OBJFILES (objfile)
0fd8e87f 2343 {
af02033e
PP
2344 int i;
2345 struct gdbarch *gdbarch;
17450429 2346 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2347
af02033e
PP
2348 gdbarch = get_objfile_arch (objfile);
2349 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2350 continue;
2351
17450429
PP
2352 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2353
2354 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2355 {
2356 struct breakpoint *b;
af02033e 2357 const char *func_name;
17450429 2358 CORE_ADDR addr;
6c95b8df 2359
17450429 2360 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2361 continue;
0fd8e87f 2362
17450429
PP
2363 func_name = longjmp_names[i];
2364 if (bp_objfile_data->longjmp_msym[i] == NULL)
2365 {
2366 struct minimal_symbol *m;
2367
2368 m = lookup_minimal_symbol_text (func_name, objfile);
2369 if (m == NULL)
2370 {
2371 /* Prevent future lookups in this objfile. */
2372 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2373 continue;
2374 }
2375 bp_objfile_data->longjmp_msym[i] = m;
2376 }
2377
2378 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
2379 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
af02033e
PP
2380 b->addr_string = xstrdup (func_name);
2381 b->enable_state = bp_disabled;
2382 }
0fd8e87f 2383 }
af02033e 2384 }
0fd8e87f 2385 update_global_location_list (1);
6c95b8df
PA
2386
2387 do_cleanups (old_chain);
0fd8e87f
UW
2388}
2389
af02033e 2390/* Create a master std::terminate breakpoint. */
aa7d318d 2391static void
af02033e 2392create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2393{
2394 struct program_space *pspace;
aa7d318d 2395 struct cleanup *old_chain;
af02033e 2396 const char *const func_name = "std::terminate()";
aa7d318d
TT
2397
2398 old_chain = save_current_program_space ();
2399
2400 ALL_PSPACES (pspace)
17450429
PP
2401 {
2402 struct objfile *objfile;
2403 CORE_ADDR addr;
2404
2405 set_current_program_space (pspace);
2406
aa7d318d
TT
2407 ALL_OBJFILES (objfile)
2408 {
2409 struct breakpoint *b;
17450429 2410 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2411
17450429 2412 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2413
17450429
PP
2414 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2415 continue;
2416
2417 if (bp_objfile_data->terminate_msym == NULL)
2418 {
2419 struct minimal_symbol *m;
2420
2421 m = lookup_minimal_symbol (func_name, NULL, objfile);
2422 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2423 && MSYMBOL_TYPE (m) != mst_file_text))
2424 {
2425 /* Prevent future lookups in this objfile. */
2426 bp_objfile_data->terminate_msym = &msym_not_found;
2427 continue;
2428 }
2429 bp_objfile_data->terminate_msym = m;
2430 }
aa7d318d 2431
17450429
PP
2432 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2433 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
aa7d318d
TT
2434 bp_std_terminate_master);
2435 b->addr_string = xstrdup (func_name);
2436 b->enable_state = bp_disabled;
2437 }
17450429
PP
2438 }
2439
aa7d318d
TT
2440 update_global_location_list (1);
2441
2442 do_cleanups (old_chain);
2443}
2444
186c406b
TT
2445/* Install a master breakpoint on the unwinder's debug hook. */
2446
2447void
2448create_exception_master_breakpoint (void)
2449{
2450 struct objfile *objfile;
17450429 2451 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2452
2453 ALL_OBJFILES (objfile)
2454 {
17450429
PP
2455 struct breakpoint *b;
2456 struct gdbarch *gdbarch;
2457 struct breakpoint_objfile_data *bp_objfile_data;
2458 CORE_ADDR addr;
2459
2460 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2461
2462 if (msym_not_found_p (bp_objfile_data->exception_msym))
2463 continue;
2464
2465 gdbarch = get_objfile_arch (objfile);
186c406b 2466
17450429 2467 if (bp_objfile_data->exception_msym == NULL)
186c406b 2468 {
17450429 2469 struct minimal_symbol *debug_hook;
186c406b 2470
17450429
PP
2471 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2472 if (debug_hook == NULL)
2473 {
2474 bp_objfile_data->exception_msym = &msym_not_found;
2475 continue;
2476 }
2477
2478 bp_objfile_data->exception_msym = debug_hook;
186c406b 2479 }
17450429
PP
2480
2481 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2482 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2483 &current_target);
2484 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
2485 b->addr_string = xstrdup (func_name);
2486 b->enable_state = bp_disabled;
186c406b
TT
2487 }
2488
2489 update_global_location_list (1);
2490}
2491
c906108c 2492void
fba45db2 2493update_breakpoints_after_exec (void)
c906108c 2494{
35df4500 2495 struct breakpoint *b, *b_tmp;
876fa593 2496 struct bp_location *bploc, **bplocp_tmp;
c906108c 2497
25b22b0a
PA
2498 /* We're about to delete breakpoints from GDB's lists. If the
2499 INSERTED flag is true, GDB will try to lift the breakpoints by
2500 writing the breakpoints' "shadow contents" back into memory. The
2501 "shadow contents" are NOT valid after an exec, so GDB should not
2502 do that. Instead, the target is responsible from marking
2503 breakpoints out as soon as it detects an exec. We don't do that
2504 here instead, because there may be other attempts to delete
2505 breakpoints after detecting an exec and before reaching here. */
876fa593 2506 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2507 if (bploc->pspace == current_program_space)
2508 gdb_assert (!bploc->inserted);
c906108c 2509
35df4500 2510 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2511 {
6c95b8df
PA
2512 if (b->pspace != current_program_space)
2513 continue;
2514
4a64f543 2515 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2516 if (b->type == bp_shlib_event)
2517 {
2518 delete_breakpoint (b);
2519 continue;
2520 }
c906108c 2521
4a64f543 2522 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2523 if (b->type == bp_jit_event)
2524 {
2525 delete_breakpoint (b);
2526 continue;
2527 }
2528
1900040c 2529 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2530 as must overlay event and longjmp master breakpoints. */
2531 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2532 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2533 || b->type == bp_exception_master)
c4093a6a
JM
2534 {
2535 delete_breakpoint (b);
2536 continue;
2537 }
2538
4a64f543 2539 /* Step-resume breakpoints are meaningless after an exec(). */
c5aa993b
JM
2540 if (b->type == bp_step_resume)
2541 {
2542 delete_breakpoint (b);
2543 continue;
2544 }
2545
611c83ae
PA
2546 /* Longjmp and longjmp-resume breakpoints are also meaningless
2547 after an exec. */
186c406b
TT
2548 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2549 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2550 {
2551 delete_breakpoint (b);
2552 continue;
2553 }
2554
ce78b96d
JB
2555 if (b->type == bp_catchpoint)
2556 {
2557 /* For now, none of the bp_catchpoint breakpoints need to
2558 do anything at this point. In the future, if some of
2559 the catchpoints need to something, we will need to add
2560 a new method, and call this method from here. */
2561 continue;
2562 }
2563
c5aa993b
JM
2564 /* bp_finish is a special case. The only way we ought to be able
2565 to see one of these when an exec() has happened, is if the user
2566 caught a vfork, and then said "finish". Ordinarily a finish just
2567 carries them to the call-site of the current callee, by setting
2568 a temporary bp there and resuming. But in this case, the finish
2569 will carry them entirely through the vfork & exec.
2570
2571 We don't want to allow a bp_finish to remain inserted now. But
2572 we can't safely delete it, 'cause finish_command has a handle to
2573 the bp on a bpstat, and will later want to delete it. There's a
2574 chance (and I've seen it happen) that if we delete the bp_finish
2575 here, that its storage will get reused by the time finish_command
2576 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2577 We really must allow finish_command to delete a bp_finish.
2578
53a5351d
JM
2579 In the absense of a general solution for the "how do we know
2580 it's safe to delete something others may have handles to?"
2581 problem, what we'll do here is just uninsert the bp_finish, and
2582 let finish_command delete it.
2583
2584 (We know the bp_finish is "doomed" in the sense that it's
2585 momentary, and will be deleted as soon as finish_command sees
2586 the inferior stopped. So it doesn't matter that the bp's
2587 address is probably bogus in the new a.out, unlike e.g., the
2588 solib breakpoints.) */
c5aa993b 2589
c5aa993b
JM
2590 if (b->type == bp_finish)
2591 {
2592 continue;
2593 }
2594
2595 /* Without a symbolic address, we have little hope of the
2596 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2597 a.out. */
c5aa993b
JM
2598 if (b->addr_string == NULL)
2599 {
2600 delete_breakpoint (b);
2601 continue;
2602 }
c5aa993b 2603 }
1900040c 2604 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2605 create_overlay_event_breakpoint ();
2606 create_longjmp_master_breakpoint ();
2607 create_std_terminate_master_breakpoint ();
186c406b 2608 create_exception_master_breakpoint ();
c906108c
SS
2609}
2610
2611int
fba45db2 2612detach_breakpoints (int pid)
c906108c 2613{
35df4500 2614 struct bp_location *bl, **blp_tmp;
3a1bae8e 2615 int val = 0;
ce696e05 2616 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2617 struct inferior *inf = current_inferior ();
c5aa993b 2618
39f77062 2619 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2620 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2621
6c95b8df 2622 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2623 inferior_ptid = pid_to_ptid (pid);
35df4500 2624 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2625 {
35df4500 2626 if (bl->pspace != inf->pspace)
6c95b8df
PA
2627 continue;
2628
35df4500
TJB
2629 if (bl->inserted)
2630 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2631 }
d03285ec
UW
2632
2633 /* Detach single-step breakpoints as well. */
2634 detach_single_step_breakpoints ();
2635
ce696e05 2636 do_cleanups (old_chain);
3a1bae8e 2637 return val;
c906108c
SS
2638}
2639
35df4500 2640/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2641 Note that this is used to detach breakpoints from a child fork.
2642 When we get here, the child isn't in the inferior list, and neither
2643 do we have objects to represent its address space --- we should
35df4500 2644 *not* look at bl->pspace->aspace here. */
6c95b8df 2645
c906108c 2646static int
35df4500 2647remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2648{
2649 int val;
c5aa993b 2650
35df4500
TJB
2651 /* BL is never in moribund_locations by our callers. */
2652 gdb_assert (bl->owner != NULL);
2bdf28a0 2653
35df4500 2654 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2655 /* Permanent breakpoints cannot be inserted or removed. */
2656 return 0;
2657
74960c60
VP
2658 /* The type of none suggests that owner is actually deleted.
2659 This should not ever happen. */
35df4500 2660 gdb_assert (bl->owner->type != bp_none);
0bde7532 2661
35df4500
TJB
2662 if (bl->loc_type == bp_loc_software_breakpoint
2663 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2664 {
c02f5703
MS
2665 /* "Normal" instruction breakpoint: either the standard
2666 trap-instruction bp (bp_breakpoint), or a
2667 bp_hardware_breakpoint. */
2668
2669 /* First check to see if we have to handle an overlay. */
2670 if (overlay_debugging == ovly_off
35df4500
TJB
2671 || bl->section == NULL
2672 || !(section_is_overlay (bl->section)))
c02f5703
MS
2673 {
2674 /* No overlay handling: just remove the breakpoint. */
2675
35df4500
TJB
2676 if (bl->loc_type == bp_loc_hardware_breakpoint)
2677 val = target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2678 else
35df4500 2679 val = target_remove_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2680 }
c906108c
SS
2681 else
2682 {
4a64f543 2683 /* This breakpoint is in an overlay section.
c02f5703
MS
2684 Did we set a breakpoint at the LMA? */
2685 if (!overlay_events_enabled)
2686 {
2687 /* Yes -- overlay event support is not active, so we
2688 should have set a breakpoint at the LMA. Remove it.
2689 */
c02f5703
MS
2690 /* Ignore any failures: if the LMA is in ROM, we will
2691 have already warned when we failed to insert it. */
35df4500
TJB
2692 if (bl->loc_type == bp_loc_hardware_breakpoint)
2693 target_remove_hw_breakpoint (bl->gdbarch,
2694 &bl->overlay_target_info);
c02f5703 2695 else
35df4500
TJB
2696 target_remove_breakpoint (bl->gdbarch,
2697 &bl->overlay_target_info);
c02f5703
MS
2698 }
2699 /* Did we set a breakpoint at the VMA?
2700 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2701 if (bl->inserted)
c906108c 2702 {
c02f5703
MS
2703 /* Yes -- remove it. Previously we did not bother to
2704 remove the breakpoint if the section had been
2705 unmapped, but let's not rely on that being safe. We
2706 don't know what the overlay manager might do. */
35df4500
TJB
2707 if (bl->loc_type == bp_loc_hardware_breakpoint)
2708 val = target_remove_hw_breakpoint (bl->gdbarch,
2709 &bl->target_info);
aa67235e
UW
2710
2711 /* However, we should remove *software* breakpoints only
2712 if the section is still mapped, or else we overwrite
2713 wrong code with the saved shadow contents. */
35df4500
TJB
2714 else if (section_is_mapped (bl->section))
2715 val = target_remove_breakpoint (bl->gdbarch,
2716 &bl->target_info);
aa67235e
UW
2717 else
2718 val = 0;
c906108c 2719 }
c02f5703
MS
2720 else
2721 {
2722 /* No -- not inserted, so no need to remove. No error. */
2723 val = 0;
2724 }
c906108c 2725 }
879d1e6b
UW
2726
2727 /* In some cases, we might not be able to remove a breakpoint
2728 in a shared library that has already been removed, but we
2729 have not yet processed the shlib unload event. */
35df4500 2730 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2731 val = 0;
2732
c906108c
SS
2733 if (val)
2734 return val;
35df4500 2735 bl->inserted = (is == mark_inserted);
c906108c 2736 }
35df4500 2737 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2738 {
77b06cd7
TJB
2739 gdb_assert (bl->owner->ops != NULL
2740 && bl->owner->ops->remove_location != NULL);
2741
35df4500 2742 bl->inserted = (is == mark_inserted);
77b06cd7 2743 bl->owner->ops->remove_location (bl);
2e70b7b9 2744
c906108c 2745 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2746 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2747 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2748 bl->owner->number);
c906108c 2749 }
35df4500
TJB
2750 else if (bl->owner->type == bp_catchpoint
2751 && breakpoint_enabled (bl->owner)
2752 && !bl->duplicate)
ce78b96d 2753 {
77b06cd7
TJB
2754 gdb_assert (bl->owner->ops != NULL
2755 && bl->owner->ops->remove_location != NULL);
ce78b96d 2756
77b06cd7 2757 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2758 if (val)
2759 return val;
77b06cd7 2760
35df4500 2761 bl->inserted = (is == mark_inserted);
ce78b96d 2762 }
c906108c
SS
2763
2764 return 0;
2765}
2766
6c95b8df 2767static int
35df4500 2768remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2769{
2770 int ret;
2771 struct cleanup *old_chain;
2772
35df4500
TJB
2773 /* BL is never in moribund_locations by our callers. */
2774 gdb_assert (bl->owner != NULL);
2bdf28a0 2775
35df4500 2776 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2777 /* Permanent breakpoints cannot be inserted or removed. */
2778 return 0;
2779
2780 /* The type of none suggests that owner is actually deleted.
2781 This should not ever happen. */
35df4500 2782 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2783
2784 old_chain = save_current_space_and_thread ();
2785
35df4500 2786 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2787
35df4500 2788 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2789
2790 do_cleanups (old_chain);
2791 return ret;
2792}
2793
c906108c
SS
2794/* Clear the "inserted" flag in all breakpoints. */
2795
25b22b0a 2796void
fba45db2 2797mark_breakpoints_out (void)
c906108c 2798{
35df4500 2799 struct bp_location *bl, **blp_tmp;
c906108c 2800
35df4500
TJB
2801 ALL_BP_LOCATIONS (bl, blp_tmp)
2802 if (bl->pspace == current_program_space)
2803 bl->inserted = 0;
c906108c
SS
2804}
2805
53a5351d
JM
2806/* Clear the "inserted" flag in all breakpoints and delete any
2807 breakpoints which should go away between runs of the program.
c906108c
SS
2808
2809 Plus other such housekeeping that has to be done for breakpoints
2810 between runs.
2811
53a5351d
JM
2812 Note: this function gets called at the end of a run (by
2813 generic_mourn_inferior) and when a run begins (by
4a64f543 2814 init_wait_for_inferior). */
c906108c
SS
2815
2816
2817
2818void
fba45db2 2819breakpoint_init_inferior (enum inf_context context)
c906108c 2820{
35df4500
TJB
2821 struct breakpoint *b, *b_tmp;
2822 struct bp_location *bl, **blp_tmp;
1c5cfe86 2823 int ix;
6c95b8df 2824 struct program_space *pspace = current_program_space;
c906108c 2825
50c71eaf
PA
2826 /* If breakpoint locations are shared across processes, then there's
2827 nothing to do. */
2567c7d9 2828 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2829 return;
2830
35df4500 2831 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2832 {
35df4500
TJB
2833 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2834 if (bl->pspace == pspace
2835 && bl->owner->enable_state != bp_permanent)
2836 bl->inserted = 0;
6c95b8df 2837 }
075f6582 2838
35df4500 2839 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2840 {
6c95b8df
PA
2841 if (b->loc && b->loc->pspace != pspace)
2842 continue;
2843
c5aa993b
JM
2844 switch (b->type)
2845 {
2846 case bp_call_dummy:
c906108c 2847
c5aa993b 2848 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2849 cause problems when the inferior is rerun, so we better get
2850 rid of it. */
2851
2852 case bp_watchpoint_scope:
2853
2854 /* Also get rid of scope breakpoints. */
2855
2856 case bp_shlib_event:
2857
2858 /* Also remove solib event breakpoints. Their addresses may
2859 have changed since the last time we ran the program.
2860 Actually we may now be debugging against different target;
2861 and so the solib backend that installed this breakpoint may
2862 not be used in by the target. E.g.,
2863
2864 (gdb) file prog-linux
2865 (gdb) run # native linux target
2866 ...
2867 (gdb) kill
2868 (gdb) file prog-win.exe
2869 (gdb) tar rem :9999 # remote Windows gdbserver.
2870 */
c906108c 2871
c5aa993b
JM
2872 delete_breakpoint (b);
2873 break;
c906108c 2874
c5aa993b
JM
2875 case bp_watchpoint:
2876 case bp_hardware_watchpoint:
2877 case bp_read_watchpoint:
2878 case bp_access_watchpoint:
c906108c 2879
c5aa993b
JM
2880 /* Likewise for watchpoints on local expressions. */
2881 if (b->exp_valid_block != NULL)
2882 delete_breakpoint (b);
967af18d 2883 else if (context == inf_starting)
c860120c 2884 {
4a64f543
MS
2885 /* Reset val field to force reread of starting value in
2886 insert_breakpoints. */
c860120c
PM
2887 if (b->val)
2888 value_free (b->val);
2889 b->val = NULL;
fa4727a6 2890 b->val_valid = 0;
c860120c 2891 }
c5aa993b
JM
2892 break;
2893 default:
c5aa993b
JM
2894 break;
2895 }
2896 }
1c5cfe86
PA
2897
2898 /* Get rid of the moribund locations. */
35df4500
TJB
2899 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2900 decref_bp_location (&bl);
1c5cfe86 2901 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2902}
2903
6c95b8df
PA
2904/* These functions concern about actual breakpoints inserted in the
2905 target --- to e.g. check if we need to do decr_pc adjustment or if
2906 we need to hop over the bkpt --- so we check for address space
2907 match, not program space. */
2908
c2c6d25f
JM
2909/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2910 exists at PC. It returns ordinary_breakpoint_here if it's an
2911 ordinary breakpoint, or permanent_breakpoint_here if it's a
2912 permanent breakpoint.
2913 - When continuing from a location with an ordinary breakpoint, we
2914 actually single step once before calling insert_breakpoints.
2915 - When continuing from a localion with a permanent breakpoint, we
2916 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2917 the target, to advance the PC past the breakpoint. */
c906108c 2918
c2c6d25f 2919enum breakpoint_here
6c95b8df 2920breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2921{
35df4500 2922 struct bp_location *bl, **blp_tmp;
c2c6d25f 2923 int any_breakpoint_here = 0;
c906108c 2924
35df4500 2925 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2926 {
35df4500
TJB
2927 if (bl->loc_type != bp_loc_software_breakpoint
2928 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2929 continue;
2930
35df4500
TJB
2931 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2932 if ((breakpoint_enabled (bl->owner)
2933 || bl->owner->enable_state == bp_permanent)
2934 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2935 aspace, pc))
075f6582
DJ
2936 {
2937 if (overlay_debugging
35df4500
TJB
2938 && section_is_overlay (bl->section)
2939 && !section_is_mapped (bl->section))
075f6582 2940 continue; /* unmapped overlay -- can't be a match */
35df4500 2941 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2942 return permanent_breakpoint_here;
2943 else
2944 any_breakpoint_here = 1;
2945 }
2946 }
c906108c 2947
c2c6d25f 2948 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2949}
2950
1c5cfe86
PA
2951/* Return true if there's a moribund breakpoint at PC. */
2952
2953int
6c95b8df 2954moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2955{
2956 struct bp_location *loc;
2957 int ix;
2958
2959 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
6c95b8df
PA
2960 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
2961 aspace, pc))
1c5cfe86
PA
2962 return 1;
2963
2964 return 0;
2965}
c2c6d25f 2966
c36b740a 2967/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2968 inserted using regular breakpoint_chain / bp_location array
2969 mechanism. This does not check for single-step breakpoints, which
2970 are inserted and removed using direct target manipulation. */
c906108c
SS
2971
2972int
4a64f543
MS
2973regular_breakpoint_inserted_here_p (struct address_space *aspace,
2974 CORE_ADDR pc)
c906108c 2975{
35df4500 2976 struct bp_location *bl, **blp_tmp;
c906108c 2977
35df4500 2978 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2979 {
35df4500
TJB
2980 if (bl->loc_type != bp_loc_software_breakpoint
2981 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2982 continue;
2983
35df4500
TJB
2984 if (bl->inserted
2985 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2986 aspace, pc))
075f6582
DJ
2987 {
2988 if (overlay_debugging
35df4500
TJB
2989 && section_is_overlay (bl->section)
2990 && !section_is_mapped (bl->section))
075f6582
DJ
2991 continue; /* unmapped overlay -- can't be a match */
2992 else
2993 return 1;
2994 }
c5aa993b 2995 }
c36b740a
VP
2996 return 0;
2997}
2998
2999/* Returns non-zero iff there's either regular breakpoint
3000 or a single step breakpoint inserted at PC. */
3001
3002int
6c95b8df 3003breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 3004{
6c95b8df 3005 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 3006 return 1;
c906108c 3007
6c95b8df 3008 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3009 return 1;
3010
c906108c
SS
3011 return 0;
3012}
3013
4fa8626c
DJ
3014/* This function returns non-zero iff there is a software breakpoint
3015 inserted at PC. */
3016
3017int
3e43a32a
MS
3018software_breakpoint_inserted_here_p (struct address_space *aspace,
3019 CORE_ADDR pc)
4fa8626c 3020{
35df4500 3021 struct bp_location *bl, **blp_tmp;
4fa8626c 3022
35df4500 3023 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 3024 {
35df4500 3025 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
3026 continue;
3027
35df4500
TJB
3028 if (bl->inserted
3029 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 3030 aspace, pc))
4fa8626c
DJ
3031 {
3032 if (overlay_debugging
35df4500
TJB
3033 && section_is_overlay (bl->section)
3034 && !section_is_mapped (bl->section))
4fa8626c
DJ
3035 continue; /* unmapped overlay -- can't be a match */
3036 else
3037 return 1;
3038 }
3039 }
3040
1aafd4da 3041 /* Also check for software single-step breakpoints. */
6c95b8df 3042 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3043 return 1;
3044
4fa8626c
DJ
3045 return 0;
3046}
3047
9093389c
PA
3048int
3049hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3050 CORE_ADDR addr, ULONGEST len)
3051{
3052 struct breakpoint *bpt;
3053
3054 ALL_BREAKPOINTS (bpt)
3055 {
3056 struct bp_location *loc;
3057
3058 if (bpt->type != bp_hardware_watchpoint
3059 && bpt->type != bp_access_watchpoint)
3060 continue;
3061
3062 if (!breakpoint_enabled (bpt))
3063 continue;
3064
3065 for (loc = bpt->loc; loc; loc = loc->next)
3066 if (loc->pspace->aspace == aspace && loc->inserted)
3067 {
3068 CORE_ADDR l, h;
3069
3070 /* Check for intersection. */
3071 l = max (loc->address, addr);
3072 h = min (loc->address + loc->length, addr + len);
3073 if (l < h)
3074 return 1;
3075 }
3076 }
3077 return 0;
3078}
3079
075f6582
DJ
3080/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3081 PC is valid for process/thread PTID. */
c906108c
SS
3082
3083int
6c95b8df
PA
3084breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3085 ptid_t ptid)
c906108c 3086{
35df4500 3087 struct bp_location *bl, **blp_tmp;
4a306c9a 3088 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3089 int thread = -1;
4a306c9a 3090 int task = 0;
a6f1cd96 3091
35df4500 3092 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3093 {
35df4500
TJB
3094 if (bl->loc_type != bp_loc_software_breakpoint
3095 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3096 continue;
3097
35df4500
TJB
3098 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3099 if (!breakpoint_enabled (bl->owner)
3100 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3101 continue;
3102
35df4500 3103 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 3104 aspace, pc))
a6f1cd96
JB
3105 continue;
3106
35df4500 3107 if (bl->owner->thread != -1)
075f6582 3108 {
a6f1cd96
JB
3109 /* This is a thread-specific breakpoint. Check that ptid
3110 matches that thread. If thread hasn't been computed yet,
3111 it is now time to do so. */
3112 if (thread == -1)
3113 thread = pid_to_thread_id (ptid);
35df4500 3114 if (bl->owner->thread != thread)
a6f1cd96 3115 continue;
075f6582 3116 }
a6f1cd96 3117
35df4500 3118 if (bl->owner->task != 0)
4a306c9a
JB
3119 {
3120 /* This is a task-specific breakpoint. Check that ptid
3121 matches that task. If task hasn't been computed yet,
3122 it is now time to do so. */
3123 if (task == 0)
3124 task = ada_get_task_number (ptid);
35df4500 3125 if (bl->owner->task != task)
4a306c9a
JB
3126 continue;
3127 }
3128
a6f1cd96 3129 if (overlay_debugging
35df4500
TJB
3130 && section_is_overlay (bl->section)
3131 && !section_is_mapped (bl->section))
a6f1cd96
JB
3132 continue; /* unmapped overlay -- can't be a match */
3133
3134 return 1;
c5aa993b 3135 }
c906108c
SS
3136
3137 return 0;
3138}
c906108c 3139\f
c5aa993b 3140
c906108c
SS
3141/* bpstat stuff. External routines' interfaces are documented
3142 in breakpoint.h. */
3143
3144int
fba45db2 3145ep_is_catchpoint (struct breakpoint *ep)
c906108c 3146{
533be4dd 3147 return (ep->type == bp_catchpoint);
c906108c
SS
3148}
3149
f431efe5
PA
3150/* Frees any storage that is part of a bpstat. Does not walk the
3151 'next' chain. */
3152
3153static void
198757a8
VP
3154bpstat_free (bpstat bs)
3155{
3156 if (bs->old_val != NULL)
3157 value_free (bs->old_val);
9add0f1b 3158 decref_counted_command_line (&bs->commands);
f431efe5 3159 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3160 xfree (bs);
3161}
3162
c906108c
SS
3163/* Clear a bpstat so that it says we are not at any breakpoint.
3164 Also free any storage that is part of a bpstat. */
3165
3166void
fba45db2 3167bpstat_clear (bpstat *bsp)
c906108c
SS
3168{
3169 bpstat p;
3170 bpstat q;
3171
3172 if (bsp == 0)
3173 return;
3174 p = *bsp;
3175 while (p != NULL)
3176 {
3177 q = p->next;
198757a8 3178 bpstat_free (p);
c906108c
SS
3179 p = q;
3180 }
3181 *bsp = NULL;
3182}
3183
3184/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3185 is part of the bpstat is copied as well. */
3186
3187bpstat
fba45db2 3188bpstat_copy (bpstat bs)
c906108c
SS
3189{
3190 bpstat p = NULL;
3191 bpstat tmp;
3192 bpstat retval = NULL;
3193
3194 if (bs == NULL)
3195 return bs;
3196
3197 for (; bs != NULL; bs = bs->next)
3198 {
3199 tmp = (bpstat) xmalloc (sizeof (*tmp));
3200 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3201 incref_counted_command_line (tmp->commands);
f431efe5 3202 incref_bp_location (tmp->bp_location_at);
31cc81e9 3203 if (bs->old_val != NULL)
3c3185ac
JK
3204 {
3205 tmp->old_val = value_copy (bs->old_val);
3206 release_value (tmp->old_val);
3207 }
31cc81e9 3208
c906108c
SS
3209 if (p == NULL)
3210 /* This is the first thing in the chain. */
3211 retval = tmp;
3212 else
3213 p->next = tmp;
3214 p = tmp;
3215 }
3216 p->next = NULL;
3217 return retval;
3218}
3219
4a64f543 3220/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3221
3222bpstat
fba45db2 3223bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3224{
c5aa993b
JM
3225 if (bsp == NULL)
3226 return NULL;
c906108c 3227
c5aa993b
JM
3228 for (; bsp != NULL; bsp = bsp->next)
3229 {
f431efe5 3230 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3231 return bsp;
3232 }
c906108c
SS
3233 return NULL;
3234}
3235
4a64f543
MS
3236/* Put in *NUM the breakpoint number of the first breakpoint we are
3237 stopped at. *BSP upon return is a bpstat which points to the
3238 remaining breakpoints stopped at (but which is not guaranteed to be
3239 good for anything but further calls to bpstat_num).
3240
8671a17b
PA
3241 Return 0 if passed a bpstat which does not indicate any breakpoints.
3242 Return -1 if stopped at a breakpoint that has been deleted since
3243 we set it.
3244 Return 1 otherwise. */
c906108c
SS
3245
3246int
8671a17b 3247bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3248{
3249 struct breakpoint *b;
3250
3251 if ((*bsp) == NULL)
3252 return 0; /* No more breakpoint values */
8671a17b 3253
4a64f543
MS
3254 /* We assume we'll never have several bpstats that correspond to a
3255 single breakpoint -- otherwise, this function might return the
3256 same number more than once and this will look ugly. */
f431efe5 3257 b = (*bsp)->breakpoint_at;
8671a17b
PA
3258 *bsp = (*bsp)->next;
3259 if (b == NULL)
3260 return -1; /* breakpoint that's been deleted since */
3261
3262 *num = b->number; /* We have its number */
3263 return 1;
c906108c
SS
3264}
3265
3266/* Modify BS so that the actions will not be performed. */
3267
3268void
fba45db2 3269bpstat_clear_actions (bpstat bs)
c906108c
SS
3270{
3271 for (; bs != NULL; bs = bs->next)
3272 {
9add0f1b 3273 decref_counted_command_line (&bs->commands);
dde2d684 3274 bs->commands_left = NULL;
c906108c
SS
3275 if (bs->old_val != NULL)
3276 {
3277 value_free (bs->old_val);
3278 bs->old_val = NULL;
3279 }
3280 }
3281}
3282
f3b1572e
PA
3283/* Called when a command is about to proceed the inferior. */
3284
3285static void
3286breakpoint_about_to_proceed (void)
3287{
3288 if (!ptid_equal (inferior_ptid, null_ptid))
3289 {
3290 struct thread_info *tp = inferior_thread ();
3291
3292 /* Allow inferior function calls in breakpoint commands to not
3293 interrupt the command list. When the call finishes
3294 successfully, the inferior will be standing at the same
3295 breakpoint as if nothing happened. */
16c381f0 3296 if (tp->control.in_infcall)
f3b1572e
PA
3297 return;
3298 }
3299
3300 breakpoint_proceeded = 1;
3301}
3302
4a64f543
MS
3303/* Stub for cleaning up our state if we error-out of a breakpoint
3304 command. */
c906108c 3305static void
4efb68b1 3306cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3307{
3308 executing_breakpoint_commands = 0;
3309}
3310
4a64f543
MS
3311/* Execute all the commands associated with all the breakpoints at
3312 this location. Any of these commands could cause the process to
3313 proceed beyond this point, etc. We look out for such changes by
3314 checking the global "breakpoint_proceeded" after each command.
c906108c 3315
347bddb7
PA
3316 Returns true if a breakpoint command resumed the inferior. In that
3317 case, it is the caller's responsibility to recall it again with the
3318 bpstat of the current thread. */
3319
3320static int
3321bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3322{
3323 bpstat bs;
3324 struct cleanup *old_chain;
347bddb7 3325 int again = 0;
c906108c
SS
3326
3327 /* Avoid endless recursion if a `source' command is contained
3328 in bs->commands. */
3329 if (executing_breakpoint_commands)
347bddb7 3330 return 0;
c906108c
SS
3331
3332 executing_breakpoint_commands = 1;
3333 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3334
4a64f543 3335 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3336 bs = *bsp;
3337
3338 breakpoint_proceeded = 0;
3339 for (; bs != NULL; bs = bs->next)
3340 {
9add0f1b 3341 struct counted_command_line *ccmd;
6c50ab1c
JB
3342 struct command_line *cmd;
3343 struct cleanup *this_cmd_tree_chain;
3344
3345 /* Take ownership of the BSP's command tree, if it has one.
3346
3347 The command tree could legitimately contain commands like
3348 'step' and 'next', which call clear_proceed_status, which
3349 frees stop_bpstat's command tree. To make sure this doesn't
3350 free the tree we're executing out from under us, we need to
3351 take ownership of the tree ourselves. Since a given bpstat's
3352 commands are only executed once, we don't need to copy it; we
3353 can clear the pointer in the bpstat, and make sure we free
3354 the tree when we're done. */
9add0f1b
TT
3355 ccmd = bs->commands;
3356 bs->commands = NULL;
3357 this_cmd_tree_chain
3358 = make_cleanup_decref_counted_command_line (&ccmd);
3359 cmd = bs->commands_left;
3360 bs->commands_left = NULL;
6c50ab1c 3361
c906108c
SS
3362 while (cmd != NULL)
3363 {
3364 execute_control_command (cmd);
3365
3366 if (breakpoint_proceeded)
3367 break;
3368 else
3369 cmd = cmd->next;
3370 }
6c50ab1c
JB
3371
3372 /* We can free this command tree now. */
3373 do_cleanups (this_cmd_tree_chain);
3374
c906108c 3375 if (breakpoint_proceeded)
32c1e744
VP
3376 {
3377 if (target_can_async_p ())
347bddb7
PA
3378 /* If we are in async mode, then the target might be still
3379 running, not stopped at any breakpoint, so nothing for
3380 us to do here -- just return to the event loop. */
3381 ;
32c1e744
VP
3382 else
3383 /* In sync mode, when execute_control_command returns
3384 we're already standing on the next breakpoint.
347bddb7
PA
3385 Breakpoint commands for that stop were not run, since
3386 execute_command does not run breakpoint commands --
3387 only command_line_handler does, but that one is not
3388 involved in execution of breakpoint commands. So, we
3389 can now execute breakpoint commands. It should be
3390 noted that making execute_command do bpstat actions is
3391 not an option -- in this case we'll have recursive
3392 invocation of bpstat for each breakpoint with a
3393 command, and can easily blow up GDB stack. Instead, we
3394 return true, which will trigger the caller to recall us
3395 with the new stop_bpstat. */
3396 again = 1;
3397 break;
32c1e744 3398 }
c906108c 3399 }
c2b8ed2c 3400 do_cleanups (old_chain);
347bddb7
PA
3401 return again;
3402}
3403
3404void
3405bpstat_do_actions (void)
3406{
3407 /* Do any commands attached to breakpoint we are stopped at. */
3408 while (!ptid_equal (inferior_ptid, null_ptid)
3409 && target_has_execution
3410 && !is_exited (inferior_ptid)
3411 && !is_executing (inferior_ptid))
3412 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3413 and only return when it is stopped at the next breakpoint, we
3414 keep doing breakpoint actions until it returns false to
3415 indicate the inferior was not resumed. */
16c381f0 3416 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3417 break;
c906108c
SS
3418}
3419
fa4727a6
DJ
3420/* Print out the (old or new) value associated with a watchpoint. */
3421
3422static void
3423watchpoint_value_print (struct value *val, struct ui_file *stream)
3424{
3425 if (val == NULL)
3426 fprintf_unfiltered (stream, _("<unreadable>"));
3427 else
79a45b7d
TT
3428 {
3429 struct value_print_options opts;
3430 get_user_print_options (&opts);
3431 value_print (val, stream, &opts);
3432 }
fa4727a6
DJ
3433}
3434
e514a9d6 3435/* This is the normal print function for a bpstat. In the future,
c906108c 3436 much of this logic could (should?) be moved to bpstat_stop_status,
e514a9d6
JM
3437 by having it set different print_it values.
3438
3439 Current scheme: When we stop, bpstat_print() is called. It loops
3440 through the bpstat list of things causing this stop, calling the
4a64f543 3441 print_bp_stop_message function on each one. The behavior of the
e514a9d6 3442 print_bp_stop_message function depends on the print_it field of
4a64f543 3443 bpstat. If such field so indicates, call this function here.
e514a9d6
JM
3444
3445 Return values from this routine (ultimately used by bpstat_print()
3446 and normal_stop() to decide what to do):
3447 PRINT_NOTHING: Means we already printed all we needed to print,
3448 don't print anything else.
3449 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
3450 that something to be followed by a location.
3451 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
3452 that something to be followed by a location.
3453 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
3454 analysis. */
c906108c 3455
917317f4 3456static enum print_stop_action
fba45db2 3457print_it_typical (bpstat bs)
c906108c 3458{
f7545552 3459 struct cleanup *old_chain;
4f8d1dc6 3460 struct breakpoint *b;
89f9893c 3461 const struct bp_location *bl;
8b93c638 3462 struct ui_stream *stb;
f7545552
TT
3463 int bp_temp = 0;
3464 enum print_stop_action result;
3465
c906108c
SS
3466 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3467 which has since been deleted. */
e514a9d6 3468 if (bs->breakpoint_at == NULL)
917317f4 3469 return PRINT_UNKNOWN;
2bdf28a0 3470
f431efe5
PA
3471 gdb_assert (bs->bp_location_at != NULL);
3472
3473 bl = bs->bp_location_at;
3474 b = bs->breakpoint_at;
c906108c 3475
f7545552
TT
3476 stb = ui_out_stream_new (uiout);
3477 old_chain = make_cleanup_ui_out_stream_delete (stb);
3478
4f8d1dc6 3479 switch (b->type)
c906108c 3480 {
e514a9d6
JM
3481 case bp_breakpoint:
3482 case bp_hardware_breakpoint:
f431efe5 3483 bp_temp = b->disposition == disp_del;
0d381245
VP
3484 if (bl->address != bl->requested_address)
3485 breakpoint_adjustment_warning (bl->requested_address,
3486 bl->address,
4f8d1dc6
VP
3487 b->number, 1);
3488 annotate_breakpoint (b->number);
2cec12e5
AR
3489 if (bp_temp)
3490 ui_out_text (uiout, "\nTemporary breakpoint ");
3491 else
3492 ui_out_text (uiout, "\nBreakpoint ");
9dc5e2a9 3493 if (ui_out_is_mi_like_p (uiout))
2cec12e5
AR
3494 {
3495 ui_out_field_string (uiout, "reason",
3496 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
3497 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3498 }
4f8d1dc6 3499 ui_out_field_int (uiout, "bkptno", b->number);
8b93c638 3500 ui_out_text (uiout, ", ");
f7545552 3501 result = PRINT_SRC_AND_LOC;
e514a9d6
JM
3502 break;
3503
3504 case bp_shlib_event:
917317f4
JM
3505 /* Did we stop because the user set the stop_on_solib_events
3506 variable? (If so, we report this as a generic, "Stopped due
3507 to shlib event" message.) */
a3f17187 3508 printf_filtered (_("Stopped due to shared library event\n"));
f7545552 3509 result = PRINT_NOTHING;
e514a9d6
JM
3510 break;
3511
c4093a6a 3512 case bp_thread_event:
4a64f543 3513 /* Not sure how we will get here.
c4093a6a 3514 GDB should not stop for these breakpoints. */
a3f17187 3515 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
f7545552 3516 result = PRINT_NOTHING;
c4093a6a
JM
3517 break;
3518
1900040c 3519 case bp_overlay_event:
4a64f543 3520 /* By analogy with the thread event, GDB should not stop for these. */
a3f17187 3521 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
f7545552 3522 result = PRINT_NOTHING;
1900040c
MS
3523 break;
3524
0fd8e87f
UW
3525 case bp_longjmp_master:
3526 /* These should never be enabled. */
3527 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
3528 result = PRINT_NOTHING;
3529 break;
3530
aa7d318d
TT
3531 case bp_std_terminate_master:
3532 /* These should never be enabled. */
3e43a32a
MS
3533 printf_filtered (_("std::terminate Master Breakpoint: "
3534 "gdb should not stop!\n"));
aa7d318d
TT
3535 result = PRINT_NOTHING;
3536 break;
3537
186c406b
TT
3538 case bp_exception_master:
3539 /* These should never be enabled. */
3e43a32a
MS
3540 printf_filtered (_("Exception Master Breakpoint: "
3541 "gdb should not stop!\n"));
186c406b
TT
3542 result = PRINT_NOTHING;
3543 break;
3544
e514a9d6
JM
3545 case bp_watchpoint:
3546 case bp_hardware_watchpoint:
fa4727a6
DJ
3547 annotate_watchpoint (b->number);
3548 if (ui_out_is_mi_like_p (uiout))
3549 ui_out_field_string
3550 (uiout, "reason",
3551 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
3552 mention (b);
f7545552 3553 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
fa4727a6
DJ
3554 ui_out_text (uiout, "\nOld value = ");
3555 watchpoint_value_print (bs->old_val, stb->stream);
3556 ui_out_field_stream (uiout, "old", stb);
3557 ui_out_text (uiout, "\nNew value = ");
3558 watchpoint_value_print (b->val, stb->stream);
3559 ui_out_field_stream (uiout, "new", stb);
fa4727a6 3560 ui_out_text (uiout, "\n");
e514a9d6 3561 /* More than one watchpoint may have been triggered. */
f7545552 3562 result = PRINT_UNKNOWN;
e514a9d6
JM
3563 break;
3564
3565 case bp_read_watchpoint:
9dc5e2a9 3566 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3567 ui_out_field_string
3568 (uiout, "reason",
3569 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
4f8d1dc6 3570 mention (b);
f7545552 3571 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3572 ui_out_text (uiout, "\nValue = ");
fa4727a6 3573 watchpoint_value_print (b->val, stb->stream);
8b93c638 3574 ui_out_field_stream (uiout, "value", stb);
8b93c638 3575 ui_out_text (uiout, "\n");
f7545552 3576 result = PRINT_UNKNOWN;
e514a9d6
JM
3577 break;
3578
3579 case bp_access_watchpoint:
fa4727a6 3580 if (bs->old_val != NULL)
8b93c638 3581 {
4f8d1dc6 3582 annotate_watchpoint (b->number);
9dc5e2a9 3583 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3584 ui_out_field_string
3585 (uiout, "reason",
3586 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
4f8d1dc6 3587 mention (b);
f7545552 3588 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3589 ui_out_text (uiout, "\nOld value = ");
fa4727a6 3590 watchpoint_value_print (bs->old_val, stb->stream);
8b93c638 3591 ui_out_field_stream (uiout, "old", stb);
8b93c638
JM
3592 ui_out_text (uiout, "\nNew value = ");
3593 }
3594 else
3595 {
4f8d1dc6 3596 mention (b);
9dc5e2a9 3597 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3598 ui_out_field_string
3599 (uiout, "reason",
3600 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
f7545552 3601 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638
JM
3602 ui_out_text (uiout, "\nValue = ");
3603 }
fa4727a6 3604 watchpoint_value_print (b->val, stb->stream);
8b93c638 3605 ui_out_field_stream (uiout, "new", stb);
8b93c638 3606 ui_out_text (uiout, "\n");
f7545552 3607 result = PRINT_UNKNOWN;
e514a9d6 3608 break;
4ce44c66 3609
e514a9d6 3610 /* Fall through, we don't deal with these types of breakpoints
4a64f543 3611 here. */
e514a9d6 3612
11cf8741 3613 case bp_finish:
9dc5e2a9 3614 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3615 ui_out_field_string
3616 (uiout, "reason",
3617 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
f7545552 3618 result = PRINT_UNKNOWN;
8b93c638
JM
3619 break;
3620
e514a9d6 3621 case bp_until:
9dc5e2a9 3622 if (ui_out_is_mi_like_p (uiout))
1fbc2a49
NR
3623 ui_out_field_string
3624 (uiout, "reason",
3625 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
f7545552 3626 result = PRINT_UNKNOWN;
8b93c638
JM
3627 break;
3628
c2d11a7d 3629 case bp_none:
e514a9d6
JM
3630 case bp_longjmp:
3631 case bp_longjmp_resume:
186c406b
TT
3632 case bp_exception:
3633 case bp_exception_resume:
e514a9d6 3634 case bp_step_resume:
e514a9d6
JM
3635 case bp_watchpoint_scope:
3636 case bp_call_dummy:
aa7d318d 3637 case bp_std_terminate:
1042e4c0 3638 case bp_tracepoint:
7a697b8d 3639 case bp_fast_tracepoint:
4efc6507 3640 case bp_jit_event:
e514a9d6 3641 default:
f7545552
TT
3642 result = PRINT_UNKNOWN;
3643 break;
e514a9d6 3644 }
f7545552
TT
3645
3646 do_cleanups (old_chain);
3647 return result;
e514a9d6
JM
3648}
3649
3650/* Generic routine for printing messages indicating why we
4a64f543 3651 stopped. The behavior of this function depends on the value
e514a9d6
JM
3652 'print_it' in the bpstat structure. Under some circumstances we
3653 may decide not to print anything here and delegate the task to
4a64f543 3654 normal_stop(). */
e514a9d6
JM
3655
3656static enum print_stop_action
3657print_bp_stop_message (bpstat bs)
3658{
3659 switch (bs->print_it)
3660 {
3661 case print_it_noop:
4a64f543 3662 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3663 return PRINT_UNKNOWN;
3664 break;
3665
3666 case print_it_done:
3667 /* We still want to print the frame, but we already printed the
4a64f543 3668 relevant messages. */
e514a9d6
JM
3669 return PRINT_SRC_AND_LOC;
3670 break;
3671
3672 case print_it_normal:
4f8d1dc6 3673 {
f431efe5
PA
3674 struct breakpoint *b = bs->breakpoint_at;
3675
4f8d1dc6
VP
3676 /* Normal case. Call the breakpoint's print_it method, or
3677 print_it_typical. */
3678 /* FIXME: how breakpoint can ever be NULL here? */
3679 if (b != NULL && b->ops != NULL && b->ops->print_it != NULL)
3680 return b->ops->print_it (b);
3681 else
3682 return print_it_typical (bs);
3683 }
3684 break;
3086aeae 3685
e514a9d6 3686 default:
8e65ff28 3687 internal_error (__FILE__, __LINE__,
e2e0b3e5 3688 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3689 break;
c906108c 3690 }
c906108c
SS
3691}
3692
e514a9d6
JM
3693/* Print a message indicating what happened. This is called from
3694 normal_stop(). The input to this routine is the head of the bpstat
3695 list - a list of the eventpoints that caused this stop. This
3696 routine calls the generic print routine for printing a message
3697 about reasons for stopping. This will print (for example) the
3698 "Breakpoint n," part of the output. The return value of this
3699 routine is one of:
c906108c 3700
4a64f543 3701 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3702 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3703 code to print the location. An example is
c5aa993b
JM
3704 "Breakpoint 1, " which should be followed by
3705 the location.
917317f4 3706 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3707 to also print the location part of the message.
3708 An example is the catch/throw messages, which
4a64f543 3709 don't require a location appended to the end.
917317f4 3710 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3711 further info to be printed. */
c906108c 3712
917317f4 3713enum print_stop_action
fba45db2 3714bpstat_print (bpstat bs)
c906108c
SS
3715{
3716 int val;
c5aa993b 3717
c906108c 3718 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3719 (Currently all watchpoints go on the bpstat whether hit or not.
3720 That probably could (should) be changed, provided care is taken
c906108c 3721 with respect to bpstat_explains_signal). */
e514a9d6
JM
3722 for (; bs; bs = bs->next)
3723 {
3724 val = print_bp_stop_message (bs);
3725 if (val == PRINT_SRC_ONLY
3726 || val == PRINT_SRC_AND_LOC
3727 || val == PRINT_NOTHING)
3728 return val;
3729 }
c906108c 3730
e514a9d6 3731 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3732 with and nothing was printed. */
917317f4 3733 return PRINT_UNKNOWN;
c906108c
SS
3734}
3735
4a64f543
MS
3736/* Evaluate the expression EXP and return 1 if value is zero. This is
3737 used inside a catch_errors to evaluate the breakpoint condition.
3738 The argument is a "struct expression *" that has been cast to a
3739 "char *" to make it pass through catch_errors. */
c906108c
SS
3740
3741static int
4efb68b1 3742breakpoint_cond_eval (void *exp)
c906108c 3743{
278cd55f 3744 struct value *mark = value_mark ();
c5aa993b 3745 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3746
c906108c
SS
3747 value_free_to_mark (mark);
3748 return i;
3749}
3750
5760d0ab 3751/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3752
3753static bpstat
5760d0ab 3754bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3755{
3756 bpstat bs;
3757
3758 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3759 bs->next = NULL;
3760 **bs_link_pointer = bs;
3761 *bs_link_pointer = &bs->next;
f431efe5
PA
3762 bs->breakpoint_at = bl->owner;
3763 bs->bp_location_at = bl;
3764 incref_bp_location (bl);
c906108c
SS
3765 /* If the condition is false, etc., don't do the commands. */
3766 bs->commands = NULL;
9add0f1b 3767 bs->commands_left = NULL;
c906108c
SS
3768 bs->old_val = NULL;
3769 bs->print_it = print_it_normal;
3770 return bs;
3771}
3772\f
d983da9c
DJ
3773/* The target has stopped with waitstatus WS. Check if any hardware
3774 watchpoints have triggered, according to the target. */
3775
3776int
3777watchpoints_triggered (struct target_waitstatus *ws)
3778{
d92524f1 3779 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3780 CORE_ADDR addr;
3781 struct breakpoint *b;
3782
3783 if (!stopped_by_watchpoint)
3784 {
3785 /* We were not stopped by a watchpoint. Mark all watchpoints
3786 as not triggered. */
3787 ALL_BREAKPOINTS (b)
cc60f2e3 3788 if (is_hardware_watchpoint (b))
d983da9c
DJ
3789 b->watchpoint_triggered = watch_triggered_no;
3790
3791 return 0;
3792 }
3793
3794 if (!target_stopped_data_address (&current_target, &addr))
3795 {
3796 /* We were stopped by a watchpoint, but we don't know where.
3797 Mark all watchpoints as unknown. */
3798 ALL_BREAKPOINTS (b)
cc60f2e3 3799 if (is_hardware_watchpoint (b))
d983da9c
DJ
3800 b->watchpoint_triggered = watch_triggered_unknown;
3801
3802 return stopped_by_watchpoint;
3803 }
3804
3805 /* The target could report the data address. Mark watchpoints
3806 affected by this data address as triggered, and all others as not
3807 triggered. */
3808
3809 ALL_BREAKPOINTS (b)
cc60f2e3 3810 if (is_hardware_watchpoint (b))
d983da9c 3811 {
a5606eee 3812 struct bp_location *loc;
d983da9c
DJ
3813
3814 b->watchpoint_triggered = watch_triggered_no;
a5606eee
VP
3815 for (loc = b->loc; loc; loc = loc->next)
3816 /* Exact match not required. Within range is
3817 sufficient. */
5009afc5
AS
3818 if (target_watchpoint_addr_within_range (&current_target,
3819 addr, loc->address,
3820 loc->length))
a5606eee
VP
3821 {
3822 b->watchpoint_triggered = watch_triggered_yes;
3823 break;
3824 }
d983da9c
DJ
3825 }
3826
3827 return 1;
3828}
3829
c906108c
SS
3830/* Possible return values for watchpoint_check (this can't be an enum
3831 because of check_errors). */
3832/* The watchpoint has been deleted. */
3833#define WP_DELETED 1
3834/* The value has changed. */
3835#define WP_VALUE_CHANGED 2
3836/* The value has not changed. */
3837#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3838/* Ignore this watchpoint, no matter if the value changed or not. */
3839#define WP_IGNORE 4
c906108c
SS
3840
3841#define BP_TEMPFLAG 1
3842#define BP_HARDWAREFLAG 2
3843
4a64f543
MS
3844/* Evaluate watchpoint condition expression and check if its value
3845 changed.
553e4c11
JB
3846
3847 P should be a pointer to struct bpstat, but is defined as a void *
3848 in order for this function to be usable with catch_errors. */
c906108c
SS
3849
3850static int
4efb68b1 3851watchpoint_check (void *p)
c906108c
SS
3852{
3853 bpstat bs = (bpstat) p;
3854 struct breakpoint *b;
3855 struct frame_info *fr;
3856 int within_current_scope;
3857
f431efe5 3858 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3859 gdb_assert (bs->breakpoint_at != NULL);
f431efe5 3860 b = bs->breakpoint_at;
c906108c 3861
f6bc2008
PA
3862 /* If this is a local watchpoint, we only want to check if the
3863 watchpoint frame is in scope if the current thread is the thread
3864 that was used to create the watchpoint. */
3865 if (!watchpoint_in_thread_scope (b))
60e1c644 3866 return WP_IGNORE;
f6bc2008 3867
c906108c
SS
3868 if (b->exp_valid_block == NULL)
3869 within_current_scope = 1;
3870 else
3871 {
edb3359d
DJ
3872 struct frame_info *frame = get_current_frame ();
3873 struct gdbarch *frame_arch = get_frame_arch (frame);
3874 CORE_ADDR frame_pc = get_frame_pc (frame);
3875
4a64f543
MS
3876 /* in_function_epilogue_p() returns a non-zero value if we're
3877 still in the function but the stack frame has already been
3878 invalidated. Since we can't rely on the values of local
3879 variables after the stack has been destroyed, we are treating
3880 the watchpoint in that state as `not changed' without further
3881 checking. Don't mark watchpoints as changed if the current
3882 frame is in an epilogue - even if they are in some other
3883 frame, our view of the stack is likely to be wrong and
3884 frame_find_by_id could error out. */
a0f49112 3885 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3886 return WP_IGNORE;
a0f49112 3887
101dcfbe 3888 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3889 within_current_scope = (fr != NULL);
69fbadd5
DJ
3890
3891 /* If we've gotten confused in the unwinder, we might have
3892 returned a frame that can't describe this variable. */
edb3359d
DJ
3893 if (within_current_scope)
3894 {
3895 struct symbol *function;
3896
3897 function = get_frame_function (fr);
3898 if (function == NULL
3899 || !contained_in (b->exp_valid_block,
3900 SYMBOL_BLOCK_VALUE (function)))
3901 within_current_scope = 0;
3902 }
69fbadd5 3903
edb3359d 3904 if (within_current_scope)
c906108c
SS
3905 /* If we end up stopping, the current frame will get selected
3906 in normal_stop. So this call to select_frame won't affect
3907 the user. */
0f7d239c 3908 select_frame (fr);
c906108c 3909 }
c5aa993b 3910
c906108c
SS
3911 if (within_current_scope)
3912 {
4a64f543
MS
3913 /* We use value_{,free_to_}mark because it could be a *long*
3914 time before we return to the command level and call
3915 free_all_values. We can't call free_all_values because we
3916 might be in the middle of evaluating a function call. */
c906108c 3917
0cf6dd15 3918 int pc = 0;
278cd55f 3919 struct value *mark = value_mark ();
fa4727a6
DJ
3920 struct value *new_val;
3921
0cf6dd15 3922 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3923
4a64f543
MS
3924 /* We use value_equal_contents instead of value_equal because
3925 the latter coerces an array to a pointer, thus comparing just
3926 the address of the array instead of its contents. This is
3927 not what we want. */
fa4727a6 3928 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3929 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3930 {
fa4727a6
DJ
3931 if (new_val != NULL)
3932 {
3933 release_value (new_val);
3934 value_free_to_mark (mark);
3935 }
c906108c
SS
3936 bs->old_val = b->val;
3937 b->val = new_val;
fa4727a6 3938 b->val_valid = 1;
c906108c
SS
3939 return WP_VALUE_CHANGED;
3940 }
3941 else
3942 {
60e1c644 3943 /* Nothing changed. */
c906108c 3944 value_free_to_mark (mark);
c906108c
SS
3945 return WP_VALUE_NOT_CHANGED;
3946 }
3947 }
3948 else
3949 {
3950 /* This seems like the only logical thing to do because
c5aa993b
JM
3951 if we temporarily ignored the watchpoint, then when
3952 we reenter the block in which it is valid it contains
3953 garbage (in the case of a function, it may have two
3954 garbage values, one before and one after the prologue).
3955 So we can't even detect the first assignment to it and
3956 watch after that (since the garbage may or may not equal
3957 the first value assigned). */
4ce44c66
JM
3958 /* We print all the stop information in print_it_typical(), but
3959 in this case, by the time we call print_it_typical() this bp
4a64f543
MS
3960 will be deleted already. So we have no choice but print the
3961 information here. */
9dc5e2a9 3962 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3963 ui_out_field_string
3964 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3965 ui_out_text (uiout, "\nWatchpoint ");
4f8d1dc6 3966 ui_out_field_int (uiout, "wpnum", b->number);
3e43a32a
MS
3967 ui_out_text (uiout,
3968 " deleted because the program has left the block in\n\
8b93c638 3969which its expression is valid.\n");
4ce44c66 3970
c906108c 3971 if (b->related_breakpoint)
60e1c644
PA
3972 {
3973 b->related_breakpoint->disposition = disp_del_at_next_stop;
3974 b->related_breakpoint->related_breakpoint = NULL;
3975 b->related_breakpoint = NULL;
3976 }
b5de0fa7 3977 b->disposition = disp_del_at_next_stop;
c906108c
SS
3978
3979 return WP_DELETED;
3980 }
3981}
3982
18a18393
VP
3983/* Return true if it looks like target has stopped due to hitting
3984 breakpoint location BL. This function does not check if we
3985 should stop, only if BL explains the stop. */
3986static int
6c95b8df
PA
3987bpstat_check_location (const struct bp_location *bl,
3988 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3989{
3990 struct breakpoint *b = bl->owner;
3991
2bdf28a0
JK
3992 /* BL is from existing struct breakpoint. */
3993 gdb_assert (b != NULL);
3994
e8595ef6
SS
3995 /* By definition, the inferior does not report stops at
3996 tracepoints. */
d77f58be 3997 if (is_tracepoint (b))
e8595ef6
SS
3998 return 0;
3999
cc60f2e3 4000 if (!is_watchpoint (b)
18a18393 4001 && b->type != bp_hardware_breakpoint
fe798b75 4002 && b->type != bp_catchpoint) /* a non-watchpoint bp */
18a18393 4003 {
6c95b8df
PA
4004 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
4005 aspace, bp_addr))
18a18393
VP
4006 return 0;
4007 if (overlay_debugging /* unmapped overlay section */
35df4500 4008 && section_is_overlay (bl->section)
18a18393
VP
4009 && !section_is_mapped (bl->section))
4010 return 0;
4011 }
cc60f2e3 4012
18a18393
VP
4013 /* Continuable hardware watchpoints are treated as non-existent if the
4014 reason we stopped wasn't a hardware watchpoint (we didn't stop on
4015 some data address). Otherwise gdb won't stop on a break instruction
4016 in the code (not from a breakpoint) when a hardware watchpoint has
4017 been defined. Also skip watchpoints which we know did not trigger
4018 (did not match the data address). */
cc60f2e3
PA
4019
4020 if (is_hardware_watchpoint (b)
18a18393
VP
4021 && b->watchpoint_triggered == watch_triggered_no)
4022 return 0;
4023
4024 if (b->type == bp_hardware_breakpoint)
4025 {
4026 if (bl->address != bp_addr)
4027 return 0;
4028 if (overlay_debugging /* unmapped overlay section */
35df4500 4029 && section_is_overlay (bl->section)
18a18393
VP
4030 && !section_is_mapped (bl->section))
4031 return 0;
4032 }
ce78b96d 4033
ce78b96d
JB
4034 if (b->type == bp_catchpoint)
4035 {
4036 gdb_assert (b->ops != NULL && b->ops->breakpoint_hit != NULL);
4037 if (!b->ops->breakpoint_hit (b))
4038 return 0;
4039 }
4040
18a18393
VP
4041 return 1;
4042}
4043
4044/* If BS refers to a watchpoint, determine if the watched values
4045 has actually changed, and we should stop. If not, set BS->stop
4046 to 0. */
4047static void
4048bpstat_check_watchpoint (bpstat bs)
4049{
2bdf28a0
JK
4050 const struct bp_location *bl;
4051 struct breakpoint *b;
4052
4053 /* BS is built for existing struct breakpoint. */
f431efe5 4054 bl = bs->bp_location_at;
2bdf28a0 4055 gdb_assert (bl != NULL);
f431efe5 4056 b = bs->breakpoint_at;
2bdf28a0 4057 gdb_assert (b != NULL);
18a18393 4058
cc60f2e3 4059 if (is_watchpoint (b))
18a18393 4060 {
18a18393
VP
4061 int must_check_value = 0;
4062
4063 if (b->type == bp_watchpoint)
4064 /* For a software watchpoint, we must always check the
4065 watched value. */
4066 must_check_value = 1;
4067 else if (b->watchpoint_triggered == watch_triggered_yes)
4068 /* We have a hardware watchpoint (read, write, or access)
4069 and the target earlier reported an address watched by
4070 this watchpoint. */
4071 must_check_value = 1;
4072 else if (b->watchpoint_triggered == watch_triggered_unknown
4073 && b->type == bp_hardware_watchpoint)
4074 /* We were stopped by a hardware watchpoint, but the target could
4075 not report the data address. We must check the watchpoint's
4076 value. Access and read watchpoints are out of luck; without
4077 a data address, we can't figure it out. */
4078 must_check_value = 1;
4079
4080 if (must_check_value)
4081 {
3e43a32a
MS
4082 char *message
4083 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
4084 b->number);
18a18393
VP
4085 struct cleanup *cleanups = make_cleanup (xfree, message);
4086 int e = catch_errors (watchpoint_check, bs, message,
4087 RETURN_MASK_ALL);
4088 do_cleanups (cleanups);
4089 switch (e)
4090 {
4091 case WP_DELETED:
4092 /* We've already printed what needs to be printed. */
4093 bs->print_it = print_it_done;
4094 /* Stop. */
4095 break;
60e1c644
PA
4096 case WP_IGNORE:
4097 bs->print_it = print_it_noop;
4098 bs->stop = 0;
4099 break;
18a18393
VP
4100 case WP_VALUE_CHANGED:
4101 if (b->type == bp_read_watchpoint)
4102 {
85d721b8
PA
4103 /* There are two cases to consider here:
4104
4a64f543 4105 1. We're watching the triggered memory for reads.
85d721b8
PA
4106 In that case, trust the target, and always report
4107 the watchpoint hit to the user. Even though
4108 reads don't cause value changes, the value may
4109 have changed since the last time it was read, and
4110 since we're not trapping writes, we will not see
4111 those, and as such we should ignore our notion of
4112 old value.
4113
4a64f543 4114 2. We're watching the triggered memory for both
85d721b8
PA
4115 reads and writes. There are two ways this may
4116 happen:
4117
4a64f543 4118 2.1. This is a target that can't break on data
85d721b8
PA
4119 reads only, but can break on accesses (reads or
4120 writes), such as e.g., x86. We detect this case
4121 at the time we try to insert read watchpoints.
4122
4a64f543 4123 2.2. Otherwise, the target supports read
85d721b8
PA
4124 watchpoints, but, the user set an access or write
4125 watchpoint watching the same memory as this read
4126 watchpoint.
4127
4128 If we're watching memory writes as well as reads,
4129 ignore watchpoint hits when we find that the
4130 value hasn't changed, as reads don't cause
4131 changes. This still gives false positives when
4132 the program writes the same value to memory as
4133 what there was already in memory (we will confuse
4134 it for a read), but it's much better than
4135 nothing. */
4136
4137 int other_write_watchpoint = 0;
4138
4139 if (bl->watchpoint_type == hw_read)
4140 {
4141 struct breakpoint *other_b;
4142
4143 ALL_BREAKPOINTS (other_b)
4144 if ((other_b->type == bp_hardware_watchpoint
4145 || other_b->type == bp_access_watchpoint)
4146 && (other_b->watchpoint_triggered
4147 == watch_triggered_yes))
4148 {
4149 other_write_watchpoint = 1;
4150 break;
4151 }
4152 }
4153
4154 if (other_write_watchpoint
4155 || bl->watchpoint_type == hw_access)
4156 {
4157 /* We're watching the same memory for writes,
4158 and the value changed since the last time we
4159 updated it, so this trap must be for a write.
4160 Ignore it. */
4161 bs->print_it = print_it_noop;
4162 bs->stop = 0;
4163 }
18a18393
VP
4164 }
4165 break;
4166 case WP_VALUE_NOT_CHANGED:
4167 if (b->type == bp_hardware_watchpoint
4168 || b->type == bp_watchpoint)
4169 {
4170 /* Don't stop: write watchpoints shouldn't fire if
4171 the value hasn't changed. */
4172 bs->print_it = print_it_noop;
4173 bs->stop = 0;
4174 }
4175 /* Stop. */
4176 break;
4177 default:
4178 /* Can't happen. */
4179 case 0:
4180 /* Error from catch_errors. */
4181 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
4182 if (b->related_breakpoint)
4183 b->related_breakpoint->disposition = disp_del_at_next_stop;
4184 b->disposition = disp_del_at_next_stop;
4185 /* We've already printed what needs to be printed. */
4186 bs->print_it = print_it_done;
4187 break;
4188 }
4189 }
4190 else /* must_check_value == 0 */
4191 {
4192 /* This is a case where some watchpoint(s) triggered, but
4193 not at the address of this watchpoint, or else no
4194 watchpoint triggered after all. So don't print
4195 anything for this watchpoint. */
4196 bs->print_it = print_it_noop;
4197 bs->stop = 0;
4198 }
4199 }
4200}
4201
4202
4203/* Check conditions (condition proper, frame, thread and ignore count)
4204 of breakpoint referred to by BS. If we should not stop for this
4205 breakpoint, set BS->stop to 0. */
f431efe5 4206
18a18393
VP
4207static void
4208bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4209{
4210 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
4211 const struct bp_location *bl;
4212 struct breakpoint *b;
4213
4214 /* BS is built for existing struct breakpoint. */
f431efe5 4215 bl = bs->bp_location_at;
2bdf28a0 4216 gdb_assert (bl != NULL);
f431efe5 4217 b = bs->breakpoint_at;
2bdf28a0 4218 gdb_assert (b != NULL);
18a18393
VP
4219
4220 if (frame_id_p (b->frame_id)
edb3359d 4221 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4222 bs->stop = 0;
4223 else if (bs->stop)
4224 {
4225 int value_is_zero = 0;
60e1c644
PA
4226 struct expression *cond;
4227
60e1c644
PA
4228 if (is_watchpoint (b))
4229 cond = b->cond_exp;
4230 else
4231 cond = bl->cond;
4232
f431efe5 4233 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4234 {
60e1c644
PA
4235 int within_current_scope = 1;
4236
c5bc3a77
DJ
4237 /* We use value_mark and value_free_to_mark because it could
4238 be a long time before we return to the command level and
4239 call free_all_values. We can't call free_all_values
4240 because we might be in the middle of evaluating a
4241 function call. */
4242 struct value *mark = value_mark ();
4243
edb3359d
DJ
4244 /* Need to select the frame, with all that implies so that
4245 the conditions will have the right context. Because we
4246 use the frame, we will not see an inlined function's
4247 variables when we arrive at a breakpoint at the start
4248 of the inlined function; the current frame will be the
4249 call site. */
60e1c644
PA
4250 if (!is_watchpoint (b) || b->cond_exp_valid_block == NULL)
4251 select_frame (get_current_frame ());
4252 else
4253 {
4254 struct frame_info *frame;
4255
4256 /* For local watchpoint expressions, which particular
4257 instance of a local is being watched matters, so we
4258 keep track of the frame to evaluate the expression
4259 in. To evaluate the condition however, it doesn't
4260 really matter which instantiation of the function
4261 where the condition makes sense triggers the
4262 watchpoint. This allows an expression like "watch
4263 global if q > 10" set in `func', catch writes to
4264 global on all threads that call `func', or catch
4265 writes on all recursive calls of `func' by a single
4266 thread. We simply always evaluate the condition in
4267 the innermost frame that's executing where it makes
4268 sense to evaluate the condition. It seems
4269 intuitive. */
4270 frame = block_innermost_frame (b->cond_exp_valid_block);
4271 if (frame != NULL)
4272 select_frame (frame);
4273 else
4274 within_current_scope = 0;
4275 }
4276 if (within_current_scope)
4277 value_is_zero
4278 = catch_errors (breakpoint_cond_eval, cond,
4279 "Error in testing breakpoint condition:\n",
4280 RETURN_MASK_ALL);
4281 else
4282 {
4283 warning (_("Watchpoint condition cannot be tested "
4284 "in the current scope"));
4285 /* If we failed to set the right context for this
4286 watchpoint, unconditionally report it. */
4287 value_is_zero = 0;
4288 }
4a64f543 4289 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4290 value_free_to_mark (mark);
18a18393 4291 }
60e1c644
PA
4292
4293 if (cond && value_is_zero)
18a18393
VP
4294 {
4295 bs->stop = 0;
4296 }
4297 else if (b->thread != -1 && b->thread != thread_id)
4298 {
4299 bs->stop = 0;
4300 }
4301 else if (b->ignore_count > 0)
4302 {
4303 b->ignore_count--;
4304 annotate_ignore_count_change ();
4305 bs->stop = 0;
4a64f543 4306 /* Increase the hit count even though we don't stop. */
18a18393
VP
4307 ++(b->hit_count);
4308 }
4309 }
4310}
4311
4312
9709f61c 4313/* Get a bpstat associated with having just stopped at address
d983da9c 4314 BP_ADDR in thread PTID.
c906108c 4315
d983da9c 4316 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4317 don't understand this stop. Result is a chain of bpstat's such
4318 that:
c906108c 4319
c5aa993b 4320 if we don't understand the stop, the result is a null pointer.
c906108c 4321
c5aa993b 4322 if we understand why we stopped, the result is not null.
c906108c 4323
c5aa993b
JM
4324 Each element of the chain refers to a particular breakpoint or
4325 watchpoint at which we have stopped. (We may have stopped for
4326 several reasons concurrently.)
c906108c 4327
c5aa993b
JM
4328 Each element of the chain has valid next, breakpoint_at,
4329 commands, FIXME??? fields. */
c906108c
SS
4330
4331bpstat
6c95b8df
PA
4332bpstat_stop_status (struct address_space *aspace,
4333 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4334{
0d381245 4335 struct breakpoint *b = NULL;
afe38095 4336 struct bp_location *bl;
20874c92 4337 struct bp_location *loc;
5760d0ab
JK
4338 /* First item of allocated bpstat's. */
4339 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4340 /* Pointer to the last thing in the chain currently. */
5760d0ab 4341 bpstat bs;
20874c92 4342 int ix;
429374b8 4343 int need_remove_insert;
f431efe5 4344 int removed_any;
c906108c 4345
f431efe5
PA
4346 /* First, build the bpstat chain with locations that explain a
4347 target stop, while being careful to not set the target running,
4348 as that may invalidate locations (in particular watchpoint
4349 locations are recreated). Resuming will happen here with
4350 breakpoint conditions or watchpoint expressions that include
4351 inferior function calls. */
c5aa993b 4352
429374b8
JK
4353 ALL_BREAKPOINTS (b)
4354 {
4355 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4356 continue;
a5606eee 4357
429374b8
JK
4358 for (bl = b->loc; bl != NULL; bl = bl->next)
4359 {
4a64f543
MS
4360 /* For hardware watchpoints, we look only at the first
4361 location. The watchpoint_check function will work on the
4362 entire expression, not the individual locations. For
4363 read watchpoints, the watchpoints_triggered function has
4364 checked all locations already. */
429374b8
JK
4365 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4366 break;
18a18393 4367
429374b8
JK
4368 if (bl->shlib_disabled)
4369 continue;
c5aa993b 4370
429374b8
JK
4371 if (!bpstat_check_location (bl, aspace, bp_addr))
4372 continue;
c5aa993b 4373
4a64f543
MS
4374 /* Come here if it's a watchpoint, or if the break address
4375 matches. */
c5aa993b 4376
4a64f543
MS
4377 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4378 explain stop. */
c5aa993b 4379
f431efe5
PA
4380 /* Assume we stop. Should we find a watchpoint that is not
4381 actually triggered, or if the condition of the breakpoint
4382 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4383 bs->stop = 1;
4384 bs->print = 1;
d983da9c 4385
f431efe5
PA
4386 /* If this is a scope breakpoint, mark the associated
4387 watchpoint as triggered so that we will handle the
4388 out-of-scope event. We'll get to the watchpoint next
4389 iteration. */
4390 if (b->type == bp_watchpoint_scope)
4391 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
4392 }
4393 }
4394
4395 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4396 {
4397 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
4398 aspace, bp_addr))
4399 {
5760d0ab 4400 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4401 /* For hits of moribund locations, we should just proceed. */
4402 bs->stop = 0;
4403 bs->print = 0;
4404 bs->print_it = print_it_noop;
4405 }
4406 }
4407
f431efe5
PA
4408 /* Now go through the locations that caused the target to stop, and
4409 check whether we're interested in reporting this stop to higher
4410 layers, or whether we should resume the target transparently. */
4411
4412 removed_any = 0;
4413
5760d0ab 4414 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4415 {
4416 if (!bs->stop)
4417 continue;
4418
4419 bpstat_check_watchpoint (bs);
4420 if (!bs->stop)
4421 continue;
4422
4423 b = bs->breakpoint_at;
18a18393 4424
429374b8 4425 if (b->type == bp_thread_event || b->type == bp_overlay_event
aa7d318d 4426 || b->type == bp_longjmp_master
186c406b
TT
4427 || b->type == bp_std_terminate_master
4428 || b->type == bp_exception_master)
429374b8
JK
4429 /* We do not stop for these. */
4430 bs->stop = 0;
4431 else
4432 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4433
429374b8
JK
4434 if (bs->stop)
4435 {
4436 ++(b->hit_count);
c906108c 4437
4a64f543 4438 /* We will stop here. */
429374b8
JK
4439 if (b->disposition == disp_disable)
4440 {
4441 if (b->enable_state != bp_permanent)
4442 b->enable_state = bp_disabled;
f431efe5 4443 removed_any = 1;
429374b8
JK
4444 }
4445 if (b->silent)
4446 bs->print = 0;
4447 bs->commands = b->commands;
9add0f1b
TT
4448 incref_counted_command_line (bs->commands);
4449 bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4450 if (bs->commands_left
4451 && (strcmp ("silent", bs->commands_left->line) == 0
4452 || (xdb_commands
4453 && strcmp ("Q",
4454 bs->commands_left->line) == 0)))
429374b8 4455 {
9add0f1b 4456 bs->commands_left = bs->commands_left->next;
429374b8
JK
4457 bs->print = 0;
4458 }
429374b8
JK
4459 }
4460
4461 /* Print nothing for this entry if we dont stop or dont print. */
4462 if (bs->stop == 0 || bs->print == 0)
4463 bs->print_it = print_it_noop;
429374b8 4464 }
876fa593 4465
d983da9c
DJ
4466 /* If we aren't stopping, the value of some hardware watchpoint may
4467 not have changed, but the intermediate memory locations we are
4468 watching may have. Don't bother if we're stopping; this will get
4469 done later. */
d832cb68 4470 need_remove_insert = 0;
5760d0ab
JK
4471 if (! bpstat_causes_stop (bs_head))
4472 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4473 if (!bs->stop
f431efe5
PA
4474 && bs->breakpoint_at
4475 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4476 {
4a64f543 4477 update_watchpoint (bs->breakpoint_at, 0 /* don't reparse. */);
d832cb68 4478 need_remove_insert = 1;
d983da9c
DJ
4479 }
4480
d832cb68 4481 if (need_remove_insert)
2d134ed3 4482 update_global_location_list (1);
f431efe5
PA
4483 else if (removed_any)
4484 update_global_location_list (0);
d832cb68 4485
5760d0ab 4486 return bs_head;
c906108c 4487}
628fe4e4
JK
4488
4489static void
4490handle_jit_event (void)
4491{
4492 struct frame_info *frame;
4493 struct gdbarch *gdbarch;
4494
4495 /* Switch terminal for any messages produced by
4496 breakpoint_re_set. */
4497 target_terminal_ours_for_output ();
4498
4499 frame = get_current_frame ();
4500 gdbarch = get_frame_arch (frame);
4501
4502 jit_event_handler (gdbarch);
4503
4504 target_terminal_inferior ();
4505}
4506
4507/* Prepare WHAT final decision for infrun. */
4508
4509/* Decide what infrun needs to do with this bpstat. */
4510
c906108c 4511struct bpstat_what
fba45db2 4512bpstat_what (bpstat bs)
c906108c 4513{
c906108c 4514 struct bpstat_what retval;
628fe4e4
JK
4515 /* We need to defer calling `solib_add', as adding new symbols
4516 resets breakpoints, which in turn deletes breakpoint locations,
4517 and hence may clear unprocessed entries in the BS chain. */
4518 int shlib_event = 0;
4519 int jit_event = 0;
c906108c 4520
628fe4e4 4521 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4522 retval.call_dummy = STOP_NONE;
186c406b 4523 retval.is_longjmp = 0;
628fe4e4 4524
c906108c
SS
4525 for (; bs != NULL; bs = bs->next)
4526 {
628fe4e4
JK
4527 /* Extract this BS's action. After processing each BS, we check
4528 if its action overrides all we've seem so far. */
4529 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4530 enum bptype bptype;
4531
c906108c 4532 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4533 {
4534 /* I suspect this can happen if it was a momentary
4535 breakpoint which has since been deleted. */
4536 bptype = bp_none;
4537 }
f431efe5 4538 else if (bs->breakpoint_at == NULL)
628fe4e4 4539 bptype = bp_none;
20874c92 4540 else
f431efe5 4541 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4542
4543 switch (bptype)
c906108c
SS
4544 {
4545 case bp_none:
628fe4e4 4546 break;
c906108c
SS
4547 case bp_breakpoint:
4548 case bp_hardware_breakpoint:
4549 case bp_until:
4550 case bp_finish:
4551 if (bs->stop)
4552 {
4553 if (bs->print)
628fe4e4 4554 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4555 else
628fe4e4 4556 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4557 }
4558 else
628fe4e4 4559 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4560 break;
4561 case bp_watchpoint:
4562 case bp_hardware_watchpoint:
4563 case bp_read_watchpoint:
4564 case bp_access_watchpoint:
4565 if (bs->stop)
4566 {
4567 if (bs->print)
628fe4e4 4568 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4569 else
628fe4e4 4570 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4571 }
4572 else
628fe4e4
JK
4573 {
4574 /* There was a watchpoint, but we're not stopping.
4575 This requires no further action. */
4576 }
c906108c
SS
4577 break;
4578 case bp_longjmp:
186c406b 4579 case bp_exception:
628fe4e4 4580 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4581 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4582 break;
4583 case bp_longjmp_resume:
186c406b 4584 case bp_exception_resume:
628fe4e4 4585 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4586 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4587 break;
4588 case bp_step_resume:
4589 if (bs->stop)
628fe4e4
JK
4590 this_action = BPSTAT_WHAT_STEP_RESUME;
4591 else
c906108c 4592 {
628fe4e4
JK
4593 /* It is for the wrong frame. */
4594 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4595 }
c906108c 4596 break;
c906108c 4597 case bp_watchpoint_scope:
c4093a6a 4598 case bp_thread_event:
1900040c 4599 case bp_overlay_event:
0fd8e87f 4600 case bp_longjmp_master:
aa7d318d 4601 case bp_std_terminate_master:
186c406b 4602 case bp_exception_master:
628fe4e4 4603 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4604 break;
ce78b96d 4605 case bp_catchpoint:
c5aa993b
JM
4606 if (bs->stop)
4607 {
4608 if (bs->print)
628fe4e4 4609 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4610 else
628fe4e4 4611 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4612 }
4613 else
628fe4e4
JK
4614 {
4615 /* There was a catchpoint, but we're not stopping.
4616 This requires no further action. */
4617 }
4618 break;
4619 case bp_shlib_event:
4620 shlib_event = 1;
4621
4622 /* If requested, stop when the dynamic linker notifies GDB
4623 of events. This allows the user to get control and place
4624 breakpoints in initializer routines for dynamically
4625 loaded objects (among other things). */
4626 if (stop_on_solib_events)
4627 this_action = BPSTAT_WHAT_STOP_NOISY;
4628 else
4629 this_action = BPSTAT_WHAT_SINGLE;
4630 break;
4631 case bp_jit_event:
4632 jit_event = 1;
4633 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4634 break;
c906108c 4635 case bp_call_dummy:
53a5351d
JM
4636 /* Make sure the action is stop (silent or noisy),
4637 so infrun.c pops the dummy frame. */
aa7d318d 4638 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4639 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4640 break;
4641 case bp_std_terminate:
4642 /* Make sure the action is stop (silent or noisy),
4643 so infrun.c pops the dummy frame. */
aa7d318d 4644 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4645 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4646 break;
1042e4c0 4647 case bp_tracepoint:
7a697b8d 4648 case bp_fast_tracepoint:
0fb4aa4b 4649 case bp_static_tracepoint:
1042e4c0
SS
4650 /* Tracepoint hits should not be reported back to GDB, and
4651 if one got through somehow, it should have been filtered
4652 out already. */
4653 internal_error (__FILE__, __LINE__,
7a697b8d 4654 _("bpstat_what: tracepoint encountered"));
628fe4e4
JK
4655 default:
4656 internal_error (__FILE__, __LINE__,
4657 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4658 }
628fe4e4
JK
4659
4660 retval.main_action = max (retval.main_action, this_action);
c906108c 4661 }
628fe4e4
JK
4662
4663 if (shlib_event)
4664 {
4665 if (debug_infrun)
4666 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4667
4668 /* Check for any newly added shared libraries if we're supposed
4669 to be adding them automatically. */
4670
4671 /* Switch terminal for any messages produced by
4672 breakpoint_re_set. */
4673 target_terminal_ours_for_output ();
4674
4675#ifdef SOLIB_ADD
4676 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4677#else
4678 solib_add (NULL, 0, &current_target, auto_solib_add);
4679#endif
4680
4681 target_terminal_inferior ();
4682 }
4683
4684 if (jit_event)
4685 {
4686 if (debug_infrun)
4687 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4688
4689 handle_jit_event ();
4690 }
4691
c906108c
SS
4692 return retval;
4693}
4694
4695/* Nonzero if we should step constantly (e.g. watchpoints on machines
4696 without hardware support). This isn't related to a specific bpstat,
4697 just to things like whether watchpoints are set. */
4698
c5aa993b 4699int
fba45db2 4700bpstat_should_step (void)
c906108c
SS
4701{
4702 struct breakpoint *b;
cc59ec59 4703
c906108c 4704 ALL_BREAKPOINTS (b)
717a8278 4705 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4706 return 1;
c906108c
SS
4707 return 0;
4708}
4709
67822962
PA
4710int
4711bpstat_causes_stop (bpstat bs)
4712{
4713 for (; bs != NULL; bs = bs->next)
4714 if (bs->stop)
4715 return 1;
4716
4717 return 0;
4718}
4719
c906108c 4720\f
c5aa993b 4721
859825b8
JK
4722/* Print the LOC location out of the list of B->LOC locations. */
4723
0d381245
VP
4724static void print_breakpoint_location (struct breakpoint *b,
4725 struct bp_location *loc,
4726 char *wrap_indent,
4727 struct ui_stream *stb)
4728{
6c95b8df
PA
4729 struct cleanup *old_chain = save_current_program_space ();
4730
859825b8
JK
4731 if (loc != NULL && loc->shlib_disabled)
4732 loc = NULL;
4733
6c95b8df
PA
4734 if (loc != NULL)
4735 set_current_program_space (loc->pspace);
4736
859825b8 4737 if (b->source_file && loc)
0d381245
VP
4738 {
4739 struct symbol *sym
4740 = find_pc_sect_function (loc->address, loc->section);
4741 if (sym)
4742 {
4743 ui_out_text (uiout, "in ");
4744 ui_out_field_string (uiout, "func",
4745 SYMBOL_PRINT_NAME (sym));
4746 ui_out_wrap_hint (uiout, wrap_indent);
4747 ui_out_text (uiout, " at ");
4748 }
4749 ui_out_field_string (uiout, "file", b->source_file);
4750 ui_out_text (uiout, ":");
4751
4752 if (ui_out_is_mi_like_p (uiout))
4753 {
4754 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4755 char *fullname = symtab_to_fullname (sal.symtab);
4756
4757 if (fullname)
4758 ui_out_field_string (uiout, "fullname", fullname);
4759 }
4760
4761 ui_out_field_int (uiout, "line", b->line_number);
4762 }
859825b8 4763 else if (loc)
0d381245 4764 {
22e722e1
DJ
4765 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4766 demangle, "");
0d381245
VP
4767 ui_out_field_stream (uiout, "at", stb);
4768 }
859825b8
JK
4769 else
4770 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4771
4772 do_cleanups (old_chain);
0d381245
VP
4773}
4774
269b11a2
PA
4775static const char *
4776bptype_string (enum bptype type)
c906108c 4777{
c4093a6a
JM
4778 struct ep_type_description
4779 {
4780 enum bptype type;
4781 char *description;
4782 };
4783 static struct ep_type_description bptypes[] =
c906108c 4784 {
c5aa993b
JM
4785 {bp_none, "?deleted?"},
4786 {bp_breakpoint, "breakpoint"},
c906108c 4787 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4788 {bp_until, "until"},
4789 {bp_finish, "finish"},
4790 {bp_watchpoint, "watchpoint"},
c906108c 4791 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4792 {bp_read_watchpoint, "read watchpoint"},
4793 {bp_access_watchpoint, "acc watchpoint"},
4794 {bp_longjmp, "longjmp"},
4795 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4796 {bp_exception, "exception"},
4797 {bp_exception_resume, "exception resume"},
c5aa993b 4798 {bp_step_resume, "step resume"},
c5aa993b
JM
4799 {bp_watchpoint_scope, "watchpoint scope"},
4800 {bp_call_dummy, "call dummy"},
aa7d318d 4801 {bp_std_terminate, "std::terminate"},
c5aa993b 4802 {bp_shlib_event, "shlib events"},
c4093a6a 4803 {bp_thread_event, "thread events"},
1900040c 4804 {bp_overlay_event, "overlay events"},
0fd8e87f 4805 {bp_longjmp_master, "longjmp master"},
aa7d318d 4806 {bp_std_terminate_master, "std::terminate master"},
186c406b 4807 {bp_exception_master, "exception master"},
ce78b96d 4808 {bp_catchpoint, "catchpoint"},
1042e4c0 4809 {bp_tracepoint, "tracepoint"},
7a697b8d 4810 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4811 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4812 {bp_jit_event, "jit events"},
c5aa993b 4813 };
269b11a2
PA
4814
4815 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4816 || ((int) type != bptypes[(int) type].type))
4817 internal_error (__FILE__, __LINE__,
4818 _("bptypes table does not describe type #%d."),
4819 (int) type);
4820
4821 return bptypes[(int) type].description;
4822}
4823
4824/* Print B to gdb_stdout. */
4825
4826static void
4827print_one_breakpoint_location (struct breakpoint *b,
4828 struct bp_location *loc,
4829 int loc_number,
4830 struct bp_location **last_loc,
4831 int print_address_bits,
4832 int allflag)
4833{
4834 struct command_line *l;
c2c6d25f 4835 static char bpenables[] = "nynny";
c906108c 4836 char wrap_indent[80];
8b93c638
JM
4837 struct ui_stream *stb = ui_out_stream_new (uiout);
4838 struct cleanup *old_chain = make_cleanup_ui_out_stream_delete (stb);
3b31d625 4839 struct cleanup *bkpt_chain;
c906108c 4840
0d381245
VP
4841 int header_of_multiple = 0;
4842 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4843 struct value_print_options opts;
4844
4845 get_user_print_options (&opts);
0d381245
VP
4846
4847 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4848 /* See comment in print_one_breakpoint concerning treatment of
4849 breakpoints with single disabled location. */
0d381245
VP
4850 if (loc == NULL
4851 && (b->loc != NULL
4852 && (b->loc->next != NULL || !b->loc->enabled)))
4853 header_of_multiple = 1;
4854 if (loc == NULL)
4855 loc = b->loc;
4856
c4093a6a 4857 annotate_record ();
3b31d625 4858 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
c4093a6a
JM
4859
4860 /* 1 */
4861 annotate_field (0);
0d381245
VP
4862 if (part_of_multiple)
4863 {
4864 char *formatted;
0c6773c1 4865 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4866 ui_out_field_string (uiout, "number", formatted);
4867 xfree (formatted);
4868 }
4869 else
4870 {
4871 ui_out_field_int (uiout, "number", b->number);
4872 }
c4093a6a
JM
4873
4874 /* 2 */
4875 annotate_field (1);
0d381245
VP
4876 if (part_of_multiple)
4877 ui_out_field_skip (uiout, "type");
269b11a2
PA
4878 else
4879 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4880
4881 /* 3 */
4882 annotate_field (2);
0d381245
VP
4883 if (part_of_multiple)
4884 ui_out_field_skip (uiout, "disp");
4885 else
2cec12e5 4886 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4887
c4093a6a
JM
4888
4889 /* 4 */
4890 annotate_field (3);
0d381245 4891 if (part_of_multiple)
54e52265 4892 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4893 else
4a64f543
MS
4894 ui_out_field_fmt (uiout, "enabled", "%c",
4895 bpenables[(int) b->enable_state]);
54e52265 4896 ui_out_spaces (uiout, 2);
0d381245 4897
c4093a6a
JM
4898
4899 /* 5 and 6 */
4900 strcpy (wrap_indent, " ");
79a45b7d 4901 if (opts.addressprint)
75ac9d7b 4902 {
a6d9a66e 4903 if (print_address_bits <= 32)
75ac9d7b
MS
4904 strcat (wrap_indent, " ");
4905 else
4906 strcat (wrap_indent, " ");
4907 }
c906108c 4908
3086aeae 4909 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4910 {
4a64f543
MS
4911 /* Although the print_one can possibly print all locations,
4912 calling it here is not likely to get any nice result. So,
4913 make sure there's just one location. */
0d381245 4914 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4915 b->ops->print_one (b, last_loc);
0d381245 4916 }
3086aeae
DJ
4917 else
4918 switch (b->type)
4919 {
4920 case bp_none:
4921 internal_error (__FILE__, __LINE__,
e2e0b3e5 4922 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4923 break;
c906108c 4924
3086aeae
DJ
4925 case bp_watchpoint:
4926 case bp_hardware_watchpoint:
4927 case bp_read_watchpoint:
4928 case bp_access_watchpoint:
4929 /* Field 4, the address, is omitted (which makes the columns
4930 not line up too nicely with the headers, but the effect
4931 is relatively readable). */
79a45b7d 4932 if (opts.addressprint)
3086aeae
DJ
4933 ui_out_field_skip (uiout, "addr");
4934 annotate_field (5);
fa8a61dc 4935 ui_out_field_string (uiout, "what", b->exp_string);
3086aeae
DJ
4936 break;
4937
3086aeae
DJ
4938 case bp_breakpoint:
4939 case bp_hardware_breakpoint:
4940 case bp_until:
4941 case bp_finish:
4942 case bp_longjmp:
4943 case bp_longjmp_resume:
186c406b
TT
4944 case bp_exception:
4945 case bp_exception_resume:
3086aeae 4946 case bp_step_resume:
3086aeae
DJ
4947 case bp_watchpoint_scope:
4948 case bp_call_dummy:
aa7d318d 4949 case bp_std_terminate:
3086aeae
DJ
4950 case bp_shlib_event:
4951 case bp_thread_event:
4952 case bp_overlay_event:
0fd8e87f 4953 case bp_longjmp_master:
aa7d318d 4954 case bp_std_terminate_master:
186c406b 4955 case bp_exception_master:
1042e4c0 4956 case bp_tracepoint:
7a697b8d 4957 case bp_fast_tracepoint:
0fb4aa4b 4958 case bp_static_tracepoint:
4efc6507 4959 case bp_jit_event:
79a45b7d 4960 if (opts.addressprint)
3086aeae
DJ
4961 {
4962 annotate_field (4);
54e52265 4963 if (header_of_multiple)
0d381245 4964 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4965 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4966 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4967 else
5af949e3
UW
4968 ui_out_field_core_addr (uiout, "addr",
4969 loc->gdbarch, loc->address);
3086aeae
DJ
4970 }
4971 annotate_field (5);
0d381245
VP
4972 if (!header_of_multiple)
4973 print_breakpoint_location (b, loc, wrap_indent, stb);
4974 if (b->loc)
a6d9a66e 4975 *last_loc = b->loc;
3086aeae
DJ
4976 break;
4977 }
c906108c 4978
6c95b8df
PA
4979
4980 /* For backward compatibility, don't display inferiors unless there
4981 are several. */
4982 if (loc != NULL
4983 && !header_of_multiple
4984 && (allflag
4985 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4986 && (number_of_program_spaces () > 1
4987 || number_of_inferiors () > 1)
4a64f543
MS
4988 /* LOC is for existing B, it cannot be in
4989 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4990 && loc->owner->type != bp_catchpoint)))
4991 {
4992 struct inferior *inf;
4993 int first = 1;
4994
4995 for (inf = inferior_list; inf != NULL; inf = inf->next)
4996 {
4997 if (inf->pspace == loc->pspace)
4998 {
4999 if (first)
5000 {
5001 first = 0;
5002 ui_out_text (uiout, " inf ");
5003 }
5004 else
5005 ui_out_text (uiout, ", ");
5006 ui_out_text (uiout, plongest (inf->num));
5007 }
5008 }
5009 }
5010
4a306c9a 5011 if (!part_of_multiple)
c4093a6a 5012 {
4a306c9a
JB
5013 if (b->thread != -1)
5014 {
5015 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 5016 "stop only in" line a little further down. */
4a306c9a
JB
5017 ui_out_text (uiout, " thread ");
5018 ui_out_field_int (uiout, "thread", b->thread);
5019 }
5020 else if (b->task != 0)
5021 {
5022 ui_out_text (uiout, " task ");
5023 ui_out_field_int (uiout, "task", b->task);
5024 }
c4093a6a
JM
5025 }
5026
8b93c638 5027 ui_out_text (uiout, "\n");
c4093a6a 5028
0fb4aa4b
PA
5029 if (!part_of_multiple && b->static_trace_marker_id)
5030 {
5031 gdb_assert (b->type == bp_static_tracepoint);
5032
5033 ui_out_text (uiout, "\tmarker id is ");
5034 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
5035 b->static_trace_marker_id);
5036 ui_out_text (uiout, "\n");
5037 }
5038
0d381245 5039 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
5040 {
5041 annotate_field (6);
8b93c638 5042 ui_out_text (uiout, "\tstop only in stack frame at ");
818dd999
AC
5043 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
5044 the frame ID. */
5af949e3
UW
5045 ui_out_field_core_addr (uiout, "frame",
5046 b->gdbarch, b->frame_id.stack_addr);
8b93c638 5047 ui_out_text (uiout, "\n");
c4093a6a
JM
5048 }
5049
0d381245 5050 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
c4093a6a 5051 {
f7f9143b
JB
5052 /* We do not print the condition for Ada exception catchpoints
5053 because the condition is an internal implementation detail
5054 that we do not want to expose to the user. */
c4093a6a 5055 annotate_field (7);
d77f58be 5056 if (is_tracepoint (b))
1042e4c0
SS
5057 ui_out_text (uiout, "\ttrace only if ");
5058 else
5059 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
5060 ui_out_field_string (uiout, "cond", b->cond_string);
5061 ui_out_text (uiout, "\n");
5062 }
5063
0d381245 5064 if (!part_of_multiple && b->thread != -1)
c4093a6a 5065 {
4a64f543 5066 /* FIXME should make an annotation for this. */
8b93c638
JM
5067 ui_out_text (uiout, "\tstop only in thread ");
5068 ui_out_field_int (uiout, "thread", b->thread);
5069 ui_out_text (uiout, "\n");
c4093a6a
JM
5070 }
5071
63c715c6 5072 if (!part_of_multiple && b->hit_count)
c4093a6a 5073 {
4a64f543 5074 /* FIXME should make an annotation for this. */
8b93c638
JM
5075 if (ep_is_catchpoint (b))
5076 ui_out_text (uiout, "\tcatchpoint");
5077 else
5078 ui_out_text (uiout, "\tbreakpoint");
5079 ui_out_text (uiout, " already hit ");
5080 ui_out_field_int (uiout, "times", b->hit_count);
5081 if (b->hit_count == 1)
5082 ui_out_text (uiout, " time\n");
5083 else
5084 ui_out_text (uiout, " times\n");
c4093a6a
JM
5085 }
5086
4a64f543
MS
5087 /* Output the count also if it is zero, but only if this is mi.
5088 FIXME: Should have a better test for this. */
9dc5e2a9 5089 if (ui_out_is_mi_like_p (uiout))
63c715c6 5090 if (!part_of_multiple && b->hit_count == 0)
fb40c209 5091 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 5092
0d381245 5093 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
5094 {
5095 annotate_field (8);
8b93c638
JM
5096 ui_out_text (uiout, "\tignore next ");
5097 ui_out_field_int (uiout, "ignore", b->ignore_count);
5098 ui_out_text (uiout, " hits\n");
c4093a6a 5099 }
059fb39f 5100
9add0f1b 5101 l = b->commands ? b->commands->commands : NULL;
059fb39f 5102 if (!part_of_multiple && l)
c4093a6a 5103 {
3b31d625
EZ
5104 struct cleanup *script_chain;
5105
c4093a6a 5106 annotate_field (9);
3b31d625 5107 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 5108 print_command_lines (uiout, l, 4);
3b31d625 5109 do_cleanups (script_chain);
c4093a6a 5110 }
d24317b4 5111
1042e4c0
SS
5112 if (!part_of_multiple && b->pass_count)
5113 {
5114 annotate_field (10);
5115 ui_out_text (uiout, "\tpass count ");
5116 ui_out_field_int (uiout, "pass", b->pass_count);
5117 ui_out_text (uiout, " \n");
5118 }
5119
d24317b4
VP
5120 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5121 {
5122 if (b->addr_string)
5123 ui_out_field_string (uiout, "original-location", b->addr_string);
5124 else if (b->exp_string)
5125 ui_out_field_string (uiout, "original-location", b->exp_string);
5126 }
5127
3b31d625 5128 do_cleanups (bkpt_chain);
8b93c638 5129 do_cleanups (old_chain);
c4093a6a 5130}
c5aa993b 5131
0d381245
VP
5132static void
5133print_one_breakpoint (struct breakpoint *b,
4a64f543
MS
5134 struct bp_location **last_loc,
5135 int print_address_bits,
6c95b8df 5136 int allflag)
0d381245 5137{
6c95b8df
PA
5138 print_one_breakpoint_location (b, NULL, 0, last_loc,
5139 print_address_bits, allflag);
0d381245
VP
5140
5141 /* If this breakpoint has custom print function,
5142 it's already printed. Otherwise, print individual
5143 locations, if any. */
5144 if (b->ops == NULL || b->ops->print_one == NULL)
5145 {
4a64f543
MS
5146 /* If breakpoint has a single location that is disabled, we
5147 print it as if it had several locations, since otherwise it's
5148 hard to represent "breakpoint enabled, location disabled"
5149 situation.
5150
5151 Note that while hardware watchpoints have several locations
a3be7890 5152 internally, that's not a property exposed to user. */
0d381245 5153 if (b->loc
a5606eee 5154 && !is_hardware_watchpoint (b)
0d381245 5155 && (b->loc->next || !b->loc->enabled)
a5606eee 5156 && !ui_out_is_mi_like_p (uiout))
0d381245
VP
5157 {
5158 struct bp_location *loc;
5159 int n = 1;
5160 for (loc = b->loc; loc; loc = loc->next, ++n)
a6d9a66e 5161 print_one_breakpoint_location (b, loc, n, last_loc,
6c95b8df 5162 print_address_bits, allflag);
0d381245
VP
5163 }
5164 }
5165}
5166
a6d9a66e
UW
5167static int
5168breakpoint_address_bits (struct breakpoint *b)
5169{
5170 int print_address_bits = 0;
5171 struct bp_location *loc;
5172
5173 for (loc = b->loc; loc; loc = loc->next)
5174 {
c7437ca6
PA
5175 int addr_bit;
5176
5177 /* Software watchpoints that aren't watching memory don't have
5178 an address to print. */
5179 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5180 continue;
5181
5182 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5183 if (addr_bit > print_address_bits)
5184 print_address_bits = addr_bit;
5185 }
5186
5187 return print_address_bits;
5188}
0d381245 5189
c4093a6a
JM
5190struct captured_breakpoint_query_args
5191 {
5192 int bnum;
5193 };
c5aa993b 5194
c4093a6a 5195static int
2b65245e 5196do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5197{
5198 struct captured_breakpoint_query_args *args = data;
52f0bd74 5199 struct breakpoint *b;
a6d9a66e 5200 struct bp_location *dummy_loc = NULL;
cc59ec59 5201
c4093a6a
JM
5202 ALL_BREAKPOINTS (b)
5203 {
5204 if (args->bnum == b->number)
c5aa993b 5205 {
a6d9a66e 5206 int print_address_bits = breakpoint_address_bits (b);
cc59ec59 5207
6c95b8df 5208 print_one_breakpoint (b, &dummy_loc, print_address_bits, 0);
c4093a6a 5209 return GDB_RC_OK;
c5aa993b 5210 }
c4093a6a
JM
5211 }
5212 return GDB_RC_NONE;
5213}
c5aa993b 5214
c4093a6a 5215enum gdb_rc
4a64f543
MS
5216gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5217 char **error_message)
c4093a6a
JM
5218{
5219 struct captured_breakpoint_query_args args;
cc59ec59 5220
c4093a6a
JM
5221 args.bnum = bnum;
5222 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5223 an error. */
b0b13bb4
DJ
5224 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5225 error_message, RETURN_MASK_ALL) < 0)
5226 return GDB_RC_FAIL;
5227 else
5228 return GDB_RC_OK;
c4093a6a 5229}
c5aa993b 5230
7f3b0473 5231/* Return non-zero if B is user settable (breakpoints, watchpoints,
4a64f543 5232 catchpoints, et.al.). */
7f3b0473
AC
5233
5234static int
5235user_settable_breakpoint (const struct breakpoint *b)
5236{
5237 return (b->type == bp_breakpoint
ce78b96d 5238 || b->type == bp_catchpoint
7f3b0473 5239 || b->type == bp_hardware_breakpoint
d77f58be 5240 || is_tracepoint (b)
cc60f2e3 5241 || is_watchpoint (b));
7f3b0473 5242}
4a64f543 5243
09d682a4
TT
5244/* Return true if this breakpoint was set by the user, false if it is
5245 internal or momentary. */
5246
5247int
5248user_breakpoint_p (struct breakpoint *b)
5249{
5250 return user_settable_breakpoint (b) && b->number > 0;
5251}
5252
7f3b0473 5253/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5254 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5255 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5256 FILTER is non-NULL, call it on each breakpoint and only include the
5257 ones for which it returns non-zero. Return the total number of
5258 breakpoints listed. */
c906108c 5259
d77f58be 5260static int
4a64f543
MS
5261breakpoint_1 (int bnum, int allflag,
5262 int (*filter) (const struct breakpoint *))
c4093a6a 5263{
52f0bd74 5264 struct breakpoint *b;
a6d9a66e 5265 struct bp_location *last_loc = NULL;
7f3b0473 5266 int nr_printable_breakpoints;
3b31d625 5267 struct cleanup *bkpttbl_chain;
79a45b7d 5268 struct value_print_options opts;
a6d9a66e 5269 int print_address_bits = 0;
269b11a2
PA
5270 int print_type_col_width = 14;
5271
79a45b7d
TT
5272 get_user_print_options (&opts);
5273
4a64f543
MS
5274 /* Compute the number of rows in the table, as well as the size
5275 required for address fields. */
7f3b0473
AC
5276 nr_printable_breakpoints = 0;
5277 ALL_BREAKPOINTS (b)
5278 if (bnum == -1
5279 || bnum == b->number)
5280 {
d77f58be
SS
5281 /* If we have a filter, only list the breakpoints it accepts. */
5282 if (filter && !filter (b))
5283 continue;
5284
09d682a4 5285 if (allflag || user_breakpoint_p (b))
a6d9a66e 5286 {
269b11a2
PA
5287 int addr_bit, type_len;
5288
5289 addr_bit = breakpoint_address_bits (b);
a6d9a66e
UW
5290 if (addr_bit > print_address_bits)
5291 print_address_bits = addr_bit;
5292
269b11a2
PA
5293 type_len = strlen (bptype_string (b->type));
5294 if (type_len > print_type_col_width)
5295 print_type_col_width = type_len;
5296
a6d9a66e
UW
5297 nr_printable_breakpoints++;
5298 }
7f3b0473
AC
5299 }
5300
79a45b7d 5301 if (opts.addressprint)
3b31d625 5302 bkpttbl_chain
3e43a32a
MS
5303 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5304 nr_printable_breakpoints,
3b31d625 5305 "BreakpointTable");
8b93c638 5306 else
3b31d625 5307 bkpttbl_chain
3e43a32a
MS
5308 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5309 nr_printable_breakpoints,
3b31d625 5310 "BreakpointTable");
8b93c638 5311
7f3b0473 5312 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5313 annotate_breakpoints_headers ();
5314 if (nr_printable_breakpoints > 0)
5315 annotate_field (0);
4a64f543 5316 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5317 if (nr_printable_breakpoints > 0)
5318 annotate_field (1);
269b11a2 5319 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5320 "type", "Type"); /* 2 */
d7faa9e7
AC
5321 if (nr_printable_breakpoints > 0)
5322 annotate_field (2);
4a64f543 5323 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5324 if (nr_printable_breakpoints > 0)
5325 annotate_field (3);
54e52265 5326 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5327 if (opts.addressprint)
7f3b0473 5328 {
d7faa9e7
AC
5329 if (nr_printable_breakpoints > 0)
5330 annotate_field (4);
a6d9a66e 5331 if (print_address_bits <= 32)
4a64f543
MS
5332 ui_out_table_header (uiout, 10, ui_left,
5333 "addr", "Address"); /* 5 */
7f3b0473 5334 else
4a64f543
MS
5335 ui_out_table_header (uiout, 18, ui_left,
5336 "addr", "Address"); /* 5 */
7f3b0473 5337 }
d7faa9e7
AC
5338 if (nr_printable_breakpoints > 0)
5339 annotate_field (5);
5340 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5341 ui_out_table_body (uiout);
5342 if (nr_printable_breakpoints > 0)
5343 annotate_breakpoints_table ();
7f3b0473 5344
c4093a6a 5345 ALL_BREAKPOINTS (b)
bad56014
TT
5346 {
5347 QUIT;
c4093a6a
JM
5348 if (bnum == -1
5349 || bnum == b->number)
5350 {
d77f58be
SS
5351 /* If we have a filter, only list the breakpoints it accepts. */
5352 if (filter && !filter (b))
5353 continue;
5354
c4093a6a 5355 /* We only print out user settable breakpoints unless the
4a64f543 5356 allflag is set. */
09d682a4 5357 if (allflag || user_breakpoint_p (b))
6c95b8df 5358 print_one_breakpoint (b, &last_loc, print_address_bits, allflag);
c4093a6a 5359 }
bad56014 5360 }
c4093a6a 5361
3b31d625 5362 do_cleanups (bkpttbl_chain);
698384cd 5363
7f3b0473 5364 if (nr_printable_breakpoints == 0)
c906108c 5365 {
4a64f543
MS
5366 /* If there's a filter, let the caller decide how to report
5367 empty list. */
d77f58be
SS
5368 if (!filter)
5369 {
5370 if (bnum == -1)
5371 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5372 else
4a64f543
MS
5373 ui_out_message (uiout, 0,
5374 "No breakpoint or watchpoint number %d.\n",
d77f58be
SS
5375 bnum);
5376 }
c906108c
SS
5377 }
5378 else
c4093a6a 5379 {
a6d9a66e
UW
5380 if (last_loc && !server_command)
5381 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5382 }
c906108c 5383
4a64f543 5384 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5385 there have been breakpoints? */
c906108c 5386 annotate_breakpoints_table_end ();
d77f58be
SS
5387
5388 return nr_printable_breakpoints;
c906108c
SS
5389}
5390
ad443146
SS
5391/* Display the value of default-collect in a way that is generally
5392 compatible with the breakpoint list. */
5393
5394static void
5395default_collect_info (void)
5396{
5397 /* If it has no value (which is frequently the case), say nothing; a
5398 message like "No default-collect." gets in user's face when it's
5399 not wanted. */
5400 if (!*default_collect)
5401 return;
5402
5403 /* The following phrase lines up nicely with per-tracepoint collect
5404 actions. */
5405 ui_out_text (uiout, "default collect ");
5406 ui_out_field_string (uiout, "default-collect", default_collect);
5407 ui_out_text (uiout, " \n");
5408}
5409
c906108c 5410static void
fba45db2 5411breakpoints_info (char *bnum_exp, int from_tty)
c906108c
SS
5412{
5413 int bnum = -1;
5414
5415 if (bnum_exp)
bb518678 5416 bnum = parse_and_eval_long (bnum_exp);
c906108c 5417
d77f58be 5418 breakpoint_1 (bnum, 0, NULL);
ad443146
SS
5419
5420 default_collect_info ();
d77f58be
SS
5421}
5422
5423static void
5424watchpoints_info (char *wpnum_exp, int from_tty)
5425{
5426 int wpnum = -1, num_printed;
5427
5428 if (wpnum_exp)
5429 wpnum = parse_and_eval_long (wpnum_exp);
5430
5431 num_printed = breakpoint_1 (wpnum, 0, is_watchpoint);
5432
5433 if (num_printed == 0)
5434 {
5435 if (wpnum == -1)
5436 ui_out_message (uiout, 0, "No watchpoints.\n");
5437 else
5438 ui_out_message (uiout, 0, "No watchpoint number %d.\n", wpnum);
5439 }
c906108c
SS
5440}
5441
7a292a7a 5442static void
fba45db2 5443maintenance_info_breakpoints (char *bnum_exp, int from_tty)
c906108c
SS
5444{
5445 int bnum = -1;
5446
5447 if (bnum_exp)
bb518678 5448 bnum = parse_and_eval_long (bnum_exp);
c906108c 5449
d77f58be 5450 breakpoint_1 (bnum, 1, NULL);
ad443146
SS
5451
5452 default_collect_info ();
c906108c
SS
5453}
5454
0d381245 5455static int
714835d5 5456breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5457 struct program_space *pspace,
714835d5 5458 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5459{
5460 struct bp_location *bl = b->loc;
cc59ec59 5461
0d381245
VP
5462 for (; bl; bl = bl->next)
5463 {
6c95b8df
PA
5464 if (bl->pspace == pspace
5465 && bl->address == pc
0d381245
VP
5466 && (!overlay_debugging || bl->section == section))
5467 return 1;
5468 }
5469 return 0;
5470}
5471
6c95b8df
PA
5472/* Print a message describing any breakpoints set at PC. This
5473 concerns with logical breakpoints, so we match program spaces, not
5474 address spaces. */
c906108c
SS
5475
5476static void
6c95b8df
PA
5477describe_other_breakpoints (struct gdbarch *gdbarch,
5478 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5479 struct obj_section *section, int thread)
c906108c 5480{
52f0bd74
AC
5481 int others = 0;
5482 struct breakpoint *b;
c906108c
SS
5483
5484 ALL_BREAKPOINTS (b)
6c95b8df 5485 others += breakpoint_has_pc (b, pspace, pc, section);
c906108c
SS
5486 if (others > 0)
5487 {
a3f17187
AC
5488 if (others == 1)
5489 printf_filtered (_("Note: breakpoint "));
5490 else /* if (others == ???) */
5491 printf_filtered (_("Note: breakpoints "));
c906108c 5492 ALL_BREAKPOINTS (b)
6c95b8df 5493 if (breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5494 {
5495 others--;
5496 printf_filtered ("%d", b->number);
5497 if (b->thread == -1 && thread != -1)
5498 printf_filtered (" (all threads)");
5499 else if (b->thread != -1)
5500 printf_filtered (" (thread %d)", b->thread);
5501 printf_filtered ("%s%s ",
059fb39f 5502 ((b->enable_state == bp_disabled
8bea4e01
UW
5503 || b->enable_state == bp_call_disabled
5504 || b->enable_state == bp_startup_disabled)
0d381245
VP
5505 ? " (disabled)"
5506 : b->enable_state == bp_permanent
5507 ? " (permanent)"
5508 : ""),
5509 (others > 1) ? ","
5510 : ((others == 1) ? " and" : ""));
5511 }
a3f17187 5512 printf_filtered (_("also set at pc "));
5af949e3 5513 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5514 printf_filtered (".\n");
5515 }
5516}
5517\f
5518/* Set the default place to put a breakpoint
5519 for the `break' command with no arguments. */
5520
5521void
6c95b8df
PA
5522set_default_breakpoint (int valid, struct program_space *pspace,
5523 CORE_ADDR addr, struct symtab *symtab,
fba45db2 5524 int line)
c906108c
SS
5525{
5526 default_breakpoint_valid = valid;
6c95b8df 5527 default_breakpoint_pspace = pspace;
c906108c
SS
5528 default_breakpoint_address = addr;
5529 default_breakpoint_symtab = symtab;
5530 default_breakpoint_line = line;
5531}
5532
e4f237da
KB
5533/* Return true iff it is meaningful to use the address member of
5534 BPT. For some breakpoint types, the address member is irrelevant
5535 and it makes no sense to attempt to compare it to other addresses
5536 (or use it for any other purpose either).
5537
4a64f543
MS
5538 More specifically, each of the following breakpoint types will
5539 always have a zero valued address and we don't want to mark
5540 breakpoints of any of these types to be a duplicate of an actual
5541 breakpoint at address zero:
e4f237da
KB
5542
5543 bp_watchpoint
2d134ed3
PA
5544 bp_catchpoint
5545
5546*/
e4f237da
KB
5547
5548static int
5549breakpoint_address_is_meaningful (struct breakpoint *bpt)
5550{
5551 enum bptype type = bpt->type;
5552
2d134ed3
PA
5553 return (type != bp_watchpoint && type != bp_catchpoint);
5554}
5555
5556/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5557 true if LOC1 and LOC2 represent the same watchpoint location. */
5558
5559static int
4a64f543
MS
5560watchpoint_locations_match (struct bp_location *loc1,
5561 struct bp_location *loc2)
2d134ed3 5562{
2bdf28a0
JK
5563 /* Both of them must not be in moribund_locations. */
5564 gdb_assert (loc1->owner != NULL);
5565 gdb_assert (loc2->owner != NULL);
5566
4a64f543
MS
5567 /* If the target can evaluate the condition expression in hardware,
5568 then we we need to insert both watchpoints even if they are at
5569 the same place. Otherwise the watchpoint will only trigger when
5570 the condition of whichever watchpoint was inserted evaluates to
5571 true, not giving a chance for GDB to check the condition of the
5572 other watchpoint. */
0cf6dd15 5573 if ((loc1->owner->cond_exp
4a64f543
MS
5574 && target_can_accel_watchpoint_condition (loc1->address,
5575 loc1->length,
0cf6dd15
TJB
5576 loc1->watchpoint_type,
5577 loc1->owner->cond_exp))
5578 || (loc2->owner->cond_exp
4a64f543
MS
5579 && target_can_accel_watchpoint_condition (loc2->address,
5580 loc2->length,
0cf6dd15
TJB
5581 loc2->watchpoint_type,
5582 loc2->owner->cond_exp)))
5583 return 0;
5584
85d721b8
PA
5585 /* Note that this checks the owner's type, not the location's. In
5586 case the target does not support read watchpoints, but does
5587 support access watchpoints, we'll have bp_read_watchpoint
5588 watchpoints with hw_access locations. Those should be considered
5589 duplicates of hw_read locations. The hw_read locations will
5590 become hw_access locations later. */
2d134ed3
PA
5591 return (loc1->owner->type == loc2->owner->type
5592 && loc1->pspace->aspace == loc2->pspace->aspace
5593 && loc1->address == loc2->address
5594 && loc1->length == loc2->length);
e4f237da
KB
5595}
5596
6c95b8df
PA
5597/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5598 same breakpoint location. In most targets, this can only be true
5599 if ASPACE1 matches ASPACE2. On targets that have global
5600 breakpoints, the address space doesn't really matter. */
5601
5602static int
5603breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5604 struct address_space *aspace2, CORE_ADDR addr2)
5605{
5606 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5607 || aspace1 == aspace2)
5608 && addr1 == addr2);
5609}
5610
2d134ed3
PA
5611/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5612 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5613 represent the same location. */
5614
5615static int
4a64f543
MS
5616breakpoint_locations_match (struct bp_location *loc1,
5617 struct bp_location *loc2)
2d134ed3 5618{
2bdf28a0
JK
5619 int hw_point1, hw_point2;
5620
5621 /* Both of them must not be in moribund_locations. */
5622 gdb_assert (loc1->owner != NULL);
5623 gdb_assert (loc2->owner != NULL);
5624
5625 hw_point1 = is_hardware_watchpoint (loc1->owner);
5626 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5627
5628 if (hw_point1 != hw_point2)
5629 return 0;
5630 else if (hw_point1)
5631 return watchpoint_locations_match (loc1, loc2);
5632 else
5633 return breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5634 loc2->pspace->aspace, loc2->address);
5635}
5636
76897487
KB
5637static void
5638breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5639 int bnum, int have_bnum)
5640{
5641 char astr1[40];
5642 char astr2[40];
5643
bb599908
PH
5644 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5645 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5646 if (have_bnum)
8a3fe4f8 5647 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5648 bnum, astr1, astr2);
5649 else
8a3fe4f8 5650 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5651}
5652
4a64f543
MS
5653/* Adjust a breakpoint's address to account for architectural
5654 constraints on breakpoint placement. Return the adjusted address.
5655 Note: Very few targets require this kind of adjustment. For most
5656 targets, this function is simply the identity function. */
76897487
KB
5657
5658static CORE_ADDR
a6d9a66e
UW
5659adjust_breakpoint_address (struct gdbarch *gdbarch,
5660 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5661{
a6d9a66e 5662 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5663 {
5664 /* Very few targets need any kind of breakpoint adjustment. */
5665 return bpaddr;
5666 }
88f7da05
KB
5667 else if (bptype == bp_watchpoint
5668 || bptype == bp_hardware_watchpoint
5669 || bptype == bp_read_watchpoint
5670 || bptype == bp_access_watchpoint
fe798b75 5671 || bptype == bp_catchpoint)
88f7da05
KB
5672 {
5673 /* Watchpoints and the various bp_catch_* eventpoints should not
5674 have their addresses modified. */
5675 return bpaddr;
5676 }
76897487
KB
5677 else
5678 {
5679 CORE_ADDR adjusted_bpaddr;
5680
5681 /* Some targets have architectural constraints on the placement
5682 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5683 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5684
5685 /* An adjusted breakpoint address can significantly alter
5686 a user's expectations. Print a warning if an adjustment
5687 is required. */
5688 if (adjusted_bpaddr != bpaddr)
5689 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5690
5691 return adjusted_bpaddr;
5692 }
5693}
5694
7cc221ef
DJ
5695/* Allocate a struct bp_location. */
5696
26bb91f3 5697static struct bp_location *
39d61571 5698allocate_bp_location (struct breakpoint *bpt)
7cc221ef 5699{
afe38095 5700 struct bp_location *loc;
7cc221ef
DJ
5701
5702 loc = xmalloc (sizeof (struct bp_location));
5703 memset (loc, 0, sizeof (*loc));
5704
e049a4b5 5705 loc->owner = bpt;
511a6cd4 5706 loc->cond = NULL;
0d381245
VP
5707 loc->shlib_disabled = 0;
5708 loc->enabled = 1;
e049a4b5 5709
39d61571 5710 switch (bpt->type)
e049a4b5
DJ
5711 {
5712 case bp_breakpoint:
5713 case bp_until:
5714 case bp_finish:
5715 case bp_longjmp:
5716 case bp_longjmp_resume:
186c406b
TT
5717 case bp_exception:
5718 case bp_exception_resume:
e049a4b5 5719 case bp_step_resume:
e049a4b5
DJ
5720 case bp_watchpoint_scope:
5721 case bp_call_dummy:
aa7d318d 5722 case bp_std_terminate:
e049a4b5
DJ
5723 case bp_shlib_event:
5724 case bp_thread_event:
5725 case bp_overlay_event:
4efc6507 5726 case bp_jit_event:
0fd8e87f 5727 case bp_longjmp_master:
aa7d318d 5728 case bp_std_terminate_master:
186c406b 5729 case bp_exception_master:
e049a4b5
DJ
5730 loc->loc_type = bp_loc_software_breakpoint;
5731 break;
5732 case bp_hardware_breakpoint:
5733 loc->loc_type = bp_loc_hardware_breakpoint;
5734 break;
5735 case bp_hardware_watchpoint:
5736 case bp_read_watchpoint:
5737 case bp_access_watchpoint:
5738 loc->loc_type = bp_loc_hardware_watchpoint;
5739 break;
5740 case bp_watchpoint:
ce78b96d 5741 case bp_catchpoint:
15c3d785
PA
5742 case bp_tracepoint:
5743 case bp_fast_tracepoint:
0fb4aa4b 5744 case bp_static_tracepoint:
e049a4b5
DJ
5745 loc->loc_type = bp_loc_other;
5746 break;
5747 default:
e2e0b3e5 5748 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5749 }
5750
f431efe5 5751 loc->refc = 1;
7cc221ef
DJ
5752 return loc;
5753}
5754
f431efe5
PA
5755static void
5756free_bp_location (struct bp_location *loc)
fe3f5fa8
VP
5757{
5758 if (loc->cond)
5759 xfree (loc->cond);
74960c60
VP
5760
5761 if (loc->function_name)
5762 xfree (loc->function_name);
f431efe5 5763
fe3f5fa8
VP
5764 xfree (loc);
5765}
5766
f431efe5
PA
5767/* Increment reference count. */
5768
5769static void
5770incref_bp_location (struct bp_location *bl)
5771{
5772 ++bl->refc;
5773}
5774
5775/* Decrement reference count. If the reference count reaches 0,
5776 destroy the bp_location. Sets *BLP to NULL. */
5777
5778static void
5779decref_bp_location (struct bp_location **blp)
5780{
0807b50c
PA
5781 gdb_assert ((*blp)->refc > 0);
5782
f431efe5
PA
5783 if (--(*blp)->refc == 0)
5784 free_bp_location (*blp);
5785 *blp = NULL;
5786}
5787
4a64f543
MS
5788/* Helper to set_raw_breakpoint below. Creates a breakpoint that has
5789 type BPTYPE and has no locations as yet. */
5790/* This function is used in gdbtk sources and thus can not be made
5791 static. */
c906108c 5792
c40e75cd 5793static struct breakpoint *
a6d9a66e
UW
5794set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
5795 enum bptype bptype)
c906108c 5796{
52f0bd74 5797 struct breakpoint *b, *b1;
c906108c
SS
5798
5799 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
5800 memset (b, 0, sizeof (*b));
2219d63c 5801
4d28f7a8 5802 b->type = bptype;
a6d9a66e 5803 b->gdbarch = gdbarch;
c906108c
SS
5804 b->language = current_language->la_language;
5805 b->input_radix = input_radix;
5806 b->thread = -1;
b5de0fa7 5807 b->enable_state = bp_enabled;
c906108c
SS
5808 b->next = 0;
5809 b->silent = 0;
5810 b->ignore_count = 0;
5811 b->commands = NULL;
818dd999 5812 b->frame_id = null_frame_id;
3a3e9ee3 5813 b->forked_inferior_pid = null_ptid;
c906108c 5814 b->exec_pathname = NULL;
a96d9b2e 5815 b->syscalls_to_be_caught = NULL;
3086aeae 5816 b->ops = NULL;
0d381245 5817 b->condition_not_parsed = 0;
84f4c1fe 5818 b->py_bp_object = NULL;
c906108c 5819
4a64f543
MS
5820 /* Add this breakpoint to the end of the chain so that a list of
5821 breakpoints will come out in order of increasing numbers. */
c906108c
SS
5822
5823 b1 = breakpoint_chain;
5824 if (b1 == 0)
5825 breakpoint_chain = b;
5826 else
5827 {
5828 while (b1->next)
5829 b1 = b1->next;
5830 b1->next = b;
5831 }
0d381245
VP
5832 return b;
5833}
5834
5835/* Initialize loc->function_name. */
5836static void
5837set_breakpoint_location_function (struct bp_location *loc)
5838{
2bdf28a0
JK
5839 gdb_assert (loc->owner != NULL);
5840
0d381245 5841 if (loc->owner->type == bp_breakpoint
1042e4c0 5842 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5843 || is_tracepoint (loc->owner))
0d381245
VP
5844 {
5845 find_pc_partial_function (loc->address, &(loc->function_name),
5846 NULL, NULL);
5847 if (loc->function_name)
5848 loc->function_name = xstrdup (loc->function_name);
5849 }
5850}
5851
a6d9a66e
UW
5852/* Attempt to determine architecture of location identified by SAL. */
5853static struct gdbarch *
5854get_sal_arch (struct symtab_and_line sal)
5855{
5856 if (sal.section)
5857 return get_objfile_arch (sal.section->objfile);
5858 if (sal.symtab)
5859 return get_objfile_arch (sal.symtab->objfile);
5860
5861 return NULL;
5862}
5863
0d381245
VP
5864/* set_raw_breakpoint is a low level routine for allocating and
5865 partially initializing a breakpoint of type BPTYPE. The newly
5866 created breakpoint's address, section, source file name, and line
5867 number are provided by SAL. The newly created and partially
5868 initialized breakpoint is added to the breakpoint chain and
5869 is also returned as the value of this function.
5870
5871 It is expected that the caller will complete the initialization of
5872 the newly created breakpoint struct as well as output any status
5873 information regarding the creation of a new breakpoint. In
5874 particular, set_raw_breakpoint does NOT set the breakpoint
5875 number! Care should be taken to not allow an error to occur
5876 prior to completing the initialization of the breakpoint. If this
5877 should happen, a bogus breakpoint will be left on the chain. */
5878
63c252f8 5879struct breakpoint *
a6d9a66e
UW
5880set_raw_breakpoint (struct gdbarch *gdbarch,
5881 struct symtab_and_line sal, enum bptype bptype)
0d381245 5882{
4a64f543
MS
5883 struct breakpoint *b = set_raw_breakpoint_without_location (gdbarch,
5884 bptype);
0d381245 5885 CORE_ADDR adjusted_address;
a6d9a66e
UW
5886 struct gdbarch *loc_gdbarch;
5887
5888 loc_gdbarch = get_sal_arch (sal);
5889 if (!loc_gdbarch)
5890 loc_gdbarch = b->gdbarch;
0d381245 5891
6c95b8df
PA
5892 if (bptype != bp_catchpoint)
5893 gdb_assert (sal.pspace != NULL);
5894
0d381245
VP
5895 /* Adjust the breakpoint's address prior to allocating a location.
5896 Once we call allocate_bp_location(), that mostly uninitialized
5897 location will be placed on the location chain. Adjustment of the
8defab1a 5898 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
5899 not want its scan of the location chain to find a breakpoint and
5900 location that's only been partially initialized. */
4a64f543
MS
5901 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5902 sal.pc, b->type);
0d381245 5903
39d61571 5904 b->loc = allocate_bp_location (b);
a6d9a66e 5905 b->loc->gdbarch = loc_gdbarch;
0d381245
VP
5906 b->loc->requested_address = sal.pc;
5907 b->loc->address = adjusted_address;
6c95b8df
PA
5908 b->loc->pspace = sal.pspace;
5909
5910 /* Store the program space that was used to set the breakpoint, for
5911 breakpoint resetting. */
5912 b->pspace = sal.pspace;
0d381245
VP
5913
5914 if (sal.symtab == NULL)
5915 b->source_file = NULL;
5916 else
1b36a34b 5917 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5918 b->loc->section = sal.section;
5919 b->line_number = sal.line;
5920
5921 set_breakpoint_location_function (b->loc);
c906108c 5922
c906108c
SS
5923 breakpoints_changed ();
5924
5925 return b;
5926}
5927
c2c6d25f
JM
5928
5929/* Note that the breakpoint object B describes a permanent breakpoint
5930 instruction, hard-wired into the inferior's code. */
5931void
5932make_breakpoint_permanent (struct breakpoint *b)
5933{
0d381245 5934 struct bp_location *bl;
cc59ec59 5935
b5de0fa7 5936 b->enable_state = bp_permanent;
c2c6d25f 5937
4a64f543
MS
5938 /* By definition, permanent breakpoints are already present in the
5939 code. Mark all locations as inserted. For now,
5940 make_breakpoint_permanent is called in just one place, so it's
5941 hard to say if it's reasonable to have permanent breakpoint with
5942 multiple locations or not, but it's easy to implmement. */
0d381245
VP
5943 for (bl = b->loc; bl; bl = bl->next)
5944 bl->inserted = 1;
c2c6d25f
JM
5945}
5946
53a5351d 5947/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5948 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5949 initiated the operation. */
c906108c
SS
5950
5951void
186c406b 5952set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5953{
35df4500 5954 struct breakpoint *b, *b_tmp;
186c406b 5955 int thread = tp->num;
0fd8e87f
UW
5956
5957 /* To avoid having to rescan all objfile symbols at every step,
5958 we maintain a list of continually-inserted but always disabled
5959 longjmp "master" breakpoints. Here, we simply create momentary
5960 clones of those and enable them for the requested thread. */
35df4500 5961 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5962 if (b->pspace == current_program_space
186c406b
TT
5963 && (b->type == bp_longjmp_master
5964 || b->type == bp_exception_master))
0fd8e87f
UW
5965 {
5966 struct breakpoint *clone = clone_momentary_breakpoint (b);
cc59ec59 5967
186c406b 5968 clone->type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
0fd8e87f
UW
5969 clone->thread = thread;
5970 }
186c406b
TT
5971
5972 tp->initiating_frame = frame;
c906108c
SS
5973}
5974
611c83ae 5975/* Delete all longjmp breakpoints from THREAD. */
c906108c 5976void
611c83ae 5977delete_longjmp_breakpoint (int thread)
c906108c 5978{
35df4500 5979 struct breakpoint *b, *b_tmp;
c906108c 5980
35df4500 5981 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5982 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5983 {
5984 if (b->thread == thread)
5985 delete_breakpoint (b);
5986 }
c906108c
SS
5987}
5988
1900040c
MS
5989void
5990enable_overlay_breakpoints (void)
5991{
52f0bd74 5992 struct breakpoint *b;
1900040c
MS
5993
5994 ALL_BREAKPOINTS (b)
5995 if (b->type == bp_overlay_event)
5996 {
5997 b->enable_state = bp_enabled;
b60e7edf 5998 update_global_location_list (1);
c02f5703 5999 overlay_events_enabled = 1;
1900040c
MS
6000 }
6001}
6002
6003void
6004disable_overlay_breakpoints (void)
6005{
52f0bd74 6006 struct breakpoint *b;
1900040c
MS
6007
6008 ALL_BREAKPOINTS (b)
6009 if (b->type == bp_overlay_event)
6010 {
6011 b->enable_state = bp_disabled;
b60e7edf 6012 update_global_location_list (0);
c02f5703 6013 overlay_events_enabled = 0;
1900040c
MS
6014 }
6015}
6016
aa7d318d
TT
6017/* Set an active std::terminate breakpoint for each std::terminate
6018 master breakpoint. */
6019void
6020set_std_terminate_breakpoint (void)
6021{
35df4500 6022 struct breakpoint *b, *b_tmp;
aa7d318d 6023
35df4500 6024 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6025 if (b->pspace == current_program_space
6026 && b->type == bp_std_terminate_master)
6027 {
6028 struct breakpoint *clone = clone_momentary_breakpoint (b);
6029 clone->type = bp_std_terminate;
6030 }
6031}
6032
6033/* Delete all the std::terminate breakpoints. */
6034void
6035delete_std_terminate_breakpoint (void)
6036{
35df4500 6037 struct breakpoint *b, *b_tmp;
aa7d318d 6038
35df4500 6039 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6040 if (b->type == bp_std_terminate)
6041 delete_breakpoint (b);
6042}
6043
c4093a6a 6044struct breakpoint *
a6d9a66e 6045create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6046{
6047 struct breakpoint *b;
c4093a6a 6048
a6d9a66e 6049 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
c4093a6a 6050
b5de0fa7 6051 b->enable_state = bp_enabled;
c4093a6a 6052 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6053 b->addr_string
6054 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6055
b60e7edf 6056 update_global_location_list_nothrow (1);
74960c60 6057
c4093a6a
JM
6058 return b;
6059}
6060
6061void
6062remove_thread_event_breakpoints (void)
6063{
35df4500 6064 struct breakpoint *b, *b_tmp;
c4093a6a 6065
35df4500 6066 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6067 if (b->type == bp_thread_event
6068 && b->loc->pspace == current_program_space)
c4093a6a
JM
6069 delete_breakpoint (b);
6070}
6071
0101ce28
JJ
6072struct captured_parse_breakpoint_args
6073 {
6074 char **arg_p;
6075 struct symtabs_and_lines *sals_p;
6076 char ***addr_string_p;
6077 int *not_found_ptr;
6078 };
6079
6080struct lang_and_radix
6081 {
6082 enum language lang;
6083 int radix;
6084 };
6085
4efc6507
DE
6086/* Create a breakpoint for JIT code registration and unregistration. */
6087
6088struct breakpoint *
6089create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6090{
6091 struct breakpoint *b;
6092
6093 b = create_internal_breakpoint (gdbarch, address, bp_jit_event);
6094 update_global_location_list_nothrow (1);
6095 return b;
6096}
0101ce28 6097
03673fc7
PP
6098/* Remove JIT code registration and unregistration breakpoint(s). */
6099
6100void
6101remove_jit_event_breakpoints (void)
6102{
6103 struct breakpoint *b, *b_tmp;
6104
6105 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6106 if (b->type == bp_jit_event
6107 && b->loc->pspace == current_program_space)
6108 delete_breakpoint (b);
6109}
6110
cae688ec
JJ
6111void
6112remove_solib_event_breakpoints (void)
6113{
35df4500 6114 struct breakpoint *b, *b_tmp;
cae688ec 6115
35df4500 6116 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6117 if (b->type == bp_shlib_event
6118 && b->loc->pspace == current_program_space)
cae688ec
JJ
6119 delete_breakpoint (b);
6120}
6121
6122struct breakpoint *
a6d9a66e 6123create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6124{
6125 struct breakpoint *b;
6126
a6d9a66e 6127 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
b60e7edf 6128 update_global_location_list_nothrow (1);
cae688ec
JJ
6129 return b;
6130}
6131
6132/* Disable any breakpoints that are on code in shared libraries. Only
6133 apply to enabled breakpoints, disabled ones can just stay disabled. */
6134
6135void
cb851954 6136disable_breakpoints_in_shlibs (void)
cae688ec 6137{
876fa593 6138 struct bp_location *loc, **locp_tmp;
cae688ec 6139
876fa593 6140 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6141 {
2bdf28a0 6142 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6143 struct breakpoint *b = loc->owner;
2bdf28a0 6144
4a64f543
MS
6145 /* We apply the check to all breakpoints, including disabled for
6146 those with loc->duplicate set. This is so that when breakpoint
6147 becomes enabled, or the duplicate is removed, gdb will try to
6148 insert all breakpoints. If we don't set shlib_disabled here,
6149 we'll try to insert those breakpoints and fail. */
1042e4c0 6150 if (((b->type == bp_breakpoint)
508ccb1f 6151 || (b->type == bp_jit_event)
1042e4c0 6152 || (b->type == bp_hardware_breakpoint)
d77f58be 6153 || (is_tracepoint (b)))
6c95b8df 6154 && loc->pspace == current_program_space
0d381245 6155 && !loc->shlib_disabled
a77053c2 6156#ifdef PC_SOLIB
0d381245 6157 && PC_SOLIB (loc->address)
a77053c2 6158#else
6c95b8df 6159 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6160#endif
6161 )
0d381245
VP
6162 {
6163 loc->shlib_disabled = 1;
6164 }
cae688ec
JJ
6165 }
6166}
6167
4a64f543
MS
6168/* Disable any breakpoints that are in in an unloaded shared library.
6169 Only apply to enabled breakpoints, disabled ones can just stay
6170 disabled. */
84acb35a 6171
75149521 6172static void
84acb35a
JJ
6173disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6174{
876fa593 6175 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6176 int disabled_shlib_breaks = 0;
6177
c86cf029
VP
6178 /* SunOS a.out shared libraries are always mapped, so do not
6179 disable breakpoints; they will only be reported as unloaded
6180 through clear_solib when GDB discards its shared library
6181 list. See clear_solib for more information. */
6182 if (exec_bfd != NULL
6183 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6184 return;
6185
876fa593 6186 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6187 {
2bdf28a0 6188 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6189 struct breakpoint *b = loc->owner;
cc59ec59 6190
0d381245
VP
6191 if ((loc->loc_type == bp_loc_hardware_breakpoint
6192 || loc->loc_type == bp_loc_software_breakpoint)
6c95b8df 6193 && solib->pspace == loc->pspace
e2dd7057 6194 && !loc->shlib_disabled
508ccb1f
TT
6195 && (b->type == bp_breakpoint
6196 || b->type == bp_jit_event
6197 || b->type == bp_hardware_breakpoint)
e2dd7057 6198 && solib_contains_address_p (solib, loc->address))
84acb35a 6199 {
e2dd7057
PP
6200 loc->shlib_disabled = 1;
6201 /* At this point, we cannot rely on remove_breakpoint
6202 succeeding so we must mark the breakpoint as not inserted
6203 to prevent future errors occurring in remove_breakpoints. */
6204 loc->inserted = 0;
6205 if (!disabled_shlib_breaks)
6206 {
6207 target_terminal_ours_for_output ();
3e43a32a
MS
6208 warning (_("Temporarily disabling breakpoints "
6209 "for unloaded shared library \"%s\""),
e2dd7057 6210 solib->so_name);
84acb35a 6211 }
e2dd7057 6212 disabled_shlib_breaks = 1;
84acb35a
JJ
6213 }
6214 }
84acb35a
JJ
6215}
6216
ce78b96d
JB
6217/* FORK & VFORK catchpoints. */
6218
4a64f543
MS
6219/* Implement the "insert" breakpoint_ops method for fork
6220 catchpoints. */
ce78b96d 6221
77b06cd7
TJB
6222static int
6223insert_catch_fork (struct bp_location *bl)
ce78b96d 6224{
77b06cd7 6225 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6226}
6227
4a64f543
MS
6228/* Implement the "remove" breakpoint_ops method for fork
6229 catchpoints. */
ce78b96d
JB
6230
6231static int
77b06cd7 6232remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6233{
6234 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6235}
6236
6237/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6238 catchpoints. */
6239
6240static int
6241breakpoint_hit_catch_fork (struct breakpoint *b)
6242{
6243 return inferior_has_forked (inferior_ptid, &b->forked_inferior_pid);
6244}
6245
4a64f543
MS
6246/* Implement the "print_it" breakpoint_ops method for fork
6247 catchpoints. */
ce78b96d
JB
6248
6249static enum print_stop_action
6250print_it_catch_fork (struct breakpoint *b)
6251{
6252 annotate_catchpoint (b->number);
6253 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
6254 b->number, ptid_get_pid (b->forked_inferior_pid));
6255 return PRINT_SRC_AND_LOC;
6256}
6257
4a64f543
MS
6258/* Implement the "print_one" breakpoint_ops method for fork
6259 catchpoints. */
ce78b96d
JB
6260
6261static void
a6d9a66e 6262print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6263{
79a45b7d
TT
6264 struct value_print_options opts;
6265
6266 get_user_print_options (&opts);
6267
4a64f543
MS
6268 /* Field 4, the address, is omitted (which makes the columns not
6269 line up too nicely with the headers, but the effect is relatively
6270 readable). */
79a45b7d 6271 if (opts.addressprint)
ce78b96d
JB
6272 ui_out_field_skip (uiout, "addr");
6273 annotate_field (5);
6274 ui_out_text (uiout, "fork");
6275 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6276 {
6277 ui_out_text (uiout, ", process ");
6278 ui_out_field_int (uiout, "what",
6279 ptid_get_pid (b->forked_inferior_pid));
6280 ui_out_spaces (uiout, 1);
6281 }
6282}
6283
6284/* Implement the "print_mention" breakpoint_ops method for fork
6285 catchpoints. */
6286
6287static void
6288print_mention_catch_fork (struct breakpoint *b)
6289{
6290 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6291}
6292
6149aea9
PA
6293/* Implement the "print_recreate" breakpoint_ops method for fork
6294 catchpoints. */
6295
6296static void
6297print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6298{
6299 fprintf_unfiltered (fp, "catch fork");
6300}
6301
ce78b96d
JB
6302/* The breakpoint_ops structure to be used in fork catchpoints. */
6303
6304static struct breakpoint_ops catch_fork_breakpoint_ops =
6305{
6306 insert_catch_fork,
6307 remove_catch_fork,
6308 breakpoint_hit_catch_fork,
e09342b5 6309 NULL, /* resources_needed */
ce78b96d
JB
6310 print_it_catch_fork,
6311 print_one_catch_fork,
6149aea9
PA
6312 print_mention_catch_fork,
6313 print_recreate_catch_fork
ce78b96d
JB
6314};
6315
4a64f543
MS
6316/* Implement the "insert" breakpoint_ops method for vfork
6317 catchpoints. */
ce78b96d 6318
77b06cd7
TJB
6319static int
6320insert_catch_vfork (struct bp_location *bl)
ce78b96d 6321{
77b06cd7 6322 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6323}
6324
4a64f543
MS
6325/* Implement the "remove" breakpoint_ops method for vfork
6326 catchpoints. */
ce78b96d
JB
6327
6328static int
77b06cd7 6329remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6330{
6331 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6332}
6333
6334/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6335 catchpoints. */
6336
6337static int
6338breakpoint_hit_catch_vfork (struct breakpoint *b)
6339{
6340 return inferior_has_vforked (inferior_ptid, &b->forked_inferior_pid);
6341}
6342
4a64f543
MS
6343/* Implement the "print_it" breakpoint_ops method for vfork
6344 catchpoints. */
ce78b96d
JB
6345
6346static enum print_stop_action
6347print_it_catch_vfork (struct breakpoint *b)
6348{
6349 annotate_catchpoint (b->number);
6350 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
6351 b->number, ptid_get_pid (b->forked_inferior_pid));
6352 return PRINT_SRC_AND_LOC;
6353}
6354
4a64f543
MS
6355/* Implement the "print_one" breakpoint_ops method for vfork
6356 catchpoints. */
ce78b96d
JB
6357
6358static void
a6d9a66e 6359print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6360{
79a45b7d
TT
6361 struct value_print_options opts;
6362
6363 get_user_print_options (&opts);
4a64f543
MS
6364 /* Field 4, the address, is omitted (which makes the columns not
6365 line up too nicely with the headers, but the effect is relatively
6366 readable). */
79a45b7d 6367 if (opts.addressprint)
ce78b96d
JB
6368 ui_out_field_skip (uiout, "addr");
6369 annotate_field (5);
6370 ui_out_text (uiout, "vfork");
6371 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6372 {
6373 ui_out_text (uiout, ", process ");
6374 ui_out_field_int (uiout, "what",
6375 ptid_get_pid (b->forked_inferior_pid));
6376 ui_out_spaces (uiout, 1);
6377 }
6378}
6379
6380/* Implement the "print_mention" breakpoint_ops method for vfork
6381 catchpoints. */
6382
6383static void
6384print_mention_catch_vfork (struct breakpoint *b)
6385{
6386 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6387}
6388
6149aea9
PA
6389/* Implement the "print_recreate" breakpoint_ops method for vfork
6390 catchpoints. */
6391
6392static void
6393print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6394{
6395 fprintf_unfiltered (fp, "catch vfork");
6396}
6397
ce78b96d
JB
6398/* The breakpoint_ops structure to be used in vfork catchpoints. */
6399
6400static struct breakpoint_ops catch_vfork_breakpoint_ops =
6401{
6402 insert_catch_vfork,
6403 remove_catch_vfork,
6404 breakpoint_hit_catch_vfork,
e09342b5 6405 NULL, /* resources_needed */
ce78b96d
JB
6406 print_it_catch_vfork,
6407 print_one_catch_vfork,
6149aea9
PA
6408 print_mention_catch_vfork,
6409 print_recreate_catch_vfork
ce78b96d
JB
6410};
6411
a96d9b2e
SDJ
6412/* Implement the "insert" breakpoint_ops method for syscall
6413 catchpoints. */
6414
77b06cd7
TJB
6415static int
6416insert_catch_syscall (struct bp_location *bl)
a96d9b2e
SDJ
6417{
6418 struct inferior *inf = current_inferior ();
6419
6420 ++inf->total_syscalls_count;
77b06cd7 6421 if (!bl->owner->syscalls_to_be_caught)
a96d9b2e
SDJ
6422 ++inf->any_syscall_count;
6423 else
6424 {
6425 int i, iter;
cc59ec59 6426
a96d9b2e 6427 for (i = 0;
77b06cd7 6428 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6429 i++)
6430 {
6431 int elem;
cc59ec59 6432
a96d9b2e
SDJ
6433 if (iter >= VEC_length (int, inf->syscalls_counts))
6434 {
6435 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6436 uintptr_t vec_addr_offset
6437 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6438 uintptr_t vec_addr;
6439 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6440 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6441 vec_addr_offset;
6442 memset ((void *) vec_addr, 0,
6443 (iter + 1 - old_size) * sizeof (int));
6444 }
6445 elem = VEC_index (int, inf->syscalls_counts, iter);
6446 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6447 }
6448 }
6449
77b06cd7
TJB
6450 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6451 inf->total_syscalls_count != 0,
6452 inf->any_syscall_count,
6453 VEC_length (int, inf->syscalls_counts),
6454 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6455}
6456
6457/* Implement the "remove" breakpoint_ops method for syscall
6458 catchpoints. */
6459
6460static int
77b06cd7 6461remove_catch_syscall (struct bp_location *bl)
a96d9b2e
SDJ
6462{
6463 struct inferior *inf = current_inferior ();
6464
6465 --inf->total_syscalls_count;
77b06cd7 6466 if (!bl->owner->syscalls_to_be_caught)
a96d9b2e
SDJ
6467 --inf->any_syscall_count;
6468 else
6469 {
6470 int i, iter;
cc59ec59 6471
a96d9b2e 6472 for (i = 0;
77b06cd7 6473 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6474 i++)
6475 {
6476 int elem;
6477 if (iter >= VEC_length (int, inf->syscalls_counts))
6478 /* Shouldn't happen. */
6479 continue;
6480 elem = VEC_index (int, inf->syscalls_counts, iter);
6481 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6482 }
6483 }
6484
6485 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6486 inf->total_syscalls_count != 0,
6487 inf->any_syscall_count,
6488 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6489 VEC_address (int,
6490 inf->syscalls_counts));
a96d9b2e
SDJ
6491}
6492
6493/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6494 catchpoints. */
6495
6496static int
6497breakpoint_hit_catch_syscall (struct breakpoint *b)
6498{
4a64f543
MS
6499 /* We must check if we are catching specific syscalls in this
6500 breakpoint. If we are, then we must guarantee that the called
6501 syscall is the same syscall we are catching. */
a96d9b2e
SDJ
6502 int syscall_number = 0;
6503
6504 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6505 return 0;
6506
6507 /* Now, checking if the syscall is the same. */
6508 if (b->syscalls_to_be_caught)
6509 {
6510 int i, iter;
cc59ec59 6511
a96d9b2e
SDJ
6512 for (i = 0;
6513 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6514 i++)
6515 if (syscall_number == iter)
6516 break;
6517 /* Not the same. */
6518 if (!iter)
6519 return 0;
6520 }
6521
6522 return 1;
6523}
6524
6525/* Implement the "print_it" breakpoint_ops method for syscall
6526 catchpoints. */
6527
6528static enum print_stop_action
6529print_it_catch_syscall (struct breakpoint *b)
6530{
6531 /* These are needed because we want to know in which state a
6532 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6533 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6534 must print "called syscall" or "returned from syscall". */
6535 ptid_t ptid;
6536 struct target_waitstatus last;
6537 struct syscall s;
6538 struct cleanup *old_chain;
6539 char *syscall_id;
6540
6541 get_last_target_status (&ptid, &last);
6542
6543 get_syscall_by_number (last.value.syscall_number, &s);
6544
6545 annotate_catchpoint (b->number);
6546
6547 if (s.name == NULL)
6548 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6549 else
6550 syscall_id = xstrprintf ("'%s'", s.name);
6551
6552 old_chain = make_cleanup (xfree, syscall_id);
6553
6554 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6555 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6556 b->number, syscall_id);
6557 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6558 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6559 b->number, syscall_id);
6560
6561 do_cleanups (old_chain);
6562
6563 return PRINT_SRC_AND_LOC;
6564}
6565
6566/* Implement the "print_one" breakpoint_ops method for syscall
6567 catchpoints. */
6568
6569static void
6570print_one_catch_syscall (struct breakpoint *b,
6571 struct bp_location **last_loc)
6572{
6573 struct value_print_options opts;
6574
6575 get_user_print_options (&opts);
4a64f543
MS
6576 /* Field 4, the address, is omitted (which makes the columns not
6577 line up too nicely with the headers, but the effect is relatively
6578 readable). */
a96d9b2e
SDJ
6579 if (opts.addressprint)
6580 ui_out_field_skip (uiout, "addr");
6581 annotate_field (5);
6582
6583 if (b->syscalls_to_be_caught
6584 && VEC_length (int, b->syscalls_to_be_caught) > 1)
6585 ui_out_text (uiout, "syscalls \"");
6586 else
6587 ui_out_text (uiout, "syscall \"");
6588
6589 if (b->syscalls_to_be_caught)
6590 {
6591 int i, iter;
6592 char *text = xstrprintf ("%s", "");
cc59ec59 6593
a96d9b2e
SDJ
6594 for (i = 0;
6595 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6596 i++)
6597 {
6598 char *x = text;
6599 struct syscall s;
6600 get_syscall_by_number (iter, &s);
6601
6602 if (s.name != NULL)
6603 text = xstrprintf ("%s%s, ", text, s.name);
6604 else
6605 text = xstrprintf ("%s%d, ", text, iter);
6606
6607 /* We have to xfree the last 'text' (now stored at 'x')
6608 because xstrprintf dinamically allocates new space for it
6609 on every call. */
6610 xfree (x);
6611 }
6612 /* Remove the last comma. */
6613 text[strlen (text) - 2] = '\0';
6614 ui_out_field_string (uiout, "what", text);
6615 }
6616 else
6617 ui_out_field_string (uiout, "what", "<any syscall>");
6618 ui_out_text (uiout, "\" ");
6619}
6620
6621/* Implement the "print_mention" breakpoint_ops method for syscall
6622 catchpoints. */
6623
6624static void
6625print_mention_catch_syscall (struct breakpoint *b)
6626{
6627 if (b->syscalls_to_be_caught)
6628 {
6629 int i, iter;
6630
6631 if (VEC_length (int, b->syscalls_to_be_caught) > 1)
6632 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6633 else
6634 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6635
6636 for (i = 0;
6637 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6638 i++)
6639 {
6640 struct syscall s;
6641 get_syscall_by_number (iter, &s);
6642
6643 if (s.name)
6644 printf_filtered (" '%s' [%d]", s.name, s.number);
6645 else
6646 printf_filtered (" %d", s.number);
6647 }
6648 printf_filtered (")");
6649 }
6650 else
6651 printf_filtered (_("Catchpoint %d (any syscall)"),
6652 b->number);
6653}
6654
6149aea9
PA
6655/* Implement the "print_recreate" breakpoint_ops method for syscall
6656 catchpoints. */
6657
6658static void
6659print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6660{
6661 fprintf_unfiltered (fp, "catch syscall");
6662
6663 if (b->syscalls_to_be_caught)
6664 {
6665 int i, iter;
6666
6667 for (i = 0;
6668 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6669 i++)
6670 {
6671 struct syscall s;
6672
6673 get_syscall_by_number (iter, &s);
6674 if (s.name)
6675 fprintf_unfiltered (fp, " %s", s.name);
6676 else
6677 fprintf_unfiltered (fp, " %d", s.number);
6678 }
6679 }
6680}
6681
a96d9b2e
SDJ
6682/* The breakpoint_ops structure to be used in syscall catchpoints. */
6683
6684static struct breakpoint_ops catch_syscall_breakpoint_ops =
6685{
6686 insert_catch_syscall,
6687 remove_catch_syscall,
6688 breakpoint_hit_catch_syscall,
e09342b5 6689 NULL, /* resources_needed */
a96d9b2e
SDJ
6690 print_it_catch_syscall,
6691 print_one_catch_syscall,
6149aea9
PA
6692 print_mention_catch_syscall,
6693 print_recreate_catch_syscall
a96d9b2e
SDJ
6694};
6695
6696/* Returns non-zero if 'b' is a syscall catchpoint. */
6697
6698static int
6699syscall_catchpoint_p (struct breakpoint *b)
6700{
6701 return (b->ops == &catch_syscall_breakpoint_ops);
6702}
6703
6704/* Create a new breakpoint of the bp_catchpoint kind and return it,
6705 but does NOT mention it nor update the global location list.
6706 This is useful if you need to fill more fields in the
6707 struct breakpoint before calling mention.
ce78b96d
JB
6708
6709 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6710 If COND_STRING is not NULL, then store it in the breakpoint.
6711 OPS, if not NULL, is the breakpoint_ops structure associated
6712 to the catchpoint. */
6713
6714static struct breakpoint *
a96d9b2e
SDJ
6715create_catchpoint_without_mention (struct gdbarch *gdbarch, int tempflag,
6716 char *cond_string,
6717 struct breakpoint_ops *ops)
c906108c 6718{
c5aa993b
JM
6719 struct symtab_and_line sal;
6720 struct breakpoint *b;
c5aa993b 6721
fe39c653 6722 init_sal (&sal);
6c95b8df 6723 sal.pspace = current_program_space;
c5aa993b 6724
a6d9a66e 6725 b = set_raw_breakpoint (gdbarch, sal, bp_catchpoint);
c906108c
SS
6726 set_breakpoint_count (breakpoint_count + 1);
6727 b->number = breakpoint_count;
ce78b96d 6728
1b36a34b 6729 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
ce78b96d 6730 b->thread = -1;
c906108c 6731 b->addr_string = NULL;
b5de0fa7
EZ
6732 b->enable_state = bp_enabled;
6733 b->disposition = tempflag ? disp_del : disp_donttouch;
ce78b96d 6734 b->ops = ops;
c5aa993b 6735
a96d9b2e
SDJ
6736 return b;
6737}
6738
6739/* Create a new breakpoint of the bp_catchpoint kind and return it.
6740
6741 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6742 If COND_STRING is not NULL, then store it in the breakpoint.
6743 OPS, if not NULL, is the breakpoint_ops structure associated
6744 to the catchpoint. */
6745
6746static struct breakpoint *
6747create_catchpoint (struct gdbarch *gdbarch, int tempflag,
6748 char *cond_string, struct breakpoint_ops *ops)
6749{
6750 struct breakpoint *b =
6751 create_catchpoint_without_mention (gdbarch, tempflag, cond_string, ops);
6752
c906108c 6753 mention (b);
ce78b96d 6754 update_global_location_list (1);
c906108c 6755
ce78b96d 6756 return b;
c906108c 6757}
c5aa993b 6758
9b70b993 6759static void
a6d9a66e
UW
6760create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6761 int tempflag, char *cond_string,
ce78b96d 6762 struct breakpoint_ops *ops)
c906108c 6763{
a6d9a66e
UW
6764 struct breakpoint *b
6765 = create_catchpoint (gdbarch, tempflag, cond_string, ops);
ce78b96d
JB
6766
6767 /* FIXME: We should put this information in a breakpoint private data
6768 area. */
6769 b->forked_inferior_pid = null_ptid;
c906108c
SS
6770}
6771
fe798b75
JB
6772/* Exec catchpoints. */
6773
77b06cd7
TJB
6774static int
6775insert_catch_exec (struct bp_location *bl)
c906108c 6776{
77b06cd7 6777 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6778}
c906108c 6779
fe798b75 6780static int
77b06cd7 6781remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6782{
6783 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6784}
c906108c 6785
fe798b75
JB
6786static int
6787breakpoint_hit_catch_exec (struct breakpoint *b)
6788{
6789 return inferior_has_execd (inferior_ptid, &b->exec_pathname);
6790}
c906108c 6791
fe798b75
JB
6792static enum print_stop_action
6793print_it_catch_exec (struct breakpoint *b)
6794{
6795 annotate_catchpoint (b->number);
6796 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
6797 b->exec_pathname);
6798 return PRINT_SRC_AND_LOC;
c906108c
SS
6799}
6800
fe798b75 6801static void
a6d9a66e 6802print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75
JB
6803{
6804 struct value_print_options opts;
6805
6806 get_user_print_options (&opts);
6807
6808 /* Field 4, the address, is omitted (which makes the columns
6809 not line up too nicely with the headers, but the effect
6810 is relatively readable). */
6811 if (opts.addressprint)
6812 ui_out_field_skip (uiout, "addr");
6813 annotate_field (5);
6814 ui_out_text (uiout, "exec");
6815 if (b->exec_pathname != NULL)
6816 {
6817 ui_out_text (uiout, ", program \"");
6818 ui_out_field_string (uiout, "what", b->exec_pathname);
6819 ui_out_text (uiout, "\" ");
6820 }
6821}
6822
6823static void
6824print_mention_catch_exec (struct breakpoint *b)
6825{
6826 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6827}
6828
6149aea9
PA
6829/* Implement the "print_recreate" breakpoint_ops method for exec
6830 catchpoints. */
6831
6832static void
6833print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6834{
6835 fprintf_unfiltered (fp, "catch exec");
6836}
6837
fe798b75
JB
6838static struct breakpoint_ops catch_exec_breakpoint_ops =
6839{
6840 insert_catch_exec,
6841 remove_catch_exec,
6842 breakpoint_hit_catch_exec,
e09342b5 6843 NULL, /* resources_needed */
fe798b75
JB
6844 print_it_catch_exec,
6845 print_one_catch_exec,
6149aea9
PA
6846 print_mention_catch_exec,
6847 print_recreate_catch_exec
fe798b75
JB
6848};
6849
a96d9b2e
SDJ
6850static void
6851create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6852 struct breakpoint_ops *ops)
6853{
6854 struct gdbarch *gdbarch = get_current_arch ();
6855 struct breakpoint *b =
6856 create_catchpoint_without_mention (gdbarch, tempflag, NULL, ops);
6857
6858 b->syscalls_to_be_caught = filter;
6859
6860 /* Now, we have to mention the breakpoint and update the global
6861 location list. */
6862 mention (b);
6863 update_global_location_list (1);
6864}
6865
c906108c 6866static int
fba45db2 6867hw_breakpoint_used_count (void)
c906108c 6868{
52f0bd74 6869 struct breakpoint *b;
c906108c
SS
6870 int i = 0;
6871
6872 ALL_BREAKPOINTS (b)
c5aa993b 6873 {
d6b74ac4 6874 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
c5aa993b
JM
6875 i++;
6876 }
c906108c
SS
6877
6878 return i;
6879}
6880
6881static int
fba45db2 6882hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 6883{
c906108c 6884 int i = 0;
e09342b5
TJB
6885 struct breakpoint *b;
6886 struct bp_location *bl;
c906108c
SS
6887
6888 *other_type_used = 0;
6889 ALL_BREAKPOINTS (b)
e09342b5
TJB
6890 {
6891 if (!breakpoint_enabled (b))
6892 continue;
6893
c5aa993b 6894 if (b->type == type)
e09342b5
TJB
6895 for (bl = b->loc; bl; bl = bl->next)
6896 {
6897 /* Special types of hardware watchpoints may use more than
6898 one register. */
6899 if (b->ops && b->ops->resources_needed)
6900 i += b->ops->resources_needed (bl);
6901 else
6902 i++;
6903 }
cc60f2e3 6904 else if (is_hardware_watchpoint (b))
c5aa993b 6905 *other_type_used = 1;
e09342b5
TJB
6906 }
6907
c906108c
SS
6908 return i;
6909}
6910
c906108c 6911void
fba45db2 6912disable_watchpoints_before_interactive_call_start (void)
c906108c 6913{
c5aa993b 6914 struct breakpoint *b;
c906108c
SS
6915
6916 ALL_BREAKPOINTS (b)
c5aa993b 6917 {
cc60f2e3 6918 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6919 {
b5de0fa7 6920 b->enable_state = bp_call_disabled;
b60e7edf 6921 update_global_location_list (0);
c5aa993b
JM
6922 }
6923 }
c906108c
SS
6924}
6925
6926void
fba45db2 6927enable_watchpoints_after_interactive_call_stop (void)
c906108c 6928{
c5aa993b 6929 struct breakpoint *b;
c906108c
SS
6930
6931 ALL_BREAKPOINTS (b)
c5aa993b 6932 {
cc60f2e3 6933 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 6934 {
b5de0fa7 6935 b->enable_state = bp_enabled;
b60e7edf 6936 update_global_location_list (1);
c5aa993b
JM
6937 }
6938 }
c906108c
SS
6939}
6940
8bea4e01
UW
6941void
6942disable_breakpoints_before_startup (void)
6943{
6944 struct breakpoint *b;
6945 int found = 0;
6946
6947 ALL_BREAKPOINTS (b)
6948 {
6c95b8df
PA
6949 if (b->pspace != current_program_space)
6950 continue;
6951
8bea4e01
UW
6952 if ((b->type == bp_breakpoint
6953 || b->type == bp_hardware_breakpoint)
6954 && breakpoint_enabled (b))
6955 {
6956 b->enable_state = bp_startup_disabled;
6957 found = 1;
6958 }
6959 }
6960
6961 if (found)
6962 update_global_location_list (0);
6963
6c95b8df 6964 current_program_space->executing_startup = 1;
8bea4e01
UW
6965}
6966
6967void
6968enable_breakpoints_after_startup (void)
6969{
6970 struct breakpoint *b;
6971 int found = 0;
6972
6c95b8df 6973 current_program_space->executing_startup = 0;
8bea4e01
UW
6974
6975 ALL_BREAKPOINTS (b)
6976 {
6c95b8df
PA
6977 if (b->pspace != current_program_space)
6978 continue;
6979
8bea4e01
UW
6980 if ((b->type == bp_breakpoint
6981 || b->type == bp_hardware_breakpoint)
6982 && b->enable_state == bp_startup_disabled)
6983 {
6984 b->enable_state = bp_enabled;
6985 found = 1;
6986 }
6987 }
6988
6989 if (found)
6990 breakpoint_re_set ();
6991}
6992
c906108c
SS
6993
6994/* Set a breakpoint that will evaporate an end of command
6995 at address specified by SAL.
6996 Restrict it to frame FRAME if FRAME is nonzero. */
6997
6998struct breakpoint *
a6d9a66e
UW
6999set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7000 struct frame_id frame_id, enum bptype type)
c906108c 7001{
52f0bd74 7002 struct breakpoint *b;
edb3359d
DJ
7003
7004 /* If FRAME_ID is valid, it should be a real frame, not an inlined
7005 one. */
7006 gdb_assert (!frame_id_inlined_p (frame_id));
7007
a6d9a66e 7008 b = set_raw_breakpoint (gdbarch, sal, type);
b5de0fa7
EZ
7009 b->enable_state = bp_enabled;
7010 b->disposition = disp_donttouch;
818dd999 7011 b->frame_id = frame_id;
c906108c 7012
4a64f543
MS
7013 /* If we're debugging a multi-threaded program, then we want
7014 momentary breakpoints to be active in only a single thread of
7015 control. */
39f77062
KB
7016 if (in_thread_list (inferior_ptid))
7017 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 7018
b60e7edf 7019 update_global_location_list_nothrow (1);
74960c60 7020
c906108c
SS
7021 return b;
7022}
611c83ae 7023
e58b0e63
PA
7024/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7025 ORIG is NULL. */
7026
7027struct breakpoint *
7028clone_momentary_breakpoint (struct breakpoint *orig)
7029{
7030 struct breakpoint *copy;
7031
7032 /* If there's nothing to clone, then return nothing. */
7033 if (orig == NULL)
7034 return NULL;
7035
a6d9a66e 7036 copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
e58b0e63
PA
7037 copy->loc = allocate_bp_location (copy);
7038 set_breakpoint_location_function (copy->loc);
7039
a6d9a66e 7040 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7041 copy->loc->requested_address = orig->loc->requested_address;
7042 copy->loc->address = orig->loc->address;
7043 copy->loc->section = orig->loc->section;
6c95b8df 7044 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
7045
7046 if (orig->source_file == NULL)
7047 copy->source_file = NULL;
7048 else
7049 copy->source_file = xstrdup (orig->source_file);
7050
7051 copy->line_number = orig->line_number;
7052 copy->frame_id = orig->frame_id;
7053 copy->thread = orig->thread;
6c95b8df 7054 copy->pspace = orig->pspace;
e58b0e63
PA
7055
7056 copy->enable_state = bp_enabled;
7057 copy->disposition = disp_donttouch;
7058 copy->number = internal_breakpoint_number--;
7059
7060 update_global_location_list_nothrow (0);
7061 return copy;
7062}
7063
611c83ae 7064struct breakpoint *
a6d9a66e
UW
7065set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7066 enum bptype type)
611c83ae
PA
7067{
7068 struct symtab_and_line sal;
7069
7070 sal = find_pc_line (pc, 0);
7071 sal.pc = pc;
7072 sal.section = find_pc_overlay (pc);
7073 sal.explicit_pc = 1;
7074
a6d9a66e 7075 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7076}
c906108c 7077\f
c5aa993b 7078
c906108c
SS
7079/* Tell the user we have just set a breakpoint B. */
7080
7081static void
fba45db2 7082mention (struct breakpoint *b)
c906108c
SS
7083{
7084 int say_where = 0;
fa8a61dc 7085 struct cleanup *ui_out_chain;
79a45b7d
TT
7086 struct value_print_options opts;
7087
7088 get_user_print_options (&opts);
8b93c638 7089
9a4105ab
AC
7090 /* FIXME: This is misplaced; mention() is called by things (like
7091 hitting a watchpoint) other than breakpoint creation. It should
7092 be possible to clean this up and at the same time replace the
7f4b89d1 7093 random calls to breakpoint_changed with this hook. */
383f836e 7094 observer_notify_breakpoint_created (b->number);
c906108c 7095
3086aeae
DJ
7096 if (b->ops != NULL && b->ops->print_mention != NULL)
7097 b->ops->print_mention (b);
7098 else
7099 switch (b->type)
7100 {
7101 case bp_none:
3e43a32a
MS
7102 printf_filtered (_("(apparently deleted?) Eventpoint %d: "),
7103 b->number);
3086aeae
DJ
7104 break;
7105 case bp_watchpoint:
7106 ui_out_text (uiout, "Watchpoint ");
7107 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
7108 ui_out_field_int (uiout, "number", b->number);
7109 ui_out_text (uiout, ": ");
fa8a61dc 7110 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7111 do_cleanups (ui_out_chain);
7112 break;
7113 case bp_hardware_watchpoint:
7114 ui_out_text (uiout, "Hardware watchpoint ");
7115 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
7116 ui_out_field_int (uiout, "number", b->number);
7117 ui_out_text (uiout, ": ");
fa8a61dc 7118 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7119 do_cleanups (ui_out_chain);
7120 break;
7121 case bp_read_watchpoint:
7122 ui_out_text (uiout, "Hardware read watchpoint ");
7123 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
7124 ui_out_field_int (uiout, "number", b->number);
7125 ui_out_text (uiout, ": ");
fa8a61dc 7126 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7127 do_cleanups (ui_out_chain);
7128 break;
7129 case bp_access_watchpoint:
7130 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
7131 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
7132 ui_out_field_int (uiout, "number", b->number);
7133 ui_out_text (uiout, ": ");
fa8a61dc 7134 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7135 do_cleanups (ui_out_chain);
7136 break;
7137 case bp_breakpoint:
7138 if (ui_out_is_mi_like_p (uiout))
7139 {
7140 say_where = 0;
7141 break;
7142 }
2cec12e5
AR
7143 if (b->disposition == disp_del)
7144 printf_filtered (_("Temporary breakpoint"));
7145 else
7146 printf_filtered (_("Breakpoint"));
7147 printf_filtered (_(" %d"), b->number);
3086aeae
DJ
7148 say_where = 1;
7149 break;
7150 case bp_hardware_breakpoint:
7151 if (ui_out_is_mi_like_p (uiout))
7152 {
7153 say_where = 0;
7154 break;
7155 }
a3f17187 7156 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
3086aeae
DJ
7157 say_where = 1;
7158 break;
1042e4c0
SS
7159 case bp_tracepoint:
7160 if (ui_out_is_mi_like_p (uiout))
7161 {
7162 say_where = 0;
7163 break;
7164 }
7165 printf_filtered (_("Tracepoint"));
7166 printf_filtered (_(" %d"), b->number);
7167 say_where = 1;
7168 break;
7a697b8d
SS
7169 case bp_fast_tracepoint:
7170 if (ui_out_is_mi_like_p (uiout))
7171 {
7172 say_where = 0;
7173 break;
7174 }
7175 printf_filtered (_("Fast tracepoint"));
7176 printf_filtered (_(" %d"), b->number);
7177 say_where = 1;
7178 break;
0fb4aa4b
PA
7179 case bp_static_tracepoint:
7180 if (ui_out_is_mi_like_p (uiout))
7181 {
7182 say_where = 0;
7183 break;
7184 }
7185 printf_filtered (_("Static tracepoint"));
7186 printf_filtered (_(" %d"), b->number);
7187 say_where = 1;
7188 break;
3086aeae
DJ
7189
7190 case bp_until:
7191 case bp_finish:
7192 case bp_longjmp:
7193 case bp_longjmp_resume:
186c406b
TT
7194 case bp_exception:
7195 case bp_exception_resume:
3086aeae 7196 case bp_step_resume:
3086aeae 7197 case bp_call_dummy:
aa7d318d 7198 case bp_std_terminate:
3086aeae
DJ
7199 case bp_watchpoint_scope:
7200 case bp_shlib_event:
7201 case bp_thread_event:
7202 case bp_overlay_event:
4efc6507 7203 case bp_jit_event:
0fd8e87f 7204 case bp_longjmp_master:
aa7d318d 7205 case bp_std_terminate_master:
186c406b 7206 case bp_exception_master:
3086aeae
DJ
7207 break;
7208 }
c906108c 7209
c906108c
SS
7210 if (say_where)
7211 {
a3f17187
AC
7212 /* i18n: cagney/2005-02-11: Below needs to be merged into a
7213 single string. */
0d381245 7214 if (b->loc == NULL)
c906108c 7215 {
a3f17187 7216 printf_filtered (_(" (%s) pending."), b->addr_string);
0101ce28
JJ
7217 }
7218 else
7219 {
79a45b7d 7220 if (opts.addressprint || b->source_file == NULL)
0101ce28
JJ
7221 {
7222 printf_filtered (" at ");
5af949e3
UW
7223 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
7224 gdb_stdout);
0101ce28
JJ
7225 }
7226 if (b->source_file)
7227 printf_filtered (": file %s, line %d.",
7228 b->source_file, b->line_number);
0d381245
VP
7229
7230 if (b->loc->next)
7231 {
7232 struct bp_location *loc = b->loc;
7233 int n = 0;
7234 for (; loc; loc = loc->next)
7235 ++n;
7236 printf_filtered (" (%d locations)", n);
7237 }
7238
c906108c 7239 }
c906108c 7240 }
9dc5e2a9 7241 if (ui_out_is_mi_like_p (uiout))
fb40c209 7242 return;
c906108c
SS
7243 printf_filtered ("\n");
7244}
c906108c 7245\f
c5aa993b 7246
0d381245 7247static struct bp_location *
39d61571 7248add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7249 const struct symtab_and_line *sal)
7250{
7251 struct bp_location *loc, **tmp;
7252
39d61571 7253 loc = allocate_bp_location (b);
0d381245
VP
7254 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7255 ;
7256 *tmp = loc;
a6d9a66e
UW
7257 loc->gdbarch = get_sal_arch (*sal);
7258 if (!loc->gdbarch)
7259 loc->gdbarch = b->gdbarch;
0d381245 7260 loc->requested_address = sal->pc;
a6d9a66e
UW
7261 loc->address = adjust_breakpoint_address (loc->gdbarch,
7262 loc->requested_address, b->type);
6c95b8df
PA
7263 loc->pspace = sal->pspace;
7264 gdb_assert (loc->pspace != NULL);
0d381245
VP
7265 loc->section = sal->section;
7266
7267 set_breakpoint_location_function (loc);
7268 return loc;
7269}
514f746b
AR
7270\f
7271
7272/* Return 1 if LOC is pointing to a permanent breakpoint,
7273 return 0 otherwise. */
7274
7275static int
7276bp_loc_is_permanent (struct bp_location *loc)
7277{
7278 int len;
7279 CORE_ADDR addr;
7280 const gdb_byte *brk;
7281 gdb_byte *target_mem;
939c61fa
JK
7282 struct cleanup *cleanup;
7283 int retval = 0;
514f746b
AR
7284
7285 gdb_assert (loc != NULL);
7286
7287 addr = loc->address;
a6d9a66e 7288 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7289
939c61fa
JK
7290 /* Software breakpoints unsupported? */
7291 if (brk == NULL)
7292 return 0;
7293
514f746b
AR
7294 target_mem = alloca (len);
7295
939c61fa
JK
7296 /* Enable the automatic memory restoration from breakpoints while
7297 we read the memory. Otherwise we could say about our temporary
7298 breakpoints they are permanent. */
6c95b8df
PA
7299 cleanup = save_current_space_and_thread ();
7300
7301 switch_to_program_space_and_thread (loc->pspace);
7302 make_show_memory_breakpoints_cleanup (0);
939c61fa 7303
514f746b
AR
7304 if (target_read_memory (loc->address, target_mem, len) == 0
7305 && memcmp (target_mem, brk, len) == 0)
939c61fa 7306 retval = 1;
514f746b 7307
939c61fa
JK
7308 do_cleanups (cleanup);
7309
7310 return retval;
514f746b
AR
7311}
7312
7313
c3f6f71d 7314
018d34a4
VP
7315/* Create a breakpoint with SAL as location. Use ADDR_STRING
7316 as textual description of the location, and COND_STRING
db107f19 7317 as condition expression. */
018d34a4
VP
7318
7319static void
8cdf0e15
VP
7320create_breakpoint_sal (struct gdbarch *gdbarch,
7321 struct symtabs_and_lines sals, char *addr_string,
7322 char *cond_string,
7323 enum bptype type, enum bpdisp disposition,
7324 int thread, int task, int ignore_count,
84f4c1fe
PM
7325 struct breakpoint_ops *ops, int from_tty,
7326 int enabled, int internal)
018d34a4 7327{
0d381245
VP
7328 struct breakpoint *b = NULL;
7329 int i;
018d34a4
VP
7330
7331 if (type == bp_hardware_breakpoint)
7332 {
7333 int i = hw_breakpoint_used_count ();
7334 int target_resources_ok =
d92524f1 7335 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7336 i + 1, 0);
7337 if (target_resources_ok == 0)
7338 error (_("No hardware breakpoint support in the target."));
7339 else if (target_resources_ok < 0)
7340 error (_("Hardware breakpoints used exceeds limit."));
7341 }
7342
6c95b8df
PA
7343 gdb_assert (sals.nelts > 0);
7344
0d381245
VP
7345 for (i = 0; i < sals.nelts; ++i)
7346 {
7347 struct symtab_and_line sal = sals.sals[i];
7348 struct bp_location *loc;
7349
7350 if (from_tty)
5af949e3
UW
7351 {
7352 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7353 if (!loc_gdbarch)
7354 loc_gdbarch = gdbarch;
7355
7356 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7357 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7358 }
0d381245
VP
7359
7360 if (i == 0)
7361 {
a6d9a66e 7362 b = set_raw_breakpoint (gdbarch, sal, type);
84f4c1fe 7363 set_breakpoint_number (internal, b);
0d381245 7364 b->thread = thread;
4a306c9a 7365 b->task = task;
018d34a4 7366
0d381245
VP
7367 b->cond_string = cond_string;
7368 b->ignore_count = ignore_count;
41447f92 7369 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7370 b->disposition = disposition;
6c95b8df
PA
7371 b->pspace = sals.sals[0].pspace;
7372
0fb4aa4b
PA
7373 if (type == bp_static_tracepoint)
7374 {
7375 struct static_tracepoint_marker marker;
7376
7377 if (is_marker_spec (addr_string))
7378 {
7379 /* We already know the marker exists, otherwise, we
7380 wouldn't see a sal for it. */
7381 char *p = &addr_string[3];
7382 char *endp;
7383 char *marker_str;
7384 int i;
7385
7386 while (*p == ' ' || *p == '\t')
7387 p++;
7388
7389 endp = p;
7390 while (*endp != ' ' && *endp != '\t' && *endp != '\0')
7391 endp++;
7392
7393 marker_str = savestring (p, endp - p);
7394 b->static_trace_marker_id = marker_str;
7395
3e43a32a
MS
7396 printf_filtered (_("Probed static tracepoint "
7397 "marker \"%s\"\n"),
0fb4aa4b
PA
7398 b->static_trace_marker_id);
7399 }
7400 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7401 {
7402 b->static_trace_marker_id = xstrdup (marker.str_id);
7403 release_static_tracepoint_marker (&marker);
7404
3e43a32a
MS
7405 printf_filtered (_("Probed static tracepoint "
7406 "marker \"%s\"\n"),
0fb4aa4b
PA
7407 b->static_trace_marker_id);
7408 }
7409 else
3e43a32a
MS
7410 warning (_("Couldn't determine the static "
7411 "tracepoint marker to probe"));
0fb4aa4b
PA
7412 }
7413
6c95b8df 7414 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7415 && (b->type == bp_breakpoint
7416 || b->type == bp_hardware_breakpoint))
7417 b->enable_state = bp_startup_disabled;
7418
0d381245
VP
7419 loc = b->loc;
7420 }
7421 else
018d34a4 7422 {
39d61571 7423 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7424 }
7425
514f746b
AR
7426 if (bp_loc_is_permanent (loc))
7427 make_breakpoint_permanent (b);
7428
0d381245
VP
7429 if (b->cond_string)
7430 {
7431 char *arg = b->cond_string;
d32a6982 7432 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7433 if (*arg)
db107f19 7434 error (_("Garbage %s follows condition"), arg);
018d34a4 7435 }
0d381245 7436 }
018d34a4
VP
7437
7438 if (addr_string)
7439 b->addr_string = addr_string;
7440 else
7441 /* addr_string has to be used or breakpoint_re_set will delete
7442 me. */
5af949e3
UW
7443 b->addr_string
7444 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
018d34a4 7445
604133b5 7446 b->ops = ops;
84f4c1fe
PM
7447 if (internal)
7448 /* Do not mention breakpoints with a negative number, but do
7449 notify observers. */
7450 observer_notify_breakpoint_created (b->number);
7451 else
7452 mention (b);
018d34a4
VP
7453}
7454
ed0616c6
VP
7455/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7456 elements to fill the void space. */
2c0b251b
PA
7457static void
7458remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7459{
7460 int i = index_to_remove+1;
7461 int last_index = sal->nelts-1;
7462
7463 for (;i <= last_index; ++i)
7464 sal->sals[i-1] = sal->sals[i];
7465
7466 --(sal->nelts);
7467}
7468
6c95b8df
PA
7469/* If appropriate, obtains all sals that correspond to the same file
7470 and line as SAL, in all program spaces. Users debugging with IDEs,
7471 will want to set a breakpoint at foo.c:line, and not really care
7472 about program spaces. This is done only if SAL does not have
7473 explicit PC and has line and file information. If we got just a
7474 single expanded sal, return the original.
ed0616c6 7475
6c95b8df
PA
7476 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7477 which the name of enclosing function is different from SAL. This
7478 makes sure that if we have breakpoint originally set in template
7479 instantiation, say foo<int>(), we won't expand SAL to locations at
7480 the same line in all existing instantiations of 'foo'. */
ed0616c6 7481
2c0b251b 7482static struct symtabs_and_lines
ed0616c6
VP
7483expand_line_sal_maybe (struct symtab_and_line sal)
7484{
7485 struct symtabs_and_lines expanded;
7486 CORE_ADDR original_pc = sal.pc;
7487 char *original_function = NULL;
7488 int found;
7489 int i;
6c95b8df 7490 struct cleanup *old_chain;
ed0616c6
VP
7491
7492 /* If we have explicit pc, don't expand.
7493 If we have no line number, we can't expand. */
7494 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7495 {
7496 expanded.nelts = 1;
7497 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7498 expanded.sals[0] = sal;
7499 return expanded;
7500 }
7501
7502 sal.pc = 0;
6c95b8df
PA
7503
7504 old_chain = save_current_space_and_thread ();
7505
7506 switch_to_program_space_and_thread (sal.pspace);
7507
ed0616c6 7508 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7509
7510 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7511 expanded = expand_line_sal (sal);
6c95b8df 7512
ed0616c6
VP
7513 if (expanded.nelts == 1)
7514 {
3dba1c98
JB
7515 /* We had one sal, we got one sal. Return that sal, adjusting it
7516 past the function prologue if necessary. */
ed0616c6
VP
7517 xfree (expanded.sals);
7518 expanded.nelts = 1;
7519 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7520 sal.pc = original_pc;
7521 expanded.sals[0] = sal;
3dba1c98 7522 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7523 do_cleanups (old_chain);
ed0616c6
VP
7524 return expanded;
7525 }
7526
7527 if (!sal.explicit_line)
7528 {
7529 CORE_ADDR func_addr, func_end;
7530 for (i = 0; i < expanded.nelts; ++i)
7531 {
7532 CORE_ADDR pc = expanded.sals[i].pc;
7533 char *this_function;
6c95b8df
PA
7534
7535 /* We need to switch threads as well since we're about to
7536 read memory. */
7537 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7538
ed0616c6
VP
7539 if (find_pc_partial_function (pc, &this_function,
7540 &func_addr, &func_end))
7541 {
059fb39f
PM
7542 if (this_function
7543 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7544 {
7545 remove_sal (&expanded, i);
7546 --i;
7547 }
ed0616c6
VP
7548 }
7549 }
7550 }
059acae7
UW
7551
7552 /* Skip the function prologue if necessary. */
7553 for (i = 0; i < expanded.nelts; ++i)
7554 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7555
6c95b8df
PA
7556 do_cleanups (old_chain);
7557
ed0616c6
VP
7558 if (expanded.nelts <= 1)
7559 {
4a64f543
MS
7560 /* This is un ugly workaround. If we get zero expanded sals
7561 then something is really wrong. Fix that by returning the
7562 original sal. */
7563
ed0616c6
VP
7564 xfree (expanded.sals);
7565 expanded.nelts = 1;
7566 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7567 sal.pc = original_pc;
7568 expanded.sals[0] = sal;
7569 return expanded;
7570 }
7571
7572 if (original_pc)
7573 {
7574 found = 0;
7575 for (i = 0; i < expanded.nelts; ++i)
7576 if (expanded.sals[i].pc == original_pc)
7577 {
7578 found = 1;
7579 break;
7580 }
7581 gdb_assert (found);
7582 }
7583
7584 return expanded;
7585}
7586
018d34a4
VP
7587/* Add SALS.nelts breakpoints to the breakpoint table. For each
7588 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7589 value. COND_STRING, if not NULL, specified the condition to be
7590 used for all breakpoints. Essentially the only case where
7591 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7592 function. In that case, it's still not possible to specify
7593 separate conditions for different overloaded functions, so
7594 we take just a single condition string.
7595
c3f6f71d 7596 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7597 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7598 array contents). If the function fails (error() is called), the
7599 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7600 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7601
7602static void
8cdf0e15
VP
7603create_breakpoints_sal (struct gdbarch *gdbarch,
7604 struct symtabs_and_lines sals, char **addr_string,
7605 char *cond_string,
7606 enum bptype type, enum bpdisp disposition,
7607 int thread, int task, int ignore_count,
7608 struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7609 int enabled, int internal)
c906108c 7610{
018d34a4 7611 int i;
cc59ec59 7612
018d34a4 7613 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7614 {
ed0616c6
VP
7615 struct symtabs_and_lines expanded =
7616 expand_line_sal_maybe (sals.sals[i]);
0d381245 7617
8cdf0e15
VP
7618 create_breakpoint_sal (gdbarch, expanded, addr_string[i],
7619 cond_string, type, disposition,
84f4c1fe
PM
7620 thread, task, ignore_count, ops,
7621 from_tty, enabled, internal);
c3f6f71d 7622 }
c3f6f71d 7623}
c906108c 7624
9998af43 7625/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7626 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7627 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7628 address strings. ADDRESS points to the end of the SAL.
7629
7630 The array and the line spec strings are allocated on the heap, it is
7631 the caller's responsibility to free them. */
c906108c 7632
b9362cc7 7633static void
c3f6f71d
JM
7634parse_breakpoint_sals (char **address,
7635 struct symtabs_and_lines *sals,
0101ce28
JJ
7636 char ***addr_string,
7637 int *not_found_ptr)
c3f6f71d
JM
7638{
7639 char *addr_start = *address;
cc59ec59 7640
c3f6f71d
JM
7641 *addr_string = NULL;
7642 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7643 breakpoint. */
c3f6f71d
JM
7644 if ((*address) == NULL
7645 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c
SS
7646 {
7647 if (default_breakpoint_valid)
7648 {
c3f6f71d 7649 struct symtab_and_line sal;
cc59ec59 7650
4a64f543 7651 init_sal (&sal); /* Initialize to zeroes. */
c3f6f71d 7652 sals->sals = (struct symtab_and_line *)
c906108c
SS
7653 xmalloc (sizeof (struct symtab_and_line));
7654 sal.pc = default_breakpoint_address;
7655 sal.line = default_breakpoint_line;
7656 sal.symtab = default_breakpoint_symtab;
6c95b8df 7657 sal.pspace = default_breakpoint_pspace;
c5aa993b 7658 sal.section = find_pc_overlay (sal.pc);
00903456 7659
4a64f543
MS
7660 /* "break" without arguments is equivalent to "break *PC"
7661 where PC is the default_breakpoint_address. So make sure
7662 to set sal.explicit_pc to prevent GDB from trying to
7663 expand the list of sals to include all other instances
7664 with the same symtab and line. */
00903456
JK
7665 sal.explicit_pc = 1;
7666
c3f6f71d
JM
7667 sals->sals[0] = sal;
7668 sals->nelts = 1;
c906108c
SS
7669 }
7670 else
8a3fe4f8 7671 error (_("No default breakpoint address now."));
c906108c
SS
7672 }
7673 else
7674 {
c906108c 7675 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7676 current_source_symtab (which is decode_line_1's default).
7677 This should produce the results we want almost all of the
7678 time while leaving default_breakpoint_* alone.
7679
1aeae86e
AF
7680 ObjC: However, don't match an Objective-C method name which
7681 may have a '+' or '-' succeeded by a '[' */
0378c332 7682
c214a6fd 7683 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 7684
c906108c 7685 if (default_breakpoint_valid
0378c332 7686 && (!cursal.symtab
1aeae86e
AF
7687 || ((strchr ("+-", (*address)[0]) != NULL)
7688 && ((*address)[1] != '['))))
c3f6f71d 7689 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
0101ce28
JJ
7690 default_breakpoint_line, addr_string,
7691 not_found_ptr);
c906108c 7692 else
0101ce28
JJ
7693 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
7694 addr_string, not_found_ptr);
c906108c 7695 }
4a64f543 7696 /* For any SAL that didn't have a canonical string, fill one in. */
c3f6f71d
JM
7697 if (sals->nelts > 0 && *addr_string == NULL)
7698 *addr_string = xcalloc (sals->nelts, sizeof (char **));
7699 if (addr_start != (*address))
c906108c 7700 {
c3f6f71d 7701 int i;
cc59ec59 7702
c3f6f71d 7703 for (i = 0; i < sals->nelts; i++)
c906108c 7704 {
4a64f543 7705 /* Add the string if not present. */
c3f6f71d 7706 if ((*addr_string)[i] == NULL)
cc59ec59
MS
7707 (*addr_string)[i] = savestring (addr_start,
7708 (*address) - addr_start);
c906108c
SS
7709 }
7710 }
c3f6f71d 7711}
c906108c 7712
c906108c 7713
c3f6f71d 7714/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7715 inserted as a breakpoint. If it can't throw an error. */
c906108c 7716
b9362cc7 7717static void
23e7acfb 7718breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7719{
7720 int i;
cc59ec59 7721
c3f6f71d 7722 for (i = 0; i < sals->nelts; i++)
ee53e872 7723 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7724}
7725
7a697b8d
SS
7726/* Fast tracepoints may have restrictions on valid locations. For
7727 instance, a fast tracepoint using a jump instead of a trap will
7728 likely have to overwrite more bytes than a trap would, and so can
7729 only be placed where the instruction is longer than the jump, or a
7730 multi-instruction sequence does not have a jump into the middle of
7731 it, etc. */
7732
7733static void
7734check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7735 struct symtabs_and_lines *sals)
7736{
7737 int i, rslt;
7738 struct symtab_and_line *sal;
7739 char *msg;
7740 struct cleanup *old_chain;
7741
7742 for (i = 0; i < sals->nelts; i++)
7743 {
7744 sal = &sals->sals[i];
7745
7746 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7747 NULL, &msg);
7748 old_chain = make_cleanup (xfree, msg);
7749
7750 if (!rslt)
7751 error (_("May not have a fast tracepoint at 0x%s%s"),
7752 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7753
7754 do_cleanups (old_chain);
7755 }
7756}
7757
05ff989b 7758static void
0101ce28
JJ
7759do_captured_parse_breakpoint (struct ui_out *ui, void *data)
7760{
7761 struct captured_parse_breakpoint_args *args = data;
7762
7763 parse_breakpoint_sals (args->arg_p, args->sals_p, args->addr_string_p,
7764 args->not_found_ptr);
0101ce28
JJ
7765}
7766
018d34a4
VP
7767/* Given TOK, a string specification of condition and thread, as
7768 accepted by the 'break' command, extract the condition
7769 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7770 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7771 If no condition is found, *COND_STRING is set to NULL.
7772 If no thread is found, *THREAD is set to -1. */
7773static void
7774find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7775 char **cond_string, int *thread, int *task)
018d34a4
VP
7776{
7777 *cond_string = NULL;
7778 *thread = -1;
7779 while (tok && *tok)
7780 {
7781 char *end_tok;
7782 int toklen;
7783 char *cond_start = NULL;
7784 char *cond_end = NULL;
cc59ec59 7785
018d34a4
VP
7786 while (*tok == ' ' || *tok == '\t')
7787 tok++;
7788
7789 end_tok = tok;
7790
7791 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
7792 end_tok++;
7793
7794 toklen = end_tok - tok;
7795
7796 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7797 {
f7545552
TT
7798 struct expression *expr;
7799
018d34a4 7800 tok = cond_start = end_tok + 1;
f7545552
TT
7801 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7802 xfree (expr);
018d34a4
VP
7803 cond_end = tok;
7804 *cond_string = savestring (cond_start,
7805 cond_end - cond_start);
7806 }
7807 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7808 {
7809 char *tmptok;
7810
7811 tok = end_tok + 1;
7812 tmptok = tok;
7813 *thread = strtol (tok, &tok, 0);
7814 if (tok == tmptok)
7815 error (_("Junk after thread keyword."));
7816 if (!valid_thread_id (*thread))
7817 error (_("Unknown thread %d."), *thread);
7818 }
4a306c9a
JB
7819 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7820 {
7821 char *tmptok;
7822
7823 tok = end_tok + 1;
7824 tmptok = tok;
7825 *task = strtol (tok, &tok, 0);
7826 if (tok == tmptok)
7827 error (_("Junk after task keyword."));
7828 if (!valid_task_id (*task))
b6199126 7829 error (_("Unknown task %d."), *task);
4a306c9a 7830 }
018d34a4
VP
7831 else
7832 error (_("Junk at end of arguments."));
7833 }
7834}
7835
0fb4aa4b
PA
7836/* Decode a static tracepoint marker spec. */
7837
7838static struct symtabs_and_lines
7839decode_static_tracepoint_spec (char **arg_p)
7840{
7841 VEC(static_tracepoint_marker_p) *markers = NULL;
7842 struct symtabs_and_lines sals;
7843 struct symtab_and_line sal;
7844 struct symbol *sym;
7845 struct cleanup *old_chain;
7846 char *p = &(*arg_p)[3];
7847 char *endp;
7848 char *marker_str;
7849 int i;
7850
7851 while (*p == ' ' || *p == '\t')
7852 p++;
7853
7854 endp = p;
7855 while (*endp != ' ' && *endp != '\t' && *endp != '\0')
7856 endp++;
7857
7858 marker_str = savestring (p, endp - p);
7859 old_chain = make_cleanup (xfree, marker_str);
7860
7861 markers = target_static_tracepoint_markers_by_strid (marker_str);
7862 if (VEC_empty(static_tracepoint_marker_p, markers))
7863 error (_("No known static tracepoint marker named %s"), marker_str);
7864
7865 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7866 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7867
7868 for (i = 0; i < sals.nelts; i++)
7869 {
7870 struct static_tracepoint_marker *marker;
7871
7872 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7873
7874 init_sal (&sals.sals[i]);
7875
7876 sals.sals[i] = find_pc_line (marker->address, 0);
7877 sals.sals[i].pc = marker->address;
7878
7879 release_static_tracepoint_marker (marker);
7880 }
7881
7882 do_cleanups (old_chain);
7883
7884 *arg_p = endp;
7885 return sals;
7886}
7887
fd9b8c24
PA
7888/* Set a breakpoint. This function is shared between CLI and MI
7889 functions for setting a breakpoint. This function has two major
7890 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7891 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7892 breakpoint location, address and thread. Otherwise, ARG is just
7893 the location of breakpoint, with condition and thread specified by
7894 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7895 the breakpoint number will be allocated from the internal
7896 breakpoint count. Returns true if any breakpoint was created;
7897 false otherwise. */
0101ce28 7898
8cdf0e15
VP
7899int
7900create_breakpoint (struct gdbarch *gdbarch,
7901 char *arg, char *cond_string, int thread,
7902 int parse_condition_and_thread,
0fb4aa4b 7903 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7904 int ignore_count,
7905 enum auto_boolean pending_break_support,
7906 struct breakpoint_ops *ops,
84f4c1fe 7907 int from_tty, int enabled, int internal)
c3f6f71d 7908{
71fff37b 7909 struct gdb_exception e;
c3f6f71d 7910 struct symtabs_and_lines sals;
0101ce28 7911 struct symtab_and_line pending_sal;
0101ce28 7912 char *copy_arg;
c3f6f71d
JM
7913 char *addr_start = arg;
7914 char **addr_string;
7915 struct cleanup *old_chain;
80c99de1 7916 struct cleanup *bkpt_chain = NULL;
0101ce28 7917 struct captured_parse_breakpoint_args parse_args;
05ff989b 7918 int i;
0101ce28 7919 int pending = 0;
0101ce28 7920 int not_found = 0;
4a306c9a 7921 int task = 0;
86b17b60 7922 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7923
c3f6f71d
JM
7924 sals.sals = NULL;
7925 sals.nelts = 0;
7926 addr_string = NULL;
c3f6f71d 7927
0101ce28
JJ
7928 parse_args.arg_p = &arg;
7929 parse_args.sals_p = &sals;
7930 parse_args.addr_string_p = &addr_string;
7931 parse_args.not_found_ptr = &not_found;
7932
0fb4aa4b
PA
7933 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7934 {
7935 int i;
7936
7937 sals = decode_static_tracepoint_spec (&arg);
7938
7939 copy_arg = savestring (addr_start, arg - addr_start);
7940 addr_string = xcalloc (sals.nelts, sizeof (char **));
7941 for (i = 0; i < sals.nelts; i++)
7942 addr_string[i] = xstrdup (copy_arg);
7943 goto done;
7944 }
7945
05ff989b
AC
7946 e = catch_exception (uiout, do_captured_parse_breakpoint,
7947 &parse_args, RETURN_MASK_ALL);
0101ce28
JJ
7948
7949 /* If caller is interested in rc value from parse, set value. */
05ff989b 7950 switch (e.reason)
0101ce28 7951 {
05ff989b 7952 case RETURN_QUIT:
98deb0da 7953 throw_exception (e);
05ff989b
AC
7954 case RETURN_ERROR:
7955 switch (e.error)
0101ce28 7956 {
05ff989b 7957 case NOT_FOUND_ERROR:
0101ce28 7958
05ff989b
AC
7959 /* If pending breakpoint support is turned off, throw
7960 error. */
fa8d40ab
JJ
7961
7962 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7963 throw_exception (e);
7964
7965 exception_print (gdb_stderr, e);
fa8d40ab 7966
05ff989b
AC
7967 /* If pending breakpoint support is auto query and the user
7968 selects no, then simply return the error code. */
059fb39f 7969 if (pending_break_support == AUTO_BOOLEAN_AUTO
3e43a32a
MS
7970 && !nquery (_("Make breakpoint pending on "
7971 "future shared library load? ")))
fd9b8c24 7972 return 0;
fa8d40ab 7973
05ff989b
AC
7974 /* At this point, either the user was queried about setting
7975 a pending breakpoint and selected yes, or pending
7976 breakpoint behavior is on and thus a pending breakpoint
7977 is defaulted on behalf of the user. */
0101ce28
JJ
7978 copy_arg = xstrdup (addr_start);
7979 addr_string = &copy_arg;
7980 sals.nelts = 1;
7981 sals.sals = &pending_sal;
7982 pending_sal.pc = 0;
7983 pending = 1;
05ff989b
AC
7984 break;
7985 default:
98deb0da 7986 throw_exception (e);
0101ce28 7987 }
05ff989b
AC
7988 default:
7989 if (!sals.nelts)
fd9b8c24 7990 return 0;
0101ce28 7991 }
c3f6f71d 7992
0fb4aa4b
PA
7993 done:
7994
4a64f543 7995 /* Create a chain of things that always need to be cleaned up. */
c3f6f71d
JM
7996 old_chain = make_cleanup (null_cleanup, 0);
7997
0101ce28
JJ
7998 if (!pending)
7999 {
8000 /* Make sure that all storage allocated to SALS gets freed. */
8001 make_cleanup (xfree, sals.sals);
8002
4a64f543 8003 /* Cleanup the addr_string array but not its contents. */
0101ce28
JJ
8004 make_cleanup (xfree, addr_string);
8005 }
c3f6f71d 8006
c3f6f71d
JM
8007 /* ----------------------------- SNIP -----------------------------
8008 Anything added to the cleanup chain beyond this point is assumed
8009 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
8010 then the memory is not reclaimed. */
8011 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d
JM
8012
8013 /* Mark the contents of the addr_string for cleanup. These go on
80c99de1 8014 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
8015 for (i = 0; i < sals.nelts; i++)
8016 {
8017 if (addr_string[i] != NULL)
b8c9b27d 8018 make_cleanup (xfree, addr_string[i]);
c3f6f71d
JM
8019 }
8020
8021 /* Resolve all line numbers to PC's and verify that the addresses
8022 are ok for the target. */
0101ce28 8023 if (!pending)
23e7acfb 8024 breakpoint_sals_to_pc (&sals);
c3f6f71d 8025
7a697b8d
SS
8026 /* Fast tracepoints may have additional restrictions on location. */
8027 if (type_wanted == bp_fast_tracepoint)
8028 check_fast_tracepoint_sals (gdbarch, &sals);
8029
c3f6f71d
JM
8030 /* Verify that condition can be parsed, before setting any
8031 breakpoints. Allocate a separate condition expression for each
4a64f543 8032 breakpoint. */
0101ce28 8033 if (!pending)
c3f6f71d 8034 {
2f069f6f 8035 if (parse_condition_and_thread)
72b2ff0e
VP
8036 {
8037 /* Here we only parse 'arg' to separate condition
8038 from thread number, so parsing in context of first
8039 sal is OK. When setting the breakpoint we'll
8040 re-parse it in context of each sal. */
8041 cond_string = NULL;
8042 thread = -1;
4a306c9a
JB
8043 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
8044 &thread, &task);
72b2ff0e
VP
8045 if (cond_string)
8046 make_cleanup (xfree, cond_string);
8047 }
2f069f6f 8048 else
72b2ff0e
VP
8049 {
8050 /* Create a private copy of condition string. */
8051 if (cond_string)
8052 {
8053 cond_string = xstrdup (cond_string);
8054 make_cleanup (xfree, cond_string);
8055 }
8056 }
0fb4aa4b
PA
8057
8058 /* If the user is creating a static tracepoint by marker id
8059 (strace -m MARKER_ID), then store the sals index, so that
8060 breakpoint_re_set can try to match up which of the newly
8061 found markers corresponds to this one, and, don't try to
8062 expand multiple locations for each sal, given than SALS
8063 already should contain all sals for MARKER_ID. */
8064 if (type_wanted == bp_static_tracepoint
8065 && is_marker_spec (addr_string[0]))
8066 {
8067 int i;
8068
8069 for (i = 0; i < sals.nelts; ++i)
8070 {
8071 struct symtabs_and_lines expanded;
8072 struct breakpoint *tp;
8073 struct cleanup *old_chain;
8074
8075 expanded.nelts = 1;
8076 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
8077 expanded.sals[0] = sals.sals[i];
8078 old_chain = make_cleanup (xfree, expanded.sals);
8079
8080 create_breakpoint_sal (gdbarch, expanded, addr_string[i],
8081 cond_string, type_wanted,
8082 tempflag ? disp_del : disp_donttouch,
8083 thread, task, ignore_count, ops,
84f4c1fe 8084 from_tty, enabled, internal);
0fb4aa4b
PA
8085
8086 do_cleanups (old_chain);
8087
8088 /* Get the tracepoint we just created. */
84f4c1fe
PM
8089 if (internal)
8090 tp = get_breakpoint (internal_breakpoint_number);
8091 else
8092 tp = get_breakpoint (breakpoint_count);
0fb4aa4b
PA
8093 gdb_assert (tp != NULL);
8094
8095 /* Given that its possible to have multiple markers with
8096 the same string id, if the user is creating a static
8097 tracepoint by marker id ("strace -m MARKER_ID"), then
8098 store the sals index, so that breakpoint_re_set can
8099 try to match up which of the newly found markers
8100 corresponds to this one */
8101 tp->static_trace_marker_id_idx = i;
8102 }
8103 }
8104 else
8105 create_breakpoints_sal (gdbarch, sals, addr_string, cond_string,
3e43a32a
MS
8106 type_wanted,
8107 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 8108 thread, task, ignore_count, ops, from_tty,
84f4c1fe 8109 enabled, internal);
c906108c 8110 }
0101ce28
JJ
8111 else
8112 {
0101ce28
JJ
8113 struct breakpoint *b;
8114
0101ce28
JJ
8115 make_cleanup (xfree, copy_arg);
8116
a6d9a66e 8117 b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
84f4c1fe 8118 set_breakpoint_number (internal, b);
72b2ff0e 8119 b->thread = -1;
018d34a4 8120 b->addr_string = addr_string[0];
72b2ff0e 8121 b->cond_string = NULL;
0101ce28 8122 b->ignore_count = ignore_count;
0101ce28 8123 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 8124 b->condition_not_parsed = 1;
604133b5 8125 b->ops = ops;
41447f92 8126 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 8127 b->pspace = current_program_space;
84f4c1fe 8128 b->py_bp_object = NULL;
74960c60 8129
6c95b8df 8130 if (enabled && b->pspace->executing_startup
8bea4e01
UW
8131 && (b->type == bp_breakpoint
8132 || b->type == bp_hardware_breakpoint))
8133 b->enable_state = bp_startup_disabled;
8134
84f4c1fe
PM
8135 if (internal)
8136 /* Do not mention breakpoints with a negative number,
8137 but do notify observers. */
8138 observer_notify_breakpoint_created (b->number);
8139 else
8140 mention (b);
0101ce28
JJ
8141 }
8142
c3f6f71d 8143 if (sals.nelts > 1)
95a42b64 8144 {
3e43a32a
MS
8145 warning (_("Multiple breakpoints were set.\nUse the "
8146 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 8147 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
8148 }
8149
80c99de1
PA
8150 /* That's it. Discard the cleanups for data inserted into the
8151 breakpoint. */
8152 discard_cleanups (bkpt_chain);
8153 /* But cleanup everything else. */
c3f6f71d 8154 do_cleanups (old_chain);
217dc9e2 8155
80c99de1 8156 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 8157 update_global_location_list (1);
fd9b8c24
PA
8158
8159 return 1;
c3f6f71d 8160}
c906108c 8161
72b2ff0e
VP
8162/* Set a breakpoint.
8163 ARG is a string describing breakpoint address,
8164 condition, and thread.
8165 FLAG specifies if a breakpoint is hardware on,
8166 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8167 and BP_TEMPFLAG. */
8168
98deb0da 8169static void
72b2ff0e 8170break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 8171{
72b2ff0e 8172 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
8173 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8174 ? bp_hardware_breakpoint
8175 : bp_breakpoint);
c3f6f71d 8176
8cdf0e15
VP
8177 create_breakpoint (get_current_arch (),
8178 arg,
8179 NULL, 0, 1 /* parse arg */,
0fb4aa4b 8180 tempflag, type_wanted,
8cdf0e15
VP
8181 0 /* Ignore count */,
8182 pending_break_support,
8183 NULL /* breakpoint_ops */,
8184 from_tty,
84f4c1fe
PM
8185 1 /* enabled */,
8186 0 /* internal */);
c906108c
SS
8187}
8188
72b2ff0e 8189
c906108c
SS
8190/* Helper function for break_command_1 and disassemble_command. */
8191
8192void
fba45db2 8193resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
8194{
8195 CORE_ADDR pc;
8196
8197 if (sal->pc == 0 && sal->symtab != NULL)
8198 {
8199 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 8200 error (_("No line %d in file \"%s\"."),
c906108c
SS
8201 sal->line, sal->symtab->filename);
8202 sal->pc = pc;
6a048695 8203
4a64f543
MS
8204 /* If this SAL corresponds to a breakpoint inserted using a line
8205 number, then skip the function prologue if necessary. */
6a048695 8206 if (sal->explicit_line)
059acae7 8207 skip_prologue_sal (sal);
c906108c
SS
8208 }
8209
8210 if (sal->section == 0 && sal->symtab != NULL)
8211 {
8212 struct blockvector *bv;
c5aa993b
JM
8213 struct block *b;
8214 struct symbol *sym;
c906108c 8215
801e3a5b 8216 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
8217 if (bv != NULL)
8218 {
7f0df278 8219 sym = block_linkage_function (b);
c906108c
SS
8220 if (sym != NULL)
8221 {
8222 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 8223 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
8224 }
8225 else
8226 {
4a64f543
MS
8227 /* It really is worthwhile to have the section, so we'll
8228 just have to look harder. This case can be executed
8229 if we have line numbers but no functions (as can
8230 happen in assembly source). */
c906108c 8231
c5aa993b 8232 struct minimal_symbol *msym;
6c95b8df
PA
8233 struct cleanup *old_chain = save_current_space_and_thread ();
8234
8235 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8236
8237 msym = lookup_minimal_symbol_by_pc (sal->pc);
8238 if (msym)
714835d5 8239 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8240
8241 do_cleanups (old_chain);
c906108c
SS
8242 }
8243 }
8244 }
8245}
8246
8247void
fba45db2 8248break_command (char *arg, int from_tty)
c906108c 8249{
db107f19 8250 break_command_1 (arg, 0, from_tty);
c906108c
SS
8251}
8252
c906108c 8253void
fba45db2 8254tbreak_command (char *arg, int from_tty)
c906108c 8255{
db107f19 8256 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8257}
8258
c906108c 8259static void
fba45db2 8260hbreak_command (char *arg, int from_tty)
c906108c 8261{
db107f19 8262 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8263}
8264
8265static void
fba45db2 8266thbreak_command (char *arg, int from_tty)
c906108c 8267{
db107f19 8268 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8269}
8270
8271static void
fba45db2 8272stop_command (char *arg, int from_tty)
c906108c 8273{
a3f17187 8274 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8275Usage: stop in <function | address>\n\
a3f17187 8276 stop at <line>\n"));
c906108c
SS
8277}
8278
8279static void
fba45db2 8280stopin_command (char *arg, int from_tty)
c906108c
SS
8281{
8282 int badInput = 0;
8283
c5aa993b 8284 if (arg == (char *) NULL)
c906108c
SS
8285 badInput = 1;
8286 else if (*arg != '*')
8287 {
8288 char *argptr = arg;
8289 int hasColon = 0;
8290
4a64f543 8291 /* Look for a ':'. If this is a line number specification, then
53a5351d 8292 say it is bad, otherwise, it should be an address or
4a64f543 8293 function/method name. */
c906108c 8294 while (*argptr && !hasColon)
c5aa993b
JM
8295 {
8296 hasColon = (*argptr == ':');
8297 argptr++;
8298 }
c906108c
SS
8299
8300 if (hasColon)
c5aa993b 8301 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8302 else
c5aa993b 8303 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8304 }
8305
8306 if (badInput)
a3f17187 8307 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8308 else
db107f19 8309 break_command_1 (arg, 0, from_tty);
c906108c
SS
8310}
8311
8312static void
fba45db2 8313stopat_command (char *arg, int from_tty)
c906108c
SS
8314{
8315 int badInput = 0;
8316
c5aa993b 8317 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8318 badInput = 1;
8319 else
8320 {
8321 char *argptr = arg;
8322 int hasColon = 0;
8323
4a64f543
MS
8324 /* Look for a ':'. If there is a '::' then get out, otherwise
8325 it is probably a line number. */
c906108c 8326 while (*argptr && !hasColon)
c5aa993b
JM
8327 {
8328 hasColon = (*argptr == ':');
8329 argptr++;
8330 }
c906108c
SS
8331
8332 if (hasColon)
c5aa993b 8333 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8334 else
c5aa993b 8335 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8336 }
8337
8338 if (badInput)
a3f17187 8339 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8340 else
db107f19 8341 break_command_1 (arg, 0, from_tty);
c906108c
SS
8342}
8343
4a64f543
MS
8344/* Return non-zero if EXP is verified as constant. Returned zero
8345 means EXP is variable. Also the constant detection may fail for
8346 some constant expressions and in such case still falsely return
8347 zero. */
65d79d4b
SDJ
8348static int
8349watchpoint_exp_is_const (const struct expression *exp)
8350{
8351 int i = exp->nelts;
8352
8353 while (i > 0)
8354 {
8355 int oplenp, argsp;
8356
8357 /* We are only interested in the descriptor of each element. */
8358 operator_length (exp, i, &oplenp, &argsp);
8359 i -= oplenp;
8360
8361 switch (exp->elts[i].opcode)
8362 {
8363 case BINOP_ADD:
8364 case BINOP_SUB:
8365 case BINOP_MUL:
8366 case BINOP_DIV:
8367 case BINOP_REM:
8368 case BINOP_MOD:
8369 case BINOP_LSH:
8370 case BINOP_RSH:
8371 case BINOP_LOGICAL_AND:
8372 case BINOP_LOGICAL_OR:
8373 case BINOP_BITWISE_AND:
8374 case BINOP_BITWISE_IOR:
8375 case BINOP_BITWISE_XOR:
8376 case BINOP_EQUAL:
8377 case BINOP_NOTEQUAL:
8378 case BINOP_LESS:
8379 case BINOP_GTR:
8380 case BINOP_LEQ:
8381 case BINOP_GEQ:
8382 case BINOP_REPEAT:
8383 case BINOP_COMMA:
8384 case BINOP_EXP:
8385 case BINOP_MIN:
8386 case BINOP_MAX:
8387 case BINOP_INTDIV:
8388 case BINOP_CONCAT:
8389 case BINOP_IN:
8390 case BINOP_RANGE:
8391 case TERNOP_COND:
8392 case TERNOP_SLICE:
8393 case TERNOP_SLICE_COUNT:
8394
8395 case OP_LONG:
8396 case OP_DOUBLE:
8397 case OP_DECFLOAT:
8398 case OP_LAST:
8399 case OP_COMPLEX:
8400 case OP_STRING:
8401 case OP_BITSTRING:
8402 case OP_ARRAY:
8403 case OP_TYPE:
8404 case OP_NAME:
8405 case OP_OBJC_NSSTRING:
8406
8407 case UNOP_NEG:
8408 case UNOP_LOGICAL_NOT:
8409 case UNOP_COMPLEMENT:
8410 case UNOP_ADDR:
8411 case UNOP_HIGH:
4a64f543
MS
8412 /* Unary, binary and ternary operators: We have to check
8413 their operands. If they are constant, then so is the
8414 result of that operation. For instance, if A and B are
8415 determined to be constants, then so is "A + B".
8416
8417 UNOP_IND is one exception to the rule above, because the
8418 value of *ADDR is not necessarily a constant, even when
8419 ADDR is. */
65d79d4b
SDJ
8420 break;
8421
8422 case OP_VAR_VALUE:
8423 /* Check whether the associated symbol is a constant.
4a64f543 8424
65d79d4b 8425 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8426 possible that a buggy compiler could mark a variable as
8427 constant even when it is not, and TYPE_CONST would return
8428 true in this case, while SYMBOL_CLASS wouldn't.
8429
8430 We also have to check for function symbols because they
8431 are always constant. */
65d79d4b
SDJ
8432 {
8433 struct symbol *s = exp->elts[i + 2].symbol;
8434
8435 if (SYMBOL_CLASS (s) != LOC_BLOCK
8436 && SYMBOL_CLASS (s) != LOC_CONST
8437 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8438 return 0;
8439 break;
8440 }
8441
8442 /* The default action is to return 0 because we are using
8443 the optimistic approach here: If we don't know something,
8444 then it is not a constant. */
8445 default:
8446 return 0;
8447 }
8448 }
8449
8450 return 1;
8451}
8452
77b06cd7
TJB
8453/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8454
8455static int
8456insert_watchpoint (struct bp_location *bl)
8457{
e09342b5
TJB
8458 int length = bl->owner->exact? 1 : bl->length;
8459
8460 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
8461 bl->owner->cond_exp);
77b06cd7
TJB
8462}
8463
8464/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8465
8466static int
8467remove_watchpoint (struct bp_location *bl)
8468{
e09342b5
TJB
8469 int length = bl->owner->exact? 1 : bl->length;
8470
8471 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
8472 bl->owner->cond_exp);
8473}
8474
8475/* Implement the "resources_needed" breakpoint_ops method for
8476 hardware watchpoints. */
8477
8478static int
8479resources_needed_watchpoint (const struct bp_location *bl)
8480{
90e4670f 8481 int length = bl->owner->exact? 1 : bl->length;
e09342b5 8482
90e4670f 8483 return target_region_ok_for_hw_watchpoint (bl->address, length);
77b06cd7
TJB
8484}
8485
8486/* The breakpoint_ops structure to be used in hardware watchpoints. */
8487
8488static struct breakpoint_ops watchpoint_breakpoint_ops =
8489{
8490 insert_watchpoint,
8491 remove_watchpoint,
8492 NULL, /* breakpoint_hit */
e09342b5 8493 resources_needed_watchpoint,
77b06cd7
TJB
8494 NULL, /* print_it */
8495 NULL, /* print_one */
8496 NULL, /* print_mention */
8497 NULL /* print_recreate */
8498};
8499
53a5351d
JM
8500/* accessflag: hw_write: watch write,
8501 hw_read: watch read,
8502 hw_access: watch access (read or write) */
c906108c 8503static void
84f4c1fe
PM
8504watch_command_1 (char *arg, int accessflag, int from_tty,
8505 int just_location, int internal)
c906108c 8506{
d983da9c 8507 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 8508 struct expression *exp;
60e1c644 8509 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 8510 struct value *val, *mark, *result;
c906108c 8511 struct frame_info *frame;
c906108c
SS
8512 char *exp_start = NULL;
8513 char *exp_end = NULL;
37e4754d 8514 char *tok, *id_tok_start, *end_tok;
c906108c
SS
8515 int toklen;
8516 char *cond_start = NULL;
8517 char *cond_end = NULL;
c906108c
SS
8518 int i, other_type_used, target_resources_ok = 0;
8519 enum bptype bp_type;
e09342b5 8520 int reg_cnt = 0;
37e4754d 8521 int thread = -1;
0cf6dd15 8522 int pc = 0;
c906108c 8523
37e4754d
LM
8524 /* Make sure that we actually have parameters to parse. */
8525 if (arg != NULL && arg[0] != '\0')
8526 {
8527 toklen = strlen (arg); /* Size of argument list. */
8528
8529 /* Points tok to the end of the argument list. */
8530 tok = arg + toklen - 1;
8531
4a64f543
MS
8532 /* Go backwards in the parameters list. Skip the last
8533 parameter. If we're expecting a 'thread <thread_num>'
8534 parameter, this should be the thread identifier. */
37e4754d
LM
8535 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8536 tok--;
8537 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8538 tok--;
8539
8540 /* Points end_tok to the beginning of the last token. */
8541 id_tok_start = tok + 1;
8542
4a64f543
MS
8543 /* Go backwards in the parameters list. Skip one more
8544 parameter. If we're expecting a 'thread <thread_num>'
8545 parameter, we should reach a "thread" token. */
37e4754d
LM
8546 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8547 tok--;
8548
8549 end_tok = tok;
8550
8551 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8552 tok--;
8553
8554 /* Move the pointer forward to skip the whitespace and
8555 calculate the length of the token. */
8556 tok++;
8557 toklen = end_tok - tok;
8558
8559 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8560 {
8561 /* At this point we've found a "thread" token, which means
8562 the user is trying to set a watchpoint that triggers
8563 only in a specific thread. */
8564 char *endp;
8565
8566 /* Extract the thread ID from the next token. */
8567 thread = strtol (id_tok_start, &endp, 0);
8568
8569 /* Check if the user provided a valid numeric value for the
8570 thread ID. */
8571 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
8572 error (_("Invalid thread ID specification %s."), id_tok_start);
8573
8574 /* Check if the thread actually exists. */
8575 if (!valid_thread_id (thread))
8576 error (_("Unknown thread %d."), thread);
8577
8578 /* Truncate the string and get rid of the thread <thread_num>
8579 parameter before the parameter list is parsed by the
8580 evaluate_expression() function. */
8581 *tok = '\0';
8582 }
8583 }
8584
8585 /* Parse the rest of the arguments. */
c906108c
SS
8586 innermost_block = NULL;
8587 exp_start = arg;
8588 exp = parse_exp_1 (&arg, 0, 0);
8589 exp_end = arg;
fa8a61dc
TT
8590 /* Remove trailing whitespace from the expression before saving it.
8591 This makes the eventual display of the expression string a bit
8592 prettier. */
8593 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
8594 --exp_end;
8595
65d79d4b
SDJ
8596 /* Checking if the expression is not constant. */
8597 if (watchpoint_exp_is_const (exp))
8598 {
8599 int len;
8600
8601 len = exp_end - exp_start;
8602 while (len > 0 && isspace (exp_start[len - 1]))
8603 len--;
8604 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
8605 }
8606
c906108c
SS
8607 exp_valid_block = innermost_block;
8608 mark = value_mark ();
a1442452 8609 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
8610
8611 if (just_location)
8612 {
8613 exp_valid_block = NULL;
a1442452 8614 val = value_addr (result);
06a64a0b
TT
8615 release_value (val);
8616 value_free_to_mark (mark);
8617 }
8618 else if (val != NULL)
fa4727a6 8619 release_value (val);
c906108c
SS
8620
8621 tok = arg;
8622 while (*tok == ' ' || *tok == '\t')
8623 tok++;
8624 end_tok = tok;
8625
8626 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
8627 end_tok++;
8628
8629 toklen = end_tok - tok;
8630 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8631 {
2d134ed3
PA
8632 struct expression *cond;
8633
60e1c644 8634 innermost_block = NULL;
c906108c
SS
8635 tok = cond_start = end_tok + 1;
8636 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
8637
8638 /* The watchpoint expression may not be local, but the condition
8639 may still be. E.g.: `watch global if local > 0'. */
8640 cond_exp_valid_block = innermost_block;
8641
2d134ed3 8642 xfree (cond);
c906108c
SS
8643 cond_end = tok;
8644 }
8645 if (*tok)
8a3fe4f8 8646 error (_("Junk at end of command."));
c906108c 8647
53a5351d 8648 if (accessflag == hw_read)
c5aa993b 8649 bp_type = bp_read_watchpoint;
53a5351d 8650 else if (accessflag == hw_access)
c5aa993b
JM
8651 bp_type = bp_access_watchpoint;
8652 else
8653 bp_type = bp_hardware_watchpoint;
c906108c 8654
e09342b5
TJB
8655 reg_cnt = can_use_hardware_watchpoint (val, target_exact_watchpoints);
8656 if (reg_cnt == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 8657 error (_("Expression cannot be implemented with read/access watchpoint."));
e09342b5 8658 if (reg_cnt != 0)
c5aa993b
JM
8659 {
8660 i = hw_watchpoint_used_count (bp_type, &other_type_used);
53a5351d 8661 target_resources_ok =
e09342b5 8662 target_can_use_hardware_watchpoint (bp_type, i + reg_cnt,
53a5351d 8663 other_type_used);
c5aa993b 8664 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 8665 error (_("Target does not support this type of hardware watchpoint."));
53a5351d 8666
c5aa993b 8667 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
3e43a32a
MS
8668 error (_("Target can only support one kind "
8669 "of HW watchpoint at a time."));
c5aa993b 8670 }
c906108c 8671
4a64f543
MS
8672 /* Change the type of breakpoint to an ordinary watchpoint if a
8673 hardware watchpoint could not be set. */
e09342b5 8674 if (!reg_cnt || target_resources_ok <= 0)
4d28f7a8
KB
8675 bp_type = bp_watchpoint;
8676
d983da9c 8677 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
8678
8679 /* If the expression is "local", then set up a "watchpoint scope"
8680 breakpoint at the point where we've left the scope of the watchpoint
8681 expression. Create the scope breakpoint before the watchpoint, so
8682 that we will encounter it first in bpstat_stop_status. */
60e1c644 8683 if (exp_valid_block && frame)
d983da9c 8684 {
edb3359d
DJ
8685 if (frame_id_p (frame_unwind_caller_id (frame)))
8686 {
8687 scope_breakpoint
a6d9a66e
UW
8688 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
8689 frame_unwind_caller_pc (frame),
edb3359d 8690 bp_watchpoint_scope);
d983da9c 8691
edb3359d 8692 scope_breakpoint->enable_state = bp_enabled;
d983da9c 8693
edb3359d
DJ
8694 /* Automatically delete the breakpoint when it hits. */
8695 scope_breakpoint->disposition = disp_del;
d983da9c 8696
edb3359d
DJ
8697 /* Only break in the proper frame (help with recursion). */
8698 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 8699
edb3359d 8700 /* Set the address at which we will stop. */
a6d9a66e
UW
8701 scope_breakpoint->loc->gdbarch
8702 = frame_unwind_caller_arch (frame);
edb3359d
DJ
8703 scope_breakpoint->loc->requested_address
8704 = frame_unwind_caller_pc (frame);
8705 scope_breakpoint->loc->address
a6d9a66e
UW
8706 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
8707 scope_breakpoint->loc->requested_address,
edb3359d
DJ
8708 scope_breakpoint->type);
8709 }
d983da9c
DJ
8710 }
8711
c906108c 8712 /* Now set up the breakpoint. */
2d134ed3 8713 b = set_raw_breakpoint_without_location (NULL, bp_type);
84f4c1fe 8714 set_breakpoint_number (internal, b);
37e4754d 8715 b->thread = thread;
b5de0fa7 8716 b->disposition = disp_donttouch;
c906108c
SS
8717 b->exp = exp;
8718 b->exp_valid_block = exp_valid_block;
60e1c644 8719 b->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
8720 if (just_location)
8721 {
8722 struct type *t = value_type (val);
8723 CORE_ADDR addr = value_as_address (val);
8724 char *name;
8725
8726 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
8727 name = type_to_string (t);
8728
d63d0675
JK
8729 b->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
8730 core_addr_to_string (addr));
06a64a0b
TT
8731 xfree (name);
8732
d63d0675
JK
8733 b->exp_string = xstrprintf ("-location: %.*s",
8734 (int) (exp_end - exp_start), exp_start);
8735
06a64a0b
TT
8736 /* The above expression is in C. */
8737 b->language = language_c;
8738 }
8739 else
8740 b->exp_string = savestring (exp_start, exp_end - exp_start);
c906108c 8741 b->val = val;
fa4727a6 8742 b->val_valid = 1;
77b06cd7
TJB
8743 b->ops = &watchpoint_breakpoint_ops;
8744
e09342b5
TJB
8745 /* Use an exact watchpoint when there's only one memory region to be
8746 watched, and only one debug register is needed to watch it. */
8747 b->exact = target_exact_watchpoints && reg_cnt == 1;
8748
c906108c
SS
8749 if (cond_start)
8750 b->cond_string = savestring (cond_start, cond_end - cond_start);
8751 else
8752 b->cond_string = 0;
c5aa993b 8753
c906108c 8754 if (frame)
f6bc2008
PA
8755 {
8756 b->watchpoint_frame = get_frame_id (frame);
8757 b->watchpoint_thread = inferior_ptid;
8758 }
c906108c 8759 else
f6bc2008
PA
8760 {
8761 b->watchpoint_frame = null_frame_id;
8762 b->watchpoint_thread = null_ptid;
8763 }
c906108c 8764
d983da9c 8765 if (scope_breakpoint != NULL)
c906108c 8766 {
d983da9c
DJ
8767 /* The scope breakpoint is related to the watchpoint. We will
8768 need to act on them together. */
8769 b->related_breakpoint = scope_breakpoint;
8770 scope_breakpoint->related_breakpoint = b;
c906108c 8771 }
d983da9c 8772
06a64a0b
TT
8773 if (!just_location)
8774 value_free_to_mark (mark);
2d134ed3
PA
8775
8776 /* Finally update the new watchpoint. This creates the locations
8777 that should be inserted. */
8778 update_watchpoint (b, 1);
84f4c1fe
PM
8779 if (internal)
8780 /* Do not mention breakpoints with a negative number, but do
8781 notify observers. */
8782 observer_notify_breakpoint_created (b->number);
8783 else
8784 mention (b);
b60e7edf 8785 update_global_location_list (1);
c906108c
SS
8786}
8787
e09342b5
TJB
8788/* Return count of debug registers needed to watch the given expression.
8789 If EXACT_WATCHPOINTS is 1, then consider that only the address of
8790 the start of the watched region will be monitored (i.e., all accesses
8791 will be aligned). This uses less debug registers on some targets.
8792
8793 If the watchpoint cannot be handled in hardware return zero. */
c906108c 8794
c906108c 8795static int
e09342b5 8796can_use_hardware_watchpoint (struct value *v, int exact_watchpoints)
c906108c
SS
8797{
8798 int found_memory_cnt = 0;
2e70b7b9 8799 struct value *head = v;
c906108c
SS
8800
8801 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 8802 if (!can_use_hw_watchpoints)
c906108c 8803 return 0;
c5aa993b 8804
5c44784c
JM
8805 /* Make sure that the value of the expression depends only upon
8806 memory contents, and values computed from them within GDB. If we
8807 find any register references or function calls, we can't use a
8808 hardware watchpoint.
8809
8810 The idea here is that evaluating an expression generates a series
8811 of values, one holding the value of every subexpression. (The
8812 expression a*b+c has five subexpressions: a, b, a*b, c, and
8813 a*b+c.) GDB's values hold almost enough information to establish
8814 the criteria given above --- they identify memory lvalues,
8815 register lvalues, computed values, etcetera. So we can evaluate
8816 the expression, and then scan the chain of values that leaves
8817 behind to decide whether we can detect any possible change to the
8818 expression's final value using only hardware watchpoints.
8819
8820 However, I don't think that the values returned by inferior
8821 function calls are special in any way. So this function may not
8822 notice that an expression involving an inferior function call
8823 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 8824 for (; v; v = value_next (v))
c906108c 8825 {
5c44784c 8826 if (VALUE_LVAL (v) == lval_memory)
c906108c 8827 {
8464be76
DJ
8828 if (v != head && value_lazy (v))
8829 /* A lazy memory lvalue in the chain is one that GDB never
8830 needed to fetch; we either just used its address (e.g.,
8831 `a' in `a.b') or we never needed it at all (e.g., `a'
8832 in `a,b'). This doesn't apply to HEAD; if that is
8833 lazy then it was not readable, but watch it anyway. */
5c44784c 8834 ;
53a5351d 8835 else
5c44784c
JM
8836 {
8837 /* Ahh, memory we actually used! Check if we can cover
8838 it with hardware watchpoints. */
df407dfe 8839 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
8840
8841 /* We only watch structs and arrays if user asked for it
8842 explicitly, never if they just happen to appear in a
8843 middle of some value chain. */
8844 if (v == head
8845 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
8846 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
8847 {
42ae5230 8848 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
8849 int len;
8850 int num_regs;
8851
8852 len = (exact_watchpoints
8853 && is_scalar_type_recursive (vtype))?
8854 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 8855
e09342b5
TJB
8856 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
8857 if (!num_regs)
2e70b7b9
MS
8858 return 0;
8859 else
e09342b5 8860 found_memory_cnt += num_regs;
2e70b7b9 8861 }
5c44784c 8862 }
c5aa993b 8863 }
5086187c
AC
8864 else if (VALUE_LVAL (v) != not_lval
8865 && deprecated_value_modifiable (v) == 0)
38b6c3b3 8866 return 0; /* These are values from the history (e.g., $1). */
5086187c 8867 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 8868 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
8869 }
8870
8871 /* The expression itself looks suitable for using a hardware
8872 watchpoint, but give the target machine a chance to reject it. */
8873 return found_memory_cnt;
8874}
8875
8b93c638 8876void
84f4c1fe 8877watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 8878{
84f4c1fe 8879 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
8880}
8881
8882/* A helper function that looks for an argument at the start of a
8883 string. The argument must also either be at the end of the string,
8884 or be followed by whitespace. Returns 1 if it finds the argument,
8885 0 otherwise. If the argument is found, it updates *STR. */
8886
8887static int
8888check_for_argument (char **str, char *arg, int arg_len)
8889{
8890 if (strncmp (*str, arg, arg_len) == 0
8891 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
8892 {
8893 *str += arg_len;
8894 return 1;
8895 }
8896 return 0;
8897}
8898
8899/* A helper function that looks for the "-location" argument and then
8900 calls watch_command_1. */
8901
8902static void
8903watch_maybe_just_location (char *arg, int accessflag, int from_tty)
8904{
8905 int just_location = 0;
8906
8907 if (arg
8908 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
8909 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
8910 {
8911 ep_skip_leading_whitespace (&arg);
8912 just_location = 1;
8913 }
8914
84f4c1fe 8915 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 8916}
8926118c 8917
c5aa993b 8918static void
fba45db2 8919watch_command (char *arg, int from_tty)
c906108c 8920{
06a64a0b 8921 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
8922}
8923
8b93c638 8924void
84f4c1fe 8925rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 8926{
84f4c1fe 8927 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 8928}
8926118c 8929
c5aa993b 8930static void
fba45db2 8931rwatch_command (char *arg, int from_tty)
c906108c 8932{
06a64a0b 8933 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
8934}
8935
8b93c638 8936void
84f4c1fe 8937awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 8938{
84f4c1fe 8939 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 8940}
8926118c 8941
c5aa993b 8942static void
fba45db2 8943awatch_command (char *arg, int from_tty)
c906108c 8944{
06a64a0b 8945 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 8946}
c906108c 8947\f
c5aa993b 8948
43ff13b4 8949/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
8950 because it uses the mechanisms of breakpoints. */
8951
bfec99b2
PA
8952struct until_break_command_continuation_args
8953{
8954 struct breakpoint *breakpoint;
8955 struct breakpoint *breakpoint2;
186c406b 8956 int thread_num;
bfec99b2
PA
8957};
8958
43ff13b4 8959/* This function is called by fetch_inferior_event via the
4a64f543 8960 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 8961 care of cleaning up the temporary breakpoints set up by the until
4a64f543 8962 command. */
c2c6d25f 8963static void
604ead4a 8964until_break_command_continuation (void *arg)
43ff13b4 8965{
bfec99b2
PA
8966 struct until_break_command_continuation_args *a = arg;
8967
8968 delete_breakpoint (a->breakpoint);
8969 if (a->breakpoint2)
8970 delete_breakpoint (a->breakpoint2);
186c406b 8971 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
8972}
8973
c906108c 8974void
ae66c1fc 8975until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
8976{
8977 struct symtabs_and_lines sals;
8978 struct symtab_and_line sal;
206415a3 8979 struct frame_info *frame = get_selected_frame (NULL);
c906108c 8980 struct breakpoint *breakpoint;
f107f563 8981 struct breakpoint *breakpoint2 = NULL;
c906108c 8982 struct cleanup *old_chain;
186c406b
TT
8983 int thread;
8984 struct thread_info *tp;
c906108c
SS
8985
8986 clear_proceed_status ();
8987
8988 /* Set a breakpoint where the user wants it and at return from
4a64f543 8989 this function. */
c5aa993b 8990
c906108c
SS
8991 if (default_breakpoint_valid)
8992 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
68219205 8993 default_breakpoint_line, (char ***) NULL, NULL);
c906108c 8994 else
53a5351d 8995 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL,
68219205 8996 0, (char ***) NULL, NULL);
c5aa993b 8997
c906108c 8998 if (sals.nelts != 1)
8a3fe4f8 8999 error (_("Couldn't get information on specified line."));
c5aa993b 9000
c906108c 9001 sal = sals.sals[0];
4a64f543 9002 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9003
c906108c 9004 if (*arg)
8a3fe4f8 9005 error (_("Junk at end of arguments."));
c5aa993b 9006
c906108c 9007 resolve_sal_pc (&sal);
c5aa993b 9008
ae66c1fc
EZ
9009 if (anywhere)
9010 /* If the user told us to continue until a specified location,
9011 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9012 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9013 null_frame_id, bp_until);
ae66c1fc 9014 else
4a64f543
MS
9015 /* Otherwise, specify the selected frame, because we want to stop
9016 only at the very same frame. */
a6d9a66e
UW
9017 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9018 get_stack_frame_id (frame),
ae66c1fc 9019 bp_until);
c5aa993b 9020
f107f563 9021 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9022
186c406b
TT
9023 tp = inferior_thread ();
9024 thread = tp->num;
9025
ae66c1fc
EZ
9026 /* Keep within the current frame, or in frames called by the current
9027 one. */
edb3359d
DJ
9028
9029 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9030 {
edb3359d
DJ
9031 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9032 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9033 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9034 sal,
edb3359d 9035 frame_unwind_caller_id (frame),
f107f563
VP
9036 bp_until);
9037 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9038
9039 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9040 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9041 }
c5aa993b 9042
c906108c 9043 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9044
4a64f543
MS
9045 /* If we are running asynchronously, and proceed call above has
9046 actually managed to start the target, arrange for breakpoints to
9047 be deleted when the target stops. Otherwise, we're already
9048 stopped and delete breakpoints via cleanup chain. */
f107f563 9049
8ea051c5 9050 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9051 {
bfec99b2
PA
9052 struct until_break_command_continuation_args *args;
9053 args = xmalloc (sizeof (*args));
f107f563 9054
bfec99b2
PA
9055 args->breakpoint = breakpoint;
9056 args->breakpoint2 = breakpoint2;
186c406b 9057 args->thread_num = thread;
f107f563
VP
9058
9059 discard_cleanups (old_chain);
95e54da7
PA
9060 add_continuation (inferior_thread (),
9061 until_break_command_continuation, args,
604ead4a 9062 xfree);
f107f563
VP
9063 }
9064 else
c5aa993b 9065 do_cleanups (old_chain);
c906108c 9066}
ae66c1fc 9067
c906108c 9068static void
fba45db2 9069ep_skip_leading_whitespace (char **s)
c906108c 9070{
c5aa993b
JM
9071 if ((s == NULL) || (*s == NULL))
9072 return;
9073 while (isspace (**s))
9074 *s += 1;
c906108c 9075}
c5aa993b 9076
c906108c
SS
9077/* This function attempts to parse an optional "if <cond>" clause
9078 from the arg string. If one is not found, it returns NULL.
c5aa993b 9079
c906108c
SS
9080 Else, it returns a pointer to the condition string. (It does not
9081 attempt to evaluate the string against a particular block.) And,
9082 it updates arg to point to the first character following the parsed
4a64f543 9083 if clause in the arg string. */
53a5351d 9084
c906108c 9085static char *
fba45db2 9086ep_parse_optional_if_clause (char **arg)
c906108c 9087{
c5aa993b
JM
9088 char *cond_string;
9089
9090 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9091 return NULL;
c5aa993b 9092
4a64f543 9093 /* Skip the "if" keyword. */
c906108c 9094 (*arg) += 2;
c5aa993b 9095
c906108c 9096 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9097 condition string. */
c906108c
SS
9098 ep_skip_leading_whitespace (arg);
9099 cond_string = *arg;
c5aa993b 9100
4a64f543
MS
9101 /* Assume that the condition occupies the remainder of the arg
9102 string. */
c906108c 9103 (*arg) += strlen (cond_string);
c5aa993b 9104
c906108c
SS
9105 return cond_string;
9106}
c5aa993b 9107
c906108c
SS
9108/* Commands to deal with catching events, such as signals, exceptions,
9109 process start/exit, etc. */
c5aa993b
JM
9110
9111typedef enum
9112{
44feb3ce
TT
9113 catch_fork_temporary, catch_vfork_temporary,
9114 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9115}
9116catch_fork_kind;
9117
c906108c 9118static void
cc59ec59
MS
9119catch_fork_command_1 (char *arg, int from_tty,
9120 struct cmd_list_element *command)
c906108c 9121{
a6d9a66e 9122 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9123 char *cond_string = NULL;
44feb3ce
TT
9124 catch_fork_kind fork_kind;
9125 int tempflag;
9126
9127 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9128 tempflag = (fork_kind == catch_fork_temporary
9129 || fork_kind == catch_vfork_temporary);
c5aa993b 9130
44feb3ce
TT
9131 if (!arg)
9132 arg = "";
c906108c 9133 ep_skip_leading_whitespace (&arg);
c5aa993b 9134
c906108c 9135 /* The allowed syntax is:
c5aa993b
JM
9136 catch [v]fork
9137 catch [v]fork if <cond>
9138
4a64f543 9139 First, check if there's an if clause. */
c906108c 9140 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9141
c906108c 9142 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9143 error (_("Junk at end of arguments."));
c5aa993b 9144
c906108c 9145 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9146 and enable reporting of such events. */
c5aa993b
JM
9147 switch (fork_kind)
9148 {
44feb3ce
TT
9149 case catch_fork_temporary:
9150 case catch_fork_permanent:
a6d9a66e 9151 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9152 &catch_fork_breakpoint_ops);
c906108c 9153 break;
44feb3ce
TT
9154 case catch_vfork_temporary:
9155 case catch_vfork_permanent:
a6d9a66e 9156 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9157 &catch_vfork_breakpoint_ops);
c906108c 9158 break;
c5aa993b 9159 default:
8a3fe4f8 9160 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9161 break;
c5aa993b 9162 }
c906108c
SS
9163}
9164
9165static void
cc59ec59
MS
9166catch_exec_command_1 (char *arg, int from_tty,
9167 struct cmd_list_element *command)
c906108c 9168{
a6d9a66e 9169 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9170 int tempflag;
c5aa993b 9171 char *cond_string = NULL;
c906108c 9172
44feb3ce
TT
9173 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9174
9175 if (!arg)
9176 arg = "";
c906108c
SS
9177 ep_skip_leading_whitespace (&arg);
9178
9179 /* The allowed syntax is:
c5aa993b
JM
9180 catch exec
9181 catch exec if <cond>
c906108c 9182
4a64f543 9183 First, check if there's an if clause. */
c906108c
SS
9184 cond_string = ep_parse_optional_if_clause (&arg);
9185
9186 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9187 error (_("Junk at end of arguments."));
c906108c
SS
9188
9189 /* If this target supports it, create an exec catchpoint
4a64f543 9190 and enable reporting of such events. */
a6d9a66e
UW
9191 create_catchpoint (gdbarch, tempflag, cond_string,
9192 &catch_exec_breakpoint_ops);
c906108c 9193}
c5aa993b 9194
3086aeae
DJ
9195static enum print_stop_action
9196print_exception_catchpoint (struct breakpoint *b)
9197{
ade92717 9198 int bp_temp, bp_throw;
3086aeae 9199
ade92717 9200 annotate_catchpoint (b->number);
3086aeae 9201
ade92717
AR
9202 bp_throw = strstr (b->addr_string, "throw") != NULL;
9203 if (b->loc->address != b->loc->requested_address)
9204 breakpoint_adjustment_warning (b->loc->requested_address,
9205 b->loc->address,
9206 b->number, 1);
df2b6d2d 9207 bp_temp = b->disposition == disp_del;
ade92717
AR
9208 ui_out_text (uiout,
9209 bp_temp ? "Temporary catchpoint "
9210 : "Catchpoint ");
9211 if (!ui_out_is_mi_like_p (uiout))
9212 ui_out_field_int (uiout, "bkptno", b->number);
9213 ui_out_text (uiout,
c0b37c48
AR
9214 bp_throw ? " (exception thrown), "
9215 : " (exception caught), ");
ade92717
AR
9216 if (ui_out_is_mi_like_p (uiout))
9217 {
9218 ui_out_field_string (uiout, "reason",
9219 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9220 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9221 ui_out_field_int (uiout, "bkptno", b->number);
9222 }
3086aeae
DJ
9223 return PRINT_SRC_AND_LOC;
9224}
9225
9226static void
cc59ec59
MS
9227print_one_exception_catchpoint (struct breakpoint *b,
9228 struct bp_location **last_loc)
3086aeae 9229{
79a45b7d 9230 struct value_print_options opts;
cc59ec59 9231
79a45b7d
TT
9232 get_user_print_options (&opts);
9233 if (opts.addressprint)
3086aeae
DJ
9234 {
9235 annotate_field (4);
604133b5
AR
9236 if (b->loc == NULL || b->loc->shlib_disabled)
9237 ui_out_field_string (uiout, "addr", "<PENDING>");
9238 else
5af949e3
UW
9239 ui_out_field_core_addr (uiout, "addr",
9240 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9241 }
9242 annotate_field (5);
604133b5 9243 if (b->loc)
a6d9a66e 9244 *last_loc = b->loc;
3086aeae
DJ
9245 if (strstr (b->addr_string, "throw") != NULL)
9246 ui_out_field_string (uiout, "what", "exception throw");
9247 else
9248 ui_out_field_string (uiout, "what", "exception catch");
9249}
9250
9251static void
9252print_mention_exception_catchpoint (struct breakpoint *b)
9253{
ade92717
AR
9254 int bp_temp;
9255 int bp_throw;
9256
df2b6d2d 9257 bp_temp = b->disposition == disp_del;
ade92717
AR
9258 bp_throw = strstr (b->addr_string, "throw") != NULL;
9259 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9260 : _("Catchpoint "));
9261 ui_out_field_int (uiout, "bkptno", b->number);
9262 ui_out_text (uiout, bp_throw ? _(" (throw)")
9263 : _(" (catch)"));
3086aeae
DJ
9264}
9265
6149aea9
PA
9266/* Implement the "print_recreate" breakpoint_ops method for throw and
9267 catch catchpoints. */
9268
9269static void
4a64f543
MS
9270print_recreate_exception_catchpoint (struct breakpoint *b,
9271 struct ui_file *fp)
6149aea9
PA
9272{
9273 int bp_temp;
9274 int bp_throw;
9275
9276 bp_temp = b->disposition == disp_del;
9277 bp_throw = strstr (b->addr_string, "throw") != NULL;
9278 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9279 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
9280}
9281
3086aeae 9282static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
ce78b96d
JB
9283 NULL, /* insert */
9284 NULL, /* remove */
9285 NULL, /* breakpoint_hit */
e09342b5 9286 NULL, /* resources_needed */
3086aeae
DJ
9287 print_exception_catchpoint,
9288 print_one_exception_catchpoint,
6149aea9
PA
9289 print_mention_exception_catchpoint,
9290 print_recreate_exception_catchpoint
3086aeae
DJ
9291};
9292
9293static int
9294handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9295 enum exception_event_kind ex_event, int from_tty)
9296{
604133b5
AR
9297 char *trigger_func_name;
9298
3086aeae 9299 if (ex_event == EX_EVENT_CATCH)
604133b5 9300 trigger_func_name = "__cxa_begin_catch";
3086aeae 9301 else
604133b5 9302 trigger_func_name = "__cxa_throw";
3086aeae 9303
8cdf0e15
VP
9304 create_breakpoint (get_current_arch (),
9305 trigger_func_name, cond_string, -1,
9306 0 /* condition and thread are valid. */,
0fb4aa4b 9307 tempflag, bp_breakpoint,
8cdf0e15
VP
9308 0,
9309 AUTO_BOOLEAN_TRUE /* pending */,
9310 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9311 1 /* enabled */,
9312 0 /* internal */);
3086aeae 9313
3086aeae
DJ
9314 return 1;
9315}
9316
4a64f543 9317/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9318
9319static void
fba45db2
KB
9320catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9321 int tempflag, int from_tty)
c906108c 9322{
c5aa993b 9323 char *cond_string = NULL;
c5aa993b 9324
44feb3ce
TT
9325 if (!arg)
9326 arg = "";
c906108c 9327 ep_skip_leading_whitespace (&arg);
c5aa993b 9328
c906108c
SS
9329 cond_string = ep_parse_optional_if_clause (&arg);
9330
9331 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9332 error (_("Junk at end of arguments."));
c906108c 9333
059fb39f
PM
9334 if (ex_event != EX_EVENT_THROW
9335 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9336 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9337
3086aeae
DJ
9338 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9339 return;
9340
8a3fe4f8 9341 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9342}
9343
44feb3ce
TT
9344/* Implementation of "catch catch" command. */
9345
9346static void
9347catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9348{
9349 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9350
44feb3ce
TT
9351 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9352}
9353
9354/* Implementation of "catch throw" command. */
9355
9356static void
9357catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9358{
9359 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9360
44feb3ce
TT
9361 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9362}
9363
f7f9143b
JB
9364/* Create a breakpoint struct for Ada exception catchpoints. */
9365
9366static void
a6d9a66e
UW
9367create_ada_exception_breakpoint (struct gdbarch *gdbarch,
9368 struct symtab_and_line sal,
f7f9143b
JB
9369 char *addr_string,
9370 char *exp_string,
9371 char *cond_string,
9372 struct expression *cond,
9373 struct breakpoint_ops *ops,
9374 int tempflag,
9375 int from_tty)
9376{
9377 struct breakpoint *b;
9378
9379 if (from_tty)
9380 {
5af949e3
UW
9381 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9382 if (!loc_gdbarch)
9383 loc_gdbarch = gdbarch;
9384
6c95b8df
PA
9385 describe_other_breakpoints (loc_gdbarch,
9386 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9387 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9388 version for exception catchpoints, because two catchpoints
9389 used for different exception names will use the same address.
9390 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9391 unproductive. Besides, the warning phrasing is also a bit
f7f9143b
JB
9392 inapropriate, we should use the word catchpoint, and tell
9393 the user what type of catchpoint it is. The above is good
9394 enough for now, though. */
9395 }
9396
a6d9a66e 9397 b = set_raw_breakpoint (gdbarch, sal, bp_breakpoint);
f7f9143b
JB
9398 set_breakpoint_count (breakpoint_count + 1);
9399
9400 b->enable_state = bp_enabled;
9401 b->disposition = tempflag ? disp_del : disp_donttouch;
9402 b->number = breakpoint_count;
9403 b->ignore_count = 0;
511a6cd4 9404 b->loc->cond = cond;
f7f9143b
JB
9405 b->addr_string = addr_string;
9406 b->language = language_ada;
9407 b->cond_string = cond_string;
9408 b->exp_string = exp_string;
9409 b->thread = -1;
9410 b->ops = ops;
f7f9143b
JB
9411
9412 mention (b);
b60e7edf 9413 update_global_location_list (1);
f7f9143b
JB
9414}
9415
9416/* Implement the "catch exception" command. */
9417
9418static void
44feb3ce
TT
9419catch_ada_exception_command (char *arg, int from_tty,
9420 struct cmd_list_element *command)
f7f9143b 9421{
a6d9a66e 9422 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9423 int tempflag;
f7f9143b 9424 struct symtab_and_line sal;
f7f9143b
JB
9425 char *addr_string = NULL;
9426 char *exp_string = NULL;
9427 char *cond_string = NULL;
9428 struct expression *cond = NULL;
9429 struct breakpoint_ops *ops = NULL;
9430
44feb3ce
TT
9431 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9432
9433 if (!arg)
9434 arg = "";
f7f9143b
JB
9435 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
9436 &cond_string, &cond, &ops);
a6d9a66e 9437 create_ada_exception_breakpoint (gdbarch, sal, addr_string, exp_string,
f7f9143b
JB
9438 cond_string, cond, ops, tempflag,
9439 from_tty);
9440}
9441
a96d9b2e
SDJ
9442/* Cleanup function for a syscall filter list. */
9443static void
9444clean_up_filters (void *arg)
9445{
9446 VEC(int) *iter = *(VEC(int) **) arg;
9447 VEC_free (int, iter);
9448}
9449
9450/* Splits the argument using space as delimiter. Returns an xmalloc'd
9451 filter list, or NULL if no filtering is required. */
9452static VEC(int) *
9453catch_syscall_split_args (char *arg)
9454{
9455 VEC(int) *result = NULL;
9456 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9457
9458 while (*arg != '\0')
9459 {
9460 int i, syscall_number;
9461 char *endptr;
9462 char cur_name[128];
9463 struct syscall s;
9464
9465 /* Skip whitespace. */
9466 while (isspace (*arg))
9467 arg++;
9468
9469 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9470 cur_name[i] = arg[i];
9471 cur_name[i] = '\0';
9472 arg += i;
9473
9474 /* Check if the user provided a syscall name or a number. */
9475 syscall_number = (int) strtol (cur_name, &endptr, 0);
9476 if (*endptr == '\0')
bccd0dd2 9477 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9478 else
9479 {
9480 /* We have a name. Let's check if it's valid and convert it
9481 to a number. */
9482 get_syscall_by_name (cur_name, &s);
9483
9484 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
9485 /* Here we have to issue an error instead of a warning,
9486 because GDB cannot do anything useful if there's no
9487 syscall number to be caught. */
a96d9b2e
SDJ
9488 error (_("Unknown syscall name '%s'."), cur_name);
9489 }
9490
9491 /* Ok, it's valid. */
9492 VEC_safe_push (int, result, s.number);
9493 }
9494
9495 discard_cleanups (cleanup);
9496 return result;
9497}
9498
9499/* Implement the "catch syscall" command. */
9500
9501static void
cc59ec59
MS
9502catch_syscall_command_1 (char *arg, int from_tty,
9503 struct cmd_list_element *command)
a96d9b2e
SDJ
9504{
9505 int tempflag;
9506 VEC(int) *filter;
9507 struct syscall s;
9508 struct gdbarch *gdbarch = get_current_arch ();
9509
9510 /* Checking if the feature if supported. */
9511 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9512 error (_("The feature 'catch syscall' is not supported on \
9513this architeture yet."));
9514
9515 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9516
9517 ep_skip_leading_whitespace (&arg);
9518
9519 /* We need to do this first "dummy" translation in order
9520 to get the syscall XML file loaded or, most important,
9521 to display a warning to the user if there's no XML file
9522 for his/her architecture. */
9523 get_syscall_by_number (0, &s);
9524
9525 /* The allowed syntax is:
9526 catch syscall
9527 catch syscall <name | number> [<name | number> ... <name | number>]
9528
9529 Let's check if there's a syscall name. */
9530
9531 if (arg != NULL)
9532 filter = catch_syscall_split_args (arg);
9533 else
9534 filter = NULL;
9535
9536 create_syscall_event_catchpoint (tempflag, filter,
9537 &catch_syscall_breakpoint_ops);
9538}
9539
f7f9143b
JB
9540/* Implement the "catch assert" command. */
9541
9542static void
3e43a32a
MS
9543catch_assert_command (char *arg, int from_tty,
9544 struct cmd_list_element *command)
f7f9143b 9545{
a6d9a66e 9546 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9547 int tempflag;
f7f9143b
JB
9548 struct symtab_and_line sal;
9549 char *addr_string = NULL;
9550 struct breakpoint_ops *ops = NULL;
9551
44feb3ce
TT
9552 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9553
9554 if (!arg)
9555 arg = "";
f7f9143b 9556 sal = ada_decode_assert_location (arg, &addr_string, &ops);
a6d9a66e
UW
9557 create_ada_exception_breakpoint (gdbarch, sal, addr_string, NULL, NULL, NULL,
9558 ops, tempflag, from_tty);
f7f9143b
JB
9559}
9560
c906108c 9561static void
fba45db2 9562catch_command (char *arg, int from_tty)
c906108c 9563{
44feb3ce 9564 error (_("Catch requires an event name."));
c906108c
SS
9565}
9566\f
9567
9568static void
fba45db2 9569tcatch_command (char *arg, int from_tty)
c906108c 9570{
44feb3ce 9571 error (_("Catch requires an event name."));
c906108c
SS
9572}
9573
80f8a6eb 9574/* Delete breakpoints by address or line. */
c906108c
SS
9575
9576static void
fba45db2 9577clear_command (char *arg, int from_tty)
c906108c 9578{
d6e956e5
VP
9579 struct breakpoint *b;
9580 VEC(breakpoint_p) *found = 0;
9581 int ix;
c906108c
SS
9582 int default_match;
9583 struct symtabs_and_lines sals;
9584 struct symtab_and_line sal;
c906108c
SS
9585 int i;
9586
9587 if (arg)
9588 {
9589 sals = decode_line_spec (arg, 1);
9590 default_match = 0;
9591 }
9592 else
9593 {
c5aa993b 9594 sals.sals = (struct symtab_and_line *)
c906108c 9595 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 9596 make_cleanup (xfree, sals.sals);
4a64f543 9597 init_sal (&sal); /* Initialize to zeroes. */
c906108c
SS
9598 sal.line = default_breakpoint_line;
9599 sal.symtab = default_breakpoint_symtab;
9600 sal.pc = default_breakpoint_address;
6c95b8df 9601 sal.pspace = default_breakpoint_pspace;
c906108c 9602 if (sal.symtab == 0)
8a3fe4f8 9603 error (_("No source file specified."));
c906108c
SS
9604
9605 sals.sals[0] = sal;
9606 sals.nelts = 1;
9607
9608 default_match = 1;
9609 }
9610
4a64f543
MS
9611 /* We don't call resolve_sal_pc here. That's not as bad as it
9612 seems, because all existing breakpoints typically have both
9613 file/line and pc set. So, if clear is given file/line, we can
9614 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
9615
9616 We only support clearing given the address explicitly
9617 present in breakpoint table. Say, we've set breakpoint
4a64f543 9618 at file:line. There were several PC values for that file:line,
ed0616c6 9619 due to optimization, all in one block.
4a64f543
MS
9620
9621 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
9622 PC corresponding to the same file:line, the breakpoint won't
9623 be cleared. We probably can still clear the breakpoint, but
9624 since the other PC value is never presented to user, user
9625 can only find it by guessing, and it does not seem important
9626 to support that. */
9627
4a64f543
MS
9628 /* For each line spec given, delete bps which correspond to it. Do
9629 it in two passes, solely to preserve the current behavior that
9630 from_tty is forced true if we delete more than one
9631 breakpoint. */
c906108c 9632
80f8a6eb 9633 found = NULL;
c906108c
SS
9634 for (i = 0; i < sals.nelts; i++)
9635 {
9636 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
9637 If line given (pc == 0), clear all bpts on specified line.
9638 If defaulting, clear all bpts on default line
c906108c 9639 or at default pc.
c5aa993b
JM
9640
9641 defaulting sal.pc != 0 tests to do
9642
9643 0 1 pc
9644 1 1 pc _and_ line
9645 0 0 line
9646 1 0 <can't happen> */
c906108c
SS
9647
9648 sal = sals.sals[i];
c906108c 9649
4a64f543 9650 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 9651 ALL_BREAKPOINTS (b)
c5aa993b 9652 {
0d381245 9653 int match = 0;
4a64f543 9654 /* Are we going to delete b? */
cc60f2e3 9655 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
9656 {
9657 struct bp_location *loc = b->loc;
9658 for (; loc; loc = loc->next)
9659 {
6c95b8df
PA
9660 int pc_match = sal.pc
9661 && (loc->pspace == sal.pspace)
0d381245
VP
9662 && (loc->address == sal.pc)
9663 && (!section_is_overlay (loc->section)
9664 || loc->section == sal.section);
9665 int line_match = ((default_match || (0 == sal.pc))
9666 && b->source_file != NULL
9667 && sal.symtab != NULL
6c95b8df 9668 && sal.pspace == loc->pspace
3e43a32a
MS
9669 && strcmp (b->source_file,
9670 sal.symtab->filename) == 0
0d381245
VP
9671 && b->line_number == sal.line);
9672 if (pc_match || line_match)
9673 {
9674 match = 1;
9675 break;
9676 }
9677 }
9678 }
9679
9680 if (match)
d6e956e5 9681 VEC_safe_push(breakpoint_p, found, b);
c906108c 9682 }
80f8a6eb
MS
9683 }
9684 /* Now go thru the 'found' chain and delete them. */
d6e956e5 9685 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
9686 {
9687 if (arg)
8a3fe4f8 9688 error (_("No breakpoint at %s."), arg);
80f8a6eb 9689 else
8a3fe4f8 9690 error (_("No breakpoint at this line."));
80f8a6eb 9691 }
c906108c 9692
d6e956e5 9693 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 9694 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 9695 if (from_tty)
a3f17187 9696 {
d6e956e5 9697 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
9698 printf_unfiltered (_("Deleted breakpoint "));
9699 else
9700 printf_unfiltered (_("Deleted breakpoints "));
9701 }
80f8a6eb 9702 breakpoints_changed ();
d6e956e5
VP
9703
9704 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 9705 {
c5aa993b 9706 if (from_tty)
d6e956e5
VP
9707 printf_unfiltered ("%d ", b->number);
9708 delete_breakpoint (b);
c906108c 9709 }
80f8a6eb
MS
9710 if (from_tty)
9711 putchar_unfiltered ('\n');
c906108c
SS
9712}
9713\f
9714/* Delete breakpoint in BS if they are `delete' breakpoints and
9715 all breakpoints that are marked for deletion, whether hit or not.
9716 This is called after any breakpoint is hit, or after errors. */
9717
9718void
fba45db2 9719breakpoint_auto_delete (bpstat bs)
c906108c 9720{
35df4500 9721 struct breakpoint *b, *b_tmp;
c906108c
SS
9722
9723 for (; bs; bs = bs->next)
f431efe5
PA
9724 if (bs->breakpoint_at
9725 && bs->breakpoint_at->disposition == disp_del
c906108c 9726 && bs->stop)
f431efe5 9727 delete_breakpoint (bs->breakpoint_at);
c906108c 9728
35df4500 9729 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 9730 {
b5de0fa7 9731 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
9732 delete_breakpoint (b);
9733 }
c906108c
SS
9734}
9735
4a64f543
MS
9736/* A comparison function for bp_location AP and BP being interfaced to
9737 qsort. Sort elements primarily by their ADDRESS (no matter what
9738 does breakpoint_address_is_meaningful say for its OWNER),
9739 secondarily by ordering first bp_permanent OWNERed elements and
9740 terciarily just ensuring the array is sorted stable way despite
9741 qsort being an instable algorithm. */
876fa593
JK
9742
9743static int
494cfb0f 9744bp_location_compare (const void *ap, const void *bp)
876fa593 9745{
494cfb0f
JK
9746 struct bp_location *a = *(void **) ap;
9747 struct bp_location *b = *(void **) bp;
2bdf28a0 9748 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
9749 int a_perm = a->owner->enable_state == bp_permanent;
9750 int b_perm = b->owner->enable_state == bp_permanent;
9751
9752 if (a->address != b->address)
9753 return (a->address > b->address) - (a->address < b->address);
9754
9755 /* Sort permanent breakpoints first. */
9756 if (a_perm != b_perm)
9757 return (a_perm < b_perm) - (a_perm > b_perm);
9758
4a64f543
MS
9759 /* Make the user-visible order stable across GDB runs. Locations of
9760 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
9761
9762 if (a->owner->number != b->owner->number)
9763 return (a->owner->number > b->owner->number)
9764 - (a->owner->number < b->owner->number);
9765
9766 return (a > b) - (a < b);
9767}
9768
876fa593 9769/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
9770 bp_location_shadow_len_after_address_max according to the current
9771 content of the bp_location array. */
f7545552
TT
9772
9773static void
876fa593 9774bp_location_target_extensions_update (void)
f7545552 9775{
876fa593
JK
9776 struct bp_location *bl, **blp_tmp;
9777
9778 bp_location_placed_address_before_address_max = 0;
9779 bp_location_shadow_len_after_address_max = 0;
9780
9781 ALL_BP_LOCATIONS (bl, blp_tmp)
9782 {
9783 CORE_ADDR start, end, addr;
9784
9785 if (!bp_location_has_shadow (bl))
9786 continue;
9787
9788 start = bl->target_info.placed_address;
9789 end = start + bl->target_info.shadow_len;
9790
9791 gdb_assert (bl->address >= start);
9792 addr = bl->address - start;
9793 if (addr > bp_location_placed_address_before_address_max)
9794 bp_location_placed_address_before_address_max = addr;
9795
9796 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
9797
9798 gdb_assert (bl->address < end);
9799 addr = end - bl->address;
9800 if (addr > bp_location_shadow_len_after_address_max)
9801 bp_location_shadow_len_after_address_max = addr;
9802 }
f7545552
TT
9803}
9804
4cd9bd08 9805/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
9806 into the inferior, only remove already-inserted locations that no
9807 longer should be inserted. Functions that delete a breakpoint or
9808 breakpoints should pass false, so that deleting a breakpoint
9809 doesn't have the side effect of inserting the locations of other
9810 breakpoints that are marked not-inserted, but should_be_inserted
9811 returns true on them.
9812
9813 This behaviour is useful is situations close to tear-down -- e.g.,
9814 after an exec, while the target still has execution, but breakpoint
9815 shadows of the previous executable image should *NOT* be restored
9816 to the new image; or before detaching, where the target still has
9817 execution and wants to delete breakpoints from GDB's lists, and all
9818 breakpoints had already been removed from the inferior. */
9819
0d381245 9820static void
b60e7edf 9821update_global_location_list (int should_insert)
0d381245 9822{
74960c60 9823 struct breakpoint *b;
876fa593 9824 struct bp_location **locp, *loc;
f7545552
TT
9825 struct cleanup *cleanups;
9826
2d134ed3
PA
9827 /* Used in the duplicates detection below. When iterating over all
9828 bp_locations, points to the first bp_location of a given address.
9829 Breakpoints and watchpoints of different types are never
9830 duplicates of each other. Keep one pointer for each type of
9831 breakpoint/watchpoint, so we only need to loop over all locations
9832 once. */
9833 struct bp_location *bp_loc_first; /* breakpoint */
9834 struct bp_location *wp_loc_first; /* hardware watchpoint */
9835 struct bp_location *awp_loc_first; /* access watchpoint */
9836 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 9837
4a64f543
MS
9838 /* Saved former bp_location array which we compare against the newly
9839 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
9840 struct bp_location **old_location, **old_locp;
9841 unsigned old_location_count;
9842
9843 old_location = bp_location;
9844 old_location_count = bp_location_count;
9845 bp_location = NULL;
9846 bp_location_count = 0;
9847 cleanups = make_cleanup (xfree, old_location);
0d381245 9848
74960c60 9849 ALL_BREAKPOINTS (b)
876fa593
JK
9850 for (loc = b->loc; loc; loc = loc->next)
9851 bp_location_count++;
9852
9853 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
9854 locp = bp_location;
9855 ALL_BREAKPOINTS (b)
9856 for (loc = b->loc; loc; loc = loc->next)
9857 *locp++ = loc;
9858 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 9859 bp_location_compare);
876fa593
JK
9860
9861 bp_location_target_extensions_update ();
74960c60 9862
4a64f543
MS
9863 /* Identify bp_location instances that are no longer present in the
9864 new list, and therefore should be freed. Note that it's not
9865 necessary that those locations should be removed from inferior --
9866 if there's another location at the same address (previously
9867 marked as duplicate), we don't need to remove/insert the
9868 location.
876fa593 9869
4a64f543
MS
9870 LOCP is kept in sync with OLD_LOCP, each pointing to the current
9871 and former bp_location array state respectively. */
876fa593
JK
9872
9873 locp = bp_location;
9874 for (old_locp = old_location; old_locp < old_location + old_location_count;
9875 old_locp++)
74960c60 9876 {
876fa593 9877 struct bp_location *old_loc = *old_locp;
c7d46a38 9878 struct bp_location **loc2p;
876fa593 9879
4a64f543
MS
9880 /* Tells if 'old_loc' is found amoung the new locations. If
9881 not, we have to free it. */
c7d46a38 9882 int found_object = 0;
20874c92
VP
9883 /* Tells if the location should remain inserted in the target. */
9884 int keep_in_target = 0;
9885 int removed = 0;
876fa593 9886
4a64f543
MS
9887 /* Skip LOCP entries which will definitely never be needed.
9888 Stop either at or being the one matching OLD_LOC. */
876fa593 9889 while (locp < bp_location + bp_location_count
c7d46a38 9890 && (*locp)->address < old_loc->address)
876fa593 9891 locp++;
c7d46a38
PA
9892
9893 for (loc2p = locp;
9894 (loc2p < bp_location + bp_location_count
9895 && (*loc2p)->address == old_loc->address);
9896 loc2p++)
9897 {
9898 if (*loc2p == old_loc)
9899 {
9900 found_object = 1;
9901 break;
9902 }
9903 }
74960c60 9904
4a64f543
MS
9905 /* If this location is no longer present, and inserted, look if
9906 there's maybe a new location at the same address. If so,
9907 mark that one inserted, and don't remove this one. This is
9908 needed so that we don't have a time window where a breakpoint
9909 at certain location is not inserted. */
74960c60 9910
876fa593 9911 if (old_loc->inserted)
0d381245 9912 {
4a64f543
MS
9913 /* If the location is inserted now, we might have to remove
9914 it. */
74960c60 9915
876fa593 9916 if (found_object && should_be_inserted (old_loc))
74960c60 9917 {
4a64f543
MS
9918 /* The location is still present in the location list,
9919 and still should be inserted. Don't do anything. */
20874c92 9920 keep_in_target = 1;
74960c60
VP
9921 }
9922 else
9923 {
4a64f543
MS
9924 /* The location is either no longer present, or got
9925 disabled. See if there's another location at the
9926 same address, in which case we don't need to remove
9927 this one from the target. */
876fa593 9928
2bdf28a0 9929 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
9930 if (breakpoint_address_is_meaningful (old_loc->owner))
9931 {
876fa593 9932 for (loc2p = locp;
c7d46a38
PA
9933 (loc2p < bp_location + bp_location_count
9934 && (*loc2p)->address == old_loc->address);
876fa593
JK
9935 loc2p++)
9936 {
9937 struct bp_location *loc2 = *loc2p;
9938
2d134ed3 9939 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38
PA
9940 {
9941 /* For the sake of should_be_inserted.
4a64f543
MS
9942 Duplicates check below will fix up this
9943 later. */
c7d46a38 9944 loc2->duplicate = 0;
85d721b8
PA
9945
9946 /* Read watchpoint locations are switched to
9947 access watchpoints, if the former are not
9948 supported, but the latter are. */
9949 if (is_hardware_watchpoint (old_loc->owner))
9950 {
9951 gdb_assert (is_hardware_watchpoint (loc2->owner));
9952 loc2->watchpoint_type = old_loc->watchpoint_type;
9953 }
9954
c7d46a38
PA
9955 if (loc2 != old_loc && should_be_inserted (loc2))
9956 {
9957 loc2->inserted = 1;
9958 loc2->target_info = old_loc->target_info;
9959 keep_in_target = 1;
9960 break;
9961 }
876fa593
JK
9962 }
9963 }
9964 }
74960c60
VP
9965 }
9966
20874c92
VP
9967 if (!keep_in_target)
9968 {
876fa593 9969 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 9970 {
4a64f543
MS
9971 /* This is just about all we can do. We could keep
9972 this location on the global list, and try to
9973 remove it next time, but there's no particular
9974 reason why we will succeed next time.
20874c92 9975
4a64f543
MS
9976 Note that at this point, old_loc->owner is still
9977 valid, as delete_breakpoint frees the breakpoint
9978 only after calling us. */
3e43a32a
MS
9979 printf_filtered (_("warning: Error removing "
9980 "breakpoint %d\n"),
876fa593 9981 old_loc->owner->number);
20874c92
VP
9982 }
9983 removed = 1;
9984 }
0d381245 9985 }
74960c60
VP
9986
9987 if (!found_object)
1c5cfe86 9988 {
db82e815
PA
9989 if (removed && non_stop
9990 && breakpoint_address_is_meaningful (old_loc->owner)
9991 && !is_hardware_watchpoint (old_loc->owner))
20874c92 9992 {
db82e815
PA
9993 /* This location was removed from the target. In
9994 non-stop mode, a race condition is possible where
9995 we've removed a breakpoint, but stop events for that
9996 breakpoint are already queued and will arrive later.
9997 We apply an heuristic to be able to distinguish such
9998 SIGTRAPs from other random SIGTRAPs: we keep this
9999 breakpoint location for a bit, and will retire it
10000 after we see some number of events. The theory here
10001 is that reporting of events should, "on the average",
10002 be fair, so after a while we'll see events from all
10003 threads that have anything of interest, and no longer
10004 need to keep this breakpoint location around. We
10005 don't hold locations forever so to reduce chances of
10006 mistaking a non-breakpoint SIGTRAP for a breakpoint
10007 SIGTRAP.
10008
10009 The heuristic failing can be disastrous on
10010 decr_pc_after_break targets.
10011
10012 On decr_pc_after_break targets, like e.g., x86-linux,
10013 if we fail to recognize a late breakpoint SIGTRAP,
10014 because events_till_retirement has reached 0 too
10015 soon, we'll fail to do the PC adjustment, and report
10016 a random SIGTRAP to the user. When the user resumes
10017 the inferior, it will most likely immediately crash
2dec564e 10018 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10019 corrupted, because of being resumed e.g., in the
10020 middle of a multi-byte instruction, or skipped a
10021 one-byte instruction. This was actually seen happen
10022 on native x86-linux, and should be less rare on
10023 targets that do not support new thread events, like
10024 remote, due to the heuristic depending on
10025 thread_count.
10026
10027 Mistaking a random SIGTRAP for a breakpoint trap
10028 causes similar symptoms (PC adjustment applied when
10029 it shouldn't), but then again, playing with SIGTRAPs
10030 behind the debugger's back is asking for trouble.
10031
10032 Since hardware watchpoint traps are always
10033 distinguishable from other traps, so we don't need to
10034 apply keep hardware watchpoint moribund locations
10035 around. We simply always ignore hardware watchpoint
10036 traps we can no longer explain. */
10037
876fa593
JK
10038 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10039 old_loc->owner = NULL;
20874c92 10040
876fa593 10041 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10042 }
10043 else
f431efe5
PA
10044 {
10045 old_loc->owner = NULL;
10046 decref_bp_location (&old_loc);
10047 }
20874c92 10048 }
74960c60 10049 }
1c5cfe86 10050
2d134ed3
PA
10051 /* Rescan breakpoints at the same address and section, marking the
10052 first one as "first" and any others as "duplicates". This is so
10053 that the bpt instruction is only inserted once. If we have a
10054 permanent breakpoint at the same place as BPT, make that one the
10055 official one, and the rest as duplicates. Permanent breakpoints
10056 are sorted first for the same address.
10057
10058 Do the same for hardware watchpoints, but also considering the
10059 watchpoint's type (regular/access/read) and length. */
876fa593 10060
2d134ed3
PA
10061 bp_loc_first = NULL;
10062 wp_loc_first = NULL;
10063 awp_loc_first = NULL;
10064 rwp_loc_first = NULL;
876fa593 10065 ALL_BP_LOCATIONS (loc, locp)
74960c60 10066 {
4a64f543
MS
10067 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10068 non-NULL. */
876fa593 10069 struct breakpoint *b = loc->owner;
2d134ed3 10070 struct bp_location **loc_first_p;
876fa593
JK
10071
10072 if (b->enable_state == bp_disabled
10073 || b->enable_state == bp_call_disabled
10074 || b->enable_state == bp_startup_disabled
10075 || !loc->enabled
10076 || loc->shlib_disabled
15c3d785 10077 || !breakpoint_address_is_meaningful (b)
d77f58be 10078 || is_tracepoint (b))
876fa593
JK
10079 continue;
10080
10081 /* Permanent breakpoint should always be inserted. */
10082 if (b->enable_state == bp_permanent && ! loc->inserted)
10083 internal_error (__FILE__, __LINE__,
10084 _("allegedly permanent breakpoint is not "
10085 "actually inserted"));
10086
2d134ed3
PA
10087 if (b->type == bp_hardware_watchpoint)
10088 loc_first_p = &wp_loc_first;
10089 else if (b->type == bp_read_watchpoint)
10090 loc_first_p = &rwp_loc_first;
10091 else if (b->type == bp_access_watchpoint)
10092 loc_first_p = &awp_loc_first;
10093 else
10094 loc_first_p = &bp_loc_first;
10095
10096 if (*loc_first_p == NULL
10097 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10098 || !breakpoint_locations_match (loc, *loc_first_p))
876fa593 10099 {
2d134ed3 10100 *loc_first_p = loc;
876fa593
JK
10101 loc->duplicate = 0;
10102 continue;
10103 }
10104
10105 loc->duplicate = 1;
10106
2d134ed3
PA
10107 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10108 && b->enable_state != bp_permanent)
876fa593
JK
10109 internal_error (__FILE__, __LINE__,
10110 _("another breakpoint was inserted on top of "
10111 "a permanent breakpoint"));
0d381245 10112 }
74960c60 10113
50c71eaf 10114 if (breakpoints_always_inserted_mode () && should_insert
c35b1492 10115 && (have_live_inferiors ()
2567c7d9 10116 || (gdbarch_has_global_breakpoints (target_gdbarch))))
74960c60 10117 insert_breakpoint_locations ();
f7545552
TT
10118
10119 do_cleanups (cleanups);
74960c60
VP
10120}
10121
20874c92
VP
10122void
10123breakpoint_retire_moribund (void)
10124{
10125 struct bp_location *loc;
10126 int ix;
10127
10128 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10129 if (--(loc->events_till_retirement) == 0)
10130 {
f431efe5 10131 decref_bp_location (&loc);
20874c92
VP
10132 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10133 --ix;
10134 }
10135}
10136
74960c60 10137static void
b60e7edf 10138update_global_location_list_nothrow (int inserting)
74960c60
VP
10139{
10140 struct gdb_exception e;
cc59ec59 10141
74960c60 10142 TRY_CATCH (e, RETURN_MASK_ERROR)
b60e7edf 10143 update_global_location_list (inserting);
0d381245
VP
10144}
10145
f431efe5
PA
10146/* Clear BKP from a BPS. */
10147
a474d7c2 10148static void
f431efe5 10149bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
a474d7c2
PA
10150{
10151 bpstat bs;
cc59ec59 10152
a474d7c2 10153 for (bs = bps; bs; bs = bs->next)
f431efe5 10154 if (bs->breakpoint_at == bpt)
a474d7c2
PA
10155 {
10156 bs->breakpoint_at = NULL;
10157 bs->old_val = NULL;
10158 /* bs->commands will be freed later. */
10159 }
10160}
10161
10162/* Callback for iterate_over_threads. */
10163static int
f431efe5 10164bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
a474d7c2 10165{
f431efe5 10166 struct breakpoint *bpt = data;
cc59ec59 10167
16c381f0 10168 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
a474d7c2
PA
10169 return 0;
10170}
10171
53a5351d 10172/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 10173 structures. */
c906108c
SS
10174
10175void
fba45db2 10176delete_breakpoint (struct breakpoint *bpt)
c906108c 10177{
52f0bd74 10178 struct breakpoint *b;
c906108c 10179
8a3fe4f8 10180 gdb_assert (bpt != NULL);
c906108c 10181
4a64f543
MS
10182 /* Has this bp already been deleted? This can happen because
10183 multiple lists can hold pointers to bp's. bpstat lists are
10184 especial culprits.
10185
10186 One example of this happening is a watchpoint's scope bp. When
10187 the scope bp triggers, we notice that the watchpoint is out of
10188 scope, and delete it. We also delete its scope bp. But the
10189 scope bp is marked "auto-deleting", and is already on a bpstat.
10190 That bpstat is then checked for auto-deleting bp's, which are
10191 deleted.
10192
10193 A real solution to this problem might involve reference counts in
10194 bp's, and/or giving them pointers back to their referencing
10195 bpstat's, and teaching delete_breakpoint to only free a bp's
10196 storage when no more references were extent. A cheaper bandaid
10197 was chosen. */
c906108c
SS
10198 if (bpt->type == bp_none)
10199 return;
10200
4a64f543
MS
10201 /* At least avoid this stale reference until the reference counting
10202 of breakpoints gets resolved. */
e5a0a904
JK
10203 if (bpt->related_breakpoint != NULL)
10204 {
10205 gdb_assert (bpt->related_breakpoint->related_breakpoint == bpt);
10206 bpt->related_breakpoint->disposition = disp_del_at_next_stop;
10207 bpt->related_breakpoint->related_breakpoint = NULL;
10208 bpt->related_breakpoint = NULL;
10209 }
10210
383f836e 10211 observer_notify_breakpoint_deleted (bpt->number);
c906108c 10212
c906108c
SS
10213 if (breakpoint_chain == bpt)
10214 breakpoint_chain = bpt->next;
10215
c906108c
SS
10216 ALL_BREAKPOINTS (b)
10217 if (b->next == bpt)
c5aa993b
JM
10218 {
10219 b->next = bpt->next;
10220 break;
10221 }
c906108c 10222
9add0f1b 10223 decref_counted_command_line (&bpt->commands);
60e1c644
PA
10224 xfree (bpt->cond_string);
10225 xfree (bpt->cond_exp);
10226 xfree (bpt->addr_string);
10227 xfree (bpt->exp);
10228 xfree (bpt->exp_string);
d63d0675 10229 xfree (bpt->exp_string_reparse);
60e1c644
PA
10230 value_free (bpt->val);
10231 xfree (bpt->source_file);
10232 xfree (bpt->exec_pathname);
a96d9b2e 10233 clean_up_filters (&bpt->syscalls_to_be_caught);
c906108c 10234
f431efe5
PA
10235
10236 /* Be sure no bpstat's are pointing at the breakpoint after it's
10237 been freed. */
10238 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
10239 in all threeds for now. Note that we cannot just remove bpstats
10240 pointing at bpt from the stop_bpstat list entirely, as breakpoint
10241 commands are associated with the bpstat; if we remove it here,
10242 then the later call to bpstat_do_actions (&stop_bpstat); in
10243 event-top.c won't do anything, and temporary breakpoints with
10244 commands won't work. */
10245
10246 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
10247
4a64f543
MS
10248 /* Now that breakpoint is removed from breakpoint list, update the
10249 global location list. This will remove locations that used to
10250 belong to this breakpoint. Do this before freeing the breakpoint
10251 itself, since remove_breakpoint looks at location's owner. It
10252 might be better design to have location completely
10253 self-contained, but it's not the case now. */
b60e7edf 10254 update_global_location_list (0);
74960c60
VP
10255
10256
4a64f543
MS
10257 /* On the chance that someone will soon try again to delete this
10258 same bp, we mark it as deleted before freeing its storage. */
c906108c
SS
10259 bpt->type = bp_none;
10260
b8c9b27d 10261 xfree (bpt);
c906108c
SS
10262}
10263
4d6140d9
AC
10264static void
10265do_delete_breakpoint_cleanup (void *b)
10266{
10267 delete_breakpoint (b);
10268}
10269
10270struct cleanup *
10271make_cleanup_delete_breakpoint (struct breakpoint *b)
10272{
10273 return make_cleanup (do_delete_breakpoint_cleanup, b);
10274}
10275
95a42b64
TT
10276/* A callback for map_breakpoint_numbers that calls
10277 delete_breakpoint. */
10278
10279static void
10280do_delete_breakpoint (struct breakpoint *b, void *ignore)
10281{
10282 delete_breakpoint (b);
10283}
10284
c906108c 10285void
fba45db2 10286delete_command (char *arg, int from_tty)
c906108c 10287{
35df4500 10288 struct breakpoint *b, *b_tmp;
c906108c 10289
ea9365bb
TT
10290 dont_repeat ();
10291
c906108c
SS
10292 if (arg == 0)
10293 {
10294 int breaks_to_delete = 0;
10295
10296 /* Delete all breakpoints if no argument.
4a64f543
MS
10297 Do not delete internal or call-dummy breakpoints, these have
10298 to be deleted with an explicit breakpoint number argument. */
c5aa993b
JM
10299 ALL_BREAKPOINTS (b)
10300 {
059fb39f 10301 if (b->type != bp_call_dummy
aa7d318d 10302 && b->type != bp_std_terminate
059fb39f 10303 && b->type != bp_shlib_event
4efc6507 10304 && b->type != bp_jit_event
059fb39f
PM
10305 && b->type != bp_thread_event
10306 && b->type != bp_overlay_event
0fd8e87f 10307 && b->type != bp_longjmp_master
aa7d318d 10308 && b->type != bp_std_terminate_master
186c406b 10309 && b->type != bp_exception_master
059fb39f 10310 && b->number >= 0)
973d738b
DJ
10311 {
10312 breaks_to_delete = 1;
10313 break;
10314 }
c5aa993b 10315 }
c906108c
SS
10316
10317 /* Ask user only if there are some breakpoints to delete. */
10318 if (!from_tty
e2e0b3e5 10319 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 10320 {
35df4500 10321 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10322 {
059fb39f 10323 if (b->type != bp_call_dummy
aa7d318d 10324 && b->type != bp_std_terminate
059fb39f
PM
10325 && b->type != bp_shlib_event
10326 && b->type != bp_thread_event
4efc6507 10327 && b->type != bp_jit_event
059fb39f 10328 && b->type != bp_overlay_event
0fd8e87f 10329 && b->type != bp_longjmp_master
aa7d318d 10330 && b->type != bp_std_terminate_master
186c406b 10331 && b->type != bp_exception_master
059fb39f 10332 && b->number >= 0)
c5aa993b
JM
10333 delete_breakpoint (b);
10334 }
c906108c
SS
10335 }
10336 }
10337 else
95a42b64 10338 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
c906108c
SS
10339}
10340
0d381245
VP
10341static int
10342all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 10343{
0d381245
VP
10344 for (; loc; loc = loc->next)
10345 if (!loc->shlib_disabled)
10346 return 0;
10347 return 1;
fe3f5fa8
VP
10348}
10349
776592bf
DE
10350/* Subroutine of update_breakpoint_locations to simplify it.
10351 Return non-zero if multiple fns in list LOC have the same name.
10352 Null names are ignored. */
10353
10354static int
10355ambiguous_names_p (struct bp_location *loc)
10356{
10357 struct bp_location *l;
10358 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
10359 (int (*) (const void *,
10360 const void *)) streq,
776592bf
DE
10361 NULL, xcalloc, xfree);
10362
10363 for (l = loc; l != NULL; l = l->next)
10364 {
10365 const char **slot;
10366 const char *name = l->function_name;
10367
10368 /* Allow for some names to be NULL, ignore them. */
10369 if (name == NULL)
10370 continue;
10371
10372 slot = (const char **) htab_find_slot (htab, (const void *) name,
10373 INSERT);
4a64f543
MS
10374 /* NOTE: We can assume slot != NULL here because xcalloc never
10375 returns NULL. */
776592bf
DE
10376 if (*slot != NULL)
10377 {
10378 htab_delete (htab);
10379 return 1;
10380 }
10381 *slot = name;
10382 }
10383
10384 htab_delete (htab);
10385 return 0;
10386}
10387
0fb4aa4b
PA
10388/* When symbols change, it probably means the sources changed as well,
10389 and it might mean the static tracepoint markers are no longer at
10390 the same address or line numbers they used to be at last we
10391 checked. Losing your static tracepoints whenever you rebuild is
10392 undesirable. This function tries to resync/rematch gdb static
10393 tracepoints with the markers on the target, for static tracepoints
10394 that have not been set by marker id. Static tracepoint that have
10395 been set by marker id are reset by marker id in breakpoint_re_set.
10396 The heuristic is:
10397
10398 1) For a tracepoint set at a specific address, look for a marker at
10399 the old PC. If one is found there, assume to be the same marker.
10400 If the name / string id of the marker found is different from the
10401 previous known name, assume that means the user renamed the marker
10402 in the sources, and output a warning.
10403
10404 2) For a tracepoint set at a given line number, look for a marker
10405 at the new address of the old line number. If one is found there,
10406 assume to be the same marker. If the name / string id of the
10407 marker found is different from the previous known name, assume that
10408 means the user renamed the marker in the sources, and output a
10409 warning.
10410
10411 3) If a marker is no longer found at the same address or line, it
10412 may mean the marker no longer exists. But it may also just mean
10413 the code changed a bit. Maybe the user added a few lines of code
10414 that made the marker move up or down (in line number terms). Ask
10415 the target for info about the marker with the string id as we knew
10416 it. If found, update line number and address in the matching
10417 static tracepoint. This will get confused if there's more than one
10418 marker with the same ID (possible in UST, although unadvised
10419 precisely because it confuses tools). */
10420
10421static struct symtab_and_line
10422update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
10423{
10424 struct static_tracepoint_marker marker;
10425 CORE_ADDR pc;
10426 int i;
10427
10428 pc = sal.pc;
10429 if (sal.line)
10430 find_line_pc (sal.symtab, sal.line, &pc);
10431
10432 if (target_static_tracepoint_marker_at (pc, &marker))
10433 {
10434 if (strcmp (b->static_trace_marker_id, marker.str_id) != 0)
10435 warning (_("static tracepoint %d changed probed marker from %s to %s"),
10436 b->number,
10437 b->static_trace_marker_id, marker.str_id);
10438
10439 xfree (b->static_trace_marker_id);
10440 b->static_trace_marker_id = xstrdup (marker.str_id);
10441 release_static_tracepoint_marker (&marker);
10442
10443 return sal;
10444 }
10445
10446 /* Old marker wasn't found on target at lineno. Try looking it up
10447 by string ID. */
10448 if (!sal.explicit_pc
10449 && sal.line != 0
10450 && sal.symtab != NULL
10451 && b->static_trace_marker_id != NULL)
10452 {
10453 VEC(static_tracepoint_marker_p) *markers;
10454
10455 markers
10456 = target_static_tracepoint_markers_by_strid (b->static_trace_marker_id);
10457
10458 if (!VEC_empty(static_tracepoint_marker_p, markers))
10459 {
10460 struct symtab_and_line sal;
10461 struct symbol *sym;
10462 struct static_tracepoint_marker *marker;
10463
10464 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
10465
10466 xfree (b->static_trace_marker_id);
10467 b->static_trace_marker_id = xstrdup (marker->str_id);
10468
10469 warning (_("marker for static tracepoint %d (%s) not "
10470 "found at previous line number"),
10471 b->number, b->static_trace_marker_id);
10472
10473 init_sal (&sal);
10474
10475 sal.pc = marker->address;
10476
10477 sal = find_pc_line (marker->address, 0);
10478 sym = find_pc_sect_function (marker->address, NULL);
10479 ui_out_text (uiout, "Now in ");
10480 if (sym)
10481 {
10482 ui_out_field_string (uiout, "func",
10483 SYMBOL_PRINT_NAME (sym));
10484 ui_out_text (uiout, " at ");
10485 }
10486 ui_out_field_string (uiout, "file", sal.symtab->filename);
10487 ui_out_text (uiout, ":");
10488
10489 if (ui_out_is_mi_like_p (uiout))
10490 {
10491 char *fullname = symtab_to_fullname (sal.symtab);
10492
10493 if (fullname)
10494 ui_out_field_string (uiout, "fullname", fullname);
10495 }
10496
10497 ui_out_field_int (uiout, "line", sal.line);
10498 ui_out_text (uiout, "\n");
10499
10500 b->line_number = sal.line;
10501
10502 xfree (b->source_file);
10503 if (sym)
10504 b->source_file = xstrdup (sal.symtab->filename);
10505 else
10506 b->source_file = NULL;
10507
10508 xfree (b->addr_string);
10509 b->addr_string = xstrprintf ("%s:%d",
10510 sal.symtab->filename, b->line_number);
10511
10512 /* Might be nice to check if function changed, and warn if
10513 so. */
10514
10515 release_static_tracepoint_marker (marker);
10516 }
10517 }
10518 return sal;
10519}
10520
fe3f5fa8 10521static void
0d381245
VP
10522update_breakpoint_locations (struct breakpoint *b,
10523 struct symtabs_and_lines sals)
fe3f5fa8
VP
10524{
10525 int i;
10526 char *s;
0d381245
VP
10527 struct bp_location *existing_locations = b->loc;
10528
4a64f543
MS
10529 /* If there's no new locations, and all existing locations are
10530 pending, don't do anything. This optimizes the common case where
10531 all locations are in the same shared library, that was unloaded.
10532 We'd like to retain the location, so that when the library is
10533 loaded again, we don't loose the enabled/disabled status of the
10534 individual locations. */
0d381245 10535 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
10536 return;
10537
fe3f5fa8
VP
10538 b->loc = NULL;
10539
0d381245 10540 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 10541 {
0d381245 10542 struct bp_location *new_loc =
39d61571 10543 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 10544
0d381245
VP
10545 /* Reparse conditions, they might contain references to the
10546 old symtab. */
10547 if (b->cond_string != NULL)
10548 {
10549 struct gdb_exception e;
fe3f5fa8 10550
0d381245
VP
10551 s = b->cond_string;
10552 TRY_CATCH (e, RETURN_MASK_ERROR)
10553 {
10554 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
10555 0);
10556 }
10557 if (e.reason < 0)
10558 {
3e43a32a
MS
10559 warning (_("failed to reevaluate condition "
10560 "for breakpoint %d: %s"),
0d381245
VP
10561 b->number, e.message);
10562 new_loc->enabled = 0;
10563 }
10564 }
fe3f5fa8 10565
0d381245
VP
10566 if (b->source_file != NULL)
10567 xfree (b->source_file);
10568 if (sals.sals[i].symtab == NULL)
10569 b->source_file = NULL;
10570 else
1b36a34b 10571 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 10572
0d381245
VP
10573 if (b->line_number == 0)
10574 b->line_number = sals.sals[i].line;
10575 }
fe3f5fa8 10576
514f746b
AR
10577 /* Update locations of permanent breakpoints. */
10578 if (b->enable_state == bp_permanent)
10579 make_breakpoint_permanent (b);
10580
4a64f543
MS
10581 /* If possible, carry over 'disable' status from existing
10582 breakpoints. */
0d381245
VP
10583 {
10584 struct bp_location *e = existing_locations;
776592bf
DE
10585 /* If there are multiple breakpoints with the same function name,
10586 e.g. for inline functions, comparing function names won't work.
10587 Instead compare pc addresses; this is just a heuristic as things
10588 may have moved, but in practice it gives the correct answer
10589 often enough until a better solution is found. */
10590 int have_ambiguous_names = ambiguous_names_p (b->loc);
10591
0d381245
VP
10592 for (; e; e = e->next)
10593 {
10594 if (!e->enabled && e->function_name)
10595 {
10596 struct bp_location *l = b->loc;
776592bf
DE
10597 if (have_ambiguous_names)
10598 {
10599 for (; l; l = l->next)
6c95b8df
PA
10600 if (breakpoint_address_match (e->pspace->aspace, e->address,
10601 l->pspace->aspace, l->address))
776592bf
DE
10602 {
10603 l->enabled = 0;
10604 break;
10605 }
10606 }
10607 else
10608 {
10609 for (; l; l = l->next)
10610 if (l->function_name
10611 && strcmp (e->function_name, l->function_name) == 0)
10612 {
10613 l->enabled = 0;
10614 break;
10615 }
10616 }
0d381245
VP
10617 }
10618 }
10619 }
fe3f5fa8 10620
b60e7edf 10621 update_global_location_list (1);
fe3f5fa8
VP
10622}
10623
c906108c
SS
10624/* Reset a breakpoint given it's struct breakpoint * BINT.
10625 The value we return ends up being the return value from catch_errors.
10626 Unused in this case. */
10627
10628static int
4efb68b1 10629breakpoint_re_set_one (void *bint)
c906108c 10630{
4a64f543 10631 /* Get past catch_errs. */
53a5351d 10632 struct breakpoint *b = (struct breakpoint *) bint;
fe3f5fa8
VP
10633 int not_found = 0;
10634 int *not_found_ptr = &not_found;
6c95b8df
PA
10635 struct symtabs_and_lines sals = {0};
10636 struct symtabs_and_lines expanded = {0};
c906108c 10637 char *s;
fe3f5fa8 10638 struct gdb_exception e;
6c95b8df 10639 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
0fb4aa4b 10640 int marker_spec = 0;
c906108c
SS
10641
10642 switch (b->type)
10643 {
10644 case bp_none:
8a3fe4f8 10645 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
53a5351d 10646 b->number);
c906108c
SS
10647 return 0;
10648 case bp_breakpoint:
10649 case bp_hardware_breakpoint:
1042e4c0 10650 case bp_tracepoint:
7a697b8d 10651 case bp_fast_tracepoint:
0fb4aa4b 10652 case bp_static_tracepoint:
8bea4e01
UW
10653 /* Do not attempt to re-set breakpoints disabled during startup. */
10654 if (b->enable_state == bp_startup_disabled)
10655 return 0;
10656
c906108c
SS
10657 if (b->addr_string == NULL)
10658 {
4a64f543 10659 /* Anything without a string can't be re-set. */
c906108c
SS
10660 delete_breakpoint (b);
10661 return 0;
10662 }
c906108c 10663
c906108c
SS
10664 input_radix = b->input_radix;
10665 s = b->addr_string;
6c95b8df
PA
10666
10667 save_current_space_and_thread ();
10668 switch_to_program_space_and_thread (b->pspace);
10669
0fb4aa4b
PA
10670 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
10671
385d04dc 10672 set_language (b->language);
fe3f5fa8 10673 TRY_CATCH (e, RETURN_MASK_ERROR)
c906108c 10674 {
0fb4aa4b
PA
10675 if (marker_spec)
10676 {
10677 sals = decode_static_tracepoint_spec (&s);
10678 if (sals.nelts > b->static_trace_marker_id_idx)
10679 {
10680 sals.sals[0] = sals.sals[b->static_trace_marker_id_idx];
10681 sals.nelts = 1;
10682 }
10683 else
10684 error (_("marker %s not found"), b->static_trace_marker_id);
10685 }
10686 else
3e43a32a
MS
10687 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0,
10688 (char ***) NULL, not_found_ptr);
fe3f5fa8
VP
10689 }
10690 if (e.reason < 0)
10691 {
10692 int not_found_and_ok = 0;
4a64f543
MS
10693 /* For pending breakpoints, it's expected that parsing will
10694 fail until the right shared library is loaded. User has
10695 already told to create pending breakpoints and don't need
10696 extra messages. If breakpoint is in bp_shlib_disabled
10697 state, then user already saw the message about that
10698 breakpoint being disabled, and don't want to see more
10699 errors. */
0d381245
VP
10700 if (not_found
10701 && (b->condition_not_parsed
10702 || (b->loc && b->loc->shlib_disabled)
10703 || b->enable_state == bp_disabled))
fe3f5fa8
VP
10704 not_found_and_ok = 1;
10705
10706 if (!not_found_and_ok)
c906108c 10707 {
fe3f5fa8
VP
10708 /* We surely don't want to warn about the same breakpoint
10709 10 times. One solution, implemented here, is disable
10710 the breakpoint on error. Another solution would be to
10711 have separate 'warning emitted' flag. Since this
10712 happens only when a binary has changed, I don't know
10713 which approach is better. */
10714 b->enable_state = bp_disabled;
10715 throw_exception (e);
c906108c 10716 }
fe3f5fa8 10717 }
c906108c 10718
6c95b8df 10719 if (!not_found)
fe3f5fa8 10720 {
6c95b8df
PA
10721 gdb_assert (sals.nelts == 1);
10722
10723 resolve_sal_pc (&sals.sals[0]);
10724 if (b->condition_not_parsed && s && s[0])
10725 {
10726 char *cond_string = 0;
10727 int thread = -1;
10728 int task = 0;
10729
10730 find_condition_and_thread (s, sals.sals[0].pc,
10731 &cond_string, &thread, &task);
10732 if (cond_string)
10733 b->cond_string = cond_string;
10734 b->thread = thread;
10735 b->task = task;
10736 b->condition_not_parsed = 0;
10737 }
10738
0fb4aa4b
PA
10739 if (b->type == bp_static_tracepoint && !marker_spec)
10740 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
10741
6c95b8df 10742 expanded = expand_line_sal_maybe (sals.sals[0]);
fe3f5fa8 10743 }
6c95b8df
PA
10744
10745 make_cleanup (xfree, sals.sals);
ed0616c6 10746 update_breakpoint_locations (b, expanded);
c906108c
SS
10747 break;
10748
10749 case bp_watchpoint:
10750 case bp_hardware_watchpoint:
10751 case bp_read_watchpoint:
10752 case bp_access_watchpoint:
4a64f543
MS
10753 /* Watchpoint can be either on expression using entirely global
10754 variables, or it can be on local variables.
10755
10756 Watchpoints of the first kind are never auto-deleted, and
10757 even persist across program restarts. Since they can use
10758 variables from shared libraries, we need to reparse
10759 expression as libraries are loaded and unloaded.
10760
10761 Watchpoints on local variables can also change meaning as
10762 result of solib event. For example, if a watchpoint uses
10763 both a local and a global variables in expression, it's a
10764 local watchpoint, but unloading of a shared library will make
10765 the expression invalid. This is not a very common use case,
10766 but we still re-evaluate expression, to avoid surprises to
10767 the user.
0b3de036
VP
10768
10769 Note that for local watchpoints, we re-evaluate it only if
10770 watchpoints frame id is still valid. If it's not, it means
4a64f543
MS
10771 the watchpoint is out of scope and will be deleted soon. In
10772 fact, I'm not sure we'll ever be called in this case.
0b3de036
VP
10773
10774 If a local watchpoint's frame id is still valid, then
4a64f543 10775 b->exp_valid_block is likewise valid, and we can safely use it.
0b3de036
VP
10776
10777 Don't do anything about disabled watchpoints, since they will
10778 be reevaluated again when enabled. */
a5606eee 10779 update_watchpoint (b, 1 /* reparse */);
c906108c 10780 break;
c5aa993b
JM
10781 /* We needn't really do anything to reset these, since the mask
10782 that requests them is unaffected by e.g., new libraries being
4a64f543 10783 loaded. */
ce78b96d 10784 case bp_catchpoint:
c906108c 10785 break;
c5aa993b 10786
c906108c 10787 default:
a3f17187 10788 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
c906108c 10789 /* fall through */
0fd8e87f
UW
10790 /* Delete overlay event and longjmp master breakpoints; they will be
10791 reset later by breakpoint_re_set. */
1900040c 10792 case bp_overlay_event:
0fd8e87f 10793 case bp_longjmp_master:
aa7d318d 10794 case bp_std_terminate_master:
186c406b 10795 case bp_exception_master:
c906108c
SS
10796 delete_breakpoint (b);
10797 break;
10798
c5aa993b
JM
10799 /* This breakpoint is special, it's set up when the inferior
10800 starts and we really don't want to touch it. */
c906108c
SS
10801 case bp_shlib_event:
10802
c4093a6a
JM
10803 /* Like bp_shlib_event, this breakpoint type is special.
10804 Once it is set up, we do not want to touch it. */
10805 case bp_thread_event:
10806
4a64f543
MS
10807 /* Keep temporary breakpoints, which can be encountered when we
10808 step over a dlopen call and SOLIB_ADD is resetting the
10809 breakpoints. Otherwise these should have been blown away via
10810 the cleanup chain or by breakpoint_init_inferior when we
10811 rerun the executable. */
c906108c
SS
10812 case bp_until:
10813 case bp_finish:
10814 case bp_watchpoint_scope:
10815 case bp_call_dummy:
aa7d318d 10816 case bp_std_terminate:
c906108c 10817 case bp_step_resume:
611c83ae
PA
10818 case bp_longjmp:
10819 case bp_longjmp_resume:
186c406b
TT
10820 case bp_exception:
10821 case bp_exception_resume:
4efc6507 10822 case bp_jit_event:
c906108c
SS
10823 break;
10824 }
10825
6c95b8df 10826 do_cleanups (cleanups);
c906108c
SS
10827 return 0;
10828}
10829
69de3c6a 10830/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 10831void
69de3c6a 10832breakpoint_re_set (void)
c906108c 10833{
35df4500 10834 struct breakpoint *b, *b_tmp;
c906108c
SS
10835 enum language save_language;
10836 int save_input_radix;
6c95b8df 10837 struct cleanup *old_chain;
c5aa993b 10838
c906108c
SS
10839 save_language = current_language->la_language;
10840 save_input_radix = input_radix;
6c95b8df
PA
10841 old_chain = save_current_program_space ();
10842
35df4500 10843 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10844 {
4a64f543 10845 /* Format possible error msg. */
fe3f5fa8 10846 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
10847 b->number);
10848 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 10849 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 10850 do_cleanups (cleanups);
c5aa993b 10851 }
c906108c
SS
10852 set_language (save_language);
10853 input_radix = save_input_radix;
e62c965a 10854
0756c555 10855 jit_breakpoint_re_set ();
4efc6507 10856
6c95b8df
PA
10857 do_cleanups (old_chain);
10858
af02033e
PP
10859 create_overlay_event_breakpoint ();
10860 create_longjmp_master_breakpoint ();
10861 create_std_terminate_master_breakpoint ();
186c406b 10862 create_exception_master_breakpoint ();
c906108c
SS
10863}
10864\f
c906108c
SS
10865/* Reset the thread number of this breakpoint:
10866
10867 - If the breakpoint is for all threads, leave it as-is.
4a64f543 10868 - Else, reset it to the current thread for inferior_ptid. */
c906108c 10869void
fba45db2 10870breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
10871{
10872 if (b->thread != -1)
10873 {
39f77062
KB
10874 if (in_thread_list (inferior_ptid))
10875 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
10876
10877 /* We're being called after following a fork. The new fork is
10878 selected as current, and unless this was a vfork will have a
10879 different program space from the original thread. Reset that
10880 as well. */
10881 b->loc->pspace = current_program_space;
c906108c
SS
10882 }
10883}
10884
03ac34d5
MS
10885/* Set ignore-count of breakpoint number BPTNUM to COUNT.
10886 If from_tty is nonzero, it prints a message to that effect,
10887 which ends with a period (no newline). */
10888
c906108c 10889void
fba45db2 10890set_ignore_count (int bptnum, int count, int from_tty)
c906108c 10891{
52f0bd74 10892 struct breakpoint *b;
c906108c
SS
10893
10894 if (count < 0)
10895 count = 0;
10896
10897 ALL_BREAKPOINTS (b)
10898 if (b->number == bptnum)
c5aa993b 10899 {
d77f58be
SS
10900 if (is_tracepoint (b))
10901 {
10902 if (from_tty && count != 0)
10903 printf_filtered (_("Ignore count ignored for tracepoint %d."),
10904 bptnum);
10905 return;
10906 }
10907
c5aa993b 10908 b->ignore_count = count;
221ea385
KS
10909 if (from_tty)
10910 {
10911 if (count == 0)
3e43a32a
MS
10912 printf_filtered (_("Will stop next time "
10913 "breakpoint %d is reached."),
221ea385
KS
10914 bptnum);
10915 else if (count == 1)
a3f17187 10916 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
10917 bptnum);
10918 else
3e43a32a
MS
10919 printf_filtered (_("Will ignore next %d "
10920 "crossings of breakpoint %d."),
221ea385
KS
10921 count, bptnum);
10922 }
c5aa993b 10923 breakpoints_changed ();
383f836e 10924 observer_notify_breakpoint_modified (b->number);
c5aa993b
JM
10925 return;
10926 }
c906108c 10927
8a3fe4f8 10928 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
10929}
10930
c906108c
SS
10931/* Command to set ignore-count of breakpoint N to COUNT. */
10932
10933static void
fba45db2 10934ignore_command (char *args, int from_tty)
c906108c
SS
10935{
10936 char *p = args;
52f0bd74 10937 int num;
c906108c
SS
10938
10939 if (p == 0)
e2e0b3e5 10940 error_no_arg (_("a breakpoint number"));
c5aa993b 10941
c906108c 10942 num = get_number (&p);
5c44784c 10943 if (num == 0)
8a3fe4f8 10944 error (_("bad breakpoint number: '%s'"), args);
c906108c 10945 if (*p == 0)
8a3fe4f8 10946 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
10947
10948 set_ignore_count (num,
10949 longest_to_int (value_as_long (parse_and_eval (p))),
10950 from_tty);
221ea385
KS
10951 if (from_tty)
10952 printf_filtered ("\n");
c906108c
SS
10953}
10954\f
10955/* Call FUNCTION on each of the breakpoints
10956 whose numbers are given in ARGS. */
10957
10958static void
95a42b64
TT
10959map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
10960 void *),
10961 void *data)
c906108c 10962{
52f0bd74 10963 char *p = args;
c906108c 10964 char *p1;
52f0bd74
AC
10965 int num;
10966 struct breakpoint *b, *tmp;
11cf8741 10967 int match;
c906108c
SS
10968
10969 if (p == 0)
e2e0b3e5 10970 error_no_arg (_("one or more breakpoint numbers"));
c906108c
SS
10971
10972 while (*p)
10973 {
11cf8741 10974 match = 0;
c906108c 10975 p1 = p;
c5aa993b 10976
5c44784c
JM
10977 num = get_number_or_range (&p1);
10978 if (num == 0)
c5aa993b 10979 {
8a3fe4f8 10980 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
10981 }
10982 else
10983 {
10984 ALL_BREAKPOINTS_SAFE (b, tmp)
10985 if (b->number == num)
10986 {
10987 struct breakpoint *related_breakpoint = b->related_breakpoint;
11cf8741 10988 match = 1;
95a42b64 10989 function (b, data);
5c44784c 10990 if (related_breakpoint)
95a42b64 10991 function (related_breakpoint, data);
11cf8741 10992 break;
5c44784c 10993 }
11cf8741 10994 if (match == 0)
a3f17187 10995 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 10996 }
c906108c
SS
10997 p = p1;
10998 }
10999}
11000
0d381245
VP
11001static struct bp_location *
11002find_location_by_number (char *number)
11003{
11004 char *dot = strchr (number, '.');
11005 char *p1;
11006 int bp_num;
11007 int loc_num;
11008 struct breakpoint *b;
11009 struct bp_location *loc;
11010
11011 *dot = '\0';
11012
11013 p1 = number;
11014 bp_num = get_number_or_range (&p1);
11015 if (bp_num == 0)
11016 error (_("Bad breakpoint number '%s'"), number);
11017
11018 ALL_BREAKPOINTS (b)
11019 if (b->number == bp_num)
11020 {
11021 break;
11022 }
11023
11024 if (!b || b->number != bp_num)
11025 error (_("Bad breakpoint number '%s'"), number);
11026
11027 p1 = dot+1;
11028 loc_num = get_number_or_range (&p1);
11029 if (loc_num == 0)
11030 error (_("Bad breakpoint location number '%s'"), number);
11031
11032 --loc_num;
11033 loc = b->loc;
11034 for (;loc_num && loc; --loc_num, loc = loc->next)
11035 ;
11036 if (!loc)
11037 error (_("Bad breakpoint location number '%s'"), dot+1);
11038
11039 return loc;
11040}
11041
11042
1900040c
MS
11043/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11044 If from_tty is nonzero, it prints a message to that effect,
11045 which ends with a period (no newline). */
11046
c906108c 11047void
fba45db2 11048disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
11049{
11050 /* Never disable a watchpoint scope breakpoint; we want to
11051 hit them when we leave scope so we can delete both the
11052 watchpoint and its scope breakpoint at that time. */
11053 if (bpt->type == bp_watchpoint_scope)
11054 return;
11055
c2c6d25f 11056 /* You can't disable permanent breakpoints. */
b5de0fa7 11057 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
11058 return;
11059
b5de0fa7 11060 bpt->enable_state = bp_disabled;
c906108c 11061
b60e7edf 11062 update_global_location_list (0);
c906108c 11063
383f836e 11064 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
11065}
11066
95a42b64
TT
11067/* A callback for map_breakpoint_numbers that calls
11068 disable_breakpoint. */
11069
11070static void
11071do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
11072{
11073 disable_breakpoint (b);
11074}
11075
c906108c 11076static void
fba45db2 11077disable_command (char *args, int from_tty)
c906108c 11078{
52f0bd74 11079 struct breakpoint *bpt;
cc59ec59 11080
c906108c
SS
11081 if (args == 0)
11082 ALL_BREAKPOINTS (bpt)
11083 switch (bpt->type)
c5aa993b
JM
11084 {
11085 case bp_none:
8a3fe4f8 11086 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
53a5351d 11087 bpt->number);
c5aa993b
JM
11088 continue;
11089 case bp_breakpoint:
1042e4c0 11090 case bp_tracepoint:
7a697b8d 11091 case bp_fast_tracepoint:
0fb4aa4b 11092 case bp_static_tracepoint:
ce78b96d 11093 case bp_catchpoint:
c5aa993b
JM
11094 case bp_hardware_breakpoint:
11095 case bp_watchpoint:
11096 case bp_hardware_watchpoint:
11097 case bp_read_watchpoint:
11098 case bp_access_watchpoint:
11099 disable_breakpoint (bpt);
11100 default:
11101 continue;
11102 }
0d381245
VP
11103 else if (strchr (args, '.'))
11104 {
11105 struct bp_location *loc = find_location_by_number (args);
11106 if (loc)
11107 loc->enabled = 0;
b60e7edf 11108 update_global_location_list (0);
0d381245 11109 }
c906108c 11110 else
95a42b64 11111 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
11112}
11113
11114static void
fba45db2 11115do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 11116{
afe38095 11117 int target_resources_ok;
c906108c
SS
11118
11119 if (bpt->type == bp_hardware_breakpoint)
11120 {
11121 int i;
c5aa993b 11122 i = hw_breakpoint_used_count ();
53a5351d 11123 target_resources_ok =
d92524f1 11124 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 11125 i + 1, 0);
c906108c 11126 if (target_resources_ok == 0)
8a3fe4f8 11127 error (_("No hardware breakpoint support in the target."));
c906108c 11128 else if (target_resources_ok < 0)
8a3fe4f8 11129 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
11130 }
11131
cc60f2e3 11132 if (is_watchpoint (bpt))
c906108c 11133 {
dde02812
ES
11134 struct gdb_exception e;
11135
11136 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 11137 {
dde02812 11138 update_watchpoint (bpt, 1 /* reparse */);
c906108c 11139 }
dde02812 11140 if (e.reason < 0)
c5aa993b 11141 {
dde02812
ES
11142 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
11143 bpt->number);
11144 return;
c5aa993b 11145 }
c906108c 11146 }
0101ce28 11147
b4c291bb
KH
11148 if (bpt->enable_state != bp_permanent)
11149 bpt->enable_state = bp_enabled;
11150 bpt->disposition = disposition;
b60e7edf 11151 update_global_location_list (1);
b4c291bb
KH
11152 breakpoints_changed ();
11153
383f836e 11154 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
11155}
11156
fe3f5fa8 11157
c906108c 11158void
fba45db2 11159enable_breakpoint (struct breakpoint *bpt)
c906108c
SS
11160{
11161 do_enable_breakpoint (bpt, bpt->disposition);
11162}
11163
95a42b64
TT
11164/* A callback for map_breakpoint_numbers that calls
11165 enable_breakpoint. */
11166
11167static void
11168do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
11169{
11170 enable_breakpoint (b);
11171}
11172
c906108c
SS
11173/* The enable command enables the specified breakpoints (or all defined
11174 breakpoints) so they once again become (or continue to be) effective
1272ad14 11175 in stopping the inferior. */
c906108c 11176
c906108c 11177static void
fba45db2 11178enable_command (char *args, int from_tty)
c906108c 11179{
52f0bd74 11180 struct breakpoint *bpt;
cc59ec59 11181
c906108c
SS
11182 if (args == 0)
11183 ALL_BREAKPOINTS (bpt)
11184 switch (bpt->type)
c5aa993b
JM
11185 {
11186 case bp_none:
8a3fe4f8 11187 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
53a5351d 11188 bpt->number);
c5aa993b
JM
11189 continue;
11190 case bp_breakpoint:
1042e4c0 11191 case bp_tracepoint:
7a697b8d 11192 case bp_fast_tracepoint:
0fb4aa4b 11193 case bp_static_tracepoint:
ce78b96d 11194 case bp_catchpoint:
c5aa993b
JM
11195 case bp_hardware_breakpoint:
11196 case bp_watchpoint:
11197 case bp_hardware_watchpoint:
11198 case bp_read_watchpoint:
11199 case bp_access_watchpoint:
11200 enable_breakpoint (bpt);
11201 default:
11202 continue;
11203 }
0d381245
VP
11204 else if (strchr (args, '.'))
11205 {
11206 struct bp_location *loc = find_location_by_number (args);
11207 if (loc)
11208 loc->enabled = 1;
b60e7edf 11209 update_global_location_list (1);
0d381245 11210 }
c906108c 11211 else
95a42b64 11212 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
11213}
11214
11215static void
95a42b64 11216enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 11217{
b5de0fa7 11218 do_enable_breakpoint (bpt, disp_disable);
c906108c
SS
11219}
11220
c906108c 11221static void
fba45db2 11222enable_once_command (char *args, int from_tty)
c906108c 11223{
95a42b64 11224 map_breakpoint_numbers (args, enable_once_breakpoint, NULL);
c906108c
SS
11225}
11226
11227static void
95a42b64 11228enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 11229{
b5de0fa7 11230 do_enable_breakpoint (bpt, disp_del);
c906108c
SS
11231}
11232
c906108c 11233static void
fba45db2 11234enable_delete_command (char *args, int from_tty)
c906108c 11235{
95a42b64 11236 map_breakpoint_numbers (args, enable_delete_breakpoint, NULL);
c906108c
SS
11237}
11238\f
fa8d40ab
JJ
11239static void
11240set_breakpoint_cmd (char *args, int from_tty)
11241{
11242}
11243
11244static void
11245show_breakpoint_cmd (char *args, int from_tty)
11246{
11247}
11248
1f3b5d1b
PP
11249/* Invalidate last known value of any hardware watchpoint if
11250 the memory which that value represents has been written to by
11251 GDB itself. */
11252
11253static void
11254invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
11255 const bfd_byte *data)
11256{
11257 struct breakpoint *bp;
11258
11259 ALL_BREAKPOINTS (bp)
11260 if (bp->enable_state == bp_enabled
11261 && bp->type == bp_hardware_watchpoint
11262 && bp->val_valid && bp->val)
11263 {
11264 struct bp_location *loc;
11265
11266 for (loc = bp->loc; loc != NULL; loc = loc->next)
11267 if (loc->loc_type == bp_loc_hardware_watchpoint
11268 && loc->address + loc->length > addr
11269 && addr + len > loc->address)
11270 {
11271 value_free (bp->val);
11272 bp->val = NULL;
11273 bp->val_valid = 0;
11274 }
11275 }
11276}
11277
c906108c
SS
11278/* Use default_breakpoint_'s, or nothing if they aren't valid. */
11279
11280struct symtabs_and_lines
fba45db2 11281decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
11282{
11283 struct symtabs_and_lines sals;
cc59ec59 11284
c906108c 11285 if (string == 0)
8a3fe4f8 11286 error (_("Empty line specification."));
c906108c
SS
11287 if (default_breakpoint_valid)
11288 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
11289 default_breakpoint_symtab,
11290 default_breakpoint_line,
68219205 11291 (char ***) NULL, NULL);
c906108c
SS
11292 else
11293 sals = decode_line_1 (&string, funfirstline,
68219205 11294 (struct symtab *) NULL, 0, (char ***) NULL, NULL);
c906108c 11295 if (*string)
8a3fe4f8 11296 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
11297 return sals;
11298}
8181d85f
DJ
11299
11300/* Create and insert a raw software breakpoint at PC. Return an
11301 identifier, which should be used to remove the breakpoint later.
11302 In general, places which call this should be using something on the
11303 breakpoint chain instead; this function should be eliminated
11304 someday. */
11305
11306void *
6c95b8df
PA
11307deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
11308 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
11309{
11310 struct bp_target_info *bp_tgt;
11311
6c95b8df 11312 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 11313
6c95b8df 11314 bp_tgt->placed_address_space = aspace;
8181d85f 11315 bp_tgt->placed_address = pc;
6c95b8df 11316
a6d9a66e 11317 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
11318 {
11319 /* Could not insert the breakpoint. */
11320 xfree (bp_tgt);
11321 return NULL;
11322 }
11323
11324 return bp_tgt;
11325}
11326
4a64f543
MS
11327/* Remove a breakpoint BP inserted by
11328 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
11329
11330int
a6d9a66e 11331deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
11332{
11333 struct bp_target_info *bp_tgt = bp;
11334 int ret;
11335
a6d9a66e 11336 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
11337 xfree (bp_tgt);
11338
11339 return ret;
11340}
11341
4a64f543
MS
11342/* One (or perhaps two) breakpoints used for software single
11343 stepping. */
8181d85f
DJ
11344
11345static void *single_step_breakpoints[2];
a6d9a66e 11346static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
11347
11348/* Create and insert a breakpoint for software single step. */
11349
11350void
6c95b8df 11351insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
11352 struct address_space *aspace,
11353 CORE_ADDR next_pc)
8181d85f
DJ
11354{
11355 void **bpt_p;
11356
11357 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
11358 {
11359 bpt_p = &single_step_breakpoints[0];
11360 single_step_gdbarch[0] = gdbarch;
11361 }
8181d85f
DJ
11362 else
11363 {
11364 gdb_assert (single_step_breakpoints[1] == NULL);
11365 bpt_p = &single_step_breakpoints[1];
a6d9a66e 11366 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
11367 }
11368
4a64f543
MS
11369 /* NOTE drow/2006-04-11: A future improvement to this function would
11370 be to only create the breakpoints once, and actually put them on
11371 the breakpoint chain. That would let us use set_raw_breakpoint.
11372 We could adjust the addresses each time they were needed. Doing
11373 this requires corresponding changes elsewhere where single step
11374 breakpoints are handled, however. So, for now, we use this. */
8181d85f 11375
6c95b8df 11376 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 11377 if (*bpt_p == NULL)
5af949e3
UW
11378 error (_("Could not insert single-step breakpoint at %s"),
11379 paddress (gdbarch, next_pc));
8181d85f
DJ
11380}
11381
f02253f1
HZ
11382/* Check if the breakpoints used for software single stepping
11383 were inserted or not. */
11384
11385int
11386single_step_breakpoints_inserted (void)
11387{
11388 return (single_step_breakpoints[0] != NULL
11389 || single_step_breakpoints[1] != NULL);
11390}
11391
8181d85f
DJ
11392/* Remove and delete any breakpoints used for software single step. */
11393
11394void
11395remove_single_step_breakpoints (void)
11396{
11397 gdb_assert (single_step_breakpoints[0] != NULL);
11398
11399 /* See insert_single_step_breakpoint for more about this deprecated
11400 call. */
a6d9a66e
UW
11401 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
11402 single_step_breakpoints[0]);
11403 single_step_gdbarch[0] = NULL;
8181d85f
DJ
11404 single_step_breakpoints[0] = NULL;
11405
11406 if (single_step_breakpoints[1] != NULL)
11407 {
a6d9a66e
UW
11408 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
11409 single_step_breakpoints[1]);
11410 single_step_gdbarch[1] = NULL;
8181d85f
DJ
11411 single_step_breakpoints[1] = NULL;
11412 }
11413}
11414
d03285ec
UW
11415/* Delete software single step breakpoints without removing them from
11416 the inferior. This is intended to be used if the inferior's address
11417 space where they were inserted is already gone, e.g. after exit or
11418 exec. */
11419
11420void
11421cancel_single_step_breakpoints (void)
11422{
11423 int i;
11424
11425 for (i = 0; i < 2; i++)
11426 if (single_step_breakpoints[i])
11427 {
11428 xfree (single_step_breakpoints[i]);
11429 single_step_breakpoints[i] = NULL;
11430 single_step_gdbarch[i] = NULL;
11431 }
11432}
11433
11434/* Detach software single-step breakpoints from INFERIOR_PTID without
11435 removing them. */
11436
11437static void
11438detach_single_step_breakpoints (void)
11439{
11440 int i;
11441
11442 for (i = 0; i < 2; i++)
11443 if (single_step_breakpoints[i])
11444 target_remove_breakpoint (single_step_gdbarch[i],
11445 single_step_breakpoints[i]);
11446}
11447
4a64f543
MS
11448/* Check whether a software single-step breakpoint is inserted at
11449 PC. */
1aafd4da
UW
11450
11451static int
cc59ec59
MS
11452single_step_breakpoint_inserted_here_p (struct address_space *aspace,
11453 CORE_ADDR pc)
1aafd4da
UW
11454{
11455 int i;
11456
11457 for (i = 0; i < 2; i++)
11458 {
11459 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
11460 if (bp_tgt
11461 && breakpoint_address_match (bp_tgt->placed_address_space,
11462 bp_tgt->placed_address,
11463 aspace, pc))
1aafd4da
UW
11464 return 1;
11465 }
11466
11467 return 0;
11468}
11469
a96d9b2e
SDJ
11470/* Returns 0 if 'bp' is NOT a syscall catchpoint,
11471 non-zero otherwise. */
11472static int
11473is_syscall_catchpoint_enabled (struct breakpoint *bp)
11474{
11475 if (syscall_catchpoint_p (bp)
11476 && bp->enable_state != bp_disabled
11477 && bp->enable_state != bp_call_disabled)
11478 return 1;
11479 else
11480 return 0;
11481}
11482
11483int
11484catch_syscall_enabled (void)
11485{
11486 struct inferior *inf = current_inferior ();
11487
11488 return inf->total_syscalls_count != 0;
11489}
11490
11491int
11492catching_syscall_number (int syscall_number)
11493{
11494 struct breakpoint *bp;
11495
11496 ALL_BREAKPOINTS (bp)
11497 if (is_syscall_catchpoint_enabled (bp))
11498 {
11499 if (bp->syscalls_to_be_caught)
11500 {
11501 int i, iter;
11502 for (i = 0;
11503 VEC_iterate (int, bp->syscalls_to_be_caught, i, iter);
11504 i++)
11505 if (syscall_number == iter)
11506 return 1;
11507 }
11508 else
11509 return 1;
11510 }
11511
11512 return 0;
11513}
11514
11515/* Complete syscall names. Used by "catch syscall". */
11516static char **
11517catch_syscall_completer (struct cmd_list_element *cmd,
11518 char *text, char *word)
11519{
11520 const char **list = get_syscall_names ();
cc59ec59 11521
a96d9b2e
SDJ
11522 return (list == NULL) ? NULL : complete_on_enum (list, text, word);
11523}
11524
1042e4c0
SS
11525/* Tracepoint-specific operations. */
11526
11527/* Set tracepoint count to NUM. */
11528static void
11529set_tracepoint_count (int num)
11530{
11531 tracepoint_count = num;
4fa62494 11532 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
11533}
11534
11535void
11536trace_command (char *arg, int from_tty)
11537{
8cdf0e15
VP
11538 if (create_breakpoint (get_current_arch (),
11539 arg,
11540 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
11541 0 /* tempflag */,
11542 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
11543 0 /* Ignore count */,
11544 pending_break_support,
11545 NULL,
11546 from_tty,
84f4c1fe
PM
11547 1 /* enabled */,
11548 0 /* internal */))
fd9b8c24 11549 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
11550}
11551
7a697b8d
SS
11552void
11553ftrace_command (char *arg, int from_tty)
11554{
8cdf0e15
VP
11555 if (create_breakpoint (get_current_arch (),
11556 arg,
11557 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
11558 0 /* tempflag */,
11559 bp_fast_tracepoint /* type_wanted */,
11560 0 /* Ignore count */,
11561 pending_break_support,
11562 NULL,
11563 from_tty,
84f4c1fe
PM
11564 1 /* enabled */,
11565 0 /* internal */))
0fb4aa4b
PA
11566 set_tracepoint_count (breakpoint_count);
11567}
11568
11569/* strace command implementation. Creates a static tracepoint. */
11570
11571void
11572strace_command (char *arg, int from_tty)
11573{
11574 if (create_breakpoint (get_current_arch (),
11575 arg,
11576 NULL, 0, 1 /* parse arg */,
11577 0 /* tempflag */,
11578 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
11579 0 /* Ignore count */,
11580 pending_break_support,
11581 NULL,
11582 from_tty,
84f4c1fe
PM
11583 1 /* enabled */,
11584 0 /* internal */))
fd9b8c24 11585 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
11586}
11587
409873ef
SS
11588/* Set up a fake reader function that gets command lines from a linked
11589 list that was acquired during tracepoint uploading. */
11590
11591static struct uploaded_tp *this_utp;
3149d8c1 11592static int next_cmd;
409873ef
SS
11593
11594static char *
11595read_uploaded_action (void)
11596{
11597 char *rslt;
11598
3149d8c1 11599 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 11600
3149d8c1 11601 next_cmd++;
409873ef
SS
11602
11603 return rslt;
11604}
11605
00bf0b85
SS
11606/* Given information about a tracepoint as recorded on a target (which
11607 can be either a live system or a trace file), attempt to create an
11608 equivalent GDB tracepoint. This is not a reliable process, since
11609 the target does not necessarily have all the information used when
11610 the tracepoint was originally defined. */
11611
11612struct breakpoint *
11613create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 11614{
409873ef 11615 char *addr_str, small_buf[100];
d5551862 11616 struct breakpoint *tp;
fd9b8c24 11617
409873ef
SS
11618 if (utp->at_string)
11619 addr_str = utp->at_string;
11620 else
11621 {
11622 /* In the absence of a source location, fall back to raw
11623 address. Since there is no way to confirm that the address
11624 means the same thing as when the trace was started, warn the
11625 user. */
3e43a32a
MS
11626 warning (_("Uploaded tracepoint %d has no "
11627 "source location, using raw address"),
409873ef
SS
11628 utp->number);
11629 sprintf (small_buf, "*%s", hex_string (utp->addr));
11630 addr_str = small_buf;
11631 }
11632
11633 /* There's not much we can do with a sequence of bytecodes. */
11634 if (utp->cond && !utp->cond_string)
3e43a32a
MS
11635 warning (_("Uploaded tracepoint %d condition "
11636 "has no source form, ignoring it"),
409873ef 11637 utp->number);
d5551862 11638
8cdf0e15 11639 if (!create_breakpoint (get_current_arch (),
409873ef
SS
11640 addr_str,
11641 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 11642 0 /* tempflag */,
0fb4aa4b 11643 utp->type /* type_wanted */,
8cdf0e15
VP
11644 0 /* Ignore count */,
11645 pending_break_support,
11646 NULL,
11647 0 /* from_tty */,
84f4c1fe
PM
11648 utp->enabled /* enabled */,
11649 0 /* internal */))
fd9b8c24
PA
11650 return NULL;
11651
00bf0b85
SS
11652 set_tracepoint_count (breakpoint_count);
11653
409873ef 11654 /* Get the tracepoint we just created. */
fd9b8c24
PA
11655 tp = get_tracepoint (tracepoint_count);
11656 gdb_assert (tp != NULL);
d5551862 11657
00bf0b85
SS
11658 if (utp->pass > 0)
11659 {
409873ef 11660 sprintf (small_buf, "%d %d", utp->pass, tp->number);
00bf0b85 11661
409873ef 11662 trace_pass_command (small_buf, 0);
00bf0b85
SS
11663 }
11664
409873ef
SS
11665 /* If we have uploaded versions of the original commands, set up a
11666 special-purpose "reader" function and call the usual command line
11667 reader, then pass the result to the breakpoint command-setting
11668 function. */
3149d8c1 11669 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 11670 {
409873ef 11671 struct command_line *cmd_list;
00bf0b85 11672
409873ef 11673 this_utp = utp;
3149d8c1 11674 next_cmd = 0;
d5551862 11675
409873ef
SS
11676 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
11677
11678 breakpoint_set_commands (tp, cmd_list);
00bf0b85 11679 }
3149d8c1
SS
11680 else if (!VEC_empty (char_ptr, utp->actions)
11681 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
11682 warning (_("Uploaded tracepoint %d actions "
11683 "have no source form, ignoring them"),
409873ef 11684 utp->number);
00bf0b85
SS
11685
11686 return tp;
11687 }
11688
1042e4c0
SS
11689/* Print information on tracepoint number TPNUM_EXP, or all if
11690 omitted. */
11691
11692static void
11693tracepoints_info (char *tpnum_exp, int from_tty)
11694{
d77f58be
SS
11695 int tpnum = -1, num_printed;
11696
11697 if (tpnum_exp)
11698 tpnum = parse_and_eval_long (tpnum_exp);
1042e4c0 11699
d77f58be
SS
11700 num_printed = breakpoint_1 (tpnum, 0, is_tracepoint);
11701
11702 if (num_printed == 0)
1042e4c0 11703 {
d77f58be
SS
11704 if (tpnum == -1)
11705 ui_out_message (uiout, 0, "No tracepoints.\n");
11706 else
11707 ui_out_message (uiout, 0, "No tracepoint number %d.\n", tpnum);
1042e4c0 11708 }
ad443146
SS
11709
11710 default_collect_info ();
1042e4c0
SS
11711}
11712
4a64f543 11713/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
11714 Not supported by all targets. */
11715static void
11716enable_trace_command (char *args, int from_tty)
11717{
11718 enable_command (args, from_tty);
11719}
11720
4a64f543 11721/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
11722 Not supported by all targets. */
11723static void
11724disable_trace_command (char *args, int from_tty)
11725{
11726 disable_command (args, from_tty);
11727}
11728
4a64f543 11729/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
11730static void
11731delete_trace_command (char *arg, int from_tty)
11732{
35df4500 11733 struct breakpoint *b, *b_tmp;
1042e4c0
SS
11734
11735 dont_repeat ();
11736
11737 if (arg == 0)
11738 {
11739 int breaks_to_delete = 0;
11740
11741 /* Delete all breakpoints if no argument.
11742 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
11743 have to be deleted with an explicit breakpoint number
11744 argument. */
1042e4c0
SS
11745 ALL_TRACEPOINTS (b)
11746 {
11747 if (b->number >= 0)
11748 {
11749 breaks_to_delete = 1;
11750 break;
11751 }
11752 }
11753
11754 /* Ask user only if there are some breakpoints to delete. */
11755 if (!from_tty
11756 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
11757 {
35df4500 11758 ALL_BREAKPOINTS_SAFE (b, b_tmp)
1042e4c0 11759 {
d77f58be 11760 if (is_tracepoint (b)
059fb39f 11761 && b->number >= 0)
1042e4c0
SS
11762 delete_breakpoint (b);
11763 }
11764 }
11765 }
11766 else
95a42b64 11767 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
1042e4c0
SS
11768}
11769
11770/* Set passcount for tracepoint.
11771
11772 First command argument is passcount, second is tracepoint number.
11773 If tracepoint number omitted, apply to most recently defined.
11774 Also accepts special argument "all". */
11775
11776static void
11777trace_pass_command (char *args, int from_tty)
11778{
11779 struct breakpoint *t1 = (struct breakpoint *) -1, *t2;
11780 unsigned int count;
11781 int all = 0;
11782
11783 if (args == 0 || *args == 0)
3e43a32a
MS
11784 error (_("passcount command requires an "
11785 "argument (count + optional TP num)"));
1042e4c0 11786
4a64f543 11787 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
11788
11789 while (*args && isspace ((int) *args))
11790 args++;
11791
11792 if (*args && strncasecmp (args, "all", 3) == 0)
11793 {
11794 args += 3; /* Skip special argument "all". */
11795 all = 1;
11796 if (*args)
11797 error (_("Junk at end of arguments."));
11798 }
11799 else
11800 t1 = get_tracepoint_by_number (&args, 1, 1);
11801
11802 do
11803 {
11804 if (t1)
11805 {
11806 ALL_TRACEPOINTS (t2)
11807 if (t1 == (struct breakpoint *) -1 || t1 == t2)
11808 {
11809 t2->pass_count = count;
11810 observer_notify_tracepoint_modified (t2->number);
11811 if (from_tty)
3e43a32a
MS
11812 printf_filtered (_("Setting tracepoint %d's "
11813 "passcount to %d\n"),
1042e4c0
SS
11814 t2->number, count);
11815 }
11816 if (! all && *args)
11817 t1 = get_tracepoint_by_number (&args, 1, 0);
11818 }
11819 }
11820 while (*args);
11821}
11822
11823struct breakpoint *
11824get_tracepoint (int num)
11825{
11826 struct breakpoint *t;
11827
11828 ALL_TRACEPOINTS (t)
11829 if (t->number == num)
11830 return t;
11831
11832 return NULL;
11833}
11834
d5551862
SS
11835/* Find the tracepoint with the given target-side number (which may be
11836 different from the tracepoint number after disconnecting and
11837 reconnecting). */
11838
11839struct breakpoint *
11840get_tracepoint_by_number_on_target (int num)
11841{
11842 struct breakpoint *t;
11843
11844 ALL_TRACEPOINTS (t)
11845 if (t->number_on_target == num)
11846 return t;
11847
11848 return NULL;
11849}
11850
1042e4c0
SS
11851/* Utility: parse a tracepoint number and look it up in the list.
11852 If MULTI_P is true, there might be a range of tracepoints in ARG.
11853 if OPTIONAL_P is true, then if the argument is missing, the most
11854 recent tracepoint (tracepoint_count) is returned. */
11855struct breakpoint *
11856get_tracepoint_by_number (char **arg, int multi_p, int optional_p)
11857{
11858 extern int tracepoint_count;
11859 struct breakpoint *t;
11860 int tpnum;
11861 char *instring = arg == NULL ? NULL : *arg;
11862
11863 if (arg == NULL || *arg == NULL || ! **arg)
11864 {
11865 if (optional_p)
11866 tpnum = tracepoint_count;
11867 else
11868 error_no_arg (_("tracepoint number"));
11869 }
11870 else
11871 tpnum = multi_p ? get_number_or_range (arg) : get_number (arg);
11872
11873 if (tpnum <= 0)
11874 {
11875 if (instring && *instring)
11876 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
11877 instring);
11878 else
3e43a32a
MS
11879 printf_filtered (_("Tracepoint argument missing "
11880 "and no previous tracepoint\n"));
1042e4c0
SS
11881 return NULL;
11882 }
11883
11884 ALL_TRACEPOINTS (t)
11885 if (t->number == tpnum)
11886 {
11887 return t;
11888 }
11889
11890 /* FIXME: if we are in the middle of a range we don't want to give
11891 a message. The current interface to get_number_or_range doesn't
11892 allow us to discover this. */
11893 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
11894 return NULL;
11895}
11896
6149aea9
PA
11897/* Save information on user settable breakpoints (watchpoints, etc) to
11898 a new script file named FILENAME. If FILTER is non-NULL, call it
11899 on each breakpoint and only include the ones for which it returns
11900 non-zero. */
11901
1042e4c0 11902static void
6149aea9
PA
11903save_breakpoints (char *filename, int from_tty,
11904 int (*filter) (const struct breakpoint *))
1042e4c0
SS
11905{
11906 struct breakpoint *tp;
6149aea9 11907 int any = 0;
a7bdde9e 11908 char *pathname;
1042e4c0 11909 struct cleanup *cleanup;
a7bdde9e 11910 struct ui_file *fp;
6149aea9 11911 int extra_trace_bits = 0;
1042e4c0 11912
6149aea9
PA
11913 if (filename == 0 || *filename == 0)
11914 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
11915
11916 /* See if we have anything to save. */
6149aea9 11917 ALL_BREAKPOINTS (tp)
1042e4c0 11918 {
6149aea9 11919 /* Skip internal and momentary breakpoints. */
09d682a4 11920 if (!user_breakpoint_p (tp))
6149aea9
PA
11921 continue;
11922
11923 /* If we have a filter, only save the breakpoints it accepts. */
11924 if (filter && !filter (tp))
11925 continue;
11926
11927 any = 1;
11928
11929 if (is_tracepoint (tp))
11930 {
11931 extra_trace_bits = 1;
11932
11933 /* We can stop searching. */
11934 break;
11935 }
1042e4c0 11936 }
6149aea9
PA
11937
11938 if (!any)
1042e4c0 11939 {
6149aea9 11940 warning (_("Nothing to save."));
1042e4c0
SS
11941 return;
11942 }
11943
6149aea9 11944 pathname = tilde_expand (filename);
1042e4c0 11945 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 11946 fp = gdb_fopen (pathname, "w");
059fb39f 11947 if (!fp)
6149aea9
PA
11948 error (_("Unable to open file '%s' for saving (%s)"),
11949 filename, safe_strerror (errno));
a7bdde9e 11950 make_cleanup_ui_file_delete (fp);
8bf6485c 11951
6149aea9
PA
11952 if (extra_trace_bits)
11953 save_trace_state_variables (fp);
8bf6485c 11954
6149aea9 11955 ALL_BREAKPOINTS (tp)
1042e4c0 11956 {
6149aea9 11957 /* Skip internal and momentary breakpoints. */
09d682a4 11958 if (!user_breakpoint_p (tp))
6149aea9 11959 continue;
8bf6485c 11960
6149aea9
PA
11961 /* If we have a filter, only save the breakpoints it accepts. */
11962 if (filter && !filter (tp))
11963 continue;
11964
11965 if (tp->ops != NULL)
11966 (tp->ops->print_recreate) (tp, fp);
1042e4c0
SS
11967 else
11968 {
6149aea9
PA
11969 if (tp->type == bp_fast_tracepoint)
11970 fprintf_unfiltered (fp, "ftrace");
0fb4aa4b
PA
11971 if (tp->type == bp_static_tracepoint)
11972 fprintf_unfiltered (fp, "strace");
6149aea9
PA
11973 else if (tp->type == bp_tracepoint)
11974 fprintf_unfiltered (fp, "trace");
11975 else if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11976 fprintf_unfiltered (fp, "tbreak");
11977 else if (tp->type == bp_breakpoint)
11978 fprintf_unfiltered (fp, "break");
11979 else if (tp->type == bp_hardware_breakpoint
11980 && tp->disposition == disp_del)
11981 fprintf_unfiltered (fp, "thbreak");
11982 else if (tp->type == bp_hardware_breakpoint)
11983 fprintf_unfiltered (fp, "hbreak");
11984 else if (tp->type == bp_watchpoint)
11985 fprintf_unfiltered (fp, "watch");
11986 else if (tp->type == bp_hardware_watchpoint)
11987 fprintf_unfiltered (fp, "watch");
11988 else if (tp->type == bp_read_watchpoint)
11989 fprintf_unfiltered (fp, "rwatch");
11990 else if (tp->type == bp_access_watchpoint)
11991 fprintf_unfiltered (fp, "awatch");
11992 else
11993 internal_error (__FILE__, __LINE__,
11994 _("unhandled breakpoint type %d"), (int) tp->type);
11995
11996 if (tp->exp_string)
11997 fprintf_unfiltered (fp, " %s", tp->exp_string);
11998 else if (tp->addr_string)
11999 fprintf_unfiltered (fp, " %s", tp->addr_string);
12000 else
12001 {
12002 char tmp[40];
12003
12004 sprintf_vma (tmp, tp->loc->address);
12005 fprintf_unfiltered (fp, " *0x%s", tmp);
12006 }
1042e4c0
SS
12007 }
12008
6149aea9
PA
12009 if (tp->thread != -1)
12010 fprintf_unfiltered (fp, " thread %d", tp->thread);
12011
12012 if (tp->task != 0)
12013 fprintf_unfiltered (fp, " task %d", tp->task);
8bf6485c
SS
12014
12015 fprintf_unfiltered (fp, "\n");
12016
6149aea9
PA
12017 /* Note, we can't rely on tp->number for anything, as we can't
12018 assume the recreated breakpoint numbers will match. Use $bpnum
12019 instead. */
12020
12021 if (tp->cond_string)
12022 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
12023
12024 if (tp->ignore_count)
12025 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
12026
1042e4c0 12027 if (tp->pass_count)
a7bdde9e 12028 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
1042e4c0 12029
a7bdde9e 12030 if (tp->commands)
1042e4c0 12031 {
a7bdde9e
VP
12032 volatile struct gdb_exception ex;
12033
6149aea9 12034 fprintf_unfiltered (fp, " commands\n");
a7bdde9e
VP
12035
12036 ui_out_redirect (uiout, fp);
14dba4b4 12037 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 12038 {
9add0f1b 12039 print_command_lines (uiout, tp->commands->commands, 2);
a7bdde9e
VP
12040 }
12041 ui_out_redirect (uiout, NULL);
1042e4c0 12042
a7bdde9e
VP
12043 if (ex.reason < 0)
12044 throw_exception (ex);
1042e4c0 12045
a7bdde9e 12046 fprintf_unfiltered (fp, " end\n");
1042e4c0 12047 }
6149aea9
PA
12048
12049 if (tp->enable_state == bp_disabled)
12050 fprintf_unfiltered (fp, "disable\n");
12051
12052 /* If this is a multi-location breakpoint, check if the locations
12053 should be individually disabled. Watchpoint locations are
12054 special, and not user visible. */
12055 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
12056 {
12057 struct bp_location *loc;
12058 int n = 1;
12059
12060 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
12061 if (!loc->enabled)
12062 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
12063 }
1042e4c0 12064 }
8bf6485c 12065
6149aea9 12066 if (extra_trace_bits && *default_collect)
8bf6485c
SS
12067 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
12068
1042e4c0
SS
12069 do_cleanups (cleanup);
12070 if (from_tty)
6149aea9
PA
12071 printf_filtered (_("Saved to file '%s'.\n"), filename);
12072}
12073
12074/* The `save breakpoints' command. */
12075
12076static void
12077save_breakpoints_command (char *args, int from_tty)
12078{
12079 save_breakpoints (args, from_tty, NULL);
12080}
12081
12082/* The `save tracepoints' command. */
12083
12084static void
12085save_tracepoints_command (char *args, int from_tty)
12086{
12087 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
12088}
12089
12090/* Create a vector of all tracepoints. */
12091
12092VEC(breakpoint_p) *
12093all_tracepoints ()
12094{
12095 VEC(breakpoint_p) *tp_vec = 0;
12096 struct breakpoint *tp;
12097
12098 ALL_TRACEPOINTS (tp)
12099 {
12100 VEC_safe_push (breakpoint_p, tp_vec, tp);
12101 }
12102
12103 return tp_vec;
12104}
12105
c906108c 12106\f
4a64f543
MS
12107/* This help string is used for the break, hbreak, tbreak and thbreak
12108 commands. It is defined as a macro to prevent duplication.
12109 COMMAND should be a string constant containing the name of the
12110 command. */
31e2b00f
AS
12111#define BREAK_ARGS_HELP(command) \
12112command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
12113LOCATION may be a line number, function name, or \"*\" and an address.\n\
12114If a line number is specified, break at start of code for that line.\n\
12115If a function is specified, break at start of code for that function.\n\
12116If an address is specified, break at that exact address.\n\
dc10affe
PA
12117With no LOCATION, uses current execution address of the selected\n\
12118stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
12119\n\
12120THREADNUM is the number from \"info threads\".\n\
12121CONDITION is a boolean expression.\n\
12122\n\
d41c0fc8
PA
12123Multiple breakpoints at one place are permitted, and useful if their\n\
12124conditions are different.\n\
31e2b00f
AS
12125\n\
12126Do \"help breakpoints\" for info on other commands dealing with breakpoints."
12127
44feb3ce
TT
12128/* List of subcommands for "catch". */
12129static struct cmd_list_element *catch_cmdlist;
12130
12131/* List of subcommands for "tcatch". */
12132static struct cmd_list_element *tcatch_cmdlist;
12133
12134/* Like add_cmd, but add the command to both the "catch" and "tcatch"
12135 lists, and pass some additional user data to the command function. */
12136static void
12137add_catch_command (char *name, char *docstring,
12138 void (*sfunc) (char *args, int from_tty,
12139 struct cmd_list_element *command),
a96d9b2e
SDJ
12140 char **(*completer) (struct cmd_list_element *cmd,
12141 char *text, char *word),
44feb3ce
TT
12142 void *user_data_catch,
12143 void *user_data_tcatch)
12144{
12145 struct cmd_list_element *command;
12146
12147 command = add_cmd (name, class_breakpoint, NULL, docstring,
12148 &catch_cmdlist);
12149 set_cmd_sfunc (command, sfunc);
12150 set_cmd_context (command, user_data_catch);
a96d9b2e 12151 set_cmd_completer (command, completer);
44feb3ce
TT
12152
12153 command = add_cmd (name, class_breakpoint, NULL, docstring,
12154 &tcatch_cmdlist);
12155 set_cmd_sfunc (command, sfunc);
12156 set_cmd_context (command, user_data_tcatch);
a96d9b2e 12157 set_cmd_completer (command, completer);
44feb3ce
TT
12158}
12159
6c95b8df 12160static void
a79b8f6e 12161clear_syscall_counts (struct inferior *inf)
6c95b8df 12162{
6c95b8df
PA
12163 inf->total_syscalls_count = 0;
12164 inf->any_syscall_count = 0;
12165 VEC_free (int, inf->syscalls_counts);
12166}
12167
6149aea9
PA
12168static void
12169save_command (char *arg, int from_tty)
12170{
3e43a32a
MS
12171 printf_unfiltered (_("\"save\" must be followed by "
12172 "the name of a save subcommand.\n"));
6149aea9
PA
12173 help_list (save_cmdlist, "save ", -1, gdb_stdout);
12174}
12175
84f4c1fe
PM
12176struct breakpoint *
12177iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
12178 void *data)
12179{
35df4500 12180 struct breakpoint *b, *b_tmp;
84f4c1fe 12181
35df4500 12182 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
12183 {
12184 if ((*callback) (b, data))
12185 return b;
12186 }
12187
12188 return NULL;
12189}
12190
c906108c 12191void
fba45db2 12192_initialize_breakpoint (void)
c906108c
SS
12193{
12194 struct cmd_list_element *c;
12195
84acb35a 12196 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 12197 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 12198 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 12199
17450429
PP
12200 breakpoint_objfile_key = register_objfile_data ();
12201
c906108c
SS
12202 breakpoint_chain = 0;
12203 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
12204 before a breakpoint is set. */
12205 breakpoint_count = 0;
12206
1042e4c0
SS
12207 tracepoint_count = 0;
12208
1bedd215
AC
12209 add_com ("ignore", class_breakpoint, ignore_command, _("\
12210Set ignore-count of breakpoint number N to COUNT.\n\
12211Usage is `ignore N COUNT'."));
c906108c 12212 if (xdb_commands)
c5aa993b 12213 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 12214
1bedd215
AC
12215 add_com ("commands", class_breakpoint, commands_command, _("\
12216Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
12217Give breakpoint number as argument after \"commands\".\n\
12218With no argument, the targeted breakpoint is the last one set.\n\
12219The commands themselves follow starting on the next line.\n\
12220Type a line containing \"end\" to indicate the end of them.\n\
12221Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 12222then no output is printed when it is hit, except what the commands print."));
c906108c 12223
1bedd215
AC
12224 add_com ("condition", class_breakpoint, condition_command, _("\
12225Specify breakpoint number N to break only if COND is true.\n\
c906108c 12226Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 12227expression to be evaluated whenever breakpoint N is reached."));
c906108c 12228
1bedd215 12229 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 12230Set a temporary breakpoint.\n\
c906108c
SS
12231Like \"break\" except the breakpoint is only temporary,\n\
12232so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
12233by using \"enable delete\" on the breakpoint number.\n\
12234\n"
12235BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 12236 set_cmd_completer (c, location_completer);
c94fdfd0 12237
1bedd215 12238 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 12239Set a hardware assisted breakpoint.\n\
c906108c 12240Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
12241some target hardware may not have this support.\n\
12242\n"
12243BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 12244 set_cmd_completer (c, location_completer);
c906108c 12245
1bedd215 12246 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 12247Set a temporary hardware assisted breakpoint.\n\
c906108c 12248Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
12249so it will be deleted when hit.\n\
12250\n"
12251BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 12252 set_cmd_completer (c, location_completer);
c906108c 12253
1bedd215
AC
12254 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
12255Enable some breakpoints.\n\
c906108c
SS
12256Give breakpoint numbers (separated by spaces) as arguments.\n\
12257With no subcommand, breakpoints are enabled until you command otherwise.\n\
12258This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12259With a subcommand you can enable temporarily."),
c906108c
SS
12260 &enablelist, "enable ", 1, &cmdlist);
12261 if (xdb_commands)
1bedd215
AC
12262 add_com ("ab", class_breakpoint, enable_command, _("\
12263Enable some breakpoints.\n\
c906108c
SS
12264Give breakpoint numbers (separated by spaces) as arguments.\n\
12265With no subcommand, breakpoints are enabled until you command otherwise.\n\
12266This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12267With a subcommand you can enable temporarily."));
c906108c
SS
12268
12269 add_com_alias ("en", "enable", class_breakpoint, 1);
12270
84951ab5 12271 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 12272Enable some breakpoints.\n\
c906108c
SS
12273Give breakpoint numbers (separated by spaces) as arguments.\n\
12274This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12275May be abbreviated to simply \"enable\".\n"),
c5aa993b 12276 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 12277
1a966eab
AC
12278 add_cmd ("once", no_class, enable_once_command, _("\
12279Enable breakpoints for one hit. Give breakpoint numbers.\n\
12280If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
12281 &enablebreaklist);
12282
1a966eab
AC
12283 add_cmd ("delete", no_class, enable_delete_command, _("\
12284Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12285If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
12286 &enablebreaklist);
12287
1a966eab
AC
12288 add_cmd ("delete", no_class, enable_delete_command, _("\
12289Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12290If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
12291 &enablelist);
12292
1a966eab
AC
12293 add_cmd ("once", no_class, enable_once_command, _("\
12294Enable breakpoints for one hit. Give breakpoint numbers.\n\
12295If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
12296 &enablelist);
12297
1bedd215
AC
12298 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
12299Disable some breakpoints.\n\
c906108c
SS
12300Arguments are breakpoint numbers with spaces in between.\n\
12301To disable all breakpoints, give no argument.\n\
1bedd215 12302A disabled breakpoint is not forgotten, but has no effect until reenabled."),
c906108c
SS
12303 &disablelist, "disable ", 1, &cmdlist);
12304 add_com_alias ("dis", "disable", class_breakpoint, 1);
12305 add_com_alias ("disa", "disable", class_breakpoint, 1);
12306 if (xdb_commands)
1bedd215
AC
12307 add_com ("sb", class_breakpoint, disable_command, _("\
12308Disable some breakpoints.\n\
c906108c
SS
12309Arguments are breakpoint numbers with spaces in between.\n\
12310To disable all breakpoints, give no argument.\n\
1bedd215 12311A disabled breakpoint is not forgotten, but has no effect until reenabled."));
c906108c 12312
1a966eab
AC
12313 add_cmd ("breakpoints", class_alias, disable_command, _("\
12314Disable some breakpoints.\n\
c906108c
SS
12315Arguments are breakpoint numbers with spaces in between.\n\
12316To disable all breakpoints, give no argument.\n\
12317A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1a966eab 12318This command may be abbreviated \"disable\"."),
c906108c
SS
12319 &disablelist);
12320
1bedd215
AC
12321 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
12322Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
12323Arguments are breakpoint numbers with spaces in between.\n\
12324To delete all breakpoints, give no argument.\n\
12325\n\
12326Also a prefix command for deletion of other GDB objects.\n\
1bedd215 12327The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
12328 &deletelist, "delete ", 1, &cmdlist);
12329 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 12330 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 12331 if (xdb_commands)
1bedd215
AC
12332 add_com ("db", class_breakpoint, delete_command, _("\
12333Delete some breakpoints.\n\
c906108c 12334Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 12335To delete all breakpoints, give no argument.\n"));
c906108c 12336
1a966eab
AC
12337 add_cmd ("breakpoints", class_alias, delete_command, _("\
12338Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
12339Arguments are breakpoint numbers with spaces in between.\n\
12340To delete all breakpoints, give no argument.\n\
1a966eab 12341This command may be abbreviated \"delete\"."),
c906108c
SS
12342 &deletelist);
12343
1bedd215
AC
12344 add_com ("clear", class_breakpoint, clear_command, _("\
12345Clear breakpoint at specified line or function.\n\
c906108c
SS
12346Argument may be line number, function name, or \"*\" and an address.\n\
12347If line number is specified, all breakpoints in that line are cleared.\n\
12348If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
12349If an address is specified, breakpoints at that address are cleared.\n\
12350\n\
12351With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
12352is executing in.\n\
12353\n\
1bedd215 12354See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 12355 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 12356
1bedd215 12357 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
12358Set breakpoint at specified line or function.\n"
12359BREAK_ARGS_HELP ("break")));
5ba2abeb 12360 set_cmd_completer (c, location_completer);
c94fdfd0 12361
c906108c
SS
12362 add_com_alias ("b", "break", class_run, 1);
12363 add_com_alias ("br", "break", class_run, 1);
12364 add_com_alias ("bre", "break", class_run, 1);
12365 add_com_alias ("brea", "break", class_run, 1);
12366
7681d515
PM
12367 if (xdb_commands)
12368 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
12369
12370 if (dbx_commands)
12371 {
1bedd215
AC
12372 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
12373Break in function/address or break at a line in the current file."),
c5aa993b
JM
12374 &stoplist, "stop ", 1, &cmdlist);
12375 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 12376 _("Break in function or address."), &stoplist);
c5aa993b 12377 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 12378 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
12379 add_com ("status", class_info, breakpoints_info, _("\
12380Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12381The \"Type\" column indicates one of:\n\
12382\tbreakpoint - normal breakpoint\n\
12383\twatchpoint - watchpoint\n\
12384The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12385the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12386breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12387address and file/line number respectively.\n\
12388\n\
12389Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12390are set to the address of the last breakpoint listed unless the command\n\
12391is prefixed with \"server \".\n\n\
c906108c 12392Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12393breakpoint set."));
c906108c
SS
12394 }
12395
1bedd215
AC
12396 add_info ("breakpoints", breakpoints_info, _("\
12397Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12398The \"Type\" column indicates one of:\n\
12399\tbreakpoint - normal breakpoint\n\
12400\twatchpoint - watchpoint\n\
12401The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12402the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12403breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12404address and file/line number respectively.\n\
12405\n\
12406Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12407are set to the address of the last breakpoint listed unless the command\n\
12408is prefixed with \"server \".\n\n\
c906108c 12409Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12410breakpoint set."));
c906108c 12411
6b04bdb7
MS
12412 add_info_alias ("b", "breakpoints", 1);
12413
c906108c 12414 if (xdb_commands)
1bedd215
AC
12415 add_com ("lb", class_breakpoint, breakpoints_info, _("\
12416Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12417The \"Type\" column indicates one of:\n\
12418\tbreakpoint - normal breakpoint\n\
12419\twatchpoint - watchpoint\n\
12420The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12421the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12422breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12423address and file/line number respectively.\n\
12424\n\
12425Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12426are set to the address of the last breakpoint listed unless the command\n\
12427is prefixed with \"server \".\n\n\
c906108c 12428Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12429breakpoint set."));
c906108c 12430
1a966eab
AC
12431 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
12432Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12433The \"Type\" column indicates one of:\n\
12434\tbreakpoint - normal breakpoint\n\
12435\twatchpoint - watchpoint\n\
12436\tlongjmp - internal breakpoint used to step through longjmp()\n\
12437\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
12438\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
12439\tfinish - internal breakpoint used by the \"finish\" command\n\
12440The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
12441the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12442breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
12443address and file/line number respectively.\n\
12444\n\
12445Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12446are set to the address of the last breakpoint listed unless the command\n\
12447is prefixed with \"server \".\n\n\
c906108c 12448Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 12449breakpoint set."),
c906108c
SS
12450 &maintenanceinfolist);
12451
44feb3ce
TT
12452 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
12453Set catchpoints to catch events."),
12454 &catch_cmdlist, "catch ",
12455 0/*allow-unknown*/, &cmdlist);
12456
12457 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
12458Set temporary catchpoints to catch events."),
12459 &tcatch_cmdlist, "tcatch ",
12460 0/*allow-unknown*/, &cmdlist);
12461
12462 /* Add catch and tcatch sub-commands. */
12463 add_catch_command ("catch", _("\
12464Catch an exception, when caught.\n\
12465With an argument, catch only exceptions with the given name."),
12466 catch_catch_command,
a96d9b2e 12467 NULL,
44feb3ce
TT
12468 CATCH_PERMANENT,
12469 CATCH_TEMPORARY);
12470 add_catch_command ("throw", _("\
12471Catch an exception, when thrown.\n\
12472With an argument, catch only exceptions with the given name."),
12473 catch_throw_command,
a96d9b2e 12474 NULL,
44feb3ce
TT
12475 CATCH_PERMANENT,
12476 CATCH_TEMPORARY);
12477 add_catch_command ("fork", _("Catch calls to fork."),
12478 catch_fork_command_1,
a96d9b2e 12479 NULL,
44feb3ce
TT
12480 (void *) (uintptr_t) catch_fork_permanent,
12481 (void *) (uintptr_t) catch_fork_temporary);
12482 add_catch_command ("vfork", _("Catch calls to vfork."),
12483 catch_fork_command_1,
a96d9b2e 12484 NULL,
44feb3ce
TT
12485 (void *) (uintptr_t) catch_vfork_permanent,
12486 (void *) (uintptr_t) catch_vfork_temporary);
12487 add_catch_command ("exec", _("Catch calls to exec."),
12488 catch_exec_command_1,
a96d9b2e
SDJ
12489 NULL,
12490 CATCH_PERMANENT,
12491 CATCH_TEMPORARY);
12492 add_catch_command ("syscall", _("\
12493Catch system calls by their names and/or numbers.\n\
12494Arguments say which system calls to catch. If no arguments\n\
12495are given, every system call will be caught.\n\
12496Arguments, if given, should be one or more system call names\n\
12497(if your system supports that), or system call numbers."),
12498 catch_syscall_command_1,
12499 catch_syscall_completer,
44feb3ce
TT
12500 CATCH_PERMANENT,
12501 CATCH_TEMPORARY);
44feb3ce
TT
12502 add_catch_command ("exception", _("\
12503Catch Ada exceptions, when raised.\n\
12504With an argument, catch only exceptions with the given name."),
12505 catch_ada_exception_command,
a96d9b2e 12506 NULL,
44feb3ce
TT
12507 CATCH_PERMANENT,
12508 CATCH_TEMPORARY);
12509 add_catch_command ("assert", _("\
12510Catch failed Ada assertions, when raised.\n\
12511With an argument, catch only exceptions with the given name."),
12512 catch_assert_command,
a96d9b2e 12513 NULL,
44feb3ce
TT
12514 CATCH_PERMANENT,
12515 CATCH_TEMPORARY);
c5aa993b 12516
1bedd215
AC
12517 c = add_com ("watch", class_breakpoint, watch_command, _("\
12518Set a watchpoint for an expression.\n\
06a64a0b 12519Usage: watch [-l|-location] EXPRESSION\n\
c906108c 12520A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12521an expression changes.\n\
12522If -l or -location is given, this evaluates EXPRESSION and watches\n\
12523the memory to which it refers."));
65d12d83 12524 set_cmd_completer (c, expression_completer);
c906108c 12525
1bedd215
AC
12526 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
12527Set a read watchpoint for an expression.\n\
06a64a0b 12528Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 12529A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12530an expression is read.\n\
12531If -l or -location is given, this evaluates EXPRESSION and watches\n\
12532the memory to which it refers."));
65d12d83 12533 set_cmd_completer (c, expression_completer);
c906108c 12534
1bedd215
AC
12535 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
12536Set a watchpoint for an expression.\n\
06a64a0b 12537Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 12538A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12539an expression is either read or written.\n\
12540If -l or -location is given, this evaluates EXPRESSION and watches\n\
12541the memory to which it refers."));
65d12d83 12542 set_cmd_completer (c, expression_completer);
c906108c 12543
d77f58be
SS
12544 add_info ("watchpoints", watchpoints_info, _("\
12545Status of watchpoints, or watchpoint number NUMBER."));
12546
c906108c
SS
12547
12548
920d2a44
AC
12549 /* XXX: cagney/2005-02-23: This should be a boolean, and should
12550 respond to changes - contrary to the description. */
85c07804
AC
12551 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
12552 &can_use_hw_watchpoints, _("\
12553Set debugger's willingness to use watchpoint hardware."), _("\
12554Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
12555If zero, gdb will not use hardware for new watchpoints, even if\n\
12556such is available. (However, any hardware watchpoints that were\n\
12557created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
12558hardware.)"),
12559 NULL,
920d2a44 12560 show_can_use_hw_watchpoints,
85c07804 12561 &setlist, &showlist);
c906108c
SS
12562
12563 can_use_hw_watchpoints = 1;
fa8d40ab 12564
1042e4c0
SS
12565 /* Tracepoint manipulation commands. */
12566
12567 c = add_com ("trace", class_breakpoint, trace_command, _("\
12568Set a tracepoint at specified line or function.\n\
12569\n"
12570BREAK_ARGS_HELP ("trace") "\n\
12571Do \"help tracepoints\" for info on other tracepoint commands."));
12572 set_cmd_completer (c, location_completer);
12573
12574 add_com_alias ("tp", "trace", class_alias, 0);
12575 add_com_alias ("tr", "trace", class_alias, 1);
12576 add_com_alias ("tra", "trace", class_alias, 1);
12577 add_com_alias ("trac", "trace", class_alias, 1);
12578
7a697b8d
SS
12579 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
12580Set a fast tracepoint at specified line or function.\n\
12581\n"
12582BREAK_ARGS_HELP ("ftrace") "\n\
12583Do \"help tracepoints\" for info on other tracepoint commands."));
12584 set_cmd_completer (c, location_completer);
12585
0fb4aa4b
PA
12586 c = add_com ("strace", class_breakpoint, strace_command, _("\
12587Set a static tracepoint at specified line, function or marker.\n\
12588\n\
12589strace [LOCATION] [if CONDITION]\n\
12590LOCATION may be a line number, function name, \"*\" and an address,\n\
12591or -m MARKER_ID.\n\
12592If a line number is specified, probe the marker at start of code\n\
12593for that line. If a function is specified, probe the marker at start\n\
12594of code for that function. If an address is specified, probe the marker\n\
12595at that exact address. If a marker id is specified, probe the marker\n\
12596with that name. With no LOCATION, uses current execution address of\n\
12597the selected stack frame.\n\
12598Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
12599This collects arbitrary user data passed in the probe point call to the\n\
12600tracing library. You can inspect it when analyzing the trace buffer,\n\
12601by printing the $_sdata variable like any other convenience variable.\n\
12602\n\
12603CONDITION is a boolean expression.\n\
12604\n\
d41c0fc8
PA
12605Multiple tracepoints at one place are permitted, and useful if their\n\
12606conditions are different.\n\
0fb4aa4b
PA
12607\n\
12608Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
12609Do \"help tracepoints\" for info on other tracepoint commands."));
12610 set_cmd_completer (c, location_completer);
12611
1042e4c0
SS
12612 add_info ("tracepoints", tracepoints_info, _("\
12613Status of tracepoints, or tracepoint number NUMBER.\n\
12614Convenience variable \"$tpnum\" contains the number of the\n\
12615last tracepoint set."));
12616
12617 add_info_alias ("tp", "tracepoints", 1);
12618
12619 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
12620Delete specified tracepoints.\n\
12621Arguments are tracepoint numbers, separated by spaces.\n\
12622No argument means delete all tracepoints."),
12623 &deletelist);
12624
12625 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
12626Disable specified tracepoints.\n\
12627Arguments are tracepoint numbers, separated by spaces.\n\
12628No argument means disable all tracepoints."),
12629 &disablelist);
12630 deprecate_cmd (c, "disable");
12631
12632 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
12633Enable specified tracepoints.\n\
12634Arguments are tracepoint numbers, separated by spaces.\n\
12635No argument means enable all tracepoints."),
12636 &enablelist);
12637 deprecate_cmd (c, "enable");
12638
12639 add_com ("passcount", class_trace, trace_pass_command, _("\
12640Set the passcount for a tracepoint.\n\
12641The trace will end when the tracepoint has been passed 'count' times.\n\
12642Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
12643if TPNUM is omitted, passcount refers to the last tracepoint defined."));
12644
6149aea9
PA
12645 add_prefix_cmd ("save", class_breakpoint, save_command,
12646 _("Save breakpoint definitions as a script."),
12647 &save_cmdlist, "save ",
12648 0/*allow-unknown*/, &cmdlist);
12649
12650 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
12651Save current breakpoint definitions as a script.\n\
cce7e648 12652This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
12653catchpoints, tracepoints). Use the 'source' command in another debug\n\
12654session to restore them."),
12655 &save_cmdlist);
12656 set_cmd_completer (c, filename_completer);
12657
12658 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 12659Save current tracepoint definitions as a script.\n\
6149aea9
PA
12660Use the 'source' command in another debug session to restore them."),
12661 &save_cmdlist);
1042e4c0
SS
12662 set_cmd_completer (c, filename_completer);
12663
6149aea9
PA
12664 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
12665 deprecate_cmd (c, "save tracepoints");
12666
1bedd215 12667 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
12668Breakpoint specific settings\n\
12669Configure various breakpoint-specific variables such as\n\
1bedd215 12670pending breakpoint behavior"),
fa8d40ab
JJ
12671 &breakpoint_set_cmdlist, "set breakpoint ",
12672 0/*allow-unknown*/, &setlist);
1bedd215 12673 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
12674Breakpoint specific settings\n\
12675Configure various breakpoint-specific variables such as\n\
1bedd215 12676pending breakpoint behavior"),
fa8d40ab
JJ
12677 &breakpoint_show_cmdlist, "show breakpoint ",
12678 0/*allow-unknown*/, &showlist);
12679
7915a72c
AC
12680 add_setshow_auto_boolean_cmd ("pending", no_class,
12681 &pending_break_support, _("\
12682Set debugger's behavior regarding pending breakpoints."), _("\
12683Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
12684If on, an unrecognized breakpoint location will cause gdb to create a\n\
12685pending breakpoint. If off, an unrecognized breakpoint location results in\n\
12686an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 12687user-query to see if a pending breakpoint should be created."),
2c5b56ce 12688 NULL,
920d2a44 12689 show_pending_break_support,
6e1d7d6c
AC
12690 &breakpoint_set_cmdlist,
12691 &breakpoint_show_cmdlist);
fa8d40ab
JJ
12692
12693 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
12694
12695 add_setshow_boolean_cmd ("auto-hw", no_class,
12696 &automatic_hardware_breakpoints, _("\
12697Set automatic usage of hardware breakpoints."), _("\
12698Show automatic usage of hardware breakpoints."), _("\
12699If set, the debugger will automatically use hardware breakpoints for\n\
12700breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
12701a warning will be emitted for such breakpoints."),
12702 NULL,
12703 show_automatic_hardware_breakpoints,
12704 &breakpoint_set_cmdlist,
12705 &breakpoint_show_cmdlist);
74960c60 12706
33e5cbd6
PA
12707 add_setshow_enum_cmd ("always-inserted", class_support,
12708 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
12709Set mode for inserting breakpoints."), _("\
12710Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
12711When this mode is off, breakpoints are inserted in inferior when it is\n\
12712resumed, and removed when execution stops. When this mode is on,\n\
12713breakpoints are inserted immediately and removed only when the user\n\
12714deletes the breakpoint. When this mode is auto (which is the default),\n\
12715the behaviour depends on the non-stop setting (see help set non-stop).\n\
12716In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
12717behaves as if always-inserted mode is on; if gdb is controlling the\n\
12718inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
12719 NULL,
12720 &show_always_inserted_mode,
12721 &breakpoint_set_cmdlist,
12722 &breakpoint_show_cmdlist);
765dc015
VP
12723
12724 automatic_hardware_breakpoints = 1;
f3b1572e
PA
12725
12726 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 12727}
This page took 2.103362 seconds and 4 git commands to generate.