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