* python/lib/gdb/command/pretty_printers.py
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1 4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7b6bb8da 5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
a6d9a66e 23#include "arch-utils.h"
c906108c 24#include <ctype.h>
776592bf 25#include "hashtab.h"
c906108c
SS
26#include "symtab.h"
27#include "frame.h"
28#include "breakpoint.h"
1042e4c0 29#include "tracepoint.h"
c906108c
SS
30#include "gdbtypes.h"
31#include "expression.h"
32#include "gdbcore.h"
33#include "gdbcmd.h"
34#include "value.h"
35#include "command.h"
36#include "inferior.h"
37#include "gdbthread.h"
38#include "target.h"
39#include "language.h"
40#include "gdb_string.h"
50f182aa 41#include "gdb-demangle.h"
0ba1096a 42#include "filenames.h"
c906108c
SS
43#include "annotate.h"
44#include "symfile.h"
45#include "objfiles.h"
0378c332 46#include "source.h"
c5f0f3d0 47#include "linespec.h"
c94fdfd0 48#include "completer.h"
5b7f31a4 49#include "gdb.h"
8b93c638 50#include "ui-out.h"
e1507482 51#include "cli/cli-script.h"
0225421b 52#include "gdb_assert.h"
fe898f56 53#include "block.h"
a77053c2 54#include "solib.h"
84acb35a
JJ
55#include "solist.h"
56#include "observer.h"
60250e8b 57#include "exceptions.h"
765dc015 58#include "memattr.h"
f7f9143b 59#include "ada-lang.h"
d1aa2f50 60#include "top.h"
fa4727a6 61#include "wrapper.h"
79a45b7d 62#include "valprint.h"
4efc6507 63#include "jit.h"
a96d9b2e 64#include "xml-syscall.h"
65d79d4b 65#include "parser-defs.h"
e9cafbcc 66#include "cli/cli-utils.h"
be34f849 67#include "continuations.h"
1bfeeb0f
JL
68#include "stack.h"
69#include "skip.h"
049e32d3 70#include "record.h"
c906108c 71
1042e4c0
SS
72/* readline include files */
73#include "readline/readline.h"
74#include "readline/history.h"
75
76/* readline defines this. */
77#undef savestring
78
034dad6f 79#include "mi/mi-common.h"
7371cf6d 80#include "python/python.h"
104c1213 81
4a64f543 82/* Prototypes for local functions. */
c906108c 83
a14ed312 84static void enable_delete_command (char *, int);
c906108c 85
a14ed312 86static void enable_once_command (char *, int);
c906108c 87
a14ed312 88static void disable_command (char *, int);
c906108c 89
a14ed312 90static void enable_command (char *, int);
c906108c 91
95a42b64
TT
92static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
93 void *),
94 void *);
c906108c 95
a14ed312 96static void ignore_command (char *, int);
c906108c 97
4efb68b1 98static int breakpoint_re_set_one (void *);
c906108c 99
348d480f
PA
100static void breakpoint_re_set_default (struct breakpoint *);
101
a14ed312 102static void clear_command (char *, int);
c906108c 103
a14ed312 104static void catch_command (char *, int);
c906108c 105
a9634178 106static int can_use_hardware_watchpoint (struct value *);
c906108c 107
98deb0da 108static void break_command_1 (char *, int, int);
c906108c 109
a14ed312 110static void mention (struct breakpoint *);
c906108c 111
348d480f
PA
112static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
113 enum bptype,
c0a91b2b 114 const struct breakpoint_ops *);
3742cc8b
YQ
115static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
116 const struct symtab_and_line *);
117
4a64f543
MS
118/* This function is used in gdbtk sources and thus can not be made
119 static. */
63c252f8 120struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 121 struct symtab_and_line,
c0a91b2b
TT
122 enum bptype,
123 const struct breakpoint_ops *);
c906108c 124
06edf0c0
PA
125static struct breakpoint *
126 momentary_breakpoint_from_master (struct breakpoint *orig,
127 enum bptype type,
c0a91b2b 128 const struct breakpoint_ops *ops);
06edf0c0 129
76897487
KB
130static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
131
a6d9a66e
UW
132static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
133 CORE_ADDR bpaddr,
88f7da05 134 enum bptype bptype);
76897487 135
6c95b8df
PA
136static void describe_other_breakpoints (struct gdbarch *,
137 struct program_space *, CORE_ADDR,
5af949e3 138 struct obj_section *, int);
c906108c 139
6c95b8df
PA
140static int breakpoint_address_match (struct address_space *aspace1,
141 CORE_ADDR addr1,
142 struct address_space *aspace2,
143 CORE_ADDR addr2);
144
85d721b8
PA
145static int watchpoint_locations_match (struct bp_location *loc1,
146 struct bp_location *loc2);
147
f1310107
TJB
148static int breakpoint_location_address_match (struct bp_location *bl,
149 struct address_space *aspace,
150 CORE_ADDR addr);
151
a14ed312 152static void breakpoints_info (char *, int);
c906108c 153
d77f58be
SS
154static void watchpoints_info (char *, int);
155
e5a67952
MS
156static int breakpoint_1 (char *, int,
157 int (*) (const struct breakpoint *));
c906108c 158
4efb68b1 159static int breakpoint_cond_eval (void *);
c906108c 160
4efb68b1 161static void cleanup_executing_breakpoints (void *);
c906108c 162
a14ed312 163static void commands_command (char *, int);
c906108c 164
a14ed312 165static void condition_command (char *, int);
c906108c 166
c5aa993b
JM
167typedef enum
168 {
169 mark_inserted,
170 mark_uninserted
171 }
172insertion_state_t;
c906108c 173
0bde7532 174static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 175static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 176
e514a9d6 177static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 178
4efb68b1 179static int watchpoint_check (void *);
c906108c 180
a14ed312 181static void maintenance_info_breakpoints (char *, int);
c906108c 182
a14ed312 183static int hw_breakpoint_used_count (void);
c906108c 184
a1398e0c
PA
185static int hw_watchpoint_use_count (struct breakpoint *);
186
187static int hw_watchpoint_used_count_others (struct breakpoint *except,
188 enum bptype type,
189 int *other_type_used);
c906108c 190
a14ed312 191static void hbreak_command (char *, int);
c906108c 192
a14ed312 193static void thbreak_command (char *, int);
c906108c 194
51be5b68 195static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp);
c906108c 196
a14ed312 197static void stop_command (char *arg, int from_tty);
7a292a7a 198
a14ed312 199static void stopin_command (char *arg, int from_tty);
7a292a7a 200
a14ed312 201static void stopat_command (char *arg, int from_tty);
7a292a7a 202
a14ed312 203static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 204
d85310f7
MS
205static void catch_exception_command_1 (enum exception_event_kind ex_event,
206 char *arg, int tempflag, int from_tty);
7a292a7a 207
a14ed312 208static void tcatch_command (char *arg, int from_tty);
7a292a7a 209
d03285ec
UW
210static void detach_single_step_breakpoints (void);
211
6c95b8df
PA
212static int single_step_breakpoint_inserted_here_p (struct address_space *,
213 CORE_ADDR pc);
1aafd4da 214
fe3f5fa8 215static void free_bp_location (struct bp_location *loc);
f431efe5
PA
216static void incref_bp_location (struct bp_location *loc);
217static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 218
39d61571 219static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 220
b60e7edf 221static void update_global_location_list (int);
a5606eee 222
b60e7edf 223static void update_global_location_list_nothrow (int);
74960c60 224
d77f58be 225static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
226
227static void insert_breakpoint_locations (void);
a5606eee 228
a96d9b2e
SDJ
229static int syscall_catchpoint_p (struct breakpoint *b);
230
1042e4c0
SS
231static void tracepoints_info (char *, int);
232
233static void delete_trace_command (char *, int);
234
235static void enable_trace_command (char *, int);
236
237static void disable_trace_command (char *, int);
238
239static void trace_pass_command (char *, int);
240
9c06b0b4
TJB
241static int is_masked_watchpoint (const struct breakpoint *b);
242
0fb4aa4b
PA
243/* Assuming we're creating a static tracepoint, does S look like a
244 static tracepoint marker spec ("-m MARKER_ID")? */
245#define is_marker_spec(s) \
f5a8e22b 246 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 247
2060206e
PA
248/* The abstract base class all breakpoint_ops structures inherit
249 from. */
250static struct breakpoint_ops base_breakpoint_ops;
251
252/* The breakpoint_ops structure to be inherited by all breakpoint_ops
253 that are implemented on top of software or hardware breakpoints
254 (user breakpoints, internal and momentary breakpoints, etc.). */
255static struct breakpoint_ops bkpt_base_breakpoint_ops;
256
257/* Internal breakpoints class type. */
06edf0c0 258static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
259
260/* Momentary breakpoints class type. */
06edf0c0
PA
261static struct breakpoint_ops momentary_breakpoint_ops;
262
2060206e
PA
263/* The breakpoint_ops structure to be used in regular user created
264 breakpoints. */
265struct breakpoint_ops bkpt_breakpoint_ops;
266
5cea2a26
PA
267/* A reference-counted struct command_line. This lets multiple
268 breakpoints share a single command list. */
269struct counted_command_line
270{
271 /* The reference count. */
272 int refc;
273
274 /* The command list. */
275 struct command_line *commands;
276};
277
278struct command_line *
279breakpoint_commands (struct breakpoint *b)
280{
281 return b->commands ? b->commands->commands : NULL;
282}
3daf8fe5 283
f3b1572e
PA
284/* Flag indicating that a command has proceeded the inferior past the
285 current breakpoint. */
286
287static int breakpoint_proceeded;
288
956a9fb9 289const char *
2cec12e5
AR
290bpdisp_text (enum bpdisp disp)
291{
4a64f543
MS
292 /* NOTE: the following values are a part of MI protocol and
293 represent values of 'disp' field returned when inferior stops at
294 a breakpoint. */
bc043ef3 295 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 296
2cec12e5
AR
297 return bpdisps[(int) disp];
298}
c906108c 299
4a64f543 300/* Prototypes for exported functions. */
c906108c 301/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 302 if such is available. */
c906108c
SS
303static int can_use_hw_watchpoints;
304
920d2a44
AC
305static void
306show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
307 struct cmd_list_element *c,
308 const char *value)
309{
3e43a32a
MS
310 fprintf_filtered (file,
311 _("Debugger's willingness to use "
312 "watchpoint hardware is %s.\n"),
920d2a44
AC
313 value);
314}
315
fa8d40ab
JJ
316/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
317 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 318 for unrecognized breakpoint locations.
fa8d40ab
JJ
319 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
320static enum auto_boolean pending_break_support;
920d2a44
AC
321static void
322show_pending_break_support (struct ui_file *file, int from_tty,
323 struct cmd_list_element *c,
324 const char *value)
325{
3e43a32a
MS
326 fprintf_filtered (file,
327 _("Debugger's behavior regarding "
328 "pending breakpoints is %s.\n"),
920d2a44
AC
329 value);
330}
fa8d40ab 331
765dc015 332/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 333 set with "break" but falling in read-only memory.
765dc015
VP
334 If 0, gdb will warn about such breakpoints, but won't automatically
335 use hardware breakpoints. */
336static int automatic_hardware_breakpoints;
337static void
338show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
339 struct cmd_list_element *c,
340 const char *value)
341{
3e43a32a
MS
342 fprintf_filtered (file,
343 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
344 value);
345}
346
33e5cbd6
PA
347/* If on, gdb will keep breakpoints inserted even as inferior is
348 stopped, and immediately insert any new breakpoints. If off, gdb
349 will insert breakpoints into inferior only when resuming it, and
350 will remove breakpoints upon stop. If auto, GDB will behave as ON
351 if in non-stop mode, and as OFF if all-stop mode.*/
352
353static const char always_inserted_auto[] = "auto";
354static const char always_inserted_on[] = "on";
355static const char always_inserted_off[] = "off";
356static const char *always_inserted_enums[] = {
357 always_inserted_auto,
358 always_inserted_off,
359 always_inserted_on,
360 NULL
361};
362static const char *always_inserted_mode = always_inserted_auto;
363static void
74960c60 364show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 365 struct cmd_list_element *c, const char *value)
74960c60 366{
33e5cbd6 367 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
368 fprintf_filtered (file,
369 _("Always inserted breakpoint "
370 "mode is %s (currently %s).\n"),
33e5cbd6
PA
371 value,
372 breakpoints_always_inserted_mode () ? "on" : "off");
373 else
3e43a32a
MS
374 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
375 value);
74960c60
VP
376}
377
33e5cbd6
PA
378int
379breakpoints_always_inserted_mode (void)
380{
049e32d3
PA
381 return ((always_inserted_mode == always_inserted_on
382 || (always_inserted_mode == always_inserted_auto && non_stop))
383 && !RECORD_IS_USED);
33e5cbd6 384}
765dc015 385
a14ed312 386void _initialize_breakpoint (void);
c906108c 387
c906108c
SS
388/* Are we executing breakpoint commands? */
389static int executing_breakpoint_commands;
390
c02f5703
MS
391/* Are overlay event breakpoints enabled? */
392static int overlay_events_enabled;
393
e09342b5
TJB
394/* See description in breakpoint.h. */
395int target_exact_watchpoints = 0;
396
c906108c 397/* Walk the following statement or block through all breakpoints.
e5dd4106 398 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 399 current breakpoint. */
c906108c 400
5c44784c 401#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 402
5c44784c
JM
403#define ALL_BREAKPOINTS_SAFE(B,TMP) \
404 for (B = breakpoint_chain; \
405 B ? (TMP=B->next, 1): 0; \
406 B = TMP)
c906108c 407
4a64f543
MS
408/* Similar iterator for the low-level breakpoints. SAFE variant is
409 not provided so update_global_location_list must not be called
410 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 411
876fa593
JK
412#define ALL_BP_LOCATIONS(B,BP_TMP) \
413 for (BP_TMP = bp_location; \
414 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
415 BP_TMP++)
7cc221ef 416
1042e4c0
SS
417/* Iterator for tracepoints only. */
418
419#define ALL_TRACEPOINTS(B) \
420 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 421 if (is_tracepoint (B))
1042e4c0 422
7cc221ef 423/* Chains of all breakpoints defined. */
c906108c
SS
424
425struct breakpoint *breakpoint_chain;
426
876fa593
JK
427/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
428
429static struct bp_location **bp_location;
430
431/* Number of elements of BP_LOCATION. */
432
433static unsigned bp_location_count;
434
4a64f543
MS
435/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
436 ADDRESS for the current elements of BP_LOCATION which get a valid
437 result from bp_location_has_shadow. You can use it for roughly
438 limiting the subrange of BP_LOCATION to scan for shadow bytes for
439 an address you need to read. */
876fa593
JK
440
441static CORE_ADDR bp_location_placed_address_before_address_max;
442
4a64f543
MS
443/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
444 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
445 BP_LOCATION which get a valid result from bp_location_has_shadow.
446 You can use it for roughly limiting the subrange of BP_LOCATION to
447 scan for shadow bytes for an address you need to read. */
876fa593
JK
448
449static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 450
4a64f543
MS
451/* The locations that no longer correspond to any breakpoint, unlinked
452 from bp_location array, but for which a hit may still be reported
453 by a target. */
20874c92
VP
454VEC(bp_location_p) *moribund_locations = NULL;
455
c906108c
SS
456/* Number of last breakpoint made. */
457
95a42b64
TT
458static int breakpoint_count;
459
86b17b60
PA
460/* The value of `breakpoint_count' before the last command that
461 created breakpoints. If the last (break-like) command created more
462 than one breakpoint, then the difference between BREAKPOINT_COUNT
463 and PREV_BREAKPOINT_COUNT is more than one. */
464static int prev_breakpoint_count;
c906108c 465
1042e4c0
SS
466/* Number of last tracepoint made. */
467
95a42b64 468static int tracepoint_count;
1042e4c0 469
6149aea9
PA
470static struct cmd_list_element *breakpoint_set_cmdlist;
471static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 472struct cmd_list_element *save_cmdlist;
6149aea9 473
468d015d
JJ
474/* Return whether a breakpoint is an active enabled breakpoint. */
475static int
476breakpoint_enabled (struct breakpoint *b)
477{
0d381245 478 return (b->enable_state == bp_enabled);
468d015d
JJ
479}
480
c906108c
SS
481/* Set breakpoint count to NUM. */
482
95a42b64 483static void
fba45db2 484set_breakpoint_count (int num)
c906108c 485{
86b17b60 486 prev_breakpoint_count = breakpoint_count;
c906108c 487 breakpoint_count = num;
4fa62494 488 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
489}
490
86b17b60
PA
491/* Used by `start_rbreak_breakpoints' below, to record the current
492 breakpoint count before "rbreak" creates any breakpoint. */
493static int rbreak_start_breakpoint_count;
494
95a42b64
TT
495/* Called at the start an "rbreak" command to record the first
496 breakpoint made. */
86b17b60 497
95a42b64
TT
498void
499start_rbreak_breakpoints (void)
500{
86b17b60 501 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
502}
503
504/* Called at the end of an "rbreak" command to record the last
505 breakpoint made. */
86b17b60 506
95a42b64
TT
507void
508end_rbreak_breakpoints (void)
509{
86b17b60 510 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
511}
512
4a64f543 513/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
514
515void
fba45db2 516clear_breakpoint_hit_counts (void)
c906108c
SS
517{
518 struct breakpoint *b;
519
520 ALL_BREAKPOINTS (b)
521 b->hit_count = 0;
522}
523
9add0f1b
TT
524/* Allocate a new counted_command_line with reference count of 1.
525 The new structure owns COMMANDS. */
526
527static struct counted_command_line *
528alloc_counted_command_line (struct command_line *commands)
529{
530 struct counted_command_line *result
531 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 532
9add0f1b
TT
533 result->refc = 1;
534 result->commands = commands;
535 return result;
536}
537
538/* Increment reference count. This does nothing if CMD is NULL. */
539
540static void
541incref_counted_command_line (struct counted_command_line *cmd)
542{
543 if (cmd)
544 ++cmd->refc;
545}
546
547/* Decrement reference count. If the reference count reaches 0,
548 destroy the counted_command_line. Sets *CMDP to NULL. This does
549 nothing if *CMDP is NULL. */
550
551static void
552decref_counted_command_line (struct counted_command_line **cmdp)
553{
554 if (*cmdp)
555 {
556 if (--(*cmdp)->refc == 0)
557 {
558 free_command_lines (&(*cmdp)->commands);
559 xfree (*cmdp);
560 }
561 *cmdp = NULL;
562 }
563}
564
565/* A cleanup function that calls decref_counted_command_line. */
566
567static void
568do_cleanup_counted_command_line (void *arg)
569{
570 decref_counted_command_line (arg);
571}
572
573/* Create a cleanup that calls decref_counted_command_line on the
574 argument. */
575
576static struct cleanup *
577make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
578{
579 return make_cleanup (do_cleanup_counted_command_line, cmdp);
580}
581
c906108c 582\f
48cb2d85
VP
583/* Return the breakpoint with the specified number, or NULL
584 if the number does not refer to an existing breakpoint. */
585
586struct breakpoint *
587get_breakpoint (int num)
588{
589 struct breakpoint *b;
590
591 ALL_BREAKPOINTS (b)
592 if (b->number == num)
593 return b;
594
595 return NULL;
596}
5c44784c 597
c906108c 598\f
adc36818
PM
599
600void
601set_breakpoint_condition (struct breakpoint *b, char *exp,
602 int from_tty)
603{
3a5c3e22
PA
604 xfree (b->cond_string);
605 b->cond_string = NULL;
adc36818 606
3a5c3e22 607 if (is_watchpoint (b))
adc36818 608 {
3a5c3e22
PA
609 struct watchpoint *w = (struct watchpoint *) b;
610
611 xfree (w->cond_exp);
612 w->cond_exp = NULL;
613 }
614 else
615 {
616 struct bp_location *loc;
617
618 for (loc = b->loc; loc; loc = loc->next)
619 {
620 xfree (loc->cond);
621 loc->cond = NULL;
622 }
adc36818 623 }
adc36818
PM
624
625 if (*exp == 0)
626 {
627 if (from_tty)
628 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
629 }
630 else
631 {
632 char *arg = exp;
cc59ec59 633
adc36818
PM
634 /* I don't know if it matters whether this is the string the user
635 typed in or the decompiled expression. */
636 b->cond_string = xstrdup (arg);
637 b->condition_not_parsed = 0;
638
639 if (is_watchpoint (b))
640 {
3a5c3e22
PA
641 struct watchpoint *w = (struct watchpoint *) b;
642
adc36818
PM
643 innermost_block = NULL;
644 arg = exp;
3a5c3e22 645 w->cond_exp = parse_exp_1 (&arg, 0, 0);
adc36818
PM
646 if (*arg)
647 error (_("Junk at end of expression"));
3a5c3e22 648 w->cond_exp_valid_block = innermost_block;
adc36818
PM
649 }
650 else
651 {
3a5c3e22
PA
652 struct bp_location *loc;
653
adc36818
PM
654 for (loc = b->loc; loc; loc = loc->next)
655 {
656 arg = exp;
657 loc->cond =
658 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
659 if (*arg)
660 error (_("Junk at end of expression"));
661 }
662 }
663 }
664 breakpoints_changed ();
8d3788bd 665 observer_notify_breakpoint_modified (b);
adc36818
PM
666}
667
c906108c
SS
668/* condition N EXP -- set break condition of breakpoint N to EXP. */
669
670static void
fba45db2 671condition_command (char *arg, int from_tty)
c906108c 672{
52f0bd74 673 struct breakpoint *b;
c906108c 674 char *p;
52f0bd74 675 int bnum;
c906108c
SS
676
677 if (arg == 0)
e2e0b3e5 678 error_no_arg (_("breakpoint number"));
c906108c
SS
679
680 p = arg;
681 bnum = get_number (&p);
5c44784c 682 if (bnum == 0)
8a3fe4f8 683 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
684
685 ALL_BREAKPOINTS (b)
686 if (b->number == bnum)
2f069f6f 687 {
7371cf6d
PM
688 /* Check if this breakpoint has a Python object assigned to
689 it, and if it has a definition of the "stop"
690 method. This method and conditions entered into GDB from
691 the CLI are mutually exclusive. */
692 if (b->py_bp_object
693 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
694 error (_("Cannot set a condition where a Python 'stop' "
695 "method has been defined in the breakpoint."));
2566ad2d 696 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
697 return;
698 }
c906108c 699
8a3fe4f8 700 error (_("No breakpoint number %d."), bnum);
c906108c
SS
701}
702
a7bdde9e
VP
703/* Check that COMMAND do not contain commands that are suitable
704 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
705 Throw if any such commands is found. */
706
a7bdde9e
VP
707static void
708check_no_tracepoint_commands (struct command_line *commands)
709{
710 struct command_line *c;
cc59ec59 711
a7bdde9e
VP
712 for (c = commands; c; c = c->next)
713 {
714 int i;
715
716 if (c->control_type == while_stepping_control)
3e43a32a
MS
717 error (_("The 'while-stepping' command can "
718 "only be used for tracepoints"));
a7bdde9e
VP
719
720 for (i = 0; i < c->body_count; ++i)
721 check_no_tracepoint_commands ((c->body_list)[i]);
722
723 /* Not that command parsing removes leading whitespace and comment
4a64f543 724 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
725 command directly. */
726 if (strstr (c->line, "collect ") == c->line)
727 error (_("The 'collect' command can only be used for tracepoints"));
728
51661e93
VP
729 if (strstr (c->line, "teval ") == c->line)
730 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
731 }
732}
733
d77f58be
SS
734/* Encapsulate tests for different types of tracepoints. */
735
d9b3f62e
PA
736static int
737is_tracepoint_type (enum bptype type)
738{
739 return (type == bp_tracepoint
740 || type == bp_fast_tracepoint
741 || type == bp_static_tracepoint);
742}
743
a7bdde9e 744int
d77f58be 745is_tracepoint (const struct breakpoint *b)
a7bdde9e 746{
d9b3f62e 747 return is_tracepoint_type (b->type);
a7bdde9e 748}
d9b3f62e 749
e5dd4106 750/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
751 breakpoint. This function will throw an exception if a problem is
752 found. */
48cb2d85 753
95a42b64
TT
754static void
755validate_commands_for_breakpoint (struct breakpoint *b,
756 struct command_line *commands)
48cb2d85 757{
d77f58be 758 if (is_tracepoint (b))
a7bdde9e 759 {
4a64f543
MS
760 /* We need to verify that each top-level element of commands is
761 valid for tracepoints, that there's at most one
762 while-stepping element, and that while-stepping's body has
763 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
764 struct command_line *c;
765 struct command_line *while_stepping = 0;
766 for (c = commands; c; c = c->next)
767 {
a7bdde9e
VP
768 if (c->control_type == while_stepping_control)
769 {
770 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
771 error (_("The 'while-stepping' command "
772 "cannot be used for fast tracepoint"));
0fb4aa4b 773 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
774 error (_("The 'while-stepping' command "
775 "cannot be used for static tracepoint"));
a7bdde9e
VP
776
777 if (while_stepping)
3e43a32a
MS
778 error (_("The 'while-stepping' command "
779 "can be used only once"));
a7bdde9e
VP
780 else
781 while_stepping = c;
782 }
783 }
784 if (while_stepping)
785 {
786 struct command_line *c2;
787
788 gdb_assert (while_stepping->body_count == 1);
789 c2 = while_stepping->body_list[0];
790 for (; c2; c2 = c2->next)
791 {
a7bdde9e
VP
792 if (c2->control_type == while_stepping_control)
793 error (_("The 'while-stepping' command cannot be nested"));
794 }
795 }
796 }
797 else
798 {
799 check_no_tracepoint_commands (commands);
800 }
95a42b64
TT
801}
802
0fb4aa4b
PA
803/* Return a vector of all the static tracepoints set at ADDR. The
804 caller is responsible for releasing the vector. */
805
806VEC(breakpoint_p) *
807static_tracepoints_here (CORE_ADDR addr)
808{
809 struct breakpoint *b;
810 VEC(breakpoint_p) *found = 0;
811 struct bp_location *loc;
812
813 ALL_BREAKPOINTS (b)
814 if (b->type == bp_static_tracepoint)
815 {
816 for (loc = b->loc; loc; loc = loc->next)
817 if (loc->address == addr)
818 VEC_safe_push(breakpoint_p, found, b);
819 }
820
821 return found;
822}
823
95a42b64 824/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 825 validate that only allowed commands are included. */
95a42b64
TT
826
827void
4a64f543
MS
828breakpoint_set_commands (struct breakpoint *b,
829 struct command_line *commands)
95a42b64
TT
830{
831 validate_commands_for_breakpoint (b, commands);
a7bdde9e 832
9add0f1b
TT
833 decref_counted_command_line (&b->commands);
834 b->commands = alloc_counted_command_line (commands);
48cb2d85 835 breakpoints_changed ();
8d3788bd 836 observer_notify_breakpoint_modified (b);
48cb2d85
VP
837}
838
45a43567
TT
839/* Set the internal `silent' flag on the breakpoint. Note that this
840 is not the same as the "silent" that may appear in the breakpoint's
841 commands. */
842
843void
844breakpoint_set_silent (struct breakpoint *b, int silent)
845{
846 int old_silent = b->silent;
847
848 b->silent = silent;
849 if (old_silent != silent)
8d3788bd 850 observer_notify_breakpoint_modified (b);
45a43567
TT
851}
852
853/* Set the thread for this breakpoint. If THREAD is -1, make the
854 breakpoint work for any thread. */
855
856void
857breakpoint_set_thread (struct breakpoint *b, int thread)
858{
859 int old_thread = b->thread;
860
861 b->thread = thread;
862 if (old_thread != thread)
8d3788bd 863 observer_notify_breakpoint_modified (b);
45a43567
TT
864}
865
866/* Set the task for this breakpoint. If TASK is 0, make the
867 breakpoint work for any task. */
868
869void
870breakpoint_set_task (struct breakpoint *b, int task)
871{
872 int old_task = b->task;
873
874 b->task = task;
875 if (old_task != task)
8d3788bd 876 observer_notify_breakpoint_modified (b);
45a43567
TT
877}
878
95a42b64
TT
879void
880check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
881{
882 struct breakpoint *b = closure;
cc59ec59 883
a7bdde9e
VP
884 validate_actionline (&line, b);
885}
886
95a42b64
TT
887/* A structure used to pass information through
888 map_breakpoint_numbers. */
889
890struct commands_info
891{
892 /* True if the command was typed at a tty. */
893 int from_tty;
86b17b60
PA
894
895 /* The breakpoint range spec. */
896 char *arg;
897
95a42b64
TT
898 /* Non-NULL if the body of the commands are being read from this
899 already-parsed command. */
900 struct command_line *control;
86b17b60 901
95a42b64
TT
902 /* The command lines read from the user, or NULL if they have not
903 yet been read. */
904 struct counted_command_line *cmd;
905};
906
907/* A callback for map_breakpoint_numbers that sets the commands for
908 commands_command. */
909
c906108c 910static void
95a42b64 911do_map_commands_command (struct breakpoint *b, void *data)
c906108c 912{
95a42b64 913 struct commands_info *info = data;
c906108c 914
95a42b64
TT
915 if (info->cmd == NULL)
916 {
917 struct command_line *l;
5c44784c 918
95a42b64
TT
919 if (info->control != NULL)
920 l = copy_command_lines (info->control->body_list[0]);
921 else
86b17b60
PA
922 {
923 struct cleanup *old_chain;
924 char *str;
c5aa993b 925
3e43a32a
MS
926 str = xstrprintf (_("Type commands for breakpoint(s) "
927 "%s, one per line."),
86b17b60
PA
928 info->arg);
929
930 old_chain = make_cleanup (xfree, str);
931
932 l = read_command_lines (str,
933 info->from_tty, 1,
d77f58be 934 (is_tracepoint (b)
86b17b60
PA
935 ? check_tracepoint_command : 0),
936 b);
937
938 do_cleanups (old_chain);
939 }
a7bdde9e 940
95a42b64
TT
941 info->cmd = alloc_counted_command_line (l);
942 }
943
944 /* If a breakpoint was on the list more than once, we don't need to
945 do anything. */
946 if (b->commands != info->cmd)
947 {
948 validate_commands_for_breakpoint (b, info->cmd->commands);
949 incref_counted_command_line (info->cmd);
950 decref_counted_command_line (&b->commands);
951 b->commands = info->cmd;
952 breakpoints_changed ();
8d3788bd 953 observer_notify_breakpoint_modified (b);
c5aa993b 954 }
95a42b64
TT
955}
956
957static void
4a64f543
MS
958commands_command_1 (char *arg, int from_tty,
959 struct command_line *control)
95a42b64
TT
960{
961 struct cleanup *cleanups;
962 struct commands_info info;
963
964 info.from_tty = from_tty;
965 info.control = control;
966 info.cmd = NULL;
967 /* If we read command lines from the user, then `info' will hold an
968 extra reference to the commands that we must clean up. */
969 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
970
971 if (arg == NULL || !*arg)
972 {
86b17b60 973 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
974 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
975 breakpoint_count);
95a42b64
TT
976 else if (breakpoint_count > 0)
977 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
978 else
979 {
980 /* So that we don't try to free the incoming non-NULL
981 argument in the cleanup below. Mapping breakpoint
982 numbers will fail in this case. */
983 arg = NULL;
984 }
95a42b64 985 }
9766ced4
SS
986 else
987 /* The command loop has some static state, so we need to preserve
988 our argument. */
989 arg = xstrdup (arg);
86b17b60
PA
990
991 if (arg != NULL)
992 make_cleanup (xfree, arg);
993
994 info.arg = arg;
95a42b64
TT
995
996 map_breakpoint_numbers (arg, do_map_commands_command, &info);
997
998 if (info.cmd == NULL)
999 error (_("No breakpoints specified."));
1000
1001 do_cleanups (cleanups);
1002}
1003
1004static void
1005commands_command (char *arg, int from_tty)
1006{
1007 commands_command_1 (arg, from_tty, NULL);
c906108c 1008}
40c03ae8
EZ
1009
1010/* Like commands_command, but instead of reading the commands from
1011 input stream, takes them from an already parsed command structure.
1012
1013 This is used by cli-script.c to DTRT with breakpoint commands
1014 that are part of if and while bodies. */
1015enum command_control_type
1016commands_from_control_command (char *arg, struct command_line *cmd)
1017{
95a42b64
TT
1018 commands_command_1 (arg, 0, cmd);
1019 return simple_control;
40c03ae8 1020}
876fa593
JK
1021
1022/* Return non-zero if BL->TARGET_INFO contains valid information. */
1023
1024static int
1025bp_location_has_shadow (struct bp_location *bl)
1026{
1027 if (bl->loc_type != bp_loc_software_breakpoint)
1028 return 0;
1029 if (!bl->inserted)
1030 return 0;
1031 if (bl->target_info.shadow_len == 0)
e5dd4106 1032 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1033 return 0;
1034 return 1;
1035}
1036
8defab1a 1037/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1038 by replacing any memory breakpoints with their shadowed contents.
1039
1040 The range of shadowed area by each bp_location is:
35df4500
TJB
1041 bl->address - bp_location_placed_address_before_address_max
1042 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1043 The range we were requested to resolve shadows for is:
1044 memaddr ... memaddr + len
1045 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1046 memaddr + len <= (bl->address
1047 - bp_location_placed_address_before_address_max)
876fa593 1048 and:
35df4500 1049 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1050
8defab1a
DJ
1051void
1052breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1053{
4a64f543
MS
1054 /* Left boundary, right boundary and median element of our binary
1055 search. */
876fa593
JK
1056 unsigned bc_l, bc_r, bc;
1057
4a64f543
MS
1058 /* Find BC_L which is a leftmost element which may affect BUF
1059 content. It is safe to report lower value but a failure to
1060 report higher one. */
876fa593
JK
1061
1062 bc_l = 0;
1063 bc_r = bp_location_count;
1064 while (bc_l + 1 < bc_r)
1065 {
35df4500 1066 struct bp_location *bl;
876fa593
JK
1067
1068 bc = (bc_l + bc_r) / 2;
35df4500 1069 bl = bp_location[bc];
876fa593 1070
4a64f543
MS
1071 /* Check first BL->ADDRESS will not overflow due to the added
1072 constant. Then advance the left boundary only if we are sure
1073 the BC element can in no way affect the BUF content (MEMADDR
1074 to MEMADDR + LEN range).
876fa593 1075
4a64f543
MS
1076 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1077 offset so that we cannot miss a breakpoint with its shadow
1078 range tail still reaching MEMADDR. */
c5aa993b 1079
35df4500
TJB
1080 if ((bl->address + bp_location_shadow_len_after_address_max
1081 >= bl->address)
1082 && (bl->address + bp_location_shadow_len_after_address_max
1083 <= memaddr))
876fa593
JK
1084 bc_l = bc;
1085 else
1086 bc_r = bc;
1087 }
1088
128070bb
PA
1089 /* Due to the binary search above, we need to make sure we pick the
1090 first location that's at BC_L's address. E.g., if there are
1091 multiple locations at the same address, BC_L may end up pointing
1092 at a duplicate location, and miss the "master"/"inserted"
1093 location. Say, given locations L1, L2 and L3 at addresses A and
1094 B:
1095
1096 L1@A, L2@A, L3@B, ...
1097
1098 BC_L could end up pointing at location L2, while the "master"
1099 location could be L1. Since the `loc->inserted' flag is only set
1100 on "master" locations, we'd forget to restore the shadow of L1
1101 and L2. */
1102 while (bc_l > 0
1103 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1104 bc_l--;
1105
876fa593
JK
1106 /* Now do full processing of the found relevant range of elements. */
1107
1108 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1109 {
35df4500 1110 struct bp_location *bl = bp_location[bc];
876fa593
JK
1111 CORE_ADDR bp_addr = 0;
1112 int bp_size = 0;
1113 int bptoffset = 0;
1114
35df4500
TJB
1115 /* bp_location array has BL->OWNER always non-NULL. */
1116 if (bl->owner->type == bp_none)
8a3fe4f8 1117 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1118 bl->owner->number);
ffce0d52 1119
e5dd4106 1120 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1121 content. */
1122
35df4500
TJB
1123 if (bl->address >= bp_location_placed_address_before_address_max
1124 && memaddr + len <= (bl->address
1125 - bp_location_placed_address_before_address_max))
876fa593
JK
1126 break;
1127
35df4500 1128 if (!bp_location_has_shadow (bl))
c5aa993b 1129 continue;
35df4500 1130 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1131 current_program_space->aspace, 0))
1132 continue;
1133
c5aa993b
JM
1134 /* Addresses and length of the part of the breakpoint that
1135 we need to copy. */
35df4500
TJB
1136 bp_addr = bl->target_info.placed_address;
1137 bp_size = bl->target_info.shadow_len;
8defab1a 1138
c5aa993b
JM
1139 if (bp_addr + bp_size <= memaddr)
1140 /* The breakpoint is entirely before the chunk of memory we
1141 are reading. */
1142 continue;
8defab1a 1143
c5aa993b
JM
1144 if (bp_addr >= memaddr + len)
1145 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1146 reading. */
c5aa993b 1147 continue;
c5aa993b 1148
8defab1a
DJ
1149 /* Offset within shadow_contents. */
1150 if (bp_addr < memaddr)
1151 {
1152 /* Only copy the second part of the breakpoint. */
1153 bp_size -= memaddr - bp_addr;
1154 bptoffset = memaddr - bp_addr;
1155 bp_addr = memaddr;
1156 }
c5aa993b 1157
8defab1a
DJ
1158 if (bp_addr + bp_size > memaddr + len)
1159 {
1160 /* Only copy the first part of the breakpoint. */
1161 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1162 }
c5aa993b 1163
8defab1a 1164 memcpy (buf + bp_addr - memaddr,
35df4500 1165 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1166 }
c906108c 1167}
c906108c 1168\f
c5aa993b 1169
60e1c644
PA
1170/* Return true if BPT is of any hardware watchpoint kind. */
1171
a5606eee 1172static int
d77f58be 1173is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1174{
1175 return (bpt->type == bp_hardware_watchpoint
1176 || bpt->type == bp_read_watchpoint
1177 || bpt->type == bp_access_watchpoint);
1178}
7270d8f2 1179
60e1c644
PA
1180/* Return true if BPT is of any watchpoint kind, hardware or
1181 software. */
1182
3a5c3e22 1183int
d77f58be 1184is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1185{
1186 return (is_hardware_watchpoint (bpt)
1187 || bpt->type == bp_watchpoint);
1188}
1189
3a5c3e22
PA
1190/* Returns true if the current thread and its running state are safe
1191 to evaluate or update watchpoint B. Watchpoints on local
1192 expressions need to be evaluated in the context of the thread that
1193 was current when the watchpoint was created, and, that thread needs
1194 to be stopped to be able to select the correct frame context.
1195 Watchpoints on global expressions can be evaluated on any thread,
1196 and in any state. It is presently left to the target allowing
1197 memory accesses when threads are running. */
f6bc2008
PA
1198
1199static int
3a5c3e22 1200watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008
PA
1201{
1202 return (ptid_equal (b->watchpoint_thread, null_ptid)
1203 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1204 && !is_executing (inferior_ptid)));
1205}
1206
d0fb5eae
JK
1207/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1208 associated bp_watchpoint_scope breakpoint. */
1209
1210static void
3a5c3e22 1211watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1212{
3a5c3e22 1213 struct breakpoint *b = &w->base;
d0fb5eae
JK
1214
1215 if (b->related_breakpoint != b)
1216 {
1217 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1218 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1219 b->related_breakpoint->disposition = disp_del_at_next_stop;
1220 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1221 b->related_breakpoint = b;
1222 }
1223 b->disposition = disp_del_at_next_stop;
1224}
1225
567e1b4e
JB
1226/* Assuming that B is a watchpoint:
1227 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1228 - Evaluate expression and store the result in B->val
567e1b4e
JB
1229 - Evaluate the condition if there is one, and store the result
1230 in b->loc->cond.
a5606eee
VP
1231 - Update the list of values that must be watched in B->loc.
1232
4a64f543
MS
1233 If the watchpoint disposition is disp_del_at_next_stop, then do
1234 nothing. If this is local watchpoint that is out of scope, delete
1235 it.
1236
1237 Even with `set breakpoint always-inserted on' the watchpoints are
1238 removed + inserted on each stop here. Normal breakpoints must
1239 never be removed because they might be missed by a running thread
1240 when debugging in non-stop mode. On the other hand, hardware
1241 watchpoints (is_hardware_watchpoint; processed here) are specific
1242 to each LWP since they are stored in each LWP's hardware debug
1243 registers. Therefore, such LWP must be stopped first in order to
1244 be able to modify its hardware watchpoints.
1245
1246 Hardware watchpoints must be reset exactly once after being
1247 presented to the user. It cannot be done sooner, because it would
1248 reset the data used to present the watchpoint hit to the user. And
1249 it must not be done later because it could display the same single
1250 watchpoint hit during multiple GDB stops. Note that the latter is
1251 relevant only to the hardware watchpoint types bp_read_watchpoint
1252 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1253 not user-visible - its hit is suppressed if the memory content has
1254 not changed.
1255
1256 The following constraints influence the location where we can reset
1257 hardware watchpoints:
1258
1259 * target_stopped_by_watchpoint and target_stopped_data_address are
1260 called several times when GDB stops.
1261
1262 [linux]
1263 * Multiple hardware watchpoints can be hit at the same time,
1264 causing GDB to stop. GDB only presents one hardware watchpoint
1265 hit at a time as the reason for stopping, and all the other hits
1266 are presented later, one after the other, each time the user
1267 requests the execution to be resumed. Execution is not resumed
1268 for the threads still having pending hit event stored in
1269 LWP_INFO->STATUS. While the watchpoint is already removed from
1270 the inferior on the first stop the thread hit event is kept being
1271 reported from its cached value by linux_nat_stopped_data_address
1272 until the real thread resume happens after the watchpoint gets
1273 presented and thus its LWP_INFO->STATUS gets reset.
1274
1275 Therefore the hardware watchpoint hit can get safely reset on the
1276 watchpoint removal from inferior. */
a79d3c27 1277
b40ce68a 1278static void
3a5c3e22 1279update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1280{
a5606eee 1281 int within_current_scope;
a5606eee 1282 struct frame_id saved_frame_id;
66076460 1283 int frame_saved;
a5606eee 1284
f6bc2008
PA
1285 /* If this is a local watchpoint, we only want to check if the
1286 watchpoint frame is in scope if the current thread is the thread
1287 that was used to create the watchpoint. */
1288 if (!watchpoint_in_thread_scope (b))
1289 return;
1290
3a5c3e22 1291 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1292 return;
1293
66076460 1294 frame_saved = 0;
a5606eee
VP
1295
1296 /* Determine if the watchpoint is within scope. */
1297 if (b->exp_valid_block == NULL)
1298 within_current_scope = 1;
1299 else
1300 {
b5db5dfc
UW
1301 struct frame_info *fi = get_current_frame ();
1302 struct gdbarch *frame_arch = get_frame_arch (fi);
1303 CORE_ADDR frame_pc = get_frame_pc (fi);
1304
1305 /* If we're in a function epilogue, unwinding may not work
1306 properly, so do not attempt to recreate locations at this
1307 point. See similar comments in watchpoint_check. */
1308 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1309 return;
66076460
DJ
1310
1311 /* Save the current frame's ID so we can restore it after
1312 evaluating the watchpoint expression on its own frame. */
1313 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1314 took a frame parameter, so that we didn't have to change the
1315 selected frame. */
1316 frame_saved = 1;
1317 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1318
a5606eee
VP
1319 fi = frame_find_by_id (b->watchpoint_frame);
1320 within_current_scope = (fi != NULL);
1321 if (within_current_scope)
1322 select_frame (fi);
1323 }
1324
b5db5dfc
UW
1325 /* We don't free locations. They are stored in the bp_location array
1326 and update_global_location_list will eventually delete them and
1327 remove breakpoints if needed. */
3a5c3e22 1328 b->base.loc = NULL;
b5db5dfc 1329
a5606eee
VP
1330 if (within_current_scope && reparse)
1331 {
1332 char *s;
d63d0675 1333
a5606eee
VP
1334 if (b->exp)
1335 {
1336 xfree (b->exp);
1337 b->exp = NULL;
1338 }
d63d0675 1339 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1340 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1341 /* If the meaning of expression itself changed, the old value is
1342 no longer relevant. We don't want to report a watchpoint hit
1343 to the user when the old value and the new value may actually
1344 be completely different objects. */
1345 value_free (b->val);
fa4727a6
DJ
1346 b->val = NULL;
1347 b->val_valid = 0;
60e1c644
PA
1348
1349 /* Note that unlike with breakpoints, the watchpoint's condition
1350 expression is stored in the breakpoint object, not in the
1351 locations (re)created below. */
3a5c3e22 1352 if (b->base.cond_string != NULL)
60e1c644
PA
1353 {
1354 if (b->cond_exp != NULL)
1355 {
1356 xfree (b->cond_exp);
1357 b->cond_exp = NULL;
1358 }
1359
3a5c3e22 1360 s = b->base.cond_string;
60e1c644
PA
1361 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1362 }
a5606eee 1363 }
a5606eee
VP
1364
1365 /* If we failed to parse the expression, for example because
1366 it refers to a global variable in a not-yet-loaded shared library,
1367 don't try to insert watchpoint. We don't automatically delete
1368 such watchpoint, though, since failure to parse expression
1369 is different from out-of-scope watchpoint. */
2d134ed3
PA
1370 if ( !target_has_execution)
1371 {
1372 /* Without execution, memory can't change. No use to try and
1373 set watchpoint locations. The watchpoint will be reset when
1374 the target gains execution, through breakpoint_re_set. */
1375 }
1376 else if (within_current_scope && b->exp)
a5606eee 1377 {
0cf6dd15 1378 int pc = 0;
fa4727a6 1379 struct value *val_chain, *v, *result, *next;
2d134ed3 1380 struct program_space *frame_pspace;
a5606eee 1381
0cf6dd15 1382 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1383
a5606eee
VP
1384 /* Avoid setting b->val if it's already set. The meaning of
1385 b->val is 'the last value' user saw, and we should update
1386 it only if we reported that last value to user. As it
9c06b0b4
TJB
1387 happens, the code that reports it updates b->val directly.
1388 We don't keep track of the memory value for masked
1389 watchpoints. */
3a5c3e22 1390 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1391 {
1392 b->val = v;
1393 b->val_valid = 1;
1394 }
a5606eee 1395
2d134ed3
PA
1396 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1397
a5606eee 1398 /* Look at each value on the value chain. */
9fa40276 1399 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1400 {
1401 /* If it's a memory location, and GDB actually needed
1402 its contents to evaluate the expression, then we
fa4727a6
DJ
1403 must watch it. If the first value returned is
1404 still lazy, that means an error occurred reading it;
1405 watch it anyway in case it becomes readable. */
a5606eee 1406 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1407 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1408 {
1409 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1410
a5606eee
VP
1411 /* We only watch structs and arrays if user asked
1412 for it explicitly, never if they just happen to
1413 appear in the middle of some value chain. */
fa4727a6 1414 if (v == result
a5606eee
VP
1415 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1416 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1417 {
1418 CORE_ADDR addr;
1419 int len, type;
1420 struct bp_location *loc, **tmp;
1421
42ae5230 1422 addr = value_address (v);
a5606eee
VP
1423 len = TYPE_LENGTH (value_type (v));
1424 type = hw_write;
3a5c3e22 1425 if (b->base.type == bp_read_watchpoint)
a5606eee 1426 type = hw_read;
3a5c3e22 1427 else if (b->base.type == bp_access_watchpoint)
a5606eee 1428 type = hw_access;
3a5c3e22
PA
1429
1430 loc = allocate_bp_location (&b->base);
1431 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1432 ;
1433 *tmp = loc;
a6d9a66e 1434 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1435
1436 loc->pspace = frame_pspace;
a5606eee
VP
1437 loc->address = addr;
1438 loc->length = len;
1439 loc->watchpoint_type = type;
1440 }
1441 }
9fa40276
TJB
1442 }
1443
1444 /* Change the type of breakpoint between hardware assisted or
1445 an ordinary watchpoint depending on the hardware support
1446 and free hardware slots. REPARSE is set when the inferior
1447 is started. */
a9634178 1448 if (reparse)
9fa40276 1449 {
e09342b5 1450 int reg_cnt;
9fa40276
TJB
1451 enum bp_loc_type loc_type;
1452 struct bp_location *bl;
a5606eee 1453
a9634178 1454 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1455
1456 if (reg_cnt)
9fa40276
TJB
1457 {
1458 int i, target_resources_ok, other_type_used;
a1398e0c 1459 enum bptype type;
9fa40276 1460
a9634178
TJB
1461 /* Use an exact watchpoint when there's only one memory region to be
1462 watched, and only one debug register is needed to watch it. */
1463 b->exact = target_exact_watchpoints && reg_cnt == 1;
1464
9fa40276 1465 /* We need to determine how many resources are already
e09342b5
TJB
1466 used for all other hardware watchpoints plus this one
1467 to see if we still have enough resources to also fit
a1398e0c
PA
1468 this watchpoint in as well. */
1469
1470 /* If this is a software watchpoint, we try to turn it
1471 to a hardware one -- count resources as if B was of
1472 hardware watchpoint type. */
1473 type = b->base.type;
1474 if (type == bp_watchpoint)
1475 type = bp_hardware_watchpoint;
1476
1477 /* This watchpoint may or may not have been placed on
1478 the list yet at this point (it won't be in the list
1479 if we're trying to create it for the first time,
1480 through watch_command), so always account for it
1481 manually. */
1482
1483 /* Count resources used by all watchpoints except B. */
1484 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1485
1486 /* Add in the resources needed for B. */
1487 i += hw_watchpoint_use_count (&b->base);
1488
1489 target_resources_ok
1490 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1491 if (target_resources_ok <= 0)
a9634178 1492 {
3a5c3e22 1493 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1494
1495 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1496 error (_("Target does not support this type of "
1497 "hardware watchpoint."));
9c06b0b4
TJB
1498 else if (target_resources_ok < 0 && !sw_mode)
1499 error (_("There are not enough available hardware "
1500 "resources for this watchpoint."));
a1398e0c
PA
1501
1502 /* Downgrade to software watchpoint. */
1503 b->base.type = bp_watchpoint;
1504 }
1505 else
1506 {
1507 /* If this was a software watchpoint, we've just
1508 found we have enough resources to turn it to a
1509 hardware watchpoint. Otherwise, this is a
1510 nop. */
1511 b->base.type = type;
a9634178 1512 }
9fa40276 1513 }
3a5c3e22 1514 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1515 error (_("Expression cannot be implemented with "
1516 "read/access watchpoint."));
9fa40276 1517 else
3a5c3e22 1518 b->base.type = bp_watchpoint;
9fa40276 1519
3a5c3e22 1520 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1521 : bp_loc_hardware_watchpoint);
3a5c3e22 1522 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1523 bl->loc_type = loc_type;
1524 }
1525
1526 for (v = val_chain; v; v = next)
1527 {
a5606eee
VP
1528 next = value_next (v);
1529 if (v != b->val)
1530 value_free (v);
1531 }
1532
c7437ca6
PA
1533 /* If a software watchpoint is not watching any memory, then the
1534 above left it without any location set up. But,
1535 bpstat_stop_status requires a location to be able to report
1536 stops, so make sure there's at least a dummy one. */
3a5c3e22 1537 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1538 {
3a5c3e22
PA
1539 struct breakpoint *base = &b->base;
1540 base->loc = allocate_bp_location (base);
1541 base->loc->pspace = frame_pspace;
1542 base->loc->address = -1;
1543 base->loc->length = -1;
1544 base->loc->watchpoint_type = -1;
c7437ca6 1545 }
a5606eee
VP
1546 }
1547 else if (!within_current_scope)
7270d8f2 1548 {
ac74f770
MS
1549 printf_filtered (_("\
1550Watchpoint %d deleted because the program has left the block\n\
1551in which its expression is valid.\n"),
3a5c3e22 1552 b->base.number);
d0fb5eae 1553 watchpoint_del_at_next_stop (b);
7270d8f2 1554 }
a5606eee
VP
1555
1556 /* Restore the selected frame. */
66076460
DJ
1557 if (frame_saved)
1558 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1559}
1560
a5606eee 1561
74960c60 1562/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1563 inserted in the inferior. We don't differentiate the type of BL's owner
1564 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1565 breakpoint_ops is not defined, because in insert_bp_location,
1566 tracepoint's insert_location will not be called. */
74960c60 1567static int
35df4500 1568should_be_inserted (struct bp_location *bl)
74960c60 1569{
35df4500 1570 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1571 return 0;
1572
35df4500 1573 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1574 return 0;
1575
35df4500 1576 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1577 return 0;
1578
56710373
PA
1579 /* This is set for example, when we're attached to the parent of a
1580 vfork, and have detached from the child. The child is running
1581 free, and we expect it to do an exec or exit, at which point the
1582 OS makes the parent schedulable again (and the target reports
1583 that the vfork is done). Until the child is done with the shared
1584 memory region, do not insert breakpoints in the parent, otherwise
1585 the child could still trip on the parent's breakpoints. Since
1586 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1587 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1588 return 0;
1589
74960c60
VP
1590 return 1;
1591}
1592
934709f0
PW
1593/* Same as should_be_inserted but does the check assuming
1594 that the location is not duplicated. */
1595
1596static int
1597unduplicated_should_be_inserted (struct bp_location *bl)
1598{
1599 int result;
1600 const int save_duplicate = bl->duplicate;
1601
1602 bl->duplicate = 0;
1603 result = should_be_inserted (bl);
1604 bl->duplicate = save_duplicate;
1605 return result;
1606}
1607
35df4500
TJB
1608/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1609 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 1610 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
1611 Returns 0 for success, 1 if the bp_location type is not supported or
1612 -1 for failure.
879bfdc2 1613
4a64f543
MS
1614 NOTE drow/2003-09-09: This routine could be broken down to an
1615 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1616static int
35df4500 1617insert_bp_location (struct bp_location *bl,
26bb91f3 1618 struct ui_file *tmp_error_stream,
3fbb6ffa 1619 int *disabled_breaks,
26bb91f3 1620 int *hw_breakpoint_error)
879bfdc2
DJ
1621{
1622 int val = 0;
1623
35df4500 1624 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1625 return 0;
1626
8181d85f 1627 /* Initialize the target-specific information. */
35df4500
TJB
1628 memset (&bl->target_info, 0, sizeof (bl->target_info));
1629 bl->target_info.placed_address = bl->address;
1630 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1631 bl->target_info.length = bl->length;
8181d85f 1632
35df4500
TJB
1633 if (bl->loc_type == bp_loc_software_breakpoint
1634 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1635 {
35df4500 1636 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1637 {
1638 /* If the explicitly specified breakpoint type
1639 is not hardware breakpoint, check the memory map to see
1640 if the breakpoint address is in read only memory or not.
4a64f543 1641
765dc015
VP
1642 Two important cases are:
1643 - location type is not hardware breakpoint, memory
1644 is readonly. We change the type of the location to
1645 hardware breakpoint.
4a64f543
MS
1646 - location type is hardware breakpoint, memory is
1647 read-write. This means we've previously made the
1648 location hardware one, but then the memory map changed,
1649 so we undo.
765dc015 1650
4a64f543
MS
1651 When breakpoints are removed, remove_breakpoints will use
1652 location types we've just set here, the only possible
1653 problem is that memory map has changed during running
1654 program, but it's not going to work anyway with current
1655 gdb. */
765dc015 1656 struct mem_region *mr
35df4500 1657 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1658
1659 if (mr)
1660 {
1661 if (automatic_hardware_breakpoints)
1662 {
765dc015
VP
1663 enum bp_loc_type new_type;
1664
1665 if (mr->attrib.mode != MEM_RW)
1666 new_type = bp_loc_hardware_breakpoint;
1667 else
1668 new_type = bp_loc_software_breakpoint;
1669
35df4500 1670 if (new_type != bl->loc_type)
765dc015
VP
1671 {
1672 static int said = 0;
cc59ec59 1673
35df4500 1674 bl->loc_type = new_type;
765dc015
VP
1675 if (!said)
1676 {
3e43a32a
MS
1677 fprintf_filtered (gdb_stdout,
1678 _("Note: automatically using "
1679 "hardware breakpoints for "
1680 "read-only addresses.\n"));
765dc015
VP
1681 said = 1;
1682 }
1683 }
1684 }
35df4500 1685 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1686 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1687 warning (_("cannot set software breakpoint "
1688 "at readonly address %s"),
35df4500 1689 paddress (bl->gdbarch, bl->address));
765dc015
VP
1690 }
1691 }
1692
879bfdc2
DJ
1693 /* First check to see if we have to handle an overlay. */
1694 if (overlay_debugging == ovly_off
35df4500
TJB
1695 || bl->section == NULL
1696 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1697 {
1698 /* No overlay handling: just set the breakpoint. */
1699
348d480f 1700 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1701 }
1702 else
1703 {
4a64f543 1704 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1705 Shall we set a breakpoint at the LMA? */
1706 if (!overlay_events_enabled)
1707 {
1708 /* Yes -- overlay event support is not active,
1709 so we must try to set a breakpoint at the LMA.
1710 This will not work for a hardware breakpoint. */
35df4500 1711 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1712 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1713 bl->owner->number);
879bfdc2
DJ
1714 else
1715 {
35df4500
TJB
1716 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1717 bl->section);
879bfdc2 1718 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1719 bl->overlay_target_info = bl->target_info;
1720 bl->overlay_target_info.placed_address = addr;
1721 val = target_insert_breakpoint (bl->gdbarch,
1722 &bl->overlay_target_info);
879bfdc2 1723 if (val != 0)
99361f52 1724 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1725 "Overlay breakpoint %d "
1726 "failed: in ROM?\n",
35df4500 1727 bl->owner->number);
879bfdc2
DJ
1728 }
1729 }
1730 /* Shall we set a breakpoint at the VMA? */
35df4500 1731 if (section_is_mapped (bl->section))
879bfdc2
DJ
1732 {
1733 /* Yes. This overlay section is mapped into memory. */
348d480f 1734 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1735 }
1736 else
1737 {
1738 /* No. This breakpoint will not be inserted.
1739 No error, but do not mark the bp as 'inserted'. */
1740 return 0;
1741 }
1742 }
1743
1744 if (val)
1745 {
1746 /* Can't set the breakpoint. */
35df4500 1747 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1748 {
4a64f543 1749 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1750 val = 0;
35df4500 1751 bl->shlib_disabled = 1;
8d3788bd 1752 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
1753 if (!*disabled_breaks)
1754 {
1755 fprintf_unfiltered (tmp_error_stream,
1756 "Cannot insert breakpoint %d.\n",
1757 bl->owner->number);
1758 fprintf_unfiltered (tmp_error_stream,
1759 "Temporarily disabling shared "
1760 "library breakpoints:\n");
1761 }
1762 *disabled_breaks = 1;
879bfdc2 1763 fprintf_unfiltered (tmp_error_stream,
35df4500 1764 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1765 }
1766 else
879bfdc2 1767 {
35df4500 1768 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1769 {
1770 *hw_breakpoint_error = 1;
3e43a32a
MS
1771 fprintf_unfiltered (tmp_error_stream,
1772 "Cannot insert hardware "
1773 "breakpoint %d.\n",
35df4500 1774 bl->owner->number);
879bfdc2
DJ
1775 }
1776 else
1777 {
1778 fprintf_unfiltered (tmp_error_stream,
1779 "Cannot insert breakpoint %d.\n",
35df4500 1780 bl->owner->number);
879bfdc2
DJ
1781 fprintf_filtered (tmp_error_stream,
1782 "Error accessing memory address ");
35df4500 1783 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1784 tmp_error_stream);
879bfdc2
DJ
1785 fprintf_filtered (tmp_error_stream, ": %s.\n",
1786 safe_strerror (val));
1787 }
1788
1789 }
1790 }
1791 else
35df4500 1792 bl->inserted = 1;
879bfdc2
DJ
1793
1794 return val;
1795 }
1796
35df4500 1797 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1798 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1799 watchpoints. It's not clear that it's necessary... */
35df4500 1800 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1801 {
77b06cd7
TJB
1802 gdb_assert (bl->owner->ops != NULL
1803 && bl->owner->ops->insert_location != NULL);
1804
1805 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1806
1807 /* If trying to set a read-watchpoint, and it turns out it's not
1808 supported, try emulating one with an access watchpoint. */
35df4500 1809 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1810 {
1811 struct bp_location *loc, **loc_temp;
1812
1813 /* But don't try to insert it, if there's already another
1814 hw_access location that would be considered a duplicate
1815 of this one. */
1816 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1817 if (loc != bl
85d721b8 1818 && loc->watchpoint_type == hw_access
35df4500 1819 && watchpoint_locations_match (bl, loc))
85d721b8 1820 {
35df4500
TJB
1821 bl->duplicate = 1;
1822 bl->inserted = 1;
1823 bl->target_info = loc->target_info;
1824 bl->watchpoint_type = hw_access;
85d721b8
PA
1825 val = 0;
1826 break;
1827 }
1828
1829 if (val == 1)
1830 {
77b06cd7
TJB
1831 bl->watchpoint_type = hw_access;
1832 val = bl->owner->ops->insert_location (bl);
1833
1834 if (val)
1835 /* Back to the original value. */
1836 bl->watchpoint_type = hw_read;
85d721b8
PA
1837 }
1838 }
1839
35df4500 1840 bl->inserted = (val == 0);
879bfdc2
DJ
1841 }
1842
35df4500 1843 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1844 {
77b06cd7
TJB
1845 gdb_assert (bl->owner->ops != NULL
1846 && bl->owner->ops->insert_location != NULL);
1847
1848 val = bl->owner->ops->insert_location (bl);
1849 if (val)
1850 {
1851 bl->owner->enable_state = bp_disabled;
1852
1853 if (val == 1)
1854 warning (_("\
1855Error inserting catchpoint %d: Your system does not support this type\n\
1856of catchpoint."), bl->owner->number);
1857 else
1858 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1859 }
1860
1861 bl->inserted = (val == 0);
1640b821
DJ
1862
1863 /* We've already printed an error message if there was a problem
1864 inserting this catchpoint, and we've disabled the catchpoint,
1865 so just return success. */
1866 return 0;
879bfdc2
DJ
1867 }
1868
1869 return 0;
1870}
1871
6c95b8df
PA
1872/* This function is called when program space PSPACE is about to be
1873 deleted. It takes care of updating breakpoints to not reference
1874 PSPACE anymore. */
1875
1876void
1877breakpoint_program_space_exit (struct program_space *pspace)
1878{
1879 struct breakpoint *b, *b_temp;
876fa593 1880 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1881
1882 /* Remove any breakpoint that was set through this program space. */
1883 ALL_BREAKPOINTS_SAFE (b, b_temp)
1884 {
1885 if (b->pspace == pspace)
1886 delete_breakpoint (b);
1887 }
1888
1889 /* Breakpoints set through other program spaces could have locations
1890 bound to PSPACE as well. Remove those. */
876fa593 1891 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1892 {
1893 struct bp_location *tmp;
1894
1895 if (loc->pspace == pspace)
1896 {
2bdf28a0 1897 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1898 if (loc->owner->loc == loc)
1899 loc->owner->loc = loc->next;
1900 else
1901 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1902 if (tmp->next == loc)
1903 {
1904 tmp->next = loc->next;
1905 break;
1906 }
1907 }
1908 }
1909
1910 /* Now update the global location list to permanently delete the
1911 removed locations above. */
1912 update_global_location_list (0);
1913}
1914
74960c60
VP
1915/* Make sure all breakpoints are inserted in inferior.
1916 Throws exception on any error.
1917 A breakpoint that is already inserted won't be inserted
1918 again, so calling this function twice is safe. */
1919void
1920insert_breakpoints (void)
1921{
1922 struct breakpoint *bpt;
1923
1924 ALL_BREAKPOINTS (bpt)
1925 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
1926 {
1927 struct watchpoint *w = (struct watchpoint *) bpt;
1928
1929 update_watchpoint (w, 0 /* don't reparse. */);
1930 }
74960c60 1931
b60e7edf 1932 update_global_location_list (1);
74960c60 1933
c35b1492
PA
1934 /* update_global_location_list does not insert breakpoints when
1935 always_inserted_mode is not enabled. Explicitly insert them
1936 now. */
1937 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1938 insert_breakpoint_locations ();
1939}
1940
c30eee59 1941/* Used when starting or continuing the program. */
c906108c 1942
74960c60
VP
1943static void
1944insert_breakpoint_locations (void)
c906108c 1945{
a5606eee 1946 struct breakpoint *bpt;
35df4500 1947 struct bp_location *bl, **blp_tmp;
e236ba44 1948 int error = 0;
c906108c 1949 int val = 0;
3fbb6ffa 1950 int disabled_breaks = 0;
81d0cc19 1951 int hw_breakpoint_error = 0;
c906108c 1952
81d0cc19 1953 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1954 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1955
81d0cc19
GS
1956 /* Explicitly mark the warning -- this will only be printed if
1957 there was an error. */
1958 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1959
1960 save_current_space_and_thread ();
1961
35df4500 1962 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1963 {
35df4500 1964 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1965 continue;
1966
4a64f543
MS
1967 /* There is no point inserting thread-specific breakpoints if
1968 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1969 has BL->OWNER always non-NULL. */
35df4500
TJB
1970 if (bl->owner->thread != -1
1971 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1972 continue;
1973
35df4500 1974 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1975
1976 /* For targets that support global breakpoints, there's no need
1977 to select an inferior to insert breakpoint to. In fact, even
1978 if we aren't attached to any process yet, we should still
1979 insert breakpoints. */
1980 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1981 && ptid_equal (inferior_ptid, null_ptid))
1982 continue;
1983
3fbb6ffa
TJB
1984 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
1985 &hw_breakpoint_error);
879bfdc2 1986 if (val)
e236ba44 1987 error = val;
879bfdc2 1988 }
c906108c 1989
4a64f543
MS
1990 /* If we failed to insert all locations of a watchpoint, remove
1991 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
1992 ALL_BREAKPOINTS (bpt)
1993 {
1994 int some_failed = 0;
1995 struct bp_location *loc;
1996
1997 if (!is_hardware_watchpoint (bpt))
1998 continue;
1999
d6b74ac4 2000 if (!breakpoint_enabled (bpt))
a5606eee 2001 continue;
74960c60
VP
2002
2003 if (bpt->disposition == disp_del_at_next_stop)
2004 continue;
a5606eee
VP
2005
2006 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2007 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2008 {
2009 some_failed = 1;
2010 break;
2011 }
2012 if (some_failed)
2013 {
2014 for (loc = bpt->loc; loc; loc = loc->next)
2015 if (loc->inserted)
2016 remove_breakpoint (loc, mark_uninserted);
2017
2018 hw_breakpoint_error = 1;
2019 fprintf_unfiltered (tmp_error_stream,
2020 "Could not insert hardware watchpoint %d.\n",
2021 bpt->number);
2022 error = -1;
2023 }
2024 }
2025
e236ba44 2026 if (error)
81d0cc19
GS
2027 {
2028 /* If a hardware breakpoint or watchpoint was inserted, add a
2029 message about possibly exhausted resources. */
879bfdc2 2030 if (hw_breakpoint_error)
81d0cc19 2031 {
c6510018
MS
2032 fprintf_unfiltered (tmp_error_stream,
2033 "Could not insert hardware breakpoints:\n\
2034You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2035 }
81d0cc19
GS
2036 target_terminal_ours_for_output ();
2037 error_stream (tmp_error_stream);
2038 }
f7545552
TT
2039
2040 do_cleanups (cleanups);
c906108c
SS
2041}
2042
c30eee59
TJB
2043/* Used when the program stops.
2044 Returns zero if successful, or non-zero if there was a problem
2045 removing a breakpoint location. */
2046
c906108c 2047int
fba45db2 2048remove_breakpoints (void)
c906108c 2049{
35df4500 2050 struct bp_location *bl, **blp_tmp;
3a1bae8e 2051 int val = 0;
c906108c 2052
35df4500 2053 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2054 {
1e4d1764 2055 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2056 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2057 }
3a1bae8e 2058 return val;
c906108c
SS
2059}
2060
6c95b8df
PA
2061/* Remove breakpoints of process PID. */
2062
2063int
2064remove_breakpoints_pid (int pid)
2065{
35df4500 2066 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2067 int val;
2068 struct inferior *inf = find_inferior_pid (pid);
2069
35df4500 2070 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2071 {
35df4500 2072 if (bl->pspace != inf->pspace)
6c95b8df
PA
2073 continue;
2074
35df4500 2075 if (bl->inserted)
6c95b8df 2076 {
35df4500 2077 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2078 if (val != 0)
2079 return val;
2080 }
2081 }
2082 return 0;
2083}
2084
c906108c 2085int
fba45db2 2086reattach_breakpoints (int pid)
c906108c 2087{
6c95b8df 2088 struct cleanup *old_chain;
35df4500 2089 struct bp_location *bl, **blp_tmp;
c906108c 2090 int val;
86b887df 2091 struct ui_file *tmp_error_stream;
3fbb6ffa 2092 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2093 struct inferior *inf;
2094 struct thread_info *tp;
2095
2096 tp = any_live_thread_of_process (pid);
2097 if (tp == NULL)
2098 return 1;
2099
2100 inf = find_inferior_pid (pid);
2101 old_chain = save_inferior_ptid ();
2102
2103 inferior_ptid = tp->ptid;
a4954f26 2104
86b887df 2105 tmp_error_stream = mem_fileopen ();
a4954f26 2106 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2107
35df4500 2108 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2109 {
35df4500 2110 if (bl->pspace != inf->pspace)
6c95b8df
PA
2111 continue;
2112
35df4500 2113 if (bl->inserted)
c5aa993b 2114 {
35df4500 2115 bl->inserted = 0;
3fbb6ffa 2116 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2117 if (val != 0)
2118 {
ce696e05 2119 do_cleanups (old_chain);
c5aa993b
JM
2120 return val;
2121 }
2122 }
2123 }
ce696e05 2124 do_cleanups (old_chain);
c906108c
SS
2125 return 0;
2126}
2127
e58b0e63
PA
2128static int internal_breakpoint_number = -1;
2129
84f4c1fe
PM
2130/* Set the breakpoint number of B, depending on the value of INTERNAL.
2131 If INTERNAL is non-zero, the breakpoint number will be populated
2132 from internal_breakpoint_number and that variable decremented.
e5dd4106 2133 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2134 breakpoint_count and that value incremented. Internal breakpoints
2135 do not set the internal var bpnum. */
2136static void
2137set_breakpoint_number (int internal, struct breakpoint *b)
2138{
2139 if (internal)
2140 b->number = internal_breakpoint_number--;
2141 else
2142 {
2143 set_breakpoint_count (breakpoint_count + 1);
2144 b->number = breakpoint_count;
2145 }
2146}
2147
e62c965a 2148static struct breakpoint *
a6d9a66e 2149create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2150 CORE_ADDR address, enum bptype type,
c0a91b2b 2151 const struct breakpoint_ops *ops)
e62c965a 2152{
e62c965a
PP
2153 struct symtab_and_line sal;
2154 struct breakpoint *b;
2155
4a64f543 2156 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2157
2158 sal.pc = address;
2159 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2160 sal.pspace = current_program_space;
e62c965a 2161
06edf0c0 2162 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2163 b->number = internal_breakpoint_number--;
2164 b->disposition = disp_donttouch;
2165
2166 return b;
2167}
2168
17450429
PP
2169static const char *const longjmp_names[] =
2170 {
2171 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2172 };
2173#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2174
2175/* Per-objfile data private to breakpoint.c. */
2176struct breakpoint_objfile_data
2177{
2178 /* Minimal symbol for "_ovly_debug_event" (if any). */
2179 struct minimal_symbol *overlay_msym;
2180
2181 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2182 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2183
2184 /* Minimal symbol for "std::terminate()" (if any). */
2185 struct minimal_symbol *terminate_msym;
2186
2187 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2188 struct minimal_symbol *exception_msym;
2189};
2190
2191static const struct objfile_data *breakpoint_objfile_key;
2192
2193/* Minimal symbol not found sentinel. */
2194static struct minimal_symbol msym_not_found;
2195
2196/* Returns TRUE if MSYM point to the "not found" sentinel. */
2197
2198static int
2199msym_not_found_p (const struct minimal_symbol *msym)
2200{
2201 return msym == &msym_not_found;
2202}
2203
2204/* Return per-objfile data needed by breakpoint.c.
2205 Allocate the data if necessary. */
2206
2207static struct breakpoint_objfile_data *
2208get_breakpoint_objfile_data (struct objfile *objfile)
2209{
2210 struct breakpoint_objfile_data *bp_objfile_data;
2211
2212 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2213 if (bp_objfile_data == NULL)
2214 {
2215 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2216 sizeof (*bp_objfile_data));
2217
2218 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2219 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2220 }
2221 return bp_objfile_data;
2222}
2223
e62c965a 2224static void
af02033e 2225create_overlay_event_breakpoint (void)
e62c965a 2226{
69de3c6a 2227 struct objfile *objfile;
af02033e 2228 const char *const func_name = "_ovly_debug_event";
e62c965a 2229
69de3c6a
PP
2230 ALL_OBJFILES (objfile)
2231 {
2232 struct breakpoint *b;
17450429
PP
2233 struct breakpoint_objfile_data *bp_objfile_data;
2234 CORE_ADDR addr;
69de3c6a 2235
17450429
PP
2236 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2237
2238 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2239 continue;
2240
2241 if (bp_objfile_data->overlay_msym == NULL)
2242 {
2243 struct minimal_symbol *m;
2244
2245 m = lookup_minimal_symbol_text (func_name, objfile);
2246 if (m == NULL)
2247 {
2248 /* Avoid future lookups in this objfile. */
2249 bp_objfile_data->overlay_msym = &msym_not_found;
2250 continue;
2251 }
2252 bp_objfile_data->overlay_msym = m;
2253 }
e62c965a 2254
17450429
PP
2255 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2256 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2257 bp_overlay_event,
2258 &internal_breakpoint_ops);
69de3c6a 2259 b->addr_string = xstrdup (func_name);
e62c965a 2260
69de3c6a
PP
2261 if (overlay_debugging == ovly_auto)
2262 {
2263 b->enable_state = bp_enabled;
2264 overlay_events_enabled = 1;
2265 }
2266 else
2267 {
2268 b->enable_state = bp_disabled;
2269 overlay_events_enabled = 0;
2270 }
e62c965a
PP
2271 }
2272 update_global_location_list (1);
2273}
2274
0fd8e87f 2275static void
af02033e 2276create_longjmp_master_breakpoint (void)
0fd8e87f 2277{
6c95b8df 2278 struct program_space *pspace;
6c95b8df
PA
2279 struct cleanup *old_chain;
2280
2281 old_chain = save_current_program_space ();
0fd8e87f 2282
6c95b8df 2283 ALL_PSPACES (pspace)
af02033e
PP
2284 {
2285 struct objfile *objfile;
2286
2287 set_current_program_space (pspace);
2288
2289 ALL_OBJFILES (objfile)
0fd8e87f 2290 {
af02033e
PP
2291 int i;
2292 struct gdbarch *gdbarch;
17450429 2293 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2294
af02033e
PP
2295 gdbarch = get_objfile_arch (objfile);
2296 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2297 continue;
2298
17450429
PP
2299 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2300
2301 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2302 {
2303 struct breakpoint *b;
af02033e 2304 const char *func_name;
17450429 2305 CORE_ADDR addr;
6c95b8df 2306
17450429 2307 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2308 continue;
0fd8e87f 2309
17450429
PP
2310 func_name = longjmp_names[i];
2311 if (bp_objfile_data->longjmp_msym[i] == NULL)
2312 {
2313 struct minimal_symbol *m;
2314
2315 m = lookup_minimal_symbol_text (func_name, objfile);
2316 if (m == NULL)
2317 {
2318 /* Prevent future lookups in this objfile. */
2319 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2320 continue;
2321 }
2322 bp_objfile_data->longjmp_msym[i] = m;
2323 }
2324
2325 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2326 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2327 &internal_breakpoint_ops);
af02033e
PP
2328 b->addr_string = xstrdup (func_name);
2329 b->enable_state = bp_disabled;
2330 }
0fd8e87f 2331 }
af02033e 2332 }
0fd8e87f 2333 update_global_location_list (1);
6c95b8df
PA
2334
2335 do_cleanups (old_chain);
0fd8e87f
UW
2336}
2337
af02033e 2338/* Create a master std::terminate breakpoint. */
aa7d318d 2339static void
af02033e 2340create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2341{
2342 struct program_space *pspace;
aa7d318d 2343 struct cleanup *old_chain;
af02033e 2344 const char *const func_name = "std::terminate()";
aa7d318d
TT
2345
2346 old_chain = save_current_program_space ();
2347
2348 ALL_PSPACES (pspace)
17450429
PP
2349 {
2350 struct objfile *objfile;
2351 CORE_ADDR addr;
2352
2353 set_current_program_space (pspace);
2354
aa7d318d
TT
2355 ALL_OBJFILES (objfile)
2356 {
2357 struct breakpoint *b;
17450429 2358 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2359
17450429 2360 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2361
17450429
PP
2362 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2363 continue;
2364
2365 if (bp_objfile_data->terminate_msym == NULL)
2366 {
2367 struct minimal_symbol *m;
2368
2369 m = lookup_minimal_symbol (func_name, NULL, objfile);
2370 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2371 && MSYMBOL_TYPE (m) != mst_file_text))
2372 {
2373 /* Prevent future lookups in this objfile. */
2374 bp_objfile_data->terminate_msym = &msym_not_found;
2375 continue;
2376 }
2377 bp_objfile_data->terminate_msym = m;
2378 }
aa7d318d 2379
17450429
PP
2380 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2381 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2382 bp_std_terminate_master,
2383 &internal_breakpoint_ops);
aa7d318d
TT
2384 b->addr_string = xstrdup (func_name);
2385 b->enable_state = bp_disabled;
2386 }
17450429
PP
2387 }
2388
aa7d318d
TT
2389 update_global_location_list (1);
2390
2391 do_cleanups (old_chain);
2392}
2393
186c406b
TT
2394/* Install a master breakpoint on the unwinder's debug hook. */
2395
2396void
2397create_exception_master_breakpoint (void)
2398{
2399 struct objfile *objfile;
17450429 2400 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2401
2402 ALL_OBJFILES (objfile)
2403 {
17450429
PP
2404 struct breakpoint *b;
2405 struct gdbarch *gdbarch;
2406 struct breakpoint_objfile_data *bp_objfile_data;
2407 CORE_ADDR addr;
2408
2409 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2410
2411 if (msym_not_found_p (bp_objfile_data->exception_msym))
2412 continue;
2413
2414 gdbarch = get_objfile_arch (objfile);
186c406b 2415
17450429 2416 if (bp_objfile_data->exception_msym == NULL)
186c406b 2417 {
17450429 2418 struct minimal_symbol *debug_hook;
186c406b 2419
17450429
PP
2420 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2421 if (debug_hook == NULL)
2422 {
2423 bp_objfile_data->exception_msym = &msym_not_found;
2424 continue;
2425 }
2426
2427 bp_objfile_data->exception_msym = debug_hook;
186c406b 2428 }
17450429
PP
2429
2430 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2431 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2432 &current_target);
06edf0c0
PA
2433 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2434 &internal_breakpoint_ops);
17450429
PP
2435 b->addr_string = xstrdup (func_name);
2436 b->enable_state = bp_disabled;
186c406b
TT
2437 }
2438
2439 update_global_location_list (1);
2440}
2441
c906108c 2442void
fba45db2 2443update_breakpoints_after_exec (void)
c906108c 2444{
35df4500 2445 struct breakpoint *b, *b_tmp;
876fa593 2446 struct bp_location *bploc, **bplocp_tmp;
c906108c 2447
25b22b0a
PA
2448 /* We're about to delete breakpoints from GDB's lists. If the
2449 INSERTED flag is true, GDB will try to lift the breakpoints by
2450 writing the breakpoints' "shadow contents" back into memory. The
2451 "shadow contents" are NOT valid after an exec, so GDB should not
2452 do that. Instead, the target is responsible from marking
2453 breakpoints out as soon as it detects an exec. We don't do that
2454 here instead, because there may be other attempts to delete
2455 breakpoints after detecting an exec and before reaching here. */
876fa593 2456 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2457 if (bploc->pspace == current_program_space)
2458 gdb_assert (!bploc->inserted);
c906108c 2459
35df4500 2460 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2461 {
6c95b8df
PA
2462 if (b->pspace != current_program_space)
2463 continue;
2464
4a64f543 2465 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2466 if (b->type == bp_shlib_event)
2467 {
2468 delete_breakpoint (b);
2469 continue;
2470 }
c906108c 2471
4a64f543 2472 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2473 if (b->type == bp_jit_event)
2474 {
2475 delete_breakpoint (b);
2476 continue;
2477 }
2478
1900040c 2479 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2480 as must overlay event and longjmp master breakpoints. */
2481 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2482 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2483 || b->type == bp_exception_master)
c4093a6a
JM
2484 {
2485 delete_breakpoint (b);
2486 continue;
2487 }
2488
4a64f543 2489 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 2490 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
2491 {
2492 delete_breakpoint (b);
2493 continue;
2494 }
2495
611c83ae
PA
2496 /* Longjmp and longjmp-resume breakpoints are also meaningless
2497 after an exec. */
186c406b
TT
2498 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2499 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2500 {
2501 delete_breakpoint (b);
2502 continue;
2503 }
2504
ce78b96d
JB
2505 if (b->type == bp_catchpoint)
2506 {
2507 /* For now, none of the bp_catchpoint breakpoints need to
2508 do anything at this point. In the future, if some of
2509 the catchpoints need to something, we will need to add
2510 a new method, and call this method from here. */
2511 continue;
2512 }
2513
c5aa993b
JM
2514 /* bp_finish is a special case. The only way we ought to be able
2515 to see one of these when an exec() has happened, is if the user
2516 caught a vfork, and then said "finish". Ordinarily a finish just
2517 carries them to the call-site of the current callee, by setting
2518 a temporary bp there and resuming. But in this case, the finish
2519 will carry them entirely through the vfork & exec.
2520
2521 We don't want to allow a bp_finish to remain inserted now. But
2522 we can't safely delete it, 'cause finish_command has a handle to
2523 the bp on a bpstat, and will later want to delete it. There's a
2524 chance (and I've seen it happen) that if we delete the bp_finish
2525 here, that its storage will get reused by the time finish_command
2526 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2527 We really must allow finish_command to delete a bp_finish.
2528
e5dd4106 2529 In the absence of a general solution for the "how do we know
53a5351d
JM
2530 it's safe to delete something others may have handles to?"
2531 problem, what we'll do here is just uninsert the bp_finish, and
2532 let finish_command delete it.
2533
2534 (We know the bp_finish is "doomed" in the sense that it's
2535 momentary, and will be deleted as soon as finish_command sees
2536 the inferior stopped. So it doesn't matter that the bp's
2537 address is probably bogus in the new a.out, unlike e.g., the
2538 solib breakpoints.) */
c5aa993b 2539
c5aa993b
JM
2540 if (b->type == bp_finish)
2541 {
2542 continue;
2543 }
2544
2545 /* Without a symbolic address, we have little hope of the
2546 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2547 a.out. */
c5aa993b
JM
2548 if (b->addr_string == NULL)
2549 {
2550 delete_breakpoint (b);
2551 continue;
2552 }
c5aa993b 2553 }
1900040c 2554 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2555 create_overlay_event_breakpoint ();
2556 create_longjmp_master_breakpoint ();
2557 create_std_terminate_master_breakpoint ();
186c406b 2558 create_exception_master_breakpoint ();
c906108c
SS
2559}
2560
2561int
fba45db2 2562detach_breakpoints (int pid)
c906108c 2563{
35df4500 2564 struct bp_location *bl, **blp_tmp;
3a1bae8e 2565 int val = 0;
ce696e05 2566 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2567 struct inferior *inf = current_inferior ();
c5aa993b 2568
39f77062 2569 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2570 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2571
6c95b8df 2572 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2573 inferior_ptid = pid_to_ptid (pid);
35df4500 2574 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2575 {
35df4500 2576 if (bl->pspace != inf->pspace)
6c95b8df
PA
2577 continue;
2578
35df4500
TJB
2579 if (bl->inserted)
2580 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2581 }
d03285ec
UW
2582
2583 /* Detach single-step breakpoints as well. */
2584 detach_single_step_breakpoints ();
2585
ce696e05 2586 do_cleanups (old_chain);
3a1bae8e 2587 return val;
c906108c
SS
2588}
2589
35df4500 2590/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2591 Note that this is used to detach breakpoints from a child fork.
2592 When we get here, the child isn't in the inferior list, and neither
2593 do we have objects to represent its address space --- we should
35df4500 2594 *not* look at bl->pspace->aspace here. */
6c95b8df 2595
c906108c 2596static int
35df4500 2597remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2598{
2599 int val;
c5aa993b 2600
35df4500
TJB
2601 /* BL is never in moribund_locations by our callers. */
2602 gdb_assert (bl->owner != NULL);
2bdf28a0 2603
35df4500 2604 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2605 /* Permanent breakpoints cannot be inserted or removed. */
2606 return 0;
2607
74960c60
VP
2608 /* The type of none suggests that owner is actually deleted.
2609 This should not ever happen. */
35df4500 2610 gdb_assert (bl->owner->type != bp_none);
0bde7532 2611
35df4500
TJB
2612 if (bl->loc_type == bp_loc_software_breakpoint
2613 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2614 {
c02f5703
MS
2615 /* "Normal" instruction breakpoint: either the standard
2616 trap-instruction bp (bp_breakpoint), or a
2617 bp_hardware_breakpoint. */
2618
2619 /* First check to see if we have to handle an overlay. */
2620 if (overlay_debugging == ovly_off
35df4500
TJB
2621 || bl->section == NULL
2622 || !(section_is_overlay (bl->section)))
c02f5703
MS
2623 {
2624 /* No overlay handling: just remove the breakpoint. */
348d480f 2625 val = bl->owner->ops->remove_location (bl);
c02f5703 2626 }
c906108c
SS
2627 else
2628 {
4a64f543 2629 /* This breakpoint is in an overlay section.
c02f5703
MS
2630 Did we set a breakpoint at the LMA? */
2631 if (!overlay_events_enabled)
2632 {
2633 /* Yes -- overlay event support is not active, so we
2634 should have set a breakpoint at the LMA. Remove it.
2635 */
c02f5703
MS
2636 /* Ignore any failures: if the LMA is in ROM, we will
2637 have already warned when we failed to insert it. */
35df4500
TJB
2638 if (bl->loc_type == bp_loc_hardware_breakpoint)
2639 target_remove_hw_breakpoint (bl->gdbarch,
2640 &bl->overlay_target_info);
c02f5703 2641 else
35df4500
TJB
2642 target_remove_breakpoint (bl->gdbarch,
2643 &bl->overlay_target_info);
c02f5703
MS
2644 }
2645 /* Did we set a breakpoint at the VMA?
2646 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2647 if (bl->inserted)
c906108c 2648 {
c02f5703
MS
2649 /* Yes -- remove it. Previously we did not bother to
2650 remove the breakpoint if the section had been
2651 unmapped, but let's not rely on that being safe. We
2652 don't know what the overlay manager might do. */
aa67235e
UW
2653
2654 /* However, we should remove *software* breakpoints only
2655 if the section is still mapped, or else we overwrite
2656 wrong code with the saved shadow contents. */
348d480f
PA
2657 if (bl->loc_type == bp_loc_hardware_breakpoint
2658 || section_is_mapped (bl->section))
2659 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
2660 else
2661 val = 0;
c906108c 2662 }
c02f5703
MS
2663 else
2664 {
2665 /* No -- not inserted, so no need to remove. No error. */
2666 val = 0;
2667 }
c906108c 2668 }
879d1e6b
UW
2669
2670 /* In some cases, we might not be able to remove a breakpoint
2671 in a shared library that has already been removed, but we
2672 have not yet processed the shlib unload event. */
35df4500 2673 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2674 val = 0;
2675
c906108c
SS
2676 if (val)
2677 return val;
35df4500 2678 bl->inserted = (is == mark_inserted);
c906108c 2679 }
35df4500 2680 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2681 {
77b06cd7
TJB
2682 gdb_assert (bl->owner->ops != NULL
2683 && bl->owner->ops->remove_location != NULL);
2684
35df4500 2685 bl->inserted = (is == mark_inserted);
77b06cd7 2686 bl->owner->ops->remove_location (bl);
2e70b7b9 2687
c906108c 2688 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2689 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2690 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2691 bl->owner->number);
c906108c 2692 }
35df4500
TJB
2693 else if (bl->owner->type == bp_catchpoint
2694 && breakpoint_enabled (bl->owner)
2695 && !bl->duplicate)
ce78b96d 2696 {
77b06cd7
TJB
2697 gdb_assert (bl->owner->ops != NULL
2698 && bl->owner->ops->remove_location != NULL);
ce78b96d 2699
77b06cd7 2700 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2701 if (val)
2702 return val;
77b06cd7 2703
35df4500 2704 bl->inserted = (is == mark_inserted);
ce78b96d 2705 }
c906108c
SS
2706
2707 return 0;
2708}
2709
6c95b8df 2710static int
35df4500 2711remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2712{
2713 int ret;
2714 struct cleanup *old_chain;
2715
35df4500
TJB
2716 /* BL is never in moribund_locations by our callers. */
2717 gdb_assert (bl->owner != NULL);
2bdf28a0 2718
35df4500 2719 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2720 /* Permanent breakpoints cannot be inserted or removed. */
2721 return 0;
2722
2723 /* The type of none suggests that owner is actually deleted.
2724 This should not ever happen. */
35df4500 2725 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2726
2727 old_chain = save_current_space_and_thread ();
2728
35df4500 2729 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2730
35df4500 2731 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2732
2733 do_cleanups (old_chain);
2734 return ret;
2735}
2736
c906108c
SS
2737/* Clear the "inserted" flag in all breakpoints. */
2738
25b22b0a 2739void
fba45db2 2740mark_breakpoints_out (void)
c906108c 2741{
35df4500 2742 struct bp_location *bl, **blp_tmp;
c906108c 2743
35df4500
TJB
2744 ALL_BP_LOCATIONS (bl, blp_tmp)
2745 if (bl->pspace == current_program_space)
2746 bl->inserted = 0;
c906108c
SS
2747}
2748
53a5351d
JM
2749/* Clear the "inserted" flag in all breakpoints and delete any
2750 breakpoints which should go away between runs of the program.
c906108c
SS
2751
2752 Plus other such housekeeping that has to be done for breakpoints
2753 between runs.
2754
53a5351d
JM
2755 Note: this function gets called at the end of a run (by
2756 generic_mourn_inferior) and when a run begins (by
4a64f543 2757 init_wait_for_inferior). */
c906108c
SS
2758
2759
2760
2761void
fba45db2 2762breakpoint_init_inferior (enum inf_context context)
c906108c 2763{
35df4500
TJB
2764 struct breakpoint *b, *b_tmp;
2765 struct bp_location *bl, **blp_tmp;
1c5cfe86 2766 int ix;
6c95b8df 2767 struct program_space *pspace = current_program_space;
c906108c 2768
50c71eaf
PA
2769 /* If breakpoint locations are shared across processes, then there's
2770 nothing to do. */
2567c7d9 2771 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2772 return;
2773
35df4500 2774 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2775 {
35df4500
TJB
2776 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2777 if (bl->pspace == pspace
2778 && bl->owner->enable_state != bp_permanent)
2779 bl->inserted = 0;
6c95b8df 2780 }
075f6582 2781
35df4500 2782 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2783 {
6c95b8df
PA
2784 if (b->loc && b->loc->pspace != pspace)
2785 continue;
2786
c5aa993b
JM
2787 switch (b->type)
2788 {
2789 case bp_call_dummy:
c906108c 2790
c5aa993b 2791 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2792 cause problems when the inferior is rerun, so we better get
2793 rid of it. */
2794
2795 case bp_watchpoint_scope:
2796
2797 /* Also get rid of scope breakpoints. */
2798
2799 case bp_shlib_event:
2800
2801 /* Also remove solib event breakpoints. Their addresses may
2802 have changed since the last time we ran the program.
2803 Actually we may now be debugging against different target;
2804 and so the solib backend that installed this breakpoint may
2805 not be used in by the target. E.g.,
2806
2807 (gdb) file prog-linux
2808 (gdb) run # native linux target
2809 ...
2810 (gdb) kill
2811 (gdb) file prog-win.exe
2812 (gdb) tar rem :9999 # remote Windows gdbserver.
2813 */
c906108c 2814
c5aa993b
JM
2815 delete_breakpoint (b);
2816 break;
c906108c 2817
c5aa993b
JM
2818 case bp_watchpoint:
2819 case bp_hardware_watchpoint:
2820 case bp_read_watchpoint:
2821 case bp_access_watchpoint:
3a5c3e22
PA
2822 {
2823 struct watchpoint *w = (struct watchpoint *) b;
c906108c 2824
3a5c3e22
PA
2825 /* Likewise for watchpoints on local expressions. */
2826 if (w->exp_valid_block != NULL)
2827 delete_breakpoint (b);
2828 else if (context == inf_starting)
2829 {
2830 /* Reset val field to force reread of starting value in
2831 insert_breakpoints. */
2832 if (w->val)
2833 value_free (w->val);
2834 w->val = NULL;
2835 w->val_valid = 0;
c860120c 2836 }
3a5c3e22 2837 }
c5aa993b
JM
2838 break;
2839 default:
c5aa993b
JM
2840 break;
2841 }
2842 }
1c5cfe86
PA
2843
2844 /* Get rid of the moribund locations. */
35df4500
TJB
2845 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2846 decref_bp_location (&bl);
1c5cfe86 2847 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2848}
2849
6c95b8df
PA
2850/* These functions concern about actual breakpoints inserted in the
2851 target --- to e.g. check if we need to do decr_pc adjustment or if
2852 we need to hop over the bkpt --- so we check for address space
2853 match, not program space. */
2854
c2c6d25f
JM
2855/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2856 exists at PC. It returns ordinary_breakpoint_here if it's an
2857 ordinary breakpoint, or permanent_breakpoint_here if it's a
2858 permanent breakpoint.
2859 - When continuing from a location with an ordinary breakpoint, we
2860 actually single step once before calling insert_breakpoints.
e5dd4106 2861 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
2862 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2863 the target, to advance the PC past the breakpoint. */
c906108c 2864
c2c6d25f 2865enum breakpoint_here
6c95b8df 2866breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2867{
35df4500 2868 struct bp_location *bl, **blp_tmp;
c2c6d25f 2869 int any_breakpoint_here = 0;
c906108c 2870
35df4500 2871 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2872 {
35df4500
TJB
2873 if (bl->loc_type != bp_loc_software_breakpoint
2874 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2875 continue;
2876
f1310107 2877 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2878 if ((breakpoint_enabled (bl->owner)
2879 || bl->owner->enable_state == bp_permanent)
f1310107 2880 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2881 {
2882 if (overlay_debugging
35df4500
TJB
2883 && section_is_overlay (bl->section)
2884 && !section_is_mapped (bl->section))
075f6582 2885 continue; /* unmapped overlay -- can't be a match */
35df4500 2886 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2887 return permanent_breakpoint_here;
2888 else
2889 any_breakpoint_here = 1;
2890 }
2891 }
c906108c 2892
c2c6d25f 2893 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2894}
2895
1c5cfe86
PA
2896/* Return true if there's a moribund breakpoint at PC. */
2897
2898int
6c95b8df 2899moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2900{
2901 struct bp_location *loc;
2902 int ix;
2903
2904 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2905 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2906 return 1;
2907
2908 return 0;
2909}
c2c6d25f 2910
c36b740a 2911/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2912 inserted using regular breakpoint_chain / bp_location array
2913 mechanism. This does not check for single-step breakpoints, which
2914 are inserted and removed using direct target manipulation. */
c906108c
SS
2915
2916int
4a64f543
MS
2917regular_breakpoint_inserted_here_p (struct address_space *aspace,
2918 CORE_ADDR pc)
c906108c 2919{
35df4500 2920 struct bp_location *bl, **blp_tmp;
c906108c 2921
35df4500 2922 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2923 {
35df4500
TJB
2924 if (bl->loc_type != bp_loc_software_breakpoint
2925 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2926 continue;
2927
35df4500 2928 if (bl->inserted
f1310107 2929 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2930 {
2931 if (overlay_debugging
35df4500
TJB
2932 && section_is_overlay (bl->section)
2933 && !section_is_mapped (bl->section))
075f6582
DJ
2934 continue; /* unmapped overlay -- can't be a match */
2935 else
2936 return 1;
2937 }
c5aa993b 2938 }
c36b740a
VP
2939 return 0;
2940}
2941
2942/* Returns non-zero iff there's either regular breakpoint
2943 or a single step breakpoint inserted at PC. */
2944
2945int
6c95b8df 2946breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2947{
6c95b8df 2948 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2949 return 1;
c906108c 2950
6c95b8df 2951 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2952 return 1;
2953
c906108c
SS
2954 return 0;
2955}
2956
4fa8626c
DJ
2957/* This function returns non-zero iff there is a software breakpoint
2958 inserted at PC. */
2959
2960int
3e43a32a
MS
2961software_breakpoint_inserted_here_p (struct address_space *aspace,
2962 CORE_ADDR pc)
4fa8626c 2963{
35df4500 2964 struct bp_location *bl, **blp_tmp;
4fa8626c 2965
35df4500 2966 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2967 {
35df4500 2968 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2969 continue;
2970
35df4500
TJB
2971 if (bl->inserted
2972 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2973 aspace, pc))
4fa8626c
DJ
2974 {
2975 if (overlay_debugging
35df4500
TJB
2976 && section_is_overlay (bl->section)
2977 && !section_is_mapped (bl->section))
4fa8626c
DJ
2978 continue; /* unmapped overlay -- can't be a match */
2979 else
2980 return 1;
2981 }
2982 }
2983
1aafd4da 2984 /* Also check for software single-step breakpoints. */
6c95b8df 2985 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2986 return 1;
2987
4fa8626c
DJ
2988 return 0;
2989}
2990
9093389c
PA
2991int
2992hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2993 CORE_ADDR addr, ULONGEST len)
2994{
2995 struct breakpoint *bpt;
2996
2997 ALL_BREAKPOINTS (bpt)
2998 {
2999 struct bp_location *loc;
3000
3001 if (bpt->type != bp_hardware_watchpoint
3002 && bpt->type != bp_access_watchpoint)
3003 continue;
3004
3005 if (!breakpoint_enabled (bpt))
3006 continue;
3007
3008 for (loc = bpt->loc; loc; loc = loc->next)
3009 if (loc->pspace->aspace == aspace && loc->inserted)
3010 {
3011 CORE_ADDR l, h;
3012
3013 /* Check for intersection. */
3014 l = max (loc->address, addr);
3015 h = min (loc->address + loc->length, addr + len);
3016 if (l < h)
3017 return 1;
3018 }
3019 }
3020 return 0;
3021}
3022
075f6582
DJ
3023/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3024 PC is valid for process/thread PTID. */
c906108c
SS
3025
3026int
6c95b8df
PA
3027breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3028 ptid_t ptid)
c906108c 3029{
35df4500 3030 struct bp_location *bl, **blp_tmp;
4a306c9a 3031 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3032 int thread = -1;
4a306c9a 3033 int task = 0;
a6f1cd96 3034
35df4500 3035 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3036 {
35df4500
TJB
3037 if (bl->loc_type != bp_loc_software_breakpoint
3038 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3039 continue;
3040
35df4500
TJB
3041 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3042 if (!breakpoint_enabled (bl->owner)
3043 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3044 continue;
3045
f1310107 3046 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3047 continue;
3048
35df4500 3049 if (bl->owner->thread != -1)
075f6582 3050 {
a6f1cd96
JB
3051 /* This is a thread-specific breakpoint. Check that ptid
3052 matches that thread. If thread hasn't been computed yet,
3053 it is now time to do so. */
3054 if (thread == -1)
3055 thread = pid_to_thread_id (ptid);
35df4500 3056 if (bl->owner->thread != thread)
a6f1cd96 3057 continue;
075f6582 3058 }
a6f1cd96 3059
35df4500 3060 if (bl->owner->task != 0)
4a306c9a
JB
3061 {
3062 /* This is a task-specific breakpoint. Check that ptid
3063 matches that task. If task hasn't been computed yet,
3064 it is now time to do so. */
3065 if (task == 0)
3066 task = ada_get_task_number (ptid);
35df4500 3067 if (bl->owner->task != task)
4a306c9a
JB
3068 continue;
3069 }
3070
a6f1cd96 3071 if (overlay_debugging
35df4500
TJB
3072 && section_is_overlay (bl->section)
3073 && !section_is_mapped (bl->section))
a6f1cd96
JB
3074 continue; /* unmapped overlay -- can't be a match */
3075
3076 return 1;
c5aa993b 3077 }
c906108c
SS
3078
3079 return 0;
3080}
c906108c 3081\f
c5aa993b 3082
c906108c
SS
3083/* bpstat stuff. External routines' interfaces are documented
3084 in breakpoint.h. */
3085
3086int
fba45db2 3087ep_is_catchpoint (struct breakpoint *ep)
c906108c 3088{
533be4dd 3089 return (ep->type == bp_catchpoint);
c906108c
SS
3090}
3091
f431efe5
PA
3092/* Frees any storage that is part of a bpstat. Does not walk the
3093 'next' chain. */
3094
3095static void
198757a8
VP
3096bpstat_free (bpstat bs)
3097{
3098 if (bs->old_val != NULL)
3099 value_free (bs->old_val);
9add0f1b 3100 decref_counted_command_line (&bs->commands);
f431efe5 3101 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3102 xfree (bs);
3103}
3104
c906108c
SS
3105/* Clear a bpstat so that it says we are not at any breakpoint.
3106 Also free any storage that is part of a bpstat. */
3107
3108void
fba45db2 3109bpstat_clear (bpstat *bsp)
c906108c
SS
3110{
3111 bpstat p;
3112 bpstat q;
3113
3114 if (bsp == 0)
3115 return;
3116 p = *bsp;
3117 while (p != NULL)
3118 {
3119 q = p->next;
198757a8 3120 bpstat_free (p);
c906108c
SS
3121 p = q;
3122 }
3123 *bsp = NULL;
3124}
3125
3126/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3127 is part of the bpstat is copied as well. */
3128
3129bpstat
fba45db2 3130bpstat_copy (bpstat bs)
c906108c
SS
3131{
3132 bpstat p = NULL;
3133 bpstat tmp;
3134 bpstat retval = NULL;
3135
3136 if (bs == NULL)
3137 return bs;
3138
3139 for (; bs != NULL; bs = bs->next)
3140 {
3141 tmp = (bpstat) xmalloc (sizeof (*tmp));
3142 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3143 incref_counted_command_line (tmp->commands);
f431efe5 3144 incref_bp_location (tmp->bp_location_at);
31cc81e9 3145 if (bs->old_val != NULL)
3c3185ac
JK
3146 {
3147 tmp->old_val = value_copy (bs->old_val);
3148 release_value (tmp->old_val);
3149 }
31cc81e9 3150
c906108c
SS
3151 if (p == NULL)
3152 /* This is the first thing in the chain. */
3153 retval = tmp;
3154 else
3155 p->next = tmp;
3156 p = tmp;
3157 }
3158 p->next = NULL;
3159 return retval;
3160}
3161
4a64f543 3162/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3163
3164bpstat
fba45db2 3165bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3166{
c5aa993b
JM
3167 if (bsp == NULL)
3168 return NULL;
c906108c 3169
c5aa993b
JM
3170 for (; bsp != NULL; bsp = bsp->next)
3171 {
f431efe5 3172 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3173 return bsp;
3174 }
c906108c
SS
3175 return NULL;
3176}
3177
4a64f543
MS
3178/* Put in *NUM the breakpoint number of the first breakpoint we are
3179 stopped at. *BSP upon return is a bpstat which points to the
3180 remaining breakpoints stopped at (but which is not guaranteed to be
3181 good for anything but further calls to bpstat_num).
3182
8671a17b
PA
3183 Return 0 if passed a bpstat which does not indicate any breakpoints.
3184 Return -1 if stopped at a breakpoint that has been deleted since
3185 we set it.
3186 Return 1 otherwise. */
c906108c
SS
3187
3188int
8671a17b 3189bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3190{
3191 struct breakpoint *b;
3192
3193 if ((*bsp) == NULL)
3194 return 0; /* No more breakpoint values */
8671a17b 3195
4a64f543
MS
3196 /* We assume we'll never have several bpstats that correspond to a
3197 single breakpoint -- otherwise, this function might return the
3198 same number more than once and this will look ugly. */
f431efe5 3199 b = (*bsp)->breakpoint_at;
8671a17b
PA
3200 *bsp = (*bsp)->next;
3201 if (b == NULL)
3202 return -1; /* breakpoint that's been deleted since */
3203
3204 *num = b->number; /* We have its number */
3205 return 1;
c906108c
SS
3206}
3207
e93ca019 3208/* See breakpoint.h. */
c906108c
SS
3209
3210void
e93ca019 3211bpstat_clear_actions (void)
c906108c 3212{
e93ca019
JK
3213 struct thread_info *tp;
3214 bpstat bs;
3215
3216 if (ptid_equal (inferior_ptid, null_ptid))
3217 return;
3218
3219 tp = find_thread_ptid (inferior_ptid);
3220 if (tp == NULL)
3221 return;
3222
3223 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 3224 {
9add0f1b 3225 decref_counted_command_line (&bs->commands);
abf85f46 3226
c906108c
SS
3227 if (bs->old_val != NULL)
3228 {
3229 value_free (bs->old_val);
3230 bs->old_val = NULL;
3231 }
3232 }
3233}
3234
f3b1572e
PA
3235/* Called when a command is about to proceed the inferior. */
3236
3237static void
3238breakpoint_about_to_proceed (void)
3239{
3240 if (!ptid_equal (inferior_ptid, null_ptid))
3241 {
3242 struct thread_info *tp = inferior_thread ();
3243
3244 /* Allow inferior function calls in breakpoint commands to not
3245 interrupt the command list. When the call finishes
3246 successfully, the inferior will be standing at the same
3247 breakpoint as if nothing happened. */
16c381f0 3248 if (tp->control.in_infcall)
f3b1572e
PA
3249 return;
3250 }
3251
3252 breakpoint_proceeded = 1;
3253}
3254
4a64f543
MS
3255/* Stub for cleaning up our state if we error-out of a breakpoint
3256 command. */
c906108c 3257static void
4efb68b1 3258cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3259{
3260 executing_breakpoint_commands = 0;
3261}
3262
abf85f46
JK
3263/* Return non-zero iff CMD as the first line of a command sequence is `silent'
3264 or its equivalent. */
3265
3266static int
3267command_line_is_silent (struct command_line *cmd)
3268{
3269 return cmd && (strcmp ("silent", cmd->line) == 0
3270 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3271}
3272
4a64f543
MS
3273/* Execute all the commands associated with all the breakpoints at
3274 this location. Any of these commands could cause the process to
3275 proceed beyond this point, etc. We look out for such changes by
3276 checking the global "breakpoint_proceeded" after each command.
c906108c 3277
347bddb7
PA
3278 Returns true if a breakpoint command resumed the inferior. In that
3279 case, it is the caller's responsibility to recall it again with the
3280 bpstat of the current thread. */
3281
3282static int
3283bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3284{
3285 bpstat bs;
3286 struct cleanup *old_chain;
347bddb7 3287 int again = 0;
c906108c
SS
3288
3289 /* Avoid endless recursion if a `source' command is contained
3290 in bs->commands. */
3291 if (executing_breakpoint_commands)
347bddb7 3292 return 0;
c906108c
SS
3293
3294 executing_breakpoint_commands = 1;
3295 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3296
cf6c5ffb
TT
3297 prevent_dont_repeat ();
3298
4a64f543 3299 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3300 bs = *bsp;
3301
3302 breakpoint_proceeded = 0;
3303 for (; bs != NULL; bs = bs->next)
3304 {
9add0f1b 3305 struct counted_command_line *ccmd;
6c50ab1c
JB
3306 struct command_line *cmd;
3307 struct cleanup *this_cmd_tree_chain;
3308
3309 /* Take ownership of the BSP's command tree, if it has one.
3310
3311 The command tree could legitimately contain commands like
3312 'step' and 'next', which call clear_proceed_status, which
3313 frees stop_bpstat's command tree. To make sure this doesn't
3314 free the tree we're executing out from under us, we need to
3315 take ownership of the tree ourselves. Since a given bpstat's
3316 commands are only executed once, we don't need to copy it; we
3317 can clear the pointer in the bpstat, and make sure we free
3318 the tree when we're done. */
9add0f1b
TT
3319 ccmd = bs->commands;
3320 bs->commands = NULL;
abf85f46
JK
3321 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
3322 cmd = ccmd ? ccmd->commands : NULL;
3323 if (command_line_is_silent (cmd))
3324 {
3325 /* The action has been already done by bpstat_stop_status. */
3326 cmd = cmd->next;
3327 }
6c50ab1c 3328
c906108c
SS
3329 while (cmd != NULL)
3330 {
3331 execute_control_command (cmd);
3332
3333 if (breakpoint_proceeded)
3334 break;
3335 else
3336 cmd = cmd->next;
3337 }
6c50ab1c
JB
3338
3339 /* We can free this command tree now. */
3340 do_cleanups (this_cmd_tree_chain);
3341
c906108c 3342 if (breakpoint_proceeded)
32c1e744
VP
3343 {
3344 if (target_can_async_p ())
347bddb7
PA
3345 /* If we are in async mode, then the target might be still
3346 running, not stopped at any breakpoint, so nothing for
3347 us to do here -- just return to the event loop. */
3348 ;
32c1e744
VP
3349 else
3350 /* In sync mode, when execute_control_command returns
3351 we're already standing on the next breakpoint.
347bddb7
PA
3352 Breakpoint commands for that stop were not run, since
3353 execute_command does not run breakpoint commands --
3354 only command_line_handler does, but that one is not
3355 involved in execution of breakpoint commands. So, we
3356 can now execute breakpoint commands. It should be
3357 noted that making execute_command do bpstat actions is
3358 not an option -- in this case we'll have recursive
3359 invocation of bpstat for each breakpoint with a
3360 command, and can easily blow up GDB stack. Instead, we
3361 return true, which will trigger the caller to recall us
3362 with the new stop_bpstat. */
3363 again = 1;
3364 break;
32c1e744 3365 }
c906108c 3366 }
c2b8ed2c 3367 do_cleanups (old_chain);
347bddb7
PA
3368 return again;
3369}
3370
3371void
3372bpstat_do_actions (void)
3373{
353d1d73
JK
3374 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
3375
347bddb7
PA
3376 /* Do any commands attached to breakpoint we are stopped at. */
3377 while (!ptid_equal (inferior_ptid, null_ptid)
3378 && target_has_execution
3379 && !is_exited (inferior_ptid)
3380 && !is_executing (inferior_ptid))
3381 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3382 and only return when it is stopped at the next breakpoint, we
3383 keep doing breakpoint actions until it returns false to
3384 indicate the inferior was not resumed. */
16c381f0 3385 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3386 break;
353d1d73
JK
3387
3388 discard_cleanups (cleanup_if_error);
c906108c
SS
3389}
3390
fa4727a6
DJ
3391/* Print out the (old or new) value associated with a watchpoint. */
3392
3393static void
3394watchpoint_value_print (struct value *val, struct ui_file *stream)
3395{
3396 if (val == NULL)
3397 fprintf_unfiltered (stream, _("<unreadable>"));
3398 else
79a45b7d
TT
3399 {
3400 struct value_print_options opts;
3401 get_user_print_options (&opts);
3402 value_print (val, stream, &opts);
3403 }
fa4727a6
DJ
3404}
3405
e514a9d6 3406/* Generic routine for printing messages indicating why we
4a64f543 3407 stopped. The behavior of this function depends on the value
e514a9d6
JM
3408 'print_it' in the bpstat structure. Under some circumstances we
3409 may decide not to print anything here and delegate the task to
4a64f543 3410 normal_stop(). */
e514a9d6
JM
3411
3412static enum print_stop_action
3413print_bp_stop_message (bpstat bs)
3414{
3415 switch (bs->print_it)
3416 {
3417 case print_it_noop:
4a64f543 3418 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3419 return PRINT_UNKNOWN;
3420 break;
3421
3422 case print_it_done:
3423 /* We still want to print the frame, but we already printed the
4a64f543 3424 relevant messages. */
e514a9d6
JM
3425 return PRINT_SRC_AND_LOC;
3426 break;
3427
3428 case print_it_normal:
4f8d1dc6 3429 {
f431efe5
PA
3430 struct breakpoint *b = bs->breakpoint_at;
3431
1a6a67de
TJB
3432 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3433 which has since been deleted. */
3434 if (b == NULL)
3435 return PRINT_UNKNOWN;
3436
348d480f
PA
3437 /* Normal case. Call the breakpoint's print_it method. */
3438 return b->ops->print_it (bs);
4f8d1dc6 3439 }
348d480f 3440 break;
3086aeae 3441
e514a9d6 3442 default:
8e65ff28 3443 internal_error (__FILE__, __LINE__,
e2e0b3e5 3444 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3445 break;
c906108c 3446 }
c906108c
SS
3447}
3448
e514a9d6
JM
3449/* Print a message indicating what happened. This is called from
3450 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
3451 list - a list of the eventpoints that caused this stop. KIND is
3452 the target_waitkind for the stopping event. This
e514a9d6
JM
3453 routine calls the generic print routine for printing a message
3454 about reasons for stopping. This will print (for example) the
3455 "Breakpoint n," part of the output. The return value of this
3456 routine is one of:
c906108c 3457
4a64f543 3458 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3459 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3460 code to print the location. An example is
c5aa993b
JM
3461 "Breakpoint 1, " which should be followed by
3462 the location.
917317f4 3463 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3464 to also print the location part of the message.
3465 An example is the catch/throw messages, which
4a64f543 3466 don't require a location appended to the end.
917317f4 3467 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3468 further info to be printed. */
c906108c 3469
917317f4 3470enum print_stop_action
36dfb11c 3471bpstat_print (bpstat bs, int kind)
c906108c
SS
3472{
3473 int val;
c5aa993b 3474
c906108c 3475 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3476 (Currently all watchpoints go on the bpstat whether hit or not.
3477 That probably could (should) be changed, provided care is taken
c906108c 3478 with respect to bpstat_explains_signal). */
e514a9d6
JM
3479 for (; bs; bs = bs->next)
3480 {
3481 val = print_bp_stop_message (bs);
3482 if (val == PRINT_SRC_ONLY
3483 || val == PRINT_SRC_AND_LOC
3484 || val == PRINT_NOTHING)
3485 return val;
3486 }
c906108c 3487
36dfb11c
TT
3488 /* If we had hit a shared library event breakpoint,
3489 print_bp_stop_message would print out this message. If we hit an
3490 OS-level shared library event, do the same thing. */
3491 if (kind == TARGET_WAITKIND_LOADED)
3492 {
3493 ui_out_text (current_uiout, _("Stopped due to shared library event\n"));
3494 if (ui_out_is_mi_like_p (current_uiout))
3495 ui_out_field_string (current_uiout, "reason",
3496 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
3497 return PRINT_NOTHING;
3498 }
3499
e514a9d6 3500 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3501 with and nothing was printed. */
917317f4 3502 return PRINT_UNKNOWN;
c906108c
SS
3503}
3504
4a64f543
MS
3505/* Evaluate the expression EXP and return 1 if value is zero. This is
3506 used inside a catch_errors to evaluate the breakpoint condition.
3507 The argument is a "struct expression *" that has been cast to a
3508 "char *" to make it pass through catch_errors. */
c906108c
SS
3509
3510static int
4efb68b1 3511breakpoint_cond_eval (void *exp)
c906108c 3512{
278cd55f 3513 struct value *mark = value_mark ();
c5aa993b 3514 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3515
c906108c
SS
3516 value_free_to_mark (mark);
3517 return i;
3518}
3519
5760d0ab 3520/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3521
3522static bpstat
5760d0ab 3523bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3524{
3525 bpstat bs;
3526
3527 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3528 bs->next = NULL;
3529 **bs_link_pointer = bs;
3530 *bs_link_pointer = &bs->next;
f431efe5
PA
3531 bs->breakpoint_at = bl->owner;
3532 bs->bp_location_at = bl;
3533 incref_bp_location (bl);
c906108c
SS
3534 /* If the condition is false, etc., don't do the commands. */
3535 bs->commands = NULL;
3536 bs->old_val = NULL;
3537 bs->print_it = print_it_normal;
3538 return bs;
3539}
3540\f
d983da9c
DJ
3541/* The target has stopped with waitstatus WS. Check if any hardware
3542 watchpoints have triggered, according to the target. */
3543
3544int
3545watchpoints_triggered (struct target_waitstatus *ws)
3546{
d92524f1 3547 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3548 CORE_ADDR addr;
3549 struct breakpoint *b;
3550
3551 if (!stopped_by_watchpoint)
3552 {
3553 /* We were not stopped by a watchpoint. Mark all watchpoints
3554 as not triggered. */
3555 ALL_BREAKPOINTS (b)
cc60f2e3 3556 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3557 {
3558 struct watchpoint *w = (struct watchpoint *) b;
3559
3560 w->watchpoint_triggered = watch_triggered_no;
3561 }
d983da9c
DJ
3562
3563 return 0;
3564 }
3565
3566 if (!target_stopped_data_address (&current_target, &addr))
3567 {
3568 /* We were stopped by a watchpoint, but we don't know where.
3569 Mark all watchpoints as unknown. */
3570 ALL_BREAKPOINTS (b)
cc60f2e3 3571 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3572 {
3573 struct watchpoint *w = (struct watchpoint *) b;
3574
3575 w->watchpoint_triggered = watch_triggered_unknown;
3576 }
d983da9c
DJ
3577
3578 return stopped_by_watchpoint;
3579 }
3580
3581 /* The target could report the data address. Mark watchpoints
3582 affected by this data address as triggered, and all others as not
3583 triggered. */
3584
3585 ALL_BREAKPOINTS (b)
cc60f2e3 3586 if (is_hardware_watchpoint (b))
d983da9c 3587 {
3a5c3e22 3588 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 3589 struct bp_location *loc;
d983da9c 3590
3a5c3e22 3591 w->watchpoint_triggered = watch_triggered_no;
a5606eee 3592 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 3593 {
3a5c3e22 3594 if (is_masked_watchpoint (b))
9c06b0b4 3595 {
3a5c3e22
PA
3596 CORE_ADDR newaddr = addr & w->hw_wp_mask;
3597 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
3598
3599 if (newaddr == start)
3600 {
3a5c3e22 3601 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3602 break;
3603 }
3604 }
3605 /* Exact match not required. Within range is sufficient. */
3606 else if (target_watchpoint_addr_within_range (&current_target,
3607 addr, loc->address,
3608 loc->length))
3609 {
3a5c3e22 3610 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3611 break;
3612 }
3613 }
d983da9c
DJ
3614 }
3615
3616 return 1;
3617}
3618
c906108c
SS
3619/* Possible return values for watchpoint_check (this can't be an enum
3620 because of check_errors). */
3621/* The watchpoint has been deleted. */
3622#define WP_DELETED 1
3623/* The value has changed. */
3624#define WP_VALUE_CHANGED 2
3625/* The value has not changed. */
3626#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3627/* Ignore this watchpoint, no matter if the value changed or not. */
3628#define WP_IGNORE 4
c906108c
SS
3629
3630#define BP_TEMPFLAG 1
3631#define BP_HARDWAREFLAG 2
3632
4a64f543
MS
3633/* Evaluate watchpoint condition expression and check if its value
3634 changed.
553e4c11
JB
3635
3636 P should be a pointer to struct bpstat, but is defined as a void *
3637 in order for this function to be usable with catch_errors. */
c906108c
SS
3638
3639static int
4efb68b1 3640watchpoint_check (void *p)
c906108c
SS
3641{
3642 bpstat bs = (bpstat) p;
3a5c3e22 3643 struct watchpoint *b;
c906108c
SS
3644 struct frame_info *fr;
3645 int within_current_scope;
3646
f431efe5 3647 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3648 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 3649 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 3650
f6bc2008
PA
3651 /* If this is a local watchpoint, we only want to check if the
3652 watchpoint frame is in scope if the current thread is the thread
3653 that was used to create the watchpoint. */
3654 if (!watchpoint_in_thread_scope (b))
60e1c644 3655 return WP_IGNORE;
f6bc2008 3656
c906108c
SS
3657 if (b->exp_valid_block == NULL)
3658 within_current_scope = 1;
3659 else
3660 {
edb3359d
DJ
3661 struct frame_info *frame = get_current_frame ();
3662 struct gdbarch *frame_arch = get_frame_arch (frame);
3663 CORE_ADDR frame_pc = get_frame_pc (frame);
3664
4a64f543
MS
3665 /* in_function_epilogue_p() returns a non-zero value if we're
3666 still in the function but the stack frame has already been
3667 invalidated. Since we can't rely on the values of local
3668 variables after the stack has been destroyed, we are treating
3669 the watchpoint in that state as `not changed' without further
3670 checking. Don't mark watchpoints as changed if the current
3671 frame is in an epilogue - even if they are in some other
3672 frame, our view of the stack is likely to be wrong and
3673 frame_find_by_id could error out. */
a0f49112 3674 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3675 return WP_IGNORE;
a0f49112 3676
101dcfbe 3677 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3678 within_current_scope = (fr != NULL);
69fbadd5
DJ
3679
3680 /* If we've gotten confused in the unwinder, we might have
3681 returned a frame that can't describe this variable. */
edb3359d
DJ
3682 if (within_current_scope)
3683 {
3684 struct symbol *function;
3685
3686 function = get_frame_function (fr);
3687 if (function == NULL
3688 || !contained_in (b->exp_valid_block,
3689 SYMBOL_BLOCK_VALUE (function)))
3690 within_current_scope = 0;
3691 }
69fbadd5 3692
edb3359d 3693 if (within_current_scope)
c906108c
SS
3694 /* If we end up stopping, the current frame will get selected
3695 in normal_stop. So this call to select_frame won't affect
3696 the user. */
0f7d239c 3697 select_frame (fr);
c906108c 3698 }
c5aa993b 3699
c906108c
SS
3700 if (within_current_scope)
3701 {
4a64f543
MS
3702 /* We use value_{,free_to_}mark because it could be a *long*
3703 time before we return to the command level and call
3704 free_all_values. We can't call free_all_values because we
3705 might be in the middle of evaluating a function call. */
c906108c 3706
0cf6dd15 3707 int pc = 0;
9c06b0b4 3708 struct value *mark;
fa4727a6
DJ
3709 struct value *new_val;
3710
3a5c3e22 3711 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
3712 /* Since we don't know the exact trigger address (from
3713 stopped_data_address), just tell the user we've triggered
3714 a mask watchpoint. */
3715 return WP_VALUE_CHANGED;
3716
3717 mark = value_mark ();
0cf6dd15 3718 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3719
4a64f543
MS
3720 /* We use value_equal_contents instead of value_equal because
3721 the latter coerces an array to a pointer, thus comparing just
3722 the address of the array instead of its contents. This is
3723 not what we want. */
fa4727a6 3724 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3725 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3726 {
fa4727a6
DJ
3727 if (new_val != NULL)
3728 {
3729 release_value (new_val);
3730 value_free_to_mark (mark);
3731 }
c906108c
SS
3732 bs->old_val = b->val;
3733 b->val = new_val;
fa4727a6 3734 b->val_valid = 1;
c906108c
SS
3735 return WP_VALUE_CHANGED;
3736 }
3737 else
3738 {
60e1c644 3739 /* Nothing changed. */
c906108c 3740 value_free_to_mark (mark);
c906108c
SS
3741 return WP_VALUE_NOT_CHANGED;
3742 }
3743 }
3744 else
3745 {
79a45e25
PA
3746 struct ui_out *uiout = current_uiout;
3747
c906108c 3748 /* This seems like the only logical thing to do because
c5aa993b
JM
3749 if we temporarily ignored the watchpoint, then when
3750 we reenter the block in which it is valid it contains
3751 garbage (in the case of a function, it may have two
3752 garbage values, one before and one after the prologue).
3753 So we can't even detect the first assignment to it and
3754 watch after that (since the garbage may or may not equal
3755 the first value assigned). */
348d480f
PA
3756 /* We print all the stop information in
3757 breakpoint_ops->print_it, but in this case, by the time we
3758 call breakpoint_ops->print_it this bp will be deleted
3759 already. So we have no choice but print the information
3760 here. */
9dc5e2a9 3761 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3762 ui_out_field_string
3763 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3764 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 3765 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
3766 ui_out_text (uiout,
3767 " deleted because the program has left the block in\n\
8b93c638 3768which its expression is valid.\n");
4ce44c66 3769
cdac0397 3770 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 3771 decref_counted_command_line (&b->base.commands);
d0fb5eae 3772 watchpoint_del_at_next_stop (b);
c906108c
SS
3773
3774 return WP_DELETED;
3775 }
3776}
3777
18a18393 3778/* Return true if it looks like target has stopped due to hitting
348d480f
PA
3779 breakpoint location BL. This function does not check if we should
3780 stop, only if BL explains the stop. */
3781
18a18393 3782static int
6c95b8df
PA
3783bpstat_check_location (const struct bp_location *bl,
3784 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3785{
3786 struct breakpoint *b = bl->owner;
3787
348d480f 3788 /* BL is from an existing breakpoint. */
2bdf28a0
JK
3789 gdb_assert (b != NULL);
3790
348d480f 3791 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
18a18393
VP
3792}
3793
3a5c3e22
PA
3794/* Determine if the watched values have actually changed, and we
3795 should stop. If not, set BS->stop to 0. */
3796
18a18393
VP
3797static void
3798bpstat_check_watchpoint (bpstat bs)
3799{
2bdf28a0 3800 const struct bp_location *bl;
3a5c3e22 3801 struct watchpoint *b;
2bdf28a0
JK
3802
3803 /* BS is built for existing struct breakpoint. */
f431efe5 3804 bl = bs->bp_location_at;
2bdf28a0 3805 gdb_assert (bl != NULL);
3a5c3e22 3806 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 3807 gdb_assert (b != NULL);
18a18393 3808
18a18393 3809 {
18a18393
VP
3810 int must_check_value = 0;
3811
3a5c3e22 3812 if (b->base.type == bp_watchpoint)
18a18393
VP
3813 /* For a software watchpoint, we must always check the
3814 watched value. */
3815 must_check_value = 1;
3816 else if (b->watchpoint_triggered == watch_triggered_yes)
3817 /* We have a hardware watchpoint (read, write, or access)
3818 and the target earlier reported an address watched by
3819 this watchpoint. */
3820 must_check_value = 1;
3821 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 3822 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
3823 /* We were stopped by a hardware watchpoint, but the target could
3824 not report the data address. We must check the watchpoint's
3825 value. Access and read watchpoints are out of luck; without
3826 a data address, we can't figure it out. */
3827 must_check_value = 1;
3a5c3e22 3828
18a18393
VP
3829 if (must_check_value)
3830 {
3e43a32a
MS
3831 char *message
3832 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 3833 b->base.number);
18a18393
VP
3834 struct cleanup *cleanups = make_cleanup (xfree, message);
3835 int e = catch_errors (watchpoint_check, bs, message,
3836 RETURN_MASK_ALL);
3837 do_cleanups (cleanups);
3838 switch (e)
3839 {
3840 case WP_DELETED:
3841 /* We've already printed what needs to be printed. */
3842 bs->print_it = print_it_done;
3843 /* Stop. */
3844 break;
60e1c644
PA
3845 case WP_IGNORE:
3846 bs->print_it = print_it_noop;
3847 bs->stop = 0;
3848 break;
18a18393 3849 case WP_VALUE_CHANGED:
3a5c3e22 3850 if (b->base.type == bp_read_watchpoint)
18a18393 3851 {
85d721b8
PA
3852 /* There are two cases to consider here:
3853
4a64f543 3854 1. We're watching the triggered memory for reads.
85d721b8
PA
3855 In that case, trust the target, and always report
3856 the watchpoint hit to the user. Even though
3857 reads don't cause value changes, the value may
3858 have changed since the last time it was read, and
3859 since we're not trapping writes, we will not see
3860 those, and as such we should ignore our notion of
3861 old value.
3862
4a64f543 3863 2. We're watching the triggered memory for both
85d721b8
PA
3864 reads and writes. There are two ways this may
3865 happen:
3866
4a64f543 3867 2.1. This is a target that can't break on data
85d721b8
PA
3868 reads only, but can break on accesses (reads or
3869 writes), such as e.g., x86. We detect this case
3870 at the time we try to insert read watchpoints.
3871
4a64f543 3872 2.2. Otherwise, the target supports read
85d721b8
PA
3873 watchpoints, but, the user set an access or write
3874 watchpoint watching the same memory as this read
3875 watchpoint.
3876
3877 If we're watching memory writes as well as reads,
3878 ignore watchpoint hits when we find that the
3879 value hasn't changed, as reads don't cause
3880 changes. This still gives false positives when
3881 the program writes the same value to memory as
3882 what there was already in memory (we will confuse
3883 it for a read), but it's much better than
3884 nothing. */
3885
3886 int other_write_watchpoint = 0;
3887
3888 if (bl->watchpoint_type == hw_read)
3889 {
3890 struct breakpoint *other_b;
3891
3892 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
3893 if (other_b->type == bp_hardware_watchpoint
3894 || other_b->type == bp_access_watchpoint)
85d721b8 3895 {
3a5c3e22
PA
3896 struct watchpoint *other_w =
3897 (struct watchpoint *) other_b;
3898
3899 if (other_w->watchpoint_triggered
3900 == watch_triggered_yes)
3901 {
3902 other_write_watchpoint = 1;
3903 break;
3904 }
85d721b8
PA
3905 }
3906 }
3907
3908 if (other_write_watchpoint
3909 || bl->watchpoint_type == hw_access)
3910 {
3911 /* We're watching the same memory for writes,
3912 and the value changed since the last time we
3913 updated it, so this trap must be for a write.
3914 Ignore it. */
3915 bs->print_it = print_it_noop;
3916 bs->stop = 0;
3917 }
18a18393
VP
3918 }
3919 break;
3920 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
3921 if (b->base.type == bp_hardware_watchpoint
3922 || b->base.type == bp_watchpoint)
18a18393
VP
3923 {
3924 /* Don't stop: write watchpoints shouldn't fire if
3925 the value hasn't changed. */
3926 bs->print_it = print_it_noop;
3927 bs->stop = 0;
3928 }
3929 /* Stop. */
3930 break;
3931 default:
3932 /* Can't happen. */
3933 case 0:
3934 /* Error from catch_errors. */
3a5c3e22 3935 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 3936 watchpoint_del_at_next_stop (b);
18a18393
VP
3937 /* We've already printed what needs to be printed. */
3938 bs->print_it = print_it_done;
3939 break;
3940 }
3941 }
3942 else /* must_check_value == 0 */
3943 {
3944 /* This is a case where some watchpoint(s) triggered, but
3945 not at the address of this watchpoint, or else no
3946 watchpoint triggered after all. So don't print
3947 anything for this watchpoint. */
3948 bs->print_it = print_it_noop;
3949 bs->stop = 0;
3950 }
3951 }
3952}
3953
3954
3955/* Check conditions (condition proper, frame, thread and ignore count)
3956 of breakpoint referred to by BS. If we should not stop for this
3957 breakpoint, set BS->stop to 0. */
f431efe5 3958
18a18393
VP
3959static void
3960bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3961{
3962 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
3963 const struct bp_location *bl;
3964 struct breakpoint *b;
3965
3966 /* BS is built for existing struct breakpoint. */
f431efe5 3967 bl = bs->bp_location_at;
2bdf28a0 3968 gdb_assert (bl != NULL);
f431efe5 3969 b = bs->breakpoint_at;
2bdf28a0 3970 gdb_assert (b != NULL);
18a18393
VP
3971
3972 if (frame_id_p (b->frame_id)
edb3359d 3973 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
3974 bs->stop = 0;
3975 else if (bs->stop)
3976 {
3977 int value_is_zero = 0;
60e1c644
PA
3978 struct expression *cond;
3979
7371cf6d
PM
3980 /* Evaluate Python breakpoints that have a "stop"
3981 method implemented. */
3982 if (b->py_bp_object)
3983 bs->stop = gdbpy_should_stop (b->py_bp_object);
3984
60e1c644 3985 if (is_watchpoint (b))
3a5c3e22
PA
3986 {
3987 struct watchpoint *w = (struct watchpoint *) b;
3988
3989 cond = w->cond_exp;
3990 }
60e1c644
PA
3991 else
3992 cond = bl->cond;
3993
f431efe5 3994 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 3995 {
60e1c644 3996 int within_current_scope = 1;
3a5c3e22 3997 struct watchpoint * w;
60e1c644 3998
c5bc3a77
DJ
3999 /* We use value_mark and value_free_to_mark because it could
4000 be a long time before we return to the command level and
4001 call free_all_values. We can't call free_all_values
4002 because we might be in the middle of evaluating a
4003 function call. */
4004 struct value *mark = value_mark ();
4005
3a5c3e22
PA
4006 if (is_watchpoint (b))
4007 w = (struct watchpoint *) b;
4008 else
4009 w = NULL;
4010
edb3359d
DJ
4011 /* Need to select the frame, with all that implies so that
4012 the conditions will have the right context. Because we
4013 use the frame, we will not see an inlined function's
4014 variables when we arrive at a breakpoint at the start
4015 of the inlined function; the current frame will be the
4016 call site. */
3a5c3e22 4017 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
4018 select_frame (get_current_frame ());
4019 else
4020 {
4021 struct frame_info *frame;
4022
4023 /* For local watchpoint expressions, which particular
4024 instance of a local is being watched matters, so we
4025 keep track of the frame to evaluate the expression
4026 in. To evaluate the condition however, it doesn't
4027 really matter which instantiation of the function
4028 where the condition makes sense triggers the
4029 watchpoint. This allows an expression like "watch
4030 global if q > 10" set in `func', catch writes to
4031 global on all threads that call `func', or catch
4032 writes on all recursive calls of `func' by a single
4033 thread. We simply always evaluate the condition in
4034 the innermost frame that's executing where it makes
4035 sense to evaluate the condition. It seems
4036 intuitive. */
3a5c3e22 4037 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
4038 if (frame != NULL)
4039 select_frame (frame);
4040 else
4041 within_current_scope = 0;
4042 }
4043 if (within_current_scope)
4044 value_is_zero
4045 = catch_errors (breakpoint_cond_eval, cond,
4046 "Error in testing breakpoint condition:\n",
4047 RETURN_MASK_ALL);
4048 else
4049 {
4050 warning (_("Watchpoint condition cannot be tested "
4051 "in the current scope"));
4052 /* If we failed to set the right context for this
4053 watchpoint, unconditionally report it. */
4054 value_is_zero = 0;
4055 }
4a64f543 4056 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4057 value_free_to_mark (mark);
18a18393 4058 }
60e1c644
PA
4059
4060 if (cond && value_is_zero)
18a18393
VP
4061 {
4062 bs->stop = 0;
4063 }
4064 else if (b->thread != -1 && b->thread != thread_id)
4065 {
4066 bs->stop = 0;
4067 }
4068 else if (b->ignore_count > 0)
4069 {
4070 b->ignore_count--;
4071 annotate_ignore_count_change ();
4072 bs->stop = 0;
4a64f543 4073 /* Increase the hit count even though we don't stop. */
18a18393 4074 ++(b->hit_count);
8d3788bd 4075 observer_notify_breakpoint_modified (b);
18a18393
VP
4076 }
4077 }
4078}
4079
4080
9709f61c 4081/* Get a bpstat associated with having just stopped at address
d983da9c 4082 BP_ADDR in thread PTID.
c906108c 4083
d983da9c 4084 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4085 don't understand this stop. Result is a chain of bpstat's such
4086 that:
c906108c 4087
c5aa993b 4088 if we don't understand the stop, the result is a null pointer.
c906108c 4089
c5aa993b 4090 if we understand why we stopped, the result is not null.
c906108c 4091
c5aa993b
JM
4092 Each element of the chain refers to a particular breakpoint or
4093 watchpoint at which we have stopped. (We may have stopped for
4094 several reasons concurrently.)
c906108c 4095
c5aa993b
JM
4096 Each element of the chain has valid next, breakpoint_at,
4097 commands, FIXME??? fields. */
c906108c
SS
4098
4099bpstat
6c95b8df
PA
4100bpstat_stop_status (struct address_space *aspace,
4101 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4102{
0d381245 4103 struct breakpoint *b = NULL;
afe38095 4104 struct bp_location *bl;
20874c92 4105 struct bp_location *loc;
5760d0ab
JK
4106 /* First item of allocated bpstat's. */
4107 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4108 /* Pointer to the last thing in the chain currently. */
5760d0ab 4109 bpstat bs;
20874c92 4110 int ix;
429374b8 4111 int need_remove_insert;
f431efe5 4112 int removed_any;
c906108c 4113
f431efe5
PA
4114 /* First, build the bpstat chain with locations that explain a
4115 target stop, while being careful to not set the target running,
4116 as that may invalidate locations (in particular watchpoint
4117 locations are recreated). Resuming will happen here with
4118 breakpoint conditions or watchpoint expressions that include
4119 inferior function calls. */
c5aa993b 4120
429374b8
JK
4121 ALL_BREAKPOINTS (b)
4122 {
4123 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4124 continue;
a5606eee 4125
429374b8
JK
4126 for (bl = b->loc; bl != NULL; bl = bl->next)
4127 {
4a64f543
MS
4128 /* For hardware watchpoints, we look only at the first
4129 location. The watchpoint_check function will work on the
4130 entire expression, not the individual locations. For
4131 read watchpoints, the watchpoints_triggered function has
4132 checked all locations already. */
429374b8
JK
4133 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4134 break;
18a18393 4135
429374b8
JK
4136 if (bl->shlib_disabled)
4137 continue;
c5aa993b 4138
429374b8
JK
4139 if (!bpstat_check_location (bl, aspace, bp_addr))
4140 continue;
c5aa993b 4141
4a64f543
MS
4142 /* Come here if it's a watchpoint, or if the break address
4143 matches. */
c5aa993b 4144
4a64f543
MS
4145 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4146 explain stop. */
c5aa993b 4147
f431efe5
PA
4148 /* Assume we stop. Should we find a watchpoint that is not
4149 actually triggered, or if the condition of the breakpoint
4150 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4151 bs->stop = 1;
4152 bs->print = 1;
d983da9c 4153
f431efe5
PA
4154 /* If this is a scope breakpoint, mark the associated
4155 watchpoint as triggered so that we will handle the
4156 out-of-scope event. We'll get to the watchpoint next
4157 iteration. */
d0fb5eae 4158 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4159 {
4160 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4161
4162 w->watchpoint_triggered = watch_triggered_yes;
4163 }
f431efe5
PA
4164 }
4165 }
4166
4167 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4168 {
f1310107 4169 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4170 {
5760d0ab 4171 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4172 /* For hits of moribund locations, we should just proceed. */
4173 bs->stop = 0;
4174 bs->print = 0;
4175 bs->print_it = print_it_noop;
4176 }
4177 }
4178
f431efe5
PA
4179 /* Now go through the locations that caused the target to stop, and
4180 check whether we're interested in reporting this stop to higher
4181 layers, or whether we should resume the target transparently. */
4182
4183 removed_any = 0;
4184
5760d0ab 4185 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4186 {
4187 if (!bs->stop)
4188 continue;
4189
f431efe5 4190 b = bs->breakpoint_at;
348d480f
PA
4191 b->ops->check_status (bs);
4192 if (bs->stop)
28010a5d 4193 {
348d480f 4194 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4195
429374b8
JK
4196 if (bs->stop)
4197 {
4198 ++(b->hit_count);
8d3788bd 4199 observer_notify_breakpoint_modified (b);
c906108c 4200
4a64f543 4201 /* We will stop here. */
429374b8
JK
4202 if (b->disposition == disp_disable)
4203 {
4204 if (b->enable_state != bp_permanent)
4205 b->enable_state = bp_disabled;
f431efe5 4206 removed_any = 1;
429374b8
JK
4207 }
4208 if (b->silent)
4209 bs->print = 0;
4210 bs->commands = b->commands;
9add0f1b 4211 incref_counted_command_line (bs->commands);
abf85f46
JK
4212 if (command_line_is_silent (bs->commands
4213 ? bs->commands->commands : NULL))
4214 bs->print = 0;
429374b8
JK
4215 }
4216
e5dd4106 4217 /* Print nothing for this entry if we don't stop or don't print. */
429374b8
JK
4218 if (bs->stop == 0 || bs->print == 0)
4219 bs->print_it = print_it_noop;
348d480f 4220 }
429374b8 4221 }
876fa593 4222
d983da9c
DJ
4223 /* If we aren't stopping, the value of some hardware watchpoint may
4224 not have changed, but the intermediate memory locations we are
4225 watching may have. Don't bother if we're stopping; this will get
4226 done later. */
d832cb68 4227 need_remove_insert = 0;
5760d0ab
JK
4228 if (! bpstat_causes_stop (bs_head))
4229 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4230 if (!bs->stop
f431efe5
PA
4231 && bs->breakpoint_at
4232 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4233 {
3a5c3e22
PA
4234 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4235
4236 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 4237 need_remove_insert = 1;
d983da9c
DJ
4238 }
4239
d832cb68 4240 if (need_remove_insert)
2d134ed3 4241 update_global_location_list (1);
f431efe5
PA
4242 else if (removed_any)
4243 update_global_location_list (0);
d832cb68 4244
5760d0ab 4245 return bs_head;
c906108c 4246}
628fe4e4
JK
4247
4248static void
4249handle_jit_event (void)
4250{
4251 struct frame_info *frame;
4252 struct gdbarch *gdbarch;
4253
4254 /* Switch terminal for any messages produced by
4255 breakpoint_re_set. */
4256 target_terminal_ours_for_output ();
4257
4258 frame = get_current_frame ();
4259 gdbarch = get_frame_arch (frame);
4260
4261 jit_event_handler (gdbarch);
4262
4263 target_terminal_inferior ();
4264}
4265
4266/* Prepare WHAT final decision for infrun. */
4267
4268/* Decide what infrun needs to do with this bpstat. */
4269
c906108c 4270struct bpstat_what
0e30163f 4271bpstat_what (bpstat bs_head)
c906108c 4272{
c906108c 4273 struct bpstat_what retval;
628fe4e4
JK
4274 /* We need to defer calling `solib_add', as adding new symbols
4275 resets breakpoints, which in turn deletes breakpoint locations,
4276 and hence may clear unprocessed entries in the BS chain. */
4277 int shlib_event = 0;
4278 int jit_event = 0;
0e30163f 4279 bpstat bs;
c906108c 4280
628fe4e4 4281 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4282 retval.call_dummy = STOP_NONE;
186c406b 4283 retval.is_longjmp = 0;
628fe4e4 4284
0e30163f 4285 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4286 {
628fe4e4
JK
4287 /* Extract this BS's action. After processing each BS, we check
4288 if its action overrides all we've seem so far. */
4289 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4290 enum bptype bptype;
4291
c906108c 4292 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4293 {
4294 /* I suspect this can happen if it was a momentary
4295 breakpoint which has since been deleted. */
4296 bptype = bp_none;
4297 }
20874c92 4298 else
f431efe5 4299 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4300
4301 switch (bptype)
c906108c
SS
4302 {
4303 case bp_none:
628fe4e4 4304 break;
c906108c
SS
4305 case bp_breakpoint:
4306 case bp_hardware_breakpoint:
4307 case bp_until:
4308 case bp_finish:
4309 if (bs->stop)
4310 {
4311 if (bs->print)
628fe4e4 4312 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4313 else
628fe4e4 4314 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4315 }
4316 else
628fe4e4 4317 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4318 break;
4319 case bp_watchpoint:
4320 case bp_hardware_watchpoint:
4321 case bp_read_watchpoint:
4322 case bp_access_watchpoint:
4323 if (bs->stop)
4324 {
4325 if (bs->print)
628fe4e4 4326 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4327 else
628fe4e4 4328 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4329 }
4330 else
628fe4e4
JK
4331 {
4332 /* There was a watchpoint, but we're not stopping.
4333 This requires no further action. */
4334 }
c906108c
SS
4335 break;
4336 case bp_longjmp:
186c406b 4337 case bp_exception:
628fe4e4 4338 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4339 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4340 break;
4341 case bp_longjmp_resume:
186c406b 4342 case bp_exception_resume:
628fe4e4 4343 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4344 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4345 break;
4346 case bp_step_resume:
4347 if (bs->stop)
628fe4e4
JK
4348 this_action = BPSTAT_WHAT_STEP_RESUME;
4349 else
c906108c 4350 {
628fe4e4
JK
4351 /* It is for the wrong frame. */
4352 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4353 }
c906108c 4354 break;
2c03e5be
PA
4355 case bp_hp_step_resume:
4356 if (bs->stop)
4357 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4358 else
4359 {
4360 /* It is for the wrong frame. */
4361 this_action = BPSTAT_WHAT_SINGLE;
4362 }
4363 break;
c906108c 4364 case bp_watchpoint_scope:
c4093a6a 4365 case bp_thread_event:
1900040c 4366 case bp_overlay_event:
0fd8e87f 4367 case bp_longjmp_master:
aa7d318d 4368 case bp_std_terminate_master:
186c406b 4369 case bp_exception_master:
628fe4e4 4370 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4371 break;
ce78b96d 4372 case bp_catchpoint:
c5aa993b
JM
4373 if (bs->stop)
4374 {
4375 if (bs->print)
628fe4e4 4376 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4377 else
628fe4e4 4378 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4379 }
4380 else
628fe4e4
JK
4381 {
4382 /* There was a catchpoint, but we're not stopping.
4383 This requires no further action. */
4384 }
4385 break;
4386 case bp_shlib_event:
4387 shlib_event = 1;
4388
4389 /* If requested, stop when the dynamic linker notifies GDB
4390 of events. This allows the user to get control and place
4391 breakpoints in initializer routines for dynamically
4392 loaded objects (among other things). */
4393 if (stop_on_solib_events)
4394 this_action = BPSTAT_WHAT_STOP_NOISY;
4395 else
4396 this_action = BPSTAT_WHAT_SINGLE;
4397 break;
4398 case bp_jit_event:
4399 jit_event = 1;
4400 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4401 break;
c906108c 4402 case bp_call_dummy:
53a5351d
JM
4403 /* Make sure the action is stop (silent or noisy),
4404 so infrun.c pops the dummy frame. */
aa7d318d 4405 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4406 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4407 break;
4408 case bp_std_terminate:
4409 /* Make sure the action is stop (silent or noisy),
4410 so infrun.c pops the dummy frame. */
aa7d318d 4411 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4412 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4413 break;
1042e4c0 4414 case bp_tracepoint:
7a697b8d 4415 case bp_fast_tracepoint:
0fb4aa4b 4416 case bp_static_tracepoint:
1042e4c0
SS
4417 /* Tracepoint hits should not be reported back to GDB, and
4418 if one got through somehow, it should have been filtered
4419 out already. */
4420 internal_error (__FILE__, __LINE__,
7a697b8d 4421 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4422 break;
4423 case bp_gnu_ifunc_resolver:
4424 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4425 this_action = BPSTAT_WHAT_SINGLE;
4426 break;
4427 case bp_gnu_ifunc_resolver_return:
4428 /* The breakpoint will be removed, execution will restart from the
4429 PC of the former breakpoint. */
4430 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4431 break;
628fe4e4
JK
4432 default:
4433 internal_error (__FILE__, __LINE__,
4434 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4435 }
628fe4e4
JK
4436
4437 retval.main_action = max (retval.main_action, this_action);
c906108c 4438 }
628fe4e4 4439
0e30163f
JK
4440 /* These operations may affect the bs->breakpoint_at state so they are
4441 delayed after MAIN_ACTION is decided above. */
4442
628fe4e4
JK
4443 if (shlib_event)
4444 {
4445 if (debug_infrun)
4446 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4447
4448 /* Check for any newly added shared libraries if we're supposed
4449 to be adding them automatically. */
4450
4451 /* Switch terminal for any messages produced by
4452 breakpoint_re_set. */
4453 target_terminal_ours_for_output ();
4454
4455#ifdef SOLIB_ADD
4456 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4457#else
4458 solib_add (NULL, 0, &current_target, auto_solib_add);
4459#endif
4460
4461 target_terminal_inferior ();
4462 }
4463
4464 if (jit_event)
4465 {
4466 if (debug_infrun)
4467 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4468
4469 handle_jit_event ();
4470 }
4471
0e30163f
JK
4472 for (bs = bs_head; bs != NULL; bs = bs->next)
4473 {
4474 struct breakpoint *b = bs->breakpoint_at;
4475
4476 if (b == NULL)
4477 continue;
4478 switch (b->type)
4479 {
4480 case bp_gnu_ifunc_resolver:
4481 gnu_ifunc_resolver_stop (b);
4482 break;
4483 case bp_gnu_ifunc_resolver_return:
4484 gnu_ifunc_resolver_return_stop (b);
4485 break;
4486 }
4487 }
4488
c906108c
SS
4489 return retval;
4490}
4491
4492/* Nonzero if we should step constantly (e.g. watchpoints on machines
4493 without hardware support). This isn't related to a specific bpstat,
4494 just to things like whether watchpoints are set. */
4495
c5aa993b 4496int
fba45db2 4497bpstat_should_step (void)
c906108c
SS
4498{
4499 struct breakpoint *b;
cc59ec59 4500
c906108c 4501 ALL_BREAKPOINTS (b)
717a8278 4502 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4503 return 1;
c906108c
SS
4504 return 0;
4505}
4506
67822962
PA
4507int
4508bpstat_causes_stop (bpstat bs)
4509{
4510 for (; bs != NULL; bs = bs->next)
4511 if (bs->stop)
4512 return 1;
4513
4514 return 0;
4515}
4516
c906108c 4517\f
c5aa993b 4518
170b53b2
UW
4519/* Compute a string of spaces suitable to indent the next line
4520 so it starts at the position corresponding to the table column
4521 named COL_NAME in the currently active table of UIOUT. */
4522
4523static char *
4524wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4525{
4526 static char wrap_indent[80];
4527 int i, total_width, width, align;
4528 char *text;
4529
4530 total_width = 0;
4531 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4532 {
4533 if (strcmp (text, col_name) == 0)
4534 {
4535 gdb_assert (total_width < sizeof wrap_indent);
4536 memset (wrap_indent, ' ', total_width);
4537 wrap_indent[total_width] = 0;
4538
4539 return wrap_indent;
4540 }
4541
4542 total_width += width + 1;
4543 }
4544
4545 return NULL;
4546}
4547
859825b8
JK
4548/* Print the LOC location out of the list of B->LOC locations. */
4549
170b53b2
UW
4550static void
4551print_breakpoint_location (struct breakpoint *b,
4552 struct bp_location *loc)
0d381245 4553{
79a45e25 4554 struct ui_out *uiout = current_uiout;
6c95b8df
PA
4555 struct cleanup *old_chain = save_current_program_space ();
4556
859825b8
JK
4557 if (loc != NULL && loc->shlib_disabled)
4558 loc = NULL;
4559
6c95b8df
PA
4560 if (loc != NULL)
4561 set_current_program_space (loc->pspace);
4562
56435ebe
TT
4563 if (b->display_canonical)
4564 ui_out_field_string (uiout, "what", b->addr_string);
4565 else if (b->source_file && loc)
0d381245
VP
4566 {
4567 struct symbol *sym
4568 = find_pc_sect_function (loc->address, loc->section);
4569 if (sym)
4570 {
4571 ui_out_text (uiout, "in ");
4572 ui_out_field_string (uiout, "func",
4573 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4574 ui_out_text (uiout, " ");
4575 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4576 ui_out_text (uiout, "at ");
0d381245
VP
4577 }
4578 ui_out_field_string (uiout, "file", b->source_file);
4579 ui_out_text (uiout, ":");
4580
4581 if (ui_out_is_mi_like_p (uiout))
4582 {
4583 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4584 char *fullname = symtab_to_fullname (sal.symtab);
4585
4586 if (fullname)
4587 ui_out_field_string (uiout, "fullname", fullname);
4588 }
4589
4590 ui_out_field_int (uiout, "line", b->line_number);
4591 }
859825b8 4592 else if (loc)
0d381245 4593 {
170b53b2
UW
4594 struct ui_stream *stb = ui_out_stream_new (uiout);
4595 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4596
22e722e1
DJ
4597 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4598 demangle, "");
0d381245 4599 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4600
4601 do_cleanups (stb_chain);
0d381245 4602 }
859825b8
JK
4603 else
4604 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4605
4606 do_cleanups (old_chain);
0d381245
VP
4607}
4608
269b11a2
PA
4609static const char *
4610bptype_string (enum bptype type)
c906108c 4611{
c4093a6a
JM
4612 struct ep_type_description
4613 {
4614 enum bptype type;
4615 char *description;
4616 };
4617 static struct ep_type_description bptypes[] =
c906108c 4618 {
c5aa993b
JM
4619 {bp_none, "?deleted?"},
4620 {bp_breakpoint, "breakpoint"},
c906108c 4621 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4622 {bp_until, "until"},
4623 {bp_finish, "finish"},
4624 {bp_watchpoint, "watchpoint"},
c906108c 4625 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4626 {bp_read_watchpoint, "read watchpoint"},
4627 {bp_access_watchpoint, "acc watchpoint"},
4628 {bp_longjmp, "longjmp"},
4629 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4630 {bp_exception, "exception"},
4631 {bp_exception_resume, "exception resume"},
c5aa993b 4632 {bp_step_resume, "step resume"},
2c03e5be 4633 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
4634 {bp_watchpoint_scope, "watchpoint scope"},
4635 {bp_call_dummy, "call dummy"},
aa7d318d 4636 {bp_std_terminate, "std::terminate"},
c5aa993b 4637 {bp_shlib_event, "shlib events"},
c4093a6a 4638 {bp_thread_event, "thread events"},
1900040c 4639 {bp_overlay_event, "overlay events"},
0fd8e87f 4640 {bp_longjmp_master, "longjmp master"},
aa7d318d 4641 {bp_std_terminate_master, "std::terminate master"},
186c406b 4642 {bp_exception_master, "exception master"},
ce78b96d 4643 {bp_catchpoint, "catchpoint"},
1042e4c0 4644 {bp_tracepoint, "tracepoint"},
7a697b8d 4645 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4646 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4647 {bp_jit_event, "jit events"},
0e30163f
JK
4648 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4649 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4650 };
269b11a2
PA
4651
4652 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4653 || ((int) type != bptypes[(int) type].type))
4654 internal_error (__FILE__, __LINE__,
4655 _("bptypes table does not describe type #%d."),
4656 (int) type);
4657
4658 return bptypes[(int) type].description;
4659}
4660
4661/* Print B to gdb_stdout. */
4662
4663static void
4664print_one_breakpoint_location (struct breakpoint *b,
4665 struct bp_location *loc,
4666 int loc_number,
4667 struct bp_location **last_loc,
269b11a2
PA
4668 int allflag)
4669{
4670 struct command_line *l;
c2c6d25f 4671 static char bpenables[] = "nynny";
c906108c 4672
79a45e25 4673 struct ui_out *uiout = current_uiout;
0d381245
VP
4674 int header_of_multiple = 0;
4675 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4676 struct value_print_options opts;
4677
4678 get_user_print_options (&opts);
0d381245
VP
4679
4680 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4681 /* See comment in print_one_breakpoint concerning treatment of
4682 breakpoints with single disabled location. */
0d381245
VP
4683 if (loc == NULL
4684 && (b->loc != NULL
4685 && (b->loc->next != NULL || !b->loc->enabled)))
4686 header_of_multiple = 1;
4687 if (loc == NULL)
4688 loc = b->loc;
4689
c4093a6a
JM
4690 annotate_record ();
4691
4692 /* 1 */
4693 annotate_field (0);
0d381245
VP
4694 if (part_of_multiple)
4695 {
4696 char *formatted;
0c6773c1 4697 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4698 ui_out_field_string (uiout, "number", formatted);
4699 xfree (formatted);
4700 }
4701 else
4702 {
4703 ui_out_field_int (uiout, "number", b->number);
4704 }
c4093a6a
JM
4705
4706 /* 2 */
4707 annotate_field (1);
0d381245
VP
4708 if (part_of_multiple)
4709 ui_out_field_skip (uiout, "type");
269b11a2
PA
4710 else
4711 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4712
4713 /* 3 */
4714 annotate_field (2);
0d381245
VP
4715 if (part_of_multiple)
4716 ui_out_field_skip (uiout, "disp");
4717 else
2cec12e5 4718 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4719
c4093a6a
JM
4720
4721 /* 4 */
4722 annotate_field (3);
0d381245 4723 if (part_of_multiple)
54e52265 4724 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4725 else
4a64f543
MS
4726 ui_out_field_fmt (uiout, "enabled", "%c",
4727 bpenables[(int) b->enable_state]);
54e52265 4728 ui_out_spaces (uiout, 2);
0d381245 4729
c4093a6a
JM
4730
4731 /* 5 and 6 */
3086aeae 4732 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4733 {
4a64f543
MS
4734 /* Although the print_one can possibly print all locations,
4735 calling it here is not likely to get any nice result. So,
4736 make sure there's just one location. */
0d381245 4737 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4738 b->ops->print_one (b, last_loc);
0d381245 4739 }
3086aeae
DJ
4740 else
4741 switch (b->type)
4742 {
4743 case bp_none:
4744 internal_error (__FILE__, __LINE__,
e2e0b3e5 4745 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4746 break;
c906108c 4747
3086aeae
DJ
4748 case bp_watchpoint:
4749 case bp_hardware_watchpoint:
4750 case bp_read_watchpoint:
4751 case bp_access_watchpoint:
3a5c3e22
PA
4752 {
4753 struct watchpoint *w = (struct watchpoint *) b;
4754
4755 /* Field 4, the address, is omitted (which makes the columns
4756 not line up too nicely with the headers, but the effect
4757 is relatively readable). */
4758 if (opts.addressprint)
4759 ui_out_field_skip (uiout, "addr");
4760 annotate_field (5);
4761 ui_out_field_string (uiout, "what", w->exp_string);
4762 }
3086aeae
DJ
4763 break;
4764
3086aeae
DJ
4765 case bp_breakpoint:
4766 case bp_hardware_breakpoint:
4767 case bp_until:
4768 case bp_finish:
4769 case bp_longjmp:
4770 case bp_longjmp_resume:
186c406b
TT
4771 case bp_exception:
4772 case bp_exception_resume:
3086aeae 4773 case bp_step_resume:
2c03e5be 4774 case bp_hp_step_resume:
3086aeae
DJ
4775 case bp_watchpoint_scope:
4776 case bp_call_dummy:
aa7d318d 4777 case bp_std_terminate:
3086aeae
DJ
4778 case bp_shlib_event:
4779 case bp_thread_event:
4780 case bp_overlay_event:
0fd8e87f 4781 case bp_longjmp_master:
aa7d318d 4782 case bp_std_terminate_master:
186c406b 4783 case bp_exception_master:
1042e4c0 4784 case bp_tracepoint:
7a697b8d 4785 case bp_fast_tracepoint:
0fb4aa4b 4786 case bp_static_tracepoint:
4efc6507 4787 case bp_jit_event:
0e30163f
JK
4788 case bp_gnu_ifunc_resolver:
4789 case bp_gnu_ifunc_resolver_return:
79a45b7d 4790 if (opts.addressprint)
3086aeae
DJ
4791 {
4792 annotate_field (4);
54e52265 4793 if (header_of_multiple)
0d381245 4794 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4795 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4796 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4797 else
5af949e3
UW
4798 ui_out_field_core_addr (uiout, "addr",
4799 loc->gdbarch, loc->address);
3086aeae
DJ
4800 }
4801 annotate_field (5);
0d381245 4802 if (!header_of_multiple)
170b53b2 4803 print_breakpoint_location (b, loc);
0d381245 4804 if (b->loc)
a6d9a66e 4805 *last_loc = b->loc;
3086aeae
DJ
4806 break;
4807 }
c906108c 4808
6c95b8df
PA
4809
4810 /* For backward compatibility, don't display inferiors unless there
4811 are several. */
4812 if (loc != NULL
4813 && !header_of_multiple
4814 && (allflag
4815 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4816 && (number_of_program_spaces () > 1
4817 || number_of_inferiors () > 1)
4a64f543
MS
4818 /* LOC is for existing B, it cannot be in
4819 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4820 && loc->owner->type != bp_catchpoint)))
4821 {
4822 struct inferior *inf;
4823 int first = 1;
4824
4825 for (inf = inferior_list; inf != NULL; inf = inf->next)
4826 {
4827 if (inf->pspace == loc->pspace)
4828 {
4829 if (first)
4830 {
4831 first = 0;
4832 ui_out_text (uiout, " inf ");
4833 }
4834 else
4835 ui_out_text (uiout, ", ");
4836 ui_out_text (uiout, plongest (inf->num));
4837 }
4838 }
4839 }
4840
4a306c9a 4841 if (!part_of_multiple)
c4093a6a 4842 {
4a306c9a
JB
4843 if (b->thread != -1)
4844 {
4845 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4846 "stop only in" line a little further down. */
4a306c9a
JB
4847 ui_out_text (uiout, " thread ");
4848 ui_out_field_int (uiout, "thread", b->thread);
4849 }
4850 else if (b->task != 0)
4851 {
4852 ui_out_text (uiout, " task ");
4853 ui_out_field_int (uiout, "task", b->task);
4854 }
c4093a6a 4855 }
f1310107 4856
8b93c638 4857 ui_out_text (uiout, "\n");
f1310107 4858
348d480f 4859 if (!part_of_multiple)
f1310107
TJB
4860 b->ops->print_one_detail (b, uiout);
4861
0d381245 4862 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4863 {
4864 annotate_field (6);
8b93c638 4865 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 4866 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 4867 the frame ID. */
5af949e3
UW
4868 ui_out_field_core_addr (uiout, "frame",
4869 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4870 ui_out_text (uiout, "\n");
c4093a6a
JM
4871 }
4872
28010a5d 4873 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
4874 {
4875 annotate_field (7);
d77f58be 4876 if (is_tracepoint (b))
1042e4c0
SS
4877 ui_out_text (uiout, "\ttrace only if ");
4878 else
4879 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4880 ui_out_field_string (uiout, "cond", b->cond_string);
4881 ui_out_text (uiout, "\n");
4882 }
4883
0d381245 4884 if (!part_of_multiple && b->thread != -1)
c4093a6a 4885 {
4a64f543 4886 /* FIXME should make an annotation for this. */
8b93c638
JM
4887 ui_out_text (uiout, "\tstop only in thread ");
4888 ui_out_field_int (uiout, "thread", b->thread);
4889 ui_out_text (uiout, "\n");
c4093a6a
JM
4890 }
4891
63c715c6 4892 if (!part_of_multiple && b->hit_count)
c4093a6a 4893 {
4a64f543 4894 /* FIXME should make an annotation for this. */
8b93c638
JM
4895 if (ep_is_catchpoint (b))
4896 ui_out_text (uiout, "\tcatchpoint");
f196051f
SS
4897 else if (is_tracepoint (b))
4898 ui_out_text (uiout, "\ttracepoint");
8b93c638
JM
4899 else
4900 ui_out_text (uiout, "\tbreakpoint");
4901 ui_out_text (uiout, " already hit ");
4902 ui_out_field_int (uiout, "times", b->hit_count);
4903 if (b->hit_count == 1)
4904 ui_out_text (uiout, " time\n");
4905 else
4906 ui_out_text (uiout, " times\n");
c4093a6a
JM
4907 }
4908
4a64f543
MS
4909 /* Output the count also if it is zero, but only if this is mi.
4910 FIXME: Should have a better test for this. */
9dc5e2a9 4911 if (ui_out_is_mi_like_p (uiout))
63c715c6 4912 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4913 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4914
0d381245 4915 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4916 {
4917 annotate_field (8);
8b93c638
JM
4918 ui_out_text (uiout, "\tignore next ");
4919 ui_out_field_int (uiout, "ignore", b->ignore_count);
4920 ui_out_text (uiout, " hits\n");
c4093a6a 4921 }
059fb39f 4922
f196051f
SS
4923 if (!part_of_multiple && is_tracepoint (b))
4924 {
4925 struct tracepoint *tp = (struct tracepoint *) b;
4926
4927 if (tp->traceframe_usage)
4928 {
4929 ui_out_text (uiout, "\ttrace buffer usage ");
4930 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
4931 ui_out_text (uiout, " bytes\n");
4932 }
4933 }
4934
9add0f1b 4935 l = b->commands ? b->commands->commands : NULL;
059fb39f 4936 if (!part_of_multiple && l)
c4093a6a 4937 {
3b31d625
EZ
4938 struct cleanup *script_chain;
4939
c4093a6a 4940 annotate_field (9);
3b31d625 4941 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4942 print_command_lines (uiout, l, 4);
3b31d625 4943 do_cleanups (script_chain);
c4093a6a 4944 }
d24317b4 4945
d9b3f62e 4946 if (is_tracepoint (b))
1042e4c0 4947 {
d9b3f62e
PA
4948 struct tracepoint *t = (struct tracepoint *) b;
4949
4950 if (!part_of_multiple && t->pass_count)
4951 {
4952 annotate_field (10);
4953 ui_out_text (uiout, "\tpass count ");
4954 ui_out_field_int (uiout, "pass", t->pass_count);
4955 ui_out_text (uiout, " \n");
4956 }
1042e4c0
SS
4957 }
4958
d24317b4
VP
4959 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4960 {
3a5c3e22
PA
4961 if (is_watchpoint (b))
4962 {
4963 struct watchpoint *w = (struct watchpoint *) b;
4964
4965 ui_out_field_string (uiout, "original-location", w->exp_string);
4966 }
4967 else if (b->addr_string)
d24317b4 4968 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 4969 }
c4093a6a 4970}
c5aa993b 4971
0d381245
VP
4972static void
4973print_one_breakpoint (struct breakpoint *b,
4a64f543 4974 struct bp_location **last_loc,
6c95b8df 4975 int allflag)
0d381245 4976{
8d3788bd 4977 struct cleanup *bkpt_chain;
79a45e25 4978 struct ui_out *uiout = current_uiout;
8d3788bd
VP
4979
4980 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
4981
12c5a436 4982 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 4983 do_cleanups (bkpt_chain);
0d381245
VP
4984
4985 /* If this breakpoint has custom print function,
4986 it's already printed. Otherwise, print individual
4987 locations, if any. */
4988 if (b->ops == NULL || b->ops->print_one == NULL)
4989 {
4a64f543
MS
4990 /* If breakpoint has a single location that is disabled, we
4991 print it as if it had several locations, since otherwise it's
4992 hard to represent "breakpoint enabled, location disabled"
4993 situation.
4994
4995 Note that while hardware watchpoints have several locations
a3be7890 4996 internally, that's not a property exposed to user. */
0d381245 4997 if (b->loc
a5606eee 4998 && !is_hardware_watchpoint (b)
8d3788bd 4999 && (b->loc->next || !b->loc->enabled))
0d381245
VP
5000 {
5001 struct bp_location *loc;
5002 int n = 1;
8d3788bd 5003
0d381245 5004 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5005 {
5006 struct cleanup *inner2 =
5007 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5008 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5009 do_cleanups (inner2);
5010 }
0d381245
VP
5011 }
5012 }
5013}
5014
a6d9a66e
UW
5015static int
5016breakpoint_address_bits (struct breakpoint *b)
5017{
5018 int print_address_bits = 0;
5019 struct bp_location *loc;
5020
5021 for (loc = b->loc; loc; loc = loc->next)
5022 {
c7437ca6
PA
5023 int addr_bit;
5024
5025 /* Software watchpoints that aren't watching memory don't have
5026 an address to print. */
5027 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5028 continue;
5029
5030 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5031 if (addr_bit > print_address_bits)
5032 print_address_bits = addr_bit;
5033 }
5034
5035 return print_address_bits;
5036}
0d381245 5037
c4093a6a
JM
5038struct captured_breakpoint_query_args
5039 {
5040 int bnum;
5041 };
c5aa993b 5042
c4093a6a 5043static int
2b65245e 5044do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5045{
5046 struct captured_breakpoint_query_args *args = data;
52f0bd74 5047 struct breakpoint *b;
a6d9a66e 5048 struct bp_location *dummy_loc = NULL;
cc59ec59 5049
c4093a6a
JM
5050 ALL_BREAKPOINTS (b)
5051 {
5052 if (args->bnum == b->number)
c5aa993b 5053 {
12c5a436 5054 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5055 return GDB_RC_OK;
c5aa993b 5056 }
c4093a6a
JM
5057 }
5058 return GDB_RC_NONE;
5059}
c5aa993b 5060
c4093a6a 5061enum gdb_rc
4a64f543
MS
5062gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5063 char **error_message)
c4093a6a
JM
5064{
5065 struct captured_breakpoint_query_args args;
cc59ec59 5066
c4093a6a
JM
5067 args.bnum = bnum;
5068 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5069 an error. */
b0b13bb4
DJ
5070 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5071 error_message, RETURN_MASK_ALL) < 0)
5072 return GDB_RC_FAIL;
5073 else
5074 return GDB_RC_OK;
c4093a6a 5075}
c5aa993b 5076
09d682a4
TT
5077/* Return true if this breakpoint was set by the user, false if it is
5078 internal or momentary. */
5079
5080int
5081user_breakpoint_p (struct breakpoint *b)
5082{
46c6471b 5083 return b->number > 0;
09d682a4
TT
5084}
5085
7f3b0473 5086/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5087 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5088 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5089 FILTER is non-NULL, call it on each breakpoint and only include the
5090 ones for which it returns non-zero. Return the total number of
5091 breakpoints listed. */
c906108c 5092
d77f58be 5093static int
e5a67952 5094breakpoint_1 (char *args, int allflag,
4a64f543 5095 int (*filter) (const struct breakpoint *))
c4093a6a 5096{
52f0bd74 5097 struct breakpoint *b;
a6d9a66e 5098 struct bp_location *last_loc = NULL;
7f3b0473 5099 int nr_printable_breakpoints;
3b31d625 5100 struct cleanup *bkpttbl_chain;
79a45b7d 5101 struct value_print_options opts;
a6d9a66e 5102 int print_address_bits = 0;
269b11a2 5103 int print_type_col_width = 14;
79a45e25 5104 struct ui_out *uiout = current_uiout;
269b11a2 5105
79a45b7d
TT
5106 get_user_print_options (&opts);
5107
4a64f543
MS
5108 /* Compute the number of rows in the table, as well as the size
5109 required for address fields. */
7f3b0473
AC
5110 nr_printable_breakpoints = 0;
5111 ALL_BREAKPOINTS (b)
e5a67952
MS
5112 {
5113 /* If we have a filter, only list the breakpoints it accepts. */
5114 if (filter && !filter (b))
5115 continue;
5116
5117 /* If we have an "args" string, it is a list of breakpoints to
5118 accept. Skip the others. */
5119 if (args != NULL && *args != '\0')
5120 {
5121 if (allflag && parse_and_eval_long (args) != b->number)
5122 continue;
5123 if (!allflag && !number_is_in_list (args, b->number))
5124 continue;
5125 }
269b11a2 5126
e5a67952
MS
5127 if (allflag || user_breakpoint_p (b))
5128 {
5129 int addr_bit, type_len;
a6d9a66e 5130
e5a67952
MS
5131 addr_bit = breakpoint_address_bits (b);
5132 if (addr_bit > print_address_bits)
5133 print_address_bits = addr_bit;
269b11a2 5134
e5a67952
MS
5135 type_len = strlen (bptype_string (b->type));
5136 if (type_len > print_type_col_width)
5137 print_type_col_width = type_len;
5138
5139 nr_printable_breakpoints++;
5140 }
5141 }
7f3b0473 5142
79a45b7d 5143 if (opts.addressprint)
3b31d625 5144 bkpttbl_chain
3e43a32a
MS
5145 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5146 nr_printable_breakpoints,
3b31d625 5147 "BreakpointTable");
8b93c638 5148 else
3b31d625 5149 bkpttbl_chain
3e43a32a
MS
5150 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5151 nr_printable_breakpoints,
3b31d625 5152 "BreakpointTable");
8b93c638 5153
7f3b0473 5154 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5155 annotate_breakpoints_headers ();
5156 if (nr_printable_breakpoints > 0)
5157 annotate_field (0);
4a64f543 5158 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5159 if (nr_printable_breakpoints > 0)
5160 annotate_field (1);
269b11a2 5161 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5162 "type", "Type"); /* 2 */
d7faa9e7
AC
5163 if (nr_printable_breakpoints > 0)
5164 annotate_field (2);
4a64f543 5165 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5166 if (nr_printable_breakpoints > 0)
5167 annotate_field (3);
54e52265 5168 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5169 if (opts.addressprint)
e5a67952
MS
5170 {
5171 if (nr_printable_breakpoints > 0)
5172 annotate_field (4);
5173 if (print_address_bits <= 32)
5174 ui_out_table_header (uiout, 10, ui_left,
5175 "addr", "Address"); /* 5 */
5176 else
5177 ui_out_table_header (uiout, 18, ui_left,
5178 "addr", "Address"); /* 5 */
5179 }
d7faa9e7
AC
5180 if (nr_printable_breakpoints > 0)
5181 annotate_field (5);
5182 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5183 ui_out_table_body (uiout);
5184 if (nr_printable_breakpoints > 0)
5185 annotate_breakpoints_table ();
7f3b0473 5186
c4093a6a 5187 ALL_BREAKPOINTS (b)
e5a67952
MS
5188 {
5189 QUIT;
5190 /* If we have a filter, only list the breakpoints it accepts. */
5191 if (filter && !filter (b))
5192 continue;
5193
5194 /* If we have an "args" string, it is a list of breakpoints to
5195 accept. Skip the others. */
5196
5197 if (args != NULL && *args != '\0')
5198 {
5199 if (allflag) /* maintenance info breakpoint */
5200 {
5201 if (parse_and_eval_long (args) != b->number)
5202 continue;
5203 }
5204 else /* all others */
5205 {
5206 if (!number_is_in_list (args, b->number))
5207 continue;
5208 }
5209 }
5210 /* We only print out user settable breakpoints unless the
5211 allflag is set. */
5212 if (allflag || user_breakpoint_p (b))
12c5a436 5213 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5214 }
5215
3b31d625 5216 do_cleanups (bkpttbl_chain);
698384cd 5217
7f3b0473 5218 if (nr_printable_breakpoints == 0)
c906108c 5219 {
4a64f543
MS
5220 /* If there's a filter, let the caller decide how to report
5221 empty list. */
d77f58be
SS
5222 if (!filter)
5223 {
e5a67952 5224 if (args == NULL || *args == '\0')
d77f58be
SS
5225 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5226 else
4a64f543 5227 ui_out_message (uiout, 0,
e5a67952
MS
5228 "No breakpoint or watchpoint matching '%s'.\n",
5229 args);
d77f58be 5230 }
c906108c
SS
5231 }
5232 else
c4093a6a 5233 {
a6d9a66e
UW
5234 if (last_loc && !server_command)
5235 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5236 }
c906108c 5237
4a64f543 5238 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5239 there have been breakpoints? */
c906108c 5240 annotate_breakpoints_table_end ();
d77f58be
SS
5241
5242 return nr_printable_breakpoints;
c906108c
SS
5243}
5244
ad443146
SS
5245/* Display the value of default-collect in a way that is generally
5246 compatible with the breakpoint list. */
5247
5248static void
5249default_collect_info (void)
5250{
79a45e25
PA
5251 struct ui_out *uiout = current_uiout;
5252
ad443146
SS
5253 /* If it has no value (which is frequently the case), say nothing; a
5254 message like "No default-collect." gets in user's face when it's
5255 not wanted. */
5256 if (!*default_collect)
5257 return;
5258
5259 /* The following phrase lines up nicely with per-tracepoint collect
5260 actions. */
5261 ui_out_text (uiout, "default collect ");
5262 ui_out_field_string (uiout, "default-collect", default_collect);
5263 ui_out_text (uiout, " \n");
5264}
5265
c906108c 5266static void
e5a67952 5267breakpoints_info (char *args, int from_tty)
c906108c 5268{
e5a67952 5269 breakpoint_1 (args, 0, NULL);
ad443146
SS
5270
5271 default_collect_info ();
d77f58be
SS
5272}
5273
5274static void
e5a67952 5275watchpoints_info (char *args, int from_tty)
d77f58be 5276{
e5a67952 5277 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 5278 struct ui_out *uiout = current_uiout;
d77f58be
SS
5279
5280 if (num_printed == 0)
5281 {
e5a67952 5282 if (args == NULL || *args == '\0')
d77f58be
SS
5283 ui_out_message (uiout, 0, "No watchpoints.\n");
5284 else
e5a67952 5285 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5286 }
c906108c
SS
5287}
5288
7a292a7a 5289static void
e5a67952 5290maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5291{
e5a67952 5292 breakpoint_1 (args, 1, NULL);
ad443146
SS
5293
5294 default_collect_info ();
c906108c
SS
5295}
5296
0d381245 5297static int
714835d5 5298breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5299 struct program_space *pspace,
714835d5 5300 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5301{
5302 struct bp_location *bl = b->loc;
cc59ec59 5303
0d381245
VP
5304 for (; bl; bl = bl->next)
5305 {
6c95b8df
PA
5306 if (bl->pspace == pspace
5307 && bl->address == pc
0d381245
VP
5308 && (!overlay_debugging || bl->section == section))
5309 return 1;
5310 }
5311 return 0;
5312}
5313
672f9b60 5314/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
5315 concerns with logical breakpoints, so we match program spaces, not
5316 address spaces. */
c906108c
SS
5317
5318static void
6c95b8df
PA
5319describe_other_breakpoints (struct gdbarch *gdbarch,
5320 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5321 struct obj_section *section, int thread)
c906108c 5322{
52f0bd74
AC
5323 int others = 0;
5324 struct breakpoint *b;
c906108c
SS
5325
5326 ALL_BREAKPOINTS (b)
672f9b60
KP
5327 others += (user_breakpoint_p (b)
5328 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
5329 if (others > 0)
5330 {
a3f17187
AC
5331 if (others == 1)
5332 printf_filtered (_("Note: breakpoint "));
5333 else /* if (others == ???) */
5334 printf_filtered (_("Note: breakpoints "));
c906108c 5335 ALL_BREAKPOINTS (b)
672f9b60 5336 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5337 {
5338 others--;
5339 printf_filtered ("%d", b->number);
5340 if (b->thread == -1 && thread != -1)
5341 printf_filtered (" (all threads)");
5342 else if (b->thread != -1)
5343 printf_filtered (" (thread %d)", b->thread);
5344 printf_filtered ("%s%s ",
059fb39f 5345 ((b->enable_state == bp_disabled
8bea4e01
UW
5346 || b->enable_state == bp_call_disabled
5347 || b->enable_state == bp_startup_disabled)
0d381245
VP
5348 ? " (disabled)"
5349 : b->enable_state == bp_permanent
5350 ? " (permanent)"
5351 : ""),
5352 (others > 1) ? ","
5353 : ((others == 1) ? " and" : ""));
5354 }
a3f17187 5355 printf_filtered (_("also set at pc "));
5af949e3 5356 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5357 printf_filtered (".\n");
5358 }
5359}
5360\f
c906108c 5361
e4f237da
KB
5362/* Return true iff it is meaningful to use the address member of
5363 BPT. For some breakpoint types, the address member is irrelevant
5364 and it makes no sense to attempt to compare it to other addresses
5365 (or use it for any other purpose either).
5366
4a64f543
MS
5367 More specifically, each of the following breakpoint types will
5368 always have a zero valued address and we don't want to mark
5369 breakpoints of any of these types to be a duplicate of an actual
5370 breakpoint at address zero:
e4f237da
KB
5371
5372 bp_watchpoint
2d134ed3
PA
5373 bp_catchpoint
5374
5375*/
e4f237da
KB
5376
5377static int
5378breakpoint_address_is_meaningful (struct breakpoint *bpt)
5379{
5380 enum bptype type = bpt->type;
5381
2d134ed3
PA
5382 return (type != bp_watchpoint && type != bp_catchpoint);
5383}
5384
5385/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5386 true if LOC1 and LOC2 represent the same watchpoint location. */
5387
5388static int
4a64f543
MS
5389watchpoint_locations_match (struct bp_location *loc1,
5390 struct bp_location *loc2)
2d134ed3 5391{
3a5c3e22
PA
5392 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
5393 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
5394
5395 /* Both of them must exist. */
5396 gdb_assert (w1 != NULL);
5397 gdb_assert (w2 != NULL);
2bdf28a0 5398
4a64f543
MS
5399 /* If the target can evaluate the condition expression in hardware,
5400 then we we need to insert both watchpoints even if they are at
5401 the same place. Otherwise the watchpoint will only trigger when
5402 the condition of whichever watchpoint was inserted evaluates to
5403 true, not giving a chance for GDB to check the condition of the
5404 other watchpoint. */
3a5c3e22 5405 if ((w1->cond_exp
4a64f543
MS
5406 && target_can_accel_watchpoint_condition (loc1->address,
5407 loc1->length,
0cf6dd15 5408 loc1->watchpoint_type,
3a5c3e22
PA
5409 w1->cond_exp))
5410 || (w2->cond_exp
4a64f543
MS
5411 && target_can_accel_watchpoint_condition (loc2->address,
5412 loc2->length,
0cf6dd15 5413 loc2->watchpoint_type,
3a5c3e22 5414 w2->cond_exp)))
0cf6dd15
TJB
5415 return 0;
5416
85d721b8
PA
5417 /* Note that this checks the owner's type, not the location's. In
5418 case the target does not support read watchpoints, but does
5419 support access watchpoints, we'll have bp_read_watchpoint
5420 watchpoints with hw_access locations. Those should be considered
5421 duplicates of hw_read locations. The hw_read locations will
5422 become hw_access locations later. */
2d134ed3
PA
5423 return (loc1->owner->type == loc2->owner->type
5424 && loc1->pspace->aspace == loc2->pspace->aspace
5425 && loc1->address == loc2->address
5426 && loc1->length == loc2->length);
e4f237da
KB
5427}
5428
6c95b8df
PA
5429/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5430 same breakpoint location. In most targets, this can only be true
5431 if ASPACE1 matches ASPACE2. On targets that have global
5432 breakpoints, the address space doesn't really matter. */
5433
5434static int
5435breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5436 struct address_space *aspace2, CORE_ADDR addr2)
5437{
5438 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5439 || aspace1 == aspace2)
5440 && addr1 == addr2);
5441}
5442
f1310107
TJB
5443/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5444 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5445 matches ASPACE2. On targets that have global breakpoints, the address
5446 space doesn't really matter. */
5447
5448static int
5449breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5450 int len1, struct address_space *aspace2,
5451 CORE_ADDR addr2)
5452{
5453 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5454 || aspace1 == aspace2)
5455 && addr2 >= addr1 && addr2 < addr1 + len1);
5456}
5457
5458/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5459 a ranged breakpoint. In most targets, a match happens only if ASPACE
5460 matches the breakpoint's address space. On targets that have global
5461 breakpoints, the address space doesn't really matter. */
5462
5463static int
5464breakpoint_location_address_match (struct bp_location *bl,
5465 struct address_space *aspace,
5466 CORE_ADDR addr)
5467{
5468 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5469 aspace, addr)
5470 || (bl->length
5471 && breakpoint_address_match_range (bl->pspace->aspace,
5472 bl->address, bl->length,
5473 aspace, addr)));
5474}
5475
1e4d1764
YQ
5476/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
5477 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
5478 true, otherwise returns false. */
5479
5480static int
5481tracepoint_locations_match (struct bp_location *loc1,
5482 struct bp_location *loc2)
5483{
5484 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
5485 /* Since tracepoint locations are never duplicated with others', tracepoint
5486 locations at the same address of different tracepoints are regarded as
5487 different locations. */
5488 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
5489 else
5490 return 0;
5491}
5492
2d134ed3
PA
5493/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5494 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5495 represent the same location. */
5496
5497static int
4a64f543
MS
5498breakpoint_locations_match (struct bp_location *loc1,
5499 struct bp_location *loc2)
2d134ed3 5500{
2bdf28a0
JK
5501 int hw_point1, hw_point2;
5502
5503 /* Both of them must not be in moribund_locations. */
5504 gdb_assert (loc1->owner != NULL);
5505 gdb_assert (loc2->owner != NULL);
5506
5507 hw_point1 = is_hardware_watchpoint (loc1->owner);
5508 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5509
5510 if (hw_point1 != hw_point2)
5511 return 0;
5512 else if (hw_point1)
5513 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
5514 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
5515 return tracepoint_locations_match (loc1, loc2);
2d134ed3 5516 else
f1310107
TJB
5517 /* We compare bp_location.length in order to cover ranged breakpoints. */
5518 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5519 loc2->pspace->aspace, loc2->address)
5520 && loc1->length == loc2->length);
2d134ed3
PA
5521}
5522
76897487
KB
5523static void
5524breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5525 int bnum, int have_bnum)
5526{
f63fbe86
MS
5527 /* The longest string possibly returned by hex_string_custom
5528 is 50 chars. These must be at least that big for safety. */
5529 char astr1[64];
5530 char astr2[64];
76897487 5531
bb599908
PH
5532 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5533 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5534 if (have_bnum)
8a3fe4f8 5535 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5536 bnum, astr1, astr2);
5537 else
8a3fe4f8 5538 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5539}
5540
4a64f543
MS
5541/* Adjust a breakpoint's address to account for architectural
5542 constraints on breakpoint placement. Return the adjusted address.
5543 Note: Very few targets require this kind of adjustment. For most
5544 targets, this function is simply the identity function. */
76897487
KB
5545
5546static CORE_ADDR
a6d9a66e
UW
5547adjust_breakpoint_address (struct gdbarch *gdbarch,
5548 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5549{
a6d9a66e 5550 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5551 {
5552 /* Very few targets need any kind of breakpoint adjustment. */
5553 return bpaddr;
5554 }
88f7da05
KB
5555 else if (bptype == bp_watchpoint
5556 || bptype == bp_hardware_watchpoint
5557 || bptype == bp_read_watchpoint
5558 || bptype == bp_access_watchpoint
fe798b75 5559 || bptype == bp_catchpoint)
88f7da05
KB
5560 {
5561 /* Watchpoints and the various bp_catch_* eventpoints should not
5562 have their addresses modified. */
5563 return bpaddr;
5564 }
76897487
KB
5565 else
5566 {
5567 CORE_ADDR adjusted_bpaddr;
5568
5569 /* Some targets have architectural constraints on the placement
5570 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5571 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5572
5573 /* An adjusted breakpoint address can significantly alter
5574 a user's expectations. Print a warning if an adjustment
5575 is required. */
5576 if (adjusted_bpaddr != bpaddr)
5577 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5578
5579 return adjusted_bpaddr;
5580 }
5581}
5582
28010a5d
PA
5583void
5584init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5585 struct breakpoint *owner)
7cc221ef 5586{
7cc221ef
DJ
5587 memset (loc, 0, sizeof (*loc));
5588
348d480f
PA
5589 gdb_assert (ops != NULL);
5590
28010a5d
PA
5591 loc->ops = ops;
5592 loc->owner = owner;
511a6cd4 5593 loc->cond = NULL;
0d381245
VP
5594 loc->shlib_disabled = 0;
5595 loc->enabled = 1;
e049a4b5 5596
28010a5d 5597 switch (owner->type)
e049a4b5
DJ
5598 {
5599 case bp_breakpoint:
5600 case bp_until:
5601 case bp_finish:
5602 case bp_longjmp:
5603 case bp_longjmp_resume:
186c406b
TT
5604 case bp_exception:
5605 case bp_exception_resume:
e049a4b5 5606 case bp_step_resume:
2c03e5be 5607 case bp_hp_step_resume:
e049a4b5
DJ
5608 case bp_watchpoint_scope:
5609 case bp_call_dummy:
aa7d318d 5610 case bp_std_terminate:
e049a4b5
DJ
5611 case bp_shlib_event:
5612 case bp_thread_event:
5613 case bp_overlay_event:
4efc6507 5614 case bp_jit_event:
0fd8e87f 5615 case bp_longjmp_master:
aa7d318d 5616 case bp_std_terminate_master:
186c406b 5617 case bp_exception_master:
0e30163f
JK
5618 case bp_gnu_ifunc_resolver:
5619 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5620 loc->loc_type = bp_loc_software_breakpoint;
5621 break;
5622 case bp_hardware_breakpoint:
5623 loc->loc_type = bp_loc_hardware_breakpoint;
5624 break;
5625 case bp_hardware_watchpoint:
5626 case bp_read_watchpoint:
5627 case bp_access_watchpoint:
5628 loc->loc_type = bp_loc_hardware_watchpoint;
5629 break;
5630 case bp_watchpoint:
ce78b96d 5631 case bp_catchpoint:
15c3d785
PA
5632 case bp_tracepoint:
5633 case bp_fast_tracepoint:
0fb4aa4b 5634 case bp_static_tracepoint:
e049a4b5
DJ
5635 loc->loc_type = bp_loc_other;
5636 break;
5637 default:
e2e0b3e5 5638 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5639 }
5640
f431efe5 5641 loc->refc = 1;
28010a5d
PA
5642}
5643
5644/* Allocate a struct bp_location. */
5645
5646static struct bp_location *
5647allocate_bp_location (struct breakpoint *bpt)
5648{
348d480f
PA
5649 return bpt->ops->allocate_location (bpt);
5650}
7cc221ef 5651
f431efe5
PA
5652static void
5653free_bp_location (struct bp_location *loc)
fe3f5fa8 5654{
348d480f 5655 loc->ops->dtor (loc);
fe3f5fa8
VP
5656 xfree (loc);
5657}
5658
f431efe5
PA
5659/* Increment reference count. */
5660
5661static void
5662incref_bp_location (struct bp_location *bl)
5663{
5664 ++bl->refc;
5665}
5666
5667/* Decrement reference count. If the reference count reaches 0,
5668 destroy the bp_location. Sets *BLP to NULL. */
5669
5670static void
5671decref_bp_location (struct bp_location **blp)
5672{
0807b50c
PA
5673 gdb_assert ((*blp)->refc > 0);
5674
f431efe5
PA
5675 if (--(*blp)->refc == 0)
5676 free_bp_location (*blp);
5677 *blp = NULL;
5678}
5679
346774a9 5680/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 5681
346774a9
PA
5682static void
5683add_to_breakpoint_chain (struct breakpoint *b)
c906108c 5684{
346774a9 5685 struct breakpoint *b1;
c906108c 5686
346774a9
PA
5687 /* Add this breakpoint to the end of the chain so that a list of
5688 breakpoints will come out in order of increasing numbers. */
5689
5690 b1 = breakpoint_chain;
5691 if (b1 == 0)
5692 breakpoint_chain = b;
5693 else
5694 {
5695 while (b1->next)
5696 b1 = b1->next;
5697 b1->next = b;
5698 }
5699}
5700
5701/* Initializes breakpoint B with type BPTYPE and no locations yet. */
5702
5703static void
5704init_raw_breakpoint_without_location (struct breakpoint *b,
5705 struct gdbarch *gdbarch,
28010a5d 5706 enum bptype bptype,
c0a91b2b 5707 const struct breakpoint_ops *ops)
346774a9 5708{
c906108c 5709 memset (b, 0, sizeof (*b));
2219d63c 5710
348d480f
PA
5711 gdb_assert (ops != NULL);
5712
28010a5d 5713 b->ops = ops;
4d28f7a8 5714 b->type = bptype;
a6d9a66e 5715 b->gdbarch = gdbarch;
c906108c
SS
5716 b->language = current_language->la_language;
5717 b->input_radix = input_radix;
5718 b->thread = -1;
b5de0fa7 5719 b->enable_state = bp_enabled;
c906108c
SS
5720 b->next = 0;
5721 b->silent = 0;
5722 b->ignore_count = 0;
5723 b->commands = NULL;
818dd999 5724 b->frame_id = null_frame_id;
0d381245 5725 b->condition_not_parsed = 0;
84f4c1fe 5726 b->py_bp_object = NULL;
d0fb5eae 5727 b->related_breakpoint = b;
346774a9
PA
5728}
5729
5730/* Helper to set_raw_breakpoint below. Creates a breakpoint
5731 that has type BPTYPE and has no locations as yet. */
346774a9
PA
5732
5733static struct breakpoint *
5734set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 5735 enum bptype bptype,
c0a91b2b 5736 const struct breakpoint_ops *ops)
346774a9
PA
5737{
5738 struct breakpoint *b = XNEW (struct breakpoint);
5739
348d480f 5740 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 5741 add_to_breakpoint_chain (b);
0d381245
VP
5742 return b;
5743}
5744
0e30163f
JK
5745/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5746 resolutions should be made as the user specified the location explicitly
5747 enough. */
5748
0d381245 5749static void
0e30163f 5750set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5751{
2bdf28a0
JK
5752 gdb_assert (loc->owner != NULL);
5753
0d381245 5754 if (loc->owner->type == bp_breakpoint
1042e4c0 5755 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5756 || is_tracepoint (loc->owner))
0d381245 5757 {
0e30163f
JK
5758 int is_gnu_ifunc;
5759
5760 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5761 NULL, NULL, &is_gnu_ifunc);
5762
5763 if (is_gnu_ifunc && !explicit_loc)
5764 {
5765 struct breakpoint *b = loc->owner;
5766
5767 gdb_assert (loc->pspace == current_program_space);
5768 if (gnu_ifunc_resolve_name (loc->function_name,
5769 &loc->requested_address))
5770 {
5771 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5772 loc->address = adjust_breakpoint_address (loc->gdbarch,
5773 loc->requested_address,
5774 b->type);
5775 }
5776 else if (b->type == bp_breakpoint && b->loc == loc
5777 && loc->next == NULL && b->related_breakpoint == b)
5778 {
5779 /* Create only the whole new breakpoint of this type but do not
5780 mess more complicated breakpoints with multiple locations. */
5781 b->type = bp_gnu_ifunc_resolver;
5782 }
5783 }
5784
0d381245
VP
5785 if (loc->function_name)
5786 loc->function_name = xstrdup (loc->function_name);
5787 }
5788}
5789
a6d9a66e 5790/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 5791struct gdbarch *
a6d9a66e
UW
5792get_sal_arch (struct symtab_and_line sal)
5793{
5794 if (sal.section)
5795 return get_objfile_arch (sal.section->objfile);
5796 if (sal.symtab)
5797 return get_objfile_arch (sal.symtab->objfile);
5798
5799 return NULL;
5800}
5801
346774a9
PA
5802/* Low level routine for partially initializing a breakpoint of type
5803 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 5804 file name, and line number are provided by SAL.
0d381245
VP
5805
5806 It is expected that the caller will complete the initialization of
5807 the newly created breakpoint struct as well as output any status
c56053d2 5808 information regarding the creation of a new breakpoint. */
0d381245 5809
346774a9
PA
5810static void
5811init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 5812 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5813 const struct breakpoint_ops *ops)
0d381245 5814{
28010a5d 5815 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 5816
3742cc8b 5817 add_location_to_breakpoint (b, &sal);
0d381245 5818
6c95b8df
PA
5819 if (bptype != bp_catchpoint)
5820 gdb_assert (sal.pspace != NULL);
5821
6c95b8df
PA
5822 /* Store the program space that was used to set the breakpoint, for
5823 breakpoint resetting. */
5824 b->pspace = sal.pspace;
0d381245
VP
5825
5826 if (sal.symtab == NULL)
5827 b->source_file = NULL;
5828 else
1b36a34b 5829 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5830 b->line_number = sal.line;
5831
c906108c 5832 breakpoints_changed ();
346774a9 5833}
c906108c 5834
346774a9
PA
5835/* set_raw_breakpoint is a low level routine for allocating and
5836 partially initializing a breakpoint of type BPTYPE. The newly
5837 created breakpoint's address, section, source file name, and line
5838 number are provided by SAL. The newly created and partially
5839 initialized breakpoint is added to the breakpoint chain and
5840 is also returned as the value of this function.
5841
5842 It is expected that the caller will complete the initialization of
5843 the newly created breakpoint struct as well as output any status
5844 information regarding the creation of a new breakpoint. In
5845 particular, set_raw_breakpoint does NOT set the breakpoint
5846 number! Care should be taken to not allow an error to occur
5847 prior to completing the initialization of the breakpoint. If this
5848 should happen, a bogus breakpoint will be left on the chain. */
5849
5850struct breakpoint *
5851set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 5852 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5853 const struct breakpoint_ops *ops)
346774a9
PA
5854{
5855 struct breakpoint *b = XNEW (struct breakpoint);
5856
348d480f 5857 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 5858 add_to_breakpoint_chain (b);
c906108c
SS
5859 return b;
5860}
5861
c2c6d25f
JM
5862
5863/* Note that the breakpoint object B describes a permanent breakpoint
5864 instruction, hard-wired into the inferior's code. */
5865void
5866make_breakpoint_permanent (struct breakpoint *b)
5867{
0d381245 5868 struct bp_location *bl;
cc59ec59 5869
b5de0fa7 5870 b->enable_state = bp_permanent;
c2c6d25f 5871
4a64f543
MS
5872 /* By definition, permanent breakpoints are already present in the
5873 code. Mark all locations as inserted. For now,
5874 make_breakpoint_permanent is called in just one place, so it's
5875 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 5876 multiple locations or not, but it's easy to implement. */
0d381245
VP
5877 for (bl = b->loc; bl; bl = bl->next)
5878 bl->inserted = 1;
c2c6d25f
JM
5879}
5880
53a5351d 5881/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5882 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5883 initiated the operation. */
c906108c
SS
5884
5885void
186c406b 5886set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5887{
35df4500 5888 struct breakpoint *b, *b_tmp;
186c406b 5889 int thread = tp->num;
0fd8e87f
UW
5890
5891 /* To avoid having to rescan all objfile symbols at every step,
5892 we maintain a list of continually-inserted but always disabled
5893 longjmp "master" breakpoints. Here, we simply create momentary
5894 clones of those and enable them for the requested thread. */
35df4500 5895 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5896 if (b->pspace == current_program_space
186c406b
TT
5897 && (b->type == bp_longjmp_master
5898 || b->type == bp_exception_master))
0fd8e87f 5899 {
06edf0c0
PA
5900 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5901 struct breakpoint *clone;
cc59ec59 5902
06edf0c0
PA
5903 clone = momentary_breakpoint_from_master (b, type,
5904 &momentary_breakpoint_ops);
0fd8e87f
UW
5905 clone->thread = thread;
5906 }
186c406b
TT
5907
5908 tp->initiating_frame = frame;
c906108c
SS
5909}
5910
611c83ae 5911/* Delete all longjmp breakpoints from THREAD. */
c906108c 5912void
611c83ae 5913delete_longjmp_breakpoint (int thread)
c906108c 5914{
35df4500 5915 struct breakpoint *b, *b_tmp;
c906108c 5916
35df4500 5917 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5918 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5919 {
5920 if (b->thread == thread)
5921 delete_breakpoint (b);
5922 }
c906108c
SS
5923}
5924
1900040c
MS
5925void
5926enable_overlay_breakpoints (void)
5927{
52f0bd74 5928 struct breakpoint *b;
1900040c
MS
5929
5930 ALL_BREAKPOINTS (b)
5931 if (b->type == bp_overlay_event)
5932 {
5933 b->enable_state = bp_enabled;
b60e7edf 5934 update_global_location_list (1);
c02f5703 5935 overlay_events_enabled = 1;
1900040c
MS
5936 }
5937}
5938
5939void
5940disable_overlay_breakpoints (void)
5941{
52f0bd74 5942 struct breakpoint *b;
1900040c
MS
5943
5944 ALL_BREAKPOINTS (b)
5945 if (b->type == bp_overlay_event)
5946 {
5947 b->enable_state = bp_disabled;
b60e7edf 5948 update_global_location_list (0);
c02f5703 5949 overlay_events_enabled = 0;
1900040c
MS
5950 }
5951}
5952
aa7d318d
TT
5953/* Set an active std::terminate breakpoint for each std::terminate
5954 master breakpoint. */
5955void
5956set_std_terminate_breakpoint (void)
5957{
35df4500 5958 struct breakpoint *b, *b_tmp;
aa7d318d 5959
35df4500 5960 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5961 if (b->pspace == current_program_space
5962 && b->type == bp_std_terminate_master)
5963 {
06edf0c0
PA
5964 momentary_breakpoint_from_master (b, bp_std_terminate,
5965 &momentary_breakpoint_ops);
aa7d318d
TT
5966 }
5967}
5968
5969/* Delete all the std::terminate breakpoints. */
5970void
5971delete_std_terminate_breakpoint (void)
5972{
35df4500 5973 struct breakpoint *b, *b_tmp;
aa7d318d 5974
35df4500 5975 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5976 if (b->type == bp_std_terminate)
5977 delete_breakpoint (b);
5978}
5979
c4093a6a 5980struct breakpoint *
a6d9a66e 5981create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
5982{
5983 struct breakpoint *b;
c4093a6a 5984
06edf0c0
PA
5985 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
5986 &internal_breakpoint_ops);
5987
b5de0fa7 5988 b->enable_state = bp_enabled;
c4093a6a 5989 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
5990 b->addr_string
5991 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 5992
b60e7edf 5993 update_global_location_list_nothrow (1);
74960c60 5994
c4093a6a
JM
5995 return b;
5996}
5997
5998void
5999remove_thread_event_breakpoints (void)
6000{
35df4500 6001 struct breakpoint *b, *b_tmp;
c4093a6a 6002
35df4500 6003 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6004 if (b->type == bp_thread_event
6005 && b->loc->pspace == current_program_space)
c4093a6a
JM
6006 delete_breakpoint (b);
6007}
6008
0101ce28
JJ
6009struct lang_and_radix
6010 {
6011 enum language lang;
6012 int radix;
6013 };
6014
4efc6507
DE
6015/* Create a breakpoint for JIT code registration and unregistration. */
6016
6017struct breakpoint *
6018create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6019{
6020 struct breakpoint *b;
6021
06edf0c0
PA
6022 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
6023 &internal_breakpoint_ops);
4efc6507
DE
6024 update_global_location_list_nothrow (1);
6025 return b;
6026}
0101ce28 6027
03673fc7
PP
6028/* Remove JIT code registration and unregistration breakpoint(s). */
6029
6030void
6031remove_jit_event_breakpoints (void)
6032{
6033 struct breakpoint *b, *b_tmp;
6034
6035 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6036 if (b->type == bp_jit_event
6037 && b->loc->pspace == current_program_space)
6038 delete_breakpoint (b);
6039}
6040
cae688ec
JJ
6041void
6042remove_solib_event_breakpoints (void)
6043{
35df4500 6044 struct breakpoint *b, *b_tmp;
cae688ec 6045
35df4500 6046 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6047 if (b->type == bp_shlib_event
6048 && b->loc->pspace == current_program_space)
cae688ec
JJ
6049 delete_breakpoint (b);
6050}
6051
6052struct breakpoint *
a6d9a66e 6053create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6054{
6055 struct breakpoint *b;
6056
06edf0c0
PA
6057 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6058 &internal_breakpoint_ops);
b60e7edf 6059 update_global_location_list_nothrow (1);
cae688ec
JJ
6060 return b;
6061}
6062
6063/* Disable any breakpoints that are on code in shared libraries. Only
6064 apply to enabled breakpoints, disabled ones can just stay disabled. */
6065
6066void
cb851954 6067disable_breakpoints_in_shlibs (void)
cae688ec 6068{
876fa593 6069 struct bp_location *loc, **locp_tmp;
cae688ec 6070
876fa593 6071 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6072 {
2bdf28a0 6073 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6074 struct breakpoint *b = loc->owner;
2bdf28a0 6075
4a64f543
MS
6076 /* We apply the check to all breakpoints, including disabled for
6077 those with loc->duplicate set. This is so that when breakpoint
6078 becomes enabled, or the duplicate is removed, gdb will try to
6079 insert all breakpoints. If we don't set shlib_disabled here,
6080 we'll try to insert those breakpoints and fail. */
1042e4c0 6081 if (((b->type == bp_breakpoint)
508ccb1f 6082 || (b->type == bp_jit_event)
1042e4c0 6083 || (b->type == bp_hardware_breakpoint)
d77f58be 6084 || (is_tracepoint (b)))
6c95b8df 6085 && loc->pspace == current_program_space
0d381245 6086 && !loc->shlib_disabled
a77053c2 6087#ifdef PC_SOLIB
0d381245 6088 && PC_SOLIB (loc->address)
a77053c2 6089#else
6c95b8df 6090 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6091#endif
6092 )
0d381245
VP
6093 {
6094 loc->shlib_disabled = 1;
6095 }
cae688ec
JJ
6096 }
6097}
6098
1e4d1764
YQ
6099/* Disable any breakpoints and tracepoints that are in an unloaded shared
6100 library. Only apply to enabled breakpoints, disabled ones can just stay
4a64f543 6101 disabled. */
84acb35a 6102
75149521 6103static void
84acb35a
JJ
6104disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6105{
876fa593 6106 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6107 int disabled_shlib_breaks = 0;
6108
c86cf029
VP
6109 /* SunOS a.out shared libraries are always mapped, so do not
6110 disable breakpoints; they will only be reported as unloaded
6111 through clear_solib when GDB discards its shared library
6112 list. See clear_solib for more information. */
6113 if (exec_bfd != NULL
6114 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6115 return;
6116
876fa593 6117 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6118 {
2bdf28a0 6119 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6120 struct breakpoint *b = loc->owner;
cc59ec59 6121
1e4d1764 6122 if (solib->pspace == loc->pspace
e2dd7057 6123 && !loc->shlib_disabled
1e4d1764
YQ
6124 && (((b->type == bp_breakpoint
6125 || b->type == bp_jit_event
6126 || b->type == bp_hardware_breakpoint)
6127 && (loc->loc_type == bp_loc_hardware_breakpoint
6128 || loc->loc_type == bp_loc_software_breakpoint))
6129 || is_tracepoint (b))
e2dd7057 6130 && solib_contains_address_p (solib, loc->address))
84acb35a 6131 {
e2dd7057
PP
6132 loc->shlib_disabled = 1;
6133 /* At this point, we cannot rely on remove_breakpoint
6134 succeeding so we must mark the breakpoint as not inserted
6135 to prevent future errors occurring in remove_breakpoints. */
6136 loc->inserted = 0;
8d3788bd
VP
6137
6138 /* This may cause duplicate notifications for the same breakpoint. */
6139 observer_notify_breakpoint_modified (b);
6140
e2dd7057
PP
6141 if (!disabled_shlib_breaks)
6142 {
6143 target_terminal_ours_for_output ();
3e43a32a
MS
6144 warning (_("Temporarily disabling breakpoints "
6145 "for unloaded shared library \"%s\""),
e2dd7057 6146 solib->so_name);
84acb35a 6147 }
e2dd7057 6148 disabled_shlib_breaks = 1;
84acb35a
JJ
6149 }
6150 }
84acb35a
JJ
6151}
6152
ce78b96d
JB
6153/* FORK & VFORK catchpoints. */
6154
e29a4733
PA
6155/* An instance of this type is used to represent a fork or vfork
6156 catchpoint. It includes a "struct breakpoint" as a kind of base
6157 class; users downcast to "struct breakpoint *" when needed. A
6158 breakpoint is really of this type iff its ops pointer points to
6159 CATCH_FORK_BREAKPOINT_OPS. */
6160
6161struct fork_catchpoint
6162{
6163 /* The base class. */
6164 struct breakpoint base;
6165
6166 /* Process id of a child process whose forking triggered this
6167 catchpoint. This field is only valid immediately after this
6168 catchpoint has triggered. */
6169 ptid_t forked_inferior_pid;
6170};
6171
4a64f543
MS
6172/* Implement the "insert" breakpoint_ops method for fork
6173 catchpoints. */
ce78b96d 6174
77b06cd7
TJB
6175static int
6176insert_catch_fork (struct bp_location *bl)
ce78b96d 6177{
77b06cd7 6178 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6179}
6180
4a64f543
MS
6181/* Implement the "remove" breakpoint_ops method for fork
6182 catchpoints. */
ce78b96d
JB
6183
6184static int
77b06cd7 6185remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6186{
6187 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6188}
6189
6190/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6191 catchpoints. */
6192
6193static int
f1310107
TJB
6194breakpoint_hit_catch_fork (const struct bp_location *bl,
6195 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6196{
e29a4733
PA
6197 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6198
6199 return inferior_has_forked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6200}
6201
4a64f543
MS
6202/* Implement the "print_it" breakpoint_ops method for fork
6203 catchpoints. */
ce78b96d
JB
6204
6205static enum print_stop_action
348d480f 6206print_it_catch_fork (bpstat bs)
ce78b96d 6207{
36dfb11c 6208 struct ui_out *uiout = current_uiout;
348d480f
PA
6209 struct breakpoint *b = bs->breakpoint_at;
6210 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 6211
ce78b96d 6212 annotate_catchpoint (b->number);
36dfb11c
TT
6213 if (b->disposition == disp_del)
6214 ui_out_text (uiout, "\nTemporary catchpoint ");
6215 else
6216 ui_out_text (uiout, "\nCatchpoint ");
6217 if (ui_out_is_mi_like_p (uiout))
6218 {
6219 ui_out_field_string (uiout, "reason",
6220 async_reason_lookup (EXEC_ASYNC_FORK));
6221 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6222 }
6223 ui_out_field_int (uiout, "bkptno", b->number);
6224 ui_out_text (uiout, " (forked process ");
6225 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6226 ui_out_text (uiout, "), ");
ce78b96d
JB
6227 return PRINT_SRC_AND_LOC;
6228}
6229
4a64f543
MS
6230/* Implement the "print_one" breakpoint_ops method for fork
6231 catchpoints. */
ce78b96d
JB
6232
6233static void
a6d9a66e 6234print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6235{
e29a4733 6236 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6237 struct value_print_options opts;
79a45e25 6238 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6239
6240 get_user_print_options (&opts);
6241
4a64f543
MS
6242 /* Field 4, the address, is omitted (which makes the columns not
6243 line up too nicely with the headers, but the effect is relatively
6244 readable). */
79a45b7d 6245 if (opts.addressprint)
ce78b96d
JB
6246 ui_out_field_skip (uiout, "addr");
6247 annotate_field (5);
6248 ui_out_text (uiout, "fork");
e29a4733 6249 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6250 {
6251 ui_out_text (uiout, ", process ");
6252 ui_out_field_int (uiout, "what",
e29a4733 6253 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6254 ui_out_spaces (uiout, 1);
6255 }
6256}
6257
6258/* Implement the "print_mention" breakpoint_ops method for fork
6259 catchpoints. */
6260
6261static void
6262print_mention_catch_fork (struct breakpoint *b)
6263{
6264 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6265}
6266
6149aea9
PA
6267/* Implement the "print_recreate" breakpoint_ops method for fork
6268 catchpoints. */
6269
6270static void
6271print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6272{
6273 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 6274 print_recreate_thread (b, fp);
6149aea9
PA
6275}
6276
ce78b96d
JB
6277/* The breakpoint_ops structure to be used in fork catchpoints. */
6278
2060206e 6279static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 6280
4a64f543
MS
6281/* Implement the "insert" breakpoint_ops method for vfork
6282 catchpoints. */
ce78b96d 6283
77b06cd7
TJB
6284static int
6285insert_catch_vfork (struct bp_location *bl)
ce78b96d 6286{
77b06cd7 6287 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6288}
6289
4a64f543
MS
6290/* Implement the "remove" breakpoint_ops method for vfork
6291 catchpoints. */
ce78b96d
JB
6292
6293static int
77b06cd7 6294remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6295{
6296 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6297}
6298
6299/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6300 catchpoints. */
6301
6302static int
f1310107
TJB
6303breakpoint_hit_catch_vfork (const struct bp_location *bl,
6304 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6305{
e29a4733
PA
6306 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6307
6308 return inferior_has_vforked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6309}
6310
4a64f543
MS
6311/* Implement the "print_it" breakpoint_ops method for vfork
6312 catchpoints. */
ce78b96d
JB
6313
6314static enum print_stop_action
348d480f 6315print_it_catch_vfork (bpstat bs)
ce78b96d 6316{
36dfb11c 6317 struct ui_out *uiout = current_uiout;
348d480f 6318 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
6319 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6320
ce78b96d 6321 annotate_catchpoint (b->number);
36dfb11c
TT
6322 if (b->disposition == disp_del)
6323 ui_out_text (uiout, "\nTemporary catchpoint ");
6324 else
6325 ui_out_text (uiout, "\nCatchpoint ");
6326 if (ui_out_is_mi_like_p (uiout))
6327 {
6328 ui_out_field_string (uiout, "reason",
6329 async_reason_lookup (EXEC_ASYNC_VFORK));
6330 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6331 }
6332 ui_out_field_int (uiout, "bkptno", b->number);
6333 ui_out_text (uiout, " (vforked process ");
6334 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6335 ui_out_text (uiout, "), ");
ce78b96d
JB
6336 return PRINT_SRC_AND_LOC;
6337}
6338
4a64f543
MS
6339/* Implement the "print_one" breakpoint_ops method for vfork
6340 catchpoints. */
ce78b96d
JB
6341
6342static void
a6d9a66e 6343print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6344{
e29a4733 6345 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6346 struct value_print_options opts;
79a45e25 6347 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6348
6349 get_user_print_options (&opts);
4a64f543
MS
6350 /* Field 4, the address, is omitted (which makes the columns not
6351 line up too nicely with the headers, but the effect is relatively
6352 readable). */
79a45b7d 6353 if (opts.addressprint)
ce78b96d
JB
6354 ui_out_field_skip (uiout, "addr");
6355 annotate_field (5);
6356 ui_out_text (uiout, "vfork");
e29a4733 6357 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6358 {
6359 ui_out_text (uiout, ", process ");
6360 ui_out_field_int (uiout, "what",
e29a4733 6361 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6362 ui_out_spaces (uiout, 1);
6363 }
6364}
6365
6366/* Implement the "print_mention" breakpoint_ops method for vfork
6367 catchpoints. */
6368
6369static void
6370print_mention_catch_vfork (struct breakpoint *b)
6371{
6372 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6373}
6374
6149aea9
PA
6375/* Implement the "print_recreate" breakpoint_ops method for vfork
6376 catchpoints. */
6377
6378static void
6379print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6380{
6381 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 6382 print_recreate_thread (b, fp);
6149aea9
PA
6383}
6384
ce78b96d
JB
6385/* The breakpoint_ops structure to be used in vfork catchpoints. */
6386
2060206e 6387static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 6388
be5c67c1
PA
6389/* An instance of this type is used to represent a syscall catchpoint.
6390 It includes a "struct breakpoint" as a kind of base class; users
6391 downcast to "struct breakpoint *" when needed. A breakpoint is
6392 really of this type iff its ops pointer points to
6393 CATCH_SYSCALL_BREAKPOINT_OPS. */
6394
6395struct syscall_catchpoint
6396{
6397 /* The base class. */
6398 struct breakpoint base;
6399
6400 /* Syscall numbers used for the 'catch syscall' feature. If no
6401 syscall has been specified for filtering, its value is NULL.
6402 Otherwise, it holds a list of all syscalls to be caught. The
6403 list elements are allocated with xmalloc. */
6404 VEC(int) *syscalls_to_be_caught;
6405};
6406
6407/* Implement the "dtor" breakpoint_ops method for syscall
6408 catchpoints. */
6409
6410static void
6411dtor_catch_syscall (struct breakpoint *b)
6412{
6413 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6414
6415 VEC_free (int, c->syscalls_to_be_caught);
348d480f 6416
2060206e 6417 base_breakpoint_ops.dtor (b);
be5c67c1
PA
6418}
6419
a96d9b2e
SDJ
6420/* Implement the "insert" breakpoint_ops method for syscall
6421 catchpoints. */
6422
77b06cd7
TJB
6423static int
6424insert_catch_syscall (struct bp_location *bl)
a96d9b2e 6425{
be5c67c1 6426 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6427 struct inferior *inf = current_inferior ();
6428
6429 ++inf->total_syscalls_count;
be5c67c1 6430 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6431 ++inf->any_syscall_count;
6432 else
6433 {
6434 int i, iter;
cc59ec59 6435
a96d9b2e 6436 for (i = 0;
be5c67c1 6437 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6438 i++)
6439 {
6440 int elem;
cc59ec59 6441
a96d9b2e
SDJ
6442 if (iter >= VEC_length (int, inf->syscalls_counts))
6443 {
6444 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6445 uintptr_t vec_addr_offset
6446 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6447 uintptr_t vec_addr;
6448 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6449 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6450 vec_addr_offset;
6451 memset ((void *) vec_addr, 0,
6452 (iter + 1 - old_size) * sizeof (int));
6453 }
6454 elem = VEC_index (int, inf->syscalls_counts, iter);
6455 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6456 }
6457 }
6458
77b06cd7
TJB
6459 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6460 inf->total_syscalls_count != 0,
6461 inf->any_syscall_count,
6462 VEC_length (int, inf->syscalls_counts),
6463 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6464}
6465
6466/* Implement the "remove" breakpoint_ops method for syscall
6467 catchpoints. */
6468
6469static int
77b06cd7 6470remove_catch_syscall (struct bp_location *bl)
a96d9b2e 6471{
be5c67c1 6472 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6473 struct inferior *inf = current_inferior ();
6474
6475 --inf->total_syscalls_count;
be5c67c1 6476 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6477 --inf->any_syscall_count;
6478 else
6479 {
6480 int i, iter;
cc59ec59 6481
a96d9b2e 6482 for (i = 0;
be5c67c1 6483 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6484 i++)
6485 {
6486 int elem;
6487 if (iter >= VEC_length (int, inf->syscalls_counts))
6488 /* Shouldn't happen. */
6489 continue;
6490 elem = VEC_index (int, inf->syscalls_counts, iter);
6491 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6492 }
6493 }
6494
6495 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6496 inf->total_syscalls_count != 0,
6497 inf->any_syscall_count,
6498 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6499 VEC_address (int,
6500 inf->syscalls_counts));
a96d9b2e
SDJ
6501}
6502
6503/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6504 catchpoints. */
6505
6506static int
f1310107
TJB
6507breakpoint_hit_catch_syscall (const struct bp_location *bl,
6508 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6509{
4a64f543
MS
6510 /* We must check if we are catching specific syscalls in this
6511 breakpoint. If we are, then we must guarantee that the called
6512 syscall is the same syscall we are catching. */
a96d9b2e 6513 int syscall_number = 0;
be5c67c1
PA
6514 const struct syscall_catchpoint *c
6515 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6516
6517 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6518 return 0;
6519
6520 /* Now, checking if the syscall is the same. */
be5c67c1 6521 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6522 {
6523 int i, iter;
cc59ec59 6524
a96d9b2e 6525 for (i = 0;
be5c67c1 6526 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6527 i++)
6528 if (syscall_number == iter)
6529 break;
6530 /* Not the same. */
6531 if (!iter)
6532 return 0;
6533 }
6534
6535 return 1;
6536}
6537
6538/* Implement the "print_it" breakpoint_ops method for syscall
6539 catchpoints. */
6540
6541static enum print_stop_action
348d480f 6542print_it_catch_syscall (bpstat bs)
a96d9b2e 6543{
36dfb11c 6544 struct ui_out *uiout = current_uiout;
348d480f 6545 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
6546 /* These are needed because we want to know in which state a
6547 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6548 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6549 must print "called syscall" or "returned from syscall". */
6550 ptid_t ptid;
6551 struct target_waitstatus last;
6552 struct syscall s;
a96d9b2e
SDJ
6553 char *syscall_id;
6554
6555 get_last_target_status (&ptid, &last);
6556
6557 get_syscall_by_number (last.value.syscall_number, &s);
6558
6559 annotate_catchpoint (b->number);
6560
36dfb11c
TT
6561 if (b->disposition == disp_del)
6562 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 6563 else
36dfb11c
TT
6564 ui_out_text (uiout, "\nCatchpoint ");
6565 if (ui_out_is_mi_like_p (uiout))
6566 {
6567 ui_out_field_string (uiout, "reason",
6568 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
6569 ? EXEC_ASYNC_SYSCALL_ENTRY
6570 : EXEC_ASYNC_SYSCALL_RETURN));
6571 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6572 }
6573 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
6574
6575 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
6576 ui_out_text (uiout, " (call to syscall ");
6577 else
6578 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 6579
36dfb11c
TT
6580 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
6581 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
6582 if (s.name != NULL)
6583 ui_out_field_string (uiout, "syscall-name", s.name);
6584
6585 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
6586
6587 return PRINT_SRC_AND_LOC;
6588}
6589
6590/* Implement the "print_one" breakpoint_ops method for syscall
6591 catchpoints. */
6592
6593static void
6594print_one_catch_syscall (struct breakpoint *b,
f1310107 6595 struct bp_location **last_loc)
a96d9b2e 6596{
be5c67c1 6597 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 6598 struct value_print_options opts;
79a45e25 6599 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
6600
6601 get_user_print_options (&opts);
4a64f543
MS
6602 /* Field 4, the address, is omitted (which makes the columns not
6603 line up too nicely with the headers, but the effect is relatively
6604 readable). */
a96d9b2e
SDJ
6605 if (opts.addressprint)
6606 ui_out_field_skip (uiout, "addr");
6607 annotate_field (5);
6608
be5c67c1
PA
6609 if (c->syscalls_to_be_caught
6610 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6611 ui_out_text (uiout, "syscalls \"");
6612 else
6613 ui_out_text (uiout, "syscall \"");
6614
be5c67c1 6615 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6616 {
6617 int i, iter;
6618 char *text = xstrprintf ("%s", "");
cc59ec59 6619
a96d9b2e 6620 for (i = 0;
be5c67c1 6621 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6622 i++)
6623 {
6624 char *x = text;
6625 struct syscall s;
6626 get_syscall_by_number (iter, &s);
6627
6628 if (s.name != NULL)
6629 text = xstrprintf ("%s%s, ", text, s.name);
6630 else
6631 text = xstrprintf ("%s%d, ", text, iter);
6632
6633 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 6634 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
6635 on every call. */
6636 xfree (x);
6637 }
6638 /* Remove the last comma. */
6639 text[strlen (text) - 2] = '\0';
6640 ui_out_field_string (uiout, "what", text);
6641 }
6642 else
6643 ui_out_field_string (uiout, "what", "<any syscall>");
6644 ui_out_text (uiout, "\" ");
6645}
6646
6647/* Implement the "print_mention" breakpoint_ops method for syscall
6648 catchpoints. */
6649
6650static void
6651print_mention_catch_syscall (struct breakpoint *b)
6652{
be5c67c1
PA
6653 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6654
6655 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6656 {
6657 int i, iter;
6658
be5c67c1 6659 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6660 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6661 else
6662 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6663
6664 for (i = 0;
be5c67c1 6665 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6666 i++)
6667 {
6668 struct syscall s;
6669 get_syscall_by_number (iter, &s);
6670
6671 if (s.name)
6672 printf_filtered (" '%s' [%d]", s.name, s.number);
6673 else
6674 printf_filtered (" %d", s.number);
6675 }
6676 printf_filtered (")");
6677 }
6678 else
6679 printf_filtered (_("Catchpoint %d (any syscall)"),
6680 b->number);
6681}
6682
6149aea9
PA
6683/* Implement the "print_recreate" breakpoint_ops method for syscall
6684 catchpoints. */
6685
6686static void
6687print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6688{
be5c67c1
PA
6689 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6690
6149aea9
PA
6691 fprintf_unfiltered (fp, "catch syscall");
6692
be5c67c1 6693 if (c->syscalls_to_be_caught)
6149aea9
PA
6694 {
6695 int i, iter;
6696
6697 for (i = 0;
be5c67c1 6698 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
6699 i++)
6700 {
6701 struct syscall s;
6702
6703 get_syscall_by_number (iter, &s);
6704 if (s.name)
6705 fprintf_unfiltered (fp, " %s", s.name);
6706 else
6707 fprintf_unfiltered (fp, " %d", s.number);
6708 }
6709 }
d9b3f62e 6710 print_recreate_thread (b, fp);
6149aea9
PA
6711}
6712
a96d9b2e
SDJ
6713/* The breakpoint_ops structure to be used in syscall catchpoints. */
6714
2060206e 6715static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
6716
6717/* Returns non-zero if 'b' is a syscall catchpoint. */
6718
6719static int
6720syscall_catchpoint_p (struct breakpoint *b)
6721{
6722 return (b->ops == &catch_syscall_breakpoint_ops);
6723}
6724
346774a9
PA
6725/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
6726 is non-zero, then make the breakpoint temporary. If COND_STRING is
6727 not NULL, then store it in the breakpoint. OPS, if not NULL, is
6728 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 6729
346774a9
PA
6730static void
6731init_catchpoint (struct breakpoint *b,
6732 struct gdbarch *gdbarch, int tempflag,
6733 char *cond_string,
c0a91b2b 6734 const struct breakpoint_ops *ops)
c906108c 6735{
c5aa993b 6736 struct symtab_and_line sal;
346774a9 6737
fe39c653 6738 init_sal (&sal);
6c95b8df 6739 sal.pspace = current_program_space;
c5aa993b 6740
28010a5d 6741 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 6742
1b36a34b 6743 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 6744 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
6745}
6746
28010a5d 6747void
3ea46bff 6748install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
6749{
6750 add_to_breakpoint_chain (b);
3a5c3e22
PA
6751 set_breakpoint_number (internal, b);
6752 if (!internal)
6753 mention (b);
c56053d2 6754 observer_notify_breakpoint_created (b);
3ea46bff
YQ
6755
6756 if (update_gll)
6757 update_global_location_list (1);
c56053d2
PA
6758}
6759
9b70b993 6760static void
a6d9a66e
UW
6761create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6762 int tempflag, char *cond_string,
c0a91b2b 6763 const struct breakpoint_ops *ops)
c906108c 6764{
e29a4733 6765 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 6766
e29a4733
PA
6767 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
6768
6769 c->forked_inferior_pid = null_ptid;
6770
3ea46bff 6771 install_breakpoint (0, &c->base, 1);
c906108c
SS
6772}
6773
fe798b75
JB
6774/* Exec catchpoints. */
6775
b4d90040
PA
6776/* An instance of this type is used to represent an exec catchpoint.
6777 It includes a "struct breakpoint" as a kind of base class; users
6778 downcast to "struct breakpoint *" when needed. A breakpoint is
6779 really of this type iff its ops pointer points to
6780 CATCH_EXEC_BREAKPOINT_OPS. */
6781
6782struct exec_catchpoint
6783{
6784 /* The base class. */
6785 struct breakpoint base;
6786
6787 /* Filename of a program whose exec triggered this catchpoint.
6788 This field is only valid immediately after this catchpoint has
6789 triggered. */
6790 char *exec_pathname;
6791};
6792
6793/* Implement the "dtor" breakpoint_ops method for exec
6794 catchpoints. */
6795
6796static void
6797dtor_catch_exec (struct breakpoint *b)
6798{
6799 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6800
6801 xfree (c->exec_pathname);
348d480f 6802
2060206e 6803 base_breakpoint_ops.dtor (b);
b4d90040
PA
6804}
6805
77b06cd7
TJB
6806static int
6807insert_catch_exec (struct bp_location *bl)
c906108c 6808{
77b06cd7 6809 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6810}
c906108c 6811
fe798b75 6812static int
77b06cd7 6813remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6814{
6815 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6816}
c906108c 6817
fe798b75 6818static int
f1310107
TJB
6819breakpoint_hit_catch_exec (const struct bp_location *bl,
6820 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6821{
b4d90040
PA
6822 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
6823
6824 return inferior_has_execd (inferior_ptid, &c->exec_pathname);
fe798b75 6825}
c906108c 6826
fe798b75 6827static enum print_stop_action
348d480f 6828print_it_catch_exec (bpstat bs)
fe798b75 6829{
36dfb11c 6830 struct ui_out *uiout = current_uiout;
348d480f 6831 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
6832 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6833
fe798b75 6834 annotate_catchpoint (b->number);
36dfb11c
TT
6835 if (b->disposition == disp_del)
6836 ui_out_text (uiout, "\nTemporary catchpoint ");
6837 else
6838 ui_out_text (uiout, "\nCatchpoint ");
6839 if (ui_out_is_mi_like_p (uiout))
6840 {
6841 ui_out_field_string (uiout, "reason",
6842 async_reason_lookup (EXEC_ASYNC_EXEC));
6843 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6844 }
6845 ui_out_field_int (uiout, "bkptno", b->number);
6846 ui_out_text (uiout, " (exec'd ");
6847 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
6848 ui_out_text (uiout, "), ");
6849
fe798b75 6850 return PRINT_SRC_AND_LOC;
c906108c
SS
6851}
6852
fe798b75 6853static void
a6d9a66e 6854print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 6855{
b4d90040 6856 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 6857 struct value_print_options opts;
79a45e25 6858 struct ui_out *uiout = current_uiout;
fe798b75
JB
6859
6860 get_user_print_options (&opts);
6861
6862 /* Field 4, the address, is omitted (which makes the columns
6863 not line up too nicely with the headers, but the effect
6864 is relatively readable). */
6865 if (opts.addressprint)
6866 ui_out_field_skip (uiout, "addr");
6867 annotate_field (5);
6868 ui_out_text (uiout, "exec");
b4d90040 6869 if (c->exec_pathname != NULL)
fe798b75
JB
6870 {
6871 ui_out_text (uiout, ", program \"");
b4d90040 6872 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
6873 ui_out_text (uiout, "\" ");
6874 }
6875}
6876
6877static void
6878print_mention_catch_exec (struct breakpoint *b)
6879{
6880 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6881}
6882
6149aea9
PA
6883/* Implement the "print_recreate" breakpoint_ops method for exec
6884 catchpoints. */
6885
6886static void
6887print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6888{
6889 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 6890 print_recreate_thread (b, fp);
6149aea9
PA
6891}
6892
2060206e 6893static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 6894
a96d9b2e
SDJ
6895static void
6896create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 6897 const struct breakpoint_ops *ops)
a96d9b2e 6898{
be5c67c1 6899 struct syscall_catchpoint *c;
a96d9b2e 6900 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 6901
be5c67c1
PA
6902 c = XNEW (struct syscall_catchpoint);
6903 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
6904 c->syscalls_to_be_caught = filter;
a96d9b2e 6905
3ea46bff 6906 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
6907}
6908
c906108c 6909static int
fba45db2 6910hw_breakpoint_used_count (void)
c906108c 6911{
c906108c 6912 int i = 0;
f1310107
TJB
6913 struct breakpoint *b;
6914 struct bp_location *bl;
c906108c
SS
6915
6916 ALL_BREAKPOINTS (b)
c5aa993b 6917 {
d6b74ac4 6918 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
6919 for (bl = b->loc; bl; bl = bl->next)
6920 {
6921 /* Special types of hardware breakpoints may use more than
6922 one register. */
348d480f 6923 i += b->ops->resources_needed (bl);
f1310107 6924 }
c5aa993b 6925 }
c906108c
SS
6926
6927 return i;
6928}
6929
a1398e0c
PA
6930/* Returns the resources B would use if it were a hardware
6931 watchpoint. */
6932
c906108c 6933static int
a1398e0c 6934hw_watchpoint_use_count (struct breakpoint *b)
c906108c 6935{
c906108c 6936 int i = 0;
e09342b5 6937 struct bp_location *bl;
c906108c 6938
a1398e0c
PA
6939 if (!breakpoint_enabled (b))
6940 return 0;
6941
6942 for (bl = b->loc; bl; bl = bl->next)
6943 {
6944 /* Special types of hardware watchpoints may use more than
6945 one register. */
6946 i += b->ops->resources_needed (bl);
6947 }
6948
6949 return i;
6950}
6951
6952/* Returns the sum the used resources of all hardware watchpoints of
6953 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
6954 the sum of the used resources of all hardware watchpoints of other
6955 types _not_ TYPE. */
6956
6957static int
6958hw_watchpoint_used_count_others (struct breakpoint *except,
6959 enum bptype type, int *other_type_used)
6960{
6961 int i = 0;
6962 struct breakpoint *b;
6963
c906108c
SS
6964 *other_type_used = 0;
6965 ALL_BREAKPOINTS (b)
e09342b5 6966 {
a1398e0c
PA
6967 if (b == except)
6968 continue;
e09342b5
TJB
6969 if (!breakpoint_enabled (b))
6970 continue;
6971
a1398e0c
PA
6972 if (b->type == type)
6973 i += hw_watchpoint_use_count (b);
6974 else if (is_hardware_watchpoint (b))
6975 *other_type_used = 1;
e09342b5
TJB
6976 }
6977
c906108c
SS
6978 return i;
6979}
6980
c906108c 6981void
fba45db2 6982disable_watchpoints_before_interactive_call_start (void)
c906108c 6983{
c5aa993b 6984 struct breakpoint *b;
c906108c
SS
6985
6986 ALL_BREAKPOINTS (b)
c5aa993b 6987 {
cc60f2e3 6988 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6989 {
b5de0fa7 6990 b->enable_state = bp_call_disabled;
b60e7edf 6991 update_global_location_list (0);
c5aa993b
JM
6992 }
6993 }
c906108c
SS
6994}
6995
6996void
fba45db2 6997enable_watchpoints_after_interactive_call_stop (void)
c906108c 6998{
c5aa993b 6999 struct breakpoint *b;
c906108c
SS
7000
7001 ALL_BREAKPOINTS (b)
c5aa993b 7002 {
cc60f2e3 7003 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 7004 {
b5de0fa7 7005 b->enable_state = bp_enabled;
b60e7edf 7006 update_global_location_list (1);
c5aa993b
JM
7007 }
7008 }
c906108c
SS
7009}
7010
8bea4e01
UW
7011void
7012disable_breakpoints_before_startup (void)
7013{
7014 struct breakpoint *b;
7015 int found = 0;
7016
7017 ALL_BREAKPOINTS (b)
7018 {
6c95b8df
PA
7019 if (b->pspace != current_program_space)
7020 continue;
7021
8bea4e01
UW
7022 if ((b->type == bp_breakpoint
7023 || b->type == bp_hardware_breakpoint)
7024 && breakpoint_enabled (b))
7025 {
7026 b->enable_state = bp_startup_disabled;
7027 found = 1;
7028 }
7029 }
7030
7031 if (found)
7032 update_global_location_list (0);
7033
6c95b8df 7034 current_program_space->executing_startup = 1;
8bea4e01
UW
7035}
7036
7037void
7038enable_breakpoints_after_startup (void)
7039{
7040 struct breakpoint *b;
7041 int found = 0;
7042
6c95b8df 7043 current_program_space->executing_startup = 0;
8bea4e01
UW
7044
7045 ALL_BREAKPOINTS (b)
7046 {
6c95b8df
PA
7047 if (b->pspace != current_program_space)
7048 continue;
7049
8bea4e01
UW
7050 if ((b->type == bp_breakpoint
7051 || b->type == bp_hardware_breakpoint)
7052 && b->enable_state == bp_startup_disabled)
7053 {
7054 b->enable_state = bp_enabled;
7055 found = 1;
7056 }
7057 }
7058
7059 if (found)
7060 breakpoint_re_set ();
7061}
7062
c906108c
SS
7063
7064/* Set a breakpoint that will evaporate an end of command
7065 at address specified by SAL.
7066 Restrict it to frame FRAME if FRAME is nonzero. */
7067
7068struct breakpoint *
a6d9a66e
UW
7069set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7070 struct frame_id frame_id, enum bptype type)
c906108c 7071{
52f0bd74 7072 struct breakpoint *b;
edb3359d
DJ
7073
7074 /* If FRAME_ID is valid, it should be a real frame, not an inlined
7075 one. */
7076 gdb_assert (!frame_id_inlined_p (frame_id));
7077
06edf0c0 7078 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
7079 b->enable_state = bp_enabled;
7080 b->disposition = disp_donttouch;
818dd999 7081 b->frame_id = frame_id;
c906108c 7082
4a64f543
MS
7083 /* If we're debugging a multi-threaded program, then we want
7084 momentary breakpoints to be active in only a single thread of
7085 control. */
39f77062
KB
7086 if (in_thread_list (inferior_ptid))
7087 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 7088
b60e7edf 7089 update_global_location_list_nothrow (1);
74960c60 7090
c906108c
SS
7091 return b;
7092}
611c83ae 7093
06edf0c0
PA
7094/* Make a momentary breakpoint based on the master breakpoint ORIG.
7095 The new breakpoint will have type TYPE, and use OPS as it
7096 breakpoint_ops. */
e58b0e63 7097
06edf0c0
PA
7098static struct breakpoint *
7099momentary_breakpoint_from_master (struct breakpoint *orig,
7100 enum bptype type,
c0a91b2b 7101 const struct breakpoint_ops *ops)
e58b0e63
PA
7102{
7103 struct breakpoint *copy;
7104
06edf0c0 7105 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 7106 copy->loc = allocate_bp_location (copy);
0e30163f 7107 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 7108
a6d9a66e 7109 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7110 copy->loc->requested_address = orig->loc->requested_address;
7111 copy->loc->address = orig->loc->address;
7112 copy->loc->section = orig->loc->section;
6c95b8df 7113 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
7114
7115 if (orig->source_file == NULL)
7116 copy->source_file = NULL;
7117 else
7118 copy->source_file = xstrdup (orig->source_file);
7119
7120 copy->line_number = orig->line_number;
7121 copy->frame_id = orig->frame_id;
7122 copy->thread = orig->thread;
6c95b8df 7123 copy->pspace = orig->pspace;
e58b0e63
PA
7124
7125 copy->enable_state = bp_enabled;
7126 copy->disposition = disp_donttouch;
7127 copy->number = internal_breakpoint_number--;
7128
7129 update_global_location_list_nothrow (0);
7130 return copy;
7131}
7132
06edf0c0
PA
7133/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7134 ORIG is NULL. */
7135
7136struct breakpoint *
7137clone_momentary_breakpoint (struct breakpoint *orig)
7138{
7139 /* If there's nothing to clone, then return nothing. */
7140 if (orig == NULL)
7141 return NULL;
7142
7143 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
7144}
7145
611c83ae 7146struct breakpoint *
a6d9a66e
UW
7147set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7148 enum bptype type)
611c83ae
PA
7149{
7150 struct symtab_and_line sal;
7151
7152 sal = find_pc_line (pc, 0);
7153 sal.pc = pc;
7154 sal.section = find_pc_overlay (pc);
7155 sal.explicit_pc = 1;
7156
a6d9a66e 7157 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7158}
c906108c 7159\f
c5aa993b 7160
c906108c
SS
7161/* Tell the user we have just set a breakpoint B. */
7162
7163static void
fba45db2 7164mention (struct breakpoint *b)
c906108c 7165{
348d480f 7166 b->ops->print_mention (b);
79a45e25 7167 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 7168 return;
c906108c
SS
7169 printf_filtered ("\n");
7170}
c906108c 7171\f
c5aa993b 7172
0d381245 7173static struct bp_location *
39d61571 7174add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7175 const struct symtab_and_line *sal)
7176{
7177 struct bp_location *loc, **tmp;
3742cc8b
YQ
7178 CORE_ADDR adjusted_address;
7179 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
7180
7181 if (loc_gdbarch == NULL)
7182 loc_gdbarch = b->gdbarch;
7183
7184 /* Adjust the breakpoint's address prior to allocating a location.
7185 Once we call allocate_bp_location(), that mostly uninitialized
7186 location will be placed on the location chain. Adjustment of the
7187 breakpoint may cause target_read_memory() to be called and we do
7188 not want its scan of the location chain to find a breakpoint and
7189 location that's only been partially initialized. */
7190 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
7191 sal->pc, b->type);
0d381245 7192
39d61571 7193 loc = allocate_bp_location (b);
0d381245
VP
7194 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7195 ;
7196 *tmp = loc;
3742cc8b 7197
0d381245 7198 loc->requested_address = sal->pc;
3742cc8b 7199 loc->address = adjusted_address;
6c95b8df
PA
7200 loc->pspace = sal->pspace;
7201 gdb_assert (loc->pspace != NULL);
0d381245 7202 loc->section = sal->section;
3742cc8b 7203 loc->gdbarch = loc_gdbarch;
0e30163f
JK
7204 set_breakpoint_location_function (loc,
7205 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7206 return loc;
7207}
514f746b
AR
7208\f
7209
7210/* Return 1 if LOC is pointing to a permanent breakpoint,
7211 return 0 otherwise. */
7212
7213static int
7214bp_loc_is_permanent (struct bp_location *loc)
7215{
7216 int len;
7217 CORE_ADDR addr;
7218 const gdb_byte *brk;
7219 gdb_byte *target_mem;
939c61fa
JK
7220 struct cleanup *cleanup;
7221 int retval = 0;
514f746b
AR
7222
7223 gdb_assert (loc != NULL);
7224
7225 addr = loc->address;
a6d9a66e 7226 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7227
939c61fa
JK
7228 /* Software breakpoints unsupported? */
7229 if (brk == NULL)
7230 return 0;
7231
514f746b
AR
7232 target_mem = alloca (len);
7233
939c61fa
JK
7234 /* Enable the automatic memory restoration from breakpoints while
7235 we read the memory. Otherwise we could say about our temporary
7236 breakpoints they are permanent. */
6c95b8df
PA
7237 cleanup = save_current_space_and_thread ();
7238
7239 switch_to_program_space_and_thread (loc->pspace);
7240 make_show_memory_breakpoints_cleanup (0);
939c61fa 7241
514f746b
AR
7242 if (target_read_memory (loc->address, target_mem, len) == 0
7243 && memcmp (target_mem, brk, len) == 0)
939c61fa 7244 retval = 1;
514f746b 7245
939c61fa
JK
7246 do_cleanups (cleanup);
7247
7248 return retval;
514f746b
AR
7249}
7250
7251
c3f6f71d 7252
018d34a4
VP
7253/* Create a breakpoint with SAL as location. Use ADDR_STRING
7254 as textual description of the location, and COND_STRING
db107f19 7255 as condition expression. */
018d34a4
VP
7256
7257static void
d9b3f62e
PA
7258init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
7259 struct symtabs_and_lines sals, char *addr_string,
7260 char *cond_string,
7261 enum bptype type, enum bpdisp disposition,
7262 int thread, int task, int ignore_count,
c0a91b2b 7263 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e 7264 int enabled, int internal, int display_canonical)
018d34a4 7265{
0d381245 7266 int i;
018d34a4
VP
7267
7268 if (type == bp_hardware_breakpoint)
7269 {
fbbd034e
AS
7270 int target_resources_ok;
7271
7272 i = hw_breakpoint_used_count ();
7273 target_resources_ok =
7274 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7275 i + 1, 0);
7276 if (target_resources_ok == 0)
7277 error (_("No hardware breakpoint support in the target."));
7278 else if (target_resources_ok < 0)
7279 error (_("Hardware breakpoints used exceeds limit."));
7280 }
7281
6c95b8df
PA
7282 gdb_assert (sals.nelts > 0);
7283
0d381245
VP
7284 for (i = 0; i < sals.nelts; ++i)
7285 {
7286 struct symtab_and_line sal = sals.sals[i];
7287 struct bp_location *loc;
7288
7289 if (from_tty)
5af949e3
UW
7290 {
7291 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7292 if (!loc_gdbarch)
7293 loc_gdbarch = gdbarch;
7294
7295 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7296 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7297 }
0d381245
VP
7298
7299 if (i == 0)
7300 {
d9b3f62e 7301 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 7302 b->thread = thread;
4a306c9a 7303 b->task = task;
018d34a4 7304
0d381245
VP
7305 b->cond_string = cond_string;
7306 b->ignore_count = ignore_count;
41447f92 7307 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7308 b->disposition = disposition;
6c95b8df
PA
7309 b->pspace = sals.sals[0].pspace;
7310
0fb4aa4b
PA
7311 if (type == bp_static_tracepoint)
7312 {
d9b3f62e 7313 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
7314 struct static_tracepoint_marker marker;
7315
7316 if (is_marker_spec (addr_string))
7317 {
7318 /* We already know the marker exists, otherwise, we
7319 wouldn't see a sal for it. */
7320 char *p = &addr_string[3];
7321 char *endp;
7322 char *marker_str;
0fb4aa4b 7323
e9cafbcc 7324 p = skip_spaces (p);
0fb4aa4b 7325
e9cafbcc 7326 endp = skip_to_space (p);
0fb4aa4b
PA
7327
7328 marker_str = savestring (p, endp - p);
d9b3f62e 7329 t->static_trace_marker_id = marker_str;
0fb4aa4b 7330
3e43a32a
MS
7331 printf_filtered (_("Probed static tracepoint "
7332 "marker \"%s\"\n"),
d9b3f62e 7333 t->static_trace_marker_id);
0fb4aa4b
PA
7334 }
7335 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7336 {
d9b3f62e 7337 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
7338 release_static_tracepoint_marker (&marker);
7339
3e43a32a
MS
7340 printf_filtered (_("Probed static tracepoint "
7341 "marker \"%s\"\n"),
d9b3f62e 7342 t->static_trace_marker_id);
0fb4aa4b
PA
7343 }
7344 else
3e43a32a
MS
7345 warning (_("Couldn't determine the static "
7346 "tracepoint marker to probe"));
0fb4aa4b
PA
7347 }
7348
6c95b8df 7349 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7350 && (b->type == bp_breakpoint
7351 || b->type == bp_hardware_breakpoint))
7352 b->enable_state = bp_startup_disabled;
7353
0d381245
VP
7354 loc = b->loc;
7355 }
7356 else
018d34a4 7357 {
39d61571 7358 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7359 }
7360
514f746b
AR
7361 if (bp_loc_is_permanent (loc))
7362 make_breakpoint_permanent (b);
7363
0d381245
VP
7364 if (b->cond_string)
7365 {
7366 char *arg = b->cond_string;
d32a6982 7367 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7368 if (*arg)
db107f19 7369 error (_("Garbage %s follows condition"), arg);
018d34a4 7370 }
0d381245 7371 }
018d34a4 7372
56435ebe 7373 b->display_canonical = display_canonical;
018d34a4
VP
7374 if (addr_string)
7375 b->addr_string = addr_string;
7376 else
7377 /* addr_string has to be used or breakpoint_re_set will delete
7378 me. */
5af949e3
UW
7379 b->addr_string
7380 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
d9b3f62e 7381}
018d34a4 7382
d9b3f62e
PA
7383static void
7384create_breakpoint_sal (struct gdbarch *gdbarch,
7385 struct symtabs_and_lines sals, char *addr_string,
7386 char *cond_string,
7387 enum bptype type, enum bpdisp disposition,
7388 int thread, int task, int ignore_count,
c0a91b2b 7389 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e
PA
7390 int enabled, int internal, int display_canonical)
7391{
7392 struct breakpoint *b;
7393 struct cleanup *old_chain;
7394
7395 if (is_tracepoint_type (type))
7396 {
7397 struct tracepoint *t;
7398
7399 t = XCNEW (struct tracepoint);
7400 b = &t->base;
7401 }
7402 else
7403 b = XNEW (struct breakpoint);
7404
7405 old_chain = make_cleanup (xfree, b);
7406
7407 init_breakpoint_sal (b, gdbarch,
7408 sals, addr_string,
7409 cond_string,
7410 type, disposition,
7411 thread, task, ignore_count,
7412 ops, from_tty,
7413 enabled, internal, display_canonical);
7414 discard_cleanups (old_chain);
7415
3ea46bff 7416 install_breakpoint (internal, b, 0);
018d34a4
VP
7417}
7418
ed0616c6
VP
7419/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7420 elements to fill the void space. */
2c0b251b
PA
7421static void
7422remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7423{
7424 int i = index_to_remove+1;
7425 int last_index = sal->nelts-1;
7426
7427 for (;i <= last_index; ++i)
7428 sal->sals[i-1] = sal->sals[i];
7429
7430 --(sal->nelts);
7431}
7432
6c95b8df
PA
7433/* If appropriate, obtains all sals that correspond to the same file
7434 and line as SAL, in all program spaces. Users debugging with IDEs,
7435 will want to set a breakpoint at foo.c:line, and not really care
7436 about program spaces. This is done only if SAL does not have
7437 explicit PC and has line and file information. If we got just a
7438 single expanded sal, return the original.
ed0616c6 7439
6c95b8df
PA
7440 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7441 which the name of enclosing function is different from SAL. This
7442 makes sure that if we have breakpoint originally set in template
7443 instantiation, say foo<int>(), we won't expand SAL to locations at
7444 the same line in all existing instantiations of 'foo'. */
ed0616c6 7445
2c0b251b 7446static struct symtabs_and_lines
ed0616c6
VP
7447expand_line_sal_maybe (struct symtab_and_line sal)
7448{
7449 struct symtabs_and_lines expanded;
7450 CORE_ADDR original_pc = sal.pc;
7451 char *original_function = NULL;
7452 int found;
7453 int i;
6c95b8df 7454 struct cleanup *old_chain;
ed0616c6
VP
7455
7456 /* If we have explicit pc, don't expand.
7457 If we have no line number, we can't expand. */
7458 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7459 {
7460 expanded.nelts = 1;
7461 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7462 expanded.sals[0] = sal;
7463 return expanded;
7464 }
7465
7466 sal.pc = 0;
6c95b8df
PA
7467
7468 old_chain = save_current_space_and_thread ();
7469
7470 switch_to_program_space_and_thread (sal.pspace);
7471
ed0616c6 7472 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7473
7474 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7475 expanded = expand_line_sal (sal);
6c95b8df 7476
ed0616c6
VP
7477 if (expanded.nelts == 1)
7478 {
3dba1c98
JB
7479 /* We had one sal, we got one sal. Return that sal, adjusting it
7480 past the function prologue if necessary. */
ed0616c6
VP
7481 xfree (expanded.sals);
7482 expanded.nelts = 1;
7483 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7484 sal.pc = original_pc;
7485 expanded.sals[0] = sal;
3dba1c98 7486 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7487 do_cleanups (old_chain);
ed0616c6
VP
7488 return expanded;
7489 }
7490
7491 if (!sal.explicit_line)
7492 {
7493 CORE_ADDR func_addr, func_end;
7494 for (i = 0; i < expanded.nelts; ++i)
7495 {
7496 CORE_ADDR pc = expanded.sals[i].pc;
7497 char *this_function;
6c95b8df
PA
7498
7499 /* We need to switch threads as well since we're about to
7500 read memory. */
7501 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7502
ed0616c6
VP
7503 if (find_pc_partial_function (pc, &this_function,
7504 &func_addr, &func_end))
7505 {
059fb39f
PM
7506 if (this_function
7507 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7508 {
7509 remove_sal (&expanded, i);
7510 --i;
7511 }
ed0616c6
VP
7512 }
7513 }
7514 }
059acae7
UW
7515
7516 /* Skip the function prologue if necessary. */
7517 for (i = 0; i < expanded.nelts; ++i)
7518 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7519
6c95b8df
PA
7520 do_cleanups (old_chain);
7521
ed0616c6
VP
7522 if (expanded.nelts <= 1)
7523 {
e5dd4106 7524 /* This is an ugly workaround. If we get zero expanded sals
4a64f543
MS
7525 then something is really wrong. Fix that by returning the
7526 original sal. */
7527
ed0616c6
VP
7528 xfree (expanded.sals);
7529 expanded.nelts = 1;
7530 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7531 sal.pc = original_pc;
7532 expanded.sals[0] = sal;
7533 return expanded;
7534 }
7535
7536 if (original_pc)
7537 {
7538 found = 0;
7539 for (i = 0; i < expanded.nelts; ++i)
7540 if (expanded.sals[i].pc == original_pc)
7541 {
7542 found = 1;
7543 break;
7544 }
7545 gdb_assert (found);
7546 }
7547
7548 return expanded;
7549}
7550
018d34a4
VP
7551/* Add SALS.nelts breakpoints to the breakpoint table. For each
7552 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7553 value. COND_STRING, if not NULL, specified the condition to be
7554 used for all breakpoints. Essentially the only case where
7555 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7556 function. In that case, it's still not possible to specify
7557 separate conditions for different overloaded functions, so
7558 we take just a single condition string.
7559
c3f6f71d 7560 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7561 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7562 array contents). If the function fails (error() is called), the
7563 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7564 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7565
7566static void
8cdf0e15 7567create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2
TT
7568 struct symtabs_and_lines sals,
7569 struct linespec_result *canonical,
8cdf0e15
VP
7570 char *cond_string,
7571 enum bptype type, enum bpdisp disposition,
7572 int thread, int task, int ignore_count,
c0a91b2b 7573 const struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7574 int enabled, int internal)
c906108c 7575{
018d34a4 7576 int i;
cc59ec59 7577
018d34a4 7578 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7579 {
ed0616c6
VP
7580 struct symtabs_and_lines expanded =
7581 expand_line_sal_maybe (sals.sals[i]);
0d381245 7582
7efd8fc2 7583 create_breakpoint_sal (gdbarch, expanded, canonical->canonical[i],
8cdf0e15 7584 cond_string, type, disposition,
84f4c1fe 7585 thread, task, ignore_count, ops,
56435ebe
TT
7586 from_tty, enabled, internal,
7587 canonical->special_display);
c3f6f71d 7588 }
c3f6f71d 7589}
c906108c 7590
9998af43 7591/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7592 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7593 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7594 address strings. ADDRESS points to the end of the SAL.
7595
7596 The array and the line spec strings are allocated on the heap, it is
7597 the caller's responsibility to free them. */
c906108c 7598
b9362cc7 7599static void
c3f6f71d
JM
7600parse_breakpoint_sals (char **address,
7601 struct symtabs_and_lines *sals,
58438ac1 7602 struct linespec_result *canonical)
c3f6f71d
JM
7603{
7604 char *addr_start = *address;
cc59ec59 7605
c3f6f71d 7606 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7607 breakpoint. */
c3f6f71d
JM
7608 if ((*address) == NULL
7609 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 7610 {
1bfeeb0f
JL
7611 /* The last displayed codepoint, if it's valid, is our default breakpoint
7612 address. */
7613 if (last_displayed_sal_is_valid ())
c906108c 7614 {
c3f6f71d 7615 struct symtab_and_line sal;
cc59ec59 7616
4a64f543 7617 init_sal (&sal); /* Initialize to zeroes. */
c3f6f71d 7618 sals->sals = (struct symtab_and_line *)
c906108c 7619 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
7620
7621 /* Set sal's pspace, pc, symtab, and line to the values
7622 corresponding to the last call to print_frame_info. */
7623 get_last_displayed_sal (&sal);
7624 sal.section = find_pc_overlay (sal.pc);
00903456 7625
4a64f543 7626 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
7627 where PC is the last displayed codepoint's address. So
7628 make sure to set sal.explicit_pc to prevent GDB from
7629 trying to expand the list of sals to include all other
7630 instances with the same symtab and line. */
00903456
JK
7631 sal.explicit_pc = 1;
7632
c3f6f71d
JM
7633 sals->sals[0] = sal;
7634 sals->nelts = 1;
c906108c
SS
7635 }
7636 else
8a3fe4f8 7637 error (_("No default breakpoint address now."));
c906108c
SS
7638 }
7639 else
7640 {
c906108c 7641 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7642 current_source_symtab (which is decode_line_1's default).
7643 This should produce the results we want almost all of the
1bfeeb0f
JL
7644 time while leaving the last displayed codepoint pointers
7645 alone.
4a64f543 7646
1aeae86e
AF
7647 ObjC: However, don't match an Objective-C method name which
7648 may have a '+' or '-' succeeded by a '[' */
0378c332 7649
c214a6fd 7650 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 7651
1bfeeb0f 7652 if (last_displayed_sal_is_valid ()
0378c332 7653 && (!cursal.symtab
1aeae86e
AF
7654 || ((strchr ("+-", (*address)[0]) != NULL)
7655 && ((*address)[1] != '['))))
1bfeeb0f
JL
7656 *sals = decode_line_1 (address, 1,
7657 get_last_displayed_symtab (),
7658 get_last_displayed_line (),
7659 canonical);
c906108c 7660 else
0101ce28 7661 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
58438ac1 7662 canonical);
c906108c 7663 }
4a64f543 7664 /* For any SAL that didn't have a canonical string, fill one in. */
7efd8fc2 7665 if (sals->nelts > 0 && canonical->canonical == NULL)
38a714bb 7666 canonical->canonical = xcalloc (sals->nelts, sizeof (char *));
c3f6f71d 7667 if (addr_start != (*address))
c906108c 7668 {
c3f6f71d 7669 int i;
cc59ec59 7670
c3f6f71d 7671 for (i = 0; i < sals->nelts; i++)
c906108c 7672 {
4a64f543 7673 /* Add the string if not present. */
7efd8fc2
TT
7674 if (canonical->canonical[i] == NULL)
7675 canonical->canonical[i] = savestring (addr_start,
7676 (*address) - addr_start);
c906108c
SS
7677 }
7678 }
c3f6f71d 7679}
c906108c 7680
c906108c 7681
c3f6f71d 7682/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7683 inserted as a breakpoint. If it can't throw an error. */
c906108c 7684
b9362cc7 7685static void
23e7acfb 7686breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7687{
7688 int i;
cc59ec59 7689
c3f6f71d 7690 for (i = 0; i < sals->nelts; i++)
ee53e872 7691 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7692}
7693
7a697b8d
SS
7694/* Fast tracepoints may have restrictions on valid locations. For
7695 instance, a fast tracepoint using a jump instead of a trap will
7696 likely have to overwrite more bytes than a trap would, and so can
7697 only be placed where the instruction is longer than the jump, or a
7698 multi-instruction sequence does not have a jump into the middle of
7699 it, etc. */
7700
7701static void
7702check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7703 struct symtabs_and_lines *sals)
7704{
7705 int i, rslt;
7706 struct symtab_and_line *sal;
7707 char *msg;
7708 struct cleanup *old_chain;
7709
7710 for (i = 0; i < sals->nelts; i++)
7711 {
7712 sal = &sals->sals[i];
7713
7714 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7715 NULL, &msg);
7716 old_chain = make_cleanup (xfree, msg);
7717
7718 if (!rslt)
7719 error (_("May not have a fast tracepoint at 0x%s%s"),
7720 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7721
7722 do_cleanups (old_chain);
7723 }
7724}
7725
018d34a4
VP
7726/* Given TOK, a string specification of condition and thread, as
7727 accepted by the 'break' command, extract the condition
7728 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7729 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7730 If no condition is found, *COND_STRING is set to NULL.
7731 If no thread is found, *THREAD is set to -1. */
7732static void
7733find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7734 char **cond_string, int *thread, int *task)
018d34a4
VP
7735{
7736 *cond_string = NULL;
7737 *thread = -1;
7738 while (tok && *tok)
7739 {
7740 char *end_tok;
7741 int toklen;
7742 char *cond_start = NULL;
7743 char *cond_end = NULL;
cc59ec59 7744
e9cafbcc 7745 tok = skip_spaces (tok);
018d34a4 7746
e9cafbcc 7747 end_tok = skip_to_space (tok);
018d34a4
VP
7748
7749 toklen = end_tok - tok;
7750
7751 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7752 {
f7545552
TT
7753 struct expression *expr;
7754
018d34a4 7755 tok = cond_start = end_tok + 1;
f7545552
TT
7756 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7757 xfree (expr);
018d34a4
VP
7758 cond_end = tok;
7759 *cond_string = savestring (cond_start,
7760 cond_end - cond_start);
7761 }
7762 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7763 {
7764 char *tmptok;
7765
7766 tok = end_tok + 1;
7767 tmptok = tok;
7768 *thread = strtol (tok, &tok, 0);
7769 if (tok == tmptok)
7770 error (_("Junk after thread keyword."));
7771 if (!valid_thread_id (*thread))
7772 error (_("Unknown thread %d."), *thread);
7773 }
4a306c9a
JB
7774 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7775 {
7776 char *tmptok;
7777
7778 tok = end_tok + 1;
7779 tmptok = tok;
7780 *task = strtol (tok, &tok, 0);
7781 if (tok == tmptok)
7782 error (_("Junk after task keyword."));
7783 if (!valid_task_id (*task))
b6199126 7784 error (_("Unknown task %d."), *task);
4a306c9a 7785 }
018d34a4
VP
7786 else
7787 error (_("Junk at end of arguments."));
7788 }
7789}
7790
0fb4aa4b
PA
7791/* Decode a static tracepoint marker spec. */
7792
7793static struct symtabs_and_lines
7794decode_static_tracepoint_spec (char **arg_p)
7795{
7796 VEC(static_tracepoint_marker_p) *markers = NULL;
7797 struct symtabs_and_lines sals;
7798 struct symtab_and_line sal;
7799 struct symbol *sym;
7800 struct cleanup *old_chain;
7801 char *p = &(*arg_p)[3];
7802 char *endp;
7803 char *marker_str;
7804 int i;
7805
e9cafbcc 7806 p = skip_spaces (p);
0fb4aa4b 7807
e9cafbcc 7808 endp = skip_to_space (p);
0fb4aa4b
PA
7809
7810 marker_str = savestring (p, endp - p);
7811 old_chain = make_cleanup (xfree, marker_str);
7812
7813 markers = target_static_tracepoint_markers_by_strid (marker_str);
7814 if (VEC_empty(static_tracepoint_marker_p, markers))
7815 error (_("No known static tracepoint marker named %s"), marker_str);
7816
7817 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7818 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7819
7820 for (i = 0; i < sals.nelts; i++)
7821 {
7822 struct static_tracepoint_marker *marker;
7823
7824 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7825
7826 init_sal (&sals.sals[i]);
7827
7828 sals.sals[i] = find_pc_line (marker->address, 0);
7829 sals.sals[i].pc = marker->address;
7830
7831 release_static_tracepoint_marker (marker);
7832 }
7833
7834 do_cleanups (old_chain);
7835
7836 *arg_p = endp;
7837 return sals;
7838}
7839
fd9b8c24
PA
7840/* Set a breakpoint. This function is shared between CLI and MI
7841 functions for setting a breakpoint. This function has two major
7842 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7843 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7844 breakpoint location, address and thread. Otherwise, ARG is just
7845 the location of breakpoint, with condition and thread specified by
7846 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7847 the breakpoint number will be allocated from the internal
7848 breakpoint count. Returns true if any breakpoint was created;
7849 false otherwise. */
0101ce28 7850
8cdf0e15
VP
7851int
7852create_breakpoint (struct gdbarch *gdbarch,
7853 char *arg, char *cond_string, int thread,
7854 int parse_condition_and_thread,
0fb4aa4b 7855 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7856 int ignore_count,
7857 enum auto_boolean pending_break_support,
c0a91b2b 7858 const struct breakpoint_ops *ops,
84f4c1fe 7859 int from_tty, int enabled, int internal)
c3f6f71d 7860{
b78a6381 7861 volatile struct gdb_exception e;
c3f6f71d 7862 struct symtabs_and_lines sals;
0101ce28 7863 struct symtab_and_line pending_sal;
0101ce28 7864 char *copy_arg;
c3f6f71d 7865 char *addr_start = arg;
7efd8fc2 7866 struct linespec_result canonical;
c3f6f71d 7867 struct cleanup *old_chain;
80c99de1 7868 struct cleanup *bkpt_chain = NULL;
05ff989b 7869 int i;
0101ce28 7870 int pending = 0;
4a306c9a 7871 int task = 0;
86b17b60 7872 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7873
348d480f
PA
7874 gdb_assert (ops != NULL);
7875
c3f6f71d
JM
7876 sals.sals = NULL;
7877 sals.nelts = 0;
7efd8fc2 7878 init_linespec_result (&canonical);
c3f6f71d 7879
0fb4aa4b
PA
7880 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7881 {
7882 int i;
7883
7884 sals = decode_static_tracepoint_spec (&arg);
7885
7886 copy_arg = savestring (addr_start, arg - addr_start);
38a714bb 7887 canonical.canonical = xcalloc (sals.nelts, sizeof (char *));
0fb4aa4b 7888 for (i = 0; i < sals.nelts; i++)
7efd8fc2 7889 canonical.canonical[i] = xstrdup (copy_arg);
0fb4aa4b
PA
7890 goto done;
7891 }
7892
b78a6381
TT
7893 TRY_CATCH (e, RETURN_MASK_ALL)
7894 {
58438ac1 7895 parse_breakpoint_sals (&arg, &sals, &canonical);
b78a6381 7896 }
0101ce28
JJ
7897
7898 /* If caller is interested in rc value from parse, set value. */
05ff989b 7899 switch (e.reason)
0101ce28 7900 {
05ff989b 7901 case RETURN_QUIT:
98deb0da 7902 throw_exception (e);
05ff989b
AC
7903 case RETURN_ERROR:
7904 switch (e.error)
0101ce28 7905 {
05ff989b 7906 case NOT_FOUND_ERROR:
0101ce28 7907
05ff989b
AC
7908 /* If pending breakpoint support is turned off, throw
7909 error. */
fa8d40ab
JJ
7910
7911 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7912 throw_exception (e);
7913
7914 exception_print (gdb_stderr, e);
fa8d40ab 7915
05ff989b
AC
7916 /* If pending breakpoint support is auto query and the user
7917 selects no, then simply return the error code. */
059fb39f 7918 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
7919 && !nquery (_("Make %s pending on future shared library load? "),
7920 bptype_string (type_wanted)))
fd9b8c24 7921 return 0;
fa8d40ab 7922
05ff989b
AC
7923 /* At this point, either the user was queried about setting
7924 a pending breakpoint and selected yes, or pending
7925 breakpoint behavior is on and thus a pending breakpoint
7926 is defaulted on behalf of the user. */
0101ce28 7927 copy_arg = xstrdup (addr_start);
7efd8fc2 7928 canonical.canonical = &copy_arg;
0101ce28
JJ
7929 sals.nelts = 1;
7930 sals.sals = &pending_sal;
7931 pending_sal.pc = 0;
7932 pending = 1;
05ff989b
AC
7933 break;
7934 default:
98deb0da 7935 throw_exception (e);
0101ce28 7936 }
2abae994 7937 break;
05ff989b
AC
7938 default:
7939 if (!sals.nelts)
fd9b8c24 7940 return 0;
0101ce28 7941 }
c3f6f71d 7942
0fb4aa4b
PA
7943 done:
7944
4a64f543 7945 /* Create a chain of things that always need to be cleaned up. */
c3f6f71d
JM
7946 old_chain = make_cleanup (null_cleanup, 0);
7947
0101ce28
JJ
7948 if (!pending)
7949 {
7950 /* Make sure that all storage allocated to SALS gets freed. */
7951 make_cleanup (xfree, sals.sals);
7952
7efd8fc2
TT
7953 /* Cleanup the canonical array but not its contents. */
7954 make_cleanup (xfree, canonical.canonical);
0101ce28 7955 }
c3f6f71d 7956
c3f6f71d
JM
7957 /* ----------------------------- SNIP -----------------------------
7958 Anything added to the cleanup chain beyond this point is assumed
7959 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7960 then the memory is not reclaimed. */
7961 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 7962
7efd8fc2 7963 /* Mark the contents of the canonical for cleanup. These go on
80c99de1 7964 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
7965 for (i = 0; i < sals.nelts; i++)
7966 {
7efd8fc2
TT
7967 if (canonical.canonical[i] != NULL)
7968 make_cleanup (xfree, canonical.canonical[i]);
c3f6f71d
JM
7969 }
7970
7971 /* Resolve all line numbers to PC's and verify that the addresses
7972 are ok for the target. */
0101ce28 7973 if (!pending)
23e7acfb 7974 breakpoint_sals_to_pc (&sals);
c3f6f71d 7975
7a697b8d 7976 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 7977 if (!pending && type_wanted == bp_fast_tracepoint)
7a697b8d
SS
7978 check_fast_tracepoint_sals (gdbarch, &sals);
7979
c3f6f71d
JM
7980 /* Verify that condition can be parsed, before setting any
7981 breakpoints. Allocate a separate condition expression for each
4a64f543 7982 breakpoint. */
0101ce28 7983 if (!pending)
c3f6f71d 7984 {
2f069f6f 7985 if (parse_condition_and_thread)
72b2ff0e
VP
7986 {
7987 /* Here we only parse 'arg' to separate condition
7988 from thread number, so parsing in context of first
7989 sal is OK. When setting the breakpoint we'll
7990 re-parse it in context of each sal. */
7991 cond_string = NULL;
7992 thread = -1;
4a306c9a
JB
7993 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
7994 &thread, &task);
72b2ff0e
VP
7995 if (cond_string)
7996 make_cleanup (xfree, cond_string);
7997 }
2f069f6f 7998 else
72b2ff0e
VP
7999 {
8000 /* Create a private copy of condition string. */
8001 if (cond_string)
8002 {
8003 cond_string = xstrdup (cond_string);
8004 make_cleanup (xfree, cond_string);
8005 }
8006 }
0fb4aa4b
PA
8007
8008 /* If the user is creating a static tracepoint by marker id
8009 (strace -m MARKER_ID), then store the sals index, so that
8010 breakpoint_re_set can try to match up which of the newly
8011 found markers corresponds to this one, and, don't try to
8012 expand multiple locations for each sal, given than SALS
8013 already should contain all sals for MARKER_ID. */
8014 if (type_wanted == bp_static_tracepoint
7efd8fc2 8015 && is_marker_spec (canonical.canonical[0]))
0fb4aa4b
PA
8016 {
8017 int i;
8018
8019 for (i = 0; i < sals.nelts; ++i)
8020 {
8021 struct symtabs_and_lines expanded;
d9b3f62e 8022 struct tracepoint *tp;
0fb4aa4b
PA
8023 struct cleanup *old_chain;
8024
8025 expanded.nelts = 1;
8026 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
8027 expanded.sals[0] = sals.sals[i];
8028 old_chain = make_cleanup (xfree, expanded.sals);
8029
d9b3f62e
PA
8030 tp = XCNEW (struct tracepoint);
8031 init_breakpoint_sal (&tp->base, gdbarch, expanded,
8032 canonical.canonical[i],
8033 cond_string, type_wanted,
8034 tempflag ? disp_del : disp_donttouch,
8035 thread, task, ignore_count, ops,
8036 from_tty, enabled, internal,
8037 canonical.special_display);
0fb4aa4b
PA
8038 /* Given that its possible to have multiple markers with
8039 the same string id, if the user is creating a static
8040 tracepoint by marker id ("strace -m MARKER_ID"), then
8041 store the sals index, so that breakpoint_re_set can
8042 try to match up which of the newly found markers
8043 corresponds to this one */
8044 tp->static_trace_marker_id_idx = i;
d9b3f62e 8045
3ea46bff 8046 install_breakpoint (internal, &tp->base, 0);
d9b3f62e
PA
8047
8048 do_cleanups (old_chain);
0fb4aa4b
PA
8049 }
8050 }
8051 else
7efd8fc2 8052 create_breakpoints_sal (gdbarch, sals, &canonical, cond_string,
3e43a32a
MS
8053 type_wanted,
8054 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 8055 thread, task, ignore_count, ops, from_tty,
84f4c1fe 8056 enabled, internal);
c906108c 8057 }
0101ce28
JJ
8058 else
8059 {
0101ce28
JJ
8060 struct breakpoint *b;
8061
0101ce28
JJ
8062 make_cleanup (xfree, copy_arg);
8063
bfccc43c
YQ
8064 if (is_tracepoint_type (type_wanted))
8065 {
8066 struct tracepoint *t;
8067
8068 t = XCNEW (struct tracepoint);
8069 b = &t->base;
8070 }
8071 else
8072 b = XNEW (struct breakpoint);
8073
8074 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
8075
7efd8fc2 8076 b->addr_string = canonical.canonical[0];
72b2ff0e 8077 b->cond_string = NULL;
0101ce28 8078 b->ignore_count = ignore_count;
0101ce28 8079 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 8080 b->condition_not_parsed = 1;
41447f92 8081 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 8082 b->pspace = current_program_space;
74960c60 8083
6c95b8df 8084 if (enabled && b->pspace->executing_startup
8bea4e01
UW
8085 && (b->type == bp_breakpoint
8086 || b->type == bp_hardware_breakpoint))
8087 b->enable_state = bp_startup_disabled;
8088
bfccc43c 8089 install_breakpoint (internal, b, 0);
0101ce28
JJ
8090 }
8091
c3f6f71d 8092 if (sals.nelts > 1)
95a42b64 8093 {
3e43a32a
MS
8094 warning (_("Multiple breakpoints were set.\nUse the "
8095 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 8096 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
8097 }
8098
80c99de1
PA
8099 /* That's it. Discard the cleanups for data inserted into the
8100 breakpoint. */
8101 discard_cleanups (bkpt_chain);
8102 /* But cleanup everything else. */
c3f6f71d 8103 do_cleanups (old_chain);
217dc9e2 8104
80c99de1 8105 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 8106 update_global_location_list (1);
fd9b8c24
PA
8107
8108 return 1;
c3f6f71d 8109}
c906108c 8110
348d480f 8111/* Set a breakpoint.
72b2ff0e
VP
8112 ARG is a string describing breakpoint address,
8113 condition, and thread.
8114 FLAG specifies if a breakpoint is hardware on,
8115 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8116 and BP_TEMPFLAG. */
348d480f 8117
98deb0da 8118static void
72b2ff0e 8119break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 8120{
72b2ff0e 8121 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
8122 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8123 ? bp_hardware_breakpoint
8124 : bp_breakpoint);
c3f6f71d 8125
8cdf0e15
VP
8126 create_breakpoint (get_current_arch (),
8127 arg,
8128 NULL, 0, 1 /* parse arg */,
0fb4aa4b 8129 tempflag, type_wanted,
8cdf0e15
VP
8130 0 /* Ignore count */,
8131 pending_break_support,
348d480f 8132 &bkpt_breakpoint_ops,
8cdf0e15 8133 from_tty,
84f4c1fe
PM
8134 1 /* enabled */,
8135 0 /* internal */);
c906108c
SS
8136}
8137
c906108c
SS
8138/* Helper function for break_command_1 and disassemble_command. */
8139
8140void
fba45db2 8141resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
8142{
8143 CORE_ADDR pc;
8144
8145 if (sal->pc == 0 && sal->symtab != NULL)
8146 {
8147 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 8148 error (_("No line %d in file \"%s\"."),
c906108c
SS
8149 sal->line, sal->symtab->filename);
8150 sal->pc = pc;
6a048695 8151
4a64f543
MS
8152 /* If this SAL corresponds to a breakpoint inserted using a line
8153 number, then skip the function prologue if necessary. */
6a048695 8154 if (sal->explicit_line)
059acae7 8155 skip_prologue_sal (sal);
c906108c
SS
8156 }
8157
8158 if (sal->section == 0 && sal->symtab != NULL)
8159 {
8160 struct blockvector *bv;
c5aa993b
JM
8161 struct block *b;
8162 struct symbol *sym;
c906108c 8163
801e3a5b 8164 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
8165 if (bv != NULL)
8166 {
7f0df278 8167 sym = block_linkage_function (b);
c906108c
SS
8168 if (sym != NULL)
8169 {
8170 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 8171 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
8172 }
8173 else
8174 {
4a64f543
MS
8175 /* It really is worthwhile to have the section, so we'll
8176 just have to look harder. This case can be executed
8177 if we have line numbers but no functions (as can
8178 happen in assembly source). */
c906108c 8179
c5aa993b 8180 struct minimal_symbol *msym;
6c95b8df
PA
8181 struct cleanup *old_chain = save_current_space_and_thread ();
8182
8183 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8184
8185 msym = lookup_minimal_symbol_by_pc (sal->pc);
8186 if (msym)
714835d5 8187 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8188
8189 do_cleanups (old_chain);
c906108c
SS
8190 }
8191 }
8192 }
8193}
8194
8195void
fba45db2 8196break_command (char *arg, int from_tty)
c906108c 8197{
db107f19 8198 break_command_1 (arg, 0, from_tty);
c906108c
SS
8199}
8200
c906108c 8201void
fba45db2 8202tbreak_command (char *arg, int from_tty)
c906108c 8203{
db107f19 8204 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8205}
8206
c906108c 8207static void
fba45db2 8208hbreak_command (char *arg, int from_tty)
c906108c 8209{
db107f19 8210 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8211}
8212
8213static void
fba45db2 8214thbreak_command (char *arg, int from_tty)
c906108c 8215{
db107f19 8216 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8217}
8218
8219static void
fba45db2 8220stop_command (char *arg, int from_tty)
c906108c 8221{
a3f17187 8222 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8223Usage: stop in <function | address>\n\
a3f17187 8224 stop at <line>\n"));
c906108c
SS
8225}
8226
8227static void
fba45db2 8228stopin_command (char *arg, int from_tty)
c906108c
SS
8229{
8230 int badInput = 0;
8231
c5aa993b 8232 if (arg == (char *) NULL)
c906108c
SS
8233 badInput = 1;
8234 else if (*arg != '*')
8235 {
8236 char *argptr = arg;
8237 int hasColon = 0;
8238
4a64f543 8239 /* Look for a ':'. If this is a line number specification, then
53a5351d 8240 say it is bad, otherwise, it should be an address or
4a64f543 8241 function/method name. */
c906108c 8242 while (*argptr && !hasColon)
c5aa993b
JM
8243 {
8244 hasColon = (*argptr == ':');
8245 argptr++;
8246 }
c906108c
SS
8247
8248 if (hasColon)
c5aa993b 8249 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8250 else
c5aa993b 8251 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8252 }
8253
8254 if (badInput)
a3f17187 8255 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8256 else
db107f19 8257 break_command_1 (arg, 0, from_tty);
c906108c
SS
8258}
8259
8260static void
fba45db2 8261stopat_command (char *arg, int from_tty)
c906108c
SS
8262{
8263 int badInput = 0;
8264
c5aa993b 8265 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8266 badInput = 1;
8267 else
8268 {
8269 char *argptr = arg;
8270 int hasColon = 0;
8271
4a64f543
MS
8272 /* Look for a ':'. If there is a '::' then get out, otherwise
8273 it is probably a line number. */
c906108c 8274 while (*argptr && !hasColon)
c5aa993b
JM
8275 {
8276 hasColon = (*argptr == ':');
8277 argptr++;
8278 }
c906108c
SS
8279
8280 if (hasColon)
c5aa993b 8281 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8282 else
c5aa993b 8283 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8284 }
8285
8286 if (badInput)
a3f17187 8287 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8288 else
db107f19 8289 break_command_1 (arg, 0, from_tty);
c906108c
SS
8290}
8291
f1310107
TJB
8292/* Implement the "breakpoint_hit" breakpoint_ops method for
8293 ranged breakpoints. */
8294
8295static int
8296breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8297 struct address_space *aspace,
8298 CORE_ADDR bp_addr)
8299{
8300 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8301 bl->length, aspace, bp_addr);
8302}
8303
8304/* Implement the "resources_needed" breakpoint_ops method for
8305 ranged breakpoints. */
8306
8307static int
8308resources_needed_ranged_breakpoint (const struct bp_location *bl)
8309{
8310 return target_ranged_break_num_registers ();
8311}
8312
8313/* Implement the "print_it" breakpoint_ops method for
8314 ranged breakpoints. */
8315
8316static enum print_stop_action
348d480f 8317print_it_ranged_breakpoint (bpstat bs)
f1310107 8318{
348d480f 8319 struct breakpoint *b = bs->breakpoint_at;
f1310107 8320 struct bp_location *bl = b->loc;
79a45e25 8321 struct ui_out *uiout = current_uiout;
f1310107
TJB
8322
8323 gdb_assert (b->type == bp_hardware_breakpoint);
8324
8325 /* Ranged breakpoints have only one location. */
8326 gdb_assert (bl && bl->next == NULL);
8327
8328 annotate_breakpoint (b->number);
8329 if (b->disposition == disp_del)
8330 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8331 else
8332 ui_out_text (uiout, "\nRanged breakpoint ");
8333 if (ui_out_is_mi_like_p (uiout))
8334 {
8335 ui_out_field_string (uiout, "reason",
8336 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8337 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8338 }
8339 ui_out_field_int (uiout, "bkptno", b->number);
8340 ui_out_text (uiout, ", ");
8341
8342 return PRINT_SRC_AND_LOC;
8343}
8344
8345/* Implement the "print_one" breakpoint_ops method for
8346 ranged breakpoints. */
8347
8348static void
8349print_one_ranged_breakpoint (struct breakpoint *b,
8350 struct bp_location **last_loc)
8351{
8352 struct bp_location *bl = b->loc;
8353 struct value_print_options opts;
79a45e25 8354 struct ui_out *uiout = current_uiout;
f1310107
TJB
8355
8356 /* Ranged breakpoints have only one location. */
8357 gdb_assert (bl && bl->next == NULL);
8358
8359 get_user_print_options (&opts);
8360
8361 if (opts.addressprint)
8362 /* We don't print the address range here, it will be printed later
8363 by print_one_detail_ranged_breakpoint. */
8364 ui_out_field_skip (uiout, "addr");
8365 annotate_field (5);
8366 print_breakpoint_location (b, bl);
8367 *last_loc = bl;
8368}
8369
8370/* Implement the "print_one_detail" breakpoint_ops method for
8371 ranged breakpoints. */
8372
8373static void
8374print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8375 struct ui_out *uiout)
8376{
8377 CORE_ADDR address_start, address_end;
8378 struct bp_location *bl = b->loc;
8379 struct ui_stream *stb = ui_out_stream_new (uiout);
8380 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8381
8382 gdb_assert (bl);
8383
8384 address_start = bl->address;
8385 address_end = address_start + bl->length - 1;
8386
8387 ui_out_text (uiout, "\taddress range: ");
8388 fprintf_unfiltered (stb->stream, "[%s, %s]",
8389 print_core_address (bl->gdbarch, address_start),
8390 print_core_address (bl->gdbarch, address_end));
8391 ui_out_field_stream (uiout, "addr", stb);
8392 ui_out_text (uiout, "\n");
8393
8394 do_cleanups (cleanup);
8395}
8396
8397/* Implement the "print_mention" breakpoint_ops method for
8398 ranged breakpoints. */
8399
8400static void
8401print_mention_ranged_breakpoint (struct breakpoint *b)
8402{
8403 struct bp_location *bl = b->loc;
79a45e25 8404 struct ui_out *uiout = current_uiout;
f1310107
TJB
8405
8406 gdb_assert (bl);
8407 gdb_assert (b->type == bp_hardware_breakpoint);
8408
8409 if (ui_out_is_mi_like_p (uiout))
8410 return;
8411
8412 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8413 b->number, paddress (bl->gdbarch, bl->address),
8414 paddress (bl->gdbarch, bl->address + bl->length - 1));
8415}
8416
8417/* Implement the "print_recreate" breakpoint_ops method for
8418 ranged breakpoints. */
8419
8420static void
8421print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8422{
8423 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8424 b->addr_string_range_end);
d9b3f62e 8425 print_recreate_thread (b, fp);
f1310107
TJB
8426}
8427
8428/* The breakpoint_ops structure to be used in ranged breakpoints. */
8429
2060206e 8430static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
8431
8432/* Find the address where the end of the breakpoint range should be
8433 placed, given the SAL of the end of the range. This is so that if
8434 the user provides a line number, the end of the range is set to the
8435 last instruction of the given line. */
8436
8437static CORE_ADDR
8438find_breakpoint_range_end (struct symtab_and_line sal)
8439{
8440 CORE_ADDR end;
8441
8442 /* If the user provided a PC value, use it. Otherwise,
8443 find the address of the end of the given location. */
8444 if (sal.explicit_pc)
8445 end = sal.pc;
8446 else
8447 {
8448 int ret;
8449 CORE_ADDR start;
8450
8451 ret = find_line_pc_range (sal, &start, &end);
8452 if (!ret)
8453 error (_("Could not find location of the end of the range."));
8454
8455 /* find_line_pc_range returns the start of the next line. */
8456 end--;
8457 }
8458
8459 return end;
8460}
8461
8462/* Implement the "break-range" CLI command. */
8463
8464static void
8465break_range_command (char *arg, int from_tty)
8466{
8467 char *arg_start, *addr_string_start, *addr_string_end;
8468 struct linespec_result canonical_start, canonical_end;
8469 int bp_count, can_use_bp, length;
8470 CORE_ADDR end;
8471 struct breakpoint *b;
8472 struct symtab_and_line sal_start, sal_end;
8473 struct symtabs_and_lines sals_start, sals_end;
8474 struct cleanup *cleanup_bkpt;
8475
8476 /* We don't support software ranged breakpoints. */
8477 if (target_ranged_break_num_registers () < 0)
8478 error (_("This target does not support hardware ranged breakpoints."));
8479
8480 bp_count = hw_breakpoint_used_count ();
8481 bp_count += target_ranged_break_num_registers ();
8482 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8483 bp_count, 0);
8484 if (can_use_bp < 0)
8485 error (_("Hardware breakpoints used exceeds limit."));
8486
8487 if (arg == NULL || arg[0] == '\0')
8488 error(_("No address range specified."));
8489
8490 sals_start.sals = NULL;
8491 sals_start.nelts = 0;
8492 init_linespec_result (&canonical_start);
8493
8494 while (*arg == ' ' || *arg == '\t')
8495 arg++;
8496
58438ac1 8497 parse_breakpoint_sals (&arg, &sals_start, &canonical_start);
f1310107
TJB
8498
8499 sal_start = sals_start.sals[0];
8500 addr_string_start = canonical_start.canonical[0];
8501 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
8502 xfree (sals_start.sals);
8503 xfree (canonical_start.canonical);
8504
8505 if (arg[0] != ',')
8506 error (_("Too few arguments."));
8507 else if (sals_start.nelts == 0)
8508 error (_("Could not find location of the beginning of the range."));
8509 else if (sals_start.nelts != 1)
8510 error (_("Cannot create a ranged breakpoint with multiple locations."));
8511
8512 resolve_sal_pc (&sal_start);
8513
8514 arg++; /* Skip the comma. */
8515 while (*arg == ' ' || *arg == '\t')
8516 arg++;
8517
8518 /* Parse the end location. */
8519
8520 sals_end.sals = NULL;
8521 sals_end.nelts = 0;
8522 init_linespec_result (&canonical_end);
8523 arg_start = arg;
8524
423f41a5 8525 /* We call decode_line_1 directly here instead of using
f1310107
TJB
8526 parse_breakpoint_sals because we need to specify the start location's
8527 symtab and line as the default symtab and line for the end of the
8528 range. This makes it possible to have ranges like "foo.c:27, +14",
8529 where +14 means 14 lines from the start location. */
8530 sals_end = decode_line_1 (&arg, 1, sal_start.symtab, sal_start.line,
58438ac1 8531 &canonical_end);
f1310107
TJB
8532
8533 /* canonical_end can be NULL if it was of the form "*0xdeadbeef". */
8534 if (canonical_end.canonical == NULL)
38a714bb 8535 canonical_end.canonical = xcalloc (1, sizeof (char *));
f1310107
TJB
8536 /* Add the string if not present. */
8537 if (arg_start != arg && canonical_end.canonical[0] == NULL)
8538 canonical_end.canonical[0] = savestring (arg_start, arg - arg_start);
8539
8540 sal_end = sals_end.sals[0];
8541 addr_string_end = canonical_end.canonical[0];
8542 make_cleanup (xfree, addr_string_end);
8543 xfree (sals_end.sals);
8544 xfree (canonical_end.canonical);
8545
8546 if (sals_end.nelts == 0)
8547 error (_("Could not find location of the end of the range."));
8548 else if (sals_end.nelts != 1)
8549 error (_("Cannot create a ranged breakpoint with multiple locations."));
8550
8551 resolve_sal_pc (&sal_end);
8552
8553 end = find_breakpoint_range_end (sal_end);
8554 if (sal_start.pc > end)
177b42fe 8555 error (_("Invalid address range, end precedes start."));
f1310107
TJB
8556
8557 length = end - sal_start.pc + 1;
8558 if (length < 0)
8559 /* Length overflowed. */
8560 error (_("Address range too large."));
8561 else if (length == 1)
8562 {
8563 /* This range is simple enough to be handled by
8564 the `hbreak' command. */
8565 hbreak_command (addr_string_start, 1);
8566
8567 do_cleanups (cleanup_bkpt);
8568
8569 return;
8570 }
8571
8572 /* Now set up the breakpoint. */
8573 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 8574 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
8575 set_breakpoint_count (breakpoint_count + 1);
8576 b->number = breakpoint_count;
8577 b->disposition = disp_donttouch;
8578 b->addr_string = addr_string_start;
8579 b->addr_string_range_end = addr_string_end;
f1310107
TJB
8580 b->loc->length = length;
8581
8582 discard_cleanups (cleanup_bkpt);
8583
8584 mention (b);
8d3788bd 8585 observer_notify_breakpoint_created (b);
f1310107
TJB
8586 update_global_location_list (1);
8587}
8588
4a64f543
MS
8589/* Return non-zero if EXP is verified as constant. Returned zero
8590 means EXP is variable. Also the constant detection may fail for
8591 some constant expressions and in such case still falsely return
8592 zero. */
65d79d4b
SDJ
8593static int
8594watchpoint_exp_is_const (const struct expression *exp)
8595{
8596 int i = exp->nelts;
8597
8598 while (i > 0)
8599 {
8600 int oplenp, argsp;
8601
8602 /* We are only interested in the descriptor of each element. */
8603 operator_length (exp, i, &oplenp, &argsp);
8604 i -= oplenp;
8605
8606 switch (exp->elts[i].opcode)
8607 {
8608 case BINOP_ADD:
8609 case BINOP_SUB:
8610 case BINOP_MUL:
8611 case BINOP_DIV:
8612 case BINOP_REM:
8613 case BINOP_MOD:
8614 case BINOP_LSH:
8615 case BINOP_RSH:
8616 case BINOP_LOGICAL_AND:
8617 case BINOP_LOGICAL_OR:
8618 case BINOP_BITWISE_AND:
8619 case BINOP_BITWISE_IOR:
8620 case BINOP_BITWISE_XOR:
8621 case BINOP_EQUAL:
8622 case BINOP_NOTEQUAL:
8623 case BINOP_LESS:
8624 case BINOP_GTR:
8625 case BINOP_LEQ:
8626 case BINOP_GEQ:
8627 case BINOP_REPEAT:
8628 case BINOP_COMMA:
8629 case BINOP_EXP:
8630 case BINOP_MIN:
8631 case BINOP_MAX:
8632 case BINOP_INTDIV:
8633 case BINOP_CONCAT:
8634 case BINOP_IN:
8635 case BINOP_RANGE:
8636 case TERNOP_COND:
8637 case TERNOP_SLICE:
8638 case TERNOP_SLICE_COUNT:
8639
8640 case OP_LONG:
8641 case OP_DOUBLE:
8642 case OP_DECFLOAT:
8643 case OP_LAST:
8644 case OP_COMPLEX:
8645 case OP_STRING:
8646 case OP_BITSTRING:
8647 case OP_ARRAY:
8648 case OP_TYPE:
8649 case OP_NAME:
8650 case OP_OBJC_NSSTRING:
8651
8652 case UNOP_NEG:
8653 case UNOP_LOGICAL_NOT:
8654 case UNOP_COMPLEMENT:
8655 case UNOP_ADDR:
8656 case UNOP_HIGH:
4a64f543
MS
8657 /* Unary, binary and ternary operators: We have to check
8658 their operands. If they are constant, then so is the
8659 result of that operation. For instance, if A and B are
8660 determined to be constants, then so is "A + B".
8661
8662 UNOP_IND is one exception to the rule above, because the
8663 value of *ADDR is not necessarily a constant, even when
8664 ADDR is. */
65d79d4b
SDJ
8665 break;
8666
8667 case OP_VAR_VALUE:
8668 /* Check whether the associated symbol is a constant.
4a64f543 8669
65d79d4b 8670 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8671 possible that a buggy compiler could mark a variable as
8672 constant even when it is not, and TYPE_CONST would return
8673 true in this case, while SYMBOL_CLASS wouldn't.
8674
8675 We also have to check for function symbols because they
8676 are always constant. */
65d79d4b
SDJ
8677 {
8678 struct symbol *s = exp->elts[i + 2].symbol;
8679
8680 if (SYMBOL_CLASS (s) != LOC_BLOCK
8681 && SYMBOL_CLASS (s) != LOC_CONST
8682 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8683 return 0;
8684 break;
8685 }
8686
8687 /* The default action is to return 0 because we are using
8688 the optimistic approach here: If we don't know something,
8689 then it is not a constant. */
8690 default:
8691 return 0;
8692 }
8693 }
8694
8695 return 1;
8696}
8697
3a5c3e22
PA
8698/* Implement the "dtor" breakpoint_ops method for watchpoints. */
8699
8700static void
8701dtor_watchpoint (struct breakpoint *self)
8702{
8703 struct watchpoint *w = (struct watchpoint *) self;
8704
8705 xfree (w->cond_exp);
8706 xfree (w->exp);
8707 xfree (w->exp_string);
8708 xfree (w->exp_string_reparse);
8709 value_free (w->val);
8710
8711 base_breakpoint_ops.dtor (self);
8712}
8713
348d480f
PA
8714/* Implement the "re_set" breakpoint_ops method for watchpoints. */
8715
8716static void
8717re_set_watchpoint (struct breakpoint *b)
8718{
3a5c3e22
PA
8719 struct watchpoint *w = (struct watchpoint *) b;
8720
348d480f
PA
8721 /* Watchpoint can be either on expression using entirely global
8722 variables, or it can be on local variables.
8723
8724 Watchpoints of the first kind are never auto-deleted, and even
8725 persist across program restarts. Since they can use variables
8726 from shared libraries, we need to reparse expression as libraries
8727 are loaded and unloaded.
8728
8729 Watchpoints on local variables can also change meaning as result
8730 of solib event. For example, if a watchpoint uses both a local
8731 and a global variables in expression, it's a local watchpoint,
8732 but unloading of a shared library will make the expression
8733 invalid. This is not a very common use case, but we still
8734 re-evaluate expression, to avoid surprises to the user.
8735
8736 Note that for local watchpoints, we re-evaluate it only if
8737 watchpoints frame id is still valid. If it's not, it means the
8738 watchpoint is out of scope and will be deleted soon. In fact,
8739 I'm not sure we'll ever be called in this case.
8740
8741 If a local watchpoint's frame id is still valid, then
3a5c3e22 8742 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 8743
3a5c3e22
PA
8744 Don't do anything about disabled watchpoints, since they will be
8745 reevaluated again when enabled. */
8746 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
8747}
8748
77b06cd7
TJB
8749/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8750
8751static int
8752insert_watchpoint (struct bp_location *bl)
8753{
3a5c3e22
PA
8754 struct watchpoint *w = (struct watchpoint *) bl->owner;
8755 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8756
8757 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8758 w->cond_exp);
77b06cd7
TJB
8759}
8760
8761/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8762
8763static int
8764remove_watchpoint (struct bp_location *bl)
8765{
3a5c3e22
PA
8766 struct watchpoint *w = (struct watchpoint *) bl->owner;
8767 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8768
8769 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8770 w->cond_exp);
e09342b5
TJB
8771}
8772
e09342b5 8773static int
348d480f
PA
8774breakpoint_hit_watchpoint (const struct bp_location *bl,
8775 struct address_space *aspace, CORE_ADDR bp_addr)
e09342b5 8776{
348d480f 8777 struct breakpoint *b = bl->owner;
3a5c3e22 8778 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 8779
348d480f
PA
8780 /* Continuable hardware watchpoints are treated as non-existent if the
8781 reason we stopped wasn't a hardware watchpoint (we didn't stop on
8782 some data address). Otherwise gdb won't stop on a break instruction
8783 in the code (not from a breakpoint) when a hardware watchpoint has
8784 been defined. Also skip watchpoints which we know did not trigger
8785 (did not match the data address). */
8786 if (is_hardware_watchpoint (b)
3a5c3e22 8787 && w->watchpoint_triggered == watch_triggered_no)
348d480f 8788 return 0;
9c06b0b4 8789
348d480f 8790 return 1;
9c06b0b4
TJB
8791}
8792
348d480f
PA
8793static void
8794check_status_watchpoint (bpstat bs)
9c06b0b4 8795{
348d480f 8796 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 8797
348d480f 8798 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
8799}
8800
8801/* Implement the "resources_needed" breakpoint_ops method for
348d480f 8802 hardware watchpoints. */
9c06b0b4
TJB
8803
8804static int
348d480f 8805resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 8806{
3a5c3e22
PA
8807 struct watchpoint *w = (struct watchpoint *) bl->owner;
8808 int length = w->exact? 1 : bl->length;
348d480f
PA
8809
8810 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
8811}
8812
8813/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 8814 hardware watchpoints. */
9c06b0b4
TJB
8815
8816static int
348d480f 8817works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 8818{
efa80663
PA
8819 /* Read and access watchpoints only work with hardware support. */
8820 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
8821}
8822
9c06b0b4 8823static enum print_stop_action
348d480f 8824print_it_watchpoint (bpstat bs)
9c06b0b4 8825{
348d480f
PA
8826 struct cleanup *old_chain;
8827 struct breakpoint *b;
8828 const struct bp_location *bl;
8829 struct ui_stream *stb;
8830 enum print_stop_action result;
3a5c3e22 8831 struct watchpoint *w;
79a45e25 8832 struct ui_out *uiout = current_uiout;
348d480f
PA
8833
8834 gdb_assert (bs->bp_location_at != NULL);
8835
8836 bl = bs->bp_location_at;
8837 b = bs->breakpoint_at;
3a5c3e22 8838 w = (struct watchpoint *) b;
348d480f
PA
8839
8840 stb = ui_out_stream_new (uiout);
8841 old_chain = make_cleanup_ui_out_stream_delete (stb);
9c06b0b4
TJB
8842
8843 switch (b->type)
8844 {
348d480f 8845 case bp_watchpoint:
9c06b0b4
TJB
8846 case bp_hardware_watchpoint:
8847 annotate_watchpoint (b->number);
8848 if (ui_out_is_mi_like_p (uiout))
8849 ui_out_field_string
8850 (uiout, "reason",
8851 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
8852 mention (b);
8853 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8854 ui_out_text (uiout, "\nOld value = ");
8855 watchpoint_value_print (bs->old_val, stb->stream);
8856 ui_out_field_stream (uiout, "old", stb);
8857 ui_out_text (uiout, "\nNew value = ");
3a5c3e22 8858 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8859 ui_out_field_stream (uiout, "new", stb);
8860 ui_out_text (uiout, "\n");
8861 /* More than one watchpoint may have been triggered. */
8862 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8863 break;
8864
8865 case bp_read_watchpoint:
8866 if (ui_out_is_mi_like_p (uiout))
8867 ui_out_field_string
8868 (uiout, "reason",
8869 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
8870 mention (b);
8871 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8872 ui_out_text (uiout, "\nValue = ");
3a5c3e22 8873 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8874 ui_out_field_stream (uiout, "value", stb);
8875 ui_out_text (uiout, "\n");
8876 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8877 break;
8878
8879 case bp_access_watchpoint:
348d480f
PA
8880 if (bs->old_val != NULL)
8881 {
8882 annotate_watchpoint (b->number);
8883 if (ui_out_is_mi_like_p (uiout))
8884 ui_out_field_string
8885 (uiout, "reason",
8886 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8887 mention (b);
8888 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8889 ui_out_text (uiout, "\nOld value = ");
8890 watchpoint_value_print (bs->old_val, stb->stream);
8891 ui_out_field_stream (uiout, "old", stb);
8892 ui_out_text (uiout, "\nNew value = ");
8893 }
8894 else
8895 {
8896 mention (b);
8897 if (ui_out_is_mi_like_p (uiout))
8898 ui_out_field_string
8899 (uiout, "reason",
8900 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8901 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8902 ui_out_text (uiout, "\nValue = ");
8903 }
3a5c3e22 8904 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8905 ui_out_field_stream (uiout, "new", stb);
8906 ui_out_text (uiout, "\n");
8907 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8908 break;
8909 default:
348d480f 8910 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8911 }
8912
348d480f
PA
8913 do_cleanups (old_chain);
8914 return result;
8915}
8916
8917/* Implement the "print_mention" breakpoint_ops method for hardware
8918 watchpoints. */
8919
8920static void
8921print_mention_watchpoint (struct breakpoint *b)
8922{
8923 struct cleanup *ui_out_chain;
3a5c3e22 8924 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 8925 struct ui_out *uiout = current_uiout;
348d480f
PA
8926
8927 switch (b->type)
8928 {
8929 case bp_watchpoint:
8930 ui_out_text (uiout, "Watchpoint ");
8931 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8932 break;
8933 case bp_hardware_watchpoint:
8934 ui_out_text (uiout, "Hardware watchpoint ");
8935 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8936 break;
8937 case bp_read_watchpoint:
8938 ui_out_text (uiout, "Hardware read watchpoint ");
8939 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8940 break;
8941 case bp_access_watchpoint:
8942 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
8943 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8944 break;
8945 default:
8946 internal_error (__FILE__, __LINE__,
8947 _("Invalid hardware watchpoint type."));
8948 }
8949
8950 ui_out_field_int (uiout, "number", b->number);
8951 ui_out_text (uiout, ": ");
3a5c3e22 8952 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
8953 do_cleanups (ui_out_chain);
8954}
8955
8956/* Implement the "print_recreate" breakpoint_ops method for
8957 watchpoints. */
8958
8959static void
8960print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
8961{
3a5c3e22
PA
8962 struct watchpoint *w = (struct watchpoint *) b;
8963
348d480f
PA
8964 switch (b->type)
8965 {
8966 case bp_watchpoint:
8967 case bp_hardware_watchpoint:
8968 fprintf_unfiltered (fp, "watch");
8969 break;
8970 case bp_read_watchpoint:
8971 fprintf_unfiltered (fp, "rwatch");
8972 break;
8973 case bp_access_watchpoint:
8974 fprintf_unfiltered (fp, "awatch");
8975 break;
8976 default:
8977 internal_error (__FILE__, __LINE__,
8978 _("Invalid watchpoint type."));
8979 }
8980
3a5c3e22 8981 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 8982 print_recreate_thread (b, fp);
348d480f
PA
8983}
8984
8985/* The breakpoint_ops structure to be used in hardware watchpoints. */
8986
2060206e 8987static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
8988
8989/* Implement the "insert" breakpoint_ops method for
8990 masked hardware watchpoints. */
8991
8992static int
8993insert_masked_watchpoint (struct bp_location *bl)
8994{
3a5c3e22
PA
8995 struct watchpoint *w = (struct watchpoint *) bl->owner;
8996
8997 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8998 bl->watchpoint_type);
8999}
9000
9001/* Implement the "remove" breakpoint_ops method for
9002 masked hardware watchpoints. */
9003
9004static int
9005remove_masked_watchpoint (struct bp_location *bl)
9006{
3a5c3e22
PA
9007 struct watchpoint *w = (struct watchpoint *) bl->owner;
9008
9009 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
9010 bl->watchpoint_type);
9011}
9012
9013/* Implement the "resources_needed" breakpoint_ops method for
9014 masked hardware watchpoints. */
9015
9016static int
9017resources_needed_masked_watchpoint (const struct bp_location *bl)
9018{
3a5c3e22
PA
9019 struct watchpoint *w = (struct watchpoint *) bl->owner;
9020
9021 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
9022}
9023
9024/* Implement the "works_in_software_mode" breakpoint_ops method for
9025 masked hardware watchpoints. */
9026
9027static int
9028works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
9029{
9030 return 0;
9031}
9032
9033/* Implement the "print_it" breakpoint_ops method for
9034 masked hardware watchpoints. */
9035
9036static enum print_stop_action
9037print_it_masked_watchpoint (bpstat bs)
9038{
9039 struct breakpoint *b = bs->breakpoint_at;
79a45e25 9040 struct ui_out *uiout = current_uiout;
348d480f
PA
9041
9042 /* Masked watchpoints have only one location. */
9043 gdb_assert (b->loc && b->loc->next == NULL);
9044
9045 switch (b->type)
9046 {
9047 case bp_hardware_watchpoint:
9048 annotate_watchpoint (b->number);
9049 if (ui_out_is_mi_like_p (uiout))
9050 ui_out_field_string
9051 (uiout, "reason",
9052 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9053 break;
9054
9055 case bp_read_watchpoint:
9056 if (ui_out_is_mi_like_p (uiout))
9057 ui_out_field_string
9058 (uiout, "reason",
9059 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9060 break;
9061
9062 case bp_access_watchpoint:
9063 if (ui_out_is_mi_like_p (uiout))
9064 ui_out_field_string
9065 (uiout, "reason",
9066 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9067 break;
9068 default:
9069 internal_error (__FILE__, __LINE__,
9070 _("Invalid hardware watchpoint type."));
9071 }
9072
9073 mention (b);
9c06b0b4
TJB
9074 ui_out_text (uiout, _("\n\
9075Check the underlying instruction at PC for the memory\n\
9076address and value which triggered this watchpoint.\n"));
9077 ui_out_text (uiout, "\n");
9078
9079 /* More than one watchpoint may have been triggered. */
9080 return PRINT_UNKNOWN;
9081}
9082
9083/* Implement the "print_one_detail" breakpoint_ops method for
9084 masked hardware watchpoints. */
9085
9086static void
9087print_one_detail_masked_watchpoint (const struct breakpoint *b,
9088 struct ui_out *uiout)
9089{
3a5c3e22
PA
9090 struct watchpoint *w = (struct watchpoint *) b;
9091
9c06b0b4
TJB
9092 /* Masked watchpoints have only one location. */
9093 gdb_assert (b->loc && b->loc->next == NULL);
9094
9095 ui_out_text (uiout, "\tmask ");
3a5c3e22 9096 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
9097 ui_out_text (uiout, "\n");
9098}
9099
9100/* Implement the "print_mention" breakpoint_ops method for
9101 masked hardware watchpoints. */
9102
9103static void
9104print_mention_masked_watchpoint (struct breakpoint *b)
9105{
3a5c3e22 9106 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 9107 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
9108 struct cleanup *ui_out_chain;
9109
9110 switch (b->type)
9111 {
9112 case bp_hardware_watchpoint:
9113 ui_out_text (uiout, "Masked hardware watchpoint ");
9114 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
9115 break;
9116 case bp_read_watchpoint:
9117 ui_out_text (uiout, "Masked hardware read watchpoint ");
9118 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
9119 break;
9120 case bp_access_watchpoint:
9121 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
9122 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
9123 break;
9124 default:
9125 internal_error (__FILE__, __LINE__,
9126 _("Invalid hardware watchpoint type."));
9127 }
9128
9129 ui_out_field_int (uiout, "number", b->number);
9130 ui_out_text (uiout, ": ");
3a5c3e22 9131 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
9132 do_cleanups (ui_out_chain);
9133}
9134
9135/* Implement the "print_recreate" breakpoint_ops method for
9136 masked hardware watchpoints. */
9137
9138static void
9139print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
9140{
3a5c3e22 9141 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
9142 char tmp[40];
9143
9144 switch (b->type)
9145 {
9146 case bp_hardware_watchpoint:
9147 fprintf_unfiltered (fp, "watch");
9148 break;
9149 case bp_read_watchpoint:
9150 fprintf_unfiltered (fp, "rwatch");
9151 break;
9152 case bp_access_watchpoint:
9153 fprintf_unfiltered (fp, "awatch");
9154 break;
9155 default:
9156 internal_error (__FILE__, __LINE__,
9157 _("Invalid hardware watchpoint type."));
9158 }
9159
3a5c3e22
PA
9160 sprintf_vma (tmp, w->hw_wp_mask);
9161 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 9162 print_recreate_thread (b, fp);
9c06b0b4
TJB
9163}
9164
9165/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
9166
2060206e 9167static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
9168
9169/* Tell whether the given watchpoint is a masked hardware watchpoint. */
9170
9171static int
9172is_masked_watchpoint (const struct breakpoint *b)
9173{
9174 return b->ops == &masked_watchpoint_breakpoint_ops;
9175}
9176
53a5351d
JM
9177/* accessflag: hw_write: watch write,
9178 hw_read: watch read,
9179 hw_access: watch access (read or write) */
c906108c 9180static void
84f4c1fe
PM
9181watch_command_1 (char *arg, int accessflag, int from_tty,
9182 int just_location, int internal)
c906108c 9183{
a9634178 9184 volatile struct gdb_exception e;
d983da9c 9185 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 9186 struct expression *exp;
60e1c644 9187 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 9188 struct value *val, *mark, *result;
c906108c 9189 struct frame_info *frame;
c906108c
SS
9190 char *exp_start = NULL;
9191 char *exp_end = NULL;
9c06b0b4
TJB
9192 char *tok, *end_tok;
9193 int toklen = -1;
c906108c
SS
9194 char *cond_start = NULL;
9195 char *cond_end = NULL;
c906108c 9196 enum bptype bp_type;
37e4754d 9197 int thread = -1;
0cf6dd15 9198 int pc = 0;
9c06b0b4
TJB
9199 /* Flag to indicate whether we are going to use masks for
9200 the hardware watchpoint. */
9201 int use_mask = 0;
9202 CORE_ADDR mask = 0;
3a5c3e22 9203 struct watchpoint *w;
c906108c 9204
37e4754d
LM
9205 /* Make sure that we actually have parameters to parse. */
9206 if (arg != NULL && arg[0] != '\0')
9207 {
9c06b0b4 9208 char *value_start;
37e4754d 9209
9c06b0b4
TJB
9210 /* Look for "parameter value" pairs at the end
9211 of the arguments string. */
9212 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
9213 {
9214 /* Skip whitespace at the end of the argument list. */
9215 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9216 tok--;
9217
9218 /* Find the beginning of the last token.
9219 This is the value of the parameter. */
9220 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9221 tok--;
9222 value_start = tok + 1;
9223
9224 /* Skip whitespace. */
9225 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9226 tok--;
9227
9228 end_tok = tok;
9229
9230 /* Find the beginning of the second to last token.
9231 This is the parameter itself. */
9232 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9233 tok--;
9234 tok++;
9235 toklen = end_tok - tok + 1;
9236
9237 if (toklen == 6 && !strncmp (tok, "thread", 6))
9238 {
9239 /* At this point we've found a "thread" token, which means
9240 the user is trying to set a watchpoint that triggers
9241 only in a specific thread. */
9242 char *endp;
37e4754d 9243
9c06b0b4
TJB
9244 if (thread != -1)
9245 error(_("You can specify only one thread."));
37e4754d 9246
9c06b0b4
TJB
9247 /* Extract the thread ID from the next token. */
9248 thread = strtol (value_start, &endp, 0);
37e4754d 9249
9c06b0b4
TJB
9250 /* Check if the user provided a valid numeric value for the
9251 thread ID. */
9252 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9253 error (_("Invalid thread ID specification %s."), value_start);
9254
9255 /* Check if the thread actually exists. */
9256 if (!valid_thread_id (thread))
9257 error (_("Unknown thread %d."), thread);
9258 }
9259 else if (toklen == 4 && !strncmp (tok, "mask", 4))
9260 {
9261 /* We've found a "mask" token, which means the user wants to
9262 create a hardware watchpoint that is going to have the mask
9263 facility. */
9264 struct value *mask_value, *mark;
37e4754d 9265
9c06b0b4
TJB
9266 if (use_mask)
9267 error(_("You can specify only one mask."));
37e4754d 9268
9c06b0b4 9269 use_mask = just_location = 1;
37e4754d 9270
9c06b0b4
TJB
9271 mark = value_mark ();
9272 mask_value = parse_to_comma_and_eval (&value_start);
9273 mask = value_as_address (mask_value);
9274 value_free_to_mark (mark);
9275 }
9276 else
9277 /* We didn't recognize what we found. We should stop here. */
9278 break;
37e4754d 9279
9c06b0b4
TJB
9280 /* Truncate the string and get rid of the "parameter value" pair before
9281 the arguments string is parsed by the parse_exp_1 function. */
9282 *tok = '\0';
9283 }
37e4754d
LM
9284 }
9285
9286 /* Parse the rest of the arguments. */
c906108c
SS
9287 innermost_block = NULL;
9288 exp_start = arg;
9289 exp = parse_exp_1 (&arg, 0, 0);
9290 exp_end = arg;
fa8a61dc
TT
9291 /* Remove trailing whitespace from the expression before saving it.
9292 This makes the eventual display of the expression string a bit
9293 prettier. */
9294 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9295 --exp_end;
9296
65d79d4b
SDJ
9297 /* Checking if the expression is not constant. */
9298 if (watchpoint_exp_is_const (exp))
9299 {
9300 int len;
9301
9302 len = exp_end - exp_start;
9303 while (len > 0 && isspace (exp_start[len - 1]))
9304 len--;
9305 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9306 }
9307
c906108c
SS
9308 exp_valid_block = innermost_block;
9309 mark = value_mark ();
a1442452 9310 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
9311
9312 if (just_location)
9313 {
9c06b0b4
TJB
9314 int ret;
9315
06a64a0b 9316 exp_valid_block = NULL;
a1442452 9317 val = value_addr (result);
06a64a0b
TT
9318 release_value (val);
9319 value_free_to_mark (mark);
9c06b0b4
TJB
9320
9321 if (use_mask)
9322 {
9323 ret = target_masked_watch_num_registers (value_as_address (val),
9324 mask);
9325 if (ret == -1)
9326 error (_("This target does not support masked watchpoints."));
9327 else if (ret == -2)
9328 error (_("Invalid mask or memory region."));
9329 }
06a64a0b
TT
9330 }
9331 else if (val != NULL)
fa4727a6 9332 release_value (val);
c906108c 9333
e9cafbcc
TT
9334 tok = skip_spaces (arg);
9335 end_tok = skip_to_space (tok);
c906108c
SS
9336
9337 toklen = end_tok - tok;
9338 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9339 {
2d134ed3
PA
9340 struct expression *cond;
9341
60e1c644 9342 innermost_block = NULL;
c906108c
SS
9343 tok = cond_start = end_tok + 1;
9344 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
9345
9346 /* The watchpoint expression may not be local, but the condition
9347 may still be. E.g.: `watch global if local > 0'. */
9348 cond_exp_valid_block = innermost_block;
9349
2d134ed3 9350 xfree (cond);
c906108c
SS
9351 cond_end = tok;
9352 }
9353 if (*tok)
8a3fe4f8 9354 error (_("Junk at end of command."));
c906108c 9355
53a5351d 9356 if (accessflag == hw_read)
c5aa993b 9357 bp_type = bp_read_watchpoint;
53a5351d 9358 else if (accessflag == hw_access)
c5aa993b
JM
9359 bp_type = bp_access_watchpoint;
9360 else
9361 bp_type = bp_hardware_watchpoint;
c906108c 9362
d983da9c 9363 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
9364
9365 /* If the expression is "local", then set up a "watchpoint scope"
9366 breakpoint at the point where we've left the scope of the watchpoint
9367 expression. Create the scope breakpoint before the watchpoint, so
9368 that we will encounter it first in bpstat_stop_status. */
60e1c644 9369 if (exp_valid_block && frame)
d983da9c 9370 {
edb3359d
DJ
9371 if (frame_id_p (frame_unwind_caller_id (frame)))
9372 {
9373 scope_breakpoint
a6d9a66e
UW
9374 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
9375 frame_unwind_caller_pc (frame),
06edf0c0
PA
9376 bp_watchpoint_scope,
9377 &momentary_breakpoint_ops);
d983da9c 9378
edb3359d 9379 scope_breakpoint->enable_state = bp_enabled;
d983da9c 9380
edb3359d
DJ
9381 /* Automatically delete the breakpoint when it hits. */
9382 scope_breakpoint->disposition = disp_del;
d983da9c 9383
edb3359d
DJ
9384 /* Only break in the proper frame (help with recursion). */
9385 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 9386
edb3359d 9387 /* Set the address at which we will stop. */
a6d9a66e
UW
9388 scope_breakpoint->loc->gdbarch
9389 = frame_unwind_caller_arch (frame);
edb3359d
DJ
9390 scope_breakpoint->loc->requested_address
9391 = frame_unwind_caller_pc (frame);
9392 scope_breakpoint->loc->address
a6d9a66e
UW
9393 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9394 scope_breakpoint->loc->requested_address,
edb3359d
DJ
9395 scope_breakpoint->type);
9396 }
d983da9c
DJ
9397 }
9398
c906108c 9399 /* Now set up the breakpoint. */
3a5c3e22
PA
9400
9401 w = XCNEW (struct watchpoint);
9402 b = &w->base;
348d480f 9403 if (use_mask)
3a5c3e22
PA
9404 init_raw_breakpoint_without_location (b, NULL, bp_type,
9405 &masked_watchpoint_breakpoint_ops);
348d480f 9406 else
3a5c3e22
PA
9407 init_raw_breakpoint_without_location (b, NULL, bp_type,
9408 &watchpoint_breakpoint_ops);
37e4754d 9409 b->thread = thread;
b5de0fa7 9410 b->disposition = disp_donttouch;
348d480f 9411 b->pspace = current_program_space;
3a5c3e22
PA
9412 w->exp = exp;
9413 w->exp_valid_block = exp_valid_block;
9414 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
9415 if (just_location)
9416 {
9417 struct type *t = value_type (val);
9418 CORE_ADDR addr = value_as_address (val);
9419 char *name;
9420
9421 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9422 name = type_to_string (t);
9423
3a5c3e22 9424 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 9425 core_addr_to_string (addr));
06a64a0b
TT
9426 xfree (name);
9427
3a5c3e22 9428 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
9429 (int) (exp_end - exp_start), exp_start);
9430
06a64a0b
TT
9431 /* The above expression is in C. */
9432 b->language = language_c;
9433 }
9434 else
3a5c3e22 9435 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
9436
9437 if (use_mask)
9438 {
3a5c3e22 9439 w->hw_wp_mask = mask;
9c06b0b4
TJB
9440 }
9441 else
9442 {
3a5c3e22
PA
9443 w->val = val;
9444 w->val_valid = 1;
9c06b0b4 9445 }
77b06cd7 9446
c906108c
SS
9447 if (cond_start)
9448 b->cond_string = savestring (cond_start, cond_end - cond_start);
9449 else
9450 b->cond_string = 0;
c5aa993b 9451
c906108c 9452 if (frame)
f6bc2008 9453 {
3a5c3e22
PA
9454 w->watchpoint_frame = get_frame_id (frame);
9455 w->watchpoint_thread = inferior_ptid;
f6bc2008 9456 }
c906108c 9457 else
f6bc2008 9458 {
3a5c3e22
PA
9459 w->watchpoint_frame = null_frame_id;
9460 w->watchpoint_thread = null_ptid;
f6bc2008 9461 }
c906108c 9462
d983da9c 9463 if (scope_breakpoint != NULL)
c906108c 9464 {
d983da9c
DJ
9465 /* The scope breakpoint is related to the watchpoint. We will
9466 need to act on them together. */
9467 b->related_breakpoint = scope_breakpoint;
9468 scope_breakpoint->related_breakpoint = b;
c906108c 9469 }
d983da9c 9470
06a64a0b
TT
9471 if (!just_location)
9472 value_free_to_mark (mark);
2d134ed3 9473
a9634178
TJB
9474 TRY_CATCH (e, RETURN_MASK_ALL)
9475 {
9476 /* Finally update the new watchpoint. This creates the locations
9477 that should be inserted. */
3a5c3e22 9478 update_watchpoint (w, 1);
a9634178
TJB
9479 }
9480 if (e.reason < 0)
9481 {
9482 delete_breakpoint (b);
9483 throw_exception (e);
9484 }
9485
3ea46bff 9486 install_breakpoint (internal, b, 1);
c906108c
SS
9487}
9488
e09342b5 9489/* Return count of debug registers needed to watch the given expression.
e09342b5 9490 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9491
c906108c 9492static int
a9634178 9493can_use_hardware_watchpoint (struct value *v)
c906108c
SS
9494{
9495 int found_memory_cnt = 0;
2e70b7b9 9496 struct value *head = v;
c906108c
SS
9497
9498 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9499 if (!can_use_hw_watchpoints)
c906108c 9500 return 0;
c5aa993b 9501
5c44784c
JM
9502 /* Make sure that the value of the expression depends only upon
9503 memory contents, and values computed from them within GDB. If we
9504 find any register references or function calls, we can't use a
9505 hardware watchpoint.
9506
9507 The idea here is that evaluating an expression generates a series
9508 of values, one holding the value of every subexpression. (The
9509 expression a*b+c has five subexpressions: a, b, a*b, c, and
9510 a*b+c.) GDB's values hold almost enough information to establish
9511 the criteria given above --- they identify memory lvalues,
9512 register lvalues, computed values, etcetera. So we can evaluate
9513 the expression, and then scan the chain of values that leaves
9514 behind to decide whether we can detect any possible change to the
9515 expression's final value using only hardware watchpoints.
9516
9517 However, I don't think that the values returned by inferior
9518 function calls are special in any way. So this function may not
9519 notice that an expression involving an inferior function call
9520 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9521 for (; v; v = value_next (v))
c906108c 9522 {
5c44784c 9523 if (VALUE_LVAL (v) == lval_memory)
c906108c 9524 {
8464be76
DJ
9525 if (v != head && value_lazy (v))
9526 /* A lazy memory lvalue in the chain is one that GDB never
9527 needed to fetch; we either just used its address (e.g.,
9528 `a' in `a.b') or we never needed it at all (e.g., `a'
9529 in `a,b'). This doesn't apply to HEAD; if that is
9530 lazy then it was not readable, but watch it anyway. */
5c44784c 9531 ;
53a5351d 9532 else
5c44784c
JM
9533 {
9534 /* Ahh, memory we actually used! Check if we can cover
9535 it with hardware watchpoints. */
df407dfe 9536 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9537
9538 /* We only watch structs and arrays if user asked for it
9539 explicitly, never if they just happen to appear in a
9540 middle of some value chain. */
9541 if (v == head
9542 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9543 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9544 {
42ae5230 9545 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9546 int len;
9547 int num_regs;
9548
a9634178 9549 len = (target_exact_watchpoints
e09342b5
TJB
9550 && is_scalar_type_recursive (vtype))?
9551 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9552
e09342b5
TJB
9553 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9554 if (!num_regs)
2e70b7b9
MS
9555 return 0;
9556 else
e09342b5 9557 found_memory_cnt += num_regs;
2e70b7b9 9558 }
5c44784c 9559 }
c5aa993b 9560 }
5086187c
AC
9561 else if (VALUE_LVAL (v) != not_lval
9562 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9563 return 0; /* These are values from the history (e.g., $1). */
5086187c 9564 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9565 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9566 }
9567
9568 /* The expression itself looks suitable for using a hardware
9569 watchpoint, but give the target machine a chance to reject it. */
9570 return found_memory_cnt;
9571}
9572
8b93c638 9573void
84f4c1fe 9574watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9575{
84f4c1fe 9576 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9577}
9578
9579/* A helper function that looks for an argument at the start of a
9580 string. The argument must also either be at the end of the string,
9581 or be followed by whitespace. Returns 1 if it finds the argument,
9582 0 otherwise. If the argument is found, it updates *STR. */
9583
9584static int
9585check_for_argument (char **str, char *arg, int arg_len)
9586{
9587 if (strncmp (*str, arg, arg_len) == 0
9588 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9589 {
9590 *str += arg_len;
9591 return 1;
9592 }
9593 return 0;
9594}
9595
9596/* A helper function that looks for the "-location" argument and then
9597 calls watch_command_1. */
9598
9599static void
9600watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9601{
9602 int just_location = 0;
9603
9604 if (arg
9605 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9606 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9607 {
e9cafbcc 9608 arg = skip_spaces (arg);
06a64a0b
TT
9609 just_location = 1;
9610 }
9611
84f4c1fe 9612 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9613}
8926118c 9614
c5aa993b 9615static void
fba45db2 9616watch_command (char *arg, int from_tty)
c906108c 9617{
06a64a0b 9618 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9619}
9620
8b93c638 9621void
84f4c1fe 9622rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9623{
84f4c1fe 9624 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9625}
8926118c 9626
c5aa993b 9627static void
fba45db2 9628rwatch_command (char *arg, int from_tty)
c906108c 9629{
06a64a0b 9630 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9631}
9632
8b93c638 9633void
84f4c1fe 9634awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9635{
84f4c1fe 9636 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9637}
8926118c 9638
c5aa993b 9639static void
fba45db2 9640awatch_command (char *arg, int from_tty)
c906108c 9641{
06a64a0b 9642 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9643}
c906108c 9644\f
c5aa993b 9645
43ff13b4 9646/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9647 because it uses the mechanisms of breakpoints. */
9648
bfec99b2
PA
9649struct until_break_command_continuation_args
9650{
9651 struct breakpoint *breakpoint;
9652 struct breakpoint *breakpoint2;
186c406b 9653 int thread_num;
bfec99b2
PA
9654};
9655
43ff13b4 9656/* This function is called by fetch_inferior_event via the
4a64f543 9657 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9658 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9659 command. */
c2c6d25f 9660static void
fa4cd53f 9661until_break_command_continuation (void *arg, int err)
43ff13b4 9662{
bfec99b2
PA
9663 struct until_break_command_continuation_args *a = arg;
9664
9665 delete_breakpoint (a->breakpoint);
9666 if (a->breakpoint2)
9667 delete_breakpoint (a->breakpoint2);
186c406b 9668 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9669}
9670
c906108c 9671void
ae66c1fc 9672until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9673{
9674 struct symtabs_and_lines sals;
9675 struct symtab_and_line sal;
206415a3 9676 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9677 struct breakpoint *breakpoint;
f107f563 9678 struct breakpoint *breakpoint2 = NULL;
c906108c 9679 struct cleanup *old_chain;
186c406b
TT
9680 int thread;
9681 struct thread_info *tp;
c906108c
SS
9682
9683 clear_proceed_status ();
9684
9685 /* Set a breakpoint where the user wants it and at return from
4a64f543 9686 this function. */
c5aa993b 9687
1bfeeb0f
JL
9688 if (last_displayed_sal_is_valid ())
9689 sals = decode_line_1 (&arg, 1,
9690 get_last_displayed_symtab (),
9691 get_last_displayed_line (),
9692 NULL);
c906108c 9693 else
58438ac1 9694 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, NULL);
c5aa993b 9695
c906108c 9696 if (sals.nelts != 1)
8a3fe4f8 9697 error (_("Couldn't get information on specified line."));
c5aa993b 9698
c906108c 9699 sal = sals.sals[0];
4a64f543 9700 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9701
c906108c 9702 if (*arg)
8a3fe4f8 9703 error (_("Junk at end of arguments."));
c5aa993b 9704
c906108c 9705 resolve_sal_pc (&sal);
c5aa993b 9706
ae66c1fc
EZ
9707 if (anywhere)
9708 /* If the user told us to continue until a specified location,
9709 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9710 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9711 null_frame_id, bp_until);
ae66c1fc 9712 else
4a64f543
MS
9713 /* Otherwise, specify the selected frame, because we want to stop
9714 only at the very same frame. */
a6d9a66e
UW
9715 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9716 get_stack_frame_id (frame),
ae66c1fc 9717 bp_until);
c5aa993b 9718
f107f563 9719 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9720
186c406b
TT
9721 tp = inferior_thread ();
9722 thread = tp->num;
9723
ae66c1fc
EZ
9724 /* Keep within the current frame, or in frames called by the current
9725 one. */
edb3359d
DJ
9726
9727 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9728 {
edb3359d
DJ
9729 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9730 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9731 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9732 sal,
edb3359d 9733 frame_unwind_caller_id (frame),
f107f563
VP
9734 bp_until);
9735 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9736
9737 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9738 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9739 }
c5aa993b 9740
c906108c 9741 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9742
4a64f543
MS
9743 /* If we are running asynchronously, and proceed call above has
9744 actually managed to start the target, arrange for breakpoints to
9745 be deleted when the target stops. Otherwise, we're already
9746 stopped and delete breakpoints via cleanup chain. */
f107f563 9747
8ea051c5 9748 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9749 {
bfec99b2
PA
9750 struct until_break_command_continuation_args *args;
9751 args = xmalloc (sizeof (*args));
f107f563 9752
bfec99b2
PA
9753 args->breakpoint = breakpoint;
9754 args->breakpoint2 = breakpoint2;
186c406b 9755 args->thread_num = thread;
f107f563
VP
9756
9757 discard_cleanups (old_chain);
95e54da7
PA
9758 add_continuation (inferior_thread (),
9759 until_break_command_continuation, args,
604ead4a 9760 xfree);
f107f563
VP
9761 }
9762 else
c5aa993b 9763 do_cleanups (old_chain);
c906108c 9764}
ae66c1fc 9765
c906108c
SS
9766/* This function attempts to parse an optional "if <cond>" clause
9767 from the arg string. If one is not found, it returns NULL.
c5aa993b 9768
c906108c
SS
9769 Else, it returns a pointer to the condition string. (It does not
9770 attempt to evaluate the string against a particular block.) And,
9771 it updates arg to point to the first character following the parsed
4a64f543 9772 if clause in the arg string. */
53a5351d 9773
c906108c 9774static char *
fba45db2 9775ep_parse_optional_if_clause (char **arg)
c906108c 9776{
c5aa993b
JM
9777 char *cond_string;
9778
9779 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9780 return NULL;
c5aa993b 9781
4a64f543 9782 /* Skip the "if" keyword. */
c906108c 9783 (*arg) += 2;
c5aa993b 9784
c906108c 9785 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9786 condition string. */
e9cafbcc 9787 *arg = skip_spaces (*arg);
c906108c 9788 cond_string = *arg;
c5aa993b 9789
4a64f543
MS
9790 /* Assume that the condition occupies the remainder of the arg
9791 string. */
c906108c 9792 (*arg) += strlen (cond_string);
c5aa993b 9793
c906108c
SS
9794 return cond_string;
9795}
c5aa993b 9796
c906108c
SS
9797/* Commands to deal with catching events, such as signals, exceptions,
9798 process start/exit, etc. */
c5aa993b
JM
9799
9800typedef enum
9801{
44feb3ce
TT
9802 catch_fork_temporary, catch_vfork_temporary,
9803 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9804}
9805catch_fork_kind;
9806
c906108c 9807static void
cc59ec59
MS
9808catch_fork_command_1 (char *arg, int from_tty,
9809 struct cmd_list_element *command)
c906108c 9810{
a6d9a66e 9811 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9812 char *cond_string = NULL;
44feb3ce
TT
9813 catch_fork_kind fork_kind;
9814 int tempflag;
9815
9816 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9817 tempflag = (fork_kind == catch_fork_temporary
9818 || fork_kind == catch_vfork_temporary);
c5aa993b 9819
44feb3ce
TT
9820 if (!arg)
9821 arg = "";
e9cafbcc 9822 arg = skip_spaces (arg);
c5aa993b 9823
c906108c 9824 /* The allowed syntax is:
c5aa993b
JM
9825 catch [v]fork
9826 catch [v]fork if <cond>
9827
4a64f543 9828 First, check if there's an if clause. */
c906108c 9829 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9830
c906108c 9831 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9832 error (_("Junk at end of arguments."));
c5aa993b 9833
c906108c 9834 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9835 and enable reporting of such events. */
c5aa993b
JM
9836 switch (fork_kind)
9837 {
44feb3ce
TT
9838 case catch_fork_temporary:
9839 case catch_fork_permanent:
a6d9a66e 9840 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9841 &catch_fork_breakpoint_ops);
c906108c 9842 break;
44feb3ce
TT
9843 case catch_vfork_temporary:
9844 case catch_vfork_permanent:
a6d9a66e 9845 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9846 &catch_vfork_breakpoint_ops);
c906108c 9847 break;
c5aa993b 9848 default:
8a3fe4f8 9849 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9850 break;
c5aa993b 9851 }
c906108c
SS
9852}
9853
9854static void
cc59ec59
MS
9855catch_exec_command_1 (char *arg, int from_tty,
9856 struct cmd_list_element *command)
c906108c 9857{
b4d90040 9858 struct exec_catchpoint *c;
a6d9a66e 9859 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9860 int tempflag;
c5aa993b 9861 char *cond_string = NULL;
c906108c 9862
44feb3ce
TT
9863 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9864
9865 if (!arg)
9866 arg = "";
e9cafbcc 9867 arg = skip_spaces (arg);
c906108c
SS
9868
9869 /* The allowed syntax is:
c5aa993b
JM
9870 catch exec
9871 catch exec if <cond>
c906108c 9872
4a64f543 9873 First, check if there's an if clause. */
c906108c
SS
9874 cond_string = ep_parse_optional_if_clause (&arg);
9875
9876 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9877 error (_("Junk at end of arguments."));
c906108c 9878
b4d90040
PA
9879 c = XNEW (struct exec_catchpoint);
9880 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
9881 &catch_exec_breakpoint_ops);
9882 c->exec_pathname = NULL;
9883
3ea46bff 9884 install_breakpoint (0, &c->base, 1);
c906108c 9885}
c5aa993b 9886
3086aeae 9887static enum print_stop_action
348d480f 9888print_it_exception_catchpoint (bpstat bs)
3086aeae 9889{
79a45e25 9890 struct ui_out *uiout = current_uiout;
348d480f 9891 struct breakpoint *b = bs->breakpoint_at;
ade92717 9892 int bp_temp, bp_throw;
3086aeae 9893
ade92717 9894 annotate_catchpoint (b->number);
3086aeae 9895
ade92717
AR
9896 bp_throw = strstr (b->addr_string, "throw") != NULL;
9897 if (b->loc->address != b->loc->requested_address)
9898 breakpoint_adjustment_warning (b->loc->requested_address,
9899 b->loc->address,
9900 b->number, 1);
df2b6d2d 9901 bp_temp = b->disposition == disp_del;
ade92717
AR
9902 ui_out_text (uiout,
9903 bp_temp ? "Temporary catchpoint "
9904 : "Catchpoint ");
9905 if (!ui_out_is_mi_like_p (uiout))
9906 ui_out_field_int (uiout, "bkptno", b->number);
9907 ui_out_text (uiout,
c0b37c48
AR
9908 bp_throw ? " (exception thrown), "
9909 : " (exception caught), ");
ade92717
AR
9910 if (ui_out_is_mi_like_p (uiout))
9911 {
9912 ui_out_field_string (uiout, "reason",
9913 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9914 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9915 ui_out_field_int (uiout, "bkptno", b->number);
9916 }
3086aeae
DJ
9917 return PRINT_SRC_AND_LOC;
9918}
9919
9920static void
cc59ec59
MS
9921print_one_exception_catchpoint (struct breakpoint *b,
9922 struct bp_location **last_loc)
3086aeae 9923{
79a45b7d 9924 struct value_print_options opts;
79a45e25 9925 struct ui_out *uiout = current_uiout;
cc59ec59 9926
79a45b7d
TT
9927 get_user_print_options (&opts);
9928 if (opts.addressprint)
3086aeae
DJ
9929 {
9930 annotate_field (4);
604133b5
AR
9931 if (b->loc == NULL || b->loc->shlib_disabled)
9932 ui_out_field_string (uiout, "addr", "<PENDING>");
9933 else
5af949e3
UW
9934 ui_out_field_core_addr (uiout, "addr",
9935 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9936 }
9937 annotate_field (5);
604133b5 9938 if (b->loc)
a6d9a66e 9939 *last_loc = b->loc;
3086aeae
DJ
9940 if (strstr (b->addr_string, "throw") != NULL)
9941 ui_out_field_string (uiout, "what", "exception throw");
9942 else
9943 ui_out_field_string (uiout, "what", "exception catch");
9944}
9945
9946static void
9947print_mention_exception_catchpoint (struct breakpoint *b)
9948{
79a45e25 9949 struct ui_out *uiout = current_uiout;
ade92717
AR
9950 int bp_temp;
9951 int bp_throw;
9952
df2b6d2d 9953 bp_temp = b->disposition == disp_del;
ade92717
AR
9954 bp_throw = strstr (b->addr_string, "throw") != NULL;
9955 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9956 : _("Catchpoint "));
9957 ui_out_field_int (uiout, "bkptno", b->number);
9958 ui_out_text (uiout, bp_throw ? _(" (throw)")
9959 : _(" (catch)"));
3086aeae
DJ
9960}
9961
6149aea9
PA
9962/* Implement the "print_recreate" breakpoint_ops method for throw and
9963 catch catchpoints. */
9964
9965static void
4a64f543
MS
9966print_recreate_exception_catchpoint (struct breakpoint *b,
9967 struct ui_file *fp)
6149aea9
PA
9968{
9969 int bp_temp;
9970 int bp_throw;
9971
9972 bp_temp = b->disposition == disp_del;
9973 bp_throw = strstr (b->addr_string, "throw") != NULL;
9974 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9975 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 9976 print_recreate_thread (b, fp);
6149aea9
PA
9977}
9978
2060206e 9979static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
9980
9981static int
9982handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9983 enum exception_event_kind ex_event, int from_tty)
9984{
604133b5
AR
9985 char *trigger_func_name;
9986
3086aeae 9987 if (ex_event == EX_EVENT_CATCH)
604133b5 9988 trigger_func_name = "__cxa_begin_catch";
3086aeae 9989 else
604133b5 9990 trigger_func_name = "__cxa_throw";
3086aeae 9991
8cdf0e15
VP
9992 create_breakpoint (get_current_arch (),
9993 trigger_func_name, cond_string, -1,
9994 0 /* condition and thread are valid. */,
0fb4aa4b 9995 tempflag, bp_breakpoint,
8cdf0e15
VP
9996 0,
9997 AUTO_BOOLEAN_TRUE /* pending */,
9998 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9999 1 /* enabled */,
10000 0 /* internal */);
3086aeae 10001
3086aeae
DJ
10002 return 1;
10003}
10004
4a64f543 10005/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
10006
10007static void
fba45db2
KB
10008catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
10009 int tempflag, int from_tty)
c906108c 10010{
c5aa993b 10011 char *cond_string = NULL;
c5aa993b 10012
44feb3ce
TT
10013 if (!arg)
10014 arg = "";
e9cafbcc 10015 arg = skip_spaces (arg);
c5aa993b 10016
c906108c
SS
10017 cond_string = ep_parse_optional_if_clause (&arg);
10018
10019 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 10020 error (_("Junk at end of arguments."));
c906108c 10021
059fb39f
PM
10022 if (ex_event != EX_EVENT_THROW
10023 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 10024 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 10025
3086aeae
DJ
10026 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
10027 return;
10028
8a3fe4f8 10029 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
10030}
10031
44feb3ce
TT
10032/* Implementation of "catch catch" command. */
10033
10034static void
10035catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
10036{
10037 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 10038
44feb3ce
TT
10039 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
10040}
10041
10042/* Implementation of "catch throw" command. */
10043
10044static void
10045catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
10046{
10047 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 10048
44feb3ce
TT
10049 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
10050}
10051
9ac4176b 10052void
28010a5d
PA
10053init_ada_exception_breakpoint (struct breakpoint *b,
10054 struct gdbarch *gdbarch,
10055 struct symtab_and_line sal,
10056 char *addr_string,
c0a91b2b 10057 const struct breakpoint_ops *ops,
28010a5d
PA
10058 int tempflag,
10059 int from_tty)
f7f9143b 10060{
f7f9143b
JB
10061 if (from_tty)
10062 {
5af949e3
UW
10063 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
10064 if (!loc_gdbarch)
10065 loc_gdbarch = gdbarch;
10066
6c95b8df
PA
10067 describe_other_breakpoints (loc_gdbarch,
10068 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
10069 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
10070 version for exception catchpoints, because two catchpoints
10071 used for different exception names will use the same address.
10072 In this case, a "breakpoint ... also set at..." warning is
4a64f543 10073 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 10074 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
10075 the user what type of catchpoint it is. The above is good
10076 enough for now, though. */
10077 }
10078
28010a5d 10079 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
10080
10081 b->enable_state = bp_enabled;
10082 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
10083 b->addr_string = addr_string;
10084 b->language = language_ada;
f7f9143b
JB
10085}
10086
a96d9b2e
SDJ
10087/* Splits the argument using space as delimiter. Returns an xmalloc'd
10088 filter list, or NULL if no filtering is required. */
10089static VEC(int) *
10090catch_syscall_split_args (char *arg)
10091{
10092 VEC(int) *result = NULL;
29d0bb3d 10093 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
10094
10095 while (*arg != '\0')
10096 {
10097 int i, syscall_number;
10098 char *endptr;
10099 char cur_name[128];
10100 struct syscall s;
10101
10102 /* Skip whitespace. */
10103 while (isspace (*arg))
10104 arg++;
10105
10106 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
10107 cur_name[i] = arg[i];
10108 cur_name[i] = '\0';
10109 arg += i;
10110
10111 /* Check if the user provided a syscall name or a number. */
10112 syscall_number = (int) strtol (cur_name, &endptr, 0);
10113 if (*endptr == '\0')
bccd0dd2 10114 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
10115 else
10116 {
10117 /* We have a name. Let's check if it's valid and convert it
10118 to a number. */
10119 get_syscall_by_name (cur_name, &s);
10120
10121 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
10122 /* Here we have to issue an error instead of a warning,
10123 because GDB cannot do anything useful if there's no
10124 syscall number to be caught. */
a96d9b2e
SDJ
10125 error (_("Unknown syscall name '%s'."), cur_name);
10126 }
10127
10128 /* Ok, it's valid. */
10129 VEC_safe_push (int, result, s.number);
10130 }
10131
10132 discard_cleanups (cleanup);
10133 return result;
10134}
10135
10136/* Implement the "catch syscall" command. */
10137
10138static void
cc59ec59
MS
10139catch_syscall_command_1 (char *arg, int from_tty,
10140 struct cmd_list_element *command)
a96d9b2e
SDJ
10141{
10142 int tempflag;
10143 VEC(int) *filter;
10144 struct syscall s;
10145 struct gdbarch *gdbarch = get_current_arch ();
10146
10147 /* Checking if the feature if supported. */
10148 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
10149 error (_("The feature 'catch syscall' is not supported on \
ea666128 10150this architecture yet."));
a96d9b2e
SDJ
10151
10152 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
10153
e9cafbcc 10154 arg = skip_spaces (arg);
a96d9b2e
SDJ
10155
10156 /* We need to do this first "dummy" translation in order
10157 to get the syscall XML file loaded or, most important,
10158 to display a warning to the user if there's no XML file
10159 for his/her architecture. */
10160 get_syscall_by_number (0, &s);
10161
10162 /* The allowed syntax is:
10163 catch syscall
10164 catch syscall <name | number> [<name | number> ... <name | number>]
10165
10166 Let's check if there's a syscall name. */
10167
10168 if (arg != NULL)
10169 filter = catch_syscall_split_args (arg);
10170 else
10171 filter = NULL;
10172
10173 create_syscall_event_catchpoint (tempflag, filter,
10174 &catch_syscall_breakpoint_ops);
10175}
10176
c906108c 10177static void
fba45db2 10178catch_command (char *arg, int from_tty)
c906108c 10179{
44feb3ce 10180 error (_("Catch requires an event name."));
c906108c
SS
10181}
10182\f
10183
10184static void
fba45db2 10185tcatch_command (char *arg, int from_tty)
c906108c 10186{
44feb3ce 10187 error (_("Catch requires an event name."));
c906108c
SS
10188}
10189
80f8a6eb 10190/* Delete breakpoints by address or line. */
c906108c
SS
10191
10192static void
fba45db2 10193clear_command (char *arg, int from_tty)
c906108c 10194{
d6e956e5
VP
10195 struct breakpoint *b;
10196 VEC(breakpoint_p) *found = 0;
10197 int ix;
c906108c
SS
10198 int default_match;
10199 struct symtabs_and_lines sals;
10200 struct symtab_and_line sal;
c906108c
SS
10201 int i;
10202
10203 if (arg)
10204 {
10205 sals = decode_line_spec (arg, 1);
10206 default_match = 0;
10207 }
10208 else
10209 {
c5aa993b 10210 sals.sals = (struct symtab_and_line *)
c906108c 10211 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 10212 make_cleanup (xfree, sals.sals);
4a64f543 10213 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
10214
10215 /* Set sal's line, symtab, pc, and pspace to the values
10216 corresponding to the last call to print_frame_info. If the
10217 codepoint is not valid, this will set all the fields to 0. */
10218 get_last_displayed_sal (&sal);
c906108c 10219 if (sal.symtab == 0)
8a3fe4f8 10220 error (_("No source file specified."));
c906108c
SS
10221
10222 sals.sals[0] = sal;
10223 sals.nelts = 1;
10224
10225 default_match = 1;
10226 }
10227
4a64f543
MS
10228 /* We don't call resolve_sal_pc here. That's not as bad as it
10229 seems, because all existing breakpoints typically have both
10230 file/line and pc set. So, if clear is given file/line, we can
10231 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10232
10233 We only support clearing given the address explicitly
10234 present in breakpoint table. Say, we've set breakpoint
4a64f543 10235 at file:line. There were several PC values for that file:line,
ed0616c6 10236 due to optimization, all in one block.
4a64f543
MS
10237
10238 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10239 PC corresponding to the same file:line, the breakpoint won't
10240 be cleared. We probably can still clear the breakpoint, but
10241 since the other PC value is never presented to user, user
10242 can only find it by guessing, and it does not seem important
10243 to support that. */
10244
4a64f543
MS
10245 /* For each line spec given, delete bps which correspond to it. Do
10246 it in two passes, solely to preserve the current behavior that
10247 from_tty is forced true if we delete more than one
10248 breakpoint. */
c906108c 10249
80f8a6eb 10250 found = NULL;
c906108c
SS
10251 for (i = 0; i < sals.nelts; i++)
10252 {
10253 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
10254 If line given (pc == 0), clear all bpts on specified line.
10255 If defaulting, clear all bpts on default line
c906108c 10256 or at default pc.
c5aa993b
JM
10257
10258 defaulting sal.pc != 0 tests to do
10259
10260 0 1 pc
10261 1 1 pc _and_ line
10262 0 0 line
10263 1 0 <can't happen> */
c906108c
SS
10264
10265 sal = sals.sals[i];
c906108c 10266
4a64f543 10267 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 10268 ALL_BREAKPOINTS (b)
c5aa993b 10269 {
0d381245 10270 int match = 0;
4a64f543 10271 /* Are we going to delete b? */
cc60f2e3 10272 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
10273 {
10274 struct bp_location *loc = b->loc;
10275 for (; loc; loc = loc->next)
10276 {
6c95b8df
PA
10277 int pc_match = sal.pc
10278 && (loc->pspace == sal.pspace)
0d381245
VP
10279 && (loc->address == sal.pc)
10280 && (!section_is_overlay (loc->section)
10281 || loc->section == sal.section);
10282 int line_match = ((default_match || (0 == sal.pc))
10283 && b->source_file != NULL
10284 && sal.symtab != NULL
6c95b8df 10285 && sal.pspace == loc->pspace
0ba1096a
KT
10286 && filename_cmp (b->source_file,
10287 sal.symtab->filename) == 0
0d381245
VP
10288 && b->line_number == sal.line);
10289 if (pc_match || line_match)
10290 {
10291 match = 1;
10292 break;
10293 }
10294 }
10295 }
10296
10297 if (match)
d6e956e5 10298 VEC_safe_push(breakpoint_p, found, b);
c906108c 10299 }
80f8a6eb
MS
10300 }
10301 /* Now go thru the 'found' chain and delete them. */
d6e956e5 10302 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
10303 {
10304 if (arg)
8a3fe4f8 10305 error (_("No breakpoint at %s."), arg);
80f8a6eb 10306 else
8a3fe4f8 10307 error (_("No breakpoint at this line."));
80f8a6eb 10308 }
c906108c 10309
d6e956e5 10310 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 10311 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10312 if (from_tty)
a3f17187 10313 {
d6e956e5 10314 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
10315 printf_unfiltered (_("Deleted breakpoint "));
10316 else
10317 printf_unfiltered (_("Deleted breakpoints "));
10318 }
80f8a6eb 10319 breakpoints_changed ();
d6e956e5
VP
10320
10321 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 10322 {
c5aa993b 10323 if (from_tty)
d6e956e5
VP
10324 printf_unfiltered ("%d ", b->number);
10325 delete_breakpoint (b);
c906108c 10326 }
80f8a6eb
MS
10327 if (from_tty)
10328 putchar_unfiltered ('\n');
c906108c
SS
10329}
10330\f
10331/* Delete breakpoint in BS if they are `delete' breakpoints and
10332 all breakpoints that are marked for deletion, whether hit or not.
10333 This is called after any breakpoint is hit, or after errors. */
10334
10335void
fba45db2 10336breakpoint_auto_delete (bpstat bs)
c906108c 10337{
35df4500 10338 struct breakpoint *b, *b_tmp;
c906108c
SS
10339
10340 for (; bs; bs = bs->next)
f431efe5
PA
10341 if (bs->breakpoint_at
10342 && bs->breakpoint_at->disposition == disp_del
c906108c 10343 && bs->stop)
f431efe5 10344 delete_breakpoint (bs->breakpoint_at);
c906108c 10345
35df4500 10346 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10347 {
b5de0fa7 10348 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10349 delete_breakpoint (b);
10350 }
c906108c
SS
10351}
10352
4a64f543
MS
10353/* A comparison function for bp_location AP and BP being interfaced to
10354 qsort. Sort elements primarily by their ADDRESS (no matter what
10355 does breakpoint_address_is_meaningful say for its OWNER),
10356 secondarily by ordering first bp_permanent OWNERed elements and
10357 terciarily just ensuring the array is sorted stable way despite
e5dd4106 10358 qsort being an unstable algorithm. */
876fa593
JK
10359
10360static int
494cfb0f 10361bp_location_compare (const void *ap, const void *bp)
876fa593 10362{
494cfb0f
JK
10363 struct bp_location *a = *(void **) ap;
10364 struct bp_location *b = *(void **) bp;
2bdf28a0 10365 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10366 int a_perm = a->owner->enable_state == bp_permanent;
10367 int b_perm = b->owner->enable_state == bp_permanent;
10368
10369 if (a->address != b->address)
10370 return (a->address > b->address) - (a->address < b->address);
10371
10372 /* Sort permanent breakpoints first. */
10373 if (a_perm != b_perm)
10374 return (a_perm < b_perm) - (a_perm > b_perm);
10375
4a64f543
MS
10376 /* Make the user-visible order stable across GDB runs. Locations of
10377 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10378
10379 if (a->owner->number != b->owner->number)
10380 return (a->owner->number > b->owner->number)
10381 - (a->owner->number < b->owner->number);
10382
10383 return (a > b) - (a < b);
10384}
10385
876fa593 10386/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10387 bp_location_shadow_len_after_address_max according to the current
10388 content of the bp_location array. */
f7545552
TT
10389
10390static void
876fa593 10391bp_location_target_extensions_update (void)
f7545552 10392{
876fa593
JK
10393 struct bp_location *bl, **blp_tmp;
10394
10395 bp_location_placed_address_before_address_max = 0;
10396 bp_location_shadow_len_after_address_max = 0;
10397
10398 ALL_BP_LOCATIONS (bl, blp_tmp)
10399 {
10400 CORE_ADDR start, end, addr;
10401
10402 if (!bp_location_has_shadow (bl))
10403 continue;
10404
10405 start = bl->target_info.placed_address;
10406 end = start + bl->target_info.shadow_len;
10407
10408 gdb_assert (bl->address >= start);
10409 addr = bl->address - start;
10410 if (addr > bp_location_placed_address_before_address_max)
10411 bp_location_placed_address_before_address_max = addr;
10412
10413 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10414
10415 gdb_assert (bl->address < end);
10416 addr = end - bl->address;
10417 if (addr > bp_location_shadow_len_after_address_max)
10418 bp_location_shadow_len_after_address_max = addr;
10419 }
f7545552
TT
10420}
10421
1e4d1764
YQ
10422/* Download tracepoint locations if they haven't been. */
10423
10424static void
10425download_tracepoint_locations (void)
10426{
10427 struct bp_location *bl, **blp_tmp;
10428 struct cleanup *old_chain;
10429
10430 if (!target_can_download_tracepoint ())
10431 return;
10432
10433 old_chain = save_current_space_and_thread ();
10434
10435 ALL_BP_LOCATIONS (bl, blp_tmp)
10436 {
10437 struct tracepoint *t;
10438
10439 if (!is_tracepoint (bl->owner))
10440 continue;
10441
10442 if ((bl->owner->type == bp_fast_tracepoint
10443 ? !may_insert_fast_tracepoints
10444 : !may_insert_tracepoints))
10445 continue;
10446
10447 /* In tracepoint, locations are _never_ duplicated, so
10448 should_be_inserted is equivalent to
10449 unduplicated_should_be_inserted. */
10450 if (!should_be_inserted (bl) || bl->inserted)
10451 continue;
10452
10453 switch_to_program_space_and_thread (bl->pspace);
10454
10455 target_download_tracepoint (bl);
10456
10457 bl->inserted = 1;
10458 t = (struct tracepoint *) bl->owner;
10459 t->number_on_target = bl->owner->number;
10460 }
10461
10462 do_cleanups (old_chain);
10463}
10464
934709f0
PW
10465/* Swap the insertion/duplication state between two locations. */
10466
10467static void
10468swap_insertion (struct bp_location *left, struct bp_location *right)
10469{
10470 const int left_inserted = left->inserted;
10471 const int left_duplicate = left->duplicate;
10472 const struct bp_target_info left_target_info = left->target_info;
10473
1e4d1764
YQ
10474 /* Locations of tracepoints can never be duplicated. */
10475 if (is_tracepoint (left->owner))
10476 gdb_assert (!left->duplicate);
10477 if (is_tracepoint (right->owner))
10478 gdb_assert (!right->duplicate);
10479
934709f0
PW
10480 left->inserted = right->inserted;
10481 left->duplicate = right->duplicate;
10482 left->target_info = right->target_info;
10483 right->inserted = left_inserted;
10484 right->duplicate = left_duplicate;
10485 right->target_info = left_target_info;
10486}
10487
4cd9bd08 10488/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10489 into the inferior, only remove already-inserted locations that no
10490 longer should be inserted. Functions that delete a breakpoint or
10491 breakpoints should pass false, so that deleting a breakpoint
10492 doesn't have the side effect of inserting the locations of other
10493 breakpoints that are marked not-inserted, but should_be_inserted
10494 returns true on them.
10495
10496 This behaviour is useful is situations close to tear-down -- e.g.,
10497 after an exec, while the target still has execution, but breakpoint
10498 shadows of the previous executable image should *NOT* be restored
10499 to the new image; or before detaching, where the target still has
10500 execution and wants to delete breakpoints from GDB's lists, and all
10501 breakpoints had already been removed from the inferior. */
10502
0d381245 10503static void
b60e7edf 10504update_global_location_list (int should_insert)
0d381245 10505{
74960c60 10506 struct breakpoint *b;
876fa593 10507 struct bp_location **locp, *loc;
f7545552
TT
10508 struct cleanup *cleanups;
10509
2d134ed3
PA
10510 /* Used in the duplicates detection below. When iterating over all
10511 bp_locations, points to the first bp_location of a given address.
10512 Breakpoints and watchpoints of different types are never
10513 duplicates of each other. Keep one pointer for each type of
10514 breakpoint/watchpoint, so we only need to loop over all locations
10515 once. */
10516 struct bp_location *bp_loc_first; /* breakpoint */
10517 struct bp_location *wp_loc_first; /* hardware watchpoint */
10518 struct bp_location *awp_loc_first; /* access watchpoint */
10519 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10520
4a64f543
MS
10521 /* Saved former bp_location array which we compare against the newly
10522 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10523 struct bp_location **old_location, **old_locp;
10524 unsigned old_location_count;
10525
10526 old_location = bp_location;
10527 old_location_count = bp_location_count;
10528 bp_location = NULL;
10529 bp_location_count = 0;
10530 cleanups = make_cleanup (xfree, old_location);
0d381245 10531
74960c60 10532 ALL_BREAKPOINTS (b)
876fa593
JK
10533 for (loc = b->loc; loc; loc = loc->next)
10534 bp_location_count++;
10535
10536 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10537 locp = bp_location;
10538 ALL_BREAKPOINTS (b)
10539 for (loc = b->loc; loc; loc = loc->next)
10540 *locp++ = loc;
10541 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10542 bp_location_compare);
876fa593
JK
10543
10544 bp_location_target_extensions_update ();
74960c60 10545
4a64f543
MS
10546 /* Identify bp_location instances that are no longer present in the
10547 new list, and therefore should be freed. Note that it's not
10548 necessary that those locations should be removed from inferior --
10549 if there's another location at the same address (previously
10550 marked as duplicate), we don't need to remove/insert the
10551 location.
876fa593 10552
4a64f543
MS
10553 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10554 and former bp_location array state respectively. */
876fa593
JK
10555
10556 locp = bp_location;
10557 for (old_locp = old_location; old_locp < old_location + old_location_count;
10558 old_locp++)
74960c60 10559 {
876fa593 10560 struct bp_location *old_loc = *old_locp;
c7d46a38 10561 struct bp_location **loc2p;
876fa593 10562
e5dd4106 10563 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 10564 not, we have to free it. */
c7d46a38 10565 int found_object = 0;
20874c92
VP
10566 /* Tells if the location should remain inserted in the target. */
10567 int keep_in_target = 0;
10568 int removed = 0;
876fa593 10569
4a64f543
MS
10570 /* Skip LOCP entries which will definitely never be needed.
10571 Stop either at or being the one matching OLD_LOC. */
876fa593 10572 while (locp < bp_location + bp_location_count
c7d46a38 10573 && (*locp)->address < old_loc->address)
876fa593 10574 locp++;
c7d46a38
PA
10575
10576 for (loc2p = locp;
10577 (loc2p < bp_location + bp_location_count
10578 && (*loc2p)->address == old_loc->address);
10579 loc2p++)
10580 {
10581 if (*loc2p == old_loc)
10582 {
10583 found_object = 1;
10584 break;
10585 }
10586 }
74960c60 10587
4a64f543
MS
10588 /* If this location is no longer present, and inserted, look if
10589 there's maybe a new location at the same address. If so,
10590 mark that one inserted, and don't remove this one. This is
10591 needed so that we don't have a time window where a breakpoint
10592 at certain location is not inserted. */
74960c60 10593
876fa593 10594 if (old_loc->inserted)
0d381245 10595 {
4a64f543
MS
10596 /* If the location is inserted now, we might have to remove
10597 it. */
74960c60 10598
876fa593 10599 if (found_object && should_be_inserted (old_loc))
74960c60 10600 {
4a64f543
MS
10601 /* The location is still present in the location list,
10602 and still should be inserted. Don't do anything. */
20874c92 10603 keep_in_target = 1;
74960c60
VP
10604 }
10605 else
10606 {
4a64f543
MS
10607 /* The location is either no longer present, or got
10608 disabled. See if there's another location at the
10609 same address, in which case we don't need to remove
10610 this one from the target. */
876fa593 10611
2bdf28a0 10612 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10613 if (breakpoint_address_is_meaningful (old_loc->owner))
10614 {
876fa593 10615 for (loc2p = locp;
c7d46a38
PA
10616 (loc2p < bp_location + bp_location_count
10617 && (*loc2p)->address == old_loc->address);
876fa593
JK
10618 loc2p++)
10619 {
10620 struct bp_location *loc2 = *loc2p;
10621
2d134ed3 10622 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 10623 {
85d721b8
PA
10624 /* Read watchpoint locations are switched to
10625 access watchpoints, if the former are not
10626 supported, but the latter are. */
10627 if (is_hardware_watchpoint (old_loc->owner))
10628 {
10629 gdb_assert (is_hardware_watchpoint (loc2->owner));
10630 loc2->watchpoint_type = old_loc->watchpoint_type;
10631 }
10632
934709f0
PW
10633 /* loc2 is a duplicated location. We need to check
10634 if it should be inserted in case it will be
10635 unduplicated. */
10636 if (loc2 != old_loc
10637 && unduplicated_should_be_inserted (loc2))
c7d46a38 10638 {
934709f0 10639 swap_insertion (old_loc, loc2);
c7d46a38
PA
10640 keep_in_target = 1;
10641 break;
10642 }
876fa593
JK
10643 }
10644 }
10645 }
74960c60
VP
10646 }
10647
20874c92
VP
10648 if (!keep_in_target)
10649 {
876fa593 10650 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10651 {
4a64f543
MS
10652 /* This is just about all we can do. We could keep
10653 this location on the global list, and try to
10654 remove it next time, but there's no particular
10655 reason why we will succeed next time.
20874c92 10656
4a64f543
MS
10657 Note that at this point, old_loc->owner is still
10658 valid, as delete_breakpoint frees the breakpoint
10659 only after calling us. */
3e43a32a
MS
10660 printf_filtered (_("warning: Error removing "
10661 "breakpoint %d\n"),
876fa593 10662 old_loc->owner->number);
20874c92
VP
10663 }
10664 removed = 1;
10665 }
0d381245 10666 }
74960c60
VP
10667
10668 if (!found_object)
1c5cfe86 10669 {
db82e815
PA
10670 if (removed && non_stop
10671 && breakpoint_address_is_meaningful (old_loc->owner)
10672 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10673 {
db82e815
PA
10674 /* This location was removed from the target. In
10675 non-stop mode, a race condition is possible where
10676 we've removed a breakpoint, but stop events for that
10677 breakpoint are already queued and will arrive later.
10678 We apply an heuristic to be able to distinguish such
10679 SIGTRAPs from other random SIGTRAPs: we keep this
10680 breakpoint location for a bit, and will retire it
10681 after we see some number of events. The theory here
10682 is that reporting of events should, "on the average",
10683 be fair, so after a while we'll see events from all
10684 threads that have anything of interest, and no longer
10685 need to keep this breakpoint location around. We
10686 don't hold locations forever so to reduce chances of
10687 mistaking a non-breakpoint SIGTRAP for a breakpoint
10688 SIGTRAP.
10689
10690 The heuristic failing can be disastrous on
10691 decr_pc_after_break targets.
10692
10693 On decr_pc_after_break targets, like e.g., x86-linux,
10694 if we fail to recognize a late breakpoint SIGTRAP,
10695 because events_till_retirement has reached 0 too
10696 soon, we'll fail to do the PC adjustment, and report
10697 a random SIGTRAP to the user. When the user resumes
10698 the inferior, it will most likely immediately crash
2dec564e 10699 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10700 corrupted, because of being resumed e.g., in the
10701 middle of a multi-byte instruction, or skipped a
10702 one-byte instruction. This was actually seen happen
10703 on native x86-linux, and should be less rare on
10704 targets that do not support new thread events, like
10705 remote, due to the heuristic depending on
10706 thread_count.
10707
10708 Mistaking a random SIGTRAP for a breakpoint trap
10709 causes similar symptoms (PC adjustment applied when
10710 it shouldn't), but then again, playing with SIGTRAPs
10711 behind the debugger's back is asking for trouble.
10712
10713 Since hardware watchpoint traps are always
10714 distinguishable from other traps, so we don't need to
10715 apply keep hardware watchpoint moribund locations
10716 around. We simply always ignore hardware watchpoint
10717 traps we can no longer explain. */
10718
876fa593
JK
10719 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10720 old_loc->owner = NULL;
20874c92 10721
876fa593 10722 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10723 }
10724 else
f431efe5
PA
10725 {
10726 old_loc->owner = NULL;
10727 decref_bp_location (&old_loc);
10728 }
20874c92 10729 }
74960c60 10730 }
1c5cfe86 10731
348d480f
PA
10732 /* Rescan breakpoints at the same address and section, marking the
10733 first one as "first" and any others as "duplicates". This is so
10734 that the bpt instruction is only inserted once. If we have a
10735 permanent breakpoint at the same place as BPT, make that one the
10736 official one, and the rest as duplicates. Permanent breakpoints
10737 are sorted first for the same address.
10738
10739 Do the same for hardware watchpoints, but also considering the
10740 watchpoint's type (regular/access/read) and length. */
10741
10742 bp_loc_first = NULL;
10743 wp_loc_first = NULL;
10744 awp_loc_first = NULL;
10745 rwp_loc_first = NULL;
10746 ALL_BP_LOCATIONS (loc, locp)
10747 {
10748 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10749 non-NULL. */
10750 struct breakpoint *b = loc->owner;
10751 struct bp_location **loc_first_p;
10752
10753 if (b->enable_state == bp_disabled
10754 || b->enable_state == bp_call_disabled
10755 || b->enable_state == bp_startup_disabled
10756 || !loc->enabled
10757 || loc->shlib_disabled
10758 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
10759 /* Don't detect duplicate for tracepoint locations because they are
10760 never duplicated. See the comments in field `duplicate' of
10761 `struct bp_location'. */
348d480f
PA
10762 || is_tracepoint (b))
10763 continue;
10764
10765 /* Permanent breakpoint should always be inserted. */
10766 if (b->enable_state == bp_permanent && ! loc->inserted)
10767 internal_error (__FILE__, __LINE__,
10768 _("allegedly permanent breakpoint is not "
10769 "actually inserted"));
10770
10771 if (b->type == bp_hardware_watchpoint)
10772 loc_first_p = &wp_loc_first;
10773 else if (b->type == bp_read_watchpoint)
10774 loc_first_p = &rwp_loc_first;
10775 else if (b->type == bp_access_watchpoint)
10776 loc_first_p = &awp_loc_first;
10777 else
10778 loc_first_p = &bp_loc_first;
10779
10780 if (*loc_first_p == NULL
10781 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10782 || !breakpoint_locations_match (loc, *loc_first_p))
10783 {
10784 *loc_first_p = loc;
10785 loc->duplicate = 0;
10786 continue;
10787 }
10788
934709f0
PW
10789
10790 /* This and the above ensure the invariant that the first location
10791 is not duplicated, and is the inserted one.
10792 All following are marked as duplicated, and are not inserted. */
10793 if (loc->inserted)
10794 swap_insertion (loc, *loc_first_p);
348d480f
PA
10795 loc->duplicate = 1;
10796
10797 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10798 && b->enable_state != bp_permanent)
10799 internal_error (__FILE__, __LINE__,
10800 _("another breakpoint was inserted on top of "
10801 "a permanent breakpoint"));
10802 }
10803
10804 if (breakpoints_always_inserted_mode () && should_insert
10805 && (have_live_inferiors ()
10806 || (gdbarch_has_global_breakpoints (target_gdbarch))))
10807 insert_breakpoint_locations ();
10808
1e4d1764
YQ
10809 if (should_insert)
10810 download_tracepoint_locations ();
10811
348d480f
PA
10812 do_cleanups (cleanups);
10813}
10814
10815void
10816breakpoint_retire_moribund (void)
10817{
10818 struct bp_location *loc;
10819 int ix;
10820
10821 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10822 if (--(loc->events_till_retirement) == 0)
10823 {
10824 decref_bp_location (&loc);
10825 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10826 --ix;
10827 }
10828}
10829
10830static void
10831update_global_location_list_nothrow (int inserting)
10832{
10833 struct gdb_exception e;
10834
10835 TRY_CATCH (e, RETURN_MASK_ERROR)
10836 update_global_location_list (inserting);
10837}
10838
10839/* Clear BKP from a BPS. */
10840
10841static void
10842bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10843{
10844 bpstat bs;
10845
10846 for (bs = bps; bs; bs = bs->next)
10847 if (bs->breakpoint_at == bpt)
10848 {
10849 bs->breakpoint_at = NULL;
10850 bs->old_val = NULL;
10851 /* bs->commands will be freed later. */
10852 }
10853}
10854
10855/* Callback for iterate_over_threads. */
10856static int
10857bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10858{
10859 struct breakpoint *bpt = data;
10860
10861 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10862 return 0;
10863}
10864
10865/* Helper for breakpoint and tracepoint breakpoint_ops->mention
10866 callbacks. */
10867
10868static void
10869say_where (struct breakpoint *b)
10870{
79a45e25 10871 struct ui_out *uiout = current_uiout;
348d480f
PA
10872 struct value_print_options opts;
10873
10874 get_user_print_options (&opts);
10875
10876 /* i18n: cagney/2005-02-11: Below needs to be merged into a
10877 single string. */
10878 if (b->loc == NULL)
10879 {
10880 printf_filtered (_(" (%s) pending."), b->addr_string);
10881 }
10882 else
10883 {
10884 if (opts.addressprint || b->source_file == NULL)
10885 {
10886 printf_filtered (" at ");
10887 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
10888 gdb_stdout);
10889 }
10890 if (b->source_file)
10891 printf_filtered (": file %s, line %d.",
10892 b->source_file, b->line_number);
10893
10894 if (b->loc->next)
10895 {
10896 struct bp_location *loc = b->loc;
10897 int n = 0;
10898 for (; loc; loc = loc->next)
10899 ++n;
10900 printf_filtered (" (%d locations)", n);
10901 }
10902 }
10903}
10904
348d480f
PA
10905/* Default bp_location_ops methods. */
10906
10907static void
10908bp_location_dtor (struct bp_location *self)
10909{
10910 xfree (self->cond);
10911 xfree (self->function_name);
10912}
10913
10914static const struct bp_location_ops bp_location_ops =
10915{
10916 bp_location_dtor
10917};
10918
2060206e
PA
10919/* Default breakpoint_ops methods all breakpoint_ops ultimately
10920 inherit from. */
348d480f 10921
2060206e
PA
10922static void
10923base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
10924{
10925 decref_counted_command_line (&self->commands);
10926 xfree (self->cond_string);
348d480f
PA
10927 xfree (self->addr_string);
10928 xfree (self->addr_string_range_end);
348d480f
PA
10929 xfree (self->source_file);
10930}
10931
2060206e
PA
10932static struct bp_location *
10933base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
10934{
10935 struct bp_location *loc;
10936
10937 loc = XNEW (struct bp_location);
10938 init_bp_location (loc, &bp_location_ops, self);
10939 return loc;
10940}
10941
2060206e
PA
10942static void
10943base_breakpoint_re_set (struct breakpoint *b)
10944{
10945 /* Nothing to re-set. */
10946}
10947
10948#define internal_error_pure_virtual_called() \
10949 gdb_assert_not_reached ("pure virtual function called")
10950
10951static int
10952base_breakpoint_insert_location (struct bp_location *bl)
10953{
10954 internal_error_pure_virtual_called ();
10955}
10956
10957static int
10958base_breakpoint_remove_location (struct bp_location *bl)
10959{
10960 internal_error_pure_virtual_called ();
10961}
10962
10963static int
10964base_breakpoint_breakpoint_hit (const struct bp_location *bl,
10965 struct address_space *aspace,
10966 CORE_ADDR bp_addr)
10967{
10968 internal_error_pure_virtual_called ();
10969}
10970
10971static void
10972base_breakpoint_check_status (bpstat bs)
10973{
10974 /* Always stop. */
10975}
10976
10977/* A "works_in_software_mode" breakpoint_ops method that just internal
10978 errors. */
10979
10980static int
10981base_breakpoint_works_in_software_mode (const struct breakpoint *b)
10982{
10983 internal_error_pure_virtual_called ();
10984}
10985
10986/* A "resources_needed" breakpoint_ops method that just internal
10987 errors. */
10988
10989static int
10990base_breakpoint_resources_needed (const struct bp_location *bl)
10991{
10992 internal_error_pure_virtual_called ();
10993}
10994
10995static enum print_stop_action
10996base_breakpoint_print_it (bpstat bs)
10997{
10998 internal_error_pure_virtual_called ();
10999}
11000
11001static void
11002base_breakpoint_print_one_detail (const struct breakpoint *self,
11003 struct ui_out *uiout)
11004{
11005 /* nothing */
11006}
11007
11008static void
11009base_breakpoint_print_mention (struct breakpoint *b)
11010{
11011 internal_error_pure_virtual_called ();
11012}
11013
11014static void
11015base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
11016{
11017 internal_error_pure_virtual_called ();
11018}
11019
11020static struct breakpoint_ops base_breakpoint_ops =
11021{
11022 base_breakpoint_dtor,
11023 base_breakpoint_allocate_location,
11024 base_breakpoint_re_set,
11025 base_breakpoint_insert_location,
11026 base_breakpoint_remove_location,
11027 base_breakpoint_breakpoint_hit,
11028 base_breakpoint_check_status,
11029 base_breakpoint_resources_needed,
11030 base_breakpoint_works_in_software_mode,
11031 base_breakpoint_print_it,
11032 NULL,
11033 base_breakpoint_print_one_detail,
11034 base_breakpoint_print_mention,
11035 base_breakpoint_print_recreate
11036};
11037
11038/* Default breakpoint_ops methods. */
11039
11040static void
348d480f
PA
11041bkpt_re_set (struct breakpoint *b)
11042{
06edf0c0
PA
11043 /* Do not attempt to re-set breakpoints disabled during startup. */
11044 if (b->enable_state == bp_startup_disabled)
11045 return;
348d480f 11046
06edf0c0
PA
11047 /* FIXME: is this still reachable? */
11048 if (b->addr_string == NULL)
11049 {
11050 /* Anything without a string can't be re-set. */
348d480f 11051 delete_breakpoint (b);
06edf0c0 11052 return;
348d480f 11053 }
06edf0c0
PA
11054
11055 breakpoint_re_set_default (b);
348d480f
PA
11056}
11057
2060206e 11058static int
348d480f
PA
11059bkpt_insert_location (struct bp_location *bl)
11060{
11061 if (bl->loc_type == bp_loc_hardware_breakpoint)
11062 return target_insert_hw_breakpoint (bl->gdbarch,
11063 &bl->target_info);
11064 else
11065 return target_insert_breakpoint (bl->gdbarch,
11066 &bl->target_info);
11067}
11068
2060206e 11069static int
348d480f
PA
11070bkpt_remove_location (struct bp_location *bl)
11071{
11072 if (bl->loc_type == bp_loc_hardware_breakpoint)
11073 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11074 else
11075 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
11076}
11077
2060206e 11078static int
348d480f
PA
11079bkpt_breakpoint_hit (const struct bp_location *bl,
11080 struct address_space *aspace, CORE_ADDR bp_addr)
11081{
11082 struct breakpoint *b = bl->owner;
11083
11084 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11085 aspace, bp_addr))
11086 return 0;
11087
11088 if (overlay_debugging /* unmapped overlay section */
11089 && section_is_overlay (bl->section)
11090 && !section_is_mapped (bl->section))
11091 return 0;
11092
11093 return 1;
11094}
11095
2060206e 11096static int
348d480f
PA
11097bkpt_resources_needed (const struct bp_location *bl)
11098{
11099 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
11100
11101 return 1;
11102}
11103
2060206e 11104static enum print_stop_action
348d480f
PA
11105bkpt_print_it (bpstat bs)
11106{
348d480f
PA
11107 struct breakpoint *b;
11108 const struct bp_location *bl;
001c8c33 11109 int bp_temp;
79a45e25 11110 struct ui_out *uiout = current_uiout;
348d480f
PA
11111
11112 gdb_assert (bs->bp_location_at != NULL);
11113
11114 bl = bs->bp_location_at;
11115 b = bs->breakpoint_at;
11116
001c8c33
PA
11117 bp_temp = b->disposition == disp_del;
11118 if (bl->address != bl->requested_address)
11119 breakpoint_adjustment_warning (bl->requested_address,
11120 bl->address,
11121 b->number, 1);
11122 annotate_breakpoint (b->number);
11123 if (bp_temp)
11124 ui_out_text (uiout, "\nTemporary breakpoint ");
11125 else
11126 ui_out_text (uiout, "\nBreakpoint ");
11127 if (ui_out_is_mi_like_p (uiout))
348d480f 11128 {
001c8c33
PA
11129 ui_out_field_string (uiout, "reason",
11130 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11131 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 11132 }
001c8c33
PA
11133 ui_out_field_int (uiout, "bkptno", b->number);
11134 ui_out_text (uiout, ", ");
06edf0c0 11135
001c8c33 11136 return PRINT_SRC_AND_LOC;
06edf0c0
PA
11137}
11138
2060206e 11139static void
06edf0c0
PA
11140bkpt_print_mention (struct breakpoint *b)
11141{
79a45e25 11142 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
11143 return;
11144
11145 switch (b->type)
11146 {
11147 case bp_breakpoint:
11148 case bp_gnu_ifunc_resolver:
11149 if (b->disposition == disp_del)
11150 printf_filtered (_("Temporary breakpoint"));
11151 else
11152 printf_filtered (_("Breakpoint"));
11153 printf_filtered (_(" %d"), b->number);
11154 if (b->type == bp_gnu_ifunc_resolver)
11155 printf_filtered (_(" at gnu-indirect-function resolver"));
11156 break;
11157 case bp_hardware_breakpoint:
11158 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
11159 break;
11160 }
11161
11162 say_where (b);
11163}
11164
2060206e 11165static void
06edf0c0
PA
11166bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
11167{
11168 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11169 fprintf_unfiltered (fp, "tbreak");
11170 else if (tp->type == bp_breakpoint)
11171 fprintf_unfiltered (fp, "break");
11172 else if (tp->type == bp_hardware_breakpoint
11173 && tp->disposition == disp_del)
11174 fprintf_unfiltered (fp, "thbreak");
11175 else if (tp->type == bp_hardware_breakpoint)
11176 fprintf_unfiltered (fp, "hbreak");
11177 else
11178 internal_error (__FILE__, __LINE__,
11179 _("unhandled breakpoint type %d"), (int) tp->type);
11180
2060206e 11181 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 11182 print_recreate_thread (tp, fp);
06edf0c0
PA
11183}
11184
06edf0c0
PA
11185/* Virtual table for internal breakpoints. */
11186
11187static void
11188internal_bkpt_re_set (struct breakpoint *b)
11189{
11190 switch (b->type)
11191 {
11192 /* Delete overlay event and longjmp master breakpoints; they
11193 will be reset later by breakpoint_re_set. */
11194 case bp_overlay_event:
11195 case bp_longjmp_master:
11196 case bp_std_terminate_master:
11197 case bp_exception_master:
11198 delete_breakpoint (b);
11199 break;
11200
11201 /* This breakpoint is special, it's set up when the inferior
11202 starts and we really don't want to touch it. */
11203 case bp_shlib_event:
11204
11205 /* Like bp_shlib_event, this breakpoint type is special. Once
11206 it is set up, we do not want to touch it. */
11207 case bp_thread_event:
11208 break;
11209 }
11210}
11211
11212static void
11213internal_bkpt_check_status (bpstat bs)
11214{
11215 /* We do not stop for these. */
11216 bs->stop = 0;
11217}
11218
11219static enum print_stop_action
11220internal_bkpt_print_it (bpstat bs)
11221{
36dfb11c 11222 struct ui_out *uiout = current_uiout;
06edf0c0 11223 struct breakpoint *b;
06edf0c0 11224
06edf0c0
PA
11225 b = bs->breakpoint_at;
11226
06edf0c0
PA
11227 switch (b->type)
11228 {
348d480f
PA
11229 case bp_shlib_event:
11230 /* Did we stop because the user set the stop_on_solib_events
11231 variable? (If so, we report this as a generic, "Stopped due
11232 to shlib event" message.) */
36dfb11c
TT
11233 ui_out_text (uiout, _("Stopped due to shared library event\n"));
11234 if (ui_out_is_mi_like_p (uiout))
11235 ui_out_field_string (uiout, "reason",
11236 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
348d480f
PA
11237 break;
11238
11239 case bp_thread_event:
11240 /* Not sure how we will get here.
11241 GDB should not stop for these breakpoints. */
11242 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11243 break;
11244
11245 case bp_overlay_event:
11246 /* By analogy with the thread event, GDB should not stop for these. */
11247 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11248 break;
11249
11250 case bp_longjmp_master:
11251 /* These should never be enabled. */
11252 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11253 break;
11254
11255 case bp_std_terminate_master:
11256 /* These should never be enabled. */
11257 printf_filtered (_("std::terminate Master Breakpoint: "
11258 "gdb should not stop!\n"));
348d480f
PA
11259 break;
11260
11261 case bp_exception_master:
11262 /* These should never be enabled. */
11263 printf_filtered (_("Exception Master Breakpoint: "
11264 "gdb should not stop!\n"));
06edf0c0
PA
11265 break;
11266 }
11267
001c8c33 11268 return PRINT_NOTHING;
06edf0c0
PA
11269}
11270
11271static void
11272internal_bkpt_print_mention (struct breakpoint *b)
11273{
11274 /* Nothing to mention. These breakpoints are internal. */
11275}
11276
06edf0c0
PA
11277/* Virtual table for momentary breakpoints */
11278
11279static void
11280momentary_bkpt_re_set (struct breakpoint *b)
11281{
11282 /* Keep temporary breakpoints, which can be encountered when we step
11283 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
11284 Otherwise these should have been blown away via the cleanup chain
11285 or by breakpoint_init_inferior when we rerun the executable. */
11286}
11287
11288static void
11289momentary_bkpt_check_status (bpstat bs)
11290{
11291 /* Nothing. The point of these breakpoints is causing a stop. */
11292}
11293
11294static enum print_stop_action
11295momentary_bkpt_print_it (bpstat bs)
11296{
79a45e25
PA
11297 struct ui_out *uiout = current_uiout;
11298
001c8c33 11299 if (ui_out_is_mi_like_p (uiout))
06edf0c0 11300 {
001c8c33 11301 struct breakpoint *b = bs->breakpoint_at;
348d480f 11302
001c8c33
PA
11303 switch (b->type)
11304 {
11305 case bp_finish:
11306 ui_out_field_string
11307 (uiout, "reason",
11308 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
11309 break;
348d480f 11310
001c8c33
PA
11311 case bp_until:
11312 ui_out_field_string
11313 (uiout, "reason",
11314 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
11315 break;
11316 }
348d480f
PA
11317 }
11318
001c8c33 11319 return PRINT_UNKNOWN;
348d480f
PA
11320}
11321
06edf0c0
PA
11322static void
11323momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 11324{
06edf0c0 11325 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11326}
11327
348d480f 11328/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 11329
348d480f
PA
11330static void
11331tracepoint_re_set (struct breakpoint *b)
11332{
11333 breakpoint_re_set_default (b);
11334}
876fa593 11335
348d480f
PA
11336static int
11337tracepoint_breakpoint_hit (const struct bp_location *bl,
11338 struct address_space *aspace, CORE_ADDR bp_addr)
11339{
11340 /* By definition, the inferior does not report stops at
11341 tracepoints. */
11342 return 0;
74960c60
VP
11343}
11344
11345static void
348d480f
PA
11346tracepoint_print_one_detail (const struct breakpoint *self,
11347 struct ui_out *uiout)
74960c60 11348{
d9b3f62e
PA
11349 struct tracepoint *tp = (struct tracepoint *) self;
11350 if (tp->static_trace_marker_id)
348d480f
PA
11351 {
11352 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 11353
348d480f
PA
11354 ui_out_text (uiout, "\tmarker id is ");
11355 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 11356 tp->static_trace_marker_id);
348d480f
PA
11357 ui_out_text (uiout, "\n");
11358 }
0d381245
VP
11359}
11360
a474d7c2 11361static void
348d480f 11362tracepoint_print_mention (struct breakpoint *b)
a474d7c2 11363{
79a45e25 11364 if (ui_out_is_mi_like_p (current_uiout))
348d480f 11365 return;
cc59ec59 11366
348d480f
PA
11367 switch (b->type)
11368 {
11369 case bp_tracepoint:
11370 printf_filtered (_("Tracepoint"));
11371 printf_filtered (_(" %d"), b->number);
11372 break;
11373 case bp_fast_tracepoint:
11374 printf_filtered (_("Fast tracepoint"));
11375 printf_filtered (_(" %d"), b->number);
11376 break;
11377 case bp_static_tracepoint:
11378 printf_filtered (_("Static tracepoint"));
11379 printf_filtered (_(" %d"), b->number);
11380 break;
11381 default:
11382 internal_error (__FILE__, __LINE__,
11383 _("unhandled tracepoint type %d"), (int) b->type);
11384 }
11385
11386 say_where (b);
a474d7c2
PA
11387}
11388
348d480f 11389static void
d9b3f62e 11390tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 11391{
d9b3f62e
PA
11392 struct tracepoint *tp = (struct tracepoint *) self;
11393
11394 if (self->type == bp_fast_tracepoint)
348d480f 11395 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 11396 if (self->type == bp_static_tracepoint)
348d480f 11397 fprintf_unfiltered (fp, "strace");
d9b3f62e 11398 else if (self->type == bp_tracepoint)
348d480f
PA
11399 fprintf_unfiltered (fp, "trace");
11400 else
11401 internal_error (__FILE__, __LINE__,
d9b3f62e 11402 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 11403
d9b3f62e
PA
11404 fprintf_unfiltered (fp, " %s", self->addr_string);
11405 print_recreate_thread (self, fp);
11406
11407 if (tp->pass_count)
11408 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
11409}
11410
2060206e 11411struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 11412
53a5351d 11413/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 11414 structures. */
c906108c
SS
11415
11416void
fba45db2 11417delete_breakpoint (struct breakpoint *bpt)
c906108c 11418{
52f0bd74 11419 struct breakpoint *b;
c906108c 11420
8a3fe4f8 11421 gdb_assert (bpt != NULL);
c906108c 11422
4a64f543
MS
11423 /* Has this bp already been deleted? This can happen because
11424 multiple lists can hold pointers to bp's. bpstat lists are
11425 especial culprits.
11426
11427 One example of this happening is a watchpoint's scope bp. When
11428 the scope bp triggers, we notice that the watchpoint is out of
11429 scope, and delete it. We also delete its scope bp. But the
11430 scope bp is marked "auto-deleting", and is already on a bpstat.
11431 That bpstat is then checked for auto-deleting bp's, which are
11432 deleted.
11433
11434 A real solution to this problem might involve reference counts in
11435 bp's, and/or giving them pointers back to their referencing
11436 bpstat's, and teaching delete_breakpoint to only free a bp's
11437 storage when no more references were extent. A cheaper bandaid
11438 was chosen. */
c906108c
SS
11439 if (bpt->type == bp_none)
11440 return;
11441
4a64f543
MS
11442 /* At least avoid this stale reference until the reference counting
11443 of breakpoints gets resolved. */
d0fb5eae 11444 if (bpt->related_breakpoint != bpt)
e5a0a904 11445 {
d0fb5eae 11446 struct breakpoint *related;
3a5c3e22 11447 struct watchpoint *w;
d0fb5eae
JK
11448
11449 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 11450 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 11451 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
11452 w = (struct watchpoint *) bpt;
11453 else
11454 w = NULL;
11455 if (w != NULL)
11456 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
11457
11458 /* Unlink bpt from the bpt->related_breakpoint ring. */
11459 for (related = bpt; related->related_breakpoint != bpt;
11460 related = related->related_breakpoint);
11461 related->related_breakpoint = bpt->related_breakpoint;
11462 bpt->related_breakpoint = bpt;
e5a0a904
JK
11463 }
11464
a9634178
TJB
11465 /* watch_command_1 creates a watchpoint but only sets its number if
11466 update_watchpoint succeeds in creating its bp_locations. If there's
11467 a problem in that process, we'll be asked to delete the half-created
11468 watchpoint. In that case, don't announce the deletion. */
11469 if (bpt->number)
11470 observer_notify_breakpoint_deleted (bpt);
c906108c 11471
c906108c
SS
11472 if (breakpoint_chain == bpt)
11473 breakpoint_chain = bpt->next;
11474
c906108c
SS
11475 ALL_BREAKPOINTS (b)
11476 if (b->next == bpt)
c5aa993b
JM
11477 {
11478 b->next = bpt->next;
11479 break;
11480 }
c906108c 11481
f431efe5
PA
11482 /* Be sure no bpstat's are pointing at the breakpoint after it's
11483 been freed. */
11484 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 11485 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
11486 pointing at bpt from the stop_bpstat list entirely, as breakpoint
11487 commands are associated with the bpstat; if we remove it here,
11488 then the later call to bpstat_do_actions (&stop_bpstat); in
11489 event-top.c won't do anything, and temporary breakpoints with
11490 commands won't work. */
11491
11492 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11493
4a64f543
MS
11494 /* Now that breakpoint is removed from breakpoint list, update the
11495 global location list. This will remove locations that used to
11496 belong to this breakpoint. Do this before freeing the breakpoint
11497 itself, since remove_breakpoint looks at location's owner. It
11498 might be better design to have location completely
11499 self-contained, but it's not the case now. */
b60e7edf 11500 update_global_location_list (0);
74960c60 11501
348d480f 11502 bpt->ops->dtor (bpt);
4a64f543
MS
11503 /* On the chance that someone will soon try again to delete this
11504 same bp, we mark it as deleted before freeing its storage. */
c906108c 11505 bpt->type = bp_none;
b8c9b27d 11506 xfree (bpt);
c906108c
SS
11507}
11508
4d6140d9
AC
11509static void
11510do_delete_breakpoint_cleanup (void *b)
11511{
11512 delete_breakpoint (b);
11513}
11514
11515struct cleanup *
11516make_cleanup_delete_breakpoint (struct breakpoint *b)
11517{
11518 return make_cleanup (do_delete_breakpoint_cleanup, b);
11519}
11520
51be5b68
PA
11521/* Iterator function to call a user-provided callback function once
11522 for each of B and its related breakpoints. */
11523
11524static void
11525iterate_over_related_breakpoints (struct breakpoint *b,
11526 void (*function) (struct breakpoint *,
11527 void *),
11528 void *data)
11529{
11530 struct breakpoint *related;
11531
11532 related = b;
11533 do
11534 {
11535 struct breakpoint *next;
11536
11537 /* FUNCTION may delete RELATED. */
11538 next = related->related_breakpoint;
11539
11540 if (next == related)
11541 {
11542 /* RELATED is the last ring entry. */
11543 function (related, data);
11544
11545 /* FUNCTION may have deleted it, so we'd never reach back to
11546 B. There's nothing left to do anyway, so just break
11547 out. */
11548 break;
11549 }
11550 else
11551 function (related, data);
11552
11553 related = next;
11554 }
11555 while (related != b);
11556}
95a42b64
TT
11557
11558static void
11559do_delete_breakpoint (struct breakpoint *b, void *ignore)
11560{
11561 delete_breakpoint (b);
11562}
11563
51be5b68
PA
11564/* A callback for map_breakpoint_numbers that calls
11565 delete_breakpoint. */
11566
11567static void
11568do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
11569{
11570 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
11571}
11572
c906108c 11573void
fba45db2 11574delete_command (char *arg, int from_tty)
c906108c 11575{
35df4500 11576 struct breakpoint *b, *b_tmp;
c906108c 11577
ea9365bb
TT
11578 dont_repeat ();
11579
c906108c
SS
11580 if (arg == 0)
11581 {
11582 int breaks_to_delete = 0;
11583
46c6471b
PA
11584 /* Delete all breakpoints if no argument. Do not delete
11585 internal breakpoints, these have to be deleted with an
11586 explicit breakpoint number argument. */
c5aa993b 11587 ALL_BREAKPOINTS (b)
46c6471b 11588 if (user_breakpoint_p (b))
973d738b
DJ
11589 {
11590 breaks_to_delete = 1;
11591 break;
11592 }
c906108c
SS
11593
11594 /* Ask user only if there are some breakpoints to delete. */
11595 if (!from_tty
e2e0b3e5 11596 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 11597 {
35df4500 11598 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 11599 if (user_breakpoint_p (b))
c5aa993b 11600 delete_breakpoint (b);
c906108c
SS
11601 }
11602 }
11603 else
51be5b68 11604 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
11605}
11606
0d381245
VP
11607static int
11608all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 11609{
0d381245
VP
11610 for (; loc; loc = loc->next)
11611 if (!loc->shlib_disabled)
11612 return 0;
11613 return 1;
fe3f5fa8
VP
11614}
11615
776592bf
DE
11616/* Subroutine of update_breakpoint_locations to simplify it.
11617 Return non-zero if multiple fns in list LOC have the same name.
11618 Null names are ignored. */
11619
11620static int
11621ambiguous_names_p (struct bp_location *loc)
11622{
11623 struct bp_location *l;
11624 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
11625 (int (*) (const void *,
11626 const void *)) streq,
776592bf
DE
11627 NULL, xcalloc, xfree);
11628
11629 for (l = loc; l != NULL; l = l->next)
11630 {
11631 const char **slot;
11632 const char *name = l->function_name;
11633
11634 /* Allow for some names to be NULL, ignore them. */
11635 if (name == NULL)
11636 continue;
11637
11638 slot = (const char **) htab_find_slot (htab, (const void *) name,
11639 INSERT);
4a64f543
MS
11640 /* NOTE: We can assume slot != NULL here because xcalloc never
11641 returns NULL. */
776592bf
DE
11642 if (*slot != NULL)
11643 {
11644 htab_delete (htab);
11645 return 1;
11646 }
11647 *slot = name;
11648 }
11649
11650 htab_delete (htab);
11651 return 0;
11652}
11653
0fb4aa4b
PA
11654/* When symbols change, it probably means the sources changed as well,
11655 and it might mean the static tracepoint markers are no longer at
11656 the same address or line numbers they used to be at last we
11657 checked. Losing your static tracepoints whenever you rebuild is
11658 undesirable. This function tries to resync/rematch gdb static
11659 tracepoints with the markers on the target, for static tracepoints
11660 that have not been set by marker id. Static tracepoint that have
11661 been set by marker id are reset by marker id in breakpoint_re_set.
11662 The heuristic is:
11663
11664 1) For a tracepoint set at a specific address, look for a marker at
11665 the old PC. If one is found there, assume to be the same marker.
11666 If the name / string id of the marker found is different from the
11667 previous known name, assume that means the user renamed the marker
11668 in the sources, and output a warning.
11669
11670 2) For a tracepoint set at a given line number, look for a marker
11671 at the new address of the old line number. If one is found there,
11672 assume to be the same marker. If the name / string id of the
11673 marker found is different from the previous known name, assume that
11674 means the user renamed the marker in the sources, and output a
11675 warning.
11676
11677 3) If a marker is no longer found at the same address or line, it
11678 may mean the marker no longer exists. But it may also just mean
11679 the code changed a bit. Maybe the user added a few lines of code
11680 that made the marker move up or down (in line number terms). Ask
11681 the target for info about the marker with the string id as we knew
11682 it. If found, update line number and address in the matching
11683 static tracepoint. This will get confused if there's more than one
11684 marker with the same ID (possible in UST, although unadvised
11685 precisely because it confuses tools). */
11686
11687static struct symtab_and_line
11688update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11689{
d9b3f62e 11690 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
11691 struct static_tracepoint_marker marker;
11692 CORE_ADDR pc;
11693 int i;
11694
11695 pc = sal.pc;
11696 if (sal.line)
11697 find_line_pc (sal.symtab, sal.line, &pc);
11698
11699 if (target_static_tracepoint_marker_at (pc, &marker))
11700 {
d9b3f62e 11701 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
11702 warning (_("static tracepoint %d changed probed marker from %s to %s"),
11703 b->number,
d9b3f62e 11704 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 11705
d9b3f62e
PA
11706 xfree (tp->static_trace_marker_id);
11707 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
11708 release_static_tracepoint_marker (&marker);
11709
11710 return sal;
11711 }
11712
11713 /* Old marker wasn't found on target at lineno. Try looking it up
11714 by string ID. */
11715 if (!sal.explicit_pc
11716 && sal.line != 0
11717 && sal.symtab != NULL
d9b3f62e 11718 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
11719 {
11720 VEC(static_tracepoint_marker_p) *markers;
11721
11722 markers
d9b3f62e 11723 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
11724
11725 if (!VEC_empty(static_tracepoint_marker_p, markers))
11726 {
11727 struct symtab_and_line sal;
11728 struct symbol *sym;
11729 struct static_tracepoint_marker *marker;
79a45e25 11730 struct ui_out *uiout = current_uiout;
0fb4aa4b
PA
11731
11732 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
11733
d9b3f62e
PA
11734 xfree (tp->static_trace_marker_id);
11735 tp->static_trace_marker_id = xstrdup (marker->str_id);
0fb4aa4b
PA
11736
11737 warning (_("marker for static tracepoint %d (%s) not "
11738 "found at previous line number"),
d9b3f62e 11739 b->number, tp->static_trace_marker_id);
0fb4aa4b
PA
11740
11741 init_sal (&sal);
11742
11743 sal.pc = marker->address;
11744
11745 sal = find_pc_line (marker->address, 0);
11746 sym = find_pc_sect_function (marker->address, NULL);
11747 ui_out_text (uiout, "Now in ");
11748 if (sym)
11749 {
11750 ui_out_field_string (uiout, "func",
11751 SYMBOL_PRINT_NAME (sym));
11752 ui_out_text (uiout, " at ");
11753 }
11754 ui_out_field_string (uiout, "file", sal.symtab->filename);
11755 ui_out_text (uiout, ":");
11756
11757 if (ui_out_is_mi_like_p (uiout))
11758 {
11759 char *fullname = symtab_to_fullname (sal.symtab);
11760
11761 if (fullname)
11762 ui_out_field_string (uiout, "fullname", fullname);
11763 }
11764
11765 ui_out_field_int (uiout, "line", sal.line);
11766 ui_out_text (uiout, "\n");
11767
11768 b->line_number = sal.line;
11769
11770 xfree (b->source_file);
11771 if (sym)
11772 b->source_file = xstrdup (sal.symtab->filename);
11773 else
11774 b->source_file = NULL;
11775
11776 xfree (b->addr_string);
11777 b->addr_string = xstrprintf ("%s:%d",
11778 sal.symtab->filename, b->line_number);
11779
11780 /* Might be nice to check if function changed, and warn if
11781 so. */
11782
11783 release_static_tracepoint_marker (marker);
11784 }
11785 }
11786 return sal;
11787}
11788
8d3788bd
VP
11789/* Returns 1 iff locations A and B are sufficiently same that
11790 we don't need to report breakpoint as changed. */
11791
11792static int
11793locations_are_equal (struct bp_location *a, struct bp_location *b)
11794{
11795 while (a && b)
11796 {
11797 if (a->address != b->address)
11798 return 0;
11799
11800 if (a->shlib_disabled != b->shlib_disabled)
11801 return 0;
11802
11803 if (a->enabled != b->enabled)
11804 return 0;
11805
11806 a = a->next;
11807 b = b->next;
11808 }
11809
11810 if ((a == NULL) != (b == NULL))
11811 return 0;
11812
11813 return 1;
11814}
11815
f1310107
TJB
11816/* Create new breakpoint locations for B (a hardware or software breakpoint)
11817 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
11818 a ranged breakpoint. */
11819
0e30163f 11820void
0d381245 11821update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
11822 struct symtabs_and_lines sals,
11823 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
11824{
11825 int i;
0d381245
VP
11826 struct bp_location *existing_locations = b->loc;
11827
f1310107
TJB
11828 /* Ranged breakpoints have only one start location and one end location. */
11829 gdb_assert (sals_end.nelts == 0 || (sals.nelts == 1 && sals_end.nelts == 1));
11830
4a64f543
MS
11831 /* If there's no new locations, and all existing locations are
11832 pending, don't do anything. This optimizes the common case where
11833 all locations are in the same shared library, that was unloaded.
11834 We'd like to retain the location, so that when the library is
11835 loaded again, we don't loose the enabled/disabled status of the
11836 individual locations. */
0d381245 11837 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
11838 return;
11839
fe3f5fa8
VP
11840 b->loc = NULL;
11841
0d381245 11842 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 11843 {
0d381245 11844 struct bp_location *new_loc =
39d61571 11845 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 11846
0d381245
VP
11847 /* Reparse conditions, they might contain references to the
11848 old symtab. */
11849 if (b->cond_string != NULL)
11850 {
f1310107 11851 char *s;
0d381245 11852 struct gdb_exception e;
fe3f5fa8 11853
0d381245
VP
11854 s = b->cond_string;
11855 TRY_CATCH (e, RETURN_MASK_ERROR)
11856 {
11857 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
11858 0);
11859 }
11860 if (e.reason < 0)
11861 {
3e43a32a
MS
11862 warning (_("failed to reevaluate condition "
11863 "for breakpoint %d: %s"),
0d381245
VP
11864 b->number, e.message);
11865 new_loc->enabled = 0;
11866 }
11867 }
fe3f5fa8 11868
0d381245
VP
11869 if (b->source_file != NULL)
11870 xfree (b->source_file);
11871 if (sals.sals[i].symtab == NULL)
11872 b->source_file = NULL;
11873 else
1b36a34b 11874 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 11875
0d381245
VP
11876 if (b->line_number == 0)
11877 b->line_number = sals.sals[i].line;
f1310107
TJB
11878
11879 if (sals_end.nelts)
11880 {
11881 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11882
11883 new_loc->length = end - sals.sals[0].pc + 1;
11884 }
0d381245 11885 }
fe3f5fa8 11886
514f746b
AR
11887 /* Update locations of permanent breakpoints. */
11888 if (b->enable_state == bp_permanent)
11889 make_breakpoint_permanent (b);
11890
4a64f543
MS
11891 /* If possible, carry over 'disable' status from existing
11892 breakpoints. */
0d381245
VP
11893 {
11894 struct bp_location *e = existing_locations;
776592bf
DE
11895 /* If there are multiple breakpoints with the same function name,
11896 e.g. for inline functions, comparing function names won't work.
11897 Instead compare pc addresses; this is just a heuristic as things
11898 may have moved, but in practice it gives the correct answer
11899 often enough until a better solution is found. */
11900 int have_ambiguous_names = ambiguous_names_p (b->loc);
11901
0d381245
VP
11902 for (; e; e = e->next)
11903 {
11904 if (!e->enabled && e->function_name)
11905 {
11906 struct bp_location *l = b->loc;
776592bf
DE
11907 if (have_ambiguous_names)
11908 {
11909 for (; l; l = l->next)
f1310107 11910 if (breakpoint_locations_match (e, l))
776592bf
DE
11911 {
11912 l->enabled = 0;
11913 break;
11914 }
11915 }
11916 else
11917 {
11918 for (; l; l = l->next)
11919 if (l->function_name
11920 && strcmp (e->function_name, l->function_name) == 0)
11921 {
11922 l->enabled = 0;
11923 break;
11924 }
11925 }
0d381245
VP
11926 }
11927 }
11928 }
fe3f5fa8 11929
8d3788bd
VP
11930 if (!locations_are_equal (existing_locations, b->loc))
11931 observer_notify_breakpoint_modified (b);
11932
b60e7edf 11933 update_global_location_list (1);
fe3f5fa8
VP
11934}
11935
ef23e705
TJB
11936/* Find the SaL locations corresponding to the given ADDR_STRING.
11937 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
11938
11939static struct symtabs_and_lines
11940addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11941{
11942 char *s;
58438ac1 11943 int marker_spec;
02d20e4a 11944 struct symtabs_and_lines sals = {0};
ef23e705
TJB
11945 struct gdb_exception e;
11946
11947 s = addr_string;
11948 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11949
11950 TRY_CATCH (e, RETURN_MASK_ERROR)
11951 {
11952 if (marker_spec)
11953 {
d9b3f62e
PA
11954 struct tracepoint *tp = (struct tracepoint *) b;
11955
ef23e705 11956 sals = decode_static_tracepoint_spec (&s);
d9b3f62e 11957 if (sals.nelts > tp->static_trace_marker_id_idx)
ef23e705 11958 {
d9b3f62e 11959 sals.sals[0] = sals.sals[tp->static_trace_marker_id_idx];
ef23e705
TJB
11960 sals.nelts = 1;
11961 }
11962 else
d9b3f62e 11963 error (_("marker %s not found"), tp->static_trace_marker_id);
ef23e705
TJB
11964 }
11965 else
58438ac1 11966 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, NULL);
ef23e705
TJB
11967 }
11968 if (e.reason < 0)
11969 {
11970 int not_found_and_ok = 0;
11971 /* For pending breakpoints, it's expected that parsing will
11972 fail until the right shared library is loaded. User has
11973 already told to create pending breakpoints and don't need
11974 extra messages. If breakpoint is in bp_shlib_disabled
11975 state, then user already saw the message about that
11976 breakpoint being disabled, and don't want to see more
11977 errors. */
58438ac1 11978 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
11979 && (b->condition_not_parsed
11980 || (b->loc && b->loc->shlib_disabled)
11981 || b->enable_state == bp_disabled))
11982 not_found_and_ok = 1;
11983
11984 if (!not_found_and_ok)
11985 {
11986 /* We surely don't want to warn about the same breakpoint
11987 10 times. One solution, implemented here, is disable
11988 the breakpoint on error. Another solution would be to
11989 have separate 'warning emitted' flag. Since this
11990 happens only when a binary has changed, I don't know
11991 which approach is better. */
11992 b->enable_state = bp_disabled;
11993 throw_exception (e);
11994 }
11995 }
11996
58438ac1 11997 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705
TJB
11998 {
11999 gdb_assert (sals.nelts == 1);
12000
12001 resolve_sal_pc (&sals.sals[0]);
12002 if (b->condition_not_parsed && s && s[0])
12003 {
12004 char *cond_string = 0;
12005 int thread = -1;
12006 int task = 0;
12007
12008 find_condition_and_thread (s, sals.sals[0].pc,
12009 &cond_string, &thread, &task);
12010 if (cond_string)
12011 b->cond_string = cond_string;
12012 b->thread = thread;
12013 b->task = task;
12014 b->condition_not_parsed = 0;
12015 }
12016
12017 if (b->type == bp_static_tracepoint && !marker_spec)
12018 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 12019
58438ac1
TT
12020 *found = 1;
12021 }
12022 else
12023 *found = 0;
ef23e705
TJB
12024
12025 return sals;
12026}
12027
348d480f
PA
12028/* The default re_set method, for typical hardware or software
12029 breakpoints. Reevaluate the breakpoint and recreate its
12030 locations. */
12031
12032static void
28010a5d 12033breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
12034{
12035 int found;
f1310107 12036 struct symtabs_and_lines sals, sals_end;
ef23e705 12037 struct symtabs_and_lines expanded = {0};
f1310107 12038 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
12039
12040 sals = addr_string_to_sals (b, b->addr_string, &found);
12041 if (found)
12042 {
12043 make_cleanup (xfree, sals.sals);
12044 expanded = expand_line_sal_maybe (sals.sals[0]);
12045 }
12046
f1310107
TJB
12047 if (b->addr_string_range_end)
12048 {
12049 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
12050 if (found)
12051 {
12052 make_cleanup (xfree, sals_end.sals);
12053 expanded_end = expand_line_sal_maybe (sals_end.sals[0]);
12054 }
12055 }
12056
12057 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
12058}
12059
12060/* Prepare the global context for a re-set of breakpoint B. */
12061
12062static struct cleanup *
12063prepare_re_set_context (struct breakpoint *b)
12064{
12065 struct cleanup *cleanups;
12066
12067 input_radix = b->input_radix;
12068 cleanups = save_current_space_and_thread ();
12069 switch_to_program_space_and_thread (b->pspace);
12070 set_language (b->language);
12071
12072 return cleanups;
ef23e705
TJB
12073}
12074
c906108c
SS
12075/* Reset a breakpoint given it's struct breakpoint * BINT.
12076 The value we return ends up being the return value from catch_errors.
12077 Unused in this case. */
12078
12079static int
4efb68b1 12080breakpoint_re_set_one (void *bint)
c906108c 12081{
4a64f543 12082 /* Get past catch_errs. */
53a5351d 12083 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 12084 struct cleanup *cleanups;
c906108c 12085
348d480f
PA
12086 cleanups = prepare_re_set_context (b);
12087 b->ops->re_set (b);
12088 do_cleanups (cleanups);
c906108c
SS
12089 return 0;
12090}
12091
69de3c6a 12092/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 12093void
69de3c6a 12094breakpoint_re_set (void)
c906108c 12095{
35df4500 12096 struct breakpoint *b, *b_tmp;
c906108c
SS
12097 enum language save_language;
12098 int save_input_radix;
6c95b8df 12099 struct cleanup *old_chain;
c5aa993b 12100
c906108c
SS
12101 save_language = current_language->la_language;
12102 save_input_radix = input_radix;
6c95b8df
PA
12103 old_chain = save_current_program_space ();
12104
35df4500 12105 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12106 {
4a64f543 12107 /* Format possible error msg. */
fe3f5fa8 12108 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
12109 b->number);
12110 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 12111 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 12112 do_cleanups (cleanups);
c5aa993b 12113 }
c906108c
SS
12114 set_language (save_language);
12115 input_radix = save_input_radix;
e62c965a 12116
0756c555 12117 jit_breakpoint_re_set ();
4efc6507 12118
6c95b8df
PA
12119 do_cleanups (old_chain);
12120
af02033e
PP
12121 create_overlay_event_breakpoint ();
12122 create_longjmp_master_breakpoint ();
12123 create_std_terminate_master_breakpoint ();
186c406b 12124 create_exception_master_breakpoint ();
1bfeeb0f
JL
12125
12126 /* While we're at it, reset the skip list too. */
12127 skip_re_set ();
c906108c
SS
12128}
12129\f
c906108c
SS
12130/* Reset the thread number of this breakpoint:
12131
12132 - If the breakpoint is for all threads, leave it as-is.
4a64f543 12133 - Else, reset it to the current thread for inferior_ptid. */
c906108c 12134void
fba45db2 12135breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
12136{
12137 if (b->thread != -1)
12138 {
39f77062
KB
12139 if (in_thread_list (inferior_ptid))
12140 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
12141
12142 /* We're being called after following a fork. The new fork is
12143 selected as current, and unless this was a vfork will have a
12144 different program space from the original thread. Reset that
12145 as well. */
12146 b->loc->pspace = current_program_space;
c906108c
SS
12147 }
12148}
12149
03ac34d5
MS
12150/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12151 If from_tty is nonzero, it prints a message to that effect,
12152 which ends with a period (no newline). */
12153
c906108c 12154void
fba45db2 12155set_ignore_count (int bptnum, int count, int from_tty)
c906108c 12156{
52f0bd74 12157 struct breakpoint *b;
c906108c
SS
12158
12159 if (count < 0)
12160 count = 0;
12161
12162 ALL_BREAKPOINTS (b)
12163 if (b->number == bptnum)
c5aa993b 12164 {
d77f58be
SS
12165 if (is_tracepoint (b))
12166 {
12167 if (from_tty && count != 0)
12168 printf_filtered (_("Ignore count ignored for tracepoint %d."),
12169 bptnum);
12170 return;
12171 }
12172
c5aa993b 12173 b->ignore_count = count;
221ea385
KS
12174 if (from_tty)
12175 {
12176 if (count == 0)
3e43a32a
MS
12177 printf_filtered (_("Will stop next time "
12178 "breakpoint %d is reached."),
221ea385
KS
12179 bptnum);
12180 else if (count == 1)
a3f17187 12181 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
12182 bptnum);
12183 else
3e43a32a
MS
12184 printf_filtered (_("Will ignore next %d "
12185 "crossings of breakpoint %d."),
221ea385
KS
12186 count, bptnum);
12187 }
c5aa993b 12188 breakpoints_changed ();
8d3788bd 12189 observer_notify_breakpoint_modified (b);
c5aa993b
JM
12190 return;
12191 }
c906108c 12192
8a3fe4f8 12193 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
12194}
12195
c906108c
SS
12196/* Command to set ignore-count of breakpoint N to COUNT. */
12197
12198static void
fba45db2 12199ignore_command (char *args, int from_tty)
c906108c
SS
12200{
12201 char *p = args;
52f0bd74 12202 int num;
c906108c
SS
12203
12204 if (p == 0)
e2e0b3e5 12205 error_no_arg (_("a breakpoint number"));
c5aa993b 12206
c906108c 12207 num = get_number (&p);
5c44784c 12208 if (num == 0)
8a3fe4f8 12209 error (_("bad breakpoint number: '%s'"), args);
c906108c 12210 if (*p == 0)
8a3fe4f8 12211 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
12212
12213 set_ignore_count (num,
12214 longest_to_int (value_as_long (parse_and_eval (p))),
12215 from_tty);
221ea385
KS
12216 if (from_tty)
12217 printf_filtered ("\n");
c906108c
SS
12218}
12219\f
12220/* Call FUNCTION on each of the breakpoints
12221 whose numbers are given in ARGS. */
12222
12223static void
95a42b64
TT
12224map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
12225 void *),
12226 void *data)
c906108c 12227{
52f0bd74
AC
12228 int num;
12229 struct breakpoint *b, *tmp;
11cf8741 12230 int match;
197f0a60 12231 struct get_number_or_range_state state;
c906108c 12232
197f0a60 12233 if (args == 0)
e2e0b3e5 12234 error_no_arg (_("one or more breakpoint numbers"));
c906108c 12235
197f0a60
TT
12236 init_number_or_range (&state, args);
12237
12238 while (!state.finished)
c906108c 12239 {
197f0a60
TT
12240 char *p = state.string;
12241
11cf8741 12242 match = 0;
c5aa993b 12243
197f0a60 12244 num = get_number_or_range (&state);
5c44784c 12245 if (num == 0)
c5aa993b 12246 {
8a3fe4f8 12247 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
12248 }
12249 else
12250 {
12251 ALL_BREAKPOINTS_SAFE (b, tmp)
12252 if (b->number == num)
12253 {
11cf8741 12254 match = 1;
cdac0397 12255 function (b, data);
11cf8741 12256 break;
5c44784c 12257 }
11cf8741 12258 if (match == 0)
a3f17187 12259 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 12260 }
c906108c
SS
12261 }
12262}
12263
0d381245
VP
12264static struct bp_location *
12265find_location_by_number (char *number)
12266{
12267 char *dot = strchr (number, '.');
12268 char *p1;
12269 int bp_num;
12270 int loc_num;
12271 struct breakpoint *b;
12272 struct bp_location *loc;
12273
12274 *dot = '\0';
12275
12276 p1 = number;
197f0a60 12277 bp_num = get_number (&p1);
0d381245
VP
12278 if (bp_num == 0)
12279 error (_("Bad breakpoint number '%s'"), number);
12280
12281 ALL_BREAKPOINTS (b)
12282 if (b->number == bp_num)
12283 {
12284 break;
12285 }
12286
12287 if (!b || b->number != bp_num)
12288 error (_("Bad breakpoint number '%s'"), number);
12289
12290 p1 = dot+1;
197f0a60 12291 loc_num = get_number (&p1);
0d381245
VP
12292 if (loc_num == 0)
12293 error (_("Bad breakpoint location number '%s'"), number);
12294
12295 --loc_num;
12296 loc = b->loc;
12297 for (;loc_num && loc; --loc_num, loc = loc->next)
12298 ;
12299 if (!loc)
12300 error (_("Bad breakpoint location number '%s'"), dot+1);
12301
12302 return loc;
12303}
12304
12305
1900040c
MS
12306/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12307 If from_tty is nonzero, it prints a message to that effect,
12308 which ends with a period (no newline). */
12309
c906108c 12310void
fba45db2 12311disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
12312{
12313 /* Never disable a watchpoint scope breakpoint; we want to
12314 hit them when we leave scope so we can delete both the
12315 watchpoint and its scope breakpoint at that time. */
12316 if (bpt->type == bp_watchpoint_scope)
12317 return;
12318
c2c6d25f 12319 /* You can't disable permanent breakpoints. */
b5de0fa7 12320 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
12321 return;
12322
b5de0fa7 12323 bpt->enable_state = bp_disabled;
c906108c 12324
d248b706
KY
12325 if (target_supports_enable_disable_tracepoint ()
12326 && current_trace_status ()->running && is_tracepoint (bpt))
12327 {
12328 struct bp_location *location;
12329
12330 for (location = bpt->loc; location; location = location->next)
12331 target_disable_tracepoint (location);
12332 }
12333
b60e7edf 12334 update_global_location_list (0);
c906108c 12335
8d3788bd 12336 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12337}
12338
51be5b68
PA
12339/* A callback for iterate_over_related_breakpoints. */
12340
12341static void
12342do_disable_breakpoint (struct breakpoint *b, void *ignore)
12343{
12344 disable_breakpoint (b);
12345}
12346
95a42b64
TT
12347/* A callback for map_breakpoint_numbers that calls
12348 disable_breakpoint. */
12349
12350static void
12351do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
12352{
51be5b68 12353 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
12354}
12355
c906108c 12356static void
fba45db2 12357disable_command (char *args, int from_tty)
c906108c 12358{
c906108c 12359 if (args == 0)
46c6471b
PA
12360 {
12361 struct breakpoint *bpt;
12362
12363 ALL_BREAKPOINTS (bpt)
12364 if (user_breakpoint_p (bpt))
12365 disable_breakpoint (bpt);
12366 }
0d381245
VP
12367 else if (strchr (args, '.'))
12368 {
12369 struct bp_location *loc = find_location_by_number (args);
12370 if (loc)
d248b706
KY
12371 {
12372 loc->enabled = 0;
12373 if (target_supports_enable_disable_tracepoint ()
12374 && current_trace_status ()->running && loc->owner
12375 && is_tracepoint (loc->owner))
12376 target_disable_tracepoint (loc);
12377 }
b60e7edf 12378 update_global_location_list (0);
0d381245 12379 }
c906108c 12380 else
95a42b64 12381 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
12382}
12383
12384static void
51be5b68 12385enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 12386{
afe38095 12387 int target_resources_ok;
c906108c
SS
12388
12389 if (bpt->type == bp_hardware_breakpoint)
12390 {
12391 int i;
c5aa993b 12392 i = hw_breakpoint_used_count ();
53a5351d 12393 target_resources_ok =
d92524f1 12394 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 12395 i + 1, 0);
c906108c 12396 if (target_resources_ok == 0)
8a3fe4f8 12397 error (_("No hardware breakpoint support in the target."));
c906108c 12398 else if (target_resources_ok < 0)
8a3fe4f8 12399 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
12400 }
12401
cc60f2e3 12402 if (is_watchpoint (bpt))
c906108c 12403 {
d07205c2
JK
12404 /* Initialize it just to avoid a GCC false warning. */
12405 enum enable_state orig_enable_state = 0;
dde02812
ES
12406 struct gdb_exception e;
12407
12408 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 12409 {
3a5c3e22
PA
12410 struct watchpoint *w = (struct watchpoint *) bpt;
12411
1e718ff1
TJB
12412 orig_enable_state = bpt->enable_state;
12413 bpt->enable_state = bp_enabled;
3a5c3e22 12414 update_watchpoint (w, 1 /* reparse */);
c906108c 12415 }
dde02812 12416 if (e.reason < 0)
c5aa993b 12417 {
1e718ff1 12418 bpt->enable_state = orig_enable_state;
dde02812
ES
12419 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12420 bpt->number);
12421 return;
c5aa993b 12422 }
c906108c 12423 }
0101ce28 12424
b4c291bb
KH
12425 if (bpt->enable_state != bp_permanent)
12426 bpt->enable_state = bp_enabled;
d248b706
KY
12427
12428 if (target_supports_enable_disable_tracepoint ()
12429 && current_trace_status ()->running && is_tracepoint (bpt))
12430 {
12431 struct bp_location *location;
12432
12433 for (location = bpt->loc; location; location = location->next)
12434 target_enable_tracepoint (location);
12435 }
12436
b4c291bb 12437 bpt->disposition = disposition;
b60e7edf 12438 update_global_location_list (1);
b4c291bb
KH
12439 breakpoints_changed ();
12440
8d3788bd 12441 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12442}
12443
fe3f5fa8 12444
c906108c 12445void
fba45db2 12446enable_breakpoint (struct breakpoint *bpt)
c906108c 12447{
51be5b68
PA
12448 enable_breakpoint_disp (bpt, bpt->disposition);
12449}
12450
12451static void
12452do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12453{
12454 enable_breakpoint (bpt);
c906108c
SS
12455}
12456
95a42b64
TT
12457/* A callback for map_breakpoint_numbers that calls
12458 enable_breakpoint. */
12459
12460static void
12461do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12462{
51be5b68 12463 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
12464}
12465
c906108c
SS
12466/* The enable command enables the specified breakpoints (or all defined
12467 breakpoints) so they once again become (or continue to be) effective
1272ad14 12468 in stopping the inferior. */
c906108c 12469
c906108c 12470static void
fba45db2 12471enable_command (char *args, int from_tty)
c906108c 12472{
c906108c 12473 if (args == 0)
46c6471b
PA
12474 {
12475 struct breakpoint *bpt;
12476
12477 ALL_BREAKPOINTS (bpt)
12478 if (user_breakpoint_p (bpt))
12479 enable_breakpoint (bpt);
12480 }
0d381245
VP
12481 else if (strchr (args, '.'))
12482 {
12483 struct bp_location *loc = find_location_by_number (args);
12484 if (loc)
d248b706
KY
12485 {
12486 loc->enabled = 1;
12487 if (target_supports_enable_disable_tracepoint ()
12488 && current_trace_status ()->running && loc->owner
12489 && is_tracepoint (loc->owner))
12490 target_enable_tracepoint (loc);
12491 }
b60e7edf 12492 update_global_location_list (1);
0d381245 12493 }
c906108c 12494 else
95a42b64 12495 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
12496}
12497
12498static void
51be5b68
PA
12499do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12500{
12501 enum bpdisp disp = *(enum bpdisp *) arg;
12502
12503 enable_breakpoint_disp (bpt, disp);
12504}
12505
12506static void
12507do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12508{
51be5b68
PA
12509 enum bpdisp disp = disp_disable;
12510
12511 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12512}
12513
c906108c 12514static void
fba45db2 12515enable_once_command (char *args, int from_tty)
c906108c 12516{
51be5b68 12517 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
12518}
12519
12520static void
51be5b68 12521do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12522{
51be5b68
PA
12523 enum bpdisp disp = disp_del;
12524
12525 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12526}
12527
c906108c 12528static void
fba45db2 12529enable_delete_command (char *args, int from_tty)
c906108c 12530{
51be5b68 12531 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
12532}
12533\f
fa8d40ab
JJ
12534static void
12535set_breakpoint_cmd (char *args, int from_tty)
12536{
12537}
12538
12539static void
12540show_breakpoint_cmd (char *args, int from_tty)
12541{
12542}
12543
1f3b5d1b
PP
12544/* Invalidate last known value of any hardware watchpoint if
12545 the memory which that value represents has been written to by
12546 GDB itself. */
12547
12548static void
12549invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12550 const bfd_byte *data)
12551{
12552 struct breakpoint *bp;
12553
12554 ALL_BREAKPOINTS (bp)
12555 if (bp->enable_state == bp_enabled
3a5c3e22 12556 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 12557 {
3a5c3e22 12558 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 12559
3a5c3e22
PA
12560 if (wp->val_valid && wp->val)
12561 {
12562 struct bp_location *loc;
12563
12564 for (loc = bp->loc; loc != NULL; loc = loc->next)
12565 if (loc->loc_type == bp_loc_hardware_watchpoint
12566 && loc->address + loc->length > addr
12567 && addr + len > loc->address)
12568 {
12569 value_free (wp->val);
12570 wp->val = NULL;
12571 wp->val_valid = 0;
12572 }
12573 }
1f3b5d1b
PP
12574 }
12575}
12576
1bfeeb0f
JL
12577/* Use the last displayed codepoint's values, or nothing
12578 if they aren't valid. */
c906108c
SS
12579
12580struct symtabs_and_lines
fba45db2 12581decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
12582{
12583 struct symtabs_and_lines sals;
cc59ec59 12584
c906108c 12585 if (string == 0)
8a3fe4f8 12586 error (_("Empty line specification."));
1bfeeb0f 12587 if (last_displayed_sal_is_valid ())
c906108c 12588 sals = decode_line_1 (&string, funfirstline,
1bfeeb0f
JL
12589 get_last_displayed_symtab (),
12590 get_last_displayed_line (),
58438ac1 12591 NULL);
c906108c
SS
12592 else
12593 sals = decode_line_1 (&string, funfirstline,
58438ac1 12594 (struct symtab *) NULL, 0, NULL);
c906108c 12595 if (*string)
8a3fe4f8 12596 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
12597 return sals;
12598}
8181d85f
DJ
12599
12600/* Create and insert a raw software breakpoint at PC. Return an
12601 identifier, which should be used to remove the breakpoint later.
12602 In general, places which call this should be using something on the
12603 breakpoint chain instead; this function should be eliminated
12604 someday. */
12605
12606void *
6c95b8df
PA
12607deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12608 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
12609{
12610 struct bp_target_info *bp_tgt;
12611
6c95b8df 12612 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 12613
6c95b8df 12614 bp_tgt->placed_address_space = aspace;
8181d85f 12615 bp_tgt->placed_address = pc;
6c95b8df 12616
a6d9a66e 12617 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
12618 {
12619 /* Could not insert the breakpoint. */
12620 xfree (bp_tgt);
12621 return NULL;
12622 }
12623
12624 return bp_tgt;
12625}
12626
4a64f543
MS
12627/* Remove a breakpoint BP inserted by
12628 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
12629
12630int
a6d9a66e 12631deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
12632{
12633 struct bp_target_info *bp_tgt = bp;
12634 int ret;
12635
a6d9a66e 12636 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
12637 xfree (bp_tgt);
12638
12639 return ret;
12640}
12641
4a64f543
MS
12642/* One (or perhaps two) breakpoints used for software single
12643 stepping. */
8181d85f
DJ
12644
12645static void *single_step_breakpoints[2];
a6d9a66e 12646static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
12647
12648/* Create and insert a breakpoint for software single step. */
12649
12650void
6c95b8df 12651insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
12652 struct address_space *aspace,
12653 CORE_ADDR next_pc)
8181d85f
DJ
12654{
12655 void **bpt_p;
12656
12657 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
12658 {
12659 bpt_p = &single_step_breakpoints[0];
12660 single_step_gdbarch[0] = gdbarch;
12661 }
8181d85f
DJ
12662 else
12663 {
12664 gdb_assert (single_step_breakpoints[1] == NULL);
12665 bpt_p = &single_step_breakpoints[1];
a6d9a66e 12666 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
12667 }
12668
4a64f543
MS
12669 /* NOTE drow/2006-04-11: A future improvement to this function would
12670 be to only create the breakpoints once, and actually put them on
12671 the breakpoint chain. That would let us use set_raw_breakpoint.
12672 We could adjust the addresses each time they were needed. Doing
12673 this requires corresponding changes elsewhere where single step
12674 breakpoints are handled, however. So, for now, we use this. */
8181d85f 12675
6c95b8df 12676 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 12677 if (*bpt_p == NULL)
5af949e3
UW
12678 error (_("Could not insert single-step breakpoint at %s"),
12679 paddress (gdbarch, next_pc));
8181d85f
DJ
12680}
12681
f02253f1
HZ
12682/* Check if the breakpoints used for software single stepping
12683 were inserted or not. */
12684
12685int
12686single_step_breakpoints_inserted (void)
12687{
12688 return (single_step_breakpoints[0] != NULL
12689 || single_step_breakpoints[1] != NULL);
12690}
12691
8181d85f
DJ
12692/* Remove and delete any breakpoints used for software single step. */
12693
12694void
12695remove_single_step_breakpoints (void)
12696{
12697 gdb_assert (single_step_breakpoints[0] != NULL);
12698
12699 /* See insert_single_step_breakpoint for more about this deprecated
12700 call. */
a6d9a66e
UW
12701 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12702 single_step_breakpoints[0]);
12703 single_step_gdbarch[0] = NULL;
8181d85f
DJ
12704 single_step_breakpoints[0] = NULL;
12705
12706 if (single_step_breakpoints[1] != NULL)
12707 {
a6d9a66e
UW
12708 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12709 single_step_breakpoints[1]);
12710 single_step_gdbarch[1] = NULL;
8181d85f
DJ
12711 single_step_breakpoints[1] = NULL;
12712 }
12713}
12714
d03285ec
UW
12715/* Delete software single step breakpoints without removing them from
12716 the inferior. This is intended to be used if the inferior's address
12717 space where they were inserted is already gone, e.g. after exit or
12718 exec. */
12719
12720void
12721cancel_single_step_breakpoints (void)
12722{
12723 int i;
12724
12725 for (i = 0; i < 2; i++)
12726 if (single_step_breakpoints[i])
12727 {
12728 xfree (single_step_breakpoints[i]);
12729 single_step_breakpoints[i] = NULL;
12730 single_step_gdbarch[i] = NULL;
12731 }
12732}
12733
12734/* Detach software single-step breakpoints from INFERIOR_PTID without
12735 removing them. */
12736
12737static void
12738detach_single_step_breakpoints (void)
12739{
12740 int i;
12741
12742 for (i = 0; i < 2; i++)
12743 if (single_step_breakpoints[i])
12744 target_remove_breakpoint (single_step_gdbarch[i],
12745 single_step_breakpoints[i]);
12746}
12747
4a64f543
MS
12748/* Check whether a software single-step breakpoint is inserted at
12749 PC. */
1aafd4da
UW
12750
12751static int
cc59ec59
MS
12752single_step_breakpoint_inserted_here_p (struct address_space *aspace,
12753 CORE_ADDR pc)
1aafd4da
UW
12754{
12755 int i;
12756
12757 for (i = 0; i < 2; i++)
12758 {
12759 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
12760 if (bp_tgt
12761 && breakpoint_address_match (bp_tgt->placed_address_space,
12762 bp_tgt->placed_address,
12763 aspace, pc))
1aafd4da
UW
12764 return 1;
12765 }
12766
12767 return 0;
12768}
12769
a96d9b2e
SDJ
12770/* Returns 0 if 'bp' is NOT a syscall catchpoint,
12771 non-zero otherwise. */
12772static int
12773is_syscall_catchpoint_enabled (struct breakpoint *bp)
12774{
12775 if (syscall_catchpoint_p (bp)
12776 && bp->enable_state != bp_disabled
12777 && bp->enable_state != bp_call_disabled)
12778 return 1;
12779 else
12780 return 0;
12781}
12782
12783int
12784catch_syscall_enabled (void)
12785{
12786 struct inferior *inf = current_inferior ();
12787
12788 return inf->total_syscalls_count != 0;
12789}
12790
12791int
12792catching_syscall_number (int syscall_number)
12793{
12794 struct breakpoint *bp;
12795
12796 ALL_BREAKPOINTS (bp)
12797 if (is_syscall_catchpoint_enabled (bp))
12798 {
be5c67c1
PA
12799 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12800
12801 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
12802 {
12803 int i, iter;
12804 for (i = 0;
be5c67c1 12805 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
12806 i++)
12807 if (syscall_number == iter)
12808 return 1;
12809 }
12810 else
12811 return 1;
12812 }
12813
12814 return 0;
12815}
12816
12817/* Complete syscall names. Used by "catch syscall". */
12818static char **
12819catch_syscall_completer (struct cmd_list_element *cmd,
12820 char *text, char *word)
12821{
12822 const char **list = get_syscall_names ();
c38eea1a
MS
12823 char **retlist
12824 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 12825
c38eea1a
MS
12826 xfree (list);
12827 return retlist;
a96d9b2e
SDJ
12828}
12829
1042e4c0
SS
12830/* Tracepoint-specific operations. */
12831
12832/* Set tracepoint count to NUM. */
12833static void
12834set_tracepoint_count (int num)
12835{
12836 tracepoint_count = num;
4fa62494 12837 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
12838}
12839
12840void
12841trace_command (char *arg, int from_tty)
12842{
8cdf0e15
VP
12843 if (create_breakpoint (get_current_arch (),
12844 arg,
12845 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12846 0 /* tempflag */,
12847 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
12848 0 /* Ignore count */,
12849 pending_break_support,
348d480f 12850 &tracepoint_breakpoint_ops,
8cdf0e15 12851 from_tty,
84f4c1fe
PM
12852 1 /* enabled */,
12853 0 /* internal */))
fd9b8c24 12854 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
12855}
12856
7a697b8d
SS
12857void
12858ftrace_command (char *arg, int from_tty)
12859{
8cdf0e15
VP
12860 if (create_breakpoint (get_current_arch (),
12861 arg,
12862 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12863 0 /* tempflag */,
12864 bp_fast_tracepoint /* type_wanted */,
12865 0 /* Ignore count */,
12866 pending_break_support,
348d480f 12867 &tracepoint_breakpoint_ops,
0fb4aa4b 12868 from_tty,
84f4c1fe
PM
12869 1 /* enabled */,
12870 0 /* internal */))
0fb4aa4b
PA
12871 set_tracepoint_count (breakpoint_count);
12872}
12873
12874/* strace command implementation. Creates a static tracepoint. */
12875
12876void
12877strace_command (char *arg, int from_tty)
12878{
12879 if (create_breakpoint (get_current_arch (),
12880 arg,
12881 NULL, 0, 1 /* parse arg */,
12882 0 /* tempflag */,
12883 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
12884 0 /* Ignore count */,
12885 pending_break_support,
348d480f 12886 &tracepoint_breakpoint_ops,
8cdf0e15 12887 from_tty,
84f4c1fe
PM
12888 1 /* enabled */,
12889 0 /* internal */))
fd9b8c24 12890 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
12891}
12892
409873ef
SS
12893/* Set up a fake reader function that gets command lines from a linked
12894 list that was acquired during tracepoint uploading. */
12895
12896static struct uploaded_tp *this_utp;
3149d8c1 12897static int next_cmd;
409873ef
SS
12898
12899static char *
12900read_uploaded_action (void)
12901{
12902 char *rslt;
12903
3149d8c1 12904 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 12905
3149d8c1 12906 next_cmd++;
409873ef
SS
12907
12908 return rslt;
12909}
12910
00bf0b85
SS
12911/* Given information about a tracepoint as recorded on a target (which
12912 can be either a live system or a trace file), attempt to create an
12913 equivalent GDB tracepoint. This is not a reliable process, since
12914 the target does not necessarily have all the information used when
12915 the tracepoint was originally defined. */
12916
d9b3f62e 12917struct tracepoint *
00bf0b85 12918create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 12919{
409873ef 12920 char *addr_str, small_buf[100];
d9b3f62e 12921 struct tracepoint *tp;
fd9b8c24 12922
409873ef
SS
12923 if (utp->at_string)
12924 addr_str = utp->at_string;
12925 else
12926 {
12927 /* In the absence of a source location, fall back to raw
12928 address. Since there is no way to confirm that the address
12929 means the same thing as when the trace was started, warn the
12930 user. */
3e43a32a
MS
12931 warning (_("Uploaded tracepoint %d has no "
12932 "source location, using raw address"),
409873ef
SS
12933 utp->number);
12934 sprintf (small_buf, "*%s", hex_string (utp->addr));
12935 addr_str = small_buf;
12936 }
12937
12938 /* There's not much we can do with a sequence of bytecodes. */
12939 if (utp->cond && !utp->cond_string)
3e43a32a
MS
12940 warning (_("Uploaded tracepoint %d condition "
12941 "has no source form, ignoring it"),
409873ef 12942 utp->number);
d5551862 12943
8cdf0e15 12944 if (!create_breakpoint (get_current_arch (),
409873ef
SS
12945 addr_str,
12946 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 12947 0 /* tempflag */,
0fb4aa4b 12948 utp->type /* type_wanted */,
8cdf0e15
VP
12949 0 /* Ignore count */,
12950 pending_break_support,
348d480f 12951 &tracepoint_breakpoint_ops,
8cdf0e15 12952 0 /* from_tty */,
84f4c1fe
PM
12953 utp->enabled /* enabled */,
12954 0 /* internal */))
fd9b8c24
PA
12955 return NULL;
12956
00bf0b85
SS
12957 set_tracepoint_count (breakpoint_count);
12958
409873ef 12959 /* Get the tracepoint we just created. */
fd9b8c24
PA
12960 tp = get_tracepoint (tracepoint_count);
12961 gdb_assert (tp != NULL);
d5551862 12962
00bf0b85
SS
12963 if (utp->pass > 0)
12964 {
d9b3f62e 12965 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 12966
409873ef 12967 trace_pass_command (small_buf, 0);
00bf0b85
SS
12968 }
12969
409873ef
SS
12970 /* If we have uploaded versions of the original commands, set up a
12971 special-purpose "reader" function and call the usual command line
12972 reader, then pass the result to the breakpoint command-setting
12973 function. */
3149d8c1 12974 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 12975 {
409873ef 12976 struct command_line *cmd_list;
00bf0b85 12977
409873ef 12978 this_utp = utp;
3149d8c1 12979 next_cmd = 0;
d5551862 12980
409873ef
SS
12981 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12982
d9b3f62e 12983 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 12984 }
3149d8c1
SS
12985 else if (!VEC_empty (char_ptr, utp->actions)
12986 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12987 warning (_("Uploaded tracepoint %d actions "
12988 "have no source form, ignoring them"),
409873ef 12989 utp->number);
00bf0b85 12990
f196051f
SS
12991 /* Copy any status information that might be available. */
12992 tp->base.hit_count = utp->hit_count;
12993 tp->traceframe_usage = utp->traceframe_usage;
12994
00bf0b85 12995 return tp;
d9b3f62e 12996}
00bf0b85 12997
1042e4c0
SS
12998/* Print information on tracepoint number TPNUM_EXP, or all if
12999 omitted. */
13000
13001static void
e5a67952 13002tracepoints_info (char *args, int from_tty)
1042e4c0 13003{
79a45e25 13004 struct ui_out *uiout = current_uiout;
e5a67952 13005 int num_printed;
1042e4c0 13006
e5a67952 13007 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
13008
13009 if (num_printed == 0)
1042e4c0 13010 {
e5a67952 13011 if (args == NULL || *args == '\0')
d77f58be
SS
13012 ui_out_message (uiout, 0, "No tracepoints.\n");
13013 else
e5a67952 13014 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 13015 }
ad443146
SS
13016
13017 default_collect_info ();
1042e4c0
SS
13018}
13019
4a64f543 13020/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
13021 Not supported by all targets. */
13022static void
13023enable_trace_command (char *args, int from_tty)
13024{
13025 enable_command (args, from_tty);
13026}
13027
4a64f543 13028/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
13029 Not supported by all targets. */
13030static void
13031disable_trace_command (char *args, int from_tty)
13032{
13033 disable_command (args, from_tty);
13034}
13035
4a64f543 13036/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
13037static void
13038delete_trace_command (char *arg, int from_tty)
13039{
35df4500 13040 struct breakpoint *b, *b_tmp;
1042e4c0
SS
13041
13042 dont_repeat ();
13043
13044 if (arg == 0)
13045 {
13046 int breaks_to_delete = 0;
13047
13048 /* Delete all breakpoints if no argument.
13049 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
13050 have to be deleted with an explicit breakpoint number
13051 argument. */
1042e4c0 13052 ALL_TRACEPOINTS (b)
46c6471b 13053 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
13054 {
13055 breaks_to_delete = 1;
13056 break;
13057 }
1042e4c0
SS
13058
13059 /* Ask user only if there are some breakpoints to delete. */
13060 if (!from_tty
13061 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13062 {
35df4500 13063 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13064 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 13065 delete_breakpoint (b);
1042e4c0
SS
13066 }
13067 }
13068 else
51be5b68 13069 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
13070}
13071
197f0a60
TT
13072/* Helper function for trace_pass_command. */
13073
13074static void
d9b3f62e 13075trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 13076{
d9b3f62e
PA
13077 tp->pass_count = count;
13078 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
13079 if (from_tty)
13080 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 13081 tp->base.number, count);
197f0a60
TT
13082}
13083
1042e4c0
SS
13084/* Set passcount for tracepoint.
13085
13086 First command argument is passcount, second is tracepoint number.
13087 If tracepoint number omitted, apply to most recently defined.
13088 Also accepts special argument "all". */
13089
13090static void
13091trace_pass_command (char *args, int from_tty)
13092{
d9b3f62e 13093 struct tracepoint *t1;
1042e4c0 13094 unsigned int count;
1042e4c0
SS
13095
13096 if (args == 0 || *args == 0)
3e43a32a
MS
13097 error (_("passcount command requires an "
13098 "argument (count + optional TP num)"));
1042e4c0 13099
4a64f543 13100 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
13101
13102 while (*args && isspace ((int) *args))
13103 args++;
13104
13105 if (*args && strncasecmp (args, "all", 3) == 0)
13106 {
d9b3f62e
PA
13107 struct breakpoint *b;
13108
1042e4c0 13109 args += 3; /* Skip special argument "all". */
1042e4c0
SS
13110 if (*args)
13111 error (_("Junk at end of arguments."));
1042e4c0 13112
d9b3f62e 13113 ALL_TRACEPOINTS (b)
197f0a60 13114 {
d9b3f62e 13115 t1 = (struct tracepoint *) b;
197f0a60
TT
13116 trace_pass_set_count (t1, count, from_tty);
13117 }
13118 }
13119 else if (*args == '\0')
1042e4c0 13120 {
197f0a60 13121 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 13122 if (t1)
197f0a60
TT
13123 trace_pass_set_count (t1, count, from_tty);
13124 }
13125 else
13126 {
13127 struct get_number_or_range_state state;
13128
13129 init_number_or_range (&state, args);
13130 while (!state.finished)
1042e4c0 13131 {
197f0a60
TT
13132 t1 = get_tracepoint_by_number (&args, &state, 1);
13133 if (t1)
13134 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
13135 }
13136 }
1042e4c0
SS
13137}
13138
d9b3f62e 13139struct tracepoint *
1042e4c0
SS
13140get_tracepoint (int num)
13141{
13142 struct breakpoint *t;
13143
13144 ALL_TRACEPOINTS (t)
13145 if (t->number == num)
d9b3f62e 13146 return (struct tracepoint *) t;
1042e4c0
SS
13147
13148 return NULL;
13149}
13150
d5551862
SS
13151/* Find the tracepoint with the given target-side number (which may be
13152 different from the tracepoint number after disconnecting and
13153 reconnecting). */
13154
d9b3f62e 13155struct tracepoint *
d5551862
SS
13156get_tracepoint_by_number_on_target (int num)
13157{
d9b3f62e 13158 struct breakpoint *b;
d5551862 13159
d9b3f62e
PA
13160 ALL_TRACEPOINTS (b)
13161 {
13162 struct tracepoint *t = (struct tracepoint *) b;
13163
13164 if (t->number_on_target == num)
13165 return t;
13166 }
d5551862
SS
13167
13168 return NULL;
13169}
13170
1042e4c0 13171/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
13172 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13173 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 13174 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 13175struct tracepoint *
197f0a60
TT
13176get_tracepoint_by_number (char **arg,
13177 struct get_number_or_range_state *state,
13178 int optional_p)
1042e4c0
SS
13179{
13180 extern int tracepoint_count;
13181 struct breakpoint *t;
13182 int tpnum;
13183 char *instring = arg == NULL ? NULL : *arg;
13184
197f0a60
TT
13185 if (state)
13186 {
13187 gdb_assert (!state->finished);
13188 tpnum = get_number_or_range (state);
13189 }
13190 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
13191 {
13192 if (optional_p)
13193 tpnum = tracepoint_count;
13194 else
13195 error_no_arg (_("tracepoint number"));
13196 }
13197 else
197f0a60 13198 tpnum = get_number (arg);
1042e4c0
SS
13199
13200 if (tpnum <= 0)
13201 {
13202 if (instring && *instring)
13203 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
13204 instring);
13205 else
3e43a32a
MS
13206 printf_filtered (_("Tracepoint argument missing "
13207 "and no previous tracepoint\n"));
1042e4c0
SS
13208 return NULL;
13209 }
13210
13211 ALL_TRACEPOINTS (t)
13212 if (t->number == tpnum)
13213 {
d9b3f62e 13214 return (struct tracepoint *) t;
1042e4c0
SS
13215 }
13216
1042e4c0
SS
13217 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
13218 return NULL;
13219}
13220
d9b3f62e
PA
13221void
13222print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
13223{
13224 if (b->thread != -1)
13225 fprintf_unfiltered (fp, " thread %d", b->thread);
13226
13227 if (b->task != 0)
13228 fprintf_unfiltered (fp, " task %d", b->task);
13229
13230 fprintf_unfiltered (fp, "\n");
13231}
13232
6149aea9
PA
13233/* Save information on user settable breakpoints (watchpoints, etc) to
13234 a new script file named FILENAME. If FILTER is non-NULL, call it
13235 on each breakpoint and only include the ones for which it returns
13236 non-zero. */
13237
1042e4c0 13238static void
6149aea9
PA
13239save_breakpoints (char *filename, int from_tty,
13240 int (*filter) (const struct breakpoint *))
1042e4c0
SS
13241{
13242 struct breakpoint *tp;
6149aea9 13243 int any = 0;
a7bdde9e 13244 char *pathname;
1042e4c0 13245 struct cleanup *cleanup;
a7bdde9e 13246 struct ui_file *fp;
6149aea9 13247 int extra_trace_bits = 0;
1042e4c0 13248
6149aea9
PA
13249 if (filename == 0 || *filename == 0)
13250 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
13251
13252 /* See if we have anything to save. */
6149aea9 13253 ALL_BREAKPOINTS (tp)
1042e4c0 13254 {
6149aea9 13255 /* Skip internal and momentary breakpoints. */
09d682a4 13256 if (!user_breakpoint_p (tp))
6149aea9
PA
13257 continue;
13258
13259 /* If we have a filter, only save the breakpoints it accepts. */
13260 if (filter && !filter (tp))
13261 continue;
13262
13263 any = 1;
13264
13265 if (is_tracepoint (tp))
13266 {
13267 extra_trace_bits = 1;
13268
13269 /* We can stop searching. */
13270 break;
13271 }
1042e4c0 13272 }
6149aea9
PA
13273
13274 if (!any)
1042e4c0 13275 {
6149aea9 13276 warning (_("Nothing to save."));
1042e4c0
SS
13277 return;
13278 }
13279
6149aea9 13280 pathname = tilde_expand (filename);
1042e4c0 13281 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 13282 fp = gdb_fopen (pathname, "w");
059fb39f 13283 if (!fp)
6149aea9
PA
13284 error (_("Unable to open file '%s' for saving (%s)"),
13285 filename, safe_strerror (errno));
a7bdde9e 13286 make_cleanup_ui_file_delete (fp);
8bf6485c 13287
6149aea9
PA
13288 if (extra_trace_bits)
13289 save_trace_state_variables (fp);
8bf6485c 13290
6149aea9 13291 ALL_BREAKPOINTS (tp)
1042e4c0 13292 {
6149aea9 13293 /* Skip internal and momentary breakpoints. */
09d682a4 13294 if (!user_breakpoint_p (tp))
6149aea9 13295 continue;
8bf6485c 13296
6149aea9
PA
13297 /* If we have a filter, only save the breakpoints it accepts. */
13298 if (filter && !filter (tp))
13299 continue;
13300
348d480f 13301 tp->ops->print_recreate (tp, fp);
1042e4c0 13302
6149aea9
PA
13303 /* Note, we can't rely on tp->number for anything, as we can't
13304 assume the recreated breakpoint numbers will match. Use $bpnum
13305 instead. */
13306
13307 if (tp->cond_string)
13308 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
13309
13310 if (tp->ignore_count)
13311 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
13312
a7bdde9e 13313 if (tp->commands)
1042e4c0 13314 {
a7bdde9e
VP
13315 volatile struct gdb_exception ex;
13316
6149aea9 13317 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 13318
79a45e25 13319 ui_out_redirect (current_uiout, fp);
14dba4b4 13320 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 13321 {
79a45e25 13322 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 13323 }
79a45e25 13324 ui_out_redirect (current_uiout, NULL);
1042e4c0 13325
a7bdde9e
VP
13326 if (ex.reason < 0)
13327 throw_exception (ex);
1042e4c0 13328
a7bdde9e 13329 fprintf_unfiltered (fp, " end\n");
1042e4c0 13330 }
6149aea9
PA
13331
13332 if (tp->enable_state == bp_disabled)
13333 fprintf_unfiltered (fp, "disable\n");
13334
13335 /* If this is a multi-location breakpoint, check if the locations
13336 should be individually disabled. Watchpoint locations are
13337 special, and not user visible. */
13338 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13339 {
13340 struct bp_location *loc;
13341 int n = 1;
13342
13343 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
13344 if (!loc->enabled)
13345 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
13346 }
1042e4c0 13347 }
8bf6485c 13348
6149aea9 13349 if (extra_trace_bits && *default_collect)
8bf6485c
SS
13350 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
13351
1042e4c0
SS
13352 do_cleanups (cleanup);
13353 if (from_tty)
6149aea9
PA
13354 printf_filtered (_("Saved to file '%s'.\n"), filename);
13355}
13356
13357/* The `save breakpoints' command. */
13358
13359static void
13360save_breakpoints_command (char *args, int from_tty)
13361{
13362 save_breakpoints (args, from_tty, NULL);
13363}
13364
13365/* The `save tracepoints' command. */
13366
13367static void
13368save_tracepoints_command (char *args, int from_tty)
13369{
13370 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
13371}
13372
13373/* Create a vector of all tracepoints. */
13374
13375VEC(breakpoint_p) *
eeae04df 13376all_tracepoints (void)
1042e4c0
SS
13377{
13378 VEC(breakpoint_p) *tp_vec = 0;
13379 struct breakpoint *tp;
13380
13381 ALL_TRACEPOINTS (tp)
13382 {
13383 VEC_safe_push (breakpoint_p, tp_vec, tp);
13384 }
13385
13386 return tp_vec;
13387}
13388
c906108c 13389\f
4a64f543
MS
13390/* This help string is used for the break, hbreak, tbreak and thbreak
13391 commands. It is defined as a macro to prevent duplication.
13392 COMMAND should be a string constant containing the name of the
13393 command. */
31e2b00f
AS
13394#define BREAK_ARGS_HELP(command) \
13395command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
13396LOCATION may be a line number, function name, or \"*\" and an address.\n\
13397If a line number is specified, break at start of code for that line.\n\
13398If a function is specified, break at start of code for that function.\n\
13399If an address is specified, break at that exact address.\n\
dc10affe
PA
13400With no LOCATION, uses current execution address of the selected\n\
13401stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
13402\n\
13403THREADNUM is the number from \"info threads\".\n\
13404CONDITION is a boolean expression.\n\
13405\n\
d41c0fc8
PA
13406Multiple breakpoints at one place are permitted, and useful if their\n\
13407conditions are different.\n\
31e2b00f
AS
13408\n\
13409Do \"help breakpoints\" for info on other commands dealing with breakpoints."
13410
44feb3ce
TT
13411/* List of subcommands for "catch". */
13412static struct cmd_list_element *catch_cmdlist;
13413
13414/* List of subcommands for "tcatch". */
13415static struct cmd_list_element *tcatch_cmdlist;
13416
9ac4176b 13417void
44feb3ce
TT
13418add_catch_command (char *name, char *docstring,
13419 void (*sfunc) (char *args, int from_tty,
13420 struct cmd_list_element *command),
a96d9b2e
SDJ
13421 char **(*completer) (struct cmd_list_element *cmd,
13422 char *text, char *word),
44feb3ce
TT
13423 void *user_data_catch,
13424 void *user_data_tcatch)
13425{
13426 struct cmd_list_element *command;
13427
13428 command = add_cmd (name, class_breakpoint, NULL, docstring,
13429 &catch_cmdlist);
13430 set_cmd_sfunc (command, sfunc);
13431 set_cmd_context (command, user_data_catch);
a96d9b2e 13432 set_cmd_completer (command, completer);
44feb3ce
TT
13433
13434 command = add_cmd (name, class_breakpoint, NULL, docstring,
13435 &tcatch_cmdlist);
13436 set_cmd_sfunc (command, sfunc);
13437 set_cmd_context (command, user_data_tcatch);
a96d9b2e 13438 set_cmd_completer (command, completer);
44feb3ce
TT
13439}
13440
6c95b8df 13441static void
a79b8f6e 13442clear_syscall_counts (struct inferior *inf)
6c95b8df 13443{
6c95b8df
PA
13444 inf->total_syscalls_count = 0;
13445 inf->any_syscall_count = 0;
13446 VEC_free (int, inf->syscalls_counts);
13447}
13448
6149aea9
PA
13449static void
13450save_command (char *arg, int from_tty)
13451{
3e43a32a
MS
13452 printf_unfiltered (_("\"save\" must be followed by "
13453 "the name of a save subcommand.\n"));
6149aea9
PA
13454 help_list (save_cmdlist, "save ", -1, gdb_stdout);
13455}
13456
84f4c1fe
PM
13457struct breakpoint *
13458iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
13459 void *data)
13460{
35df4500 13461 struct breakpoint *b, *b_tmp;
84f4c1fe 13462
35df4500 13463 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
13464 {
13465 if ((*callback) (b, data))
13466 return b;
13467 }
13468
13469 return NULL;
13470}
13471
0574c78f
GB
13472/* Zero if any of the breakpoint's locations could be a location where
13473 functions have been inlined, nonzero otherwise. */
13474
13475static int
13476is_non_inline_function (struct breakpoint *b)
13477{
13478 /* The shared library event breakpoint is set on the address of a
13479 non-inline function. */
13480 if (b->type == bp_shlib_event)
13481 return 1;
13482
13483 return 0;
13484}
13485
13486/* Nonzero if the specified PC cannot be a location where functions
13487 have been inlined. */
13488
13489int
13490pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc)
13491{
13492 struct breakpoint *b;
13493 struct bp_location *bl;
13494
13495 ALL_BREAKPOINTS (b)
13496 {
13497 if (!is_non_inline_function (b))
13498 continue;
13499
13500 for (bl = b->loc; bl != NULL; bl = bl->next)
13501 {
13502 if (!bl->shlib_disabled
13503 && bpstat_check_location (bl, aspace, pc))
13504 return 1;
13505 }
13506 }
13507
13508 return 0;
13509}
13510
2060206e
PA
13511void
13512initialize_breakpoint_ops (void)
13513{
13514 static int initialized = 0;
13515
13516 struct breakpoint_ops *ops;
13517
13518 if (initialized)
13519 return;
13520 initialized = 1;
13521
13522 /* The breakpoint_ops structure to be inherit by all kinds of
13523 breakpoints (real breakpoints, i.e., user "break" breakpoints,
13524 internal and momentary breakpoints, etc.). */
13525 ops = &bkpt_base_breakpoint_ops;
13526 *ops = base_breakpoint_ops;
13527 ops->re_set = bkpt_re_set;
13528 ops->insert_location = bkpt_insert_location;
13529 ops->remove_location = bkpt_remove_location;
13530 ops->breakpoint_hit = bkpt_breakpoint_hit;
13531
13532 /* The breakpoint_ops structure to be used in regular breakpoints. */
13533 ops = &bkpt_breakpoint_ops;
13534 *ops = bkpt_base_breakpoint_ops;
13535 ops->re_set = bkpt_re_set;
13536 ops->resources_needed = bkpt_resources_needed;
13537 ops->print_it = bkpt_print_it;
13538 ops->print_mention = bkpt_print_mention;
13539 ops->print_recreate = bkpt_print_recreate;
13540
13541 /* Ranged breakpoints. */
13542 ops = &ranged_breakpoint_ops;
13543 *ops = bkpt_breakpoint_ops;
13544 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
13545 ops->resources_needed = resources_needed_ranged_breakpoint;
13546 ops->print_it = print_it_ranged_breakpoint;
13547 ops->print_one = print_one_ranged_breakpoint;
13548 ops->print_one_detail = print_one_detail_ranged_breakpoint;
13549 ops->print_mention = print_mention_ranged_breakpoint;
13550 ops->print_recreate = print_recreate_ranged_breakpoint;
13551
13552 /* Internal breakpoints. */
13553 ops = &internal_breakpoint_ops;
13554 *ops = bkpt_base_breakpoint_ops;
13555 ops->re_set = internal_bkpt_re_set;
13556 ops->check_status = internal_bkpt_check_status;
13557 ops->print_it = internal_bkpt_print_it;
13558 ops->print_mention = internal_bkpt_print_mention;
13559
13560 /* Momentary breakpoints. */
13561 ops = &momentary_breakpoint_ops;
13562 *ops = bkpt_base_breakpoint_ops;
13563 ops->re_set = momentary_bkpt_re_set;
13564 ops->check_status = momentary_bkpt_check_status;
13565 ops->print_it = momentary_bkpt_print_it;
13566 ops->print_mention = momentary_bkpt_print_mention;
13567
13568 /* GNU v3 exception catchpoints. */
13569 ops = &gnu_v3_exception_catchpoint_ops;
13570 *ops = bkpt_breakpoint_ops;
13571 ops->print_it = print_it_exception_catchpoint;
13572 ops->print_one = print_one_exception_catchpoint;
13573 ops->print_mention = print_mention_exception_catchpoint;
13574 ops->print_recreate = print_recreate_exception_catchpoint;
13575
13576 /* Watchpoints. */
13577 ops = &watchpoint_breakpoint_ops;
13578 *ops = base_breakpoint_ops;
3a5c3e22 13579 ops->dtor = dtor_watchpoint;
2060206e
PA
13580 ops->re_set = re_set_watchpoint;
13581 ops->insert_location = insert_watchpoint;
13582 ops->remove_location = remove_watchpoint;
13583 ops->breakpoint_hit = breakpoint_hit_watchpoint;
13584 ops->check_status = check_status_watchpoint;
13585 ops->resources_needed = resources_needed_watchpoint;
13586 ops->works_in_software_mode = works_in_software_mode_watchpoint;
13587 ops->print_it = print_it_watchpoint;
13588 ops->print_mention = print_mention_watchpoint;
13589 ops->print_recreate = print_recreate_watchpoint;
13590
13591 /* Masked watchpoints. */
13592 ops = &masked_watchpoint_breakpoint_ops;
13593 *ops = watchpoint_breakpoint_ops;
13594 ops->insert_location = insert_masked_watchpoint;
13595 ops->remove_location = remove_masked_watchpoint;
13596 ops->resources_needed = resources_needed_masked_watchpoint;
13597 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
13598 ops->print_it = print_it_masked_watchpoint;
13599 ops->print_one_detail = print_one_detail_masked_watchpoint;
13600 ops->print_mention = print_mention_masked_watchpoint;
13601 ops->print_recreate = print_recreate_masked_watchpoint;
13602
13603 /* Tracepoints. */
13604 ops = &tracepoint_breakpoint_ops;
13605 *ops = base_breakpoint_ops;
13606 ops->re_set = tracepoint_re_set;
13607 ops->breakpoint_hit = tracepoint_breakpoint_hit;
13608 ops->print_one_detail = tracepoint_print_one_detail;
13609 ops->print_mention = tracepoint_print_mention;
13610 ops->print_recreate = tracepoint_print_recreate;
13611
13612 /* Fork catchpoints. */
13613 ops = &catch_fork_breakpoint_ops;
13614 *ops = base_breakpoint_ops;
13615 ops->insert_location = insert_catch_fork;
13616 ops->remove_location = remove_catch_fork;
13617 ops->breakpoint_hit = breakpoint_hit_catch_fork;
13618 ops->print_it = print_it_catch_fork;
13619 ops->print_one = print_one_catch_fork;
13620 ops->print_mention = print_mention_catch_fork;
13621 ops->print_recreate = print_recreate_catch_fork;
13622
13623 /* Vfork catchpoints. */
13624 ops = &catch_vfork_breakpoint_ops;
13625 *ops = base_breakpoint_ops;
13626 ops->insert_location = insert_catch_vfork;
13627 ops->remove_location = remove_catch_vfork;
13628 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
13629 ops->print_it = print_it_catch_vfork;
13630 ops->print_one = print_one_catch_vfork;
13631 ops->print_mention = print_mention_catch_vfork;
13632 ops->print_recreate = print_recreate_catch_vfork;
13633
13634 /* Exec catchpoints. */
13635 ops = &catch_exec_breakpoint_ops;
13636 *ops = base_breakpoint_ops;
13637 ops->dtor = dtor_catch_exec;
13638 ops->insert_location = insert_catch_exec;
13639 ops->remove_location = remove_catch_exec;
13640 ops->breakpoint_hit = breakpoint_hit_catch_exec;
13641 ops->print_it = print_it_catch_exec;
13642 ops->print_one = print_one_catch_exec;
13643 ops->print_mention = print_mention_catch_exec;
13644 ops->print_recreate = print_recreate_catch_exec;
13645
13646 /* Syscall catchpoints. */
13647 ops = &catch_syscall_breakpoint_ops;
13648 *ops = base_breakpoint_ops;
13649 ops->dtor = dtor_catch_syscall;
13650 ops->insert_location = insert_catch_syscall;
13651 ops->remove_location = remove_catch_syscall;
13652 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
13653 ops->print_it = print_it_catch_syscall;
13654 ops->print_one = print_one_catch_syscall;
13655 ops->print_mention = print_mention_catch_syscall;
13656 ops->print_recreate = print_recreate_catch_syscall;
13657}
13658
c906108c 13659void
fba45db2 13660_initialize_breakpoint (void)
c906108c
SS
13661{
13662 struct cmd_list_element *c;
13663
2060206e
PA
13664 initialize_breakpoint_ops ();
13665
84acb35a 13666 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 13667 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 13668 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 13669
17450429
PP
13670 breakpoint_objfile_key = register_objfile_data ();
13671
c906108c
SS
13672 breakpoint_chain = 0;
13673 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
13674 before a breakpoint is set. */
13675 breakpoint_count = 0;
13676
1042e4c0
SS
13677 tracepoint_count = 0;
13678
1bedd215
AC
13679 add_com ("ignore", class_breakpoint, ignore_command, _("\
13680Set ignore-count of breakpoint number N to COUNT.\n\
13681Usage is `ignore N COUNT'."));
c906108c 13682 if (xdb_commands)
c5aa993b 13683 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 13684
1bedd215
AC
13685 add_com ("commands", class_breakpoint, commands_command, _("\
13686Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
13687Give breakpoint number as argument after \"commands\".\n\
13688With no argument, the targeted breakpoint is the last one set.\n\
13689The commands themselves follow starting on the next line.\n\
13690Type a line containing \"end\" to indicate the end of them.\n\
13691Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 13692then no output is printed when it is hit, except what the commands print."));
c906108c 13693
1bedd215
AC
13694 add_com ("condition", class_breakpoint, condition_command, _("\
13695Specify breakpoint number N to break only if COND is true.\n\
c906108c 13696Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 13697expression to be evaluated whenever breakpoint N is reached."));
c906108c 13698
1bedd215 13699 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 13700Set a temporary breakpoint.\n\
c906108c
SS
13701Like \"break\" except the breakpoint is only temporary,\n\
13702so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
13703by using \"enable delete\" on the breakpoint number.\n\
13704\n"
13705BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 13706 set_cmd_completer (c, location_completer);
c94fdfd0 13707
1bedd215 13708 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 13709Set a hardware assisted breakpoint.\n\
c906108c 13710Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
13711some target hardware may not have this support.\n\
13712\n"
13713BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 13714 set_cmd_completer (c, location_completer);
c906108c 13715
1bedd215 13716 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 13717Set a temporary hardware assisted breakpoint.\n\
c906108c 13718Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
13719so it will be deleted when hit.\n\
13720\n"
13721BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 13722 set_cmd_completer (c, location_completer);
c906108c 13723
1bedd215
AC
13724 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
13725Enable some breakpoints.\n\
c906108c
SS
13726Give breakpoint numbers (separated by spaces) as arguments.\n\
13727With no subcommand, breakpoints are enabled until you command otherwise.\n\
13728This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13729With a subcommand you can enable temporarily."),
c906108c
SS
13730 &enablelist, "enable ", 1, &cmdlist);
13731 if (xdb_commands)
1bedd215
AC
13732 add_com ("ab", class_breakpoint, enable_command, _("\
13733Enable some breakpoints.\n\
c906108c
SS
13734Give breakpoint numbers (separated by spaces) as arguments.\n\
13735With no subcommand, breakpoints are enabled until you command otherwise.\n\
13736This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13737With a subcommand you can enable temporarily."));
c906108c
SS
13738
13739 add_com_alias ("en", "enable", class_breakpoint, 1);
13740
84951ab5 13741 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 13742Enable some breakpoints.\n\
c906108c
SS
13743Give breakpoint numbers (separated by spaces) as arguments.\n\
13744This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13745May be abbreviated to simply \"enable\".\n"),
c5aa993b 13746 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 13747
1a966eab
AC
13748 add_cmd ("once", no_class, enable_once_command, _("\
13749Enable breakpoints for one hit. Give breakpoint numbers.\n\
13750If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13751 &enablebreaklist);
13752
1a966eab
AC
13753 add_cmd ("delete", no_class, enable_delete_command, _("\
13754Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13755If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13756 &enablebreaklist);
13757
1a966eab
AC
13758 add_cmd ("delete", no_class, enable_delete_command, _("\
13759Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13760If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13761 &enablelist);
13762
1a966eab
AC
13763 add_cmd ("once", no_class, enable_once_command, _("\
13764Enable breakpoints for one hit. Give breakpoint numbers.\n\
13765If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13766 &enablelist);
13767
1bedd215
AC
13768 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13769Disable some breakpoints.\n\
c906108c
SS
13770Arguments are breakpoint numbers with spaces in between.\n\
13771To disable all breakpoints, give no argument.\n\
64b9b334 13772A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
13773 &disablelist, "disable ", 1, &cmdlist);
13774 add_com_alias ("dis", "disable", class_breakpoint, 1);
13775 add_com_alias ("disa", "disable", class_breakpoint, 1);
13776 if (xdb_commands)
1bedd215
AC
13777 add_com ("sb", class_breakpoint, disable_command, _("\
13778Disable some breakpoints.\n\
c906108c
SS
13779Arguments are breakpoint numbers with spaces in between.\n\
13780To disable all breakpoints, give no argument.\n\
64b9b334 13781A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 13782
1a966eab
AC
13783 add_cmd ("breakpoints", class_alias, disable_command, _("\
13784Disable some breakpoints.\n\
c906108c
SS
13785Arguments are breakpoint numbers with spaces in between.\n\
13786To disable all breakpoints, give no argument.\n\
64b9b334 13787A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 13788This command may be abbreviated \"disable\"."),
c906108c
SS
13789 &disablelist);
13790
1bedd215
AC
13791 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13792Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13793Arguments are breakpoint numbers with spaces in between.\n\
13794To delete all breakpoints, give no argument.\n\
13795\n\
13796Also a prefix command for deletion of other GDB objects.\n\
1bedd215 13797The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
13798 &deletelist, "delete ", 1, &cmdlist);
13799 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 13800 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 13801 if (xdb_commands)
1bedd215
AC
13802 add_com ("db", class_breakpoint, delete_command, _("\
13803Delete some breakpoints.\n\
c906108c 13804Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 13805To delete all breakpoints, give no argument.\n"));
c906108c 13806
1a966eab
AC
13807 add_cmd ("breakpoints", class_alias, delete_command, _("\
13808Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13809Arguments are breakpoint numbers with spaces in between.\n\
13810To delete all breakpoints, give no argument.\n\
1a966eab 13811This command may be abbreviated \"delete\"."),
c906108c
SS
13812 &deletelist);
13813
1bedd215
AC
13814 add_com ("clear", class_breakpoint, clear_command, _("\
13815Clear breakpoint at specified line or function.\n\
c906108c
SS
13816Argument may be line number, function name, or \"*\" and an address.\n\
13817If line number is specified, all breakpoints in that line are cleared.\n\
13818If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
13819If an address is specified, breakpoints at that address are cleared.\n\
13820\n\
13821With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
13822is executing in.\n\
13823\n\
1bedd215 13824See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 13825 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 13826
1bedd215 13827 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
13828Set breakpoint at specified line or function.\n"
13829BREAK_ARGS_HELP ("break")));
5ba2abeb 13830 set_cmd_completer (c, location_completer);
c94fdfd0 13831
c906108c
SS
13832 add_com_alias ("b", "break", class_run, 1);
13833 add_com_alias ("br", "break", class_run, 1);
13834 add_com_alias ("bre", "break", class_run, 1);
13835 add_com_alias ("brea", "break", class_run, 1);
13836
7681d515
PM
13837 if (xdb_commands)
13838 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
13839
13840 if (dbx_commands)
13841 {
1bedd215
AC
13842 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13843Break in function/address or break at a line in the current file."),
c5aa993b
JM
13844 &stoplist, "stop ", 1, &cmdlist);
13845 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 13846 _("Break in function or address."), &stoplist);
c5aa993b 13847 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 13848 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
13849 add_com ("status", class_info, breakpoints_info, _("\
13850Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13851The \"Type\" column indicates one of:\n\
13852\tbreakpoint - normal breakpoint\n\
13853\twatchpoint - watchpoint\n\
13854The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13855the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13856breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13857address and file/line number respectively.\n\
13858\n\
13859Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13860are set to the address of the last breakpoint listed unless the command\n\
13861is prefixed with \"server \".\n\n\
c906108c 13862Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13863breakpoint set."));
c906108c
SS
13864 }
13865
1bedd215 13866 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 13867Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
13868The \"Type\" column indicates one of:\n\
13869\tbreakpoint - normal breakpoint\n\
13870\twatchpoint - watchpoint\n\
13871The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13872the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13873breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13874address and file/line number respectively.\n\
13875\n\
13876Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13877are set to the address of the last breakpoint listed unless the command\n\
13878is prefixed with \"server \".\n\n\
c906108c 13879Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13880breakpoint set."));
c906108c 13881
6b04bdb7
MS
13882 add_info_alias ("b", "breakpoints", 1);
13883
c906108c 13884 if (xdb_commands)
1bedd215
AC
13885 add_com ("lb", class_breakpoint, breakpoints_info, _("\
13886Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13887The \"Type\" column indicates one of:\n\
13888\tbreakpoint - normal breakpoint\n\
13889\twatchpoint - watchpoint\n\
13890The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13891the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13892breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13893address and file/line number respectively.\n\
13894\n\
13895Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13896are set to the address of the last breakpoint listed unless the command\n\
13897is prefixed with \"server \".\n\n\
c906108c 13898Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13899breakpoint set."));
c906108c 13900
1a966eab
AC
13901 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13902Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13903The \"Type\" column indicates one of:\n\
13904\tbreakpoint - normal breakpoint\n\
13905\twatchpoint - watchpoint\n\
13906\tlongjmp - internal breakpoint used to step through longjmp()\n\
13907\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13908\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
13909\tfinish - internal breakpoint used by the \"finish\" command\n\
13910The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
13911the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13912breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
13913address and file/line number respectively.\n\
13914\n\
13915Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13916are set to the address of the last breakpoint listed unless the command\n\
13917is prefixed with \"server \".\n\n\
c906108c 13918Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 13919breakpoint set."),
c906108c
SS
13920 &maintenanceinfolist);
13921
44feb3ce
TT
13922 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13923Set catchpoints to catch events."),
13924 &catch_cmdlist, "catch ",
13925 0/*allow-unknown*/, &cmdlist);
13926
13927 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13928Set temporary catchpoints to catch events."),
13929 &tcatch_cmdlist, "tcatch ",
13930 0/*allow-unknown*/, &cmdlist);
13931
13932 /* Add catch and tcatch sub-commands. */
13933 add_catch_command ("catch", _("\
13934Catch an exception, when caught.\n\
13935With an argument, catch only exceptions with the given name."),
13936 catch_catch_command,
a96d9b2e 13937 NULL,
44feb3ce
TT
13938 CATCH_PERMANENT,
13939 CATCH_TEMPORARY);
13940 add_catch_command ("throw", _("\
13941Catch an exception, when thrown.\n\
13942With an argument, catch only exceptions with the given name."),
13943 catch_throw_command,
a96d9b2e 13944 NULL,
44feb3ce
TT
13945 CATCH_PERMANENT,
13946 CATCH_TEMPORARY);
13947 add_catch_command ("fork", _("Catch calls to fork."),
13948 catch_fork_command_1,
a96d9b2e 13949 NULL,
44feb3ce
TT
13950 (void *) (uintptr_t) catch_fork_permanent,
13951 (void *) (uintptr_t) catch_fork_temporary);
13952 add_catch_command ("vfork", _("Catch calls to vfork."),
13953 catch_fork_command_1,
a96d9b2e 13954 NULL,
44feb3ce
TT
13955 (void *) (uintptr_t) catch_vfork_permanent,
13956 (void *) (uintptr_t) catch_vfork_temporary);
13957 add_catch_command ("exec", _("Catch calls to exec."),
13958 catch_exec_command_1,
a96d9b2e
SDJ
13959 NULL,
13960 CATCH_PERMANENT,
13961 CATCH_TEMPORARY);
13962 add_catch_command ("syscall", _("\
13963Catch system calls by their names and/or numbers.\n\
13964Arguments say which system calls to catch. If no arguments\n\
13965are given, every system call will be caught.\n\
13966Arguments, if given, should be one or more system call names\n\
13967(if your system supports that), or system call numbers."),
13968 catch_syscall_command_1,
13969 catch_syscall_completer,
44feb3ce
TT
13970 CATCH_PERMANENT,
13971 CATCH_TEMPORARY);
c5aa993b 13972
1bedd215
AC
13973 c = add_com ("watch", class_breakpoint, watch_command, _("\
13974Set a watchpoint for an expression.\n\
06a64a0b 13975Usage: watch [-l|-location] EXPRESSION\n\
c906108c 13976A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13977an expression changes.\n\
13978If -l or -location is given, this evaluates EXPRESSION and watches\n\
13979the memory to which it refers."));
65d12d83 13980 set_cmd_completer (c, expression_completer);
c906108c 13981
1bedd215
AC
13982 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13983Set a read watchpoint for an expression.\n\
06a64a0b 13984Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 13985A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13986an expression is read.\n\
13987If -l or -location is given, this evaluates EXPRESSION and watches\n\
13988the memory to which it refers."));
65d12d83 13989 set_cmd_completer (c, expression_completer);
c906108c 13990
1bedd215
AC
13991 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13992Set a watchpoint for an expression.\n\
06a64a0b 13993Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 13994A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13995an expression is either read or written.\n\
13996If -l or -location is given, this evaluates EXPRESSION and watches\n\
13997the memory to which it refers."));
65d12d83 13998 set_cmd_completer (c, expression_completer);
c906108c 13999
d77f58be 14000 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 14001Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 14002
920d2a44
AC
14003 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14004 respond to changes - contrary to the description. */
85c07804
AC
14005 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14006 &can_use_hw_watchpoints, _("\
14007Set debugger's willingness to use watchpoint hardware."), _("\
14008Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
14009If zero, gdb will not use hardware for new watchpoints, even if\n\
14010such is available. (However, any hardware watchpoints that were\n\
14011created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
14012hardware.)"),
14013 NULL,
920d2a44 14014 show_can_use_hw_watchpoints,
85c07804 14015 &setlist, &showlist);
c906108c
SS
14016
14017 can_use_hw_watchpoints = 1;
fa8d40ab 14018
1042e4c0
SS
14019 /* Tracepoint manipulation commands. */
14020
14021 c = add_com ("trace", class_breakpoint, trace_command, _("\
14022Set a tracepoint at specified line or function.\n\
14023\n"
14024BREAK_ARGS_HELP ("trace") "\n\
14025Do \"help tracepoints\" for info on other tracepoint commands."));
14026 set_cmd_completer (c, location_completer);
14027
14028 add_com_alias ("tp", "trace", class_alias, 0);
14029 add_com_alias ("tr", "trace", class_alias, 1);
14030 add_com_alias ("tra", "trace", class_alias, 1);
14031 add_com_alias ("trac", "trace", class_alias, 1);
14032
7a697b8d
SS
14033 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14034Set a fast tracepoint at specified line or function.\n\
14035\n"
14036BREAK_ARGS_HELP ("ftrace") "\n\
14037Do \"help tracepoints\" for info on other tracepoint commands."));
14038 set_cmd_completer (c, location_completer);
14039
0fb4aa4b
PA
14040 c = add_com ("strace", class_breakpoint, strace_command, _("\
14041Set a static tracepoint at specified line, function or marker.\n\
14042\n\
14043strace [LOCATION] [if CONDITION]\n\
14044LOCATION may be a line number, function name, \"*\" and an address,\n\
14045or -m MARKER_ID.\n\
14046If a line number is specified, probe the marker at start of code\n\
14047for that line. If a function is specified, probe the marker at start\n\
14048of code for that function. If an address is specified, probe the marker\n\
14049at that exact address. If a marker id is specified, probe the marker\n\
14050with that name. With no LOCATION, uses current execution address of\n\
14051the selected stack frame.\n\
14052Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14053This collects arbitrary user data passed in the probe point call to the\n\
14054tracing library. You can inspect it when analyzing the trace buffer,\n\
14055by printing the $_sdata variable like any other convenience variable.\n\
14056\n\
14057CONDITION is a boolean expression.\n\
14058\n\
d41c0fc8
PA
14059Multiple tracepoints at one place are permitted, and useful if their\n\
14060conditions are different.\n\
0fb4aa4b
PA
14061\n\
14062Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14063Do \"help tracepoints\" for info on other tracepoint commands."));
14064 set_cmd_completer (c, location_completer);
14065
1042e4c0 14066 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 14067Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
14068Convenience variable \"$tpnum\" contains the number of the\n\
14069last tracepoint set."));
14070
14071 add_info_alias ("tp", "tracepoints", 1);
14072
14073 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
14074Delete specified tracepoints.\n\
14075Arguments are tracepoint numbers, separated by spaces.\n\
14076No argument means delete all tracepoints."),
14077 &deletelist);
14078
14079 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14080Disable specified tracepoints.\n\
14081Arguments are tracepoint numbers, separated by spaces.\n\
14082No argument means disable all tracepoints."),
14083 &disablelist);
14084 deprecate_cmd (c, "disable");
14085
14086 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14087Enable specified tracepoints.\n\
14088Arguments are tracepoint numbers, separated by spaces.\n\
14089No argument means enable all tracepoints."),
14090 &enablelist);
14091 deprecate_cmd (c, "enable");
14092
14093 add_com ("passcount", class_trace, trace_pass_command, _("\
14094Set the passcount for a tracepoint.\n\
14095The trace will end when the tracepoint has been passed 'count' times.\n\
14096Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14097if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14098
6149aea9
PA
14099 add_prefix_cmd ("save", class_breakpoint, save_command,
14100 _("Save breakpoint definitions as a script."),
14101 &save_cmdlist, "save ",
14102 0/*allow-unknown*/, &cmdlist);
14103
14104 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14105Save current breakpoint definitions as a script.\n\
cce7e648 14106This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14107catchpoints, tracepoints). Use the 'source' command in another debug\n\
14108session to restore them."),
14109 &save_cmdlist);
14110 set_cmd_completer (c, filename_completer);
14111
14112 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14113Save current tracepoint definitions as a script.\n\
6149aea9
PA
14114Use the 'source' command in another debug session to restore them."),
14115 &save_cmdlist);
1042e4c0
SS
14116 set_cmd_completer (c, filename_completer);
14117
6149aea9
PA
14118 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
14119 deprecate_cmd (c, "save tracepoints");
14120
1bedd215 14121 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
14122Breakpoint specific settings\n\
14123Configure various breakpoint-specific variables such as\n\
1bedd215 14124pending breakpoint behavior"),
fa8d40ab
JJ
14125 &breakpoint_set_cmdlist, "set breakpoint ",
14126 0/*allow-unknown*/, &setlist);
1bedd215 14127 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
14128Breakpoint specific settings\n\
14129Configure various breakpoint-specific variables such as\n\
1bedd215 14130pending breakpoint behavior"),
fa8d40ab
JJ
14131 &breakpoint_show_cmdlist, "show breakpoint ",
14132 0/*allow-unknown*/, &showlist);
14133
7915a72c
AC
14134 add_setshow_auto_boolean_cmd ("pending", no_class,
14135 &pending_break_support, _("\
14136Set debugger's behavior regarding pending breakpoints."), _("\
14137Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14138If on, an unrecognized breakpoint location will cause gdb to create a\n\
14139pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14140an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14141user-query to see if a pending breakpoint should be created."),
2c5b56ce 14142 NULL,
920d2a44 14143 show_pending_break_support,
6e1d7d6c
AC
14144 &breakpoint_set_cmdlist,
14145 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14146
14147 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14148
14149 add_setshow_boolean_cmd ("auto-hw", no_class,
14150 &automatic_hardware_breakpoints, _("\
14151Set automatic usage of hardware breakpoints."), _("\
14152Show automatic usage of hardware breakpoints."), _("\
14153If set, the debugger will automatically use hardware breakpoints for\n\
14154breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14155a warning will be emitted for such breakpoints."),
14156 NULL,
14157 show_automatic_hardware_breakpoints,
14158 &breakpoint_set_cmdlist,
14159 &breakpoint_show_cmdlist);
74960c60 14160
33e5cbd6
PA
14161 add_setshow_enum_cmd ("always-inserted", class_support,
14162 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
14163Set mode for inserting breakpoints."), _("\
14164Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
14165When this mode is off, breakpoints are inserted in inferior when it is\n\
14166resumed, and removed when execution stops. When this mode is on,\n\
14167breakpoints are inserted immediately and removed only when the user\n\
14168deletes the breakpoint. When this mode is auto (which is the default),\n\
14169the behaviour depends on the non-stop setting (see help set non-stop).\n\
14170In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
14171behaves as if always-inserted mode is on; if gdb is controlling the\n\
14172inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
14173 NULL,
14174 &show_always_inserted_mode,
14175 &breakpoint_set_cmdlist,
14176 &breakpoint_show_cmdlist);
f1310107
TJB
14177
14178 add_com ("break-range", class_breakpoint, break_range_command, _("\
14179Set a breakpoint for an address range.\n\
14180break-range START-LOCATION, END-LOCATION\n\
14181where START-LOCATION and END-LOCATION can be one of the following:\n\
14182 LINENUM, for that line in the current file,\n\
14183 FILE:LINENUM, for that line in that file,\n\
14184 +OFFSET, for that number of lines after the current line\n\
14185 or the start of the range\n\
14186 FUNCTION, for the first line in that function,\n\
14187 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14188 *ADDRESS, for the instruction at that address.\n\
14189\n\
14190The breakpoint will stop execution of the inferior whenever it executes\n\
14191an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14192range (including START-LOCATION and END-LOCATION)."));
14193
765dc015 14194 automatic_hardware_breakpoints = 1;
f3b1572e
PA
14195
14196 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 14197}
This page took 2.10904 seconds and 4 git commands to generate.