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