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