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