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