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