2011-12-16 Phil Muldoon <pmuldoon@redhat.com>
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1 4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7b6bb8da 5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
a6d9a66e 23#include "arch-utils.h"
c906108c 24#include <ctype.h>
776592bf 25#include "hashtab.h"
c906108c
SS
26#include "symtab.h"
27#include "frame.h"
28#include "breakpoint.h"
1042e4c0 29#include "tracepoint.h"
c906108c
SS
30#include "gdbtypes.h"
31#include "expression.h"
32#include "gdbcore.h"
33#include "gdbcmd.h"
34#include "value.h"
35#include "command.h"
36#include "inferior.h"
37#include "gdbthread.h"
38#include "target.h"
39#include "language.h"
40#include "gdb_string.h"
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 1051void
f0ba3972
PA
1052breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1053 const gdb_byte *writebuf_org,
1054 ULONGEST memaddr, LONGEST len)
c906108c 1055{
4a64f543
MS
1056 /* Left boundary, right boundary and median element of our binary
1057 search. */
876fa593
JK
1058 unsigned bc_l, bc_r, bc;
1059
4a64f543
MS
1060 /* Find BC_L which is a leftmost element which may affect BUF
1061 content. It is safe to report lower value but a failure to
1062 report higher one. */
876fa593
JK
1063
1064 bc_l = 0;
1065 bc_r = bp_location_count;
1066 while (bc_l + 1 < bc_r)
1067 {
35df4500 1068 struct bp_location *bl;
876fa593
JK
1069
1070 bc = (bc_l + bc_r) / 2;
35df4500 1071 bl = bp_location[bc];
876fa593 1072
4a64f543
MS
1073 /* Check first BL->ADDRESS will not overflow due to the added
1074 constant. Then advance the left boundary only if we are sure
1075 the BC element can in no way affect the BUF content (MEMADDR
1076 to MEMADDR + LEN range).
876fa593 1077
4a64f543
MS
1078 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1079 offset so that we cannot miss a breakpoint with its shadow
1080 range tail still reaching MEMADDR. */
c5aa993b 1081
35df4500
TJB
1082 if ((bl->address + bp_location_shadow_len_after_address_max
1083 >= bl->address)
1084 && (bl->address + bp_location_shadow_len_after_address_max
1085 <= memaddr))
876fa593
JK
1086 bc_l = bc;
1087 else
1088 bc_r = bc;
1089 }
1090
128070bb
PA
1091 /* Due to the binary search above, we need to make sure we pick the
1092 first location that's at BC_L's address. E.g., if there are
1093 multiple locations at the same address, BC_L may end up pointing
1094 at a duplicate location, and miss the "master"/"inserted"
1095 location. Say, given locations L1, L2 and L3 at addresses A and
1096 B:
1097
1098 L1@A, L2@A, L3@B, ...
1099
1100 BC_L could end up pointing at location L2, while the "master"
1101 location could be L1. Since the `loc->inserted' flag is only set
1102 on "master" locations, we'd forget to restore the shadow of L1
1103 and L2. */
1104 while (bc_l > 0
1105 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1106 bc_l--;
1107
876fa593
JK
1108 /* Now do full processing of the found relevant range of elements. */
1109
1110 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1111 {
35df4500 1112 struct bp_location *bl = bp_location[bc];
876fa593
JK
1113 CORE_ADDR bp_addr = 0;
1114 int bp_size = 0;
1115 int bptoffset = 0;
1116
35df4500
TJB
1117 /* bp_location array has BL->OWNER always non-NULL. */
1118 if (bl->owner->type == bp_none)
8a3fe4f8 1119 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1120 bl->owner->number);
ffce0d52 1121
e5dd4106 1122 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1123 content. */
1124
35df4500
TJB
1125 if (bl->address >= bp_location_placed_address_before_address_max
1126 && memaddr + len <= (bl->address
1127 - bp_location_placed_address_before_address_max))
876fa593
JK
1128 break;
1129
35df4500 1130 if (!bp_location_has_shadow (bl))
c5aa993b 1131 continue;
35df4500 1132 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1133 current_program_space->aspace, 0))
1134 continue;
1135
c5aa993b
JM
1136 /* Addresses and length of the part of the breakpoint that
1137 we need to copy. */
35df4500
TJB
1138 bp_addr = bl->target_info.placed_address;
1139 bp_size = bl->target_info.shadow_len;
8defab1a 1140
c5aa993b
JM
1141 if (bp_addr + bp_size <= memaddr)
1142 /* The breakpoint is entirely before the chunk of memory we
1143 are reading. */
1144 continue;
8defab1a 1145
c5aa993b
JM
1146 if (bp_addr >= memaddr + len)
1147 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1148 reading. */
c5aa993b 1149 continue;
c5aa993b 1150
8defab1a
DJ
1151 /* Offset within shadow_contents. */
1152 if (bp_addr < memaddr)
1153 {
1154 /* Only copy the second part of the breakpoint. */
1155 bp_size -= memaddr - bp_addr;
1156 bptoffset = memaddr - bp_addr;
1157 bp_addr = memaddr;
1158 }
c5aa993b 1159
8defab1a
DJ
1160 if (bp_addr + bp_size > memaddr + len)
1161 {
1162 /* Only copy the first part of the breakpoint. */
1163 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1164 }
c5aa993b 1165
f0ba3972
PA
1166 if (readbuf != NULL)
1167 {
1168 /* Update the read buffer with this inserted breakpoint's
1169 shadow. */
1170 memcpy (readbuf + bp_addr - memaddr,
1171 bl->target_info.shadow_contents + bptoffset, bp_size);
1172 }
1173 else
1174 {
1175 struct gdbarch *gdbarch = bl->gdbarch;
1176 const unsigned char *bp;
1177 CORE_ADDR placed_address = bl->target_info.placed_address;
1178 unsigned placed_size = bl->target_info.placed_size;
1179
1180 /* Update the shadow with what we want to write to memory. */
1181 memcpy (bl->target_info.shadow_contents + bptoffset,
1182 writebuf_org + bp_addr - memaddr, bp_size);
1183
1184 /* Determine appropriate breakpoint contents and size for this
1185 address. */
1186 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1187
1188 /* Update the final write buffer with this inserted
1189 breakpoint's INSN. */
1190 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1191 }
c5aa993b 1192 }
c906108c 1193}
c906108c 1194\f
c5aa993b 1195
60e1c644
PA
1196/* Return true if BPT is of any hardware watchpoint kind. */
1197
a5606eee 1198static int
d77f58be 1199is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1200{
1201 return (bpt->type == bp_hardware_watchpoint
1202 || bpt->type == bp_read_watchpoint
1203 || bpt->type == bp_access_watchpoint);
1204}
7270d8f2 1205
60e1c644
PA
1206/* Return true if BPT is of any watchpoint kind, hardware or
1207 software. */
1208
3a5c3e22 1209int
d77f58be 1210is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1211{
1212 return (is_hardware_watchpoint (bpt)
1213 || bpt->type == bp_watchpoint);
1214}
1215
3a5c3e22
PA
1216/* Returns true if the current thread and its running state are safe
1217 to evaluate or update watchpoint B. Watchpoints on local
1218 expressions need to be evaluated in the context of the thread that
1219 was current when the watchpoint was created, and, that thread needs
1220 to be stopped to be able to select the correct frame context.
1221 Watchpoints on global expressions can be evaluated on any thread,
1222 and in any state. It is presently left to the target allowing
1223 memory accesses when threads are running. */
f6bc2008
PA
1224
1225static int
3a5c3e22 1226watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008
PA
1227{
1228 return (ptid_equal (b->watchpoint_thread, null_ptid)
1229 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1230 && !is_executing (inferior_ptid)));
1231}
1232
d0fb5eae
JK
1233/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1234 associated bp_watchpoint_scope breakpoint. */
1235
1236static void
3a5c3e22 1237watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1238{
3a5c3e22 1239 struct breakpoint *b = &w->base;
d0fb5eae
JK
1240
1241 if (b->related_breakpoint != b)
1242 {
1243 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1244 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1245 b->related_breakpoint->disposition = disp_del_at_next_stop;
1246 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1247 b->related_breakpoint = b;
1248 }
1249 b->disposition = disp_del_at_next_stop;
1250}
1251
567e1b4e
JB
1252/* Assuming that B is a watchpoint:
1253 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1254 - Evaluate expression and store the result in B->val
567e1b4e
JB
1255 - Evaluate the condition if there is one, and store the result
1256 in b->loc->cond.
a5606eee
VP
1257 - Update the list of values that must be watched in B->loc.
1258
4a64f543
MS
1259 If the watchpoint disposition is disp_del_at_next_stop, then do
1260 nothing. If this is local watchpoint that is out of scope, delete
1261 it.
1262
1263 Even with `set breakpoint always-inserted on' the watchpoints are
1264 removed + inserted on each stop here. Normal breakpoints must
1265 never be removed because they might be missed by a running thread
1266 when debugging in non-stop mode. On the other hand, hardware
1267 watchpoints (is_hardware_watchpoint; processed here) are specific
1268 to each LWP since they are stored in each LWP's hardware debug
1269 registers. Therefore, such LWP must be stopped first in order to
1270 be able to modify its hardware watchpoints.
1271
1272 Hardware watchpoints must be reset exactly once after being
1273 presented to the user. It cannot be done sooner, because it would
1274 reset the data used to present the watchpoint hit to the user. And
1275 it must not be done later because it could display the same single
1276 watchpoint hit during multiple GDB stops. Note that the latter is
1277 relevant only to the hardware watchpoint types bp_read_watchpoint
1278 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1279 not user-visible - its hit is suppressed if the memory content has
1280 not changed.
1281
1282 The following constraints influence the location where we can reset
1283 hardware watchpoints:
1284
1285 * target_stopped_by_watchpoint and target_stopped_data_address are
1286 called several times when GDB stops.
1287
1288 [linux]
1289 * Multiple hardware watchpoints can be hit at the same time,
1290 causing GDB to stop. GDB only presents one hardware watchpoint
1291 hit at a time as the reason for stopping, and all the other hits
1292 are presented later, one after the other, each time the user
1293 requests the execution to be resumed. Execution is not resumed
1294 for the threads still having pending hit event stored in
1295 LWP_INFO->STATUS. While the watchpoint is already removed from
1296 the inferior on the first stop the thread hit event is kept being
1297 reported from its cached value by linux_nat_stopped_data_address
1298 until the real thread resume happens after the watchpoint gets
1299 presented and thus its LWP_INFO->STATUS gets reset.
1300
1301 Therefore the hardware watchpoint hit can get safely reset on the
1302 watchpoint removal from inferior. */
a79d3c27 1303
b40ce68a 1304static void
3a5c3e22 1305update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1306{
a5606eee 1307 int within_current_scope;
a5606eee 1308 struct frame_id saved_frame_id;
66076460 1309 int frame_saved;
a5606eee 1310
f6bc2008
PA
1311 /* If this is a local watchpoint, we only want to check if the
1312 watchpoint frame is in scope if the current thread is the thread
1313 that was used to create the watchpoint. */
1314 if (!watchpoint_in_thread_scope (b))
1315 return;
1316
3a5c3e22 1317 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1318 return;
1319
66076460 1320 frame_saved = 0;
a5606eee
VP
1321
1322 /* Determine if the watchpoint is within scope. */
1323 if (b->exp_valid_block == NULL)
1324 within_current_scope = 1;
1325 else
1326 {
b5db5dfc
UW
1327 struct frame_info *fi = get_current_frame ();
1328 struct gdbarch *frame_arch = get_frame_arch (fi);
1329 CORE_ADDR frame_pc = get_frame_pc (fi);
1330
1331 /* If we're in a function epilogue, unwinding may not work
1332 properly, so do not attempt to recreate locations at this
1333 point. See similar comments in watchpoint_check. */
1334 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1335 return;
66076460
DJ
1336
1337 /* Save the current frame's ID so we can restore it after
1338 evaluating the watchpoint expression on its own frame. */
1339 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1340 took a frame parameter, so that we didn't have to change the
1341 selected frame. */
1342 frame_saved = 1;
1343 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1344
a5606eee
VP
1345 fi = frame_find_by_id (b->watchpoint_frame);
1346 within_current_scope = (fi != NULL);
1347 if (within_current_scope)
1348 select_frame (fi);
1349 }
1350
b5db5dfc
UW
1351 /* We don't free locations. They are stored in the bp_location array
1352 and update_global_location_list will eventually delete them and
1353 remove breakpoints if needed. */
3a5c3e22 1354 b->base.loc = NULL;
b5db5dfc 1355
a5606eee
VP
1356 if (within_current_scope && reparse)
1357 {
1358 char *s;
d63d0675 1359
a5606eee
VP
1360 if (b->exp)
1361 {
1362 xfree (b->exp);
1363 b->exp = NULL;
1364 }
d63d0675 1365 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1366 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1367 /* If the meaning of expression itself changed, the old value is
1368 no longer relevant. We don't want to report a watchpoint hit
1369 to the user when the old value and the new value may actually
1370 be completely different objects. */
1371 value_free (b->val);
fa4727a6
DJ
1372 b->val = NULL;
1373 b->val_valid = 0;
60e1c644
PA
1374
1375 /* Note that unlike with breakpoints, the watchpoint's condition
1376 expression is stored in the breakpoint object, not in the
1377 locations (re)created below. */
3a5c3e22 1378 if (b->base.cond_string != NULL)
60e1c644
PA
1379 {
1380 if (b->cond_exp != NULL)
1381 {
1382 xfree (b->cond_exp);
1383 b->cond_exp = NULL;
1384 }
1385
3a5c3e22 1386 s = b->base.cond_string;
60e1c644
PA
1387 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1388 }
a5606eee 1389 }
a5606eee
VP
1390
1391 /* If we failed to parse the expression, for example because
1392 it refers to a global variable in a not-yet-loaded shared library,
1393 don't try to insert watchpoint. We don't automatically delete
1394 such watchpoint, though, since failure to parse expression
1395 is different from out-of-scope watchpoint. */
2d134ed3
PA
1396 if ( !target_has_execution)
1397 {
1398 /* Without execution, memory can't change. No use to try and
1399 set watchpoint locations. The watchpoint will be reset when
1400 the target gains execution, through breakpoint_re_set. */
1401 }
1402 else if (within_current_scope && b->exp)
a5606eee 1403 {
0cf6dd15 1404 int pc = 0;
fa4727a6 1405 struct value *val_chain, *v, *result, *next;
2d134ed3 1406 struct program_space *frame_pspace;
a5606eee 1407
0cf6dd15 1408 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1409
a5606eee
VP
1410 /* Avoid setting b->val if it's already set. The meaning of
1411 b->val is 'the last value' user saw, and we should update
1412 it only if we reported that last value to user. As it
9c06b0b4
TJB
1413 happens, the code that reports it updates b->val directly.
1414 We don't keep track of the memory value for masked
1415 watchpoints. */
3a5c3e22 1416 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1417 {
1418 b->val = v;
1419 b->val_valid = 1;
1420 }
a5606eee 1421
2d134ed3
PA
1422 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1423
a5606eee 1424 /* Look at each value on the value chain. */
9fa40276 1425 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1426 {
1427 /* If it's a memory location, and GDB actually needed
1428 its contents to evaluate the expression, then we
fa4727a6
DJ
1429 must watch it. If the first value returned is
1430 still lazy, that means an error occurred reading it;
1431 watch it anyway in case it becomes readable. */
a5606eee 1432 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1433 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1434 {
1435 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1436
a5606eee
VP
1437 /* We only watch structs and arrays if user asked
1438 for it explicitly, never if they just happen to
1439 appear in the middle of some value chain. */
fa4727a6 1440 if (v == result
a5606eee
VP
1441 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1442 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1443 {
1444 CORE_ADDR addr;
1445 int len, type;
1446 struct bp_location *loc, **tmp;
1447
42ae5230 1448 addr = value_address (v);
a5606eee
VP
1449 len = TYPE_LENGTH (value_type (v));
1450 type = hw_write;
3a5c3e22 1451 if (b->base.type == bp_read_watchpoint)
a5606eee 1452 type = hw_read;
3a5c3e22 1453 else if (b->base.type == bp_access_watchpoint)
a5606eee 1454 type = hw_access;
3a5c3e22
PA
1455
1456 loc = allocate_bp_location (&b->base);
1457 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1458 ;
1459 *tmp = loc;
a6d9a66e 1460 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1461
1462 loc->pspace = frame_pspace;
a5606eee
VP
1463 loc->address = addr;
1464 loc->length = len;
1465 loc->watchpoint_type = type;
1466 }
1467 }
9fa40276
TJB
1468 }
1469
1470 /* Change the type of breakpoint between hardware assisted or
1471 an ordinary watchpoint depending on the hardware support
1472 and free hardware slots. REPARSE is set when the inferior
1473 is started. */
a9634178 1474 if (reparse)
9fa40276 1475 {
e09342b5 1476 int reg_cnt;
9fa40276
TJB
1477 enum bp_loc_type loc_type;
1478 struct bp_location *bl;
a5606eee 1479
a9634178 1480 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1481
1482 if (reg_cnt)
9fa40276
TJB
1483 {
1484 int i, target_resources_ok, other_type_used;
a1398e0c 1485 enum bptype type;
9fa40276 1486
a9634178
TJB
1487 /* Use an exact watchpoint when there's only one memory region to be
1488 watched, and only one debug register is needed to watch it. */
1489 b->exact = target_exact_watchpoints && reg_cnt == 1;
1490
9fa40276 1491 /* We need to determine how many resources are already
e09342b5
TJB
1492 used for all other hardware watchpoints plus this one
1493 to see if we still have enough resources to also fit
a1398e0c
PA
1494 this watchpoint in as well. */
1495
1496 /* If this is a software watchpoint, we try to turn it
1497 to a hardware one -- count resources as if B was of
1498 hardware watchpoint type. */
1499 type = b->base.type;
1500 if (type == bp_watchpoint)
1501 type = bp_hardware_watchpoint;
1502
1503 /* This watchpoint may or may not have been placed on
1504 the list yet at this point (it won't be in the list
1505 if we're trying to create it for the first time,
1506 through watch_command), so always account for it
1507 manually. */
1508
1509 /* Count resources used by all watchpoints except B. */
1510 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1511
1512 /* Add in the resources needed for B. */
1513 i += hw_watchpoint_use_count (&b->base);
1514
1515 target_resources_ok
1516 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1517 if (target_resources_ok <= 0)
a9634178 1518 {
3a5c3e22 1519 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1520
1521 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1522 error (_("Target does not support this type of "
1523 "hardware watchpoint."));
9c06b0b4
TJB
1524 else if (target_resources_ok < 0 && !sw_mode)
1525 error (_("There are not enough available hardware "
1526 "resources for this watchpoint."));
a1398e0c
PA
1527
1528 /* Downgrade to software watchpoint. */
1529 b->base.type = bp_watchpoint;
1530 }
1531 else
1532 {
1533 /* If this was a software watchpoint, we've just
1534 found we have enough resources to turn it to a
1535 hardware watchpoint. Otherwise, this is a
1536 nop. */
1537 b->base.type = type;
a9634178 1538 }
9fa40276 1539 }
3a5c3e22 1540 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1541 error (_("Expression cannot be implemented with "
1542 "read/access watchpoint."));
9fa40276 1543 else
3a5c3e22 1544 b->base.type = bp_watchpoint;
9fa40276 1545
3a5c3e22 1546 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1547 : bp_loc_hardware_watchpoint);
3a5c3e22 1548 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1549 bl->loc_type = loc_type;
1550 }
1551
1552 for (v = val_chain; v; v = next)
1553 {
a5606eee
VP
1554 next = value_next (v);
1555 if (v != b->val)
1556 value_free (v);
1557 }
1558
c7437ca6
PA
1559 /* If a software watchpoint is not watching any memory, then the
1560 above left it without any location set up. But,
1561 bpstat_stop_status requires a location to be able to report
1562 stops, so make sure there's at least a dummy one. */
3a5c3e22 1563 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1564 {
3a5c3e22
PA
1565 struct breakpoint *base = &b->base;
1566 base->loc = allocate_bp_location (base);
1567 base->loc->pspace = frame_pspace;
1568 base->loc->address = -1;
1569 base->loc->length = -1;
1570 base->loc->watchpoint_type = -1;
c7437ca6 1571 }
a5606eee
VP
1572 }
1573 else if (!within_current_scope)
7270d8f2 1574 {
ac74f770
MS
1575 printf_filtered (_("\
1576Watchpoint %d deleted because the program has left the block\n\
1577in which its expression is valid.\n"),
3a5c3e22 1578 b->base.number);
d0fb5eae 1579 watchpoint_del_at_next_stop (b);
7270d8f2 1580 }
a5606eee
VP
1581
1582 /* Restore the selected frame. */
66076460
DJ
1583 if (frame_saved)
1584 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1585}
1586
a5606eee 1587
74960c60 1588/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1589 inserted in the inferior. We don't differentiate the type of BL's owner
1590 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1591 breakpoint_ops is not defined, because in insert_bp_location,
1592 tracepoint's insert_location will not be called. */
74960c60 1593static int
35df4500 1594should_be_inserted (struct bp_location *bl)
74960c60 1595{
35df4500 1596 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1597 return 0;
1598
35df4500 1599 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1600 return 0;
1601
35df4500 1602 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1603 return 0;
1604
f8eba3c6
TT
1605 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1606 return 0;
1607
56710373
PA
1608 /* This is set for example, when we're attached to the parent of a
1609 vfork, and have detached from the child. The child is running
1610 free, and we expect it to do an exec or exit, at which point the
1611 OS makes the parent schedulable again (and the target reports
1612 that the vfork is done). Until the child is done with the shared
1613 memory region, do not insert breakpoints in the parent, otherwise
1614 the child could still trip on the parent's breakpoints. Since
1615 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1616 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1617 return 0;
1618
74960c60
VP
1619 return 1;
1620}
1621
934709f0
PW
1622/* Same as should_be_inserted but does the check assuming
1623 that the location is not duplicated. */
1624
1625static int
1626unduplicated_should_be_inserted (struct bp_location *bl)
1627{
1628 int result;
1629 const int save_duplicate = bl->duplicate;
1630
1631 bl->duplicate = 0;
1632 result = should_be_inserted (bl);
1633 bl->duplicate = save_duplicate;
1634 return result;
1635}
1636
35df4500
TJB
1637/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1638 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 1639 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
1640 Returns 0 for success, 1 if the bp_location type is not supported or
1641 -1 for failure.
879bfdc2 1642
4a64f543
MS
1643 NOTE drow/2003-09-09: This routine could be broken down to an
1644 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1645static int
35df4500 1646insert_bp_location (struct bp_location *bl,
26bb91f3 1647 struct ui_file *tmp_error_stream,
3fbb6ffa 1648 int *disabled_breaks,
26bb91f3 1649 int *hw_breakpoint_error)
879bfdc2
DJ
1650{
1651 int val = 0;
1652
35df4500 1653 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1654 return 0;
1655
8181d85f 1656 /* Initialize the target-specific information. */
35df4500
TJB
1657 memset (&bl->target_info, 0, sizeof (bl->target_info));
1658 bl->target_info.placed_address = bl->address;
1659 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1660 bl->target_info.length = bl->length;
8181d85f 1661
35df4500
TJB
1662 if (bl->loc_type == bp_loc_software_breakpoint
1663 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1664 {
35df4500 1665 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1666 {
1667 /* If the explicitly specified breakpoint type
1668 is not hardware breakpoint, check the memory map to see
1669 if the breakpoint address is in read only memory or not.
4a64f543 1670
765dc015
VP
1671 Two important cases are:
1672 - location type is not hardware breakpoint, memory
1673 is readonly. We change the type of the location to
1674 hardware breakpoint.
4a64f543
MS
1675 - location type is hardware breakpoint, memory is
1676 read-write. This means we've previously made the
1677 location hardware one, but then the memory map changed,
1678 so we undo.
765dc015 1679
4a64f543
MS
1680 When breakpoints are removed, remove_breakpoints will use
1681 location types we've just set here, the only possible
1682 problem is that memory map has changed during running
1683 program, but it's not going to work anyway with current
1684 gdb. */
765dc015 1685 struct mem_region *mr
35df4500 1686 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1687
1688 if (mr)
1689 {
1690 if (automatic_hardware_breakpoints)
1691 {
765dc015
VP
1692 enum bp_loc_type new_type;
1693
1694 if (mr->attrib.mode != MEM_RW)
1695 new_type = bp_loc_hardware_breakpoint;
1696 else
1697 new_type = bp_loc_software_breakpoint;
1698
35df4500 1699 if (new_type != bl->loc_type)
765dc015
VP
1700 {
1701 static int said = 0;
cc59ec59 1702
35df4500 1703 bl->loc_type = new_type;
765dc015
VP
1704 if (!said)
1705 {
3e43a32a
MS
1706 fprintf_filtered (gdb_stdout,
1707 _("Note: automatically using "
1708 "hardware breakpoints for "
1709 "read-only addresses.\n"));
765dc015
VP
1710 said = 1;
1711 }
1712 }
1713 }
35df4500 1714 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1715 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1716 warning (_("cannot set software breakpoint "
1717 "at readonly address %s"),
35df4500 1718 paddress (bl->gdbarch, bl->address));
765dc015
VP
1719 }
1720 }
1721
879bfdc2
DJ
1722 /* First check to see if we have to handle an overlay. */
1723 if (overlay_debugging == ovly_off
35df4500
TJB
1724 || bl->section == NULL
1725 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1726 {
1727 /* No overlay handling: just set the breakpoint. */
1728
348d480f 1729 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1730 }
1731 else
1732 {
4a64f543 1733 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1734 Shall we set a breakpoint at the LMA? */
1735 if (!overlay_events_enabled)
1736 {
1737 /* Yes -- overlay event support is not active,
1738 so we must try to set a breakpoint at the LMA.
1739 This will not work for a hardware breakpoint. */
35df4500 1740 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1741 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1742 bl->owner->number);
879bfdc2
DJ
1743 else
1744 {
35df4500
TJB
1745 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1746 bl->section);
879bfdc2 1747 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1748 bl->overlay_target_info = bl->target_info;
1749 bl->overlay_target_info.placed_address = addr;
1750 val = target_insert_breakpoint (bl->gdbarch,
1751 &bl->overlay_target_info);
879bfdc2 1752 if (val != 0)
99361f52 1753 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1754 "Overlay breakpoint %d "
1755 "failed: in ROM?\n",
35df4500 1756 bl->owner->number);
879bfdc2
DJ
1757 }
1758 }
1759 /* Shall we set a breakpoint at the VMA? */
35df4500 1760 if (section_is_mapped (bl->section))
879bfdc2
DJ
1761 {
1762 /* Yes. This overlay section is mapped into memory. */
348d480f 1763 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1764 }
1765 else
1766 {
1767 /* No. This breakpoint will not be inserted.
1768 No error, but do not mark the bp as 'inserted'. */
1769 return 0;
1770 }
1771 }
1772
1773 if (val)
1774 {
1775 /* Can't set the breakpoint. */
35df4500 1776 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1777 {
4a64f543 1778 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1779 val = 0;
35df4500 1780 bl->shlib_disabled = 1;
8d3788bd 1781 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
1782 if (!*disabled_breaks)
1783 {
1784 fprintf_unfiltered (tmp_error_stream,
1785 "Cannot insert breakpoint %d.\n",
1786 bl->owner->number);
1787 fprintf_unfiltered (tmp_error_stream,
1788 "Temporarily disabling shared "
1789 "library breakpoints:\n");
1790 }
1791 *disabled_breaks = 1;
879bfdc2 1792 fprintf_unfiltered (tmp_error_stream,
35df4500 1793 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1794 }
1795 else
879bfdc2 1796 {
35df4500 1797 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1798 {
1799 *hw_breakpoint_error = 1;
3e43a32a
MS
1800 fprintf_unfiltered (tmp_error_stream,
1801 "Cannot insert hardware "
1802 "breakpoint %d.\n",
35df4500 1803 bl->owner->number);
879bfdc2
DJ
1804 }
1805 else
1806 {
1807 fprintf_unfiltered (tmp_error_stream,
1808 "Cannot insert breakpoint %d.\n",
35df4500 1809 bl->owner->number);
879bfdc2
DJ
1810 fprintf_filtered (tmp_error_stream,
1811 "Error accessing memory address ");
35df4500 1812 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1813 tmp_error_stream);
879bfdc2
DJ
1814 fprintf_filtered (tmp_error_stream, ": %s.\n",
1815 safe_strerror (val));
1816 }
1817
1818 }
1819 }
1820 else
35df4500 1821 bl->inserted = 1;
879bfdc2
DJ
1822
1823 return val;
1824 }
1825
35df4500 1826 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1827 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1828 watchpoints. It's not clear that it's necessary... */
35df4500 1829 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1830 {
77b06cd7
TJB
1831 gdb_assert (bl->owner->ops != NULL
1832 && bl->owner->ops->insert_location != NULL);
1833
1834 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1835
1836 /* If trying to set a read-watchpoint, and it turns out it's not
1837 supported, try emulating one with an access watchpoint. */
35df4500 1838 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1839 {
1840 struct bp_location *loc, **loc_temp;
1841
1842 /* But don't try to insert it, if there's already another
1843 hw_access location that would be considered a duplicate
1844 of this one. */
1845 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1846 if (loc != bl
85d721b8 1847 && loc->watchpoint_type == hw_access
35df4500 1848 && watchpoint_locations_match (bl, loc))
85d721b8 1849 {
35df4500
TJB
1850 bl->duplicate = 1;
1851 bl->inserted = 1;
1852 bl->target_info = loc->target_info;
1853 bl->watchpoint_type = hw_access;
85d721b8
PA
1854 val = 0;
1855 break;
1856 }
1857
1858 if (val == 1)
1859 {
77b06cd7
TJB
1860 bl->watchpoint_type = hw_access;
1861 val = bl->owner->ops->insert_location (bl);
1862
1863 if (val)
1864 /* Back to the original value. */
1865 bl->watchpoint_type = hw_read;
85d721b8
PA
1866 }
1867 }
1868
35df4500 1869 bl->inserted = (val == 0);
879bfdc2
DJ
1870 }
1871
35df4500 1872 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1873 {
77b06cd7
TJB
1874 gdb_assert (bl->owner->ops != NULL
1875 && bl->owner->ops->insert_location != NULL);
1876
1877 val = bl->owner->ops->insert_location (bl);
1878 if (val)
1879 {
1880 bl->owner->enable_state = bp_disabled;
1881
1882 if (val == 1)
1883 warning (_("\
1884Error inserting catchpoint %d: Your system does not support this type\n\
1885of catchpoint."), bl->owner->number);
1886 else
1887 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1888 }
1889
1890 bl->inserted = (val == 0);
1640b821
DJ
1891
1892 /* We've already printed an error message if there was a problem
1893 inserting this catchpoint, and we've disabled the catchpoint,
1894 so just return success. */
1895 return 0;
879bfdc2
DJ
1896 }
1897
1898 return 0;
1899}
1900
6c95b8df
PA
1901/* This function is called when program space PSPACE is about to be
1902 deleted. It takes care of updating breakpoints to not reference
1903 PSPACE anymore. */
1904
1905void
1906breakpoint_program_space_exit (struct program_space *pspace)
1907{
1908 struct breakpoint *b, *b_temp;
876fa593 1909 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1910
1911 /* Remove any breakpoint that was set through this program space. */
1912 ALL_BREAKPOINTS_SAFE (b, b_temp)
1913 {
1914 if (b->pspace == pspace)
1915 delete_breakpoint (b);
1916 }
1917
1918 /* Breakpoints set through other program spaces could have locations
1919 bound to PSPACE as well. Remove those. */
876fa593 1920 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1921 {
1922 struct bp_location *tmp;
1923
1924 if (loc->pspace == pspace)
1925 {
2bdf28a0 1926 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1927 if (loc->owner->loc == loc)
1928 loc->owner->loc = loc->next;
1929 else
1930 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1931 if (tmp->next == loc)
1932 {
1933 tmp->next = loc->next;
1934 break;
1935 }
1936 }
1937 }
1938
1939 /* Now update the global location list to permanently delete the
1940 removed locations above. */
1941 update_global_location_list (0);
1942}
1943
74960c60
VP
1944/* Make sure all breakpoints are inserted in inferior.
1945 Throws exception on any error.
1946 A breakpoint that is already inserted won't be inserted
1947 again, so calling this function twice is safe. */
1948void
1949insert_breakpoints (void)
1950{
1951 struct breakpoint *bpt;
1952
1953 ALL_BREAKPOINTS (bpt)
1954 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
1955 {
1956 struct watchpoint *w = (struct watchpoint *) bpt;
1957
1958 update_watchpoint (w, 0 /* don't reparse. */);
1959 }
74960c60 1960
b60e7edf 1961 update_global_location_list (1);
74960c60 1962
c35b1492
PA
1963 /* update_global_location_list does not insert breakpoints when
1964 always_inserted_mode is not enabled. Explicitly insert them
1965 now. */
1966 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1967 insert_breakpoint_locations ();
1968}
1969
c30eee59 1970/* Used when starting or continuing the program. */
c906108c 1971
74960c60
VP
1972static void
1973insert_breakpoint_locations (void)
c906108c 1974{
a5606eee 1975 struct breakpoint *bpt;
35df4500 1976 struct bp_location *bl, **blp_tmp;
eacd795a 1977 int error_flag = 0;
c906108c 1978 int val = 0;
3fbb6ffa 1979 int disabled_breaks = 0;
81d0cc19 1980 int hw_breakpoint_error = 0;
c906108c 1981
81d0cc19 1982 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1983 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1984
81d0cc19
GS
1985 /* Explicitly mark the warning -- this will only be printed if
1986 there was an error. */
1987 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1988
1989 save_current_space_and_thread ();
1990
35df4500 1991 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1992 {
35df4500 1993 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1994 continue;
1995
4a64f543
MS
1996 /* There is no point inserting thread-specific breakpoints if
1997 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1998 has BL->OWNER always non-NULL. */
35df4500
TJB
1999 if (bl->owner->thread != -1
2000 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2001 continue;
2002
35df4500 2003 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2004
2005 /* For targets that support global breakpoints, there's no need
2006 to select an inferior to insert breakpoint to. In fact, even
2007 if we aren't attached to any process yet, we should still
2008 insert breakpoints. */
2009 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2010 && ptid_equal (inferior_ptid, null_ptid))
2011 continue;
2012
3fbb6ffa
TJB
2013 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2014 &hw_breakpoint_error);
879bfdc2 2015 if (val)
eacd795a 2016 error_flag = val;
879bfdc2 2017 }
c906108c 2018
4a64f543
MS
2019 /* If we failed to insert all locations of a watchpoint, remove
2020 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2021 ALL_BREAKPOINTS (bpt)
2022 {
2023 int some_failed = 0;
2024 struct bp_location *loc;
2025
2026 if (!is_hardware_watchpoint (bpt))
2027 continue;
2028
d6b74ac4 2029 if (!breakpoint_enabled (bpt))
a5606eee 2030 continue;
74960c60
VP
2031
2032 if (bpt->disposition == disp_del_at_next_stop)
2033 continue;
a5606eee
VP
2034
2035 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2036 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2037 {
2038 some_failed = 1;
2039 break;
2040 }
2041 if (some_failed)
2042 {
2043 for (loc = bpt->loc; loc; loc = loc->next)
2044 if (loc->inserted)
2045 remove_breakpoint (loc, mark_uninserted);
2046
2047 hw_breakpoint_error = 1;
2048 fprintf_unfiltered (tmp_error_stream,
2049 "Could not insert hardware watchpoint %d.\n",
2050 bpt->number);
eacd795a 2051 error_flag = -1;
a5606eee
VP
2052 }
2053 }
2054
eacd795a 2055 if (error_flag)
81d0cc19
GS
2056 {
2057 /* If a hardware breakpoint or watchpoint was inserted, add a
2058 message about possibly exhausted resources. */
879bfdc2 2059 if (hw_breakpoint_error)
81d0cc19 2060 {
c6510018
MS
2061 fprintf_unfiltered (tmp_error_stream,
2062 "Could not insert hardware breakpoints:\n\
2063You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2064 }
81d0cc19
GS
2065 target_terminal_ours_for_output ();
2066 error_stream (tmp_error_stream);
2067 }
f7545552
TT
2068
2069 do_cleanups (cleanups);
c906108c
SS
2070}
2071
c30eee59
TJB
2072/* Used when the program stops.
2073 Returns zero if successful, or non-zero if there was a problem
2074 removing a breakpoint location. */
2075
c906108c 2076int
fba45db2 2077remove_breakpoints (void)
c906108c 2078{
35df4500 2079 struct bp_location *bl, **blp_tmp;
3a1bae8e 2080 int val = 0;
c906108c 2081
35df4500 2082 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2083 {
1e4d1764 2084 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2085 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2086 }
3a1bae8e 2087 return val;
c906108c
SS
2088}
2089
6c95b8df
PA
2090/* Remove breakpoints of process PID. */
2091
2092int
2093remove_breakpoints_pid (int pid)
2094{
35df4500 2095 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2096 int val;
2097 struct inferior *inf = find_inferior_pid (pid);
2098
35df4500 2099 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2100 {
35df4500 2101 if (bl->pspace != inf->pspace)
6c95b8df
PA
2102 continue;
2103
35df4500 2104 if (bl->inserted)
6c95b8df 2105 {
35df4500 2106 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2107 if (val != 0)
2108 return val;
2109 }
2110 }
2111 return 0;
2112}
2113
c906108c 2114int
fba45db2 2115reattach_breakpoints (int pid)
c906108c 2116{
6c95b8df 2117 struct cleanup *old_chain;
35df4500 2118 struct bp_location *bl, **blp_tmp;
c906108c 2119 int val;
86b887df 2120 struct ui_file *tmp_error_stream;
3fbb6ffa 2121 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2122 struct inferior *inf;
2123 struct thread_info *tp;
2124
2125 tp = any_live_thread_of_process (pid);
2126 if (tp == NULL)
2127 return 1;
2128
2129 inf = find_inferior_pid (pid);
2130 old_chain = save_inferior_ptid ();
2131
2132 inferior_ptid = tp->ptid;
a4954f26 2133
86b887df 2134 tmp_error_stream = mem_fileopen ();
a4954f26 2135 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2136
35df4500 2137 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2138 {
35df4500 2139 if (bl->pspace != inf->pspace)
6c95b8df
PA
2140 continue;
2141
35df4500 2142 if (bl->inserted)
c5aa993b 2143 {
35df4500 2144 bl->inserted = 0;
3fbb6ffa 2145 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2146 if (val != 0)
2147 {
ce696e05 2148 do_cleanups (old_chain);
c5aa993b
JM
2149 return val;
2150 }
2151 }
2152 }
ce696e05 2153 do_cleanups (old_chain);
c906108c
SS
2154 return 0;
2155}
2156
e58b0e63
PA
2157static int internal_breakpoint_number = -1;
2158
84f4c1fe
PM
2159/* Set the breakpoint number of B, depending on the value of INTERNAL.
2160 If INTERNAL is non-zero, the breakpoint number will be populated
2161 from internal_breakpoint_number and that variable decremented.
e5dd4106 2162 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2163 breakpoint_count and that value incremented. Internal breakpoints
2164 do not set the internal var bpnum. */
2165static void
2166set_breakpoint_number (int internal, struct breakpoint *b)
2167{
2168 if (internal)
2169 b->number = internal_breakpoint_number--;
2170 else
2171 {
2172 set_breakpoint_count (breakpoint_count + 1);
2173 b->number = breakpoint_count;
2174 }
2175}
2176
e62c965a 2177static struct breakpoint *
a6d9a66e 2178create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2179 CORE_ADDR address, enum bptype type,
c0a91b2b 2180 const struct breakpoint_ops *ops)
e62c965a 2181{
e62c965a
PP
2182 struct symtab_and_line sal;
2183 struct breakpoint *b;
2184
4a64f543 2185 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2186
2187 sal.pc = address;
2188 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2189 sal.pspace = current_program_space;
e62c965a 2190
06edf0c0 2191 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2192 b->number = internal_breakpoint_number--;
2193 b->disposition = disp_donttouch;
2194
2195 return b;
2196}
2197
17450429
PP
2198static const char *const longjmp_names[] =
2199 {
2200 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2201 };
2202#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2203
2204/* Per-objfile data private to breakpoint.c. */
2205struct breakpoint_objfile_data
2206{
2207 /* Minimal symbol for "_ovly_debug_event" (if any). */
2208 struct minimal_symbol *overlay_msym;
2209
2210 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2211 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2212
2213 /* Minimal symbol for "std::terminate()" (if any). */
2214 struct minimal_symbol *terminate_msym;
2215
2216 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2217 struct minimal_symbol *exception_msym;
2218};
2219
2220static const struct objfile_data *breakpoint_objfile_key;
2221
2222/* Minimal symbol not found sentinel. */
2223static struct minimal_symbol msym_not_found;
2224
2225/* Returns TRUE if MSYM point to the "not found" sentinel. */
2226
2227static int
2228msym_not_found_p (const struct minimal_symbol *msym)
2229{
2230 return msym == &msym_not_found;
2231}
2232
2233/* Return per-objfile data needed by breakpoint.c.
2234 Allocate the data if necessary. */
2235
2236static struct breakpoint_objfile_data *
2237get_breakpoint_objfile_data (struct objfile *objfile)
2238{
2239 struct breakpoint_objfile_data *bp_objfile_data;
2240
2241 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2242 if (bp_objfile_data == NULL)
2243 {
2244 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2245 sizeof (*bp_objfile_data));
2246
2247 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2248 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2249 }
2250 return bp_objfile_data;
2251}
2252
e62c965a 2253static void
af02033e 2254create_overlay_event_breakpoint (void)
e62c965a 2255{
69de3c6a 2256 struct objfile *objfile;
af02033e 2257 const char *const func_name = "_ovly_debug_event";
e62c965a 2258
69de3c6a
PP
2259 ALL_OBJFILES (objfile)
2260 {
2261 struct breakpoint *b;
17450429
PP
2262 struct breakpoint_objfile_data *bp_objfile_data;
2263 CORE_ADDR addr;
69de3c6a 2264
17450429
PP
2265 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2266
2267 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2268 continue;
2269
2270 if (bp_objfile_data->overlay_msym == NULL)
2271 {
2272 struct minimal_symbol *m;
2273
2274 m = lookup_minimal_symbol_text (func_name, objfile);
2275 if (m == NULL)
2276 {
2277 /* Avoid future lookups in this objfile. */
2278 bp_objfile_data->overlay_msym = &msym_not_found;
2279 continue;
2280 }
2281 bp_objfile_data->overlay_msym = m;
2282 }
e62c965a 2283
17450429
PP
2284 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2285 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2286 bp_overlay_event,
2287 &internal_breakpoint_ops);
69de3c6a 2288 b->addr_string = xstrdup (func_name);
e62c965a 2289
69de3c6a
PP
2290 if (overlay_debugging == ovly_auto)
2291 {
2292 b->enable_state = bp_enabled;
2293 overlay_events_enabled = 1;
2294 }
2295 else
2296 {
2297 b->enable_state = bp_disabled;
2298 overlay_events_enabled = 0;
2299 }
e62c965a
PP
2300 }
2301 update_global_location_list (1);
2302}
2303
0fd8e87f 2304static void
af02033e 2305create_longjmp_master_breakpoint (void)
0fd8e87f 2306{
6c95b8df 2307 struct program_space *pspace;
6c95b8df
PA
2308 struct cleanup *old_chain;
2309
2310 old_chain = save_current_program_space ();
0fd8e87f 2311
6c95b8df 2312 ALL_PSPACES (pspace)
af02033e
PP
2313 {
2314 struct objfile *objfile;
2315
2316 set_current_program_space (pspace);
2317
2318 ALL_OBJFILES (objfile)
0fd8e87f 2319 {
af02033e
PP
2320 int i;
2321 struct gdbarch *gdbarch;
17450429 2322 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2323
af02033e
PP
2324 gdbarch = get_objfile_arch (objfile);
2325 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2326 continue;
2327
17450429
PP
2328 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2329
2330 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2331 {
2332 struct breakpoint *b;
af02033e 2333 const char *func_name;
17450429 2334 CORE_ADDR addr;
6c95b8df 2335
17450429 2336 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2337 continue;
0fd8e87f 2338
17450429
PP
2339 func_name = longjmp_names[i];
2340 if (bp_objfile_data->longjmp_msym[i] == NULL)
2341 {
2342 struct minimal_symbol *m;
2343
2344 m = lookup_minimal_symbol_text (func_name, objfile);
2345 if (m == NULL)
2346 {
2347 /* Prevent future lookups in this objfile. */
2348 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2349 continue;
2350 }
2351 bp_objfile_data->longjmp_msym[i] = m;
2352 }
2353
2354 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2355 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2356 &internal_breakpoint_ops);
af02033e
PP
2357 b->addr_string = xstrdup (func_name);
2358 b->enable_state = bp_disabled;
2359 }
0fd8e87f 2360 }
af02033e 2361 }
0fd8e87f 2362 update_global_location_list (1);
6c95b8df
PA
2363
2364 do_cleanups (old_chain);
0fd8e87f
UW
2365}
2366
af02033e 2367/* Create a master std::terminate breakpoint. */
aa7d318d 2368static void
af02033e 2369create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2370{
2371 struct program_space *pspace;
aa7d318d 2372 struct cleanup *old_chain;
af02033e 2373 const char *const func_name = "std::terminate()";
aa7d318d
TT
2374
2375 old_chain = save_current_program_space ();
2376
2377 ALL_PSPACES (pspace)
17450429
PP
2378 {
2379 struct objfile *objfile;
2380 CORE_ADDR addr;
2381
2382 set_current_program_space (pspace);
2383
aa7d318d
TT
2384 ALL_OBJFILES (objfile)
2385 {
2386 struct breakpoint *b;
17450429 2387 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2388
17450429 2389 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2390
17450429
PP
2391 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2392 continue;
2393
2394 if (bp_objfile_data->terminate_msym == NULL)
2395 {
2396 struct minimal_symbol *m;
2397
2398 m = lookup_minimal_symbol (func_name, NULL, objfile);
2399 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2400 && MSYMBOL_TYPE (m) != mst_file_text))
2401 {
2402 /* Prevent future lookups in this objfile. */
2403 bp_objfile_data->terminate_msym = &msym_not_found;
2404 continue;
2405 }
2406 bp_objfile_data->terminate_msym = m;
2407 }
aa7d318d 2408
17450429
PP
2409 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2410 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2411 bp_std_terminate_master,
2412 &internal_breakpoint_ops);
aa7d318d
TT
2413 b->addr_string = xstrdup (func_name);
2414 b->enable_state = bp_disabled;
2415 }
17450429
PP
2416 }
2417
aa7d318d
TT
2418 update_global_location_list (1);
2419
2420 do_cleanups (old_chain);
2421}
2422
186c406b
TT
2423/* Install a master breakpoint on the unwinder's debug hook. */
2424
2425void
2426create_exception_master_breakpoint (void)
2427{
2428 struct objfile *objfile;
17450429 2429 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2430
2431 ALL_OBJFILES (objfile)
2432 {
17450429
PP
2433 struct breakpoint *b;
2434 struct gdbarch *gdbarch;
2435 struct breakpoint_objfile_data *bp_objfile_data;
2436 CORE_ADDR addr;
2437
2438 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2439
2440 if (msym_not_found_p (bp_objfile_data->exception_msym))
2441 continue;
2442
2443 gdbarch = get_objfile_arch (objfile);
186c406b 2444
17450429 2445 if (bp_objfile_data->exception_msym == NULL)
186c406b 2446 {
17450429 2447 struct minimal_symbol *debug_hook;
186c406b 2448
17450429
PP
2449 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2450 if (debug_hook == NULL)
2451 {
2452 bp_objfile_data->exception_msym = &msym_not_found;
2453 continue;
2454 }
2455
2456 bp_objfile_data->exception_msym = debug_hook;
186c406b 2457 }
17450429
PP
2458
2459 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2460 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2461 &current_target);
06edf0c0
PA
2462 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2463 &internal_breakpoint_ops);
17450429
PP
2464 b->addr_string = xstrdup (func_name);
2465 b->enable_state = bp_disabled;
186c406b
TT
2466 }
2467
2468 update_global_location_list (1);
2469}
2470
c906108c 2471void
fba45db2 2472update_breakpoints_after_exec (void)
c906108c 2473{
35df4500 2474 struct breakpoint *b, *b_tmp;
876fa593 2475 struct bp_location *bploc, **bplocp_tmp;
c906108c 2476
25b22b0a
PA
2477 /* We're about to delete breakpoints from GDB's lists. If the
2478 INSERTED flag is true, GDB will try to lift the breakpoints by
2479 writing the breakpoints' "shadow contents" back into memory. The
2480 "shadow contents" are NOT valid after an exec, so GDB should not
2481 do that. Instead, the target is responsible from marking
2482 breakpoints out as soon as it detects an exec. We don't do that
2483 here instead, because there may be other attempts to delete
2484 breakpoints after detecting an exec and before reaching here. */
876fa593 2485 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2486 if (bploc->pspace == current_program_space)
2487 gdb_assert (!bploc->inserted);
c906108c 2488
35df4500 2489 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2490 {
6c95b8df
PA
2491 if (b->pspace != current_program_space)
2492 continue;
2493
4a64f543 2494 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2495 if (b->type == bp_shlib_event)
2496 {
2497 delete_breakpoint (b);
2498 continue;
2499 }
c906108c 2500
4a64f543 2501 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2502 if (b->type == bp_jit_event)
2503 {
2504 delete_breakpoint (b);
2505 continue;
2506 }
2507
1900040c 2508 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2509 as must overlay event and longjmp master breakpoints. */
2510 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2511 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2512 || b->type == bp_exception_master)
c4093a6a
JM
2513 {
2514 delete_breakpoint (b);
2515 continue;
2516 }
2517
4a64f543 2518 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 2519 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
2520 {
2521 delete_breakpoint (b);
2522 continue;
2523 }
2524
611c83ae
PA
2525 /* Longjmp and longjmp-resume breakpoints are also meaningless
2526 after an exec. */
186c406b
TT
2527 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2528 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2529 {
2530 delete_breakpoint (b);
2531 continue;
2532 }
2533
ce78b96d
JB
2534 if (b->type == bp_catchpoint)
2535 {
2536 /* For now, none of the bp_catchpoint breakpoints need to
2537 do anything at this point. In the future, if some of
2538 the catchpoints need to something, we will need to add
2539 a new method, and call this method from here. */
2540 continue;
2541 }
2542
c5aa993b
JM
2543 /* bp_finish is a special case. The only way we ought to be able
2544 to see one of these when an exec() has happened, is if the user
2545 caught a vfork, and then said "finish". Ordinarily a finish just
2546 carries them to the call-site of the current callee, by setting
2547 a temporary bp there and resuming. But in this case, the finish
2548 will carry them entirely through the vfork & exec.
2549
2550 We don't want to allow a bp_finish to remain inserted now. But
2551 we can't safely delete it, 'cause finish_command has a handle to
2552 the bp on a bpstat, and will later want to delete it. There's a
2553 chance (and I've seen it happen) that if we delete the bp_finish
2554 here, that its storage will get reused by the time finish_command
2555 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2556 We really must allow finish_command to delete a bp_finish.
2557
e5dd4106 2558 In the absence of a general solution for the "how do we know
53a5351d
JM
2559 it's safe to delete something others may have handles to?"
2560 problem, what we'll do here is just uninsert the bp_finish, and
2561 let finish_command delete it.
2562
2563 (We know the bp_finish is "doomed" in the sense that it's
2564 momentary, and will be deleted as soon as finish_command sees
2565 the inferior stopped. So it doesn't matter that the bp's
2566 address is probably bogus in the new a.out, unlike e.g., the
2567 solib breakpoints.) */
c5aa993b 2568
c5aa993b
JM
2569 if (b->type == bp_finish)
2570 {
2571 continue;
2572 }
2573
2574 /* Without a symbolic address, we have little hope of the
2575 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2576 a.out. */
c5aa993b
JM
2577 if (b->addr_string == NULL)
2578 {
2579 delete_breakpoint (b);
2580 continue;
2581 }
c5aa993b 2582 }
1900040c 2583 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2584 create_overlay_event_breakpoint ();
2585 create_longjmp_master_breakpoint ();
2586 create_std_terminate_master_breakpoint ();
186c406b 2587 create_exception_master_breakpoint ();
c906108c
SS
2588}
2589
2590int
fba45db2 2591detach_breakpoints (int pid)
c906108c 2592{
35df4500 2593 struct bp_location *bl, **blp_tmp;
3a1bae8e 2594 int val = 0;
ce696e05 2595 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2596 struct inferior *inf = current_inferior ();
c5aa993b 2597
39f77062 2598 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2599 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2600
6c95b8df 2601 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2602 inferior_ptid = pid_to_ptid (pid);
35df4500 2603 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2604 {
35df4500 2605 if (bl->pspace != inf->pspace)
6c95b8df
PA
2606 continue;
2607
35df4500
TJB
2608 if (bl->inserted)
2609 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2610 }
d03285ec
UW
2611
2612 /* Detach single-step breakpoints as well. */
2613 detach_single_step_breakpoints ();
2614
ce696e05 2615 do_cleanups (old_chain);
3a1bae8e 2616 return val;
c906108c
SS
2617}
2618
35df4500 2619/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2620 Note that this is used to detach breakpoints from a child fork.
2621 When we get here, the child isn't in the inferior list, and neither
2622 do we have objects to represent its address space --- we should
35df4500 2623 *not* look at bl->pspace->aspace here. */
6c95b8df 2624
c906108c 2625static int
35df4500 2626remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2627{
2628 int val;
c5aa993b 2629
35df4500
TJB
2630 /* BL is never in moribund_locations by our callers. */
2631 gdb_assert (bl->owner != NULL);
2bdf28a0 2632
35df4500 2633 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2634 /* Permanent breakpoints cannot be inserted or removed. */
2635 return 0;
2636
74960c60
VP
2637 /* The type of none suggests that owner is actually deleted.
2638 This should not ever happen. */
35df4500 2639 gdb_assert (bl->owner->type != bp_none);
0bde7532 2640
35df4500
TJB
2641 if (bl->loc_type == bp_loc_software_breakpoint
2642 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2643 {
c02f5703
MS
2644 /* "Normal" instruction breakpoint: either the standard
2645 trap-instruction bp (bp_breakpoint), or a
2646 bp_hardware_breakpoint. */
2647
2648 /* First check to see if we have to handle an overlay. */
2649 if (overlay_debugging == ovly_off
35df4500
TJB
2650 || bl->section == NULL
2651 || !(section_is_overlay (bl->section)))
c02f5703
MS
2652 {
2653 /* No overlay handling: just remove the breakpoint. */
348d480f 2654 val = bl->owner->ops->remove_location (bl);
c02f5703 2655 }
c906108c
SS
2656 else
2657 {
4a64f543 2658 /* This breakpoint is in an overlay section.
c02f5703
MS
2659 Did we set a breakpoint at the LMA? */
2660 if (!overlay_events_enabled)
2661 {
2662 /* Yes -- overlay event support is not active, so we
2663 should have set a breakpoint at the LMA. Remove it.
2664 */
c02f5703
MS
2665 /* Ignore any failures: if the LMA is in ROM, we will
2666 have already warned when we failed to insert it. */
35df4500
TJB
2667 if (bl->loc_type == bp_loc_hardware_breakpoint)
2668 target_remove_hw_breakpoint (bl->gdbarch,
2669 &bl->overlay_target_info);
c02f5703 2670 else
35df4500
TJB
2671 target_remove_breakpoint (bl->gdbarch,
2672 &bl->overlay_target_info);
c02f5703
MS
2673 }
2674 /* Did we set a breakpoint at the VMA?
2675 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2676 if (bl->inserted)
c906108c 2677 {
c02f5703
MS
2678 /* Yes -- remove it. Previously we did not bother to
2679 remove the breakpoint if the section had been
2680 unmapped, but let's not rely on that being safe. We
2681 don't know what the overlay manager might do. */
aa67235e
UW
2682
2683 /* However, we should remove *software* breakpoints only
2684 if the section is still mapped, or else we overwrite
2685 wrong code with the saved shadow contents. */
348d480f
PA
2686 if (bl->loc_type == bp_loc_hardware_breakpoint
2687 || section_is_mapped (bl->section))
2688 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
2689 else
2690 val = 0;
c906108c 2691 }
c02f5703
MS
2692 else
2693 {
2694 /* No -- not inserted, so no need to remove. No error. */
2695 val = 0;
2696 }
c906108c 2697 }
879d1e6b
UW
2698
2699 /* In some cases, we might not be able to remove a breakpoint
2700 in a shared library that has already been removed, but we
2701 have not yet processed the shlib unload event. */
35df4500 2702 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2703 val = 0;
2704
c906108c
SS
2705 if (val)
2706 return val;
35df4500 2707 bl->inserted = (is == mark_inserted);
c906108c 2708 }
35df4500 2709 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2710 {
77b06cd7
TJB
2711 gdb_assert (bl->owner->ops != NULL
2712 && bl->owner->ops->remove_location != NULL);
2713
35df4500 2714 bl->inserted = (is == mark_inserted);
77b06cd7 2715 bl->owner->ops->remove_location (bl);
2e70b7b9 2716
c906108c 2717 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2718 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2719 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2720 bl->owner->number);
c906108c 2721 }
35df4500
TJB
2722 else if (bl->owner->type == bp_catchpoint
2723 && breakpoint_enabled (bl->owner)
2724 && !bl->duplicate)
ce78b96d 2725 {
77b06cd7
TJB
2726 gdb_assert (bl->owner->ops != NULL
2727 && bl->owner->ops->remove_location != NULL);
ce78b96d 2728
77b06cd7 2729 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2730 if (val)
2731 return val;
77b06cd7 2732
35df4500 2733 bl->inserted = (is == mark_inserted);
ce78b96d 2734 }
c906108c
SS
2735
2736 return 0;
2737}
2738
6c95b8df 2739static int
35df4500 2740remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2741{
2742 int ret;
2743 struct cleanup *old_chain;
2744
35df4500
TJB
2745 /* BL is never in moribund_locations by our callers. */
2746 gdb_assert (bl->owner != NULL);
2bdf28a0 2747
35df4500 2748 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2749 /* Permanent breakpoints cannot be inserted or removed. */
2750 return 0;
2751
2752 /* The type of none suggests that owner is actually deleted.
2753 This should not ever happen. */
35df4500 2754 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2755
2756 old_chain = save_current_space_and_thread ();
2757
35df4500 2758 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2759
35df4500 2760 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2761
2762 do_cleanups (old_chain);
2763 return ret;
2764}
2765
c906108c
SS
2766/* Clear the "inserted" flag in all breakpoints. */
2767
25b22b0a 2768void
fba45db2 2769mark_breakpoints_out (void)
c906108c 2770{
35df4500 2771 struct bp_location *bl, **blp_tmp;
c906108c 2772
35df4500
TJB
2773 ALL_BP_LOCATIONS (bl, blp_tmp)
2774 if (bl->pspace == current_program_space)
2775 bl->inserted = 0;
c906108c
SS
2776}
2777
53a5351d
JM
2778/* Clear the "inserted" flag in all breakpoints and delete any
2779 breakpoints which should go away between runs of the program.
c906108c
SS
2780
2781 Plus other such housekeeping that has to be done for breakpoints
2782 between runs.
2783
53a5351d
JM
2784 Note: this function gets called at the end of a run (by
2785 generic_mourn_inferior) and when a run begins (by
4a64f543 2786 init_wait_for_inferior). */
c906108c
SS
2787
2788
2789
2790void
fba45db2 2791breakpoint_init_inferior (enum inf_context context)
c906108c 2792{
35df4500
TJB
2793 struct breakpoint *b, *b_tmp;
2794 struct bp_location *bl, **blp_tmp;
1c5cfe86 2795 int ix;
6c95b8df 2796 struct program_space *pspace = current_program_space;
c906108c 2797
50c71eaf
PA
2798 /* If breakpoint locations are shared across processes, then there's
2799 nothing to do. */
2567c7d9 2800 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2801 return;
2802
35df4500 2803 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2804 {
35df4500
TJB
2805 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2806 if (bl->pspace == pspace
2807 && bl->owner->enable_state != bp_permanent)
2808 bl->inserted = 0;
6c95b8df 2809 }
075f6582 2810
35df4500 2811 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2812 {
6c95b8df
PA
2813 if (b->loc && b->loc->pspace != pspace)
2814 continue;
2815
c5aa993b
JM
2816 switch (b->type)
2817 {
2818 case bp_call_dummy:
c906108c 2819
c5aa993b 2820 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2821 cause problems when the inferior is rerun, so we better get
2822 rid of it. */
2823
2824 case bp_watchpoint_scope:
2825
2826 /* Also get rid of scope breakpoints. */
2827
2828 case bp_shlib_event:
2829
2830 /* Also remove solib event breakpoints. Their addresses may
2831 have changed since the last time we ran the program.
2832 Actually we may now be debugging against different target;
2833 and so the solib backend that installed this breakpoint may
2834 not be used in by the target. E.g.,
2835
2836 (gdb) file prog-linux
2837 (gdb) run # native linux target
2838 ...
2839 (gdb) kill
2840 (gdb) file prog-win.exe
2841 (gdb) tar rem :9999 # remote Windows gdbserver.
2842 */
c906108c 2843
c5aa993b
JM
2844 delete_breakpoint (b);
2845 break;
c906108c 2846
c5aa993b
JM
2847 case bp_watchpoint:
2848 case bp_hardware_watchpoint:
2849 case bp_read_watchpoint:
2850 case bp_access_watchpoint:
3a5c3e22
PA
2851 {
2852 struct watchpoint *w = (struct watchpoint *) b;
c906108c 2853
3a5c3e22
PA
2854 /* Likewise for watchpoints on local expressions. */
2855 if (w->exp_valid_block != NULL)
2856 delete_breakpoint (b);
2857 else if (context == inf_starting)
2858 {
2859 /* Reset val field to force reread of starting value in
2860 insert_breakpoints. */
2861 if (w->val)
2862 value_free (w->val);
2863 w->val = NULL;
2864 w->val_valid = 0;
c860120c 2865 }
3a5c3e22 2866 }
c5aa993b
JM
2867 break;
2868 default:
c5aa993b
JM
2869 break;
2870 }
2871 }
1c5cfe86
PA
2872
2873 /* Get rid of the moribund locations. */
35df4500
TJB
2874 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2875 decref_bp_location (&bl);
1c5cfe86 2876 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2877}
2878
6c95b8df
PA
2879/* These functions concern about actual breakpoints inserted in the
2880 target --- to e.g. check if we need to do decr_pc adjustment or if
2881 we need to hop over the bkpt --- so we check for address space
2882 match, not program space. */
2883
c2c6d25f
JM
2884/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2885 exists at PC. It returns ordinary_breakpoint_here if it's an
2886 ordinary breakpoint, or permanent_breakpoint_here if it's a
2887 permanent breakpoint.
2888 - When continuing from a location with an ordinary breakpoint, we
2889 actually single step once before calling insert_breakpoints.
e5dd4106 2890 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
2891 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2892 the target, to advance the PC past the breakpoint. */
c906108c 2893
c2c6d25f 2894enum breakpoint_here
6c95b8df 2895breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2896{
35df4500 2897 struct bp_location *bl, **blp_tmp;
c2c6d25f 2898 int any_breakpoint_here = 0;
c906108c 2899
35df4500 2900 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2901 {
35df4500
TJB
2902 if (bl->loc_type != bp_loc_software_breakpoint
2903 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2904 continue;
2905
f1310107 2906 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2907 if ((breakpoint_enabled (bl->owner)
2908 || bl->owner->enable_state == bp_permanent)
f1310107 2909 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2910 {
2911 if (overlay_debugging
35df4500
TJB
2912 && section_is_overlay (bl->section)
2913 && !section_is_mapped (bl->section))
075f6582 2914 continue; /* unmapped overlay -- can't be a match */
35df4500 2915 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2916 return permanent_breakpoint_here;
2917 else
2918 any_breakpoint_here = 1;
2919 }
2920 }
c906108c 2921
c2c6d25f 2922 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2923}
2924
1c5cfe86
PA
2925/* Return true if there's a moribund breakpoint at PC. */
2926
2927int
6c95b8df 2928moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2929{
2930 struct bp_location *loc;
2931 int ix;
2932
2933 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2934 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2935 return 1;
2936
2937 return 0;
2938}
c2c6d25f 2939
c36b740a 2940/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2941 inserted using regular breakpoint_chain / bp_location array
2942 mechanism. This does not check for single-step breakpoints, which
2943 are inserted and removed using direct target manipulation. */
c906108c
SS
2944
2945int
4a64f543
MS
2946regular_breakpoint_inserted_here_p (struct address_space *aspace,
2947 CORE_ADDR pc)
c906108c 2948{
35df4500 2949 struct bp_location *bl, **blp_tmp;
c906108c 2950
35df4500 2951 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2952 {
35df4500
TJB
2953 if (bl->loc_type != bp_loc_software_breakpoint
2954 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2955 continue;
2956
35df4500 2957 if (bl->inserted
f1310107 2958 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2959 {
2960 if (overlay_debugging
35df4500
TJB
2961 && section_is_overlay (bl->section)
2962 && !section_is_mapped (bl->section))
075f6582
DJ
2963 continue; /* unmapped overlay -- can't be a match */
2964 else
2965 return 1;
2966 }
c5aa993b 2967 }
c36b740a
VP
2968 return 0;
2969}
2970
2971/* Returns non-zero iff there's either regular breakpoint
2972 or a single step breakpoint inserted at PC. */
2973
2974int
6c95b8df 2975breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2976{
6c95b8df 2977 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2978 return 1;
c906108c 2979
6c95b8df 2980 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2981 return 1;
2982
c906108c
SS
2983 return 0;
2984}
2985
4fa8626c
DJ
2986/* This function returns non-zero iff there is a software breakpoint
2987 inserted at PC. */
2988
2989int
3e43a32a
MS
2990software_breakpoint_inserted_here_p (struct address_space *aspace,
2991 CORE_ADDR pc)
4fa8626c 2992{
35df4500 2993 struct bp_location *bl, **blp_tmp;
4fa8626c 2994
35df4500 2995 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2996 {
35df4500 2997 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2998 continue;
2999
35df4500
TJB
3000 if (bl->inserted
3001 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 3002 aspace, pc))
4fa8626c
DJ
3003 {
3004 if (overlay_debugging
35df4500
TJB
3005 && section_is_overlay (bl->section)
3006 && !section_is_mapped (bl->section))
4fa8626c
DJ
3007 continue; /* unmapped overlay -- can't be a match */
3008 else
3009 return 1;
3010 }
3011 }
3012
1aafd4da 3013 /* Also check for software single-step breakpoints. */
6c95b8df 3014 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3015 return 1;
3016
4fa8626c
DJ
3017 return 0;
3018}
3019
9093389c
PA
3020int
3021hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3022 CORE_ADDR addr, ULONGEST len)
3023{
3024 struct breakpoint *bpt;
3025
3026 ALL_BREAKPOINTS (bpt)
3027 {
3028 struct bp_location *loc;
3029
3030 if (bpt->type != bp_hardware_watchpoint
3031 && bpt->type != bp_access_watchpoint)
3032 continue;
3033
3034 if (!breakpoint_enabled (bpt))
3035 continue;
3036
3037 for (loc = bpt->loc; loc; loc = loc->next)
3038 if (loc->pspace->aspace == aspace && loc->inserted)
3039 {
3040 CORE_ADDR l, h;
3041
3042 /* Check for intersection. */
3043 l = max (loc->address, addr);
3044 h = min (loc->address + loc->length, addr + len);
3045 if (l < h)
3046 return 1;
3047 }
3048 }
3049 return 0;
3050}
3051
075f6582
DJ
3052/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3053 PC is valid for process/thread PTID. */
c906108c
SS
3054
3055int
6c95b8df
PA
3056breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3057 ptid_t ptid)
c906108c 3058{
35df4500 3059 struct bp_location *bl, **blp_tmp;
4a306c9a 3060 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3061 int thread = -1;
4a306c9a 3062 int task = 0;
a6f1cd96 3063
35df4500 3064 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3065 {
35df4500
TJB
3066 if (bl->loc_type != bp_loc_software_breakpoint
3067 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3068 continue;
3069
35df4500
TJB
3070 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3071 if (!breakpoint_enabled (bl->owner)
3072 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3073 continue;
3074
f1310107 3075 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3076 continue;
3077
35df4500 3078 if (bl->owner->thread != -1)
075f6582 3079 {
a6f1cd96
JB
3080 /* This is a thread-specific breakpoint. Check that ptid
3081 matches that thread. If thread hasn't been computed yet,
3082 it is now time to do so. */
3083 if (thread == -1)
3084 thread = pid_to_thread_id (ptid);
35df4500 3085 if (bl->owner->thread != thread)
a6f1cd96 3086 continue;
075f6582 3087 }
a6f1cd96 3088
35df4500 3089 if (bl->owner->task != 0)
4a306c9a
JB
3090 {
3091 /* This is a task-specific breakpoint. Check that ptid
3092 matches that task. If task hasn't been computed yet,
3093 it is now time to do so. */
3094 if (task == 0)
3095 task = ada_get_task_number (ptid);
35df4500 3096 if (bl->owner->task != task)
4a306c9a
JB
3097 continue;
3098 }
3099
a6f1cd96 3100 if (overlay_debugging
35df4500
TJB
3101 && section_is_overlay (bl->section)
3102 && !section_is_mapped (bl->section))
a6f1cd96
JB
3103 continue; /* unmapped overlay -- can't be a match */
3104
3105 return 1;
c5aa993b 3106 }
c906108c
SS
3107
3108 return 0;
3109}
c906108c 3110\f
c5aa993b 3111
c906108c
SS
3112/* bpstat stuff. External routines' interfaces are documented
3113 in breakpoint.h. */
3114
3115int
fba45db2 3116ep_is_catchpoint (struct breakpoint *ep)
c906108c 3117{
533be4dd 3118 return (ep->type == bp_catchpoint);
c906108c
SS
3119}
3120
f431efe5
PA
3121/* Frees any storage that is part of a bpstat. Does not walk the
3122 'next' chain. */
3123
3124static void
198757a8
VP
3125bpstat_free (bpstat bs)
3126{
3127 if (bs->old_val != NULL)
3128 value_free (bs->old_val);
9add0f1b 3129 decref_counted_command_line (&bs->commands);
f431efe5 3130 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3131 xfree (bs);
3132}
3133
c906108c
SS
3134/* Clear a bpstat so that it says we are not at any breakpoint.
3135 Also free any storage that is part of a bpstat. */
3136
3137void
fba45db2 3138bpstat_clear (bpstat *bsp)
c906108c
SS
3139{
3140 bpstat p;
3141 bpstat q;
3142
3143 if (bsp == 0)
3144 return;
3145 p = *bsp;
3146 while (p != NULL)
3147 {
3148 q = p->next;
198757a8 3149 bpstat_free (p);
c906108c
SS
3150 p = q;
3151 }
3152 *bsp = NULL;
3153}
3154
3155/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3156 is part of the bpstat is copied as well. */
3157
3158bpstat
fba45db2 3159bpstat_copy (bpstat bs)
c906108c
SS
3160{
3161 bpstat p = NULL;
3162 bpstat tmp;
3163 bpstat retval = NULL;
3164
3165 if (bs == NULL)
3166 return bs;
3167
3168 for (; bs != NULL; bs = bs->next)
3169 {
3170 tmp = (bpstat) xmalloc (sizeof (*tmp));
3171 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3172 incref_counted_command_line (tmp->commands);
f431efe5 3173 incref_bp_location (tmp->bp_location_at);
31cc81e9 3174 if (bs->old_val != NULL)
3c3185ac
JK
3175 {
3176 tmp->old_val = value_copy (bs->old_val);
3177 release_value (tmp->old_val);
3178 }
31cc81e9 3179
c906108c
SS
3180 if (p == NULL)
3181 /* This is the first thing in the chain. */
3182 retval = tmp;
3183 else
3184 p->next = tmp;
3185 p = tmp;
3186 }
3187 p->next = NULL;
3188 return retval;
3189}
3190
4a64f543 3191/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3192
3193bpstat
fba45db2 3194bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3195{
c5aa993b
JM
3196 if (bsp == NULL)
3197 return NULL;
c906108c 3198
c5aa993b
JM
3199 for (; bsp != NULL; bsp = bsp->next)
3200 {
f431efe5 3201 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3202 return bsp;
3203 }
c906108c
SS
3204 return NULL;
3205}
3206
4a64f543
MS
3207/* Put in *NUM the breakpoint number of the first breakpoint we are
3208 stopped at. *BSP upon return is a bpstat which points to the
3209 remaining breakpoints stopped at (but which is not guaranteed to be
3210 good for anything but further calls to bpstat_num).
3211
8671a17b
PA
3212 Return 0 if passed a bpstat which does not indicate any breakpoints.
3213 Return -1 if stopped at a breakpoint that has been deleted since
3214 we set it.
3215 Return 1 otherwise. */
c906108c
SS
3216
3217int
8671a17b 3218bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3219{
3220 struct breakpoint *b;
3221
3222 if ((*bsp) == NULL)
3223 return 0; /* No more breakpoint values */
8671a17b 3224
4a64f543
MS
3225 /* We assume we'll never have several bpstats that correspond to a
3226 single breakpoint -- otherwise, this function might return the
3227 same number more than once and this will look ugly. */
f431efe5 3228 b = (*bsp)->breakpoint_at;
8671a17b
PA
3229 *bsp = (*bsp)->next;
3230 if (b == NULL)
3231 return -1; /* breakpoint that's been deleted since */
3232
3233 *num = b->number; /* We have its number */
3234 return 1;
c906108c
SS
3235}
3236
e93ca019 3237/* See breakpoint.h. */
c906108c
SS
3238
3239void
e93ca019 3240bpstat_clear_actions (void)
c906108c 3241{
e93ca019
JK
3242 struct thread_info *tp;
3243 bpstat bs;
3244
3245 if (ptid_equal (inferior_ptid, null_ptid))
3246 return;
3247
3248 tp = find_thread_ptid (inferior_ptid);
3249 if (tp == NULL)
3250 return;
3251
3252 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 3253 {
9add0f1b 3254 decref_counted_command_line (&bs->commands);
abf85f46 3255
c906108c
SS
3256 if (bs->old_val != NULL)
3257 {
3258 value_free (bs->old_val);
3259 bs->old_val = NULL;
3260 }
3261 }
3262}
3263
f3b1572e
PA
3264/* Called when a command is about to proceed the inferior. */
3265
3266static void
3267breakpoint_about_to_proceed (void)
3268{
3269 if (!ptid_equal (inferior_ptid, null_ptid))
3270 {
3271 struct thread_info *tp = inferior_thread ();
3272
3273 /* Allow inferior function calls in breakpoint commands to not
3274 interrupt the command list. When the call finishes
3275 successfully, the inferior will be standing at the same
3276 breakpoint as if nothing happened. */
16c381f0 3277 if (tp->control.in_infcall)
f3b1572e
PA
3278 return;
3279 }
3280
3281 breakpoint_proceeded = 1;
3282}
3283
4a64f543
MS
3284/* Stub for cleaning up our state if we error-out of a breakpoint
3285 command. */
c906108c 3286static void
4efb68b1 3287cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3288{
3289 executing_breakpoint_commands = 0;
3290}
3291
abf85f46
JK
3292/* Return non-zero iff CMD as the first line of a command sequence is `silent'
3293 or its equivalent. */
3294
3295static int
3296command_line_is_silent (struct command_line *cmd)
3297{
3298 return cmd && (strcmp ("silent", cmd->line) == 0
3299 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3300}
3301
4a64f543
MS
3302/* Execute all the commands associated with all the breakpoints at
3303 this location. Any of these commands could cause the process to
3304 proceed beyond this point, etc. We look out for such changes by
3305 checking the global "breakpoint_proceeded" after each command.
c906108c 3306
347bddb7
PA
3307 Returns true if a breakpoint command resumed the inferior. In that
3308 case, it is the caller's responsibility to recall it again with the
3309 bpstat of the current thread. */
3310
3311static int
3312bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3313{
3314 bpstat bs;
3315 struct cleanup *old_chain;
347bddb7 3316 int again = 0;
c906108c
SS
3317
3318 /* Avoid endless recursion if a `source' command is contained
3319 in bs->commands. */
3320 if (executing_breakpoint_commands)
347bddb7 3321 return 0;
c906108c
SS
3322
3323 executing_breakpoint_commands = 1;
3324 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3325
cf6c5ffb
TT
3326 prevent_dont_repeat ();
3327
4a64f543 3328 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3329 bs = *bsp;
3330
3331 breakpoint_proceeded = 0;
3332 for (; bs != NULL; bs = bs->next)
3333 {
9add0f1b 3334 struct counted_command_line *ccmd;
6c50ab1c
JB
3335 struct command_line *cmd;
3336 struct cleanup *this_cmd_tree_chain;
3337
3338 /* Take ownership of the BSP's command tree, if it has one.
3339
3340 The command tree could legitimately contain commands like
3341 'step' and 'next', which call clear_proceed_status, which
3342 frees stop_bpstat's command tree. To make sure this doesn't
3343 free the tree we're executing out from under us, we need to
3344 take ownership of the tree ourselves. Since a given bpstat's
3345 commands are only executed once, we don't need to copy it; we
3346 can clear the pointer in the bpstat, and make sure we free
3347 the tree when we're done. */
9add0f1b
TT
3348 ccmd = bs->commands;
3349 bs->commands = NULL;
abf85f46
JK
3350 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
3351 cmd = ccmd ? ccmd->commands : NULL;
3352 if (command_line_is_silent (cmd))
3353 {
3354 /* The action has been already done by bpstat_stop_status. */
3355 cmd = cmd->next;
3356 }
6c50ab1c 3357
c906108c
SS
3358 while (cmd != NULL)
3359 {
3360 execute_control_command (cmd);
3361
3362 if (breakpoint_proceeded)
3363 break;
3364 else
3365 cmd = cmd->next;
3366 }
6c50ab1c
JB
3367
3368 /* We can free this command tree now. */
3369 do_cleanups (this_cmd_tree_chain);
3370
c906108c 3371 if (breakpoint_proceeded)
32c1e744
VP
3372 {
3373 if (target_can_async_p ())
347bddb7
PA
3374 /* If we are in async mode, then the target might be still
3375 running, not stopped at any breakpoint, so nothing for
3376 us to do here -- just return to the event loop. */
3377 ;
32c1e744
VP
3378 else
3379 /* In sync mode, when execute_control_command returns
3380 we're already standing on the next breakpoint.
347bddb7
PA
3381 Breakpoint commands for that stop were not run, since
3382 execute_command does not run breakpoint commands --
3383 only command_line_handler does, but that one is not
3384 involved in execution of breakpoint commands. So, we
3385 can now execute breakpoint commands. It should be
3386 noted that making execute_command do bpstat actions is
3387 not an option -- in this case we'll have recursive
3388 invocation of bpstat for each breakpoint with a
3389 command, and can easily blow up GDB stack. Instead, we
3390 return true, which will trigger the caller to recall us
3391 with the new stop_bpstat. */
3392 again = 1;
3393 break;
32c1e744 3394 }
c906108c 3395 }
c2b8ed2c 3396 do_cleanups (old_chain);
347bddb7
PA
3397 return again;
3398}
3399
3400void
3401bpstat_do_actions (void)
3402{
353d1d73
JK
3403 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
3404
347bddb7
PA
3405 /* Do any commands attached to breakpoint we are stopped at. */
3406 while (!ptid_equal (inferior_ptid, null_ptid)
3407 && target_has_execution
3408 && !is_exited (inferior_ptid)
3409 && !is_executing (inferior_ptid))
3410 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3411 and only return when it is stopped at the next breakpoint, we
3412 keep doing breakpoint actions until it returns false to
3413 indicate the inferior was not resumed. */
16c381f0 3414 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3415 break;
353d1d73
JK
3416
3417 discard_cleanups (cleanup_if_error);
c906108c
SS
3418}
3419
fa4727a6
DJ
3420/* Print out the (old or new) value associated with a watchpoint. */
3421
3422static void
3423watchpoint_value_print (struct value *val, struct ui_file *stream)
3424{
3425 if (val == NULL)
3426 fprintf_unfiltered (stream, _("<unreadable>"));
3427 else
79a45b7d
TT
3428 {
3429 struct value_print_options opts;
3430 get_user_print_options (&opts);
3431 value_print (val, stream, &opts);
3432 }
fa4727a6
DJ
3433}
3434
e514a9d6 3435/* Generic routine for printing messages indicating why we
4a64f543 3436 stopped. The behavior of this function depends on the value
e514a9d6
JM
3437 'print_it' in the bpstat structure. Under some circumstances we
3438 may decide not to print anything here and delegate the task to
4a64f543 3439 normal_stop(). */
e514a9d6
JM
3440
3441static enum print_stop_action
3442print_bp_stop_message (bpstat bs)
3443{
3444 switch (bs->print_it)
3445 {
3446 case print_it_noop:
4a64f543 3447 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3448 return PRINT_UNKNOWN;
3449 break;
3450
3451 case print_it_done:
3452 /* We still want to print the frame, but we already printed the
4a64f543 3453 relevant messages. */
e514a9d6
JM
3454 return PRINT_SRC_AND_LOC;
3455 break;
3456
3457 case print_it_normal:
4f8d1dc6 3458 {
f431efe5
PA
3459 struct breakpoint *b = bs->breakpoint_at;
3460
1a6a67de
TJB
3461 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3462 which has since been deleted. */
3463 if (b == NULL)
3464 return PRINT_UNKNOWN;
3465
348d480f
PA
3466 /* Normal case. Call the breakpoint's print_it method. */
3467 return b->ops->print_it (bs);
4f8d1dc6 3468 }
348d480f 3469 break;
3086aeae 3470
e514a9d6 3471 default:
8e65ff28 3472 internal_error (__FILE__, __LINE__,
e2e0b3e5 3473 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3474 break;
c906108c 3475 }
c906108c
SS
3476}
3477
e514a9d6
JM
3478/* Print a message indicating what happened. This is called from
3479 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
3480 list - a list of the eventpoints that caused this stop. KIND is
3481 the target_waitkind for the stopping event. This
e514a9d6
JM
3482 routine calls the generic print routine for printing a message
3483 about reasons for stopping. This will print (for example) the
3484 "Breakpoint n," part of the output. The return value of this
3485 routine is one of:
c906108c 3486
4a64f543 3487 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3488 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3489 code to print the location. An example is
c5aa993b
JM
3490 "Breakpoint 1, " which should be followed by
3491 the location.
917317f4 3492 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3493 to also print the location part of the message.
3494 An example is the catch/throw messages, which
4a64f543 3495 don't require a location appended to the end.
917317f4 3496 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3497 further info to be printed. */
c906108c 3498
917317f4 3499enum print_stop_action
36dfb11c 3500bpstat_print (bpstat bs, int kind)
c906108c
SS
3501{
3502 int val;
c5aa993b 3503
c906108c 3504 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3505 (Currently all watchpoints go on the bpstat whether hit or not.
3506 That probably could (should) be changed, provided care is taken
c906108c 3507 with respect to bpstat_explains_signal). */
e514a9d6
JM
3508 for (; bs; bs = bs->next)
3509 {
3510 val = print_bp_stop_message (bs);
3511 if (val == PRINT_SRC_ONLY
3512 || val == PRINT_SRC_AND_LOC
3513 || val == PRINT_NOTHING)
3514 return val;
3515 }
c906108c 3516
36dfb11c
TT
3517 /* If we had hit a shared library event breakpoint,
3518 print_bp_stop_message would print out this message. If we hit an
3519 OS-level shared library event, do the same thing. */
3520 if (kind == TARGET_WAITKIND_LOADED)
3521 {
3522 ui_out_text (current_uiout, _("Stopped due to shared library event\n"));
3523 if (ui_out_is_mi_like_p (current_uiout))
3524 ui_out_field_string (current_uiout, "reason",
3525 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
3526 return PRINT_NOTHING;
3527 }
3528
e514a9d6 3529 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3530 with and nothing was printed. */
917317f4 3531 return PRINT_UNKNOWN;
c906108c
SS
3532}
3533
4a64f543
MS
3534/* Evaluate the expression EXP and return 1 if value is zero. This is
3535 used inside a catch_errors to evaluate the breakpoint condition.
3536 The argument is a "struct expression *" that has been cast to a
3537 "char *" to make it pass through catch_errors. */
c906108c
SS
3538
3539static int
4efb68b1 3540breakpoint_cond_eval (void *exp)
c906108c 3541{
278cd55f 3542 struct value *mark = value_mark ();
c5aa993b 3543 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3544
c906108c
SS
3545 value_free_to_mark (mark);
3546 return i;
3547}
3548
5760d0ab 3549/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3550
3551static bpstat
5760d0ab 3552bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3553{
3554 bpstat bs;
3555
3556 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3557 bs->next = NULL;
3558 **bs_link_pointer = bs;
3559 *bs_link_pointer = &bs->next;
f431efe5
PA
3560 bs->breakpoint_at = bl->owner;
3561 bs->bp_location_at = bl;
3562 incref_bp_location (bl);
c906108c
SS
3563 /* If the condition is false, etc., don't do the commands. */
3564 bs->commands = NULL;
3565 bs->old_val = NULL;
3566 bs->print_it = print_it_normal;
3567 return bs;
3568}
3569\f
d983da9c
DJ
3570/* The target has stopped with waitstatus WS. Check if any hardware
3571 watchpoints have triggered, according to the target. */
3572
3573int
3574watchpoints_triggered (struct target_waitstatus *ws)
3575{
d92524f1 3576 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3577 CORE_ADDR addr;
3578 struct breakpoint *b;
3579
3580 if (!stopped_by_watchpoint)
3581 {
3582 /* We were not stopped by a watchpoint. Mark all watchpoints
3583 as not triggered. */
3584 ALL_BREAKPOINTS (b)
cc60f2e3 3585 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3586 {
3587 struct watchpoint *w = (struct watchpoint *) b;
3588
3589 w->watchpoint_triggered = watch_triggered_no;
3590 }
d983da9c
DJ
3591
3592 return 0;
3593 }
3594
3595 if (!target_stopped_data_address (&current_target, &addr))
3596 {
3597 /* We were stopped by a watchpoint, but we don't know where.
3598 Mark all watchpoints as unknown. */
3599 ALL_BREAKPOINTS (b)
cc60f2e3 3600 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3601 {
3602 struct watchpoint *w = (struct watchpoint *) b;
3603
3604 w->watchpoint_triggered = watch_triggered_unknown;
3605 }
d983da9c
DJ
3606
3607 return stopped_by_watchpoint;
3608 }
3609
3610 /* The target could report the data address. Mark watchpoints
3611 affected by this data address as triggered, and all others as not
3612 triggered. */
3613
3614 ALL_BREAKPOINTS (b)
cc60f2e3 3615 if (is_hardware_watchpoint (b))
d983da9c 3616 {
3a5c3e22 3617 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 3618 struct bp_location *loc;
d983da9c 3619
3a5c3e22 3620 w->watchpoint_triggered = watch_triggered_no;
a5606eee 3621 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 3622 {
3a5c3e22 3623 if (is_masked_watchpoint (b))
9c06b0b4 3624 {
3a5c3e22
PA
3625 CORE_ADDR newaddr = addr & w->hw_wp_mask;
3626 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
3627
3628 if (newaddr == start)
3629 {
3a5c3e22 3630 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3631 break;
3632 }
3633 }
3634 /* Exact match not required. Within range is sufficient. */
3635 else if (target_watchpoint_addr_within_range (&current_target,
3636 addr, loc->address,
3637 loc->length))
3638 {
3a5c3e22 3639 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3640 break;
3641 }
3642 }
d983da9c
DJ
3643 }
3644
3645 return 1;
3646}
3647
c906108c
SS
3648/* Possible return values for watchpoint_check (this can't be an enum
3649 because of check_errors). */
3650/* The watchpoint has been deleted. */
3651#define WP_DELETED 1
3652/* The value has changed. */
3653#define WP_VALUE_CHANGED 2
3654/* The value has not changed. */
3655#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3656/* Ignore this watchpoint, no matter if the value changed or not. */
3657#define WP_IGNORE 4
c906108c
SS
3658
3659#define BP_TEMPFLAG 1
3660#define BP_HARDWAREFLAG 2
3661
4a64f543
MS
3662/* Evaluate watchpoint condition expression and check if its value
3663 changed.
553e4c11
JB
3664
3665 P should be a pointer to struct bpstat, but is defined as a void *
3666 in order for this function to be usable with catch_errors. */
c906108c
SS
3667
3668static int
4efb68b1 3669watchpoint_check (void *p)
c906108c
SS
3670{
3671 bpstat bs = (bpstat) p;
3a5c3e22 3672 struct watchpoint *b;
c906108c
SS
3673 struct frame_info *fr;
3674 int within_current_scope;
3675
f431efe5 3676 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3677 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 3678 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 3679
f6bc2008
PA
3680 /* If this is a local watchpoint, we only want to check if the
3681 watchpoint frame is in scope if the current thread is the thread
3682 that was used to create the watchpoint. */
3683 if (!watchpoint_in_thread_scope (b))
60e1c644 3684 return WP_IGNORE;
f6bc2008 3685
c906108c
SS
3686 if (b->exp_valid_block == NULL)
3687 within_current_scope = 1;
3688 else
3689 {
edb3359d
DJ
3690 struct frame_info *frame = get_current_frame ();
3691 struct gdbarch *frame_arch = get_frame_arch (frame);
3692 CORE_ADDR frame_pc = get_frame_pc (frame);
3693
4a64f543
MS
3694 /* in_function_epilogue_p() returns a non-zero value if we're
3695 still in the function but the stack frame has already been
3696 invalidated. Since we can't rely on the values of local
3697 variables after the stack has been destroyed, we are treating
3698 the watchpoint in that state as `not changed' without further
3699 checking. Don't mark watchpoints as changed if the current
3700 frame is in an epilogue - even if they are in some other
3701 frame, our view of the stack is likely to be wrong and
3702 frame_find_by_id could error out. */
a0f49112 3703 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3704 return WP_IGNORE;
a0f49112 3705
101dcfbe 3706 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3707 within_current_scope = (fr != NULL);
69fbadd5
DJ
3708
3709 /* If we've gotten confused in the unwinder, we might have
3710 returned a frame that can't describe this variable. */
edb3359d
DJ
3711 if (within_current_scope)
3712 {
3713 struct symbol *function;
3714
3715 function = get_frame_function (fr);
3716 if (function == NULL
3717 || !contained_in (b->exp_valid_block,
3718 SYMBOL_BLOCK_VALUE (function)))
3719 within_current_scope = 0;
3720 }
69fbadd5 3721
edb3359d 3722 if (within_current_scope)
c906108c
SS
3723 /* If we end up stopping, the current frame will get selected
3724 in normal_stop. So this call to select_frame won't affect
3725 the user. */
0f7d239c 3726 select_frame (fr);
c906108c 3727 }
c5aa993b 3728
c906108c
SS
3729 if (within_current_scope)
3730 {
4a64f543
MS
3731 /* We use value_{,free_to_}mark because it could be a *long*
3732 time before we return to the command level and call
3733 free_all_values. We can't call free_all_values because we
3734 might be in the middle of evaluating a function call. */
c906108c 3735
0cf6dd15 3736 int pc = 0;
9c06b0b4 3737 struct value *mark;
fa4727a6
DJ
3738 struct value *new_val;
3739
3a5c3e22 3740 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
3741 /* Since we don't know the exact trigger address (from
3742 stopped_data_address), just tell the user we've triggered
3743 a mask watchpoint. */
3744 return WP_VALUE_CHANGED;
3745
3746 mark = value_mark ();
0cf6dd15 3747 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3748
4a64f543
MS
3749 /* We use value_equal_contents instead of value_equal because
3750 the latter coerces an array to a pointer, thus comparing just
3751 the address of the array instead of its contents. This is
3752 not what we want. */
fa4727a6 3753 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3754 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3755 {
fa4727a6
DJ
3756 if (new_val != NULL)
3757 {
3758 release_value (new_val);
3759 value_free_to_mark (mark);
3760 }
c906108c
SS
3761 bs->old_val = b->val;
3762 b->val = new_val;
fa4727a6 3763 b->val_valid = 1;
c906108c
SS
3764 return WP_VALUE_CHANGED;
3765 }
3766 else
3767 {
60e1c644 3768 /* Nothing changed. */
c906108c 3769 value_free_to_mark (mark);
c906108c
SS
3770 return WP_VALUE_NOT_CHANGED;
3771 }
3772 }
3773 else
3774 {
79a45e25
PA
3775 struct ui_out *uiout = current_uiout;
3776
c906108c 3777 /* This seems like the only logical thing to do because
c5aa993b
JM
3778 if we temporarily ignored the watchpoint, then when
3779 we reenter the block in which it is valid it contains
3780 garbage (in the case of a function, it may have two
3781 garbage values, one before and one after the prologue).
3782 So we can't even detect the first assignment to it and
3783 watch after that (since the garbage may or may not equal
3784 the first value assigned). */
348d480f
PA
3785 /* We print all the stop information in
3786 breakpoint_ops->print_it, but in this case, by the time we
3787 call breakpoint_ops->print_it this bp will be deleted
3788 already. So we have no choice but print the information
3789 here. */
9dc5e2a9 3790 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3791 ui_out_field_string
3792 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3793 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 3794 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
3795 ui_out_text (uiout,
3796 " deleted because the program has left the block in\n\
8b93c638 3797which its expression is valid.\n");
4ce44c66 3798
cdac0397 3799 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 3800 decref_counted_command_line (&b->base.commands);
d0fb5eae 3801 watchpoint_del_at_next_stop (b);
c906108c
SS
3802
3803 return WP_DELETED;
3804 }
3805}
3806
18a18393 3807/* Return true if it looks like target has stopped due to hitting
348d480f
PA
3808 breakpoint location BL. This function does not check if we should
3809 stop, only if BL explains the stop. */
3810
18a18393 3811static int
6c95b8df
PA
3812bpstat_check_location (const struct bp_location *bl,
3813 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3814{
3815 struct breakpoint *b = bl->owner;
3816
348d480f 3817 /* BL is from an existing breakpoint. */
2bdf28a0
JK
3818 gdb_assert (b != NULL);
3819
348d480f 3820 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
18a18393
VP
3821}
3822
3a5c3e22
PA
3823/* Determine if the watched values have actually changed, and we
3824 should stop. If not, set BS->stop to 0. */
3825
18a18393
VP
3826static void
3827bpstat_check_watchpoint (bpstat bs)
3828{
2bdf28a0 3829 const struct bp_location *bl;
3a5c3e22 3830 struct watchpoint *b;
2bdf28a0
JK
3831
3832 /* BS is built for existing struct breakpoint. */
f431efe5 3833 bl = bs->bp_location_at;
2bdf28a0 3834 gdb_assert (bl != NULL);
3a5c3e22 3835 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 3836 gdb_assert (b != NULL);
18a18393 3837
18a18393 3838 {
18a18393
VP
3839 int must_check_value = 0;
3840
3a5c3e22 3841 if (b->base.type == bp_watchpoint)
18a18393
VP
3842 /* For a software watchpoint, we must always check the
3843 watched value. */
3844 must_check_value = 1;
3845 else if (b->watchpoint_triggered == watch_triggered_yes)
3846 /* We have a hardware watchpoint (read, write, or access)
3847 and the target earlier reported an address watched by
3848 this watchpoint. */
3849 must_check_value = 1;
3850 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 3851 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
3852 /* We were stopped by a hardware watchpoint, but the target could
3853 not report the data address. We must check the watchpoint's
3854 value. Access and read watchpoints are out of luck; without
3855 a data address, we can't figure it out. */
3856 must_check_value = 1;
3a5c3e22 3857
18a18393
VP
3858 if (must_check_value)
3859 {
3e43a32a
MS
3860 char *message
3861 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 3862 b->base.number);
18a18393
VP
3863 struct cleanup *cleanups = make_cleanup (xfree, message);
3864 int e = catch_errors (watchpoint_check, bs, message,
3865 RETURN_MASK_ALL);
3866 do_cleanups (cleanups);
3867 switch (e)
3868 {
3869 case WP_DELETED:
3870 /* We've already printed what needs to be printed. */
3871 bs->print_it = print_it_done;
3872 /* Stop. */
3873 break;
60e1c644
PA
3874 case WP_IGNORE:
3875 bs->print_it = print_it_noop;
3876 bs->stop = 0;
3877 break;
18a18393 3878 case WP_VALUE_CHANGED:
3a5c3e22 3879 if (b->base.type == bp_read_watchpoint)
18a18393 3880 {
85d721b8
PA
3881 /* There are two cases to consider here:
3882
4a64f543 3883 1. We're watching the triggered memory for reads.
85d721b8
PA
3884 In that case, trust the target, and always report
3885 the watchpoint hit to the user. Even though
3886 reads don't cause value changes, the value may
3887 have changed since the last time it was read, and
3888 since we're not trapping writes, we will not see
3889 those, and as such we should ignore our notion of
3890 old value.
3891
4a64f543 3892 2. We're watching the triggered memory for both
85d721b8
PA
3893 reads and writes. There are two ways this may
3894 happen:
3895
4a64f543 3896 2.1. This is a target that can't break on data
85d721b8
PA
3897 reads only, but can break on accesses (reads or
3898 writes), such as e.g., x86. We detect this case
3899 at the time we try to insert read watchpoints.
3900
4a64f543 3901 2.2. Otherwise, the target supports read
85d721b8
PA
3902 watchpoints, but, the user set an access or write
3903 watchpoint watching the same memory as this read
3904 watchpoint.
3905
3906 If we're watching memory writes as well as reads,
3907 ignore watchpoint hits when we find that the
3908 value hasn't changed, as reads don't cause
3909 changes. This still gives false positives when
3910 the program writes the same value to memory as
3911 what there was already in memory (we will confuse
3912 it for a read), but it's much better than
3913 nothing. */
3914
3915 int other_write_watchpoint = 0;
3916
3917 if (bl->watchpoint_type == hw_read)
3918 {
3919 struct breakpoint *other_b;
3920
3921 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
3922 if (other_b->type == bp_hardware_watchpoint
3923 || other_b->type == bp_access_watchpoint)
85d721b8 3924 {
3a5c3e22
PA
3925 struct watchpoint *other_w =
3926 (struct watchpoint *) other_b;
3927
3928 if (other_w->watchpoint_triggered
3929 == watch_triggered_yes)
3930 {
3931 other_write_watchpoint = 1;
3932 break;
3933 }
85d721b8
PA
3934 }
3935 }
3936
3937 if (other_write_watchpoint
3938 || bl->watchpoint_type == hw_access)
3939 {
3940 /* We're watching the same memory for writes,
3941 and the value changed since the last time we
3942 updated it, so this trap must be for a write.
3943 Ignore it. */
3944 bs->print_it = print_it_noop;
3945 bs->stop = 0;
3946 }
18a18393
VP
3947 }
3948 break;
3949 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
3950 if (b->base.type == bp_hardware_watchpoint
3951 || b->base.type == bp_watchpoint)
18a18393
VP
3952 {
3953 /* Don't stop: write watchpoints shouldn't fire if
3954 the value hasn't changed. */
3955 bs->print_it = print_it_noop;
3956 bs->stop = 0;
3957 }
3958 /* Stop. */
3959 break;
3960 default:
3961 /* Can't happen. */
3962 case 0:
3963 /* Error from catch_errors. */
3a5c3e22 3964 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 3965 watchpoint_del_at_next_stop (b);
18a18393
VP
3966 /* We've already printed what needs to be printed. */
3967 bs->print_it = print_it_done;
3968 break;
3969 }
3970 }
3971 else /* must_check_value == 0 */
3972 {
3973 /* This is a case where some watchpoint(s) triggered, but
3974 not at the address of this watchpoint, or else no
3975 watchpoint triggered after all. So don't print
3976 anything for this watchpoint. */
3977 bs->print_it = print_it_noop;
3978 bs->stop = 0;
3979 }
3980 }
3981}
3982
3983
3984/* Check conditions (condition proper, frame, thread and ignore count)
3985 of breakpoint referred to by BS. If we should not stop for this
3986 breakpoint, set BS->stop to 0. */
f431efe5 3987
18a18393
VP
3988static void
3989bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3990{
3991 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
3992 const struct bp_location *bl;
3993 struct breakpoint *b;
3994
3995 /* BS is built for existing struct breakpoint. */
f431efe5 3996 bl = bs->bp_location_at;
2bdf28a0 3997 gdb_assert (bl != NULL);
f431efe5 3998 b = bs->breakpoint_at;
2bdf28a0 3999 gdb_assert (b != NULL);
18a18393
VP
4000
4001 if (frame_id_p (b->frame_id)
edb3359d 4002 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4003 bs->stop = 0;
4004 else if (bs->stop)
4005 {
4006 int value_is_zero = 0;
60e1c644
PA
4007 struct expression *cond;
4008
7371cf6d
PM
4009 /* Evaluate Python breakpoints that have a "stop"
4010 method implemented. */
4011 if (b->py_bp_object)
4012 bs->stop = gdbpy_should_stop (b->py_bp_object);
4013
60e1c644 4014 if (is_watchpoint (b))
3a5c3e22
PA
4015 {
4016 struct watchpoint *w = (struct watchpoint *) b;
4017
4018 cond = w->cond_exp;
4019 }
60e1c644
PA
4020 else
4021 cond = bl->cond;
4022
f431efe5 4023 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4024 {
60e1c644 4025 int within_current_scope = 1;
3a5c3e22 4026 struct watchpoint * w;
60e1c644 4027
c5bc3a77
DJ
4028 /* We use value_mark and value_free_to_mark because it could
4029 be a long time before we return to the command level and
4030 call free_all_values. We can't call free_all_values
4031 because we might be in the middle of evaluating a
4032 function call. */
4033 struct value *mark = value_mark ();
4034
3a5c3e22
PA
4035 if (is_watchpoint (b))
4036 w = (struct watchpoint *) b;
4037 else
4038 w = NULL;
4039
edb3359d
DJ
4040 /* Need to select the frame, with all that implies so that
4041 the conditions will have the right context. Because we
4042 use the frame, we will not see an inlined function's
4043 variables when we arrive at a breakpoint at the start
4044 of the inlined function; the current frame will be the
4045 call site. */
3a5c3e22 4046 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
4047 select_frame (get_current_frame ());
4048 else
4049 {
4050 struct frame_info *frame;
4051
4052 /* For local watchpoint expressions, which particular
4053 instance of a local is being watched matters, so we
4054 keep track of the frame to evaluate the expression
4055 in. To evaluate the condition however, it doesn't
4056 really matter which instantiation of the function
4057 where the condition makes sense triggers the
4058 watchpoint. This allows an expression like "watch
4059 global if q > 10" set in `func', catch writes to
4060 global on all threads that call `func', or catch
4061 writes on all recursive calls of `func' by a single
4062 thread. We simply always evaluate the condition in
4063 the innermost frame that's executing where it makes
4064 sense to evaluate the condition. It seems
4065 intuitive. */
3a5c3e22 4066 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
4067 if (frame != NULL)
4068 select_frame (frame);
4069 else
4070 within_current_scope = 0;
4071 }
4072 if (within_current_scope)
4073 value_is_zero
4074 = catch_errors (breakpoint_cond_eval, cond,
4075 "Error in testing breakpoint condition:\n",
4076 RETURN_MASK_ALL);
4077 else
4078 {
4079 warning (_("Watchpoint condition cannot be tested "
4080 "in the current scope"));
4081 /* If we failed to set the right context for this
4082 watchpoint, unconditionally report it. */
4083 value_is_zero = 0;
4084 }
4a64f543 4085 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4086 value_free_to_mark (mark);
18a18393 4087 }
60e1c644
PA
4088
4089 if (cond && value_is_zero)
18a18393
VP
4090 {
4091 bs->stop = 0;
4092 }
4093 else if (b->thread != -1 && b->thread != thread_id)
4094 {
4095 bs->stop = 0;
4096 }
4097 else if (b->ignore_count > 0)
4098 {
4099 b->ignore_count--;
4100 annotate_ignore_count_change ();
4101 bs->stop = 0;
4a64f543 4102 /* Increase the hit count even though we don't stop. */
18a18393 4103 ++(b->hit_count);
8d3788bd 4104 observer_notify_breakpoint_modified (b);
18a18393
VP
4105 }
4106 }
4107}
4108
4109
9709f61c 4110/* Get a bpstat associated with having just stopped at address
d983da9c 4111 BP_ADDR in thread PTID.
c906108c 4112
d983da9c 4113 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4114 don't understand this stop. Result is a chain of bpstat's such
4115 that:
c906108c 4116
c5aa993b 4117 if we don't understand the stop, the result is a null pointer.
c906108c 4118
c5aa993b 4119 if we understand why we stopped, the result is not null.
c906108c 4120
c5aa993b
JM
4121 Each element of the chain refers to a particular breakpoint or
4122 watchpoint at which we have stopped. (We may have stopped for
4123 several reasons concurrently.)
c906108c 4124
c5aa993b
JM
4125 Each element of the chain has valid next, breakpoint_at,
4126 commands, FIXME??? fields. */
c906108c
SS
4127
4128bpstat
6c95b8df
PA
4129bpstat_stop_status (struct address_space *aspace,
4130 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4131{
0d381245 4132 struct breakpoint *b = NULL;
afe38095 4133 struct bp_location *bl;
20874c92 4134 struct bp_location *loc;
5760d0ab
JK
4135 /* First item of allocated bpstat's. */
4136 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4137 /* Pointer to the last thing in the chain currently. */
5760d0ab 4138 bpstat bs;
20874c92 4139 int ix;
429374b8 4140 int need_remove_insert;
f431efe5 4141 int removed_any;
c906108c 4142
f431efe5
PA
4143 /* First, build the bpstat chain with locations that explain a
4144 target stop, while being careful to not set the target running,
4145 as that may invalidate locations (in particular watchpoint
4146 locations are recreated). Resuming will happen here with
4147 breakpoint conditions or watchpoint expressions that include
4148 inferior function calls. */
c5aa993b 4149
429374b8
JK
4150 ALL_BREAKPOINTS (b)
4151 {
4152 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4153 continue;
a5606eee 4154
429374b8
JK
4155 for (bl = b->loc; bl != NULL; bl = bl->next)
4156 {
4a64f543
MS
4157 /* For hardware watchpoints, we look only at the first
4158 location. The watchpoint_check function will work on the
4159 entire expression, not the individual locations. For
4160 read watchpoints, the watchpoints_triggered function has
4161 checked all locations already. */
429374b8
JK
4162 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4163 break;
18a18393 4164
429374b8
JK
4165 if (bl->shlib_disabled)
4166 continue;
c5aa993b 4167
429374b8
JK
4168 if (!bpstat_check_location (bl, aspace, bp_addr))
4169 continue;
c5aa993b 4170
4a64f543
MS
4171 /* Come here if it's a watchpoint, or if the break address
4172 matches. */
c5aa993b 4173
4a64f543
MS
4174 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4175 explain stop. */
c5aa993b 4176
f431efe5
PA
4177 /* Assume we stop. Should we find a watchpoint that is not
4178 actually triggered, or if the condition of the breakpoint
4179 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4180 bs->stop = 1;
4181 bs->print = 1;
d983da9c 4182
f431efe5
PA
4183 /* If this is a scope breakpoint, mark the associated
4184 watchpoint as triggered so that we will handle the
4185 out-of-scope event. We'll get to the watchpoint next
4186 iteration. */
d0fb5eae 4187 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4188 {
4189 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4190
4191 w->watchpoint_triggered = watch_triggered_yes;
4192 }
f431efe5
PA
4193 }
4194 }
4195
4196 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4197 {
f1310107 4198 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4199 {
5760d0ab 4200 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4201 /* For hits of moribund locations, we should just proceed. */
4202 bs->stop = 0;
4203 bs->print = 0;
4204 bs->print_it = print_it_noop;
4205 }
4206 }
4207
f431efe5
PA
4208 /* Now go through the locations that caused the target to stop, and
4209 check whether we're interested in reporting this stop to higher
4210 layers, or whether we should resume the target transparently. */
4211
4212 removed_any = 0;
4213
5760d0ab 4214 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4215 {
4216 if (!bs->stop)
4217 continue;
4218
f431efe5 4219 b = bs->breakpoint_at;
348d480f
PA
4220 b->ops->check_status (bs);
4221 if (bs->stop)
28010a5d 4222 {
348d480f 4223 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4224
429374b8
JK
4225 if (bs->stop)
4226 {
4227 ++(b->hit_count);
8d3788bd 4228 observer_notify_breakpoint_modified (b);
c906108c 4229
4a64f543 4230 /* We will stop here. */
429374b8
JK
4231 if (b->disposition == disp_disable)
4232 {
4233 if (b->enable_state != bp_permanent)
4234 b->enable_state = bp_disabled;
f431efe5 4235 removed_any = 1;
429374b8
JK
4236 }
4237 if (b->silent)
4238 bs->print = 0;
4239 bs->commands = b->commands;
9add0f1b 4240 incref_counted_command_line (bs->commands);
abf85f46
JK
4241 if (command_line_is_silent (bs->commands
4242 ? bs->commands->commands : NULL))
4243 bs->print = 0;
429374b8
JK
4244 }
4245
e5dd4106 4246 /* Print nothing for this entry if we don't stop or don't print. */
429374b8
JK
4247 if (bs->stop == 0 || bs->print == 0)
4248 bs->print_it = print_it_noop;
348d480f 4249 }
429374b8 4250 }
876fa593 4251
d983da9c
DJ
4252 /* If we aren't stopping, the value of some hardware watchpoint may
4253 not have changed, but the intermediate memory locations we are
4254 watching may have. Don't bother if we're stopping; this will get
4255 done later. */
d832cb68 4256 need_remove_insert = 0;
5760d0ab
JK
4257 if (! bpstat_causes_stop (bs_head))
4258 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4259 if (!bs->stop
f431efe5
PA
4260 && bs->breakpoint_at
4261 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4262 {
3a5c3e22
PA
4263 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4264
4265 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 4266 need_remove_insert = 1;
d983da9c
DJ
4267 }
4268
d832cb68 4269 if (need_remove_insert)
2d134ed3 4270 update_global_location_list (1);
f431efe5
PA
4271 else if (removed_any)
4272 update_global_location_list (0);
d832cb68 4273
5760d0ab 4274 return bs_head;
c906108c 4275}
628fe4e4
JK
4276
4277static void
4278handle_jit_event (void)
4279{
4280 struct frame_info *frame;
4281 struct gdbarch *gdbarch;
4282
4283 /* Switch terminal for any messages produced by
4284 breakpoint_re_set. */
4285 target_terminal_ours_for_output ();
4286
4287 frame = get_current_frame ();
4288 gdbarch = get_frame_arch (frame);
4289
4290 jit_event_handler (gdbarch);
4291
4292 target_terminal_inferior ();
4293}
4294
4295/* Prepare WHAT final decision for infrun. */
4296
4297/* Decide what infrun needs to do with this bpstat. */
4298
c906108c 4299struct bpstat_what
0e30163f 4300bpstat_what (bpstat bs_head)
c906108c 4301{
c906108c 4302 struct bpstat_what retval;
628fe4e4
JK
4303 /* We need to defer calling `solib_add', as adding new symbols
4304 resets breakpoints, which in turn deletes breakpoint locations,
4305 and hence may clear unprocessed entries in the BS chain. */
4306 int shlib_event = 0;
4307 int jit_event = 0;
0e30163f 4308 bpstat bs;
c906108c 4309
628fe4e4 4310 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4311 retval.call_dummy = STOP_NONE;
186c406b 4312 retval.is_longjmp = 0;
628fe4e4 4313
0e30163f 4314 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4315 {
628fe4e4
JK
4316 /* Extract this BS's action. After processing each BS, we check
4317 if its action overrides all we've seem so far. */
4318 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4319 enum bptype bptype;
4320
c906108c 4321 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4322 {
4323 /* I suspect this can happen if it was a momentary
4324 breakpoint which has since been deleted. */
4325 bptype = bp_none;
4326 }
20874c92 4327 else
f431efe5 4328 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4329
4330 switch (bptype)
c906108c
SS
4331 {
4332 case bp_none:
628fe4e4 4333 break;
c906108c
SS
4334 case bp_breakpoint:
4335 case bp_hardware_breakpoint:
4336 case bp_until:
4337 case bp_finish:
4338 if (bs->stop)
4339 {
4340 if (bs->print)
628fe4e4 4341 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4342 else
628fe4e4 4343 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4344 }
4345 else
628fe4e4 4346 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4347 break;
4348 case bp_watchpoint:
4349 case bp_hardware_watchpoint:
4350 case bp_read_watchpoint:
4351 case bp_access_watchpoint:
4352 if (bs->stop)
4353 {
4354 if (bs->print)
628fe4e4 4355 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4356 else
628fe4e4 4357 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4358 }
4359 else
628fe4e4
JK
4360 {
4361 /* There was a watchpoint, but we're not stopping.
4362 This requires no further action. */
4363 }
c906108c
SS
4364 break;
4365 case bp_longjmp:
186c406b 4366 case bp_exception:
628fe4e4 4367 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4368 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4369 break;
4370 case bp_longjmp_resume:
186c406b 4371 case bp_exception_resume:
628fe4e4 4372 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4373 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4374 break;
4375 case bp_step_resume:
4376 if (bs->stop)
628fe4e4
JK
4377 this_action = BPSTAT_WHAT_STEP_RESUME;
4378 else
c906108c 4379 {
628fe4e4
JK
4380 /* It is for the wrong frame. */
4381 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4382 }
c906108c 4383 break;
2c03e5be
PA
4384 case bp_hp_step_resume:
4385 if (bs->stop)
4386 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4387 else
4388 {
4389 /* It is for the wrong frame. */
4390 this_action = BPSTAT_WHAT_SINGLE;
4391 }
4392 break;
c906108c 4393 case bp_watchpoint_scope:
c4093a6a 4394 case bp_thread_event:
1900040c 4395 case bp_overlay_event:
0fd8e87f 4396 case bp_longjmp_master:
aa7d318d 4397 case bp_std_terminate_master:
186c406b 4398 case bp_exception_master:
628fe4e4 4399 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4400 break;
ce78b96d 4401 case bp_catchpoint:
c5aa993b
JM
4402 if (bs->stop)
4403 {
4404 if (bs->print)
628fe4e4 4405 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4406 else
628fe4e4 4407 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4408 }
4409 else
628fe4e4
JK
4410 {
4411 /* There was a catchpoint, but we're not stopping.
4412 This requires no further action. */
4413 }
4414 break;
4415 case bp_shlib_event:
4416 shlib_event = 1;
4417
4418 /* If requested, stop when the dynamic linker notifies GDB
4419 of events. This allows the user to get control and place
4420 breakpoints in initializer routines for dynamically
4421 loaded objects (among other things). */
4422 if (stop_on_solib_events)
4423 this_action = BPSTAT_WHAT_STOP_NOISY;
4424 else
4425 this_action = BPSTAT_WHAT_SINGLE;
4426 break;
4427 case bp_jit_event:
4428 jit_event = 1;
4429 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4430 break;
c906108c 4431 case bp_call_dummy:
53a5351d
JM
4432 /* Make sure the action is stop (silent or noisy),
4433 so infrun.c pops the dummy frame. */
aa7d318d 4434 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4435 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4436 break;
4437 case bp_std_terminate:
4438 /* Make sure the action is stop (silent or noisy),
4439 so infrun.c pops the dummy frame. */
aa7d318d 4440 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4441 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4442 break;
1042e4c0 4443 case bp_tracepoint:
7a697b8d 4444 case bp_fast_tracepoint:
0fb4aa4b 4445 case bp_static_tracepoint:
1042e4c0
SS
4446 /* Tracepoint hits should not be reported back to GDB, and
4447 if one got through somehow, it should have been filtered
4448 out already. */
4449 internal_error (__FILE__, __LINE__,
7a697b8d 4450 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4451 break;
4452 case bp_gnu_ifunc_resolver:
4453 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4454 this_action = BPSTAT_WHAT_SINGLE;
4455 break;
4456 case bp_gnu_ifunc_resolver_return:
4457 /* The breakpoint will be removed, execution will restart from the
4458 PC of the former breakpoint. */
4459 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4460 break;
628fe4e4
JK
4461 default:
4462 internal_error (__FILE__, __LINE__,
4463 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4464 }
628fe4e4
JK
4465
4466 retval.main_action = max (retval.main_action, this_action);
c906108c 4467 }
628fe4e4 4468
0e30163f
JK
4469 /* These operations may affect the bs->breakpoint_at state so they are
4470 delayed after MAIN_ACTION is decided above. */
4471
628fe4e4
JK
4472 if (shlib_event)
4473 {
4474 if (debug_infrun)
4475 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4476
4477 /* Check for any newly added shared libraries if we're supposed
4478 to be adding them automatically. */
4479
4480 /* Switch terminal for any messages produced by
4481 breakpoint_re_set. */
4482 target_terminal_ours_for_output ();
4483
4484#ifdef SOLIB_ADD
4485 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4486#else
4487 solib_add (NULL, 0, &current_target, auto_solib_add);
4488#endif
4489
4490 target_terminal_inferior ();
4491 }
4492
4493 if (jit_event)
4494 {
4495 if (debug_infrun)
4496 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4497
4498 handle_jit_event ();
4499 }
4500
0e30163f
JK
4501 for (bs = bs_head; bs != NULL; bs = bs->next)
4502 {
4503 struct breakpoint *b = bs->breakpoint_at;
4504
4505 if (b == NULL)
4506 continue;
4507 switch (b->type)
4508 {
4509 case bp_gnu_ifunc_resolver:
4510 gnu_ifunc_resolver_stop (b);
4511 break;
4512 case bp_gnu_ifunc_resolver_return:
4513 gnu_ifunc_resolver_return_stop (b);
4514 break;
4515 }
4516 }
4517
c906108c
SS
4518 return retval;
4519}
4520
4521/* Nonzero if we should step constantly (e.g. watchpoints on machines
4522 without hardware support). This isn't related to a specific bpstat,
4523 just to things like whether watchpoints are set. */
4524
c5aa993b 4525int
fba45db2 4526bpstat_should_step (void)
c906108c
SS
4527{
4528 struct breakpoint *b;
cc59ec59 4529
c906108c 4530 ALL_BREAKPOINTS (b)
717a8278 4531 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4532 return 1;
c906108c
SS
4533 return 0;
4534}
4535
67822962
PA
4536int
4537bpstat_causes_stop (bpstat bs)
4538{
4539 for (; bs != NULL; bs = bs->next)
4540 if (bs->stop)
4541 return 1;
4542
4543 return 0;
4544}
4545
c906108c 4546\f
c5aa993b 4547
170b53b2
UW
4548/* Compute a string of spaces suitable to indent the next line
4549 so it starts at the position corresponding to the table column
4550 named COL_NAME in the currently active table of UIOUT. */
4551
4552static char *
4553wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4554{
4555 static char wrap_indent[80];
4556 int i, total_width, width, align;
4557 char *text;
4558
4559 total_width = 0;
4560 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4561 {
4562 if (strcmp (text, col_name) == 0)
4563 {
4564 gdb_assert (total_width < sizeof wrap_indent);
4565 memset (wrap_indent, ' ', total_width);
4566 wrap_indent[total_width] = 0;
4567
4568 return wrap_indent;
4569 }
4570
4571 total_width += width + 1;
4572 }
4573
4574 return NULL;
4575}
4576
859825b8
JK
4577/* Print the LOC location out of the list of B->LOC locations. */
4578
170b53b2
UW
4579static void
4580print_breakpoint_location (struct breakpoint *b,
4581 struct bp_location *loc)
0d381245 4582{
79a45e25 4583 struct ui_out *uiout = current_uiout;
6c95b8df
PA
4584 struct cleanup *old_chain = save_current_program_space ();
4585
859825b8
JK
4586 if (loc != NULL && loc->shlib_disabled)
4587 loc = NULL;
4588
6c95b8df
PA
4589 if (loc != NULL)
4590 set_current_program_space (loc->pspace);
4591
56435ebe
TT
4592 if (b->display_canonical)
4593 ui_out_field_string (uiout, "what", b->addr_string);
f8eba3c6 4594 else if (loc && loc->source_file)
0d381245
VP
4595 {
4596 struct symbol *sym
4597 = find_pc_sect_function (loc->address, loc->section);
4598 if (sym)
4599 {
4600 ui_out_text (uiout, "in ");
4601 ui_out_field_string (uiout, "func",
4602 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4603 ui_out_text (uiout, " ");
4604 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4605 ui_out_text (uiout, "at ");
0d381245 4606 }
f8eba3c6 4607 ui_out_field_string (uiout, "file", loc->source_file);
0d381245
VP
4608 ui_out_text (uiout, ":");
4609
4610 if (ui_out_is_mi_like_p (uiout))
4611 {
4612 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4613 char *fullname = symtab_to_fullname (sal.symtab);
4614
4615 if (fullname)
4616 ui_out_field_string (uiout, "fullname", fullname);
4617 }
4618
f8eba3c6 4619 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 4620 }
859825b8 4621 else if (loc)
0d381245 4622 {
170b53b2
UW
4623 struct ui_stream *stb = ui_out_stream_new (uiout);
4624 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4625
22e722e1
DJ
4626 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4627 demangle, "");
0d381245 4628 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4629
4630 do_cleanups (stb_chain);
0d381245 4631 }
859825b8
JK
4632 else
4633 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4634
4635 do_cleanups (old_chain);
0d381245
VP
4636}
4637
269b11a2
PA
4638static const char *
4639bptype_string (enum bptype type)
c906108c 4640{
c4093a6a
JM
4641 struct ep_type_description
4642 {
4643 enum bptype type;
4644 char *description;
4645 };
4646 static struct ep_type_description bptypes[] =
c906108c 4647 {
c5aa993b
JM
4648 {bp_none, "?deleted?"},
4649 {bp_breakpoint, "breakpoint"},
c906108c 4650 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4651 {bp_until, "until"},
4652 {bp_finish, "finish"},
4653 {bp_watchpoint, "watchpoint"},
c906108c 4654 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4655 {bp_read_watchpoint, "read watchpoint"},
4656 {bp_access_watchpoint, "acc watchpoint"},
4657 {bp_longjmp, "longjmp"},
4658 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4659 {bp_exception, "exception"},
4660 {bp_exception_resume, "exception resume"},
c5aa993b 4661 {bp_step_resume, "step resume"},
2c03e5be 4662 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
4663 {bp_watchpoint_scope, "watchpoint scope"},
4664 {bp_call_dummy, "call dummy"},
aa7d318d 4665 {bp_std_terminate, "std::terminate"},
c5aa993b 4666 {bp_shlib_event, "shlib events"},
c4093a6a 4667 {bp_thread_event, "thread events"},
1900040c 4668 {bp_overlay_event, "overlay events"},
0fd8e87f 4669 {bp_longjmp_master, "longjmp master"},
aa7d318d 4670 {bp_std_terminate_master, "std::terminate master"},
186c406b 4671 {bp_exception_master, "exception master"},
ce78b96d 4672 {bp_catchpoint, "catchpoint"},
1042e4c0 4673 {bp_tracepoint, "tracepoint"},
7a697b8d 4674 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4675 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4676 {bp_jit_event, "jit events"},
0e30163f
JK
4677 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4678 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4679 };
269b11a2
PA
4680
4681 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4682 || ((int) type != bptypes[(int) type].type))
4683 internal_error (__FILE__, __LINE__,
4684 _("bptypes table does not describe type #%d."),
4685 (int) type);
4686
4687 return bptypes[(int) type].description;
4688}
4689
4690/* Print B to gdb_stdout. */
4691
4692static void
4693print_one_breakpoint_location (struct breakpoint *b,
4694 struct bp_location *loc,
4695 int loc_number,
4696 struct bp_location **last_loc,
269b11a2
PA
4697 int allflag)
4698{
4699 struct command_line *l;
c2c6d25f 4700 static char bpenables[] = "nynny";
c906108c 4701
79a45e25 4702 struct ui_out *uiout = current_uiout;
0d381245
VP
4703 int header_of_multiple = 0;
4704 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4705 struct value_print_options opts;
4706
4707 get_user_print_options (&opts);
0d381245
VP
4708
4709 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4710 /* See comment in print_one_breakpoint concerning treatment of
4711 breakpoints with single disabled location. */
0d381245
VP
4712 if (loc == NULL
4713 && (b->loc != NULL
4714 && (b->loc->next != NULL || !b->loc->enabled)))
4715 header_of_multiple = 1;
4716 if (loc == NULL)
4717 loc = b->loc;
4718
c4093a6a
JM
4719 annotate_record ();
4720
4721 /* 1 */
4722 annotate_field (0);
0d381245
VP
4723 if (part_of_multiple)
4724 {
4725 char *formatted;
0c6773c1 4726 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4727 ui_out_field_string (uiout, "number", formatted);
4728 xfree (formatted);
4729 }
4730 else
4731 {
4732 ui_out_field_int (uiout, "number", b->number);
4733 }
c4093a6a
JM
4734
4735 /* 2 */
4736 annotate_field (1);
0d381245
VP
4737 if (part_of_multiple)
4738 ui_out_field_skip (uiout, "type");
269b11a2
PA
4739 else
4740 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4741
4742 /* 3 */
4743 annotate_field (2);
0d381245
VP
4744 if (part_of_multiple)
4745 ui_out_field_skip (uiout, "disp");
4746 else
2cec12e5 4747 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4748
c4093a6a
JM
4749
4750 /* 4 */
4751 annotate_field (3);
0d381245 4752 if (part_of_multiple)
54e52265 4753 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4754 else
4a64f543
MS
4755 ui_out_field_fmt (uiout, "enabled", "%c",
4756 bpenables[(int) b->enable_state]);
54e52265 4757 ui_out_spaces (uiout, 2);
0d381245 4758
c4093a6a
JM
4759
4760 /* 5 and 6 */
3086aeae 4761 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4762 {
4a64f543
MS
4763 /* Although the print_one can possibly print all locations,
4764 calling it here is not likely to get any nice result. So,
4765 make sure there's just one location. */
0d381245 4766 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4767 b->ops->print_one (b, last_loc);
0d381245 4768 }
3086aeae
DJ
4769 else
4770 switch (b->type)
4771 {
4772 case bp_none:
4773 internal_error (__FILE__, __LINE__,
e2e0b3e5 4774 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4775 break;
c906108c 4776
3086aeae
DJ
4777 case bp_watchpoint:
4778 case bp_hardware_watchpoint:
4779 case bp_read_watchpoint:
4780 case bp_access_watchpoint:
3a5c3e22
PA
4781 {
4782 struct watchpoint *w = (struct watchpoint *) b;
4783
4784 /* Field 4, the address, is omitted (which makes the columns
4785 not line up too nicely with the headers, but the effect
4786 is relatively readable). */
4787 if (opts.addressprint)
4788 ui_out_field_skip (uiout, "addr");
4789 annotate_field (5);
4790 ui_out_field_string (uiout, "what", w->exp_string);
4791 }
3086aeae
DJ
4792 break;
4793
3086aeae
DJ
4794 case bp_breakpoint:
4795 case bp_hardware_breakpoint:
4796 case bp_until:
4797 case bp_finish:
4798 case bp_longjmp:
4799 case bp_longjmp_resume:
186c406b
TT
4800 case bp_exception:
4801 case bp_exception_resume:
3086aeae 4802 case bp_step_resume:
2c03e5be 4803 case bp_hp_step_resume:
3086aeae
DJ
4804 case bp_watchpoint_scope:
4805 case bp_call_dummy:
aa7d318d 4806 case bp_std_terminate:
3086aeae
DJ
4807 case bp_shlib_event:
4808 case bp_thread_event:
4809 case bp_overlay_event:
0fd8e87f 4810 case bp_longjmp_master:
aa7d318d 4811 case bp_std_terminate_master:
186c406b 4812 case bp_exception_master:
1042e4c0 4813 case bp_tracepoint:
7a697b8d 4814 case bp_fast_tracepoint:
0fb4aa4b 4815 case bp_static_tracepoint:
4efc6507 4816 case bp_jit_event:
0e30163f
JK
4817 case bp_gnu_ifunc_resolver:
4818 case bp_gnu_ifunc_resolver_return:
79a45b7d 4819 if (opts.addressprint)
3086aeae
DJ
4820 {
4821 annotate_field (4);
54e52265 4822 if (header_of_multiple)
0d381245 4823 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4824 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4825 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4826 else
5af949e3
UW
4827 ui_out_field_core_addr (uiout, "addr",
4828 loc->gdbarch, loc->address);
3086aeae
DJ
4829 }
4830 annotate_field (5);
0d381245 4831 if (!header_of_multiple)
170b53b2 4832 print_breakpoint_location (b, loc);
0d381245 4833 if (b->loc)
a6d9a66e 4834 *last_loc = b->loc;
3086aeae
DJ
4835 break;
4836 }
c906108c 4837
6c95b8df
PA
4838
4839 /* For backward compatibility, don't display inferiors unless there
4840 are several. */
4841 if (loc != NULL
4842 && !header_of_multiple
4843 && (allflag
4844 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4845 && (number_of_program_spaces () > 1
4846 || number_of_inferiors () > 1)
4a64f543
MS
4847 /* LOC is for existing B, it cannot be in
4848 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4849 && loc->owner->type != bp_catchpoint)))
4850 {
4851 struct inferior *inf;
4852 int first = 1;
4853
4854 for (inf = inferior_list; inf != NULL; inf = inf->next)
4855 {
4856 if (inf->pspace == loc->pspace)
4857 {
4858 if (first)
4859 {
4860 first = 0;
4861 ui_out_text (uiout, " inf ");
4862 }
4863 else
4864 ui_out_text (uiout, ", ");
4865 ui_out_text (uiout, plongest (inf->num));
4866 }
4867 }
4868 }
4869
4a306c9a 4870 if (!part_of_multiple)
c4093a6a 4871 {
4a306c9a
JB
4872 if (b->thread != -1)
4873 {
4874 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4875 "stop only in" line a little further down. */
4a306c9a
JB
4876 ui_out_text (uiout, " thread ");
4877 ui_out_field_int (uiout, "thread", b->thread);
4878 }
4879 else if (b->task != 0)
4880 {
4881 ui_out_text (uiout, " task ");
4882 ui_out_field_int (uiout, "task", b->task);
4883 }
c4093a6a 4884 }
f1310107 4885
8b93c638 4886 ui_out_text (uiout, "\n");
f1310107 4887
348d480f 4888 if (!part_of_multiple)
f1310107
TJB
4889 b->ops->print_one_detail (b, uiout);
4890
0d381245 4891 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4892 {
4893 annotate_field (6);
8b93c638 4894 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 4895 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 4896 the frame ID. */
5af949e3
UW
4897 ui_out_field_core_addr (uiout, "frame",
4898 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4899 ui_out_text (uiout, "\n");
c4093a6a
JM
4900 }
4901
28010a5d 4902 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
4903 {
4904 annotate_field (7);
d77f58be 4905 if (is_tracepoint (b))
1042e4c0
SS
4906 ui_out_text (uiout, "\ttrace only if ");
4907 else
4908 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4909 ui_out_field_string (uiout, "cond", b->cond_string);
4910 ui_out_text (uiout, "\n");
4911 }
4912
0d381245 4913 if (!part_of_multiple && b->thread != -1)
c4093a6a 4914 {
4a64f543 4915 /* FIXME should make an annotation for this. */
8b93c638
JM
4916 ui_out_text (uiout, "\tstop only in thread ");
4917 ui_out_field_int (uiout, "thread", b->thread);
4918 ui_out_text (uiout, "\n");
c4093a6a
JM
4919 }
4920
63c715c6 4921 if (!part_of_multiple && b->hit_count)
c4093a6a 4922 {
4a64f543 4923 /* FIXME should make an annotation for this. */
8b93c638
JM
4924 if (ep_is_catchpoint (b))
4925 ui_out_text (uiout, "\tcatchpoint");
f196051f
SS
4926 else if (is_tracepoint (b))
4927 ui_out_text (uiout, "\ttracepoint");
8b93c638
JM
4928 else
4929 ui_out_text (uiout, "\tbreakpoint");
4930 ui_out_text (uiout, " already hit ");
4931 ui_out_field_int (uiout, "times", b->hit_count);
4932 if (b->hit_count == 1)
4933 ui_out_text (uiout, " time\n");
4934 else
4935 ui_out_text (uiout, " times\n");
c4093a6a
JM
4936 }
4937
4a64f543
MS
4938 /* Output the count also if it is zero, but only if this is mi.
4939 FIXME: Should have a better test for this. */
9dc5e2a9 4940 if (ui_out_is_mi_like_p (uiout))
63c715c6 4941 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4942 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4943
0d381245 4944 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4945 {
4946 annotate_field (8);
8b93c638
JM
4947 ui_out_text (uiout, "\tignore next ");
4948 ui_out_field_int (uiout, "ignore", b->ignore_count);
4949 ui_out_text (uiout, " hits\n");
c4093a6a 4950 }
059fb39f 4951
f196051f
SS
4952 if (!part_of_multiple && is_tracepoint (b))
4953 {
4954 struct tracepoint *tp = (struct tracepoint *) b;
4955
4956 if (tp->traceframe_usage)
4957 {
4958 ui_out_text (uiout, "\ttrace buffer usage ");
4959 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
4960 ui_out_text (uiout, " bytes\n");
4961 }
4962 }
4963
9add0f1b 4964 l = b->commands ? b->commands->commands : NULL;
059fb39f 4965 if (!part_of_multiple && l)
c4093a6a 4966 {
3b31d625
EZ
4967 struct cleanup *script_chain;
4968
c4093a6a 4969 annotate_field (9);
3b31d625 4970 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4971 print_command_lines (uiout, l, 4);
3b31d625 4972 do_cleanups (script_chain);
c4093a6a 4973 }
d24317b4 4974
d9b3f62e 4975 if (is_tracepoint (b))
1042e4c0 4976 {
d9b3f62e
PA
4977 struct tracepoint *t = (struct tracepoint *) b;
4978
4979 if (!part_of_multiple && t->pass_count)
4980 {
4981 annotate_field (10);
4982 ui_out_text (uiout, "\tpass count ");
4983 ui_out_field_int (uiout, "pass", t->pass_count);
4984 ui_out_text (uiout, " \n");
4985 }
1042e4c0
SS
4986 }
4987
d24317b4
VP
4988 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4989 {
3a5c3e22
PA
4990 if (is_watchpoint (b))
4991 {
4992 struct watchpoint *w = (struct watchpoint *) b;
4993
4994 ui_out_field_string (uiout, "original-location", w->exp_string);
4995 }
4996 else if (b->addr_string)
d24317b4 4997 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 4998 }
c4093a6a 4999}
c5aa993b 5000
0d381245
VP
5001static void
5002print_one_breakpoint (struct breakpoint *b,
4a64f543 5003 struct bp_location **last_loc,
6c95b8df 5004 int allflag)
0d381245 5005{
8d3788bd 5006 struct cleanup *bkpt_chain;
79a45e25 5007 struct ui_out *uiout = current_uiout;
8d3788bd
VP
5008
5009 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5010
12c5a436 5011 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 5012 do_cleanups (bkpt_chain);
0d381245
VP
5013
5014 /* If this breakpoint has custom print function,
5015 it's already printed. Otherwise, print individual
5016 locations, if any. */
5017 if (b->ops == NULL || b->ops->print_one == NULL)
5018 {
4a64f543
MS
5019 /* If breakpoint has a single location that is disabled, we
5020 print it as if it had several locations, since otherwise it's
5021 hard to represent "breakpoint enabled, location disabled"
5022 situation.
5023
5024 Note that while hardware watchpoints have several locations
a3be7890 5025 internally, that's not a property exposed to user. */
0d381245 5026 if (b->loc
a5606eee 5027 && !is_hardware_watchpoint (b)
8d3788bd 5028 && (b->loc->next || !b->loc->enabled))
0d381245
VP
5029 {
5030 struct bp_location *loc;
5031 int n = 1;
8d3788bd 5032
0d381245 5033 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5034 {
5035 struct cleanup *inner2 =
5036 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5037 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5038 do_cleanups (inner2);
5039 }
0d381245
VP
5040 }
5041 }
5042}
5043
a6d9a66e
UW
5044static int
5045breakpoint_address_bits (struct breakpoint *b)
5046{
5047 int print_address_bits = 0;
5048 struct bp_location *loc;
5049
5050 for (loc = b->loc; loc; loc = loc->next)
5051 {
c7437ca6
PA
5052 int addr_bit;
5053
5054 /* Software watchpoints that aren't watching memory don't have
5055 an address to print. */
5056 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5057 continue;
5058
5059 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5060 if (addr_bit > print_address_bits)
5061 print_address_bits = addr_bit;
5062 }
5063
5064 return print_address_bits;
5065}
0d381245 5066
c4093a6a
JM
5067struct captured_breakpoint_query_args
5068 {
5069 int bnum;
5070 };
c5aa993b 5071
c4093a6a 5072static int
2b65245e 5073do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5074{
5075 struct captured_breakpoint_query_args *args = data;
52f0bd74 5076 struct breakpoint *b;
a6d9a66e 5077 struct bp_location *dummy_loc = NULL;
cc59ec59 5078
c4093a6a
JM
5079 ALL_BREAKPOINTS (b)
5080 {
5081 if (args->bnum == b->number)
c5aa993b 5082 {
12c5a436 5083 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5084 return GDB_RC_OK;
c5aa993b 5085 }
c4093a6a
JM
5086 }
5087 return GDB_RC_NONE;
5088}
c5aa993b 5089
c4093a6a 5090enum gdb_rc
4a64f543
MS
5091gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5092 char **error_message)
c4093a6a
JM
5093{
5094 struct captured_breakpoint_query_args args;
cc59ec59 5095
c4093a6a
JM
5096 args.bnum = bnum;
5097 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5098 an error. */
b0b13bb4
DJ
5099 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5100 error_message, RETURN_MASK_ALL) < 0)
5101 return GDB_RC_FAIL;
5102 else
5103 return GDB_RC_OK;
c4093a6a 5104}
c5aa993b 5105
09d682a4
TT
5106/* Return true if this breakpoint was set by the user, false if it is
5107 internal or momentary. */
5108
5109int
5110user_breakpoint_p (struct breakpoint *b)
5111{
46c6471b 5112 return b->number > 0;
09d682a4
TT
5113}
5114
7f3b0473 5115/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5116 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5117 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5118 FILTER is non-NULL, call it on each breakpoint and only include the
5119 ones for which it returns non-zero. Return the total number of
5120 breakpoints listed. */
c906108c 5121
d77f58be 5122static int
e5a67952 5123breakpoint_1 (char *args, int allflag,
4a64f543 5124 int (*filter) (const struct breakpoint *))
c4093a6a 5125{
52f0bd74 5126 struct breakpoint *b;
a6d9a66e 5127 struct bp_location *last_loc = NULL;
7f3b0473 5128 int nr_printable_breakpoints;
3b31d625 5129 struct cleanup *bkpttbl_chain;
79a45b7d 5130 struct value_print_options opts;
a6d9a66e 5131 int print_address_bits = 0;
269b11a2 5132 int print_type_col_width = 14;
79a45e25 5133 struct ui_out *uiout = current_uiout;
269b11a2 5134
79a45b7d
TT
5135 get_user_print_options (&opts);
5136
4a64f543
MS
5137 /* Compute the number of rows in the table, as well as the size
5138 required for address fields. */
7f3b0473
AC
5139 nr_printable_breakpoints = 0;
5140 ALL_BREAKPOINTS (b)
e5a67952
MS
5141 {
5142 /* If we have a filter, only list the breakpoints it accepts. */
5143 if (filter && !filter (b))
5144 continue;
5145
5146 /* If we have an "args" string, it is a list of breakpoints to
5147 accept. Skip the others. */
5148 if (args != NULL && *args != '\0')
5149 {
5150 if (allflag && parse_and_eval_long (args) != b->number)
5151 continue;
5152 if (!allflag && !number_is_in_list (args, b->number))
5153 continue;
5154 }
269b11a2 5155
e5a67952
MS
5156 if (allflag || user_breakpoint_p (b))
5157 {
5158 int addr_bit, type_len;
a6d9a66e 5159
e5a67952
MS
5160 addr_bit = breakpoint_address_bits (b);
5161 if (addr_bit > print_address_bits)
5162 print_address_bits = addr_bit;
269b11a2 5163
e5a67952
MS
5164 type_len = strlen (bptype_string (b->type));
5165 if (type_len > print_type_col_width)
5166 print_type_col_width = type_len;
5167
5168 nr_printable_breakpoints++;
5169 }
5170 }
7f3b0473 5171
79a45b7d 5172 if (opts.addressprint)
3b31d625 5173 bkpttbl_chain
3e43a32a
MS
5174 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5175 nr_printable_breakpoints,
3b31d625 5176 "BreakpointTable");
8b93c638 5177 else
3b31d625 5178 bkpttbl_chain
3e43a32a
MS
5179 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5180 nr_printable_breakpoints,
3b31d625 5181 "BreakpointTable");
8b93c638 5182
7f3b0473 5183 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5184 annotate_breakpoints_headers ();
5185 if (nr_printable_breakpoints > 0)
5186 annotate_field (0);
4a64f543 5187 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5188 if (nr_printable_breakpoints > 0)
5189 annotate_field (1);
269b11a2 5190 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5191 "type", "Type"); /* 2 */
d7faa9e7
AC
5192 if (nr_printable_breakpoints > 0)
5193 annotate_field (2);
4a64f543 5194 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5195 if (nr_printable_breakpoints > 0)
5196 annotate_field (3);
54e52265 5197 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5198 if (opts.addressprint)
e5a67952
MS
5199 {
5200 if (nr_printable_breakpoints > 0)
5201 annotate_field (4);
5202 if (print_address_bits <= 32)
5203 ui_out_table_header (uiout, 10, ui_left,
5204 "addr", "Address"); /* 5 */
5205 else
5206 ui_out_table_header (uiout, 18, ui_left,
5207 "addr", "Address"); /* 5 */
5208 }
d7faa9e7
AC
5209 if (nr_printable_breakpoints > 0)
5210 annotate_field (5);
5211 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5212 ui_out_table_body (uiout);
5213 if (nr_printable_breakpoints > 0)
5214 annotate_breakpoints_table ();
7f3b0473 5215
c4093a6a 5216 ALL_BREAKPOINTS (b)
e5a67952
MS
5217 {
5218 QUIT;
5219 /* If we have a filter, only list the breakpoints it accepts. */
5220 if (filter && !filter (b))
5221 continue;
5222
5223 /* If we have an "args" string, it is a list of breakpoints to
5224 accept. Skip the others. */
5225
5226 if (args != NULL && *args != '\0')
5227 {
5228 if (allflag) /* maintenance info breakpoint */
5229 {
5230 if (parse_and_eval_long (args) != b->number)
5231 continue;
5232 }
5233 else /* all others */
5234 {
5235 if (!number_is_in_list (args, b->number))
5236 continue;
5237 }
5238 }
5239 /* We only print out user settable breakpoints unless the
5240 allflag is set. */
5241 if (allflag || user_breakpoint_p (b))
12c5a436 5242 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5243 }
5244
3b31d625 5245 do_cleanups (bkpttbl_chain);
698384cd 5246
7f3b0473 5247 if (nr_printable_breakpoints == 0)
c906108c 5248 {
4a64f543
MS
5249 /* If there's a filter, let the caller decide how to report
5250 empty list. */
d77f58be
SS
5251 if (!filter)
5252 {
e5a67952 5253 if (args == NULL || *args == '\0')
d77f58be
SS
5254 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5255 else
4a64f543 5256 ui_out_message (uiout, 0,
e5a67952
MS
5257 "No breakpoint or watchpoint matching '%s'.\n",
5258 args);
d77f58be 5259 }
c906108c
SS
5260 }
5261 else
c4093a6a 5262 {
a6d9a66e
UW
5263 if (last_loc && !server_command)
5264 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5265 }
c906108c 5266
4a64f543 5267 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5268 there have been breakpoints? */
c906108c 5269 annotate_breakpoints_table_end ();
d77f58be
SS
5270
5271 return nr_printable_breakpoints;
c906108c
SS
5272}
5273
ad443146
SS
5274/* Display the value of default-collect in a way that is generally
5275 compatible with the breakpoint list. */
5276
5277static void
5278default_collect_info (void)
5279{
79a45e25
PA
5280 struct ui_out *uiout = current_uiout;
5281
ad443146
SS
5282 /* If it has no value (which is frequently the case), say nothing; a
5283 message like "No default-collect." gets in user's face when it's
5284 not wanted. */
5285 if (!*default_collect)
5286 return;
5287
5288 /* The following phrase lines up nicely with per-tracepoint collect
5289 actions. */
5290 ui_out_text (uiout, "default collect ");
5291 ui_out_field_string (uiout, "default-collect", default_collect);
5292 ui_out_text (uiout, " \n");
5293}
5294
c906108c 5295static void
e5a67952 5296breakpoints_info (char *args, int from_tty)
c906108c 5297{
e5a67952 5298 breakpoint_1 (args, 0, NULL);
ad443146
SS
5299
5300 default_collect_info ();
d77f58be
SS
5301}
5302
5303static void
e5a67952 5304watchpoints_info (char *args, int from_tty)
d77f58be 5305{
e5a67952 5306 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 5307 struct ui_out *uiout = current_uiout;
d77f58be
SS
5308
5309 if (num_printed == 0)
5310 {
e5a67952 5311 if (args == NULL || *args == '\0')
d77f58be
SS
5312 ui_out_message (uiout, 0, "No watchpoints.\n");
5313 else
e5a67952 5314 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5315 }
c906108c
SS
5316}
5317
7a292a7a 5318static void
e5a67952 5319maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5320{
e5a67952 5321 breakpoint_1 (args, 1, NULL);
ad443146
SS
5322
5323 default_collect_info ();
c906108c
SS
5324}
5325
0d381245 5326static int
714835d5 5327breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5328 struct program_space *pspace,
714835d5 5329 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5330{
5331 struct bp_location *bl = b->loc;
cc59ec59 5332
0d381245
VP
5333 for (; bl; bl = bl->next)
5334 {
6c95b8df
PA
5335 if (bl->pspace == pspace
5336 && bl->address == pc
0d381245
VP
5337 && (!overlay_debugging || bl->section == section))
5338 return 1;
5339 }
5340 return 0;
5341}
5342
672f9b60 5343/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
5344 concerns with logical breakpoints, so we match program spaces, not
5345 address spaces. */
c906108c
SS
5346
5347static void
6c95b8df
PA
5348describe_other_breakpoints (struct gdbarch *gdbarch,
5349 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5350 struct obj_section *section, int thread)
c906108c 5351{
52f0bd74
AC
5352 int others = 0;
5353 struct breakpoint *b;
c906108c
SS
5354
5355 ALL_BREAKPOINTS (b)
672f9b60
KP
5356 others += (user_breakpoint_p (b)
5357 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
5358 if (others > 0)
5359 {
a3f17187
AC
5360 if (others == 1)
5361 printf_filtered (_("Note: breakpoint "));
5362 else /* if (others == ???) */
5363 printf_filtered (_("Note: breakpoints "));
c906108c 5364 ALL_BREAKPOINTS (b)
672f9b60 5365 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5366 {
5367 others--;
5368 printf_filtered ("%d", b->number);
5369 if (b->thread == -1 && thread != -1)
5370 printf_filtered (" (all threads)");
5371 else if (b->thread != -1)
5372 printf_filtered (" (thread %d)", b->thread);
5373 printf_filtered ("%s%s ",
059fb39f 5374 ((b->enable_state == bp_disabled
f8eba3c6 5375 || b->enable_state == bp_call_disabled)
0d381245
VP
5376 ? " (disabled)"
5377 : b->enable_state == bp_permanent
5378 ? " (permanent)"
5379 : ""),
5380 (others > 1) ? ","
5381 : ((others == 1) ? " and" : ""));
5382 }
a3f17187 5383 printf_filtered (_("also set at pc "));
5af949e3 5384 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5385 printf_filtered (".\n");
5386 }
5387}
5388\f
c906108c 5389
e4f237da
KB
5390/* Return true iff it is meaningful to use the address member of
5391 BPT. For some breakpoint types, the address member is irrelevant
5392 and it makes no sense to attempt to compare it to other addresses
5393 (or use it for any other purpose either).
5394
4a64f543
MS
5395 More specifically, each of the following breakpoint types will
5396 always have a zero valued address and we don't want to mark
5397 breakpoints of any of these types to be a duplicate of an actual
5398 breakpoint at address zero:
e4f237da
KB
5399
5400 bp_watchpoint
2d134ed3
PA
5401 bp_catchpoint
5402
5403*/
e4f237da
KB
5404
5405static int
5406breakpoint_address_is_meaningful (struct breakpoint *bpt)
5407{
5408 enum bptype type = bpt->type;
5409
2d134ed3
PA
5410 return (type != bp_watchpoint && type != bp_catchpoint);
5411}
5412
5413/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5414 true if LOC1 and LOC2 represent the same watchpoint location. */
5415
5416static int
4a64f543
MS
5417watchpoint_locations_match (struct bp_location *loc1,
5418 struct bp_location *loc2)
2d134ed3 5419{
3a5c3e22
PA
5420 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
5421 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
5422
5423 /* Both of them must exist. */
5424 gdb_assert (w1 != NULL);
5425 gdb_assert (w2 != NULL);
2bdf28a0 5426
4a64f543
MS
5427 /* If the target can evaluate the condition expression in hardware,
5428 then we we need to insert both watchpoints even if they are at
5429 the same place. Otherwise the watchpoint will only trigger when
5430 the condition of whichever watchpoint was inserted evaluates to
5431 true, not giving a chance for GDB to check the condition of the
5432 other watchpoint. */
3a5c3e22 5433 if ((w1->cond_exp
4a64f543
MS
5434 && target_can_accel_watchpoint_condition (loc1->address,
5435 loc1->length,
0cf6dd15 5436 loc1->watchpoint_type,
3a5c3e22
PA
5437 w1->cond_exp))
5438 || (w2->cond_exp
4a64f543
MS
5439 && target_can_accel_watchpoint_condition (loc2->address,
5440 loc2->length,
0cf6dd15 5441 loc2->watchpoint_type,
3a5c3e22 5442 w2->cond_exp)))
0cf6dd15
TJB
5443 return 0;
5444
85d721b8
PA
5445 /* Note that this checks the owner's type, not the location's. In
5446 case the target does not support read watchpoints, but does
5447 support access watchpoints, we'll have bp_read_watchpoint
5448 watchpoints with hw_access locations. Those should be considered
5449 duplicates of hw_read locations. The hw_read locations will
5450 become hw_access locations later. */
2d134ed3
PA
5451 return (loc1->owner->type == loc2->owner->type
5452 && loc1->pspace->aspace == loc2->pspace->aspace
5453 && loc1->address == loc2->address
5454 && loc1->length == loc2->length);
e4f237da
KB
5455}
5456
6c95b8df
PA
5457/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5458 same breakpoint location. In most targets, this can only be true
5459 if ASPACE1 matches ASPACE2. On targets that have global
5460 breakpoints, the address space doesn't really matter. */
5461
5462static int
5463breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5464 struct address_space *aspace2, CORE_ADDR addr2)
5465{
5466 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5467 || aspace1 == aspace2)
5468 && addr1 == addr2);
5469}
5470
f1310107
TJB
5471/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5472 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5473 matches ASPACE2. On targets that have global breakpoints, the address
5474 space doesn't really matter. */
5475
5476static int
5477breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5478 int len1, struct address_space *aspace2,
5479 CORE_ADDR addr2)
5480{
5481 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5482 || aspace1 == aspace2)
5483 && addr2 >= addr1 && addr2 < addr1 + len1);
5484}
5485
5486/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5487 a ranged breakpoint. In most targets, a match happens only if ASPACE
5488 matches the breakpoint's address space. On targets that have global
5489 breakpoints, the address space doesn't really matter. */
5490
5491static int
5492breakpoint_location_address_match (struct bp_location *bl,
5493 struct address_space *aspace,
5494 CORE_ADDR addr)
5495{
5496 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5497 aspace, addr)
5498 || (bl->length
5499 && breakpoint_address_match_range (bl->pspace->aspace,
5500 bl->address, bl->length,
5501 aspace, addr)));
5502}
5503
1e4d1764
YQ
5504/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
5505 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
5506 true, otherwise returns false. */
5507
5508static int
5509tracepoint_locations_match (struct bp_location *loc1,
5510 struct bp_location *loc2)
5511{
5512 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
5513 /* Since tracepoint locations are never duplicated with others', tracepoint
5514 locations at the same address of different tracepoints are regarded as
5515 different locations. */
5516 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
5517 else
5518 return 0;
5519}
5520
2d134ed3
PA
5521/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5522 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5523 represent the same location. */
5524
5525static int
4a64f543
MS
5526breakpoint_locations_match (struct bp_location *loc1,
5527 struct bp_location *loc2)
2d134ed3 5528{
2bdf28a0
JK
5529 int hw_point1, hw_point2;
5530
5531 /* Both of them must not be in moribund_locations. */
5532 gdb_assert (loc1->owner != NULL);
5533 gdb_assert (loc2->owner != NULL);
5534
5535 hw_point1 = is_hardware_watchpoint (loc1->owner);
5536 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5537
5538 if (hw_point1 != hw_point2)
5539 return 0;
5540 else if (hw_point1)
5541 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
5542 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
5543 return tracepoint_locations_match (loc1, loc2);
2d134ed3 5544 else
f1310107
TJB
5545 /* We compare bp_location.length in order to cover ranged breakpoints. */
5546 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5547 loc2->pspace->aspace, loc2->address)
5548 && loc1->length == loc2->length);
2d134ed3
PA
5549}
5550
76897487
KB
5551static void
5552breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5553 int bnum, int have_bnum)
5554{
f63fbe86
MS
5555 /* The longest string possibly returned by hex_string_custom
5556 is 50 chars. These must be at least that big for safety. */
5557 char astr1[64];
5558 char astr2[64];
76897487 5559
bb599908
PH
5560 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5561 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5562 if (have_bnum)
8a3fe4f8 5563 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5564 bnum, astr1, astr2);
5565 else
8a3fe4f8 5566 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5567}
5568
4a64f543
MS
5569/* Adjust a breakpoint's address to account for architectural
5570 constraints on breakpoint placement. Return the adjusted address.
5571 Note: Very few targets require this kind of adjustment. For most
5572 targets, this function is simply the identity function. */
76897487
KB
5573
5574static CORE_ADDR
a6d9a66e
UW
5575adjust_breakpoint_address (struct gdbarch *gdbarch,
5576 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5577{
a6d9a66e 5578 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5579 {
5580 /* Very few targets need any kind of breakpoint adjustment. */
5581 return bpaddr;
5582 }
88f7da05
KB
5583 else if (bptype == bp_watchpoint
5584 || bptype == bp_hardware_watchpoint
5585 || bptype == bp_read_watchpoint
5586 || bptype == bp_access_watchpoint
fe798b75 5587 || bptype == bp_catchpoint)
88f7da05
KB
5588 {
5589 /* Watchpoints and the various bp_catch_* eventpoints should not
5590 have their addresses modified. */
5591 return bpaddr;
5592 }
76897487
KB
5593 else
5594 {
5595 CORE_ADDR adjusted_bpaddr;
5596
5597 /* Some targets have architectural constraints on the placement
5598 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5599 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5600
5601 /* An adjusted breakpoint address can significantly alter
5602 a user's expectations. Print a warning if an adjustment
5603 is required. */
5604 if (adjusted_bpaddr != bpaddr)
5605 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5606
5607 return adjusted_bpaddr;
5608 }
5609}
5610
28010a5d
PA
5611void
5612init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5613 struct breakpoint *owner)
7cc221ef 5614{
7cc221ef
DJ
5615 memset (loc, 0, sizeof (*loc));
5616
348d480f
PA
5617 gdb_assert (ops != NULL);
5618
28010a5d
PA
5619 loc->ops = ops;
5620 loc->owner = owner;
511a6cd4 5621 loc->cond = NULL;
0d381245
VP
5622 loc->shlib_disabled = 0;
5623 loc->enabled = 1;
e049a4b5 5624
28010a5d 5625 switch (owner->type)
e049a4b5
DJ
5626 {
5627 case bp_breakpoint:
5628 case bp_until:
5629 case bp_finish:
5630 case bp_longjmp:
5631 case bp_longjmp_resume:
186c406b
TT
5632 case bp_exception:
5633 case bp_exception_resume:
e049a4b5 5634 case bp_step_resume:
2c03e5be 5635 case bp_hp_step_resume:
e049a4b5
DJ
5636 case bp_watchpoint_scope:
5637 case bp_call_dummy:
aa7d318d 5638 case bp_std_terminate:
e049a4b5
DJ
5639 case bp_shlib_event:
5640 case bp_thread_event:
5641 case bp_overlay_event:
4efc6507 5642 case bp_jit_event:
0fd8e87f 5643 case bp_longjmp_master:
aa7d318d 5644 case bp_std_terminate_master:
186c406b 5645 case bp_exception_master:
0e30163f
JK
5646 case bp_gnu_ifunc_resolver:
5647 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5648 loc->loc_type = bp_loc_software_breakpoint;
5649 break;
5650 case bp_hardware_breakpoint:
5651 loc->loc_type = bp_loc_hardware_breakpoint;
5652 break;
5653 case bp_hardware_watchpoint:
5654 case bp_read_watchpoint:
5655 case bp_access_watchpoint:
5656 loc->loc_type = bp_loc_hardware_watchpoint;
5657 break;
5658 case bp_watchpoint:
ce78b96d 5659 case bp_catchpoint:
15c3d785
PA
5660 case bp_tracepoint:
5661 case bp_fast_tracepoint:
0fb4aa4b 5662 case bp_static_tracepoint:
e049a4b5
DJ
5663 loc->loc_type = bp_loc_other;
5664 break;
5665 default:
e2e0b3e5 5666 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5667 }
5668
f431efe5 5669 loc->refc = 1;
28010a5d
PA
5670}
5671
5672/* Allocate a struct bp_location. */
5673
5674static struct bp_location *
5675allocate_bp_location (struct breakpoint *bpt)
5676{
348d480f
PA
5677 return bpt->ops->allocate_location (bpt);
5678}
7cc221ef 5679
f431efe5
PA
5680static void
5681free_bp_location (struct bp_location *loc)
fe3f5fa8 5682{
348d480f 5683 loc->ops->dtor (loc);
fe3f5fa8
VP
5684 xfree (loc);
5685}
5686
f431efe5
PA
5687/* Increment reference count. */
5688
5689static void
5690incref_bp_location (struct bp_location *bl)
5691{
5692 ++bl->refc;
5693}
5694
5695/* Decrement reference count. If the reference count reaches 0,
5696 destroy the bp_location. Sets *BLP to NULL. */
5697
5698static void
5699decref_bp_location (struct bp_location **blp)
5700{
0807b50c
PA
5701 gdb_assert ((*blp)->refc > 0);
5702
f431efe5
PA
5703 if (--(*blp)->refc == 0)
5704 free_bp_location (*blp);
5705 *blp = NULL;
5706}
5707
346774a9 5708/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 5709
346774a9
PA
5710static void
5711add_to_breakpoint_chain (struct breakpoint *b)
c906108c 5712{
346774a9 5713 struct breakpoint *b1;
c906108c 5714
346774a9
PA
5715 /* Add this breakpoint to the end of the chain so that a list of
5716 breakpoints will come out in order of increasing numbers. */
5717
5718 b1 = breakpoint_chain;
5719 if (b1 == 0)
5720 breakpoint_chain = b;
5721 else
5722 {
5723 while (b1->next)
5724 b1 = b1->next;
5725 b1->next = b;
5726 }
5727}
5728
5729/* Initializes breakpoint B with type BPTYPE and no locations yet. */
5730
5731static void
5732init_raw_breakpoint_without_location (struct breakpoint *b,
5733 struct gdbarch *gdbarch,
28010a5d 5734 enum bptype bptype,
c0a91b2b 5735 const struct breakpoint_ops *ops)
346774a9 5736{
c906108c 5737 memset (b, 0, sizeof (*b));
2219d63c 5738
348d480f
PA
5739 gdb_assert (ops != NULL);
5740
28010a5d 5741 b->ops = ops;
4d28f7a8 5742 b->type = bptype;
a6d9a66e 5743 b->gdbarch = gdbarch;
c906108c
SS
5744 b->language = current_language->la_language;
5745 b->input_radix = input_radix;
5746 b->thread = -1;
b5de0fa7 5747 b->enable_state = bp_enabled;
c906108c
SS
5748 b->next = 0;
5749 b->silent = 0;
5750 b->ignore_count = 0;
5751 b->commands = NULL;
818dd999 5752 b->frame_id = null_frame_id;
0d381245 5753 b->condition_not_parsed = 0;
84f4c1fe 5754 b->py_bp_object = NULL;
d0fb5eae 5755 b->related_breakpoint = b;
346774a9
PA
5756}
5757
5758/* Helper to set_raw_breakpoint below. Creates a breakpoint
5759 that has type BPTYPE and has no locations as yet. */
346774a9
PA
5760
5761static struct breakpoint *
5762set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 5763 enum bptype bptype,
c0a91b2b 5764 const struct breakpoint_ops *ops)
346774a9
PA
5765{
5766 struct breakpoint *b = XNEW (struct breakpoint);
5767
348d480f 5768 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 5769 add_to_breakpoint_chain (b);
0d381245
VP
5770 return b;
5771}
5772
0e30163f
JK
5773/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5774 resolutions should be made as the user specified the location explicitly
5775 enough. */
5776
0d381245 5777static void
0e30163f 5778set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5779{
2bdf28a0
JK
5780 gdb_assert (loc->owner != NULL);
5781
0d381245 5782 if (loc->owner->type == bp_breakpoint
1042e4c0 5783 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5784 || is_tracepoint (loc->owner))
0d381245 5785 {
0e30163f
JK
5786 int is_gnu_ifunc;
5787
5788 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5789 NULL, NULL, &is_gnu_ifunc);
5790
5791 if (is_gnu_ifunc && !explicit_loc)
5792 {
5793 struct breakpoint *b = loc->owner;
5794
5795 gdb_assert (loc->pspace == current_program_space);
5796 if (gnu_ifunc_resolve_name (loc->function_name,
5797 &loc->requested_address))
5798 {
5799 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5800 loc->address = adjust_breakpoint_address (loc->gdbarch,
5801 loc->requested_address,
5802 b->type);
5803 }
5804 else if (b->type == bp_breakpoint && b->loc == loc
5805 && loc->next == NULL && b->related_breakpoint == b)
5806 {
5807 /* Create only the whole new breakpoint of this type but do not
5808 mess more complicated breakpoints with multiple locations. */
5809 b->type = bp_gnu_ifunc_resolver;
5810 }
5811 }
5812
0d381245
VP
5813 if (loc->function_name)
5814 loc->function_name = xstrdup (loc->function_name);
5815 }
5816}
5817
a6d9a66e 5818/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 5819struct gdbarch *
a6d9a66e
UW
5820get_sal_arch (struct symtab_and_line sal)
5821{
5822 if (sal.section)
5823 return get_objfile_arch (sal.section->objfile);
5824 if (sal.symtab)
5825 return get_objfile_arch (sal.symtab->objfile);
5826
5827 return NULL;
5828}
5829
346774a9
PA
5830/* Low level routine for partially initializing a breakpoint of type
5831 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 5832 file name, and line number are provided by SAL.
0d381245
VP
5833
5834 It is expected that the caller will complete the initialization of
5835 the newly created breakpoint struct as well as output any status
c56053d2 5836 information regarding the creation of a new breakpoint. */
0d381245 5837
346774a9
PA
5838static void
5839init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 5840 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5841 const struct breakpoint_ops *ops)
0d381245 5842{
28010a5d 5843 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 5844
3742cc8b 5845 add_location_to_breakpoint (b, &sal);
0d381245 5846
6c95b8df
PA
5847 if (bptype != bp_catchpoint)
5848 gdb_assert (sal.pspace != NULL);
5849
f8eba3c6
TT
5850 /* Store the program space that was used to set the breakpoint,
5851 except for ordinary breakpoints, which are independent of the
5852 program space. */
5853 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
5854 b->pspace = sal.pspace;
0d381245 5855
c906108c 5856 breakpoints_changed ();
346774a9 5857}
c906108c 5858
346774a9
PA
5859/* set_raw_breakpoint is a low level routine for allocating and
5860 partially initializing a breakpoint of type BPTYPE. The newly
5861 created breakpoint's address, section, source file name, and line
5862 number are provided by SAL. The newly created and partially
5863 initialized breakpoint is added to the breakpoint chain and
5864 is also returned as the value of this function.
5865
5866 It is expected that the caller will complete the initialization of
5867 the newly created breakpoint struct as well as output any status
5868 information regarding the creation of a new breakpoint. In
5869 particular, set_raw_breakpoint does NOT set the breakpoint
5870 number! Care should be taken to not allow an error to occur
5871 prior to completing the initialization of the breakpoint. If this
5872 should happen, a bogus breakpoint will be left on the chain. */
5873
5874struct breakpoint *
5875set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 5876 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5877 const struct breakpoint_ops *ops)
346774a9
PA
5878{
5879 struct breakpoint *b = XNEW (struct breakpoint);
5880
348d480f 5881 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 5882 add_to_breakpoint_chain (b);
c906108c
SS
5883 return b;
5884}
5885
c2c6d25f
JM
5886
5887/* Note that the breakpoint object B describes a permanent breakpoint
5888 instruction, hard-wired into the inferior's code. */
5889void
5890make_breakpoint_permanent (struct breakpoint *b)
5891{
0d381245 5892 struct bp_location *bl;
cc59ec59 5893
b5de0fa7 5894 b->enable_state = bp_permanent;
c2c6d25f 5895
4a64f543
MS
5896 /* By definition, permanent breakpoints are already present in the
5897 code. Mark all locations as inserted. For now,
5898 make_breakpoint_permanent is called in just one place, so it's
5899 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 5900 multiple locations or not, but it's easy to implement. */
0d381245
VP
5901 for (bl = b->loc; bl; bl = bl->next)
5902 bl->inserted = 1;
c2c6d25f
JM
5903}
5904
53a5351d 5905/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5906 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5907 initiated the operation. */
c906108c
SS
5908
5909void
186c406b 5910set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5911{
35df4500 5912 struct breakpoint *b, *b_tmp;
186c406b 5913 int thread = tp->num;
0fd8e87f
UW
5914
5915 /* To avoid having to rescan all objfile symbols at every step,
5916 we maintain a list of continually-inserted but always disabled
5917 longjmp "master" breakpoints. Here, we simply create momentary
5918 clones of those and enable them for the requested thread. */
35df4500 5919 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5920 if (b->pspace == current_program_space
186c406b
TT
5921 && (b->type == bp_longjmp_master
5922 || b->type == bp_exception_master))
0fd8e87f 5923 {
06edf0c0
PA
5924 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5925 struct breakpoint *clone;
cc59ec59 5926
06edf0c0
PA
5927 clone = momentary_breakpoint_from_master (b, type,
5928 &momentary_breakpoint_ops);
0fd8e87f
UW
5929 clone->thread = thread;
5930 }
186c406b
TT
5931
5932 tp->initiating_frame = frame;
c906108c
SS
5933}
5934
611c83ae 5935/* Delete all longjmp breakpoints from THREAD. */
c906108c 5936void
611c83ae 5937delete_longjmp_breakpoint (int thread)
c906108c 5938{
35df4500 5939 struct breakpoint *b, *b_tmp;
c906108c 5940
35df4500 5941 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5942 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5943 {
5944 if (b->thread == thread)
5945 delete_breakpoint (b);
5946 }
c906108c
SS
5947}
5948
1900040c
MS
5949void
5950enable_overlay_breakpoints (void)
5951{
52f0bd74 5952 struct breakpoint *b;
1900040c
MS
5953
5954 ALL_BREAKPOINTS (b)
5955 if (b->type == bp_overlay_event)
5956 {
5957 b->enable_state = bp_enabled;
b60e7edf 5958 update_global_location_list (1);
c02f5703 5959 overlay_events_enabled = 1;
1900040c
MS
5960 }
5961}
5962
5963void
5964disable_overlay_breakpoints (void)
5965{
52f0bd74 5966 struct breakpoint *b;
1900040c
MS
5967
5968 ALL_BREAKPOINTS (b)
5969 if (b->type == bp_overlay_event)
5970 {
5971 b->enable_state = bp_disabled;
b60e7edf 5972 update_global_location_list (0);
c02f5703 5973 overlay_events_enabled = 0;
1900040c
MS
5974 }
5975}
5976
aa7d318d
TT
5977/* Set an active std::terminate breakpoint for each std::terminate
5978 master breakpoint. */
5979void
5980set_std_terminate_breakpoint (void)
5981{
35df4500 5982 struct breakpoint *b, *b_tmp;
aa7d318d 5983
35df4500 5984 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5985 if (b->pspace == current_program_space
5986 && b->type == bp_std_terminate_master)
5987 {
06edf0c0
PA
5988 momentary_breakpoint_from_master (b, bp_std_terminate,
5989 &momentary_breakpoint_ops);
aa7d318d
TT
5990 }
5991}
5992
5993/* Delete all the std::terminate breakpoints. */
5994void
5995delete_std_terminate_breakpoint (void)
5996{
35df4500 5997 struct breakpoint *b, *b_tmp;
aa7d318d 5998
35df4500 5999 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6000 if (b->type == bp_std_terminate)
6001 delete_breakpoint (b);
6002}
6003
c4093a6a 6004struct breakpoint *
a6d9a66e 6005create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6006{
6007 struct breakpoint *b;
c4093a6a 6008
06edf0c0
PA
6009 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
6010 &internal_breakpoint_ops);
6011
b5de0fa7 6012 b->enable_state = bp_enabled;
c4093a6a 6013 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6014 b->addr_string
6015 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6016
b60e7edf 6017 update_global_location_list_nothrow (1);
74960c60 6018
c4093a6a
JM
6019 return b;
6020}
6021
6022void
6023remove_thread_event_breakpoints (void)
6024{
35df4500 6025 struct breakpoint *b, *b_tmp;
c4093a6a 6026
35df4500 6027 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6028 if (b->type == bp_thread_event
6029 && b->loc->pspace == current_program_space)
c4093a6a
JM
6030 delete_breakpoint (b);
6031}
6032
0101ce28
JJ
6033struct lang_and_radix
6034 {
6035 enum language lang;
6036 int radix;
6037 };
6038
4efc6507
DE
6039/* Create a breakpoint for JIT code registration and unregistration. */
6040
6041struct breakpoint *
6042create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6043{
6044 struct breakpoint *b;
6045
06edf0c0
PA
6046 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
6047 &internal_breakpoint_ops);
4efc6507
DE
6048 update_global_location_list_nothrow (1);
6049 return b;
6050}
0101ce28 6051
03673fc7
PP
6052/* Remove JIT code registration and unregistration breakpoint(s). */
6053
6054void
6055remove_jit_event_breakpoints (void)
6056{
6057 struct breakpoint *b, *b_tmp;
6058
6059 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6060 if (b->type == bp_jit_event
6061 && b->loc->pspace == current_program_space)
6062 delete_breakpoint (b);
6063}
6064
cae688ec
JJ
6065void
6066remove_solib_event_breakpoints (void)
6067{
35df4500 6068 struct breakpoint *b, *b_tmp;
cae688ec 6069
35df4500 6070 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6071 if (b->type == bp_shlib_event
6072 && b->loc->pspace == current_program_space)
cae688ec
JJ
6073 delete_breakpoint (b);
6074}
6075
6076struct breakpoint *
a6d9a66e 6077create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6078{
6079 struct breakpoint *b;
6080
06edf0c0
PA
6081 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6082 &internal_breakpoint_ops);
b60e7edf 6083 update_global_location_list_nothrow (1);
cae688ec
JJ
6084 return b;
6085}
6086
6087/* Disable any breakpoints that are on code in shared libraries. Only
6088 apply to enabled breakpoints, disabled ones can just stay disabled. */
6089
6090void
cb851954 6091disable_breakpoints_in_shlibs (void)
cae688ec 6092{
876fa593 6093 struct bp_location *loc, **locp_tmp;
cae688ec 6094
876fa593 6095 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6096 {
2bdf28a0 6097 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6098 struct breakpoint *b = loc->owner;
2bdf28a0 6099
4a64f543
MS
6100 /* We apply the check to all breakpoints, including disabled for
6101 those with loc->duplicate set. This is so that when breakpoint
6102 becomes enabled, or the duplicate is removed, gdb will try to
6103 insert all breakpoints. If we don't set shlib_disabled here,
6104 we'll try to insert those breakpoints and fail. */
1042e4c0 6105 if (((b->type == bp_breakpoint)
508ccb1f 6106 || (b->type == bp_jit_event)
1042e4c0 6107 || (b->type == bp_hardware_breakpoint)
d77f58be 6108 || (is_tracepoint (b)))
6c95b8df 6109 && loc->pspace == current_program_space
0d381245 6110 && !loc->shlib_disabled
a77053c2 6111#ifdef PC_SOLIB
0d381245 6112 && PC_SOLIB (loc->address)
a77053c2 6113#else
6c95b8df 6114 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6115#endif
6116 )
0d381245
VP
6117 {
6118 loc->shlib_disabled = 1;
6119 }
cae688ec
JJ
6120 }
6121}
6122
1e4d1764
YQ
6123/* Disable any breakpoints and tracepoints that are in an unloaded shared
6124 library. Only apply to enabled breakpoints, disabled ones can just stay
4a64f543 6125 disabled. */
84acb35a 6126
75149521 6127static void
84acb35a
JJ
6128disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6129{
876fa593 6130 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6131 int disabled_shlib_breaks = 0;
6132
c86cf029
VP
6133 /* SunOS a.out shared libraries are always mapped, so do not
6134 disable breakpoints; they will only be reported as unloaded
6135 through clear_solib when GDB discards its shared library
6136 list. See clear_solib for more information. */
6137 if (exec_bfd != NULL
6138 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6139 return;
6140
876fa593 6141 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6142 {
2bdf28a0 6143 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6144 struct breakpoint *b = loc->owner;
cc59ec59 6145
1e4d1764 6146 if (solib->pspace == loc->pspace
e2dd7057 6147 && !loc->shlib_disabled
1e4d1764
YQ
6148 && (((b->type == bp_breakpoint
6149 || b->type == bp_jit_event
6150 || b->type == bp_hardware_breakpoint)
6151 && (loc->loc_type == bp_loc_hardware_breakpoint
6152 || loc->loc_type == bp_loc_software_breakpoint))
6153 || is_tracepoint (b))
e2dd7057 6154 && solib_contains_address_p (solib, loc->address))
84acb35a 6155 {
e2dd7057
PP
6156 loc->shlib_disabled = 1;
6157 /* At this point, we cannot rely on remove_breakpoint
6158 succeeding so we must mark the breakpoint as not inserted
6159 to prevent future errors occurring in remove_breakpoints. */
6160 loc->inserted = 0;
8d3788bd
VP
6161
6162 /* This may cause duplicate notifications for the same breakpoint. */
6163 observer_notify_breakpoint_modified (b);
6164
e2dd7057
PP
6165 if (!disabled_shlib_breaks)
6166 {
6167 target_terminal_ours_for_output ();
3e43a32a
MS
6168 warning (_("Temporarily disabling breakpoints "
6169 "for unloaded shared library \"%s\""),
e2dd7057 6170 solib->so_name);
84acb35a 6171 }
e2dd7057 6172 disabled_shlib_breaks = 1;
84acb35a
JJ
6173 }
6174 }
84acb35a
JJ
6175}
6176
ce78b96d
JB
6177/* FORK & VFORK catchpoints. */
6178
e29a4733
PA
6179/* An instance of this type is used to represent a fork or vfork
6180 catchpoint. It includes a "struct breakpoint" as a kind of base
6181 class; users downcast to "struct breakpoint *" when needed. A
6182 breakpoint is really of this type iff its ops pointer points to
6183 CATCH_FORK_BREAKPOINT_OPS. */
6184
6185struct fork_catchpoint
6186{
6187 /* The base class. */
6188 struct breakpoint base;
6189
6190 /* Process id of a child process whose forking triggered this
6191 catchpoint. This field is only valid immediately after this
6192 catchpoint has triggered. */
6193 ptid_t forked_inferior_pid;
6194};
6195
4a64f543
MS
6196/* Implement the "insert" breakpoint_ops method for fork
6197 catchpoints. */
ce78b96d 6198
77b06cd7
TJB
6199static int
6200insert_catch_fork (struct bp_location *bl)
ce78b96d 6201{
77b06cd7 6202 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6203}
6204
4a64f543
MS
6205/* Implement the "remove" breakpoint_ops method for fork
6206 catchpoints. */
ce78b96d
JB
6207
6208static int
77b06cd7 6209remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6210{
6211 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6212}
6213
6214/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6215 catchpoints. */
6216
6217static int
f1310107
TJB
6218breakpoint_hit_catch_fork (const struct bp_location *bl,
6219 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6220{
e29a4733
PA
6221 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6222
6223 return inferior_has_forked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6224}
6225
4a64f543
MS
6226/* Implement the "print_it" breakpoint_ops method for fork
6227 catchpoints. */
ce78b96d
JB
6228
6229static enum print_stop_action
348d480f 6230print_it_catch_fork (bpstat bs)
ce78b96d 6231{
36dfb11c 6232 struct ui_out *uiout = current_uiout;
348d480f
PA
6233 struct breakpoint *b = bs->breakpoint_at;
6234 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 6235
ce78b96d 6236 annotate_catchpoint (b->number);
36dfb11c
TT
6237 if (b->disposition == disp_del)
6238 ui_out_text (uiout, "\nTemporary catchpoint ");
6239 else
6240 ui_out_text (uiout, "\nCatchpoint ");
6241 if (ui_out_is_mi_like_p (uiout))
6242 {
6243 ui_out_field_string (uiout, "reason",
6244 async_reason_lookup (EXEC_ASYNC_FORK));
6245 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6246 }
6247 ui_out_field_int (uiout, "bkptno", b->number);
6248 ui_out_text (uiout, " (forked process ");
6249 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6250 ui_out_text (uiout, "), ");
ce78b96d
JB
6251 return PRINT_SRC_AND_LOC;
6252}
6253
4a64f543
MS
6254/* Implement the "print_one" breakpoint_ops method for fork
6255 catchpoints. */
ce78b96d
JB
6256
6257static void
a6d9a66e 6258print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6259{
e29a4733 6260 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6261 struct value_print_options opts;
79a45e25 6262 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6263
6264 get_user_print_options (&opts);
6265
4a64f543
MS
6266 /* Field 4, the address, is omitted (which makes the columns not
6267 line up too nicely with the headers, but the effect is relatively
6268 readable). */
79a45b7d 6269 if (opts.addressprint)
ce78b96d
JB
6270 ui_out_field_skip (uiout, "addr");
6271 annotate_field (5);
6272 ui_out_text (uiout, "fork");
e29a4733 6273 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6274 {
6275 ui_out_text (uiout, ", process ");
6276 ui_out_field_int (uiout, "what",
e29a4733 6277 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6278 ui_out_spaces (uiout, 1);
6279 }
6280}
6281
6282/* Implement the "print_mention" breakpoint_ops method for fork
6283 catchpoints. */
6284
6285static void
6286print_mention_catch_fork (struct breakpoint *b)
6287{
6288 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6289}
6290
6149aea9
PA
6291/* Implement the "print_recreate" breakpoint_ops method for fork
6292 catchpoints. */
6293
6294static void
6295print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6296{
6297 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 6298 print_recreate_thread (b, fp);
6149aea9
PA
6299}
6300
ce78b96d
JB
6301/* The breakpoint_ops structure to be used in fork catchpoints. */
6302
2060206e 6303static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 6304
4a64f543
MS
6305/* Implement the "insert" breakpoint_ops method for vfork
6306 catchpoints. */
ce78b96d 6307
77b06cd7
TJB
6308static int
6309insert_catch_vfork (struct bp_location *bl)
ce78b96d 6310{
77b06cd7 6311 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6312}
6313
4a64f543
MS
6314/* Implement the "remove" breakpoint_ops method for vfork
6315 catchpoints. */
ce78b96d
JB
6316
6317static int
77b06cd7 6318remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6319{
6320 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6321}
6322
6323/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6324 catchpoints. */
6325
6326static int
f1310107
TJB
6327breakpoint_hit_catch_vfork (const struct bp_location *bl,
6328 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6329{
e29a4733
PA
6330 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6331
6332 return inferior_has_vforked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6333}
6334
4a64f543
MS
6335/* Implement the "print_it" breakpoint_ops method for vfork
6336 catchpoints. */
ce78b96d
JB
6337
6338static enum print_stop_action
348d480f 6339print_it_catch_vfork (bpstat bs)
ce78b96d 6340{
36dfb11c 6341 struct ui_out *uiout = current_uiout;
348d480f 6342 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
6343 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6344
ce78b96d 6345 annotate_catchpoint (b->number);
36dfb11c
TT
6346 if (b->disposition == disp_del)
6347 ui_out_text (uiout, "\nTemporary catchpoint ");
6348 else
6349 ui_out_text (uiout, "\nCatchpoint ");
6350 if (ui_out_is_mi_like_p (uiout))
6351 {
6352 ui_out_field_string (uiout, "reason",
6353 async_reason_lookup (EXEC_ASYNC_VFORK));
6354 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6355 }
6356 ui_out_field_int (uiout, "bkptno", b->number);
6357 ui_out_text (uiout, " (vforked process ");
6358 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6359 ui_out_text (uiout, "), ");
ce78b96d
JB
6360 return PRINT_SRC_AND_LOC;
6361}
6362
4a64f543
MS
6363/* Implement the "print_one" breakpoint_ops method for vfork
6364 catchpoints. */
ce78b96d
JB
6365
6366static void
a6d9a66e 6367print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6368{
e29a4733 6369 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6370 struct value_print_options opts;
79a45e25 6371 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6372
6373 get_user_print_options (&opts);
4a64f543
MS
6374 /* Field 4, the address, is omitted (which makes the columns not
6375 line up too nicely with the headers, but the effect is relatively
6376 readable). */
79a45b7d 6377 if (opts.addressprint)
ce78b96d
JB
6378 ui_out_field_skip (uiout, "addr");
6379 annotate_field (5);
6380 ui_out_text (uiout, "vfork");
e29a4733 6381 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6382 {
6383 ui_out_text (uiout, ", process ");
6384 ui_out_field_int (uiout, "what",
e29a4733 6385 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6386 ui_out_spaces (uiout, 1);
6387 }
6388}
6389
6390/* Implement the "print_mention" breakpoint_ops method for vfork
6391 catchpoints. */
6392
6393static void
6394print_mention_catch_vfork (struct breakpoint *b)
6395{
6396 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6397}
6398
6149aea9
PA
6399/* Implement the "print_recreate" breakpoint_ops method for vfork
6400 catchpoints. */
6401
6402static void
6403print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6404{
6405 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 6406 print_recreate_thread (b, fp);
6149aea9
PA
6407}
6408
ce78b96d
JB
6409/* The breakpoint_ops structure to be used in vfork catchpoints. */
6410
2060206e 6411static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 6412
be5c67c1
PA
6413/* An instance of this type is used to represent a syscall catchpoint.
6414 It includes a "struct breakpoint" as a kind of base class; users
6415 downcast to "struct breakpoint *" when needed. A breakpoint is
6416 really of this type iff its ops pointer points to
6417 CATCH_SYSCALL_BREAKPOINT_OPS. */
6418
6419struct syscall_catchpoint
6420{
6421 /* The base class. */
6422 struct breakpoint base;
6423
6424 /* Syscall numbers used for the 'catch syscall' feature. If no
6425 syscall has been specified for filtering, its value is NULL.
6426 Otherwise, it holds a list of all syscalls to be caught. The
6427 list elements are allocated with xmalloc. */
6428 VEC(int) *syscalls_to_be_caught;
6429};
6430
6431/* Implement the "dtor" breakpoint_ops method for syscall
6432 catchpoints. */
6433
6434static void
6435dtor_catch_syscall (struct breakpoint *b)
6436{
6437 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6438
6439 VEC_free (int, c->syscalls_to_be_caught);
348d480f 6440
2060206e 6441 base_breakpoint_ops.dtor (b);
be5c67c1
PA
6442}
6443
a96d9b2e
SDJ
6444/* Implement the "insert" breakpoint_ops method for syscall
6445 catchpoints. */
6446
77b06cd7
TJB
6447static int
6448insert_catch_syscall (struct bp_location *bl)
a96d9b2e 6449{
be5c67c1 6450 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6451 struct inferior *inf = current_inferior ();
6452
6453 ++inf->total_syscalls_count;
be5c67c1 6454 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6455 ++inf->any_syscall_count;
6456 else
6457 {
6458 int i, iter;
cc59ec59 6459
a96d9b2e 6460 for (i = 0;
be5c67c1 6461 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6462 i++)
6463 {
6464 int elem;
cc59ec59 6465
a96d9b2e
SDJ
6466 if (iter >= VEC_length (int, inf->syscalls_counts))
6467 {
6468 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6469 uintptr_t vec_addr_offset
6470 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6471 uintptr_t vec_addr;
6472 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6473 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6474 vec_addr_offset;
6475 memset ((void *) vec_addr, 0,
6476 (iter + 1 - old_size) * sizeof (int));
6477 }
6478 elem = VEC_index (int, inf->syscalls_counts, iter);
6479 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6480 }
6481 }
6482
77b06cd7
TJB
6483 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6484 inf->total_syscalls_count != 0,
6485 inf->any_syscall_count,
6486 VEC_length (int, inf->syscalls_counts),
6487 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6488}
6489
6490/* Implement the "remove" breakpoint_ops method for syscall
6491 catchpoints. */
6492
6493static int
77b06cd7 6494remove_catch_syscall (struct bp_location *bl)
a96d9b2e 6495{
be5c67c1 6496 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6497 struct inferior *inf = current_inferior ();
6498
6499 --inf->total_syscalls_count;
be5c67c1 6500 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6501 --inf->any_syscall_count;
6502 else
6503 {
6504 int i, iter;
cc59ec59 6505
a96d9b2e 6506 for (i = 0;
be5c67c1 6507 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6508 i++)
6509 {
6510 int elem;
6511 if (iter >= VEC_length (int, inf->syscalls_counts))
6512 /* Shouldn't happen. */
6513 continue;
6514 elem = VEC_index (int, inf->syscalls_counts, iter);
6515 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6516 }
6517 }
6518
6519 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6520 inf->total_syscalls_count != 0,
6521 inf->any_syscall_count,
6522 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6523 VEC_address (int,
6524 inf->syscalls_counts));
a96d9b2e
SDJ
6525}
6526
6527/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6528 catchpoints. */
6529
6530static int
f1310107
TJB
6531breakpoint_hit_catch_syscall (const struct bp_location *bl,
6532 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6533{
4a64f543
MS
6534 /* We must check if we are catching specific syscalls in this
6535 breakpoint. If we are, then we must guarantee that the called
6536 syscall is the same syscall we are catching. */
a96d9b2e 6537 int syscall_number = 0;
be5c67c1
PA
6538 const struct syscall_catchpoint *c
6539 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6540
6541 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6542 return 0;
6543
6544 /* Now, checking if the syscall is the same. */
be5c67c1 6545 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6546 {
6547 int i, iter;
cc59ec59 6548
a96d9b2e 6549 for (i = 0;
be5c67c1 6550 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6551 i++)
6552 if (syscall_number == iter)
6553 break;
6554 /* Not the same. */
6555 if (!iter)
6556 return 0;
6557 }
6558
6559 return 1;
6560}
6561
6562/* Implement the "print_it" breakpoint_ops method for syscall
6563 catchpoints. */
6564
6565static enum print_stop_action
348d480f 6566print_it_catch_syscall (bpstat bs)
a96d9b2e 6567{
36dfb11c 6568 struct ui_out *uiout = current_uiout;
348d480f 6569 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
6570 /* These are needed because we want to know in which state a
6571 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6572 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6573 must print "called syscall" or "returned from syscall". */
6574 ptid_t ptid;
6575 struct target_waitstatus last;
6576 struct syscall s;
a96d9b2e
SDJ
6577 char *syscall_id;
6578
6579 get_last_target_status (&ptid, &last);
6580
6581 get_syscall_by_number (last.value.syscall_number, &s);
6582
6583 annotate_catchpoint (b->number);
6584
36dfb11c
TT
6585 if (b->disposition == disp_del)
6586 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 6587 else
36dfb11c
TT
6588 ui_out_text (uiout, "\nCatchpoint ");
6589 if (ui_out_is_mi_like_p (uiout))
6590 {
6591 ui_out_field_string (uiout, "reason",
6592 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
6593 ? EXEC_ASYNC_SYSCALL_ENTRY
6594 : EXEC_ASYNC_SYSCALL_RETURN));
6595 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6596 }
6597 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
6598
6599 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
6600 ui_out_text (uiout, " (call to syscall ");
6601 else
6602 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 6603
36dfb11c
TT
6604 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
6605 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
6606 if (s.name != NULL)
6607 ui_out_field_string (uiout, "syscall-name", s.name);
6608
6609 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
6610
6611 return PRINT_SRC_AND_LOC;
6612}
6613
6614/* Implement the "print_one" breakpoint_ops method for syscall
6615 catchpoints. */
6616
6617static void
6618print_one_catch_syscall (struct breakpoint *b,
f1310107 6619 struct bp_location **last_loc)
a96d9b2e 6620{
be5c67c1 6621 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 6622 struct value_print_options opts;
79a45e25 6623 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
6624
6625 get_user_print_options (&opts);
4a64f543
MS
6626 /* Field 4, the address, is omitted (which makes the columns not
6627 line up too nicely with the headers, but the effect is relatively
6628 readable). */
a96d9b2e
SDJ
6629 if (opts.addressprint)
6630 ui_out_field_skip (uiout, "addr");
6631 annotate_field (5);
6632
be5c67c1
PA
6633 if (c->syscalls_to_be_caught
6634 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6635 ui_out_text (uiout, "syscalls \"");
6636 else
6637 ui_out_text (uiout, "syscall \"");
6638
be5c67c1 6639 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6640 {
6641 int i, iter;
6642 char *text = xstrprintf ("%s", "");
cc59ec59 6643
a96d9b2e 6644 for (i = 0;
be5c67c1 6645 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6646 i++)
6647 {
6648 char *x = text;
6649 struct syscall s;
6650 get_syscall_by_number (iter, &s);
6651
6652 if (s.name != NULL)
6653 text = xstrprintf ("%s%s, ", text, s.name);
6654 else
6655 text = xstrprintf ("%s%d, ", text, iter);
6656
6657 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 6658 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
6659 on every call. */
6660 xfree (x);
6661 }
6662 /* Remove the last comma. */
6663 text[strlen (text) - 2] = '\0';
6664 ui_out_field_string (uiout, "what", text);
6665 }
6666 else
6667 ui_out_field_string (uiout, "what", "<any syscall>");
6668 ui_out_text (uiout, "\" ");
6669}
6670
6671/* Implement the "print_mention" breakpoint_ops method for syscall
6672 catchpoints. */
6673
6674static void
6675print_mention_catch_syscall (struct breakpoint *b)
6676{
be5c67c1
PA
6677 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6678
6679 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6680 {
6681 int i, iter;
6682
be5c67c1 6683 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6684 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6685 else
6686 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6687
6688 for (i = 0;
be5c67c1 6689 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6690 i++)
6691 {
6692 struct syscall s;
6693 get_syscall_by_number (iter, &s);
6694
6695 if (s.name)
6696 printf_filtered (" '%s' [%d]", s.name, s.number);
6697 else
6698 printf_filtered (" %d", s.number);
6699 }
6700 printf_filtered (")");
6701 }
6702 else
6703 printf_filtered (_("Catchpoint %d (any syscall)"),
6704 b->number);
6705}
6706
6149aea9
PA
6707/* Implement the "print_recreate" breakpoint_ops method for syscall
6708 catchpoints. */
6709
6710static void
6711print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6712{
be5c67c1
PA
6713 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6714
6149aea9
PA
6715 fprintf_unfiltered (fp, "catch syscall");
6716
be5c67c1 6717 if (c->syscalls_to_be_caught)
6149aea9
PA
6718 {
6719 int i, iter;
6720
6721 for (i = 0;
be5c67c1 6722 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
6723 i++)
6724 {
6725 struct syscall s;
6726
6727 get_syscall_by_number (iter, &s);
6728 if (s.name)
6729 fprintf_unfiltered (fp, " %s", s.name);
6730 else
6731 fprintf_unfiltered (fp, " %d", s.number);
6732 }
6733 }
d9b3f62e 6734 print_recreate_thread (b, fp);
6149aea9
PA
6735}
6736
a96d9b2e
SDJ
6737/* The breakpoint_ops structure to be used in syscall catchpoints. */
6738
2060206e 6739static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
6740
6741/* Returns non-zero if 'b' is a syscall catchpoint. */
6742
6743static int
6744syscall_catchpoint_p (struct breakpoint *b)
6745{
6746 return (b->ops == &catch_syscall_breakpoint_ops);
6747}
6748
346774a9
PA
6749/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
6750 is non-zero, then make the breakpoint temporary. If COND_STRING is
6751 not NULL, then store it in the breakpoint. OPS, if not NULL, is
6752 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 6753
346774a9
PA
6754static void
6755init_catchpoint (struct breakpoint *b,
6756 struct gdbarch *gdbarch, int tempflag,
6757 char *cond_string,
c0a91b2b 6758 const struct breakpoint_ops *ops)
c906108c 6759{
c5aa993b 6760 struct symtab_and_line sal;
346774a9 6761
fe39c653 6762 init_sal (&sal);
6c95b8df 6763 sal.pspace = current_program_space;
c5aa993b 6764
28010a5d 6765 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 6766
1b36a34b 6767 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 6768 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
6769}
6770
28010a5d 6771void
3ea46bff 6772install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
6773{
6774 add_to_breakpoint_chain (b);
3a5c3e22
PA
6775 set_breakpoint_number (internal, b);
6776 if (!internal)
6777 mention (b);
c56053d2 6778 observer_notify_breakpoint_created (b);
3ea46bff
YQ
6779
6780 if (update_gll)
6781 update_global_location_list (1);
c56053d2
PA
6782}
6783
9b70b993 6784static void
a6d9a66e
UW
6785create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6786 int tempflag, char *cond_string,
c0a91b2b 6787 const struct breakpoint_ops *ops)
c906108c 6788{
e29a4733 6789 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 6790
e29a4733
PA
6791 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
6792
6793 c->forked_inferior_pid = null_ptid;
6794
3ea46bff 6795 install_breakpoint (0, &c->base, 1);
c906108c
SS
6796}
6797
fe798b75
JB
6798/* Exec catchpoints. */
6799
b4d90040
PA
6800/* An instance of this type is used to represent an exec catchpoint.
6801 It includes a "struct breakpoint" as a kind of base class; users
6802 downcast to "struct breakpoint *" when needed. A breakpoint is
6803 really of this type iff its ops pointer points to
6804 CATCH_EXEC_BREAKPOINT_OPS. */
6805
6806struct exec_catchpoint
6807{
6808 /* The base class. */
6809 struct breakpoint base;
6810
6811 /* Filename of a program whose exec triggered this catchpoint.
6812 This field is only valid immediately after this catchpoint has
6813 triggered. */
6814 char *exec_pathname;
6815};
6816
6817/* Implement the "dtor" breakpoint_ops method for exec
6818 catchpoints. */
6819
6820static void
6821dtor_catch_exec (struct breakpoint *b)
6822{
6823 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6824
6825 xfree (c->exec_pathname);
348d480f 6826
2060206e 6827 base_breakpoint_ops.dtor (b);
b4d90040
PA
6828}
6829
77b06cd7
TJB
6830static int
6831insert_catch_exec (struct bp_location *bl)
c906108c 6832{
77b06cd7 6833 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6834}
c906108c 6835
fe798b75 6836static int
77b06cd7 6837remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6838{
6839 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6840}
c906108c 6841
fe798b75 6842static int
f1310107
TJB
6843breakpoint_hit_catch_exec (const struct bp_location *bl,
6844 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6845{
b4d90040
PA
6846 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
6847
6848 return inferior_has_execd (inferior_ptid, &c->exec_pathname);
fe798b75 6849}
c906108c 6850
fe798b75 6851static enum print_stop_action
348d480f 6852print_it_catch_exec (bpstat bs)
fe798b75 6853{
36dfb11c 6854 struct ui_out *uiout = current_uiout;
348d480f 6855 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
6856 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6857
fe798b75 6858 annotate_catchpoint (b->number);
36dfb11c
TT
6859 if (b->disposition == disp_del)
6860 ui_out_text (uiout, "\nTemporary catchpoint ");
6861 else
6862 ui_out_text (uiout, "\nCatchpoint ");
6863 if (ui_out_is_mi_like_p (uiout))
6864 {
6865 ui_out_field_string (uiout, "reason",
6866 async_reason_lookup (EXEC_ASYNC_EXEC));
6867 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6868 }
6869 ui_out_field_int (uiout, "bkptno", b->number);
6870 ui_out_text (uiout, " (exec'd ");
6871 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
6872 ui_out_text (uiout, "), ");
6873
fe798b75 6874 return PRINT_SRC_AND_LOC;
c906108c
SS
6875}
6876
fe798b75 6877static void
a6d9a66e 6878print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 6879{
b4d90040 6880 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 6881 struct value_print_options opts;
79a45e25 6882 struct ui_out *uiout = current_uiout;
fe798b75
JB
6883
6884 get_user_print_options (&opts);
6885
6886 /* Field 4, the address, is omitted (which makes the columns
6887 not line up too nicely with the headers, but the effect
6888 is relatively readable). */
6889 if (opts.addressprint)
6890 ui_out_field_skip (uiout, "addr");
6891 annotate_field (5);
6892 ui_out_text (uiout, "exec");
b4d90040 6893 if (c->exec_pathname != NULL)
fe798b75
JB
6894 {
6895 ui_out_text (uiout, ", program \"");
b4d90040 6896 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
6897 ui_out_text (uiout, "\" ");
6898 }
6899}
6900
6901static void
6902print_mention_catch_exec (struct breakpoint *b)
6903{
6904 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6905}
6906
6149aea9
PA
6907/* Implement the "print_recreate" breakpoint_ops method for exec
6908 catchpoints. */
6909
6910static void
6911print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6912{
6913 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 6914 print_recreate_thread (b, fp);
6149aea9
PA
6915}
6916
2060206e 6917static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 6918
a96d9b2e
SDJ
6919static void
6920create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 6921 const struct breakpoint_ops *ops)
a96d9b2e 6922{
be5c67c1 6923 struct syscall_catchpoint *c;
a96d9b2e 6924 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 6925
be5c67c1
PA
6926 c = XNEW (struct syscall_catchpoint);
6927 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
6928 c->syscalls_to_be_caught = filter;
a96d9b2e 6929
3ea46bff 6930 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
6931}
6932
c906108c 6933static int
fba45db2 6934hw_breakpoint_used_count (void)
c906108c 6935{
c906108c 6936 int i = 0;
f1310107
TJB
6937 struct breakpoint *b;
6938 struct bp_location *bl;
c906108c
SS
6939
6940 ALL_BREAKPOINTS (b)
c5aa993b 6941 {
d6b74ac4 6942 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
6943 for (bl = b->loc; bl; bl = bl->next)
6944 {
6945 /* Special types of hardware breakpoints may use more than
6946 one register. */
348d480f 6947 i += b->ops->resources_needed (bl);
f1310107 6948 }
c5aa993b 6949 }
c906108c
SS
6950
6951 return i;
6952}
6953
a1398e0c
PA
6954/* Returns the resources B would use if it were a hardware
6955 watchpoint. */
6956
c906108c 6957static int
a1398e0c 6958hw_watchpoint_use_count (struct breakpoint *b)
c906108c 6959{
c906108c 6960 int i = 0;
e09342b5 6961 struct bp_location *bl;
c906108c 6962
a1398e0c
PA
6963 if (!breakpoint_enabled (b))
6964 return 0;
6965
6966 for (bl = b->loc; bl; bl = bl->next)
6967 {
6968 /* Special types of hardware watchpoints may use more than
6969 one register. */
6970 i += b->ops->resources_needed (bl);
6971 }
6972
6973 return i;
6974}
6975
6976/* Returns the sum the used resources of all hardware watchpoints of
6977 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
6978 the sum of the used resources of all hardware watchpoints of other
6979 types _not_ TYPE. */
6980
6981static int
6982hw_watchpoint_used_count_others (struct breakpoint *except,
6983 enum bptype type, int *other_type_used)
6984{
6985 int i = 0;
6986 struct breakpoint *b;
6987
c906108c
SS
6988 *other_type_used = 0;
6989 ALL_BREAKPOINTS (b)
e09342b5 6990 {
a1398e0c
PA
6991 if (b == except)
6992 continue;
e09342b5
TJB
6993 if (!breakpoint_enabled (b))
6994 continue;
6995
a1398e0c
PA
6996 if (b->type == type)
6997 i += hw_watchpoint_use_count (b);
6998 else if (is_hardware_watchpoint (b))
6999 *other_type_used = 1;
e09342b5
TJB
7000 }
7001
c906108c
SS
7002 return i;
7003}
7004
c906108c 7005void
fba45db2 7006disable_watchpoints_before_interactive_call_start (void)
c906108c 7007{
c5aa993b 7008 struct breakpoint *b;
c906108c
SS
7009
7010 ALL_BREAKPOINTS (b)
c5aa993b 7011 {
cc60f2e3 7012 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 7013 {
b5de0fa7 7014 b->enable_state = bp_call_disabled;
b60e7edf 7015 update_global_location_list (0);
c5aa993b
JM
7016 }
7017 }
c906108c
SS
7018}
7019
7020void
fba45db2 7021enable_watchpoints_after_interactive_call_stop (void)
c906108c 7022{
c5aa993b 7023 struct breakpoint *b;
c906108c
SS
7024
7025 ALL_BREAKPOINTS (b)
c5aa993b 7026 {
cc60f2e3 7027 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 7028 {
b5de0fa7 7029 b->enable_state = bp_enabled;
b60e7edf 7030 update_global_location_list (1);
c5aa993b
JM
7031 }
7032 }
c906108c
SS
7033}
7034
8bea4e01
UW
7035void
7036disable_breakpoints_before_startup (void)
7037{
6c95b8df 7038 current_program_space->executing_startup = 1;
f8eba3c6 7039 update_global_location_list (0);
8bea4e01
UW
7040}
7041
7042void
7043enable_breakpoints_after_startup (void)
7044{
6c95b8df 7045 current_program_space->executing_startup = 0;
f8eba3c6 7046 breakpoint_re_set ();
8bea4e01
UW
7047}
7048
c906108c
SS
7049
7050/* Set a breakpoint that will evaporate an end of command
7051 at address specified by SAL.
7052 Restrict it to frame FRAME if FRAME is nonzero. */
7053
7054struct breakpoint *
a6d9a66e
UW
7055set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7056 struct frame_id frame_id, enum bptype type)
c906108c 7057{
52f0bd74 7058 struct breakpoint *b;
edb3359d
DJ
7059
7060 /* If FRAME_ID is valid, it should be a real frame, not an inlined
7061 one. */
7062 gdb_assert (!frame_id_inlined_p (frame_id));
7063
06edf0c0 7064 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
7065 b->enable_state = bp_enabled;
7066 b->disposition = disp_donttouch;
818dd999 7067 b->frame_id = frame_id;
c906108c 7068
4a64f543
MS
7069 /* If we're debugging a multi-threaded program, then we want
7070 momentary breakpoints to be active in only a single thread of
7071 control. */
39f77062
KB
7072 if (in_thread_list (inferior_ptid))
7073 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 7074
b60e7edf 7075 update_global_location_list_nothrow (1);
74960c60 7076
c906108c
SS
7077 return b;
7078}
611c83ae 7079
06edf0c0
PA
7080/* Make a momentary breakpoint based on the master breakpoint ORIG.
7081 The new breakpoint will have type TYPE, and use OPS as it
7082 breakpoint_ops. */
e58b0e63 7083
06edf0c0
PA
7084static struct breakpoint *
7085momentary_breakpoint_from_master (struct breakpoint *orig,
7086 enum bptype type,
c0a91b2b 7087 const struct breakpoint_ops *ops)
e58b0e63
PA
7088{
7089 struct breakpoint *copy;
7090
06edf0c0 7091 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 7092 copy->loc = allocate_bp_location (copy);
0e30163f 7093 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 7094
a6d9a66e 7095 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7096 copy->loc->requested_address = orig->loc->requested_address;
7097 copy->loc->address = orig->loc->address;
7098 copy->loc->section = orig->loc->section;
6c95b8df 7099 copy->loc->pspace = orig->loc->pspace;
e58b0e63 7100
f8eba3c6
TT
7101 if (orig->loc->source_file != NULL)
7102 copy->loc->source_file = xstrdup (orig->loc->source_file);
e58b0e63 7103
f8eba3c6 7104 copy->loc->line_number = orig->loc->line_number;
e58b0e63
PA
7105 copy->frame_id = orig->frame_id;
7106 copy->thread = orig->thread;
6c95b8df 7107 copy->pspace = orig->pspace;
e58b0e63
PA
7108
7109 copy->enable_state = bp_enabled;
7110 copy->disposition = disp_donttouch;
7111 copy->number = internal_breakpoint_number--;
7112
7113 update_global_location_list_nothrow (0);
7114 return copy;
7115}
7116
06edf0c0
PA
7117/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7118 ORIG is NULL. */
7119
7120struct breakpoint *
7121clone_momentary_breakpoint (struct breakpoint *orig)
7122{
7123 /* If there's nothing to clone, then return nothing. */
7124 if (orig == NULL)
7125 return NULL;
7126
7127 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
7128}
7129
611c83ae 7130struct breakpoint *
a6d9a66e
UW
7131set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7132 enum bptype type)
611c83ae
PA
7133{
7134 struct symtab_and_line sal;
7135
7136 sal = find_pc_line (pc, 0);
7137 sal.pc = pc;
7138 sal.section = find_pc_overlay (pc);
7139 sal.explicit_pc = 1;
7140
a6d9a66e 7141 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7142}
c906108c 7143\f
c5aa993b 7144
c906108c
SS
7145/* Tell the user we have just set a breakpoint B. */
7146
7147static void
fba45db2 7148mention (struct breakpoint *b)
c906108c 7149{
348d480f 7150 b->ops->print_mention (b);
79a45e25 7151 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 7152 return;
c906108c
SS
7153 printf_filtered ("\n");
7154}
c906108c 7155\f
c5aa993b 7156
0d381245 7157static struct bp_location *
39d61571 7158add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7159 const struct symtab_and_line *sal)
7160{
7161 struct bp_location *loc, **tmp;
3742cc8b
YQ
7162 CORE_ADDR adjusted_address;
7163 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
7164
7165 if (loc_gdbarch == NULL)
7166 loc_gdbarch = b->gdbarch;
7167
7168 /* Adjust the breakpoint's address prior to allocating a location.
7169 Once we call allocate_bp_location(), that mostly uninitialized
7170 location will be placed on the location chain. Adjustment of the
7171 breakpoint may cause target_read_memory() to be called and we do
7172 not want its scan of the location chain to find a breakpoint and
7173 location that's only been partially initialized. */
7174 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
7175 sal->pc, b->type);
0d381245 7176
39d61571 7177 loc = allocate_bp_location (b);
0d381245
VP
7178 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7179 ;
7180 *tmp = loc;
3742cc8b 7181
0d381245 7182 loc->requested_address = sal->pc;
3742cc8b 7183 loc->address = adjusted_address;
6c95b8df
PA
7184 loc->pspace = sal->pspace;
7185 gdb_assert (loc->pspace != NULL);
0d381245 7186 loc->section = sal->section;
3742cc8b 7187 loc->gdbarch = loc_gdbarch;
f8eba3c6
TT
7188
7189 if (sal->symtab != NULL)
7190 loc->source_file = xstrdup (sal->symtab->filename);
7191 loc->line_number = sal->line;
7192
0e30163f
JK
7193 set_breakpoint_location_function (loc,
7194 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7195 return loc;
7196}
514f746b
AR
7197\f
7198
7199/* Return 1 if LOC is pointing to a permanent breakpoint,
7200 return 0 otherwise. */
7201
7202static int
7203bp_loc_is_permanent (struct bp_location *loc)
7204{
7205 int len;
7206 CORE_ADDR addr;
1afeeb75 7207 const gdb_byte *bpoint;
514f746b 7208 gdb_byte *target_mem;
939c61fa
JK
7209 struct cleanup *cleanup;
7210 int retval = 0;
514f746b
AR
7211
7212 gdb_assert (loc != NULL);
7213
7214 addr = loc->address;
1afeeb75 7215 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7216
939c61fa 7217 /* Software breakpoints unsupported? */
1afeeb75 7218 if (bpoint == NULL)
939c61fa
JK
7219 return 0;
7220
514f746b
AR
7221 target_mem = alloca (len);
7222
939c61fa
JK
7223 /* Enable the automatic memory restoration from breakpoints while
7224 we read the memory. Otherwise we could say about our temporary
7225 breakpoints they are permanent. */
6c95b8df
PA
7226 cleanup = save_current_space_and_thread ();
7227
7228 switch_to_program_space_and_thread (loc->pspace);
7229 make_show_memory_breakpoints_cleanup (0);
939c61fa 7230
514f746b 7231 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 7232 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 7233 retval = 1;
514f746b 7234
939c61fa
JK
7235 do_cleanups (cleanup);
7236
7237 return retval;
514f746b
AR
7238}
7239
7240
c3f6f71d 7241
018d34a4
VP
7242/* Create a breakpoint with SAL as location. Use ADDR_STRING
7243 as textual description of the location, and COND_STRING
db107f19 7244 as condition expression. */
018d34a4
VP
7245
7246static void
d9b3f62e
PA
7247init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
7248 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 7249 char *filter, char *cond_string,
d9b3f62e
PA
7250 enum bptype type, enum bpdisp disposition,
7251 int thread, int task, int ignore_count,
c0a91b2b 7252 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e 7253 int enabled, int internal, int display_canonical)
018d34a4 7254{
0d381245 7255 int i;
018d34a4
VP
7256
7257 if (type == bp_hardware_breakpoint)
7258 {
fbbd034e
AS
7259 int target_resources_ok;
7260
7261 i = hw_breakpoint_used_count ();
7262 target_resources_ok =
7263 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7264 i + 1, 0);
7265 if (target_resources_ok == 0)
7266 error (_("No hardware breakpoint support in the target."));
7267 else if (target_resources_ok < 0)
7268 error (_("Hardware breakpoints used exceeds limit."));
7269 }
7270
6c95b8df
PA
7271 gdb_assert (sals.nelts > 0);
7272
0d381245
VP
7273 for (i = 0; i < sals.nelts; ++i)
7274 {
7275 struct symtab_and_line sal = sals.sals[i];
7276 struct bp_location *loc;
7277
7278 if (from_tty)
5af949e3
UW
7279 {
7280 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7281 if (!loc_gdbarch)
7282 loc_gdbarch = gdbarch;
7283
7284 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7285 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7286 }
0d381245
VP
7287
7288 if (i == 0)
7289 {
d9b3f62e 7290 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 7291 b->thread = thread;
4a306c9a 7292 b->task = task;
018d34a4 7293
0d381245
VP
7294 b->cond_string = cond_string;
7295 b->ignore_count = ignore_count;
41447f92 7296 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7297 b->disposition = disposition;
6c95b8df 7298
0fb4aa4b
PA
7299 if (type == bp_static_tracepoint)
7300 {
d9b3f62e 7301 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
7302 struct static_tracepoint_marker marker;
7303
7304 if (is_marker_spec (addr_string))
7305 {
7306 /* We already know the marker exists, otherwise, we
7307 wouldn't see a sal for it. */
7308 char *p = &addr_string[3];
7309 char *endp;
7310 char *marker_str;
0fb4aa4b 7311
e9cafbcc 7312 p = skip_spaces (p);
0fb4aa4b 7313
e9cafbcc 7314 endp = skip_to_space (p);
0fb4aa4b
PA
7315
7316 marker_str = savestring (p, endp - p);
d9b3f62e 7317 t->static_trace_marker_id = marker_str;
0fb4aa4b 7318
3e43a32a
MS
7319 printf_filtered (_("Probed static tracepoint "
7320 "marker \"%s\"\n"),
d9b3f62e 7321 t->static_trace_marker_id);
0fb4aa4b
PA
7322 }
7323 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7324 {
d9b3f62e 7325 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
7326 release_static_tracepoint_marker (&marker);
7327
3e43a32a
MS
7328 printf_filtered (_("Probed static tracepoint "
7329 "marker \"%s\"\n"),
d9b3f62e 7330 t->static_trace_marker_id);
0fb4aa4b
PA
7331 }
7332 else
3e43a32a
MS
7333 warning (_("Couldn't determine the static "
7334 "tracepoint marker to probe"));
0fb4aa4b
PA
7335 }
7336
0d381245
VP
7337 loc = b->loc;
7338 }
7339 else
018d34a4 7340 {
39d61571 7341 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7342 }
7343
514f746b
AR
7344 if (bp_loc_is_permanent (loc))
7345 make_breakpoint_permanent (b);
7346
0d381245
VP
7347 if (b->cond_string)
7348 {
7349 char *arg = b->cond_string;
d32a6982 7350 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7351 if (*arg)
db107f19 7352 error (_("Garbage %s follows condition"), arg);
018d34a4 7353 }
0d381245 7354 }
018d34a4 7355
56435ebe 7356 b->display_canonical = display_canonical;
018d34a4
VP
7357 if (addr_string)
7358 b->addr_string = addr_string;
7359 else
7360 /* addr_string has to be used or breakpoint_re_set will delete
7361 me. */
5af949e3
UW
7362 b->addr_string
7363 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 7364 b->filter = filter;
d9b3f62e 7365}
018d34a4 7366
d9b3f62e
PA
7367static void
7368create_breakpoint_sal (struct gdbarch *gdbarch,
7369 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 7370 char *filter, char *cond_string,
d9b3f62e
PA
7371 enum bptype type, enum bpdisp disposition,
7372 int thread, int task, int ignore_count,
c0a91b2b 7373 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e
PA
7374 int enabled, int internal, int display_canonical)
7375{
7376 struct breakpoint *b;
7377 struct cleanup *old_chain;
7378
7379 if (is_tracepoint_type (type))
7380 {
7381 struct tracepoint *t;
7382
7383 t = XCNEW (struct tracepoint);
7384 b = &t->base;
7385 }
7386 else
7387 b = XNEW (struct breakpoint);
7388
7389 old_chain = make_cleanup (xfree, b);
7390
7391 init_breakpoint_sal (b, gdbarch,
7392 sals, addr_string,
f8eba3c6 7393 filter, cond_string,
d9b3f62e
PA
7394 type, disposition,
7395 thread, task, ignore_count,
7396 ops, from_tty,
7397 enabled, internal, display_canonical);
7398 discard_cleanups (old_chain);
7399
3ea46bff 7400 install_breakpoint (internal, b, 0);
018d34a4
VP
7401}
7402
7403/* Add SALS.nelts breakpoints to the breakpoint table. For each
7404 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7405 value. COND_STRING, if not NULL, specified the condition to be
7406 used for all breakpoints. Essentially the only case where
7407 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7408 function. In that case, it's still not possible to specify
7409 separate conditions for different overloaded functions, so
7410 we take just a single condition string.
7411
c3f6f71d 7412 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7413 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7414 array contents). If the function fails (error() is called), the
7415 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7416 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7417
7418static void
8cdf0e15 7419create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 7420 struct linespec_result *canonical,
8cdf0e15
VP
7421 char *cond_string,
7422 enum bptype type, enum bpdisp disposition,
7423 int thread, int task, int ignore_count,
c0a91b2b 7424 const struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7425 int enabled, int internal)
c906108c 7426{
018d34a4 7427 int i;
f8eba3c6 7428 struct linespec_sals *lsal;
cc59ec59 7429
f8eba3c6
TT
7430 if (canonical->pre_expanded)
7431 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
7432
7433 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 7434 {
f8eba3c6
TT
7435 /* Note that 'addr_string' can be NULL in the case of a plain
7436 'break', without arguments. */
7437 char *addr_string = (canonical->addr_string
7438 ? xstrdup (canonical->addr_string)
7439 : NULL);
7440 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
7441 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 7442
f8eba3c6
TT
7443 make_cleanup (xfree, filter_string);
7444 create_breakpoint_sal (gdbarch, lsal->sals,
7445 addr_string,
7446 filter_string,
8cdf0e15 7447 cond_string, type, disposition,
84f4c1fe 7448 thread, task, ignore_count, ops,
56435ebe
TT
7449 from_tty, enabled, internal,
7450 canonical->special_display);
f8eba3c6 7451 discard_cleanups (inner);
c3f6f71d 7452 }
c3f6f71d 7453}
c906108c 7454
9998af43 7455/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7456 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7457 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7458 address strings. ADDRESS points to the end of the SAL.
7459
7460 The array and the line spec strings are allocated on the heap, it is
7461 the caller's responsibility to free them. */
c906108c 7462
b9362cc7 7463static void
c3f6f71d 7464parse_breakpoint_sals (char **address,
58438ac1 7465 struct linespec_result *canonical)
c3f6f71d
JM
7466{
7467 char *addr_start = *address;
cc59ec59 7468
c3f6f71d 7469 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7470 breakpoint. */
c3f6f71d
JM
7471 if ((*address) == NULL
7472 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 7473 {
1bfeeb0f
JL
7474 /* The last displayed codepoint, if it's valid, is our default breakpoint
7475 address. */
7476 if (last_displayed_sal_is_valid ())
c906108c 7477 {
f8eba3c6 7478 struct linespec_sals lsal;
c3f6f71d 7479 struct symtab_and_line sal;
cc59ec59 7480
4a64f543 7481 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 7482 lsal.sals.sals = (struct symtab_and_line *)
c906108c 7483 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
7484
7485 /* Set sal's pspace, pc, symtab, and line to the values
7486 corresponding to the last call to print_frame_info. */
7487 get_last_displayed_sal (&sal);
7488 sal.section = find_pc_overlay (sal.pc);
00903456 7489
4a64f543 7490 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
7491 where PC is the last displayed codepoint's address. So
7492 make sure to set sal.explicit_pc to prevent GDB from
7493 trying to expand the list of sals to include all other
7494 instances with the same symtab and line. */
00903456
JK
7495 sal.explicit_pc = 1;
7496
f8eba3c6
TT
7497 lsal.sals.sals[0] = sal;
7498 lsal.sals.nelts = 1;
7499 lsal.canonical = NULL;
7500
7501 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
7502 }
7503 else
8a3fe4f8 7504 error (_("No default breakpoint address now."));
c906108c
SS
7505 }
7506 else
7507 {
c906108c 7508 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7509 current_source_symtab (which is decode_line_1's default).
7510 This should produce the results we want almost all of the
f8eba3c6
TT
7511 time while leaving default_breakpoint_* alone. */
7512 if (last_displayed_sal_is_valid ())
7513 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
7514 get_last_displayed_symtab (),
7515 get_last_displayed_line (),
7516 canonical, NULL, NULL);
c906108c 7517 else
f8eba3c6
TT
7518 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
7519 (struct symtab *) NULL, 0,
7520 canonical, NULL, NULL);
c906108c 7521 }
c3f6f71d 7522}
c906108c 7523
c906108c 7524
c3f6f71d 7525/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7526 inserted as a breakpoint. If it can't throw an error. */
c906108c 7527
b9362cc7 7528static void
23e7acfb 7529breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7530{
7531 int i;
cc59ec59 7532
c3f6f71d 7533 for (i = 0; i < sals->nelts; i++)
ee53e872 7534 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7535}
7536
7a697b8d
SS
7537/* Fast tracepoints may have restrictions on valid locations. For
7538 instance, a fast tracepoint using a jump instead of a trap will
7539 likely have to overwrite more bytes than a trap would, and so can
7540 only be placed where the instruction is longer than the jump, or a
7541 multi-instruction sequence does not have a jump into the middle of
7542 it, etc. */
7543
7544static void
7545check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7546 struct symtabs_and_lines *sals)
7547{
7548 int i, rslt;
7549 struct symtab_and_line *sal;
7550 char *msg;
7551 struct cleanup *old_chain;
7552
7553 for (i = 0; i < sals->nelts; i++)
7554 {
f8eba3c6
TT
7555 struct gdbarch *sarch;
7556
7a697b8d
SS
7557 sal = &sals->sals[i];
7558
f8eba3c6
TT
7559 sarch = get_sal_arch (*sal);
7560 /* We fall back to GDBARCH if there is no architecture
7561 associated with SAL. */
7562 if (sarch == NULL)
7563 sarch = gdbarch;
7564 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
7565 NULL, &msg);
7566 old_chain = make_cleanup (xfree, msg);
7567
7568 if (!rslt)
7569 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 7570 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
7571
7572 do_cleanups (old_chain);
7573 }
7574}
7575
018d34a4
VP
7576/* Given TOK, a string specification of condition and thread, as
7577 accepted by the 'break' command, extract the condition
7578 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7579 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7580 If no condition is found, *COND_STRING is set to NULL.
7581 If no thread is found, *THREAD is set to -1. */
7582static void
7583find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7584 char **cond_string, int *thread, int *task)
018d34a4
VP
7585{
7586 *cond_string = NULL;
7587 *thread = -1;
7588 while (tok && *tok)
7589 {
7590 char *end_tok;
7591 int toklen;
7592 char *cond_start = NULL;
7593 char *cond_end = NULL;
cc59ec59 7594
e9cafbcc 7595 tok = skip_spaces (tok);
018d34a4 7596
e9cafbcc 7597 end_tok = skip_to_space (tok);
018d34a4
VP
7598
7599 toklen = end_tok - tok;
7600
7601 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7602 {
f7545552
TT
7603 struct expression *expr;
7604
018d34a4 7605 tok = cond_start = end_tok + 1;
f7545552
TT
7606 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7607 xfree (expr);
018d34a4
VP
7608 cond_end = tok;
7609 *cond_string = savestring (cond_start,
7610 cond_end - cond_start);
7611 }
7612 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7613 {
7614 char *tmptok;
7615
7616 tok = end_tok + 1;
7617 tmptok = tok;
7618 *thread = strtol (tok, &tok, 0);
7619 if (tok == tmptok)
7620 error (_("Junk after thread keyword."));
7621 if (!valid_thread_id (*thread))
7622 error (_("Unknown thread %d."), *thread);
7623 }
4a306c9a
JB
7624 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7625 {
7626 char *tmptok;
7627
7628 tok = end_tok + 1;
7629 tmptok = tok;
7630 *task = strtol (tok, &tok, 0);
7631 if (tok == tmptok)
7632 error (_("Junk after task keyword."));
7633 if (!valid_task_id (*task))
b6199126 7634 error (_("Unknown task %d."), *task);
4a306c9a 7635 }
018d34a4
VP
7636 else
7637 error (_("Junk at end of arguments."));
7638 }
7639}
7640
0fb4aa4b
PA
7641/* Decode a static tracepoint marker spec. */
7642
7643static struct symtabs_and_lines
7644decode_static_tracepoint_spec (char **arg_p)
7645{
7646 VEC(static_tracepoint_marker_p) *markers = NULL;
7647 struct symtabs_and_lines sals;
7648 struct symtab_and_line sal;
7649 struct symbol *sym;
7650 struct cleanup *old_chain;
7651 char *p = &(*arg_p)[3];
7652 char *endp;
7653 char *marker_str;
7654 int i;
7655
e9cafbcc 7656 p = skip_spaces (p);
0fb4aa4b 7657
e9cafbcc 7658 endp = skip_to_space (p);
0fb4aa4b
PA
7659
7660 marker_str = savestring (p, endp - p);
7661 old_chain = make_cleanup (xfree, marker_str);
7662
7663 markers = target_static_tracepoint_markers_by_strid (marker_str);
7664 if (VEC_empty(static_tracepoint_marker_p, markers))
7665 error (_("No known static tracepoint marker named %s"), marker_str);
7666
7667 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7668 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7669
7670 for (i = 0; i < sals.nelts; i++)
7671 {
7672 struct static_tracepoint_marker *marker;
7673
7674 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7675
7676 init_sal (&sals.sals[i]);
7677
7678 sals.sals[i] = find_pc_line (marker->address, 0);
7679 sals.sals[i].pc = marker->address;
7680
7681 release_static_tracepoint_marker (marker);
7682 }
7683
7684 do_cleanups (old_chain);
7685
7686 *arg_p = endp;
7687 return sals;
7688}
7689
fd9b8c24
PA
7690/* Set a breakpoint. This function is shared between CLI and MI
7691 functions for setting a breakpoint. This function has two major
7692 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7693 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7694 breakpoint location, address and thread. Otherwise, ARG is just
7695 the location of breakpoint, with condition and thread specified by
7696 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7697 the breakpoint number will be allocated from the internal
7698 breakpoint count. Returns true if any breakpoint was created;
7699 false otherwise. */
0101ce28 7700
8cdf0e15
VP
7701int
7702create_breakpoint (struct gdbarch *gdbarch,
7703 char *arg, char *cond_string, int thread,
7704 int parse_condition_and_thread,
0fb4aa4b 7705 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7706 int ignore_count,
7707 enum auto_boolean pending_break_support,
c0a91b2b 7708 const struct breakpoint_ops *ops,
84f4c1fe 7709 int from_tty, int enabled, int internal)
c3f6f71d 7710{
b78a6381 7711 volatile struct gdb_exception e;
f8eba3c6 7712 char *copy_arg = NULL;
c3f6f71d 7713 char *addr_start = arg;
7efd8fc2 7714 struct linespec_result canonical;
c3f6f71d 7715 struct cleanup *old_chain;
80c99de1 7716 struct cleanup *bkpt_chain = NULL;
05ff989b 7717 int i;
0101ce28 7718 int pending = 0;
4a306c9a 7719 int task = 0;
86b17b60 7720 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7721
348d480f
PA
7722 gdb_assert (ops != NULL);
7723
7efd8fc2 7724 init_linespec_result (&canonical);
c3f6f71d 7725
0fb4aa4b
PA
7726 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7727 {
7728 int i;
f8eba3c6 7729 struct linespec_sals lsal;
0fb4aa4b 7730
f8eba3c6 7731 lsal.sals = decode_static_tracepoint_spec (&arg);
0fb4aa4b
PA
7732
7733 copy_arg = savestring (addr_start, arg - addr_start);
f8eba3c6 7734
d14c4eb7 7735 canonical.addr_string = xstrdup (copy_arg);
f8eba3c6
TT
7736 lsal.canonical = xstrdup (copy_arg);
7737 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
7738
0fb4aa4b
PA
7739 goto done;
7740 }
7741
b78a6381
TT
7742 TRY_CATCH (e, RETURN_MASK_ALL)
7743 {
f8eba3c6 7744 parse_breakpoint_sals (&arg, &canonical);
b78a6381 7745 }
0101ce28
JJ
7746
7747 /* If caller is interested in rc value from parse, set value. */
05ff989b 7748 switch (e.reason)
0101ce28 7749 {
05ff989b 7750 case RETURN_QUIT:
98deb0da 7751 throw_exception (e);
05ff989b
AC
7752 case RETURN_ERROR:
7753 switch (e.error)
0101ce28 7754 {
05ff989b 7755 case NOT_FOUND_ERROR:
0101ce28 7756
05ff989b
AC
7757 /* If pending breakpoint support is turned off, throw
7758 error. */
fa8d40ab
JJ
7759
7760 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7761 throw_exception (e);
7762
7763 exception_print (gdb_stderr, e);
fa8d40ab 7764
05ff989b
AC
7765 /* If pending breakpoint support is auto query and the user
7766 selects no, then simply return the error code. */
059fb39f 7767 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
7768 && !nquery (_("Make %s pending on future shared library load? "),
7769 bptype_string (type_wanted)))
fd9b8c24 7770 return 0;
fa8d40ab 7771
05ff989b
AC
7772 /* At this point, either the user was queried about setting
7773 a pending breakpoint and selected yes, or pending
7774 breakpoint behavior is on and thus a pending breakpoint
7775 is defaulted on behalf of the user. */
f8eba3c6
TT
7776 {
7777 struct linespec_sals lsal;
7778
7779 copy_arg = xstrdup (addr_start);
7780 lsal.canonical = xstrdup (copy_arg);
7781 lsal.sals.nelts = 1;
7782 lsal.sals.sals = XNEW (struct symtab_and_line);
7783 init_sal (&lsal.sals.sals[0]);
7784 pending = 1;
7785 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
7786 }
05ff989b
AC
7787 break;
7788 default:
98deb0da 7789 throw_exception (e);
0101ce28 7790 }
2abae994 7791 break;
05ff989b 7792 default:
f8eba3c6 7793 if (VEC_empty (linespec_sals, canonical.sals))
fd9b8c24 7794 return 0;
0101ce28 7795 }
c3f6f71d 7796
0fb4aa4b
PA
7797 done:
7798
4a64f543 7799 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 7800 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 7801
c3f6f71d
JM
7802 /* ----------------------------- SNIP -----------------------------
7803 Anything added to the cleanup chain beyond this point is assumed
7804 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7805 then the memory is not reclaimed. */
7806 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 7807
c3f6f71d
JM
7808 /* Resolve all line numbers to PC's and verify that the addresses
7809 are ok for the target. */
0101ce28 7810 if (!pending)
f8eba3c6
TT
7811 {
7812 int ix;
7813 struct linespec_sals *iter;
7814
7815 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
7816 breakpoint_sals_to_pc (&iter->sals);
7817 }
c3f6f71d 7818
7a697b8d 7819 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 7820 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
7821 {
7822 int ix;
7823 struct linespec_sals *iter;
7824
7825 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
7826 check_fast_tracepoint_sals (gdbarch, &iter->sals);
7827 }
7a697b8d 7828
c3f6f71d
JM
7829 /* Verify that condition can be parsed, before setting any
7830 breakpoints. Allocate a separate condition expression for each
4a64f543 7831 breakpoint. */
0101ce28 7832 if (!pending)
c3f6f71d 7833 {
f8eba3c6
TT
7834 struct linespec_sals *lsal;
7835
7836 lsal = VEC_index (linespec_sals, canonical.sals, 0);
7837
2f069f6f 7838 if (parse_condition_and_thread)
72b2ff0e
VP
7839 {
7840 /* Here we only parse 'arg' to separate condition
7841 from thread number, so parsing in context of first
7842 sal is OK. When setting the breakpoint we'll
7843 re-parse it in context of each sal. */
7844 cond_string = NULL;
7845 thread = -1;
f8eba3c6 7846 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
4a306c9a 7847 &thread, &task);
72b2ff0e
VP
7848 if (cond_string)
7849 make_cleanup (xfree, cond_string);
7850 }
2f069f6f 7851 else
72b2ff0e
VP
7852 {
7853 /* Create a private copy of condition string. */
7854 if (cond_string)
7855 {
7856 cond_string = xstrdup (cond_string);
7857 make_cleanup (xfree, cond_string);
7858 }
7859 }
0fb4aa4b
PA
7860
7861 /* If the user is creating a static tracepoint by marker id
7862 (strace -m MARKER_ID), then store the sals index, so that
7863 breakpoint_re_set can try to match up which of the newly
7864 found markers corresponds to this one, and, don't try to
7865 expand multiple locations for each sal, given than SALS
7866 already should contain all sals for MARKER_ID. */
7867 if (type_wanted == bp_static_tracepoint
f8eba3c6 7868 && is_marker_spec (copy_arg))
0fb4aa4b
PA
7869 {
7870 int i;
7871
f8eba3c6 7872 for (i = 0; i < lsal->sals.nelts; ++i)
0fb4aa4b
PA
7873 {
7874 struct symtabs_and_lines expanded;
d9b3f62e 7875 struct tracepoint *tp;
0fb4aa4b 7876 struct cleanup *old_chain;
f8eba3c6 7877 char *addr_string;
0fb4aa4b
PA
7878
7879 expanded.nelts = 1;
f8eba3c6
TT
7880 expanded.sals = &lsal->sals.sals[i];
7881
7882 addr_string = xstrdup (canonical.addr_string);
7883 old_chain = make_cleanup (xfree, addr_string);
0fb4aa4b 7884
d9b3f62e
PA
7885 tp = XCNEW (struct tracepoint);
7886 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f8eba3c6 7887 addr_string, NULL,
d9b3f62e
PA
7888 cond_string, type_wanted,
7889 tempflag ? disp_del : disp_donttouch,
7890 thread, task, ignore_count, ops,
7891 from_tty, enabled, internal,
7892 canonical.special_display);
0fb4aa4b
PA
7893 /* Given that its possible to have multiple markers with
7894 the same string id, if the user is creating a static
7895 tracepoint by marker id ("strace -m MARKER_ID"), then
7896 store the sals index, so that breakpoint_re_set can
7897 try to match up which of the newly found markers
7898 corresponds to this one */
7899 tp->static_trace_marker_id_idx = i;
d9b3f62e 7900
3ea46bff 7901 install_breakpoint (internal, &tp->base, 0);
d9b3f62e 7902
f8eba3c6 7903 discard_cleanups (old_chain);
0fb4aa4b
PA
7904 }
7905 }
7906 else
f8eba3c6 7907 create_breakpoints_sal (gdbarch, &canonical, cond_string,
3e43a32a
MS
7908 type_wanted,
7909 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 7910 thread, task, ignore_count, ops, from_tty,
84f4c1fe 7911 enabled, internal);
c906108c 7912 }
0101ce28
JJ
7913 else
7914 {
0101ce28
JJ
7915 struct breakpoint *b;
7916
0101ce28
JJ
7917 make_cleanup (xfree, copy_arg);
7918
bfccc43c
YQ
7919 if (is_tracepoint_type (type_wanted))
7920 {
7921 struct tracepoint *t;
7922
7923 t = XCNEW (struct tracepoint);
7924 b = &t->base;
7925 }
7926 else
7927 b = XNEW (struct breakpoint);
7928
7929 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
7930
f8eba3c6 7931 b->addr_string = copy_arg;
72b2ff0e 7932 b->cond_string = NULL;
0101ce28 7933 b->ignore_count = ignore_count;
0101ce28 7934 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 7935 b->condition_not_parsed = 1;
41447f92 7936 b->enable_state = enabled ? bp_enabled : bp_disabled;
f8eba3c6
TT
7937 if (type_wanted != bp_breakpoint && type_wanted != bp_hardware_breakpoint)
7938 b->pspace = current_program_space;
8bea4e01 7939
bfccc43c 7940 install_breakpoint (internal, b, 0);
0101ce28
JJ
7941 }
7942
f8eba3c6 7943 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 7944 {
3e43a32a
MS
7945 warning (_("Multiple breakpoints were set.\nUse the "
7946 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 7947 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
7948 }
7949
80c99de1
PA
7950 /* That's it. Discard the cleanups for data inserted into the
7951 breakpoint. */
7952 discard_cleanups (bkpt_chain);
7953 /* But cleanup everything else. */
c3f6f71d 7954 do_cleanups (old_chain);
217dc9e2 7955
80c99de1 7956 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 7957 update_global_location_list (1);
fd9b8c24
PA
7958
7959 return 1;
c3f6f71d 7960}
c906108c 7961
348d480f 7962/* Set a breakpoint.
72b2ff0e
VP
7963 ARG is a string describing breakpoint address,
7964 condition, and thread.
7965 FLAG specifies if a breakpoint is hardware on,
7966 and if breakpoint is temporary, using BP_HARDWARE_FLAG
7967 and BP_TEMPFLAG. */
348d480f 7968
98deb0da 7969static void
72b2ff0e 7970break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 7971{
72b2ff0e 7972 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
7973 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
7974 ? bp_hardware_breakpoint
7975 : bp_breakpoint);
c3f6f71d 7976
8cdf0e15
VP
7977 create_breakpoint (get_current_arch (),
7978 arg,
7979 NULL, 0, 1 /* parse arg */,
0fb4aa4b 7980 tempflag, type_wanted,
8cdf0e15
VP
7981 0 /* Ignore count */,
7982 pending_break_support,
348d480f 7983 &bkpt_breakpoint_ops,
8cdf0e15 7984 from_tty,
84f4c1fe
PM
7985 1 /* enabled */,
7986 0 /* internal */);
c906108c
SS
7987}
7988
c906108c
SS
7989/* Helper function for break_command_1 and disassemble_command. */
7990
7991void
fba45db2 7992resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
7993{
7994 CORE_ADDR pc;
7995
7996 if (sal->pc == 0 && sal->symtab != NULL)
7997 {
7998 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 7999 error (_("No line %d in file \"%s\"."),
c906108c
SS
8000 sal->line, sal->symtab->filename);
8001 sal->pc = pc;
6a048695 8002
4a64f543
MS
8003 /* If this SAL corresponds to a breakpoint inserted using a line
8004 number, then skip the function prologue if necessary. */
6a048695 8005 if (sal->explicit_line)
059acae7 8006 skip_prologue_sal (sal);
c906108c
SS
8007 }
8008
8009 if (sal->section == 0 && sal->symtab != NULL)
8010 {
8011 struct blockvector *bv;
c5aa993b
JM
8012 struct block *b;
8013 struct symbol *sym;
c906108c 8014
801e3a5b 8015 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
8016 if (bv != NULL)
8017 {
7f0df278 8018 sym = block_linkage_function (b);
c906108c
SS
8019 if (sym != NULL)
8020 {
8021 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 8022 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
8023 }
8024 else
8025 {
4a64f543
MS
8026 /* It really is worthwhile to have the section, so we'll
8027 just have to look harder. This case can be executed
8028 if we have line numbers but no functions (as can
8029 happen in assembly source). */
c906108c 8030
c5aa993b 8031 struct minimal_symbol *msym;
6c95b8df
PA
8032 struct cleanup *old_chain = save_current_space_and_thread ();
8033
8034 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8035
8036 msym = lookup_minimal_symbol_by_pc (sal->pc);
8037 if (msym)
714835d5 8038 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8039
8040 do_cleanups (old_chain);
c906108c
SS
8041 }
8042 }
8043 }
8044}
8045
8046void
fba45db2 8047break_command (char *arg, int from_tty)
c906108c 8048{
db107f19 8049 break_command_1 (arg, 0, from_tty);
c906108c
SS
8050}
8051
c906108c 8052void
fba45db2 8053tbreak_command (char *arg, int from_tty)
c906108c 8054{
db107f19 8055 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8056}
8057
c906108c 8058static void
fba45db2 8059hbreak_command (char *arg, int from_tty)
c906108c 8060{
db107f19 8061 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8062}
8063
8064static void
fba45db2 8065thbreak_command (char *arg, int from_tty)
c906108c 8066{
db107f19 8067 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8068}
8069
8070static void
fba45db2 8071stop_command (char *arg, int from_tty)
c906108c 8072{
a3f17187 8073 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8074Usage: stop in <function | address>\n\
a3f17187 8075 stop at <line>\n"));
c906108c
SS
8076}
8077
8078static void
fba45db2 8079stopin_command (char *arg, int from_tty)
c906108c
SS
8080{
8081 int badInput = 0;
8082
c5aa993b 8083 if (arg == (char *) NULL)
c906108c
SS
8084 badInput = 1;
8085 else if (*arg != '*')
8086 {
8087 char *argptr = arg;
8088 int hasColon = 0;
8089
4a64f543 8090 /* Look for a ':'. If this is a line number specification, then
53a5351d 8091 say it is bad, otherwise, it should be an address or
4a64f543 8092 function/method name. */
c906108c 8093 while (*argptr && !hasColon)
c5aa993b
JM
8094 {
8095 hasColon = (*argptr == ':');
8096 argptr++;
8097 }
c906108c
SS
8098
8099 if (hasColon)
c5aa993b 8100 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8101 else
c5aa993b 8102 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8103 }
8104
8105 if (badInput)
a3f17187 8106 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8107 else
db107f19 8108 break_command_1 (arg, 0, from_tty);
c906108c
SS
8109}
8110
8111static void
fba45db2 8112stopat_command (char *arg, int from_tty)
c906108c
SS
8113{
8114 int badInput = 0;
8115
c5aa993b 8116 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8117 badInput = 1;
8118 else
8119 {
8120 char *argptr = arg;
8121 int hasColon = 0;
8122
4a64f543
MS
8123 /* Look for a ':'. If there is a '::' then get out, otherwise
8124 it is probably a line number. */
c906108c 8125 while (*argptr && !hasColon)
c5aa993b
JM
8126 {
8127 hasColon = (*argptr == ':');
8128 argptr++;
8129 }
c906108c
SS
8130
8131 if (hasColon)
c5aa993b 8132 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8133 else
c5aa993b 8134 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8135 }
8136
8137 if (badInput)
a3f17187 8138 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8139 else
db107f19 8140 break_command_1 (arg, 0, from_tty);
c906108c
SS
8141}
8142
f1310107
TJB
8143/* Implement the "breakpoint_hit" breakpoint_ops method for
8144 ranged breakpoints. */
8145
8146static int
8147breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8148 struct address_space *aspace,
8149 CORE_ADDR bp_addr)
8150{
8151 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8152 bl->length, aspace, bp_addr);
8153}
8154
8155/* Implement the "resources_needed" breakpoint_ops method for
8156 ranged breakpoints. */
8157
8158static int
8159resources_needed_ranged_breakpoint (const struct bp_location *bl)
8160{
8161 return target_ranged_break_num_registers ();
8162}
8163
8164/* Implement the "print_it" breakpoint_ops method for
8165 ranged breakpoints. */
8166
8167static enum print_stop_action
348d480f 8168print_it_ranged_breakpoint (bpstat bs)
f1310107 8169{
348d480f 8170 struct breakpoint *b = bs->breakpoint_at;
f1310107 8171 struct bp_location *bl = b->loc;
79a45e25 8172 struct ui_out *uiout = current_uiout;
f1310107
TJB
8173
8174 gdb_assert (b->type == bp_hardware_breakpoint);
8175
8176 /* Ranged breakpoints have only one location. */
8177 gdb_assert (bl && bl->next == NULL);
8178
8179 annotate_breakpoint (b->number);
8180 if (b->disposition == disp_del)
8181 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8182 else
8183 ui_out_text (uiout, "\nRanged breakpoint ");
8184 if (ui_out_is_mi_like_p (uiout))
8185 {
8186 ui_out_field_string (uiout, "reason",
8187 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8188 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8189 }
8190 ui_out_field_int (uiout, "bkptno", b->number);
8191 ui_out_text (uiout, ", ");
8192
8193 return PRINT_SRC_AND_LOC;
8194}
8195
8196/* Implement the "print_one" breakpoint_ops method for
8197 ranged breakpoints. */
8198
8199static void
8200print_one_ranged_breakpoint (struct breakpoint *b,
8201 struct bp_location **last_loc)
8202{
8203 struct bp_location *bl = b->loc;
8204 struct value_print_options opts;
79a45e25 8205 struct ui_out *uiout = current_uiout;
f1310107
TJB
8206
8207 /* Ranged breakpoints have only one location. */
8208 gdb_assert (bl && bl->next == NULL);
8209
8210 get_user_print_options (&opts);
8211
8212 if (opts.addressprint)
8213 /* We don't print the address range here, it will be printed later
8214 by print_one_detail_ranged_breakpoint. */
8215 ui_out_field_skip (uiout, "addr");
8216 annotate_field (5);
8217 print_breakpoint_location (b, bl);
8218 *last_loc = bl;
8219}
8220
8221/* Implement the "print_one_detail" breakpoint_ops method for
8222 ranged breakpoints. */
8223
8224static void
8225print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8226 struct ui_out *uiout)
8227{
8228 CORE_ADDR address_start, address_end;
8229 struct bp_location *bl = b->loc;
8230 struct ui_stream *stb = ui_out_stream_new (uiout);
8231 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8232
8233 gdb_assert (bl);
8234
8235 address_start = bl->address;
8236 address_end = address_start + bl->length - 1;
8237
8238 ui_out_text (uiout, "\taddress range: ");
8239 fprintf_unfiltered (stb->stream, "[%s, %s]",
8240 print_core_address (bl->gdbarch, address_start),
8241 print_core_address (bl->gdbarch, address_end));
8242 ui_out_field_stream (uiout, "addr", stb);
8243 ui_out_text (uiout, "\n");
8244
8245 do_cleanups (cleanup);
8246}
8247
8248/* Implement the "print_mention" breakpoint_ops method for
8249 ranged breakpoints. */
8250
8251static void
8252print_mention_ranged_breakpoint (struct breakpoint *b)
8253{
8254 struct bp_location *bl = b->loc;
79a45e25 8255 struct ui_out *uiout = current_uiout;
f1310107
TJB
8256
8257 gdb_assert (bl);
8258 gdb_assert (b->type == bp_hardware_breakpoint);
8259
8260 if (ui_out_is_mi_like_p (uiout))
8261 return;
8262
8263 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8264 b->number, paddress (bl->gdbarch, bl->address),
8265 paddress (bl->gdbarch, bl->address + bl->length - 1));
8266}
8267
8268/* Implement the "print_recreate" breakpoint_ops method for
8269 ranged breakpoints. */
8270
8271static void
8272print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8273{
8274 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8275 b->addr_string_range_end);
d9b3f62e 8276 print_recreate_thread (b, fp);
f1310107
TJB
8277}
8278
8279/* The breakpoint_ops structure to be used in ranged breakpoints. */
8280
2060206e 8281static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
8282
8283/* Find the address where the end of the breakpoint range should be
8284 placed, given the SAL of the end of the range. This is so that if
8285 the user provides a line number, the end of the range is set to the
8286 last instruction of the given line. */
8287
8288static CORE_ADDR
8289find_breakpoint_range_end (struct symtab_and_line sal)
8290{
8291 CORE_ADDR end;
8292
8293 /* If the user provided a PC value, use it. Otherwise,
8294 find the address of the end of the given location. */
8295 if (sal.explicit_pc)
8296 end = sal.pc;
8297 else
8298 {
8299 int ret;
8300 CORE_ADDR start;
8301
8302 ret = find_line_pc_range (sal, &start, &end);
8303 if (!ret)
8304 error (_("Could not find location of the end of the range."));
8305
8306 /* find_line_pc_range returns the start of the next line. */
8307 end--;
8308 }
8309
8310 return end;
8311}
8312
8313/* Implement the "break-range" CLI command. */
8314
8315static void
8316break_range_command (char *arg, int from_tty)
8317{
8318 char *arg_start, *addr_string_start, *addr_string_end;
8319 struct linespec_result canonical_start, canonical_end;
8320 int bp_count, can_use_bp, length;
8321 CORE_ADDR end;
8322 struct breakpoint *b;
8323 struct symtab_and_line sal_start, sal_end;
f1310107 8324 struct cleanup *cleanup_bkpt;
f8eba3c6 8325 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
8326
8327 /* We don't support software ranged breakpoints. */
8328 if (target_ranged_break_num_registers () < 0)
8329 error (_("This target does not support hardware ranged breakpoints."));
8330
8331 bp_count = hw_breakpoint_used_count ();
8332 bp_count += target_ranged_break_num_registers ();
8333 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8334 bp_count, 0);
8335 if (can_use_bp < 0)
8336 error (_("Hardware breakpoints used exceeds limit."));
8337
f8eba3c6 8338 arg = skip_spaces (arg);
f1310107
TJB
8339 if (arg == NULL || arg[0] == '\0')
8340 error(_("No address range specified."));
8341
f1310107
TJB
8342 init_linespec_result (&canonical_start);
8343
f8eba3c6
TT
8344 arg_start = arg;
8345 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 8346
f8eba3c6 8347 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
8348
8349 if (arg[0] != ',')
8350 error (_("Too few arguments."));
f8eba3c6 8351 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 8352 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
8353
8354 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
8355
8356 if (VEC_length (linespec_sals, canonical_start.sals) > 1
8357 || lsal_start->sals.nelts != 1)
f1310107
TJB
8358 error (_("Cannot create a ranged breakpoint with multiple locations."));
8359
f8eba3c6
TT
8360 sal_start = lsal_start->sals.sals[0];
8361 addr_string_start = savestring (arg_start, arg - arg_start);
8362 make_cleanup (xfree, addr_string_start);
f1310107
TJB
8363
8364 arg++; /* Skip the comma. */
f8eba3c6 8365 arg = skip_spaces (arg);
f1310107
TJB
8366
8367 /* Parse the end location. */
8368
f1310107
TJB
8369 init_linespec_result (&canonical_end);
8370 arg_start = arg;
8371
f8eba3c6 8372 /* We call decode_line_full directly here instead of using
f1310107
TJB
8373 parse_breakpoint_sals because we need to specify the start location's
8374 symtab and line as the default symtab and line for the end of the
8375 range. This makes it possible to have ranges like "foo.c:27, +14",
8376 where +14 means 14 lines from the start location. */
f8eba3c6
TT
8377 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
8378 sal_start.symtab, sal_start.line,
8379 &canonical_end, NULL, NULL);
8380
8381 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 8382
f8eba3c6 8383 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 8384 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
8385
8386 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
8387 if (VEC_length (linespec_sals, canonical_end.sals) > 1
8388 || lsal_end->sals.nelts != 1)
f1310107
TJB
8389 error (_("Cannot create a ranged breakpoint with multiple locations."));
8390
f8eba3c6
TT
8391 sal_end = lsal_end->sals.sals[0];
8392 addr_string_end = savestring (arg_start, arg - arg_start);
8393 make_cleanup (xfree, addr_string_end);
f1310107
TJB
8394
8395 end = find_breakpoint_range_end (sal_end);
8396 if (sal_start.pc > end)
177b42fe 8397 error (_("Invalid address range, end precedes start."));
f1310107
TJB
8398
8399 length = end - sal_start.pc + 1;
8400 if (length < 0)
8401 /* Length overflowed. */
8402 error (_("Address range too large."));
8403 else if (length == 1)
8404 {
8405 /* This range is simple enough to be handled by
8406 the `hbreak' command. */
8407 hbreak_command (addr_string_start, 1);
8408
8409 do_cleanups (cleanup_bkpt);
8410
8411 return;
8412 }
8413
8414 /* Now set up the breakpoint. */
8415 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 8416 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
8417 set_breakpoint_count (breakpoint_count + 1);
8418 b->number = breakpoint_count;
8419 b->disposition = disp_donttouch;
f8eba3c6
TT
8420 b->addr_string = xstrdup (addr_string_start);
8421 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
8422 b->loc->length = length;
8423
f8eba3c6 8424 do_cleanups (cleanup_bkpt);
f1310107
TJB
8425
8426 mention (b);
8d3788bd 8427 observer_notify_breakpoint_created (b);
f1310107
TJB
8428 update_global_location_list (1);
8429}
8430
4a64f543
MS
8431/* Return non-zero if EXP is verified as constant. Returned zero
8432 means EXP is variable. Also the constant detection may fail for
8433 some constant expressions and in such case still falsely return
8434 zero. */
65d79d4b
SDJ
8435static int
8436watchpoint_exp_is_const (const struct expression *exp)
8437{
8438 int i = exp->nelts;
8439
8440 while (i > 0)
8441 {
8442 int oplenp, argsp;
8443
8444 /* We are only interested in the descriptor of each element. */
8445 operator_length (exp, i, &oplenp, &argsp);
8446 i -= oplenp;
8447
8448 switch (exp->elts[i].opcode)
8449 {
8450 case BINOP_ADD:
8451 case BINOP_SUB:
8452 case BINOP_MUL:
8453 case BINOP_DIV:
8454 case BINOP_REM:
8455 case BINOP_MOD:
8456 case BINOP_LSH:
8457 case BINOP_RSH:
8458 case BINOP_LOGICAL_AND:
8459 case BINOP_LOGICAL_OR:
8460 case BINOP_BITWISE_AND:
8461 case BINOP_BITWISE_IOR:
8462 case BINOP_BITWISE_XOR:
8463 case BINOP_EQUAL:
8464 case BINOP_NOTEQUAL:
8465 case BINOP_LESS:
8466 case BINOP_GTR:
8467 case BINOP_LEQ:
8468 case BINOP_GEQ:
8469 case BINOP_REPEAT:
8470 case BINOP_COMMA:
8471 case BINOP_EXP:
8472 case BINOP_MIN:
8473 case BINOP_MAX:
8474 case BINOP_INTDIV:
8475 case BINOP_CONCAT:
8476 case BINOP_IN:
8477 case BINOP_RANGE:
8478 case TERNOP_COND:
8479 case TERNOP_SLICE:
8480 case TERNOP_SLICE_COUNT:
8481
8482 case OP_LONG:
8483 case OP_DOUBLE:
8484 case OP_DECFLOAT:
8485 case OP_LAST:
8486 case OP_COMPLEX:
8487 case OP_STRING:
8488 case OP_BITSTRING:
8489 case OP_ARRAY:
8490 case OP_TYPE:
8491 case OP_NAME:
8492 case OP_OBJC_NSSTRING:
8493
8494 case UNOP_NEG:
8495 case UNOP_LOGICAL_NOT:
8496 case UNOP_COMPLEMENT:
8497 case UNOP_ADDR:
8498 case UNOP_HIGH:
4a64f543
MS
8499 /* Unary, binary and ternary operators: We have to check
8500 their operands. If they are constant, then so is the
8501 result of that operation. For instance, if A and B are
8502 determined to be constants, then so is "A + B".
8503
8504 UNOP_IND is one exception to the rule above, because the
8505 value of *ADDR is not necessarily a constant, even when
8506 ADDR is. */
65d79d4b
SDJ
8507 break;
8508
8509 case OP_VAR_VALUE:
8510 /* Check whether the associated symbol is a constant.
4a64f543 8511
65d79d4b 8512 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8513 possible that a buggy compiler could mark a variable as
8514 constant even when it is not, and TYPE_CONST would return
8515 true in this case, while SYMBOL_CLASS wouldn't.
8516
8517 We also have to check for function symbols because they
8518 are always constant. */
65d79d4b
SDJ
8519 {
8520 struct symbol *s = exp->elts[i + 2].symbol;
8521
8522 if (SYMBOL_CLASS (s) != LOC_BLOCK
8523 && SYMBOL_CLASS (s) != LOC_CONST
8524 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8525 return 0;
8526 break;
8527 }
8528
8529 /* The default action is to return 0 because we are using
8530 the optimistic approach here: If we don't know something,
8531 then it is not a constant. */
8532 default:
8533 return 0;
8534 }
8535 }
8536
8537 return 1;
8538}
8539
3a5c3e22
PA
8540/* Implement the "dtor" breakpoint_ops method for watchpoints. */
8541
8542static void
8543dtor_watchpoint (struct breakpoint *self)
8544{
8545 struct watchpoint *w = (struct watchpoint *) self;
8546
8547 xfree (w->cond_exp);
8548 xfree (w->exp);
8549 xfree (w->exp_string);
8550 xfree (w->exp_string_reparse);
8551 value_free (w->val);
8552
8553 base_breakpoint_ops.dtor (self);
8554}
8555
348d480f
PA
8556/* Implement the "re_set" breakpoint_ops method for watchpoints. */
8557
8558static void
8559re_set_watchpoint (struct breakpoint *b)
8560{
3a5c3e22
PA
8561 struct watchpoint *w = (struct watchpoint *) b;
8562
348d480f
PA
8563 /* Watchpoint can be either on expression using entirely global
8564 variables, or it can be on local variables.
8565
8566 Watchpoints of the first kind are never auto-deleted, and even
8567 persist across program restarts. Since they can use variables
8568 from shared libraries, we need to reparse expression as libraries
8569 are loaded and unloaded.
8570
8571 Watchpoints on local variables can also change meaning as result
8572 of solib event. For example, if a watchpoint uses both a local
8573 and a global variables in expression, it's a local watchpoint,
8574 but unloading of a shared library will make the expression
8575 invalid. This is not a very common use case, but we still
8576 re-evaluate expression, to avoid surprises to the user.
8577
8578 Note that for local watchpoints, we re-evaluate it only if
8579 watchpoints frame id is still valid. If it's not, it means the
8580 watchpoint is out of scope and will be deleted soon. In fact,
8581 I'm not sure we'll ever be called in this case.
8582
8583 If a local watchpoint's frame id is still valid, then
3a5c3e22 8584 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 8585
3a5c3e22
PA
8586 Don't do anything about disabled watchpoints, since they will be
8587 reevaluated again when enabled. */
8588 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
8589}
8590
77b06cd7
TJB
8591/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8592
8593static int
8594insert_watchpoint (struct bp_location *bl)
8595{
3a5c3e22
PA
8596 struct watchpoint *w = (struct watchpoint *) bl->owner;
8597 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8598
8599 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8600 w->cond_exp);
77b06cd7
TJB
8601}
8602
8603/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8604
8605static int
8606remove_watchpoint (struct bp_location *bl)
8607{
3a5c3e22
PA
8608 struct watchpoint *w = (struct watchpoint *) bl->owner;
8609 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8610
8611 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8612 w->cond_exp);
e09342b5
TJB
8613}
8614
e09342b5 8615static int
348d480f
PA
8616breakpoint_hit_watchpoint (const struct bp_location *bl,
8617 struct address_space *aspace, CORE_ADDR bp_addr)
e09342b5 8618{
348d480f 8619 struct breakpoint *b = bl->owner;
3a5c3e22 8620 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 8621
348d480f
PA
8622 /* Continuable hardware watchpoints are treated as non-existent if the
8623 reason we stopped wasn't a hardware watchpoint (we didn't stop on
8624 some data address). Otherwise gdb won't stop on a break instruction
8625 in the code (not from a breakpoint) when a hardware watchpoint has
8626 been defined. Also skip watchpoints which we know did not trigger
8627 (did not match the data address). */
8628 if (is_hardware_watchpoint (b)
3a5c3e22 8629 && w->watchpoint_triggered == watch_triggered_no)
348d480f 8630 return 0;
9c06b0b4 8631
348d480f 8632 return 1;
9c06b0b4
TJB
8633}
8634
348d480f
PA
8635static void
8636check_status_watchpoint (bpstat bs)
9c06b0b4 8637{
348d480f 8638 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 8639
348d480f 8640 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
8641}
8642
8643/* Implement the "resources_needed" breakpoint_ops method for
348d480f 8644 hardware watchpoints. */
9c06b0b4
TJB
8645
8646static int
348d480f 8647resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 8648{
3a5c3e22
PA
8649 struct watchpoint *w = (struct watchpoint *) bl->owner;
8650 int length = w->exact? 1 : bl->length;
348d480f
PA
8651
8652 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
8653}
8654
8655/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 8656 hardware watchpoints. */
9c06b0b4
TJB
8657
8658static int
348d480f 8659works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 8660{
efa80663
PA
8661 /* Read and access watchpoints only work with hardware support. */
8662 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
8663}
8664
9c06b0b4 8665static enum print_stop_action
348d480f 8666print_it_watchpoint (bpstat bs)
9c06b0b4 8667{
348d480f
PA
8668 struct cleanup *old_chain;
8669 struct breakpoint *b;
8670 const struct bp_location *bl;
8671 struct ui_stream *stb;
8672 enum print_stop_action result;
3a5c3e22 8673 struct watchpoint *w;
79a45e25 8674 struct ui_out *uiout = current_uiout;
348d480f
PA
8675
8676 gdb_assert (bs->bp_location_at != NULL);
8677
8678 bl = bs->bp_location_at;
8679 b = bs->breakpoint_at;
3a5c3e22 8680 w = (struct watchpoint *) b;
348d480f
PA
8681
8682 stb = ui_out_stream_new (uiout);
8683 old_chain = make_cleanup_ui_out_stream_delete (stb);
9c06b0b4
TJB
8684
8685 switch (b->type)
8686 {
348d480f 8687 case bp_watchpoint:
9c06b0b4
TJB
8688 case bp_hardware_watchpoint:
8689 annotate_watchpoint (b->number);
8690 if (ui_out_is_mi_like_p (uiout))
8691 ui_out_field_string
8692 (uiout, "reason",
8693 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
8694 mention (b);
8695 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8696 ui_out_text (uiout, "\nOld value = ");
8697 watchpoint_value_print (bs->old_val, stb->stream);
8698 ui_out_field_stream (uiout, "old", stb);
8699 ui_out_text (uiout, "\nNew value = ");
3a5c3e22 8700 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8701 ui_out_field_stream (uiout, "new", stb);
8702 ui_out_text (uiout, "\n");
8703 /* More than one watchpoint may have been triggered. */
8704 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8705 break;
8706
8707 case bp_read_watchpoint:
8708 if (ui_out_is_mi_like_p (uiout))
8709 ui_out_field_string
8710 (uiout, "reason",
8711 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
8712 mention (b);
8713 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8714 ui_out_text (uiout, "\nValue = ");
3a5c3e22 8715 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8716 ui_out_field_stream (uiout, "value", stb);
8717 ui_out_text (uiout, "\n");
8718 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8719 break;
8720
8721 case bp_access_watchpoint:
348d480f
PA
8722 if (bs->old_val != NULL)
8723 {
8724 annotate_watchpoint (b->number);
8725 if (ui_out_is_mi_like_p (uiout))
8726 ui_out_field_string
8727 (uiout, "reason",
8728 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8729 mention (b);
8730 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8731 ui_out_text (uiout, "\nOld value = ");
8732 watchpoint_value_print (bs->old_val, stb->stream);
8733 ui_out_field_stream (uiout, "old", stb);
8734 ui_out_text (uiout, "\nNew value = ");
8735 }
8736 else
8737 {
8738 mention (b);
8739 if (ui_out_is_mi_like_p (uiout))
8740 ui_out_field_string
8741 (uiout, "reason",
8742 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8743 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8744 ui_out_text (uiout, "\nValue = ");
8745 }
3a5c3e22 8746 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8747 ui_out_field_stream (uiout, "new", stb);
8748 ui_out_text (uiout, "\n");
8749 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8750 break;
8751 default:
348d480f 8752 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8753 }
8754
348d480f
PA
8755 do_cleanups (old_chain);
8756 return result;
8757}
8758
8759/* Implement the "print_mention" breakpoint_ops method for hardware
8760 watchpoints. */
8761
8762static void
8763print_mention_watchpoint (struct breakpoint *b)
8764{
8765 struct cleanup *ui_out_chain;
3a5c3e22 8766 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 8767 struct ui_out *uiout = current_uiout;
348d480f
PA
8768
8769 switch (b->type)
8770 {
8771 case bp_watchpoint:
8772 ui_out_text (uiout, "Watchpoint ");
8773 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8774 break;
8775 case bp_hardware_watchpoint:
8776 ui_out_text (uiout, "Hardware watchpoint ");
8777 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8778 break;
8779 case bp_read_watchpoint:
8780 ui_out_text (uiout, "Hardware read watchpoint ");
8781 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8782 break;
8783 case bp_access_watchpoint:
8784 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
8785 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8786 break;
8787 default:
8788 internal_error (__FILE__, __LINE__,
8789 _("Invalid hardware watchpoint type."));
8790 }
8791
8792 ui_out_field_int (uiout, "number", b->number);
8793 ui_out_text (uiout, ": ");
3a5c3e22 8794 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
8795 do_cleanups (ui_out_chain);
8796}
8797
8798/* Implement the "print_recreate" breakpoint_ops method for
8799 watchpoints. */
8800
8801static void
8802print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
8803{
3a5c3e22
PA
8804 struct watchpoint *w = (struct watchpoint *) b;
8805
348d480f
PA
8806 switch (b->type)
8807 {
8808 case bp_watchpoint:
8809 case bp_hardware_watchpoint:
8810 fprintf_unfiltered (fp, "watch");
8811 break;
8812 case bp_read_watchpoint:
8813 fprintf_unfiltered (fp, "rwatch");
8814 break;
8815 case bp_access_watchpoint:
8816 fprintf_unfiltered (fp, "awatch");
8817 break;
8818 default:
8819 internal_error (__FILE__, __LINE__,
8820 _("Invalid watchpoint type."));
8821 }
8822
3a5c3e22 8823 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 8824 print_recreate_thread (b, fp);
348d480f
PA
8825}
8826
8827/* The breakpoint_ops structure to be used in hardware watchpoints. */
8828
2060206e 8829static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
8830
8831/* Implement the "insert" breakpoint_ops method for
8832 masked hardware watchpoints. */
8833
8834static int
8835insert_masked_watchpoint (struct bp_location *bl)
8836{
3a5c3e22
PA
8837 struct watchpoint *w = (struct watchpoint *) bl->owner;
8838
8839 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8840 bl->watchpoint_type);
8841}
8842
8843/* Implement the "remove" breakpoint_ops method for
8844 masked hardware watchpoints. */
8845
8846static int
8847remove_masked_watchpoint (struct bp_location *bl)
8848{
3a5c3e22
PA
8849 struct watchpoint *w = (struct watchpoint *) bl->owner;
8850
8851 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8852 bl->watchpoint_type);
8853}
8854
8855/* Implement the "resources_needed" breakpoint_ops method for
8856 masked hardware watchpoints. */
8857
8858static int
8859resources_needed_masked_watchpoint (const struct bp_location *bl)
8860{
3a5c3e22
PA
8861 struct watchpoint *w = (struct watchpoint *) bl->owner;
8862
8863 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
8864}
8865
8866/* Implement the "works_in_software_mode" breakpoint_ops method for
8867 masked hardware watchpoints. */
8868
8869static int
8870works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
8871{
8872 return 0;
8873}
8874
8875/* Implement the "print_it" breakpoint_ops method for
8876 masked hardware watchpoints. */
8877
8878static enum print_stop_action
8879print_it_masked_watchpoint (bpstat bs)
8880{
8881 struct breakpoint *b = bs->breakpoint_at;
79a45e25 8882 struct ui_out *uiout = current_uiout;
348d480f
PA
8883
8884 /* Masked watchpoints have only one location. */
8885 gdb_assert (b->loc && b->loc->next == NULL);
8886
8887 switch (b->type)
8888 {
8889 case bp_hardware_watchpoint:
8890 annotate_watchpoint (b->number);
8891 if (ui_out_is_mi_like_p (uiout))
8892 ui_out_field_string
8893 (uiout, "reason",
8894 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
8895 break;
8896
8897 case bp_read_watchpoint:
8898 if (ui_out_is_mi_like_p (uiout))
8899 ui_out_field_string
8900 (uiout, "reason",
8901 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
8902 break;
8903
8904 case bp_access_watchpoint:
8905 if (ui_out_is_mi_like_p (uiout))
8906 ui_out_field_string
8907 (uiout, "reason",
8908 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8909 break;
8910 default:
8911 internal_error (__FILE__, __LINE__,
8912 _("Invalid hardware watchpoint type."));
8913 }
8914
8915 mention (b);
9c06b0b4
TJB
8916 ui_out_text (uiout, _("\n\
8917Check the underlying instruction at PC for the memory\n\
8918address and value which triggered this watchpoint.\n"));
8919 ui_out_text (uiout, "\n");
8920
8921 /* More than one watchpoint may have been triggered. */
8922 return PRINT_UNKNOWN;
8923}
8924
8925/* Implement the "print_one_detail" breakpoint_ops method for
8926 masked hardware watchpoints. */
8927
8928static void
8929print_one_detail_masked_watchpoint (const struct breakpoint *b,
8930 struct ui_out *uiout)
8931{
3a5c3e22
PA
8932 struct watchpoint *w = (struct watchpoint *) b;
8933
9c06b0b4
TJB
8934 /* Masked watchpoints have only one location. */
8935 gdb_assert (b->loc && b->loc->next == NULL);
8936
8937 ui_out_text (uiout, "\tmask ");
3a5c3e22 8938 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
8939 ui_out_text (uiout, "\n");
8940}
8941
8942/* Implement the "print_mention" breakpoint_ops method for
8943 masked hardware watchpoints. */
8944
8945static void
8946print_mention_masked_watchpoint (struct breakpoint *b)
8947{
3a5c3e22 8948 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 8949 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
8950 struct cleanup *ui_out_chain;
8951
8952 switch (b->type)
8953 {
8954 case bp_hardware_watchpoint:
8955 ui_out_text (uiout, "Masked hardware watchpoint ");
8956 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8957 break;
8958 case bp_read_watchpoint:
8959 ui_out_text (uiout, "Masked hardware read watchpoint ");
8960 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8961 break;
8962 case bp_access_watchpoint:
8963 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
8964 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8965 break;
8966 default:
8967 internal_error (__FILE__, __LINE__,
8968 _("Invalid hardware watchpoint type."));
8969 }
8970
8971 ui_out_field_int (uiout, "number", b->number);
8972 ui_out_text (uiout, ": ");
3a5c3e22 8973 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
8974 do_cleanups (ui_out_chain);
8975}
8976
8977/* Implement the "print_recreate" breakpoint_ops method for
8978 masked hardware watchpoints. */
8979
8980static void
8981print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
8982{
3a5c3e22 8983 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
8984 char tmp[40];
8985
8986 switch (b->type)
8987 {
8988 case bp_hardware_watchpoint:
8989 fprintf_unfiltered (fp, "watch");
8990 break;
8991 case bp_read_watchpoint:
8992 fprintf_unfiltered (fp, "rwatch");
8993 break;
8994 case bp_access_watchpoint:
8995 fprintf_unfiltered (fp, "awatch");
8996 break;
8997 default:
8998 internal_error (__FILE__, __LINE__,
8999 _("Invalid hardware watchpoint type."));
9000 }
9001
3a5c3e22
PA
9002 sprintf_vma (tmp, w->hw_wp_mask);
9003 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 9004 print_recreate_thread (b, fp);
9c06b0b4
TJB
9005}
9006
9007/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
9008
2060206e 9009static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
9010
9011/* Tell whether the given watchpoint is a masked hardware watchpoint. */
9012
9013static int
9014is_masked_watchpoint (const struct breakpoint *b)
9015{
9016 return b->ops == &masked_watchpoint_breakpoint_ops;
9017}
9018
53a5351d
JM
9019/* accessflag: hw_write: watch write,
9020 hw_read: watch read,
9021 hw_access: watch access (read or write) */
c906108c 9022static void
84f4c1fe
PM
9023watch_command_1 (char *arg, int accessflag, int from_tty,
9024 int just_location, int internal)
c906108c 9025{
a9634178 9026 volatile struct gdb_exception e;
d983da9c 9027 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 9028 struct expression *exp;
60e1c644 9029 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 9030 struct value *val, *mark, *result;
c906108c 9031 struct frame_info *frame;
c906108c
SS
9032 char *exp_start = NULL;
9033 char *exp_end = NULL;
9c06b0b4
TJB
9034 char *tok, *end_tok;
9035 int toklen = -1;
c906108c
SS
9036 char *cond_start = NULL;
9037 char *cond_end = NULL;
c906108c 9038 enum bptype bp_type;
37e4754d 9039 int thread = -1;
0cf6dd15 9040 int pc = 0;
9c06b0b4
TJB
9041 /* Flag to indicate whether we are going to use masks for
9042 the hardware watchpoint. */
9043 int use_mask = 0;
9044 CORE_ADDR mask = 0;
3a5c3e22 9045 struct watchpoint *w;
c906108c 9046
37e4754d
LM
9047 /* Make sure that we actually have parameters to parse. */
9048 if (arg != NULL && arg[0] != '\0')
9049 {
9c06b0b4 9050 char *value_start;
37e4754d 9051
9c06b0b4
TJB
9052 /* Look for "parameter value" pairs at the end
9053 of the arguments string. */
9054 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
9055 {
9056 /* Skip whitespace at the end of the argument list. */
9057 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9058 tok--;
9059
9060 /* Find the beginning of the last token.
9061 This is the value of the parameter. */
9062 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9063 tok--;
9064 value_start = tok + 1;
9065
9066 /* Skip whitespace. */
9067 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9068 tok--;
9069
9070 end_tok = tok;
9071
9072 /* Find the beginning of the second to last token.
9073 This is the parameter itself. */
9074 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9075 tok--;
9076 tok++;
9077 toklen = end_tok - tok + 1;
9078
9079 if (toklen == 6 && !strncmp (tok, "thread", 6))
9080 {
9081 /* At this point we've found a "thread" token, which means
9082 the user is trying to set a watchpoint that triggers
9083 only in a specific thread. */
9084 char *endp;
37e4754d 9085
9c06b0b4
TJB
9086 if (thread != -1)
9087 error(_("You can specify only one thread."));
37e4754d 9088
9c06b0b4
TJB
9089 /* Extract the thread ID from the next token. */
9090 thread = strtol (value_start, &endp, 0);
37e4754d 9091
9c06b0b4
TJB
9092 /* Check if the user provided a valid numeric value for the
9093 thread ID. */
9094 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9095 error (_("Invalid thread ID specification %s."), value_start);
9096
9097 /* Check if the thread actually exists. */
9098 if (!valid_thread_id (thread))
9099 error (_("Unknown thread %d."), thread);
9100 }
9101 else if (toklen == 4 && !strncmp (tok, "mask", 4))
9102 {
9103 /* We've found a "mask" token, which means the user wants to
9104 create a hardware watchpoint that is going to have the mask
9105 facility. */
9106 struct value *mask_value, *mark;
37e4754d 9107
9c06b0b4
TJB
9108 if (use_mask)
9109 error(_("You can specify only one mask."));
37e4754d 9110
9c06b0b4 9111 use_mask = just_location = 1;
37e4754d 9112
9c06b0b4
TJB
9113 mark = value_mark ();
9114 mask_value = parse_to_comma_and_eval (&value_start);
9115 mask = value_as_address (mask_value);
9116 value_free_to_mark (mark);
9117 }
9118 else
9119 /* We didn't recognize what we found. We should stop here. */
9120 break;
37e4754d 9121
9c06b0b4
TJB
9122 /* Truncate the string and get rid of the "parameter value" pair before
9123 the arguments string is parsed by the parse_exp_1 function. */
9124 *tok = '\0';
9125 }
37e4754d
LM
9126 }
9127
9128 /* Parse the rest of the arguments. */
c906108c
SS
9129 innermost_block = NULL;
9130 exp_start = arg;
9131 exp = parse_exp_1 (&arg, 0, 0);
9132 exp_end = arg;
fa8a61dc
TT
9133 /* Remove trailing whitespace from the expression before saving it.
9134 This makes the eventual display of the expression string a bit
9135 prettier. */
9136 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9137 --exp_end;
9138
65d79d4b
SDJ
9139 /* Checking if the expression is not constant. */
9140 if (watchpoint_exp_is_const (exp))
9141 {
9142 int len;
9143
9144 len = exp_end - exp_start;
9145 while (len > 0 && isspace (exp_start[len - 1]))
9146 len--;
9147 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9148 }
9149
c906108c
SS
9150 exp_valid_block = innermost_block;
9151 mark = value_mark ();
a1442452 9152 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
9153
9154 if (just_location)
9155 {
9c06b0b4
TJB
9156 int ret;
9157
06a64a0b 9158 exp_valid_block = NULL;
a1442452 9159 val = value_addr (result);
06a64a0b
TT
9160 release_value (val);
9161 value_free_to_mark (mark);
9c06b0b4
TJB
9162
9163 if (use_mask)
9164 {
9165 ret = target_masked_watch_num_registers (value_as_address (val),
9166 mask);
9167 if (ret == -1)
9168 error (_("This target does not support masked watchpoints."));
9169 else if (ret == -2)
9170 error (_("Invalid mask or memory region."));
9171 }
06a64a0b
TT
9172 }
9173 else if (val != NULL)
fa4727a6 9174 release_value (val);
c906108c 9175
e9cafbcc
TT
9176 tok = skip_spaces (arg);
9177 end_tok = skip_to_space (tok);
c906108c
SS
9178
9179 toklen = end_tok - tok;
9180 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9181 {
2d134ed3
PA
9182 struct expression *cond;
9183
60e1c644 9184 innermost_block = NULL;
c906108c
SS
9185 tok = cond_start = end_tok + 1;
9186 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
9187
9188 /* The watchpoint expression may not be local, but the condition
9189 may still be. E.g.: `watch global if local > 0'. */
9190 cond_exp_valid_block = innermost_block;
9191
2d134ed3 9192 xfree (cond);
c906108c
SS
9193 cond_end = tok;
9194 }
9195 if (*tok)
8a3fe4f8 9196 error (_("Junk at end of command."));
c906108c 9197
53a5351d 9198 if (accessflag == hw_read)
c5aa993b 9199 bp_type = bp_read_watchpoint;
53a5351d 9200 else if (accessflag == hw_access)
c5aa993b
JM
9201 bp_type = bp_access_watchpoint;
9202 else
9203 bp_type = bp_hardware_watchpoint;
c906108c 9204
d983da9c 9205 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
9206
9207 /* If the expression is "local", then set up a "watchpoint scope"
9208 breakpoint at the point where we've left the scope of the watchpoint
9209 expression. Create the scope breakpoint before the watchpoint, so
9210 that we will encounter it first in bpstat_stop_status. */
60e1c644 9211 if (exp_valid_block && frame)
d983da9c 9212 {
edb3359d
DJ
9213 if (frame_id_p (frame_unwind_caller_id (frame)))
9214 {
9215 scope_breakpoint
a6d9a66e
UW
9216 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
9217 frame_unwind_caller_pc (frame),
06edf0c0
PA
9218 bp_watchpoint_scope,
9219 &momentary_breakpoint_ops);
d983da9c 9220
edb3359d 9221 scope_breakpoint->enable_state = bp_enabled;
d983da9c 9222
edb3359d
DJ
9223 /* Automatically delete the breakpoint when it hits. */
9224 scope_breakpoint->disposition = disp_del;
d983da9c 9225
edb3359d
DJ
9226 /* Only break in the proper frame (help with recursion). */
9227 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 9228
edb3359d 9229 /* Set the address at which we will stop. */
a6d9a66e
UW
9230 scope_breakpoint->loc->gdbarch
9231 = frame_unwind_caller_arch (frame);
edb3359d
DJ
9232 scope_breakpoint->loc->requested_address
9233 = frame_unwind_caller_pc (frame);
9234 scope_breakpoint->loc->address
a6d9a66e
UW
9235 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9236 scope_breakpoint->loc->requested_address,
edb3359d
DJ
9237 scope_breakpoint->type);
9238 }
d983da9c
DJ
9239 }
9240
c906108c 9241 /* Now set up the breakpoint. */
3a5c3e22
PA
9242
9243 w = XCNEW (struct watchpoint);
9244 b = &w->base;
348d480f 9245 if (use_mask)
3a5c3e22
PA
9246 init_raw_breakpoint_without_location (b, NULL, bp_type,
9247 &masked_watchpoint_breakpoint_ops);
348d480f 9248 else
3a5c3e22
PA
9249 init_raw_breakpoint_without_location (b, NULL, bp_type,
9250 &watchpoint_breakpoint_ops);
37e4754d 9251 b->thread = thread;
b5de0fa7 9252 b->disposition = disp_donttouch;
348d480f 9253 b->pspace = current_program_space;
3a5c3e22
PA
9254 w->exp = exp;
9255 w->exp_valid_block = exp_valid_block;
9256 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
9257 if (just_location)
9258 {
9259 struct type *t = value_type (val);
9260 CORE_ADDR addr = value_as_address (val);
9261 char *name;
9262
9263 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9264 name = type_to_string (t);
9265
3a5c3e22 9266 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 9267 core_addr_to_string (addr));
06a64a0b
TT
9268 xfree (name);
9269
3a5c3e22 9270 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
9271 (int) (exp_end - exp_start), exp_start);
9272
06a64a0b
TT
9273 /* The above expression is in C. */
9274 b->language = language_c;
9275 }
9276 else
3a5c3e22 9277 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
9278
9279 if (use_mask)
9280 {
3a5c3e22 9281 w->hw_wp_mask = mask;
9c06b0b4
TJB
9282 }
9283 else
9284 {
3a5c3e22
PA
9285 w->val = val;
9286 w->val_valid = 1;
9c06b0b4 9287 }
77b06cd7 9288
c906108c
SS
9289 if (cond_start)
9290 b->cond_string = savestring (cond_start, cond_end - cond_start);
9291 else
9292 b->cond_string = 0;
c5aa993b 9293
c906108c 9294 if (frame)
f6bc2008 9295 {
3a5c3e22
PA
9296 w->watchpoint_frame = get_frame_id (frame);
9297 w->watchpoint_thread = inferior_ptid;
f6bc2008 9298 }
c906108c 9299 else
f6bc2008 9300 {
3a5c3e22
PA
9301 w->watchpoint_frame = null_frame_id;
9302 w->watchpoint_thread = null_ptid;
f6bc2008 9303 }
c906108c 9304
d983da9c 9305 if (scope_breakpoint != NULL)
c906108c 9306 {
d983da9c
DJ
9307 /* The scope breakpoint is related to the watchpoint. We will
9308 need to act on them together. */
9309 b->related_breakpoint = scope_breakpoint;
9310 scope_breakpoint->related_breakpoint = b;
c906108c 9311 }
d983da9c 9312
06a64a0b
TT
9313 if (!just_location)
9314 value_free_to_mark (mark);
2d134ed3 9315
a9634178
TJB
9316 TRY_CATCH (e, RETURN_MASK_ALL)
9317 {
9318 /* Finally update the new watchpoint. This creates the locations
9319 that should be inserted. */
3a5c3e22 9320 update_watchpoint (w, 1);
a9634178
TJB
9321 }
9322 if (e.reason < 0)
9323 {
9324 delete_breakpoint (b);
9325 throw_exception (e);
9326 }
9327
3ea46bff 9328 install_breakpoint (internal, b, 1);
c906108c
SS
9329}
9330
e09342b5 9331/* Return count of debug registers needed to watch the given expression.
e09342b5 9332 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9333
c906108c 9334static int
a9634178 9335can_use_hardware_watchpoint (struct value *v)
c906108c
SS
9336{
9337 int found_memory_cnt = 0;
2e70b7b9 9338 struct value *head = v;
c906108c
SS
9339
9340 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9341 if (!can_use_hw_watchpoints)
c906108c 9342 return 0;
c5aa993b 9343
5c44784c
JM
9344 /* Make sure that the value of the expression depends only upon
9345 memory contents, and values computed from them within GDB. If we
9346 find any register references or function calls, we can't use a
9347 hardware watchpoint.
9348
9349 The idea here is that evaluating an expression generates a series
9350 of values, one holding the value of every subexpression. (The
9351 expression a*b+c has five subexpressions: a, b, a*b, c, and
9352 a*b+c.) GDB's values hold almost enough information to establish
9353 the criteria given above --- they identify memory lvalues,
9354 register lvalues, computed values, etcetera. So we can evaluate
9355 the expression, and then scan the chain of values that leaves
9356 behind to decide whether we can detect any possible change to the
9357 expression's final value using only hardware watchpoints.
9358
9359 However, I don't think that the values returned by inferior
9360 function calls are special in any way. So this function may not
9361 notice that an expression involving an inferior function call
9362 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9363 for (; v; v = value_next (v))
c906108c 9364 {
5c44784c 9365 if (VALUE_LVAL (v) == lval_memory)
c906108c 9366 {
8464be76
DJ
9367 if (v != head && value_lazy (v))
9368 /* A lazy memory lvalue in the chain is one that GDB never
9369 needed to fetch; we either just used its address (e.g.,
9370 `a' in `a.b') or we never needed it at all (e.g., `a'
9371 in `a,b'). This doesn't apply to HEAD; if that is
9372 lazy then it was not readable, but watch it anyway. */
5c44784c 9373 ;
53a5351d 9374 else
5c44784c
JM
9375 {
9376 /* Ahh, memory we actually used! Check if we can cover
9377 it with hardware watchpoints. */
df407dfe 9378 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9379
9380 /* We only watch structs and arrays if user asked for it
9381 explicitly, never if they just happen to appear in a
9382 middle of some value chain. */
9383 if (v == head
9384 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9385 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9386 {
42ae5230 9387 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9388 int len;
9389 int num_regs;
9390
a9634178 9391 len = (target_exact_watchpoints
e09342b5
TJB
9392 && is_scalar_type_recursive (vtype))?
9393 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9394
e09342b5
TJB
9395 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9396 if (!num_regs)
2e70b7b9
MS
9397 return 0;
9398 else
e09342b5 9399 found_memory_cnt += num_regs;
2e70b7b9 9400 }
5c44784c 9401 }
c5aa993b 9402 }
5086187c
AC
9403 else if (VALUE_LVAL (v) != not_lval
9404 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9405 return 0; /* These are values from the history (e.g., $1). */
5086187c 9406 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9407 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9408 }
9409
9410 /* The expression itself looks suitable for using a hardware
9411 watchpoint, but give the target machine a chance to reject it. */
9412 return found_memory_cnt;
9413}
9414
8b93c638 9415void
84f4c1fe 9416watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9417{
84f4c1fe 9418 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9419}
9420
9421/* A helper function that looks for an argument at the start of a
9422 string. The argument must also either be at the end of the string,
9423 or be followed by whitespace. Returns 1 if it finds the argument,
9424 0 otherwise. If the argument is found, it updates *STR. */
9425
9426static int
9427check_for_argument (char **str, char *arg, int arg_len)
9428{
9429 if (strncmp (*str, arg, arg_len) == 0
9430 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9431 {
9432 *str += arg_len;
9433 return 1;
9434 }
9435 return 0;
9436}
9437
9438/* A helper function that looks for the "-location" argument and then
9439 calls watch_command_1. */
9440
9441static void
9442watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9443{
9444 int just_location = 0;
9445
9446 if (arg
9447 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9448 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9449 {
e9cafbcc 9450 arg = skip_spaces (arg);
06a64a0b
TT
9451 just_location = 1;
9452 }
9453
84f4c1fe 9454 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9455}
8926118c 9456
c5aa993b 9457static void
fba45db2 9458watch_command (char *arg, int from_tty)
c906108c 9459{
06a64a0b 9460 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9461}
9462
8b93c638 9463void
84f4c1fe 9464rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9465{
84f4c1fe 9466 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9467}
8926118c 9468
c5aa993b 9469static void
fba45db2 9470rwatch_command (char *arg, int from_tty)
c906108c 9471{
06a64a0b 9472 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9473}
9474
8b93c638 9475void
84f4c1fe 9476awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9477{
84f4c1fe 9478 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9479}
8926118c 9480
c5aa993b 9481static void
fba45db2 9482awatch_command (char *arg, int from_tty)
c906108c 9483{
06a64a0b 9484 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9485}
c906108c 9486\f
c5aa993b 9487
43ff13b4 9488/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9489 because it uses the mechanisms of breakpoints. */
9490
bfec99b2
PA
9491struct until_break_command_continuation_args
9492{
9493 struct breakpoint *breakpoint;
9494 struct breakpoint *breakpoint2;
186c406b 9495 int thread_num;
bfec99b2
PA
9496};
9497
43ff13b4 9498/* This function is called by fetch_inferior_event via the
4a64f543 9499 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9500 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9501 command. */
c2c6d25f 9502static void
fa4cd53f 9503until_break_command_continuation (void *arg, int err)
43ff13b4 9504{
bfec99b2
PA
9505 struct until_break_command_continuation_args *a = arg;
9506
9507 delete_breakpoint (a->breakpoint);
9508 if (a->breakpoint2)
9509 delete_breakpoint (a->breakpoint2);
186c406b 9510 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9511}
9512
c906108c 9513void
ae66c1fc 9514until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9515{
9516 struct symtabs_and_lines sals;
9517 struct symtab_and_line sal;
206415a3 9518 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9519 struct breakpoint *breakpoint;
f107f563 9520 struct breakpoint *breakpoint2 = NULL;
c906108c 9521 struct cleanup *old_chain;
186c406b
TT
9522 int thread;
9523 struct thread_info *tp;
c906108c
SS
9524
9525 clear_proceed_status ();
9526
9527 /* Set a breakpoint where the user wants it and at return from
4a64f543 9528 this function. */
c5aa993b 9529
1bfeeb0f 9530 if (last_displayed_sal_is_valid ())
f8eba3c6 9531 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 9532 get_last_displayed_symtab (),
f8eba3c6 9533 get_last_displayed_line ());
c906108c 9534 else
f8eba3c6
TT
9535 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
9536 (struct symtab *) NULL, 0);
c5aa993b 9537
c906108c 9538 if (sals.nelts != 1)
8a3fe4f8 9539 error (_("Couldn't get information on specified line."));
c5aa993b 9540
c906108c 9541 sal = sals.sals[0];
4a64f543 9542 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9543
c906108c 9544 if (*arg)
8a3fe4f8 9545 error (_("Junk at end of arguments."));
c5aa993b 9546
c906108c 9547 resolve_sal_pc (&sal);
c5aa993b 9548
ae66c1fc
EZ
9549 if (anywhere)
9550 /* If the user told us to continue until a specified location,
9551 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9552 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9553 null_frame_id, bp_until);
ae66c1fc 9554 else
4a64f543
MS
9555 /* Otherwise, specify the selected frame, because we want to stop
9556 only at the very same frame. */
a6d9a66e
UW
9557 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9558 get_stack_frame_id (frame),
ae66c1fc 9559 bp_until);
c5aa993b 9560
f107f563 9561 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9562
186c406b
TT
9563 tp = inferior_thread ();
9564 thread = tp->num;
9565
ae66c1fc
EZ
9566 /* Keep within the current frame, or in frames called by the current
9567 one. */
edb3359d
DJ
9568
9569 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9570 {
edb3359d
DJ
9571 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9572 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9573 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9574 sal,
edb3359d 9575 frame_unwind_caller_id (frame),
f107f563
VP
9576 bp_until);
9577 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9578
9579 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9580 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9581 }
c5aa993b 9582
c906108c 9583 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9584
4a64f543
MS
9585 /* If we are running asynchronously, and proceed call above has
9586 actually managed to start the target, arrange for breakpoints to
9587 be deleted when the target stops. Otherwise, we're already
9588 stopped and delete breakpoints via cleanup chain. */
f107f563 9589
8ea051c5 9590 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9591 {
bfec99b2
PA
9592 struct until_break_command_continuation_args *args;
9593 args = xmalloc (sizeof (*args));
f107f563 9594
bfec99b2
PA
9595 args->breakpoint = breakpoint;
9596 args->breakpoint2 = breakpoint2;
186c406b 9597 args->thread_num = thread;
f107f563
VP
9598
9599 discard_cleanups (old_chain);
95e54da7
PA
9600 add_continuation (inferior_thread (),
9601 until_break_command_continuation, args,
604ead4a 9602 xfree);
f107f563
VP
9603 }
9604 else
c5aa993b 9605 do_cleanups (old_chain);
c906108c 9606}
ae66c1fc 9607
c906108c
SS
9608/* This function attempts to parse an optional "if <cond>" clause
9609 from the arg string. If one is not found, it returns NULL.
c5aa993b 9610
c906108c
SS
9611 Else, it returns a pointer to the condition string. (It does not
9612 attempt to evaluate the string against a particular block.) And,
9613 it updates arg to point to the first character following the parsed
4a64f543 9614 if clause in the arg string. */
53a5351d 9615
c906108c 9616static char *
fba45db2 9617ep_parse_optional_if_clause (char **arg)
c906108c 9618{
c5aa993b
JM
9619 char *cond_string;
9620
9621 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9622 return NULL;
c5aa993b 9623
4a64f543 9624 /* Skip the "if" keyword. */
c906108c 9625 (*arg) += 2;
c5aa993b 9626
c906108c 9627 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9628 condition string. */
e9cafbcc 9629 *arg = skip_spaces (*arg);
c906108c 9630 cond_string = *arg;
c5aa993b 9631
4a64f543
MS
9632 /* Assume that the condition occupies the remainder of the arg
9633 string. */
c906108c 9634 (*arg) += strlen (cond_string);
c5aa993b 9635
c906108c
SS
9636 return cond_string;
9637}
c5aa993b 9638
c906108c
SS
9639/* Commands to deal with catching events, such as signals, exceptions,
9640 process start/exit, etc. */
c5aa993b
JM
9641
9642typedef enum
9643{
44feb3ce
TT
9644 catch_fork_temporary, catch_vfork_temporary,
9645 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9646}
9647catch_fork_kind;
9648
c906108c 9649static void
cc59ec59
MS
9650catch_fork_command_1 (char *arg, int from_tty,
9651 struct cmd_list_element *command)
c906108c 9652{
a6d9a66e 9653 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9654 char *cond_string = NULL;
44feb3ce
TT
9655 catch_fork_kind fork_kind;
9656 int tempflag;
9657
9658 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9659 tempflag = (fork_kind == catch_fork_temporary
9660 || fork_kind == catch_vfork_temporary);
c5aa993b 9661
44feb3ce
TT
9662 if (!arg)
9663 arg = "";
e9cafbcc 9664 arg = skip_spaces (arg);
c5aa993b 9665
c906108c 9666 /* The allowed syntax is:
c5aa993b
JM
9667 catch [v]fork
9668 catch [v]fork if <cond>
9669
4a64f543 9670 First, check if there's an if clause. */
c906108c 9671 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9672
c906108c 9673 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9674 error (_("Junk at end of arguments."));
c5aa993b 9675
c906108c 9676 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9677 and enable reporting of such events. */
c5aa993b
JM
9678 switch (fork_kind)
9679 {
44feb3ce
TT
9680 case catch_fork_temporary:
9681 case catch_fork_permanent:
a6d9a66e 9682 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9683 &catch_fork_breakpoint_ops);
c906108c 9684 break;
44feb3ce
TT
9685 case catch_vfork_temporary:
9686 case catch_vfork_permanent:
a6d9a66e 9687 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9688 &catch_vfork_breakpoint_ops);
c906108c 9689 break;
c5aa993b 9690 default:
8a3fe4f8 9691 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9692 break;
c5aa993b 9693 }
c906108c
SS
9694}
9695
9696static void
cc59ec59
MS
9697catch_exec_command_1 (char *arg, int from_tty,
9698 struct cmd_list_element *command)
c906108c 9699{
b4d90040 9700 struct exec_catchpoint *c;
a6d9a66e 9701 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9702 int tempflag;
c5aa993b 9703 char *cond_string = NULL;
c906108c 9704
44feb3ce
TT
9705 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9706
9707 if (!arg)
9708 arg = "";
e9cafbcc 9709 arg = skip_spaces (arg);
c906108c
SS
9710
9711 /* The allowed syntax is:
c5aa993b
JM
9712 catch exec
9713 catch exec if <cond>
c906108c 9714
4a64f543 9715 First, check if there's an if clause. */
c906108c
SS
9716 cond_string = ep_parse_optional_if_clause (&arg);
9717
9718 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9719 error (_("Junk at end of arguments."));
c906108c 9720
b4d90040
PA
9721 c = XNEW (struct exec_catchpoint);
9722 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
9723 &catch_exec_breakpoint_ops);
9724 c->exec_pathname = NULL;
9725
3ea46bff 9726 install_breakpoint (0, &c->base, 1);
c906108c 9727}
c5aa993b 9728
3086aeae 9729static enum print_stop_action
348d480f 9730print_it_exception_catchpoint (bpstat bs)
3086aeae 9731{
79a45e25 9732 struct ui_out *uiout = current_uiout;
348d480f 9733 struct breakpoint *b = bs->breakpoint_at;
ade92717 9734 int bp_temp, bp_throw;
3086aeae 9735
ade92717 9736 annotate_catchpoint (b->number);
3086aeae 9737
ade92717
AR
9738 bp_throw = strstr (b->addr_string, "throw") != NULL;
9739 if (b->loc->address != b->loc->requested_address)
9740 breakpoint_adjustment_warning (b->loc->requested_address,
9741 b->loc->address,
9742 b->number, 1);
df2b6d2d 9743 bp_temp = b->disposition == disp_del;
ade92717
AR
9744 ui_out_text (uiout,
9745 bp_temp ? "Temporary catchpoint "
9746 : "Catchpoint ");
9747 if (!ui_out_is_mi_like_p (uiout))
9748 ui_out_field_int (uiout, "bkptno", b->number);
9749 ui_out_text (uiout,
c0b37c48
AR
9750 bp_throw ? " (exception thrown), "
9751 : " (exception caught), ");
ade92717
AR
9752 if (ui_out_is_mi_like_p (uiout))
9753 {
9754 ui_out_field_string (uiout, "reason",
9755 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9756 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9757 ui_out_field_int (uiout, "bkptno", b->number);
9758 }
3086aeae
DJ
9759 return PRINT_SRC_AND_LOC;
9760}
9761
9762static void
cc59ec59
MS
9763print_one_exception_catchpoint (struct breakpoint *b,
9764 struct bp_location **last_loc)
3086aeae 9765{
79a45b7d 9766 struct value_print_options opts;
79a45e25 9767 struct ui_out *uiout = current_uiout;
cc59ec59 9768
79a45b7d
TT
9769 get_user_print_options (&opts);
9770 if (opts.addressprint)
3086aeae
DJ
9771 {
9772 annotate_field (4);
604133b5
AR
9773 if (b->loc == NULL || b->loc->shlib_disabled)
9774 ui_out_field_string (uiout, "addr", "<PENDING>");
9775 else
5af949e3
UW
9776 ui_out_field_core_addr (uiout, "addr",
9777 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9778 }
9779 annotate_field (5);
604133b5 9780 if (b->loc)
a6d9a66e 9781 *last_loc = b->loc;
3086aeae
DJ
9782 if (strstr (b->addr_string, "throw") != NULL)
9783 ui_out_field_string (uiout, "what", "exception throw");
9784 else
9785 ui_out_field_string (uiout, "what", "exception catch");
9786}
9787
9788static void
9789print_mention_exception_catchpoint (struct breakpoint *b)
9790{
79a45e25 9791 struct ui_out *uiout = current_uiout;
ade92717
AR
9792 int bp_temp;
9793 int bp_throw;
9794
df2b6d2d 9795 bp_temp = b->disposition == disp_del;
ade92717
AR
9796 bp_throw = strstr (b->addr_string, "throw") != NULL;
9797 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9798 : _("Catchpoint "));
9799 ui_out_field_int (uiout, "bkptno", b->number);
9800 ui_out_text (uiout, bp_throw ? _(" (throw)")
9801 : _(" (catch)"));
3086aeae
DJ
9802}
9803
6149aea9
PA
9804/* Implement the "print_recreate" breakpoint_ops method for throw and
9805 catch catchpoints. */
9806
9807static void
4a64f543
MS
9808print_recreate_exception_catchpoint (struct breakpoint *b,
9809 struct ui_file *fp)
6149aea9
PA
9810{
9811 int bp_temp;
9812 int bp_throw;
9813
9814 bp_temp = b->disposition == disp_del;
9815 bp_throw = strstr (b->addr_string, "throw") != NULL;
9816 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9817 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 9818 print_recreate_thread (b, fp);
6149aea9
PA
9819}
9820
2060206e 9821static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
9822
9823static int
9824handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9825 enum exception_event_kind ex_event, int from_tty)
9826{
604133b5
AR
9827 char *trigger_func_name;
9828
3086aeae 9829 if (ex_event == EX_EVENT_CATCH)
604133b5 9830 trigger_func_name = "__cxa_begin_catch";
3086aeae 9831 else
604133b5 9832 trigger_func_name = "__cxa_throw";
3086aeae 9833
8cdf0e15
VP
9834 create_breakpoint (get_current_arch (),
9835 trigger_func_name, cond_string, -1,
9836 0 /* condition and thread are valid. */,
0fb4aa4b 9837 tempflag, bp_breakpoint,
8cdf0e15
VP
9838 0,
9839 AUTO_BOOLEAN_TRUE /* pending */,
9840 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9841 1 /* enabled */,
9842 0 /* internal */);
3086aeae 9843
3086aeae
DJ
9844 return 1;
9845}
9846
4a64f543 9847/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9848
9849static void
fba45db2
KB
9850catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9851 int tempflag, int from_tty)
c906108c 9852{
c5aa993b 9853 char *cond_string = NULL;
c5aa993b 9854
44feb3ce
TT
9855 if (!arg)
9856 arg = "";
e9cafbcc 9857 arg = skip_spaces (arg);
c5aa993b 9858
c906108c
SS
9859 cond_string = ep_parse_optional_if_clause (&arg);
9860
9861 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9862 error (_("Junk at end of arguments."));
c906108c 9863
059fb39f
PM
9864 if (ex_event != EX_EVENT_THROW
9865 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9866 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9867
3086aeae
DJ
9868 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9869 return;
9870
8a3fe4f8 9871 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9872}
9873
44feb3ce
TT
9874/* Implementation of "catch catch" command. */
9875
9876static void
9877catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9878{
9879 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9880
44feb3ce
TT
9881 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9882}
9883
9884/* Implementation of "catch throw" command. */
9885
9886static void
9887catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9888{
9889 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9890
44feb3ce
TT
9891 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9892}
9893
9ac4176b 9894void
28010a5d
PA
9895init_ada_exception_breakpoint (struct breakpoint *b,
9896 struct gdbarch *gdbarch,
9897 struct symtab_and_line sal,
9898 char *addr_string,
c0a91b2b 9899 const struct breakpoint_ops *ops,
28010a5d
PA
9900 int tempflag,
9901 int from_tty)
f7f9143b 9902{
f7f9143b
JB
9903 if (from_tty)
9904 {
5af949e3
UW
9905 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9906 if (!loc_gdbarch)
9907 loc_gdbarch = gdbarch;
9908
6c95b8df
PA
9909 describe_other_breakpoints (loc_gdbarch,
9910 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9911 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9912 version for exception catchpoints, because two catchpoints
9913 used for different exception names will use the same address.
9914 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9915 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 9916 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
9917 the user what type of catchpoint it is. The above is good
9918 enough for now, though. */
9919 }
9920
28010a5d 9921 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
9922
9923 b->enable_state = bp_enabled;
9924 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
9925 b->addr_string = addr_string;
9926 b->language = language_ada;
f7f9143b
JB
9927}
9928
a96d9b2e
SDJ
9929/* Splits the argument using space as delimiter. Returns an xmalloc'd
9930 filter list, or NULL if no filtering is required. */
9931static VEC(int) *
9932catch_syscall_split_args (char *arg)
9933{
9934 VEC(int) *result = NULL;
29d0bb3d 9935 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
9936
9937 while (*arg != '\0')
9938 {
9939 int i, syscall_number;
9940 char *endptr;
9941 char cur_name[128];
9942 struct syscall s;
9943
9944 /* Skip whitespace. */
9945 while (isspace (*arg))
9946 arg++;
9947
9948 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9949 cur_name[i] = arg[i];
9950 cur_name[i] = '\0';
9951 arg += i;
9952
9953 /* Check if the user provided a syscall name or a number. */
9954 syscall_number = (int) strtol (cur_name, &endptr, 0);
9955 if (*endptr == '\0')
bccd0dd2 9956 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9957 else
9958 {
9959 /* We have a name. Let's check if it's valid and convert it
9960 to a number. */
9961 get_syscall_by_name (cur_name, &s);
9962
9963 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
9964 /* Here we have to issue an error instead of a warning,
9965 because GDB cannot do anything useful if there's no
9966 syscall number to be caught. */
a96d9b2e
SDJ
9967 error (_("Unknown syscall name '%s'."), cur_name);
9968 }
9969
9970 /* Ok, it's valid. */
9971 VEC_safe_push (int, result, s.number);
9972 }
9973
9974 discard_cleanups (cleanup);
9975 return result;
9976}
9977
9978/* Implement the "catch syscall" command. */
9979
9980static void
cc59ec59
MS
9981catch_syscall_command_1 (char *arg, int from_tty,
9982 struct cmd_list_element *command)
a96d9b2e
SDJ
9983{
9984 int tempflag;
9985 VEC(int) *filter;
9986 struct syscall s;
9987 struct gdbarch *gdbarch = get_current_arch ();
9988
9989 /* Checking if the feature if supported. */
9990 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9991 error (_("The feature 'catch syscall' is not supported on \
ea666128 9992this architecture yet."));
a96d9b2e
SDJ
9993
9994 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9995
e9cafbcc 9996 arg = skip_spaces (arg);
a96d9b2e
SDJ
9997
9998 /* We need to do this first "dummy" translation in order
9999 to get the syscall XML file loaded or, most important,
10000 to display a warning to the user if there's no XML file
10001 for his/her architecture. */
10002 get_syscall_by_number (0, &s);
10003
10004 /* The allowed syntax is:
10005 catch syscall
10006 catch syscall <name | number> [<name | number> ... <name | number>]
10007
10008 Let's check if there's a syscall name. */
10009
10010 if (arg != NULL)
10011 filter = catch_syscall_split_args (arg);
10012 else
10013 filter = NULL;
10014
10015 create_syscall_event_catchpoint (tempflag, filter,
10016 &catch_syscall_breakpoint_ops);
10017}
10018
c906108c 10019static void
fba45db2 10020catch_command (char *arg, int from_tty)
c906108c 10021{
44feb3ce 10022 error (_("Catch requires an event name."));
c906108c
SS
10023}
10024\f
10025
10026static void
fba45db2 10027tcatch_command (char *arg, int from_tty)
c906108c 10028{
44feb3ce 10029 error (_("Catch requires an event name."));
c906108c
SS
10030}
10031
8a2c437b
TT
10032/* A qsort comparison function that sorts breakpoints in order. */
10033
10034static int
10035compare_breakpoints (const void *a, const void *b)
10036{
10037 const breakpoint_p *ba = a;
10038 uintptr_t ua = (uintptr_t) *ba;
10039 const breakpoint_p *bb = b;
10040 uintptr_t ub = (uintptr_t) *bb;
10041
10042 if ((*ba)->number < (*bb)->number)
10043 return -1;
10044 else if ((*ba)->number > (*bb)->number)
10045 return 1;
10046
10047 /* Now sort by address, in case we see, e..g, two breakpoints with
10048 the number 0. */
10049 if (ua < ub)
10050 return -1;
10051 return ub > ub ? 1 : 0;
10052}
10053
80f8a6eb 10054/* Delete breakpoints by address or line. */
c906108c
SS
10055
10056static void
fba45db2 10057clear_command (char *arg, int from_tty)
c906108c 10058{
8a2c437b 10059 struct breakpoint *b, *prev;
d6e956e5
VP
10060 VEC(breakpoint_p) *found = 0;
10061 int ix;
c906108c
SS
10062 int default_match;
10063 struct symtabs_and_lines sals;
10064 struct symtab_and_line sal;
c906108c 10065 int i;
8a2c437b 10066 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
10067
10068 if (arg)
10069 {
f8eba3c6
TT
10070 sals = decode_line_spec (arg, (DECODE_LINE_FUNFIRSTLINE
10071 | DECODE_LINE_LIST_MODE));
c906108c
SS
10072 default_match = 0;
10073 }
10074 else
10075 {
c5aa993b 10076 sals.sals = (struct symtab_and_line *)
c906108c 10077 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 10078 make_cleanup (xfree, sals.sals);
4a64f543 10079 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
10080
10081 /* Set sal's line, symtab, pc, and pspace to the values
10082 corresponding to the last call to print_frame_info. If the
10083 codepoint is not valid, this will set all the fields to 0. */
10084 get_last_displayed_sal (&sal);
c906108c 10085 if (sal.symtab == 0)
8a3fe4f8 10086 error (_("No source file specified."));
c906108c
SS
10087
10088 sals.sals[0] = sal;
10089 sals.nelts = 1;
10090
10091 default_match = 1;
10092 }
10093
4a64f543
MS
10094 /* We don't call resolve_sal_pc here. That's not as bad as it
10095 seems, because all existing breakpoints typically have both
10096 file/line and pc set. So, if clear is given file/line, we can
10097 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10098
10099 We only support clearing given the address explicitly
10100 present in breakpoint table. Say, we've set breakpoint
4a64f543 10101 at file:line. There were several PC values for that file:line,
ed0616c6 10102 due to optimization, all in one block.
4a64f543
MS
10103
10104 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10105 PC corresponding to the same file:line, the breakpoint won't
10106 be cleared. We probably can still clear the breakpoint, but
10107 since the other PC value is never presented to user, user
10108 can only find it by guessing, and it does not seem important
10109 to support that. */
10110
4a64f543
MS
10111 /* For each line spec given, delete bps which correspond to it. Do
10112 it in two passes, solely to preserve the current behavior that
10113 from_tty is forced true if we delete more than one
10114 breakpoint. */
c906108c 10115
80f8a6eb 10116 found = NULL;
8a2c437b 10117 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
10118 for (i = 0; i < sals.nelts; i++)
10119 {
10120 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
10121 If line given (pc == 0), clear all bpts on specified line.
10122 If defaulting, clear all bpts on default line
c906108c 10123 or at default pc.
c5aa993b
JM
10124
10125 defaulting sal.pc != 0 tests to do
10126
10127 0 1 pc
10128 1 1 pc _and_ line
10129 0 0 line
10130 1 0 <can't happen> */
c906108c
SS
10131
10132 sal = sals.sals[i];
c906108c 10133
4a64f543 10134 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 10135 ALL_BREAKPOINTS (b)
c5aa993b 10136 {
0d381245 10137 int match = 0;
4a64f543 10138 /* Are we going to delete b? */
cc60f2e3 10139 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
10140 {
10141 struct bp_location *loc = b->loc;
10142 for (; loc; loc = loc->next)
10143 {
f8eba3c6
TT
10144 /* If the user specified file:line, don't allow a PC
10145 match. This matches historical gdb behavior. */
10146 int pc_match = (!sal.explicit_line
10147 && sal.pc
10148 && (loc->pspace == sal.pspace)
10149 && (loc->address == sal.pc)
10150 && (!section_is_overlay (loc->section)
10151 || loc->section == sal.section));
10152 int line_match = ((default_match || sal.explicit_line)
10153 && loc->source_file != NULL
0d381245 10154 && sal.symtab != NULL
6c95b8df 10155 && sal.pspace == loc->pspace
f8eba3c6 10156 && filename_cmp (loc->source_file,
0ba1096a 10157 sal.symtab->filename) == 0
f8eba3c6 10158 && loc->line_number == sal.line);
0d381245
VP
10159 if (pc_match || line_match)
10160 {
10161 match = 1;
10162 break;
10163 }
10164 }
10165 }
10166
10167 if (match)
d6e956e5 10168 VEC_safe_push(breakpoint_p, found, b);
c906108c 10169 }
80f8a6eb 10170 }
8a2c437b 10171
80f8a6eb 10172 /* Now go thru the 'found' chain and delete them. */
d6e956e5 10173 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
10174 {
10175 if (arg)
8a3fe4f8 10176 error (_("No breakpoint at %s."), arg);
80f8a6eb 10177 else
8a3fe4f8 10178 error (_("No breakpoint at this line."));
80f8a6eb 10179 }
c906108c 10180
8a2c437b
TT
10181 /* Remove duplicates from the vec. */
10182 qsort (VEC_address (breakpoint_p, found),
10183 VEC_length (breakpoint_p, found),
10184 sizeof (breakpoint_p),
10185 compare_breakpoints);
10186 prev = VEC_index (breakpoint_p, found, 0);
10187 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
10188 {
10189 if (b == prev)
10190 {
10191 VEC_ordered_remove (breakpoint_p, found, ix);
10192 --ix;
10193 }
10194 }
10195
d6e956e5 10196 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 10197 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10198 if (from_tty)
a3f17187 10199 {
d6e956e5 10200 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
10201 printf_unfiltered (_("Deleted breakpoint "));
10202 else
10203 printf_unfiltered (_("Deleted breakpoints "));
10204 }
80f8a6eb 10205 breakpoints_changed ();
d6e956e5
VP
10206
10207 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 10208 {
c5aa993b 10209 if (from_tty)
d6e956e5
VP
10210 printf_unfiltered ("%d ", b->number);
10211 delete_breakpoint (b);
c906108c 10212 }
80f8a6eb
MS
10213 if (from_tty)
10214 putchar_unfiltered ('\n');
8a2c437b
TT
10215
10216 do_cleanups (cleanups);
c906108c
SS
10217}
10218\f
10219/* Delete breakpoint in BS if they are `delete' breakpoints and
10220 all breakpoints that are marked for deletion, whether hit or not.
10221 This is called after any breakpoint is hit, or after errors. */
10222
10223void
fba45db2 10224breakpoint_auto_delete (bpstat bs)
c906108c 10225{
35df4500 10226 struct breakpoint *b, *b_tmp;
c906108c
SS
10227
10228 for (; bs; bs = bs->next)
f431efe5
PA
10229 if (bs->breakpoint_at
10230 && bs->breakpoint_at->disposition == disp_del
c906108c 10231 && bs->stop)
f431efe5 10232 delete_breakpoint (bs->breakpoint_at);
c906108c 10233
35df4500 10234 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10235 {
b5de0fa7 10236 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10237 delete_breakpoint (b);
10238 }
c906108c
SS
10239}
10240
4a64f543
MS
10241/* A comparison function for bp_location AP and BP being interfaced to
10242 qsort. Sort elements primarily by their ADDRESS (no matter what
10243 does breakpoint_address_is_meaningful say for its OWNER),
10244 secondarily by ordering first bp_permanent OWNERed elements and
10245 terciarily just ensuring the array is sorted stable way despite
e5dd4106 10246 qsort being an unstable algorithm. */
876fa593
JK
10247
10248static int
494cfb0f 10249bp_location_compare (const void *ap, const void *bp)
876fa593 10250{
494cfb0f
JK
10251 struct bp_location *a = *(void **) ap;
10252 struct bp_location *b = *(void **) bp;
2bdf28a0 10253 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10254 int a_perm = a->owner->enable_state == bp_permanent;
10255 int b_perm = b->owner->enable_state == bp_permanent;
10256
10257 if (a->address != b->address)
10258 return (a->address > b->address) - (a->address < b->address);
10259
10260 /* Sort permanent breakpoints first. */
10261 if (a_perm != b_perm)
10262 return (a_perm < b_perm) - (a_perm > b_perm);
10263
4a64f543
MS
10264 /* Make the user-visible order stable across GDB runs. Locations of
10265 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10266
10267 if (a->owner->number != b->owner->number)
10268 return (a->owner->number > b->owner->number)
10269 - (a->owner->number < b->owner->number);
10270
10271 return (a > b) - (a < b);
10272}
10273
876fa593 10274/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10275 bp_location_shadow_len_after_address_max according to the current
10276 content of the bp_location array. */
f7545552
TT
10277
10278static void
876fa593 10279bp_location_target_extensions_update (void)
f7545552 10280{
876fa593
JK
10281 struct bp_location *bl, **blp_tmp;
10282
10283 bp_location_placed_address_before_address_max = 0;
10284 bp_location_shadow_len_after_address_max = 0;
10285
10286 ALL_BP_LOCATIONS (bl, blp_tmp)
10287 {
10288 CORE_ADDR start, end, addr;
10289
10290 if (!bp_location_has_shadow (bl))
10291 continue;
10292
10293 start = bl->target_info.placed_address;
10294 end = start + bl->target_info.shadow_len;
10295
10296 gdb_assert (bl->address >= start);
10297 addr = bl->address - start;
10298 if (addr > bp_location_placed_address_before_address_max)
10299 bp_location_placed_address_before_address_max = addr;
10300
10301 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10302
10303 gdb_assert (bl->address < end);
10304 addr = end - bl->address;
10305 if (addr > bp_location_shadow_len_after_address_max)
10306 bp_location_shadow_len_after_address_max = addr;
10307 }
f7545552
TT
10308}
10309
1e4d1764
YQ
10310/* Download tracepoint locations if they haven't been. */
10311
10312static void
10313download_tracepoint_locations (void)
10314{
10315 struct bp_location *bl, **blp_tmp;
10316 struct cleanup *old_chain;
10317
10318 if (!target_can_download_tracepoint ())
10319 return;
10320
10321 old_chain = save_current_space_and_thread ();
10322
10323 ALL_BP_LOCATIONS (bl, blp_tmp)
10324 {
10325 struct tracepoint *t;
10326
10327 if (!is_tracepoint (bl->owner))
10328 continue;
10329
10330 if ((bl->owner->type == bp_fast_tracepoint
10331 ? !may_insert_fast_tracepoints
10332 : !may_insert_tracepoints))
10333 continue;
10334
10335 /* In tracepoint, locations are _never_ duplicated, so
10336 should_be_inserted is equivalent to
10337 unduplicated_should_be_inserted. */
10338 if (!should_be_inserted (bl) || bl->inserted)
10339 continue;
10340
10341 switch_to_program_space_and_thread (bl->pspace);
10342
10343 target_download_tracepoint (bl);
10344
10345 bl->inserted = 1;
10346 t = (struct tracepoint *) bl->owner;
10347 t->number_on_target = bl->owner->number;
10348 }
10349
10350 do_cleanups (old_chain);
10351}
10352
934709f0
PW
10353/* Swap the insertion/duplication state between two locations. */
10354
10355static void
10356swap_insertion (struct bp_location *left, struct bp_location *right)
10357{
10358 const int left_inserted = left->inserted;
10359 const int left_duplicate = left->duplicate;
10360 const struct bp_target_info left_target_info = left->target_info;
10361
1e4d1764
YQ
10362 /* Locations of tracepoints can never be duplicated. */
10363 if (is_tracepoint (left->owner))
10364 gdb_assert (!left->duplicate);
10365 if (is_tracepoint (right->owner))
10366 gdb_assert (!right->duplicate);
10367
934709f0
PW
10368 left->inserted = right->inserted;
10369 left->duplicate = right->duplicate;
10370 left->target_info = right->target_info;
10371 right->inserted = left_inserted;
10372 right->duplicate = left_duplicate;
10373 right->target_info = left_target_info;
10374}
10375
4cd9bd08 10376/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10377 into the inferior, only remove already-inserted locations that no
10378 longer should be inserted. Functions that delete a breakpoint or
10379 breakpoints should pass false, so that deleting a breakpoint
10380 doesn't have the side effect of inserting the locations of other
10381 breakpoints that are marked not-inserted, but should_be_inserted
10382 returns true on them.
10383
10384 This behaviour is useful is situations close to tear-down -- e.g.,
10385 after an exec, while the target still has execution, but breakpoint
10386 shadows of the previous executable image should *NOT* be restored
10387 to the new image; or before detaching, where the target still has
10388 execution and wants to delete breakpoints from GDB's lists, and all
10389 breakpoints had already been removed from the inferior. */
10390
0d381245 10391static void
b60e7edf 10392update_global_location_list (int should_insert)
0d381245 10393{
74960c60 10394 struct breakpoint *b;
876fa593 10395 struct bp_location **locp, *loc;
f7545552
TT
10396 struct cleanup *cleanups;
10397
2d134ed3
PA
10398 /* Used in the duplicates detection below. When iterating over all
10399 bp_locations, points to the first bp_location of a given address.
10400 Breakpoints and watchpoints of different types are never
10401 duplicates of each other. Keep one pointer for each type of
10402 breakpoint/watchpoint, so we only need to loop over all locations
10403 once. */
10404 struct bp_location *bp_loc_first; /* breakpoint */
10405 struct bp_location *wp_loc_first; /* hardware watchpoint */
10406 struct bp_location *awp_loc_first; /* access watchpoint */
10407 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10408
4a64f543
MS
10409 /* Saved former bp_location array which we compare against the newly
10410 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10411 struct bp_location **old_location, **old_locp;
10412 unsigned old_location_count;
10413
10414 old_location = bp_location;
10415 old_location_count = bp_location_count;
10416 bp_location = NULL;
10417 bp_location_count = 0;
10418 cleanups = make_cleanup (xfree, old_location);
0d381245 10419
74960c60 10420 ALL_BREAKPOINTS (b)
876fa593
JK
10421 for (loc = b->loc; loc; loc = loc->next)
10422 bp_location_count++;
10423
10424 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10425 locp = bp_location;
10426 ALL_BREAKPOINTS (b)
10427 for (loc = b->loc; loc; loc = loc->next)
10428 *locp++ = loc;
10429 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10430 bp_location_compare);
876fa593
JK
10431
10432 bp_location_target_extensions_update ();
74960c60 10433
4a64f543
MS
10434 /* Identify bp_location instances that are no longer present in the
10435 new list, and therefore should be freed. Note that it's not
10436 necessary that those locations should be removed from inferior --
10437 if there's another location at the same address (previously
10438 marked as duplicate), we don't need to remove/insert the
10439 location.
876fa593 10440
4a64f543
MS
10441 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10442 and former bp_location array state respectively. */
876fa593
JK
10443
10444 locp = bp_location;
10445 for (old_locp = old_location; old_locp < old_location + old_location_count;
10446 old_locp++)
74960c60 10447 {
876fa593 10448 struct bp_location *old_loc = *old_locp;
c7d46a38 10449 struct bp_location **loc2p;
876fa593 10450
e5dd4106 10451 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 10452 not, we have to free it. */
c7d46a38 10453 int found_object = 0;
20874c92
VP
10454 /* Tells if the location should remain inserted in the target. */
10455 int keep_in_target = 0;
10456 int removed = 0;
876fa593 10457
4a64f543
MS
10458 /* Skip LOCP entries which will definitely never be needed.
10459 Stop either at or being the one matching OLD_LOC. */
876fa593 10460 while (locp < bp_location + bp_location_count
c7d46a38 10461 && (*locp)->address < old_loc->address)
876fa593 10462 locp++;
c7d46a38
PA
10463
10464 for (loc2p = locp;
10465 (loc2p < bp_location + bp_location_count
10466 && (*loc2p)->address == old_loc->address);
10467 loc2p++)
10468 {
10469 if (*loc2p == old_loc)
10470 {
10471 found_object = 1;
10472 break;
10473 }
10474 }
74960c60 10475
4a64f543
MS
10476 /* If this location is no longer present, and inserted, look if
10477 there's maybe a new location at the same address. If so,
10478 mark that one inserted, and don't remove this one. This is
10479 needed so that we don't have a time window where a breakpoint
10480 at certain location is not inserted. */
74960c60 10481
876fa593 10482 if (old_loc->inserted)
0d381245 10483 {
4a64f543
MS
10484 /* If the location is inserted now, we might have to remove
10485 it. */
74960c60 10486
876fa593 10487 if (found_object && should_be_inserted (old_loc))
74960c60 10488 {
4a64f543
MS
10489 /* The location is still present in the location list,
10490 and still should be inserted. Don't do anything. */
20874c92 10491 keep_in_target = 1;
74960c60
VP
10492 }
10493 else
10494 {
4a64f543
MS
10495 /* The location is either no longer present, or got
10496 disabled. See if there's another location at the
10497 same address, in which case we don't need to remove
10498 this one from the target. */
876fa593 10499
2bdf28a0 10500 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10501 if (breakpoint_address_is_meaningful (old_loc->owner))
10502 {
876fa593 10503 for (loc2p = locp;
c7d46a38
PA
10504 (loc2p < bp_location + bp_location_count
10505 && (*loc2p)->address == old_loc->address);
876fa593
JK
10506 loc2p++)
10507 {
10508 struct bp_location *loc2 = *loc2p;
10509
2d134ed3 10510 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 10511 {
85d721b8
PA
10512 /* Read watchpoint locations are switched to
10513 access watchpoints, if the former are not
10514 supported, but the latter are. */
10515 if (is_hardware_watchpoint (old_loc->owner))
10516 {
10517 gdb_assert (is_hardware_watchpoint (loc2->owner));
10518 loc2->watchpoint_type = old_loc->watchpoint_type;
10519 }
10520
934709f0
PW
10521 /* loc2 is a duplicated location. We need to check
10522 if it should be inserted in case it will be
10523 unduplicated. */
10524 if (loc2 != old_loc
10525 && unduplicated_should_be_inserted (loc2))
c7d46a38 10526 {
934709f0 10527 swap_insertion (old_loc, loc2);
c7d46a38
PA
10528 keep_in_target = 1;
10529 break;
10530 }
876fa593
JK
10531 }
10532 }
10533 }
74960c60
VP
10534 }
10535
20874c92
VP
10536 if (!keep_in_target)
10537 {
876fa593 10538 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10539 {
4a64f543
MS
10540 /* This is just about all we can do. We could keep
10541 this location on the global list, and try to
10542 remove it next time, but there's no particular
10543 reason why we will succeed next time.
20874c92 10544
4a64f543
MS
10545 Note that at this point, old_loc->owner is still
10546 valid, as delete_breakpoint frees the breakpoint
10547 only after calling us. */
3e43a32a
MS
10548 printf_filtered (_("warning: Error removing "
10549 "breakpoint %d\n"),
876fa593 10550 old_loc->owner->number);
20874c92
VP
10551 }
10552 removed = 1;
10553 }
0d381245 10554 }
74960c60
VP
10555
10556 if (!found_object)
1c5cfe86 10557 {
db82e815
PA
10558 if (removed && non_stop
10559 && breakpoint_address_is_meaningful (old_loc->owner)
10560 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10561 {
db82e815
PA
10562 /* This location was removed from the target. In
10563 non-stop mode, a race condition is possible where
10564 we've removed a breakpoint, but stop events for that
10565 breakpoint are already queued and will arrive later.
10566 We apply an heuristic to be able to distinguish such
10567 SIGTRAPs from other random SIGTRAPs: we keep this
10568 breakpoint location for a bit, and will retire it
10569 after we see some number of events. The theory here
10570 is that reporting of events should, "on the average",
10571 be fair, so after a while we'll see events from all
10572 threads that have anything of interest, and no longer
10573 need to keep this breakpoint location around. We
10574 don't hold locations forever so to reduce chances of
10575 mistaking a non-breakpoint SIGTRAP for a breakpoint
10576 SIGTRAP.
10577
10578 The heuristic failing can be disastrous on
10579 decr_pc_after_break targets.
10580
10581 On decr_pc_after_break targets, like e.g., x86-linux,
10582 if we fail to recognize a late breakpoint SIGTRAP,
10583 because events_till_retirement has reached 0 too
10584 soon, we'll fail to do the PC adjustment, and report
10585 a random SIGTRAP to the user. When the user resumes
10586 the inferior, it will most likely immediately crash
2dec564e 10587 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10588 corrupted, because of being resumed e.g., in the
10589 middle of a multi-byte instruction, or skipped a
10590 one-byte instruction. This was actually seen happen
10591 on native x86-linux, and should be less rare on
10592 targets that do not support new thread events, like
10593 remote, due to the heuristic depending on
10594 thread_count.
10595
10596 Mistaking a random SIGTRAP for a breakpoint trap
10597 causes similar symptoms (PC adjustment applied when
10598 it shouldn't), but then again, playing with SIGTRAPs
10599 behind the debugger's back is asking for trouble.
10600
10601 Since hardware watchpoint traps are always
10602 distinguishable from other traps, so we don't need to
10603 apply keep hardware watchpoint moribund locations
10604 around. We simply always ignore hardware watchpoint
10605 traps we can no longer explain. */
10606
876fa593
JK
10607 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10608 old_loc->owner = NULL;
20874c92 10609
876fa593 10610 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10611 }
10612 else
f431efe5
PA
10613 {
10614 old_loc->owner = NULL;
10615 decref_bp_location (&old_loc);
10616 }
20874c92 10617 }
74960c60 10618 }
1c5cfe86 10619
348d480f
PA
10620 /* Rescan breakpoints at the same address and section, marking the
10621 first one as "first" and any others as "duplicates". This is so
10622 that the bpt instruction is only inserted once. If we have a
10623 permanent breakpoint at the same place as BPT, make that one the
10624 official one, and the rest as duplicates. Permanent breakpoints
10625 are sorted first for the same address.
10626
10627 Do the same for hardware watchpoints, but also considering the
10628 watchpoint's type (regular/access/read) and length. */
10629
10630 bp_loc_first = NULL;
10631 wp_loc_first = NULL;
10632 awp_loc_first = NULL;
10633 rwp_loc_first = NULL;
10634 ALL_BP_LOCATIONS (loc, locp)
10635 {
10636 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10637 non-NULL. */
348d480f 10638 struct bp_location **loc_first_p;
d3fbdd86 10639 b = loc->owner;
348d480f 10640
f8eba3c6 10641 if (!should_be_inserted (loc)
348d480f 10642 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
10643 /* Don't detect duplicate for tracepoint locations because they are
10644 never duplicated. See the comments in field `duplicate' of
10645 `struct bp_location'. */
348d480f
PA
10646 || is_tracepoint (b))
10647 continue;
10648
10649 /* Permanent breakpoint should always be inserted. */
10650 if (b->enable_state == bp_permanent && ! loc->inserted)
10651 internal_error (__FILE__, __LINE__,
10652 _("allegedly permanent breakpoint is not "
10653 "actually inserted"));
10654
10655 if (b->type == bp_hardware_watchpoint)
10656 loc_first_p = &wp_loc_first;
10657 else if (b->type == bp_read_watchpoint)
10658 loc_first_p = &rwp_loc_first;
10659 else if (b->type == bp_access_watchpoint)
10660 loc_first_p = &awp_loc_first;
10661 else
10662 loc_first_p = &bp_loc_first;
10663
10664 if (*loc_first_p == NULL
10665 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10666 || !breakpoint_locations_match (loc, *loc_first_p))
10667 {
10668 *loc_first_p = loc;
10669 loc->duplicate = 0;
10670 continue;
10671 }
10672
934709f0
PW
10673
10674 /* This and the above ensure the invariant that the first location
10675 is not duplicated, and is the inserted one.
10676 All following are marked as duplicated, and are not inserted. */
10677 if (loc->inserted)
10678 swap_insertion (loc, *loc_first_p);
348d480f
PA
10679 loc->duplicate = 1;
10680
10681 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10682 && b->enable_state != bp_permanent)
10683 internal_error (__FILE__, __LINE__,
10684 _("another breakpoint was inserted on top of "
10685 "a permanent breakpoint"));
10686 }
10687
10688 if (breakpoints_always_inserted_mode () && should_insert
10689 && (have_live_inferiors ()
10690 || (gdbarch_has_global_breakpoints (target_gdbarch))))
10691 insert_breakpoint_locations ();
10692
1e4d1764
YQ
10693 if (should_insert)
10694 download_tracepoint_locations ();
10695
348d480f
PA
10696 do_cleanups (cleanups);
10697}
10698
10699void
10700breakpoint_retire_moribund (void)
10701{
10702 struct bp_location *loc;
10703 int ix;
10704
10705 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10706 if (--(loc->events_till_retirement) == 0)
10707 {
10708 decref_bp_location (&loc);
10709 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10710 --ix;
10711 }
10712}
10713
10714static void
10715update_global_location_list_nothrow (int inserting)
10716{
10717 struct gdb_exception e;
10718
10719 TRY_CATCH (e, RETURN_MASK_ERROR)
10720 update_global_location_list (inserting);
10721}
10722
10723/* Clear BKP from a BPS. */
10724
10725static void
10726bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10727{
10728 bpstat bs;
10729
10730 for (bs = bps; bs; bs = bs->next)
10731 if (bs->breakpoint_at == bpt)
10732 {
10733 bs->breakpoint_at = NULL;
10734 bs->old_val = NULL;
10735 /* bs->commands will be freed later. */
10736 }
10737}
10738
10739/* Callback for iterate_over_threads. */
10740static int
10741bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10742{
10743 struct breakpoint *bpt = data;
10744
10745 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10746 return 0;
10747}
10748
10749/* Helper for breakpoint and tracepoint breakpoint_ops->mention
10750 callbacks. */
10751
10752static void
10753say_where (struct breakpoint *b)
10754{
79a45e25 10755 struct ui_out *uiout = current_uiout;
348d480f
PA
10756 struct value_print_options opts;
10757
10758 get_user_print_options (&opts);
10759
10760 /* i18n: cagney/2005-02-11: Below needs to be merged into a
10761 single string. */
10762 if (b->loc == NULL)
10763 {
10764 printf_filtered (_(" (%s) pending."), b->addr_string);
10765 }
10766 else
10767 {
f8eba3c6 10768 if (opts.addressprint || b->loc->source_file == NULL)
348d480f
PA
10769 {
10770 printf_filtered (" at ");
10771 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
10772 gdb_stdout);
10773 }
f8eba3c6
TT
10774 if (b->loc->source_file)
10775 {
10776 /* If there is a single location, we can print the location
10777 more nicely. */
10778 if (b->loc->next == NULL)
10779 printf_filtered (": file %s, line %d.",
10780 b->loc->source_file, b->loc->line_number);
10781 else
10782 /* This is not ideal, but each location may have a
10783 different file name, and this at least reflects the
10784 real situation somewhat. */
10785 printf_filtered (": %s.", b->addr_string);
10786 }
348d480f
PA
10787
10788 if (b->loc->next)
10789 {
10790 struct bp_location *loc = b->loc;
10791 int n = 0;
10792 for (; loc; loc = loc->next)
10793 ++n;
10794 printf_filtered (" (%d locations)", n);
10795 }
10796 }
10797}
10798
348d480f
PA
10799/* Default bp_location_ops methods. */
10800
10801static void
10802bp_location_dtor (struct bp_location *self)
10803{
10804 xfree (self->cond);
10805 xfree (self->function_name);
f8eba3c6 10806 xfree (self->source_file);
348d480f
PA
10807}
10808
10809static const struct bp_location_ops bp_location_ops =
10810{
10811 bp_location_dtor
10812};
10813
2060206e
PA
10814/* Default breakpoint_ops methods all breakpoint_ops ultimately
10815 inherit from. */
348d480f 10816
2060206e
PA
10817static void
10818base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
10819{
10820 decref_counted_command_line (&self->commands);
10821 xfree (self->cond_string);
348d480f 10822 xfree (self->addr_string);
f8eba3c6 10823 xfree (self->filter);
348d480f 10824 xfree (self->addr_string_range_end);
348d480f
PA
10825}
10826
2060206e
PA
10827static struct bp_location *
10828base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
10829{
10830 struct bp_location *loc;
10831
10832 loc = XNEW (struct bp_location);
10833 init_bp_location (loc, &bp_location_ops, self);
10834 return loc;
10835}
10836
2060206e
PA
10837static void
10838base_breakpoint_re_set (struct breakpoint *b)
10839{
10840 /* Nothing to re-set. */
10841}
10842
10843#define internal_error_pure_virtual_called() \
10844 gdb_assert_not_reached ("pure virtual function called")
10845
10846static int
10847base_breakpoint_insert_location (struct bp_location *bl)
10848{
10849 internal_error_pure_virtual_called ();
10850}
10851
10852static int
10853base_breakpoint_remove_location (struct bp_location *bl)
10854{
10855 internal_error_pure_virtual_called ();
10856}
10857
10858static int
10859base_breakpoint_breakpoint_hit (const struct bp_location *bl,
10860 struct address_space *aspace,
10861 CORE_ADDR bp_addr)
10862{
10863 internal_error_pure_virtual_called ();
10864}
10865
10866static void
10867base_breakpoint_check_status (bpstat bs)
10868{
10869 /* Always stop. */
10870}
10871
10872/* A "works_in_software_mode" breakpoint_ops method that just internal
10873 errors. */
10874
10875static int
10876base_breakpoint_works_in_software_mode (const struct breakpoint *b)
10877{
10878 internal_error_pure_virtual_called ();
10879}
10880
10881/* A "resources_needed" breakpoint_ops method that just internal
10882 errors. */
10883
10884static int
10885base_breakpoint_resources_needed (const struct bp_location *bl)
10886{
10887 internal_error_pure_virtual_called ();
10888}
10889
10890static enum print_stop_action
10891base_breakpoint_print_it (bpstat bs)
10892{
10893 internal_error_pure_virtual_called ();
10894}
10895
10896static void
10897base_breakpoint_print_one_detail (const struct breakpoint *self,
10898 struct ui_out *uiout)
10899{
10900 /* nothing */
10901}
10902
10903static void
10904base_breakpoint_print_mention (struct breakpoint *b)
10905{
10906 internal_error_pure_virtual_called ();
10907}
10908
10909static void
10910base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
10911{
10912 internal_error_pure_virtual_called ();
10913}
10914
10915static struct breakpoint_ops base_breakpoint_ops =
10916{
10917 base_breakpoint_dtor,
10918 base_breakpoint_allocate_location,
10919 base_breakpoint_re_set,
10920 base_breakpoint_insert_location,
10921 base_breakpoint_remove_location,
10922 base_breakpoint_breakpoint_hit,
10923 base_breakpoint_check_status,
10924 base_breakpoint_resources_needed,
10925 base_breakpoint_works_in_software_mode,
10926 base_breakpoint_print_it,
10927 NULL,
10928 base_breakpoint_print_one_detail,
10929 base_breakpoint_print_mention,
10930 base_breakpoint_print_recreate
10931};
10932
10933/* Default breakpoint_ops methods. */
10934
10935static void
348d480f
PA
10936bkpt_re_set (struct breakpoint *b)
10937{
06edf0c0
PA
10938 /* FIXME: is this still reachable? */
10939 if (b->addr_string == NULL)
10940 {
10941 /* Anything without a string can't be re-set. */
348d480f 10942 delete_breakpoint (b);
06edf0c0 10943 return;
348d480f 10944 }
06edf0c0
PA
10945
10946 breakpoint_re_set_default (b);
348d480f
PA
10947}
10948
2060206e 10949static int
348d480f
PA
10950bkpt_insert_location (struct bp_location *bl)
10951{
10952 if (bl->loc_type == bp_loc_hardware_breakpoint)
10953 return target_insert_hw_breakpoint (bl->gdbarch,
10954 &bl->target_info);
10955 else
10956 return target_insert_breakpoint (bl->gdbarch,
10957 &bl->target_info);
10958}
10959
2060206e 10960static int
348d480f
PA
10961bkpt_remove_location (struct bp_location *bl)
10962{
10963 if (bl->loc_type == bp_loc_hardware_breakpoint)
10964 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
10965 else
10966 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
10967}
10968
2060206e 10969static int
348d480f
PA
10970bkpt_breakpoint_hit (const struct bp_location *bl,
10971 struct address_space *aspace, CORE_ADDR bp_addr)
10972{
10973 struct breakpoint *b = bl->owner;
10974
10975 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
10976 aspace, bp_addr))
10977 return 0;
10978
10979 if (overlay_debugging /* unmapped overlay section */
10980 && section_is_overlay (bl->section)
10981 && !section_is_mapped (bl->section))
10982 return 0;
10983
10984 return 1;
10985}
10986
2060206e 10987static int
348d480f
PA
10988bkpt_resources_needed (const struct bp_location *bl)
10989{
10990 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
10991
10992 return 1;
10993}
10994
2060206e 10995static enum print_stop_action
348d480f
PA
10996bkpt_print_it (bpstat bs)
10997{
348d480f
PA
10998 struct breakpoint *b;
10999 const struct bp_location *bl;
001c8c33 11000 int bp_temp;
79a45e25 11001 struct ui_out *uiout = current_uiout;
348d480f
PA
11002
11003 gdb_assert (bs->bp_location_at != NULL);
11004
11005 bl = bs->bp_location_at;
11006 b = bs->breakpoint_at;
11007
001c8c33
PA
11008 bp_temp = b->disposition == disp_del;
11009 if (bl->address != bl->requested_address)
11010 breakpoint_adjustment_warning (bl->requested_address,
11011 bl->address,
11012 b->number, 1);
11013 annotate_breakpoint (b->number);
11014 if (bp_temp)
11015 ui_out_text (uiout, "\nTemporary breakpoint ");
11016 else
11017 ui_out_text (uiout, "\nBreakpoint ");
11018 if (ui_out_is_mi_like_p (uiout))
348d480f 11019 {
001c8c33
PA
11020 ui_out_field_string (uiout, "reason",
11021 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11022 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 11023 }
001c8c33
PA
11024 ui_out_field_int (uiout, "bkptno", b->number);
11025 ui_out_text (uiout, ", ");
06edf0c0 11026
001c8c33 11027 return PRINT_SRC_AND_LOC;
06edf0c0
PA
11028}
11029
2060206e 11030static void
06edf0c0
PA
11031bkpt_print_mention (struct breakpoint *b)
11032{
79a45e25 11033 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
11034 return;
11035
11036 switch (b->type)
11037 {
11038 case bp_breakpoint:
11039 case bp_gnu_ifunc_resolver:
11040 if (b->disposition == disp_del)
11041 printf_filtered (_("Temporary breakpoint"));
11042 else
11043 printf_filtered (_("Breakpoint"));
11044 printf_filtered (_(" %d"), b->number);
11045 if (b->type == bp_gnu_ifunc_resolver)
11046 printf_filtered (_(" at gnu-indirect-function resolver"));
11047 break;
11048 case bp_hardware_breakpoint:
11049 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
11050 break;
11051 }
11052
11053 say_where (b);
11054}
11055
2060206e 11056static void
06edf0c0
PA
11057bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
11058{
11059 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11060 fprintf_unfiltered (fp, "tbreak");
11061 else if (tp->type == bp_breakpoint)
11062 fprintf_unfiltered (fp, "break");
11063 else if (tp->type == bp_hardware_breakpoint
11064 && tp->disposition == disp_del)
11065 fprintf_unfiltered (fp, "thbreak");
11066 else if (tp->type == bp_hardware_breakpoint)
11067 fprintf_unfiltered (fp, "hbreak");
11068 else
11069 internal_error (__FILE__, __LINE__,
11070 _("unhandled breakpoint type %d"), (int) tp->type);
11071
2060206e 11072 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 11073 print_recreate_thread (tp, fp);
06edf0c0
PA
11074}
11075
06edf0c0
PA
11076/* Virtual table for internal breakpoints. */
11077
11078static void
11079internal_bkpt_re_set (struct breakpoint *b)
11080{
11081 switch (b->type)
11082 {
11083 /* Delete overlay event and longjmp master breakpoints; they
11084 will be reset later by breakpoint_re_set. */
11085 case bp_overlay_event:
11086 case bp_longjmp_master:
11087 case bp_std_terminate_master:
11088 case bp_exception_master:
11089 delete_breakpoint (b);
11090 break;
11091
11092 /* This breakpoint is special, it's set up when the inferior
11093 starts and we really don't want to touch it. */
11094 case bp_shlib_event:
11095
11096 /* Like bp_shlib_event, this breakpoint type is special. Once
11097 it is set up, we do not want to touch it. */
11098 case bp_thread_event:
11099 break;
11100 }
11101}
11102
11103static void
11104internal_bkpt_check_status (bpstat bs)
11105{
11106 /* We do not stop for these. */
11107 bs->stop = 0;
11108}
11109
11110static enum print_stop_action
11111internal_bkpt_print_it (bpstat bs)
11112{
36dfb11c 11113 struct ui_out *uiout = current_uiout;
06edf0c0 11114 struct breakpoint *b;
06edf0c0 11115
06edf0c0
PA
11116 b = bs->breakpoint_at;
11117
06edf0c0
PA
11118 switch (b->type)
11119 {
348d480f
PA
11120 case bp_shlib_event:
11121 /* Did we stop because the user set the stop_on_solib_events
11122 variable? (If so, we report this as a generic, "Stopped due
11123 to shlib event" message.) */
36dfb11c
TT
11124 ui_out_text (uiout, _("Stopped due to shared library event\n"));
11125 if (ui_out_is_mi_like_p (uiout))
11126 ui_out_field_string (uiout, "reason",
11127 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
348d480f
PA
11128 break;
11129
11130 case bp_thread_event:
11131 /* Not sure how we will get here.
11132 GDB should not stop for these breakpoints. */
11133 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11134 break;
11135
11136 case bp_overlay_event:
11137 /* By analogy with the thread event, GDB should not stop for these. */
11138 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11139 break;
11140
11141 case bp_longjmp_master:
11142 /* These should never be enabled. */
11143 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11144 break;
11145
11146 case bp_std_terminate_master:
11147 /* These should never be enabled. */
11148 printf_filtered (_("std::terminate Master Breakpoint: "
11149 "gdb should not stop!\n"));
348d480f
PA
11150 break;
11151
11152 case bp_exception_master:
11153 /* These should never be enabled. */
11154 printf_filtered (_("Exception Master Breakpoint: "
11155 "gdb should not stop!\n"));
06edf0c0
PA
11156 break;
11157 }
11158
001c8c33 11159 return PRINT_NOTHING;
06edf0c0
PA
11160}
11161
11162static void
11163internal_bkpt_print_mention (struct breakpoint *b)
11164{
11165 /* Nothing to mention. These breakpoints are internal. */
11166}
11167
06edf0c0
PA
11168/* Virtual table for momentary breakpoints */
11169
11170static void
11171momentary_bkpt_re_set (struct breakpoint *b)
11172{
11173 /* Keep temporary breakpoints, which can be encountered when we step
11174 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
11175 Otherwise these should have been blown away via the cleanup chain
11176 or by breakpoint_init_inferior when we rerun the executable. */
11177}
11178
11179static void
11180momentary_bkpt_check_status (bpstat bs)
11181{
11182 /* Nothing. The point of these breakpoints is causing a stop. */
11183}
11184
11185static enum print_stop_action
11186momentary_bkpt_print_it (bpstat bs)
11187{
79a45e25
PA
11188 struct ui_out *uiout = current_uiout;
11189
001c8c33 11190 if (ui_out_is_mi_like_p (uiout))
06edf0c0 11191 {
001c8c33 11192 struct breakpoint *b = bs->breakpoint_at;
348d480f 11193
001c8c33
PA
11194 switch (b->type)
11195 {
11196 case bp_finish:
11197 ui_out_field_string
11198 (uiout, "reason",
11199 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
11200 break;
348d480f 11201
001c8c33
PA
11202 case bp_until:
11203 ui_out_field_string
11204 (uiout, "reason",
11205 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
11206 break;
11207 }
348d480f
PA
11208 }
11209
001c8c33 11210 return PRINT_UNKNOWN;
348d480f
PA
11211}
11212
06edf0c0
PA
11213static void
11214momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 11215{
06edf0c0 11216 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11217}
11218
348d480f 11219/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 11220
348d480f
PA
11221static void
11222tracepoint_re_set (struct breakpoint *b)
11223{
11224 breakpoint_re_set_default (b);
11225}
876fa593 11226
348d480f
PA
11227static int
11228tracepoint_breakpoint_hit (const struct bp_location *bl,
11229 struct address_space *aspace, CORE_ADDR bp_addr)
11230{
11231 /* By definition, the inferior does not report stops at
11232 tracepoints. */
11233 return 0;
74960c60
VP
11234}
11235
11236static void
348d480f
PA
11237tracepoint_print_one_detail (const struct breakpoint *self,
11238 struct ui_out *uiout)
74960c60 11239{
d9b3f62e
PA
11240 struct tracepoint *tp = (struct tracepoint *) self;
11241 if (tp->static_trace_marker_id)
348d480f
PA
11242 {
11243 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 11244
348d480f
PA
11245 ui_out_text (uiout, "\tmarker id is ");
11246 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 11247 tp->static_trace_marker_id);
348d480f
PA
11248 ui_out_text (uiout, "\n");
11249 }
0d381245
VP
11250}
11251
a474d7c2 11252static void
348d480f 11253tracepoint_print_mention (struct breakpoint *b)
a474d7c2 11254{
79a45e25 11255 if (ui_out_is_mi_like_p (current_uiout))
348d480f 11256 return;
cc59ec59 11257
348d480f
PA
11258 switch (b->type)
11259 {
11260 case bp_tracepoint:
11261 printf_filtered (_("Tracepoint"));
11262 printf_filtered (_(" %d"), b->number);
11263 break;
11264 case bp_fast_tracepoint:
11265 printf_filtered (_("Fast tracepoint"));
11266 printf_filtered (_(" %d"), b->number);
11267 break;
11268 case bp_static_tracepoint:
11269 printf_filtered (_("Static tracepoint"));
11270 printf_filtered (_(" %d"), b->number);
11271 break;
11272 default:
11273 internal_error (__FILE__, __LINE__,
11274 _("unhandled tracepoint type %d"), (int) b->type);
11275 }
11276
11277 say_where (b);
a474d7c2
PA
11278}
11279
348d480f 11280static void
d9b3f62e 11281tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 11282{
d9b3f62e
PA
11283 struct tracepoint *tp = (struct tracepoint *) self;
11284
11285 if (self->type == bp_fast_tracepoint)
348d480f 11286 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 11287 if (self->type == bp_static_tracepoint)
348d480f 11288 fprintf_unfiltered (fp, "strace");
d9b3f62e 11289 else if (self->type == bp_tracepoint)
348d480f
PA
11290 fprintf_unfiltered (fp, "trace");
11291 else
11292 internal_error (__FILE__, __LINE__,
d9b3f62e 11293 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 11294
d9b3f62e
PA
11295 fprintf_unfiltered (fp, " %s", self->addr_string);
11296 print_recreate_thread (self, fp);
11297
11298 if (tp->pass_count)
11299 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
11300}
11301
2060206e 11302struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 11303
53a5351d 11304/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 11305 structures. */
c906108c
SS
11306
11307void
fba45db2 11308delete_breakpoint (struct breakpoint *bpt)
c906108c 11309{
52f0bd74 11310 struct breakpoint *b;
c906108c 11311
8a3fe4f8 11312 gdb_assert (bpt != NULL);
c906108c 11313
4a64f543
MS
11314 /* Has this bp already been deleted? This can happen because
11315 multiple lists can hold pointers to bp's. bpstat lists are
11316 especial culprits.
11317
11318 One example of this happening is a watchpoint's scope bp. When
11319 the scope bp triggers, we notice that the watchpoint is out of
11320 scope, and delete it. We also delete its scope bp. But the
11321 scope bp is marked "auto-deleting", and is already on a bpstat.
11322 That bpstat is then checked for auto-deleting bp's, which are
11323 deleted.
11324
11325 A real solution to this problem might involve reference counts in
11326 bp's, and/or giving them pointers back to their referencing
11327 bpstat's, and teaching delete_breakpoint to only free a bp's
11328 storage when no more references were extent. A cheaper bandaid
11329 was chosen. */
c906108c
SS
11330 if (bpt->type == bp_none)
11331 return;
11332
4a64f543
MS
11333 /* At least avoid this stale reference until the reference counting
11334 of breakpoints gets resolved. */
d0fb5eae 11335 if (bpt->related_breakpoint != bpt)
e5a0a904 11336 {
d0fb5eae 11337 struct breakpoint *related;
3a5c3e22 11338 struct watchpoint *w;
d0fb5eae
JK
11339
11340 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 11341 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 11342 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
11343 w = (struct watchpoint *) bpt;
11344 else
11345 w = NULL;
11346 if (w != NULL)
11347 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
11348
11349 /* Unlink bpt from the bpt->related_breakpoint ring. */
11350 for (related = bpt; related->related_breakpoint != bpt;
11351 related = related->related_breakpoint);
11352 related->related_breakpoint = bpt->related_breakpoint;
11353 bpt->related_breakpoint = bpt;
e5a0a904
JK
11354 }
11355
a9634178
TJB
11356 /* watch_command_1 creates a watchpoint but only sets its number if
11357 update_watchpoint succeeds in creating its bp_locations. If there's
11358 a problem in that process, we'll be asked to delete the half-created
11359 watchpoint. In that case, don't announce the deletion. */
11360 if (bpt->number)
11361 observer_notify_breakpoint_deleted (bpt);
c906108c 11362
c906108c
SS
11363 if (breakpoint_chain == bpt)
11364 breakpoint_chain = bpt->next;
11365
c906108c
SS
11366 ALL_BREAKPOINTS (b)
11367 if (b->next == bpt)
c5aa993b
JM
11368 {
11369 b->next = bpt->next;
11370 break;
11371 }
c906108c 11372
f431efe5
PA
11373 /* Be sure no bpstat's are pointing at the breakpoint after it's
11374 been freed. */
11375 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 11376 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
11377 pointing at bpt from the stop_bpstat list entirely, as breakpoint
11378 commands are associated with the bpstat; if we remove it here,
11379 then the later call to bpstat_do_actions (&stop_bpstat); in
11380 event-top.c won't do anything, and temporary breakpoints with
11381 commands won't work. */
11382
11383 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11384
4a64f543
MS
11385 /* Now that breakpoint is removed from breakpoint list, update the
11386 global location list. This will remove locations that used to
11387 belong to this breakpoint. Do this before freeing the breakpoint
11388 itself, since remove_breakpoint looks at location's owner. It
11389 might be better design to have location completely
11390 self-contained, but it's not the case now. */
b60e7edf 11391 update_global_location_list (0);
74960c60 11392
348d480f 11393 bpt->ops->dtor (bpt);
4a64f543
MS
11394 /* On the chance that someone will soon try again to delete this
11395 same bp, we mark it as deleted before freeing its storage. */
c906108c 11396 bpt->type = bp_none;
b8c9b27d 11397 xfree (bpt);
c906108c
SS
11398}
11399
4d6140d9
AC
11400static void
11401do_delete_breakpoint_cleanup (void *b)
11402{
11403 delete_breakpoint (b);
11404}
11405
11406struct cleanup *
11407make_cleanup_delete_breakpoint (struct breakpoint *b)
11408{
11409 return make_cleanup (do_delete_breakpoint_cleanup, b);
11410}
11411
51be5b68
PA
11412/* Iterator function to call a user-provided callback function once
11413 for each of B and its related breakpoints. */
11414
11415static void
11416iterate_over_related_breakpoints (struct breakpoint *b,
11417 void (*function) (struct breakpoint *,
11418 void *),
11419 void *data)
11420{
11421 struct breakpoint *related;
11422
11423 related = b;
11424 do
11425 {
11426 struct breakpoint *next;
11427
11428 /* FUNCTION may delete RELATED. */
11429 next = related->related_breakpoint;
11430
11431 if (next == related)
11432 {
11433 /* RELATED is the last ring entry. */
11434 function (related, data);
11435
11436 /* FUNCTION may have deleted it, so we'd never reach back to
11437 B. There's nothing left to do anyway, so just break
11438 out. */
11439 break;
11440 }
11441 else
11442 function (related, data);
11443
11444 related = next;
11445 }
11446 while (related != b);
11447}
95a42b64
TT
11448
11449static void
11450do_delete_breakpoint (struct breakpoint *b, void *ignore)
11451{
11452 delete_breakpoint (b);
11453}
11454
51be5b68
PA
11455/* A callback for map_breakpoint_numbers that calls
11456 delete_breakpoint. */
11457
11458static void
11459do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
11460{
11461 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
11462}
11463
c906108c 11464void
fba45db2 11465delete_command (char *arg, int from_tty)
c906108c 11466{
35df4500 11467 struct breakpoint *b, *b_tmp;
c906108c 11468
ea9365bb
TT
11469 dont_repeat ();
11470
c906108c
SS
11471 if (arg == 0)
11472 {
11473 int breaks_to_delete = 0;
11474
46c6471b
PA
11475 /* Delete all breakpoints if no argument. Do not delete
11476 internal breakpoints, these have to be deleted with an
11477 explicit breakpoint number argument. */
c5aa993b 11478 ALL_BREAKPOINTS (b)
46c6471b 11479 if (user_breakpoint_p (b))
973d738b
DJ
11480 {
11481 breaks_to_delete = 1;
11482 break;
11483 }
c906108c
SS
11484
11485 /* Ask user only if there are some breakpoints to delete. */
11486 if (!from_tty
e2e0b3e5 11487 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 11488 {
35df4500 11489 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 11490 if (user_breakpoint_p (b))
c5aa993b 11491 delete_breakpoint (b);
c906108c
SS
11492 }
11493 }
11494 else
51be5b68 11495 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
11496}
11497
0d381245
VP
11498static int
11499all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 11500{
0d381245
VP
11501 for (; loc; loc = loc->next)
11502 if (!loc->shlib_disabled)
11503 return 0;
11504 return 1;
fe3f5fa8
VP
11505}
11506
776592bf
DE
11507/* Subroutine of update_breakpoint_locations to simplify it.
11508 Return non-zero if multiple fns in list LOC have the same name.
11509 Null names are ignored. */
11510
11511static int
11512ambiguous_names_p (struct bp_location *loc)
11513{
11514 struct bp_location *l;
11515 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
11516 (int (*) (const void *,
11517 const void *)) streq,
776592bf
DE
11518 NULL, xcalloc, xfree);
11519
11520 for (l = loc; l != NULL; l = l->next)
11521 {
11522 const char **slot;
11523 const char *name = l->function_name;
11524
11525 /* Allow for some names to be NULL, ignore them. */
11526 if (name == NULL)
11527 continue;
11528
11529 slot = (const char **) htab_find_slot (htab, (const void *) name,
11530 INSERT);
4a64f543
MS
11531 /* NOTE: We can assume slot != NULL here because xcalloc never
11532 returns NULL. */
776592bf
DE
11533 if (*slot != NULL)
11534 {
11535 htab_delete (htab);
11536 return 1;
11537 }
11538 *slot = name;
11539 }
11540
11541 htab_delete (htab);
11542 return 0;
11543}
11544
0fb4aa4b
PA
11545/* When symbols change, it probably means the sources changed as well,
11546 and it might mean the static tracepoint markers are no longer at
11547 the same address or line numbers they used to be at last we
11548 checked. Losing your static tracepoints whenever you rebuild is
11549 undesirable. This function tries to resync/rematch gdb static
11550 tracepoints with the markers on the target, for static tracepoints
11551 that have not been set by marker id. Static tracepoint that have
11552 been set by marker id are reset by marker id in breakpoint_re_set.
11553 The heuristic is:
11554
11555 1) For a tracepoint set at a specific address, look for a marker at
11556 the old PC. If one is found there, assume to be the same marker.
11557 If the name / string id of the marker found is different from the
11558 previous known name, assume that means the user renamed the marker
11559 in the sources, and output a warning.
11560
11561 2) For a tracepoint set at a given line number, look for a marker
11562 at the new address of the old line number. If one is found there,
11563 assume to be the same marker. If the name / string id of the
11564 marker found is different from the previous known name, assume that
11565 means the user renamed the marker in the sources, and output a
11566 warning.
11567
11568 3) If a marker is no longer found at the same address or line, it
11569 may mean the marker no longer exists. But it may also just mean
11570 the code changed a bit. Maybe the user added a few lines of code
11571 that made the marker move up or down (in line number terms). Ask
11572 the target for info about the marker with the string id as we knew
11573 it. If found, update line number and address in the matching
11574 static tracepoint. This will get confused if there's more than one
11575 marker with the same ID (possible in UST, although unadvised
11576 precisely because it confuses tools). */
11577
11578static struct symtab_and_line
11579update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11580{
d9b3f62e 11581 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
11582 struct static_tracepoint_marker marker;
11583 CORE_ADDR pc;
11584 int i;
11585
11586 pc = sal.pc;
11587 if (sal.line)
11588 find_line_pc (sal.symtab, sal.line, &pc);
11589
11590 if (target_static_tracepoint_marker_at (pc, &marker))
11591 {
d9b3f62e 11592 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
11593 warning (_("static tracepoint %d changed probed marker from %s to %s"),
11594 b->number,
d9b3f62e 11595 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 11596
d9b3f62e
PA
11597 xfree (tp->static_trace_marker_id);
11598 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
11599 release_static_tracepoint_marker (&marker);
11600
11601 return sal;
11602 }
11603
11604 /* Old marker wasn't found on target at lineno. Try looking it up
11605 by string ID. */
11606 if (!sal.explicit_pc
11607 && sal.line != 0
11608 && sal.symtab != NULL
d9b3f62e 11609 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
11610 {
11611 VEC(static_tracepoint_marker_p) *markers;
11612
11613 markers
d9b3f62e 11614 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
11615
11616 if (!VEC_empty(static_tracepoint_marker_p, markers))
11617 {
80e1d417 11618 struct symtab_and_line sal2;
0fb4aa4b 11619 struct symbol *sym;
80e1d417 11620 struct static_tracepoint_marker *tpmarker;
79a45e25 11621 struct ui_out *uiout = current_uiout;
0fb4aa4b 11622
80e1d417 11623 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 11624
d9b3f62e 11625 xfree (tp->static_trace_marker_id);
80e1d417 11626 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
11627
11628 warning (_("marker for static tracepoint %d (%s) not "
11629 "found at previous line number"),
d9b3f62e 11630 b->number, tp->static_trace_marker_id);
0fb4aa4b 11631
80e1d417 11632 init_sal (&sal2);
0fb4aa4b 11633
80e1d417 11634 sal2.pc = tpmarker->address;
0fb4aa4b 11635
80e1d417
AS
11636 sal2 = find_pc_line (tpmarker->address, 0);
11637 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
11638 ui_out_text (uiout, "Now in ");
11639 if (sym)
11640 {
11641 ui_out_field_string (uiout, "func",
11642 SYMBOL_PRINT_NAME (sym));
11643 ui_out_text (uiout, " at ");
11644 }
80e1d417 11645 ui_out_field_string (uiout, "file", sal2.symtab->filename);
0fb4aa4b
PA
11646 ui_out_text (uiout, ":");
11647
11648 if (ui_out_is_mi_like_p (uiout))
11649 {
80e1d417 11650 char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b
PA
11651
11652 if (fullname)
11653 ui_out_field_string (uiout, "fullname", fullname);
11654 }
11655
80e1d417 11656 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
11657 ui_out_text (uiout, "\n");
11658
80e1d417 11659 b->loc->line_number = sal2.line;
0fb4aa4b 11660
f8eba3c6 11661 xfree (b->loc->source_file);
0fb4aa4b 11662 if (sym)
80e1d417 11663 b->loc->source_file = xstrdup (sal2.symtab->filename);
0fb4aa4b 11664 else
f8eba3c6 11665 b->loc->source_file = NULL;
0fb4aa4b
PA
11666
11667 xfree (b->addr_string);
11668 b->addr_string = xstrprintf ("%s:%d",
80e1d417 11669 sal2.symtab->filename,
f8eba3c6 11670 b->loc->line_number);
0fb4aa4b
PA
11671
11672 /* Might be nice to check if function changed, and warn if
11673 so. */
11674
80e1d417 11675 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
11676 }
11677 }
11678 return sal;
11679}
11680
8d3788bd
VP
11681/* Returns 1 iff locations A and B are sufficiently same that
11682 we don't need to report breakpoint as changed. */
11683
11684static int
11685locations_are_equal (struct bp_location *a, struct bp_location *b)
11686{
11687 while (a && b)
11688 {
11689 if (a->address != b->address)
11690 return 0;
11691
11692 if (a->shlib_disabled != b->shlib_disabled)
11693 return 0;
11694
11695 if (a->enabled != b->enabled)
11696 return 0;
11697
11698 a = a->next;
11699 b = b->next;
11700 }
11701
11702 if ((a == NULL) != (b == NULL))
11703 return 0;
11704
11705 return 1;
11706}
11707
f1310107
TJB
11708/* Create new breakpoint locations for B (a hardware or software breakpoint)
11709 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
11710 a ranged breakpoint. */
11711
0e30163f 11712void
0d381245 11713update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
11714 struct symtabs_and_lines sals,
11715 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
11716{
11717 int i;
0d381245
VP
11718 struct bp_location *existing_locations = b->loc;
11719
f8eba3c6
TT
11720 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
11721 {
11722 /* Ranged breakpoints have only one start location and one end
11723 location. */
11724 b->enable_state = bp_disabled;
11725 update_global_location_list (1);
11726 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
11727 "multiple locations found\n"),
11728 b->number);
11729 return;
11730 }
f1310107 11731
4a64f543
MS
11732 /* If there's no new locations, and all existing locations are
11733 pending, don't do anything. This optimizes the common case where
11734 all locations are in the same shared library, that was unloaded.
11735 We'd like to retain the location, so that when the library is
11736 loaded again, we don't loose the enabled/disabled status of the
11737 individual locations. */
0d381245 11738 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
11739 return;
11740
fe3f5fa8
VP
11741 b->loc = NULL;
11742
0d381245 11743 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 11744 {
f8eba3c6
TT
11745 struct bp_location *new_loc;
11746
11747 switch_to_program_space_and_thread (sals.sals[i].pspace);
11748
11749 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 11750
0d381245
VP
11751 /* Reparse conditions, they might contain references to the
11752 old symtab. */
11753 if (b->cond_string != NULL)
11754 {
f1310107 11755 char *s;
0d381245 11756 struct gdb_exception e;
fe3f5fa8 11757
0d381245
VP
11758 s = b->cond_string;
11759 TRY_CATCH (e, RETURN_MASK_ERROR)
11760 {
11761 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
11762 0);
11763 }
11764 if (e.reason < 0)
11765 {
3e43a32a
MS
11766 warning (_("failed to reevaluate condition "
11767 "for breakpoint %d: %s"),
0d381245
VP
11768 b->number, e.message);
11769 new_loc->enabled = 0;
11770 }
11771 }
fe3f5fa8 11772
f1310107
TJB
11773 if (sals_end.nelts)
11774 {
11775 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11776
11777 new_loc->length = end - sals.sals[0].pc + 1;
11778 }
0d381245 11779 }
fe3f5fa8 11780
514f746b
AR
11781 /* Update locations of permanent breakpoints. */
11782 if (b->enable_state == bp_permanent)
11783 make_breakpoint_permanent (b);
11784
4a64f543
MS
11785 /* If possible, carry over 'disable' status from existing
11786 breakpoints. */
0d381245
VP
11787 {
11788 struct bp_location *e = existing_locations;
776592bf
DE
11789 /* If there are multiple breakpoints with the same function name,
11790 e.g. for inline functions, comparing function names won't work.
11791 Instead compare pc addresses; this is just a heuristic as things
11792 may have moved, but in practice it gives the correct answer
11793 often enough until a better solution is found. */
11794 int have_ambiguous_names = ambiguous_names_p (b->loc);
11795
0d381245
VP
11796 for (; e; e = e->next)
11797 {
11798 if (!e->enabled && e->function_name)
11799 {
11800 struct bp_location *l = b->loc;
776592bf
DE
11801 if (have_ambiguous_names)
11802 {
11803 for (; l; l = l->next)
f1310107 11804 if (breakpoint_locations_match (e, l))
776592bf
DE
11805 {
11806 l->enabled = 0;
11807 break;
11808 }
11809 }
11810 else
11811 {
11812 for (; l; l = l->next)
11813 if (l->function_name
11814 && strcmp (e->function_name, l->function_name) == 0)
11815 {
11816 l->enabled = 0;
11817 break;
11818 }
11819 }
0d381245
VP
11820 }
11821 }
11822 }
fe3f5fa8 11823
8d3788bd
VP
11824 if (!locations_are_equal (existing_locations, b->loc))
11825 observer_notify_breakpoint_modified (b);
11826
b60e7edf 11827 update_global_location_list (1);
fe3f5fa8
VP
11828}
11829
ef23e705
TJB
11830/* Find the SaL locations corresponding to the given ADDR_STRING.
11831 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
11832
11833static struct symtabs_and_lines
11834addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11835{
11836 char *s;
58438ac1 11837 int marker_spec;
02d20e4a 11838 struct symtabs_and_lines sals = {0};
f8eba3c6 11839 volatile struct gdb_exception e;
ef23e705
TJB
11840
11841 s = addr_string;
11842 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11843
11844 TRY_CATCH (e, RETURN_MASK_ERROR)
11845 {
11846 if (marker_spec)
11847 {
d9b3f62e
PA
11848 struct tracepoint *tp = (struct tracepoint *) b;
11849
ef23e705 11850 sals = decode_static_tracepoint_spec (&s);
d9b3f62e 11851 if (sals.nelts > tp->static_trace_marker_id_idx)
ef23e705 11852 {
d9b3f62e 11853 sals.sals[0] = sals.sals[tp->static_trace_marker_id_idx];
ef23e705
TJB
11854 sals.nelts = 1;
11855 }
11856 else
d9b3f62e 11857 error (_("marker %s not found"), tp->static_trace_marker_id);
ef23e705
TJB
11858 }
11859 else
f8eba3c6
TT
11860 {
11861 struct linespec_result canonical;
11862
11863 init_linespec_result (&canonical);
11864 decode_line_full (&s, DECODE_LINE_FUNFIRSTLINE,
11865 (struct symtab *) NULL, 0,
11866 &canonical, multiple_symbols_all,
11867 b->filter);
11868
11869 /* We should get 0 or 1 resulting SALs. */
11870 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
11871
11872 if (VEC_length (linespec_sals, canonical.sals) > 0)
11873 {
11874 struct linespec_sals *lsal;
11875
11876 lsal = VEC_index (linespec_sals, canonical.sals, 0);
11877 sals = lsal->sals;
11878 /* Arrange it so the destructor does not free the
11879 contents. */
11880 lsal->sals.sals = NULL;
11881 }
11882
11883 destroy_linespec_result (&canonical);
11884 }
ef23e705
TJB
11885 }
11886 if (e.reason < 0)
11887 {
11888 int not_found_and_ok = 0;
11889 /* For pending breakpoints, it's expected that parsing will
11890 fail until the right shared library is loaded. User has
11891 already told to create pending breakpoints and don't need
11892 extra messages. If breakpoint is in bp_shlib_disabled
11893 state, then user already saw the message about that
11894 breakpoint being disabled, and don't want to see more
11895 errors. */
58438ac1 11896 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
11897 && (b->condition_not_parsed
11898 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 11899 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
11900 || b->enable_state == bp_disabled))
11901 not_found_and_ok = 1;
11902
11903 if (!not_found_and_ok)
11904 {
11905 /* We surely don't want to warn about the same breakpoint
11906 10 times. One solution, implemented here, is disable
11907 the breakpoint on error. Another solution would be to
11908 have separate 'warning emitted' flag. Since this
11909 happens only when a binary has changed, I don't know
11910 which approach is better. */
11911 b->enable_state = bp_disabled;
11912 throw_exception (e);
11913 }
11914 }
11915
58438ac1 11916 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 11917 {
f8eba3c6 11918 int i;
ef23e705 11919
f8eba3c6
TT
11920 for (i = 0; i < sals.nelts; ++i)
11921 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
11922 if (b->condition_not_parsed && s && s[0])
11923 {
11924 char *cond_string = 0;
11925 int thread = -1;
11926 int task = 0;
11927
11928 find_condition_and_thread (s, sals.sals[0].pc,
11929 &cond_string, &thread, &task);
11930 if (cond_string)
11931 b->cond_string = cond_string;
11932 b->thread = thread;
11933 b->task = task;
11934 b->condition_not_parsed = 0;
11935 }
11936
11937 if (b->type == bp_static_tracepoint && !marker_spec)
11938 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 11939
58438ac1
TT
11940 *found = 1;
11941 }
11942 else
11943 *found = 0;
ef23e705
TJB
11944
11945 return sals;
11946}
11947
348d480f
PA
11948/* The default re_set method, for typical hardware or software
11949 breakpoints. Reevaluate the breakpoint and recreate its
11950 locations. */
11951
11952static void
28010a5d 11953breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
11954{
11955 int found;
f1310107 11956 struct symtabs_and_lines sals, sals_end;
ef23e705 11957 struct symtabs_and_lines expanded = {0};
f1310107 11958 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
11959
11960 sals = addr_string_to_sals (b, b->addr_string, &found);
11961 if (found)
11962 {
11963 make_cleanup (xfree, sals.sals);
f8eba3c6 11964 expanded = sals;
ef23e705
TJB
11965 }
11966
f1310107
TJB
11967 if (b->addr_string_range_end)
11968 {
11969 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11970 if (found)
11971 {
11972 make_cleanup (xfree, sals_end.sals);
f8eba3c6 11973 expanded_end = sals_end;
f1310107
TJB
11974 }
11975 }
11976
11977 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
11978}
11979
11980/* Prepare the global context for a re-set of breakpoint B. */
11981
11982static struct cleanup *
11983prepare_re_set_context (struct breakpoint *b)
11984{
11985 struct cleanup *cleanups;
11986
11987 input_radix = b->input_radix;
11988 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
11989 if (b->pspace != NULL)
11990 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
11991 set_language (b->language);
11992
11993 return cleanups;
ef23e705
TJB
11994}
11995
c906108c
SS
11996/* Reset a breakpoint given it's struct breakpoint * BINT.
11997 The value we return ends up being the return value from catch_errors.
11998 Unused in this case. */
11999
12000static int
4efb68b1 12001breakpoint_re_set_one (void *bint)
c906108c 12002{
4a64f543 12003 /* Get past catch_errs. */
53a5351d 12004 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 12005 struct cleanup *cleanups;
c906108c 12006
348d480f
PA
12007 cleanups = prepare_re_set_context (b);
12008 b->ops->re_set (b);
12009 do_cleanups (cleanups);
c906108c
SS
12010 return 0;
12011}
12012
69de3c6a 12013/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 12014void
69de3c6a 12015breakpoint_re_set (void)
c906108c 12016{
35df4500 12017 struct breakpoint *b, *b_tmp;
c906108c
SS
12018 enum language save_language;
12019 int save_input_radix;
6c95b8df 12020 struct cleanup *old_chain;
c5aa993b 12021
c906108c
SS
12022 save_language = current_language->la_language;
12023 save_input_radix = input_radix;
6c95b8df
PA
12024 old_chain = save_current_program_space ();
12025
35df4500 12026 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12027 {
4a64f543 12028 /* Format possible error msg. */
fe3f5fa8 12029 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
12030 b->number);
12031 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 12032 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 12033 do_cleanups (cleanups);
c5aa993b 12034 }
c906108c
SS
12035 set_language (save_language);
12036 input_radix = save_input_radix;
e62c965a 12037
0756c555 12038 jit_breakpoint_re_set ();
4efc6507 12039
6c95b8df
PA
12040 do_cleanups (old_chain);
12041
af02033e
PP
12042 create_overlay_event_breakpoint ();
12043 create_longjmp_master_breakpoint ();
12044 create_std_terminate_master_breakpoint ();
186c406b 12045 create_exception_master_breakpoint ();
1bfeeb0f
JL
12046
12047 /* While we're at it, reset the skip list too. */
12048 skip_re_set ();
c906108c
SS
12049}
12050\f
c906108c
SS
12051/* Reset the thread number of this breakpoint:
12052
12053 - If the breakpoint is for all threads, leave it as-is.
4a64f543 12054 - Else, reset it to the current thread for inferior_ptid. */
c906108c 12055void
fba45db2 12056breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
12057{
12058 if (b->thread != -1)
12059 {
39f77062
KB
12060 if (in_thread_list (inferior_ptid))
12061 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
12062
12063 /* We're being called after following a fork. The new fork is
12064 selected as current, and unless this was a vfork will have a
12065 different program space from the original thread. Reset that
12066 as well. */
12067 b->loc->pspace = current_program_space;
c906108c
SS
12068 }
12069}
12070
03ac34d5
MS
12071/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12072 If from_tty is nonzero, it prints a message to that effect,
12073 which ends with a period (no newline). */
12074
c906108c 12075void
fba45db2 12076set_ignore_count (int bptnum, int count, int from_tty)
c906108c 12077{
52f0bd74 12078 struct breakpoint *b;
c906108c
SS
12079
12080 if (count < 0)
12081 count = 0;
12082
12083 ALL_BREAKPOINTS (b)
12084 if (b->number == bptnum)
c5aa993b 12085 {
d77f58be
SS
12086 if (is_tracepoint (b))
12087 {
12088 if (from_tty && count != 0)
12089 printf_filtered (_("Ignore count ignored for tracepoint %d."),
12090 bptnum);
12091 return;
12092 }
12093
c5aa993b 12094 b->ignore_count = count;
221ea385
KS
12095 if (from_tty)
12096 {
12097 if (count == 0)
3e43a32a
MS
12098 printf_filtered (_("Will stop next time "
12099 "breakpoint %d is reached."),
221ea385
KS
12100 bptnum);
12101 else if (count == 1)
a3f17187 12102 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
12103 bptnum);
12104 else
3e43a32a
MS
12105 printf_filtered (_("Will ignore next %d "
12106 "crossings of breakpoint %d."),
221ea385
KS
12107 count, bptnum);
12108 }
c5aa993b 12109 breakpoints_changed ();
8d3788bd 12110 observer_notify_breakpoint_modified (b);
c5aa993b
JM
12111 return;
12112 }
c906108c 12113
8a3fe4f8 12114 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
12115}
12116
c906108c
SS
12117/* Command to set ignore-count of breakpoint N to COUNT. */
12118
12119static void
fba45db2 12120ignore_command (char *args, int from_tty)
c906108c
SS
12121{
12122 char *p = args;
52f0bd74 12123 int num;
c906108c
SS
12124
12125 if (p == 0)
e2e0b3e5 12126 error_no_arg (_("a breakpoint number"));
c5aa993b 12127
c906108c 12128 num = get_number (&p);
5c44784c 12129 if (num == 0)
8a3fe4f8 12130 error (_("bad breakpoint number: '%s'"), args);
c906108c 12131 if (*p == 0)
8a3fe4f8 12132 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
12133
12134 set_ignore_count (num,
12135 longest_to_int (value_as_long (parse_and_eval (p))),
12136 from_tty);
221ea385
KS
12137 if (from_tty)
12138 printf_filtered ("\n");
c906108c
SS
12139}
12140\f
12141/* Call FUNCTION on each of the breakpoints
12142 whose numbers are given in ARGS. */
12143
12144static void
95a42b64
TT
12145map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
12146 void *),
12147 void *data)
c906108c 12148{
52f0bd74
AC
12149 int num;
12150 struct breakpoint *b, *tmp;
11cf8741 12151 int match;
197f0a60 12152 struct get_number_or_range_state state;
c906108c 12153
197f0a60 12154 if (args == 0)
e2e0b3e5 12155 error_no_arg (_("one or more breakpoint numbers"));
c906108c 12156
197f0a60
TT
12157 init_number_or_range (&state, args);
12158
12159 while (!state.finished)
c906108c 12160 {
197f0a60
TT
12161 char *p = state.string;
12162
11cf8741 12163 match = 0;
c5aa993b 12164
197f0a60 12165 num = get_number_or_range (&state);
5c44784c 12166 if (num == 0)
c5aa993b 12167 {
8a3fe4f8 12168 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
12169 }
12170 else
12171 {
12172 ALL_BREAKPOINTS_SAFE (b, tmp)
12173 if (b->number == num)
12174 {
11cf8741 12175 match = 1;
cdac0397 12176 function (b, data);
11cf8741 12177 break;
5c44784c 12178 }
11cf8741 12179 if (match == 0)
a3f17187 12180 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 12181 }
c906108c
SS
12182 }
12183}
12184
0d381245
VP
12185static struct bp_location *
12186find_location_by_number (char *number)
12187{
12188 char *dot = strchr (number, '.');
12189 char *p1;
12190 int bp_num;
12191 int loc_num;
12192 struct breakpoint *b;
12193 struct bp_location *loc;
12194
12195 *dot = '\0';
12196
12197 p1 = number;
197f0a60 12198 bp_num = get_number (&p1);
0d381245
VP
12199 if (bp_num == 0)
12200 error (_("Bad breakpoint number '%s'"), number);
12201
12202 ALL_BREAKPOINTS (b)
12203 if (b->number == bp_num)
12204 {
12205 break;
12206 }
12207
12208 if (!b || b->number != bp_num)
12209 error (_("Bad breakpoint number '%s'"), number);
12210
12211 p1 = dot+1;
197f0a60 12212 loc_num = get_number (&p1);
0d381245
VP
12213 if (loc_num == 0)
12214 error (_("Bad breakpoint location number '%s'"), number);
12215
12216 --loc_num;
12217 loc = b->loc;
12218 for (;loc_num && loc; --loc_num, loc = loc->next)
12219 ;
12220 if (!loc)
12221 error (_("Bad breakpoint location number '%s'"), dot+1);
12222
12223 return loc;
12224}
12225
12226
1900040c
MS
12227/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12228 If from_tty is nonzero, it prints a message to that effect,
12229 which ends with a period (no newline). */
12230
c906108c 12231void
fba45db2 12232disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
12233{
12234 /* Never disable a watchpoint scope breakpoint; we want to
12235 hit them when we leave scope so we can delete both the
12236 watchpoint and its scope breakpoint at that time. */
12237 if (bpt->type == bp_watchpoint_scope)
12238 return;
12239
c2c6d25f 12240 /* You can't disable permanent breakpoints. */
b5de0fa7 12241 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
12242 return;
12243
b5de0fa7 12244 bpt->enable_state = bp_disabled;
c906108c 12245
d248b706
KY
12246 if (target_supports_enable_disable_tracepoint ()
12247 && current_trace_status ()->running && is_tracepoint (bpt))
12248 {
12249 struct bp_location *location;
12250
12251 for (location = bpt->loc; location; location = location->next)
12252 target_disable_tracepoint (location);
12253 }
12254
b60e7edf 12255 update_global_location_list (0);
c906108c 12256
8d3788bd 12257 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12258}
12259
51be5b68
PA
12260/* A callback for iterate_over_related_breakpoints. */
12261
12262static void
12263do_disable_breakpoint (struct breakpoint *b, void *ignore)
12264{
12265 disable_breakpoint (b);
12266}
12267
95a42b64
TT
12268/* A callback for map_breakpoint_numbers that calls
12269 disable_breakpoint. */
12270
12271static void
12272do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
12273{
51be5b68 12274 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
12275}
12276
c906108c 12277static void
fba45db2 12278disable_command (char *args, int from_tty)
c906108c 12279{
c906108c 12280 if (args == 0)
46c6471b
PA
12281 {
12282 struct breakpoint *bpt;
12283
12284 ALL_BREAKPOINTS (bpt)
12285 if (user_breakpoint_p (bpt))
12286 disable_breakpoint (bpt);
12287 }
0d381245
VP
12288 else if (strchr (args, '.'))
12289 {
12290 struct bp_location *loc = find_location_by_number (args);
12291 if (loc)
d248b706
KY
12292 {
12293 loc->enabled = 0;
12294 if (target_supports_enable_disable_tracepoint ()
12295 && current_trace_status ()->running && loc->owner
12296 && is_tracepoint (loc->owner))
12297 target_disable_tracepoint (loc);
12298 }
b60e7edf 12299 update_global_location_list (0);
0d381245 12300 }
c906108c 12301 else
95a42b64 12302 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
12303}
12304
12305static void
51be5b68 12306enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 12307{
afe38095 12308 int target_resources_ok;
c906108c
SS
12309
12310 if (bpt->type == bp_hardware_breakpoint)
12311 {
12312 int i;
c5aa993b 12313 i = hw_breakpoint_used_count ();
53a5351d 12314 target_resources_ok =
d92524f1 12315 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 12316 i + 1, 0);
c906108c 12317 if (target_resources_ok == 0)
8a3fe4f8 12318 error (_("No hardware breakpoint support in the target."));
c906108c 12319 else if (target_resources_ok < 0)
8a3fe4f8 12320 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
12321 }
12322
cc60f2e3 12323 if (is_watchpoint (bpt))
c906108c 12324 {
d07205c2
JK
12325 /* Initialize it just to avoid a GCC false warning. */
12326 enum enable_state orig_enable_state = 0;
dde02812
ES
12327 struct gdb_exception e;
12328
12329 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 12330 {
3a5c3e22
PA
12331 struct watchpoint *w = (struct watchpoint *) bpt;
12332
1e718ff1
TJB
12333 orig_enable_state = bpt->enable_state;
12334 bpt->enable_state = bp_enabled;
3a5c3e22 12335 update_watchpoint (w, 1 /* reparse */);
c906108c 12336 }
dde02812 12337 if (e.reason < 0)
c5aa993b 12338 {
1e718ff1 12339 bpt->enable_state = orig_enable_state;
dde02812
ES
12340 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12341 bpt->number);
12342 return;
c5aa993b 12343 }
c906108c 12344 }
0101ce28 12345
b4c291bb
KH
12346 if (bpt->enable_state != bp_permanent)
12347 bpt->enable_state = bp_enabled;
d248b706
KY
12348
12349 if (target_supports_enable_disable_tracepoint ()
12350 && current_trace_status ()->running && is_tracepoint (bpt))
12351 {
12352 struct bp_location *location;
12353
12354 for (location = bpt->loc; location; location = location->next)
12355 target_enable_tracepoint (location);
12356 }
12357
b4c291bb 12358 bpt->disposition = disposition;
b60e7edf 12359 update_global_location_list (1);
b4c291bb
KH
12360 breakpoints_changed ();
12361
8d3788bd 12362 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12363}
12364
fe3f5fa8 12365
c906108c 12366void
fba45db2 12367enable_breakpoint (struct breakpoint *bpt)
c906108c 12368{
51be5b68
PA
12369 enable_breakpoint_disp (bpt, bpt->disposition);
12370}
12371
12372static void
12373do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12374{
12375 enable_breakpoint (bpt);
c906108c
SS
12376}
12377
95a42b64
TT
12378/* A callback for map_breakpoint_numbers that calls
12379 enable_breakpoint. */
12380
12381static void
12382do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12383{
51be5b68 12384 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
12385}
12386
c906108c
SS
12387/* The enable command enables the specified breakpoints (or all defined
12388 breakpoints) so they once again become (or continue to be) effective
1272ad14 12389 in stopping the inferior. */
c906108c 12390
c906108c 12391static void
fba45db2 12392enable_command (char *args, int from_tty)
c906108c 12393{
c906108c 12394 if (args == 0)
46c6471b
PA
12395 {
12396 struct breakpoint *bpt;
12397
12398 ALL_BREAKPOINTS (bpt)
12399 if (user_breakpoint_p (bpt))
12400 enable_breakpoint (bpt);
12401 }
0d381245
VP
12402 else if (strchr (args, '.'))
12403 {
12404 struct bp_location *loc = find_location_by_number (args);
12405 if (loc)
d248b706
KY
12406 {
12407 loc->enabled = 1;
12408 if (target_supports_enable_disable_tracepoint ()
12409 && current_trace_status ()->running && loc->owner
12410 && is_tracepoint (loc->owner))
12411 target_enable_tracepoint (loc);
12412 }
b60e7edf 12413 update_global_location_list (1);
0d381245 12414 }
c906108c 12415 else
95a42b64 12416 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
12417}
12418
12419static void
51be5b68
PA
12420do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12421{
12422 enum bpdisp disp = *(enum bpdisp *) arg;
12423
12424 enable_breakpoint_disp (bpt, disp);
12425}
12426
12427static void
12428do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12429{
51be5b68
PA
12430 enum bpdisp disp = disp_disable;
12431
12432 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12433}
12434
c906108c 12435static void
fba45db2 12436enable_once_command (char *args, int from_tty)
c906108c 12437{
51be5b68 12438 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
12439}
12440
12441static void
51be5b68 12442do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12443{
51be5b68
PA
12444 enum bpdisp disp = disp_del;
12445
12446 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12447}
12448
c906108c 12449static void
fba45db2 12450enable_delete_command (char *args, int from_tty)
c906108c 12451{
51be5b68 12452 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
12453}
12454\f
fa8d40ab
JJ
12455static void
12456set_breakpoint_cmd (char *args, int from_tty)
12457{
12458}
12459
12460static void
12461show_breakpoint_cmd (char *args, int from_tty)
12462{
12463}
12464
1f3b5d1b
PP
12465/* Invalidate last known value of any hardware watchpoint if
12466 the memory which that value represents has been written to by
12467 GDB itself. */
12468
12469static void
12470invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12471 const bfd_byte *data)
12472{
12473 struct breakpoint *bp;
12474
12475 ALL_BREAKPOINTS (bp)
12476 if (bp->enable_state == bp_enabled
3a5c3e22 12477 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 12478 {
3a5c3e22 12479 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 12480
3a5c3e22
PA
12481 if (wp->val_valid && wp->val)
12482 {
12483 struct bp_location *loc;
12484
12485 for (loc = bp->loc; loc != NULL; loc = loc->next)
12486 if (loc->loc_type == bp_loc_hardware_watchpoint
12487 && loc->address + loc->length > addr
12488 && addr + len > loc->address)
12489 {
12490 value_free (wp->val);
12491 wp->val = NULL;
12492 wp->val_valid = 0;
12493 }
12494 }
1f3b5d1b
PP
12495 }
12496}
12497
1bfeeb0f
JL
12498/* Use the last displayed codepoint's values, or nothing
12499 if they aren't valid. */
c906108c
SS
12500
12501struct symtabs_and_lines
f8eba3c6 12502decode_line_spec_1 (char *string, int flags)
c906108c
SS
12503{
12504 struct symtabs_and_lines sals;
cc59ec59 12505
c906108c 12506 if (string == 0)
8a3fe4f8 12507 error (_("Empty line specification."));
1bfeeb0f 12508 if (last_displayed_sal_is_valid ())
f8eba3c6 12509 sals = decode_line_1 (&string, flags,
1bfeeb0f 12510 get_last_displayed_symtab (),
f8eba3c6 12511 get_last_displayed_line ());
c906108c 12512 else
f8eba3c6 12513 sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
c906108c 12514 if (*string)
8a3fe4f8 12515 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
12516 return sals;
12517}
8181d85f
DJ
12518
12519/* Create and insert a raw software breakpoint at PC. Return an
12520 identifier, which should be used to remove the breakpoint later.
12521 In general, places which call this should be using something on the
12522 breakpoint chain instead; this function should be eliminated
12523 someday. */
12524
12525void *
6c95b8df
PA
12526deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12527 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
12528{
12529 struct bp_target_info *bp_tgt;
12530
6c95b8df 12531 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 12532
6c95b8df 12533 bp_tgt->placed_address_space = aspace;
8181d85f 12534 bp_tgt->placed_address = pc;
6c95b8df 12535
a6d9a66e 12536 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
12537 {
12538 /* Could not insert the breakpoint. */
12539 xfree (bp_tgt);
12540 return NULL;
12541 }
12542
12543 return bp_tgt;
12544}
12545
4a64f543
MS
12546/* Remove a breakpoint BP inserted by
12547 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
12548
12549int
a6d9a66e 12550deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
12551{
12552 struct bp_target_info *bp_tgt = bp;
12553 int ret;
12554
a6d9a66e 12555 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
12556 xfree (bp_tgt);
12557
12558 return ret;
12559}
12560
4a64f543
MS
12561/* One (or perhaps two) breakpoints used for software single
12562 stepping. */
8181d85f
DJ
12563
12564static void *single_step_breakpoints[2];
a6d9a66e 12565static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
12566
12567/* Create and insert a breakpoint for software single step. */
12568
12569void
6c95b8df 12570insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
12571 struct address_space *aspace,
12572 CORE_ADDR next_pc)
8181d85f
DJ
12573{
12574 void **bpt_p;
12575
12576 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
12577 {
12578 bpt_p = &single_step_breakpoints[0];
12579 single_step_gdbarch[0] = gdbarch;
12580 }
8181d85f
DJ
12581 else
12582 {
12583 gdb_assert (single_step_breakpoints[1] == NULL);
12584 bpt_p = &single_step_breakpoints[1];
a6d9a66e 12585 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
12586 }
12587
4a64f543
MS
12588 /* NOTE drow/2006-04-11: A future improvement to this function would
12589 be to only create the breakpoints once, and actually put them on
12590 the breakpoint chain. That would let us use set_raw_breakpoint.
12591 We could adjust the addresses each time they were needed. Doing
12592 this requires corresponding changes elsewhere where single step
12593 breakpoints are handled, however. So, for now, we use this. */
8181d85f 12594
6c95b8df 12595 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 12596 if (*bpt_p == NULL)
5af949e3
UW
12597 error (_("Could not insert single-step breakpoint at %s"),
12598 paddress (gdbarch, next_pc));
8181d85f
DJ
12599}
12600
f02253f1
HZ
12601/* Check if the breakpoints used for software single stepping
12602 were inserted or not. */
12603
12604int
12605single_step_breakpoints_inserted (void)
12606{
12607 return (single_step_breakpoints[0] != NULL
12608 || single_step_breakpoints[1] != NULL);
12609}
12610
8181d85f
DJ
12611/* Remove and delete any breakpoints used for software single step. */
12612
12613void
12614remove_single_step_breakpoints (void)
12615{
12616 gdb_assert (single_step_breakpoints[0] != NULL);
12617
12618 /* See insert_single_step_breakpoint for more about this deprecated
12619 call. */
a6d9a66e
UW
12620 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12621 single_step_breakpoints[0]);
12622 single_step_gdbarch[0] = NULL;
8181d85f
DJ
12623 single_step_breakpoints[0] = NULL;
12624
12625 if (single_step_breakpoints[1] != NULL)
12626 {
a6d9a66e
UW
12627 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12628 single_step_breakpoints[1]);
12629 single_step_gdbarch[1] = NULL;
8181d85f
DJ
12630 single_step_breakpoints[1] = NULL;
12631 }
12632}
12633
d03285ec
UW
12634/* Delete software single step breakpoints without removing them from
12635 the inferior. This is intended to be used if the inferior's address
12636 space where they were inserted is already gone, e.g. after exit or
12637 exec. */
12638
12639void
12640cancel_single_step_breakpoints (void)
12641{
12642 int i;
12643
12644 for (i = 0; i < 2; i++)
12645 if (single_step_breakpoints[i])
12646 {
12647 xfree (single_step_breakpoints[i]);
12648 single_step_breakpoints[i] = NULL;
12649 single_step_gdbarch[i] = NULL;
12650 }
12651}
12652
12653/* Detach software single-step breakpoints from INFERIOR_PTID without
12654 removing them. */
12655
12656static void
12657detach_single_step_breakpoints (void)
12658{
12659 int i;
12660
12661 for (i = 0; i < 2; i++)
12662 if (single_step_breakpoints[i])
12663 target_remove_breakpoint (single_step_gdbarch[i],
12664 single_step_breakpoints[i]);
12665}
12666
4a64f543
MS
12667/* Check whether a software single-step breakpoint is inserted at
12668 PC. */
1aafd4da
UW
12669
12670static int
cc59ec59
MS
12671single_step_breakpoint_inserted_here_p (struct address_space *aspace,
12672 CORE_ADDR pc)
1aafd4da
UW
12673{
12674 int i;
12675
12676 for (i = 0; i < 2; i++)
12677 {
12678 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
12679 if (bp_tgt
12680 && breakpoint_address_match (bp_tgt->placed_address_space,
12681 bp_tgt->placed_address,
12682 aspace, pc))
1aafd4da
UW
12683 return 1;
12684 }
12685
12686 return 0;
12687}
12688
a96d9b2e
SDJ
12689/* Returns 0 if 'bp' is NOT a syscall catchpoint,
12690 non-zero otherwise. */
12691static int
12692is_syscall_catchpoint_enabled (struct breakpoint *bp)
12693{
12694 if (syscall_catchpoint_p (bp)
12695 && bp->enable_state != bp_disabled
12696 && bp->enable_state != bp_call_disabled)
12697 return 1;
12698 else
12699 return 0;
12700}
12701
12702int
12703catch_syscall_enabled (void)
12704{
12705 struct inferior *inf = current_inferior ();
12706
12707 return inf->total_syscalls_count != 0;
12708}
12709
12710int
12711catching_syscall_number (int syscall_number)
12712{
12713 struct breakpoint *bp;
12714
12715 ALL_BREAKPOINTS (bp)
12716 if (is_syscall_catchpoint_enabled (bp))
12717 {
be5c67c1
PA
12718 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12719
12720 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
12721 {
12722 int i, iter;
12723 for (i = 0;
be5c67c1 12724 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
12725 i++)
12726 if (syscall_number == iter)
12727 return 1;
12728 }
12729 else
12730 return 1;
12731 }
12732
12733 return 0;
12734}
12735
12736/* Complete syscall names. Used by "catch syscall". */
12737static char **
12738catch_syscall_completer (struct cmd_list_element *cmd,
12739 char *text, char *word)
12740{
12741 const char **list = get_syscall_names ();
c38eea1a
MS
12742 char **retlist
12743 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 12744
c38eea1a
MS
12745 xfree (list);
12746 return retlist;
a96d9b2e
SDJ
12747}
12748
1042e4c0
SS
12749/* Tracepoint-specific operations. */
12750
12751/* Set tracepoint count to NUM. */
12752static void
12753set_tracepoint_count (int num)
12754{
12755 tracepoint_count = num;
4fa62494 12756 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
12757}
12758
12759void
12760trace_command (char *arg, int from_tty)
12761{
8cdf0e15
VP
12762 if (create_breakpoint (get_current_arch (),
12763 arg,
12764 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12765 0 /* tempflag */,
12766 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
12767 0 /* Ignore count */,
12768 pending_break_support,
348d480f 12769 &tracepoint_breakpoint_ops,
8cdf0e15 12770 from_tty,
84f4c1fe
PM
12771 1 /* enabled */,
12772 0 /* internal */))
fd9b8c24 12773 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
12774}
12775
7a697b8d
SS
12776void
12777ftrace_command (char *arg, int from_tty)
12778{
8cdf0e15
VP
12779 if (create_breakpoint (get_current_arch (),
12780 arg,
12781 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12782 0 /* tempflag */,
12783 bp_fast_tracepoint /* type_wanted */,
12784 0 /* Ignore count */,
12785 pending_break_support,
348d480f 12786 &tracepoint_breakpoint_ops,
0fb4aa4b 12787 from_tty,
84f4c1fe
PM
12788 1 /* enabled */,
12789 0 /* internal */))
0fb4aa4b
PA
12790 set_tracepoint_count (breakpoint_count);
12791}
12792
12793/* strace command implementation. Creates a static tracepoint. */
12794
12795void
12796strace_command (char *arg, int from_tty)
12797{
12798 if (create_breakpoint (get_current_arch (),
12799 arg,
12800 NULL, 0, 1 /* parse arg */,
12801 0 /* tempflag */,
12802 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
12803 0 /* Ignore count */,
12804 pending_break_support,
348d480f 12805 &tracepoint_breakpoint_ops,
8cdf0e15 12806 from_tty,
84f4c1fe
PM
12807 1 /* enabled */,
12808 0 /* internal */))
fd9b8c24 12809 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
12810}
12811
409873ef
SS
12812/* Set up a fake reader function that gets command lines from a linked
12813 list that was acquired during tracepoint uploading. */
12814
12815static struct uploaded_tp *this_utp;
3149d8c1 12816static int next_cmd;
409873ef
SS
12817
12818static char *
12819read_uploaded_action (void)
12820{
12821 char *rslt;
12822
3149d8c1 12823 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 12824
3149d8c1 12825 next_cmd++;
409873ef
SS
12826
12827 return rslt;
12828}
12829
00bf0b85
SS
12830/* Given information about a tracepoint as recorded on a target (which
12831 can be either a live system or a trace file), attempt to create an
12832 equivalent GDB tracepoint. This is not a reliable process, since
12833 the target does not necessarily have all the information used when
12834 the tracepoint was originally defined. */
12835
d9b3f62e 12836struct tracepoint *
00bf0b85 12837create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 12838{
409873ef 12839 char *addr_str, small_buf[100];
d9b3f62e 12840 struct tracepoint *tp;
fd9b8c24 12841
409873ef
SS
12842 if (utp->at_string)
12843 addr_str = utp->at_string;
12844 else
12845 {
12846 /* In the absence of a source location, fall back to raw
12847 address. Since there is no way to confirm that the address
12848 means the same thing as when the trace was started, warn the
12849 user. */
3e43a32a
MS
12850 warning (_("Uploaded tracepoint %d has no "
12851 "source location, using raw address"),
409873ef
SS
12852 utp->number);
12853 sprintf (small_buf, "*%s", hex_string (utp->addr));
12854 addr_str = small_buf;
12855 }
12856
12857 /* There's not much we can do with a sequence of bytecodes. */
12858 if (utp->cond && !utp->cond_string)
3e43a32a
MS
12859 warning (_("Uploaded tracepoint %d condition "
12860 "has no source form, ignoring it"),
409873ef 12861 utp->number);
d5551862 12862
8cdf0e15 12863 if (!create_breakpoint (get_current_arch (),
409873ef
SS
12864 addr_str,
12865 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 12866 0 /* tempflag */,
0fb4aa4b 12867 utp->type /* type_wanted */,
8cdf0e15
VP
12868 0 /* Ignore count */,
12869 pending_break_support,
348d480f 12870 &tracepoint_breakpoint_ops,
8cdf0e15 12871 0 /* from_tty */,
84f4c1fe
PM
12872 utp->enabled /* enabled */,
12873 0 /* internal */))
fd9b8c24
PA
12874 return NULL;
12875
00bf0b85
SS
12876 set_tracepoint_count (breakpoint_count);
12877
409873ef 12878 /* Get the tracepoint we just created. */
fd9b8c24
PA
12879 tp = get_tracepoint (tracepoint_count);
12880 gdb_assert (tp != NULL);
d5551862 12881
00bf0b85
SS
12882 if (utp->pass > 0)
12883 {
d9b3f62e 12884 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 12885
409873ef 12886 trace_pass_command (small_buf, 0);
00bf0b85
SS
12887 }
12888
409873ef
SS
12889 /* If we have uploaded versions of the original commands, set up a
12890 special-purpose "reader" function and call the usual command line
12891 reader, then pass the result to the breakpoint command-setting
12892 function. */
3149d8c1 12893 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 12894 {
409873ef 12895 struct command_line *cmd_list;
00bf0b85 12896
409873ef 12897 this_utp = utp;
3149d8c1 12898 next_cmd = 0;
d5551862 12899
409873ef
SS
12900 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12901
d9b3f62e 12902 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 12903 }
3149d8c1
SS
12904 else if (!VEC_empty (char_ptr, utp->actions)
12905 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12906 warning (_("Uploaded tracepoint %d actions "
12907 "have no source form, ignoring them"),
409873ef 12908 utp->number);
00bf0b85 12909
f196051f
SS
12910 /* Copy any status information that might be available. */
12911 tp->base.hit_count = utp->hit_count;
12912 tp->traceframe_usage = utp->traceframe_usage;
12913
00bf0b85 12914 return tp;
d9b3f62e 12915}
00bf0b85 12916
1042e4c0
SS
12917/* Print information on tracepoint number TPNUM_EXP, or all if
12918 omitted. */
12919
12920static void
e5a67952 12921tracepoints_info (char *args, int from_tty)
1042e4c0 12922{
79a45e25 12923 struct ui_out *uiout = current_uiout;
e5a67952 12924 int num_printed;
1042e4c0 12925
e5a67952 12926 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
12927
12928 if (num_printed == 0)
1042e4c0 12929 {
e5a67952 12930 if (args == NULL || *args == '\0')
d77f58be
SS
12931 ui_out_message (uiout, 0, "No tracepoints.\n");
12932 else
e5a67952 12933 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 12934 }
ad443146
SS
12935
12936 default_collect_info ();
1042e4c0
SS
12937}
12938
4a64f543 12939/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
12940 Not supported by all targets. */
12941static void
12942enable_trace_command (char *args, int from_tty)
12943{
12944 enable_command (args, from_tty);
12945}
12946
4a64f543 12947/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
12948 Not supported by all targets. */
12949static void
12950disable_trace_command (char *args, int from_tty)
12951{
12952 disable_command (args, from_tty);
12953}
12954
4a64f543 12955/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
12956static void
12957delete_trace_command (char *arg, int from_tty)
12958{
35df4500 12959 struct breakpoint *b, *b_tmp;
1042e4c0
SS
12960
12961 dont_repeat ();
12962
12963 if (arg == 0)
12964 {
12965 int breaks_to_delete = 0;
12966
12967 /* Delete all breakpoints if no argument.
12968 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
12969 have to be deleted with an explicit breakpoint number
12970 argument. */
1042e4c0 12971 ALL_TRACEPOINTS (b)
46c6471b 12972 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
12973 {
12974 breaks_to_delete = 1;
12975 break;
12976 }
1042e4c0
SS
12977
12978 /* Ask user only if there are some breakpoints to delete. */
12979 if (!from_tty
12980 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12981 {
35df4500 12982 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 12983 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 12984 delete_breakpoint (b);
1042e4c0
SS
12985 }
12986 }
12987 else
51be5b68 12988 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
12989}
12990
197f0a60
TT
12991/* Helper function for trace_pass_command. */
12992
12993static void
d9b3f62e 12994trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 12995{
d9b3f62e
PA
12996 tp->pass_count = count;
12997 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
12998 if (from_tty)
12999 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 13000 tp->base.number, count);
197f0a60
TT
13001}
13002
1042e4c0
SS
13003/* Set passcount for tracepoint.
13004
13005 First command argument is passcount, second is tracepoint number.
13006 If tracepoint number omitted, apply to most recently defined.
13007 Also accepts special argument "all". */
13008
13009static void
13010trace_pass_command (char *args, int from_tty)
13011{
d9b3f62e 13012 struct tracepoint *t1;
1042e4c0 13013 unsigned int count;
1042e4c0
SS
13014
13015 if (args == 0 || *args == 0)
3e43a32a
MS
13016 error (_("passcount command requires an "
13017 "argument (count + optional TP num)"));
1042e4c0 13018
4a64f543 13019 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
13020
13021 while (*args && isspace ((int) *args))
13022 args++;
13023
13024 if (*args && strncasecmp (args, "all", 3) == 0)
13025 {
d9b3f62e
PA
13026 struct breakpoint *b;
13027
1042e4c0 13028 args += 3; /* Skip special argument "all". */
1042e4c0
SS
13029 if (*args)
13030 error (_("Junk at end of arguments."));
1042e4c0 13031
d9b3f62e 13032 ALL_TRACEPOINTS (b)
197f0a60 13033 {
d9b3f62e 13034 t1 = (struct tracepoint *) b;
197f0a60
TT
13035 trace_pass_set_count (t1, count, from_tty);
13036 }
13037 }
13038 else if (*args == '\0')
1042e4c0 13039 {
197f0a60 13040 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 13041 if (t1)
197f0a60
TT
13042 trace_pass_set_count (t1, count, from_tty);
13043 }
13044 else
13045 {
13046 struct get_number_or_range_state state;
13047
13048 init_number_or_range (&state, args);
13049 while (!state.finished)
1042e4c0 13050 {
197f0a60
TT
13051 t1 = get_tracepoint_by_number (&args, &state, 1);
13052 if (t1)
13053 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
13054 }
13055 }
1042e4c0
SS
13056}
13057
d9b3f62e 13058struct tracepoint *
1042e4c0
SS
13059get_tracepoint (int num)
13060{
13061 struct breakpoint *t;
13062
13063 ALL_TRACEPOINTS (t)
13064 if (t->number == num)
d9b3f62e 13065 return (struct tracepoint *) t;
1042e4c0
SS
13066
13067 return NULL;
13068}
13069
d5551862
SS
13070/* Find the tracepoint with the given target-side number (which may be
13071 different from the tracepoint number after disconnecting and
13072 reconnecting). */
13073
d9b3f62e 13074struct tracepoint *
d5551862
SS
13075get_tracepoint_by_number_on_target (int num)
13076{
d9b3f62e 13077 struct breakpoint *b;
d5551862 13078
d9b3f62e
PA
13079 ALL_TRACEPOINTS (b)
13080 {
13081 struct tracepoint *t = (struct tracepoint *) b;
13082
13083 if (t->number_on_target == num)
13084 return t;
13085 }
d5551862
SS
13086
13087 return NULL;
13088}
13089
1042e4c0 13090/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
13091 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13092 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 13093 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 13094struct tracepoint *
197f0a60
TT
13095get_tracepoint_by_number (char **arg,
13096 struct get_number_or_range_state *state,
13097 int optional_p)
1042e4c0
SS
13098{
13099 extern int tracepoint_count;
13100 struct breakpoint *t;
13101 int tpnum;
13102 char *instring = arg == NULL ? NULL : *arg;
13103
197f0a60
TT
13104 if (state)
13105 {
13106 gdb_assert (!state->finished);
13107 tpnum = get_number_or_range (state);
13108 }
13109 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
13110 {
13111 if (optional_p)
13112 tpnum = tracepoint_count;
13113 else
13114 error_no_arg (_("tracepoint number"));
13115 }
13116 else
197f0a60 13117 tpnum = get_number (arg);
1042e4c0
SS
13118
13119 if (tpnum <= 0)
13120 {
13121 if (instring && *instring)
13122 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
13123 instring);
13124 else
3e43a32a
MS
13125 printf_filtered (_("Tracepoint argument missing "
13126 "and no previous tracepoint\n"));
1042e4c0
SS
13127 return NULL;
13128 }
13129
13130 ALL_TRACEPOINTS (t)
13131 if (t->number == tpnum)
13132 {
d9b3f62e 13133 return (struct tracepoint *) t;
1042e4c0
SS
13134 }
13135
1042e4c0
SS
13136 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
13137 return NULL;
13138}
13139
d9b3f62e
PA
13140void
13141print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
13142{
13143 if (b->thread != -1)
13144 fprintf_unfiltered (fp, " thread %d", b->thread);
13145
13146 if (b->task != 0)
13147 fprintf_unfiltered (fp, " task %d", b->task);
13148
13149 fprintf_unfiltered (fp, "\n");
13150}
13151
6149aea9
PA
13152/* Save information on user settable breakpoints (watchpoints, etc) to
13153 a new script file named FILENAME. If FILTER is non-NULL, call it
13154 on each breakpoint and only include the ones for which it returns
13155 non-zero. */
13156
1042e4c0 13157static void
6149aea9
PA
13158save_breakpoints (char *filename, int from_tty,
13159 int (*filter) (const struct breakpoint *))
1042e4c0
SS
13160{
13161 struct breakpoint *tp;
6149aea9 13162 int any = 0;
a7bdde9e 13163 char *pathname;
1042e4c0 13164 struct cleanup *cleanup;
a7bdde9e 13165 struct ui_file *fp;
6149aea9 13166 int extra_trace_bits = 0;
1042e4c0 13167
6149aea9
PA
13168 if (filename == 0 || *filename == 0)
13169 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
13170
13171 /* See if we have anything to save. */
6149aea9 13172 ALL_BREAKPOINTS (tp)
1042e4c0 13173 {
6149aea9 13174 /* Skip internal and momentary breakpoints. */
09d682a4 13175 if (!user_breakpoint_p (tp))
6149aea9
PA
13176 continue;
13177
13178 /* If we have a filter, only save the breakpoints it accepts. */
13179 if (filter && !filter (tp))
13180 continue;
13181
13182 any = 1;
13183
13184 if (is_tracepoint (tp))
13185 {
13186 extra_trace_bits = 1;
13187
13188 /* We can stop searching. */
13189 break;
13190 }
1042e4c0 13191 }
6149aea9
PA
13192
13193 if (!any)
1042e4c0 13194 {
6149aea9 13195 warning (_("Nothing to save."));
1042e4c0
SS
13196 return;
13197 }
13198
6149aea9 13199 pathname = tilde_expand (filename);
1042e4c0 13200 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 13201 fp = gdb_fopen (pathname, "w");
059fb39f 13202 if (!fp)
6149aea9
PA
13203 error (_("Unable to open file '%s' for saving (%s)"),
13204 filename, safe_strerror (errno));
a7bdde9e 13205 make_cleanup_ui_file_delete (fp);
8bf6485c 13206
6149aea9
PA
13207 if (extra_trace_bits)
13208 save_trace_state_variables (fp);
8bf6485c 13209
6149aea9 13210 ALL_BREAKPOINTS (tp)
1042e4c0 13211 {
6149aea9 13212 /* Skip internal and momentary breakpoints. */
09d682a4 13213 if (!user_breakpoint_p (tp))
6149aea9 13214 continue;
8bf6485c 13215
6149aea9
PA
13216 /* If we have a filter, only save the breakpoints it accepts. */
13217 if (filter && !filter (tp))
13218 continue;
13219
348d480f 13220 tp->ops->print_recreate (tp, fp);
1042e4c0 13221
6149aea9
PA
13222 /* Note, we can't rely on tp->number for anything, as we can't
13223 assume the recreated breakpoint numbers will match. Use $bpnum
13224 instead. */
13225
13226 if (tp->cond_string)
13227 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
13228
13229 if (tp->ignore_count)
13230 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
13231
a7bdde9e 13232 if (tp->commands)
1042e4c0 13233 {
a7bdde9e
VP
13234 volatile struct gdb_exception ex;
13235
6149aea9 13236 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 13237
79a45e25 13238 ui_out_redirect (current_uiout, fp);
14dba4b4 13239 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 13240 {
79a45e25 13241 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 13242 }
79a45e25 13243 ui_out_redirect (current_uiout, NULL);
1042e4c0 13244
a7bdde9e
VP
13245 if (ex.reason < 0)
13246 throw_exception (ex);
1042e4c0 13247
a7bdde9e 13248 fprintf_unfiltered (fp, " end\n");
1042e4c0 13249 }
6149aea9
PA
13250
13251 if (tp->enable_state == bp_disabled)
13252 fprintf_unfiltered (fp, "disable\n");
13253
13254 /* If this is a multi-location breakpoint, check if the locations
13255 should be individually disabled. Watchpoint locations are
13256 special, and not user visible. */
13257 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13258 {
13259 struct bp_location *loc;
13260 int n = 1;
13261
13262 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
13263 if (!loc->enabled)
13264 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
13265 }
1042e4c0 13266 }
8bf6485c 13267
6149aea9 13268 if (extra_trace_bits && *default_collect)
8bf6485c
SS
13269 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
13270
1042e4c0
SS
13271 do_cleanups (cleanup);
13272 if (from_tty)
6149aea9
PA
13273 printf_filtered (_("Saved to file '%s'.\n"), filename);
13274}
13275
13276/* The `save breakpoints' command. */
13277
13278static void
13279save_breakpoints_command (char *args, int from_tty)
13280{
13281 save_breakpoints (args, from_tty, NULL);
13282}
13283
13284/* The `save tracepoints' command. */
13285
13286static void
13287save_tracepoints_command (char *args, int from_tty)
13288{
13289 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
13290}
13291
13292/* Create a vector of all tracepoints. */
13293
13294VEC(breakpoint_p) *
eeae04df 13295all_tracepoints (void)
1042e4c0
SS
13296{
13297 VEC(breakpoint_p) *tp_vec = 0;
13298 struct breakpoint *tp;
13299
13300 ALL_TRACEPOINTS (tp)
13301 {
13302 VEC_safe_push (breakpoint_p, tp_vec, tp);
13303 }
13304
13305 return tp_vec;
13306}
13307
c906108c 13308\f
4a64f543
MS
13309/* This help string is used for the break, hbreak, tbreak and thbreak
13310 commands. It is defined as a macro to prevent duplication.
13311 COMMAND should be a string constant containing the name of the
13312 command. */
31e2b00f
AS
13313#define BREAK_ARGS_HELP(command) \
13314command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
13315LOCATION may be a line number, function name, or \"*\" and an address.\n\
13316If a line number is specified, break at start of code for that line.\n\
13317If a function is specified, break at start of code for that function.\n\
13318If an address is specified, break at that exact address.\n\
dc10affe
PA
13319With no LOCATION, uses current execution address of the selected\n\
13320stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
13321\n\
13322THREADNUM is the number from \"info threads\".\n\
13323CONDITION is a boolean expression.\n\
13324\n\
d41c0fc8
PA
13325Multiple breakpoints at one place are permitted, and useful if their\n\
13326conditions are different.\n\
31e2b00f
AS
13327\n\
13328Do \"help breakpoints\" for info on other commands dealing with breakpoints."
13329
44feb3ce
TT
13330/* List of subcommands for "catch". */
13331static struct cmd_list_element *catch_cmdlist;
13332
13333/* List of subcommands for "tcatch". */
13334static struct cmd_list_element *tcatch_cmdlist;
13335
9ac4176b 13336void
44feb3ce
TT
13337add_catch_command (char *name, char *docstring,
13338 void (*sfunc) (char *args, int from_tty,
13339 struct cmd_list_element *command),
a96d9b2e
SDJ
13340 char **(*completer) (struct cmd_list_element *cmd,
13341 char *text, char *word),
44feb3ce
TT
13342 void *user_data_catch,
13343 void *user_data_tcatch)
13344{
13345 struct cmd_list_element *command;
13346
13347 command = add_cmd (name, class_breakpoint, NULL, docstring,
13348 &catch_cmdlist);
13349 set_cmd_sfunc (command, sfunc);
13350 set_cmd_context (command, user_data_catch);
a96d9b2e 13351 set_cmd_completer (command, completer);
44feb3ce
TT
13352
13353 command = add_cmd (name, class_breakpoint, NULL, docstring,
13354 &tcatch_cmdlist);
13355 set_cmd_sfunc (command, sfunc);
13356 set_cmd_context (command, user_data_tcatch);
a96d9b2e 13357 set_cmd_completer (command, completer);
44feb3ce
TT
13358}
13359
6c95b8df 13360static void
a79b8f6e 13361clear_syscall_counts (struct inferior *inf)
6c95b8df 13362{
6c95b8df
PA
13363 inf->total_syscalls_count = 0;
13364 inf->any_syscall_count = 0;
13365 VEC_free (int, inf->syscalls_counts);
13366}
13367
6149aea9
PA
13368static void
13369save_command (char *arg, int from_tty)
13370{
3e43a32a
MS
13371 printf_unfiltered (_("\"save\" must be followed by "
13372 "the name of a save subcommand.\n"));
6149aea9
PA
13373 help_list (save_cmdlist, "save ", -1, gdb_stdout);
13374}
13375
84f4c1fe
PM
13376struct breakpoint *
13377iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
13378 void *data)
13379{
35df4500 13380 struct breakpoint *b, *b_tmp;
84f4c1fe 13381
35df4500 13382 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
13383 {
13384 if ((*callback) (b, data))
13385 return b;
13386 }
13387
13388 return NULL;
13389}
13390
0574c78f
GB
13391/* Zero if any of the breakpoint's locations could be a location where
13392 functions have been inlined, nonzero otherwise. */
13393
13394static int
13395is_non_inline_function (struct breakpoint *b)
13396{
13397 /* The shared library event breakpoint is set on the address of a
13398 non-inline function. */
13399 if (b->type == bp_shlib_event)
13400 return 1;
13401
13402 return 0;
13403}
13404
13405/* Nonzero if the specified PC cannot be a location where functions
13406 have been inlined. */
13407
13408int
13409pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc)
13410{
13411 struct breakpoint *b;
13412 struct bp_location *bl;
13413
13414 ALL_BREAKPOINTS (b)
13415 {
13416 if (!is_non_inline_function (b))
13417 continue;
13418
13419 for (bl = b->loc; bl != NULL; bl = bl->next)
13420 {
13421 if (!bl->shlib_disabled
13422 && bpstat_check_location (bl, aspace, pc))
13423 return 1;
13424 }
13425 }
13426
13427 return 0;
13428}
13429
2060206e
PA
13430void
13431initialize_breakpoint_ops (void)
13432{
13433 static int initialized = 0;
13434
13435 struct breakpoint_ops *ops;
13436
13437 if (initialized)
13438 return;
13439 initialized = 1;
13440
13441 /* The breakpoint_ops structure to be inherit by all kinds of
13442 breakpoints (real breakpoints, i.e., user "break" breakpoints,
13443 internal and momentary breakpoints, etc.). */
13444 ops = &bkpt_base_breakpoint_ops;
13445 *ops = base_breakpoint_ops;
13446 ops->re_set = bkpt_re_set;
13447 ops->insert_location = bkpt_insert_location;
13448 ops->remove_location = bkpt_remove_location;
13449 ops->breakpoint_hit = bkpt_breakpoint_hit;
13450
13451 /* The breakpoint_ops structure to be used in regular breakpoints. */
13452 ops = &bkpt_breakpoint_ops;
13453 *ops = bkpt_base_breakpoint_ops;
13454 ops->re_set = bkpt_re_set;
13455 ops->resources_needed = bkpt_resources_needed;
13456 ops->print_it = bkpt_print_it;
13457 ops->print_mention = bkpt_print_mention;
13458 ops->print_recreate = bkpt_print_recreate;
13459
13460 /* Ranged breakpoints. */
13461 ops = &ranged_breakpoint_ops;
13462 *ops = bkpt_breakpoint_ops;
13463 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
13464 ops->resources_needed = resources_needed_ranged_breakpoint;
13465 ops->print_it = print_it_ranged_breakpoint;
13466 ops->print_one = print_one_ranged_breakpoint;
13467 ops->print_one_detail = print_one_detail_ranged_breakpoint;
13468 ops->print_mention = print_mention_ranged_breakpoint;
13469 ops->print_recreate = print_recreate_ranged_breakpoint;
13470
13471 /* Internal breakpoints. */
13472 ops = &internal_breakpoint_ops;
13473 *ops = bkpt_base_breakpoint_ops;
13474 ops->re_set = internal_bkpt_re_set;
13475 ops->check_status = internal_bkpt_check_status;
13476 ops->print_it = internal_bkpt_print_it;
13477 ops->print_mention = internal_bkpt_print_mention;
13478
13479 /* Momentary breakpoints. */
13480 ops = &momentary_breakpoint_ops;
13481 *ops = bkpt_base_breakpoint_ops;
13482 ops->re_set = momentary_bkpt_re_set;
13483 ops->check_status = momentary_bkpt_check_status;
13484 ops->print_it = momentary_bkpt_print_it;
13485 ops->print_mention = momentary_bkpt_print_mention;
13486
13487 /* GNU v3 exception catchpoints. */
13488 ops = &gnu_v3_exception_catchpoint_ops;
13489 *ops = bkpt_breakpoint_ops;
13490 ops->print_it = print_it_exception_catchpoint;
13491 ops->print_one = print_one_exception_catchpoint;
13492 ops->print_mention = print_mention_exception_catchpoint;
13493 ops->print_recreate = print_recreate_exception_catchpoint;
13494
13495 /* Watchpoints. */
13496 ops = &watchpoint_breakpoint_ops;
13497 *ops = base_breakpoint_ops;
3a5c3e22 13498 ops->dtor = dtor_watchpoint;
2060206e
PA
13499 ops->re_set = re_set_watchpoint;
13500 ops->insert_location = insert_watchpoint;
13501 ops->remove_location = remove_watchpoint;
13502 ops->breakpoint_hit = breakpoint_hit_watchpoint;
13503 ops->check_status = check_status_watchpoint;
13504 ops->resources_needed = resources_needed_watchpoint;
13505 ops->works_in_software_mode = works_in_software_mode_watchpoint;
13506 ops->print_it = print_it_watchpoint;
13507 ops->print_mention = print_mention_watchpoint;
13508 ops->print_recreate = print_recreate_watchpoint;
13509
13510 /* Masked watchpoints. */
13511 ops = &masked_watchpoint_breakpoint_ops;
13512 *ops = watchpoint_breakpoint_ops;
13513 ops->insert_location = insert_masked_watchpoint;
13514 ops->remove_location = remove_masked_watchpoint;
13515 ops->resources_needed = resources_needed_masked_watchpoint;
13516 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
13517 ops->print_it = print_it_masked_watchpoint;
13518 ops->print_one_detail = print_one_detail_masked_watchpoint;
13519 ops->print_mention = print_mention_masked_watchpoint;
13520 ops->print_recreate = print_recreate_masked_watchpoint;
13521
13522 /* Tracepoints. */
13523 ops = &tracepoint_breakpoint_ops;
13524 *ops = base_breakpoint_ops;
13525 ops->re_set = tracepoint_re_set;
13526 ops->breakpoint_hit = tracepoint_breakpoint_hit;
13527 ops->print_one_detail = tracepoint_print_one_detail;
13528 ops->print_mention = tracepoint_print_mention;
13529 ops->print_recreate = tracepoint_print_recreate;
13530
13531 /* Fork catchpoints. */
13532 ops = &catch_fork_breakpoint_ops;
13533 *ops = base_breakpoint_ops;
13534 ops->insert_location = insert_catch_fork;
13535 ops->remove_location = remove_catch_fork;
13536 ops->breakpoint_hit = breakpoint_hit_catch_fork;
13537 ops->print_it = print_it_catch_fork;
13538 ops->print_one = print_one_catch_fork;
13539 ops->print_mention = print_mention_catch_fork;
13540 ops->print_recreate = print_recreate_catch_fork;
13541
13542 /* Vfork catchpoints. */
13543 ops = &catch_vfork_breakpoint_ops;
13544 *ops = base_breakpoint_ops;
13545 ops->insert_location = insert_catch_vfork;
13546 ops->remove_location = remove_catch_vfork;
13547 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
13548 ops->print_it = print_it_catch_vfork;
13549 ops->print_one = print_one_catch_vfork;
13550 ops->print_mention = print_mention_catch_vfork;
13551 ops->print_recreate = print_recreate_catch_vfork;
13552
13553 /* Exec catchpoints. */
13554 ops = &catch_exec_breakpoint_ops;
13555 *ops = base_breakpoint_ops;
13556 ops->dtor = dtor_catch_exec;
13557 ops->insert_location = insert_catch_exec;
13558 ops->remove_location = remove_catch_exec;
13559 ops->breakpoint_hit = breakpoint_hit_catch_exec;
13560 ops->print_it = print_it_catch_exec;
13561 ops->print_one = print_one_catch_exec;
13562 ops->print_mention = print_mention_catch_exec;
13563 ops->print_recreate = print_recreate_catch_exec;
13564
13565 /* Syscall catchpoints. */
13566 ops = &catch_syscall_breakpoint_ops;
13567 *ops = base_breakpoint_ops;
13568 ops->dtor = dtor_catch_syscall;
13569 ops->insert_location = insert_catch_syscall;
13570 ops->remove_location = remove_catch_syscall;
13571 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
13572 ops->print_it = print_it_catch_syscall;
13573 ops->print_one = print_one_catch_syscall;
13574 ops->print_mention = print_mention_catch_syscall;
13575 ops->print_recreate = print_recreate_catch_syscall;
13576}
13577
c906108c 13578void
fba45db2 13579_initialize_breakpoint (void)
c906108c
SS
13580{
13581 struct cmd_list_element *c;
13582
2060206e
PA
13583 initialize_breakpoint_ops ();
13584
84acb35a 13585 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 13586 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 13587 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 13588
17450429
PP
13589 breakpoint_objfile_key = register_objfile_data ();
13590
c906108c
SS
13591 breakpoint_chain = 0;
13592 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
13593 before a breakpoint is set. */
13594 breakpoint_count = 0;
13595
1042e4c0
SS
13596 tracepoint_count = 0;
13597
1bedd215
AC
13598 add_com ("ignore", class_breakpoint, ignore_command, _("\
13599Set ignore-count of breakpoint number N to COUNT.\n\
13600Usage is `ignore N COUNT'."));
c906108c 13601 if (xdb_commands)
c5aa993b 13602 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 13603
1bedd215
AC
13604 add_com ("commands", class_breakpoint, commands_command, _("\
13605Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
13606Give breakpoint number as argument after \"commands\".\n\
13607With no argument, the targeted breakpoint is the last one set.\n\
13608The commands themselves follow starting on the next line.\n\
13609Type a line containing \"end\" to indicate the end of them.\n\
13610Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 13611then no output is printed when it is hit, except what the commands print."));
c906108c 13612
1bedd215
AC
13613 add_com ("condition", class_breakpoint, condition_command, _("\
13614Specify breakpoint number N to break only if COND is true.\n\
c906108c 13615Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 13616expression to be evaluated whenever breakpoint N is reached."));
c906108c 13617
1bedd215 13618 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 13619Set a temporary breakpoint.\n\
c906108c
SS
13620Like \"break\" except the breakpoint is only temporary,\n\
13621so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
13622by using \"enable delete\" on the breakpoint number.\n\
13623\n"
13624BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 13625 set_cmd_completer (c, location_completer);
c94fdfd0 13626
1bedd215 13627 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 13628Set a hardware assisted breakpoint.\n\
c906108c 13629Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
13630some target hardware may not have this support.\n\
13631\n"
13632BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 13633 set_cmd_completer (c, location_completer);
c906108c 13634
1bedd215 13635 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 13636Set a temporary hardware assisted breakpoint.\n\
c906108c 13637Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
13638so it will be deleted when hit.\n\
13639\n"
13640BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 13641 set_cmd_completer (c, location_completer);
c906108c 13642
1bedd215
AC
13643 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
13644Enable some breakpoints.\n\
c906108c
SS
13645Give breakpoint numbers (separated by spaces) as arguments.\n\
13646With no subcommand, breakpoints are enabled until you command otherwise.\n\
13647This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13648With a subcommand you can enable temporarily."),
c906108c
SS
13649 &enablelist, "enable ", 1, &cmdlist);
13650 if (xdb_commands)
1bedd215
AC
13651 add_com ("ab", class_breakpoint, enable_command, _("\
13652Enable some breakpoints.\n\
c906108c
SS
13653Give breakpoint numbers (separated by spaces) as arguments.\n\
13654With no subcommand, breakpoints are enabled until you command otherwise.\n\
13655This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13656With a subcommand you can enable temporarily."));
c906108c
SS
13657
13658 add_com_alias ("en", "enable", class_breakpoint, 1);
13659
84951ab5 13660 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 13661Enable some breakpoints.\n\
c906108c
SS
13662Give breakpoint numbers (separated by spaces) as arguments.\n\
13663This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13664May be abbreviated to simply \"enable\".\n"),
c5aa993b 13665 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 13666
1a966eab
AC
13667 add_cmd ("once", no_class, enable_once_command, _("\
13668Enable breakpoints for one hit. Give breakpoint numbers.\n\
13669If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13670 &enablebreaklist);
13671
1a966eab
AC
13672 add_cmd ("delete", no_class, enable_delete_command, _("\
13673Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13674If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13675 &enablebreaklist);
13676
1a966eab
AC
13677 add_cmd ("delete", no_class, enable_delete_command, _("\
13678Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13679If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13680 &enablelist);
13681
1a966eab
AC
13682 add_cmd ("once", no_class, enable_once_command, _("\
13683Enable breakpoints for one hit. Give breakpoint numbers.\n\
13684If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13685 &enablelist);
13686
1bedd215
AC
13687 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13688Disable some breakpoints.\n\
c906108c
SS
13689Arguments are breakpoint numbers with spaces in between.\n\
13690To disable all breakpoints, give no argument.\n\
64b9b334 13691A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
13692 &disablelist, "disable ", 1, &cmdlist);
13693 add_com_alias ("dis", "disable", class_breakpoint, 1);
13694 add_com_alias ("disa", "disable", class_breakpoint, 1);
13695 if (xdb_commands)
1bedd215
AC
13696 add_com ("sb", class_breakpoint, disable_command, _("\
13697Disable some breakpoints.\n\
c906108c
SS
13698Arguments are breakpoint numbers with spaces in between.\n\
13699To disable all breakpoints, give no argument.\n\
64b9b334 13700A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 13701
1a966eab
AC
13702 add_cmd ("breakpoints", class_alias, disable_command, _("\
13703Disable some breakpoints.\n\
c906108c
SS
13704Arguments are breakpoint numbers with spaces in between.\n\
13705To disable all breakpoints, give no argument.\n\
64b9b334 13706A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 13707This command may be abbreviated \"disable\"."),
c906108c
SS
13708 &disablelist);
13709
1bedd215
AC
13710 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13711Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13712Arguments are breakpoint numbers with spaces in between.\n\
13713To delete all breakpoints, give no argument.\n\
13714\n\
13715Also a prefix command for deletion of other GDB objects.\n\
1bedd215 13716The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
13717 &deletelist, "delete ", 1, &cmdlist);
13718 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 13719 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 13720 if (xdb_commands)
1bedd215
AC
13721 add_com ("db", class_breakpoint, delete_command, _("\
13722Delete some breakpoints.\n\
c906108c 13723Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 13724To delete all breakpoints, give no argument.\n"));
c906108c 13725
1a966eab
AC
13726 add_cmd ("breakpoints", class_alias, delete_command, _("\
13727Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13728Arguments are breakpoint numbers with spaces in between.\n\
13729To delete all breakpoints, give no argument.\n\
1a966eab 13730This command may be abbreviated \"delete\"."),
c906108c
SS
13731 &deletelist);
13732
1bedd215
AC
13733 add_com ("clear", class_breakpoint, clear_command, _("\
13734Clear breakpoint at specified line or function.\n\
c906108c
SS
13735Argument may be line number, function name, or \"*\" and an address.\n\
13736If line number is specified, all breakpoints in that line are cleared.\n\
13737If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
13738If an address is specified, breakpoints at that address are cleared.\n\
13739\n\
13740With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
13741is executing in.\n\
13742\n\
1bedd215 13743See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 13744 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 13745
1bedd215 13746 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
13747Set breakpoint at specified line or function.\n"
13748BREAK_ARGS_HELP ("break")));
5ba2abeb 13749 set_cmd_completer (c, location_completer);
c94fdfd0 13750
c906108c
SS
13751 add_com_alias ("b", "break", class_run, 1);
13752 add_com_alias ("br", "break", class_run, 1);
13753 add_com_alias ("bre", "break", class_run, 1);
13754 add_com_alias ("brea", "break", class_run, 1);
13755
7681d515
PM
13756 if (xdb_commands)
13757 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
13758
13759 if (dbx_commands)
13760 {
1bedd215
AC
13761 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13762Break in function/address or break at a line in the current file."),
c5aa993b
JM
13763 &stoplist, "stop ", 1, &cmdlist);
13764 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 13765 _("Break in function or address."), &stoplist);
c5aa993b 13766 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 13767 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
13768 add_com ("status", class_info, breakpoints_info, _("\
13769Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13770The \"Type\" column indicates one of:\n\
13771\tbreakpoint - normal breakpoint\n\
13772\twatchpoint - watchpoint\n\
13773The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13774the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13775breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13776address and file/line number respectively.\n\
13777\n\
13778Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13779are set to the address of the last breakpoint listed unless the command\n\
13780is prefixed with \"server \".\n\n\
c906108c 13781Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13782breakpoint set."));
c906108c
SS
13783 }
13784
1bedd215 13785 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 13786Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
13787The \"Type\" column indicates one of:\n\
13788\tbreakpoint - normal breakpoint\n\
13789\twatchpoint - watchpoint\n\
13790The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13791the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13792breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13793address and file/line number respectively.\n\
13794\n\
13795Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13796are set to the address of the last breakpoint listed unless the command\n\
13797is prefixed with \"server \".\n\n\
c906108c 13798Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13799breakpoint set."));
c906108c 13800
6b04bdb7
MS
13801 add_info_alias ("b", "breakpoints", 1);
13802
c906108c 13803 if (xdb_commands)
1bedd215
AC
13804 add_com ("lb", class_breakpoint, breakpoints_info, _("\
13805Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13806The \"Type\" column indicates one of:\n\
13807\tbreakpoint - normal breakpoint\n\
13808\twatchpoint - watchpoint\n\
13809The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13810the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13811breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13812address and file/line number respectively.\n\
13813\n\
13814Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13815are set to the address of the last breakpoint listed unless the command\n\
13816is prefixed with \"server \".\n\n\
c906108c 13817Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13818breakpoint set."));
c906108c 13819
1a966eab
AC
13820 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13821Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13822The \"Type\" column indicates one of:\n\
13823\tbreakpoint - normal breakpoint\n\
13824\twatchpoint - watchpoint\n\
13825\tlongjmp - internal breakpoint used to step through longjmp()\n\
13826\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13827\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
13828\tfinish - internal breakpoint used by the \"finish\" command\n\
13829The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
13830the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13831breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
13832address and file/line number respectively.\n\
13833\n\
13834Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13835are set to the address of the last breakpoint listed unless the command\n\
13836is prefixed with \"server \".\n\n\
c906108c 13837Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 13838breakpoint set."),
c906108c
SS
13839 &maintenanceinfolist);
13840
44feb3ce
TT
13841 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13842Set catchpoints to catch events."),
13843 &catch_cmdlist, "catch ",
13844 0/*allow-unknown*/, &cmdlist);
13845
13846 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13847Set temporary catchpoints to catch events."),
13848 &tcatch_cmdlist, "tcatch ",
13849 0/*allow-unknown*/, &cmdlist);
13850
13851 /* Add catch and tcatch sub-commands. */
13852 add_catch_command ("catch", _("\
13853Catch an exception, when caught.\n\
13854With an argument, catch only exceptions with the given name."),
13855 catch_catch_command,
a96d9b2e 13856 NULL,
44feb3ce
TT
13857 CATCH_PERMANENT,
13858 CATCH_TEMPORARY);
13859 add_catch_command ("throw", _("\
13860Catch an exception, when thrown.\n\
13861With an argument, catch only exceptions with the given name."),
13862 catch_throw_command,
a96d9b2e 13863 NULL,
44feb3ce
TT
13864 CATCH_PERMANENT,
13865 CATCH_TEMPORARY);
13866 add_catch_command ("fork", _("Catch calls to fork."),
13867 catch_fork_command_1,
a96d9b2e 13868 NULL,
44feb3ce
TT
13869 (void *) (uintptr_t) catch_fork_permanent,
13870 (void *) (uintptr_t) catch_fork_temporary);
13871 add_catch_command ("vfork", _("Catch calls to vfork."),
13872 catch_fork_command_1,
a96d9b2e 13873 NULL,
44feb3ce
TT
13874 (void *) (uintptr_t) catch_vfork_permanent,
13875 (void *) (uintptr_t) catch_vfork_temporary);
13876 add_catch_command ("exec", _("Catch calls to exec."),
13877 catch_exec_command_1,
a96d9b2e
SDJ
13878 NULL,
13879 CATCH_PERMANENT,
13880 CATCH_TEMPORARY);
13881 add_catch_command ("syscall", _("\
13882Catch system calls by their names and/or numbers.\n\
13883Arguments say which system calls to catch. If no arguments\n\
13884are given, every system call will be caught.\n\
13885Arguments, if given, should be one or more system call names\n\
13886(if your system supports that), or system call numbers."),
13887 catch_syscall_command_1,
13888 catch_syscall_completer,
44feb3ce
TT
13889 CATCH_PERMANENT,
13890 CATCH_TEMPORARY);
c5aa993b 13891
1bedd215
AC
13892 c = add_com ("watch", class_breakpoint, watch_command, _("\
13893Set a watchpoint for an expression.\n\
06a64a0b 13894Usage: watch [-l|-location] EXPRESSION\n\
c906108c 13895A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13896an expression changes.\n\
13897If -l or -location is given, this evaluates EXPRESSION and watches\n\
13898the memory to which it refers."));
65d12d83 13899 set_cmd_completer (c, expression_completer);
c906108c 13900
1bedd215
AC
13901 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13902Set a read watchpoint for an expression.\n\
06a64a0b 13903Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 13904A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13905an expression is read.\n\
13906If -l or -location is given, this evaluates EXPRESSION and watches\n\
13907the memory to which it refers."));
65d12d83 13908 set_cmd_completer (c, expression_completer);
c906108c 13909
1bedd215
AC
13910 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13911Set a watchpoint for an expression.\n\
06a64a0b 13912Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 13913A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13914an expression is either read or written.\n\
13915If -l or -location is given, this evaluates EXPRESSION and watches\n\
13916the memory to which it refers."));
65d12d83 13917 set_cmd_completer (c, expression_completer);
c906108c 13918
d77f58be 13919 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 13920Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 13921
920d2a44
AC
13922 /* XXX: cagney/2005-02-23: This should be a boolean, and should
13923 respond to changes - contrary to the description. */
85c07804
AC
13924 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
13925 &can_use_hw_watchpoints, _("\
13926Set debugger's willingness to use watchpoint hardware."), _("\
13927Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
13928If zero, gdb will not use hardware for new watchpoints, even if\n\
13929such is available. (However, any hardware watchpoints that were\n\
13930created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
13931hardware.)"),
13932 NULL,
920d2a44 13933 show_can_use_hw_watchpoints,
85c07804 13934 &setlist, &showlist);
c906108c
SS
13935
13936 can_use_hw_watchpoints = 1;
fa8d40ab 13937
1042e4c0
SS
13938 /* Tracepoint manipulation commands. */
13939
13940 c = add_com ("trace", class_breakpoint, trace_command, _("\
13941Set a tracepoint at specified line or function.\n\
13942\n"
13943BREAK_ARGS_HELP ("trace") "\n\
13944Do \"help tracepoints\" for info on other tracepoint commands."));
13945 set_cmd_completer (c, location_completer);
13946
13947 add_com_alias ("tp", "trace", class_alias, 0);
13948 add_com_alias ("tr", "trace", class_alias, 1);
13949 add_com_alias ("tra", "trace", class_alias, 1);
13950 add_com_alias ("trac", "trace", class_alias, 1);
13951
7a697b8d
SS
13952 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
13953Set a fast tracepoint at specified line or function.\n\
13954\n"
13955BREAK_ARGS_HELP ("ftrace") "\n\
13956Do \"help tracepoints\" for info on other tracepoint commands."));
13957 set_cmd_completer (c, location_completer);
13958
0fb4aa4b
PA
13959 c = add_com ("strace", class_breakpoint, strace_command, _("\
13960Set a static tracepoint at specified line, function or marker.\n\
13961\n\
13962strace [LOCATION] [if CONDITION]\n\
13963LOCATION may be a line number, function name, \"*\" and an address,\n\
13964or -m MARKER_ID.\n\
13965If a line number is specified, probe the marker at start of code\n\
13966for that line. If a function is specified, probe the marker at start\n\
13967of code for that function. If an address is specified, probe the marker\n\
13968at that exact address. If a marker id is specified, probe the marker\n\
13969with that name. With no LOCATION, uses current execution address of\n\
13970the selected stack frame.\n\
13971Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
13972This collects arbitrary user data passed in the probe point call to the\n\
13973tracing library. You can inspect it when analyzing the trace buffer,\n\
13974by printing the $_sdata variable like any other convenience variable.\n\
13975\n\
13976CONDITION is a boolean expression.\n\
13977\n\
d41c0fc8
PA
13978Multiple tracepoints at one place are permitted, and useful if their\n\
13979conditions are different.\n\
0fb4aa4b
PA
13980\n\
13981Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
13982Do \"help tracepoints\" for info on other tracepoint commands."));
13983 set_cmd_completer (c, location_completer);
13984
1042e4c0 13985 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 13986Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
13987Convenience variable \"$tpnum\" contains the number of the\n\
13988last tracepoint set."));
13989
13990 add_info_alias ("tp", "tracepoints", 1);
13991
13992 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
13993Delete specified tracepoints.\n\
13994Arguments are tracepoint numbers, separated by spaces.\n\
13995No argument means delete all tracepoints."),
13996 &deletelist);
13997
13998 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13999Disable specified tracepoints.\n\
14000Arguments are tracepoint numbers, separated by spaces.\n\
14001No argument means disable all tracepoints."),
14002 &disablelist);
14003 deprecate_cmd (c, "disable");
14004
14005 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14006Enable specified tracepoints.\n\
14007Arguments are tracepoint numbers, separated by spaces.\n\
14008No argument means enable all tracepoints."),
14009 &enablelist);
14010 deprecate_cmd (c, "enable");
14011
14012 add_com ("passcount", class_trace, trace_pass_command, _("\
14013Set the passcount for a tracepoint.\n\
14014The trace will end when the tracepoint has been passed 'count' times.\n\
14015Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14016if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14017
6149aea9
PA
14018 add_prefix_cmd ("save", class_breakpoint, save_command,
14019 _("Save breakpoint definitions as a script."),
14020 &save_cmdlist, "save ",
14021 0/*allow-unknown*/, &cmdlist);
14022
14023 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14024Save current breakpoint definitions as a script.\n\
cce7e648 14025This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14026catchpoints, tracepoints). Use the 'source' command in another debug\n\
14027session to restore them."),
14028 &save_cmdlist);
14029 set_cmd_completer (c, filename_completer);
14030
14031 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14032Save current tracepoint definitions as a script.\n\
6149aea9
PA
14033Use the 'source' command in another debug session to restore them."),
14034 &save_cmdlist);
1042e4c0
SS
14035 set_cmd_completer (c, filename_completer);
14036
6149aea9
PA
14037 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
14038 deprecate_cmd (c, "save tracepoints");
14039
1bedd215 14040 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
14041Breakpoint specific settings\n\
14042Configure various breakpoint-specific variables such as\n\
1bedd215 14043pending breakpoint behavior"),
fa8d40ab
JJ
14044 &breakpoint_set_cmdlist, "set breakpoint ",
14045 0/*allow-unknown*/, &setlist);
1bedd215 14046 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
14047Breakpoint specific settings\n\
14048Configure various breakpoint-specific variables such as\n\
1bedd215 14049pending breakpoint behavior"),
fa8d40ab
JJ
14050 &breakpoint_show_cmdlist, "show breakpoint ",
14051 0/*allow-unknown*/, &showlist);
14052
7915a72c
AC
14053 add_setshow_auto_boolean_cmd ("pending", no_class,
14054 &pending_break_support, _("\
14055Set debugger's behavior regarding pending breakpoints."), _("\
14056Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14057If on, an unrecognized breakpoint location will cause gdb to create a\n\
14058pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14059an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14060user-query to see if a pending breakpoint should be created."),
2c5b56ce 14061 NULL,
920d2a44 14062 show_pending_break_support,
6e1d7d6c
AC
14063 &breakpoint_set_cmdlist,
14064 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14065
14066 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14067
14068 add_setshow_boolean_cmd ("auto-hw", no_class,
14069 &automatic_hardware_breakpoints, _("\
14070Set automatic usage of hardware breakpoints."), _("\
14071Show automatic usage of hardware breakpoints."), _("\
14072If set, the debugger will automatically use hardware breakpoints for\n\
14073breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14074a warning will be emitted for such breakpoints."),
14075 NULL,
14076 show_automatic_hardware_breakpoints,
14077 &breakpoint_set_cmdlist,
14078 &breakpoint_show_cmdlist);
74960c60 14079
33e5cbd6
PA
14080 add_setshow_enum_cmd ("always-inserted", class_support,
14081 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
14082Set mode for inserting breakpoints."), _("\
14083Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
14084When this mode is off, breakpoints are inserted in inferior when it is\n\
14085resumed, and removed when execution stops. When this mode is on,\n\
14086breakpoints are inserted immediately and removed only when the user\n\
14087deletes the breakpoint. When this mode is auto (which is the default),\n\
14088the behaviour depends on the non-stop setting (see help set non-stop).\n\
14089In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
14090behaves as if always-inserted mode is on; if gdb is controlling the\n\
14091inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
14092 NULL,
14093 &show_always_inserted_mode,
14094 &breakpoint_set_cmdlist,
14095 &breakpoint_show_cmdlist);
f1310107
TJB
14096
14097 add_com ("break-range", class_breakpoint, break_range_command, _("\
14098Set a breakpoint for an address range.\n\
14099break-range START-LOCATION, END-LOCATION\n\
14100where START-LOCATION and END-LOCATION can be one of the following:\n\
14101 LINENUM, for that line in the current file,\n\
14102 FILE:LINENUM, for that line in that file,\n\
14103 +OFFSET, for that number of lines after the current line\n\
14104 or the start of the range\n\
14105 FUNCTION, for the first line in that function,\n\
14106 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14107 *ADDRESS, for the instruction at that address.\n\
14108\n\
14109The breakpoint will stop execution of the inferior whenever it executes\n\
14110an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14111range (including START-LOCATION and END-LOCATION)."));
14112
765dc015 14113 automatic_hardware_breakpoints = 1;
f3b1572e
PA
14114
14115 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 14116}
This page took 2.258011 seconds and 4 git commands to generate.