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