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