the "ambiguous linespec" series
[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"
50f182aa 41#include "gdb-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"
1bfeeb0f
JL
68#include "stack.h"
69#include "skip.h"
049e32d3 70#include "record.h"
c906108c 71
1042e4c0
SS
72/* readline include files */
73#include "readline/readline.h"
74#include "readline/history.h"
75
76/* readline defines this. */
77#undef savestring
78
034dad6f 79#include "mi/mi-common.h"
7371cf6d 80#include "python/python.h"
104c1213 81
4a64f543 82/* Prototypes for local functions. */
c906108c 83
a14ed312 84static void enable_delete_command (char *, int);
c906108c 85
a14ed312 86static void enable_once_command (char *, int);
c906108c 87
a14ed312 88static void disable_command (char *, int);
c906108c 89
a14ed312 90static void enable_command (char *, int);
c906108c 91
95a42b64
TT
92static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
93 void *),
94 void *);
c906108c 95
a14ed312 96static void ignore_command (char *, int);
c906108c 97
4efb68b1 98static int breakpoint_re_set_one (void *);
c906108c 99
348d480f
PA
100static void breakpoint_re_set_default (struct breakpoint *);
101
a14ed312 102static void clear_command (char *, int);
c906108c 103
a14ed312 104static void catch_command (char *, int);
c906108c 105
a9634178 106static int can_use_hardware_watchpoint (struct value *);
c906108c 107
98deb0da 108static void break_command_1 (char *, int, int);
c906108c 109
a14ed312 110static void mention (struct breakpoint *);
c906108c 111
348d480f
PA
112static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
113 enum bptype,
c0a91b2b 114 const struct breakpoint_ops *);
3742cc8b
YQ
115static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
116 const struct symtab_and_line *);
117
4a64f543
MS
118/* This function is used in gdbtk sources and thus can not be made
119 static. */
63c252f8 120struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 121 struct symtab_and_line,
c0a91b2b
TT
122 enum bptype,
123 const struct breakpoint_ops *);
c906108c 124
06edf0c0
PA
125static struct breakpoint *
126 momentary_breakpoint_from_master (struct breakpoint *orig,
127 enum bptype type,
c0a91b2b 128 const struct breakpoint_ops *ops);
06edf0c0 129
76897487
KB
130static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
131
a6d9a66e
UW
132static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
133 CORE_ADDR bpaddr,
88f7da05 134 enum bptype bptype);
76897487 135
6c95b8df
PA
136static void describe_other_breakpoints (struct gdbarch *,
137 struct program_space *, CORE_ADDR,
5af949e3 138 struct obj_section *, int);
c906108c 139
6c95b8df
PA
140static int breakpoint_address_match (struct address_space *aspace1,
141 CORE_ADDR addr1,
142 struct address_space *aspace2,
143 CORE_ADDR addr2);
144
85d721b8
PA
145static int watchpoint_locations_match (struct bp_location *loc1,
146 struct bp_location *loc2);
147
f1310107
TJB
148static int breakpoint_location_address_match (struct bp_location *bl,
149 struct address_space *aspace,
150 CORE_ADDR addr);
151
a14ed312 152static void breakpoints_info (char *, int);
c906108c 153
d77f58be
SS
154static void watchpoints_info (char *, int);
155
e5a67952
MS
156static int breakpoint_1 (char *, int,
157 int (*) (const struct breakpoint *));
c906108c 158
4efb68b1 159static int breakpoint_cond_eval (void *);
c906108c 160
4efb68b1 161static void cleanup_executing_breakpoints (void *);
c906108c 162
a14ed312 163static void commands_command (char *, int);
c906108c 164
a14ed312 165static void condition_command (char *, int);
c906108c 166
c5aa993b
JM
167typedef enum
168 {
169 mark_inserted,
170 mark_uninserted
171 }
172insertion_state_t;
c906108c 173
0bde7532 174static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 175static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 176
e514a9d6 177static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 178
4efb68b1 179static int watchpoint_check (void *);
c906108c 180
a14ed312 181static void maintenance_info_breakpoints (char *, int);
c906108c 182
a14ed312 183static int hw_breakpoint_used_count (void);
c906108c 184
a1398e0c
PA
185static int hw_watchpoint_use_count (struct breakpoint *);
186
187static int hw_watchpoint_used_count_others (struct breakpoint *except,
188 enum bptype type,
189 int *other_type_used);
c906108c 190
a14ed312 191static void hbreak_command (char *, int);
c906108c 192
a14ed312 193static void thbreak_command (char *, int);
c906108c 194
51be5b68 195static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp);
c906108c 196
a14ed312 197static void stop_command (char *arg, int from_tty);
7a292a7a 198
a14ed312 199static void stopin_command (char *arg, int from_tty);
7a292a7a 200
a14ed312 201static void stopat_command (char *arg, int from_tty);
7a292a7a 202
a14ed312 203static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 204
d85310f7
MS
205static void catch_exception_command_1 (enum exception_event_kind ex_event,
206 char *arg, int tempflag, int from_tty);
7a292a7a 207
a14ed312 208static void tcatch_command (char *arg, int from_tty);
7a292a7a 209
d03285ec
UW
210static void detach_single_step_breakpoints (void);
211
6c95b8df
PA
212static int single_step_breakpoint_inserted_here_p (struct address_space *,
213 CORE_ADDR pc);
1aafd4da 214
fe3f5fa8 215static void free_bp_location (struct bp_location *loc);
f431efe5
PA
216static void incref_bp_location (struct bp_location *loc);
217static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 218
39d61571 219static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 220
b60e7edf 221static void update_global_location_list (int);
a5606eee 222
b60e7edf 223static void update_global_location_list_nothrow (int);
74960c60 224
d77f58be 225static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
226
227static void insert_breakpoint_locations (void);
a5606eee 228
a96d9b2e
SDJ
229static int syscall_catchpoint_p (struct breakpoint *b);
230
1042e4c0
SS
231static void tracepoints_info (char *, int);
232
233static void delete_trace_command (char *, int);
234
235static void enable_trace_command (char *, int);
236
237static void disable_trace_command (char *, int);
238
239static void trace_pass_command (char *, int);
240
9c06b0b4
TJB
241static int is_masked_watchpoint (const struct breakpoint *b);
242
0fb4aa4b
PA
243/* Assuming we're creating a static tracepoint, does S look like a
244 static tracepoint marker spec ("-m MARKER_ID")? */
245#define is_marker_spec(s) \
f5a8e22b 246 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 247
2060206e
PA
248/* The abstract base class all breakpoint_ops structures inherit
249 from. */
250static struct breakpoint_ops base_breakpoint_ops;
251
252/* The breakpoint_ops structure to be inherited by all breakpoint_ops
253 that are implemented on top of software or hardware breakpoints
254 (user breakpoints, internal and momentary breakpoints, etc.). */
255static struct breakpoint_ops bkpt_base_breakpoint_ops;
256
257/* Internal breakpoints class type. */
06edf0c0 258static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
259
260/* Momentary breakpoints class type. */
06edf0c0
PA
261static struct breakpoint_ops momentary_breakpoint_ops;
262
2060206e
PA
263/* The breakpoint_ops structure to be used in regular user created
264 breakpoints. */
265struct breakpoint_ops bkpt_breakpoint_ops;
266
5cea2a26
PA
267/* A reference-counted struct command_line. This lets multiple
268 breakpoints share a single command list. */
269struct counted_command_line
270{
271 /* The reference count. */
272 int refc;
273
274 /* The command list. */
275 struct command_line *commands;
276};
277
278struct command_line *
279breakpoint_commands (struct breakpoint *b)
280{
281 return b->commands ? b->commands->commands : NULL;
282}
3daf8fe5 283
f3b1572e
PA
284/* Flag indicating that a command has proceeded the inferior past the
285 current breakpoint. */
286
287static int breakpoint_proceeded;
288
956a9fb9 289const char *
2cec12e5
AR
290bpdisp_text (enum bpdisp disp)
291{
4a64f543
MS
292 /* NOTE: the following values are a part of MI protocol and
293 represent values of 'disp' field returned when inferior stops at
294 a breakpoint. */
bc043ef3 295 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 296
2cec12e5
AR
297 return bpdisps[(int) disp];
298}
c906108c 299
4a64f543 300/* Prototypes for exported functions. */
c906108c 301/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 302 if such is available. */
c906108c
SS
303static int can_use_hw_watchpoints;
304
920d2a44
AC
305static void
306show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
307 struct cmd_list_element *c,
308 const char *value)
309{
3e43a32a
MS
310 fprintf_filtered (file,
311 _("Debugger's willingness to use "
312 "watchpoint hardware is %s.\n"),
920d2a44
AC
313 value);
314}
315
fa8d40ab
JJ
316/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
317 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 318 for unrecognized breakpoint locations.
fa8d40ab
JJ
319 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
320static enum auto_boolean pending_break_support;
920d2a44
AC
321static void
322show_pending_break_support (struct ui_file *file, int from_tty,
323 struct cmd_list_element *c,
324 const char *value)
325{
3e43a32a
MS
326 fprintf_filtered (file,
327 _("Debugger's behavior regarding "
328 "pending breakpoints is %s.\n"),
920d2a44
AC
329 value);
330}
fa8d40ab 331
765dc015 332/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 333 set with "break" but falling in read-only memory.
765dc015
VP
334 If 0, gdb will warn about such breakpoints, but won't automatically
335 use hardware breakpoints. */
336static int automatic_hardware_breakpoints;
337static void
338show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
339 struct cmd_list_element *c,
340 const char *value)
341{
3e43a32a
MS
342 fprintf_filtered (file,
343 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
344 value);
345}
346
33e5cbd6
PA
347/* If on, gdb will keep breakpoints inserted even as inferior is
348 stopped, and immediately insert any new breakpoints. If off, gdb
349 will insert breakpoints into inferior only when resuming it, and
350 will remove breakpoints upon stop. If auto, GDB will behave as ON
351 if in non-stop mode, and as OFF if all-stop mode.*/
352
353static const char always_inserted_auto[] = "auto";
354static const char always_inserted_on[] = "on";
355static const char always_inserted_off[] = "off";
356static const char *always_inserted_enums[] = {
357 always_inserted_auto,
358 always_inserted_off,
359 always_inserted_on,
360 NULL
361};
362static const char *always_inserted_mode = always_inserted_auto;
363static void
74960c60 364show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 365 struct cmd_list_element *c, const char *value)
74960c60 366{
33e5cbd6 367 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
368 fprintf_filtered (file,
369 _("Always inserted breakpoint "
370 "mode is %s (currently %s).\n"),
33e5cbd6
PA
371 value,
372 breakpoints_always_inserted_mode () ? "on" : "off");
373 else
3e43a32a
MS
374 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
375 value);
74960c60
VP
376}
377
33e5cbd6
PA
378int
379breakpoints_always_inserted_mode (void)
380{
049e32d3
PA
381 return ((always_inserted_mode == always_inserted_on
382 || (always_inserted_mode == always_inserted_auto && non_stop))
383 && !RECORD_IS_USED);
33e5cbd6 384}
765dc015 385
a14ed312 386void _initialize_breakpoint (void);
c906108c 387
c906108c
SS
388/* Are we executing breakpoint commands? */
389static int executing_breakpoint_commands;
390
c02f5703
MS
391/* Are overlay event breakpoints enabled? */
392static int overlay_events_enabled;
393
e09342b5
TJB
394/* See description in breakpoint.h. */
395int target_exact_watchpoints = 0;
396
c906108c 397/* Walk the following statement or block through all breakpoints.
e5dd4106 398 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 399 current breakpoint. */
c906108c 400
5c44784c 401#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 402
5c44784c
JM
403#define ALL_BREAKPOINTS_SAFE(B,TMP) \
404 for (B = breakpoint_chain; \
405 B ? (TMP=B->next, 1): 0; \
406 B = TMP)
c906108c 407
4a64f543
MS
408/* Similar iterator for the low-level breakpoints. SAFE variant is
409 not provided so update_global_location_list must not be called
410 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 411
876fa593
JK
412#define ALL_BP_LOCATIONS(B,BP_TMP) \
413 for (BP_TMP = bp_location; \
414 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
415 BP_TMP++)
7cc221ef 416
1042e4c0
SS
417/* Iterator for tracepoints only. */
418
419#define ALL_TRACEPOINTS(B) \
420 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 421 if (is_tracepoint (B))
1042e4c0 422
7cc221ef 423/* Chains of all breakpoints defined. */
c906108c
SS
424
425struct breakpoint *breakpoint_chain;
426
876fa593
JK
427/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
428
429static struct bp_location **bp_location;
430
431/* Number of elements of BP_LOCATION. */
432
433static unsigned bp_location_count;
434
4a64f543
MS
435/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
436 ADDRESS for the current elements of BP_LOCATION which get a valid
437 result from bp_location_has_shadow. You can use it for roughly
438 limiting the subrange of BP_LOCATION to scan for shadow bytes for
439 an address you need to read. */
876fa593
JK
440
441static CORE_ADDR bp_location_placed_address_before_address_max;
442
4a64f543
MS
443/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
444 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
445 BP_LOCATION which get a valid result from bp_location_has_shadow.
446 You can use it for roughly limiting the subrange of BP_LOCATION to
447 scan for shadow bytes for an address you need to read. */
876fa593
JK
448
449static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 450
4a64f543
MS
451/* The locations that no longer correspond to any breakpoint, unlinked
452 from bp_location array, but for which a hit may still be reported
453 by a target. */
20874c92
VP
454VEC(bp_location_p) *moribund_locations = NULL;
455
c906108c
SS
456/* Number of last breakpoint made. */
457
95a42b64
TT
458static int breakpoint_count;
459
86b17b60
PA
460/* The value of `breakpoint_count' before the last command that
461 created breakpoints. If the last (break-like) command created more
462 than one breakpoint, then the difference between BREAKPOINT_COUNT
463 and PREV_BREAKPOINT_COUNT is more than one. */
464static int prev_breakpoint_count;
c906108c 465
1042e4c0
SS
466/* Number of last tracepoint made. */
467
95a42b64 468static int tracepoint_count;
1042e4c0 469
6149aea9
PA
470static struct cmd_list_element *breakpoint_set_cmdlist;
471static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 472struct cmd_list_element *save_cmdlist;
6149aea9 473
468d015d
JJ
474/* Return whether a breakpoint is an active enabled breakpoint. */
475static int
476breakpoint_enabled (struct breakpoint *b)
477{
0d381245 478 return (b->enable_state == bp_enabled);
468d015d
JJ
479}
480
c906108c
SS
481/* Set breakpoint count to NUM. */
482
95a42b64 483static void
fba45db2 484set_breakpoint_count (int num)
c906108c 485{
86b17b60 486 prev_breakpoint_count = breakpoint_count;
c906108c 487 breakpoint_count = num;
4fa62494 488 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
489}
490
86b17b60
PA
491/* Used by `start_rbreak_breakpoints' below, to record the current
492 breakpoint count before "rbreak" creates any breakpoint. */
493static int rbreak_start_breakpoint_count;
494
95a42b64
TT
495/* Called at the start an "rbreak" command to record the first
496 breakpoint made. */
86b17b60 497
95a42b64
TT
498void
499start_rbreak_breakpoints (void)
500{
86b17b60 501 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
502}
503
504/* Called at the end of an "rbreak" command to record the last
505 breakpoint made. */
86b17b60 506
95a42b64
TT
507void
508end_rbreak_breakpoints (void)
509{
86b17b60 510 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
511}
512
4a64f543 513/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
514
515void
fba45db2 516clear_breakpoint_hit_counts (void)
c906108c
SS
517{
518 struct breakpoint *b;
519
520 ALL_BREAKPOINTS (b)
521 b->hit_count = 0;
522}
523
9add0f1b
TT
524/* Allocate a new counted_command_line with reference count of 1.
525 The new structure owns COMMANDS. */
526
527static struct counted_command_line *
528alloc_counted_command_line (struct command_line *commands)
529{
530 struct counted_command_line *result
531 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 532
9add0f1b
TT
533 result->refc = 1;
534 result->commands = commands;
535 return result;
536}
537
538/* Increment reference count. This does nothing if CMD is NULL. */
539
540static void
541incref_counted_command_line (struct counted_command_line *cmd)
542{
543 if (cmd)
544 ++cmd->refc;
545}
546
547/* Decrement reference count. If the reference count reaches 0,
548 destroy the counted_command_line. Sets *CMDP to NULL. This does
549 nothing if *CMDP is NULL. */
550
551static void
552decref_counted_command_line (struct counted_command_line **cmdp)
553{
554 if (*cmdp)
555 {
556 if (--(*cmdp)->refc == 0)
557 {
558 free_command_lines (&(*cmdp)->commands);
559 xfree (*cmdp);
560 }
561 *cmdp = NULL;
562 }
563}
564
565/* A cleanup function that calls decref_counted_command_line. */
566
567static void
568do_cleanup_counted_command_line (void *arg)
569{
570 decref_counted_command_line (arg);
571}
572
573/* Create a cleanup that calls decref_counted_command_line on the
574 argument. */
575
576static struct cleanup *
577make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
578{
579 return make_cleanup (do_cleanup_counted_command_line, cmdp);
580}
581
c906108c 582\f
48cb2d85
VP
583/* Return the breakpoint with the specified number, or NULL
584 if the number does not refer to an existing breakpoint. */
585
586struct breakpoint *
587get_breakpoint (int num)
588{
589 struct breakpoint *b;
590
591 ALL_BREAKPOINTS (b)
592 if (b->number == num)
593 return b;
594
595 return NULL;
596}
5c44784c 597
c906108c 598\f
adc36818
PM
599
600void
601set_breakpoint_condition (struct breakpoint *b, char *exp,
602 int from_tty)
603{
3a5c3e22
PA
604 xfree (b->cond_string);
605 b->cond_string = NULL;
adc36818 606
3a5c3e22 607 if (is_watchpoint (b))
adc36818 608 {
3a5c3e22
PA
609 struct watchpoint *w = (struct watchpoint *) b;
610
611 xfree (w->cond_exp);
612 w->cond_exp = NULL;
613 }
614 else
615 {
616 struct bp_location *loc;
617
618 for (loc = b->loc; loc; loc = loc->next)
619 {
620 xfree (loc->cond);
621 loc->cond = NULL;
622 }
adc36818 623 }
adc36818
PM
624
625 if (*exp == 0)
626 {
627 if (from_tty)
628 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
629 }
630 else
631 {
632 char *arg = exp;
cc59ec59 633
adc36818
PM
634 /* I don't know if it matters whether this is the string the user
635 typed in or the decompiled expression. */
636 b->cond_string = xstrdup (arg);
637 b->condition_not_parsed = 0;
638
639 if (is_watchpoint (b))
640 {
3a5c3e22
PA
641 struct watchpoint *w = (struct watchpoint *) b;
642
adc36818
PM
643 innermost_block = NULL;
644 arg = exp;
3a5c3e22 645 w->cond_exp = parse_exp_1 (&arg, 0, 0);
adc36818
PM
646 if (*arg)
647 error (_("Junk at end of expression"));
3a5c3e22 648 w->cond_exp_valid_block = innermost_block;
adc36818
PM
649 }
650 else
651 {
3a5c3e22
PA
652 struct bp_location *loc;
653
adc36818
PM
654 for (loc = b->loc; loc; loc = loc->next)
655 {
656 arg = exp;
657 loc->cond =
658 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
659 if (*arg)
660 error (_("Junk at end of expression"));
661 }
662 }
663 }
664 breakpoints_changed ();
8d3788bd 665 observer_notify_breakpoint_modified (b);
adc36818
PM
666}
667
c906108c
SS
668/* condition N EXP -- set break condition of breakpoint N to EXP. */
669
670static void
fba45db2 671condition_command (char *arg, int from_tty)
c906108c 672{
52f0bd74 673 struct breakpoint *b;
c906108c 674 char *p;
52f0bd74 675 int bnum;
c906108c
SS
676
677 if (arg == 0)
e2e0b3e5 678 error_no_arg (_("breakpoint number"));
c906108c
SS
679
680 p = arg;
681 bnum = get_number (&p);
5c44784c 682 if (bnum == 0)
8a3fe4f8 683 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
684
685 ALL_BREAKPOINTS (b)
686 if (b->number == bnum)
2f069f6f 687 {
7371cf6d
PM
688 /* Check if this breakpoint has a Python object assigned to
689 it, and if it has a definition of the "stop"
690 method. This method and conditions entered into GDB from
691 the CLI are mutually exclusive. */
692 if (b->py_bp_object
693 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
694 error (_("Cannot set a condition where a Python 'stop' "
695 "method has been defined in the breakpoint."));
2566ad2d 696 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
697 return;
698 }
c906108c 699
8a3fe4f8 700 error (_("No breakpoint number %d."), bnum);
c906108c
SS
701}
702
a7bdde9e
VP
703/* Check that COMMAND do not contain commands that are suitable
704 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
705 Throw if any such commands is found. */
706
a7bdde9e
VP
707static void
708check_no_tracepoint_commands (struct command_line *commands)
709{
710 struct command_line *c;
cc59ec59 711
a7bdde9e
VP
712 for (c = commands; c; c = c->next)
713 {
714 int i;
715
716 if (c->control_type == while_stepping_control)
3e43a32a
MS
717 error (_("The 'while-stepping' command can "
718 "only be used for tracepoints"));
a7bdde9e
VP
719
720 for (i = 0; i < c->body_count; ++i)
721 check_no_tracepoint_commands ((c->body_list)[i]);
722
723 /* Not that command parsing removes leading whitespace and comment
4a64f543 724 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
725 command directly. */
726 if (strstr (c->line, "collect ") == c->line)
727 error (_("The 'collect' command can only be used for tracepoints"));
728
51661e93
VP
729 if (strstr (c->line, "teval ") == c->line)
730 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
731 }
732}
733
d77f58be
SS
734/* Encapsulate tests for different types of tracepoints. */
735
d9b3f62e
PA
736static int
737is_tracepoint_type (enum bptype type)
738{
739 return (type == bp_tracepoint
740 || type == bp_fast_tracepoint
741 || type == bp_static_tracepoint);
742}
743
a7bdde9e 744int
d77f58be 745is_tracepoint (const struct breakpoint *b)
a7bdde9e 746{
d9b3f62e 747 return is_tracepoint_type (b->type);
a7bdde9e 748}
d9b3f62e 749
e5dd4106 750/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
751 breakpoint. This function will throw an exception if a problem is
752 found. */
48cb2d85 753
95a42b64
TT
754static void
755validate_commands_for_breakpoint (struct breakpoint *b,
756 struct command_line *commands)
48cb2d85 757{
d77f58be 758 if (is_tracepoint (b))
a7bdde9e 759 {
4a64f543
MS
760 /* We need to verify that each top-level element of commands is
761 valid for tracepoints, that there's at most one
762 while-stepping element, and that while-stepping's body has
763 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
764 struct command_line *c;
765 struct command_line *while_stepping = 0;
766 for (c = commands; c; c = c->next)
767 {
a7bdde9e
VP
768 if (c->control_type == while_stepping_control)
769 {
770 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
771 error (_("The 'while-stepping' command "
772 "cannot be used for fast tracepoint"));
0fb4aa4b 773 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
774 error (_("The 'while-stepping' command "
775 "cannot be used for static tracepoint"));
a7bdde9e
VP
776
777 if (while_stepping)
3e43a32a
MS
778 error (_("The 'while-stepping' command "
779 "can be used only once"));
a7bdde9e
VP
780 else
781 while_stepping = c;
782 }
783 }
784 if (while_stepping)
785 {
786 struct command_line *c2;
787
788 gdb_assert (while_stepping->body_count == 1);
789 c2 = while_stepping->body_list[0];
790 for (; c2; c2 = c2->next)
791 {
a7bdde9e
VP
792 if (c2->control_type == while_stepping_control)
793 error (_("The 'while-stepping' command cannot be nested"));
794 }
795 }
796 }
797 else
798 {
799 check_no_tracepoint_commands (commands);
800 }
95a42b64
TT
801}
802
0fb4aa4b
PA
803/* Return a vector of all the static tracepoints set at ADDR. The
804 caller is responsible for releasing the vector. */
805
806VEC(breakpoint_p) *
807static_tracepoints_here (CORE_ADDR addr)
808{
809 struct breakpoint *b;
810 VEC(breakpoint_p) *found = 0;
811 struct bp_location *loc;
812
813 ALL_BREAKPOINTS (b)
814 if (b->type == bp_static_tracepoint)
815 {
816 for (loc = b->loc; loc; loc = loc->next)
817 if (loc->address == addr)
818 VEC_safe_push(breakpoint_p, found, b);
819 }
820
821 return found;
822}
823
95a42b64 824/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 825 validate that only allowed commands are included. */
95a42b64
TT
826
827void
4a64f543
MS
828breakpoint_set_commands (struct breakpoint *b,
829 struct command_line *commands)
95a42b64
TT
830{
831 validate_commands_for_breakpoint (b, commands);
a7bdde9e 832
9add0f1b
TT
833 decref_counted_command_line (&b->commands);
834 b->commands = alloc_counted_command_line (commands);
48cb2d85 835 breakpoints_changed ();
8d3788bd 836 observer_notify_breakpoint_modified (b);
48cb2d85
VP
837}
838
45a43567
TT
839/* Set the internal `silent' flag on the breakpoint. Note that this
840 is not the same as the "silent" that may appear in the breakpoint's
841 commands. */
842
843void
844breakpoint_set_silent (struct breakpoint *b, int silent)
845{
846 int old_silent = b->silent;
847
848 b->silent = silent;
849 if (old_silent != silent)
8d3788bd 850 observer_notify_breakpoint_modified (b);
45a43567
TT
851}
852
853/* Set the thread for this breakpoint. If THREAD is -1, make the
854 breakpoint work for any thread. */
855
856void
857breakpoint_set_thread (struct breakpoint *b, int thread)
858{
859 int old_thread = b->thread;
860
861 b->thread = thread;
862 if (old_thread != thread)
8d3788bd 863 observer_notify_breakpoint_modified (b);
45a43567
TT
864}
865
866/* Set the task for this breakpoint. If TASK is 0, make the
867 breakpoint work for any task. */
868
869void
870breakpoint_set_task (struct breakpoint *b, int task)
871{
872 int old_task = b->task;
873
874 b->task = task;
875 if (old_task != task)
8d3788bd 876 observer_notify_breakpoint_modified (b);
45a43567
TT
877}
878
95a42b64
TT
879void
880check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
881{
882 struct breakpoint *b = closure;
cc59ec59 883
a7bdde9e
VP
884 validate_actionline (&line, b);
885}
886
95a42b64
TT
887/* A structure used to pass information through
888 map_breakpoint_numbers. */
889
890struct commands_info
891{
892 /* True if the command was typed at a tty. */
893 int from_tty;
86b17b60
PA
894
895 /* The breakpoint range spec. */
896 char *arg;
897
95a42b64
TT
898 /* Non-NULL if the body of the commands are being read from this
899 already-parsed command. */
900 struct command_line *control;
86b17b60 901
95a42b64
TT
902 /* The command lines read from the user, or NULL if they have not
903 yet been read. */
904 struct counted_command_line *cmd;
905};
906
907/* A callback for map_breakpoint_numbers that sets the commands for
908 commands_command. */
909
c906108c 910static void
95a42b64 911do_map_commands_command (struct breakpoint *b, void *data)
c906108c 912{
95a42b64 913 struct commands_info *info = data;
c906108c 914
95a42b64
TT
915 if (info->cmd == NULL)
916 {
917 struct command_line *l;
5c44784c 918
95a42b64
TT
919 if (info->control != NULL)
920 l = copy_command_lines (info->control->body_list[0]);
921 else
86b17b60
PA
922 {
923 struct cleanup *old_chain;
924 char *str;
c5aa993b 925
3e43a32a
MS
926 str = xstrprintf (_("Type commands for breakpoint(s) "
927 "%s, one per line."),
86b17b60
PA
928 info->arg);
929
930 old_chain = make_cleanup (xfree, str);
931
932 l = read_command_lines (str,
933 info->from_tty, 1,
d77f58be 934 (is_tracepoint (b)
86b17b60
PA
935 ? check_tracepoint_command : 0),
936 b);
937
938 do_cleanups (old_chain);
939 }
a7bdde9e 940
95a42b64
TT
941 info->cmd = alloc_counted_command_line (l);
942 }
943
944 /* If a breakpoint was on the list more than once, we don't need to
945 do anything. */
946 if (b->commands != info->cmd)
947 {
948 validate_commands_for_breakpoint (b, info->cmd->commands);
949 incref_counted_command_line (info->cmd);
950 decref_counted_command_line (&b->commands);
951 b->commands = info->cmd;
952 breakpoints_changed ();
8d3788bd 953 observer_notify_breakpoint_modified (b);
c5aa993b 954 }
95a42b64
TT
955}
956
957static void
4a64f543
MS
958commands_command_1 (char *arg, int from_tty,
959 struct command_line *control)
95a42b64
TT
960{
961 struct cleanup *cleanups;
962 struct commands_info info;
963
964 info.from_tty = from_tty;
965 info.control = control;
966 info.cmd = NULL;
967 /* If we read command lines from the user, then `info' will hold an
968 extra reference to the commands that we must clean up. */
969 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
970
971 if (arg == NULL || !*arg)
972 {
86b17b60 973 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
974 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
975 breakpoint_count);
95a42b64
TT
976 else if (breakpoint_count > 0)
977 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
978 else
979 {
980 /* So that we don't try to free the incoming non-NULL
981 argument in the cleanup below. Mapping breakpoint
982 numbers will fail in this case. */
983 arg = NULL;
984 }
95a42b64 985 }
9766ced4
SS
986 else
987 /* The command loop has some static state, so we need to preserve
988 our argument. */
989 arg = xstrdup (arg);
86b17b60
PA
990
991 if (arg != NULL)
992 make_cleanup (xfree, arg);
993
994 info.arg = arg;
95a42b64
TT
995
996 map_breakpoint_numbers (arg, do_map_commands_command, &info);
997
998 if (info.cmd == NULL)
999 error (_("No breakpoints specified."));
1000
1001 do_cleanups (cleanups);
1002}
1003
1004static void
1005commands_command (char *arg, int from_tty)
1006{
1007 commands_command_1 (arg, from_tty, NULL);
c906108c 1008}
40c03ae8
EZ
1009
1010/* Like commands_command, but instead of reading the commands from
1011 input stream, takes them from an already parsed command structure.
1012
1013 This is used by cli-script.c to DTRT with breakpoint commands
1014 that are part of if and while bodies. */
1015enum command_control_type
1016commands_from_control_command (char *arg, struct command_line *cmd)
1017{
95a42b64
TT
1018 commands_command_1 (arg, 0, cmd);
1019 return simple_control;
40c03ae8 1020}
876fa593
JK
1021
1022/* Return non-zero if BL->TARGET_INFO contains valid information. */
1023
1024static int
1025bp_location_has_shadow (struct bp_location *bl)
1026{
1027 if (bl->loc_type != bp_loc_software_breakpoint)
1028 return 0;
1029 if (!bl->inserted)
1030 return 0;
1031 if (bl->target_info.shadow_len == 0)
e5dd4106 1032 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1033 return 0;
1034 return 1;
1035}
1036
8defab1a 1037/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1038 by replacing any memory breakpoints with their shadowed contents.
1039
1040 The range of shadowed area by each bp_location is:
35df4500
TJB
1041 bl->address - bp_location_placed_address_before_address_max
1042 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1043 The range we were requested to resolve shadows for is:
1044 memaddr ... memaddr + len
1045 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1046 memaddr + len <= (bl->address
1047 - bp_location_placed_address_before_address_max)
876fa593 1048 and:
35df4500 1049 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1050
8defab1a
DJ
1051void
1052breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1053{
4a64f543
MS
1054 /* Left boundary, right boundary and median element of our binary
1055 search. */
876fa593
JK
1056 unsigned bc_l, bc_r, bc;
1057
4a64f543
MS
1058 /* Find BC_L which is a leftmost element which may affect BUF
1059 content. It is safe to report lower value but a failure to
1060 report higher one. */
876fa593
JK
1061
1062 bc_l = 0;
1063 bc_r = bp_location_count;
1064 while (bc_l + 1 < bc_r)
1065 {
35df4500 1066 struct bp_location *bl;
876fa593
JK
1067
1068 bc = (bc_l + bc_r) / 2;
35df4500 1069 bl = bp_location[bc];
876fa593 1070
4a64f543
MS
1071 /* Check first BL->ADDRESS will not overflow due to the added
1072 constant. Then advance the left boundary only if we are sure
1073 the BC element can in no way affect the BUF content (MEMADDR
1074 to MEMADDR + LEN range).
876fa593 1075
4a64f543
MS
1076 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1077 offset so that we cannot miss a breakpoint with its shadow
1078 range tail still reaching MEMADDR. */
c5aa993b 1079
35df4500
TJB
1080 if ((bl->address + bp_location_shadow_len_after_address_max
1081 >= bl->address)
1082 && (bl->address + bp_location_shadow_len_after_address_max
1083 <= memaddr))
876fa593
JK
1084 bc_l = bc;
1085 else
1086 bc_r = bc;
1087 }
1088
128070bb
PA
1089 /* Due to the binary search above, we need to make sure we pick the
1090 first location that's at BC_L's address. E.g., if there are
1091 multiple locations at the same address, BC_L may end up pointing
1092 at a duplicate location, and miss the "master"/"inserted"
1093 location. Say, given locations L1, L2 and L3 at addresses A and
1094 B:
1095
1096 L1@A, L2@A, L3@B, ...
1097
1098 BC_L could end up pointing at location L2, while the "master"
1099 location could be L1. Since the `loc->inserted' flag is only set
1100 on "master" locations, we'd forget to restore the shadow of L1
1101 and L2. */
1102 while (bc_l > 0
1103 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1104 bc_l--;
1105
876fa593
JK
1106 /* Now do full processing of the found relevant range of elements. */
1107
1108 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1109 {
35df4500 1110 struct bp_location *bl = bp_location[bc];
876fa593
JK
1111 CORE_ADDR bp_addr = 0;
1112 int bp_size = 0;
1113 int bptoffset = 0;
1114
35df4500
TJB
1115 /* bp_location array has BL->OWNER always non-NULL. */
1116 if (bl->owner->type == bp_none)
8a3fe4f8 1117 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1118 bl->owner->number);
ffce0d52 1119
e5dd4106 1120 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1121 content. */
1122
35df4500
TJB
1123 if (bl->address >= bp_location_placed_address_before_address_max
1124 && memaddr + len <= (bl->address
1125 - bp_location_placed_address_before_address_max))
876fa593
JK
1126 break;
1127
35df4500 1128 if (!bp_location_has_shadow (bl))
c5aa993b 1129 continue;
35df4500 1130 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1131 current_program_space->aspace, 0))
1132 continue;
1133
c5aa993b
JM
1134 /* Addresses and length of the part of the breakpoint that
1135 we need to copy. */
35df4500
TJB
1136 bp_addr = bl->target_info.placed_address;
1137 bp_size = bl->target_info.shadow_len;
8defab1a 1138
c5aa993b
JM
1139 if (bp_addr + bp_size <= memaddr)
1140 /* The breakpoint is entirely before the chunk of memory we
1141 are reading. */
1142 continue;
8defab1a 1143
c5aa993b
JM
1144 if (bp_addr >= memaddr + len)
1145 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1146 reading. */
c5aa993b 1147 continue;
c5aa993b 1148
8defab1a
DJ
1149 /* Offset within shadow_contents. */
1150 if (bp_addr < memaddr)
1151 {
1152 /* Only copy the second part of the breakpoint. */
1153 bp_size -= memaddr - bp_addr;
1154 bptoffset = memaddr - bp_addr;
1155 bp_addr = memaddr;
1156 }
c5aa993b 1157
8defab1a
DJ
1158 if (bp_addr + bp_size > memaddr + len)
1159 {
1160 /* Only copy the first part of the breakpoint. */
1161 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1162 }
c5aa993b 1163
8defab1a 1164 memcpy (buf + bp_addr - memaddr,
35df4500 1165 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1166 }
c906108c 1167}
c906108c 1168\f
c5aa993b 1169
60e1c644
PA
1170/* Return true if BPT is of any hardware watchpoint kind. */
1171
a5606eee 1172static int
d77f58be 1173is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1174{
1175 return (bpt->type == bp_hardware_watchpoint
1176 || bpt->type == bp_read_watchpoint
1177 || bpt->type == bp_access_watchpoint);
1178}
7270d8f2 1179
60e1c644
PA
1180/* Return true if BPT is of any watchpoint kind, hardware or
1181 software. */
1182
3a5c3e22 1183int
d77f58be 1184is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1185{
1186 return (is_hardware_watchpoint (bpt)
1187 || bpt->type == bp_watchpoint);
1188}
1189
3a5c3e22
PA
1190/* Returns true if the current thread and its running state are safe
1191 to evaluate or update watchpoint B. Watchpoints on local
1192 expressions need to be evaluated in the context of the thread that
1193 was current when the watchpoint was created, and, that thread needs
1194 to be stopped to be able to select the correct frame context.
1195 Watchpoints on global expressions can be evaluated on any thread,
1196 and in any state. It is presently left to the target allowing
1197 memory accesses when threads are running. */
f6bc2008
PA
1198
1199static int
3a5c3e22 1200watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008
PA
1201{
1202 return (ptid_equal (b->watchpoint_thread, null_ptid)
1203 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1204 && !is_executing (inferior_ptid)));
1205}
1206
d0fb5eae
JK
1207/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1208 associated bp_watchpoint_scope breakpoint. */
1209
1210static void
3a5c3e22 1211watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1212{
3a5c3e22 1213 struct breakpoint *b = &w->base;
d0fb5eae
JK
1214
1215 if (b->related_breakpoint != b)
1216 {
1217 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1218 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1219 b->related_breakpoint->disposition = disp_del_at_next_stop;
1220 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1221 b->related_breakpoint = b;
1222 }
1223 b->disposition = disp_del_at_next_stop;
1224}
1225
567e1b4e
JB
1226/* Assuming that B is a watchpoint:
1227 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1228 - Evaluate expression and store the result in B->val
567e1b4e
JB
1229 - Evaluate the condition if there is one, and store the result
1230 in b->loc->cond.
a5606eee
VP
1231 - Update the list of values that must be watched in B->loc.
1232
4a64f543
MS
1233 If the watchpoint disposition is disp_del_at_next_stop, then do
1234 nothing. If this is local watchpoint that is out of scope, delete
1235 it.
1236
1237 Even with `set breakpoint always-inserted on' the watchpoints are
1238 removed + inserted on each stop here. Normal breakpoints must
1239 never be removed because they might be missed by a running thread
1240 when debugging in non-stop mode. On the other hand, hardware
1241 watchpoints (is_hardware_watchpoint; processed here) are specific
1242 to each LWP since they are stored in each LWP's hardware debug
1243 registers. Therefore, such LWP must be stopped first in order to
1244 be able to modify its hardware watchpoints.
1245
1246 Hardware watchpoints must be reset exactly once after being
1247 presented to the user. It cannot be done sooner, because it would
1248 reset the data used to present the watchpoint hit to the user. And
1249 it must not be done later because it could display the same single
1250 watchpoint hit during multiple GDB stops. Note that the latter is
1251 relevant only to the hardware watchpoint types bp_read_watchpoint
1252 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1253 not user-visible - its hit is suppressed if the memory content has
1254 not changed.
1255
1256 The following constraints influence the location where we can reset
1257 hardware watchpoints:
1258
1259 * target_stopped_by_watchpoint and target_stopped_data_address are
1260 called several times when GDB stops.
1261
1262 [linux]
1263 * Multiple hardware watchpoints can be hit at the same time,
1264 causing GDB to stop. GDB only presents one hardware watchpoint
1265 hit at a time as the reason for stopping, and all the other hits
1266 are presented later, one after the other, each time the user
1267 requests the execution to be resumed. Execution is not resumed
1268 for the threads still having pending hit event stored in
1269 LWP_INFO->STATUS. While the watchpoint is already removed from
1270 the inferior on the first stop the thread hit event is kept being
1271 reported from its cached value by linux_nat_stopped_data_address
1272 until the real thread resume happens after the watchpoint gets
1273 presented and thus its LWP_INFO->STATUS gets reset.
1274
1275 Therefore the hardware watchpoint hit can get safely reset on the
1276 watchpoint removal from inferior. */
a79d3c27 1277
b40ce68a 1278static void
3a5c3e22 1279update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1280{
a5606eee 1281 int within_current_scope;
a5606eee 1282 struct frame_id saved_frame_id;
66076460 1283 int frame_saved;
a5606eee 1284
f6bc2008
PA
1285 /* If this is a local watchpoint, we only want to check if the
1286 watchpoint frame is in scope if the current thread is the thread
1287 that was used to create the watchpoint. */
1288 if (!watchpoint_in_thread_scope (b))
1289 return;
1290
3a5c3e22 1291 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1292 return;
1293
66076460 1294 frame_saved = 0;
a5606eee
VP
1295
1296 /* Determine if the watchpoint is within scope. */
1297 if (b->exp_valid_block == NULL)
1298 within_current_scope = 1;
1299 else
1300 {
b5db5dfc
UW
1301 struct frame_info *fi = get_current_frame ();
1302 struct gdbarch *frame_arch = get_frame_arch (fi);
1303 CORE_ADDR frame_pc = get_frame_pc (fi);
1304
1305 /* If we're in a function epilogue, unwinding may not work
1306 properly, so do not attempt to recreate locations at this
1307 point. See similar comments in watchpoint_check. */
1308 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1309 return;
66076460
DJ
1310
1311 /* Save the current frame's ID so we can restore it after
1312 evaluating the watchpoint expression on its own frame. */
1313 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1314 took a frame parameter, so that we didn't have to change the
1315 selected frame. */
1316 frame_saved = 1;
1317 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1318
a5606eee
VP
1319 fi = frame_find_by_id (b->watchpoint_frame);
1320 within_current_scope = (fi != NULL);
1321 if (within_current_scope)
1322 select_frame (fi);
1323 }
1324
b5db5dfc
UW
1325 /* We don't free locations. They are stored in the bp_location array
1326 and update_global_location_list will eventually delete them and
1327 remove breakpoints if needed. */
3a5c3e22 1328 b->base.loc = NULL;
b5db5dfc 1329
a5606eee
VP
1330 if (within_current_scope && reparse)
1331 {
1332 char *s;
d63d0675 1333
a5606eee
VP
1334 if (b->exp)
1335 {
1336 xfree (b->exp);
1337 b->exp = NULL;
1338 }
d63d0675 1339 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1340 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1341 /* If the meaning of expression itself changed, the old value is
1342 no longer relevant. We don't want to report a watchpoint hit
1343 to the user when the old value and the new value may actually
1344 be completely different objects. */
1345 value_free (b->val);
fa4727a6
DJ
1346 b->val = NULL;
1347 b->val_valid = 0;
60e1c644
PA
1348
1349 /* Note that unlike with breakpoints, the watchpoint's condition
1350 expression is stored in the breakpoint object, not in the
1351 locations (re)created below. */
3a5c3e22 1352 if (b->base.cond_string != NULL)
60e1c644
PA
1353 {
1354 if (b->cond_exp != NULL)
1355 {
1356 xfree (b->cond_exp);
1357 b->cond_exp = NULL;
1358 }
1359
3a5c3e22 1360 s = b->base.cond_string;
60e1c644
PA
1361 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1362 }
a5606eee 1363 }
a5606eee
VP
1364
1365 /* If we failed to parse the expression, for example because
1366 it refers to a global variable in a not-yet-loaded shared library,
1367 don't try to insert watchpoint. We don't automatically delete
1368 such watchpoint, though, since failure to parse expression
1369 is different from out-of-scope watchpoint. */
2d134ed3
PA
1370 if ( !target_has_execution)
1371 {
1372 /* Without execution, memory can't change. No use to try and
1373 set watchpoint locations. The watchpoint will be reset when
1374 the target gains execution, through breakpoint_re_set. */
1375 }
1376 else if (within_current_scope && b->exp)
a5606eee 1377 {
0cf6dd15 1378 int pc = 0;
fa4727a6 1379 struct value *val_chain, *v, *result, *next;
2d134ed3 1380 struct program_space *frame_pspace;
a5606eee 1381
0cf6dd15 1382 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1383
a5606eee
VP
1384 /* Avoid setting b->val if it's already set. The meaning of
1385 b->val is 'the last value' user saw, and we should update
1386 it only if we reported that last value to user. As it
9c06b0b4
TJB
1387 happens, the code that reports it updates b->val directly.
1388 We don't keep track of the memory value for masked
1389 watchpoints. */
3a5c3e22 1390 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1391 {
1392 b->val = v;
1393 b->val_valid = 1;
1394 }
a5606eee 1395
2d134ed3
PA
1396 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1397
a5606eee 1398 /* Look at each value on the value chain. */
9fa40276 1399 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1400 {
1401 /* If it's a memory location, and GDB actually needed
1402 its contents to evaluate the expression, then we
fa4727a6
DJ
1403 must watch it. If the first value returned is
1404 still lazy, that means an error occurred reading it;
1405 watch it anyway in case it becomes readable. */
a5606eee 1406 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1407 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1408 {
1409 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1410
a5606eee
VP
1411 /* We only watch structs and arrays if user asked
1412 for it explicitly, never if they just happen to
1413 appear in the middle of some value chain. */
fa4727a6 1414 if (v == result
a5606eee
VP
1415 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1416 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1417 {
1418 CORE_ADDR addr;
1419 int len, type;
1420 struct bp_location *loc, **tmp;
1421
42ae5230 1422 addr = value_address (v);
a5606eee
VP
1423 len = TYPE_LENGTH (value_type (v));
1424 type = hw_write;
3a5c3e22 1425 if (b->base.type == bp_read_watchpoint)
a5606eee 1426 type = hw_read;
3a5c3e22 1427 else if (b->base.type == bp_access_watchpoint)
a5606eee 1428 type = hw_access;
3a5c3e22
PA
1429
1430 loc = allocate_bp_location (&b->base);
1431 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1432 ;
1433 *tmp = loc;
a6d9a66e 1434 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1435
1436 loc->pspace = frame_pspace;
a5606eee
VP
1437 loc->address = addr;
1438 loc->length = len;
1439 loc->watchpoint_type = type;
1440 }
1441 }
9fa40276
TJB
1442 }
1443
1444 /* Change the type of breakpoint between hardware assisted or
1445 an ordinary watchpoint depending on the hardware support
1446 and free hardware slots. REPARSE is set when the inferior
1447 is started. */
a9634178 1448 if (reparse)
9fa40276 1449 {
e09342b5 1450 int reg_cnt;
9fa40276
TJB
1451 enum bp_loc_type loc_type;
1452 struct bp_location *bl;
a5606eee 1453
a9634178 1454 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1455
1456 if (reg_cnt)
9fa40276
TJB
1457 {
1458 int i, target_resources_ok, other_type_used;
a1398e0c 1459 enum bptype type;
9fa40276 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
a1398e0c
PA
1468 this watchpoint in as well. */
1469
1470 /* If this is a software watchpoint, we try to turn it
1471 to a hardware one -- count resources as if B was of
1472 hardware watchpoint type. */
1473 type = b->base.type;
1474 if (type == bp_watchpoint)
1475 type = bp_hardware_watchpoint;
1476
1477 /* This watchpoint may or may not have been placed on
1478 the list yet at this point (it won't be in the list
1479 if we're trying to create it for the first time,
1480 through watch_command), so always account for it
1481 manually. */
1482
1483 /* Count resources used by all watchpoints except B. */
1484 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1485
1486 /* Add in the resources needed for B. */
1487 i += hw_watchpoint_use_count (&b->base);
1488
1489 target_resources_ok
1490 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1491 if (target_resources_ok <= 0)
a9634178 1492 {
3a5c3e22 1493 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1494
1495 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1496 error (_("Target does not support this type of "
1497 "hardware watchpoint."));
9c06b0b4
TJB
1498 else if (target_resources_ok < 0 && !sw_mode)
1499 error (_("There are not enough available hardware "
1500 "resources for this watchpoint."));
a1398e0c
PA
1501
1502 /* Downgrade to software watchpoint. */
1503 b->base.type = bp_watchpoint;
1504 }
1505 else
1506 {
1507 /* If this was a software watchpoint, we've just
1508 found we have enough resources to turn it to a
1509 hardware watchpoint. Otherwise, this is a
1510 nop. */
1511 b->base.type = type;
a9634178 1512 }
9fa40276 1513 }
3a5c3e22 1514 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1515 error (_("Expression cannot be implemented with "
1516 "read/access watchpoint."));
9fa40276 1517 else
3a5c3e22 1518 b->base.type = bp_watchpoint;
9fa40276 1519
3a5c3e22 1520 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1521 : bp_loc_hardware_watchpoint);
3a5c3e22 1522 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1523 bl->loc_type = loc_type;
1524 }
1525
1526 for (v = val_chain; v; v = next)
1527 {
a5606eee
VP
1528 next = value_next (v);
1529 if (v != b->val)
1530 value_free (v);
1531 }
1532
c7437ca6
PA
1533 /* If a software watchpoint is not watching any memory, then the
1534 above left it without any location set up. But,
1535 bpstat_stop_status requires a location to be able to report
1536 stops, so make sure there's at least a dummy one. */
3a5c3e22 1537 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1538 {
3a5c3e22
PA
1539 struct breakpoint *base = &b->base;
1540 base->loc = allocate_bp_location (base);
1541 base->loc->pspace = frame_pspace;
1542 base->loc->address = -1;
1543 base->loc->length = -1;
1544 base->loc->watchpoint_type = -1;
c7437ca6 1545 }
a5606eee
VP
1546 }
1547 else if (!within_current_scope)
7270d8f2 1548 {
ac74f770
MS
1549 printf_filtered (_("\
1550Watchpoint %d deleted because the program has left the block\n\
1551in which its expression is valid.\n"),
3a5c3e22 1552 b->base.number);
d0fb5eae 1553 watchpoint_del_at_next_stop (b);
7270d8f2 1554 }
a5606eee
VP
1555
1556 /* Restore the selected frame. */
66076460
DJ
1557 if (frame_saved)
1558 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1559}
1560
a5606eee 1561
74960c60 1562/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1563 inserted in the inferior. We don't differentiate the type of BL's owner
1564 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1565 breakpoint_ops is not defined, because in insert_bp_location,
1566 tracepoint's insert_location will not be called. */
74960c60 1567static int
35df4500 1568should_be_inserted (struct bp_location *bl)
74960c60 1569{
35df4500 1570 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1571 return 0;
1572
35df4500 1573 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1574 return 0;
1575
35df4500 1576 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1577 return 0;
1578
f8eba3c6
TT
1579 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1580 return 0;
1581
56710373
PA
1582 /* This is set for example, when we're attached to the parent of a
1583 vfork, and have detached from the child. The child is running
1584 free, and we expect it to do an exec or exit, at which point the
1585 OS makes the parent schedulable again (and the target reports
1586 that the vfork is done). Until the child is done with the shared
1587 memory region, do not insert breakpoints in the parent, otherwise
1588 the child could still trip on the parent's breakpoints. Since
1589 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1590 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1591 return 0;
1592
74960c60
VP
1593 return 1;
1594}
1595
934709f0
PW
1596/* Same as should_be_inserted but does the check assuming
1597 that the location is not duplicated. */
1598
1599static int
1600unduplicated_should_be_inserted (struct bp_location *bl)
1601{
1602 int result;
1603 const int save_duplicate = bl->duplicate;
1604
1605 bl->duplicate = 0;
1606 result = should_be_inserted (bl);
1607 bl->duplicate = save_duplicate;
1608 return result;
1609}
1610
35df4500
TJB
1611/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1612 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 1613 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
1614 Returns 0 for success, 1 if the bp_location type is not supported or
1615 -1 for failure.
879bfdc2 1616
4a64f543
MS
1617 NOTE drow/2003-09-09: This routine could be broken down to an
1618 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1619static int
35df4500 1620insert_bp_location (struct bp_location *bl,
26bb91f3 1621 struct ui_file *tmp_error_stream,
3fbb6ffa 1622 int *disabled_breaks,
26bb91f3 1623 int *hw_breakpoint_error)
879bfdc2
DJ
1624{
1625 int val = 0;
1626
35df4500 1627 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1628 return 0;
1629
8181d85f 1630 /* Initialize the target-specific information. */
35df4500
TJB
1631 memset (&bl->target_info, 0, sizeof (bl->target_info));
1632 bl->target_info.placed_address = bl->address;
1633 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1634 bl->target_info.length = bl->length;
8181d85f 1635
35df4500
TJB
1636 if (bl->loc_type == bp_loc_software_breakpoint
1637 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1638 {
35df4500 1639 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1640 {
1641 /* If the explicitly specified breakpoint type
1642 is not hardware breakpoint, check the memory map to see
1643 if the breakpoint address is in read only memory or not.
4a64f543 1644
765dc015
VP
1645 Two important cases are:
1646 - location type is not hardware breakpoint, memory
1647 is readonly. We change the type of the location to
1648 hardware breakpoint.
4a64f543
MS
1649 - location type is hardware breakpoint, memory is
1650 read-write. This means we've previously made the
1651 location hardware one, but then the memory map changed,
1652 so we undo.
765dc015 1653
4a64f543
MS
1654 When breakpoints are removed, remove_breakpoints will use
1655 location types we've just set here, the only possible
1656 problem is that memory map has changed during running
1657 program, but it's not going to work anyway with current
1658 gdb. */
765dc015 1659 struct mem_region *mr
35df4500 1660 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1661
1662 if (mr)
1663 {
1664 if (automatic_hardware_breakpoints)
1665 {
765dc015
VP
1666 enum bp_loc_type new_type;
1667
1668 if (mr->attrib.mode != MEM_RW)
1669 new_type = bp_loc_hardware_breakpoint;
1670 else
1671 new_type = bp_loc_software_breakpoint;
1672
35df4500 1673 if (new_type != bl->loc_type)
765dc015
VP
1674 {
1675 static int said = 0;
cc59ec59 1676
35df4500 1677 bl->loc_type = new_type;
765dc015
VP
1678 if (!said)
1679 {
3e43a32a
MS
1680 fprintf_filtered (gdb_stdout,
1681 _("Note: automatically using "
1682 "hardware breakpoints for "
1683 "read-only addresses.\n"));
765dc015
VP
1684 said = 1;
1685 }
1686 }
1687 }
35df4500 1688 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1689 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1690 warning (_("cannot set software breakpoint "
1691 "at readonly address %s"),
35df4500 1692 paddress (bl->gdbarch, bl->address));
765dc015
VP
1693 }
1694 }
1695
879bfdc2
DJ
1696 /* First check to see if we have to handle an overlay. */
1697 if (overlay_debugging == ovly_off
35df4500
TJB
1698 || bl->section == NULL
1699 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1700 {
1701 /* No overlay handling: just set the breakpoint. */
1702
348d480f 1703 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1704 }
1705 else
1706 {
4a64f543 1707 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1708 Shall we set a breakpoint at the LMA? */
1709 if (!overlay_events_enabled)
1710 {
1711 /* Yes -- overlay event support is not active,
1712 so we must try to set a breakpoint at the LMA.
1713 This will not work for a hardware breakpoint. */
35df4500 1714 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1715 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1716 bl->owner->number);
879bfdc2
DJ
1717 else
1718 {
35df4500
TJB
1719 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1720 bl->section);
879bfdc2 1721 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1722 bl->overlay_target_info = bl->target_info;
1723 bl->overlay_target_info.placed_address = addr;
1724 val = target_insert_breakpoint (bl->gdbarch,
1725 &bl->overlay_target_info);
879bfdc2 1726 if (val != 0)
99361f52 1727 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1728 "Overlay breakpoint %d "
1729 "failed: in ROM?\n",
35df4500 1730 bl->owner->number);
879bfdc2
DJ
1731 }
1732 }
1733 /* Shall we set a breakpoint at the VMA? */
35df4500 1734 if (section_is_mapped (bl->section))
879bfdc2
DJ
1735 {
1736 /* Yes. This overlay section is mapped into memory. */
348d480f 1737 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1738 }
1739 else
1740 {
1741 /* No. This breakpoint will not be inserted.
1742 No error, but do not mark the bp as 'inserted'. */
1743 return 0;
1744 }
1745 }
1746
1747 if (val)
1748 {
1749 /* Can't set the breakpoint. */
35df4500 1750 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1751 {
4a64f543 1752 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1753 val = 0;
35df4500 1754 bl->shlib_disabled = 1;
8d3788bd 1755 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
1756 if (!*disabled_breaks)
1757 {
1758 fprintf_unfiltered (tmp_error_stream,
1759 "Cannot insert breakpoint %d.\n",
1760 bl->owner->number);
1761 fprintf_unfiltered (tmp_error_stream,
1762 "Temporarily disabling shared "
1763 "library breakpoints:\n");
1764 }
1765 *disabled_breaks = 1;
879bfdc2 1766 fprintf_unfiltered (tmp_error_stream,
35df4500 1767 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1768 }
1769 else
879bfdc2 1770 {
35df4500 1771 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1772 {
1773 *hw_breakpoint_error = 1;
3e43a32a
MS
1774 fprintf_unfiltered (tmp_error_stream,
1775 "Cannot insert hardware "
1776 "breakpoint %d.\n",
35df4500 1777 bl->owner->number);
879bfdc2
DJ
1778 }
1779 else
1780 {
1781 fprintf_unfiltered (tmp_error_stream,
1782 "Cannot insert breakpoint %d.\n",
35df4500 1783 bl->owner->number);
879bfdc2
DJ
1784 fprintf_filtered (tmp_error_stream,
1785 "Error accessing memory address ");
35df4500 1786 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1787 tmp_error_stream);
879bfdc2
DJ
1788 fprintf_filtered (tmp_error_stream, ": %s.\n",
1789 safe_strerror (val));
1790 }
1791
1792 }
1793 }
1794 else
35df4500 1795 bl->inserted = 1;
879bfdc2
DJ
1796
1797 return val;
1798 }
1799
35df4500 1800 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1801 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1802 watchpoints. It's not clear that it's necessary... */
35df4500 1803 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1804 {
77b06cd7
TJB
1805 gdb_assert (bl->owner->ops != NULL
1806 && bl->owner->ops->insert_location != NULL);
1807
1808 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1809
1810 /* If trying to set a read-watchpoint, and it turns out it's not
1811 supported, try emulating one with an access watchpoint. */
35df4500 1812 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1813 {
1814 struct bp_location *loc, **loc_temp;
1815
1816 /* But don't try to insert it, if there's already another
1817 hw_access location that would be considered a duplicate
1818 of this one. */
1819 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1820 if (loc != bl
85d721b8 1821 && loc->watchpoint_type == hw_access
35df4500 1822 && watchpoint_locations_match (bl, loc))
85d721b8 1823 {
35df4500
TJB
1824 bl->duplicate = 1;
1825 bl->inserted = 1;
1826 bl->target_info = loc->target_info;
1827 bl->watchpoint_type = hw_access;
85d721b8
PA
1828 val = 0;
1829 break;
1830 }
1831
1832 if (val == 1)
1833 {
77b06cd7
TJB
1834 bl->watchpoint_type = hw_access;
1835 val = bl->owner->ops->insert_location (bl);
1836
1837 if (val)
1838 /* Back to the original value. */
1839 bl->watchpoint_type = hw_read;
85d721b8
PA
1840 }
1841 }
1842
35df4500 1843 bl->inserted = (val == 0);
879bfdc2
DJ
1844 }
1845
35df4500 1846 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1847 {
77b06cd7
TJB
1848 gdb_assert (bl->owner->ops != NULL
1849 && bl->owner->ops->insert_location != NULL);
1850
1851 val = bl->owner->ops->insert_location (bl);
1852 if (val)
1853 {
1854 bl->owner->enable_state = bp_disabled;
1855
1856 if (val == 1)
1857 warning (_("\
1858Error inserting catchpoint %d: Your system does not support this type\n\
1859of catchpoint."), bl->owner->number);
1860 else
1861 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1862 }
1863
1864 bl->inserted = (val == 0);
1640b821
DJ
1865
1866 /* We've already printed an error message if there was a problem
1867 inserting this catchpoint, and we've disabled the catchpoint,
1868 so just return success. */
1869 return 0;
879bfdc2
DJ
1870 }
1871
1872 return 0;
1873}
1874
6c95b8df
PA
1875/* This function is called when program space PSPACE is about to be
1876 deleted. It takes care of updating breakpoints to not reference
1877 PSPACE anymore. */
1878
1879void
1880breakpoint_program_space_exit (struct program_space *pspace)
1881{
1882 struct breakpoint *b, *b_temp;
876fa593 1883 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1884
1885 /* Remove any breakpoint that was set through this program space. */
1886 ALL_BREAKPOINTS_SAFE (b, b_temp)
1887 {
1888 if (b->pspace == pspace)
1889 delete_breakpoint (b);
1890 }
1891
1892 /* Breakpoints set through other program spaces could have locations
1893 bound to PSPACE as well. Remove those. */
876fa593 1894 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1895 {
1896 struct bp_location *tmp;
1897
1898 if (loc->pspace == pspace)
1899 {
2bdf28a0 1900 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1901 if (loc->owner->loc == loc)
1902 loc->owner->loc = loc->next;
1903 else
1904 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1905 if (tmp->next == loc)
1906 {
1907 tmp->next = loc->next;
1908 break;
1909 }
1910 }
1911 }
1912
1913 /* Now update the global location list to permanently delete the
1914 removed locations above. */
1915 update_global_location_list (0);
1916}
1917
74960c60
VP
1918/* Make sure all breakpoints are inserted in inferior.
1919 Throws exception on any error.
1920 A breakpoint that is already inserted won't be inserted
1921 again, so calling this function twice is safe. */
1922void
1923insert_breakpoints (void)
1924{
1925 struct breakpoint *bpt;
1926
1927 ALL_BREAKPOINTS (bpt)
1928 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
1929 {
1930 struct watchpoint *w = (struct watchpoint *) bpt;
1931
1932 update_watchpoint (w, 0 /* don't reparse. */);
1933 }
74960c60 1934
b60e7edf 1935 update_global_location_list (1);
74960c60 1936
c35b1492
PA
1937 /* update_global_location_list does not insert breakpoints when
1938 always_inserted_mode is not enabled. Explicitly insert them
1939 now. */
1940 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1941 insert_breakpoint_locations ();
1942}
1943
c30eee59 1944/* Used when starting or continuing the program. */
c906108c 1945
74960c60
VP
1946static void
1947insert_breakpoint_locations (void)
c906108c 1948{
a5606eee 1949 struct breakpoint *bpt;
35df4500 1950 struct bp_location *bl, **blp_tmp;
e236ba44 1951 int error = 0;
c906108c 1952 int val = 0;
3fbb6ffa 1953 int disabled_breaks = 0;
81d0cc19 1954 int hw_breakpoint_error = 0;
c906108c 1955
81d0cc19 1956 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1957 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1958
81d0cc19
GS
1959 /* Explicitly mark the warning -- this will only be printed if
1960 there was an error. */
1961 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1962
1963 save_current_space_and_thread ();
1964
35df4500 1965 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1966 {
35df4500 1967 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1968 continue;
1969
4a64f543
MS
1970 /* There is no point inserting thread-specific breakpoints if
1971 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1972 has BL->OWNER always non-NULL. */
35df4500
TJB
1973 if (bl->owner->thread != -1
1974 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1975 continue;
1976
35df4500 1977 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1978
1979 /* For targets that support global breakpoints, there's no need
1980 to select an inferior to insert breakpoint to. In fact, even
1981 if we aren't attached to any process yet, we should still
1982 insert breakpoints. */
1983 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1984 && ptid_equal (inferior_ptid, null_ptid))
1985 continue;
1986
3fbb6ffa
TJB
1987 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
1988 &hw_breakpoint_error);
879bfdc2 1989 if (val)
e236ba44 1990 error = val;
879bfdc2 1991 }
c906108c 1992
4a64f543
MS
1993 /* If we failed to insert all locations of a watchpoint, remove
1994 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
1995 ALL_BREAKPOINTS (bpt)
1996 {
1997 int some_failed = 0;
1998 struct bp_location *loc;
1999
2000 if (!is_hardware_watchpoint (bpt))
2001 continue;
2002
d6b74ac4 2003 if (!breakpoint_enabled (bpt))
a5606eee 2004 continue;
74960c60
VP
2005
2006 if (bpt->disposition == disp_del_at_next_stop)
2007 continue;
a5606eee
VP
2008
2009 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2010 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2011 {
2012 some_failed = 1;
2013 break;
2014 }
2015 if (some_failed)
2016 {
2017 for (loc = bpt->loc; loc; loc = loc->next)
2018 if (loc->inserted)
2019 remove_breakpoint (loc, mark_uninserted);
2020
2021 hw_breakpoint_error = 1;
2022 fprintf_unfiltered (tmp_error_stream,
2023 "Could not insert hardware watchpoint %d.\n",
2024 bpt->number);
2025 error = -1;
2026 }
2027 }
2028
e236ba44 2029 if (error)
81d0cc19
GS
2030 {
2031 /* If a hardware breakpoint or watchpoint was inserted, add a
2032 message about possibly exhausted resources. */
879bfdc2 2033 if (hw_breakpoint_error)
81d0cc19 2034 {
c6510018
MS
2035 fprintf_unfiltered (tmp_error_stream,
2036 "Could not insert hardware breakpoints:\n\
2037You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2038 }
81d0cc19
GS
2039 target_terminal_ours_for_output ();
2040 error_stream (tmp_error_stream);
2041 }
f7545552
TT
2042
2043 do_cleanups (cleanups);
c906108c
SS
2044}
2045
c30eee59
TJB
2046/* Used when the program stops.
2047 Returns zero if successful, or non-zero if there was a problem
2048 removing a breakpoint location. */
2049
c906108c 2050int
fba45db2 2051remove_breakpoints (void)
c906108c 2052{
35df4500 2053 struct bp_location *bl, **blp_tmp;
3a1bae8e 2054 int val = 0;
c906108c 2055
35df4500 2056 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2057 {
1e4d1764 2058 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2059 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2060 }
3a1bae8e 2061 return val;
c906108c
SS
2062}
2063
6c95b8df
PA
2064/* Remove breakpoints of process PID. */
2065
2066int
2067remove_breakpoints_pid (int pid)
2068{
35df4500 2069 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2070 int val;
2071 struct inferior *inf = find_inferior_pid (pid);
2072
35df4500 2073 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2074 {
35df4500 2075 if (bl->pspace != inf->pspace)
6c95b8df
PA
2076 continue;
2077
35df4500 2078 if (bl->inserted)
6c95b8df 2079 {
35df4500 2080 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2081 if (val != 0)
2082 return val;
2083 }
2084 }
2085 return 0;
2086}
2087
c906108c 2088int
fba45db2 2089reattach_breakpoints (int pid)
c906108c 2090{
6c95b8df 2091 struct cleanup *old_chain;
35df4500 2092 struct bp_location *bl, **blp_tmp;
c906108c 2093 int val;
86b887df 2094 struct ui_file *tmp_error_stream;
3fbb6ffa 2095 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2096 struct inferior *inf;
2097 struct thread_info *tp;
2098
2099 tp = any_live_thread_of_process (pid);
2100 if (tp == NULL)
2101 return 1;
2102
2103 inf = find_inferior_pid (pid);
2104 old_chain = save_inferior_ptid ();
2105
2106 inferior_ptid = tp->ptid;
a4954f26 2107
86b887df 2108 tmp_error_stream = mem_fileopen ();
a4954f26 2109 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2110
35df4500 2111 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2112 {
35df4500 2113 if (bl->pspace != inf->pspace)
6c95b8df
PA
2114 continue;
2115
35df4500 2116 if (bl->inserted)
c5aa993b 2117 {
35df4500 2118 bl->inserted = 0;
3fbb6ffa 2119 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2120 if (val != 0)
2121 {
ce696e05 2122 do_cleanups (old_chain);
c5aa993b
JM
2123 return val;
2124 }
2125 }
2126 }
ce696e05 2127 do_cleanups (old_chain);
c906108c
SS
2128 return 0;
2129}
2130
e58b0e63
PA
2131static int internal_breakpoint_number = -1;
2132
84f4c1fe
PM
2133/* Set the breakpoint number of B, depending on the value of INTERNAL.
2134 If INTERNAL is non-zero, the breakpoint number will be populated
2135 from internal_breakpoint_number and that variable decremented.
e5dd4106 2136 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2137 breakpoint_count and that value incremented. Internal breakpoints
2138 do not set the internal var bpnum. */
2139static void
2140set_breakpoint_number (int internal, struct breakpoint *b)
2141{
2142 if (internal)
2143 b->number = internal_breakpoint_number--;
2144 else
2145 {
2146 set_breakpoint_count (breakpoint_count + 1);
2147 b->number = breakpoint_count;
2148 }
2149}
2150
e62c965a 2151static struct breakpoint *
a6d9a66e 2152create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2153 CORE_ADDR address, enum bptype type,
c0a91b2b 2154 const struct breakpoint_ops *ops)
e62c965a 2155{
e62c965a
PP
2156 struct symtab_and_line sal;
2157 struct breakpoint *b;
2158
4a64f543 2159 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2160
2161 sal.pc = address;
2162 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2163 sal.pspace = current_program_space;
e62c965a 2164
06edf0c0 2165 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2166 b->number = internal_breakpoint_number--;
2167 b->disposition = disp_donttouch;
2168
2169 return b;
2170}
2171
17450429
PP
2172static const char *const longjmp_names[] =
2173 {
2174 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2175 };
2176#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2177
2178/* Per-objfile data private to breakpoint.c. */
2179struct breakpoint_objfile_data
2180{
2181 /* Minimal symbol for "_ovly_debug_event" (if any). */
2182 struct minimal_symbol *overlay_msym;
2183
2184 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2185 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2186
2187 /* Minimal symbol for "std::terminate()" (if any). */
2188 struct minimal_symbol *terminate_msym;
2189
2190 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2191 struct minimal_symbol *exception_msym;
2192};
2193
2194static const struct objfile_data *breakpoint_objfile_key;
2195
2196/* Minimal symbol not found sentinel. */
2197static struct minimal_symbol msym_not_found;
2198
2199/* Returns TRUE if MSYM point to the "not found" sentinel. */
2200
2201static int
2202msym_not_found_p (const struct minimal_symbol *msym)
2203{
2204 return msym == &msym_not_found;
2205}
2206
2207/* Return per-objfile data needed by breakpoint.c.
2208 Allocate the data if necessary. */
2209
2210static struct breakpoint_objfile_data *
2211get_breakpoint_objfile_data (struct objfile *objfile)
2212{
2213 struct breakpoint_objfile_data *bp_objfile_data;
2214
2215 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2216 if (bp_objfile_data == NULL)
2217 {
2218 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2219 sizeof (*bp_objfile_data));
2220
2221 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2222 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2223 }
2224 return bp_objfile_data;
2225}
2226
e62c965a 2227static void
af02033e 2228create_overlay_event_breakpoint (void)
e62c965a 2229{
69de3c6a 2230 struct objfile *objfile;
af02033e 2231 const char *const func_name = "_ovly_debug_event";
e62c965a 2232
69de3c6a
PP
2233 ALL_OBJFILES (objfile)
2234 {
2235 struct breakpoint *b;
17450429
PP
2236 struct breakpoint_objfile_data *bp_objfile_data;
2237 CORE_ADDR addr;
69de3c6a 2238
17450429
PP
2239 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2240
2241 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2242 continue;
2243
2244 if (bp_objfile_data->overlay_msym == NULL)
2245 {
2246 struct minimal_symbol *m;
2247
2248 m = lookup_minimal_symbol_text (func_name, objfile);
2249 if (m == NULL)
2250 {
2251 /* Avoid future lookups in this objfile. */
2252 bp_objfile_data->overlay_msym = &msym_not_found;
2253 continue;
2254 }
2255 bp_objfile_data->overlay_msym = m;
2256 }
e62c965a 2257
17450429
PP
2258 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2259 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2260 bp_overlay_event,
2261 &internal_breakpoint_ops);
69de3c6a 2262 b->addr_string = xstrdup (func_name);
e62c965a 2263
69de3c6a
PP
2264 if (overlay_debugging == ovly_auto)
2265 {
2266 b->enable_state = bp_enabled;
2267 overlay_events_enabled = 1;
2268 }
2269 else
2270 {
2271 b->enable_state = bp_disabled;
2272 overlay_events_enabled = 0;
2273 }
e62c965a
PP
2274 }
2275 update_global_location_list (1);
2276}
2277
0fd8e87f 2278static void
af02033e 2279create_longjmp_master_breakpoint (void)
0fd8e87f 2280{
6c95b8df 2281 struct program_space *pspace;
6c95b8df
PA
2282 struct cleanup *old_chain;
2283
2284 old_chain = save_current_program_space ();
0fd8e87f 2285
6c95b8df 2286 ALL_PSPACES (pspace)
af02033e
PP
2287 {
2288 struct objfile *objfile;
2289
2290 set_current_program_space (pspace);
2291
2292 ALL_OBJFILES (objfile)
0fd8e87f 2293 {
af02033e
PP
2294 int i;
2295 struct gdbarch *gdbarch;
17450429 2296 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2297
af02033e
PP
2298 gdbarch = get_objfile_arch (objfile);
2299 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2300 continue;
2301
17450429
PP
2302 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2303
2304 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2305 {
2306 struct breakpoint *b;
af02033e 2307 const char *func_name;
17450429 2308 CORE_ADDR addr;
6c95b8df 2309
17450429 2310 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2311 continue;
0fd8e87f 2312
17450429
PP
2313 func_name = longjmp_names[i];
2314 if (bp_objfile_data->longjmp_msym[i] == NULL)
2315 {
2316 struct minimal_symbol *m;
2317
2318 m = lookup_minimal_symbol_text (func_name, objfile);
2319 if (m == NULL)
2320 {
2321 /* Prevent future lookups in this objfile. */
2322 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2323 continue;
2324 }
2325 bp_objfile_data->longjmp_msym[i] = m;
2326 }
2327
2328 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2329 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2330 &internal_breakpoint_ops);
af02033e
PP
2331 b->addr_string = xstrdup (func_name);
2332 b->enable_state = bp_disabled;
2333 }
0fd8e87f 2334 }
af02033e 2335 }
0fd8e87f 2336 update_global_location_list (1);
6c95b8df
PA
2337
2338 do_cleanups (old_chain);
0fd8e87f
UW
2339}
2340
af02033e 2341/* Create a master std::terminate breakpoint. */
aa7d318d 2342static void
af02033e 2343create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2344{
2345 struct program_space *pspace;
aa7d318d 2346 struct cleanup *old_chain;
af02033e 2347 const char *const func_name = "std::terminate()";
aa7d318d
TT
2348
2349 old_chain = save_current_program_space ();
2350
2351 ALL_PSPACES (pspace)
17450429
PP
2352 {
2353 struct objfile *objfile;
2354 CORE_ADDR addr;
2355
2356 set_current_program_space (pspace);
2357
aa7d318d
TT
2358 ALL_OBJFILES (objfile)
2359 {
2360 struct breakpoint *b;
17450429 2361 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2362
17450429 2363 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2364
17450429
PP
2365 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2366 continue;
2367
2368 if (bp_objfile_data->terminate_msym == NULL)
2369 {
2370 struct minimal_symbol *m;
2371
2372 m = lookup_minimal_symbol (func_name, NULL, objfile);
2373 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2374 && MSYMBOL_TYPE (m) != mst_file_text))
2375 {
2376 /* Prevent future lookups in this objfile. */
2377 bp_objfile_data->terminate_msym = &msym_not_found;
2378 continue;
2379 }
2380 bp_objfile_data->terminate_msym = m;
2381 }
aa7d318d 2382
17450429
PP
2383 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2384 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2385 bp_std_terminate_master,
2386 &internal_breakpoint_ops);
aa7d318d
TT
2387 b->addr_string = xstrdup (func_name);
2388 b->enable_state = bp_disabled;
2389 }
17450429
PP
2390 }
2391
aa7d318d
TT
2392 update_global_location_list (1);
2393
2394 do_cleanups (old_chain);
2395}
2396
186c406b
TT
2397/* Install a master breakpoint on the unwinder's debug hook. */
2398
2399void
2400create_exception_master_breakpoint (void)
2401{
2402 struct objfile *objfile;
17450429 2403 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2404
2405 ALL_OBJFILES (objfile)
2406 {
17450429
PP
2407 struct breakpoint *b;
2408 struct gdbarch *gdbarch;
2409 struct breakpoint_objfile_data *bp_objfile_data;
2410 CORE_ADDR addr;
2411
2412 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2413
2414 if (msym_not_found_p (bp_objfile_data->exception_msym))
2415 continue;
2416
2417 gdbarch = get_objfile_arch (objfile);
186c406b 2418
17450429 2419 if (bp_objfile_data->exception_msym == NULL)
186c406b 2420 {
17450429 2421 struct minimal_symbol *debug_hook;
186c406b 2422
17450429
PP
2423 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2424 if (debug_hook == NULL)
2425 {
2426 bp_objfile_data->exception_msym = &msym_not_found;
2427 continue;
2428 }
2429
2430 bp_objfile_data->exception_msym = debug_hook;
186c406b 2431 }
17450429
PP
2432
2433 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2434 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2435 &current_target);
06edf0c0
PA
2436 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2437 &internal_breakpoint_ops);
17450429
PP
2438 b->addr_string = xstrdup (func_name);
2439 b->enable_state = bp_disabled;
186c406b
TT
2440 }
2441
2442 update_global_location_list (1);
2443}
2444
c906108c 2445void
fba45db2 2446update_breakpoints_after_exec (void)
c906108c 2447{
35df4500 2448 struct breakpoint *b, *b_tmp;
876fa593 2449 struct bp_location *bploc, **bplocp_tmp;
c906108c 2450
25b22b0a
PA
2451 /* We're about to delete breakpoints from GDB's lists. If the
2452 INSERTED flag is true, GDB will try to lift the breakpoints by
2453 writing the breakpoints' "shadow contents" back into memory. The
2454 "shadow contents" are NOT valid after an exec, so GDB should not
2455 do that. Instead, the target is responsible from marking
2456 breakpoints out as soon as it detects an exec. We don't do that
2457 here instead, because there may be other attempts to delete
2458 breakpoints after detecting an exec and before reaching here. */
876fa593 2459 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2460 if (bploc->pspace == current_program_space)
2461 gdb_assert (!bploc->inserted);
c906108c 2462
35df4500 2463 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2464 {
6c95b8df
PA
2465 if (b->pspace != current_program_space)
2466 continue;
2467
4a64f543 2468 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2469 if (b->type == bp_shlib_event)
2470 {
2471 delete_breakpoint (b);
2472 continue;
2473 }
c906108c 2474
4a64f543 2475 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2476 if (b->type == bp_jit_event)
2477 {
2478 delete_breakpoint (b);
2479 continue;
2480 }
2481
1900040c 2482 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2483 as must overlay event and longjmp master breakpoints. */
2484 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2485 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2486 || b->type == bp_exception_master)
c4093a6a
JM
2487 {
2488 delete_breakpoint (b);
2489 continue;
2490 }
2491
4a64f543 2492 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 2493 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
2494 {
2495 delete_breakpoint (b);
2496 continue;
2497 }
2498
611c83ae
PA
2499 /* Longjmp and longjmp-resume breakpoints are also meaningless
2500 after an exec. */
186c406b
TT
2501 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2502 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2503 {
2504 delete_breakpoint (b);
2505 continue;
2506 }
2507
ce78b96d
JB
2508 if (b->type == bp_catchpoint)
2509 {
2510 /* For now, none of the bp_catchpoint breakpoints need to
2511 do anything at this point. In the future, if some of
2512 the catchpoints need to something, we will need to add
2513 a new method, and call this method from here. */
2514 continue;
2515 }
2516
c5aa993b
JM
2517 /* bp_finish is a special case. The only way we ought to be able
2518 to see one of these when an exec() has happened, is if the user
2519 caught a vfork, and then said "finish". Ordinarily a finish just
2520 carries them to the call-site of the current callee, by setting
2521 a temporary bp there and resuming. But in this case, the finish
2522 will carry them entirely through the vfork & exec.
2523
2524 We don't want to allow a bp_finish to remain inserted now. But
2525 we can't safely delete it, 'cause finish_command has a handle to
2526 the bp on a bpstat, and will later want to delete it. There's a
2527 chance (and I've seen it happen) that if we delete the bp_finish
2528 here, that its storage will get reused by the time finish_command
2529 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2530 We really must allow finish_command to delete a bp_finish.
2531
e5dd4106 2532 In the absence of a general solution for the "how do we know
53a5351d
JM
2533 it's safe to delete something others may have handles to?"
2534 problem, what we'll do here is just uninsert the bp_finish, and
2535 let finish_command delete it.
2536
2537 (We know the bp_finish is "doomed" in the sense that it's
2538 momentary, and will be deleted as soon as finish_command sees
2539 the inferior stopped. So it doesn't matter that the bp's
2540 address is probably bogus in the new a.out, unlike e.g., the
2541 solib breakpoints.) */
c5aa993b 2542
c5aa993b
JM
2543 if (b->type == bp_finish)
2544 {
2545 continue;
2546 }
2547
2548 /* Without a symbolic address, we have little hope of the
2549 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2550 a.out. */
c5aa993b
JM
2551 if (b->addr_string == NULL)
2552 {
2553 delete_breakpoint (b);
2554 continue;
2555 }
c5aa993b 2556 }
1900040c 2557 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2558 create_overlay_event_breakpoint ();
2559 create_longjmp_master_breakpoint ();
2560 create_std_terminate_master_breakpoint ();
186c406b 2561 create_exception_master_breakpoint ();
c906108c
SS
2562}
2563
2564int
fba45db2 2565detach_breakpoints (int pid)
c906108c 2566{
35df4500 2567 struct bp_location *bl, **blp_tmp;
3a1bae8e 2568 int val = 0;
ce696e05 2569 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2570 struct inferior *inf = current_inferior ();
c5aa993b 2571
39f77062 2572 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2573 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2574
6c95b8df 2575 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2576 inferior_ptid = pid_to_ptid (pid);
35df4500 2577 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2578 {
35df4500 2579 if (bl->pspace != inf->pspace)
6c95b8df
PA
2580 continue;
2581
35df4500
TJB
2582 if (bl->inserted)
2583 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2584 }
d03285ec
UW
2585
2586 /* Detach single-step breakpoints as well. */
2587 detach_single_step_breakpoints ();
2588
ce696e05 2589 do_cleanups (old_chain);
3a1bae8e 2590 return val;
c906108c
SS
2591}
2592
35df4500 2593/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2594 Note that this is used to detach breakpoints from a child fork.
2595 When we get here, the child isn't in the inferior list, and neither
2596 do we have objects to represent its address space --- we should
35df4500 2597 *not* look at bl->pspace->aspace here. */
6c95b8df 2598
c906108c 2599static int
35df4500 2600remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2601{
2602 int val;
c5aa993b 2603
35df4500
TJB
2604 /* BL is never in moribund_locations by our callers. */
2605 gdb_assert (bl->owner != NULL);
2bdf28a0 2606
35df4500 2607 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2608 /* Permanent breakpoints cannot be inserted or removed. */
2609 return 0;
2610
74960c60
VP
2611 /* The type of none suggests that owner is actually deleted.
2612 This should not ever happen. */
35df4500 2613 gdb_assert (bl->owner->type != bp_none);
0bde7532 2614
35df4500
TJB
2615 if (bl->loc_type == bp_loc_software_breakpoint
2616 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2617 {
c02f5703
MS
2618 /* "Normal" instruction breakpoint: either the standard
2619 trap-instruction bp (bp_breakpoint), or a
2620 bp_hardware_breakpoint. */
2621
2622 /* First check to see if we have to handle an overlay. */
2623 if (overlay_debugging == ovly_off
35df4500
TJB
2624 || bl->section == NULL
2625 || !(section_is_overlay (bl->section)))
c02f5703
MS
2626 {
2627 /* No overlay handling: just remove the breakpoint. */
348d480f 2628 val = bl->owner->ops->remove_location (bl);
c02f5703 2629 }
c906108c
SS
2630 else
2631 {
4a64f543 2632 /* This breakpoint is in an overlay section.
c02f5703
MS
2633 Did we set a breakpoint at the LMA? */
2634 if (!overlay_events_enabled)
2635 {
2636 /* Yes -- overlay event support is not active, so we
2637 should have set a breakpoint at the LMA. Remove it.
2638 */
c02f5703
MS
2639 /* Ignore any failures: if the LMA is in ROM, we will
2640 have already warned when we failed to insert it. */
35df4500
TJB
2641 if (bl->loc_type == bp_loc_hardware_breakpoint)
2642 target_remove_hw_breakpoint (bl->gdbarch,
2643 &bl->overlay_target_info);
c02f5703 2644 else
35df4500
TJB
2645 target_remove_breakpoint (bl->gdbarch,
2646 &bl->overlay_target_info);
c02f5703
MS
2647 }
2648 /* Did we set a breakpoint at the VMA?
2649 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2650 if (bl->inserted)
c906108c 2651 {
c02f5703
MS
2652 /* Yes -- remove it. Previously we did not bother to
2653 remove the breakpoint if the section had been
2654 unmapped, but let's not rely on that being safe. We
2655 don't know what the overlay manager might do. */
aa67235e
UW
2656
2657 /* However, we should remove *software* breakpoints only
2658 if the section is still mapped, or else we overwrite
2659 wrong code with the saved shadow contents. */
348d480f
PA
2660 if (bl->loc_type == bp_loc_hardware_breakpoint
2661 || section_is_mapped (bl->section))
2662 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
2663 else
2664 val = 0;
c906108c 2665 }
c02f5703
MS
2666 else
2667 {
2668 /* No -- not inserted, so no need to remove. No error. */
2669 val = 0;
2670 }
c906108c 2671 }
879d1e6b
UW
2672
2673 /* In some cases, we might not be able to remove a breakpoint
2674 in a shared library that has already been removed, but we
2675 have not yet processed the shlib unload event. */
35df4500 2676 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2677 val = 0;
2678
c906108c
SS
2679 if (val)
2680 return val;
35df4500 2681 bl->inserted = (is == mark_inserted);
c906108c 2682 }
35df4500 2683 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2684 {
77b06cd7
TJB
2685 gdb_assert (bl->owner->ops != NULL
2686 && bl->owner->ops->remove_location != NULL);
2687
35df4500 2688 bl->inserted = (is == mark_inserted);
77b06cd7 2689 bl->owner->ops->remove_location (bl);
2e70b7b9 2690
c906108c 2691 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2692 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2693 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2694 bl->owner->number);
c906108c 2695 }
35df4500
TJB
2696 else if (bl->owner->type == bp_catchpoint
2697 && breakpoint_enabled (bl->owner)
2698 && !bl->duplicate)
ce78b96d 2699 {
77b06cd7
TJB
2700 gdb_assert (bl->owner->ops != NULL
2701 && bl->owner->ops->remove_location != NULL);
ce78b96d 2702
77b06cd7 2703 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2704 if (val)
2705 return val;
77b06cd7 2706
35df4500 2707 bl->inserted = (is == mark_inserted);
ce78b96d 2708 }
c906108c
SS
2709
2710 return 0;
2711}
2712
6c95b8df 2713static int
35df4500 2714remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2715{
2716 int ret;
2717 struct cleanup *old_chain;
2718
35df4500
TJB
2719 /* BL is never in moribund_locations by our callers. */
2720 gdb_assert (bl->owner != NULL);
2bdf28a0 2721
35df4500 2722 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2723 /* Permanent breakpoints cannot be inserted or removed. */
2724 return 0;
2725
2726 /* The type of none suggests that owner is actually deleted.
2727 This should not ever happen. */
35df4500 2728 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2729
2730 old_chain = save_current_space_and_thread ();
2731
35df4500 2732 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2733
35df4500 2734 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2735
2736 do_cleanups (old_chain);
2737 return ret;
2738}
2739
c906108c
SS
2740/* Clear the "inserted" flag in all breakpoints. */
2741
25b22b0a 2742void
fba45db2 2743mark_breakpoints_out (void)
c906108c 2744{
35df4500 2745 struct bp_location *bl, **blp_tmp;
c906108c 2746
35df4500
TJB
2747 ALL_BP_LOCATIONS (bl, blp_tmp)
2748 if (bl->pspace == current_program_space)
2749 bl->inserted = 0;
c906108c
SS
2750}
2751
53a5351d
JM
2752/* Clear the "inserted" flag in all breakpoints and delete any
2753 breakpoints which should go away between runs of the program.
c906108c
SS
2754
2755 Plus other such housekeeping that has to be done for breakpoints
2756 between runs.
2757
53a5351d
JM
2758 Note: this function gets called at the end of a run (by
2759 generic_mourn_inferior) and when a run begins (by
4a64f543 2760 init_wait_for_inferior). */
c906108c
SS
2761
2762
2763
2764void
fba45db2 2765breakpoint_init_inferior (enum inf_context context)
c906108c 2766{
35df4500
TJB
2767 struct breakpoint *b, *b_tmp;
2768 struct bp_location *bl, **blp_tmp;
1c5cfe86 2769 int ix;
6c95b8df 2770 struct program_space *pspace = current_program_space;
c906108c 2771
50c71eaf
PA
2772 /* If breakpoint locations are shared across processes, then there's
2773 nothing to do. */
2567c7d9 2774 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2775 return;
2776
35df4500 2777 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2778 {
35df4500
TJB
2779 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2780 if (bl->pspace == pspace
2781 && bl->owner->enable_state != bp_permanent)
2782 bl->inserted = 0;
6c95b8df 2783 }
075f6582 2784
35df4500 2785 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2786 {
6c95b8df
PA
2787 if (b->loc && b->loc->pspace != pspace)
2788 continue;
2789
c5aa993b
JM
2790 switch (b->type)
2791 {
2792 case bp_call_dummy:
c906108c 2793
c5aa993b 2794 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2795 cause problems when the inferior is rerun, so we better get
2796 rid of it. */
2797
2798 case bp_watchpoint_scope:
2799
2800 /* Also get rid of scope breakpoints. */
2801
2802 case bp_shlib_event:
2803
2804 /* Also remove solib event breakpoints. Their addresses may
2805 have changed since the last time we ran the program.
2806 Actually we may now be debugging against different target;
2807 and so the solib backend that installed this breakpoint may
2808 not be used in by the target. E.g.,
2809
2810 (gdb) file prog-linux
2811 (gdb) run # native linux target
2812 ...
2813 (gdb) kill
2814 (gdb) file prog-win.exe
2815 (gdb) tar rem :9999 # remote Windows gdbserver.
2816 */
c906108c 2817
c5aa993b
JM
2818 delete_breakpoint (b);
2819 break;
c906108c 2820
c5aa993b
JM
2821 case bp_watchpoint:
2822 case bp_hardware_watchpoint:
2823 case bp_read_watchpoint:
2824 case bp_access_watchpoint:
3a5c3e22
PA
2825 {
2826 struct watchpoint *w = (struct watchpoint *) b;
c906108c 2827
3a5c3e22
PA
2828 /* Likewise for watchpoints on local expressions. */
2829 if (w->exp_valid_block != NULL)
2830 delete_breakpoint (b);
2831 else if (context == inf_starting)
2832 {
2833 /* Reset val field to force reread of starting value in
2834 insert_breakpoints. */
2835 if (w->val)
2836 value_free (w->val);
2837 w->val = NULL;
2838 w->val_valid = 0;
c860120c 2839 }
3a5c3e22 2840 }
c5aa993b
JM
2841 break;
2842 default:
c5aa993b
JM
2843 break;
2844 }
2845 }
1c5cfe86
PA
2846
2847 /* Get rid of the moribund locations. */
35df4500
TJB
2848 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2849 decref_bp_location (&bl);
1c5cfe86 2850 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2851}
2852
6c95b8df
PA
2853/* These functions concern about actual breakpoints inserted in the
2854 target --- to e.g. check if we need to do decr_pc adjustment or if
2855 we need to hop over the bkpt --- so we check for address space
2856 match, not program space. */
2857
c2c6d25f
JM
2858/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2859 exists at PC. It returns ordinary_breakpoint_here if it's an
2860 ordinary breakpoint, or permanent_breakpoint_here if it's a
2861 permanent breakpoint.
2862 - When continuing from a location with an ordinary breakpoint, we
2863 actually single step once before calling insert_breakpoints.
e5dd4106 2864 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
2865 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2866 the target, to advance the PC past the breakpoint. */
c906108c 2867
c2c6d25f 2868enum breakpoint_here
6c95b8df 2869breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2870{
35df4500 2871 struct bp_location *bl, **blp_tmp;
c2c6d25f 2872 int any_breakpoint_here = 0;
c906108c 2873
35df4500 2874 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2875 {
35df4500
TJB
2876 if (bl->loc_type != bp_loc_software_breakpoint
2877 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2878 continue;
2879
f1310107 2880 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2881 if ((breakpoint_enabled (bl->owner)
2882 || bl->owner->enable_state == bp_permanent)
f1310107 2883 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2884 {
2885 if (overlay_debugging
35df4500
TJB
2886 && section_is_overlay (bl->section)
2887 && !section_is_mapped (bl->section))
075f6582 2888 continue; /* unmapped overlay -- can't be a match */
35df4500 2889 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2890 return permanent_breakpoint_here;
2891 else
2892 any_breakpoint_here = 1;
2893 }
2894 }
c906108c 2895
c2c6d25f 2896 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2897}
2898
1c5cfe86
PA
2899/* Return true if there's a moribund breakpoint at PC. */
2900
2901int
6c95b8df 2902moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2903{
2904 struct bp_location *loc;
2905 int ix;
2906
2907 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2908 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2909 return 1;
2910
2911 return 0;
2912}
c2c6d25f 2913
c36b740a 2914/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2915 inserted using regular breakpoint_chain / bp_location array
2916 mechanism. This does not check for single-step breakpoints, which
2917 are inserted and removed using direct target manipulation. */
c906108c
SS
2918
2919int
4a64f543
MS
2920regular_breakpoint_inserted_here_p (struct address_space *aspace,
2921 CORE_ADDR pc)
c906108c 2922{
35df4500 2923 struct bp_location *bl, **blp_tmp;
c906108c 2924
35df4500 2925 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2926 {
35df4500
TJB
2927 if (bl->loc_type != bp_loc_software_breakpoint
2928 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2929 continue;
2930
35df4500 2931 if (bl->inserted
f1310107 2932 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2933 {
2934 if (overlay_debugging
35df4500
TJB
2935 && section_is_overlay (bl->section)
2936 && !section_is_mapped (bl->section))
075f6582
DJ
2937 continue; /* unmapped overlay -- can't be a match */
2938 else
2939 return 1;
2940 }
c5aa993b 2941 }
c36b740a
VP
2942 return 0;
2943}
2944
2945/* Returns non-zero iff there's either regular breakpoint
2946 or a single step breakpoint inserted at PC. */
2947
2948int
6c95b8df 2949breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2950{
6c95b8df 2951 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2952 return 1;
c906108c 2953
6c95b8df 2954 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2955 return 1;
2956
c906108c
SS
2957 return 0;
2958}
2959
4fa8626c
DJ
2960/* This function returns non-zero iff there is a software breakpoint
2961 inserted at PC. */
2962
2963int
3e43a32a
MS
2964software_breakpoint_inserted_here_p (struct address_space *aspace,
2965 CORE_ADDR pc)
4fa8626c 2966{
35df4500 2967 struct bp_location *bl, **blp_tmp;
4fa8626c 2968
35df4500 2969 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2970 {
35df4500 2971 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2972 continue;
2973
35df4500
TJB
2974 if (bl->inserted
2975 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2976 aspace, pc))
4fa8626c
DJ
2977 {
2978 if (overlay_debugging
35df4500
TJB
2979 && section_is_overlay (bl->section)
2980 && !section_is_mapped (bl->section))
4fa8626c
DJ
2981 continue; /* unmapped overlay -- can't be a match */
2982 else
2983 return 1;
2984 }
2985 }
2986
1aafd4da 2987 /* Also check for software single-step breakpoints. */
6c95b8df 2988 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2989 return 1;
2990
4fa8626c
DJ
2991 return 0;
2992}
2993
9093389c
PA
2994int
2995hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2996 CORE_ADDR addr, ULONGEST len)
2997{
2998 struct breakpoint *bpt;
2999
3000 ALL_BREAKPOINTS (bpt)
3001 {
3002 struct bp_location *loc;
3003
3004 if (bpt->type != bp_hardware_watchpoint
3005 && bpt->type != bp_access_watchpoint)
3006 continue;
3007
3008 if (!breakpoint_enabled (bpt))
3009 continue;
3010
3011 for (loc = bpt->loc; loc; loc = loc->next)
3012 if (loc->pspace->aspace == aspace && loc->inserted)
3013 {
3014 CORE_ADDR l, h;
3015
3016 /* Check for intersection. */
3017 l = max (loc->address, addr);
3018 h = min (loc->address + loc->length, addr + len);
3019 if (l < h)
3020 return 1;
3021 }
3022 }
3023 return 0;
3024}
3025
075f6582
DJ
3026/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3027 PC is valid for process/thread PTID. */
c906108c
SS
3028
3029int
6c95b8df
PA
3030breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3031 ptid_t ptid)
c906108c 3032{
35df4500 3033 struct bp_location *bl, **blp_tmp;
4a306c9a 3034 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3035 int thread = -1;
4a306c9a 3036 int task = 0;
a6f1cd96 3037
35df4500 3038 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3039 {
35df4500
TJB
3040 if (bl->loc_type != bp_loc_software_breakpoint
3041 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3042 continue;
3043
35df4500
TJB
3044 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3045 if (!breakpoint_enabled (bl->owner)
3046 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3047 continue;
3048
f1310107 3049 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3050 continue;
3051
35df4500 3052 if (bl->owner->thread != -1)
075f6582 3053 {
a6f1cd96
JB
3054 /* This is a thread-specific breakpoint. Check that ptid
3055 matches that thread. If thread hasn't been computed yet,
3056 it is now time to do so. */
3057 if (thread == -1)
3058 thread = pid_to_thread_id (ptid);
35df4500 3059 if (bl->owner->thread != thread)
a6f1cd96 3060 continue;
075f6582 3061 }
a6f1cd96 3062
35df4500 3063 if (bl->owner->task != 0)
4a306c9a
JB
3064 {
3065 /* This is a task-specific breakpoint. Check that ptid
3066 matches that task. If task hasn't been computed yet,
3067 it is now time to do so. */
3068 if (task == 0)
3069 task = ada_get_task_number (ptid);
35df4500 3070 if (bl->owner->task != task)
4a306c9a
JB
3071 continue;
3072 }
3073
a6f1cd96 3074 if (overlay_debugging
35df4500
TJB
3075 && section_is_overlay (bl->section)
3076 && !section_is_mapped (bl->section))
a6f1cd96
JB
3077 continue; /* unmapped overlay -- can't be a match */
3078
3079 return 1;
c5aa993b 3080 }
c906108c
SS
3081
3082 return 0;
3083}
c906108c 3084\f
c5aa993b 3085
c906108c
SS
3086/* bpstat stuff. External routines' interfaces are documented
3087 in breakpoint.h. */
3088
3089int
fba45db2 3090ep_is_catchpoint (struct breakpoint *ep)
c906108c 3091{
533be4dd 3092 return (ep->type == bp_catchpoint);
c906108c
SS
3093}
3094
f431efe5
PA
3095/* Frees any storage that is part of a bpstat. Does not walk the
3096 'next' chain. */
3097
3098static void
198757a8
VP
3099bpstat_free (bpstat bs)
3100{
3101 if (bs->old_val != NULL)
3102 value_free (bs->old_val);
9add0f1b 3103 decref_counted_command_line (&bs->commands);
f431efe5 3104 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3105 xfree (bs);
3106}
3107
c906108c
SS
3108/* Clear a bpstat so that it says we are not at any breakpoint.
3109 Also free any storage that is part of a bpstat. */
3110
3111void
fba45db2 3112bpstat_clear (bpstat *bsp)
c906108c
SS
3113{
3114 bpstat p;
3115 bpstat q;
3116
3117 if (bsp == 0)
3118 return;
3119 p = *bsp;
3120 while (p != NULL)
3121 {
3122 q = p->next;
198757a8 3123 bpstat_free (p);
c906108c
SS
3124 p = q;
3125 }
3126 *bsp = NULL;
3127}
3128
3129/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3130 is part of the bpstat is copied as well. */
3131
3132bpstat
fba45db2 3133bpstat_copy (bpstat bs)
c906108c
SS
3134{
3135 bpstat p = NULL;
3136 bpstat tmp;
3137 bpstat retval = NULL;
3138
3139 if (bs == NULL)
3140 return bs;
3141
3142 for (; bs != NULL; bs = bs->next)
3143 {
3144 tmp = (bpstat) xmalloc (sizeof (*tmp));
3145 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3146 incref_counted_command_line (tmp->commands);
f431efe5 3147 incref_bp_location (tmp->bp_location_at);
31cc81e9 3148 if (bs->old_val != NULL)
3c3185ac
JK
3149 {
3150 tmp->old_val = value_copy (bs->old_val);
3151 release_value (tmp->old_val);
3152 }
31cc81e9 3153
c906108c
SS
3154 if (p == NULL)
3155 /* This is the first thing in the chain. */
3156 retval = tmp;
3157 else
3158 p->next = tmp;
3159 p = tmp;
3160 }
3161 p->next = NULL;
3162 return retval;
3163}
3164
4a64f543 3165/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3166
3167bpstat
fba45db2 3168bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3169{
c5aa993b
JM
3170 if (bsp == NULL)
3171 return NULL;
c906108c 3172
c5aa993b
JM
3173 for (; bsp != NULL; bsp = bsp->next)
3174 {
f431efe5 3175 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3176 return bsp;
3177 }
c906108c
SS
3178 return NULL;
3179}
3180
4a64f543
MS
3181/* Put in *NUM the breakpoint number of the first breakpoint we are
3182 stopped at. *BSP upon return is a bpstat which points to the
3183 remaining breakpoints stopped at (but which is not guaranteed to be
3184 good for anything but further calls to bpstat_num).
3185
8671a17b
PA
3186 Return 0 if passed a bpstat which does not indicate any breakpoints.
3187 Return -1 if stopped at a breakpoint that has been deleted since
3188 we set it.
3189 Return 1 otherwise. */
c906108c
SS
3190
3191int
8671a17b 3192bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3193{
3194 struct breakpoint *b;
3195
3196 if ((*bsp) == NULL)
3197 return 0; /* No more breakpoint values */
8671a17b 3198
4a64f543
MS
3199 /* We assume we'll never have several bpstats that correspond to a
3200 single breakpoint -- otherwise, this function might return the
3201 same number more than once and this will look ugly. */
f431efe5 3202 b = (*bsp)->breakpoint_at;
8671a17b
PA
3203 *bsp = (*bsp)->next;
3204 if (b == NULL)
3205 return -1; /* breakpoint that's been deleted since */
3206
3207 *num = b->number; /* We have its number */
3208 return 1;
c906108c
SS
3209}
3210
e93ca019 3211/* See breakpoint.h. */
c906108c
SS
3212
3213void
e93ca019 3214bpstat_clear_actions (void)
c906108c 3215{
e93ca019
JK
3216 struct thread_info *tp;
3217 bpstat bs;
3218
3219 if (ptid_equal (inferior_ptid, null_ptid))
3220 return;
3221
3222 tp = find_thread_ptid (inferior_ptid);
3223 if (tp == NULL)
3224 return;
3225
3226 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 3227 {
9add0f1b 3228 decref_counted_command_line (&bs->commands);
abf85f46 3229
c906108c
SS
3230 if (bs->old_val != NULL)
3231 {
3232 value_free (bs->old_val);
3233 bs->old_val = NULL;
3234 }
3235 }
3236}
3237
f3b1572e
PA
3238/* Called when a command is about to proceed the inferior. */
3239
3240static void
3241breakpoint_about_to_proceed (void)
3242{
3243 if (!ptid_equal (inferior_ptid, null_ptid))
3244 {
3245 struct thread_info *tp = inferior_thread ();
3246
3247 /* Allow inferior function calls in breakpoint commands to not
3248 interrupt the command list. When the call finishes
3249 successfully, the inferior will be standing at the same
3250 breakpoint as if nothing happened. */
16c381f0 3251 if (tp->control.in_infcall)
f3b1572e
PA
3252 return;
3253 }
3254
3255 breakpoint_proceeded = 1;
3256}
3257
4a64f543
MS
3258/* Stub for cleaning up our state if we error-out of a breakpoint
3259 command. */
c906108c 3260static void
4efb68b1 3261cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3262{
3263 executing_breakpoint_commands = 0;
3264}
3265
abf85f46
JK
3266/* Return non-zero iff CMD as the first line of a command sequence is `silent'
3267 or its equivalent. */
3268
3269static int
3270command_line_is_silent (struct command_line *cmd)
3271{
3272 return cmd && (strcmp ("silent", cmd->line) == 0
3273 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3274}
3275
4a64f543
MS
3276/* Execute all the commands associated with all the breakpoints at
3277 this location. Any of these commands could cause the process to
3278 proceed beyond this point, etc. We look out for such changes by
3279 checking the global "breakpoint_proceeded" after each command.
c906108c 3280
347bddb7
PA
3281 Returns true if a breakpoint command resumed the inferior. In that
3282 case, it is the caller's responsibility to recall it again with the
3283 bpstat of the current thread. */
3284
3285static int
3286bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3287{
3288 bpstat bs;
3289 struct cleanup *old_chain;
347bddb7 3290 int again = 0;
c906108c
SS
3291
3292 /* Avoid endless recursion if a `source' command is contained
3293 in bs->commands. */
3294 if (executing_breakpoint_commands)
347bddb7 3295 return 0;
c906108c
SS
3296
3297 executing_breakpoint_commands = 1;
3298 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3299
cf6c5ffb
TT
3300 prevent_dont_repeat ();
3301
4a64f543 3302 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3303 bs = *bsp;
3304
3305 breakpoint_proceeded = 0;
3306 for (; bs != NULL; bs = bs->next)
3307 {
9add0f1b 3308 struct counted_command_line *ccmd;
6c50ab1c
JB
3309 struct command_line *cmd;
3310 struct cleanup *this_cmd_tree_chain;
3311
3312 /* Take ownership of the BSP's command tree, if it has one.
3313
3314 The command tree could legitimately contain commands like
3315 'step' and 'next', which call clear_proceed_status, which
3316 frees stop_bpstat's command tree. To make sure this doesn't
3317 free the tree we're executing out from under us, we need to
3318 take ownership of the tree ourselves. Since a given bpstat's
3319 commands are only executed once, we don't need to copy it; we
3320 can clear the pointer in the bpstat, and make sure we free
3321 the tree when we're done. */
9add0f1b
TT
3322 ccmd = bs->commands;
3323 bs->commands = NULL;
abf85f46
JK
3324 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
3325 cmd = ccmd ? ccmd->commands : NULL;
3326 if (command_line_is_silent (cmd))
3327 {
3328 /* The action has been already done by bpstat_stop_status. */
3329 cmd = cmd->next;
3330 }
6c50ab1c 3331
c906108c
SS
3332 while (cmd != NULL)
3333 {
3334 execute_control_command (cmd);
3335
3336 if (breakpoint_proceeded)
3337 break;
3338 else
3339 cmd = cmd->next;
3340 }
6c50ab1c
JB
3341
3342 /* We can free this command tree now. */
3343 do_cleanups (this_cmd_tree_chain);
3344
c906108c 3345 if (breakpoint_proceeded)
32c1e744
VP
3346 {
3347 if (target_can_async_p ())
347bddb7
PA
3348 /* If we are in async mode, then the target might be still
3349 running, not stopped at any breakpoint, so nothing for
3350 us to do here -- just return to the event loop. */
3351 ;
32c1e744
VP
3352 else
3353 /* In sync mode, when execute_control_command returns
3354 we're already standing on the next breakpoint.
347bddb7
PA
3355 Breakpoint commands for that stop were not run, since
3356 execute_command does not run breakpoint commands --
3357 only command_line_handler does, but that one is not
3358 involved in execution of breakpoint commands. So, we
3359 can now execute breakpoint commands. It should be
3360 noted that making execute_command do bpstat actions is
3361 not an option -- in this case we'll have recursive
3362 invocation of bpstat for each breakpoint with a
3363 command, and can easily blow up GDB stack. Instead, we
3364 return true, which will trigger the caller to recall us
3365 with the new stop_bpstat. */
3366 again = 1;
3367 break;
32c1e744 3368 }
c906108c 3369 }
c2b8ed2c 3370 do_cleanups (old_chain);
347bddb7
PA
3371 return again;
3372}
3373
3374void
3375bpstat_do_actions (void)
3376{
353d1d73
JK
3377 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
3378
347bddb7
PA
3379 /* Do any commands attached to breakpoint we are stopped at. */
3380 while (!ptid_equal (inferior_ptid, null_ptid)
3381 && target_has_execution
3382 && !is_exited (inferior_ptid)
3383 && !is_executing (inferior_ptid))
3384 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3385 and only return when it is stopped at the next breakpoint, we
3386 keep doing breakpoint actions until it returns false to
3387 indicate the inferior was not resumed. */
16c381f0 3388 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3389 break;
353d1d73
JK
3390
3391 discard_cleanups (cleanup_if_error);
c906108c
SS
3392}
3393
fa4727a6
DJ
3394/* Print out the (old or new) value associated with a watchpoint. */
3395
3396static void
3397watchpoint_value_print (struct value *val, struct ui_file *stream)
3398{
3399 if (val == NULL)
3400 fprintf_unfiltered (stream, _("<unreadable>"));
3401 else
79a45b7d
TT
3402 {
3403 struct value_print_options opts;
3404 get_user_print_options (&opts);
3405 value_print (val, stream, &opts);
3406 }
fa4727a6
DJ
3407}
3408
e514a9d6 3409/* Generic routine for printing messages indicating why we
4a64f543 3410 stopped. The behavior of this function depends on the value
e514a9d6
JM
3411 'print_it' in the bpstat structure. Under some circumstances we
3412 may decide not to print anything here and delegate the task to
4a64f543 3413 normal_stop(). */
e514a9d6
JM
3414
3415static enum print_stop_action
3416print_bp_stop_message (bpstat bs)
3417{
3418 switch (bs->print_it)
3419 {
3420 case print_it_noop:
4a64f543 3421 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3422 return PRINT_UNKNOWN;
3423 break;
3424
3425 case print_it_done:
3426 /* We still want to print the frame, but we already printed the
4a64f543 3427 relevant messages. */
e514a9d6
JM
3428 return PRINT_SRC_AND_LOC;
3429 break;
3430
3431 case print_it_normal:
4f8d1dc6 3432 {
f431efe5
PA
3433 struct breakpoint *b = bs->breakpoint_at;
3434
1a6a67de
TJB
3435 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3436 which has since been deleted. */
3437 if (b == NULL)
3438 return PRINT_UNKNOWN;
3439
348d480f
PA
3440 /* Normal case. Call the breakpoint's print_it method. */
3441 return b->ops->print_it (bs);
4f8d1dc6 3442 }
348d480f 3443 break;
3086aeae 3444
e514a9d6 3445 default:
8e65ff28 3446 internal_error (__FILE__, __LINE__,
e2e0b3e5 3447 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3448 break;
c906108c 3449 }
c906108c
SS
3450}
3451
e514a9d6
JM
3452/* Print a message indicating what happened. This is called from
3453 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
3454 list - a list of the eventpoints that caused this stop. KIND is
3455 the target_waitkind for the stopping event. This
e514a9d6
JM
3456 routine calls the generic print routine for printing a message
3457 about reasons for stopping. This will print (for example) the
3458 "Breakpoint n," part of the output. The return value of this
3459 routine is one of:
c906108c 3460
4a64f543 3461 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3462 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3463 code to print the location. An example is
c5aa993b
JM
3464 "Breakpoint 1, " which should be followed by
3465 the location.
917317f4 3466 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3467 to also print the location part of the message.
3468 An example is the catch/throw messages, which
4a64f543 3469 don't require a location appended to the end.
917317f4 3470 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3471 further info to be printed. */
c906108c 3472
917317f4 3473enum print_stop_action
36dfb11c 3474bpstat_print (bpstat bs, int kind)
c906108c
SS
3475{
3476 int val;
c5aa993b 3477
c906108c 3478 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3479 (Currently all watchpoints go on the bpstat whether hit or not.
3480 That probably could (should) be changed, provided care is taken
c906108c 3481 with respect to bpstat_explains_signal). */
e514a9d6
JM
3482 for (; bs; bs = bs->next)
3483 {
3484 val = print_bp_stop_message (bs);
3485 if (val == PRINT_SRC_ONLY
3486 || val == PRINT_SRC_AND_LOC
3487 || val == PRINT_NOTHING)
3488 return val;
3489 }
c906108c 3490
36dfb11c
TT
3491 /* If we had hit a shared library event breakpoint,
3492 print_bp_stop_message would print out this message. If we hit an
3493 OS-level shared library event, do the same thing. */
3494 if (kind == TARGET_WAITKIND_LOADED)
3495 {
3496 ui_out_text (current_uiout, _("Stopped due to shared library event\n"));
3497 if (ui_out_is_mi_like_p (current_uiout))
3498 ui_out_field_string (current_uiout, "reason",
3499 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
3500 return PRINT_NOTHING;
3501 }
3502
e514a9d6 3503 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3504 with and nothing was printed. */
917317f4 3505 return PRINT_UNKNOWN;
c906108c
SS
3506}
3507
4a64f543
MS
3508/* Evaluate the expression EXP and return 1 if value is zero. This is
3509 used inside a catch_errors to evaluate the breakpoint condition.
3510 The argument is a "struct expression *" that has been cast to a
3511 "char *" to make it pass through catch_errors. */
c906108c
SS
3512
3513static int
4efb68b1 3514breakpoint_cond_eval (void *exp)
c906108c 3515{
278cd55f 3516 struct value *mark = value_mark ();
c5aa993b 3517 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3518
c906108c
SS
3519 value_free_to_mark (mark);
3520 return i;
3521}
3522
5760d0ab 3523/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3524
3525static bpstat
5760d0ab 3526bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3527{
3528 bpstat bs;
3529
3530 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3531 bs->next = NULL;
3532 **bs_link_pointer = bs;
3533 *bs_link_pointer = &bs->next;
f431efe5
PA
3534 bs->breakpoint_at = bl->owner;
3535 bs->bp_location_at = bl;
3536 incref_bp_location (bl);
c906108c
SS
3537 /* If the condition is false, etc., don't do the commands. */
3538 bs->commands = NULL;
3539 bs->old_val = NULL;
3540 bs->print_it = print_it_normal;
3541 return bs;
3542}
3543\f
d983da9c
DJ
3544/* The target has stopped with waitstatus WS. Check if any hardware
3545 watchpoints have triggered, according to the target. */
3546
3547int
3548watchpoints_triggered (struct target_waitstatus *ws)
3549{
d92524f1 3550 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3551 CORE_ADDR addr;
3552 struct breakpoint *b;
3553
3554 if (!stopped_by_watchpoint)
3555 {
3556 /* We were not stopped by a watchpoint. Mark all watchpoints
3557 as not triggered. */
3558 ALL_BREAKPOINTS (b)
cc60f2e3 3559 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3560 {
3561 struct watchpoint *w = (struct watchpoint *) b;
3562
3563 w->watchpoint_triggered = watch_triggered_no;
3564 }
d983da9c
DJ
3565
3566 return 0;
3567 }
3568
3569 if (!target_stopped_data_address (&current_target, &addr))
3570 {
3571 /* We were stopped by a watchpoint, but we don't know where.
3572 Mark all watchpoints as unknown. */
3573 ALL_BREAKPOINTS (b)
cc60f2e3 3574 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3575 {
3576 struct watchpoint *w = (struct watchpoint *) b;
3577
3578 w->watchpoint_triggered = watch_triggered_unknown;
3579 }
d983da9c
DJ
3580
3581 return stopped_by_watchpoint;
3582 }
3583
3584 /* The target could report the data address. Mark watchpoints
3585 affected by this data address as triggered, and all others as not
3586 triggered. */
3587
3588 ALL_BREAKPOINTS (b)
cc60f2e3 3589 if (is_hardware_watchpoint (b))
d983da9c 3590 {
3a5c3e22 3591 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 3592 struct bp_location *loc;
d983da9c 3593
3a5c3e22 3594 w->watchpoint_triggered = watch_triggered_no;
a5606eee 3595 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 3596 {
3a5c3e22 3597 if (is_masked_watchpoint (b))
9c06b0b4 3598 {
3a5c3e22
PA
3599 CORE_ADDR newaddr = addr & w->hw_wp_mask;
3600 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
3601
3602 if (newaddr == start)
3603 {
3a5c3e22 3604 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3605 break;
3606 }
3607 }
3608 /* Exact match not required. Within range is sufficient. */
3609 else if (target_watchpoint_addr_within_range (&current_target,
3610 addr, loc->address,
3611 loc->length))
3612 {
3a5c3e22 3613 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3614 break;
3615 }
3616 }
d983da9c
DJ
3617 }
3618
3619 return 1;
3620}
3621
c906108c
SS
3622/* Possible return values for watchpoint_check (this can't be an enum
3623 because of check_errors). */
3624/* The watchpoint has been deleted. */
3625#define WP_DELETED 1
3626/* The value has changed. */
3627#define WP_VALUE_CHANGED 2
3628/* The value has not changed. */
3629#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3630/* Ignore this watchpoint, no matter if the value changed or not. */
3631#define WP_IGNORE 4
c906108c
SS
3632
3633#define BP_TEMPFLAG 1
3634#define BP_HARDWAREFLAG 2
3635
4a64f543
MS
3636/* Evaluate watchpoint condition expression and check if its value
3637 changed.
553e4c11
JB
3638
3639 P should be a pointer to struct bpstat, but is defined as a void *
3640 in order for this function to be usable with catch_errors. */
c906108c
SS
3641
3642static int
4efb68b1 3643watchpoint_check (void *p)
c906108c
SS
3644{
3645 bpstat bs = (bpstat) p;
3a5c3e22 3646 struct watchpoint *b;
c906108c
SS
3647 struct frame_info *fr;
3648 int within_current_scope;
3649
f431efe5 3650 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3651 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 3652 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 3653
f6bc2008
PA
3654 /* If this is a local watchpoint, we only want to check if the
3655 watchpoint frame is in scope if the current thread is the thread
3656 that was used to create the watchpoint. */
3657 if (!watchpoint_in_thread_scope (b))
60e1c644 3658 return WP_IGNORE;
f6bc2008 3659
c906108c
SS
3660 if (b->exp_valid_block == NULL)
3661 within_current_scope = 1;
3662 else
3663 {
edb3359d
DJ
3664 struct frame_info *frame = get_current_frame ();
3665 struct gdbarch *frame_arch = get_frame_arch (frame);
3666 CORE_ADDR frame_pc = get_frame_pc (frame);
3667
4a64f543
MS
3668 /* in_function_epilogue_p() returns a non-zero value if we're
3669 still in the function but the stack frame has already been
3670 invalidated. Since we can't rely on the values of local
3671 variables after the stack has been destroyed, we are treating
3672 the watchpoint in that state as `not changed' without further
3673 checking. Don't mark watchpoints as changed if the current
3674 frame is in an epilogue - even if they are in some other
3675 frame, our view of the stack is likely to be wrong and
3676 frame_find_by_id could error out. */
a0f49112 3677 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3678 return WP_IGNORE;
a0f49112 3679
101dcfbe 3680 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3681 within_current_scope = (fr != NULL);
69fbadd5
DJ
3682
3683 /* If we've gotten confused in the unwinder, we might have
3684 returned a frame that can't describe this variable. */
edb3359d
DJ
3685 if (within_current_scope)
3686 {
3687 struct symbol *function;
3688
3689 function = get_frame_function (fr);
3690 if (function == NULL
3691 || !contained_in (b->exp_valid_block,
3692 SYMBOL_BLOCK_VALUE (function)))
3693 within_current_scope = 0;
3694 }
69fbadd5 3695
edb3359d 3696 if (within_current_scope)
c906108c
SS
3697 /* If we end up stopping, the current frame will get selected
3698 in normal_stop. So this call to select_frame won't affect
3699 the user. */
0f7d239c 3700 select_frame (fr);
c906108c 3701 }
c5aa993b 3702
c906108c
SS
3703 if (within_current_scope)
3704 {
4a64f543
MS
3705 /* We use value_{,free_to_}mark because it could be a *long*
3706 time before we return to the command level and call
3707 free_all_values. We can't call free_all_values because we
3708 might be in the middle of evaluating a function call. */
c906108c 3709
0cf6dd15 3710 int pc = 0;
9c06b0b4 3711 struct value *mark;
fa4727a6
DJ
3712 struct value *new_val;
3713
3a5c3e22 3714 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
3715 /* Since we don't know the exact trigger address (from
3716 stopped_data_address), just tell the user we've triggered
3717 a mask watchpoint. */
3718 return WP_VALUE_CHANGED;
3719
3720 mark = value_mark ();
0cf6dd15 3721 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3722
4a64f543
MS
3723 /* We use value_equal_contents instead of value_equal because
3724 the latter coerces an array to a pointer, thus comparing just
3725 the address of the array instead of its contents. This is
3726 not what we want. */
fa4727a6 3727 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3728 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3729 {
fa4727a6
DJ
3730 if (new_val != NULL)
3731 {
3732 release_value (new_val);
3733 value_free_to_mark (mark);
3734 }
c906108c
SS
3735 bs->old_val = b->val;
3736 b->val = new_val;
fa4727a6 3737 b->val_valid = 1;
c906108c
SS
3738 return WP_VALUE_CHANGED;
3739 }
3740 else
3741 {
60e1c644 3742 /* Nothing changed. */
c906108c 3743 value_free_to_mark (mark);
c906108c
SS
3744 return WP_VALUE_NOT_CHANGED;
3745 }
3746 }
3747 else
3748 {
79a45e25
PA
3749 struct ui_out *uiout = current_uiout;
3750
c906108c 3751 /* This seems like the only logical thing to do because
c5aa993b
JM
3752 if we temporarily ignored the watchpoint, then when
3753 we reenter the block in which it is valid it contains
3754 garbage (in the case of a function, it may have two
3755 garbage values, one before and one after the prologue).
3756 So we can't even detect the first assignment to it and
3757 watch after that (since the garbage may or may not equal
3758 the first value assigned). */
348d480f
PA
3759 /* We print all the stop information in
3760 breakpoint_ops->print_it, but in this case, by the time we
3761 call breakpoint_ops->print_it this bp will be deleted
3762 already. So we have no choice but print the information
3763 here. */
9dc5e2a9 3764 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3765 ui_out_field_string
3766 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3767 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 3768 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
3769 ui_out_text (uiout,
3770 " deleted because the program has left the block in\n\
8b93c638 3771which its expression is valid.\n");
4ce44c66 3772
cdac0397 3773 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 3774 decref_counted_command_line (&b->base.commands);
d0fb5eae 3775 watchpoint_del_at_next_stop (b);
c906108c
SS
3776
3777 return WP_DELETED;
3778 }
3779}
3780
18a18393 3781/* Return true if it looks like target has stopped due to hitting
348d480f
PA
3782 breakpoint location BL. This function does not check if we should
3783 stop, only if BL explains the stop. */
3784
18a18393 3785static int
6c95b8df
PA
3786bpstat_check_location (const struct bp_location *bl,
3787 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3788{
3789 struct breakpoint *b = bl->owner;
3790
348d480f 3791 /* BL is from an existing breakpoint. */
2bdf28a0
JK
3792 gdb_assert (b != NULL);
3793
348d480f 3794 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
18a18393
VP
3795}
3796
3a5c3e22
PA
3797/* Determine if the watched values have actually changed, and we
3798 should stop. If not, set BS->stop to 0. */
3799
18a18393
VP
3800static void
3801bpstat_check_watchpoint (bpstat bs)
3802{
2bdf28a0 3803 const struct bp_location *bl;
3a5c3e22 3804 struct watchpoint *b;
2bdf28a0
JK
3805
3806 /* BS is built for existing struct breakpoint. */
f431efe5 3807 bl = bs->bp_location_at;
2bdf28a0 3808 gdb_assert (bl != NULL);
3a5c3e22 3809 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 3810 gdb_assert (b != NULL);
18a18393 3811
18a18393 3812 {
18a18393
VP
3813 int must_check_value = 0;
3814
3a5c3e22 3815 if (b->base.type == bp_watchpoint)
18a18393
VP
3816 /* For a software watchpoint, we must always check the
3817 watched value. */
3818 must_check_value = 1;
3819 else if (b->watchpoint_triggered == watch_triggered_yes)
3820 /* We have a hardware watchpoint (read, write, or access)
3821 and the target earlier reported an address watched by
3822 this watchpoint. */
3823 must_check_value = 1;
3824 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 3825 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
3826 /* We were stopped by a hardware watchpoint, but the target could
3827 not report the data address. We must check the watchpoint's
3828 value. Access and read watchpoints are out of luck; without
3829 a data address, we can't figure it out. */
3830 must_check_value = 1;
3a5c3e22 3831
18a18393
VP
3832 if (must_check_value)
3833 {
3e43a32a
MS
3834 char *message
3835 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 3836 b->base.number);
18a18393
VP
3837 struct cleanup *cleanups = make_cleanup (xfree, message);
3838 int e = catch_errors (watchpoint_check, bs, message,
3839 RETURN_MASK_ALL);
3840 do_cleanups (cleanups);
3841 switch (e)
3842 {
3843 case WP_DELETED:
3844 /* We've already printed what needs to be printed. */
3845 bs->print_it = print_it_done;
3846 /* Stop. */
3847 break;
60e1c644
PA
3848 case WP_IGNORE:
3849 bs->print_it = print_it_noop;
3850 bs->stop = 0;
3851 break;
18a18393 3852 case WP_VALUE_CHANGED:
3a5c3e22 3853 if (b->base.type == bp_read_watchpoint)
18a18393 3854 {
85d721b8
PA
3855 /* There are two cases to consider here:
3856
4a64f543 3857 1. We're watching the triggered memory for reads.
85d721b8
PA
3858 In that case, trust the target, and always report
3859 the watchpoint hit to the user. Even though
3860 reads don't cause value changes, the value may
3861 have changed since the last time it was read, and
3862 since we're not trapping writes, we will not see
3863 those, and as such we should ignore our notion of
3864 old value.
3865
4a64f543 3866 2. We're watching the triggered memory for both
85d721b8
PA
3867 reads and writes. There are two ways this may
3868 happen:
3869
4a64f543 3870 2.1. This is a target that can't break on data
85d721b8
PA
3871 reads only, but can break on accesses (reads or
3872 writes), such as e.g., x86. We detect this case
3873 at the time we try to insert read watchpoints.
3874
4a64f543 3875 2.2. Otherwise, the target supports read
85d721b8
PA
3876 watchpoints, but, the user set an access or write
3877 watchpoint watching the same memory as this read
3878 watchpoint.
3879
3880 If we're watching memory writes as well as reads,
3881 ignore watchpoint hits when we find that the
3882 value hasn't changed, as reads don't cause
3883 changes. This still gives false positives when
3884 the program writes the same value to memory as
3885 what there was already in memory (we will confuse
3886 it for a read), but it's much better than
3887 nothing. */
3888
3889 int other_write_watchpoint = 0;
3890
3891 if (bl->watchpoint_type == hw_read)
3892 {
3893 struct breakpoint *other_b;
3894
3895 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
3896 if (other_b->type == bp_hardware_watchpoint
3897 || other_b->type == bp_access_watchpoint)
85d721b8 3898 {
3a5c3e22
PA
3899 struct watchpoint *other_w =
3900 (struct watchpoint *) other_b;
3901
3902 if (other_w->watchpoint_triggered
3903 == watch_triggered_yes)
3904 {
3905 other_write_watchpoint = 1;
3906 break;
3907 }
85d721b8
PA
3908 }
3909 }
3910
3911 if (other_write_watchpoint
3912 || bl->watchpoint_type == hw_access)
3913 {
3914 /* We're watching the same memory for writes,
3915 and the value changed since the last time we
3916 updated it, so this trap must be for a write.
3917 Ignore it. */
3918 bs->print_it = print_it_noop;
3919 bs->stop = 0;
3920 }
18a18393
VP
3921 }
3922 break;
3923 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
3924 if (b->base.type == bp_hardware_watchpoint
3925 || b->base.type == bp_watchpoint)
18a18393
VP
3926 {
3927 /* Don't stop: write watchpoints shouldn't fire if
3928 the value hasn't changed. */
3929 bs->print_it = print_it_noop;
3930 bs->stop = 0;
3931 }
3932 /* Stop. */
3933 break;
3934 default:
3935 /* Can't happen. */
3936 case 0:
3937 /* Error from catch_errors. */
3a5c3e22 3938 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 3939 watchpoint_del_at_next_stop (b);
18a18393
VP
3940 /* We've already printed what needs to be printed. */
3941 bs->print_it = print_it_done;
3942 break;
3943 }
3944 }
3945 else /* must_check_value == 0 */
3946 {
3947 /* This is a case where some watchpoint(s) triggered, but
3948 not at the address of this watchpoint, or else no
3949 watchpoint triggered after all. So don't print
3950 anything for this watchpoint. */
3951 bs->print_it = print_it_noop;
3952 bs->stop = 0;
3953 }
3954 }
3955}
3956
3957
3958/* Check conditions (condition proper, frame, thread and ignore count)
3959 of breakpoint referred to by BS. If we should not stop for this
3960 breakpoint, set BS->stop to 0. */
f431efe5 3961
18a18393
VP
3962static void
3963bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3964{
3965 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
3966 const struct bp_location *bl;
3967 struct breakpoint *b;
3968
3969 /* BS is built for existing struct breakpoint. */
f431efe5 3970 bl = bs->bp_location_at;
2bdf28a0 3971 gdb_assert (bl != NULL);
f431efe5 3972 b = bs->breakpoint_at;
2bdf28a0 3973 gdb_assert (b != NULL);
18a18393
VP
3974
3975 if (frame_id_p (b->frame_id)
edb3359d 3976 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
3977 bs->stop = 0;
3978 else if (bs->stop)
3979 {
3980 int value_is_zero = 0;
60e1c644
PA
3981 struct expression *cond;
3982
7371cf6d
PM
3983 /* Evaluate Python breakpoints that have a "stop"
3984 method implemented. */
3985 if (b->py_bp_object)
3986 bs->stop = gdbpy_should_stop (b->py_bp_object);
3987
60e1c644 3988 if (is_watchpoint (b))
3a5c3e22
PA
3989 {
3990 struct watchpoint *w = (struct watchpoint *) b;
3991
3992 cond = w->cond_exp;
3993 }
60e1c644
PA
3994 else
3995 cond = bl->cond;
3996
f431efe5 3997 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 3998 {
60e1c644 3999 int within_current_scope = 1;
3a5c3e22 4000 struct watchpoint * w;
60e1c644 4001
c5bc3a77
DJ
4002 /* We use value_mark and value_free_to_mark because it could
4003 be a long time before we return to the command level and
4004 call free_all_values. We can't call free_all_values
4005 because we might be in the middle of evaluating a
4006 function call. */
4007 struct value *mark = value_mark ();
4008
3a5c3e22
PA
4009 if (is_watchpoint (b))
4010 w = (struct watchpoint *) b;
4011 else
4012 w = NULL;
4013
edb3359d
DJ
4014 /* Need to select the frame, with all that implies so that
4015 the conditions will have the right context. Because we
4016 use the frame, we will not see an inlined function's
4017 variables when we arrive at a breakpoint at the start
4018 of the inlined function; the current frame will be the
4019 call site. */
3a5c3e22 4020 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
4021 select_frame (get_current_frame ());
4022 else
4023 {
4024 struct frame_info *frame;
4025
4026 /* For local watchpoint expressions, which particular
4027 instance of a local is being watched matters, so we
4028 keep track of the frame to evaluate the expression
4029 in. To evaluate the condition however, it doesn't
4030 really matter which instantiation of the function
4031 where the condition makes sense triggers the
4032 watchpoint. This allows an expression like "watch
4033 global if q > 10" set in `func', catch writes to
4034 global on all threads that call `func', or catch
4035 writes on all recursive calls of `func' by a single
4036 thread. We simply always evaluate the condition in
4037 the innermost frame that's executing where it makes
4038 sense to evaluate the condition. It seems
4039 intuitive. */
3a5c3e22 4040 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
4041 if (frame != NULL)
4042 select_frame (frame);
4043 else
4044 within_current_scope = 0;
4045 }
4046 if (within_current_scope)
4047 value_is_zero
4048 = catch_errors (breakpoint_cond_eval, cond,
4049 "Error in testing breakpoint condition:\n",
4050 RETURN_MASK_ALL);
4051 else
4052 {
4053 warning (_("Watchpoint condition cannot be tested "
4054 "in the current scope"));
4055 /* If we failed to set the right context for this
4056 watchpoint, unconditionally report it. */
4057 value_is_zero = 0;
4058 }
4a64f543 4059 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4060 value_free_to_mark (mark);
18a18393 4061 }
60e1c644
PA
4062
4063 if (cond && value_is_zero)
18a18393
VP
4064 {
4065 bs->stop = 0;
4066 }
4067 else if (b->thread != -1 && b->thread != thread_id)
4068 {
4069 bs->stop = 0;
4070 }
4071 else if (b->ignore_count > 0)
4072 {
4073 b->ignore_count--;
4074 annotate_ignore_count_change ();
4075 bs->stop = 0;
4a64f543 4076 /* Increase the hit count even though we don't stop. */
18a18393 4077 ++(b->hit_count);
8d3788bd 4078 observer_notify_breakpoint_modified (b);
18a18393
VP
4079 }
4080 }
4081}
4082
4083
9709f61c 4084/* Get a bpstat associated with having just stopped at address
d983da9c 4085 BP_ADDR in thread PTID.
c906108c 4086
d983da9c 4087 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4088 don't understand this stop. Result is a chain of bpstat's such
4089 that:
c906108c 4090
c5aa993b 4091 if we don't understand the stop, the result is a null pointer.
c906108c 4092
c5aa993b 4093 if we understand why we stopped, the result is not null.
c906108c 4094
c5aa993b
JM
4095 Each element of the chain refers to a particular breakpoint or
4096 watchpoint at which we have stopped. (We may have stopped for
4097 several reasons concurrently.)
c906108c 4098
c5aa993b
JM
4099 Each element of the chain has valid next, breakpoint_at,
4100 commands, FIXME??? fields. */
c906108c
SS
4101
4102bpstat
6c95b8df
PA
4103bpstat_stop_status (struct address_space *aspace,
4104 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4105{
0d381245 4106 struct breakpoint *b = NULL;
afe38095 4107 struct bp_location *bl;
20874c92 4108 struct bp_location *loc;
5760d0ab
JK
4109 /* First item of allocated bpstat's. */
4110 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4111 /* Pointer to the last thing in the chain currently. */
5760d0ab 4112 bpstat bs;
20874c92 4113 int ix;
429374b8 4114 int need_remove_insert;
f431efe5 4115 int removed_any;
c906108c 4116
f431efe5
PA
4117 /* First, build the bpstat chain with locations that explain a
4118 target stop, while being careful to not set the target running,
4119 as that may invalidate locations (in particular watchpoint
4120 locations are recreated). Resuming will happen here with
4121 breakpoint conditions or watchpoint expressions that include
4122 inferior function calls. */
c5aa993b 4123
429374b8
JK
4124 ALL_BREAKPOINTS (b)
4125 {
4126 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4127 continue;
a5606eee 4128
429374b8
JK
4129 for (bl = b->loc; bl != NULL; bl = bl->next)
4130 {
4a64f543
MS
4131 /* For hardware watchpoints, we look only at the first
4132 location. The watchpoint_check function will work on the
4133 entire expression, not the individual locations. For
4134 read watchpoints, the watchpoints_triggered function has
4135 checked all locations already. */
429374b8
JK
4136 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4137 break;
18a18393 4138
429374b8
JK
4139 if (bl->shlib_disabled)
4140 continue;
c5aa993b 4141
429374b8
JK
4142 if (!bpstat_check_location (bl, aspace, bp_addr))
4143 continue;
c5aa993b 4144
4a64f543
MS
4145 /* Come here if it's a watchpoint, or if the break address
4146 matches. */
c5aa993b 4147
4a64f543
MS
4148 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4149 explain stop. */
c5aa993b 4150
f431efe5
PA
4151 /* Assume we stop. Should we find a watchpoint that is not
4152 actually triggered, or if the condition of the breakpoint
4153 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4154 bs->stop = 1;
4155 bs->print = 1;
d983da9c 4156
f431efe5
PA
4157 /* If this is a scope breakpoint, mark the associated
4158 watchpoint as triggered so that we will handle the
4159 out-of-scope event. We'll get to the watchpoint next
4160 iteration. */
d0fb5eae 4161 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4162 {
4163 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4164
4165 w->watchpoint_triggered = watch_triggered_yes;
4166 }
f431efe5
PA
4167 }
4168 }
4169
4170 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4171 {
f1310107 4172 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4173 {
5760d0ab 4174 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4175 /* For hits of moribund locations, we should just proceed. */
4176 bs->stop = 0;
4177 bs->print = 0;
4178 bs->print_it = print_it_noop;
4179 }
4180 }
4181
f431efe5
PA
4182 /* Now go through the locations that caused the target to stop, and
4183 check whether we're interested in reporting this stop to higher
4184 layers, or whether we should resume the target transparently. */
4185
4186 removed_any = 0;
4187
5760d0ab 4188 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4189 {
4190 if (!bs->stop)
4191 continue;
4192
f431efe5 4193 b = bs->breakpoint_at;
348d480f
PA
4194 b->ops->check_status (bs);
4195 if (bs->stop)
28010a5d 4196 {
348d480f 4197 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4198
429374b8
JK
4199 if (bs->stop)
4200 {
4201 ++(b->hit_count);
8d3788bd 4202 observer_notify_breakpoint_modified (b);
c906108c 4203
4a64f543 4204 /* We will stop here. */
429374b8
JK
4205 if (b->disposition == disp_disable)
4206 {
4207 if (b->enable_state != bp_permanent)
4208 b->enable_state = bp_disabled;
f431efe5 4209 removed_any = 1;
429374b8
JK
4210 }
4211 if (b->silent)
4212 bs->print = 0;
4213 bs->commands = b->commands;
9add0f1b 4214 incref_counted_command_line (bs->commands);
abf85f46
JK
4215 if (command_line_is_silent (bs->commands
4216 ? bs->commands->commands : NULL))
4217 bs->print = 0;
429374b8
JK
4218 }
4219
e5dd4106 4220 /* Print nothing for this entry if we don't stop or don't print. */
429374b8
JK
4221 if (bs->stop == 0 || bs->print == 0)
4222 bs->print_it = print_it_noop;
348d480f 4223 }
429374b8 4224 }
876fa593 4225
d983da9c
DJ
4226 /* If we aren't stopping, the value of some hardware watchpoint may
4227 not have changed, but the intermediate memory locations we are
4228 watching may have. Don't bother if we're stopping; this will get
4229 done later. */
d832cb68 4230 need_remove_insert = 0;
5760d0ab
JK
4231 if (! bpstat_causes_stop (bs_head))
4232 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4233 if (!bs->stop
f431efe5
PA
4234 && bs->breakpoint_at
4235 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4236 {
3a5c3e22
PA
4237 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4238
4239 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 4240 need_remove_insert = 1;
d983da9c
DJ
4241 }
4242
d832cb68 4243 if (need_remove_insert)
2d134ed3 4244 update_global_location_list (1);
f431efe5
PA
4245 else if (removed_any)
4246 update_global_location_list (0);
d832cb68 4247
5760d0ab 4248 return bs_head;
c906108c 4249}
628fe4e4
JK
4250
4251static void
4252handle_jit_event (void)
4253{
4254 struct frame_info *frame;
4255 struct gdbarch *gdbarch;
4256
4257 /* Switch terminal for any messages produced by
4258 breakpoint_re_set. */
4259 target_terminal_ours_for_output ();
4260
4261 frame = get_current_frame ();
4262 gdbarch = get_frame_arch (frame);
4263
4264 jit_event_handler (gdbarch);
4265
4266 target_terminal_inferior ();
4267}
4268
4269/* Prepare WHAT final decision for infrun. */
4270
4271/* Decide what infrun needs to do with this bpstat. */
4272
c906108c 4273struct bpstat_what
0e30163f 4274bpstat_what (bpstat bs_head)
c906108c 4275{
c906108c 4276 struct bpstat_what retval;
628fe4e4
JK
4277 /* We need to defer calling `solib_add', as adding new symbols
4278 resets breakpoints, which in turn deletes breakpoint locations,
4279 and hence may clear unprocessed entries in the BS chain. */
4280 int shlib_event = 0;
4281 int jit_event = 0;
0e30163f 4282 bpstat bs;
c906108c 4283
628fe4e4 4284 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4285 retval.call_dummy = STOP_NONE;
186c406b 4286 retval.is_longjmp = 0;
628fe4e4 4287
0e30163f 4288 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4289 {
628fe4e4
JK
4290 /* Extract this BS's action. After processing each BS, we check
4291 if its action overrides all we've seem so far. */
4292 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4293 enum bptype bptype;
4294
c906108c 4295 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4296 {
4297 /* I suspect this can happen if it was a momentary
4298 breakpoint which has since been deleted. */
4299 bptype = bp_none;
4300 }
20874c92 4301 else
f431efe5 4302 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4303
4304 switch (bptype)
c906108c
SS
4305 {
4306 case bp_none:
628fe4e4 4307 break;
c906108c
SS
4308 case bp_breakpoint:
4309 case bp_hardware_breakpoint:
4310 case bp_until:
4311 case bp_finish:
4312 if (bs->stop)
4313 {
4314 if (bs->print)
628fe4e4 4315 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4316 else
628fe4e4 4317 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4318 }
4319 else
628fe4e4 4320 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4321 break;
4322 case bp_watchpoint:
4323 case bp_hardware_watchpoint:
4324 case bp_read_watchpoint:
4325 case bp_access_watchpoint:
4326 if (bs->stop)
4327 {
4328 if (bs->print)
628fe4e4 4329 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4330 else
628fe4e4 4331 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4332 }
4333 else
628fe4e4
JK
4334 {
4335 /* There was a watchpoint, but we're not stopping.
4336 This requires no further action. */
4337 }
c906108c
SS
4338 break;
4339 case bp_longjmp:
186c406b 4340 case bp_exception:
628fe4e4 4341 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4342 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4343 break;
4344 case bp_longjmp_resume:
186c406b 4345 case bp_exception_resume:
628fe4e4 4346 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4347 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4348 break;
4349 case bp_step_resume:
4350 if (bs->stop)
628fe4e4
JK
4351 this_action = BPSTAT_WHAT_STEP_RESUME;
4352 else
c906108c 4353 {
628fe4e4
JK
4354 /* It is for the wrong frame. */
4355 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4356 }
c906108c 4357 break;
2c03e5be
PA
4358 case bp_hp_step_resume:
4359 if (bs->stop)
4360 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4361 else
4362 {
4363 /* It is for the wrong frame. */
4364 this_action = BPSTAT_WHAT_SINGLE;
4365 }
4366 break;
c906108c 4367 case bp_watchpoint_scope:
c4093a6a 4368 case bp_thread_event:
1900040c 4369 case bp_overlay_event:
0fd8e87f 4370 case bp_longjmp_master:
aa7d318d 4371 case bp_std_terminate_master:
186c406b 4372 case bp_exception_master:
628fe4e4 4373 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4374 break;
ce78b96d 4375 case bp_catchpoint:
c5aa993b
JM
4376 if (bs->stop)
4377 {
4378 if (bs->print)
628fe4e4 4379 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4380 else
628fe4e4 4381 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4382 }
4383 else
628fe4e4
JK
4384 {
4385 /* There was a catchpoint, but we're not stopping.
4386 This requires no further action. */
4387 }
4388 break;
4389 case bp_shlib_event:
4390 shlib_event = 1;
4391
4392 /* If requested, stop when the dynamic linker notifies GDB
4393 of events. This allows the user to get control and place
4394 breakpoints in initializer routines for dynamically
4395 loaded objects (among other things). */
4396 if (stop_on_solib_events)
4397 this_action = BPSTAT_WHAT_STOP_NOISY;
4398 else
4399 this_action = BPSTAT_WHAT_SINGLE;
4400 break;
4401 case bp_jit_event:
4402 jit_event = 1;
4403 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4404 break;
c906108c 4405 case bp_call_dummy:
53a5351d
JM
4406 /* Make sure the action is stop (silent or noisy),
4407 so infrun.c pops the dummy frame. */
aa7d318d 4408 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4409 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4410 break;
4411 case bp_std_terminate:
4412 /* Make sure the action is stop (silent or noisy),
4413 so infrun.c pops the dummy frame. */
aa7d318d 4414 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4415 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4416 break;
1042e4c0 4417 case bp_tracepoint:
7a697b8d 4418 case bp_fast_tracepoint:
0fb4aa4b 4419 case bp_static_tracepoint:
1042e4c0
SS
4420 /* Tracepoint hits should not be reported back to GDB, and
4421 if one got through somehow, it should have been filtered
4422 out already. */
4423 internal_error (__FILE__, __LINE__,
7a697b8d 4424 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4425 break;
4426 case bp_gnu_ifunc_resolver:
4427 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4428 this_action = BPSTAT_WHAT_SINGLE;
4429 break;
4430 case bp_gnu_ifunc_resolver_return:
4431 /* The breakpoint will be removed, execution will restart from the
4432 PC of the former breakpoint. */
4433 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4434 break;
628fe4e4
JK
4435 default:
4436 internal_error (__FILE__, __LINE__,
4437 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4438 }
628fe4e4
JK
4439
4440 retval.main_action = max (retval.main_action, this_action);
c906108c 4441 }
628fe4e4 4442
0e30163f
JK
4443 /* These operations may affect the bs->breakpoint_at state so they are
4444 delayed after MAIN_ACTION is decided above. */
4445
628fe4e4
JK
4446 if (shlib_event)
4447 {
4448 if (debug_infrun)
4449 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4450
4451 /* Check for any newly added shared libraries if we're supposed
4452 to be adding them automatically. */
4453
4454 /* Switch terminal for any messages produced by
4455 breakpoint_re_set. */
4456 target_terminal_ours_for_output ();
4457
4458#ifdef SOLIB_ADD
4459 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4460#else
4461 solib_add (NULL, 0, &current_target, auto_solib_add);
4462#endif
4463
4464 target_terminal_inferior ();
4465 }
4466
4467 if (jit_event)
4468 {
4469 if (debug_infrun)
4470 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4471
4472 handle_jit_event ();
4473 }
4474
0e30163f
JK
4475 for (bs = bs_head; bs != NULL; bs = bs->next)
4476 {
4477 struct breakpoint *b = bs->breakpoint_at;
4478
4479 if (b == NULL)
4480 continue;
4481 switch (b->type)
4482 {
4483 case bp_gnu_ifunc_resolver:
4484 gnu_ifunc_resolver_stop (b);
4485 break;
4486 case bp_gnu_ifunc_resolver_return:
4487 gnu_ifunc_resolver_return_stop (b);
4488 break;
4489 }
4490 }
4491
c906108c
SS
4492 return retval;
4493}
4494
4495/* Nonzero if we should step constantly (e.g. watchpoints on machines
4496 without hardware support). This isn't related to a specific bpstat,
4497 just to things like whether watchpoints are set. */
4498
c5aa993b 4499int
fba45db2 4500bpstat_should_step (void)
c906108c
SS
4501{
4502 struct breakpoint *b;
cc59ec59 4503
c906108c 4504 ALL_BREAKPOINTS (b)
717a8278 4505 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4506 return 1;
c906108c
SS
4507 return 0;
4508}
4509
67822962
PA
4510int
4511bpstat_causes_stop (bpstat bs)
4512{
4513 for (; bs != NULL; bs = bs->next)
4514 if (bs->stop)
4515 return 1;
4516
4517 return 0;
4518}
4519
c906108c 4520\f
c5aa993b 4521
170b53b2
UW
4522/* Compute a string of spaces suitable to indent the next line
4523 so it starts at the position corresponding to the table column
4524 named COL_NAME in the currently active table of UIOUT. */
4525
4526static char *
4527wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4528{
4529 static char wrap_indent[80];
4530 int i, total_width, width, align;
4531 char *text;
4532
4533 total_width = 0;
4534 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4535 {
4536 if (strcmp (text, col_name) == 0)
4537 {
4538 gdb_assert (total_width < sizeof wrap_indent);
4539 memset (wrap_indent, ' ', total_width);
4540 wrap_indent[total_width] = 0;
4541
4542 return wrap_indent;
4543 }
4544
4545 total_width += width + 1;
4546 }
4547
4548 return NULL;
4549}
4550
859825b8
JK
4551/* Print the LOC location out of the list of B->LOC locations. */
4552
170b53b2
UW
4553static void
4554print_breakpoint_location (struct breakpoint *b,
4555 struct bp_location *loc)
0d381245 4556{
79a45e25 4557 struct ui_out *uiout = current_uiout;
6c95b8df
PA
4558 struct cleanup *old_chain = save_current_program_space ();
4559
859825b8
JK
4560 if (loc != NULL && loc->shlib_disabled)
4561 loc = NULL;
4562
6c95b8df
PA
4563 if (loc != NULL)
4564 set_current_program_space (loc->pspace);
4565
56435ebe
TT
4566 if (b->display_canonical)
4567 ui_out_field_string (uiout, "what", b->addr_string);
f8eba3c6 4568 else if (loc && loc->source_file)
0d381245
VP
4569 {
4570 struct symbol *sym
4571 = find_pc_sect_function (loc->address, loc->section);
4572 if (sym)
4573 {
4574 ui_out_text (uiout, "in ");
4575 ui_out_field_string (uiout, "func",
4576 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4577 ui_out_text (uiout, " ");
4578 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4579 ui_out_text (uiout, "at ");
0d381245 4580 }
f8eba3c6 4581 ui_out_field_string (uiout, "file", loc->source_file);
0d381245
VP
4582 ui_out_text (uiout, ":");
4583
4584 if (ui_out_is_mi_like_p (uiout))
4585 {
4586 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4587 char *fullname = symtab_to_fullname (sal.symtab);
4588
4589 if (fullname)
4590 ui_out_field_string (uiout, "fullname", fullname);
4591 }
4592
f8eba3c6 4593 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 4594 }
859825b8 4595 else if (loc)
0d381245 4596 {
170b53b2
UW
4597 struct ui_stream *stb = ui_out_stream_new (uiout);
4598 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4599
22e722e1
DJ
4600 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4601 demangle, "");
0d381245 4602 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4603
4604 do_cleanups (stb_chain);
0d381245 4605 }
859825b8
JK
4606 else
4607 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4608
4609 do_cleanups (old_chain);
0d381245
VP
4610}
4611
269b11a2
PA
4612static const char *
4613bptype_string (enum bptype type)
c906108c 4614{
c4093a6a
JM
4615 struct ep_type_description
4616 {
4617 enum bptype type;
4618 char *description;
4619 };
4620 static struct ep_type_description bptypes[] =
c906108c 4621 {
c5aa993b
JM
4622 {bp_none, "?deleted?"},
4623 {bp_breakpoint, "breakpoint"},
c906108c 4624 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4625 {bp_until, "until"},
4626 {bp_finish, "finish"},
4627 {bp_watchpoint, "watchpoint"},
c906108c 4628 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4629 {bp_read_watchpoint, "read watchpoint"},
4630 {bp_access_watchpoint, "acc watchpoint"},
4631 {bp_longjmp, "longjmp"},
4632 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4633 {bp_exception, "exception"},
4634 {bp_exception_resume, "exception resume"},
c5aa993b 4635 {bp_step_resume, "step resume"},
2c03e5be 4636 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
4637 {bp_watchpoint_scope, "watchpoint scope"},
4638 {bp_call_dummy, "call dummy"},
aa7d318d 4639 {bp_std_terminate, "std::terminate"},
c5aa993b 4640 {bp_shlib_event, "shlib events"},
c4093a6a 4641 {bp_thread_event, "thread events"},
1900040c 4642 {bp_overlay_event, "overlay events"},
0fd8e87f 4643 {bp_longjmp_master, "longjmp master"},
aa7d318d 4644 {bp_std_terminate_master, "std::terminate master"},
186c406b 4645 {bp_exception_master, "exception master"},
ce78b96d 4646 {bp_catchpoint, "catchpoint"},
1042e4c0 4647 {bp_tracepoint, "tracepoint"},
7a697b8d 4648 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4649 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4650 {bp_jit_event, "jit events"},
0e30163f
JK
4651 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4652 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4653 };
269b11a2
PA
4654
4655 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4656 || ((int) type != bptypes[(int) type].type))
4657 internal_error (__FILE__, __LINE__,
4658 _("bptypes table does not describe type #%d."),
4659 (int) type);
4660
4661 return bptypes[(int) type].description;
4662}
4663
4664/* Print B to gdb_stdout. */
4665
4666static void
4667print_one_breakpoint_location (struct breakpoint *b,
4668 struct bp_location *loc,
4669 int loc_number,
4670 struct bp_location **last_loc,
269b11a2
PA
4671 int allflag)
4672{
4673 struct command_line *l;
c2c6d25f 4674 static char bpenables[] = "nynny";
c906108c 4675
79a45e25 4676 struct ui_out *uiout = current_uiout;
0d381245
VP
4677 int header_of_multiple = 0;
4678 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4679 struct value_print_options opts;
4680
4681 get_user_print_options (&opts);
0d381245
VP
4682
4683 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4684 /* See comment in print_one_breakpoint concerning treatment of
4685 breakpoints with single disabled location. */
0d381245
VP
4686 if (loc == NULL
4687 && (b->loc != NULL
4688 && (b->loc->next != NULL || !b->loc->enabled)))
4689 header_of_multiple = 1;
4690 if (loc == NULL)
4691 loc = b->loc;
4692
c4093a6a
JM
4693 annotate_record ();
4694
4695 /* 1 */
4696 annotate_field (0);
0d381245
VP
4697 if (part_of_multiple)
4698 {
4699 char *formatted;
0c6773c1 4700 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4701 ui_out_field_string (uiout, "number", formatted);
4702 xfree (formatted);
4703 }
4704 else
4705 {
4706 ui_out_field_int (uiout, "number", b->number);
4707 }
c4093a6a
JM
4708
4709 /* 2 */
4710 annotate_field (1);
0d381245
VP
4711 if (part_of_multiple)
4712 ui_out_field_skip (uiout, "type");
269b11a2
PA
4713 else
4714 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4715
4716 /* 3 */
4717 annotate_field (2);
0d381245
VP
4718 if (part_of_multiple)
4719 ui_out_field_skip (uiout, "disp");
4720 else
2cec12e5 4721 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4722
c4093a6a
JM
4723
4724 /* 4 */
4725 annotate_field (3);
0d381245 4726 if (part_of_multiple)
54e52265 4727 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4728 else
4a64f543
MS
4729 ui_out_field_fmt (uiout, "enabled", "%c",
4730 bpenables[(int) b->enable_state]);
54e52265 4731 ui_out_spaces (uiout, 2);
0d381245 4732
c4093a6a
JM
4733
4734 /* 5 and 6 */
3086aeae 4735 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4736 {
4a64f543
MS
4737 /* Although the print_one can possibly print all locations,
4738 calling it here is not likely to get any nice result. So,
4739 make sure there's just one location. */
0d381245 4740 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4741 b->ops->print_one (b, last_loc);
0d381245 4742 }
3086aeae
DJ
4743 else
4744 switch (b->type)
4745 {
4746 case bp_none:
4747 internal_error (__FILE__, __LINE__,
e2e0b3e5 4748 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4749 break;
c906108c 4750
3086aeae
DJ
4751 case bp_watchpoint:
4752 case bp_hardware_watchpoint:
4753 case bp_read_watchpoint:
4754 case bp_access_watchpoint:
3a5c3e22
PA
4755 {
4756 struct watchpoint *w = (struct watchpoint *) b;
4757
4758 /* Field 4, the address, is omitted (which makes the columns
4759 not line up too nicely with the headers, but the effect
4760 is relatively readable). */
4761 if (opts.addressprint)
4762 ui_out_field_skip (uiout, "addr");
4763 annotate_field (5);
4764 ui_out_field_string (uiout, "what", w->exp_string);
4765 }
3086aeae
DJ
4766 break;
4767
3086aeae
DJ
4768 case bp_breakpoint:
4769 case bp_hardware_breakpoint:
4770 case bp_until:
4771 case bp_finish:
4772 case bp_longjmp:
4773 case bp_longjmp_resume:
186c406b
TT
4774 case bp_exception:
4775 case bp_exception_resume:
3086aeae 4776 case bp_step_resume:
2c03e5be 4777 case bp_hp_step_resume:
3086aeae
DJ
4778 case bp_watchpoint_scope:
4779 case bp_call_dummy:
aa7d318d 4780 case bp_std_terminate:
3086aeae
DJ
4781 case bp_shlib_event:
4782 case bp_thread_event:
4783 case bp_overlay_event:
0fd8e87f 4784 case bp_longjmp_master:
aa7d318d 4785 case bp_std_terminate_master:
186c406b 4786 case bp_exception_master:
1042e4c0 4787 case bp_tracepoint:
7a697b8d 4788 case bp_fast_tracepoint:
0fb4aa4b 4789 case bp_static_tracepoint:
4efc6507 4790 case bp_jit_event:
0e30163f
JK
4791 case bp_gnu_ifunc_resolver:
4792 case bp_gnu_ifunc_resolver_return:
79a45b7d 4793 if (opts.addressprint)
3086aeae
DJ
4794 {
4795 annotate_field (4);
54e52265 4796 if (header_of_multiple)
0d381245 4797 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4798 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4799 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4800 else
5af949e3
UW
4801 ui_out_field_core_addr (uiout, "addr",
4802 loc->gdbarch, loc->address);
3086aeae
DJ
4803 }
4804 annotate_field (5);
0d381245 4805 if (!header_of_multiple)
170b53b2 4806 print_breakpoint_location (b, loc);
0d381245 4807 if (b->loc)
a6d9a66e 4808 *last_loc = b->loc;
3086aeae
DJ
4809 break;
4810 }
c906108c 4811
6c95b8df
PA
4812
4813 /* For backward compatibility, don't display inferiors unless there
4814 are several. */
4815 if (loc != NULL
4816 && !header_of_multiple
4817 && (allflag
4818 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4819 && (number_of_program_spaces () > 1
4820 || number_of_inferiors () > 1)
4a64f543
MS
4821 /* LOC is for existing B, it cannot be in
4822 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4823 && loc->owner->type != bp_catchpoint)))
4824 {
4825 struct inferior *inf;
4826 int first = 1;
4827
4828 for (inf = inferior_list; inf != NULL; inf = inf->next)
4829 {
4830 if (inf->pspace == loc->pspace)
4831 {
4832 if (first)
4833 {
4834 first = 0;
4835 ui_out_text (uiout, " inf ");
4836 }
4837 else
4838 ui_out_text (uiout, ", ");
4839 ui_out_text (uiout, plongest (inf->num));
4840 }
4841 }
4842 }
4843
4a306c9a 4844 if (!part_of_multiple)
c4093a6a 4845 {
4a306c9a
JB
4846 if (b->thread != -1)
4847 {
4848 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4849 "stop only in" line a little further down. */
4a306c9a
JB
4850 ui_out_text (uiout, " thread ");
4851 ui_out_field_int (uiout, "thread", b->thread);
4852 }
4853 else if (b->task != 0)
4854 {
4855 ui_out_text (uiout, " task ");
4856 ui_out_field_int (uiout, "task", b->task);
4857 }
c4093a6a 4858 }
f1310107 4859
8b93c638 4860 ui_out_text (uiout, "\n");
f1310107 4861
348d480f 4862 if (!part_of_multiple)
f1310107
TJB
4863 b->ops->print_one_detail (b, uiout);
4864
0d381245 4865 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4866 {
4867 annotate_field (6);
8b93c638 4868 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 4869 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 4870 the frame ID. */
5af949e3
UW
4871 ui_out_field_core_addr (uiout, "frame",
4872 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4873 ui_out_text (uiout, "\n");
c4093a6a
JM
4874 }
4875
28010a5d 4876 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
4877 {
4878 annotate_field (7);
d77f58be 4879 if (is_tracepoint (b))
1042e4c0
SS
4880 ui_out_text (uiout, "\ttrace only if ");
4881 else
4882 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4883 ui_out_field_string (uiout, "cond", b->cond_string);
4884 ui_out_text (uiout, "\n");
4885 }
4886
0d381245 4887 if (!part_of_multiple && b->thread != -1)
c4093a6a 4888 {
4a64f543 4889 /* FIXME should make an annotation for this. */
8b93c638
JM
4890 ui_out_text (uiout, "\tstop only in thread ");
4891 ui_out_field_int (uiout, "thread", b->thread);
4892 ui_out_text (uiout, "\n");
c4093a6a
JM
4893 }
4894
63c715c6 4895 if (!part_of_multiple && b->hit_count)
c4093a6a 4896 {
4a64f543 4897 /* FIXME should make an annotation for this. */
8b93c638
JM
4898 if (ep_is_catchpoint (b))
4899 ui_out_text (uiout, "\tcatchpoint");
f196051f
SS
4900 else if (is_tracepoint (b))
4901 ui_out_text (uiout, "\ttracepoint");
8b93c638
JM
4902 else
4903 ui_out_text (uiout, "\tbreakpoint");
4904 ui_out_text (uiout, " already hit ");
4905 ui_out_field_int (uiout, "times", b->hit_count);
4906 if (b->hit_count == 1)
4907 ui_out_text (uiout, " time\n");
4908 else
4909 ui_out_text (uiout, " times\n");
c4093a6a
JM
4910 }
4911
4a64f543
MS
4912 /* Output the count also if it is zero, but only if this is mi.
4913 FIXME: Should have a better test for this. */
9dc5e2a9 4914 if (ui_out_is_mi_like_p (uiout))
63c715c6 4915 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4916 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4917
0d381245 4918 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4919 {
4920 annotate_field (8);
8b93c638
JM
4921 ui_out_text (uiout, "\tignore next ");
4922 ui_out_field_int (uiout, "ignore", b->ignore_count);
4923 ui_out_text (uiout, " hits\n");
c4093a6a 4924 }
059fb39f 4925
f196051f
SS
4926 if (!part_of_multiple && is_tracepoint (b))
4927 {
4928 struct tracepoint *tp = (struct tracepoint *) b;
4929
4930 if (tp->traceframe_usage)
4931 {
4932 ui_out_text (uiout, "\ttrace buffer usage ");
4933 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
4934 ui_out_text (uiout, " bytes\n");
4935 }
4936 }
4937
9add0f1b 4938 l = b->commands ? b->commands->commands : NULL;
059fb39f 4939 if (!part_of_multiple && l)
c4093a6a 4940 {
3b31d625
EZ
4941 struct cleanup *script_chain;
4942
c4093a6a 4943 annotate_field (9);
3b31d625 4944 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4945 print_command_lines (uiout, l, 4);
3b31d625 4946 do_cleanups (script_chain);
c4093a6a 4947 }
d24317b4 4948
d9b3f62e 4949 if (is_tracepoint (b))
1042e4c0 4950 {
d9b3f62e
PA
4951 struct tracepoint *t = (struct tracepoint *) b;
4952
4953 if (!part_of_multiple && t->pass_count)
4954 {
4955 annotate_field (10);
4956 ui_out_text (uiout, "\tpass count ");
4957 ui_out_field_int (uiout, "pass", t->pass_count);
4958 ui_out_text (uiout, " \n");
4959 }
1042e4c0
SS
4960 }
4961
d24317b4
VP
4962 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4963 {
3a5c3e22
PA
4964 if (is_watchpoint (b))
4965 {
4966 struct watchpoint *w = (struct watchpoint *) b;
4967
4968 ui_out_field_string (uiout, "original-location", w->exp_string);
4969 }
4970 else if (b->addr_string)
d24317b4 4971 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 4972 }
c4093a6a 4973}
c5aa993b 4974
0d381245
VP
4975static void
4976print_one_breakpoint (struct breakpoint *b,
4a64f543 4977 struct bp_location **last_loc,
6c95b8df 4978 int allflag)
0d381245 4979{
8d3788bd 4980 struct cleanup *bkpt_chain;
79a45e25 4981 struct ui_out *uiout = current_uiout;
8d3788bd
VP
4982
4983 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
4984
12c5a436 4985 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 4986 do_cleanups (bkpt_chain);
0d381245
VP
4987
4988 /* If this breakpoint has custom print function,
4989 it's already printed. Otherwise, print individual
4990 locations, if any. */
4991 if (b->ops == NULL || b->ops->print_one == NULL)
4992 {
4a64f543
MS
4993 /* If breakpoint has a single location that is disabled, we
4994 print it as if it had several locations, since otherwise it's
4995 hard to represent "breakpoint enabled, location disabled"
4996 situation.
4997
4998 Note that while hardware watchpoints have several locations
a3be7890 4999 internally, that's not a property exposed to user. */
0d381245 5000 if (b->loc
a5606eee 5001 && !is_hardware_watchpoint (b)
8d3788bd 5002 && (b->loc->next || !b->loc->enabled))
0d381245
VP
5003 {
5004 struct bp_location *loc;
5005 int n = 1;
8d3788bd 5006
0d381245 5007 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5008 {
5009 struct cleanup *inner2 =
5010 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5011 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5012 do_cleanups (inner2);
5013 }
0d381245
VP
5014 }
5015 }
5016}
5017
a6d9a66e
UW
5018static int
5019breakpoint_address_bits (struct breakpoint *b)
5020{
5021 int print_address_bits = 0;
5022 struct bp_location *loc;
5023
5024 for (loc = b->loc; loc; loc = loc->next)
5025 {
c7437ca6
PA
5026 int addr_bit;
5027
5028 /* Software watchpoints that aren't watching memory don't have
5029 an address to print. */
5030 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5031 continue;
5032
5033 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5034 if (addr_bit > print_address_bits)
5035 print_address_bits = addr_bit;
5036 }
5037
5038 return print_address_bits;
5039}
0d381245 5040
c4093a6a
JM
5041struct captured_breakpoint_query_args
5042 {
5043 int bnum;
5044 };
c5aa993b 5045
c4093a6a 5046static int
2b65245e 5047do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5048{
5049 struct captured_breakpoint_query_args *args = data;
52f0bd74 5050 struct breakpoint *b;
a6d9a66e 5051 struct bp_location *dummy_loc = NULL;
cc59ec59 5052
c4093a6a
JM
5053 ALL_BREAKPOINTS (b)
5054 {
5055 if (args->bnum == b->number)
c5aa993b 5056 {
12c5a436 5057 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5058 return GDB_RC_OK;
c5aa993b 5059 }
c4093a6a
JM
5060 }
5061 return GDB_RC_NONE;
5062}
c5aa993b 5063
c4093a6a 5064enum gdb_rc
4a64f543
MS
5065gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5066 char **error_message)
c4093a6a
JM
5067{
5068 struct captured_breakpoint_query_args args;
cc59ec59 5069
c4093a6a
JM
5070 args.bnum = bnum;
5071 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5072 an error. */
b0b13bb4
DJ
5073 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5074 error_message, RETURN_MASK_ALL) < 0)
5075 return GDB_RC_FAIL;
5076 else
5077 return GDB_RC_OK;
c4093a6a 5078}
c5aa993b 5079
09d682a4
TT
5080/* Return true if this breakpoint was set by the user, false if it is
5081 internal or momentary. */
5082
5083int
5084user_breakpoint_p (struct breakpoint *b)
5085{
46c6471b 5086 return b->number > 0;
09d682a4
TT
5087}
5088
7f3b0473 5089/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5090 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5091 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5092 FILTER is non-NULL, call it on each breakpoint and only include the
5093 ones for which it returns non-zero. Return the total number of
5094 breakpoints listed. */
c906108c 5095
d77f58be 5096static int
e5a67952 5097breakpoint_1 (char *args, int allflag,
4a64f543 5098 int (*filter) (const struct breakpoint *))
c4093a6a 5099{
52f0bd74 5100 struct breakpoint *b;
a6d9a66e 5101 struct bp_location *last_loc = NULL;
7f3b0473 5102 int nr_printable_breakpoints;
3b31d625 5103 struct cleanup *bkpttbl_chain;
79a45b7d 5104 struct value_print_options opts;
a6d9a66e 5105 int print_address_bits = 0;
269b11a2 5106 int print_type_col_width = 14;
79a45e25 5107 struct ui_out *uiout = current_uiout;
269b11a2 5108
79a45b7d
TT
5109 get_user_print_options (&opts);
5110
4a64f543
MS
5111 /* Compute the number of rows in the table, as well as the size
5112 required for address fields. */
7f3b0473
AC
5113 nr_printable_breakpoints = 0;
5114 ALL_BREAKPOINTS (b)
e5a67952
MS
5115 {
5116 /* If we have a filter, only list the breakpoints it accepts. */
5117 if (filter && !filter (b))
5118 continue;
5119
5120 /* If we have an "args" string, it is a list of breakpoints to
5121 accept. Skip the others. */
5122 if (args != NULL && *args != '\0')
5123 {
5124 if (allflag && parse_and_eval_long (args) != b->number)
5125 continue;
5126 if (!allflag && !number_is_in_list (args, b->number))
5127 continue;
5128 }
269b11a2 5129
e5a67952
MS
5130 if (allflag || user_breakpoint_p (b))
5131 {
5132 int addr_bit, type_len;
a6d9a66e 5133
e5a67952
MS
5134 addr_bit = breakpoint_address_bits (b);
5135 if (addr_bit > print_address_bits)
5136 print_address_bits = addr_bit;
269b11a2 5137
e5a67952
MS
5138 type_len = strlen (bptype_string (b->type));
5139 if (type_len > print_type_col_width)
5140 print_type_col_width = type_len;
5141
5142 nr_printable_breakpoints++;
5143 }
5144 }
7f3b0473 5145
79a45b7d 5146 if (opts.addressprint)
3b31d625 5147 bkpttbl_chain
3e43a32a
MS
5148 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5149 nr_printable_breakpoints,
3b31d625 5150 "BreakpointTable");
8b93c638 5151 else
3b31d625 5152 bkpttbl_chain
3e43a32a
MS
5153 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5154 nr_printable_breakpoints,
3b31d625 5155 "BreakpointTable");
8b93c638 5156
7f3b0473 5157 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5158 annotate_breakpoints_headers ();
5159 if (nr_printable_breakpoints > 0)
5160 annotate_field (0);
4a64f543 5161 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5162 if (nr_printable_breakpoints > 0)
5163 annotate_field (1);
269b11a2 5164 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5165 "type", "Type"); /* 2 */
d7faa9e7
AC
5166 if (nr_printable_breakpoints > 0)
5167 annotate_field (2);
4a64f543 5168 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5169 if (nr_printable_breakpoints > 0)
5170 annotate_field (3);
54e52265 5171 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5172 if (opts.addressprint)
e5a67952
MS
5173 {
5174 if (nr_printable_breakpoints > 0)
5175 annotate_field (4);
5176 if (print_address_bits <= 32)
5177 ui_out_table_header (uiout, 10, ui_left,
5178 "addr", "Address"); /* 5 */
5179 else
5180 ui_out_table_header (uiout, 18, ui_left,
5181 "addr", "Address"); /* 5 */
5182 }
d7faa9e7
AC
5183 if (nr_printable_breakpoints > 0)
5184 annotate_field (5);
5185 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5186 ui_out_table_body (uiout);
5187 if (nr_printable_breakpoints > 0)
5188 annotate_breakpoints_table ();
7f3b0473 5189
c4093a6a 5190 ALL_BREAKPOINTS (b)
e5a67952
MS
5191 {
5192 QUIT;
5193 /* If we have a filter, only list the breakpoints it accepts. */
5194 if (filter && !filter (b))
5195 continue;
5196
5197 /* If we have an "args" string, it is a list of breakpoints to
5198 accept. Skip the others. */
5199
5200 if (args != NULL && *args != '\0')
5201 {
5202 if (allflag) /* maintenance info breakpoint */
5203 {
5204 if (parse_and_eval_long (args) != b->number)
5205 continue;
5206 }
5207 else /* all others */
5208 {
5209 if (!number_is_in_list (args, b->number))
5210 continue;
5211 }
5212 }
5213 /* We only print out user settable breakpoints unless the
5214 allflag is set. */
5215 if (allflag || user_breakpoint_p (b))
12c5a436 5216 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5217 }
5218
3b31d625 5219 do_cleanups (bkpttbl_chain);
698384cd 5220
7f3b0473 5221 if (nr_printable_breakpoints == 0)
c906108c 5222 {
4a64f543
MS
5223 /* If there's a filter, let the caller decide how to report
5224 empty list. */
d77f58be
SS
5225 if (!filter)
5226 {
e5a67952 5227 if (args == NULL || *args == '\0')
d77f58be
SS
5228 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5229 else
4a64f543 5230 ui_out_message (uiout, 0,
e5a67952
MS
5231 "No breakpoint or watchpoint matching '%s'.\n",
5232 args);
d77f58be 5233 }
c906108c
SS
5234 }
5235 else
c4093a6a 5236 {
a6d9a66e
UW
5237 if (last_loc && !server_command)
5238 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5239 }
c906108c 5240
4a64f543 5241 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5242 there have been breakpoints? */
c906108c 5243 annotate_breakpoints_table_end ();
d77f58be
SS
5244
5245 return nr_printable_breakpoints;
c906108c
SS
5246}
5247
ad443146
SS
5248/* Display the value of default-collect in a way that is generally
5249 compatible with the breakpoint list. */
5250
5251static void
5252default_collect_info (void)
5253{
79a45e25
PA
5254 struct ui_out *uiout = current_uiout;
5255
ad443146
SS
5256 /* If it has no value (which is frequently the case), say nothing; a
5257 message like "No default-collect." gets in user's face when it's
5258 not wanted. */
5259 if (!*default_collect)
5260 return;
5261
5262 /* The following phrase lines up nicely with per-tracepoint collect
5263 actions. */
5264 ui_out_text (uiout, "default collect ");
5265 ui_out_field_string (uiout, "default-collect", default_collect);
5266 ui_out_text (uiout, " \n");
5267}
5268
c906108c 5269static void
e5a67952 5270breakpoints_info (char *args, int from_tty)
c906108c 5271{
e5a67952 5272 breakpoint_1 (args, 0, NULL);
ad443146
SS
5273
5274 default_collect_info ();
d77f58be
SS
5275}
5276
5277static void
e5a67952 5278watchpoints_info (char *args, int from_tty)
d77f58be 5279{
e5a67952 5280 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 5281 struct ui_out *uiout = current_uiout;
d77f58be
SS
5282
5283 if (num_printed == 0)
5284 {
e5a67952 5285 if (args == NULL || *args == '\0')
d77f58be
SS
5286 ui_out_message (uiout, 0, "No watchpoints.\n");
5287 else
e5a67952 5288 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5289 }
c906108c
SS
5290}
5291
7a292a7a 5292static void
e5a67952 5293maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5294{
e5a67952 5295 breakpoint_1 (args, 1, NULL);
ad443146
SS
5296
5297 default_collect_info ();
c906108c
SS
5298}
5299
0d381245 5300static int
714835d5 5301breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5302 struct program_space *pspace,
714835d5 5303 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5304{
5305 struct bp_location *bl = b->loc;
cc59ec59 5306
0d381245
VP
5307 for (; bl; bl = bl->next)
5308 {
6c95b8df
PA
5309 if (bl->pspace == pspace
5310 && bl->address == pc
0d381245
VP
5311 && (!overlay_debugging || bl->section == section))
5312 return 1;
5313 }
5314 return 0;
5315}
5316
672f9b60 5317/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
5318 concerns with logical breakpoints, so we match program spaces, not
5319 address spaces. */
c906108c
SS
5320
5321static void
6c95b8df
PA
5322describe_other_breakpoints (struct gdbarch *gdbarch,
5323 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5324 struct obj_section *section, int thread)
c906108c 5325{
52f0bd74
AC
5326 int others = 0;
5327 struct breakpoint *b;
c906108c
SS
5328
5329 ALL_BREAKPOINTS (b)
672f9b60
KP
5330 others += (user_breakpoint_p (b)
5331 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
5332 if (others > 0)
5333 {
a3f17187
AC
5334 if (others == 1)
5335 printf_filtered (_("Note: breakpoint "));
5336 else /* if (others == ???) */
5337 printf_filtered (_("Note: breakpoints "));
c906108c 5338 ALL_BREAKPOINTS (b)
672f9b60 5339 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5340 {
5341 others--;
5342 printf_filtered ("%d", b->number);
5343 if (b->thread == -1 && thread != -1)
5344 printf_filtered (" (all threads)");
5345 else if (b->thread != -1)
5346 printf_filtered (" (thread %d)", b->thread);
5347 printf_filtered ("%s%s ",
059fb39f 5348 ((b->enable_state == bp_disabled
f8eba3c6 5349 || b->enable_state == bp_call_disabled)
0d381245
VP
5350 ? " (disabled)"
5351 : b->enable_state == bp_permanent
5352 ? " (permanent)"
5353 : ""),
5354 (others > 1) ? ","
5355 : ((others == 1) ? " and" : ""));
5356 }
a3f17187 5357 printf_filtered (_("also set at pc "));
5af949e3 5358 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5359 printf_filtered (".\n");
5360 }
5361}
5362\f
c906108c 5363
e4f237da
KB
5364/* Return true iff it is meaningful to use the address member of
5365 BPT. For some breakpoint types, the address member is irrelevant
5366 and it makes no sense to attempt to compare it to other addresses
5367 (or use it for any other purpose either).
5368
4a64f543
MS
5369 More specifically, each of the following breakpoint types will
5370 always have a zero valued address and we don't want to mark
5371 breakpoints of any of these types to be a duplicate of an actual
5372 breakpoint at address zero:
e4f237da
KB
5373
5374 bp_watchpoint
2d134ed3
PA
5375 bp_catchpoint
5376
5377*/
e4f237da
KB
5378
5379static int
5380breakpoint_address_is_meaningful (struct breakpoint *bpt)
5381{
5382 enum bptype type = bpt->type;
5383
2d134ed3
PA
5384 return (type != bp_watchpoint && type != bp_catchpoint);
5385}
5386
5387/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5388 true if LOC1 and LOC2 represent the same watchpoint location. */
5389
5390static int
4a64f543
MS
5391watchpoint_locations_match (struct bp_location *loc1,
5392 struct bp_location *loc2)
2d134ed3 5393{
3a5c3e22
PA
5394 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
5395 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
5396
5397 /* Both of them must exist. */
5398 gdb_assert (w1 != NULL);
5399 gdb_assert (w2 != NULL);
2bdf28a0 5400
4a64f543
MS
5401 /* If the target can evaluate the condition expression in hardware,
5402 then we we need to insert both watchpoints even if they are at
5403 the same place. Otherwise the watchpoint will only trigger when
5404 the condition of whichever watchpoint was inserted evaluates to
5405 true, not giving a chance for GDB to check the condition of the
5406 other watchpoint. */
3a5c3e22 5407 if ((w1->cond_exp
4a64f543
MS
5408 && target_can_accel_watchpoint_condition (loc1->address,
5409 loc1->length,
0cf6dd15 5410 loc1->watchpoint_type,
3a5c3e22
PA
5411 w1->cond_exp))
5412 || (w2->cond_exp
4a64f543
MS
5413 && target_can_accel_watchpoint_condition (loc2->address,
5414 loc2->length,
0cf6dd15 5415 loc2->watchpoint_type,
3a5c3e22 5416 w2->cond_exp)))
0cf6dd15
TJB
5417 return 0;
5418
85d721b8
PA
5419 /* Note that this checks the owner's type, not the location's. In
5420 case the target does not support read watchpoints, but does
5421 support access watchpoints, we'll have bp_read_watchpoint
5422 watchpoints with hw_access locations. Those should be considered
5423 duplicates of hw_read locations. The hw_read locations will
5424 become hw_access locations later. */
2d134ed3
PA
5425 return (loc1->owner->type == loc2->owner->type
5426 && loc1->pspace->aspace == loc2->pspace->aspace
5427 && loc1->address == loc2->address
5428 && loc1->length == loc2->length);
e4f237da
KB
5429}
5430
6c95b8df
PA
5431/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5432 same breakpoint location. In most targets, this can only be true
5433 if ASPACE1 matches ASPACE2. On targets that have global
5434 breakpoints, the address space doesn't really matter. */
5435
5436static int
5437breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5438 struct address_space *aspace2, CORE_ADDR addr2)
5439{
5440 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5441 || aspace1 == aspace2)
5442 && addr1 == addr2);
5443}
5444
f1310107
TJB
5445/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5446 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5447 matches ASPACE2. On targets that have global breakpoints, the address
5448 space doesn't really matter. */
5449
5450static int
5451breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5452 int len1, struct address_space *aspace2,
5453 CORE_ADDR addr2)
5454{
5455 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5456 || aspace1 == aspace2)
5457 && addr2 >= addr1 && addr2 < addr1 + len1);
5458}
5459
5460/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5461 a ranged breakpoint. In most targets, a match happens only if ASPACE
5462 matches the breakpoint's address space. On targets that have global
5463 breakpoints, the address space doesn't really matter. */
5464
5465static int
5466breakpoint_location_address_match (struct bp_location *bl,
5467 struct address_space *aspace,
5468 CORE_ADDR addr)
5469{
5470 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5471 aspace, addr)
5472 || (bl->length
5473 && breakpoint_address_match_range (bl->pspace->aspace,
5474 bl->address, bl->length,
5475 aspace, addr)));
5476}
5477
1e4d1764
YQ
5478/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
5479 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
5480 true, otherwise returns false. */
5481
5482static int
5483tracepoint_locations_match (struct bp_location *loc1,
5484 struct bp_location *loc2)
5485{
5486 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
5487 /* Since tracepoint locations are never duplicated with others', tracepoint
5488 locations at the same address of different tracepoints are regarded as
5489 different locations. */
5490 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
5491 else
5492 return 0;
5493}
5494
2d134ed3
PA
5495/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5496 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5497 represent the same location. */
5498
5499static int
4a64f543
MS
5500breakpoint_locations_match (struct bp_location *loc1,
5501 struct bp_location *loc2)
2d134ed3 5502{
2bdf28a0
JK
5503 int hw_point1, hw_point2;
5504
5505 /* Both of them must not be in moribund_locations. */
5506 gdb_assert (loc1->owner != NULL);
5507 gdb_assert (loc2->owner != NULL);
5508
5509 hw_point1 = is_hardware_watchpoint (loc1->owner);
5510 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5511
5512 if (hw_point1 != hw_point2)
5513 return 0;
5514 else if (hw_point1)
5515 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
5516 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
5517 return tracepoint_locations_match (loc1, loc2);
2d134ed3 5518 else
f1310107
TJB
5519 /* We compare bp_location.length in order to cover ranged breakpoints. */
5520 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5521 loc2->pspace->aspace, loc2->address)
5522 && loc1->length == loc2->length);
2d134ed3
PA
5523}
5524
76897487
KB
5525static void
5526breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5527 int bnum, int have_bnum)
5528{
f63fbe86
MS
5529 /* The longest string possibly returned by hex_string_custom
5530 is 50 chars. These must be at least that big for safety. */
5531 char astr1[64];
5532 char astr2[64];
76897487 5533
bb599908
PH
5534 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5535 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5536 if (have_bnum)
8a3fe4f8 5537 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5538 bnum, astr1, astr2);
5539 else
8a3fe4f8 5540 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5541}
5542
4a64f543
MS
5543/* Adjust a breakpoint's address to account for architectural
5544 constraints on breakpoint placement. Return the adjusted address.
5545 Note: Very few targets require this kind of adjustment. For most
5546 targets, this function is simply the identity function. */
76897487
KB
5547
5548static CORE_ADDR
a6d9a66e
UW
5549adjust_breakpoint_address (struct gdbarch *gdbarch,
5550 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5551{
a6d9a66e 5552 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5553 {
5554 /* Very few targets need any kind of breakpoint adjustment. */
5555 return bpaddr;
5556 }
88f7da05
KB
5557 else if (bptype == bp_watchpoint
5558 || bptype == bp_hardware_watchpoint
5559 || bptype == bp_read_watchpoint
5560 || bptype == bp_access_watchpoint
fe798b75 5561 || bptype == bp_catchpoint)
88f7da05
KB
5562 {
5563 /* Watchpoints and the various bp_catch_* eventpoints should not
5564 have their addresses modified. */
5565 return bpaddr;
5566 }
76897487
KB
5567 else
5568 {
5569 CORE_ADDR adjusted_bpaddr;
5570
5571 /* Some targets have architectural constraints on the placement
5572 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5573 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5574
5575 /* An adjusted breakpoint address can significantly alter
5576 a user's expectations. Print a warning if an adjustment
5577 is required. */
5578 if (adjusted_bpaddr != bpaddr)
5579 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5580
5581 return adjusted_bpaddr;
5582 }
5583}
5584
28010a5d
PA
5585void
5586init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5587 struct breakpoint *owner)
7cc221ef 5588{
7cc221ef
DJ
5589 memset (loc, 0, sizeof (*loc));
5590
348d480f
PA
5591 gdb_assert (ops != NULL);
5592
28010a5d
PA
5593 loc->ops = ops;
5594 loc->owner = owner;
511a6cd4 5595 loc->cond = NULL;
0d381245
VP
5596 loc->shlib_disabled = 0;
5597 loc->enabled = 1;
e049a4b5 5598
28010a5d 5599 switch (owner->type)
e049a4b5
DJ
5600 {
5601 case bp_breakpoint:
5602 case bp_until:
5603 case bp_finish:
5604 case bp_longjmp:
5605 case bp_longjmp_resume:
186c406b
TT
5606 case bp_exception:
5607 case bp_exception_resume:
e049a4b5 5608 case bp_step_resume:
2c03e5be 5609 case bp_hp_step_resume:
e049a4b5
DJ
5610 case bp_watchpoint_scope:
5611 case bp_call_dummy:
aa7d318d 5612 case bp_std_terminate:
e049a4b5
DJ
5613 case bp_shlib_event:
5614 case bp_thread_event:
5615 case bp_overlay_event:
4efc6507 5616 case bp_jit_event:
0fd8e87f 5617 case bp_longjmp_master:
aa7d318d 5618 case bp_std_terminate_master:
186c406b 5619 case bp_exception_master:
0e30163f
JK
5620 case bp_gnu_ifunc_resolver:
5621 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5622 loc->loc_type = bp_loc_software_breakpoint;
5623 break;
5624 case bp_hardware_breakpoint:
5625 loc->loc_type = bp_loc_hardware_breakpoint;
5626 break;
5627 case bp_hardware_watchpoint:
5628 case bp_read_watchpoint:
5629 case bp_access_watchpoint:
5630 loc->loc_type = bp_loc_hardware_watchpoint;
5631 break;
5632 case bp_watchpoint:
ce78b96d 5633 case bp_catchpoint:
15c3d785
PA
5634 case bp_tracepoint:
5635 case bp_fast_tracepoint:
0fb4aa4b 5636 case bp_static_tracepoint:
e049a4b5
DJ
5637 loc->loc_type = bp_loc_other;
5638 break;
5639 default:
e2e0b3e5 5640 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5641 }
5642
f431efe5 5643 loc->refc = 1;
28010a5d
PA
5644}
5645
5646/* Allocate a struct bp_location. */
5647
5648static struct bp_location *
5649allocate_bp_location (struct breakpoint *bpt)
5650{
348d480f
PA
5651 return bpt->ops->allocate_location (bpt);
5652}
7cc221ef 5653
f431efe5
PA
5654static void
5655free_bp_location (struct bp_location *loc)
fe3f5fa8 5656{
348d480f 5657 loc->ops->dtor (loc);
fe3f5fa8
VP
5658 xfree (loc);
5659}
5660
f431efe5
PA
5661/* Increment reference count. */
5662
5663static void
5664incref_bp_location (struct bp_location *bl)
5665{
5666 ++bl->refc;
5667}
5668
5669/* Decrement reference count. If the reference count reaches 0,
5670 destroy the bp_location. Sets *BLP to NULL. */
5671
5672static void
5673decref_bp_location (struct bp_location **blp)
5674{
0807b50c
PA
5675 gdb_assert ((*blp)->refc > 0);
5676
f431efe5
PA
5677 if (--(*blp)->refc == 0)
5678 free_bp_location (*blp);
5679 *blp = NULL;
5680}
5681
346774a9 5682/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 5683
346774a9
PA
5684static void
5685add_to_breakpoint_chain (struct breakpoint *b)
c906108c 5686{
346774a9 5687 struct breakpoint *b1;
c906108c 5688
346774a9
PA
5689 /* Add this breakpoint to the end of the chain so that a list of
5690 breakpoints will come out in order of increasing numbers. */
5691
5692 b1 = breakpoint_chain;
5693 if (b1 == 0)
5694 breakpoint_chain = b;
5695 else
5696 {
5697 while (b1->next)
5698 b1 = b1->next;
5699 b1->next = b;
5700 }
5701}
5702
5703/* Initializes breakpoint B with type BPTYPE and no locations yet. */
5704
5705static void
5706init_raw_breakpoint_without_location (struct breakpoint *b,
5707 struct gdbarch *gdbarch,
28010a5d 5708 enum bptype bptype,
c0a91b2b 5709 const struct breakpoint_ops *ops)
346774a9 5710{
c906108c 5711 memset (b, 0, sizeof (*b));
2219d63c 5712
348d480f
PA
5713 gdb_assert (ops != NULL);
5714
28010a5d 5715 b->ops = ops;
4d28f7a8 5716 b->type = bptype;
a6d9a66e 5717 b->gdbarch = gdbarch;
c906108c
SS
5718 b->language = current_language->la_language;
5719 b->input_radix = input_radix;
5720 b->thread = -1;
b5de0fa7 5721 b->enable_state = bp_enabled;
c906108c
SS
5722 b->next = 0;
5723 b->silent = 0;
5724 b->ignore_count = 0;
5725 b->commands = NULL;
818dd999 5726 b->frame_id = null_frame_id;
0d381245 5727 b->condition_not_parsed = 0;
84f4c1fe 5728 b->py_bp_object = NULL;
d0fb5eae 5729 b->related_breakpoint = b;
346774a9
PA
5730}
5731
5732/* Helper to set_raw_breakpoint below. Creates a breakpoint
5733 that has type BPTYPE and has no locations as yet. */
346774a9
PA
5734
5735static struct breakpoint *
5736set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 5737 enum bptype bptype,
c0a91b2b 5738 const struct breakpoint_ops *ops)
346774a9
PA
5739{
5740 struct breakpoint *b = XNEW (struct breakpoint);
5741
348d480f 5742 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 5743 add_to_breakpoint_chain (b);
0d381245
VP
5744 return b;
5745}
5746
0e30163f
JK
5747/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5748 resolutions should be made as the user specified the location explicitly
5749 enough. */
5750
0d381245 5751static void
0e30163f 5752set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5753{
2bdf28a0
JK
5754 gdb_assert (loc->owner != NULL);
5755
0d381245 5756 if (loc->owner->type == bp_breakpoint
1042e4c0 5757 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5758 || is_tracepoint (loc->owner))
0d381245 5759 {
0e30163f
JK
5760 int is_gnu_ifunc;
5761
5762 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5763 NULL, NULL, &is_gnu_ifunc);
5764
5765 if (is_gnu_ifunc && !explicit_loc)
5766 {
5767 struct breakpoint *b = loc->owner;
5768
5769 gdb_assert (loc->pspace == current_program_space);
5770 if (gnu_ifunc_resolve_name (loc->function_name,
5771 &loc->requested_address))
5772 {
5773 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5774 loc->address = adjust_breakpoint_address (loc->gdbarch,
5775 loc->requested_address,
5776 b->type);
5777 }
5778 else if (b->type == bp_breakpoint && b->loc == loc
5779 && loc->next == NULL && b->related_breakpoint == b)
5780 {
5781 /* Create only the whole new breakpoint of this type but do not
5782 mess more complicated breakpoints with multiple locations. */
5783 b->type = bp_gnu_ifunc_resolver;
5784 }
5785 }
5786
0d381245
VP
5787 if (loc->function_name)
5788 loc->function_name = xstrdup (loc->function_name);
5789 }
5790}
5791
a6d9a66e 5792/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 5793struct gdbarch *
a6d9a66e
UW
5794get_sal_arch (struct symtab_and_line sal)
5795{
5796 if (sal.section)
5797 return get_objfile_arch (sal.section->objfile);
5798 if (sal.symtab)
5799 return get_objfile_arch (sal.symtab->objfile);
5800
5801 return NULL;
5802}
5803
346774a9
PA
5804/* Low level routine for partially initializing a breakpoint of type
5805 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 5806 file name, and line number are provided by SAL.
0d381245
VP
5807
5808 It is expected that the caller will complete the initialization of
5809 the newly created breakpoint struct as well as output any status
c56053d2 5810 information regarding the creation of a new breakpoint. */
0d381245 5811
346774a9
PA
5812static void
5813init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 5814 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5815 const struct breakpoint_ops *ops)
0d381245 5816{
28010a5d 5817 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 5818
3742cc8b 5819 add_location_to_breakpoint (b, &sal);
0d381245 5820
6c95b8df
PA
5821 if (bptype != bp_catchpoint)
5822 gdb_assert (sal.pspace != NULL);
5823
f8eba3c6
TT
5824 /* Store the program space that was used to set the breakpoint,
5825 except for ordinary breakpoints, which are independent of the
5826 program space. */
5827 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
5828 b->pspace = sal.pspace;
0d381245 5829
c906108c 5830 breakpoints_changed ();
346774a9 5831}
c906108c 5832
346774a9
PA
5833/* set_raw_breakpoint is a low level routine for allocating and
5834 partially initializing a breakpoint of type BPTYPE. The newly
5835 created breakpoint's address, section, source file name, and line
5836 number are provided by SAL. The newly created and partially
5837 initialized breakpoint is added to the breakpoint chain and
5838 is also returned as the value of this function.
5839
5840 It is expected that the caller will complete the initialization of
5841 the newly created breakpoint struct as well as output any status
5842 information regarding the creation of a new breakpoint. In
5843 particular, set_raw_breakpoint does NOT set the breakpoint
5844 number! Care should be taken to not allow an error to occur
5845 prior to completing the initialization of the breakpoint. If this
5846 should happen, a bogus breakpoint will be left on the chain. */
5847
5848struct breakpoint *
5849set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 5850 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5851 const struct breakpoint_ops *ops)
346774a9
PA
5852{
5853 struct breakpoint *b = XNEW (struct breakpoint);
5854
348d480f 5855 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 5856 add_to_breakpoint_chain (b);
c906108c
SS
5857 return b;
5858}
5859
c2c6d25f
JM
5860
5861/* Note that the breakpoint object B describes a permanent breakpoint
5862 instruction, hard-wired into the inferior's code. */
5863void
5864make_breakpoint_permanent (struct breakpoint *b)
5865{
0d381245 5866 struct bp_location *bl;
cc59ec59 5867
b5de0fa7 5868 b->enable_state = bp_permanent;
c2c6d25f 5869
4a64f543
MS
5870 /* By definition, permanent breakpoints are already present in the
5871 code. Mark all locations as inserted. For now,
5872 make_breakpoint_permanent is called in just one place, so it's
5873 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 5874 multiple locations or not, but it's easy to implement. */
0d381245
VP
5875 for (bl = b->loc; bl; bl = bl->next)
5876 bl->inserted = 1;
c2c6d25f
JM
5877}
5878
53a5351d 5879/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5880 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5881 initiated the operation. */
c906108c
SS
5882
5883void
186c406b 5884set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5885{
35df4500 5886 struct breakpoint *b, *b_tmp;
186c406b 5887 int thread = tp->num;
0fd8e87f
UW
5888
5889 /* To avoid having to rescan all objfile symbols at every step,
5890 we maintain a list of continually-inserted but always disabled
5891 longjmp "master" breakpoints. Here, we simply create momentary
5892 clones of those and enable them for the requested thread. */
35df4500 5893 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5894 if (b->pspace == current_program_space
186c406b
TT
5895 && (b->type == bp_longjmp_master
5896 || b->type == bp_exception_master))
0fd8e87f 5897 {
06edf0c0
PA
5898 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5899 struct breakpoint *clone;
cc59ec59 5900
06edf0c0
PA
5901 clone = momentary_breakpoint_from_master (b, type,
5902 &momentary_breakpoint_ops);
0fd8e87f
UW
5903 clone->thread = thread;
5904 }
186c406b
TT
5905
5906 tp->initiating_frame = frame;
c906108c
SS
5907}
5908
611c83ae 5909/* Delete all longjmp breakpoints from THREAD. */
c906108c 5910void
611c83ae 5911delete_longjmp_breakpoint (int thread)
c906108c 5912{
35df4500 5913 struct breakpoint *b, *b_tmp;
c906108c 5914
35df4500 5915 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5916 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5917 {
5918 if (b->thread == thread)
5919 delete_breakpoint (b);
5920 }
c906108c
SS
5921}
5922
1900040c
MS
5923void
5924enable_overlay_breakpoints (void)
5925{
52f0bd74 5926 struct breakpoint *b;
1900040c
MS
5927
5928 ALL_BREAKPOINTS (b)
5929 if (b->type == bp_overlay_event)
5930 {
5931 b->enable_state = bp_enabled;
b60e7edf 5932 update_global_location_list (1);
c02f5703 5933 overlay_events_enabled = 1;
1900040c
MS
5934 }
5935}
5936
5937void
5938disable_overlay_breakpoints (void)
5939{
52f0bd74 5940 struct breakpoint *b;
1900040c
MS
5941
5942 ALL_BREAKPOINTS (b)
5943 if (b->type == bp_overlay_event)
5944 {
5945 b->enable_state = bp_disabled;
b60e7edf 5946 update_global_location_list (0);
c02f5703 5947 overlay_events_enabled = 0;
1900040c
MS
5948 }
5949}
5950
aa7d318d
TT
5951/* Set an active std::terminate breakpoint for each std::terminate
5952 master breakpoint. */
5953void
5954set_std_terminate_breakpoint (void)
5955{
35df4500 5956 struct breakpoint *b, *b_tmp;
aa7d318d 5957
35df4500 5958 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5959 if (b->pspace == current_program_space
5960 && b->type == bp_std_terminate_master)
5961 {
06edf0c0
PA
5962 momentary_breakpoint_from_master (b, bp_std_terminate,
5963 &momentary_breakpoint_ops);
aa7d318d
TT
5964 }
5965}
5966
5967/* Delete all the std::terminate breakpoints. */
5968void
5969delete_std_terminate_breakpoint (void)
5970{
35df4500 5971 struct breakpoint *b, *b_tmp;
aa7d318d 5972
35df4500 5973 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5974 if (b->type == bp_std_terminate)
5975 delete_breakpoint (b);
5976}
5977
c4093a6a 5978struct breakpoint *
a6d9a66e 5979create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
5980{
5981 struct breakpoint *b;
c4093a6a 5982
06edf0c0
PA
5983 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
5984 &internal_breakpoint_ops);
5985
b5de0fa7 5986 b->enable_state = bp_enabled;
c4093a6a 5987 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
5988 b->addr_string
5989 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 5990
b60e7edf 5991 update_global_location_list_nothrow (1);
74960c60 5992
c4093a6a
JM
5993 return b;
5994}
5995
5996void
5997remove_thread_event_breakpoints (void)
5998{
35df4500 5999 struct breakpoint *b, *b_tmp;
c4093a6a 6000
35df4500 6001 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6002 if (b->type == bp_thread_event
6003 && b->loc->pspace == current_program_space)
c4093a6a
JM
6004 delete_breakpoint (b);
6005}
6006
0101ce28
JJ
6007struct lang_and_radix
6008 {
6009 enum language lang;
6010 int radix;
6011 };
6012
4efc6507
DE
6013/* Create a breakpoint for JIT code registration and unregistration. */
6014
6015struct breakpoint *
6016create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6017{
6018 struct breakpoint *b;
6019
06edf0c0
PA
6020 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
6021 &internal_breakpoint_ops);
4efc6507
DE
6022 update_global_location_list_nothrow (1);
6023 return b;
6024}
0101ce28 6025
03673fc7
PP
6026/* Remove JIT code registration and unregistration breakpoint(s). */
6027
6028void
6029remove_jit_event_breakpoints (void)
6030{
6031 struct breakpoint *b, *b_tmp;
6032
6033 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6034 if (b->type == bp_jit_event
6035 && b->loc->pspace == current_program_space)
6036 delete_breakpoint (b);
6037}
6038
cae688ec
JJ
6039void
6040remove_solib_event_breakpoints (void)
6041{
35df4500 6042 struct breakpoint *b, *b_tmp;
cae688ec 6043
35df4500 6044 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6045 if (b->type == bp_shlib_event
6046 && b->loc->pspace == current_program_space)
cae688ec
JJ
6047 delete_breakpoint (b);
6048}
6049
6050struct breakpoint *
a6d9a66e 6051create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6052{
6053 struct breakpoint *b;
6054
06edf0c0
PA
6055 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6056 &internal_breakpoint_ops);
b60e7edf 6057 update_global_location_list_nothrow (1);
cae688ec
JJ
6058 return b;
6059}
6060
6061/* Disable any breakpoints that are on code in shared libraries. Only
6062 apply to enabled breakpoints, disabled ones can just stay disabled. */
6063
6064void
cb851954 6065disable_breakpoints_in_shlibs (void)
cae688ec 6066{
876fa593 6067 struct bp_location *loc, **locp_tmp;
cae688ec 6068
876fa593 6069 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6070 {
2bdf28a0 6071 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6072 struct breakpoint *b = loc->owner;
2bdf28a0 6073
4a64f543
MS
6074 /* We apply the check to all breakpoints, including disabled for
6075 those with loc->duplicate set. This is so that when breakpoint
6076 becomes enabled, or the duplicate is removed, gdb will try to
6077 insert all breakpoints. If we don't set shlib_disabled here,
6078 we'll try to insert those breakpoints and fail. */
1042e4c0 6079 if (((b->type == bp_breakpoint)
508ccb1f 6080 || (b->type == bp_jit_event)
1042e4c0 6081 || (b->type == bp_hardware_breakpoint)
d77f58be 6082 || (is_tracepoint (b)))
6c95b8df 6083 && loc->pspace == current_program_space
0d381245 6084 && !loc->shlib_disabled
a77053c2 6085#ifdef PC_SOLIB
0d381245 6086 && PC_SOLIB (loc->address)
a77053c2 6087#else
6c95b8df 6088 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6089#endif
6090 )
0d381245
VP
6091 {
6092 loc->shlib_disabled = 1;
6093 }
cae688ec
JJ
6094 }
6095}
6096
1e4d1764
YQ
6097/* Disable any breakpoints and tracepoints that are in an unloaded shared
6098 library. Only apply to enabled breakpoints, disabled ones can just stay
4a64f543 6099 disabled. */
84acb35a 6100
75149521 6101static void
84acb35a
JJ
6102disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6103{
876fa593 6104 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6105 int disabled_shlib_breaks = 0;
6106
c86cf029
VP
6107 /* SunOS a.out shared libraries are always mapped, so do not
6108 disable breakpoints; they will only be reported as unloaded
6109 through clear_solib when GDB discards its shared library
6110 list. See clear_solib for more information. */
6111 if (exec_bfd != NULL
6112 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6113 return;
6114
876fa593 6115 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6116 {
2bdf28a0 6117 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6118 struct breakpoint *b = loc->owner;
cc59ec59 6119
1e4d1764 6120 if (solib->pspace == loc->pspace
e2dd7057 6121 && !loc->shlib_disabled
1e4d1764
YQ
6122 && (((b->type == bp_breakpoint
6123 || b->type == bp_jit_event
6124 || b->type == bp_hardware_breakpoint)
6125 && (loc->loc_type == bp_loc_hardware_breakpoint
6126 || loc->loc_type == bp_loc_software_breakpoint))
6127 || is_tracepoint (b))
e2dd7057 6128 && solib_contains_address_p (solib, loc->address))
84acb35a 6129 {
e2dd7057
PP
6130 loc->shlib_disabled = 1;
6131 /* At this point, we cannot rely on remove_breakpoint
6132 succeeding so we must mark the breakpoint as not inserted
6133 to prevent future errors occurring in remove_breakpoints. */
6134 loc->inserted = 0;
8d3788bd
VP
6135
6136 /* This may cause duplicate notifications for the same breakpoint. */
6137 observer_notify_breakpoint_modified (b);
6138
e2dd7057
PP
6139 if (!disabled_shlib_breaks)
6140 {
6141 target_terminal_ours_for_output ();
3e43a32a
MS
6142 warning (_("Temporarily disabling breakpoints "
6143 "for unloaded shared library \"%s\""),
e2dd7057 6144 solib->so_name);
84acb35a 6145 }
e2dd7057 6146 disabled_shlib_breaks = 1;
84acb35a
JJ
6147 }
6148 }
84acb35a
JJ
6149}
6150
ce78b96d
JB
6151/* FORK & VFORK catchpoints. */
6152
e29a4733
PA
6153/* An instance of this type is used to represent a fork or vfork
6154 catchpoint. It includes a "struct breakpoint" as a kind of base
6155 class; users downcast to "struct breakpoint *" when needed. A
6156 breakpoint is really of this type iff its ops pointer points to
6157 CATCH_FORK_BREAKPOINT_OPS. */
6158
6159struct fork_catchpoint
6160{
6161 /* The base class. */
6162 struct breakpoint base;
6163
6164 /* Process id of a child process whose forking triggered this
6165 catchpoint. This field is only valid immediately after this
6166 catchpoint has triggered. */
6167 ptid_t forked_inferior_pid;
6168};
6169
4a64f543
MS
6170/* Implement the "insert" breakpoint_ops method for fork
6171 catchpoints. */
ce78b96d 6172
77b06cd7
TJB
6173static int
6174insert_catch_fork (struct bp_location *bl)
ce78b96d 6175{
77b06cd7 6176 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6177}
6178
4a64f543
MS
6179/* Implement the "remove" breakpoint_ops method for fork
6180 catchpoints. */
ce78b96d
JB
6181
6182static int
77b06cd7 6183remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6184{
6185 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6186}
6187
6188/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6189 catchpoints. */
6190
6191static int
f1310107
TJB
6192breakpoint_hit_catch_fork (const struct bp_location *bl,
6193 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6194{
e29a4733
PA
6195 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6196
6197 return inferior_has_forked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6198}
6199
4a64f543
MS
6200/* Implement the "print_it" breakpoint_ops method for fork
6201 catchpoints. */
ce78b96d
JB
6202
6203static enum print_stop_action
348d480f 6204print_it_catch_fork (bpstat bs)
ce78b96d 6205{
36dfb11c 6206 struct ui_out *uiout = current_uiout;
348d480f
PA
6207 struct breakpoint *b = bs->breakpoint_at;
6208 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 6209
ce78b96d 6210 annotate_catchpoint (b->number);
36dfb11c
TT
6211 if (b->disposition == disp_del)
6212 ui_out_text (uiout, "\nTemporary catchpoint ");
6213 else
6214 ui_out_text (uiout, "\nCatchpoint ");
6215 if (ui_out_is_mi_like_p (uiout))
6216 {
6217 ui_out_field_string (uiout, "reason",
6218 async_reason_lookup (EXEC_ASYNC_FORK));
6219 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6220 }
6221 ui_out_field_int (uiout, "bkptno", b->number);
6222 ui_out_text (uiout, " (forked process ");
6223 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6224 ui_out_text (uiout, "), ");
ce78b96d
JB
6225 return PRINT_SRC_AND_LOC;
6226}
6227
4a64f543
MS
6228/* Implement the "print_one" breakpoint_ops method for fork
6229 catchpoints. */
ce78b96d
JB
6230
6231static void
a6d9a66e 6232print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6233{
e29a4733 6234 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6235 struct value_print_options opts;
79a45e25 6236 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6237
6238 get_user_print_options (&opts);
6239
4a64f543
MS
6240 /* Field 4, the address, is omitted (which makes the columns not
6241 line up too nicely with the headers, but the effect is relatively
6242 readable). */
79a45b7d 6243 if (opts.addressprint)
ce78b96d
JB
6244 ui_out_field_skip (uiout, "addr");
6245 annotate_field (5);
6246 ui_out_text (uiout, "fork");
e29a4733 6247 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6248 {
6249 ui_out_text (uiout, ", process ");
6250 ui_out_field_int (uiout, "what",
e29a4733 6251 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6252 ui_out_spaces (uiout, 1);
6253 }
6254}
6255
6256/* Implement the "print_mention" breakpoint_ops method for fork
6257 catchpoints. */
6258
6259static void
6260print_mention_catch_fork (struct breakpoint *b)
6261{
6262 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6263}
6264
6149aea9
PA
6265/* Implement the "print_recreate" breakpoint_ops method for fork
6266 catchpoints. */
6267
6268static void
6269print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6270{
6271 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 6272 print_recreate_thread (b, fp);
6149aea9
PA
6273}
6274
ce78b96d
JB
6275/* The breakpoint_ops structure to be used in fork catchpoints. */
6276
2060206e 6277static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 6278
4a64f543
MS
6279/* Implement the "insert" breakpoint_ops method for vfork
6280 catchpoints. */
ce78b96d 6281
77b06cd7
TJB
6282static int
6283insert_catch_vfork (struct bp_location *bl)
ce78b96d 6284{
77b06cd7 6285 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6286}
6287
4a64f543
MS
6288/* Implement the "remove" breakpoint_ops method for vfork
6289 catchpoints. */
ce78b96d
JB
6290
6291static int
77b06cd7 6292remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6293{
6294 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6295}
6296
6297/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6298 catchpoints. */
6299
6300static int
f1310107
TJB
6301breakpoint_hit_catch_vfork (const struct bp_location *bl,
6302 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6303{
e29a4733
PA
6304 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6305
6306 return inferior_has_vforked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6307}
6308
4a64f543
MS
6309/* Implement the "print_it" breakpoint_ops method for vfork
6310 catchpoints. */
ce78b96d
JB
6311
6312static enum print_stop_action
348d480f 6313print_it_catch_vfork (bpstat bs)
ce78b96d 6314{
36dfb11c 6315 struct ui_out *uiout = current_uiout;
348d480f 6316 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
6317 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6318
ce78b96d 6319 annotate_catchpoint (b->number);
36dfb11c
TT
6320 if (b->disposition == disp_del)
6321 ui_out_text (uiout, "\nTemporary catchpoint ");
6322 else
6323 ui_out_text (uiout, "\nCatchpoint ");
6324 if (ui_out_is_mi_like_p (uiout))
6325 {
6326 ui_out_field_string (uiout, "reason",
6327 async_reason_lookup (EXEC_ASYNC_VFORK));
6328 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6329 }
6330 ui_out_field_int (uiout, "bkptno", b->number);
6331 ui_out_text (uiout, " (vforked process ");
6332 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6333 ui_out_text (uiout, "), ");
ce78b96d
JB
6334 return PRINT_SRC_AND_LOC;
6335}
6336
4a64f543
MS
6337/* Implement the "print_one" breakpoint_ops method for vfork
6338 catchpoints. */
ce78b96d
JB
6339
6340static void
a6d9a66e 6341print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6342{
e29a4733 6343 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6344 struct value_print_options opts;
79a45e25 6345 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6346
6347 get_user_print_options (&opts);
4a64f543
MS
6348 /* Field 4, the address, is omitted (which makes the columns not
6349 line up too nicely with the headers, but the effect is relatively
6350 readable). */
79a45b7d 6351 if (opts.addressprint)
ce78b96d
JB
6352 ui_out_field_skip (uiout, "addr");
6353 annotate_field (5);
6354 ui_out_text (uiout, "vfork");
e29a4733 6355 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6356 {
6357 ui_out_text (uiout, ", process ");
6358 ui_out_field_int (uiout, "what",
e29a4733 6359 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6360 ui_out_spaces (uiout, 1);
6361 }
6362}
6363
6364/* Implement the "print_mention" breakpoint_ops method for vfork
6365 catchpoints. */
6366
6367static void
6368print_mention_catch_vfork (struct breakpoint *b)
6369{
6370 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6371}
6372
6149aea9
PA
6373/* Implement the "print_recreate" breakpoint_ops method for vfork
6374 catchpoints. */
6375
6376static void
6377print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6378{
6379 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 6380 print_recreate_thread (b, fp);
6149aea9
PA
6381}
6382
ce78b96d
JB
6383/* The breakpoint_ops structure to be used in vfork catchpoints. */
6384
2060206e 6385static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 6386
be5c67c1
PA
6387/* An instance of this type is used to represent a syscall catchpoint.
6388 It includes a "struct breakpoint" as a kind of base class; users
6389 downcast to "struct breakpoint *" when needed. A breakpoint is
6390 really of this type iff its ops pointer points to
6391 CATCH_SYSCALL_BREAKPOINT_OPS. */
6392
6393struct syscall_catchpoint
6394{
6395 /* The base class. */
6396 struct breakpoint base;
6397
6398 /* Syscall numbers used for the 'catch syscall' feature. If no
6399 syscall has been specified for filtering, its value is NULL.
6400 Otherwise, it holds a list of all syscalls to be caught. The
6401 list elements are allocated with xmalloc. */
6402 VEC(int) *syscalls_to_be_caught;
6403};
6404
6405/* Implement the "dtor" breakpoint_ops method for syscall
6406 catchpoints. */
6407
6408static void
6409dtor_catch_syscall (struct breakpoint *b)
6410{
6411 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6412
6413 VEC_free (int, c->syscalls_to_be_caught);
348d480f 6414
2060206e 6415 base_breakpoint_ops.dtor (b);
be5c67c1
PA
6416}
6417
a96d9b2e
SDJ
6418/* Implement the "insert" breakpoint_ops method for syscall
6419 catchpoints. */
6420
77b06cd7
TJB
6421static int
6422insert_catch_syscall (struct bp_location *bl)
a96d9b2e 6423{
be5c67c1 6424 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6425 struct inferior *inf = current_inferior ();
6426
6427 ++inf->total_syscalls_count;
be5c67c1 6428 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6429 ++inf->any_syscall_count;
6430 else
6431 {
6432 int i, iter;
cc59ec59 6433
a96d9b2e 6434 for (i = 0;
be5c67c1 6435 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6436 i++)
6437 {
6438 int elem;
cc59ec59 6439
a96d9b2e
SDJ
6440 if (iter >= VEC_length (int, inf->syscalls_counts))
6441 {
6442 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6443 uintptr_t vec_addr_offset
6444 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6445 uintptr_t vec_addr;
6446 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6447 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6448 vec_addr_offset;
6449 memset ((void *) vec_addr, 0,
6450 (iter + 1 - old_size) * sizeof (int));
6451 }
6452 elem = VEC_index (int, inf->syscalls_counts, iter);
6453 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6454 }
6455 }
6456
77b06cd7
TJB
6457 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6458 inf->total_syscalls_count != 0,
6459 inf->any_syscall_count,
6460 VEC_length (int, inf->syscalls_counts),
6461 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6462}
6463
6464/* Implement the "remove" breakpoint_ops method for syscall
6465 catchpoints. */
6466
6467static int
77b06cd7 6468remove_catch_syscall (struct bp_location *bl)
a96d9b2e 6469{
be5c67c1 6470 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6471 struct inferior *inf = current_inferior ();
6472
6473 --inf->total_syscalls_count;
be5c67c1 6474 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6475 --inf->any_syscall_count;
6476 else
6477 {
6478 int i, iter;
cc59ec59 6479
a96d9b2e 6480 for (i = 0;
be5c67c1 6481 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6482 i++)
6483 {
6484 int elem;
6485 if (iter >= VEC_length (int, inf->syscalls_counts))
6486 /* Shouldn't happen. */
6487 continue;
6488 elem = VEC_index (int, inf->syscalls_counts, iter);
6489 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6490 }
6491 }
6492
6493 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6494 inf->total_syscalls_count != 0,
6495 inf->any_syscall_count,
6496 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6497 VEC_address (int,
6498 inf->syscalls_counts));
a96d9b2e
SDJ
6499}
6500
6501/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6502 catchpoints. */
6503
6504static int
f1310107
TJB
6505breakpoint_hit_catch_syscall (const struct bp_location *bl,
6506 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6507{
4a64f543
MS
6508 /* We must check if we are catching specific syscalls in this
6509 breakpoint. If we are, then we must guarantee that the called
6510 syscall is the same syscall we are catching. */
a96d9b2e 6511 int syscall_number = 0;
be5c67c1
PA
6512 const struct syscall_catchpoint *c
6513 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6514
6515 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6516 return 0;
6517
6518 /* Now, checking if the syscall is the same. */
be5c67c1 6519 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6520 {
6521 int i, iter;
cc59ec59 6522
a96d9b2e 6523 for (i = 0;
be5c67c1 6524 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6525 i++)
6526 if (syscall_number == iter)
6527 break;
6528 /* Not the same. */
6529 if (!iter)
6530 return 0;
6531 }
6532
6533 return 1;
6534}
6535
6536/* Implement the "print_it" breakpoint_ops method for syscall
6537 catchpoints. */
6538
6539static enum print_stop_action
348d480f 6540print_it_catch_syscall (bpstat bs)
a96d9b2e 6541{
36dfb11c 6542 struct ui_out *uiout = current_uiout;
348d480f 6543 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
6544 /* These are needed because we want to know in which state a
6545 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6546 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6547 must print "called syscall" or "returned from syscall". */
6548 ptid_t ptid;
6549 struct target_waitstatus last;
6550 struct syscall s;
a96d9b2e
SDJ
6551 char *syscall_id;
6552
6553 get_last_target_status (&ptid, &last);
6554
6555 get_syscall_by_number (last.value.syscall_number, &s);
6556
6557 annotate_catchpoint (b->number);
6558
36dfb11c
TT
6559 if (b->disposition == disp_del)
6560 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 6561 else
36dfb11c
TT
6562 ui_out_text (uiout, "\nCatchpoint ");
6563 if (ui_out_is_mi_like_p (uiout))
6564 {
6565 ui_out_field_string (uiout, "reason",
6566 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
6567 ? EXEC_ASYNC_SYSCALL_ENTRY
6568 : EXEC_ASYNC_SYSCALL_RETURN));
6569 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6570 }
6571 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
6572
6573 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
6574 ui_out_text (uiout, " (call to syscall ");
6575 else
6576 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 6577
36dfb11c
TT
6578 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
6579 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
6580 if (s.name != NULL)
6581 ui_out_field_string (uiout, "syscall-name", s.name);
6582
6583 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
6584
6585 return PRINT_SRC_AND_LOC;
6586}
6587
6588/* Implement the "print_one" breakpoint_ops method for syscall
6589 catchpoints. */
6590
6591static void
6592print_one_catch_syscall (struct breakpoint *b,
f1310107 6593 struct bp_location **last_loc)
a96d9b2e 6594{
be5c67c1 6595 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 6596 struct value_print_options opts;
79a45e25 6597 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
6598
6599 get_user_print_options (&opts);
4a64f543
MS
6600 /* Field 4, the address, is omitted (which makes the columns not
6601 line up too nicely with the headers, but the effect is relatively
6602 readable). */
a96d9b2e
SDJ
6603 if (opts.addressprint)
6604 ui_out_field_skip (uiout, "addr");
6605 annotate_field (5);
6606
be5c67c1
PA
6607 if (c->syscalls_to_be_caught
6608 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6609 ui_out_text (uiout, "syscalls \"");
6610 else
6611 ui_out_text (uiout, "syscall \"");
6612
be5c67c1 6613 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6614 {
6615 int i, iter;
6616 char *text = xstrprintf ("%s", "");
cc59ec59 6617
a96d9b2e 6618 for (i = 0;
be5c67c1 6619 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6620 i++)
6621 {
6622 char *x = text;
6623 struct syscall s;
6624 get_syscall_by_number (iter, &s);
6625
6626 if (s.name != NULL)
6627 text = xstrprintf ("%s%s, ", text, s.name);
6628 else
6629 text = xstrprintf ("%s%d, ", text, iter);
6630
6631 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 6632 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
6633 on every call. */
6634 xfree (x);
6635 }
6636 /* Remove the last comma. */
6637 text[strlen (text) - 2] = '\0';
6638 ui_out_field_string (uiout, "what", text);
6639 }
6640 else
6641 ui_out_field_string (uiout, "what", "<any syscall>");
6642 ui_out_text (uiout, "\" ");
6643}
6644
6645/* Implement the "print_mention" breakpoint_ops method for syscall
6646 catchpoints. */
6647
6648static void
6649print_mention_catch_syscall (struct breakpoint *b)
6650{
be5c67c1
PA
6651 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6652
6653 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6654 {
6655 int i, iter;
6656
be5c67c1 6657 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6658 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6659 else
6660 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6661
6662 for (i = 0;
be5c67c1 6663 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6664 i++)
6665 {
6666 struct syscall s;
6667 get_syscall_by_number (iter, &s);
6668
6669 if (s.name)
6670 printf_filtered (" '%s' [%d]", s.name, s.number);
6671 else
6672 printf_filtered (" %d", s.number);
6673 }
6674 printf_filtered (")");
6675 }
6676 else
6677 printf_filtered (_("Catchpoint %d (any syscall)"),
6678 b->number);
6679}
6680
6149aea9
PA
6681/* Implement the "print_recreate" breakpoint_ops method for syscall
6682 catchpoints. */
6683
6684static void
6685print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6686{
be5c67c1
PA
6687 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6688
6149aea9
PA
6689 fprintf_unfiltered (fp, "catch syscall");
6690
be5c67c1 6691 if (c->syscalls_to_be_caught)
6149aea9
PA
6692 {
6693 int i, iter;
6694
6695 for (i = 0;
be5c67c1 6696 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
6697 i++)
6698 {
6699 struct syscall s;
6700
6701 get_syscall_by_number (iter, &s);
6702 if (s.name)
6703 fprintf_unfiltered (fp, " %s", s.name);
6704 else
6705 fprintf_unfiltered (fp, " %d", s.number);
6706 }
6707 }
d9b3f62e 6708 print_recreate_thread (b, fp);
6149aea9
PA
6709}
6710
a96d9b2e
SDJ
6711/* The breakpoint_ops structure to be used in syscall catchpoints. */
6712
2060206e 6713static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
6714
6715/* Returns non-zero if 'b' is a syscall catchpoint. */
6716
6717static int
6718syscall_catchpoint_p (struct breakpoint *b)
6719{
6720 return (b->ops == &catch_syscall_breakpoint_ops);
6721}
6722
346774a9
PA
6723/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
6724 is non-zero, then make the breakpoint temporary. If COND_STRING is
6725 not NULL, then store it in the breakpoint. OPS, if not NULL, is
6726 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 6727
346774a9
PA
6728static void
6729init_catchpoint (struct breakpoint *b,
6730 struct gdbarch *gdbarch, int tempflag,
6731 char *cond_string,
c0a91b2b 6732 const struct breakpoint_ops *ops)
c906108c 6733{
c5aa993b 6734 struct symtab_and_line sal;
346774a9 6735
fe39c653 6736 init_sal (&sal);
6c95b8df 6737 sal.pspace = current_program_space;
c5aa993b 6738
28010a5d 6739 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 6740
1b36a34b 6741 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 6742 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
6743}
6744
28010a5d 6745void
3ea46bff 6746install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
6747{
6748 add_to_breakpoint_chain (b);
3a5c3e22
PA
6749 set_breakpoint_number (internal, b);
6750 if (!internal)
6751 mention (b);
c56053d2 6752 observer_notify_breakpoint_created (b);
3ea46bff
YQ
6753
6754 if (update_gll)
6755 update_global_location_list (1);
c56053d2
PA
6756}
6757
9b70b993 6758static void
a6d9a66e
UW
6759create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6760 int tempflag, char *cond_string,
c0a91b2b 6761 const struct breakpoint_ops *ops)
c906108c 6762{
e29a4733 6763 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 6764
e29a4733
PA
6765 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
6766
6767 c->forked_inferior_pid = null_ptid;
6768
3ea46bff 6769 install_breakpoint (0, &c->base, 1);
c906108c
SS
6770}
6771
fe798b75
JB
6772/* Exec catchpoints. */
6773
b4d90040
PA
6774/* An instance of this type is used to represent an exec catchpoint.
6775 It includes a "struct breakpoint" as a kind of base class; users
6776 downcast to "struct breakpoint *" when needed. A breakpoint is
6777 really of this type iff its ops pointer points to
6778 CATCH_EXEC_BREAKPOINT_OPS. */
6779
6780struct exec_catchpoint
6781{
6782 /* The base class. */
6783 struct breakpoint base;
6784
6785 /* Filename of a program whose exec triggered this catchpoint.
6786 This field is only valid immediately after this catchpoint has
6787 triggered. */
6788 char *exec_pathname;
6789};
6790
6791/* Implement the "dtor" breakpoint_ops method for exec
6792 catchpoints. */
6793
6794static void
6795dtor_catch_exec (struct breakpoint *b)
6796{
6797 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6798
6799 xfree (c->exec_pathname);
348d480f 6800
2060206e 6801 base_breakpoint_ops.dtor (b);
b4d90040
PA
6802}
6803
77b06cd7
TJB
6804static int
6805insert_catch_exec (struct bp_location *bl)
c906108c 6806{
77b06cd7 6807 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6808}
c906108c 6809
fe798b75 6810static int
77b06cd7 6811remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6812{
6813 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6814}
c906108c 6815
fe798b75 6816static int
f1310107
TJB
6817breakpoint_hit_catch_exec (const struct bp_location *bl,
6818 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6819{
b4d90040
PA
6820 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
6821
6822 return inferior_has_execd (inferior_ptid, &c->exec_pathname);
fe798b75 6823}
c906108c 6824
fe798b75 6825static enum print_stop_action
348d480f 6826print_it_catch_exec (bpstat bs)
fe798b75 6827{
36dfb11c 6828 struct ui_out *uiout = current_uiout;
348d480f 6829 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
6830 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6831
fe798b75 6832 annotate_catchpoint (b->number);
36dfb11c
TT
6833 if (b->disposition == disp_del)
6834 ui_out_text (uiout, "\nTemporary catchpoint ");
6835 else
6836 ui_out_text (uiout, "\nCatchpoint ");
6837 if (ui_out_is_mi_like_p (uiout))
6838 {
6839 ui_out_field_string (uiout, "reason",
6840 async_reason_lookup (EXEC_ASYNC_EXEC));
6841 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6842 }
6843 ui_out_field_int (uiout, "bkptno", b->number);
6844 ui_out_text (uiout, " (exec'd ");
6845 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
6846 ui_out_text (uiout, "), ");
6847
fe798b75 6848 return PRINT_SRC_AND_LOC;
c906108c
SS
6849}
6850
fe798b75 6851static void
a6d9a66e 6852print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 6853{
b4d90040 6854 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 6855 struct value_print_options opts;
79a45e25 6856 struct ui_out *uiout = current_uiout;
fe798b75
JB
6857
6858 get_user_print_options (&opts);
6859
6860 /* Field 4, the address, is omitted (which makes the columns
6861 not line up too nicely with the headers, but the effect
6862 is relatively readable). */
6863 if (opts.addressprint)
6864 ui_out_field_skip (uiout, "addr");
6865 annotate_field (5);
6866 ui_out_text (uiout, "exec");
b4d90040 6867 if (c->exec_pathname != NULL)
fe798b75
JB
6868 {
6869 ui_out_text (uiout, ", program \"");
b4d90040 6870 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
6871 ui_out_text (uiout, "\" ");
6872 }
6873}
6874
6875static void
6876print_mention_catch_exec (struct breakpoint *b)
6877{
6878 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6879}
6880
6149aea9
PA
6881/* Implement the "print_recreate" breakpoint_ops method for exec
6882 catchpoints. */
6883
6884static void
6885print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6886{
6887 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 6888 print_recreate_thread (b, fp);
6149aea9
PA
6889}
6890
2060206e 6891static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 6892
a96d9b2e
SDJ
6893static void
6894create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 6895 const struct breakpoint_ops *ops)
a96d9b2e 6896{
be5c67c1 6897 struct syscall_catchpoint *c;
a96d9b2e 6898 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 6899
be5c67c1
PA
6900 c = XNEW (struct syscall_catchpoint);
6901 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
6902 c->syscalls_to_be_caught = filter;
a96d9b2e 6903
3ea46bff 6904 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
6905}
6906
c906108c 6907static int
fba45db2 6908hw_breakpoint_used_count (void)
c906108c 6909{
c906108c 6910 int i = 0;
f1310107
TJB
6911 struct breakpoint *b;
6912 struct bp_location *bl;
c906108c
SS
6913
6914 ALL_BREAKPOINTS (b)
c5aa993b 6915 {
d6b74ac4 6916 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
6917 for (bl = b->loc; bl; bl = bl->next)
6918 {
6919 /* Special types of hardware breakpoints may use more than
6920 one register. */
348d480f 6921 i += b->ops->resources_needed (bl);
f1310107 6922 }
c5aa993b 6923 }
c906108c
SS
6924
6925 return i;
6926}
6927
a1398e0c
PA
6928/* Returns the resources B would use if it were a hardware
6929 watchpoint. */
6930
c906108c 6931static int
a1398e0c 6932hw_watchpoint_use_count (struct breakpoint *b)
c906108c 6933{
c906108c 6934 int i = 0;
e09342b5 6935 struct bp_location *bl;
c906108c 6936
a1398e0c
PA
6937 if (!breakpoint_enabled (b))
6938 return 0;
6939
6940 for (bl = b->loc; bl; bl = bl->next)
6941 {
6942 /* Special types of hardware watchpoints may use more than
6943 one register. */
6944 i += b->ops->resources_needed (bl);
6945 }
6946
6947 return i;
6948}
6949
6950/* Returns the sum the used resources of all hardware watchpoints of
6951 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
6952 the sum of the used resources of all hardware watchpoints of other
6953 types _not_ TYPE. */
6954
6955static int
6956hw_watchpoint_used_count_others (struct breakpoint *except,
6957 enum bptype type, int *other_type_used)
6958{
6959 int i = 0;
6960 struct breakpoint *b;
6961
c906108c
SS
6962 *other_type_used = 0;
6963 ALL_BREAKPOINTS (b)
e09342b5 6964 {
a1398e0c
PA
6965 if (b == except)
6966 continue;
e09342b5
TJB
6967 if (!breakpoint_enabled (b))
6968 continue;
6969
a1398e0c
PA
6970 if (b->type == type)
6971 i += hw_watchpoint_use_count (b);
6972 else if (is_hardware_watchpoint (b))
6973 *other_type_used = 1;
e09342b5
TJB
6974 }
6975
c906108c
SS
6976 return i;
6977}
6978
c906108c 6979void
fba45db2 6980disable_watchpoints_before_interactive_call_start (void)
c906108c 6981{
c5aa993b 6982 struct breakpoint *b;
c906108c
SS
6983
6984 ALL_BREAKPOINTS (b)
c5aa993b 6985 {
cc60f2e3 6986 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6987 {
b5de0fa7 6988 b->enable_state = bp_call_disabled;
b60e7edf 6989 update_global_location_list (0);
c5aa993b
JM
6990 }
6991 }
c906108c
SS
6992}
6993
6994void
fba45db2 6995enable_watchpoints_after_interactive_call_stop (void)
c906108c 6996{
c5aa993b 6997 struct breakpoint *b;
c906108c
SS
6998
6999 ALL_BREAKPOINTS (b)
c5aa993b 7000 {
cc60f2e3 7001 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 7002 {
b5de0fa7 7003 b->enable_state = bp_enabled;
b60e7edf 7004 update_global_location_list (1);
c5aa993b
JM
7005 }
7006 }
c906108c
SS
7007}
7008
8bea4e01
UW
7009void
7010disable_breakpoints_before_startup (void)
7011{
6c95b8df 7012 current_program_space->executing_startup = 1;
f8eba3c6 7013 update_global_location_list (0);
8bea4e01
UW
7014}
7015
7016void
7017enable_breakpoints_after_startup (void)
7018{
6c95b8df 7019 current_program_space->executing_startup = 0;
f8eba3c6 7020 breakpoint_re_set ();
8bea4e01
UW
7021}
7022
c906108c
SS
7023
7024/* Set a breakpoint that will evaporate an end of command
7025 at address specified by SAL.
7026 Restrict it to frame FRAME if FRAME is nonzero. */
7027
7028struct breakpoint *
a6d9a66e
UW
7029set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7030 struct frame_id frame_id, enum bptype type)
c906108c 7031{
52f0bd74 7032 struct breakpoint *b;
edb3359d
DJ
7033
7034 /* If FRAME_ID is valid, it should be a real frame, not an inlined
7035 one. */
7036 gdb_assert (!frame_id_inlined_p (frame_id));
7037
06edf0c0 7038 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
7039 b->enable_state = bp_enabled;
7040 b->disposition = disp_donttouch;
818dd999 7041 b->frame_id = frame_id;
c906108c 7042
4a64f543
MS
7043 /* If we're debugging a multi-threaded program, then we want
7044 momentary breakpoints to be active in only a single thread of
7045 control. */
39f77062
KB
7046 if (in_thread_list (inferior_ptid))
7047 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 7048
b60e7edf 7049 update_global_location_list_nothrow (1);
74960c60 7050
c906108c
SS
7051 return b;
7052}
611c83ae 7053
06edf0c0
PA
7054/* Make a momentary breakpoint based on the master breakpoint ORIG.
7055 The new breakpoint will have type TYPE, and use OPS as it
7056 breakpoint_ops. */
e58b0e63 7057
06edf0c0
PA
7058static struct breakpoint *
7059momentary_breakpoint_from_master (struct breakpoint *orig,
7060 enum bptype type,
c0a91b2b 7061 const struct breakpoint_ops *ops)
e58b0e63
PA
7062{
7063 struct breakpoint *copy;
7064
06edf0c0 7065 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 7066 copy->loc = allocate_bp_location (copy);
0e30163f 7067 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 7068
a6d9a66e 7069 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7070 copy->loc->requested_address = orig->loc->requested_address;
7071 copy->loc->address = orig->loc->address;
7072 copy->loc->section = orig->loc->section;
6c95b8df 7073 copy->loc->pspace = orig->loc->pspace;
e58b0e63 7074
f8eba3c6
TT
7075 if (orig->loc->source_file != NULL)
7076 copy->loc->source_file = xstrdup (orig->loc->source_file);
e58b0e63 7077
f8eba3c6 7078 copy->loc->line_number = orig->loc->line_number;
e58b0e63
PA
7079 copy->frame_id = orig->frame_id;
7080 copy->thread = orig->thread;
6c95b8df 7081 copy->pspace = orig->pspace;
e58b0e63
PA
7082
7083 copy->enable_state = bp_enabled;
7084 copy->disposition = disp_donttouch;
7085 copy->number = internal_breakpoint_number--;
7086
7087 update_global_location_list_nothrow (0);
7088 return copy;
7089}
7090
06edf0c0
PA
7091/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7092 ORIG is NULL. */
7093
7094struct breakpoint *
7095clone_momentary_breakpoint (struct breakpoint *orig)
7096{
7097 /* If there's nothing to clone, then return nothing. */
7098 if (orig == NULL)
7099 return NULL;
7100
7101 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
7102}
7103
611c83ae 7104struct breakpoint *
a6d9a66e
UW
7105set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7106 enum bptype type)
611c83ae
PA
7107{
7108 struct symtab_and_line sal;
7109
7110 sal = find_pc_line (pc, 0);
7111 sal.pc = pc;
7112 sal.section = find_pc_overlay (pc);
7113 sal.explicit_pc = 1;
7114
a6d9a66e 7115 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7116}
c906108c 7117\f
c5aa993b 7118
c906108c
SS
7119/* Tell the user we have just set a breakpoint B. */
7120
7121static void
fba45db2 7122mention (struct breakpoint *b)
c906108c 7123{
348d480f 7124 b->ops->print_mention (b);
79a45e25 7125 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 7126 return;
c906108c
SS
7127 printf_filtered ("\n");
7128}
c906108c 7129\f
c5aa993b 7130
0d381245 7131static struct bp_location *
39d61571 7132add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7133 const struct symtab_and_line *sal)
7134{
7135 struct bp_location *loc, **tmp;
3742cc8b
YQ
7136 CORE_ADDR adjusted_address;
7137 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
7138
7139 if (loc_gdbarch == NULL)
7140 loc_gdbarch = b->gdbarch;
7141
7142 /* Adjust the breakpoint's address prior to allocating a location.
7143 Once we call allocate_bp_location(), that mostly uninitialized
7144 location will be placed on the location chain. Adjustment of the
7145 breakpoint may cause target_read_memory() to be called and we do
7146 not want its scan of the location chain to find a breakpoint and
7147 location that's only been partially initialized. */
7148 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
7149 sal->pc, b->type);
0d381245 7150
39d61571 7151 loc = allocate_bp_location (b);
0d381245
VP
7152 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7153 ;
7154 *tmp = loc;
3742cc8b 7155
0d381245 7156 loc->requested_address = sal->pc;
3742cc8b 7157 loc->address = adjusted_address;
6c95b8df
PA
7158 loc->pspace = sal->pspace;
7159 gdb_assert (loc->pspace != NULL);
0d381245 7160 loc->section = sal->section;
3742cc8b 7161 loc->gdbarch = loc_gdbarch;
f8eba3c6
TT
7162
7163 if (sal->symtab != NULL)
7164 loc->source_file = xstrdup (sal->symtab->filename);
7165 loc->line_number = sal->line;
7166
0e30163f
JK
7167 set_breakpoint_location_function (loc,
7168 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7169 return loc;
7170}
514f746b
AR
7171\f
7172
7173/* Return 1 if LOC is pointing to a permanent breakpoint,
7174 return 0 otherwise. */
7175
7176static int
7177bp_loc_is_permanent (struct bp_location *loc)
7178{
7179 int len;
7180 CORE_ADDR addr;
7181 const gdb_byte *brk;
7182 gdb_byte *target_mem;
939c61fa
JK
7183 struct cleanup *cleanup;
7184 int retval = 0;
514f746b
AR
7185
7186 gdb_assert (loc != NULL);
7187
7188 addr = loc->address;
a6d9a66e 7189 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7190
939c61fa
JK
7191 /* Software breakpoints unsupported? */
7192 if (brk == NULL)
7193 return 0;
7194
514f746b
AR
7195 target_mem = alloca (len);
7196
939c61fa
JK
7197 /* Enable the automatic memory restoration from breakpoints while
7198 we read the memory. Otherwise we could say about our temporary
7199 breakpoints they are permanent. */
6c95b8df
PA
7200 cleanup = save_current_space_and_thread ();
7201
7202 switch_to_program_space_and_thread (loc->pspace);
7203 make_show_memory_breakpoints_cleanup (0);
939c61fa 7204
514f746b
AR
7205 if (target_read_memory (loc->address, target_mem, len) == 0
7206 && memcmp (target_mem, brk, len) == 0)
939c61fa 7207 retval = 1;
514f746b 7208
939c61fa
JK
7209 do_cleanups (cleanup);
7210
7211 return retval;
514f746b
AR
7212}
7213
7214
c3f6f71d 7215
018d34a4
VP
7216/* Create a breakpoint with SAL as location. Use ADDR_STRING
7217 as textual description of the location, and COND_STRING
db107f19 7218 as condition expression. */
018d34a4
VP
7219
7220static void
d9b3f62e
PA
7221init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
7222 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 7223 char *filter, char *cond_string,
d9b3f62e
PA
7224 enum bptype type, enum bpdisp disposition,
7225 int thread, int task, int ignore_count,
c0a91b2b 7226 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e 7227 int enabled, int internal, int display_canonical)
018d34a4 7228{
0d381245 7229 int i;
018d34a4
VP
7230
7231 if (type == bp_hardware_breakpoint)
7232 {
fbbd034e
AS
7233 int target_resources_ok;
7234
7235 i = hw_breakpoint_used_count ();
7236 target_resources_ok =
7237 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7238 i + 1, 0);
7239 if (target_resources_ok == 0)
7240 error (_("No hardware breakpoint support in the target."));
7241 else if (target_resources_ok < 0)
7242 error (_("Hardware breakpoints used exceeds limit."));
7243 }
7244
6c95b8df
PA
7245 gdb_assert (sals.nelts > 0);
7246
0d381245
VP
7247 for (i = 0; i < sals.nelts; ++i)
7248 {
7249 struct symtab_and_line sal = sals.sals[i];
7250 struct bp_location *loc;
7251
7252 if (from_tty)
5af949e3
UW
7253 {
7254 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7255 if (!loc_gdbarch)
7256 loc_gdbarch = gdbarch;
7257
7258 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7259 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7260 }
0d381245
VP
7261
7262 if (i == 0)
7263 {
d9b3f62e 7264 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 7265 b->thread = thread;
4a306c9a 7266 b->task = task;
018d34a4 7267
0d381245
VP
7268 b->cond_string = cond_string;
7269 b->ignore_count = ignore_count;
41447f92 7270 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7271 b->disposition = disposition;
6c95b8df 7272
0fb4aa4b
PA
7273 if (type == bp_static_tracepoint)
7274 {
d9b3f62e 7275 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
7276 struct static_tracepoint_marker marker;
7277
7278 if (is_marker_spec (addr_string))
7279 {
7280 /* We already know the marker exists, otherwise, we
7281 wouldn't see a sal for it. */
7282 char *p = &addr_string[3];
7283 char *endp;
7284 char *marker_str;
0fb4aa4b 7285
e9cafbcc 7286 p = skip_spaces (p);
0fb4aa4b 7287
e9cafbcc 7288 endp = skip_to_space (p);
0fb4aa4b
PA
7289
7290 marker_str = savestring (p, endp - p);
d9b3f62e 7291 t->static_trace_marker_id = marker_str;
0fb4aa4b 7292
3e43a32a
MS
7293 printf_filtered (_("Probed static tracepoint "
7294 "marker \"%s\"\n"),
d9b3f62e 7295 t->static_trace_marker_id);
0fb4aa4b
PA
7296 }
7297 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7298 {
d9b3f62e 7299 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
7300 release_static_tracepoint_marker (&marker);
7301
3e43a32a
MS
7302 printf_filtered (_("Probed static tracepoint "
7303 "marker \"%s\"\n"),
d9b3f62e 7304 t->static_trace_marker_id);
0fb4aa4b
PA
7305 }
7306 else
3e43a32a
MS
7307 warning (_("Couldn't determine the static "
7308 "tracepoint marker to probe"));
0fb4aa4b
PA
7309 }
7310
0d381245
VP
7311 loc = b->loc;
7312 }
7313 else
018d34a4 7314 {
39d61571 7315 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7316 }
7317
514f746b
AR
7318 if (bp_loc_is_permanent (loc))
7319 make_breakpoint_permanent (b);
7320
0d381245
VP
7321 if (b->cond_string)
7322 {
7323 char *arg = b->cond_string;
d32a6982 7324 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7325 if (*arg)
db107f19 7326 error (_("Garbage %s follows condition"), arg);
018d34a4 7327 }
0d381245 7328 }
018d34a4 7329
56435ebe 7330 b->display_canonical = display_canonical;
018d34a4
VP
7331 if (addr_string)
7332 b->addr_string = addr_string;
7333 else
7334 /* addr_string has to be used or breakpoint_re_set will delete
7335 me. */
5af949e3
UW
7336 b->addr_string
7337 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 7338 b->filter = filter;
d9b3f62e 7339}
018d34a4 7340
d9b3f62e
PA
7341static void
7342create_breakpoint_sal (struct gdbarch *gdbarch,
7343 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 7344 char *filter, char *cond_string,
d9b3f62e
PA
7345 enum bptype type, enum bpdisp disposition,
7346 int thread, int task, int ignore_count,
c0a91b2b 7347 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e
PA
7348 int enabled, int internal, int display_canonical)
7349{
7350 struct breakpoint *b;
7351 struct cleanup *old_chain;
7352
7353 if (is_tracepoint_type (type))
7354 {
7355 struct tracepoint *t;
7356
7357 t = XCNEW (struct tracepoint);
7358 b = &t->base;
7359 }
7360 else
7361 b = XNEW (struct breakpoint);
7362
7363 old_chain = make_cleanup (xfree, b);
7364
7365 init_breakpoint_sal (b, gdbarch,
7366 sals, addr_string,
f8eba3c6 7367 filter, cond_string,
d9b3f62e
PA
7368 type, disposition,
7369 thread, task, ignore_count,
7370 ops, from_tty,
7371 enabled, internal, display_canonical);
7372 discard_cleanups (old_chain);
7373
3ea46bff 7374 install_breakpoint (internal, b, 0);
018d34a4
VP
7375}
7376
7377/* Add SALS.nelts breakpoints to the breakpoint table. For each
7378 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7379 value. COND_STRING, if not NULL, specified the condition to be
7380 used for all breakpoints. Essentially the only case where
7381 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7382 function. In that case, it's still not possible to specify
7383 separate conditions for different overloaded functions, so
7384 we take just a single condition string.
7385
c3f6f71d 7386 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7387 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7388 array contents). If the function fails (error() is called), the
7389 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7390 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7391
7392static void
8cdf0e15 7393create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 7394 struct linespec_result *canonical,
8cdf0e15
VP
7395 char *cond_string,
7396 enum bptype type, enum bpdisp disposition,
7397 int thread, int task, int ignore_count,
c0a91b2b 7398 const struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7399 int enabled, int internal)
c906108c 7400{
018d34a4 7401 int i;
f8eba3c6 7402 struct linespec_sals *lsal;
cc59ec59 7403
f8eba3c6
TT
7404 if (canonical->pre_expanded)
7405 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
7406
7407 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 7408 {
f8eba3c6
TT
7409 /* Note that 'addr_string' can be NULL in the case of a plain
7410 'break', without arguments. */
7411 char *addr_string = (canonical->addr_string
7412 ? xstrdup (canonical->addr_string)
7413 : NULL);
7414 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
7415 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 7416
f8eba3c6
TT
7417 make_cleanup (xfree, filter_string);
7418 create_breakpoint_sal (gdbarch, lsal->sals,
7419 addr_string,
7420 filter_string,
8cdf0e15 7421 cond_string, type, disposition,
84f4c1fe 7422 thread, task, ignore_count, ops,
56435ebe
TT
7423 from_tty, enabled, internal,
7424 canonical->special_display);
f8eba3c6 7425 discard_cleanups (inner);
c3f6f71d 7426 }
c3f6f71d 7427}
c906108c 7428
9998af43 7429/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7430 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7431 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7432 address strings. ADDRESS points to the end of the SAL.
7433
7434 The array and the line spec strings are allocated on the heap, it is
7435 the caller's responsibility to free them. */
c906108c 7436
b9362cc7 7437static void
c3f6f71d 7438parse_breakpoint_sals (char **address,
58438ac1 7439 struct linespec_result *canonical)
c3f6f71d
JM
7440{
7441 char *addr_start = *address;
cc59ec59 7442
c3f6f71d 7443 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7444 breakpoint. */
c3f6f71d
JM
7445 if ((*address) == NULL
7446 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 7447 {
1bfeeb0f
JL
7448 /* The last displayed codepoint, if it's valid, is our default breakpoint
7449 address. */
7450 if (last_displayed_sal_is_valid ())
c906108c 7451 {
f8eba3c6 7452 struct linespec_sals lsal;
c3f6f71d 7453 struct symtab_and_line sal;
cc59ec59 7454
4a64f543 7455 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 7456 lsal.sals.sals = (struct symtab_and_line *)
c906108c 7457 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
7458
7459 /* Set sal's pspace, pc, symtab, and line to the values
7460 corresponding to the last call to print_frame_info. */
7461 get_last_displayed_sal (&sal);
7462 sal.section = find_pc_overlay (sal.pc);
00903456 7463
4a64f543 7464 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
7465 where PC is the last displayed codepoint's address. So
7466 make sure to set sal.explicit_pc to prevent GDB from
7467 trying to expand the list of sals to include all other
7468 instances with the same symtab and line. */
00903456
JK
7469 sal.explicit_pc = 1;
7470
f8eba3c6
TT
7471 lsal.sals.sals[0] = sal;
7472 lsal.sals.nelts = 1;
7473 lsal.canonical = NULL;
7474
7475 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
7476 }
7477 else
8a3fe4f8 7478 error (_("No default breakpoint address now."));
c906108c
SS
7479 }
7480 else
7481 {
c906108c 7482 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7483 current_source_symtab (which is decode_line_1's default).
7484 This should produce the results we want almost all of the
f8eba3c6
TT
7485 time while leaving default_breakpoint_* alone. */
7486 if (last_displayed_sal_is_valid ())
7487 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
7488 get_last_displayed_symtab (),
7489 get_last_displayed_line (),
7490 canonical, NULL, NULL);
c906108c 7491 else
f8eba3c6
TT
7492 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
7493 (struct symtab *) NULL, 0,
7494 canonical, NULL, NULL);
c906108c 7495 }
c3f6f71d 7496}
c906108c 7497
c906108c 7498
c3f6f71d 7499/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7500 inserted as a breakpoint. If it can't throw an error. */
c906108c 7501
b9362cc7 7502static void
23e7acfb 7503breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7504{
7505 int i;
cc59ec59 7506
c3f6f71d 7507 for (i = 0; i < sals->nelts; i++)
ee53e872 7508 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7509}
7510
7a697b8d
SS
7511/* Fast tracepoints may have restrictions on valid locations. For
7512 instance, a fast tracepoint using a jump instead of a trap will
7513 likely have to overwrite more bytes than a trap would, and so can
7514 only be placed where the instruction is longer than the jump, or a
7515 multi-instruction sequence does not have a jump into the middle of
7516 it, etc. */
7517
7518static void
7519check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7520 struct symtabs_and_lines *sals)
7521{
7522 int i, rslt;
7523 struct symtab_and_line *sal;
7524 char *msg;
7525 struct cleanup *old_chain;
7526
7527 for (i = 0; i < sals->nelts; i++)
7528 {
f8eba3c6
TT
7529 struct gdbarch *sarch;
7530
7a697b8d
SS
7531 sal = &sals->sals[i];
7532
f8eba3c6
TT
7533 sarch = get_sal_arch (*sal);
7534 /* We fall back to GDBARCH if there is no architecture
7535 associated with SAL. */
7536 if (sarch == NULL)
7537 sarch = gdbarch;
7538 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
7539 NULL, &msg);
7540 old_chain = make_cleanup (xfree, msg);
7541
7542 if (!rslt)
7543 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 7544 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
7545
7546 do_cleanups (old_chain);
7547 }
7548}
7549
018d34a4
VP
7550/* Given TOK, a string specification of condition and thread, as
7551 accepted by the 'break' command, extract the condition
7552 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7553 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7554 If no condition is found, *COND_STRING is set to NULL.
7555 If no thread is found, *THREAD is set to -1. */
7556static void
7557find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7558 char **cond_string, int *thread, int *task)
018d34a4
VP
7559{
7560 *cond_string = NULL;
7561 *thread = -1;
7562 while (tok && *tok)
7563 {
7564 char *end_tok;
7565 int toklen;
7566 char *cond_start = NULL;
7567 char *cond_end = NULL;
cc59ec59 7568
e9cafbcc 7569 tok = skip_spaces (tok);
018d34a4 7570
e9cafbcc 7571 end_tok = skip_to_space (tok);
018d34a4
VP
7572
7573 toklen = end_tok - tok;
7574
7575 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7576 {
f7545552
TT
7577 struct expression *expr;
7578
018d34a4 7579 tok = cond_start = end_tok + 1;
f7545552
TT
7580 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7581 xfree (expr);
018d34a4
VP
7582 cond_end = tok;
7583 *cond_string = savestring (cond_start,
7584 cond_end - cond_start);
7585 }
7586 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7587 {
7588 char *tmptok;
7589
7590 tok = end_tok + 1;
7591 tmptok = tok;
7592 *thread = strtol (tok, &tok, 0);
7593 if (tok == tmptok)
7594 error (_("Junk after thread keyword."));
7595 if (!valid_thread_id (*thread))
7596 error (_("Unknown thread %d."), *thread);
7597 }
4a306c9a
JB
7598 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7599 {
7600 char *tmptok;
7601
7602 tok = end_tok + 1;
7603 tmptok = tok;
7604 *task = strtol (tok, &tok, 0);
7605 if (tok == tmptok)
7606 error (_("Junk after task keyword."));
7607 if (!valid_task_id (*task))
b6199126 7608 error (_("Unknown task %d."), *task);
4a306c9a 7609 }
018d34a4
VP
7610 else
7611 error (_("Junk at end of arguments."));
7612 }
7613}
7614
0fb4aa4b
PA
7615/* Decode a static tracepoint marker spec. */
7616
7617static struct symtabs_and_lines
7618decode_static_tracepoint_spec (char **arg_p)
7619{
7620 VEC(static_tracepoint_marker_p) *markers = NULL;
7621 struct symtabs_and_lines sals;
7622 struct symtab_and_line sal;
7623 struct symbol *sym;
7624 struct cleanup *old_chain;
7625 char *p = &(*arg_p)[3];
7626 char *endp;
7627 char *marker_str;
7628 int i;
7629
e9cafbcc 7630 p = skip_spaces (p);
0fb4aa4b 7631
e9cafbcc 7632 endp = skip_to_space (p);
0fb4aa4b
PA
7633
7634 marker_str = savestring (p, endp - p);
7635 old_chain = make_cleanup (xfree, marker_str);
7636
7637 markers = target_static_tracepoint_markers_by_strid (marker_str);
7638 if (VEC_empty(static_tracepoint_marker_p, markers))
7639 error (_("No known static tracepoint marker named %s"), marker_str);
7640
7641 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7642 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7643
7644 for (i = 0; i < sals.nelts; i++)
7645 {
7646 struct static_tracepoint_marker *marker;
7647
7648 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7649
7650 init_sal (&sals.sals[i]);
7651
7652 sals.sals[i] = find_pc_line (marker->address, 0);
7653 sals.sals[i].pc = marker->address;
7654
7655 release_static_tracepoint_marker (marker);
7656 }
7657
7658 do_cleanups (old_chain);
7659
7660 *arg_p = endp;
7661 return sals;
7662}
7663
fd9b8c24
PA
7664/* Set a breakpoint. This function is shared between CLI and MI
7665 functions for setting a breakpoint. This function has two major
7666 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7667 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7668 breakpoint location, address and thread. Otherwise, ARG is just
7669 the location of breakpoint, with condition and thread specified by
7670 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7671 the breakpoint number will be allocated from the internal
7672 breakpoint count. Returns true if any breakpoint was created;
7673 false otherwise. */
0101ce28 7674
8cdf0e15
VP
7675int
7676create_breakpoint (struct gdbarch *gdbarch,
7677 char *arg, char *cond_string, int thread,
7678 int parse_condition_and_thread,
0fb4aa4b 7679 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7680 int ignore_count,
7681 enum auto_boolean pending_break_support,
c0a91b2b 7682 const struct breakpoint_ops *ops,
84f4c1fe 7683 int from_tty, int enabled, int internal)
c3f6f71d 7684{
b78a6381 7685 volatile struct gdb_exception e;
f8eba3c6 7686 char *copy_arg = NULL;
c3f6f71d 7687 char *addr_start = arg;
7efd8fc2 7688 struct linespec_result canonical;
c3f6f71d 7689 struct cleanup *old_chain;
80c99de1 7690 struct cleanup *bkpt_chain = NULL;
05ff989b 7691 int i;
0101ce28 7692 int pending = 0;
4a306c9a 7693 int task = 0;
86b17b60 7694 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7695
348d480f
PA
7696 gdb_assert (ops != NULL);
7697
7efd8fc2 7698 init_linespec_result (&canonical);
c3f6f71d 7699
0fb4aa4b
PA
7700 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7701 {
7702 int i;
f8eba3c6 7703 struct linespec_sals lsal;
0fb4aa4b 7704
f8eba3c6 7705 lsal.sals = decode_static_tracepoint_spec (&arg);
0fb4aa4b
PA
7706
7707 copy_arg = savestring (addr_start, arg - addr_start);
f8eba3c6
TT
7708
7709 lsal.canonical = xstrdup (copy_arg);
7710 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
7711
0fb4aa4b
PA
7712 goto done;
7713 }
7714
b78a6381
TT
7715 TRY_CATCH (e, RETURN_MASK_ALL)
7716 {
f8eba3c6 7717 parse_breakpoint_sals (&arg, &canonical);
b78a6381 7718 }
0101ce28
JJ
7719
7720 /* If caller is interested in rc value from parse, set value. */
05ff989b 7721 switch (e.reason)
0101ce28 7722 {
05ff989b 7723 case RETURN_QUIT:
98deb0da 7724 throw_exception (e);
05ff989b
AC
7725 case RETURN_ERROR:
7726 switch (e.error)
0101ce28 7727 {
05ff989b 7728 case NOT_FOUND_ERROR:
0101ce28 7729
05ff989b
AC
7730 /* If pending breakpoint support is turned off, throw
7731 error. */
fa8d40ab
JJ
7732
7733 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7734 throw_exception (e);
7735
7736 exception_print (gdb_stderr, e);
fa8d40ab 7737
05ff989b
AC
7738 /* If pending breakpoint support is auto query and the user
7739 selects no, then simply return the error code. */
059fb39f 7740 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
7741 && !nquery (_("Make %s pending on future shared library load? "),
7742 bptype_string (type_wanted)))
fd9b8c24 7743 return 0;
fa8d40ab 7744
05ff989b
AC
7745 /* At this point, either the user was queried about setting
7746 a pending breakpoint and selected yes, or pending
7747 breakpoint behavior is on and thus a pending breakpoint
7748 is defaulted on behalf of the user. */
f8eba3c6
TT
7749 {
7750 struct linespec_sals lsal;
7751
7752 copy_arg = xstrdup (addr_start);
7753 lsal.canonical = xstrdup (copy_arg);
7754 lsal.sals.nelts = 1;
7755 lsal.sals.sals = XNEW (struct symtab_and_line);
7756 init_sal (&lsal.sals.sals[0]);
7757 pending = 1;
7758 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
7759 }
05ff989b
AC
7760 break;
7761 default:
98deb0da 7762 throw_exception (e);
0101ce28 7763 }
2abae994 7764 break;
05ff989b 7765 default:
f8eba3c6 7766 if (VEC_empty (linespec_sals, canonical.sals))
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. */
f8eba3c6 7773 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 7774
c3f6f71d
JM
7775 /* ----------------------------- SNIP -----------------------------
7776 Anything added to the cleanup chain beyond this point is assumed
7777 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7778 then the memory is not reclaimed. */
7779 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 7780
c3f6f71d
JM
7781 /* Resolve all line numbers to PC's and verify that the addresses
7782 are ok for the target. */
0101ce28 7783 if (!pending)
f8eba3c6
TT
7784 {
7785 int ix;
7786 struct linespec_sals *iter;
7787
7788 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
7789 breakpoint_sals_to_pc (&iter->sals);
7790 }
c3f6f71d 7791
7a697b8d 7792 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 7793 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
7794 {
7795 int ix;
7796 struct linespec_sals *iter;
7797
7798 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
7799 check_fast_tracepoint_sals (gdbarch, &iter->sals);
7800 }
7a697b8d 7801
c3f6f71d
JM
7802 /* Verify that condition can be parsed, before setting any
7803 breakpoints. Allocate a separate condition expression for each
4a64f543 7804 breakpoint. */
0101ce28 7805 if (!pending)
c3f6f71d 7806 {
f8eba3c6
TT
7807 struct linespec_sals *lsal;
7808
7809 lsal = VEC_index (linespec_sals, canonical.sals, 0);
7810
2f069f6f 7811 if (parse_condition_and_thread)
72b2ff0e
VP
7812 {
7813 /* Here we only parse 'arg' to separate condition
7814 from thread number, so parsing in context of first
7815 sal is OK. When setting the breakpoint we'll
7816 re-parse it in context of each sal. */
7817 cond_string = NULL;
7818 thread = -1;
f8eba3c6 7819 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
4a306c9a 7820 &thread, &task);
72b2ff0e
VP
7821 if (cond_string)
7822 make_cleanup (xfree, cond_string);
7823 }
2f069f6f 7824 else
72b2ff0e
VP
7825 {
7826 /* Create a private copy of condition string. */
7827 if (cond_string)
7828 {
7829 cond_string = xstrdup (cond_string);
7830 make_cleanup (xfree, cond_string);
7831 }
7832 }
0fb4aa4b
PA
7833
7834 /* If the user is creating a static tracepoint by marker id
7835 (strace -m MARKER_ID), then store the sals index, so that
7836 breakpoint_re_set can try to match up which of the newly
7837 found markers corresponds to this one, and, don't try to
7838 expand multiple locations for each sal, given than SALS
7839 already should contain all sals for MARKER_ID. */
7840 if (type_wanted == bp_static_tracepoint
f8eba3c6 7841 && is_marker_spec (copy_arg))
0fb4aa4b
PA
7842 {
7843 int i;
7844
f8eba3c6 7845 for (i = 0; i < lsal->sals.nelts; ++i)
0fb4aa4b
PA
7846 {
7847 struct symtabs_and_lines expanded;
d9b3f62e 7848 struct tracepoint *tp;
0fb4aa4b 7849 struct cleanup *old_chain;
f8eba3c6 7850 char *addr_string;
0fb4aa4b
PA
7851
7852 expanded.nelts = 1;
f8eba3c6
TT
7853 expanded.sals = &lsal->sals.sals[i];
7854
7855 addr_string = xstrdup (canonical.addr_string);
7856 old_chain = make_cleanup (xfree, addr_string);
0fb4aa4b 7857
d9b3f62e
PA
7858 tp = XCNEW (struct tracepoint);
7859 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f8eba3c6 7860 addr_string, NULL,
d9b3f62e
PA
7861 cond_string, type_wanted,
7862 tempflag ? disp_del : disp_donttouch,
7863 thread, task, ignore_count, ops,
7864 from_tty, enabled, internal,
7865 canonical.special_display);
0fb4aa4b
PA
7866 /* Given that its possible to have multiple markers with
7867 the same string id, if the user is creating a static
7868 tracepoint by marker id ("strace -m MARKER_ID"), then
7869 store the sals index, so that breakpoint_re_set can
7870 try to match up which of the newly found markers
7871 corresponds to this one */
7872 tp->static_trace_marker_id_idx = i;
d9b3f62e 7873
3ea46bff 7874 install_breakpoint (internal, &tp->base, 0);
d9b3f62e 7875
f8eba3c6 7876 discard_cleanups (old_chain);
0fb4aa4b
PA
7877 }
7878 }
7879 else
f8eba3c6 7880 create_breakpoints_sal (gdbarch, &canonical, cond_string,
3e43a32a
MS
7881 type_wanted,
7882 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 7883 thread, task, ignore_count, ops, from_tty,
84f4c1fe 7884 enabled, internal);
c906108c 7885 }
0101ce28
JJ
7886 else
7887 {
0101ce28
JJ
7888 struct breakpoint *b;
7889
0101ce28
JJ
7890 make_cleanup (xfree, copy_arg);
7891
bfccc43c
YQ
7892 if (is_tracepoint_type (type_wanted))
7893 {
7894 struct tracepoint *t;
7895
7896 t = XCNEW (struct tracepoint);
7897 b = &t->base;
7898 }
7899 else
7900 b = XNEW (struct breakpoint);
7901
7902 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
7903
f8eba3c6 7904 b->addr_string = copy_arg;
72b2ff0e 7905 b->cond_string = NULL;
0101ce28 7906 b->ignore_count = ignore_count;
0101ce28 7907 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 7908 b->condition_not_parsed = 1;
41447f92 7909 b->enable_state = enabled ? bp_enabled : bp_disabled;
f8eba3c6
TT
7910 if (type_wanted != bp_breakpoint && type_wanted != bp_hardware_breakpoint)
7911 b->pspace = current_program_space;
8bea4e01 7912
bfccc43c 7913 install_breakpoint (internal, b, 0);
0101ce28
JJ
7914 }
7915
f8eba3c6 7916 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 7917 {
3e43a32a
MS
7918 warning (_("Multiple breakpoints were set.\nUse the "
7919 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 7920 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
7921 }
7922
80c99de1
PA
7923 /* That's it. Discard the cleanups for data inserted into the
7924 breakpoint. */
7925 discard_cleanups (bkpt_chain);
7926 /* But cleanup everything else. */
c3f6f71d 7927 do_cleanups (old_chain);
217dc9e2 7928
80c99de1 7929 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 7930 update_global_location_list (1);
fd9b8c24
PA
7931
7932 return 1;
c3f6f71d 7933}
c906108c 7934
348d480f 7935/* Set a breakpoint.
72b2ff0e
VP
7936 ARG is a string describing breakpoint address,
7937 condition, and thread.
7938 FLAG specifies if a breakpoint is hardware on,
7939 and if breakpoint is temporary, using BP_HARDWARE_FLAG
7940 and BP_TEMPFLAG. */
348d480f 7941
98deb0da 7942static void
72b2ff0e 7943break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 7944{
72b2ff0e 7945 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
7946 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
7947 ? bp_hardware_breakpoint
7948 : bp_breakpoint);
c3f6f71d 7949
8cdf0e15
VP
7950 create_breakpoint (get_current_arch (),
7951 arg,
7952 NULL, 0, 1 /* parse arg */,
0fb4aa4b 7953 tempflag, type_wanted,
8cdf0e15
VP
7954 0 /* Ignore count */,
7955 pending_break_support,
348d480f 7956 &bkpt_breakpoint_ops,
8cdf0e15 7957 from_tty,
84f4c1fe
PM
7958 1 /* enabled */,
7959 0 /* internal */);
c906108c
SS
7960}
7961
c906108c
SS
7962/* Helper function for break_command_1 and disassemble_command. */
7963
7964void
fba45db2 7965resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
7966{
7967 CORE_ADDR pc;
7968
7969 if (sal->pc == 0 && sal->symtab != NULL)
7970 {
7971 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 7972 error (_("No line %d in file \"%s\"."),
c906108c
SS
7973 sal->line, sal->symtab->filename);
7974 sal->pc = pc;
6a048695 7975
4a64f543
MS
7976 /* If this SAL corresponds to a breakpoint inserted using a line
7977 number, then skip the function prologue if necessary. */
6a048695 7978 if (sal->explicit_line)
059acae7 7979 skip_prologue_sal (sal);
c906108c
SS
7980 }
7981
7982 if (sal->section == 0 && sal->symtab != NULL)
7983 {
7984 struct blockvector *bv;
c5aa993b
JM
7985 struct block *b;
7986 struct symbol *sym;
c906108c 7987
801e3a5b 7988 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
7989 if (bv != NULL)
7990 {
7f0df278 7991 sym = block_linkage_function (b);
c906108c
SS
7992 if (sym != NULL)
7993 {
7994 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 7995 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
7996 }
7997 else
7998 {
4a64f543
MS
7999 /* It really is worthwhile to have the section, so we'll
8000 just have to look harder. This case can be executed
8001 if we have line numbers but no functions (as can
8002 happen in assembly source). */
c906108c 8003
c5aa993b 8004 struct minimal_symbol *msym;
6c95b8df
PA
8005 struct cleanup *old_chain = save_current_space_and_thread ();
8006
8007 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8008
8009 msym = lookup_minimal_symbol_by_pc (sal->pc);
8010 if (msym)
714835d5 8011 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8012
8013 do_cleanups (old_chain);
c906108c
SS
8014 }
8015 }
8016 }
8017}
8018
8019void
fba45db2 8020break_command (char *arg, int from_tty)
c906108c 8021{
db107f19 8022 break_command_1 (arg, 0, from_tty);
c906108c
SS
8023}
8024
c906108c 8025void
fba45db2 8026tbreak_command (char *arg, int from_tty)
c906108c 8027{
db107f19 8028 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8029}
8030
c906108c 8031static void
fba45db2 8032hbreak_command (char *arg, int from_tty)
c906108c 8033{
db107f19 8034 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8035}
8036
8037static void
fba45db2 8038thbreak_command (char *arg, int from_tty)
c906108c 8039{
db107f19 8040 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8041}
8042
8043static void
fba45db2 8044stop_command (char *arg, int from_tty)
c906108c 8045{
a3f17187 8046 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8047Usage: stop in <function | address>\n\
a3f17187 8048 stop at <line>\n"));
c906108c
SS
8049}
8050
8051static void
fba45db2 8052stopin_command (char *arg, int from_tty)
c906108c
SS
8053{
8054 int badInput = 0;
8055
c5aa993b 8056 if (arg == (char *) NULL)
c906108c
SS
8057 badInput = 1;
8058 else if (*arg != '*')
8059 {
8060 char *argptr = arg;
8061 int hasColon = 0;
8062
4a64f543 8063 /* Look for a ':'. If this is a line number specification, then
53a5351d 8064 say it is bad, otherwise, it should be an address or
4a64f543 8065 function/method name. */
c906108c 8066 while (*argptr && !hasColon)
c5aa993b
JM
8067 {
8068 hasColon = (*argptr == ':');
8069 argptr++;
8070 }
c906108c
SS
8071
8072 if (hasColon)
c5aa993b 8073 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8074 else
c5aa993b 8075 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8076 }
8077
8078 if (badInput)
a3f17187 8079 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8080 else
db107f19 8081 break_command_1 (arg, 0, from_tty);
c906108c
SS
8082}
8083
8084static void
fba45db2 8085stopat_command (char *arg, int from_tty)
c906108c
SS
8086{
8087 int badInput = 0;
8088
c5aa993b 8089 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8090 badInput = 1;
8091 else
8092 {
8093 char *argptr = arg;
8094 int hasColon = 0;
8095
4a64f543
MS
8096 /* Look for a ':'. If there is a '::' then get out, otherwise
8097 it is probably a line number. */
c906108c 8098 while (*argptr && !hasColon)
c5aa993b
JM
8099 {
8100 hasColon = (*argptr == ':');
8101 argptr++;
8102 }
c906108c
SS
8103
8104 if (hasColon)
c5aa993b 8105 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8106 else
c5aa993b 8107 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8108 }
8109
8110 if (badInput)
a3f17187 8111 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8112 else
db107f19 8113 break_command_1 (arg, 0, from_tty);
c906108c
SS
8114}
8115
f1310107
TJB
8116/* Implement the "breakpoint_hit" breakpoint_ops method for
8117 ranged breakpoints. */
8118
8119static int
8120breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8121 struct address_space *aspace,
8122 CORE_ADDR bp_addr)
8123{
8124 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8125 bl->length, aspace, bp_addr);
8126}
8127
8128/* Implement the "resources_needed" breakpoint_ops method for
8129 ranged breakpoints. */
8130
8131static int
8132resources_needed_ranged_breakpoint (const struct bp_location *bl)
8133{
8134 return target_ranged_break_num_registers ();
8135}
8136
8137/* Implement the "print_it" breakpoint_ops method for
8138 ranged breakpoints. */
8139
8140static enum print_stop_action
348d480f 8141print_it_ranged_breakpoint (bpstat bs)
f1310107 8142{
348d480f 8143 struct breakpoint *b = bs->breakpoint_at;
f1310107 8144 struct bp_location *bl = b->loc;
79a45e25 8145 struct ui_out *uiout = current_uiout;
f1310107
TJB
8146
8147 gdb_assert (b->type == bp_hardware_breakpoint);
8148
8149 /* Ranged breakpoints have only one location. */
8150 gdb_assert (bl && bl->next == NULL);
8151
8152 annotate_breakpoint (b->number);
8153 if (b->disposition == disp_del)
8154 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8155 else
8156 ui_out_text (uiout, "\nRanged breakpoint ");
8157 if (ui_out_is_mi_like_p (uiout))
8158 {
8159 ui_out_field_string (uiout, "reason",
8160 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8161 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8162 }
8163 ui_out_field_int (uiout, "bkptno", b->number);
8164 ui_out_text (uiout, ", ");
8165
8166 return PRINT_SRC_AND_LOC;
8167}
8168
8169/* Implement the "print_one" breakpoint_ops method for
8170 ranged breakpoints. */
8171
8172static void
8173print_one_ranged_breakpoint (struct breakpoint *b,
8174 struct bp_location **last_loc)
8175{
8176 struct bp_location *bl = b->loc;
8177 struct value_print_options opts;
79a45e25 8178 struct ui_out *uiout = current_uiout;
f1310107
TJB
8179
8180 /* Ranged breakpoints have only one location. */
8181 gdb_assert (bl && bl->next == NULL);
8182
8183 get_user_print_options (&opts);
8184
8185 if (opts.addressprint)
8186 /* We don't print the address range here, it will be printed later
8187 by print_one_detail_ranged_breakpoint. */
8188 ui_out_field_skip (uiout, "addr");
8189 annotate_field (5);
8190 print_breakpoint_location (b, bl);
8191 *last_loc = bl;
8192}
8193
8194/* Implement the "print_one_detail" breakpoint_ops method for
8195 ranged breakpoints. */
8196
8197static void
8198print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8199 struct ui_out *uiout)
8200{
8201 CORE_ADDR address_start, address_end;
8202 struct bp_location *bl = b->loc;
8203 struct ui_stream *stb = ui_out_stream_new (uiout);
8204 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8205
8206 gdb_assert (bl);
8207
8208 address_start = bl->address;
8209 address_end = address_start + bl->length - 1;
8210
8211 ui_out_text (uiout, "\taddress range: ");
8212 fprintf_unfiltered (stb->stream, "[%s, %s]",
8213 print_core_address (bl->gdbarch, address_start),
8214 print_core_address (bl->gdbarch, address_end));
8215 ui_out_field_stream (uiout, "addr", stb);
8216 ui_out_text (uiout, "\n");
8217
8218 do_cleanups (cleanup);
8219}
8220
8221/* Implement the "print_mention" breakpoint_ops method for
8222 ranged breakpoints. */
8223
8224static void
8225print_mention_ranged_breakpoint (struct breakpoint *b)
8226{
8227 struct bp_location *bl = b->loc;
79a45e25 8228 struct ui_out *uiout = current_uiout;
f1310107
TJB
8229
8230 gdb_assert (bl);
8231 gdb_assert (b->type == bp_hardware_breakpoint);
8232
8233 if (ui_out_is_mi_like_p (uiout))
8234 return;
8235
8236 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8237 b->number, paddress (bl->gdbarch, bl->address),
8238 paddress (bl->gdbarch, bl->address + bl->length - 1));
8239}
8240
8241/* Implement the "print_recreate" breakpoint_ops method for
8242 ranged breakpoints. */
8243
8244static void
8245print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8246{
8247 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8248 b->addr_string_range_end);
d9b3f62e 8249 print_recreate_thread (b, fp);
f1310107
TJB
8250}
8251
8252/* The breakpoint_ops structure to be used in ranged breakpoints. */
8253
2060206e 8254static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
8255
8256/* Find the address where the end of the breakpoint range should be
8257 placed, given the SAL of the end of the range. This is so that if
8258 the user provides a line number, the end of the range is set to the
8259 last instruction of the given line. */
8260
8261static CORE_ADDR
8262find_breakpoint_range_end (struct symtab_and_line sal)
8263{
8264 CORE_ADDR end;
8265
8266 /* If the user provided a PC value, use it. Otherwise,
8267 find the address of the end of the given location. */
8268 if (sal.explicit_pc)
8269 end = sal.pc;
8270 else
8271 {
8272 int ret;
8273 CORE_ADDR start;
8274
8275 ret = find_line_pc_range (sal, &start, &end);
8276 if (!ret)
8277 error (_("Could not find location of the end of the range."));
8278
8279 /* find_line_pc_range returns the start of the next line. */
8280 end--;
8281 }
8282
8283 return end;
8284}
8285
8286/* Implement the "break-range" CLI command. */
8287
8288static void
8289break_range_command (char *arg, int from_tty)
8290{
8291 char *arg_start, *addr_string_start, *addr_string_end;
8292 struct linespec_result canonical_start, canonical_end;
8293 int bp_count, can_use_bp, length;
8294 CORE_ADDR end;
8295 struct breakpoint *b;
8296 struct symtab_and_line sal_start, sal_end;
f1310107 8297 struct cleanup *cleanup_bkpt;
f8eba3c6 8298 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
8299
8300 /* We don't support software ranged breakpoints. */
8301 if (target_ranged_break_num_registers () < 0)
8302 error (_("This target does not support hardware ranged breakpoints."));
8303
8304 bp_count = hw_breakpoint_used_count ();
8305 bp_count += target_ranged_break_num_registers ();
8306 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8307 bp_count, 0);
8308 if (can_use_bp < 0)
8309 error (_("Hardware breakpoints used exceeds limit."));
8310
f8eba3c6 8311 arg = skip_spaces (arg);
f1310107
TJB
8312 if (arg == NULL || arg[0] == '\0')
8313 error(_("No address range specified."));
8314
f1310107
TJB
8315 init_linespec_result (&canonical_start);
8316
f8eba3c6
TT
8317 arg_start = arg;
8318 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 8319
f8eba3c6 8320 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
8321
8322 if (arg[0] != ',')
8323 error (_("Too few arguments."));
f8eba3c6 8324 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 8325 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
8326
8327 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
8328
8329 if (VEC_length (linespec_sals, canonical_start.sals) > 1
8330 || lsal_start->sals.nelts != 1)
f1310107
TJB
8331 error (_("Cannot create a ranged breakpoint with multiple locations."));
8332
f8eba3c6
TT
8333 sal_start = lsal_start->sals.sals[0];
8334 addr_string_start = savestring (arg_start, arg - arg_start);
8335 make_cleanup (xfree, addr_string_start);
f1310107
TJB
8336
8337 arg++; /* Skip the comma. */
f8eba3c6 8338 arg = skip_spaces (arg);
f1310107
TJB
8339
8340 /* Parse the end location. */
8341
f1310107
TJB
8342 init_linespec_result (&canonical_end);
8343 arg_start = arg;
8344
f8eba3c6 8345 /* We call decode_line_full 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. */
f8eba3c6
TT
8350 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
8351 sal_start.symtab, sal_start.line,
8352 &canonical_end, NULL, NULL);
8353
8354 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 8355
f8eba3c6 8356 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 8357 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
8358
8359 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
8360 if (VEC_length (linespec_sals, canonical_end.sals) > 1
8361 || lsal_end->sals.nelts != 1)
f1310107
TJB
8362 error (_("Cannot create a ranged breakpoint with multiple locations."));
8363
f8eba3c6
TT
8364 sal_end = lsal_end->sals.sals[0];
8365 addr_string_end = savestring (arg_start, arg - arg_start);
8366 make_cleanup (xfree, addr_string_end);
f1310107
TJB
8367
8368 end = find_breakpoint_range_end (sal_end);
8369 if (sal_start.pc > end)
177b42fe 8370 error (_("Invalid address range, end precedes start."));
f1310107
TJB
8371
8372 length = end - sal_start.pc + 1;
8373 if (length < 0)
8374 /* Length overflowed. */
8375 error (_("Address range too large."));
8376 else if (length == 1)
8377 {
8378 /* This range is simple enough to be handled by
8379 the `hbreak' command. */
8380 hbreak_command (addr_string_start, 1);
8381
8382 do_cleanups (cleanup_bkpt);
8383
8384 return;
8385 }
8386
8387 /* Now set up the breakpoint. */
8388 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 8389 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
8390 set_breakpoint_count (breakpoint_count + 1);
8391 b->number = breakpoint_count;
8392 b->disposition = disp_donttouch;
f8eba3c6
TT
8393 b->addr_string = xstrdup (addr_string_start);
8394 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
8395 b->loc->length = length;
8396
f8eba3c6 8397 do_cleanups (cleanup_bkpt);
f1310107
TJB
8398
8399 mention (b);
8d3788bd 8400 observer_notify_breakpoint_created (b);
f1310107
TJB
8401 update_global_location_list (1);
8402}
8403
4a64f543
MS
8404/* Return non-zero if EXP is verified as constant. Returned zero
8405 means EXP is variable. Also the constant detection may fail for
8406 some constant expressions and in such case still falsely return
8407 zero. */
65d79d4b
SDJ
8408static int
8409watchpoint_exp_is_const (const struct expression *exp)
8410{
8411 int i = exp->nelts;
8412
8413 while (i > 0)
8414 {
8415 int oplenp, argsp;
8416
8417 /* We are only interested in the descriptor of each element. */
8418 operator_length (exp, i, &oplenp, &argsp);
8419 i -= oplenp;
8420
8421 switch (exp->elts[i].opcode)
8422 {
8423 case BINOP_ADD:
8424 case BINOP_SUB:
8425 case BINOP_MUL:
8426 case BINOP_DIV:
8427 case BINOP_REM:
8428 case BINOP_MOD:
8429 case BINOP_LSH:
8430 case BINOP_RSH:
8431 case BINOP_LOGICAL_AND:
8432 case BINOP_LOGICAL_OR:
8433 case BINOP_BITWISE_AND:
8434 case BINOP_BITWISE_IOR:
8435 case BINOP_BITWISE_XOR:
8436 case BINOP_EQUAL:
8437 case BINOP_NOTEQUAL:
8438 case BINOP_LESS:
8439 case BINOP_GTR:
8440 case BINOP_LEQ:
8441 case BINOP_GEQ:
8442 case BINOP_REPEAT:
8443 case BINOP_COMMA:
8444 case BINOP_EXP:
8445 case BINOP_MIN:
8446 case BINOP_MAX:
8447 case BINOP_INTDIV:
8448 case BINOP_CONCAT:
8449 case BINOP_IN:
8450 case BINOP_RANGE:
8451 case TERNOP_COND:
8452 case TERNOP_SLICE:
8453 case TERNOP_SLICE_COUNT:
8454
8455 case OP_LONG:
8456 case OP_DOUBLE:
8457 case OP_DECFLOAT:
8458 case OP_LAST:
8459 case OP_COMPLEX:
8460 case OP_STRING:
8461 case OP_BITSTRING:
8462 case OP_ARRAY:
8463 case OP_TYPE:
8464 case OP_NAME:
8465 case OP_OBJC_NSSTRING:
8466
8467 case UNOP_NEG:
8468 case UNOP_LOGICAL_NOT:
8469 case UNOP_COMPLEMENT:
8470 case UNOP_ADDR:
8471 case UNOP_HIGH:
4a64f543
MS
8472 /* Unary, binary and ternary operators: We have to check
8473 their operands. If they are constant, then so is the
8474 result of that operation. For instance, if A and B are
8475 determined to be constants, then so is "A + B".
8476
8477 UNOP_IND is one exception to the rule above, because the
8478 value of *ADDR is not necessarily a constant, even when
8479 ADDR is. */
65d79d4b
SDJ
8480 break;
8481
8482 case OP_VAR_VALUE:
8483 /* Check whether the associated symbol is a constant.
4a64f543 8484
65d79d4b 8485 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8486 possible that a buggy compiler could mark a variable as
8487 constant even when it is not, and TYPE_CONST would return
8488 true in this case, while SYMBOL_CLASS wouldn't.
8489
8490 We also have to check for function symbols because they
8491 are always constant. */
65d79d4b
SDJ
8492 {
8493 struct symbol *s = exp->elts[i + 2].symbol;
8494
8495 if (SYMBOL_CLASS (s) != LOC_BLOCK
8496 && SYMBOL_CLASS (s) != LOC_CONST
8497 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8498 return 0;
8499 break;
8500 }
8501
8502 /* The default action is to return 0 because we are using
8503 the optimistic approach here: If we don't know something,
8504 then it is not a constant. */
8505 default:
8506 return 0;
8507 }
8508 }
8509
8510 return 1;
8511}
8512
3a5c3e22
PA
8513/* Implement the "dtor" breakpoint_ops method for watchpoints. */
8514
8515static void
8516dtor_watchpoint (struct breakpoint *self)
8517{
8518 struct watchpoint *w = (struct watchpoint *) self;
8519
8520 xfree (w->cond_exp);
8521 xfree (w->exp);
8522 xfree (w->exp_string);
8523 xfree (w->exp_string_reparse);
8524 value_free (w->val);
8525
8526 base_breakpoint_ops.dtor (self);
8527}
8528
348d480f
PA
8529/* Implement the "re_set" breakpoint_ops method for watchpoints. */
8530
8531static void
8532re_set_watchpoint (struct breakpoint *b)
8533{
3a5c3e22
PA
8534 struct watchpoint *w = (struct watchpoint *) b;
8535
348d480f
PA
8536 /* Watchpoint can be either on expression using entirely global
8537 variables, or it can be on local variables.
8538
8539 Watchpoints of the first kind are never auto-deleted, and even
8540 persist across program restarts. Since they can use variables
8541 from shared libraries, we need to reparse expression as libraries
8542 are loaded and unloaded.
8543
8544 Watchpoints on local variables can also change meaning as result
8545 of solib event. For example, if a watchpoint uses both a local
8546 and a global variables in expression, it's a local watchpoint,
8547 but unloading of a shared library will make the expression
8548 invalid. This is not a very common use case, but we still
8549 re-evaluate expression, to avoid surprises to the user.
8550
8551 Note that for local watchpoints, we re-evaluate it only if
8552 watchpoints frame id is still valid. If it's not, it means the
8553 watchpoint is out of scope and will be deleted soon. In fact,
8554 I'm not sure we'll ever be called in this case.
8555
8556 If a local watchpoint's frame id is still valid, then
3a5c3e22 8557 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 8558
3a5c3e22
PA
8559 Don't do anything about disabled watchpoints, since they will be
8560 reevaluated again when enabled. */
8561 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
8562}
8563
77b06cd7
TJB
8564/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8565
8566static int
8567insert_watchpoint (struct bp_location *bl)
8568{
3a5c3e22
PA
8569 struct watchpoint *w = (struct watchpoint *) bl->owner;
8570 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8571
8572 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8573 w->cond_exp);
77b06cd7
TJB
8574}
8575
8576/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8577
8578static int
8579remove_watchpoint (struct bp_location *bl)
8580{
3a5c3e22
PA
8581 struct watchpoint *w = (struct watchpoint *) bl->owner;
8582 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8583
8584 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8585 w->cond_exp);
e09342b5
TJB
8586}
8587
e09342b5 8588static int
348d480f
PA
8589breakpoint_hit_watchpoint (const struct bp_location *bl,
8590 struct address_space *aspace, CORE_ADDR bp_addr)
e09342b5 8591{
348d480f 8592 struct breakpoint *b = bl->owner;
3a5c3e22 8593 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 8594
348d480f
PA
8595 /* Continuable hardware watchpoints are treated as non-existent if the
8596 reason we stopped wasn't a hardware watchpoint (we didn't stop on
8597 some data address). Otherwise gdb won't stop on a break instruction
8598 in the code (not from a breakpoint) when a hardware watchpoint has
8599 been defined. Also skip watchpoints which we know did not trigger
8600 (did not match the data address). */
8601 if (is_hardware_watchpoint (b)
3a5c3e22 8602 && w->watchpoint_triggered == watch_triggered_no)
348d480f 8603 return 0;
9c06b0b4 8604
348d480f 8605 return 1;
9c06b0b4
TJB
8606}
8607
348d480f
PA
8608static void
8609check_status_watchpoint (bpstat bs)
9c06b0b4 8610{
348d480f 8611 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 8612
348d480f 8613 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
8614}
8615
8616/* Implement the "resources_needed" breakpoint_ops method for
348d480f 8617 hardware watchpoints. */
9c06b0b4
TJB
8618
8619static int
348d480f 8620resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 8621{
3a5c3e22
PA
8622 struct watchpoint *w = (struct watchpoint *) bl->owner;
8623 int length = w->exact? 1 : bl->length;
348d480f
PA
8624
8625 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
8626}
8627
8628/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 8629 hardware watchpoints. */
9c06b0b4
TJB
8630
8631static int
348d480f 8632works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 8633{
efa80663
PA
8634 /* Read and access watchpoints only work with hardware support. */
8635 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
8636}
8637
9c06b0b4 8638static enum print_stop_action
348d480f 8639print_it_watchpoint (bpstat bs)
9c06b0b4 8640{
348d480f
PA
8641 struct cleanup *old_chain;
8642 struct breakpoint *b;
8643 const struct bp_location *bl;
8644 struct ui_stream *stb;
8645 enum print_stop_action result;
3a5c3e22 8646 struct watchpoint *w;
79a45e25 8647 struct ui_out *uiout = current_uiout;
348d480f
PA
8648
8649 gdb_assert (bs->bp_location_at != NULL);
8650
8651 bl = bs->bp_location_at;
8652 b = bs->breakpoint_at;
3a5c3e22 8653 w = (struct watchpoint *) b;
348d480f
PA
8654
8655 stb = ui_out_stream_new (uiout);
8656 old_chain = make_cleanup_ui_out_stream_delete (stb);
9c06b0b4
TJB
8657
8658 switch (b->type)
8659 {
348d480f 8660 case bp_watchpoint:
9c06b0b4
TJB
8661 case bp_hardware_watchpoint:
8662 annotate_watchpoint (b->number);
8663 if (ui_out_is_mi_like_p (uiout))
8664 ui_out_field_string
8665 (uiout, "reason",
8666 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
8667 mention (b);
8668 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8669 ui_out_text (uiout, "\nOld value = ");
8670 watchpoint_value_print (bs->old_val, stb->stream);
8671 ui_out_field_stream (uiout, "old", stb);
8672 ui_out_text (uiout, "\nNew value = ");
3a5c3e22 8673 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8674 ui_out_field_stream (uiout, "new", stb);
8675 ui_out_text (uiout, "\n");
8676 /* More than one watchpoint may have been triggered. */
8677 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8678 break;
8679
8680 case bp_read_watchpoint:
8681 if (ui_out_is_mi_like_p (uiout))
8682 ui_out_field_string
8683 (uiout, "reason",
8684 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
8685 mention (b);
8686 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8687 ui_out_text (uiout, "\nValue = ");
3a5c3e22 8688 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8689 ui_out_field_stream (uiout, "value", stb);
8690 ui_out_text (uiout, "\n");
8691 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8692 break;
8693
8694 case bp_access_watchpoint:
348d480f
PA
8695 if (bs->old_val != NULL)
8696 {
8697 annotate_watchpoint (b->number);
8698 if (ui_out_is_mi_like_p (uiout))
8699 ui_out_field_string
8700 (uiout, "reason",
8701 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8702 mention (b);
8703 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8704 ui_out_text (uiout, "\nOld value = ");
8705 watchpoint_value_print (bs->old_val, stb->stream);
8706 ui_out_field_stream (uiout, "old", stb);
8707 ui_out_text (uiout, "\nNew value = ");
8708 }
8709 else
8710 {
8711 mention (b);
8712 if (ui_out_is_mi_like_p (uiout))
8713 ui_out_field_string
8714 (uiout, "reason",
8715 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8716 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8717 ui_out_text (uiout, "\nValue = ");
8718 }
3a5c3e22 8719 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8720 ui_out_field_stream (uiout, "new", stb);
8721 ui_out_text (uiout, "\n");
8722 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8723 break;
8724 default:
348d480f 8725 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8726 }
8727
348d480f
PA
8728 do_cleanups (old_chain);
8729 return result;
8730}
8731
8732/* Implement the "print_mention" breakpoint_ops method for hardware
8733 watchpoints. */
8734
8735static void
8736print_mention_watchpoint (struct breakpoint *b)
8737{
8738 struct cleanup *ui_out_chain;
3a5c3e22 8739 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 8740 struct ui_out *uiout = current_uiout;
348d480f
PA
8741
8742 switch (b->type)
8743 {
8744 case bp_watchpoint:
8745 ui_out_text (uiout, "Watchpoint ");
8746 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8747 break;
8748 case bp_hardware_watchpoint:
8749 ui_out_text (uiout, "Hardware watchpoint ");
8750 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8751 break;
8752 case bp_read_watchpoint:
8753 ui_out_text (uiout, "Hardware read watchpoint ");
8754 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8755 break;
8756 case bp_access_watchpoint:
8757 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
8758 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8759 break;
8760 default:
8761 internal_error (__FILE__, __LINE__,
8762 _("Invalid hardware watchpoint type."));
8763 }
8764
8765 ui_out_field_int (uiout, "number", b->number);
8766 ui_out_text (uiout, ": ");
3a5c3e22 8767 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
8768 do_cleanups (ui_out_chain);
8769}
8770
8771/* Implement the "print_recreate" breakpoint_ops method for
8772 watchpoints. */
8773
8774static void
8775print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
8776{
3a5c3e22
PA
8777 struct watchpoint *w = (struct watchpoint *) b;
8778
348d480f
PA
8779 switch (b->type)
8780 {
8781 case bp_watchpoint:
8782 case bp_hardware_watchpoint:
8783 fprintf_unfiltered (fp, "watch");
8784 break;
8785 case bp_read_watchpoint:
8786 fprintf_unfiltered (fp, "rwatch");
8787 break;
8788 case bp_access_watchpoint:
8789 fprintf_unfiltered (fp, "awatch");
8790 break;
8791 default:
8792 internal_error (__FILE__, __LINE__,
8793 _("Invalid watchpoint type."));
8794 }
8795
3a5c3e22 8796 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 8797 print_recreate_thread (b, fp);
348d480f
PA
8798}
8799
8800/* The breakpoint_ops structure to be used in hardware watchpoints. */
8801
2060206e 8802static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
8803
8804/* Implement the "insert" breakpoint_ops method for
8805 masked hardware watchpoints. */
8806
8807static int
8808insert_masked_watchpoint (struct bp_location *bl)
8809{
3a5c3e22
PA
8810 struct watchpoint *w = (struct watchpoint *) bl->owner;
8811
8812 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8813 bl->watchpoint_type);
8814}
8815
8816/* Implement the "remove" breakpoint_ops method for
8817 masked hardware watchpoints. */
8818
8819static int
8820remove_masked_watchpoint (struct bp_location *bl)
8821{
3a5c3e22
PA
8822 struct watchpoint *w = (struct watchpoint *) bl->owner;
8823
8824 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8825 bl->watchpoint_type);
8826}
8827
8828/* Implement the "resources_needed" breakpoint_ops method for
8829 masked hardware watchpoints. */
8830
8831static int
8832resources_needed_masked_watchpoint (const struct bp_location *bl)
8833{
3a5c3e22
PA
8834 struct watchpoint *w = (struct watchpoint *) bl->owner;
8835
8836 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
8837}
8838
8839/* Implement the "works_in_software_mode" breakpoint_ops method for
8840 masked hardware watchpoints. */
8841
8842static int
8843works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
8844{
8845 return 0;
8846}
8847
8848/* Implement the "print_it" breakpoint_ops method for
8849 masked hardware watchpoints. */
8850
8851static enum print_stop_action
8852print_it_masked_watchpoint (bpstat bs)
8853{
8854 struct breakpoint *b = bs->breakpoint_at;
79a45e25 8855 struct ui_out *uiout = current_uiout;
348d480f
PA
8856
8857 /* Masked watchpoints have only one location. */
8858 gdb_assert (b->loc && b->loc->next == NULL);
8859
8860 switch (b->type)
8861 {
8862 case bp_hardware_watchpoint:
8863 annotate_watchpoint (b->number);
8864 if (ui_out_is_mi_like_p (uiout))
8865 ui_out_field_string
8866 (uiout, "reason",
8867 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
8868 break;
8869
8870 case bp_read_watchpoint:
8871 if (ui_out_is_mi_like_p (uiout))
8872 ui_out_field_string
8873 (uiout, "reason",
8874 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
8875 break;
8876
8877 case bp_access_watchpoint:
8878 if (ui_out_is_mi_like_p (uiout))
8879 ui_out_field_string
8880 (uiout, "reason",
8881 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8882 break;
8883 default:
8884 internal_error (__FILE__, __LINE__,
8885 _("Invalid hardware watchpoint type."));
8886 }
8887
8888 mention (b);
9c06b0b4
TJB
8889 ui_out_text (uiout, _("\n\
8890Check the underlying instruction at PC for the memory\n\
8891address and value which triggered this watchpoint.\n"));
8892 ui_out_text (uiout, "\n");
8893
8894 /* More than one watchpoint may have been triggered. */
8895 return PRINT_UNKNOWN;
8896}
8897
8898/* Implement the "print_one_detail" breakpoint_ops method for
8899 masked hardware watchpoints. */
8900
8901static void
8902print_one_detail_masked_watchpoint (const struct breakpoint *b,
8903 struct ui_out *uiout)
8904{
3a5c3e22
PA
8905 struct watchpoint *w = (struct watchpoint *) b;
8906
9c06b0b4
TJB
8907 /* Masked watchpoints have only one location. */
8908 gdb_assert (b->loc && b->loc->next == NULL);
8909
8910 ui_out_text (uiout, "\tmask ");
3a5c3e22 8911 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
8912 ui_out_text (uiout, "\n");
8913}
8914
8915/* Implement the "print_mention" breakpoint_ops method for
8916 masked hardware watchpoints. */
8917
8918static void
8919print_mention_masked_watchpoint (struct breakpoint *b)
8920{
3a5c3e22 8921 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 8922 struct ui_out *uiout = current_uiout;
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
3ea46bff 9301 install_breakpoint (internal, b, 1);
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
1bfeeb0f 9503 if (last_displayed_sal_is_valid ())
f8eba3c6 9504 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 9505 get_last_displayed_symtab (),
f8eba3c6 9506 get_last_displayed_line ());
c906108c 9507 else
f8eba3c6
TT
9508 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
9509 (struct symtab *) NULL, 0);
c5aa993b 9510
c906108c 9511 if (sals.nelts != 1)
8a3fe4f8 9512 error (_("Couldn't get information on specified line."));
c5aa993b 9513
c906108c 9514 sal = sals.sals[0];
4a64f543 9515 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9516
c906108c 9517 if (*arg)
8a3fe4f8 9518 error (_("Junk at end of arguments."));
c5aa993b 9519
c906108c 9520 resolve_sal_pc (&sal);
c5aa993b 9521
ae66c1fc
EZ
9522 if (anywhere)
9523 /* If the user told us to continue until a specified location,
9524 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9525 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9526 null_frame_id, bp_until);
ae66c1fc 9527 else
4a64f543
MS
9528 /* Otherwise, specify the selected frame, because we want to stop
9529 only at the very same frame. */
a6d9a66e
UW
9530 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9531 get_stack_frame_id (frame),
ae66c1fc 9532 bp_until);
c5aa993b 9533
f107f563 9534 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9535
186c406b
TT
9536 tp = inferior_thread ();
9537 thread = tp->num;
9538
ae66c1fc
EZ
9539 /* Keep within the current frame, or in frames called by the current
9540 one. */
edb3359d
DJ
9541
9542 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9543 {
edb3359d
DJ
9544 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9545 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9546 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9547 sal,
edb3359d 9548 frame_unwind_caller_id (frame),
f107f563
VP
9549 bp_until);
9550 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9551
9552 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9553 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9554 }
c5aa993b 9555
c906108c 9556 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9557
4a64f543
MS
9558 /* If we are running asynchronously, and proceed call above has
9559 actually managed to start the target, arrange for breakpoints to
9560 be deleted when the target stops. Otherwise, we're already
9561 stopped and delete breakpoints via cleanup chain. */
f107f563 9562
8ea051c5 9563 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9564 {
bfec99b2
PA
9565 struct until_break_command_continuation_args *args;
9566 args = xmalloc (sizeof (*args));
f107f563 9567
bfec99b2
PA
9568 args->breakpoint = breakpoint;
9569 args->breakpoint2 = breakpoint2;
186c406b 9570 args->thread_num = thread;
f107f563
VP
9571
9572 discard_cleanups (old_chain);
95e54da7
PA
9573 add_continuation (inferior_thread (),
9574 until_break_command_continuation, args,
604ead4a 9575 xfree);
f107f563
VP
9576 }
9577 else
c5aa993b 9578 do_cleanups (old_chain);
c906108c 9579}
ae66c1fc 9580
c906108c
SS
9581/* This function attempts to parse an optional "if <cond>" clause
9582 from the arg string. If one is not found, it returns NULL.
c5aa993b 9583
c906108c
SS
9584 Else, it returns a pointer to the condition string. (It does not
9585 attempt to evaluate the string against a particular block.) And,
9586 it updates arg to point to the first character following the parsed
4a64f543 9587 if clause in the arg string. */
53a5351d 9588
c906108c 9589static char *
fba45db2 9590ep_parse_optional_if_clause (char **arg)
c906108c 9591{
c5aa993b
JM
9592 char *cond_string;
9593
9594 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9595 return NULL;
c5aa993b 9596
4a64f543 9597 /* Skip the "if" keyword. */
c906108c 9598 (*arg) += 2;
c5aa993b 9599
c906108c 9600 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9601 condition string. */
e9cafbcc 9602 *arg = skip_spaces (*arg);
c906108c 9603 cond_string = *arg;
c5aa993b 9604
4a64f543
MS
9605 /* Assume that the condition occupies the remainder of the arg
9606 string. */
c906108c 9607 (*arg) += strlen (cond_string);
c5aa993b 9608
c906108c
SS
9609 return cond_string;
9610}
c5aa993b 9611
c906108c
SS
9612/* Commands to deal with catching events, such as signals, exceptions,
9613 process start/exit, etc. */
c5aa993b
JM
9614
9615typedef enum
9616{
44feb3ce
TT
9617 catch_fork_temporary, catch_vfork_temporary,
9618 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9619}
9620catch_fork_kind;
9621
c906108c 9622static void
cc59ec59
MS
9623catch_fork_command_1 (char *arg, int from_tty,
9624 struct cmd_list_element *command)
c906108c 9625{
a6d9a66e 9626 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9627 char *cond_string = NULL;
44feb3ce
TT
9628 catch_fork_kind fork_kind;
9629 int tempflag;
9630
9631 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9632 tempflag = (fork_kind == catch_fork_temporary
9633 || fork_kind == catch_vfork_temporary);
c5aa993b 9634
44feb3ce
TT
9635 if (!arg)
9636 arg = "";
e9cafbcc 9637 arg = skip_spaces (arg);
c5aa993b 9638
c906108c 9639 /* The allowed syntax is:
c5aa993b
JM
9640 catch [v]fork
9641 catch [v]fork if <cond>
9642
4a64f543 9643 First, check if there's an if clause. */
c906108c 9644 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9645
c906108c 9646 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9647 error (_("Junk at end of arguments."));
c5aa993b 9648
c906108c 9649 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9650 and enable reporting of such events. */
c5aa993b
JM
9651 switch (fork_kind)
9652 {
44feb3ce
TT
9653 case catch_fork_temporary:
9654 case catch_fork_permanent:
a6d9a66e 9655 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9656 &catch_fork_breakpoint_ops);
c906108c 9657 break;
44feb3ce
TT
9658 case catch_vfork_temporary:
9659 case catch_vfork_permanent:
a6d9a66e 9660 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9661 &catch_vfork_breakpoint_ops);
c906108c 9662 break;
c5aa993b 9663 default:
8a3fe4f8 9664 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9665 break;
c5aa993b 9666 }
c906108c
SS
9667}
9668
9669static void
cc59ec59
MS
9670catch_exec_command_1 (char *arg, int from_tty,
9671 struct cmd_list_element *command)
c906108c 9672{
b4d90040 9673 struct exec_catchpoint *c;
a6d9a66e 9674 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9675 int tempflag;
c5aa993b 9676 char *cond_string = NULL;
c906108c 9677
44feb3ce
TT
9678 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9679
9680 if (!arg)
9681 arg = "";
e9cafbcc 9682 arg = skip_spaces (arg);
c906108c
SS
9683
9684 /* The allowed syntax is:
c5aa993b
JM
9685 catch exec
9686 catch exec if <cond>
c906108c 9687
4a64f543 9688 First, check if there's an if clause. */
c906108c
SS
9689 cond_string = ep_parse_optional_if_clause (&arg);
9690
9691 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9692 error (_("Junk at end of arguments."));
c906108c 9693
b4d90040
PA
9694 c = XNEW (struct exec_catchpoint);
9695 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
9696 &catch_exec_breakpoint_ops);
9697 c->exec_pathname = NULL;
9698
3ea46bff 9699 install_breakpoint (0, &c->base, 1);
c906108c 9700}
c5aa993b 9701
3086aeae 9702static enum print_stop_action
348d480f 9703print_it_exception_catchpoint (bpstat bs)
3086aeae 9704{
79a45e25 9705 struct ui_out *uiout = current_uiout;
348d480f 9706 struct breakpoint *b = bs->breakpoint_at;
ade92717 9707 int bp_temp, bp_throw;
3086aeae 9708
ade92717 9709 annotate_catchpoint (b->number);
3086aeae 9710
ade92717
AR
9711 bp_throw = strstr (b->addr_string, "throw") != NULL;
9712 if (b->loc->address != b->loc->requested_address)
9713 breakpoint_adjustment_warning (b->loc->requested_address,
9714 b->loc->address,
9715 b->number, 1);
df2b6d2d 9716 bp_temp = b->disposition == disp_del;
ade92717
AR
9717 ui_out_text (uiout,
9718 bp_temp ? "Temporary catchpoint "
9719 : "Catchpoint ");
9720 if (!ui_out_is_mi_like_p (uiout))
9721 ui_out_field_int (uiout, "bkptno", b->number);
9722 ui_out_text (uiout,
c0b37c48
AR
9723 bp_throw ? " (exception thrown), "
9724 : " (exception caught), ");
ade92717
AR
9725 if (ui_out_is_mi_like_p (uiout))
9726 {
9727 ui_out_field_string (uiout, "reason",
9728 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9729 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9730 ui_out_field_int (uiout, "bkptno", b->number);
9731 }
3086aeae
DJ
9732 return PRINT_SRC_AND_LOC;
9733}
9734
9735static void
cc59ec59
MS
9736print_one_exception_catchpoint (struct breakpoint *b,
9737 struct bp_location **last_loc)
3086aeae 9738{
79a45b7d 9739 struct value_print_options opts;
79a45e25 9740 struct ui_out *uiout = current_uiout;
cc59ec59 9741
79a45b7d
TT
9742 get_user_print_options (&opts);
9743 if (opts.addressprint)
3086aeae
DJ
9744 {
9745 annotate_field (4);
604133b5
AR
9746 if (b->loc == NULL || b->loc->shlib_disabled)
9747 ui_out_field_string (uiout, "addr", "<PENDING>");
9748 else
5af949e3
UW
9749 ui_out_field_core_addr (uiout, "addr",
9750 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9751 }
9752 annotate_field (5);
604133b5 9753 if (b->loc)
a6d9a66e 9754 *last_loc = b->loc;
3086aeae
DJ
9755 if (strstr (b->addr_string, "throw") != NULL)
9756 ui_out_field_string (uiout, "what", "exception throw");
9757 else
9758 ui_out_field_string (uiout, "what", "exception catch");
9759}
9760
9761static void
9762print_mention_exception_catchpoint (struct breakpoint *b)
9763{
79a45e25 9764 struct ui_out *uiout = current_uiout;
ade92717
AR
9765 int bp_temp;
9766 int bp_throw;
9767
df2b6d2d 9768 bp_temp = b->disposition == disp_del;
ade92717
AR
9769 bp_throw = strstr (b->addr_string, "throw") != NULL;
9770 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9771 : _("Catchpoint "));
9772 ui_out_field_int (uiout, "bkptno", b->number);
9773 ui_out_text (uiout, bp_throw ? _(" (throw)")
9774 : _(" (catch)"));
3086aeae
DJ
9775}
9776
6149aea9
PA
9777/* Implement the "print_recreate" breakpoint_ops method for throw and
9778 catch catchpoints. */
9779
9780static void
4a64f543
MS
9781print_recreate_exception_catchpoint (struct breakpoint *b,
9782 struct ui_file *fp)
6149aea9
PA
9783{
9784 int bp_temp;
9785 int bp_throw;
9786
9787 bp_temp = b->disposition == disp_del;
9788 bp_throw = strstr (b->addr_string, "throw") != NULL;
9789 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9790 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 9791 print_recreate_thread (b, fp);
6149aea9
PA
9792}
9793
2060206e 9794static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
9795
9796static int
9797handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9798 enum exception_event_kind ex_event, int from_tty)
9799{
604133b5
AR
9800 char *trigger_func_name;
9801
3086aeae 9802 if (ex_event == EX_EVENT_CATCH)
604133b5 9803 trigger_func_name = "__cxa_begin_catch";
3086aeae 9804 else
604133b5 9805 trigger_func_name = "__cxa_throw";
3086aeae 9806
8cdf0e15
VP
9807 create_breakpoint (get_current_arch (),
9808 trigger_func_name, cond_string, -1,
9809 0 /* condition and thread are valid. */,
0fb4aa4b 9810 tempflag, bp_breakpoint,
8cdf0e15
VP
9811 0,
9812 AUTO_BOOLEAN_TRUE /* pending */,
9813 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9814 1 /* enabled */,
9815 0 /* internal */);
3086aeae 9816
3086aeae
DJ
9817 return 1;
9818}
9819
4a64f543 9820/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9821
9822static void
fba45db2
KB
9823catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9824 int tempflag, int from_tty)
c906108c 9825{
c5aa993b 9826 char *cond_string = NULL;
c5aa993b 9827
44feb3ce
TT
9828 if (!arg)
9829 arg = "";
e9cafbcc 9830 arg = skip_spaces (arg);
c5aa993b 9831
c906108c
SS
9832 cond_string = ep_parse_optional_if_clause (&arg);
9833
9834 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9835 error (_("Junk at end of arguments."));
c906108c 9836
059fb39f
PM
9837 if (ex_event != EX_EVENT_THROW
9838 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9839 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9840
3086aeae
DJ
9841 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9842 return;
9843
8a3fe4f8 9844 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9845}
9846
44feb3ce
TT
9847/* Implementation of "catch catch" command. */
9848
9849static void
9850catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9851{
9852 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9853
44feb3ce
TT
9854 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9855}
9856
9857/* Implementation of "catch throw" command. */
9858
9859static void
9860catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9861{
9862 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9863
44feb3ce
TT
9864 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9865}
9866
9ac4176b 9867void
28010a5d
PA
9868init_ada_exception_breakpoint (struct breakpoint *b,
9869 struct gdbarch *gdbarch,
9870 struct symtab_and_line sal,
9871 char *addr_string,
c0a91b2b 9872 const struct breakpoint_ops *ops,
28010a5d
PA
9873 int tempflag,
9874 int from_tty)
f7f9143b 9875{
f7f9143b
JB
9876 if (from_tty)
9877 {
5af949e3
UW
9878 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9879 if (!loc_gdbarch)
9880 loc_gdbarch = gdbarch;
9881
6c95b8df
PA
9882 describe_other_breakpoints (loc_gdbarch,
9883 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9884 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9885 version for exception catchpoints, because two catchpoints
9886 used for different exception names will use the same address.
9887 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9888 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 9889 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
9890 the user what type of catchpoint it is. The above is good
9891 enough for now, though. */
9892 }
9893
28010a5d 9894 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
9895
9896 b->enable_state = bp_enabled;
9897 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
9898 b->addr_string = addr_string;
9899 b->language = language_ada;
f7f9143b
JB
9900}
9901
a96d9b2e
SDJ
9902/* Splits the argument using space as delimiter. Returns an xmalloc'd
9903 filter list, or NULL if no filtering is required. */
9904static VEC(int) *
9905catch_syscall_split_args (char *arg)
9906{
9907 VEC(int) *result = NULL;
29d0bb3d 9908 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
9909
9910 while (*arg != '\0')
9911 {
9912 int i, syscall_number;
9913 char *endptr;
9914 char cur_name[128];
9915 struct syscall s;
9916
9917 /* Skip whitespace. */
9918 while (isspace (*arg))
9919 arg++;
9920
9921 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9922 cur_name[i] = arg[i];
9923 cur_name[i] = '\0';
9924 arg += i;
9925
9926 /* Check if the user provided a syscall name or a number. */
9927 syscall_number = (int) strtol (cur_name, &endptr, 0);
9928 if (*endptr == '\0')
bccd0dd2 9929 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9930 else
9931 {
9932 /* We have a name. Let's check if it's valid and convert it
9933 to a number. */
9934 get_syscall_by_name (cur_name, &s);
9935
9936 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
9937 /* Here we have to issue an error instead of a warning,
9938 because GDB cannot do anything useful if there's no
9939 syscall number to be caught. */
a96d9b2e
SDJ
9940 error (_("Unknown syscall name '%s'."), cur_name);
9941 }
9942
9943 /* Ok, it's valid. */
9944 VEC_safe_push (int, result, s.number);
9945 }
9946
9947 discard_cleanups (cleanup);
9948 return result;
9949}
9950
9951/* Implement the "catch syscall" command. */
9952
9953static void
cc59ec59
MS
9954catch_syscall_command_1 (char *arg, int from_tty,
9955 struct cmd_list_element *command)
a96d9b2e
SDJ
9956{
9957 int tempflag;
9958 VEC(int) *filter;
9959 struct syscall s;
9960 struct gdbarch *gdbarch = get_current_arch ();
9961
9962 /* Checking if the feature if supported. */
9963 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9964 error (_("The feature 'catch syscall' is not supported on \
ea666128 9965this architecture yet."));
a96d9b2e
SDJ
9966
9967 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9968
e9cafbcc 9969 arg = skip_spaces (arg);
a96d9b2e
SDJ
9970
9971 /* We need to do this first "dummy" translation in order
9972 to get the syscall XML file loaded or, most important,
9973 to display a warning to the user if there's no XML file
9974 for his/her architecture. */
9975 get_syscall_by_number (0, &s);
9976
9977 /* The allowed syntax is:
9978 catch syscall
9979 catch syscall <name | number> [<name | number> ... <name | number>]
9980
9981 Let's check if there's a syscall name. */
9982
9983 if (arg != NULL)
9984 filter = catch_syscall_split_args (arg);
9985 else
9986 filter = NULL;
9987
9988 create_syscall_event_catchpoint (tempflag, filter,
9989 &catch_syscall_breakpoint_ops);
9990}
9991
c906108c 9992static void
fba45db2 9993catch_command (char *arg, int from_tty)
c906108c 9994{
44feb3ce 9995 error (_("Catch requires an event name."));
c906108c
SS
9996}
9997\f
9998
9999static void
fba45db2 10000tcatch_command (char *arg, int from_tty)
c906108c 10001{
44feb3ce 10002 error (_("Catch requires an event name."));
c906108c
SS
10003}
10004
80f8a6eb 10005/* Delete breakpoints by address or line. */
c906108c
SS
10006
10007static void
fba45db2 10008clear_command (char *arg, int from_tty)
c906108c 10009{
d6e956e5
VP
10010 struct breakpoint *b;
10011 VEC(breakpoint_p) *found = 0;
10012 int ix;
c906108c
SS
10013 int default_match;
10014 struct symtabs_and_lines sals;
10015 struct symtab_and_line sal;
c906108c
SS
10016 int i;
10017
10018 if (arg)
10019 {
f8eba3c6
TT
10020 sals = decode_line_spec (arg, (DECODE_LINE_FUNFIRSTLINE
10021 | DECODE_LINE_LIST_MODE));
c906108c
SS
10022 default_match = 0;
10023 }
10024 else
10025 {
c5aa993b 10026 sals.sals = (struct symtab_and_line *)
c906108c 10027 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 10028 make_cleanup (xfree, sals.sals);
4a64f543 10029 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
10030
10031 /* Set sal's line, symtab, pc, and pspace to the values
10032 corresponding to the last call to print_frame_info. If the
10033 codepoint is not valid, this will set all the fields to 0. */
10034 get_last_displayed_sal (&sal);
c906108c 10035 if (sal.symtab == 0)
8a3fe4f8 10036 error (_("No source file specified."));
c906108c
SS
10037
10038 sals.sals[0] = sal;
10039 sals.nelts = 1;
10040
10041 default_match = 1;
10042 }
10043
4a64f543
MS
10044 /* We don't call resolve_sal_pc here. That's not as bad as it
10045 seems, because all existing breakpoints typically have both
10046 file/line and pc set. So, if clear is given file/line, we can
10047 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10048
10049 We only support clearing given the address explicitly
10050 present in breakpoint table. Say, we've set breakpoint
4a64f543 10051 at file:line. There were several PC values for that file:line,
ed0616c6 10052 due to optimization, all in one block.
4a64f543
MS
10053
10054 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10055 PC corresponding to the same file:line, the breakpoint won't
10056 be cleared. We probably can still clear the breakpoint, but
10057 since the other PC value is never presented to user, user
10058 can only find it by guessing, and it does not seem important
10059 to support that. */
10060
4a64f543
MS
10061 /* For each line spec given, delete bps which correspond to it. Do
10062 it in two passes, solely to preserve the current behavior that
10063 from_tty is forced true if we delete more than one
10064 breakpoint. */
c906108c 10065
80f8a6eb 10066 found = NULL;
c906108c
SS
10067 for (i = 0; i < sals.nelts; i++)
10068 {
10069 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
10070 If line given (pc == 0), clear all bpts on specified line.
10071 If defaulting, clear all bpts on default line
c906108c 10072 or at default pc.
c5aa993b
JM
10073
10074 defaulting sal.pc != 0 tests to do
10075
10076 0 1 pc
10077 1 1 pc _and_ line
10078 0 0 line
10079 1 0 <can't happen> */
c906108c
SS
10080
10081 sal = sals.sals[i];
c906108c 10082
4a64f543 10083 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 10084 ALL_BREAKPOINTS (b)
c5aa993b 10085 {
0d381245 10086 int match = 0;
4a64f543 10087 /* Are we going to delete b? */
cc60f2e3 10088 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
10089 {
10090 struct bp_location *loc = b->loc;
10091 for (; loc; loc = loc->next)
10092 {
f8eba3c6
TT
10093 /* If the user specified file:line, don't allow a PC
10094 match. This matches historical gdb behavior. */
10095 int pc_match = (!sal.explicit_line
10096 && sal.pc
10097 && (loc->pspace == sal.pspace)
10098 && (loc->address == sal.pc)
10099 && (!section_is_overlay (loc->section)
10100 || loc->section == sal.section));
10101 int line_match = ((default_match || sal.explicit_line)
10102 && loc->source_file != NULL
0d381245 10103 && sal.symtab != NULL
6c95b8df 10104 && sal.pspace == loc->pspace
f8eba3c6 10105 && filename_cmp (loc->source_file,
0ba1096a 10106 sal.symtab->filename) == 0
f8eba3c6 10107 && loc->line_number == sal.line);
0d381245
VP
10108 if (pc_match || line_match)
10109 {
10110 match = 1;
10111 break;
10112 }
10113 }
10114 }
10115
10116 if (match)
d6e956e5 10117 VEC_safe_push(breakpoint_p, found, b);
c906108c 10118 }
80f8a6eb
MS
10119 }
10120 /* Now go thru the 'found' chain and delete them. */
d6e956e5 10121 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
10122 {
10123 if (arg)
8a3fe4f8 10124 error (_("No breakpoint at %s."), arg);
80f8a6eb 10125 else
8a3fe4f8 10126 error (_("No breakpoint at this line."));
80f8a6eb 10127 }
c906108c 10128
d6e956e5 10129 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 10130 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10131 if (from_tty)
a3f17187 10132 {
d6e956e5 10133 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
10134 printf_unfiltered (_("Deleted breakpoint "));
10135 else
10136 printf_unfiltered (_("Deleted breakpoints "));
10137 }
80f8a6eb 10138 breakpoints_changed ();
d6e956e5
VP
10139
10140 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 10141 {
c5aa993b 10142 if (from_tty)
d6e956e5
VP
10143 printf_unfiltered ("%d ", b->number);
10144 delete_breakpoint (b);
c906108c 10145 }
80f8a6eb
MS
10146 if (from_tty)
10147 putchar_unfiltered ('\n');
c906108c
SS
10148}
10149\f
10150/* Delete breakpoint in BS if they are `delete' breakpoints and
10151 all breakpoints that are marked for deletion, whether hit or not.
10152 This is called after any breakpoint is hit, or after errors. */
10153
10154void
fba45db2 10155breakpoint_auto_delete (bpstat bs)
c906108c 10156{
35df4500 10157 struct breakpoint *b, *b_tmp;
c906108c
SS
10158
10159 for (; bs; bs = bs->next)
f431efe5
PA
10160 if (bs->breakpoint_at
10161 && bs->breakpoint_at->disposition == disp_del
c906108c 10162 && bs->stop)
f431efe5 10163 delete_breakpoint (bs->breakpoint_at);
c906108c 10164
35df4500 10165 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10166 {
b5de0fa7 10167 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10168 delete_breakpoint (b);
10169 }
c906108c
SS
10170}
10171
4a64f543
MS
10172/* A comparison function for bp_location AP and BP being interfaced to
10173 qsort. Sort elements primarily by their ADDRESS (no matter what
10174 does breakpoint_address_is_meaningful say for its OWNER),
10175 secondarily by ordering first bp_permanent OWNERed elements and
10176 terciarily just ensuring the array is sorted stable way despite
e5dd4106 10177 qsort being an unstable algorithm. */
876fa593
JK
10178
10179static int
494cfb0f 10180bp_location_compare (const void *ap, const void *bp)
876fa593 10181{
494cfb0f
JK
10182 struct bp_location *a = *(void **) ap;
10183 struct bp_location *b = *(void **) bp;
2bdf28a0 10184 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10185 int a_perm = a->owner->enable_state == bp_permanent;
10186 int b_perm = b->owner->enable_state == bp_permanent;
10187
10188 if (a->address != b->address)
10189 return (a->address > b->address) - (a->address < b->address);
10190
10191 /* Sort permanent breakpoints first. */
10192 if (a_perm != b_perm)
10193 return (a_perm < b_perm) - (a_perm > b_perm);
10194
4a64f543
MS
10195 /* Make the user-visible order stable across GDB runs. Locations of
10196 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10197
10198 if (a->owner->number != b->owner->number)
10199 return (a->owner->number > b->owner->number)
10200 - (a->owner->number < b->owner->number);
10201
10202 return (a > b) - (a < b);
10203}
10204
876fa593 10205/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10206 bp_location_shadow_len_after_address_max according to the current
10207 content of the bp_location array. */
f7545552
TT
10208
10209static void
876fa593 10210bp_location_target_extensions_update (void)
f7545552 10211{
876fa593
JK
10212 struct bp_location *bl, **blp_tmp;
10213
10214 bp_location_placed_address_before_address_max = 0;
10215 bp_location_shadow_len_after_address_max = 0;
10216
10217 ALL_BP_LOCATIONS (bl, blp_tmp)
10218 {
10219 CORE_ADDR start, end, addr;
10220
10221 if (!bp_location_has_shadow (bl))
10222 continue;
10223
10224 start = bl->target_info.placed_address;
10225 end = start + bl->target_info.shadow_len;
10226
10227 gdb_assert (bl->address >= start);
10228 addr = bl->address - start;
10229 if (addr > bp_location_placed_address_before_address_max)
10230 bp_location_placed_address_before_address_max = addr;
10231
10232 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10233
10234 gdb_assert (bl->address < end);
10235 addr = end - bl->address;
10236 if (addr > bp_location_shadow_len_after_address_max)
10237 bp_location_shadow_len_after_address_max = addr;
10238 }
f7545552
TT
10239}
10240
1e4d1764
YQ
10241/* Download tracepoint locations if they haven't been. */
10242
10243static void
10244download_tracepoint_locations (void)
10245{
10246 struct bp_location *bl, **blp_tmp;
10247 struct cleanup *old_chain;
10248
10249 if (!target_can_download_tracepoint ())
10250 return;
10251
10252 old_chain = save_current_space_and_thread ();
10253
10254 ALL_BP_LOCATIONS (bl, blp_tmp)
10255 {
10256 struct tracepoint *t;
10257
10258 if (!is_tracepoint (bl->owner))
10259 continue;
10260
10261 if ((bl->owner->type == bp_fast_tracepoint
10262 ? !may_insert_fast_tracepoints
10263 : !may_insert_tracepoints))
10264 continue;
10265
10266 /* In tracepoint, locations are _never_ duplicated, so
10267 should_be_inserted is equivalent to
10268 unduplicated_should_be_inserted. */
10269 if (!should_be_inserted (bl) || bl->inserted)
10270 continue;
10271
10272 switch_to_program_space_and_thread (bl->pspace);
10273
10274 target_download_tracepoint (bl);
10275
10276 bl->inserted = 1;
10277 t = (struct tracepoint *) bl->owner;
10278 t->number_on_target = bl->owner->number;
10279 }
10280
10281 do_cleanups (old_chain);
10282}
10283
934709f0
PW
10284/* Swap the insertion/duplication state between two locations. */
10285
10286static void
10287swap_insertion (struct bp_location *left, struct bp_location *right)
10288{
10289 const int left_inserted = left->inserted;
10290 const int left_duplicate = left->duplicate;
10291 const struct bp_target_info left_target_info = left->target_info;
10292
1e4d1764
YQ
10293 /* Locations of tracepoints can never be duplicated. */
10294 if (is_tracepoint (left->owner))
10295 gdb_assert (!left->duplicate);
10296 if (is_tracepoint (right->owner))
10297 gdb_assert (!right->duplicate);
10298
934709f0
PW
10299 left->inserted = right->inserted;
10300 left->duplicate = right->duplicate;
10301 left->target_info = right->target_info;
10302 right->inserted = left_inserted;
10303 right->duplicate = left_duplicate;
10304 right->target_info = left_target_info;
10305}
10306
4cd9bd08 10307/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10308 into the inferior, only remove already-inserted locations that no
10309 longer should be inserted. Functions that delete a breakpoint or
10310 breakpoints should pass false, so that deleting a breakpoint
10311 doesn't have the side effect of inserting the locations of other
10312 breakpoints that are marked not-inserted, but should_be_inserted
10313 returns true on them.
10314
10315 This behaviour is useful is situations close to tear-down -- e.g.,
10316 after an exec, while the target still has execution, but breakpoint
10317 shadows of the previous executable image should *NOT* be restored
10318 to the new image; or before detaching, where the target still has
10319 execution and wants to delete breakpoints from GDB's lists, and all
10320 breakpoints had already been removed from the inferior. */
10321
0d381245 10322static void
b60e7edf 10323update_global_location_list (int should_insert)
0d381245 10324{
74960c60 10325 struct breakpoint *b;
876fa593 10326 struct bp_location **locp, *loc;
f7545552
TT
10327 struct cleanup *cleanups;
10328
2d134ed3
PA
10329 /* Used in the duplicates detection below. When iterating over all
10330 bp_locations, points to the first bp_location of a given address.
10331 Breakpoints and watchpoints of different types are never
10332 duplicates of each other. Keep one pointer for each type of
10333 breakpoint/watchpoint, so we only need to loop over all locations
10334 once. */
10335 struct bp_location *bp_loc_first; /* breakpoint */
10336 struct bp_location *wp_loc_first; /* hardware watchpoint */
10337 struct bp_location *awp_loc_first; /* access watchpoint */
10338 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10339
4a64f543
MS
10340 /* Saved former bp_location array which we compare against the newly
10341 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10342 struct bp_location **old_location, **old_locp;
10343 unsigned old_location_count;
10344
10345 old_location = bp_location;
10346 old_location_count = bp_location_count;
10347 bp_location = NULL;
10348 bp_location_count = 0;
10349 cleanups = make_cleanup (xfree, old_location);
0d381245 10350
74960c60 10351 ALL_BREAKPOINTS (b)
876fa593
JK
10352 for (loc = b->loc; loc; loc = loc->next)
10353 bp_location_count++;
10354
10355 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10356 locp = bp_location;
10357 ALL_BREAKPOINTS (b)
10358 for (loc = b->loc; loc; loc = loc->next)
10359 *locp++ = loc;
10360 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10361 bp_location_compare);
876fa593
JK
10362
10363 bp_location_target_extensions_update ();
74960c60 10364
4a64f543
MS
10365 /* Identify bp_location instances that are no longer present in the
10366 new list, and therefore should be freed. Note that it's not
10367 necessary that those locations should be removed from inferior --
10368 if there's another location at the same address (previously
10369 marked as duplicate), we don't need to remove/insert the
10370 location.
876fa593 10371
4a64f543
MS
10372 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10373 and former bp_location array state respectively. */
876fa593
JK
10374
10375 locp = bp_location;
10376 for (old_locp = old_location; old_locp < old_location + old_location_count;
10377 old_locp++)
74960c60 10378 {
876fa593 10379 struct bp_location *old_loc = *old_locp;
c7d46a38 10380 struct bp_location **loc2p;
876fa593 10381
e5dd4106 10382 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 10383 not, we have to free it. */
c7d46a38 10384 int found_object = 0;
20874c92
VP
10385 /* Tells if the location should remain inserted in the target. */
10386 int keep_in_target = 0;
10387 int removed = 0;
876fa593 10388
4a64f543
MS
10389 /* Skip LOCP entries which will definitely never be needed.
10390 Stop either at or being the one matching OLD_LOC. */
876fa593 10391 while (locp < bp_location + bp_location_count
c7d46a38 10392 && (*locp)->address < old_loc->address)
876fa593 10393 locp++;
c7d46a38
PA
10394
10395 for (loc2p = locp;
10396 (loc2p < bp_location + bp_location_count
10397 && (*loc2p)->address == old_loc->address);
10398 loc2p++)
10399 {
10400 if (*loc2p == old_loc)
10401 {
10402 found_object = 1;
10403 break;
10404 }
10405 }
74960c60 10406
4a64f543
MS
10407 /* If this location is no longer present, and inserted, look if
10408 there's maybe a new location at the same address. If so,
10409 mark that one inserted, and don't remove this one. This is
10410 needed so that we don't have a time window where a breakpoint
10411 at certain location is not inserted. */
74960c60 10412
876fa593 10413 if (old_loc->inserted)
0d381245 10414 {
4a64f543
MS
10415 /* If the location is inserted now, we might have to remove
10416 it. */
74960c60 10417
876fa593 10418 if (found_object && should_be_inserted (old_loc))
74960c60 10419 {
4a64f543
MS
10420 /* The location is still present in the location list,
10421 and still should be inserted. Don't do anything. */
20874c92 10422 keep_in_target = 1;
74960c60
VP
10423 }
10424 else
10425 {
4a64f543
MS
10426 /* The location is either no longer present, or got
10427 disabled. See if there's another location at the
10428 same address, in which case we don't need to remove
10429 this one from the target. */
876fa593 10430
2bdf28a0 10431 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10432 if (breakpoint_address_is_meaningful (old_loc->owner))
10433 {
876fa593 10434 for (loc2p = locp;
c7d46a38
PA
10435 (loc2p < bp_location + bp_location_count
10436 && (*loc2p)->address == old_loc->address);
876fa593
JK
10437 loc2p++)
10438 {
10439 struct bp_location *loc2 = *loc2p;
10440
2d134ed3 10441 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 10442 {
85d721b8
PA
10443 /* Read watchpoint locations are switched to
10444 access watchpoints, if the former are not
10445 supported, but the latter are. */
10446 if (is_hardware_watchpoint (old_loc->owner))
10447 {
10448 gdb_assert (is_hardware_watchpoint (loc2->owner));
10449 loc2->watchpoint_type = old_loc->watchpoint_type;
10450 }
10451
934709f0
PW
10452 /* loc2 is a duplicated location. We need to check
10453 if it should be inserted in case it will be
10454 unduplicated. */
10455 if (loc2 != old_loc
10456 && unduplicated_should_be_inserted (loc2))
c7d46a38 10457 {
934709f0 10458 swap_insertion (old_loc, loc2);
c7d46a38
PA
10459 keep_in_target = 1;
10460 break;
10461 }
876fa593
JK
10462 }
10463 }
10464 }
74960c60
VP
10465 }
10466
20874c92
VP
10467 if (!keep_in_target)
10468 {
876fa593 10469 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10470 {
4a64f543
MS
10471 /* This is just about all we can do. We could keep
10472 this location on the global list, and try to
10473 remove it next time, but there's no particular
10474 reason why we will succeed next time.
20874c92 10475
4a64f543
MS
10476 Note that at this point, old_loc->owner is still
10477 valid, as delete_breakpoint frees the breakpoint
10478 only after calling us. */
3e43a32a
MS
10479 printf_filtered (_("warning: Error removing "
10480 "breakpoint %d\n"),
876fa593 10481 old_loc->owner->number);
20874c92
VP
10482 }
10483 removed = 1;
10484 }
0d381245 10485 }
74960c60
VP
10486
10487 if (!found_object)
1c5cfe86 10488 {
db82e815
PA
10489 if (removed && non_stop
10490 && breakpoint_address_is_meaningful (old_loc->owner)
10491 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10492 {
db82e815
PA
10493 /* This location was removed from the target. In
10494 non-stop mode, a race condition is possible where
10495 we've removed a breakpoint, but stop events for that
10496 breakpoint are already queued and will arrive later.
10497 We apply an heuristic to be able to distinguish such
10498 SIGTRAPs from other random SIGTRAPs: we keep this
10499 breakpoint location for a bit, and will retire it
10500 after we see some number of events. The theory here
10501 is that reporting of events should, "on the average",
10502 be fair, so after a while we'll see events from all
10503 threads that have anything of interest, and no longer
10504 need to keep this breakpoint location around. We
10505 don't hold locations forever so to reduce chances of
10506 mistaking a non-breakpoint SIGTRAP for a breakpoint
10507 SIGTRAP.
10508
10509 The heuristic failing can be disastrous on
10510 decr_pc_after_break targets.
10511
10512 On decr_pc_after_break targets, like e.g., x86-linux,
10513 if we fail to recognize a late breakpoint SIGTRAP,
10514 because events_till_retirement has reached 0 too
10515 soon, we'll fail to do the PC adjustment, and report
10516 a random SIGTRAP to the user. When the user resumes
10517 the inferior, it will most likely immediately crash
2dec564e 10518 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10519 corrupted, because of being resumed e.g., in the
10520 middle of a multi-byte instruction, or skipped a
10521 one-byte instruction. This was actually seen happen
10522 on native x86-linux, and should be less rare on
10523 targets that do not support new thread events, like
10524 remote, due to the heuristic depending on
10525 thread_count.
10526
10527 Mistaking a random SIGTRAP for a breakpoint trap
10528 causes similar symptoms (PC adjustment applied when
10529 it shouldn't), but then again, playing with SIGTRAPs
10530 behind the debugger's back is asking for trouble.
10531
10532 Since hardware watchpoint traps are always
10533 distinguishable from other traps, so we don't need to
10534 apply keep hardware watchpoint moribund locations
10535 around. We simply always ignore hardware watchpoint
10536 traps we can no longer explain. */
10537
876fa593
JK
10538 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10539 old_loc->owner = NULL;
20874c92 10540
876fa593 10541 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10542 }
10543 else
f431efe5
PA
10544 {
10545 old_loc->owner = NULL;
10546 decref_bp_location (&old_loc);
10547 }
20874c92 10548 }
74960c60 10549 }
1c5cfe86 10550
348d480f
PA
10551 /* Rescan breakpoints at the same address and section, marking the
10552 first one as "first" and any others as "duplicates". This is so
10553 that the bpt instruction is only inserted once. If we have a
10554 permanent breakpoint at the same place as BPT, make that one the
10555 official one, and the rest as duplicates. Permanent breakpoints
10556 are sorted first for the same address.
10557
10558 Do the same for hardware watchpoints, but also considering the
10559 watchpoint's type (regular/access/read) and length. */
10560
10561 bp_loc_first = NULL;
10562 wp_loc_first = NULL;
10563 awp_loc_first = NULL;
10564 rwp_loc_first = NULL;
10565 ALL_BP_LOCATIONS (loc, locp)
10566 {
10567 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10568 non-NULL. */
10569 struct breakpoint *b = loc->owner;
10570 struct bp_location **loc_first_p;
10571
f8eba3c6 10572 if (!should_be_inserted (loc)
348d480f 10573 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
10574 /* Don't detect duplicate for tracepoint locations because they are
10575 never duplicated. See the comments in field `duplicate' of
10576 `struct bp_location'. */
348d480f
PA
10577 || is_tracepoint (b))
10578 continue;
10579
10580 /* Permanent breakpoint should always be inserted. */
10581 if (b->enable_state == bp_permanent && ! loc->inserted)
10582 internal_error (__FILE__, __LINE__,
10583 _("allegedly permanent breakpoint is not "
10584 "actually inserted"));
10585
10586 if (b->type == bp_hardware_watchpoint)
10587 loc_first_p = &wp_loc_first;
10588 else if (b->type == bp_read_watchpoint)
10589 loc_first_p = &rwp_loc_first;
10590 else if (b->type == bp_access_watchpoint)
10591 loc_first_p = &awp_loc_first;
10592 else
10593 loc_first_p = &bp_loc_first;
10594
10595 if (*loc_first_p == NULL
10596 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10597 || !breakpoint_locations_match (loc, *loc_first_p))
10598 {
10599 *loc_first_p = loc;
10600 loc->duplicate = 0;
10601 continue;
10602 }
10603
934709f0
PW
10604
10605 /* This and the above ensure the invariant that the first location
10606 is not duplicated, and is the inserted one.
10607 All following are marked as duplicated, and are not inserted. */
10608 if (loc->inserted)
10609 swap_insertion (loc, *loc_first_p);
348d480f
PA
10610 loc->duplicate = 1;
10611
10612 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10613 && b->enable_state != bp_permanent)
10614 internal_error (__FILE__, __LINE__,
10615 _("another breakpoint was inserted on top of "
10616 "a permanent breakpoint"));
10617 }
10618
10619 if (breakpoints_always_inserted_mode () && should_insert
10620 && (have_live_inferiors ()
10621 || (gdbarch_has_global_breakpoints (target_gdbarch))))
10622 insert_breakpoint_locations ();
10623
1e4d1764
YQ
10624 if (should_insert)
10625 download_tracepoint_locations ();
10626
348d480f
PA
10627 do_cleanups (cleanups);
10628}
10629
10630void
10631breakpoint_retire_moribund (void)
10632{
10633 struct bp_location *loc;
10634 int ix;
10635
10636 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10637 if (--(loc->events_till_retirement) == 0)
10638 {
10639 decref_bp_location (&loc);
10640 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10641 --ix;
10642 }
10643}
10644
10645static void
10646update_global_location_list_nothrow (int inserting)
10647{
10648 struct gdb_exception e;
10649
10650 TRY_CATCH (e, RETURN_MASK_ERROR)
10651 update_global_location_list (inserting);
10652}
10653
10654/* Clear BKP from a BPS. */
10655
10656static void
10657bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10658{
10659 bpstat bs;
10660
10661 for (bs = bps; bs; bs = bs->next)
10662 if (bs->breakpoint_at == bpt)
10663 {
10664 bs->breakpoint_at = NULL;
10665 bs->old_val = NULL;
10666 /* bs->commands will be freed later. */
10667 }
10668}
10669
10670/* Callback for iterate_over_threads. */
10671static int
10672bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10673{
10674 struct breakpoint *bpt = data;
10675
10676 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10677 return 0;
10678}
10679
10680/* Helper for breakpoint and tracepoint breakpoint_ops->mention
10681 callbacks. */
10682
10683static void
10684say_where (struct breakpoint *b)
10685{
79a45e25 10686 struct ui_out *uiout = current_uiout;
348d480f
PA
10687 struct value_print_options opts;
10688
10689 get_user_print_options (&opts);
10690
10691 /* i18n: cagney/2005-02-11: Below needs to be merged into a
10692 single string. */
10693 if (b->loc == NULL)
10694 {
10695 printf_filtered (_(" (%s) pending."), b->addr_string);
10696 }
10697 else
10698 {
f8eba3c6 10699 if (opts.addressprint || b->loc->source_file == NULL)
348d480f
PA
10700 {
10701 printf_filtered (" at ");
10702 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
10703 gdb_stdout);
10704 }
f8eba3c6
TT
10705 if (b->loc->source_file)
10706 {
10707 /* If there is a single location, we can print the location
10708 more nicely. */
10709 if (b->loc->next == NULL)
10710 printf_filtered (": file %s, line %d.",
10711 b->loc->source_file, b->loc->line_number);
10712 else
10713 /* This is not ideal, but each location may have a
10714 different file name, and this at least reflects the
10715 real situation somewhat. */
10716 printf_filtered (": %s.", b->addr_string);
10717 }
348d480f
PA
10718
10719 if (b->loc->next)
10720 {
10721 struct bp_location *loc = b->loc;
10722 int n = 0;
10723 for (; loc; loc = loc->next)
10724 ++n;
10725 printf_filtered (" (%d locations)", n);
10726 }
10727 }
10728}
10729
348d480f
PA
10730/* Default bp_location_ops methods. */
10731
10732static void
10733bp_location_dtor (struct bp_location *self)
10734{
10735 xfree (self->cond);
10736 xfree (self->function_name);
f8eba3c6 10737 xfree (self->source_file);
348d480f
PA
10738}
10739
10740static const struct bp_location_ops bp_location_ops =
10741{
10742 bp_location_dtor
10743};
10744
2060206e
PA
10745/* Default breakpoint_ops methods all breakpoint_ops ultimately
10746 inherit from. */
348d480f 10747
2060206e
PA
10748static void
10749base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
10750{
10751 decref_counted_command_line (&self->commands);
10752 xfree (self->cond_string);
348d480f 10753 xfree (self->addr_string);
f8eba3c6 10754 xfree (self->filter);
348d480f 10755 xfree (self->addr_string_range_end);
348d480f
PA
10756}
10757
2060206e
PA
10758static struct bp_location *
10759base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
10760{
10761 struct bp_location *loc;
10762
10763 loc = XNEW (struct bp_location);
10764 init_bp_location (loc, &bp_location_ops, self);
10765 return loc;
10766}
10767
2060206e
PA
10768static void
10769base_breakpoint_re_set (struct breakpoint *b)
10770{
10771 /* Nothing to re-set. */
10772}
10773
10774#define internal_error_pure_virtual_called() \
10775 gdb_assert_not_reached ("pure virtual function called")
10776
10777static int
10778base_breakpoint_insert_location (struct bp_location *bl)
10779{
10780 internal_error_pure_virtual_called ();
10781}
10782
10783static int
10784base_breakpoint_remove_location (struct bp_location *bl)
10785{
10786 internal_error_pure_virtual_called ();
10787}
10788
10789static int
10790base_breakpoint_breakpoint_hit (const struct bp_location *bl,
10791 struct address_space *aspace,
10792 CORE_ADDR bp_addr)
10793{
10794 internal_error_pure_virtual_called ();
10795}
10796
10797static void
10798base_breakpoint_check_status (bpstat bs)
10799{
10800 /* Always stop. */
10801}
10802
10803/* A "works_in_software_mode" breakpoint_ops method that just internal
10804 errors. */
10805
10806static int
10807base_breakpoint_works_in_software_mode (const struct breakpoint *b)
10808{
10809 internal_error_pure_virtual_called ();
10810}
10811
10812/* A "resources_needed" breakpoint_ops method that just internal
10813 errors. */
10814
10815static int
10816base_breakpoint_resources_needed (const struct bp_location *bl)
10817{
10818 internal_error_pure_virtual_called ();
10819}
10820
10821static enum print_stop_action
10822base_breakpoint_print_it (bpstat bs)
10823{
10824 internal_error_pure_virtual_called ();
10825}
10826
10827static void
10828base_breakpoint_print_one_detail (const struct breakpoint *self,
10829 struct ui_out *uiout)
10830{
10831 /* nothing */
10832}
10833
10834static void
10835base_breakpoint_print_mention (struct breakpoint *b)
10836{
10837 internal_error_pure_virtual_called ();
10838}
10839
10840static void
10841base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
10842{
10843 internal_error_pure_virtual_called ();
10844}
10845
10846static struct breakpoint_ops base_breakpoint_ops =
10847{
10848 base_breakpoint_dtor,
10849 base_breakpoint_allocate_location,
10850 base_breakpoint_re_set,
10851 base_breakpoint_insert_location,
10852 base_breakpoint_remove_location,
10853 base_breakpoint_breakpoint_hit,
10854 base_breakpoint_check_status,
10855 base_breakpoint_resources_needed,
10856 base_breakpoint_works_in_software_mode,
10857 base_breakpoint_print_it,
10858 NULL,
10859 base_breakpoint_print_one_detail,
10860 base_breakpoint_print_mention,
10861 base_breakpoint_print_recreate
10862};
10863
10864/* Default breakpoint_ops methods. */
10865
10866static void
348d480f
PA
10867bkpt_re_set (struct breakpoint *b)
10868{
06edf0c0
PA
10869 /* FIXME: is this still reachable? */
10870 if (b->addr_string == NULL)
10871 {
10872 /* Anything without a string can't be re-set. */
348d480f 10873 delete_breakpoint (b);
06edf0c0 10874 return;
348d480f 10875 }
06edf0c0
PA
10876
10877 breakpoint_re_set_default (b);
348d480f
PA
10878}
10879
2060206e 10880static int
348d480f
PA
10881bkpt_insert_location (struct bp_location *bl)
10882{
10883 if (bl->loc_type == bp_loc_hardware_breakpoint)
10884 return target_insert_hw_breakpoint (bl->gdbarch,
10885 &bl->target_info);
10886 else
10887 return target_insert_breakpoint (bl->gdbarch,
10888 &bl->target_info);
10889}
10890
2060206e 10891static int
348d480f
PA
10892bkpt_remove_location (struct bp_location *bl)
10893{
10894 if (bl->loc_type == bp_loc_hardware_breakpoint)
10895 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
10896 else
10897 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
10898}
10899
2060206e 10900static int
348d480f
PA
10901bkpt_breakpoint_hit (const struct bp_location *bl,
10902 struct address_space *aspace, CORE_ADDR bp_addr)
10903{
10904 struct breakpoint *b = bl->owner;
10905
10906 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
10907 aspace, bp_addr))
10908 return 0;
10909
10910 if (overlay_debugging /* unmapped overlay section */
10911 && section_is_overlay (bl->section)
10912 && !section_is_mapped (bl->section))
10913 return 0;
10914
10915 return 1;
10916}
10917
2060206e 10918static int
348d480f
PA
10919bkpt_resources_needed (const struct bp_location *bl)
10920{
10921 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
10922
10923 return 1;
10924}
10925
2060206e 10926static enum print_stop_action
348d480f
PA
10927bkpt_print_it (bpstat bs)
10928{
348d480f
PA
10929 struct breakpoint *b;
10930 const struct bp_location *bl;
001c8c33 10931 int bp_temp;
79a45e25 10932 struct ui_out *uiout = current_uiout;
348d480f
PA
10933
10934 gdb_assert (bs->bp_location_at != NULL);
10935
10936 bl = bs->bp_location_at;
10937 b = bs->breakpoint_at;
10938
001c8c33
PA
10939 bp_temp = b->disposition == disp_del;
10940 if (bl->address != bl->requested_address)
10941 breakpoint_adjustment_warning (bl->requested_address,
10942 bl->address,
10943 b->number, 1);
10944 annotate_breakpoint (b->number);
10945 if (bp_temp)
10946 ui_out_text (uiout, "\nTemporary breakpoint ");
10947 else
10948 ui_out_text (uiout, "\nBreakpoint ");
10949 if (ui_out_is_mi_like_p (uiout))
348d480f 10950 {
001c8c33
PA
10951 ui_out_field_string (uiout, "reason",
10952 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10953 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 10954 }
001c8c33
PA
10955 ui_out_field_int (uiout, "bkptno", b->number);
10956 ui_out_text (uiout, ", ");
06edf0c0 10957
001c8c33 10958 return PRINT_SRC_AND_LOC;
06edf0c0
PA
10959}
10960
2060206e 10961static void
06edf0c0
PA
10962bkpt_print_mention (struct breakpoint *b)
10963{
79a45e25 10964 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
10965 return;
10966
10967 switch (b->type)
10968 {
10969 case bp_breakpoint:
10970 case bp_gnu_ifunc_resolver:
10971 if (b->disposition == disp_del)
10972 printf_filtered (_("Temporary breakpoint"));
10973 else
10974 printf_filtered (_("Breakpoint"));
10975 printf_filtered (_(" %d"), b->number);
10976 if (b->type == bp_gnu_ifunc_resolver)
10977 printf_filtered (_(" at gnu-indirect-function resolver"));
10978 break;
10979 case bp_hardware_breakpoint:
10980 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
10981 break;
10982 }
10983
10984 say_where (b);
10985}
10986
2060206e 10987static void
06edf0c0
PA
10988bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
10989{
10990 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
10991 fprintf_unfiltered (fp, "tbreak");
10992 else if (tp->type == bp_breakpoint)
10993 fprintf_unfiltered (fp, "break");
10994 else if (tp->type == bp_hardware_breakpoint
10995 && tp->disposition == disp_del)
10996 fprintf_unfiltered (fp, "thbreak");
10997 else if (tp->type == bp_hardware_breakpoint)
10998 fprintf_unfiltered (fp, "hbreak");
10999 else
11000 internal_error (__FILE__, __LINE__,
11001 _("unhandled breakpoint type %d"), (int) tp->type);
11002
2060206e 11003 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 11004 print_recreate_thread (tp, fp);
06edf0c0
PA
11005}
11006
06edf0c0
PA
11007/* Virtual table for internal breakpoints. */
11008
11009static void
11010internal_bkpt_re_set (struct breakpoint *b)
11011{
11012 switch (b->type)
11013 {
11014 /* Delete overlay event and longjmp master breakpoints; they
11015 will be reset later by breakpoint_re_set. */
11016 case bp_overlay_event:
11017 case bp_longjmp_master:
11018 case bp_std_terminate_master:
11019 case bp_exception_master:
11020 delete_breakpoint (b);
11021 break;
11022
11023 /* This breakpoint is special, it's set up when the inferior
11024 starts and we really don't want to touch it. */
11025 case bp_shlib_event:
11026
11027 /* Like bp_shlib_event, this breakpoint type is special. Once
11028 it is set up, we do not want to touch it. */
11029 case bp_thread_event:
11030 break;
11031 }
11032}
11033
11034static void
11035internal_bkpt_check_status (bpstat bs)
11036{
11037 /* We do not stop for these. */
11038 bs->stop = 0;
11039}
11040
11041static enum print_stop_action
11042internal_bkpt_print_it (bpstat bs)
11043{
36dfb11c 11044 struct ui_out *uiout = current_uiout;
06edf0c0 11045 struct breakpoint *b;
06edf0c0 11046
06edf0c0
PA
11047 b = bs->breakpoint_at;
11048
06edf0c0
PA
11049 switch (b->type)
11050 {
348d480f
PA
11051 case bp_shlib_event:
11052 /* Did we stop because the user set the stop_on_solib_events
11053 variable? (If so, we report this as a generic, "Stopped due
11054 to shlib event" message.) */
36dfb11c
TT
11055 ui_out_text (uiout, _("Stopped due to shared library event\n"));
11056 if (ui_out_is_mi_like_p (uiout))
11057 ui_out_field_string (uiout, "reason",
11058 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
348d480f
PA
11059 break;
11060
11061 case bp_thread_event:
11062 /* Not sure how we will get here.
11063 GDB should not stop for these breakpoints. */
11064 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11065 break;
11066
11067 case bp_overlay_event:
11068 /* By analogy with the thread event, GDB should not stop for these. */
11069 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11070 break;
11071
11072 case bp_longjmp_master:
11073 /* These should never be enabled. */
11074 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11075 break;
11076
11077 case bp_std_terminate_master:
11078 /* These should never be enabled. */
11079 printf_filtered (_("std::terminate Master Breakpoint: "
11080 "gdb should not stop!\n"));
348d480f
PA
11081 break;
11082
11083 case bp_exception_master:
11084 /* These should never be enabled. */
11085 printf_filtered (_("Exception Master Breakpoint: "
11086 "gdb should not stop!\n"));
06edf0c0
PA
11087 break;
11088 }
11089
001c8c33 11090 return PRINT_NOTHING;
06edf0c0
PA
11091}
11092
11093static void
11094internal_bkpt_print_mention (struct breakpoint *b)
11095{
11096 /* Nothing to mention. These breakpoints are internal. */
11097}
11098
06edf0c0
PA
11099/* Virtual table for momentary breakpoints */
11100
11101static void
11102momentary_bkpt_re_set (struct breakpoint *b)
11103{
11104 /* Keep temporary breakpoints, which can be encountered when we step
11105 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
11106 Otherwise these should have been blown away via the cleanup chain
11107 or by breakpoint_init_inferior when we rerun the executable. */
11108}
11109
11110static void
11111momentary_bkpt_check_status (bpstat bs)
11112{
11113 /* Nothing. The point of these breakpoints is causing a stop. */
11114}
11115
11116static enum print_stop_action
11117momentary_bkpt_print_it (bpstat bs)
11118{
79a45e25
PA
11119 struct ui_out *uiout = current_uiout;
11120
001c8c33 11121 if (ui_out_is_mi_like_p (uiout))
06edf0c0 11122 {
001c8c33 11123 struct breakpoint *b = bs->breakpoint_at;
348d480f 11124
001c8c33
PA
11125 switch (b->type)
11126 {
11127 case bp_finish:
11128 ui_out_field_string
11129 (uiout, "reason",
11130 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
11131 break;
348d480f 11132
001c8c33
PA
11133 case bp_until:
11134 ui_out_field_string
11135 (uiout, "reason",
11136 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
11137 break;
11138 }
348d480f
PA
11139 }
11140
001c8c33 11141 return PRINT_UNKNOWN;
348d480f
PA
11142}
11143
06edf0c0
PA
11144static void
11145momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 11146{
06edf0c0 11147 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11148}
11149
348d480f 11150/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 11151
348d480f
PA
11152static void
11153tracepoint_re_set (struct breakpoint *b)
11154{
11155 breakpoint_re_set_default (b);
11156}
876fa593 11157
348d480f
PA
11158static int
11159tracepoint_breakpoint_hit (const struct bp_location *bl,
11160 struct address_space *aspace, CORE_ADDR bp_addr)
11161{
11162 /* By definition, the inferior does not report stops at
11163 tracepoints. */
11164 return 0;
74960c60
VP
11165}
11166
11167static void
348d480f
PA
11168tracepoint_print_one_detail (const struct breakpoint *self,
11169 struct ui_out *uiout)
74960c60 11170{
d9b3f62e
PA
11171 struct tracepoint *tp = (struct tracepoint *) self;
11172 if (tp->static_trace_marker_id)
348d480f
PA
11173 {
11174 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 11175
348d480f
PA
11176 ui_out_text (uiout, "\tmarker id is ");
11177 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 11178 tp->static_trace_marker_id);
348d480f
PA
11179 ui_out_text (uiout, "\n");
11180 }
0d381245
VP
11181}
11182
a474d7c2 11183static void
348d480f 11184tracepoint_print_mention (struct breakpoint *b)
a474d7c2 11185{
79a45e25 11186 if (ui_out_is_mi_like_p (current_uiout))
348d480f 11187 return;
cc59ec59 11188
348d480f
PA
11189 switch (b->type)
11190 {
11191 case bp_tracepoint:
11192 printf_filtered (_("Tracepoint"));
11193 printf_filtered (_(" %d"), b->number);
11194 break;
11195 case bp_fast_tracepoint:
11196 printf_filtered (_("Fast tracepoint"));
11197 printf_filtered (_(" %d"), b->number);
11198 break;
11199 case bp_static_tracepoint:
11200 printf_filtered (_("Static tracepoint"));
11201 printf_filtered (_(" %d"), b->number);
11202 break;
11203 default:
11204 internal_error (__FILE__, __LINE__,
11205 _("unhandled tracepoint type %d"), (int) b->type);
11206 }
11207
11208 say_where (b);
a474d7c2
PA
11209}
11210
348d480f 11211static void
d9b3f62e 11212tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 11213{
d9b3f62e
PA
11214 struct tracepoint *tp = (struct tracepoint *) self;
11215
11216 if (self->type == bp_fast_tracepoint)
348d480f 11217 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 11218 if (self->type == bp_static_tracepoint)
348d480f 11219 fprintf_unfiltered (fp, "strace");
d9b3f62e 11220 else if (self->type == bp_tracepoint)
348d480f
PA
11221 fprintf_unfiltered (fp, "trace");
11222 else
11223 internal_error (__FILE__, __LINE__,
d9b3f62e 11224 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 11225
d9b3f62e
PA
11226 fprintf_unfiltered (fp, " %s", self->addr_string);
11227 print_recreate_thread (self, fp);
11228
11229 if (tp->pass_count)
11230 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
11231}
11232
2060206e 11233struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 11234
53a5351d 11235/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 11236 structures. */
c906108c
SS
11237
11238void
fba45db2 11239delete_breakpoint (struct breakpoint *bpt)
c906108c 11240{
52f0bd74 11241 struct breakpoint *b;
c906108c 11242
8a3fe4f8 11243 gdb_assert (bpt != NULL);
c906108c 11244
4a64f543
MS
11245 /* Has this bp already been deleted? This can happen because
11246 multiple lists can hold pointers to bp's. bpstat lists are
11247 especial culprits.
11248
11249 One example of this happening is a watchpoint's scope bp. When
11250 the scope bp triggers, we notice that the watchpoint is out of
11251 scope, and delete it. We also delete its scope bp. But the
11252 scope bp is marked "auto-deleting", and is already on a bpstat.
11253 That bpstat is then checked for auto-deleting bp's, which are
11254 deleted.
11255
11256 A real solution to this problem might involve reference counts in
11257 bp's, and/or giving them pointers back to their referencing
11258 bpstat's, and teaching delete_breakpoint to only free a bp's
11259 storage when no more references were extent. A cheaper bandaid
11260 was chosen. */
c906108c
SS
11261 if (bpt->type == bp_none)
11262 return;
11263
4a64f543
MS
11264 /* At least avoid this stale reference until the reference counting
11265 of breakpoints gets resolved. */
d0fb5eae 11266 if (bpt->related_breakpoint != bpt)
e5a0a904 11267 {
d0fb5eae 11268 struct breakpoint *related;
3a5c3e22 11269 struct watchpoint *w;
d0fb5eae
JK
11270
11271 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 11272 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 11273 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
11274 w = (struct watchpoint *) bpt;
11275 else
11276 w = NULL;
11277 if (w != NULL)
11278 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
11279
11280 /* Unlink bpt from the bpt->related_breakpoint ring. */
11281 for (related = bpt; related->related_breakpoint != bpt;
11282 related = related->related_breakpoint);
11283 related->related_breakpoint = bpt->related_breakpoint;
11284 bpt->related_breakpoint = bpt;
e5a0a904
JK
11285 }
11286
a9634178
TJB
11287 /* watch_command_1 creates a watchpoint but only sets its number if
11288 update_watchpoint succeeds in creating its bp_locations. If there's
11289 a problem in that process, we'll be asked to delete the half-created
11290 watchpoint. In that case, don't announce the deletion. */
11291 if (bpt->number)
11292 observer_notify_breakpoint_deleted (bpt);
c906108c 11293
c906108c
SS
11294 if (breakpoint_chain == bpt)
11295 breakpoint_chain = bpt->next;
11296
c906108c
SS
11297 ALL_BREAKPOINTS (b)
11298 if (b->next == bpt)
c5aa993b
JM
11299 {
11300 b->next = bpt->next;
11301 break;
11302 }
c906108c 11303
f431efe5
PA
11304 /* Be sure no bpstat's are pointing at the breakpoint after it's
11305 been freed. */
11306 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 11307 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
11308 pointing at bpt from the stop_bpstat list entirely, as breakpoint
11309 commands are associated with the bpstat; if we remove it here,
11310 then the later call to bpstat_do_actions (&stop_bpstat); in
11311 event-top.c won't do anything, and temporary breakpoints with
11312 commands won't work. */
11313
11314 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11315
4a64f543
MS
11316 /* Now that breakpoint is removed from breakpoint list, update the
11317 global location list. This will remove locations that used to
11318 belong to this breakpoint. Do this before freeing the breakpoint
11319 itself, since remove_breakpoint looks at location's owner. It
11320 might be better design to have location completely
11321 self-contained, but it's not the case now. */
b60e7edf 11322 update_global_location_list (0);
74960c60 11323
348d480f 11324 bpt->ops->dtor (bpt);
4a64f543
MS
11325 /* On the chance that someone will soon try again to delete this
11326 same bp, we mark it as deleted before freeing its storage. */
c906108c 11327 bpt->type = bp_none;
b8c9b27d 11328 xfree (bpt);
c906108c
SS
11329}
11330
4d6140d9
AC
11331static void
11332do_delete_breakpoint_cleanup (void *b)
11333{
11334 delete_breakpoint (b);
11335}
11336
11337struct cleanup *
11338make_cleanup_delete_breakpoint (struct breakpoint *b)
11339{
11340 return make_cleanup (do_delete_breakpoint_cleanup, b);
11341}
11342
51be5b68
PA
11343/* Iterator function to call a user-provided callback function once
11344 for each of B and its related breakpoints. */
11345
11346static void
11347iterate_over_related_breakpoints (struct breakpoint *b,
11348 void (*function) (struct breakpoint *,
11349 void *),
11350 void *data)
11351{
11352 struct breakpoint *related;
11353
11354 related = b;
11355 do
11356 {
11357 struct breakpoint *next;
11358
11359 /* FUNCTION may delete RELATED. */
11360 next = related->related_breakpoint;
11361
11362 if (next == related)
11363 {
11364 /* RELATED is the last ring entry. */
11365 function (related, data);
11366
11367 /* FUNCTION may have deleted it, so we'd never reach back to
11368 B. There's nothing left to do anyway, so just break
11369 out. */
11370 break;
11371 }
11372 else
11373 function (related, data);
11374
11375 related = next;
11376 }
11377 while (related != b);
11378}
95a42b64
TT
11379
11380static void
11381do_delete_breakpoint (struct breakpoint *b, void *ignore)
11382{
11383 delete_breakpoint (b);
11384}
11385
51be5b68
PA
11386/* A callback for map_breakpoint_numbers that calls
11387 delete_breakpoint. */
11388
11389static void
11390do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
11391{
11392 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
11393}
11394
c906108c 11395void
fba45db2 11396delete_command (char *arg, int from_tty)
c906108c 11397{
35df4500 11398 struct breakpoint *b, *b_tmp;
c906108c 11399
ea9365bb
TT
11400 dont_repeat ();
11401
c906108c
SS
11402 if (arg == 0)
11403 {
11404 int breaks_to_delete = 0;
11405
46c6471b
PA
11406 /* Delete all breakpoints if no argument. Do not delete
11407 internal breakpoints, these have to be deleted with an
11408 explicit breakpoint number argument. */
c5aa993b 11409 ALL_BREAKPOINTS (b)
46c6471b 11410 if (user_breakpoint_p (b))
973d738b
DJ
11411 {
11412 breaks_to_delete = 1;
11413 break;
11414 }
c906108c
SS
11415
11416 /* Ask user only if there are some breakpoints to delete. */
11417 if (!from_tty
e2e0b3e5 11418 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 11419 {
35df4500 11420 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 11421 if (user_breakpoint_p (b))
c5aa993b 11422 delete_breakpoint (b);
c906108c
SS
11423 }
11424 }
11425 else
51be5b68 11426 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
11427}
11428
0d381245
VP
11429static int
11430all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 11431{
0d381245
VP
11432 for (; loc; loc = loc->next)
11433 if (!loc->shlib_disabled)
11434 return 0;
11435 return 1;
fe3f5fa8
VP
11436}
11437
776592bf
DE
11438/* Subroutine of update_breakpoint_locations to simplify it.
11439 Return non-zero if multiple fns in list LOC have the same name.
11440 Null names are ignored. */
11441
11442static int
11443ambiguous_names_p (struct bp_location *loc)
11444{
11445 struct bp_location *l;
11446 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
11447 (int (*) (const void *,
11448 const void *)) streq,
776592bf
DE
11449 NULL, xcalloc, xfree);
11450
11451 for (l = loc; l != NULL; l = l->next)
11452 {
11453 const char **slot;
11454 const char *name = l->function_name;
11455
11456 /* Allow for some names to be NULL, ignore them. */
11457 if (name == NULL)
11458 continue;
11459
11460 slot = (const char **) htab_find_slot (htab, (const void *) name,
11461 INSERT);
4a64f543
MS
11462 /* NOTE: We can assume slot != NULL here because xcalloc never
11463 returns NULL. */
776592bf
DE
11464 if (*slot != NULL)
11465 {
11466 htab_delete (htab);
11467 return 1;
11468 }
11469 *slot = name;
11470 }
11471
11472 htab_delete (htab);
11473 return 0;
11474}
11475
0fb4aa4b
PA
11476/* When symbols change, it probably means the sources changed as well,
11477 and it might mean the static tracepoint markers are no longer at
11478 the same address or line numbers they used to be at last we
11479 checked. Losing your static tracepoints whenever you rebuild is
11480 undesirable. This function tries to resync/rematch gdb static
11481 tracepoints with the markers on the target, for static tracepoints
11482 that have not been set by marker id. Static tracepoint that have
11483 been set by marker id are reset by marker id in breakpoint_re_set.
11484 The heuristic is:
11485
11486 1) For a tracepoint set at a specific address, look for a marker at
11487 the old PC. If one is found there, assume to be the same marker.
11488 If the name / string id of the marker found is different from the
11489 previous known name, assume that means the user renamed the marker
11490 in the sources, and output a warning.
11491
11492 2) For a tracepoint set at a given line number, look for a marker
11493 at the new address of the old line number. If one is found there,
11494 assume to be the same marker. If the name / string id of the
11495 marker found is different from the previous known name, assume that
11496 means the user renamed the marker in the sources, and output a
11497 warning.
11498
11499 3) If a marker is no longer found at the same address or line, it
11500 may mean the marker no longer exists. But it may also just mean
11501 the code changed a bit. Maybe the user added a few lines of code
11502 that made the marker move up or down (in line number terms). Ask
11503 the target for info about the marker with the string id as we knew
11504 it. If found, update line number and address in the matching
11505 static tracepoint. This will get confused if there's more than one
11506 marker with the same ID (possible in UST, although unadvised
11507 precisely because it confuses tools). */
11508
11509static struct symtab_and_line
11510update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11511{
d9b3f62e 11512 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
11513 struct static_tracepoint_marker marker;
11514 CORE_ADDR pc;
11515 int i;
11516
11517 pc = sal.pc;
11518 if (sal.line)
11519 find_line_pc (sal.symtab, sal.line, &pc);
11520
11521 if (target_static_tracepoint_marker_at (pc, &marker))
11522 {
d9b3f62e 11523 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
11524 warning (_("static tracepoint %d changed probed marker from %s to %s"),
11525 b->number,
d9b3f62e 11526 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 11527
d9b3f62e
PA
11528 xfree (tp->static_trace_marker_id);
11529 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
11530 release_static_tracepoint_marker (&marker);
11531
11532 return sal;
11533 }
11534
11535 /* Old marker wasn't found on target at lineno. Try looking it up
11536 by string ID. */
11537 if (!sal.explicit_pc
11538 && sal.line != 0
11539 && sal.symtab != NULL
d9b3f62e 11540 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
11541 {
11542 VEC(static_tracepoint_marker_p) *markers;
11543
11544 markers
d9b3f62e 11545 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
11546
11547 if (!VEC_empty(static_tracepoint_marker_p, markers))
11548 {
11549 struct symtab_and_line sal;
11550 struct symbol *sym;
11551 struct static_tracepoint_marker *marker;
79a45e25 11552 struct ui_out *uiout = current_uiout;
0fb4aa4b
PA
11553
11554 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
11555
d9b3f62e
PA
11556 xfree (tp->static_trace_marker_id);
11557 tp->static_trace_marker_id = xstrdup (marker->str_id);
0fb4aa4b
PA
11558
11559 warning (_("marker for static tracepoint %d (%s) not "
11560 "found at previous line number"),
d9b3f62e 11561 b->number, tp->static_trace_marker_id);
0fb4aa4b
PA
11562
11563 init_sal (&sal);
11564
11565 sal.pc = marker->address;
11566
11567 sal = find_pc_line (marker->address, 0);
11568 sym = find_pc_sect_function (marker->address, NULL);
11569 ui_out_text (uiout, "Now in ");
11570 if (sym)
11571 {
11572 ui_out_field_string (uiout, "func",
11573 SYMBOL_PRINT_NAME (sym));
11574 ui_out_text (uiout, " at ");
11575 }
11576 ui_out_field_string (uiout, "file", sal.symtab->filename);
11577 ui_out_text (uiout, ":");
11578
11579 if (ui_out_is_mi_like_p (uiout))
11580 {
11581 char *fullname = symtab_to_fullname (sal.symtab);
11582
11583 if (fullname)
11584 ui_out_field_string (uiout, "fullname", fullname);
11585 }
11586
11587 ui_out_field_int (uiout, "line", sal.line);
11588 ui_out_text (uiout, "\n");
11589
f8eba3c6 11590 b->loc->line_number = sal.line;
0fb4aa4b 11591
f8eba3c6 11592 xfree (b->loc->source_file);
0fb4aa4b 11593 if (sym)
f8eba3c6 11594 b->loc->source_file = xstrdup (sal.symtab->filename);
0fb4aa4b 11595 else
f8eba3c6 11596 b->loc->source_file = NULL;
0fb4aa4b
PA
11597
11598 xfree (b->addr_string);
11599 b->addr_string = xstrprintf ("%s:%d",
f8eba3c6
TT
11600 sal.symtab->filename,
11601 b->loc->line_number);
0fb4aa4b
PA
11602
11603 /* Might be nice to check if function changed, and warn if
11604 so. */
11605
11606 release_static_tracepoint_marker (marker);
11607 }
11608 }
11609 return sal;
11610}
11611
8d3788bd
VP
11612/* Returns 1 iff locations A and B are sufficiently same that
11613 we don't need to report breakpoint as changed. */
11614
11615static int
11616locations_are_equal (struct bp_location *a, struct bp_location *b)
11617{
11618 while (a && b)
11619 {
11620 if (a->address != b->address)
11621 return 0;
11622
11623 if (a->shlib_disabled != b->shlib_disabled)
11624 return 0;
11625
11626 if (a->enabled != b->enabled)
11627 return 0;
11628
11629 a = a->next;
11630 b = b->next;
11631 }
11632
11633 if ((a == NULL) != (b == NULL))
11634 return 0;
11635
11636 return 1;
11637}
11638
f1310107
TJB
11639/* Create new breakpoint locations for B (a hardware or software breakpoint)
11640 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
11641 a ranged breakpoint. */
11642
0e30163f 11643void
0d381245 11644update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
11645 struct symtabs_and_lines sals,
11646 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
11647{
11648 int i;
0d381245
VP
11649 struct bp_location *existing_locations = b->loc;
11650
f8eba3c6
TT
11651 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
11652 {
11653 /* Ranged breakpoints have only one start location and one end
11654 location. */
11655 b->enable_state = bp_disabled;
11656 update_global_location_list (1);
11657 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
11658 "multiple locations found\n"),
11659 b->number);
11660 return;
11661 }
f1310107 11662
4a64f543
MS
11663 /* If there's no new locations, and all existing locations are
11664 pending, don't do anything. This optimizes the common case where
11665 all locations are in the same shared library, that was unloaded.
11666 We'd like to retain the location, so that when the library is
11667 loaded again, we don't loose the enabled/disabled status of the
11668 individual locations. */
0d381245 11669 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
11670 return;
11671
fe3f5fa8
VP
11672 b->loc = NULL;
11673
0d381245 11674 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 11675 {
f8eba3c6
TT
11676 struct bp_location *new_loc;
11677
11678 switch_to_program_space_and_thread (sals.sals[i].pspace);
11679
11680 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 11681
0d381245
VP
11682 /* Reparse conditions, they might contain references to the
11683 old symtab. */
11684 if (b->cond_string != NULL)
11685 {
f1310107 11686 char *s;
0d381245 11687 struct gdb_exception e;
fe3f5fa8 11688
0d381245
VP
11689 s = b->cond_string;
11690 TRY_CATCH (e, RETURN_MASK_ERROR)
11691 {
11692 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
11693 0);
11694 }
11695 if (e.reason < 0)
11696 {
3e43a32a
MS
11697 warning (_("failed to reevaluate condition "
11698 "for breakpoint %d: %s"),
0d381245
VP
11699 b->number, e.message);
11700 new_loc->enabled = 0;
11701 }
11702 }
fe3f5fa8 11703
f1310107
TJB
11704 if (sals_end.nelts)
11705 {
11706 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11707
11708 new_loc->length = end - sals.sals[0].pc + 1;
11709 }
0d381245 11710 }
fe3f5fa8 11711
514f746b
AR
11712 /* Update locations of permanent breakpoints. */
11713 if (b->enable_state == bp_permanent)
11714 make_breakpoint_permanent (b);
11715
4a64f543
MS
11716 /* If possible, carry over 'disable' status from existing
11717 breakpoints. */
0d381245
VP
11718 {
11719 struct bp_location *e = existing_locations;
776592bf
DE
11720 /* If there are multiple breakpoints with the same function name,
11721 e.g. for inline functions, comparing function names won't work.
11722 Instead compare pc addresses; this is just a heuristic as things
11723 may have moved, but in practice it gives the correct answer
11724 often enough until a better solution is found. */
11725 int have_ambiguous_names = ambiguous_names_p (b->loc);
11726
0d381245
VP
11727 for (; e; e = e->next)
11728 {
11729 if (!e->enabled && e->function_name)
11730 {
11731 struct bp_location *l = b->loc;
776592bf
DE
11732 if (have_ambiguous_names)
11733 {
11734 for (; l; l = l->next)
f1310107 11735 if (breakpoint_locations_match (e, l))
776592bf
DE
11736 {
11737 l->enabled = 0;
11738 break;
11739 }
11740 }
11741 else
11742 {
11743 for (; l; l = l->next)
11744 if (l->function_name
11745 && strcmp (e->function_name, l->function_name) == 0)
11746 {
11747 l->enabled = 0;
11748 break;
11749 }
11750 }
0d381245
VP
11751 }
11752 }
11753 }
fe3f5fa8 11754
8d3788bd
VP
11755 if (!locations_are_equal (existing_locations, b->loc))
11756 observer_notify_breakpoint_modified (b);
11757
b60e7edf 11758 update_global_location_list (1);
fe3f5fa8
VP
11759}
11760
ef23e705
TJB
11761/* Find the SaL locations corresponding to the given ADDR_STRING.
11762 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
11763
11764static struct symtabs_and_lines
11765addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11766{
11767 char *s;
58438ac1 11768 int marker_spec;
02d20e4a 11769 struct symtabs_and_lines sals = {0};
f8eba3c6 11770 volatile struct gdb_exception e;
ef23e705
TJB
11771
11772 s = addr_string;
11773 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11774
11775 TRY_CATCH (e, RETURN_MASK_ERROR)
11776 {
11777 if (marker_spec)
11778 {
d9b3f62e
PA
11779 struct tracepoint *tp = (struct tracepoint *) b;
11780
ef23e705 11781 sals = decode_static_tracepoint_spec (&s);
d9b3f62e 11782 if (sals.nelts > tp->static_trace_marker_id_idx)
ef23e705 11783 {
d9b3f62e 11784 sals.sals[0] = sals.sals[tp->static_trace_marker_id_idx];
ef23e705
TJB
11785 sals.nelts = 1;
11786 }
11787 else
d9b3f62e 11788 error (_("marker %s not found"), tp->static_trace_marker_id);
ef23e705
TJB
11789 }
11790 else
f8eba3c6
TT
11791 {
11792 struct linespec_result canonical;
11793
11794 init_linespec_result (&canonical);
11795 decode_line_full (&s, DECODE_LINE_FUNFIRSTLINE,
11796 (struct symtab *) NULL, 0,
11797 &canonical, multiple_symbols_all,
11798 b->filter);
11799
11800 /* We should get 0 or 1 resulting SALs. */
11801 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
11802
11803 if (VEC_length (linespec_sals, canonical.sals) > 0)
11804 {
11805 struct linespec_sals *lsal;
11806
11807 lsal = VEC_index (linespec_sals, canonical.sals, 0);
11808 sals = lsal->sals;
11809 /* Arrange it so the destructor does not free the
11810 contents. */
11811 lsal->sals.sals = NULL;
11812 }
11813
11814 destroy_linespec_result (&canonical);
11815 }
ef23e705
TJB
11816 }
11817 if (e.reason < 0)
11818 {
11819 int not_found_and_ok = 0;
11820 /* For pending breakpoints, it's expected that parsing will
11821 fail until the right shared library is loaded. User has
11822 already told to create pending breakpoints and don't need
11823 extra messages. If breakpoint is in bp_shlib_disabled
11824 state, then user already saw the message about that
11825 breakpoint being disabled, and don't want to see more
11826 errors. */
58438ac1 11827 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
11828 && (b->condition_not_parsed
11829 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 11830 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
11831 || b->enable_state == bp_disabled))
11832 not_found_and_ok = 1;
11833
11834 if (!not_found_and_ok)
11835 {
11836 /* We surely don't want to warn about the same breakpoint
11837 10 times. One solution, implemented here, is disable
11838 the breakpoint on error. Another solution would be to
11839 have separate 'warning emitted' flag. Since this
11840 happens only when a binary has changed, I don't know
11841 which approach is better. */
11842 b->enable_state = bp_disabled;
11843 throw_exception (e);
11844 }
11845 }
11846
58438ac1 11847 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 11848 {
f8eba3c6 11849 int i;
ef23e705 11850
f8eba3c6
TT
11851 for (i = 0; i < sals.nelts; ++i)
11852 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
11853 if (b->condition_not_parsed && s && s[0])
11854 {
11855 char *cond_string = 0;
11856 int thread = -1;
11857 int task = 0;
11858
11859 find_condition_and_thread (s, sals.sals[0].pc,
11860 &cond_string, &thread, &task);
11861 if (cond_string)
11862 b->cond_string = cond_string;
11863 b->thread = thread;
11864 b->task = task;
11865 b->condition_not_parsed = 0;
11866 }
11867
11868 if (b->type == bp_static_tracepoint && !marker_spec)
11869 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 11870
58438ac1
TT
11871 *found = 1;
11872 }
11873 else
11874 *found = 0;
ef23e705
TJB
11875
11876 return sals;
11877}
11878
348d480f
PA
11879/* The default re_set method, for typical hardware or software
11880 breakpoints. Reevaluate the breakpoint and recreate its
11881 locations. */
11882
11883static void
28010a5d 11884breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
11885{
11886 int found;
f1310107 11887 struct symtabs_and_lines sals, sals_end;
ef23e705 11888 struct symtabs_and_lines expanded = {0};
f1310107 11889 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
11890
11891 sals = addr_string_to_sals (b, b->addr_string, &found);
11892 if (found)
11893 {
11894 make_cleanup (xfree, sals.sals);
f8eba3c6 11895 expanded = sals;
ef23e705
TJB
11896 }
11897
f1310107
TJB
11898 if (b->addr_string_range_end)
11899 {
11900 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11901 if (found)
11902 {
11903 make_cleanup (xfree, sals_end.sals);
f8eba3c6 11904 expanded_end = sals_end;
f1310107
TJB
11905 }
11906 }
11907
11908 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
11909}
11910
11911/* Prepare the global context for a re-set of breakpoint B. */
11912
11913static struct cleanup *
11914prepare_re_set_context (struct breakpoint *b)
11915{
11916 struct cleanup *cleanups;
11917
11918 input_radix = b->input_radix;
11919 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
11920 if (b->pspace != NULL)
11921 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
11922 set_language (b->language);
11923
11924 return cleanups;
ef23e705
TJB
11925}
11926
c906108c
SS
11927/* Reset a breakpoint given it's struct breakpoint * BINT.
11928 The value we return ends up being the return value from catch_errors.
11929 Unused in this case. */
11930
11931static int
4efb68b1 11932breakpoint_re_set_one (void *bint)
c906108c 11933{
4a64f543 11934 /* Get past catch_errs. */
53a5351d 11935 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 11936 struct cleanup *cleanups;
c906108c 11937
348d480f
PA
11938 cleanups = prepare_re_set_context (b);
11939 b->ops->re_set (b);
11940 do_cleanups (cleanups);
c906108c
SS
11941 return 0;
11942}
11943
69de3c6a 11944/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 11945void
69de3c6a 11946breakpoint_re_set (void)
c906108c 11947{
35df4500 11948 struct breakpoint *b, *b_tmp;
c906108c
SS
11949 enum language save_language;
11950 int save_input_radix;
6c95b8df 11951 struct cleanup *old_chain;
c5aa993b 11952
c906108c
SS
11953 save_language = current_language->la_language;
11954 save_input_radix = input_radix;
6c95b8df
PA
11955 old_chain = save_current_program_space ();
11956
35df4500 11957 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11958 {
4a64f543 11959 /* Format possible error msg. */
fe3f5fa8 11960 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
11961 b->number);
11962 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 11963 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 11964 do_cleanups (cleanups);
c5aa993b 11965 }
c906108c
SS
11966 set_language (save_language);
11967 input_radix = save_input_radix;
e62c965a 11968
0756c555 11969 jit_breakpoint_re_set ();
4efc6507 11970
6c95b8df
PA
11971 do_cleanups (old_chain);
11972
af02033e
PP
11973 create_overlay_event_breakpoint ();
11974 create_longjmp_master_breakpoint ();
11975 create_std_terminate_master_breakpoint ();
186c406b 11976 create_exception_master_breakpoint ();
1bfeeb0f
JL
11977
11978 /* While we're at it, reset the skip list too. */
11979 skip_re_set ();
c906108c
SS
11980}
11981\f
c906108c
SS
11982/* Reset the thread number of this breakpoint:
11983
11984 - If the breakpoint is for all threads, leave it as-is.
4a64f543 11985 - Else, reset it to the current thread for inferior_ptid. */
c906108c 11986void
fba45db2 11987breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
11988{
11989 if (b->thread != -1)
11990 {
39f77062
KB
11991 if (in_thread_list (inferior_ptid))
11992 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
11993
11994 /* We're being called after following a fork. The new fork is
11995 selected as current, and unless this was a vfork will have a
11996 different program space from the original thread. Reset that
11997 as well. */
11998 b->loc->pspace = current_program_space;
c906108c
SS
11999 }
12000}
12001
03ac34d5
MS
12002/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12003 If from_tty is nonzero, it prints a message to that effect,
12004 which ends with a period (no newline). */
12005
c906108c 12006void
fba45db2 12007set_ignore_count (int bptnum, int count, int from_tty)
c906108c 12008{
52f0bd74 12009 struct breakpoint *b;
c906108c
SS
12010
12011 if (count < 0)
12012 count = 0;
12013
12014 ALL_BREAKPOINTS (b)
12015 if (b->number == bptnum)
c5aa993b 12016 {
d77f58be
SS
12017 if (is_tracepoint (b))
12018 {
12019 if (from_tty && count != 0)
12020 printf_filtered (_("Ignore count ignored for tracepoint %d."),
12021 bptnum);
12022 return;
12023 }
12024
c5aa993b 12025 b->ignore_count = count;
221ea385
KS
12026 if (from_tty)
12027 {
12028 if (count == 0)
3e43a32a
MS
12029 printf_filtered (_("Will stop next time "
12030 "breakpoint %d is reached."),
221ea385
KS
12031 bptnum);
12032 else if (count == 1)
a3f17187 12033 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
12034 bptnum);
12035 else
3e43a32a
MS
12036 printf_filtered (_("Will ignore next %d "
12037 "crossings of breakpoint %d."),
221ea385
KS
12038 count, bptnum);
12039 }
c5aa993b 12040 breakpoints_changed ();
8d3788bd 12041 observer_notify_breakpoint_modified (b);
c5aa993b
JM
12042 return;
12043 }
c906108c 12044
8a3fe4f8 12045 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
12046}
12047
c906108c
SS
12048/* Command to set ignore-count of breakpoint N to COUNT. */
12049
12050static void
fba45db2 12051ignore_command (char *args, int from_tty)
c906108c
SS
12052{
12053 char *p = args;
52f0bd74 12054 int num;
c906108c
SS
12055
12056 if (p == 0)
e2e0b3e5 12057 error_no_arg (_("a breakpoint number"));
c5aa993b 12058
c906108c 12059 num = get_number (&p);
5c44784c 12060 if (num == 0)
8a3fe4f8 12061 error (_("bad breakpoint number: '%s'"), args);
c906108c 12062 if (*p == 0)
8a3fe4f8 12063 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
12064
12065 set_ignore_count (num,
12066 longest_to_int (value_as_long (parse_and_eval (p))),
12067 from_tty);
221ea385
KS
12068 if (from_tty)
12069 printf_filtered ("\n");
c906108c
SS
12070}
12071\f
12072/* Call FUNCTION on each of the breakpoints
12073 whose numbers are given in ARGS. */
12074
12075static void
95a42b64
TT
12076map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
12077 void *),
12078 void *data)
c906108c 12079{
52f0bd74
AC
12080 int num;
12081 struct breakpoint *b, *tmp;
11cf8741 12082 int match;
197f0a60 12083 struct get_number_or_range_state state;
c906108c 12084
197f0a60 12085 if (args == 0)
e2e0b3e5 12086 error_no_arg (_("one or more breakpoint numbers"));
c906108c 12087
197f0a60
TT
12088 init_number_or_range (&state, args);
12089
12090 while (!state.finished)
c906108c 12091 {
197f0a60
TT
12092 char *p = state.string;
12093
11cf8741 12094 match = 0;
c5aa993b 12095
197f0a60 12096 num = get_number_or_range (&state);
5c44784c 12097 if (num == 0)
c5aa993b 12098 {
8a3fe4f8 12099 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
12100 }
12101 else
12102 {
12103 ALL_BREAKPOINTS_SAFE (b, tmp)
12104 if (b->number == num)
12105 {
11cf8741 12106 match = 1;
cdac0397 12107 function (b, data);
11cf8741 12108 break;
5c44784c 12109 }
11cf8741 12110 if (match == 0)
a3f17187 12111 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 12112 }
c906108c
SS
12113 }
12114}
12115
0d381245
VP
12116static struct bp_location *
12117find_location_by_number (char *number)
12118{
12119 char *dot = strchr (number, '.');
12120 char *p1;
12121 int bp_num;
12122 int loc_num;
12123 struct breakpoint *b;
12124 struct bp_location *loc;
12125
12126 *dot = '\0';
12127
12128 p1 = number;
197f0a60 12129 bp_num = get_number (&p1);
0d381245
VP
12130 if (bp_num == 0)
12131 error (_("Bad breakpoint number '%s'"), number);
12132
12133 ALL_BREAKPOINTS (b)
12134 if (b->number == bp_num)
12135 {
12136 break;
12137 }
12138
12139 if (!b || b->number != bp_num)
12140 error (_("Bad breakpoint number '%s'"), number);
12141
12142 p1 = dot+1;
197f0a60 12143 loc_num = get_number (&p1);
0d381245
VP
12144 if (loc_num == 0)
12145 error (_("Bad breakpoint location number '%s'"), number);
12146
12147 --loc_num;
12148 loc = b->loc;
12149 for (;loc_num && loc; --loc_num, loc = loc->next)
12150 ;
12151 if (!loc)
12152 error (_("Bad breakpoint location number '%s'"), dot+1);
12153
12154 return loc;
12155}
12156
12157
1900040c
MS
12158/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12159 If from_tty is nonzero, it prints a message to that effect,
12160 which ends with a period (no newline). */
12161
c906108c 12162void
fba45db2 12163disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
12164{
12165 /* Never disable a watchpoint scope breakpoint; we want to
12166 hit them when we leave scope so we can delete both the
12167 watchpoint and its scope breakpoint at that time. */
12168 if (bpt->type == bp_watchpoint_scope)
12169 return;
12170
c2c6d25f 12171 /* You can't disable permanent breakpoints. */
b5de0fa7 12172 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
12173 return;
12174
b5de0fa7 12175 bpt->enable_state = bp_disabled;
c906108c 12176
d248b706
KY
12177 if (target_supports_enable_disable_tracepoint ()
12178 && current_trace_status ()->running && is_tracepoint (bpt))
12179 {
12180 struct bp_location *location;
12181
12182 for (location = bpt->loc; location; location = location->next)
12183 target_disable_tracepoint (location);
12184 }
12185
b60e7edf 12186 update_global_location_list (0);
c906108c 12187
8d3788bd 12188 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12189}
12190
51be5b68
PA
12191/* A callback for iterate_over_related_breakpoints. */
12192
12193static void
12194do_disable_breakpoint (struct breakpoint *b, void *ignore)
12195{
12196 disable_breakpoint (b);
12197}
12198
95a42b64
TT
12199/* A callback for map_breakpoint_numbers that calls
12200 disable_breakpoint. */
12201
12202static void
12203do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
12204{
51be5b68 12205 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
12206}
12207
c906108c 12208static void
fba45db2 12209disable_command (char *args, int from_tty)
c906108c 12210{
c906108c 12211 if (args == 0)
46c6471b
PA
12212 {
12213 struct breakpoint *bpt;
12214
12215 ALL_BREAKPOINTS (bpt)
12216 if (user_breakpoint_p (bpt))
12217 disable_breakpoint (bpt);
12218 }
0d381245
VP
12219 else if (strchr (args, '.'))
12220 {
12221 struct bp_location *loc = find_location_by_number (args);
12222 if (loc)
d248b706
KY
12223 {
12224 loc->enabled = 0;
12225 if (target_supports_enable_disable_tracepoint ()
12226 && current_trace_status ()->running && loc->owner
12227 && is_tracepoint (loc->owner))
12228 target_disable_tracepoint (loc);
12229 }
b60e7edf 12230 update_global_location_list (0);
0d381245 12231 }
c906108c 12232 else
95a42b64 12233 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
12234}
12235
12236static void
51be5b68 12237enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 12238{
afe38095 12239 int target_resources_ok;
c906108c
SS
12240
12241 if (bpt->type == bp_hardware_breakpoint)
12242 {
12243 int i;
c5aa993b 12244 i = hw_breakpoint_used_count ();
53a5351d 12245 target_resources_ok =
d92524f1 12246 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 12247 i + 1, 0);
c906108c 12248 if (target_resources_ok == 0)
8a3fe4f8 12249 error (_("No hardware breakpoint support in the target."));
c906108c 12250 else if (target_resources_ok < 0)
8a3fe4f8 12251 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
12252 }
12253
cc60f2e3 12254 if (is_watchpoint (bpt))
c906108c 12255 {
d07205c2
JK
12256 /* Initialize it just to avoid a GCC false warning. */
12257 enum enable_state orig_enable_state = 0;
dde02812
ES
12258 struct gdb_exception e;
12259
12260 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 12261 {
3a5c3e22
PA
12262 struct watchpoint *w = (struct watchpoint *) bpt;
12263
1e718ff1
TJB
12264 orig_enable_state = bpt->enable_state;
12265 bpt->enable_state = bp_enabled;
3a5c3e22 12266 update_watchpoint (w, 1 /* reparse */);
c906108c 12267 }
dde02812 12268 if (e.reason < 0)
c5aa993b 12269 {
1e718ff1 12270 bpt->enable_state = orig_enable_state;
dde02812
ES
12271 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12272 bpt->number);
12273 return;
c5aa993b 12274 }
c906108c 12275 }
0101ce28 12276
b4c291bb
KH
12277 if (bpt->enable_state != bp_permanent)
12278 bpt->enable_state = bp_enabled;
d248b706
KY
12279
12280 if (target_supports_enable_disable_tracepoint ()
12281 && current_trace_status ()->running && is_tracepoint (bpt))
12282 {
12283 struct bp_location *location;
12284
12285 for (location = bpt->loc; location; location = location->next)
12286 target_enable_tracepoint (location);
12287 }
12288
b4c291bb 12289 bpt->disposition = disposition;
b60e7edf 12290 update_global_location_list (1);
b4c291bb
KH
12291 breakpoints_changed ();
12292
8d3788bd 12293 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12294}
12295
fe3f5fa8 12296
c906108c 12297void
fba45db2 12298enable_breakpoint (struct breakpoint *bpt)
c906108c 12299{
51be5b68
PA
12300 enable_breakpoint_disp (bpt, bpt->disposition);
12301}
12302
12303static void
12304do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12305{
12306 enable_breakpoint (bpt);
c906108c
SS
12307}
12308
95a42b64
TT
12309/* A callback for map_breakpoint_numbers that calls
12310 enable_breakpoint. */
12311
12312static void
12313do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12314{
51be5b68 12315 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
12316}
12317
c906108c
SS
12318/* The enable command enables the specified breakpoints (or all defined
12319 breakpoints) so they once again become (or continue to be) effective
1272ad14 12320 in stopping the inferior. */
c906108c 12321
c906108c 12322static void
fba45db2 12323enable_command (char *args, int from_tty)
c906108c 12324{
c906108c 12325 if (args == 0)
46c6471b
PA
12326 {
12327 struct breakpoint *bpt;
12328
12329 ALL_BREAKPOINTS (bpt)
12330 if (user_breakpoint_p (bpt))
12331 enable_breakpoint (bpt);
12332 }
0d381245
VP
12333 else if (strchr (args, '.'))
12334 {
12335 struct bp_location *loc = find_location_by_number (args);
12336 if (loc)
d248b706
KY
12337 {
12338 loc->enabled = 1;
12339 if (target_supports_enable_disable_tracepoint ()
12340 && current_trace_status ()->running && loc->owner
12341 && is_tracepoint (loc->owner))
12342 target_enable_tracepoint (loc);
12343 }
b60e7edf 12344 update_global_location_list (1);
0d381245 12345 }
c906108c 12346 else
95a42b64 12347 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
12348}
12349
12350static void
51be5b68
PA
12351do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12352{
12353 enum bpdisp disp = *(enum bpdisp *) arg;
12354
12355 enable_breakpoint_disp (bpt, disp);
12356}
12357
12358static void
12359do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12360{
51be5b68
PA
12361 enum bpdisp disp = disp_disable;
12362
12363 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12364}
12365
c906108c 12366static void
fba45db2 12367enable_once_command (char *args, int from_tty)
c906108c 12368{
51be5b68 12369 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
12370}
12371
12372static void
51be5b68 12373do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12374{
51be5b68
PA
12375 enum bpdisp disp = disp_del;
12376
12377 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12378}
12379
c906108c 12380static void
fba45db2 12381enable_delete_command (char *args, int from_tty)
c906108c 12382{
51be5b68 12383 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
12384}
12385\f
fa8d40ab
JJ
12386static void
12387set_breakpoint_cmd (char *args, int from_tty)
12388{
12389}
12390
12391static void
12392show_breakpoint_cmd (char *args, int from_tty)
12393{
12394}
12395
1f3b5d1b
PP
12396/* Invalidate last known value of any hardware watchpoint if
12397 the memory which that value represents has been written to by
12398 GDB itself. */
12399
12400static void
12401invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12402 const bfd_byte *data)
12403{
12404 struct breakpoint *bp;
12405
12406 ALL_BREAKPOINTS (bp)
12407 if (bp->enable_state == bp_enabled
3a5c3e22 12408 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 12409 {
3a5c3e22 12410 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 12411
3a5c3e22
PA
12412 if (wp->val_valid && wp->val)
12413 {
12414 struct bp_location *loc;
12415
12416 for (loc = bp->loc; loc != NULL; loc = loc->next)
12417 if (loc->loc_type == bp_loc_hardware_watchpoint
12418 && loc->address + loc->length > addr
12419 && addr + len > loc->address)
12420 {
12421 value_free (wp->val);
12422 wp->val = NULL;
12423 wp->val_valid = 0;
12424 }
12425 }
1f3b5d1b
PP
12426 }
12427}
12428
1bfeeb0f
JL
12429/* Use the last displayed codepoint's values, or nothing
12430 if they aren't valid. */
c906108c
SS
12431
12432struct symtabs_and_lines
f8eba3c6 12433decode_line_spec_1 (char *string, int flags)
c906108c
SS
12434{
12435 struct symtabs_and_lines sals;
cc59ec59 12436
c906108c 12437 if (string == 0)
8a3fe4f8 12438 error (_("Empty line specification."));
1bfeeb0f 12439 if (last_displayed_sal_is_valid ())
f8eba3c6 12440 sals = decode_line_1 (&string, flags,
1bfeeb0f 12441 get_last_displayed_symtab (),
f8eba3c6 12442 get_last_displayed_line ());
c906108c 12443 else
f8eba3c6 12444 sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
c906108c 12445 if (*string)
8a3fe4f8 12446 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
12447 return sals;
12448}
8181d85f
DJ
12449
12450/* Create and insert a raw software breakpoint at PC. Return an
12451 identifier, which should be used to remove the breakpoint later.
12452 In general, places which call this should be using something on the
12453 breakpoint chain instead; this function should be eliminated
12454 someday. */
12455
12456void *
6c95b8df
PA
12457deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12458 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
12459{
12460 struct bp_target_info *bp_tgt;
12461
6c95b8df 12462 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 12463
6c95b8df 12464 bp_tgt->placed_address_space = aspace;
8181d85f 12465 bp_tgt->placed_address = pc;
6c95b8df 12466
a6d9a66e 12467 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
12468 {
12469 /* Could not insert the breakpoint. */
12470 xfree (bp_tgt);
12471 return NULL;
12472 }
12473
12474 return bp_tgt;
12475}
12476
4a64f543
MS
12477/* Remove a breakpoint BP inserted by
12478 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
12479
12480int
a6d9a66e 12481deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
12482{
12483 struct bp_target_info *bp_tgt = bp;
12484 int ret;
12485
a6d9a66e 12486 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
12487 xfree (bp_tgt);
12488
12489 return ret;
12490}
12491
4a64f543
MS
12492/* One (or perhaps two) breakpoints used for software single
12493 stepping. */
8181d85f
DJ
12494
12495static void *single_step_breakpoints[2];
a6d9a66e 12496static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
12497
12498/* Create and insert a breakpoint for software single step. */
12499
12500void
6c95b8df 12501insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
12502 struct address_space *aspace,
12503 CORE_ADDR next_pc)
8181d85f
DJ
12504{
12505 void **bpt_p;
12506
12507 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
12508 {
12509 bpt_p = &single_step_breakpoints[0];
12510 single_step_gdbarch[0] = gdbarch;
12511 }
8181d85f
DJ
12512 else
12513 {
12514 gdb_assert (single_step_breakpoints[1] == NULL);
12515 bpt_p = &single_step_breakpoints[1];
a6d9a66e 12516 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
12517 }
12518
4a64f543
MS
12519 /* NOTE drow/2006-04-11: A future improvement to this function would
12520 be to only create the breakpoints once, and actually put them on
12521 the breakpoint chain. That would let us use set_raw_breakpoint.
12522 We could adjust the addresses each time they were needed. Doing
12523 this requires corresponding changes elsewhere where single step
12524 breakpoints are handled, however. So, for now, we use this. */
8181d85f 12525
6c95b8df 12526 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 12527 if (*bpt_p == NULL)
5af949e3
UW
12528 error (_("Could not insert single-step breakpoint at %s"),
12529 paddress (gdbarch, next_pc));
8181d85f
DJ
12530}
12531
f02253f1
HZ
12532/* Check if the breakpoints used for software single stepping
12533 were inserted or not. */
12534
12535int
12536single_step_breakpoints_inserted (void)
12537{
12538 return (single_step_breakpoints[0] != NULL
12539 || single_step_breakpoints[1] != NULL);
12540}
12541
8181d85f
DJ
12542/* Remove and delete any breakpoints used for software single step. */
12543
12544void
12545remove_single_step_breakpoints (void)
12546{
12547 gdb_assert (single_step_breakpoints[0] != NULL);
12548
12549 /* See insert_single_step_breakpoint for more about this deprecated
12550 call. */
a6d9a66e
UW
12551 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12552 single_step_breakpoints[0]);
12553 single_step_gdbarch[0] = NULL;
8181d85f
DJ
12554 single_step_breakpoints[0] = NULL;
12555
12556 if (single_step_breakpoints[1] != NULL)
12557 {
a6d9a66e
UW
12558 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12559 single_step_breakpoints[1]);
12560 single_step_gdbarch[1] = NULL;
8181d85f
DJ
12561 single_step_breakpoints[1] = NULL;
12562 }
12563}
12564
d03285ec
UW
12565/* Delete software single step breakpoints without removing them from
12566 the inferior. This is intended to be used if the inferior's address
12567 space where they were inserted is already gone, e.g. after exit or
12568 exec. */
12569
12570void
12571cancel_single_step_breakpoints (void)
12572{
12573 int i;
12574
12575 for (i = 0; i < 2; i++)
12576 if (single_step_breakpoints[i])
12577 {
12578 xfree (single_step_breakpoints[i]);
12579 single_step_breakpoints[i] = NULL;
12580 single_step_gdbarch[i] = NULL;
12581 }
12582}
12583
12584/* Detach software single-step breakpoints from INFERIOR_PTID without
12585 removing them. */
12586
12587static void
12588detach_single_step_breakpoints (void)
12589{
12590 int i;
12591
12592 for (i = 0; i < 2; i++)
12593 if (single_step_breakpoints[i])
12594 target_remove_breakpoint (single_step_gdbarch[i],
12595 single_step_breakpoints[i]);
12596}
12597
4a64f543
MS
12598/* Check whether a software single-step breakpoint is inserted at
12599 PC. */
1aafd4da
UW
12600
12601static int
cc59ec59
MS
12602single_step_breakpoint_inserted_here_p (struct address_space *aspace,
12603 CORE_ADDR pc)
1aafd4da
UW
12604{
12605 int i;
12606
12607 for (i = 0; i < 2; i++)
12608 {
12609 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
12610 if (bp_tgt
12611 && breakpoint_address_match (bp_tgt->placed_address_space,
12612 bp_tgt->placed_address,
12613 aspace, pc))
1aafd4da
UW
12614 return 1;
12615 }
12616
12617 return 0;
12618}
12619
a96d9b2e
SDJ
12620/* Returns 0 if 'bp' is NOT a syscall catchpoint,
12621 non-zero otherwise. */
12622static int
12623is_syscall_catchpoint_enabled (struct breakpoint *bp)
12624{
12625 if (syscall_catchpoint_p (bp)
12626 && bp->enable_state != bp_disabled
12627 && bp->enable_state != bp_call_disabled)
12628 return 1;
12629 else
12630 return 0;
12631}
12632
12633int
12634catch_syscall_enabled (void)
12635{
12636 struct inferior *inf = current_inferior ();
12637
12638 return inf->total_syscalls_count != 0;
12639}
12640
12641int
12642catching_syscall_number (int syscall_number)
12643{
12644 struct breakpoint *bp;
12645
12646 ALL_BREAKPOINTS (bp)
12647 if (is_syscall_catchpoint_enabled (bp))
12648 {
be5c67c1
PA
12649 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12650
12651 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
12652 {
12653 int i, iter;
12654 for (i = 0;
be5c67c1 12655 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
12656 i++)
12657 if (syscall_number == iter)
12658 return 1;
12659 }
12660 else
12661 return 1;
12662 }
12663
12664 return 0;
12665}
12666
12667/* Complete syscall names. Used by "catch syscall". */
12668static char **
12669catch_syscall_completer (struct cmd_list_element *cmd,
12670 char *text, char *word)
12671{
12672 const char **list = get_syscall_names ();
c38eea1a
MS
12673 char **retlist
12674 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 12675
c38eea1a
MS
12676 xfree (list);
12677 return retlist;
a96d9b2e
SDJ
12678}
12679
1042e4c0
SS
12680/* Tracepoint-specific operations. */
12681
12682/* Set tracepoint count to NUM. */
12683static void
12684set_tracepoint_count (int num)
12685{
12686 tracepoint_count = num;
4fa62494 12687 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
12688}
12689
12690void
12691trace_command (char *arg, int from_tty)
12692{
8cdf0e15
VP
12693 if (create_breakpoint (get_current_arch (),
12694 arg,
12695 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12696 0 /* tempflag */,
12697 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
12698 0 /* Ignore count */,
12699 pending_break_support,
348d480f 12700 &tracepoint_breakpoint_ops,
8cdf0e15 12701 from_tty,
84f4c1fe
PM
12702 1 /* enabled */,
12703 0 /* internal */))
fd9b8c24 12704 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
12705}
12706
7a697b8d
SS
12707void
12708ftrace_command (char *arg, int from_tty)
12709{
8cdf0e15
VP
12710 if (create_breakpoint (get_current_arch (),
12711 arg,
12712 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12713 0 /* tempflag */,
12714 bp_fast_tracepoint /* type_wanted */,
12715 0 /* Ignore count */,
12716 pending_break_support,
348d480f 12717 &tracepoint_breakpoint_ops,
0fb4aa4b 12718 from_tty,
84f4c1fe
PM
12719 1 /* enabled */,
12720 0 /* internal */))
0fb4aa4b
PA
12721 set_tracepoint_count (breakpoint_count);
12722}
12723
12724/* strace command implementation. Creates a static tracepoint. */
12725
12726void
12727strace_command (char *arg, int from_tty)
12728{
12729 if (create_breakpoint (get_current_arch (),
12730 arg,
12731 NULL, 0, 1 /* parse arg */,
12732 0 /* tempflag */,
12733 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
12734 0 /* Ignore count */,
12735 pending_break_support,
348d480f 12736 &tracepoint_breakpoint_ops,
8cdf0e15 12737 from_tty,
84f4c1fe
PM
12738 1 /* enabled */,
12739 0 /* internal */))
fd9b8c24 12740 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
12741}
12742
409873ef
SS
12743/* Set up a fake reader function that gets command lines from a linked
12744 list that was acquired during tracepoint uploading. */
12745
12746static struct uploaded_tp *this_utp;
3149d8c1 12747static int next_cmd;
409873ef
SS
12748
12749static char *
12750read_uploaded_action (void)
12751{
12752 char *rslt;
12753
3149d8c1 12754 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 12755
3149d8c1 12756 next_cmd++;
409873ef
SS
12757
12758 return rslt;
12759}
12760
00bf0b85
SS
12761/* Given information about a tracepoint as recorded on a target (which
12762 can be either a live system or a trace file), attempt to create an
12763 equivalent GDB tracepoint. This is not a reliable process, since
12764 the target does not necessarily have all the information used when
12765 the tracepoint was originally defined. */
12766
d9b3f62e 12767struct tracepoint *
00bf0b85 12768create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 12769{
409873ef 12770 char *addr_str, small_buf[100];
d9b3f62e 12771 struct tracepoint *tp;
fd9b8c24 12772
409873ef
SS
12773 if (utp->at_string)
12774 addr_str = utp->at_string;
12775 else
12776 {
12777 /* In the absence of a source location, fall back to raw
12778 address. Since there is no way to confirm that the address
12779 means the same thing as when the trace was started, warn the
12780 user. */
3e43a32a
MS
12781 warning (_("Uploaded tracepoint %d has no "
12782 "source location, using raw address"),
409873ef
SS
12783 utp->number);
12784 sprintf (small_buf, "*%s", hex_string (utp->addr));
12785 addr_str = small_buf;
12786 }
12787
12788 /* There's not much we can do with a sequence of bytecodes. */
12789 if (utp->cond && !utp->cond_string)
3e43a32a
MS
12790 warning (_("Uploaded tracepoint %d condition "
12791 "has no source form, ignoring it"),
409873ef 12792 utp->number);
d5551862 12793
8cdf0e15 12794 if (!create_breakpoint (get_current_arch (),
409873ef
SS
12795 addr_str,
12796 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 12797 0 /* tempflag */,
0fb4aa4b 12798 utp->type /* type_wanted */,
8cdf0e15
VP
12799 0 /* Ignore count */,
12800 pending_break_support,
348d480f 12801 &tracepoint_breakpoint_ops,
8cdf0e15 12802 0 /* from_tty */,
84f4c1fe
PM
12803 utp->enabled /* enabled */,
12804 0 /* internal */))
fd9b8c24
PA
12805 return NULL;
12806
00bf0b85
SS
12807 set_tracepoint_count (breakpoint_count);
12808
409873ef 12809 /* Get the tracepoint we just created. */
fd9b8c24
PA
12810 tp = get_tracepoint (tracepoint_count);
12811 gdb_assert (tp != NULL);
d5551862 12812
00bf0b85
SS
12813 if (utp->pass > 0)
12814 {
d9b3f62e 12815 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 12816
409873ef 12817 trace_pass_command (small_buf, 0);
00bf0b85
SS
12818 }
12819
409873ef
SS
12820 /* If we have uploaded versions of the original commands, set up a
12821 special-purpose "reader" function and call the usual command line
12822 reader, then pass the result to the breakpoint command-setting
12823 function. */
3149d8c1 12824 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 12825 {
409873ef 12826 struct command_line *cmd_list;
00bf0b85 12827
409873ef 12828 this_utp = utp;
3149d8c1 12829 next_cmd = 0;
d5551862 12830
409873ef
SS
12831 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12832
d9b3f62e 12833 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 12834 }
3149d8c1
SS
12835 else if (!VEC_empty (char_ptr, utp->actions)
12836 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12837 warning (_("Uploaded tracepoint %d actions "
12838 "have no source form, ignoring them"),
409873ef 12839 utp->number);
00bf0b85 12840
f196051f
SS
12841 /* Copy any status information that might be available. */
12842 tp->base.hit_count = utp->hit_count;
12843 tp->traceframe_usage = utp->traceframe_usage;
12844
00bf0b85 12845 return tp;
d9b3f62e 12846}
00bf0b85 12847
1042e4c0
SS
12848/* Print information on tracepoint number TPNUM_EXP, or all if
12849 omitted. */
12850
12851static void
e5a67952 12852tracepoints_info (char *args, int from_tty)
1042e4c0 12853{
79a45e25 12854 struct ui_out *uiout = current_uiout;
e5a67952 12855 int num_printed;
1042e4c0 12856
e5a67952 12857 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
12858
12859 if (num_printed == 0)
1042e4c0 12860 {
e5a67952 12861 if (args == NULL || *args == '\0')
d77f58be
SS
12862 ui_out_message (uiout, 0, "No tracepoints.\n");
12863 else
e5a67952 12864 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 12865 }
ad443146
SS
12866
12867 default_collect_info ();
1042e4c0
SS
12868}
12869
4a64f543 12870/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
12871 Not supported by all targets. */
12872static void
12873enable_trace_command (char *args, int from_tty)
12874{
12875 enable_command (args, from_tty);
12876}
12877
4a64f543 12878/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
12879 Not supported by all targets. */
12880static void
12881disable_trace_command (char *args, int from_tty)
12882{
12883 disable_command (args, from_tty);
12884}
12885
4a64f543 12886/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
12887static void
12888delete_trace_command (char *arg, int from_tty)
12889{
35df4500 12890 struct breakpoint *b, *b_tmp;
1042e4c0
SS
12891
12892 dont_repeat ();
12893
12894 if (arg == 0)
12895 {
12896 int breaks_to_delete = 0;
12897
12898 /* Delete all breakpoints if no argument.
12899 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
12900 have to be deleted with an explicit breakpoint number
12901 argument. */
1042e4c0 12902 ALL_TRACEPOINTS (b)
46c6471b 12903 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
12904 {
12905 breaks_to_delete = 1;
12906 break;
12907 }
1042e4c0
SS
12908
12909 /* Ask user only if there are some breakpoints to delete. */
12910 if (!from_tty
12911 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12912 {
35df4500 12913 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 12914 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 12915 delete_breakpoint (b);
1042e4c0
SS
12916 }
12917 }
12918 else
51be5b68 12919 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
12920}
12921
197f0a60
TT
12922/* Helper function for trace_pass_command. */
12923
12924static void
d9b3f62e 12925trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 12926{
d9b3f62e
PA
12927 tp->pass_count = count;
12928 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
12929 if (from_tty)
12930 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 12931 tp->base.number, count);
197f0a60
TT
12932}
12933
1042e4c0
SS
12934/* Set passcount for tracepoint.
12935
12936 First command argument is passcount, second is tracepoint number.
12937 If tracepoint number omitted, apply to most recently defined.
12938 Also accepts special argument "all". */
12939
12940static void
12941trace_pass_command (char *args, int from_tty)
12942{
d9b3f62e 12943 struct tracepoint *t1;
1042e4c0 12944 unsigned int count;
1042e4c0
SS
12945
12946 if (args == 0 || *args == 0)
3e43a32a
MS
12947 error (_("passcount command requires an "
12948 "argument (count + optional TP num)"));
1042e4c0 12949
4a64f543 12950 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
12951
12952 while (*args && isspace ((int) *args))
12953 args++;
12954
12955 if (*args && strncasecmp (args, "all", 3) == 0)
12956 {
d9b3f62e
PA
12957 struct breakpoint *b;
12958
1042e4c0 12959 args += 3; /* Skip special argument "all". */
1042e4c0
SS
12960 if (*args)
12961 error (_("Junk at end of arguments."));
1042e4c0 12962
d9b3f62e 12963 ALL_TRACEPOINTS (b)
197f0a60 12964 {
d9b3f62e 12965 t1 = (struct tracepoint *) b;
197f0a60
TT
12966 trace_pass_set_count (t1, count, from_tty);
12967 }
12968 }
12969 else if (*args == '\0')
1042e4c0 12970 {
197f0a60 12971 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 12972 if (t1)
197f0a60
TT
12973 trace_pass_set_count (t1, count, from_tty);
12974 }
12975 else
12976 {
12977 struct get_number_or_range_state state;
12978
12979 init_number_or_range (&state, args);
12980 while (!state.finished)
1042e4c0 12981 {
197f0a60
TT
12982 t1 = get_tracepoint_by_number (&args, &state, 1);
12983 if (t1)
12984 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
12985 }
12986 }
1042e4c0
SS
12987}
12988
d9b3f62e 12989struct tracepoint *
1042e4c0
SS
12990get_tracepoint (int num)
12991{
12992 struct breakpoint *t;
12993
12994 ALL_TRACEPOINTS (t)
12995 if (t->number == num)
d9b3f62e 12996 return (struct tracepoint *) t;
1042e4c0
SS
12997
12998 return NULL;
12999}
13000
d5551862
SS
13001/* Find the tracepoint with the given target-side number (which may be
13002 different from the tracepoint number after disconnecting and
13003 reconnecting). */
13004
d9b3f62e 13005struct tracepoint *
d5551862
SS
13006get_tracepoint_by_number_on_target (int num)
13007{
d9b3f62e 13008 struct breakpoint *b;
d5551862 13009
d9b3f62e
PA
13010 ALL_TRACEPOINTS (b)
13011 {
13012 struct tracepoint *t = (struct tracepoint *) b;
13013
13014 if (t->number_on_target == num)
13015 return t;
13016 }
d5551862
SS
13017
13018 return NULL;
13019}
13020
1042e4c0 13021/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
13022 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13023 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 13024 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 13025struct tracepoint *
197f0a60
TT
13026get_tracepoint_by_number (char **arg,
13027 struct get_number_or_range_state *state,
13028 int optional_p)
1042e4c0
SS
13029{
13030 extern int tracepoint_count;
13031 struct breakpoint *t;
13032 int tpnum;
13033 char *instring = arg == NULL ? NULL : *arg;
13034
197f0a60
TT
13035 if (state)
13036 {
13037 gdb_assert (!state->finished);
13038 tpnum = get_number_or_range (state);
13039 }
13040 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
13041 {
13042 if (optional_p)
13043 tpnum = tracepoint_count;
13044 else
13045 error_no_arg (_("tracepoint number"));
13046 }
13047 else
197f0a60 13048 tpnum = get_number (arg);
1042e4c0
SS
13049
13050 if (tpnum <= 0)
13051 {
13052 if (instring && *instring)
13053 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
13054 instring);
13055 else
3e43a32a
MS
13056 printf_filtered (_("Tracepoint argument missing "
13057 "and no previous tracepoint\n"));
1042e4c0
SS
13058 return NULL;
13059 }
13060
13061 ALL_TRACEPOINTS (t)
13062 if (t->number == tpnum)
13063 {
d9b3f62e 13064 return (struct tracepoint *) t;
1042e4c0
SS
13065 }
13066
1042e4c0
SS
13067 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
13068 return NULL;
13069}
13070
d9b3f62e
PA
13071void
13072print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
13073{
13074 if (b->thread != -1)
13075 fprintf_unfiltered (fp, " thread %d", b->thread);
13076
13077 if (b->task != 0)
13078 fprintf_unfiltered (fp, " task %d", b->task);
13079
13080 fprintf_unfiltered (fp, "\n");
13081}
13082
6149aea9
PA
13083/* Save information on user settable breakpoints (watchpoints, etc) to
13084 a new script file named FILENAME. If FILTER is non-NULL, call it
13085 on each breakpoint and only include the ones for which it returns
13086 non-zero. */
13087
1042e4c0 13088static void
6149aea9
PA
13089save_breakpoints (char *filename, int from_tty,
13090 int (*filter) (const struct breakpoint *))
1042e4c0
SS
13091{
13092 struct breakpoint *tp;
6149aea9 13093 int any = 0;
a7bdde9e 13094 char *pathname;
1042e4c0 13095 struct cleanup *cleanup;
a7bdde9e 13096 struct ui_file *fp;
6149aea9 13097 int extra_trace_bits = 0;
1042e4c0 13098
6149aea9
PA
13099 if (filename == 0 || *filename == 0)
13100 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
13101
13102 /* See if we have anything to save. */
6149aea9 13103 ALL_BREAKPOINTS (tp)
1042e4c0 13104 {
6149aea9 13105 /* Skip internal and momentary breakpoints. */
09d682a4 13106 if (!user_breakpoint_p (tp))
6149aea9
PA
13107 continue;
13108
13109 /* If we have a filter, only save the breakpoints it accepts. */
13110 if (filter && !filter (tp))
13111 continue;
13112
13113 any = 1;
13114
13115 if (is_tracepoint (tp))
13116 {
13117 extra_trace_bits = 1;
13118
13119 /* We can stop searching. */
13120 break;
13121 }
1042e4c0 13122 }
6149aea9
PA
13123
13124 if (!any)
1042e4c0 13125 {
6149aea9 13126 warning (_("Nothing to save."));
1042e4c0
SS
13127 return;
13128 }
13129
6149aea9 13130 pathname = tilde_expand (filename);
1042e4c0 13131 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 13132 fp = gdb_fopen (pathname, "w");
059fb39f 13133 if (!fp)
6149aea9
PA
13134 error (_("Unable to open file '%s' for saving (%s)"),
13135 filename, safe_strerror (errno));
a7bdde9e 13136 make_cleanup_ui_file_delete (fp);
8bf6485c 13137
6149aea9
PA
13138 if (extra_trace_bits)
13139 save_trace_state_variables (fp);
8bf6485c 13140
6149aea9 13141 ALL_BREAKPOINTS (tp)
1042e4c0 13142 {
6149aea9 13143 /* Skip internal and momentary breakpoints. */
09d682a4 13144 if (!user_breakpoint_p (tp))
6149aea9 13145 continue;
8bf6485c 13146
6149aea9
PA
13147 /* If we have a filter, only save the breakpoints it accepts. */
13148 if (filter && !filter (tp))
13149 continue;
13150
348d480f 13151 tp->ops->print_recreate (tp, fp);
1042e4c0 13152
6149aea9
PA
13153 /* Note, we can't rely on tp->number for anything, as we can't
13154 assume the recreated breakpoint numbers will match. Use $bpnum
13155 instead. */
13156
13157 if (tp->cond_string)
13158 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
13159
13160 if (tp->ignore_count)
13161 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
13162
a7bdde9e 13163 if (tp->commands)
1042e4c0 13164 {
a7bdde9e
VP
13165 volatile struct gdb_exception ex;
13166
6149aea9 13167 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 13168
79a45e25 13169 ui_out_redirect (current_uiout, fp);
14dba4b4 13170 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 13171 {
79a45e25 13172 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 13173 }
79a45e25 13174 ui_out_redirect (current_uiout, NULL);
1042e4c0 13175
a7bdde9e
VP
13176 if (ex.reason < 0)
13177 throw_exception (ex);
1042e4c0 13178
a7bdde9e 13179 fprintf_unfiltered (fp, " end\n");
1042e4c0 13180 }
6149aea9
PA
13181
13182 if (tp->enable_state == bp_disabled)
13183 fprintf_unfiltered (fp, "disable\n");
13184
13185 /* If this is a multi-location breakpoint, check if the locations
13186 should be individually disabled. Watchpoint locations are
13187 special, and not user visible. */
13188 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13189 {
13190 struct bp_location *loc;
13191 int n = 1;
13192
13193 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
13194 if (!loc->enabled)
13195 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
13196 }
1042e4c0 13197 }
8bf6485c 13198
6149aea9 13199 if (extra_trace_bits && *default_collect)
8bf6485c
SS
13200 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
13201
1042e4c0
SS
13202 do_cleanups (cleanup);
13203 if (from_tty)
6149aea9
PA
13204 printf_filtered (_("Saved to file '%s'.\n"), filename);
13205}
13206
13207/* The `save breakpoints' command. */
13208
13209static void
13210save_breakpoints_command (char *args, int from_tty)
13211{
13212 save_breakpoints (args, from_tty, NULL);
13213}
13214
13215/* The `save tracepoints' command. */
13216
13217static void
13218save_tracepoints_command (char *args, int from_tty)
13219{
13220 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
13221}
13222
13223/* Create a vector of all tracepoints. */
13224
13225VEC(breakpoint_p) *
eeae04df 13226all_tracepoints (void)
1042e4c0
SS
13227{
13228 VEC(breakpoint_p) *tp_vec = 0;
13229 struct breakpoint *tp;
13230
13231 ALL_TRACEPOINTS (tp)
13232 {
13233 VEC_safe_push (breakpoint_p, tp_vec, tp);
13234 }
13235
13236 return tp_vec;
13237}
13238
c906108c 13239\f
4a64f543
MS
13240/* This help string is used for the break, hbreak, tbreak and thbreak
13241 commands. It is defined as a macro to prevent duplication.
13242 COMMAND should be a string constant containing the name of the
13243 command. */
31e2b00f
AS
13244#define BREAK_ARGS_HELP(command) \
13245command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
13246LOCATION may be a line number, function name, or \"*\" and an address.\n\
13247If a line number is specified, break at start of code for that line.\n\
13248If a function is specified, break at start of code for that function.\n\
13249If an address is specified, break at that exact address.\n\
dc10affe
PA
13250With no LOCATION, uses current execution address of the selected\n\
13251stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
13252\n\
13253THREADNUM is the number from \"info threads\".\n\
13254CONDITION is a boolean expression.\n\
13255\n\
d41c0fc8
PA
13256Multiple breakpoints at one place are permitted, and useful if their\n\
13257conditions are different.\n\
31e2b00f
AS
13258\n\
13259Do \"help breakpoints\" for info on other commands dealing with breakpoints."
13260
44feb3ce
TT
13261/* List of subcommands for "catch". */
13262static struct cmd_list_element *catch_cmdlist;
13263
13264/* List of subcommands for "tcatch". */
13265static struct cmd_list_element *tcatch_cmdlist;
13266
9ac4176b 13267void
44feb3ce
TT
13268add_catch_command (char *name, char *docstring,
13269 void (*sfunc) (char *args, int from_tty,
13270 struct cmd_list_element *command),
a96d9b2e
SDJ
13271 char **(*completer) (struct cmd_list_element *cmd,
13272 char *text, char *word),
44feb3ce
TT
13273 void *user_data_catch,
13274 void *user_data_tcatch)
13275{
13276 struct cmd_list_element *command;
13277
13278 command = add_cmd (name, class_breakpoint, NULL, docstring,
13279 &catch_cmdlist);
13280 set_cmd_sfunc (command, sfunc);
13281 set_cmd_context (command, user_data_catch);
a96d9b2e 13282 set_cmd_completer (command, completer);
44feb3ce
TT
13283
13284 command = add_cmd (name, class_breakpoint, NULL, docstring,
13285 &tcatch_cmdlist);
13286 set_cmd_sfunc (command, sfunc);
13287 set_cmd_context (command, user_data_tcatch);
a96d9b2e 13288 set_cmd_completer (command, completer);
44feb3ce
TT
13289}
13290
6c95b8df 13291static void
a79b8f6e 13292clear_syscall_counts (struct inferior *inf)
6c95b8df 13293{
6c95b8df
PA
13294 inf->total_syscalls_count = 0;
13295 inf->any_syscall_count = 0;
13296 VEC_free (int, inf->syscalls_counts);
13297}
13298
6149aea9
PA
13299static void
13300save_command (char *arg, int from_tty)
13301{
3e43a32a
MS
13302 printf_unfiltered (_("\"save\" must be followed by "
13303 "the name of a save subcommand.\n"));
6149aea9
PA
13304 help_list (save_cmdlist, "save ", -1, gdb_stdout);
13305}
13306
84f4c1fe
PM
13307struct breakpoint *
13308iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
13309 void *data)
13310{
35df4500 13311 struct breakpoint *b, *b_tmp;
84f4c1fe 13312
35df4500 13313 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
13314 {
13315 if ((*callback) (b, data))
13316 return b;
13317 }
13318
13319 return NULL;
13320}
13321
0574c78f
GB
13322/* Zero if any of the breakpoint's locations could be a location where
13323 functions have been inlined, nonzero otherwise. */
13324
13325static int
13326is_non_inline_function (struct breakpoint *b)
13327{
13328 /* The shared library event breakpoint is set on the address of a
13329 non-inline function. */
13330 if (b->type == bp_shlib_event)
13331 return 1;
13332
13333 return 0;
13334}
13335
13336/* Nonzero if the specified PC cannot be a location where functions
13337 have been inlined. */
13338
13339int
13340pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc)
13341{
13342 struct breakpoint *b;
13343 struct bp_location *bl;
13344
13345 ALL_BREAKPOINTS (b)
13346 {
13347 if (!is_non_inline_function (b))
13348 continue;
13349
13350 for (bl = b->loc; bl != NULL; bl = bl->next)
13351 {
13352 if (!bl->shlib_disabled
13353 && bpstat_check_location (bl, aspace, pc))
13354 return 1;
13355 }
13356 }
13357
13358 return 0;
13359}
13360
2060206e
PA
13361void
13362initialize_breakpoint_ops (void)
13363{
13364 static int initialized = 0;
13365
13366 struct breakpoint_ops *ops;
13367
13368 if (initialized)
13369 return;
13370 initialized = 1;
13371
13372 /* The breakpoint_ops structure to be inherit by all kinds of
13373 breakpoints (real breakpoints, i.e., user "break" breakpoints,
13374 internal and momentary breakpoints, etc.). */
13375 ops = &bkpt_base_breakpoint_ops;
13376 *ops = base_breakpoint_ops;
13377 ops->re_set = bkpt_re_set;
13378 ops->insert_location = bkpt_insert_location;
13379 ops->remove_location = bkpt_remove_location;
13380 ops->breakpoint_hit = bkpt_breakpoint_hit;
13381
13382 /* The breakpoint_ops structure to be used in regular breakpoints. */
13383 ops = &bkpt_breakpoint_ops;
13384 *ops = bkpt_base_breakpoint_ops;
13385 ops->re_set = bkpt_re_set;
13386 ops->resources_needed = bkpt_resources_needed;
13387 ops->print_it = bkpt_print_it;
13388 ops->print_mention = bkpt_print_mention;
13389 ops->print_recreate = bkpt_print_recreate;
13390
13391 /* Ranged breakpoints. */
13392 ops = &ranged_breakpoint_ops;
13393 *ops = bkpt_breakpoint_ops;
13394 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
13395 ops->resources_needed = resources_needed_ranged_breakpoint;
13396 ops->print_it = print_it_ranged_breakpoint;
13397 ops->print_one = print_one_ranged_breakpoint;
13398 ops->print_one_detail = print_one_detail_ranged_breakpoint;
13399 ops->print_mention = print_mention_ranged_breakpoint;
13400 ops->print_recreate = print_recreate_ranged_breakpoint;
13401
13402 /* Internal breakpoints. */
13403 ops = &internal_breakpoint_ops;
13404 *ops = bkpt_base_breakpoint_ops;
13405 ops->re_set = internal_bkpt_re_set;
13406 ops->check_status = internal_bkpt_check_status;
13407 ops->print_it = internal_bkpt_print_it;
13408 ops->print_mention = internal_bkpt_print_mention;
13409
13410 /* Momentary breakpoints. */
13411 ops = &momentary_breakpoint_ops;
13412 *ops = bkpt_base_breakpoint_ops;
13413 ops->re_set = momentary_bkpt_re_set;
13414 ops->check_status = momentary_bkpt_check_status;
13415 ops->print_it = momentary_bkpt_print_it;
13416 ops->print_mention = momentary_bkpt_print_mention;
13417
13418 /* GNU v3 exception catchpoints. */
13419 ops = &gnu_v3_exception_catchpoint_ops;
13420 *ops = bkpt_breakpoint_ops;
13421 ops->print_it = print_it_exception_catchpoint;
13422 ops->print_one = print_one_exception_catchpoint;
13423 ops->print_mention = print_mention_exception_catchpoint;
13424 ops->print_recreate = print_recreate_exception_catchpoint;
13425
13426 /* Watchpoints. */
13427 ops = &watchpoint_breakpoint_ops;
13428 *ops = base_breakpoint_ops;
3a5c3e22 13429 ops->dtor = dtor_watchpoint;
2060206e
PA
13430 ops->re_set = re_set_watchpoint;
13431 ops->insert_location = insert_watchpoint;
13432 ops->remove_location = remove_watchpoint;
13433 ops->breakpoint_hit = breakpoint_hit_watchpoint;
13434 ops->check_status = check_status_watchpoint;
13435 ops->resources_needed = resources_needed_watchpoint;
13436 ops->works_in_software_mode = works_in_software_mode_watchpoint;
13437 ops->print_it = print_it_watchpoint;
13438 ops->print_mention = print_mention_watchpoint;
13439 ops->print_recreate = print_recreate_watchpoint;
13440
13441 /* Masked watchpoints. */
13442 ops = &masked_watchpoint_breakpoint_ops;
13443 *ops = watchpoint_breakpoint_ops;
13444 ops->insert_location = insert_masked_watchpoint;
13445 ops->remove_location = remove_masked_watchpoint;
13446 ops->resources_needed = resources_needed_masked_watchpoint;
13447 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
13448 ops->print_it = print_it_masked_watchpoint;
13449 ops->print_one_detail = print_one_detail_masked_watchpoint;
13450 ops->print_mention = print_mention_masked_watchpoint;
13451 ops->print_recreate = print_recreate_masked_watchpoint;
13452
13453 /* Tracepoints. */
13454 ops = &tracepoint_breakpoint_ops;
13455 *ops = base_breakpoint_ops;
13456 ops->re_set = tracepoint_re_set;
13457 ops->breakpoint_hit = tracepoint_breakpoint_hit;
13458 ops->print_one_detail = tracepoint_print_one_detail;
13459 ops->print_mention = tracepoint_print_mention;
13460 ops->print_recreate = tracepoint_print_recreate;
13461
13462 /* Fork catchpoints. */
13463 ops = &catch_fork_breakpoint_ops;
13464 *ops = base_breakpoint_ops;
13465 ops->insert_location = insert_catch_fork;
13466 ops->remove_location = remove_catch_fork;
13467 ops->breakpoint_hit = breakpoint_hit_catch_fork;
13468 ops->print_it = print_it_catch_fork;
13469 ops->print_one = print_one_catch_fork;
13470 ops->print_mention = print_mention_catch_fork;
13471 ops->print_recreate = print_recreate_catch_fork;
13472
13473 /* Vfork catchpoints. */
13474 ops = &catch_vfork_breakpoint_ops;
13475 *ops = base_breakpoint_ops;
13476 ops->insert_location = insert_catch_vfork;
13477 ops->remove_location = remove_catch_vfork;
13478 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
13479 ops->print_it = print_it_catch_vfork;
13480 ops->print_one = print_one_catch_vfork;
13481 ops->print_mention = print_mention_catch_vfork;
13482 ops->print_recreate = print_recreate_catch_vfork;
13483
13484 /* Exec catchpoints. */
13485 ops = &catch_exec_breakpoint_ops;
13486 *ops = base_breakpoint_ops;
13487 ops->dtor = dtor_catch_exec;
13488 ops->insert_location = insert_catch_exec;
13489 ops->remove_location = remove_catch_exec;
13490 ops->breakpoint_hit = breakpoint_hit_catch_exec;
13491 ops->print_it = print_it_catch_exec;
13492 ops->print_one = print_one_catch_exec;
13493 ops->print_mention = print_mention_catch_exec;
13494 ops->print_recreate = print_recreate_catch_exec;
13495
13496 /* Syscall catchpoints. */
13497 ops = &catch_syscall_breakpoint_ops;
13498 *ops = base_breakpoint_ops;
13499 ops->dtor = dtor_catch_syscall;
13500 ops->insert_location = insert_catch_syscall;
13501 ops->remove_location = remove_catch_syscall;
13502 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
13503 ops->print_it = print_it_catch_syscall;
13504 ops->print_one = print_one_catch_syscall;
13505 ops->print_mention = print_mention_catch_syscall;
13506 ops->print_recreate = print_recreate_catch_syscall;
13507}
13508
c906108c 13509void
fba45db2 13510_initialize_breakpoint (void)
c906108c
SS
13511{
13512 struct cmd_list_element *c;
13513
2060206e
PA
13514 initialize_breakpoint_ops ();
13515
84acb35a 13516 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 13517 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 13518 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 13519
17450429
PP
13520 breakpoint_objfile_key = register_objfile_data ();
13521
c906108c
SS
13522 breakpoint_chain = 0;
13523 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
13524 before a breakpoint is set. */
13525 breakpoint_count = 0;
13526
1042e4c0
SS
13527 tracepoint_count = 0;
13528
1bedd215
AC
13529 add_com ("ignore", class_breakpoint, ignore_command, _("\
13530Set ignore-count of breakpoint number N to COUNT.\n\
13531Usage is `ignore N COUNT'."));
c906108c 13532 if (xdb_commands)
c5aa993b 13533 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 13534
1bedd215
AC
13535 add_com ("commands", class_breakpoint, commands_command, _("\
13536Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
13537Give breakpoint number as argument after \"commands\".\n\
13538With no argument, the targeted breakpoint is the last one set.\n\
13539The commands themselves follow starting on the next line.\n\
13540Type a line containing \"end\" to indicate the end of them.\n\
13541Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 13542then no output is printed when it is hit, except what the commands print."));
c906108c 13543
1bedd215
AC
13544 add_com ("condition", class_breakpoint, condition_command, _("\
13545Specify breakpoint number N to break only if COND is true.\n\
c906108c 13546Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 13547expression to be evaluated whenever breakpoint N is reached."));
c906108c 13548
1bedd215 13549 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 13550Set a temporary breakpoint.\n\
c906108c
SS
13551Like \"break\" except the breakpoint is only temporary,\n\
13552so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
13553by using \"enable delete\" on the breakpoint number.\n\
13554\n"
13555BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 13556 set_cmd_completer (c, location_completer);
c94fdfd0 13557
1bedd215 13558 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 13559Set a hardware assisted breakpoint.\n\
c906108c 13560Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
13561some target hardware may not have this support.\n\
13562\n"
13563BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 13564 set_cmd_completer (c, location_completer);
c906108c 13565
1bedd215 13566 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 13567Set a temporary hardware assisted breakpoint.\n\
c906108c 13568Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
13569so it will be deleted when hit.\n\
13570\n"
13571BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 13572 set_cmd_completer (c, location_completer);
c906108c 13573
1bedd215
AC
13574 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
13575Enable some breakpoints.\n\
c906108c
SS
13576Give breakpoint numbers (separated by spaces) as arguments.\n\
13577With no subcommand, breakpoints are enabled until you command otherwise.\n\
13578This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13579With a subcommand you can enable temporarily."),
c906108c
SS
13580 &enablelist, "enable ", 1, &cmdlist);
13581 if (xdb_commands)
1bedd215
AC
13582 add_com ("ab", class_breakpoint, enable_command, _("\
13583Enable some breakpoints.\n\
c906108c
SS
13584Give breakpoint numbers (separated by spaces) as arguments.\n\
13585With no subcommand, breakpoints are enabled until you command otherwise.\n\
13586This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13587With a subcommand you can enable temporarily."));
c906108c
SS
13588
13589 add_com_alias ("en", "enable", class_breakpoint, 1);
13590
84951ab5 13591 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 13592Enable some breakpoints.\n\
c906108c
SS
13593Give breakpoint numbers (separated by spaces) as arguments.\n\
13594This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13595May be abbreviated to simply \"enable\".\n"),
c5aa993b 13596 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 13597
1a966eab
AC
13598 add_cmd ("once", no_class, enable_once_command, _("\
13599Enable breakpoints for one hit. Give breakpoint numbers.\n\
13600If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13601 &enablebreaklist);
13602
1a966eab
AC
13603 add_cmd ("delete", no_class, enable_delete_command, _("\
13604Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13605If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13606 &enablebreaklist);
13607
1a966eab
AC
13608 add_cmd ("delete", no_class, enable_delete_command, _("\
13609Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13610If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13611 &enablelist);
13612
1a966eab
AC
13613 add_cmd ("once", no_class, enable_once_command, _("\
13614Enable breakpoints for one hit. Give breakpoint numbers.\n\
13615If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13616 &enablelist);
13617
1bedd215
AC
13618 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13619Disable some breakpoints.\n\
c906108c
SS
13620Arguments are breakpoint numbers with spaces in between.\n\
13621To disable all breakpoints, give no argument.\n\
64b9b334 13622A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
13623 &disablelist, "disable ", 1, &cmdlist);
13624 add_com_alias ("dis", "disable", class_breakpoint, 1);
13625 add_com_alias ("disa", "disable", class_breakpoint, 1);
13626 if (xdb_commands)
1bedd215
AC
13627 add_com ("sb", class_breakpoint, disable_command, _("\
13628Disable some breakpoints.\n\
c906108c
SS
13629Arguments are breakpoint numbers with spaces in between.\n\
13630To disable all breakpoints, give no argument.\n\
64b9b334 13631A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 13632
1a966eab
AC
13633 add_cmd ("breakpoints", class_alias, disable_command, _("\
13634Disable some breakpoints.\n\
c906108c
SS
13635Arguments are breakpoint numbers with spaces in between.\n\
13636To disable all breakpoints, give no argument.\n\
64b9b334 13637A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 13638This command may be abbreviated \"disable\"."),
c906108c
SS
13639 &disablelist);
13640
1bedd215
AC
13641 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13642Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13643Arguments are breakpoint numbers with spaces in between.\n\
13644To delete all breakpoints, give no argument.\n\
13645\n\
13646Also a prefix command for deletion of other GDB objects.\n\
1bedd215 13647The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
13648 &deletelist, "delete ", 1, &cmdlist);
13649 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 13650 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 13651 if (xdb_commands)
1bedd215
AC
13652 add_com ("db", class_breakpoint, delete_command, _("\
13653Delete some breakpoints.\n\
c906108c 13654Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 13655To delete all breakpoints, give no argument.\n"));
c906108c 13656
1a966eab
AC
13657 add_cmd ("breakpoints", class_alias, delete_command, _("\
13658Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13659Arguments are breakpoint numbers with spaces in between.\n\
13660To delete all breakpoints, give no argument.\n\
1a966eab 13661This command may be abbreviated \"delete\"."),
c906108c
SS
13662 &deletelist);
13663
1bedd215
AC
13664 add_com ("clear", class_breakpoint, clear_command, _("\
13665Clear breakpoint at specified line or function.\n\
c906108c
SS
13666Argument may be line number, function name, or \"*\" and an address.\n\
13667If line number is specified, all breakpoints in that line are cleared.\n\
13668If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
13669If an address is specified, breakpoints at that address are cleared.\n\
13670\n\
13671With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
13672is executing in.\n\
13673\n\
1bedd215 13674See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 13675 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 13676
1bedd215 13677 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
13678Set breakpoint at specified line or function.\n"
13679BREAK_ARGS_HELP ("break")));
5ba2abeb 13680 set_cmd_completer (c, location_completer);
c94fdfd0 13681
c906108c
SS
13682 add_com_alias ("b", "break", class_run, 1);
13683 add_com_alias ("br", "break", class_run, 1);
13684 add_com_alias ("bre", "break", class_run, 1);
13685 add_com_alias ("brea", "break", class_run, 1);
13686
7681d515
PM
13687 if (xdb_commands)
13688 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
13689
13690 if (dbx_commands)
13691 {
1bedd215
AC
13692 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13693Break in function/address or break at a line in the current file."),
c5aa993b
JM
13694 &stoplist, "stop ", 1, &cmdlist);
13695 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 13696 _("Break in function or address."), &stoplist);
c5aa993b 13697 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 13698 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
13699 add_com ("status", class_info, breakpoints_info, _("\
13700Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13701The \"Type\" column indicates one of:\n\
13702\tbreakpoint - normal breakpoint\n\
13703\twatchpoint - watchpoint\n\
13704The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13705the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13706breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13707address and file/line number respectively.\n\
13708\n\
13709Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13710are set to the address of the last breakpoint listed unless the command\n\
13711is prefixed with \"server \".\n\n\
c906108c 13712Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13713breakpoint set."));
c906108c
SS
13714 }
13715
1bedd215 13716 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 13717Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
13718The \"Type\" column indicates one of:\n\
13719\tbreakpoint - normal breakpoint\n\
13720\twatchpoint - watchpoint\n\
13721The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13722the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13723breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13724address and file/line number respectively.\n\
13725\n\
13726Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13727are set to the address of the last breakpoint listed unless the command\n\
13728is prefixed with \"server \".\n\n\
c906108c 13729Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13730breakpoint set."));
c906108c 13731
6b04bdb7
MS
13732 add_info_alias ("b", "breakpoints", 1);
13733
c906108c 13734 if (xdb_commands)
1bedd215
AC
13735 add_com ("lb", class_breakpoint, breakpoints_info, _("\
13736Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13737The \"Type\" column indicates one of:\n\
13738\tbreakpoint - normal breakpoint\n\
13739\twatchpoint - watchpoint\n\
13740The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13741the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13742breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13743address and file/line number respectively.\n\
13744\n\
13745Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13746are set to the address of the last breakpoint listed unless the command\n\
13747is prefixed with \"server \".\n\n\
c906108c 13748Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13749breakpoint set."));
c906108c 13750
1a966eab
AC
13751 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13752Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13753The \"Type\" column indicates one of:\n\
13754\tbreakpoint - normal breakpoint\n\
13755\twatchpoint - watchpoint\n\
13756\tlongjmp - internal breakpoint used to step through longjmp()\n\
13757\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13758\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
13759\tfinish - internal breakpoint used by the \"finish\" command\n\
13760The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
13761the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13762breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
13763address and file/line number respectively.\n\
13764\n\
13765Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13766are set to the address of the last breakpoint listed unless the command\n\
13767is prefixed with \"server \".\n\n\
c906108c 13768Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 13769breakpoint set."),
c906108c
SS
13770 &maintenanceinfolist);
13771
44feb3ce
TT
13772 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13773Set catchpoints to catch events."),
13774 &catch_cmdlist, "catch ",
13775 0/*allow-unknown*/, &cmdlist);
13776
13777 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13778Set temporary catchpoints to catch events."),
13779 &tcatch_cmdlist, "tcatch ",
13780 0/*allow-unknown*/, &cmdlist);
13781
13782 /* Add catch and tcatch sub-commands. */
13783 add_catch_command ("catch", _("\
13784Catch an exception, when caught.\n\
13785With an argument, catch only exceptions with the given name."),
13786 catch_catch_command,
a96d9b2e 13787 NULL,
44feb3ce
TT
13788 CATCH_PERMANENT,
13789 CATCH_TEMPORARY);
13790 add_catch_command ("throw", _("\
13791Catch an exception, when thrown.\n\
13792With an argument, catch only exceptions with the given name."),
13793 catch_throw_command,
a96d9b2e 13794 NULL,
44feb3ce
TT
13795 CATCH_PERMANENT,
13796 CATCH_TEMPORARY);
13797 add_catch_command ("fork", _("Catch calls to fork."),
13798 catch_fork_command_1,
a96d9b2e 13799 NULL,
44feb3ce
TT
13800 (void *) (uintptr_t) catch_fork_permanent,
13801 (void *) (uintptr_t) catch_fork_temporary);
13802 add_catch_command ("vfork", _("Catch calls to vfork."),
13803 catch_fork_command_1,
a96d9b2e 13804 NULL,
44feb3ce
TT
13805 (void *) (uintptr_t) catch_vfork_permanent,
13806 (void *) (uintptr_t) catch_vfork_temporary);
13807 add_catch_command ("exec", _("Catch calls to exec."),
13808 catch_exec_command_1,
a96d9b2e
SDJ
13809 NULL,
13810 CATCH_PERMANENT,
13811 CATCH_TEMPORARY);
13812 add_catch_command ("syscall", _("\
13813Catch system calls by their names and/or numbers.\n\
13814Arguments say which system calls to catch. If no arguments\n\
13815are given, every system call will be caught.\n\
13816Arguments, if given, should be one or more system call names\n\
13817(if your system supports that), or system call numbers."),
13818 catch_syscall_command_1,
13819 catch_syscall_completer,
44feb3ce
TT
13820 CATCH_PERMANENT,
13821 CATCH_TEMPORARY);
c5aa993b 13822
1bedd215
AC
13823 c = add_com ("watch", class_breakpoint, watch_command, _("\
13824Set a watchpoint for an expression.\n\
06a64a0b 13825Usage: watch [-l|-location] EXPRESSION\n\
c906108c 13826A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13827an expression changes.\n\
13828If -l or -location is given, this evaluates EXPRESSION and watches\n\
13829the memory to which it refers."));
65d12d83 13830 set_cmd_completer (c, expression_completer);
c906108c 13831
1bedd215
AC
13832 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13833Set a read watchpoint for an expression.\n\
06a64a0b 13834Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 13835A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13836an expression is read.\n\
13837If -l or -location is given, this evaluates EXPRESSION and watches\n\
13838the memory to which it refers."));
65d12d83 13839 set_cmd_completer (c, expression_completer);
c906108c 13840
1bedd215
AC
13841 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13842Set a watchpoint for an expression.\n\
06a64a0b 13843Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 13844A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13845an expression is either read or written.\n\
13846If -l or -location is given, this evaluates EXPRESSION and watches\n\
13847the memory to which it refers."));
65d12d83 13848 set_cmd_completer (c, expression_completer);
c906108c 13849
d77f58be 13850 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 13851Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 13852
920d2a44
AC
13853 /* XXX: cagney/2005-02-23: This should be a boolean, and should
13854 respond to changes - contrary to the description. */
85c07804
AC
13855 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
13856 &can_use_hw_watchpoints, _("\
13857Set debugger's willingness to use watchpoint hardware."), _("\
13858Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
13859If zero, gdb will not use hardware for new watchpoints, even if\n\
13860such is available. (However, any hardware watchpoints that were\n\
13861created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
13862hardware.)"),
13863 NULL,
920d2a44 13864 show_can_use_hw_watchpoints,
85c07804 13865 &setlist, &showlist);
c906108c
SS
13866
13867 can_use_hw_watchpoints = 1;
fa8d40ab 13868
1042e4c0
SS
13869 /* Tracepoint manipulation commands. */
13870
13871 c = add_com ("trace", class_breakpoint, trace_command, _("\
13872Set a tracepoint at specified line or function.\n\
13873\n"
13874BREAK_ARGS_HELP ("trace") "\n\
13875Do \"help tracepoints\" for info on other tracepoint commands."));
13876 set_cmd_completer (c, location_completer);
13877
13878 add_com_alias ("tp", "trace", class_alias, 0);
13879 add_com_alias ("tr", "trace", class_alias, 1);
13880 add_com_alias ("tra", "trace", class_alias, 1);
13881 add_com_alias ("trac", "trace", class_alias, 1);
13882
7a697b8d
SS
13883 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
13884Set a fast tracepoint at specified line or function.\n\
13885\n"
13886BREAK_ARGS_HELP ("ftrace") "\n\
13887Do \"help tracepoints\" for info on other tracepoint commands."));
13888 set_cmd_completer (c, location_completer);
13889
0fb4aa4b
PA
13890 c = add_com ("strace", class_breakpoint, strace_command, _("\
13891Set a static tracepoint at specified line, function or marker.\n\
13892\n\
13893strace [LOCATION] [if CONDITION]\n\
13894LOCATION may be a line number, function name, \"*\" and an address,\n\
13895or -m MARKER_ID.\n\
13896If a line number is specified, probe the marker at start of code\n\
13897for that line. If a function is specified, probe the marker at start\n\
13898of code for that function. If an address is specified, probe the marker\n\
13899at that exact address. If a marker id is specified, probe the marker\n\
13900with that name. With no LOCATION, uses current execution address of\n\
13901the selected stack frame.\n\
13902Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
13903This collects arbitrary user data passed in the probe point call to the\n\
13904tracing library. You can inspect it when analyzing the trace buffer,\n\
13905by printing the $_sdata variable like any other convenience variable.\n\
13906\n\
13907CONDITION is a boolean expression.\n\
13908\n\
d41c0fc8
PA
13909Multiple tracepoints at one place are permitted, and useful if their\n\
13910conditions are different.\n\
0fb4aa4b
PA
13911\n\
13912Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
13913Do \"help tracepoints\" for info on other tracepoint commands."));
13914 set_cmd_completer (c, location_completer);
13915
1042e4c0 13916 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 13917Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
13918Convenience variable \"$tpnum\" contains the number of the\n\
13919last tracepoint set."));
13920
13921 add_info_alias ("tp", "tracepoints", 1);
13922
13923 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
13924Delete specified tracepoints.\n\
13925Arguments are tracepoint numbers, separated by spaces.\n\
13926No argument means delete all tracepoints."),
13927 &deletelist);
13928
13929 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13930Disable specified tracepoints.\n\
13931Arguments are tracepoint numbers, separated by spaces.\n\
13932No argument means disable all tracepoints."),
13933 &disablelist);
13934 deprecate_cmd (c, "disable");
13935
13936 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
13937Enable specified tracepoints.\n\
13938Arguments are tracepoint numbers, separated by spaces.\n\
13939No argument means enable all tracepoints."),
13940 &enablelist);
13941 deprecate_cmd (c, "enable");
13942
13943 add_com ("passcount", class_trace, trace_pass_command, _("\
13944Set the passcount for a tracepoint.\n\
13945The trace will end when the tracepoint has been passed 'count' times.\n\
13946Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
13947if TPNUM is omitted, passcount refers to the last tracepoint defined."));
13948
6149aea9
PA
13949 add_prefix_cmd ("save", class_breakpoint, save_command,
13950 _("Save breakpoint definitions as a script."),
13951 &save_cmdlist, "save ",
13952 0/*allow-unknown*/, &cmdlist);
13953
13954 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
13955Save current breakpoint definitions as a script.\n\
cce7e648 13956This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
13957catchpoints, tracepoints). Use the 'source' command in another debug\n\
13958session to restore them."),
13959 &save_cmdlist);
13960 set_cmd_completer (c, filename_completer);
13961
13962 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 13963Save current tracepoint definitions as a script.\n\
6149aea9
PA
13964Use the 'source' command in another debug session to restore them."),
13965 &save_cmdlist);
1042e4c0
SS
13966 set_cmd_completer (c, filename_completer);
13967
6149aea9
PA
13968 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
13969 deprecate_cmd (c, "save tracepoints");
13970
1bedd215 13971 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
13972Breakpoint specific settings\n\
13973Configure various breakpoint-specific variables such as\n\
1bedd215 13974pending breakpoint behavior"),
fa8d40ab
JJ
13975 &breakpoint_set_cmdlist, "set breakpoint ",
13976 0/*allow-unknown*/, &setlist);
1bedd215 13977 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
13978Breakpoint specific settings\n\
13979Configure various breakpoint-specific variables such as\n\
1bedd215 13980pending breakpoint behavior"),
fa8d40ab
JJ
13981 &breakpoint_show_cmdlist, "show breakpoint ",
13982 0/*allow-unknown*/, &showlist);
13983
7915a72c
AC
13984 add_setshow_auto_boolean_cmd ("pending", no_class,
13985 &pending_break_support, _("\
13986Set debugger's behavior regarding pending breakpoints."), _("\
13987Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
13988If on, an unrecognized breakpoint location will cause gdb to create a\n\
13989pending breakpoint. If off, an unrecognized breakpoint location results in\n\
13990an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 13991user-query to see if a pending breakpoint should be created."),
2c5b56ce 13992 NULL,
920d2a44 13993 show_pending_break_support,
6e1d7d6c
AC
13994 &breakpoint_set_cmdlist,
13995 &breakpoint_show_cmdlist);
fa8d40ab
JJ
13996
13997 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
13998
13999 add_setshow_boolean_cmd ("auto-hw", no_class,
14000 &automatic_hardware_breakpoints, _("\
14001Set automatic usage of hardware breakpoints."), _("\
14002Show automatic usage of hardware breakpoints."), _("\
14003If set, the debugger will automatically use hardware breakpoints for\n\
14004breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14005a warning will be emitted for such breakpoints."),
14006 NULL,
14007 show_automatic_hardware_breakpoints,
14008 &breakpoint_set_cmdlist,
14009 &breakpoint_show_cmdlist);
74960c60 14010
33e5cbd6
PA
14011 add_setshow_enum_cmd ("always-inserted", class_support,
14012 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
14013Set mode for inserting breakpoints."), _("\
14014Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
14015When this mode is off, breakpoints are inserted in inferior when it is\n\
14016resumed, and removed when execution stops. When this mode is on,\n\
14017breakpoints are inserted immediately and removed only when the user\n\
14018deletes the breakpoint. When this mode is auto (which is the default),\n\
14019the behaviour depends on the non-stop setting (see help set non-stop).\n\
14020In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
14021behaves as if always-inserted mode is on; if gdb is controlling the\n\
14022inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
14023 NULL,
14024 &show_always_inserted_mode,
14025 &breakpoint_set_cmdlist,
14026 &breakpoint_show_cmdlist);
f1310107
TJB
14027
14028 add_com ("break-range", class_breakpoint, break_range_command, _("\
14029Set a breakpoint for an address range.\n\
14030break-range START-LOCATION, END-LOCATION\n\
14031where START-LOCATION and END-LOCATION can be one of the following:\n\
14032 LINENUM, for that line in the current file,\n\
14033 FILE:LINENUM, for that line in that file,\n\
14034 +OFFSET, for that number of lines after the current line\n\
14035 or the start of the range\n\
14036 FUNCTION, for the first line in that function,\n\
14037 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14038 *ADDRESS, for the instruction at that address.\n\
14039\n\
14040The breakpoint will stop execution of the inferior whenever it executes\n\
14041an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14042range (including START-LOCATION and END-LOCATION)."));
14043
765dc015 14044 automatic_hardware_breakpoints = 1;
f3b1572e
PA
14045
14046 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 14047}
This page took 2.1131 seconds and 4 git commands to generate.