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