2011-07-25 Pedro Alves <pedro@codesourcery.com>
[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,
7b6bb8da 5 2008, 2009, 2010, 2011 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"
0ba1096a 42#include "filenames.h"
c906108c
SS
43#include "annotate.h"
44#include "symfile.h"
45#include "objfiles.h"
0378c332 46#include "source.h"
c5f0f3d0 47#include "linespec.h"
c94fdfd0 48#include "completer.h"
5b7f31a4 49#include "gdb.h"
8b93c638 50#include "ui-out.h"
e1507482 51#include "cli/cli-script.h"
0225421b 52#include "gdb_assert.h"
fe898f56 53#include "block.h"
a77053c2 54#include "solib.h"
84acb35a
JJ
55#include "solist.h"
56#include "observer.h"
60250e8b 57#include "exceptions.h"
765dc015 58#include "memattr.h"
f7f9143b 59#include "ada-lang.h"
d1aa2f50 60#include "top.h"
fa4727a6 61#include "wrapper.h"
79a45b7d 62#include "valprint.h"
4efc6507 63#include "jit.h"
a96d9b2e 64#include "xml-syscall.h"
65d79d4b 65#include "parser-defs.h"
e9cafbcc 66#include "cli/cli-utils.h"
be34f849 67#include "continuations.h"
c906108c 68
1042e4c0
SS
69/* readline include files */
70#include "readline/readline.h"
71#include "readline/history.h"
72
73/* readline defines this. */
74#undef savestring
75
034dad6f 76#include "mi/mi-common.h"
7371cf6d 77#include "python/python.h"
104c1213 78
4a64f543 79/* Prototypes for local functions. */
c906108c 80
a14ed312 81static void enable_delete_command (char *, int);
c906108c 82
a14ed312 83static void enable_once_command (char *, int);
c906108c 84
a14ed312 85static void disable_command (char *, int);
c906108c 86
a14ed312 87static void enable_command (char *, int);
c906108c 88
95a42b64
TT
89static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
90 void *),
91 void *);
c906108c 92
a14ed312 93static void ignore_command (char *, int);
c906108c 94
4efb68b1 95static int breakpoint_re_set_one (void *);
c906108c 96
348d480f
PA
97static void breakpoint_re_set_default (struct breakpoint *);
98
a14ed312 99static void clear_command (char *, int);
c906108c 100
a14ed312 101static void catch_command (char *, int);
c906108c 102
a9634178 103static int can_use_hardware_watchpoint (struct value *);
c906108c 104
98deb0da 105static void break_command_1 (char *, int, int);
c906108c 106
a14ed312 107static void mention (struct breakpoint *);
c906108c 108
348d480f
PA
109static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
110 enum bptype,
111 struct breakpoint_ops *);
4a64f543
MS
112/* This function is used in gdbtk sources and thus can not be made
113 static. */
63c252f8 114struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f
PA
115 struct symtab_and_line,
116 enum bptype, struct breakpoint_ops *);
c906108c 117
06edf0c0
PA
118static struct breakpoint *
119 momentary_breakpoint_from_master (struct breakpoint *orig,
120 enum bptype type,
121 struct breakpoint_ops *ops);
122
76897487
KB
123static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
124
a6d9a66e
UW
125static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
126 CORE_ADDR bpaddr,
88f7da05 127 enum bptype bptype);
76897487 128
6c95b8df
PA
129static void describe_other_breakpoints (struct gdbarch *,
130 struct program_space *, CORE_ADDR,
5af949e3 131 struct obj_section *, int);
c906108c 132
6c95b8df
PA
133static int breakpoint_address_match (struct address_space *aspace1,
134 CORE_ADDR addr1,
135 struct address_space *aspace2,
136 CORE_ADDR addr2);
137
85d721b8
PA
138static int watchpoint_locations_match (struct bp_location *loc1,
139 struct bp_location *loc2);
140
f1310107
TJB
141static int breakpoint_location_address_match (struct bp_location *bl,
142 struct address_space *aspace,
143 CORE_ADDR addr);
144
a14ed312 145static void breakpoints_info (char *, int);
c906108c 146
d77f58be
SS
147static void watchpoints_info (char *, int);
148
e5a67952
MS
149static int breakpoint_1 (char *, int,
150 int (*) (const struct breakpoint *));
c906108c 151
4efb68b1 152static int breakpoint_cond_eval (void *);
c906108c 153
4efb68b1 154static void cleanup_executing_breakpoints (void *);
c906108c 155
a14ed312 156static void commands_command (char *, int);
c906108c 157
a14ed312 158static void condition_command (char *, int);
c906108c 159
c5aa993b
JM
160typedef enum
161 {
162 mark_inserted,
163 mark_uninserted
164 }
165insertion_state_t;
c906108c 166
0bde7532 167static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 168static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 169
e514a9d6 170static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 171
4efb68b1 172static int watchpoint_check (void *);
c906108c 173
a14ed312 174static void maintenance_info_breakpoints (char *, int);
c906108c 175
a14ed312 176static int hw_breakpoint_used_count (void);
c906108c 177
a14ed312 178static int hw_watchpoint_used_count (enum bptype, int *);
c906108c 179
a14ed312 180static void hbreak_command (char *, int);
c906108c 181
a14ed312 182static void thbreak_command (char *, int);
c906108c 183
51be5b68 184static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp);
c906108c 185
a14ed312 186static void stop_command (char *arg, int from_tty);
7a292a7a 187
a14ed312 188static void stopin_command (char *arg, int from_tty);
7a292a7a 189
a14ed312 190static void stopat_command (char *arg, int from_tty);
7a292a7a 191
a14ed312 192static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 193
d85310f7
MS
194static void catch_exception_command_1 (enum exception_event_kind ex_event,
195 char *arg, int tempflag, int from_tty);
7a292a7a 196
a14ed312 197static void tcatch_command (char *arg, int from_tty);
7a292a7a 198
d03285ec
UW
199static void detach_single_step_breakpoints (void);
200
6c95b8df
PA
201static int single_step_breakpoint_inserted_here_p (struct address_space *,
202 CORE_ADDR pc);
1aafd4da 203
fe3f5fa8 204static void free_bp_location (struct bp_location *loc);
f431efe5
PA
205static void incref_bp_location (struct bp_location *loc);
206static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 207
39d61571 208static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 209
b60e7edf 210static void update_global_location_list (int);
a5606eee 211
b60e7edf 212static void update_global_location_list_nothrow (int);
74960c60 213
d77f58be 214static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
215
216static void insert_breakpoint_locations (void);
a5606eee 217
a96d9b2e
SDJ
218static int syscall_catchpoint_p (struct breakpoint *b);
219
1042e4c0
SS
220static void tracepoints_info (char *, int);
221
222static void delete_trace_command (char *, int);
223
224static void enable_trace_command (char *, int);
225
226static void disable_trace_command (char *, int);
227
228static void trace_pass_command (char *, int);
229
9c06b0b4
TJB
230static int is_masked_watchpoint (const struct breakpoint *b);
231
0fb4aa4b
PA
232/* Assuming we're creating a static tracepoint, does S look like a
233 static tracepoint marker spec ("-m MARKER_ID")? */
234#define is_marker_spec(s) \
f5a8e22b 235 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 236
2060206e
PA
237/* The abstract base class all breakpoint_ops structures inherit
238 from. */
239static struct breakpoint_ops base_breakpoint_ops;
240
241/* The breakpoint_ops structure to be inherited by all breakpoint_ops
242 that are implemented on top of software or hardware breakpoints
243 (user breakpoints, internal and momentary breakpoints, etc.). */
244static struct breakpoint_ops bkpt_base_breakpoint_ops;
245
246/* Internal breakpoints class type. */
06edf0c0 247static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
248
249/* Momentary breakpoints class type. */
06edf0c0
PA
250static struct breakpoint_ops momentary_breakpoint_ops;
251
2060206e
PA
252/* The breakpoint_ops structure to be used in regular user created
253 breakpoints. */
254struct breakpoint_ops bkpt_breakpoint_ops;
255
5cea2a26
PA
256/* A reference-counted struct command_line. This lets multiple
257 breakpoints share a single command list. */
258struct counted_command_line
259{
260 /* The reference count. */
261 int refc;
262
263 /* The command list. */
264 struct command_line *commands;
265};
266
267struct command_line *
268breakpoint_commands (struct breakpoint *b)
269{
270 return b->commands ? b->commands->commands : NULL;
271}
3daf8fe5 272
f3b1572e
PA
273/* Flag indicating that a command has proceeded the inferior past the
274 current breakpoint. */
275
276static int breakpoint_proceeded;
277
956a9fb9 278const char *
2cec12e5
AR
279bpdisp_text (enum bpdisp disp)
280{
4a64f543
MS
281 /* NOTE: the following values are a part of MI protocol and
282 represent values of 'disp' field returned when inferior stops at
283 a breakpoint. */
bc043ef3 284 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 285
2cec12e5
AR
286 return bpdisps[(int) disp];
287}
c906108c 288
4a64f543 289/* Prototypes for exported functions. */
c906108c 290/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 291 if such is available. */
c906108c
SS
292static int can_use_hw_watchpoints;
293
920d2a44
AC
294static void
295show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
296 struct cmd_list_element *c,
297 const char *value)
298{
3e43a32a
MS
299 fprintf_filtered (file,
300 _("Debugger's willingness to use "
301 "watchpoint hardware is %s.\n"),
920d2a44
AC
302 value);
303}
304
fa8d40ab
JJ
305/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
306 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 307 for unrecognized breakpoint locations.
fa8d40ab
JJ
308 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
309static enum auto_boolean pending_break_support;
920d2a44
AC
310static void
311show_pending_break_support (struct ui_file *file, int from_tty,
312 struct cmd_list_element *c,
313 const char *value)
314{
3e43a32a
MS
315 fprintf_filtered (file,
316 _("Debugger's behavior regarding "
317 "pending breakpoints is %s.\n"),
920d2a44
AC
318 value);
319}
fa8d40ab 320
765dc015 321/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 322 set with "break" but falling in read-only memory.
765dc015
VP
323 If 0, gdb will warn about such breakpoints, but won't automatically
324 use hardware breakpoints. */
325static int automatic_hardware_breakpoints;
326static void
327show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
328 struct cmd_list_element *c,
329 const char *value)
330{
3e43a32a
MS
331 fprintf_filtered (file,
332 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
333 value);
334}
335
33e5cbd6
PA
336/* If on, gdb will keep breakpoints inserted even as inferior is
337 stopped, and immediately insert any new breakpoints. If off, gdb
338 will insert breakpoints into inferior only when resuming it, and
339 will remove breakpoints upon stop. If auto, GDB will behave as ON
340 if in non-stop mode, and as OFF if all-stop mode.*/
341
342static const char always_inserted_auto[] = "auto";
343static const char always_inserted_on[] = "on";
344static const char always_inserted_off[] = "off";
345static const char *always_inserted_enums[] = {
346 always_inserted_auto,
347 always_inserted_off,
348 always_inserted_on,
349 NULL
350};
351static const char *always_inserted_mode = always_inserted_auto;
352static void
74960c60 353show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 354 struct cmd_list_element *c, const char *value)
74960c60 355{
33e5cbd6 356 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
357 fprintf_filtered (file,
358 _("Always inserted breakpoint "
359 "mode is %s (currently %s).\n"),
33e5cbd6
PA
360 value,
361 breakpoints_always_inserted_mode () ? "on" : "off");
362 else
3e43a32a
MS
363 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
364 value);
74960c60
VP
365}
366
33e5cbd6
PA
367int
368breakpoints_always_inserted_mode (void)
369{
370 return (always_inserted_mode == always_inserted_on
371 || (always_inserted_mode == always_inserted_auto && non_stop));
372}
765dc015 373
a14ed312 374void _initialize_breakpoint (void);
c906108c 375
c906108c
SS
376/* Are we executing breakpoint commands? */
377static int executing_breakpoint_commands;
378
c02f5703
MS
379/* Are overlay event breakpoints enabled? */
380static int overlay_events_enabled;
381
e09342b5
TJB
382/* See description in breakpoint.h. */
383int target_exact_watchpoints = 0;
384
c906108c 385/* Walk the following statement or block through all breakpoints.
e5dd4106 386 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 387 current breakpoint. */
c906108c 388
5c44784c 389#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 390
5c44784c
JM
391#define ALL_BREAKPOINTS_SAFE(B,TMP) \
392 for (B = breakpoint_chain; \
393 B ? (TMP=B->next, 1): 0; \
394 B = TMP)
c906108c 395
4a64f543
MS
396/* Similar iterator for the low-level breakpoints. SAFE variant is
397 not provided so update_global_location_list must not be called
398 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 399
876fa593
JK
400#define ALL_BP_LOCATIONS(B,BP_TMP) \
401 for (BP_TMP = bp_location; \
402 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
403 BP_TMP++)
7cc221ef 404
1042e4c0
SS
405/* Iterator for tracepoints only. */
406
407#define ALL_TRACEPOINTS(B) \
408 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 409 if (is_tracepoint (B))
1042e4c0 410
7cc221ef 411/* Chains of all breakpoints defined. */
c906108c
SS
412
413struct breakpoint *breakpoint_chain;
414
876fa593
JK
415/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
416
417static struct bp_location **bp_location;
418
419/* Number of elements of BP_LOCATION. */
420
421static unsigned bp_location_count;
422
4a64f543
MS
423/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
424 ADDRESS for the current elements of BP_LOCATION which get a valid
425 result from bp_location_has_shadow. You can use it for roughly
426 limiting the subrange of BP_LOCATION to scan for shadow bytes for
427 an address you need to read. */
876fa593
JK
428
429static CORE_ADDR bp_location_placed_address_before_address_max;
430
4a64f543
MS
431/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
432 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
433 BP_LOCATION which get a valid result from bp_location_has_shadow.
434 You can use it for roughly limiting the subrange of BP_LOCATION to
435 scan for shadow bytes for an address you need to read. */
876fa593
JK
436
437static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 438
4a64f543
MS
439/* The locations that no longer correspond to any breakpoint, unlinked
440 from bp_location array, but for which a hit may still be reported
441 by a target. */
20874c92
VP
442VEC(bp_location_p) *moribund_locations = NULL;
443
c906108c
SS
444/* Number of last breakpoint made. */
445
95a42b64
TT
446static int breakpoint_count;
447
86b17b60
PA
448/* The value of `breakpoint_count' before the last command that
449 created breakpoints. If the last (break-like) command created more
450 than one breakpoint, then the difference between BREAKPOINT_COUNT
451 and PREV_BREAKPOINT_COUNT is more than one. */
452static int prev_breakpoint_count;
c906108c 453
1042e4c0
SS
454/* Number of last tracepoint made. */
455
95a42b64 456static int tracepoint_count;
1042e4c0 457
6149aea9
PA
458static struct cmd_list_element *breakpoint_set_cmdlist;
459static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 460struct cmd_list_element *save_cmdlist;
6149aea9 461
468d015d
JJ
462/* Return whether a breakpoint is an active enabled breakpoint. */
463static int
464breakpoint_enabled (struct breakpoint *b)
465{
0d381245 466 return (b->enable_state == bp_enabled);
468d015d
JJ
467}
468
c906108c
SS
469/* Set breakpoint count to NUM. */
470
95a42b64 471static void
fba45db2 472set_breakpoint_count (int num)
c906108c 473{
86b17b60 474 prev_breakpoint_count = breakpoint_count;
c906108c 475 breakpoint_count = num;
4fa62494 476 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
477}
478
86b17b60
PA
479/* Used by `start_rbreak_breakpoints' below, to record the current
480 breakpoint count before "rbreak" creates any breakpoint. */
481static int rbreak_start_breakpoint_count;
482
95a42b64
TT
483/* Called at the start an "rbreak" command to record the first
484 breakpoint made. */
86b17b60 485
95a42b64
TT
486void
487start_rbreak_breakpoints (void)
488{
86b17b60 489 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
490}
491
492/* Called at the end of an "rbreak" command to record the last
493 breakpoint made. */
86b17b60 494
95a42b64
TT
495void
496end_rbreak_breakpoints (void)
497{
86b17b60 498 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
499}
500
4a64f543 501/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
502
503void
fba45db2 504clear_breakpoint_hit_counts (void)
c906108c
SS
505{
506 struct breakpoint *b;
507
508 ALL_BREAKPOINTS (b)
509 b->hit_count = 0;
510}
511
9add0f1b
TT
512/* Allocate a new counted_command_line with reference count of 1.
513 The new structure owns COMMANDS. */
514
515static struct counted_command_line *
516alloc_counted_command_line (struct command_line *commands)
517{
518 struct counted_command_line *result
519 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 520
9add0f1b
TT
521 result->refc = 1;
522 result->commands = commands;
523 return result;
524}
525
526/* Increment reference count. This does nothing if CMD is NULL. */
527
528static void
529incref_counted_command_line (struct counted_command_line *cmd)
530{
531 if (cmd)
532 ++cmd->refc;
533}
534
535/* Decrement reference count. If the reference count reaches 0,
536 destroy the counted_command_line. Sets *CMDP to NULL. This does
537 nothing if *CMDP is NULL. */
538
539static void
540decref_counted_command_line (struct counted_command_line **cmdp)
541{
542 if (*cmdp)
543 {
544 if (--(*cmdp)->refc == 0)
545 {
546 free_command_lines (&(*cmdp)->commands);
547 xfree (*cmdp);
548 }
549 *cmdp = NULL;
550 }
551}
552
553/* A cleanup function that calls decref_counted_command_line. */
554
555static void
556do_cleanup_counted_command_line (void *arg)
557{
558 decref_counted_command_line (arg);
559}
560
561/* Create a cleanup that calls decref_counted_command_line on the
562 argument. */
563
564static struct cleanup *
565make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
566{
567 return make_cleanup (do_cleanup_counted_command_line, cmdp);
568}
569
c906108c
SS
570/* Default address, symtab and line to put a breakpoint at
571 for "break" command with no arg.
4a64f543 572 If default_breakpoint_valid is zero, the other three are
c906108c
SS
573 not valid, and "break" with no arg is an error.
574
575 This set by print_stack_frame, which calls set_default_breakpoint. */
576
577int default_breakpoint_valid;
578CORE_ADDR default_breakpoint_address;
579struct symtab *default_breakpoint_symtab;
580int default_breakpoint_line;
6c95b8df
PA
581struct program_space *default_breakpoint_pspace;
582
c906108c 583\f
48cb2d85
VP
584/* Return the breakpoint with the specified number, or NULL
585 if the number does not refer to an existing breakpoint. */
586
587struct breakpoint *
588get_breakpoint (int num)
589{
590 struct breakpoint *b;
591
592 ALL_BREAKPOINTS (b)
593 if (b->number == num)
594 return b;
595
596 return NULL;
597}
5c44784c 598
c906108c 599\f
adc36818
PM
600
601void
602set_breakpoint_condition (struct breakpoint *b, char *exp,
603 int from_tty)
604{
3a5c3e22
PA
605 xfree (b->cond_string);
606 b->cond_string = NULL;
adc36818 607
3a5c3e22 608 if (is_watchpoint (b))
adc36818 609 {
3a5c3e22
PA
610 struct watchpoint *w = (struct watchpoint *) b;
611
612 xfree (w->cond_exp);
613 w->cond_exp = NULL;
614 }
615 else
616 {
617 struct bp_location *loc;
618
619 for (loc = b->loc; loc; loc = loc->next)
620 {
621 xfree (loc->cond);
622 loc->cond = NULL;
623 }
adc36818 624 }
adc36818
PM
625
626 if (*exp == 0)
627 {
628 if (from_tty)
629 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
630 }
631 else
632 {
633 char *arg = exp;
cc59ec59 634
adc36818
PM
635 /* I don't know if it matters whether this is the string the user
636 typed in or the decompiled expression. */
637 b->cond_string = xstrdup (arg);
638 b->condition_not_parsed = 0;
639
640 if (is_watchpoint (b))
641 {
3a5c3e22
PA
642 struct watchpoint *w = (struct watchpoint *) b;
643
adc36818
PM
644 innermost_block = NULL;
645 arg = exp;
3a5c3e22 646 w->cond_exp = parse_exp_1 (&arg, 0, 0);
adc36818
PM
647 if (*arg)
648 error (_("Junk at end of expression"));
3a5c3e22 649 w->cond_exp_valid_block = innermost_block;
adc36818
PM
650 }
651 else
652 {
3a5c3e22
PA
653 struct bp_location *loc;
654
adc36818
PM
655 for (loc = b->loc; loc; loc = loc->next)
656 {
657 arg = exp;
658 loc->cond =
659 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
660 if (*arg)
661 error (_("Junk at end of expression"));
662 }
663 }
664 }
665 breakpoints_changed ();
8d3788bd 666 observer_notify_breakpoint_modified (b);
adc36818
PM
667}
668
c906108c
SS
669/* condition N EXP -- set break condition of breakpoint N to EXP. */
670
671static void
fba45db2 672condition_command (char *arg, int from_tty)
c906108c 673{
52f0bd74 674 struct breakpoint *b;
c906108c 675 char *p;
52f0bd74 676 int bnum;
c906108c
SS
677
678 if (arg == 0)
e2e0b3e5 679 error_no_arg (_("breakpoint number"));
c906108c
SS
680
681 p = arg;
682 bnum = get_number (&p);
5c44784c 683 if (bnum == 0)
8a3fe4f8 684 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
685
686 ALL_BREAKPOINTS (b)
687 if (b->number == bnum)
2f069f6f 688 {
7371cf6d
PM
689 /* Check if this breakpoint has a Python object assigned to
690 it, and if it has a definition of the "stop"
691 method. This method and conditions entered into GDB from
692 the CLI are mutually exclusive. */
693 if (b->py_bp_object
694 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
695 error (_("Cannot set a condition where a Python 'stop' "
696 "method has been defined in the breakpoint."));
2566ad2d 697 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
698 return;
699 }
c906108c 700
8a3fe4f8 701 error (_("No breakpoint number %d."), bnum);
c906108c
SS
702}
703
a7bdde9e
VP
704/* Check that COMMAND do not contain commands that are suitable
705 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
706 Throw if any such commands is found. */
707
a7bdde9e
VP
708static void
709check_no_tracepoint_commands (struct command_line *commands)
710{
711 struct command_line *c;
cc59ec59 712
a7bdde9e
VP
713 for (c = commands; c; c = c->next)
714 {
715 int i;
716
717 if (c->control_type == while_stepping_control)
3e43a32a
MS
718 error (_("The 'while-stepping' command can "
719 "only be used for tracepoints"));
a7bdde9e
VP
720
721 for (i = 0; i < c->body_count; ++i)
722 check_no_tracepoint_commands ((c->body_list)[i]);
723
724 /* Not that command parsing removes leading whitespace and comment
4a64f543 725 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
726 command directly. */
727 if (strstr (c->line, "collect ") == c->line)
728 error (_("The 'collect' command can only be used for tracepoints"));
729
51661e93
VP
730 if (strstr (c->line, "teval ") == c->line)
731 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
732 }
733}
734
d77f58be
SS
735/* Encapsulate tests for different types of tracepoints. */
736
d9b3f62e
PA
737static int
738is_tracepoint_type (enum bptype type)
739{
740 return (type == bp_tracepoint
741 || type == bp_fast_tracepoint
742 || type == bp_static_tracepoint);
743}
744
a7bdde9e 745int
d77f58be 746is_tracepoint (const struct breakpoint *b)
a7bdde9e 747{
d9b3f62e 748 return is_tracepoint_type (b->type);
a7bdde9e 749}
d9b3f62e 750
e5dd4106 751/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
752 breakpoint. This function will throw an exception if a problem is
753 found. */
48cb2d85 754
95a42b64
TT
755static void
756validate_commands_for_breakpoint (struct breakpoint *b,
757 struct command_line *commands)
48cb2d85 758{
d77f58be 759 if (is_tracepoint (b))
a7bdde9e 760 {
4a64f543
MS
761 /* We need to verify that each top-level element of commands is
762 valid for tracepoints, that there's at most one
763 while-stepping element, and that while-stepping's body has
764 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
765 struct command_line *c;
766 struct command_line *while_stepping = 0;
767 for (c = commands; c; c = c->next)
768 {
a7bdde9e
VP
769 if (c->control_type == while_stepping_control)
770 {
771 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
772 error (_("The 'while-stepping' command "
773 "cannot be used for fast tracepoint"));
0fb4aa4b 774 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
775 error (_("The 'while-stepping' command "
776 "cannot be used for static tracepoint"));
a7bdde9e
VP
777
778 if (while_stepping)
3e43a32a
MS
779 error (_("The 'while-stepping' command "
780 "can be used only once"));
a7bdde9e
VP
781 else
782 while_stepping = c;
783 }
784 }
785 if (while_stepping)
786 {
787 struct command_line *c2;
788
789 gdb_assert (while_stepping->body_count == 1);
790 c2 = while_stepping->body_list[0];
791 for (; c2; c2 = c2->next)
792 {
a7bdde9e
VP
793 if (c2->control_type == while_stepping_control)
794 error (_("The 'while-stepping' command cannot be nested"));
795 }
796 }
797 }
798 else
799 {
800 check_no_tracepoint_commands (commands);
801 }
95a42b64
TT
802}
803
0fb4aa4b
PA
804/* Return a vector of all the static tracepoints set at ADDR. The
805 caller is responsible for releasing the vector. */
806
807VEC(breakpoint_p) *
808static_tracepoints_here (CORE_ADDR addr)
809{
810 struct breakpoint *b;
811 VEC(breakpoint_p) *found = 0;
812 struct bp_location *loc;
813
814 ALL_BREAKPOINTS (b)
815 if (b->type == bp_static_tracepoint)
816 {
817 for (loc = b->loc; loc; loc = loc->next)
818 if (loc->address == addr)
819 VEC_safe_push(breakpoint_p, found, b);
820 }
821
822 return found;
823}
824
95a42b64 825/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 826 validate that only allowed commands are included. */
95a42b64
TT
827
828void
4a64f543
MS
829breakpoint_set_commands (struct breakpoint *b,
830 struct command_line *commands)
95a42b64
TT
831{
832 validate_commands_for_breakpoint (b, commands);
a7bdde9e 833
9add0f1b
TT
834 decref_counted_command_line (&b->commands);
835 b->commands = alloc_counted_command_line (commands);
48cb2d85 836 breakpoints_changed ();
8d3788bd 837 observer_notify_breakpoint_modified (b);
48cb2d85
VP
838}
839
45a43567
TT
840/* Set the internal `silent' flag on the breakpoint. Note that this
841 is not the same as the "silent" that may appear in the breakpoint's
842 commands. */
843
844void
845breakpoint_set_silent (struct breakpoint *b, int silent)
846{
847 int old_silent = b->silent;
848
849 b->silent = silent;
850 if (old_silent != silent)
8d3788bd 851 observer_notify_breakpoint_modified (b);
45a43567
TT
852}
853
854/* Set the thread for this breakpoint. If THREAD is -1, make the
855 breakpoint work for any thread. */
856
857void
858breakpoint_set_thread (struct breakpoint *b, int thread)
859{
860 int old_thread = b->thread;
861
862 b->thread = thread;
863 if (old_thread != thread)
8d3788bd 864 observer_notify_breakpoint_modified (b);
45a43567
TT
865}
866
867/* Set the task for this breakpoint. If TASK is 0, make the
868 breakpoint work for any task. */
869
870void
871breakpoint_set_task (struct breakpoint *b, int task)
872{
873 int old_task = b->task;
874
875 b->task = task;
876 if (old_task != task)
8d3788bd 877 observer_notify_breakpoint_modified (b);
45a43567
TT
878}
879
95a42b64
TT
880void
881check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
882{
883 struct breakpoint *b = closure;
cc59ec59 884
a7bdde9e
VP
885 validate_actionline (&line, b);
886}
887
95a42b64
TT
888/* A structure used to pass information through
889 map_breakpoint_numbers. */
890
891struct commands_info
892{
893 /* True if the command was typed at a tty. */
894 int from_tty;
86b17b60
PA
895
896 /* The breakpoint range spec. */
897 char *arg;
898
95a42b64
TT
899 /* Non-NULL if the body of the commands are being read from this
900 already-parsed command. */
901 struct command_line *control;
86b17b60 902
95a42b64
TT
903 /* The command lines read from the user, or NULL if they have not
904 yet been read. */
905 struct counted_command_line *cmd;
906};
907
908/* A callback for map_breakpoint_numbers that sets the commands for
909 commands_command. */
910
c906108c 911static void
95a42b64 912do_map_commands_command (struct breakpoint *b, void *data)
c906108c 913{
95a42b64 914 struct commands_info *info = data;
c906108c 915
95a42b64
TT
916 if (info->cmd == NULL)
917 {
918 struct command_line *l;
5c44784c 919
95a42b64
TT
920 if (info->control != NULL)
921 l = copy_command_lines (info->control->body_list[0]);
922 else
86b17b60
PA
923 {
924 struct cleanup *old_chain;
925 char *str;
c5aa993b 926
3e43a32a
MS
927 str = xstrprintf (_("Type commands for breakpoint(s) "
928 "%s, one per line."),
86b17b60
PA
929 info->arg);
930
931 old_chain = make_cleanup (xfree, str);
932
933 l = read_command_lines (str,
934 info->from_tty, 1,
d77f58be 935 (is_tracepoint (b)
86b17b60
PA
936 ? check_tracepoint_command : 0),
937 b);
938
939 do_cleanups (old_chain);
940 }
a7bdde9e 941
95a42b64
TT
942 info->cmd = alloc_counted_command_line (l);
943 }
944
945 /* If a breakpoint was on the list more than once, we don't need to
946 do anything. */
947 if (b->commands != info->cmd)
948 {
949 validate_commands_for_breakpoint (b, info->cmd->commands);
950 incref_counted_command_line (info->cmd);
951 decref_counted_command_line (&b->commands);
952 b->commands = info->cmd;
953 breakpoints_changed ();
8d3788bd 954 observer_notify_breakpoint_modified (b);
c5aa993b 955 }
95a42b64
TT
956}
957
958static void
4a64f543
MS
959commands_command_1 (char *arg, int from_tty,
960 struct command_line *control)
95a42b64
TT
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 974 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
975 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
976 breakpoint_count);
95a42b64
TT
977 else if (breakpoint_count > 0)
978 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
979 else
980 {
981 /* So that we don't try to free the incoming non-NULL
982 argument in the cleanup below. Mapping breakpoint
983 numbers will fail in this case. */
984 arg = NULL;
985 }
95a42b64 986 }
9766ced4
SS
987 else
988 /* The command loop has some static state, so we need to preserve
989 our argument. */
990 arg = xstrdup (arg);
86b17b60
PA
991
992 if (arg != NULL)
993 make_cleanup (xfree, arg);
994
995 info.arg = arg;
95a42b64
TT
996
997 map_breakpoint_numbers (arg, do_map_commands_command, &info);
998
999 if (info.cmd == NULL)
1000 error (_("No breakpoints specified."));
1001
1002 do_cleanups (cleanups);
1003}
1004
1005static void
1006commands_command (char *arg, int from_tty)
1007{
1008 commands_command_1 (arg, from_tty, NULL);
c906108c 1009}
40c03ae8
EZ
1010
1011/* Like commands_command, but instead of reading the commands from
1012 input stream, takes them from an already parsed command structure.
1013
1014 This is used by cli-script.c to DTRT with breakpoint commands
1015 that are part of if and while bodies. */
1016enum command_control_type
1017commands_from_control_command (char *arg, struct command_line *cmd)
1018{
95a42b64
TT
1019 commands_command_1 (arg, 0, cmd);
1020 return simple_control;
40c03ae8 1021}
876fa593
JK
1022
1023/* Return non-zero if BL->TARGET_INFO contains valid information. */
1024
1025static int
1026bp_location_has_shadow (struct bp_location *bl)
1027{
1028 if (bl->loc_type != bp_loc_software_breakpoint)
1029 return 0;
1030 if (!bl->inserted)
1031 return 0;
1032 if (bl->target_info.shadow_len == 0)
e5dd4106 1033 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1034 return 0;
1035 return 1;
1036}
1037
8defab1a 1038/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1039 by replacing any memory breakpoints with their shadowed contents.
1040
1041 The range of shadowed area by each bp_location is:
35df4500
TJB
1042 bl->address - bp_location_placed_address_before_address_max
1043 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1044 The range we were requested to resolve shadows for is:
1045 memaddr ... memaddr + len
1046 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1047 memaddr + len <= (bl->address
1048 - bp_location_placed_address_before_address_max)
876fa593 1049 and:
35df4500 1050 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1051
8defab1a
DJ
1052void
1053breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1054{
4a64f543
MS
1055 /* Left boundary, right boundary and median element of our binary
1056 search. */
876fa593
JK
1057 unsigned bc_l, bc_r, bc;
1058
4a64f543
MS
1059 /* Find BC_L which is a leftmost element which may affect BUF
1060 content. It is safe to report lower value but a failure to
1061 report higher one. */
876fa593
JK
1062
1063 bc_l = 0;
1064 bc_r = bp_location_count;
1065 while (bc_l + 1 < bc_r)
1066 {
35df4500 1067 struct bp_location *bl;
876fa593
JK
1068
1069 bc = (bc_l + bc_r) / 2;
35df4500 1070 bl = bp_location[bc];
876fa593 1071
4a64f543
MS
1072 /* Check first BL->ADDRESS will not overflow due to the added
1073 constant. Then advance the left boundary only if we are sure
1074 the BC element can in no way affect the BUF content (MEMADDR
1075 to MEMADDR + LEN range).
876fa593 1076
4a64f543
MS
1077 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1078 offset so that we cannot miss a breakpoint with its shadow
1079 range tail still reaching MEMADDR. */
c5aa993b 1080
35df4500
TJB
1081 if ((bl->address + bp_location_shadow_len_after_address_max
1082 >= bl->address)
1083 && (bl->address + bp_location_shadow_len_after_address_max
1084 <= memaddr))
876fa593
JK
1085 bc_l = bc;
1086 else
1087 bc_r = bc;
1088 }
1089
128070bb
PA
1090 /* Due to the binary search above, we need to make sure we pick the
1091 first location that's at BC_L's address. E.g., if there are
1092 multiple locations at the same address, BC_L may end up pointing
1093 at a duplicate location, and miss the "master"/"inserted"
1094 location. Say, given locations L1, L2 and L3 at addresses A and
1095 B:
1096
1097 L1@A, L2@A, L3@B, ...
1098
1099 BC_L could end up pointing at location L2, while the "master"
1100 location could be L1. Since the `loc->inserted' flag is only set
1101 on "master" locations, we'd forget to restore the shadow of L1
1102 and L2. */
1103 while (bc_l > 0
1104 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1105 bc_l--;
1106
876fa593
JK
1107 /* Now do full processing of the found relevant range of elements. */
1108
1109 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1110 {
35df4500 1111 struct bp_location *bl = bp_location[bc];
876fa593
JK
1112 CORE_ADDR bp_addr = 0;
1113 int bp_size = 0;
1114 int bptoffset = 0;
1115
35df4500
TJB
1116 /* bp_location array has BL->OWNER always non-NULL. */
1117 if (bl->owner->type == bp_none)
8a3fe4f8 1118 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1119 bl->owner->number);
ffce0d52 1120
e5dd4106 1121 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1122 content. */
1123
35df4500
TJB
1124 if (bl->address >= bp_location_placed_address_before_address_max
1125 && memaddr + len <= (bl->address
1126 - bp_location_placed_address_before_address_max))
876fa593
JK
1127 break;
1128
35df4500 1129 if (!bp_location_has_shadow (bl))
c5aa993b 1130 continue;
35df4500 1131 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1132 current_program_space->aspace, 0))
1133 continue;
1134
c5aa993b
JM
1135 /* Addresses and length of the part of the breakpoint that
1136 we need to copy. */
35df4500
TJB
1137 bp_addr = bl->target_info.placed_address;
1138 bp_size = bl->target_info.shadow_len;
8defab1a 1139
c5aa993b
JM
1140 if (bp_addr + bp_size <= memaddr)
1141 /* The breakpoint is entirely before the chunk of memory we
1142 are reading. */
1143 continue;
8defab1a 1144
c5aa993b
JM
1145 if (bp_addr >= memaddr + len)
1146 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1147 reading. */
c5aa993b 1148 continue;
c5aa993b 1149
8defab1a
DJ
1150 /* Offset within shadow_contents. */
1151 if (bp_addr < memaddr)
1152 {
1153 /* Only copy the second part of the breakpoint. */
1154 bp_size -= memaddr - bp_addr;
1155 bptoffset = memaddr - bp_addr;
1156 bp_addr = memaddr;
1157 }
c5aa993b 1158
8defab1a
DJ
1159 if (bp_addr + bp_size > memaddr + len)
1160 {
1161 /* Only copy the first part of the breakpoint. */
1162 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1163 }
c5aa993b 1164
8defab1a 1165 memcpy (buf + bp_addr - memaddr,
35df4500 1166 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1167 }
c906108c 1168}
c906108c 1169\f
c5aa993b 1170
60e1c644
PA
1171/* Return true if BPT is of any hardware watchpoint kind. */
1172
a5606eee 1173static int
d77f58be 1174is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1175{
1176 return (bpt->type == bp_hardware_watchpoint
1177 || bpt->type == bp_read_watchpoint
1178 || bpt->type == bp_access_watchpoint);
1179}
7270d8f2 1180
60e1c644
PA
1181/* Return true if BPT is of any watchpoint kind, hardware or
1182 software. */
1183
3a5c3e22 1184int
d77f58be 1185is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1186{
1187 return (is_hardware_watchpoint (bpt)
1188 || bpt->type == bp_watchpoint);
1189}
1190
3a5c3e22
PA
1191/* Returns true if the current thread and its running state are safe
1192 to evaluate or update watchpoint B. Watchpoints on local
1193 expressions need to be evaluated in the context of the thread that
1194 was current when the watchpoint was created, and, that thread needs
1195 to be stopped to be able to select the correct frame context.
1196 Watchpoints on global expressions can be evaluated on any thread,
1197 and in any state. It is presently left to the target allowing
1198 memory accesses when threads are running. */
f6bc2008
PA
1199
1200static int
3a5c3e22 1201watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008
PA
1202{
1203 return (ptid_equal (b->watchpoint_thread, null_ptid)
1204 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1205 && !is_executing (inferior_ptid)));
1206}
1207
d0fb5eae
JK
1208/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1209 associated bp_watchpoint_scope breakpoint. */
1210
1211static void
3a5c3e22 1212watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1213{
3a5c3e22 1214 struct breakpoint *b = &w->base;
d0fb5eae
JK
1215
1216 if (b->related_breakpoint != b)
1217 {
1218 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1219 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1220 b->related_breakpoint->disposition = disp_del_at_next_stop;
1221 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1222 b->related_breakpoint = b;
1223 }
1224 b->disposition = disp_del_at_next_stop;
1225}
1226
567e1b4e
JB
1227/* Assuming that B is a watchpoint:
1228 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1229 - Evaluate expression and store the result in B->val
567e1b4e
JB
1230 - Evaluate the condition if there is one, and store the result
1231 in b->loc->cond.
a5606eee
VP
1232 - Update the list of values that must be watched in B->loc.
1233
4a64f543
MS
1234 If the watchpoint disposition is disp_del_at_next_stop, then do
1235 nothing. If this is local watchpoint that is out of scope, delete
1236 it.
1237
1238 Even with `set breakpoint always-inserted on' the watchpoints are
1239 removed + inserted on each stop here. Normal breakpoints must
1240 never be removed because they might be missed by a running thread
1241 when debugging in non-stop mode. On the other hand, hardware
1242 watchpoints (is_hardware_watchpoint; processed here) are specific
1243 to each LWP since they are stored in each LWP's hardware debug
1244 registers. Therefore, such LWP must be stopped first in order to
1245 be able to modify its hardware watchpoints.
1246
1247 Hardware watchpoints must be reset exactly once after being
1248 presented to the user. It cannot be done sooner, because it would
1249 reset the data used to present the watchpoint hit to the user. And
1250 it must not be done later because it could display the same single
1251 watchpoint hit during multiple GDB stops. Note that the latter is
1252 relevant only to the hardware watchpoint types bp_read_watchpoint
1253 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1254 not user-visible - its hit is suppressed if the memory content has
1255 not changed.
1256
1257 The following constraints influence the location where we can reset
1258 hardware watchpoints:
1259
1260 * target_stopped_by_watchpoint and target_stopped_data_address are
1261 called several times when GDB stops.
1262
1263 [linux]
1264 * Multiple hardware watchpoints can be hit at the same time,
1265 causing GDB to stop. GDB only presents one hardware watchpoint
1266 hit at a time as the reason for stopping, and all the other hits
1267 are presented later, one after the other, each time the user
1268 requests the execution to be resumed. Execution is not resumed
1269 for the threads still having pending hit event stored in
1270 LWP_INFO->STATUS. While the watchpoint is already removed from
1271 the inferior on the first stop the thread hit event is kept being
1272 reported from its cached value by linux_nat_stopped_data_address
1273 until the real thread resume happens after the watchpoint gets
1274 presented and thus its LWP_INFO->STATUS gets reset.
1275
1276 Therefore the hardware watchpoint hit can get safely reset on the
1277 watchpoint removal from inferior. */
a79d3c27 1278
b40ce68a 1279static void
3a5c3e22 1280update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1281{
a5606eee 1282 int within_current_scope;
a5606eee 1283 struct frame_id saved_frame_id;
66076460 1284 int frame_saved;
a5606eee 1285
f6bc2008
PA
1286 /* If this is a local watchpoint, we only want to check if the
1287 watchpoint frame is in scope if the current thread is the thread
1288 that was used to create the watchpoint. */
1289 if (!watchpoint_in_thread_scope (b))
1290 return;
1291
3a5c3e22 1292 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1293 return;
1294
66076460 1295 frame_saved = 0;
a5606eee
VP
1296
1297 /* Determine if the watchpoint is within scope. */
1298 if (b->exp_valid_block == NULL)
1299 within_current_scope = 1;
1300 else
1301 {
b5db5dfc
UW
1302 struct frame_info *fi = get_current_frame ();
1303 struct gdbarch *frame_arch = get_frame_arch (fi);
1304 CORE_ADDR frame_pc = get_frame_pc (fi);
1305
1306 /* If we're in a function epilogue, unwinding may not work
1307 properly, so do not attempt to recreate locations at this
1308 point. See similar comments in watchpoint_check. */
1309 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1310 return;
66076460
DJ
1311
1312 /* Save the current frame's ID so we can restore it after
1313 evaluating the watchpoint expression on its own frame. */
1314 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1315 took a frame parameter, so that we didn't have to change the
1316 selected frame. */
1317 frame_saved = 1;
1318 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1319
a5606eee
VP
1320 fi = frame_find_by_id (b->watchpoint_frame);
1321 within_current_scope = (fi != NULL);
1322 if (within_current_scope)
1323 select_frame (fi);
1324 }
1325
b5db5dfc
UW
1326 /* We don't free locations. They are stored in the bp_location array
1327 and update_global_location_list will eventually delete them and
1328 remove breakpoints if needed. */
3a5c3e22 1329 b->base.loc = NULL;
b5db5dfc 1330
a5606eee
VP
1331 if (within_current_scope && reparse)
1332 {
1333 char *s;
d63d0675 1334
a5606eee
VP
1335 if (b->exp)
1336 {
1337 xfree (b->exp);
1338 b->exp = NULL;
1339 }
d63d0675 1340 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1341 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1342 /* If the meaning of expression itself changed, the old value is
1343 no longer relevant. We don't want to report a watchpoint hit
1344 to the user when the old value and the new value may actually
1345 be completely different objects. */
1346 value_free (b->val);
fa4727a6
DJ
1347 b->val = NULL;
1348 b->val_valid = 0;
60e1c644
PA
1349
1350 /* Note that unlike with breakpoints, the watchpoint's condition
1351 expression is stored in the breakpoint object, not in the
1352 locations (re)created below. */
3a5c3e22 1353 if (b->base.cond_string != NULL)
60e1c644
PA
1354 {
1355 if (b->cond_exp != NULL)
1356 {
1357 xfree (b->cond_exp);
1358 b->cond_exp = NULL;
1359 }
1360
3a5c3e22 1361 s = b->base.cond_string;
60e1c644
PA
1362 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1363 }
a5606eee 1364 }
a5606eee
VP
1365
1366 /* If we failed to parse the expression, for example because
1367 it refers to a global variable in a not-yet-loaded shared library,
1368 don't try to insert watchpoint. We don't automatically delete
1369 such watchpoint, though, since failure to parse expression
1370 is different from out-of-scope watchpoint. */
2d134ed3
PA
1371 if ( !target_has_execution)
1372 {
1373 /* Without execution, memory can't change. No use to try and
1374 set watchpoint locations. The watchpoint will be reset when
1375 the target gains execution, through breakpoint_re_set. */
1376 }
1377 else if (within_current_scope && b->exp)
a5606eee 1378 {
0cf6dd15 1379 int pc = 0;
fa4727a6 1380 struct value *val_chain, *v, *result, *next;
2d134ed3 1381 struct program_space *frame_pspace;
a5606eee 1382
0cf6dd15 1383 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1384
a5606eee
VP
1385 /* Avoid setting b->val if it's already set. The meaning of
1386 b->val is 'the last value' user saw, and we should update
1387 it only if we reported that last value to user. As it
9c06b0b4
TJB
1388 happens, the code that reports it updates b->val directly.
1389 We don't keep track of the memory value for masked
1390 watchpoints. */
3a5c3e22 1391 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1392 {
1393 b->val = v;
1394 b->val_valid = 1;
1395 }
a5606eee 1396
2d134ed3
PA
1397 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1398
a5606eee 1399 /* Look at each value on the value chain. */
9fa40276 1400 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1401 {
1402 /* If it's a memory location, and GDB actually needed
1403 its contents to evaluate the expression, then we
fa4727a6
DJ
1404 must watch it. If the first value returned is
1405 still lazy, that means an error occurred reading it;
1406 watch it anyway in case it becomes readable. */
a5606eee 1407 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1408 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1409 {
1410 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1411
a5606eee
VP
1412 /* We only watch structs and arrays if user asked
1413 for it explicitly, never if they just happen to
1414 appear in the middle of some value chain. */
fa4727a6 1415 if (v == result
a5606eee
VP
1416 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1417 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1418 {
1419 CORE_ADDR addr;
1420 int len, type;
1421 struct bp_location *loc, **tmp;
1422
42ae5230 1423 addr = value_address (v);
a5606eee
VP
1424 len = TYPE_LENGTH (value_type (v));
1425 type = hw_write;
3a5c3e22 1426 if (b->base.type == bp_read_watchpoint)
a5606eee 1427 type = hw_read;
3a5c3e22 1428 else if (b->base.type == bp_access_watchpoint)
a5606eee 1429 type = hw_access;
3a5c3e22
PA
1430
1431 loc = allocate_bp_location (&b->base);
1432 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1433 ;
1434 *tmp = loc;
a6d9a66e 1435 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1436
1437 loc->pspace = frame_pspace;
a5606eee
VP
1438 loc->address = addr;
1439 loc->length = len;
1440 loc->watchpoint_type = type;
1441 }
1442 }
9fa40276
TJB
1443 }
1444
1445 /* Change the type of breakpoint between hardware assisted or
1446 an ordinary watchpoint depending on the hardware support
1447 and free hardware slots. REPARSE is set when the inferior
1448 is started. */
a9634178 1449 if (reparse)
9fa40276 1450 {
e09342b5 1451 int reg_cnt;
9fa40276
TJB
1452 enum bp_loc_type loc_type;
1453 struct bp_location *bl;
a5606eee 1454
a9634178 1455 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1456
1457 if (reg_cnt)
9fa40276
TJB
1458 {
1459 int i, target_resources_ok, other_type_used;
1460
a9634178
TJB
1461 /* Use an exact watchpoint when there's only one memory region to be
1462 watched, and only one debug register is needed to watch it. */
1463 b->exact = target_exact_watchpoints && reg_cnt == 1;
1464
9fa40276 1465 /* We need to determine how many resources are already
e09342b5
TJB
1466 used for all other hardware watchpoints plus this one
1467 to see if we still have enough resources to also fit
1468 this watchpoint in as well. To guarantee the
1469 hw_watchpoint_used_count call below counts this
1470 watchpoint, make sure that it is marked as a hardware
1471 watchpoint. */
3a5c3e22
PA
1472 if (b->base.type == bp_watchpoint)
1473 b->base.type = bp_hardware_watchpoint;
9fa40276 1474
3a5c3e22 1475 i = hw_watchpoint_used_count (b->base.type, &other_type_used);
e09342b5 1476 target_resources_ok = target_can_use_hardware_watchpoint
3a5c3e22 1477 (b->base.type, i, other_type_used);
e09342b5 1478 if (target_resources_ok <= 0)
a9634178 1479 {
3a5c3e22 1480 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1481
1482 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1483 error (_("Target does not support this type of "
1484 "hardware watchpoint."));
9c06b0b4
TJB
1485 else if (target_resources_ok < 0 && !sw_mode)
1486 error (_("There are not enough available hardware "
1487 "resources for this watchpoint."));
a9634178 1488 else
3a5c3e22 1489 b->base.type = bp_watchpoint;
a9634178 1490 }
9fa40276 1491 }
3a5c3e22 1492 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1493 error (_("Expression cannot be implemented with "
1494 "read/access watchpoint."));
9fa40276 1495 else
3a5c3e22 1496 b->base.type = bp_watchpoint;
9fa40276 1497
3a5c3e22 1498 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1499 : bp_loc_hardware_watchpoint);
3a5c3e22 1500 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1501 bl->loc_type = loc_type;
1502 }
1503
1504 for (v = val_chain; v; v = next)
1505 {
a5606eee
VP
1506 next = value_next (v);
1507 if (v != b->val)
1508 value_free (v);
1509 }
1510
c7437ca6
PA
1511 /* If a software watchpoint is not watching any memory, then the
1512 above left it without any location set up. But,
1513 bpstat_stop_status requires a location to be able to report
1514 stops, so make sure there's at least a dummy one. */
3a5c3e22 1515 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1516 {
3a5c3e22
PA
1517 struct breakpoint *base = &b->base;
1518 base->loc = allocate_bp_location (base);
1519 base->loc->pspace = frame_pspace;
1520 base->loc->address = -1;
1521 base->loc->length = -1;
1522 base->loc->watchpoint_type = -1;
c7437ca6 1523 }
a5606eee
VP
1524 }
1525 else if (!within_current_scope)
7270d8f2 1526 {
ac74f770
MS
1527 printf_filtered (_("\
1528Watchpoint %d deleted because the program has left the block\n\
1529in which its expression is valid.\n"),
3a5c3e22 1530 b->base.number);
d0fb5eae 1531 watchpoint_del_at_next_stop (b);
7270d8f2 1532 }
a5606eee
VP
1533
1534 /* Restore the selected frame. */
66076460
DJ
1535 if (frame_saved)
1536 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1537}
1538
a5606eee 1539
74960c60
VP
1540/* Returns 1 iff breakpoint location should be
1541 inserted in the inferior. */
1542static int
35df4500 1543should_be_inserted (struct bp_location *bl)
74960c60 1544{
35df4500 1545 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1546 return 0;
1547
35df4500 1548 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1549 return 0;
1550
35df4500 1551 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1552 return 0;
1553
56710373
PA
1554 /* This is set for example, when we're attached to the parent of a
1555 vfork, and have detached from the child. The child is running
1556 free, and we expect it to do an exec or exit, at which point the
1557 OS makes the parent schedulable again (and the target reports
1558 that the vfork is done). Until the child is done with the shared
1559 memory region, do not insert breakpoints in the parent, otherwise
1560 the child could still trip on the parent's breakpoints. Since
1561 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1562 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1563 return 0;
1564
1042e4c0
SS
1565 /* Tracepoints are inserted by the target at a time of its choosing,
1566 not by us. */
35df4500 1567 if (is_tracepoint (bl->owner))
1042e4c0
SS
1568 return 0;
1569
74960c60
VP
1570 return 1;
1571}
1572
35df4500
TJB
1573/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1574 location. Any error messages are printed to TMP_ERROR_STREAM; and
1575 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
879bfdc2 1576
4a64f543
MS
1577 NOTE drow/2003-09-09: This routine could be broken down to an
1578 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1579static int
35df4500 1580insert_bp_location (struct bp_location *bl,
26bb91f3 1581 struct ui_file *tmp_error_stream,
fa3a767f 1582 int *disabled_breaks,
26bb91f3 1583 int *hw_breakpoint_error)
879bfdc2
DJ
1584{
1585 int val = 0;
1586
35df4500 1587 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1588 return 0;
1589
8181d85f 1590 /* Initialize the target-specific information. */
35df4500
TJB
1591 memset (&bl->target_info, 0, sizeof (bl->target_info));
1592 bl->target_info.placed_address = bl->address;
1593 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1594 bl->target_info.length = bl->length;
8181d85f 1595
35df4500
TJB
1596 if (bl->loc_type == bp_loc_software_breakpoint
1597 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1598 {
35df4500 1599 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1600 {
1601 /* If the explicitly specified breakpoint type
1602 is not hardware breakpoint, check the memory map to see
1603 if the breakpoint address is in read only memory or not.
4a64f543 1604
765dc015
VP
1605 Two important cases are:
1606 - location type is not hardware breakpoint, memory
1607 is readonly. We change the type of the location to
1608 hardware breakpoint.
4a64f543
MS
1609 - location type is hardware breakpoint, memory is
1610 read-write. This means we've previously made the
1611 location hardware one, but then the memory map changed,
1612 so we undo.
765dc015 1613
4a64f543
MS
1614 When breakpoints are removed, remove_breakpoints will use
1615 location types we've just set here, the only possible
1616 problem is that memory map has changed during running
1617 program, but it's not going to work anyway with current
1618 gdb. */
765dc015 1619 struct mem_region *mr
35df4500 1620 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1621
1622 if (mr)
1623 {
1624 if (automatic_hardware_breakpoints)
1625 {
765dc015
VP
1626 enum bp_loc_type new_type;
1627
1628 if (mr->attrib.mode != MEM_RW)
1629 new_type = bp_loc_hardware_breakpoint;
1630 else
1631 new_type = bp_loc_software_breakpoint;
1632
35df4500 1633 if (new_type != bl->loc_type)
765dc015
VP
1634 {
1635 static int said = 0;
cc59ec59 1636
35df4500 1637 bl->loc_type = new_type;
765dc015
VP
1638 if (!said)
1639 {
3e43a32a
MS
1640 fprintf_filtered (gdb_stdout,
1641 _("Note: automatically using "
1642 "hardware breakpoints for "
1643 "read-only addresses.\n"));
765dc015
VP
1644 said = 1;
1645 }
1646 }
1647 }
35df4500 1648 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1649 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1650 warning (_("cannot set software breakpoint "
1651 "at readonly address %s"),
35df4500 1652 paddress (bl->gdbarch, bl->address));
765dc015
VP
1653 }
1654 }
1655
879bfdc2
DJ
1656 /* First check to see if we have to handle an overlay. */
1657 if (overlay_debugging == ovly_off
35df4500
TJB
1658 || bl->section == NULL
1659 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1660 {
1661 /* No overlay handling: just set the breakpoint. */
1662
348d480f 1663 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1664 }
1665 else
1666 {
4a64f543 1667 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1668 Shall we set a breakpoint at the LMA? */
1669 if (!overlay_events_enabled)
1670 {
1671 /* Yes -- overlay event support is not active,
1672 so we must try to set a breakpoint at the LMA.
1673 This will not work for a hardware breakpoint. */
35df4500 1674 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1675 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1676 bl->owner->number);
879bfdc2
DJ
1677 else
1678 {
35df4500
TJB
1679 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1680 bl->section);
879bfdc2 1681 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1682 bl->overlay_target_info = bl->target_info;
1683 bl->overlay_target_info.placed_address = addr;
1684 val = target_insert_breakpoint (bl->gdbarch,
1685 &bl->overlay_target_info);
879bfdc2 1686 if (val != 0)
99361f52 1687 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1688 "Overlay breakpoint %d "
1689 "failed: in ROM?\n",
35df4500 1690 bl->owner->number);
879bfdc2
DJ
1691 }
1692 }
1693 /* Shall we set a breakpoint at the VMA? */
35df4500 1694 if (section_is_mapped (bl->section))
879bfdc2
DJ
1695 {
1696 /* Yes. This overlay section is mapped into memory. */
348d480f 1697 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1698 }
1699 else
1700 {
1701 /* No. This breakpoint will not be inserted.
1702 No error, but do not mark the bp as 'inserted'. */
1703 return 0;
1704 }
1705 }
1706
1707 if (val)
1708 {
1709 /* Can't set the breakpoint. */
35df4500 1710 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1711 {
4a64f543 1712 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1713 val = 0;
35df4500 1714 bl->shlib_disabled = 1;
8d3788bd 1715 observer_notify_breakpoint_modified (bl->owner);
879bfdc2
DJ
1716 if (!*disabled_breaks)
1717 {
1718 fprintf_unfiltered (tmp_error_stream,
1719 "Cannot insert breakpoint %d.\n",
35df4500 1720 bl->owner->number);
879bfdc2 1721 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1722 "Temporarily disabling shared "
1723 "library breakpoints:\n");
879bfdc2
DJ
1724 }
1725 *disabled_breaks = 1;
1726 fprintf_unfiltered (tmp_error_stream,
35df4500 1727 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1728 }
1729 else
879bfdc2 1730 {
35df4500 1731 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1732 {
1733 *hw_breakpoint_error = 1;
3e43a32a
MS
1734 fprintf_unfiltered (tmp_error_stream,
1735 "Cannot insert hardware "
1736 "breakpoint %d.\n",
35df4500 1737 bl->owner->number);
879bfdc2
DJ
1738 }
1739 else
1740 {
1741 fprintf_unfiltered (tmp_error_stream,
1742 "Cannot insert breakpoint %d.\n",
35df4500 1743 bl->owner->number);
879bfdc2
DJ
1744 fprintf_filtered (tmp_error_stream,
1745 "Error accessing memory address ");
35df4500 1746 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1747 tmp_error_stream);
879bfdc2
DJ
1748 fprintf_filtered (tmp_error_stream, ": %s.\n",
1749 safe_strerror (val));
1750 }
1751
1752 }
1753 }
1754 else
35df4500 1755 bl->inserted = 1;
879bfdc2
DJ
1756
1757 return val;
1758 }
1759
35df4500 1760 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1761 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1762 watchpoints. It's not clear that it's necessary... */
35df4500 1763 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1764 {
77b06cd7
TJB
1765 gdb_assert (bl->owner->ops != NULL
1766 && bl->owner->ops->insert_location != NULL);
1767
1768 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1769
1770 /* If trying to set a read-watchpoint, and it turns out it's not
1771 supported, try emulating one with an access watchpoint. */
35df4500 1772 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1773 {
1774 struct bp_location *loc, **loc_temp;
1775
1776 /* But don't try to insert it, if there's already another
1777 hw_access location that would be considered a duplicate
1778 of this one. */
1779 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1780 if (loc != bl
85d721b8 1781 && loc->watchpoint_type == hw_access
35df4500 1782 && watchpoint_locations_match (bl, loc))
85d721b8 1783 {
35df4500
TJB
1784 bl->duplicate = 1;
1785 bl->inserted = 1;
1786 bl->target_info = loc->target_info;
1787 bl->watchpoint_type = hw_access;
85d721b8
PA
1788 val = 0;
1789 break;
1790 }
1791
1792 if (val == 1)
1793 {
77b06cd7
TJB
1794 bl->watchpoint_type = hw_access;
1795 val = bl->owner->ops->insert_location (bl);
1796
1797 if (val)
1798 /* Back to the original value. */
1799 bl->watchpoint_type = hw_read;
85d721b8
PA
1800 }
1801 }
1802
35df4500 1803 bl->inserted = (val == 0);
879bfdc2
DJ
1804 }
1805
35df4500 1806 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1807 {
77b06cd7
TJB
1808 gdb_assert (bl->owner->ops != NULL
1809 && bl->owner->ops->insert_location != NULL);
1810
1811 val = bl->owner->ops->insert_location (bl);
1812 if (val)
1813 {
1814 bl->owner->enable_state = bp_disabled;
1815
1816 if (val == 1)
1817 warning (_("\
1818Error inserting catchpoint %d: Your system does not support this type\n\
1819of catchpoint."), bl->owner->number);
1820 else
1821 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1822 }
1823
1824 bl->inserted = (val == 0);
1640b821
DJ
1825
1826 /* We've already printed an error message if there was a problem
1827 inserting this catchpoint, and we've disabled the catchpoint,
1828 so just return success. */
1829 return 0;
879bfdc2
DJ
1830 }
1831
1832 return 0;
1833}
1834
6c95b8df
PA
1835/* This function is called when program space PSPACE is about to be
1836 deleted. It takes care of updating breakpoints to not reference
1837 PSPACE anymore. */
1838
1839void
1840breakpoint_program_space_exit (struct program_space *pspace)
1841{
1842 struct breakpoint *b, *b_temp;
876fa593 1843 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1844
1845 /* Remove any breakpoint that was set through this program space. */
1846 ALL_BREAKPOINTS_SAFE (b, b_temp)
1847 {
1848 if (b->pspace == pspace)
1849 delete_breakpoint (b);
1850 }
1851
1852 /* Breakpoints set through other program spaces could have locations
1853 bound to PSPACE as well. Remove those. */
876fa593 1854 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1855 {
1856 struct bp_location *tmp;
1857
1858 if (loc->pspace == pspace)
1859 {
2bdf28a0 1860 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1861 if (loc->owner->loc == loc)
1862 loc->owner->loc = loc->next;
1863 else
1864 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1865 if (tmp->next == loc)
1866 {
1867 tmp->next = loc->next;
1868 break;
1869 }
1870 }
1871 }
1872
1873 /* Now update the global location list to permanently delete the
1874 removed locations above. */
1875 update_global_location_list (0);
1876}
1877
74960c60
VP
1878/* Make sure all breakpoints are inserted in inferior.
1879 Throws exception on any error.
1880 A breakpoint that is already inserted won't be inserted
1881 again, so calling this function twice is safe. */
1882void
1883insert_breakpoints (void)
1884{
1885 struct breakpoint *bpt;
1886
1887 ALL_BREAKPOINTS (bpt)
1888 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
1889 {
1890 struct watchpoint *w = (struct watchpoint *) bpt;
1891
1892 update_watchpoint (w, 0 /* don't reparse. */);
1893 }
74960c60 1894
b60e7edf 1895 update_global_location_list (1);
74960c60 1896
c35b1492
PA
1897 /* update_global_location_list does not insert breakpoints when
1898 always_inserted_mode is not enabled. Explicitly insert them
1899 now. */
1900 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1901 insert_breakpoint_locations ();
1902}
1903
c906108c
SS
1904/* insert_breakpoints is used when starting or continuing the program.
1905 remove_breakpoints is used when the program stops.
1906 Both return zero if successful,
1907 or an `errno' value if could not write the inferior. */
1908
74960c60
VP
1909static void
1910insert_breakpoint_locations (void)
c906108c 1911{
a5606eee 1912 struct breakpoint *bpt;
35df4500 1913 struct bp_location *bl, **blp_tmp;
e236ba44 1914 int error = 0;
c906108c
SS
1915 int val = 0;
1916 int disabled_breaks = 0;
81d0cc19 1917 int hw_breakpoint_error = 0;
c906108c 1918
81d0cc19 1919 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1920 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1921
81d0cc19
GS
1922 /* Explicitly mark the warning -- this will only be printed if
1923 there was an error. */
1924 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1925
1926 save_current_space_and_thread ();
1927
35df4500 1928 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1929 {
35df4500 1930 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1931 continue;
1932
4a64f543
MS
1933 /* There is no point inserting thread-specific breakpoints if
1934 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1935 has BL->OWNER always non-NULL. */
35df4500
TJB
1936 if (bl->owner->thread != -1
1937 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1938 continue;
1939
35df4500 1940 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1941
1942 /* For targets that support global breakpoints, there's no need
1943 to select an inferior to insert breakpoint to. In fact, even
1944 if we aren't attached to any process yet, we should still
1945 insert breakpoints. */
1946 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1947 && ptid_equal (inferior_ptid, null_ptid))
1948 continue;
1949
35df4500 1950 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
879bfdc2
DJ
1951 &hw_breakpoint_error);
1952 if (val)
e236ba44 1953 error = val;
879bfdc2 1954 }
c906108c 1955
4a64f543
MS
1956 /* If we failed to insert all locations of a watchpoint, remove
1957 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
1958 ALL_BREAKPOINTS (bpt)
1959 {
1960 int some_failed = 0;
1961 struct bp_location *loc;
1962
1963 if (!is_hardware_watchpoint (bpt))
1964 continue;
1965
d6b74ac4 1966 if (!breakpoint_enabled (bpt))
a5606eee 1967 continue;
74960c60
VP
1968
1969 if (bpt->disposition == disp_del_at_next_stop)
1970 continue;
a5606eee
VP
1971
1972 for (loc = bpt->loc; loc; loc = loc->next)
56710373 1973 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
1974 {
1975 some_failed = 1;
1976 break;
1977 }
1978 if (some_failed)
1979 {
1980 for (loc = bpt->loc; loc; loc = loc->next)
1981 if (loc->inserted)
1982 remove_breakpoint (loc, mark_uninserted);
1983
1984 hw_breakpoint_error = 1;
1985 fprintf_unfiltered (tmp_error_stream,
1986 "Could not insert hardware watchpoint %d.\n",
1987 bpt->number);
1988 error = -1;
1989 }
1990 }
1991
e236ba44 1992 if (error)
81d0cc19
GS
1993 {
1994 /* If a hardware breakpoint or watchpoint was inserted, add a
1995 message about possibly exhausted resources. */
879bfdc2 1996 if (hw_breakpoint_error)
81d0cc19 1997 {
c6510018
MS
1998 fprintf_unfiltered (tmp_error_stream,
1999 "Could not insert hardware breakpoints:\n\
2000You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2001 }
81d0cc19
GS
2002 target_terminal_ours_for_output ();
2003 error_stream (tmp_error_stream);
2004 }
f7545552
TT
2005
2006 do_cleanups (cleanups);
c906108c
SS
2007}
2008
c906108c 2009int
fba45db2 2010remove_breakpoints (void)
c906108c 2011{
35df4500 2012 struct bp_location *bl, **blp_tmp;
3a1bae8e 2013 int val = 0;
c906108c 2014
35df4500 2015 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2016 {
35df4500
TJB
2017 if (bl->inserted)
2018 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2019 }
3a1bae8e 2020 return val;
c906108c
SS
2021}
2022
6c95b8df
PA
2023/* Remove breakpoints of process PID. */
2024
2025int
2026remove_breakpoints_pid (int pid)
2027{
35df4500 2028 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2029 int val;
2030 struct inferior *inf = find_inferior_pid (pid);
2031
35df4500 2032 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2033 {
35df4500 2034 if (bl->pspace != inf->pspace)
6c95b8df
PA
2035 continue;
2036
35df4500 2037 if (bl->inserted)
6c95b8df 2038 {
35df4500 2039 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2040 if (val != 0)
2041 return val;
2042 }
2043 }
2044 return 0;
2045}
2046
c906108c 2047int
fba45db2 2048reattach_breakpoints (int pid)
c906108c 2049{
6c95b8df 2050 struct cleanup *old_chain;
35df4500 2051 struct bp_location *bl, **blp_tmp;
c906108c 2052 int val;
86b887df 2053 struct ui_file *tmp_error_stream;
fa3a767f 2054 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2055 struct inferior *inf;
2056 struct thread_info *tp;
2057
2058 tp = any_live_thread_of_process (pid);
2059 if (tp == NULL)
2060 return 1;
2061
2062 inf = find_inferior_pid (pid);
2063 old_chain = save_inferior_ptid ();
2064
2065 inferior_ptid = tp->ptid;
a4954f26 2066
86b887df 2067 tmp_error_stream = mem_fileopen ();
a4954f26 2068 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2069
35df4500 2070 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2071 {
35df4500 2072 if (bl->pspace != inf->pspace)
6c95b8df
PA
2073 continue;
2074
35df4500 2075 if (bl->inserted)
c5aa993b 2076 {
35df4500
TJB
2077 bl->inserted = 0;
2078 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2079 if (val != 0)
2080 {
ce696e05 2081 do_cleanups (old_chain);
c5aa993b
JM
2082 return val;
2083 }
2084 }
2085 }
ce696e05 2086 do_cleanups (old_chain);
c906108c
SS
2087 return 0;
2088}
2089
e58b0e63
PA
2090static int internal_breakpoint_number = -1;
2091
84f4c1fe
PM
2092/* Set the breakpoint number of B, depending on the value of INTERNAL.
2093 If INTERNAL is non-zero, the breakpoint number will be populated
2094 from internal_breakpoint_number and that variable decremented.
e5dd4106 2095 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2096 breakpoint_count and that value incremented. Internal breakpoints
2097 do not set the internal var bpnum. */
2098static void
2099set_breakpoint_number (int internal, struct breakpoint *b)
2100{
2101 if (internal)
2102 b->number = internal_breakpoint_number--;
2103 else
2104 {
2105 set_breakpoint_count (breakpoint_count + 1);
2106 b->number = breakpoint_count;
2107 }
2108}
2109
e62c965a 2110static struct breakpoint *
a6d9a66e 2111create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0
PA
2112 CORE_ADDR address, enum bptype type,
2113 struct breakpoint_ops *ops)
e62c965a 2114{
e62c965a
PP
2115 struct symtab_and_line sal;
2116 struct breakpoint *b;
2117
4a64f543 2118 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2119
2120 sal.pc = address;
2121 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2122 sal.pspace = current_program_space;
e62c965a 2123
06edf0c0 2124 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2125 b->number = internal_breakpoint_number--;
2126 b->disposition = disp_donttouch;
2127
2128 return b;
2129}
2130
17450429
PP
2131static const char *const longjmp_names[] =
2132 {
2133 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2134 };
2135#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2136
2137/* Per-objfile data private to breakpoint.c. */
2138struct breakpoint_objfile_data
2139{
2140 /* Minimal symbol for "_ovly_debug_event" (if any). */
2141 struct minimal_symbol *overlay_msym;
2142
2143 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2144 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2145
2146 /* Minimal symbol for "std::terminate()" (if any). */
2147 struct minimal_symbol *terminate_msym;
2148
2149 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2150 struct minimal_symbol *exception_msym;
2151};
2152
2153static const struct objfile_data *breakpoint_objfile_key;
2154
2155/* Minimal symbol not found sentinel. */
2156static struct minimal_symbol msym_not_found;
2157
2158/* Returns TRUE if MSYM point to the "not found" sentinel. */
2159
2160static int
2161msym_not_found_p (const struct minimal_symbol *msym)
2162{
2163 return msym == &msym_not_found;
2164}
2165
2166/* Return per-objfile data needed by breakpoint.c.
2167 Allocate the data if necessary. */
2168
2169static struct breakpoint_objfile_data *
2170get_breakpoint_objfile_data (struct objfile *objfile)
2171{
2172 struct breakpoint_objfile_data *bp_objfile_data;
2173
2174 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2175 if (bp_objfile_data == NULL)
2176 {
2177 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2178 sizeof (*bp_objfile_data));
2179
2180 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2181 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2182 }
2183 return bp_objfile_data;
2184}
2185
e62c965a 2186static void
af02033e 2187create_overlay_event_breakpoint (void)
e62c965a 2188{
69de3c6a 2189 struct objfile *objfile;
af02033e 2190 const char *const func_name = "_ovly_debug_event";
e62c965a 2191
69de3c6a
PP
2192 ALL_OBJFILES (objfile)
2193 {
2194 struct breakpoint *b;
17450429
PP
2195 struct breakpoint_objfile_data *bp_objfile_data;
2196 CORE_ADDR addr;
69de3c6a 2197
17450429
PP
2198 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2199
2200 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2201 continue;
2202
2203 if (bp_objfile_data->overlay_msym == NULL)
2204 {
2205 struct minimal_symbol *m;
2206
2207 m = lookup_minimal_symbol_text (func_name, objfile);
2208 if (m == NULL)
2209 {
2210 /* Avoid future lookups in this objfile. */
2211 bp_objfile_data->overlay_msym = &msym_not_found;
2212 continue;
2213 }
2214 bp_objfile_data->overlay_msym = m;
2215 }
e62c965a 2216
17450429
PP
2217 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2218 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2219 bp_overlay_event,
2220 &internal_breakpoint_ops);
69de3c6a 2221 b->addr_string = xstrdup (func_name);
e62c965a 2222
69de3c6a
PP
2223 if (overlay_debugging == ovly_auto)
2224 {
2225 b->enable_state = bp_enabled;
2226 overlay_events_enabled = 1;
2227 }
2228 else
2229 {
2230 b->enable_state = bp_disabled;
2231 overlay_events_enabled = 0;
2232 }
e62c965a
PP
2233 }
2234 update_global_location_list (1);
2235}
2236
0fd8e87f 2237static void
af02033e 2238create_longjmp_master_breakpoint (void)
0fd8e87f 2239{
6c95b8df 2240 struct program_space *pspace;
6c95b8df
PA
2241 struct cleanup *old_chain;
2242
2243 old_chain = save_current_program_space ();
0fd8e87f 2244
6c95b8df 2245 ALL_PSPACES (pspace)
af02033e
PP
2246 {
2247 struct objfile *objfile;
2248
2249 set_current_program_space (pspace);
2250
2251 ALL_OBJFILES (objfile)
0fd8e87f 2252 {
af02033e
PP
2253 int i;
2254 struct gdbarch *gdbarch;
17450429 2255 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2256
af02033e
PP
2257 gdbarch = get_objfile_arch (objfile);
2258 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2259 continue;
2260
17450429
PP
2261 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2262
2263 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2264 {
2265 struct breakpoint *b;
af02033e 2266 const char *func_name;
17450429 2267 CORE_ADDR addr;
6c95b8df 2268
17450429 2269 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2270 continue;
0fd8e87f 2271
17450429
PP
2272 func_name = longjmp_names[i];
2273 if (bp_objfile_data->longjmp_msym[i] == NULL)
2274 {
2275 struct minimal_symbol *m;
2276
2277 m = lookup_minimal_symbol_text (func_name, objfile);
2278 if (m == NULL)
2279 {
2280 /* Prevent future lookups in this objfile. */
2281 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2282 continue;
2283 }
2284 bp_objfile_data->longjmp_msym[i] = m;
2285 }
2286
2287 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2288 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2289 &internal_breakpoint_ops);
af02033e
PP
2290 b->addr_string = xstrdup (func_name);
2291 b->enable_state = bp_disabled;
2292 }
0fd8e87f 2293 }
af02033e 2294 }
0fd8e87f 2295 update_global_location_list (1);
6c95b8df
PA
2296
2297 do_cleanups (old_chain);
0fd8e87f
UW
2298}
2299
af02033e 2300/* Create a master std::terminate breakpoint. */
aa7d318d 2301static void
af02033e 2302create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2303{
2304 struct program_space *pspace;
aa7d318d 2305 struct cleanup *old_chain;
af02033e 2306 const char *const func_name = "std::terminate()";
aa7d318d
TT
2307
2308 old_chain = save_current_program_space ();
2309
2310 ALL_PSPACES (pspace)
17450429
PP
2311 {
2312 struct objfile *objfile;
2313 CORE_ADDR addr;
2314
2315 set_current_program_space (pspace);
2316
aa7d318d
TT
2317 ALL_OBJFILES (objfile)
2318 {
2319 struct breakpoint *b;
17450429 2320 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2321
17450429 2322 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2323
17450429
PP
2324 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2325 continue;
2326
2327 if (bp_objfile_data->terminate_msym == NULL)
2328 {
2329 struct minimal_symbol *m;
2330
2331 m = lookup_minimal_symbol (func_name, NULL, objfile);
2332 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2333 && MSYMBOL_TYPE (m) != mst_file_text))
2334 {
2335 /* Prevent future lookups in this objfile. */
2336 bp_objfile_data->terminate_msym = &msym_not_found;
2337 continue;
2338 }
2339 bp_objfile_data->terminate_msym = m;
2340 }
aa7d318d 2341
17450429
PP
2342 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2343 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2344 bp_std_terminate_master,
2345 &internal_breakpoint_ops);
aa7d318d
TT
2346 b->addr_string = xstrdup (func_name);
2347 b->enable_state = bp_disabled;
2348 }
17450429
PP
2349 }
2350
aa7d318d
TT
2351 update_global_location_list (1);
2352
2353 do_cleanups (old_chain);
2354}
2355
186c406b
TT
2356/* Install a master breakpoint on the unwinder's debug hook. */
2357
2358void
2359create_exception_master_breakpoint (void)
2360{
2361 struct objfile *objfile;
17450429 2362 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2363
2364 ALL_OBJFILES (objfile)
2365 {
17450429
PP
2366 struct breakpoint *b;
2367 struct gdbarch *gdbarch;
2368 struct breakpoint_objfile_data *bp_objfile_data;
2369 CORE_ADDR addr;
2370
2371 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2372
2373 if (msym_not_found_p (bp_objfile_data->exception_msym))
2374 continue;
2375
2376 gdbarch = get_objfile_arch (objfile);
186c406b 2377
17450429 2378 if (bp_objfile_data->exception_msym == NULL)
186c406b 2379 {
17450429 2380 struct minimal_symbol *debug_hook;
186c406b 2381
17450429
PP
2382 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2383 if (debug_hook == NULL)
2384 {
2385 bp_objfile_data->exception_msym = &msym_not_found;
2386 continue;
2387 }
2388
2389 bp_objfile_data->exception_msym = debug_hook;
186c406b 2390 }
17450429
PP
2391
2392 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2393 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2394 &current_target);
06edf0c0
PA
2395 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2396 &internal_breakpoint_ops);
17450429
PP
2397 b->addr_string = xstrdup (func_name);
2398 b->enable_state = bp_disabled;
186c406b
TT
2399 }
2400
2401 update_global_location_list (1);
2402}
2403
c906108c 2404void
fba45db2 2405update_breakpoints_after_exec (void)
c906108c 2406{
35df4500 2407 struct breakpoint *b, *b_tmp;
876fa593 2408 struct bp_location *bploc, **bplocp_tmp;
c906108c 2409
25b22b0a
PA
2410 /* We're about to delete breakpoints from GDB's lists. If the
2411 INSERTED flag is true, GDB will try to lift the breakpoints by
2412 writing the breakpoints' "shadow contents" back into memory. The
2413 "shadow contents" are NOT valid after an exec, so GDB should not
2414 do that. Instead, the target is responsible from marking
2415 breakpoints out as soon as it detects an exec. We don't do that
2416 here instead, because there may be other attempts to delete
2417 breakpoints after detecting an exec and before reaching here. */
876fa593 2418 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2419 if (bploc->pspace == current_program_space)
2420 gdb_assert (!bploc->inserted);
c906108c 2421
35df4500 2422 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2423 {
6c95b8df
PA
2424 if (b->pspace != current_program_space)
2425 continue;
2426
4a64f543 2427 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2428 if (b->type == bp_shlib_event)
2429 {
2430 delete_breakpoint (b);
2431 continue;
2432 }
c906108c 2433
4a64f543 2434 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2435 if (b->type == bp_jit_event)
2436 {
2437 delete_breakpoint (b);
2438 continue;
2439 }
2440
1900040c 2441 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2442 as must overlay event and longjmp master breakpoints. */
2443 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2444 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2445 || b->type == bp_exception_master)
c4093a6a
JM
2446 {
2447 delete_breakpoint (b);
2448 continue;
2449 }
2450
4a64f543 2451 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 2452 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
2453 {
2454 delete_breakpoint (b);
2455 continue;
2456 }
2457
611c83ae
PA
2458 /* Longjmp and longjmp-resume breakpoints are also meaningless
2459 after an exec. */
186c406b
TT
2460 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2461 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2462 {
2463 delete_breakpoint (b);
2464 continue;
2465 }
2466
ce78b96d
JB
2467 if (b->type == bp_catchpoint)
2468 {
2469 /* For now, none of the bp_catchpoint breakpoints need to
2470 do anything at this point. In the future, if some of
2471 the catchpoints need to something, we will need to add
2472 a new method, and call this method from here. */
2473 continue;
2474 }
2475
c5aa993b
JM
2476 /* bp_finish is a special case. The only way we ought to be able
2477 to see one of these when an exec() has happened, is if the user
2478 caught a vfork, and then said "finish". Ordinarily a finish just
2479 carries them to the call-site of the current callee, by setting
2480 a temporary bp there and resuming. But in this case, the finish
2481 will carry them entirely through the vfork & exec.
2482
2483 We don't want to allow a bp_finish to remain inserted now. But
2484 we can't safely delete it, 'cause finish_command has a handle to
2485 the bp on a bpstat, and will later want to delete it. There's a
2486 chance (and I've seen it happen) that if we delete the bp_finish
2487 here, that its storage will get reused by the time finish_command
2488 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2489 We really must allow finish_command to delete a bp_finish.
2490
e5dd4106 2491 In the absence of a general solution for the "how do we know
53a5351d
JM
2492 it's safe to delete something others may have handles to?"
2493 problem, what we'll do here is just uninsert the bp_finish, and
2494 let finish_command delete it.
2495
2496 (We know the bp_finish is "doomed" in the sense that it's
2497 momentary, and will be deleted as soon as finish_command sees
2498 the inferior stopped. So it doesn't matter that the bp's
2499 address is probably bogus in the new a.out, unlike e.g., the
2500 solib breakpoints.) */
c5aa993b 2501
c5aa993b
JM
2502 if (b->type == bp_finish)
2503 {
2504 continue;
2505 }
2506
2507 /* Without a symbolic address, we have little hope of the
2508 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2509 a.out. */
c5aa993b
JM
2510 if (b->addr_string == NULL)
2511 {
2512 delete_breakpoint (b);
2513 continue;
2514 }
c5aa993b 2515 }
1900040c 2516 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2517 create_overlay_event_breakpoint ();
2518 create_longjmp_master_breakpoint ();
2519 create_std_terminate_master_breakpoint ();
186c406b 2520 create_exception_master_breakpoint ();
c906108c
SS
2521}
2522
2523int
fba45db2 2524detach_breakpoints (int pid)
c906108c 2525{
35df4500 2526 struct bp_location *bl, **blp_tmp;
3a1bae8e 2527 int val = 0;
ce696e05 2528 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2529 struct inferior *inf = current_inferior ();
c5aa993b 2530
39f77062 2531 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2532 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2533
6c95b8df 2534 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2535 inferior_ptid = pid_to_ptid (pid);
35df4500 2536 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2537 {
35df4500 2538 if (bl->pspace != inf->pspace)
6c95b8df
PA
2539 continue;
2540
35df4500
TJB
2541 if (bl->inserted)
2542 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2543 }
d03285ec
UW
2544
2545 /* Detach single-step breakpoints as well. */
2546 detach_single_step_breakpoints ();
2547
ce696e05 2548 do_cleanups (old_chain);
3a1bae8e 2549 return val;
c906108c
SS
2550}
2551
35df4500 2552/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2553 Note that this is used to detach breakpoints from a child fork.
2554 When we get here, the child isn't in the inferior list, and neither
2555 do we have objects to represent its address space --- we should
35df4500 2556 *not* look at bl->pspace->aspace here. */
6c95b8df 2557
c906108c 2558static int
35df4500 2559remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2560{
2561 int val;
c5aa993b 2562
35df4500
TJB
2563 /* BL is never in moribund_locations by our callers. */
2564 gdb_assert (bl->owner != NULL);
2bdf28a0 2565
35df4500 2566 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2567 /* Permanent breakpoints cannot be inserted or removed. */
2568 return 0;
2569
74960c60
VP
2570 /* The type of none suggests that owner is actually deleted.
2571 This should not ever happen. */
35df4500 2572 gdb_assert (bl->owner->type != bp_none);
0bde7532 2573
35df4500
TJB
2574 if (bl->loc_type == bp_loc_software_breakpoint
2575 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2576 {
c02f5703
MS
2577 /* "Normal" instruction breakpoint: either the standard
2578 trap-instruction bp (bp_breakpoint), or a
2579 bp_hardware_breakpoint. */
2580
2581 /* First check to see if we have to handle an overlay. */
2582 if (overlay_debugging == ovly_off
35df4500
TJB
2583 || bl->section == NULL
2584 || !(section_is_overlay (bl->section)))
c02f5703
MS
2585 {
2586 /* No overlay handling: just remove the breakpoint. */
348d480f 2587 val = bl->owner->ops->remove_location (bl);
c02f5703 2588 }
c906108c
SS
2589 else
2590 {
4a64f543 2591 /* This breakpoint is in an overlay section.
c02f5703
MS
2592 Did we set a breakpoint at the LMA? */
2593 if (!overlay_events_enabled)
2594 {
2595 /* Yes -- overlay event support is not active, so we
2596 should have set a breakpoint at the LMA. Remove it.
2597 */
c02f5703
MS
2598 /* Ignore any failures: if the LMA is in ROM, we will
2599 have already warned when we failed to insert it. */
35df4500
TJB
2600 if (bl->loc_type == bp_loc_hardware_breakpoint)
2601 target_remove_hw_breakpoint (bl->gdbarch,
2602 &bl->overlay_target_info);
c02f5703 2603 else
35df4500
TJB
2604 target_remove_breakpoint (bl->gdbarch,
2605 &bl->overlay_target_info);
c02f5703
MS
2606 }
2607 /* Did we set a breakpoint at the VMA?
2608 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2609 if (bl->inserted)
c906108c 2610 {
c02f5703
MS
2611 /* Yes -- remove it. Previously we did not bother to
2612 remove the breakpoint if the section had been
2613 unmapped, but let's not rely on that being safe. We
2614 don't know what the overlay manager might do. */
aa67235e
UW
2615
2616 /* However, we should remove *software* breakpoints only
2617 if the section is still mapped, or else we overwrite
2618 wrong code with the saved shadow contents. */
348d480f
PA
2619 if (bl->loc_type == bp_loc_hardware_breakpoint
2620 || section_is_mapped (bl->section))
2621 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
2622 else
2623 val = 0;
c906108c 2624 }
c02f5703
MS
2625 else
2626 {
2627 /* No -- not inserted, so no need to remove. No error. */
2628 val = 0;
2629 }
c906108c 2630 }
879d1e6b
UW
2631
2632 /* In some cases, we might not be able to remove a breakpoint
2633 in a shared library that has already been removed, but we
2634 have not yet processed the shlib unload event. */
35df4500 2635 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2636 val = 0;
2637
c906108c
SS
2638 if (val)
2639 return val;
35df4500 2640 bl->inserted = (is == mark_inserted);
c906108c 2641 }
35df4500 2642 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2643 {
77b06cd7
TJB
2644 gdb_assert (bl->owner->ops != NULL
2645 && bl->owner->ops->remove_location != NULL);
2646
35df4500 2647 bl->inserted = (is == mark_inserted);
77b06cd7 2648 bl->owner->ops->remove_location (bl);
2e70b7b9 2649
c906108c 2650 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2651 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2652 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2653 bl->owner->number);
c906108c 2654 }
35df4500
TJB
2655 else if (bl->owner->type == bp_catchpoint
2656 && breakpoint_enabled (bl->owner)
2657 && !bl->duplicate)
ce78b96d 2658 {
77b06cd7
TJB
2659 gdb_assert (bl->owner->ops != NULL
2660 && bl->owner->ops->remove_location != NULL);
ce78b96d 2661
77b06cd7 2662 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2663 if (val)
2664 return val;
77b06cd7 2665
35df4500 2666 bl->inserted = (is == mark_inserted);
ce78b96d 2667 }
c906108c
SS
2668
2669 return 0;
2670}
2671
6c95b8df 2672static int
35df4500 2673remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2674{
2675 int ret;
2676 struct cleanup *old_chain;
2677
35df4500
TJB
2678 /* BL is never in moribund_locations by our callers. */
2679 gdb_assert (bl->owner != NULL);
2bdf28a0 2680
35df4500 2681 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2682 /* Permanent breakpoints cannot be inserted or removed. */
2683 return 0;
2684
2685 /* The type of none suggests that owner is actually deleted.
2686 This should not ever happen. */
35df4500 2687 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2688
2689 old_chain = save_current_space_and_thread ();
2690
35df4500 2691 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2692
35df4500 2693 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2694
2695 do_cleanups (old_chain);
2696 return ret;
2697}
2698
c906108c
SS
2699/* Clear the "inserted" flag in all breakpoints. */
2700
25b22b0a 2701void
fba45db2 2702mark_breakpoints_out (void)
c906108c 2703{
35df4500 2704 struct bp_location *bl, **blp_tmp;
c906108c 2705
35df4500
TJB
2706 ALL_BP_LOCATIONS (bl, blp_tmp)
2707 if (bl->pspace == current_program_space)
2708 bl->inserted = 0;
c906108c
SS
2709}
2710
53a5351d
JM
2711/* Clear the "inserted" flag in all breakpoints and delete any
2712 breakpoints which should go away between runs of the program.
c906108c
SS
2713
2714 Plus other such housekeeping that has to be done for breakpoints
2715 between runs.
2716
53a5351d
JM
2717 Note: this function gets called at the end of a run (by
2718 generic_mourn_inferior) and when a run begins (by
4a64f543 2719 init_wait_for_inferior). */
c906108c
SS
2720
2721
2722
2723void
fba45db2 2724breakpoint_init_inferior (enum inf_context context)
c906108c 2725{
35df4500
TJB
2726 struct breakpoint *b, *b_tmp;
2727 struct bp_location *bl, **blp_tmp;
1c5cfe86 2728 int ix;
6c95b8df 2729 struct program_space *pspace = current_program_space;
c906108c 2730
50c71eaf
PA
2731 /* If breakpoint locations are shared across processes, then there's
2732 nothing to do. */
2567c7d9 2733 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2734 return;
2735
35df4500 2736 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2737 {
35df4500
TJB
2738 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2739 if (bl->pspace == pspace
2740 && bl->owner->enable_state != bp_permanent)
2741 bl->inserted = 0;
6c95b8df 2742 }
075f6582 2743
35df4500 2744 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2745 {
6c95b8df
PA
2746 if (b->loc && b->loc->pspace != pspace)
2747 continue;
2748
c5aa993b
JM
2749 switch (b->type)
2750 {
2751 case bp_call_dummy:
c906108c 2752
c5aa993b 2753 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2754 cause problems when the inferior is rerun, so we better get
2755 rid of it. */
2756
2757 case bp_watchpoint_scope:
2758
2759 /* Also get rid of scope breakpoints. */
2760
2761 case bp_shlib_event:
2762
2763 /* Also remove solib event breakpoints. Their addresses may
2764 have changed since the last time we ran the program.
2765 Actually we may now be debugging against different target;
2766 and so the solib backend that installed this breakpoint may
2767 not be used in by the target. E.g.,
2768
2769 (gdb) file prog-linux
2770 (gdb) run # native linux target
2771 ...
2772 (gdb) kill
2773 (gdb) file prog-win.exe
2774 (gdb) tar rem :9999 # remote Windows gdbserver.
2775 */
c906108c 2776
c5aa993b
JM
2777 delete_breakpoint (b);
2778 break;
c906108c 2779
c5aa993b
JM
2780 case bp_watchpoint:
2781 case bp_hardware_watchpoint:
2782 case bp_read_watchpoint:
2783 case bp_access_watchpoint:
3a5c3e22
PA
2784 {
2785 struct watchpoint *w = (struct watchpoint *) b;
c906108c 2786
3a5c3e22
PA
2787 /* Likewise for watchpoints on local expressions. */
2788 if (w->exp_valid_block != NULL)
2789 delete_breakpoint (b);
2790 else if (context == inf_starting)
2791 {
2792 /* Reset val field to force reread of starting value in
2793 insert_breakpoints. */
2794 if (w->val)
2795 value_free (w->val);
2796 w->val = NULL;
2797 w->val_valid = 0;
c860120c 2798 }
3a5c3e22 2799 }
c5aa993b
JM
2800 break;
2801 default:
c5aa993b
JM
2802 break;
2803 }
2804 }
1c5cfe86
PA
2805
2806 /* Get rid of the moribund locations. */
35df4500
TJB
2807 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2808 decref_bp_location (&bl);
1c5cfe86 2809 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2810}
2811
6c95b8df
PA
2812/* These functions concern about actual breakpoints inserted in the
2813 target --- to e.g. check if we need to do decr_pc adjustment or if
2814 we need to hop over the bkpt --- so we check for address space
2815 match, not program space. */
2816
c2c6d25f
JM
2817/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2818 exists at PC. It returns ordinary_breakpoint_here if it's an
2819 ordinary breakpoint, or permanent_breakpoint_here if it's a
2820 permanent breakpoint.
2821 - When continuing from a location with an ordinary breakpoint, we
2822 actually single step once before calling insert_breakpoints.
e5dd4106 2823 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
2824 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2825 the target, to advance the PC past the breakpoint. */
c906108c 2826
c2c6d25f 2827enum breakpoint_here
6c95b8df 2828breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2829{
35df4500 2830 struct bp_location *bl, **blp_tmp;
c2c6d25f 2831 int any_breakpoint_here = 0;
c906108c 2832
35df4500 2833 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2834 {
35df4500
TJB
2835 if (bl->loc_type != bp_loc_software_breakpoint
2836 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2837 continue;
2838
f1310107 2839 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2840 if ((breakpoint_enabled (bl->owner)
2841 || bl->owner->enable_state == bp_permanent)
f1310107 2842 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2843 {
2844 if (overlay_debugging
35df4500
TJB
2845 && section_is_overlay (bl->section)
2846 && !section_is_mapped (bl->section))
075f6582 2847 continue; /* unmapped overlay -- can't be a match */
35df4500 2848 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2849 return permanent_breakpoint_here;
2850 else
2851 any_breakpoint_here = 1;
2852 }
2853 }
c906108c 2854
c2c6d25f 2855 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2856}
2857
1c5cfe86
PA
2858/* Return true if there's a moribund breakpoint at PC. */
2859
2860int
6c95b8df 2861moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2862{
2863 struct bp_location *loc;
2864 int ix;
2865
2866 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2867 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2868 return 1;
2869
2870 return 0;
2871}
c2c6d25f 2872
c36b740a 2873/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2874 inserted using regular breakpoint_chain / bp_location array
2875 mechanism. This does not check for single-step breakpoints, which
2876 are inserted and removed using direct target manipulation. */
c906108c
SS
2877
2878int
4a64f543
MS
2879regular_breakpoint_inserted_here_p (struct address_space *aspace,
2880 CORE_ADDR pc)
c906108c 2881{
35df4500 2882 struct bp_location *bl, **blp_tmp;
c906108c 2883
35df4500 2884 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2885 {
35df4500
TJB
2886 if (bl->loc_type != bp_loc_software_breakpoint
2887 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2888 continue;
2889
35df4500 2890 if (bl->inserted
f1310107 2891 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2892 {
2893 if (overlay_debugging
35df4500
TJB
2894 && section_is_overlay (bl->section)
2895 && !section_is_mapped (bl->section))
075f6582
DJ
2896 continue; /* unmapped overlay -- can't be a match */
2897 else
2898 return 1;
2899 }
c5aa993b 2900 }
c36b740a
VP
2901 return 0;
2902}
2903
2904/* Returns non-zero iff there's either regular breakpoint
2905 or a single step breakpoint inserted at PC. */
2906
2907int
6c95b8df 2908breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2909{
6c95b8df 2910 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2911 return 1;
c906108c 2912
6c95b8df 2913 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2914 return 1;
2915
c906108c
SS
2916 return 0;
2917}
2918
4fa8626c
DJ
2919/* This function returns non-zero iff there is a software breakpoint
2920 inserted at PC. */
2921
2922int
3e43a32a
MS
2923software_breakpoint_inserted_here_p (struct address_space *aspace,
2924 CORE_ADDR pc)
4fa8626c 2925{
35df4500 2926 struct bp_location *bl, **blp_tmp;
4fa8626c 2927
35df4500 2928 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2929 {
35df4500 2930 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2931 continue;
2932
35df4500
TJB
2933 if (bl->inserted
2934 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2935 aspace, pc))
4fa8626c
DJ
2936 {
2937 if (overlay_debugging
35df4500
TJB
2938 && section_is_overlay (bl->section)
2939 && !section_is_mapped (bl->section))
4fa8626c
DJ
2940 continue; /* unmapped overlay -- can't be a match */
2941 else
2942 return 1;
2943 }
2944 }
2945
1aafd4da 2946 /* Also check for software single-step breakpoints. */
6c95b8df 2947 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2948 return 1;
2949
4fa8626c
DJ
2950 return 0;
2951}
2952
9093389c
PA
2953int
2954hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2955 CORE_ADDR addr, ULONGEST len)
2956{
2957 struct breakpoint *bpt;
2958
2959 ALL_BREAKPOINTS (bpt)
2960 {
2961 struct bp_location *loc;
2962
2963 if (bpt->type != bp_hardware_watchpoint
2964 && bpt->type != bp_access_watchpoint)
2965 continue;
2966
2967 if (!breakpoint_enabled (bpt))
2968 continue;
2969
2970 for (loc = bpt->loc; loc; loc = loc->next)
2971 if (loc->pspace->aspace == aspace && loc->inserted)
2972 {
2973 CORE_ADDR l, h;
2974
2975 /* Check for intersection. */
2976 l = max (loc->address, addr);
2977 h = min (loc->address + loc->length, addr + len);
2978 if (l < h)
2979 return 1;
2980 }
2981 }
2982 return 0;
2983}
2984
075f6582
DJ
2985/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2986 PC is valid for process/thread PTID. */
c906108c
SS
2987
2988int
6c95b8df
PA
2989breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2990 ptid_t ptid)
c906108c 2991{
35df4500 2992 struct bp_location *bl, **blp_tmp;
4a306c9a 2993 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 2994 int thread = -1;
4a306c9a 2995 int task = 0;
a6f1cd96 2996
35df4500 2997 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2998 {
35df4500
TJB
2999 if (bl->loc_type != bp_loc_software_breakpoint
3000 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3001 continue;
3002
35df4500
TJB
3003 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3004 if (!breakpoint_enabled (bl->owner)
3005 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3006 continue;
3007
f1310107 3008 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3009 continue;
3010
35df4500 3011 if (bl->owner->thread != -1)
075f6582 3012 {
a6f1cd96
JB
3013 /* This is a thread-specific breakpoint. Check that ptid
3014 matches that thread. If thread hasn't been computed yet,
3015 it is now time to do so. */
3016 if (thread == -1)
3017 thread = pid_to_thread_id (ptid);
35df4500 3018 if (bl->owner->thread != thread)
a6f1cd96 3019 continue;
075f6582 3020 }
a6f1cd96 3021
35df4500 3022 if (bl->owner->task != 0)
4a306c9a
JB
3023 {
3024 /* This is a task-specific breakpoint. Check that ptid
3025 matches that task. If task hasn't been computed yet,
3026 it is now time to do so. */
3027 if (task == 0)
3028 task = ada_get_task_number (ptid);
35df4500 3029 if (bl->owner->task != task)
4a306c9a
JB
3030 continue;
3031 }
3032
a6f1cd96 3033 if (overlay_debugging
35df4500
TJB
3034 && section_is_overlay (bl->section)
3035 && !section_is_mapped (bl->section))
a6f1cd96
JB
3036 continue; /* unmapped overlay -- can't be a match */
3037
3038 return 1;
c5aa993b 3039 }
c906108c
SS
3040
3041 return 0;
3042}
c906108c 3043\f
c5aa993b 3044
c906108c
SS
3045/* bpstat stuff. External routines' interfaces are documented
3046 in breakpoint.h. */
3047
3048int
fba45db2 3049ep_is_catchpoint (struct breakpoint *ep)
c906108c 3050{
533be4dd 3051 return (ep->type == bp_catchpoint);
c906108c
SS
3052}
3053
f431efe5
PA
3054/* Frees any storage that is part of a bpstat. Does not walk the
3055 'next' chain. */
3056
3057static void
198757a8
VP
3058bpstat_free (bpstat bs)
3059{
3060 if (bs->old_val != NULL)
3061 value_free (bs->old_val);
9add0f1b 3062 decref_counted_command_line (&bs->commands);
f431efe5 3063 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3064 xfree (bs);
3065}
3066
c906108c
SS
3067/* Clear a bpstat so that it says we are not at any breakpoint.
3068 Also free any storage that is part of a bpstat. */
3069
3070void
fba45db2 3071bpstat_clear (bpstat *bsp)
c906108c
SS
3072{
3073 bpstat p;
3074 bpstat q;
3075
3076 if (bsp == 0)
3077 return;
3078 p = *bsp;
3079 while (p != NULL)
3080 {
3081 q = p->next;
198757a8 3082 bpstat_free (p);
c906108c
SS
3083 p = q;
3084 }
3085 *bsp = NULL;
3086}
3087
3088/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3089 is part of the bpstat is copied as well. */
3090
3091bpstat
fba45db2 3092bpstat_copy (bpstat bs)
c906108c
SS
3093{
3094 bpstat p = NULL;
3095 bpstat tmp;
3096 bpstat retval = NULL;
3097
3098 if (bs == NULL)
3099 return bs;
3100
3101 for (; bs != NULL; bs = bs->next)
3102 {
3103 tmp = (bpstat) xmalloc (sizeof (*tmp));
3104 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3105 incref_counted_command_line (tmp->commands);
f431efe5 3106 incref_bp_location (tmp->bp_location_at);
31cc81e9 3107 if (bs->old_val != NULL)
3c3185ac
JK
3108 {
3109 tmp->old_val = value_copy (bs->old_val);
3110 release_value (tmp->old_val);
3111 }
31cc81e9 3112
c906108c
SS
3113 if (p == NULL)
3114 /* This is the first thing in the chain. */
3115 retval = tmp;
3116 else
3117 p->next = tmp;
3118 p = tmp;
3119 }
3120 p->next = NULL;
3121 return retval;
3122}
3123
4a64f543 3124/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3125
3126bpstat
fba45db2 3127bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3128{
c5aa993b
JM
3129 if (bsp == NULL)
3130 return NULL;
c906108c 3131
c5aa993b
JM
3132 for (; bsp != NULL; bsp = bsp->next)
3133 {
f431efe5 3134 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3135 return bsp;
3136 }
c906108c
SS
3137 return NULL;
3138}
3139
4a64f543
MS
3140/* Put in *NUM the breakpoint number of the first breakpoint we are
3141 stopped at. *BSP upon return is a bpstat which points to the
3142 remaining breakpoints stopped at (but which is not guaranteed to be
3143 good for anything but further calls to bpstat_num).
3144
8671a17b
PA
3145 Return 0 if passed a bpstat which does not indicate any breakpoints.
3146 Return -1 if stopped at a breakpoint that has been deleted since
3147 we set it.
3148 Return 1 otherwise. */
c906108c
SS
3149
3150int
8671a17b 3151bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3152{
3153 struct breakpoint *b;
3154
3155 if ((*bsp) == NULL)
3156 return 0; /* No more breakpoint values */
8671a17b 3157
4a64f543
MS
3158 /* We assume we'll never have several bpstats that correspond to a
3159 single breakpoint -- otherwise, this function might return the
3160 same number more than once and this will look ugly. */
f431efe5 3161 b = (*bsp)->breakpoint_at;
8671a17b
PA
3162 *bsp = (*bsp)->next;
3163 if (b == NULL)
3164 return -1; /* breakpoint that's been deleted since */
3165
3166 *num = b->number; /* We have its number */
3167 return 1;
c906108c
SS
3168}
3169
3170/* Modify BS so that the actions will not be performed. */
3171
3172void
fba45db2 3173bpstat_clear_actions (bpstat bs)
c906108c
SS
3174{
3175 for (; bs != NULL; bs = bs->next)
3176 {
9add0f1b 3177 decref_counted_command_line (&bs->commands);
dde2d684 3178 bs->commands_left = NULL;
c906108c
SS
3179 if (bs->old_val != NULL)
3180 {
3181 value_free (bs->old_val);
3182 bs->old_val = NULL;
3183 }
3184 }
3185}
3186
f3b1572e
PA
3187/* Called when a command is about to proceed the inferior. */
3188
3189static void
3190breakpoint_about_to_proceed (void)
3191{
3192 if (!ptid_equal (inferior_ptid, null_ptid))
3193 {
3194 struct thread_info *tp = inferior_thread ();
3195
3196 /* Allow inferior function calls in breakpoint commands to not
3197 interrupt the command list. When the call finishes
3198 successfully, the inferior will be standing at the same
3199 breakpoint as if nothing happened. */
16c381f0 3200 if (tp->control.in_infcall)
f3b1572e
PA
3201 return;
3202 }
3203
3204 breakpoint_proceeded = 1;
3205}
3206
4a64f543
MS
3207/* Stub for cleaning up our state if we error-out of a breakpoint
3208 command. */
c906108c 3209static void
4efb68b1 3210cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3211{
3212 executing_breakpoint_commands = 0;
3213}
3214
4a64f543
MS
3215/* Execute all the commands associated with all the breakpoints at
3216 this location. Any of these commands could cause the process to
3217 proceed beyond this point, etc. We look out for such changes by
3218 checking the global "breakpoint_proceeded" after each command.
c906108c 3219
347bddb7
PA
3220 Returns true if a breakpoint command resumed the inferior. In that
3221 case, it is the caller's responsibility to recall it again with the
3222 bpstat of the current thread. */
3223
3224static int
3225bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3226{
3227 bpstat bs;
3228 struct cleanup *old_chain;
347bddb7 3229 int again = 0;
c906108c
SS
3230
3231 /* Avoid endless recursion if a `source' command is contained
3232 in bs->commands. */
3233 if (executing_breakpoint_commands)
347bddb7 3234 return 0;
c906108c
SS
3235
3236 executing_breakpoint_commands = 1;
3237 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3238
cf6c5ffb
TT
3239 prevent_dont_repeat ();
3240
4a64f543 3241 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3242 bs = *bsp;
3243
3244 breakpoint_proceeded = 0;
3245 for (; bs != NULL; bs = bs->next)
3246 {
9add0f1b 3247 struct counted_command_line *ccmd;
6c50ab1c
JB
3248 struct command_line *cmd;
3249 struct cleanup *this_cmd_tree_chain;
3250
3251 /* Take ownership of the BSP's command tree, if it has one.
3252
3253 The command tree could legitimately contain commands like
3254 'step' and 'next', which call clear_proceed_status, which
3255 frees stop_bpstat's command tree. To make sure this doesn't
3256 free the tree we're executing out from under us, we need to
3257 take ownership of the tree ourselves. Since a given bpstat's
3258 commands are only executed once, we don't need to copy it; we
3259 can clear the pointer in the bpstat, and make sure we free
3260 the tree when we're done. */
9add0f1b
TT
3261 ccmd = bs->commands;
3262 bs->commands = NULL;
3263 this_cmd_tree_chain
3264 = make_cleanup_decref_counted_command_line (&ccmd);
3265 cmd = bs->commands_left;
3266 bs->commands_left = NULL;
6c50ab1c 3267
c906108c
SS
3268 while (cmd != NULL)
3269 {
3270 execute_control_command (cmd);
3271
3272 if (breakpoint_proceeded)
3273 break;
3274 else
3275 cmd = cmd->next;
3276 }
6c50ab1c
JB
3277
3278 /* We can free this command tree now. */
3279 do_cleanups (this_cmd_tree_chain);
3280
c906108c 3281 if (breakpoint_proceeded)
32c1e744
VP
3282 {
3283 if (target_can_async_p ())
347bddb7
PA
3284 /* If we are in async mode, then the target might be still
3285 running, not stopped at any breakpoint, so nothing for
3286 us to do here -- just return to the event loop. */
3287 ;
32c1e744
VP
3288 else
3289 /* In sync mode, when execute_control_command returns
3290 we're already standing on the next breakpoint.
347bddb7
PA
3291 Breakpoint commands for that stop were not run, since
3292 execute_command does not run breakpoint commands --
3293 only command_line_handler does, but that one is not
3294 involved in execution of breakpoint commands. So, we
3295 can now execute breakpoint commands. It should be
3296 noted that making execute_command do bpstat actions is
3297 not an option -- in this case we'll have recursive
3298 invocation of bpstat for each breakpoint with a
3299 command, and can easily blow up GDB stack. Instead, we
3300 return true, which will trigger the caller to recall us
3301 with the new stop_bpstat. */
3302 again = 1;
3303 break;
32c1e744 3304 }
c906108c 3305 }
c2b8ed2c 3306 do_cleanups (old_chain);
347bddb7
PA
3307 return again;
3308}
3309
3310void
3311bpstat_do_actions (void)
3312{
3313 /* Do any commands attached to breakpoint we are stopped at. */
3314 while (!ptid_equal (inferior_ptid, null_ptid)
3315 && target_has_execution
3316 && !is_exited (inferior_ptid)
3317 && !is_executing (inferior_ptid))
3318 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3319 and only return when it is stopped at the next breakpoint, we
3320 keep doing breakpoint actions until it returns false to
3321 indicate the inferior was not resumed. */
16c381f0 3322 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3323 break;
c906108c
SS
3324}
3325
fa4727a6
DJ
3326/* Print out the (old or new) value associated with a watchpoint. */
3327
3328static void
3329watchpoint_value_print (struct value *val, struct ui_file *stream)
3330{
3331 if (val == NULL)
3332 fprintf_unfiltered (stream, _("<unreadable>"));
3333 else
79a45b7d
TT
3334 {
3335 struct value_print_options opts;
3336 get_user_print_options (&opts);
3337 value_print (val, stream, &opts);
3338 }
fa4727a6
DJ
3339}
3340
e514a9d6 3341/* Generic routine for printing messages indicating why we
4a64f543 3342 stopped. The behavior of this function depends on the value
e514a9d6
JM
3343 'print_it' in the bpstat structure. Under some circumstances we
3344 may decide not to print anything here and delegate the task to
4a64f543 3345 normal_stop(). */
e514a9d6
JM
3346
3347static enum print_stop_action
3348print_bp_stop_message (bpstat bs)
3349{
3350 switch (bs->print_it)
3351 {
3352 case print_it_noop:
4a64f543 3353 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3354 return PRINT_UNKNOWN;
3355 break;
3356
3357 case print_it_done:
3358 /* We still want to print the frame, but we already printed the
4a64f543 3359 relevant messages. */
e514a9d6
JM
3360 return PRINT_SRC_AND_LOC;
3361 break;
3362
3363 case print_it_normal:
4f8d1dc6 3364 {
f431efe5
PA
3365 struct breakpoint *b = bs->breakpoint_at;
3366
1a6a67de
TJB
3367 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3368 which has since been deleted. */
3369 if (b == NULL)
3370 return PRINT_UNKNOWN;
3371
348d480f
PA
3372 /* Normal case. Call the breakpoint's print_it method. */
3373 return b->ops->print_it (bs);
4f8d1dc6 3374 }
348d480f 3375 break;
3086aeae 3376
e514a9d6 3377 default:
8e65ff28 3378 internal_error (__FILE__, __LINE__,
e2e0b3e5 3379 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3380 break;
c906108c 3381 }
c906108c
SS
3382}
3383
e514a9d6
JM
3384/* Print a message indicating what happened. This is called from
3385 normal_stop(). The input to this routine is the head of the bpstat
3386 list - a list of the eventpoints that caused this stop. This
3387 routine calls the generic print routine for printing a message
3388 about reasons for stopping. This will print (for example) the
3389 "Breakpoint n," part of the output. The return value of this
3390 routine is one of:
c906108c 3391
4a64f543 3392 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3393 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3394 code to print the location. An example is
c5aa993b
JM
3395 "Breakpoint 1, " which should be followed by
3396 the location.
917317f4 3397 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3398 to also print the location part of the message.
3399 An example is the catch/throw messages, which
4a64f543 3400 don't require a location appended to the end.
917317f4 3401 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3402 further info to be printed. */
c906108c 3403
917317f4 3404enum print_stop_action
fba45db2 3405bpstat_print (bpstat bs)
c906108c
SS
3406{
3407 int val;
c5aa993b 3408
c906108c 3409 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3410 (Currently all watchpoints go on the bpstat whether hit or not.
3411 That probably could (should) be changed, provided care is taken
c906108c 3412 with respect to bpstat_explains_signal). */
e514a9d6
JM
3413 for (; bs; bs = bs->next)
3414 {
3415 val = print_bp_stop_message (bs);
3416 if (val == PRINT_SRC_ONLY
3417 || val == PRINT_SRC_AND_LOC
3418 || val == PRINT_NOTHING)
3419 return val;
3420 }
c906108c 3421
e514a9d6 3422 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3423 with and nothing was printed. */
917317f4 3424 return PRINT_UNKNOWN;
c906108c
SS
3425}
3426
4a64f543
MS
3427/* Evaluate the expression EXP and return 1 if value is zero. This is
3428 used inside a catch_errors to evaluate the breakpoint condition.
3429 The argument is a "struct expression *" that has been cast to a
3430 "char *" to make it pass through catch_errors. */
c906108c
SS
3431
3432static int
4efb68b1 3433breakpoint_cond_eval (void *exp)
c906108c 3434{
278cd55f 3435 struct value *mark = value_mark ();
c5aa993b 3436 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3437
c906108c
SS
3438 value_free_to_mark (mark);
3439 return i;
3440}
3441
5760d0ab 3442/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3443
3444static bpstat
5760d0ab 3445bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3446{
3447 bpstat bs;
3448
3449 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3450 bs->next = NULL;
3451 **bs_link_pointer = bs;
3452 *bs_link_pointer = &bs->next;
f431efe5
PA
3453 bs->breakpoint_at = bl->owner;
3454 bs->bp_location_at = bl;
3455 incref_bp_location (bl);
c906108c
SS
3456 /* If the condition is false, etc., don't do the commands. */
3457 bs->commands = NULL;
9add0f1b 3458 bs->commands_left = NULL;
c906108c
SS
3459 bs->old_val = NULL;
3460 bs->print_it = print_it_normal;
3461 return bs;
3462}
3463\f
d983da9c
DJ
3464/* The target has stopped with waitstatus WS. Check if any hardware
3465 watchpoints have triggered, according to the target. */
3466
3467int
3468watchpoints_triggered (struct target_waitstatus *ws)
3469{
d92524f1 3470 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3471 CORE_ADDR addr;
3472 struct breakpoint *b;
3473
3474 if (!stopped_by_watchpoint)
3475 {
3476 /* We were not stopped by a watchpoint. Mark all watchpoints
3477 as not triggered. */
3478 ALL_BREAKPOINTS (b)
cc60f2e3 3479 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3480 {
3481 struct watchpoint *w = (struct watchpoint *) b;
3482
3483 w->watchpoint_triggered = watch_triggered_no;
3484 }
d983da9c
DJ
3485
3486 return 0;
3487 }
3488
3489 if (!target_stopped_data_address (&current_target, &addr))
3490 {
3491 /* We were stopped by a watchpoint, but we don't know where.
3492 Mark all watchpoints as unknown. */
3493 ALL_BREAKPOINTS (b)
cc60f2e3 3494 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3495 {
3496 struct watchpoint *w = (struct watchpoint *) b;
3497
3498 w->watchpoint_triggered = watch_triggered_unknown;
3499 }
d983da9c
DJ
3500
3501 return stopped_by_watchpoint;
3502 }
3503
3504 /* The target could report the data address. Mark watchpoints
3505 affected by this data address as triggered, and all others as not
3506 triggered. */
3507
3508 ALL_BREAKPOINTS (b)
cc60f2e3 3509 if (is_hardware_watchpoint (b))
d983da9c 3510 {
3a5c3e22 3511 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 3512 struct bp_location *loc;
d983da9c 3513
3a5c3e22 3514 w->watchpoint_triggered = watch_triggered_no;
a5606eee 3515 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 3516 {
3a5c3e22 3517 if (is_masked_watchpoint (b))
9c06b0b4 3518 {
3a5c3e22
PA
3519 CORE_ADDR newaddr = addr & w->hw_wp_mask;
3520 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
3521
3522 if (newaddr == start)
3523 {
3a5c3e22 3524 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3525 break;
3526 }
3527 }
3528 /* Exact match not required. Within range is sufficient. */
3529 else if (target_watchpoint_addr_within_range (&current_target,
3530 addr, loc->address,
3531 loc->length))
3532 {
3a5c3e22 3533 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3534 break;
3535 }
3536 }
d983da9c
DJ
3537 }
3538
3539 return 1;
3540}
3541
c906108c
SS
3542/* Possible return values for watchpoint_check (this can't be an enum
3543 because of check_errors). */
3544/* The watchpoint has been deleted. */
3545#define WP_DELETED 1
3546/* The value has changed. */
3547#define WP_VALUE_CHANGED 2
3548/* The value has not changed. */
3549#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3550/* Ignore this watchpoint, no matter if the value changed or not. */
3551#define WP_IGNORE 4
c906108c
SS
3552
3553#define BP_TEMPFLAG 1
3554#define BP_HARDWAREFLAG 2
3555
4a64f543
MS
3556/* Evaluate watchpoint condition expression and check if its value
3557 changed.
553e4c11
JB
3558
3559 P should be a pointer to struct bpstat, but is defined as a void *
3560 in order for this function to be usable with catch_errors. */
c906108c
SS
3561
3562static int
4efb68b1 3563watchpoint_check (void *p)
c906108c
SS
3564{
3565 bpstat bs = (bpstat) p;
3a5c3e22 3566 struct watchpoint *b;
c906108c
SS
3567 struct frame_info *fr;
3568 int within_current_scope;
3569
f431efe5 3570 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3571 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 3572 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 3573
f6bc2008
PA
3574 /* If this is a local watchpoint, we only want to check if the
3575 watchpoint frame is in scope if the current thread is the thread
3576 that was used to create the watchpoint. */
3577 if (!watchpoint_in_thread_scope (b))
60e1c644 3578 return WP_IGNORE;
f6bc2008 3579
c906108c
SS
3580 if (b->exp_valid_block == NULL)
3581 within_current_scope = 1;
3582 else
3583 {
edb3359d
DJ
3584 struct frame_info *frame = get_current_frame ();
3585 struct gdbarch *frame_arch = get_frame_arch (frame);
3586 CORE_ADDR frame_pc = get_frame_pc (frame);
3587
4a64f543
MS
3588 /* in_function_epilogue_p() returns a non-zero value if we're
3589 still in the function but the stack frame has already been
3590 invalidated. Since we can't rely on the values of local
3591 variables after the stack has been destroyed, we are treating
3592 the watchpoint in that state as `not changed' without further
3593 checking. Don't mark watchpoints as changed if the current
3594 frame is in an epilogue - even if they are in some other
3595 frame, our view of the stack is likely to be wrong and
3596 frame_find_by_id could error out. */
a0f49112 3597 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3598 return WP_IGNORE;
a0f49112 3599
101dcfbe 3600 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3601 within_current_scope = (fr != NULL);
69fbadd5
DJ
3602
3603 /* If we've gotten confused in the unwinder, we might have
3604 returned a frame that can't describe this variable. */
edb3359d
DJ
3605 if (within_current_scope)
3606 {
3607 struct symbol *function;
3608
3609 function = get_frame_function (fr);
3610 if (function == NULL
3611 || !contained_in (b->exp_valid_block,
3612 SYMBOL_BLOCK_VALUE (function)))
3613 within_current_scope = 0;
3614 }
69fbadd5 3615
edb3359d 3616 if (within_current_scope)
c906108c
SS
3617 /* If we end up stopping, the current frame will get selected
3618 in normal_stop. So this call to select_frame won't affect
3619 the user. */
0f7d239c 3620 select_frame (fr);
c906108c 3621 }
c5aa993b 3622
c906108c
SS
3623 if (within_current_scope)
3624 {
4a64f543
MS
3625 /* We use value_{,free_to_}mark because it could be a *long*
3626 time before we return to the command level and call
3627 free_all_values. We can't call free_all_values because we
3628 might be in the middle of evaluating a function call. */
c906108c 3629
0cf6dd15 3630 int pc = 0;
9c06b0b4 3631 struct value *mark;
fa4727a6
DJ
3632 struct value *new_val;
3633
3a5c3e22 3634 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
3635 /* Since we don't know the exact trigger address (from
3636 stopped_data_address), just tell the user we've triggered
3637 a mask watchpoint. */
3638 return WP_VALUE_CHANGED;
3639
3640 mark = value_mark ();
0cf6dd15 3641 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3642
4a64f543
MS
3643 /* We use value_equal_contents instead of value_equal because
3644 the latter coerces an array to a pointer, thus comparing just
3645 the address of the array instead of its contents. This is
3646 not what we want. */
fa4727a6 3647 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3648 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3649 {
fa4727a6
DJ
3650 if (new_val != NULL)
3651 {
3652 release_value (new_val);
3653 value_free_to_mark (mark);
3654 }
c906108c
SS
3655 bs->old_val = b->val;
3656 b->val = new_val;
fa4727a6 3657 b->val_valid = 1;
c906108c
SS
3658 return WP_VALUE_CHANGED;
3659 }
3660 else
3661 {
60e1c644 3662 /* Nothing changed. */
c906108c 3663 value_free_to_mark (mark);
c906108c
SS
3664 return WP_VALUE_NOT_CHANGED;
3665 }
3666 }
3667 else
3668 {
3669 /* This seems like the only logical thing to do because
c5aa993b
JM
3670 if we temporarily ignored the watchpoint, then when
3671 we reenter the block in which it is valid it contains
3672 garbage (in the case of a function, it may have two
3673 garbage values, one before and one after the prologue).
3674 So we can't even detect the first assignment to it and
3675 watch after that (since the garbage may or may not equal
3676 the first value assigned). */
348d480f
PA
3677 /* We print all the stop information in
3678 breakpoint_ops->print_it, but in this case, by the time we
3679 call breakpoint_ops->print_it this bp will be deleted
3680 already. So we have no choice but print the information
3681 here. */
9dc5e2a9 3682 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3683 ui_out_field_string
3684 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3685 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 3686 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
3687 ui_out_text (uiout,
3688 " deleted because the program has left the block in\n\
8b93c638 3689which its expression is valid.\n");
4ce44c66 3690
cdac0397 3691 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 3692 decref_counted_command_line (&b->base.commands);
d0fb5eae 3693 watchpoint_del_at_next_stop (b);
c906108c
SS
3694
3695 return WP_DELETED;
3696 }
3697}
3698
18a18393 3699/* Return true if it looks like target has stopped due to hitting
348d480f
PA
3700 breakpoint location BL. This function does not check if we should
3701 stop, only if BL explains the stop. */
3702
18a18393 3703static int
6c95b8df
PA
3704bpstat_check_location (const struct bp_location *bl,
3705 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3706{
3707 struct breakpoint *b = bl->owner;
3708
348d480f 3709 /* BL is from an existing breakpoint. */
2bdf28a0
JK
3710 gdb_assert (b != NULL);
3711
348d480f 3712 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
18a18393
VP
3713}
3714
3a5c3e22
PA
3715/* Determine if the watched values have actually changed, and we
3716 should stop. If not, set BS->stop to 0. */
3717
18a18393
VP
3718static void
3719bpstat_check_watchpoint (bpstat bs)
3720{
2bdf28a0 3721 const struct bp_location *bl;
3a5c3e22 3722 struct watchpoint *b;
2bdf28a0
JK
3723
3724 /* BS is built for existing struct breakpoint. */
f431efe5 3725 bl = bs->bp_location_at;
2bdf28a0 3726 gdb_assert (bl != NULL);
3a5c3e22 3727 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 3728 gdb_assert (b != NULL);
18a18393 3729
18a18393 3730 {
18a18393
VP
3731 int must_check_value = 0;
3732
3a5c3e22 3733 if (b->base.type == bp_watchpoint)
18a18393
VP
3734 /* For a software watchpoint, we must always check the
3735 watched value. */
3736 must_check_value = 1;
3737 else if (b->watchpoint_triggered == watch_triggered_yes)
3738 /* We have a hardware watchpoint (read, write, or access)
3739 and the target earlier reported an address watched by
3740 this watchpoint. */
3741 must_check_value = 1;
3742 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 3743 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
3744 /* We were stopped by a hardware watchpoint, but the target could
3745 not report the data address. We must check the watchpoint's
3746 value. Access and read watchpoints are out of luck; without
3747 a data address, we can't figure it out. */
3748 must_check_value = 1;
3a5c3e22 3749
18a18393
VP
3750 if (must_check_value)
3751 {
3e43a32a
MS
3752 char *message
3753 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 3754 b->base.number);
18a18393
VP
3755 struct cleanup *cleanups = make_cleanup (xfree, message);
3756 int e = catch_errors (watchpoint_check, bs, message,
3757 RETURN_MASK_ALL);
3758 do_cleanups (cleanups);
3759 switch (e)
3760 {
3761 case WP_DELETED:
3762 /* We've already printed what needs to be printed. */
3763 bs->print_it = print_it_done;
3764 /* Stop. */
3765 break;
60e1c644
PA
3766 case WP_IGNORE:
3767 bs->print_it = print_it_noop;
3768 bs->stop = 0;
3769 break;
18a18393 3770 case WP_VALUE_CHANGED:
3a5c3e22 3771 if (b->base.type == bp_read_watchpoint)
18a18393 3772 {
85d721b8
PA
3773 /* There are two cases to consider here:
3774
4a64f543 3775 1. We're watching the triggered memory for reads.
85d721b8
PA
3776 In that case, trust the target, and always report
3777 the watchpoint hit to the user. Even though
3778 reads don't cause value changes, the value may
3779 have changed since the last time it was read, and
3780 since we're not trapping writes, we will not see
3781 those, and as such we should ignore our notion of
3782 old value.
3783
4a64f543 3784 2. We're watching the triggered memory for both
85d721b8
PA
3785 reads and writes. There are two ways this may
3786 happen:
3787
4a64f543 3788 2.1. This is a target that can't break on data
85d721b8
PA
3789 reads only, but can break on accesses (reads or
3790 writes), such as e.g., x86. We detect this case
3791 at the time we try to insert read watchpoints.
3792
4a64f543 3793 2.2. Otherwise, the target supports read
85d721b8
PA
3794 watchpoints, but, the user set an access or write
3795 watchpoint watching the same memory as this read
3796 watchpoint.
3797
3798 If we're watching memory writes as well as reads,
3799 ignore watchpoint hits when we find that the
3800 value hasn't changed, as reads don't cause
3801 changes. This still gives false positives when
3802 the program writes the same value to memory as
3803 what there was already in memory (we will confuse
3804 it for a read), but it's much better than
3805 nothing. */
3806
3807 int other_write_watchpoint = 0;
3808
3809 if (bl->watchpoint_type == hw_read)
3810 {
3811 struct breakpoint *other_b;
3812
3813 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
3814 if (other_b->type == bp_hardware_watchpoint
3815 || other_b->type == bp_access_watchpoint)
85d721b8 3816 {
3a5c3e22
PA
3817 struct watchpoint *other_w =
3818 (struct watchpoint *) other_b;
3819
3820 if (other_w->watchpoint_triggered
3821 == watch_triggered_yes)
3822 {
3823 other_write_watchpoint = 1;
3824 break;
3825 }
85d721b8
PA
3826 }
3827 }
3828
3829 if (other_write_watchpoint
3830 || bl->watchpoint_type == hw_access)
3831 {
3832 /* We're watching the same memory for writes,
3833 and the value changed since the last time we
3834 updated it, so this trap must be for a write.
3835 Ignore it. */
3836 bs->print_it = print_it_noop;
3837 bs->stop = 0;
3838 }
18a18393
VP
3839 }
3840 break;
3841 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
3842 if (b->base.type == bp_hardware_watchpoint
3843 || b->base.type == bp_watchpoint)
18a18393
VP
3844 {
3845 /* Don't stop: write watchpoints shouldn't fire if
3846 the value hasn't changed. */
3847 bs->print_it = print_it_noop;
3848 bs->stop = 0;
3849 }
3850 /* Stop. */
3851 break;
3852 default:
3853 /* Can't happen. */
3854 case 0:
3855 /* Error from catch_errors. */
3a5c3e22 3856 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 3857 watchpoint_del_at_next_stop (b);
18a18393
VP
3858 /* We've already printed what needs to be printed. */
3859 bs->print_it = print_it_done;
3860 break;
3861 }
3862 }
3863 else /* must_check_value == 0 */
3864 {
3865 /* This is a case where some watchpoint(s) triggered, but
3866 not at the address of this watchpoint, or else no
3867 watchpoint triggered after all. So don't print
3868 anything for this watchpoint. */
3869 bs->print_it = print_it_noop;
3870 bs->stop = 0;
3871 }
3872 }
3873}
3874
3875
3876/* Check conditions (condition proper, frame, thread and ignore count)
3877 of breakpoint referred to by BS. If we should not stop for this
3878 breakpoint, set BS->stop to 0. */
f431efe5 3879
18a18393
VP
3880static void
3881bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3882{
3883 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
3884 const struct bp_location *bl;
3885 struct breakpoint *b;
3886
3887 /* BS is built for existing struct breakpoint. */
f431efe5 3888 bl = bs->bp_location_at;
2bdf28a0 3889 gdb_assert (bl != NULL);
f431efe5 3890 b = bs->breakpoint_at;
2bdf28a0 3891 gdb_assert (b != NULL);
18a18393
VP
3892
3893 if (frame_id_p (b->frame_id)
edb3359d 3894 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
3895 bs->stop = 0;
3896 else if (bs->stop)
3897 {
3898 int value_is_zero = 0;
60e1c644
PA
3899 struct expression *cond;
3900
7371cf6d
PM
3901 /* Evaluate Python breakpoints that have a "stop"
3902 method implemented. */
3903 if (b->py_bp_object)
3904 bs->stop = gdbpy_should_stop (b->py_bp_object);
3905
60e1c644 3906 if (is_watchpoint (b))
3a5c3e22
PA
3907 {
3908 struct watchpoint *w = (struct watchpoint *) b;
3909
3910 cond = w->cond_exp;
3911 }
60e1c644
PA
3912 else
3913 cond = bl->cond;
3914
f431efe5 3915 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 3916 {
60e1c644 3917 int within_current_scope = 1;
3a5c3e22 3918 struct watchpoint * w;
60e1c644 3919
c5bc3a77
DJ
3920 /* We use value_mark and value_free_to_mark because it could
3921 be a long time before we return to the command level and
3922 call free_all_values. We can't call free_all_values
3923 because we might be in the middle of evaluating a
3924 function call. */
3925 struct value *mark = value_mark ();
3926
3a5c3e22
PA
3927 if (is_watchpoint (b))
3928 w = (struct watchpoint *) b;
3929 else
3930 w = NULL;
3931
edb3359d
DJ
3932 /* Need to select the frame, with all that implies so that
3933 the conditions will have the right context. Because we
3934 use the frame, we will not see an inlined function's
3935 variables when we arrive at a breakpoint at the start
3936 of the inlined function; the current frame will be the
3937 call site. */
3a5c3e22 3938 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
3939 select_frame (get_current_frame ());
3940 else
3941 {
3942 struct frame_info *frame;
3943
3944 /* For local watchpoint expressions, which particular
3945 instance of a local is being watched matters, so we
3946 keep track of the frame to evaluate the expression
3947 in. To evaluate the condition however, it doesn't
3948 really matter which instantiation of the function
3949 where the condition makes sense triggers the
3950 watchpoint. This allows an expression like "watch
3951 global if q > 10" set in `func', catch writes to
3952 global on all threads that call `func', or catch
3953 writes on all recursive calls of `func' by a single
3954 thread. We simply always evaluate the condition in
3955 the innermost frame that's executing where it makes
3956 sense to evaluate the condition. It seems
3957 intuitive. */
3a5c3e22 3958 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
3959 if (frame != NULL)
3960 select_frame (frame);
3961 else
3962 within_current_scope = 0;
3963 }
3964 if (within_current_scope)
3965 value_is_zero
3966 = catch_errors (breakpoint_cond_eval, cond,
3967 "Error in testing breakpoint condition:\n",
3968 RETURN_MASK_ALL);
3969 else
3970 {
3971 warning (_("Watchpoint condition cannot be tested "
3972 "in the current scope"));
3973 /* If we failed to set the right context for this
3974 watchpoint, unconditionally report it. */
3975 value_is_zero = 0;
3976 }
4a64f543 3977 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 3978 value_free_to_mark (mark);
18a18393 3979 }
60e1c644
PA
3980
3981 if (cond && value_is_zero)
18a18393
VP
3982 {
3983 bs->stop = 0;
3984 }
3985 else if (b->thread != -1 && b->thread != thread_id)
3986 {
3987 bs->stop = 0;
3988 }
3989 else if (b->ignore_count > 0)
3990 {
3991 b->ignore_count--;
3992 annotate_ignore_count_change ();
3993 bs->stop = 0;
4a64f543 3994 /* Increase the hit count even though we don't stop. */
18a18393 3995 ++(b->hit_count);
8d3788bd 3996 observer_notify_breakpoint_modified (b);
18a18393
VP
3997 }
3998 }
3999}
4000
4001
9709f61c 4002/* Get a bpstat associated with having just stopped at address
d983da9c 4003 BP_ADDR in thread PTID.
c906108c 4004
d983da9c 4005 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4006 don't understand this stop. Result is a chain of bpstat's such
4007 that:
c906108c 4008
c5aa993b 4009 if we don't understand the stop, the result is a null pointer.
c906108c 4010
c5aa993b 4011 if we understand why we stopped, the result is not null.
c906108c 4012
c5aa993b
JM
4013 Each element of the chain refers to a particular breakpoint or
4014 watchpoint at which we have stopped. (We may have stopped for
4015 several reasons concurrently.)
c906108c 4016
c5aa993b
JM
4017 Each element of the chain has valid next, breakpoint_at,
4018 commands, FIXME??? fields. */
c906108c
SS
4019
4020bpstat
6c95b8df
PA
4021bpstat_stop_status (struct address_space *aspace,
4022 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4023{
0d381245 4024 struct breakpoint *b = NULL;
afe38095 4025 struct bp_location *bl;
20874c92 4026 struct bp_location *loc;
5760d0ab
JK
4027 /* First item of allocated bpstat's. */
4028 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4029 /* Pointer to the last thing in the chain currently. */
5760d0ab 4030 bpstat bs;
20874c92 4031 int ix;
429374b8 4032 int need_remove_insert;
f431efe5 4033 int removed_any;
c906108c 4034
f431efe5
PA
4035 /* First, build the bpstat chain with locations that explain a
4036 target stop, while being careful to not set the target running,
4037 as that may invalidate locations (in particular watchpoint
4038 locations are recreated). Resuming will happen here with
4039 breakpoint conditions or watchpoint expressions that include
4040 inferior function calls. */
c5aa993b 4041
429374b8
JK
4042 ALL_BREAKPOINTS (b)
4043 {
4044 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4045 continue;
a5606eee 4046
429374b8
JK
4047 for (bl = b->loc; bl != NULL; bl = bl->next)
4048 {
4a64f543
MS
4049 /* For hardware watchpoints, we look only at the first
4050 location. The watchpoint_check function will work on the
4051 entire expression, not the individual locations. For
4052 read watchpoints, the watchpoints_triggered function has
4053 checked all locations already. */
429374b8
JK
4054 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4055 break;
18a18393 4056
429374b8
JK
4057 if (bl->shlib_disabled)
4058 continue;
c5aa993b 4059
429374b8
JK
4060 if (!bpstat_check_location (bl, aspace, bp_addr))
4061 continue;
c5aa993b 4062
4a64f543
MS
4063 /* Come here if it's a watchpoint, or if the break address
4064 matches. */
c5aa993b 4065
4a64f543
MS
4066 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4067 explain stop. */
c5aa993b 4068
f431efe5
PA
4069 /* Assume we stop. Should we find a watchpoint that is not
4070 actually triggered, or if the condition of the breakpoint
4071 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4072 bs->stop = 1;
4073 bs->print = 1;
d983da9c 4074
f431efe5
PA
4075 /* If this is a scope breakpoint, mark the associated
4076 watchpoint as triggered so that we will handle the
4077 out-of-scope event. We'll get to the watchpoint next
4078 iteration. */
d0fb5eae 4079 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4080 {
4081 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4082
4083 w->watchpoint_triggered = watch_triggered_yes;
4084 }
f431efe5
PA
4085 }
4086 }
4087
4088 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4089 {
f1310107 4090 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4091 {
5760d0ab 4092 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4093 /* For hits of moribund locations, we should just proceed. */
4094 bs->stop = 0;
4095 bs->print = 0;
4096 bs->print_it = print_it_noop;
4097 }
4098 }
4099
f431efe5
PA
4100 /* Now go through the locations that caused the target to stop, and
4101 check whether we're interested in reporting this stop to higher
4102 layers, or whether we should resume the target transparently. */
4103
4104 removed_any = 0;
4105
5760d0ab 4106 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4107 {
4108 if (!bs->stop)
4109 continue;
4110
f431efe5 4111 b = bs->breakpoint_at;
348d480f
PA
4112 b->ops->check_status (bs);
4113 if (bs->stop)
28010a5d 4114 {
348d480f 4115 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4116
429374b8
JK
4117 if (bs->stop)
4118 {
4119 ++(b->hit_count);
8d3788bd 4120 observer_notify_breakpoint_modified (b);
c906108c 4121
4a64f543 4122 /* We will stop here. */
429374b8
JK
4123 if (b->disposition == disp_disable)
4124 {
4125 if (b->enable_state != bp_permanent)
4126 b->enable_state = bp_disabled;
f431efe5 4127 removed_any = 1;
429374b8
JK
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
e5dd4106 4145 /* Print nothing for this entry if we don't stop or don't print. */
429374b8
JK
4146 if (bs->stop == 0 || bs->print == 0)
4147 bs->print_it = print_it_noop;
348d480f 4148 }
429374b8 4149 }
876fa593 4150
d983da9c
DJ
4151 /* If we aren't stopping, the value of some hardware watchpoint may
4152 not have changed, but the intermediate memory locations we are
4153 watching may have. Don't bother if we're stopping; this will get
4154 done later. */
d832cb68 4155 need_remove_insert = 0;
5760d0ab
JK
4156 if (! bpstat_causes_stop (bs_head))
4157 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4158 if (!bs->stop
f431efe5
PA
4159 && bs->breakpoint_at
4160 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4161 {
3a5c3e22
PA
4162 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4163
4164 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 4165 need_remove_insert = 1;
d983da9c
DJ
4166 }
4167
d832cb68 4168 if (need_remove_insert)
2d134ed3 4169 update_global_location_list (1);
f431efe5
PA
4170 else if (removed_any)
4171 update_global_location_list (0);
d832cb68 4172
5760d0ab 4173 return bs_head;
c906108c 4174}
628fe4e4
JK
4175
4176static void
4177handle_jit_event (void)
4178{
4179 struct frame_info *frame;
4180 struct gdbarch *gdbarch;
4181
4182 /* Switch terminal for any messages produced by
4183 breakpoint_re_set. */
4184 target_terminal_ours_for_output ();
4185
4186 frame = get_current_frame ();
4187 gdbarch = get_frame_arch (frame);
4188
4189 jit_event_handler (gdbarch);
4190
4191 target_terminal_inferior ();
4192}
4193
4194/* Prepare WHAT final decision for infrun. */
4195
4196/* Decide what infrun needs to do with this bpstat. */
4197
c906108c 4198struct bpstat_what
0e30163f 4199bpstat_what (bpstat bs_head)
c906108c 4200{
c906108c 4201 struct bpstat_what retval;
628fe4e4
JK
4202 /* We need to defer calling `solib_add', as adding new symbols
4203 resets breakpoints, which in turn deletes breakpoint locations,
4204 and hence may clear unprocessed entries in the BS chain. */
4205 int shlib_event = 0;
4206 int jit_event = 0;
0e30163f 4207 bpstat bs;
c906108c 4208
628fe4e4 4209 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4210 retval.call_dummy = STOP_NONE;
186c406b 4211 retval.is_longjmp = 0;
628fe4e4 4212
0e30163f 4213 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4214 {
628fe4e4
JK
4215 /* Extract this BS's action. After processing each BS, we check
4216 if its action overrides all we've seem so far. */
4217 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4218 enum bptype bptype;
4219
c906108c 4220 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4221 {
4222 /* I suspect this can happen if it was a momentary
4223 breakpoint which has since been deleted. */
4224 bptype = bp_none;
4225 }
20874c92 4226 else
f431efe5 4227 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4228
4229 switch (bptype)
c906108c
SS
4230 {
4231 case bp_none:
628fe4e4 4232 break;
c906108c
SS
4233 case bp_breakpoint:
4234 case bp_hardware_breakpoint:
4235 case bp_until:
4236 case bp_finish:
4237 if (bs->stop)
4238 {
4239 if (bs->print)
628fe4e4 4240 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4241 else
628fe4e4 4242 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4243 }
4244 else
628fe4e4 4245 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4246 break;
4247 case bp_watchpoint:
4248 case bp_hardware_watchpoint:
4249 case bp_read_watchpoint:
4250 case bp_access_watchpoint:
4251 if (bs->stop)
4252 {
4253 if (bs->print)
628fe4e4 4254 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4255 else
628fe4e4 4256 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4257 }
4258 else
628fe4e4
JK
4259 {
4260 /* There was a watchpoint, but we're not stopping.
4261 This requires no further action. */
4262 }
c906108c
SS
4263 break;
4264 case bp_longjmp:
186c406b 4265 case bp_exception:
628fe4e4 4266 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4267 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4268 break;
4269 case bp_longjmp_resume:
186c406b 4270 case bp_exception_resume:
628fe4e4 4271 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4272 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4273 break;
4274 case bp_step_resume:
4275 if (bs->stop)
628fe4e4
JK
4276 this_action = BPSTAT_WHAT_STEP_RESUME;
4277 else
c906108c 4278 {
628fe4e4
JK
4279 /* It is for the wrong frame. */
4280 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4281 }
c906108c 4282 break;
2c03e5be
PA
4283 case bp_hp_step_resume:
4284 if (bs->stop)
4285 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4286 else
4287 {
4288 /* It is for the wrong frame. */
4289 this_action = BPSTAT_WHAT_SINGLE;
4290 }
4291 break;
c906108c 4292 case bp_watchpoint_scope:
c4093a6a 4293 case bp_thread_event:
1900040c 4294 case bp_overlay_event:
0fd8e87f 4295 case bp_longjmp_master:
aa7d318d 4296 case bp_std_terminate_master:
186c406b 4297 case bp_exception_master:
628fe4e4 4298 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4299 break;
ce78b96d 4300 case bp_catchpoint:
c5aa993b
JM
4301 if (bs->stop)
4302 {
4303 if (bs->print)
628fe4e4 4304 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4305 else
628fe4e4 4306 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4307 }
4308 else
628fe4e4
JK
4309 {
4310 /* There was a catchpoint, but we're not stopping.
4311 This requires no further action. */
4312 }
4313 break;
4314 case bp_shlib_event:
4315 shlib_event = 1;
4316
4317 /* If requested, stop when the dynamic linker notifies GDB
4318 of events. This allows the user to get control and place
4319 breakpoints in initializer routines for dynamically
4320 loaded objects (among other things). */
4321 if (stop_on_solib_events)
4322 this_action = BPSTAT_WHAT_STOP_NOISY;
4323 else
4324 this_action = BPSTAT_WHAT_SINGLE;
4325 break;
4326 case bp_jit_event:
4327 jit_event = 1;
4328 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4329 break;
c906108c 4330 case bp_call_dummy:
53a5351d
JM
4331 /* Make sure the action is stop (silent or noisy),
4332 so infrun.c pops the dummy frame. */
aa7d318d 4333 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4334 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4335 break;
4336 case bp_std_terminate:
4337 /* Make sure the action is stop (silent or noisy),
4338 so infrun.c pops the dummy frame. */
aa7d318d 4339 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4340 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4341 break;
1042e4c0 4342 case bp_tracepoint:
7a697b8d 4343 case bp_fast_tracepoint:
0fb4aa4b 4344 case bp_static_tracepoint:
1042e4c0
SS
4345 /* Tracepoint hits should not be reported back to GDB, and
4346 if one got through somehow, it should have been filtered
4347 out already. */
4348 internal_error (__FILE__, __LINE__,
7a697b8d 4349 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4350 break;
4351 case bp_gnu_ifunc_resolver:
4352 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4353 this_action = BPSTAT_WHAT_SINGLE;
4354 break;
4355 case bp_gnu_ifunc_resolver_return:
4356 /* The breakpoint will be removed, execution will restart from the
4357 PC of the former breakpoint. */
4358 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4359 break;
628fe4e4
JK
4360 default:
4361 internal_error (__FILE__, __LINE__,
4362 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4363 }
628fe4e4
JK
4364
4365 retval.main_action = max (retval.main_action, this_action);
c906108c 4366 }
628fe4e4 4367
0e30163f
JK
4368 /* These operations may affect the bs->breakpoint_at state so they are
4369 delayed after MAIN_ACTION is decided above. */
4370
628fe4e4
JK
4371 if (shlib_event)
4372 {
4373 if (debug_infrun)
4374 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4375
4376 /* Check for any newly added shared libraries if we're supposed
4377 to be adding them automatically. */
4378
4379 /* Switch terminal for any messages produced by
4380 breakpoint_re_set. */
4381 target_terminal_ours_for_output ();
4382
4383#ifdef SOLIB_ADD
4384 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4385#else
4386 solib_add (NULL, 0, &current_target, auto_solib_add);
4387#endif
4388
4389 target_terminal_inferior ();
4390 }
4391
4392 if (jit_event)
4393 {
4394 if (debug_infrun)
4395 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4396
4397 handle_jit_event ();
4398 }
4399
0e30163f
JK
4400 for (bs = bs_head; bs != NULL; bs = bs->next)
4401 {
4402 struct breakpoint *b = bs->breakpoint_at;
4403
4404 if (b == NULL)
4405 continue;
4406 switch (b->type)
4407 {
4408 case bp_gnu_ifunc_resolver:
4409 gnu_ifunc_resolver_stop (b);
4410 break;
4411 case bp_gnu_ifunc_resolver_return:
4412 gnu_ifunc_resolver_return_stop (b);
4413 break;
4414 }
4415 }
4416
c906108c
SS
4417 return retval;
4418}
4419
4420/* Nonzero if we should step constantly (e.g. watchpoints on machines
4421 without hardware support). This isn't related to a specific bpstat,
4422 just to things like whether watchpoints are set. */
4423
c5aa993b 4424int
fba45db2 4425bpstat_should_step (void)
c906108c
SS
4426{
4427 struct breakpoint *b;
cc59ec59 4428
c906108c 4429 ALL_BREAKPOINTS (b)
717a8278 4430 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4431 return 1;
c906108c
SS
4432 return 0;
4433}
4434
67822962
PA
4435int
4436bpstat_causes_stop (bpstat bs)
4437{
4438 for (; bs != NULL; bs = bs->next)
4439 if (bs->stop)
4440 return 1;
4441
4442 return 0;
4443}
4444
c906108c 4445\f
c5aa993b 4446
170b53b2
UW
4447/* Compute a string of spaces suitable to indent the next line
4448 so it starts at the position corresponding to the table column
4449 named COL_NAME in the currently active table of UIOUT. */
4450
4451static char *
4452wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4453{
4454 static char wrap_indent[80];
4455 int i, total_width, width, align;
4456 char *text;
4457
4458 total_width = 0;
4459 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4460 {
4461 if (strcmp (text, col_name) == 0)
4462 {
4463 gdb_assert (total_width < sizeof wrap_indent);
4464 memset (wrap_indent, ' ', total_width);
4465 wrap_indent[total_width] = 0;
4466
4467 return wrap_indent;
4468 }
4469
4470 total_width += width + 1;
4471 }
4472
4473 return NULL;
4474}
4475
859825b8
JK
4476/* Print the LOC location out of the list of B->LOC locations. */
4477
170b53b2
UW
4478static void
4479print_breakpoint_location (struct breakpoint *b,
4480 struct bp_location *loc)
0d381245 4481{
6c95b8df
PA
4482 struct cleanup *old_chain = save_current_program_space ();
4483
859825b8
JK
4484 if (loc != NULL && loc->shlib_disabled)
4485 loc = NULL;
4486
6c95b8df
PA
4487 if (loc != NULL)
4488 set_current_program_space (loc->pspace);
4489
56435ebe
TT
4490 if (b->display_canonical)
4491 ui_out_field_string (uiout, "what", b->addr_string);
4492 else if (b->source_file && loc)
0d381245
VP
4493 {
4494 struct symbol *sym
4495 = find_pc_sect_function (loc->address, loc->section);
4496 if (sym)
4497 {
4498 ui_out_text (uiout, "in ");
4499 ui_out_field_string (uiout, "func",
4500 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4501 ui_out_text (uiout, " ");
4502 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4503 ui_out_text (uiout, "at ");
0d381245
VP
4504 }
4505 ui_out_field_string (uiout, "file", b->source_file);
4506 ui_out_text (uiout, ":");
4507
4508 if (ui_out_is_mi_like_p (uiout))
4509 {
4510 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4511 char *fullname = symtab_to_fullname (sal.symtab);
4512
4513 if (fullname)
4514 ui_out_field_string (uiout, "fullname", fullname);
4515 }
4516
4517 ui_out_field_int (uiout, "line", b->line_number);
4518 }
859825b8 4519 else if (loc)
0d381245 4520 {
170b53b2
UW
4521 struct ui_stream *stb = ui_out_stream_new (uiout);
4522 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4523
22e722e1
DJ
4524 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4525 demangle, "");
0d381245 4526 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4527
4528 do_cleanups (stb_chain);
0d381245 4529 }
859825b8
JK
4530 else
4531 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4532
4533 do_cleanups (old_chain);
0d381245
VP
4534}
4535
269b11a2
PA
4536static const char *
4537bptype_string (enum bptype type)
c906108c 4538{
c4093a6a
JM
4539 struct ep_type_description
4540 {
4541 enum bptype type;
4542 char *description;
4543 };
4544 static struct ep_type_description bptypes[] =
c906108c 4545 {
c5aa993b
JM
4546 {bp_none, "?deleted?"},
4547 {bp_breakpoint, "breakpoint"},
c906108c 4548 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4549 {bp_until, "until"},
4550 {bp_finish, "finish"},
4551 {bp_watchpoint, "watchpoint"},
c906108c 4552 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4553 {bp_read_watchpoint, "read watchpoint"},
4554 {bp_access_watchpoint, "acc watchpoint"},
4555 {bp_longjmp, "longjmp"},
4556 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4557 {bp_exception, "exception"},
4558 {bp_exception_resume, "exception resume"},
c5aa993b 4559 {bp_step_resume, "step resume"},
2c03e5be 4560 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
4561 {bp_watchpoint_scope, "watchpoint scope"},
4562 {bp_call_dummy, "call dummy"},
aa7d318d 4563 {bp_std_terminate, "std::terminate"},
c5aa993b 4564 {bp_shlib_event, "shlib events"},
c4093a6a 4565 {bp_thread_event, "thread events"},
1900040c 4566 {bp_overlay_event, "overlay events"},
0fd8e87f 4567 {bp_longjmp_master, "longjmp master"},
aa7d318d 4568 {bp_std_terminate_master, "std::terminate master"},
186c406b 4569 {bp_exception_master, "exception master"},
ce78b96d 4570 {bp_catchpoint, "catchpoint"},
1042e4c0 4571 {bp_tracepoint, "tracepoint"},
7a697b8d 4572 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4573 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4574 {bp_jit_event, "jit events"},
0e30163f
JK
4575 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4576 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4577 };
269b11a2
PA
4578
4579 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4580 || ((int) type != bptypes[(int) type].type))
4581 internal_error (__FILE__, __LINE__,
4582 _("bptypes table does not describe type #%d."),
4583 (int) type);
4584
4585 return bptypes[(int) type].description;
4586}
4587
4588/* Print B to gdb_stdout. */
4589
4590static void
4591print_one_breakpoint_location (struct breakpoint *b,
4592 struct bp_location *loc,
4593 int loc_number,
4594 struct bp_location **last_loc,
269b11a2
PA
4595 int allflag)
4596{
4597 struct command_line *l;
c2c6d25f 4598 static char bpenables[] = "nynny";
c906108c 4599
0d381245
VP
4600 int header_of_multiple = 0;
4601 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4602 struct value_print_options opts;
4603
4604 get_user_print_options (&opts);
0d381245
VP
4605
4606 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4607 /* See comment in print_one_breakpoint concerning treatment of
4608 breakpoints with single disabled location. */
0d381245
VP
4609 if (loc == NULL
4610 && (b->loc != NULL
4611 && (b->loc->next != NULL || !b->loc->enabled)))
4612 header_of_multiple = 1;
4613 if (loc == NULL)
4614 loc = b->loc;
4615
c4093a6a
JM
4616 annotate_record ();
4617
4618 /* 1 */
4619 annotate_field (0);
0d381245
VP
4620 if (part_of_multiple)
4621 {
4622 char *formatted;
0c6773c1 4623 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4624 ui_out_field_string (uiout, "number", formatted);
4625 xfree (formatted);
4626 }
4627 else
4628 {
4629 ui_out_field_int (uiout, "number", b->number);
4630 }
c4093a6a
JM
4631
4632 /* 2 */
4633 annotate_field (1);
0d381245
VP
4634 if (part_of_multiple)
4635 ui_out_field_skip (uiout, "type");
269b11a2
PA
4636 else
4637 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4638
4639 /* 3 */
4640 annotate_field (2);
0d381245
VP
4641 if (part_of_multiple)
4642 ui_out_field_skip (uiout, "disp");
4643 else
2cec12e5 4644 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4645
c4093a6a
JM
4646
4647 /* 4 */
4648 annotate_field (3);
0d381245 4649 if (part_of_multiple)
54e52265 4650 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4651 else
4a64f543
MS
4652 ui_out_field_fmt (uiout, "enabled", "%c",
4653 bpenables[(int) b->enable_state]);
54e52265 4654 ui_out_spaces (uiout, 2);
0d381245 4655
c4093a6a
JM
4656
4657 /* 5 and 6 */
3086aeae 4658 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4659 {
4a64f543
MS
4660 /* Although the print_one can possibly print all locations,
4661 calling it here is not likely to get any nice result. So,
4662 make sure there's just one location. */
0d381245 4663 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4664 b->ops->print_one (b, last_loc);
0d381245 4665 }
3086aeae
DJ
4666 else
4667 switch (b->type)
4668 {
4669 case bp_none:
4670 internal_error (__FILE__, __LINE__,
e2e0b3e5 4671 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4672 break;
c906108c 4673
3086aeae
DJ
4674 case bp_watchpoint:
4675 case bp_hardware_watchpoint:
4676 case bp_read_watchpoint:
4677 case bp_access_watchpoint:
3a5c3e22
PA
4678 {
4679 struct watchpoint *w = (struct watchpoint *) b;
4680
4681 /* Field 4, the address, is omitted (which makes the columns
4682 not line up too nicely with the headers, but the effect
4683 is relatively readable). */
4684 if (opts.addressprint)
4685 ui_out_field_skip (uiout, "addr");
4686 annotate_field (5);
4687 ui_out_field_string (uiout, "what", w->exp_string);
4688 }
3086aeae
DJ
4689 break;
4690
3086aeae
DJ
4691 case bp_breakpoint:
4692 case bp_hardware_breakpoint:
4693 case bp_until:
4694 case bp_finish:
4695 case bp_longjmp:
4696 case bp_longjmp_resume:
186c406b
TT
4697 case bp_exception:
4698 case bp_exception_resume:
3086aeae 4699 case bp_step_resume:
2c03e5be 4700 case bp_hp_step_resume:
3086aeae
DJ
4701 case bp_watchpoint_scope:
4702 case bp_call_dummy:
aa7d318d 4703 case bp_std_terminate:
3086aeae
DJ
4704 case bp_shlib_event:
4705 case bp_thread_event:
4706 case bp_overlay_event:
0fd8e87f 4707 case bp_longjmp_master:
aa7d318d 4708 case bp_std_terminate_master:
186c406b 4709 case bp_exception_master:
1042e4c0 4710 case bp_tracepoint:
7a697b8d 4711 case bp_fast_tracepoint:
0fb4aa4b 4712 case bp_static_tracepoint:
4efc6507 4713 case bp_jit_event:
0e30163f
JK
4714 case bp_gnu_ifunc_resolver:
4715 case bp_gnu_ifunc_resolver_return:
79a45b7d 4716 if (opts.addressprint)
3086aeae
DJ
4717 {
4718 annotate_field (4);
54e52265 4719 if (header_of_multiple)
0d381245 4720 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4721 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4722 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4723 else
5af949e3
UW
4724 ui_out_field_core_addr (uiout, "addr",
4725 loc->gdbarch, loc->address);
3086aeae
DJ
4726 }
4727 annotate_field (5);
0d381245 4728 if (!header_of_multiple)
170b53b2 4729 print_breakpoint_location (b, loc);
0d381245 4730 if (b->loc)
a6d9a66e 4731 *last_loc = b->loc;
3086aeae
DJ
4732 break;
4733 }
c906108c 4734
6c95b8df
PA
4735
4736 /* For backward compatibility, don't display inferiors unless there
4737 are several. */
4738 if (loc != NULL
4739 && !header_of_multiple
4740 && (allflag
4741 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4742 && (number_of_program_spaces () > 1
4743 || number_of_inferiors () > 1)
4a64f543
MS
4744 /* LOC is for existing B, it cannot be in
4745 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4746 && loc->owner->type != bp_catchpoint)))
4747 {
4748 struct inferior *inf;
4749 int first = 1;
4750
4751 for (inf = inferior_list; inf != NULL; inf = inf->next)
4752 {
4753 if (inf->pspace == loc->pspace)
4754 {
4755 if (first)
4756 {
4757 first = 0;
4758 ui_out_text (uiout, " inf ");
4759 }
4760 else
4761 ui_out_text (uiout, ", ");
4762 ui_out_text (uiout, plongest (inf->num));
4763 }
4764 }
4765 }
4766
4a306c9a 4767 if (!part_of_multiple)
c4093a6a 4768 {
4a306c9a
JB
4769 if (b->thread != -1)
4770 {
4771 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4772 "stop only in" line a little further down. */
4a306c9a
JB
4773 ui_out_text (uiout, " thread ");
4774 ui_out_field_int (uiout, "thread", b->thread);
4775 }
4776 else if (b->task != 0)
4777 {
4778 ui_out_text (uiout, " task ");
4779 ui_out_field_int (uiout, "task", b->task);
4780 }
c4093a6a 4781 }
f1310107 4782
8b93c638 4783 ui_out_text (uiout, "\n");
f1310107 4784
348d480f 4785 if (!part_of_multiple)
f1310107
TJB
4786 b->ops->print_one_detail (b, uiout);
4787
0d381245 4788 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4789 {
4790 annotate_field (6);
8b93c638 4791 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 4792 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 4793 the frame ID. */
5af949e3
UW
4794 ui_out_field_core_addr (uiout, "frame",
4795 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4796 ui_out_text (uiout, "\n");
c4093a6a
JM
4797 }
4798
28010a5d 4799 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
4800 {
4801 annotate_field (7);
d77f58be 4802 if (is_tracepoint (b))
1042e4c0
SS
4803 ui_out_text (uiout, "\ttrace only if ");
4804 else
4805 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4806 ui_out_field_string (uiout, "cond", b->cond_string);
4807 ui_out_text (uiout, "\n");
4808 }
4809
0d381245 4810 if (!part_of_multiple && b->thread != -1)
c4093a6a 4811 {
4a64f543 4812 /* FIXME should make an annotation for this. */
8b93c638
JM
4813 ui_out_text (uiout, "\tstop only in thread ");
4814 ui_out_field_int (uiout, "thread", b->thread);
4815 ui_out_text (uiout, "\n");
c4093a6a
JM
4816 }
4817
63c715c6 4818 if (!part_of_multiple && b->hit_count)
c4093a6a 4819 {
4a64f543 4820 /* FIXME should make an annotation for this. */
8b93c638
JM
4821 if (ep_is_catchpoint (b))
4822 ui_out_text (uiout, "\tcatchpoint");
4823 else
4824 ui_out_text (uiout, "\tbreakpoint");
4825 ui_out_text (uiout, " already hit ");
4826 ui_out_field_int (uiout, "times", b->hit_count);
4827 if (b->hit_count == 1)
4828 ui_out_text (uiout, " time\n");
4829 else
4830 ui_out_text (uiout, " times\n");
c4093a6a
JM
4831 }
4832
4a64f543
MS
4833 /* Output the count also if it is zero, but only if this is mi.
4834 FIXME: Should have a better test for this. */
9dc5e2a9 4835 if (ui_out_is_mi_like_p (uiout))
63c715c6 4836 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4837 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4838
0d381245 4839 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4840 {
4841 annotate_field (8);
8b93c638
JM
4842 ui_out_text (uiout, "\tignore next ");
4843 ui_out_field_int (uiout, "ignore", b->ignore_count);
4844 ui_out_text (uiout, " hits\n");
c4093a6a 4845 }
059fb39f 4846
9add0f1b 4847 l = b->commands ? b->commands->commands : NULL;
059fb39f 4848 if (!part_of_multiple && l)
c4093a6a 4849 {
3b31d625
EZ
4850 struct cleanup *script_chain;
4851
c4093a6a 4852 annotate_field (9);
3b31d625 4853 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4854 print_command_lines (uiout, l, 4);
3b31d625 4855 do_cleanups (script_chain);
c4093a6a 4856 }
d24317b4 4857
d9b3f62e 4858 if (is_tracepoint (b))
1042e4c0 4859 {
d9b3f62e
PA
4860 struct tracepoint *t = (struct tracepoint *) b;
4861
4862 if (!part_of_multiple && t->pass_count)
4863 {
4864 annotate_field (10);
4865 ui_out_text (uiout, "\tpass count ");
4866 ui_out_field_int (uiout, "pass", t->pass_count);
4867 ui_out_text (uiout, " \n");
4868 }
1042e4c0
SS
4869 }
4870
d24317b4
VP
4871 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4872 {
3a5c3e22
PA
4873 if (is_watchpoint (b))
4874 {
4875 struct watchpoint *w = (struct watchpoint *) b;
4876
4877 ui_out_field_string (uiout, "original-location", w->exp_string);
4878 }
4879 else if (b->addr_string)
d24317b4 4880 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 4881 }
c4093a6a 4882}
c5aa993b 4883
0d381245
VP
4884static void
4885print_one_breakpoint (struct breakpoint *b,
4a64f543 4886 struct bp_location **last_loc,
6c95b8df 4887 int allflag)
0d381245 4888{
8d3788bd
VP
4889 struct cleanup *bkpt_chain;
4890
4891 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
4892
12c5a436 4893 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 4894 do_cleanups (bkpt_chain);
0d381245
VP
4895
4896 /* If this breakpoint has custom print function,
4897 it's already printed. Otherwise, print individual
4898 locations, if any. */
4899 if (b->ops == NULL || b->ops->print_one == NULL)
4900 {
4a64f543
MS
4901 /* If breakpoint has a single location that is disabled, we
4902 print it as if it had several locations, since otherwise it's
4903 hard to represent "breakpoint enabled, location disabled"
4904 situation.
4905
4906 Note that while hardware watchpoints have several locations
a3be7890 4907 internally, that's not a property exposed to user. */
0d381245 4908 if (b->loc
a5606eee 4909 && !is_hardware_watchpoint (b)
8d3788bd 4910 && (b->loc->next || !b->loc->enabled))
0d381245
VP
4911 {
4912 struct bp_location *loc;
4913 int n = 1;
8d3788bd 4914
0d381245 4915 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
4916 {
4917 struct cleanup *inner2 =
4918 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
4919 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
4920 do_cleanups (inner2);
4921 }
0d381245
VP
4922 }
4923 }
4924}
4925
a6d9a66e
UW
4926static int
4927breakpoint_address_bits (struct breakpoint *b)
4928{
4929 int print_address_bits = 0;
4930 struct bp_location *loc;
4931
4932 for (loc = b->loc; loc; loc = loc->next)
4933 {
c7437ca6
PA
4934 int addr_bit;
4935
4936 /* Software watchpoints that aren't watching memory don't have
4937 an address to print. */
4938 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
4939 continue;
4940
4941 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
4942 if (addr_bit > print_address_bits)
4943 print_address_bits = addr_bit;
4944 }
4945
4946 return print_address_bits;
4947}
0d381245 4948
c4093a6a
JM
4949struct captured_breakpoint_query_args
4950 {
4951 int bnum;
4952 };
c5aa993b 4953
c4093a6a 4954static int
2b65245e 4955do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
4956{
4957 struct captured_breakpoint_query_args *args = data;
52f0bd74 4958 struct breakpoint *b;
a6d9a66e 4959 struct bp_location *dummy_loc = NULL;
cc59ec59 4960
c4093a6a
JM
4961 ALL_BREAKPOINTS (b)
4962 {
4963 if (args->bnum == b->number)
c5aa993b 4964 {
12c5a436 4965 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 4966 return GDB_RC_OK;
c5aa993b 4967 }
c4093a6a
JM
4968 }
4969 return GDB_RC_NONE;
4970}
c5aa993b 4971
c4093a6a 4972enum gdb_rc
4a64f543
MS
4973gdb_breakpoint_query (struct ui_out *uiout, int bnum,
4974 char **error_message)
c4093a6a
JM
4975{
4976 struct captured_breakpoint_query_args args;
cc59ec59 4977
c4093a6a
JM
4978 args.bnum = bnum;
4979 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 4980 an error. */
b0b13bb4
DJ
4981 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
4982 error_message, RETURN_MASK_ALL) < 0)
4983 return GDB_RC_FAIL;
4984 else
4985 return GDB_RC_OK;
c4093a6a 4986}
c5aa993b 4987
09d682a4
TT
4988/* Return true if this breakpoint was set by the user, false if it is
4989 internal or momentary. */
4990
4991int
4992user_breakpoint_p (struct breakpoint *b)
4993{
46c6471b 4994 return b->number > 0;
09d682a4
TT
4995}
4996
7f3b0473 4997/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
4998 number BNUM. If BNUM is -1 print all user-settable breakpoints.
4999 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5000 FILTER is non-NULL, call it on each breakpoint and only include the
5001 ones for which it returns non-zero. Return the total number of
5002 breakpoints listed. */
c906108c 5003
d77f58be 5004static int
e5a67952 5005breakpoint_1 (char *args, int allflag,
4a64f543 5006 int (*filter) (const struct breakpoint *))
c4093a6a 5007{
52f0bd74 5008 struct breakpoint *b;
a6d9a66e 5009 struct bp_location *last_loc = NULL;
7f3b0473 5010 int nr_printable_breakpoints;
3b31d625 5011 struct cleanup *bkpttbl_chain;
79a45b7d 5012 struct value_print_options opts;
a6d9a66e 5013 int print_address_bits = 0;
269b11a2
PA
5014 int print_type_col_width = 14;
5015
79a45b7d
TT
5016 get_user_print_options (&opts);
5017
4a64f543
MS
5018 /* Compute the number of rows in the table, as well as the size
5019 required for address fields. */
7f3b0473
AC
5020 nr_printable_breakpoints = 0;
5021 ALL_BREAKPOINTS (b)
e5a67952
MS
5022 {
5023 /* If we have a filter, only list the breakpoints it accepts. */
5024 if (filter && !filter (b))
5025 continue;
5026
5027 /* If we have an "args" string, it is a list of breakpoints to
5028 accept. Skip the others. */
5029 if (args != NULL && *args != '\0')
5030 {
5031 if (allflag && parse_and_eval_long (args) != b->number)
5032 continue;
5033 if (!allflag && !number_is_in_list (args, b->number))
5034 continue;
5035 }
269b11a2 5036
e5a67952
MS
5037 if (allflag || user_breakpoint_p (b))
5038 {
5039 int addr_bit, type_len;
a6d9a66e 5040
e5a67952
MS
5041 addr_bit = breakpoint_address_bits (b);
5042 if (addr_bit > print_address_bits)
5043 print_address_bits = addr_bit;
269b11a2 5044
e5a67952
MS
5045 type_len = strlen (bptype_string (b->type));
5046 if (type_len > print_type_col_width)
5047 print_type_col_width = type_len;
5048
5049 nr_printable_breakpoints++;
5050 }
5051 }
7f3b0473 5052
79a45b7d 5053 if (opts.addressprint)
3b31d625 5054 bkpttbl_chain
3e43a32a
MS
5055 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5056 nr_printable_breakpoints,
3b31d625 5057 "BreakpointTable");
8b93c638 5058 else
3b31d625 5059 bkpttbl_chain
3e43a32a
MS
5060 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5061 nr_printable_breakpoints,
3b31d625 5062 "BreakpointTable");
8b93c638 5063
7f3b0473 5064 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5065 annotate_breakpoints_headers ();
5066 if (nr_printable_breakpoints > 0)
5067 annotate_field (0);
4a64f543 5068 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5069 if (nr_printable_breakpoints > 0)
5070 annotate_field (1);
269b11a2 5071 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5072 "type", "Type"); /* 2 */
d7faa9e7
AC
5073 if (nr_printable_breakpoints > 0)
5074 annotate_field (2);
4a64f543 5075 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5076 if (nr_printable_breakpoints > 0)
5077 annotate_field (3);
54e52265 5078 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5079 if (opts.addressprint)
e5a67952
MS
5080 {
5081 if (nr_printable_breakpoints > 0)
5082 annotate_field (4);
5083 if (print_address_bits <= 32)
5084 ui_out_table_header (uiout, 10, ui_left,
5085 "addr", "Address"); /* 5 */
5086 else
5087 ui_out_table_header (uiout, 18, ui_left,
5088 "addr", "Address"); /* 5 */
5089 }
d7faa9e7
AC
5090 if (nr_printable_breakpoints > 0)
5091 annotate_field (5);
5092 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5093 ui_out_table_body (uiout);
5094 if (nr_printable_breakpoints > 0)
5095 annotate_breakpoints_table ();
7f3b0473 5096
c4093a6a 5097 ALL_BREAKPOINTS (b)
e5a67952
MS
5098 {
5099 QUIT;
5100 /* If we have a filter, only list the breakpoints it accepts. */
5101 if (filter && !filter (b))
5102 continue;
5103
5104 /* If we have an "args" string, it is a list of breakpoints to
5105 accept. Skip the others. */
5106
5107 if (args != NULL && *args != '\0')
5108 {
5109 if (allflag) /* maintenance info breakpoint */
5110 {
5111 if (parse_and_eval_long (args) != b->number)
5112 continue;
5113 }
5114 else /* all others */
5115 {
5116 if (!number_is_in_list (args, b->number))
5117 continue;
5118 }
5119 }
5120 /* We only print out user settable breakpoints unless the
5121 allflag is set. */
5122 if (allflag || user_breakpoint_p (b))
12c5a436 5123 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5124 }
5125
3b31d625 5126 do_cleanups (bkpttbl_chain);
698384cd 5127
7f3b0473 5128 if (nr_printable_breakpoints == 0)
c906108c 5129 {
4a64f543
MS
5130 /* If there's a filter, let the caller decide how to report
5131 empty list. */
d77f58be
SS
5132 if (!filter)
5133 {
e5a67952 5134 if (args == NULL || *args == '\0')
d77f58be
SS
5135 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5136 else
4a64f543 5137 ui_out_message (uiout, 0,
e5a67952
MS
5138 "No breakpoint or watchpoint matching '%s'.\n",
5139 args);
d77f58be 5140 }
c906108c
SS
5141 }
5142 else
c4093a6a 5143 {
a6d9a66e
UW
5144 if (last_loc && !server_command)
5145 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5146 }
c906108c 5147
4a64f543 5148 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5149 there have been breakpoints? */
c906108c 5150 annotate_breakpoints_table_end ();
d77f58be
SS
5151
5152 return nr_printable_breakpoints;
c906108c
SS
5153}
5154
ad443146
SS
5155/* Display the value of default-collect in a way that is generally
5156 compatible with the breakpoint list. */
5157
5158static void
5159default_collect_info (void)
5160{
5161 /* If it has no value (which is frequently the case), say nothing; a
5162 message like "No default-collect." gets in user's face when it's
5163 not wanted. */
5164 if (!*default_collect)
5165 return;
5166
5167 /* The following phrase lines up nicely with per-tracepoint collect
5168 actions. */
5169 ui_out_text (uiout, "default collect ");
5170 ui_out_field_string (uiout, "default-collect", default_collect);
5171 ui_out_text (uiout, " \n");
5172}
5173
c906108c 5174static void
e5a67952 5175breakpoints_info (char *args, int from_tty)
c906108c 5176{
e5a67952 5177 breakpoint_1 (args, 0, NULL);
ad443146
SS
5178
5179 default_collect_info ();
d77f58be
SS
5180}
5181
5182static void
e5a67952 5183watchpoints_info (char *args, int from_tty)
d77f58be 5184{
e5a67952 5185 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
d77f58be
SS
5186
5187 if (num_printed == 0)
5188 {
e5a67952 5189 if (args == NULL || *args == '\0')
d77f58be
SS
5190 ui_out_message (uiout, 0, "No watchpoints.\n");
5191 else
e5a67952 5192 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5193 }
c906108c
SS
5194}
5195
7a292a7a 5196static void
e5a67952 5197maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5198{
e5a67952 5199 breakpoint_1 (args, 1, NULL);
ad443146
SS
5200
5201 default_collect_info ();
c906108c
SS
5202}
5203
0d381245 5204static int
714835d5 5205breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5206 struct program_space *pspace,
714835d5 5207 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5208{
5209 struct bp_location *bl = b->loc;
cc59ec59 5210
0d381245
VP
5211 for (; bl; bl = bl->next)
5212 {
6c95b8df
PA
5213 if (bl->pspace == pspace
5214 && bl->address == pc
0d381245
VP
5215 && (!overlay_debugging || bl->section == section))
5216 return 1;
5217 }
5218 return 0;
5219}
5220
6c95b8df
PA
5221/* Print a message describing any breakpoints set at PC. This
5222 concerns with logical breakpoints, so we match program spaces, not
5223 address spaces. */
c906108c
SS
5224
5225static void
6c95b8df
PA
5226describe_other_breakpoints (struct gdbarch *gdbarch,
5227 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5228 struct obj_section *section, int thread)
c906108c 5229{
52f0bd74
AC
5230 int others = 0;
5231 struct breakpoint *b;
c906108c
SS
5232
5233 ALL_BREAKPOINTS (b)
6c95b8df 5234 others += breakpoint_has_pc (b, pspace, pc, section);
c906108c
SS
5235 if (others > 0)
5236 {
a3f17187
AC
5237 if (others == 1)
5238 printf_filtered (_("Note: breakpoint "));
5239 else /* if (others == ???) */
5240 printf_filtered (_("Note: breakpoints "));
c906108c 5241 ALL_BREAKPOINTS (b)
6c95b8df 5242 if (breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5243 {
5244 others--;
5245 printf_filtered ("%d", b->number);
5246 if (b->thread == -1 && thread != -1)
5247 printf_filtered (" (all threads)");
5248 else if (b->thread != -1)
5249 printf_filtered (" (thread %d)", b->thread);
5250 printf_filtered ("%s%s ",
059fb39f 5251 ((b->enable_state == bp_disabled
8bea4e01
UW
5252 || b->enable_state == bp_call_disabled
5253 || b->enable_state == bp_startup_disabled)
0d381245
VP
5254 ? " (disabled)"
5255 : b->enable_state == bp_permanent
5256 ? " (permanent)"
5257 : ""),
5258 (others > 1) ? ","
5259 : ((others == 1) ? " and" : ""));
5260 }
a3f17187 5261 printf_filtered (_("also set at pc "));
5af949e3 5262 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5263 printf_filtered (".\n");
5264 }
5265}
5266\f
5267/* Set the default place to put a breakpoint
5268 for the `break' command with no arguments. */
5269
5270void
6c95b8df
PA
5271set_default_breakpoint (int valid, struct program_space *pspace,
5272 CORE_ADDR addr, struct symtab *symtab,
fba45db2 5273 int line)
c906108c
SS
5274{
5275 default_breakpoint_valid = valid;
6c95b8df 5276 default_breakpoint_pspace = pspace;
c906108c
SS
5277 default_breakpoint_address = addr;
5278 default_breakpoint_symtab = symtab;
5279 default_breakpoint_line = line;
5280}
5281
e4f237da
KB
5282/* Return true iff it is meaningful to use the address member of
5283 BPT. For some breakpoint types, the address member is irrelevant
5284 and it makes no sense to attempt to compare it to other addresses
5285 (or use it for any other purpose either).
5286
4a64f543
MS
5287 More specifically, each of the following breakpoint types will
5288 always have a zero valued address and we don't want to mark
5289 breakpoints of any of these types to be a duplicate of an actual
5290 breakpoint at address zero:
e4f237da
KB
5291
5292 bp_watchpoint
2d134ed3
PA
5293 bp_catchpoint
5294
5295*/
e4f237da
KB
5296
5297static int
5298breakpoint_address_is_meaningful (struct breakpoint *bpt)
5299{
5300 enum bptype type = bpt->type;
5301
2d134ed3
PA
5302 return (type != bp_watchpoint && type != bp_catchpoint);
5303}
5304
5305/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5306 true if LOC1 and LOC2 represent the same watchpoint location. */
5307
5308static int
4a64f543
MS
5309watchpoint_locations_match (struct bp_location *loc1,
5310 struct bp_location *loc2)
2d134ed3 5311{
3a5c3e22
PA
5312 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
5313 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
5314
5315 /* Both of them must exist. */
5316 gdb_assert (w1 != NULL);
5317 gdb_assert (w2 != NULL);
2bdf28a0 5318
4a64f543
MS
5319 /* If the target can evaluate the condition expression in hardware,
5320 then we we need to insert both watchpoints even if they are at
5321 the same place. Otherwise the watchpoint will only trigger when
5322 the condition of whichever watchpoint was inserted evaluates to
5323 true, not giving a chance for GDB to check the condition of the
5324 other watchpoint. */
3a5c3e22 5325 if ((w1->cond_exp
4a64f543
MS
5326 && target_can_accel_watchpoint_condition (loc1->address,
5327 loc1->length,
0cf6dd15 5328 loc1->watchpoint_type,
3a5c3e22
PA
5329 w1->cond_exp))
5330 || (w2->cond_exp
4a64f543
MS
5331 && target_can_accel_watchpoint_condition (loc2->address,
5332 loc2->length,
0cf6dd15 5333 loc2->watchpoint_type,
3a5c3e22 5334 w2->cond_exp)))
0cf6dd15
TJB
5335 return 0;
5336
85d721b8
PA
5337 /* Note that this checks the owner's type, not the location's. In
5338 case the target does not support read watchpoints, but does
5339 support access watchpoints, we'll have bp_read_watchpoint
5340 watchpoints with hw_access locations. Those should be considered
5341 duplicates of hw_read locations. The hw_read locations will
5342 become hw_access locations later. */
2d134ed3
PA
5343 return (loc1->owner->type == loc2->owner->type
5344 && loc1->pspace->aspace == loc2->pspace->aspace
5345 && loc1->address == loc2->address
5346 && loc1->length == loc2->length);
e4f237da
KB
5347}
5348
6c95b8df
PA
5349/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5350 same breakpoint location. In most targets, this can only be true
5351 if ASPACE1 matches ASPACE2. On targets that have global
5352 breakpoints, the address space doesn't really matter. */
5353
5354static int
5355breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5356 struct address_space *aspace2, CORE_ADDR addr2)
5357{
5358 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5359 || aspace1 == aspace2)
5360 && addr1 == addr2);
5361}
5362
f1310107
TJB
5363/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5364 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5365 matches ASPACE2. On targets that have global breakpoints, the address
5366 space doesn't really matter. */
5367
5368static int
5369breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5370 int len1, struct address_space *aspace2,
5371 CORE_ADDR addr2)
5372{
5373 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5374 || aspace1 == aspace2)
5375 && addr2 >= addr1 && addr2 < addr1 + len1);
5376}
5377
5378/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5379 a ranged breakpoint. In most targets, a match happens only if ASPACE
5380 matches the breakpoint's address space. On targets that have global
5381 breakpoints, the address space doesn't really matter. */
5382
5383static int
5384breakpoint_location_address_match (struct bp_location *bl,
5385 struct address_space *aspace,
5386 CORE_ADDR addr)
5387{
5388 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5389 aspace, addr)
5390 || (bl->length
5391 && breakpoint_address_match_range (bl->pspace->aspace,
5392 bl->address, bl->length,
5393 aspace, addr)));
5394}
5395
2d134ed3
PA
5396/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5397 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5398 represent the same location. */
5399
5400static int
4a64f543
MS
5401breakpoint_locations_match (struct bp_location *loc1,
5402 struct bp_location *loc2)
2d134ed3 5403{
2bdf28a0
JK
5404 int hw_point1, hw_point2;
5405
5406 /* Both of them must not be in moribund_locations. */
5407 gdb_assert (loc1->owner != NULL);
5408 gdb_assert (loc2->owner != NULL);
5409
5410 hw_point1 = is_hardware_watchpoint (loc1->owner);
5411 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5412
5413 if (hw_point1 != hw_point2)
5414 return 0;
5415 else if (hw_point1)
5416 return watchpoint_locations_match (loc1, loc2);
5417 else
f1310107
TJB
5418 /* We compare bp_location.length in order to cover ranged breakpoints. */
5419 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5420 loc2->pspace->aspace, loc2->address)
5421 && loc1->length == loc2->length);
2d134ed3
PA
5422}
5423
76897487
KB
5424static void
5425breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5426 int bnum, int have_bnum)
5427{
f63fbe86
MS
5428 /* The longest string possibly returned by hex_string_custom
5429 is 50 chars. These must be at least that big for safety. */
5430 char astr1[64];
5431 char astr2[64];
76897487 5432
bb599908
PH
5433 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5434 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5435 if (have_bnum)
8a3fe4f8 5436 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5437 bnum, astr1, astr2);
5438 else
8a3fe4f8 5439 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5440}
5441
4a64f543
MS
5442/* Adjust a breakpoint's address to account for architectural
5443 constraints on breakpoint placement. Return the adjusted address.
5444 Note: Very few targets require this kind of adjustment. For most
5445 targets, this function is simply the identity function. */
76897487
KB
5446
5447static CORE_ADDR
a6d9a66e
UW
5448adjust_breakpoint_address (struct gdbarch *gdbarch,
5449 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5450{
a6d9a66e 5451 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5452 {
5453 /* Very few targets need any kind of breakpoint adjustment. */
5454 return bpaddr;
5455 }
88f7da05
KB
5456 else if (bptype == bp_watchpoint
5457 || bptype == bp_hardware_watchpoint
5458 || bptype == bp_read_watchpoint
5459 || bptype == bp_access_watchpoint
fe798b75 5460 || bptype == bp_catchpoint)
88f7da05
KB
5461 {
5462 /* Watchpoints and the various bp_catch_* eventpoints should not
5463 have their addresses modified. */
5464 return bpaddr;
5465 }
76897487
KB
5466 else
5467 {
5468 CORE_ADDR adjusted_bpaddr;
5469
5470 /* Some targets have architectural constraints on the placement
5471 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5472 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5473
5474 /* An adjusted breakpoint address can significantly alter
5475 a user's expectations. Print a warning if an adjustment
5476 is required. */
5477 if (adjusted_bpaddr != bpaddr)
5478 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5479
5480 return adjusted_bpaddr;
5481 }
5482}
5483
28010a5d
PA
5484void
5485init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5486 struct breakpoint *owner)
7cc221ef 5487{
7cc221ef
DJ
5488 memset (loc, 0, sizeof (*loc));
5489
348d480f
PA
5490 gdb_assert (ops != NULL);
5491
28010a5d
PA
5492 loc->ops = ops;
5493 loc->owner = owner;
511a6cd4 5494 loc->cond = NULL;
0d381245
VP
5495 loc->shlib_disabled = 0;
5496 loc->enabled = 1;
e049a4b5 5497
28010a5d 5498 switch (owner->type)
e049a4b5
DJ
5499 {
5500 case bp_breakpoint:
5501 case bp_until:
5502 case bp_finish:
5503 case bp_longjmp:
5504 case bp_longjmp_resume:
186c406b
TT
5505 case bp_exception:
5506 case bp_exception_resume:
e049a4b5 5507 case bp_step_resume:
2c03e5be 5508 case bp_hp_step_resume:
e049a4b5
DJ
5509 case bp_watchpoint_scope:
5510 case bp_call_dummy:
aa7d318d 5511 case bp_std_terminate:
e049a4b5
DJ
5512 case bp_shlib_event:
5513 case bp_thread_event:
5514 case bp_overlay_event:
4efc6507 5515 case bp_jit_event:
0fd8e87f 5516 case bp_longjmp_master:
aa7d318d 5517 case bp_std_terminate_master:
186c406b 5518 case bp_exception_master:
0e30163f
JK
5519 case bp_gnu_ifunc_resolver:
5520 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5521 loc->loc_type = bp_loc_software_breakpoint;
5522 break;
5523 case bp_hardware_breakpoint:
5524 loc->loc_type = bp_loc_hardware_breakpoint;
5525 break;
5526 case bp_hardware_watchpoint:
5527 case bp_read_watchpoint:
5528 case bp_access_watchpoint:
5529 loc->loc_type = bp_loc_hardware_watchpoint;
5530 break;
5531 case bp_watchpoint:
ce78b96d 5532 case bp_catchpoint:
15c3d785
PA
5533 case bp_tracepoint:
5534 case bp_fast_tracepoint:
0fb4aa4b 5535 case bp_static_tracepoint:
e049a4b5
DJ
5536 loc->loc_type = bp_loc_other;
5537 break;
5538 default:
e2e0b3e5 5539 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5540 }
5541
f431efe5 5542 loc->refc = 1;
28010a5d
PA
5543}
5544
5545/* Allocate a struct bp_location. */
5546
5547static struct bp_location *
5548allocate_bp_location (struct breakpoint *bpt)
5549{
348d480f
PA
5550 return bpt->ops->allocate_location (bpt);
5551}
7cc221ef 5552
f431efe5
PA
5553static void
5554free_bp_location (struct bp_location *loc)
fe3f5fa8 5555{
348d480f 5556 loc->ops->dtor (loc);
fe3f5fa8
VP
5557 xfree (loc);
5558}
5559
f431efe5
PA
5560/* Increment reference count. */
5561
5562static void
5563incref_bp_location (struct bp_location *bl)
5564{
5565 ++bl->refc;
5566}
5567
5568/* Decrement reference count. If the reference count reaches 0,
5569 destroy the bp_location. Sets *BLP to NULL. */
5570
5571static void
5572decref_bp_location (struct bp_location **blp)
5573{
0807b50c
PA
5574 gdb_assert ((*blp)->refc > 0);
5575
f431efe5
PA
5576 if (--(*blp)->refc == 0)
5577 free_bp_location (*blp);
5578 *blp = NULL;
5579}
5580
346774a9 5581/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 5582
346774a9
PA
5583static void
5584add_to_breakpoint_chain (struct breakpoint *b)
c906108c 5585{
346774a9 5586 struct breakpoint *b1;
c906108c 5587
346774a9
PA
5588 /* Add this breakpoint to the end of the chain so that a list of
5589 breakpoints will come out in order of increasing numbers. */
5590
5591 b1 = breakpoint_chain;
5592 if (b1 == 0)
5593 breakpoint_chain = b;
5594 else
5595 {
5596 while (b1->next)
5597 b1 = b1->next;
5598 b1->next = b;
5599 }
5600}
5601
5602/* Initializes breakpoint B with type BPTYPE and no locations yet. */
5603
5604static void
5605init_raw_breakpoint_without_location (struct breakpoint *b,
5606 struct gdbarch *gdbarch,
28010a5d
PA
5607 enum bptype bptype,
5608 struct breakpoint_ops *ops)
346774a9 5609{
c906108c 5610 memset (b, 0, sizeof (*b));
2219d63c 5611
348d480f
PA
5612 gdb_assert (ops != NULL);
5613
28010a5d 5614 b->ops = ops;
4d28f7a8 5615 b->type = bptype;
a6d9a66e 5616 b->gdbarch = gdbarch;
c906108c
SS
5617 b->language = current_language->la_language;
5618 b->input_radix = input_radix;
5619 b->thread = -1;
b5de0fa7 5620 b->enable_state = bp_enabled;
c906108c
SS
5621 b->next = 0;
5622 b->silent = 0;
5623 b->ignore_count = 0;
5624 b->commands = NULL;
818dd999 5625 b->frame_id = null_frame_id;
0d381245 5626 b->condition_not_parsed = 0;
84f4c1fe 5627 b->py_bp_object = NULL;
d0fb5eae 5628 b->related_breakpoint = b;
346774a9
PA
5629}
5630
5631/* Helper to set_raw_breakpoint below. Creates a breakpoint
5632 that has type BPTYPE and has no locations as yet. */
346774a9
PA
5633
5634static struct breakpoint *
5635set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f
PA
5636 enum bptype bptype,
5637 struct breakpoint_ops *ops)
346774a9
PA
5638{
5639 struct breakpoint *b = XNEW (struct breakpoint);
5640
348d480f 5641 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 5642 add_to_breakpoint_chain (b);
0d381245
VP
5643 return b;
5644}
5645
0e30163f
JK
5646/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5647 resolutions should be made as the user specified the location explicitly
5648 enough. */
5649
0d381245 5650static void
0e30163f 5651set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5652{
2bdf28a0
JK
5653 gdb_assert (loc->owner != NULL);
5654
0d381245 5655 if (loc->owner->type == bp_breakpoint
1042e4c0 5656 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5657 || is_tracepoint (loc->owner))
0d381245 5658 {
0e30163f
JK
5659 int is_gnu_ifunc;
5660
5661 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5662 NULL, NULL, &is_gnu_ifunc);
5663
5664 if (is_gnu_ifunc && !explicit_loc)
5665 {
5666 struct breakpoint *b = loc->owner;
5667
5668 gdb_assert (loc->pspace == current_program_space);
5669 if (gnu_ifunc_resolve_name (loc->function_name,
5670 &loc->requested_address))
5671 {
5672 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5673 loc->address = adjust_breakpoint_address (loc->gdbarch,
5674 loc->requested_address,
5675 b->type);
5676 }
5677 else if (b->type == bp_breakpoint && b->loc == loc
5678 && loc->next == NULL && b->related_breakpoint == b)
5679 {
5680 /* Create only the whole new breakpoint of this type but do not
5681 mess more complicated breakpoints with multiple locations. */
5682 b->type = bp_gnu_ifunc_resolver;
5683 }
5684 }
5685
0d381245
VP
5686 if (loc->function_name)
5687 loc->function_name = xstrdup (loc->function_name);
5688 }
5689}
5690
a6d9a66e
UW
5691/* Attempt to determine architecture of location identified by SAL. */
5692static struct gdbarch *
5693get_sal_arch (struct symtab_and_line sal)
5694{
5695 if (sal.section)
5696 return get_objfile_arch (sal.section->objfile);
5697 if (sal.symtab)
5698 return get_objfile_arch (sal.symtab->objfile);
5699
5700 return NULL;
5701}
5702
346774a9
PA
5703/* Low level routine for partially initializing a breakpoint of type
5704 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 5705 file name, and line number are provided by SAL.
0d381245
VP
5706
5707 It is expected that the caller will complete the initialization of
5708 the newly created breakpoint struct as well as output any status
c56053d2 5709 information regarding the creation of a new breakpoint. */
0d381245 5710
346774a9
PA
5711static void
5712init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d
PA
5713 struct symtab_and_line sal, enum bptype bptype,
5714 struct breakpoint_ops *ops)
0d381245 5715{
0d381245 5716 CORE_ADDR adjusted_address;
a6d9a66e
UW
5717 struct gdbarch *loc_gdbarch;
5718
28010a5d 5719 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 5720
a6d9a66e
UW
5721 loc_gdbarch = get_sal_arch (sal);
5722 if (!loc_gdbarch)
5723 loc_gdbarch = b->gdbarch;
0d381245 5724
6c95b8df
PA
5725 if (bptype != bp_catchpoint)
5726 gdb_assert (sal.pspace != NULL);
5727
0d381245
VP
5728 /* Adjust the breakpoint's address prior to allocating a location.
5729 Once we call allocate_bp_location(), that mostly uninitialized
5730 location will be placed on the location chain. Adjustment of the
8defab1a 5731 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
5732 not want its scan of the location chain to find a breakpoint and
5733 location that's only been partially initialized. */
4a64f543
MS
5734 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5735 sal.pc, b->type);
0d381245 5736
39d61571 5737 b->loc = allocate_bp_location (b);
a6d9a66e 5738 b->loc->gdbarch = loc_gdbarch;
0d381245
VP
5739 b->loc->requested_address = sal.pc;
5740 b->loc->address = adjusted_address;
6c95b8df
PA
5741 b->loc->pspace = sal.pspace;
5742
5743 /* Store the program space that was used to set the breakpoint, for
5744 breakpoint resetting. */
5745 b->pspace = sal.pspace;
0d381245
VP
5746
5747 if (sal.symtab == NULL)
5748 b->source_file = NULL;
5749 else
1b36a34b 5750 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5751 b->loc->section = sal.section;
5752 b->line_number = sal.line;
5753
0e30163f
JK
5754 set_breakpoint_location_function (b->loc,
5755 sal.explicit_pc || sal.explicit_line);
c906108c 5756
c906108c 5757 breakpoints_changed ();
346774a9 5758}
c906108c 5759
346774a9
PA
5760/* set_raw_breakpoint is a low level routine for allocating and
5761 partially initializing a breakpoint of type BPTYPE. The newly
5762 created breakpoint's address, section, source file name, and line
5763 number are provided by SAL. The newly created and partially
5764 initialized breakpoint is added to the breakpoint chain and
5765 is also returned as the value of this function.
5766
5767 It is expected that the caller will complete the initialization of
5768 the newly created breakpoint struct as well as output any status
5769 information regarding the creation of a new breakpoint. In
5770 particular, set_raw_breakpoint does NOT set the breakpoint
5771 number! Care should be taken to not allow an error to occur
5772 prior to completing the initialization of the breakpoint. If this
5773 should happen, a bogus breakpoint will be left on the chain. */
5774
5775struct breakpoint *
5776set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f
PA
5777 struct symtab_and_line sal, enum bptype bptype,
5778 struct breakpoint_ops *ops)
346774a9
PA
5779{
5780 struct breakpoint *b = XNEW (struct breakpoint);
5781
348d480f 5782 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 5783 add_to_breakpoint_chain (b);
c906108c
SS
5784 return b;
5785}
5786
c2c6d25f
JM
5787
5788/* Note that the breakpoint object B describes a permanent breakpoint
5789 instruction, hard-wired into the inferior's code. */
5790void
5791make_breakpoint_permanent (struct breakpoint *b)
5792{
0d381245 5793 struct bp_location *bl;
cc59ec59 5794
b5de0fa7 5795 b->enable_state = bp_permanent;
c2c6d25f 5796
4a64f543
MS
5797 /* By definition, permanent breakpoints are already present in the
5798 code. Mark all locations as inserted. For now,
5799 make_breakpoint_permanent is called in just one place, so it's
5800 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 5801 multiple locations or not, but it's easy to implement. */
0d381245
VP
5802 for (bl = b->loc; bl; bl = bl->next)
5803 bl->inserted = 1;
c2c6d25f
JM
5804}
5805
53a5351d 5806/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5807 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5808 initiated the operation. */
c906108c
SS
5809
5810void
186c406b 5811set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5812{
35df4500 5813 struct breakpoint *b, *b_tmp;
186c406b 5814 int thread = tp->num;
0fd8e87f
UW
5815
5816 /* To avoid having to rescan all objfile symbols at every step,
5817 we maintain a list of continually-inserted but always disabled
5818 longjmp "master" breakpoints. Here, we simply create momentary
5819 clones of those and enable them for the requested thread. */
35df4500 5820 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5821 if (b->pspace == current_program_space
186c406b
TT
5822 && (b->type == bp_longjmp_master
5823 || b->type == bp_exception_master))
0fd8e87f 5824 {
06edf0c0
PA
5825 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5826 struct breakpoint *clone;
cc59ec59 5827
06edf0c0
PA
5828 clone = momentary_breakpoint_from_master (b, type,
5829 &momentary_breakpoint_ops);
0fd8e87f
UW
5830 clone->thread = thread;
5831 }
186c406b
TT
5832
5833 tp->initiating_frame = frame;
c906108c
SS
5834}
5835
611c83ae 5836/* Delete all longjmp breakpoints from THREAD. */
c906108c 5837void
611c83ae 5838delete_longjmp_breakpoint (int thread)
c906108c 5839{
35df4500 5840 struct breakpoint *b, *b_tmp;
c906108c 5841
35df4500 5842 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5843 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5844 {
5845 if (b->thread == thread)
5846 delete_breakpoint (b);
5847 }
c906108c
SS
5848}
5849
1900040c
MS
5850void
5851enable_overlay_breakpoints (void)
5852{
52f0bd74 5853 struct breakpoint *b;
1900040c
MS
5854
5855 ALL_BREAKPOINTS (b)
5856 if (b->type == bp_overlay_event)
5857 {
5858 b->enable_state = bp_enabled;
b60e7edf 5859 update_global_location_list (1);
c02f5703 5860 overlay_events_enabled = 1;
1900040c
MS
5861 }
5862}
5863
5864void
5865disable_overlay_breakpoints (void)
5866{
52f0bd74 5867 struct breakpoint *b;
1900040c
MS
5868
5869 ALL_BREAKPOINTS (b)
5870 if (b->type == bp_overlay_event)
5871 {
5872 b->enable_state = bp_disabled;
b60e7edf 5873 update_global_location_list (0);
c02f5703 5874 overlay_events_enabled = 0;
1900040c
MS
5875 }
5876}
5877
aa7d318d
TT
5878/* Set an active std::terminate breakpoint for each std::terminate
5879 master breakpoint. */
5880void
5881set_std_terminate_breakpoint (void)
5882{
35df4500 5883 struct breakpoint *b, *b_tmp;
aa7d318d 5884
35df4500 5885 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5886 if (b->pspace == current_program_space
5887 && b->type == bp_std_terminate_master)
5888 {
06edf0c0
PA
5889 momentary_breakpoint_from_master (b, bp_std_terminate,
5890 &momentary_breakpoint_ops);
aa7d318d
TT
5891 }
5892}
5893
5894/* Delete all the std::terminate breakpoints. */
5895void
5896delete_std_terminate_breakpoint (void)
5897{
35df4500 5898 struct breakpoint *b, *b_tmp;
aa7d318d 5899
35df4500 5900 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5901 if (b->type == bp_std_terminate)
5902 delete_breakpoint (b);
5903}
5904
c4093a6a 5905struct breakpoint *
a6d9a66e 5906create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
5907{
5908 struct breakpoint *b;
c4093a6a 5909
06edf0c0
PA
5910 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
5911 &internal_breakpoint_ops);
5912
b5de0fa7 5913 b->enable_state = bp_enabled;
c4093a6a 5914 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
5915 b->addr_string
5916 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 5917
b60e7edf 5918 update_global_location_list_nothrow (1);
74960c60 5919
c4093a6a
JM
5920 return b;
5921}
5922
5923void
5924remove_thread_event_breakpoints (void)
5925{
35df4500 5926 struct breakpoint *b, *b_tmp;
c4093a6a 5927
35df4500 5928 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
5929 if (b->type == bp_thread_event
5930 && b->loc->pspace == current_program_space)
c4093a6a
JM
5931 delete_breakpoint (b);
5932}
5933
0101ce28
JJ
5934struct lang_and_radix
5935 {
5936 enum language lang;
5937 int radix;
5938 };
5939
4efc6507
DE
5940/* Create a breakpoint for JIT code registration and unregistration. */
5941
5942struct breakpoint *
5943create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5944{
5945 struct breakpoint *b;
5946
06edf0c0
PA
5947 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
5948 &internal_breakpoint_ops);
4efc6507
DE
5949 update_global_location_list_nothrow (1);
5950 return b;
5951}
0101ce28 5952
03673fc7
PP
5953/* Remove JIT code registration and unregistration breakpoint(s). */
5954
5955void
5956remove_jit_event_breakpoints (void)
5957{
5958 struct breakpoint *b, *b_tmp;
5959
5960 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5961 if (b->type == bp_jit_event
5962 && b->loc->pspace == current_program_space)
5963 delete_breakpoint (b);
5964}
5965
cae688ec
JJ
5966void
5967remove_solib_event_breakpoints (void)
5968{
35df4500 5969 struct breakpoint *b, *b_tmp;
cae688ec 5970
35df4500 5971 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
5972 if (b->type == bp_shlib_event
5973 && b->loc->pspace == current_program_space)
cae688ec
JJ
5974 delete_breakpoint (b);
5975}
5976
5977struct breakpoint *
a6d9a66e 5978create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
5979{
5980 struct breakpoint *b;
5981
06edf0c0
PA
5982 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
5983 &internal_breakpoint_ops);
b60e7edf 5984 update_global_location_list_nothrow (1);
cae688ec
JJ
5985 return b;
5986}
5987
5988/* Disable any breakpoints that are on code in shared libraries. Only
5989 apply to enabled breakpoints, disabled ones can just stay disabled. */
5990
5991void
cb851954 5992disable_breakpoints_in_shlibs (void)
cae688ec 5993{
876fa593 5994 struct bp_location *loc, **locp_tmp;
cae688ec 5995
876fa593 5996 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 5997 {
2bdf28a0 5998 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 5999 struct breakpoint *b = loc->owner;
2bdf28a0 6000
4a64f543
MS
6001 /* We apply the check to all breakpoints, including disabled for
6002 those with loc->duplicate set. This is so that when breakpoint
6003 becomes enabled, or the duplicate is removed, gdb will try to
6004 insert all breakpoints. If we don't set shlib_disabled here,
6005 we'll try to insert those breakpoints and fail. */
1042e4c0 6006 if (((b->type == bp_breakpoint)
508ccb1f 6007 || (b->type == bp_jit_event)
1042e4c0 6008 || (b->type == bp_hardware_breakpoint)
d77f58be 6009 || (is_tracepoint (b)))
6c95b8df 6010 && loc->pspace == current_program_space
0d381245 6011 && !loc->shlib_disabled
a77053c2 6012#ifdef PC_SOLIB
0d381245 6013 && PC_SOLIB (loc->address)
a77053c2 6014#else
6c95b8df 6015 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6016#endif
6017 )
0d381245
VP
6018 {
6019 loc->shlib_disabled = 1;
6020 }
cae688ec
JJ
6021 }
6022}
6023
7a9dd1b2 6024/* Disable any breakpoints that are in an unloaded shared library.
4a64f543
MS
6025 Only apply to enabled breakpoints, disabled ones can just stay
6026 disabled. */
84acb35a 6027
75149521 6028static void
84acb35a
JJ
6029disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6030{
876fa593 6031 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6032 int disabled_shlib_breaks = 0;
6033
c86cf029
VP
6034 /* SunOS a.out shared libraries are always mapped, so do not
6035 disable breakpoints; they will only be reported as unloaded
6036 through clear_solib when GDB discards its shared library
6037 list. See clear_solib for more information. */
6038 if (exec_bfd != NULL
6039 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6040 return;
6041
876fa593 6042 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6043 {
2bdf28a0 6044 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6045 struct breakpoint *b = loc->owner;
cc59ec59 6046
0d381245
VP
6047 if ((loc->loc_type == bp_loc_hardware_breakpoint
6048 || loc->loc_type == bp_loc_software_breakpoint)
6c95b8df 6049 && solib->pspace == loc->pspace
e2dd7057 6050 && !loc->shlib_disabled
508ccb1f
TT
6051 && (b->type == bp_breakpoint
6052 || b->type == bp_jit_event
6053 || b->type == bp_hardware_breakpoint)
e2dd7057 6054 && solib_contains_address_p (solib, loc->address))
84acb35a 6055 {
e2dd7057
PP
6056 loc->shlib_disabled = 1;
6057 /* At this point, we cannot rely on remove_breakpoint
6058 succeeding so we must mark the breakpoint as not inserted
6059 to prevent future errors occurring in remove_breakpoints. */
6060 loc->inserted = 0;
8d3788bd
VP
6061
6062 /* This may cause duplicate notifications for the same breakpoint. */
6063 observer_notify_breakpoint_modified (b);
6064
e2dd7057
PP
6065 if (!disabled_shlib_breaks)
6066 {
6067 target_terminal_ours_for_output ();
3e43a32a
MS
6068 warning (_("Temporarily disabling breakpoints "
6069 "for unloaded shared library \"%s\""),
e2dd7057 6070 solib->so_name);
84acb35a 6071 }
e2dd7057 6072 disabled_shlib_breaks = 1;
84acb35a
JJ
6073 }
6074 }
84acb35a
JJ
6075}
6076
ce78b96d
JB
6077/* FORK & VFORK catchpoints. */
6078
e29a4733
PA
6079/* An instance of this type is used to represent a fork or vfork
6080 catchpoint. It includes a "struct breakpoint" as a kind of base
6081 class; users downcast to "struct breakpoint *" when needed. A
6082 breakpoint is really of this type iff its ops pointer points to
6083 CATCH_FORK_BREAKPOINT_OPS. */
6084
6085struct fork_catchpoint
6086{
6087 /* The base class. */
6088 struct breakpoint base;
6089
6090 /* Process id of a child process whose forking triggered this
6091 catchpoint. This field is only valid immediately after this
6092 catchpoint has triggered. */
6093 ptid_t forked_inferior_pid;
6094};
6095
4a64f543
MS
6096/* Implement the "insert" breakpoint_ops method for fork
6097 catchpoints. */
ce78b96d 6098
77b06cd7
TJB
6099static int
6100insert_catch_fork (struct bp_location *bl)
ce78b96d 6101{
77b06cd7 6102 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6103}
6104
4a64f543
MS
6105/* Implement the "remove" breakpoint_ops method for fork
6106 catchpoints. */
ce78b96d
JB
6107
6108static int
77b06cd7 6109remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6110{
6111 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6112}
6113
6114/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6115 catchpoints. */
6116
6117static int
f1310107
TJB
6118breakpoint_hit_catch_fork (const struct bp_location *bl,
6119 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6120{
e29a4733
PA
6121 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6122
6123 return inferior_has_forked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6124}
6125
4a64f543
MS
6126/* Implement the "print_it" breakpoint_ops method for fork
6127 catchpoints. */
ce78b96d
JB
6128
6129static enum print_stop_action
348d480f 6130print_it_catch_fork (bpstat bs)
ce78b96d 6131{
348d480f
PA
6132 struct breakpoint *b = bs->breakpoint_at;
6133 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 6134
ce78b96d
JB
6135 annotate_catchpoint (b->number);
6136 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
e29a4733 6137 b->number, ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6138 return PRINT_SRC_AND_LOC;
6139}
6140
4a64f543
MS
6141/* Implement the "print_one" breakpoint_ops method for fork
6142 catchpoints. */
ce78b96d
JB
6143
6144static void
a6d9a66e 6145print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6146{
e29a4733 6147 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d
TT
6148 struct value_print_options opts;
6149
6150 get_user_print_options (&opts);
6151
4a64f543
MS
6152 /* Field 4, the address, is omitted (which makes the columns not
6153 line up too nicely with the headers, but the effect is relatively
6154 readable). */
79a45b7d 6155 if (opts.addressprint)
ce78b96d
JB
6156 ui_out_field_skip (uiout, "addr");
6157 annotate_field (5);
6158 ui_out_text (uiout, "fork");
e29a4733 6159 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6160 {
6161 ui_out_text (uiout, ", process ");
6162 ui_out_field_int (uiout, "what",
e29a4733 6163 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6164 ui_out_spaces (uiout, 1);
6165 }
6166}
6167
6168/* Implement the "print_mention" breakpoint_ops method for fork
6169 catchpoints. */
6170
6171static void
6172print_mention_catch_fork (struct breakpoint *b)
6173{
6174 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6175}
6176
6149aea9
PA
6177/* Implement the "print_recreate" breakpoint_ops method for fork
6178 catchpoints. */
6179
6180static void
6181print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6182{
6183 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 6184 print_recreate_thread (b, fp);
6149aea9
PA
6185}
6186
ce78b96d
JB
6187/* The breakpoint_ops structure to be used in fork catchpoints. */
6188
2060206e 6189static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 6190
4a64f543
MS
6191/* Implement the "insert" breakpoint_ops method for vfork
6192 catchpoints. */
ce78b96d 6193
77b06cd7
TJB
6194static int
6195insert_catch_vfork (struct bp_location *bl)
ce78b96d 6196{
77b06cd7 6197 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6198}
6199
4a64f543
MS
6200/* Implement the "remove" breakpoint_ops method for vfork
6201 catchpoints. */
ce78b96d
JB
6202
6203static int
77b06cd7 6204remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6205{
6206 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6207}
6208
6209/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6210 catchpoints. */
6211
6212static int
f1310107
TJB
6213breakpoint_hit_catch_vfork (const struct bp_location *bl,
6214 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6215{
e29a4733
PA
6216 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6217
6218 return inferior_has_vforked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6219}
6220
4a64f543
MS
6221/* Implement the "print_it" breakpoint_ops method for vfork
6222 catchpoints. */
ce78b96d
JB
6223
6224static enum print_stop_action
348d480f 6225print_it_catch_vfork (bpstat bs)
ce78b96d 6226{
348d480f 6227 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
6228 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6229
ce78b96d
JB
6230 annotate_catchpoint (b->number);
6231 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
e29a4733 6232 b->number, ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6233 return PRINT_SRC_AND_LOC;
6234}
6235
4a64f543
MS
6236/* Implement the "print_one" breakpoint_ops method for vfork
6237 catchpoints. */
ce78b96d
JB
6238
6239static void
a6d9a66e 6240print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6241{
e29a4733 6242 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d
TT
6243 struct value_print_options opts;
6244
6245 get_user_print_options (&opts);
4a64f543
MS
6246 /* Field 4, the address, is omitted (which makes the columns not
6247 line up too nicely with the headers, but the effect is relatively
6248 readable). */
79a45b7d 6249 if (opts.addressprint)
ce78b96d
JB
6250 ui_out_field_skip (uiout, "addr");
6251 annotate_field (5);
6252 ui_out_text (uiout, "vfork");
e29a4733 6253 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6254 {
6255 ui_out_text (uiout, ", process ");
6256 ui_out_field_int (uiout, "what",
e29a4733 6257 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6258 ui_out_spaces (uiout, 1);
6259 }
6260}
6261
6262/* Implement the "print_mention" breakpoint_ops method for vfork
6263 catchpoints. */
6264
6265static void
6266print_mention_catch_vfork (struct breakpoint *b)
6267{
6268 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6269}
6270
6149aea9
PA
6271/* Implement the "print_recreate" breakpoint_ops method for vfork
6272 catchpoints. */
6273
6274static void
6275print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6276{
6277 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 6278 print_recreate_thread (b, fp);
6149aea9
PA
6279}
6280
ce78b96d
JB
6281/* The breakpoint_ops structure to be used in vfork catchpoints. */
6282
2060206e 6283static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 6284
be5c67c1
PA
6285/* An instance of this type is used to represent a syscall catchpoint.
6286 It includes a "struct breakpoint" as a kind of base class; users
6287 downcast to "struct breakpoint *" when needed. A breakpoint is
6288 really of this type iff its ops pointer points to
6289 CATCH_SYSCALL_BREAKPOINT_OPS. */
6290
6291struct syscall_catchpoint
6292{
6293 /* The base class. */
6294 struct breakpoint base;
6295
6296 /* Syscall numbers used for the 'catch syscall' feature. If no
6297 syscall has been specified for filtering, its value is NULL.
6298 Otherwise, it holds a list of all syscalls to be caught. The
6299 list elements are allocated with xmalloc. */
6300 VEC(int) *syscalls_to_be_caught;
6301};
6302
6303/* Implement the "dtor" breakpoint_ops method for syscall
6304 catchpoints. */
6305
6306static void
6307dtor_catch_syscall (struct breakpoint *b)
6308{
6309 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6310
6311 VEC_free (int, c->syscalls_to_be_caught);
348d480f 6312
2060206e 6313 base_breakpoint_ops.dtor (b);
be5c67c1
PA
6314}
6315
a96d9b2e
SDJ
6316/* Implement the "insert" breakpoint_ops method for syscall
6317 catchpoints. */
6318
77b06cd7
TJB
6319static int
6320insert_catch_syscall (struct bp_location *bl)
a96d9b2e 6321{
be5c67c1 6322 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6323 struct inferior *inf = current_inferior ();
6324
6325 ++inf->total_syscalls_count;
be5c67c1 6326 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6327 ++inf->any_syscall_count;
6328 else
6329 {
6330 int i, iter;
cc59ec59 6331
a96d9b2e 6332 for (i = 0;
be5c67c1 6333 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6334 i++)
6335 {
6336 int elem;
cc59ec59 6337
a96d9b2e
SDJ
6338 if (iter >= VEC_length (int, inf->syscalls_counts))
6339 {
6340 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6341 uintptr_t vec_addr_offset
6342 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6343 uintptr_t vec_addr;
6344 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6345 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6346 vec_addr_offset;
6347 memset ((void *) vec_addr, 0,
6348 (iter + 1 - old_size) * sizeof (int));
6349 }
6350 elem = VEC_index (int, inf->syscalls_counts, iter);
6351 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6352 }
6353 }
6354
77b06cd7
TJB
6355 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6356 inf->total_syscalls_count != 0,
6357 inf->any_syscall_count,
6358 VEC_length (int, inf->syscalls_counts),
6359 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6360}
6361
6362/* Implement the "remove" breakpoint_ops method for syscall
6363 catchpoints. */
6364
6365static int
77b06cd7 6366remove_catch_syscall (struct bp_location *bl)
a96d9b2e 6367{
be5c67c1 6368 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6369 struct inferior *inf = current_inferior ();
6370
6371 --inf->total_syscalls_count;
be5c67c1 6372 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6373 --inf->any_syscall_count;
6374 else
6375 {
6376 int i, iter;
cc59ec59 6377
a96d9b2e 6378 for (i = 0;
be5c67c1 6379 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6380 i++)
6381 {
6382 int elem;
6383 if (iter >= VEC_length (int, inf->syscalls_counts))
6384 /* Shouldn't happen. */
6385 continue;
6386 elem = VEC_index (int, inf->syscalls_counts, iter);
6387 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6388 }
6389 }
6390
6391 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6392 inf->total_syscalls_count != 0,
6393 inf->any_syscall_count,
6394 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6395 VEC_address (int,
6396 inf->syscalls_counts));
a96d9b2e
SDJ
6397}
6398
6399/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6400 catchpoints. */
6401
6402static int
f1310107
TJB
6403breakpoint_hit_catch_syscall (const struct bp_location *bl,
6404 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6405{
4a64f543
MS
6406 /* We must check if we are catching specific syscalls in this
6407 breakpoint. If we are, then we must guarantee that the called
6408 syscall is the same syscall we are catching. */
a96d9b2e 6409 int syscall_number = 0;
be5c67c1
PA
6410 const struct syscall_catchpoint *c
6411 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6412
6413 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6414 return 0;
6415
6416 /* Now, checking if the syscall is the same. */
be5c67c1 6417 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6418 {
6419 int i, iter;
cc59ec59 6420
a96d9b2e 6421 for (i = 0;
be5c67c1 6422 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6423 i++)
6424 if (syscall_number == iter)
6425 break;
6426 /* Not the same. */
6427 if (!iter)
6428 return 0;
6429 }
6430
6431 return 1;
6432}
6433
6434/* Implement the "print_it" breakpoint_ops method for syscall
6435 catchpoints. */
6436
6437static enum print_stop_action
348d480f 6438print_it_catch_syscall (bpstat bs)
a96d9b2e 6439{
348d480f 6440 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
6441 /* These are needed because we want to know in which state a
6442 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6443 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6444 must print "called syscall" or "returned from syscall". */
6445 ptid_t ptid;
6446 struct target_waitstatus last;
6447 struct syscall s;
6448 struct cleanup *old_chain;
6449 char *syscall_id;
6450
6451 get_last_target_status (&ptid, &last);
6452
6453 get_syscall_by_number (last.value.syscall_number, &s);
6454
6455 annotate_catchpoint (b->number);
6456
6457 if (s.name == NULL)
6458 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6459 else
6460 syscall_id = xstrprintf ("'%s'", s.name);
6461
6462 old_chain = make_cleanup (xfree, syscall_id);
6463
6464 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6465 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6466 b->number, syscall_id);
6467 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6468 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6469 b->number, syscall_id);
6470
6471 do_cleanups (old_chain);
6472
6473 return PRINT_SRC_AND_LOC;
6474}
6475
6476/* Implement the "print_one" breakpoint_ops method for syscall
6477 catchpoints. */
6478
6479static void
6480print_one_catch_syscall (struct breakpoint *b,
f1310107 6481 struct bp_location **last_loc)
a96d9b2e 6482{
be5c67c1 6483 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e
SDJ
6484 struct value_print_options opts;
6485
6486 get_user_print_options (&opts);
4a64f543
MS
6487 /* Field 4, the address, is omitted (which makes the columns not
6488 line up too nicely with the headers, but the effect is relatively
6489 readable). */
a96d9b2e
SDJ
6490 if (opts.addressprint)
6491 ui_out_field_skip (uiout, "addr");
6492 annotate_field (5);
6493
be5c67c1
PA
6494 if (c->syscalls_to_be_caught
6495 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6496 ui_out_text (uiout, "syscalls \"");
6497 else
6498 ui_out_text (uiout, "syscall \"");
6499
be5c67c1 6500 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6501 {
6502 int i, iter;
6503 char *text = xstrprintf ("%s", "");
cc59ec59 6504
a96d9b2e 6505 for (i = 0;
be5c67c1 6506 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6507 i++)
6508 {
6509 char *x = text;
6510 struct syscall s;
6511 get_syscall_by_number (iter, &s);
6512
6513 if (s.name != NULL)
6514 text = xstrprintf ("%s%s, ", text, s.name);
6515 else
6516 text = xstrprintf ("%s%d, ", text, iter);
6517
6518 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 6519 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
6520 on every call. */
6521 xfree (x);
6522 }
6523 /* Remove the last comma. */
6524 text[strlen (text) - 2] = '\0';
6525 ui_out_field_string (uiout, "what", text);
6526 }
6527 else
6528 ui_out_field_string (uiout, "what", "<any syscall>");
6529 ui_out_text (uiout, "\" ");
6530}
6531
6532/* Implement the "print_mention" breakpoint_ops method for syscall
6533 catchpoints. */
6534
6535static void
6536print_mention_catch_syscall (struct breakpoint *b)
6537{
be5c67c1
PA
6538 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6539
6540 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6541 {
6542 int i, iter;
6543
be5c67c1 6544 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6545 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6546 else
6547 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6548
6549 for (i = 0;
be5c67c1 6550 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6551 i++)
6552 {
6553 struct syscall s;
6554 get_syscall_by_number (iter, &s);
6555
6556 if (s.name)
6557 printf_filtered (" '%s' [%d]", s.name, s.number);
6558 else
6559 printf_filtered (" %d", s.number);
6560 }
6561 printf_filtered (")");
6562 }
6563 else
6564 printf_filtered (_("Catchpoint %d (any syscall)"),
6565 b->number);
6566}
6567
6149aea9
PA
6568/* Implement the "print_recreate" breakpoint_ops method for syscall
6569 catchpoints. */
6570
6571static void
6572print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6573{
be5c67c1
PA
6574 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6575
6149aea9
PA
6576 fprintf_unfiltered (fp, "catch syscall");
6577
be5c67c1 6578 if (c->syscalls_to_be_caught)
6149aea9
PA
6579 {
6580 int i, iter;
6581
6582 for (i = 0;
be5c67c1 6583 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
6584 i++)
6585 {
6586 struct syscall s;
6587
6588 get_syscall_by_number (iter, &s);
6589 if (s.name)
6590 fprintf_unfiltered (fp, " %s", s.name);
6591 else
6592 fprintf_unfiltered (fp, " %d", s.number);
6593 }
6594 }
d9b3f62e 6595 print_recreate_thread (b, fp);
6149aea9
PA
6596}
6597
a96d9b2e
SDJ
6598/* The breakpoint_ops structure to be used in syscall catchpoints. */
6599
2060206e 6600static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
6601
6602/* Returns non-zero if 'b' is a syscall catchpoint. */
6603
6604static int
6605syscall_catchpoint_p (struct breakpoint *b)
6606{
6607 return (b->ops == &catch_syscall_breakpoint_ops);
6608}
6609
346774a9
PA
6610/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
6611 is non-zero, then make the breakpoint temporary. If COND_STRING is
6612 not NULL, then store it in the breakpoint. OPS, if not NULL, is
6613 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 6614
346774a9
PA
6615static void
6616init_catchpoint (struct breakpoint *b,
6617 struct gdbarch *gdbarch, int tempflag,
6618 char *cond_string,
6619 struct breakpoint_ops *ops)
c906108c 6620{
c5aa993b 6621 struct symtab_and_line sal;
346774a9 6622
fe39c653 6623 init_sal (&sal);
6c95b8df 6624 sal.pspace = current_program_space;
c5aa993b 6625
28010a5d 6626 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 6627
1b36a34b 6628 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 6629 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
6630}
6631
28010a5d 6632void
3a5c3e22 6633install_breakpoint (int internal, struct breakpoint *b)
c56053d2
PA
6634{
6635 add_to_breakpoint_chain (b);
3a5c3e22
PA
6636 set_breakpoint_number (internal, b);
6637 if (!internal)
6638 mention (b);
c56053d2
PA
6639 observer_notify_breakpoint_created (b);
6640 update_global_location_list (1);
6641}
6642
9b70b993 6643static void
a6d9a66e
UW
6644create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6645 int tempflag, char *cond_string,
ce78b96d 6646 struct breakpoint_ops *ops)
c906108c 6647{
e29a4733 6648 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 6649
e29a4733
PA
6650 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
6651
6652 c->forked_inferior_pid = null_ptid;
6653
3a5c3e22 6654 install_breakpoint (0, &c->base);
c906108c
SS
6655}
6656
fe798b75
JB
6657/* Exec catchpoints. */
6658
b4d90040
PA
6659/* An instance of this type is used to represent an exec catchpoint.
6660 It includes a "struct breakpoint" as a kind of base class; users
6661 downcast to "struct breakpoint *" when needed. A breakpoint is
6662 really of this type iff its ops pointer points to
6663 CATCH_EXEC_BREAKPOINT_OPS. */
6664
6665struct exec_catchpoint
6666{
6667 /* The base class. */
6668 struct breakpoint base;
6669
6670 /* Filename of a program whose exec triggered this catchpoint.
6671 This field is only valid immediately after this catchpoint has
6672 triggered. */
6673 char *exec_pathname;
6674};
6675
6676/* Implement the "dtor" breakpoint_ops method for exec
6677 catchpoints. */
6678
6679static void
6680dtor_catch_exec (struct breakpoint *b)
6681{
6682 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6683
6684 xfree (c->exec_pathname);
348d480f 6685
2060206e 6686 base_breakpoint_ops.dtor (b);
b4d90040
PA
6687}
6688
77b06cd7
TJB
6689static int
6690insert_catch_exec (struct bp_location *bl)
c906108c 6691{
77b06cd7 6692 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6693}
c906108c 6694
fe798b75 6695static int
77b06cd7 6696remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6697{
6698 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6699}
c906108c 6700
fe798b75 6701static int
f1310107
TJB
6702breakpoint_hit_catch_exec (const struct bp_location *bl,
6703 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6704{
b4d90040
PA
6705 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
6706
6707 return inferior_has_execd (inferior_ptid, &c->exec_pathname);
fe798b75 6708}
c906108c 6709
fe798b75 6710static enum print_stop_action
348d480f 6711print_it_catch_exec (bpstat bs)
fe798b75 6712{
348d480f 6713 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
6714 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6715
fe798b75
JB
6716 annotate_catchpoint (b->number);
6717 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
b4d90040 6718 c->exec_pathname);
fe798b75 6719 return PRINT_SRC_AND_LOC;
c906108c
SS
6720}
6721
fe798b75 6722static void
a6d9a66e 6723print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 6724{
b4d90040 6725 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75
JB
6726 struct value_print_options opts;
6727
6728 get_user_print_options (&opts);
6729
6730 /* Field 4, the address, is omitted (which makes the columns
6731 not line up too nicely with the headers, but the effect
6732 is relatively readable). */
6733 if (opts.addressprint)
6734 ui_out_field_skip (uiout, "addr");
6735 annotate_field (5);
6736 ui_out_text (uiout, "exec");
b4d90040 6737 if (c->exec_pathname != NULL)
fe798b75
JB
6738 {
6739 ui_out_text (uiout, ", program \"");
b4d90040 6740 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
6741 ui_out_text (uiout, "\" ");
6742 }
6743}
6744
6745static void
6746print_mention_catch_exec (struct breakpoint *b)
6747{
6748 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6749}
6750
6149aea9
PA
6751/* Implement the "print_recreate" breakpoint_ops method for exec
6752 catchpoints. */
6753
6754static void
6755print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6756{
6757 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 6758 print_recreate_thread (b, fp);
6149aea9
PA
6759}
6760
2060206e 6761static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 6762
a96d9b2e
SDJ
6763static void
6764create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6765 struct breakpoint_ops *ops)
6766{
be5c67c1 6767 struct syscall_catchpoint *c;
a96d9b2e 6768 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 6769
be5c67c1
PA
6770 c = XNEW (struct syscall_catchpoint);
6771 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
6772 c->syscalls_to_be_caught = filter;
a96d9b2e 6773
3a5c3e22 6774 install_breakpoint (0, &c->base);
a96d9b2e
SDJ
6775}
6776
c906108c 6777static int
fba45db2 6778hw_breakpoint_used_count (void)
c906108c 6779{
c906108c 6780 int i = 0;
f1310107
TJB
6781 struct breakpoint *b;
6782 struct bp_location *bl;
c906108c
SS
6783
6784 ALL_BREAKPOINTS (b)
c5aa993b 6785 {
d6b74ac4 6786 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
6787 for (bl = b->loc; bl; bl = bl->next)
6788 {
6789 /* Special types of hardware breakpoints may use more than
6790 one register. */
348d480f 6791 i += b->ops->resources_needed (bl);
f1310107 6792 }
c5aa993b 6793 }
c906108c
SS
6794
6795 return i;
6796}
6797
6798static int
fba45db2 6799hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 6800{
c906108c 6801 int i = 0;
e09342b5
TJB
6802 struct breakpoint *b;
6803 struct bp_location *bl;
c906108c
SS
6804
6805 *other_type_used = 0;
6806 ALL_BREAKPOINTS (b)
e09342b5
TJB
6807 {
6808 if (!breakpoint_enabled (b))
6809 continue;
6810
c5aa993b 6811 if (b->type == type)
e09342b5
TJB
6812 for (bl = b->loc; bl; bl = bl->next)
6813 {
6814 /* Special types of hardware watchpoints may use more than
6815 one register. */
348d480f 6816 i += b->ops->resources_needed (bl);
e09342b5 6817 }
cc60f2e3 6818 else if (is_hardware_watchpoint (b))
c5aa993b 6819 *other_type_used = 1;
e09342b5
TJB
6820 }
6821
c906108c
SS
6822 return i;
6823}
6824
c906108c 6825void
fba45db2 6826disable_watchpoints_before_interactive_call_start (void)
c906108c 6827{
c5aa993b 6828 struct breakpoint *b;
c906108c
SS
6829
6830 ALL_BREAKPOINTS (b)
c5aa993b 6831 {
cc60f2e3 6832 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6833 {
b5de0fa7 6834 b->enable_state = bp_call_disabled;
b60e7edf 6835 update_global_location_list (0);
c5aa993b
JM
6836 }
6837 }
c906108c
SS
6838}
6839
6840void
fba45db2 6841enable_watchpoints_after_interactive_call_stop (void)
c906108c 6842{
c5aa993b 6843 struct breakpoint *b;
c906108c
SS
6844
6845 ALL_BREAKPOINTS (b)
c5aa993b 6846 {
cc60f2e3 6847 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 6848 {
b5de0fa7 6849 b->enable_state = bp_enabled;
b60e7edf 6850 update_global_location_list (1);
c5aa993b
JM
6851 }
6852 }
c906108c
SS
6853}
6854
8bea4e01
UW
6855void
6856disable_breakpoints_before_startup (void)
6857{
6858 struct breakpoint *b;
6859 int found = 0;
6860
6861 ALL_BREAKPOINTS (b)
6862 {
6c95b8df
PA
6863 if (b->pspace != current_program_space)
6864 continue;
6865
8bea4e01
UW
6866 if ((b->type == bp_breakpoint
6867 || b->type == bp_hardware_breakpoint)
6868 && breakpoint_enabled (b))
6869 {
6870 b->enable_state = bp_startup_disabled;
6871 found = 1;
6872 }
6873 }
6874
6875 if (found)
6876 update_global_location_list (0);
6877
6c95b8df 6878 current_program_space->executing_startup = 1;
8bea4e01
UW
6879}
6880
6881void
6882enable_breakpoints_after_startup (void)
6883{
6884 struct breakpoint *b;
6885 int found = 0;
6886
6c95b8df 6887 current_program_space->executing_startup = 0;
8bea4e01
UW
6888
6889 ALL_BREAKPOINTS (b)
6890 {
6c95b8df
PA
6891 if (b->pspace != current_program_space)
6892 continue;
6893
8bea4e01
UW
6894 if ((b->type == bp_breakpoint
6895 || b->type == bp_hardware_breakpoint)
6896 && b->enable_state == bp_startup_disabled)
6897 {
6898 b->enable_state = bp_enabled;
6899 found = 1;
6900 }
6901 }
6902
6903 if (found)
6904 breakpoint_re_set ();
6905}
6906
c906108c
SS
6907
6908/* Set a breakpoint that will evaporate an end of command
6909 at address specified by SAL.
6910 Restrict it to frame FRAME if FRAME is nonzero. */
6911
6912struct breakpoint *
a6d9a66e
UW
6913set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
6914 struct frame_id frame_id, enum bptype type)
c906108c 6915{
52f0bd74 6916 struct breakpoint *b;
edb3359d
DJ
6917
6918 /* If FRAME_ID is valid, it should be a real frame, not an inlined
6919 one. */
6920 gdb_assert (!frame_id_inlined_p (frame_id));
6921
06edf0c0 6922 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
6923 b->enable_state = bp_enabled;
6924 b->disposition = disp_donttouch;
818dd999 6925 b->frame_id = frame_id;
c906108c 6926
4a64f543
MS
6927 /* If we're debugging a multi-threaded program, then we want
6928 momentary breakpoints to be active in only a single thread of
6929 control. */
39f77062
KB
6930 if (in_thread_list (inferior_ptid))
6931 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 6932
b60e7edf 6933 update_global_location_list_nothrow (1);
74960c60 6934
c906108c
SS
6935 return b;
6936}
611c83ae 6937
06edf0c0
PA
6938/* Make a momentary breakpoint based on the master breakpoint ORIG.
6939 The new breakpoint will have type TYPE, and use OPS as it
6940 breakpoint_ops. */
e58b0e63 6941
06edf0c0
PA
6942static struct breakpoint *
6943momentary_breakpoint_from_master (struct breakpoint *orig,
6944 enum bptype type,
6945 struct breakpoint_ops *ops)
e58b0e63
PA
6946{
6947 struct breakpoint *copy;
6948
06edf0c0 6949 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 6950 copy->loc = allocate_bp_location (copy);
0e30163f 6951 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 6952
a6d9a66e 6953 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
6954 copy->loc->requested_address = orig->loc->requested_address;
6955 copy->loc->address = orig->loc->address;
6956 copy->loc->section = orig->loc->section;
6c95b8df 6957 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
6958
6959 if (orig->source_file == NULL)
6960 copy->source_file = NULL;
6961 else
6962 copy->source_file = xstrdup (orig->source_file);
6963
6964 copy->line_number = orig->line_number;
6965 copy->frame_id = orig->frame_id;
6966 copy->thread = orig->thread;
6c95b8df 6967 copy->pspace = orig->pspace;
e58b0e63
PA
6968
6969 copy->enable_state = bp_enabled;
6970 copy->disposition = disp_donttouch;
6971 copy->number = internal_breakpoint_number--;
6972
6973 update_global_location_list_nothrow (0);
6974 return copy;
6975}
6976
06edf0c0
PA
6977/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
6978 ORIG is NULL. */
6979
6980struct breakpoint *
6981clone_momentary_breakpoint (struct breakpoint *orig)
6982{
6983 /* If there's nothing to clone, then return nothing. */
6984 if (orig == NULL)
6985 return NULL;
6986
6987 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
6988}
6989
611c83ae 6990struct breakpoint *
a6d9a66e
UW
6991set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
6992 enum bptype type)
611c83ae
PA
6993{
6994 struct symtab_and_line sal;
6995
6996 sal = find_pc_line (pc, 0);
6997 sal.pc = pc;
6998 sal.section = find_pc_overlay (pc);
6999 sal.explicit_pc = 1;
7000
a6d9a66e 7001 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7002}
c906108c 7003\f
c5aa993b 7004
c906108c
SS
7005/* Tell the user we have just set a breakpoint B. */
7006
7007static void
fba45db2 7008mention (struct breakpoint *b)
c906108c 7009{
348d480f 7010 b->ops->print_mention (b);
9dc5e2a9 7011 if (ui_out_is_mi_like_p (uiout))
fb40c209 7012 return;
c906108c
SS
7013 printf_filtered ("\n");
7014}
c906108c 7015\f
c5aa993b 7016
0d381245 7017static struct bp_location *
39d61571 7018add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7019 const struct symtab_and_line *sal)
7020{
7021 struct bp_location *loc, **tmp;
7022
39d61571 7023 loc = allocate_bp_location (b);
0d381245
VP
7024 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7025 ;
7026 *tmp = loc;
a6d9a66e
UW
7027 loc->gdbarch = get_sal_arch (*sal);
7028 if (!loc->gdbarch)
7029 loc->gdbarch = b->gdbarch;
0d381245 7030 loc->requested_address = sal->pc;
a6d9a66e
UW
7031 loc->address = adjust_breakpoint_address (loc->gdbarch,
7032 loc->requested_address, b->type);
6c95b8df
PA
7033 loc->pspace = sal->pspace;
7034 gdb_assert (loc->pspace != NULL);
0d381245
VP
7035 loc->section = sal->section;
7036
0e30163f
JK
7037 set_breakpoint_location_function (loc,
7038 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7039 return loc;
7040}
514f746b
AR
7041\f
7042
7043/* Return 1 if LOC is pointing to a permanent breakpoint,
7044 return 0 otherwise. */
7045
7046static int
7047bp_loc_is_permanent (struct bp_location *loc)
7048{
7049 int len;
7050 CORE_ADDR addr;
7051 const gdb_byte *brk;
7052 gdb_byte *target_mem;
939c61fa
JK
7053 struct cleanup *cleanup;
7054 int retval = 0;
514f746b
AR
7055
7056 gdb_assert (loc != NULL);
7057
7058 addr = loc->address;
a6d9a66e 7059 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7060
939c61fa
JK
7061 /* Software breakpoints unsupported? */
7062 if (brk == NULL)
7063 return 0;
7064
514f746b
AR
7065 target_mem = alloca (len);
7066
939c61fa
JK
7067 /* Enable the automatic memory restoration from breakpoints while
7068 we read the memory. Otherwise we could say about our temporary
7069 breakpoints they are permanent. */
6c95b8df
PA
7070 cleanup = save_current_space_and_thread ();
7071
7072 switch_to_program_space_and_thread (loc->pspace);
7073 make_show_memory_breakpoints_cleanup (0);
939c61fa 7074
514f746b
AR
7075 if (target_read_memory (loc->address, target_mem, len) == 0
7076 && memcmp (target_mem, brk, len) == 0)
939c61fa 7077 retval = 1;
514f746b 7078
939c61fa
JK
7079 do_cleanups (cleanup);
7080
7081 return retval;
514f746b
AR
7082}
7083
7084
c3f6f71d 7085
018d34a4
VP
7086/* Create a breakpoint with SAL as location. Use ADDR_STRING
7087 as textual description of the location, and COND_STRING
db107f19 7088 as condition expression. */
018d34a4
VP
7089
7090static void
d9b3f62e
PA
7091init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
7092 struct symtabs_and_lines sals, char *addr_string,
7093 char *cond_string,
7094 enum bptype type, enum bpdisp disposition,
7095 int thread, int task, int ignore_count,
7096 struct breakpoint_ops *ops, int from_tty,
7097 int enabled, int internal, int display_canonical)
018d34a4 7098{
0d381245 7099 int i;
018d34a4
VP
7100
7101 if (type == bp_hardware_breakpoint)
7102 {
7103 int i = hw_breakpoint_used_count ();
7104 int target_resources_ok =
d92524f1 7105 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7106 i + 1, 0);
7107 if (target_resources_ok == 0)
7108 error (_("No hardware breakpoint support in the target."));
7109 else if (target_resources_ok < 0)
7110 error (_("Hardware breakpoints used exceeds limit."));
7111 }
7112
6c95b8df
PA
7113 gdb_assert (sals.nelts > 0);
7114
0d381245
VP
7115 for (i = 0; i < sals.nelts; ++i)
7116 {
7117 struct symtab_and_line sal = sals.sals[i];
7118 struct bp_location *loc;
7119
7120 if (from_tty)
5af949e3
UW
7121 {
7122 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7123 if (!loc_gdbarch)
7124 loc_gdbarch = gdbarch;
7125
7126 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7127 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7128 }
0d381245
VP
7129
7130 if (i == 0)
7131 {
d9b3f62e 7132 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 7133 b->thread = thread;
4a306c9a 7134 b->task = task;
018d34a4 7135
0d381245
VP
7136 b->cond_string = cond_string;
7137 b->ignore_count = ignore_count;
41447f92 7138 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7139 b->disposition = disposition;
6c95b8df
PA
7140 b->pspace = sals.sals[0].pspace;
7141
0fb4aa4b
PA
7142 if (type == bp_static_tracepoint)
7143 {
d9b3f62e 7144 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
7145 struct static_tracepoint_marker marker;
7146
7147 if (is_marker_spec (addr_string))
7148 {
7149 /* We already know the marker exists, otherwise, we
7150 wouldn't see a sal for it. */
7151 char *p = &addr_string[3];
7152 char *endp;
7153 char *marker_str;
7154 int i;
7155
e9cafbcc 7156 p = skip_spaces (p);
0fb4aa4b 7157
e9cafbcc 7158 endp = skip_to_space (p);
0fb4aa4b
PA
7159
7160 marker_str = savestring (p, endp - p);
d9b3f62e 7161 t->static_trace_marker_id = marker_str;
0fb4aa4b 7162
3e43a32a
MS
7163 printf_filtered (_("Probed static tracepoint "
7164 "marker \"%s\"\n"),
d9b3f62e 7165 t->static_trace_marker_id);
0fb4aa4b
PA
7166 }
7167 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7168 {
d9b3f62e 7169 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
7170 release_static_tracepoint_marker (&marker);
7171
3e43a32a
MS
7172 printf_filtered (_("Probed static tracepoint "
7173 "marker \"%s\"\n"),
d9b3f62e 7174 t->static_trace_marker_id);
0fb4aa4b
PA
7175 }
7176 else
3e43a32a
MS
7177 warning (_("Couldn't determine the static "
7178 "tracepoint marker to probe"));
0fb4aa4b
PA
7179 }
7180
6c95b8df 7181 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7182 && (b->type == bp_breakpoint
7183 || b->type == bp_hardware_breakpoint))
7184 b->enable_state = bp_startup_disabled;
7185
0d381245
VP
7186 loc = b->loc;
7187 }
7188 else
018d34a4 7189 {
39d61571 7190 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7191 }
7192
514f746b
AR
7193 if (bp_loc_is_permanent (loc))
7194 make_breakpoint_permanent (b);
7195
0d381245
VP
7196 if (b->cond_string)
7197 {
7198 char *arg = b->cond_string;
d32a6982 7199 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7200 if (*arg)
db107f19 7201 error (_("Garbage %s follows condition"), arg);
018d34a4 7202 }
0d381245 7203 }
018d34a4 7204
56435ebe 7205 b->display_canonical = display_canonical;
018d34a4
VP
7206 if (addr_string)
7207 b->addr_string = addr_string;
7208 else
7209 /* addr_string has to be used or breakpoint_re_set will delete
7210 me. */
5af949e3
UW
7211 b->addr_string
7212 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
d9b3f62e 7213}
018d34a4 7214
d9b3f62e
PA
7215static void
7216create_breakpoint_sal (struct gdbarch *gdbarch,
7217 struct symtabs_and_lines sals, char *addr_string,
7218 char *cond_string,
7219 enum bptype type, enum bpdisp disposition,
7220 int thread, int task, int ignore_count,
7221 struct breakpoint_ops *ops, int from_tty,
7222 int enabled, int internal, int display_canonical)
7223{
7224 struct breakpoint *b;
7225 struct cleanup *old_chain;
7226
7227 if (is_tracepoint_type (type))
7228 {
7229 struct tracepoint *t;
7230
7231 t = XCNEW (struct tracepoint);
7232 b = &t->base;
7233 }
7234 else
7235 b = XNEW (struct breakpoint);
7236
7237 old_chain = make_cleanup (xfree, b);
7238
7239 init_breakpoint_sal (b, gdbarch,
7240 sals, addr_string,
7241 cond_string,
7242 type, disposition,
7243 thread, task, ignore_count,
7244 ops, from_tty,
7245 enabled, internal, display_canonical);
7246 discard_cleanups (old_chain);
7247
7248 install_breakpoint (internal, b);
018d34a4
VP
7249}
7250
ed0616c6
VP
7251/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7252 elements to fill the void space. */
2c0b251b
PA
7253static void
7254remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7255{
7256 int i = index_to_remove+1;
7257 int last_index = sal->nelts-1;
7258
7259 for (;i <= last_index; ++i)
7260 sal->sals[i-1] = sal->sals[i];
7261
7262 --(sal->nelts);
7263}
7264
6c95b8df
PA
7265/* If appropriate, obtains all sals that correspond to the same file
7266 and line as SAL, in all program spaces. Users debugging with IDEs,
7267 will want to set a breakpoint at foo.c:line, and not really care
7268 about program spaces. This is done only if SAL does not have
7269 explicit PC and has line and file information. If we got just a
7270 single expanded sal, return the original.
ed0616c6 7271
6c95b8df
PA
7272 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7273 which the name of enclosing function is different from SAL. This
7274 makes sure that if we have breakpoint originally set in template
7275 instantiation, say foo<int>(), we won't expand SAL to locations at
7276 the same line in all existing instantiations of 'foo'. */
ed0616c6 7277
2c0b251b 7278static struct symtabs_and_lines
ed0616c6
VP
7279expand_line_sal_maybe (struct symtab_and_line sal)
7280{
7281 struct symtabs_and_lines expanded;
7282 CORE_ADDR original_pc = sal.pc;
7283 char *original_function = NULL;
7284 int found;
7285 int i;
6c95b8df 7286 struct cleanup *old_chain;
ed0616c6
VP
7287
7288 /* If we have explicit pc, don't expand.
7289 If we have no line number, we can't expand. */
7290 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7291 {
7292 expanded.nelts = 1;
7293 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7294 expanded.sals[0] = sal;
7295 return expanded;
7296 }
7297
7298 sal.pc = 0;
6c95b8df
PA
7299
7300 old_chain = save_current_space_and_thread ();
7301
7302 switch_to_program_space_and_thread (sal.pspace);
7303
ed0616c6 7304 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7305
7306 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7307 expanded = expand_line_sal (sal);
6c95b8df 7308
ed0616c6
VP
7309 if (expanded.nelts == 1)
7310 {
3dba1c98
JB
7311 /* We had one sal, we got one sal. Return that sal, adjusting it
7312 past the function prologue if necessary. */
ed0616c6
VP
7313 xfree (expanded.sals);
7314 expanded.nelts = 1;
7315 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7316 sal.pc = original_pc;
7317 expanded.sals[0] = sal;
3dba1c98 7318 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7319 do_cleanups (old_chain);
ed0616c6
VP
7320 return expanded;
7321 }
7322
7323 if (!sal.explicit_line)
7324 {
7325 CORE_ADDR func_addr, func_end;
7326 for (i = 0; i < expanded.nelts; ++i)
7327 {
7328 CORE_ADDR pc = expanded.sals[i].pc;
7329 char *this_function;
6c95b8df
PA
7330
7331 /* We need to switch threads as well since we're about to
7332 read memory. */
7333 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7334
ed0616c6
VP
7335 if (find_pc_partial_function (pc, &this_function,
7336 &func_addr, &func_end))
7337 {
059fb39f
PM
7338 if (this_function
7339 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7340 {
7341 remove_sal (&expanded, i);
7342 --i;
7343 }
ed0616c6
VP
7344 }
7345 }
7346 }
059acae7
UW
7347
7348 /* Skip the function prologue if necessary. */
7349 for (i = 0; i < expanded.nelts; ++i)
7350 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7351
6c95b8df
PA
7352 do_cleanups (old_chain);
7353
ed0616c6
VP
7354 if (expanded.nelts <= 1)
7355 {
e5dd4106 7356 /* This is an ugly workaround. If we get zero expanded sals
4a64f543
MS
7357 then something is really wrong. Fix that by returning the
7358 original sal. */
7359
ed0616c6
VP
7360 xfree (expanded.sals);
7361 expanded.nelts = 1;
7362 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7363 sal.pc = original_pc;
7364 expanded.sals[0] = sal;
7365 return expanded;
7366 }
7367
7368 if (original_pc)
7369 {
7370 found = 0;
7371 for (i = 0; i < expanded.nelts; ++i)
7372 if (expanded.sals[i].pc == original_pc)
7373 {
7374 found = 1;
7375 break;
7376 }
7377 gdb_assert (found);
7378 }
7379
7380 return expanded;
7381}
7382
018d34a4
VP
7383/* Add SALS.nelts breakpoints to the breakpoint table. For each
7384 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7385 value. COND_STRING, if not NULL, specified the condition to be
7386 used for all breakpoints. Essentially the only case where
7387 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7388 function. In that case, it's still not possible to specify
7389 separate conditions for different overloaded functions, so
7390 we take just a single condition string.
7391
c3f6f71d 7392 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7393 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7394 array contents). If the function fails (error() is called), the
7395 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7396 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7397
7398static void
8cdf0e15 7399create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2
TT
7400 struct symtabs_and_lines sals,
7401 struct linespec_result *canonical,
8cdf0e15
VP
7402 char *cond_string,
7403 enum bptype type, enum bpdisp disposition,
7404 int thread, int task, int ignore_count,
7405 struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7406 int enabled, int internal)
c906108c 7407{
018d34a4 7408 int i;
cc59ec59 7409
018d34a4 7410 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7411 {
ed0616c6
VP
7412 struct symtabs_and_lines expanded =
7413 expand_line_sal_maybe (sals.sals[i]);
0d381245 7414
7efd8fc2 7415 create_breakpoint_sal (gdbarch, expanded, canonical->canonical[i],
8cdf0e15 7416 cond_string, type, disposition,
84f4c1fe 7417 thread, task, ignore_count, ops,
56435ebe
TT
7418 from_tty, enabled, internal,
7419 canonical->special_display);
c3f6f71d 7420 }
c3f6f71d 7421}
c906108c 7422
9998af43 7423/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7424 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7425 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7426 address strings. ADDRESS points to the end of the SAL.
7427
7428 The array and the line spec strings are allocated on the heap, it is
7429 the caller's responsibility to free them. */
c906108c 7430
b9362cc7 7431static void
c3f6f71d
JM
7432parse_breakpoint_sals (char **address,
7433 struct symtabs_and_lines *sals,
58438ac1 7434 struct linespec_result *canonical)
c3f6f71d
JM
7435{
7436 char *addr_start = *address;
cc59ec59 7437
c3f6f71d 7438 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7439 breakpoint. */
c3f6f71d
JM
7440 if ((*address) == NULL
7441 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c
SS
7442 {
7443 if (default_breakpoint_valid)
7444 {
c3f6f71d 7445 struct symtab_and_line sal;
cc59ec59 7446
4a64f543 7447 init_sal (&sal); /* Initialize to zeroes. */
c3f6f71d 7448 sals->sals = (struct symtab_and_line *)
c906108c
SS
7449 xmalloc (sizeof (struct symtab_and_line));
7450 sal.pc = default_breakpoint_address;
7451 sal.line = default_breakpoint_line;
7452 sal.symtab = default_breakpoint_symtab;
6c95b8df 7453 sal.pspace = default_breakpoint_pspace;
c5aa993b 7454 sal.section = find_pc_overlay (sal.pc);
00903456 7455
4a64f543
MS
7456 /* "break" without arguments is equivalent to "break *PC"
7457 where PC is the default_breakpoint_address. So make sure
7458 to set sal.explicit_pc to prevent GDB from trying to
7459 expand the list of sals to include all other instances
7460 with the same symtab and line. */
00903456
JK
7461 sal.explicit_pc = 1;
7462
c3f6f71d
JM
7463 sals->sals[0] = sal;
7464 sals->nelts = 1;
c906108c
SS
7465 }
7466 else
8a3fe4f8 7467 error (_("No default breakpoint address now."));
c906108c
SS
7468 }
7469 else
7470 {
c906108c 7471 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7472 current_source_symtab (which is decode_line_1's default).
7473 This should produce the results we want almost all of the
7474 time while leaving default_breakpoint_* alone.
7475
1aeae86e
AF
7476 ObjC: However, don't match an Objective-C method name which
7477 may have a '+' or '-' succeeded by a '[' */
0378c332 7478
c214a6fd 7479 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 7480
c906108c 7481 if (default_breakpoint_valid
0378c332 7482 && (!cursal.symtab
1aeae86e
AF
7483 || ((strchr ("+-", (*address)[0]) != NULL)
7484 && ((*address)[1] != '['))))
c3f6f71d 7485 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
58438ac1 7486 default_breakpoint_line, canonical);
c906108c 7487 else
0101ce28 7488 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
58438ac1 7489 canonical);
c906108c 7490 }
4a64f543 7491 /* For any SAL that didn't have a canonical string, fill one in. */
7efd8fc2 7492 if (sals->nelts > 0 && canonical->canonical == NULL)
38a714bb 7493 canonical->canonical = xcalloc (sals->nelts, sizeof (char *));
c3f6f71d 7494 if (addr_start != (*address))
c906108c 7495 {
c3f6f71d 7496 int i;
cc59ec59 7497
c3f6f71d 7498 for (i = 0; i < sals->nelts; i++)
c906108c 7499 {
4a64f543 7500 /* Add the string if not present. */
7efd8fc2
TT
7501 if (canonical->canonical[i] == NULL)
7502 canonical->canonical[i] = savestring (addr_start,
7503 (*address) - addr_start);
c906108c
SS
7504 }
7505 }
c3f6f71d 7506}
c906108c 7507
c906108c 7508
c3f6f71d 7509/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7510 inserted as a breakpoint. If it can't throw an error. */
c906108c 7511
b9362cc7 7512static void
23e7acfb 7513breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7514{
7515 int i;
cc59ec59 7516
c3f6f71d 7517 for (i = 0; i < sals->nelts; i++)
ee53e872 7518 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7519}
7520
7a697b8d
SS
7521/* Fast tracepoints may have restrictions on valid locations. For
7522 instance, a fast tracepoint using a jump instead of a trap will
7523 likely have to overwrite more bytes than a trap would, and so can
7524 only be placed where the instruction is longer than the jump, or a
7525 multi-instruction sequence does not have a jump into the middle of
7526 it, etc. */
7527
7528static void
7529check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7530 struct symtabs_and_lines *sals)
7531{
7532 int i, rslt;
7533 struct symtab_and_line *sal;
7534 char *msg;
7535 struct cleanup *old_chain;
7536
7537 for (i = 0; i < sals->nelts; i++)
7538 {
7539 sal = &sals->sals[i];
7540
7541 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7542 NULL, &msg);
7543 old_chain = make_cleanup (xfree, msg);
7544
7545 if (!rslt)
7546 error (_("May not have a fast tracepoint at 0x%s%s"),
7547 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7548
7549 do_cleanups (old_chain);
7550 }
7551}
7552
018d34a4
VP
7553/* Given TOK, a string specification of condition and thread, as
7554 accepted by the 'break' command, extract the condition
7555 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7556 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7557 If no condition is found, *COND_STRING is set to NULL.
7558 If no thread is found, *THREAD is set to -1. */
7559static void
7560find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7561 char **cond_string, int *thread, int *task)
018d34a4
VP
7562{
7563 *cond_string = NULL;
7564 *thread = -1;
7565 while (tok && *tok)
7566 {
7567 char *end_tok;
7568 int toklen;
7569 char *cond_start = NULL;
7570 char *cond_end = NULL;
cc59ec59 7571
e9cafbcc 7572 tok = skip_spaces (tok);
018d34a4 7573
e9cafbcc 7574 end_tok = skip_to_space (tok);
018d34a4
VP
7575
7576 toklen = end_tok - tok;
7577
7578 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7579 {
f7545552
TT
7580 struct expression *expr;
7581
018d34a4 7582 tok = cond_start = end_tok + 1;
f7545552
TT
7583 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7584 xfree (expr);
018d34a4
VP
7585 cond_end = tok;
7586 *cond_string = savestring (cond_start,
7587 cond_end - cond_start);
7588 }
7589 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7590 {
7591 char *tmptok;
7592
7593 tok = end_tok + 1;
7594 tmptok = tok;
7595 *thread = strtol (tok, &tok, 0);
7596 if (tok == tmptok)
7597 error (_("Junk after thread keyword."));
7598 if (!valid_thread_id (*thread))
7599 error (_("Unknown thread %d."), *thread);
7600 }
4a306c9a
JB
7601 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7602 {
7603 char *tmptok;
7604
7605 tok = end_tok + 1;
7606 tmptok = tok;
7607 *task = strtol (tok, &tok, 0);
7608 if (tok == tmptok)
7609 error (_("Junk after task keyword."));
7610 if (!valid_task_id (*task))
b6199126 7611 error (_("Unknown task %d."), *task);
4a306c9a 7612 }
018d34a4
VP
7613 else
7614 error (_("Junk at end of arguments."));
7615 }
7616}
7617
0fb4aa4b
PA
7618/* Decode a static tracepoint marker spec. */
7619
7620static struct symtabs_and_lines
7621decode_static_tracepoint_spec (char **arg_p)
7622{
7623 VEC(static_tracepoint_marker_p) *markers = NULL;
7624 struct symtabs_and_lines sals;
7625 struct symtab_and_line sal;
7626 struct symbol *sym;
7627 struct cleanup *old_chain;
7628 char *p = &(*arg_p)[3];
7629 char *endp;
7630 char *marker_str;
7631 int i;
7632
e9cafbcc 7633 p = skip_spaces (p);
0fb4aa4b 7634
e9cafbcc 7635 endp = skip_to_space (p);
0fb4aa4b
PA
7636
7637 marker_str = savestring (p, endp - p);
7638 old_chain = make_cleanup (xfree, marker_str);
7639
7640 markers = target_static_tracepoint_markers_by_strid (marker_str);
7641 if (VEC_empty(static_tracepoint_marker_p, markers))
7642 error (_("No known static tracepoint marker named %s"), marker_str);
7643
7644 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7645 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7646
7647 for (i = 0; i < sals.nelts; i++)
7648 {
7649 struct static_tracepoint_marker *marker;
7650
7651 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7652
7653 init_sal (&sals.sals[i]);
7654
7655 sals.sals[i] = find_pc_line (marker->address, 0);
7656 sals.sals[i].pc = marker->address;
7657
7658 release_static_tracepoint_marker (marker);
7659 }
7660
7661 do_cleanups (old_chain);
7662
7663 *arg_p = endp;
7664 return sals;
7665}
7666
fd9b8c24
PA
7667/* Set a breakpoint. This function is shared between CLI and MI
7668 functions for setting a breakpoint. This function has two major
7669 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7670 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7671 breakpoint location, address and thread. Otherwise, ARG is just
7672 the location of breakpoint, with condition and thread specified by
7673 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7674 the breakpoint number will be allocated from the internal
7675 breakpoint count. Returns true if any breakpoint was created;
7676 false otherwise. */
0101ce28 7677
8cdf0e15
VP
7678int
7679create_breakpoint (struct gdbarch *gdbarch,
7680 char *arg, char *cond_string, int thread,
7681 int parse_condition_and_thread,
0fb4aa4b 7682 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7683 int ignore_count,
7684 enum auto_boolean pending_break_support,
7685 struct breakpoint_ops *ops,
84f4c1fe 7686 int from_tty, int enabled, int internal)
c3f6f71d 7687{
b78a6381 7688 volatile struct gdb_exception e;
c3f6f71d 7689 struct symtabs_and_lines sals;
0101ce28 7690 struct symtab_and_line pending_sal;
0101ce28 7691 char *copy_arg;
c3f6f71d 7692 char *addr_start = arg;
7efd8fc2 7693 struct linespec_result canonical;
c3f6f71d 7694 struct cleanup *old_chain;
80c99de1 7695 struct cleanup *bkpt_chain = NULL;
05ff989b 7696 int i;
0101ce28 7697 int pending = 0;
4a306c9a 7698 int task = 0;
86b17b60 7699 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7700
348d480f
PA
7701 gdb_assert (ops != NULL);
7702
c3f6f71d
JM
7703 sals.sals = NULL;
7704 sals.nelts = 0;
7efd8fc2 7705 init_linespec_result (&canonical);
c3f6f71d 7706
0fb4aa4b
PA
7707 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7708 {
7709 int i;
7710
7711 sals = decode_static_tracepoint_spec (&arg);
7712
7713 copy_arg = savestring (addr_start, arg - addr_start);
38a714bb 7714 canonical.canonical = xcalloc (sals.nelts, sizeof (char *));
0fb4aa4b 7715 for (i = 0; i < sals.nelts; i++)
7efd8fc2 7716 canonical.canonical[i] = xstrdup (copy_arg);
0fb4aa4b
PA
7717 goto done;
7718 }
7719
b78a6381
TT
7720 TRY_CATCH (e, RETURN_MASK_ALL)
7721 {
58438ac1 7722 parse_breakpoint_sals (&arg, &sals, &canonical);
b78a6381 7723 }
0101ce28
JJ
7724
7725 /* If caller is interested in rc value from parse, set value. */
05ff989b 7726 switch (e.reason)
0101ce28 7727 {
05ff989b 7728 case RETURN_QUIT:
98deb0da 7729 throw_exception (e);
05ff989b
AC
7730 case RETURN_ERROR:
7731 switch (e.error)
0101ce28 7732 {
05ff989b 7733 case NOT_FOUND_ERROR:
0101ce28 7734
05ff989b
AC
7735 /* If pending breakpoint support is turned off, throw
7736 error. */
fa8d40ab
JJ
7737
7738 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7739 throw_exception (e);
7740
7741 exception_print (gdb_stderr, e);
fa8d40ab 7742
05ff989b
AC
7743 /* If pending breakpoint support is auto query and the user
7744 selects no, then simply return the error code. */
059fb39f 7745 if (pending_break_support == AUTO_BOOLEAN_AUTO
3e43a32a
MS
7746 && !nquery (_("Make breakpoint pending on "
7747 "future shared library load? ")))
fd9b8c24 7748 return 0;
fa8d40ab 7749
05ff989b
AC
7750 /* At this point, either the user was queried about setting
7751 a pending breakpoint and selected yes, or pending
7752 breakpoint behavior is on and thus a pending breakpoint
7753 is defaulted on behalf of the user. */
0101ce28 7754 copy_arg = xstrdup (addr_start);
7efd8fc2 7755 canonical.canonical = &copy_arg;
0101ce28
JJ
7756 sals.nelts = 1;
7757 sals.sals = &pending_sal;
7758 pending_sal.pc = 0;
7759 pending = 1;
05ff989b
AC
7760 break;
7761 default:
98deb0da 7762 throw_exception (e);
0101ce28 7763 }
2abae994 7764 break;
05ff989b
AC
7765 default:
7766 if (!sals.nelts)
fd9b8c24 7767 return 0;
0101ce28 7768 }
c3f6f71d 7769
0fb4aa4b
PA
7770 done:
7771
4a64f543 7772 /* Create a chain of things that always need to be cleaned up. */
c3f6f71d
JM
7773 old_chain = make_cleanup (null_cleanup, 0);
7774
0101ce28
JJ
7775 if (!pending)
7776 {
7777 /* Make sure that all storage allocated to SALS gets freed. */
7778 make_cleanup (xfree, sals.sals);
7779
7efd8fc2
TT
7780 /* Cleanup the canonical array but not its contents. */
7781 make_cleanup (xfree, canonical.canonical);
0101ce28 7782 }
c3f6f71d 7783
c3f6f71d
JM
7784 /* ----------------------------- SNIP -----------------------------
7785 Anything added to the cleanup chain beyond this point is assumed
7786 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7787 then the memory is not reclaimed. */
7788 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 7789
7efd8fc2 7790 /* Mark the contents of the canonical for cleanup. These go on
80c99de1 7791 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
7792 for (i = 0; i < sals.nelts; i++)
7793 {
7efd8fc2
TT
7794 if (canonical.canonical[i] != NULL)
7795 make_cleanup (xfree, canonical.canonical[i]);
c3f6f71d
JM
7796 }
7797
7798 /* Resolve all line numbers to PC's and verify that the addresses
7799 are ok for the target. */
0101ce28 7800 if (!pending)
23e7acfb 7801 breakpoint_sals_to_pc (&sals);
c3f6f71d 7802
7a697b8d
SS
7803 /* Fast tracepoints may have additional restrictions on location. */
7804 if (type_wanted == bp_fast_tracepoint)
7805 check_fast_tracepoint_sals (gdbarch, &sals);
7806
c3f6f71d
JM
7807 /* Verify that condition can be parsed, before setting any
7808 breakpoints. Allocate a separate condition expression for each
4a64f543 7809 breakpoint. */
0101ce28 7810 if (!pending)
c3f6f71d 7811 {
2f069f6f 7812 if (parse_condition_and_thread)
72b2ff0e
VP
7813 {
7814 /* Here we only parse 'arg' to separate condition
7815 from thread number, so parsing in context of first
7816 sal is OK. When setting the breakpoint we'll
7817 re-parse it in context of each sal. */
7818 cond_string = NULL;
7819 thread = -1;
4a306c9a
JB
7820 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
7821 &thread, &task);
72b2ff0e
VP
7822 if (cond_string)
7823 make_cleanup (xfree, cond_string);
7824 }
2f069f6f 7825 else
72b2ff0e
VP
7826 {
7827 /* Create a private copy of condition string. */
7828 if (cond_string)
7829 {
7830 cond_string = xstrdup (cond_string);
7831 make_cleanup (xfree, cond_string);
7832 }
7833 }
0fb4aa4b
PA
7834
7835 /* If the user is creating a static tracepoint by marker id
7836 (strace -m MARKER_ID), then store the sals index, so that
7837 breakpoint_re_set can try to match up which of the newly
7838 found markers corresponds to this one, and, don't try to
7839 expand multiple locations for each sal, given than SALS
7840 already should contain all sals for MARKER_ID. */
7841 if (type_wanted == bp_static_tracepoint
7efd8fc2 7842 && is_marker_spec (canonical.canonical[0]))
0fb4aa4b
PA
7843 {
7844 int i;
7845
7846 for (i = 0; i < sals.nelts; ++i)
7847 {
7848 struct symtabs_and_lines expanded;
d9b3f62e 7849 struct tracepoint *tp;
0fb4aa4b
PA
7850 struct cleanup *old_chain;
7851
7852 expanded.nelts = 1;
7853 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7854 expanded.sals[0] = sals.sals[i];
7855 old_chain = make_cleanup (xfree, expanded.sals);
7856
d9b3f62e
PA
7857 tp = XCNEW (struct tracepoint);
7858 init_breakpoint_sal (&tp->base, gdbarch, expanded,
7859 canonical.canonical[i],
7860 cond_string, type_wanted,
7861 tempflag ? disp_del : disp_donttouch,
7862 thread, task, ignore_count, ops,
7863 from_tty, enabled, internal,
7864 canonical.special_display);
0fb4aa4b
PA
7865 /* Given that its possible to have multiple markers with
7866 the same string id, if the user is creating a static
7867 tracepoint by marker id ("strace -m MARKER_ID"), then
7868 store the sals index, so that breakpoint_re_set can
7869 try to match up which of the newly found markers
7870 corresponds to this one */
7871 tp->static_trace_marker_id_idx = i;
d9b3f62e
PA
7872
7873 install_breakpoint (internal, &tp->base);
7874
7875 do_cleanups (old_chain);
0fb4aa4b
PA
7876 }
7877 }
7878 else
7efd8fc2 7879 create_breakpoints_sal (gdbarch, sals, &canonical, cond_string,
3e43a32a
MS
7880 type_wanted,
7881 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 7882 thread, task, ignore_count, ops, from_tty,
84f4c1fe 7883 enabled, internal);
c906108c 7884 }
0101ce28
JJ
7885 else
7886 {
0101ce28
JJ
7887 struct breakpoint *b;
7888
0101ce28
JJ
7889 make_cleanup (xfree, copy_arg);
7890
348d480f 7891 b = set_raw_breakpoint_without_location (gdbarch, type_wanted, ops);
84f4c1fe 7892 set_breakpoint_number (internal, b);
72b2ff0e 7893 b->thread = -1;
7efd8fc2 7894 b->addr_string = canonical.canonical[0];
72b2ff0e 7895 b->cond_string = NULL;
0101ce28 7896 b->ignore_count = ignore_count;
0101ce28 7897 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 7898 b->condition_not_parsed = 1;
41447f92 7899 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 7900 b->pspace = current_program_space;
84f4c1fe 7901 b->py_bp_object = NULL;
74960c60 7902
6c95b8df 7903 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7904 && (b->type == bp_breakpoint
7905 || b->type == bp_hardware_breakpoint))
7906 b->enable_state = bp_startup_disabled;
7907
8d3788bd 7908 if (!internal)
84f4c1fe
PM
7909 /* Do not mention breakpoints with a negative number,
7910 but do notify observers. */
8d3788bd
VP
7911 mention (b);
7912 observer_notify_breakpoint_created (b);
0101ce28
JJ
7913 }
7914
c3f6f71d 7915 if (sals.nelts > 1)
95a42b64 7916 {
3e43a32a
MS
7917 warning (_("Multiple breakpoints were set.\nUse the "
7918 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 7919 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
7920 }
7921
80c99de1
PA
7922 /* That's it. Discard the cleanups for data inserted into the
7923 breakpoint. */
7924 discard_cleanups (bkpt_chain);
7925 /* But cleanup everything else. */
c3f6f71d 7926 do_cleanups (old_chain);
217dc9e2 7927
80c99de1 7928 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 7929 update_global_location_list (1);
fd9b8c24
PA
7930
7931 return 1;
c3f6f71d 7932}
c906108c 7933
348d480f 7934/* Set a breakpoint.
72b2ff0e
VP
7935 ARG is a string describing breakpoint address,
7936 condition, and thread.
7937 FLAG specifies if a breakpoint is hardware on,
7938 and if breakpoint is temporary, using BP_HARDWARE_FLAG
7939 and BP_TEMPFLAG. */
348d480f 7940
98deb0da 7941static void
72b2ff0e 7942break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 7943{
72b2ff0e 7944 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
7945 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
7946 ? bp_hardware_breakpoint
7947 : bp_breakpoint);
c3f6f71d 7948
8cdf0e15
VP
7949 create_breakpoint (get_current_arch (),
7950 arg,
7951 NULL, 0, 1 /* parse arg */,
0fb4aa4b 7952 tempflag, type_wanted,
8cdf0e15
VP
7953 0 /* Ignore count */,
7954 pending_break_support,
348d480f 7955 &bkpt_breakpoint_ops,
8cdf0e15 7956 from_tty,
84f4c1fe
PM
7957 1 /* enabled */,
7958 0 /* internal */);
c906108c
SS
7959}
7960
c906108c
SS
7961/* Helper function for break_command_1 and disassemble_command. */
7962
7963void
fba45db2 7964resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
7965{
7966 CORE_ADDR pc;
7967
7968 if (sal->pc == 0 && sal->symtab != NULL)
7969 {
7970 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 7971 error (_("No line %d in file \"%s\"."),
c906108c
SS
7972 sal->line, sal->symtab->filename);
7973 sal->pc = pc;
6a048695 7974
4a64f543
MS
7975 /* If this SAL corresponds to a breakpoint inserted using a line
7976 number, then skip the function prologue if necessary. */
6a048695 7977 if (sal->explicit_line)
059acae7 7978 skip_prologue_sal (sal);
c906108c
SS
7979 }
7980
7981 if (sal->section == 0 && sal->symtab != NULL)
7982 {
7983 struct blockvector *bv;
c5aa993b
JM
7984 struct block *b;
7985 struct symbol *sym;
c906108c 7986
801e3a5b 7987 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
7988 if (bv != NULL)
7989 {
7f0df278 7990 sym = block_linkage_function (b);
c906108c
SS
7991 if (sym != NULL)
7992 {
7993 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 7994 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
7995 }
7996 else
7997 {
4a64f543
MS
7998 /* It really is worthwhile to have the section, so we'll
7999 just have to look harder. This case can be executed
8000 if we have line numbers but no functions (as can
8001 happen in assembly source). */
c906108c 8002
c5aa993b 8003 struct minimal_symbol *msym;
6c95b8df
PA
8004 struct cleanup *old_chain = save_current_space_and_thread ();
8005
8006 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8007
8008 msym = lookup_minimal_symbol_by_pc (sal->pc);
8009 if (msym)
714835d5 8010 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8011
8012 do_cleanups (old_chain);
c906108c
SS
8013 }
8014 }
8015 }
8016}
8017
8018void
fba45db2 8019break_command (char *arg, int from_tty)
c906108c 8020{
db107f19 8021 break_command_1 (arg, 0, from_tty);
c906108c
SS
8022}
8023
c906108c 8024void
fba45db2 8025tbreak_command (char *arg, int from_tty)
c906108c 8026{
db107f19 8027 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8028}
8029
c906108c 8030static void
fba45db2 8031hbreak_command (char *arg, int from_tty)
c906108c 8032{
db107f19 8033 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8034}
8035
8036static void
fba45db2 8037thbreak_command (char *arg, int from_tty)
c906108c 8038{
db107f19 8039 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8040}
8041
8042static void
fba45db2 8043stop_command (char *arg, int from_tty)
c906108c 8044{
a3f17187 8045 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8046Usage: stop in <function | address>\n\
a3f17187 8047 stop at <line>\n"));
c906108c
SS
8048}
8049
8050static void
fba45db2 8051stopin_command (char *arg, int from_tty)
c906108c
SS
8052{
8053 int badInput = 0;
8054
c5aa993b 8055 if (arg == (char *) NULL)
c906108c
SS
8056 badInput = 1;
8057 else if (*arg != '*')
8058 {
8059 char *argptr = arg;
8060 int hasColon = 0;
8061
4a64f543 8062 /* Look for a ':'. If this is a line number specification, then
53a5351d 8063 say it is bad, otherwise, it should be an address or
4a64f543 8064 function/method name. */
c906108c 8065 while (*argptr && !hasColon)
c5aa993b
JM
8066 {
8067 hasColon = (*argptr == ':');
8068 argptr++;
8069 }
c906108c
SS
8070
8071 if (hasColon)
c5aa993b 8072 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8073 else
c5aa993b 8074 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8075 }
8076
8077 if (badInput)
a3f17187 8078 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8079 else
db107f19 8080 break_command_1 (arg, 0, from_tty);
c906108c
SS
8081}
8082
8083static void
fba45db2 8084stopat_command (char *arg, int from_tty)
c906108c
SS
8085{
8086 int badInput = 0;
8087
c5aa993b 8088 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8089 badInput = 1;
8090 else
8091 {
8092 char *argptr = arg;
8093 int hasColon = 0;
8094
4a64f543
MS
8095 /* Look for a ':'. If there is a '::' then get out, otherwise
8096 it is probably a line number. */
c906108c 8097 while (*argptr && !hasColon)
c5aa993b
JM
8098 {
8099 hasColon = (*argptr == ':');
8100 argptr++;
8101 }
c906108c
SS
8102
8103 if (hasColon)
c5aa993b 8104 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8105 else
c5aa993b 8106 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8107 }
8108
8109 if (badInput)
a3f17187 8110 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8111 else
db107f19 8112 break_command_1 (arg, 0, from_tty);
c906108c
SS
8113}
8114
f1310107
TJB
8115/* Implement the "breakpoint_hit" breakpoint_ops method for
8116 ranged breakpoints. */
8117
8118static int
8119breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8120 struct address_space *aspace,
8121 CORE_ADDR bp_addr)
8122{
8123 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8124 bl->length, aspace, bp_addr);
8125}
8126
8127/* Implement the "resources_needed" breakpoint_ops method for
8128 ranged breakpoints. */
8129
8130static int
8131resources_needed_ranged_breakpoint (const struct bp_location *bl)
8132{
8133 return target_ranged_break_num_registers ();
8134}
8135
8136/* Implement the "print_it" breakpoint_ops method for
8137 ranged breakpoints. */
8138
8139static enum print_stop_action
348d480f 8140print_it_ranged_breakpoint (bpstat bs)
f1310107 8141{
348d480f 8142 struct breakpoint *b = bs->breakpoint_at;
f1310107
TJB
8143 struct bp_location *bl = b->loc;
8144
8145 gdb_assert (b->type == bp_hardware_breakpoint);
8146
8147 /* Ranged breakpoints have only one location. */
8148 gdb_assert (bl && bl->next == NULL);
8149
8150 annotate_breakpoint (b->number);
8151 if (b->disposition == disp_del)
8152 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8153 else
8154 ui_out_text (uiout, "\nRanged breakpoint ");
8155 if (ui_out_is_mi_like_p (uiout))
8156 {
8157 ui_out_field_string (uiout, "reason",
8158 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8159 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8160 }
8161 ui_out_field_int (uiout, "bkptno", b->number);
8162 ui_out_text (uiout, ", ");
8163
8164 return PRINT_SRC_AND_LOC;
8165}
8166
8167/* Implement the "print_one" breakpoint_ops method for
8168 ranged breakpoints. */
8169
8170static void
8171print_one_ranged_breakpoint (struct breakpoint *b,
8172 struct bp_location **last_loc)
8173{
8174 struct bp_location *bl = b->loc;
8175 struct value_print_options opts;
8176
8177 /* Ranged breakpoints have only one location. */
8178 gdb_assert (bl && bl->next == NULL);
8179
8180 get_user_print_options (&opts);
8181
8182 if (opts.addressprint)
8183 /* We don't print the address range here, it will be printed later
8184 by print_one_detail_ranged_breakpoint. */
8185 ui_out_field_skip (uiout, "addr");
8186 annotate_field (5);
8187 print_breakpoint_location (b, bl);
8188 *last_loc = bl;
8189}
8190
8191/* Implement the "print_one_detail" breakpoint_ops method for
8192 ranged breakpoints. */
8193
8194static void
8195print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8196 struct ui_out *uiout)
8197{
8198 CORE_ADDR address_start, address_end;
8199 struct bp_location *bl = b->loc;
8200 struct ui_stream *stb = ui_out_stream_new (uiout);
8201 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8202
8203 gdb_assert (bl);
8204
8205 address_start = bl->address;
8206 address_end = address_start + bl->length - 1;
8207
8208 ui_out_text (uiout, "\taddress range: ");
8209 fprintf_unfiltered (stb->stream, "[%s, %s]",
8210 print_core_address (bl->gdbarch, address_start),
8211 print_core_address (bl->gdbarch, address_end));
8212 ui_out_field_stream (uiout, "addr", stb);
8213 ui_out_text (uiout, "\n");
8214
8215 do_cleanups (cleanup);
8216}
8217
8218/* Implement the "print_mention" breakpoint_ops method for
8219 ranged breakpoints. */
8220
8221static void
8222print_mention_ranged_breakpoint (struct breakpoint *b)
8223{
8224 struct bp_location *bl = b->loc;
8225
8226 gdb_assert (bl);
8227 gdb_assert (b->type == bp_hardware_breakpoint);
8228
8229 if (ui_out_is_mi_like_p (uiout))
8230 return;
8231
8232 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8233 b->number, paddress (bl->gdbarch, bl->address),
8234 paddress (bl->gdbarch, bl->address + bl->length - 1));
8235}
8236
8237/* Implement the "print_recreate" breakpoint_ops method for
8238 ranged breakpoints. */
8239
8240static void
8241print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8242{
8243 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8244 b->addr_string_range_end);
d9b3f62e 8245 print_recreate_thread (b, fp);
f1310107
TJB
8246}
8247
8248/* The breakpoint_ops structure to be used in ranged breakpoints. */
8249
2060206e 8250static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
8251
8252/* Find the address where the end of the breakpoint range should be
8253 placed, given the SAL of the end of the range. This is so that if
8254 the user provides a line number, the end of the range is set to the
8255 last instruction of the given line. */
8256
8257static CORE_ADDR
8258find_breakpoint_range_end (struct symtab_and_line sal)
8259{
8260 CORE_ADDR end;
8261
8262 /* If the user provided a PC value, use it. Otherwise,
8263 find the address of the end of the given location. */
8264 if (sal.explicit_pc)
8265 end = sal.pc;
8266 else
8267 {
8268 int ret;
8269 CORE_ADDR start;
8270
8271 ret = find_line_pc_range (sal, &start, &end);
8272 if (!ret)
8273 error (_("Could not find location of the end of the range."));
8274
8275 /* find_line_pc_range returns the start of the next line. */
8276 end--;
8277 }
8278
8279 return end;
8280}
8281
8282/* Implement the "break-range" CLI command. */
8283
8284static void
8285break_range_command (char *arg, int from_tty)
8286{
8287 char *arg_start, *addr_string_start, *addr_string_end;
8288 struct linespec_result canonical_start, canonical_end;
8289 int bp_count, can_use_bp, length;
8290 CORE_ADDR end;
8291 struct breakpoint *b;
8292 struct symtab_and_line sal_start, sal_end;
8293 struct symtabs_and_lines sals_start, sals_end;
8294 struct cleanup *cleanup_bkpt;
8295
8296 /* We don't support software ranged breakpoints. */
8297 if (target_ranged_break_num_registers () < 0)
8298 error (_("This target does not support hardware ranged breakpoints."));
8299
8300 bp_count = hw_breakpoint_used_count ();
8301 bp_count += target_ranged_break_num_registers ();
8302 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8303 bp_count, 0);
8304 if (can_use_bp < 0)
8305 error (_("Hardware breakpoints used exceeds limit."));
8306
8307 if (arg == NULL || arg[0] == '\0')
8308 error(_("No address range specified."));
8309
8310 sals_start.sals = NULL;
8311 sals_start.nelts = 0;
8312 init_linespec_result (&canonical_start);
8313
8314 while (*arg == ' ' || *arg == '\t')
8315 arg++;
8316
58438ac1 8317 parse_breakpoint_sals (&arg, &sals_start, &canonical_start);
f1310107
TJB
8318
8319 sal_start = sals_start.sals[0];
8320 addr_string_start = canonical_start.canonical[0];
8321 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
8322 xfree (sals_start.sals);
8323 xfree (canonical_start.canonical);
8324
8325 if (arg[0] != ',')
8326 error (_("Too few arguments."));
8327 else if (sals_start.nelts == 0)
8328 error (_("Could not find location of the beginning of the range."));
8329 else if (sals_start.nelts != 1)
8330 error (_("Cannot create a ranged breakpoint with multiple locations."));
8331
8332 resolve_sal_pc (&sal_start);
8333
8334 arg++; /* Skip the comma. */
8335 while (*arg == ' ' || *arg == '\t')
8336 arg++;
8337
8338 /* Parse the end location. */
8339
8340 sals_end.sals = NULL;
8341 sals_end.nelts = 0;
8342 init_linespec_result (&canonical_end);
8343 arg_start = arg;
8344
423f41a5 8345 /* We call decode_line_1 directly here instead of using
f1310107
TJB
8346 parse_breakpoint_sals because we need to specify the start location's
8347 symtab and line as the default symtab and line for the end of the
8348 range. This makes it possible to have ranges like "foo.c:27, +14",
8349 where +14 means 14 lines from the start location. */
8350 sals_end = decode_line_1 (&arg, 1, sal_start.symtab, sal_start.line,
58438ac1 8351 &canonical_end);
f1310107
TJB
8352
8353 /* canonical_end can be NULL if it was of the form "*0xdeadbeef". */
8354 if (canonical_end.canonical == NULL)
38a714bb 8355 canonical_end.canonical = xcalloc (1, sizeof (char *));
f1310107
TJB
8356 /* Add the string if not present. */
8357 if (arg_start != arg && canonical_end.canonical[0] == NULL)
8358 canonical_end.canonical[0] = savestring (arg_start, arg - arg_start);
8359
8360 sal_end = sals_end.sals[0];
8361 addr_string_end = canonical_end.canonical[0];
8362 make_cleanup (xfree, addr_string_end);
8363 xfree (sals_end.sals);
8364 xfree (canonical_end.canonical);
8365
8366 if (sals_end.nelts == 0)
8367 error (_("Could not find location of the end of the range."));
8368 else if (sals_end.nelts != 1)
8369 error (_("Cannot create a ranged breakpoint with multiple locations."));
8370
8371 resolve_sal_pc (&sal_end);
8372
8373 end = find_breakpoint_range_end (sal_end);
8374 if (sal_start.pc > end)
177b42fe 8375 error (_("Invalid address range, end precedes start."));
f1310107
TJB
8376
8377 length = end - sal_start.pc + 1;
8378 if (length < 0)
8379 /* Length overflowed. */
8380 error (_("Address range too large."));
8381 else if (length == 1)
8382 {
8383 /* This range is simple enough to be handled by
8384 the `hbreak' command. */
8385 hbreak_command (addr_string_start, 1);
8386
8387 do_cleanups (cleanup_bkpt);
8388
8389 return;
8390 }
8391
8392 /* Now set up the breakpoint. */
8393 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 8394 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
8395 set_breakpoint_count (breakpoint_count + 1);
8396 b->number = breakpoint_count;
8397 b->disposition = disp_donttouch;
8398 b->addr_string = addr_string_start;
8399 b->addr_string_range_end = addr_string_end;
f1310107
TJB
8400 b->loc->length = length;
8401
8402 discard_cleanups (cleanup_bkpt);
8403
8404 mention (b);
8d3788bd 8405 observer_notify_breakpoint_created (b);
f1310107
TJB
8406 update_global_location_list (1);
8407}
8408
4a64f543
MS
8409/* Return non-zero if EXP is verified as constant. Returned zero
8410 means EXP is variable. Also the constant detection may fail for
8411 some constant expressions and in such case still falsely return
8412 zero. */
65d79d4b
SDJ
8413static int
8414watchpoint_exp_is_const (const struct expression *exp)
8415{
8416 int i = exp->nelts;
8417
8418 while (i > 0)
8419 {
8420 int oplenp, argsp;
8421
8422 /* We are only interested in the descriptor of each element. */
8423 operator_length (exp, i, &oplenp, &argsp);
8424 i -= oplenp;
8425
8426 switch (exp->elts[i].opcode)
8427 {
8428 case BINOP_ADD:
8429 case BINOP_SUB:
8430 case BINOP_MUL:
8431 case BINOP_DIV:
8432 case BINOP_REM:
8433 case BINOP_MOD:
8434 case BINOP_LSH:
8435 case BINOP_RSH:
8436 case BINOP_LOGICAL_AND:
8437 case BINOP_LOGICAL_OR:
8438 case BINOP_BITWISE_AND:
8439 case BINOP_BITWISE_IOR:
8440 case BINOP_BITWISE_XOR:
8441 case BINOP_EQUAL:
8442 case BINOP_NOTEQUAL:
8443 case BINOP_LESS:
8444 case BINOP_GTR:
8445 case BINOP_LEQ:
8446 case BINOP_GEQ:
8447 case BINOP_REPEAT:
8448 case BINOP_COMMA:
8449 case BINOP_EXP:
8450 case BINOP_MIN:
8451 case BINOP_MAX:
8452 case BINOP_INTDIV:
8453 case BINOP_CONCAT:
8454 case BINOP_IN:
8455 case BINOP_RANGE:
8456 case TERNOP_COND:
8457 case TERNOP_SLICE:
8458 case TERNOP_SLICE_COUNT:
8459
8460 case OP_LONG:
8461 case OP_DOUBLE:
8462 case OP_DECFLOAT:
8463 case OP_LAST:
8464 case OP_COMPLEX:
8465 case OP_STRING:
8466 case OP_BITSTRING:
8467 case OP_ARRAY:
8468 case OP_TYPE:
8469 case OP_NAME:
8470 case OP_OBJC_NSSTRING:
8471
8472 case UNOP_NEG:
8473 case UNOP_LOGICAL_NOT:
8474 case UNOP_COMPLEMENT:
8475 case UNOP_ADDR:
8476 case UNOP_HIGH:
4a64f543
MS
8477 /* Unary, binary and ternary operators: We have to check
8478 their operands. If they are constant, then so is the
8479 result of that operation. For instance, if A and B are
8480 determined to be constants, then so is "A + B".
8481
8482 UNOP_IND is one exception to the rule above, because the
8483 value of *ADDR is not necessarily a constant, even when
8484 ADDR is. */
65d79d4b
SDJ
8485 break;
8486
8487 case OP_VAR_VALUE:
8488 /* Check whether the associated symbol is a constant.
4a64f543 8489
65d79d4b 8490 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8491 possible that a buggy compiler could mark a variable as
8492 constant even when it is not, and TYPE_CONST would return
8493 true in this case, while SYMBOL_CLASS wouldn't.
8494
8495 We also have to check for function symbols because they
8496 are always constant. */
65d79d4b
SDJ
8497 {
8498 struct symbol *s = exp->elts[i + 2].symbol;
8499
8500 if (SYMBOL_CLASS (s) != LOC_BLOCK
8501 && SYMBOL_CLASS (s) != LOC_CONST
8502 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8503 return 0;
8504 break;
8505 }
8506
8507 /* The default action is to return 0 because we are using
8508 the optimistic approach here: If we don't know something,
8509 then it is not a constant. */
8510 default:
8511 return 0;
8512 }
8513 }
8514
8515 return 1;
8516}
8517
3a5c3e22
PA
8518/* Implement the "dtor" breakpoint_ops method for watchpoints. */
8519
8520static void
8521dtor_watchpoint (struct breakpoint *self)
8522{
8523 struct watchpoint *w = (struct watchpoint *) self;
8524
8525 xfree (w->cond_exp);
8526 xfree (w->exp);
8527 xfree (w->exp_string);
8528 xfree (w->exp_string_reparse);
8529 value_free (w->val);
8530
8531 base_breakpoint_ops.dtor (self);
8532}
8533
348d480f
PA
8534/* Implement the "re_set" breakpoint_ops method for watchpoints. */
8535
8536static void
8537re_set_watchpoint (struct breakpoint *b)
8538{
3a5c3e22
PA
8539 struct watchpoint *w = (struct watchpoint *) b;
8540
348d480f
PA
8541 /* Watchpoint can be either on expression using entirely global
8542 variables, or it can be on local variables.
8543
8544 Watchpoints of the first kind are never auto-deleted, and even
8545 persist across program restarts. Since they can use variables
8546 from shared libraries, we need to reparse expression as libraries
8547 are loaded and unloaded.
8548
8549 Watchpoints on local variables can also change meaning as result
8550 of solib event. For example, if a watchpoint uses both a local
8551 and a global variables in expression, it's a local watchpoint,
8552 but unloading of a shared library will make the expression
8553 invalid. This is not a very common use case, but we still
8554 re-evaluate expression, to avoid surprises to the user.
8555
8556 Note that for local watchpoints, we re-evaluate it only if
8557 watchpoints frame id is still valid. If it's not, it means the
8558 watchpoint is out of scope and will be deleted soon. In fact,
8559 I'm not sure we'll ever be called in this case.
8560
8561 If a local watchpoint's frame id is still valid, then
3a5c3e22 8562 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 8563
3a5c3e22
PA
8564 Don't do anything about disabled watchpoints, since they will be
8565 reevaluated again when enabled. */
8566 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
8567}
8568
77b06cd7
TJB
8569/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8570
8571static int
8572insert_watchpoint (struct bp_location *bl)
8573{
3a5c3e22
PA
8574 struct watchpoint *w = (struct watchpoint *) bl->owner;
8575 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8576
8577 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8578 w->cond_exp);
77b06cd7
TJB
8579}
8580
8581/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8582
8583static int
8584remove_watchpoint (struct bp_location *bl)
8585{
3a5c3e22
PA
8586 struct watchpoint *w = (struct watchpoint *) bl->owner;
8587 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8588
8589 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8590 w->cond_exp);
e09342b5
TJB
8591}
8592
e09342b5 8593static int
348d480f
PA
8594breakpoint_hit_watchpoint (const struct bp_location *bl,
8595 struct address_space *aspace, CORE_ADDR bp_addr)
e09342b5 8596{
348d480f 8597 struct breakpoint *b = bl->owner;
3a5c3e22 8598 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 8599
348d480f
PA
8600 /* Continuable hardware watchpoints are treated as non-existent if the
8601 reason we stopped wasn't a hardware watchpoint (we didn't stop on
8602 some data address). Otherwise gdb won't stop on a break instruction
8603 in the code (not from a breakpoint) when a hardware watchpoint has
8604 been defined. Also skip watchpoints which we know did not trigger
8605 (did not match the data address). */
8606 if (is_hardware_watchpoint (b)
3a5c3e22 8607 && w->watchpoint_triggered == watch_triggered_no)
348d480f 8608 return 0;
9c06b0b4 8609
348d480f 8610 return 1;
9c06b0b4
TJB
8611}
8612
348d480f
PA
8613static void
8614check_status_watchpoint (bpstat bs)
9c06b0b4 8615{
348d480f 8616 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 8617
348d480f 8618 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
8619}
8620
8621/* Implement the "resources_needed" breakpoint_ops method for
348d480f 8622 hardware watchpoints. */
9c06b0b4
TJB
8623
8624static int
348d480f 8625resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 8626{
3a5c3e22
PA
8627 struct watchpoint *w = (struct watchpoint *) bl->owner;
8628 int length = w->exact? 1 : bl->length;
348d480f
PA
8629
8630 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
8631}
8632
8633/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 8634 hardware watchpoints. */
9c06b0b4
TJB
8635
8636static int
348d480f 8637works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 8638{
348d480f 8639 return b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
8640}
8641
9c06b0b4 8642static enum print_stop_action
348d480f 8643print_it_watchpoint (bpstat bs)
9c06b0b4 8644{
348d480f
PA
8645 struct cleanup *old_chain;
8646 struct breakpoint *b;
8647 const struct bp_location *bl;
8648 struct ui_stream *stb;
8649 enum print_stop_action result;
3a5c3e22 8650 struct watchpoint *w;
348d480f
PA
8651
8652 gdb_assert (bs->bp_location_at != NULL);
8653
8654 bl = bs->bp_location_at;
8655 b = bs->breakpoint_at;
3a5c3e22 8656 w = (struct watchpoint *) b;
348d480f
PA
8657
8658 stb = ui_out_stream_new (uiout);
8659 old_chain = make_cleanup_ui_out_stream_delete (stb);
9c06b0b4
TJB
8660
8661 switch (b->type)
8662 {
348d480f 8663 case bp_watchpoint:
9c06b0b4
TJB
8664 case bp_hardware_watchpoint:
8665 annotate_watchpoint (b->number);
8666 if (ui_out_is_mi_like_p (uiout))
8667 ui_out_field_string
8668 (uiout, "reason",
8669 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
8670 mention (b);
8671 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8672 ui_out_text (uiout, "\nOld value = ");
8673 watchpoint_value_print (bs->old_val, stb->stream);
8674 ui_out_field_stream (uiout, "old", stb);
8675 ui_out_text (uiout, "\nNew value = ");
3a5c3e22 8676 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8677 ui_out_field_stream (uiout, "new", stb);
8678 ui_out_text (uiout, "\n");
8679 /* More than one watchpoint may have been triggered. */
8680 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8681 break;
8682
8683 case bp_read_watchpoint:
8684 if (ui_out_is_mi_like_p (uiout))
8685 ui_out_field_string
8686 (uiout, "reason",
8687 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
8688 mention (b);
8689 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8690 ui_out_text (uiout, "\nValue = ");
3a5c3e22 8691 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8692 ui_out_field_stream (uiout, "value", stb);
8693 ui_out_text (uiout, "\n");
8694 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8695 break;
8696
8697 case bp_access_watchpoint:
348d480f
PA
8698 if (bs->old_val != NULL)
8699 {
8700 annotate_watchpoint (b->number);
8701 if (ui_out_is_mi_like_p (uiout))
8702 ui_out_field_string
8703 (uiout, "reason",
8704 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8705 mention (b);
8706 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8707 ui_out_text (uiout, "\nOld value = ");
8708 watchpoint_value_print (bs->old_val, stb->stream);
8709 ui_out_field_stream (uiout, "old", stb);
8710 ui_out_text (uiout, "\nNew value = ");
8711 }
8712 else
8713 {
8714 mention (b);
8715 if (ui_out_is_mi_like_p (uiout))
8716 ui_out_field_string
8717 (uiout, "reason",
8718 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8719 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8720 ui_out_text (uiout, "\nValue = ");
8721 }
3a5c3e22 8722 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8723 ui_out_field_stream (uiout, "new", stb);
8724 ui_out_text (uiout, "\n");
8725 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8726 break;
8727 default:
348d480f 8728 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8729 }
8730
348d480f
PA
8731 do_cleanups (old_chain);
8732 return result;
8733}
8734
8735/* Implement the "print_mention" breakpoint_ops method for hardware
8736 watchpoints. */
8737
8738static void
8739print_mention_watchpoint (struct breakpoint *b)
8740{
8741 struct cleanup *ui_out_chain;
3a5c3e22 8742 struct watchpoint *w = (struct watchpoint *) b;
348d480f
PA
8743
8744 switch (b->type)
8745 {
8746 case bp_watchpoint:
8747 ui_out_text (uiout, "Watchpoint ");
8748 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8749 break;
8750 case bp_hardware_watchpoint:
8751 ui_out_text (uiout, "Hardware watchpoint ");
8752 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8753 break;
8754 case bp_read_watchpoint:
8755 ui_out_text (uiout, "Hardware read watchpoint ");
8756 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8757 break;
8758 case bp_access_watchpoint:
8759 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
8760 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8761 break;
8762 default:
8763 internal_error (__FILE__, __LINE__,
8764 _("Invalid hardware watchpoint type."));
8765 }
8766
8767 ui_out_field_int (uiout, "number", b->number);
8768 ui_out_text (uiout, ": ");
3a5c3e22 8769 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
8770 do_cleanups (ui_out_chain);
8771}
8772
8773/* Implement the "print_recreate" breakpoint_ops method for
8774 watchpoints. */
8775
8776static void
8777print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
8778{
3a5c3e22
PA
8779 struct watchpoint *w = (struct watchpoint *) b;
8780
348d480f
PA
8781 switch (b->type)
8782 {
8783 case bp_watchpoint:
8784 case bp_hardware_watchpoint:
8785 fprintf_unfiltered (fp, "watch");
8786 break;
8787 case bp_read_watchpoint:
8788 fprintf_unfiltered (fp, "rwatch");
8789 break;
8790 case bp_access_watchpoint:
8791 fprintf_unfiltered (fp, "awatch");
8792 break;
8793 default:
8794 internal_error (__FILE__, __LINE__,
8795 _("Invalid watchpoint type."));
8796 }
8797
3a5c3e22 8798 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 8799 print_recreate_thread (b, fp);
348d480f
PA
8800}
8801
8802/* The breakpoint_ops structure to be used in hardware watchpoints. */
8803
2060206e 8804static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
8805
8806/* Implement the "insert" breakpoint_ops method for
8807 masked hardware watchpoints. */
8808
8809static int
8810insert_masked_watchpoint (struct bp_location *bl)
8811{
3a5c3e22
PA
8812 struct watchpoint *w = (struct watchpoint *) bl->owner;
8813
8814 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8815 bl->watchpoint_type);
8816}
8817
8818/* Implement the "remove" breakpoint_ops method for
8819 masked hardware watchpoints. */
8820
8821static int
8822remove_masked_watchpoint (struct bp_location *bl)
8823{
3a5c3e22
PA
8824 struct watchpoint *w = (struct watchpoint *) bl->owner;
8825
8826 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8827 bl->watchpoint_type);
8828}
8829
8830/* Implement the "resources_needed" breakpoint_ops method for
8831 masked hardware watchpoints. */
8832
8833static int
8834resources_needed_masked_watchpoint (const struct bp_location *bl)
8835{
3a5c3e22
PA
8836 struct watchpoint *w = (struct watchpoint *) bl->owner;
8837
8838 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
8839}
8840
8841/* Implement the "works_in_software_mode" breakpoint_ops method for
8842 masked hardware watchpoints. */
8843
8844static int
8845works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
8846{
8847 return 0;
8848}
8849
8850/* Implement the "print_it" breakpoint_ops method for
8851 masked hardware watchpoints. */
8852
8853static enum print_stop_action
8854print_it_masked_watchpoint (bpstat bs)
8855{
8856 struct breakpoint *b = bs->breakpoint_at;
8857
8858 /* Masked watchpoints have only one location. */
8859 gdb_assert (b->loc && b->loc->next == NULL);
8860
8861 switch (b->type)
8862 {
8863 case bp_hardware_watchpoint:
8864 annotate_watchpoint (b->number);
8865 if (ui_out_is_mi_like_p (uiout))
8866 ui_out_field_string
8867 (uiout, "reason",
8868 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
8869 break;
8870
8871 case bp_read_watchpoint:
8872 if (ui_out_is_mi_like_p (uiout))
8873 ui_out_field_string
8874 (uiout, "reason",
8875 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
8876 break;
8877
8878 case bp_access_watchpoint:
8879 if (ui_out_is_mi_like_p (uiout))
8880 ui_out_field_string
8881 (uiout, "reason",
8882 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8883 break;
8884 default:
8885 internal_error (__FILE__, __LINE__,
8886 _("Invalid hardware watchpoint type."));
8887 }
8888
8889 mention (b);
9c06b0b4
TJB
8890 ui_out_text (uiout, _("\n\
8891Check the underlying instruction at PC for the memory\n\
8892address and value which triggered this watchpoint.\n"));
8893 ui_out_text (uiout, "\n");
8894
8895 /* More than one watchpoint may have been triggered. */
8896 return PRINT_UNKNOWN;
8897}
8898
8899/* Implement the "print_one_detail" breakpoint_ops method for
8900 masked hardware watchpoints. */
8901
8902static void
8903print_one_detail_masked_watchpoint (const struct breakpoint *b,
8904 struct ui_out *uiout)
8905{
3a5c3e22
PA
8906 struct watchpoint *w = (struct watchpoint *) b;
8907
9c06b0b4
TJB
8908 /* Masked watchpoints have only one location. */
8909 gdb_assert (b->loc && b->loc->next == NULL);
8910
8911 ui_out_text (uiout, "\tmask ");
3a5c3e22 8912 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
8913 ui_out_text (uiout, "\n");
8914}
8915
8916/* Implement the "print_mention" breakpoint_ops method for
8917 masked hardware watchpoints. */
8918
8919static void
8920print_mention_masked_watchpoint (struct breakpoint *b)
8921{
3a5c3e22 8922 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
8923 struct cleanup *ui_out_chain;
8924
8925 switch (b->type)
8926 {
8927 case bp_hardware_watchpoint:
8928 ui_out_text (uiout, "Masked hardware watchpoint ");
8929 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8930 break;
8931 case bp_read_watchpoint:
8932 ui_out_text (uiout, "Masked hardware read watchpoint ");
8933 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8934 break;
8935 case bp_access_watchpoint:
8936 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
8937 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8938 break;
8939 default:
8940 internal_error (__FILE__, __LINE__,
8941 _("Invalid hardware watchpoint type."));
8942 }
8943
8944 ui_out_field_int (uiout, "number", b->number);
8945 ui_out_text (uiout, ": ");
3a5c3e22 8946 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
8947 do_cleanups (ui_out_chain);
8948}
8949
8950/* Implement the "print_recreate" breakpoint_ops method for
8951 masked hardware watchpoints. */
8952
8953static void
8954print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
8955{
3a5c3e22 8956 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
8957 char tmp[40];
8958
8959 switch (b->type)
8960 {
8961 case bp_hardware_watchpoint:
8962 fprintf_unfiltered (fp, "watch");
8963 break;
8964 case bp_read_watchpoint:
8965 fprintf_unfiltered (fp, "rwatch");
8966 break;
8967 case bp_access_watchpoint:
8968 fprintf_unfiltered (fp, "awatch");
8969 break;
8970 default:
8971 internal_error (__FILE__, __LINE__,
8972 _("Invalid hardware watchpoint type."));
8973 }
8974
3a5c3e22
PA
8975 sprintf_vma (tmp, w->hw_wp_mask);
8976 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 8977 print_recreate_thread (b, fp);
9c06b0b4
TJB
8978}
8979
8980/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
8981
2060206e 8982static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
8983
8984/* Tell whether the given watchpoint is a masked hardware watchpoint. */
8985
8986static int
8987is_masked_watchpoint (const struct breakpoint *b)
8988{
8989 return b->ops == &masked_watchpoint_breakpoint_ops;
8990}
8991
53a5351d
JM
8992/* accessflag: hw_write: watch write,
8993 hw_read: watch read,
8994 hw_access: watch access (read or write) */
c906108c 8995static void
84f4c1fe
PM
8996watch_command_1 (char *arg, int accessflag, int from_tty,
8997 int just_location, int internal)
c906108c 8998{
a9634178 8999 volatile struct gdb_exception e;
d983da9c 9000 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 9001 struct expression *exp;
60e1c644 9002 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 9003 struct value *val, *mark, *result;
c906108c 9004 struct frame_info *frame;
c906108c
SS
9005 char *exp_start = NULL;
9006 char *exp_end = NULL;
9c06b0b4
TJB
9007 char *tok, *end_tok;
9008 int toklen = -1;
c906108c
SS
9009 char *cond_start = NULL;
9010 char *cond_end = NULL;
c906108c 9011 enum bptype bp_type;
37e4754d 9012 int thread = -1;
0cf6dd15 9013 int pc = 0;
9c06b0b4
TJB
9014 /* Flag to indicate whether we are going to use masks for
9015 the hardware watchpoint. */
9016 int use_mask = 0;
9017 CORE_ADDR mask = 0;
3a5c3e22 9018 struct watchpoint *w;
c906108c 9019
37e4754d
LM
9020 /* Make sure that we actually have parameters to parse. */
9021 if (arg != NULL && arg[0] != '\0')
9022 {
9c06b0b4 9023 char *value_start;
37e4754d 9024
9c06b0b4
TJB
9025 /* Look for "parameter value" pairs at the end
9026 of the arguments string. */
9027 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
9028 {
9029 /* Skip whitespace at the end of the argument list. */
9030 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9031 tok--;
9032
9033 /* Find the beginning of the last token.
9034 This is the value of the parameter. */
9035 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9036 tok--;
9037 value_start = tok + 1;
9038
9039 /* Skip whitespace. */
9040 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9041 tok--;
9042
9043 end_tok = tok;
9044
9045 /* Find the beginning of the second to last token.
9046 This is the parameter itself. */
9047 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9048 tok--;
9049 tok++;
9050 toklen = end_tok - tok + 1;
9051
9052 if (toklen == 6 && !strncmp (tok, "thread", 6))
9053 {
9054 /* At this point we've found a "thread" token, which means
9055 the user is trying to set a watchpoint that triggers
9056 only in a specific thread. */
9057 char *endp;
37e4754d 9058
9c06b0b4
TJB
9059 if (thread != -1)
9060 error(_("You can specify only one thread."));
37e4754d 9061
9c06b0b4
TJB
9062 /* Extract the thread ID from the next token. */
9063 thread = strtol (value_start, &endp, 0);
37e4754d 9064
9c06b0b4
TJB
9065 /* Check if the user provided a valid numeric value for the
9066 thread ID. */
9067 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9068 error (_("Invalid thread ID specification %s."), value_start);
9069
9070 /* Check if the thread actually exists. */
9071 if (!valid_thread_id (thread))
9072 error (_("Unknown thread %d."), thread);
9073 }
9074 else if (toklen == 4 && !strncmp (tok, "mask", 4))
9075 {
9076 /* We've found a "mask" token, which means the user wants to
9077 create a hardware watchpoint that is going to have the mask
9078 facility. */
9079 struct value *mask_value, *mark;
37e4754d 9080
9c06b0b4
TJB
9081 if (use_mask)
9082 error(_("You can specify only one mask."));
37e4754d 9083
9c06b0b4 9084 use_mask = just_location = 1;
37e4754d 9085
9c06b0b4
TJB
9086 mark = value_mark ();
9087 mask_value = parse_to_comma_and_eval (&value_start);
9088 mask = value_as_address (mask_value);
9089 value_free_to_mark (mark);
9090 }
9091 else
9092 /* We didn't recognize what we found. We should stop here. */
9093 break;
37e4754d 9094
9c06b0b4
TJB
9095 /* Truncate the string and get rid of the "parameter value" pair before
9096 the arguments string is parsed by the parse_exp_1 function. */
9097 *tok = '\0';
9098 }
37e4754d
LM
9099 }
9100
9101 /* Parse the rest of the arguments. */
c906108c
SS
9102 innermost_block = NULL;
9103 exp_start = arg;
9104 exp = parse_exp_1 (&arg, 0, 0);
9105 exp_end = arg;
fa8a61dc
TT
9106 /* Remove trailing whitespace from the expression before saving it.
9107 This makes the eventual display of the expression string a bit
9108 prettier. */
9109 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9110 --exp_end;
9111
65d79d4b
SDJ
9112 /* Checking if the expression is not constant. */
9113 if (watchpoint_exp_is_const (exp))
9114 {
9115 int len;
9116
9117 len = exp_end - exp_start;
9118 while (len > 0 && isspace (exp_start[len - 1]))
9119 len--;
9120 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9121 }
9122
c906108c
SS
9123 exp_valid_block = innermost_block;
9124 mark = value_mark ();
a1442452 9125 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
9126
9127 if (just_location)
9128 {
9c06b0b4
TJB
9129 int ret;
9130
06a64a0b 9131 exp_valid_block = NULL;
a1442452 9132 val = value_addr (result);
06a64a0b
TT
9133 release_value (val);
9134 value_free_to_mark (mark);
9c06b0b4
TJB
9135
9136 if (use_mask)
9137 {
9138 ret = target_masked_watch_num_registers (value_as_address (val),
9139 mask);
9140 if (ret == -1)
9141 error (_("This target does not support masked watchpoints."));
9142 else if (ret == -2)
9143 error (_("Invalid mask or memory region."));
9144 }
06a64a0b
TT
9145 }
9146 else if (val != NULL)
fa4727a6 9147 release_value (val);
c906108c 9148
e9cafbcc
TT
9149 tok = skip_spaces (arg);
9150 end_tok = skip_to_space (tok);
c906108c
SS
9151
9152 toklen = end_tok - tok;
9153 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9154 {
2d134ed3
PA
9155 struct expression *cond;
9156
60e1c644 9157 innermost_block = NULL;
c906108c
SS
9158 tok = cond_start = end_tok + 1;
9159 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
9160
9161 /* The watchpoint expression may not be local, but the condition
9162 may still be. E.g.: `watch global if local > 0'. */
9163 cond_exp_valid_block = innermost_block;
9164
2d134ed3 9165 xfree (cond);
c906108c
SS
9166 cond_end = tok;
9167 }
9168 if (*tok)
8a3fe4f8 9169 error (_("Junk at end of command."));
c906108c 9170
53a5351d 9171 if (accessflag == hw_read)
c5aa993b 9172 bp_type = bp_read_watchpoint;
53a5351d 9173 else if (accessflag == hw_access)
c5aa993b
JM
9174 bp_type = bp_access_watchpoint;
9175 else
9176 bp_type = bp_hardware_watchpoint;
c906108c 9177
d983da9c 9178 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
9179
9180 /* If the expression is "local", then set up a "watchpoint scope"
9181 breakpoint at the point where we've left the scope of the watchpoint
9182 expression. Create the scope breakpoint before the watchpoint, so
9183 that we will encounter it first in bpstat_stop_status. */
60e1c644 9184 if (exp_valid_block && frame)
d983da9c 9185 {
edb3359d
DJ
9186 if (frame_id_p (frame_unwind_caller_id (frame)))
9187 {
9188 scope_breakpoint
a6d9a66e
UW
9189 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
9190 frame_unwind_caller_pc (frame),
06edf0c0
PA
9191 bp_watchpoint_scope,
9192 &momentary_breakpoint_ops);
d983da9c 9193
edb3359d 9194 scope_breakpoint->enable_state = bp_enabled;
d983da9c 9195
edb3359d
DJ
9196 /* Automatically delete the breakpoint when it hits. */
9197 scope_breakpoint->disposition = disp_del;
d983da9c 9198
edb3359d
DJ
9199 /* Only break in the proper frame (help with recursion). */
9200 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 9201
edb3359d 9202 /* Set the address at which we will stop. */
a6d9a66e
UW
9203 scope_breakpoint->loc->gdbarch
9204 = frame_unwind_caller_arch (frame);
edb3359d
DJ
9205 scope_breakpoint->loc->requested_address
9206 = frame_unwind_caller_pc (frame);
9207 scope_breakpoint->loc->address
a6d9a66e
UW
9208 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9209 scope_breakpoint->loc->requested_address,
edb3359d
DJ
9210 scope_breakpoint->type);
9211 }
d983da9c
DJ
9212 }
9213
c906108c 9214 /* Now set up the breakpoint. */
3a5c3e22
PA
9215
9216 w = XCNEW (struct watchpoint);
9217 b = &w->base;
348d480f 9218 if (use_mask)
3a5c3e22
PA
9219 init_raw_breakpoint_without_location (b, NULL, bp_type,
9220 &masked_watchpoint_breakpoint_ops);
348d480f 9221 else
3a5c3e22
PA
9222 init_raw_breakpoint_without_location (b, NULL, bp_type,
9223 &watchpoint_breakpoint_ops);
37e4754d 9224 b->thread = thread;
b5de0fa7 9225 b->disposition = disp_donttouch;
348d480f 9226 b->pspace = current_program_space;
3a5c3e22
PA
9227 w->exp = exp;
9228 w->exp_valid_block = exp_valid_block;
9229 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
9230 if (just_location)
9231 {
9232 struct type *t = value_type (val);
9233 CORE_ADDR addr = value_as_address (val);
9234 char *name;
9235
9236 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9237 name = type_to_string (t);
9238
3a5c3e22 9239 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 9240 core_addr_to_string (addr));
06a64a0b
TT
9241 xfree (name);
9242
3a5c3e22 9243 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
9244 (int) (exp_end - exp_start), exp_start);
9245
06a64a0b
TT
9246 /* The above expression is in C. */
9247 b->language = language_c;
9248 }
9249 else
3a5c3e22 9250 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
9251
9252 if (use_mask)
9253 {
3a5c3e22 9254 w->hw_wp_mask = mask;
9c06b0b4
TJB
9255 }
9256 else
9257 {
3a5c3e22
PA
9258 w->val = val;
9259 w->val_valid = 1;
9c06b0b4 9260 }
77b06cd7 9261
c906108c
SS
9262 if (cond_start)
9263 b->cond_string = savestring (cond_start, cond_end - cond_start);
9264 else
9265 b->cond_string = 0;
c5aa993b 9266
c906108c 9267 if (frame)
f6bc2008 9268 {
3a5c3e22
PA
9269 w->watchpoint_frame = get_frame_id (frame);
9270 w->watchpoint_thread = inferior_ptid;
f6bc2008 9271 }
c906108c 9272 else
f6bc2008 9273 {
3a5c3e22
PA
9274 w->watchpoint_frame = null_frame_id;
9275 w->watchpoint_thread = null_ptid;
f6bc2008 9276 }
c906108c 9277
d983da9c 9278 if (scope_breakpoint != NULL)
c906108c 9279 {
d983da9c
DJ
9280 /* The scope breakpoint is related to the watchpoint. We will
9281 need to act on them together. */
9282 b->related_breakpoint = scope_breakpoint;
9283 scope_breakpoint->related_breakpoint = b;
c906108c 9284 }
d983da9c 9285
06a64a0b
TT
9286 if (!just_location)
9287 value_free_to_mark (mark);
2d134ed3 9288
a9634178
TJB
9289 TRY_CATCH (e, RETURN_MASK_ALL)
9290 {
9291 /* Finally update the new watchpoint. This creates the locations
9292 that should be inserted. */
3a5c3e22 9293 update_watchpoint (w, 1);
a9634178
TJB
9294 }
9295 if (e.reason < 0)
9296 {
9297 delete_breakpoint (b);
9298 throw_exception (e);
9299 }
9300
3a5c3e22 9301 install_breakpoint (internal, b);
c906108c
SS
9302}
9303
e09342b5 9304/* Return count of debug registers needed to watch the given expression.
e09342b5 9305 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9306
c906108c 9307static int
a9634178 9308can_use_hardware_watchpoint (struct value *v)
c906108c
SS
9309{
9310 int found_memory_cnt = 0;
2e70b7b9 9311 struct value *head = v;
c906108c
SS
9312
9313 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9314 if (!can_use_hw_watchpoints)
c906108c 9315 return 0;
c5aa993b 9316
5c44784c
JM
9317 /* Make sure that the value of the expression depends only upon
9318 memory contents, and values computed from them within GDB. If we
9319 find any register references or function calls, we can't use a
9320 hardware watchpoint.
9321
9322 The idea here is that evaluating an expression generates a series
9323 of values, one holding the value of every subexpression. (The
9324 expression a*b+c has five subexpressions: a, b, a*b, c, and
9325 a*b+c.) GDB's values hold almost enough information to establish
9326 the criteria given above --- they identify memory lvalues,
9327 register lvalues, computed values, etcetera. So we can evaluate
9328 the expression, and then scan the chain of values that leaves
9329 behind to decide whether we can detect any possible change to the
9330 expression's final value using only hardware watchpoints.
9331
9332 However, I don't think that the values returned by inferior
9333 function calls are special in any way. So this function may not
9334 notice that an expression involving an inferior function call
9335 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9336 for (; v; v = value_next (v))
c906108c 9337 {
5c44784c 9338 if (VALUE_LVAL (v) == lval_memory)
c906108c 9339 {
8464be76
DJ
9340 if (v != head && value_lazy (v))
9341 /* A lazy memory lvalue in the chain is one that GDB never
9342 needed to fetch; we either just used its address (e.g.,
9343 `a' in `a.b') or we never needed it at all (e.g., `a'
9344 in `a,b'). This doesn't apply to HEAD; if that is
9345 lazy then it was not readable, but watch it anyway. */
5c44784c 9346 ;
53a5351d 9347 else
5c44784c
JM
9348 {
9349 /* Ahh, memory we actually used! Check if we can cover
9350 it with hardware watchpoints. */
df407dfe 9351 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9352
9353 /* We only watch structs and arrays if user asked for it
9354 explicitly, never if they just happen to appear in a
9355 middle of some value chain. */
9356 if (v == head
9357 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9358 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9359 {
42ae5230 9360 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9361 int len;
9362 int num_regs;
9363
a9634178 9364 len = (target_exact_watchpoints
e09342b5
TJB
9365 && is_scalar_type_recursive (vtype))?
9366 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9367
e09342b5
TJB
9368 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9369 if (!num_regs)
2e70b7b9
MS
9370 return 0;
9371 else
e09342b5 9372 found_memory_cnt += num_regs;
2e70b7b9 9373 }
5c44784c 9374 }
c5aa993b 9375 }
5086187c
AC
9376 else if (VALUE_LVAL (v) != not_lval
9377 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9378 return 0; /* These are values from the history (e.g., $1). */
5086187c 9379 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9380 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9381 }
9382
9383 /* The expression itself looks suitable for using a hardware
9384 watchpoint, but give the target machine a chance to reject it. */
9385 return found_memory_cnt;
9386}
9387
8b93c638 9388void
84f4c1fe 9389watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9390{
84f4c1fe 9391 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9392}
9393
9394/* A helper function that looks for an argument at the start of a
9395 string. The argument must also either be at the end of the string,
9396 or be followed by whitespace. Returns 1 if it finds the argument,
9397 0 otherwise. If the argument is found, it updates *STR. */
9398
9399static int
9400check_for_argument (char **str, char *arg, int arg_len)
9401{
9402 if (strncmp (*str, arg, arg_len) == 0
9403 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9404 {
9405 *str += arg_len;
9406 return 1;
9407 }
9408 return 0;
9409}
9410
9411/* A helper function that looks for the "-location" argument and then
9412 calls watch_command_1. */
9413
9414static void
9415watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9416{
9417 int just_location = 0;
9418
9419 if (arg
9420 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9421 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9422 {
e9cafbcc 9423 arg = skip_spaces (arg);
06a64a0b
TT
9424 just_location = 1;
9425 }
9426
84f4c1fe 9427 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9428}
8926118c 9429
c5aa993b 9430static void
fba45db2 9431watch_command (char *arg, int from_tty)
c906108c 9432{
06a64a0b 9433 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9434}
9435
8b93c638 9436void
84f4c1fe 9437rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9438{
84f4c1fe 9439 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9440}
8926118c 9441
c5aa993b 9442static void
fba45db2 9443rwatch_command (char *arg, int from_tty)
c906108c 9444{
06a64a0b 9445 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9446}
9447
8b93c638 9448void
84f4c1fe 9449awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9450{
84f4c1fe 9451 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9452}
8926118c 9453
c5aa993b 9454static void
fba45db2 9455awatch_command (char *arg, int from_tty)
c906108c 9456{
06a64a0b 9457 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9458}
c906108c 9459\f
c5aa993b 9460
43ff13b4 9461/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9462 because it uses the mechanisms of breakpoints. */
9463
bfec99b2
PA
9464struct until_break_command_continuation_args
9465{
9466 struct breakpoint *breakpoint;
9467 struct breakpoint *breakpoint2;
186c406b 9468 int thread_num;
bfec99b2
PA
9469};
9470
43ff13b4 9471/* This function is called by fetch_inferior_event via the
4a64f543 9472 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9473 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9474 command. */
c2c6d25f 9475static void
fa4cd53f 9476until_break_command_continuation (void *arg, int err)
43ff13b4 9477{
bfec99b2
PA
9478 struct until_break_command_continuation_args *a = arg;
9479
9480 delete_breakpoint (a->breakpoint);
9481 if (a->breakpoint2)
9482 delete_breakpoint (a->breakpoint2);
186c406b 9483 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9484}
9485
c906108c 9486void
ae66c1fc 9487until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9488{
9489 struct symtabs_and_lines sals;
9490 struct symtab_and_line sal;
206415a3 9491 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9492 struct breakpoint *breakpoint;
f107f563 9493 struct breakpoint *breakpoint2 = NULL;
c906108c 9494 struct cleanup *old_chain;
186c406b
TT
9495 int thread;
9496 struct thread_info *tp;
c906108c
SS
9497
9498 clear_proceed_status ();
9499
9500 /* Set a breakpoint where the user wants it and at return from
4a64f543 9501 this function. */
c5aa993b 9502
c906108c
SS
9503 if (default_breakpoint_valid)
9504 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
58438ac1 9505 default_breakpoint_line, NULL);
c906108c 9506 else
58438ac1 9507 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, NULL);
c5aa993b 9508
c906108c 9509 if (sals.nelts != 1)
8a3fe4f8 9510 error (_("Couldn't get information on specified line."));
c5aa993b 9511
c906108c 9512 sal = sals.sals[0];
4a64f543 9513 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9514
c906108c 9515 if (*arg)
8a3fe4f8 9516 error (_("Junk at end of arguments."));
c5aa993b 9517
c906108c 9518 resolve_sal_pc (&sal);
c5aa993b 9519
ae66c1fc
EZ
9520 if (anywhere)
9521 /* If the user told us to continue until a specified location,
9522 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9523 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9524 null_frame_id, bp_until);
ae66c1fc 9525 else
4a64f543
MS
9526 /* Otherwise, specify the selected frame, because we want to stop
9527 only at the very same frame. */
a6d9a66e
UW
9528 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9529 get_stack_frame_id (frame),
ae66c1fc 9530 bp_until);
c5aa993b 9531
f107f563 9532 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9533
186c406b
TT
9534 tp = inferior_thread ();
9535 thread = tp->num;
9536
ae66c1fc
EZ
9537 /* Keep within the current frame, or in frames called by the current
9538 one. */
edb3359d
DJ
9539
9540 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9541 {
edb3359d
DJ
9542 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9543 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9544 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9545 sal,
edb3359d 9546 frame_unwind_caller_id (frame),
f107f563
VP
9547 bp_until);
9548 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9549
9550 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9551 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9552 }
c5aa993b 9553
c906108c 9554 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9555
4a64f543
MS
9556 /* If we are running asynchronously, and proceed call above has
9557 actually managed to start the target, arrange for breakpoints to
9558 be deleted when the target stops. Otherwise, we're already
9559 stopped and delete breakpoints via cleanup chain. */
f107f563 9560
8ea051c5 9561 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9562 {
bfec99b2
PA
9563 struct until_break_command_continuation_args *args;
9564 args = xmalloc (sizeof (*args));
f107f563 9565
bfec99b2
PA
9566 args->breakpoint = breakpoint;
9567 args->breakpoint2 = breakpoint2;
186c406b 9568 args->thread_num = thread;
f107f563
VP
9569
9570 discard_cleanups (old_chain);
95e54da7
PA
9571 add_continuation (inferior_thread (),
9572 until_break_command_continuation, args,
604ead4a 9573 xfree);
f107f563
VP
9574 }
9575 else
c5aa993b 9576 do_cleanups (old_chain);
c906108c 9577}
ae66c1fc 9578
c906108c
SS
9579/* This function attempts to parse an optional "if <cond>" clause
9580 from the arg string. If one is not found, it returns NULL.
c5aa993b 9581
c906108c
SS
9582 Else, it returns a pointer to the condition string. (It does not
9583 attempt to evaluate the string against a particular block.) And,
9584 it updates arg to point to the first character following the parsed
4a64f543 9585 if clause in the arg string. */
53a5351d 9586
c906108c 9587static char *
fba45db2 9588ep_parse_optional_if_clause (char **arg)
c906108c 9589{
c5aa993b
JM
9590 char *cond_string;
9591
9592 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9593 return NULL;
c5aa993b 9594
4a64f543 9595 /* Skip the "if" keyword. */
c906108c 9596 (*arg) += 2;
c5aa993b 9597
c906108c 9598 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9599 condition string. */
e9cafbcc 9600 *arg = skip_spaces (*arg);
c906108c 9601 cond_string = *arg;
c5aa993b 9602
4a64f543
MS
9603 /* Assume that the condition occupies the remainder of the arg
9604 string. */
c906108c 9605 (*arg) += strlen (cond_string);
c5aa993b 9606
c906108c
SS
9607 return cond_string;
9608}
c5aa993b 9609
c906108c
SS
9610/* Commands to deal with catching events, such as signals, exceptions,
9611 process start/exit, etc. */
c5aa993b
JM
9612
9613typedef enum
9614{
44feb3ce
TT
9615 catch_fork_temporary, catch_vfork_temporary,
9616 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9617}
9618catch_fork_kind;
9619
c906108c 9620static void
cc59ec59
MS
9621catch_fork_command_1 (char *arg, int from_tty,
9622 struct cmd_list_element *command)
c906108c 9623{
a6d9a66e 9624 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9625 char *cond_string = NULL;
44feb3ce
TT
9626 catch_fork_kind fork_kind;
9627 int tempflag;
9628
9629 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9630 tempflag = (fork_kind == catch_fork_temporary
9631 || fork_kind == catch_vfork_temporary);
c5aa993b 9632
44feb3ce
TT
9633 if (!arg)
9634 arg = "";
e9cafbcc 9635 arg = skip_spaces (arg);
c5aa993b 9636
c906108c 9637 /* The allowed syntax is:
c5aa993b
JM
9638 catch [v]fork
9639 catch [v]fork if <cond>
9640
4a64f543 9641 First, check if there's an if clause. */
c906108c 9642 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9643
c906108c 9644 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9645 error (_("Junk at end of arguments."));
c5aa993b 9646
c906108c 9647 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9648 and enable reporting of such events. */
c5aa993b
JM
9649 switch (fork_kind)
9650 {
44feb3ce
TT
9651 case catch_fork_temporary:
9652 case catch_fork_permanent:
a6d9a66e 9653 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9654 &catch_fork_breakpoint_ops);
c906108c 9655 break;
44feb3ce
TT
9656 case catch_vfork_temporary:
9657 case catch_vfork_permanent:
a6d9a66e 9658 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9659 &catch_vfork_breakpoint_ops);
c906108c 9660 break;
c5aa993b 9661 default:
8a3fe4f8 9662 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9663 break;
c5aa993b 9664 }
c906108c
SS
9665}
9666
9667static void
cc59ec59
MS
9668catch_exec_command_1 (char *arg, int from_tty,
9669 struct cmd_list_element *command)
c906108c 9670{
b4d90040 9671 struct exec_catchpoint *c;
a6d9a66e 9672 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9673 int tempflag;
c5aa993b 9674 char *cond_string = NULL;
c906108c 9675
44feb3ce
TT
9676 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9677
9678 if (!arg)
9679 arg = "";
e9cafbcc 9680 arg = skip_spaces (arg);
c906108c
SS
9681
9682 /* The allowed syntax is:
c5aa993b
JM
9683 catch exec
9684 catch exec if <cond>
c906108c 9685
4a64f543 9686 First, check if there's an if clause. */
c906108c
SS
9687 cond_string = ep_parse_optional_if_clause (&arg);
9688
9689 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9690 error (_("Junk at end of arguments."));
c906108c 9691
b4d90040
PA
9692 c = XNEW (struct exec_catchpoint);
9693 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
9694 &catch_exec_breakpoint_ops);
9695 c->exec_pathname = NULL;
9696
3a5c3e22 9697 install_breakpoint (0, &c->base);
c906108c 9698}
c5aa993b 9699
3086aeae 9700static enum print_stop_action
348d480f 9701print_it_exception_catchpoint (bpstat bs)
3086aeae 9702{
348d480f 9703 struct breakpoint *b = bs->breakpoint_at;
ade92717 9704 int bp_temp, bp_throw;
3086aeae 9705
ade92717 9706 annotate_catchpoint (b->number);
3086aeae 9707
ade92717
AR
9708 bp_throw = strstr (b->addr_string, "throw") != NULL;
9709 if (b->loc->address != b->loc->requested_address)
9710 breakpoint_adjustment_warning (b->loc->requested_address,
9711 b->loc->address,
9712 b->number, 1);
df2b6d2d 9713 bp_temp = b->disposition == disp_del;
ade92717
AR
9714 ui_out_text (uiout,
9715 bp_temp ? "Temporary catchpoint "
9716 : "Catchpoint ");
9717 if (!ui_out_is_mi_like_p (uiout))
9718 ui_out_field_int (uiout, "bkptno", b->number);
9719 ui_out_text (uiout,
c0b37c48
AR
9720 bp_throw ? " (exception thrown), "
9721 : " (exception caught), ");
ade92717
AR
9722 if (ui_out_is_mi_like_p (uiout))
9723 {
9724 ui_out_field_string (uiout, "reason",
9725 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9726 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9727 ui_out_field_int (uiout, "bkptno", b->number);
9728 }
3086aeae
DJ
9729 return PRINT_SRC_AND_LOC;
9730}
9731
9732static void
cc59ec59
MS
9733print_one_exception_catchpoint (struct breakpoint *b,
9734 struct bp_location **last_loc)
3086aeae 9735{
79a45b7d 9736 struct value_print_options opts;
cc59ec59 9737
79a45b7d
TT
9738 get_user_print_options (&opts);
9739 if (opts.addressprint)
3086aeae
DJ
9740 {
9741 annotate_field (4);
604133b5
AR
9742 if (b->loc == NULL || b->loc->shlib_disabled)
9743 ui_out_field_string (uiout, "addr", "<PENDING>");
9744 else
5af949e3
UW
9745 ui_out_field_core_addr (uiout, "addr",
9746 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9747 }
9748 annotate_field (5);
604133b5 9749 if (b->loc)
a6d9a66e 9750 *last_loc = b->loc;
3086aeae
DJ
9751 if (strstr (b->addr_string, "throw") != NULL)
9752 ui_out_field_string (uiout, "what", "exception throw");
9753 else
9754 ui_out_field_string (uiout, "what", "exception catch");
9755}
9756
9757static void
9758print_mention_exception_catchpoint (struct breakpoint *b)
9759{
ade92717
AR
9760 int bp_temp;
9761 int bp_throw;
9762
df2b6d2d 9763 bp_temp = b->disposition == disp_del;
ade92717
AR
9764 bp_throw = strstr (b->addr_string, "throw") != NULL;
9765 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9766 : _("Catchpoint "));
9767 ui_out_field_int (uiout, "bkptno", b->number);
9768 ui_out_text (uiout, bp_throw ? _(" (throw)")
9769 : _(" (catch)"));
3086aeae
DJ
9770}
9771
6149aea9
PA
9772/* Implement the "print_recreate" breakpoint_ops method for throw and
9773 catch catchpoints. */
9774
9775static void
4a64f543
MS
9776print_recreate_exception_catchpoint (struct breakpoint *b,
9777 struct ui_file *fp)
6149aea9
PA
9778{
9779 int bp_temp;
9780 int bp_throw;
9781
9782 bp_temp = b->disposition == disp_del;
9783 bp_throw = strstr (b->addr_string, "throw") != NULL;
9784 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9785 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 9786 print_recreate_thread (b, fp);
6149aea9
PA
9787}
9788
2060206e 9789static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
9790
9791static int
9792handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9793 enum exception_event_kind ex_event, int from_tty)
9794{
604133b5
AR
9795 char *trigger_func_name;
9796
3086aeae 9797 if (ex_event == EX_EVENT_CATCH)
604133b5 9798 trigger_func_name = "__cxa_begin_catch";
3086aeae 9799 else
604133b5 9800 trigger_func_name = "__cxa_throw";
3086aeae 9801
8cdf0e15
VP
9802 create_breakpoint (get_current_arch (),
9803 trigger_func_name, cond_string, -1,
9804 0 /* condition and thread are valid. */,
0fb4aa4b 9805 tempflag, bp_breakpoint,
8cdf0e15
VP
9806 0,
9807 AUTO_BOOLEAN_TRUE /* pending */,
9808 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9809 1 /* enabled */,
9810 0 /* internal */);
3086aeae 9811
3086aeae
DJ
9812 return 1;
9813}
9814
4a64f543 9815/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9816
9817static void
fba45db2
KB
9818catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9819 int tempflag, int from_tty)
c906108c 9820{
c5aa993b 9821 char *cond_string = NULL;
c5aa993b 9822
44feb3ce
TT
9823 if (!arg)
9824 arg = "";
e9cafbcc 9825 arg = skip_spaces (arg);
c5aa993b 9826
c906108c
SS
9827 cond_string = ep_parse_optional_if_clause (&arg);
9828
9829 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9830 error (_("Junk at end of arguments."));
c906108c 9831
059fb39f
PM
9832 if (ex_event != EX_EVENT_THROW
9833 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9834 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9835
3086aeae
DJ
9836 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9837 return;
9838
8a3fe4f8 9839 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9840}
9841
44feb3ce
TT
9842/* Implementation of "catch catch" command. */
9843
9844static void
9845catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9846{
9847 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9848
44feb3ce
TT
9849 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9850}
9851
9852/* Implementation of "catch throw" command. */
9853
9854static void
9855catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9856{
9857 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9858
44feb3ce
TT
9859 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9860}
9861
9ac4176b 9862void
28010a5d
PA
9863init_ada_exception_breakpoint (struct breakpoint *b,
9864 struct gdbarch *gdbarch,
9865 struct symtab_and_line sal,
9866 char *addr_string,
9867 struct breakpoint_ops *ops,
9868 int tempflag,
9869 int from_tty)
f7f9143b 9870{
f7f9143b
JB
9871 if (from_tty)
9872 {
5af949e3
UW
9873 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9874 if (!loc_gdbarch)
9875 loc_gdbarch = gdbarch;
9876
6c95b8df
PA
9877 describe_other_breakpoints (loc_gdbarch,
9878 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9879 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9880 version for exception catchpoints, because two catchpoints
9881 used for different exception names will use the same address.
9882 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9883 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 9884 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
9885 the user what type of catchpoint it is. The above is good
9886 enough for now, though. */
9887 }
9888
28010a5d 9889 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
9890
9891 b->enable_state = bp_enabled;
9892 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
9893 b->addr_string = addr_string;
9894 b->language = language_ada;
f7f9143b
JB
9895}
9896
a96d9b2e
SDJ
9897/* Cleanup function for a syscall filter list. */
9898static void
9899clean_up_filters (void *arg)
9900{
9901 VEC(int) *iter = *(VEC(int) **) arg;
9902 VEC_free (int, iter);
9903}
9904
9905/* Splits the argument using space as delimiter. Returns an xmalloc'd
9906 filter list, or NULL if no filtering is required. */
9907static VEC(int) *
9908catch_syscall_split_args (char *arg)
9909{
9910 VEC(int) *result = NULL;
9911 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9912
9913 while (*arg != '\0')
9914 {
9915 int i, syscall_number;
9916 char *endptr;
9917 char cur_name[128];
9918 struct syscall s;
9919
9920 /* Skip whitespace. */
9921 while (isspace (*arg))
9922 arg++;
9923
9924 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9925 cur_name[i] = arg[i];
9926 cur_name[i] = '\0';
9927 arg += i;
9928
9929 /* Check if the user provided a syscall name or a number. */
9930 syscall_number = (int) strtol (cur_name, &endptr, 0);
9931 if (*endptr == '\0')
bccd0dd2 9932 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9933 else
9934 {
9935 /* We have a name. Let's check if it's valid and convert it
9936 to a number. */
9937 get_syscall_by_name (cur_name, &s);
9938
9939 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
9940 /* Here we have to issue an error instead of a warning,
9941 because GDB cannot do anything useful if there's no
9942 syscall number to be caught. */
a96d9b2e
SDJ
9943 error (_("Unknown syscall name '%s'."), cur_name);
9944 }
9945
9946 /* Ok, it's valid. */
9947 VEC_safe_push (int, result, s.number);
9948 }
9949
9950 discard_cleanups (cleanup);
9951 return result;
9952}
9953
9954/* Implement the "catch syscall" command. */
9955
9956static void
cc59ec59
MS
9957catch_syscall_command_1 (char *arg, int from_tty,
9958 struct cmd_list_element *command)
a96d9b2e
SDJ
9959{
9960 int tempflag;
9961 VEC(int) *filter;
9962 struct syscall s;
9963 struct gdbarch *gdbarch = get_current_arch ();
9964
9965 /* Checking if the feature if supported. */
9966 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9967 error (_("The feature 'catch syscall' is not supported on \
ea666128 9968this architecture yet."));
a96d9b2e
SDJ
9969
9970 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9971
e9cafbcc 9972 arg = skip_spaces (arg);
a96d9b2e
SDJ
9973
9974 /* We need to do this first "dummy" translation in order
9975 to get the syscall XML file loaded or, most important,
9976 to display a warning to the user if there's no XML file
9977 for his/her architecture. */
9978 get_syscall_by_number (0, &s);
9979
9980 /* The allowed syntax is:
9981 catch syscall
9982 catch syscall <name | number> [<name | number> ... <name | number>]
9983
9984 Let's check if there's a syscall name. */
9985
9986 if (arg != NULL)
9987 filter = catch_syscall_split_args (arg);
9988 else
9989 filter = NULL;
9990
9991 create_syscall_event_catchpoint (tempflag, filter,
9992 &catch_syscall_breakpoint_ops);
9993}
9994
c906108c 9995static void
fba45db2 9996catch_command (char *arg, int from_tty)
c906108c 9997{
44feb3ce 9998 error (_("Catch requires an event name."));
c906108c
SS
9999}
10000\f
10001
10002static void
fba45db2 10003tcatch_command (char *arg, int from_tty)
c906108c 10004{
44feb3ce 10005 error (_("Catch requires an event name."));
c906108c
SS
10006}
10007
80f8a6eb 10008/* Delete breakpoints by address or line. */
c906108c
SS
10009
10010static void
fba45db2 10011clear_command (char *arg, int from_tty)
c906108c 10012{
d6e956e5
VP
10013 struct breakpoint *b;
10014 VEC(breakpoint_p) *found = 0;
10015 int ix;
c906108c
SS
10016 int default_match;
10017 struct symtabs_and_lines sals;
10018 struct symtab_and_line sal;
c906108c
SS
10019 int i;
10020
10021 if (arg)
10022 {
10023 sals = decode_line_spec (arg, 1);
10024 default_match = 0;
10025 }
10026 else
10027 {
c5aa993b 10028 sals.sals = (struct symtab_and_line *)
c906108c 10029 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 10030 make_cleanup (xfree, sals.sals);
4a64f543 10031 init_sal (&sal); /* Initialize to zeroes. */
c906108c
SS
10032 sal.line = default_breakpoint_line;
10033 sal.symtab = default_breakpoint_symtab;
10034 sal.pc = default_breakpoint_address;
6c95b8df 10035 sal.pspace = default_breakpoint_pspace;
c906108c 10036 if (sal.symtab == 0)
8a3fe4f8 10037 error (_("No source file specified."));
c906108c
SS
10038
10039 sals.sals[0] = sal;
10040 sals.nelts = 1;
10041
10042 default_match = 1;
10043 }
10044
4a64f543
MS
10045 /* We don't call resolve_sal_pc here. That's not as bad as it
10046 seems, because all existing breakpoints typically have both
10047 file/line and pc set. So, if clear is given file/line, we can
10048 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10049
10050 We only support clearing given the address explicitly
10051 present in breakpoint table. Say, we've set breakpoint
4a64f543 10052 at file:line. There were several PC values for that file:line,
ed0616c6 10053 due to optimization, all in one block.
4a64f543
MS
10054
10055 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10056 PC corresponding to the same file:line, the breakpoint won't
10057 be cleared. We probably can still clear the breakpoint, but
10058 since the other PC value is never presented to user, user
10059 can only find it by guessing, and it does not seem important
10060 to support that. */
10061
4a64f543
MS
10062 /* For each line spec given, delete bps which correspond to it. Do
10063 it in two passes, solely to preserve the current behavior that
10064 from_tty is forced true if we delete more than one
10065 breakpoint. */
c906108c 10066
80f8a6eb 10067 found = NULL;
c906108c
SS
10068 for (i = 0; i < sals.nelts; i++)
10069 {
10070 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
10071 If line given (pc == 0), clear all bpts on specified line.
10072 If defaulting, clear all bpts on default line
c906108c 10073 or at default pc.
c5aa993b
JM
10074
10075 defaulting sal.pc != 0 tests to do
10076
10077 0 1 pc
10078 1 1 pc _and_ line
10079 0 0 line
10080 1 0 <can't happen> */
c906108c
SS
10081
10082 sal = sals.sals[i];
c906108c 10083
4a64f543 10084 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 10085 ALL_BREAKPOINTS (b)
c5aa993b 10086 {
0d381245 10087 int match = 0;
4a64f543 10088 /* Are we going to delete b? */
cc60f2e3 10089 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
10090 {
10091 struct bp_location *loc = b->loc;
10092 for (; loc; loc = loc->next)
10093 {
6c95b8df
PA
10094 int pc_match = sal.pc
10095 && (loc->pspace == sal.pspace)
0d381245
VP
10096 && (loc->address == sal.pc)
10097 && (!section_is_overlay (loc->section)
10098 || loc->section == sal.section);
10099 int line_match = ((default_match || (0 == sal.pc))
10100 && b->source_file != NULL
10101 && sal.symtab != NULL
6c95b8df 10102 && sal.pspace == loc->pspace
0ba1096a
KT
10103 && filename_cmp (b->source_file,
10104 sal.symtab->filename) == 0
0d381245
VP
10105 && b->line_number == sal.line);
10106 if (pc_match || line_match)
10107 {
10108 match = 1;
10109 break;
10110 }
10111 }
10112 }
10113
10114 if (match)
d6e956e5 10115 VEC_safe_push(breakpoint_p, found, b);
c906108c 10116 }
80f8a6eb
MS
10117 }
10118 /* Now go thru the 'found' chain and delete them. */
d6e956e5 10119 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
10120 {
10121 if (arg)
8a3fe4f8 10122 error (_("No breakpoint at %s."), arg);
80f8a6eb 10123 else
8a3fe4f8 10124 error (_("No breakpoint at this line."));
80f8a6eb 10125 }
c906108c 10126
d6e956e5 10127 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 10128 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10129 if (from_tty)
a3f17187 10130 {
d6e956e5 10131 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
10132 printf_unfiltered (_("Deleted breakpoint "));
10133 else
10134 printf_unfiltered (_("Deleted breakpoints "));
10135 }
80f8a6eb 10136 breakpoints_changed ();
d6e956e5
VP
10137
10138 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 10139 {
c5aa993b 10140 if (from_tty)
d6e956e5
VP
10141 printf_unfiltered ("%d ", b->number);
10142 delete_breakpoint (b);
c906108c 10143 }
80f8a6eb
MS
10144 if (from_tty)
10145 putchar_unfiltered ('\n');
c906108c
SS
10146}
10147\f
10148/* Delete breakpoint in BS if they are `delete' breakpoints and
10149 all breakpoints that are marked for deletion, whether hit or not.
10150 This is called after any breakpoint is hit, or after errors. */
10151
10152void
fba45db2 10153breakpoint_auto_delete (bpstat bs)
c906108c 10154{
35df4500 10155 struct breakpoint *b, *b_tmp;
c906108c
SS
10156
10157 for (; bs; bs = bs->next)
f431efe5
PA
10158 if (bs->breakpoint_at
10159 && bs->breakpoint_at->disposition == disp_del
c906108c 10160 && bs->stop)
f431efe5 10161 delete_breakpoint (bs->breakpoint_at);
c906108c 10162
35df4500 10163 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10164 {
b5de0fa7 10165 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10166 delete_breakpoint (b);
10167 }
c906108c
SS
10168}
10169
4a64f543
MS
10170/* A comparison function for bp_location AP and BP being interfaced to
10171 qsort. Sort elements primarily by their ADDRESS (no matter what
10172 does breakpoint_address_is_meaningful say for its OWNER),
10173 secondarily by ordering first bp_permanent OWNERed elements and
10174 terciarily just ensuring the array is sorted stable way despite
e5dd4106 10175 qsort being an unstable algorithm. */
876fa593
JK
10176
10177static int
494cfb0f 10178bp_location_compare (const void *ap, const void *bp)
876fa593 10179{
494cfb0f
JK
10180 struct bp_location *a = *(void **) ap;
10181 struct bp_location *b = *(void **) bp;
2bdf28a0 10182 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10183 int a_perm = a->owner->enable_state == bp_permanent;
10184 int b_perm = b->owner->enable_state == bp_permanent;
10185
10186 if (a->address != b->address)
10187 return (a->address > b->address) - (a->address < b->address);
10188
10189 /* Sort permanent breakpoints first. */
10190 if (a_perm != b_perm)
10191 return (a_perm < b_perm) - (a_perm > b_perm);
10192
4a64f543
MS
10193 /* Make the user-visible order stable across GDB runs. Locations of
10194 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10195
10196 if (a->owner->number != b->owner->number)
10197 return (a->owner->number > b->owner->number)
10198 - (a->owner->number < b->owner->number);
10199
10200 return (a > b) - (a < b);
10201}
10202
876fa593 10203/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10204 bp_location_shadow_len_after_address_max according to the current
10205 content of the bp_location array. */
f7545552
TT
10206
10207static void
876fa593 10208bp_location_target_extensions_update (void)
f7545552 10209{
876fa593
JK
10210 struct bp_location *bl, **blp_tmp;
10211
10212 bp_location_placed_address_before_address_max = 0;
10213 bp_location_shadow_len_after_address_max = 0;
10214
10215 ALL_BP_LOCATIONS (bl, blp_tmp)
10216 {
10217 CORE_ADDR start, end, addr;
10218
10219 if (!bp_location_has_shadow (bl))
10220 continue;
10221
10222 start = bl->target_info.placed_address;
10223 end = start + bl->target_info.shadow_len;
10224
10225 gdb_assert (bl->address >= start);
10226 addr = bl->address - start;
10227 if (addr > bp_location_placed_address_before_address_max)
10228 bp_location_placed_address_before_address_max = addr;
10229
10230 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10231
10232 gdb_assert (bl->address < end);
10233 addr = end - bl->address;
10234 if (addr > bp_location_shadow_len_after_address_max)
10235 bp_location_shadow_len_after_address_max = addr;
10236 }
f7545552
TT
10237}
10238
4cd9bd08 10239/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10240 into the inferior, only remove already-inserted locations that no
10241 longer should be inserted. Functions that delete a breakpoint or
10242 breakpoints should pass false, so that deleting a breakpoint
10243 doesn't have the side effect of inserting the locations of other
10244 breakpoints that are marked not-inserted, but should_be_inserted
10245 returns true on them.
10246
10247 This behaviour is useful is situations close to tear-down -- e.g.,
10248 after an exec, while the target still has execution, but breakpoint
10249 shadows of the previous executable image should *NOT* be restored
10250 to the new image; or before detaching, where the target still has
10251 execution and wants to delete breakpoints from GDB's lists, and all
10252 breakpoints had already been removed from the inferior. */
10253
0d381245 10254static void
b60e7edf 10255update_global_location_list (int should_insert)
0d381245 10256{
74960c60 10257 struct breakpoint *b;
876fa593 10258 struct bp_location **locp, *loc;
f7545552
TT
10259 struct cleanup *cleanups;
10260
2d134ed3
PA
10261 /* Used in the duplicates detection below. When iterating over all
10262 bp_locations, points to the first bp_location of a given address.
10263 Breakpoints and watchpoints of different types are never
10264 duplicates of each other. Keep one pointer for each type of
10265 breakpoint/watchpoint, so we only need to loop over all locations
10266 once. */
10267 struct bp_location *bp_loc_first; /* breakpoint */
10268 struct bp_location *wp_loc_first; /* hardware watchpoint */
10269 struct bp_location *awp_loc_first; /* access watchpoint */
10270 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10271
4a64f543
MS
10272 /* Saved former bp_location array which we compare against the newly
10273 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10274 struct bp_location **old_location, **old_locp;
10275 unsigned old_location_count;
10276
10277 old_location = bp_location;
10278 old_location_count = bp_location_count;
10279 bp_location = NULL;
10280 bp_location_count = 0;
10281 cleanups = make_cleanup (xfree, old_location);
0d381245 10282
74960c60 10283 ALL_BREAKPOINTS (b)
876fa593
JK
10284 for (loc = b->loc; loc; loc = loc->next)
10285 bp_location_count++;
10286
10287 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10288 locp = bp_location;
10289 ALL_BREAKPOINTS (b)
10290 for (loc = b->loc; loc; loc = loc->next)
10291 *locp++ = loc;
10292 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10293 bp_location_compare);
876fa593
JK
10294
10295 bp_location_target_extensions_update ();
74960c60 10296
4a64f543
MS
10297 /* Identify bp_location instances that are no longer present in the
10298 new list, and therefore should be freed. Note that it's not
10299 necessary that those locations should be removed from inferior --
10300 if there's another location at the same address (previously
10301 marked as duplicate), we don't need to remove/insert the
10302 location.
876fa593 10303
4a64f543
MS
10304 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10305 and former bp_location array state respectively. */
876fa593
JK
10306
10307 locp = bp_location;
10308 for (old_locp = old_location; old_locp < old_location + old_location_count;
10309 old_locp++)
74960c60 10310 {
876fa593 10311 struct bp_location *old_loc = *old_locp;
c7d46a38 10312 struct bp_location **loc2p;
876fa593 10313
e5dd4106 10314 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 10315 not, we have to free it. */
c7d46a38 10316 int found_object = 0;
20874c92
VP
10317 /* Tells if the location should remain inserted in the target. */
10318 int keep_in_target = 0;
10319 int removed = 0;
876fa593 10320
4a64f543
MS
10321 /* Skip LOCP entries which will definitely never be needed.
10322 Stop either at or being the one matching OLD_LOC. */
876fa593 10323 while (locp < bp_location + bp_location_count
c7d46a38 10324 && (*locp)->address < old_loc->address)
876fa593 10325 locp++;
c7d46a38
PA
10326
10327 for (loc2p = locp;
10328 (loc2p < bp_location + bp_location_count
10329 && (*loc2p)->address == old_loc->address);
10330 loc2p++)
10331 {
10332 if (*loc2p == old_loc)
10333 {
10334 found_object = 1;
10335 break;
10336 }
10337 }
74960c60 10338
4a64f543
MS
10339 /* If this location is no longer present, and inserted, look if
10340 there's maybe a new location at the same address. If so,
10341 mark that one inserted, and don't remove this one. This is
10342 needed so that we don't have a time window where a breakpoint
10343 at certain location is not inserted. */
74960c60 10344
876fa593 10345 if (old_loc->inserted)
0d381245 10346 {
4a64f543
MS
10347 /* If the location is inserted now, we might have to remove
10348 it. */
74960c60 10349
876fa593 10350 if (found_object && should_be_inserted (old_loc))
74960c60 10351 {
4a64f543
MS
10352 /* The location is still present in the location list,
10353 and still should be inserted. Don't do anything. */
20874c92 10354 keep_in_target = 1;
74960c60
VP
10355 }
10356 else
10357 {
4a64f543
MS
10358 /* The location is either no longer present, or got
10359 disabled. See if there's another location at the
10360 same address, in which case we don't need to remove
10361 this one from the target. */
876fa593 10362
2bdf28a0 10363 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10364 if (breakpoint_address_is_meaningful (old_loc->owner))
10365 {
876fa593 10366 for (loc2p = locp;
c7d46a38
PA
10367 (loc2p < bp_location + bp_location_count
10368 && (*loc2p)->address == old_loc->address);
876fa593
JK
10369 loc2p++)
10370 {
10371 struct bp_location *loc2 = *loc2p;
10372
2d134ed3 10373 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38
PA
10374 {
10375 /* For the sake of should_be_inserted.
4a64f543
MS
10376 Duplicates check below will fix up this
10377 later. */
c7d46a38 10378 loc2->duplicate = 0;
85d721b8
PA
10379
10380 /* Read watchpoint locations are switched to
10381 access watchpoints, if the former are not
10382 supported, but the latter are. */
10383 if (is_hardware_watchpoint (old_loc->owner))
10384 {
10385 gdb_assert (is_hardware_watchpoint (loc2->owner));
10386 loc2->watchpoint_type = old_loc->watchpoint_type;
10387 }
10388
c7d46a38
PA
10389 if (loc2 != old_loc && should_be_inserted (loc2))
10390 {
10391 loc2->inserted = 1;
10392 loc2->target_info = old_loc->target_info;
10393 keep_in_target = 1;
10394 break;
10395 }
876fa593
JK
10396 }
10397 }
10398 }
74960c60
VP
10399 }
10400
20874c92
VP
10401 if (!keep_in_target)
10402 {
876fa593 10403 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10404 {
4a64f543
MS
10405 /* This is just about all we can do. We could keep
10406 this location on the global list, and try to
10407 remove it next time, but there's no particular
10408 reason why we will succeed next time.
20874c92 10409
4a64f543
MS
10410 Note that at this point, old_loc->owner is still
10411 valid, as delete_breakpoint frees the breakpoint
10412 only after calling us. */
3e43a32a
MS
10413 printf_filtered (_("warning: Error removing "
10414 "breakpoint %d\n"),
876fa593 10415 old_loc->owner->number);
20874c92
VP
10416 }
10417 removed = 1;
10418 }
0d381245 10419 }
74960c60
VP
10420
10421 if (!found_object)
1c5cfe86 10422 {
db82e815
PA
10423 if (removed && non_stop
10424 && breakpoint_address_is_meaningful (old_loc->owner)
10425 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10426 {
db82e815
PA
10427 /* This location was removed from the target. In
10428 non-stop mode, a race condition is possible where
10429 we've removed a breakpoint, but stop events for that
10430 breakpoint are already queued and will arrive later.
10431 We apply an heuristic to be able to distinguish such
10432 SIGTRAPs from other random SIGTRAPs: we keep this
10433 breakpoint location for a bit, and will retire it
10434 after we see some number of events. The theory here
10435 is that reporting of events should, "on the average",
10436 be fair, so after a while we'll see events from all
10437 threads that have anything of interest, and no longer
10438 need to keep this breakpoint location around. We
10439 don't hold locations forever so to reduce chances of
10440 mistaking a non-breakpoint SIGTRAP for a breakpoint
10441 SIGTRAP.
10442
10443 The heuristic failing can be disastrous on
10444 decr_pc_after_break targets.
10445
10446 On decr_pc_after_break targets, like e.g., x86-linux,
10447 if we fail to recognize a late breakpoint SIGTRAP,
10448 because events_till_retirement has reached 0 too
10449 soon, we'll fail to do the PC adjustment, and report
10450 a random SIGTRAP to the user. When the user resumes
10451 the inferior, it will most likely immediately crash
2dec564e 10452 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10453 corrupted, because of being resumed e.g., in the
10454 middle of a multi-byte instruction, or skipped a
10455 one-byte instruction. This was actually seen happen
10456 on native x86-linux, and should be less rare on
10457 targets that do not support new thread events, like
10458 remote, due to the heuristic depending on
10459 thread_count.
10460
10461 Mistaking a random SIGTRAP for a breakpoint trap
10462 causes similar symptoms (PC adjustment applied when
10463 it shouldn't), but then again, playing with SIGTRAPs
10464 behind the debugger's back is asking for trouble.
10465
10466 Since hardware watchpoint traps are always
10467 distinguishable from other traps, so we don't need to
10468 apply keep hardware watchpoint moribund locations
10469 around. We simply always ignore hardware watchpoint
10470 traps we can no longer explain. */
10471
876fa593
JK
10472 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10473 old_loc->owner = NULL;
20874c92 10474
876fa593 10475 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10476 }
10477 else
f431efe5
PA
10478 {
10479 old_loc->owner = NULL;
10480 decref_bp_location (&old_loc);
10481 }
20874c92 10482 }
74960c60 10483 }
1c5cfe86 10484
348d480f
PA
10485 /* Rescan breakpoints at the same address and section, marking the
10486 first one as "first" and any others as "duplicates". This is so
10487 that the bpt instruction is only inserted once. If we have a
10488 permanent breakpoint at the same place as BPT, make that one the
10489 official one, and the rest as duplicates. Permanent breakpoints
10490 are sorted first for the same address.
10491
10492 Do the same for hardware watchpoints, but also considering the
10493 watchpoint's type (regular/access/read) and length. */
10494
10495 bp_loc_first = NULL;
10496 wp_loc_first = NULL;
10497 awp_loc_first = NULL;
10498 rwp_loc_first = NULL;
10499 ALL_BP_LOCATIONS (loc, locp)
10500 {
10501 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10502 non-NULL. */
10503 struct breakpoint *b = loc->owner;
10504 struct bp_location **loc_first_p;
10505
10506 if (b->enable_state == bp_disabled
10507 || b->enable_state == bp_call_disabled
10508 || b->enable_state == bp_startup_disabled
10509 || !loc->enabled
10510 || loc->shlib_disabled
10511 || !breakpoint_address_is_meaningful (b)
10512 || is_tracepoint (b))
10513 continue;
10514
10515 /* Permanent breakpoint should always be inserted. */
10516 if (b->enable_state == bp_permanent && ! loc->inserted)
10517 internal_error (__FILE__, __LINE__,
10518 _("allegedly permanent breakpoint is not "
10519 "actually inserted"));
10520
10521 if (b->type == bp_hardware_watchpoint)
10522 loc_first_p = &wp_loc_first;
10523 else if (b->type == bp_read_watchpoint)
10524 loc_first_p = &rwp_loc_first;
10525 else if (b->type == bp_access_watchpoint)
10526 loc_first_p = &awp_loc_first;
10527 else
10528 loc_first_p = &bp_loc_first;
10529
10530 if (*loc_first_p == NULL
10531 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10532 || !breakpoint_locations_match (loc, *loc_first_p))
10533 {
10534 *loc_first_p = loc;
10535 loc->duplicate = 0;
10536 continue;
10537 }
10538
10539 loc->duplicate = 1;
10540
10541 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10542 && b->enable_state != bp_permanent)
10543 internal_error (__FILE__, __LINE__,
10544 _("another breakpoint was inserted on top of "
10545 "a permanent breakpoint"));
10546 }
10547
10548 if (breakpoints_always_inserted_mode () && should_insert
10549 && (have_live_inferiors ()
10550 || (gdbarch_has_global_breakpoints (target_gdbarch))))
10551 insert_breakpoint_locations ();
10552
10553 do_cleanups (cleanups);
10554}
10555
10556void
10557breakpoint_retire_moribund (void)
10558{
10559 struct bp_location *loc;
10560 int ix;
10561
10562 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10563 if (--(loc->events_till_retirement) == 0)
10564 {
10565 decref_bp_location (&loc);
10566 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10567 --ix;
10568 }
10569}
10570
10571static void
10572update_global_location_list_nothrow (int inserting)
10573{
10574 struct gdb_exception e;
10575
10576 TRY_CATCH (e, RETURN_MASK_ERROR)
10577 update_global_location_list (inserting);
10578}
10579
10580/* Clear BKP from a BPS. */
10581
10582static void
10583bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10584{
10585 bpstat bs;
10586
10587 for (bs = bps; bs; bs = bs->next)
10588 if (bs->breakpoint_at == bpt)
10589 {
10590 bs->breakpoint_at = NULL;
10591 bs->old_val = NULL;
10592 /* bs->commands will be freed later. */
10593 }
10594}
10595
10596/* Callback for iterate_over_threads. */
10597static int
10598bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10599{
10600 struct breakpoint *bpt = data;
10601
10602 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10603 return 0;
10604}
10605
10606/* Helper for breakpoint and tracepoint breakpoint_ops->mention
10607 callbacks. */
10608
10609static void
10610say_where (struct breakpoint *b)
10611{
10612 struct value_print_options opts;
10613
10614 get_user_print_options (&opts);
10615
10616 /* i18n: cagney/2005-02-11: Below needs to be merged into a
10617 single string. */
10618 if (b->loc == NULL)
10619 {
10620 printf_filtered (_(" (%s) pending."), b->addr_string);
10621 }
10622 else
10623 {
10624 if (opts.addressprint || b->source_file == NULL)
10625 {
10626 printf_filtered (" at ");
10627 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
10628 gdb_stdout);
10629 }
10630 if (b->source_file)
10631 printf_filtered (": file %s, line %d.",
10632 b->source_file, b->line_number);
10633
10634 if (b->loc->next)
10635 {
10636 struct bp_location *loc = b->loc;
10637 int n = 0;
10638 for (; loc; loc = loc->next)
10639 ++n;
10640 printf_filtered (" (%d locations)", n);
10641 }
10642 }
10643}
10644
348d480f
PA
10645/* Default bp_location_ops methods. */
10646
10647static void
10648bp_location_dtor (struct bp_location *self)
10649{
10650 xfree (self->cond);
10651 xfree (self->function_name);
10652}
10653
10654static const struct bp_location_ops bp_location_ops =
10655{
10656 bp_location_dtor
10657};
10658
2060206e
PA
10659/* Default breakpoint_ops methods all breakpoint_ops ultimately
10660 inherit from. */
348d480f 10661
2060206e
PA
10662static void
10663base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
10664{
10665 decref_counted_command_line (&self->commands);
10666 xfree (self->cond_string);
348d480f
PA
10667 xfree (self->addr_string);
10668 xfree (self->addr_string_range_end);
348d480f
PA
10669 xfree (self->source_file);
10670}
10671
2060206e
PA
10672static struct bp_location *
10673base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
10674{
10675 struct bp_location *loc;
10676
10677 loc = XNEW (struct bp_location);
10678 init_bp_location (loc, &bp_location_ops, self);
10679 return loc;
10680}
10681
2060206e
PA
10682static void
10683base_breakpoint_re_set (struct breakpoint *b)
10684{
10685 /* Nothing to re-set. */
10686}
10687
10688#define internal_error_pure_virtual_called() \
10689 gdb_assert_not_reached ("pure virtual function called")
10690
10691static int
10692base_breakpoint_insert_location (struct bp_location *bl)
10693{
10694 internal_error_pure_virtual_called ();
10695}
10696
10697static int
10698base_breakpoint_remove_location (struct bp_location *bl)
10699{
10700 internal_error_pure_virtual_called ();
10701}
10702
10703static int
10704base_breakpoint_breakpoint_hit (const struct bp_location *bl,
10705 struct address_space *aspace,
10706 CORE_ADDR bp_addr)
10707{
10708 internal_error_pure_virtual_called ();
10709}
10710
10711static void
10712base_breakpoint_check_status (bpstat bs)
10713{
10714 /* Always stop. */
10715}
10716
10717/* A "works_in_software_mode" breakpoint_ops method that just internal
10718 errors. */
10719
10720static int
10721base_breakpoint_works_in_software_mode (const struct breakpoint *b)
10722{
10723 internal_error_pure_virtual_called ();
10724}
10725
10726/* A "resources_needed" breakpoint_ops method that just internal
10727 errors. */
10728
10729static int
10730base_breakpoint_resources_needed (const struct bp_location *bl)
10731{
10732 internal_error_pure_virtual_called ();
10733}
10734
10735static enum print_stop_action
10736base_breakpoint_print_it (bpstat bs)
10737{
10738 internal_error_pure_virtual_called ();
10739}
10740
10741static void
10742base_breakpoint_print_one_detail (const struct breakpoint *self,
10743 struct ui_out *uiout)
10744{
10745 /* nothing */
10746}
10747
10748static void
10749base_breakpoint_print_mention (struct breakpoint *b)
10750{
10751 internal_error_pure_virtual_called ();
10752}
10753
10754static void
10755base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
10756{
10757 internal_error_pure_virtual_called ();
10758}
10759
10760static struct breakpoint_ops base_breakpoint_ops =
10761{
10762 base_breakpoint_dtor,
10763 base_breakpoint_allocate_location,
10764 base_breakpoint_re_set,
10765 base_breakpoint_insert_location,
10766 base_breakpoint_remove_location,
10767 base_breakpoint_breakpoint_hit,
10768 base_breakpoint_check_status,
10769 base_breakpoint_resources_needed,
10770 base_breakpoint_works_in_software_mode,
10771 base_breakpoint_print_it,
10772 NULL,
10773 base_breakpoint_print_one_detail,
10774 base_breakpoint_print_mention,
10775 base_breakpoint_print_recreate
10776};
10777
10778/* Default breakpoint_ops methods. */
10779
10780static void
348d480f
PA
10781bkpt_re_set (struct breakpoint *b)
10782{
06edf0c0
PA
10783 /* Do not attempt to re-set breakpoints disabled during startup. */
10784 if (b->enable_state == bp_startup_disabled)
10785 return;
348d480f 10786
06edf0c0
PA
10787 /* FIXME: is this still reachable? */
10788 if (b->addr_string == NULL)
10789 {
10790 /* Anything without a string can't be re-set. */
348d480f 10791 delete_breakpoint (b);
06edf0c0 10792 return;
348d480f 10793 }
06edf0c0
PA
10794
10795 breakpoint_re_set_default (b);
348d480f
PA
10796}
10797
2060206e 10798static int
348d480f
PA
10799bkpt_insert_location (struct bp_location *bl)
10800{
10801 if (bl->loc_type == bp_loc_hardware_breakpoint)
10802 return target_insert_hw_breakpoint (bl->gdbarch,
10803 &bl->target_info);
10804 else
10805 return target_insert_breakpoint (bl->gdbarch,
10806 &bl->target_info);
10807}
10808
2060206e 10809static int
348d480f
PA
10810bkpt_remove_location (struct bp_location *bl)
10811{
10812 if (bl->loc_type == bp_loc_hardware_breakpoint)
10813 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
10814 else
10815 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
10816}
10817
2060206e 10818static int
348d480f
PA
10819bkpt_breakpoint_hit (const struct bp_location *bl,
10820 struct address_space *aspace, CORE_ADDR bp_addr)
10821{
10822 struct breakpoint *b = bl->owner;
10823
10824 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
10825 aspace, bp_addr))
10826 return 0;
10827
10828 if (overlay_debugging /* unmapped overlay section */
10829 && section_is_overlay (bl->section)
10830 && !section_is_mapped (bl->section))
10831 return 0;
10832
10833 return 1;
10834}
10835
2060206e 10836static int
348d480f
PA
10837bkpt_resources_needed (const struct bp_location *bl)
10838{
10839 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
10840
10841 return 1;
10842}
10843
2060206e 10844static enum print_stop_action
348d480f
PA
10845bkpt_print_it (bpstat bs)
10846{
348d480f
PA
10847 struct breakpoint *b;
10848 const struct bp_location *bl;
001c8c33 10849 int bp_temp;
348d480f
PA
10850
10851 gdb_assert (bs->bp_location_at != NULL);
10852
10853 bl = bs->bp_location_at;
10854 b = bs->breakpoint_at;
10855
001c8c33
PA
10856 bp_temp = b->disposition == disp_del;
10857 if (bl->address != bl->requested_address)
10858 breakpoint_adjustment_warning (bl->requested_address,
10859 bl->address,
10860 b->number, 1);
10861 annotate_breakpoint (b->number);
10862 if (bp_temp)
10863 ui_out_text (uiout, "\nTemporary breakpoint ");
10864 else
10865 ui_out_text (uiout, "\nBreakpoint ");
10866 if (ui_out_is_mi_like_p (uiout))
348d480f 10867 {
001c8c33
PA
10868 ui_out_field_string (uiout, "reason",
10869 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10870 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 10871 }
001c8c33
PA
10872 ui_out_field_int (uiout, "bkptno", b->number);
10873 ui_out_text (uiout, ", ");
06edf0c0 10874
001c8c33 10875 return PRINT_SRC_AND_LOC;
06edf0c0
PA
10876}
10877
2060206e 10878static void
06edf0c0
PA
10879bkpt_print_mention (struct breakpoint *b)
10880{
10881 if (ui_out_is_mi_like_p (uiout))
10882 return;
10883
10884 switch (b->type)
10885 {
10886 case bp_breakpoint:
10887 case bp_gnu_ifunc_resolver:
10888 if (b->disposition == disp_del)
10889 printf_filtered (_("Temporary breakpoint"));
10890 else
10891 printf_filtered (_("Breakpoint"));
10892 printf_filtered (_(" %d"), b->number);
10893 if (b->type == bp_gnu_ifunc_resolver)
10894 printf_filtered (_(" at gnu-indirect-function resolver"));
10895 break;
10896 case bp_hardware_breakpoint:
10897 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
10898 break;
10899 }
10900
10901 say_where (b);
10902}
10903
2060206e 10904static void
06edf0c0
PA
10905bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
10906{
10907 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
10908 fprintf_unfiltered (fp, "tbreak");
10909 else if (tp->type == bp_breakpoint)
10910 fprintf_unfiltered (fp, "break");
10911 else if (tp->type == bp_hardware_breakpoint
10912 && tp->disposition == disp_del)
10913 fprintf_unfiltered (fp, "thbreak");
10914 else if (tp->type == bp_hardware_breakpoint)
10915 fprintf_unfiltered (fp, "hbreak");
10916 else
10917 internal_error (__FILE__, __LINE__,
10918 _("unhandled breakpoint type %d"), (int) tp->type);
10919
2060206e 10920 fprintf_unfiltered (fp, " %s", tp->addr_string);
06edf0c0
PA
10921}
10922
06edf0c0
PA
10923/* Virtual table for internal breakpoints. */
10924
10925static void
10926internal_bkpt_re_set (struct breakpoint *b)
10927{
10928 switch (b->type)
10929 {
10930 /* Delete overlay event and longjmp master breakpoints; they
10931 will be reset later by breakpoint_re_set. */
10932 case bp_overlay_event:
10933 case bp_longjmp_master:
10934 case bp_std_terminate_master:
10935 case bp_exception_master:
10936 delete_breakpoint (b);
10937 break;
10938
10939 /* This breakpoint is special, it's set up when the inferior
10940 starts and we really don't want to touch it. */
10941 case bp_shlib_event:
10942
10943 /* Like bp_shlib_event, this breakpoint type is special. Once
10944 it is set up, we do not want to touch it. */
10945 case bp_thread_event:
10946 break;
10947 }
10948}
10949
10950static void
10951internal_bkpt_check_status (bpstat bs)
10952{
10953 /* We do not stop for these. */
10954 bs->stop = 0;
10955}
10956
10957static enum print_stop_action
10958internal_bkpt_print_it (bpstat bs)
10959{
06edf0c0 10960 struct breakpoint *b;
06edf0c0 10961
06edf0c0
PA
10962 b = bs->breakpoint_at;
10963
06edf0c0
PA
10964 switch (b->type)
10965 {
348d480f
PA
10966 case bp_shlib_event:
10967 /* Did we stop because the user set the stop_on_solib_events
10968 variable? (If so, we report this as a generic, "Stopped due
10969 to shlib event" message.) */
10970 printf_filtered (_("Stopped due to shared library event\n"));
348d480f
PA
10971 break;
10972
10973 case bp_thread_event:
10974 /* Not sure how we will get here.
10975 GDB should not stop for these breakpoints. */
10976 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
10977 break;
10978
10979 case bp_overlay_event:
10980 /* By analogy with the thread event, GDB should not stop for these. */
10981 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
10982 break;
10983
10984 case bp_longjmp_master:
10985 /* These should never be enabled. */
10986 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
10987 break;
10988
10989 case bp_std_terminate_master:
10990 /* These should never be enabled. */
10991 printf_filtered (_("std::terminate Master Breakpoint: "
10992 "gdb should not stop!\n"));
348d480f
PA
10993 break;
10994
10995 case bp_exception_master:
10996 /* These should never be enabled. */
10997 printf_filtered (_("Exception Master Breakpoint: "
10998 "gdb should not stop!\n"));
06edf0c0
PA
10999 break;
11000 }
11001
001c8c33 11002 return PRINT_NOTHING;
06edf0c0
PA
11003}
11004
11005static void
11006internal_bkpt_print_mention (struct breakpoint *b)
11007{
11008 /* Nothing to mention. These breakpoints are internal. */
11009}
11010
06edf0c0
PA
11011/* Virtual table for momentary breakpoints */
11012
11013static void
11014momentary_bkpt_re_set (struct breakpoint *b)
11015{
11016 /* Keep temporary breakpoints, which can be encountered when we step
11017 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
11018 Otherwise these should have been blown away via the cleanup chain
11019 or by breakpoint_init_inferior when we rerun the executable. */
11020}
11021
11022static void
11023momentary_bkpt_check_status (bpstat bs)
11024{
11025 /* Nothing. The point of these breakpoints is causing a stop. */
11026}
11027
11028static enum print_stop_action
11029momentary_bkpt_print_it (bpstat bs)
11030{
001c8c33 11031 if (ui_out_is_mi_like_p (uiout))
06edf0c0 11032 {
001c8c33 11033 struct breakpoint *b = bs->breakpoint_at;
348d480f 11034
001c8c33
PA
11035 switch (b->type)
11036 {
11037 case bp_finish:
11038 ui_out_field_string
11039 (uiout, "reason",
11040 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
11041 break;
348d480f 11042
001c8c33
PA
11043 case bp_until:
11044 ui_out_field_string
11045 (uiout, "reason",
11046 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
11047 break;
11048 }
348d480f
PA
11049 }
11050
001c8c33 11051 return PRINT_UNKNOWN;
348d480f
PA
11052}
11053
06edf0c0
PA
11054static void
11055momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 11056{
06edf0c0 11057 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11058}
11059
348d480f 11060/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 11061
348d480f
PA
11062static void
11063tracepoint_re_set (struct breakpoint *b)
11064{
11065 breakpoint_re_set_default (b);
11066}
876fa593 11067
348d480f
PA
11068static int
11069tracepoint_breakpoint_hit (const struct bp_location *bl,
11070 struct address_space *aspace, CORE_ADDR bp_addr)
11071{
11072 /* By definition, the inferior does not report stops at
11073 tracepoints. */
11074 return 0;
74960c60
VP
11075}
11076
11077static void
348d480f
PA
11078tracepoint_print_one_detail (const struct breakpoint *self,
11079 struct ui_out *uiout)
74960c60 11080{
d9b3f62e
PA
11081 struct tracepoint *tp = (struct tracepoint *) self;
11082 if (tp->static_trace_marker_id)
348d480f
PA
11083 {
11084 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 11085
348d480f
PA
11086 ui_out_text (uiout, "\tmarker id is ");
11087 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 11088 tp->static_trace_marker_id);
348d480f
PA
11089 ui_out_text (uiout, "\n");
11090 }
0d381245
VP
11091}
11092
a474d7c2 11093static void
348d480f 11094tracepoint_print_mention (struct breakpoint *b)
a474d7c2 11095{
348d480f
PA
11096 if (ui_out_is_mi_like_p (uiout))
11097 return;
cc59ec59 11098
348d480f
PA
11099 switch (b->type)
11100 {
11101 case bp_tracepoint:
11102 printf_filtered (_("Tracepoint"));
11103 printf_filtered (_(" %d"), b->number);
11104 break;
11105 case bp_fast_tracepoint:
11106 printf_filtered (_("Fast tracepoint"));
11107 printf_filtered (_(" %d"), b->number);
11108 break;
11109 case bp_static_tracepoint:
11110 printf_filtered (_("Static tracepoint"));
11111 printf_filtered (_(" %d"), b->number);
11112 break;
11113 default:
11114 internal_error (__FILE__, __LINE__,
11115 _("unhandled tracepoint type %d"), (int) b->type);
11116 }
11117
11118 say_where (b);
a474d7c2
PA
11119}
11120
348d480f 11121static void
d9b3f62e 11122tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 11123{
d9b3f62e
PA
11124 struct tracepoint *tp = (struct tracepoint *) self;
11125
11126 if (self->type == bp_fast_tracepoint)
348d480f 11127 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 11128 if (self->type == bp_static_tracepoint)
348d480f 11129 fprintf_unfiltered (fp, "strace");
d9b3f62e 11130 else if (self->type == bp_tracepoint)
348d480f
PA
11131 fprintf_unfiltered (fp, "trace");
11132 else
11133 internal_error (__FILE__, __LINE__,
d9b3f62e 11134 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 11135
d9b3f62e
PA
11136 fprintf_unfiltered (fp, " %s", self->addr_string);
11137 print_recreate_thread (self, fp);
11138
11139 if (tp->pass_count)
11140 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
11141}
11142
2060206e 11143struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 11144
53a5351d 11145/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 11146 structures. */
c906108c
SS
11147
11148void
fba45db2 11149delete_breakpoint (struct breakpoint *bpt)
c906108c 11150{
52f0bd74 11151 struct breakpoint *b;
c906108c 11152
8a3fe4f8 11153 gdb_assert (bpt != NULL);
c906108c 11154
4a64f543
MS
11155 /* Has this bp already been deleted? This can happen because
11156 multiple lists can hold pointers to bp's. bpstat lists are
11157 especial culprits.
11158
11159 One example of this happening is a watchpoint's scope bp. When
11160 the scope bp triggers, we notice that the watchpoint is out of
11161 scope, and delete it. We also delete its scope bp. But the
11162 scope bp is marked "auto-deleting", and is already on a bpstat.
11163 That bpstat is then checked for auto-deleting bp's, which are
11164 deleted.
11165
11166 A real solution to this problem might involve reference counts in
11167 bp's, and/or giving them pointers back to their referencing
11168 bpstat's, and teaching delete_breakpoint to only free a bp's
11169 storage when no more references were extent. A cheaper bandaid
11170 was chosen. */
c906108c
SS
11171 if (bpt->type == bp_none)
11172 return;
11173
4a64f543
MS
11174 /* At least avoid this stale reference until the reference counting
11175 of breakpoints gets resolved. */
d0fb5eae 11176 if (bpt->related_breakpoint != bpt)
e5a0a904 11177 {
d0fb5eae 11178 struct breakpoint *related;
3a5c3e22 11179 struct watchpoint *w;
d0fb5eae
JK
11180
11181 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 11182 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 11183 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
11184 w = (struct watchpoint *) bpt;
11185 else
11186 w = NULL;
11187 if (w != NULL)
11188 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
11189
11190 /* Unlink bpt from the bpt->related_breakpoint ring. */
11191 for (related = bpt; related->related_breakpoint != bpt;
11192 related = related->related_breakpoint);
11193 related->related_breakpoint = bpt->related_breakpoint;
11194 bpt->related_breakpoint = bpt;
e5a0a904
JK
11195 }
11196
a9634178
TJB
11197 /* watch_command_1 creates a watchpoint but only sets its number if
11198 update_watchpoint succeeds in creating its bp_locations. If there's
11199 a problem in that process, we'll be asked to delete the half-created
11200 watchpoint. In that case, don't announce the deletion. */
11201 if (bpt->number)
11202 observer_notify_breakpoint_deleted (bpt);
c906108c 11203
c906108c
SS
11204 if (breakpoint_chain == bpt)
11205 breakpoint_chain = bpt->next;
11206
c906108c
SS
11207 ALL_BREAKPOINTS (b)
11208 if (b->next == bpt)
c5aa993b
JM
11209 {
11210 b->next = bpt->next;
11211 break;
11212 }
c906108c 11213
f431efe5
PA
11214 /* Be sure no bpstat's are pointing at the breakpoint after it's
11215 been freed. */
11216 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 11217 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
11218 pointing at bpt from the stop_bpstat list entirely, as breakpoint
11219 commands are associated with the bpstat; if we remove it here,
11220 then the later call to bpstat_do_actions (&stop_bpstat); in
11221 event-top.c won't do anything, and temporary breakpoints with
11222 commands won't work. */
11223
11224 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11225
4a64f543
MS
11226 /* Now that breakpoint is removed from breakpoint list, update the
11227 global location list. This will remove locations that used to
11228 belong to this breakpoint. Do this before freeing the breakpoint
11229 itself, since remove_breakpoint looks at location's owner. It
11230 might be better design to have location completely
11231 self-contained, but it's not the case now. */
b60e7edf 11232 update_global_location_list (0);
74960c60 11233
348d480f 11234 bpt->ops->dtor (bpt);
4a64f543
MS
11235 /* On the chance that someone will soon try again to delete this
11236 same bp, we mark it as deleted before freeing its storage. */
c906108c 11237 bpt->type = bp_none;
b8c9b27d 11238 xfree (bpt);
c906108c
SS
11239}
11240
4d6140d9
AC
11241static void
11242do_delete_breakpoint_cleanup (void *b)
11243{
11244 delete_breakpoint (b);
11245}
11246
11247struct cleanup *
11248make_cleanup_delete_breakpoint (struct breakpoint *b)
11249{
11250 return make_cleanup (do_delete_breakpoint_cleanup, b);
11251}
11252
51be5b68
PA
11253/* Iterator function to call a user-provided callback function once
11254 for each of B and its related breakpoints. */
11255
11256static void
11257iterate_over_related_breakpoints (struct breakpoint *b,
11258 void (*function) (struct breakpoint *,
11259 void *),
11260 void *data)
11261{
11262 struct breakpoint *related;
11263
11264 related = b;
11265 do
11266 {
11267 struct breakpoint *next;
11268
11269 /* FUNCTION may delete RELATED. */
11270 next = related->related_breakpoint;
11271
11272 if (next == related)
11273 {
11274 /* RELATED is the last ring entry. */
11275 function (related, data);
11276
11277 /* FUNCTION may have deleted it, so we'd never reach back to
11278 B. There's nothing left to do anyway, so just break
11279 out. */
11280 break;
11281 }
11282 else
11283 function (related, data);
11284
11285 related = next;
11286 }
11287 while (related != b);
11288}
95a42b64
TT
11289
11290static void
11291do_delete_breakpoint (struct breakpoint *b, void *ignore)
11292{
11293 delete_breakpoint (b);
11294}
11295
51be5b68
PA
11296/* A callback for map_breakpoint_numbers that calls
11297 delete_breakpoint. */
11298
11299static void
11300do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
11301{
11302 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
11303}
11304
c906108c 11305void
fba45db2 11306delete_command (char *arg, int from_tty)
c906108c 11307{
35df4500 11308 struct breakpoint *b, *b_tmp;
c906108c 11309
ea9365bb
TT
11310 dont_repeat ();
11311
c906108c
SS
11312 if (arg == 0)
11313 {
11314 int breaks_to_delete = 0;
11315
46c6471b
PA
11316 /* Delete all breakpoints if no argument. Do not delete
11317 internal breakpoints, these have to be deleted with an
11318 explicit breakpoint number argument. */
c5aa993b 11319 ALL_BREAKPOINTS (b)
46c6471b 11320 if (user_breakpoint_p (b))
973d738b
DJ
11321 {
11322 breaks_to_delete = 1;
11323 break;
11324 }
c906108c
SS
11325
11326 /* Ask user only if there are some breakpoints to delete. */
11327 if (!from_tty
e2e0b3e5 11328 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 11329 {
35df4500 11330 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 11331 if (user_breakpoint_p (b))
c5aa993b 11332 delete_breakpoint (b);
c906108c
SS
11333 }
11334 }
11335 else
51be5b68 11336 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
11337}
11338
0d381245
VP
11339static int
11340all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 11341{
0d381245
VP
11342 for (; loc; loc = loc->next)
11343 if (!loc->shlib_disabled)
11344 return 0;
11345 return 1;
fe3f5fa8
VP
11346}
11347
776592bf
DE
11348/* Subroutine of update_breakpoint_locations to simplify it.
11349 Return non-zero if multiple fns in list LOC have the same name.
11350 Null names are ignored. */
11351
11352static int
11353ambiguous_names_p (struct bp_location *loc)
11354{
11355 struct bp_location *l;
11356 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
11357 (int (*) (const void *,
11358 const void *)) streq,
776592bf
DE
11359 NULL, xcalloc, xfree);
11360
11361 for (l = loc; l != NULL; l = l->next)
11362 {
11363 const char **slot;
11364 const char *name = l->function_name;
11365
11366 /* Allow for some names to be NULL, ignore them. */
11367 if (name == NULL)
11368 continue;
11369
11370 slot = (const char **) htab_find_slot (htab, (const void *) name,
11371 INSERT);
4a64f543
MS
11372 /* NOTE: We can assume slot != NULL here because xcalloc never
11373 returns NULL. */
776592bf
DE
11374 if (*slot != NULL)
11375 {
11376 htab_delete (htab);
11377 return 1;
11378 }
11379 *slot = name;
11380 }
11381
11382 htab_delete (htab);
11383 return 0;
11384}
11385
0fb4aa4b
PA
11386/* When symbols change, it probably means the sources changed as well,
11387 and it might mean the static tracepoint markers are no longer at
11388 the same address or line numbers they used to be at last we
11389 checked. Losing your static tracepoints whenever you rebuild is
11390 undesirable. This function tries to resync/rematch gdb static
11391 tracepoints with the markers on the target, for static tracepoints
11392 that have not been set by marker id. Static tracepoint that have
11393 been set by marker id are reset by marker id in breakpoint_re_set.
11394 The heuristic is:
11395
11396 1) For a tracepoint set at a specific address, look for a marker at
11397 the old PC. If one is found there, assume to be the same marker.
11398 If the name / string id of the marker found is different from the
11399 previous known name, assume that means the user renamed the marker
11400 in the sources, and output a warning.
11401
11402 2) For a tracepoint set at a given line number, look for a marker
11403 at the new address of the old line number. If one is found there,
11404 assume to be the same marker. If the name / string id of the
11405 marker found is different from the previous known name, assume that
11406 means the user renamed the marker in the sources, and output a
11407 warning.
11408
11409 3) If a marker is no longer found at the same address or line, it
11410 may mean the marker no longer exists. But it may also just mean
11411 the code changed a bit. Maybe the user added a few lines of code
11412 that made the marker move up or down (in line number terms). Ask
11413 the target for info about the marker with the string id as we knew
11414 it. If found, update line number and address in the matching
11415 static tracepoint. This will get confused if there's more than one
11416 marker with the same ID (possible in UST, although unadvised
11417 precisely because it confuses tools). */
11418
11419static struct symtab_and_line
11420update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11421{
d9b3f62e 11422 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
11423 struct static_tracepoint_marker marker;
11424 CORE_ADDR pc;
11425 int i;
11426
11427 pc = sal.pc;
11428 if (sal.line)
11429 find_line_pc (sal.symtab, sal.line, &pc);
11430
11431 if (target_static_tracepoint_marker_at (pc, &marker))
11432 {
d9b3f62e 11433 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
11434 warning (_("static tracepoint %d changed probed marker from %s to %s"),
11435 b->number,
d9b3f62e 11436 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 11437
d9b3f62e
PA
11438 xfree (tp->static_trace_marker_id);
11439 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
11440 release_static_tracepoint_marker (&marker);
11441
11442 return sal;
11443 }
11444
11445 /* Old marker wasn't found on target at lineno. Try looking it up
11446 by string ID. */
11447 if (!sal.explicit_pc
11448 && sal.line != 0
11449 && sal.symtab != NULL
d9b3f62e 11450 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
11451 {
11452 VEC(static_tracepoint_marker_p) *markers;
11453
11454 markers
d9b3f62e 11455 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
11456
11457 if (!VEC_empty(static_tracepoint_marker_p, markers))
11458 {
11459 struct symtab_and_line sal;
11460 struct symbol *sym;
11461 struct static_tracepoint_marker *marker;
11462
11463 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
11464
d9b3f62e
PA
11465 xfree (tp->static_trace_marker_id);
11466 tp->static_trace_marker_id = xstrdup (marker->str_id);
0fb4aa4b
PA
11467
11468 warning (_("marker for static tracepoint %d (%s) not "
11469 "found at previous line number"),
d9b3f62e 11470 b->number, tp->static_trace_marker_id);
0fb4aa4b
PA
11471
11472 init_sal (&sal);
11473
11474 sal.pc = marker->address;
11475
11476 sal = find_pc_line (marker->address, 0);
11477 sym = find_pc_sect_function (marker->address, NULL);
11478 ui_out_text (uiout, "Now in ");
11479 if (sym)
11480 {
11481 ui_out_field_string (uiout, "func",
11482 SYMBOL_PRINT_NAME (sym));
11483 ui_out_text (uiout, " at ");
11484 }
11485 ui_out_field_string (uiout, "file", sal.symtab->filename);
11486 ui_out_text (uiout, ":");
11487
11488 if (ui_out_is_mi_like_p (uiout))
11489 {
11490 char *fullname = symtab_to_fullname (sal.symtab);
11491
11492 if (fullname)
11493 ui_out_field_string (uiout, "fullname", fullname);
11494 }
11495
11496 ui_out_field_int (uiout, "line", sal.line);
11497 ui_out_text (uiout, "\n");
11498
11499 b->line_number = sal.line;
11500
11501 xfree (b->source_file);
11502 if (sym)
11503 b->source_file = xstrdup (sal.symtab->filename);
11504 else
11505 b->source_file = NULL;
11506
11507 xfree (b->addr_string);
11508 b->addr_string = xstrprintf ("%s:%d",
11509 sal.symtab->filename, b->line_number);
11510
11511 /* Might be nice to check if function changed, and warn if
11512 so. */
11513
11514 release_static_tracepoint_marker (marker);
11515 }
11516 }
11517 return sal;
11518}
11519
8d3788bd
VP
11520/* Returns 1 iff locations A and B are sufficiently same that
11521 we don't need to report breakpoint as changed. */
11522
11523static int
11524locations_are_equal (struct bp_location *a, struct bp_location *b)
11525{
11526 while (a && b)
11527 {
11528 if (a->address != b->address)
11529 return 0;
11530
11531 if (a->shlib_disabled != b->shlib_disabled)
11532 return 0;
11533
11534 if (a->enabled != b->enabled)
11535 return 0;
11536
11537 a = a->next;
11538 b = b->next;
11539 }
11540
11541 if ((a == NULL) != (b == NULL))
11542 return 0;
11543
11544 return 1;
11545}
11546
f1310107
TJB
11547/* Create new breakpoint locations for B (a hardware or software breakpoint)
11548 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
11549 a ranged breakpoint. */
11550
0e30163f 11551void
0d381245 11552update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
11553 struct symtabs_and_lines sals,
11554 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
11555{
11556 int i;
0d381245
VP
11557 struct bp_location *existing_locations = b->loc;
11558
f1310107
TJB
11559 /* Ranged breakpoints have only one start location and one end location. */
11560 gdb_assert (sals_end.nelts == 0 || (sals.nelts == 1 && sals_end.nelts == 1));
11561
4a64f543
MS
11562 /* If there's no new locations, and all existing locations are
11563 pending, don't do anything. This optimizes the common case where
11564 all locations are in the same shared library, that was unloaded.
11565 We'd like to retain the location, so that when the library is
11566 loaded again, we don't loose the enabled/disabled status of the
11567 individual locations. */
0d381245 11568 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
11569 return;
11570
fe3f5fa8
VP
11571 b->loc = NULL;
11572
0d381245 11573 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 11574 {
0d381245 11575 struct bp_location *new_loc =
39d61571 11576 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 11577
0d381245
VP
11578 /* Reparse conditions, they might contain references to the
11579 old symtab. */
11580 if (b->cond_string != NULL)
11581 {
f1310107 11582 char *s;
0d381245 11583 struct gdb_exception e;
fe3f5fa8 11584
0d381245
VP
11585 s = b->cond_string;
11586 TRY_CATCH (e, RETURN_MASK_ERROR)
11587 {
11588 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
11589 0);
11590 }
11591 if (e.reason < 0)
11592 {
3e43a32a
MS
11593 warning (_("failed to reevaluate condition "
11594 "for breakpoint %d: %s"),
0d381245
VP
11595 b->number, e.message);
11596 new_loc->enabled = 0;
11597 }
11598 }
fe3f5fa8 11599
0d381245
VP
11600 if (b->source_file != NULL)
11601 xfree (b->source_file);
11602 if (sals.sals[i].symtab == NULL)
11603 b->source_file = NULL;
11604 else
1b36a34b 11605 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 11606
0d381245
VP
11607 if (b->line_number == 0)
11608 b->line_number = sals.sals[i].line;
f1310107
TJB
11609
11610 if (sals_end.nelts)
11611 {
11612 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11613
11614 new_loc->length = end - sals.sals[0].pc + 1;
11615 }
0d381245 11616 }
fe3f5fa8 11617
514f746b
AR
11618 /* Update locations of permanent breakpoints. */
11619 if (b->enable_state == bp_permanent)
11620 make_breakpoint_permanent (b);
11621
4a64f543
MS
11622 /* If possible, carry over 'disable' status from existing
11623 breakpoints. */
0d381245
VP
11624 {
11625 struct bp_location *e = existing_locations;
776592bf
DE
11626 /* If there are multiple breakpoints with the same function name,
11627 e.g. for inline functions, comparing function names won't work.
11628 Instead compare pc addresses; this is just a heuristic as things
11629 may have moved, but in practice it gives the correct answer
11630 often enough until a better solution is found. */
11631 int have_ambiguous_names = ambiguous_names_p (b->loc);
11632
0d381245
VP
11633 for (; e; e = e->next)
11634 {
11635 if (!e->enabled && e->function_name)
11636 {
11637 struct bp_location *l = b->loc;
776592bf
DE
11638 if (have_ambiguous_names)
11639 {
11640 for (; l; l = l->next)
f1310107 11641 if (breakpoint_locations_match (e, l))
776592bf
DE
11642 {
11643 l->enabled = 0;
11644 break;
11645 }
11646 }
11647 else
11648 {
11649 for (; l; l = l->next)
11650 if (l->function_name
11651 && strcmp (e->function_name, l->function_name) == 0)
11652 {
11653 l->enabled = 0;
11654 break;
11655 }
11656 }
0d381245
VP
11657 }
11658 }
11659 }
fe3f5fa8 11660
8d3788bd
VP
11661 if (!locations_are_equal (existing_locations, b->loc))
11662 observer_notify_breakpoint_modified (b);
11663
b60e7edf 11664 update_global_location_list (1);
fe3f5fa8
VP
11665}
11666
ef23e705
TJB
11667/* Find the SaL locations corresponding to the given ADDR_STRING.
11668 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
11669
11670static struct symtabs_and_lines
11671addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11672{
11673 char *s;
58438ac1 11674 int marker_spec;
02d20e4a 11675 struct symtabs_and_lines sals = {0};
ef23e705
TJB
11676 struct gdb_exception e;
11677
11678 s = addr_string;
11679 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11680
11681 TRY_CATCH (e, RETURN_MASK_ERROR)
11682 {
11683 if (marker_spec)
11684 {
d9b3f62e
PA
11685 struct tracepoint *tp = (struct tracepoint *) b;
11686
ef23e705 11687 sals = decode_static_tracepoint_spec (&s);
d9b3f62e 11688 if (sals.nelts > tp->static_trace_marker_id_idx)
ef23e705 11689 {
d9b3f62e 11690 sals.sals[0] = sals.sals[tp->static_trace_marker_id_idx];
ef23e705
TJB
11691 sals.nelts = 1;
11692 }
11693 else
d9b3f62e 11694 error (_("marker %s not found"), tp->static_trace_marker_id);
ef23e705
TJB
11695 }
11696 else
58438ac1 11697 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, NULL);
ef23e705
TJB
11698 }
11699 if (e.reason < 0)
11700 {
11701 int not_found_and_ok = 0;
11702 /* For pending breakpoints, it's expected that parsing will
11703 fail until the right shared library is loaded. User has
11704 already told to create pending breakpoints and don't need
11705 extra messages. If breakpoint is in bp_shlib_disabled
11706 state, then user already saw the message about that
11707 breakpoint being disabled, and don't want to see more
11708 errors. */
58438ac1 11709 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
11710 && (b->condition_not_parsed
11711 || (b->loc && b->loc->shlib_disabled)
11712 || b->enable_state == bp_disabled))
11713 not_found_and_ok = 1;
11714
11715 if (!not_found_and_ok)
11716 {
11717 /* We surely don't want to warn about the same breakpoint
11718 10 times. One solution, implemented here, is disable
11719 the breakpoint on error. Another solution would be to
11720 have separate 'warning emitted' flag. Since this
11721 happens only when a binary has changed, I don't know
11722 which approach is better. */
11723 b->enable_state = bp_disabled;
11724 throw_exception (e);
11725 }
11726 }
11727
58438ac1 11728 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705
TJB
11729 {
11730 gdb_assert (sals.nelts == 1);
11731
11732 resolve_sal_pc (&sals.sals[0]);
11733 if (b->condition_not_parsed && s && s[0])
11734 {
11735 char *cond_string = 0;
11736 int thread = -1;
11737 int task = 0;
11738
11739 find_condition_and_thread (s, sals.sals[0].pc,
11740 &cond_string, &thread, &task);
11741 if (cond_string)
11742 b->cond_string = cond_string;
11743 b->thread = thread;
11744 b->task = task;
11745 b->condition_not_parsed = 0;
11746 }
11747
11748 if (b->type == bp_static_tracepoint && !marker_spec)
11749 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 11750
58438ac1
TT
11751 *found = 1;
11752 }
11753 else
11754 *found = 0;
ef23e705
TJB
11755
11756 return sals;
11757}
11758
348d480f
PA
11759/* The default re_set method, for typical hardware or software
11760 breakpoints. Reevaluate the breakpoint and recreate its
11761 locations. */
11762
11763static void
28010a5d 11764breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
11765{
11766 int found;
f1310107 11767 struct symtabs_and_lines sals, sals_end;
ef23e705 11768 struct symtabs_and_lines expanded = {0};
f1310107 11769 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
11770
11771 sals = addr_string_to_sals (b, b->addr_string, &found);
11772 if (found)
11773 {
11774 make_cleanup (xfree, sals.sals);
11775 expanded = expand_line_sal_maybe (sals.sals[0]);
11776 }
11777
f1310107
TJB
11778 if (b->addr_string_range_end)
11779 {
11780 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11781 if (found)
11782 {
11783 make_cleanup (xfree, sals_end.sals);
11784 expanded_end = expand_line_sal_maybe (sals_end.sals[0]);
11785 }
11786 }
11787
11788 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
11789}
11790
11791/* Prepare the global context for a re-set of breakpoint B. */
11792
11793static struct cleanup *
11794prepare_re_set_context (struct breakpoint *b)
11795{
11796 struct cleanup *cleanups;
11797
11798 input_radix = b->input_radix;
11799 cleanups = save_current_space_and_thread ();
11800 switch_to_program_space_and_thread (b->pspace);
11801 set_language (b->language);
11802
11803 return cleanups;
ef23e705
TJB
11804}
11805
c906108c
SS
11806/* Reset a breakpoint given it's struct breakpoint * BINT.
11807 The value we return ends up being the return value from catch_errors.
11808 Unused in this case. */
11809
11810static int
4efb68b1 11811breakpoint_re_set_one (void *bint)
c906108c 11812{
4a64f543 11813 /* Get past catch_errs. */
53a5351d 11814 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 11815 struct cleanup *cleanups;
c906108c 11816
348d480f
PA
11817 cleanups = prepare_re_set_context (b);
11818 b->ops->re_set (b);
11819 do_cleanups (cleanups);
c906108c
SS
11820 return 0;
11821}
11822
69de3c6a 11823/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 11824void
69de3c6a 11825breakpoint_re_set (void)
c906108c 11826{
35df4500 11827 struct breakpoint *b, *b_tmp;
c906108c
SS
11828 enum language save_language;
11829 int save_input_radix;
6c95b8df 11830 struct cleanup *old_chain;
c5aa993b 11831
c906108c
SS
11832 save_language = current_language->la_language;
11833 save_input_radix = input_radix;
6c95b8df
PA
11834 old_chain = save_current_program_space ();
11835
35df4500 11836 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11837 {
4a64f543 11838 /* Format possible error msg. */
fe3f5fa8 11839 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
11840 b->number);
11841 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 11842 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 11843 do_cleanups (cleanups);
c5aa993b 11844 }
c906108c
SS
11845 set_language (save_language);
11846 input_radix = save_input_radix;
e62c965a 11847
0756c555 11848 jit_breakpoint_re_set ();
4efc6507 11849
6c95b8df
PA
11850 do_cleanups (old_chain);
11851
af02033e
PP
11852 create_overlay_event_breakpoint ();
11853 create_longjmp_master_breakpoint ();
11854 create_std_terminate_master_breakpoint ();
186c406b 11855 create_exception_master_breakpoint ();
c906108c
SS
11856}
11857\f
c906108c
SS
11858/* Reset the thread number of this breakpoint:
11859
11860 - If the breakpoint is for all threads, leave it as-is.
4a64f543 11861 - Else, reset it to the current thread for inferior_ptid. */
c906108c 11862void
fba45db2 11863breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
11864{
11865 if (b->thread != -1)
11866 {
39f77062
KB
11867 if (in_thread_list (inferior_ptid))
11868 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
11869
11870 /* We're being called after following a fork. The new fork is
11871 selected as current, and unless this was a vfork will have a
11872 different program space from the original thread. Reset that
11873 as well. */
11874 b->loc->pspace = current_program_space;
c906108c
SS
11875 }
11876}
11877
03ac34d5
MS
11878/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11879 If from_tty is nonzero, it prints a message to that effect,
11880 which ends with a period (no newline). */
11881
c906108c 11882void
fba45db2 11883set_ignore_count (int bptnum, int count, int from_tty)
c906108c 11884{
52f0bd74 11885 struct breakpoint *b;
c906108c
SS
11886
11887 if (count < 0)
11888 count = 0;
11889
11890 ALL_BREAKPOINTS (b)
11891 if (b->number == bptnum)
c5aa993b 11892 {
d77f58be
SS
11893 if (is_tracepoint (b))
11894 {
11895 if (from_tty && count != 0)
11896 printf_filtered (_("Ignore count ignored for tracepoint %d."),
11897 bptnum);
11898 return;
11899 }
11900
c5aa993b 11901 b->ignore_count = count;
221ea385
KS
11902 if (from_tty)
11903 {
11904 if (count == 0)
3e43a32a
MS
11905 printf_filtered (_("Will stop next time "
11906 "breakpoint %d is reached."),
221ea385
KS
11907 bptnum);
11908 else if (count == 1)
a3f17187 11909 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
11910 bptnum);
11911 else
3e43a32a
MS
11912 printf_filtered (_("Will ignore next %d "
11913 "crossings of breakpoint %d."),
221ea385
KS
11914 count, bptnum);
11915 }
c5aa993b 11916 breakpoints_changed ();
8d3788bd 11917 observer_notify_breakpoint_modified (b);
c5aa993b
JM
11918 return;
11919 }
c906108c 11920
8a3fe4f8 11921 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
11922}
11923
c906108c
SS
11924/* Command to set ignore-count of breakpoint N to COUNT. */
11925
11926static void
fba45db2 11927ignore_command (char *args, int from_tty)
c906108c
SS
11928{
11929 char *p = args;
52f0bd74 11930 int num;
c906108c
SS
11931
11932 if (p == 0)
e2e0b3e5 11933 error_no_arg (_("a breakpoint number"));
c5aa993b 11934
c906108c 11935 num = get_number (&p);
5c44784c 11936 if (num == 0)
8a3fe4f8 11937 error (_("bad breakpoint number: '%s'"), args);
c906108c 11938 if (*p == 0)
8a3fe4f8 11939 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
11940
11941 set_ignore_count (num,
11942 longest_to_int (value_as_long (parse_and_eval (p))),
11943 from_tty);
221ea385
KS
11944 if (from_tty)
11945 printf_filtered ("\n");
c906108c
SS
11946}
11947\f
11948/* Call FUNCTION on each of the breakpoints
11949 whose numbers are given in ARGS. */
11950
11951static void
95a42b64
TT
11952map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
11953 void *),
11954 void *data)
c906108c 11955{
52f0bd74
AC
11956 int num;
11957 struct breakpoint *b, *tmp;
11cf8741 11958 int match;
197f0a60 11959 struct get_number_or_range_state state;
c906108c 11960
197f0a60 11961 if (args == 0)
e2e0b3e5 11962 error_no_arg (_("one or more breakpoint numbers"));
c906108c 11963
197f0a60
TT
11964 init_number_or_range (&state, args);
11965
11966 while (!state.finished)
c906108c 11967 {
197f0a60
TT
11968 char *p = state.string;
11969
11cf8741 11970 match = 0;
c5aa993b 11971
197f0a60 11972 num = get_number_or_range (&state);
5c44784c 11973 if (num == 0)
c5aa993b 11974 {
8a3fe4f8 11975 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
11976 }
11977 else
11978 {
11979 ALL_BREAKPOINTS_SAFE (b, tmp)
11980 if (b->number == num)
11981 {
11cf8741 11982 match = 1;
cdac0397 11983 function (b, data);
11cf8741 11984 break;
5c44784c 11985 }
11cf8741 11986 if (match == 0)
a3f17187 11987 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 11988 }
c906108c
SS
11989 }
11990}
11991
0d381245
VP
11992static struct bp_location *
11993find_location_by_number (char *number)
11994{
11995 char *dot = strchr (number, '.');
11996 char *p1;
11997 int bp_num;
11998 int loc_num;
11999 struct breakpoint *b;
12000 struct bp_location *loc;
12001
12002 *dot = '\0';
12003
12004 p1 = number;
197f0a60 12005 bp_num = get_number (&p1);
0d381245
VP
12006 if (bp_num == 0)
12007 error (_("Bad breakpoint number '%s'"), number);
12008
12009 ALL_BREAKPOINTS (b)
12010 if (b->number == bp_num)
12011 {
12012 break;
12013 }
12014
12015 if (!b || b->number != bp_num)
12016 error (_("Bad breakpoint number '%s'"), number);
12017
12018 p1 = dot+1;
197f0a60 12019 loc_num = get_number (&p1);
0d381245
VP
12020 if (loc_num == 0)
12021 error (_("Bad breakpoint location number '%s'"), number);
12022
12023 --loc_num;
12024 loc = b->loc;
12025 for (;loc_num && loc; --loc_num, loc = loc->next)
12026 ;
12027 if (!loc)
12028 error (_("Bad breakpoint location number '%s'"), dot+1);
12029
12030 return loc;
12031}
12032
12033
1900040c
MS
12034/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12035 If from_tty is nonzero, it prints a message to that effect,
12036 which ends with a period (no newline). */
12037
c906108c 12038void
fba45db2 12039disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
12040{
12041 /* Never disable a watchpoint scope breakpoint; we want to
12042 hit them when we leave scope so we can delete both the
12043 watchpoint and its scope breakpoint at that time. */
12044 if (bpt->type == bp_watchpoint_scope)
12045 return;
12046
c2c6d25f 12047 /* You can't disable permanent breakpoints. */
b5de0fa7 12048 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
12049 return;
12050
b5de0fa7 12051 bpt->enable_state = bp_disabled;
c906108c 12052
d248b706
KY
12053 if (target_supports_enable_disable_tracepoint ()
12054 && current_trace_status ()->running && is_tracepoint (bpt))
12055 {
12056 struct bp_location *location;
12057
12058 for (location = bpt->loc; location; location = location->next)
12059 target_disable_tracepoint (location);
12060 }
12061
b60e7edf 12062 update_global_location_list (0);
c906108c 12063
8d3788bd 12064 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12065}
12066
51be5b68
PA
12067/* A callback for iterate_over_related_breakpoints. */
12068
12069static void
12070do_disable_breakpoint (struct breakpoint *b, void *ignore)
12071{
12072 disable_breakpoint (b);
12073}
12074
95a42b64
TT
12075/* A callback for map_breakpoint_numbers that calls
12076 disable_breakpoint. */
12077
12078static void
12079do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
12080{
51be5b68 12081 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
12082}
12083
c906108c 12084static void
fba45db2 12085disable_command (char *args, int from_tty)
c906108c 12086{
c906108c 12087 if (args == 0)
46c6471b
PA
12088 {
12089 struct breakpoint *bpt;
12090
12091 ALL_BREAKPOINTS (bpt)
12092 if (user_breakpoint_p (bpt))
12093 disable_breakpoint (bpt);
12094 }
0d381245
VP
12095 else if (strchr (args, '.'))
12096 {
12097 struct bp_location *loc = find_location_by_number (args);
12098 if (loc)
d248b706
KY
12099 {
12100 loc->enabled = 0;
12101 if (target_supports_enable_disable_tracepoint ()
12102 && current_trace_status ()->running && loc->owner
12103 && is_tracepoint (loc->owner))
12104 target_disable_tracepoint (loc);
12105 }
b60e7edf 12106 update_global_location_list (0);
0d381245 12107 }
c906108c 12108 else
95a42b64 12109 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
12110}
12111
12112static void
51be5b68 12113enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 12114{
afe38095 12115 int target_resources_ok;
c906108c
SS
12116
12117 if (bpt->type == bp_hardware_breakpoint)
12118 {
12119 int i;
c5aa993b 12120 i = hw_breakpoint_used_count ();
53a5351d 12121 target_resources_ok =
d92524f1 12122 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 12123 i + 1, 0);
c906108c 12124 if (target_resources_ok == 0)
8a3fe4f8 12125 error (_("No hardware breakpoint support in the target."));
c906108c 12126 else if (target_resources_ok < 0)
8a3fe4f8 12127 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
12128 }
12129
cc60f2e3 12130 if (is_watchpoint (bpt))
c906108c 12131 {
d07205c2
JK
12132 /* Initialize it just to avoid a GCC false warning. */
12133 enum enable_state orig_enable_state = 0;
dde02812
ES
12134 struct gdb_exception e;
12135
12136 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 12137 {
3a5c3e22
PA
12138 struct watchpoint *w = (struct watchpoint *) bpt;
12139
1e718ff1
TJB
12140 orig_enable_state = bpt->enable_state;
12141 bpt->enable_state = bp_enabled;
3a5c3e22 12142 update_watchpoint (w, 1 /* reparse */);
c906108c 12143 }
dde02812 12144 if (e.reason < 0)
c5aa993b 12145 {
1e718ff1 12146 bpt->enable_state = orig_enable_state;
dde02812
ES
12147 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12148 bpt->number);
12149 return;
c5aa993b 12150 }
c906108c 12151 }
0101ce28 12152
b4c291bb
KH
12153 if (bpt->enable_state != bp_permanent)
12154 bpt->enable_state = bp_enabled;
d248b706
KY
12155
12156 if (target_supports_enable_disable_tracepoint ()
12157 && current_trace_status ()->running && is_tracepoint (bpt))
12158 {
12159 struct bp_location *location;
12160
12161 for (location = bpt->loc; location; location = location->next)
12162 target_enable_tracepoint (location);
12163 }
12164
b4c291bb 12165 bpt->disposition = disposition;
b60e7edf 12166 update_global_location_list (1);
b4c291bb
KH
12167 breakpoints_changed ();
12168
8d3788bd 12169 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12170}
12171
fe3f5fa8 12172
c906108c 12173void
fba45db2 12174enable_breakpoint (struct breakpoint *bpt)
c906108c 12175{
51be5b68
PA
12176 enable_breakpoint_disp (bpt, bpt->disposition);
12177}
12178
12179static void
12180do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12181{
12182 enable_breakpoint (bpt);
c906108c
SS
12183}
12184
95a42b64
TT
12185/* A callback for map_breakpoint_numbers that calls
12186 enable_breakpoint. */
12187
12188static void
12189do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12190{
51be5b68 12191 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
12192}
12193
c906108c
SS
12194/* The enable command enables the specified breakpoints (or all defined
12195 breakpoints) so they once again become (or continue to be) effective
1272ad14 12196 in stopping the inferior. */
c906108c 12197
c906108c 12198static void
fba45db2 12199enable_command (char *args, int from_tty)
c906108c 12200{
c906108c 12201 if (args == 0)
46c6471b
PA
12202 {
12203 struct breakpoint *bpt;
12204
12205 ALL_BREAKPOINTS (bpt)
12206 if (user_breakpoint_p (bpt))
12207 enable_breakpoint (bpt);
12208 }
0d381245
VP
12209 else if (strchr (args, '.'))
12210 {
12211 struct bp_location *loc = find_location_by_number (args);
12212 if (loc)
d248b706
KY
12213 {
12214 loc->enabled = 1;
12215 if (target_supports_enable_disable_tracepoint ()
12216 && current_trace_status ()->running && loc->owner
12217 && is_tracepoint (loc->owner))
12218 target_enable_tracepoint (loc);
12219 }
b60e7edf 12220 update_global_location_list (1);
0d381245 12221 }
c906108c 12222 else
95a42b64 12223 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
12224}
12225
12226static void
51be5b68
PA
12227do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12228{
12229 enum bpdisp disp = *(enum bpdisp *) arg;
12230
12231 enable_breakpoint_disp (bpt, disp);
12232}
12233
12234static void
12235do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12236{
51be5b68
PA
12237 enum bpdisp disp = disp_disable;
12238
12239 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12240}
12241
c906108c 12242static void
fba45db2 12243enable_once_command (char *args, int from_tty)
c906108c 12244{
51be5b68 12245 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
12246}
12247
12248static void
51be5b68 12249do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12250{
51be5b68
PA
12251 enum bpdisp disp = disp_del;
12252
12253 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12254}
12255
c906108c 12256static void
fba45db2 12257enable_delete_command (char *args, int from_tty)
c906108c 12258{
51be5b68 12259 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
12260}
12261\f
fa8d40ab
JJ
12262static void
12263set_breakpoint_cmd (char *args, int from_tty)
12264{
12265}
12266
12267static void
12268show_breakpoint_cmd (char *args, int from_tty)
12269{
12270}
12271
1f3b5d1b
PP
12272/* Invalidate last known value of any hardware watchpoint if
12273 the memory which that value represents has been written to by
12274 GDB itself. */
12275
12276static void
12277invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12278 const bfd_byte *data)
12279{
12280 struct breakpoint *bp;
12281
12282 ALL_BREAKPOINTS (bp)
12283 if (bp->enable_state == bp_enabled
3a5c3e22 12284 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 12285 {
3a5c3e22 12286 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 12287
3a5c3e22
PA
12288 if (wp->val_valid && wp->val)
12289 {
12290 struct bp_location *loc;
12291
12292 for (loc = bp->loc; loc != NULL; loc = loc->next)
12293 if (loc->loc_type == bp_loc_hardware_watchpoint
12294 && loc->address + loc->length > addr
12295 && addr + len > loc->address)
12296 {
12297 value_free (wp->val);
12298 wp->val = NULL;
12299 wp->val_valid = 0;
12300 }
12301 }
1f3b5d1b
PP
12302 }
12303}
12304
c906108c
SS
12305/* Use default_breakpoint_'s, or nothing if they aren't valid. */
12306
12307struct symtabs_and_lines
fba45db2 12308decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
12309{
12310 struct symtabs_and_lines sals;
cc59ec59 12311
c906108c 12312 if (string == 0)
8a3fe4f8 12313 error (_("Empty line specification."));
c906108c
SS
12314 if (default_breakpoint_valid)
12315 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
12316 default_breakpoint_symtab,
12317 default_breakpoint_line,
58438ac1 12318 NULL);
c906108c
SS
12319 else
12320 sals = decode_line_1 (&string, funfirstline,
58438ac1 12321 (struct symtab *) NULL, 0, NULL);
c906108c 12322 if (*string)
8a3fe4f8 12323 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
12324 return sals;
12325}
8181d85f
DJ
12326
12327/* Create and insert a raw software breakpoint at PC. Return an
12328 identifier, which should be used to remove the breakpoint later.
12329 In general, places which call this should be using something on the
12330 breakpoint chain instead; this function should be eliminated
12331 someday. */
12332
12333void *
6c95b8df
PA
12334deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12335 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
12336{
12337 struct bp_target_info *bp_tgt;
12338
6c95b8df 12339 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 12340
6c95b8df 12341 bp_tgt->placed_address_space = aspace;
8181d85f 12342 bp_tgt->placed_address = pc;
6c95b8df 12343
a6d9a66e 12344 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
12345 {
12346 /* Could not insert the breakpoint. */
12347 xfree (bp_tgt);
12348 return NULL;
12349 }
12350
12351 return bp_tgt;
12352}
12353
4a64f543
MS
12354/* Remove a breakpoint BP inserted by
12355 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
12356
12357int
a6d9a66e 12358deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
12359{
12360 struct bp_target_info *bp_tgt = bp;
12361 int ret;
12362
a6d9a66e 12363 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
12364 xfree (bp_tgt);
12365
12366 return ret;
12367}
12368
4a64f543
MS
12369/* One (or perhaps two) breakpoints used for software single
12370 stepping. */
8181d85f
DJ
12371
12372static void *single_step_breakpoints[2];
a6d9a66e 12373static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
12374
12375/* Create and insert a breakpoint for software single step. */
12376
12377void
6c95b8df 12378insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
12379 struct address_space *aspace,
12380 CORE_ADDR next_pc)
8181d85f
DJ
12381{
12382 void **bpt_p;
12383
12384 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
12385 {
12386 bpt_p = &single_step_breakpoints[0];
12387 single_step_gdbarch[0] = gdbarch;
12388 }
8181d85f
DJ
12389 else
12390 {
12391 gdb_assert (single_step_breakpoints[1] == NULL);
12392 bpt_p = &single_step_breakpoints[1];
a6d9a66e 12393 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
12394 }
12395
4a64f543
MS
12396 /* NOTE drow/2006-04-11: A future improvement to this function would
12397 be to only create the breakpoints once, and actually put them on
12398 the breakpoint chain. That would let us use set_raw_breakpoint.
12399 We could adjust the addresses each time they were needed. Doing
12400 this requires corresponding changes elsewhere where single step
12401 breakpoints are handled, however. So, for now, we use this. */
8181d85f 12402
6c95b8df 12403 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 12404 if (*bpt_p == NULL)
5af949e3
UW
12405 error (_("Could not insert single-step breakpoint at %s"),
12406 paddress (gdbarch, next_pc));
8181d85f
DJ
12407}
12408
f02253f1
HZ
12409/* Check if the breakpoints used for software single stepping
12410 were inserted or not. */
12411
12412int
12413single_step_breakpoints_inserted (void)
12414{
12415 return (single_step_breakpoints[0] != NULL
12416 || single_step_breakpoints[1] != NULL);
12417}
12418
8181d85f
DJ
12419/* Remove and delete any breakpoints used for software single step. */
12420
12421void
12422remove_single_step_breakpoints (void)
12423{
12424 gdb_assert (single_step_breakpoints[0] != NULL);
12425
12426 /* See insert_single_step_breakpoint for more about this deprecated
12427 call. */
a6d9a66e
UW
12428 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12429 single_step_breakpoints[0]);
12430 single_step_gdbarch[0] = NULL;
8181d85f
DJ
12431 single_step_breakpoints[0] = NULL;
12432
12433 if (single_step_breakpoints[1] != NULL)
12434 {
a6d9a66e
UW
12435 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12436 single_step_breakpoints[1]);
12437 single_step_gdbarch[1] = NULL;
8181d85f
DJ
12438 single_step_breakpoints[1] = NULL;
12439 }
12440}
12441
d03285ec
UW
12442/* Delete software single step breakpoints without removing them from
12443 the inferior. This is intended to be used if the inferior's address
12444 space where they were inserted is already gone, e.g. after exit or
12445 exec. */
12446
12447void
12448cancel_single_step_breakpoints (void)
12449{
12450 int i;
12451
12452 for (i = 0; i < 2; i++)
12453 if (single_step_breakpoints[i])
12454 {
12455 xfree (single_step_breakpoints[i]);
12456 single_step_breakpoints[i] = NULL;
12457 single_step_gdbarch[i] = NULL;
12458 }
12459}
12460
12461/* Detach software single-step breakpoints from INFERIOR_PTID without
12462 removing them. */
12463
12464static void
12465detach_single_step_breakpoints (void)
12466{
12467 int i;
12468
12469 for (i = 0; i < 2; i++)
12470 if (single_step_breakpoints[i])
12471 target_remove_breakpoint (single_step_gdbarch[i],
12472 single_step_breakpoints[i]);
12473}
12474
4a64f543
MS
12475/* Check whether a software single-step breakpoint is inserted at
12476 PC. */
1aafd4da
UW
12477
12478static int
cc59ec59
MS
12479single_step_breakpoint_inserted_here_p (struct address_space *aspace,
12480 CORE_ADDR pc)
1aafd4da
UW
12481{
12482 int i;
12483
12484 for (i = 0; i < 2; i++)
12485 {
12486 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
12487 if (bp_tgt
12488 && breakpoint_address_match (bp_tgt->placed_address_space,
12489 bp_tgt->placed_address,
12490 aspace, pc))
1aafd4da
UW
12491 return 1;
12492 }
12493
12494 return 0;
12495}
12496
a96d9b2e
SDJ
12497/* Returns 0 if 'bp' is NOT a syscall catchpoint,
12498 non-zero otherwise. */
12499static int
12500is_syscall_catchpoint_enabled (struct breakpoint *bp)
12501{
12502 if (syscall_catchpoint_p (bp)
12503 && bp->enable_state != bp_disabled
12504 && bp->enable_state != bp_call_disabled)
12505 return 1;
12506 else
12507 return 0;
12508}
12509
12510int
12511catch_syscall_enabled (void)
12512{
12513 struct inferior *inf = current_inferior ();
12514
12515 return inf->total_syscalls_count != 0;
12516}
12517
12518int
12519catching_syscall_number (int syscall_number)
12520{
12521 struct breakpoint *bp;
12522
12523 ALL_BREAKPOINTS (bp)
12524 if (is_syscall_catchpoint_enabled (bp))
12525 {
be5c67c1
PA
12526 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12527
12528 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
12529 {
12530 int i, iter;
12531 for (i = 0;
be5c67c1 12532 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
12533 i++)
12534 if (syscall_number == iter)
12535 return 1;
12536 }
12537 else
12538 return 1;
12539 }
12540
12541 return 0;
12542}
12543
12544/* Complete syscall names. Used by "catch syscall". */
12545static char **
12546catch_syscall_completer (struct cmd_list_element *cmd,
12547 char *text, char *word)
12548{
12549 const char **list = get_syscall_names ();
c38eea1a
MS
12550 char **retlist
12551 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 12552
c38eea1a
MS
12553 xfree (list);
12554 return retlist;
a96d9b2e
SDJ
12555}
12556
1042e4c0
SS
12557/* Tracepoint-specific operations. */
12558
12559/* Set tracepoint count to NUM. */
12560static void
12561set_tracepoint_count (int num)
12562{
12563 tracepoint_count = num;
4fa62494 12564 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
12565}
12566
12567void
12568trace_command (char *arg, int from_tty)
12569{
8cdf0e15
VP
12570 if (create_breakpoint (get_current_arch (),
12571 arg,
12572 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12573 0 /* tempflag */,
12574 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
12575 0 /* Ignore count */,
12576 pending_break_support,
348d480f 12577 &tracepoint_breakpoint_ops,
8cdf0e15 12578 from_tty,
84f4c1fe
PM
12579 1 /* enabled */,
12580 0 /* internal */))
fd9b8c24 12581 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
12582}
12583
7a697b8d
SS
12584void
12585ftrace_command (char *arg, int from_tty)
12586{
8cdf0e15
VP
12587 if (create_breakpoint (get_current_arch (),
12588 arg,
12589 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12590 0 /* tempflag */,
12591 bp_fast_tracepoint /* type_wanted */,
12592 0 /* Ignore count */,
12593 pending_break_support,
348d480f 12594 &tracepoint_breakpoint_ops,
0fb4aa4b 12595 from_tty,
84f4c1fe
PM
12596 1 /* enabled */,
12597 0 /* internal */))
0fb4aa4b
PA
12598 set_tracepoint_count (breakpoint_count);
12599}
12600
12601/* strace command implementation. Creates a static tracepoint. */
12602
12603void
12604strace_command (char *arg, int from_tty)
12605{
12606 if (create_breakpoint (get_current_arch (),
12607 arg,
12608 NULL, 0, 1 /* parse arg */,
12609 0 /* tempflag */,
12610 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
12611 0 /* Ignore count */,
12612 pending_break_support,
348d480f 12613 &tracepoint_breakpoint_ops,
8cdf0e15 12614 from_tty,
84f4c1fe
PM
12615 1 /* enabled */,
12616 0 /* internal */))
fd9b8c24 12617 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
12618}
12619
409873ef
SS
12620/* Set up a fake reader function that gets command lines from a linked
12621 list that was acquired during tracepoint uploading. */
12622
12623static struct uploaded_tp *this_utp;
3149d8c1 12624static int next_cmd;
409873ef
SS
12625
12626static char *
12627read_uploaded_action (void)
12628{
12629 char *rslt;
12630
3149d8c1 12631 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 12632
3149d8c1 12633 next_cmd++;
409873ef
SS
12634
12635 return rslt;
12636}
12637
00bf0b85
SS
12638/* Given information about a tracepoint as recorded on a target (which
12639 can be either a live system or a trace file), attempt to create an
12640 equivalent GDB tracepoint. This is not a reliable process, since
12641 the target does not necessarily have all the information used when
12642 the tracepoint was originally defined. */
12643
d9b3f62e 12644struct tracepoint *
00bf0b85 12645create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 12646{
409873ef 12647 char *addr_str, small_buf[100];
d9b3f62e 12648 struct tracepoint *tp;
fd9b8c24 12649
409873ef
SS
12650 if (utp->at_string)
12651 addr_str = utp->at_string;
12652 else
12653 {
12654 /* In the absence of a source location, fall back to raw
12655 address. Since there is no way to confirm that the address
12656 means the same thing as when the trace was started, warn the
12657 user. */
3e43a32a
MS
12658 warning (_("Uploaded tracepoint %d has no "
12659 "source location, using raw address"),
409873ef
SS
12660 utp->number);
12661 sprintf (small_buf, "*%s", hex_string (utp->addr));
12662 addr_str = small_buf;
12663 }
12664
12665 /* There's not much we can do with a sequence of bytecodes. */
12666 if (utp->cond && !utp->cond_string)
3e43a32a
MS
12667 warning (_("Uploaded tracepoint %d condition "
12668 "has no source form, ignoring it"),
409873ef 12669 utp->number);
d5551862 12670
8cdf0e15 12671 if (!create_breakpoint (get_current_arch (),
409873ef
SS
12672 addr_str,
12673 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 12674 0 /* tempflag */,
0fb4aa4b 12675 utp->type /* type_wanted */,
8cdf0e15
VP
12676 0 /* Ignore count */,
12677 pending_break_support,
348d480f 12678 &tracepoint_breakpoint_ops,
8cdf0e15 12679 0 /* from_tty */,
84f4c1fe
PM
12680 utp->enabled /* enabled */,
12681 0 /* internal */))
fd9b8c24
PA
12682 return NULL;
12683
00bf0b85
SS
12684 set_tracepoint_count (breakpoint_count);
12685
409873ef 12686 /* Get the tracepoint we just created. */
fd9b8c24
PA
12687 tp = get_tracepoint (tracepoint_count);
12688 gdb_assert (tp != NULL);
d5551862 12689
00bf0b85
SS
12690 if (utp->pass > 0)
12691 {
d9b3f62e 12692 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 12693
409873ef 12694 trace_pass_command (small_buf, 0);
00bf0b85
SS
12695 }
12696
409873ef
SS
12697 /* If we have uploaded versions of the original commands, set up a
12698 special-purpose "reader" function and call the usual command line
12699 reader, then pass the result to the breakpoint command-setting
12700 function. */
3149d8c1 12701 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 12702 {
409873ef 12703 struct command_line *cmd_list;
00bf0b85 12704
409873ef 12705 this_utp = utp;
3149d8c1 12706 next_cmd = 0;
d5551862 12707
409873ef
SS
12708 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12709
d9b3f62e 12710 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 12711 }
3149d8c1
SS
12712 else if (!VEC_empty (char_ptr, utp->actions)
12713 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12714 warning (_("Uploaded tracepoint %d actions "
12715 "have no source form, ignoring them"),
409873ef 12716 utp->number);
00bf0b85
SS
12717
12718 return tp;
d9b3f62e 12719}
00bf0b85 12720
1042e4c0
SS
12721/* Print information on tracepoint number TPNUM_EXP, or all if
12722 omitted. */
12723
12724static void
e5a67952 12725tracepoints_info (char *args, int from_tty)
1042e4c0 12726{
e5a67952 12727 int num_printed;
1042e4c0 12728
e5a67952 12729 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
12730
12731 if (num_printed == 0)
1042e4c0 12732 {
e5a67952 12733 if (args == NULL || *args == '\0')
d77f58be
SS
12734 ui_out_message (uiout, 0, "No tracepoints.\n");
12735 else
e5a67952 12736 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 12737 }
ad443146
SS
12738
12739 default_collect_info ();
1042e4c0
SS
12740}
12741
4a64f543 12742/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
12743 Not supported by all targets. */
12744static void
12745enable_trace_command (char *args, int from_tty)
12746{
12747 enable_command (args, from_tty);
12748}
12749
4a64f543 12750/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
12751 Not supported by all targets. */
12752static void
12753disable_trace_command (char *args, int from_tty)
12754{
12755 disable_command (args, from_tty);
12756}
12757
4a64f543 12758/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
12759static void
12760delete_trace_command (char *arg, int from_tty)
12761{
35df4500 12762 struct breakpoint *b, *b_tmp;
1042e4c0
SS
12763
12764 dont_repeat ();
12765
12766 if (arg == 0)
12767 {
12768 int breaks_to_delete = 0;
12769
12770 /* Delete all breakpoints if no argument.
12771 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
12772 have to be deleted with an explicit breakpoint number
12773 argument. */
1042e4c0 12774 ALL_TRACEPOINTS (b)
46c6471b 12775 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
12776 {
12777 breaks_to_delete = 1;
12778 break;
12779 }
1042e4c0
SS
12780
12781 /* Ask user only if there are some breakpoints to delete. */
12782 if (!from_tty
12783 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12784 {
35df4500 12785 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 12786 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 12787 delete_breakpoint (b);
1042e4c0
SS
12788 }
12789 }
12790 else
51be5b68 12791 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
12792}
12793
197f0a60
TT
12794/* Helper function for trace_pass_command. */
12795
12796static void
d9b3f62e 12797trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 12798{
d9b3f62e
PA
12799 tp->pass_count = count;
12800 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
12801 if (from_tty)
12802 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 12803 tp->base.number, count);
197f0a60
TT
12804}
12805
1042e4c0
SS
12806/* Set passcount for tracepoint.
12807
12808 First command argument is passcount, second is tracepoint number.
12809 If tracepoint number omitted, apply to most recently defined.
12810 Also accepts special argument "all". */
12811
12812static void
12813trace_pass_command (char *args, int from_tty)
12814{
d9b3f62e 12815 struct tracepoint *t1;
1042e4c0 12816 unsigned int count;
1042e4c0
SS
12817
12818 if (args == 0 || *args == 0)
3e43a32a
MS
12819 error (_("passcount command requires an "
12820 "argument (count + optional TP num)"));
1042e4c0 12821
4a64f543 12822 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
12823
12824 while (*args && isspace ((int) *args))
12825 args++;
12826
12827 if (*args && strncasecmp (args, "all", 3) == 0)
12828 {
d9b3f62e
PA
12829 struct breakpoint *b;
12830
1042e4c0 12831 args += 3; /* Skip special argument "all". */
1042e4c0
SS
12832 if (*args)
12833 error (_("Junk at end of arguments."));
1042e4c0 12834
d9b3f62e 12835 ALL_TRACEPOINTS (b)
197f0a60 12836 {
d9b3f62e 12837 t1 = (struct tracepoint *) b;
197f0a60
TT
12838 trace_pass_set_count (t1, count, from_tty);
12839 }
12840 }
12841 else if (*args == '\0')
1042e4c0 12842 {
197f0a60 12843 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 12844 if (t1)
197f0a60
TT
12845 trace_pass_set_count (t1, count, from_tty);
12846 }
12847 else
12848 {
12849 struct get_number_or_range_state state;
12850
12851 init_number_or_range (&state, args);
12852 while (!state.finished)
1042e4c0 12853 {
197f0a60
TT
12854 t1 = get_tracepoint_by_number (&args, &state, 1);
12855 if (t1)
12856 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
12857 }
12858 }
1042e4c0
SS
12859}
12860
d9b3f62e 12861struct tracepoint *
1042e4c0
SS
12862get_tracepoint (int num)
12863{
12864 struct breakpoint *t;
12865
12866 ALL_TRACEPOINTS (t)
12867 if (t->number == num)
d9b3f62e 12868 return (struct tracepoint *) t;
1042e4c0
SS
12869
12870 return NULL;
12871}
12872
d5551862
SS
12873/* Find the tracepoint with the given target-side number (which may be
12874 different from the tracepoint number after disconnecting and
12875 reconnecting). */
12876
d9b3f62e 12877struct tracepoint *
d5551862
SS
12878get_tracepoint_by_number_on_target (int num)
12879{
d9b3f62e 12880 struct breakpoint *b;
d5551862 12881
d9b3f62e
PA
12882 ALL_TRACEPOINTS (b)
12883 {
12884 struct tracepoint *t = (struct tracepoint *) b;
12885
12886 if (t->number_on_target == num)
12887 return t;
12888 }
d5551862
SS
12889
12890 return NULL;
12891}
12892
1042e4c0 12893/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
12894 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
12895 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 12896 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 12897struct tracepoint *
197f0a60
TT
12898get_tracepoint_by_number (char **arg,
12899 struct get_number_or_range_state *state,
12900 int optional_p)
1042e4c0
SS
12901{
12902 extern int tracepoint_count;
12903 struct breakpoint *t;
12904 int tpnum;
12905 char *instring = arg == NULL ? NULL : *arg;
12906
197f0a60
TT
12907 if (state)
12908 {
12909 gdb_assert (!state->finished);
12910 tpnum = get_number_or_range (state);
12911 }
12912 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
12913 {
12914 if (optional_p)
12915 tpnum = tracepoint_count;
12916 else
12917 error_no_arg (_("tracepoint number"));
12918 }
12919 else
197f0a60 12920 tpnum = get_number (arg);
1042e4c0
SS
12921
12922 if (tpnum <= 0)
12923 {
12924 if (instring && *instring)
12925 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
12926 instring);
12927 else
3e43a32a
MS
12928 printf_filtered (_("Tracepoint argument missing "
12929 "and no previous tracepoint\n"));
1042e4c0
SS
12930 return NULL;
12931 }
12932
12933 ALL_TRACEPOINTS (t)
12934 if (t->number == tpnum)
12935 {
d9b3f62e 12936 return (struct tracepoint *) t;
1042e4c0
SS
12937 }
12938
1042e4c0
SS
12939 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
12940 return NULL;
12941}
12942
d9b3f62e
PA
12943void
12944print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
12945{
12946 if (b->thread != -1)
12947 fprintf_unfiltered (fp, " thread %d", b->thread);
12948
12949 if (b->task != 0)
12950 fprintf_unfiltered (fp, " task %d", b->task);
12951
12952 fprintf_unfiltered (fp, "\n");
12953}
12954
6149aea9
PA
12955/* Save information on user settable breakpoints (watchpoints, etc) to
12956 a new script file named FILENAME. If FILTER is non-NULL, call it
12957 on each breakpoint and only include the ones for which it returns
12958 non-zero. */
12959
1042e4c0 12960static void
6149aea9
PA
12961save_breakpoints (char *filename, int from_tty,
12962 int (*filter) (const struct breakpoint *))
1042e4c0
SS
12963{
12964 struct breakpoint *tp;
6149aea9 12965 int any = 0;
a7bdde9e 12966 char *pathname;
1042e4c0 12967 struct cleanup *cleanup;
a7bdde9e 12968 struct ui_file *fp;
6149aea9 12969 int extra_trace_bits = 0;
1042e4c0 12970
6149aea9
PA
12971 if (filename == 0 || *filename == 0)
12972 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
12973
12974 /* See if we have anything to save. */
6149aea9 12975 ALL_BREAKPOINTS (tp)
1042e4c0 12976 {
6149aea9 12977 /* Skip internal and momentary breakpoints. */
09d682a4 12978 if (!user_breakpoint_p (tp))
6149aea9
PA
12979 continue;
12980
12981 /* If we have a filter, only save the breakpoints it accepts. */
12982 if (filter && !filter (tp))
12983 continue;
12984
12985 any = 1;
12986
12987 if (is_tracepoint (tp))
12988 {
12989 extra_trace_bits = 1;
12990
12991 /* We can stop searching. */
12992 break;
12993 }
1042e4c0 12994 }
6149aea9
PA
12995
12996 if (!any)
1042e4c0 12997 {
6149aea9 12998 warning (_("Nothing to save."));
1042e4c0
SS
12999 return;
13000 }
13001
6149aea9 13002 pathname = tilde_expand (filename);
1042e4c0 13003 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 13004 fp = gdb_fopen (pathname, "w");
059fb39f 13005 if (!fp)
6149aea9
PA
13006 error (_("Unable to open file '%s' for saving (%s)"),
13007 filename, safe_strerror (errno));
a7bdde9e 13008 make_cleanup_ui_file_delete (fp);
8bf6485c 13009
6149aea9
PA
13010 if (extra_trace_bits)
13011 save_trace_state_variables (fp);
8bf6485c 13012
6149aea9 13013 ALL_BREAKPOINTS (tp)
1042e4c0 13014 {
6149aea9 13015 /* Skip internal and momentary breakpoints. */
09d682a4 13016 if (!user_breakpoint_p (tp))
6149aea9 13017 continue;
8bf6485c 13018
6149aea9
PA
13019 /* If we have a filter, only save the breakpoints it accepts. */
13020 if (filter && !filter (tp))
13021 continue;
13022
348d480f 13023 tp->ops->print_recreate (tp, fp);
1042e4c0 13024
6149aea9
PA
13025 /* Note, we can't rely on tp->number for anything, as we can't
13026 assume the recreated breakpoint numbers will match. Use $bpnum
13027 instead. */
13028
13029 if (tp->cond_string)
13030 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
13031
13032 if (tp->ignore_count)
13033 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
13034
a7bdde9e 13035 if (tp->commands)
1042e4c0 13036 {
a7bdde9e
VP
13037 volatile struct gdb_exception ex;
13038
6149aea9 13039 fprintf_unfiltered (fp, " commands\n");
a7bdde9e
VP
13040
13041 ui_out_redirect (uiout, fp);
14dba4b4 13042 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 13043 {
9add0f1b 13044 print_command_lines (uiout, tp->commands->commands, 2);
a7bdde9e
VP
13045 }
13046 ui_out_redirect (uiout, NULL);
1042e4c0 13047
a7bdde9e
VP
13048 if (ex.reason < 0)
13049 throw_exception (ex);
1042e4c0 13050
a7bdde9e 13051 fprintf_unfiltered (fp, " end\n");
1042e4c0 13052 }
6149aea9
PA
13053
13054 if (tp->enable_state == bp_disabled)
13055 fprintf_unfiltered (fp, "disable\n");
13056
13057 /* If this is a multi-location breakpoint, check if the locations
13058 should be individually disabled. Watchpoint locations are
13059 special, and not user visible. */
13060 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13061 {
13062 struct bp_location *loc;
13063 int n = 1;
13064
13065 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
13066 if (!loc->enabled)
13067 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
13068 }
1042e4c0 13069 }
8bf6485c 13070
6149aea9 13071 if (extra_trace_bits && *default_collect)
8bf6485c
SS
13072 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
13073
1042e4c0
SS
13074 do_cleanups (cleanup);
13075 if (from_tty)
6149aea9
PA
13076 printf_filtered (_("Saved to file '%s'.\n"), filename);
13077}
13078
13079/* The `save breakpoints' command. */
13080
13081static void
13082save_breakpoints_command (char *args, int from_tty)
13083{
13084 save_breakpoints (args, from_tty, NULL);
13085}
13086
13087/* The `save tracepoints' command. */
13088
13089static void
13090save_tracepoints_command (char *args, int from_tty)
13091{
13092 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
13093}
13094
13095/* Create a vector of all tracepoints. */
13096
13097VEC(breakpoint_p) *
eeae04df 13098all_tracepoints (void)
1042e4c0
SS
13099{
13100 VEC(breakpoint_p) *tp_vec = 0;
13101 struct breakpoint *tp;
13102
13103 ALL_TRACEPOINTS (tp)
13104 {
13105 VEC_safe_push (breakpoint_p, tp_vec, tp);
13106 }
13107
13108 return tp_vec;
13109}
13110
c906108c 13111\f
4a64f543
MS
13112/* This help string is used for the break, hbreak, tbreak and thbreak
13113 commands. It is defined as a macro to prevent duplication.
13114 COMMAND should be a string constant containing the name of the
13115 command. */
31e2b00f
AS
13116#define BREAK_ARGS_HELP(command) \
13117command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
13118LOCATION may be a line number, function name, or \"*\" and an address.\n\
13119If a line number is specified, break at start of code for that line.\n\
13120If a function is specified, break at start of code for that function.\n\
13121If an address is specified, break at that exact address.\n\
dc10affe
PA
13122With no LOCATION, uses current execution address of the selected\n\
13123stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
13124\n\
13125THREADNUM is the number from \"info threads\".\n\
13126CONDITION is a boolean expression.\n\
13127\n\
d41c0fc8
PA
13128Multiple breakpoints at one place are permitted, and useful if their\n\
13129conditions are different.\n\
31e2b00f
AS
13130\n\
13131Do \"help breakpoints\" for info on other commands dealing with breakpoints."
13132
44feb3ce
TT
13133/* List of subcommands for "catch". */
13134static struct cmd_list_element *catch_cmdlist;
13135
13136/* List of subcommands for "tcatch". */
13137static struct cmd_list_element *tcatch_cmdlist;
13138
9ac4176b 13139void
44feb3ce
TT
13140add_catch_command (char *name, char *docstring,
13141 void (*sfunc) (char *args, int from_tty,
13142 struct cmd_list_element *command),
a96d9b2e
SDJ
13143 char **(*completer) (struct cmd_list_element *cmd,
13144 char *text, char *word),
44feb3ce
TT
13145 void *user_data_catch,
13146 void *user_data_tcatch)
13147{
13148 struct cmd_list_element *command;
13149
13150 command = add_cmd (name, class_breakpoint, NULL, docstring,
13151 &catch_cmdlist);
13152 set_cmd_sfunc (command, sfunc);
13153 set_cmd_context (command, user_data_catch);
a96d9b2e 13154 set_cmd_completer (command, completer);
44feb3ce
TT
13155
13156 command = add_cmd (name, class_breakpoint, NULL, docstring,
13157 &tcatch_cmdlist);
13158 set_cmd_sfunc (command, sfunc);
13159 set_cmd_context (command, user_data_tcatch);
a96d9b2e 13160 set_cmd_completer (command, completer);
44feb3ce
TT
13161}
13162
6c95b8df 13163static void
a79b8f6e 13164clear_syscall_counts (struct inferior *inf)
6c95b8df 13165{
6c95b8df
PA
13166 inf->total_syscalls_count = 0;
13167 inf->any_syscall_count = 0;
13168 VEC_free (int, inf->syscalls_counts);
13169}
13170
6149aea9
PA
13171static void
13172save_command (char *arg, int from_tty)
13173{
3e43a32a
MS
13174 printf_unfiltered (_("\"save\" must be followed by "
13175 "the name of a save subcommand.\n"));
6149aea9
PA
13176 help_list (save_cmdlist, "save ", -1, gdb_stdout);
13177}
13178
84f4c1fe
PM
13179struct breakpoint *
13180iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
13181 void *data)
13182{
35df4500 13183 struct breakpoint *b, *b_tmp;
84f4c1fe 13184
35df4500 13185 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
13186 {
13187 if ((*callback) (b, data))
13188 return b;
13189 }
13190
13191 return NULL;
13192}
13193
2060206e
PA
13194void
13195initialize_breakpoint_ops (void)
13196{
13197 static int initialized = 0;
13198
13199 struct breakpoint_ops *ops;
13200
13201 if (initialized)
13202 return;
13203 initialized = 1;
13204
13205 /* The breakpoint_ops structure to be inherit by all kinds of
13206 breakpoints (real breakpoints, i.e., user "break" breakpoints,
13207 internal and momentary breakpoints, etc.). */
13208 ops = &bkpt_base_breakpoint_ops;
13209 *ops = base_breakpoint_ops;
13210 ops->re_set = bkpt_re_set;
13211 ops->insert_location = bkpt_insert_location;
13212 ops->remove_location = bkpt_remove_location;
13213 ops->breakpoint_hit = bkpt_breakpoint_hit;
13214
13215 /* The breakpoint_ops structure to be used in regular breakpoints. */
13216 ops = &bkpt_breakpoint_ops;
13217 *ops = bkpt_base_breakpoint_ops;
13218 ops->re_set = bkpt_re_set;
13219 ops->resources_needed = bkpt_resources_needed;
13220 ops->print_it = bkpt_print_it;
13221 ops->print_mention = bkpt_print_mention;
13222 ops->print_recreate = bkpt_print_recreate;
13223
13224 /* Ranged breakpoints. */
13225 ops = &ranged_breakpoint_ops;
13226 *ops = bkpt_breakpoint_ops;
13227 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
13228 ops->resources_needed = resources_needed_ranged_breakpoint;
13229 ops->print_it = print_it_ranged_breakpoint;
13230 ops->print_one = print_one_ranged_breakpoint;
13231 ops->print_one_detail = print_one_detail_ranged_breakpoint;
13232 ops->print_mention = print_mention_ranged_breakpoint;
13233 ops->print_recreate = print_recreate_ranged_breakpoint;
13234
13235 /* Internal breakpoints. */
13236 ops = &internal_breakpoint_ops;
13237 *ops = bkpt_base_breakpoint_ops;
13238 ops->re_set = internal_bkpt_re_set;
13239 ops->check_status = internal_bkpt_check_status;
13240 ops->print_it = internal_bkpt_print_it;
13241 ops->print_mention = internal_bkpt_print_mention;
13242
13243 /* Momentary breakpoints. */
13244 ops = &momentary_breakpoint_ops;
13245 *ops = bkpt_base_breakpoint_ops;
13246 ops->re_set = momentary_bkpt_re_set;
13247 ops->check_status = momentary_bkpt_check_status;
13248 ops->print_it = momentary_bkpt_print_it;
13249 ops->print_mention = momentary_bkpt_print_mention;
13250
13251 /* GNU v3 exception catchpoints. */
13252 ops = &gnu_v3_exception_catchpoint_ops;
13253 *ops = bkpt_breakpoint_ops;
13254 ops->print_it = print_it_exception_catchpoint;
13255 ops->print_one = print_one_exception_catchpoint;
13256 ops->print_mention = print_mention_exception_catchpoint;
13257 ops->print_recreate = print_recreate_exception_catchpoint;
13258
13259 /* Watchpoints. */
13260 ops = &watchpoint_breakpoint_ops;
13261 *ops = base_breakpoint_ops;
3a5c3e22 13262 ops->dtor = dtor_watchpoint;
2060206e
PA
13263 ops->re_set = re_set_watchpoint;
13264 ops->insert_location = insert_watchpoint;
13265 ops->remove_location = remove_watchpoint;
13266 ops->breakpoint_hit = breakpoint_hit_watchpoint;
13267 ops->check_status = check_status_watchpoint;
13268 ops->resources_needed = resources_needed_watchpoint;
13269 ops->works_in_software_mode = works_in_software_mode_watchpoint;
13270 ops->print_it = print_it_watchpoint;
13271 ops->print_mention = print_mention_watchpoint;
13272 ops->print_recreate = print_recreate_watchpoint;
13273
13274 /* Masked watchpoints. */
13275 ops = &masked_watchpoint_breakpoint_ops;
13276 *ops = watchpoint_breakpoint_ops;
13277 ops->insert_location = insert_masked_watchpoint;
13278 ops->remove_location = remove_masked_watchpoint;
13279 ops->resources_needed = resources_needed_masked_watchpoint;
13280 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
13281 ops->print_it = print_it_masked_watchpoint;
13282 ops->print_one_detail = print_one_detail_masked_watchpoint;
13283 ops->print_mention = print_mention_masked_watchpoint;
13284 ops->print_recreate = print_recreate_masked_watchpoint;
13285
13286 /* Tracepoints. */
13287 ops = &tracepoint_breakpoint_ops;
13288 *ops = base_breakpoint_ops;
13289 ops->re_set = tracepoint_re_set;
13290 ops->breakpoint_hit = tracepoint_breakpoint_hit;
13291 ops->print_one_detail = tracepoint_print_one_detail;
13292 ops->print_mention = tracepoint_print_mention;
13293 ops->print_recreate = tracepoint_print_recreate;
13294
13295 /* Fork catchpoints. */
13296 ops = &catch_fork_breakpoint_ops;
13297 *ops = base_breakpoint_ops;
13298 ops->insert_location = insert_catch_fork;
13299 ops->remove_location = remove_catch_fork;
13300 ops->breakpoint_hit = breakpoint_hit_catch_fork;
13301 ops->print_it = print_it_catch_fork;
13302 ops->print_one = print_one_catch_fork;
13303 ops->print_mention = print_mention_catch_fork;
13304 ops->print_recreate = print_recreate_catch_fork;
13305
13306 /* Vfork catchpoints. */
13307 ops = &catch_vfork_breakpoint_ops;
13308 *ops = base_breakpoint_ops;
13309 ops->insert_location = insert_catch_vfork;
13310 ops->remove_location = remove_catch_vfork;
13311 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
13312 ops->print_it = print_it_catch_vfork;
13313 ops->print_one = print_one_catch_vfork;
13314 ops->print_mention = print_mention_catch_vfork;
13315 ops->print_recreate = print_recreate_catch_vfork;
13316
13317 /* Exec catchpoints. */
13318 ops = &catch_exec_breakpoint_ops;
13319 *ops = base_breakpoint_ops;
13320 ops->dtor = dtor_catch_exec;
13321 ops->insert_location = insert_catch_exec;
13322 ops->remove_location = remove_catch_exec;
13323 ops->breakpoint_hit = breakpoint_hit_catch_exec;
13324 ops->print_it = print_it_catch_exec;
13325 ops->print_one = print_one_catch_exec;
13326 ops->print_mention = print_mention_catch_exec;
13327 ops->print_recreate = print_recreate_catch_exec;
13328
13329 /* Syscall catchpoints. */
13330 ops = &catch_syscall_breakpoint_ops;
13331 *ops = base_breakpoint_ops;
13332 ops->dtor = dtor_catch_syscall;
13333 ops->insert_location = insert_catch_syscall;
13334 ops->remove_location = remove_catch_syscall;
13335 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
13336 ops->print_it = print_it_catch_syscall;
13337 ops->print_one = print_one_catch_syscall;
13338 ops->print_mention = print_mention_catch_syscall;
13339 ops->print_recreate = print_recreate_catch_syscall;
13340}
13341
c906108c 13342void
fba45db2 13343_initialize_breakpoint (void)
c906108c
SS
13344{
13345 struct cmd_list_element *c;
13346
2060206e
PA
13347 initialize_breakpoint_ops ();
13348
84acb35a 13349 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 13350 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 13351 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 13352
17450429
PP
13353 breakpoint_objfile_key = register_objfile_data ();
13354
c906108c
SS
13355 breakpoint_chain = 0;
13356 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
13357 before a breakpoint is set. */
13358 breakpoint_count = 0;
13359
1042e4c0
SS
13360 tracepoint_count = 0;
13361
1bedd215
AC
13362 add_com ("ignore", class_breakpoint, ignore_command, _("\
13363Set ignore-count of breakpoint number N to COUNT.\n\
13364Usage is `ignore N COUNT'."));
c906108c 13365 if (xdb_commands)
c5aa993b 13366 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 13367
1bedd215
AC
13368 add_com ("commands", class_breakpoint, commands_command, _("\
13369Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
13370Give breakpoint number as argument after \"commands\".\n\
13371With no argument, the targeted breakpoint is the last one set.\n\
13372The commands themselves follow starting on the next line.\n\
13373Type a line containing \"end\" to indicate the end of them.\n\
13374Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 13375then no output is printed when it is hit, except what the commands print."));
c906108c 13376
1bedd215
AC
13377 add_com ("condition", class_breakpoint, condition_command, _("\
13378Specify breakpoint number N to break only if COND is true.\n\
c906108c 13379Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 13380expression to be evaluated whenever breakpoint N is reached."));
c906108c 13381
1bedd215 13382 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 13383Set a temporary breakpoint.\n\
c906108c
SS
13384Like \"break\" except the breakpoint is only temporary,\n\
13385so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
13386by using \"enable delete\" on the breakpoint number.\n\
13387\n"
13388BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 13389 set_cmd_completer (c, location_completer);
c94fdfd0 13390
1bedd215 13391 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 13392Set a hardware assisted breakpoint.\n\
c906108c 13393Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
13394some target hardware may not have this support.\n\
13395\n"
13396BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 13397 set_cmd_completer (c, location_completer);
c906108c 13398
1bedd215 13399 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 13400Set a temporary hardware assisted breakpoint.\n\
c906108c 13401Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
13402so it will be deleted when hit.\n\
13403\n"
13404BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 13405 set_cmd_completer (c, location_completer);
c906108c 13406
1bedd215
AC
13407 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
13408Enable some breakpoints.\n\
c906108c
SS
13409Give breakpoint numbers (separated by spaces) as arguments.\n\
13410With no subcommand, breakpoints are enabled until you command otherwise.\n\
13411This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13412With a subcommand you can enable temporarily."),
c906108c
SS
13413 &enablelist, "enable ", 1, &cmdlist);
13414 if (xdb_commands)
1bedd215
AC
13415 add_com ("ab", class_breakpoint, enable_command, _("\
13416Enable some breakpoints.\n\
c906108c
SS
13417Give breakpoint numbers (separated by spaces) as arguments.\n\
13418With no subcommand, breakpoints are enabled until you command otherwise.\n\
13419This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13420With a subcommand you can enable temporarily."));
c906108c
SS
13421
13422 add_com_alias ("en", "enable", class_breakpoint, 1);
13423
84951ab5 13424 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 13425Enable some breakpoints.\n\
c906108c
SS
13426Give breakpoint numbers (separated by spaces) as arguments.\n\
13427This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13428May be abbreviated to simply \"enable\".\n"),
c5aa993b 13429 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 13430
1a966eab
AC
13431 add_cmd ("once", no_class, enable_once_command, _("\
13432Enable breakpoints for one hit. Give breakpoint numbers.\n\
13433If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13434 &enablebreaklist);
13435
1a966eab
AC
13436 add_cmd ("delete", no_class, enable_delete_command, _("\
13437Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13438If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13439 &enablebreaklist);
13440
1a966eab
AC
13441 add_cmd ("delete", no_class, enable_delete_command, _("\
13442Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13443If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13444 &enablelist);
13445
1a966eab
AC
13446 add_cmd ("once", no_class, enable_once_command, _("\
13447Enable breakpoints for one hit. Give breakpoint numbers.\n\
13448If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13449 &enablelist);
13450
1bedd215
AC
13451 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13452Disable some breakpoints.\n\
c906108c
SS
13453Arguments are breakpoint numbers with spaces in between.\n\
13454To disable all breakpoints, give no argument.\n\
64b9b334 13455A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
13456 &disablelist, "disable ", 1, &cmdlist);
13457 add_com_alias ("dis", "disable", class_breakpoint, 1);
13458 add_com_alias ("disa", "disable", class_breakpoint, 1);
13459 if (xdb_commands)
1bedd215
AC
13460 add_com ("sb", class_breakpoint, disable_command, _("\
13461Disable some breakpoints.\n\
c906108c
SS
13462Arguments are breakpoint numbers with spaces in between.\n\
13463To disable all breakpoints, give no argument.\n\
64b9b334 13464A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 13465
1a966eab
AC
13466 add_cmd ("breakpoints", class_alias, disable_command, _("\
13467Disable some breakpoints.\n\
c906108c
SS
13468Arguments are breakpoint numbers with spaces in between.\n\
13469To disable all breakpoints, give no argument.\n\
64b9b334 13470A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 13471This command may be abbreviated \"disable\"."),
c906108c
SS
13472 &disablelist);
13473
1bedd215
AC
13474 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13475Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13476Arguments are breakpoint numbers with spaces in between.\n\
13477To delete all breakpoints, give no argument.\n\
13478\n\
13479Also a prefix command for deletion of other GDB objects.\n\
1bedd215 13480The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
13481 &deletelist, "delete ", 1, &cmdlist);
13482 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 13483 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 13484 if (xdb_commands)
1bedd215
AC
13485 add_com ("db", class_breakpoint, delete_command, _("\
13486Delete some breakpoints.\n\
c906108c 13487Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 13488To delete all breakpoints, give no argument.\n"));
c906108c 13489
1a966eab
AC
13490 add_cmd ("breakpoints", class_alias, delete_command, _("\
13491Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13492Arguments are breakpoint numbers with spaces in between.\n\
13493To delete all breakpoints, give no argument.\n\
1a966eab 13494This command may be abbreviated \"delete\"."),
c906108c
SS
13495 &deletelist);
13496
1bedd215
AC
13497 add_com ("clear", class_breakpoint, clear_command, _("\
13498Clear breakpoint at specified line or function.\n\
c906108c
SS
13499Argument may be line number, function name, or \"*\" and an address.\n\
13500If line number is specified, all breakpoints in that line are cleared.\n\
13501If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
13502If an address is specified, breakpoints at that address are cleared.\n\
13503\n\
13504With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
13505is executing in.\n\
13506\n\
1bedd215 13507See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 13508 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 13509
1bedd215 13510 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
13511Set breakpoint at specified line or function.\n"
13512BREAK_ARGS_HELP ("break")));
5ba2abeb 13513 set_cmd_completer (c, location_completer);
c94fdfd0 13514
c906108c
SS
13515 add_com_alias ("b", "break", class_run, 1);
13516 add_com_alias ("br", "break", class_run, 1);
13517 add_com_alias ("bre", "break", class_run, 1);
13518 add_com_alias ("brea", "break", class_run, 1);
13519
7681d515
PM
13520 if (xdb_commands)
13521 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
13522
13523 if (dbx_commands)
13524 {
1bedd215
AC
13525 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13526Break in function/address or break at a line in the current file."),
c5aa993b
JM
13527 &stoplist, "stop ", 1, &cmdlist);
13528 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 13529 _("Break in function or address."), &stoplist);
c5aa993b 13530 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 13531 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
13532 add_com ("status", class_info, breakpoints_info, _("\
13533Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13534The \"Type\" column indicates one of:\n\
13535\tbreakpoint - normal breakpoint\n\
13536\twatchpoint - watchpoint\n\
13537The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13538the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13539breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13540address and file/line number respectively.\n\
13541\n\
13542Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13543are set to the address of the last breakpoint listed unless the command\n\
13544is prefixed with \"server \".\n\n\
c906108c 13545Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13546breakpoint set."));
c906108c
SS
13547 }
13548
1bedd215 13549 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 13550Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
13551The \"Type\" column indicates one of:\n\
13552\tbreakpoint - normal breakpoint\n\
13553\twatchpoint - watchpoint\n\
13554The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13555the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13556breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13557address and file/line number respectively.\n\
13558\n\
13559Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13560are set to the address of the last breakpoint listed unless the command\n\
13561is prefixed with \"server \".\n\n\
c906108c 13562Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13563breakpoint set."));
c906108c 13564
6b04bdb7
MS
13565 add_info_alias ("b", "breakpoints", 1);
13566
c906108c 13567 if (xdb_commands)
1bedd215
AC
13568 add_com ("lb", class_breakpoint, breakpoints_info, _("\
13569Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13570The \"Type\" column indicates one of:\n\
13571\tbreakpoint - normal breakpoint\n\
13572\twatchpoint - watchpoint\n\
13573The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13574the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13575breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13576address and file/line number respectively.\n\
13577\n\
13578Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13579are set to the address of the last breakpoint listed unless the command\n\
13580is prefixed with \"server \".\n\n\
c906108c 13581Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13582breakpoint set."));
c906108c 13583
1a966eab
AC
13584 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13585Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13586The \"Type\" column indicates one of:\n\
13587\tbreakpoint - normal breakpoint\n\
13588\twatchpoint - watchpoint\n\
13589\tlongjmp - internal breakpoint used to step through longjmp()\n\
13590\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13591\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
13592\tfinish - internal breakpoint used by the \"finish\" command\n\
13593The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
13594the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13595breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
13596address and file/line number respectively.\n\
13597\n\
13598Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13599are set to the address of the last breakpoint listed unless the command\n\
13600is prefixed with \"server \".\n\n\
c906108c 13601Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 13602breakpoint set."),
c906108c
SS
13603 &maintenanceinfolist);
13604
44feb3ce
TT
13605 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13606Set catchpoints to catch events."),
13607 &catch_cmdlist, "catch ",
13608 0/*allow-unknown*/, &cmdlist);
13609
13610 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13611Set temporary catchpoints to catch events."),
13612 &tcatch_cmdlist, "tcatch ",
13613 0/*allow-unknown*/, &cmdlist);
13614
13615 /* Add catch and tcatch sub-commands. */
13616 add_catch_command ("catch", _("\
13617Catch an exception, when caught.\n\
13618With an argument, catch only exceptions with the given name."),
13619 catch_catch_command,
a96d9b2e 13620 NULL,
44feb3ce
TT
13621 CATCH_PERMANENT,
13622 CATCH_TEMPORARY);
13623 add_catch_command ("throw", _("\
13624Catch an exception, when thrown.\n\
13625With an argument, catch only exceptions with the given name."),
13626 catch_throw_command,
a96d9b2e 13627 NULL,
44feb3ce
TT
13628 CATCH_PERMANENT,
13629 CATCH_TEMPORARY);
13630 add_catch_command ("fork", _("Catch calls to fork."),
13631 catch_fork_command_1,
a96d9b2e 13632 NULL,
44feb3ce
TT
13633 (void *) (uintptr_t) catch_fork_permanent,
13634 (void *) (uintptr_t) catch_fork_temporary);
13635 add_catch_command ("vfork", _("Catch calls to vfork."),
13636 catch_fork_command_1,
a96d9b2e 13637 NULL,
44feb3ce
TT
13638 (void *) (uintptr_t) catch_vfork_permanent,
13639 (void *) (uintptr_t) catch_vfork_temporary);
13640 add_catch_command ("exec", _("Catch calls to exec."),
13641 catch_exec_command_1,
a96d9b2e
SDJ
13642 NULL,
13643 CATCH_PERMANENT,
13644 CATCH_TEMPORARY);
13645 add_catch_command ("syscall", _("\
13646Catch system calls by their names and/or numbers.\n\
13647Arguments say which system calls to catch. If no arguments\n\
13648are given, every system call will be caught.\n\
13649Arguments, if given, should be one or more system call names\n\
13650(if your system supports that), or system call numbers."),
13651 catch_syscall_command_1,
13652 catch_syscall_completer,
44feb3ce
TT
13653 CATCH_PERMANENT,
13654 CATCH_TEMPORARY);
c5aa993b 13655
1bedd215
AC
13656 c = add_com ("watch", class_breakpoint, watch_command, _("\
13657Set a watchpoint for an expression.\n\
06a64a0b 13658Usage: watch [-l|-location] EXPRESSION\n\
c906108c 13659A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13660an expression changes.\n\
13661If -l or -location is given, this evaluates EXPRESSION and watches\n\
13662the memory to which it refers."));
65d12d83 13663 set_cmd_completer (c, expression_completer);
c906108c 13664
1bedd215
AC
13665 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13666Set a read watchpoint for an expression.\n\
06a64a0b 13667Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 13668A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13669an expression is read.\n\
13670If -l or -location is given, this evaluates EXPRESSION and watches\n\
13671the memory to which it refers."));
65d12d83 13672 set_cmd_completer (c, expression_completer);
c906108c 13673
1bedd215
AC
13674 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13675Set a watchpoint for an expression.\n\
06a64a0b 13676Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 13677A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13678an expression is either read or written.\n\
13679If -l or -location is given, this evaluates EXPRESSION and watches\n\
13680the memory to which it refers."));
65d12d83 13681 set_cmd_completer (c, expression_completer);
c906108c 13682
d77f58be 13683 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 13684Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 13685
920d2a44
AC
13686 /* XXX: cagney/2005-02-23: This should be a boolean, and should
13687 respond to changes - contrary to the description. */
85c07804
AC
13688 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
13689 &can_use_hw_watchpoints, _("\
13690Set debugger's willingness to use watchpoint hardware."), _("\
13691Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
13692If zero, gdb will not use hardware for new watchpoints, even if\n\
13693such is available. (However, any hardware watchpoints that were\n\
13694created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
13695hardware.)"),
13696 NULL,
920d2a44 13697 show_can_use_hw_watchpoints,
85c07804 13698 &setlist, &showlist);
c906108c
SS
13699
13700 can_use_hw_watchpoints = 1;
fa8d40ab 13701
1042e4c0
SS
13702 /* Tracepoint manipulation commands. */
13703
13704 c = add_com ("trace", class_breakpoint, trace_command, _("\
13705Set a tracepoint at specified line or function.\n\
13706\n"
13707BREAK_ARGS_HELP ("trace") "\n\
13708Do \"help tracepoints\" for info on other tracepoint commands."));
13709 set_cmd_completer (c, location_completer);
13710
13711 add_com_alias ("tp", "trace", class_alias, 0);
13712 add_com_alias ("tr", "trace", class_alias, 1);
13713 add_com_alias ("tra", "trace", class_alias, 1);
13714 add_com_alias ("trac", "trace", class_alias, 1);
13715
7a697b8d
SS
13716 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
13717Set a fast tracepoint at specified line or function.\n\
13718\n"
13719BREAK_ARGS_HELP ("ftrace") "\n\
13720Do \"help tracepoints\" for info on other tracepoint commands."));
13721 set_cmd_completer (c, location_completer);
13722
0fb4aa4b
PA
13723 c = add_com ("strace", class_breakpoint, strace_command, _("\
13724Set a static tracepoint at specified line, function or marker.\n\
13725\n\
13726strace [LOCATION] [if CONDITION]\n\
13727LOCATION may be a line number, function name, \"*\" and an address,\n\
13728or -m MARKER_ID.\n\
13729If a line number is specified, probe the marker at start of code\n\
13730for that line. If a function is specified, probe the marker at start\n\
13731of code for that function. If an address is specified, probe the marker\n\
13732at that exact address. If a marker id is specified, probe the marker\n\
13733with that name. With no LOCATION, uses current execution address of\n\
13734the selected stack frame.\n\
13735Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
13736This collects arbitrary user data passed in the probe point call to the\n\
13737tracing library. You can inspect it when analyzing the trace buffer,\n\
13738by printing the $_sdata variable like any other convenience variable.\n\
13739\n\
13740CONDITION is a boolean expression.\n\
13741\n\
d41c0fc8
PA
13742Multiple tracepoints at one place are permitted, and useful if their\n\
13743conditions are different.\n\
0fb4aa4b
PA
13744\n\
13745Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
13746Do \"help tracepoints\" for info on other tracepoint commands."));
13747 set_cmd_completer (c, location_completer);
13748
1042e4c0 13749 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 13750Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
13751Convenience variable \"$tpnum\" contains the number of the\n\
13752last tracepoint set."));
13753
13754 add_info_alias ("tp", "tracepoints", 1);
13755
13756 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
13757Delete specified tracepoints.\n\
13758Arguments are tracepoint numbers, separated by spaces.\n\
13759No argument means delete all tracepoints."),
13760 &deletelist);
13761
13762 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13763Disable specified tracepoints.\n\
13764Arguments are tracepoint numbers, separated by spaces.\n\
13765No argument means disable all tracepoints."),
13766 &disablelist);
13767 deprecate_cmd (c, "disable");
13768
13769 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
13770Enable specified tracepoints.\n\
13771Arguments are tracepoint numbers, separated by spaces.\n\
13772No argument means enable all tracepoints."),
13773 &enablelist);
13774 deprecate_cmd (c, "enable");
13775
13776 add_com ("passcount", class_trace, trace_pass_command, _("\
13777Set the passcount for a tracepoint.\n\
13778The trace will end when the tracepoint has been passed 'count' times.\n\
13779Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
13780if TPNUM is omitted, passcount refers to the last tracepoint defined."));
13781
6149aea9
PA
13782 add_prefix_cmd ("save", class_breakpoint, save_command,
13783 _("Save breakpoint definitions as a script."),
13784 &save_cmdlist, "save ",
13785 0/*allow-unknown*/, &cmdlist);
13786
13787 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
13788Save current breakpoint definitions as a script.\n\
cce7e648 13789This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
13790catchpoints, tracepoints). Use the 'source' command in another debug\n\
13791session to restore them."),
13792 &save_cmdlist);
13793 set_cmd_completer (c, filename_completer);
13794
13795 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 13796Save current tracepoint definitions as a script.\n\
6149aea9
PA
13797Use the 'source' command in another debug session to restore them."),
13798 &save_cmdlist);
1042e4c0
SS
13799 set_cmd_completer (c, filename_completer);
13800
6149aea9
PA
13801 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
13802 deprecate_cmd (c, "save tracepoints");
13803
1bedd215 13804 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
13805Breakpoint specific settings\n\
13806Configure various breakpoint-specific variables such as\n\
1bedd215 13807pending breakpoint behavior"),
fa8d40ab
JJ
13808 &breakpoint_set_cmdlist, "set breakpoint ",
13809 0/*allow-unknown*/, &setlist);
1bedd215 13810 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
13811Breakpoint specific settings\n\
13812Configure various breakpoint-specific variables such as\n\
1bedd215 13813pending breakpoint behavior"),
fa8d40ab
JJ
13814 &breakpoint_show_cmdlist, "show breakpoint ",
13815 0/*allow-unknown*/, &showlist);
13816
7915a72c
AC
13817 add_setshow_auto_boolean_cmd ("pending", no_class,
13818 &pending_break_support, _("\
13819Set debugger's behavior regarding pending breakpoints."), _("\
13820Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
13821If on, an unrecognized breakpoint location will cause gdb to create a\n\
13822pending breakpoint. If off, an unrecognized breakpoint location results in\n\
13823an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 13824user-query to see if a pending breakpoint should be created."),
2c5b56ce 13825 NULL,
920d2a44 13826 show_pending_break_support,
6e1d7d6c
AC
13827 &breakpoint_set_cmdlist,
13828 &breakpoint_show_cmdlist);
fa8d40ab
JJ
13829
13830 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
13831
13832 add_setshow_boolean_cmd ("auto-hw", no_class,
13833 &automatic_hardware_breakpoints, _("\
13834Set automatic usage of hardware breakpoints."), _("\
13835Show automatic usage of hardware breakpoints."), _("\
13836If set, the debugger will automatically use hardware breakpoints for\n\
13837breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
13838a warning will be emitted for such breakpoints."),
13839 NULL,
13840 show_automatic_hardware_breakpoints,
13841 &breakpoint_set_cmdlist,
13842 &breakpoint_show_cmdlist);
74960c60 13843
33e5cbd6
PA
13844 add_setshow_enum_cmd ("always-inserted", class_support,
13845 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
13846Set mode for inserting breakpoints."), _("\
13847Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
13848When this mode is off, breakpoints are inserted in inferior when it is\n\
13849resumed, and removed when execution stops. When this mode is on,\n\
13850breakpoints are inserted immediately and removed only when the user\n\
13851deletes the breakpoint. When this mode is auto (which is the default),\n\
13852the behaviour depends on the non-stop setting (see help set non-stop).\n\
13853In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
13854behaves as if always-inserted mode is on; if gdb is controlling the\n\
13855inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
13856 NULL,
13857 &show_always_inserted_mode,
13858 &breakpoint_set_cmdlist,
13859 &breakpoint_show_cmdlist);
f1310107
TJB
13860
13861 add_com ("break-range", class_breakpoint, break_range_command, _("\
13862Set a breakpoint for an address range.\n\
13863break-range START-LOCATION, END-LOCATION\n\
13864where START-LOCATION and END-LOCATION can be one of the following:\n\
13865 LINENUM, for that line in the current file,\n\
13866 FILE:LINENUM, for that line in that file,\n\
13867 +OFFSET, for that number of lines after the current line\n\
13868 or the start of the range\n\
13869 FUNCTION, for the first line in that function,\n\
13870 FILE:FUNCTION, to distinguish among like-named static functions.\n\
13871 *ADDRESS, for the instruction at that address.\n\
13872\n\
13873The breakpoint will stop execution of the inferior whenever it executes\n\
13874an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
13875range (including START-LOCATION and END-LOCATION)."));
13876
765dc015 13877 automatic_hardware_breakpoints = 1;
f3b1572e
PA
13878
13879 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 13880}
This page took 2.051184 seconds and 4 git commands to generate.